Merge commit 'dc97a43d4a70c8773a619f11b95b07a787f6f5b7' into merges
[unleashed.git] / kernel / fs / zfs / zfs_vnops.c
blobf9bed3c741d27313c434d52dac44a3c13005e203
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 it's checksum is being calculated
1104 * that no one can change the data. We need to re-check
1105 * blocksize after we get the lock in case it's changed!
1107 for (;;) {
1108 uint64_t blkoff;
1109 size = zp->z_blksz;
1110 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1111 offset -= blkoff;
1112 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1113 RL_READER);
1114 if (zp->z_blksz == size)
1115 break;
1116 offset += blkoff;
1117 zfs_range_unlock(zgd->zgd_rl);
1119 /* test for truncation needs to be done while range locked */
1120 if (lr->lr_offset >= zp->z_size)
1121 error = SET_ERROR(ENOENT);
1122 #ifdef DEBUG
1123 if (zil_fault_io) {
1124 error = SET_ERROR(EIO);
1125 zil_fault_io = 0;
1127 #endif
1128 if (error == 0)
1129 error = dmu_buf_hold(os, object, offset, zgd, &db,
1130 DMU_READ_NO_PREFETCH);
1132 if (error == 0) {
1133 blkptr_t *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;
1156 error = 0;
1161 zfs_get_done(zgd, error);
1163 return (error);
1166 /*ARGSUSED*/
1167 static int
1168 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1169 caller_context_t *ct)
1171 znode_t *zp = VTOZ(vp);
1172 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1173 int error;
1175 ZFS_ENTER(zfsvfs);
1176 ZFS_VERIFY_ZP(zp);
1178 if (flag & V_ACE_MASK)
1179 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1180 else
1181 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1183 ZFS_EXIT(zfsvfs);
1184 return (error);
1188 * If vnode is for a device return a specfs vnode instead.
1190 static int
1191 specvp_check(vnode_t **vpp, cred_t *cr)
1193 int error = 0;
1195 if (IS_DEVVP(*vpp)) {
1196 struct vnode *svp;
1198 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1199 VN_RELE(*vpp);
1200 if (svp == NULL)
1201 error = SET_ERROR(ENOSYS);
1202 *vpp = svp;
1204 return (error);
1209 * Lookup an entry in a directory, or an extended attribute directory.
1210 * If it exists, return a held vnode reference for it.
1212 * IN: dvp - vnode of directory to search.
1213 * nm - name of entry to lookup.
1214 * pnp - full pathname to lookup [UNUSED].
1215 * flags - LOOKUP_XATTR set if looking for an attribute.
1216 * rdir - root directory vnode [UNUSED].
1217 * cr - credentials of caller.
1218 * ct - caller context
1219 * direntflags - directory lookup flags
1220 * realpnp - returned pathname.
1222 * OUT: vpp - vnode of located entry, NULL if not found.
1224 * RETURN: 0 on success, error code on failure.
1226 * Timestamps:
1227 * NA
1229 /* ARGSUSED */
1230 static int
1231 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1232 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1233 int *direntflags, pathname_t *realpnp)
1235 znode_t *zdp = VTOZ(dvp);
1236 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1237 int error = 0;
1240 * Fast path lookup, however we must skip DNLC lookup
1241 * for case folding or normalizing lookups because the
1242 * DNLC code only stores the passed in name. This means
1243 * creating 'a' and removing 'A' on a case insensitive
1244 * file system would work, but DNLC still thinks 'a'
1245 * exists and won't let you create it again on the next
1246 * pass through fast path.
1248 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1250 if (dvp->v_type != VDIR) {
1251 return (SET_ERROR(ENOTDIR));
1252 } else if (zdp->z_sa_hdl == NULL) {
1253 return (SET_ERROR(EIO));
1256 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1257 error = zfs_fastaccesschk_execute(zdp, cr);
1258 if (!error) {
1259 *vpp = dvp;
1260 VN_HOLD(*vpp);
1261 return (0);
1263 return (error);
1264 } else if (!zdp->z_zfsvfs->z_norm &&
1265 (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) {
1267 vnode_t *tvp = dnlc_lookup(dvp, nm);
1269 if (tvp) {
1270 error = zfs_fastaccesschk_execute(zdp, cr);
1271 if (error) {
1272 VN_RELE(tvp);
1273 return (error);
1275 if (tvp == DNLC_NO_VNODE) {
1276 VN_RELE(tvp);
1277 return (SET_ERROR(ENOENT));
1278 } else {
1279 *vpp = tvp;
1280 return (specvp_check(vpp, cr));
1286 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1288 ZFS_ENTER(zfsvfs);
1289 ZFS_VERIFY_ZP(zdp);
1291 *vpp = NULL;
1293 if (flags & LOOKUP_XATTR) {
1295 * If the xattr property is off, refuse the lookup request.
1297 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1298 ZFS_EXIT(zfsvfs);
1299 return (SET_ERROR(EINVAL));
1303 * We don't allow recursive attributes..
1304 * Maybe someday we will.
1306 if (zdp->z_pflags & ZFS_XATTR) {
1307 ZFS_EXIT(zfsvfs);
1308 return (SET_ERROR(EINVAL));
1311 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1312 ZFS_EXIT(zfsvfs);
1313 return (error);
1317 * Do we have permission to get into attribute directory?
1320 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1321 B_FALSE, cr)) {
1322 VN_RELE(*vpp);
1323 *vpp = NULL;
1326 ZFS_EXIT(zfsvfs);
1327 return (error);
1330 if (dvp->v_type != VDIR) {
1331 ZFS_EXIT(zfsvfs);
1332 return (SET_ERROR(ENOTDIR));
1336 * Check accessibility of directory.
1339 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1340 ZFS_EXIT(zfsvfs);
1341 return (error);
1344 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1345 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1346 ZFS_EXIT(zfsvfs);
1347 return (SET_ERROR(EILSEQ));
1350 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1351 if (error == 0)
1352 error = specvp_check(vpp, cr);
1354 ZFS_EXIT(zfsvfs);
1355 return (error);
1359 * Attempt to create a new entry in a directory. If the entry
1360 * already exists, truncate the file if permissible, else return
1361 * an error. Return the vp of the created or trunc'd file.
1363 * IN: dvp - vnode of directory to put new file entry in.
1364 * name - name of new file entry.
1365 * vap - attributes of new file.
1366 * excl - flag indicating exclusive or non-exclusive mode.
1367 * mode - mode to open file with.
1368 * cr - credentials of caller.
1369 * flag - large file flag [UNUSED].
1370 * ct - caller context
1371 * vsecp - ACL to be set
1373 * OUT: vpp - vnode of created or trunc'd entry.
1375 * RETURN: 0 on success, error code on failure.
1377 * Timestamps:
1378 * dvp - ctime|mtime updated if new entry created
1379 * vp - ctime|mtime always, atime if new
1382 /* ARGSUSED */
1383 static int
1384 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1385 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1386 vsecattr_t *vsecp)
1388 znode_t *zp, *dzp = VTOZ(dvp);
1389 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1390 zilog_t *zilog;
1391 objset_t *os;
1392 zfs_dirlock_t *dl;
1393 dmu_tx_t *tx;
1394 int error;
1395 ksid_t *ksid;
1396 uid_t uid;
1397 gid_t gid = crgetgid(cr);
1398 zfs_acl_ids_t acl_ids;
1399 boolean_t fuid_dirtied;
1400 boolean_t have_acl = B_FALSE;
1401 boolean_t waited = B_FALSE;
1404 * If we have an ephemeral id, ACL, or XVATTR then
1405 * make sure file system is at proper version
1408 ksid = crgetsid(cr, KSID_OWNER);
1409 if (ksid)
1410 uid = ksid_getid(ksid);
1411 else
1412 uid = crgetuid(cr);
1414 if (zfsvfs->z_use_fuids == B_FALSE &&
1415 (vsecp || (vap->va_mask & AT_XVATTR) ||
1416 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1417 return (SET_ERROR(EINVAL));
1419 ZFS_ENTER(zfsvfs);
1420 ZFS_VERIFY_ZP(dzp);
1421 os = zfsvfs->z_os;
1422 zilog = zfsvfs->z_log;
1424 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1425 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1426 ZFS_EXIT(zfsvfs);
1427 return (SET_ERROR(EILSEQ));
1430 if (vap->va_mask & AT_XVATTR) {
1431 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1432 crgetuid(cr), cr, vap->va_type)) != 0) {
1433 ZFS_EXIT(zfsvfs);
1434 return (error);
1437 top:
1438 *vpp = NULL;
1440 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1441 vap->va_mode &= ~VSVTX;
1443 if (*name == '\0') {
1445 * Null component name refers to the directory itself.
1447 VN_HOLD(dvp);
1448 zp = dzp;
1449 dl = NULL;
1450 error = 0;
1451 } else {
1452 /* possible VN_HOLD(zp) */
1453 int zflg = 0;
1455 if (flag & FIGNORECASE)
1456 zflg |= ZCILOOK;
1458 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1459 NULL, NULL);
1460 if (error) {
1461 if (have_acl)
1462 zfs_acl_ids_free(&acl_ids);
1463 if (strcmp(name, "..") == 0)
1464 error = SET_ERROR(EISDIR);
1465 ZFS_EXIT(zfsvfs);
1466 return (error);
1470 if (zp == NULL) {
1471 uint64_t txtype;
1474 * Create a new file object and update the directory
1475 * to reference it.
1477 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1478 if (have_acl)
1479 zfs_acl_ids_free(&acl_ids);
1480 goto out;
1484 * We only support the creation of regular files in
1485 * extended attribute directories.
1488 if ((dzp->z_pflags & ZFS_XATTR) &&
1489 (vap->va_type != VREG)) {
1490 if (have_acl)
1491 zfs_acl_ids_free(&acl_ids);
1492 error = SET_ERROR(EINVAL);
1493 goto out;
1496 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1497 cr, vsecp, &acl_ids)) != 0)
1498 goto out;
1499 have_acl = B_TRUE;
1501 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1502 zfs_acl_ids_free(&acl_ids);
1503 error = SET_ERROR(EDQUOT);
1504 goto out;
1507 tx = dmu_tx_create(os);
1509 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1510 ZFS_SA_BASE_ATTR_SIZE);
1512 fuid_dirtied = zfsvfs->z_fuid_dirty;
1513 if (fuid_dirtied)
1514 zfs_fuid_txhold(zfsvfs, tx);
1515 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1516 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1517 if (!zfsvfs->z_use_sa &&
1518 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1519 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1520 0, acl_ids.z_aclp->z_acl_bytes);
1522 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1523 if (error) {
1524 zfs_dirent_unlock(dl);
1525 if (error == ERESTART) {
1526 waited = B_TRUE;
1527 dmu_tx_wait(tx);
1528 dmu_tx_abort(tx);
1529 goto top;
1531 zfs_acl_ids_free(&acl_ids);
1532 dmu_tx_abort(tx);
1533 ZFS_EXIT(zfsvfs);
1534 return (error);
1536 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1538 if (fuid_dirtied)
1539 zfs_fuid_sync(zfsvfs, tx);
1541 (void) zfs_link_create(dl, zp, tx, ZNEW);
1542 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1543 if (flag & FIGNORECASE)
1544 txtype |= TX_CI;
1545 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1546 vsecp, acl_ids.z_fuidp, vap);
1547 zfs_acl_ids_free(&acl_ids);
1548 dmu_tx_commit(tx);
1549 } else {
1550 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1552 if (have_acl)
1553 zfs_acl_ids_free(&acl_ids);
1554 have_acl = B_FALSE;
1557 * A directory entry already exists for this name.
1560 * Can't truncate an existing file if in exclusive mode.
1562 if (excl == EXCL) {
1563 error = SET_ERROR(EEXIST);
1564 goto out;
1567 * Can't open a directory for writing.
1569 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1570 error = SET_ERROR(EISDIR);
1571 goto out;
1574 * Verify requested access to file.
1576 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1577 goto out;
1580 mutex_enter(&dzp->z_lock);
1581 dzp->z_seq++;
1582 mutex_exit(&dzp->z_lock);
1585 * Truncate regular files if requested.
1587 if ((ZTOV(zp)->v_type == VREG) &&
1588 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1589 /* we can't hold any locks when calling zfs_freesp() */
1590 zfs_dirent_unlock(dl);
1591 dl = NULL;
1592 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1593 if (error == 0) {
1594 vnevent_create(ZTOV(zp), ct);
1598 out:
1600 if (dl)
1601 zfs_dirent_unlock(dl);
1603 if (error) {
1604 if (zp)
1605 VN_RELE(ZTOV(zp));
1606 } else {
1607 *vpp = ZTOV(zp);
1608 error = specvp_check(vpp, cr);
1611 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1612 zil_commit(zilog, 0);
1614 ZFS_EXIT(zfsvfs);
1615 return (error);
1619 * Remove an entry from a directory.
1621 * IN: dvp - vnode of directory to remove entry from.
1622 * name - name of entry to remove.
1623 * cr - credentials of caller.
1624 * ct - caller context
1625 * flags - case flags
1627 * RETURN: 0 on success, error code on failure.
1629 * Timestamps:
1630 * dvp - ctime|mtime
1631 * vp - ctime (if nlink > 0)
1634 uint64_t null_xattr = 0;
1636 /*ARGSUSED*/
1637 static int
1638 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1639 int flags)
1641 znode_t *zp, *dzp = VTOZ(dvp);
1642 znode_t *xzp;
1643 vnode_t *vp;
1644 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1645 zilog_t *zilog;
1646 uint64_t acl_obj, xattr_obj;
1647 uint64_t xattr_obj_unlinked = 0;
1648 uint64_t obj = 0;
1649 zfs_dirlock_t *dl;
1650 dmu_tx_t *tx;
1651 boolean_t may_delete_now, delete_now = FALSE;
1652 boolean_t unlinked, toobig = FALSE;
1653 uint64_t txtype;
1654 pathname_t *realnmp = NULL;
1655 pathname_t realnm;
1656 int error;
1657 int zflg = ZEXISTS;
1658 boolean_t waited = B_FALSE;
1660 ZFS_ENTER(zfsvfs);
1661 ZFS_VERIFY_ZP(dzp);
1662 zilog = zfsvfs->z_log;
1664 if (flags & FIGNORECASE) {
1665 zflg |= ZCILOOK;
1666 pn_alloc(&realnm);
1667 realnmp = &realnm;
1670 top:
1671 xattr_obj = 0;
1672 xzp = NULL;
1674 * Attempt to lock directory; fail if entry doesn't exist.
1676 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1677 NULL, realnmp)) {
1678 if (realnmp)
1679 pn_free(realnmp);
1680 ZFS_EXIT(zfsvfs);
1681 return (error);
1684 vp = ZTOV(zp);
1686 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1687 goto out;
1691 * Need to use rmdir for removing directories.
1693 if (vp->v_type == VDIR) {
1694 error = SET_ERROR(EPERM);
1695 goto out;
1698 vnevent_remove(vp, dvp, name, ct);
1700 if (realnmp)
1701 dnlc_remove(dvp, realnmp->pn_buf);
1702 else
1703 dnlc_remove(dvp, name);
1705 mutex_enter(&vp->v_lock);
1706 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1707 mutex_exit(&vp->v_lock);
1710 * We may delete the znode now, or we may put it in the unlinked set;
1711 * it depends on whether we're the last link, and on whether there are
1712 * other holds on the vnode. So we dmu_tx_hold() the right things to
1713 * allow for either case.
1715 obj = zp->z_id;
1716 tx = dmu_tx_create(zfsvfs->z_os);
1717 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1718 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1719 zfs_sa_upgrade_txholds(tx, zp);
1720 zfs_sa_upgrade_txholds(tx, dzp);
1721 if (may_delete_now) {
1722 toobig =
1723 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1724 /* if the file is too big, only hold_free a token amount */
1725 dmu_tx_hold_free(tx, zp->z_id, 0,
1726 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1729 /* are there any extended attributes? */
1730 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1731 &xattr_obj, sizeof (xattr_obj));
1732 if (error == 0 && xattr_obj) {
1733 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1734 ASSERT0(error);
1735 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1736 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1739 mutex_enter(&zp->z_lock);
1740 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1741 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1742 mutex_exit(&zp->z_lock);
1744 /* charge as an update -- would be nice not to charge at all */
1745 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1748 * Mark this transaction as typically resulting in a net free of space
1750 dmu_tx_mark_netfree(tx);
1752 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1753 if (error) {
1754 zfs_dirent_unlock(dl);
1755 VN_RELE(vp);
1756 if (xzp)
1757 VN_RELE(ZTOV(xzp));
1758 if (error == ERESTART) {
1759 waited = B_TRUE;
1760 dmu_tx_wait(tx);
1761 dmu_tx_abort(tx);
1762 goto top;
1764 if (realnmp)
1765 pn_free(realnmp);
1766 dmu_tx_abort(tx);
1767 ZFS_EXIT(zfsvfs);
1768 return (error);
1772 * Remove the directory entry.
1774 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1776 if (error) {
1777 dmu_tx_commit(tx);
1778 goto out;
1781 if (unlinked) {
1783 * Hold z_lock so that we can make sure that the ACL obj
1784 * hasn't changed. Could have been deleted due to
1785 * zfs_sa_upgrade().
1787 mutex_enter(&zp->z_lock);
1788 mutex_enter(&vp->v_lock);
1789 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1790 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1791 delete_now = may_delete_now && !toobig &&
1792 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1793 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1794 acl_obj;
1795 mutex_exit(&vp->v_lock);
1798 if (delete_now) {
1799 if (xattr_obj_unlinked) {
1800 ASSERT3U(xzp->z_links, ==, 2);
1801 mutex_enter(&xzp->z_lock);
1802 xzp->z_unlinked = 1;
1803 xzp->z_links = 0;
1804 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1805 &xzp->z_links, sizeof (xzp->z_links), tx);
1806 ASSERT3U(error, ==, 0);
1807 mutex_exit(&xzp->z_lock);
1808 zfs_unlinked_add(xzp, tx);
1810 if (zp->z_is_sa)
1811 error = sa_remove(zp->z_sa_hdl,
1812 SA_ZPL_XATTR(zfsvfs), tx);
1813 else
1814 error = sa_update(zp->z_sa_hdl,
1815 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1816 sizeof (uint64_t), tx);
1817 ASSERT0(error);
1819 mutex_enter(&vp->v_lock);
1820 VN_RELE_LOCKED(vp);
1821 ASSERT0(vp->v_count);
1822 mutex_exit(&vp->v_lock);
1823 mutex_exit(&zp->z_lock);
1824 zfs_znode_delete(zp, tx);
1825 } else if (unlinked) {
1826 mutex_exit(&zp->z_lock);
1827 zfs_unlinked_add(zp, tx);
1830 txtype = TX_REMOVE;
1831 if (flags & FIGNORECASE)
1832 txtype |= TX_CI;
1833 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1835 dmu_tx_commit(tx);
1836 out:
1837 if (realnmp)
1838 pn_free(realnmp);
1840 zfs_dirent_unlock(dl);
1842 if (!delete_now)
1843 VN_RELE(vp);
1844 if (xzp)
1845 VN_RELE(ZTOV(xzp));
1847 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1848 zil_commit(zilog, 0);
1850 ZFS_EXIT(zfsvfs);
1851 return (error);
1855 * Create a new directory and insert it into dvp using the name
1856 * provided. Return a pointer to the inserted directory.
1858 * IN: dvp - vnode of directory to add subdir to.
1859 * dirname - name of new directory.
1860 * vap - attributes of new directory.
1861 * cr - credentials of caller.
1862 * ct - caller context
1863 * flags - case flags
1864 * vsecp - ACL to be set
1866 * OUT: vpp - vnode of created directory.
1868 * RETURN: 0 on success, error code on failure.
1870 * Timestamps:
1871 * dvp - ctime|mtime updated
1872 * vp - ctime|mtime|atime updated
1874 /*ARGSUSED*/
1875 static int
1876 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1877 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1879 znode_t *zp, *dzp = VTOZ(dvp);
1880 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1881 zilog_t *zilog;
1882 zfs_dirlock_t *dl;
1883 uint64_t txtype;
1884 dmu_tx_t *tx;
1885 int error;
1886 int zf = ZNEW;
1887 ksid_t *ksid;
1888 uid_t uid;
1889 gid_t gid = crgetgid(cr);
1890 zfs_acl_ids_t acl_ids;
1891 boolean_t fuid_dirtied;
1892 boolean_t waited = B_FALSE;
1894 ASSERT(vap->va_type == VDIR);
1897 * If we have an ephemeral id, ACL, or XVATTR then
1898 * make sure file system is at proper version
1901 ksid = crgetsid(cr, KSID_OWNER);
1902 if (ksid)
1903 uid = ksid_getid(ksid);
1904 else
1905 uid = crgetuid(cr);
1906 if (zfsvfs->z_use_fuids == B_FALSE &&
1907 (vsecp || (vap->va_mask & AT_XVATTR) ||
1908 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1909 return (SET_ERROR(EINVAL));
1911 ZFS_ENTER(zfsvfs);
1912 ZFS_VERIFY_ZP(dzp);
1913 zilog = zfsvfs->z_log;
1915 if (dzp->z_pflags & ZFS_XATTR) {
1916 ZFS_EXIT(zfsvfs);
1917 return (SET_ERROR(EINVAL));
1920 if (zfsvfs->z_utf8 && u8_validate(dirname,
1921 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1922 ZFS_EXIT(zfsvfs);
1923 return (SET_ERROR(EILSEQ));
1925 if (flags & FIGNORECASE)
1926 zf |= ZCILOOK;
1928 if (vap->va_mask & AT_XVATTR) {
1929 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1930 crgetuid(cr), cr, vap->va_type)) != 0) {
1931 ZFS_EXIT(zfsvfs);
1932 return (error);
1936 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1937 vsecp, &acl_ids)) != 0) {
1938 ZFS_EXIT(zfsvfs);
1939 return (error);
1942 * First make sure the new directory doesn't exist.
1944 * Existence is checked first to make sure we don't return
1945 * EACCES instead of EEXIST which can cause some applications
1946 * to fail.
1948 top:
1949 *vpp = NULL;
1951 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1952 NULL, NULL)) {
1953 zfs_acl_ids_free(&acl_ids);
1954 ZFS_EXIT(zfsvfs);
1955 return (error);
1958 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1959 zfs_acl_ids_free(&acl_ids);
1960 zfs_dirent_unlock(dl);
1961 ZFS_EXIT(zfsvfs);
1962 return (error);
1965 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1966 zfs_acl_ids_free(&acl_ids);
1967 zfs_dirent_unlock(dl);
1968 ZFS_EXIT(zfsvfs);
1969 return (SET_ERROR(EDQUOT));
1973 * Add a new entry to the directory.
1975 tx = dmu_tx_create(zfsvfs->z_os);
1976 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1977 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1978 fuid_dirtied = zfsvfs->z_fuid_dirty;
1979 if (fuid_dirtied)
1980 zfs_fuid_txhold(zfsvfs, tx);
1981 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1982 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1983 acl_ids.z_aclp->z_acl_bytes);
1986 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1987 ZFS_SA_BASE_ATTR_SIZE);
1989 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1990 if (error) {
1991 zfs_dirent_unlock(dl);
1992 if (error == ERESTART) {
1993 waited = B_TRUE;
1994 dmu_tx_wait(tx);
1995 dmu_tx_abort(tx);
1996 goto top;
1998 zfs_acl_ids_free(&acl_ids);
1999 dmu_tx_abort(tx);
2000 ZFS_EXIT(zfsvfs);
2001 return (error);
2005 * Create new node.
2007 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
2009 if (fuid_dirtied)
2010 zfs_fuid_sync(zfsvfs, tx);
2013 * Now put new name in parent dir.
2015 (void) zfs_link_create(dl, zp, tx, ZNEW);
2017 *vpp = ZTOV(zp);
2019 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
2020 if (flags & FIGNORECASE)
2021 txtype |= TX_CI;
2022 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
2023 acl_ids.z_fuidp, vap);
2025 zfs_acl_ids_free(&acl_ids);
2027 dmu_tx_commit(tx);
2029 zfs_dirent_unlock(dl);
2031 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2032 zil_commit(zilog, 0);
2034 ZFS_EXIT(zfsvfs);
2035 return (0);
2039 * Remove a directory subdir entry. If the current working
2040 * directory is the same as the subdir to be removed, the
2041 * remove will fail.
2043 * IN: dvp - vnode of directory to remove from.
2044 * name - name of directory to be removed.
2045 * cwd - vnode of current working directory.
2046 * cr - credentials of caller.
2047 * ct - caller context
2048 * flags - case flags
2050 * RETURN: 0 on success, error code on failure.
2052 * Timestamps:
2053 * dvp - ctime|mtime updated
2055 /*ARGSUSED*/
2056 static int
2057 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
2058 caller_context_t *ct, int flags)
2060 znode_t *dzp = VTOZ(dvp);
2061 znode_t *zp;
2062 vnode_t *vp;
2063 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
2064 zilog_t *zilog;
2065 zfs_dirlock_t *dl;
2066 dmu_tx_t *tx;
2067 int error;
2068 int zflg = ZEXISTS;
2069 boolean_t waited = B_FALSE;
2071 ZFS_ENTER(zfsvfs);
2072 ZFS_VERIFY_ZP(dzp);
2073 zilog = zfsvfs->z_log;
2075 if (flags & FIGNORECASE)
2076 zflg |= ZCILOOK;
2077 top:
2078 zp = NULL;
2081 * Attempt to lock directory; fail if entry doesn't exist.
2083 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2084 NULL, NULL)) {
2085 ZFS_EXIT(zfsvfs);
2086 return (error);
2089 vp = ZTOV(zp);
2091 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2092 goto out;
2095 if (vp->v_type != VDIR) {
2096 error = SET_ERROR(ENOTDIR);
2097 goto out;
2100 if (vp == cwd) {
2101 error = SET_ERROR(EINVAL);
2102 goto out;
2105 vnevent_rmdir(vp, dvp, name, ct);
2108 * Grab a lock on the directory to make sure that noone is
2109 * trying to add (or lookup) entries while we are removing it.
2111 rw_enter(&zp->z_name_lock, RW_WRITER);
2114 * Grab a lock on the parent pointer to make sure we play well
2115 * with the treewalk and directory rename code.
2117 rw_enter(&zp->z_parent_lock, RW_WRITER);
2119 tx = dmu_tx_create(zfsvfs->z_os);
2120 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2121 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2122 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2123 zfs_sa_upgrade_txholds(tx, zp);
2124 zfs_sa_upgrade_txholds(tx, dzp);
2125 dmu_tx_mark_netfree(tx);
2126 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2127 if (error) {
2128 rw_exit(&zp->z_parent_lock);
2129 rw_exit(&zp->z_name_lock);
2130 zfs_dirent_unlock(dl);
2131 VN_RELE(vp);
2132 if (error == ERESTART) {
2133 waited = B_TRUE;
2134 dmu_tx_wait(tx);
2135 dmu_tx_abort(tx);
2136 goto top;
2138 dmu_tx_abort(tx);
2139 ZFS_EXIT(zfsvfs);
2140 return (error);
2143 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2145 if (error == 0) {
2146 uint64_t txtype = TX_RMDIR;
2147 if (flags & FIGNORECASE)
2148 txtype |= TX_CI;
2149 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2152 dmu_tx_commit(tx);
2154 rw_exit(&zp->z_parent_lock);
2155 rw_exit(&zp->z_name_lock);
2156 out:
2157 zfs_dirent_unlock(dl);
2159 VN_RELE(vp);
2161 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2162 zil_commit(zilog, 0);
2164 ZFS_EXIT(zfsvfs);
2165 return (error);
2169 * Read as many directory entries as will fit into the provided
2170 * buffer from the given directory cursor position (specified in
2171 * the uio structure).
2173 * IN: vp - vnode of directory to read.
2174 * uio - structure supplying read location, range info,
2175 * and return buffer.
2176 * cr - credentials of caller.
2177 * ct - caller context
2178 * flags - case flags
2180 * OUT: uio - updated offset and range, buffer filled.
2181 * eofp - set to true if end-of-file detected.
2183 * RETURN: 0 on success, error code on failure.
2185 * Timestamps:
2186 * vp - atime updated
2188 * Note that the low 4 bits of the cookie returned by zap is always zero.
2189 * This allows us to use the low range for "special" directory entries:
2190 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2191 * we use the offset 2 for the '.zfs' directory.
2193 /* ARGSUSED */
2194 static int
2195 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2196 caller_context_t *ct, int flags)
2198 znode_t *zp = VTOZ(vp);
2199 iovec_t *iovp;
2200 edirent_t *eodp;
2201 dirent64_t *odp;
2202 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2203 objset_t *os;
2204 caddr_t outbuf;
2205 size_t bufsize;
2206 zap_cursor_t zc;
2207 zap_attribute_t zap;
2208 uint_t bytes_wanted;
2209 uint64_t offset; /* must be unsigned; checks for < 1 */
2210 uint64_t parent;
2211 int local_eof;
2212 int outcount;
2213 int error;
2214 uint8_t prefetch;
2215 boolean_t check_sysattrs;
2217 ZFS_ENTER(zfsvfs);
2218 ZFS_VERIFY_ZP(zp);
2220 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2221 &parent, sizeof (parent))) != 0) {
2222 ZFS_EXIT(zfsvfs);
2223 return (error);
2227 * If we are not given an eof variable,
2228 * use a local one.
2230 if (eofp == NULL)
2231 eofp = &local_eof;
2234 * Check for valid iov_len.
2236 if (uio->uio_iov->iov_len <= 0) {
2237 ZFS_EXIT(zfsvfs);
2238 return (SET_ERROR(EINVAL));
2242 * Quit if directory has been removed (posix)
2244 if ((*eofp = zp->z_unlinked) != 0) {
2245 ZFS_EXIT(zfsvfs);
2246 return (0);
2249 error = 0;
2250 os = zfsvfs->z_os;
2251 offset = uio->uio_loffset;
2252 prefetch = zp->z_zn_prefetch;
2255 * Initialize the iterator cursor.
2257 if (offset <= 3) {
2259 * Start iteration from the beginning of the directory.
2261 zap_cursor_init(&zc, os, zp->z_id);
2262 } else {
2264 * The offset is a serialized cursor.
2266 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2270 * Get space to change directory entries into fs independent format.
2272 iovp = uio->uio_iov;
2273 bytes_wanted = iovp->iov_len;
2274 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2275 bufsize = bytes_wanted;
2276 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2277 odp = (struct dirent64 *)outbuf;
2278 } else {
2279 bufsize = bytes_wanted;
2280 outbuf = NULL;
2281 odp = (struct dirent64 *)iovp->iov_base;
2283 eodp = (struct edirent *)odp;
2286 * If this VFS supports the system attribute view interface; and
2287 * we're looking at an extended attribute directory; and we care
2288 * about normalization conflicts on this vfs; then we must check
2289 * for normalization conflicts with the sysattr name space.
2291 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2292 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2293 (flags & V_RDDIR_ENTFLAGS);
2296 * Transform to file-system independent format
2298 outcount = 0;
2299 while (outcount < bytes_wanted) {
2300 ino64_t objnum;
2301 ushort_t reclen;
2302 off64_t *next = NULL;
2305 * Special case `.', `..', and `.zfs'.
2307 if (offset == 0) {
2308 (void) strcpy(zap.za_name, ".");
2309 zap.za_normalization_conflict = 0;
2310 objnum = zp->z_id;
2311 } else if (offset == 1) {
2312 (void) strcpy(zap.za_name, "..");
2313 zap.za_normalization_conflict = 0;
2314 objnum = parent;
2315 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2316 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2317 zap.za_normalization_conflict = 0;
2318 objnum = ZFSCTL_INO_ROOT;
2319 } else {
2321 * Grab next entry.
2323 if (error = zap_cursor_retrieve(&zc, &zap)) {
2324 if ((*eofp = (error == ENOENT)) != 0)
2325 break;
2326 else
2327 goto update;
2330 if (zap.za_integer_length != 8 ||
2331 zap.za_num_integers != 1) {
2332 cmn_err(CE_WARN, "zap_readdir: bad directory "
2333 "entry, obj = %lld, offset = %lld\n",
2334 (u_longlong_t)zp->z_id,
2335 (u_longlong_t)offset);
2336 error = SET_ERROR(ENXIO);
2337 goto update;
2340 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2342 * MacOS X can extract the object type here such as:
2343 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2346 if (check_sysattrs && !zap.za_normalization_conflict) {
2347 zap.za_normalization_conflict =
2348 xattr_sysattr_casechk(zap.za_name);
2352 if (flags & V_RDDIR_ACCFILTER) {
2354 * If we have no access at all, don't include
2355 * this entry in the returned information
2357 znode_t *ezp;
2358 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2359 goto skip_entry;
2360 if (!zfs_has_access(ezp, cr)) {
2361 VN_RELE(ZTOV(ezp));
2362 goto skip_entry;
2364 VN_RELE(ZTOV(ezp));
2367 if (flags & V_RDDIR_ENTFLAGS)
2368 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2369 else
2370 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2373 * Will this entry fit in the buffer?
2375 if (outcount + reclen > bufsize) {
2377 * Did we manage to fit anything in the buffer?
2379 if (!outcount) {
2380 error = SET_ERROR(EINVAL);
2381 goto update;
2383 break;
2385 if (flags & V_RDDIR_ENTFLAGS) {
2387 * Add extended flag entry:
2389 eodp->ed_ino = objnum;
2390 eodp->ed_reclen = reclen;
2391 /* NOTE: ed_off is the offset for the *next* entry */
2392 next = &(eodp->ed_off);
2393 eodp->ed_eflags = zap.za_normalization_conflict ?
2394 ED_CASE_CONFLICT : 0;
2395 (void) strncpy(eodp->ed_name, zap.za_name,
2396 EDIRENT_NAMELEN(reclen));
2397 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2398 } else {
2400 * Add normal entry:
2402 odp->d_ino = objnum;
2403 odp->d_reclen = reclen;
2404 /* NOTE: d_off is the offset for the *next* entry */
2405 next = &(odp->d_off);
2406 (void) strncpy(odp->d_name, zap.za_name,
2407 DIRENT64_NAMELEN(reclen));
2408 odp = (dirent64_t *)((intptr_t)odp + reclen);
2410 outcount += reclen;
2412 ASSERT(outcount <= bufsize);
2414 /* Prefetch znode */
2415 if (prefetch)
2416 dmu_prefetch(os, objnum, 0, 0, 0,
2417 ZIO_PRIORITY_SYNC_READ);
2419 skip_entry:
2421 * Move to the next entry, fill in the previous offset.
2423 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2424 zap_cursor_advance(&zc);
2425 offset = zap_cursor_serialize(&zc);
2426 } else {
2427 offset += 1;
2429 if (next)
2430 *next = offset;
2432 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2434 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2435 iovp->iov_base += outcount;
2436 iovp->iov_len -= outcount;
2437 uio->uio_resid -= outcount;
2438 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2440 * Reset the pointer.
2442 offset = uio->uio_loffset;
2445 update:
2446 zap_cursor_fini(&zc);
2447 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2448 kmem_free(outbuf, bufsize);
2450 if (error == ENOENT)
2451 error = 0;
2453 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2455 uio->uio_loffset = offset;
2456 ZFS_EXIT(zfsvfs);
2457 return (error);
2460 ulong_t zfs_fsync_sync_cnt = 4;
2462 static int
2463 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2465 znode_t *zp = VTOZ(vp);
2466 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2469 * Regardless of whether this is required for standards conformance,
2470 * this is the logical behavior when fsync() is called on a file with
2471 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2472 * going to be pushed out as part of the zil_commit().
2474 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2475 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2476 (void) fop_putpage(vp, 0, (size_t)0, B_ASYNC, cr, ct);
2478 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2480 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2481 ZFS_ENTER(zfsvfs);
2482 ZFS_VERIFY_ZP(zp);
2483 zil_commit(zfsvfs->z_log, zp->z_id);
2484 ZFS_EXIT(zfsvfs);
2486 return (0);
2491 * Get the requested file attributes and place them in the provided
2492 * vattr structure.
2494 * IN: vp - vnode of file.
2495 * vap - va_mask identifies requested attributes.
2496 * If AT_XVATTR set, then optional attrs are requested
2497 * flags - ATTR_NOACLCHECK (CIFS server context)
2498 * cr - credentials of caller.
2499 * ct - caller context
2501 * OUT: vap - attribute values.
2503 * RETURN: 0 (always succeeds).
2505 /* ARGSUSED */
2506 static int
2507 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2508 caller_context_t *ct)
2510 znode_t *zp = VTOZ(vp);
2511 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2512 int error = 0;
2513 uint64_t links;
2514 uint64_t mtime[2], ctime[2];
2515 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2516 xoptattr_t *xoap = NULL;
2517 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2518 sa_bulk_attr_t bulk[2];
2519 int count = 0;
2521 ZFS_ENTER(zfsvfs);
2522 ZFS_VERIFY_ZP(zp);
2524 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2526 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2527 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2529 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2530 ZFS_EXIT(zfsvfs);
2531 return (error);
2535 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2536 * Also, if we are the owner don't bother, since owner should
2537 * always be allowed to read basic attributes of file.
2539 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2540 (vap->va_uid != crgetuid(cr))) {
2541 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2542 skipaclchk, cr)) {
2543 ZFS_EXIT(zfsvfs);
2544 return (error);
2549 * Return all attributes. It's cheaper to provide the answer
2550 * than to determine whether we were asked the question.
2553 mutex_enter(&zp->z_lock);
2554 vap->va_type = vp->v_type;
2555 vap->va_mode = zp->z_mode & MODEMASK;
2556 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2557 vap->va_nodeid = zp->z_id;
2558 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2559 links = zp->z_links + 1;
2560 else
2561 links = zp->z_links;
2562 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2563 vap->va_size = zp->z_size;
2564 vap->va_rdev = vp->v_rdev;
2565 vap->va_seq = zp->z_seq;
2568 * Add in any requested optional attributes and the create time.
2569 * Also set the corresponding bits in the returned attribute bitmap.
2571 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2572 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2573 xoap->xoa_archive =
2574 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2575 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2578 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2579 xoap->xoa_readonly =
2580 ((zp->z_pflags & ZFS_READONLY) != 0);
2581 XVA_SET_RTN(xvap, XAT_READONLY);
2584 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2585 xoap->xoa_system =
2586 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2587 XVA_SET_RTN(xvap, XAT_SYSTEM);
2590 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2591 xoap->xoa_hidden =
2592 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2593 XVA_SET_RTN(xvap, XAT_HIDDEN);
2596 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2597 xoap->xoa_nounlink =
2598 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2599 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2602 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2603 xoap->xoa_immutable =
2604 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2605 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2608 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2609 xoap->xoa_appendonly =
2610 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2611 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2614 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2615 xoap->xoa_nodump =
2616 ((zp->z_pflags & ZFS_NODUMP) != 0);
2617 XVA_SET_RTN(xvap, XAT_NODUMP);
2620 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2621 xoap->xoa_opaque =
2622 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2623 XVA_SET_RTN(xvap, XAT_OPAQUE);
2626 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2627 xoap->xoa_av_quarantined =
2628 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2629 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2632 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2633 xoap->xoa_av_modified =
2634 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2635 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2638 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2639 vp->v_type == VREG) {
2640 zfs_sa_get_scanstamp(zp, xvap);
2643 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2644 uint64_t times[2];
2646 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2647 times, sizeof (times));
2648 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2649 XVA_SET_RTN(xvap, XAT_CREATETIME);
2652 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2653 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2654 XVA_SET_RTN(xvap, XAT_REPARSE);
2656 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2657 xoap->xoa_generation = zp->z_gen;
2658 XVA_SET_RTN(xvap, XAT_GEN);
2661 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2662 xoap->xoa_offline =
2663 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2664 XVA_SET_RTN(xvap, XAT_OFFLINE);
2667 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2668 xoap->xoa_sparse =
2669 ((zp->z_pflags & ZFS_SPARSE) != 0);
2670 XVA_SET_RTN(xvap, XAT_SPARSE);
2674 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2675 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2676 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2678 mutex_exit(&zp->z_lock);
2680 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2682 if (zp->z_blksz == 0) {
2684 * Block size hasn't been set; suggest maximal I/O transfers.
2686 vap->va_blksize = zfsvfs->z_max_blksz;
2689 ZFS_EXIT(zfsvfs);
2690 return (0);
2694 * Set the file attributes to the values contained in the
2695 * vattr structure.
2697 * IN: vp - vnode of file to be modified.
2698 * vap - new attribute values.
2699 * If AT_XVATTR set, then optional attrs are being set
2700 * flags - ATTR_UTIME set if non-default time values provided.
2701 * - ATTR_NOACLCHECK (CIFS context only).
2702 * cr - credentials of caller.
2703 * ct - caller context
2705 * RETURN: 0 on success, error code on failure.
2707 * Timestamps:
2708 * vp - ctime updated, mtime updated if size changed.
2710 /* ARGSUSED */
2711 static int
2712 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2713 caller_context_t *ct)
2715 znode_t *zp = VTOZ(vp);
2716 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2717 zilog_t *zilog;
2718 dmu_tx_t *tx;
2719 vattr_t oldva;
2720 xvattr_t tmpxvattr;
2721 uint_t mask = vap->va_mask;
2722 uint_t saved_mask = 0;
2723 int trim_mask = 0;
2724 uint64_t new_mode;
2725 uint64_t new_uid, new_gid;
2726 uint64_t xattr_obj;
2727 uint64_t mtime[2], ctime[2];
2728 znode_t *attrzp;
2729 int need_policy = FALSE;
2730 int err, err2;
2731 zfs_fuid_info_t *fuidp = NULL;
2732 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2733 xoptattr_t *xoap;
2734 zfs_acl_t *aclp;
2735 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2736 boolean_t fuid_dirtied = B_FALSE;
2737 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2738 int count = 0, xattr_count = 0;
2740 if (mask == 0)
2741 return (0);
2743 if (mask & AT_NOSET)
2744 return (SET_ERROR(EINVAL));
2746 ZFS_ENTER(zfsvfs);
2747 ZFS_VERIFY_ZP(zp);
2749 zilog = zfsvfs->z_log;
2752 * Make sure that if we have ephemeral uid/gid or xvattr specified
2753 * that file system is at proper version level
2756 if (zfsvfs->z_use_fuids == B_FALSE &&
2757 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2758 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2759 (mask & AT_XVATTR))) {
2760 ZFS_EXIT(zfsvfs);
2761 return (SET_ERROR(EINVAL));
2764 if (mask & AT_SIZE && vp->v_type == VDIR) {
2765 ZFS_EXIT(zfsvfs);
2766 return (SET_ERROR(EISDIR));
2769 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2770 ZFS_EXIT(zfsvfs);
2771 return (SET_ERROR(EINVAL));
2775 * If this is an xvattr_t, then get a pointer to the structure of
2776 * optional attributes. If this is NULL, then we have a vattr_t.
2778 xoap = xva_getxoptattr(xvap);
2780 xva_init(&tmpxvattr);
2783 * Immutable files can only alter immutable bit and atime
2785 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2786 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2787 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2788 ZFS_EXIT(zfsvfs);
2789 return (SET_ERROR(EPERM));
2793 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
2797 * Verify timestamps doesn't overflow 32 bits.
2798 * ZFS can handle large timestamps, but 32bit syscalls can't
2799 * handle times greater than 2039. This check should be removed
2800 * once large timestamps are fully supported.
2802 if (mask & (AT_ATIME | AT_MTIME)) {
2803 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2804 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2805 ZFS_EXIT(zfsvfs);
2806 return (SET_ERROR(EOVERFLOW));
2810 top:
2811 attrzp = NULL;
2812 aclp = NULL;
2814 /* Can this be moved to before the top label? */
2815 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2816 ZFS_EXIT(zfsvfs);
2817 return (SET_ERROR(EROFS));
2821 * First validate permissions
2824 if (mask & AT_SIZE) {
2825 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2826 if (err) {
2827 ZFS_EXIT(zfsvfs);
2828 return (err);
2831 * XXX - Note, we are not providing any open
2832 * mode flags here (like FNDELAY), so we may
2833 * block if there are locks present... this
2834 * should be addressed in openat().
2836 /* XXX - would it be OK to generate a log record here? */
2837 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2838 if (err) {
2839 ZFS_EXIT(zfsvfs);
2840 return (err);
2843 if (vap->va_size == 0)
2844 vnevent_truncate(ZTOV(zp), ct);
2847 if (mask & (AT_ATIME|AT_MTIME) ||
2848 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2849 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2850 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2851 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2852 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2853 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2854 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2855 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2856 skipaclchk, cr);
2859 if (mask & (AT_UID|AT_GID)) {
2860 int idmask = (mask & (AT_UID|AT_GID));
2861 int take_owner;
2862 int take_group;
2865 * NOTE: even if a new mode is being set,
2866 * we may clear S_ISUID/S_ISGID bits.
2869 if (!(mask & AT_MODE))
2870 vap->va_mode = zp->z_mode;
2873 * Take ownership or chgrp to group we are a member of
2876 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2877 take_group = (mask & AT_GID) &&
2878 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2881 * If both AT_UID and AT_GID are set then take_owner and
2882 * take_group must both be set in order to allow taking
2883 * ownership.
2885 * Otherwise, send the check through secpolicy_vnode_setattr()
2889 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2890 ((idmask == AT_UID) && take_owner) ||
2891 ((idmask == AT_GID) && take_group)) {
2892 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2893 skipaclchk, cr) == 0) {
2895 * Remove setuid/setgid for non-privileged users
2897 secpolicy_setid_clear(vap, cr);
2898 trim_mask = (mask & (AT_UID|AT_GID));
2899 } else {
2900 need_policy = TRUE;
2902 } else {
2903 need_policy = TRUE;
2907 mutex_enter(&zp->z_lock);
2908 oldva.va_mode = zp->z_mode;
2909 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2910 if (mask & AT_XVATTR) {
2912 * Update xvattr mask to include only those attributes
2913 * that are actually changing.
2915 * the bits will be restored prior to actually setting
2916 * the attributes so the caller thinks they were set.
2918 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2919 if (xoap->xoa_appendonly !=
2920 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2921 need_policy = TRUE;
2922 } else {
2923 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2924 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2928 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2929 if (xoap->xoa_nounlink !=
2930 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2931 need_policy = TRUE;
2932 } else {
2933 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2934 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2938 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2939 if (xoap->xoa_immutable !=
2940 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2941 need_policy = TRUE;
2942 } else {
2943 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2944 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2948 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2949 if (xoap->xoa_nodump !=
2950 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2951 need_policy = TRUE;
2952 } else {
2953 XVA_CLR_REQ(xvap, XAT_NODUMP);
2954 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2958 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2959 if (xoap->xoa_av_modified !=
2960 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2961 need_policy = TRUE;
2962 } else {
2963 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2964 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2968 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2969 if ((vp->v_type != VREG &&
2970 xoap->xoa_av_quarantined) ||
2971 xoap->xoa_av_quarantined !=
2972 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2973 need_policy = TRUE;
2974 } else {
2975 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2976 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2980 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2981 mutex_exit(&zp->z_lock);
2982 ZFS_EXIT(zfsvfs);
2983 return (SET_ERROR(EPERM));
2986 if (need_policy == FALSE &&
2987 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2988 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2989 need_policy = TRUE;
2993 mutex_exit(&zp->z_lock);
2995 if (mask & AT_MODE) {
2996 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2997 err = secpolicy_setid_setsticky_clear(vp, vap,
2998 &oldva, cr);
2999 if (err) {
3000 ZFS_EXIT(zfsvfs);
3001 return (err);
3003 trim_mask |= AT_MODE;
3004 } else {
3005 need_policy = TRUE;
3009 if (need_policy) {
3011 * If trim_mask is set then take ownership
3012 * has been granted or write_acl is present and user
3013 * has the ability to modify mode. In that case remove
3014 * UID|GID and or MODE from mask so that
3015 * secpolicy_vnode_setattr() doesn't revoke it.
3018 if (trim_mask) {
3019 saved_mask = vap->va_mask;
3020 vap->va_mask &= ~trim_mask;
3022 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
3023 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
3024 if (err) {
3025 ZFS_EXIT(zfsvfs);
3026 return (err);
3029 if (trim_mask)
3030 vap->va_mask |= saved_mask;
3034 * secpolicy_vnode_setattr, or take ownership may have
3035 * changed va_mask
3037 mask = vap->va_mask;
3039 if ((mask & (AT_UID | AT_GID))) {
3040 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
3041 &xattr_obj, sizeof (xattr_obj));
3043 if (err == 0 && xattr_obj) {
3044 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
3045 if (err)
3046 goto out2;
3048 if (mask & AT_UID) {
3049 new_uid = zfs_fuid_create(zfsvfs,
3050 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
3051 if (new_uid != zp->z_uid &&
3052 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
3053 if (attrzp)
3054 VN_RELE(ZTOV(attrzp));
3055 err = SET_ERROR(EDQUOT);
3056 goto out2;
3060 if (mask & AT_GID) {
3061 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
3062 cr, ZFS_GROUP, &fuidp);
3063 if (new_gid != zp->z_gid &&
3064 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
3065 if (attrzp)
3066 VN_RELE(ZTOV(attrzp));
3067 err = SET_ERROR(EDQUOT);
3068 goto out2;
3072 tx = dmu_tx_create(zfsvfs->z_os);
3074 if (mask & AT_MODE) {
3075 uint64_t pmode = zp->z_mode;
3076 uint64_t acl_obj;
3077 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
3079 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
3080 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
3081 err = SET_ERROR(EPERM);
3082 goto out;
3085 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3086 goto out;
3088 mutex_enter(&zp->z_lock);
3089 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3091 * Are we upgrading ACL from old V0 format
3092 * to V1 format?
3094 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3095 zfs_znode_acl_version(zp) ==
3096 ZFS_ACL_VERSION_INITIAL) {
3097 dmu_tx_hold_free(tx, acl_obj, 0,
3098 DMU_OBJECT_END);
3099 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3100 0, aclp->z_acl_bytes);
3101 } else {
3102 dmu_tx_hold_write(tx, acl_obj, 0,
3103 aclp->z_acl_bytes);
3105 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3106 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3107 0, aclp->z_acl_bytes);
3109 mutex_exit(&zp->z_lock);
3110 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3111 } else {
3112 if ((mask & AT_XVATTR) &&
3113 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3114 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3115 else
3116 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3119 if (attrzp) {
3120 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3123 fuid_dirtied = zfsvfs->z_fuid_dirty;
3124 if (fuid_dirtied)
3125 zfs_fuid_txhold(zfsvfs, tx);
3127 zfs_sa_upgrade_txholds(tx, zp);
3129 err = dmu_tx_assign(tx, TXG_WAIT);
3130 if (err)
3131 goto out;
3133 count = 0;
3135 * Set each attribute requested.
3136 * We group settings according to the locks they need to acquire.
3138 * Note: you cannot set ctime directly, although it will be
3139 * updated as a side-effect of calling this function.
3143 if (mask & (AT_UID|AT_GID|AT_MODE))
3144 mutex_enter(&zp->z_acl_lock);
3145 mutex_enter(&zp->z_lock);
3147 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3148 &zp->z_pflags, sizeof (zp->z_pflags));
3150 if (attrzp) {
3151 if (mask & (AT_UID|AT_GID|AT_MODE))
3152 mutex_enter(&attrzp->z_acl_lock);
3153 mutex_enter(&attrzp->z_lock);
3154 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3155 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3156 sizeof (attrzp->z_pflags));
3159 if (mask & (AT_UID|AT_GID)) {
3161 if (mask & AT_UID) {
3162 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3163 &new_uid, sizeof (new_uid));
3164 zp->z_uid = new_uid;
3165 if (attrzp) {
3166 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3167 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3168 sizeof (new_uid));
3169 attrzp->z_uid = new_uid;
3173 if (mask & AT_GID) {
3174 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3175 NULL, &new_gid, sizeof (new_gid));
3176 zp->z_gid = new_gid;
3177 if (attrzp) {
3178 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3179 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3180 sizeof (new_gid));
3181 attrzp->z_gid = new_gid;
3184 if (!(mask & AT_MODE)) {
3185 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3186 NULL, &new_mode, sizeof (new_mode));
3187 new_mode = zp->z_mode;
3189 err = zfs_acl_chown_setattr(zp);
3190 ASSERT(err == 0);
3191 if (attrzp) {
3192 err = zfs_acl_chown_setattr(attrzp);
3193 ASSERT(err == 0);
3197 if (mask & AT_MODE) {
3198 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3199 &new_mode, sizeof (new_mode));
3200 zp->z_mode = new_mode;
3201 ASSERT3U((uintptr_t)aclp, !=, (uintptr_t)NULL);
3202 err = zfs_aclset_common(zp, aclp, cr, tx);
3203 ASSERT0(err);
3204 if (zp->z_acl_cached)
3205 zfs_acl_free(zp->z_acl_cached);
3206 zp->z_acl_cached = aclp;
3207 aclp = NULL;
3211 if (mask & AT_ATIME) {
3212 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3213 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3214 &zp->z_atime, sizeof (zp->z_atime));
3217 if (mask & AT_MTIME) {
3218 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3219 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3220 mtime, sizeof (mtime));
3223 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3224 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3225 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3226 NULL, mtime, sizeof (mtime));
3227 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3228 &ctime, sizeof (ctime));
3229 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3230 B_TRUE);
3231 } else if (mask != 0) {
3232 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3233 &ctime, sizeof (ctime));
3234 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3235 B_TRUE);
3236 if (attrzp) {
3237 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3238 SA_ZPL_CTIME(zfsvfs), NULL,
3239 &ctime, sizeof (ctime));
3240 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3241 mtime, ctime, B_TRUE);
3245 * Do this after setting timestamps to prevent timestamp
3246 * update from toggling bit
3249 if (xoap && (mask & AT_XVATTR)) {
3252 * restore trimmed off masks
3253 * so that return masks can be set for caller.
3256 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3257 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3259 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3260 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3262 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3263 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3265 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3266 XVA_SET_REQ(xvap, XAT_NODUMP);
3268 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3269 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3271 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3272 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3275 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3276 ASSERT(vp->v_type == VREG);
3278 zfs_xvattr_set(zp, xvap, tx);
3281 if (fuid_dirtied)
3282 zfs_fuid_sync(zfsvfs, tx);
3284 if (mask != 0)
3285 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3287 mutex_exit(&zp->z_lock);
3288 if (mask & (AT_UID|AT_GID|AT_MODE))
3289 mutex_exit(&zp->z_acl_lock);
3291 if (attrzp) {
3292 if (mask & (AT_UID|AT_GID|AT_MODE))
3293 mutex_exit(&attrzp->z_acl_lock);
3294 mutex_exit(&attrzp->z_lock);
3296 out:
3297 if (err == 0 && attrzp) {
3298 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3299 xattr_count, tx);
3300 ASSERT(err2 == 0);
3303 if (attrzp)
3304 VN_RELE(ZTOV(attrzp));
3306 if (aclp)
3307 zfs_acl_free(aclp);
3309 if (fuidp) {
3310 zfs_fuid_info_free(fuidp);
3311 fuidp = NULL;
3314 if (err) {
3315 dmu_tx_abort(tx);
3316 if (err == ERESTART)
3317 goto top;
3318 } else {
3319 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3320 dmu_tx_commit(tx);
3323 out2:
3324 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3325 zil_commit(zilog, 0);
3327 ZFS_EXIT(zfsvfs);
3328 return (err);
3331 typedef struct zfs_zlock {
3332 krwlock_t *zl_rwlock; /* lock we acquired */
3333 znode_t *zl_znode; /* znode we held */
3334 struct zfs_zlock *zl_next; /* next in list */
3335 } zfs_zlock_t;
3338 * Drop locks and release vnodes that were held by zfs_rename_lock().
3340 static void
3341 zfs_rename_unlock(zfs_zlock_t **zlpp)
3343 zfs_zlock_t *zl;
3345 while ((zl = *zlpp) != NULL) {
3346 if (zl->zl_znode != NULL)
3347 VN_RELE(ZTOV(zl->zl_znode));
3348 rw_exit(zl->zl_rwlock);
3349 *zlpp = zl->zl_next;
3350 kmem_free(zl, sizeof (*zl));
3355 * Search back through the directory tree, using the ".." entries.
3356 * Lock each directory in the chain to prevent concurrent renames.
3357 * Fail any attempt to move a directory into one of its own descendants.
3358 * XXX - z_parent_lock can overlap with map or grow locks
3360 static int
3361 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3363 zfs_zlock_t *zl;
3364 znode_t *zp = tdzp;
3365 uint64_t rootid = zp->z_zfsvfs->z_root;
3366 uint64_t oidp = zp->z_id;
3367 krwlock_t *rwlp = &szp->z_parent_lock;
3368 krw_t rw = RW_WRITER;
3371 * First pass write-locks szp and compares to zp->z_id.
3372 * Later passes read-lock zp and compare to zp->z_parent.
3374 do {
3375 if (!rw_tryenter(rwlp, rw)) {
3377 * Another thread is renaming in this path.
3378 * Note that if we are a WRITER, we don't have any
3379 * parent_locks held yet.
3381 if (rw == RW_READER && zp->z_id > szp->z_id) {
3383 * Drop our locks and restart
3385 zfs_rename_unlock(&zl);
3386 *zlpp = NULL;
3387 zp = tdzp;
3388 oidp = zp->z_id;
3389 rwlp = &szp->z_parent_lock;
3390 rw = RW_WRITER;
3391 continue;
3392 } else {
3394 * Wait for other thread to drop its locks
3396 rw_enter(rwlp, rw);
3400 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3401 zl->zl_rwlock = rwlp;
3402 zl->zl_znode = NULL;
3403 zl->zl_next = *zlpp;
3404 *zlpp = zl;
3406 if (oidp == szp->z_id) /* We're a descendant of szp */
3407 return (SET_ERROR(EINVAL));
3409 if (oidp == rootid) /* We've hit the top */
3410 return (0);
3412 if (rw == RW_READER) { /* i.e. not the first pass */
3413 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3414 if (error)
3415 return (error);
3416 zl->zl_znode = zp;
3418 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3419 &oidp, sizeof (oidp));
3420 rwlp = &zp->z_parent_lock;
3421 rw = RW_READER;
3423 } while (zp->z_id != sdzp->z_id);
3425 return (0);
3429 * Move an entry from the provided source directory to the target
3430 * directory. Change the entry name as indicated.
3432 * IN: sdvp - Source directory containing the "old entry".
3433 * snm - Old entry name.
3434 * tdvp - Target directory to contain the "new entry".
3435 * tnm - New entry name.
3436 * cr - credentials of caller.
3437 * ct - caller context
3438 * flags - case flags
3440 * RETURN: 0 on success, error code on failure.
3442 * Timestamps:
3443 * sdvp,tdvp - ctime|mtime updated
3445 /*ARGSUSED*/
3446 static int
3447 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3448 caller_context_t *ct, int flags)
3450 znode_t *tdzp, *szp, *tzp;
3451 znode_t *sdzp = VTOZ(sdvp);
3452 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3453 zilog_t *zilog;
3454 vnode_t *realvp;
3455 zfs_dirlock_t *sdl, *tdl;
3456 dmu_tx_t *tx;
3457 zfs_zlock_t *zl;
3458 int cmp, serr, terr;
3459 int error = 0, rm_err = 0;
3460 int zflg = 0;
3461 boolean_t waited = B_FALSE;
3463 ZFS_ENTER(zfsvfs);
3464 ZFS_VERIFY_ZP(sdzp);
3465 zilog = zfsvfs->z_log;
3468 * Make sure we have the real vp for the target directory.
3470 if (fop_realvp(tdvp, &realvp, ct) == 0)
3471 tdvp = realvp;
3473 tdzp = VTOZ(tdvp);
3474 ZFS_VERIFY_ZP(tdzp);
3477 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3478 * ctldir appear to have the same v_vfsp.
3480 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3481 ZFS_EXIT(zfsvfs);
3482 return (SET_ERROR(EXDEV));
3485 if (zfsvfs->z_utf8 && u8_validate(tnm,
3486 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3487 ZFS_EXIT(zfsvfs);
3488 return (SET_ERROR(EILSEQ));
3491 if (flags & FIGNORECASE)
3492 zflg |= ZCILOOK;
3494 top:
3495 szp = NULL;
3496 tzp = NULL;
3497 zl = NULL;
3500 * This is to prevent the creation of links into attribute space
3501 * by renaming a linked file into/outof an attribute directory.
3502 * See the comment in zfs_link() for why this is considered bad.
3504 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3505 ZFS_EXIT(zfsvfs);
3506 return (SET_ERROR(EINVAL));
3510 * Lock source and target directory entries. To prevent deadlock,
3511 * a lock ordering must be defined. We lock the directory with
3512 * the smallest object id first, or if it's a tie, the one with
3513 * the lexically first name.
3515 if (sdzp->z_id < tdzp->z_id) {
3516 cmp = -1;
3517 } else if (sdzp->z_id > tdzp->z_id) {
3518 cmp = 1;
3519 } else {
3521 * First compare the two name arguments without
3522 * considering any case folding.
3524 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3526 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3527 ASSERT(error == 0 || !zfsvfs->z_utf8);
3528 if (cmp == 0) {
3530 * POSIX: "If the old argument and the new argument
3531 * both refer to links to the same existing file,
3532 * the rename() function shall return successfully
3533 * and perform no other action."
3535 ZFS_EXIT(zfsvfs);
3536 return (0);
3539 * If the file system is case-folding, then we may
3540 * have some more checking to do. A case-folding file
3541 * system is either supporting mixed case sensitivity
3542 * access or is completely case-insensitive. Note
3543 * that the file system is always case preserving.
3545 * In mixed sensitivity mode case sensitive behavior
3546 * is the default. FIGNORECASE must be used to
3547 * explicitly request case insensitive behavior.
3549 * If the source and target names provided differ only
3550 * by case (e.g., a request to rename 'tim' to 'Tim'),
3551 * we will treat this as a special case in the
3552 * case-insensitive mode: as long as the source name
3553 * is an exact match, we will allow this to proceed as
3554 * a name-change request.
3556 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3557 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3558 flags & FIGNORECASE)) &&
3559 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3560 &error) == 0) {
3562 * case preserving rename request, require exact
3563 * name matches
3565 zflg |= ZCIEXACT;
3566 zflg &= ~ZCILOOK;
3571 * If the source and destination directories are the same, we should
3572 * grab the z_name_lock of that directory only once.
3574 if (sdzp == tdzp) {
3575 zflg |= ZHAVELOCK;
3576 rw_enter(&sdzp->z_name_lock, RW_READER);
3579 if (cmp < 0) {
3580 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3581 ZEXISTS | zflg, NULL, NULL);
3582 terr = zfs_dirent_lock(&tdl,
3583 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3584 } else {
3585 terr = zfs_dirent_lock(&tdl,
3586 tdzp, tnm, &tzp, zflg, NULL, NULL);
3587 serr = zfs_dirent_lock(&sdl,
3588 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3589 NULL, NULL);
3592 if (serr) {
3594 * Source entry invalid or not there.
3596 if (!terr) {
3597 zfs_dirent_unlock(tdl);
3598 if (tzp)
3599 VN_RELE(ZTOV(tzp));
3602 if (sdzp == tdzp)
3603 rw_exit(&sdzp->z_name_lock);
3605 if (strcmp(snm, "..") == 0)
3606 serr = SET_ERROR(EINVAL);
3607 ZFS_EXIT(zfsvfs);
3608 return (serr);
3610 if (terr) {
3611 zfs_dirent_unlock(sdl);
3612 VN_RELE(ZTOV(szp));
3614 if (sdzp == tdzp)
3615 rw_exit(&sdzp->z_name_lock);
3617 if (strcmp(tnm, "..") == 0)
3618 terr = SET_ERROR(EINVAL);
3619 ZFS_EXIT(zfsvfs);
3620 return (terr);
3624 * Must have write access at the source to remove the old entry
3625 * and write access at the target to create the new entry.
3626 * Note that if target and source are the same, this can be
3627 * done in a single check.
3630 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3631 goto out;
3633 if (ZTOV(szp)->v_type == VDIR) {
3635 * Check to make sure rename is valid.
3636 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3638 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3639 goto out;
3643 * Does target exist?
3645 if (tzp) {
3647 * Source and target must be the same type.
3649 if (ZTOV(szp)->v_type == VDIR) {
3650 if (ZTOV(tzp)->v_type != VDIR) {
3651 error = SET_ERROR(ENOTDIR);
3652 goto out;
3654 } else {
3655 if (ZTOV(tzp)->v_type == VDIR) {
3656 error = SET_ERROR(EISDIR);
3657 goto out;
3661 * POSIX dictates that when the source and target
3662 * entries refer to the same file object, rename
3663 * must do nothing and exit without error.
3665 if (szp->z_id == tzp->z_id) {
3666 error = 0;
3667 goto out;
3671 vnevent_pre_rename_src(ZTOV(szp), sdvp, snm, ct);
3672 if (tzp)
3673 vnevent_pre_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3676 * notify the target directory if it is not the same
3677 * as source directory.
3679 if (tdvp != sdvp) {
3680 vnevent_pre_rename_dest_dir(tdvp, ZTOV(szp), tnm, ct);
3683 tx = dmu_tx_create(zfsvfs->z_os);
3684 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3685 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3686 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3687 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3688 if (sdzp != tdzp) {
3689 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3690 zfs_sa_upgrade_txholds(tx, tdzp);
3692 if (tzp) {
3693 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3694 zfs_sa_upgrade_txholds(tx, tzp);
3697 zfs_sa_upgrade_txholds(tx, szp);
3698 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3699 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3700 if (error) {
3701 if (zl != NULL)
3702 zfs_rename_unlock(&zl);
3703 zfs_dirent_unlock(sdl);
3704 zfs_dirent_unlock(tdl);
3706 if (sdzp == tdzp)
3707 rw_exit(&sdzp->z_name_lock);
3709 VN_RELE(ZTOV(szp));
3710 if (tzp)
3711 VN_RELE(ZTOV(tzp));
3712 if (error == ERESTART) {
3713 waited = B_TRUE;
3714 dmu_tx_wait(tx);
3715 dmu_tx_abort(tx);
3716 goto top;
3718 dmu_tx_abort(tx);
3719 ZFS_EXIT(zfsvfs);
3720 return (error);
3723 if (tzp) /* Attempt to remove the existing target */
3724 error = rm_err = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3726 if (error == 0) {
3727 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3728 if (error == 0) {
3729 szp->z_pflags |= ZFS_AV_MODIFIED;
3731 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3732 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3733 ASSERT0(error);
3735 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3736 if (error == 0) {
3737 zfs_log_rename(zilog, tx, TX_RENAME |
3738 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3739 sdl->dl_name, tdzp, tdl->dl_name, szp);
3742 * Update path information for the target vnode
3744 vn_renamepath(tdvp, ZTOV(szp), tnm,
3745 strlen(tnm));
3746 } else {
3748 * At this point, we have successfully created
3749 * the target name, but have failed to remove
3750 * the source name. Since the create was done
3751 * with the ZRENAMING flag, there are
3752 * complications; for one, the link count is
3753 * wrong. The easiest way to deal with this
3754 * is to remove the newly created target, and
3755 * return the original error. This must
3756 * succeed; fortunately, it is very unlikely to
3757 * fail, since we just created it.
3759 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3760 ZRENAMING, NULL), ==, 0);
3765 dmu_tx_commit(tx);
3767 if (tzp && rm_err == 0)
3768 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3770 if (error == 0) {
3771 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3772 /* notify the target dir if it is not the same as source dir */
3773 if (tdvp != sdvp)
3774 vnevent_rename_dest_dir(tdvp, ct);
3776 out:
3777 if (zl != NULL)
3778 zfs_rename_unlock(&zl);
3780 zfs_dirent_unlock(sdl);
3781 zfs_dirent_unlock(tdl);
3783 if (sdzp == tdzp)
3784 rw_exit(&sdzp->z_name_lock);
3787 VN_RELE(ZTOV(szp));
3788 if (tzp)
3789 VN_RELE(ZTOV(tzp));
3791 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3792 zil_commit(zilog, 0);
3794 ZFS_EXIT(zfsvfs);
3795 return (error);
3799 * Insert the indicated symbolic reference entry into the directory.
3801 * IN: dvp - Directory to contain new symbolic link.
3802 * link - Name for new symlink entry.
3803 * vap - Attributes of new entry.
3804 * cr - credentials of caller.
3805 * ct - caller context
3806 * flags - case flags
3808 * RETURN: 0 on success, error code on failure.
3810 * Timestamps:
3811 * dvp - ctime|mtime updated
3813 /*ARGSUSED*/
3814 static int
3815 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3816 caller_context_t *ct, int flags)
3818 znode_t *zp, *dzp = VTOZ(dvp);
3819 zfs_dirlock_t *dl;
3820 dmu_tx_t *tx;
3821 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3822 zilog_t *zilog;
3823 uint64_t len = strlen(link);
3824 int error;
3825 int zflg = ZNEW;
3826 zfs_acl_ids_t acl_ids;
3827 boolean_t fuid_dirtied;
3828 uint64_t txtype = TX_SYMLINK;
3829 boolean_t waited = B_FALSE;
3831 ASSERT(vap->va_type == VLNK);
3833 ZFS_ENTER(zfsvfs);
3834 ZFS_VERIFY_ZP(dzp);
3835 zilog = zfsvfs->z_log;
3837 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3838 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3839 ZFS_EXIT(zfsvfs);
3840 return (SET_ERROR(EILSEQ));
3842 if (flags & FIGNORECASE)
3843 zflg |= ZCILOOK;
3845 if (len > MAXPATHLEN) {
3846 ZFS_EXIT(zfsvfs);
3847 return (SET_ERROR(ENAMETOOLONG));
3850 if ((error = zfs_acl_ids_create(dzp, 0,
3851 vap, cr, NULL, &acl_ids)) != 0) {
3852 ZFS_EXIT(zfsvfs);
3853 return (error);
3855 top:
3857 * Attempt to lock directory; fail if entry already exists.
3859 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3860 if (error) {
3861 zfs_acl_ids_free(&acl_ids);
3862 ZFS_EXIT(zfsvfs);
3863 return (error);
3866 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3867 zfs_acl_ids_free(&acl_ids);
3868 zfs_dirent_unlock(dl);
3869 ZFS_EXIT(zfsvfs);
3870 return (error);
3873 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3874 zfs_acl_ids_free(&acl_ids);
3875 zfs_dirent_unlock(dl);
3876 ZFS_EXIT(zfsvfs);
3877 return (SET_ERROR(EDQUOT));
3879 tx = dmu_tx_create(zfsvfs->z_os);
3880 fuid_dirtied = zfsvfs->z_fuid_dirty;
3881 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3882 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3883 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3884 ZFS_SA_BASE_ATTR_SIZE + len);
3885 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3886 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3887 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3888 acl_ids.z_aclp->z_acl_bytes);
3890 if (fuid_dirtied)
3891 zfs_fuid_txhold(zfsvfs, tx);
3892 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3893 if (error) {
3894 zfs_dirent_unlock(dl);
3895 if (error == ERESTART) {
3896 waited = B_TRUE;
3897 dmu_tx_wait(tx);
3898 dmu_tx_abort(tx);
3899 goto top;
3901 zfs_acl_ids_free(&acl_ids);
3902 dmu_tx_abort(tx);
3903 ZFS_EXIT(zfsvfs);
3904 return (error);
3908 * Create a new object for the symlink.
3909 * for version 4 ZPL datsets the symlink will be an SA attribute
3911 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3913 if (fuid_dirtied)
3914 zfs_fuid_sync(zfsvfs, tx);
3916 mutex_enter(&zp->z_lock);
3917 if (zp->z_is_sa)
3918 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3919 link, len, tx);
3920 else
3921 zfs_sa_symlink(zp, link, len, tx);
3922 mutex_exit(&zp->z_lock);
3924 zp->z_size = len;
3925 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3926 &zp->z_size, sizeof (zp->z_size), tx);
3928 * Insert the new object into the directory.
3930 (void) zfs_link_create(dl, zp, tx, ZNEW);
3932 if (flags & FIGNORECASE)
3933 txtype |= TX_CI;
3934 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3936 zfs_acl_ids_free(&acl_ids);
3938 dmu_tx_commit(tx);
3940 zfs_dirent_unlock(dl);
3942 VN_RELE(ZTOV(zp));
3944 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3945 zil_commit(zilog, 0);
3947 ZFS_EXIT(zfsvfs);
3948 return (error);
3952 * Return, in the buffer contained in the provided uio structure,
3953 * the symbolic path referred to by vp.
3955 * IN: vp - vnode of symbolic link.
3956 * uio - structure to contain the link path.
3957 * cr - credentials of caller.
3958 * ct - caller context
3960 * OUT: uio - structure containing the link path.
3962 * RETURN: 0 on success, error code on failure.
3964 * Timestamps:
3965 * vp - atime updated
3967 /* ARGSUSED */
3968 static int
3969 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3971 znode_t *zp = VTOZ(vp);
3972 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3973 int error;
3975 ZFS_ENTER(zfsvfs);
3976 ZFS_VERIFY_ZP(zp);
3978 mutex_enter(&zp->z_lock);
3979 if (zp->z_is_sa)
3980 error = sa_lookup_uio(zp->z_sa_hdl,
3981 SA_ZPL_SYMLINK(zfsvfs), uio);
3982 else
3983 error = zfs_sa_readlink(zp, uio);
3984 mutex_exit(&zp->z_lock);
3986 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3988 ZFS_EXIT(zfsvfs);
3989 return (error);
3993 * Insert a new entry into directory tdvp referencing svp.
3995 * IN: tdvp - Directory to contain new entry.
3996 * svp - vnode of new entry.
3997 * name - name of new entry.
3998 * cr - credentials of caller.
3999 * ct - caller context
4001 * RETURN: 0 on success, error code on failure.
4003 * Timestamps:
4004 * tdvp - ctime|mtime updated
4005 * svp - ctime updated
4007 /* ARGSUSED */
4008 static int
4009 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
4010 caller_context_t *ct, int flags)
4012 znode_t *dzp = VTOZ(tdvp);
4013 znode_t *tzp, *szp;
4014 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
4015 zilog_t *zilog;
4016 zfs_dirlock_t *dl;
4017 dmu_tx_t *tx;
4018 vnode_t *realvp;
4019 int error;
4020 int zf = ZNEW;
4021 uint64_t parent;
4022 uid_t owner;
4023 boolean_t waited = B_FALSE;
4025 ASSERT(tdvp->v_type == VDIR);
4027 ZFS_ENTER(zfsvfs);
4028 ZFS_VERIFY_ZP(dzp);
4029 zilog = zfsvfs->z_log;
4031 if (fop_realvp(svp, &realvp, ct) == 0)
4032 svp = realvp;
4035 * POSIX dictates that we return EPERM here.
4036 * Better choices include ENOTSUP or EISDIR.
4038 if (svp->v_type == VDIR) {
4039 ZFS_EXIT(zfsvfs);
4040 return (SET_ERROR(EPERM));
4043 szp = VTOZ(svp);
4044 ZFS_VERIFY_ZP(szp);
4047 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
4048 * ctldir appear to have the same v_vfsp.
4050 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
4051 ZFS_EXIT(zfsvfs);
4052 return (SET_ERROR(EXDEV));
4055 /* Prevent links to .zfs/shares files */
4057 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
4058 &parent, sizeof (uint64_t))) != 0) {
4059 ZFS_EXIT(zfsvfs);
4060 return (error);
4062 if (parent == zfsvfs->z_shares_dir) {
4063 ZFS_EXIT(zfsvfs);
4064 return (SET_ERROR(EPERM));
4067 if (zfsvfs->z_utf8 && u8_validate(name,
4068 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
4069 ZFS_EXIT(zfsvfs);
4070 return (SET_ERROR(EILSEQ));
4072 if (flags & FIGNORECASE)
4073 zf |= ZCILOOK;
4076 * We do not support links between attributes and non-attributes
4077 * because of the potential security risk of creating links
4078 * into "normal" file space in order to circumvent restrictions
4079 * imposed in attribute space.
4081 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
4082 ZFS_EXIT(zfsvfs);
4083 return (SET_ERROR(EINVAL));
4087 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
4088 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
4089 ZFS_EXIT(zfsvfs);
4090 return (SET_ERROR(EPERM));
4093 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4094 ZFS_EXIT(zfsvfs);
4095 return (error);
4098 top:
4100 * Attempt to lock directory; fail if entry already exists.
4102 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4103 if (error) {
4104 ZFS_EXIT(zfsvfs);
4105 return (error);
4108 tx = dmu_tx_create(zfsvfs->z_os);
4109 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4110 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4111 zfs_sa_upgrade_txholds(tx, szp);
4112 zfs_sa_upgrade_txholds(tx, dzp);
4113 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4114 if (error) {
4115 zfs_dirent_unlock(dl);
4116 if (error == ERESTART) {
4117 waited = B_TRUE;
4118 dmu_tx_wait(tx);
4119 dmu_tx_abort(tx);
4120 goto top;
4122 dmu_tx_abort(tx);
4123 ZFS_EXIT(zfsvfs);
4124 return (error);
4127 error = zfs_link_create(dl, szp, tx, 0);
4129 if (error == 0) {
4130 uint64_t txtype = TX_LINK;
4131 if (flags & FIGNORECASE)
4132 txtype |= TX_CI;
4133 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4136 dmu_tx_commit(tx);
4138 zfs_dirent_unlock(dl);
4140 if (error == 0) {
4141 vnevent_link(svp, ct);
4144 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4145 zil_commit(zilog, 0);
4147 ZFS_EXIT(zfsvfs);
4148 return (error);
4152 * zfs_null_putapage() is used when the file system has been force
4153 * unmounted. It just drops the pages.
4155 /* ARGSUSED */
4156 static int
4157 zfs_null_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4158 size_t *lenp, int flags, cred_t *cr)
4160 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4161 return (0);
4165 * Push a page out to disk, klustering if possible.
4167 * IN: vp - file to push page to.
4168 * pp - page to push.
4169 * flags - additional flags.
4170 * cr - credentials of caller.
4172 * OUT: offp - start of range pushed.
4173 * lenp - len of range pushed.
4175 * RETURN: 0 on success, error code on failure.
4177 * NOTE: callers must have locked the page to be pushed. On
4178 * exit, the page (and all other pages in the kluster) must be
4179 * unlocked.
4181 /* ARGSUSED */
4182 static int
4183 zfs_putapage(vnode_t *vp, page_t *pp, uoff_t *offp,
4184 size_t *lenp, int flags, cred_t *cr)
4186 znode_t *zp = VTOZ(vp);
4187 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4188 dmu_tx_t *tx;
4189 uoff_t off, koff;
4190 size_t len, klen;
4191 int err;
4193 off = pp->p_offset;
4194 len = PAGESIZE;
4196 * If our blocksize is bigger than the page size, try to kluster
4197 * multiple pages so that we write a full block (thus avoiding
4198 * a read-modify-write).
4200 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4201 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4202 koff = ISP2(klen) ? P2ALIGN(off, (uoff_t)klen) : 0;
4203 ASSERT(koff <= zp->z_size);
4204 if (koff + klen > zp->z_size)
4205 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4206 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4208 ASSERT3U(btop(len), ==, btopr(len));
4211 * Can't push pages past end-of-file.
4213 if (off >= zp->z_size) {
4214 /* ignore all pages */
4215 err = 0;
4216 goto out;
4217 } else if (off + len > zp->z_size) {
4218 int npages = btopr(zp->z_size - off);
4219 page_t *trunc;
4221 page_list_break(&pp, &trunc, npages);
4222 /* ignore pages past end of file */
4223 if (trunc)
4224 pvn_write_done(trunc, flags);
4225 len = zp->z_size - off;
4228 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4229 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4230 err = SET_ERROR(EDQUOT);
4231 goto out;
4233 tx = dmu_tx_create(zfsvfs->z_os);
4234 dmu_tx_hold_write(tx, zp->z_id, off, len);
4236 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4237 zfs_sa_upgrade_txholds(tx, zp);
4238 err = dmu_tx_assign(tx, TXG_WAIT);
4239 if (err != 0) {
4240 dmu_tx_abort(tx);
4241 goto out;
4244 if (zp->z_blksz <= PAGESIZE) {
4245 caddr_t va = zfs_map_page(pp, S_READ);
4246 ASSERT3U(len, <=, PAGESIZE);
4247 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4248 zfs_unmap_page(pp, va);
4249 } else {
4250 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4253 if (err == 0) {
4254 uint64_t mtime[2], ctime[2];
4255 sa_bulk_attr_t bulk[3];
4256 int count = 0;
4258 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4259 &mtime, 16);
4260 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4261 &ctime, 16);
4262 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4263 &zp->z_pflags, 8);
4264 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4265 B_TRUE);
4266 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
4267 ASSERT0(err);
4268 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4270 dmu_tx_commit(tx);
4272 out:
4273 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4274 if (offp)
4275 *offp = off;
4276 if (lenp)
4277 *lenp = len;
4279 return (err);
4283 * Copy the portion of the file indicated from pages into the file.
4284 * The pages are stored in a page list attached to the files vnode.
4286 * IN: vp - vnode of file to push page data to.
4287 * off - position in file to put data.
4288 * len - amount of data to write.
4289 * flags - flags to control the operation.
4290 * cr - credentials of caller.
4291 * ct - caller context.
4293 * RETURN: 0 on success, error code on failure.
4295 * Timestamps:
4296 * vp - ctime|mtime updated
4298 /*ARGSUSED*/
4299 static int
4300 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4301 caller_context_t *ct)
4303 znode_t *zp = VTOZ(vp);
4304 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4305 page_t *pp;
4306 size_t io_len;
4307 uoff_t io_off;
4308 uint_t blksz;
4309 rl_t *rl;
4310 int error = 0;
4312 ZFS_ENTER(zfsvfs);
4313 ZFS_VERIFY_ZP(zp);
4316 * There's nothing to do if no data is cached.
4318 if (!vn_has_cached_data(vp)) {
4319 ZFS_EXIT(zfsvfs);
4320 return (0);
4324 * Align this request to the file block size in case we kluster.
4325 * XXX - this can result in pretty aggresive locking, which can
4326 * impact simultanious read/write access. One option might be
4327 * to break up long requests (len == 0) into block-by-block
4328 * operations to get narrower locking.
4330 blksz = zp->z_blksz;
4331 if (ISP2(blksz))
4332 io_off = P2ALIGN_TYPED(off, blksz, uoff_t);
4333 else
4334 io_off = 0;
4335 if (len > 0 && ISP2(blksz))
4336 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4337 else
4338 io_len = 0;
4340 if (io_len == 0) {
4342 * Search the entire vp list for pages >= io_off.
4344 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4345 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4346 goto out;
4348 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4350 if (off > zp->z_size) {
4351 /* past end of file */
4352 zfs_range_unlock(rl);
4353 ZFS_EXIT(zfsvfs);
4354 return (0);
4357 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4359 for (off = io_off; io_off < off + len; io_off += io_len) {
4360 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4361 pp = page_lookup(&vp->v_object, io_off,
4362 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4363 } else {
4364 pp = page_lookup_nowait(&vp->v_object, io_off,
4365 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4368 if (pp != NULL && pvn_getdirty(pp, flags)) {
4369 int err;
4372 * Found a dirty page to push
4374 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4375 if (err)
4376 error = err;
4377 } else {
4378 io_len = PAGESIZE;
4381 out:
4382 zfs_range_unlock(rl);
4383 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4384 zil_commit(zfsvfs->z_log, zp->z_id);
4385 ZFS_EXIT(zfsvfs);
4386 return (error);
4389 /*ARGSUSED*/
4390 void
4391 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4393 znode_t *zp = VTOZ(vp);
4394 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4395 int error;
4397 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4398 if (zp->z_sa_hdl == NULL) {
4400 * The fs has been unmounted, or we did a
4401 * suspend/resume and this file no longer exists.
4403 if (vn_has_cached_data(vp)) {
4404 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4405 B_INVAL, cr);
4408 mutex_enter(&zp->z_lock);
4409 mutex_enter(&vp->v_lock);
4410 ASSERT(vp->v_count == 1);
4411 VN_RELE_LOCKED(vp);
4412 mutex_exit(&vp->v_lock);
4413 mutex_exit(&zp->z_lock);
4414 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4415 zfs_znode_free(zp);
4416 return;
4420 * Attempt to push any data in the page cache. If this fails
4421 * we will get kicked out later in zfs_zinactive().
4423 if (vn_has_cached_data(vp)) {
4424 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4425 cr);
4428 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4429 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4431 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4432 zfs_sa_upgrade_txholds(tx, zp);
4433 error = dmu_tx_assign(tx, TXG_WAIT);
4434 if (error) {
4435 dmu_tx_abort(tx);
4436 } else {
4437 mutex_enter(&zp->z_lock);
4438 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4439 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4440 zp->z_atime_dirty = 0;
4441 mutex_exit(&zp->z_lock);
4442 dmu_tx_commit(tx);
4446 zfs_zinactive(zp);
4447 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4451 * Bounds-check the seek operation.
4453 * IN: vp - vnode seeking within
4454 * ooff - old file offset
4455 * noffp - pointer to new file offset
4456 * ct - caller context
4458 * RETURN: 0 on success, EINVAL if new offset invalid.
4460 /* ARGSUSED */
4461 static int
4462 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4463 caller_context_t *ct)
4465 if (vp->v_type == VDIR)
4466 return (0);
4467 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4471 * Pre-filter the generic locking function to trap attempts to place
4472 * a mandatory lock on a memory mapped file.
4474 static int
4475 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4476 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4478 znode_t *zp = VTOZ(vp);
4479 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4481 ZFS_ENTER(zfsvfs);
4482 ZFS_VERIFY_ZP(zp);
4485 * We are following the UFS semantics with respect to mapcnt
4486 * here: If we see that the file is mapped already, then we will
4487 * return an error, but we don't worry about races between this
4488 * function and zfs_map().
4490 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4491 ZFS_EXIT(zfsvfs);
4492 return (SET_ERROR(EAGAIN));
4494 ZFS_EXIT(zfsvfs);
4495 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4499 * If we can't find a page in the cache, we will create a new page
4500 * and fill it with file data. For efficiency, we may try to fill
4501 * multiple pages at once (klustering) to fill up the supplied page
4502 * list. Note that the pages to be filled are held with an exclusive
4503 * lock to prevent access by other threads while they are being filled.
4505 static int
4506 zfs_fillpage(vnode_t *vp, uoff_t off, struct seg *seg,
4507 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4509 znode_t *zp = VTOZ(vp);
4510 page_t *pp, *cur_pp;
4511 objset_t *os = zp->z_zfsvfs->z_os;
4512 uoff_t io_off, total;
4513 size_t io_len;
4514 int err;
4516 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4518 * We only have a single page, don't bother klustering
4520 io_off = off;
4521 io_len = PAGESIZE;
4522 pp = page_create_va(&vp->v_object, io_off, io_len,
4523 PG_EXCL | PG_WAIT, seg, addr);
4524 } else {
4526 * Try to find enough pages to fill the page list
4528 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4529 &io_len, off, plsz, 0);
4531 if (pp == NULL) {
4533 * The page already exists, nothing to do here.
4535 *pl = NULL;
4536 return (0);
4540 * Fill the pages in the kluster.
4542 cur_pp = pp;
4543 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4544 caddr_t va;
4546 ASSERT3U(io_off, ==, cur_pp->p_offset);
4547 va = zfs_map_page(cur_pp, S_WRITE);
4548 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4549 DMU_READ_PREFETCH);
4550 zfs_unmap_page(cur_pp, va);
4551 if (err) {
4552 /* On error, toss the entire kluster */
4553 pvn_read_done(pp, B_ERROR);
4554 /* convert checksum errors into IO errors */
4555 if (err == ECKSUM)
4556 err = SET_ERROR(EIO);
4557 return (err);
4559 cur_pp = cur_pp->p_next;
4563 * Fill in the page list array from the kluster starting
4564 * from the desired offset `off'.
4565 * NOTE: the page list will always be null terminated.
4567 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4568 ASSERT(pl == NULL || (*pl)->p_offset == off);
4570 return (0);
4574 * Return pointers to the pages for the file region [off, off + len]
4575 * in the pl array. If plsz is greater than len, this function may
4576 * also return page pointers from after the specified region
4577 * (i.e. the region [off, off + plsz]). These additional pages are
4578 * only returned if they are already in the cache, or were created as
4579 * part of a klustered read.
4581 * IN: vp - vnode of file to get data from.
4582 * off - position in file to get data from.
4583 * len - amount of data to retrieve.
4584 * plsz - length of provided page list.
4585 * seg - segment to obtain pages for.
4586 * addr - virtual address of fault.
4587 * rw - mode of created pages.
4588 * cr - credentials of caller.
4589 * ct - caller context.
4591 * OUT: protp - protection mode of created pages.
4592 * pl - list of pages created.
4594 * RETURN: 0 on success, error code on failure.
4596 * Timestamps:
4597 * vp - atime updated
4599 /* ARGSUSED */
4600 static int
4601 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4602 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4603 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4605 znode_t *zp = VTOZ(vp);
4606 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4607 page_t **pl0 = pl;
4608 int err = 0;
4610 /* we do our own caching, faultahead is unnecessary */
4611 if (pl == NULL)
4612 return (0);
4613 else if (len > plsz)
4614 len = plsz;
4615 else
4616 len = P2ROUNDUP(len, PAGESIZE);
4617 ASSERT(plsz >= len);
4619 ZFS_ENTER(zfsvfs);
4620 ZFS_VERIFY_ZP(zp);
4622 if (protp)
4623 *protp = PROT_ALL;
4626 * Loop through the requested range [off, off + len) looking
4627 * for pages. If we don't find a page, we will need to create
4628 * a new page and fill it with data from the file.
4630 while (len > 0) {
4631 if (*pl = page_lookup(&vp->v_object, off, SE_SHARED))
4632 *(pl+1) = NULL;
4633 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4634 goto out;
4635 while (*pl) {
4636 ASSERT3U((*pl)->p_offset, ==, off);
4637 off += PAGESIZE;
4638 addr += PAGESIZE;
4639 if (len > 0) {
4640 ASSERT3U(len, >=, PAGESIZE);
4641 len -= PAGESIZE;
4643 ASSERT3U(plsz, >=, PAGESIZE);
4644 plsz -= PAGESIZE;
4645 pl++;
4650 * Fill out the page array with any pages already in the cache.
4652 while (plsz > 0 &&
4653 (*pl++ = page_lookup_nowait(&vp->v_object, off, SE_SHARED))) {
4654 off += PAGESIZE;
4655 plsz -= PAGESIZE;
4657 out:
4658 if (err) {
4660 * Release any pages we have previously locked.
4662 while (pl > pl0)
4663 page_unlock(*--pl);
4664 } else {
4665 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4668 *pl = NULL;
4670 ZFS_EXIT(zfsvfs);
4671 return (err);
4675 * Request a memory map for a section of a file. This code interacts
4676 * with common code and the VM system as follows:
4678 * - common code calls mmap(), which ends up in smmap_common()
4679 * - this calls fop_map(), which takes you into (say) zfs
4680 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4681 * - segvn_create() creates the new segment and calls fop_addmap()
4682 * - zfs_addmap() updates z_mapcnt
4684 /*ARGSUSED*/
4685 static int
4686 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4687 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4688 caller_context_t *ct)
4690 znode_t *zp = VTOZ(vp);
4691 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4692 segvn_crargs_t vn_a;
4693 int error;
4695 ZFS_ENTER(zfsvfs);
4696 ZFS_VERIFY_ZP(zp);
4699 * Note: ZFS_READONLY is handled in zfs_zaccess_common.
4702 if ((prot & PROT_WRITE) && (zp->z_pflags &
4703 (ZFS_IMMUTABLE | ZFS_APPENDONLY))) {
4704 ZFS_EXIT(zfsvfs);
4705 return (SET_ERROR(EPERM));
4708 if ((prot & (PROT_READ | PROT_EXEC)) &&
4709 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4710 ZFS_EXIT(zfsvfs);
4711 return (SET_ERROR(EACCES));
4714 if (vp->v_flag & VNOMAP) {
4715 ZFS_EXIT(zfsvfs);
4716 return (SET_ERROR(ENOSYS));
4719 if (off < 0 || len > MAXOFFSET_T - off) {
4720 ZFS_EXIT(zfsvfs);
4721 return (SET_ERROR(ENXIO));
4724 if (vp->v_type != VREG) {
4725 ZFS_EXIT(zfsvfs);
4726 return (SET_ERROR(ENODEV));
4730 * If file is locked, disallow mapping.
4732 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4733 ZFS_EXIT(zfsvfs);
4734 return (SET_ERROR(EAGAIN));
4737 as_rangelock(as);
4738 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4739 if (error != 0) {
4740 as_rangeunlock(as);
4741 ZFS_EXIT(zfsvfs);
4742 return (error);
4745 vn_a.vp = vp;
4746 vn_a.offset = (uoff_t)off;
4747 vn_a.type = flags & MAP_TYPE;
4748 vn_a.prot = prot;
4749 vn_a.maxprot = maxprot;
4750 vn_a.cred = cr;
4751 vn_a.amp = NULL;
4752 vn_a.flags = flags & ~MAP_TYPE;
4753 vn_a.szc = 0;
4754 vn_a.lgrp_mem_policy_flags = 0;
4756 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4758 as_rangeunlock(as);
4759 ZFS_EXIT(zfsvfs);
4760 return (error);
4763 /* ARGSUSED */
4764 static int
4765 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4766 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4767 caller_context_t *ct)
4769 uint64_t pages = btopr(len);
4771 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4772 return (0);
4776 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4777 * more accurate mtime for the associated file. Since we don't have a way of
4778 * detecting when the data was actually modified, we have to resort to
4779 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4780 * last page is pushed. The problem occurs when the msync() call is omitted,
4781 * which by far the most common case:
4783 * open()
4784 * mmap()
4785 * <modify memory>
4786 * munmap()
4787 * close()
4788 * <time lapse>
4789 * putpage() via fsflush
4791 * If we wait until fsflush to come along, we can have a modification time that
4792 * is some arbitrary point in the future. In order to prevent this in the
4793 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4794 * torn down.
4796 /* ARGSUSED */
4797 static int
4798 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4799 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4800 caller_context_t *ct)
4802 uint64_t pages = btopr(len);
4804 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4805 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4807 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4808 vn_has_cached_data(vp))
4809 (void) fop_putpage(vp, off, len, B_ASYNC, cr, ct);
4811 return (0);
4815 * Free or allocate space in a file. Currently, this function only
4816 * supports the `F_FREESP' command. However, this command is somewhat
4817 * misnamed, as its functionality includes the ability to allocate as
4818 * well as free space.
4820 * IN: vp - vnode of file to free data in.
4821 * cmd - action to take (only F_FREESP supported).
4822 * bfp - section of file to free/alloc.
4823 * flag - current file open mode flags.
4824 * offset - current file offset.
4825 * cr - credentials of caller [UNUSED].
4826 * ct - caller context.
4828 * RETURN: 0 on success, error code on failure.
4830 * Timestamps:
4831 * vp - ctime|mtime updated
4833 /* ARGSUSED */
4834 static int
4835 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4836 offset_t offset, cred_t *cr, caller_context_t *ct)
4838 znode_t *zp = VTOZ(vp);
4839 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4840 uint64_t off, len;
4841 int error;
4843 ZFS_ENTER(zfsvfs);
4844 ZFS_VERIFY_ZP(zp);
4846 if (cmd != F_FREESP) {
4847 ZFS_EXIT(zfsvfs);
4848 return (SET_ERROR(EINVAL));
4852 * In a case vp->v_vfsp != zp->z_zfsvfs->z_vfs (e.g. snapshots) our
4853 * callers might not be able to detect properly that we are read-only,
4854 * so check it explicitly here.
4856 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
4857 ZFS_EXIT(zfsvfs);
4858 return (SET_ERROR(EROFS));
4861 if (error = convoff(vp, bfp, 0, offset)) {
4862 ZFS_EXIT(zfsvfs);
4863 return (error);
4866 if (bfp->l_len < 0) {
4867 ZFS_EXIT(zfsvfs);
4868 return (SET_ERROR(EINVAL));
4871 off = bfp->l_start;
4872 len = bfp->l_len; /* 0 means from off to end of file */
4874 error = zfs_freesp(zp, off, len, flag, TRUE);
4876 if (error == 0 && off == 0 && len == 0)
4877 vnevent_truncate(ZTOV(zp), ct);
4879 ZFS_EXIT(zfsvfs);
4880 return (error);
4883 /*ARGSUSED*/
4884 static int
4885 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4887 znode_t *zp = VTOZ(vp);
4888 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4889 uint32_t gen;
4890 uint64_t gen64;
4891 uint64_t object = zp->z_id;
4892 zfid_short_t *zfid;
4893 int size, i, error;
4895 ZFS_ENTER(zfsvfs);
4896 ZFS_VERIFY_ZP(zp);
4898 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4899 &gen64, sizeof (uint64_t))) != 0) {
4900 ZFS_EXIT(zfsvfs);
4901 return (error);
4904 gen = (uint32_t)gen64;
4906 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4907 if (fidp->fid_len < size) {
4908 fidp->fid_len = size;
4909 ZFS_EXIT(zfsvfs);
4910 return (SET_ERROR(ENOSPC));
4913 zfid = (zfid_short_t *)fidp;
4915 zfid->zf_len = size;
4917 for (i = 0; i < sizeof (zfid->zf_object); i++)
4918 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4920 /* Must have a non-zero generation number to distinguish from .zfs */
4921 if (gen == 0)
4922 gen = 1;
4923 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4924 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4926 if (size == LONG_FID_LEN) {
4927 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4928 zfid_long_t *zlfid;
4930 zlfid = (zfid_long_t *)fidp;
4932 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4933 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4935 /* XXX - this should be the generation number for the objset */
4936 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4937 zlfid->zf_setgen[i] = 0;
4940 ZFS_EXIT(zfsvfs);
4941 return (0);
4944 static int
4945 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4946 caller_context_t *ct)
4948 znode_t *zp, *xzp;
4949 zfsvfs_t *zfsvfs;
4950 zfs_dirlock_t *dl;
4951 int error;
4953 switch (cmd) {
4954 case _PC_LINK_MAX:
4955 *valp = ULONG_MAX;
4956 return (0);
4958 case _PC_FILESIZEBITS:
4959 *valp = 64;
4960 return (0);
4962 case _PC_XATTR_EXISTS:
4963 zp = VTOZ(vp);
4964 zfsvfs = zp->z_zfsvfs;
4965 ZFS_ENTER(zfsvfs);
4966 ZFS_VERIFY_ZP(zp);
4967 *valp = 0;
4968 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4969 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4970 if (error == 0) {
4971 zfs_dirent_unlock(dl);
4972 if (!zfs_dirempty(xzp))
4973 *valp = 1;
4974 VN_RELE(ZTOV(xzp));
4975 } else if (error == ENOENT) {
4977 * If there aren't extended attributes, it's the
4978 * same as having zero of them.
4980 error = 0;
4982 ZFS_EXIT(zfsvfs);
4983 return (error);
4985 case _PC_SATTR_ENABLED:
4986 case _PC_SATTR_EXISTS:
4987 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4988 (vp->v_type == VREG || vp->v_type == VDIR);
4989 return (0);
4991 case _PC_ACCESS_FILTERING:
4992 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4993 vp->v_type == VDIR;
4994 return (0);
4996 case _PC_ACL_ENABLED:
4997 *valp = _ACL_ACE_ENABLED;
4998 return (0);
5000 case _PC_MIN_HOLE_SIZE:
5001 *valp = (ulong_t)SPA_MINBLOCKSIZE;
5002 return (0);
5004 case _PC_TIMESTAMP_RESOLUTION:
5005 /* nanosecond timestamp resolution */
5006 *valp = 1L;
5007 return (0);
5009 default:
5010 return (fs_pathconf(vp, cmd, valp, cr, ct));
5014 /*ARGSUSED*/
5015 static int
5016 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5017 caller_context_t *ct)
5019 znode_t *zp = VTOZ(vp);
5020 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5021 int error;
5022 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5024 ZFS_ENTER(zfsvfs);
5025 ZFS_VERIFY_ZP(zp);
5026 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
5027 ZFS_EXIT(zfsvfs);
5029 return (error);
5032 /*ARGSUSED*/
5033 static int
5034 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
5035 caller_context_t *ct)
5037 znode_t *zp = VTOZ(vp);
5038 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5039 int error;
5040 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
5041 zilog_t *zilog = zfsvfs->z_log;
5043 ZFS_ENTER(zfsvfs);
5044 ZFS_VERIFY_ZP(zp);
5046 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
5048 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
5049 zil_commit(zilog, 0);
5051 ZFS_EXIT(zfsvfs);
5052 return (error);
5056 * The smallest read we may consider to loan out an arcbuf.
5057 * This must be a power of 2.
5059 int zcr_blksz_min = (1 << 10); /* 1K */
5061 * If set to less than the file block size, allow loaning out of an
5062 * arcbuf for a partial block read. This must be a power of 2.
5064 int zcr_blksz_max = (1 << 17); /* 128K */
5066 /*ARGSUSED*/
5067 static int
5068 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
5069 caller_context_t *ct)
5071 znode_t *zp = VTOZ(vp);
5072 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5073 int max_blksz = zfsvfs->z_max_blksz;
5074 uio_t *uio = &xuio->xu_uio;
5075 ssize_t size = uio->uio_resid;
5076 offset_t offset = uio->uio_loffset;
5077 int blksz;
5078 int fullblk, i;
5079 arc_buf_t *abuf;
5080 ssize_t maxsize;
5081 int preamble, postamble;
5083 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
5084 return (SET_ERROR(EINVAL));
5086 ZFS_ENTER(zfsvfs);
5087 ZFS_VERIFY_ZP(zp);
5088 switch (ioflag) {
5089 case UIO_WRITE:
5091 * Loan out an arc_buf for write if write size is bigger than
5092 * max_blksz, and the file's block size is also max_blksz.
5094 blksz = max_blksz;
5095 if (size < blksz || zp->z_blksz != blksz) {
5096 ZFS_EXIT(zfsvfs);
5097 return (SET_ERROR(EINVAL));
5100 * Caller requests buffers for write before knowing where the
5101 * write offset might be (e.g. NFS TCP write).
5103 if (offset == -1) {
5104 preamble = 0;
5105 } else {
5106 preamble = P2PHASE(offset, blksz);
5107 if (preamble) {
5108 preamble = blksz - preamble;
5109 size -= preamble;
5113 postamble = P2PHASE(size, blksz);
5114 size -= postamble;
5116 fullblk = size / blksz;
5117 (void) dmu_xuio_init(xuio,
5118 (preamble != 0) + fullblk + (postamble != 0));
5119 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5120 int, postamble, int,
5121 (preamble != 0) + fullblk + (postamble != 0));
5124 * Have to fix iov base/len for partial buffers. They
5125 * currently represent full arc_buf's.
5127 if (preamble) {
5128 /* data begins in the middle of the arc_buf */
5129 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5130 blksz);
5131 ASSERT(abuf);
5132 (void) dmu_xuio_add(xuio, abuf,
5133 blksz - preamble, preamble);
5136 for (i = 0; i < fullblk; i++) {
5137 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5138 blksz);
5139 ASSERT(abuf);
5140 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5143 if (postamble) {
5144 /* data ends in the middle of the arc_buf */
5145 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5146 blksz);
5147 ASSERT(abuf);
5148 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5150 break;
5151 case UIO_READ:
5153 * Loan out an arc_buf for read if the read size is larger than
5154 * the current file block size. Block alignment is not
5155 * considered. Partial arc_buf will be loaned out for read.
5157 blksz = zp->z_blksz;
5158 if (blksz < zcr_blksz_min)
5159 blksz = zcr_blksz_min;
5160 if (blksz > zcr_blksz_max)
5161 blksz = zcr_blksz_max;
5162 /* avoid potential complexity of dealing with it */
5163 if (blksz > max_blksz) {
5164 ZFS_EXIT(zfsvfs);
5165 return (SET_ERROR(EINVAL));
5168 maxsize = zp->z_size - uio->uio_loffset;
5169 if (size > maxsize)
5170 size = maxsize;
5172 if (size < blksz || vn_has_cached_data(vp)) {
5173 ZFS_EXIT(zfsvfs);
5174 return (SET_ERROR(EINVAL));
5176 break;
5177 default:
5178 ZFS_EXIT(zfsvfs);
5179 return (SET_ERROR(EINVAL));
5182 uio->uio_extflg = UIO_XUIO;
5183 XUIO_XUZC_RW(xuio) = ioflag;
5184 ZFS_EXIT(zfsvfs);
5185 return (0);
5188 /*ARGSUSED*/
5189 static int
5190 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5192 int i;
5193 arc_buf_t *abuf;
5194 int ioflag = XUIO_XUZC_RW(xuio);
5196 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5198 i = dmu_xuio_cnt(xuio);
5199 while (i-- > 0) {
5200 abuf = dmu_xuio_arcbuf(xuio, i);
5202 * if abuf == NULL, it must be a write buffer
5203 * that has been returned in zfs_write().
5205 if (abuf)
5206 dmu_return_arcbuf(abuf);
5207 ASSERT(abuf || ioflag == UIO_WRITE);
5210 dmu_xuio_fini(xuio);
5211 return (0);
5215 * Predeclare these here so that the compiler assumes that
5216 * this is an "old style" function declaration that does
5217 * not include arguments => we won't get type mismatch errors
5218 * in the initializations that follow.
5220 static int zfs_inval();
5221 static int zfs_isdir();
5223 static int
5224 zfs_inval()
5226 return (SET_ERROR(EINVAL));
5229 static int
5230 zfs_isdir()
5232 return (SET_ERROR(EISDIR));
5236 * Directory vnode operations
5238 const struct vnodeops zfs_dvnodeops = {
5239 .vnop_name = "zfs",
5240 .vop_open = zfs_open,
5241 .vop_close = zfs_close,
5242 .vop_read = zfs_isdir,
5243 .vop_write = zfs_isdir,
5244 .vop_ioctl = zfs_ioctl,
5245 .vop_getattr = zfs_getattr,
5246 .vop_setattr = zfs_setattr,
5247 .vop_access = zfs_access,
5248 .vop_lookup = zfs_lookup,
5249 .vop_create = zfs_create,
5250 .vop_remove = zfs_remove,
5251 .vop_link = zfs_link,
5252 .vop_rename = zfs_rename,
5253 .vop_mkdir = zfs_mkdir,
5254 .vop_rmdir = zfs_rmdir,
5255 .vop_readdir = zfs_readdir,
5256 .vop_symlink = zfs_symlink,
5257 .vop_fsync = zfs_fsync,
5258 .vop_inactive = zfs_inactive,
5259 .vop_fid = zfs_fid,
5260 .vop_seek = zfs_seek,
5261 .vop_pathconf = zfs_pathconf,
5262 .vop_getsecattr = zfs_getsecattr,
5263 .vop_setsecattr = zfs_setsecattr,
5264 .vop_vnevent = fs_vnevent_support,
5268 * Regular file vnode operations
5270 const struct vnodeops zfs_fvnodeops = {
5271 .vnop_name = "zfs",
5272 .vop_open = zfs_open,
5273 .vop_close = zfs_close,
5274 .vop_read = zfs_read,
5275 .vop_write = zfs_write,
5276 .vop_ioctl = zfs_ioctl,
5277 .vop_getattr = zfs_getattr,
5278 .vop_setattr = zfs_setattr,
5279 .vop_access = zfs_access,
5280 .vop_lookup = zfs_lookup,
5281 .vop_rename = zfs_rename,
5282 .vop_fsync = zfs_fsync,
5283 .vop_inactive = zfs_inactive,
5284 .vop_fid = zfs_fid,
5285 .vop_seek = zfs_seek,
5286 .vop_frlock = zfs_frlock,
5287 .vop_space = zfs_space,
5288 .vop_getpage = zfs_getpage,
5289 .vop_putpage = zfs_putpage,
5290 .vop_map = zfs_map,
5291 .vop_addmap = zfs_addmap,
5292 .vop_delmap = zfs_delmap,
5293 .vop_pathconf = zfs_pathconf,
5294 .vop_getsecattr = zfs_getsecattr,
5295 .vop_setsecattr = zfs_setsecattr,
5296 .vop_vnevent = fs_vnevent_support,
5297 .vop_reqzcbuf = zfs_reqzcbuf,
5298 .vop_retzcbuf = zfs_retzcbuf,
5302 * Symbolic link vnode operations
5304 const struct vnodeops zfs_symvnodeops = {
5305 .vnop_name = "zfs",
5306 .vop_getattr = zfs_getattr,
5307 .vop_setattr = zfs_setattr,
5308 .vop_access = zfs_access,
5309 .vop_rename = zfs_rename,
5310 .vop_readlink = zfs_readlink,
5311 .vop_inactive = zfs_inactive,
5312 .vop_fid = zfs_fid,
5313 .vop_pathconf = zfs_pathconf,
5314 .vop_vnevent = fs_vnevent_support,
5318 * special share hidden files vnode operations
5320 const struct vnodeops zfs_sharevnodeops = {
5321 .vnop_name = "zfs",
5322 .vop_getattr = zfs_getattr,
5323 .vop_access = zfs_access,
5324 .vop_inactive = zfs_inactive,
5325 .vop_fid = zfs_fid,
5326 .vop_pathconf = zfs_pathconf,
5327 .vop_getsecattr = zfs_getsecattr,
5328 .vop_setsecattr = zfs_setsecattr,
5329 .vop_vnevent = fs_vnevent_support,
5333 * Extended attribute directory vnode operations
5335 * These ops are identical to the directory vnode
5336 * operations except for restricted operations:
5337 * fop_mkdir()
5338 * fop_symlink()
5340 * Note that there are other restrictions embedded in:
5341 * zfs_create() - restrict type to VREG
5342 * zfs_link() - no links into/out of attribute space
5343 * zfs_rename() - no moves into/out of attribute space
5345 const struct vnodeops zfs_xdvnodeops = {
5346 .vnop_name = "zfs",
5347 .vop_open = zfs_open,
5348 .vop_close = zfs_close,
5349 .vop_ioctl = zfs_ioctl,
5350 .vop_getattr = zfs_getattr,
5351 .vop_setattr = zfs_setattr,
5352 .vop_access = zfs_access,
5353 .vop_lookup = zfs_lookup,
5354 .vop_create = zfs_create,
5355 .vop_remove = zfs_remove,
5356 .vop_link = zfs_link,
5357 .vop_rename = zfs_rename,
5358 .vop_mkdir = zfs_inval,
5359 .vop_rmdir = zfs_rmdir,
5360 .vop_readdir = zfs_readdir,
5361 .vop_symlink = zfs_inval,
5362 .vop_fsync = zfs_fsync,
5363 .vop_inactive = zfs_inactive,
5364 .vop_fid = zfs_fid,
5365 .vop_seek = zfs_seek,
5366 .vop_pathconf = zfs_pathconf,
5367 .vop_getsecattr = zfs_getsecattr,
5368 .vop_setsecattr = zfs_setsecattr,
5369 .vop_vnevent = fs_vnevent_support,
5373 * Error vnode operations
5375 const struct vnodeops zfs_evnodeops = {
5376 .vnop_name = "zfs",
5377 .vop_inactive = zfs_inactive,
5378 .vop_pathconf = zfs_pathconf,