zfs_dir.c revision af2c4821c0a23e873f2a63bca4145080aa2183e3
5cff782560a1c3cf913ba5574a5123a299f3315emh * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5cff782560a1c3cf913ba5574a5123a299f3315emh * See the License for the specific language governing permissions
5cff782560a1c3cf913ba5574a5123a299f3315emh * and limitations under the License.
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 * CDDL HEADER END
fcddbe1ff917b2a8770cd3575f46e72601a06df6Mark Haywood * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Use is subject to license terms.
5cff782560a1c3cf913ba5574a5123a299f3315emh#pragma ident "%Z%%M% %I% %E% SMI"
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 * 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 * 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 * Return value: 0 on success or errno on failure.
5cff782560a1c3cf913ba5574a5123a299f3315emh * NOTE: Always checks for, and rejects, '.' and '..'.
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
5cff782560a1c3cf913ba5574a5123a299f3315emh * Verify that we are not trying to lock '.', '..', or '.zfs'
5cff782560a1c3cf913ba5574a5123a299f3315emh (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
5cff782560a1c3cf913ba5574a5123a299f3315emh zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
5cff782560a1c3cf913ba5574a5123a299f3315emh * Wait until there are no locks on this name.
5cff782560a1c3cf913ba5574a5123a299f3315emh for (;;) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Allocate a new dirlock and add it to the list.
5cff782560a1c3cf913ba5574a5123a299f3315emh if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
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 * 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.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } else if (vp) {
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh * Unlock this directory entry and wake anyone who was waiting for it.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Look up an entry in a directory.
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.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * If we are a snapshot mounted under .zfs, return
5cff782560a1c3cf913ba5574a5123a299f3315emh * the vp for the snapshot directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED);
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == 0) {
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywoodstatic char *
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe } while (x != 0);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * unlinked Set (formerly known as the "delete queue") Error Handling
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.
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);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * Clean up any znodes that had no links when we either crashed or
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * (force) umounted the file system.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood * Interate over the contents of the unlinked set.
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
5cff782560a1c3cf913ba5574a5123a299f3315emh * See what kind of object we have in list
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
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.
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
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 * Delete the entire contents of a directory. Return a count
5cff782560a1c3cf913ba5574a5123a299f3315emh * of the number of entries that could not be deleted.
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
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
7f606acec863be28b51fb0f694ca86b41ca76e6dMark Haywood dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
5cff782560a1c3cf913ba5574a5123a299f3315emh * If this is an attribute directory, purge its contents.
5cff782560a1c3cf913ba5574a5123a299f3315emh if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
173531301317dd4f31e83d4785873141e984ab86Mark Haywood * Not enough space to delete some xattrs.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Leave it on the unlinked set.
5cff782560a1c3cf913ba5574a5123a299f3315emh * If the file has extended attributes, we're going to unlink
5cff782560a1c3cf913ba5574a5123a299f3315emh * the xattr dir.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Set up the transaction.
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).
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe xzp->z_phys->zp_links = 0; /* no more links to it */
5cff782560a1c3cf913ba5574a5123a299f3315emh /* Remove this znode from the unlinked set */
5cff782560a1c3cf913ba5574a5123a299f3315emh * Link zp into dl. Can only fail if zp has been unlinked.
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
5cff782560a1c3cf913ba5574a5123a299f3315emh zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
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.
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (vn_vfswlock(vp)) /* prevent new mounts on zp */
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */
5cff782560a1c3cf913ba5574a5123a299f3315emh "should be at least %u",
5cff782560a1c3cf913ba5574a5123a299f3315emh dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */
5cff782560a1c3cf913ba5574a5123a299f3315emh error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
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 return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr))
5cff782560a1c3cf913ba5574a5123a299f3315emh (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "");
5cff782560a1c3cf913ba5574a5123a299f3315emh * Return a znode for the extended attribute directory for zp.
5cff782560a1c3cf913ba5574a5123a299f3315emh * ** If the directory does not already exist, it is created **
5cff782560a1c3cf913ba5574a5123a299f3315emh * IN: zp - znode to obtain attribute directory from
5cff782560a1c3cf913ba5574a5123a299f3315emh * cr - credentials of caller
5cff782560a1c3cf913ba5574a5123a299f3315emh * flags - flags from the VOP_LOOKUP call
5cff782560a1c3cf913ba5574a5123a299f3315emh * OUT: xzpp - pointer to extended attribute znode
5cff782560a1c3cf913ba5574a5123a299f3315emh * RETURN: 0 on success
5cff782560a1c3cf913ba5574a5123a299f3315emh * error number on failure
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxezfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
5cff782560a1c3cf913ba5574a5123a299f3315emh * The ability to 'create' files in an attribute
5cff782560a1c3cf913ba5574a5123a299f3315emh * directory comes from the write_xattr permission on the base file.
5cff782560a1c3cf913ba5574a5123a299f3315emh * The ability to 'search' an attribute directory requires
0e7515250c8395f368aa45fb9acae7c4f8f8b786Eric Saxe * read_xattr permission on the base file.
5cff782560a1c3cf913ba5574a5123a299f3315emh * Once in a directory the ability to read/write attributes
5cff782560a1c3cf913ba5574a5123a299f3315emh * is controlled by the permissions on the attribute file.
5cff782560a1c3cf913ba5574a5123a299f3315emh if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
5cff782560a1c3cf913ba5574a5123a299f3315emh /* NB: we already did dmu_tx_wait() if necessary */
5cff782560a1c3cf913ba5574a5123a299f3315emh * Decide whether it is okay to remove within a sticky directory.
5cff782560a1c3cf913ba5574a5123a299f3315emh * In sticky directories, write access is not sufficient;
5cff782560a1c3cf913ba5574a5123a299f3315emh * you can remove entries from a directory only if:
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...).
5cff782560a1c3cf913ba5574a5123a299f3315emh * The function returns 0 if remove access is granted.
5cff782560a1c3cf913ba5574a5123a299f3315emhzfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
5cff782560a1c3cf913ba5574a5123a299f3315emh if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);
5cff782560a1c3cf913ba5574a5123a299f3315emh return (0);