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.
26 * Copyright (c) 2015, Joyent, Inc.
29 #include <sys/types.h>
30 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
36 #include <sys/vnode.h>
41 #include <sys/pathname.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
45 #include <sys/unistd.h>
46 #include <sys/sunddi.h>
47 #include <sys/random.h>
48 #include <sys/policy.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/fs/zfs.h>
52 #include "sys/fs_subr.h"
55 #include <sys/atomic.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/zfs_fuid.h>
59 #include <sys/zfs_sa.h>
61 #include <sys/extdirent.h>
64 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
65 * of names after deciding which is the appropriate lookup interface.
68 zfs_match_find(zfsvfs_t
*zfsvfs
, znode_t
*dzp
, char *name
, matchtype_t mt
,
69 boolean_t update
, int *deflags
, pathname_t
*rpnp
, uint64_t *zoid
)
74 boolean_t conflict
= B_FALSE
;
80 bufsz
= rpnp
->pn_bufsize
;
84 * In the non-mixed case we only expect there would ever
85 * be one match, but we need to use the normalizing lookup.
87 error
= zap_lookup_norm(zfsvfs
->z_os
, dzp
->z_id
, name
, 8, 1,
88 zoid
, mt
, buf
, bufsz
, &conflict
);
89 if (!error
&& deflags
)
90 *deflags
= conflict
? ED_CASE_CONFLICT
: 0;
92 error
= zap_lookup(zfsvfs
->z_os
, dzp
->z_id
, name
, 8, 1, zoid
);
94 *zoid
= ZFS_DIRENT_OBJ(*zoid
);
96 if (error
== ENOENT
&& update
)
97 dnlc_update(ZTOV(dzp
), name
, DNLC_NO_VNODE
);
103 * Lock a directory entry. A dirlock on <dzp, name> protects that name
104 * in dzp's directory zap object. As long as you hold a dirlock, you can
105 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
106 * can change the zap entry for (i.e. link or unlink) this name.
109 * dzp - znode for directory
110 * name - name of entry to lock
111 * flag - ZNEW: if the entry already exists, fail with EEXIST.
112 * ZEXISTS: if the entry does not exist, fail with ENOENT.
113 * ZSHARED: allow concurrent access with other ZSHARED callers.
114 * ZXATTR: we want dzp's xattr directory
115 * ZCILOOK: On a mixed sensitivity file system,
116 * this lookup should be case-insensitive.
117 * ZCIEXACT: On a purely case-insensitive file system,
118 * this lookup should be case-sensitive.
119 * ZRENAMING: we are locking for renaming, force narrow locks
120 * ZHAVELOCK: Don't grab the z_name_lock for this call. The
121 * current thread already holds it.
124 * zpp - pointer to the znode for the entry (NULL if there isn't one)
125 * dlpp - pointer to the dirlock for this entry (NULL on error)
126 * direntflags - (case-insensitive lookup only)
127 * flags if multiple case-sensitive matches exist in directory
128 * realpnp - (case-insensitive lookup only)
129 * actual name matched within the directory
131 * Return value: 0 on success or errno on failure.
133 * NOTE: Always checks for, and rejects, '.' and '..'.
134 * NOTE: For case-insensitive file systems we take wide locks (see below),
135 * but return znode pointers to a single match.
138 zfs_dirent_lock(zfs_dirlock_t
**dlpp
, znode_t
*dzp
, char *name
, znode_t
**zpp
,
139 int flag
, int *direntflags
, pathname_t
*realpnp
)
141 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
154 * Verify that we are not trying to lock '.', '..', or '.zfs'
156 if (name
[0] == '.' &&
157 (name
[1] == '\0' || (name
[1] == '.' && name
[2] == '\0')) ||
158 zfs_has_ctldir(dzp
) && strcmp(name
, ZFS_CTLDIR_NAME
) == 0)
159 return (SET_ERROR(EEXIST
));
162 * Case sensitivity and normalization preferences are set when
163 * the file system is created. These are stored in the
164 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
165 * affect what vnodes can be cached in the DNLC, how we
166 * perform zap lookups, and the "width" of our dirlocks.
168 * A normal dirlock locks a single name. Note that with
169 * normalization a name can be composed multiple ways, but
170 * when normalized, these names all compare equal. A wide
171 * dirlock locks multiple names. We need these when the file
172 * system is supporting mixed-mode access. It is sometimes
173 * necessary to lock all case permutations of file name at
174 * once so that simultaneous case-insensitive/case-sensitive
175 * behaves as rationally as possible.
179 * When matching we may need to normalize & change case according to
182 * Note that a normalized match is necessary for a case insensitive
183 * filesystem when the lookup request is not exact because normalization
184 * can fold case independent of normalizing code point sequences.
186 * See the table above zfs_dropname().
188 if (zfsvfs
->z_norm
!= 0) {
192 * Determine if the match needs to honor the case specified in
193 * lookup, and if so keep track of that so that during
194 * normalization we don't fold case.
196 if ((zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
&&
197 (flag
& ZCIEXACT
)) ||
198 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&& !(flag
& ZCILOOK
))) {
204 * Only look in or update the DNLC if we are looking for the
205 * name on a file system that does not require normalization
206 * or case folding. We can also look there if we happen to be
207 * on a non-normalizing, mixed sensitivity file system IF we
208 * are looking for the exact name.
210 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
211 * case for performance improvement?
213 update
= !zfsvfs
->z_norm
||
214 (zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
215 !(zfsvfs
->z_norm
& ~U8_TEXTPREP_TOUPPER
) && !(flag
& ZCILOOK
));
218 * ZRENAMING indicates we are in a situation where we should
219 * take narrow locks regardless of the file system's
220 * preferences for normalizing and case folding. This will
221 * prevent us deadlocking trying to grab the same wide lock
222 * twice if the two names happen to be case-insensitive
225 if (flag
& ZRENAMING
)
228 cmpflags
= zfsvfs
->z_norm
;
231 * Wait until there are no locks on this name.
233 * Don't grab the the lock if it is already held. However, cannot
234 * have both ZSHARED and ZHAVELOCK together.
236 ASSERT(!(flag
& ZSHARED
) || !(flag
& ZHAVELOCK
));
237 if (!(flag
& ZHAVELOCK
))
238 rw_enter(&dzp
->z_name_lock
, RW_READER
);
240 mutex_enter(&dzp
->z_lock
);
242 if (dzp
->z_unlinked
) {
243 mutex_exit(&dzp
->z_lock
);
244 if (!(flag
& ZHAVELOCK
))
245 rw_exit(&dzp
->z_name_lock
);
246 return (SET_ERROR(ENOENT
));
248 for (dl
= dzp
->z_dirlocks
; dl
!= NULL
; dl
= dl
->dl_next
) {
249 if ((u8_strcmp(name
, dl
->dl_name
, 0, cmpflags
,
250 U8_UNICODE_LATEST
, &error
) == 0) || error
!= 0)
254 mutex_exit(&dzp
->z_lock
);
255 if (!(flag
& ZHAVELOCK
))
256 rw_exit(&dzp
->z_name_lock
);
257 return (SET_ERROR(ENOENT
));
261 * Allocate a new dirlock and add it to the list.
263 dl
= kmem_alloc(sizeof (zfs_dirlock_t
), KM_SLEEP
);
264 cv_init(&dl
->dl_cv
, NULL
, CV_DEFAULT
, NULL
);
270 dl
->dl_next
= dzp
->z_dirlocks
;
271 dzp
->z_dirlocks
= dl
;
274 if ((flag
& ZSHARED
) && dl
->dl_sharecnt
!= 0)
276 cv_wait(&dl
->dl_cv
, &dzp
->z_lock
);
280 * If the z_name_lock was NOT held for this dirlock record it.
282 if (flag
& ZHAVELOCK
)
285 if ((flag
& ZSHARED
) && ++dl
->dl_sharecnt
> 1 && dl
->dl_namesize
== 0) {
287 * We're the second shared reference to dl. Make a copy of
288 * dl_name in case the first thread goes away before we do.
289 * Note that we initialize the new name before storing its
290 * pointer into dl_name, because the first thread may load
291 * dl->dl_name at any time. It'll either see the old value,
292 * which belongs to it, or the new shared copy; either is OK.
294 dl
->dl_namesize
= strlen(dl
->dl_name
) + 1;
295 name
= kmem_alloc(dl
->dl_namesize
, KM_SLEEP
);
296 bcopy(dl
->dl_name
, name
, dl
->dl_namesize
);
300 mutex_exit(&dzp
->z_lock
);
303 * We have a dirlock on the name. (Note that it is the dirlock,
304 * not the dzp's z_lock, that protects the name in the zap object.)
305 * See if there's an object by this name; if so, put a hold on it.
308 error
= sa_lookup(dzp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
), &zoid
,
311 error
= (zoid
== 0 ? ENOENT
: 0);
314 vp
= dnlc_lookup(ZTOV(dzp
), name
);
315 if (vp
== DNLC_NO_VNODE
) {
317 error
= SET_ERROR(ENOENT
);
320 zfs_dirent_unlock(dl
);
322 return (SET_ERROR(EEXIST
));
328 error
= zfs_match_find(zfsvfs
, dzp
, name
, mt
,
329 update
, direntflags
, realpnp
, &zoid
);
333 if (error
!= ENOENT
|| (flag
& ZEXISTS
)) {
334 zfs_dirent_unlock(dl
);
339 zfs_dirent_unlock(dl
);
340 return (SET_ERROR(EEXIST
));
342 error
= zfs_zget(zfsvfs
, zoid
, zpp
);
344 zfs_dirent_unlock(dl
);
347 if (!(flag
& ZXATTR
) && update
)
348 dnlc_update(ZTOV(dzp
), name
, ZTOV(*zpp
));
357 * Unlock this directory entry and wake anyone who was waiting for it.
360 zfs_dirent_unlock(zfs_dirlock_t
*dl
)
362 znode_t
*dzp
= dl
->dl_dzp
;
363 zfs_dirlock_t
**prev_dl
, *cur_dl
;
365 mutex_enter(&dzp
->z_lock
);
367 if (!dl
->dl_namelock
)
368 rw_exit(&dzp
->z_name_lock
);
370 if (dl
->dl_sharecnt
> 1) {
372 mutex_exit(&dzp
->z_lock
);
375 prev_dl
= &dzp
->z_dirlocks
;
376 while ((cur_dl
= *prev_dl
) != dl
)
377 prev_dl
= &cur_dl
->dl_next
;
378 *prev_dl
= dl
->dl_next
;
379 cv_broadcast(&dl
->dl_cv
);
380 mutex_exit(&dzp
->z_lock
);
382 if (dl
->dl_namesize
!= 0)
383 kmem_free(dl
->dl_name
, dl
->dl_namesize
);
384 cv_destroy(&dl
->dl_cv
);
385 kmem_free(dl
, sizeof (*dl
));
389 * Look up an entry in a directory.
391 * NOTE: '.' and '..' are handled as special cases because
392 * no directory entries are actually stored for them. If this is
393 * the root of a filesystem, then '.zfs' is also treated as a
394 * special pseudo-directory.
397 zfs_dirlook(znode_t
*dzp
, char *name
, vnode_t
**vpp
, int flags
,
398 int *deflg
, pathname_t
*rpnp
)
405 if (name
[0] == 0 || (name
[0] == '.' && name
[1] == 0)) {
408 } else if (name
[0] == '.' && name
[1] == '.' && name
[2] == 0) {
409 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
412 * If we are a snapshot mounted under .zfs, return
413 * the vp for the snapshot directory.
415 if ((error
= sa_lookup(dzp
->z_sa_hdl
,
416 SA_ZPL_PARENT(zfsvfs
), &parent
, sizeof (parent
))) != 0)
418 if (parent
== dzp
->z_id
&& zfsvfs
->z_parent
!= zfsvfs
) {
419 error
= zfsctl_root_lookup(zfsvfs
->z_parent
->z_ctldir
,
420 "snapshot", vpp
, NULL
, 0, NULL
, kcred
,
424 rw_enter(&dzp
->z_parent_lock
, RW_READER
);
425 error
= zfs_zget(zfsvfs
, parent
, &zp
);
428 rw_exit(&dzp
->z_parent_lock
);
429 } else if (zfs_has_ctldir(dzp
) && strcmp(name
, ZFS_CTLDIR_NAME
) == 0) {
430 *vpp
= zfsctl_root(dzp
);
434 zf
= ZEXISTS
| ZSHARED
;
435 if (flags
& FIGNORECASE
)
438 error
= zfs_dirent_lock(&dl
, dzp
, name
, &zp
, zf
, deflg
, rpnp
);
441 zfs_dirent_unlock(dl
);
442 dzp
->z_zn_prefetch
= B_TRUE
; /* enable prefetching */
447 if ((flags
& FIGNORECASE
) && rpnp
&& !error
)
448 (void) strlcpy(rpnp
->pn_buf
, name
, rpnp
->pn_bufsize
);
454 * unlinked Set (formerly known as the "delete queue") Error Handling
456 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
457 * don't specify the name of the entry that we will be manipulating. We
458 * also fib and say that we won't be adding any new entries to the
459 * unlinked set, even though we might (this is to lower the minimum file
460 * size that can be deleted in a full filesystem). So on the small
461 * chance that the nlink list is using a fat zap (ie. has more than
462 * 2000 entries), we *may* not pre-read a block that's needed.
463 * Therefore it is remotely possible for some of the assertions
464 * regarding the unlinked set below to fail due to i/o error. On a
465 * nondebug system, this will result in the space being leaked.
468 zfs_unlinked_add(znode_t
*zp
, dmu_tx_t
*tx
)
470 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
472 ASSERT(zp
->z_unlinked
);
473 ASSERT(zp
->z_links
== 0);
476 zap_add_int(zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
, zp
->z_id
, tx
));
480 * Clean up any znodes that had no links when we either crashed or
481 * (force) umounted the file system.
484 zfs_unlinked_drain(zfsvfs_t
*zfsvfs
)
488 dmu_object_info_t doi
;
493 * Interate over the contents of the unlinked set.
495 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
);
496 zap_cursor_retrieve(&zc
, &zap
) == 0;
497 zap_cursor_advance(&zc
)) {
500 * See what kind of object we have in list
503 error
= dmu_object_info(zfsvfs
->z_os
,
504 zap
.za_first_integer
, &doi
);
508 ASSERT((doi
.doi_type
== DMU_OT_PLAIN_FILE_CONTENTS
) ||
509 (doi
.doi_type
== DMU_OT_DIRECTORY_CONTENTS
));
511 * We need to re-mark these list entries for deletion,
512 * so we pull them back into core and set zp->z_unlinked.
514 error
= zfs_zget(zfsvfs
, zap
.za_first_integer
, &zp
);
517 * We may pick up znodes that are already marked for deletion.
518 * This could happen during the purge of an extended attribute
519 * directory. All we need to do is skip over them, since they
520 * are already in the system marked z_unlinked.
525 zp
->z_unlinked
= B_TRUE
;
528 zap_cursor_fini(&zc
);
532 * Delete the entire contents of a directory. Return a count
533 * of the number of entries that could not be deleted. If we encounter
534 * an error, return a count of at least one so that the directory stays
535 * in the unlinked set.
537 * NOTE: this function assumes that the directory is inactive,
538 * so there is no need to lock its entries before deletion.
539 * Also, it assumes the directory contents is *only* regular
543 zfs_purgedir(znode_t
*dzp
)
549 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
554 for (zap_cursor_init(&zc
, zfsvfs
->z_os
, dzp
->z_id
);
555 (error
= zap_cursor_retrieve(&zc
, &zap
)) == 0;
556 zap_cursor_advance(&zc
)) {
557 error
= zfs_zget(zfsvfs
,
558 ZFS_DIRENT_OBJ(zap
.za_first_integer
), &xzp
);
564 ASSERT((ZTOV(xzp
)->v_type
== VREG
) ||
565 (ZTOV(xzp
)->v_type
== VLNK
));
567 tx
= dmu_tx_create(zfsvfs
->z_os
);
568 dmu_tx_hold_sa(tx
, dzp
->z_sa_hdl
, B_FALSE
);
569 dmu_tx_hold_zap(tx
, dzp
->z_id
, FALSE
, zap
.za_name
);
570 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
571 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
572 /* Is this really needed ? */
573 zfs_sa_upgrade_txholds(tx
, xzp
);
574 dmu_tx_mark_netfree(tx
);
575 error
= dmu_tx_assign(tx
, TXG_WAIT
);
582 bzero(&dl
, sizeof (dl
));
584 dl
.dl_name
= zap
.za_name
;
586 error
= zfs_link_destroy(&dl
, xzp
, tx
, 0, NULL
);
593 zap_cursor_fini(&zc
);
600 zfs_rmnode(znode_t
*zp
)
602 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
603 objset_t
*os
= zfsvfs
->z_os
;
610 ASSERT(zp
->z_links
== 0);
611 ASSERT(ZTOV(zp
)->v_count
== 0);
614 * If this is an attribute directory, purge its contents.
616 if (ZTOV(zp
)->v_type
== VDIR
&& (zp
->z_pflags
& ZFS_XATTR
)) {
617 if (zfs_purgedir(zp
) != 0) {
619 * Not enough space to delete some xattrs.
620 * Leave it in the unlinked set.
622 zfs_znode_dmu_fini(zp
);
628 * Free up all the data in the file. We don't do this for
629 * XATTR directories because we need truncate and remove to be
630 * in the same tx, like in zfs_znode_delete(). Otherwise, if
631 * we crash here we'll end up with an inconsistent truncated
632 * zap object in the delete queue. Note a truncated file is
633 * harmless since it only contains user data.
635 error
= dmu_free_long_range(os
, zp
->z_id
, 0, DMU_OBJECT_END
);
638 * Not enough space or we were interrupted by unmount.
639 * Leave the file in the unlinked set.
641 zfs_znode_dmu_fini(zp
);
648 * If the file has extended attributes, we're going to unlink
651 error
= sa_lookup(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
),
652 &xattr_obj
, sizeof (xattr_obj
));
653 if (error
== 0 && xattr_obj
) {
654 error
= zfs_zget(zfsvfs
, xattr_obj
, &xzp
);
658 acl_obj
= zfs_external_acl(zp
);
661 * Set up the final transaction.
663 tx
= dmu_tx_create(os
);
664 dmu_tx_hold_free(tx
, zp
->z_id
, 0, DMU_OBJECT_END
);
665 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, FALSE
, NULL
);
667 dmu_tx_hold_zap(tx
, zfsvfs
->z_unlinkedobj
, TRUE
, NULL
);
668 dmu_tx_hold_sa(tx
, xzp
->z_sa_hdl
, B_FALSE
);
671 dmu_tx_hold_free(tx
, acl_obj
, 0, DMU_OBJECT_END
);
673 zfs_sa_upgrade_txholds(tx
, zp
);
674 error
= dmu_tx_assign(tx
, TXG_WAIT
);
677 * Not enough space to delete the file. Leave it in the
678 * unlinked set, leaking it until the fs is remounted (at
679 * which point we'll call zfs_unlinked_drain() to process it).
682 zfs_znode_dmu_fini(zp
);
689 mutex_enter(&xzp
->z_lock
);
690 xzp
->z_unlinked
= B_TRUE
; /* mark xzp for deletion */
691 xzp
->z_links
= 0; /* no more links to it */
692 VERIFY(0 == sa_update(xzp
->z_sa_hdl
, SA_ZPL_LINKS(zfsvfs
),
693 &xzp
->z_links
, sizeof (xzp
->z_links
), tx
));
694 mutex_exit(&xzp
->z_lock
);
695 zfs_unlinked_add(xzp
, tx
);
698 /* Remove this znode from the unlinked set */
700 zap_remove_int(zfsvfs
->z_os
, zfsvfs
->z_unlinkedobj
, zp
->z_id
, tx
));
702 zfs_znode_delete(zp
, tx
);
711 zfs_dirent(znode_t
*zp
, uint64_t mode
)
713 uint64_t de
= zp
->z_id
;
715 if (zp
->z_zfsvfs
->z_version
>= ZPL_VERSION_DIRENT_TYPE
)
716 de
|= IFTODT(mode
) << 60;
721 * Link zp into dl. Can only fail if zp has been unlinked.
724 zfs_link_create(zfs_dirlock_t
*dl
, znode_t
*zp
, dmu_tx_t
*tx
, int flag
)
726 znode_t
*dzp
= dl
->dl_dzp
;
727 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
728 vnode_t
*vp
= ZTOV(zp
);
730 int zp_is_dir
= (vp
->v_type
== VDIR
);
731 sa_bulk_attr_t bulk
[5];
732 uint64_t mtime
[2], ctime
[2];
736 mutex_enter(&zp
->z_lock
);
738 if (!(flag
& ZRENAMING
)) {
739 if (zp
->z_unlinked
) { /* no new links to unlinked zp */
740 ASSERT(!(flag
& (ZNEW
| ZEXISTS
)));
741 mutex_exit(&zp
->z_lock
);
742 return (SET_ERROR(ENOENT
));
745 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
746 &zp
->z_links
, sizeof (zp
->z_links
));
749 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_PARENT(zfsvfs
), NULL
,
750 &dzp
->z_id
, sizeof (dzp
->z_id
));
751 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
752 &zp
->z_pflags
, sizeof (zp
->z_pflags
));
754 if (!(flag
& ZNEW
)) {
755 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
756 ctime
, sizeof (ctime
));
757 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
,
760 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
763 mutex_exit(&zp
->z_lock
);
765 mutex_enter(&dzp
->z_lock
);
767 dzp
->z_links
+= zp_is_dir
;
769 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
), NULL
,
770 &dzp
->z_size
, sizeof (dzp
->z_size
));
771 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
), NULL
,
772 &dzp
->z_links
, sizeof (dzp
->z_links
));
773 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
), NULL
,
774 mtime
, sizeof (mtime
));
775 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
), NULL
,
776 ctime
, sizeof (ctime
));
777 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
), NULL
,
778 &dzp
->z_pflags
, sizeof (dzp
->z_pflags
));
779 zfs_tstamp_update_setup(dzp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
780 error
= sa_bulk_update(dzp
->z_sa_hdl
, bulk
, count
, tx
);
782 mutex_exit(&dzp
->z_lock
);
784 value
= zfs_dirent(zp
, zp
->z_mode
);
785 error
= zap_add(zp
->z_zfsvfs
->z_os
, dzp
->z_id
, dl
->dl_name
,
789 dnlc_update(ZTOV(dzp
), dl
->dl_name
, vp
);
795 * The match type in the code for this function should conform to:
797 * ------------------------------------------------------------------------
798 * fs type | z_norm | lookup type | match type
799 * ---------|-------------|-------------|----------------------------------
800 * CS !norm | 0 | 0 | 0 (exact)
801 * CS norm | formX | 0 | MT_NORMALIZE
802 * CI !norm | upper | !ZCIEXACT | MT_NORMALIZE
803 * CI !norm | upper | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
804 * CI norm | upper|formX | !ZCIEXACT | MT_NORMALIZE
805 * CI norm | upper|formX | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
806 * CM !norm | upper | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
807 * CM !norm | upper | ZCILOOK | MT_NORMALIZE
808 * CM norm | upper|formX | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
809 * CM norm | upper|formX | ZCILOOK | MT_NORMALIZE
812 * CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
813 * upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
814 * formX = unicode normalization form set on fs creation
817 zfs_dropname(zfs_dirlock_t
*dl
, znode_t
*zp
, znode_t
*dzp
, dmu_tx_t
*tx
,
822 if (zp
->z_zfsvfs
->z_norm
) {
823 matchtype_t mt
= MT_NORMALIZE
;
825 if ((zp
->z_zfsvfs
->z_case
== ZFS_CASE_INSENSITIVE
&&
826 (flag
& ZCIEXACT
)) ||
827 (zp
->z_zfsvfs
->z_case
== ZFS_CASE_MIXED
&&
828 !(flag
& ZCILOOK
))) {
832 error
= zap_remove_norm(zp
->z_zfsvfs
->z_os
, dzp
->z_id
,
833 dl
->dl_name
, mt
, tx
);
835 error
= zap_remove(zp
->z_zfsvfs
->z_os
, dzp
->z_id
, dl
->dl_name
,
843 * Unlink zp from dl, and mark zp for deletion if this was the last link.
844 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
845 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
846 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
847 * and it's the caller's job to do it.
850 zfs_link_destroy(zfs_dirlock_t
*dl
, znode_t
*zp
, dmu_tx_t
*tx
, int flag
,
851 boolean_t
*unlinkedp
)
853 znode_t
*dzp
= dl
->dl_dzp
;
854 zfsvfs_t
*zfsvfs
= dzp
->z_zfsvfs
;
855 vnode_t
*vp
= ZTOV(zp
);
856 int zp_is_dir
= (vp
->v_type
== VDIR
);
857 boolean_t unlinked
= B_FALSE
;
858 sa_bulk_attr_t bulk
[5];
859 uint64_t mtime
[2], ctime
[2];
863 dnlc_remove(ZTOV(dzp
), dl
->dl_name
);
865 if (!(flag
& ZRENAMING
)) {
866 if (vn_vfswlock(vp
)) /* prevent new mounts on zp */
867 return (SET_ERROR(EBUSY
));
869 if (vn_ismntpt(vp
)) { /* don't remove mount point */
871 return (SET_ERROR(EBUSY
));
874 mutex_enter(&zp
->z_lock
);
876 if (zp_is_dir
&& !zfs_dirempty(zp
)) {
877 mutex_exit(&zp
->z_lock
);
879 return (SET_ERROR(EEXIST
));
883 * If we get here, we are going to try to remove the object.
884 * First try removing the name from the directory; if that
885 * fails, return the error.
887 error
= zfs_dropname(dl
, zp
, dzp
, tx
, flag
);
889 mutex_exit(&zp
->z_lock
);
894 if (zp
->z_links
<= zp_is_dir
) {
895 zfs_panic_recover("zfs: link count on %s is %u, "
896 "should be at least %u",
897 zp
->z_vnode
->v_path
!= vn_vpath_empty
?
898 zp
->z_vnode
->v_path
: "<unknown>",
899 (int)zp
->z_links
, zp_is_dir
+ 1);
900 zp
->z_links
= zp_is_dir
+ 1;
902 if (--zp
->z_links
== zp_is_dir
) {
903 zp
->z_unlinked
= B_TRUE
;
907 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
),
908 NULL
, &ctime
, sizeof (ctime
));
909 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
910 NULL
, &zp
->z_pflags
, sizeof (zp
->z_pflags
));
911 zfs_tstamp_update_setup(zp
, STATE_CHANGED
, mtime
, ctime
,
914 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
),
915 NULL
, &zp
->z_links
, sizeof (zp
->z_links
));
916 error
= sa_bulk_update(zp
->z_sa_hdl
, bulk
, count
, tx
);
919 mutex_exit(&zp
->z_lock
);
922 error
= zfs_dropname(dl
, zp
, dzp
, tx
, flag
);
927 mutex_enter(&dzp
->z_lock
);
928 dzp
->z_size
--; /* one dirent removed */
929 dzp
->z_links
-= zp_is_dir
; /* ".." link from zp */
930 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_LINKS(zfsvfs
),
931 NULL
, &dzp
->z_links
, sizeof (dzp
->z_links
));
932 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_SIZE(zfsvfs
),
933 NULL
, &dzp
->z_size
, sizeof (dzp
->z_size
));
934 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_CTIME(zfsvfs
),
935 NULL
, ctime
, sizeof (ctime
));
936 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_MTIME(zfsvfs
),
937 NULL
, mtime
, sizeof (mtime
));
938 SA_ADD_BULK_ATTR(bulk
, count
, SA_ZPL_FLAGS(zfsvfs
),
939 NULL
, &dzp
->z_pflags
, sizeof (dzp
->z_pflags
));
940 zfs_tstamp_update_setup(dzp
, CONTENT_MODIFIED
, mtime
, ctime
, B_TRUE
);
941 error
= sa_bulk_update(dzp
->z_sa_hdl
, bulk
, count
, tx
);
943 mutex_exit(&dzp
->z_lock
);
945 if (unlinkedp
!= NULL
)
946 *unlinkedp
= unlinked
;
948 zfs_unlinked_add(zp
, tx
);
954 * Indicate whether the directory is empty. Works with or without z_lock
955 * held, but can only be consider a hint in the latter case. Returns true
956 * if only "." and ".." remain and there's no work in progress.
959 zfs_dirempty(znode_t
*dzp
)
961 return (dzp
->z_size
== 2 && dzp
->z_dirlocks
== 0);
965 zfs_make_xattrdir(znode_t
*zp
, vattr_t
*vap
, vnode_t
**xvpp
, cred_t
*cr
)
967 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
971 zfs_acl_ids_t acl_ids
;
972 boolean_t fuid_dirtied
;
977 if (error
= zfs_zaccess(zp
, ACE_WRITE_NAMED_ATTRS
, 0, B_FALSE
, cr
))
980 if ((error
= zfs_acl_ids_create(zp
, IS_XATTR
, vap
, cr
, NULL
,
983 if (zfs_acl_ids_overquota(zfsvfs
, &acl_ids
)) {
984 zfs_acl_ids_free(&acl_ids
);
985 return (SET_ERROR(EDQUOT
));
988 tx
= dmu_tx_create(zfsvfs
->z_os
);
989 dmu_tx_hold_sa_create(tx
, acl_ids
.z_aclp
->z_acl_bytes
+
990 ZFS_SA_BASE_ATTR_SIZE
);
991 dmu_tx_hold_sa(tx
, zp
->z_sa_hdl
, B_TRUE
);
992 dmu_tx_hold_zap(tx
, DMU_NEW_OBJECT
, FALSE
, NULL
);
993 fuid_dirtied
= zfsvfs
->z_fuid_dirty
;
995 zfs_fuid_txhold(zfsvfs
, tx
);
996 error
= dmu_tx_assign(tx
, TXG_WAIT
);
998 zfs_acl_ids_free(&acl_ids
);
1002 zfs_mknode(zp
, vap
, tx
, cr
, IS_XATTR
, &xzp
, &acl_ids
);
1005 zfs_fuid_sync(zfsvfs
, tx
);
1008 error
= sa_lookup(xzp
->z_sa_hdl
, SA_ZPL_PARENT(zfsvfs
),
1009 &parent
, sizeof (parent
));
1010 ASSERT(error
== 0 && parent
== zp
->z_id
);
1013 VERIFY(0 == sa_update(zp
->z_sa_hdl
, SA_ZPL_XATTR(zfsvfs
), &xzp
->z_id
,
1014 sizeof (xzp
->z_id
), tx
));
1016 (void) zfs_log_create(zfsvfs
->z_log
, tx
, TX_MKXATTR
, zp
,
1017 xzp
, "", NULL
, acl_ids
.z_fuidp
, vap
);
1019 zfs_acl_ids_free(&acl_ids
);
1028 * Return a znode for the extended attribute directory for zp.
1029 * ** If the directory does not already exist, it is created **
1031 * IN: zp - znode to obtain attribute directory from
1032 * cr - credentials of caller
1033 * flags - flags from the fop_lookup call
1035 * OUT: xzpp - pointer to extended attribute znode
1037 * RETURN: 0 on success
1038 * error number on failure
1041 zfs_get_xattrdir(znode_t
*zp
, vnode_t
**xvpp
, cred_t
*cr
, int flags
)
1043 zfsvfs_t
*zfsvfs
= zp
->z_zfsvfs
;
1049 error
= zfs_dirent_lock(&dl
, zp
, "", &xzp
, ZXATTR
, NULL
, NULL
);
1055 zfs_dirent_unlock(dl
);
1060 if (!(flags
& CREATE_XATTR_DIR
)) {
1061 zfs_dirent_unlock(dl
);
1062 return (SET_ERROR(ENOENT
));
1065 if (zfsvfs
->z_vfs
->vfs_flag
& VFS_RDONLY
) {
1066 zfs_dirent_unlock(dl
);
1067 return (SET_ERROR(EROFS
));
1071 * The ability to 'create' files in an attribute
1072 * directory comes from the write_xattr permission on the base file.
1074 * The ability to 'search' an attribute directory requires
1075 * read_xattr permission on the base file.
1077 * Once in a directory the ability to read/write attributes
1078 * is controlled by the permissions on the attribute file.
1080 va
.va_mask
= AT_TYPE
| AT_MODE
| AT_UID
| AT_GID
;
1082 va
.va_mode
= S_IFDIR
| S_ISVTX
| 0777;
1083 zfs_fuid_map_ids(zp
, cr
, &va
.va_uid
, &va
.va_gid
);
1085 error
= zfs_make_xattrdir(zp
, &va
, xvpp
, cr
);
1086 zfs_dirent_unlock(dl
);
1088 if (error
== ERESTART
) {
1089 /* NB: we already did dmu_tx_wait() if necessary */
1097 * Decide whether it is okay to remove within a sticky directory.
1099 * In sticky directories, write access is not sufficient;
1100 * you can remove entries from a directory only if:
1102 * you own the directory,
1103 * you own the entry,
1104 * the entry is a plain file and you have write access,
1105 * or you are privileged (checked in secpolicy...).
1107 * The function returns 0 if remove access is granted.
1110 zfs_sticky_remove_access(znode_t
*zdp
, znode_t
*zp
, cred_t
*cr
)
1115 zfsvfs_t
*zfsvfs
= zdp
->z_zfsvfs
;
1117 if (zdp
->z_zfsvfs
->z_replay
)
1120 if ((zdp
->z_mode
& S_ISVTX
) == 0)
1123 downer
= zfs_fuid_map_id(zfsvfs
, zdp
->z_uid
, cr
, ZFS_OWNER
);
1124 fowner
= zfs_fuid_map_id(zfsvfs
, zp
->z_uid
, cr
, ZFS_OWNER
);
1126 if ((uid
= crgetuid(cr
)) == downer
|| uid
== fowner
||
1127 (ZTOV(zp
)->v_type
== VREG
&&
1128 zfs_zaccess(zp
, ACE_WRITE_DATA
, 0, B_FALSE
, cr
) == 0))
1131 return (secpolicy_vnode_remove(cr
));