8416 abd.h is not C++ friendly
[unleashed.git] / usr / src / uts / common / fs / zfs / zfs_vnops.c
blobb2a5cf51b5a1cf6b7782adef0b6d434721123253
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2014 Integros [integros.com]
26 * Copyright 2015 Joyent, Inc.
27 * Copyright 2017 Nexenta Systems, Inc.
30 /* Portions Copyright 2007 Jeremy Teo */
31 /* Portions Copyright 2010 Robert Milkowski */
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/vnode.h>
42 #include <sys/file.h>
43 #include <sys/stat.h>
44 #include <sys/kmem.h>
45 #include <sys/taskq.h>
46 #include <sys/uio.h>
47 #include <sys/vmsystm.h>
48 #include <sys/atomic.h>
49 #include <sys/vm.h>
50 #include <vm/seg_vn.h>
51 #include <vm/pvn.h>
52 #include <vm/as.h>
53 #include <vm/kpm.h>
54 #include <vm/seg_kpm.h>
55 #include <sys/mman.h>
56 #include <sys/pathname.h>
57 #include <sys/cmn_err.h>
58 #include <sys/errno.h>
59 #include <sys/unistd.h>
60 #include <sys/zfs_dir.h>
61 #include <sys/zfs_acl.h>
62 #include <sys/zfs_ioctl.h>
63 #include <sys/fs/zfs.h>
64 #include <sys/dmu.h>
65 #include <sys/dmu_objset.h>
66 #include <sys/spa.h>
67 #include <sys/txg.h>
68 #include <sys/dbuf.h>
69 #include <sys/zap.h>
70 #include <sys/sa.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/sunddi.h>
74 #include <sys/filio.h>
75 #include <sys/sid.h>
76 #include "fs/fs_subr.h"
77 #include <sys/zfs_ctldir.h>
78 #include <sys/zfs_fuid.h>
79 #include <sys/zfs_sa.h>
80 #include <sys/dnlc.h>
81 #include <sys/zfs_rlock.h>
82 #include <sys/extdirent.h>
83 #include <sys/kidmap.h>
84 #include <sys/cred.h>
85 #include <sys/attr.h>
88 * Programming rules.
90 * Each vnode op performs some logical unit of work. To do this, the ZPL must
91 * properly lock its in-core state, create a DMU transaction, do the work,
92 * record this work in the intent log (ZIL), commit the DMU transaction,
93 * and wait for the intent log to commit if it is a synchronous operation.
94 * Moreover, the vnode ops must work in both normal and log replay context.
95 * The ordering of events is important to avoid deadlocks and references
96 * to freed memory. The example below illustrates the following Big Rules:
98 * (1) A check must be made in each zfs thread for a mounted file system.
99 * This is done avoiding races using ZFS_ENTER(zfsvfs).
100 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
101 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
102 * can return EIO from the calling function.
104 * (2) VN_RELE() should always be the last thing except for zil_commit()
105 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
106 * First, if it's the last reference, the vnode/znode
107 * can be freed, so the zp may point to freed memory. Second, the last
108 * reference will call zfs_zinactive(), which may induce a lot of work --
109 * pushing cached pages (which acquires range locks) and syncing out
110 * cached atime changes. Third, zfs_zinactive() may require a new tx,
111 * which could deadlock the system if you were already holding one.
112 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
114 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
115 * as they can span dmu_tx_assign() calls.
117 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
118 * dmu_tx_assign(). This is critical because we don't want to block
119 * while holding locks.
121 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
122 * reduces lock contention and CPU usage when we must wait (note that if
123 * throughput is constrained by the storage, nearly every transaction
124 * must wait).
126 * Note, in particular, that if a lock is sometimes acquired before
127 * the tx assigns, and sometimes after (e.g. z_lock), then failing
128 * to use a non-blocking assign can deadlock the system. The scenario:
130 * Thread A has grabbed a lock before calling dmu_tx_assign().
131 * Thread B is in an already-assigned tx, and blocks for this lock.
132 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
133 * forever, because the previous txg can't quiesce until B's tx commits.
135 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
136 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
137 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
138 * to indicate that this operation has already called dmu_tx_wait().
139 * This will ensure that we don't retry forever, waiting a short bit
140 * each time.
142 * (5) If the operation succeeded, generate the intent log entry for it
143 * before dropping locks. This ensures that the ordering of events
144 * in the intent log matches the order in which they actually occurred.
145 * During ZIL replay the zfs_log_* functions will update the sequence
146 * number to indicate the zil transaction has replayed.
148 * (6) At the end of each vnode op, the DMU tx must always commit,
149 * regardless of whether there were any errors.
151 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
152 * to ensure that synchronous semantics are provided when necessary.
154 * In general, this is how things should be ordered in each vnode op:
156 * ZFS_ENTER(zfsvfs); // exit if unmounted
157 * top:
158 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
159 * rw_enter(...); // grab any other locks you need
160 * tx = dmu_tx_create(...); // get DMU tx
161 * dmu_tx_hold_*(); // hold each object you might modify
162 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
163 * if (error) {
164 * rw_exit(...); // drop locks
165 * zfs_dirent_unlock(dl); // unlock directory entry
166 * VN_RELE(...); // release held vnodes
167 * if (error == ERESTART) {
168 * waited = B_TRUE;
169 * dmu_tx_wait(tx);
170 * dmu_tx_abort(tx);
171 * goto top;
173 * dmu_tx_abort(tx); // abort DMU tx
174 * ZFS_EXIT(zfsvfs); // finished in zfs
175 * return (error); // really out of space
177 * error = do_real_work(); // do whatever this VOP does
178 * if (error == 0)
179 * zfs_log_*(...); // on success, make ZIL entry
180 * dmu_tx_commit(tx); // commit DMU tx -- error or not
181 * rw_exit(...); // drop locks
182 * zfs_dirent_unlock(dl); // unlock directory entry
183 * VN_RELE(...); // release held vnodes
184 * zil_commit(zilog, foid); // synchronous when necessary
185 * ZFS_EXIT(zfsvfs); // finished in zfs
186 * return (error); // done, report error
189 /* ARGSUSED */
190 static int
191 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
193 znode_t *zp = VTOZ(*vpp);
194 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
196 ZFS_ENTER(zfsvfs);
197 ZFS_VERIFY_ZP(zp);
199 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
200 ((flag & FAPPEND) == 0)) {
201 ZFS_EXIT(zfsvfs);
202 return (SET_ERROR(EPERM));
205 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
206 ZTOV(zp)->v_type == VREG &&
207 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
208 if (fs_vscan(*vpp, cr, 0) != 0) {
209 ZFS_EXIT(zfsvfs);
210 return (SET_ERROR(EACCES));
214 /* Keep a count of the synchronous opens in the znode */
215 if (flag & (FSYNC | FDSYNC))
216 atomic_inc_32(&zp->z_sync_cnt);
218 ZFS_EXIT(zfsvfs);
219 return (0);
222 /* ARGSUSED */
223 static int
224 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
225 caller_context_t *ct)
227 znode_t *zp = VTOZ(vp);
228 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
231 * Clean up any locks held by this process on the vp.
233 cleanlocks(vp, ddi_get_pid(), 0);
234 cleanshares(vp, ddi_get_pid());
236 ZFS_ENTER(zfsvfs);
237 ZFS_VERIFY_ZP(zp);
239 /* Decrement the synchronous opens in the znode */
240 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
241 atomic_dec_32(&zp->z_sync_cnt);
243 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
244 ZTOV(zp)->v_type == VREG &&
245 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
246 VERIFY(fs_vscan(vp, cr, 1) == 0);
248 ZFS_EXIT(zfsvfs);
249 return (0);
253 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
254 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
256 static int
257 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
259 znode_t *zp = VTOZ(vp);
260 uint64_t noff = (uint64_t)*off; /* new offset */
261 uint64_t file_sz;
262 int error;
263 boolean_t hole;
265 file_sz = zp->z_size;
266 if (noff >= file_sz) {
267 return (SET_ERROR(ENXIO));
270 if (cmd == _FIO_SEEK_HOLE)
271 hole = B_TRUE;
272 else
273 hole = B_FALSE;
275 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
277 if (error == ESRCH)
278 return (SET_ERROR(ENXIO));
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff > file_sz) {
288 ASSERT(hole);
289 noff = file_sz;
292 if (noff < *off)
293 return (error);
294 *off = noff;
295 return (error);
298 /* ARGSUSED */
299 static int
300 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
301 int *rvalp, caller_context_t *ct)
303 offset_t off;
304 offset_t ndata;
305 dmu_object_info_t doi;
306 int error;
307 zfsvfs_t *zfsvfs;
308 znode_t *zp;
310 switch (com) {
311 case _FIOFFS:
313 return (zfs_sync(vp->v_vfsp, 0, cred));
316 * The following two ioctls are used by bfu. Faking out,
317 * necessary to avoid bfu errors.
320 case _FIOGDIO:
321 case _FIOSDIO:
323 return (0);
326 case _FIO_SEEK_DATA:
327 case _FIO_SEEK_HOLE:
329 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
330 return (SET_ERROR(EFAULT));
332 zp = VTOZ(vp);
333 zfsvfs = zp->z_zfsvfs;
334 ZFS_ENTER(zfsvfs);
335 ZFS_VERIFY_ZP(zp);
337 /* offset parameter is in/out */
338 error = zfs_holey(vp, com, &off);
339 ZFS_EXIT(zfsvfs);
340 if (error)
341 return (error);
342 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
343 return (SET_ERROR(EFAULT));
344 return (0);
346 case _FIO_COUNT_FILLED:
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
351 * ZFS object.
353 zp = VTOZ(vp);
354 zfsvfs = zp->z_zfsvfs;
355 ZFS_ENTER(zfsvfs);
356 ZFS_VERIFY_ZP(zp);
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
361 * updated.
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
364 if (error) {
365 ZFS_EXIT(zfsvfs);
366 return (error);
370 * Retrieve fill count from DMU object.
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
373 if (error) {
374 ZFS_EXIT(zfsvfs);
375 return (error);
378 ndata = doi.doi_fill_count;
380 ZFS_EXIT(zfsvfs);
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
383 return (0);
386 return (SET_ERROR(ENOTTY));
390 * Utility functions to map and unmap a single physical page. These
391 * are used to manage the mappable copies of ZFS file data, and therefore
392 * do not update ref/mod bits.
394 caddr_t
395 zfs_map_page(page_t *pp, enum seg_rw rw)
397 if (kpm_enable)
398 return (hat_kpm_mapin(pp, 0));
399 ASSERT(rw == S_READ || rw == S_WRITE);
400 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
401 (caddr_t)-1));
404 void
405 zfs_unmap_page(page_t *pp, caddr_t addr)
407 if (kpm_enable) {
408 hat_kpm_mapout(pp, 0, addr);
409 } else {
410 ppmapout(addr);
415 * When a file is memory mapped, we must keep the IO data synchronized
416 * between the DMU cache and the memory mapped pages. What this means:
418 * On Write: If we find a memory mapped page, we write to *both*
419 * the page and the dmu buffer.
421 static void
422 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
424 int64_t off;
426 off = start & PAGEOFFSET;
427 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
428 page_t *pp;
429 uint64_t nbytes = MIN(PAGESIZE - off, len);
431 if (pp = page_lookup(vp, start, SE_SHARED)) {
432 caddr_t va;
434 va = zfs_map_page(pp, S_WRITE);
435 (void) dmu_read(os, oid, start+off, nbytes, va+off,
436 DMU_READ_PREFETCH);
437 zfs_unmap_page(pp, va);
438 page_unlock(pp);
440 len -= nbytes;
441 off = 0;
446 * When a file is memory mapped, we must keep the IO data synchronized
447 * between the DMU cache and the memory mapped pages. What this means:
449 * On Read: We "read" preferentially from memory mapped pages,
450 * else we default from the dmu buffer.
452 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
453 * the file is memory mapped.
455 static int
456 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
458 znode_t *zp = VTOZ(vp);
459 int64_t start, off;
460 int len = nbytes;
461 int error = 0;
463 start = uio->uio_loffset;
464 off = start & PAGEOFFSET;
465 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
466 page_t *pp;
467 uint64_t bytes = MIN(PAGESIZE - off, len);
469 if (pp = page_lookup(vp, start, SE_SHARED)) {
470 caddr_t va;
472 va = zfs_map_page(pp, S_READ);
473 error = uiomove(va + off, bytes, UIO_READ, uio);
474 zfs_unmap_page(pp, va);
475 page_unlock(pp);
476 } else {
477 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
478 uio, bytes);
480 len -= bytes;
481 off = 0;
482 if (error)
483 break;
485 return (error);
488 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
491 * Read bytes from specified file into supplied buffer.
493 * IN: vp - vnode of file to be read from.
494 * uio - structure supplying read location, range info,
495 * and return buffer.
496 * ioflag - SYNC flags; used to provide FRSYNC semantics.
497 * cr - credentials of caller.
498 * ct - caller context
500 * OUT: uio - updated offset and range, buffer filled.
502 * RETURN: 0 on success, error code on failure.
504 * Side Effects:
505 * vp - atime updated if byte count > 0
507 /* ARGSUSED */
508 static int
509 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
511 znode_t *zp = VTOZ(vp);
512 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
513 ssize_t n, nbytes;
514 int error = 0;
515 rl_t *rl;
516 xuio_t *xuio = NULL;
518 ZFS_ENTER(zfsvfs);
519 ZFS_VERIFY_ZP(zp);
521 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
522 ZFS_EXIT(zfsvfs);
523 return (SET_ERROR(EACCES));
527 * Validate file offset
529 if (uio->uio_loffset < (offset_t)0) {
530 ZFS_EXIT(zfsvfs);
531 return (SET_ERROR(EINVAL));
535 * Fasttrack empty reads
537 if (uio->uio_resid == 0) {
538 ZFS_EXIT(zfsvfs);
539 return (0);
543 * Check for mandatory locks
545 if (MANDMODE(zp->z_mode)) {
546 if (error = chklock(vp, FREAD,
547 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
548 ZFS_EXIT(zfsvfs);
549 return (error);
554 * If we're in FRSYNC mode, sync out this znode before reading it.
556 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
557 zil_commit(zfsvfs->z_log, zp->z_id);
560 * Lock the range against changes.
562 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
565 * If we are reading past end-of-file we can skip
566 * to the end; but we might still need to set atime.
568 if (uio->uio_loffset >= zp->z_size) {
569 error = 0;
570 goto out;
573 ASSERT(uio->uio_loffset < zp->z_size);
574 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
576 if ((uio->uio_extflg == UIO_XUIO) &&
577 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
578 int nblk;
579 int blksz = zp->z_blksz;
580 uint64_t offset = uio->uio_loffset;
582 xuio = (xuio_t *)uio;
583 if ((ISP2(blksz))) {
584 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
585 blksz)) / blksz;
586 } else {
587 ASSERT(offset + n <= blksz);
588 nblk = 1;
590 (void) dmu_xuio_init(xuio, nblk);
592 if (vn_has_cached_data(vp)) {
594 * For simplicity, we always allocate a full buffer
595 * even if we only expect to read a portion of a block.
597 while (--nblk >= 0) {
598 (void) dmu_xuio_add(xuio,
599 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
600 blksz), 0, blksz);
605 while (n > 0) {
606 nbytes = MIN(n, zfs_read_chunk_size -
607 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
609 if (vn_has_cached_data(vp)) {
610 error = mappedread(vp, nbytes, uio);
611 } else {
612 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
613 uio, nbytes);
615 if (error) {
616 /* convert checksum errors into IO errors */
617 if (error == ECKSUM)
618 error = SET_ERROR(EIO);
619 break;
622 n -= nbytes;
624 out:
625 zfs_range_unlock(rl);
627 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
628 ZFS_EXIT(zfsvfs);
629 return (error);
633 * Write the bytes to a file.
635 * IN: vp - vnode of file to be written to.
636 * uio - structure supplying write location, range info,
637 * and data buffer.
638 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
639 * set if in append mode.
640 * cr - credentials of caller.
641 * ct - caller context (NFS/CIFS fem monitor only)
643 * OUT: uio - updated offset and range.
645 * RETURN: 0 on success, error code on failure.
647 * Timestamps:
648 * vp - ctime|mtime updated if byte count > 0
651 /* ARGSUSED */
652 static int
653 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
655 znode_t *zp = VTOZ(vp);
656 rlim64_t limit = uio->uio_llimit;
657 ssize_t start_resid = uio->uio_resid;
658 ssize_t tx_bytes;
659 uint64_t end_size;
660 dmu_tx_t *tx;
661 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
662 zilog_t *zilog;
663 offset_t woff;
664 ssize_t n, nbytes;
665 rl_t *rl;
666 int max_blksz = zfsvfs->z_max_blksz;
667 int error = 0;
668 arc_buf_t *abuf;
669 iovec_t *aiov = NULL;
670 xuio_t *xuio = NULL;
671 int i_iov = 0;
672 int iovcnt = uio->uio_iovcnt;
673 iovec_t *iovp = uio->uio_iov;
674 int write_eof;
675 int count = 0;
676 sa_bulk_attr_t bulk[4];
677 uint64_t mtime[2], ctime[2];
680 * Fasttrack empty write
682 n = start_resid;
683 if (n == 0)
684 return (0);
686 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
687 limit = MAXOFFSET_T;
689 ZFS_ENTER(zfsvfs);
690 ZFS_VERIFY_ZP(zp);
692 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
693 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
694 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
695 &zp->z_size, 8);
696 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
697 &zp->z_pflags, 8);
700 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
701 * callers might not be able to detect properly that we are read-only,
702 * so check it explicitly here.
704 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
705 ZFS_EXIT(zfsvfs);
706 return (SET_ERROR(EROFS));
710 * If immutable or not appending then return EPERM.
711 * Intentionally allow ZFS_READONLY through here.
712 * See zfs_zaccess_common()
714 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
715 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
716 (uio->uio_loffset < zp->z_size))) {
717 ZFS_EXIT(zfsvfs);
718 return (SET_ERROR(EPERM));
721 zilog = zfsvfs->z_log;
724 * Validate file offset
726 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
727 if (woff < 0) {
728 ZFS_EXIT(zfsvfs);
729 return (SET_ERROR(EINVAL));
733 * Check for mandatory locks before calling zfs_range_lock()
734 * in order to prevent a deadlock with locks set via fcntl().
736 if (MANDMODE((mode_t)zp->z_mode) &&
737 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
738 ZFS_EXIT(zfsvfs);
739 return (error);
743 * Pre-fault the pages to ensure slow (eg NFS) pages
744 * don't hold up txg.
745 * Skip this if uio contains loaned arc_buf.
747 if ((uio->uio_extflg == UIO_XUIO) &&
748 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
749 xuio = (xuio_t *)uio;
750 else
751 uio_prefaultpages(MIN(n, max_blksz), uio);
754 * If in append mode, set the io offset pointer to eof.
756 if (ioflag & FAPPEND) {
758 * Obtain an appending range lock to guarantee file append
759 * semantics. We reset the write offset once we have the lock.
761 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
762 woff = rl->r_off;
763 if (rl->r_len == UINT64_MAX) {
765 * We overlocked the file because this write will cause
766 * the file block size to increase.
767 * Note that zp_size cannot change with this lock held.
769 woff = zp->z_size;
771 uio->uio_loffset = woff;
772 } else {
774 * Note that if the file block size will change as a result of
775 * this write, then this range lock will lock the entire file
776 * so that we can re-write the block safely.
778 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
781 if (woff >= limit) {
782 zfs_range_unlock(rl);
783 ZFS_EXIT(zfsvfs);
784 return (SET_ERROR(EFBIG));
787 if ((woff + n) > limit || woff > (limit - n))
788 n = limit - woff;
790 /* Will this write extend the file length? */
791 write_eof = (woff + n > zp->z_size);
793 end_size = MAX(zp->z_size, woff + n);
796 * Write the file in reasonable size chunks. Each chunk is written
797 * in a separate transaction; this keeps the intent log records small
798 * and allows us to do more fine-grained space accounting.
800 while (n > 0) {
801 abuf = NULL;
802 woff = uio->uio_loffset;
803 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
804 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
805 if (abuf != NULL)
806 dmu_return_arcbuf(abuf);
807 error = SET_ERROR(EDQUOT);
808 break;
811 if (xuio && abuf == NULL) {
812 ASSERT(i_iov < iovcnt);
813 aiov = &iovp[i_iov];
814 abuf = dmu_xuio_arcbuf(xuio, i_iov);
815 dmu_xuio_clear(xuio, i_iov);
816 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
817 iovec_t *, aiov, arc_buf_t *, abuf);
818 ASSERT((aiov->iov_base == abuf->b_data) ||
819 ((char *)aiov->iov_base - (char *)abuf->b_data +
820 aiov->iov_len == arc_buf_size(abuf)));
821 i_iov++;
822 } else if (abuf == NULL && n >= max_blksz &&
823 woff >= zp->z_size &&
824 P2PHASE(woff, max_blksz) == 0 &&
825 zp->z_blksz == max_blksz) {
827 * This write covers a full block. "Borrow" a buffer
828 * from the dmu so that we can fill it before we enter
829 * a transaction. This avoids the possibility of
830 * holding up the transaction if the data copy hangs
831 * up on a pagefault (e.g., from an NFS server mapping).
833 size_t cbytes;
835 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
836 max_blksz);
837 ASSERT(abuf != NULL);
838 ASSERT(arc_buf_size(abuf) == max_blksz);
839 if (error = uiocopy(abuf->b_data, max_blksz,
840 UIO_WRITE, uio, &cbytes)) {
841 dmu_return_arcbuf(abuf);
842 break;
844 ASSERT(cbytes == max_blksz);
848 * Start a transaction.
850 tx = dmu_tx_create(zfsvfs->z_os);
851 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
852 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
853 zfs_sa_upgrade_txholds(tx, zp);
854 error = dmu_tx_assign(tx, TXG_WAIT);
855 if (error) {
856 dmu_tx_abort(tx);
857 if (abuf != NULL)
858 dmu_return_arcbuf(abuf);
859 break;
863 * If zfs_range_lock() over-locked we grow the blocksize
864 * and then reduce the lock range. This will only happen
865 * on the first iteration since zfs_range_reduce() will
866 * shrink down r_len to the appropriate size.
868 if (rl->r_len == UINT64_MAX) {
869 uint64_t new_blksz;
871 if (zp->z_blksz > max_blksz) {
873 * File's blocksize is already larger than the
874 * "recordsize" property. Only let it grow to
875 * the next power of 2.
877 ASSERT(!ISP2(zp->z_blksz));
878 new_blksz = MIN(end_size,
879 1 << highbit64(zp->z_blksz));
880 } else {
881 new_blksz = MIN(end_size, max_blksz);
883 zfs_grow_blocksize(zp, new_blksz, tx);
884 zfs_range_reduce(rl, woff, n);
888 * XXX - should we really limit each write to z_max_blksz?
889 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
891 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
893 if (abuf == NULL) {
894 tx_bytes = uio->uio_resid;
895 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
896 uio, nbytes, tx);
897 tx_bytes -= uio->uio_resid;
898 } else {
899 tx_bytes = nbytes;
900 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
902 * If this is not a full block write, but we are
903 * extending the file past EOF and this data starts
904 * block-aligned, use assign_arcbuf(). Otherwise,
905 * write via dmu_write().
907 if (tx_bytes < max_blksz && (!write_eof ||
908 aiov->iov_base != abuf->b_data)) {
909 ASSERT(xuio);
910 dmu_write(zfsvfs->z_os, zp->z_id, woff,
911 aiov->iov_len, aiov->iov_base, tx);
912 dmu_return_arcbuf(abuf);
913 xuio_stat_wbuf_copied();
914 } else {
915 ASSERT(xuio || tx_bytes == max_blksz);
916 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
917 woff, abuf, tx);
919 ASSERT(tx_bytes <= uio->uio_resid);
920 uioskip(uio, tx_bytes);
922 if (tx_bytes && vn_has_cached_data(vp)) {
923 update_pages(vp, woff,
924 tx_bytes, zfsvfs->z_os, zp->z_id);
928 * If we made no progress, we're done. If we made even
929 * partial progress, update the znode and ZIL accordingly.
931 if (tx_bytes == 0) {
932 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
933 (void *)&zp->z_size, sizeof (uint64_t), tx);
934 dmu_tx_commit(tx);
935 ASSERT(error != 0);
936 break;
940 * Clear Set-UID/Set-GID bits on successful write if not
941 * privileged and at least one of the excute bits is set.
943 * It would be nice to to this after all writes have
944 * been done, but that would still expose the ISUID/ISGID
945 * to another app after the partial write is committed.
947 * Note: we don't call zfs_fuid_map_id() here because
948 * user 0 is not an ephemeral uid.
950 mutex_enter(&zp->z_acl_lock);
951 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
952 (S_IXUSR >> 6))) != 0 &&
953 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
954 secpolicy_vnode_setid_retain(cr,
955 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
956 uint64_t newmode;
957 zp->z_mode &= ~(S_ISUID | S_ISGID);
958 newmode = zp->z_mode;
959 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
960 (void *)&newmode, sizeof (uint64_t), tx);
962 mutex_exit(&zp->z_acl_lock);
964 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
965 B_TRUE);
968 * Update the file size (zp_size) if it has changed;
969 * account for possible concurrent updates.
971 while ((end_size = zp->z_size) < uio->uio_loffset) {
972 (void) atomic_cas_64(&zp->z_size, end_size,
973 uio->uio_loffset);
974 ASSERT(error == 0);
977 * If we are replaying and eof is non zero then force
978 * the file size to the specified eof. Note, there's no
979 * concurrency during replay.
981 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
982 zp->z_size = zfsvfs->z_replay_eof;
984 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
986 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
987 dmu_tx_commit(tx);
989 if (error != 0)
990 break;
991 ASSERT(tx_bytes == nbytes);
992 n -= nbytes;
994 if (!xuio && n > 0)
995 uio_prefaultpages(MIN(n, max_blksz), uio);
998 zfs_range_unlock(rl);
1001 * If we're in replay mode, or we made no progress, return error.
1002 * Otherwise, it's at least a partial write, so it's successful.
1004 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1005 ZFS_EXIT(zfsvfs);
1006 return (error);
1009 if (ioflag & (FSYNC | FDSYNC) ||
1010 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1011 zil_commit(zilog, zp->z_id);
1013 ZFS_EXIT(zfsvfs);
1014 return (0);
1017 void
1018 zfs_get_done(zgd_t *zgd, int error)
1020 znode_t *zp = zgd->zgd_private;
1021 objset_t *os = zp->z_zfsvfs->z_os;
1023 if (zgd->zgd_db)
1024 dmu_buf_rele(zgd->zgd_db, zgd);
1026 zfs_range_unlock(zgd->zgd_rl);
1029 * Release the vnode asynchronously as we currently have the
1030 * txg stopped from syncing.
1032 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1034 if (error == 0 && zgd->zgd_bp)
1035 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
1037 kmem_free(zgd, sizeof (zgd_t));
1040 #ifdef DEBUG
1041 static int zil_fault_io = 0;
1042 #endif
1045 * Get data to generate a TX_WRITE intent log record.
1048 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
1050 zfsvfs_t *zfsvfs = arg;
1051 objset_t *os = zfsvfs->z_os;
1052 znode_t *zp;
1053 uint64_t object = lr->lr_foid;
1054 uint64_t offset = lr->lr_offset;
1055 uint64_t size = lr->lr_length;
1056 blkptr_t *bp = &lr->lr_blkptr;
1057 dmu_buf_t *db;
1058 zgd_t *zgd;
1059 int error = 0;
1061 ASSERT(zio != NULL);
1062 ASSERT(size != 0);
1065 * Nothing to do if the file has been removed
1067 if (zfs_zget(zfsvfs, object, &zp) != 0)
1068 return (SET_ERROR(ENOENT));
1069 if (zp->z_unlinked) {
1071 * Release the vnode asynchronously as we currently have the
1072 * txg stopped from syncing.
1074 VN_RELE_ASYNC(ZTOV(zp),
1075 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1076 return (SET_ERROR(ENOENT));
1079 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1080 zgd->zgd_zilog = zfsvfs->z_log;
1081 zgd->zgd_private = zp;
1084 * Write records come in two flavors: immediate and indirect.
1085 * For small writes it's cheaper to store the data with the
1086 * log record (immediate); for large writes it's cheaper to
1087 * sync the data and get a pointer to it (indirect) so that
1088 * we don't have to write the data twice.
1090 if (buf != NULL) { /* immediate write */
1091 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1092 /* test for truncation needs to be done while range locked */
1093 if (offset >= zp->z_size) {
1094 error = SET_ERROR(ENOENT);
1095 } else {
1096 error = dmu_read(os, object, offset, size, buf,
1097 DMU_READ_NO_PREFETCH);
1099 ASSERT(error == 0 || error == ENOENT);
1100 } else { /* indirect write */
1102 * Have to lock the whole block to ensure when it's
1103 * written out and it's checksum is being calculated
1104 * that no one can change the data. We need to re-check
1105 * blocksize after we get the lock in case it's changed!
1107 for (;;) {
1108 uint64_t blkoff;
1109 size = zp->z_blksz;
1110 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1111 offset -= blkoff;
1112 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1113 RL_READER);
1114 if (zp->z_blksz == size)
1115 break;
1116 offset += blkoff;
1117 zfs_range_unlock(zgd->zgd_rl);
1119 /* test for truncation needs to be done while range locked */
1120 if (lr->lr_offset >= zp->z_size)
1121 error = SET_ERROR(ENOENT);
1122 #ifdef DEBUG
1123 if (zil_fault_io) {
1124 error = SET_ERROR(EIO);
1125 zil_fault_io = 0;
1127 #endif
1128 if (error == 0)
1129 error = dmu_buf_hold(os, object, offset, zgd, &db,
1130 DMU_READ_NO_PREFETCH);
1132 if (error == 0) {
1133 blkptr_t *obp = dmu_buf_get_blkptr(db);
1134 if (obp) {
1135 ASSERT(BP_IS_HOLE(bp));
1136 *bp = *obp;
1139 zgd->zgd_db = db;
1140 zgd->zgd_bp = bp;
1142 ASSERT(db->db_offset == offset);
1143 ASSERT(db->db_size == size);
1145 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1146 zfs_get_done, zgd);
1147 ASSERT(error || lr->lr_length <= size);
1150 * On success, we need to wait for the write I/O
1151 * initiated by dmu_sync() to complete before we can
1152 * release this dbuf. We will finish everything up
1153 * in the zfs_get_done() callback.
1155 if (error == 0)
1156 return (0);
1158 if (error == EALREADY) {
1159 lr->lr_common.lrc_txtype = TX_WRITE2;
1160 error = 0;
1165 zfs_get_done(zgd, error);
1167 return (error);
1170 /*ARGSUSED*/
1171 static int
1172 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1173 caller_context_t *ct)
1175 znode_t *zp = VTOZ(vp);
1176 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1177 int error;
1179 ZFS_ENTER(zfsvfs);
1180 ZFS_VERIFY_ZP(zp);
1182 if (flag & V_ACE_MASK)
1183 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1184 else
1185 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1187 ZFS_EXIT(zfsvfs);
1188 return (error);
1192 * If vnode is for a device return a specfs vnode instead.
1194 static int
1195 specvp_check(vnode_t **vpp, cred_t *cr)
1197 int error = 0;
1199 if (IS_DEVVP(*vpp)) {
1200 struct vnode *svp;
1202 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1203 VN_RELE(*vpp);
1204 if (svp == NULL)
1205 error = SET_ERROR(ENOSYS);
1206 *vpp = svp;
1208 return (error);
1213 * Lookup an entry in a directory, or an extended attribute directory.
1214 * If it exists, return a held vnode reference for it.
1216 * IN: dvp - vnode of directory to search.
1217 * nm - name of entry to lookup.
1218 * pnp - full pathname to lookup [UNUSED].
1219 * flags - LOOKUP_XATTR set if looking for an attribute.
1220 * rdir - root directory vnode [UNUSED].
1221 * cr - credentials of caller.
1222 * ct - caller context
1223 * direntflags - directory lookup flags
1224 * realpnp - returned pathname.
1226 * OUT: vpp - vnode of located entry, NULL if not found.
1228 * RETURN: 0 on success, error code on failure.
1230 * Timestamps:
1231 * NA
1233 /* ARGSUSED */
1234 static int
1235 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1236 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1237 int *direntflags, pathname_t *realpnp)
1239 znode_t *zdp = VTOZ(dvp);
1240 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1241 int error = 0;
1244 * Fast path lookup, however we must skip DNLC lookup
1245 * for case folding or normalizing lookups because the
1246 * DNLC code only stores the passed in name. This means
1247 * creating 'a' and removing 'A' on a case insensitive
1248 * file system would work, but DNLC still thinks 'a'
1249 * exists and won't let you create it again on the next
1250 * pass through fast path.
1252 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1254 if (dvp->v_type != VDIR) {
1255 return (SET_ERROR(ENOTDIR));
1256 } else if (zdp->z_sa_hdl == NULL) {
1257 return (SET_ERROR(EIO));
1260 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1261 error = zfs_fastaccesschk_execute(zdp, cr);
1262 if (!error) {
1263 *vpp = dvp;
1264 VN_HOLD(*vpp);
1265 return (0);
1267 return (error);
1268 } else if (!zdp->z_zfsvfs->z_norm &&
1269 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1271 vnode_t *tvp = dnlc_lookup(dvp, nm);
1273 if (tvp) {
1274 error = zfs_fastaccesschk_execute(zdp, cr);
1275 if (error) {
1276 VN_RELE(tvp);
1277 return (error);
1279 if (tvp == DNLC_NO_VNODE) {
1280 VN_RELE(tvp);
1281 return (SET_ERROR(ENOENT));
1282 } else {
1283 *vpp = tvp;
1284 return (specvp_check(vpp, cr));
1290 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1292 ZFS_ENTER(zfsvfs);
1293 ZFS_VERIFY_ZP(zdp);
1295 *vpp = NULL;
1297 if (flags & LOOKUP_XATTR) {
1299 * If the xattr property is off, refuse the lookup request.
1301 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1302 ZFS_EXIT(zfsvfs);
1303 return (SET_ERROR(EINVAL));
1307 * We don't allow recursive attributes..
1308 * Maybe someday we will.
1310 if (zdp->z_pflags & ZFS_XATTR) {
1311 ZFS_EXIT(zfsvfs);
1312 return (SET_ERROR(EINVAL));
1315 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1316 ZFS_EXIT(zfsvfs);
1317 return (error);
1321 * Do we have permission to get into attribute directory?
1324 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1325 B_FALSE, cr)) {
1326 VN_RELE(*vpp);
1327 *vpp = NULL;
1330 ZFS_EXIT(zfsvfs);
1331 return (error);
1334 if (dvp->v_type != VDIR) {
1335 ZFS_EXIT(zfsvfs);
1336 return (SET_ERROR(ENOTDIR));
1340 * Check accessibility of directory.
1343 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1344 ZFS_EXIT(zfsvfs);
1345 return (error);
1348 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1349 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1350 ZFS_EXIT(zfsvfs);
1351 return (SET_ERROR(EILSEQ));
1354 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1355 if (error == 0)
1356 error = specvp_check(vpp, cr);
1358 ZFS_EXIT(zfsvfs);
1359 return (error);
1363 * Attempt to create a new entry in a directory. If the entry
1364 * already exists, truncate the file if permissible, else return
1365 * an error. Return the vp of the created or trunc'd file.
1367 * IN: dvp - vnode of directory to put new file entry in.
1368 * name - name of new file entry.
1369 * vap - attributes of new file.
1370 * excl - flag indicating exclusive or non-exclusive mode.
1371 * mode - mode to open file with.
1372 * cr - credentials of caller.
1373 * flag - large file flag [UNUSED].
1374 * ct - caller context
1375 * vsecp - ACL to be set
1377 * OUT: vpp - vnode of created or trunc'd entry.
1379 * RETURN: 0 on success, error code on failure.
1381 * Timestamps:
1382 * dvp - ctime|mtime updated if new entry created
1383 * vp - ctime|mtime always, atime if new
1386 /* ARGSUSED */
1387 static int
1388 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1389 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1390 vsecattr_t *vsecp)
1392 znode_t *zp, *dzp = VTOZ(dvp);
1393 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1394 zilog_t *zilog;
1395 objset_t *os;
1396 zfs_dirlock_t *dl;
1397 dmu_tx_t *tx;
1398 int error;
1399 ksid_t *ksid;
1400 uid_t uid;
1401 gid_t gid = crgetgid(cr);
1402 zfs_acl_ids_t acl_ids;
1403 boolean_t fuid_dirtied;
1404 boolean_t have_acl = B_FALSE;
1405 boolean_t waited = B_FALSE;
1408 * If we have an ephemeral id, ACL, or XVATTR then
1409 * make sure file system is at proper version
1412 ksid = crgetsid(cr, KSID_OWNER);
1413 if (ksid)
1414 uid = ksid_getid(ksid);
1415 else
1416 uid = crgetuid(cr);
1418 if (zfsvfs->z_use_fuids == B_FALSE &&
1419 (vsecp || (vap->va_mask & AT_XVATTR) ||
1420 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1421 return (SET_ERROR(EINVAL));
1423 ZFS_ENTER(zfsvfs);
1424 ZFS_VERIFY_ZP(dzp);
1425 os = zfsvfs->z_os;
1426 zilog = zfsvfs->z_log;
1428 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1429 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1430 ZFS_EXIT(zfsvfs);
1431 return (SET_ERROR(EILSEQ));
1434 if (vap->va_mask & AT_XVATTR) {
1435 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1436 crgetuid(cr), cr, vap->va_type)) != 0) {
1437 ZFS_EXIT(zfsvfs);
1438 return (error);
1441 top:
1442 *vpp = NULL;
1444 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1445 vap->va_mode &= ~VSVTX;
1447 if (*name == '\0') {
1449 * Null component name refers to the directory itself.
1451 VN_HOLD(dvp);
1452 zp = dzp;
1453 dl = NULL;
1454 error = 0;
1455 } else {
1456 /* possible VN_HOLD(zp) */
1457 int zflg = 0;
1459 if (flag & FIGNORECASE)
1460 zflg |= ZCILOOK;
1462 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1463 NULL, NULL);
1464 if (error) {
1465 if (have_acl)
1466 zfs_acl_ids_free(&acl_ids);
1467 if (strcmp(name, "..") == 0)
1468 error = SET_ERROR(EISDIR);
1469 ZFS_EXIT(zfsvfs);
1470 return (error);
1474 if (zp == NULL) {
1475 uint64_t txtype;
1478 * Create a new file object and update the directory
1479 * to reference it.
1481 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1482 if (have_acl)
1483 zfs_acl_ids_free(&acl_ids);
1484 goto out;
1488 * We only support the creation of regular files in
1489 * extended attribute directories.
1492 if ((dzp->z_pflags & ZFS_XATTR) &&
1493 (vap->va_type != VREG)) {
1494 if (have_acl)
1495 zfs_acl_ids_free(&acl_ids);
1496 error = SET_ERROR(EINVAL);
1497 goto out;
1500 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1501 cr, vsecp, &acl_ids)) != 0)
1502 goto out;
1503 have_acl = B_TRUE;
1505 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1506 zfs_acl_ids_free(&acl_ids);
1507 error = SET_ERROR(EDQUOT);
1508 goto out;
1511 tx = dmu_tx_create(os);
1513 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1514 ZFS_SA_BASE_ATTR_SIZE);
1516 fuid_dirtied = zfsvfs->z_fuid_dirty;
1517 if (fuid_dirtied)
1518 zfs_fuid_txhold(zfsvfs, tx);
1519 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1520 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1521 if (!zfsvfs->z_use_sa &&
1522 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1523 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1524 0, acl_ids.z_aclp->z_acl_bytes);
1526 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1527 if (error) {
1528 zfs_dirent_unlock(dl);
1529 if (error == ERESTART) {
1530 waited = B_TRUE;
1531 dmu_tx_wait(tx);
1532 dmu_tx_abort(tx);
1533 goto top;
1535 zfs_acl_ids_free(&acl_ids);
1536 dmu_tx_abort(tx);
1537 ZFS_EXIT(zfsvfs);
1538 return (error);
1540 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1542 if (fuid_dirtied)
1543 zfs_fuid_sync(zfsvfs, tx);
1545 (void) zfs_link_create(dl, zp, tx, ZNEW);
1546 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1547 if (flag & FIGNORECASE)
1548 txtype |= TX_CI;
1549 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1550 vsecp, acl_ids.z_fuidp, vap);
1551 zfs_acl_ids_free(&acl_ids);
1552 dmu_tx_commit(tx);
1553 } else {
1554 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1556 if (have_acl)
1557 zfs_acl_ids_free(&acl_ids);
1558 have_acl = B_FALSE;
1561 * A directory entry already exists for this name.
1564 * Can't truncate an existing file if in exclusive mode.
1566 if (excl == EXCL) {
1567 error = SET_ERROR(EEXIST);
1568 goto out;
1571 * Can't open a directory for writing.
1573 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1574 error = SET_ERROR(EISDIR);
1575 goto out;
1578 * Verify requested access to file.
1580 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1581 goto out;
1584 mutex_enter(&dzp->z_lock);
1585 dzp->z_seq++;
1586 mutex_exit(&dzp->z_lock);
1589 * Truncate regular files if requested.
1591 if ((ZTOV(zp)->v_type == VREG) &&
1592 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1593 /* we can't hold any locks when calling zfs_freesp() */
1594 zfs_dirent_unlock(dl);
1595 dl = NULL;
1596 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1597 if (error == 0) {
1598 vnevent_create(ZTOV(zp), ct);
1602 out:
1604 if (dl)
1605 zfs_dirent_unlock(dl);
1607 if (error) {
1608 if (zp)
1609 VN_RELE(ZTOV(zp));
1610 } else {
1611 *vpp = ZTOV(zp);
1612 error = specvp_check(vpp, cr);
1615 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1616 zil_commit(zilog, 0);
1618 ZFS_EXIT(zfsvfs);
1619 return (error);
1623 * Remove an entry from a directory.
1625 * IN: dvp - vnode of directory to remove entry from.
1626 * name - name of entry to remove.
1627 * cr - credentials of caller.
1628 * ct - caller context
1629 * flags - case flags
1631 * RETURN: 0 on success, error code on failure.
1633 * Timestamps:
1634 * dvp - ctime|mtime
1635 * vp - ctime (if nlink > 0)
1638 uint64_t null_xattr = 0;
1640 /*ARGSUSED*/
1641 static int
1642 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1643 int flags)
1645 znode_t *zp, *dzp = VTOZ(dvp);
1646 znode_t *xzp;
1647 vnode_t *vp;
1648 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1649 zilog_t *zilog;
1650 uint64_t acl_obj, xattr_obj;
1651 uint64_t xattr_obj_unlinked = 0;
1652 uint64_t obj = 0;
1653 zfs_dirlock_t *dl;
1654 dmu_tx_t *tx;
1655 boolean_t may_delete_now, delete_now = FALSE;
1656 boolean_t unlinked, toobig = FALSE;
1657 uint64_t txtype;
1658 pathname_t *realnmp = NULL;
1659 pathname_t realnm;
1660 int error;
1661 int zflg = ZEXISTS;
1662 boolean_t waited = B_FALSE;
1664 ZFS_ENTER(zfsvfs);
1665 ZFS_VERIFY_ZP(dzp);
1666 zilog = zfsvfs->z_log;
1668 if (flags & FIGNORECASE) {
1669 zflg |= ZCILOOK;
1670 pn_alloc(&realnm);
1671 realnmp = &realnm;
1674 top:
1675 xattr_obj = 0;
1676 xzp = NULL;
1678 * Attempt to lock directory; fail if entry doesn't exist.
1680 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1681 NULL, realnmp)) {
1682 if (realnmp)
1683 pn_free(realnmp);
1684 ZFS_EXIT(zfsvfs);
1685 return (error);
1688 vp = ZTOV(zp);
1690 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1691 goto out;
1695 * Need to use rmdir for removing directories.
1697 if (vp->v_type == VDIR) {
1698 error = SET_ERROR(EPERM);
1699 goto out;
1702 vnevent_remove(vp, dvp, name, ct);
1704 if (realnmp)
1705 dnlc_remove(dvp, realnmp->pn_buf);
1706 else
1707 dnlc_remove(dvp, name);
1709 mutex_enter(&vp->v_lock);
1710 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1711 mutex_exit(&vp->v_lock);
1714 * We may delete the znode now, or we may put it in the unlinked set;
1715 * it depends on whether we're the last link, and on whether there are
1716 * other holds on the vnode. So we dmu_tx_hold() the right things to
1717 * allow for either case.
1719 obj = zp->z_id;
1720 tx = dmu_tx_create(zfsvfs->z_os);
1721 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1722 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1723 zfs_sa_upgrade_txholds(tx, zp);
1724 zfs_sa_upgrade_txholds(tx, dzp);
1725 if (may_delete_now) {
1726 toobig =
1727 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1728 /* if the file is too big, only hold_free a token amount */
1729 dmu_tx_hold_free(tx, zp->z_id, 0,
1730 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1733 /* are there any extended attributes? */
1734 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1735 &xattr_obj, sizeof (xattr_obj));
1736 if (error == 0 && xattr_obj) {
1737 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1738 ASSERT0(error);
1739 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1740 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1743 mutex_enter(&zp->z_lock);
1744 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1745 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1746 mutex_exit(&zp->z_lock);
1748 /* charge as an update -- would be nice not to charge at all */
1749 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1752 * Mark this transaction as typically resulting in a net free of space
1754 dmu_tx_mark_netfree(tx);
1756 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1757 if (error) {
1758 zfs_dirent_unlock(dl);
1759 VN_RELE(vp);
1760 if (xzp)
1761 VN_RELE(ZTOV(xzp));
1762 if (error == ERESTART) {
1763 waited = B_TRUE;
1764 dmu_tx_wait(tx);
1765 dmu_tx_abort(tx);
1766 goto top;
1768 if (realnmp)
1769 pn_free(realnmp);
1770 dmu_tx_abort(tx);
1771 ZFS_EXIT(zfsvfs);
1772 return (error);
1776 * Remove the directory entry.
1778 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1780 if (error) {
1781 dmu_tx_commit(tx);
1782 goto out;
1785 if (unlinked) {
1787 * Hold z_lock so that we can make sure that the ACL obj
1788 * hasn't changed. Could have been deleted due to
1789 * zfs_sa_upgrade().
1791 mutex_enter(&zp->z_lock);
1792 mutex_enter(&vp->v_lock);
1793 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1794 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1795 delete_now = may_delete_now && !toobig &&
1796 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1797 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1798 acl_obj;
1799 mutex_exit(&vp->v_lock);
1802 if (delete_now) {
1803 if (xattr_obj_unlinked) {
1804 ASSERT3U(xzp->z_links, ==, 2);
1805 mutex_enter(&xzp->z_lock);
1806 xzp->z_unlinked = 1;
1807 xzp->z_links = 0;
1808 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1809 &xzp->z_links, sizeof (xzp->z_links), tx);
1810 ASSERT3U(error, ==, 0);
1811 mutex_exit(&xzp->z_lock);
1812 zfs_unlinked_add(xzp, tx);
1814 if (zp->z_is_sa)
1815 error = sa_remove(zp->z_sa_hdl,
1816 SA_ZPL_XATTR(zfsvfs), tx);
1817 else
1818 error = sa_update(zp->z_sa_hdl,
1819 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1820 sizeof (uint64_t), tx);
1821 ASSERT0(error);
1823 mutex_enter(&vp->v_lock);
1824 VN_RELE_LOCKED(vp);
1825 ASSERT0(vp->v_count);
1826 mutex_exit(&vp->v_lock);
1827 mutex_exit(&zp->z_lock);
1828 zfs_znode_delete(zp, tx);
1829 } else if (unlinked) {
1830 mutex_exit(&zp->z_lock);
1831 zfs_unlinked_add(zp, tx);
1834 txtype = TX_REMOVE;
1835 if (flags & FIGNORECASE)
1836 txtype |= TX_CI;
1837 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1839 dmu_tx_commit(tx);
1840 out:
1841 if (realnmp)
1842 pn_free(realnmp);
1844 zfs_dirent_unlock(dl);
1846 if (!delete_now)
1847 VN_RELE(vp);
1848 if (xzp)
1849 VN_RELE(ZTOV(xzp));
1851 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1852 zil_commit(zilog, 0);
1854 ZFS_EXIT(zfsvfs);
1855 return (error);
1859 * Create a new directory and insert it into dvp using the name
1860 * provided. Return a pointer to the inserted directory.
1862 * IN: dvp - vnode of directory to add subdir to.
1863 * dirname - name of new directory.
1864 * vap - attributes of new directory.
1865 * cr - credentials of caller.
1866 * ct - caller context
1867 * flags - case flags
1868 * vsecp - ACL to be set
1870 * OUT: vpp - vnode of created directory.
1872 * RETURN: 0 on success, error code on failure.
1874 * Timestamps:
1875 * dvp - ctime|mtime updated
1876 * vp - ctime|mtime|atime updated
1878 /*ARGSUSED*/
1879 static int
1880 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1881 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1883 znode_t *zp, *dzp = VTOZ(dvp);
1884 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1885 zilog_t *zilog;
1886 zfs_dirlock_t *dl;
1887 uint64_t txtype;
1888 dmu_tx_t *tx;
1889 int error;
1890 int zf = ZNEW;
1891 ksid_t *ksid;
1892 uid_t uid;
1893 gid_t gid = crgetgid(cr);
1894 zfs_acl_ids_t acl_ids;
1895 boolean_t fuid_dirtied;
1896 boolean_t waited = B_FALSE;
1898 ASSERT(vap->va_type == VDIR);
1901 * If we have an ephemeral id, ACL, or XVATTR then
1902 * make sure file system is at proper version
1905 ksid = crgetsid(cr, KSID_OWNER);
1906 if (ksid)
1907 uid = ksid_getid(ksid);
1908 else
1909 uid = crgetuid(cr);
1910 if (zfsvfs->z_use_fuids == B_FALSE &&
1911 (vsecp || (vap->va_mask & AT_XVATTR) ||
1912 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1913 return (SET_ERROR(EINVAL));
1915 ZFS_ENTER(zfsvfs);
1916 ZFS_VERIFY_ZP(dzp);
1917 zilog = zfsvfs->z_log;
1919 if (dzp->z_pflags & ZFS_XATTR) {
1920 ZFS_EXIT(zfsvfs);
1921 return (SET_ERROR(EINVAL));
1924 if (zfsvfs->z_utf8 && u8_validate(dirname,
1925 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1926 ZFS_EXIT(zfsvfs);
1927 return (SET_ERROR(EILSEQ));
1929 if (flags & FIGNORECASE)
1930 zf |= ZCILOOK;
1932 if (vap->va_mask & AT_XVATTR) {
1933 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1934 crgetuid(cr), cr, vap->va_type)) != 0) {
1935 ZFS_EXIT(zfsvfs);
1936 return (error);
1940 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1941 vsecp, &acl_ids)) != 0) {
1942 ZFS_EXIT(zfsvfs);
1943 return (error);
1946 * First make sure the new directory doesn't exist.
1948 * Existence is checked first to make sure we don't return
1949 * EACCES instead of EEXIST which can cause some applications
1950 * to fail.
1952 top:
1953 *vpp = NULL;
1955 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1956 NULL, NULL)) {
1957 zfs_acl_ids_free(&acl_ids);
1958 ZFS_EXIT(zfsvfs);
1959 return (error);
1962 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1963 zfs_acl_ids_free(&acl_ids);
1964 zfs_dirent_unlock(dl);
1965 ZFS_EXIT(zfsvfs);
1966 return (error);
1969 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1970 zfs_acl_ids_free(&acl_ids);
1971 zfs_dirent_unlock(dl);
1972 ZFS_EXIT(zfsvfs);
1973 return (SET_ERROR(EDQUOT));
1977 * Add a new entry to the directory.
1979 tx = dmu_tx_create(zfsvfs->z_os);
1980 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1981 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1982 fuid_dirtied = zfsvfs->z_fuid_dirty;
1983 if (fuid_dirtied)
1984 zfs_fuid_txhold(zfsvfs, tx);
1985 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1986 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1987 acl_ids.z_aclp->z_acl_bytes);
1990 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1991 ZFS_SA_BASE_ATTR_SIZE);
1993 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1994 if (error) {
1995 zfs_dirent_unlock(dl);
1996 if (error == ERESTART) {
1997 waited = B_TRUE;
1998 dmu_tx_wait(tx);
1999 dmu_tx_abort(tx);
2000 goto top;
2002 zfs_acl_ids_free(&acl_ids);
2003 dmu_tx_abort(tx);
2004 ZFS_EXIT(zfsvfs);
2005 return (error);
2009 * Create new node.
2011 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2013 if (fuid_dirtied)
2014 zfs_fuid_sync(zfsvfs, tx);
2017 * Now put new name in parent dir.
2019 (void) zfs_link_create(dl, zp, tx, ZNEW);
2021 *vpp = ZTOV(zp);
2023 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2024 if (flags & FIGNORECASE)
2025 txtype |= TX_CI;
2026 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2027 acl_ids.z_fuidp, vap);
2029 zfs_acl_ids_free(&acl_ids);
2031 dmu_tx_commit(tx);
2033 zfs_dirent_unlock(dl);
2035 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2036 zil_commit(zilog, 0);
2038 ZFS_EXIT(zfsvfs);
2039 return (0);
2043 * Remove a directory subdir entry. If the current working
2044 * directory is the same as the subdir to be removed, the
2045 * remove will fail.
2047 * IN: dvp - vnode of directory to remove from.
2048 * name - name of directory to be removed.
2049 * cwd - vnode of current working directory.
2050 * cr - credentials of caller.
2051 * ct - caller context
2052 * flags - case flags
2054 * RETURN: 0 on success, error code on failure.
2056 * Timestamps:
2057 * dvp - ctime|mtime updated
2059 /*ARGSUSED*/
2060 static int
2061 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2062 caller_context_t *ct, int flags)
2064 znode_t *dzp = VTOZ(dvp);
2065 znode_t *zp;
2066 vnode_t *vp;
2067 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2068 zilog_t *zilog;
2069 zfs_dirlock_t *dl;
2070 dmu_tx_t *tx;
2071 int error;
2072 int zflg = ZEXISTS;
2073 boolean_t waited = B_FALSE;
2075 ZFS_ENTER(zfsvfs);
2076 ZFS_VERIFY_ZP(dzp);
2077 zilog = zfsvfs->z_log;
2079 if (flags & FIGNORECASE)
2080 zflg |= ZCILOOK;
2081 top:
2082 zp = NULL;
2085 * Attempt to lock directory; fail if entry doesn't exist.
2087 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2088 NULL, NULL)) {
2089 ZFS_EXIT(zfsvfs);
2090 return (error);
2093 vp = ZTOV(zp);
2095 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2096 goto out;
2099 if (vp->v_type != VDIR) {
2100 error = SET_ERROR(ENOTDIR);
2101 goto out;
2104 if (vp == cwd) {
2105 error = SET_ERROR(EINVAL);
2106 goto out;
2109 vnevent_rmdir(vp, dvp, name, ct);
2112 * Grab a lock on the directory to make sure that noone is
2113 * trying to add (or lookup) entries while we are removing it.
2115 rw_enter(&zp->z_name_lock, RW_WRITER);
2118 * Grab a lock on the parent pointer to make sure we play well
2119 * with the treewalk and directory rename code.
2121 rw_enter(&zp->z_parent_lock, RW_WRITER);
2123 tx = dmu_tx_create(zfsvfs->z_os);
2124 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2125 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2126 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2127 zfs_sa_upgrade_txholds(tx, zp);
2128 zfs_sa_upgrade_txholds(tx, dzp);
2129 dmu_tx_mark_netfree(tx);
2130 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2131 if (error) {
2132 rw_exit(&zp->z_parent_lock);
2133 rw_exit(&zp->z_name_lock);
2134 zfs_dirent_unlock(dl);
2135 VN_RELE(vp);
2136 if (error == ERESTART) {
2137 waited = B_TRUE;
2138 dmu_tx_wait(tx);
2139 dmu_tx_abort(tx);
2140 goto top;
2142 dmu_tx_abort(tx);
2143 ZFS_EXIT(zfsvfs);
2144 return (error);
2147 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2149 if (error == 0) {
2150 uint64_t txtype = TX_RMDIR;
2151 if (flags & FIGNORECASE)
2152 txtype |= TX_CI;
2153 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2156 dmu_tx_commit(tx);
2158 rw_exit(&zp->z_parent_lock);
2159 rw_exit(&zp->z_name_lock);
2160 out:
2161 zfs_dirent_unlock(dl);
2163 VN_RELE(vp);
2165 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2166 zil_commit(zilog, 0);
2168 ZFS_EXIT(zfsvfs);
2169 return (error);
2173 * Read as many directory entries as will fit into the provided
2174 * buffer from the given directory cursor position (specified in
2175 * the uio structure).
2177 * IN: vp - vnode of directory to read.
2178 * uio - structure supplying read location, range info,
2179 * and return buffer.
2180 * cr - credentials of caller.
2181 * ct - caller context
2182 * flags - case flags
2184 * OUT: uio - updated offset and range, buffer filled.
2185 * eofp - set to true if end-of-file detected.
2187 * RETURN: 0 on success, error code on failure.
2189 * Timestamps:
2190 * vp - atime updated
2192 * Note that the low 4 bits of the cookie returned by zap is always zero.
2193 * This allows us to use the low range for "special" directory entries:
2194 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2195 * we use the offset 2 for the '.zfs' directory.
2197 /* ARGSUSED */
2198 static int
2199 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2200 caller_context_t *ct, int flags)
2202 znode_t *zp = VTOZ(vp);
2203 iovec_t *iovp;
2204 edirent_t *eodp;
2205 dirent64_t *odp;
2206 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2207 objset_t *os;
2208 caddr_t outbuf;
2209 size_t bufsize;
2210 zap_cursor_t zc;
2211 zap_attribute_t zap;
2212 uint_t bytes_wanted;
2213 uint64_t offset; /* must be unsigned; checks for < 1 */
2214 uint64_t parent;
2215 int local_eof;
2216 int outcount;
2217 int error;
2218 uint8_t prefetch;
2219 boolean_t check_sysattrs;
2221 ZFS_ENTER(zfsvfs);
2222 ZFS_VERIFY_ZP(zp);
2224 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2225 &parent, sizeof (parent))) != 0) {
2226 ZFS_EXIT(zfsvfs);
2227 return (error);
2231 * If we are not given an eof variable,
2232 * use a local one.
2234 if (eofp == NULL)
2235 eofp = &local_eof;
2238 * Check for valid iov_len.
2240 if (uio->uio_iov->iov_len <= 0) {
2241 ZFS_EXIT(zfsvfs);
2242 return (SET_ERROR(EINVAL));
2246 * Quit if directory has been removed (posix)
2248 if ((*eofp = zp->z_unlinked) != 0) {
2249 ZFS_EXIT(zfsvfs);
2250 return (0);
2253 error = 0;
2254 os = zfsvfs->z_os;
2255 offset = uio->uio_loffset;
2256 prefetch = zp->z_zn_prefetch;
2259 * Initialize the iterator cursor.
2261 if (offset <= 3) {
2263 * Start iteration from the beginning of the directory.
2265 zap_cursor_init(&zc, os, zp->z_id);
2266 } else {
2268 * The offset is a serialized cursor.
2270 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2274 * Get space to change directory entries into fs independent format.
2276 iovp = uio->uio_iov;
2277 bytes_wanted = iovp->iov_len;
2278 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2279 bufsize = bytes_wanted;
2280 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2281 odp = (struct dirent64 *)outbuf;
2282 } else {
2283 bufsize = bytes_wanted;
2284 outbuf = NULL;
2285 odp = (struct dirent64 *)iovp->iov_base;
2287 eodp = (struct edirent *)odp;
2290 * If this VFS supports the system attribute view interface; and
2291 * we're looking at an extended attribute directory; and we care
2292 * about normalization conflicts on this vfs; then we must check
2293 * for normalization conflicts with the sysattr name space.
2295 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2296 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2297 (flags & V_RDDIR_ENTFLAGS);
2300 * Transform to file-system independent format
2302 outcount = 0;
2303 while (outcount < bytes_wanted) {
2304 ino64_t objnum;
2305 ushort_t reclen;
2306 off64_t *next = NULL;
2309 * Special case `.', `..', and `.zfs'.
2311 if (offset == 0) {
2312 (void) strcpy(zap.za_name, ".");
2313 zap.za_normalization_conflict = 0;
2314 objnum = zp->z_id;
2315 } else if (offset == 1) {
2316 (void) strcpy(zap.za_name, "..");
2317 zap.za_normalization_conflict = 0;
2318 objnum = parent;
2319 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2320 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2321 zap.za_normalization_conflict = 0;
2322 objnum = ZFSCTL_INO_ROOT;
2323 } else {
2325 * Grab next entry.
2327 if (error = zap_cursor_retrieve(&zc, &zap)) {
2328 if ((*eofp = (error == ENOENT)) != 0)
2329 break;
2330 else
2331 goto update;
2334 if (zap.za_integer_length != 8 ||
2335 zap.za_num_integers != 1) {
2336 cmn_err(CE_WARN, "zap_readdir: bad directory "
2337 "entry, obj = %lld, offset = %lld\n",
2338 (u_longlong_t)zp->z_id,
2339 (u_longlong_t)offset);
2340 error = SET_ERROR(ENXIO);
2341 goto update;
2344 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2346 * MacOS X can extract the object type here such as:
2347 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2350 if (check_sysattrs && !zap.za_normalization_conflict) {
2351 zap.za_normalization_conflict =
2352 xattr_sysattr_casechk(zap.za_name);
2356 if (flags & V_RDDIR_ACCFILTER) {
2358 * If we have no access at all, don't include
2359 * this entry in the returned information
2361 znode_t *ezp;
2362 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2363 goto skip_entry;
2364 if (!zfs_has_access(ezp, cr)) {
2365 VN_RELE(ZTOV(ezp));
2366 goto skip_entry;
2368 VN_RELE(ZTOV(ezp));
2371 if (flags & V_RDDIR_ENTFLAGS)
2372 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2373 else
2374 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2377 * Will this entry fit in the buffer?
2379 if (outcount + reclen > bufsize) {
2381 * Did we manage to fit anything in the buffer?
2383 if (!outcount) {
2384 error = SET_ERROR(EINVAL);
2385 goto update;
2387 break;
2389 if (flags & V_RDDIR_ENTFLAGS) {
2391 * Add extended flag entry:
2393 eodp->ed_ino = objnum;
2394 eodp->ed_reclen = reclen;
2395 /* NOTE: ed_off is the offset for the *next* entry */
2396 next = &(eodp->ed_off);
2397 eodp->ed_eflags = zap.za_normalization_conflict ?
2398 ED_CASE_CONFLICT : 0;
2399 (void) strncpy(eodp->ed_name, zap.za_name,
2400 EDIRENT_NAMELEN(reclen));
2401 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2402 } else {
2404 * Add normal entry:
2406 odp->d_ino = objnum;
2407 odp->d_reclen = reclen;
2408 /* NOTE: d_off is the offset for the *next* entry */
2409 next = &(odp->d_off);
2410 (void) strncpy(odp->d_name, zap.za_name,
2411 DIRENT64_NAMELEN(reclen));
2412 odp = (dirent64_t *)((intptr_t)odp + reclen);
2414 outcount += reclen;
2416 ASSERT(outcount <= bufsize);
2418 /* Prefetch znode */
2419 if (prefetch)
2420 dmu_prefetch(os, objnum, 0, 0, 0,
2421 ZIO_PRIORITY_SYNC_READ);
2423 skip_entry:
2425 * Move to the next entry, fill in the previous offset.
2427 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2428 zap_cursor_advance(&zc);
2429 offset = zap_cursor_serialize(&zc);
2430 } else {
2431 offset += 1;
2433 if (next)
2434 *next = offset;
2436 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2438 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2439 iovp->iov_base += outcount;
2440 iovp->iov_len -= outcount;
2441 uio->uio_resid -= outcount;
2442 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2444 * Reset the pointer.
2446 offset = uio->uio_loffset;
2449 update:
2450 zap_cursor_fini(&zc);
2451 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2452 kmem_free(outbuf, bufsize);
2454 if (error == ENOENT)
2455 error = 0;
2457 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2459 uio->uio_loffset = offset;
2460 ZFS_EXIT(zfsvfs);
2461 return (error);
2464 ulong_t zfs_fsync_sync_cnt = 4;
2466 static int
2467 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2469 znode_t *zp = VTOZ(vp);
2470 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2473 * Regardless of whether this is required for standards conformance,
2474 * this is the logical behavior when fsync() is called on a file with
2475 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2476 * going to be pushed out as part of the zil_commit().
2478 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2479 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2480 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2482 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2484 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2485 ZFS_ENTER(zfsvfs);
2486 ZFS_VERIFY_ZP(zp);
2487 zil_commit(zfsvfs->z_log, zp->z_id);
2488 ZFS_EXIT(zfsvfs);
2490 return (0);
2495 * Get the requested file attributes and place them in the provided
2496 * vattr structure.
2498 * IN: vp - vnode of file.
2499 * vap - va_mask identifies requested attributes.
2500 * If AT_XVATTR set, then optional attrs are requested
2501 * flags - ATTR_NOACLCHECK (CIFS server context)
2502 * cr - credentials of caller.
2503 * ct - caller context
2505 * OUT: vap - attribute values.
2507 * RETURN: 0 (always succeeds).
2509 /* ARGSUSED */
2510 static int
2511 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2512 caller_context_t *ct)
2514 znode_t *zp = VTOZ(vp);
2515 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2516 int error = 0;
2517 uint64_t links;
2518 uint64_t mtime[2], ctime[2];
2519 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2520 xoptattr_t *xoap = NULL;
2521 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2522 sa_bulk_attr_t bulk[2];
2523 int count = 0;
2525 ZFS_ENTER(zfsvfs);
2526 ZFS_VERIFY_ZP(zp);
2528 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2530 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2531 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2533 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2534 ZFS_EXIT(zfsvfs);
2535 return (error);
2539 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2540 * Also, if we are the owner don't bother, since owner should
2541 * always be allowed to read basic attributes of file.
2543 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2544 (vap->va_uid != crgetuid(cr))) {
2545 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2546 skipaclchk, cr)) {
2547 ZFS_EXIT(zfsvfs);
2548 return (error);
2553 * Return all attributes. It's cheaper to provide the answer
2554 * than to determine whether we were asked the question.
2557 mutex_enter(&zp->z_lock);
2558 vap->va_type = vp->v_type;
2559 vap->va_mode = zp->z_mode & MODEMASK;
2560 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2561 vap->va_nodeid = zp->z_id;
2562 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2563 links = zp->z_links + 1;
2564 else
2565 links = zp->z_links;
2566 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2567 vap->va_size = zp->z_size;
2568 vap->va_rdev = vp->v_rdev;
2569 vap->va_seq = zp->z_seq;
2572 * Add in any requested optional attributes and the create time.
2573 * Also set the corresponding bits in the returned attribute bitmap.
2575 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2576 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2577 xoap->xoa_archive =
2578 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2579 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2582 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2583 xoap->xoa_readonly =
2584 ((zp->z_pflags & ZFS_READONLY) != 0);
2585 XVA_SET_RTN(xvap, XAT_READONLY);
2588 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2589 xoap->xoa_system =
2590 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2591 XVA_SET_RTN(xvap, XAT_SYSTEM);
2594 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2595 xoap->xoa_hidden =
2596 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2597 XVA_SET_RTN(xvap, XAT_HIDDEN);
2600 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2601 xoap->xoa_nounlink =
2602 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2603 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2606 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2607 xoap->xoa_immutable =
2608 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2609 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2612 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2613 xoap->xoa_appendonly =
2614 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2615 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2618 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2619 xoap->xoa_nodump =
2620 ((zp->z_pflags & ZFS_NODUMP) != 0);
2621 XVA_SET_RTN(xvap, XAT_NODUMP);
2624 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2625 xoap->xoa_opaque =
2626 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2627 XVA_SET_RTN(xvap, XAT_OPAQUE);
2630 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2631 xoap->xoa_av_quarantined =
2632 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2633 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2636 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2637 xoap->xoa_av_modified =
2638 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2639 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2642 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2643 vp->v_type == VREG) {
2644 zfs_sa_get_scanstamp(zp, xvap);
2647 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2648 uint64_t times[2];
2650 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2651 times, sizeof (times));
2652 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2653 XVA_SET_RTN(xvap, XAT_CREATETIME);
2656 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2657 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2658 XVA_SET_RTN(xvap, XAT_REPARSE);
2660 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2661 xoap->xoa_generation = zp->z_gen;
2662 XVA_SET_RTN(xvap, XAT_GEN);
2665 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2666 xoap->xoa_offline =
2667 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2668 XVA_SET_RTN(xvap, XAT_OFFLINE);
2671 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2672 xoap->xoa_sparse =
2673 ((zp->z_pflags & ZFS_SPARSE) != 0);
2674 XVA_SET_RTN(xvap, XAT_SPARSE);
2678 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2679 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2680 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2682 mutex_exit(&zp->z_lock);
2684 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2686 if (zp->z_blksz == 0) {
2688 * Block size hasn't been set; suggest maximal I/O transfers.
2690 vap->va_blksize = zfsvfs->z_max_blksz;
2693 ZFS_EXIT(zfsvfs);
2694 return (0);
2698 * Set the file attributes to the values contained in the
2699 * vattr structure.
2701 * IN: vp - vnode of file to be modified.
2702 * vap - new attribute values.
2703 * If AT_XVATTR set, then optional attrs are being set
2704 * flags - ATTR_UTIME set if non-default time values provided.
2705 * - ATTR_NOACLCHECK (CIFS context only).
2706 * cr - credentials of caller.
2707 * ct - caller context
2709 * RETURN: 0 on success, error code on failure.
2711 * Timestamps:
2712 * vp - ctime updated, mtime updated if size changed.
2714 /* ARGSUSED */
2715 static int
2716 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2717 caller_context_t *ct)
2719 znode_t *zp = VTOZ(vp);
2720 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2721 zilog_t *zilog;
2722 dmu_tx_t *tx;
2723 vattr_t oldva;
2724 xvattr_t tmpxvattr;
2725 uint_t mask = vap->va_mask;
2726 uint_t saved_mask = 0;
2727 int trim_mask = 0;
2728 uint64_t new_mode;
2729 uint64_t new_uid, new_gid;
2730 uint64_t xattr_obj;
2731 uint64_t mtime[2], ctime[2];
2732 znode_t *attrzp;
2733 int need_policy = FALSE;
2734 int err, err2;
2735 zfs_fuid_info_t *fuidp = NULL;
2736 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2737 xoptattr_t *xoap;
2738 zfs_acl_t *aclp;
2739 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2740 boolean_t fuid_dirtied = B_FALSE;
2741 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2742 int count = 0, xattr_count = 0;
2744 if (mask == 0)
2745 return (0);
2747 if (mask & AT_NOSET)
2748 return (SET_ERROR(EINVAL));
2750 ZFS_ENTER(zfsvfs);
2751 ZFS_VERIFY_ZP(zp);
2753 zilog = zfsvfs->z_log;
2756 * Make sure that if we have ephemeral uid/gid or xvattr specified
2757 * that file system is at proper version level
2760 if (zfsvfs->z_use_fuids == B_FALSE &&
2761 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2762 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2763 (mask & AT_XVATTR))) {
2764 ZFS_EXIT(zfsvfs);
2765 return (SET_ERROR(EINVAL));
2768 if (mask & AT_SIZE && vp->v_type == VDIR) {
2769 ZFS_EXIT(zfsvfs);
2770 return (SET_ERROR(EISDIR));
2773 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2774 ZFS_EXIT(zfsvfs);
2775 return (SET_ERROR(EINVAL));
2779 * If this is an xvattr_t, then get a pointer to the structure of
2780 * optional attributes. If this is NULL, then we have a vattr_t.
2782 xoap = xva_getxoptattr(xvap);
2784 xva_init(&tmpxvattr);
2787 * Immutable files can only alter immutable bit and atime
2789 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2790 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2791 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2792 ZFS_EXIT(zfsvfs);
2793 return (SET_ERROR(EPERM));
2797 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2801 * Verify timestamps doesn't overflow 32 bits.
2802 * ZFS can handle large timestamps, but 32bit syscalls can't
2803 * handle times greater than 2039. This check should be removed
2804 * once large timestamps are fully supported.
2806 if (mask & (AT_ATIME | AT_MTIME)) {
2807 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2808 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2809 ZFS_EXIT(zfsvfs);
2810 return (SET_ERROR(EOVERFLOW));
2814 top:
2815 attrzp = NULL;
2816 aclp = NULL;
2818 /* Can this be moved to before the top label? */
2819 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2820 ZFS_EXIT(zfsvfs);
2821 return (SET_ERROR(EROFS));
2825 * First validate permissions
2828 if (mask & AT_SIZE) {
2829 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2830 if (err) {
2831 ZFS_EXIT(zfsvfs);
2832 return (err);
2835 * XXX - Note, we are not providing any open
2836 * mode flags here (like FNDELAY), so we may
2837 * block if there are locks present... this
2838 * should be addressed in openat().
2840 /* XXX - would it be OK to generate a log record here? */
2841 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2842 if (err) {
2843 ZFS_EXIT(zfsvfs);
2844 return (err);
2847 if (vap->va_size == 0)
2848 vnevent_truncate(ZTOV(zp), ct);
2851 if (mask & (AT_ATIME|AT_MTIME) ||
2852 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2853 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2854 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2855 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2856 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2857 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2858 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2859 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2860 skipaclchk, cr);
2863 if (mask & (AT_UID|AT_GID)) {
2864 int idmask = (mask & (AT_UID|AT_GID));
2865 int take_owner;
2866 int take_group;
2869 * NOTE: even if a new mode is being set,
2870 * we may clear S_ISUID/S_ISGID bits.
2873 if (!(mask & AT_MODE))
2874 vap->va_mode = zp->z_mode;
2877 * Take ownership or chgrp to group we are a member of
2880 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2881 take_group = (mask & AT_GID) &&
2882 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2885 * If both AT_UID and AT_GID are set then take_owner and
2886 * take_group must both be set in order to allow taking
2887 * ownership.
2889 * Otherwise, send the check through secpolicy_vnode_setattr()
2893 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2894 ((idmask == AT_UID) && take_owner) ||
2895 ((idmask == AT_GID) && take_group)) {
2896 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2897 skipaclchk, cr) == 0) {
2899 * Remove setuid/setgid for non-privileged users
2901 secpolicy_setid_clear(vap, cr);
2902 trim_mask = (mask & (AT_UID|AT_GID));
2903 } else {
2904 need_policy = TRUE;
2906 } else {
2907 need_policy = TRUE;
2911 mutex_enter(&zp->z_lock);
2912 oldva.va_mode = zp->z_mode;
2913 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2914 if (mask & AT_XVATTR) {
2916 * Update xvattr mask to include only those attributes
2917 * that are actually changing.
2919 * the bits will be restored prior to actually setting
2920 * the attributes so the caller thinks they were set.
2922 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2923 if (xoap->xoa_appendonly !=
2924 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2925 need_policy = TRUE;
2926 } else {
2927 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2928 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2932 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2933 if (xoap->xoa_nounlink !=
2934 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2935 need_policy = TRUE;
2936 } else {
2937 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2938 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2942 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2943 if (xoap->xoa_immutable !=
2944 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2945 need_policy = TRUE;
2946 } else {
2947 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2948 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2952 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2953 if (xoap->xoa_nodump !=
2954 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2955 need_policy = TRUE;
2956 } else {
2957 XVA_CLR_REQ(xvap, XAT_NODUMP);
2958 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2962 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2963 if (xoap->xoa_av_modified !=
2964 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2965 need_policy = TRUE;
2966 } else {
2967 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2968 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2972 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2973 if ((vp->v_type != VREG &&
2974 xoap->xoa_av_quarantined) ||
2975 xoap->xoa_av_quarantined !=
2976 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2977 need_policy = TRUE;
2978 } else {
2979 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2980 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2984 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2985 mutex_exit(&zp->z_lock);
2986 ZFS_EXIT(zfsvfs);
2987 return (SET_ERROR(EPERM));
2990 if (need_policy == FALSE &&
2991 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2992 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2993 need_policy = TRUE;
2997 mutex_exit(&zp->z_lock);
2999 if (mask & AT_MODE) {
3000 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3001 err = secpolicy_setid_setsticky_clear(vp, vap,
3002 &oldva, cr);
3003 if (err) {
3004 ZFS_EXIT(zfsvfs);
3005 return (err);
3007 trim_mask |= AT_MODE;
3008 } else {
3009 need_policy = TRUE;
3013 if (need_policy) {
3015 * If trim_mask is set then take ownership
3016 * has been granted or write_acl is present and user
3017 * has the ability to modify mode. In that case remove
3018 * UID|GID and or MODE from mask so that
3019 * secpolicy_vnode_setattr() doesn't revoke it.
3022 if (trim_mask) {
3023 saved_mask = vap->va_mask;
3024 vap->va_mask &= ~trim_mask;
3026 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3027 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3028 if (err) {
3029 ZFS_EXIT(zfsvfs);
3030 return (err);
3033 if (trim_mask)
3034 vap->va_mask |= saved_mask;
3038 * secpolicy_vnode_setattr, or take ownership may have
3039 * changed va_mask
3041 mask = vap->va_mask;
3043 if ((mask & (AT_UID | AT_GID))) {
3044 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3045 &xattr_obj, sizeof (xattr_obj));
3047 if (err == 0 && xattr_obj) {
3048 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3049 if (err)
3050 goto out2;
3052 if (mask & AT_UID) {
3053 new_uid = zfs_fuid_create(zfsvfs,
3054 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3055 if (new_uid != zp->z_uid &&
3056 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3057 if (attrzp)
3058 VN_RELE(ZTOV(attrzp));
3059 err = SET_ERROR(EDQUOT);
3060 goto out2;
3064 if (mask & AT_GID) {
3065 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3066 cr, ZFS_GROUP, &fuidp);
3067 if (new_gid != zp->z_gid &&
3068 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3069 if (attrzp)
3070 VN_RELE(ZTOV(attrzp));
3071 err = SET_ERROR(EDQUOT);
3072 goto out2;
3076 tx = dmu_tx_create(zfsvfs->z_os);
3078 if (mask & AT_MODE) {
3079 uint64_t pmode = zp->z_mode;
3080 uint64_t acl_obj;
3081 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3083 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3084 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3085 err = SET_ERROR(EPERM);
3086 goto out;
3089 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3090 goto out;
3092 mutex_enter(&zp->z_lock);
3093 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3095 * Are we upgrading ACL from old V0 format
3096 * to V1 format?
3098 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3099 zfs_znode_acl_version(zp) ==
3100 ZFS_ACL_VERSION_INITIAL) {
3101 dmu_tx_hold_free(tx, acl_obj, 0,
3102 DMU_OBJECT_END);
3103 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3104 0, aclp->z_acl_bytes);
3105 } else {
3106 dmu_tx_hold_write(tx, acl_obj, 0,
3107 aclp->z_acl_bytes);
3109 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3110 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3111 0, aclp->z_acl_bytes);
3113 mutex_exit(&zp->z_lock);
3114 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3115 } else {
3116 if ((mask & AT_XVATTR) &&
3117 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3118 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3119 else
3120 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3123 if (attrzp) {
3124 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3127 fuid_dirtied = zfsvfs->z_fuid_dirty;
3128 if (fuid_dirtied)
3129 zfs_fuid_txhold(zfsvfs, tx);
3131 zfs_sa_upgrade_txholds(tx, zp);
3133 err = dmu_tx_assign(tx, TXG_WAIT);
3134 if (err)
3135 goto out;
3137 count = 0;
3139 * Set each attribute requested.
3140 * We group settings according to the locks they need to acquire.
3142 * Note: you cannot set ctime directly, although it will be
3143 * updated as a side-effect of calling this function.
3147 if (mask & (AT_UID|AT_GID|AT_MODE))
3148 mutex_enter(&zp->z_acl_lock);
3149 mutex_enter(&zp->z_lock);
3151 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3152 &zp->z_pflags, sizeof (zp->z_pflags));
3154 if (attrzp) {
3155 if (mask & (AT_UID|AT_GID|AT_MODE))
3156 mutex_enter(&attrzp->z_acl_lock);
3157 mutex_enter(&attrzp->z_lock);
3158 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3159 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3160 sizeof (attrzp->z_pflags));
3163 if (mask & (AT_UID|AT_GID)) {
3165 if (mask & AT_UID) {
3166 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3167 &new_uid, sizeof (new_uid));
3168 zp->z_uid = new_uid;
3169 if (attrzp) {
3170 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3171 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3172 sizeof (new_uid));
3173 attrzp->z_uid = new_uid;
3177 if (mask & AT_GID) {
3178 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3179 NULL, &new_gid, sizeof (new_gid));
3180 zp->z_gid = new_gid;
3181 if (attrzp) {
3182 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3183 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3184 sizeof (new_gid));
3185 attrzp->z_gid = new_gid;
3188 if (!(mask & AT_MODE)) {
3189 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3190 NULL, &new_mode, sizeof (new_mode));
3191 new_mode = zp->z_mode;
3193 err = zfs_acl_chown_setattr(zp);
3194 ASSERT(err == 0);
3195 if (attrzp) {
3196 err = zfs_acl_chown_setattr(attrzp);
3197 ASSERT(err == 0);
3201 if (mask & AT_MODE) {
3202 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3203 &new_mode, sizeof (new_mode));
3204 zp->z_mode = new_mode;
3205 ASSERT3U((uintptr_t)aclp, !=, NULL);
3206 err = zfs_aclset_common(zp, aclp, cr, tx);
3207 ASSERT0(err);
3208 if (zp->z_acl_cached)
3209 zfs_acl_free(zp->z_acl_cached);
3210 zp->z_acl_cached = aclp;
3211 aclp = NULL;
3215 if (mask & AT_ATIME) {
3216 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3217 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3218 &zp->z_atime, sizeof (zp->z_atime));
3221 if (mask & AT_MTIME) {
3222 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3223 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3224 mtime, sizeof (mtime));
3227 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3228 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3229 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3230 NULL, mtime, sizeof (mtime));
3231 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3232 &ctime, sizeof (ctime));
3233 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3234 B_TRUE);
3235 } else if (mask != 0) {
3236 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3237 &ctime, sizeof (ctime));
3238 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3239 B_TRUE);
3240 if (attrzp) {
3241 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3242 SA_ZPL_CTIME(zfsvfs), NULL,
3243 &ctime, sizeof (ctime));
3244 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3245 mtime, ctime, B_TRUE);
3249 * Do this after setting timestamps to prevent timestamp
3250 * update from toggling bit
3253 if (xoap && (mask & AT_XVATTR)) {
3256 * restore trimmed off masks
3257 * so that return masks can be set for caller.
3260 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3261 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3263 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3264 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3266 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3267 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3269 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3270 XVA_SET_REQ(xvap, XAT_NODUMP);
3272 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3273 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3275 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3276 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3279 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3280 ASSERT(vp->v_type == VREG);
3282 zfs_xvattr_set(zp, xvap, tx);
3285 if (fuid_dirtied)
3286 zfs_fuid_sync(zfsvfs, tx);
3288 if (mask != 0)
3289 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3291 mutex_exit(&zp->z_lock);
3292 if (mask & (AT_UID|AT_GID|AT_MODE))
3293 mutex_exit(&zp->z_acl_lock);
3295 if (attrzp) {
3296 if (mask & (AT_UID|AT_GID|AT_MODE))
3297 mutex_exit(&attrzp->z_acl_lock);
3298 mutex_exit(&attrzp->z_lock);
3300 out:
3301 if (err == 0 && attrzp) {
3302 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3303 xattr_count, tx);
3304 ASSERT(err2 == 0);
3307 if (attrzp)
3308 VN_RELE(ZTOV(attrzp));
3310 if (aclp)
3311 zfs_acl_free(aclp);
3313 if (fuidp) {
3314 zfs_fuid_info_free(fuidp);
3315 fuidp = NULL;
3318 if (err) {
3319 dmu_tx_abort(tx);
3320 if (err == ERESTART)
3321 goto top;
3322 } else {
3323 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3324 dmu_tx_commit(tx);
3327 out2:
3328 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3329 zil_commit(zilog, 0);
3331 ZFS_EXIT(zfsvfs);
3332 return (err);
3335 typedef struct zfs_zlock {
3336 krwlock_t *zl_rwlock; /* lock we acquired */
3337 znode_t *zl_znode; /* znode we held */
3338 struct zfs_zlock *zl_next; /* next in list */
3339 } zfs_zlock_t;
3342 * Drop locks and release vnodes that were held by zfs_rename_lock().
3344 static void
3345 zfs_rename_unlock(zfs_zlock_t **zlpp)
3347 zfs_zlock_t *zl;
3349 while ((zl = *zlpp) != NULL) {
3350 if (zl->zl_znode != NULL)
3351 VN_RELE(ZTOV(zl->zl_znode));
3352 rw_exit(zl->zl_rwlock);
3353 *zlpp = zl->zl_next;
3354 kmem_free(zl, sizeof (*zl));
3359 * Search back through the directory tree, using the ".." entries.
3360 * Lock each directory in the chain to prevent concurrent renames.
3361 * Fail any attempt to move a directory into one of its own descendants.
3362 * XXX - z_parent_lock can overlap with map or grow locks
3364 static int
3365 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3367 zfs_zlock_t *zl;
3368 znode_t *zp = tdzp;
3369 uint64_t rootid = zp->z_zfsvfs->z_root;
3370 uint64_t oidp = zp->z_id;
3371 krwlock_t *rwlp = &szp->z_parent_lock;
3372 krw_t rw = RW_WRITER;
3375 * First pass write-locks szp and compares to zp->z_id.
3376 * Later passes read-lock zp and compare to zp->z_parent.
3378 do {
3379 if (!rw_tryenter(rwlp, rw)) {
3381 * Another thread is renaming in this path.
3382 * Note that if we are a WRITER, we don't have any
3383 * parent_locks held yet.
3385 if (rw == RW_READER && zp->z_id > szp->z_id) {
3387 * Drop our locks and restart
3389 zfs_rename_unlock(&zl);
3390 *zlpp = NULL;
3391 zp = tdzp;
3392 oidp = zp->z_id;
3393 rwlp = &szp->z_parent_lock;
3394 rw = RW_WRITER;
3395 continue;
3396 } else {
3398 * Wait for other thread to drop its locks
3400 rw_enter(rwlp, rw);
3404 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3405 zl->zl_rwlock = rwlp;
3406 zl->zl_znode = NULL;
3407 zl->zl_next = *zlpp;
3408 *zlpp = zl;
3410 if (oidp == szp->z_id) /* We're a descendant of szp */
3411 return (SET_ERROR(EINVAL));
3413 if (oidp == rootid) /* We've hit the top */
3414 return (0);
3416 if (rw == RW_READER) { /* i.e. not the first pass */
3417 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3418 if (error)
3419 return (error);
3420 zl->zl_znode = zp;
3422 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3423 &oidp, sizeof (oidp));
3424 rwlp = &zp->z_parent_lock;
3425 rw = RW_READER;
3427 } while (zp->z_id != sdzp->z_id);
3429 return (0);
3433 * Move an entry from the provided source directory to the target
3434 * directory. Change the entry name as indicated.
3436 * IN: sdvp - Source directory containing the "old entry".
3437 * snm - Old entry name.
3438 * tdvp - Target directory to contain the "new entry".
3439 * tnm - New entry name.
3440 * cr - credentials of caller.
3441 * ct - caller context
3442 * flags - case flags
3444 * RETURN: 0 on success, error code on failure.
3446 * Timestamps:
3447 * sdvp,tdvp - ctime|mtime updated
3449 /*ARGSUSED*/
3450 static int
3451 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3452 caller_context_t *ct, int flags)
3454 znode_t *tdzp, *szp, *tzp;
3455 znode_t *sdzp = VTOZ(sdvp);
3456 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3457 zilog_t *zilog;
3458 vnode_t *realvp;
3459 zfs_dirlock_t *sdl, *tdl;
3460 dmu_tx_t *tx;
3461 zfs_zlock_t *zl;
3462 int cmp, serr, terr;
3463 int error = 0, rm_err = 0;
3464 int zflg = 0;
3465 boolean_t waited = B_FALSE;
3467 ZFS_ENTER(zfsvfs);
3468 ZFS_VERIFY_ZP(sdzp);
3469 zilog = zfsvfs->z_log;
3472 * Make sure we have the real vp for the target directory.
3474 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3475 tdvp = realvp;
3477 tdzp = VTOZ(tdvp);
3478 ZFS_VERIFY_ZP(tdzp);
3481 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3482 * ctldir appear to have the same v_vfsp.
3484 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3485 ZFS_EXIT(zfsvfs);
3486 return (SET_ERROR(EXDEV));
3489 if (zfsvfs->z_utf8 && u8_validate(tnm,
3490 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3491 ZFS_EXIT(zfsvfs);
3492 return (SET_ERROR(EILSEQ));
3495 if (flags & FIGNORECASE)
3496 zflg |= ZCILOOK;
3498 top:
3499 szp = NULL;
3500 tzp = NULL;
3501 zl = NULL;
3504 * This is to prevent the creation of links into attribute space
3505 * by renaming a linked file into/outof an attribute directory.
3506 * See the comment in zfs_link() for why this is considered bad.
3508 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3509 ZFS_EXIT(zfsvfs);
3510 return (SET_ERROR(EINVAL));
3514 * Lock source and target directory entries. To prevent deadlock,
3515 * a lock ordering must be defined. We lock the directory with
3516 * the smallest object id first, or if it's a tie, the one with
3517 * the lexically first name.
3519 if (sdzp->z_id < tdzp->z_id) {
3520 cmp = -1;
3521 } else if (sdzp->z_id > tdzp->z_id) {
3522 cmp = 1;
3523 } else {
3525 * First compare the two name arguments without
3526 * considering any case folding.
3528 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3530 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3531 ASSERT(error == 0 || !zfsvfs->z_utf8);
3532 if (cmp == 0) {
3534 * POSIX: "If the old argument and the new argument
3535 * both refer to links to the same existing file,
3536 * the rename() function shall return successfully
3537 * and perform no other action."
3539 ZFS_EXIT(zfsvfs);
3540 return (0);
3543 * If the file system is case-folding, then we may
3544 * have some more checking to do. A case-folding file
3545 * system is either supporting mixed case sensitivity
3546 * access or is completely case-insensitive. Note
3547 * that the file system is always case preserving.
3549 * In mixed sensitivity mode case sensitive behavior
3550 * is the default. FIGNORECASE must be used to
3551 * explicitly request case insensitive behavior.
3553 * If the source and target names provided differ only
3554 * by case (e.g., a request to rename 'tim' to 'Tim'),
3555 * we will treat this as a special case in the
3556 * case-insensitive mode: as long as the source name
3557 * is an exact match, we will allow this to proceed as
3558 * a name-change request.
3560 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3561 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3562 flags & FIGNORECASE)) &&
3563 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3564 &error) == 0) {
3566 * case preserving rename request, require exact
3567 * name matches
3569 zflg |= ZCIEXACT;
3570 zflg &= ~ZCILOOK;
3575 * If the source and destination directories are the same, we should
3576 * grab the z_name_lock of that directory only once.
3578 if (sdzp == tdzp) {
3579 zflg |= ZHAVELOCK;
3580 rw_enter(&sdzp->z_name_lock, RW_READER);
3583 if (cmp < 0) {
3584 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3585 ZEXISTS | zflg, NULL, NULL);
3586 terr = zfs_dirent_lock(&tdl,
3587 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3588 } else {
3589 terr = zfs_dirent_lock(&tdl,
3590 tdzp, tnm, &tzp, zflg, NULL, NULL);
3591 serr = zfs_dirent_lock(&sdl,
3592 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3593 NULL, NULL);
3596 if (serr) {
3598 * Source entry invalid or not there.
3600 if (!terr) {
3601 zfs_dirent_unlock(tdl);
3602 if (tzp)
3603 VN_RELE(ZTOV(tzp));
3606 if (sdzp == tdzp)
3607 rw_exit(&sdzp->z_name_lock);
3609 if (strcmp(snm, "..") == 0)
3610 serr = SET_ERROR(EINVAL);
3611 ZFS_EXIT(zfsvfs);
3612 return (serr);
3614 if (terr) {
3615 zfs_dirent_unlock(sdl);
3616 VN_RELE(ZTOV(szp));
3618 if (sdzp == tdzp)
3619 rw_exit(&sdzp->z_name_lock);
3621 if (strcmp(tnm, "..") == 0)
3622 terr = SET_ERROR(EINVAL);
3623 ZFS_EXIT(zfsvfs);
3624 return (terr);
3628 * Must have write access at the source to remove the old entry
3629 * and write access at the target to create the new entry.
3630 * Note that if target and source are the same, this can be
3631 * done in a single check.
3634 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3635 goto out;
3637 if (ZTOV(szp)->v_type == VDIR) {
3639 * Check to make sure rename is valid.
3640 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3642 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3643 goto out;
3647 * Does target exist?
3649 if (tzp) {
3651 * Source and target must be the same type.
3653 if (ZTOV(szp)->v_type == VDIR) {
3654 if (ZTOV(tzp)->v_type != VDIR) {
3655 error = SET_ERROR(ENOTDIR);
3656 goto out;
3658 } else {
3659 if (ZTOV(tzp)->v_type == VDIR) {
3660 error = SET_ERROR(EISDIR);
3661 goto out;
3665 * POSIX dictates that when the source and target
3666 * entries refer to the same file object, rename
3667 * must do nothing and exit without error.
3669 if (szp->z_id == tzp->z_id) {
3670 error = 0;
3671 goto out;
3675 vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3676 if (tzp)
3677 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3680 * notify the target directory if it is not the same
3681 * as source directory.
3683 if (tdvp != sdvp) {
3684 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3687 tx = dmu_tx_create(zfsvfs->z_os);
3688 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3689 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3690 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3691 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3692 if (sdzp != tdzp) {
3693 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3694 zfs_sa_upgrade_txholds(tx, tdzp);
3696 if (tzp) {
3697 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3698 zfs_sa_upgrade_txholds(tx, tzp);
3701 zfs_sa_upgrade_txholds(tx, szp);
3702 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3703 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3704 if (error) {
3705 if (zl != NULL)
3706 zfs_rename_unlock(&zl);
3707 zfs_dirent_unlock(sdl);
3708 zfs_dirent_unlock(tdl);
3710 if (sdzp == tdzp)
3711 rw_exit(&sdzp->z_name_lock);
3713 VN_RELE(ZTOV(szp));
3714 if (tzp)
3715 VN_RELE(ZTOV(tzp));
3716 if (error == ERESTART) {
3717 waited = B_TRUE;
3718 dmu_tx_wait(tx);
3719 dmu_tx_abort(tx);
3720 goto top;
3722 dmu_tx_abort(tx);
3723 ZFS_EXIT(zfsvfs);
3724 return (error);
3727 if (tzp) /* Attempt to remove the existing target */
3728 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3730 if (error == 0) {
3731 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3732 if (error == 0) {
3733 szp->z_pflags |= ZFS_AV_MODIFIED;
3735 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3736 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3737 ASSERT0(error);
3739 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3740 if (error == 0) {
3741 zfs_log_rename(zilog, tx, TX_RENAME |
3742 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3743 sdl->dl_name, tdzp, tdl->dl_name, szp);
3746 * Update path information for the target vnode
3748 vn_renamepath(tdvp, ZTOV(szp), tnm,
3749 strlen(tnm));
3750 } else {
3752 * At this point, we have successfully created
3753 * the target name, but have failed to remove
3754 * the source name. Since the create was done
3755 * with the ZRENAMING flag, there are
3756 * complications; for one, the link count is
3757 * wrong. The easiest way to deal with this
3758 * is to remove the newly created target, and
3759 * return the original error. This must
3760 * succeed; fortunately, it is very unlikely to
3761 * fail, since we just created it.
3763 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3764 ZRENAMING, NULL), ==, 0);
3769 dmu_tx_commit(tx);
3771 if (tzp && rm_err == 0)
3772 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3774 if (error == 0) {
3775 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3776 /* notify the target dir if it is not the same as source dir */
3777 if (tdvp != sdvp)
3778 vnevent_rename_dest_dir(tdvp, ct);
3780 out:
3781 if (zl != NULL)
3782 zfs_rename_unlock(&zl);
3784 zfs_dirent_unlock(sdl);
3785 zfs_dirent_unlock(tdl);
3787 if (sdzp == tdzp)
3788 rw_exit(&sdzp->z_name_lock);
3791 VN_RELE(ZTOV(szp));
3792 if (tzp)
3793 VN_RELE(ZTOV(tzp));
3795 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3796 zil_commit(zilog, 0);
3798 ZFS_EXIT(zfsvfs);
3799 return (error);
3803 * Insert the indicated symbolic reference entry into the directory.
3805 * IN: dvp - Directory to contain new symbolic link.
3806 * link - Name for new symlink entry.
3807 * vap - Attributes of new entry.
3808 * cr - credentials of caller.
3809 * ct - caller context
3810 * flags - case flags
3812 * RETURN: 0 on success, error code on failure.
3814 * Timestamps:
3815 * dvp - ctime|mtime updated
3817 /*ARGSUSED*/
3818 static int
3819 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3820 caller_context_t *ct, int flags)
3822 znode_t *zp, *dzp = VTOZ(dvp);
3823 zfs_dirlock_t *dl;
3824 dmu_tx_t *tx;
3825 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3826 zilog_t *zilog;
3827 uint64_t len = strlen(link);
3828 int error;
3829 int zflg = ZNEW;
3830 zfs_acl_ids_t acl_ids;
3831 boolean_t fuid_dirtied;
3832 uint64_t txtype = TX_SYMLINK;
3833 boolean_t waited = B_FALSE;
3835 ASSERT(vap->va_type == VLNK);
3837 ZFS_ENTER(zfsvfs);
3838 ZFS_VERIFY_ZP(dzp);
3839 zilog = zfsvfs->z_log;
3841 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3842 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3843 ZFS_EXIT(zfsvfs);
3844 return (SET_ERROR(EILSEQ));
3846 if (flags & FIGNORECASE)
3847 zflg |= ZCILOOK;
3849 if (len > MAXPATHLEN) {
3850 ZFS_EXIT(zfsvfs);
3851 return (SET_ERROR(ENAMETOOLONG));
3854 if ((error = zfs_acl_ids_create(dzp, 0,
3855 vap, cr, NULL, &acl_ids)) != 0) {
3856 ZFS_EXIT(zfsvfs);
3857 return (error);
3859 top:
3861 * Attempt to lock directory; fail if entry already exists.
3863 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3864 if (error) {
3865 zfs_acl_ids_free(&acl_ids);
3866 ZFS_EXIT(zfsvfs);
3867 return (error);
3870 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3871 zfs_acl_ids_free(&acl_ids);
3872 zfs_dirent_unlock(dl);
3873 ZFS_EXIT(zfsvfs);
3874 return (error);
3877 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3878 zfs_acl_ids_free(&acl_ids);
3879 zfs_dirent_unlock(dl);
3880 ZFS_EXIT(zfsvfs);
3881 return (SET_ERROR(EDQUOT));
3883 tx = dmu_tx_create(zfsvfs->z_os);
3884 fuid_dirtied = zfsvfs->z_fuid_dirty;
3885 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3886 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3887 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3888 ZFS_SA_BASE_ATTR_SIZE + len);
3889 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3890 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3891 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3892 acl_ids.z_aclp->z_acl_bytes);
3894 if (fuid_dirtied)
3895 zfs_fuid_txhold(zfsvfs, tx);
3896 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3897 if (error) {
3898 zfs_dirent_unlock(dl);
3899 if (error == ERESTART) {
3900 waited = B_TRUE;
3901 dmu_tx_wait(tx);
3902 dmu_tx_abort(tx);
3903 goto top;
3905 zfs_acl_ids_free(&acl_ids);
3906 dmu_tx_abort(tx);
3907 ZFS_EXIT(zfsvfs);
3908 return (error);
3912 * Create a new object for the symlink.
3913 * for version 4 ZPL datsets the symlink will be an SA attribute
3915 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3917 if (fuid_dirtied)
3918 zfs_fuid_sync(zfsvfs, tx);
3920 mutex_enter(&zp->z_lock);
3921 if (zp->z_is_sa)
3922 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3923 link, len, tx);
3924 else
3925 zfs_sa_symlink(zp, link, len, tx);
3926 mutex_exit(&zp->z_lock);
3928 zp->z_size = len;
3929 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3930 &zp->z_size, sizeof (zp->z_size), tx);
3932 * Insert the new object into the directory.
3934 (void) zfs_link_create(dl, zp, tx, ZNEW);
3936 if (flags & FIGNORECASE)
3937 txtype |= TX_CI;
3938 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3940 zfs_acl_ids_free(&acl_ids);
3942 dmu_tx_commit(tx);
3944 zfs_dirent_unlock(dl);
3946 VN_RELE(ZTOV(zp));
3948 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3949 zil_commit(zilog, 0);
3951 ZFS_EXIT(zfsvfs);
3952 return (error);
3956 * Return, in the buffer contained in the provided uio structure,
3957 * the symbolic path referred to by vp.
3959 * IN: vp - vnode of symbolic link.
3960 * uio - structure to contain the link path.
3961 * cr - credentials of caller.
3962 * ct - caller context
3964 * OUT: uio - structure containing the link path.
3966 * RETURN: 0 on success, error code on failure.
3968 * Timestamps:
3969 * vp - atime updated
3971 /* ARGSUSED */
3972 static int
3973 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3975 znode_t *zp = VTOZ(vp);
3976 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3977 int error;
3979 ZFS_ENTER(zfsvfs);
3980 ZFS_VERIFY_ZP(zp);
3982 mutex_enter(&zp->z_lock);
3983 if (zp->z_is_sa)
3984 error = sa_lookup_uio(zp->z_sa_hdl,
3985 SA_ZPL_SYMLINK(zfsvfs), uio);
3986 else
3987 error = zfs_sa_readlink(zp, uio);
3988 mutex_exit(&zp->z_lock);
3990 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3992 ZFS_EXIT(zfsvfs);
3993 return (error);
3997 * Insert a new entry into directory tdvp referencing svp.
3999 * IN: tdvp - Directory to contain new entry.
4000 * svp - vnode of new entry.
4001 * name - name of new entry.
4002 * cr - credentials of caller.
4003 * ct - caller context
4005 * RETURN: 0 on success, error code on failure.
4007 * Timestamps:
4008 * tdvp - ctime|mtime updated
4009 * svp - ctime updated
4011 /* ARGSUSED */
4012 static int
4013 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4014 caller_context_t *ct, int flags)
4016 znode_t *dzp = VTOZ(tdvp);
4017 znode_t *tzp, *szp;
4018 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4019 zilog_t *zilog;
4020 zfs_dirlock_t *dl;
4021 dmu_tx_t *tx;
4022 vnode_t *realvp;
4023 int error;
4024 int zf = ZNEW;
4025 uint64_t parent;
4026 uid_t owner;
4027 boolean_t waited = B_FALSE;
4029 ASSERT(tdvp->v_type == VDIR);
4031 ZFS_ENTER(zfsvfs);
4032 ZFS_VERIFY_ZP(dzp);
4033 zilog = zfsvfs->z_log;
4035 if (VOP_REALVP(svp, &realvp, ct) == 0)
4036 svp = realvp;
4039 * POSIX dictates that we return EPERM here.
4040 * Better choices include ENOTSUP or EISDIR.
4042 if (svp->v_type == VDIR) {
4043 ZFS_EXIT(zfsvfs);
4044 return (SET_ERROR(EPERM));
4047 szp = VTOZ(svp);
4048 ZFS_VERIFY_ZP(szp);
4051 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4052 * ctldir appear to have the same v_vfsp.
4054 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4055 ZFS_EXIT(zfsvfs);
4056 return (SET_ERROR(EXDEV));
4059 /* Prevent links to .zfs/shares files */
4061 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4062 &parent, sizeof (uint64_t))) != 0) {
4063 ZFS_EXIT(zfsvfs);
4064 return (error);
4066 if (parent == zfsvfs->z_shares_dir) {
4067 ZFS_EXIT(zfsvfs);
4068 return (SET_ERROR(EPERM));
4071 if (zfsvfs->z_utf8 && u8_validate(name,
4072 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4073 ZFS_EXIT(zfsvfs);
4074 return (SET_ERROR(EILSEQ));
4076 if (flags & FIGNORECASE)
4077 zf |= ZCILOOK;
4080 * We do not support links between attributes and non-attributes
4081 * because of the potential security risk of creating links
4082 * into "normal" file space in order to circumvent restrictions
4083 * imposed in attribute space.
4085 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4086 ZFS_EXIT(zfsvfs);
4087 return (SET_ERROR(EINVAL));
4091 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4092 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4093 ZFS_EXIT(zfsvfs);
4094 return (SET_ERROR(EPERM));
4097 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4098 ZFS_EXIT(zfsvfs);
4099 return (error);
4102 top:
4104 * Attempt to lock directory; fail if entry already exists.
4106 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4107 if (error) {
4108 ZFS_EXIT(zfsvfs);
4109 return (error);
4112 tx = dmu_tx_create(zfsvfs->z_os);
4113 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4114 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4115 zfs_sa_upgrade_txholds(tx, szp);
4116 zfs_sa_upgrade_txholds(tx, dzp);
4117 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4118 if (error) {
4119 zfs_dirent_unlock(dl);
4120 if (error == ERESTART) {
4121 waited = B_TRUE;
4122 dmu_tx_wait(tx);
4123 dmu_tx_abort(tx);
4124 goto top;
4126 dmu_tx_abort(tx);
4127 ZFS_EXIT(zfsvfs);
4128 return (error);
4131 error = zfs_link_create(dl, szp, tx, 0);
4133 if (error == 0) {
4134 uint64_t txtype = TX_LINK;
4135 if (flags & FIGNORECASE)
4136 txtype |= TX_CI;
4137 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4140 dmu_tx_commit(tx);
4142 zfs_dirent_unlock(dl);
4144 if (error == 0) {
4145 vnevent_link(svp, ct);
4148 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4149 zil_commit(zilog, 0);
4151 ZFS_EXIT(zfsvfs);
4152 return (error);
4156 * zfs_null_putapage() is used when the file system has been force
4157 * unmounted. It just drops the pages.
4159 /* ARGSUSED */
4160 static int
4161 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4162 size_t *lenp, int flags, cred_t *cr)
4164 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4165 return (0);
4169 * Push a page out to disk, klustering if possible.
4171 * IN: vp - file to push page to.
4172 * pp - page to push.
4173 * flags - additional flags.
4174 * cr - credentials of caller.
4176 * OUT: offp - start of range pushed.
4177 * lenp - len of range pushed.
4179 * RETURN: 0 on success, error code on failure.
4181 * NOTE: callers must have locked the page to be pushed. On
4182 * exit, the page (and all other pages in the kluster) must be
4183 * unlocked.
4185 /* ARGSUSED */
4186 static int
4187 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4188 size_t *lenp, int flags, cred_t *cr)
4190 znode_t *zp = VTOZ(vp);
4191 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4192 dmu_tx_t *tx;
4193 u_offset_t off, koff;
4194 size_t len, klen;
4195 int err;
4197 off = pp->p_offset;
4198 len = PAGESIZE;
4200 * If our blocksize is bigger than the page size, try to kluster
4201 * multiple pages so that we write a full block (thus avoiding
4202 * a read-modify-write).
4204 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4205 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4206 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4207 ASSERT(koff <= zp->z_size);
4208 if (koff + klen > zp->z_size)
4209 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4210 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4212 ASSERT3U(btop(len), ==, btopr(len));
4215 * Can't push pages past end-of-file.
4217 if (off >= zp->z_size) {
4218 /* ignore all pages */
4219 err = 0;
4220 goto out;
4221 } else if (off + len > zp->z_size) {
4222 int npages = btopr(zp->z_size - off);
4223 page_t *trunc;
4225 page_list_break(&pp, &trunc, npages);
4226 /* ignore pages past end of file */
4227 if (trunc)
4228 pvn_write_done(trunc, flags);
4229 len = zp->z_size - off;
4232 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4233 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4234 err = SET_ERROR(EDQUOT);
4235 goto out;
4237 tx = dmu_tx_create(zfsvfs->z_os);
4238 dmu_tx_hold_write(tx, zp->z_id, off, len);
4240 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4241 zfs_sa_upgrade_txholds(tx, zp);
4242 err = dmu_tx_assign(tx, TXG_WAIT);
4243 if (err != 0) {
4244 dmu_tx_abort(tx);
4245 goto out;
4248 if (zp->z_blksz <= PAGESIZE) {
4249 caddr_t va = zfs_map_page(pp, S_READ);
4250 ASSERT3U(len, <=, PAGESIZE);
4251 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4252 zfs_unmap_page(pp, va);
4253 } else {
4254 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4257 if (err == 0) {
4258 uint64_t mtime[2], ctime[2];
4259 sa_bulk_attr_t bulk[3];
4260 int count = 0;
4262 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4263 &mtime, 16);
4264 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4265 &ctime, 16);
4266 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4267 &zp->z_pflags, 8);
4268 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4269 B_TRUE);
4270 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4271 ASSERT0(err);
4272 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4274 dmu_tx_commit(tx);
4276 out:
4277 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4278 if (offp)
4279 *offp = off;
4280 if (lenp)
4281 *lenp = len;
4283 return (err);
4287 * Copy the portion of the file indicated from pages into the file.
4288 * The pages are stored in a page list attached to the files vnode.
4290 * IN: vp - vnode of file to push page data to.
4291 * off - position in file to put data.
4292 * len - amount of data to write.
4293 * flags - flags to control the operation.
4294 * cr - credentials of caller.
4295 * ct - caller context.
4297 * RETURN: 0 on success, error code on failure.
4299 * Timestamps:
4300 * vp - ctime|mtime updated
4302 /*ARGSUSED*/
4303 static int
4304 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4305 caller_context_t *ct)
4307 znode_t *zp = VTOZ(vp);
4308 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4309 page_t *pp;
4310 size_t io_len;
4311 u_offset_t io_off;
4312 uint_t blksz;
4313 rl_t *rl;
4314 int error = 0;
4316 ZFS_ENTER(zfsvfs);
4317 ZFS_VERIFY_ZP(zp);
4320 * There's nothing to do if no data is cached.
4322 if (!vn_has_cached_data(vp)) {
4323 ZFS_EXIT(zfsvfs);
4324 return (0);
4328 * Align this request to the file block size in case we kluster.
4329 * XXX - this can result in pretty aggresive locking, which can
4330 * impact simultanious read/write access. One option might be
4331 * to break up long requests (len == 0) into block-by-block
4332 * operations to get narrower locking.
4334 blksz = zp->z_blksz;
4335 if (ISP2(blksz))
4336 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4337 else
4338 io_off = 0;
4339 if (len > 0 && ISP2(blksz))
4340 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4341 else
4342 io_len = 0;
4344 if (io_len == 0) {
4346 * Search the entire vp list for pages >= io_off.
4348 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4349 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4350 goto out;
4352 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4354 if (off > zp->z_size) {
4355 /* past end of file */
4356 zfs_range_unlock(rl);
4357 ZFS_EXIT(zfsvfs);
4358 return (0);
4361 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4363 for (off = io_off; io_off < off + len; io_off += io_len) {
4364 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4365 pp = page_lookup(vp, io_off,
4366 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4367 } else {
4368 pp = page_lookup_nowait(vp, io_off,
4369 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4372 if (pp != NULL && pvn_getdirty(pp, flags)) {
4373 int err;
4376 * Found a dirty page to push
4378 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4379 if (err)
4380 error = err;
4381 } else {
4382 io_len = PAGESIZE;
4385 out:
4386 zfs_range_unlock(rl);
4387 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4388 zil_commit(zfsvfs->z_log, zp->z_id);
4389 ZFS_EXIT(zfsvfs);
4390 return (error);
4393 /*ARGSUSED*/
4394 void
4395 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4397 znode_t *zp = VTOZ(vp);
4398 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4399 int error;
4401 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4402 if (zp->z_sa_hdl == NULL) {
4404 * The fs has been unmounted, or we did a
4405 * suspend/resume and this file no longer exists.
4407 if (vn_has_cached_data(vp)) {
4408 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4409 B_INVAL, cr);
4412 mutex_enter(&zp->z_lock);
4413 mutex_enter(&vp->v_lock);
4414 ASSERT(vp->v_count == 1);
4415 VN_RELE_LOCKED(vp);
4416 mutex_exit(&vp->v_lock);
4417 mutex_exit(&zp->z_lock);
4418 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4419 zfs_znode_free(zp);
4420 return;
4424 * Attempt to push any data in the page cache. If this fails
4425 * we will get kicked out later in zfs_zinactive().
4427 if (vn_has_cached_data(vp)) {
4428 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4429 cr);
4432 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4433 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4435 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4436 zfs_sa_upgrade_txholds(tx, zp);
4437 error = dmu_tx_assign(tx, TXG_WAIT);
4438 if (error) {
4439 dmu_tx_abort(tx);
4440 } else {
4441 mutex_enter(&zp->z_lock);
4442 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4443 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4444 zp->z_atime_dirty = 0;
4445 mutex_exit(&zp->z_lock);
4446 dmu_tx_commit(tx);
4450 zfs_zinactive(zp);
4451 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4455 * Bounds-check the seek operation.
4457 * IN: vp - vnode seeking within
4458 * ooff - old file offset
4459 * noffp - pointer to new file offset
4460 * ct - caller context
4462 * RETURN: 0 on success, EINVAL if new offset invalid.
4464 /* ARGSUSED */
4465 static int
4466 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4467 caller_context_t *ct)
4469 if (vp->v_type == VDIR)
4470 return (0);
4471 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4475 * Pre-filter the generic locking function to trap attempts to place
4476 * a mandatory lock on a memory mapped file.
4478 static int
4479 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4480 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4482 znode_t *zp = VTOZ(vp);
4483 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4485 ZFS_ENTER(zfsvfs);
4486 ZFS_VERIFY_ZP(zp);
4489 * We are following the UFS semantics with respect to mapcnt
4490 * here: If we see that the file is mapped already, then we will
4491 * return an error, but we don't worry about races between this
4492 * function and zfs_map().
4494 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4495 ZFS_EXIT(zfsvfs);
4496 return (SET_ERROR(EAGAIN));
4498 ZFS_EXIT(zfsvfs);
4499 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4503 * If we can't find a page in the cache, we will create a new page
4504 * and fill it with file data. For efficiency, we may try to fill
4505 * multiple pages at once (klustering) to fill up the supplied page
4506 * list. Note that the pages to be filled are held with an exclusive
4507 * lock to prevent access by other threads while they are being filled.
4509 static int
4510 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4511 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4513 znode_t *zp = VTOZ(vp);
4514 page_t *pp, *cur_pp;
4515 objset_t *os = zp->z_zfsvfs->z_os;
4516 u_offset_t io_off, total;
4517 size_t io_len;
4518 int err;
4520 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4522 * We only have a single page, don't bother klustering
4524 io_off = off;
4525 io_len = PAGESIZE;
4526 pp = page_create_va(vp, io_off, io_len,
4527 PG_EXCL | PG_WAIT, seg, addr);
4528 } else {
4530 * Try to find enough pages to fill the page list
4532 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4533 &io_len, off, plsz, 0);
4535 if (pp == NULL) {
4537 * The page already exists, nothing to do here.
4539 *pl = NULL;
4540 return (0);
4544 * Fill the pages in the kluster.
4546 cur_pp = pp;
4547 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4548 caddr_t va;
4550 ASSERT3U(io_off, ==, cur_pp->p_offset);
4551 va = zfs_map_page(cur_pp, S_WRITE);
4552 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4553 DMU_READ_PREFETCH);
4554 zfs_unmap_page(cur_pp, va);
4555 if (err) {
4556 /* On error, toss the entire kluster */
4557 pvn_read_done(pp, B_ERROR);
4558 /* convert checksum errors into IO errors */
4559 if (err == ECKSUM)
4560 err = SET_ERROR(EIO);
4561 return (err);
4563 cur_pp = cur_pp->p_next;
4567 * Fill in the page list array from the kluster starting
4568 * from the desired offset `off'.
4569 * NOTE: the page list will always be null terminated.
4571 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4572 ASSERT(pl == NULL || (*pl)->p_offset == off);
4574 return (0);
4578 * Return pointers to the pages for the file region [off, off + len]
4579 * in the pl array. If plsz is greater than len, this function may
4580 * also return page pointers from after the specified region
4581 * (i.e. the region [off, off + plsz]). These additional pages are
4582 * only returned if they are already in the cache, or were created as
4583 * part of a klustered read.
4585 * IN: vp - vnode of file to get data from.
4586 * off - position in file to get data from.
4587 * len - amount of data to retrieve.
4588 * plsz - length of provided page list.
4589 * seg - segment to obtain pages for.
4590 * addr - virtual address of fault.
4591 * rw - mode of created pages.
4592 * cr - credentials of caller.
4593 * ct - caller context.
4595 * OUT: protp - protection mode of created pages.
4596 * pl - list of pages created.
4598 * RETURN: 0 on success, error code on failure.
4600 * Timestamps:
4601 * vp - atime updated
4603 /* ARGSUSED */
4604 static int
4605 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4606 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4607 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4609 znode_t *zp = VTOZ(vp);
4610 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4611 page_t **pl0 = pl;
4612 int err = 0;
4614 /* we do our own caching, faultahead is unnecessary */
4615 if (pl == NULL)
4616 return (0);
4617 else if (len > plsz)
4618 len = plsz;
4619 else
4620 len = P2ROUNDUP(len, PAGESIZE);
4621 ASSERT(plsz >= len);
4623 ZFS_ENTER(zfsvfs);
4624 ZFS_VERIFY_ZP(zp);
4626 if (protp)
4627 *protp = PROT_ALL;
4630 * Loop through the requested range [off, off + len) looking
4631 * for pages. If we don't find a page, we will need to create
4632 * a new page and fill it with data from the file.
4634 while (len > 0) {
4635 if (*pl = page_lookup(vp, off, SE_SHARED))
4636 *(pl+1) = NULL;
4637 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4638 goto out;
4639 while (*pl) {
4640 ASSERT3U((*pl)->p_offset, ==, off);
4641 off += PAGESIZE;
4642 addr += PAGESIZE;
4643 if (len > 0) {
4644 ASSERT3U(len, >=, PAGESIZE);
4645 len -= PAGESIZE;
4647 ASSERT3U(plsz, >=, PAGESIZE);
4648 plsz -= PAGESIZE;
4649 pl++;
4654 * Fill out the page array with any pages already in the cache.
4656 while (plsz > 0 &&
4657 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4658 off += PAGESIZE;
4659 plsz -= PAGESIZE;
4661 out:
4662 if (err) {
4664 * Release any pages we have previously locked.
4666 while (pl > pl0)
4667 page_unlock(*--pl);
4668 } else {
4669 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4672 *pl = NULL;
4674 ZFS_EXIT(zfsvfs);
4675 return (err);
4679 * Request a memory map for a section of a file. This code interacts
4680 * with common code and the VM system as follows:
4682 * - common code calls mmap(), which ends up in smmap_common()
4683 * - this calls VOP_MAP(), which takes you into (say) zfs
4684 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4685 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4686 * - zfs_addmap() updates z_mapcnt
4688 /*ARGSUSED*/
4689 static int
4690 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4691 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4692 caller_context_t *ct)
4694 znode_t *zp = VTOZ(vp);
4695 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4696 segvn_crargs_t vn_a;
4697 int error;
4699 ZFS_ENTER(zfsvfs);
4700 ZFS_VERIFY_ZP(zp);
4703 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
4706 if ((prot & PROT_WRITE) && (zp->z_pflags &
4707 (ZFS_IMMUTABLE | ZFS_APPENDONLY))) {
4708 ZFS_EXIT(zfsvfs);
4709 return (SET_ERROR(EPERM));
4712 if ((prot & (PROT_READ | PROT_EXEC)) &&
4713 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4714 ZFS_EXIT(zfsvfs);
4715 return (SET_ERROR(EACCES));
4718 if (vp->v_flag & VNOMAP) {
4719 ZFS_EXIT(zfsvfs);
4720 return (SET_ERROR(ENOSYS));
4723 if (off < 0 || len > MAXOFFSET_T - off) {
4724 ZFS_EXIT(zfsvfs);
4725 return (SET_ERROR(ENXIO));
4728 if (vp->v_type != VREG) {
4729 ZFS_EXIT(zfsvfs);
4730 return (SET_ERROR(ENODEV));
4734 * If file is locked, disallow mapping.
4736 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4737 ZFS_EXIT(zfsvfs);
4738 return (SET_ERROR(EAGAIN));
4741 as_rangelock(as);
4742 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4743 if (error != 0) {
4744 as_rangeunlock(as);
4745 ZFS_EXIT(zfsvfs);
4746 return (error);
4749 vn_a.vp = vp;
4750 vn_a.offset = (u_offset_t)off;
4751 vn_a.type = flags & MAP_TYPE;
4752 vn_a.prot = prot;
4753 vn_a.maxprot = maxprot;
4754 vn_a.cred = cr;
4755 vn_a.amp = NULL;
4756 vn_a.flags = flags & ~MAP_TYPE;
4757 vn_a.szc = 0;
4758 vn_a.lgrp_mem_policy_flags = 0;
4760 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4762 as_rangeunlock(as);
4763 ZFS_EXIT(zfsvfs);
4764 return (error);
4767 /* ARGSUSED */
4768 static int
4769 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4770 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4771 caller_context_t *ct)
4773 uint64_t pages = btopr(len);
4775 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4776 return (0);
4780 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4781 * more accurate mtime for the associated file. Since we don't have a way of
4782 * detecting when the data was actually modified, we have to resort to
4783 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4784 * last page is pushed. The problem occurs when the msync() call is omitted,
4785 * which by far the most common case:
4787 * open()
4788 * mmap()
4789 * <modify memory>
4790 * munmap()
4791 * close()
4792 * <time lapse>
4793 * putpage() via fsflush
4795 * If we wait until fsflush to come along, we can have a modification time that
4796 * is some arbitrary point in the future. In order to prevent this in the
4797 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4798 * torn down.
4800 /* ARGSUSED */
4801 static int
4802 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4803 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4804 caller_context_t *ct)
4806 uint64_t pages = btopr(len);
4808 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4809 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4811 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4812 vn_has_cached_data(vp))
4813 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4815 return (0);
4819 * Free or allocate space in a file. Currently, this function only
4820 * supports the `F_FREESP' command. However, this command is somewhat
4821 * misnamed, as its functionality includes the ability to allocate as
4822 * well as free space.
4824 * IN: vp - vnode of file to free data in.
4825 * cmd - action to take (only F_FREESP supported).
4826 * bfp - section of file to free/alloc.
4827 * flag - current file open mode flags.
4828 * offset - current file offset.
4829 * cr - credentials of caller [UNUSED].
4830 * ct - caller context.
4832 * RETURN: 0 on success, error code on failure.
4834 * Timestamps:
4835 * vp - ctime|mtime updated
4837 /* ARGSUSED */
4838 static int
4839 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4840 offset_t offset, cred_t *cr, caller_context_t *ct)
4842 znode_t *zp = VTOZ(vp);
4843 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4844 uint64_t off, len;
4845 int error;
4847 ZFS_ENTER(zfsvfs);
4848 ZFS_VERIFY_ZP(zp);
4850 if (cmd != F_FREESP) {
4851 ZFS_EXIT(zfsvfs);
4852 return (SET_ERROR(EINVAL));
4856 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4857 * callers might not be able to detect properly that we are read-only,
4858 * so check it explicitly here.
4860 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4861 ZFS_EXIT(zfsvfs);
4862 return (SET_ERROR(EROFS));
4865 if (error = convoff(vp, bfp, 0, offset)) {
4866 ZFS_EXIT(zfsvfs);
4867 return (error);
4870 if (bfp->l_len < 0) {
4871 ZFS_EXIT(zfsvfs);
4872 return (SET_ERROR(EINVAL));
4875 off = bfp->l_start;
4876 len = bfp->l_len; /* 0 means from off to end of file */
4878 error = zfs_freesp(zp, off, len, flag, TRUE);
4880 if (error == 0 && off == 0 && len == 0)
4881 vnevent_truncate(ZTOV(zp), ct);
4883 ZFS_EXIT(zfsvfs);
4884 return (error);
4887 /*ARGSUSED*/
4888 static int
4889 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4891 znode_t *zp = VTOZ(vp);
4892 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4893 uint32_t gen;
4894 uint64_t gen64;
4895 uint64_t object = zp->z_id;
4896 zfid_short_t *zfid;
4897 int size, i, error;
4899 ZFS_ENTER(zfsvfs);
4900 ZFS_VERIFY_ZP(zp);
4902 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4903 &gen64, sizeof (uint64_t))) != 0) {
4904 ZFS_EXIT(zfsvfs);
4905 return (error);
4908 gen = (uint32_t)gen64;
4910 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4911 if (fidp->fid_len < size) {
4912 fidp->fid_len = size;
4913 ZFS_EXIT(zfsvfs);
4914 return (SET_ERROR(ENOSPC));
4917 zfid = (zfid_short_t *)fidp;
4919 zfid->zf_len = size;
4921 for (i = 0; i < sizeof (zfid->zf_object); i++)
4922 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4924 /* Must have a non-zero generation number to distinguish from .zfs */
4925 if (gen == 0)
4926 gen = 1;
4927 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4928 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4930 if (size == LONG_FID_LEN) {
4931 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4932 zfid_long_t *zlfid;
4934 zlfid = (zfid_long_t *)fidp;
4936 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4937 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4939 /* XXX - this should be the generation number for the objset */
4940 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4941 zlfid->zf_setgen[i] = 0;
4944 ZFS_EXIT(zfsvfs);
4945 return (0);
4948 static int
4949 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4950 caller_context_t *ct)
4952 znode_t *zp, *xzp;
4953 zfsvfs_t *zfsvfs;
4954 zfs_dirlock_t *dl;
4955 int error;
4957 switch (cmd) {
4958 case _PC_LINK_MAX:
4959 *valp = ULONG_MAX;
4960 return (0);
4962 case _PC_FILESIZEBITS:
4963 *valp = 64;
4964 return (0);
4966 case _PC_XATTR_EXISTS:
4967 zp = VTOZ(vp);
4968 zfsvfs = zp->z_zfsvfs;
4969 ZFS_ENTER(zfsvfs);
4970 ZFS_VERIFY_ZP(zp);
4971 *valp = 0;
4972 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4973 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4974 if (error == 0) {
4975 zfs_dirent_unlock(dl);
4976 if (!zfs_dirempty(xzp))
4977 *valp = 1;
4978 VN_RELE(ZTOV(xzp));
4979 } else if (error == ENOENT) {
4981 * If there aren't extended attributes, it's the
4982 * same as having zero of them.
4984 error = 0;
4986 ZFS_EXIT(zfsvfs);
4987 return (error);
4989 case _PC_SATTR_ENABLED:
4990 case _PC_SATTR_EXISTS:
4991 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4992 (vp->v_type == VREG || vp->v_type == VDIR);
4993 return (0);
4995 case _PC_ACCESS_FILTERING:
4996 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4997 vp->v_type == VDIR;
4998 return (0);
5000 case _PC_ACL_ENABLED:
5001 *valp = _ACL_ACE_ENABLED;
5002 return (0);
5004 case _PC_MIN_HOLE_SIZE:
5005 *valp = (ulong_t)SPA_MINBLOCKSIZE;
5006 return (0);
5008 case _PC_TIMESTAMP_RESOLUTION:
5009 /* nanosecond timestamp resolution */
5010 *valp = 1L;
5011 return (0);
5013 default:
5014 return (fs_pathconf(vp, cmd, valp, cr, ct));
5018 /*ARGSUSED*/
5019 static int
5020 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5021 caller_context_t *ct)
5023 znode_t *zp = VTOZ(vp);
5024 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5025 int error;
5026 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5028 ZFS_ENTER(zfsvfs);
5029 ZFS_VERIFY_ZP(zp);
5030 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5031 ZFS_EXIT(zfsvfs);
5033 return (error);
5036 /*ARGSUSED*/
5037 static int
5038 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5039 caller_context_t *ct)
5041 znode_t *zp = VTOZ(vp);
5042 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5043 int error;
5044 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5045 zilog_t *zilog = zfsvfs->z_log;
5047 ZFS_ENTER(zfsvfs);
5048 ZFS_VERIFY_ZP(zp);
5050 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5052 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5053 zil_commit(zilog, 0);
5055 ZFS_EXIT(zfsvfs);
5056 return (error);
5060 * The smallest read we may consider to loan out an arcbuf.
5061 * This must be a power of 2.
5063 int zcr_blksz_min = (1 << 10); /* 1K */
5065 * If set to less than the file block size, allow loaning out of an
5066 * arcbuf for a partial block read. This must be a power of 2.
5068 int zcr_blksz_max = (1 << 17); /* 128K */
5070 /*ARGSUSED*/
5071 static int
5072 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5073 caller_context_t *ct)
5075 znode_t *zp = VTOZ(vp);
5076 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5077 int max_blksz = zfsvfs->z_max_blksz;
5078 uio_t *uio = &xuio->xu_uio;
5079 ssize_t size = uio->uio_resid;
5080 offset_t offset = uio->uio_loffset;
5081 int blksz;
5082 int fullblk, i;
5083 arc_buf_t *abuf;
5084 ssize_t maxsize;
5085 int preamble, postamble;
5087 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5088 return (SET_ERROR(EINVAL));
5090 ZFS_ENTER(zfsvfs);
5091 ZFS_VERIFY_ZP(zp);
5092 switch (ioflag) {
5093 case UIO_WRITE:
5095 * Loan out an arc_buf for write if write size is bigger than
5096 * max_blksz, and the file's block size is also max_blksz.
5098 blksz = max_blksz;
5099 if (size < blksz || zp->z_blksz != blksz) {
5100 ZFS_EXIT(zfsvfs);
5101 return (SET_ERROR(EINVAL));
5104 * Caller requests buffers for write before knowing where the
5105 * write offset might be (e.g. NFS TCP write).
5107 if (offset == -1) {
5108 preamble = 0;
5109 } else {
5110 preamble = P2PHASE(offset, blksz);
5111 if (preamble) {
5112 preamble = blksz - preamble;
5113 size -= preamble;
5117 postamble = P2PHASE(size, blksz);
5118 size -= postamble;
5120 fullblk = size / blksz;
5121 (void) dmu_xuio_init(xuio,
5122 (preamble != 0) + fullblk + (postamble != 0));
5123 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5124 int, postamble, int,
5125 (preamble != 0) + fullblk + (postamble != 0));
5128 * Have to fix iov base/len for partial buffers. They
5129 * currently represent full arc_buf's.
5131 if (preamble) {
5132 /* data begins in the middle of the arc_buf */
5133 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5134 blksz);
5135 ASSERT(abuf);
5136 (void) dmu_xuio_add(xuio, abuf,
5137 blksz - preamble, preamble);
5140 for (i = 0; i < fullblk; i++) {
5141 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5142 blksz);
5143 ASSERT(abuf);
5144 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5147 if (postamble) {
5148 /* data ends in the middle of the arc_buf */
5149 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5150 blksz);
5151 ASSERT(abuf);
5152 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5154 break;
5155 case UIO_READ:
5157 * Loan out an arc_buf for read if the read size is larger than
5158 * the current file block size. Block alignment is not
5159 * considered. Partial arc_buf will be loaned out for read.
5161 blksz = zp->z_blksz;
5162 if (blksz < zcr_blksz_min)
5163 blksz = zcr_blksz_min;
5164 if (blksz > zcr_blksz_max)
5165 blksz = zcr_blksz_max;
5166 /* avoid potential complexity of dealing with it */
5167 if (blksz > max_blksz) {
5168 ZFS_EXIT(zfsvfs);
5169 return (SET_ERROR(EINVAL));
5172 maxsize = zp->z_size - uio->uio_loffset;
5173 if (size > maxsize)
5174 size = maxsize;
5176 if (size < blksz || vn_has_cached_data(vp)) {
5177 ZFS_EXIT(zfsvfs);
5178 return (SET_ERROR(EINVAL));
5180 break;
5181 default:
5182 ZFS_EXIT(zfsvfs);
5183 return (SET_ERROR(EINVAL));
5186 uio->uio_extflg = UIO_XUIO;
5187 XUIO_XUZC_RW(xuio) = ioflag;
5188 ZFS_EXIT(zfsvfs);
5189 return (0);
5192 /*ARGSUSED*/
5193 static int
5194 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5196 int i;
5197 arc_buf_t *abuf;
5198 int ioflag = XUIO_XUZC_RW(xuio);
5200 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5202 i = dmu_xuio_cnt(xuio);
5203 while (i-- > 0) {
5204 abuf = dmu_xuio_arcbuf(xuio, i);
5206 * if abuf == NULL, it must be a write buffer
5207 * that has been returned in zfs_write().
5209 if (abuf)
5210 dmu_return_arcbuf(abuf);
5211 ASSERT(abuf || ioflag == UIO_WRITE);
5214 dmu_xuio_fini(xuio);
5215 return (0);
5219 * Predeclare these here so that the compiler assumes that
5220 * this is an "old style" function declaration that does
5221 * not include arguments => we won't get type mismatch errors
5222 * in the initializations that follow.
5224 static int zfs_inval();
5225 static int zfs_isdir();
5227 static int
5228 zfs_inval()
5230 return (SET_ERROR(EINVAL));
5233 static int
5234 zfs_isdir()
5236 return (SET_ERROR(EISDIR));
5239 * Directory vnode operations template
5241 vnodeops_t *zfs_dvnodeops;
5242 const fs_operation_def_t zfs_dvnodeops_template[] = {
5243 VOPNAME_OPEN, { .vop_open = zfs_open },
5244 VOPNAME_CLOSE, { .vop_close = zfs_close },
5245 VOPNAME_READ, { .error = zfs_isdir },
5246 VOPNAME_WRITE, { .error = zfs_isdir },
5247 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5248 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5249 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5250 VOPNAME_ACCESS, { .vop_access = zfs_access },
5251 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5252 VOPNAME_CREATE, { .vop_create = zfs_create },
5253 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5254 VOPNAME_LINK, { .vop_link = zfs_link },
5255 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5256 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5257 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5258 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5259 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5260 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5261 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5262 VOPNAME_FID, { .vop_fid = zfs_fid },
5263 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5264 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5265 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5266 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5267 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5268 NULL, NULL
5272 * Regular file vnode operations template
5274 vnodeops_t *zfs_fvnodeops;
5275 const fs_operation_def_t zfs_fvnodeops_template[] = {
5276 VOPNAME_OPEN, { .vop_open = zfs_open },
5277 VOPNAME_CLOSE, { .vop_close = zfs_close },
5278 VOPNAME_READ, { .vop_read = zfs_read },
5279 VOPNAME_WRITE, { .vop_write = zfs_write },
5280 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5281 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5282 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5283 VOPNAME_ACCESS, { .vop_access = zfs_access },
5284 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5285 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5286 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5287 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5288 VOPNAME_FID, { .vop_fid = zfs_fid },
5289 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5290 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5291 VOPNAME_SPACE, { .vop_space = zfs_space },
5292 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5293 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5294 VOPNAME_MAP, { .vop_map = zfs_map },
5295 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5296 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5297 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5298 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5299 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5300 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5301 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5302 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5303 NULL, NULL
5307 * Symbolic link vnode operations template
5309 vnodeops_t *zfs_symvnodeops;
5310 const fs_operation_def_t zfs_symvnodeops_template[] = {
5311 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5312 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5313 VOPNAME_ACCESS, { .vop_access = zfs_access },
5314 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5315 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5316 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5317 VOPNAME_FID, { .vop_fid = zfs_fid },
5318 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5319 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5320 NULL, NULL
5324 * special share hidden files vnode operations template
5326 vnodeops_t *zfs_sharevnodeops;
5327 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5328 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5329 VOPNAME_ACCESS, { .vop_access = zfs_access },
5330 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5331 VOPNAME_FID, { .vop_fid = zfs_fid },
5332 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5333 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5334 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5335 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5336 NULL, NULL
5340 * Extended attribute directory vnode operations template
5342 * This template is identical to the directory vnodes
5343 * operation template except for restricted operations:
5344 * VOP_MKDIR()
5345 * VOP_SYMLINK()
5347 * Note that there are other restrictions embedded in:
5348 * zfs_create() - restrict type to VREG
5349 * zfs_link() - no links into/out of attribute space
5350 * zfs_rename() - no moves into/out of attribute space
5352 vnodeops_t *zfs_xdvnodeops;
5353 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5354 VOPNAME_OPEN, { .vop_open = zfs_open },
5355 VOPNAME_CLOSE, { .vop_close = zfs_close },
5356 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5357 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5358 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5359 VOPNAME_ACCESS, { .vop_access = zfs_access },
5360 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5361 VOPNAME_CREATE, { .vop_create = zfs_create },
5362 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5363 VOPNAME_LINK, { .vop_link = zfs_link },
5364 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5365 VOPNAME_MKDIR, { .error = zfs_inval },
5366 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5367 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5368 VOPNAME_SYMLINK, { .error = zfs_inval },
5369 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5370 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5371 VOPNAME_FID, { .vop_fid = zfs_fid },
5372 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5373 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5374 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5375 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5376 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5377 NULL, NULL
5381 * Error vnode operations template
5383 vnodeops_t *zfs_evnodeops;
5384 const fs_operation_def_t zfs_evnodeops_template[] = {
5385 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5386 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5387 NULL, NULL