Merge commit '5cabbc6b49070407fb9610cfe73d4c0e0dea3e77' into merges
[unleashed.git] / kernel / fs / zfs / zfs_vnops.c
blob89ae03ca595e9e7b301e5ed5370858c1f6ced437
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_WAITED rather than TXG_NOWAIT,
138 * to indicate that this operation has already called dmu_tx_wait().
139 * This will ensure that we don't retry forever, waiting a short bit
140 * each time.
142 * (5) If the operation succeeded, generate the intent log entry for it
143 * before dropping locks. This ensures that the ordering of events
144 * in the intent log matches the order in which they actually occurred.
145 * During ZIL replay the zfs_log_* functions will update the sequence
146 * number to indicate the zil transaction has replayed.
148 * (6) At the end of each vnode op, the DMU tx must always commit,
149 * regardless of whether there were any errors.
151 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
152 * to ensure that synchronous semantics are provided when necessary.
154 * In general, this is how things should be ordered in each vnode op:
156 * ZFS_ENTER(zfsvfs); // exit if unmounted
157 * top:
158 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
159 * rw_enter(...); // grab any other locks you need
160 * tx = dmu_tx_create(...); // get DMU tx
161 * dmu_tx_hold_*(); // hold each object you might modify
162 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
163 * if (error) {
164 * rw_exit(...); // drop locks
165 * zfs_dirent_unlock(dl); // unlock directory entry
166 * VN_RELE(...); // release held vnodes
167 * if (error == ERESTART) {
168 * waited = B_TRUE;
169 * dmu_tx_wait(tx);
170 * dmu_tx_abort(tx);
171 * goto top;
173 * dmu_tx_abort(tx); // abort DMU tx
174 * ZFS_EXIT(zfsvfs); // finished in zfs
175 * return (error); // really out of space
177 * error = do_real_work(); // do whatever this VOP does
178 * if (error == 0)
179 * zfs_log_*(...); // on success, make ZIL entry
180 * dmu_tx_commit(tx); // commit DMU tx -- error or not
181 * rw_exit(...); // drop locks
182 * zfs_dirent_unlock(dl); // unlock directory entry
183 * VN_RELE(...); // release held vnodes
184 * zil_commit(zilog, foid); // synchronous when necessary
185 * ZFS_EXIT(zfsvfs); // finished in zfs
186 * return (error); // done, report error
189 /* ARGSUSED */
190 static int
191 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
193 znode_t *zp = VTOZ(*vpp);
194 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
196 ZFS_ENTER(zfsvfs);
197 ZFS_VERIFY_ZP(zp);
199 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
200 ((flag & FAPPEND) == 0)) {
201 ZFS_EXIT(zfsvfs);
202 return (SET_ERROR(EPERM));
205 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
206 ZTOV(zp)->v_type == VREG &&
207 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
208 if (fs_vscan(*vpp, cr, 0) != 0) {
209 ZFS_EXIT(zfsvfs);
210 return (SET_ERROR(EACCES));
214 /* Keep a count of the synchronous opens in the znode */
215 if (flag & (FSYNC | FDSYNC))
216 atomic_inc_32(&zp->z_sync_cnt);
218 ZFS_EXIT(zfsvfs);
219 return (0);
222 /* ARGSUSED */
223 static int
224 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
225 caller_context_t *ct)
227 znode_t *zp = VTOZ(vp);
228 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
231 * Clean up any locks held by this process on the vp.
233 cleanlocks(vp, ddi_get_pid(), 0);
234 cleanshares(vp, ddi_get_pid());
236 ZFS_ENTER(zfsvfs);
237 ZFS_VERIFY_ZP(zp);
239 /* Decrement the synchronous opens in the znode */
240 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
241 atomic_dec_32(&zp->z_sync_cnt);
243 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
244 ZTOV(zp)->v_type == VREG &&
245 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
246 VERIFY(fs_vscan(vp, cr, 1) == 0);
248 ZFS_EXIT(zfsvfs);
249 return (0);
253 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
254 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
256 static int
257 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
259 znode_t *zp = VTOZ(vp);
260 uint64_t noff = (uint64_t)*off; /* new offset */
261 uint64_t file_sz;
262 int error;
263 boolean_t hole;
265 file_sz = zp->z_size;
266 if (noff >= file_sz) {
267 return (SET_ERROR(ENXIO));
270 if (cmd == _FIO_SEEK_HOLE)
271 hole = B_TRUE;
272 else
273 hole = B_FALSE;
275 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
277 if (error == ESRCH)
278 return (SET_ERROR(ENXIO));
281 * We could find a hole that begins after the logical end-of-file,
282 * because dmu_offset_next() only works on whole blocks. If the
283 * EOF falls mid-block, then indicate that the "virtual hole"
284 * at the end of the file begins at the logical EOF, rather than
285 * at the end of the last block.
287 if (noff > file_sz) {
288 ASSERT(hole);
289 noff = file_sz;
292 if (noff < *off)
293 return (error);
294 *off = noff;
295 return (error);
298 /* ARGSUSED */
299 static int
300 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
301 int *rvalp, caller_context_t *ct)
303 offset_t off;
304 offset_t ndata;
305 dmu_object_info_t doi;
306 int error;
307 zfsvfs_t *zfsvfs;
308 znode_t *zp;
310 switch (com) {
311 case _FIOFFS:
313 return (zfs_sync(vp->v_vfsp, 0, cred));
316 * The following two ioctls are used by bfu. Faking out,
317 * necessary to avoid bfu errors.
320 case _FIOGDIO:
321 case _FIOSDIO:
323 return (0);
326 case _FIO_SEEK_DATA:
327 case _FIO_SEEK_HOLE:
329 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
330 return (SET_ERROR(EFAULT));
332 zp = VTOZ(vp);
333 zfsvfs = zp->z_zfsvfs;
334 ZFS_ENTER(zfsvfs);
335 ZFS_VERIFY_ZP(zp);
337 /* offset parameter is in/out */
338 error = zfs_holey(vp, com, &off);
339 ZFS_EXIT(zfsvfs);
340 if (error)
341 return (error);
342 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
343 return (SET_ERROR(EFAULT));
344 return (0);
346 case _FIO_COUNT_FILLED:
349 * _FIO_COUNT_FILLED adds a new ioctl command which
350 * exposes the number of filled blocks in a
351 * ZFS object.
353 zp = VTOZ(vp);
354 zfsvfs = zp->z_zfsvfs;
355 ZFS_ENTER(zfsvfs);
356 ZFS_VERIFY_ZP(zp);
359 * Wait for all dirty blocks for this object
360 * to get synced out to disk, and the DMU info
361 * updated.
363 error = dmu_object_wait_synced(zfsvfs->z_os, zp->z_id);
364 if (error) {
365 ZFS_EXIT(zfsvfs);
366 return (error);
370 * Retrieve fill count from DMU object.
372 error = dmu_object_info(zfsvfs->z_os, zp->z_id, &doi);
373 if (error) {
374 ZFS_EXIT(zfsvfs);
375 return (error);
378 ndata = doi.doi_fill_count;
380 ZFS_EXIT(zfsvfs);
381 if (ddi_copyout(&ndata, (void *)data, sizeof (ndata), flag))
382 return (SET_ERROR(EFAULT));
383 return (0);
386 return (SET_ERROR(ENOTTY));
390 * Utility functions to map and unmap a single physical page. These
391 * are used to manage the mappable copies of ZFS file data, and therefore
392 * do not update ref/mod bits.
394 caddr_t
395 zfs_map_page(page_t *pp, enum seg_rw rw)
397 if (kpm_enable)
398 return (hat_kpm_mapin(pp, 0));
399 ASSERT(rw == S_READ || rw == S_WRITE);
400 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
401 (caddr_t)-1));
404 void
405 zfs_unmap_page(page_t *pp, caddr_t addr)
407 if (kpm_enable) {
408 hat_kpm_mapout(pp, 0, addr);
409 } else {
410 ppmapout(addr);
415 * When a file is memory mapped, we must keep the IO data synchronized
416 * between the DMU cache and the memory mapped pages. What this means:
418 * On Write: If we find a memory mapped page, we write to *both*
419 * the page and the dmu buffer.
421 static void
422 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
424 int64_t off;
426 off = start & PAGEOFFSET;
427 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
428 page_t *pp;
429 uint64_t nbytes = MIN(PAGESIZE - off, len);
431 if (pp = page_lookup(&vp->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 rl_t *rl;
516 xuio_t *xuio = NULL;
518 ZFS_ENTER(zfsvfs);
519 ZFS_VERIFY_ZP(zp);
521 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
522 ZFS_EXIT(zfsvfs);
523 return (SET_ERROR(EACCES));
527 * Validate file offset
529 if (uio->uio_loffset < 0) {
530 ZFS_EXIT(zfsvfs);
531 return (SET_ERROR(EINVAL));
535 * Fasttrack empty reads
537 if (uio->uio_resid == 0) {
538 ZFS_EXIT(zfsvfs);
539 return (0);
543 * Check for mandatory locks
545 if (MANDMODE(zp->z_mode)) {
546 if (error = chklock(vp, FREAD,
547 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
548 ZFS_EXIT(zfsvfs);
549 return (error);
554 * If we're in FRSYNC mode, sync out this znode before reading it.
556 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
557 zil_commit(zfsvfs->z_log, zp->z_id);
560 * Lock the range against changes.
562 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
565 * If we are reading past end-of-file we can skip
566 * to the end; but we might still need to set atime.
568 if (uio->uio_loffset >= zp->z_size) {
569 error = 0;
570 goto out;
573 ASSERT(uio->uio_loffset < zp->z_size);
574 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
576 if ((uio->uio_extflg == UIO_XUIO) &&
577 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
578 int nblk;
579 int blksz = zp->z_blksz;
580 uint64_t offset = uio->uio_loffset;
582 xuio = (xuio_t *)uio;
583 if ((ISP2(blksz))) {
584 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
585 blksz)) / blksz;
586 } else {
587 ASSERT(offset + n <= blksz);
588 nblk = 1;
590 (void) dmu_xuio_init(xuio, nblk);
592 if (vn_has_cached_data(vp)) {
594 * For simplicity, we always allocate a full buffer
595 * even if we only expect to read a portion of a block.
597 while (--nblk >= 0) {
598 (void) dmu_xuio_add(xuio,
599 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
600 blksz), 0, blksz);
605 while (n > 0) {
606 nbytes = MIN(n, zfs_read_chunk_size -
607 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
609 if (vn_has_cached_data(vp)) {
610 error = mappedread(vp, nbytes, uio);
611 } else {
612 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl),
613 uio, nbytes);
615 if (error) {
616 /* convert checksum errors into IO errors */
617 if (error == ECKSUM)
618 error = SET_ERROR(EIO);
619 break;
622 n -= nbytes;
624 out:
625 zfs_range_unlock(rl);
627 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
628 ZFS_EXIT(zfsvfs);
629 return (error);
633 * Write the bytes to a file.
635 * IN: vp - vnode of file to be written to.
636 * uio - structure supplying write location, range info,
637 * and data buffer.
638 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
639 * set if in append mode.
640 * cr - credentials of caller.
641 * ct - caller context (NFS/CIFS fem monitor only)
643 * OUT: uio - updated offset and range.
645 * RETURN: 0 on success, error code on failure.
647 * Timestamps:
648 * vp - ctime|mtime updated if byte count > 0
651 /* ARGSUSED */
652 static int
653 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
655 znode_t *zp = VTOZ(vp);
656 rlim64_t limit = uio->uio_llimit;
657 ssize_t start_resid = uio->uio_resid;
658 ssize_t tx_bytes;
659 uint64_t end_size;
660 dmu_tx_t *tx;
661 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
662 zilog_t *zilog;
663 offset_t woff;
664 ssize_t n, nbytes;
665 rl_t *rl;
666 int max_blksz = zfsvfs->z_max_blksz;
667 int error = 0;
668 arc_buf_t *abuf;
669 iovec_t *aiov = NULL;
670 xuio_t *xuio = NULL;
671 int i_iov = 0;
672 int iovcnt = uio->uio_iovcnt;
673 iovec_t *iovp = uio->uio_iov;
674 int write_eof;
675 int count = 0;
676 sa_bulk_attr_t bulk[4];
677 uint64_t mtime[2], ctime[2];
680 * Fasttrack empty write
682 n = start_resid;
683 if (n == 0)
684 return (0);
686 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
687 limit = MAXOFFSET_T;
689 ZFS_ENTER(zfsvfs);
690 ZFS_VERIFY_ZP(zp);
692 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
693 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
694 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
695 &zp->z_size, 8);
696 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
697 &zp->z_pflags, 8);
700 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
701 * callers might not be able to detect properly that we are read-only,
702 * so check it explicitly here.
704 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
705 ZFS_EXIT(zfsvfs);
706 return (SET_ERROR(EROFS));
710 * If immutable or not appending then return EPERM.
711 * Intentionally allow ZFS_READONLY through here.
712 * See zfs_zaccess_common()
714 if ((zp->z_pflags & ZFS_IMMUTABLE) ||
715 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
716 (uio->uio_loffset < zp->z_size))) {
717 ZFS_EXIT(zfsvfs);
718 return (SET_ERROR(EPERM));
721 zilog = zfsvfs->z_log;
724 * Validate file offset
726 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
727 if (woff < 0) {
728 ZFS_EXIT(zfsvfs);
729 return (SET_ERROR(EINVAL));
733 * Check for mandatory locks before calling zfs_range_lock()
734 * in order to prevent a deadlock with locks set via fcntl().
736 if (MANDMODE((mode_t)zp->z_mode) &&
737 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
738 ZFS_EXIT(zfsvfs);
739 return (error);
743 * Pre-fault the pages to ensure slow (eg NFS) pages
744 * don't hold up txg.
745 * Skip this if uio contains loaned arc_buf.
747 if ((uio->uio_extflg == UIO_XUIO) &&
748 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
749 xuio = (xuio_t *)uio;
750 else
751 uio_prefaultpages(MIN(n, max_blksz), uio);
754 * If in append mode, set the io offset pointer to eof.
756 if (ioflag & FAPPEND) {
758 * Obtain an appending range lock to guarantee file append
759 * semantics. We reset the write offset once we have the lock.
761 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
762 woff = rl->r_off;
763 if (rl->r_len == UINT64_MAX) {
765 * We overlocked the file because this write will cause
766 * the file block size to increase.
767 * Note that zp_size cannot change with this lock held.
769 woff = zp->z_size;
771 uio->uio_loffset = woff;
772 } else {
774 * Note that if the file block size will change as a result of
775 * this write, then this range lock will lock the entire file
776 * so that we can re-write the block safely.
778 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
781 if (woff >= limit) {
782 zfs_range_unlock(rl);
783 ZFS_EXIT(zfsvfs);
784 return (SET_ERROR(EFBIG));
787 if ((woff + n) > limit || woff > (limit - n))
788 n = limit - woff;
790 /* Will this write extend the file length? */
791 write_eof = (woff + n > zp->z_size);
793 end_size = MAX(zp->z_size, woff + n);
796 * Write the file in reasonable size chunks. Each chunk is written
797 * in a separate transaction; this keeps the intent log records small
798 * and allows us to do more fine-grained space accounting.
800 while (n > 0) {
801 abuf = NULL;
802 woff = uio->uio_loffset;
803 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
804 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
805 if (abuf != NULL)
806 dmu_return_arcbuf(abuf);
807 error = SET_ERROR(EDQUOT);
808 break;
811 if (xuio && abuf == NULL) {
812 ASSERT(i_iov < iovcnt);
813 aiov = &iovp[i_iov];
814 abuf = dmu_xuio_arcbuf(xuio, i_iov);
815 dmu_xuio_clear(xuio, i_iov);
816 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
817 iovec_t *, aiov, arc_buf_t *, abuf);
818 ASSERT((aiov->iov_base == abuf->b_data) ||
819 ((char *)aiov->iov_base - (char *)abuf->b_data +
820 aiov->iov_len == arc_buf_size(abuf)));
821 i_iov++;
822 } else if (abuf == NULL && n >= max_blksz &&
823 woff >= zp->z_size &&
824 P2PHASE(woff, max_blksz) == 0 &&
825 zp->z_blksz == max_blksz) {
827 * This write covers a full block. "Borrow" a buffer
828 * from the dmu so that we can fill it before we enter
829 * a transaction. This avoids the possibility of
830 * holding up the transaction if the data copy hangs
831 * up on a pagefault (e.g., from an NFS server mapping).
833 size_t cbytes;
835 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
836 max_blksz);
837 ASSERT(abuf != NULL);
838 ASSERT(arc_buf_size(abuf) == max_blksz);
839 if (error = uiocopy(abuf->b_data, max_blksz,
840 UIO_WRITE, uio, &cbytes)) {
841 dmu_return_arcbuf(abuf);
842 break;
844 ASSERT(cbytes == max_blksz);
848 * Start a transaction.
850 tx = dmu_tx_create(zfsvfs->z_os);
851 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
852 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
853 zfs_sa_upgrade_txholds(tx, zp);
854 error = dmu_tx_assign(tx, TXG_WAIT);
855 if (error) {
856 dmu_tx_abort(tx);
857 if (abuf != NULL)
858 dmu_return_arcbuf(abuf);
859 break;
863 * If zfs_range_lock() over-locked we grow the blocksize
864 * and then reduce the lock range. This will only happen
865 * on the first iteration since zfs_range_reduce() will
866 * shrink down r_len to the appropriate size.
868 if (rl->r_len == UINT64_MAX) {
869 uint64_t new_blksz;
871 if (zp->z_blksz > max_blksz) {
873 * File's blocksize is already larger than the
874 * "recordsize" property. Only let it grow to
875 * the next power of 2.
877 ASSERT(!ISP2(zp->z_blksz));
878 new_blksz = MIN(end_size,
879 1 << highbit64(zp->z_blksz));
880 } else {
881 new_blksz = MIN(end_size, max_blksz);
883 zfs_grow_blocksize(zp, new_blksz, tx);
884 zfs_range_reduce(rl, woff, n);
888 * XXX - should we really limit each write to z_max_blksz?
889 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
891 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
893 if (abuf == NULL) {
894 tx_bytes = uio->uio_resid;
895 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
896 uio, nbytes, tx);
897 tx_bytes -= uio->uio_resid;
898 } else {
899 tx_bytes = nbytes;
900 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
902 * If this is not a full block write, but we are
903 * extending the file past EOF and this data starts
904 * block-aligned, use assign_arcbuf(). Otherwise,
905 * write via dmu_write().
907 if (tx_bytes < max_blksz && (!write_eof ||
908 aiov->iov_base != abuf->b_data)) {
909 ASSERT(xuio);
910 dmu_write(zfsvfs->z_os, zp->z_id, woff,
911 aiov->iov_len, aiov->iov_base, tx);
912 dmu_return_arcbuf(abuf);
913 xuio_stat_wbuf_copied();
914 } else {
915 ASSERT(xuio || tx_bytes == max_blksz);
916 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
917 woff, abuf, tx);
919 ASSERT(tx_bytes <= uio->uio_resid);
920 uioskip(uio, tx_bytes);
922 if (tx_bytes && vn_has_cached_data(vp)) {
923 update_pages(vp, woff,
924 tx_bytes, zfsvfs->z_os, zp->z_id);
928 * If we made no progress, we're done. If we made even
929 * partial progress, update the znode and ZIL accordingly.
931 if (tx_bytes == 0) {
932 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
933 (void *)&zp->z_size, sizeof (uint64_t), tx);
934 dmu_tx_commit(tx);
935 ASSERT(error != 0);
936 break;
940 * Clear Set-UID/Set-GID bits on successful write if not
941 * privileged and at least one of the excute bits is set.
943 * It would be nice to to this after all writes have
944 * been done, but that would still expose the ISUID/ISGID
945 * to another app after the partial write is committed.
947 * Note: we don't call zfs_fuid_map_id() here because
948 * user 0 is not an ephemeral uid.
950 mutex_enter(&zp->z_acl_lock);
951 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
952 (S_IXUSR >> 6))) != 0 &&
953 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
954 secpolicy_vnode_setid_retain(cr,
955 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
956 uint64_t newmode;
957 zp->z_mode &= ~(S_ISUID | S_ISGID);
958 newmode = zp->z_mode;
959 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
960 (void *)&newmode, sizeof (uint64_t), tx);
962 mutex_exit(&zp->z_acl_lock);
964 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
965 B_TRUE);
968 * Update the file size (zp_size) if it has changed;
969 * account for possible concurrent updates.
971 while ((end_size = zp->z_size) < uio->uio_loffset) {
972 (void) atomic_cas_64(&zp->z_size, end_size,
973 uio->uio_loffset);
974 ASSERT(error == 0);
977 * If we are replaying and eof is non zero then force
978 * the file size to the specified eof. Note, there's no
979 * concurrency during replay.
981 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
982 zp->z_size = zfsvfs->z_replay_eof;
984 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
986 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
987 dmu_tx_commit(tx);
989 if (error != 0)
990 break;
991 ASSERT(tx_bytes == nbytes);
992 n -= nbytes;
994 if (!xuio && n > 0)
995 uio_prefaultpages(MIN(n, max_blksz), uio);
998 zfs_range_unlock(rl);
1001 * If we're in replay mode, or we made no progress, return error.
1002 * Otherwise, it's at least a partial write, so it's successful.
1004 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
1005 ZFS_EXIT(zfsvfs);
1006 return (error);
1009 if (ioflag & (FSYNC | FDSYNC) ||
1010 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1011 zil_commit(zilog, zp->z_id);
1013 ZFS_EXIT(zfsvfs);
1014 return (0);
1017 void
1018 zfs_get_done(zgd_t *zgd, int error)
1020 znode_t *zp = zgd->zgd_private;
1021 objset_t *os = zp->z_zfsvfs->z_os;
1023 if (zgd->zgd_db)
1024 dmu_buf_rele(zgd->zgd_db, zgd);
1026 zfs_range_unlock(zgd->zgd_rl);
1029 * Release the vnode asynchronously as we currently have the
1030 * txg stopped from syncing.
1032 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1034 if (error == 0 && zgd->zgd_bp)
1035 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1037 kmem_free(zgd, sizeof (zgd_t));
1040 #ifdef DEBUG
1041 static int zil_fault_io = 0;
1042 #endif
1045 * Get data to generate a TX_WRITE intent log record.
1048 zfs_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1050 zfsvfs_t *zfsvfs = arg;
1051 objset_t *os = zfsvfs->z_os;
1052 znode_t *zp;
1053 uint64_t object = lr->lr_foid;
1054 uint64_t offset = lr->lr_offset;
1055 uint64_t size = lr->lr_length;
1056 dmu_buf_t *db;
1057 zgd_t *zgd;
1058 int error = 0;
1060 ASSERT3P(lwb, !=, NULL);
1061 ASSERT3P(zio, !=, NULL);
1062 ASSERT3U(size, !=, 0);
1065 * Nothing to do if the file has been removed
1067 if (zfs_zget(zfsvfs, object, &zp) != 0)
1068 return (SET_ERROR(ENOENT));
1069 if (zp->z_unlinked) {
1071 * Release the vnode asynchronously as we currently have the
1072 * txg stopped from syncing.
1074 VN_RELE_ASYNC(ZTOV(zp),
1075 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1076 return (SET_ERROR(ENOENT));
1079 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1080 zgd->zgd_lwb = lwb;
1081 zgd->zgd_private = zp;
1084 * Write records come in two flavors: immediate and indirect.
1085 * For small writes it's cheaper to store the data with the
1086 * log record (immediate); for large writes it's cheaper to
1087 * sync the data and get a pointer to it (indirect) so that
1088 * we don't have to write the data twice.
1090 if (buf != NULL) { /* immediate write */
1091 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1092 /* test for truncation needs to be done while range locked */
1093 if (offset >= zp->z_size) {
1094 error = SET_ERROR(ENOENT);
1095 } else {
1096 error = dmu_read(os, object, offset, size, buf,
1097 DMU_READ_NO_PREFETCH);
1099 ASSERT(error == 0 || error == ENOENT);
1100 } else { /* indirect write */
1102 * Have to lock the whole block to ensure when it's
1103 * written out and its checksum is being calculated
1104 * that no one can change the data. We need to re-check
1105 * blocksize after we get the lock in case it's changed!
1107 for (;;) {
1108 uint64_t blkoff;
1109 size = zp->z_blksz;
1110 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1111 offset -= blkoff;
1112 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1113 RL_READER);
1114 if (zp->z_blksz == size)
1115 break;
1116 offset += blkoff;
1117 zfs_range_unlock(zgd->zgd_rl);
1119 /* test for truncation needs to be done while range locked */
1120 if (lr->lr_offset >= zp->z_size)
1121 error = SET_ERROR(ENOENT);
1122 #ifdef DEBUG
1123 if (zil_fault_io) {
1124 error = SET_ERROR(EIO);
1125 zil_fault_io = 0;
1127 #endif
1128 if (error == 0)
1129 error = dmu_buf_hold(os, object, offset, zgd, &db,
1130 DMU_READ_NO_PREFETCH);
1132 if (error == 0) {
1133 blkptr_t *bp = &lr->lr_blkptr;
1135 zgd->zgd_db = db;
1136 zgd->zgd_bp = bp;
1138 ASSERT(db->db_offset == offset);
1139 ASSERT(db->db_size == size);
1141 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1142 zfs_get_done, zgd);
1143 ASSERT(error || lr->lr_length <= size);
1146 * On success, we need to wait for the write I/O
1147 * initiated by dmu_sync() to complete before we can
1148 * release this dbuf. We will finish everything up
1149 * in the zfs_get_done() callback.
1151 if (error == 0)
1152 return (0);
1154 if (error == EALREADY) {
1155 lr->lr_common.lrc_txtype = TX_WRITE2;
1157 * TX_WRITE2 relies on the data previously
1158 * written by the TX_WRITE that caused
1159 * EALREADY. We zero out the BP because
1160 * it is the old, currently-on-disk BP,
1161 * so there's no need to zio_flush() its
1162 * vdevs (flushing would needlesly hurt
1163 * performance, and doesn't work on
1164 * indirect vdevs).
1166 zgd->zgd_bp = NULL;
1167 BP_ZERO(bp);
1168 error = 0;
1173 zfs_get_done(zgd, error);
1175 return (error);
1178 /*ARGSUSED*/
1179 static int
1180 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1181 caller_context_t *ct)
1183 znode_t *zp = VTOZ(vp);
1184 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1185 int error;
1187 ZFS_ENTER(zfsvfs);
1188 ZFS_VERIFY_ZP(zp);
1190 if (flag & V_ACE_MASK)
1191 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1192 else
1193 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1195 ZFS_EXIT(zfsvfs);
1196 return (error);
1200 * If vnode is for a device return a specfs vnode instead.
1202 static int
1203 specvp_check(vnode_t **vpp, cred_t *cr)
1205 int error = 0;
1207 if (IS_DEVVP(*vpp)) {
1208 struct vnode *svp;
1210 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1211 VN_RELE(*vpp);
1212 if (svp == NULL)
1213 error = SET_ERROR(ENOSYS);
1214 *vpp = svp;
1216 return (error);
1221 * Lookup an entry in a directory, or an extended attribute directory.
1222 * If it exists, return a held vnode reference for it.
1224 * IN: dvp - vnode of directory to search.
1225 * nm - name of entry to lookup.
1226 * pnp - full pathname to lookup [UNUSED].
1227 * flags - LOOKUP_XATTR set if looking for an attribute.
1228 * rdir - root directory vnode [UNUSED].
1229 * cr - credentials of caller.
1230 * ct - caller context
1231 * direntflags - directory lookup flags
1232 * realpnp - returned pathname.
1234 * OUT: vpp - vnode of located entry, NULL if not found.
1236 * RETURN: 0 on success, error code on failure.
1238 * Timestamps:
1239 * NA
1241 /* ARGSUSED */
1242 static int
1243 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1244 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1245 int *direntflags, pathname_t *realpnp)
1247 znode_t *zdp = VTOZ(dvp);
1248 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1249 int error = 0;
1252 * Fast path lookup, however we must skip DNLC lookup
1253 * for case folding or normalizing lookups because the
1254 * DNLC code only stores the passed in name. This means
1255 * creating 'a' and removing 'A' on a case insensitive
1256 * file system would work, but DNLC still thinks 'a'
1257 * exists and won't let you create it again on the next
1258 * pass through fast path.
1260 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1262 if (dvp->v_type != VDIR) {
1263 return (SET_ERROR(ENOTDIR));
1264 } else if (zdp->z_sa_hdl == NULL) {
1265 return (SET_ERROR(EIO));
1268 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1269 error = zfs_fastaccesschk_execute(zdp, cr);
1270 if (!error) {
1271 *vpp = dvp;
1272 VN_HOLD(*vpp);
1273 return (0);
1275 return (error);
1276 } else if (!zdp->z_zfsvfs->z_norm &&
1277 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1279 vnode_t *tvp = dnlc_lookup(dvp, nm);
1281 if (tvp) {
1282 error = zfs_fastaccesschk_execute(zdp, cr);
1283 if (error) {
1284 VN_RELE(tvp);
1285 return (error);
1287 if (tvp == DNLC_NO_VNODE) {
1288 VN_RELE(tvp);
1289 return (SET_ERROR(ENOENT));
1290 } else {
1291 *vpp = tvp;
1292 return (specvp_check(vpp, cr));
1298 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1300 ZFS_ENTER(zfsvfs);
1301 ZFS_VERIFY_ZP(zdp);
1303 *vpp = NULL;
1305 if (flags & LOOKUP_XATTR) {
1307 * If the xattr property is off, refuse the lookup request.
1309 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1310 ZFS_EXIT(zfsvfs);
1311 return (SET_ERROR(EINVAL));
1315 * We don't allow recursive attributes..
1316 * Maybe someday we will.
1318 if (zdp->z_pflags & ZFS_XATTR) {
1319 ZFS_EXIT(zfsvfs);
1320 return (SET_ERROR(EINVAL));
1323 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1324 ZFS_EXIT(zfsvfs);
1325 return (error);
1329 * Do we have permission to get into attribute directory?
1332 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1333 B_FALSE, cr)) {
1334 VN_RELE(*vpp);
1335 *vpp = NULL;
1338 ZFS_EXIT(zfsvfs);
1339 return (error);
1342 if (dvp->v_type != VDIR) {
1343 ZFS_EXIT(zfsvfs);
1344 return (SET_ERROR(ENOTDIR));
1348 * Check accessibility of directory.
1351 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1352 ZFS_EXIT(zfsvfs);
1353 return (error);
1356 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1357 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1358 ZFS_EXIT(zfsvfs);
1359 return (SET_ERROR(EILSEQ));
1362 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1363 if (error == 0)
1364 error = specvp_check(vpp, cr);
1366 ZFS_EXIT(zfsvfs);
1367 return (error);
1371 * Attempt to create a new entry in a directory. If the entry
1372 * already exists, truncate the file if permissible, else return
1373 * an error. Return the vp of the created or trunc'd file.
1375 * IN: dvp - vnode of directory to put new file entry in.
1376 * name - name of new file entry.
1377 * vap - attributes of new file.
1378 * excl - flag indicating exclusive or non-exclusive mode.
1379 * mode - mode to open file with.
1380 * cr - credentials of caller.
1381 * flag - large file flag [UNUSED].
1382 * ct - caller context
1383 * vsecp - ACL to be set
1385 * OUT: vpp - vnode of created or trunc'd entry.
1387 * RETURN: 0 on success, error code on failure.
1389 * Timestamps:
1390 * dvp - ctime|mtime updated if new entry created
1391 * vp - ctime|mtime always, atime if new
1394 /* ARGSUSED */
1395 static int
1396 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1397 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1398 vsecattr_t *vsecp)
1400 znode_t *zp, *dzp = VTOZ(dvp);
1401 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1402 zilog_t *zilog;
1403 objset_t *os;
1404 zfs_dirlock_t *dl;
1405 dmu_tx_t *tx;
1406 int error;
1407 ksid_t *ksid;
1408 uid_t uid;
1409 gid_t gid = crgetgid(cr);
1410 zfs_acl_ids_t acl_ids;
1411 boolean_t fuid_dirtied;
1412 boolean_t have_acl = B_FALSE;
1413 boolean_t waited = B_FALSE;
1416 * If we have an ephemeral id, ACL, or XVATTR then
1417 * make sure file system is at proper version
1420 ksid = crgetsid(cr, KSID_OWNER);
1421 if (ksid)
1422 uid = ksid_getid(ksid);
1423 else
1424 uid = crgetuid(cr);
1426 if (zfsvfs->z_use_fuids == B_FALSE &&
1427 (vsecp || (vap->va_mask & AT_XVATTR) ||
1428 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1429 return (SET_ERROR(EINVAL));
1431 ZFS_ENTER(zfsvfs);
1432 ZFS_VERIFY_ZP(dzp);
1433 os = zfsvfs->z_os;
1434 zilog = zfsvfs->z_log;
1436 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1437 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1438 ZFS_EXIT(zfsvfs);
1439 return (SET_ERROR(EILSEQ));
1442 if (vap->va_mask & AT_XVATTR) {
1443 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1444 crgetuid(cr), cr, vap->va_type)) != 0) {
1445 ZFS_EXIT(zfsvfs);
1446 return (error);
1449 top:
1450 *vpp = NULL;
1452 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1453 vap->va_mode &= ~VSVTX;
1455 if (*name == '\0') {
1457 * Null component name refers to the directory itself.
1459 VN_HOLD(dvp);
1460 zp = dzp;
1461 dl = NULL;
1462 error = 0;
1463 } else {
1464 /* possible VN_HOLD(zp) */
1465 int zflg = 0;
1467 if (flag & FIGNORECASE)
1468 zflg |= ZCILOOK;
1470 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1471 NULL, NULL);
1472 if (error) {
1473 if (have_acl)
1474 zfs_acl_ids_free(&acl_ids);
1475 if (strcmp(name, "..") == 0)
1476 error = SET_ERROR(EISDIR);
1477 ZFS_EXIT(zfsvfs);
1478 return (error);
1482 if (zp == NULL) {
1483 uint64_t txtype;
1486 * Create a new file object and update the directory
1487 * to reference it.
1489 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1490 if (have_acl)
1491 zfs_acl_ids_free(&acl_ids);
1492 goto out;
1496 * We only support the creation of regular files in
1497 * extended attribute directories.
1500 if ((dzp->z_pflags & ZFS_XATTR) &&
1501 (vap->va_type != VREG)) {
1502 if (have_acl)
1503 zfs_acl_ids_free(&acl_ids);
1504 error = SET_ERROR(EINVAL);
1505 goto out;
1508 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1509 cr, vsecp, &acl_ids)) != 0)
1510 goto out;
1511 have_acl = B_TRUE;
1513 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1514 zfs_acl_ids_free(&acl_ids);
1515 error = SET_ERROR(EDQUOT);
1516 goto out;
1519 tx = dmu_tx_create(os);
1521 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1522 ZFS_SA_BASE_ATTR_SIZE);
1524 fuid_dirtied = zfsvfs->z_fuid_dirty;
1525 if (fuid_dirtied)
1526 zfs_fuid_txhold(zfsvfs, tx);
1527 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1528 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1529 if (!zfsvfs->z_use_sa &&
1530 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1531 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1532 0, acl_ids.z_aclp->z_acl_bytes);
1534 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1535 if (error) {
1536 zfs_dirent_unlock(dl);
1537 if (error == ERESTART) {
1538 waited = B_TRUE;
1539 dmu_tx_wait(tx);
1540 dmu_tx_abort(tx);
1541 goto top;
1543 zfs_acl_ids_free(&acl_ids);
1544 dmu_tx_abort(tx);
1545 ZFS_EXIT(zfsvfs);
1546 return (error);
1548 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1550 if (fuid_dirtied)
1551 zfs_fuid_sync(zfsvfs, tx);
1553 (void) zfs_link_create(dl, zp, tx, ZNEW);
1554 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1555 if (flag & FIGNORECASE)
1556 txtype |= TX_CI;
1557 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1558 vsecp, acl_ids.z_fuidp, vap);
1559 zfs_acl_ids_free(&acl_ids);
1560 dmu_tx_commit(tx);
1561 } else {
1562 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1564 if (have_acl)
1565 zfs_acl_ids_free(&acl_ids);
1566 have_acl = B_FALSE;
1569 * A directory entry already exists for this name.
1572 * Can't truncate an existing file if in exclusive mode.
1574 if (excl == EXCL) {
1575 error = SET_ERROR(EEXIST);
1576 goto out;
1579 * Can't open a directory for writing.
1581 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1582 error = SET_ERROR(EISDIR);
1583 goto out;
1586 * Verify requested access to file.
1588 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1589 goto out;
1592 mutex_enter(&dzp->z_lock);
1593 dzp->z_seq++;
1594 mutex_exit(&dzp->z_lock);
1597 * Truncate regular files if requested.
1599 if ((ZTOV(zp)->v_type == VREG) &&
1600 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1601 /* we can't hold any locks when calling zfs_freesp() */
1602 zfs_dirent_unlock(dl);
1603 dl = NULL;
1604 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1605 if (error == 0) {
1606 vnevent_create(ZTOV(zp), ct);
1610 out:
1612 if (dl)
1613 zfs_dirent_unlock(dl);
1615 if (error) {
1616 if (zp)
1617 VN_RELE(ZTOV(zp));
1618 } else {
1619 *vpp = ZTOV(zp);
1620 error = specvp_check(vpp, cr);
1623 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1624 zil_commit(zilog, 0);
1626 ZFS_EXIT(zfsvfs);
1627 return (error);
1631 * Remove an entry from a directory.
1633 * IN: dvp - vnode of directory to remove entry from.
1634 * name - name of entry to remove.
1635 * cr - credentials of caller.
1636 * ct - caller context
1637 * flags - case flags
1639 * RETURN: 0 on success, error code on failure.
1641 * Timestamps:
1642 * dvp - ctime|mtime
1643 * vp - ctime (if nlink > 0)
1646 uint64_t null_xattr = 0;
1648 /*ARGSUSED*/
1649 static int
1650 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1651 int flags)
1653 znode_t *zp, *dzp = VTOZ(dvp);
1654 znode_t *xzp;
1655 vnode_t *vp;
1656 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1657 zilog_t *zilog;
1658 uint64_t acl_obj, xattr_obj;
1659 uint64_t xattr_obj_unlinked = 0;
1660 uint64_t obj = 0;
1661 zfs_dirlock_t *dl;
1662 dmu_tx_t *tx;
1663 boolean_t may_delete_now, delete_now = FALSE;
1664 boolean_t unlinked, toobig = FALSE;
1665 uint64_t txtype;
1666 pathname_t *realnmp = NULL;
1667 pathname_t realnm;
1668 int error;
1669 int zflg = ZEXISTS;
1670 boolean_t waited = B_FALSE;
1672 ZFS_ENTER(zfsvfs);
1673 ZFS_VERIFY_ZP(dzp);
1674 zilog = zfsvfs->z_log;
1676 if (flags & FIGNORECASE) {
1677 zflg |= ZCILOOK;
1678 pn_alloc(&realnm);
1679 realnmp = &realnm;
1682 top:
1683 xattr_obj = 0;
1684 xzp = NULL;
1686 * Attempt to lock directory; fail if entry doesn't exist.
1688 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1689 NULL, realnmp)) {
1690 if (realnmp)
1691 pn_free(realnmp);
1692 ZFS_EXIT(zfsvfs);
1693 return (error);
1696 vp = ZTOV(zp);
1698 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1699 goto out;
1703 * Need to use rmdir for removing directories.
1705 if (vp->v_type == VDIR) {
1706 error = SET_ERROR(EPERM);
1707 goto out;
1710 vnevent_remove(vp, dvp, name, ct);
1712 if (realnmp)
1713 dnlc_remove(dvp, realnmp->pn_buf);
1714 else
1715 dnlc_remove(dvp, name);
1717 mutex_enter(&vp->v_lock);
1718 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1719 mutex_exit(&vp->v_lock);
1722 * We may delete the znode now, or we may put it in the unlinked set;
1723 * it depends on whether we're the last link, and on whether there are
1724 * other holds on the vnode. So we dmu_tx_hold() the right things to
1725 * allow for either case.
1727 obj = zp->z_id;
1728 tx = dmu_tx_create(zfsvfs->z_os);
1729 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1730 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1731 zfs_sa_upgrade_txholds(tx, zp);
1732 zfs_sa_upgrade_txholds(tx, dzp);
1733 if (may_delete_now) {
1734 toobig =
1735 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1736 /* if the file is too big, only hold_free a token amount */
1737 dmu_tx_hold_free(tx, zp->z_id, 0,
1738 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1741 /* are there any extended attributes? */
1742 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1743 &xattr_obj, sizeof (xattr_obj));
1744 if (error == 0 && xattr_obj) {
1745 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1746 ASSERT0(error);
1747 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1748 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1751 mutex_enter(&zp->z_lock);
1752 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1753 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1754 mutex_exit(&zp->z_lock);
1756 /* charge as an update -- would be nice not to charge at all */
1757 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1760 * Mark this transaction as typically resulting in a net free of space
1762 dmu_tx_mark_netfree(tx);
1764 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1765 if (error) {
1766 zfs_dirent_unlock(dl);
1767 VN_RELE(vp);
1768 if (xzp)
1769 VN_RELE(ZTOV(xzp));
1770 if (error == ERESTART) {
1771 waited = B_TRUE;
1772 dmu_tx_wait(tx);
1773 dmu_tx_abort(tx);
1774 goto top;
1776 if (realnmp)
1777 pn_free(realnmp);
1778 dmu_tx_abort(tx);
1779 ZFS_EXIT(zfsvfs);
1780 return (error);
1784 * Remove the directory entry.
1786 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1788 if (error) {
1789 dmu_tx_commit(tx);
1790 goto out;
1793 if (unlinked) {
1795 * Hold z_lock so that we can make sure that the ACL obj
1796 * hasn't changed. Could have been deleted due to
1797 * zfs_sa_upgrade().
1799 mutex_enter(&zp->z_lock);
1800 mutex_enter(&vp->v_lock);
1801 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1802 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1803 delete_now = may_delete_now && !toobig &&
1804 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1805 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1806 acl_obj;
1807 mutex_exit(&vp->v_lock);
1810 if (delete_now) {
1811 if (xattr_obj_unlinked) {
1812 ASSERT3U(xzp->z_links, ==, 2);
1813 mutex_enter(&xzp->z_lock);
1814 xzp->z_unlinked = 1;
1815 xzp->z_links = 0;
1816 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1817 &xzp->z_links, sizeof (xzp->z_links), tx);
1818 ASSERT3U(error, ==, 0);
1819 mutex_exit(&xzp->z_lock);
1820 zfs_unlinked_add(xzp, tx);
1822 if (zp->z_is_sa)
1823 error = sa_remove(zp->z_sa_hdl,
1824 SA_ZPL_XATTR(zfsvfs), tx);
1825 else
1826 error = sa_update(zp->z_sa_hdl,
1827 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1828 sizeof (uint64_t), tx);
1829 ASSERT0(error);
1831 mutex_enter(&vp->v_lock);
1832 VN_RELE_LOCKED(vp);
1833 ASSERT0(vp->v_count);
1834 mutex_exit(&vp->v_lock);
1835 mutex_exit(&zp->z_lock);
1836 zfs_znode_delete(zp, tx);
1837 } else if (unlinked) {
1838 mutex_exit(&zp->z_lock);
1839 zfs_unlinked_add(zp, tx);
1842 txtype = TX_REMOVE;
1843 if (flags & FIGNORECASE)
1844 txtype |= TX_CI;
1845 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1847 dmu_tx_commit(tx);
1848 out:
1849 if (realnmp)
1850 pn_free(realnmp);
1852 zfs_dirent_unlock(dl);
1854 if (!delete_now)
1855 VN_RELE(vp);
1856 if (xzp)
1857 VN_RELE(ZTOV(xzp));
1859 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1860 zil_commit(zilog, 0);
1862 ZFS_EXIT(zfsvfs);
1863 return (error);
1867 * Create a new directory and insert it into dvp using the name
1868 * provided. Return a pointer to the inserted directory.
1870 * IN: dvp - vnode of directory to add subdir to.
1871 * dirname - name of new directory.
1872 * vap - attributes of new directory.
1873 * cr - credentials of caller.
1874 * ct - caller context
1875 * flags - case flags
1876 * vsecp - ACL to be set
1878 * OUT: vpp - vnode of created directory.
1880 * RETURN: 0 on success, error code on failure.
1882 * Timestamps:
1883 * dvp - ctime|mtime updated
1884 * vp - ctime|mtime|atime updated
1886 /*ARGSUSED*/
1887 static int
1888 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1889 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1891 znode_t *zp, *dzp = VTOZ(dvp);
1892 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1893 zilog_t *zilog;
1894 zfs_dirlock_t *dl;
1895 uint64_t txtype;
1896 dmu_tx_t *tx;
1897 int error;
1898 int zf = ZNEW;
1899 ksid_t *ksid;
1900 uid_t uid;
1901 gid_t gid = crgetgid(cr);
1902 zfs_acl_ids_t acl_ids;
1903 boolean_t fuid_dirtied;
1904 boolean_t waited = B_FALSE;
1906 ASSERT(vap->va_type == VDIR);
1909 * If we have an ephemeral id, ACL, or XVATTR then
1910 * make sure file system is at proper version
1913 ksid = crgetsid(cr, KSID_OWNER);
1914 if (ksid)
1915 uid = ksid_getid(ksid);
1916 else
1917 uid = crgetuid(cr);
1918 if (zfsvfs->z_use_fuids == B_FALSE &&
1919 (vsecp || (vap->va_mask & AT_XVATTR) ||
1920 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1921 return (SET_ERROR(EINVAL));
1923 ZFS_ENTER(zfsvfs);
1924 ZFS_VERIFY_ZP(dzp);
1925 zilog = zfsvfs->z_log;
1927 if (dzp->z_pflags & ZFS_XATTR) {
1928 ZFS_EXIT(zfsvfs);
1929 return (SET_ERROR(EINVAL));
1932 if (zfsvfs->z_utf8 && u8_validate(dirname,
1933 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1934 ZFS_EXIT(zfsvfs);
1935 return (SET_ERROR(EILSEQ));
1937 if (flags & FIGNORECASE)
1938 zf |= ZCILOOK;
1940 if (vap->va_mask & AT_XVATTR) {
1941 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1942 crgetuid(cr), cr, vap->va_type)) != 0) {
1943 ZFS_EXIT(zfsvfs);
1944 return (error);
1948 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1949 vsecp, &acl_ids)) != 0) {
1950 ZFS_EXIT(zfsvfs);
1951 return (error);
1954 * First make sure the new directory doesn't exist.
1956 * Existence is checked first to make sure we don't return
1957 * EACCES instead of EEXIST which can cause some applications
1958 * to fail.
1960 top:
1961 *vpp = NULL;
1963 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1964 NULL, NULL)) {
1965 zfs_acl_ids_free(&acl_ids);
1966 ZFS_EXIT(zfsvfs);
1967 return (error);
1970 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1971 zfs_acl_ids_free(&acl_ids);
1972 zfs_dirent_unlock(dl);
1973 ZFS_EXIT(zfsvfs);
1974 return (error);
1977 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1978 zfs_acl_ids_free(&acl_ids);
1979 zfs_dirent_unlock(dl);
1980 ZFS_EXIT(zfsvfs);
1981 return (SET_ERROR(EDQUOT));
1985 * Add a new entry to the directory.
1987 tx = dmu_tx_create(zfsvfs->z_os);
1988 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1989 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1990 fuid_dirtied = zfsvfs->z_fuid_dirty;
1991 if (fuid_dirtied)
1992 zfs_fuid_txhold(zfsvfs, tx);
1993 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1994 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1995 acl_ids.z_aclp->z_acl_bytes);
1998 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1999 ZFS_SA_BASE_ATTR_SIZE);
2001 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2002 if (error) {
2003 zfs_dirent_unlock(dl);
2004 if (error == ERESTART) {
2005 waited = B_TRUE;
2006 dmu_tx_wait(tx);
2007 dmu_tx_abort(tx);
2008 goto top;
2010 zfs_acl_ids_free(&acl_ids);
2011 dmu_tx_abort(tx);
2012 ZFS_EXIT(zfsvfs);
2013 return (error);
2017 * Create new node.
2019 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2021 if (fuid_dirtied)
2022 zfs_fuid_sync(zfsvfs, tx);
2025 * Now put new name in parent dir.
2027 (void) zfs_link_create(dl, zp, tx, ZNEW);
2029 *vpp = ZTOV(zp);
2031 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2032 if (flags & FIGNORECASE)
2033 txtype |= TX_CI;
2034 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2035 acl_ids.z_fuidp, vap);
2037 zfs_acl_ids_free(&acl_ids);
2039 dmu_tx_commit(tx);
2041 zfs_dirent_unlock(dl);
2043 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2044 zil_commit(zilog, 0);
2046 ZFS_EXIT(zfsvfs);
2047 return (0);
2051 * Remove a directory subdir entry. If the current working
2052 * directory is the same as the subdir to be removed, the
2053 * remove will fail.
2055 * IN: dvp - vnode of directory to remove from.
2056 * name - name of directory to be removed.
2057 * cwd - vnode of current working directory.
2058 * cr - credentials of caller.
2059 * ct - caller context
2060 * flags - case flags
2062 * RETURN: 0 on success, error code on failure.
2064 * Timestamps:
2065 * dvp - ctime|mtime updated
2067 /*ARGSUSED*/
2068 static int
2069 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2070 caller_context_t *ct, int flags)
2072 znode_t *dzp = VTOZ(dvp);
2073 znode_t *zp;
2074 vnode_t *vp;
2075 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2076 zilog_t *zilog;
2077 zfs_dirlock_t *dl;
2078 dmu_tx_t *tx;
2079 int error;
2080 int zflg = ZEXISTS;
2081 boolean_t waited = B_FALSE;
2083 ZFS_ENTER(zfsvfs);
2084 ZFS_VERIFY_ZP(dzp);
2085 zilog = zfsvfs->z_log;
2087 if (flags & FIGNORECASE)
2088 zflg |= ZCILOOK;
2089 top:
2090 zp = NULL;
2093 * Attempt to lock directory; fail if entry doesn't exist.
2095 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2096 NULL, NULL)) {
2097 ZFS_EXIT(zfsvfs);
2098 return (error);
2101 vp = ZTOV(zp);
2103 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2104 goto out;
2107 if (vp->v_type != VDIR) {
2108 error = SET_ERROR(ENOTDIR);
2109 goto out;
2112 if (vp == cwd) {
2113 error = SET_ERROR(EINVAL);
2114 goto out;
2117 vnevent_rmdir(vp, dvp, name, ct);
2120 * Grab a lock on the directory to make sure that noone is
2121 * trying to add (or lookup) entries while we are removing it.
2123 rw_enter(&zp->z_name_lock, RW_WRITER);
2126 * Grab a lock on the parent pointer to make sure we play well
2127 * with the treewalk and directory rename code.
2129 rw_enter(&zp->z_parent_lock, RW_WRITER);
2131 tx = dmu_tx_create(zfsvfs->z_os);
2132 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2133 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2134 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2135 zfs_sa_upgrade_txholds(tx, zp);
2136 zfs_sa_upgrade_txholds(tx, dzp);
2137 dmu_tx_mark_netfree(tx);
2138 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2139 if (error) {
2140 rw_exit(&zp->z_parent_lock);
2141 rw_exit(&zp->z_name_lock);
2142 zfs_dirent_unlock(dl);
2143 VN_RELE(vp);
2144 if (error == ERESTART) {
2145 waited = B_TRUE;
2146 dmu_tx_wait(tx);
2147 dmu_tx_abort(tx);
2148 goto top;
2150 dmu_tx_abort(tx);
2151 ZFS_EXIT(zfsvfs);
2152 return (error);
2155 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2157 if (error == 0) {
2158 uint64_t txtype = TX_RMDIR;
2159 if (flags & FIGNORECASE)
2160 txtype |= TX_CI;
2161 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2164 dmu_tx_commit(tx);
2166 rw_exit(&zp->z_parent_lock);
2167 rw_exit(&zp->z_name_lock);
2168 out:
2169 zfs_dirent_unlock(dl);
2171 VN_RELE(vp);
2173 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2174 zil_commit(zilog, 0);
2176 ZFS_EXIT(zfsvfs);
2177 return (error);
2181 * Read as many directory entries as will fit into the provided
2182 * buffer from the given directory cursor position (specified in
2183 * the uio structure).
2185 * IN: vp - vnode of directory to read.
2186 * uio - structure supplying read location, range info,
2187 * and return buffer.
2188 * cr - credentials of caller.
2189 * ct - caller context
2190 * flags - case flags
2192 * OUT: uio - updated offset and range, buffer filled.
2193 * eofp - set to true if end-of-file detected.
2195 * RETURN: 0 on success, error code on failure.
2197 * Timestamps:
2198 * vp - atime updated
2200 * Note that the low 4 bits of the cookie returned by zap is always zero.
2201 * This allows us to use the low range for "special" directory entries:
2202 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2203 * we use the offset 2 for the '.zfs' directory.
2205 /* ARGSUSED */
2206 static int
2207 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2208 caller_context_t *ct, int flags)
2210 znode_t *zp = VTOZ(vp);
2211 iovec_t *iovp;
2212 edirent_t *eodp;
2213 dirent64_t *odp;
2214 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2215 objset_t *os;
2216 caddr_t outbuf;
2217 size_t bufsize;
2218 zap_cursor_t zc;
2219 zap_attribute_t zap;
2220 uint_t bytes_wanted;
2221 uint64_t offset; /* must be unsigned; checks for < 1 */
2222 uint64_t parent;
2223 int local_eof;
2224 int outcount;
2225 int error;
2226 uint8_t prefetch;
2227 boolean_t check_sysattrs;
2229 ZFS_ENTER(zfsvfs);
2230 ZFS_VERIFY_ZP(zp);
2232 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2233 &parent, sizeof (parent))) != 0) {
2234 ZFS_EXIT(zfsvfs);
2235 return (error);
2239 * If we are not given an eof variable,
2240 * use a local one.
2242 if (eofp == NULL)
2243 eofp = &local_eof;
2246 * Check for valid iov_len.
2248 if (uio->uio_iov->iov_len <= 0) {
2249 ZFS_EXIT(zfsvfs);
2250 return (SET_ERROR(EINVAL));
2254 * Quit if directory has been removed (posix)
2256 if ((*eofp = zp->z_unlinked) != 0) {
2257 ZFS_EXIT(zfsvfs);
2258 return (0);
2261 error = 0;
2262 os = zfsvfs->z_os;
2263 offset = uio->uio_loffset;
2264 prefetch = zp->z_zn_prefetch;
2267 * Initialize the iterator cursor.
2269 if (offset <= 3) {
2271 * Start iteration from the beginning of the directory.
2273 zap_cursor_init(&zc, os, zp->z_id);
2274 } else {
2276 * The offset is a serialized cursor.
2278 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2282 * Get space to change directory entries into fs independent format.
2284 iovp = uio->uio_iov;
2285 bytes_wanted = iovp->iov_len;
2286 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2287 bufsize = bytes_wanted;
2288 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2289 odp = (struct dirent64 *)outbuf;
2290 } else {
2291 bufsize = bytes_wanted;
2292 outbuf = NULL;
2293 odp = (struct dirent64 *)iovp->iov_base;
2295 eodp = (struct edirent *)odp;
2298 * If this VFS supports the system attribute view interface; and
2299 * we're looking at an extended attribute directory; and we care
2300 * about normalization conflicts on this vfs; then we must check
2301 * for normalization conflicts with the sysattr name space.
2303 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2304 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2305 (flags & V_RDDIR_ENTFLAGS);
2308 * Transform to file-system independent format
2310 outcount = 0;
2311 while (outcount < bytes_wanted) {
2312 ino64_t objnum;
2313 ushort_t reclen;
2314 off64_t *next = NULL;
2317 * Special case `.', `..', and `.zfs'.
2319 if (offset == 0) {
2320 (void) strcpy(zap.za_name, ".");
2321 zap.za_normalization_conflict = 0;
2322 objnum = zp->z_id;
2323 } else if (offset == 1) {
2324 (void) strcpy(zap.za_name, "..");
2325 zap.za_normalization_conflict = 0;
2326 objnum = parent;
2327 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2328 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2329 zap.za_normalization_conflict = 0;
2330 objnum = ZFSCTL_INO_ROOT;
2331 } else {
2333 * Grab next entry.
2335 if (error = zap_cursor_retrieve(&zc, &zap)) {
2336 if ((*eofp = (error == ENOENT)) != 0)
2337 break;
2338 else
2339 goto update;
2342 if (zap.za_integer_length != 8 ||
2343 zap.za_num_integers != 1) {
2344 cmn_err(CE_WARN, "zap_readdir: bad directory "
2345 "entry, obj = %lld, offset = %lld\n",
2346 (u_longlong_t)zp->z_id,
2347 (u_longlong_t)offset);
2348 error = SET_ERROR(ENXIO);
2349 goto update;
2352 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2354 * MacOS X can extract the object type here such as:
2355 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2358 if (check_sysattrs && !zap.za_normalization_conflict) {
2359 zap.za_normalization_conflict =
2360 xattr_sysattr_casechk(zap.za_name);
2364 if (flags & V_RDDIR_ACCFILTER) {
2366 * If we have no access at all, don't include
2367 * this entry in the returned information
2369 znode_t *ezp;
2370 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2371 goto skip_entry;
2372 if (!zfs_has_access(ezp, cr)) {
2373 VN_RELE(ZTOV(ezp));
2374 goto skip_entry;
2376 VN_RELE(ZTOV(ezp));
2379 if (flags & V_RDDIR_ENTFLAGS)
2380 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2381 else
2382 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2385 * Will this entry fit in the buffer?
2387 if (outcount + reclen > bufsize) {
2389 * Did we manage to fit anything in the buffer?
2391 if (!outcount) {
2392 error = SET_ERROR(EINVAL);
2393 goto update;
2395 break;
2397 if (flags & V_RDDIR_ENTFLAGS) {
2399 * Add extended flag entry:
2401 eodp->ed_ino = objnum;
2402 eodp->ed_reclen = reclen;
2403 /* NOTE: ed_off is the offset for the *next* entry */
2404 next = &(eodp->ed_off);
2405 eodp->ed_eflags = zap.za_normalization_conflict ?
2406 ED_CASE_CONFLICT : 0;
2407 (void) strncpy(eodp->ed_name, zap.za_name,
2408 EDIRENT_NAMELEN(reclen));
2409 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2410 } else {
2412 * Add normal entry:
2414 odp->d_ino = objnum;
2415 odp->d_reclen = reclen;
2416 /* NOTE: d_off is the offset for the *next* entry */
2417 next = &(odp->d_off);
2418 (void) strncpy(odp->d_name, zap.za_name,
2419 DIRENT64_NAMELEN(reclen));
2420 odp = (dirent64_t *)((intptr_t)odp + reclen);
2422 outcount += reclen;
2424 ASSERT(outcount <= bufsize);
2426 /* Prefetch znode */
2427 if (prefetch)
2428 dmu_prefetch(os, objnum, 0, 0, 0,
2429 ZIO_PRIORITY_SYNC_READ);
2431 skip_entry:
2433 * Move to the next entry, fill in the previous offset.
2435 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2436 zap_cursor_advance(&zc);
2437 offset = zap_cursor_serialize(&zc);
2438 } else {
2439 offset += 1;
2441 if (next)
2442 *next = offset;
2444 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2446 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2447 iovp->iov_base += outcount;
2448 iovp->iov_len -= outcount;
2449 uio->uio_resid -= outcount;
2450 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2452 * Reset the pointer.
2454 offset = uio->uio_loffset;
2457 update:
2458 zap_cursor_fini(&zc);
2459 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2460 kmem_free(outbuf, bufsize);
2462 if (error == ENOENT)
2463 error = 0;
2465 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2467 uio->uio_loffset = offset;
2468 ZFS_EXIT(zfsvfs);
2469 return (error);
2472 ulong_t zfs_fsync_sync_cnt = 4;
2474 static int
2475 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2477 znode_t *zp = VTOZ(vp);
2478 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2481 * Regardless of whether this is required for standards conformance,
2482 * this is the logical behavior when fsync() is called on a file with
2483 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2484 * going to be pushed out as part of the zil_commit().
2486 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2487 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2488 (void) fop_putpage(vp, 0, (size_t)0, B_ASYNC, cr, ct);
2490 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2492 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2493 ZFS_ENTER(zfsvfs);
2494 ZFS_VERIFY_ZP(zp);
2495 zil_commit(zfsvfs->z_log, zp->z_id);
2496 ZFS_EXIT(zfsvfs);
2498 return (0);
2503 * Get the requested file attributes and place them in the provided
2504 * vattr structure.
2506 * IN: vp - vnode of file.
2507 * vap - va_mask identifies requested attributes.
2508 * If AT_XVATTR set, then optional attrs are requested
2509 * flags - ATTR_NOACLCHECK (CIFS server context)
2510 * cr - credentials of caller.
2511 * ct - caller context
2513 * OUT: vap - attribute values.
2515 * RETURN: 0 (always succeeds).
2517 /* ARGSUSED */
2518 static int
2519 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2520 caller_context_t *ct)
2522 znode_t *zp = VTOZ(vp);
2523 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2524 int error = 0;
2525 uint64_t links;
2526 uint64_t mtime[2], ctime[2];
2527 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2528 xoptattr_t *xoap = NULL;
2529 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2530 sa_bulk_attr_t bulk[2];
2531 int count = 0;
2533 ZFS_ENTER(zfsvfs);
2534 ZFS_VERIFY_ZP(zp);
2536 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2538 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2539 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2541 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2542 ZFS_EXIT(zfsvfs);
2543 return (error);
2547 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2548 * Also, if we are the owner don't bother, since owner should
2549 * always be allowed to read basic attributes of file.
2551 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2552 (vap->va_uid != crgetuid(cr))) {
2553 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2554 skipaclchk, cr)) {
2555 ZFS_EXIT(zfsvfs);
2556 return (error);
2561 * Return all attributes. It's cheaper to provide the answer
2562 * than to determine whether we were asked the question.
2565 mutex_enter(&zp->z_lock);
2566 vap->va_type = vp->v_type;
2567 vap->va_mode = zp->z_mode & MODEMASK;
2568 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2569 vap->va_nodeid = zp->z_id;
2570 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2571 links = zp->z_links + 1;
2572 else
2573 links = zp->z_links;
2574 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2575 vap->va_size = zp->z_size;
2576 vap->va_rdev = vp->v_rdev;
2577 vap->va_seq = zp->z_seq;
2580 * Add in any requested optional attributes and the create time.
2581 * Also set the corresponding bits in the returned attribute bitmap.
2583 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2584 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2585 xoap->xoa_archive =
2586 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2587 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2590 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2591 xoap->xoa_readonly =
2592 ((zp->z_pflags & ZFS_READONLY) != 0);
2593 XVA_SET_RTN(xvap, XAT_READONLY);
2596 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2597 xoap->xoa_system =
2598 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2599 XVA_SET_RTN(xvap, XAT_SYSTEM);
2602 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2603 xoap->xoa_hidden =
2604 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2605 XVA_SET_RTN(xvap, XAT_HIDDEN);
2608 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2609 xoap->xoa_nounlink =
2610 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2611 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2614 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2615 xoap->xoa_immutable =
2616 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2617 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2620 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2621 xoap->xoa_appendonly =
2622 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2623 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2626 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2627 xoap->xoa_nodump =
2628 ((zp->z_pflags & ZFS_NODUMP) != 0);
2629 XVA_SET_RTN(xvap, XAT_NODUMP);
2632 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2633 xoap->xoa_opaque =
2634 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2635 XVA_SET_RTN(xvap, XAT_OPAQUE);
2638 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2639 xoap->xoa_av_quarantined =
2640 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2641 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2644 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2645 xoap->xoa_av_modified =
2646 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2647 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2650 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2651 vp->v_type == VREG) {
2652 zfs_sa_get_scanstamp(zp, xvap);
2655 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2656 uint64_t times[2];
2658 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2659 times, sizeof (times));
2660 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2661 XVA_SET_RTN(xvap, XAT_CREATETIME);
2664 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2665 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2666 XVA_SET_RTN(xvap, XAT_REPARSE);
2668 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2669 xoap->xoa_generation = zp->z_gen;
2670 XVA_SET_RTN(xvap, XAT_GEN);
2673 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2674 xoap->xoa_offline =
2675 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2676 XVA_SET_RTN(xvap, XAT_OFFLINE);
2679 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2680 xoap->xoa_sparse =
2681 ((zp->z_pflags & ZFS_SPARSE) != 0);
2682 XVA_SET_RTN(xvap, XAT_SPARSE);
2686 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2687 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2688 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2690 mutex_exit(&zp->z_lock);
2692 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2694 if (zp->z_blksz == 0) {
2696 * Block size hasn't been set; suggest maximal I/O transfers.
2698 vap->va_blksize = zfsvfs->z_max_blksz;
2701 ZFS_EXIT(zfsvfs);
2702 return (0);
2706 * Set the file attributes to the values contained in the
2707 * vattr structure.
2709 * IN: vp - vnode of file to be modified.
2710 * vap - new attribute values.
2711 * If AT_XVATTR set, then optional attrs are being set
2712 * flags - ATTR_UTIME set if non-default time values provided.
2713 * - ATTR_NOACLCHECK (CIFS context only).
2714 * cr - credentials of caller.
2715 * ct - caller context
2717 * RETURN: 0 on success, error code on failure.
2719 * Timestamps:
2720 * vp - ctime updated, mtime updated if size changed.
2722 /* ARGSUSED */
2723 static int
2724 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2725 caller_context_t *ct)
2727 znode_t *zp = VTOZ(vp);
2728 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2729 zilog_t *zilog;
2730 dmu_tx_t *tx;
2731 vattr_t oldva;
2732 xvattr_t tmpxvattr;
2733 uint_t mask = vap->va_mask;
2734 uint_t saved_mask = 0;
2735 int trim_mask = 0;
2736 uint64_t new_mode;
2737 uint64_t new_uid, new_gid;
2738 uint64_t xattr_obj;
2739 uint64_t mtime[2], ctime[2];
2740 znode_t *attrzp;
2741 int need_policy = FALSE;
2742 int err, err2;
2743 zfs_fuid_info_t *fuidp = NULL;
2744 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2745 xoptattr_t *xoap;
2746 zfs_acl_t *aclp;
2747 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2748 boolean_t fuid_dirtied = B_FALSE;
2749 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2750 int count = 0, xattr_count = 0;
2752 if (mask == 0)
2753 return (0);
2755 if (mask & AT_NOSET)
2756 return (SET_ERROR(EINVAL));
2758 ZFS_ENTER(zfsvfs);
2759 ZFS_VERIFY_ZP(zp);
2761 zilog = zfsvfs->z_log;
2764 * Make sure that if we have ephemeral uid/gid or xvattr specified
2765 * that file system is at proper version level
2768 if (zfsvfs->z_use_fuids == B_FALSE &&
2769 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2770 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2771 (mask & AT_XVATTR))) {
2772 ZFS_EXIT(zfsvfs);
2773 return (SET_ERROR(EINVAL));
2776 if (mask & AT_SIZE && vp->v_type == VDIR) {
2777 ZFS_EXIT(zfsvfs);
2778 return (SET_ERROR(EISDIR));
2781 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2782 ZFS_EXIT(zfsvfs);
2783 return (SET_ERROR(EINVAL));
2787 * If this is an xvattr_t, then get a pointer to the structure of
2788 * optional attributes. If this is NULL, then we have a vattr_t.
2790 xoap = xva_getxoptattr(xvap);
2792 xva_init(&tmpxvattr);
2795 * Immutable files can only alter immutable bit and atime
2797 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2798 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2799 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2800 ZFS_EXIT(zfsvfs);
2801 return (SET_ERROR(EPERM));
2805 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2809 * Verify timestamps doesn't overflow 32 bits.
2810 * ZFS can handle large timestamps, but 32bit syscalls can't
2811 * handle times greater than 2039. This check should be removed
2812 * once large timestamps are fully supported.
2814 if (mask & (AT_ATIME | AT_MTIME)) {
2815 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2816 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2817 ZFS_EXIT(zfsvfs);
2818 return (SET_ERROR(EOVERFLOW));
2822 top:
2823 attrzp = NULL;
2824 aclp = NULL;
2826 /* Can this be moved to before the top label? */
2827 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2828 ZFS_EXIT(zfsvfs);
2829 return (SET_ERROR(EROFS));
2833 * First validate permissions
2836 if (mask & AT_SIZE) {
2837 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2838 if (err) {
2839 ZFS_EXIT(zfsvfs);
2840 return (err);
2843 * XXX - Note, we are not providing any open
2844 * mode flags here (like FNDELAY), so we may
2845 * block if there are locks present... this
2846 * should be addressed in openat().
2848 /* XXX - would it be OK to generate a log record here? */
2849 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2850 if (err) {
2851 ZFS_EXIT(zfsvfs);
2852 return (err);
2855 if (vap->va_size == 0)
2856 vnevent_truncate(ZTOV(zp), ct);
2859 if (mask & (AT_ATIME|AT_MTIME) ||
2860 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2861 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2862 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2863 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2864 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2865 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2866 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2867 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2868 skipaclchk, cr);
2871 if (mask & (AT_UID|AT_GID)) {
2872 int idmask = (mask & (AT_UID|AT_GID));
2873 int take_owner;
2874 int take_group;
2877 * NOTE: even if a new mode is being set,
2878 * we may clear S_ISUID/S_ISGID bits.
2881 if (!(mask & AT_MODE))
2882 vap->va_mode = zp->z_mode;
2885 * Take ownership or chgrp to group we are a member of
2888 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2889 take_group = (mask & AT_GID) &&
2890 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2893 * If both AT_UID and AT_GID are set then take_owner and
2894 * take_group must both be set in order to allow taking
2895 * ownership.
2897 * Otherwise, send the check through secpolicy_vnode_setattr()
2901 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2902 ((idmask == AT_UID) && take_owner) ||
2903 ((idmask == AT_GID) && take_group)) {
2904 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2905 skipaclchk, cr) == 0) {
2907 * Remove setuid/setgid for non-privileged users
2909 secpolicy_setid_clear(vap, cr);
2910 trim_mask = (mask & (AT_UID|AT_GID));
2911 } else {
2912 need_policy = TRUE;
2914 } else {
2915 need_policy = TRUE;
2919 mutex_enter(&zp->z_lock);
2920 oldva.va_mode = zp->z_mode;
2921 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2922 if (mask & AT_XVATTR) {
2924 * Update xvattr mask to include only those attributes
2925 * that are actually changing.
2927 * the bits will be restored prior to actually setting
2928 * the attributes so the caller thinks they were set.
2930 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2931 if (xoap->xoa_appendonly !=
2932 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2933 need_policy = TRUE;
2934 } else {
2935 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2936 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2940 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2941 if (xoap->xoa_nounlink !=
2942 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2943 need_policy = TRUE;
2944 } else {
2945 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2946 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2950 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2951 if (xoap->xoa_immutable !=
2952 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2953 need_policy = TRUE;
2954 } else {
2955 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2956 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2960 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2961 if (xoap->xoa_nodump !=
2962 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2963 need_policy = TRUE;
2964 } else {
2965 XVA_CLR_REQ(xvap, XAT_NODUMP);
2966 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2970 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2971 if (xoap->xoa_av_modified !=
2972 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2973 need_policy = TRUE;
2974 } else {
2975 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2976 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2980 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2981 if ((vp->v_type != VREG &&
2982 xoap->xoa_av_quarantined) ||
2983 xoap->xoa_av_quarantined !=
2984 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2985 need_policy = TRUE;
2986 } else {
2987 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2988 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2992 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2993 mutex_exit(&zp->z_lock);
2994 ZFS_EXIT(zfsvfs);
2995 return (SET_ERROR(EPERM));
2998 if (need_policy == FALSE &&
2999 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
3000 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
3001 need_policy = TRUE;
3005 mutex_exit(&zp->z_lock);
3007 if (mask & AT_MODE) {
3008 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
3009 err = secpolicy_setid_setsticky_clear(vp, vap,
3010 &oldva, cr);
3011 if (err) {
3012 ZFS_EXIT(zfsvfs);
3013 return (err);
3015 trim_mask |= AT_MODE;
3016 } else {
3017 need_policy = TRUE;
3021 if (need_policy) {
3023 * If trim_mask is set then take ownership
3024 * has been granted or write_acl is present and user
3025 * has the ability to modify mode. In that case remove
3026 * UID|GID and or MODE from mask so that
3027 * secpolicy_vnode_setattr() doesn't revoke it.
3030 if (trim_mask) {
3031 saved_mask = vap->va_mask;
3032 vap->va_mask &= ~trim_mask;
3034 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3035 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3036 if (err) {
3037 ZFS_EXIT(zfsvfs);
3038 return (err);
3041 if (trim_mask)
3042 vap->va_mask |= saved_mask;
3046 * secpolicy_vnode_setattr, or take ownership may have
3047 * changed va_mask
3049 mask = vap->va_mask;
3051 if ((mask & (AT_UID | AT_GID))) {
3052 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3053 &xattr_obj, sizeof (xattr_obj));
3055 if (err == 0 && xattr_obj) {
3056 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3057 if (err)
3058 goto out2;
3060 if (mask & AT_UID) {
3061 new_uid = zfs_fuid_create(zfsvfs,
3062 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3063 if (new_uid != zp->z_uid &&
3064 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3065 if (attrzp)
3066 VN_RELE(ZTOV(attrzp));
3067 err = SET_ERROR(EDQUOT);
3068 goto out2;
3072 if (mask & AT_GID) {
3073 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3074 cr, ZFS_GROUP, &fuidp);
3075 if (new_gid != zp->z_gid &&
3076 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3077 if (attrzp)
3078 VN_RELE(ZTOV(attrzp));
3079 err = SET_ERROR(EDQUOT);
3080 goto out2;
3084 tx = dmu_tx_create(zfsvfs->z_os);
3086 if (mask & AT_MODE) {
3087 uint64_t pmode = zp->z_mode;
3088 uint64_t acl_obj;
3089 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3091 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3092 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3093 err = SET_ERROR(EPERM);
3094 goto out;
3097 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3098 goto out;
3100 mutex_enter(&zp->z_lock);
3101 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3103 * Are we upgrading ACL from old V0 format
3104 * to V1 format?
3106 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3107 zfs_znode_acl_version(zp) ==
3108 ZFS_ACL_VERSION_INITIAL) {
3109 dmu_tx_hold_free(tx, acl_obj, 0,
3110 DMU_OBJECT_END);
3111 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3112 0, aclp->z_acl_bytes);
3113 } else {
3114 dmu_tx_hold_write(tx, acl_obj, 0,
3115 aclp->z_acl_bytes);
3117 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3118 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3119 0, aclp->z_acl_bytes);
3121 mutex_exit(&zp->z_lock);
3122 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3123 } else {
3124 if ((mask & AT_XVATTR) &&
3125 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3126 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3127 else
3128 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3131 if (attrzp) {
3132 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3135 fuid_dirtied = zfsvfs->z_fuid_dirty;
3136 if (fuid_dirtied)
3137 zfs_fuid_txhold(zfsvfs, tx);
3139 zfs_sa_upgrade_txholds(tx, zp);
3141 err = dmu_tx_assign(tx, TXG_WAIT);
3142 if (err)
3143 goto out;
3145 count = 0;
3147 * Set each attribute requested.
3148 * We group settings according to the locks they need to acquire.
3150 * Note: you cannot set ctime directly, although it will be
3151 * updated as a side-effect of calling this function.
3155 if (mask & (AT_UID|AT_GID|AT_MODE))
3156 mutex_enter(&zp->z_acl_lock);
3157 mutex_enter(&zp->z_lock);
3159 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3160 &zp->z_pflags, sizeof (zp->z_pflags));
3162 if (attrzp) {
3163 if (mask & (AT_UID|AT_GID|AT_MODE))
3164 mutex_enter(&attrzp->z_acl_lock);
3165 mutex_enter(&attrzp->z_lock);
3166 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3167 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3168 sizeof (attrzp->z_pflags));
3171 if (mask & (AT_UID|AT_GID)) {
3173 if (mask & AT_UID) {
3174 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3175 &new_uid, sizeof (new_uid));
3176 zp->z_uid = new_uid;
3177 if (attrzp) {
3178 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3179 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3180 sizeof (new_uid));
3181 attrzp->z_uid = new_uid;
3185 if (mask & AT_GID) {
3186 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3187 NULL, &new_gid, sizeof (new_gid));
3188 zp->z_gid = new_gid;
3189 if (attrzp) {
3190 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3191 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3192 sizeof (new_gid));
3193 attrzp->z_gid = new_gid;
3196 if (!(mask & AT_MODE)) {
3197 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3198 NULL, &new_mode, sizeof (new_mode));
3199 new_mode = zp->z_mode;
3201 err = zfs_acl_chown_setattr(zp);
3202 ASSERT(err == 0);
3203 if (attrzp) {
3204 err = zfs_acl_chown_setattr(attrzp);
3205 ASSERT(err == 0);
3209 if (mask & AT_MODE) {
3210 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3211 &new_mode, sizeof (new_mode));
3212 zp->z_mode = new_mode;
3213 ASSERT3U((uintptr_t)aclp, !=, (uintptr_t)NULL);
3214 err = zfs_aclset_common(zp, aclp, cr, tx);
3215 ASSERT0(err);
3216 if (zp->z_acl_cached)
3217 zfs_acl_free(zp->z_acl_cached);
3218 zp->z_acl_cached = aclp;
3219 aclp = NULL;
3223 if (mask & AT_ATIME) {
3224 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3225 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3226 &zp->z_atime, sizeof (zp->z_atime));
3229 if (mask & AT_MTIME) {
3230 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3231 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3232 mtime, sizeof (mtime));
3235 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3236 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3237 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3238 NULL, mtime, sizeof (mtime));
3239 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3240 &ctime, sizeof (ctime));
3241 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3242 B_TRUE);
3243 } else if (mask != 0) {
3244 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3245 &ctime, sizeof (ctime));
3246 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3247 B_TRUE);
3248 if (attrzp) {
3249 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3250 SA_ZPL_CTIME(zfsvfs), NULL,
3251 &ctime, sizeof (ctime));
3252 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3253 mtime, ctime, B_TRUE);
3257 * Do this after setting timestamps to prevent timestamp
3258 * update from toggling bit
3261 if (xoap && (mask & AT_XVATTR)) {
3264 * restore trimmed off masks
3265 * so that return masks can be set for caller.
3268 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3269 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3271 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3272 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3274 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3275 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3277 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3278 XVA_SET_REQ(xvap, XAT_NODUMP);
3280 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3281 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3283 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3284 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3287 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3288 ASSERT(vp->v_type == VREG);
3290 zfs_xvattr_set(zp, xvap, tx);
3293 if (fuid_dirtied)
3294 zfs_fuid_sync(zfsvfs, tx);
3296 if (mask != 0)
3297 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3299 mutex_exit(&zp->z_lock);
3300 if (mask & (AT_UID|AT_GID|AT_MODE))
3301 mutex_exit(&zp->z_acl_lock);
3303 if (attrzp) {
3304 if (mask & (AT_UID|AT_GID|AT_MODE))
3305 mutex_exit(&attrzp->z_acl_lock);
3306 mutex_exit(&attrzp->z_lock);
3308 out:
3309 if (err == 0 && attrzp) {
3310 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3311 xattr_count, tx);
3312 ASSERT(err2 == 0);
3315 if (attrzp)
3316 VN_RELE(ZTOV(attrzp));
3318 if (aclp)
3319 zfs_acl_free(aclp);
3321 if (fuidp) {
3322 zfs_fuid_info_free(fuidp);
3323 fuidp = NULL;
3326 if (err) {
3327 dmu_tx_abort(tx);
3328 if (err == ERESTART)
3329 goto top;
3330 } else {
3331 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3332 dmu_tx_commit(tx);
3335 out2:
3336 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3337 zil_commit(zilog, 0);
3339 ZFS_EXIT(zfsvfs);
3340 return (err);
3343 typedef struct zfs_zlock {
3344 krwlock_t *zl_rwlock; /* lock we acquired */
3345 znode_t *zl_znode; /* znode we held */
3346 struct zfs_zlock *zl_next; /* next in list */
3347 } zfs_zlock_t;
3350 * Drop locks and release vnodes that were held by zfs_rename_lock().
3352 static void
3353 zfs_rename_unlock(zfs_zlock_t **zlpp)
3355 zfs_zlock_t *zl;
3357 while ((zl = *zlpp) != NULL) {
3358 if (zl->zl_znode != NULL)
3359 VN_RELE(ZTOV(zl->zl_znode));
3360 rw_exit(zl->zl_rwlock);
3361 *zlpp = zl->zl_next;
3362 kmem_free(zl, sizeof (*zl));
3367 * Search back through the directory tree, using the ".." entries.
3368 * Lock each directory in the chain to prevent concurrent renames.
3369 * Fail any attempt to move a directory into one of its own descendants.
3370 * XXX - z_parent_lock can overlap with map or grow locks
3372 static int
3373 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3375 zfs_zlock_t *zl;
3376 znode_t *zp = tdzp;
3377 uint64_t rootid = zp->z_zfsvfs->z_root;
3378 uint64_t oidp = zp->z_id;
3379 krwlock_t *rwlp = &szp->z_parent_lock;
3380 krw_t rw = RW_WRITER;
3383 * First pass write-locks szp and compares to zp->z_id.
3384 * Later passes read-lock zp and compare to zp->z_parent.
3386 do {
3387 if (!rw_tryenter(rwlp, rw)) {
3389 * Another thread is renaming in this path.
3390 * Note that if we are a WRITER, we don't have any
3391 * parent_locks held yet.
3393 if (rw == RW_READER && zp->z_id > szp->z_id) {
3395 * Drop our locks and restart
3397 zfs_rename_unlock(&zl);
3398 *zlpp = NULL;
3399 zp = tdzp;
3400 oidp = zp->z_id;
3401 rwlp = &szp->z_parent_lock;
3402 rw = RW_WRITER;
3403 continue;
3404 } else {
3406 * Wait for other thread to drop its locks
3408 rw_enter(rwlp, rw);
3412 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3413 zl->zl_rwlock = rwlp;
3414 zl->zl_znode = NULL;
3415 zl->zl_next = *zlpp;
3416 *zlpp = zl;
3418 if (oidp == szp->z_id) /* We're a descendant of szp */
3419 return (SET_ERROR(EINVAL));
3421 if (oidp == rootid) /* We've hit the top */
3422 return (0);
3424 if (rw == RW_READER) { /* i.e. not the first pass */
3425 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3426 if (error)
3427 return (error);
3428 zl->zl_znode = zp;
3430 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3431 &oidp, sizeof (oidp));
3432 rwlp = &zp->z_parent_lock;
3433 rw = RW_READER;
3435 } while (zp->z_id != sdzp->z_id);
3437 return (0);
3441 * Move an entry from the provided source directory to the target
3442 * directory. Change the entry name as indicated.
3444 * IN: sdvp - Source directory containing the "old entry".
3445 * snm - Old entry name.
3446 * tdvp - Target directory to contain the "new entry".
3447 * tnm - New entry name.
3448 * cr - credentials of caller.
3449 * ct - caller context
3450 * flags - case flags
3452 * RETURN: 0 on success, error code on failure.
3454 * Timestamps:
3455 * sdvp,tdvp - ctime|mtime updated
3457 /*ARGSUSED*/
3458 static int
3459 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3460 caller_context_t *ct, int flags)
3462 znode_t *tdzp, *szp, *tzp;
3463 znode_t *sdzp = VTOZ(sdvp);
3464 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3465 zilog_t *zilog;
3466 vnode_t *realvp;
3467 zfs_dirlock_t *sdl, *tdl;
3468 dmu_tx_t *tx;
3469 zfs_zlock_t *zl;
3470 int cmp, serr, terr;
3471 int error = 0, rm_err = 0;
3472 int zflg = 0;
3473 boolean_t waited = B_FALSE;
3475 ZFS_ENTER(zfsvfs);
3476 ZFS_VERIFY_ZP(sdzp);
3477 zilog = zfsvfs->z_log;
3480 * Make sure we have the real vp for the target directory.
3482 if (fop_realvp(tdvp, &realvp, ct) == 0)
3483 tdvp = realvp;
3485 tdzp = VTOZ(tdvp);
3486 ZFS_VERIFY_ZP(tdzp);
3489 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3490 * ctldir appear to have the same v_vfsp.
3492 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3493 ZFS_EXIT(zfsvfs);
3494 return (SET_ERROR(EXDEV));
3497 if (zfsvfs->z_utf8 && u8_validate(tnm,
3498 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3499 ZFS_EXIT(zfsvfs);
3500 return (SET_ERROR(EILSEQ));
3503 if (flags & FIGNORECASE)
3504 zflg |= ZCILOOK;
3506 top:
3507 szp = NULL;
3508 tzp = NULL;
3509 zl = NULL;
3512 * This is to prevent the creation of links into attribute space
3513 * by renaming a linked file into/outof an attribute directory.
3514 * See the comment in zfs_link() for why this is considered bad.
3516 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3517 ZFS_EXIT(zfsvfs);
3518 return (SET_ERROR(EINVAL));
3522 * Lock source and target directory entries. To prevent deadlock,
3523 * a lock ordering must be defined. We lock the directory with
3524 * the smallest object id first, or if it's a tie, the one with
3525 * the lexically first name.
3527 if (sdzp->z_id < tdzp->z_id) {
3528 cmp = -1;
3529 } else if (sdzp->z_id > tdzp->z_id) {
3530 cmp = 1;
3531 } else {
3533 * First compare the two name arguments without
3534 * considering any case folding.
3536 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3538 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3539 ASSERT(error == 0 || !zfsvfs->z_utf8);
3540 if (cmp == 0) {
3542 * POSIX: "If the old argument and the new argument
3543 * both refer to links to the same existing file,
3544 * the rename() function shall return successfully
3545 * and perform no other action."
3547 ZFS_EXIT(zfsvfs);
3548 return (0);
3551 * If the file system is case-folding, then we may
3552 * have some more checking to do. A case-folding file
3553 * system is either supporting mixed case sensitivity
3554 * access or is completely case-insensitive. Note
3555 * that the file system is always case preserving.
3557 * In mixed sensitivity mode case sensitive behavior
3558 * is the default. FIGNORECASE must be used to
3559 * explicitly request case insensitive behavior.
3561 * If the source and target names provided differ only
3562 * by case (e.g., a request to rename 'tim' to 'Tim'),
3563 * we will treat this as a special case in the
3564 * case-insensitive mode: as long as the source name
3565 * is an exact match, we will allow this to proceed as
3566 * a name-change request.
3568 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3569 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3570 flags & FIGNORECASE)) &&
3571 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3572 &error) == 0) {
3574 * case preserving rename request, require exact
3575 * name matches
3577 zflg |= ZCIEXACT;
3578 zflg &= ~ZCILOOK;
3583 * If the source and destination directories are the same, we should
3584 * grab the z_name_lock of that directory only once.
3586 if (sdzp == tdzp) {
3587 zflg |= ZHAVELOCK;
3588 rw_enter(&sdzp->z_name_lock, RW_READER);
3591 if (cmp < 0) {
3592 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3593 ZEXISTS | zflg, NULL, NULL);
3594 terr = zfs_dirent_lock(&tdl,
3595 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3596 } else {
3597 terr = zfs_dirent_lock(&tdl,
3598 tdzp, tnm, &tzp, zflg, NULL, NULL);
3599 serr = zfs_dirent_lock(&sdl,
3600 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3601 NULL, NULL);
3604 if (serr) {
3606 * Source entry invalid or not there.
3608 if (!terr) {
3609 zfs_dirent_unlock(tdl);
3610 if (tzp)
3611 VN_RELE(ZTOV(tzp));
3614 if (sdzp == tdzp)
3615 rw_exit(&sdzp->z_name_lock);
3617 if (strcmp(snm, "..") == 0)
3618 serr = SET_ERROR(EINVAL);
3619 ZFS_EXIT(zfsvfs);
3620 return (serr);
3622 if (terr) {
3623 zfs_dirent_unlock(sdl);
3624 VN_RELE(ZTOV(szp));
3626 if (sdzp == tdzp)
3627 rw_exit(&sdzp->z_name_lock);
3629 if (strcmp(tnm, "..") == 0)
3630 terr = SET_ERROR(EINVAL);
3631 ZFS_EXIT(zfsvfs);
3632 return (terr);
3636 * Must have write access at the source to remove the old entry
3637 * and write access at the target to create the new entry.
3638 * Note that if target and source are the same, this can be
3639 * done in a single check.
3642 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3643 goto out;
3645 if (ZTOV(szp)->v_type == VDIR) {
3647 * Check to make sure rename is valid.
3648 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3650 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3651 goto out;
3655 * Does target exist?
3657 if (tzp) {
3659 * Source and target must be the same type.
3661 if (ZTOV(szp)->v_type == VDIR) {
3662 if (ZTOV(tzp)->v_type != VDIR) {
3663 error = SET_ERROR(ENOTDIR);
3664 goto out;
3666 } else {
3667 if (ZTOV(tzp)->v_type == VDIR) {
3668 error = SET_ERROR(EISDIR);
3669 goto out;
3673 * POSIX dictates that when the source and target
3674 * entries refer to the same file object, rename
3675 * must do nothing and exit without error.
3677 if (szp->z_id == tzp->z_id) {
3678 error = 0;
3679 goto out;
3683 vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3684 if (tzp)
3685 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3688 * notify the target directory if it is not the same
3689 * as source directory.
3691 if (tdvp != sdvp) {
3692 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3695 tx = dmu_tx_create(zfsvfs->z_os);
3696 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3697 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3698 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3699 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3700 if (sdzp != tdzp) {
3701 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3702 zfs_sa_upgrade_txholds(tx, tdzp);
3704 if (tzp) {
3705 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3706 zfs_sa_upgrade_txholds(tx, tzp);
3709 zfs_sa_upgrade_txholds(tx, szp);
3710 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3711 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3712 if (error) {
3713 if (zl != NULL)
3714 zfs_rename_unlock(&zl);
3715 zfs_dirent_unlock(sdl);
3716 zfs_dirent_unlock(tdl);
3718 if (sdzp == tdzp)
3719 rw_exit(&sdzp->z_name_lock);
3721 VN_RELE(ZTOV(szp));
3722 if (tzp)
3723 VN_RELE(ZTOV(tzp));
3724 if (error == ERESTART) {
3725 waited = B_TRUE;
3726 dmu_tx_wait(tx);
3727 dmu_tx_abort(tx);
3728 goto top;
3730 dmu_tx_abort(tx);
3731 ZFS_EXIT(zfsvfs);
3732 return (error);
3735 if (tzp) /* Attempt to remove the existing target */
3736 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3738 if (error == 0) {
3739 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3740 if (error == 0) {
3741 szp->z_pflags |= ZFS_AV_MODIFIED;
3743 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3744 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3745 ASSERT0(error);
3747 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3748 if (error == 0) {
3749 zfs_log_rename(zilog, tx, TX_RENAME |
3750 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3751 sdl->dl_name, tdzp, tdl->dl_name, szp);
3754 * Update path information for the target vnode
3756 vn_renamepath(tdvp, ZTOV(szp), tnm,
3757 strlen(tnm));
3758 } else {
3760 * At this point, we have successfully created
3761 * the target name, but have failed to remove
3762 * the source name. Since the create was done
3763 * with the ZRENAMING flag, there are
3764 * complications; for one, the link count is
3765 * wrong. The easiest way to deal with this
3766 * is to remove the newly created target, and
3767 * return the original error. This must
3768 * succeed; fortunately, it is very unlikely to
3769 * fail, since we just created it.
3771 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3772 ZRENAMING, NULL), ==, 0);
3777 dmu_tx_commit(tx);
3779 if (tzp && rm_err == 0)
3780 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3782 if (error == 0) {
3783 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3784 /* notify the target dir if it is not the same as source dir */
3785 if (tdvp != sdvp)
3786 vnevent_rename_dest_dir(tdvp, ct);
3788 out:
3789 if (zl != NULL)
3790 zfs_rename_unlock(&zl);
3792 zfs_dirent_unlock(sdl);
3793 zfs_dirent_unlock(tdl);
3795 if (sdzp == tdzp)
3796 rw_exit(&sdzp->z_name_lock);
3799 VN_RELE(ZTOV(szp));
3800 if (tzp)
3801 VN_RELE(ZTOV(tzp));
3803 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3804 zil_commit(zilog, 0);
3806 ZFS_EXIT(zfsvfs);
3807 return (error);
3811 * Insert the indicated symbolic reference entry into the directory.
3813 * IN: dvp - Directory to contain new symbolic link.
3814 * link - Name for new symlink entry.
3815 * vap - Attributes of new entry.
3816 * cr - credentials of caller.
3817 * ct - caller context
3818 * flags - case flags
3820 * RETURN: 0 on success, error code on failure.
3822 * Timestamps:
3823 * dvp - ctime|mtime updated
3825 /*ARGSUSED*/
3826 static int
3827 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3828 caller_context_t *ct, int flags)
3830 znode_t *zp, *dzp = VTOZ(dvp);
3831 zfs_dirlock_t *dl;
3832 dmu_tx_t *tx;
3833 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3834 zilog_t *zilog;
3835 uint64_t len = strlen(link);
3836 int error;
3837 int zflg = ZNEW;
3838 zfs_acl_ids_t acl_ids;
3839 boolean_t fuid_dirtied;
3840 uint64_t txtype = TX_SYMLINK;
3841 boolean_t waited = B_FALSE;
3843 ASSERT(vap->va_type == VLNK);
3845 ZFS_ENTER(zfsvfs);
3846 ZFS_VERIFY_ZP(dzp);
3847 zilog = zfsvfs->z_log;
3849 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3850 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3851 ZFS_EXIT(zfsvfs);
3852 return (SET_ERROR(EILSEQ));
3854 if (flags & FIGNORECASE)
3855 zflg |= ZCILOOK;
3857 if (len > MAXPATHLEN) {
3858 ZFS_EXIT(zfsvfs);
3859 return (SET_ERROR(ENAMETOOLONG));
3862 if ((error = zfs_acl_ids_create(dzp, 0,
3863 vap, cr, NULL, &acl_ids)) != 0) {
3864 ZFS_EXIT(zfsvfs);
3865 return (error);
3867 top:
3869 * Attempt to lock directory; fail if entry already exists.
3871 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3872 if (error) {
3873 zfs_acl_ids_free(&acl_ids);
3874 ZFS_EXIT(zfsvfs);
3875 return (error);
3878 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3879 zfs_acl_ids_free(&acl_ids);
3880 zfs_dirent_unlock(dl);
3881 ZFS_EXIT(zfsvfs);
3882 return (error);
3885 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3886 zfs_acl_ids_free(&acl_ids);
3887 zfs_dirent_unlock(dl);
3888 ZFS_EXIT(zfsvfs);
3889 return (SET_ERROR(EDQUOT));
3891 tx = dmu_tx_create(zfsvfs->z_os);
3892 fuid_dirtied = zfsvfs->z_fuid_dirty;
3893 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3894 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3895 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3896 ZFS_SA_BASE_ATTR_SIZE + len);
3897 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3898 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3899 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3900 acl_ids.z_aclp->z_acl_bytes);
3902 if (fuid_dirtied)
3903 zfs_fuid_txhold(zfsvfs, tx);
3904 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3905 if (error) {
3906 zfs_dirent_unlock(dl);
3907 if (error == ERESTART) {
3908 waited = B_TRUE;
3909 dmu_tx_wait(tx);
3910 dmu_tx_abort(tx);
3911 goto top;
3913 zfs_acl_ids_free(&acl_ids);
3914 dmu_tx_abort(tx);
3915 ZFS_EXIT(zfsvfs);
3916 return (error);
3920 * Create a new object for the symlink.
3921 * for version 4 ZPL datsets the symlink will be an SA attribute
3923 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3925 if (fuid_dirtied)
3926 zfs_fuid_sync(zfsvfs, tx);
3928 mutex_enter(&zp->z_lock);
3929 if (zp->z_is_sa)
3930 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3931 link, len, tx);
3932 else
3933 zfs_sa_symlink(zp, link, len, tx);
3934 mutex_exit(&zp->z_lock);
3936 zp->z_size = len;
3937 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3938 &zp->z_size, sizeof (zp->z_size), tx);
3940 * Insert the new object into the directory.
3942 (void) zfs_link_create(dl, zp, tx, ZNEW);
3944 if (flags & FIGNORECASE)
3945 txtype |= TX_CI;
3946 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3948 zfs_acl_ids_free(&acl_ids);
3950 dmu_tx_commit(tx);
3952 zfs_dirent_unlock(dl);
3954 VN_RELE(ZTOV(zp));
3956 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3957 zil_commit(zilog, 0);
3959 ZFS_EXIT(zfsvfs);
3960 return (error);
3964 * Return, in the buffer contained in the provided uio structure,
3965 * the symbolic path referred to by vp.
3967 * IN: vp - vnode of symbolic link.
3968 * uio - structure to contain the link path.
3969 * cr - credentials of caller.
3970 * ct - caller context
3972 * OUT: uio - structure containing the link path.
3974 * RETURN: 0 on success, error code on failure.
3976 * Timestamps:
3977 * vp - atime updated
3979 /* ARGSUSED */
3980 static int
3981 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3983 znode_t *zp = VTOZ(vp);
3984 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3985 int error;
3987 ZFS_ENTER(zfsvfs);
3988 ZFS_VERIFY_ZP(zp);
3990 mutex_enter(&zp->z_lock);
3991 if (zp->z_is_sa)
3992 error = sa_lookup_uio(zp->z_sa_hdl,
3993 SA_ZPL_SYMLINK(zfsvfs), uio);
3994 else
3995 error = zfs_sa_readlink(zp, uio);
3996 mutex_exit(&zp->z_lock);
3998 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4000 ZFS_EXIT(zfsvfs);
4001 return (error);
4005 * Insert a new entry into directory tdvp referencing svp.
4007 * IN: tdvp - Directory to contain new entry.
4008 * svp - vnode of new entry.
4009 * name - name of new entry.
4010 * cr - credentials of caller.
4011 * ct - caller context
4013 * RETURN: 0 on success, error code on failure.
4015 * Timestamps:
4016 * tdvp - ctime|mtime updated
4017 * svp - ctime updated
4019 /* ARGSUSED */
4020 static int
4021 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4022 caller_context_t *ct, int flags)
4024 znode_t *dzp = VTOZ(tdvp);
4025 znode_t *tzp, *szp;
4026 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4027 zilog_t *zilog;
4028 zfs_dirlock_t *dl;
4029 dmu_tx_t *tx;
4030 vnode_t *realvp;
4031 int error;
4032 int zf = ZNEW;
4033 uint64_t parent;
4034 uid_t owner;
4035 boolean_t waited = B_FALSE;
4037 ASSERT(tdvp->v_type == VDIR);
4039 ZFS_ENTER(zfsvfs);
4040 ZFS_VERIFY_ZP(dzp);
4041 zilog = zfsvfs->z_log;
4043 if (fop_realvp(svp, &realvp, ct) == 0)
4044 svp = realvp;
4047 * POSIX dictates that we return EPERM here.
4048 * Better choices include ENOTSUP or EISDIR.
4050 if (svp->v_type == VDIR) {
4051 ZFS_EXIT(zfsvfs);
4052 return (SET_ERROR(EPERM));
4055 szp = VTOZ(svp);
4056 ZFS_VERIFY_ZP(szp);
4059 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4060 * ctldir appear to have the same v_vfsp.
4062 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4063 ZFS_EXIT(zfsvfs);
4064 return (SET_ERROR(EXDEV));
4067 /* Prevent links to .zfs/shares files */
4069 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4070 &parent, sizeof (uint64_t))) != 0) {
4071 ZFS_EXIT(zfsvfs);
4072 return (error);
4074 if (parent == zfsvfs->z_shares_dir) {
4075 ZFS_EXIT(zfsvfs);
4076 return (SET_ERROR(EPERM));
4079 if (zfsvfs->z_utf8 && u8_validate(name,
4080 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4081 ZFS_EXIT(zfsvfs);
4082 return (SET_ERROR(EILSEQ));
4084 if (flags & FIGNORECASE)
4085 zf |= ZCILOOK;
4088 * We do not support links between attributes and non-attributes
4089 * because of the potential security risk of creating links
4090 * into "normal" file space in order to circumvent restrictions
4091 * imposed in attribute space.
4093 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4094 ZFS_EXIT(zfsvfs);
4095 return (SET_ERROR(EINVAL));
4099 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4100 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4101 ZFS_EXIT(zfsvfs);
4102 return (SET_ERROR(EPERM));
4105 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4106 ZFS_EXIT(zfsvfs);
4107 return (error);
4110 top:
4112 * Attempt to lock directory; fail if entry already exists.
4114 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4115 if (error) {
4116 ZFS_EXIT(zfsvfs);
4117 return (error);
4120 tx = dmu_tx_create(zfsvfs->z_os);
4121 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4122 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4123 zfs_sa_upgrade_txholds(tx, szp);
4124 zfs_sa_upgrade_txholds(tx, dzp);
4125 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4126 if (error) {
4127 zfs_dirent_unlock(dl);
4128 if (error == ERESTART) {
4129 waited = B_TRUE;
4130 dmu_tx_wait(tx);
4131 dmu_tx_abort(tx);
4132 goto top;
4134 dmu_tx_abort(tx);
4135 ZFS_EXIT(zfsvfs);
4136 return (error);
4139 error = zfs_link_create(dl, szp, tx, 0);
4141 if (error == 0) {
4142 uint64_t txtype = TX_LINK;
4143 if (flags & FIGNORECASE)
4144 txtype |= TX_CI;
4145 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4148 dmu_tx_commit(tx);
4150 zfs_dirent_unlock(dl);
4152 if (error == 0) {
4153 vnevent_link(svp, ct);
4156 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4157 zil_commit(zilog, 0);
4159 ZFS_EXIT(zfsvfs);
4160 return (error);
4164 * zfs_null_putapage() is used when the file system has been force
4165 * unmounted. It just drops the pages.
4167 /* ARGSUSED */
4168 static int
4169 zfs_null_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4170 size_t *lenp, int flags, cred_t *cr)
4172 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4173 return (0);
4177 * Push a page out to disk, klustering if possible.
4179 * IN: vp - file to push page to.
4180 * pp - page to push.
4181 * flags - additional flags.
4182 * cr - credentials of caller.
4184 * OUT: offp - start of range pushed.
4185 * lenp - len of range pushed.
4187 * RETURN: 0 on success, error code on failure.
4189 * NOTE: callers must have locked the page to be pushed. On
4190 * exit, the page (and all other pages in the kluster) must be
4191 * unlocked.
4193 /* ARGSUSED */
4194 static int
4195 zfs_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4196 size_t *lenp, int flags, cred_t *cr)
4198 znode_t *zp = VTOZ(vp);
4199 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4200 dmu_tx_t *tx;
4201 uoff_t off, koff;
4202 size_t len, klen;
4203 int err;
4205 off = pp->p_offset;
4206 len = PAGESIZE;
4208 * If our blocksize is bigger than the page size, try to kluster
4209 * multiple pages so that we write a full block (thus avoiding
4210 * a read-modify-write).
4212 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4213 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4214 koff = ISP2(klen) ? P2ALIGN(off, (uoff_t)klen) : 0;
4215 ASSERT(koff <= zp->z_size);
4216 if (koff + klen > zp->z_size)
4217 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4218 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4220 ASSERT3U(btop(len), ==, btopr(len));
4223 * Can't push pages past end-of-file.
4225 if (off >= zp->z_size) {
4226 /* ignore all pages */
4227 err = 0;
4228 goto out;
4229 } else if (off + len > zp->z_size) {
4230 int npages = btopr(zp->z_size - off);
4231 page_t *trunc;
4233 page_list_break(&pp, &trunc, npages);
4234 /* ignore pages past end of file */
4235 if (trunc)
4236 pvn_write_done(trunc, flags);
4237 len = zp->z_size - off;
4240 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4241 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4242 err = SET_ERROR(EDQUOT);
4243 goto out;
4245 tx = dmu_tx_create(zfsvfs->z_os);
4246 dmu_tx_hold_write(tx, zp->z_id, off, len);
4248 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4249 zfs_sa_upgrade_txholds(tx, zp);
4250 err = dmu_tx_assign(tx, TXG_WAIT);
4251 if (err != 0) {
4252 dmu_tx_abort(tx);
4253 goto out;
4256 if (zp->z_blksz <= PAGESIZE) {
4257 caddr_t va = zfs_map_page(pp, S_READ);
4258 ASSERT3U(len, <=, PAGESIZE);
4259 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4260 zfs_unmap_page(pp, va);
4261 } else {
4262 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4265 if (err == 0) {
4266 uint64_t mtime[2], ctime[2];
4267 sa_bulk_attr_t bulk[3];
4268 int count = 0;
4270 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4271 &mtime, 16);
4272 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4273 &ctime, 16);
4274 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4275 &zp->z_pflags, 8);
4276 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4277 B_TRUE);
4278 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4279 ASSERT0(err);
4280 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4282 dmu_tx_commit(tx);
4284 out:
4285 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4286 if (offp)
4287 *offp = off;
4288 if (lenp)
4289 *lenp = len;
4291 return (err);
4295 * Copy the portion of the file indicated from pages into the file.
4296 * The pages are stored in a page list attached to the files vnode.
4298 * IN: vp - vnode of file to push page data to.
4299 * off - position in file to put data.
4300 * len - amount of data to write.
4301 * flags - flags to control the operation.
4302 * cr - credentials of caller.
4303 * ct - caller context.
4305 * RETURN: 0 on success, error code on failure.
4307 * Timestamps:
4308 * vp - ctime|mtime updated
4310 /*ARGSUSED*/
4311 static int
4312 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4313 caller_context_t *ct)
4315 znode_t *zp = VTOZ(vp);
4316 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4317 page_t *pp;
4318 size_t io_len;
4319 uoff_t io_off;
4320 uint_t blksz;
4321 rl_t *rl;
4322 int error = 0;
4324 ZFS_ENTER(zfsvfs);
4325 ZFS_VERIFY_ZP(zp);
4328 * There's nothing to do if no data is cached.
4330 if (!vn_has_cached_data(vp)) {
4331 ZFS_EXIT(zfsvfs);
4332 return (0);
4336 * Align this request to the file block size in case we kluster.
4337 * XXX - this can result in pretty aggresive locking, which can
4338 * impact simultanious read/write access. One option might be
4339 * to break up long requests (len == 0) into block-by-block
4340 * operations to get narrower locking.
4342 blksz = zp->z_blksz;
4343 if (ISP2(blksz))
4344 io_off = P2ALIGN_TYPED(off, blksz, uoff_t);
4345 else
4346 io_off = 0;
4347 if (len > 0 && ISP2(blksz))
4348 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4349 else
4350 io_len = 0;
4352 if (io_len == 0) {
4354 * Search the entire vp list for pages >= io_off.
4356 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4357 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4358 goto out;
4360 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4362 if (off > zp->z_size) {
4363 /* past end of file */
4364 zfs_range_unlock(rl);
4365 ZFS_EXIT(zfsvfs);
4366 return (0);
4369 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4371 for (off = io_off; io_off < off + len; io_off += io_len) {
4372 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4373 pp = page_lookup(&vp->v_object, io_off,
4374 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4375 } else {
4376 pp = page_lookup_nowait(&vp->v_object, io_off,
4377 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4380 if (pp != NULL && pvn_getdirty(pp, flags)) {
4381 int err;
4384 * Found a dirty page to push
4386 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4387 if (err)
4388 error = err;
4389 } else {
4390 io_len = PAGESIZE;
4393 out:
4394 zfs_range_unlock(rl);
4395 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4396 zil_commit(zfsvfs->z_log, zp->z_id);
4397 ZFS_EXIT(zfsvfs);
4398 return (error);
4401 /*ARGSUSED*/
4402 void
4403 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4405 znode_t *zp = VTOZ(vp);
4406 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4407 int error;
4409 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4410 if (zp->z_sa_hdl == NULL) {
4412 * The fs has been unmounted, or we did a
4413 * suspend/resume and this file no longer exists.
4415 if (vn_has_cached_data(vp)) {
4416 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4417 B_INVAL, cr);
4420 mutex_enter(&zp->z_lock);
4421 mutex_enter(&vp->v_lock);
4422 ASSERT(vp->v_count == 1);
4423 VN_RELE_LOCKED(vp);
4424 mutex_exit(&vp->v_lock);
4425 mutex_exit(&zp->z_lock);
4426 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4427 zfs_znode_free(zp);
4428 return;
4432 * Attempt to push any data in the page cache. If this fails
4433 * we will get kicked out later in zfs_zinactive().
4435 if (vn_has_cached_data(vp)) {
4436 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4437 cr);
4440 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4441 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4443 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4444 zfs_sa_upgrade_txholds(tx, zp);
4445 error = dmu_tx_assign(tx, TXG_WAIT);
4446 if (error) {
4447 dmu_tx_abort(tx);
4448 } else {
4449 mutex_enter(&zp->z_lock);
4450 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4451 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4452 zp->z_atime_dirty = 0;
4453 mutex_exit(&zp->z_lock);
4454 dmu_tx_commit(tx);
4458 zfs_zinactive(zp);
4459 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4463 * Bounds-check the seek operation.
4465 * IN: vp - vnode seeking within
4466 * ooff - old file offset
4467 * noffp - pointer to new file offset
4468 * ct - caller context
4470 * RETURN: 0 on success, EINVAL if new offset invalid.
4472 /* ARGSUSED */
4473 static int
4474 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4475 caller_context_t *ct)
4477 if (vp->v_type == VDIR)
4478 return (0);
4479 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4483 * Pre-filter the generic locking function to trap attempts to place
4484 * a mandatory lock on a memory mapped file.
4486 static int
4487 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4488 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4490 znode_t *zp = VTOZ(vp);
4491 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4493 ZFS_ENTER(zfsvfs);
4494 ZFS_VERIFY_ZP(zp);
4497 * We are following the UFS semantics with respect to mapcnt
4498 * here: If we see that the file is mapped already, then we will
4499 * return an error, but we don't worry about races between this
4500 * function and zfs_map().
4502 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4503 ZFS_EXIT(zfsvfs);
4504 return (SET_ERROR(EAGAIN));
4506 ZFS_EXIT(zfsvfs);
4507 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4511 * If we can't find a page in the cache, we will create a new page
4512 * and fill it with file data. For efficiency, we may try to fill
4513 * multiple pages at once (klustering) to fill up the supplied page
4514 * list. Note that the pages to be filled are held with an exclusive
4515 * lock to prevent access by other threads while they are being filled.
4517 static int
4518 zfs_fillpage(vnode_t *vp, uoff_t off, struct seg *seg,
4519 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4521 znode_t *zp = VTOZ(vp);
4522 page_t *pp, *cur_pp;
4523 objset_t *os = zp->z_zfsvfs->z_os;
4524 uoff_t io_off, total;
4525 size_t io_len;
4526 int err;
4528 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4530 * We only have a single page, don't bother klustering
4532 io_off = off;
4533 io_len = PAGESIZE;
4534 pp = page_create_va(&vp->v_object, io_off, io_len,
4535 PG_EXCL | PG_WAIT, seg, addr);
4536 } else {
4538 * Try to find enough pages to fill the page list
4540 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4541 &io_len, off, plsz, 0);
4543 if (pp == NULL) {
4545 * The page already exists, nothing to do here.
4547 *pl = NULL;
4548 return (0);
4552 * Fill the pages in the kluster.
4554 cur_pp = pp;
4555 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4556 caddr_t va;
4558 ASSERT3U(io_off, ==, cur_pp->p_offset);
4559 va = zfs_map_page(cur_pp, S_WRITE);
4560 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4561 DMU_READ_PREFETCH);
4562 zfs_unmap_page(cur_pp, va);
4563 if (err) {
4564 /* On error, toss the entire kluster */
4565 pvn_read_done(pp, B_ERROR);
4566 /* convert checksum errors into IO errors */
4567 if (err == ECKSUM)
4568 err = SET_ERROR(EIO);
4569 return (err);
4571 cur_pp = cur_pp->p_next;
4575 * Fill in the page list array from the kluster starting
4576 * from the desired offset `off'.
4577 * NOTE: the page list will always be null terminated.
4579 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4580 ASSERT(pl == NULL || (*pl)->p_offset == off);
4582 return (0);
4586 * Return pointers to the pages for the file region [off, off + len]
4587 * in the pl array. If plsz is greater than len, this function may
4588 * also return page pointers from after the specified region
4589 * (i.e. the region [off, off + plsz]). These additional pages are
4590 * only returned if they are already in the cache, or were created as
4591 * part of a klustered read.
4593 * IN: vp - vnode of file to get data from.
4594 * off - position in file to get data from.
4595 * len - amount of data to retrieve.
4596 * plsz - length of provided page list.
4597 * seg - segment to obtain pages for.
4598 * addr - virtual address of fault.
4599 * rw - mode of created pages.
4600 * cr - credentials of caller.
4601 * ct - caller context.
4603 * OUT: protp - protection mode of created pages.
4604 * pl - list of pages created.
4606 * RETURN: 0 on success, error code on failure.
4608 * Timestamps:
4609 * vp - atime updated
4611 /* ARGSUSED */
4612 static int
4613 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4614 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4615 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4617 znode_t *zp = VTOZ(vp);
4618 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4619 page_t **pl0 = pl;
4620 int err = 0;
4622 /* we do our own caching, faultahead is unnecessary */
4623 if (pl == NULL)
4624 return (0);
4625 else if (len > plsz)
4626 len = plsz;
4627 else
4628 len = P2ROUNDUP(len, PAGESIZE);
4629 ASSERT(plsz >= len);
4631 ZFS_ENTER(zfsvfs);
4632 ZFS_VERIFY_ZP(zp);
4634 if (protp)
4635 *protp = PROT_ALL;
4638 * Loop through the requested range [off, off + len) looking
4639 * for pages. If we don't find a page, we will need to create
4640 * a new page and fill it with data from the file.
4642 while (len > 0) {
4643 if (*pl = page_lookup(&vp->v_object, off, SE_SHARED))
4644 *(pl+1) = NULL;
4645 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4646 goto out;
4647 while (*pl) {
4648 ASSERT3U((*pl)->p_offset, ==, off);
4649 off += PAGESIZE;
4650 addr += PAGESIZE;
4651 if (len > 0) {
4652 ASSERT3U(len, >=, PAGESIZE);
4653 len -= PAGESIZE;
4655 ASSERT3U(plsz, >=, PAGESIZE);
4656 plsz -= PAGESIZE;
4657 pl++;
4662 * Fill out the page array with any pages already in the cache.
4664 while (plsz > 0 &&
4665 (*pl++ = page_lookup_nowait(&vp->v_object, off, SE_SHARED))) {
4666 off += PAGESIZE;
4667 plsz -= PAGESIZE;
4669 out:
4670 if (err) {
4672 * Release any pages we have previously locked.
4674 while (pl > pl0)
4675 page_unlock(*--pl);
4676 } else {
4677 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4680 *pl = NULL;
4682 ZFS_EXIT(zfsvfs);
4683 return (err);
4687 * Request a memory map for a section of a file. This code interacts
4688 * with common code and the VM system as follows:
4690 * - common code calls mmap(), which ends up in smmap_common()
4691 * - this calls fop_map(), which takes you into (say) zfs
4692 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4693 * - segvn_create() creates the new segment and calls fop_addmap()
4694 * - zfs_addmap() updates z_mapcnt
4696 /*ARGSUSED*/
4697 static int
4698 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4699 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4700 caller_context_t *ct)
4702 znode_t *zp = VTOZ(vp);
4703 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4704 segvn_crargs_t vn_a;
4705 int error;
4707 ZFS_ENTER(zfsvfs);
4708 ZFS_VERIFY_ZP(zp);
4711 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
4714 if ((prot & PROT_WRITE) && (zp->z_pflags &
4715 (ZFS_IMMUTABLE | ZFS_APPENDONLY))) {
4716 ZFS_EXIT(zfsvfs);
4717 return (SET_ERROR(EPERM));
4720 if ((prot & (PROT_READ | PROT_EXEC)) &&
4721 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4722 ZFS_EXIT(zfsvfs);
4723 return (SET_ERROR(EACCES));
4726 if (vp->v_flag & VNOMAP) {
4727 ZFS_EXIT(zfsvfs);
4728 return (SET_ERROR(ENOSYS));
4731 if (off < 0 || len > MAXOFFSET_T - off) {
4732 ZFS_EXIT(zfsvfs);
4733 return (SET_ERROR(ENXIO));
4736 if (vp->v_type != VREG) {
4737 ZFS_EXIT(zfsvfs);
4738 return (SET_ERROR(ENODEV));
4742 * If file is locked, disallow mapping.
4744 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4745 ZFS_EXIT(zfsvfs);
4746 return (SET_ERROR(EAGAIN));
4749 as_rangelock(as);
4750 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4751 if (error != 0) {
4752 as_rangeunlock(as);
4753 ZFS_EXIT(zfsvfs);
4754 return (error);
4757 vn_a.vp = vp;
4758 vn_a.offset = (uoff_t)off;
4759 vn_a.type = flags & MAP_TYPE;
4760 vn_a.prot = prot;
4761 vn_a.maxprot = maxprot;
4762 vn_a.cred = cr;
4763 vn_a.amp = NULL;
4764 vn_a.flags = flags & ~MAP_TYPE;
4765 vn_a.szc = 0;
4766 vn_a.lgrp_mem_policy_flags = 0;
4768 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4770 as_rangeunlock(as);
4771 ZFS_EXIT(zfsvfs);
4772 return (error);
4775 /* ARGSUSED */
4776 static int
4777 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4778 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4779 caller_context_t *ct)
4781 uint64_t pages = btopr(len);
4783 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4784 return (0);
4788 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4789 * more accurate mtime for the associated file. Since we don't have a way of
4790 * detecting when the data was actually modified, we have to resort to
4791 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4792 * last page is pushed. The problem occurs when the msync() call is omitted,
4793 * which by far the most common case:
4795 * open()
4796 * mmap()
4797 * <modify memory>
4798 * munmap()
4799 * close()
4800 * <time lapse>
4801 * putpage() via fsflush
4803 * If we wait until fsflush to come along, we can have a modification time that
4804 * is some arbitrary point in the future. In order to prevent this in the
4805 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4806 * torn down.
4808 /* ARGSUSED */
4809 static int
4810 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4811 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4812 caller_context_t *ct)
4814 uint64_t pages = btopr(len);
4816 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4817 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4819 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4820 vn_has_cached_data(vp))
4821 (void) fop_putpage(vp, off, len, B_ASYNC, cr, ct);
4823 return (0);
4827 * Free or allocate space in a file. Currently, this function only
4828 * supports the `F_FREESP' command. However, this command is somewhat
4829 * misnamed, as its functionality includes the ability to allocate as
4830 * well as free space.
4832 * IN: vp - vnode of file to free data in.
4833 * cmd - action to take (only F_FREESP supported).
4834 * bfp - section of file to free/alloc.
4835 * flag - current file open mode flags.
4836 * offset - current file offset.
4837 * cr - credentials of caller [UNUSED].
4838 * ct - caller context.
4840 * RETURN: 0 on success, error code on failure.
4842 * Timestamps:
4843 * vp - ctime|mtime updated
4845 /* ARGSUSED */
4846 static int
4847 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4848 offset_t offset, cred_t *cr, caller_context_t *ct)
4850 znode_t *zp = VTOZ(vp);
4851 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4852 uint64_t off, len;
4853 int error;
4855 ZFS_ENTER(zfsvfs);
4856 ZFS_VERIFY_ZP(zp);
4858 if (cmd != F_FREESP) {
4859 ZFS_EXIT(zfsvfs);
4860 return (SET_ERROR(EINVAL));
4864 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4865 * callers might not be able to detect properly that we are read-only,
4866 * so check it explicitly here.
4868 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4869 ZFS_EXIT(zfsvfs);
4870 return (SET_ERROR(EROFS));
4873 if (error = convoff(vp, bfp, 0, offset)) {
4874 ZFS_EXIT(zfsvfs);
4875 return (error);
4878 if (bfp->l_len < 0) {
4879 ZFS_EXIT(zfsvfs);
4880 return (SET_ERROR(EINVAL));
4883 off = bfp->l_start;
4884 len = bfp->l_len; /* 0 means from off to end of file */
4886 error = zfs_freesp(zp, off, len, flag, TRUE);
4888 if (error == 0 && off == 0 && len == 0)
4889 vnevent_truncate(ZTOV(zp), ct);
4891 ZFS_EXIT(zfsvfs);
4892 return (error);
4895 /*ARGSUSED*/
4896 static int
4897 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4899 znode_t *zp = VTOZ(vp);
4900 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4901 uint32_t gen;
4902 uint64_t gen64;
4903 uint64_t object = zp->z_id;
4904 zfid_short_t *zfid;
4905 int size, i, error;
4907 ZFS_ENTER(zfsvfs);
4908 ZFS_VERIFY_ZP(zp);
4910 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4911 &gen64, sizeof (uint64_t))) != 0) {
4912 ZFS_EXIT(zfsvfs);
4913 return (error);
4916 gen = (uint32_t)gen64;
4918 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4919 if (fidp->fid_len < size) {
4920 fidp->fid_len = size;
4921 ZFS_EXIT(zfsvfs);
4922 return (SET_ERROR(ENOSPC));
4925 zfid = (zfid_short_t *)fidp;
4927 zfid->zf_len = size;
4929 for (i = 0; i < sizeof (zfid->zf_object); i++)
4930 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4932 /* Must have a non-zero generation number to distinguish from .zfs */
4933 if (gen == 0)
4934 gen = 1;
4935 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4936 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4938 if (size == LONG_FID_LEN) {
4939 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4940 zfid_long_t *zlfid;
4942 zlfid = (zfid_long_t *)fidp;
4944 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4945 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4947 /* XXX - this should be the generation number for the objset */
4948 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4949 zlfid->zf_setgen[i] = 0;
4952 ZFS_EXIT(zfsvfs);
4953 return (0);
4956 static int
4957 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4958 caller_context_t *ct)
4960 znode_t *zp, *xzp;
4961 zfsvfs_t *zfsvfs;
4962 zfs_dirlock_t *dl;
4963 int error;
4965 switch (cmd) {
4966 case _PC_LINK_MAX:
4967 *valp = ULONG_MAX;
4968 return (0);
4970 case _PC_FILESIZEBITS:
4971 *valp = 64;
4972 return (0);
4974 case _PC_XATTR_EXISTS:
4975 zp = VTOZ(vp);
4976 zfsvfs = zp->z_zfsvfs;
4977 ZFS_ENTER(zfsvfs);
4978 ZFS_VERIFY_ZP(zp);
4979 *valp = 0;
4980 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4981 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4982 if (error == 0) {
4983 zfs_dirent_unlock(dl);
4984 if (!zfs_dirempty(xzp))
4985 *valp = 1;
4986 VN_RELE(ZTOV(xzp));
4987 } else if (error == ENOENT) {
4989 * If there aren't extended attributes, it's the
4990 * same as having zero of them.
4992 error = 0;
4994 ZFS_EXIT(zfsvfs);
4995 return (error);
4997 case _PC_SATTR_ENABLED:
4998 case _PC_SATTR_EXISTS:
4999 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5000 (vp->v_type == VREG || vp->v_type == VDIR);
5001 return (0);
5003 case _PC_ACCESS_FILTERING:
5004 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
5005 vp->v_type == VDIR;
5006 return (0);
5008 case _PC_ACL_ENABLED:
5009 *valp = _ACL_ACE_ENABLED;
5010 return (0);
5012 case _PC_MIN_HOLE_SIZE:
5013 *valp = (ulong_t)SPA_MINBLOCKSIZE;
5014 return (0);
5016 case _PC_TIMESTAMP_RESOLUTION:
5017 /* nanosecond timestamp resolution */
5018 *valp = 1L;
5019 return (0);
5021 default:
5022 return (fs_pathconf(vp, cmd, valp, cr, ct));
5026 /*ARGSUSED*/
5027 static int
5028 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5029 caller_context_t *ct)
5031 znode_t *zp = VTOZ(vp);
5032 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5033 int error;
5034 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5036 ZFS_ENTER(zfsvfs);
5037 ZFS_VERIFY_ZP(zp);
5038 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5039 ZFS_EXIT(zfsvfs);
5041 return (error);
5044 /*ARGSUSED*/
5045 static int
5046 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5047 caller_context_t *ct)
5049 znode_t *zp = VTOZ(vp);
5050 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5051 int error;
5052 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5053 zilog_t *zilog = zfsvfs->z_log;
5055 ZFS_ENTER(zfsvfs);
5056 ZFS_VERIFY_ZP(zp);
5058 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5060 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5061 zil_commit(zilog, 0);
5063 ZFS_EXIT(zfsvfs);
5064 return (error);
5068 * The smallest read we may consider to loan out an arcbuf.
5069 * This must be a power of 2.
5071 int zcr_blksz_min = (1 << 10); /* 1K */
5073 * If set to less than the file block size, allow loaning out of an
5074 * arcbuf for a partial block read. This must be a power of 2.
5076 int zcr_blksz_max = (1 << 17); /* 128K */
5078 /*ARGSUSED*/
5079 static int
5080 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5081 caller_context_t *ct)
5083 znode_t *zp = VTOZ(vp);
5084 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5085 int max_blksz = zfsvfs->z_max_blksz;
5086 uio_t *uio = &xuio->xu_uio;
5087 ssize_t size = uio->uio_resid;
5088 offset_t offset = uio->uio_loffset;
5089 int blksz;
5090 int fullblk, i;
5091 arc_buf_t *abuf;
5092 ssize_t maxsize;
5093 int preamble, postamble;
5095 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5096 return (SET_ERROR(EINVAL));
5098 ZFS_ENTER(zfsvfs);
5099 ZFS_VERIFY_ZP(zp);
5100 switch (ioflag) {
5101 case UIO_WRITE:
5103 * Loan out an arc_buf for write if write size is bigger than
5104 * max_blksz, and the file's block size is also max_blksz.
5106 blksz = max_blksz;
5107 if (size < blksz || zp->z_blksz != blksz) {
5108 ZFS_EXIT(zfsvfs);
5109 return (SET_ERROR(EINVAL));
5112 * Caller requests buffers for write before knowing where the
5113 * write offset might be (e.g. NFS TCP write).
5115 if (offset == -1) {
5116 preamble = 0;
5117 } else {
5118 preamble = P2PHASE(offset, blksz);
5119 if (preamble) {
5120 preamble = blksz - preamble;
5121 size -= preamble;
5125 postamble = P2PHASE(size, blksz);
5126 size -= postamble;
5128 fullblk = size / blksz;
5129 (void) dmu_xuio_init(xuio,
5130 (preamble != 0) + fullblk + (postamble != 0));
5131 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5132 int, postamble, int,
5133 (preamble != 0) + fullblk + (postamble != 0));
5136 * Have to fix iov base/len for partial buffers. They
5137 * currently represent full arc_buf's.
5139 if (preamble) {
5140 /* data begins in the middle of the arc_buf */
5141 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5142 blksz);
5143 ASSERT(abuf);
5144 (void) dmu_xuio_add(xuio, abuf,
5145 blksz - preamble, preamble);
5148 for (i = 0; i < fullblk; i++) {
5149 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5150 blksz);
5151 ASSERT(abuf);
5152 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5155 if (postamble) {
5156 /* data ends in the middle of the arc_buf */
5157 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5158 blksz);
5159 ASSERT(abuf);
5160 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5162 break;
5163 case UIO_READ:
5165 * Loan out an arc_buf for read if the read size is larger than
5166 * the current file block size. Block alignment is not
5167 * considered. Partial arc_buf will be loaned out for read.
5169 blksz = zp->z_blksz;
5170 if (blksz < zcr_blksz_min)
5171 blksz = zcr_blksz_min;
5172 if (blksz > zcr_blksz_max)
5173 blksz = zcr_blksz_max;
5174 /* avoid potential complexity of dealing with it */
5175 if (blksz > max_blksz) {
5176 ZFS_EXIT(zfsvfs);
5177 return (SET_ERROR(EINVAL));
5180 maxsize = zp->z_size - uio->uio_loffset;
5181 if (size > maxsize)
5182 size = maxsize;
5184 if (size < blksz || vn_has_cached_data(vp)) {
5185 ZFS_EXIT(zfsvfs);
5186 return (SET_ERROR(EINVAL));
5188 break;
5189 default:
5190 ZFS_EXIT(zfsvfs);
5191 return (SET_ERROR(EINVAL));
5194 uio->uio_extflg = UIO_XUIO;
5195 XUIO_XUZC_RW(xuio) = ioflag;
5196 ZFS_EXIT(zfsvfs);
5197 return (0);
5200 /*ARGSUSED*/
5201 static int
5202 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5204 int i;
5205 arc_buf_t *abuf;
5206 int ioflag = XUIO_XUZC_RW(xuio);
5208 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5210 i = dmu_xuio_cnt(xuio);
5211 while (i-- > 0) {
5212 abuf = dmu_xuio_arcbuf(xuio, i);
5214 * if abuf == NULL, it must be a write buffer
5215 * that has been returned in zfs_write().
5217 if (abuf)
5218 dmu_return_arcbuf(abuf);
5219 ASSERT(abuf || ioflag == UIO_WRITE);
5222 dmu_xuio_fini(xuio);
5223 return (0);
5227 * Predeclare these here so that the compiler assumes that
5228 * this is an "old style" function declaration that does
5229 * not include arguments => we won't get type mismatch errors
5230 * in the initializations that follow.
5232 static int zfs_inval();
5233 static int zfs_isdir();
5235 static int
5236 zfs_inval()
5238 return (SET_ERROR(EINVAL));
5241 static int
5242 zfs_isdir()
5244 return (SET_ERROR(EISDIR));
5248 * Directory vnode operations
5250 const struct vnodeops zfs_dvnodeops = {
5251 .vnop_name = "zfs",
5252 .vop_open = zfs_open,
5253 .vop_close = zfs_close,
5254 .vop_read = zfs_isdir,
5255 .vop_write = zfs_isdir,
5256 .vop_ioctl = zfs_ioctl,
5257 .vop_getattr = zfs_getattr,
5258 .vop_setattr = zfs_setattr,
5259 .vop_access = zfs_access,
5260 .vop_lookup = zfs_lookup,
5261 .vop_create = zfs_create,
5262 .vop_remove = zfs_remove,
5263 .vop_link = zfs_link,
5264 .vop_rename = zfs_rename,
5265 .vop_mkdir = zfs_mkdir,
5266 .vop_rmdir = zfs_rmdir,
5267 .vop_readdir = zfs_readdir,
5268 .vop_symlink = zfs_symlink,
5269 .vop_fsync = zfs_fsync,
5270 .vop_inactive = zfs_inactive,
5271 .vop_fid = zfs_fid,
5272 .vop_seek = zfs_seek,
5273 .vop_pathconf = zfs_pathconf,
5274 .vop_getsecattr = zfs_getsecattr,
5275 .vop_setsecattr = zfs_setsecattr,
5276 .vop_vnevent = fs_vnevent_support,
5280 * Regular file vnode operations
5282 const struct vnodeops zfs_fvnodeops = {
5283 .vnop_name = "zfs",
5284 .vop_open = zfs_open,
5285 .vop_close = zfs_close,
5286 .vop_read = zfs_read,
5287 .vop_write = zfs_write,
5288 .vop_ioctl = zfs_ioctl,
5289 .vop_getattr = zfs_getattr,
5290 .vop_setattr = zfs_setattr,
5291 .vop_access = zfs_access,
5292 .vop_lookup = zfs_lookup,
5293 .vop_rename = zfs_rename,
5294 .vop_fsync = zfs_fsync,
5295 .vop_inactive = zfs_inactive,
5296 .vop_fid = zfs_fid,
5297 .vop_seek = zfs_seek,
5298 .vop_frlock = zfs_frlock,
5299 .vop_space = zfs_space,
5300 .vop_getpage = zfs_getpage,
5301 .vop_putpage = zfs_putpage,
5302 .vop_map = zfs_map,
5303 .vop_addmap = zfs_addmap,
5304 .vop_delmap = zfs_delmap,
5305 .vop_pathconf = zfs_pathconf,
5306 .vop_getsecattr = zfs_getsecattr,
5307 .vop_setsecattr = zfs_setsecattr,
5308 .vop_vnevent = fs_vnevent_support,
5309 .vop_reqzcbuf = zfs_reqzcbuf,
5310 .vop_retzcbuf = zfs_retzcbuf,
5314 * Symbolic link vnode operations
5316 const struct vnodeops zfs_symvnodeops = {
5317 .vnop_name = "zfs",
5318 .vop_getattr = zfs_getattr,
5319 .vop_setattr = zfs_setattr,
5320 .vop_access = zfs_access,
5321 .vop_rename = zfs_rename,
5322 .vop_readlink = zfs_readlink,
5323 .vop_inactive = zfs_inactive,
5324 .vop_fid = zfs_fid,
5325 .vop_pathconf = zfs_pathconf,
5326 .vop_vnevent = fs_vnevent_support,
5330 * special share hidden files vnode operations
5332 const struct vnodeops zfs_sharevnodeops = {
5333 .vnop_name = "zfs",
5334 .vop_getattr = zfs_getattr,
5335 .vop_access = zfs_access,
5336 .vop_inactive = zfs_inactive,
5337 .vop_fid = zfs_fid,
5338 .vop_pathconf = zfs_pathconf,
5339 .vop_getsecattr = zfs_getsecattr,
5340 .vop_setsecattr = zfs_setsecattr,
5341 .vop_vnevent = fs_vnevent_support,
5345 * Extended attribute directory vnode operations
5347 * These ops are identical to the directory vnode
5348 * operations except for restricted operations:
5349 * fop_mkdir()
5350 * fop_symlink()
5352 * Note that there are other restrictions embedded in:
5353 * zfs_create() - restrict type to VREG
5354 * zfs_link() - no links into/out of attribute space
5355 * zfs_rename() - no moves into/out of attribute space
5357 const struct vnodeops zfs_xdvnodeops = {
5358 .vnop_name = "zfs",
5359 .vop_open = zfs_open,
5360 .vop_close = zfs_close,
5361 .vop_ioctl = zfs_ioctl,
5362 .vop_getattr = zfs_getattr,
5363 .vop_setattr = zfs_setattr,
5364 .vop_access = zfs_access,
5365 .vop_lookup = zfs_lookup,
5366 .vop_create = zfs_create,
5367 .vop_remove = zfs_remove,
5368 .vop_link = zfs_link,
5369 .vop_rename = zfs_rename,
5370 .vop_mkdir = zfs_inval,
5371 .vop_rmdir = zfs_rmdir,
5372 .vop_readdir = zfs_readdir,
5373 .vop_symlink = zfs_inval,
5374 .vop_fsync = zfs_fsync,
5375 .vop_inactive = zfs_inactive,
5376 .vop_fid = zfs_fid,
5377 .vop_seek = zfs_seek,
5378 .vop_pathconf = zfs_pathconf,
5379 .vop_getsecattr = zfs_getsecattr,
5380 .vop_setsecattr = zfs_setsecattr,
5381 .vop_vnevent = fs_vnevent_support,
5385 * Error vnode operations
5387 const struct vnodeops zfs_evnodeops = {
5388 .vnop_name = "zfs",
5389 .vop_inactive = zfs_inactive,
5390 .vop_pathconf = zfs_pathconf,