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 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)zfs_dir.c 1.25 08/04/27 SMI"
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>
40 #include <sys/pathname.h>
41 #include <sys/cmn_err.h>
42 #include <sys/errno.h>
44 #include <sys/unistd.h>
45 #include <sys/sunddi.h>
46 #include <sys/random.h>
47 #include <sys/policy.h>
48 #include <sys/zfs_dir.h>
49 #include <sys/zfs_acl.h>
50 #include <sys/fs/zfs.h>
51 #include "fs/fs_subr.h"
54 #include <sys/atomic.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/zfs_fuid.h>
58 #include <sys/extdirent.h>
61 * zfs_match_find() is used by zfs_dirent_lock() 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
, char *name
, boolean_t exact
,
66 boolean_t update
, int *deflags
, pathname_t
*rpnp
, uint64_t *zoid
)
71 matchtype_t mt
= MT_FIRST
;
72 boolean_t conflict
= B_FALSE
;
78 bufsz
= rpnp
->pn_bufsize
;
83 * In the non-mixed case we only expect there would ever
84 * be one match, but we need to use the normalizing lookup.
86 error
= zap_lookup_norm(zfsvfs
->z_os
, dzp
->z_id
, name
, 8, 1,
87 zoid
, mt
, buf
, bufsz
, &conflict
);
88 if (!error
&& deflags
)
89 *deflags
= conflict
? ED_CASE_CONFLICT
: 0;
91 error
= zap_lookup(zfsvfs
->z_os
, dzp
->z_id
, name
, 8, 1, zoid
);
93 *zoid
= ZFS_DIRENT_OBJ(*zoid
);
95 if (error
== ENOENT
&& update
)
96 dnlc_update(ZTOV(dzp
), name
, DNLC_NO_VNODE
);
102 * Lock a directory entry. A dirlock on <dzp, name> protects that name
103 * in dzp's directory zap object. As long as you hold a dirlock, you can
104 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
105 * can change the zap entry for (i.e. link or unlink) this name.
108 * dzp - znode for directory
109 * name - name of entry to lock
110 * flag - ZNEW: if the entry already exists, fail with EEXIST.
111 * ZEXISTS: if the entry does not exist, fail with ENOENT.
112 * ZSHARED: allow concurrent access with other ZSHARED callers.
113 * ZXATTR: we want dzp's xattr directory
114 * ZCILOOK: On a mixed sensitivity file system,
115 * this lookup should be case-insensitive.
116 * ZCIEXACT: On a purely case-insensitive file system,
117 * this lookup should be case-sensitive.
118 * ZRENAMING: we are locking for renaming, force narrow locks
121 * zpp - pointer to the znode for the entry (NULL if there isn't one)
122 * dlpp - pointer to the dirlock for this entry (NULL on error)
123 * direntflags - (case-insensitive lookup only)
124 * flags if multiple case-sensitive matches exist in directory
125 * realpnp - (case-insensitive lookup only)
126 * actual name matched within the directory
128 * Return value: 0 on success or errno on failure.
130 * NOTE: Always checks for, and rejects, '.' and '..'.
131 * NOTE: For case-insensitive file systems we take wide locks (see below),
132 * but return znode pointers to a single match.
135 zfs_dirent_lock(zfs_dirlock_t
**dlpp
, znode_t
*dzp
, char *name
, znode_t
**zpp
,
136 int flag
, int *direntflags
, pathname_t
*realpnp
)
138 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
151 * Verify that we are not trying to lock '.', '..', or '.zfs'
153 if (name
[0] == '.' &&
154 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0')) ||
155 zfs_has_ctldir(dzp
) && strcmp(name
, ZFS_CTLDIR_NAME
) == 0)
159 * Case sensitivity and normalization preferences are set when
160 * the file system is created. These are stored in the
161 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
162 * affect what vnodes can be cached in the DNLC, how we
163 * perform zap lookups, and the "width" of our dirlocks.
165 * A normal dirlock locks a single name. Note that with
166 * normalization a name can be composed multiple ways, but
167 * when normalized, these names all compare equal. A wide
168 * dirlock locks multiple names. We need these when the file
169 * system is supporting mixed-mode access. It is sometimes
170 * necessary to lock all case permutations of file name at
171 * once so that simultaneous case-insensitive/case-sensitive
172 * behaves as rationally as possible.
176 * Decide if exact matches should be requested when performing
177 * a zap lookup on file systems supporting case-insensitive
181 ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) && (flag
& ZCIEXACT
)) ||
182 ((zfsvfs
->z_case
== ZFS_CASE_MIXED
) && !(flag
& ZCILOOK
));
185 * Only look in or update the DNLC if we are looking for the
186 * name on a file system that does not require normalization
187 * or case folding. We can also look there if we happen to be
188 * on a non-normalizing, mixed sensitivity file system IF we
189 * are looking for the exact name.
191 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
192 * case for performance improvement?
194 update
= !zfsvfs
->z_norm
||
195 ((zfsvfs
->z_case
== ZFS_CASE_MIXED
) &&
196 !(zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
) && !(flag
& ZCILOOK
));
199 * ZRENAMING indicates we are in a situation where we should
200 * take narrow locks regardless of the file system's
201 * preferences for normalizing and case folding. This will
202 * prevent us deadlocking trying to grab the same wide lock
203 * twice if the two names happen to be case-insensitive
206 if (flag
& ZRENAMING
)
209 cmpflags
= zfsvfs
->z_norm
;
212 * Wait until there are no locks on this name.
214 rw_enter(&dzp
->z_name_lock
, RW_READER
);
215 mutex_enter(&dzp
->z_lock
);
217 if (dzp
->z_unlinked
) {
218 mutex_exit(&dzp
->z_lock
);
219 rw_exit(&dzp
->z_name_lock
);
222 for (dl
= dzp
->z_dirlocks
; dl
!= NULL
; dl
= dl
->dl_next
) {
223 if ((u8_strcmp(name
, dl
->dl_name
, 0, cmpflags
,
224 U8_UNICODE_LATEST
, &error
) == 0) || error
!= 0)
228 mutex_exit(&dzp
->z_lock
);
229 rw_exit(&dzp
->z_name_lock
);
234 * Allocate a new dirlock and add it to the list.
236 dl
= kmem_alloc(sizeof (zfs_dirlock_t
), KM_SLEEP
);
237 cv_init(&dl
->dl_cv
, NULL
, CV_DEFAULT
, NULL
);
242 dl
->dl_next
= dzp
->z_dirlocks
;
243 dzp
->z_dirlocks
= dl
;
246 if ((flag
& ZSHARED
) && dl
->dl_sharecnt
!= 0)
248 cv_wait(&dl
->dl_cv
, &dzp
->z_lock
);
251 if ((flag
& ZSHARED
) && ++dl
->dl_sharecnt
> 1 && dl
->dl_namesize
== 0) {
253 * We're the second shared reference to dl. Make a copy of
254 * dl_name in case the first thread goes away before we do.
255 * Note that we initialize the new name before storing its
256 * pointer into dl_name, because the first thread may load
257 * dl->dl_name at any time. He'll either see the old value,
258 * which is his, or the new shared copy; either is OK.
260 dl
->dl_namesize
= strlen(dl
->dl_name
) + 1;
261 name
= kmem_alloc(dl
->dl_namesize
, KM_SLEEP
);
262 bcopy(dl
->dl_name
, name
, dl
->dl_namesize
);
266 mutex_exit(&dzp
->z_lock
);
269 * We have a dirlock on the name. (Note that it is the dirlock,
270 * not the dzp's z_lock, that protects the name in the zap object.)
271 * See if there's an object by this name; if so, put a hold on it.
274 zoid
= dzp
->z_phys
->zp_xattr
;
275 error
= (zoid
== 0 ? ENOENT
: 0);
278 vp
= dnlc_lookup(ZTOV(dzp
), name
);
279 if (vp
== DNLC_NO_VNODE
) {
284 zfs_dirent_unlock(dl
);
292 error
= zfs_match_find(zfsvfs
, dzp
, name
, exact
,
293 update
, direntflags
, realpnp
, &zoid
);
297 if (error
!= ENOENT
|| (flag
& ZEXISTS
)) {
298 zfs_dirent_unlock(dl
);
303 zfs_dirent_unlock(dl
);
306 error
= zfs_zget(zfsvfs
, zoid
, zpp
);
308 zfs_dirent_unlock(dl
);
311 if (!(flag
& ZXATTR
) && update
)
312 dnlc_update(ZTOV(dzp
), name
, ZTOV(*zpp
));
321 * Unlock this directory entry and wake anyone who was waiting for it.
324 zfs_dirent_unlock(zfs_dirlock_t
*dl
)
326 znode_t
*dzp
= dl
->dl_dzp
;
327 zfs_dirlock_t
**prev_dl
, *cur_dl
;
329 mutex_enter(&dzp
->z_lock
);
330 rw_exit(&dzp
->z_name_lock
);
331 if (dl
->dl_sharecnt
> 1) {
333 mutex_exit(&dzp
->z_lock
);
336 prev_dl
= &dzp
->z_dirlocks
;
337 while ((cur_dl
= *prev_dl
) != dl
)
338 prev_dl
= &cur_dl
->dl_next
;
339 *prev_dl
= dl
->dl_next
;
340 cv_broadcast(&dl
->dl_cv
);
341 mutex_exit(&dzp
->z_lock
);
343 if (dl
->dl_namesize
!= 0)
344 kmem_free(dl
->dl_name
, dl
->dl_namesize
);
345 cv_destroy(&dl
->dl_cv
);
346 kmem_free(dl
, sizeof (*dl
));
350 * Look up an entry in a directory.
352 * NOTE: '.' and '..' are handled as special cases because
353 * no directory entries are actually stored for them. If this is
354 * the root of a filesystem, then '.zfs' is also treated as a
355 * special pseudo-directory.
358 zfs_dirlook(znode_t
*dzp
, char *name
, vnode_t
**vpp
, int flags
,
359 int *deflg
, pathname_t
*rpnp
)
365 if (name
[0] == 0 || (name
[0] == '.' && name
[1] == 0)) {
368 } else if (name
[0] == '.' && name
[1] == '.' && name
[2] == 0) {
369 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
371 * If we are a snapshot mounted under .zfs, return
372 * the vp for the snapshot directory.
374 if (dzp
->z_phys
->zp_parent
== dzp
->z_id
&&
375 zfsvfs
->z_parent
!= zfsvfs
) {
376 error
= zfsctl_root_lookup(zfsvfs
->z_parent
->z_ctldir
,
377 "snapshot", vpp
, NULL
, 0, NULL
, kcred
,
381 rw_enter(&dzp
->z_parent_lock
, RW_READER
);
382 error
= zfs_zget(zfsvfs
, dzp
->z_phys
->zp_parent
, &zp
);
385 rw_exit(&dzp
->z_parent_lock
);
386 } else if (zfs_has_ctldir(dzp
) && strcmp(name
, ZFS_CTLDIR_NAME
) == 0) {
387 *vpp
= zfsctl_root(dzp
);
391 zf
= ZEXISTS
| ZSHARED
;
392 if (flags
& FIGNORECASE
)
395 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zf
, deflg
, rpnp
);
398 zfs_dirent_unlock(dl
);
399 dzp
->z_zn_prefetch
= B_TRUE
; /* enable prefetching */
404 if ((flags
& FIGNORECASE
) && rpnp
&& !error
)
405 (void) strlcpy(rpnp
->pn_buf
, name
, rpnp
->pn_bufsize
);
411 zfs_unlinked_hexname(char namebuf
[17], uint64_t x
)
413 char *name
= &namebuf
[16];
414 const char digits
[16] = "0123456789abcdef";
418 *--name
= digits
[x
& 0xf];
426 * unlinked Set (formerly known as the "delete queue") Error Handling
428 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
429 * don't specify the name of the entry that we will be manipulating. We
430 * also fib and say that we won't be adding any new entries to the
431 * unlinked set, even though we might (this is to lower the minimum file
432 * size that can be deleted in a full filesystem). So on the small
433 * chance that the nlink list is using a fat zap (ie. has more than
434 * 2000 entries), we *may* not pre-read a block that's needed.
435 * Therefore it is remotely possible for some of the assertions
436 * regarding the unlinked set below to fail due to i/o error. On a
437 * nondebug system, this will result in the space being leaked.
440 zfs_unlinked_add(znode_t
*zp
, dmu_tx_t
*tx
)
442 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
446 ASSERT(zp
->z_unlinked
);
447 ASSERT3U(zp
->z_phys
->zp_links
, ==, 0);
449 error
= zap_add(zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
,
450 zfs_unlinked_hexname(obj_name
, zp
->z_id
), 8, 1, &zp
->z_id
, tx
);
451 ASSERT3U(error
, ==, 0);
455 * Clean up any znodes that had no links when we either crashed or
456 * (force) umounted the file system.
459 zfs_unlinked_drain(zfsvfs_t
*zfsvfs
)
463 dmu_object_info_t doi
;
468 * Interate over the contents of the unlinked set.
470 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
);
471 zap_cursor_retrieve(&zc
, &zap
) == 0;
472 zap_cursor_advance(&zc
)) {
475 * See what kind of object we have in list
478 error
= dmu_object_info(zfsvfs
->z_os
,
479 zap
.za_first_integer
, &doi
);
483 ASSERT((doi
.doi_type
== DMU_OT_PLAIN_FILE_CONTENTS
) ||
484 (doi
.doi_type
== DMU_OT_DIRECTORY_CONTENTS
));
486 * We need to re-mark these list entries for deletion,
487 * so we pull them back into core and set zp->z_unlinked.
489 error
= zfs_zget(zfsvfs
, zap
.za_first_integer
, &zp
);
492 * We may pick up znodes that are already marked for deletion.
493 * This could happen during the purge of an extended attribute
494 * directory. All we need to do is skip over them, since they
495 * are already in the system marked z_unlinked.
500 zp
->z_unlinked
= B_TRUE
;
503 zap_cursor_fini(&zc
);
507 * Delete the entire contents of a directory. Return a count
508 * of the number of entries that could not be deleted. If we encounter
509 * an error, return a count of at least one so that the directory stays
510 * in the unlinked set.
512 * NOTE: this function assumes that the directory is inactive,
513 * so there is no need to lock its entries before deletion.
514 * Also, it assumes the directory contents is *only* regular
518 zfs_purgedir(znode_t
*dzp
)
524 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
529 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
530 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
531 zap_cursor_advance(&zc
)) {
532 error
= zfs_zget(zfsvfs
,
533 ZFS_DIRENT_OBJ(zap
.za_first_integer
), &xzp
);
539 ASSERT((ZTOV(xzp
)->v_type
== VREG
) ||
540 (ZTOV(xzp
)->v_type
== VLNK
));
542 tx
= dmu_tx_create(zfsvfs
->z_os
);
543 dmu_tx_hold_bonus(tx
, dzp
->z_id
);
544 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, zap
.za_name
);
545 dmu_tx_hold_bonus(tx
, xzp
->z_id
);
546 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
547 error
= dmu_tx_assign(tx
, TXG_WAIT
);
554 bzero(&dl
, sizeof (dl
));
556 dl
.dl_name
= zap
.za_name
;
558 error
= zfs_link_destroy(&dl
, xzp
, tx
, 0, NULL
);
565 zap_cursor_fini(&zc
);
572 zfs_rmnode(znode_t
*zp
)
574 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
575 objset_t
*os
= zfsvfs
->z_os
;
582 ASSERT(ZTOV(zp
)->v_count
== 0);
583 ASSERT(zp
->z_phys
->zp_links
== 0);
586 * If this is an attribute directory, purge its contents.
588 if (ZTOV(zp
)->v_type
== VDIR
&& (zp
->z_phys
->zp_flags
& ZFS_XATTR
)) {
589 if (zfs_purgedir(zp
) != 0) {
591 * Not enough space to delete some xattrs.
592 * Leave it on the unlinked set.
594 zfs_znode_dmu_fini(zp
);
601 * If the file has extended attributes, we're going to unlink
604 if (zp
->z_phys
->zp_xattr
) {
605 error
= zfs_zget(zfsvfs
, zp
->z_phys
->zp_xattr
, &xzp
);
609 acl_obj
= zp
->z_phys
->zp_acl
.z_acl_extern_obj
;
612 * Set up the transaction.
614 tx
= dmu_tx_create(os
);
615 dmu_tx_hold_free(tx
, zp
->z_id
, 0, DMU_OBJECT_END
);
616 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
618 dmu_tx_hold_bonus(tx
, xzp
->z_id
);
619 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, TRUE
, NULL
);
622 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
623 error
= dmu_tx_assign(tx
, TXG_WAIT
);
626 * Not enough space to delete the file. Leave it in the
627 * unlinked set, leaking it until the fs is remounted (at
628 * which point we'll call zfs_unlinked_drain() to process it).
631 zfs_znode_dmu_fini(zp
);
637 dmu_buf_will_dirty(xzp
->z_dbuf
, tx
);
638 mutex_enter(&xzp
->z_lock
);
639 xzp
->z_unlinked
= B_TRUE
; /* mark xzp for deletion */
640 xzp
->z_phys
->zp_links
= 0; /* no more links to it */
641 mutex_exit(&xzp
->z_lock
);
642 zfs_unlinked_add(xzp
, tx
);
645 /* Remove this znode from the unlinked set */
646 error
= zap_remove(os
, zfsvfs
->z_unlinkedobj
,
647 zfs_unlinked_hexname(obj_name
, zp
->z_id
), tx
);
648 ASSERT3U(error
, ==, 0);
650 zfs_znode_delete(zp
, tx
);
659 zfs_dirent(znode_t
*zp
)
661 uint64_t de
= zp
->z_id
;
662 if (zp
->z_zfsvfs
->z_version
>= ZPL_VERSION_DIRENT_TYPE
)
663 de
|= IFTODT((zp
)->z_phys
->zp_mode
) << 60;
668 * Link zp into dl. Can only fail if zp has been unlinked.
671 zfs_link_create(zfs_dirlock_t
*dl
, znode_t
*zp
, dmu_tx_t
*tx
, int flag
)
673 znode_t
*dzp
= dl
->dl_dzp
;
674 vnode_t
*vp
= ZTOV(zp
);
676 int zp_is_dir
= (vp
->v_type
== VDIR
);
679 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
680 mutex_enter(&zp
->z_lock
);
682 if (!(flag
& ZRENAMING
)) {
683 if (zp
->z_unlinked
) { /* no new links to unlinked zp */
684 ASSERT(!(flag
& (ZNEW
| ZEXISTS
)));
685 mutex_exit(&zp
->z_lock
);
688 zp
->z_phys
->zp_links
++;
690 zp
->z_phys
->zp_parent
= dzp
->z_id
; /* dzp is now zp's parent */
693 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
694 mutex_exit(&zp
->z_lock
);
696 dmu_buf_will_dirty(dzp
->z_dbuf
, tx
);
697 mutex_enter(&dzp
->z_lock
);
698 dzp
->z_phys
->zp_size
++; /* one dirent added */
699 dzp
->z_phys
->zp_links
+= zp_is_dir
; /* ".." link from zp */
700 zfs_time_stamper_locked(dzp
, CONTENT_MODIFIED
, tx
);
701 mutex_exit(&dzp
->z_lock
);
703 value
= zfs_dirent(zp
);
704 error
= zap_add(zp
->z_zfsvfs
->z_os
, dzp
->z_id
, dl
->dl_name
,
708 dnlc_update(ZTOV(dzp
), dl
->dl_name
, vp
);
714 * Unlink zp from dl, and mark zp for deletion if this was the last link.
715 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
716 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
717 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
718 * and it's the caller's job to do it.
721 zfs_link_destroy(zfs_dirlock_t
*dl
, znode_t
*zp
, dmu_tx_t
*tx
, int flag
,
722 boolean_t
*unlinkedp
)
724 znode_t
*dzp
= dl
->dl_dzp
;
725 vnode_t
*vp
= ZTOV(zp
);
726 int zp_is_dir
= (vp
->v_type
== VDIR
);
727 boolean_t unlinked
= B_FALSE
;
730 dnlc_remove(ZTOV(dzp
), dl
->dl_name
);
732 if (!(flag
& ZRENAMING
)) {
733 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
735 if (vn_vfswlock(vp
)) /* prevent new mounts on zp */
738 if (vn_ismntpt(vp
)) { /* don't remove mount point */
743 mutex_enter(&zp
->z_lock
);
744 if (zp_is_dir
&& !zfs_dirempty(zp
)) { /* dir not empty */
745 mutex_exit(&zp
->z_lock
);
749 if (zp
->z_phys
->zp_links
<= zp_is_dir
) {
750 zfs_panic_recover("zfs: link count on %s is %u, "
751 "should be at least %u",
752 zp
->z_vnode
->v_path
? zp
->z_vnode
->v_path
:
753 "<unknown>", (int)zp
->z_phys
->zp_links
,
755 zp
->z_phys
->zp_links
= zp_is_dir
+ 1;
757 if (--zp
->z_phys
->zp_links
== zp_is_dir
) {
758 zp
->z_unlinked
= B_TRUE
;
759 zp
->z_phys
->zp_links
= 0;
762 zfs_time_stamper_locked(zp
, STATE_CHANGED
, tx
);
764 mutex_exit(&zp
->z_lock
);
768 dmu_buf_will_dirty(dzp
->z_dbuf
, tx
);
769 mutex_enter(&dzp
->z_lock
);
770 dzp
->z_phys
->zp_size
--; /* one dirent removed */
771 dzp
->z_phys
->zp_links
-= zp_is_dir
; /* ".." link from zp */
772 zfs_time_stamper_locked(dzp
, CONTENT_MODIFIED
, tx
);
773 mutex_exit(&dzp
->z_lock
);
775 if (zp
->z_zfsvfs
->z_norm
) {
776 if (((zp
->z_zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
) &&
777 (flag
& ZCIEXACT
)) ||
778 ((zp
->z_zfsvfs
->z_case
== ZFS_CASE_MIXED
) &&
780 error
= zap_remove_norm(zp
->z_zfsvfs
->z_os
,
781 dzp
->z_id
, dl
->dl_name
, MT_EXACT
, tx
);
783 error
= zap_remove_norm(zp
->z_zfsvfs
->z_os
,
784 dzp
->z_id
, dl
->dl_name
, MT_FIRST
, tx
);
786 error
= zap_remove(zp
->z_zfsvfs
->z_os
,
787 dzp
->z_id
, dl
->dl_name
, tx
);
791 if (unlinkedp
!= NULL
)
792 *unlinkedp
= unlinked
;
794 zfs_unlinked_add(zp
, tx
);
800 * Indicate whether the directory is empty. Works with or without z_lock
801 * held, but can only be consider a hint in the latter case. Returns true
802 * if only "." and ".." remain and there's no work in progress.
805 zfs_dirempty(znode_t
*dzp
)
807 return (dzp
->z_phys
->zp_size
== 2 && dzp
->z_dirlocks
== 0);
811 zfs_make_xattrdir(znode_t
*zp
, vattr_t
*vap
, vnode_t
**xvpp
, cred_t
*cr
)
813 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
817 zfs_fuid_info_t
*fuidp
= NULL
;
821 if (error
= zfs_zaccess(zp
, ACE_WRITE_NAMED_ATTRS
, 0, B_FALSE
, cr
))
824 tx
= dmu_tx_create(zfsvfs
->z_os
);
825 dmu_tx_hold_bonus(tx
, zp
->z_id
);
826 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
827 if (IS_EPHEMERAL(crgetuid(cr
)) || IS_EPHEMERAL(crgetgid(cr
))) {
828 if (zfsvfs
->z_fuid_obj
== 0) {
829 dmu_tx_hold_bonus(tx
, DMU_NEW_OBJECT
);
830 dmu_tx_hold_write(tx
, DMU_NEW_OBJECT
, 0,
831 FUID_SIZE_ESTIMATE(zfsvfs
));
832 dmu_tx_hold_zap(tx
, MASTER_NODE_OBJ
, FALSE
, NULL
);
834 dmu_tx_hold_bonus(tx
, zfsvfs
->z_fuid_obj
);
835 dmu_tx_hold_write(tx
, zfsvfs
->z_fuid_obj
, 0,
836 FUID_SIZE_ESTIMATE(zfsvfs
));
839 error
= dmu_tx_assign(tx
, zfsvfs
->z_assign
);
841 if (error
== ERESTART
&& zfsvfs
->z_assign
== TXG_NOWAIT
)
846 zfs_mknode(zp
, vap
, tx
, cr
, IS_XATTR
, &xzp
, 0, NULL
, &fuidp
);
847 ASSERT(xzp
->z_phys
->zp_parent
== zp
->z_id
);
848 dmu_buf_will_dirty(zp
->z_dbuf
, tx
);
849 zp
->z_phys
->zp_xattr
= xzp
->z_id
;
851 (void) zfs_log_create(zfsvfs
->z_log
, tx
, TX_MKXATTR
, zp
,
852 xzp
, "", NULL
, fuidp
, vap
);
854 zfs_fuid_info_free(fuidp
);
863 * Return a znode for the extended attribute directory for zp.
864 * ** If the directory does not already exist, it is created **
866 * IN: zp - znode to obtain attribute directory from
867 * cr - credentials of caller
868 * flags - flags from the VOP_LOOKUP call
870 * OUT: xzpp - pointer to extended attribute znode
872 * RETURN: 0 on success
873 * error number on failure
876 zfs_get_xattrdir(znode_t
*zp
, vnode_t
**xvpp
, cred_t
*cr
, int flags
)
878 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
884 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
, ZXATTR
, NULL
, NULL
);
890 zfs_dirent_unlock(dl
);
894 ASSERT(zp
->z_phys
->zp_xattr
== 0);
896 if (!(flags
& CREATE_XATTR_DIR
)) {
897 zfs_dirent_unlock(dl
);
901 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
902 zfs_dirent_unlock(dl
);
907 * The ability to 'create' files in an attribute
908 * directory comes from the write_xattr permission on the base file.
910 * The ability to 'search' an attribute directory requires
911 * read_xattr permission on the base file.
913 * Once in a directory the ability to read/write attributes
914 * is controlled by the permissions on the attribute file.
916 va
.va_mask
= AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
;
918 va
.va_mode
= S_IFDIR
| S_ISVTX
| 0777;
919 zfs_fuid_map_ids(zp
, cr
, &va
.va_uid
, &va
.va_gid
);
921 error
= zfs_make_xattrdir(zp
, &va
, xvpp
, cr
);
922 zfs_dirent_unlock(dl
);
924 if (error
== ERESTART
&& zfsvfs
->z_assign
== TXG_NOWAIT
) {
925 /* NB: we already did dmu_tx_wait() if necessary */
933 * Decide whether it is okay to remove within a sticky directory.
935 * In sticky directories, write access is not sufficient;
936 * you can remove entries from a directory only if:
938 * you own the directory,
940 * the entry is a plain file and you have write access,
941 * or you are privileged (checked in secpolicy...).
943 * The function returns 0 if remove access is granted.
946 zfs_sticky_remove_access(znode_t
*zdp
, znode_t
*zp
, cred_t
*cr
)
951 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
953 if (zdp
->z_zfsvfs
->z_assign
>= TXG_INITIAL
) /* ZIL replay */
956 if ((zdp
->z_phys
->zp_mode
& S_ISVTX
) == 0)
959 downer
= zfs_fuid_map_id(zfsvfs
, zdp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
960 fowner
= zfs_fuid_map_id(zfsvfs
, zp
->z_phys
->zp_uid
, cr
, ZFS_OWNER
);
962 if ((uid
= crgetuid(cr
)) == downer
|| uid
== fowner
||
963 (ZTOV(zp
)->v_type
== VREG
&&
964 zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
) == 0))
967 return (secpolicy_vnode_remove(cr
));