zfs_dir.c revision af2c4821c0a23e873f2a63bca4145080aa2183e3
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER START
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The contents of this file are subject to the terms of the
5cff782560a1c3cf913ba5574a5123a299f3315emh * Common Development and Distribution License (the "License").
5cff782560a1c3cf913ba5574a5123a299f3315emh * You may not use this file except in compliance with the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5cff782560a1c3cf913ba5574a5123a299f3315emh * or http://www.opensolaris.org/os/licensing.
5cff782560a1c3cf913ba5574a5123a299f3315emh * See the License for the specific language governing permissions
5cff782560a1c3cf913ba5574a5123a299f3315emh * and limitations under the License.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * When distributing Covered Code, include this CDDL HEADER in each
5cff782560a1c3cf913ba5574a5123a299f3315emh * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5cff782560a1c3cf913ba5574a5123a299f3315emh * If applicable, add the following below this CDDL HEADER, with the
5cff782560a1c3cf913ba5574a5123a299f3315emh * fields enclosed by brackets "[]" replaced with your own identifying
5cff782560a1c3cf913ba5574a5123a299f3315emh * information: Portions Copyright [yyyy] [name of copyright owner]
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER END
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Use is subject to license terms.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#pragma ident "%Z%%M% %I% %E% SMI"
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/types.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/param.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/time.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/systm.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/sysmacros.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/resource.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/vfs.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/vnode.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/file.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/mode.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/kmem.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/uio.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/pathname.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/cmn_err.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/errno.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/stat.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/unistd.h>
c210ded489f4b4bd3e26ba118f5603c41a5c8c02esaxe#include <sys/random.h>
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe#include <sys/policy.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/zfs_dir.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/zfs_acl.h>
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood#include <sys/fs/zfs.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include "fs/fs_subr.h"
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/zap.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/dmu.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/atomic.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/zfs_ctldir.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh#include <sys/dnlc.h>
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Lock a directory entry. A dirlock on <dzp, name> protects that name
5cff782560a1c3cf913ba5574a5123a299f3315emh * in dzp's directory zap object. As long as you hold a dirlock, you can
5cff782560a1c3cf913ba5574a5123a299f3315emh * assume two things: (1) dzp cannot be reaped, and (2) no other thread
5cff782560a1c3cf913ba5574a5123a299f3315emh * can change the zap entry for (i.e. link or unlink) this name.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Input arguments:
5cff782560a1c3cf913ba5574a5123a299f3315emh * dzp - znode for directory
5cff782560a1c3cf913ba5574a5123a299f3315emh * name - name of entry to lock
5cff782560a1c3cf913ba5574a5123a299f3315emh * flag - ZNEW: if the entry already exists, fail with EEXIST.
5cff782560a1c3cf913ba5574a5123a299f3315emh * ZEXISTS: if the entry does not exist, fail with ENOENT.
5cff782560a1c3cf913ba5574a5123a299f3315emh * ZSHARED: allow concurrent access with other ZSHARED callers.
5cff782560a1c3cf913ba5574a5123a299f3315emh * ZXATTR: we want dzp's xattr directory
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Output arguments:
5cff782560a1c3cf913ba5574a5123a299f3315emh * zpp - pointer to the znode for the entry (NULL if there isn't one)
5cff782560a1c3cf913ba5574a5123a299f3315emh * dlpp - pointer to the dirlock for this entry (NULL on error)
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Return value: 0 on success or errno on failure.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * NOTE: Always checks for, and rejects, '.' and '..'.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
5cff782560a1c3cf913ba5574a5123a299f3315emh int flag)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirlock_t *dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint64_t zoid;
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh vnode_t *vp;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh *zpp = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh *dlpp = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Verify that we are not trying to lock '.', '..', or '.zfs'
193974072f41a843678abf5f61979c748687e66bSherry Moore */
193974072f41a843678abf5f61979c748687e66bSherry Moore if (name[0] == '.' &&
5cff782560a1c3cf913ba5574a5123a299f3315emh (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh return (EEXIST);
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Wait until there are no locks on this name.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh rw_enter(&dzp->z_name_lock, RW_READER);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh for (;;) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (dzp->z_unlinked) {
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh rw_exit(&dzp->z_name_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (ENOENT);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next)
5cff782560a1c3cf913ba5574a5123a299f3315emh if (strcmp(name, dl->dl_name) == 0)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe break;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (dl == NULL) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe /*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Allocate a new dirlock and add it to the list.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe */
5cff782560a1c3cf913ba5574a5123a299f3315emh dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_name = name;
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_sharecnt = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_namesize = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_dzp = dzp;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dl->dl_next = dzp->z_dirlocks;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dzp->z_dirlocks = dl;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe break;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe break;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe cv_wait(&dl->dl_cv, &dzp->z_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We're the second shared reference to dl. Make a copy of
5cff782560a1c3cf913ba5574a5123a299f3315emh * dl_name in case the first thread goes away before we do.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Note that we initialize the new name before storing its
5cff782560a1c3cf913ba5574a5123a299f3315emh * pointer into dl_name, because the first thread may load
5cff782560a1c3cf913ba5574a5123a299f3315emh * dl->dl_name at any time. He'll either see the old value,
5cff782560a1c3cf913ba5574a5123a299f3315emh * which is his, or the new shared copy; either is OK.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_namesize = strlen(dl->dl_name) + 1;
5cff782560a1c3cf913ba5574a5123a299f3315emh name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
5cff782560a1c3cf913ba5574a5123a299f3315emh bcopy(dl->dl_name, name, dl->dl_namesize);
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_name = name;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We have a dirlock on the name. (Note that it is the dirlock,
5cff782560a1c3cf913ba5574a5123a299f3315emh * not the dzp's z_lock, that protects the name in the zap object.)
5cff782560a1c3cf913ba5574a5123a299f3315emh * See if there's an object by this name; if so, put a hold on it.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (flag & ZXATTR) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zoid = dzp->z_phys->zp_xattr;
5cff782560a1c3cf913ba5574a5123a299f3315emh error = (zoid == 0 ? ENOENT : 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe vp = dnlc_lookup(ZTOV(dzp), name);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (vp == DNLC_NO_VNODE) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood VN_RELE(vp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood error = ENOENT;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } else if (vp) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (flag & ZNEW) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe zfs_dirent_unlock(dl);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood VN_RELE(vp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (EEXIST);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh *dlpp = dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh *zpp = VTOZ(vp);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zap_lookup(zfsvfs->z_os, dzp->z_id, name,
5cff782560a1c3cf913ba5574a5123a299f3315emh 8, 1, &zoid);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == ENOENT)
5cff782560a1c3cf913ba5574a5123a299f3315emh dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (error) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (error != ENOENT || (flag & ZEXISTS)) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zfs_dirent_unlock(dl);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (error);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood }
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood } else {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (flag & ZNEW) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zfs_dirent_unlock(dl);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (EEXIST);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_zget(zfsvfs, zoid, zpp);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirent_unlock(dl);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!(flag & ZXATTR))
5cff782560a1c3cf913ba5574a5123a299f3315emh dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh *dlpp = dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Unlock this directory entry and wake anyone who was waiting for it.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhvoid
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_dirent_unlock(zfs_dirlock_t *dl)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe{
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *dzp = dl->dl_dzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirlock_t **prev_dl, *cur_dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh rw_exit(&dzp->z_name_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (dl->dl_sharecnt > 1) {
5cff782560a1c3cf913ba5574a5123a299f3315emh dl->dl_sharecnt--;
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh prev_dl = &dzp->z_dirlocks;
5cff782560a1c3cf913ba5574a5123a299f3315emh while ((cur_dl = *prev_dl) != dl)
5cff782560a1c3cf913ba5574a5123a299f3315emh prev_dl = &cur_dl->dl_next;
5cff782560a1c3cf913ba5574a5123a299f3315emh *prev_dl = dl->dl_next;
5cff782560a1c3cf913ba5574a5123a299f3315emh cv_broadcast(&dl->dl_cv);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (dl->dl_namesize != 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(dl->dl_name, dl->dl_namesize);
5cff782560a1c3cf913ba5574a5123a299f3315emh cv_destroy(&dl->dl_cv);
5cff782560a1c3cf913ba5574a5123a299f3315emh kmem_free(dl, sizeof (*dl));
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Look up an entry in a directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * NOTE: '.' and '..' are handled as special cases because
5cff782560a1c3cf913ba5574a5123a299f3315emh * no directory entries are actually stored for them. If this is
5cff782560a1c3cf913ba5574a5123a299f3315emh * the root of a filesystem, then '.zfs' is also treated as a
5cff782560a1c3cf913ba5574a5123a299f3315emh * special pseudo-directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirlock_t *dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *zp;
5cff782560a1c3cf913ba5574a5123a299f3315emh int error = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood *vpp = ZTOV(dzp);
5cff782560a1c3cf913ba5574a5123a299f3315emh VN_HOLD(*vpp);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * If we are a snapshot mounted under .zfs, return
5cff782560a1c3cf913ba5574a5123a299f3315emh * the vp for the snapshot directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (dzp->z_phys->zp_parent == dzp->z_id &&
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs->z_parent != zfsvfs) {
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
5cff782560a1c3cf913ba5574a5123a299f3315emh "snapshot", vpp, NULL, 0, NULL, kcred);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
5cff782560a1c3cf913ba5574a5123a299f3315emh rw_enter(&dzp->z_parent_lock, RW_READER);
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh *vpp = ZTOV(zp);
5cff782560a1c3cf913ba5574a5123a299f3315emh rw_exit(&dzp->z_parent_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh *vpp = zfsctl_root(dzp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh *vpp = ZTOV(zp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe zfs_dirent_unlock(dl);
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
5cff782560a1c3cf913ba5574a5123a299f3315emh }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodstatic char *
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxezfs_unlinked_hexname(char namebuf[17], uint64_t x)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe{
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe char *name = &namebuf[16];
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe const char digits[16] = "0123456789abcdef";
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood *name = '\0';
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood do {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe *--name = digits[x & 0xf];
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe x >>= 4;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } while (x != 0);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood return (name);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood}
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood/*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * unlinked Set (formerly known as the "delete queue") Error Handling
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe *
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * don't specify the name of the entry that we will be manipulating. We
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * also fib and say that we won't be adding any new entries to the
5cff782560a1c3cf913ba5574a5123a299f3315emh * unlinked set, even though we might (this is to lower the minimum file
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * size that can be deleted in a full filesystem). So on the small
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * chance that the nlink list is using a fat zap (ie. has more than
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * 2000 entries), we *may* not pre-read a block that's needed.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Therefore it is remotely possible for some of the assertions
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * regarding the unlinked set below to fail due to i/o error. On a
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * nondebug system, this will result in the space being leaked.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxevoid
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxezfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood{
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zfsvfs_t *zfsvfs = zp->z_zfsvfs;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood char obj_name[17];
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood int error;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT(zp->z_unlinked);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT3U(zp->z_phys->zp_links, ==, 0);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT3U(error, ==, 0);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood}
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood/*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Clean up any znodes that had no links when we either crashed or
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * (force) umounted the file system.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodvoid
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodzfs_unlinked_drain(zfsvfs_t *zfsvfs)
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood{
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zap_cursor_t zc;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zap_attribute_t zap;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_object_info_t doi;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood znode_t *zp;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood int error;
173531301317dd4f31e83d4785873141e984ab86Mark Haywood
173531301317dd4f31e83d4785873141e984ab86Mark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Interate over the contents of the unlinked set.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe */
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_cursor_retrieve(&zc, &zap) == 0;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood zap_cursor_advance(&zc)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * See what kind of object we have in list
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh error = dmu_object_info(zfsvfs->z_os,
5cff782560a1c3cf913ba5574a5123a299f3315emh zap.za_first_integer, &doi);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error != 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh continue;
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * We need to re-mark these list entries for deletion,
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * so we pull them back into core and set zp->z_unlinked.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * We may pick up znodes that are already marked for deletion.
5cff782560a1c3cf913ba5574a5123a299f3315emh * This could happen during the purge of an extended attribute
5cff782560a1c3cf913ba5574a5123a299f3315emh * directory. All we need to do is skip over them, since they
5cff782560a1c3cf913ba5574a5123a299f3315emh * are already in the system marked z_unlinked.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error != 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh continue;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_unlinked = B_TRUE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe VN_RELE(ZTOV(zp));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_cursor_fini(&zc);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Delete the entire contents of a directory. Return a count
5cff782560a1c3cf913ba5574a5123a299f3315emh * of the number of entries that could not be deleted.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * NOTE: this function assumes that the directory is inactive,
5cff782560a1c3cf913ba5574a5123a299f3315emh * so there is no need to lock its entries before deletion.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Also, it assumes the directory contents is *only* regular
5cff782560a1c3cf913ba5574a5123a299f3315emh * files.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhstatic int
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_purgedir(znode_t *dzp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_cursor_t zc;
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_attribute_t zap;
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *xzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_t *tx;
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirlock_t dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh int skipped = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
5cff782560a1c3cf913ba5574a5123a299f3315emh (error = zap_cursor_retrieve(&zc, &zap)) == 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_cursor_advance(&zc)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT3U(error, ==, 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT((ZTOV(xzp)->v_type == VREG) ||
5cff782560a1c3cf913ba5574a5123a299f3315emh (ZTOV(xzp)->v_type == VLNK));
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood tx = dmu_tx_create(zfsvfs->z_os);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_bonus(tx, dzp->z_id);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_bonus(tx, xzp->z_id);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood error = dmu_tx_assign(tx, TXG_WAIT);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood if (error) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dmu_tx_abort(tx);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood VN_RELE(ZTOV(xzp));
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood skipped += 1;
5cff782560a1c3cf913ba5574a5123a299f3315emh continue;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh bzero(&dl, sizeof (dl));
5cff782560a1c3cf913ba5574a5123a299f3315emh dl.dl_dzp = dzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh dl.dl_name = zap.za_name;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT3U(error, ==, 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_commit(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh VN_RELE(ZTOV(xzp));
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh zap_cursor_fini(&zc);
173531301317dd4f31e83d4785873141e984ab86Mark Haywood ASSERT(error == ENOENT);
173531301317dd4f31e83d4785873141e984ab86Mark Haywood return (skipped);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe}
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxevoid
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_rmnode(znode_t *zp)
173531301317dd4f31e83d4785873141e984ab86Mark Haywood{
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh objset_t *os = zfsvfs->z_os;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe znode_t *xzp = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh char obj_name[17];
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_t *tx;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint64_t acl_obj;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(ZTOV(zp)->v_count == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(zp->z_phys->zp_links == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * If this is an attribute directory, purge its contents.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (zfs_purgedir(zp) != 0) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe /*
173531301317dd4f31e83d4785873141e984ab86Mark Haywood * Not enough space to delete some xattrs.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Leave it on the unlinked set.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe */
5cff782560a1c3cf913ba5574a5123a299f3315emh return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
173531301317dd4f31e83d4785873141e984ab86Mark Haywood }
173531301317dd4f31e83d4785873141e984ab86Mark Haywood
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * If the file has extended attributes, we're going to unlink
5cff782560a1c3cf913ba5574a5123a299f3315emh * the xattr dir.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zp->z_phys->zp_xattr) {
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(error == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Set up the transaction.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh tx = dmu_tx_create(os);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (xzp) {
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_bonus(tx, xzp->z_id);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (acl_obj)
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
5cff782560a1c3cf913ba5574a5123a299f3315emh error = dmu_tx_assign(tx, TXG_WAIT);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Not enough space to delete the file. Leave it in the
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * unlinked set, leaking it until the fs is remounted (at
5cff782560a1c3cf913ba5574a5123a299f3315emh * which point we'll call zfs_unlinked_drain() to process it).
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_abort(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh return;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (xzp) {
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_buf_will_dirty(xzp->z_dbuf, tx);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe mutex_enter(&xzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe xzp->z_phys->zp_links = 0; /* no more links to it */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe mutex_exit(&xzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_unlinked_add(xzp, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /* Remove this znode from the unlinked set */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zap_remove(os, zfsvfs->z_unlinkedobj,
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_unlinked_hexname(obj_name, zp->z_id), tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT3U(error, ==, 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_znode_delete(zp, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_commit(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (xzp)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe VN_RELE(ZTOV(xzp));
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe}
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Link zp into dl. Can only fail if zp has been unlinked.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe{
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe znode_t *dzp = dl->dl_dzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh vnode_t *vp = ZTOV(zp);
5cff782560a1c3cf913ba5574a5123a299f3315emh int zp_is_dir = (vp->v_type == VDIR);
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dmu_buf_will_dirty(zp->z_dbuf, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&zp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!(flag & ZRENAMING)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zp->z_unlinked) { /* no new links to unlinked zp */
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(!(flag & (ZNEW | ZEXISTS)));
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&zp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (ENOENT);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_links++;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!(flag & ZNEW))
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&zp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dmu_buf_will_dirty(dzp->z_dbuf, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_phys->zp_size++; /* one dirent added */
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
5cff782560a1c3cf913ba5574a5123a299f3315emh 8, 1, &zp->z_id, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(error == 0);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh dnlc_update(ZTOV(dzp), dl->dl_name, vp);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe/*
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Unlink zp from dl, and mark zp for deletion if this was the last link.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * If it's non-NULL, we use it to indicate whether the znode needs deletion,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * and it's the caller's job to do it.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
5cff782560a1c3cf913ba5574a5123a299f3315emh boolean_t *unlinkedp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *dzp = dl->dl_dzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh vnode_t *vp = ZTOV(zp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe int zp_is_dir = (vp->v_type == VDIR);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe boolean_t unlinked = B_FALSE;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe int error;
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dnlc_remove(ZTOV(dzp), dl->dl_name);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (!(flag & ZRENAMING)) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dmu_buf_will_dirty(zp->z_dbuf, tx);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (vn_vfswlock(vp)) /* prevent new mounts on zp */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe return (EBUSY);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (vn_ismntpt(vp)) { /* don't remove mount point */
5cff782560a1c3cf913ba5574a5123a299f3315emh vn_vfsunlock(vp);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (EBUSY);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe mutex_enter(&zp->z_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe mutex_exit(&zp->z_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe vn_vfsunlock(vp);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (EEXIST);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zp->z_phys->zp_links <= zp_is_dir) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_panic_recover("zfs: link count on %s is %u, "
5cff782560a1c3cf913ba5574a5123a299f3315emh "should be at least %u",
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_vnode->v_path ? zp->z_vnode->v_path :
5cff782560a1c3cf913ba5574a5123a299f3315emh "<unknown>", (int)zp->z_phys->zp_links,
5cff782560a1c3cf913ba5574a5123a299f3315emh zp_is_dir + 1);
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_links = zp_is_dir + 1;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe }
5cff782560a1c3cf913ba5574a5123a299f3315emh if (--zp->z_phys->zp_links == zp_is_dir) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_unlinked = B_TRUE;
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_links = 0;
5cff782560a1c3cf913ba5574a5123a299f3315emh unlinked = B_TRUE;
5cff782560a1c3cf913ba5574a5123a299f3315emh } else {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&zp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh vn_vfsunlock(vp);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_buf_will_dirty(dzp->z_dbuf, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_enter(&dzp->z_lock);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dzp->z_phys->zp_size--; /* one dirent removed */
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh mutex_exit(&dzp->z_lock);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(error == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (unlinkedp != NULL)
5cff782560a1c3cf913ba5574a5123a299f3315emh *unlinkedp = unlinked;
5cff782560a1c3cf913ba5574a5123a299f3315emh else if (unlinked)
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_unlinked_add(zp, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Indicate whether the directory is empty. Works with or without z_lock
5cff782560a1c3cf913ba5574a5123a299f3315emh * held, but can only be consider a hint in the latter case. Returns true
5cff782560a1c3cf913ba5574a5123a299f3315emh * if only "." and ".." remain and there's no work in progress.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhboolean_t
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_dirempty(znode_t *dzp)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *xzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_t *tx;
5cff782560a1c3cf913ba5574a5123a299f3315emh uint64_t xoid;
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe *xvpp = NULL;
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh tx = dmu_tx_create(zfsvfs->z_os);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_hold_bonus(tx, zp->z_id);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh error = dmu_tx_assign(tx, zfsvfs->z_assign);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error) {
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_wait(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_abort(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(xzp->z_id == xoid);
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(xzp->z_phys->zp_parent == zp->z_id);
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_buf_will_dirty(zp->z_dbuf, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_xattr = xoid;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
5cff782560a1c3cf913ba5574a5123a299f3315emh dmu_tx_commit(tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh *xvpp = ZTOV(xzp);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Return a znode for the extended attribute directory for zp.
5cff782560a1c3cf913ba5574a5123a299f3315emh * ** If the directory does not already exist, it is created **
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe *
5cff782560a1c3cf913ba5574a5123a299f3315emh * IN: zp - znode to obtain attribute directory from
5cff782560a1c3cf913ba5574a5123a299f3315emh * cr - credentials of caller
5cff782560a1c3cf913ba5574a5123a299f3315emh * flags - flags from the VOP_LOOKUP call
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * OUT: xzpp - pointer to extended attribute znode
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * RETURN: 0 on success
5cff782560a1c3cf913ba5574a5123a299f3315emh * error number on failure
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxezfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe{
5cff782560a1c3cf913ba5574a5123a299f3315emh zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5cff782560a1c3cf913ba5574a5123a299f3315emh znode_t *xzp;
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirlock_t *dl;
5cff782560a1c3cf913ba5574a5123a299f3315emh vattr_t va;
5cff782560a1c3cf913ba5574a5123a299f3315emh int error;
5cff782560a1c3cf913ba5574a5123a299f3315emhtop:
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error)
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh if (xzp != NULL) {
5cff782560a1c3cf913ba5574a5123a299f3315emh *xvpp = ZTOV(xzp);
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirent_unlock(dl);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh ASSERT(zp->z_phys->zp_xattr == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (!(flags & CREATE_XATTR_DIR)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirent_unlock(dl);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (ENOENT);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirent_unlock(dl);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe return (EROFS);
5cff782560a1c3cf913ba5574a5123a299f3315emh }
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh /*
5cff782560a1c3cf913ba5574a5123a299f3315emh * The ability to 'create' files in an attribute
5cff782560a1c3cf913ba5574a5123a299f3315emh * directory comes from the write_xattr permission on the base file.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The ability to 'search' an attribute directory requires
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * read_xattr permission on the base file.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * Once in a directory the ability to read/write attributes
5cff782560a1c3cf913ba5574a5123a299f3315emh * is controlled by the permissions on the attribute file.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emh va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
5cff782560a1c3cf913ba5574a5123a299f3315emh va.va_type = VDIR;
5cff782560a1c3cf913ba5574a5123a299f3315emh va.va_mode = S_IFDIR | S_ISVTX | 0777;
5cff782560a1c3cf913ba5574a5123a299f3315emh va.va_uid = (uid_t)zp->z_phys->zp_uid;
5cff782560a1c3cf913ba5574a5123a299f3315emh va.va_gid = (gid_t)zp->z_phys->zp_gid;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_make_xattrdir(zp, &va, xvpp, cr);
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_dirent_unlock(dl);
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /* NB: we already did dmu_tx_wait() if necessary */
5cff782560a1c3cf913ba5574a5123a299f3315emh goto top;
5cff782560a1c3cf913ba5574a5123a299f3315emh }
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe
5cff782560a1c3cf913ba5574a5123a299f3315emh return (error);
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh/*
5cff782560a1c3cf913ba5574a5123a299f3315emh * Decide whether it is okay to remove within a sticky directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * In sticky directories, write access is not sufficient;
5cff782560a1c3cf913ba5574a5123a299f3315emh * you can remove entries from a directory only if:
5cff782560a1c3cf913ba5574a5123a299f3315emh *
5cff782560a1c3cf913ba5574a5123a299f3315emh * you own the directory,
5cff782560a1c3cf913ba5574a5123a299f3315emh * you own the entry,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * the entry is a plain file and you have write access,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * or you are privileged (checked in secpolicy...).
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe *
5cff782560a1c3cf913ba5574a5123a299f3315emh * The function returns 0 if remove access is granted.
5cff782560a1c3cf913ba5574a5123a299f3315emh */
5cff782560a1c3cf913ba5574a5123a299f3315emhint
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
5cff782560a1c3cf913ba5574a5123a299f3315emh{
5cff782560a1c3cf913ba5574a5123a299f3315emh uid_t uid;
5cff782560a1c3cf913ba5574a5123a299f3315emh
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 ||
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe (uid = crgetuid(cr)) == zdp->z_phys->zp_uid ||
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe uid == zp->z_phys->zp_uid ||
5cff782560a1c3cf913ba5574a5123a299f3315emh (ZTOV(zp)->v_type == VREG &&
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0))
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh else
5cff782560a1c3cf913ba5574a5123a299f3315emh return (secpolicy_vnode_remove(cr));
5cff782560a1c3cf913ba5574a5123a299f3315emh}
5cff782560a1c3cf913ba5574a5123a299f3315emh