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