Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / uts / common / fs / zfs / zfs_dir.c
blob555cc82cf09be2cad83c8939eaa48186a5ea88c1
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/systm.h>
30 #include <sys/sysmacros.h>
31 #include <sys/resource.h>
32 #include <sys/vfs.h>
33 #include <sys/vnode.h>
34 #include <sys/file.h>
35 #include <sys/mode.h>
36 #include <sys/kmem.h>
37 #include <sys/uio.h>
38 #include <sys/pathname.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/stat.h>
42 #include <sys/unistd.h>
43 #include <sys/sunddi.h>
44 #include <sys/random.h>
45 #include <sys/policy.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/zap.h>
50 #include <sys/dmu.h>
51 #include <sys/atomic.h>
52 #include <sys/zfs_ctldir.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/dnlc.h>
55 #include <sys/extdirent.h>
58 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
59 * of names after deciding which is the appropriate lookup interface.
61 static int
62 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
63 boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
65 int error;
67 if (zfsvfs->z_norm) {
68 matchtype_t mt = MT_FIRST;
69 boolean_t conflict = B_FALSE;
70 size_t bufsz = 0;
71 char *buf = NULL;
73 if (rpnp) {
74 buf = rpnp->pn_buf;
75 bufsz = rpnp->pn_bufsize;
77 if (exact)
78 mt = MT_EXACT;
80 * In the non-mixed case we only expect there would ever
81 * be one match, but we need to use the normalizing lookup.
83 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
84 zoid, mt, buf, bufsz, &conflict);
85 if (!error && deflags)
86 *deflags = conflict ? ED_CASE_CONFLICT : 0;
87 } else {
88 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
90 *zoid = ZFS_DIRENT_OBJ(*zoid);
92 if (error == ENOENT && update)
93 dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
95 return (error);
98 static void
99 zfs_dirlock_free(zfs_dirlock_t *dl)
102 if (--(dl->dl_refcnt) == 0) {
103 if (dl->dl_namesize != 0)
104 kmem_free(dl->dl_name, dl->dl_namesize);
105 cv_destroy(&dl->dl_cv);
106 kmem_free(dl, sizeof (*dl));
112 * Lock a directory entry. A dirlock on <dzp, name> protects that name
113 * in dzp's directory zap object. As long as you hold a dirlock, you can
114 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
115 * can change the zap entry for (i.e. link or unlink) this name.
117 * Input arguments:
118 * dzp - znode for directory
119 * name - name of entry to lock
120 * flag - ZNEW: if the entry already exists, fail with EEXIST.
121 * ZEXISTS: if the entry does not exist, fail with ENOENT.
122 * ZSHARED: allow concurrent access with other ZSHARED callers.
123 * ZXATTR: we want dzp's xattr directory
124 * ZCILOOK: On a mixed sensitivity file system,
125 * this lookup should be case-insensitive.
126 * ZCIEXACT: On a purely case-insensitive file system,
127 * this lookup should be case-sensitive.
128 * ZRENAMING: we are locking for renaming, force narrow locks
130 * Output arguments:
131 * zpp - pointer to the znode for the entry (NULL if there isn't one)
132 * dlpp - pointer to the dirlock for this entry (NULL on error)
133 * direntflags - (case-insensitive lookup only)
134 * flags if multiple case-sensitive matches exist in directory
135 * realpnp - (case-insensitive lookup only)
136 * actual name matched within the directory
138 * Return value: 0 on success or errno on failure.
140 * NOTE: Always checks for, and rejects, '.' and '..'.
141 * NOTE: For case-insensitive file systems we take wide locks (see below),
142 * but return znode pointers to a single match.
145 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
146 int flag, int *direntflags, pathname_t *realpnp)
148 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
149 zfs_dirlock_t *dl;
150 boolean_t update;
151 boolean_t exact;
152 uint64_t zoid;
153 vnode_t *vp = NULL;
154 int error = 0;
155 int cmpflags;
157 *zpp = NULL;
158 *dlpp = NULL;
161 * Verify that we are not trying to lock '.', '..', or '.zfs'
163 if (name[0] == '.' &&
164 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
165 zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
166 return (EEXIST);
169 * Case sensitivity and normalization preferences are set when
170 * the file system is created. These are stored in the
171 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
172 * affect what vnodes can be cached in the DNLC, how we
173 * perform zap lookups, and the "width" of our dirlocks.
175 * A normal dirlock locks a single name. Note that with
176 * normalization a name can be composed multiple ways, but
177 * when normalized, these names all compare equal. A wide
178 * dirlock locks multiple names. We need these when the file
179 * system is supporting mixed-mode access. It is sometimes
180 * necessary to lock all case permutations of file name at
181 * once so that simultaneous case-insensitive/case-sensitive
182 * behaves as rationally as possible.
186 * Decide if exact matches should be requested when performing
187 * a zap lookup on file systems supporting case-insensitive
188 * access.
190 exact =
191 ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
192 ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
195 * Only look in or update the DNLC if we are looking for the
196 * name on a file system that does not require normalization
197 * or case folding. We can also look there if we happen to be
198 * on a non-normalizing, mixed sensitivity file system IF we
199 * are looking for the exact name.
201 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
202 * case for performance improvement?
204 update = !zfsvfs->z_norm ||
205 ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
206 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
209 * ZRENAMING indicates we are in a situation where we should
210 * take narrow locks regardless of the file system's
211 * preferences for normalizing and case folding. This will
212 * prevent us deadlocking trying to grab the same wide lock
213 * twice if the two names happen to be case-insensitive
214 * matches.
216 if (flag & ZRENAMING)
217 cmpflags = 0;
218 else
219 cmpflags = zfsvfs->z_norm;
222 * Wait until there are no locks on this name.
224 if (flag & ZSAMEDIR)
225 ASSERT(RW_LOCK_HELD(&dzp->z_name_lock));
226 else
227 rw_enter(&dzp->z_name_lock, RW_READER);
228 mutex_enter(&dzp->z_lock);
229 for (;;) {
230 if (dzp->z_unlinked) {
231 mutex_exit(&dzp->z_lock);
232 if (!(flag & ZSAMEDIR))
233 rw_exit(&dzp->z_name_lock);
234 return (ENOENT);
236 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
237 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
238 U8_UNICODE_LATEST, &error) == 0) || error != 0)
239 break;
241 if (error != 0) {
242 mutex_exit(&dzp->z_lock);
243 if (!(flag & ZSAMEDIR))
244 rw_exit(&dzp->z_name_lock);
245 return (ENOENT);
247 if (dl == NULL) {
249 * Allocate a new dirlock and add it to the list.
251 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
252 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
253 dl->dl_name = name;
254 dl->dl_sharecnt = 0;
255 dl->dl_refcnt = 1;
256 dl->dl_namesize = 0;
257 dl->dl_dzp = dzp;
258 dl->dl_next = dzp->z_dirlocks;
259 dzp->z_dirlocks = dl;
260 break;
261 } else {
262 /* Hold reference counter first */
263 dl->dl_refcnt++;
265 if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
266 break;
267 cv_wait(&dl->dl_cv, &dzp->z_lock);
268 zfs_dirlock_free(dl);
271 if ((flag & ZSHARED) && ++(dl->dl_sharecnt) > 1 && dl->dl_namesize == 0) {
273 * We're the second shared reference to dl. Make a copy of
274 * dl_name in case the first thread goes away before we do.
275 * Note that we initialize the new name before storing its
276 * pointer into dl_name, because the first thread may load
277 * dl->dl_name at any time. He'll either see the old value,
278 * which is his, or the new shared copy; either is OK.
280 dl->dl_namesize = strlen(dl->dl_name) + 1;
281 name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
282 bcopy(dl->dl_name, name, dl->dl_namesize);
283 dl->dl_name = name;
286 mutex_exit(&dzp->z_lock);
289 * We have a dirlock on the name. (Note that it is the dirlock,
290 * not the dzp's z_lock, that protects the name in the zap object.)
291 * See if there's an object by this name; if so, put a hold on it.
293 if (flag & ZXATTR) {
294 zoid = dzp->z_phys->zp_xattr;
295 error = (zoid == 0 ? ENOENT : 0);
296 } else {
297 if (update)
298 vp = dnlc_lookup(ZTOV(dzp), name);
299 if (vp == DNLC_NO_VNODE) {
300 VN_RELE(vp);
301 error = ENOENT;
302 } else if (vp) {
303 if (flag & ZNEW) {
304 zfs_dirent_unlock(dl, flag);
305 VN_RELE(vp);
306 return (EEXIST);
308 *dlpp = dl;
309 *zpp = VTOZ(vp);
310 return (0);
311 } else {
312 error = zfs_match_find(zfsvfs, dzp, name, exact,
313 update, direntflags, realpnp, &zoid);
316 if (error) {
317 if (error != ENOENT || (flag & ZEXISTS)) {
318 zfs_dirent_unlock(dl, flag);
319 return (error);
321 } else {
322 if (flag & ZNEW) {
323 zfs_dirent_unlock(dl, flag);
324 return (EEXIST);
326 error = zfs_zget(zfsvfs, zoid, zpp);
327 if (error) {
328 zfs_dirent_unlock(dl, flag);
329 return (error);
331 if (!(flag & ZXATTR) && update)
332 dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
335 *dlpp = dl;
337 return (0);
341 * Unlock this directory entry and wake anyone who was waiting for it.
343 void
344 zfs_dirent_unlock(zfs_dirlock_t *dl, int flag)
346 znode_t *dzp = dl->dl_dzp;
347 zfs_dirlock_t **prev_dl, *cur_dl;
349 mutex_enter(&dzp->z_lock);
350 if (!(flag & ZSAMEDIR))
351 rw_exit(&dzp->z_name_lock);
352 if (dl->dl_sharecnt > 1) {
353 dl->dl_sharecnt--;
354 mutex_exit(&dzp->z_lock);
355 return;
357 prev_dl = &dzp->z_dirlocks;
358 while ((cur_dl = *prev_dl) != dl)
359 prev_dl = &cur_dl->dl_next;
360 *prev_dl = dl->dl_next;
361 cv_broadcast(&dl->dl_cv);
362 mutex_exit(&dzp->z_lock);
364 zfs_dirlock_free(dl);
368 * Look up an entry in a directory.
370 * NOTE: '.' and '..' are handled as special cases because
371 * no directory entries are actually stored for them. If this is
372 * the root of a filesystem, then '.zfs' is also treated as a
373 * special pseudo-directory.
376 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
377 int *deflg, pathname_t *rpnp)
379 zfs_dirlock_t *dl;
380 znode_t *zp;
381 int error = 0;
383 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
384 *vpp = ZTOV(dzp);
385 VN_HOLD(*vpp);
386 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
387 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
389 * If we are a snapshot mounted under .zfs, return
390 * the vp for the snapshot directory.
392 if (dzp->z_phys->zp_parent == dzp->z_id &&
393 zfsvfs->z_parent != zfsvfs) {
394 error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
395 "snapshot", vpp, NULL, 0, NULL, kcred,
396 NULL, NULL, NULL);
397 return (error);
399 rw_enter(&dzp->z_parent_lock, RW_READER);
400 error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
401 if (error == 0)
402 *vpp = ZTOV(zp);
403 rw_exit(&dzp->z_parent_lock);
404 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
405 *vpp = zfsctl_root(dzp);
406 } else {
407 int zf;
409 zf = ZEXISTS | ZSHARED;
410 if (flags & FIGNORECASE)
411 zf |= ZCILOOK;
413 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
414 if (error == 0) {
415 *vpp = ZTOV(zp);
416 zfs_dirent_unlock(dl, 0);
417 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
419 rpnp = NULL;
422 if ((flags & FIGNORECASE) && rpnp && !error)
423 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
425 return (error);
429 * unlinked Set (formerly known as the "delete queue") Error Handling
431 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
432 * don't specify the name of the entry that we will be manipulating. We
433 * also fib and say that we won't be adding any new entries to the
434 * unlinked set, even though we might (this is to lower the minimum file
435 * size that can be deleted in a full filesystem). So on the small
436 * chance that the nlink list is using a fat zap (ie. has more than
437 * 2000 entries), we *may* not pre-read a block that's needed.
438 * Therefore it is remotely possible for some of the assertions
439 * regarding the unlinked set below to fail due to i/o error. On a
440 * nondebug system, this will result in the space being leaked.
442 void
443 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
445 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
447 ASSERT(zp->z_unlinked);
448 ASSERT3U(zp->z_phys->zp_links, ==, 0);
450 VERIFY3U(0, ==,
451 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
455 * Clean up any znodes that had no links when we either crashed or
456 * (force) umounted the file system.
458 void
459 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
461 zap_cursor_t zc;
462 zap_attribute_t zap;
463 dmu_object_info_t doi;
464 znode_t *zp;
465 int error;
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);
480 if (error != 0)
481 continue;
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.
497 if (error != 0)
498 continue;
500 zp->z_unlinked = B_TRUE;
501 VN_RELE(ZTOV(zp));
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
515 * files.
517 static int
518 zfs_purgedir(znode_t *dzp)
520 zap_cursor_t zc;
521 zap_attribute_t zap;
522 znode_t *xzp;
523 dmu_tx_t *tx;
524 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
525 zfs_dirlock_t dl;
526 int skipped = 0;
527 int error;
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);
534 if (error) {
535 skipped += 1;
536 continue;
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);
548 if (error) {
549 dmu_tx_abort(tx);
550 VN_RELE(ZTOV(xzp));
551 skipped += 1;
552 continue;
554 bzero(&dl, sizeof (dl));
555 dl.dl_dzp = dzp;
556 dl.dl_name = zap.za_name;
558 error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
559 if (error)
560 skipped += 1;
561 dmu_tx_commit(tx);
563 VN_RELE(ZTOV(xzp));
565 zap_cursor_fini(&zc);
566 if (error != ENOENT)
567 skipped += 1;
568 return (skipped);
571 void
572 zfs_rmnode(znode_t *zp)
574 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
575 objset_t *os = zfsvfs->z_os;
576 znode_t *xzp = NULL;
577 dmu_tx_t *tx;
578 uint64_t acl_obj;
579 int error;
581 ASSERT(ZTOV(zp)->v_count == 0);
582 ASSERT(zp->z_phys->zp_links == 0);
585 * If this is a ZIL replay then leave the object in the unlinked set.
586 * Otherwise we can get a deadlock, because the delete can be
587 * quite large and span multiple tx's and txgs, but each replay
588 * creates a tx to atomically run the replay function and mark the
589 * replay record as complete. We deadlock trying to start a tx in
590 * a new txg to further the deletion but can't because the replay
591 * tx hasn't finished.
593 * We actually delete the object if we get a failure to create an
594 * object in zil_replay_log_record(), or after calling zil_replay().
596 if (zfsvfs->z_assign >= TXG_INITIAL) {
597 zfs_znode_dmu_fini(zp);
598 zfs_znode_free(zp);
599 return;
603 * If this is an attribute directory, purge its contents.
605 if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
606 if (zfs_purgedir(zp) != 0) {
608 * Not enough space to delete some xattrs.
609 * Leave it in the unlinked set.
611 zfs_znode_dmu_fini(zp);
612 zfs_znode_free(zp);
613 return;
618 * Free up all the data in the file.
620 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
621 if (error) {
623 * Not enough space. Leave the file in the unlinked set.
625 zfs_znode_dmu_fini(zp);
626 zfs_znode_free(zp);
627 return;
631 * If the file has extended attributes, we're going to unlink
632 * the xattr dir.
634 if (zp->z_phys->zp_xattr) {
635 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
636 ASSERT(error == 0);
639 acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
642 * Set up the final transaction.
644 tx = dmu_tx_create(os);
645 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
646 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
647 if (xzp) {
648 dmu_tx_hold_bonus(tx, xzp->z_id);
649 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
651 if (acl_obj)
652 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
653 error = dmu_tx_assign(tx, TXG_WAIT);
654 if (error) {
656 * Not enough space to delete the file. Leave it in the
657 * unlinked set, leaking it until the fs is remounted (at
658 * which point we'll call zfs_unlinked_drain() to process it).
660 dmu_tx_abort(tx);
661 zfs_znode_dmu_fini(zp);
662 zfs_znode_free(zp);
663 goto out;
666 if (xzp) {
667 dmu_buf_will_dirty(xzp->z_dbuf, tx);
668 mutex_enter(&xzp->z_lock);
669 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
670 xzp->z_phys->zp_links = 0; /* no more links to it */
671 mutex_exit(&xzp->z_lock);
672 zfs_unlinked_add(xzp, tx);
675 /* Remove this znode from the unlinked set */
676 VERIFY3U(0, ==,
677 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
679 zfs_znode_delete(zp, tx);
681 dmu_tx_commit(tx);
682 out:
683 if (xzp)
684 VN_RELE(ZTOV(xzp));
687 static uint64_t
688 zfs_dirent(znode_t *zp)
690 uint64_t de = zp->z_id;
691 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
692 de |= IFTODT((zp)->z_phys->zp_mode) << 60;
693 return (de);
697 * Link zp into dl. Can only fail if zp has been unlinked.
700 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
702 znode_t *dzp = dl->dl_dzp;
703 vnode_t *vp = ZTOV(zp);
704 uint64_t value;
705 int zp_is_dir = (vp->v_type == VDIR);
706 int error;
708 dmu_buf_will_dirty(zp->z_dbuf, tx);
709 mutex_enter(&zp->z_lock);
711 if (!(flag & ZRENAMING)) {
712 if (zp->z_unlinked) { /* no new links to unlinked zp */
713 ASSERT(!(flag & (ZNEW | ZEXISTS)));
714 mutex_exit(&zp->z_lock);
715 return (ENOENT);
717 zp->z_phys->zp_links++;
719 zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */
721 if (!(flag & ZNEW))
722 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
723 mutex_exit(&zp->z_lock);
725 dmu_buf_will_dirty(dzp->z_dbuf, tx);
726 mutex_enter(&dzp->z_lock);
727 dzp->z_phys->zp_size++; /* one dirent added */
728 dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */
729 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
730 mutex_exit(&dzp->z_lock);
732 value = zfs_dirent(zp);
733 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
734 8, 1, &value, tx);
735 ASSERT(error == 0);
737 dnlc_update(ZTOV(dzp), dl->dl_name, vp);
739 return (0);
743 * Unlink zp from dl, and mark zp for deletion if this was the last link.
744 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
745 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
746 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
747 * and it's the caller's job to do it.
750 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
751 boolean_t *unlinkedp)
753 znode_t *dzp = dl->dl_dzp;
754 vnode_t *vp = ZTOV(zp);
755 int zp_is_dir = (vp->v_type == VDIR);
756 boolean_t unlinked = B_FALSE;
757 int error;
759 dnlc_remove(ZTOV(dzp), dl->dl_name);
761 if (!(flag & ZRENAMING)) {
762 dmu_buf_will_dirty(zp->z_dbuf, tx);
764 if (vn_vfswlock(vp)) /* prevent new mounts on zp */
765 return (EBUSY);
767 if (vn_ismntpt(vp)) { /* don't remove mount point */
768 vn_vfsunlock(vp);
769 return (EBUSY);
772 mutex_enter(&zp->z_lock);
773 if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */
774 mutex_exit(&zp->z_lock);
775 vn_vfsunlock(vp);
776 return (EEXIST);
778 if (zp->z_phys->zp_links <= zp_is_dir) {
779 zfs_panic_recover("zfs: link count on vnode %p is %u, "
780 "should be at least %u",
781 zp->z_vnode, (int)zp->z_phys->zp_links,
782 zp_is_dir + 1);
783 zp->z_phys->zp_links = zp_is_dir + 1;
785 if (--zp->z_phys->zp_links == zp_is_dir) {
786 zp->z_unlinked = B_TRUE;
787 zp->z_phys->zp_links = 0;
788 unlinked = B_TRUE;
789 } else {
790 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
792 mutex_exit(&zp->z_lock);
793 vn_vfsunlock(vp);
796 dmu_buf_will_dirty(dzp->z_dbuf, tx);
797 mutex_enter(&dzp->z_lock);
798 dzp->z_phys->zp_size--; /* one dirent removed */
799 dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */
800 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
801 mutex_exit(&dzp->z_lock);
803 if (zp->z_zfsvfs->z_norm) {
804 if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
805 (flag & ZCIEXACT)) ||
806 ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
807 !(flag & ZCILOOK)))
808 error = zap_remove_norm(zp->z_zfsvfs->z_os,
809 dzp->z_id, dl->dl_name, MT_EXACT, tx);
810 else
811 error = zap_remove_norm(zp->z_zfsvfs->z_os,
812 dzp->z_id, dl->dl_name, MT_FIRST, tx);
813 } else {
814 error = zap_remove(zp->z_zfsvfs->z_os,
815 dzp->z_id, dl->dl_name, tx);
817 ASSERT(error == 0);
819 if (unlinkedp != NULL)
820 *unlinkedp = unlinked;
821 else if (unlinked)
822 zfs_unlinked_add(zp, tx);
824 return (0);
828 * Indicate whether the directory is empty. Works with or without z_lock
829 * held, but can only be consider a hint in the latter case. Returns true
830 * if only "." and ".." remain and there's no work in progress.
832 boolean_t
833 zfs_dirempty(znode_t *dzp)
835 return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
839 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
841 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
842 znode_t *xzp;
843 dmu_tx_t *tx;
844 int error;
845 zfs_fuid_info_t *fuidp = NULL;
847 *xvpp = NULL;
849 if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
850 return (error);
852 tx = dmu_tx_create(zfsvfs->z_os);
853 dmu_tx_hold_bonus(tx, zp->z_id);
854 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
855 if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
856 if (zfsvfs->z_fuid_obj == 0) {
857 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
858 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
859 FUID_SIZE_ESTIMATE(zfsvfs));
860 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
861 } else {
862 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
863 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
864 FUID_SIZE_ESTIMATE(zfsvfs));
867 error = dmu_tx_assign(tx, zfsvfs->z_assign);
868 if (error) {
869 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
870 dmu_tx_wait(tx);
871 dmu_tx_abort(tx);
872 return (error);
874 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp);
875 ASSERT(xzp->z_phys->zp_parent == zp->z_id);
876 dmu_buf_will_dirty(zp->z_dbuf, tx);
877 zp->z_phys->zp_xattr = xzp->z_id;
879 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
880 xzp, "", NULL, fuidp, vap);
881 if (fuidp)
882 zfs_fuid_info_free(fuidp);
883 dmu_tx_commit(tx);
885 *xvpp = ZTOV(xzp);
887 return (0);
891 * Return a znode for the extended attribute directory for zp.
892 * ** If the directory does not already exist, it is created **
894 * IN: zp - znode to obtain attribute directory from
895 * cr - credentials of caller
896 * flags - flags from the VOP_LOOKUP call
898 * OUT: xzpp - pointer to extended attribute znode
900 * RETURN: 0 on success
901 * error number on failure
904 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
906 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
907 znode_t *xzp;
908 zfs_dirlock_t *dl;
909 vattr_t va;
910 int error;
911 top:
912 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
913 if (error)
914 return (error);
916 if (xzp != NULL) {
917 *xvpp = ZTOV(xzp);
918 zfs_dirent_unlock(dl, 0);
919 return (0);
922 ASSERT(zp->z_phys->zp_xattr == 0);
924 if (!(flags & CREATE_XATTR_DIR)) {
925 zfs_dirent_unlock(dl, 0);
926 return (ENOENT);
929 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
930 zfs_dirent_unlock(dl, 0);
931 return (EROFS);
935 * The ability to 'create' files in an attribute
936 * directory comes from the write_xattr permission on the base file.
938 * The ability to 'search' an attribute directory requires
939 * read_xattr permission on the base file.
941 * Once in a directory the ability to read/write attributes
942 * is controlled by the permissions on the attribute file.
944 va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
945 va.va_type = VDIR;
946 va.va_mode = S_IFDIR | S_ISVTX | 0777;
947 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
949 error = zfs_make_xattrdir(zp, &va, xvpp, cr);
950 zfs_dirent_unlock(dl, 0);
952 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
953 /* NB: we already did dmu_tx_wait() if necessary */
954 goto top;
957 return (error);
961 * Decide whether it is okay to remove within a sticky directory.
963 * In sticky directories, write access is not sufficient;
964 * you can remove entries from a directory only if:
966 * you own the directory,
967 * you own the entry,
968 * the entry is a plain file and you have write access,
969 * or you are privileged (checked in secpolicy...).
971 * The function returns 0 if remove access is granted.
974 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
976 uid_t uid;
977 uid_t downer;
978 uid_t fowner;
979 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
981 if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */
982 return (0);
984 if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
985 return (0);
987 downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
988 fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
990 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
991 (ZTOV(zp)->v_type == VREG &&
992 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
993 return (0);
994 else
995 return (secpolicy_vnode_remove(cr));