Unleashed v1.4
[unleashed.git] / kernel / fs / zfs / zfs_vnops.c
blobfd01a0277c9633a645c08f82f1967dc7bb47d32d
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/vnode.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/kmem.h>
44 #include <sys/taskq.h>
45 #include <sys/uio.h>
46 #include <sys/vmsystm.h>
47 #include <sys/atomic.h>
48 #include <sys/vm.h>
49 #include <vm/seg_vn.h>
50 #include <vm/pvn.h>
51 #include <vm/as.h>
52 #include <vm/kpm.h>
53 #include <vm/seg_kpm.h>
54 #include <sys/mman.h>
55 #include <sys/pathname.h>
56 #include <sys/cmn_err.h>
57 #include <sys/errno.h>
58 #include <sys/unistd.h>
59 #include <sys/zfs_dir.h>
60 #include <sys/zfs_acl.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/fs/zfs.h>
63 #include <sys/dmu.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/spa.h>
66 #include <sys/txg.h>
67 #include <sys/dbuf.h>
68 #include <sys/zap.h>
69 #include <sys/sa.h>
70 #include <sys/dirent.h>
71 #include <sys/policy.h>
72 #include <sys/sunddi.h>
73 #include <sys/filio.h>
74 #include <sys/sid.h>
75 #include "sys/fs_subr.h"
76 #include <sys/zfs_ctldir.h>
77 #include <sys/zfs_fuid.h>
78 #include <sys/zfs_sa.h>
79 #include <sys/dnlc.h>
80 #include <sys/zfs_rlock.h>
81 #include <sys/extdirent.h>
82 #include <sys/kidmap.h>
83 #include <sys/cred.h>
84 #include <sys/attr.h>
85 #include <sys/zil.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_NOTHROTTLE in addition to 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_NOTHROTTLE : 0) | 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->v_object, 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->v_object, 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 xuio_t *xuio = NULL;
517 ZFS_ENTER(zfsvfs);
518 ZFS_VERIFY_ZP(zp);
520 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
521 ZFS_EXIT(zfsvfs);
522 return (SET_ERROR(EACCES));
526 * Validate file offset
528 if (uio->uio_loffset < 0) {
529 ZFS_EXIT(zfsvfs);
530 return (SET_ERROR(EINVAL));
534 * Fasttrack empty reads
536 if (uio->uio_resid == 0) {
537 ZFS_EXIT(zfsvfs);
538 return (0);
542 * Check for mandatory locks
544 if (MANDMODE(zp->z_mode)) {
545 if (error = chklock(vp, FREAD,
546 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
547 ZFS_EXIT(zfsvfs);
548 return (error);
553 * If we're in FRSYNC mode, sync out this znode before reading it.
555 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
556 zil_commit(zfsvfs->z_log, zp->z_id);
559 * Lock the range against changes.
561 locked_range_t *lr = rangelock_enter(&zp->z_rangelock,
562 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 rangelock_exit(lr);
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 rlim_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 int max_blksz = zfsvfs->z_max_blksz;
666 int error = 0;
667 arc_buf_t *abuf;
668 iovec_t *aiov = NULL;
669 xuio_t *xuio = NULL;
670 int i_iov = 0;
671 int iovcnt = uio->uio_iovcnt;
672 iovec_t *iovp = uio->uio_iov;
673 int write_eof;
674 int count = 0;
675 sa_bulk_attr_t bulk[4];
676 uint64_t mtime[2], ctime[2];
679 * Fasttrack empty write
681 n = start_resid;
682 if (n == 0)
683 return (0);
685 if (limit == RLIM_INFINITY || limit > MAXOFFSET_T)
686 limit = MAXOFFSET_T;
688 ZFS_ENTER(zfsvfs);
689 ZFS_VERIFY_ZP(zp);
691 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
692 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
693 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
694 &zp->z_size, 8);
695 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
696 &zp->z_pflags, 8);
699 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
700 * callers might not be able to detect properly that we are read-only,
701 * so check it explicitly here.
703 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
704 ZFS_EXIT(zfsvfs);
705 return (SET_ERROR(EROFS));
709 * If immutable or not appending then return EPERM.
710 * Intentionally allow ZFS_READONLY through here.
711 * See zfs_zaccess_common()
713 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
714 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
715 (uio->uio_loffset < zp->z_size))) {
716 ZFS_EXIT(zfsvfs);
717 return (SET_ERROR(EPERM));
720 zilog = zfsvfs->z_log;
723 * Validate file offset
725 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
726 if (woff < 0) {
727 ZFS_EXIT(zfsvfs);
728 return (SET_ERROR(EINVAL));
732 * Check for mandatory locks before calling rangelock_enter()
733 * in order to prevent a deadlock with locks set via fcntl().
735 if (MANDMODE((mode_t)zp->z_mode) &&
736 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
737 ZFS_EXIT(zfsvfs);
738 return (error);
742 * Pre-fault the pages to ensure slow (eg NFS) pages
743 * don't hold up txg.
744 * Skip this if uio contains loaned arc_buf.
746 if ((uio->uio_extflg == UIO_XUIO) &&
747 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
748 xuio = (xuio_t *)uio;
749 else
750 uio_prefaultpages(MIN(n, max_blksz), uio);
753 * If in append mode, set the io offset pointer to eof.
755 locked_range_t *lr;
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 lr = rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND);
762 woff = lr->lr_offset;
763 if (lr->lr_length == 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 lr = rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER);
781 if (woff >= limit) {
782 rangelock_exit(lr);
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 rangelock_enter() over-locked we grow the blocksize
864 * and then reduce the lock range. This will only happen
865 * on the first iteration since rangelock_reduce() will
866 * shrink down lr_length to the appropriate size.
868 if (lr->lr_length == 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 rangelock_reduce(lr, 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 rangelock_exit(lr);
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 /* ARGSUSED */
1018 void
1019 zfs_get_done(zgd_t *zgd, int error)
1021 znode_t *zp = zgd->zgd_private;
1022 objset_t *os = zp->z_zfsvfs->z_os;
1024 if (zgd->zgd_db)
1025 dmu_buf_rele(zgd->zgd_db, zgd);
1027 rangelock_exit(zgd->zgd_lr);
1030 * Release the vnode asynchronously as we currently have the
1031 * txg stopped from syncing.
1033 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1035 kmem_free(zgd, sizeof (zgd_t));
1038 #ifdef DEBUG
1039 static int zil_fault_io = 0;
1040 #endif
1043 * Get data to generate a TX_WRITE intent log record.
1046 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1048 zfsvfs_t *zfsvfs = arg;
1049 objset_t *os = zfsvfs->z_os;
1050 znode_t *zp;
1051 uint64_t object = lr->lr_foid;
1052 uint64_t offset = lr->lr_offset;
1053 uint64_t size = lr->lr_length;
1054 dmu_buf_t *db;
1055 zgd_t *zgd;
1056 int error = 0;
1058 ASSERT3P(lwb, !=, NULL);
1059 ASSERT3P(zio, !=, NULL);
1060 ASSERT3U(size, !=, 0);
1063 * Nothing to do if the file has been removed
1065 if (zfs_zget(zfsvfs, object, &zp) != 0)
1066 return (SET_ERROR(ENOENT));
1067 if (zp->z_unlinked) {
1069 * Release the vnode asynchronously as we currently have the
1070 * txg stopped from syncing.
1072 VN_RELE_ASYNC(ZTOV(zp),
1073 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1074 return (SET_ERROR(ENOENT));
1077 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1078 zgd->zgd_lwb = lwb;
1079 zgd->zgd_private = zp;
1082 * Write records come in two flavors: immediate and indirect.
1083 * For small writes it's cheaper to store the data with the
1084 * log record (immediate); for large writes it's cheaper to
1085 * sync the data and get a pointer to it (indirect) so that
1086 * we don't have to write the data twice.
1088 if (buf != NULL) { /* immediate write */
1089 zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1090 offset, size, RL_READER);
1091 /* test for truncation needs to be done while range locked */
1092 if (offset >= zp->z_size) {
1093 error = SET_ERROR(ENOENT);
1094 } else {
1095 error = dmu_read(os, object, offset, size, buf,
1096 DMU_READ_NO_PREFETCH);
1098 ASSERT(error == 0 || error == ENOENT);
1099 } else { /* indirect write */
1101 * Have to lock the whole block to ensure when it's
1102 * written out and its checksum is being calculated
1103 * that no one can change the data. We need to re-check
1104 * blocksize after we get the lock in case it's changed!
1106 for (;;) {
1107 uint64_t blkoff;
1108 size = zp->z_blksz;
1109 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1110 offset -= blkoff;
1111 zgd->zgd_lr = rangelock_enter(&zp->z_rangelock,
1112 offset, size, RL_READER);
1113 if (zp->z_blksz == size)
1114 break;
1115 offset += blkoff;
1116 rangelock_exit(zgd->zgd_lr);
1118 /* test for truncation needs to be done while range locked */
1119 if (lr->lr_offset >= zp->z_size)
1120 error = SET_ERROR(ENOENT);
1121 #ifdef DEBUG
1122 if (zil_fault_io) {
1123 error = SET_ERROR(EIO);
1124 zil_fault_io = 0;
1126 #endif
1127 if (error == 0)
1128 error = dmu_buf_hold(os, object, offset, zgd, &db,
1129 DMU_READ_NO_PREFETCH);
1131 if (error == 0) {
1132 blkptr_t *bp = &lr->lr_blkptr;
1134 zgd->zgd_db = db;
1135 zgd->zgd_bp = bp;
1137 ASSERT(db->db_offset == offset);
1138 ASSERT(db->db_size == size);
1140 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1141 zfs_get_done, zgd);
1142 ASSERT(error || lr->lr_length <= size);
1145 * On success, we need to wait for the write I/O
1146 * initiated by dmu_sync() to complete before we can
1147 * release this dbuf. We will finish everything up
1148 * in the zfs_get_done() callback.
1150 if (error == 0)
1151 return (0);
1153 if (error == EALREADY) {
1154 lr->lr_common.lrc_txtype = TX_WRITE2;
1156 * TX_WRITE2 relies on the data previously
1157 * written by the TX_WRITE that caused
1158 * EALREADY. We zero out the BP because
1159 * it is the old, currently-on-disk BP.
1161 zgd->zgd_bp = NULL;
1162 BP_ZERO(bp);
1163 error = 0;
1168 zfs_get_done(zgd, error);
1170 return (error);
1173 /*ARGSUSED*/
1174 static int
1175 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1176 caller_context_t *ct)
1178 znode_t *zp = VTOZ(vp);
1179 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1180 int error;
1182 ZFS_ENTER(zfsvfs);
1183 ZFS_VERIFY_ZP(zp);
1185 if (flag & V_ACE_MASK)
1186 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1187 else
1188 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1190 ZFS_EXIT(zfsvfs);
1191 return (error);
1195 * If vnode is for a device return a specfs vnode instead.
1197 static int
1198 specvp_check(vnode_t **vpp, cred_t *cr)
1200 int error = 0;
1202 if (IS_DEVVP(*vpp)) {
1203 struct vnode *svp;
1205 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1206 VN_RELE(*vpp);
1207 if (svp == NULL)
1208 error = SET_ERROR(ENOSYS);
1209 *vpp = svp;
1211 return (error);
1216 * Lookup an entry in a directory, or an extended attribute directory.
1217 * If it exists, return a held vnode reference for it.
1219 * IN: dvp - vnode of directory to search.
1220 * nm - name of entry to lookup.
1221 * pnp - full pathname to lookup [UNUSED].
1222 * flags - LOOKUP_XATTR set if looking for an attribute.
1223 * rdir - root directory vnode [UNUSED].
1224 * cr - credentials of caller.
1225 * ct - caller context
1226 * direntflags - directory lookup flags
1227 * realpnp - returned pathname.
1229 * OUT: vpp - vnode of located entry, NULL if not found.
1231 * RETURN: 0 on success, error code on failure.
1233 * Timestamps:
1234 * NA
1236 /* ARGSUSED */
1237 static int
1238 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1239 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1240 int *direntflags, pathname_t *realpnp)
1242 znode_t *zdp = VTOZ(dvp);
1243 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1244 int error = 0;
1247 * Fast path lookup, however we must skip DNLC lookup
1248 * for case folding or normalizing lookups because the
1249 * DNLC code only stores the passed in name. This means
1250 * creating 'a' and removing 'A' on a case insensitive
1251 * file system would work, but DNLC still thinks 'a'
1252 * exists and won't let you create it again on the next
1253 * pass through fast path.
1255 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1257 if (dvp->v_type != VDIR) {
1258 return (SET_ERROR(ENOTDIR));
1259 } else if (zdp->z_sa_hdl == NULL) {
1260 return (SET_ERROR(EIO));
1263 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1264 error = zfs_fastaccesschk_execute(zdp, cr);
1265 if (!error) {
1266 *vpp = dvp;
1267 VN_HOLD(*vpp);
1268 return (0);
1270 return (error);
1271 } else if (!zdp->z_zfsvfs->z_norm &&
1272 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1274 vnode_t *tvp = dnlc_lookup(dvp, nm);
1276 if (tvp) {
1277 error = zfs_fastaccesschk_execute(zdp, cr);
1278 if (error) {
1279 VN_RELE(tvp);
1280 return (error);
1282 if (tvp == DNLC_NO_VNODE) {
1283 VN_RELE(tvp);
1284 return (SET_ERROR(ENOENT));
1285 } else {
1286 *vpp = tvp;
1287 return (specvp_check(vpp, cr));
1293 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1295 ZFS_ENTER(zfsvfs);
1296 ZFS_VERIFY_ZP(zdp);
1298 *vpp = NULL;
1300 if (flags & LOOKUP_XATTR) {
1302 * If the xattr property is off, refuse the lookup request.
1304 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1305 ZFS_EXIT(zfsvfs);
1306 return (SET_ERROR(EINVAL));
1310 * We don't allow recursive attributes..
1311 * Maybe someday we will.
1313 if (zdp->z_pflags & ZFS_XATTR) {
1314 ZFS_EXIT(zfsvfs);
1315 return (SET_ERROR(EINVAL));
1318 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1319 ZFS_EXIT(zfsvfs);
1320 return (error);
1324 * Do we have permission to get into attribute directory?
1327 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1328 B_FALSE, cr)) {
1329 VN_RELE(*vpp);
1330 *vpp = NULL;
1333 ZFS_EXIT(zfsvfs);
1334 return (error);
1337 if (dvp->v_type != VDIR) {
1338 ZFS_EXIT(zfsvfs);
1339 return (SET_ERROR(ENOTDIR));
1343 * Check accessibility of directory.
1346 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1347 ZFS_EXIT(zfsvfs);
1348 return (error);
1351 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1352 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1353 ZFS_EXIT(zfsvfs);
1354 return (SET_ERROR(EILSEQ));
1357 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1358 if (error == 0)
1359 error = specvp_check(vpp, cr);
1361 ZFS_EXIT(zfsvfs);
1362 return (error);
1366 * Attempt to create a new entry in a directory. If the entry
1367 * already exists, truncate the file if permissible, else return
1368 * an error. Return the vp of the created or trunc'd file.
1370 * IN: dvp - vnode of directory to put new file entry in.
1371 * name - name of new file entry.
1372 * vap - attributes of new file.
1373 * excl - flag indicating exclusive or non-exclusive mode.
1374 * mode - mode to open file with.
1375 * cr - credentials of caller.
1376 * flag - large file flag [UNUSED].
1377 * ct - caller context
1378 * vsecp - ACL to be set
1380 * OUT: vpp - vnode of created or trunc'd entry.
1382 * RETURN: 0 on success, error code on failure.
1384 * Timestamps:
1385 * dvp - ctime|mtime updated if new entry created
1386 * vp - ctime|mtime always, atime if new
1389 /* ARGSUSED */
1390 static int
1391 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1392 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1393 vsecattr_t *vsecp)
1395 znode_t *zp, *dzp = VTOZ(dvp);
1396 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1397 zilog_t *zilog;
1398 objset_t *os;
1399 zfs_dirlock_t *dl;
1400 dmu_tx_t *tx;
1401 int error;
1402 ksid_t *ksid;
1403 uid_t uid;
1404 gid_t gid = crgetgid(cr);
1405 zfs_acl_ids_t acl_ids;
1406 boolean_t fuid_dirtied;
1407 boolean_t have_acl = B_FALSE;
1408 boolean_t waited = B_FALSE;
1411 * If we have an ephemeral id, ACL, or XVATTR then
1412 * make sure file system is at proper version
1415 ksid = crgetsid(cr, KSID_OWNER);
1416 if (ksid)
1417 uid = ksid_getid(ksid);
1418 else
1419 uid = crgetuid(cr);
1421 if (zfsvfs->z_use_fuids == B_FALSE &&
1422 (vsecp || (vap->va_mask & VATTR_XVATTR) ||
1423 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1424 return (SET_ERROR(EINVAL));
1426 ZFS_ENTER(zfsvfs);
1427 ZFS_VERIFY_ZP(dzp);
1428 os = zfsvfs->z_os;
1429 zilog = zfsvfs->z_log;
1431 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1432 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1433 ZFS_EXIT(zfsvfs);
1434 return (SET_ERROR(EILSEQ));
1437 if (vap->va_mask & VATTR_XVATTR) {
1438 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1439 crgetuid(cr), cr, vap->va_type)) != 0) {
1440 ZFS_EXIT(zfsvfs);
1441 return (error);
1444 top:
1445 *vpp = NULL;
1447 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1448 vap->va_mode &= ~VSVTX;
1450 if (*name == '\0') {
1452 * Null component name refers to the directory itself.
1454 VN_HOLD(dvp);
1455 zp = dzp;
1456 dl = NULL;
1457 error = 0;
1458 } else {
1459 /* possible VN_HOLD(zp) */
1460 int zflg = 0;
1462 if (flag & FIGNORECASE)
1463 zflg |= ZCILOOK;
1465 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1466 NULL, NULL);
1467 if (error) {
1468 if (have_acl)
1469 zfs_acl_ids_free(&acl_ids);
1470 if (strcmp(name, "..") == 0)
1471 error = SET_ERROR(EISDIR);
1472 ZFS_EXIT(zfsvfs);
1473 return (error);
1477 if (zp == NULL) {
1478 uint64_t txtype;
1481 * Create a new file object and update the directory
1482 * to reference it.
1484 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1485 if (have_acl)
1486 zfs_acl_ids_free(&acl_ids);
1487 goto out;
1491 * We only support the creation of regular files in
1492 * extended attribute directories.
1495 if ((dzp->z_pflags & ZFS_XATTR) &&
1496 (vap->va_type != VREG)) {
1497 if (have_acl)
1498 zfs_acl_ids_free(&acl_ids);
1499 error = SET_ERROR(EINVAL);
1500 goto out;
1503 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1504 cr, vsecp, &acl_ids)) != 0)
1505 goto out;
1506 have_acl = B_TRUE;
1508 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1509 zfs_acl_ids_free(&acl_ids);
1510 error = SET_ERROR(EDQUOT);
1511 goto out;
1514 tx = dmu_tx_create(os);
1516 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1517 ZFS_SA_BASE_ATTR_SIZE);
1519 fuid_dirtied = zfsvfs->z_fuid_dirty;
1520 if (fuid_dirtied)
1521 zfs_fuid_txhold(zfsvfs, tx);
1522 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1523 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1524 if (!zfsvfs->z_use_sa &&
1525 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1526 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1527 0, acl_ids.z_aclp->z_acl_bytes);
1529 error = dmu_tx_assign(tx,
1530 (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1531 if (error) {
1532 zfs_dirent_unlock(dl);
1533 if (error == ERESTART) {
1534 waited = B_TRUE;
1535 dmu_tx_wait(tx);
1536 dmu_tx_abort(tx);
1537 goto top;
1539 zfs_acl_ids_free(&acl_ids);
1540 dmu_tx_abort(tx);
1541 ZFS_EXIT(zfsvfs);
1542 return (error);
1544 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1546 if (fuid_dirtied)
1547 zfs_fuid_sync(zfsvfs, tx);
1549 (void) zfs_link_create(dl, zp, tx, ZNEW);
1550 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1551 if (flag & FIGNORECASE)
1552 txtype |= TX_CI;
1553 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1554 vsecp, acl_ids.z_fuidp, vap);
1555 zfs_acl_ids_free(&acl_ids);
1556 dmu_tx_commit(tx);
1557 } else {
1558 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1560 if (have_acl)
1561 zfs_acl_ids_free(&acl_ids);
1562 have_acl = B_FALSE;
1565 * A directory entry already exists for this name.
1568 * Can't truncate an existing file if in exclusive mode.
1570 if (excl == EXCL) {
1571 error = SET_ERROR(EEXIST);
1572 goto out;
1575 * Can't open a directory for writing.
1577 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1578 error = SET_ERROR(EISDIR);
1579 goto out;
1582 * Verify requested access to file.
1584 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1585 goto out;
1588 mutex_enter(&dzp->z_lock);
1589 dzp->z_seq++;
1590 mutex_exit(&dzp->z_lock);
1593 * Truncate regular files if requested.
1595 if ((ZTOV(zp)->v_type == VREG) &&
1596 (vap->va_mask & VATTR_SIZE) && (vap->va_size == 0)) {
1597 /* we can't hold any locks when calling zfs_freesp() */
1598 zfs_dirent_unlock(dl);
1599 dl = NULL;
1600 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1601 if (error == 0) {
1602 vnevent_create(ZTOV(zp), ct);
1606 out:
1608 if (dl)
1609 zfs_dirent_unlock(dl);
1611 if (error) {
1612 if (zp)
1613 VN_RELE(ZTOV(zp));
1614 } else {
1615 *vpp = ZTOV(zp);
1616 error = specvp_check(vpp, cr);
1619 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1620 zil_commit(zilog, 0);
1622 ZFS_EXIT(zfsvfs);
1623 return (error);
1627 * Remove an entry from a directory.
1629 * IN: dvp - vnode of directory to remove entry from.
1630 * name - name of entry to remove.
1631 * cr - credentials of caller.
1632 * ct - caller context
1633 * flags - case flags
1635 * RETURN: 0 on success, error code on failure.
1637 * Timestamps:
1638 * dvp - ctime|mtime
1639 * vp - ctime (if nlink > 0)
1642 uint64_t null_xattr = 0;
1644 /*ARGSUSED*/
1645 static int
1646 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1647 int flags)
1649 znode_t *zp, *dzp = VTOZ(dvp);
1650 znode_t *xzp;
1651 vnode_t *vp;
1652 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1653 zilog_t *zilog;
1654 uint64_t acl_obj, xattr_obj;
1655 uint64_t xattr_obj_unlinked = 0;
1656 uint64_t obj = 0;
1657 zfs_dirlock_t *dl;
1658 dmu_tx_t *tx;
1659 boolean_t may_delete_now, delete_now = FALSE;
1660 boolean_t unlinked, toobig = FALSE;
1661 uint64_t txtype;
1662 pathname_t *realnmp = NULL;
1663 pathname_t realnm;
1664 int error;
1665 int zflg = ZEXISTS;
1666 boolean_t waited = B_FALSE;
1668 ZFS_ENTER(zfsvfs);
1669 ZFS_VERIFY_ZP(dzp);
1670 zilog = zfsvfs->z_log;
1672 if (flags & FIGNORECASE) {
1673 zflg |= ZCILOOK;
1674 pn_alloc(&realnm);
1675 realnmp = &realnm;
1678 top:
1679 xattr_obj = 0;
1680 xzp = NULL;
1682 * Attempt to lock directory; fail if entry doesn't exist.
1684 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1685 NULL, realnmp)) {
1686 if (realnmp)
1687 pn_free(realnmp);
1688 ZFS_EXIT(zfsvfs);
1689 return (error);
1692 vp = ZTOV(zp);
1694 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1695 goto out;
1699 * Need to use rmdir for removing directories.
1701 if (vp->v_type == VDIR) {
1702 error = SET_ERROR(EPERM);
1703 goto out;
1706 vnevent_remove(vp, dvp, name, ct);
1708 if (realnmp)
1709 dnlc_remove(dvp, realnmp->pn_buf);
1710 else
1711 dnlc_remove(dvp, name);
1713 mutex_enter(&vp->v_lock);
1714 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1715 mutex_exit(&vp->v_lock);
1718 * We may delete the znode now, or we may put it in the unlinked set;
1719 * it depends on whether we're the last link, and on whether there are
1720 * other holds on the vnode. So we dmu_tx_hold() the right things to
1721 * allow for either case.
1723 obj = zp->z_id;
1724 tx = dmu_tx_create(zfsvfs->z_os);
1725 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1726 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1727 zfs_sa_upgrade_txholds(tx, zp);
1728 zfs_sa_upgrade_txholds(tx, dzp);
1729 if (may_delete_now) {
1730 toobig =
1731 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1732 /* if the file is too big, only hold_free a token amount */
1733 dmu_tx_hold_free(tx, zp->z_id, 0,
1734 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1737 /* are there any extended attributes? */
1738 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1739 &xattr_obj, sizeof (xattr_obj));
1740 if (error == 0 && xattr_obj) {
1741 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1742 ASSERT0(error);
1743 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1744 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1747 mutex_enter(&zp->z_lock);
1748 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1749 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1750 mutex_exit(&zp->z_lock);
1752 /* charge as an update -- would be nice not to charge at all */
1753 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1756 * Mark this transaction as typically resulting in a net free of space
1758 dmu_tx_mark_netfree(tx);
1760 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1761 if (error) {
1762 zfs_dirent_unlock(dl);
1763 VN_RELE(vp);
1764 if (xzp)
1765 VN_RELE(ZTOV(xzp));
1766 if (error == ERESTART) {
1767 waited = B_TRUE;
1768 dmu_tx_wait(tx);
1769 dmu_tx_abort(tx);
1770 goto top;
1772 if (realnmp)
1773 pn_free(realnmp);
1774 dmu_tx_abort(tx);
1775 ZFS_EXIT(zfsvfs);
1776 return (error);
1780 * Remove the directory entry.
1782 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1784 if (error) {
1785 dmu_tx_commit(tx);
1786 goto out;
1789 if (unlinked) {
1791 * Hold z_lock so that we can make sure that the ACL obj
1792 * hasn't changed. Could have been deleted due to
1793 * zfs_sa_upgrade().
1795 mutex_enter(&zp->z_lock);
1796 mutex_enter(&vp->v_lock);
1797 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1798 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1799 delete_now = may_delete_now && !toobig &&
1800 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1801 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1802 acl_obj;
1803 mutex_exit(&vp->v_lock);
1806 if (delete_now) {
1807 if (xattr_obj_unlinked) {
1808 ASSERT3U(xzp->z_links, ==, 2);
1809 mutex_enter(&xzp->z_lock);
1810 xzp->z_unlinked = 1;
1811 xzp->z_links = 0;
1812 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1813 &xzp->z_links, sizeof (xzp->z_links), tx);
1814 ASSERT3U(error, ==, 0);
1815 mutex_exit(&xzp->z_lock);
1816 zfs_unlinked_add(xzp, tx);
1818 if (zp->z_is_sa)
1819 error = sa_remove(zp->z_sa_hdl,
1820 SA_ZPL_XATTR(zfsvfs), tx);
1821 else
1822 error = sa_update(zp->z_sa_hdl,
1823 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1824 sizeof (uint64_t), tx);
1825 ASSERT0(error);
1827 mutex_enter(&vp->v_lock);
1828 VN_RELE_LOCKED(vp);
1829 ASSERT0(vp->v_count);
1830 mutex_exit(&vp->v_lock);
1831 mutex_exit(&zp->z_lock);
1832 zfs_znode_delete(zp, tx);
1833 } else if (unlinked) {
1834 mutex_exit(&zp->z_lock);
1835 zfs_unlinked_add(zp, tx);
1838 txtype = TX_REMOVE;
1839 if (flags & FIGNORECASE)
1840 txtype |= TX_CI;
1841 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1843 dmu_tx_commit(tx);
1844 out:
1845 if (realnmp)
1846 pn_free(realnmp);
1848 zfs_dirent_unlock(dl);
1850 if (!delete_now)
1851 VN_RELE(vp);
1852 if (xzp)
1853 VN_RELE(ZTOV(xzp));
1855 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1856 zil_commit(zilog, 0);
1858 ZFS_EXIT(zfsvfs);
1859 return (error);
1863 * Create a new directory and insert it into dvp using the name
1864 * provided. Return a pointer to the inserted directory.
1866 * IN: dvp - vnode of directory to add subdir to.
1867 * dirname - name of new directory.
1868 * vap - attributes of new directory.
1869 * cr - credentials of caller.
1870 * ct - caller context
1871 * flags - case flags
1872 * vsecp - ACL to be set
1874 * OUT: vpp - vnode of created directory.
1876 * RETURN: 0 on success, error code on failure.
1878 * Timestamps:
1879 * dvp - ctime|mtime updated
1880 * vp - ctime|mtime|atime updated
1882 /*ARGSUSED*/
1883 static int
1884 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1885 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1887 znode_t *zp, *dzp = VTOZ(dvp);
1888 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1889 zilog_t *zilog;
1890 zfs_dirlock_t *dl;
1891 uint64_t txtype;
1892 dmu_tx_t *tx;
1893 int error;
1894 int zf = ZNEW;
1895 ksid_t *ksid;
1896 uid_t uid;
1897 gid_t gid = crgetgid(cr);
1898 zfs_acl_ids_t acl_ids;
1899 boolean_t fuid_dirtied;
1900 boolean_t waited = B_FALSE;
1902 ASSERT(vap->va_type == VDIR);
1905 * If we have an ephemeral id, ACL, or XVATTR then
1906 * make sure file system is at proper version
1909 ksid = crgetsid(cr, KSID_OWNER);
1910 if (ksid)
1911 uid = ksid_getid(ksid);
1912 else
1913 uid = crgetuid(cr);
1914 if (zfsvfs->z_use_fuids == B_FALSE &&
1915 (vsecp || (vap->va_mask & VATTR_XVATTR) ||
1916 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1917 return (SET_ERROR(EINVAL));
1919 ZFS_ENTER(zfsvfs);
1920 ZFS_VERIFY_ZP(dzp);
1921 zilog = zfsvfs->z_log;
1923 if (dzp->z_pflags & ZFS_XATTR) {
1924 ZFS_EXIT(zfsvfs);
1925 return (SET_ERROR(EINVAL));
1928 if (zfsvfs->z_utf8 && u8_validate(dirname,
1929 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1930 ZFS_EXIT(zfsvfs);
1931 return (SET_ERROR(EILSEQ));
1933 if (flags & FIGNORECASE)
1934 zf |= ZCILOOK;
1936 if (vap->va_mask & VATTR_XVATTR) {
1937 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1938 crgetuid(cr), cr, vap->va_type)) != 0) {
1939 ZFS_EXIT(zfsvfs);
1940 return (error);
1944 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1945 vsecp, &acl_ids)) != 0) {
1946 ZFS_EXIT(zfsvfs);
1947 return (error);
1950 * First make sure the new directory doesn't exist.
1952 * Existence is checked first to make sure we don't return
1953 * EACCES instead of EEXIST which can cause some applications
1954 * to fail.
1956 top:
1957 *vpp = NULL;
1959 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1960 NULL, NULL)) {
1961 zfs_acl_ids_free(&acl_ids);
1962 ZFS_EXIT(zfsvfs);
1963 return (error);
1966 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1967 zfs_acl_ids_free(&acl_ids);
1968 zfs_dirent_unlock(dl);
1969 ZFS_EXIT(zfsvfs);
1970 return (error);
1973 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1974 zfs_acl_ids_free(&acl_ids);
1975 zfs_dirent_unlock(dl);
1976 ZFS_EXIT(zfsvfs);
1977 return (SET_ERROR(EDQUOT));
1981 * Add a new entry to the directory.
1983 tx = dmu_tx_create(zfsvfs->z_os);
1984 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1985 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1986 fuid_dirtied = zfsvfs->z_fuid_dirty;
1987 if (fuid_dirtied)
1988 zfs_fuid_txhold(zfsvfs, tx);
1989 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1990 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1991 acl_ids.z_aclp->z_acl_bytes);
1994 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1995 ZFS_SA_BASE_ATTR_SIZE);
1997 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
1998 if (error) {
1999 zfs_dirent_unlock(dl);
2000 if (error == ERESTART) {
2001 waited = B_TRUE;
2002 dmu_tx_wait(tx);
2003 dmu_tx_abort(tx);
2004 goto top;
2006 zfs_acl_ids_free(&acl_ids);
2007 dmu_tx_abort(tx);
2008 ZFS_EXIT(zfsvfs);
2009 return (error);
2013 * Create new node.
2015 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2017 if (fuid_dirtied)
2018 zfs_fuid_sync(zfsvfs, tx);
2021 * Now put new name in parent dir.
2023 (void) zfs_link_create(dl, zp, tx, ZNEW);
2025 *vpp = ZTOV(zp);
2027 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2028 if (flags & FIGNORECASE)
2029 txtype |= TX_CI;
2030 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2031 acl_ids.z_fuidp, vap);
2033 zfs_acl_ids_free(&acl_ids);
2035 dmu_tx_commit(tx);
2037 zfs_dirent_unlock(dl);
2039 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2040 zil_commit(zilog, 0);
2042 ZFS_EXIT(zfsvfs);
2043 return (0);
2047 * Remove a directory subdir entry. If the current working
2048 * directory is the same as the subdir to be removed, the
2049 * remove will fail.
2051 * IN: dvp - vnode of directory to remove from.
2052 * name - name of directory to be removed.
2053 * cwd - vnode of current working directory.
2054 * cr - credentials of caller.
2055 * ct - caller context
2056 * flags - case flags
2058 * RETURN: 0 on success, error code on failure.
2060 * Timestamps:
2061 * dvp - ctime|mtime updated
2063 /*ARGSUSED*/
2064 static int
2065 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2066 caller_context_t *ct, int flags)
2068 znode_t *dzp = VTOZ(dvp);
2069 znode_t *zp;
2070 vnode_t *vp;
2071 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2072 zilog_t *zilog;
2073 zfs_dirlock_t *dl;
2074 dmu_tx_t *tx;
2075 int error;
2076 int zflg = ZEXISTS;
2077 boolean_t waited = B_FALSE;
2079 ZFS_ENTER(zfsvfs);
2080 ZFS_VERIFY_ZP(dzp);
2081 zilog = zfsvfs->z_log;
2083 if (flags & FIGNORECASE)
2084 zflg |= ZCILOOK;
2085 top:
2086 zp = NULL;
2089 * Attempt to lock directory; fail if entry doesn't exist.
2091 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2092 NULL, NULL)) {
2093 ZFS_EXIT(zfsvfs);
2094 return (error);
2097 vp = ZTOV(zp);
2099 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2100 goto out;
2103 if (vp->v_type != VDIR) {
2104 error = SET_ERROR(ENOTDIR);
2105 goto out;
2108 if (vp == cwd) {
2109 error = SET_ERROR(EINVAL);
2110 goto out;
2113 vnevent_rmdir(vp, dvp, name, ct);
2116 * Grab a lock on the directory to make sure that noone is
2117 * trying to add (or lookup) entries while we are removing it.
2119 rw_enter(&zp->z_name_lock, RW_WRITER);
2122 * Grab a lock on the parent pointer to make sure we play well
2123 * with the treewalk and directory rename code.
2125 rw_enter(&zp->z_parent_lock, RW_WRITER);
2127 tx = dmu_tx_create(zfsvfs->z_os);
2128 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2129 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2130 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2131 zfs_sa_upgrade_txholds(tx, zp);
2132 zfs_sa_upgrade_txholds(tx, dzp);
2133 dmu_tx_mark_netfree(tx);
2134 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
2135 if (error) {
2136 rw_exit(&zp->z_parent_lock);
2137 rw_exit(&zp->z_name_lock);
2138 zfs_dirent_unlock(dl);
2139 VN_RELE(vp);
2140 if (error == ERESTART) {
2141 waited = B_TRUE;
2142 dmu_tx_wait(tx);
2143 dmu_tx_abort(tx);
2144 goto top;
2146 dmu_tx_abort(tx);
2147 ZFS_EXIT(zfsvfs);
2148 return (error);
2151 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2153 if (error == 0) {
2154 uint64_t txtype = TX_RMDIR;
2155 if (flags & FIGNORECASE)
2156 txtype |= TX_CI;
2157 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2160 dmu_tx_commit(tx);
2162 rw_exit(&zp->z_parent_lock);
2163 rw_exit(&zp->z_name_lock);
2164 out:
2165 zfs_dirent_unlock(dl);
2167 VN_RELE(vp);
2169 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2170 zil_commit(zilog, 0);
2172 ZFS_EXIT(zfsvfs);
2173 return (error);
2177 * Read as many directory entries as will fit into the provided
2178 * buffer from the given directory cursor position (specified in
2179 * the uio structure).
2181 * IN: vp - vnode of directory to read.
2182 * uio - structure supplying read location, range info,
2183 * and return buffer.
2184 * cr - credentials of caller.
2185 * ct - caller context
2186 * flags - case flags
2188 * OUT: uio - updated offset and range, buffer filled.
2189 * eofp - set to true if end-of-file detected.
2191 * RETURN: 0 on success, error code on failure.
2193 * Timestamps:
2194 * vp - atime updated
2196 * Note that the low 4 bits of the cookie returned by zap is always zero.
2197 * This allows us to use the low range for "special" directory entries:
2198 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2199 * we use the offset 2 for the '.zfs' directory.
2201 /* ARGSUSED */
2202 static int
2203 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2204 caller_context_t *ct, int flags)
2206 znode_t *zp = VTOZ(vp);
2207 iovec_t *iovp;
2208 edirent_t *eodp;
2209 dirent_t *odp;
2210 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2211 objset_t *os;
2212 caddr_t outbuf;
2213 size_t bufsize;
2214 zap_cursor_t zc;
2215 zap_attribute_t zap;
2216 uint_t bytes_wanted;
2217 uint64_t offset; /* must be unsigned; checks for < 1 */
2218 uint64_t parent;
2219 int local_eof;
2220 int outcount;
2221 int error;
2222 uint8_t prefetch;
2223 boolean_t check_sysattrs;
2225 ZFS_ENTER(zfsvfs);
2226 ZFS_VERIFY_ZP(zp);
2228 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2229 &parent, sizeof (parent))) != 0) {
2230 ZFS_EXIT(zfsvfs);
2231 return (error);
2235 * If we are not given an eof variable,
2236 * use a local one.
2238 if (eofp == NULL)
2239 eofp = &local_eof;
2242 * Check for valid iov_len.
2244 if (uio->uio_iov->iov_len <= 0) {
2245 ZFS_EXIT(zfsvfs);
2246 return (SET_ERROR(EINVAL));
2250 * Quit if directory has been removed (posix)
2252 if ((*eofp = zp->z_unlinked) != 0) {
2253 ZFS_EXIT(zfsvfs);
2254 return (0);
2257 error = 0;
2258 os = zfsvfs->z_os;
2259 offset = uio->uio_loffset;
2260 prefetch = zp->z_zn_prefetch;
2263 * Initialize the iterator cursor.
2265 if (offset <= 3) {
2267 * Start iteration from the beginning of the directory.
2269 zap_cursor_init(&zc, os, zp->z_id);
2270 } else {
2272 * The offset is a serialized cursor.
2274 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2278 * Get space to change directory entries into fs independent format.
2280 iovp = uio->uio_iov;
2281 bytes_wanted = iovp->iov_len;
2282 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2283 bufsize = bytes_wanted;
2284 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2285 odp = (struct dirent *)outbuf;
2286 } else {
2287 bufsize = bytes_wanted;
2288 outbuf = NULL;
2289 odp = (struct dirent *)iovp->iov_base;
2291 eodp = (struct edirent *)odp;
2294 * If this VFS supports the system attribute view interface; and
2295 * we're looking at an extended attribute directory; and we care
2296 * about normalization conflicts on this vfs; then we must check
2297 * for normalization conflicts with the sysattr name space.
2299 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2300 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2301 (flags & V_RDDIR_ENTFLAGS);
2304 * Transform to file-system independent format
2306 outcount = 0;
2307 while (outcount < bytes_wanted) {
2308 ino64_t objnum;
2309 ushort_t reclen;
2310 off64_t *next = NULL;
2313 * Special case `.', `..', and `.zfs'.
2315 if (offset == 0) {
2316 (void) strcpy(zap.za_name, ".");
2317 zap.za_normalization_conflict = 0;
2318 objnum = zp->z_id;
2319 } else if (offset == 1) {
2320 (void) strcpy(zap.za_name, "..");
2321 zap.za_normalization_conflict = 0;
2322 objnum = parent;
2323 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2324 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2325 zap.za_normalization_conflict = 0;
2326 objnum = ZFSCTL_INO_ROOT;
2327 } else {
2329 * Grab next entry.
2331 if (error = zap_cursor_retrieve(&zc, &zap)) {
2332 if ((*eofp = (error == ENOENT)) != 0)
2333 break;
2334 else
2335 goto update;
2338 if (zap.za_integer_length != 8 ||
2339 zap.za_num_integers != 1) {
2340 cmn_err(CE_WARN, "zap_readdir: bad directory "
2341 "entry, obj = %lld, offset = %lld\n",
2342 (u_longlong_t)zp->z_id,
2343 (u_longlong_t)offset);
2344 error = SET_ERROR(ENXIO);
2345 goto update;
2348 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2350 * MacOS X can extract the object type here such as:
2351 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2354 if (check_sysattrs && !zap.za_normalization_conflict) {
2355 zap.za_normalization_conflict =
2356 xattr_sysattr_casechk(zap.za_name);
2360 if (flags & V_RDDIR_ACCFILTER) {
2362 * If we have no access at all, don't include
2363 * this entry in the returned information
2365 znode_t *ezp;
2366 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2367 goto skip_entry;
2368 if (!zfs_has_access(ezp, cr)) {
2369 VN_RELE(ZTOV(ezp));
2370 goto skip_entry;
2372 VN_RELE(ZTOV(ezp));
2375 if (flags & V_RDDIR_ENTFLAGS)
2376 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2377 else
2378 reclen = DIRENT_RECLEN(strlen(zap.za_name));
2381 * Will this entry fit in the buffer?
2383 if (outcount + reclen > bufsize) {
2385 * Did we manage to fit anything in the buffer?
2387 if (!outcount) {
2388 error = SET_ERROR(EINVAL);
2389 goto update;
2391 break;
2393 if (flags & V_RDDIR_ENTFLAGS) {
2395 * Add extended flag entry:
2397 eodp->ed_ino = objnum;
2398 eodp->ed_reclen = reclen;
2399 /* NOTE: ed_off is the offset for the *next* entry */
2400 next = &(eodp->ed_off);
2401 eodp->ed_eflags = zap.za_normalization_conflict ?
2402 ED_CASE_CONFLICT : 0;
2403 (void) strncpy(eodp->ed_name, zap.za_name,
2404 EDIRENT_NAMELEN(reclen));
2405 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2406 } else {
2408 * Add normal entry:
2410 odp->d_ino = objnum;
2411 odp->d_reclen = reclen;
2412 /* NOTE: d_off is the offset for the *next* entry */
2413 next = &(odp->d_off);
2414 (void) strncpy(odp->d_name, zap.za_name,
2415 DIRENT_NAMELEN(reclen));
2416 odp = (dirent_t *)((intptr_t)odp + reclen);
2418 outcount += reclen;
2420 ASSERT(outcount <= bufsize);
2422 /* Prefetch znode */
2423 if (prefetch)
2424 dmu_prefetch(os, objnum, 0, 0, 0,
2425 ZIO_PRIORITY_SYNC_READ);
2427 skip_entry:
2429 * Move to the next entry, fill in the previous offset.
2431 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2432 zap_cursor_advance(&zc);
2433 offset = zap_cursor_serialize(&zc);
2434 } else {
2435 offset += 1;
2437 if (next)
2438 *next = offset;
2440 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2442 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2443 iovp->iov_base += outcount;
2444 iovp->iov_len -= outcount;
2445 uio->uio_resid -= outcount;
2446 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2448 * Reset the pointer.
2450 offset = uio->uio_loffset;
2453 update:
2454 zap_cursor_fini(&zc);
2455 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2456 kmem_free(outbuf, bufsize);
2458 if (error == ENOENT)
2459 error = 0;
2461 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2463 uio->uio_loffset = offset;
2464 ZFS_EXIT(zfsvfs);
2465 return (error);
2468 ulong_t zfs_fsync_sync_cnt = 4;
2470 static int
2471 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2473 znode_t *zp = VTOZ(vp);
2474 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2477 * Regardless of whether this is required for standards conformance,
2478 * this is the logical behavior when fsync() is called on a file with
2479 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2480 * going to be pushed out as part of the zil_commit().
2482 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2483 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2484 (void) fop_putpage(vp, 0, (size_t)0, B_ASYNC, cr, ct);
2486 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2488 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2489 ZFS_ENTER(zfsvfs);
2490 ZFS_VERIFY_ZP(zp);
2491 zil_commit(zfsvfs->z_log, zp->z_id);
2492 ZFS_EXIT(zfsvfs);
2494 return (0);
2499 * Get the requested file attributes and place them in the provided
2500 * vattr structure.
2502 * IN: vp - vnode of file.
2503 * vap - va_mask identifies requested attributes.
2504 * If VATTR_XVATTR set, then optional attrs are requested
2505 * flags - ATTR_NOACLCHECK (CIFS server context)
2506 * cr - credentials of caller.
2507 * ct - caller context
2509 * OUT: vap - attribute values.
2511 * RETURN: 0 (always succeeds).
2513 /* ARGSUSED */
2514 static int
2515 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2516 caller_context_t *ct)
2518 znode_t *zp = VTOZ(vp);
2519 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2520 int error = 0;
2521 uint64_t links;
2522 uint64_t mtime[2], ctime[2];
2523 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2524 xoptattr_t *xoap = NULL;
2525 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2526 sa_bulk_attr_t bulk[2];
2527 int count = 0;
2529 ZFS_ENTER(zfsvfs);
2530 ZFS_VERIFY_ZP(zp);
2532 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2534 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2535 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2537 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2538 ZFS_EXIT(zfsvfs);
2539 return (error);
2543 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2544 * Also, if we are the owner don't bother, since owner should
2545 * always be allowed to read basic attributes of file.
2547 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2548 (vap->va_uid != crgetuid(cr))) {
2549 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2550 skipaclchk, cr)) {
2551 ZFS_EXIT(zfsvfs);
2552 return (error);
2557 * Return all attributes. It's cheaper to provide the answer
2558 * than to determine whether we were asked the question.
2561 mutex_enter(&zp->z_lock);
2562 vap->va_type = vp->v_type;
2563 vap->va_mode = zp->z_mode & MODEMASK;
2564 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2565 vap->va_nodeid = zp->z_id;
2566 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2567 links = zp->z_links + 1;
2568 else
2569 links = zp->z_links;
2570 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2571 vap->va_size = zp->z_size;
2572 vap->va_rdev = vp->v_rdev;
2573 vap->va_seq = zp->z_seq;
2576 * Add in any requested optional attributes and the create time.
2577 * Also set the corresponding bits in the returned attribute bitmap.
2579 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2580 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2581 xoap->xoa_archive =
2582 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2583 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2586 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2587 xoap->xoa_readonly =
2588 ((zp->z_pflags & ZFS_READONLY) != 0);
2589 XVA_SET_RTN(xvap, XAT_READONLY);
2592 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2593 xoap->xoa_system =
2594 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2595 XVA_SET_RTN(xvap, XAT_SYSTEM);
2598 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2599 xoap->xoa_hidden =
2600 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2601 XVA_SET_RTN(xvap, XAT_HIDDEN);
2604 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2605 xoap->xoa_nounlink =
2606 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2607 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2610 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2611 xoap->xoa_immutable =
2612 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2613 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2616 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2617 xoap->xoa_appendonly =
2618 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2619 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2622 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2623 xoap->xoa_nodump =
2624 ((zp->z_pflags & ZFS_NODUMP) != 0);
2625 XVA_SET_RTN(xvap, XAT_NODUMP);
2628 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2629 xoap->xoa_opaque =
2630 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2631 XVA_SET_RTN(xvap, XAT_OPAQUE);
2634 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2635 xoap->xoa_av_quarantined =
2636 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2637 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2640 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2641 xoap->xoa_av_modified =
2642 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2643 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2646 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2647 vp->v_type == VREG) {
2648 zfs_sa_get_scanstamp(zp, xvap);
2651 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2652 uint64_t times[2];
2654 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2655 times, sizeof (times));
2656 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2657 XVA_SET_RTN(xvap, XAT_CREATETIME);
2660 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2661 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2662 XVA_SET_RTN(xvap, XAT_REPARSE);
2664 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2665 xoap->xoa_generation = zp->z_gen;
2666 XVA_SET_RTN(xvap, XAT_GEN);
2669 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2670 xoap->xoa_offline =
2671 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2672 XVA_SET_RTN(xvap, XAT_OFFLINE);
2675 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2676 xoap->xoa_sparse =
2677 ((zp->z_pflags & ZFS_SPARSE) != 0);
2678 XVA_SET_RTN(xvap, XAT_SPARSE);
2682 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2683 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2684 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2686 mutex_exit(&zp->z_lock);
2688 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2690 if (zp->z_blksz == 0) {
2692 * Block size hasn't been set; suggest maximal I/O transfers.
2694 vap->va_blksize = zfsvfs->z_max_blksz;
2697 ZFS_EXIT(zfsvfs);
2698 return (0);
2702 * Set the file attributes to the values contained in the
2703 * vattr structure.
2705 * IN: vp - vnode of file to be modified.
2706 * vap - new attribute values.
2707 * If VATTR_XVATTR set, then optional attrs are being set
2708 * flags - ATTR_UTIME set if non-default time values provided.
2709 * - ATTR_NOACLCHECK (CIFS context only).
2710 * cr - credentials of caller.
2711 * ct - caller context
2713 * RETURN: 0 on success, error code on failure.
2715 * Timestamps:
2716 * vp - ctime updated, mtime updated if size changed.
2718 /* ARGSUSED */
2719 static int
2720 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2721 caller_context_t *ct)
2723 znode_t *zp = VTOZ(vp);
2724 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2725 zilog_t *zilog;
2726 dmu_tx_t *tx;
2727 vattr_t oldva;
2728 xvattr_t tmpxvattr;
2729 uint_t mask = vap->va_mask;
2730 uint_t saved_mask = 0;
2731 int trim_mask = 0;
2732 uint64_t new_mode;
2733 uint64_t new_uid, new_gid;
2734 uint64_t xattr_obj;
2735 uint64_t mtime[2], ctime[2];
2736 znode_t *attrzp;
2737 int need_policy = FALSE;
2738 int err, err2;
2739 zfs_fuid_info_t *fuidp = NULL;
2740 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2741 xoptattr_t *xoap;
2742 zfs_acl_t *aclp;
2743 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2744 boolean_t fuid_dirtied = B_FALSE;
2745 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2746 int count = 0, xattr_count = 0;
2748 if (mask == 0)
2749 return (0);
2751 if (mask & VATTR_NOSET)
2752 return (SET_ERROR(EINVAL));
2754 ZFS_ENTER(zfsvfs);
2755 ZFS_VERIFY_ZP(zp);
2757 zilog = zfsvfs->z_log;
2760 * Make sure that if we have ephemeral uid/gid or xvattr specified
2761 * that file system is at proper version level
2764 if (zfsvfs->z_use_fuids == B_FALSE &&
2765 (((mask & VATTR_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2766 ((mask & VATTR_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2767 (mask & VATTR_XVATTR))) {
2768 ZFS_EXIT(zfsvfs);
2769 return (SET_ERROR(EINVAL));
2772 if (mask & VATTR_SIZE && vp->v_type == VDIR) {
2773 ZFS_EXIT(zfsvfs);
2774 return (SET_ERROR(EISDIR));
2777 if (mask & VATTR_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2778 ZFS_EXIT(zfsvfs);
2779 return (SET_ERROR(EINVAL));
2783 * If this is an xvattr_t, then get a pointer to the structure of
2784 * optional attributes. If this is NULL, then we have a vattr_t.
2786 xoap = xva_getxoptattr(xvap);
2788 xva_init(&tmpxvattr);
2791 * Immutable files can only alter immutable bit and atime
2793 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2794 ((mask & (VATTR_SIZE|VATTR_UID|VATTR_GID|VATTR_MTIME|VATTR_MODE)) ||
2795 ((mask & VATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2796 ZFS_EXIT(zfsvfs);
2797 return (SET_ERROR(EPERM));
2801 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2805 * Verify timestamps doesn't overflow 32 bits.
2806 * ZFS can handle large timestamps, but 32bit syscalls can't
2807 * handle times greater than 2039. This check should be removed
2808 * once large timestamps are fully supported.
2810 if (mask & (VATTR_ATIME | VATTR_MTIME)) {
2811 if (((mask & VATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2812 ((mask & VATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2813 ZFS_EXIT(zfsvfs);
2814 return (SET_ERROR(EOVERFLOW));
2818 top:
2819 attrzp = NULL;
2820 aclp = NULL;
2822 /* Can this be moved to before the top label? */
2823 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2824 ZFS_EXIT(zfsvfs);
2825 return (SET_ERROR(EROFS));
2829 * First validate permissions
2832 if (mask & VATTR_SIZE) {
2833 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2834 if (err) {
2835 ZFS_EXIT(zfsvfs);
2836 return (err);
2839 * XXX - Note, we are not providing any open
2840 * mode flags here (like FNDELAY), so we may
2841 * block if there are locks present... this
2842 * should be addressed in openat().
2844 /* XXX - would it be OK to generate a log record here? */
2845 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2846 if (err) {
2847 ZFS_EXIT(zfsvfs);
2848 return (err);
2851 if (vap->va_size == 0)
2852 vnevent_truncate(ZTOV(zp), ct);
2855 if (mask & (VATTR_ATIME|VATTR_MTIME) ||
2856 ((mask & VATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2857 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2858 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2859 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2860 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2861 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2862 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2863 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2864 skipaclchk, cr);
2867 if (mask & (VATTR_UID|VATTR_GID)) {
2868 int idmask = (mask & (VATTR_UID|VATTR_GID));
2869 int take_owner;
2870 int take_group;
2873 * NOTE: even if a new mode is being set,
2874 * we may clear S_ISUID/S_ISGID bits.
2877 if (!(mask & VATTR_MODE))
2878 vap->va_mode = zp->z_mode;
2881 * Take ownership or chgrp to group we are a member of
2884 take_owner = (mask & VATTR_UID) && (vap->va_uid == crgetuid(cr));
2885 take_group = (mask & VATTR_GID) &&
2886 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2889 * If both VATTR_UID and VATTR_GID are set then take_owner and
2890 * take_group must both be set in order to allow taking
2891 * ownership.
2893 * Otherwise, send the check through secpolicy_vnode_setattr()
2897 if (((idmask == (VATTR_UID|VATTR_GID)) && take_owner && take_group) ||
2898 ((idmask == VATTR_UID) && take_owner) ||
2899 ((idmask == VATTR_GID) && take_group)) {
2900 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2901 skipaclchk, cr) == 0) {
2903 * Remove setuid/setgid for non-privileged users
2905 secpolicy_setid_clear(vap, cr);
2906 trim_mask = (mask & (VATTR_UID|VATTR_GID));
2907 } else {
2908 need_policy = TRUE;
2910 } else {
2911 need_policy = TRUE;
2915 mutex_enter(&zp->z_lock);
2916 oldva.va_mode = zp->z_mode;
2917 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2918 if (mask & VATTR_XVATTR) {
2920 * Update xvattr mask to include only those attributes
2921 * that are actually changing.
2923 * the bits will be restored prior to actually setting
2924 * the attributes so the caller thinks they were set.
2926 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2927 if (xoap->xoa_appendonly !=
2928 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2929 need_policy = TRUE;
2930 } else {
2931 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2932 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2936 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2937 if (xoap->xoa_nounlink !=
2938 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2939 need_policy = TRUE;
2940 } else {
2941 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2942 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2946 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2947 if (xoap->xoa_immutable !=
2948 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2949 need_policy = TRUE;
2950 } else {
2951 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2952 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2956 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2957 if (xoap->xoa_nodump !=
2958 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2959 need_policy = TRUE;
2960 } else {
2961 XVA_CLR_REQ(xvap, XAT_NODUMP);
2962 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2966 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2967 if (xoap->xoa_av_modified !=
2968 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2969 need_policy = TRUE;
2970 } else {
2971 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2972 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2976 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2977 if ((vp->v_type != VREG &&
2978 xoap->xoa_av_quarantined) ||
2979 xoap->xoa_av_quarantined !=
2980 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2981 need_policy = TRUE;
2982 } else {
2983 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2984 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2988 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2989 mutex_exit(&zp->z_lock);
2990 ZFS_EXIT(zfsvfs);
2991 return (SET_ERROR(EPERM));
2994 if (need_policy == FALSE &&
2995 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2996 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2997 need_policy = TRUE;
3001 mutex_exit(&zp->z_lock);
3003 if (mask & VATTR_MODE) {
3004 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3005 err = secpolicy_setid_setsticky_clear(vp, vap,
3006 &oldva, cr);
3007 if (err) {
3008 ZFS_EXIT(zfsvfs);
3009 return (err);
3011 trim_mask |= VATTR_MODE;
3012 } else {
3013 need_policy = TRUE;
3017 if (need_policy) {
3019 * If trim_mask is set then take ownership
3020 * has been granted or write_acl is present and user
3021 * has the ability to modify mode. In that case remove
3022 * UID|GID and or MODE from mask so that
3023 * secpolicy_vnode_setattr() doesn't revoke it.
3026 if (trim_mask) {
3027 saved_mask = vap->va_mask;
3028 vap->va_mask &= ~trim_mask;
3030 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3031 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3032 if (err) {
3033 ZFS_EXIT(zfsvfs);
3034 return (err);
3037 if (trim_mask)
3038 vap->va_mask |= saved_mask;
3042 * secpolicy_vnode_setattr, or take ownership may have
3043 * changed va_mask
3045 mask = vap->va_mask;
3047 if ((mask & (VATTR_UID | VATTR_GID))) {
3048 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3049 &xattr_obj, sizeof (xattr_obj));
3051 if (err == 0 && xattr_obj) {
3052 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3053 if (err)
3054 goto out2;
3056 if (mask & VATTR_UID) {
3057 new_uid = zfs_fuid_create(zfsvfs,
3058 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3059 if (new_uid != zp->z_uid &&
3060 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3061 if (attrzp)
3062 VN_RELE(ZTOV(attrzp));
3063 err = SET_ERROR(EDQUOT);
3064 goto out2;
3068 if (mask & VATTR_GID) {
3069 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3070 cr, ZFS_GROUP, &fuidp);
3071 if (new_gid != zp->z_gid &&
3072 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3073 if (attrzp)
3074 VN_RELE(ZTOV(attrzp));
3075 err = SET_ERROR(EDQUOT);
3076 goto out2;
3080 tx = dmu_tx_create(zfsvfs->z_os);
3082 if (mask & VATTR_MODE) {
3083 uint64_t pmode = zp->z_mode;
3084 uint64_t acl_obj;
3085 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3087 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3088 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3089 err = SET_ERROR(EPERM);
3090 goto out;
3093 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3094 goto out;
3096 mutex_enter(&zp->z_lock);
3097 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3099 * Are we upgrading ACL from old V0 format
3100 * to V1 format?
3102 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3103 zfs_znode_acl_version(zp) ==
3104 ZFS_ACL_VERSION_INITIAL) {
3105 dmu_tx_hold_free(tx, acl_obj, 0,
3106 DMU_OBJECT_END);
3107 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3108 0, aclp->z_acl_bytes);
3109 } else {
3110 dmu_tx_hold_write(tx, acl_obj, 0,
3111 aclp->z_acl_bytes);
3113 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3114 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3115 0, aclp->z_acl_bytes);
3117 mutex_exit(&zp->z_lock);
3118 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3119 } else {
3120 if ((mask & VATTR_XVATTR) &&
3121 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3122 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3123 else
3124 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3127 if (attrzp) {
3128 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3131 fuid_dirtied = zfsvfs->z_fuid_dirty;
3132 if (fuid_dirtied)
3133 zfs_fuid_txhold(zfsvfs, tx);
3135 zfs_sa_upgrade_txholds(tx, zp);
3137 err = dmu_tx_assign(tx, TXG_WAIT);
3138 if (err)
3139 goto out;
3141 count = 0;
3143 * Set each attribute requested.
3144 * We group settings according to the locks they need to acquire.
3146 * Note: you cannot set ctime directly, although it will be
3147 * updated as a side-effect of calling this function.
3151 if (mask & (VATTR_UID|VATTR_GID|VATTR_MODE))
3152 mutex_enter(&zp->z_acl_lock);
3153 mutex_enter(&zp->z_lock);
3155 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3156 &zp->z_pflags, sizeof (zp->z_pflags));
3158 if (attrzp) {
3159 if (mask & (VATTR_UID|VATTR_GID|VATTR_MODE))
3160 mutex_enter(&attrzp->z_acl_lock);
3161 mutex_enter(&attrzp->z_lock);
3162 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3163 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3164 sizeof (attrzp->z_pflags));
3167 if (mask & (VATTR_UID|VATTR_GID)) {
3169 if (mask & VATTR_UID) {
3170 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3171 &new_uid, sizeof (new_uid));
3172 zp->z_uid = new_uid;
3173 if (attrzp) {
3174 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3175 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3176 sizeof (new_uid));
3177 attrzp->z_uid = new_uid;
3181 if (mask & VATTR_GID) {
3182 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3183 NULL, &new_gid, sizeof (new_gid));
3184 zp->z_gid = new_gid;
3185 if (attrzp) {
3186 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3187 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3188 sizeof (new_gid));
3189 attrzp->z_gid = new_gid;
3192 if (!(mask & VATTR_MODE)) {
3193 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3194 NULL, &new_mode, sizeof (new_mode));
3195 new_mode = zp->z_mode;
3197 err = zfs_acl_chown_setattr(zp);
3198 ASSERT(err == 0);
3199 if (attrzp) {
3200 err = zfs_acl_chown_setattr(attrzp);
3201 ASSERT(err == 0);
3205 if (mask & VATTR_MODE) {
3206 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3207 &new_mode, sizeof (new_mode));
3208 zp->z_mode = new_mode;
3209 ASSERT3U((uintptr_t)aclp, !=, (uintptr_t)NULL);
3210 err = zfs_aclset_common(zp, aclp, cr, tx);
3211 ASSERT0(err);
3212 if (zp->z_acl_cached)
3213 zfs_acl_free(zp->z_acl_cached);
3214 zp->z_acl_cached = aclp;
3215 aclp = NULL;
3219 if (mask & VATTR_ATIME) {
3220 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3221 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3222 &zp->z_atime, sizeof (zp->z_atime));
3225 if (mask & VATTR_MTIME) {
3226 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3227 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3228 mtime, sizeof (mtime));
3231 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3232 if (mask & VATTR_SIZE && !(mask & VATTR_MTIME)) {
3233 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3234 NULL, mtime, sizeof (mtime));
3235 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3236 &ctime, sizeof (ctime));
3237 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3238 B_TRUE);
3239 } else if (mask != 0) {
3240 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3241 &ctime, sizeof (ctime));
3242 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3243 B_TRUE);
3244 if (attrzp) {
3245 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3246 SA_ZPL_CTIME(zfsvfs), NULL,
3247 &ctime, sizeof (ctime));
3248 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3249 mtime, ctime, B_TRUE);
3253 * Do this after setting timestamps to prevent timestamp
3254 * update from toggling bit
3257 if (xoap && (mask & VATTR_XVATTR)) {
3260 * restore trimmed off masks
3261 * so that return masks can be set for caller.
3264 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3265 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3267 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3268 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3270 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3271 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3273 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3274 XVA_SET_REQ(xvap, XAT_NODUMP);
3276 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3277 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3279 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3280 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3283 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3284 ASSERT(vp->v_type == VREG);
3286 zfs_xvattr_set(zp, xvap, tx);
3289 if (fuid_dirtied)
3290 zfs_fuid_sync(zfsvfs, tx);
3292 if (mask != 0)
3293 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3295 mutex_exit(&zp->z_lock);
3296 if (mask & (VATTR_UID|VATTR_GID|VATTR_MODE))
3297 mutex_exit(&zp->z_acl_lock);
3299 if (attrzp) {
3300 if (mask & (VATTR_UID|VATTR_GID|VATTR_MODE))
3301 mutex_exit(&attrzp->z_acl_lock);
3302 mutex_exit(&attrzp->z_lock);
3304 out:
3305 if (err == 0 && attrzp) {
3306 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3307 xattr_count, tx);
3308 ASSERT(err2 == 0);
3311 if (attrzp)
3312 VN_RELE(ZTOV(attrzp));
3314 if (aclp)
3315 zfs_acl_free(aclp);
3317 if (fuidp) {
3318 zfs_fuid_info_free(fuidp);
3319 fuidp = NULL;
3322 if (err) {
3323 dmu_tx_abort(tx);
3324 if (err == ERESTART)
3325 goto top;
3326 } else {
3327 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3328 dmu_tx_commit(tx);
3331 out2:
3332 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3333 zil_commit(zilog, 0);
3335 ZFS_EXIT(zfsvfs);
3336 return (err);
3339 typedef struct zfs_zlock {
3340 krwlock_t *zl_rwlock; /* lock we acquired */
3341 znode_t *zl_znode; /* znode we held */
3342 struct zfs_zlock *zl_next; /* next in list */
3343 } zfs_zlock_t;
3346 * Drop locks and release vnodes that were held by zfs_rename_lock().
3348 static void
3349 zfs_rename_unlock(zfs_zlock_t **zlpp)
3351 zfs_zlock_t *zl;
3353 while ((zl = *zlpp) != NULL) {
3354 if (zl->zl_znode != NULL)
3355 VN_RELE(ZTOV(zl->zl_znode));
3356 rw_exit(zl->zl_rwlock);
3357 *zlpp = zl->zl_next;
3358 kmem_free(zl, sizeof (*zl));
3363 * Search back through the directory tree, using the ".." entries.
3364 * Lock each directory in the chain to prevent concurrent renames.
3365 * Fail any attempt to move a directory into one of its own descendants.
3366 * XXX - z_parent_lock can overlap with map or grow locks
3368 static int
3369 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3371 zfs_zlock_t *zl;
3372 znode_t *zp = tdzp;
3373 uint64_t rootid = zp->z_zfsvfs->z_root;
3374 uint64_t oidp = zp->z_id;
3375 krwlock_t *rwlp = &szp->z_parent_lock;
3376 krw_t rw = RW_WRITER;
3379 * First pass write-locks szp and compares to zp->z_id.
3380 * Later passes read-lock zp and compare to zp->z_parent.
3382 do {
3383 if (!rw_tryenter(rwlp, rw)) {
3385 * Another thread is renaming in this path.
3386 * Note that if we are a WRITER, we don't have any
3387 * parent_locks held yet.
3389 if (rw == RW_READER && zp->z_id > szp->z_id) {
3391 * Drop our locks and restart
3393 zfs_rename_unlock(&zl);
3394 *zlpp = NULL;
3395 zp = tdzp;
3396 oidp = zp->z_id;
3397 rwlp = &szp->z_parent_lock;
3398 rw = RW_WRITER;
3399 continue;
3400 } else {
3402 * Wait for other thread to drop its locks
3404 rw_enter(rwlp, rw);
3408 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3409 zl->zl_rwlock = rwlp;
3410 zl->zl_znode = NULL;
3411 zl->zl_next = *zlpp;
3412 *zlpp = zl;
3414 if (oidp == szp->z_id) /* We're a descendant of szp */
3415 return (SET_ERROR(EINVAL));
3417 if (oidp == rootid) /* We've hit the top */
3418 return (0);
3420 if (rw == RW_READER) { /* i.e. not the first pass */
3421 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3422 if (error)
3423 return (error);
3424 zl->zl_znode = zp;
3426 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3427 &oidp, sizeof (oidp));
3428 rwlp = &zp->z_parent_lock;
3429 rw = RW_READER;
3431 } while (zp->z_id != sdzp->z_id);
3433 return (0);
3437 * Move an entry from the provided source directory to the target
3438 * directory. Change the entry name as indicated.
3440 * IN: sdvp - Source directory containing the "old entry".
3441 * snm - Old entry name.
3442 * tdvp - Target directory to contain the "new entry".
3443 * tnm - New entry name.
3444 * cr - credentials of caller.
3445 * ct - caller context
3446 * flags - case flags
3448 * RETURN: 0 on success, error code on failure.
3450 * Timestamps:
3451 * sdvp,tdvp - ctime|mtime updated
3453 /*ARGSUSED*/
3454 static int
3455 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3456 caller_context_t *ct, int flags)
3458 znode_t *tdzp, *szp, *tzp;
3459 znode_t *sdzp = VTOZ(sdvp);
3460 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3461 zilog_t *zilog;
3462 vnode_t *realvp;
3463 zfs_dirlock_t *sdl, *tdl;
3464 dmu_tx_t *tx;
3465 zfs_zlock_t *zl;
3466 int cmp, serr, terr;
3467 int error = 0, rm_err = 0;
3468 int zflg = 0;
3469 boolean_t waited = B_FALSE;
3471 ZFS_ENTER(zfsvfs);
3472 ZFS_VERIFY_ZP(sdzp);
3473 zilog = zfsvfs->z_log;
3476 * Make sure we have the real vp for the target directory.
3478 if (fop_realvp(tdvp, &realvp, ct) == 0)
3479 tdvp = realvp;
3481 tdzp = VTOZ(tdvp);
3482 ZFS_VERIFY_ZP(tdzp);
3485 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3486 * ctldir appear to have the same v_vfsp.
3488 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3489 ZFS_EXIT(zfsvfs);
3490 return (SET_ERROR(EXDEV));
3493 if (zfsvfs->z_utf8 && u8_validate(tnm,
3494 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3495 ZFS_EXIT(zfsvfs);
3496 return (SET_ERROR(EILSEQ));
3499 if (flags & FIGNORECASE)
3500 zflg |= ZCILOOK;
3502 top:
3503 szp = NULL;
3504 tzp = NULL;
3505 zl = NULL;
3508 * This is to prevent the creation of links into attribute space
3509 * by renaming a linked file into/outof an attribute directory.
3510 * See the comment in zfs_link() for why this is considered bad.
3512 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3513 ZFS_EXIT(zfsvfs);
3514 return (SET_ERROR(EINVAL));
3518 * Lock source and target directory entries. To prevent deadlock,
3519 * a lock ordering must be defined. We lock the directory with
3520 * the smallest object id first, or if it's a tie, the one with
3521 * the lexically first name.
3523 if (sdzp->z_id < tdzp->z_id) {
3524 cmp = -1;
3525 } else if (sdzp->z_id > tdzp->z_id) {
3526 cmp = 1;
3527 } else {
3529 * First compare the two name arguments without
3530 * considering any case folding.
3532 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3534 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3535 ASSERT(error == 0 || !zfsvfs->z_utf8);
3536 if (cmp == 0) {
3538 * POSIX: "If the old argument and the new argument
3539 * both refer to links to the same existing file,
3540 * the rename() function shall return successfully
3541 * and perform no other action."
3543 ZFS_EXIT(zfsvfs);
3544 return (0);
3547 * If the file system is case-folding, then we may
3548 * have some more checking to do. A case-folding file
3549 * system is either supporting mixed case sensitivity
3550 * access or is completely case-insensitive. Note
3551 * that the file system is always case preserving.
3553 * In mixed sensitivity mode case sensitive behavior
3554 * is the default. FIGNORECASE must be used to
3555 * explicitly request case insensitive behavior.
3557 * If the source and target names provided differ only
3558 * by case (e.g., a request to rename 'tim' to 'Tim'),
3559 * we will treat this as a special case in the
3560 * case-insensitive mode: as long as the source name
3561 * is an exact match, we will allow this to proceed as
3562 * a name-change request.
3564 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3565 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3566 flags & FIGNORECASE)) &&
3567 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3568 &error) == 0) {
3570 * case preserving rename request, require exact
3571 * name matches
3573 zflg |= ZCIEXACT;
3574 zflg &= ~ZCILOOK;
3579 * If the source and destination directories are the same, we should
3580 * grab the z_name_lock of that directory only once.
3582 if (sdzp == tdzp) {
3583 zflg |= ZHAVELOCK;
3584 rw_enter(&sdzp->z_name_lock, RW_READER);
3587 if (cmp < 0) {
3588 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3589 ZEXISTS | zflg, NULL, NULL);
3590 terr = zfs_dirent_lock(&tdl,
3591 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3592 } else {
3593 terr = zfs_dirent_lock(&tdl,
3594 tdzp, tnm, &tzp, zflg, NULL, NULL);
3595 serr = zfs_dirent_lock(&sdl,
3596 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3597 NULL, NULL);
3600 if (serr) {
3602 * Source entry invalid or not there.
3604 if (!terr) {
3605 zfs_dirent_unlock(tdl);
3606 if (tzp)
3607 VN_RELE(ZTOV(tzp));
3610 if (sdzp == tdzp)
3611 rw_exit(&sdzp->z_name_lock);
3613 if (strcmp(snm, "..") == 0)
3614 serr = SET_ERROR(EINVAL);
3615 ZFS_EXIT(zfsvfs);
3616 return (serr);
3618 if (terr) {
3619 zfs_dirent_unlock(sdl);
3620 VN_RELE(ZTOV(szp));
3622 if (sdzp == tdzp)
3623 rw_exit(&sdzp->z_name_lock);
3625 if (strcmp(tnm, "..") == 0)
3626 terr = SET_ERROR(EINVAL);
3627 ZFS_EXIT(zfsvfs);
3628 return (terr);
3632 * Must have write access at the source to remove the old entry
3633 * and write access at the target to create the new entry.
3634 * Note that if target and source are the same, this can be
3635 * done in a single check.
3638 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3639 goto out;
3641 if (ZTOV(szp)->v_type == VDIR) {
3643 * Check to make sure rename is valid.
3644 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3646 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3647 goto out;
3651 * Does target exist?
3653 if (tzp) {
3655 * Source and target must be the same type.
3657 if (ZTOV(szp)->v_type == VDIR) {
3658 if (ZTOV(tzp)->v_type != VDIR) {
3659 error = SET_ERROR(ENOTDIR);
3660 goto out;
3662 } else {
3663 if (ZTOV(tzp)->v_type == VDIR) {
3664 error = SET_ERROR(EISDIR);
3665 goto out;
3669 * POSIX dictates that when the source and target
3670 * entries refer to the same file object, rename
3671 * must do nothing and exit without error.
3673 if (szp->z_id == tzp->z_id) {
3674 error = 0;
3675 goto out;
3679 vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3680 if (tzp)
3681 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3684 * notify the target directory if it is not the same
3685 * as source directory.
3687 if (tdvp != sdvp) {
3688 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3691 tx = dmu_tx_create(zfsvfs->z_os);
3692 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3693 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3694 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3695 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3696 if (sdzp != tdzp) {
3697 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3698 zfs_sa_upgrade_txholds(tx, tdzp);
3700 if (tzp) {
3701 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3702 zfs_sa_upgrade_txholds(tx, tzp);
3705 zfs_sa_upgrade_txholds(tx, szp);
3706 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3707 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
3708 if (error) {
3709 if (zl != NULL)
3710 zfs_rename_unlock(&zl);
3711 zfs_dirent_unlock(sdl);
3712 zfs_dirent_unlock(tdl);
3714 if (sdzp == tdzp)
3715 rw_exit(&sdzp->z_name_lock);
3717 VN_RELE(ZTOV(szp));
3718 if (tzp)
3719 VN_RELE(ZTOV(tzp));
3720 if (error == ERESTART) {
3721 waited = B_TRUE;
3722 dmu_tx_wait(tx);
3723 dmu_tx_abort(tx);
3724 goto top;
3726 dmu_tx_abort(tx);
3727 ZFS_EXIT(zfsvfs);
3728 return (error);
3731 if (tzp) /* Attempt to remove the existing target */
3732 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3734 if (error == 0) {
3735 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3736 if (error == 0) {
3737 szp->z_pflags |= ZFS_AV_MODIFIED;
3739 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3740 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3741 ASSERT0(error);
3743 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3744 if (error == 0) {
3745 zfs_log_rename(zilog, tx, TX_RENAME |
3746 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3747 sdl->dl_name, tdzp, tdl->dl_name, szp);
3750 * Update path information for the target vnode
3752 vn_renamepath(tdvp, ZTOV(szp), tnm,
3753 strlen(tnm));
3754 } else {
3756 * At this point, we have successfully created
3757 * the target name, but have failed to remove
3758 * the source name. Since the create was done
3759 * with the ZRENAMING flag, there are
3760 * complications; for one, the link count is
3761 * wrong. The easiest way to deal with this
3762 * is to remove the newly created target, and
3763 * return the original error. This must
3764 * succeed; fortunately, it is very unlikely to
3765 * fail, since we just created it.
3767 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3768 ZRENAMING, NULL), ==, 0);
3773 dmu_tx_commit(tx);
3775 if (tzp && rm_err == 0)
3776 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3778 if (error == 0) {
3779 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3780 /* notify the target dir if it is not the same as source dir */
3781 if (tdvp != sdvp)
3782 vnevent_rename_dest_dir(tdvp, ct);
3784 out:
3785 if (zl != NULL)
3786 zfs_rename_unlock(&zl);
3788 zfs_dirent_unlock(sdl);
3789 zfs_dirent_unlock(tdl);
3791 if (sdzp == tdzp)
3792 rw_exit(&sdzp->z_name_lock);
3795 VN_RELE(ZTOV(szp));
3796 if (tzp)
3797 VN_RELE(ZTOV(tzp));
3799 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3800 zil_commit(zilog, 0);
3802 ZFS_EXIT(zfsvfs);
3803 return (error);
3807 * Insert the indicated symbolic reference entry into the directory.
3809 * IN: dvp - Directory to contain new symbolic link.
3810 * link - Name for new symlink entry.
3811 * vap - Attributes of new entry.
3812 * cr - credentials of caller.
3813 * ct - caller context
3814 * flags - case flags
3816 * RETURN: 0 on success, error code on failure.
3818 * Timestamps:
3819 * dvp - ctime|mtime updated
3821 /*ARGSUSED*/
3822 static int
3823 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3824 caller_context_t *ct, int flags)
3826 znode_t *zp, *dzp = VTOZ(dvp);
3827 zfs_dirlock_t *dl;
3828 dmu_tx_t *tx;
3829 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3830 zilog_t *zilog;
3831 uint64_t len = strlen(link);
3832 int error;
3833 int zflg = ZNEW;
3834 zfs_acl_ids_t acl_ids;
3835 boolean_t fuid_dirtied;
3836 uint64_t txtype = TX_SYMLINK;
3837 boolean_t waited = B_FALSE;
3839 ASSERT(vap->va_type == VLNK);
3841 ZFS_ENTER(zfsvfs);
3842 ZFS_VERIFY_ZP(dzp);
3843 zilog = zfsvfs->z_log;
3845 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3846 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3847 ZFS_EXIT(zfsvfs);
3848 return (SET_ERROR(EILSEQ));
3850 if (flags & FIGNORECASE)
3851 zflg |= ZCILOOK;
3853 if (len > MAXPATHLEN) {
3854 ZFS_EXIT(zfsvfs);
3855 return (SET_ERROR(ENAMETOOLONG));
3858 if ((error = zfs_acl_ids_create(dzp, 0,
3859 vap, cr, NULL, &acl_ids)) != 0) {
3860 ZFS_EXIT(zfsvfs);
3861 return (error);
3863 top:
3865 * Attempt to lock directory; fail if entry already exists.
3867 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3868 if (error) {
3869 zfs_acl_ids_free(&acl_ids);
3870 ZFS_EXIT(zfsvfs);
3871 return (error);
3874 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3875 zfs_acl_ids_free(&acl_ids);
3876 zfs_dirent_unlock(dl);
3877 ZFS_EXIT(zfsvfs);
3878 return (error);
3881 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3882 zfs_acl_ids_free(&acl_ids);
3883 zfs_dirent_unlock(dl);
3884 ZFS_EXIT(zfsvfs);
3885 return (SET_ERROR(EDQUOT));
3887 tx = dmu_tx_create(zfsvfs->z_os);
3888 fuid_dirtied = zfsvfs->z_fuid_dirty;
3889 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3890 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3891 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3892 ZFS_SA_BASE_ATTR_SIZE + len);
3893 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3894 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3895 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3896 acl_ids.z_aclp->z_acl_bytes);
3898 if (fuid_dirtied)
3899 zfs_fuid_txhold(zfsvfs, tx);
3900 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
3901 if (error) {
3902 zfs_dirent_unlock(dl);
3903 if (error == ERESTART) {
3904 waited = B_TRUE;
3905 dmu_tx_wait(tx);
3906 dmu_tx_abort(tx);
3907 goto top;
3909 zfs_acl_ids_free(&acl_ids);
3910 dmu_tx_abort(tx);
3911 ZFS_EXIT(zfsvfs);
3912 return (error);
3916 * Create a new object for the symlink.
3917 * for version 4 ZPL datsets the symlink will be an SA attribute
3919 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3921 if (fuid_dirtied)
3922 zfs_fuid_sync(zfsvfs, tx);
3924 mutex_enter(&zp->z_lock);
3925 if (zp->z_is_sa)
3926 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3927 link, len, tx);
3928 else
3929 zfs_sa_symlink(zp, link, len, tx);
3930 mutex_exit(&zp->z_lock);
3932 zp->z_size = len;
3933 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3934 &zp->z_size, sizeof (zp->z_size), tx);
3936 * Insert the new object into the directory.
3938 (void) zfs_link_create(dl, zp, tx, ZNEW);
3940 if (flags & FIGNORECASE)
3941 txtype |= TX_CI;
3942 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3944 zfs_acl_ids_free(&acl_ids);
3946 dmu_tx_commit(tx);
3948 zfs_dirent_unlock(dl);
3950 VN_RELE(ZTOV(zp));
3952 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3953 zil_commit(zilog, 0);
3955 ZFS_EXIT(zfsvfs);
3956 return (error);
3960 * Return, in the buffer contained in the provided uio structure,
3961 * the symbolic path referred to by vp.
3963 * IN: vp - vnode of symbolic link.
3964 * uio - structure to contain the link path.
3965 * cr - credentials of caller.
3966 * ct - caller context
3968 * OUT: uio - structure containing the link path.
3970 * RETURN: 0 on success, error code on failure.
3972 * Timestamps:
3973 * vp - atime updated
3975 /* ARGSUSED */
3976 static int
3977 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3979 znode_t *zp = VTOZ(vp);
3980 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3981 int error;
3983 ZFS_ENTER(zfsvfs);
3984 ZFS_VERIFY_ZP(zp);
3986 mutex_enter(&zp->z_lock);
3987 if (zp->z_is_sa)
3988 error = sa_lookup_uio(zp->z_sa_hdl,
3989 SA_ZPL_SYMLINK(zfsvfs), uio);
3990 else
3991 error = zfs_sa_readlink(zp, uio);
3992 mutex_exit(&zp->z_lock);
3994 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3996 ZFS_EXIT(zfsvfs);
3997 return (error);
4001 * Insert a new entry into directory tdvp referencing svp.
4003 * IN: tdvp - Directory to contain new entry.
4004 * svp - vnode of new entry.
4005 * name - name of new entry.
4006 * cr - credentials of caller.
4007 * ct - caller context
4009 * RETURN: 0 on success, error code on failure.
4011 * Timestamps:
4012 * tdvp - ctime|mtime updated
4013 * svp - ctime updated
4015 /* ARGSUSED */
4016 static int
4017 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4018 caller_context_t *ct, int flags)
4020 znode_t *dzp = VTOZ(tdvp);
4021 znode_t *tzp, *szp;
4022 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4023 zilog_t *zilog;
4024 zfs_dirlock_t *dl;
4025 dmu_tx_t *tx;
4026 vnode_t *realvp;
4027 int error;
4028 int zf = ZNEW;
4029 uint64_t parent;
4030 uid_t owner;
4031 boolean_t waited = B_FALSE;
4033 ASSERT(tdvp->v_type == VDIR);
4035 ZFS_ENTER(zfsvfs);
4036 ZFS_VERIFY_ZP(dzp);
4037 zilog = zfsvfs->z_log;
4039 if (fop_realvp(svp, &realvp, ct) == 0)
4040 svp = realvp;
4043 * POSIX dictates that we return EPERM here.
4044 * Better choices include ENOTSUP or EISDIR.
4046 if (svp->v_type == VDIR) {
4047 ZFS_EXIT(zfsvfs);
4048 return (SET_ERROR(EPERM));
4051 szp = VTOZ(svp);
4052 ZFS_VERIFY_ZP(szp);
4055 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4056 * ctldir appear to have the same v_vfsp.
4058 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4059 ZFS_EXIT(zfsvfs);
4060 return (SET_ERROR(EXDEV));
4063 /* Prevent links to .zfs/shares files */
4065 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4066 &parent, sizeof (uint64_t))) != 0) {
4067 ZFS_EXIT(zfsvfs);
4068 return (error);
4070 if (parent == zfsvfs->z_shares_dir) {
4071 ZFS_EXIT(zfsvfs);
4072 return (SET_ERROR(EPERM));
4075 if (zfsvfs->z_utf8 && u8_validate(name,
4076 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4077 ZFS_EXIT(zfsvfs);
4078 return (SET_ERROR(EILSEQ));
4080 if (flags & FIGNORECASE)
4081 zf |= ZCILOOK;
4084 * We do not support links between attributes and non-attributes
4085 * because of the potential security risk of creating links
4086 * into "normal" file space in order to circumvent restrictions
4087 * imposed in attribute space.
4089 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4090 ZFS_EXIT(zfsvfs);
4091 return (SET_ERROR(EINVAL));
4095 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4096 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4097 ZFS_EXIT(zfsvfs);
4098 return (SET_ERROR(EPERM));
4101 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4102 ZFS_EXIT(zfsvfs);
4103 return (error);
4106 top:
4108 * Attempt to lock directory; fail if entry already exists.
4110 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4111 if (error) {
4112 ZFS_EXIT(zfsvfs);
4113 return (error);
4116 tx = dmu_tx_create(zfsvfs->z_os);
4117 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4118 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4119 zfs_sa_upgrade_txholds(tx, szp);
4120 zfs_sa_upgrade_txholds(tx, dzp);
4121 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT);
4122 if (error) {
4123 zfs_dirent_unlock(dl);
4124 if (error == ERESTART) {
4125 waited = B_TRUE;
4126 dmu_tx_wait(tx);
4127 dmu_tx_abort(tx);
4128 goto top;
4130 dmu_tx_abort(tx);
4131 ZFS_EXIT(zfsvfs);
4132 return (error);
4135 error = zfs_link_create(dl, szp, tx, 0);
4137 if (error == 0) {
4138 uint64_t txtype = TX_LINK;
4139 if (flags & FIGNORECASE)
4140 txtype |= TX_CI;
4141 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4144 dmu_tx_commit(tx);
4146 zfs_dirent_unlock(dl);
4148 if (error == 0) {
4149 vnevent_link(svp, ct);
4152 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4153 zil_commit(zilog, 0);
4155 ZFS_EXIT(zfsvfs);
4156 return (error);
4160 * zfs_null_putapage() is used when the file system has been force
4161 * unmounted. It just drops the pages.
4163 /* ARGSUSED */
4164 static int
4165 zfs_null_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4166 size_t *lenp, int flags, cred_t *cr)
4168 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4169 return (0);
4173 * Push a page out to disk, klustering if possible.
4175 * IN: vp - file to push page to.
4176 * pp - page to push.
4177 * flags - additional flags.
4178 * cr - credentials of caller.
4180 * OUT: offp - start of range pushed.
4181 * lenp - len of range pushed.
4183 * RETURN: 0 on success, error code on failure.
4185 * NOTE: callers must have locked the page to be pushed. On
4186 * exit, the page (and all other pages in the kluster) must be
4187 * unlocked.
4189 /* ARGSUSED */
4190 static int
4191 zfs_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4192 size_t *lenp, int flags, cred_t *cr)
4194 znode_t *zp = VTOZ(vp);
4195 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4196 dmu_tx_t *tx;
4197 uoff_t off, koff;
4198 size_t len, klen;
4199 int err;
4201 off = pp->p_offset;
4202 len = PAGESIZE;
4204 * If our blocksize is bigger than the page size, try to kluster
4205 * multiple pages so that we write a full block (thus avoiding
4206 * a read-modify-write).
4208 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4209 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4210 koff = ISP2(klen) ? P2ALIGN(off, (uoff_t)klen) : 0;
4211 ASSERT(koff <= zp->z_size);
4212 if (koff + klen > zp->z_size)
4213 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4214 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4216 ASSERT3U(btop(len), ==, btopr(len));
4219 * Can't push pages past end-of-file.
4221 if (off >= zp->z_size) {
4222 /* ignore all pages */
4223 err = 0;
4224 goto out;
4225 } else if (off + len > zp->z_size) {
4226 int npages = btopr(zp->z_size - off);
4227 page_t *trunc;
4229 page_list_break(&pp, &trunc, npages);
4230 /* ignore pages past end of file */
4231 if (trunc)
4232 pvn_write_done(trunc, flags);
4233 len = zp->z_size - off;
4236 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4237 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4238 err = SET_ERROR(EDQUOT);
4239 goto out;
4241 tx = dmu_tx_create(zfsvfs->z_os);
4242 dmu_tx_hold_write(tx, zp->z_id, off, len);
4244 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4245 zfs_sa_upgrade_txholds(tx, zp);
4246 err = dmu_tx_assign(tx, TXG_WAIT);
4247 if (err != 0) {
4248 dmu_tx_abort(tx);
4249 goto out;
4252 if (zp->z_blksz <= PAGESIZE) {
4253 caddr_t va = zfs_map_page(pp, S_READ);
4254 ASSERT3U(len, <=, PAGESIZE);
4255 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4256 zfs_unmap_page(pp, va);
4257 } else {
4258 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4261 if (err == 0) {
4262 uint64_t mtime[2], ctime[2];
4263 sa_bulk_attr_t bulk[3];
4264 int count = 0;
4266 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4267 &mtime, 16);
4268 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4269 &ctime, 16);
4270 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4271 &zp->z_pflags, 8);
4272 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4273 B_TRUE);
4274 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4275 ASSERT0(err);
4276 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4278 dmu_tx_commit(tx);
4280 out:
4281 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4282 if (offp)
4283 *offp = off;
4284 if (lenp)
4285 *lenp = len;
4287 return (err);
4291 * Copy the portion of the file indicated from pages into the file.
4292 * The pages are stored in a page list attached to the files vnode.
4294 * IN: vp - vnode of file to push page data to.
4295 * off - position in file to put data.
4296 * len - amount of data to write.
4297 * flags - flags to control the operation.
4298 * cr - credentials of caller.
4299 * ct - caller context.
4301 * RETURN: 0 on success, error code on failure.
4303 * Timestamps:
4304 * vp - ctime|mtime updated
4306 /*ARGSUSED*/
4307 static int
4308 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4309 caller_context_t *ct)
4311 znode_t *zp = VTOZ(vp);
4312 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4313 page_t *pp;
4314 size_t io_len;
4315 uoff_t io_off;
4316 uint_t blksz;
4317 locked_range_t *lr;
4318 int error = 0;
4320 ZFS_ENTER(zfsvfs);
4321 ZFS_VERIFY_ZP(zp);
4324 * There's nothing to do if no data is cached.
4326 if (!vn_has_cached_data(vp)) {
4327 ZFS_EXIT(zfsvfs);
4328 return (0);
4332 * Align this request to the file block size in case we kluster.
4333 * XXX - this can result in pretty aggresive locking, which can
4334 * impact simultanious read/write access. One option might be
4335 * to break up long requests (len == 0) into block-by-block
4336 * operations to get narrower locking.
4338 blksz = zp->z_blksz;
4339 if (ISP2(blksz))
4340 io_off = P2ALIGN_TYPED(off, blksz, uoff_t);
4341 else
4342 io_off = 0;
4343 if (len > 0 && ISP2(blksz))
4344 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4345 else
4346 io_len = 0;
4348 if (io_len == 0) {
4350 * Search the entire vp list for pages >= io_off.
4352 lr = rangelock_enter(&zp->z_rangelock,
4353 io_off, UINT64_MAX, RL_WRITER);
4354 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4355 goto out;
4357 lr = rangelock_enter(&zp->z_rangelock, io_off, io_len, RL_WRITER);
4359 if (off > zp->z_size) {
4360 /* past end of file */
4361 rangelock_exit(lr);
4362 ZFS_EXIT(zfsvfs);
4363 return (0);
4366 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4368 for (off = io_off; io_off < off + len; io_off += io_len) {
4369 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4370 pp = page_lookup(&vp->v_object, io_off,
4371 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4372 } else {
4373 pp = page_lookup_nowait(&vp->v_object, io_off,
4374 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4377 if (pp != NULL && pvn_getdirty(pp, flags)) {
4378 int err;
4381 * Found a dirty page to push
4383 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4384 if (err)
4385 error = err;
4386 } else {
4387 io_len = PAGESIZE;
4390 out:
4391 rangelock_exit(lr);
4392 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4393 zil_commit(zfsvfs->z_log, zp->z_id);
4394 ZFS_EXIT(zfsvfs);
4395 return (error);
4398 /*ARGSUSED*/
4399 void
4400 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4402 znode_t *zp = VTOZ(vp);
4403 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4404 int error;
4406 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4407 if (zp->z_sa_hdl == NULL) {
4409 * The fs has been unmounted, or we did a
4410 * suspend/resume and this file no longer exists.
4412 if (vn_has_cached_data(vp)) {
4413 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4414 B_INVAL, cr);
4417 mutex_enter(&zp->z_lock);
4418 mutex_enter(&vp->v_lock);
4419 ASSERT(vp->v_count == 1);
4420 VN_RELE_LOCKED(vp);
4421 mutex_exit(&vp->v_lock);
4422 mutex_exit(&zp->z_lock);
4423 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4424 zfs_znode_free(zp);
4425 return;
4429 * Attempt to push any data in the page cache. If this fails
4430 * we will get kicked out later in zfs_zinactive().
4432 if (vn_has_cached_data(vp)) {
4433 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4434 cr);
4437 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4438 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4440 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4441 zfs_sa_upgrade_txholds(tx, zp);
4442 error = dmu_tx_assign(tx, TXG_WAIT);
4443 if (error) {
4444 dmu_tx_abort(tx);
4445 } else {
4446 mutex_enter(&zp->z_lock);
4447 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4448 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4449 zp->z_atime_dirty = 0;
4450 mutex_exit(&zp->z_lock);
4451 dmu_tx_commit(tx);
4455 zfs_zinactive(zp);
4456 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4460 * Bounds-check the seek operation.
4462 * IN: vp - vnode seeking within
4463 * ooff - old file offset
4464 * noffp - pointer to new file offset
4465 * ct - caller context
4467 * RETURN: 0 on success, EINVAL if new offset invalid.
4469 /* ARGSUSED */
4470 static int
4471 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4472 caller_context_t *ct)
4474 if (vp->v_type == VDIR)
4475 return (0);
4476 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4480 * Pre-filter the generic locking function to trap attempts to place
4481 * a mandatory lock on a memory mapped file.
4483 static int
4484 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4485 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4487 znode_t *zp = VTOZ(vp);
4488 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4490 ZFS_ENTER(zfsvfs);
4491 ZFS_VERIFY_ZP(zp);
4494 * We are following the UFS semantics with respect to mapcnt
4495 * here: If we see that the file is mapped already, then we will
4496 * return an error, but we don't worry about races between this
4497 * function and zfs_map().
4499 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4500 ZFS_EXIT(zfsvfs);
4501 return (SET_ERROR(EAGAIN));
4503 ZFS_EXIT(zfsvfs);
4504 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4508 * If we can't find a page in the cache, we will create a new page
4509 * and fill it with file data. For efficiency, we may try to fill
4510 * multiple pages at once (klustering) to fill up the supplied page
4511 * list. Note that the pages to be filled are held with an exclusive
4512 * lock to prevent access by other threads while they are being filled.
4514 static int
4515 zfs_fillpage(vnode_t *vp, uoff_t off, struct seg *seg,
4516 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4518 znode_t *zp = VTOZ(vp);
4519 page_t *pp, *cur_pp;
4520 objset_t *os = zp->z_zfsvfs->z_os;
4521 uoff_t io_off, total;
4522 size_t io_len;
4523 int err;
4525 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4527 * We only have a single page, don't bother klustering
4529 io_off = off;
4530 io_len = PAGESIZE;
4531 pp = page_create_va(&vp->v_object, io_off, io_len,
4532 PG_EXCL | PG_WAIT, seg, addr);
4533 } else {
4535 * Try to find enough pages to fill the page list
4537 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4538 &io_len, off, plsz, 0);
4540 if (pp == NULL) {
4542 * The page already exists, nothing to do here.
4544 *pl = NULL;
4545 return (0);
4549 * Fill the pages in the kluster.
4551 cur_pp = pp;
4552 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4553 caddr_t va;
4555 ASSERT3U(io_off, ==, cur_pp->p_offset);
4556 va = zfs_map_page(cur_pp, S_WRITE);
4557 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4558 DMU_READ_PREFETCH);
4559 zfs_unmap_page(cur_pp, va);
4560 if (err) {
4561 /* On error, toss the entire kluster */
4562 pvn_read_done(pp, B_ERROR);
4563 /* convert checksum errors into IO errors */
4564 if (err == ECKSUM)
4565 err = SET_ERROR(EIO);
4566 return (err);
4568 cur_pp = cur_pp->p_next;
4572 * Fill in the page list array from the kluster starting
4573 * from the desired offset `off'.
4574 * NOTE: the page list will always be null terminated.
4576 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4577 ASSERT(pl == NULL || (*pl)->p_offset == off);
4579 return (0);
4583 * Return pointers to the pages for the file region [off, off + len]
4584 * in the pl array. If plsz is greater than len, this function may
4585 * also return page pointers from after the specified region
4586 * (i.e. the region [off, off + plsz]). These additional pages are
4587 * only returned if they are already in the cache, or were created as
4588 * part of a klustered read.
4590 * IN: vp - vnode of file to get data from.
4591 * off - position in file to get data from.
4592 * len - amount of data to retrieve.
4593 * plsz - length of provided page list.
4594 * seg - segment to obtain pages for.
4595 * addr - virtual address of fault.
4596 * rw - mode of created pages.
4597 * cr - credentials of caller.
4598 * ct - caller context.
4600 * OUT: protp - protection mode of created pages.
4601 * pl - list of pages created.
4603 * RETURN: 0 on success, error code on failure.
4605 * Timestamps:
4606 * vp - atime updated
4608 /* ARGSUSED */
4609 static int
4610 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4611 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4612 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4614 znode_t *zp = VTOZ(vp);
4615 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4616 page_t **pl0 = pl;
4617 int err = 0;
4619 /* we do our own caching, faultahead is unnecessary */
4620 if (pl == NULL)
4621 return (0);
4622 else if (len > plsz)
4623 len = plsz;
4624 else
4625 len = P2ROUNDUP(len, PAGESIZE);
4626 ASSERT(plsz >= len);
4628 ZFS_ENTER(zfsvfs);
4629 ZFS_VERIFY_ZP(zp);
4631 if (protp)
4632 *protp = PROT_ALL;
4635 * Loop through the requested range [off, off + len) looking
4636 * for pages. If we don't find a page, we will need to create
4637 * a new page and fill it with data from the file.
4639 while (len > 0) {
4640 if (*pl = page_lookup(&vp->v_object, off, SE_SHARED))
4641 *(pl+1) = NULL;
4642 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4643 goto out;
4644 while (*pl) {
4645 ASSERT3U((*pl)->p_offset, ==, off);
4646 off += PAGESIZE;
4647 addr += PAGESIZE;
4648 if (len > 0) {
4649 ASSERT3U(len, >=, PAGESIZE);
4650 len -= PAGESIZE;
4652 ASSERT3U(plsz, >=, PAGESIZE);
4653 plsz -= PAGESIZE;
4654 pl++;
4659 * Fill out the page array with any pages already in the cache.
4661 while (plsz > 0 &&
4662 (*pl++ = page_lookup_nowait(&vp->v_object, off, SE_SHARED))) {
4663 off += PAGESIZE;
4664 plsz -= PAGESIZE;
4666 out:
4667 if (err) {
4669 * Release any pages we have previously locked.
4671 while (pl > pl0)
4672 page_unlock(*--pl);
4673 } else {
4674 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4677 *pl = NULL;
4679 ZFS_EXIT(zfsvfs);
4680 return (err);
4684 * Request a memory map for a section of a file. This code interacts
4685 * with common code and the VM system as follows:
4687 * - common code calls mmap(), which ends up in smmap_common()
4688 * - this calls fop_map(), which takes you into (say) zfs
4689 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4690 * - segvn_create() creates the new segment and calls fop_addmap()
4691 * - zfs_addmap() updates z_mapcnt
4693 /*ARGSUSED*/
4694 static int
4695 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4696 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4697 caller_context_t *ct)
4699 znode_t *zp = VTOZ(vp);
4700 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4701 segvn_crargs_t vn_a;
4702 int error;
4704 ZFS_ENTER(zfsvfs);
4705 ZFS_VERIFY_ZP(zp);
4708 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
4711 if ((prot & PROT_WRITE) && (zp->z_pflags &
4712 (ZFS_IMMUTABLE | ZFS_APPENDONLY))) {
4713 ZFS_EXIT(zfsvfs);
4714 return (SET_ERROR(EPERM));
4717 if ((prot & (PROT_READ | PROT_EXEC)) &&
4718 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4719 ZFS_EXIT(zfsvfs);
4720 return (SET_ERROR(EACCES));
4723 if (vp->v_flag & VNOMAP) {
4724 ZFS_EXIT(zfsvfs);
4725 return (SET_ERROR(ENOSYS));
4728 if (off < 0 || len > MAXOFFSET_T - off) {
4729 ZFS_EXIT(zfsvfs);
4730 return (SET_ERROR(ENXIO));
4733 if (vp->v_type != VREG) {
4734 ZFS_EXIT(zfsvfs);
4735 return (SET_ERROR(ENODEV));
4739 * If file is locked, disallow mapping.
4741 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4742 ZFS_EXIT(zfsvfs);
4743 return (SET_ERROR(EAGAIN));
4746 as_rangelock(as);
4747 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4748 if (error != 0) {
4749 as_rangeunlock(as);
4750 ZFS_EXIT(zfsvfs);
4751 return (error);
4754 vn_a.vp = vp;
4755 vn_a.offset = (uoff_t)off;
4756 vn_a.type = flags & MAP_TYPE;
4757 vn_a.prot = prot;
4758 vn_a.maxprot = maxprot;
4759 vn_a.cred = cr;
4760 vn_a.amp = NULL;
4761 vn_a.flags = flags & ~MAP_TYPE;
4762 vn_a.szc = 0;
4763 vn_a.lgrp_mem_policy_flags = 0;
4765 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4767 as_rangeunlock(as);
4768 ZFS_EXIT(zfsvfs);
4769 return (error);
4772 /* ARGSUSED */
4773 static int
4774 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4775 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4776 caller_context_t *ct)
4778 uint64_t pages = btopr(len);
4780 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4781 return (0);
4785 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4786 * more accurate mtime for the associated file. Since we don't have a way of
4787 * detecting when the data was actually modified, we have to resort to
4788 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4789 * last page is pushed. The problem occurs when the msync() call is omitted,
4790 * which by far the most common case:
4792 * open()
4793 * mmap()
4794 * <modify memory>
4795 * munmap()
4796 * close()
4797 * <time lapse>
4798 * putpage() via fsflush
4800 * If we wait until fsflush to come along, we can have a modification time that
4801 * is some arbitrary point in the future. In order to prevent this in the
4802 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4803 * torn down.
4805 /* ARGSUSED */
4806 static int
4807 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4808 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4809 caller_context_t *ct)
4811 uint64_t pages = btopr(len);
4813 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4814 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4816 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4817 vn_has_cached_data(vp))
4818 (void) fop_putpage(vp, off, len, B_ASYNC, cr, ct);
4820 return (0);
4824 * Free or allocate space in a file. Currently, this function only
4825 * supports the `F_FREESP' command. However, this command is somewhat
4826 * misnamed, as its functionality includes the ability to allocate as
4827 * well as free space.
4829 * IN: vp - vnode of file to free data in.
4830 * cmd - action to take (only F_FREESP supported).
4831 * bfp - section of file to free/alloc.
4832 * flag - current file open mode flags.
4833 * offset - current file offset.
4834 * cr - credentials of caller [UNUSED].
4835 * ct - caller context.
4837 * RETURN: 0 on success, error code on failure.
4839 * Timestamps:
4840 * vp - ctime|mtime updated
4842 /* ARGSUSED */
4843 static int
4844 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4845 offset_t offset, cred_t *cr, caller_context_t *ct)
4847 znode_t *zp = VTOZ(vp);
4848 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4849 uint64_t off, len;
4850 int error;
4852 ZFS_ENTER(zfsvfs);
4853 ZFS_VERIFY_ZP(zp);
4855 if (cmd != F_FREESP) {
4856 ZFS_EXIT(zfsvfs);
4857 return (SET_ERROR(EINVAL));
4861 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4862 * callers might not be able to detect properly that we are read-only,
4863 * so check it explicitly here.
4865 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4866 ZFS_EXIT(zfsvfs);
4867 return (SET_ERROR(EROFS));
4870 if (error = convoff(vp, bfp, 0, offset)) {
4871 ZFS_EXIT(zfsvfs);
4872 return (error);
4875 if (bfp->l_len < 0) {
4876 ZFS_EXIT(zfsvfs);
4877 return (SET_ERROR(EINVAL));
4880 off = bfp->l_start;
4881 len = bfp->l_len; /* 0 means from off to end of file */
4883 error = zfs_freesp(zp, off, len, flag, TRUE);
4885 if (error == 0 && off == 0 && len == 0)
4886 vnevent_truncate(ZTOV(zp), ct);
4888 ZFS_EXIT(zfsvfs);
4889 return (error);
4892 /*ARGSUSED*/
4893 static int
4894 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4896 znode_t *zp = VTOZ(vp);
4897 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4898 uint32_t gen;
4899 uint64_t gen64;
4900 uint64_t object = zp->z_id;
4901 zfid_short_t *zfid;
4902 int size, i, error;
4904 ZFS_ENTER(zfsvfs);
4905 ZFS_VERIFY_ZP(zp);
4907 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4908 &gen64, sizeof (uint64_t))) != 0) {
4909 ZFS_EXIT(zfsvfs);
4910 return (error);
4913 gen = (uint32_t)gen64;
4915 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4916 if (fidp->fid_len < size) {
4917 fidp->fid_len = size;
4918 ZFS_EXIT(zfsvfs);
4919 return (SET_ERROR(ENOSPC));
4922 zfid = (zfid_short_t *)fidp;
4924 zfid->zf_len = size;
4926 for (i = 0; i < sizeof (zfid->zf_object); i++)
4927 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4929 /* Must have a non-zero generation number to distinguish from .zfs */
4930 if (gen == 0)
4931 gen = 1;
4932 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4933 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4935 if (size == LONG_FID_LEN) {
4936 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4937 zfid_long_t *zlfid;
4939 zlfid = (zfid_long_t *)fidp;
4941 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4942 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4944 /* XXX - this should be the generation number for the objset */
4945 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4946 zlfid->zf_setgen[i] = 0;
4949 ZFS_EXIT(zfsvfs);
4950 return (0);
4953 static int
4954 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4955 caller_context_t *ct)
4957 znode_t *zp, *xzp;
4958 zfsvfs_t *zfsvfs;
4959 zfs_dirlock_t *dl;
4960 int error;
4962 switch (cmd) {
4963 case _PC_LINK_MAX:
4964 *valp = ULONG_MAX;
4965 return (0);
4967 case _PC_FILESIZEBITS:
4968 *valp = 64;
4969 return (0);
4971 case _PC_XATTR_EXISTS:
4972 zp = VTOZ(vp);
4973 zfsvfs = zp->z_zfsvfs;
4974 ZFS_ENTER(zfsvfs);
4975 ZFS_VERIFY_ZP(zp);
4976 *valp = 0;
4977 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4978 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4979 if (error == 0) {
4980 zfs_dirent_unlock(dl);
4981 if (!zfs_dirempty(xzp))
4982 *valp = 1;
4983 VN_RELE(ZTOV(xzp));
4984 } else if (error == ENOENT) {
4986 * If there aren't extended attributes, it's the
4987 * same as having zero of them.
4989 error = 0;
4991 ZFS_EXIT(zfsvfs);
4992 return (error);
4994 case _PC_SATTR_ENABLED:
4995 case _PC_SATTR_EXISTS:
4996 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4997 (vp->v_type == VREG || vp->v_type == VDIR);
4998 return (0);
5000 case _PC_ACCESS_FILTERING:
5001 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5002 vp->v_type == VDIR;
5003 return (0);
5005 case _PC_ACL_ENABLED:
5006 *valp = _ACL_ACE_ENABLED;
5007 return (0);
5009 case _PC_MIN_HOLE_SIZE:
5010 *valp = (ulong_t)SPA_MINBLOCKSIZE;
5011 return (0);
5013 case _PC_TIMESTAMP_RESOLUTION:
5014 /* nanosecond timestamp resolution */
5015 *valp = 1L;
5016 return (0);
5018 default:
5019 return (fs_pathconf(vp, cmd, valp, cr, ct));
5023 /*ARGSUSED*/
5024 static int
5025 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5026 caller_context_t *ct)
5028 znode_t *zp = VTOZ(vp);
5029 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5030 int error;
5031 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5033 ZFS_ENTER(zfsvfs);
5034 ZFS_VERIFY_ZP(zp);
5035 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5036 ZFS_EXIT(zfsvfs);
5038 return (error);
5041 /*ARGSUSED*/
5042 static int
5043 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5044 caller_context_t *ct)
5046 znode_t *zp = VTOZ(vp);
5047 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5048 int error;
5049 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5050 zilog_t *zilog = zfsvfs->z_log;
5052 ZFS_ENTER(zfsvfs);
5053 ZFS_VERIFY_ZP(zp);
5055 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5057 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5058 zil_commit(zilog, 0);
5060 ZFS_EXIT(zfsvfs);
5061 return (error);
5065 * The smallest read we may consider to loan out an arcbuf.
5066 * This must be a power of 2.
5068 int zcr_blksz_min = (1 << 10); /* 1K */
5070 * If set to less than the file block size, allow loaning out of an
5071 * arcbuf for a partial block read. This must be a power of 2.
5073 int zcr_blksz_max = (1 << 17); /* 128K */
5075 /*ARGSUSED*/
5076 static int
5077 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5078 caller_context_t *ct)
5080 znode_t *zp = VTOZ(vp);
5081 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5082 int max_blksz = zfsvfs->z_max_blksz;
5083 uio_t *uio = &xuio->xu_uio;
5084 ssize_t size = uio->uio_resid;
5085 offset_t offset = uio->uio_loffset;
5086 int blksz;
5087 int fullblk, i;
5088 arc_buf_t *abuf;
5089 ssize_t maxsize;
5090 int preamble, postamble;
5092 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5093 return (SET_ERROR(EINVAL));
5095 ZFS_ENTER(zfsvfs);
5096 ZFS_VERIFY_ZP(zp);
5097 switch (ioflag) {
5098 case UIO_WRITE:
5100 * Loan out an arc_buf for write if write size is bigger than
5101 * max_blksz, and the file's block size is also max_blksz.
5103 blksz = max_blksz;
5104 if (size < blksz || zp->z_blksz != blksz) {
5105 ZFS_EXIT(zfsvfs);
5106 return (SET_ERROR(EINVAL));
5109 * Caller requests buffers for write before knowing where the
5110 * write offset might be (e.g. NFS TCP write).
5112 if (offset == -1) {
5113 preamble = 0;
5114 } else {
5115 preamble = P2PHASE(offset, blksz);
5116 if (preamble) {
5117 preamble = blksz - preamble;
5118 size -= preamble;
5122 postamble = P2PHASE(size, blksz);
5123 size -= postamble;
5125 fullblk = size / blksz;
5126 (void) dmu_xuio_init(xuio,
5127 (preamble != 0) + fullblk + (postamble != 0));
5128 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5129 int, postamble, int,
5130 (preamble != 0) + fullblk + (postamble != 0));
5133 * Have to fix iov base/len for partial buffers. They
5134 * currently represent full arc_buf's.
5136 if (preamble) {
5137 /* data begins in the middle of the arc_buf */
5138 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5139 blksz);
5140 ASSERT(abuf);
5141 (void) dmu_xuio_add(xuio, abuf,
5142 blksz - preamble, preamble);
5145 for (i = 0; i < fullblk; i++) {
5146 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5147 blksz);
5148 ASSERT(abuf);
5149 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5152 if (postamble) {
5153 /* data ends in the middle of the arc_buf */
5154 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5155 blksz);
5156 ASSERT(abuf);
5157 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5159 break;
5160 case UIO_READ:
5162 * Loan out an arc_buf for read if the read size is larger than
5163 * the current file block size. Block alignment is not
5164 * considered. Partial arc_buf will be loaned out for read.
5166 blksz = zp->z_blksz;
5167 if (blksz < zcr_blksz_min)
5168 blksz = zcr_blksz_min;
5169 if (blksz > zcr_blksz_max)
5170 blksz = zcr_blksz_max;
5171 /* avoid potential complexity of dealing with it */
5172 if (blksz > max_blksz) {
5173 ZFS_EXIT(zfsvfs);
5174 return (SET_ERROR(EINVAL));
5177 maxsize = zp->z_size - uio->uio_loffset;
5178 if (size > maxsize)
5179 size = maxsize;
5181 if (size < blksz || vn_has_cached_data(vp)) {
5182 ZFS_EXIT(zfsvfs);
5183 return (SET_ERROR(EINVAL));
5185 break;
5186 default:
5187 ZFS_EXIT(zfsvfs);
5188 return (SET_ERROR(EINVAL));
5191 uio->uio_extflg = UIO_XUIO;
5192 XUIO_XUZC_RW(xuio) = ioflag;
5193 ZFS_EXIT(zfsvfs);
5194 return (0);
5197 /*ARGSUSED*/
5198 static int
5199 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5201 int i;
5202 arc_buf_t *abuf;
5203 int ioflag = XUIO_XUZC_RW(xuio);
5205 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5207 i = dmu_xuio_cnt(xuio);
5208 while (i-- > 0) {
5209 abuf = dmu_xuio_arcbuf(xuio, i);
5211 * if abuf == NULL, it must be a write buffer
5212 * that has been returned in zfs_write().
5214 if (abuf)
5215 dmu_return_arcbuf(abuf);
5216 ASSERT(abuf || ioflag == UIO_WRITE);
5219 dmu_xuio_fini(xuio);
5220 return (0);
5224 * Predeclare these here so that the compiler assumes that
5225 * this is an "old style" function declaration that does
5226 * not include arguments => we won't get type mismatch errors
5227 * in the initializations that follow.
5229 static int zfs_inval();
5230 static int zfs_isdir();
5232 static int
5233 zfs_inval()
5235 return (SET_ERROR(EINVAL));
5238 static int
5239 zfs_isdir()
5241 return (SET_ERROR(EISDIR));
5245 * Directory vnode operations
5247 const struct vnodeops zfs_dvnodeops = {
5248 .vnop_name = "zfs",
5249 .vop_open = zfs_open,
5250 .vop_close = zfs_close,
5251 .vop_read = zfs_isdir,
5252 .vop_write = zfs_isdir,
5253 .vop_ioctl = zfs_ioctl,
5254 .vop_getattr = zfs_getattr,
5255 .vop_setattr = zfs_setattr,
5256 .vop_access = zfs_access,
5257 .vop_lookup = zfs_lookup,
5258 .vop_create = zfs_create,
5259 .vop_remove = zfs_remove,
5260 .vop_link = zfs_link,
5261 .vop_rename = zfs_rename,
5262 .vop_mkdir = zfs_mkdir,
5263 .vop_rmdir = zfs_rmdir,
5264 .vop_readdir = zfs_readdir,
5265 .vop_symlink = zfs_symlink,
5266 .vop_fsync = zfs_fsync,
5267 .vop_inactive = zfs_inactive,
5268 .vop_fid = zfs_fid,
5269 .vop_seek = zfs_seek,
5270 .vop_pathconf = zfs_pathconf,
5271 .vop_getsecattr = zfs_getsecattr,
5272 .vop_setsecattr = zfs_setsecattr,
5273 .vop_vnevent = fs_vnevent_support,
5277 * Regular file vnode operations
5279 const struct vnodeops zfs_fvnodeops = {
5280 .vnop_name = "zfs",
5281 .vop_open = zfs_open,
5282 .vop_close = zfs_close,
5283 .vop_read = zfs_read,
5284 .vop_write = zfs_write,
5285 .vop_ioctl = zfs_ioctl,
5286 .vop_getattr = zfs_getattr,
5287 .vop_setattr = zfs_setattr,
5288 .vop_access = zfs_access,
5289 .vop_lookup = zfs_lookup,
5290 .vop_rename = zfs_rename,
5291 .vop_fsync = zfs_fsync,
5292 .vop_inactive = zfs_inactive,
5293 .vop_fid = zfs_fid,
5294 .vop_seek = zfs_seek,
5295 .vop_frlock = zfs_frlock,
5296 .vop_space = zfs_space,
5297 .vop_getpage = zfs_getpage,
5298 .vop_putpage = zfs_putpage,
5299 .vop_map = zfs_map,
5300 .vop_addmap = zfs_addmap,
5301 .vop_delmap = zfs_delmap,
5302 .vop_pathconf = zfs_pathconf,
5303 .vop_getsecattr = zfs_getsecattr,
5304 .vop_setsecattr = zfs_setsecattr,
5305 .vop_vnevent = fs_vnevent_support,
5306 .vop_reqzcbuf = zfs_reqzcbuf,
5307 .vop_retzcbuf = zfs_retzcbuf,
5311 * Symbolic link vnode operations
5313 const struct vnodeops zfs_symvnodeops = {
5314 .vnop_name = "zfs",
5315 .vop_getattr = zfs_getattr,
5316 .vop_setattr = zfs_setattr,
5317 .vop_access = zfs_access,
5318 .vop_rename = zfs_rename,
5319 .vop_readlink = zfs_readlink,
5320 .vop_inactive = zfs_inactive,
5321 .vop_fid = zfs_fid,
5322 .vop_pathconf = zfs_pathconf,
5323 .vop_vnevent = fs_vnevent_support,
5327 * special share hidden files vnode operations
5329 const struct vnodeops zfs_sharevnodeops = {
5330 .vnop_name = "zfs",
5331 .vop_getattr = zfs_getattr,
5332 .vop_access = zfs_access,
5333 .vop_inactive = zfs_inactive,
5334 .vop_fid = zfs_fid,
5335 .vop_pathconf = zfs_pathconf,
5336 .vop_getsecattr = zfs_getsecattr,
5337 .vop_setsecattr = zfs_setsecattr,
5338 .vop_vnevent = fs_vnevent_support,
5342 * Extended attribute directory vnode operations
5344 * These ops are identical to the directory vnode
5345 * operations except for restricted operations:
5346 * fop_mkdir()
5347 * fop_symlink()
5349 * Note that there are other restrictions embedded in:
5350 * zfs_create() - restrict type to VREG
5351 * zfs_link() - no links into/out of attribute space
5352 * zfs_rename() - no moves into/out of attribute space
5354 const struct vnodeops zfs_xdvnodeops = {
5355 .vnop_name = "zfs",
5356 .vop_open = zfs_open,
5357 .vop_close = zfs_close,
5358 .vop_ioctl = zfs_ioctl,
5359 .vop_getattr = zfs_getattr,
5360 .vop_setattr = zfs_setattr,
5361 .vop_access = zfs_access,
5362 .vop_lookup = zfs_lookup,
5363 .vop_create = zfs_create,
5364 .vop_remove = zfs_remove,
5365 .vop_link = zfs_link,
5366 .vop_rename = zfs_rename,
5367 .vop_mkdir = zfs_inval,
5368 .vop_rmdir = zfs_rmdir,
5369 .vop_readdir = zfs_readdir,
5370 .vop_symlink = zfs_inval,
5371 .vop_fsync = zfs_fsync,
5372 .vop_inactive = zfs_inactive,
5373 .vop_fid = zfs_fid,
5374 .vop_seek = zfs_seek,
5375 .vop_pathconf = zfs_pathconf,
5376 .vop_getsecattr = zfs_getsecattr,
5377 .vop_setsecattr = zfs_setsecattr,
5378 .vop_vnevent = fs_vnevent_support,
5382 * Error vnode operations
5384 const struct vnodeops zfs_evnodeops = {
5385 .vnop_name = "zfs",
5386 .vop_inactive = zfs_inactive,
5387 .vop_pathconf = zfs_pathconf,