]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/zfs_vnops.c
Illumos 5027 - zfs large block support
[mirror_zfs.git] / module / zfs / zfs_vnops.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2e528b49 23 * Copyright (c) 2013 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
26/* Portions Copyright 2007 Jeremy Teo */
428870ff 27/* Portions Copyright 2010 Robert Milkowski */
34dc7c2f 28
60101509 29
34dc7c2f
BB
30#include <sys/types.h>
31#include <sys/param.h>
32#include <sys/time.h>
33#include <sys/systm.h>
34#include <sys/sysmacros.h>
35#include <sys/resource.h>
36#include <sys/vfs.h>
37#include <sys/vfs_opreg.h>
34dc7c2f
BB
38#include <sys/file.h>
39#include <sys/stat.h>
40#include <sys/kmem.h>
41#include <sys/taskq.h>
42#include <sys/uio.h>
43#include <sys/vmsystm.h>
44#include <sys/atomic.h>
34dc7c2f 45#include <vm/pvn.h>
34dc7c2f
BB
46#include <sys/pathname.h>
47#include <sys/cmn_err.h>
48#include <sys/errno.h>
49#include <sys/unistd.h>
50#include <sys/zfs_dir.h>
51#include <sys/zfs_acl.h>
52#include <sys/zfs_ioctl.h>
53#include <sys/fs/zfs.h>
54#include <sys/dmu.h>
428870ff 55#include <sys/dmu_objset.h>
34dc7c2f
BB
56#include <sys/spa.h>
57#include <sys/txg.h>
58#include <sys/dbuf.h>
59#include <sys/zap.h>
428870ff 60#include <sys/sa.h>
34dc7c2f
BB
61#include <sys/dirent.h>
62#include <sys/policy.h>
63#include <sys/sunddi.h>
b128c09f 64#include <sys/sid.h>
bcf30822 65#include <sys/mode.h>
34dc7c2f 66#include "fs/fs_subr.h"
ebe7e575 67#include <sys/zfs_ctldir.h>
34dc7c2f 68#include <sys/zfs_fuid.h>
428870ff 69#include <sys/zfs_sa.h>
e5c39b95 70#include <sys/zfs_vnops.h>
34dc7c2f
BB
71#include <sys/dnlc.h>
72#include <sys/zfs_rlock.h>
73#include <sys/extdirent.h>
74#include <sys/kidmap.h>
428870ff 75#include <sys/cred.h>
34dc7c2f 76#include <sys/attr.h>
218b8eaf 77#include <sys/zpl.h>
34dc7c2f
BB
78
79/*
80 * Programming rules.
81 *
82 * Each vnode op performs some logical unit of work. To do this, the ZPL must
83 * properly lock its in-core state, create a DMU transaction, do the work,
84 * record this work in the intent log (ZIL), commit the DMU transaction,
85 * and wait for the intent log to commit if it is a synchronous operation.
86 * Moreover, the vnode ops must work in both normal and log replay context.
87 * The ordering of events is important to avoid deadlocks and references
88 * to freed memory. The example below illustrates the following Big Rules:
89 *
90 * (1) A check must be made in each zfs thread for a mounted file system.
3558fd73
BB
91 * This is done avoiding races using ZFS_ENTER(zsb).
92 * A ZFS_EXIT(zsb) is needed before all returns. Any znodes
34dc7c2f
BB
93 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
94 * can return EIO from the calling function.
95 *
3558fd73 96 * (2) iput() should always be the last thing except for zil_commit()
34dc7c2f
BB
97 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
98 * First, if it's the last reference, the vnode/znode
99 * can be freed, so the zp may point to freed memory. Second, the last
100 * reference will call zfs_zinactive(), which may induce a lot of work --
101 * pushing cached pages (which acquires range locks) and syncing out
102 * cached atime changes. Third, zfs_zinactive() may require a new tx,
103 * which could deadlock the system if you were already holding one.
0a50679c 104 * If you must call iput() within a tx then use zfs_iput_async().
34dc7c2f
BB
105 *
106 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
107 * as they can span dmu_tx_assign() calls.
108 *
384f8a09
MA
109 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
110 * dmu_tx_assign(). This is critical because we don't want to block
111 * while holding locks.
112 *
113 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
114 * reduces lock contention and CPU usage when we must wait (note that if
115 * throughput is constrained by the storage, nearly every transaction
116 * must wait).
117 *
118 * Note, in particular, that if a lock is sometimes acquired before
119 * the tx assigns, and sometimes after (e.g. z_lock), then failing
120 * to use a non-blocking assign can deadlock the system. The scenario:
34dc7c2f
BB
121 *
122 * Thread A has grabbed a lock before calling dmu_tx_assign().
123 * Thread B is in an already-assigned tx, and blocks for this lock.
124 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
125 * forever, because the previous txg can't quiesce until B's tx commits.
126 *
3558fd73 127 * If dmu_tx_assign() returns ERESTART and zsb->z_assign is TXG_NOWAIT,
e8b96c60
MA
128 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
129 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
130 * to indicate that this operation has already called dmu_tx_wait().
131 * This will ensure that we don't retry forever, waiting a short bit
132 * each time.
34dc7c2f
BB
133 *
134 * (5) If the operation succeeded, generate the intent log entry for it
135 * before dropping locks. This ensures that the ordering of events
136 * in the intent log matches the order in which they actually occurred.
d3cc8b15 137 * During ZIL replay the zfs_log_* functions will update the sequence
fb5f0bc8 138 * number to indicate the zil transaction has replayed.
34dc7c2f
BB
139 *
140 * (6) At the end of each vnode op, the DMU tx must always commit,
141 * regardless of whether there were any errors.
142 *
572e2857 143 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
34dc7c2f
BB
144 * to ensure that synchronous semantics are provided when necessary.
145 *
146 * In general, this is how things should be ordered in each vnode op:
147 *
3558fd73 148 * ZFS_ENTER(zsb); // exit if unmounted
34dc7c2f 149 * top:
3558fd73 150 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab())
34dc7c2f
BB
151 * rw_enter(...); // grab any other locks you need
152 * tx = dmu_tx_create(...); // get DMU tx
153 * dmu_tx_hold_*(); // hold each object you might modify
e8b96c60 154 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
155 * if (error) {
156 * rw_exit(...); // drop locks
157 * zfs_dirent_unlock(dl); // unlock directory entry
3558fd73 158 * iput(...); // release held vnodes
fb5f0bc8 159 * if (error == ERESTART) {
e8b96c60 160 * waited = B_TRUE;
34dc7c2f
BB
161 * dmu_tx_wait(tx);
162 * dmu_tx_abort(tx);
163 * goto top;
164 * }
165 * dmu_tx_abort(tx); // abort DMU tx
3558fd73 166 * ZFS_EXIT(zsb); // finished in zfs
34dc7c2f
BB
167 * return (error); // really out of space
168 * }
169 * error = do_real_work(); // do whatever this VOP does
170 * if (error == 0)
171 * zfs_log_*(...); // on success, make ZIL entry
172 * dmu_tx_commit(tx); // commit DMU tx -- error or not
173 * rw_exit(...); // drop locks
174 * zfs_dirent_unlock(dl); // unlock directory entry
3558fd73 175 * iput(...); // release held vnodes
572e2857 176 * zil_commit(zilog, foid); // synchronous when necessary
3558fd73 177 * ZFS_EXIT(zsb); // finished in zfs
34dc7c2f
BB
178 * return (error); // done, report error
179 */
180
126400a1
BB
181/*
182 * Virus scanning is unsupported. It would be possible to add a hook
183 * here to performance the required virus scan. This could be done
184 * entirely in the kernel or potentially as an update to invoke a
185 * scanning utility.
186 */
187static int
188zfs_vscan(struct inode *ip, cred_t *cr, int async)
189{
190 return (0);
191}
192
193/* ARGSUSED */
194int
195zfs_open(struct inode *ip, int mode, int flag, cred_t *cr)
196{
197 znode_t *zp = ITOZ(ip);
198 zfs_sb_t *zsb = ITOZSB(ip);
199
200 ZFS_ENTER(zsb);
201 ZFS_VERIFY_ZP(zp);
202
203 /* Honor ZFS_APPENDONLY file attribute */
204 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
205 ((flag & O_APPEND) == 0)) {
206 ZFS_EXIT(zsb);
2e528b49 207 return (SET_ERROR(EPERM));
126400a1
BB
208 }
209
210 /* Virus scan eligible files on open */
211 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
212 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
213 if (zfs_vscan(ip, cr, 0) != 0) {
214 ZFS_EXIT(zsb);
2e528b49 215 return (SET_ERROR(EACCES));
126400a1
BB
216 }
217 }
218
219 /* Keep a count of the synchronous opens in the znode */
220 if (flag & O_SYNC)
221 atomic_inc_32(&zp->z_sync_cnt);
222
223 ZFS_EXIT(zsb);
224 return (0);
225}
226EXPORT_SYMBOL(zfs_open);
227
228/* ARGSUSED */
229int
230zfs_close(struct inode *ip, int flag, cred_t *cr)
231{
232 znode_t *zp = ITOZ(ip);
233 zfs_sb_t *zsb = ITOZSB(ip);
234
235 ZFS_ENTER(zsb);
236 ZFS_VERIFY_ZP(zp);
237
7dc71949 238 /* Decrement the synchronous opens in the znode */
126400a1 239 if (flag & O_SYNC)
7dc71949 240 atomic_dec_32(&zp->z_sync_cnt);
126400a1
BB
241
242 if (!zfs_has_ctldir(zp) && zsb->z_vscan && S_ISREG(ip->i_mode) &&
243 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
244 VERIFY(zfs_vscan(ip, cr, 1) == 0);
245
246 ZFS_EXIT(zsb);
8780c539 247 return (0);
126400a1
BB
248}
249EXPORT_SYMBOL(zfs_close);
250
802e7b5f 251#if defined(SEEK_HOLE) && defined(SEEK_DATA)
cf91b2b6 252/*
802e7b5f
LD
253 * Lseek support for finding holes (cmd == SEEK_HOLE) and
254 * data (cmd == SEEK_DATA). "off" is an in/out parameter.
cf91b2b6
MA
255 */
256static int
802e7b5f 257zfs_holey_common(struct inode *ip, int cmd, loff_t *off)
cf91b2b6 258{
802e7b5f 259 znode_t *zp = ITOZ(ip);
cf91b2b6
MA
260 uint64_t noff = (uint64_t)*off; /* new offset */
261 uint64_t file_sz;
262 int error;
263 boolean_t hole;
264
265 file_sz = zp->z_size;
266 if (noff >= file_sz) {
2e528b49 267 return (SET_ERROR(ENXIO));
cf91b2b6
MA
268 }
269
802e7b5f 270 if (cmd == SEEK_HOLE)
cf91b2b6
MA
271 hole = B_TRUE;
272 else
273 hole = B_FALSE;
274
802e7b5f 275 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff);
cf91b2b6 276
d97aa48f 277 if (error == ESRCH)
2e528b49 278 return (SET_ERROR(ENXIO));
d97aa48f
MA
279
280 /*
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
286 */
287 if (noff > file_sz) {
288 ASSERT(hole);
289 noff = file_sz;
cf91b2b6
MA
290 }
291
292 if (noff < *off)
293 return (error);
294 *off = noff;
295 return (error);
296}
802e7b5f
LD
297
298int
299zfs_holey(struct inode *ip, int cmd, loff_t *off)
300{
301 znode_t *zp = ITOZ(ip);
302 zfs_sb_t *zsb = ITOZSB(ip);
303 int error;
304
305 ZFS_ENTER(zsb);
306 ZFS_VERIFY_ZP(zp);
307
308 error = zfs_holey_common(ip, cmd, off);
309
310 ZFS_EXIT(zsb);
311 return (error);
312}
313EXPORT_SYMBOL(zfs_holey);
314#endif /* SEEK_HOLE && SEEK_DATA */
cf91b2b6 315
c0d35759 316#if defined(_KERNEL)
34dc7c2f
BB
317/*
318 * When a file is memory mapped, we must keep the IO data synchronized
319 * between the DMU cache and the memory mapped pages. What this means:
320 *
321 * On Write: If we find a memory mapped page, we write to *both*
322 * the page and the dmu buffer.
34dc7c2f 323 */
d164b209 324static void
c0d35759
BB
325update_pages(struct inode *ip, int64_t start, int len,
326 objset_t *os, uint64_t oid)
34dc7c2f 327{
c0d35759
BB
328 struct address_space *mp = ip->i_mapping;
329 struct page *pp;
330 uint64_t nbytes;
d164b209 331 int64_t off;
c0d35759 332 void *pb;
34dc7c2f 333
c0d35759
BB
334 off = start & (PAGE_CACHE_SIZE-1);
335 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
336 nbytes = MIN(PAGE_CACHE_SIZE - off, len);
34dc7c2f 337
c0d35759
BB
338 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
339 if (pp) {
340 if (mapping_writably_mapped(mp))
341 flush_dcache_page(pp);
34dc7c2f 342
c0d35759
BB
343 pb = kmap(pp);
344 (void) dmu_read(os, oid, start+off, nbytes, pb+off,
9babb374 345 DMU_READ_PREFETCH);
c0d35759
BB
346 kunmap(pp);
347
348 if (mapping_writably_mapped(mp))
349 flush_dcache_page(pp);
350
351 mark_page_accessed(pp);
352 SetPageUptodate(pp);
353 ClearPageError(pp);
354 unlock_page(pp);
355 page_cache_release(pp);
34dc7c2f 356 }
c0d35759 357
d164b209 358 len -= nbytes;
34dc7c2f 359 off = 0;
34dc7c2f 360 }
34dc7c2f
BB
361}
362
363/*
364 * When a file is memory mapped, we must keep the IO data synchronized
365 * between the DMU cache and the memory mapped pages. What this means:
366 *
367 * On Read: We "read" preferentially from memory mapped pages,
368 * else we default from the dmu buffer.
369 *
370 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
d3cc8b15 371 * the file is memory mapped.
34dc7c2f
BB
372 */
373static int
3558fd73 374mappedread(struct inode *ip, int nbytes, uio_t *uio)
34dc7c2f 375{
c0d35759
BB
376 struct address_space *mp = ip->i_mapping;
377 struct page *pp;
3558fd73
BB
378 znode_t *zp = ITOZ(ip);
379 objset_t *os = ITOZSB(ip)->z_os;
34dc7c2f 380 int64_t start, off;
c0d35759 381 uint64_t bytes;
34dc7c2f
BB
382 int len = nbytes;
383 int error = 0;
c0d35759 384 void *pb;
34dc7c2f
BB
385
386 start = uio->uio_loffset;
c0d35759
BB
387 off = start & (PAGE_CACHE_SIZE-1);
388 for (start &= PAGE_CACHE_MASK; len > 0; start += PAGE_CACHE_SIZE) {
389 bytes = MIN(PAGE_CACHE_SIZE - off, len);
390
391 pp = find_lock_page(mp, start >> PAGE_CACHE_SHIFT);
392 if (pp) {
393 ASSERT(PageUptodate(pp));
394
395 pb = kmap(pp);
396 error = uiomove(pb + off, bytes, UIO_READ, uio);
397 kunmap(pp);
398
399 if (mapping_writably_mapped(mp))
400 flush_dcache_page(pp);
401
402 mark_page_accessed(pp);
403 unlock_page(pp);
404 page_cache_release(pp);
34dc7c2f
BB
405 } else {
406 error = dmu_read_uio(os, zp->z_id, uio, bytes);
407 }
c0d35759 408
34dc7c2f
BB
409 len -= bytes;
410 off = 0;
411 if (error)
412 break;
413 }
414 return (error);
415}
c0d35759 416#endif /* _KERNEL */
34dc7c2f 417
c409e464 418unsigned long zfs_read_chunk_size = 1024 * 1024; /* Tunable */
34dc7c2f
BB
419
420/*
421 * Read bytes from specified file into supplied buffer.
422 *
3558fd73 423 * IN: ip - inode of file to be read from.
34dc7c2f
BB
424 * uio - structure supplying read location, range info,
425 * and return buffer.
c0d35759
BB
426 * ioflag - FSYNC flags; used to provide FRSYNC semantics.
427 * O_DIRECT flag; used to bypass page cache.
34dc7c2f 428 * cr - credentials of caller.
34dc7c2f
BB
429 *
430 * OUT: uio - updated offset and range, buffer filled.
431 *
d3cc8b15 432 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
433 *
434 * Side Effects:
3558fd73 435 * inode - atime updated if byte count > 0
34dc7c2f
BB
436 */
437/* ARGSUSED */
e5c39b95 438int
3558fd73 439zfs_read(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
34dc7c2f 440{
3558fd73
BB
441 znode_t *zp = ITOZ(ip);
442 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
443 objset_t *os;
444 ssize_t n, nbytes;
149e873a 445 int error = 0;
34dc7c2f 446 rl_t *rl;
3558fd73 447#ifdef HAVE_UIO_ZEROCOPY
428870ff 448 xuio_t *xuio = NULL;
3558fd73 449#endif /* HAVE_UIO_ZEROCOPY */
34dc7c2f 450
3558fd73 451 ZFS_ENTER(zsb);
34dc7c2f 452 ZFS_VERIFY_ZP(zp);
3558fd73 453 os = zsb->z_os;
34dc7c2f 454
428870ff 455 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
3558fd73 456 ZFS_EXIT(zsb);
2e528b49 457 return (SET_ERROR(EACCES));
34dc7c2f
BB
458 }
459
460 /*
461 * Validate file offset
462 */
463 if (uio->uio_loffset < (offset_t)0) {
3558fd73 464 ZFS_EXIT(zsb);
2e528b49 465 return (SET_ERROR(EINVAL));
34dc7c2f
BB
466 }
467
468 /*
469 * Fasttrack empty reads
470 */
471 if (uio->uio_resid == 0) {
3558fd73 472 ZFS_EXIT(zsb);
34dc7c2f
BB
473 return (0);
474 }
475
34dc7c2f
BB
476 /*
477 * If we're in FRSYNC mode, sync out this znode before reading it.
478 */
3558fd73
BB
479 if (ioflag & FRSYNC || zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
480 zil_commit(zsb->z_log, zp->z_id);
34dc7c2f
BB
481
482 /*
483 * Lock the range against changes.
484 */
485 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
486
487 /*
488 * If we are reading past end-of-file we can skip
489 * to the end; but we might still need to set atime.
490 */
428870ff 491 if (uio->uio_loffset >= zp->z_size) {
34dc7c2f
BB
492 error = 0;
493 goto out;
494 }
495
428870ff
BB
496 ASSERT(uio->uio_loffset < zp->z_size);
497 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
498
3558fd73 499#ifdef HAVE_UIO_ZEROCOPY
428870ff
BB
500 if ((uio->uio_extflg == UIO_XUIO) &&
501 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
502 int nblk;
503 int blksz = zp->z_blksz;
504 uint64_t offset = uio->uio_loffset;
505
506 xuio = (xuio_t *)uio;
507 if ((ISP2(blksz))) {
508 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
509 blksz)) / blksz;
510 } else {
511 ASSERT(offset + n <= blksz);
512 nblk = 1;
513 }
514 (void) dmu_xuio_init(xuio, nblk);
515
3558fd73 516 if (vn_has_cached_data(ip)) {
428870ff
BB
517 /*
518 * For simplicity, we always allocate a full buffer
519 * even if we only expect to read a portion of a block.
520 */
521 while (--nblk >= 0) {
522 (void) dmu_xuio_add(xuio,
523 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
524 blksz), 0, blksz);
525 }
526 }
527 }
3558fd73 528#endif /* HAVE_UIO_ZEROCOPY */
34dc7c2f
BB
529
530 while (n > 0) {
531 nbytes = MIN(n, zfs_read_chunk_size -
532 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
533
c0d35759 534 if (zp->z_is_mapped && !(ioflag & O_DIRECT))
3558fd73 535 error = mappedread(ip, nbytes, uio);
34dc7c2f
BB
536 else
537 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
c0d35759 538
b128c09f
BB
539 if (error) {
540 /* convert checksum errors into IO errors */
541 if (error == ECKSUM)
2e528b49 542 error = SET_ERROR(EIO);
34dc7c2f 543 break;
b128c09f 544 }
34dc7c2f
BB
545
546 n -= nbytes;
547 }
34dc7c2f
BB
548out:
549 zfs_range_unlock(rl);
550
3558fd73 551 ZFS_ACCESSTIME_STAMP(zsb, zp);
3558fd73 552 ZFS_EXIT(zsb);
34dc7c2f
BB
553 return (error);
554}
e5c39b95 555EXPORT_SYMBOL(zfs_read);
34dc7c2f 556
34dc7c2f
BB
557/*
558 * Write the bytes to a file.
559 *
3558fd73 560 * IN: ip - inode of file to be written to.
34dc7c2f
BB
561 * uio - structure supplying write location, range info,
562 * and data buffer.
563 * ioflag - FAPPEND flag set if in append mode.
c0d35759 564 * O_DIRECT flag; used to bypass page cache.
34dc7c2f 565 * cr - credentials of caller.
34dc7c2f
BB
566 *
567 * OUT: uio - updated offset and range.
568 *
569 * RETURN: 0 if success
570 * error code if failure
571 *
572 * Timestamps:
3558fd73 573 * ip - ctime|mtime updated if byte count > 0
34dc7c2f 574 */
428870ff 575
34dc7c2f 576/* ARGSUSED */
e5c39b95 577int
3558fd73 578zfs_write(struct inode *ip, uio_t *uio, int ioflag, cred_t *cr)
34dc7c2f 579{
3558fd73
BB
580 znode_t *zp = ITOZ(ip);
581 rlim64_t limit = uio->uio_limit;
34dc7c2f
BB
582 ssize_t start_resid = uio->uio_resid;
583 ssize_t tx_bytes;
584 uint64_t end_size;
585 dmu_tx_t *tx;
3558fd73 586 zfs_sb_t *zsb = ZTOZSB(zp);
34dc7c2f
BB
587 zilog_t *zilog;
588 offset_t woff;
589 ssize_t n, nbytes;
590 rl_t *rl;
3558fd73
BB
591 int max_blksz = zsb->z_max_blksz;
592 int error = 0;
9babb374 593 arc_buf_t *abuf;
3558fd73 594 iovec_t *aiov = NULL;
428870ff
BB
595 xuio_t *xuio = NULL;
596 int i_iov = 0;
428870ff
BB
597 iovec_t *iovp = uio->uio_iov;
598 int write_eof;
599 int count = 0;
600 sa_bulk_attr_t bulk[4];
601 uint64_t mtime[2], ctime[2];
3558fd73 602 ASSERTV(int iovcnt = uio->uio_iovcnt);
34dc7c2f 603
34dc7c2f
BB
604 /*
605 * Fasttrack empty write
606 */
607 n = start_resid;
608 if (n == 0)
609 return (0);
610
611 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
612 limit = MAXOFFSET_T;
613
3558fd73 614 ZFS_ENTER(zsb);
34dc7c2f 615 ZFS_VERIFY_ZP(zp);
b128c09f 616
3558fd73
BB
617 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
618 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
619 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
620 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
428870ff
BB
621 &zp->z_pflags, 8);
622
b128c09f
BB
623 /*
624 * If immutable or not appending then return EPERM
625 */
428870ff
BB
626 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
627 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
628 (uio->uio_loffset < zp->z_size))) {
3558fd73 629 ZFS_EXIT(zsb);
2e528b49 630 return (SET_ERROR(EPERM));
b128c09f
BB
631 }
632
3558fd73 633 zilog = zsb->z_log;
34dc7c2f 634
428870ff
BB
635 /*
636 * Validate file offset
637 */
638 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
639 if (woff < 0) {
3558fd73 640 ZFS_EXIT(zsb);
2e528b49 641 return (SET_ERROR(EINVAL));
428870ff
BB
642 }
643
34dc7c2f
BB
644 /*
645 * Pre-fault the pages to ensure slow (eg NFS) pages
646 * don't hold up txg.
428870ff 647 * Skip this if uio contains loaned arc_buf.
34dc7c2f 648 */
9cac042c 649#ifdef HAVE_UIO_ZEROCOPY
428870ff
BB
650 if ((uio->uio_extflg == UIO_XUIO) &&
651 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
652 xuio = (xuio_t *)uio;
653 else
9cac042c 654#endif
572e2857 655 uio_prefaultpages(MIN(n, max_blksz), uio);
34dc7c2f
BB
656
657 /*
658 * If in append mode, set the io offset pointer to eof.
659 */
660 if (ioflag & FAPPEND) {
661 /*
428870ff
BB
662 * Obtain an appending range lock to guarantee file append
663 * semantics. We reset the write offset once we have the lock.
34dc7c2f
BB
664 */
665 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
428870ff 666 woff = rl->r_off;
34dc7c2f 667 if (rl->r_len == UINT64_MAX) {
428870ff
BB
668 /*
669 * We overlocked the file because this write will cause
670 * the file block size to increase.
671 * Note that zp_size cannot change with this lock held.
672 */
673 woff = zp->z_size;
34dc7c2f 674 }
428870ff 675 uio->uio_loffset = woff;
34dc7c2f 676 } else {
34dc7c2f 677 /*
428870ff
BB
678 * Note that if the file block size will change as a result of
679 * this write, then this range lock will lock the entire file
680 * so that we can re-write the block safely.
34dc7c2f
BB
681 */
682 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
683 }
684
685 if (woff >= limit) {
686 zfs_range_unlock(rl);
3558fd73 687 ZFS_EXIT(zsb);
2e528b49 688 return (SET_ERROR(EFBIG));
34dc7c2f
BB
689 }
690
691 if ((woff + n) > limit || woff > (limit - n))
692 n = limit - woff;
693
428870ff
BB
694 /* Will this write extend the file length? */
695 write_eof = (woff + n > zp->z_size);
696
697 end_size = MAX(zp->z_size, woff + n);
34dc7c2f
BB
698
699 /*
700 * Write the file in reasonable size chunks. Each chunk is written
701 * in a separate transaction; this keeps the intent log records small
702 * and allows us to do more fine-grained space accounting.
703 */
704 while (n > 0) {
9babb374
BB
705 abuf = NULL;
706 woff = uio->uio_loffset;
3558fd73
BB
707 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
708 zfs_owner_overquota(zsb, zp, B_TRUE)) {
9babb374
BB
709 if (abuf != NULL)
710 dmu_return_arcbuf(abuf);
2e528b49 711 error = SET_ERROR(EDQUOT);
9babb374
BB
712 break;
713 }
714
428870ff
BB
715 if (xuio && abuf == NULL) {
716 ASSERT(i_iov < iovcnt);
717 aiov = &iovp[i_iov];
718 abuf = dmu_xuio_arcbuf(xuio, i_iov);
719 dmu_xuio_clear(xuio, i_iov);
428870ff
BB
720 ASSERT((aiov->iov_base == abuf->b_data) ||
721 ((char *)aiov->iov_base - (char *)abuf->b_data +
722 aiov->iov_len == arc_buf_size(abuf)));
723 i_iov++;
724 } else if (abuf == NULL && n >= max_blksz &&
725 woff >= zp->z_size &&
9babb374
BB
726 P2PHASE(woff, max_blksz) == 0 &&
727 zp->z_blksz == max_blksz) {
428870ff
BB
728 /*
729 * This write covers a full block. "Borrow" a buffer
730 * from the dmu so that we can fill it before we enter
731 * a transaction. This avoids the possibility of
732 * holding up the transaction if the data copy hangs
733 * up on a pagefault (e.g., from an NFS server mapping).
734 */
9babb374
BB
735 size_t cbytes;
736
428870ff
BB
737 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
738 max_blksz);
9babb374
BB
739 ASSERT(abuf != NULL);
740 ASSERT(arc_buf_size(abuf) == max_blksz);
149e873a
BB
741 if ((error = uiocopy(abuf->b_data, max_blksz,
742 UIO_WRITE, uio, &cbytes))) {
9babb374
BB
743 dmu_return_arcbuf(abuf);
744 break;
745 }
746 ASSERT(cbytes == max_blksz);
747 }
748
34dc7c2f
BB
749 /*
750 * Start a transaction.
751 */
3558fd73 752 tx = dmu_tx_create(zsb->z_os);
428870ff 753 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
34dc7c2f 754 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
428870ff 755 zfs_sa_upgrade_txholds(tx, zp);
384f8a09 756 error = dmu_tx_assign(tx, TXG_WAIT);
34dc7c2f 757 if (error) {
34dc7c2f 758 dmu_tx_abort(tx);
9babb374
BB
759 if (abuf != NULL)
760 dmu_return_arcbuf(abuf);
34dc7c2f
BB
761 break;
762 }
763
764 /*
765 * If zfs_range_lock() over-locked we grow the blocksize
766 * and then reduce the lock range. This will only happen
767 * on the first iteration since zfs_range_reduce() will
768 * shrink down r_len to the appropriate size.
769 */
770 if (rl->r_len == UINT64_MAX) {
771 uint64_t new_blksz;
772
773 if (zp->z_blksz > max_blksz) {
f1512ee6
MA
774 /*
775 * File's blocksize is already larger than the
776 * "recordsize" property. Only let it grow to
777 * the next power of 2.
778 */
34dc7c2f 779 ASSERT(!ISP2(zp->z_blksz));
f1512ee6
MA
780 new_blksz = MIN(end_size,
781 1 << highbit64(zp->z_blksz));
34dc7c2f
BB
782 } else {
783 new_blksz = MIN(end_size, max_blksz);
784 }
785 zfs_grow_blocksize(zp, new_blksz, tx);
786 zfs_range_reduce(rl, woff, n);
787 }
788
789 /*
790 * XXX - should we really limit each write to z_max_blksz?
791 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
792 */
793 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
34dc7c2f 794
9babb374
BB
795 if (abuf == NULL) {
796 tx_bytes = uio->uio_resid;
428870ff
BB
797 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
798 uio, nbytes, tx);
9babb374
BB
799 tx_bytes -= uio->uio_resid;
800 } else {
801 tx_bytes = nbytes;
428870ff
BB
802 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
803 /*
804 * If this is not a full block write, but we are
805 * extending the file past EOF and this data starts
806 * block-aligned, use assign_arcbuf(). Otherwise,
807 * write via dmu_write().
808 */
809 if (tx_bytes < max_blksz && (!write_eof ||
810 aiov->iov_base != abuf->b_data)) {
811 ASSERT(xuio);
3558fd73 812 dmu_write(zsb->z_os, zp->z_id, woff,
428870ff
BB
813 aiov->iov_len, aiov->iov_base, tx);
814 dmu_return_arcbuf(abuf);
815 xuio_stat_wbuf_copied();
816 } else {
817 ASSERT(xuio || tx_bytes == max_blksz);
818 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
819 woff, abuf, tx);
820 }
9babb374
BB
821 ASSERT(tx_bytes <= uio->uio_resid);
822 uioskip(uio, tx_bytes);
823 }
c0d35759
BB
824
825 if (tx_bytes && zp->z_is_mapped && !(ioflag & O_DIRECT))
826 update_pages(ip, woff, tx_bytes, zsb->z_os, zp->z_id);
34dc7c2f
BB
827
828 /*
829 * If we made no progress, we're done. If we made even
830 * partial progress, update the znode and ZIL accordingly.
831 */
832 if (tx_bytes == 0) {
3558fd73 833 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
428870ff 834 (void *)&zp->z_size, sizeof (uint64_t), tx);
34dc7c2f
BB
835 dmu_tx_commit(tx);
836 ASSERT(error != 0);
837 break;
838 }
839
840 /*
841 * Clear Set-UID/Set-GID bits on successful write if not
842 * privileged and at least one of the excute bits is set.
843 *
844 * It would be nice to to this after all writes have
845 * been done, but that would still expose the ISUID/ISGID
846 * to another app after the partial write is committed.
847 *
572e2857
BB
848 * Note: we don't call zfs_fuid_map_id() here because
849 * user 0 is not an ephemeral uid.
34dc7c2f
BB
850 */
851 mutex_enter(&zp->z_acl_lock);
428870ff 852 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
34dc7c2f 853 (S_IXUSR >> 6))) != 0 &&
428870ff 854 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
34dc7c2f 855 secpolicy_vnode_setid_retain(cr,
428870ff
BB
856 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
857 uint64_t newmode;
858 zp->z_mode &= ~(S_ISUID | S_ISGID);
859 newmode = zp->z_mode;
3558fd73 860 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zsb),
428870ff 861 (void *)&newmode, sizeof (uint64_t), tx);
34dc7c2f
BB
862 }
863 mutex_exit(&zp->z_acl_lock);
864
428870ff
BB
865 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
866 B_TRUE);
34dc7c2f
BB
867
868 /*
869 * Update the file size (zp_size) if it has changed;
870 * account for possible concurrent updates.
871 */
428870ff
BB
872 while ((end_size = zp->z_size) < uio->uio_loffset) {
873 (void) atomic_cas_64(&zp->z_size, end_size,
34dc7c2f 874 uio->uio_loffset);
428870ff
BB
875 ASSERT(error == 0);
876 }
572e2857
BB
877 /*
878 * If we are replaying and eof is non zero then force
879 * the file size to the specified eof. Note, there's no
880 * concurrency during replay.
881 */
3558fd73
BB
882 if (zsb->z_replay && zsb->z_replay_eof != 0)
883 zp->z_size = zsb->z_replay_eof;
572e2857 884
428870ff
BB
885 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
886
119a394a
ED
887 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag,
888 NULL, NULL);
34dc7c2f
BB
889 dmu_tx_commit(tx);
890
891 if (error != 0)
892 break;
893 ASSERT(tx_bytes == nbytes);
894 n -= nbytes;
572e2857
BB
895
896 if (!xuio && n > 0)
897 uio_prefaultpages(MIN(n, max_blksz), uio);
34dc7c2f
BB
898 }
899
900 zfs_range_unlock(rl);
901
902 /*
903 * If we're in replay mode, or we made no progress, return error.
904 * Otherwise, it's at least a partial write, so it's successful.
905 */
3558fd73
BB
906 if (zsb->z_replay || uio->uio_resid == start_resid) {
907 ZFS_EXIT(zsb);
34dc7c2f
BB
908 return (error);
909 }
910
428870ff 911 if (ioflag & (FSYNC | FDSYNC) ||
3558fd73 912 zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 913 zil_commit(zilog, zp->z_id);
34dc7c2f 914
960e08fe 915 zfs_inode_update(zp);
3558fd73 916 ZFS_EXIT(zsb);
34dc7c2f
BB
917 return (0);
918}
e5c39b95 919EXPORT_SYMBOL(zfs_write);
34dc7c2f 920
0a50679c
BB
921void
922zfs_iput_async(struct inode *ip)
3558fd73 923{
0a50679c
BB
924 objset_t *os = ITOZSB(ip)->z_os;
925
3558fd73 926 ASSERT(atomic_read(&ip->i_count) > 0);
0a50679c
BB
927 ASSERT(os != NULL);
928
3558fd73 929 if (atomic_read(&ip->i_count) == 1)
0a50679c 930 taskq_dispatch(dsl_pool_iput_taskq(dmu_objset_pool(os)),
79c76d5b 931 (task_func_t *)iput, ip, TQ_SLEEP);
3558fd73
BB
932 else
933 iput(ip);
934}
935
34dc7c2f 936void
428870ff 937zfs_get_done(zgd_t *zgd, int error)
34dc7c2f 938{
428870ff 939 znode_t *zp = zgd->zgd_private;
428870ff
BB
940
941 if (zgd->zgd_db)
942 dmu_buf_rele(zgd->zgd_db, zgd);
943
944 zfs_range_unlock(zgd->zgd_rl);
34dc7c2f 945
9babb374
BB
946 /*
947 * Release the vnode asynchronously as we currently have the
948 * txg stopped from syncing.
949 */
0a50679c 950 zfs_iput_async(ZTOI(zp));
428870ff
BB
951
952 if (error == 0 && zgd->zgd_bp)
953 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
954
34dc7c2f
BB
955 kmem_free(zgd, sizeof (zgd_t));
956}
957
45d1cae3
BB
958#ifdef DEBUG
959static int zil_fault_io = 0;
960#endif
961
34dc7c2f
BB
962/*
963 * Get data to generate a TX_WRITE intent log record.
964 */
965int
966zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
967{
3558fd73
BB
968 zfs_sb_t *zsb = arg;
969 objset_t *os = zsb->z_os;
34dc7c2f 970 znode_t *zp;
428870ff
BB
971 uint64_t object = lr->lr_foid;
972 uint64_t offset = lr->lr_offset;
973 uint64_t size = lr->lr_length;
974 blkptr_t *bp = &lr->lr_blkptr;
34dc7c2f 975 dmu_buf_t *db;
34dc7c2f 976 zgd_t *zgd;
34dc7c2f
BB
977 int error = 0;
978
428870ff
BB
979 ASSERT(zio != NULL);
980 ASSERT(size != 0);
34dc7c2f
BB
981
982 /*
983 * Nothing to do if the file has been removed
984 */
3558fd73 985 if (zfs_zget(zsb, object, &zp) != 0)
2e528b49 986 return (SET_ERROR(ENOENT));
34dc7c2f 987 if (zp->z_unlinked) {
9babb374
BB
988 /*
989 * Release the vnode asynchronously as we currently have the
990 * txg stopped from syncing.
991 */
0a50679c 992 zfs_iput_async(ZTOI(zp));
2e528b49 993 return (SET_ERROR(ENOENT));
34dc7c2f
BB
994 }
995
79c76d5b 996 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
3558fd73 997 zgd->zgd_zilog = zsb->z_log;
428870ff
BB
998 zgd->zgd_private = zp;
999
34dc7c2f
BB
1000 /*
1001 * Write records come in two flavors: immediate and indirect.
1002 * For small writes it's cheaper to store the data with the
1003 * log record (immediate); for large writes it's cheaper to
1004 * sync the data and get a pointer to it (indirect) so that
1005 * we don't have to write the data twice.
1006 */
1007 if (buf != NULL) { /* immediate write */
428870ff 1008 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
34dc7c2f 1009 /* test for truncation needs to be done while range locked */
428870ff 1010 if (offset >= zp->z_size) {
2e528b49 1011 error = SET_ERROR(ENOENT);
428870ff
BB
1012 } else {
1013 error = dmu_read(os, object, offset, size, buf,
1014 DMU_READ_NO_PREFETCH);
34dc7c2f 1015 }
428870ff 1016 ASSERT(error == 0 || error == ENOENT);
34dc7c2f 1017 } else { /* indirect write */
34dc7c2f
BB
1018 /*
1019 * Have to lock the whole block to ensure when it's
1020 * written out and it's checksum is being calculated
1021 * that no one can change the data. We need to re-check
1022 * blocksize after we get the lock in case it's changed!
1023 */
1024 for (;;) {
428870ff
BB
1025 uint64_t blkoff;
1026 size = zp->z_blksz;
1027 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1028 offset -= blkoff;
1029 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1030 RL_READER);
1031 if (zp->z_blksz == size)
34dc7c2f 1032 break;
428870ff
BB
1033 offset += blkoff;
1034 zfs_range_unlock(zgd->zgd_rl);
34dc7c2f
BB
1035 }
1036 /* test for truncation needs to be done while range locked */
428870ff 1037 if (lr->lr_offset >= zp->z_size)
2e528b49 1038 error = SET_ERROR(ENOENT);
45d1cae3
BB
1039#ifdef DEBUG
1040 if (zil_fault_io) {
2e528b49 1041 error = SET_ERROR(EIO);
45d1cae3 1042 zil_fault_io = 0;
45d1cae3 1043 }
45d1cae3 1044#endif
34dc7c2f 1045 if (error == 0)
428870ff
BB
1046 error = dmu_buf_hold(os, object, offset, zgd, &db,
1047 DMU_READ_NO_PREFETCH);
1048
1049 if (error == 0) {
03c6040b
GW
1050 blkptr_t *obp = dmu_buf_get_blkptr(db);
1051 if (obp) {
1052 ASSERT(BP_IS_HOLE(bp));
1053 *bp = *obp;
1054 }
1055
428870ff
BB
1056 zgd->zgd_db = db;
1057 zgd->zgd_bp = bp;
1058
1059 ASSERT(db->db_offset == offset);
1060 ASSERT(db->db_size == size);
1061
1062 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1063 zfs_get_done, zgd);
1064 ASSERT(error || lr->lr_length <= zp->z_blksz);
1065
1066 /*
1067 * On success, we need to wait for the write I/O
1068 * initiated by dmu_sync() to complete before we can
1069 * release this dbuf. We will finish everything up
1070 * in the zfs_get_done() callback.
1071 */
1072 if (error == 0)
1073 return (0);
1074
1075 if (error == EALREADY) {
1076 lr->lr_common.lrc_txtype = TX_WRITE2;
1077 error = 0;
1078 }
1079 }
34dc7c2f 1080 }
428870ff
BB
1081
1082 zfs_get_done(zgd, error);
1083
34dc7c2f
BB
1084 return (error);
1085}
1086
1087/*ARGSUSED*/
3558fd73
BB
1088int
1089zfs_access(struct inode *ip, int mode, int flag, cred_t *cr)
34dc7c2f 1090{
3558fd73
BB
1091 znode_t *zp = ITOZ(ip);
1092 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
1093 int error;
1094
3558fd73 1095 ZFS_ENTER(zsb);
34dc7c2f
BB
1096 ZFS_VERIFY_ZP(zp);
1097
1098 if (flag & V_ACE_MASK)
1099 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1100 else
1101 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1102
3558fd73 1103 ZFS_EXIT(zsb);
45d1cae3
BB
1104 return (error);
1105}
3558fd73 1106EXPORT_SYMBOL(zfs_access);
45d1cae3 1107
34dc7c2f
BB
1108/*
1109 * Lookup an entry in a directory, or an extended attribute directory.
3558fd73 1110 * If it exists, return a held inode reference for it.
34dc7c2f 1111 *
3558fd73 1112 * IN: dip - inode of directory to search.
34dc7c2f 1113 * nm - name of entry to lookup.
34dc7c2f 1114 * flags - LOOKUP_XATTR set if looking for an attribute.
34dc7c2f 1115 * cr - credentials of caller.
34dc7c2f
BB
1116 * direntflags - directory lookup flags
1117 * realpnp - returned pathname.
1118 *
3558fd73 1119 * OUT: ipp - inode of located entry, NULL if not found.
34dc7c2f 1120 *
d3cc8b15 1121 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
1122 *
1123 * Timestamps:
1124 * NA
1125 */
1126/* ARGSUSED */
e5c39b95 1127int
3558fd73
BB
1128zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags,
1129 cred_t *cr, int *direntflags, pathname_t *realpnp)
34dc7c2f 1130{
3558fd73
BB
1131 znode_t *zdp = ITOZ(dip);
1132 zfs_sb_t *zsb = ITOZSB(dip);
1133 int error = 0;
45d1cae3
BB
1134
1135 /* fast path */
1136 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1137
3558fd73 1138 if (!S_ISDIR(dip->i_mode)) {
2e528b49 1139 return (SET_ERROR(ENOTDIR));
428870ff 1140 } else if (zdp->z_sa_hdl == NULL) {
2e528b49 1141 return (SET_ERROR(EIO));
45d1cae3
BB
1142 }
1143
1144 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1145 error = zfs_fastaccesschk_execute(zdp, cr);
1146 if (!error) {
3558fd73
BB
1147 *ipp = dip;
1148 igrab(*ipp);
45d1cae3
BB
1149 return (0);
1150 }
1151 return (error);
3558fd73 1152#ifdef HAVE_DNLC
45d1cae3
BB
1153 } else {
1154 vnode_t *tvp = dnlc_lookup(dvp, nm);
1155
1156 if (tvp) {
1157 error = zfs_fastaccesschk_execute(zdp, cr);
1158 if (error) {
3558fd73 1159 iput(tvp);
45d1cae3
BB
1160 return (error);
1161 }
1162 if (tvp == DNLC_NO_VNODE) {
3558fd73 1163 iput(tvp);
2e528b49 1164 return (SET_ERROR(ENOENT));
45d1cae3
BB
1165 } else {
1166 *vpp = tvp;
1167 return (specvp_check(vpp, cr));
1168 }
1169 }
3558fd73 1170#endif /* HAVE_DNLC */
45d1cae3
BB
1171 }
1172 }
1173
3558fd73 1174 ZFS_ENTER(zsb);
34dc7c2f
BB
1175 ZFS_VERIFY_ZP(zdp);
1176
3558fd73 1177 *ipp = NULL;
34dc7c2f
BB
1178
1179 if (flags & LOOKUP_XATTR) {
34dc7c2f
BB
1180 /*
1181 * We don't allow recursive attributes..
1182 * Maybe someday we will.
1183 */
428870ff 1184 if (zdp->z_pflags & ZFS_XATTR) {
3558fd73 1185 ZFS_EXIT(zsb);
2e528b49 1186 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1187 }
1188
3558fd73
BB
1189 if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) {
1190 ZFS_EXIT(zsb);
34dc7c2f
BB
1191 return (error);
1192 }
1193
1194 /*
1195 * Do we have permission to get into attribute directory?
1196 */
1197
3558fd73 1198 if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0,
149e873a 1199 B_FALSE, cr))) {
3558fd73
BB
1200 iput(*ipp);
1201 *ipp = NULL;
34dc7c2f
BB
1202 }
1203
3558fd73 1204 ZFS_EXIT(zsb);
34dc7c2f
BB
1205 return (error);
1206 }
1207
3558fd73
BB
1208 if (!S_ISDIR(dip->i_mode)) {
1209 ZFS_EXIT(zsb);
2e528b49 1210 return (SET_ERROR(ENOTDIR));
34dc7c2f
BB
1211 }
1212
1213 /*
1214 * Check accessibility of directory.
1215 */
1216
149e873a 1217 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) {
3558fd73 1218 ZFS_EXIT(zsb);
34dc7c2f
BB
1219 return (error);
1220 }
1221
3558fd73 1222 if (zsb->z_utf8 && u8_validate(nm, strlen(nm),
34dc7c2f 1223 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 1224 ZFS_EXIT(zsb);
2e528b49 1225 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
1226 }
1227
3558fd73
BB
1228 error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp);
1229 if ((error == 0) && (*ipp))
1230 zfs_inode_update(ITOZ(*ipp));
34dc7c2f 1231
3558fd73 1232 ZFS_EXIT(zsb);
34dc7c2f
BB
1233 return (error);
1234}
e5c39b95 1235EXPORT_SYMBOL(zfs_lookup);
34dc7c2f
BB
1236
1237/*
1238 * Attempt to create a new entry in a directory. If the entry
1239 * already exists, truncate the file if permissible, else return
3558fd73 1240 * an error. Return the ip of the created or trunc'd file.
34dc7c2f 1241 *
3558fd73 1242 * IN: dip - inode of directory to put new file entry in.
34dc7c2f
BB
1243 * name - name of new file entry.
1244 * vap - attributes of new file.
1245 * excl - flag indicating exclusive or non-exclusive mode.
1246 * mode - mode to open file with.
1247 * cr - credentials of caller.
1248 * flag - large file flag [UNUSED].
3558fd73 1249 * vsecp - ACL to be set
34dc7c2f 1250 *
3558fd73 1251 * OUT: ipp - inode of created or trunc'd entry.
34dc7c2f 1252 *
d3cc8b15 1253 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
1254 *
1255 * Timestamps:
3558fd73
BB
1256 * dip - ctime|mtime updated if new entry created
1257 * ip - ctime|mtime always, atime if new
34dc7c2f
BB
1258 */
1259
1260/* ARGSUSED */
e5c39b95 1261int
3558fd73
BB
1262zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl,
1263 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp)
34dc7c2f 1264{
3558fd73
BB
1265 znode_t *zp, *dzp = ITOZ(dip);
1266 zfs_sb_t *zsb = ITOZSB(dip);
34dc7c2f
BB
1267 zilog_t *zilog;
1268 objset_t *os;
1269 zfs_dirlock_t *dl;
1270 dmu_tx_t *tx;
1271 int error;
b128c09f 1272 uid_t uid;
149e873a 1273 gid_t gid;
428870ff 1274 zfs_acl_ids_t acl_ids;
9babb374 1275 boolean_t fuid_dirtied;
428870ff 1276 boolean_t have_acl = B_FALSE;
e8b96c60 1277 boolean_t waited = B_FALSE;
34dc7c2f
BB
1278
1279 /*
1280 * If we have an ephemeral id, ACL, or XVATTR then
1281 * make sure file system is at proper version
1282 */
1283
149e873a 1284 gid = crgetgid(cr);
3558fd73 1285 uid = crgetuid(cr);
b128c09f 1286
3558fd73
BB
1287 if (zsb->z_use_fuids == B_FALSE &&
1288 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2e528b49 1289 return (SET_ERROR(EINVAL));
34dc7c2f 1290
3558fd73 1291 ZFS_ENTER(zsb);
34dc7c2f 1292 ZFS_VERIFY_ZP(dzp);
3558fd73
BB
1293 os = zsb->z_os;
1294 zilog = zsb->z_log;
34dc7c2f 1295
3558fd73 1296 if (zsb->z_utf8 && u8_validate(name, strlen(name),
34dc7c2f 1297 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 1298 ZFS_EXIT(zsb);
2e528b49 1299 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
1300 }
1301
5484965a 1302 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f 1303 if ((error = secpolicy_xvattr((xvattr_t *)vap,
3558fd73
BB
1304 crgetuid(cr), cr, vap->va_mode)) != 0) {
1305 ZFS_EXIT(zsb);
34dc7c2f
BB
1306 return (error);
1307 }
1308 }
34dc7c2f 1309
3558fd73
BB
1310top:
1311 *ipp = NULL;
34dc7c2f
BB
1312 if (*name == '\0') {
1313 /*
1314 * Null component name refers to the directory itself.
1315 */
3558fd73 1316 igrab(dip);
34dc7c2f
BB
1317 zp = dzp;
1318 dl = NULL;
1319 error = 0;
1320 } else {
3558fd73 1321 /* possible igrab(zp) */
34dc7c2f
BB
1322 int zflg = 0;
1323
1324 if (flag & FIGNORECASE)
1325 zflg |= ZCILOOK;
1326
1327 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1328 NULL, NULL);
1329 if (error) {
572e2857
BB
1330 if (have_acl)
1331 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1332 if (strcmp(name, "..") == 0)
2e528b49 1333 error = SET_ERROR(EISDIR);
3558fd73 1334 ZFS_EXIT(zsb);
34dc7c2f
BB
1335 return (error);
1336 }
1337 }
428870ff 1338
34dc7c2f
BB
1339 if (zp == NULL) {
1340 uint64_t txtype;
1341
1342 /*
1343 * Create a new file object and update the directory
1344 * to reference it.
1345 */
149e873a 1346 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
572e2857
BB
1347 if (have_acl)
1348 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
1349 goto out;
1350 }
1351
1352 /*
1353 * We only support the creation of regular files in
1354 * extended attribute directories.
1355 */
428870ff 1356
3558fd73 1357 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) {
572e2857
BB
1358 if (have_acl)
1359 zfs_acl_ids_free(&acl_ids);
2e528b49 1360 error = SET_ERROR(EINVAL);
34dc7c2f
BB
1361 goto out;
1362 }
1363
428870ff
BB
1364 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1365 cr, vsecp, &acl_ids)) != 0)
9babb374 1366 goto out;
428870ff
BB
1367 have_acl = B_TRUE;
1368
3558fd73 1369 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
45d1cae3 1370 zfs_acl_ids_free(&acl_ids);
2e528b49 1371 error = SET_ERROR(EDQUOT);
9babb374
BB
1372 goto out;
1373 }
1374
34dc7c2f 1375 tx = dmu_tx_create(os);
428870ff
BB
1376
1377 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1378 ZFS_SA_BASE_ATTR_SIZE);
1379
3558fd73 1380 fuid_dirtied = zsb->z_fuid_dirty;
9babb374 1381 if (fuid_dirtied)
3558fd73 1382 zfs_fuid_txhold(zsb, tx);
34dc7c2f 1383 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
428870ff 1384 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3558fd73 1385 if (!zsb->z_use_sa &&
428870ff 1386 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
34dc7c2f 1387 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
428870ff 1388 0, acl_ids.z_aclp->z_acl_bytes);
34dc7c2f 1389 }
e8b96c60 1390 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
1391 if (error) {
1392 zfs_dirent_unlock(dl);
fb5f0bc8 1393 if (error == ERESTART) {
e8b96c60 1394 waited = B_TRUE;
34dc7c2f
BB
1395 dmu_tx_wait(tx);
1396 dmu_tx_abort(tx);
1397 goto top;
1398 }
428870ff 1399 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1400 dmu_tx_abort(tx);
3558fd73 1401 ZFS_EXIT(zsb);
34dc7c2f
BB
1402 return (error);
1403 }
428870ff 1404 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
9babb374
BB
1405
1406 if (fuid_dirtied)
3558fd73 1407 zfs_fuid_sync(zsb, tx);
9babb374 1408
34dc7c2f
BB
1409 (void) zfs_link_create(dl, zp, tx, ZNEW);
1410 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1411 if (flag & FIGNORECASE)
1412 txtype |= TX_CI;
1413 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
9babb374
BB
1414 vsecp, acl_ids.z_fuidp, vap);
1415 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
1416 dmu_tx_commit(tx);
1417 } else {
1418 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1419
572e2857
BB
1420 if (have_acl)
1421 zfs_acl_ids_free(&acl_ids);
1422 have_acl = B_FALSE;
1423
34dc7c2f
BB
1424 /*
1425 * A directory entry already exists for this name.
1426 */
1427 /*
1428 * Can't truncate an existing file if in exclusive mode.
1429 */
3558fd73 1430 if (excl) {
2e528b49 1431 error = SET_ERROR(EEXIST);
34dc7c2f
BB
1432 goto out;
1433 }
1434 /*
1435 * Can't open a directory for writing.
1436 */
3558fd73 1437 if (S_ISDIR(ZTOI(zp)->i_mode)) {
2e528b49 1438 error = SET_ERROR(EISDIR);
34dc7c2f
BB
1439 goto out;
1440 }
1441 /*
1442 * Verify requested access to file.
1443 */
1444 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1445 goto out;
1446 }
1447
1448 mutex_enter(&dzp->z_lock);
1449 dzp->z_seq++;
1450 mutex_exit(&dzp->z_lock);
1451
1452 /*
1453 * Truncate regular files if requested.
1454 */
3558fd73
BB
1455 if (S_ISREG(ZTOI(zp)->i_mode) &&
1456 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) {
b128c09f
BB
1457 /* we can't hold any locks when calling zfs_freesp() */
1458 zfs_dirent_unlock(dl);
1459 dl = NULL;
34dc7c2f 1460 error = zfs_freesp(zp, 0, 0, mode, TRUE);
34dc7c2f
BB
1461 }
1462 }
1463out:
1464
1465 if (dl)
1466 zfs_dirent_unlock(dl);
1467
1468 if (error) {
1469 if (zp)
3558fd73 1470 iput(ZTOI(zp));
34dc7c2f 1471 } else {
960e08fe
BB
1472 zfs_inode_update(dzp);
1473 zfs_inode_update(zp);
3558fd73 1474 *ipp = ZTOI(zp);
34dc7c2f 1475 }
34dc7c2f 1476
3558fd73 1477 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1478 zil_commit(zilog, 0);
428870ff 1479
3558fd73 1480 ZFS_EXIT(zsb);
34dc7c2f
BB
1481 return (error);
1482}
e5c39b95 1483EXPORT_SYMBOL(zfs_create);
34dc7c2f
BB
1484
1485/*
1486 * Remove an entry from a directory.
1487 *
3558fd73 1488 * IN: dip - inode of directory to remove entry from.
34dc7c2f
BB
1489 * name - name of entry to remove.
1490 * cr - credentials of caller.
34dc7c2f
BB
1491 *
1492 * RETURN: 0 if success
1493 * error code if failure
1494 *
1495 * Timestamps:
3558fd73
BB
1496 * dip - ctime|mtime
1497 * ip - ctime (if nlink > 0)
34dc7c2f 1498 */
428870ff
BB
1499
1500uint64_t null_xattr = 0;
1501
34dc7c2f 1502/*ARGSUSED*/
e5c39b95 1503int
3558fd73 1504zfs_remove(struct inode *dip, char *name, cred_t *cr)
34dc7c2f 1505{
3558fd73 1506 znode_t *zp, *dzp = ITOZ(dip);
572e2857 1507 znode_t *xzp;
3558fd73
BB
1508 struct inode *ip;
1509 zfs_sb_t *zsb = ITOZSB(dip);
34dc7c2f 1510 zilog_t *zilog;
3558fd73
BB
1511 uint64_t xattr_obj;
1512 uint64_t xattr_obj_unlinked = 0;
572e2857 1513 uint64_t obj = 0;
34dc7c2f
BB
1514 zfs_dirlock_t *dl;
1515 dmu_tx_t *tx;
3558fd73 1516 boolean_t unlinked;
34dc7c2f
BB
1517 uint64_t txtype;
1518 pathname_t *realnmp = NULL;
3558fd73 1519#ifdef HAVE_PN_UTILS
34dc7c2f 1520 pathname_t realnm;
3558fd73 1521#endif /* HAVE_PN_UTILS */
34dc7c2f
BB
1522 int error;
1523 int zflg = ZEXISTS;
e8b96c60 1524 boolean_t waited = B_FALSE;
34dc7c2f 1525
3558fd73 1526 ZFS_ENTER(zsb);
34dc7c2f 1527 ZFS_VERIFY_ZP(dzp);
3558fd73 1528 zilog = zsb->z_log;
34dc7c2f 1529
3558fd73 1530#ifdef HAVE_PN_UTILS
34dc7c2f
BB
1531 if (flags & FIGNORECASE) {
1532 zflg |= ZCILOOK;
1533 pn_alloc(&realnm);
1534 realnmp = &realnm;
1535 }
3558fd73 1536#endif /* HAVE_PN_UTILS */
34dc7c2f
BB
1537
1538top:
572e2857
BB
1539 xattr_obj = 0;
1540 xzp = NULL;
34dc7c2f
BB
1541 /*
1542 * Attempt to lock directory; fail if entry doesn't exist.
1543 */
149e873a
BB
1544 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1545 NULL, realnmp))) {
3558fd73 1546#ifdef HAVE_PN_UTILS
34dc7c2f
BB
1547 if (realnmp)
1548 pn_free(realnmp);
3558fd73
BB
1549#endif /* HAVE_PN_UTILS */
1550 ZFS_EXIT(zsb);
34dc7c2f
BB
1551 return (error);
1552 }
1553
3558fd73 1554 ip = ZTOI(zp);
34dc7c2f 1555
149e873a 1556 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
34dc7c2f
BB
1557 goto out;
1558 }
1559
1560 /*
1561 * Need to use rmdir for removing directories.
1562 */
3558fd73 1563 if (S_ISDIR(ip->i_mode)) {
2e528b49 1564 error = SET_ERROR(EPERM);
34dc7c2f
BB
1565 goto out;
1566 }
1567
3558fd73 1568#ifdef HAVE_DNLC
34dc7c2f
BB
1569 if (realnmp)
1570 dnlc_remove(dvp, realnmp->pn_buf);
1571 else
1572 dnlc_remove(dvp, name);
3558fd73 1573#endif /* HAVE_DNLC */
34dc7c2f
BB
1574
1575 /*
3558fd73
BB
1576 * We never delete the znode and always place it in the unlinked
1577 * set. The dentry cache will always hold the last reference and
1578 * is responsible for safely freeing the znode.
34dc7c2f 1579 */
572e2857 1580 obj = zp->z_id;
3558fd73 1581 tx = dmu_tx_create(zsb->z_os);
34dc7c2f 1582 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
428870ff
BB
1583 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1584 zfs_sa_upgrade_txholds(tx, zp);
1585 zfs_sa_upgrade_txholds(tx, dzp);
34dc7c2f
BB
1586
1587 /* are there any extended attributes? */
3558fd73 1588 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
428870ff 1589 &xattr_obj, sizeof (xattr_obj));
572e2857 1590 if (error == 0 && xattr_obj) {
3558fd73 1591 error = zfs_zget(zsb, xattr_obj, &xzp);
c99c9001 1592 ASSERT0(error);
428870ff
BB
1593 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1594 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
1595 }
1596
34dc7c2f 1597 /* charge as an update -- would be nice not to charge at all */
3558fd73 1598 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
34dc7c2f 1599
e8b96c60 1600 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
1601 if (error) {
1602 zfs_dirent_unlock(dl);
3558fd73 1603 iput(ip);
572e2857 1604 if (xzp)
3558fd73 1605 iput(ZTOI(xzp));
fb5f0bc8 1606 if (error == ERESTART) {
e8b96c60 1607 waited = B_TRUE;
34dc7c2f
BB
1608 dmu_tx_wait(tx);
1609 dmu_tx_abort(tx);
1610 goto top;
1611 }
3558fd73 1612#ifdef HAVE_PN_UTILS
34dc7c2f
BB
1613 if (realnmp)
1614 pn_free(realnmp);
3558fd73 1615#endif /* HAVE_PN_UTILS */
34dc7c2f 1616 dmu_tx_abort(tx);
3558fd73 1617 ZFS_EXIT(zsb);
34dc7c2f
BB
1618 return (error);
1619 }
1620
1621 /*
1622 * Remove the directory entry.
1623 */
1624 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1625
1626 if (error) {
1627 dmu_tx_commit(tx);
1628 goto out;
1629 }
1630
1631 if (unlinked) {
572e2857
BB
1632 /*
1633 * Hold z_lock so that we can make sure that the ACL obj
1634 * hasn't changed. Could have been deleted due to
1635 * zfs_sa_upgrade().
1636 */
1637 mutex_enter(&zp->z_lock);
3558fd73 1638 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
428870ff 1639 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
572e2857 1640 mutex_exit(&zp->z_lock);
34dc7c2f
BB
1641 zfs_unlinked_add(zp, tx);
1642 }
1643
1644 txtype = TX_REMOVE;
3558fd73 1645#ifdef HAVE_PN_UTILS
34dc7c2f
BB
1646 if (flags & FIGNORECASE)
1647 txtype |= TX_CI;
3558fd73 1648#endif /* HAVE_PN_UTILS */
572e2857 1649 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
34dc7c2f
BB
1650
1651 dmu_tx_commit(tx);
1652out:
3558fd73 1653#ifdef HAVE_PN_UTILS
34dc7c2f
BB
1654 if (realnmp)
1655 pn_free(realnmp);
3558fd73 1656#endif /* HAVE_PN_UTILS */
34dc7c2f
BB
1657
1658 zfs_dirent_unlock(dl);
960e08fe
BB
1659 zfs_inode_update(dzp);
1660 zfs_inode_update(zp);
1661 if (xzp)
1662 zfs_inode_update(xzp);
34dc7c2f 1663
3558fd73 1664 iput(ip);
428870ff 1665 if (xzp)
3558fd73 1666 iput(ZTOI(xzp));
428870ff 1667
3558fd73 1668 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1669 zil_commit(zilog, 0);
34dc7c2f 1670
3558fd73 1671 ZFS_EXIT(zsb);
34dc7c2f
BB
1672 return (error);
1673}
e5c39b95 1674EXPORT_SYMBOL(zfs_remove);
34dc7c2f
BB
1675
1676/*
3558fd73 1677 * Create a new directory and insert it into dip using the name
34dc7c2f
BB
1678 * provided. Return a pointer to the inserted directory.
1679 *
3558fd73 1680 * IN: dip - inode of directory to add subdir to.
34dc7c2f
BB
1681 * dirname - name of new directory.
1682 * vap - attributes of new directory.
1683 * cr - credentials of caller.
34dc7c2f
BB
1684 * vsecp - ACL to be set
1685 *
3558fd73 1686 * OUT: ipp - inode of created directory.
34dc7c2f
BB
1687 *
1688 * RETURN: 0 if success
1689 * error code if failure
1690 *
1691 * Timestamps:
3558fd73
BB
1692 * dip - ctime|mtime updated
1693 * ipp - ctime|mtime|atime updated
34dc7c2f
BB
1694 */
1695/*ARGSUSED*/
e5c39b95 1696int
3558fd73
BB
1697zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp,
1698 cred_t *cr, int flags, vsecattr_t *vsecp)
34dc7c2f 1699{
3558fd73
BB
1700 znode_t *zp, *dzp = ITOZ(dip);
1701 zfs_sb_t *zsb = ITOZSB(dip);
34dc7c2f
BB
1702 zilog_t *zilog;
1703 zfs_dirlock_t *dl;
1704 uint64_t txtype;
1705 dmu_tx_t *tx;
1706 int error;
34dc7c2f 1707 int zf = ZNEW;
b128c09f
BB
1708 uid_t uid;
1709 gid_t gid = crgetgid(cr);
428870ff 1710 zfs_acl_ids_t acl_ids;
9babb374 1711 boolean_t fuid_dirtied;
e8b96c60 1712 boolean_t waited = B_FALSE;
34dc7c2f 1713
3558fd73 1714 ASSERT(S_ISDIR(vap->va_mode));
34dc7c2f
BB
1715
1716 /*
1717 * If we have an ephemeral id, ACL, or XVATTR then
1718 * make sure file system is at proper version
1719 */
1720
3558fd73
BB
1721 uid = crgetuid(cr);
1722 if (zsb->z_use_fuids == B_FALSE &&
1723 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
2e528b49 1724 return (SET_ERROR(EINVAL));
34dc7c2f 1725
3558fd73 1726 ZFS_ENTER(zsb);
34dc7c2f 1727 ZFS_VERIFY_ZP(dzp);
3558fd73 1728 zilog = zsb->z_log;
34dc7c2f 1729
428870ff 1730 if (dzp->z_pflags & ZFS_XATTR) {
3558fd73 1731 ZFS_EXIT(zsb);
2e528b49 1732 return (SET_ERROR(EINVAL));
34dc7c2f
BB
1733 }
1734
3558fd73 1735 if (zsb->z_utf8 && u8_validate(dirname,
34dc7c2f 1736 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 1737 ZFS_EXIT(zsb);
2e528b49 1738 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
1739 }
1740 if (flags & FIGNORECASE)
1741 zf |= ZCILOOK;
1742
5484965a 1743 if (vap->va_mask & ATTR_XVATTR) {
34dc7c2f 1744 if ((error = secpolicy_xvattr((xvattr_t *)vap,
3558fd73
BB
1745 crgetuid(cr), cr, vap->va_mode)) != 0) {
1746 ZFS_EXIT(zsb);
34dc7c2f
BB
1747 return (error);
1748 }
428870ff 1749 }
34dc7c2f 1750
428870ff
BB
1751 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1752 vsecp, &acl_ids)) != 0) {
3558fd73 1753 ZFS_EXIT(zsb);
428870ff
BB
1754 return (error);
1755 }
34dc7c2f
BB
1756 /*
1757 * First make sure the new directory doesn't exist.
428870ff
BB
1758 *
1759 * Existence is checked first to make sure we don't return
1760 * EACCES instead of EEXIST which can cause some applications
1761 * to fail.
34dc7c2f
BB
1762 */
1763top:
3558fd73 1764 *ipp = NULL;
34dc7c2f 1765
149e873a
BB
1766 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1767 NULL, NULL))) {
428870ff 1768 zfs_acl_ids_free(&acl_ids);
3558fd73 1769 ZFS_EXIT(zsb);
34dc7c2f
BB
1770 return (error);
1771 }
1772
149e873a 1773 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) {
428870ff 1774 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1775 zfs_dirent_unlock(dl);
3558fd73 1776 ZFS_EXIT(zsb);
34dc7c2f
BB
1777 return (error);
1778 }
1779
3558fd73 1780 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
45d1cae3 1781 zfs_acl_ids_free(&acl_ids);
9babb374 1782 zfs_dirent_unlock(dl);
3558fd73 1783 ZFS_EXIT(zsb);
2e528b49 1784 return (SET_ERROR(EDQUOT));
9babb374
BB
1785 }
1786
34dc7c2f
BB
1787 /*
1788 * Add a new entry to the directory.
1789 */
3558fd73 1790 tx = dmu_tx_create(zsb->z_os);
34dc7c2f
BB
1791 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1792 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
3558fd73 1793 fuid_dirtied = zsb->z_fuid_dirty;
9babb374 1794 if (fuid_dirtied)
3558fd73
BB
1795 zfs_fuid_txhold(zsb, tx);
1796 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
428870ff
BB
1797 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1798 acl_ids.z_aclp->z_acl_bytes);
1799 }
1800
1801 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1802 ZFS_SA_BASE_ATTR_SIZE);
1803
e8b96c60 1804 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
1805 if (error) {
1806 zfs_dirent_unlock(dl);
fb5f0bc8 1807 if (error == ERESTART) {
e8b96c60 1808 waited = B_TRUE;
34dc7c2f
BB
1809 dmu_tx_wait(tx);
1810 dmu_tx_abort(tx);
1811 goto top;
1812 }
428870ff 1813 zfs_acl_ids_free(&acl_ids);
34dc7c2f 1814 dmu_tx_abort(tx);
3558fd73 1815 ZFS_EXIT(zsb);
34dc7c2f
BB
1816 return (error);
1817 }
1818
1819 /*
1820 * Create new node.
1821 */
428870ff 1822 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
34dc7c2f 1823
9babb374 1824 if (fuid_dirtied)
3558fd73 1825 zfs_fuid_sync(zsb, tx);
428870ff 1826
34dc7c2f
BB
1827 /*
1828 * Now put new name in parent dir.
1829 */
1830 (void) zfs_link_create(dl, zp, tx, ZNEW);
1831
3558fd73 1832 *ipp = ZTOI(zp);
34dc7c2f
BB
1833
1834 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1835 if (flags & FIGNORECASE)
1836 txtype |= TX_CI;
9babb374
BB
1837 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1838 acl_ids.z_fuidp, vap);
34dc7c2f 1839
9babb374 1840 zfs_acl_ids_free(&acl_ids);
428870ff 1841
34dc7c2f
BB
1842 dmu_tx_commit(tx);
1843
1844 zfs_dirent_unlock(dl);
1845
3558fd73 1846 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1847 zil_commit(zilog, 0);
428870ff 1848
960e08fe
BB
1849 zfs_inode_update(dzp);
1850 zfs_inode_update(zp);
3558fd73 1851 ZFS_EXIT(zsb);
34dc7c2f
BB
1852 return (0);
1853}
e5c39b95 1854EXPORT_SYMBOL(zfs_mkdir);
34dc7c2f
BB
1855
1856/*
1857 * Remove a directory subdir entry. If the current working
1858 * directory is the same as the subdir to be removed, the
1859 * remove will fail.
1860 *
3558fd73 1861 * IN: dip - inode of directory to remove from.
34dc7c2f 1862 * name - name of directory to be removed.
3558fd73 1863 * cwd - inode of current working directory.
34dc7c2f 1864 * cr - credentials of caller.
34dc7c2f
BB
1865 * flags - case flags
1866 *
d3cc8b15 1867 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
1868 *
1869 * Timestamps:
3558fd73 1870 * dip - ctime|mtime updated
34dc7c2f
BB
1871 */
1872/*ARGSUSED*/
e5c39b95 1873int
3558fd73
BB
1874zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr,
1875 int flags)
34dc7c2f 1876{
3558fd73 1877 znode_t *dzp = ITOZ(dip);
34dc7c2f 1878 znode_t *zp;
3558fd73
BB
1879 struct inode *ip;
1880 zfs_sb_t *zsb = ITOZSB(dip);
34dc7c2f
BB
1881 zilog_t *zilog;
1882 zfs_dirlock_t *dl;
1883 dmu_tx_t *tx;
1884 int error;
1885 int zflg = ZEXISTS;
e8b96c60 1886 boolean_t waited = B_FALSE;
34dc7c2f 1887
3558fd73 1888 ZFS_ENTER(zsb);
34dc7c2f 1889 ZFS_VERIFY_ZP(dzp);
3558fd73 1890 zilog = zsb->z_log;
34dc7c2f
BB
1891
1892 if (flags & FIGNORECASE)
1893 zflg |= ZCILOOK;
1894top:
1895 zp = NULL;
1896
1897 /*
1898 * Attempt to lock directory; fail if entry doesn't exist.
1899 */
149e873a
BB
1900 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1901 NULL, NULL))) {
3558fd73 1902 ZFS_EXIT(zsb);
34dc7c2f
BB
1903 return (error);
1904 }
1905
3558fd73 1906 ip = ZTOI(zp);
34dc7c2f 1907
149e873a 1908 if ((error = zfs_zaccess_delete(dzp, zp, cr))) {
34dc7c2f
BB
1909 goto out;
1910 }
1911
3558fd73 1912 if (!S_ISDIR(ip->i_mode)) {
2e528b49 1913 error = SET_ERROR(ENOTDIR);
34dc7c2f
BB
1914 goto out;
1915 }
1916
3558fd73 1917 if (ip == cwd) {
2e528b49 1918 error = SET_ERROR(EINVAL);
34dc7c2f
BB
1919 goto out;
1920 }
1921
34dc7c2f
BB
1922 /*
1923 * Grab a lock on the directory to make sure that noone is
1924 * trying to add (or lookup) entries while we are removing it.
1925 */
1926 rw_enter(&zp->z_name_lock, RW_WRITER);
1927
1928 /*
1929 * Grab a lock on the parent pointer to make sure we play well
1930 * with the treewalk and directory rename code.
1931 */
1932 rw_enter(&zp->z_parent_lock, RW_WRITER);
1933
3558fd73 1934 tx = dmu_tx_create(zsb->z_os);
34dc7c2f 1935 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
428870ff 1936 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3558fd73 1937 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
428870ff
BB
1938 zfs_sa_upgrade_txholds(tx, zp);
1939 zfs_sa_upgrade_txholds(tx, dzp);
e8b96c60 1940 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
1941 if (error) {
1942 rw_exit(&zp->z_parent_lock);
1943 rw_exit(&zp->z_name_lock);
1944 zfs_dirent_unlock(dl);
3558fd73 1945 iput(ip);
fb5f0bc8 1946 if (error == ERESTART) {
e8b96c60 1947 waited = B_TRUE;
34dc7c2f
BB
1948 dmu_tx_wait(tx);
1949 dmu_tx_abort(tx);
1950 goto top;
1951 }
1952 dmu_tx_abort(tx);
3558fd73 1953 ZFS_EXIT(zsb);
34dc7c2f
BB
1954 return (error);
1955 }
1956
1957 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1958
1959 if (error == 0) {
1960 uint64_t txtype = TX_RMDIR;
1961 if (flags & FIGNORECASE)
1962 txtype |= TX_CI;
572e2857 1963 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
34dc7c2f
BB
1964 }
1965
1966 dmu_tx_commit(tx);
1967
1968 rw_exit(&zp->z_parent_lock);
1969 rw_exit(&zp->z_name_lock);
1970out:
1971 zfs_dirent_unlock(dl);
1972
59157910
BB
1973 zfs_inode_update(dzp);
1974 zfs_inode_update(zp);
3558fd73 1975 iput(ip);
34dc7c2f 1976
3558fd73 1977 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 1978 zil_commit(zilog, 0);
428870ff 1979
3558fd73 1980 ZFS_EXIT(zsb);
34dc7c2f
BB
1981 return (error);
1982}
e5c39b95 1983EXPORT_SYMBOL(zfs_rmdir);
34dc7c2f
BB
1984
1985/*
1986 * Read as many directory entries as will fit into the provided
3558fd73 1987 * dirent buffer from the given directory cursor position.
34dc7c2f 1988 *
3558fd73
BB
1989 * IN: ip - inode of directory to read.
1990 * dirent - buffer for directory entries.
34dc7c2f 1991 *
3558fd73 1992 * OUT: dirent - filler buffer of directory entries.
34dc7c2f
BB
1993 *
1994 * RETURN: 0 if success
1995 * error code if failure
1996 *
1997 * Timestamps:
3558fd73 1998 * ip - atime updated
34dc7c2f
BB
1999 *
2000 * Note that the low 4 bits of the cookie returned by zap is always zero.
2001 * This allows us to use the low range for "special" directory entries:
2002 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2003 * we use the offset 2 for the '.zfs' directory.
2004 */
2005/* ARGSUSED */
3558fd73 2006int
0f37d0c8 2007zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr)
34dc7c2f 2008{
3558fd73
BB
2009 znode_t *zp = ITOZ(ip);
2010 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f 2011 objset_t *os;
34dc7c2f
BB
2012 zap_cursor_t zc;
2013 zap_attribute_t zap;
34dc7c2f
BB
2014 int error;
2015 uint8_t prefetch;
c12e3a59 2016 uint8_t type;
3558fd73
BB
2017 int done = 0;
2018 uint64_t parent;
c12e3a59 2019 uint64_t offset; /* must be unsigned; checks for < 1 */
34dc7c2f 2020
3558fd73 2021 ZFS_ENTER(zsb);
34dc7c2f
BB
2022 ZFS_VERIFY_ZP(zp);
2023
3558fd73
BB
2024 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb),
2025 &parent, sizeof (parent))) != 0)
2026 goto out;
34dc7c2f
BB
2027
2028 /*
2029 * Quit if directory has been removed (posix)
2030 */
3558fd73
BB
2031 if (zp->z_unlinked)
2032 goto out;
2033
c12e3a59 2034 error = 0;
3558fd73 2035 os = zsb->z_os;
c12e3a59 2036 offset = ctx->pos;
34dc7c2f
BB
2037 prefetch = zp->z_zn_prefetch;
2038
2039 /*
2040 * Initialize the iterator cursor.
2041 */
c12e3a59 2042 if (offset <= 3) {
34dc7c2f
BB
2043 /*
2044 * Start iteration from the beginning of the directory.
2045 */
2046 zap_cursor_init(&zc, os, zp->z_id);
2047 } else {
2048 /*
2049 * The offset is a serialized cursor.
2050 */
c12e3a59 2051 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
34dc7c2f
BB
2052 }
2053
34dc7c2f
BB
2054 /*
2055 * Transform to file-system independent format
2056 */
3558fd73
BB
2057 while (!done) {
2058 uint64_t objnum;
34dc7c2f
BB
2059 /*
2060 * Special case `.', `..', and `.zfs'.
2061 */
c12e3a59 2062 if (offset == 0) {
34dc7c2f
BB
2063 (void) strcpy(zap.za_name, ".");
2064 zap.za_normalization_conflict = 0;
2065 objnum = zp->z_id;
c12e3a59
RY
2066 type = DT_DIR;
2067 } else if (offset == 1) {
34dc7c2f
BB
2068 (void) strcpy(zap.za_name, "..");
2069 zap.za_normalization_conflict = 0;
428870ff 2070 objnum = parent;
c12e3a59
RY
2071 type = DT_DIR;
2072 } else if (offset == 2 && zfs_show_ctldir(zp)) {
34dc7c2f
BB
2073 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2074 zap.za_normalization_conflict = 0;
2075 objnum = ZFSCTL_INO_ROOT;
c12e3a59 2076 type = DT_DIR;
34dc7c2f
BB
2077 } else {
2078 /*
2079 * Grab next entry.
2080 */
3558fd73
BB
2081 if ((error = zap_cursor_retrieve(&zc, &zap))) {
2082 if (error == ENOENT)
34dc7c2f
BB
2083 break;
2084 else
2085 goto update;
2086 }
2087
0c5dde49
BB
2088 /*
2089 * Allow multiple entries provided the first entry is
2090 * the object id. Non-zpl consumers may safely make
2091 * use of the additional space.
2092 *
2093 * XXX: This should be a feature flag for compatibility
2094 */
34dc7c2f 2095 if (zap.za_integer_length != 8 ||
0c5dde49 2096 zap.za_num_integers == 0) {
34dc7c2f 2097 cmn_err(CE_WARN, "zap_readdir: bad directory "
0c5dde49
BB
2098 "entry, obj = %lld, offset = %lld, "
2099 "length = %d, num = %lld\n",
34dc7c2f 2100 (u_longlong_t)zp->z_id,
c12e3a59 2101 (u_longlong_t)offset,
0c5dde49
BB
2102 zap.za_integer_length,
2103 (u_longlong_t)zap.za_num_integers);
2e528b49 2104 error = SET_ERROR(ENXIO);
34dc7c2f
BB
2105 goto update;
2106 }
2107
2108 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
c12e3a59 2109 type = ZFS_DIRENT_TYPE(zap.za_first_integer);
34dc7c2f 2110 }
0f37d0c8
RY
2111
2112 done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name),
c12e3a59 2113 objnum, type);
0f37d0c8 2114 if (done)
34dc7c2f 2115 break;
34dc7c2f
BB
2116
2117 /* Prefetch znode */
3558fd73 2118 if (prefetch) {
34dc7c2f 2119 dmu_prefetch(os, objnum, 0, 0);
3558fd73 2120 }
34dc7c2f 2121
c12e3a59
RY
2122 /*
2123 * Move to the next entry, fill in the previous offset.
2124 */
2125 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
34dc7c2f 2126 zap_cursor_advance(&zc);
c12e3a59 2127 offset = zap_cursor_serialize(&zc);
34dc7c2f 2128 } else {
c12e3a59 2129 offset += 1;
34dc7c2f 2130 }
c12e3a59 2131 ctx->pos = offset;
34dc7c2f
BB
2132 }
2133 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2134
34dc7c2f
BB
2135update:
2136 zap_cursor_fini(&zc);
34dc7c2f
BB
2137 if (error == ENOENT)
2138 error = 0;
2139
3558fd73 2140 ZFS_ACCESSTIME_STAMP(zsb, zp);
3558fd73
BB
2141
2142out:
2143 ZFS_EXIT(zsb);
34dc7c2f 2144
34dc7c2f
BB
2145 return (error);
2146}
3558fd73 2147EXPORT_SYMBOL(zfs_readdir);
34dc7c2f 2148
d5446cfc
BB
2149ulong_t zfs_fsync_sync_cnt = 4;
2150
e5c39b95 2151int
3558fd73 2152zfs_fsync(struct inode *ip, int syncflag, cred_t *cr)
34dc7c2f 2153{
3558fd73
BB
2154 znode_t *zp = ITOZ(ip);
2155 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f 2156
d5446cfc
BB
2157 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2158
3558fd73
BB
2159 if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) {
2160 ZFS_ENTER(zsb);
428870ff 2161 ZFS_VERIFY_ZP(zp);
3558fd73
BB
2162 zil_commit(zsb->z_log, zp->z_id);
2163 ZFS_EXIT(zsb);
428870ff 2164 }
07012da6
CC
2165 tsd_set(zfs_fsyncer_key, NULL);
2166
34dc7c2f
BB
2167 return (0);
2168}
e5c39b95 2169EXPORT_SYMBOL(zfs_fsync);
34dc7c2f
BB
2170
2171
2172/*
2173 * Get the requested file attributes and place them in the provided
2174 * vattr structure.
2175 *
3558fd73 2176 * IN: ip - inode of file.
5484965a
BB
2177 * vap - va_mask identifies requested attributes.
2178 * If ATTR_XVATTR set, then optional attrs are requested
34dc7c2f
BB
2179 * flags - ATTR_NOACLCHECK (CIFS server context)
2180 * cr - credentials of caller.
34dc7c2f 2181 *
5484965a
BB
2182 * OUT: vap - attribute values.
2183 *
2184 * RETURN: 0 (always succeeds)
34dc7c2f
BB
2185 */
2186/* ARGSUSED */
e5c39b95 2187int
5484965a 2188zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
34dc7c2f 2189{
3558fd73
BB
2190 znode_t *zp = ITOZ(ip);
2191 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
2192 int error = 0;
2193 uint64_t links;
428870ff 2194 uint64_t mtime[2], ctime[2];
5484965a
BB
2195 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2196 xoptattr_t *xoap = NULL;
34dc7c2f 2197 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
428870ff
BB
2198 sa_bulk_attr_t bulk[2];
2199 int count = 0;
34dc7c2f 2200
3558fd73 2201 ZFS_ENTER(zsb);
34dc7c2f 2202 ZFS_VERIFY_ZP(zp);
428870ff 2203
5484965a 2204 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
572e2857 2205
3558fd73
BB
2206 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
2207 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
428870ff
BB
2208
2209 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
3558fd73 2210 ZFS_EXIT(zsb);
428870ff
BB
2211 return (error);
2212 }
34dc7c2f 2213
34dc7c2f
BB
2214 /*
2215 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2216 * Also, if we are the owner don't bother, since owner should
2217 * always be allowed to read basic attributes of file.
2218 */
572e2857 2219 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
5484965a 2220 (vap->va_uid != crgetuid(cr))) {
149e873a
BB
2221 if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2222 skipaclchk, cr))) {
3558fd73 2223 ZFS_EXIT(zsb);
34dc7c2f
BB
2224 return (error);
2225 }
2226 }
2227
2228 /*
2229 * Return all attributes. It's cheaper to provide the answer
2230 * than to determine whether we were asked the question.
2231 */
2232
9babb374 2233 mutex_enter(&zp->z_lock);
5484965a
BB
2234 vap->va_type = vn_mode_to_vtype(zp->z_mode);
2235 vap->va_mode = zp->z_mode;
53cf50e0 2236 vap->va_fsid = ZTOI(zp)->i_sb->s_dev;
5484965a 2237 vap->va_nodeid = zp->z_id;
3558fd73 2238 if ((zp->z_id == zsb->z_root) && zfs_show_ctldir(zp))
428870ff 2239 links = zp->z_links + 1;
34dc7c2f 2240 else
428870ff 2241 links = zp->z_links;
5484965a
BB
2242 vap->va_nlink = MIN(links, ZFS_LINK_MAX);
2243 vap->va_size = i_size_read(ip);
2244 vap->va_rdev = ip->i_rdev;
2245 vap->va_seq = ip->i_generation;
2246
2247 /*
2248 * Add in any requested optional attributes and the create time.
2249 * Also set the corresponding bits in the returned attribute bitmap.
2250 */
2251 if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) {
2252 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2253 xoap->xoa_archive =
2254 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2255 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2256 }
2257
2258 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2259 xoap->xoa_readonly =
2260 ((zp->z_pflags & ZFS_READONLY) != 0);
2261 XVA_SET_RTN(xvap, XAT_READONLY);
2262 }
2263
2264 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2265 xoap->xoa_system =
2266 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2267 XVA_SET_RTN(xvap, XAT_SYSTEM);
2268 }
2269
2270 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2271 xoap->xoa_hidden =
2272 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2273 XVA_SET_RTN(xvap, XAT_HIDDEN);
2274 }
2275
2276 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2277 xoap->xoa_nounlink =
2278 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2279 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2280 }
2281
2282 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2283 xoap->xoa_immutable =
2284 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2285 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2286 }
2287
2288 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2289 xoap->xoa_appendonly =
2290 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2291 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2292 }
2293
2294 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2295 xoap->xoa_nodump =
2296 ((zp->z_pflags & ZFS_NODUMP) != 0);
2297 XVA_SET_RTN(xvap, XAT_NODUMP);
2298 }
2299
2300 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2301 xoap->xoa_opaque =
2302 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2303 XVA_SET_RTN(xvap, XAT_OPAQUE);
2304 }
2305
2306 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2307 xoap->xoa_av_quarantined =
2308 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2309 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2310 }
2311
2312 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2313 xoap->xoa_av_modified =
2314 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2315 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2316 }
2317
2318 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2319 S_ISREG(ip->i_mode)) {
2320 zfs_sa_get_scanstamp(zp, xvap);
2321 }
34dc7c2f 2322
5484965a
BB
2323 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2324 uint64_t times[2];
2325
2326 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb),
2327 times, sizeof (times));
2328 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2329 XVA_SET_RTN(xvap, XAT_CREATETIME);
2330 }
2331
2332 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2333 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2334 XVA_SET_RTN(xvap, XAT_REPARSE);
2335 }
2336 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2337 xoap->xoa_generation = zp->z_gen;
2338 XVA_SET_RTN(xvap, XAT_GEN);
2339 }
2340
2341 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2342 xoap->xoa_offline =
2343 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2344 XVA_SET_RTN(xvap, XAT_OFFLINE);
2345 }
2346
2347 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2348 xoap->xoa_sparse =
2349 ((zp->z_pflags & ZFS_SPARSE) != 0);
2350 XVA_SET_RTN(xvap, XAT_SPARSE);
2351 }
2352 }
2353
2354 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2355 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2356 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
34dc7c2f
BB
2357
2358 mutex_exit(&zp->z_lock);
2359
5484965a 2360 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
34dc7c2f
BB
2361
2362 if (zp->z_blksz == 0) {
2363 /*
2364 * Block size hasn't been set; suggest maximal I/O transfers.
2365 */
5484965a 2366 vap->va_blksize = zsb->z_max_blksz;
34dc7c2f
BB
2367 }
2368
3558fd73 2369 ZFS_EXIT(zsb);
34dc7c2f
BB
2370 return (0);
2371}
e5c39b95 2372EXPORT_SYMBOL(zfs_getattr);
34dc7c2f 2373
057e8eee
BB
2374/*
2375 * Get the basic file attributes and place them in the provided kstat
2376 * structure. The inode is assumed to be the authoritative source
2377 * for most of the attributes. However, the znode currently has the
2378 * authoritative atime, blksize, and block count.
2379 *
2380 * IN: ip - inode of file.
2381 *
2382 * OUT: sp - kstat values.
2383 *
2384 * RETURN: 0 (always succeeds)
2385 */
2386/* ARGSUSED */
2387int
2388zfs_getattr_fast(struct inode *ip, struct kstat *sp)
2389{
2390 znode_t *zp = ITOZ(ip);
2391 zfs_sb_t *zsb = ITOZSB(ip);
b585bc4a
BB
2392 uint32_t blksize;
2393 u_longlong_t nblocks;
057e8eee 2394
a7b125e9
GB
2395 ZFS_ENTER(zsb);
2396 ZFS_VERIFY_ZP(zp);
2397
057e8eee
BB
2398 mutex_enter(&zp->z_lock);
2399
2400 generic_fillattr(ip, sp);
2401 ZFS_TIME_DECODE(&sp->atime, zp->z_atime);
2402
b585bc4a
BB
2403 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks);
2404 sp->blksize = blksize;
2405 sp->blocks = nblocks;
2406
057e8eee
BB
2407 if (unlikely(zp->z_blksz == 0)) {
2408 /*
2409 * Block size hasn't been set; suggest maximal I/O transfers.
2410 */
2411 sp->blksize = zsb->z_max_blksz;
2412 }
2413
2414 mutex_exit(&zp->z_lock);
2415
a7b125e9
GB
2416 ZFS_EXIT(zsb);
2417
057e8eee
BB
2418 return (0);
2419}
2420EXPORT_SYMBOL(zfs_getattr_fast);
2421
34dc7c2f
BB
2422/*
2423 * Set the file attributes to the values contained in the
2424 * vattr structure.
2425 *
3558fd73 2426 * IN: ip - inode of file to be modified.
34dc7c2f 2427 * vap - new attribute values.
5484965a 2428 * If ATTR_XVATTR set, then optional attrs are being set
34dc7c2f
BB
2429 * flags - ATTR_UTIME set if non-default time values provided.
2430 * - ATTR_NOACLCHECK (CIFS context only).
2431 * cr - credentials of caller.
34dc7c2f
BB
2432 *
2433 * RETURN: 0 if success
2434 * error code if failure
2435 *
2436 * Timestamps:
3558fd73 2437 * ip - ctime updated, mtime updated if size changed.
34dc7c2f
BB
2438 */
2439/* ARGSUSED */
e5c39b95 2440int
5484965a 2441zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr)
34dc7c2f 2442{
3558fd73
BB
2443 znode_t *zp = ITOZ(ip);
2444 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
2445 zilog_t *zilog;
2446 dmu_tx_t *tx;
2447 vattr_t oldva;
f4ea75d4 2448 xvattr_t *tmpxvattr;
5484965a 2449 uint_t mask = vap->va_mask;
a117a6d6 2450 uint_t saved_mask = 0;
34dc7c2f
BB
2451 int trim_mask = 0;
2452 uint64_t new_mode;
9babb374 2453 uint64_t new_uid, new_gid;
572e2857 2454 uint64_t xattr_obj;
428870ff 2455 uint64_t mtime[2], ctime[2];
34dc7c2f
BB
2456 znode_t *attrzp;
2457 int need_policy = FALSE;
428870ff 2458 int err, err2;
34dc7c2f 2459 zfs_fuid_info_t *fuidp = NULL;
5484965a
BB
2460 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2461 xoptattr_t *xoap;
2462 zfs_acl_t *aclp;
34dc7c2f 2463 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
428870ff 2464 boolean_t fuid_dirtied = B_FALSE;
17c37660 2465 sa_bulk_attr_t *bulk, *xattr_bulk;
428870ff 2466 int count = 0, xattr_count = 0;
34dc7c2f
BB
2467
2468 if (mask == 0)
2469 return (0);
2470
3558fd73 2471 ZFS_ENTER(zsb);
34dc7c2f
BB
2472 ZFS_VERIFY_ZP(zp);
2473
3558fd73 2474 zilog = zsb->z_log;
34dc7c2f
BB
2475
2476 /*
2477 * Make sure that if we have ephemeral uid/gid or xvattr specified
2478 * that file system is at proper version level
2479 */
5484965a 2480
3558fd73 2481 if (zsb->z_use_fuids == B_FALSE &&
5484965a
BB
2482 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2483 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2484 (mask & ATTR_XVATTR))) {
3558fd73 2485 ZFS_EXIT(zsb);
2e528b49 2486 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2487 }
2488
3558fd73
BB
2489 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) {
2490 ZFS_EXIT(zsb);
2e528b49 2491 return (SET_ERROR(EISDIR));
34dc7c2f
BB
2492 }
2493
3558fd73
BB
2494 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) {
2495 ZFS_EXIT(zsb);
2e528b49 2496 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2497 }
2498
5484965a
BB
2499 /*
2500 * If this is an xvattr_t, then get a pointer to the structure of
2501 * optional attributes. If this is NULL, then we have a vattr_t.
2502 */
2503 xoap = xva_getxoptattr(xvap);
2504
d1d7e268 2505 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP);
f4ea75d4 2506 xva_init(tmpxvattr);
5484965a 2507
d1d7e268
MK
2508 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
2509 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * 7, KM_SLEEP);
17c37660 2510
5484965a
BB
2511 /*
2512 * Immutable files can only alter immutable bit and atime
2513 */
2514 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2515 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) ||
2516 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
f4ea75d4
BB
2517 err = EPERM;
2518 goto out3;
5484965a
BB
2519 }
2520
3558fd73 2521 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
f4ea75d4
BB
2522 err = EPERM;
2523 goto out3;
34dc7c2f
BB
2524 }
2525
5484965a
BB
2526 /*
2527 * Verify timestamps doesn't overflow 32 bits.
2528 * ZFS can handle large timestamps, but 32bit syscalls can't
2529 * handle times greater than 2039. This check should be removed
2530 * once large timestamps are fully supported.
2531 */
2532 if (mask & (ATTR_ATIME | ATTR_MTIME)) {
d1d7e268
MK
2533 if (((mask & ATTR_ATIME) &&
2534 TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2535 ((mask & ATTR_MTIME) &&
2536 TIMESPEC_OVERFLOW(&vap->va_mtime))) {
f4ea75d4
BB
2537 err = EOVERFLOW;
2538 goto out3;
5484965a
BB
2539 }
2540 }
2541
34dc7c2f
BB
2542top:
2543 attrzp = NULL;
572e2857 2544 aclp = NULL;
34dc7c2f 2545
45d1cae3 2546 /* Can this be moved to before the top label? */
2cf7f52b 2547 if (zfs_is_readonly(zsb)) {
f4ea75d4
BB
2548 err = EROFS;
2549 goto out3;
34dc7c2f
BB
2550 }
2551
2552 /*
2553 * First validate permissions
2554 */
2555
3558fd73 2556 if (mask & ATTR_SIZE) {
34dc7c2f 2557 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
f4ea75d4
BB
2558 if (err)
2559 goto out3;
2560
34dc7c2f
BB
2561 /*
2562 * XXX - Note, we are not providing any open
2563 * mode flags here (like FNDELAY), so we may
2564 * block if there are locks present... this
2565 * should be addressed in openat().
2566 */
b128c09f 2567 /* XXX - would it be OK to generate a log record here? */
5484965a 2568 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
f4ea75d4
BB
2569 if (err)
2570 goto out3;
428870ff 2571 }
34dc7c2f 2572
5484965a
BB
2573 if (mask & (ATTR_ATIME|ATTR_MTIME) ||
2574 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2575 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2576 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2577 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2578 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2579 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2580 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2581 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2582 skipaclchk, cr);
2583 }
2584
3558fd73
BB
2585 if (mask & (ATTR_UID|ATTR_GID)) {
2586 int idmask = (mask & (ATTR_UID|ATTR_GID));
34dc7c2f
BB
2587 int take_owner;
2588 int take_group;
2589
2590 /*
2591 * NOTE: even if a new mode is being set,
2592 * we may clear S_ISUID/S_ISGID bits.
2593 */
2594
3558fd73 2595 if (!(mask & ATTR_MODE))
5484965a 2596 vap->va_mode = zp->z_mode;
34dc7c2f
BB
2597
2598 /*
2599 * Take ownership or chgrp to group we are a member of
2600 */
2601
5484965a 2602 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr));
3558fd73 2603 take_group = (mask & ATTR_GID) &&
5484965a 2604 zfs_groupmember(zsb, vap->va_gid, cr);
34dc7c2f
BB
2605
2606 /*
5484965a 2607 * If both ATTR_UID and ATTR_GID are set then take_owner and
34dc7c2f
BB
2608 * take_group must both be set in order to allow taking
2609 * ownership.
2610 *
2611 * Otherwise, send the check through secpolicy_vnode_setattr()
2612 *
2613 */
2614
3558fd73
BB
2615 if (((idmask == (ATTR_UID|ATTR_GID)) &&
2616 take_owner && take_group) ||
2617 ((idmask == ATTR_UID) && take_owner) ||
2618 ((idmask == ATTR_GID) && take_group)) {
34dc7c2f
BB
2619 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2620 skipaclchk, cr) == 0) {
2621 /*
2622 * Remove setuid/setgid for non-privileged users
2623 */
5484965a 2624 (void) secpolicy_setid_clear(vap, cr);
3558fd73 2625 trim_mask = (mask & (ATTR_UID|ATTR_GID));
34dc7c2f
BB
2626 } else {
2627 need_policy = TRUE;
2628 }
2629 } else {
2630 need_policy = TRUE;
2631 }
2632 }
2633
2634 mutex_enter(&zp->z_lock);
428870ff 2635 oldva.va_mode = zp->z_mode;
572e2857 2636 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
5484965a
BB
2637 if (mask & ATTR_XVATTR) {
2638 /*
2639 * Update xvattr mask to include only those attributes
2640 * that are actually changing.
2641 *
2642 * the bits will be restored prior to actually setting
2643 * the attributes so the caller thinks they were set.
2644 */
2645 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2646 if (xoap->xoa_appendonly !=
2647 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2648 need_policy = TRUE;
2649 } else {
2650 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
f4ea75d4 2651 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY);
5484965a
BB
2652 }
2653 }
2654
2655 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2656 if (xoap->xoa_nounlink !=
2657 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2658 need_policy = TRUE;
2659 } else {
2660 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
f4ea75d4 2661 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK);
5484965a
BB
2662 }
2663 }
2664
2665 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2666 if (xoap->xoa_immutable !=
2667 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2668 need_policy = TRUE;
2669 } else {
2670 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
f4ea75d4 2671 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE);
5484965a
BB
2672 }
2673 }
2674
2675 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2676 if (xoap->xoa_nodump !=
2677 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2678 need_policy = TRUE;
2679 } else {
2680 XVA_CLR_REQ(xvap, XAT_NODUMP);
f4ea75d4 2681 XVA_SET_REQ(tmpxvattr, XAT_NODUMP);
5484965a
BB
2682 }
2683 }
2684
2685 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2686 if (xoap->xoa_av_modified !=
2687 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2688 need_policy = TRUE;
2689 } else {
2690 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
f4ea75d4 2691 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED);
5484965a
BB
2692 }
2693 }
2694
2695 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2696 if ((!S_ISREG(ip->i_mode) &&
2697 xoap->xoa_av_quarantined) ||
2698 xoap->xoa_av_quarantined !=
2699 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2700 need_policy = TRUE;
2701 } else {
2702 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
f4ea75d4 2703 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED);
5484965a
BB
2704 }
2705 }
2706
2707 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2708 mutex_exit(&zp->z_lock);
f4ea75d4
BB
2709 err = EPERM;
2710 goto out3;
5484965a
BB
2711 }
2712
2713 if (need_policy == FALSE &&
2714 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2715 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2716 need_policy = TRUE;
2717 }
2718 }
34dc7c2f
BB
2719
2720 mutex_exit(&zp->z_lock);
2721
3558fd73 2722 if (mask & ATTR_MODE) {
34dc7c2f 2723 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
5484965a 2724 err = secpolicy_setid_setsticky_clear(ip, vap,
34dc7c2f 2725 &oldva, cr);
f4ea75d4
BB
2726 if (err)
2727 goto out3;
2728
3558fd73 2729 trim_mask |= ATTR_MODE;
34dc7c2f
BB
2730 } else {
2731 need_policy = TRUE;
2732 }
2733 }
2734
2735 if (need_policy) {
2736 /*
2737 * If trim_mask is set then take ownership
2738 * has been granted or write_acl is present and user
2739 * has the ability to modify mode. In that case remove
2740 * UID|GID and or MODE from mask so that
2741 * secpolicy_vnode_setattr() doesn't revoke it.
2742 */
2743
2744 if (trim_mask) {
5484965a
BB
2745 saved_mask = vap->va_mask;
2746 vap->va_mask &= ~trim_mask;
34dc7c2f 2747 }
5484965a 2748 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags,
34dc7c2f 2749 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
f4ea75d4
BB
2750 if (err)
2751 goto out3;
34dc7c2f
BB
2752
2753 if (trim_mask)
5484965a 2754 vap->va_mask |= saved_mask;
34dc7c2f
BB
2755 }
2756
2757 /*
2758 * secpolicy_vnode_setattr, or take ownership may have
2759 * changed va_mask
2760 */
5484965a 2761 mask = vap->va_mask;
34dc7c2f 2762
3558fd73
BB
2763 if ((mask & (ATTR_UID | ATTR_GID))) {
2764 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
572e2857 2765 &xattr_obj, sizeof (xattr_obj));
428870ff 2766
572e2857 2767 if (err == 0 && xattr_obj) {
3558fd73 2768 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp);
428870ff
BB
2769 if (err)
2770 goto out2;
2771 }
3558fd73
BB
2772 if (mask & ATTR_UID) {
2773 new_uid = zfs_fuid_create(zsb,
5484965a 2774 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
572e2857 2775 if (new_uid != zp->z_uid &&
3558fd73 2776 zfs_fuid_overquota(zsb, B_FALSE, new_uid)) {
572e2857 2777 if (attrzp)
3558fd73 2778 iput(ZTOI(attrzp));
428870ff
BB
2779 err = EDQUOT;
2780 goto out2;
2781 }
2782 }
2783
3558fd73 2784 if (mask & ATTR_GID) {
5484965a 2785 new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid,
428870ff
BB
2786 cr, ZFS_GROUP, &fuidp);
2787 if (new_gid != zp->z_gid &&
3558fd73 2788 zfs_fuid_overquota(zsb, B_TRUE, new_gid)) {
572e2857 2789 if (attrzp)
3558fd73 2790 iput(ZTOI(attrzp));
428870ff
BB
2791 err = EDQUOT;
2792 goto out2;
2793 }
2794 }
2795 }
3558fd73 2796 tx = dmu_tx_create(zsb->z_os);
34dc7c2f 2797
3558fd73 2798 if (mask & ATTR_MODE) {
428870ff 2799 uint64_t pmode = zp->z_mode;
572e2857 2800 uint64_t acl_obj;
5484965a 2801 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
34dc7c2f 2802
572e2857 2803 zfs_acl_chmod_setattr(zp, &aclp, new_mode);
428870ff 2804
572e2857
BB
2805 mutex_enter(&zp->z_lock);
2806 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
428870ff
BB
2807 /*
2808 * Are we upgrading ACL from old V0 format
2809 * to V1 format?
2810 */
3558fd73 2811 if (zsb->z_version >= ZPL_VERSION_FUID &&
572e2857 2812 zfs_znode_acl_version(zp) ==
34dc7c2f 2813 ZFS_ACL_VERSION_INITIAL) {
572e2857 2814 dmu_tx_hold_free(tx, acl_obj, 0,
34dc7c2f
BB
2815 DMU_OBJECT_END);
2816 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2817 0, aclp->z_acl_bytes);
2818 } else {
572e2857 2819 dmu_tx_hold_write(tx, acl_obj, 0,
34dc7c2f
BB
2820 aclp->z_acl_bytes);
2821 }
428870ff 2822 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
34dc7c2f
BB
2823 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2824 0, aclp->z_acl_bytes);
2825 }
572e2857 2826 mutex_exit(&zp->z_lock);
428870ff
BB
2827 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2828 } else {
5484965a
BB
2829 if ((mask & ATTR_XVATTR) &&
2830 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2831 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
2832 else
2833 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
2834 }
2835
428870ff
BB
2836 if (attrzp) {
2837 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
2838 }
2839
3558fd73 2840 fuid_dirtied = zsb->z_fuid_dirty;
428870ff 2841 if (fuid_dirtied)
3558fd73 2842 zfs_fuid_txhold(zsb, tx);
428870ff
BB
2843
2844 zfs_sa_upgrade_txholds(tx, zp);
2845
384f8a09
MA
2846 err = dmu_tx_assign(tx, TXG_WAIT);
2847 if (err)
9babb374 2848 goto out;
34dc7c2f 2849
428870ff 2850 count = 0;
34dc7c2f
BB
2851 /*
2852 * Set each attribute requested.
2853 * We group settings according to the locks they need to acquire.
2854 *
2855 * Note: you cannot set ctime directly, although it will be
2856 * updated as a side-effect of calling this function.
2857 */
2858
572e2857 2859
3558fd73 2860 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 2861 mutex_enter(&zp->z_acl_lock);
34dc7c2f
BB
2862 mutex_enter(&zp->z_lock);
2863
3558fd73 2864 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
428870ff
BB
2865 &zp->z_pflags, sizeof (zp->z_pflags));
2866
2867 if (attrzp) {
3558fd73 2868 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 2869 mutex_enter(&attrzp->z_acl_lock);
428870ff
BB
2870 mutex_enter(&attrzp->z_lock);
2871 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3558fd73 2872 SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags,
428870ff
BB
2873 sizeof (attrzp->z_pflags));
2874 }
2875
3558fd73 2876 if (mask & (ATTR_UID|ATTR_GID)) {
428870ff 2877
3558fd73
BB
2878 if (mask & ATTR_UID) {
2879 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
428870ff 2880 &new_uid, sizeof (new_uid));
572e2857 2881 zp->z_uid = new_uid;
428870ff
BB
2882 if (attrzp) {
2883 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3558fd73 2884 SA_ZPL_UID(zsb), NULL, &new_uid,
428870ff 2885 sizeof (new_uid));
572e2857 2886 attrzp->z_uid = new_uid;
428870ff
BB
2887 }
2888 }
2889
3558fd73
BB
2890 if (mask & ATTR_GID) {
2891 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb),
428870ff 2892 NULL, &new_gid, sizeof (new_gid));
572e2857 2893 zp->z_gid = new_gid;
428870ff
BB
2894 if (attrzp) {
2895 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3558fd73 2896 SA_ZPL_GID(zsb), NULL, &new_gid,
428870ff 2897 sizeof (new_gid));
572e2857 2898 attrzp->z_gid = new_gid;
428870ff
BB
2899 }
2900 }
3558fd73
BB
2901 if (!(mask & ATTR_MODE)) {
2902 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb),
428870ff
BB
2903 NULL, &new_mode, sizeof (new_mode));
2904 new_mode = zp->z_mode;
2905 }
2906 err = zfs_acl_chown_setattr(zp);
2907 ASSERT(err == 0);
2908 if (attrzp) {
2909 err = zfs_acl_chown_setattr(attrzp);
2910 ASSERT(err == 0);
2911 }
2912 }
2913
3558fd73
BB
2914 if (mask & ATTR_MODE) {
2915 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
428870ff
BB
2916 &new_mode, sizeof (new_mode));
2917 zp->z_mode = new_mode;
99c564bc 2918 ASSERT3P(aclp, !=, NULL);
9babb374 2919 err = zfs_aclset_common(zp, aclp, cr, tx);
c99c9001 2920 ASSERT0(err);
572e2857
BB
2921 if (zp->z_acl_cached)
2922 zfs_acl_free(zp->z_acl_cached);
45d1cae3
BB
2923 zp->z_acl_cached = aclp;
2924 aclp = NULL;
34dc7c2f
BB
2925 }
2926
34dc7c2f 2927
3558fd73 2928 if (mask & ATTR_ATIME) {
5484965a 2929 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3558fd73 2930 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
428870ff 2931 &zp->z_atime, sizeof (zp->z_atime));
34dc7c2f
BB
2932 }
2933
3558fd73 2934 if (mask & ATTR_MTIME) {
5484965a 2935 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3558fd73 2936 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
428870ff 2937 mtime, sizeof (mtime));
34dc7c2f
BB
2938 }
2939
b128c09f 2940 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3558fd73
BB
2941 if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) {
2942 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
428870ff 2943 NULL, mtime, sizeof (mtime));
3558fd73 2944 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
428870ff
BB
2945 &ctime, sizeof (ctime));
2946 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
2947 B_TRUE);
2948 } else if (mask != 0) {
3558fd73 2949 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
428870ff
BB
2950 &ctime, sizeof (ctime));
2951 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
2952 B_TRUE);
2953 if (attrzp) {
2954 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3558fd73 2955 SA_ZPL_CTIME(zsb), NULL,
428870ff
BB
2956 &ctime, sizeof (ctime));
2957 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
2958 mtime, ctime, B_TRUE);
2959 }
2960 }
34dc7c2f
BB
2961 /*
2962 * Do this after setting timestamps to prevent timestamp
2963 * update from toggling bit
2964 */
2965
5484965a
BB
2966 if (xoap && (mask & ATTR_XVATTR)) {
2967
2968 /*
2969 * restore trimmed off masks
2970 * so that return masks can be set for caller.
2971 */
2972
f4ea75d4 2973 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) {
5484965a
BB
2974 XVA_SET_REQ(xvap, XAT_APPENDONLY);
2975 }
f4ea75d4 2976 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) {
5484965a
BB
2977 XVA_SET_REQ(xvap, XAT_NOUNLINK);
2978 }
f4ea75d4 2979 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) {
5484965a
BB
2980 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
2981 }
f4ea75d4 2982 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) {
5484965a
BB
2983 XVA_SET_REQ(xvap, XAT_NODUMP);
2984 }
f4ea75d4 2985 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) {
5484965a
BB
2986 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
2987 }
f4ea75d4 2988 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) {
5484965a
BB
2989 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
2990 }
2991
2992 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
2993 ASSERT(S_ISREG(ip->i_mode));
2994
2995 zfs_xvattr_set(zp, xvap, tx);
2996 }
2997
9babb374 2998 if (fuid_dirtied)
3558fd73 2999 zfs_fuid_sync(zsb, tx);
9babb374 3000
34dc7c2f 3001 if (mask != 0)
5484965a 3002 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
34dc7c2f 3003
34dc7c2f 3004 mutex_exit(&zp->z_lock);
3558fd73 3005 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857 3006 mutex_exit(&zp->z_acl_lock);
34dc7c2f 3007
572e2857 3008 if (attrzp) {
3558fd73 3009 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE))
572e2857
BB
3010 mutex_exit(&attrzp->z_acl_lock);
3011 mutex_exit(&attrzp->z_lock);
3012 }
9babb374 3013out:
428870ff
BB
3014 if (err == 0 && attrzp) {
3015 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3016 xattr_count, tx);
3017 ASSERT(err2 == 0);
3018 }
3019
34dc7c2f 3020 if (attrzp)
3558fd73 3021 iput(ZTOI(attrzp));
45d1cae3 3022 if (aclp)
9babb374 3023 zfs_acl_free(aclp);
9babb374
BB
3024
3025 if (fuidp) {
3026 zfs_fuid_info_free(fuidp);
3027 fuidp = NULL;
3028 }
3029
428870ff 3030 if (err) {
9babb374 3031 dmu_tx_abort(tx);
428870ff
BB
3032 if (err == ERESTART)
3033 goto top;
3034 } else {
3035 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
9babb374 3036 dmu_tx_commit(tx);
037849f8 3037 zfs_inode_update(zp);
428870ff
BB
3038 }
3039
428870ff 3040out2:
3558fd73 3041 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3042 zil_commit(zilog, 0);
34dc7c2f 3043
f4ea75d4 3044out3:
d1d7e268
MK
3045 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * 7);
3046 kmem_free(bulk, sizeof (sa_bulk_attr_t) * 7);
3047 kmem_free(tmpxvattr, sizeof (xvattr_t));
3558fd73 3048 ZFS_EXIT(zsb);
34dc7c2f
BB
3049 return (err);
3050}
e5c39b95 3051EXPORT_SYMBOL(zfs_setattr);
34dc7c2f
BB
3052
3053typedef struct zfs_zlock {
3054 krwlock_t *zl_rwlock; /* lock we acquired */
3055 znode_t *zl_znode; /* znode we held */
3056 struct zfs_zlock *zl_next; /* next in list */
3057} zfs_zlock_t;
3058
3059/*
3060 * Drop locks and release vnodes that were held by zfs_rename_lock().
3061 */
3062static void
3063zfs_rename_unlock(zfs_zlock_t **zlpp)
3064{
3065 zfs_zlock_t *zl;
3066
3067 while ((zl = *zlpp) != NULL) {
3068 if (zl->zl_znode != NULL)
3558fd73 3069 iput(ZTOI(zl->zl_znode));
34dc7c2f
BB
3070 rw_exit(zl->zl_rwlock);
3071 *zlpp = zl->zl_next;
3072 kmem_free(zl, sizeof (*zl));
3073 }
3074}
3075
3076/*
3077 * Search back through the directory tree, using the ".." entries.
3078 * Lock each directory in the chain to prevent concurrent renames.
3079 * Fail any attempt to move a directory into one of its own descendants.
3080 * XXX - z_parent_lock can overlap with map or grow locks
3081 */
3082static int
3083zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3084{
3085 zfs_zlock_t *zl;
3086 znode_t *zp = tdzp;
3558fd73 3087 uint64_t rootid = ZTOZSB(zp)->z_root;
428870ff 3088 uint64_t oidp = zp->z_id;
34dc7c2f
BB
3089 krwlock_t *rwlp = &szp->z_parent_lock;
3090 krw_t rw = RW_WRITER;
3091
3092 /*
3093 * First pass write-locks szp and compares to zp->z_id.
3094 * Later passes read-lock zp and compare to zp->z_parent.
3095 */
3096 do {
3097 if (!rw_tryenter(rwlp, rw)) {
3098 /*
3099 * Another thread is renaming in this path.
3100 * Note that if we are a WRITER, we don't have any
3101 * parent_locks held yet.
3102 */
3103 if (rw == RW_READER && zp->z_id > szp->z_id) {
3104 /*
3105 * Drop our locks and restart
3106 */
3107 zfs_rename_unlock(&zl);
3108 *zlpp = NULL;
3109 zp = tdzp;
428870ff 3110 oidp = zp->z_id;
34dc7c2f
BB
3111 rwlp = &szp->z_parent_lock;
3112 rw = RW_WRITER;
3113 continue;
3114 } else {
3115 /*
3116 * Wait for other thread to drop its locks
3117 */
3118 rw_enter(rwlp, rw);
3119 }
3120 }
3121
3122 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3123 zl->zl_rwlock = rwlp;
3124 zl->zl_znode = NULL;
3125 zl->zl_next = *zlpp;
3126 *zlpp = zl;
3127
428870ff 3128 if (oidp == szp->z_id) /* We're a descendant of szp */
2e528b49 3129 return (SET_ERROR(EINVAL));
34dc7c2f 3130
428870ff 3131 if (oidp == rootid) /* We've hit the top */
34dc7c2f
BB
3132 return (0);
3133
3134 if (rw == RW_READER) { /* i.e. not the first pass */
3558fd73 3135 int error = zfs_zget(ZTOZSB(zp), oidp, &zp);
34dc7c2f
BB
3136 if (error)
3137 return (error);
3138 zl->zl_znode = zp;
3139 }
3558fd73 3140 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)),
428870ff 3141 &oidp, sizeof (oidp));
34dc7c2f
BB
3142 rwlp = &zp->z_parent_lock;
3143 rw = RW_READER;
3144
3145 } while (zp->z_id != sdzp->z_id);
3146
3147 return (0);
3148}
3149
3150/*
3151 * Move an entry from the provided source directory to the target
3152 * directory. Change the entry name as indicated.
3153 *
3558fd73 3154 * IN: sdip - Source directory containing the "old entry".
34dc7c2f 3155 * snm - Old entry name.
3558fd73 3156 * tdip - Target directory to contain the "new entry".
34dc7c2f
BB
3157 * tnm - New entry name.
3158 * cr - credentials of caller.
34dc7c2f
BB
3159 * flags - case flags
3160 *
d3cc8b15 3161 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
3162 *
3163 * Timestamps:
3558fd73 3164 * sdip,tdip - ctime|mtime updated
34dc7c2f
BB
3165 */
3166/*ARGSUSED*/
e5c39b95 3167int
3558fd73
BB
3168zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm,
3169 cred_t *cr, int flags)
34dc7c2f
BB
3170{
3171 znode_t *tdzp, *szp, *tzp;
3558fd73
BB
3172 znode_t *sdzp = ITOZ(sdip);
3173 zfs_sb_t *zsb = ITOZSB(sdip);
34dc7c2f 3174 zilog_t *zilog;
34dc7c2f
BB
3175 zfs_dirlock_t *sdl, *tdl;
3176 dmu_tx_t *tx;
3177 zfs_zlock_t *zl;
3178 int cmp, serr, terr;
3179 int error = 0;
3180 int zflg = 0;
e8b96c60 3181 boolean_t waited = B_FALSE;
34dc7c2f 3182
3558fd73 3183 ZFS_ENTER(zsb);
34dc7c2f 3184 ZFS_VERIFY_ZP(sdzp);
3558fd73 3185 zilog = zsb->z_log;
34dc7c2f 3186
c0ebc844 3187 if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) {
3558fd73 3188 ZFS_EXIT(zsb);
2e528b49 3189 return (SET_ERROR(EXDEV));
34dc7c2f
BB
3190 }
3191
3558fd73 3192 tdzp = ITOZ(tdip);
34dc7c2f 3193 ZFS_VERIFY_ZP(tdzp);
3558fd73 3194 if (zsb->z_utf8 && u8_validate(tnm,
34dc7c2f 3195 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 3196 ZFS_EXIT(zsb);
2e528b49 3197 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
3198 }
3199
3200 if (flags & FIGNORECASE)
3201 zflg |= ZCILOOK;
3202
3203top:
3204 szp = NULL;
3205 tzp = NULL;
3206 zl = NULL;
3207
3208 /*
3209 * This is to prevent the creation of links into attribute space
3210 * by renaming a linked file into/outof an attribute directory.
3211 * See the comment in zfs_link() for why this is considered bad.
3212 */
428870ff 3213 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3558fd73 3214 ZFS_EXIT(zsb);
2e528b49 3215 return (SET_ERROR(EINVAL));
34dc7c2f
BB
3216 }
3217
3218 /*
3219 * Lock source and target directory entries. To prevent deadlock,
3220 * a lock ordering must be defined. We lock the directory with
3221 * the smallest object id first, or if it's a tie, the one with
3222 * the lexically first name.
3223 */
3224 if (sdzp->z_id < tdzp->z_id) {
3225 cmp = -1;
3226 } else if (sdzp->z_id > tdzp->z_id) {
3227 cmp = 1;
3228 } else {
3229 /*
3230 * First compare the two name arguments without
3231 * considering any case folding.
3232 */
3558fd73 3233 int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER);
34dc7c2f
BB
3234
3235 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3558fd73 3236 ASSERT(error == 0 || !zsb->z_utf8);
34dc7c2f
BB
3237 if (cmp == 0) {
3238 /*
3239 * POSIX: "If the old argument and the new argument
3240 * both refer to links to the same existing file,
3241 * the rename() function shall return successfully
3242 * and perform no other action."
3243 */
3558fd73 3244 ZFS_EXIT(zsb);
34dc7c2f
BB
3245 return (0);
3246 }
3247 /*
3248 * If the file system is case-folding, then we may
3249 * have some more checking to do. A case-folding file
3250 * system is either supporting mixed case sensitivity
3251 * access or is completely case-insensitive. Note
3252 * that the file system is always case preserving.
3253 *
3254 * In mixed sensitivity mode case sensitive behavior
3255 * is the default. FIGNORECASE must be used to
3256 * explicitly request case insensitive behavior.
3257 *
3258 * If the source and target names provided differ only
3259 * by case (e.g., a request to rename 'tim' to 'Tim'),
3260 * we will treat this as a special case in the
3261 * case-insensitive mode: as long as the source name
3262 * is an exact match, we will allow this to proceed as
3263 * a name-change request.
3264 */
3558fd73
BB
3265 if ((zsb->z_case == ZFS_CASE_INSENSITIVE ||
3266 (zsb->z_case == ZFS_CASE_MIXED &&
34dc7c2f 3267 flags & FIGNORECASE)) &&
3558fd73 3268 u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST,
34dc7c2f
BB
3269 &error) == 0) {
3270 /*
3271 * case preserving rename request, require exact
3272 * name matches
3273 */
3274 zflg |= ZCIEXACT;
3275 zflg &= ~ZCILOOK;
3276 }
3277 }
3278
428870ff
BB
3279 /*
3280 * If the source and destination directories are the same, we should
3281 * grab the z_name_lock of that directory only once.
3282 */
3283 if (sdzp == tdzp) {
3284 zflg |= ZHAVELOCK;
3285 rw_enter(&sdzp->z_name_lock, RW_READER);
3286 }
3287
34dc7c2f
BB
3288 if (cmp < 0) {
3289 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3290 ZEXISTS | zflg, NULL, NULL);
3291 terr = zfs_dirent_lock(&tdl,
3292 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3293 } else {
3294 terr = zfs_dirent_lock(&tdl,
3295 tdzp, tnm, &tzp, zflg, NULL, NULL);
3296 serr = zfs_dirent_lock(&sdl,
3297 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3298 NULL, NULL);
3299 }
3300
3301 if (serr) {
3302 /*
3303 * Source entry invalid or not there.
3304 */
3305 if (!terr) {
3306 zfs_dirent_unlock(tdl);
3307 if (tzp)
3558fd73 3308 iput(ZTOI(tzp));
34dc7c2f 3309 }
428870ff
BB
3310
3311 if (sdzp == tdzp)
3312 rw_exit(&sdzp->z_name_lock);
3313
34dc7c2f
BB
3314 if (strcmp(snm, "..") == 0)
3315 serr = EINVAL;
3558fd73 3316 ZFS_EXIT(zsb);
34dc7c2f
BB
3317 return (serr);
3318 }
3319 if (terr) {
3320 zfs_dirent_unlock(sdl);
3558fd73 3321 iput(ZTOI(szp));
428870ff
BB
3322
3323 if (sdzp == tdzp)
3324 rw_exit(&sdzp->z_name_lock);
3325
34dc7c2f
BB
3326 if (strcmp(tnm, "..") == 0)
3327 terr = EINVAL;
3558fd73 3328 ZFS_EXIT(zsb);
34dc7c2f
BB
3329 return (terr);
3330 }
3331
3332 /*
3333 * Must have write access at the source to remove the old entry
3334 * and write access at the target to create the new entry.
3335 * Note that if target and source are the same, this can be
3336 * done in a single check.
3337 */
3338
149e873a 3339 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)))
34dc7c2f
BB
3340 goto out;
3341
3558fd73 3342 if (S_ISDIR(ZTOI(szp)->i_mode)) {
34dc7c2f
BB
3343 /*
3344 * Check to make sure rename is valid.
3345 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3346 */
149e873a 3347 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl)))
34dc7c2f
BB
3348 goto out;
3349 }
3350
3351 /*
3352 * Does target exist?
3353 */
3354 if (tzp) {
3355 /*
3356 * Source and target must be the same type.
3357 */
3558fd73
BB
3358 if (S_ISDIR(ZTOI(szp)->i_mode)) {
3359 if (!S_ISDIR(ZTOI(tzp)->i_mode)) {
2e528b49 3360 error = SET_ERROR(ENOTDIR);
34dc7c2f
BB
3361 goto out;
3362 }
3363 } else {
3558fd73 3364 if (S_ISDIR(ZTOI(tzp)->i_mode)) {
2e528b49 3365 error = SET_ERROR(EISDIR);
34dc7c2f
BB
3366 goto out;
3367 }
3368 }
3369 /*
3370 * POSIX dictates that when the source and target
3371 * entries refer to the same file object, rename
3372 * must do nothing and exit without error.
3373 */
3374 if (szp->z_id == tzp->z_id) {
3375 error = 0;
3376 goto out;
3377 }
3378 }
3379
3558fd73 3380 tx = dmu_tx_create(zsb->z_os);
428870ff
BB
3381 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3382 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
3383 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3384 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
428870ff
BB
3385 if (sdzp != tdzp) {
3386 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3387 zfs_sa_upgrade_txholds(tx, tdzp);
3388 }
3389 if (tzp) {
3390 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3391 zfs_sa_upgrade_txholds(tx, tzp);
3392 }
3393
3394 zfs_sa_upgrade_txholds(tx, szp);
3558fd73 3395 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
e8b96c60 3396 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
3397 if (error) {
3398 if (zl != NULL)
3399 zfs_rename_unlock(&zl);
3400 zfs_dirent_unlock(sdl);
3401 zfs_dirent_unlock(tdl);
428870ff
BB
3402
3403 if (sdzp == tdzp)
3404 rw_exit(&sdzp->z_name_lock);
3405
3558fd73 3406 iput(ZTOI(szp));
34dc7c2f 3407 if (tzp)
3558fd73 3408 iput(ZTOI(tzp));
fb5f0bc8 3409 if (error == ERESTART) {
e8b96c60 3410 waited = B_TRUE;
34dc7c2f
BB
3411 dmu_tx_wait(tx);
3412 dmu_tx_abort(tx);
3413 goto top;
3414 }
3415 dmu_tx_abort(tx);
3558fd73 3416 ZFS_EXIT(zsb);
34dc7c2f
BB
3417 return (error);
3418 }
3419
3420 if (tzp) /* Attempt to remove the existing target */
3421 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3422
3423 if (error == 0) {
3424 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3425 if (error == 0) {
428870ff 3426 szp->z_pflags |= ZFS_AV_MODIFIED;
34dc7c2f 3427
3558fd73 3428 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb),
428870ff 3429 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
c99c9001 3430 ASSERT0(error);
34dc7c2f 3431
428870ff
BB
3432 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3433 if (error == 0) {
3434 zfs_log_rename(zilog, tx, TX_RENAME |
572e2857
BB
3435 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3436 sdl->dl_name, tdzp, tdl->dl_name, szp);
428870ff
BB
3437 } else {
3438 /*
3439 * At this point, we have successfully created
3440 * the target name, but have failed to remove
3441 * the source name. Since the create was done
3442 * with the ZRENAMING flag, there are
3443 * complications; for one, the link count is
3444 * wrong. The easiest way to deal with this
3445 * is to remove the newly created target, and
3446 * return the original error. This must
3447 * succeed; fortunately, it is very unlikely to
3448 * fail, since we just created it.
3449 */
3450 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3451 ZRENAMING, NULL), ==, 0);
3452 }
34dc7c2f
BB
3453 }
3454 }
3455
3456 dmu_tx_commit(tx);
3457out:
3458 if (zl != NULL)
3459 zfs_rename_unlock(&zl);
3460
3461 zfs_dirent_unlock(sdl);
3462 zfs_dirent_unlock(tdl);
3463
960e08fe 3464 zfs_inode_update(sdzp);
428870ff
BB
3465 if (sdzp == tdzp)
3466 rw_exit(&sdzp->z_name_lock);
3467
960e08fe
BB
3468 if (sdzp != tdzp)
3469 zfs_inode_update(tdzp);
428870ff 3470
960e08fe 3471 zfs_inode_update(szp);
3558fd73 3472 iput(ZTOI(szp));
960e08fe
BB
3473 if (tzp) {
3474 zfs_inode_update(tzp);
3558fd73 3475 iput(ZTOI(tzp));
960e08fe 3476 }
34dc7c2f 3477
3558fd73 3478 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3479 zil_commit(zilog, 0);
428870ff 3480
3558fd73 3481 ZFS_EXIT(zsb);
34dc7c2f
BB
3482 return (error);
3483}
e5c39b95 3484EXPORT_SYMBOL(zfs_rename);
34dc7c2f
BB
3485
3486/*
3487 * Insert the indicated symbolic reference entry into the directory.
3488 *
3558fd73 3489 * IN: dip - Directory to contain new symbolic link.
34dc7c2f
BB
3490 * link - Name for new symlink entry.
3491 * vap - Attributes of new entry.
3492 * target - Target path of new symlink.
3558fd73 3493 *
34dc7c2f 3494 * cr - credentials of caller.
34dc7c2f
BB
3495 * flags - case flags
3496 *
d3cc8b15 3497 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
3498 *
3499 * Timestamps:
3558fd73 3500 * dip - ctime|mtime updated
34dc7c2f
BB
3501 */
3502/*ARGSUSED*/
e5c39b95 3503int
3558fd73
BB
3504zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link,
3505 struct inode **ipp, cred_t *cr, int flags)
34dc7c2f 3506{
3558fd73 3507 znode_t *zp, *dzp = ITOZ(dip);
34dc7c2f
BB
3508 zfs_dirlock_t *dl;
3509 dmu_tx_t *tx;
3558fd73 3510 zfs_sb_t *zsb = ITOZSB(dip);
34dc7c2f 3511 zilog_t *zilog;
428870ff 3512 uint64_t len = strlen(link);
34dc7c2f
BB
3513 int error;
3514 int zflg = ZNEW;
9babb374
BB
3515 zfs_acl_ids_t acl_ids;
3516 boolean_t fuid_dirtied;
428870ff 3517 uint64_t txtype = TX_SYMLINK;
e8b96c60 3518 boolean_t waited = B_FALSE;
34dc7c2f 3519
3558fd73 3520 ASSERT(S_ISLNK(vap->va_mode));
34dc7c2f 3521
3558fd73 3522 ZFS_ENTER(zsb);
34dc7c2f 3523 ZFS_VERIFY_ZP(dzp);
3558fd73 3524 zilog = zsb->z_log;
34dc7c2f 3525
3558fd73 3526 if (zsb->z_utf8 && u8_validate(name, strlen(name),
34dc7c2f 3527 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 3528 ZFS_EXIT(zsb);
2e528b49 3529 return (SET_ERROR(EILSEQ));
34dc7c2f
BB
3530 }
3531 if (flags & FIGNORECASE)
3532 zflg |= ZCILOOK;
34dc7c2f
BB
3533
3534 if (len > MAXPATHLEN) {
3558fd73 3535 ZFS_EXIT(zsb);
2e528b49 3536 return (SET_ERROR(ENAMETOOLONG));
34dc7c2f
BB
3537 }
3538
428870ff
BB
3539 if ((error = zfs_acl_ids_create(dzp, 0,
3540 vap, cr, NULL, &acl_ids)) != 0) {
3558fd73 3541 ZFS_EXIT(zsb);
428870ff
BB
3542 return (error);
3543 }
3544top:
3558fd73
BB
3545 *ipp = NULL;
3546
34dc7c2f
BB
3547 /*
3548 * Attempt to lock directory; fail if entry already exists.
3549 */
3550 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3551 if (error) {
428870ff 3552 zfs_acl_ids_free(&acl_ids);
3558fd73 3553 ZFS_EXIT(zsb);
428870ff
BB
3554 return (error);
3555 }
3556
149e873a 3557 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
428870ff
BB
3558 zfs_acl_ids_free(&acl_ids);
3559 zfs_dirent_unlock(dl);
3558fd73 3560 ZFS_EXIT(zsb);
34dc7c2f
BB
3561 return (error);
3562 }
3563
3558fd73 3564 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
9babb374
BB
3565 zfs_acl_ids_free(&acl_ids);
3566 zfs_dirent_unlock(dl);
3558fd73 3567 ZFS_EXIT(zsb);
2e528b49 3568 return (SET_ERROR(EDQUOT));
9babb374 3569 }
3558fd73
BB
3570 tx = dmu_tx_create(zsb->z_os);
3571 fuid_dirtied = zsb->z_fuid_dirty;
34dc7c2f 3572 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
34dc7c2f 3573 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
428870ff
BB
3574 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3575 ZFS_SA_BASE_ATTR_SIZE + len);
3576 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3558fd73 3577 if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
428870ff
BB
3578 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3579 acl_ids.z_aclp->z_acl_bytes);
3580 }
9babb374 3581 if (fuid_dirtied)
3558fd73 3582 zfs_fuid_txhold(zsb, tx);
e8b96c60 3583 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
3584 if (error) {
3585 zfs_dirent_unlock(dl);
fb5f0bc8 3586 if (error == ERESTART) {
e8b96c60 3587 waited = B_TRUE;
34dc7c2f
BB
3588 dmu_tx_wait(tx);
3589 dmu_tx_abort(tx);
3590 goto top;
3591 }
428870ff 3592 zfs_acl_ids_free(&acl_ids);
34dc7c2f 3593 dmu_tx_abort(tx);
3558fd73 3594 ZFS_EXIT(zsb);
34dc7c2f
BB
3595 return (error);
3596 }
3597
34dc7c2f
BB
3598 /*
3599 * Create a new object for the symlink.
428870ff 3600 * for version 4 ZPL datsets the symlink will be an SA attribute
34dc7c2f 3601 */
428870ff 3602 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
9babb374 3603
428870ff 3604 if (fuid_dirtied)
3558fd73 3605 zfs_fuid_sync(zsb, tx);
34dc7c2f 3606
572e2857 3607 mutex_enter(&zp->z_lock);
428870ff 3608 if (zp->z_is_sa)
3558fd73 3609 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb),
428870ff
BB
3610 link, len, tx);
3611 else
3612 zfs_sa_symlink(zp, link, len, tx);
572e2857 3613 mutex_exit(&zp->z_lock);
34dc7c2f 3614
428870ff 3615 zp->z_size = len;
3558fd73 3616 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb),
428870ff 3617 &zp->z_size, sizeof (zp->z_size), tx);
34dc7c2f
BB
3618 /*
3619 * Insert the new object into the directory.
3620 */
3621 (void) zfs_link_create(dl, zp, tx, ZNEW);
428870ff
BB
3622
3623 if (flags & FIGNORECASE)
3624 txtype |= TX_CI;
3625 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
9babb374 3626
960e08fe
BB
3627 zfs_inode_update(dzp);
3628 zfs_inode_update(zp);
3629
9babb374 3630 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
3631
3632 dmu_tx_commit(tx);
3633
3634 zfs_dirent_unlock(dl);
3635
3558fd73 3636 *ipp = ZTOI(zp);
34dc7c2f 3637
3558fd73 3638 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3639 zil_commit(zilog, 0);
428870ff 3640
3558fd73 3641 ZFS_EXIT(zsb);
34dc7c2f
BB
3642 return (error);
3643}
e5c39b95 3644EXPORT_SYMBOL(zfs_symlink);
34dc7c2f
BB
3645
3646/*
3647 * Return, in the buffer contained in the provided uio structure,
3558fd73 3648 * the symbolic path referred to by ip.
34dc7c2f 3649 *
8b4f9a2d
BB
3650 * IN: ip - inode of symbolic link
3651 * uio - structure to contain the link path.
3652 * cr - credentials of caller.
34dc7c2f
BB
3653 *
3654 * RETURN: 0 if success
3655 * error code if failure
3656 *
3657 * Timestamps:
3558fd73 3658 * ip - atime updated
34dc7c2f
BB
3659 */
3660/* ARGSUSED */
e5c39b95 3661int
8b4f9a2d 3662zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr)
34dc7c2f 3663{
3558fd73
BB
3664 znode_t *zp = ITOZ(ip);
3665 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
3666 int error;
3667
3558fd73 3668 ZFS_ENTER(zsb);
34dc7c2f
BB
3669 ZFS_VERIFY_ZP(zp);
3670
572e2857 3671 mutex_enter(&zp->z_lock);
428870ff 3672 if (zp->z_is_sa)
8b4f9a2d
BB
3673 error = sa_lookup_uio(zp->z_sa_hdl,
3674 SA_ZPL_SYMLINK(zsb), uio);
428870ff 3675 else
8b4f9a2d 3676 error = zfs_sa_readlink(zp, uio);
572e2857 3677 mutex_exit(&zp->z_lock);
34dc7c2f 3678
3558fd73 3679 ZFS_ACCESSTIME_STAMP(zsb, zp);
3558fd73 3680 ZFS_EXIT(zsb);
34dc7c2f
BB
3681 return (error);
3682}
8b4f9a2d 3683EXPORT_SYMBOL(zfs_readlink);
34dc7c2f
BB
3684
3685/*
3558fd73 3686 * Insert a new entry into directory tdip referencing sip.
34dc7c2f 3687 *
3558fd73
BB
3688 * IN: tdip - Directory to contain new entry.
3689 * sip - inode of new entry.
34dc7c2f
BB
3690 * name - name of new entry.
3691 * cr - credentials of caller.
34dc7c2f
BB
3692 *
3693 * RETURN: 0 if success
3694 * error code if failure
3695 *
3696 * Timestamps:
3558fd73
BB
3697 * tdip - ctime|mtime updated
3698 * sip - ctime updated
34dc7c2f
BB
3699 */
3700/* ARGSUSED */
e5c39b95 3701int
3558fd73 3702zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr)
34dc7c2f 3703{
3558fd73 3704 znode_t *dzp = ITOZ(tdip);
34dc7c2f 3705 znode_t *tzp, *szp;
3558fd73 3706 zfs_sb_t *zsb = ITOZSB(tdip);
34dc7c2f
BB
3707 zilog_t *zilog;
3708 zfs_dirlock_t *dl;
3709 dmu_tx_t *tx;
34dc7c2f
BB
3710 int error;
3711 int zf = ZNEW;
428870ff 3712 uint64_t parent;
572e2857 3713 uid_t owner;
e8b96c60 3714 boolean_t waited = B_FALSE;
34dc7c2f 3715
3558fd73 3716 ASSERT(S_ISDIR(tdip->i_mode));
34dc7c2f 3717
3558fd73 3718 ZFS_ENTER(zsb);
34dc7c2f 3719 ZFS_VERIFY_ZP(dzp);
3558fd73 3720 zilog = zsb->z_log;
34dc7c2f 3721
428870ff
BB
3722 /*
3723 * POSIX dictates that we return EPERM here.
3724 * Better choices include ENOTSUP or EISDIR.
3725 */
3558fd73
BB
3726 if (S_ISDIR(sip->i_mode)) {
3727 ZFS_EXIT(zsb);
2e528b49 3728 return (SET_ERROR(EPERM));
428870ff
BB
3729 }
3730
c0ebc844 3731 if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) {
3558fd73 3732 ZFS_EXIT(zsb);
2e528b49 3733 return (SET_ERROR(EXDEV));
34dc7c2f 3734 }
428870ff 3735
3558fd73 3736 szp = ITOZ(sip);
34dc7c2f
BB
3737 ZFS_VERIFY_ZP(szp);
3738
428870ff
BB
3739 /* Prevent links to .zfs/shares files */
3740
3558fd73 3741 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb),
428870ff 3742 &parent, sizeof (uint64_t))) != 0) {
3558fd73 3743 ZFS_EXIT(zsb);
428870ff
BB
3744 return (error);
3745 }
3558fd73
BB
3746 if (parent == zsb->z_shares_dir) {
3747 ZFS_EXIT(zsb);
2e528b49 3748 return (SET_ERROR(EPERM));
428870ff
BB
3749 }
3750
3558fd73 3751 if (zsb->z_utf8 && u8_validate(name,
34dc7c2f 3752 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3558fd73 3753 ZFS_EXIT(zsb);
2e528b49 3754 return (SET_ERROR(EILSEQ));
34dc7c2f 3755 }
3558fd73 3756#ifdef HAVE_PN_UTILS
34dc7c2f
BB
3757 if (flags & FIGNORECASE)
3758 zf |= ZCILOOK;
3558fd73 3759#endif /* HAVE_PN_UTILS */
34dc7c2f 3760
34dc7c2f
BB
3761 /*
3762 * We do not support links between attributes and non-attributes
3763 * because of the potential security risk of creating links
3764 * into "normal" file space in order to circumvent restrictions
3765 * imposed in attribute space.
3766 */
428870ff 3767 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3558fd73 3768 ZFS_EXIT(zsb);
2e528b49 3769 return (SET_ERROR(EINVAL));
34dc7c2f
BB
3770 }
3771
3558fd73 3772 owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER);
572e2857 3773 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3558fd73 3774 ZFS_EXIT(zsb);
2e528b49 3775 return (SET_ERROR(EPERM));
34dc7c2f
BB
3776 }
3777
149e873a 3778 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) {
3558fd73 3779 ZFS_EXIT(zsb);
34dc7c2f
BB
3780 return (error);
3781 }
3782
428870ff 3783top:
34dc7c2f
BB
3784 /*
3785 * Attempt to lock directory; fail if entry already exists.
3786 */
3787 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3788 if (error) {
3558fd73 3789 ZFS_EXIT(zsb);
34dc7c2f
BB
3790 return (error);
3791 }
3792
3558fd73 3793 tx = dmu_tx_create(zsb->z_os);
428870ff 3794 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
34dc7c2f 3795 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
428870ff
BB
3796 zfs_sa_upgrade_txholds(tx, szp);
3797 zfs_sa_upgrade_txholds(tx, dzp);
e8b96c60 3798 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
34dc7c2f
BB
3799 if (error) {
3800 zfs_dirent_unlock(dl);
fb5f0bc8 3801 if (error == ERESTART) {
e8b96c60 3802 waited = B_TRUE;
34dc7c2f
BB
3803 dmu_tx_wait(tx);
3804 dmu_tx_abort(tx);
3805 goto top;
3806 }
3807 dmu_tx_abort(tx);
3558fd73 3808 ZFS_EXIT(zsb);
34dc7c2f
BB
3809 return (error);
3810 }
3811
3812 error = zfs_link_create(dl, szp, tx, 0);
3813
3814 if (error == 0) {
3815 uint64_t txtype = TX_LINK;
3558fd73 3816#ifdef HAVE_PN_UTILS
34dc7c2f
BB
3817 if (flags & FIGNORECASE)
3818 txtype |= TX_CI;
3558fd73 3819#endif /* HAVE_PN_UTILS */
34dc7c2f
BB
3820 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3821 }
3822
3823 dmu_tx_commit(tx);
3824
3825 zfs_dirent_unlock(dl);
3826
3558fd73 3827 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 3828 zil_commit(zilog, 0);
428870ff 3829
960e08fe
BB
3830 zfs_inode_update(dzp);
3831 zfs_inode_update(szp);
3558fd73 3832 ZFS_EXIT(zsb);
34dc7c2f
BB
3833 return (error);
3834}
e5c39b95 3835EXPORT_SYMBOL(zfs_link);
34dc7c2f 3836
3c0e5c0f 3837static void
119a394a 3838zfs_putpage_commit_cb(void *arg)
3c0e5c0f
BB
3839{
3840 struct page *pp = arg;
3841
119a394a 3842 ClearPageError(pp);
3c0e5c0f
BB
3843 end_page_writeback(pp);
3844}
3845
34dc7c2f 3846/*
3c0e5c0f
BB
3847 * Push a page out to disk, once the page is on stable storage the
3848 * registered commit callback will be run as notification of completion.
34dc7c2f 3849 *
3c0e5c0f
BB
3850 * IN: ip - page mapped for inode.
3851 * pp - page to push (page is locked)
3852 * wbc - writeback control data
34dc7c2f
BB
3853 *
3854 * RETURN: 0 if success
3855 * error code if failure
3856 *
3c0e5c0f
BB
3857 * Timestamps:
3858 * ip - ctime|mtime updated
34dc7c2f
BB
3859 */
3860/* ARGSUSED */
3c0e5c0f
BB
3861int
3862zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc)
34dc7c2f 3863{
3c0e5c0f
BB
3864 znode_t *zp = ITOZ(ip);
3865 zfs_sb_t *zsb = ITOZSB(ip);
3866 loff_t offset;
3867 loff_t pgoff;
4c837f0d
BB
3868 unsigned int pglen;
3869 rl_t *rl;
3c0e5c0f
BB
3870 dmu_tx_t *tx;
3871 caddr_t va;
3872 int err = 0;
3873 uint64_t mtime[2], ctime[2];
3874 sa_bulk_attr_t bulk[3];
3875 int cnt = 0;
3876
4c837f0d
BB
3877 ZFS_ENTER(zsb);
3878 ZFS_VERIFY_ZP(zp);
d164b209 3879
3c0e5c0f
BB
3880 ASSERT(PageLocked(pp));
3881
d1d7e268
MK
3882 pgoff = page_offset(pp); /* Page byte-offset in file */
3883 offset = i_size_read(ip); /* File length in bytes */
3884 pglen = MIN(PAGE_CACHE_SIZE, /* Page length in bytes */
3c0e5c0f
BB
3885 P2ROUNDUP(offset, PAGE_CACHE_SIZE)-pgoff);
3886
3887 /* Page is beyond end of file */
3888 if (pgoff >= offset) {
3889 unlock_page(pp);
4c837f0d 3890 ZFS_EXIT(zsb);
3c0e5c0f
BB
3891 return (0);
3892 }
3893
3894 /* Truncate page length to end of file */
3895 if (pgoff + pglen > offset)
3896 pglen = offset - pgoff;
3897
3898#if 0
34dc7c2f 3899 /*
3c0e5c0f
BB
3900 * FIXME: Allow mmap writes past its quota. The correct fix
3901 * is to register a page_mkwrite() handler to count the page
3902 * against its quota when it is about to be dirtied.
34dc7c2f 3903 */
dde471ef
PJ
3904 if (zfs_owner_overquota(zsb, zp, B_FALSE) ||
3905 zfs_owner_overquota(zsb, zp, B_TRUE)) {
9babb374 3906 err = EDQUOT;
9babb374 3907 }
3c0e5c0f
BB
3908#endif
3909
d958324f
BB
3910 /*
3911 * The ordering here is critical and must adhere to the following
3912 * rules in order to avoid deadlocking in either zfs_read() or
3913 * zfs_free_range() due to a lock inversion.
3914 *
3915 * 1) The page must be unlocked prior to acquiring the range lock.
3916 * This is critical because zfs_read() calls find_lock_page()
3917 * which may block on the page lock while holding the range lock.
3918 *
3919 * 2) Before setting or clearing write back on a page the range lock
3920 * must be held in order to prevent a lock inversion with the
3921 * zfs_free_range() function.
3922 */
3923 unlock_page(pp);
74328ee1 3924 rl = zfs_range_lock(zp, pgoff, pglen, RL_WRITER);
3c0e5c0f 3925 set_page_writeback(pp);
3c0e5c0f 3926
dde471ef 3927 tx = dmu_tx_create(zsb->z_os);
3c0e5c0f 3928 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen);
428870ff
BB
3929 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3930 zfs_sa_upgrade_txholds(tx, zp);
d958324f 3931
fb5f0bc8 3932 err = dmu_tx_assign(tx, TXG_NOWAIT);
34dc7c2f 3933 if (err != 0) {
3c0e5c0f 3934 if (err == ERESTART)
34dc7c2f 3935 dmu_tx_wait(tx);
3c0e5c0f 3936
34dc7c2f 3937 dmu_tx_abort(tx);
119a394a
ED
3938 __set_page_dirty_nobuffers(pp);
3939 ClearPageError(pp);
3940 end_page_writeback(pp);
4c837f0d
BB
3941 zfs_range_unlock(rl);
3942 ZFS_EXIT(zsb);
3c0e5c0f 3943 return (err);
34dc7c2f
BB
3944 }
3945
dde471ef 3946 va = kmap(pp);
3c0e5c0f
BB
3947 ASSERT3U(pglen, <=, PAGE_CACHE_SIZE);
3948 dmu_write(zsb->z_os, zp->z_id, pgoff, pglen, va, tx);
dde471ef 3949 kunmap(pp);
34dc7c2f 3950
3c0e5c0f
BB
3951 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
3952 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
3953 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zsb), NULL, &zp->z_pflags, 8);
428870ff 3954
d3aa3ea9
BB
3955 /* Preserve the mtime and ctime provided by the inode */
3956 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
3957 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
3958 zp->z_atime_dirty = 0;
3959 zp->z_seq++;
3960
3961 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
3962
119a394a
ED
3963 zfs_log_write(zsb->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0,
3964 zfs_putpage_commit_cb, pp);
45d1cae3 3965 dmu_tx_commit(tx);
d3aa3ea9 3966
4c837f0d 3967 zfs_range_unlock(rl);
34dc7c2f 3968
119a394a
ED
3969 if (wbc->sync_mode != WB_SYNC_NONE) {
3970 /*
3971 * Note that this is rarely called under writepages(), because
3972 * writepages() normally handles the entire commit for
3973 * performance reasons.
3974 */
1e8db771
BB
3975 if (zsb->z_log != NULL)
3976 zil_commit(zsb->z_log, zp->z_id);
2b286136 3977 }
3c0e5c0f 3978
4c837f0d 3979 ZFS_EXIT(zsb);
3c0e5c0f 3980 return (err);
34dc7c2f
BB
3981}
3982
8780c539
BB
3983/*
3984 * Update the system attributes when the inode has been dirtied. For the
023699cd 3985 * moment we only update the mode, atime, mtime, and ctime.
8780c539
BB
3986 */
3987int
3988zfs_dirty_inode(struct inode *ip, int flags)
3989{
3990 znode_t *zp = ITOZ(ip);
3991 zfs_sb_t *zsb = ITOZSB(ip);
3992 dmu_tx_t *tx;
023699cd
MM
3993 uint64_t mode, atime[2], mtime[2], ctime[2];
3994 sa_bulk_attr_t bulk[4];
8780c539
BB
3995 int error;
3996 int cnt = 0;
3997
c944be5d
BB
3998 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
3999 return (0);
4000
8780c539
BB
4001 ZFS_ENTER(zsb);
4002 ZFS_VERIFY_ZP(zp);
4003
4004 tx = dmu_tx_create(zsb->z_os);
4005
4006 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4007 zfs_sa_upgrade_txholds(tx, zp);
4008
4009 error = dmu_tx_assign(tx, TXG_WAIT);
4010 if (error) {
4011 dmu_tx_abort(tx);
4012 goto out;
4013 }
4014
4015 mutex_enter(&zp->z_lock);
023699cd 4016 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zsb), NULL, &mode, 8);
8780c539
BB
4017 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zsb), NULL, &atime, 16);
4018 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zsb), NULL, &mtime, 16);
4019 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zsb), NULL, &ctime, 16);
4020
023699cd 4021 /* Preserve the mode, mtime and ctime provided by the inode */
8780c539
BB
4022 ZFS_TIME_ENCODE(&ip->i_atime, atime);
4023 ZFS_TIME_ENCODE(&ip->i_mtime, mtime);
4024 ZFS_TIME_ENCODE(&ip->i_ctime, ctime);
023699cd
MM
4025 mode = ip->i_mode;
4026
4027 zp->z_mode = mode;
8780c539
BB
4028 zp->z_atime_dirty = 0;
4029
4030 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx);
4031 mutex_exit(&zp->z_lock);
4032
4033 dmu_tx_commit(tx);
4034out:
4035 ZFS_EXIT(zsb);
4036 return (error);
4037}
4038EXPORT_SYMBOL(zfs_dirty_inode);
4039
34dc7c2f
BB
4040/*ARGSUSED*/
4041void
c0d35759 4042zfs_inactive(struct inode *ip)
34dc7c2f 4043{
c0d35759
BB
4044 znode_t *zp = ITOZ(ip);
4045 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
4046 int error;
4047
ebe7e575
BB
4048 if (zfsctl_is_node(ip)) {
4049 zfsctl_inode_inactive(ip);
4050 return;
4051 }
34dc7c2f 4052
c0d35759
BB
4053 rw_enter(&zsb->z_teardown_inactive_lock, RW_READER);
4054 if (zp->z_sa_hdl == NULL) {
4055 rw_exit(&zsb->z_teardown_inactive_lock);
4056 return;
34dc7c2f
BB
4057 }
4058
4059 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
c0d35759 4060 dmu_tx_t *tx = dmu_tx_create(zsb->z_os);
34dc7c2f 4061
428870ff
BB
4062 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4063 zfs_sa_upgrade_txholds(tx, zp);
34dc7c2f
BB
4064 error = dmu_tx_assign(tx, TXG_WAIT);
4065 if (error) {
4066 dmu_tx_abort(tx);
4067 } else {
34dc7c2f 4068 mutex_enter(&zp->z_lock);
3558fd73 4069 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb),
428870ff 4070 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
34dc7c2f
BB
4071 zp->z_atime_dirty = 0;
4072 mutex_exit(&zp->z_lock);
4073 dmu_tx_commit(tx);
4074 }
4075 }
4076
4077 zfs_zinactive(zp);
3558fd73 4078 rw_exit(&zsb->z_teardown_inactive_lock);
34dc7c2f 4079}
e5c39b95 4080EXPORT_SYMBOL(zfs_inactive);
34dc7c2f
BB
4081
4082/*
4083 * Bounds-check the seek operation.
4084 *
3558fd73 4085 * IN: ip - inode seeking within
34dc7c2f
BB
4086 * ooff - old file offset
4087 * noffp - pointer to new file offset
4088 * ct - caller context
4089 *
4090 * RETURN: 0 if success
4091 * EINVAL if new offset invalid
4092 */
4093/* ARGSUSED */
3558fd73 4094int
9623f736 4095zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp)
34dc7c2f 4096{
3558fd73 4097 if (S_ISDIR(ip->i_mode))
34dc7c2f
BB
4098 return (0);
4099 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4100}
3558fd73 4101EXPORT_SYMBOL(zfs_seek);
34dc7c2f 4102
34dc7c2f 4103/*
dde471ef 4104 * Fill pages with data from the disk.
34dc7c2f
BB
4105 */
4106static int
dde471ef 4107zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages)
34dc7c2f 4108{
d1d7e268
MK
4109 znode_t *zp = ITOZ(ip);
4110 zfs_sb_t *zsb = ITOZSB(ip);
4111 objset_t *os;
dde471ef 4112 struct page *cur_pp;
d1d7e268
MK
4113 u_offset_t io_off, total;
4114 size_t io_len;
4115 loff_t i_size;
4116 unsigned page_idx;
4117 int err;
34dc7c2f 4118
d1d7e268 4119 os = zsb->z_os;
dde471ef
PJ
4120 io_len = nr_pages << PAGE_CACHE_SHIFT;
4121 i_size = i_size_read(ip);
4122 io_off = page_offset(pl[0]);
4123
4124 if (io_off + io_len > i_size)
4125 io_len = i_size - io_off;
34dc7c2f
BB
4126
4127 /*
dde471ef 4128 * Iterate over list of pages and read each page individually.
34dc7c2f 4129 */
dde471ef
PJ
4130 page_idx = 0;
4131 cur_pp = pl[0];
34dc7c2f 4132 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
d164b209
BB
4133 caddr_t va;
4134
dde471ef 4135 va = kmap(cur_pp);
9babb374
BB
4136 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4137 DMU_READ_PREFETCH);
dde471ef 4138 kunmap(cur_pp);
34dc7c2f 4139 if (err) {
b128c09f
BB
4140 /* convert checksum errors into IO errors */
4141 if (err == ECKSUM)
2e528b49 4142 err = SET_ERROR(EIO);
34dc7c2f
BB
4143 return (err);
4144 }
dde471ef 4145 cur_pp = pl[++page_idx];
34dc7c2f 4146 }
d164b209 4147
34dc7c2f
BB
4148 return (0);
4149}
4150
4151/*
dde471ef 4152 * Uses zfs_fillpage to read data from the file and fill the pages.
34dc7c2f 4153 *
dde471ef
PJ
4154 * IN: ip - inode of file to get data from.
4155 * pl - list of pages to read
4156 * nr_pages - number of pages to read
34dc7c2f 4157 *
d3cc8b15 4158 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
4159 *
4160 * Timestamps:
4161 * vp - atime updated
4162 */
4163/* ARGSUSED */
dde471ef
PJ
4164int
4165zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages)
34dc7c2f 4166{
dde471ef
PJ
4167 znode_t *zp = ITOZ(ip);
4168 zfs_sb_t *zsb = ITOZSB(ip);
4169 int err;
d164b209 4170
d164b209
BB
4171 if (pl == NULL)
4172 return (0);
34dc7c2f 4173
dde471ef 4174 ZFS_ENTER(zsb);
34dc7c2f
BB
4175 ZFS_VERIFY_ZP(zp);
4176
dde471ef 4177 err = zfs_fillpage(ip, pl, nr_pages);
34dc7c2f 4178
dde471ef
PJ
4179 if (!err)
4180 ZFS_ACCESSTIME_STAMP(zsb, zp);
34dc7c2f 4181
dde471ef 4182 ZFS_EXIT(zsb);
34dc7c2f
BB
4183 return (err);
4184}
dde471ef 4185EXPORT_SYMBOL(zfs_getpage);
34dc7c2f
BB
4186
4187/*
e2e7aa2d 4188 * Check ZFS specific permissions to memory map a section of a file.
34dc7c2f 4189 *
e2e7aa2d
BB
4190 * IN: ip - inode of the file to mmap
4191 * off - file offset
4192 * addrp - start address in memory region
4193 * len - length of memory region
4194 * vm_flags- address flags
34dc7c2f 4195 *
e2e7aa2d
BB
4196 * RETURN: 0 if success
4197 * error code if failure
34dc7c2f
BB
4198 */
4199/*ARGSUSED*/
e2e7aa2d
BB
4200int
4201zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len,
4202 unsigned long vm_flags)
34dc7c2f 4203{
e2e7aa2d
BB
4204 znode_t *zp = ITOZ(ip);
4205 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f 4206
e2e7aa2d 4207 ZFS_ENTER(zsb);
34dc7c2f
BB
4208 ZFS_VERIFY_ZP(zp);
4209
e2e7aa2d 4210 if ((vm_flags & VM_WRITE) && (zp->z_pflags &
428870ff 4211 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
e2e7aa2d 4212 ZFS_EXIT(zsb);
2e528b49 4213 return (SET_ERROR(EPERM));
34dc7c2f
BB
4214 }
4215
e2e7aa2d 4216 if ((vm_flags & (VM_READ | VM_EXEC)) &&
428870ff 4217 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
e2e7aa2d 4218 ZFS_EXIT(zsb);
2e528b49 4219 return (SET_ERROR(EACCES));
34dc7c2f
BB
4220 }
4221
34dc7c2f 4222 if (off < 0 || len > MAXOFFSET_T - off) {
e2e7aa2d 4223 ZFS_EXIT(zsb);
2e528b49 4224 return (SET_ERROR(ENXIO));
34dc7c2f
BB
4225 }
4226
e2e7aa2d 4227 ZFS_EXIT(zsb);
34dc7c2f
BB
4228 return (0);
4229}
e2e7aa2d 4230EXPORT_SYMBOL(zfs_map);
34dc7c2f 4231
3558fd73
BB
4232/*
4233 * convoff - converts the given data (start, whence) to the
4234 * given whence.
4235 */
4236int
4237convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset)
4238{
5484965a 4239 vattr_t vap;
3558fd73
BB
4240 int error;
4241
4242 if ((lckdat->l_whence == 2) || (whence == 2)) {
5484965a 4243 if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0))
3558fd73
BB
4244 return (error);
4245 }
4246
4247 switch (lckdat->l_whence) {
4248 case 1:
4249 lckdat->l_start += offset;
4250 break;
4251 case 2:
5484965a 4252 lckdat->l_start += vap.va_size;
3558fd73
BB
4253 /* FALLTHRU */
4254 case 0:
4255 break;
4256 default:
2e528b49 4257 return (SET_ERROR(EINVAL));
3558fd73
BB
4258 }
4259
4260 if (lckdat->l_start < 0)
2e528b49 4261 return (SET_ERROR(EINVAL));
3558fd73
BB
4262
4263 switch (whence) {
4264 case 1:
4265 lckdat->l_start -= offset;
4266 break;
4267 case 2:
5484965a 4268 lckdat->l_start -= vap.va_size;
3558fd73
BB
4269 /* FALLTHRU */
4270 case 0:
4271 break;
4272 default:
2e528b49 4273 return (SET_ERROR(EINVAL));
3558fd73
BB
4274 }
4275
4276 lckdat->l_whence = (short)whence;
4277 return (0);
4278}
4279
34dc7c2f
BB
4280/*
4281 * Free or allocate space in a file. Currently, this function only
4282 * supports the `F_FREESP' command. However, this command is somewhat
4283 * misnamed, as its functionality includes the ability to allocate as
4284 * well as free space.
4285 *
3558fd73 4286 * IN: ip - inode of file to free data in.
34dc7c2f
BB
4287 * cmd - action to take (only F_FREESP supported).
4288 * bfp - section of file to free/alloc.
4289 * flag - current file open mode flags.
4290 * offset - current file offset.
4291 * cr - credentials of caller [UNUSED].
34dc7c2f 4292 *
d3cc8b15 4293 * RETURN: 0 on success, error code on failure.
34dc7c2f
BB
4294 *
4295 * Timestamps:
3558fd73 4296 * ip - ctime|mtime updated
34dc7c2f
BB
4297 */
4298/* ARGSUSED */
e5c39b95 4299int
3558fd73
BB
4300zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag,
4301 offset_t offset, cred_t *cr)
34dc7c2f 4302{
3558fd73
BB
4303 znode_t *zp = ITOZ(ip);
4304 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
4305 uint64_t off, len;
4306 int error;
4307
3558fd73 4308 ZFS_ENTER(zsb);
34dc7c2f
BB
4309 ZFS_VERIFY_ZP(zp);
4310
34dc7c2f 4311 if (cmd != F_FREESP) {
3558fd73 4312 ZFS_EXIT(zsb);
2e528b49 4313 return (SET_ERROR(EINVAL));
34dc7c2f
BB
4314 }
4315
3558fd73
BB
4316 if ((error = convoff(ip, bfp, 0, offset))) {
4317 ZFS_EXIT(zsb);
34dc7c2f
BB
4318 return (error);
4319 }
4320
4321 if (bfp->l_len < 0) {
3558fd73 4322 ZFS_EXIT(zsb);
2e528b49 4323 return (SET_ERROR(EINVAL));
34dc7c2f
BB
4324 }
4325
aec69371
ED
4326 /*
4327 * Permissions aren't checked on Solaris because on this OS
4328 * zfs_space() can only be called with an opened file handle.
4329 * On Linux we can get here through truncate_range() which
4330 * operates directly on inodes, so we need to check access rights.
4331 */
4332 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) {
4333 ZFS_EXIT(zsb);
4334 return (error);
4335 }
4336
34dc7c2f
BB
4337 off = bfp->l_start;
4338 len = bfp->l_len; /* 0 means from off to end of file */
4339
b128c09f 4340 error = zfs_freesp(zp, off, len, flag, TRUE);
34dc7c2f 4341
3558fd73 4342 ZFS_EXIT(zsb);
34dc7c2f
BB
4343 return (error);
4344}
e5c39b95 4345EXPORT_SYMBOL(zfs_space);
34dc7c2f
BB
4346
4347/*ARGSUSED*/
e5c39b95 4348int
3558fd73 4349zfs_fid(struct inode *ip, fid_t *fidp)
34dc7c2f 4350{
3558fd73
BB
4351 znode_t *zp = ITOZ(ip);
4352 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f 4353 uint32_t gen;
428870ff 4354 uint64_t gen64;
34dc7c2f
BB
4355 uint64_t object = zp->z_id;
4356 zfid_short_t *zfid;
428870ff 4357 int size, i, error;
34dc7c2f 4358
3558fd73 4359 ZFS_ENTER(zsb);
34dc7c2f 4360 ZFS_VERIFY_ZP(zp);
428870ff 4361
3558fd73 4362 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb),
428870ff 4363 &gen64, sizeof (uint64_t))) != 0) {
3558fd73 4364 ZFS_EXIT(zsb);
428870ff
BB
4365 return (error);
4366 }
4367
4368 gen = (uint32_t)gen64;
34dc7c2f 4369
3558fd73 4370 size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN;
34dc7c2f
BB
4371 if (fidp->fid_len < size) {
4372 fidp->fid_len = size;
3558fd73 4373 ZFS_EXIT(zsb);
2e528b49 4374 return (SET_ERROR(ENOSPC));
34dc7c2f
BB
4375 }
4376
4377 zfid = (zfid_short_t *)fidp;
4378
4379 zfid->zf_len = size;
4380
4381 for (i = 0; i < sizeof (zfid->zf_object); i++)
4382 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4383
4384 /* Must have a non-zero generation number to distinguish from .zfs */
4385 if (gen == 0)
4386 gen = 1;
4387 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4388 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4389
4390 if (size == LONG_FID_LEN) {
3558fd73 4391 uint64_t objsetid = dmu_objset_id(zsb->z_os);
34dc7c2f
BB
4392 zfid_long_t *zlfid;
4393
4394 zlfid = (zfid_long_t *)fidp;
4395
4396 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4397 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4398
4399 /* XXX - this should be the generation number for the objset */
4400 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4401 zlfid->zf_setgen[i] = 0;
4402 }
4403
3558fd73 4404 ZFS_EXIT(zsb);
34dc7c2f
BB
4405 return (0);
4406}
e5c39b95 4407EXPORT_SYMBOL(zfs_fid);
34dc7c2f 4408
34dc7c2f 4409/*ARGSUSED*/
e5c39b95 4410int
3558fd73 4411zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
34dc7c2f 4412{
3558fd73
BB
4413 znode_t *zp = ITOZ(ip);
4414 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
4415 int error;
4416 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4417
3558fd73 4418 ZFS_ENTER(zsb);
34dc7c2f
BB
4419 ZFS_VERIFY_ZP(zp);
4420 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
3558fd73 4421 ZFS_EXIT(zsb);
34dc7c2f
BB
4422
4423 return (error);
4424}
e5c39b95 4425EXPORT_SYMBOL(zfs_getsecattr);
34dc7c2f
BB
4426
4427/*ARGSUSED*/
e5c39b95 4428int
3558fd73 4429zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr)
34dc7c2f 4430{
3558fd73
BB
4431 znode_t *zp = ITOZ(ip);
4432 zfs_sb_t *zsb = ITOZSB(ip);
34dc7c2f
BB
4433 int error;
4434 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
3558fd73 4435 zilog_t *zilog = zsb->z_log;
34dc7c2f 4436
3558fd73 4437 ZFS_ENTER(zsb);
34dc7c2f 4438 ZFS_VERIFY_ZP(zp);
428870ff 4439
34dc7c2f 4440 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
428870ff 4441
3558fd73 4442 if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS)
572e2857 4443 zil_commit(zilog, 0);
428870ff 4444
3558fd73 4445 ZFS_EXIT(zsb);
34dc7c2f
BB
4446 return (error);
4447}
e5c39b95 4448EXPORT_SYMBOL(zfs_setsecattr);
34dc7c2f 4449
3558fd73 4450#ifdef HAVE_UIO_ZEROCOPY
428870ff
BB
4451/*
4452 * Tunable, both must be a power of 2.
4453 *
4454 * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf
4455 * zcr_blksz_max: if set to less than the file block size, allow loaning out of
3558fd73 4456 * an arcbuf for a partial block read
428870ff
BB
4457 */
4458int zcr_blksz_min = (1 << 10); /* 1K */
4459int zcr_blksz_max = (1 << 17); /* 128K */
4460
4461/*ARGSUSED*/
4462static int
3558fd73 4463zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr)
428870ff 4464{
3558fd73
BB
4465 znode_t *zp = ITOZ(ip);
4466 zfs_sb_t *zsb = ITOZSB(ip);
4467 int max_blksz = zsb->z_max_blksz;
428870ff
BB
4468 uio_t *uio = &xuio->xu_uio;
4469 ssize_t size = uio->uio_resid;
4470 offset_t offset = uio->uio_loffset;
4471 int blksz;
4472 int fullblk, i;
4473 arc_buf_t *abuf;
4474 ssize_t maxsize;
4475 int preamble, postamble;
4476
4477 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
2e528b49 4478 return (SET_ERROR(EINVAL));
428870ff 4479
3558fd73 4480 ZFS_ENTER(zsb);
428870ff
BB
4481 ZFS_VERIFY_ZP(zp);
4482 switch (ioflag) {
4483 case UIO_WRITE:
4484 /*
4485 * Loan out an arc_buf for write if write size is bigger than
4486 * max_blksz, and the file's block size is also max_blksz.
4487 */
4488 blksz = max_blksz;
4489 if (size < blksz || zp->z_blksz != blksz) {
3558fd73 4490 ZFS_EXIT(zsb);
2e528b49 4491 return (SET_ERROR(EINVAL));
428870ff
BB
4492 }
4493 /*
4494 * Caller requests buffers for write before knowing where the
4495 * write offset might be (e.g. NFS TCP write).
4496 */
4497 if (offset == -1) {
4498 preamble = 0;
4499 } else {
4500 preamble = P2PHASE(offset, blksz);
4501 if (preamble) {
4502 preamble = blksz - preamble;
4503 size -= preamble;
4504 }
4505 }
4506
4507 postamble = P2PHASE(size, blksz);
4508 size -= postamble;
4509
4510 fullblk = size / blksz;
4511 (void) dmu_xuio_init(xuio,
4512 (preamble != 0) + fullblk + (postamble != 0));
428870ff
BB
4513
4514 /*
4515 * Have to fix iov base/len for partial buffers. They
4516 * currently represent full arc_buf's.
4517 */
4518 if (preamble) {
4519 /* data begins in the middle of the arc_buf */
4520 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4521 blksz);
4522 ASSERT(abuf);
4523 (void) dmu_xuio_add(xuio, abuf,
4524 blksz - preamble, preamble);
4525 }
4526
4527 for (i = 0; i < fullblk; i++) {
4528 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4529 blksz);
4530 ASSERT(abuf);
4531 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
4532 }
4533
4534 if (postamble) {
4535 /* data ends in the middle of the arc_buf */
4536 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
4537 blksz);
4538 ASSERT(abuf);
4539 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
4540 }
4541 break;
4542 case UIO_READ:
4543 /*
4544 * Loan out an arc_buf for read if the read size is larger than
4545 * the current file block size. Block alignment is not
4546 * considered. Partial arc_buf will be loaned out for read.
4547 */
4548 blksz = zp->z_blksz;
4549 if (blksz < zcr_blksz_min)
4550 blksz = zcr_blksz_min;
4551 if (blksz > zcr_blksz_max)
4552 blksz = zcr_blksz_max;
4553 /* avoid potential complexity of dealing with it */
4554 if (blksz > max_blksz) {
3558fd73 4555 ZFS_EXIT(zsb);
2e528b49 4556 return (SET_ERROR(EINVAL));
428870ff
BB
4557 }
4558
4559 maxsize = zp->z_size - uio->uio_loffset;
4560 if (size > maxsize)
4561 size = maxsize;
4562
3558fd73
BB
4563 if (size < blksz) {
4564 ZFS_EXIT(zsb);
2e528b49 4565 return (SET_ERROR(EINVAL));
428870ff
BB
4566 }
4567 break;
4568 default:
3558fd73 4569 ZFS_EXIT(zsb);
2e528b49 4570 return (SET_ERROR(EINVAL));
428870ff
BB
4571 }
4572
4573 uio->uio_extflg = UIO_XUIO;
4574 XUIO_XUZC_RW(xuio) = ioflag;
3558fd73 4575 ZFS_EXIT(zsb);
428870ff
BB
4576 return (0);
4577}
4578
4579/*ARGSUSED*/
4580static int
3558fd73 4581zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr)
428870ff
BB
4582{
4583 int i;
4584 arc_buf_t *abuf;
4585 int ioflag = XUIO_XUZC_RW(xuio);
4586
4587 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
4588
4589 i = dmu_xuio_cnt(xuio);
4590 while (i-- > 0) {
4591 abuf = dmu_xuio_arcbuf(xuio, i);
4592 /*
4593 * if abuf == NULL, it must be a write buffer
4594 * that has been returned in zfs_write().
4595 */
4596 if (abuf)
4597 dmu_return_arcbuf(abuf);
4598 ASSERT(abuf || ioflag == UIO_WRITE);
4599 }
4600
4601 dmu_xuio_fini(xuio);
4602 return (0);
4603}
3558fd73 4604#endif /* HAVE_UIO_ZEROCOPY */
c409e464
BB
4605
4606#if defined(_KERNEL) && defined(HAVE_SPL)
4607module_param(zfs_read_chunk_size, long, 0644);
4608MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk");
4609#endif