Changeset View
Changeset View
Standalone View
Standalone View
sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_dir.c
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | |||||
#include <sys/zfs_ctldir.h> | #include <sys/zfs_ctldir.h> | ||||
#include <sys/zfs_fuid.h> | #include <sys/zfs_fuid.h> | ||||
#include <sys/sa.h> | #include <sys/sa.h> | ||||
#include <sys/zfs_sa.h> | #include <sys/zfs_sa.h> | ||||
#include <sys/dnlc.h> | #include <sys/dnlc.h> | ||||
#include <sys/extdirent.h> | #include <sys/extdirent.h> | ||||
/* | /* | ||||
* zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups | * zfs_match_find() is used by zfs_dirent_lookup() to peform zap lookups | ||||
* of names after deciding which is the appropriate lookup interface. | * of names after deciding which is the appropriate lookup interface. | ||||
*/ | */ | ||||
static int | static int | ||||
zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact, | zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name, | ||||
boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid) | boolean_t exact, uint64_t *zoid) | ||||
{ | { | ||||
int error; | int error; | ||||
if (zfsvfs->z_norm) { | if (zfsvfs->z_norm) { | ||||
matchtype_t mt = MT_FIRST; | matchtype_t mt = exact? MT_EXACT : MT_FIRST; | ||||
boolean_t conflict = B_FALSE; | |||||
size_t bufsz = 0; | |||||
char *buf = NULL; | |||||
if (rpnp) { | |||||
buf = rpnp->pn_buf; | |||||
bufsz = rpnp->pn_bufsize; | |||||
} | |||||
if (exact) | |||||
mt = MT_EXACT; | |||||
/* | /* | ||||
* In the non-mixed case we only expect there would ever | * In the non-mixed case we only expect there would ever | ||||
* be one match, but we need to use the normalizing lookup. | * be one match, but we need to use the normalizing lookup. | ||||
*/ | */ | ||||
error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1, | error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1, | ||||
zoid, mt, buf, bufsz, &conflict); | zoid, mt, NULL, 0, NULL); | ||||
if (!error && deflags) | |||||
*deflags = conflict ? ED_CASE_CONFLICT : 0; | |||||
} else { | } else { | ||||
error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid); | error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid); | ||||
} | } | ||||
*zoid = ZFS_DIRENT_OBJ(*zoid); | *zoid = ZFS_DIRENT_OBJ(*zoid); | ||||
if (error == ENOENT && update) | |||||
dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE); | |||||
return (error); | return (error); | ||||
} | } | ||||
/* | /* | ||||
* Lock a directory entry. A dirlock on <dzp, name> protects that name | * Look up a directory entry under a locked vnode. | ||||
* in dzp's directory zap object. As long as you hold a dirlock, you can | * dvp being locked gives us a guarantee that there are no concurrent | ||||
* assume two things: (1) dzp cannot be reaped, and (2) no other thread | * modification of the directory and, thus, if a node can be found in | ||||
* can change the zap entry for (i.e. link or unlink) this name. | * the directory, then it must not be unlinked. | ||||
* | * | ||||
* Input arguments: | * Input arguments: | ||||
* dzp - znode for directory | * dzp - znode for directory | ||||
* name - name of entry to lock | * name - name of entry to lock | ||||
* flag - ZNEW: if the entry already exists, fail with EEXIST. | * flag - ZNEW: if the entry already exists, fail with EEXIST. | ||||
* ZEXISTS: if the entry does not exist, fail with ENOENT. | * ZEXISTS: if the entry does not exist, fail with ENOENT. | ||||
* ZSHARED: allow concurrent access with other ZSHARED callers. | |||||
* ZXATTR: we want dzp's xattr directory | * ZXATTR: we want dzp's xattr directory | ||||
* ZCILOOK: On a mixed sensitivity file system, | |||||
* this lookup should be case-insensitive. | |||||
* ZCIEXACT: On a purely case-insensitive file system, | |||||
* this lookup should be case-sensitive. | |||||
* ZRENAMING: we are locking for renaming, force narrow locks | |||||
* ZHAVELOCK: Don't grab the z_name_lock for this call. The | |||||
* current thread already holds it. | |||||
* | * | ||||
* Output arguments: | * Output arguments: | ||||
* zpp - pointer to the znode for the entry (NULL if there isn't one) | * zpp - pointer to the znode for the entry (NULL if there isn't one) | ||||
* dlpp - pointer to the dirlock for this entry (NULL on error) | |||||
* direntflags - (case-insensitive lookup only) | |||||
* flags if multiple case-sensitive matches exist in directory | |||||
* realpnp - (case-insensitive lookup only) | |||||
* actual name matched within the directory | |||||
* | * | ||||
* Return value: 0 on success or errno on failure. | * Return value: 0 on success or errno on failure. | ||||
* | * | ||||
* NOTE: Always checks for, and rejects, '.' and '..'. | * NOTE: Always checks for, and rejects, '.' and '..'. | ||||
* NOTE: For case-insensitive file systems we take wide locks (see below), | |||||
* but return znode pointers to a single match. | |||||
*/ | */ | ||||
int | int | ||||
zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp, | zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag) | ||||
int flag, int *direntflags, pathname_t *realpnp) | |||||
{ | { | ||||
zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | ||||
zfs_dirlock_t *dl; | |||||
boolean_t update; | |||||
boolean_t exact; | boolean_t exact; | ||||
uint64_t zoid; | uint64_t zoid; | ||||
vnode_t *vp = NULL; | vnode_t *vp = NULL; | ||||
int error = 0; | int error = 0; | ||||
int cmpflags; | |||||
ASSERT_VOP_LOCKED(ZTOV(dzp), "zfs_dirent_lookup"); | |||||
*zpp = NULL; | *zpp = NULL; | ||||
*dlpp = NULL; | |||||
/* | /* | ||||
* Verify that we are not trying to lock '.', '..', or '.zfs' | * Verify that we are not trying to lock '.', '..', or '.zfs' | ||||
*/ | */ | ||||
if (name[0] == '.' && | if (name[0] == '.' && | ||||
(name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) || | (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) || | ||||
zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) | zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) | ||||
return (SET_ERROR(EEXIST)); | return (SET_ERROR(EEXIST)); | ||||
/* | /* | ||||
* Case sensitivity and normalization preferences are set when | * Case sensitivity and normalization preferences are set when | ||||
* the file system is created. These are stored in the | * the file system is created. These are stored in the | ||||
* zfsvfs->z_case and zfsvfs->z_norm fields. These choices | * zfsvfs->z_case and zfsvfs->z_norm fields. These choices | ||||
* affect what vnodes can be cached in the DNLC, how we | * affect how we perform zap lookups. | ||||
* perform zap lookups, and the "width" of our dirlocks. | |||||
* | * | ||||
* A normal dirlock locks a single name. Note that with | |||||
* normalization a name can be composed multiple ways, but | |||||
* when normalized, these names all compare equal. A wide | |||||
* dirlock locks multiple names. We need these when the file | |||||
* system is supporting mixed-mode access. It is sometimes | |||||
* necessary to lock all case permutations of file name at | |||||
* once so that simultaneous case-insensitive/case-sensitive | |||||
* behaves as rationally as possible. | |||||
*/ | |||||
/* | |||||
* Decide if exact matches should be requested when performing | * Decide if exact matches should be requested when performing | ||||
* a zap lookup on file systems supporting case-insensitive | * a zap lookup on file systems supporting case-insensitive | ||||
* access. | * access. | ||||
*/ | |||||
exact = | |||||
((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) || | |||||
((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK)); | |||||
/* | |||||
* Only look in or update the DNLC if we are looking for the | |||||
* name on a file system that does not require normalization | |||||
* or case folding. We can also look there if we happen to be | |||||
* on a non-normalizing, mixed sensitivity file system IF we | |||||
* are looking for the exact name. | |||||
* | * | ||||
* Maybe can add TO-UPPERed version of name to dnlc in ci-only | * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE | ||||
* case for performance improvement? | * because in that case MT_EXACT and MT_FIRST should produce exactly | ||||
* the same result. | |||||
*/ | */ | ||||
update = !zfsvfs->z_norm || | exact = zfsvfs->z_case == ZFS_CASE_MIXED; | ||||
((zfsvfs->z_case == ZFS_CASE_MIXED) && | |||||
!(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK)); | |||||
/* | if (dzp->z_unlinked && !(flag & ZXATTR)) | ||||
* ZRENAMING indicates we are in a situation where we should | return (ENOENT); | ||||
* take narrow locks regardless of the file system's | |||||
* preferences for normalizing and case folding. This will | |||||
* prevent us deadlocking trying to grab the same wide lock | |||||
* twice if the two names happen to be case-insensitive | |||||
* matches. | |||||
*/ | |||||
if (flag & ZRENAMING) | |||||
cmpflags = 0; | |||||
else | |||||
cmpflags = zfsvfs->z_norm; | |||||
/* | |||||
* Wait until there are no locks on this name. | |||||
* | |||||
* Don't grab the the lock if it is already held. However, cannot | |||||
* have both ZSHARED and ZHAVELOCK together. | |||||
*/ | |||||
ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK)); | |||||
if (!(flag & ZHAVELOCK)) | |||||
rw_enter(&dzp->z_name_lock, RW_READER); | |||||
mutex_enter(&dzp->z_lock); | |||||
for (;;) { | |||||
if (dzp->z_unlinked && !(flag & ZXATTR)) { | |||||
mutex_exit(&dzp->z_lock); | |||||
if (!(flag & ZHAVELOCK)) | |||||
rw_exit(&dzp->z_name_lock); | |||||
return (SET_ERROR(ENOENT)); | |||||
} | |||||
for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) { | |||||
if ((u8_strcmp(name, dl->dl_name, 0, cmpflags, | |||||
U8_UNICODE_LATEST, &error) == 0) || error != 0) | |||||
break; | |||||
} | |||||
if (error != 0) { | |||||
mutex_exit(&dzp->z_lock); | |||||
if (!(flag & ZHAVELOCK)) | |||||
rw_exit(&dzp->z_name_lock); | |||||
return (SET_ERROR(ENOENT)); | |||||
} | |||||
if (dl == NULL) { | |||||
size_t namesize; | |||||
/* | |||||
* Allocate a new dirlock and add it to the list. | |||||
*/ | |||||
namesize = strlen(name) + 1; | |||||
dl = kmem_alloc(sizeof (zfs_dirlock_t) + namesize, | |||||
KM_SLEEP); | |||||
cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL); | |||||
dl->dl_name = (char *)(dl + 1); | |||||
bcopy(name, dl->dl_name, namesize); | |||||
dl->dl_sharecnt = 0; | |||||
dl->dl_namelock = 0; | |||||
dl->dl_namesize = namesize; | |||||
dl->dl_dzp = dzp; | |||||
dl->dl_next = dzp->z_dirlocks; | |||||
dzp->z_dirlocks = dl; | |||||
break; | |||||
} | |||||
if ((flag & ZSHARED) && dl->dl_sharecnt != 0) | |||||
break; | |||||
cv_wait(&dl->dl_cv, &dzp->z_lock); | |||||
} | |||||
/* | |||||
* If the z_name_lock was NOT held for this dirlock record it. | |||||
*/ | |||||
if (flag & ZHAVELOCK) | |||||
dl->dl_namelock = 1; | |||||
if (flag & ZSHARED) | |||||
dl->dl_sharecnt++; | |||||
mutex_exit(&dzp->z_lock); | |||||
/* | |||||
* We have a dirlock on the name. (Note that it is the dirlock, | |||||
* not the dzp's z_lock, that protects the name in the zap object.) | |||||
* See if there's an object by this name; if so, put a hold on it. | |||||
*/ | |||||
if (flag & ZXATTR) { | if (flag & ZXATTR) { | ||||
error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid, | error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid, | ||||
sizeof (zoid)); | sizeof (zoid)); | ||||
if (error == 0) | if (error == 0) | ||||
error = (zoid == 0 ? ENOENT : 0); | error = (zoid == 0 ? ENOENT : 0); | ||||
} else { | } else { | ||||
if (update) | error = zfs_match_find(zfsvfs, dzp, name, exact, &zoid); | ||||
vp = dnlc_lookup(ZTOV(dzp), name); | |||||
if (vp == DNLC_NO_VNODE) { | |||||
VN_RELE(vp); | |||||
error = SET_ERROR(ENOENT); | |||||
} else if (vp) { | |||||
if (flag & ZNEW) { | |||||
zfs_dirent_unlock(dl); | |||||
VN_RELE(vp); | |||||
return (SET_ERROR(EEXIST)); | |||||
} | } | ||||
*dlpp = dl; | |||||
*zpp = VTOZ(vp); | |||||
return (0); | |||||
} else { | |||||
error = zfs_match_find(zfsvfs, dzp, name, exact, | |||||
update, direntflags, realpnp, &zoid); | |||||
} | |||||
} | |||||
if (error) { | if (error) { | ||||
if (error != ENOENT || (flag & ZEXISTS)) { | if (error != ENOENT || (flag & ZEXISTS)) { | ||||
zfs_dirent_unlock(dl); | |||||
return (error); | return (error); | ||||
} | } | ||||
} else { | } else { | ||||
if (flag & ZNEW) { | if (flag & ZNEW) { | ||||
zfs_dirent_unlock(dl); | |||||
return (SET_ERROR(EEXIST)); | return (SET_ERROR(EEXIST)); | ||||
} | } | ||||
error = zfs_zget(zfsvfs, zoid, zpp); | error = zfs_zget(zfsvfs, zoid, zpp); | ||||
if (error) { | if (error) | ||||
zfs_dirent_unlock(dl); | |||||
return (error); | return (error); | ||||
ASSERT(!(*zpp)->z_unlinked); | |||||
} | } | ||||
if (!(flag & ZXATTR) && update) | |||||
dnlc_update(ZTOV(dzp), name, ZTOV(*zpp)); | |||||
} | |||||
*dlpp = dl; | |||||
return (0); | return (0); | ||||
} | } | ||||
/* | static int | ||||
* Unlock this directory entry and wake anyone who was waiting for it. | zfs_dd_lookup(znode_t *dzp, znode_t **zpp) | ||||
*/ | |||||
void | |||||
zfs_dirent_unlock(zfs_dirlock_t *dl) | |||||
{ | { | ||||
znode_t *dzp = dl->dl_dzp; | zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | ||||
zfs_dirlock_t **prev_dl, *cur_dl; | |||||
mutex_enter(&dzp->z_lock); | |||||
if (!dl->dl_namelock) | |||||
rw_exit(&dzp->z_name_lock); | |||||
if (dl->dl_sharecnt > 1) { | |||||
dl->dl_sharecnt--; | |||||
mutex_exit(&dzp->z_lock); | |||||
return; | |||||
} | |||||
prev_dl = &dzp->z_dirlocks; | |||||
while ((cur_dl = *prev_dl) != dl) | |||||
prev_dl = &cur_dl->dl_next; | |||||
*prev_dl = dl->dl_next; | |||||
cv_broadcast(&dl->dl_cv); | |||||
mutex_exit(&dzp->z_lock); | |||||
cv_destroy(&dl->dl_cv); | |||||
kmem_free(dl, sizeof (*dl) + dl->dl_namesize); | |||||
} | |||||
/* | |||||
* Look up an entry in a directory. | |||||
* | |||||
* NOTE: '.' and '..' are handled as special cases because | |||||
* no directory entries are actually stored for them. If this is | |||||
* the root of a filesystem, then '.zfs' is also treated as a | |||||
* special pseudo-directory. | |||||
*/ | |||||
int | |||||
zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags, | |||||
int *deflg, pathname_t *rpnp) | |||||
{ | |||||
zfs_dirlock_t *dl; | |||||
znode_t *zp; | znode_t *zp; | ||||
int error = 0; | vnode_t *vp; | ||||
uint64_t parent; | uint64_t parent; | ||||
int unlinked; | int error; | ||||
if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { | ASSERT_VOP_LOCKED(ZTOV(dzp), "zfs_dirlook"); | ||||
mutex_enter(&dzp->z_lock); | ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock)); | ||||
unlinked = dzp->z_unlinked; | |||||
mutex_exit(&dzp->z_lock); | if (dzp->z_unlinked) | ||||
if (unlinked) | |||||
return (ENOENT); | return (ENOENT); | ||||
*vpp = ZTOV(dzp); | if ((error = sa_lookup(dzp->z_sa_hdl, | ||||
VN_HOLD(*vpp); | SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) | ||||
} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { | return (error); | ||||
zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | |||||
/* | /* | ||||
* If we are a snapshot mounted under .zfs, return | * If we are a snapshot mounted under .zfs, return | ||||
* the vp for the snapshot directory. | * the snapshot directory. | ||||
*/ | */ | ||||
if ((error = sa_lookup(dzp->z_sa_hdl, | |||||
SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) | |||||
return (error); | |||||
if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) { | if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) { | ||||
error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir, | error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir, | ||||
"snapshot", vpp, NULL, 0, NULL, kcred, | "snapshot", &vp, NULL, 0, NULL, kcred, | ||||
NULL, NULL, NULL); | NULL, NULL, NULL); | ||||
if (error == 0) | |||||
zp = VTOZ(vp); | |||||
} else { | |||||
error = zfs_zget(zfsvfs, parent, &zp); | |||||
} | |||||
if (error == 0) | |||||
*zpp = zp; | |||||
return (error); | return (error); | ||||
} | } | ||||
mutex_enter(&dzp->z_lock); | int | ||||
unlinked = dzp->z_unlinked; | zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp) | ||||
mutex_exit(&dzp->z_lock); | { | ||||
if (unlinked) | zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | ||||
return (ENOENT); | znode_t *zp; | ||||
int error = 0; | |||||
rw_enter(&dzp->z_parent_lock, RW_READER); | ASSERT_VOP_LOCKED(ZTOV(dzp), "zfs_dirlook"); | ||||
error = zfs_zget(zfsvfs, parent, &zp); | ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock)); | ||||
if (error == 0) | |||||
*vpp = ZTOV(zp); | |||||
rw_exit(&dzp->z_parent_lock); | |||||
} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) { | |||||
*vpp = zfsctl_root(dzp); | |||||
} else { | |||||
int zf; | |||||
zf = ZEXISTS | ZSHARED; | if (dzp->z_unlinked) | ||||
if (flags & FIGNORECASE) | return (SET_ERROR(ENOENT)); | ||||
zf |= ZCILOOK; | |||||
error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp); | if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { | ||||
*zpp = dzp; | |||||
} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { | |||||
error = zfs_dd_lookup(dzp, zpp); | |||||
} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) { | |||||
*zpp = VTOZ(zfsctl_root(dzp)); | |||||
} else { | |||||
error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS); | |||||
if (error == 0) { | if (error == 0) { | ||||
*vpp = ZTOV(zp); | |||||
zfs_dirent_unlock(dl); | |||||
dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ | dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ | ||||
*zpp = zp; | |||||
} | } | ||||
rpnp = NULL; | |||||
} | } | ||||
if ((flags & FIGNORECASE) && rpnp && !error) | |||||
(void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize); | |||||
return (error); | return (error); | ||||
} | } | ||||
/* | /* | ||||
* unlinked Set (formerly known as the "delete queue") Error Handling | * unlinked Set (formerly known as the "delete queue") Error Handling | ||||
* | * | ||||
* When dealing with the unlinked set, we dmu_tx_hold_zap(), but we | * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we | ||||
* don't specify the name of the entry that we will be manipulating. We | * don't specify the name of the entry that we will be manipulating. We | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj); | ||||
* We may pick up znodes that are already marked for deletion. | * We may pick up znodes that are already marked for deletion. | ||||
* This could happen during the purge of an extended attribute | * This could happen during the purge of an extended attribute | ||||
* directory. All we need to do is skip over them, since they | * directory. All we need to do is skip over them, since they | ||||
* are already in the system marked z_unlinked. | * are already in the system marked z_unlinked. | ||||
*/ | */ | ||||
if (error != 0) | if (error != 0) | ||||
continue; | continue; | ||||
vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY); | |||||
zp->z_unlinked = B_TRUE; | zp->z_unlinked = B_TRUE; | ||||
VN_RELE(ZTOV(zp)); | vput(ZTOV(zp)); | ||||
} | } | ||||
zap_cursor_fini(&zc); | zap_cursor_fini(&zc); | ||||
} | } | ||||
/* | /* | ||||
* Delete the entire contents of a directory. Return a count | * Delete the entire contents of a directory. Return a count | ||||
* of the number of entries that could not be deleted. If we encounter | * of the number of entries that could not be deleted. If we encounter | ||||
* an error, return a count of at least one so that the directory stays | * an error, return a count of at least one so that the directory stays | ||||
* in the unlinked set. | * in the unlinked set. | ||||
* | * | ||||
* NOTE: this function assumes that the directory is inactive, | * NOTE: this function assumes that the directory is inactive, | ||||
* so there is no need to lock its entries before deletion. | * so there is no need to lock its entries before deletion. | ||||
* Also, it assumes the directory contents is *only* regular | * Also, it assumes the directory contents is *only* regular | ||||
* files. | * files. | ||||
*/ | */ | ||||
static int | static int | ||||
zfs_purgedir(znode_t *dzp) | zfs_purgedir(znode_t *dzp) | ||||
{ | { | ||||
zap_cursor_t zc; | zap_cursor_t zc; | ||||
zap_attribute_t zap; | zap_attribute_t zap; | ||||
znode_t *xzp; | znode_t *xzp; | ||||
dmu_tx_t *tx; | dmu_tx_t *tx; | ||||
zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | ||||
zfs_dirlock_t dl; | |||||
int skipped = 0; | int skipped = 0; | ||||
int error; | int error; | ||||
for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); | for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); | ||||
(error = zap_cursor_retrieve(&zc, &zap)) == 0; | (error = zap_cursor_retrieve(&zc, &zap)) == 0; | ||||
zap_cursor_advance(&zc)) { | zap_cursor_advance(&zc)) { | ||||
error = zfs_zget(zfsvfs, | error = zfs_zget(zfsvfs, | ||||
ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp); | ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp); | ||||
if (error) { | if (error) { | ||||
skipped += 1; | skipped += 1; | ||||
continue; | continue; | ||||
} | } | ||||
vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY); | |||||
ASSERT((ZTOV(xzp)->v_type == VREG) || | ASSERT((ZTOV(xzp)->v_type == VREG) || | ||||
(ZTOV(xzp)->v_type == VLNK)); | (ZTOV(xzp)->v_type == VLNK)); | ||||
tx = dmu_tx_create(zfsvfs->z_os); | tx = dmu_tx_create(zfsvfs->z_os); | ||||
dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); | dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); | ||||
dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); | dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); | ||||
dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); | dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); | ||||
dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); | ||||
/* Is this really needed ? */ | /* Is this really needed ? */ | ||||
zfs_sa_upgrade_txholds(tx, xzp); | zfs_sa_upgrade_txholds(tx, xzp); | ||||
dmu_tx_mark_netfree(tx); | dmu_tx_mark_netfree(tx); | ||||
error = dmu_tx_assign(tx, TXG_WAIT); | error = dmu_tx_assign(tx, TXG_WAIT); | ||||
if (error) { | if (error) { | ||||
dmu_tx_abort(tx); | dmu_tx_abort(tx); | ||||
VN_RELE(ZTOV(xzp)); | vput(ZTOV(xzp)); | ||||
skipped += 1; | skipped += 1; | ||||
continue; | continue; | ||||
} | } | ||||
bzero(&dl, sizeof (dl)); | |||||
dl.dl_dzp = dzp; | |||||
dl.dl_name = zap.za_name; | |||||
error = zfs_link_destroy(&dl, xzp, tx, 0, NULL); | error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL); | ||||
if (error) | if (error) | ||||
skipped += 1; | skipped += 1; | ||||
dmu_tx_commit(tx); | dmu_tx_commit(tx); | ||||
VN_RELE(ZTOV(xzp)); | vput(ZTOV(xzp)); | ||||
} | } | ||||
zap_cursor_fini(&zc); | zap_cursor_fini(&zc); | ||||
if (error != ENOENT) | if (error != ENOENT) | ||||
skipped += 1; | skipped += 1; | ||||
return (skipped); | return (skipped); | ||||
} | } | ||||
void | void | ||||
zfs_rmnode(znode_t *zp) | zfs_rmnode(znode_t *zp) | ||||
{ | { | ||||
zfsvfs_t *zfsvfs = zp->z_zfsvfs; | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | ||||
objset_t *os = zfsvfs->z_os; | objset_t *os = zfsvfs->z_os; | ||||
znode_t *xzp = NULL; | znode_t *xzp = NULL; | ||||
dmu_tx_t *tx; | dmu_tx_t *tx; | ||||
uint64_t acl_obj; | uint64_t acl_obj; | ||||
uint64_t xattr_obj; | uint64_t xattr_obj; | ||||
int error; | int error; | ||||
ASSERT(zp->z_links == 0); | ASSERT(zp->z_links == 0); | ||||
ASSERT_VOP_ELOCKED(ZTOV(zp), "zfs_rmnode"); | |||||
/* | /* | ||||
* If this is an attribute directory, purge its contents. | * If this is an attribute directory, purge its contents. | ||||
*/ | */ | ||||
if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR && | if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR && | ||||
(zp->z_pflags & ZFS_XATTR)) { | (zp->z_pflags & ZFS_XATTR)) { | ||||
if (zfs_purgedir(zp) != 0) { | if (zfs_purgedir(zp) != 0) { | ||||
/* | /* | ||||
Show All 28 Lines | zfs_rmnode(znode_t *zp) | ||||
/* | /* | ||||
* If the file has extended attributes, we're going to unlink | * If the file has extended attributes, we're going to unlink | ||||
* the xattr dir. | * the xattr dir. | ||||
*/ | */ | ||||
error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), | error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), | ||||
&xattr_obj, sizeof (xattr_obj)); | &xattr_obj, sizeof (xattr_obj)); | ||||
if (error == 0 && xattr_obj) { | if (error == 0 && xattr_obj) { | ||||
error = zfs_zget(zfsvfs, xattr_obj, &xzp); | error = zfs_zget(zfsvfs, xattr_obj, &xzp); | ||||
ASSERT(error == 0); | ASSERT3S(error, ==, 0); | ||||
vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY); | |||||
} | } | ||||
acl_obj = zfs_external_acl(zp); | acl_obj = zfs_external_acl(zp); | ||||
/* | /* | ||||
* Set up the final transaction. | * Set up the final transaction. | ||||
*/ | */ | ||||
tx = dmu_tx_create(os); | tx = dmu_tx_create(os); | ||||
Show All 17 Lines | if (error) { | ||||
dmu_tx_abort(tx); | dmu_tx_abort(tx); | ||||
zfs_znode_dmu_fini(zp); | zfs_znode_dmu_fini(zp); | ||||
zfs_znode_free(zp); | zfs_znode_free(zp); | ||||
goto out; | goto out; | ||||
} | } | ||||
if (xzp) { | if (xzp) { | ||||
ASSERT(error == 0); | ASSERT(error == 0); | ||||
mutex_enter(&xzp->z_lock); | |||||
xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */ | xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */ | ||||
xzp->z_links = 0; /* no more links to it */ | xzp->z_links = 0; /* no more links to it */ | ||||
VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), | VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), | ||||
&xzp->z_links, sizeof (xzp->z_links), tx)); | &xzp->z_links, sizeof (xzp->z_links), tx)); | ||||
mutex_exit(&xzp->z_lock); | |||||
zfs_unlinked_add(xzp, tx); | zfs_unlinked_add(xzp, tx); | ||||
} | } | ||||
/* Remove this znode from the unlinked set */ | /* Remove this znode from the unlinked set */ | ||||
VERIFY3U(0, ==, | VERIFY3U(0, ==, | ||||
zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); | zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); | ||||
zfs_znode_delete(zp, tx); | zfs_znode_delete(zp, tx); | ||||
dmu_tx_commit(tx); | dmu_tx_commit(tx); | ||||
out: | out: | ||||
if (xzp) | if (xzp) | ||||
VN_RELE(ZTOV(xzp)); | vput(ZTOV(xzp)); | ||||
} | } | ||||
static uint64_t | static uint64_t | ||||
zfs_dirent(znode_t *zp, uint64_t mode) | zfs_dirent(znode_t *zp, uint64_t mode) | ||||
{ | { | ||||
uint64_t de = zp->z_id; | uint64_t de = zp->z_id; | ||||
if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE) | if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE) | ||||
de |= IFTODT(mode) << 60; | de |= IFTODT(mode) << 60; | ||||
return (de); | return (de); | ||||
} | } | ||||
/* | /* | ||||
* Link zp into dl. Can only fail if zp has been unlinked. | * Link zp into dzp. Can only fail if zp has been unlinked. | ||||
*/ | */ | ||||
int | int | ||||
zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag) | zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, | ||||
int flag) | |||||
{ | { | ||||
znode_t *dzp = dl->dl_dzp; | |||||
zfsvfs_t *zfsvfs = zp->z_zfsvfs; | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | ||||
vnode_t *vp = ZTOV(zp); | vnode_t *vp = ZTOV(zp); | ||||
uint64_t value; | uint64_t value; | ||||
int zp_is_dir = (vp->v_type == VDIR); | int zp_is_dir = (vp->v_type == VDIR); | ||||
sa_bulk_attr_t bulk[5]; | sa_bulk_attr_t bulk[5]; | ||||
uint64_t mtime[2], ctime[2]; | uint64_t mtime[2], ctime[2]; | ||||
int count = 0; | int count = 0; | ||||
int error; | int error; | ||||
mutex_enter(&zp->z_lock); | ASSERT_VOP_ELOCKED(ZTOV(dzp), "zfs_link_create"); | ||||
ASSERT_VOP_ELOCKED(ZTOV(zp), "zfs_link_create"); | |||||
#if 0 | |||||
smh: intentionally left in? | |||||
Not Done Inline ActionsYes. On the one hand, there is this limit. On the other hand, we never enforced it before in this code. So... avg: Yes. On the one hand, there is this limit. On the other hand, we never enforced it before in… | |||||
Not Done Inline ActionsCool, be good to add a quick comment to this effect to the code, so when we look back its obvious why its currently disabled. smh: Cool, be good to add a quick comment to this effect to the code, so when we look back its… | |||||
if (zp_is_dir) { | |||||
error = 0; | |||||
if (dzp->z_links >= LINK_MAX) | |||||
error = SET_ERROR(EMLINK); | |||||
return (error); | |||||
} | |||||
#endif | |||||
if (!(flag & ZRENAMING)) { | if (!(flag & ZRENAMING)) { | ||||
if (zp->z_unlinked) { /* no new links to unlinked zp */ | if (zp->z_unlinked) { /* no new links to unlinked zp */ | ||||
ASSERT(!(flag & (ZNEW | ZEXISTS))); | ASSERT(!(flag & (ZNEW | ZEXISTS))); | ||||
mutex_exit(&zp->z_lock); | |||||
return (SET_ERROR(ENOENT)); | return (SET_ERROR(ENOENT)); | ||||
} | } | ||||
#if 0 | |||||
Not Done Inline Actionsintentionally left in? smh: intentionally left in? | |||||
Not Done Inline Actionsditto avg: ditto | |||||
if (zp->z_links >= LINK_MAX) { | |||||
return (SET_ERROR(EMLINK)); | |||||
} | |||||
#endif | |||||
zp->z_links++; | zp->z_links++; | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, | ||||
&zp->z_links, sizeof (zp->z_links)); | &zp->z_links, sizeof (zp->z_links)); | ||||
} else { | |||||
ASSERT(zp->z_unlinked == 0); | |||||
} | } | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, | ||||
&dzp->z_id, sizeof (dzp->z_id)); | &dzp->z_id, sizeof (dzp->z_id)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, | ||||
&zp->z_pflags, sizeof (zp->z_pflags)); | &zp->z_pflags, sizeof (zp->z_pflags)); | ||||
if (!(flag & ZNEW)) { | if (!(flag & ZNEW)) { | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, | ||||
ctime, sizeof (ctime)); | ctime, sizeof (ctime)); | ||||
zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, | zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, | ||||
ctime, B_TRUE); | ctime, B_TRUE); | ||||
} | } | ||||
error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | ||||
ASSERT(error == 0); | ASSERT0(error); | ||||
mutex_exit(&zp->z_lock); | |||||
mutex_enter(&dzp->z_lock); | |||||
dzp->z_size++; | dzp->z_size++; | ||||
dzp->z_links += zp_is_dir; | dzp->z_links += zp_is_dir; | ||||
count = 0; | count = 0; | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, | ||||
&dzp->z_size, sizeof (dzp->z_size)); | &dzp->z_size, sizeof (dzp->z_size)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, | ||||
&dzp->z_links, sizeof (dzp->z_links)); | &dzp->z_links, sizeof (dzp->z_links)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, | ||||
mtime, sizeof (mtime)); | mtime, sizeof (mtime)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, | ||||
ctime, sizeof (ctime)); | ctime, sizeof (ctime)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, | ||||
&dzp->z_pflags, sizeof (dzp->z_pflags)); | &dzp->z_pflags, sizeof (dzp->z_pflags)); | ||||
zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | ||||
error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); | error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); | ||||
ASSERT(error == 0); | ASSERT0(error); | ||||
mutex_exit(&dzp->z_lock); | |||||
value = zfs_dirent(zp, zp->z_mode); | value = zfs_dirent(zp, zp->z_mode); | ||||
error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, | error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name, | ||||
8, 1, &value, tx); | 8, 1, &value, tx); | ||||
ASSERT(error == 0); | VERIFY0(error); | ||||
dnlc_update(ZTOV(dzp), dl->dl_name, vp); | |||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx, | zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, | ||||
int flag) | int flag) | ||||
{ | { | ||||
int error; | int error; | ||||
if (zp->z_zfsvfs->z_norm) { | if (zp->z_zfsvfs->z_norm) { | ||||
if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && | if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) | ||||
(flag & ZCIEXACT)) || | |||||
((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) && | |||||
!(flag & ZCILOOK))) | |||||
error = zap_remove_norm(zp->z_zfsvfs->z_os, | error = zap_remove_norm(zp->z_zfsvfs->z_os, | ||||
dzp->z_id, dl->dl_name, MT_EXACT, tx); | dzp->z_id, name, MT_EXACT, tx); | ||||
else | else | ||||
error = zap_remove_norm(zp->z_zfsvfs->z_os, | error = zap_remove_norm(zp->z_zfsvfs->z_os, | ||||
dzp->z_id, dl->dl_name, MT_FIRST, tx); | dzp->z_id, name, MT_FIRST, tx); | ||||
} else { | } else { | ||||
error = zap_remove(zp->z_zfsvfs->z_os, | error = zap_remove(zp->z_zfsvfs->z_os, | ||||
dzp->z_id, dl->dl_name, tx); | dzp->z_id, name, tx); | ||||
} | } | ||||
return (error); | return (error); | ||||
} | } | ||||
/* | /* | ||||
* Unlink zp from dl, and mark zp for deletion if this was the last link. | * Unlink zp from dzp, and mark zp for deletion if this was the last link. | ||||
* Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). | * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). | ||||
* If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. | * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. | ||||
* If it's non-NULL, we use it to indicate whether the znode needs deletion, | * If it's non-NULL, we use it to indicate whether the znode needs deletion, | ||||
* and it's the caller's job to do it. | * and it's the caller's job to do it. | ||||
*/ | */ | ||||
int | int | ||||
zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag, | zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, | ||||
boolean_t *unlinkedp) | int flag, boolean_t *unlinkedp) | ||||
{ | { | ||||
znode_t *dzp = dl->dl_dzp; | |||||
zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | zfsvfs_t *zfsvfs = dzp->z_zfsvfs; | ||||
vnode_t *vp = ZTOV(zp); | vnode_t *vp = ZTOV(zp); | ||||
int zp_is_dir = (vp->v_type == VDIR); | int zp_is_dir = (vp->v_type == VDIR); | ||||
boolean_t unlinked = B_FALSE; | boolean_t unlinked = B_FALSE; | ||||
sa_bulk_attr_t bulk[5]; | sa_bulk_attr_t bulk[5]; | ||||
uint64_t mtime[2], ctime[2]; | uint64_t mtime[2], ctime[2]; | ||||
int count = 0; | int count = 0; | ||||
int error; | int error; | ||||
dnlc_remove(ZTOV(dzp), dl->dl_name); | ASSERT_VOP_ELOCKED(ZTOV(dzp), "zfs_link_destroy"); | ||||
ASSERT_VOP_ELOCKED(ZTOV(zp), "zfs_link_destroy"); | |||||
if (!(flag & ZRENAMING)) { | if (!(flag & ZRENAMING)) { | ||||
if (vn_vfswlock(vp)) /* prevent new mounts on zp */ | |||||
return (SET_ERROR(EBUSY)); | |||||
if (vn_ismntpt(vp)) { /* don't remove mount point */ | |||||
vn_vfsunlock(vp); | |||||
return (SET_ERROR(EBUSY)); | |||||
} | |||||
mutex_enter(&zp->z_lock); | |||||
if (zp_is_dir && !zfs_dirempty(zp)) { | if (zp_is_dir && !zfs_dirempty(zp)) { | ||||
mutex_exit(&zp->z_lock); | |||||
vn_vfsunlock(vp); | |||||
#ifdef illumos | #ifdef illumos | ||||
return (SET_ERROR(EEXIST)); | return (SET_ERROR(EEXIST)); | ||||
#else | #else | ||||
return (SET_ERROR(ENOTEMPTY)); | return (SET_ERROR(ENOTEMPTY)); | ||||
#endif | #endif | ||||
} | } | ||||
/* | /* | ||||
* If we get here, we are going to try to remove the object. | * If we get here, we are going to try to remove the object. | ||||
* First try removing the name from the directory; if that | * First try removing the name from the directory; if that | ||||
* fails, return the error. | * fails, return the error. | ||||
*/ | */ | ||||
error = zfs_dropname(dl, zp, dzp, tx, flag); | error = zfs_dropname(dzp, name, zp, tx, flag); | ||||
if (error != 0) { | if (error != 0) { | ||||
mutex_exit(&zp->z_lock); | |||||
vn_vfsunlock(vp); | |||||
return (error); | return (error); | ||||
} | } | ||||
if (zp->z_links <= zp_is_dir) { | if (zp->z_links <= zp_is_dir) { | ||||
zfs_panic_recover("zfs: link count on vnode %p is %u, " | zfs_panic_recover("zfs: link count on vnode %p is %u, " | ||||
"should be at least %u", zp->z_vnode, | "should be at least %u", zp->z_vnode, | ||||
(int)zp->z_links, | (int)zp->z_links, | ||||
zp_is_dir + 1); | zp_is_dir + 1); | ||||
Show All 10 Lines | if (--zp->z_links == zp_is_dir) { | ||||
NULL, &zp->z_pflags, sizeof (zp->z_pflags)); | NULL, &zp->z_pflags, sizeof (zp->z_pflags)); | ||||
zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, | zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, | ||||
B_TRUE); | B_TRUE); | ||||
} | } | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), | ||||
NULL, &zp->z_links, sizeof (zp->z_links)); | NULL, &zp->z_links, sizeof (zp->z_links)); | ||||
error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | ||||
count = 0; | count = 0; | ||||
ASSERT(error == 0); | ASSERT0(error); | ||||
mutex_exit(&zp->z_lock); | |||||
vn_vfsunlock(vp); | |||||
} else { | } else { | ||||
error = zfs_dropname(dl, zp, dzp, tx, flag); | ASSERT(zp->z_unlinked == 0); | ||||
error = zfs_dropname(dzp, name, zp, tx, flag); | |||||
if (error != 0) | if (error != 0) | ||||
return (error); | return (error); | ||||
} | } | ||||
mutex_enter(&dzp->z_lock); | |||||
dzp->z_size--; /* one dirent removed */ | dzp->z_size--; /* one dirent removed */ | ||||
dzp->z_links -= zp_is_dir; /* ".." link from zp */ | dzp->z_links -= zp_is_dir; /* ".." link from zp */ | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), | ||||
NULL, &dzp->z_links, sizeof (dzp->z_links)); | NULL, &dzp->z_links, sizeof (dzp->z_links)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), | ||||
NULL, &dzp->z_size, sizeof (dzp->z_size)); | NULL, &dzp->z_size, sizeof (dzp->z_size)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), | ||||
NULL, ctime, sizeof (ctime)); | NULL, ctime, sizeof (ctime)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), | ||||
NULL, mtime, sizeof (mtime)); | NULL, mtime, sizeof (mtime)); | ||||
SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), | ||||
NULL, &dzp->z_pflags, sizeof (dzp->z_pflags)); | NULL, &dzp->z_pflags, sizeof (dzp->z_pflags)); | ||||
zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); | ||||
error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); | error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); | ||||
ASSERT(error == 0); | ASSERT0(error); | ||||
mutex_exit(&dzp->z_lock); | |||||
if (unlinkedp != NULL) | if (unlinkedp != NULL) | ||||
*unlinkedp = unlinked; | *unlinkedp = unlinked; | ||||
else if (unlinked) | else if (unlinked) | ||||
zfs_unlinked_add(zp, tx); | zfs_unlinked_add(zp, tx); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* | /* | ||||
* Indicate whether the directory is empty. Works with or without z_lock | * Indicate whether the directory is empty. | ||||
* held, but can only be consider a hint in the latter case. Returns true | |||||
* if only "." and ".." remain and there's no work in progress. | |||||
*/ | */ | ||||
boolean_t | boolean_t | ||||
zfs_dirempty(znode_t *dzp) | zfs_dirempty(znode_t *dzp) | ||||
{ | { | ||||
return (dzp->z_size == 2 && dzp->z_dirlocks == 0); | return (dzp->z_size == 2); | ||||
} | } | ||||
int | int | ||||
zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) | zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) | ||||
{ | { | ||||
zfsvfs_t *zfsvfs = zp->z_zfsvfs; | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | ||||
znode_t *xzp; | znode_t *xzp; | ||||
dmu_tx_t *tx; | dmu_tx_t *tx; | ||||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | |||||
* RETURN: 0 on success | * RETURN: 0 on success | ||||
* error number on failure | * error number on failure | ||||
*/ | */ | ||||
int | int | ||||
zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags) | zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags) | ||||
{ | { | ||||
zfsvfs_t *zfsvfs = zp->z_zfsvfs; | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | ||||
znode_t *xzp; | znode_t *xzp; | ||||
zfs_dirlock_t *dl; | |||||
vattr_t va; | vattr_t va; | ||||
int error; | int error; | ||||
top: | top: | ||||
error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL); | error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR); | ||||
if (error) | if (error) | ||||
return (error); | return (error); | ||||
if (xzp != NULL) { | if (xzp != NULL) { | ||||
*xvpp = ZTOV(xzp); | *xvpp = ZTOV(xzp); | ||||
zfs_dirent_unlock(dl); | |||||
return (0); | return (0); | ||||
} | } | ||||
if (!(flags & CREATE_XATTR_DIR)) { | if (!(flags & CREATE_XATTR_DIR)) { | ||||
zfs_dirent_unlock(dl); | |||||
#ifdef illumos | #ifdef illumos | ||||
return (SET_ERROR(ENOENT)); | return (SET_ERROR(ENOENT)); | ||||
#else | #else | ||||
return (SET_ERROR(ENOATTR)); | return (SET_ERROR(ENOATTR)); | ||||
#endif | #endif | ||||
} | } | ||||
if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { | if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { | ||||
zfs_dirent_unlock(dl); | |||||
return (SET_ERROR(EROFS)); | return (SET_ERROR(EROFS)); | ||||
} | } | ||||
/* | /* | ||||
* The ability to 'create' files in an attribute | * The ability to 'create' files in an attribute | ||||
* directory comes from the write_xattr permission on the base file. | * directory comes from the write_xattr permission on the base file. | ||||
* | * | ||||
* The ability to 'search' an attribute directory requires | * The ability to 'search' an attribute directory requires | ||||
* read_xattr permission on the base file. | * read_xattr permission on the base file. | ||||
* | * | ||||
* Once in a directory the ability to read/write attributes | * Once in a directory the ability to read/write attributes | ||||
* is controlled by the permissions on the attribute file. | * is controlled by the permissions on the attribute file. | ||||
*/ | */ | ||||
va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; | va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; | ||||
va.va_type = VDIR; | va.va_type = VDIR; | ||||
va.va_mode = S_IFDIR | S_ISVTX | 0777; | va.va_mode = S_IFDIR | S_ISVTX | 0777; | ||||
zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid); | zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid); | ||||
error = zfs_make_xattrdir(zp, &va, xvpp, cr); | error = zfs_make_xattrdir(zp, &va, xvpp, cr); | ||||
zfs_dirent_unlock(dl); | |||||
if (error == ERESTART) { | if (error == ERESTART) { | ||||
/* NB: we already did dmu_tx_wait() if necessary */ | /* NB: we already did dmu_tx_wait() if necessary */ | ||||
goto top; | goto top; | ||||
} | } | ||||
if (error == 0) | if (error == 0) | ||||
VOP_UNLOCK(*xvpp, 0); | VOP_UNLOCK(*xvpp, 0); | ||||
Show All 40 Lines |
intentionally left in?