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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
28 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
35 #include <sys/vnode.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
42 #include <sys/unistd.h>
43 #include <sys/sunddi.h>
44 #include <sys/random.h>
45 #include <sys/policy.h>
46 #include <sys/kcondvar.h>
47 #include <sys/callb.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/fs/zfs.h>
54 #include <sys/atomic.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/zfs_fuid.h>
58 #include <sys/zfs_sa.h>
60 #include <sys/extdirent.h>
63 * zfs_match_find() is used by zfs_dirent_lookup() to peform zap lookups
64 * of names after deciding which is the appropriate lookup interface.
67 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
68 matchtype_t mt, uint64_t *zoid)
75 * In the non-mixed case we only expect there would ever
76 * be one match, but we need to use the normalizing lookup.
78 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
79 zoid, mt, NULL, 0, NULL);
81 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
83 *zoid = ZFS_DIRENT_OBJ(*zoid);
89 * Look up a directory entry under a locked vnode.
90 * dvp being locked gives us a guarantee that there are no concurrent
91 * modification of the directory and, thus, if a node can be found in
92 * the directory, then it must not be unlinked.
95 * dzp - znode for directory
96 * name - name of entry to lock
97 * flag - ZNEW: if the entry already exists, fail with EEXIST.
98 * ZEXISTS: if the entry does not exist, fail with ENOENT.
99 * ZXATTR: we want dzp's xattr directory
102 * zpp - pointer to the znode for the entry (NULL if there isn't one)
104 * Return value: 0 on success or errno on failure.
106 * NOTE: Always checks for, and rejects, '.' and '..'.
109 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag)
111 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
117 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
122 * Verify that we are not trying to lock '.', '..', or '.zfs'
124 if (name[0] == '.' &&
125 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
126 zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
127 return (SET_ERROR(EEXIST));
130 * Case sensitivity and normalization preferences are set when
131 * the file system is created. These are stored in the
132 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
133 * affect how we perform zap lookups.
135 * When matching we may need to normalize & change case according to
138 * Note that a normalized match is necessary for a case insensitive
139 * filesystem when the lookup request is not exact because normalization
140 * can fold case independent of normalizing code point sequences.
142 * See the table above zfs_dropname().
144 if (zfsvfs->z_norm != 0) {
148 * Determine if the match needs to honor the case specified in
149 * lookup, and if so keep track of that so that during
150 * normalization we don't fold case.
152 if (zfsvfs->z_case == ZFS_CASE_MIXED) {
158 * Only look in or update the DNLC if we are looking for the
159 * name on a file system that does not require normalization
160 * or case folding. We can also look there if we happen to be
161 * on a non-normalizing, mixed sensitivity file system IF we
162 * are looking for the exact name.
164 * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE
165 * because in that case MT_EXACT and MT_FIRST should produce exactly
169 if (dzp->z_unlinked && !(flag & ZXATTR))
172 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
175 error = (zoid == 0 ? ENOENT : 0);
177 error = zfs_match_find(zfsvfs, dzp, name, mt, &zoid);
180 if (error != ENOENT || (flag & ZEXISTS)) {
185 return (SET_ERROR(EEXIST));
187 error = zfs_zget(zfsvfs, zoid, zpp);
190 ASSERT(!(*zpp)->z_unlinked);
197 zfs_dd_lookup(znode_t *dzp, znode_t **zpp)
199 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
204 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
205 ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
210 if ((error = sa_lookup(dzp->z_sa_hdl,
211 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
214 error = zfs_zget(zfsvfs, parent, &zp);
221 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp)
223 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
227 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
228 ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
231 return (SET_ERROR(ENOENT));
233 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
235 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
236 error = zfs_dd_lookup(dzp, zpp);
238 error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS);
240 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
248 * unlinked Set (formerly known as the "delete queue") Error Handling
250 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
251 * don't specify the name of the entry that we will be manipulating. We
252 * also fib and say that we won't be adding any new entries to the
253 * unlinked set, even though we might (this is to lower the minimum file
254 * size that can be deleted in a full filesystem). So on the small
255 * chance that the nlink list is using a fat zap (ie. has more than
256 * 2000 entries), we *may* not pre-read a block that's needed.
257 * Therefore it is remotely possible for some of the assertions
258 * regarding the unlinked set below to fail due to i/o error. On a
259 * nondebug system, this will result in the space being leaked.
262 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
264 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
266 ASSERT(zp->z_unlinked);
267 ASSERT(zp->z_links == 0);
270 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
274 * Clean up any znodes that had no links when we either crashed or
275 * (force) umounted the file system.
278 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
282 dmu_object_info_t doi;
288 * Interate over the contents of the unlinked set.
290 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
291 zap_cursor_retrieve(&zc, &zap) == 0;
292 zap_cursor_advance(&zc)) {
295 * See what kind of object we have in list
298 error = dmu_object_info(zfsvfs->z_os,
299 zap.za_first_integer, &doi);
303 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
304 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
306 * We need to re-mark these list entries for deletion,
307 * so we pull them back into core and set zp->z_unlinked.
309 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
312 * We may pick up znodes that are already marked for deletion.
313 * This could happen during the purge of an extended attribute
314 * directory. All we need to do is skip over them, since they
315 * are already in the system marked z_unlinked.
320 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
321 #if defined(__FreeBSD__)
323 * Due to changes in zfs_rmnode we need to make sure the
324 * link count is set to zero here.
326 if (zp->z_links != 0) {
327 tx = dmu_tx_create(zfsvfs->z_os);
328 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
329 error = dmu_tx_assign(tx, TXG_WAIT);
336 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
337 &zp->z_links, sizeof (zp->z_links), tx));
341 zp->z_unlinked = B_TRUE;
344 zap_cursor_fini(&zc);
348 * Delete the entire contents of a directory. Return a count
349 * of the number of entries that could not be deleted. If we encounter
350 * an error, return a count of at least one so that the directory stays
351 * in the unlinked set.
353 * NOTE: this function assumes that the directory is inactive,
354 * so there is no need to lock its entries before deletion.
355 * Also, it assumes the directory contents is *only* regular
359 zfs_purgedir(znode_t *dzp)
365 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
369 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
370 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
371 zap_cursor_advance(&zc)) {
372 error = zfs_zget(zfsvfs,
373 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
379 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
380 ASSERT((ZTOV(xzp)->v_type == VREG) ||
381 (ZTOV(xzp)->v_type == VLNK));
383 tx = dmu_tx_create(zfsvfs->z_os);
384 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
385 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
386 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
387 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
388 /* Is this really needed ? */
389 zfs_sa_upgrade_txholds(tx, xzp);
390 dmu_tx_mark_netfree(tx);
391 error = dmu_tx_assign(tx, TXG_WAIT);
399 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
406 zap_cursor_fini(&zc);
412 #if defined(__FreeBSD__)
413 extern taskq_t *zfsvfs_taskq;
417 zfs_rmnode(znode_t *zp)
419 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
420 objset_t *os = zfsvfs->z_os;
426 ASSERT(zp->z_links == 0);
427 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
430 * If this is an attribute directory, purge its contents.
432 if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
433 (zp->z_pflags & ZFS_XATTR)) {
434 if (zfs_purgedir(zp) != 0) {
436 * Not enough space to delete some xattrs.
437 * Leave it in the unlinked set.
439 zfs_znode_dmu_fini(zp);
445 * Free up all the data in the file. We don't do this for
446 * XATTR directories because we need truncate and remove to be
447 * in the same tx, like in zfs_znode_delete(). Otherwise, if
448 * we crash here we'll end up with an inconsistent truncated
449 * zap object in the delete queue. Note a truncated file is
450 * harmless since it only contains user data.
452 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
455 * Not enough space or we were interrupted by unmount.
456 * Leave the file in the unlinked set.
458 zfs_znode_dmu_fini(zp);
465 * If the file has extended attributes, we're going to unlink
468 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
469 &xattr_obj, sizeof (xattr_obj));
473 acl_obj = zfs_external_acl(zp);
476 * Set up the final transaction.
478 tx = dmu_tx_create(os);
479 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
480 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
482 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
484 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
486 zfs_sa_upgrade_txholds(tx, zp);
487 error = dmu_tx_assign(tx, TXG_WAIT);
490 * Not enough space to delete the file. Leave it in the
491 * unlinked set, leaking it until the fs is remounted (at
492 * which point we'll call zfs_unlinked_drain() to process it).
495 zfs_znode_dmu_fini(zp);
500 #if defined(__FreeBSD__)
502 * FreeBSD's implemention of zfs_zget requires a vnode to back it.
503 * This means that we could end up calling into getnewvnode while
504 * calling zfs_rmnode as a result of a prior call to getnewvnode
505 * trying to clear vnodes out of the cache. If this repeats we can
506 * recurse enough that we overflow our stack. To avoid this, we
507 * avoid calling zfs_zget on the xattr znode and instead simply add
508 * it to the unlinked set and schedule a call to zfs_unlinked_drain.
511 /* Add extended attribute directory to the unlinked set. */
513 zap_add_int(os, zfsvfs->z_unlinkedobj, xattr_obj, tx));
518 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
519 xzp->z_links = 0; /* no more links to it */
520 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
521 &xzp->z_links, sizeof (xzp->z_links), tx));
522 zfs_unlinked_add(xzp, tx);
526 /* Remove this znode from the unlinked set */
528 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
530 zfs_znode_delete(zp, tx);
534 #if defined(__FreeBSD__)
537 * We're using the FreeBSD taskqueue API here instead of
538 * the Solaris taskq API since the FreeBSD API allows for a
539 * task to be enqueued multiple times but executed once.
541 taskqueue_enqueue(zfsvfs_taskq->tq_queue,
542 &zfsvfs->z_unlinked_drain_task);
548 zfs_dirent(znode_t *zp, uint64_t mode)
550 uint64_t de = zp->z_id;
552 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
553 de |= IFTODT(mode) << 60;
558 * Link zp into dzp. Can only fail if zp has been unlinked.
561 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
564 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
565 vnode_t *vp = ZTOV(zp);
567 int zp_is_dir = (vp->v_type == VDIR);
568 sa_bulk_attr_t bulk[5];
569 uint64_t mtime[2], ctime[2];
573 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
574 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
577 if (dzp->z_links >= ZFS_LINK_MAX)
578 return (SET_ERROR(EMLINK));
581 if (!(flag & ZRENAMING)) {
582 if (zp->z_unlinked) { /* no new links to unlinked zp */
583 ASSERT(!(flag & (ZNEW | ZEXISTS)));
584 return (SET_ERROR(ENOENT));
587 if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) {
588 return (SET_ERROR(EMLINK));
592 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
593 &zp->z_links, sizeof (zp->z_links));
596 ASSERT(zp->z_unlinked == 0);
598 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
599 &dzp->z_id, sizeof (dzp->z_id));
600 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
601 &zp->z_pflags, sizeof (zp->z_pflags));
603 if (!(flag & ZNEW)) {
604 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
605 ctime, sizeof (ctime));
606 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
609 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
613 dzp->z_links += zp_is_dir;
615 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
616 &dzp->z_size, sizeof (dzp->z_size));
617 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
618 &dzp->z_links, sizeof (dzp->z_links));
619 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
620 mtime, sizeof (mtime));
621 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
622 ctime, sizeof (ctime));
623 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
624 &dzp->z_pflags, sizeof (dzp->z_pflags));
625 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
626 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
629 value = zfs_dirent(zp, zp->z_mode);
630 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
638 * The match type in the code for this function should conform to:
640 * ------------------------------------------------------------------------
641 * fs type | z_norm | lookup type | match type
642 * ---------|-------------|-------------|----------------------------------
643 * CS !norm | 0 | 0 | 0 (exact)
644 * CS norm | formX | 0 | MT_NORMALIZE
645 * CI !norm | upper | !ZCIEXACT | MT_NORMALIZE
646 * CI !norm | upper | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
647 * CI norm | upper|formX | !ZCIEXACT | MT_NORMALIZE
648 * CI norm | upper|formX | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
649 * CM !norm | upper | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
650 * CM !norm | upper | ZCILOOK | MT_NORMALIZE
651 * CM norm | upper|formX | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
652 * CM norm | upper|formX | ZCILOOK | MT_NORMALIZE
655 * CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
656 * upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
657 * formX = unicode normalization form set on fs creation
660 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
665 if (zp->z_zfsvfs->z_norm) {
666 matchtype_t mt = MT_NORMALIZE;
668 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
672 error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
675 error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
682 * Unlink zp from dzp, and mark zp for deletion if this was the last link.
683 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
684 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
685 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
686 * and it's the caller's job to do it.
689 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
690 int flag, boolean_t *unlinkedp)
692 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
693 vnode_t *vp = ZTOV(zp);
694 int zp_is_dir = (vp->v_type == VDIR);
695 boolean_t unlinked = B_FALSE;
696 sa_bulk_attr_t bulk[5];
697 uint64_t mtime[2], ctime[2];
701 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
702 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
704 if (!(flag & ZRENAMING)) {
706 if (zp_is_dir && !zfs_dirempty(zp)) {
708 return (SET_ERROR(EEXIST));
710 return (SET_ERROR(ENOTEMPTY));
715 * If we get here, we are going to try to remove the object.
716 * First try removing the name from the directory; if that
717 * fails, return the error.
719 error = zfs_dropname(dzp, name, zp, tx, flag);
724 if (zp->z_links <= zp_is_dir) {
725 zfs_panic_recover("zfs: link count on vnode %p is %u, "
726 "should be at least %u", zp->z_vnode,
729 zp->z_links = zp_is_dir + 1;
731 if (--zp->z_links == zp_is_dir) {
732 zp->z_unlinked = B_TRUE;
736 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
737 NULL, &ctime, sizeof (ctime));
738 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
739 NULL, &zp->z_pflags, sizeof (zp->z_pflags));
740 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
743 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
744 NULL, &zp->z_links, sizeof (zp->z_links));
745 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
749 ASSERT(zp->z_unlinked == 0);
750 error = zfs_dropname(dzp, name, zp, tx, flag);
755 dzp->z_size--; /* one dirent removed */
756 dzp->z_links -= zp_is_dir; /* ".." link from zp */
757 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
758 NULL, &dzp->z_links, sizeof (dzp->z_links));
759 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
760 NULL, &dzp->z_size, sizeof (dzp->z_size));
761 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
762 NULL, ctime, sizeof (ctime));
763 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
764 NULL, mtime, sizeof (mtime));
765 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
766 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
767 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
768 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
771 if (unlinkedp != NULL)
772 *unlinkedp = unlinked;
774 zfs_unlinked_add(zp, tx);
780 * Indicate whether the directory is empty.
783 zfs_dirempty(znode_t *dzp)
785 return (dzp->z_size == 2);
789 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
791 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
795 zfs_acl_ids_t acl_ids;
796 boolean_t fuid_dirtied;
802 * In FreeBSD, access checking for creating an EA is being done
803 * in zfs_setextattr(),
805 #ifndef __FreeBSD_kernel__
806 if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
810 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
813 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
814 zfs_acl_ids_free(&acl_ids);
815 return (SET_ERROR(EDQUOT));
818 getnewvnode_reserve(1);
820 tx = dmu_tx_create(zfsvfs->z_os);
821 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
822 ZFS_SA_BASE_ATTR_SIZE);
823 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
824 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
825 fuid_dirtied = zfsvfs->z_fuid_dirty;
827 zfs_fuid_txhold(zfsvfs, tx);
828 error = dmu_tx_assign(tx, TXG_WAIT);
830 zfs_acl_ids_free(&acl_ids);
834 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
837 zfs_fuid_sync(zfsvfs, tx);
840 error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
841 &parent, sizeof (parent));
842 ASSERT(error == 0 && parent == zp->z_id);
845 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
846 sizeof (xzp->z_id), tx));
848 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
849 xzp, "", NULL, acl_ids.z_fuidp, vap);
851 zfs_acl_ids_free(&acl_ids);
854 getnewvnode_drop_reserve();
862 * Return a znode for the extended attribute directory for zp.
863 * ** If the directory does not already exist, it is created **
865 * IN: zp - znode to obtain attribute directory from
866 * cr - credentials of caller
867 * flags - flags from the VOP_LOOKUP call
869 * OUT: xzpp - pointer to extended attribute znode
871 * RETURN: 0 on success
872 * error number on failure
875 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
877 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
882 error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
892 if (!(flags & CREATE_XATTR_DIR)) {
894 return (SET_ERROR(ENOENT));
896 return (SET_ERROR(ENOATTR));
900 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
901 return (SET_ERROR(EROFS));
905 * The ability to 'create' files in an attribute
906 * directory comes from the write_xattr permission on the base file.
908 * The ability to 'search' an attribute directory requires
909 * read_xattr permission on the base file.
911 * Once in a directory the ability to read/write attributes
912 * is controlled by the permissions on the attribute file.
914 va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
916 va.va_mode = S_IFDIR | S_ISVTX | 0777;
917 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
919 error = zfs_make_xattrdir(zp, &va, xvpp, cr);
921 if (error == ERESTART) {
922 /* NB: we already did dmu_tx_wait() if necessary */
926 VOP_UNLOCK(*xvpp, 0);
932 * Decide whether it is okay to remove within a sticky directory.
934 * In sticky directories, write access is not sufficient;
935 * you can remove entries from a directory only if:
937 * you own the directory,
939 * the entry is a plain file and you have write access,
940 * or you are privileged (checked in secpolicy...).
942 * The function returns 0 if remove access is granted.
945 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
950 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
952 if (zdp->z_zfsvfs->z_replay)
955 if ((zdp->z_mode & S_ISVTX) == 0)
958 downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
959 fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
961 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
962 (ZTOV(zp)->v_type == VREG &&
963 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
966 return (secpolicy_vnode_remove(ZTOV(zp), cr));