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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/resource.h>
33 #include <sys/vnode.h>
37 #include <sys/cmn_err.h>
38 #include <sys/errno.h>
40 #include <sys/unistd.h>
41 #include <sys/sunddi.h>
42 #include <sys/random.h>
43 #include <sys/policy.h>
44 #include <sys/kcondvar.h>
45 #include <sys/callb.h>
47 #include <sys/zfs_dir.h>
48 #include <sys/zfs_acl.h>
49 #include <sys/fs/zfs.h>
52 #include <sys/atomic.h>
53 #include <sys/zfs_ctldir.h>
54 #include <sys/zfs_fuid.h>
56 #include <sys/zfs_sa.h>
58 #include <sys/extdirent.h>
61 * zfs_match_find() is used by zfs_dirent_lookup() to peform zap lookups
62 * of names after deciding which is the appropriate lookup interface.
65 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
66 boolean_t exact, uint64_t *zoid)
71 matchtype_t mt = exact? MT_EXACT : MT_FIRST;
74 * In the non-mixed case we only expect there would ever
75 * be one match, but we need to use the normalizing lookup.
77 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
78 zoid, mt, NULL, 0, NULL);
80 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
82 *zoid = ZFS_DIRENT_OBJ(*zoid);
88 * Look up a directory entry under a locked vnode.
89 * dvp being locked gives us a guarantee that there are no concurrent
90 * modification of the directory and, thus, if a node can be found in
91 * the directory, then it must not be unlinked.
94 * dzp - znode for directory
95 * name - name of entry to lock
96 * flag - ZNEW: if the entry already exists, fail with EEXIST.
97 * ZEXISTS: if the entry does not exist, fail with ENOENT.
98 * ZXATTR: we want dzp's xattr directory
101 * zpp - pointer to the znode for the entry (NULL if there isn't one)
103 * Return value: 0 on success or errno on failure.
105 * NOTE: Always checks for, and rejects, '.' and '..'.
108 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag)
110 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
116 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
121 * Verify that we are not trying to lock '.', '..', or '.zfs'
123 if (name[0] == '.' &&
124 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
125 zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
126 return (SET_ERROR(EEXIST));
129 * Case sensitivity and normalization preferences are set when
130 * the file system is created. These are stored in the
131 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
132 * affect how we perform zap lookups.
134 * Decide if exact matches should be requested when performing
135 * a zap lookup on file systems supporting case-insensitive
138 * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE
139 * because in that case MT_EXACT and MT_FIRST should produce exactly
142 exact = zfsvfs->z_case == ZFS_CASE_MIXED;
144 if (dzp->z_unlinked && !(flag & ZXATTR))
147 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
150 error = (zoid == 0 ? ENOENT : 0);
152 error = zfs_match_find(zfsvfs, dzp, name, exact, &zoid);
155 if (error != ENOENT || (flag & ZEXISTS)) {
160 return (SET_ERROR(EEXIST));
162 error = zfs_zget(zfsvfs, zoid, zpp);
165 ASSERT(!(*zpp)->z_unlinked);
172 zfs_dd_lookup(znode_t *dzp, znode_t **zpp)
174 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
179 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
180 ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
185 if ((error = sa_lookup(dzp->z_sa_hdl,
186 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
189 error = zfs_zget(zfsvfs, parent, &zp);
196 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp)
198 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
202 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
203 ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
206 return (SET_ERROR(ENOENT));
208 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
210 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
211 error = zfs_dd_lookup(dzp, zpp);
213 error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS);
215 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
223 * unlinked Set (formerly known as the "delete queue") Error Handling
225 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
226 * don't specify the name of the entry that we will be manipulating. We
227 * also fib and say that we won't be adding any new entries to the
228 * unlinked set, even though we might (this is to lower the minimum file
229 * size that can be deleted in a full filesystem). So on the small
230 * chance that the nlink list is using a fat zap (ie. has more than
231 * 2000 entries), we *may* not pre-read a block that's needed.
232 * Therefore it is remotely possible for some of the assertions
233 * regarding the unlinked set below to fail due to i/o error. On a
234 * nondebug system, this will result in the space being leaked.
237 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
239 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
241 ASSERT(zp->z_unlinked);
242 ASSERT(zp->z_links == 0);
245 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
249 * Clean up any znodes that had no links when we either crashed or
250 * (force) umounted the file system.
253 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
257 dmu_object_info_t doi;
262 * Interate over the contents of the unlinked set.
264 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
265 zap_cursor_retrieve(&zc, &zap) == 0;
266 zap_cursor_advance(&zc)) {
269 * See what kind of object we have in list
272 error = dmu_object_info(zfsvfs->z_os,
273 zap.za_first_integer, &doi);
277 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
278 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
280 * We need to re-mark these list entries for deletion,
281 * so we pull them back into core and set zp->z_unlinked.
283 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
286 * We may pick up znodes that are already marked for deletion.
287 * This could happen during the purge of an extended attribute
288 * directory. All we need to do is skip over them, since they
289 * are already in the system marked z_unlinked.
294 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
295 zp->z_unlinked = B_TRUE;
298 zap_cursor_fini(&zc);
302 * Delete the entire contents of a directory. Return a count
303 * of the number of entries that could not be deleted. If we encounter
304 * an error, return a count of at least one so that the directory stays
305 * in the unlinked set.
307 * NOTE: this function assumes that the directory is inactive,
308 * so there is no need to lock its entries before deletion.
309 * Also, it assumes the directory contents is *only* regular
313 zfs_purgedir(znode_t *dzp)
319 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
323 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
324 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
325 zap_cursor_advance(&zc)) {
326 error = zfs_zget(zfsvfs,
327 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
333 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
334 ASSERT((ZTOV(xzp)->v_type == VREG) ||
335 (ZTOV(xzp)->v_type == VLNK));
337 tx = dmu_tx_create(zfsvfs->z_os);
338 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
339 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
340 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
341 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
342 /* Is this really needed ? */
343 zfs_sa_upgrade_txholds(tx, xzp);
344 dmu_tx_mark_netfree(tx);
345 error = dmu_tx_assign(tx, TXG_WAIT);
353 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
360 zap_cursor_fini(&zc);
367 zfs_rmnode(znode_t *zp)
369 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
370 objset_t *os = zfsvfs->z_os;
377 ASSERT(zp->z_links == 0);
378 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
381 * If this is an attribute directory, purge its contents.
383 if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
384 (zp->z_pflags & ZFS_XATTR)) {
385 if (zfs_purgedir(zp) != 0) {
387 * Not enough space to delete some xattrs.
388 * Leave it in the unlinked set.
390 zfs_znode_dmu_fini(zp);
397 * Free up all the data in the file.
399 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
402 * Not enough space. Leave the file in the unlinked set.
404 zfs_znode_dmu_fini(zp);
410 * If the file has extended attributes, we're going to unlink
413 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
414 &xattr_obj, sizeof (xattr_obj));
415 if (error == 0 && xattr_obj) {
416 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
417 ASSERT3S(error, ==, 0);
418 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
421 acl_obj = zfs_external_acl(zp);
424 * Set up the final transaction.
426 tx = dmu_tx_create(os);
427 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
428 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
430 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
431 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
434 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
436 zfs_sa_upgrade_txholds(tx, zp);
437 error = dmu_tx_assign(tx, TXG_WAIT);
440 * Not enough space to delete the file. Leave it in the
441 * unlinked set, leaking it until the fs is remounted (at
442 * which point we'll call zfs_unlinked_drain() to process it).
445 zfs_znode_dmu_fini(zp);
452 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
453 xzp->z_links = 0; /* no more links to it */
454 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
455 &xzp->z_links, sizeof (xzp->z_links), tx));
456 zfs_unlinked_add(xzp, tx);
459 /* Remove this znode from the unlinked set */
461 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
463 zfs_znode_delete(zp, tx);
472 zfs_dirent(znode_t *zp, uint64_t mode)
474 uint64_t de = zp->z_id;
476 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
477 de |= IFTODT(mode) << 60;
482 * Link zp into dzp. Can only fail if zp has been unlinked.
485 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
488 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
489 vnode_t *vp = ZTOV(zp);
491 int zp_is_dir = (vp->v_type == VDIR);
492 sa_bulk_attr_t bulk[5];
493 uint64_t mtime[2], ctime[2];
497 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
498 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
502 if (dzp->z_links >= LINK_MAX)
503 error = SET_ERROR(EMLINK);
507 if (!(flag & ZRENAMING)) {
508 if (zp->z_unlinked) { /* no new links to unlinked zp */
509 ASSERT(!(flag & (ZNEW | ZEXISTS)));
510 return (SET_ERROR(ENOENT));
513 if (zp->z_links >= LINK_MAX) {
514 return (SET_ERROR(EMLINK));
518 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
519 &zp->z_links, sizeof (zp->z_links));
522 ASSERT(zp->z_unlinked == 0);
524 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
525 &dzp->z_id, sizeof (dzp->z_id));
526 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
527 &zp->z_pflags, sizeof (zp->z_pflags));
529 if (!(flag & ZNEW)) {
530 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
531 ctime, sizeof (ctime));
532 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
535 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
539 dzp->z_links += zp_is_dir;
541 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
542 &dzp->z_size, sizeof (dzp->z_size));
543 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
544 &dzp->z_links, sizeof (dzp->z_links));
545 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
546 mtime, sizeof (mtime));
547 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
548 ctime, sizeof (ctime));
549 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
550 &dzp->z_pflags, sizeof (dzp->z_pflags));
551 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
552 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
555 value = zfs_dirent(zp, zp->z_mode);
556 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
564 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
569 if (zp->z_zfsvfs->z_norm) {
570 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED)
571 error = zap_remove_norm(zp->z_zfsvfs->z_os,
572 dzp->z_id, name, MT_EXACT, tx);
574 error = zap_remove_norm(zp->z_zfsvfs->z_os,
575 dzp->z_id, name, MT_FIRST, tx);
577 error = zap_remove(zp->z_zfsvfs->z_os,
578 dzp->z_id, name, tx);
585 * Unlink zp from dzp, and mark zp for deletion if this was the last link.
586 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
587 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
588 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
589 * and it's the caller's job to do it.
592 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
593 int flag, boolean_t *unlinkedp)
595 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
596 vnode_t *vp = ZTOV(zp);
597 int zp_is_dir = (vp->v_type == VDIR);
598 boolean_t unlinked = B_FALSE;
599 sa_bulk_attr_t bulk[5];
600 uint64_t mtime[2], ctime[2];
604 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
605 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
607 if (!(flag & ZRENAMING)) {
609 if (zp_is_dir && !zfs_dirempty(zp)) {
611 return (SET_ERROR(EEXIST));
613 return (SET_ERROR(ENOTEMPTY));
618 * If we get here, we are going to try to remove the object.
619 * First try removing the name from the directory; if that
620 * fails, return the error.
622 error = zfs_dropname(dzp, name, zp, tx, flag);
627 if (zp->z_links <= zp_is_dir) {
628 zfs_panic_recover("zfs: link count on vnode %p is %u, "
629 "should be at least %u", zp->z_vnode,
632 zp->z_links = zp_is_dir + 1;
634 if (--zp->z_links == zp_is_dir) {
635 zp->z_unlinked = B_TRUE;
639 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
640 NULL, &ctime, sizeof (ctime));
641 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
642 NULL, &zp->z_pflags, sizeof (zp->z_pflags));
643 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
646 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
647 NULL, &zp->z_links, sizeof (zp->z_links));
648 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
652 ASSERT(zp->z_unlinked == 0);
653 error = zfs_dropname(dzp, name, zp, tx, flag);
658 dzp->z_size--; /* one dirent removed */
659 dzp->z_links -= zp_is_dir; /* ".." link from zp */
660 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
661 NULL, &dzp->z_links, sizeof (dzp->z_links));
662 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
663 NULL, &dzp->z_size, sizeof (dzp->z_size));
664 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
665 NULL, ctime, sizeof (ctime));
666 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
667 NULL, mtime, sizeof (mtime));
668 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
669 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
670 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
671 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
674 if (unlinkedp != NULL)
675 *unlinkedp = unlinked;
677 zfs_unlinked_add(zp, tx);
683 * Indicate whether the directory is empty.
686 zfs_dirempty(znode_t *dzp)
688 return (dzp->z_size == 2);
692 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
694 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
698 zfs_acl_ids_t acl_ids;
699 boolean_t fuid_dirtied;
705 * In FreeBSD, access checking for creating an EA is being done
706 * in zfs_setextattr(),
708 #ifndef __FreeBSD_kernel__
709 if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
713 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
716 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
717 zfs_acl_ids_free(&acl_ids);
718 return (SET_ERROR(EDQUOT));
721 getnewvnode_reserve(1);
723 tx = dmu_tx_create(zfsvfs->z_os);
724 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
725 ZFS_SA_BASE_ATTR_SIZE);
726 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
727 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
728 fuid_dirtied = zfsvfs->z_fuid_dirty;
730 zfs_fuid_txhold(zfsvfs, tx);
731 error = dmu_tx_assign(tx, TXG_WAIT);
733 zfs_acl_ids_free(&acl_ids);
737 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
740 zfs_fuid_sync(zfsvfs, tx);
743 error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
744 &parent, sizeof (parent));
745 ASSERT(error == 0 && parent == zp->z_id);
748 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
749 sizeof (xzp->z_id), tx));
751 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
752 xzp, "", NULL, acl_ids.z_fuidp, vap);
754 zfs_acl_ids_free(&acl_ids);
757 getnewvnode_drop_reserve();
765 * Return a znode for the extended attribute directory for zp.
766 * ** If the directory does not already exist, it is created **
768 * IN: zp - znode to obtain attribute directory from
769 * cr - credentials of caller
770 * flags - flags from the VOP_LOOKUP call
772 * OUT: xzpp - pointer to extended attribute znode
774 * RETURN: 0 on success
775 * error number on failure
778 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
780 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
785 error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
795 if (!(flags & CREATE_XATTR_DIR)) {
797 return (SET_ERROR(ENOENT));
799 return (SET_ERROR(ENOATTR));
803 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
804 return (SET_ERROR(EROFS));
808 * The ability to 'create' files in an attribute
809 * directory comes from the write_xattr permission on the base file.
811 * The ability to 'search' an attribute directory requires
812 * read_xattr permission on the base file.
814 * Once in a directory the ability to read/write attributes
815 * is controlled by the permissions on the attribute file.
817 va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
819 va.va_mode = S_IFDIR | S_ISVTX | 0777;
820 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
822 error = zfs_make_xattrdir(zp, &va, xvpp, cr);
824 if (error == ERESTART) {
825 /* NB: we already did dmu_tx_wait() if necessary */
829 VOP_UNLOCK(*xvpp, 0);
835 * Decide whether it is okay to remove within a sticky directory.
837 * In sticky directories, write access is not sufficient;
838 * you can remove entries from a directory only if:
840 * you own the directory,
842 * the entry is a plain file and you have write access,
843 * or you are privileged (checked in secpolicy...).
845 * The function returns 0 if remove access is granted.
848 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
853 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
855 if (zdp->z_zfsvfs->z_replay)
858 if ((zdp->z_mode & S_ISVTX) == 0)
861 downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
862 fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
864 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
865 (ZTOV(zp)->v_type == VREG &&
866 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
869 return (secpolicy_vnode_remove(ZTOV(zp), cr));