zfs_dir.c revision a6e57bd4c7a2bf9cc33be939d674d4c7d3e67cce
1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A/*
1N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A#include <sys/types.h>
1N/A#include <sys/param.h>
1N/A#include <sys/time.h>
1N/A#include <sys/systm.h>
1N/A#include <sys/sysmacros.h>
1N/A#include <sys/resource.h>
1N/A#include <sys/vfs.h>
1N/A#include <sys/vnode.h>
1N/A#include <sys/file.h>
1N/A#include <sys/mode.h>
1N/A#include <sys/kmem.h>
1N/A#include <sys/uio.h>
1N/A#include <sys/pathname.h>
1N/A#include <sys/cmn_err.h>
1N/A#include <sys/errno.h>
1N/A#include <sys/stat.h>
1N/A#include <sys/unistd.h>
1N/A#include <sys/sunddi.h>
1N/A#include <sys/random.h>
1N/A#include <sys/policy.h>
1N/A#include <sys/zfs_dir.h>
1N/A#include <sys/zfs_acl.h>
1N/A#include <sys/fs/zfs.h>
1N/A#include "fs/fs_subr.h"
1N/A#include <sys/zap.h>
1N/A#include <sys/dmu.h>
1N/A#include <sys/atomic.h>
1N/A#include <sys/zfs_ctldir.h>
1N/A#include <sys/zfs_fuid.h>
1N/A#include <sys/dnlc.h>
1N/A#include <sys/extdirent.h>
1N/A
1N/A/*
1N/A * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
1N/A * of names after deciding which is the appropriate lookup interface.
1N/A */
1N/Astatic int
1N/Azfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
1N/A boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
1N/A{
1N/A int error;
1N/A
1N/A if (zfsvfs->z_norm) {
1N/A matchtype_t mt = MT_FIRST;
1N/A boolean_t conflict = B_FALSE;
1N/A size_t bufsz = 0;
1N/A char *buf = NULL;
1N/A
1N/A if (rpnp) {
1N/A buf = rpnp->pn_buf;
1N/A bufsz = rpnp->pn_bufsize;
1N/A }
1N/A if (exact)
1N/A mt = MT_EXACT;
1N/A /*
1N/A * In the non-mixed case we only expect there would ever
1N/A * be one match, but we need to use the normalizing lookup.
1N/A */
1N/A error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
1N/A zoid, mt, buf, bufsz, &conflict);
1N/A if (!error && deflags)
1N/A *deflags = conflict ? ED_CASE_CONFLICT : 0;
1N/A } else {
1N/A error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
1N/A }
1N/A *zoid = ZFS_DIRENT_OBJ(*zoid);
1N/A
1N/A if (error == ENOENT && update)
1N/A dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
1N/A
1N/A return (error);
1N/A}
1N/A
1N/A/*
1N/A * Lock a directory entry. A dirlock on <dzp, name> protects that name
1N/A * in dzp's directory zap object. As long as you hold a dirlock, you can
1N/A * assume two things: (1) dzp cannot be reaped, and (2) no other thread
1N/A * can change the zap entry for (i.e. link or unlink) this name.
1N/A *
1N/A * Input arguments:
1N/A * dzp - znode for directory
1N/A * name - name of entry to lock
1N/A * flag - ZNEW: if the entry already exists, fail with EEXIST.
1N/A * ZEXISTS: if the entry does not exist, fail with ENOENT.
1N/A * ZSHARED: allow concurrent access with other ZSHARED callers.
1N/A * ZXATTR: we want dzp's xattr directory
1N/A * ZCILOOK: On a mixed sensitivity file system,
1N/A * this lookup should be case-insensitive.
1N/A * ZCIEXACT: On a purely case-insensitive file system,
1N/A * this lookup should be case-sensitive.
1N/A * ZRENAMING: we are locking for renaming, force narrow locks
1N/A *
1N/A * Output arguments:
1N/A * zpp - pointer to the znode for the entry (NULL if there isn't one)
1N/A * dlpp - pointer to the dirlock for this entry (NULL on error)
1N/A * direntflags - (case-insensitive lookup only)
1N/A * flags if multiple case-sensitive matches exist in directory
1N/A * realpnp - (case-insensitive lookup only)
1N/A * actual name matched within the directory
1N/A *
1N/A * Return value: 0 on success or errno on failure.
1N/A *
1N/A * NOTE: Always checks for, and rejects, '.' and '..'.
1N/A * NOTE: For case-insensitive file systems we take wide locks (see below),
1N/A * but return znode pointers to a single match.
1N/A */
1N/Aint
1N/Azfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
1N/A int flag, int *direntflags, pathname_t *realpnp)
1N/A{
1N/A zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1N/A zfs_dirlock_t *dl;
1N/A boolean_t update;
1N/A boolean_t exact;
1N/A uint64_t zoid;
1N/A vnode_t *vp = NULL;
1N/A int error = 0;
1N/A int cmpflags;
1N/A
1N/A *zpp = NULL;
1N/A *dlpp = NULL;
1N/A
1N/A /*
1N/A * Verify that we are not trying to lock '.', '..', or '.zfs'
1N/A */
1N/A if (name[0] == '.' &&
1N/A (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
1N/A zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
1N/A return (EEXIST);
1N/A
1N/A /*
1N/A * Case sensitivity and normalization preferences are set when
1N/A * the file system is created. These are stored in the
1N/A * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
1N/A * affect what vnodes can be cached in the DNLC, how we
1N/A * perform zap lookups, and the "width" of our dirlocks.
1N/A *
1N/A * A normal dirlock locks a single name. Note that with
1N/A * normalization a name can be composed multiple ways, but
1N/A * when normalized, these names all compare equal. A wide
1N/A * dirlock locks multiple names. We need these when the file
1N/A * system is supporting mixed-mode access. It is sometimes
1N/A * necessary to lock all case permutations of file name at
1N/A * once so that simultaneous case-insensitive/case-sensitive
1N/A * behaves as rationally as possible.
1N/A */
1N/A
1N/A /*
1N/A * Decide if exact matches should be requested when performing
1N/A * a zap lookup on file systems supporting case-insensitive
1N/A * access.
1N/A */
1N/A exact =
1N/A ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
1N/A ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
1N/A
1N/A /*
1N/A * Only look in or update the DNLC if we are looking for the
1N/A * name on a file system that does not require normalization
1N/A * or case folding. We can also look there if we happen to be
1N/A * on a non-normalizing, mixed sensitivity file system IF we
1N/A * are looking for the exact name.
1N/A *
1N/A * Maybe can add TO-UPPERed version of name to dnlc in ci-only
1N/A * case for performance improvement?
1N/A */
1N/A update = !zfsvfs->z_norm ||
1N/A ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
1N/A !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
1N/A
1N/A /*
1N/A * ZRENAMING indicates we are in a situation where we should
1N/A * take narrow locks regardless of the file system's
1N/A * preferences for normalizing and case folding. This will
1N/A * prevent us deadlocking trying to grab the same wide lock
1N/A * twice if the two names happen to be case-insensitive
1N/A * matches.
1N/A */
1N/A if (flag & ZRENAMING)
1N/A cmpflags = 0;
1N/A else
1N/A cmpflags = zfsvfs->z_norm;
1N/A
1N/A /*
1N/A * Wait until there are no locks on this name.
1N/A */
1N/A rw_enter(&dzp->z_name_lock, RW_READER);
1N/A mutex_enter(&dzp->z_lock);
1N/A for (;;) {
1N/A if (dzp->z_unlinked) {
1N/A mutex_exit(&dzp->z_lock);
1N/A rw_exit(&dzp->z_name_lock);
1N/A return (ENOENT);
1N/A }
1N/A for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
1N/A if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
1N/A U8_UNICODE_LATEST, &error) == 0) || error != 0)
1N/A break;
1N/A }
1N/A if (error != 0) {
1N/A mutex_exit(&dzp->z_lock);
1N/A rw_exit(&dzp->z_name_lock);
1N/A return (ENOENT);
1N/A }
1N/A if (dl == NULL) {
1N/A /*
1N/A * Allocate a new dirlock and add it to the list.
1N/A */
1N/A dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
1N/A cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
1N/A dl->dl_name = name;
1N/A dl->dl_sharecnt = 0;
1N/A dl->dl_namesize = 0;
1N/A dl->dl_dzp = dzp;
1N/A dl->dl_next = dzp->z_dirlocks;
1N/A dzp->z_dirlocks = dl;
1N/A break;
1N/A }
1N/A if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
1N/A break;
1N/A cv_wait(&dl->dl_cv, &dzp->z_lock);
1N/A }
1N/A
1N/A if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
1N/A /*
1N/A * We're the second shared reference to dl. Make a copy of
1N/A * dl_name in case the first thread goes away before we do.
1N/A * Note that we initialize the new name before storing its
1N/A * pointer into dl_name, because the first thread may load
1N/A * dl->dl_name at any time. He'll either see the old value,
1N/A * which is his, or the new shared copy; either is OK.
1N/A */
1N/A dl->dl_namesize = strlen(dl->dl_name) + 1;
1N/A name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
1N/A bcopy(dl->dl_name, name, dl->dl_namesize);
1N/A dl->dl_name = name;
1N/A }
1N/A
1N/A mutex_exit(&dzp->z_lock);
1N/A
1N/A /*
1N/A * We have a dirlock on the name. (Note that it is the dirlock,
1N/A * not the dzp's z_lock, that protects the name in the zap object.)
1N/A * See if there's an object by this name; if so, put a hold on it.
1N/A */
1N/A if (flag & ZXATTR) {
1N/A zoid = dzp->z_phys->zp_xattr;
1N/A error = (zoid == 0 ? ENOENT : 0);
1N/A } else {
1N/A if (update)
1N/A vp = dnlc_lookup(ZTOV(dzp), name);
1N/A if (vp == DNLC_NO_VNODE) {
1N/A VN_RELE(vp);
1N/A error = ENOENT;
1N/A } else if (vp) {
1N/A if (flag & ZNEW) {
1N/A zfs_dirent_unlock(dl);
1N/A VN_RELE(vp);
1N/A return (EEXIST);
1N/A }
1N/A *dlpp = dl;
1N/A *zpp = VTOZ(vp);
1N/A return (0);
1N/A } else {
1N/A error = zfs_match_find(zfsvfs, dzp, name, exact,
1N/A update, direntflags, realpnp, &zoid);
1N/A }
1N/A }
1N/A if (error) {
1N/A if (error != ENOENT || (flag & ZEXISTS)) {
1N/A zfs_dirent_unlock(dl);
1N/A return (error);
1N/A }
1N/A } else {
1N/A if (flag & ZNEW) {
1N/A zfs_dirent_unlock(dl);
1N/A return (EEXIST);
1N/A }
1N/A error = zfs_zget(zfsvfs, zoid, zpp);
1N/A if (error) {
1N/A zfs_dirent_unlock(dl);
1N/A return (error);
1N/A }
1N/A if (!(flag & ZXATTR) && update)
1N/A dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
1N/A }
1N/A
1N/A *dlpp = dl;
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Unlock this directory entry and wake anyone who was waiting for it.
1N/A */
1N/Avoid
1N/Azfs_dirent_unlock(zfs_dirlock_t *dl)
1N/A{
1N/A znode_t *dzp = dl->dl_dzp;
1N/A zfs_dirlock_t **prev_dl, *cur_dl;
1N/A
1N/A mutex_enter(&dzp->z_lock);
1N/A rw_exit(&dzp->z_name_lock);
1N/A if (dl->dl_sharecnt > 1) {
1N/A dl->dl_sharecnt--;
1N/A mutex_exit(&dzp->z_lock);
1N/A return;
1N/A }
1N/A prev_dl = &dzp->z_dirlocks;
1N/A while ((cur_dl = *prev_dl) != dl)
1N/A prev_dl = &cur_dl->dl_next;
1N/A *prev_dl = dl->dl_next;
1N/A cv_broadcast(&dl->dl_cv);
1N/A mutex_exit(&dzp->z_lock);
1N/A
1N/A if (dl->dl_namesize != 0)
1N/A kmem_free(dl->dl_name, dl->dl_namesize);
1N/A cv_destroy(&dl->dl_cv);
1N/A kmem_free(dl, sizeof (*dl));
1N/A}
1N/A
1N/A/*
1N/A * Look up an entry in a directory.
1N/A *
1N/A * NOTE: '.' and '..' are handled as special cases because
1N/A * no directory entries are actually stored for them. If this is
1N/A * the root of a filesystem, then '.zfs' is also treated as a
1N/A * special pseudo-directory.
1N/A */
1N/Aint
1N/Azfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
1N/A int *deflg, pathname_t *rpnp)
1N/A{
1N/A zfs_dirlock_t *dl;
1N/A znode_t *zp;
1N/A int error = 0;
1N/A
1N/A if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
1N/A *vpp = ZTOV(dzp);
1N/A VN_HOLD(*vpp);
1N/A } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
1N/A zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1N/A /*
1N/A * If we are a snapshot mounted under .zfs, return
1N/A * the vp for the snapshot directory.
1N/A */
1N/A if (dzp->z_phys->zp_parent == dzp->z_id &&
1N/A zfsvfs->z_parent != zfsvfs) {
1N/A error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
1N/A "snapshot", vpp, NULL, 0, NULL, kcred,
1N/A NULL, NULL, NULL);
1N/A return (error);
1N/A }
1N/A rw_enter(&dzp->z_parent_lock, RW_READER);
1N/A error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
1N/A if (error == 0)
1N/A *vpp = ZTOV(zp);
1N/A rw_exit(&dzp->z_parent_lock);
1N/A } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
1N/A *vpp = zfsctl_root(dzp);
1N/A } else {
1N/A int zf;
1N/A
1N/A zf = ZEXISTS | ZSHARED;
1N/A if (flags & FIGNORECASE)
1N/A zf |= ZCILOOK;
1N/A
1N/A error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
1N/A if (error == 0) {
1N/A *vpp = ZTOV(zp);
1N/A zfs_dirent_unlock(dl);
1N/A dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
1N/A }
1N/A rpnp = NULL;
1N/A }
1N/A
1N/A if ((flags & FIGNORECASE) && rpnp && !error)
1N/A (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
1N/A
1N/A return (error);
1N/A}
1N/A
1N/A/*
1N/A * unlinked Set (formerly known as the "delete queue") Error Handling
1N/A *
1N/A * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
1N/A * don't specify the name of the entry that we will be manipulating. We
1N/A * also fib and say that we won't be adding any new entries to the
1N/A * unlinked set, even though we might (this is to lower the minimum file
1N/A * size that can be deleted in a full filesystem). So on the small
1N/A * chance that the nlink list is using a fat zap (ie. has more than
1N/A * 2000 entries), we *may* not pre-read a block that's needed.
1N/A * Therefore it is remotely possible for some of the assertions
1N/A * regarding the unlinked set below to fail due to i/o error. On a
1N/A * nondebug system, this will result in the space being leaked.
1N/A */
1N/Avoid
1N/Azfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
1N/A{
1N/A zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1N/A
1N/A ASSERT(zp->z_unlinked);
1N/A ASSERT3U(zp->z_phys->zp_links, ==, 0);
1N/A
1N/A VERIFY3U(0, ==,
1N/A zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
1N/A}
1N/A
1N/A/*
1N/A * Clean up any znodes that had no links when we either crashed or
1N/A * (force) umounted the file system.
1N/A */
1N/Avoid
1N/Azfs_unlinked_drain(zfsvfs_t *zfsvfs)
1N/A{
1N/A zap_cursor_t zc;
1N/A zap_attribute_t zap;
1N/A dmu_object_info_t doi;
1N/A znode_t *zp;
1N/A int error;
1N/A
1N/A /*
1N/A * Interate over the contents of the unlinked set.
1N/A */
1N/A for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
1N/A zap_cursor_retrieve(&zc, &zap) == 0;
1N/A zap_cursor_advance(&zc)) {
1N/A
1N/A /*
1N/A * See what kind of object we have in list
1N/A */
1N/A
1N/A error = dmu_object_info(zfsvfs->z_os,
1N/A zap.za_first_integer, &doi);
1N/A if (error != 0)
1N/A continue;
1N/A
1N/A ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
1N/A (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
1N/A /*
1N/A * We need to re-mark these list entries for deletion,
1N/A * so we pull them back into core and set zp->z_unlinked.
1N/A */
1N/A error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
1N/A
1N/A /*
1N/A * We may pick up znodes that are already marked for deletion.
1N/A * This could happen during the purge of an extended attribute
1N/A * directory. All we need to do is skip over them, since they
1N/A * are already in the system marked z_unlinked.
1N/A */
1N/A if (error != 0)
1N/A continue;
1N/A
1N/A zp->z_unlinked = B_TRUE;
1N/A VN_RELE(ZTOV(zp));
1N/A }
1N/A zap_cursor_fini(&zc);
1N/A}
1N/A
1N/A/*
1N/A * Delete the entire contents of a directory. Return a count
1N/A * of the number of entries that could not be deleted. If we encounter
1N/A * an error, return a count of at least one so that the directory stays
1N/A * in the unlinked set.
1N/A *
1N/A * NOTE: this function assumes that the directory is inactive,
1N/A * so there is no need to lock its entries before deletion.
1N/A * Also, it assumes the directory contents is *only* regular
1N/A * files.
1N/A */
1N/Astatic int
1N/Azfs_purgedir(znode_t *dzp)
1N/A{
1N/A zap_cursor_t zc;
1N/A zap_attribute_t zap;
1N/A znode_t *xzp;
1N/A dmu_tx_t *tx;
1N/A zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1N/A zfs_dirlock_t dl;
1N/A int skipped = 0;
1N/A int error;
1N/A
1N/A for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
1N/A (error = zap_cursor_retrieve(&zc, &zap)) == 0;
1N/A zap_cursor_advance(&zc)) {
1N/A error = zfs_zget(zfsvfs,
1N/A ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
1N/A if (error) {
1N/A skipped += 1;
1N/A continue;
1N/A }
1N/A
1N/A ASSERT((ZTOV(xzp)->v_type == VREG) ||
1N/A (ZTOV(xzp)->v_type == VLNK));
1N/A
1N/A tx = dmu_tx_create(zfsvfs->z_os);
1N/A dmu_tx_hold_bonus(tx, dzp->z_id);
1N/A dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
1N/A dmu_tx_hold_bonus(tx, xzp->z_id);
1N/A dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1N/A error = dmu_tx_assign(tx, TXG_WAIT);
1N/A if (error) {
1N/A dmu_tx_abort(tx);
1N/A VN_RELE(ZTOV(xzp));
1N/A skipped += 1;
1N/A continue;
1N/A }
1N/A bzero(&dl, sizeof (dl));
1N/A dl.dl_dzp = dzp;
1N/A dl.dl_name = zap.za_name;
1N/A
1N/A error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
1N/A if (error)
1N/A skipped += 1;
1N/A dmu_tx_commit(tx);
1N/A
1N/A VN_RELE(ZTOV(xzp));
1N/A }
1N/A zap_cursor_fini(&zc);
1N/A if (error != ENOENT)
1N/A skipped += 1;
1N/A return (skipped);
1N/A}
1N/A
1N/Avoid
1N/Azfs_rmnode(znode_t *zp)
1N/A{
1N/A zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1N/A objset_t *os = zfsvfs->z_os;
1N/A znode_t *xzp = NULL;
1N/A dmu_tx_t *tx;
1N/A uint64_t acl_obj;
1N/A int error;
1N/A
1N/A ASSERT(ZTOV(zp)->v_count == 0);
1N/A ASSERT(zp->z_phys->zp_links == 0);
1N/A
1N/A /*
1N/A * If this is a ZIL replay then leave the object in the unlinked set.
1N/A * Otherwise we can get a deadlock, because the delete can be
1N/A * quite large and span multiple tx's and txgs, but each replay
1N/A * creates a tx to atomically run the replay function and mark the
1N/A * replay record as complete. We deadlock trying to start a tx in
1N/A * a new txg to further the deletion but can't because the replay
1N/A * tx hasn't finished.
1N/A *
1N/A * We actually delete the object if we get a failure to create an
1N/A * object in zil_replay_log_record(), or after calling zil_replay().
1N/A */
1N/A if (zfsvfs->z_assign >= TXG_INITIAL) {
1N/A zfs_znode_dmu_fini(zp);
1N/A zfs_znode_free(zp);
1N/A return;
1N/A }
1N/A
1N/A /*
1N/A * If this is an attribute directory, purge its contents.
1N/A */
1N/A if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
1N/A if (zfs_purgedir(zp) != 0) {
1N/A /*
1N/A * Not enough space to delete some xattrs.
1N/A * Leave it in the unlinked set.
1N/A */
1N/A zfs_znode_dmu_fini(zp);
1N/A zfs_znode_free(zp);
1N/A return;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * Free up all the data in the file.
1N/A */
1N/A error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
1N/A if (error) {
1N/A /*
1N/A * Not enough space. Leave the file in the unlinked set.
1N/A */
1N/A zfs_znode_dmu_fini(zp);
1N/A zfs_znode_free(zp);
1N/A return;
1N/A }
1N/A
1N/A /*
1N/A * If the file has extended attributes, we're going to unlink
1N/A * the xattr dir.
1N/A */
1N/A if (zp->z_phys->zp_xattr) {
1N/A error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1N/A ASSERT(error == 0);
1N/A }
1N/A
1N/A acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1N/A
1N/A /*
1N/A * Set up the final transaction.
1N/A */
1N/A tx = dmu_tx_create(os);
1N/A dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1N/A dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1N/A if (xzp) {
1N/A dmu_tx_hold_bonus(tx, xzp->z_id);
1N/A dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
1N/A }
1N/A if (acl_obj)
1N/A dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1N/A error = dmu_tx_assign(tx, TXG_WAIT);
1N/A if (error) {
1N/A /*
1N/A * Not enough space to delete the file. Leave it in the
1N/A * unlinked set, leaking it until the fs is remounted (at
1N/A * which point we'll call zfs_unlinked_drain() to process it).
1N/A */
1N/A dmu_tx_abort(tx);
1N/A zfs_znode_dmu_fini(zp);
1N/A zfs_znode_free(zp);
1N/A goto out;
1N/A }
1N/A
1N/A if (xzp) {
1N/A dmu_buf_will_dirty(xzp->z_dbuf, tx);
1N/A mutex_enter(&xzp->z_lock);
1N/A xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
1N/A xzp->z_phys->zp_links = 0; /* no more links to it */
1N/A mutex_exit(&xzp->z_lock);
1N/A zfs_unlinked_add(xzp, tx);
1N/A }
1N/A
1N/A /* Remove this znode from the unlinked set */
1N/A VERIFY3U(0, ==,
1N/A zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
1N/A
1N/A zfs_znode_delete(zp, tx);
1N/A
1N/A dmu_tx_commit(tx);
1N/Aout:
1N/A if (xzp)
1N/A VN_RELE(ZTOV(xzp));
1N/A}
1N/A
1N/Astatic uint64_t
1N/Azfs_dirent(znode_t *zp)
1N/A{
1N/A uint64_t de = zp->z_id;
1N/A if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
1N/A de |= IFTODT((zp)->z_phys->zp_mode) << 60;
1N/A return (de);
1N/A}
1N/A
1N/A/*
1N/A * Link zp into dl. Can only fail if zp has been unlinked.
1N/A */
1N/Aint
1N/Azfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
1N/A{
1N/A znode_t *dzp = dl->dl_dzp;
1N/A vnode_t *vp = ZTOV(zp);
1N/A uint64_t value;
1N/A int zp_is_dir = (vp->v_type == VDIR);
1N/A int error;
1N/A
1N/A dmu_buf_will_dirty(zp->z_dbuf, tx);
1N/A mutex_enter(&zp->z_lock);
1N/A
1N/A if (!(flag & ZRENAMING)) {
1N/A if (zp->z_unlinked) { /* no new links to unlinked zp */
1N/A ASSERT(!(flag & (ZNEW | ZEXISTS)));
1N/A mutex_exit(&zp->z_lock);
1N/A return (ENOENT);
1N/A }
1N/A zp->z_phys->zp_links++;
1N/A }
1N/A zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */
1N/A
1N/A if (!(flag & ZNEW))
1N/A zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
1N/A mutex_exit(&zp->z_lock);
1N/A
1N/A dmu_buf_will_dirty(dzp->z_dbuf, tx);
1N/A mutex_enter(&dzp->z_lock);
1N/A dzp->z_phys->zp_size++; /* one dirent added */
1N/A dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */
1N/A zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
1N/A mutex_exit(&dzp->z_lock);
1N/A
1N/A value = zfs_dirent(zp);
1N/A error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
1N/A 8, 1, &value, tx);
1N/A ASSERT(error == 0);
1N/A
1N/A dnlc_update(ZTOV(dzp), dl->dl_name, vp);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Unlink zp from dl, and mark zp for deletion if this was the last link.
1N/A * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
1N/A * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
1N/A * If it's non-NULL, we use it to indicate whether the znode needs deletion,
1N/A * and it's the caller's job to do it.
1N/A */
1N/Aint
1N/Azfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
1N/A boolean_t *unlinkedp)
1N/A{
1N/A znode_t *dzp = dl->dl_dzp;
1N/A vnode_t *vp = ZTOV(zp);
1N/A int zp_is_dir = (vp->v_type == VDIR);
1N/A boolean_t unlinked = B_FALSE;
1N/A int error;
1N/A
1N/A dnlc_remove(ZTOV(dzp), dl->dl_name);
1N/A
1N/A if (!(flag & ZRENAMING)) {
1N/A dmu_buf_will_dirty(zp->z_dbuf, tx);
1N/A
1N/A if (vn_vfswlock(vp)) /* prevent new mounts on zp */
1N/A return (EBUSY);
1N/A
1N/A if (vn_ismntpt(vp)) { /* don't remove mount point */
1N/A vn_vfsunlock(vp);
1N/A return (EBUSY);
1N/A }
1N/A
1N/A mutex_enter(&zp->z_lock);
1N/A if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */
1N/A mutex_exit(&zp->z_lock);
1N/A vn_vfsunlock(vp);
1N/A return (EEXIST);
1N/A }
1N/A if (zp->z_phys->zp_links <= zp_is_dir) {
1N/A zfs_panic_recover("zfs: link count on %s is %u, "
1N/A "should be at least %u",
1N/A zp->z_vnode->v_path ? zp->z_vnode->v_path :
1N/A "<unknown>", (int)zp->z_phys->zp_links,
1N/A zp_is_dir + 1);
1N/A zp->z_phys->zp_links = zp_is_dir + 1;
1N/A }
1N/A if (--zp->z_phys->zp_links == zp_is_dir) {
1N/A zp->z_unlinked = B_TRUE;
1N/A zp->z_phys->zp_links = 0;
1N/A unlinked = B_TRUE;
1N/A } else {
1N/A zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
1N/A }
1N/A mutex_exit(&zp->z_lock);
1N/A vn_vfsunlock(vp);
1N/A }
1N/A
1N/A dmu_buf_will_dirty(dzp->z_dbuf, tx);
1N/A mutex_enter(&dzp->z_lock);
1N/A dzp->z_phys->zp_size--; /* one dirent removed */
1N/A dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */
1N/A zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
1N/A mutex_exit(&dzp->z_lock);
1N/A
1N/A if (zp->z_zfsvfs->z_norm) {
1N/A if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
1N/A (flag & ZCIEXACT)) ||
1N/A ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
1N/A !(flag & ZCILOOK)))
1N/A error = zap_remove_norm(zp->z_zfsvfs->z_os,
1N/A dzp->z_id, dl->dl_name, MT_EXACT, tx);
1N/A else
1N/A error = zap_remove_norm(zp->z_zfsvfs->z_os,
1N/A dzp->z_id, dl->dl_name, MT_FIRST, tx);
1N/A } else {
1N/A error = zap_remove(zp->z_zfsvfs->z_os,
1N/A dzp->z_id, dl->dl_name, tx);
1N/A }
1N/A ASSERT(error == 0);
1N/A
1N/A if (unlinkedp != NULL)
1N/A *unlinkedp = unlinked;
1N/A else if (unlinked)
1N/A zfs_unlinked_add(zp, tx);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Indicate whether the directory is empty. Works with or without z_lock
1N/A * held, but can only be consider a hint in the latter case. Returns true
1N/A * if only "." and ".." remain and there's no work in progress.
1N/A */
1N/Aboolean_t
1N/Azfs_dirempty(znode_t *dzp)
1N/A{
1N/A return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
1N/A}
1N/A
1N/Aint
1N/Azfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
1N/A{
1N/A zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1N/A znode_t *xzp;
1N/A dmu_tx_t *tx;
1N/A int error;
1N/A zfs_fuid_info_t *fuidp = NULL;
1N/A
1N/A *xvpp = NULL;
1N/A
1N/A if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
1N/A return (error);
1N/A
1N/A tx = dmu_tx_create(zfsvfs->z_os);
1N/A dmu_tx_hold_bonus(tx, zp->z_id);
1N/A dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1N/A if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
1N/A if (zfsvfs->z_fuid_obj == 0) {
1N/A dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1N/A dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1N/A FUID_SIZE_ESTIMATE(zfsvfs));
1N/A dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1N/A } else {
1N/A dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1N/A dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1N/A FUID_SIZE_ESTIMATE(zfsvfs));
1N/A }
1N/A }
1N/A error = dmu_tx_assign(tx, zfsvfs->z_assign);
1N/A if (error) {
1N/A if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
1N/A dmu_tx_wait(tx);
1N/A dmu_tx_abort(tx);
1N/A return (error);
1N/A }
1N/A zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp);
1N/A ASSERT(xzp->z_phys->zp_parent == zp->z_id);
1N/A dmu_buf_will_dirty(zp->z_dbuf, tx);
1N/A zp->z_phys->zp_xattr = xzp->z_id;
1N/A
1N/A (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1N/A xzp, "", NULL, fuidp, vap);
1N/A if (fuidp)
1N/A zfs_fuid_info_free(fuidp);
1N/A dmu_tx_commit(tx);
1N/A
1N/A *xvpp = ZTOV(xzp);
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Return a znode for the extended attribute directory for zp.
1N/A * ** If the directory does not already exist, it is created **
1N/A *
1N/A * IN: zp - znode to obtain attribute directory from
1N/A * cr - credentials of caller
1N/A * flags - flags from the VOP_LOOKUP call
1N/A *
1N/A * OUT: xzpp - pointer to extended attribute znode
1N/A *
1N/A * RETURN: 0 on success
1N/A * error number on failure
1N/A */
1N/Aint
1N/Azfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
1N/A{
1N/A zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1N/A znode_t *xzp;
1N/A zfs_dirlock_t *dl;
1N/A vattr_t va;
1N/A int error;
1N/Atop:
1N/A error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1N/A if (error)
1N/A return (error);
1N/A
1N/A if (xzp != NULL) {
1N/A *xvpp = ZTOV(xzp);
1N/A zfs_dirent_unlock(dl);
1N/A return (0);
1N/A }
1N/A
1N/A ASSERT(zp->z_phys->zp_xattr == 0);
1N/A
1N/A if (!(flags & CREATE_XATTR_DIR)) {
1N/A zfs_dirent_unlock(dl);
1N/A return (ENOENT);
1N/A }
1N/A
1N/A if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
1N/A zfs_dirent_unlock(dl);
1N/A return (EROFS);
1N/A }
1N/A
1N/A /*
1N/A * The ability to 'create' files in an attribute
1N/A * directory comes from the write_xattr permission on the base file.
1N/A *
1N/A * The ability to 'search' an attribute directory requires
1N/A * read_xattr permission on the base file.
1N/A *
1N/A * Once in a directory the ability to read/write attributes
1N/A * is controlled by the permissions on the attribute file.
1N/A */
1N/A va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
1N/A va.va_type = VDIR;
1N/A va.va_mode = S_IFDIR | S_ISVTX | 0777;
1N/A zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1N/A
1N/A error = zfs_make_xattrdir(zp, &va, xvpp, cr);
1N/A zfs_dirent_unlock(dl);
1N/A
1N/A if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1N/A /* NB: we already did dmu_tx_wait() if necessary */
1N/A goto top;
1N/A }
1N/A
1N/A return (error);
1N/A}
1N/A
1N/A/*
1N/A * Decide whether it is okay to remove within a sticky directory.
1N/A *
1N/A * In sticky directories, write access is not sufficient;
1N/A * you can remove entries from a directory only if:
1N/A *
1N/A * you own the directory,
1N/A * you own the entry,
1N/A * the entry is a plain file and you have write access,
1N/A * or you are privileged (checked in secpolicy...).
1N/A *
1N/A * The function returns 0 if remove access is granted.
1N/A */
1N/Aint
1N/Azfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1N/A{
1N/A uid_t uid;
1N/A uid_t downer;
1N/A uid_t fowner;
1N/A zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1N/A
1N/A if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */
1N/A return (0);
1N/A
1N/A if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
1N/A return (0);
1N/A
1N/A downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
1N/A fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
1N/A
1N/A if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1N/A (ZTOV(zp)->v_type == VREG &&
1N/A zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1N/A return (0);
1N/A else
1N/A return (secpolicy_vnode_remove(cr));
1N/A}
1N/A