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