1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51484Sek110237 * Common Development and Distribution License (the "License"). 61484Sek110237 * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 223461Sahrens * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/types.h> 29789Sahrens #include <sys/param.h> 30789Sahrens #include <sys/time.h> 31789Sahrens #include <sys/systm.h> 32789Sahrens #include <sys/sysmacros.h> 33789Sahrens #include <sys/resource.h> 34789Sahrens #include <sys/vfs.h> 35789Sahrens #include <sys/vnode.h> 36789Sahrens #include <sys/file.h> 37789Sahrens #include <sys/mode.h> 38789Sahrens #include <sys/kmem.h> 39789Sahrens #include <sys/uio.h> 40789Sahrens #include <sys/pathname.h> 41789Sahrens #include <sys/cmn_err.h> 42789Sahrens #include <sys/errno.h> 43789Sahrens #include <sys/stat.h> 44789Sahrens #include <sys/unistd.h> 45789Sahrens #include <sys/random.h> 46789Sahrens #include <sys/policy.h> 47789Sahrens #include <sys/zfs_dir.h> 48789Sahrens #include <sys/zfs_acl.h> 49789Sahrens #include <sys/fs/zfs.h> 50789Sahrens #include "fs/fs_subr.h" 51789Sahrens #include <sys/zap.h> 52789Sahrens #include <sys/dmu.h> 53789Sahrens #include <sys/atomic.h> 54789Sahrens #include <sys/zfs_ctldir.h> 555331Samw #include <sys/zfs_fuid.h> 561484Sek110237 #include <sys/dnlc.h> 575331Samw #include <sys/extdirent.h> 585331Samw #include <sys/zfs_i18n.h> 595331Samw 605331Samw /* 615331Samw * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups 625331Samw * of names after deciding which is the appropriate lookup interface. 635331Samw */ 645331Samw static int 655331Samw zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact, 665331Samw boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid) 675331Samw { 685331Samw int error; 695331Samw 705331Samw if (zfsvfs->z_norm) { 715331Samw matchtype_t mt = MT_FIRST; 725331Samw boolean_t conflict = B_FALSE; 735331Samw size_t bufsz = 0; 745331Samw char *buf = NULL; 755331Samw 765331Samw if (rpnp) { 775331Samw buf = rpnp->pn_path; 785331Samw bufsz = rpnp->pn_bufsize; 795331Samw } 805331Samw if (exact) 815331Samw mt = MT_EXACT; 825331Samw /* 835331Samw * In the non-mixed case we only expect there would ever 845331Samw * be one match, but we need to use the normalizing lookup. 855331Samw */ 865331Samw error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1, 875331Samw zoid, mt, buf, bufsz, &conflict); 885331Samw if (deflags) 895331Samw *deflags = conflict ? ED_CASE_CONFLICT : 0; 905331Samw } else { 915331Samw error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid); 925331Samw } 935331Samw *zoid = ZFS_DIRENT_OBJ(*zoid); 945331Samw 955331Samw if (error == ENOENT && update) 965331Samw dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE); 975331Samw 985331Samw return (error); 995331Samw } 100789Sahrens 101789Sahrens /* 102789Sahrens * Lock a directory entry. A dirlock on <dzp, name> protects that name 103789Sahrens * in dzp's directory zap object. As long as you hold a dirlock, you can 104789Sahrens * assume two things: (1) dzp cannot be reaped, and (2) no other thread 105789Sahrens * can change the zap entry for (i.e. link or unlink) this name. 106789Sahrens * 107789Sahrens * Input arguments: 108789Sahrens * dzp - znode for directory 109789Sahrens * name - name of entry to lock 110789Sahrens * flag - ZNEW: if the entry already exists, fail with EEXIST. 111789Sahrens * ZEXISTS: if the entry does not exist, fail with ENOENT. 112789Sahrens * ZSHARED: allow concurrent access with other ZSHARED callers. 113789Sahrens * ZXATTR: we want dzp's xattr directory 1145331Samw * ZCILOOK: On a mixed sensitivity file system, 1155331Samw * this lookup should be case-insensitive. 1165331Samw * ZCIEXACT: On a purely case-insensitive file system, 1175331Samw * this lookup should be case-sensitive. 1185331Samw * ZRENAMING: we are locking for renaming, force narrow locks 119789Sahrens * 120789Sahrens * Output arguments: 121789Sahrens * zpp - pointer to the znode for the entry (NULL if there isn't one) 122789Sahrens * dlpp - pointer to the dirlock for this entry (NULL on error) 1235331Samw * direntflags - (case-insensitive lookup only) 1245331Samw * flags if multiple case-sensitive matches exist in directory 1255331Samw * realpnp - (case-insensitive lookup only) 1265331Samw * actual name matched within the directory 127789Sahrens * 128789Sahrens * Return value: 0 on success or errno on failure. 129789Sahrens * 130789Sahrens * NOTE: Always checks for, and rejects, '.' and '..'. 1315331Samw * NOTE: For case-insensitive file systems we take wide locks (see below), 1325331Samw * but return znode pointers to a single match. 133789Sahrens */ 134789Sahrens int 135789Sahrens zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp, 1365331Samw int flag, int *direntflags, pathname_t *realpnp) 137789Sahrens { 138789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 139789Sahrens zfs_dirlock_t *dl; 1405331Samw boolean_t update; 1415331Samw boolean_t exact; 142789Sahrens uint64_t zoid; 1435331Samw vnode_t *vp = NULL; 1445331Samw int error = 0; 1455331Samw int cmpflags; 146789Sahrens 147789Sahrens *zpp = NULL; 148789Sahrens *dlpp = NULL; 149789Sahrens 150789Sahrens /* 151789Sahrens * Verify that we are not trying to lock '.', '..', or '.zfs' 152789Sahrens */ 153789Sahrens if (name[0] == '.' && 154789Sahrens (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) || 155789Sahrens zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) 156789Sahrens return (EEXIST); 157789Sahrens 158789Sahrens /* 1595331Samw * Case sensitivity and normalization preferences are set when 1605331Samw * the file system is created. These are stored in the 1615331Samw * zfsvfs->z_case and zfsvfs->z_norm fields. These choices 1625331Samw * affect what vnodes can be cached in the DNLC, how we 1635331Samw * perform zap lookups, and the "width" of our dirlocks. 1645331Samw * 1655331Samw * A normal dirlock locks a single name. Note that with 1665331Samw * normalization a name can be composed multiple ways, but 1675331Samw * when normalized, these names all compare equal. A wide 1685331Samw * dirlock locks multiple names. We need these when the file 1695331Samw * system is supporting mixed-mode access. It is sometimes 1705331Samw * necessary to lock all case permutations of file name at 1715331Samw * once so that simultaneous case-insensitive/case-sensitive 1725331Samw * behaves as rationally as possible. 1735331Samw */ 1745331Samw 1755331Samw /* 1765331Samw * Decide if exact matches should be requested when performing 1775331Samw * a zap lookup on file systems supporting case-insensitive 1785331Samw * access. 1795331Samw */ 1805331Samw exact = ((zfsvfs->z_case & ZFS_CI_ONLY) && (flag & ZCIEXACT)) || 1815331Samw ((zfsvfs->z_case & ZFS_CI_MIXD) && !(flag & ZCILOOK)); 1825331Samw 1835331Samw /* 1845331Samw * Only look in or update the DNLC if we are looking for the 1855331Samw * name on a file system that does not require normalization 1865331Samw * or case folding. We can also look there if we happen to be 1875331Samw * on a non-normalizing, mixed sensitivity file system IF we 1885331Samw * are looking for the exact name. 1895331Samw * 1905331Samw * Maybe can add TO-UPPERed version of name to dnlc in ci-only 1915331Samw * case for performance improvement? 1925331Samw */ 1935331Samw update = !zfsvfs->z_norm || 1945331Samw ((zfsvfs->z_case & ZFS_CI_MIXD) && 1955331Samw !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK)); 1965331Samw 1975331Samw /* 1985331Samw * ZRENAMING indicates we are in a situation where we should 1995331Samw * take narrow locks regardless of the file system's 2005331Samw * preferences for normalizing and case folding. This will 2015331Samw * prevent us deadlocking trying to grab the same wide lock 2025331Samw * twice if the two names happen to be case-insensitive 2035331Samw * matches. 2045331Samw */ 2055331Samw if (flag & ZRENAMING) 2065331Samw cmpflags = 0; 2075331Samw else 2085331Samw cmpflags = zfsvfs->z_norm; 2095331Samw 2105331Samw /* 211789Sahrens * Wait until there are no locks on this name. 212789Sahrens */ 2133897Smaybee rw_enter(&dzp->z_name_lock, RW_READER); 214789Sahrens mutex_enter(&dzp->z_lock); 215789Sahrens for (;;) { 2163461Sahrens if (dzp->z_unlinked) { 217789Sahrens mutex_exit(&dzp->z_lock); 2183897Smaybee rw_exit(&dzp->z_name_lock); 219789Sahrens return (ENOENT); 220789Sahrens } 2215331Samw for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) { 2225331Samw if ((u8_strcmp(name, dl->dl_name, 0, cmpflags, 2235331Samw U8_UNICODE_LATEST, &error) == 0) || error != 0) 224789Sahrens break; 2255331Samw } 2265331Samw if (error != 0) { 2275331Samw mutex_exit(&dzp->z_lock); 2285331Samw rw_exit(&dzp->z_name_lock); 2295331Samw return (ENOENT); 2305331Samw } 231789Sahrens if (dl == NULL) { 232789Sahrens /* 233789Sahrens * Allocate a new dirlock and add it to the list. 234789Sahrens */ 235789Sahrens dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP); 236789Sahrens cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL); 237789Sahrens dl->dl_name = name; 238789Sahrens dl->dl_sharecnt = 0; 239789Sahrens dl->dl_namesize = 0; 240789Sahrens dl->dl_dzp = dzp; 241789Sahrens dl->dl_next = dzp->z_dirlocks; 242789Sahrens dzp->z_dirlocks = dl; 243789Sahrens break; 244789Sahrens } 245789Sahrens if ((flag & ZSHARED) && dl->dl_sharecnt != 0) 246789Sahrens break; 247789Sahrens cv_wait(&dl->dl_cv, &dzp->z_lock); 248789Sahrens } 249789Sahrens 250789Sahrens if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) { 251789Sahrens /* 252789Sahrens * We're the second shared reference to dl. Make a copy of 253789Sahrens * dl_name in case the first thread goes away before we do. 254789Sahrens * Note that we initialize the new name before storing its 255789Sahrens * pointer into dl_name, because the first thread may load 256789Sahrens * dl->dl_name at any time. He'll either see the old value, 257789Sahrens * which is his, or the new shared copy; either is OK. 258789Sahrens */ 259789Sahrens dl->dl_namesize = strlen(dl->dl_name) + 1; 260789Sahrens name = kmem_alloc(dl->dl_namesize, KM_SLEEP); 261789Sahrens bcopy(dl->dl_name, name, dl->dl_namesize); 262789Sahrens dl->dl_name = name; 263789Sahrens } 264789Sahrens 265789Sahrens mutex_exit(&dzp->z_lock); 266789Sahrens 267789Sahrens /* 268789Sahrens * We have a dirlock on the name. (Note that it is the dirlock, 269789Sahrens * not the dzp's z_lock, that protects the name in the zap object.) 270789Sahrens * See if there's an object by this name; if so, put a hold on it. 271789Sahrens */ 272789Sahrens if (flag & ZXATTR) { 273789Sahrens zoid = dzp->z_phys->zp_xattr; 274789Sahrens error = (zoid == 0 ? ENOENT : 0); 275789Sahrens } else { 2765331Samw if (update) 2775331Samw vp = dnlc_lookup(ZTOV(dzp), name); 2781484Sek110237 if (vp == DNLC_NO_VNODE) { 2791484Sek110237 VN_RELE(vp); 2801484Sek110237 error = ENOENT; 2811484Sek110237 } else if (vp) { 2821484Sek110237 if (flag & ZNEW) { 2831484Sek110237 zfs_dirent_unlock(dl); 2841484Sek110237 VN_RELE(vp); 2851484Sek110237 return (EEXIST); 2861484Sek110237 } 2871484Sek110237 *dlpp = dl; 2881484Sek110237 *zpp = VTOZ(vp); 2891484Sek110237 return (0); 2901484Sek110237 } else { 2915331Samw error = zfs_match_find(zfsvfs, dzp, name, exact, 2925331Samw update, direntflags, realpnp, &zoid); 2931484Sek110237 } 294789Sahrens } 295789Sahrens if (error) { 296789Sahrens if (error != ENOENT || (flag & ZEXISTS)) { 297789Sahrens zfs_dirent_unlock(dl); 298789Sahrens return (error); 299789Sahrens } 300789Sahrens } else { 301789Sahrens if (flag & ZNEW) { 302789Sahrens zfs_dirent_unlock(dl); 303789Sahrens return (EEXIST); 304789Sahrens } 305789Sahrens error = zfs_zget(zfsvfs, zoid, zpp); 306789Sahrens if (error) { 307789Sahrens zfs_dirent_unlock(dl); 308789Sahrens return (error); 309789Sahrens } 3105331Samw if (!(flag & ZXATTR) && update) 3111484Sek110237 dnlc_update(ZTOV(dzp), name, ZTOV(*zpp)); 312789Sahrens } 313789Sahrens 314789Sahrens *dlpp = dl; 315789Sahrens 316789Sahrens return (0); 317789Sahrens } 318789Sahrens 319789Sahrens /* 320789Sahrens * Unlock this directory entry and wake anyone who was waiting for it. 321789Sahrens */ 322789Sahrens void 323789Sahrens zfs_dirent_unlock(zfs_dirlock_t *dl) 324789Sahrens { 325789Sahrens znode_t *dzp = dl->dl_dzp; 326789Sahrens zfs_dirlock_t **prev_dl, *cur_dl; 327789Sahrens 328789Sahrens mutex_enter(&dzp->z_lock); 3293897Smaybee rw_exit(&dzp->z_name_lock); 330789Sahrens if (dl->dl_sharecnt > 1) { 331789Sahrens dl->dl_sharecnt--; 332789Sahrens mutex_exit(&dzp->z_lock); 333789Sahrens return; 334789Sahrens } 335789Sahrens prev_dl = &dzp->z_dirlocks; 336789Sahrens while ((cur_dl = *prev_dl) != dl) 337789Sahrens prev_dl = &cur_dl->dl_next; 338789Sahrens *prev_dl = dl->dl_next; 339789Sahrens cv_broadcast(&dl->dl_cv); 340789Sahrens mutex_exit(&dzp->z_lock); 341789Sahrens 342789Sahrens if (dl->dl_namesize != 0) 343789Sahrens kmem_free(dl->dl_name, dl->dl_namesize); 344789Sahrens cv_destroy(&dl->dl_cv); 345789Sahrens kmem_free(dl, sizeof (*dl)); 346789Sahrens } 347789Sahrens 348789Sahrens /* 349789Sahrens * Look up an entry in a directory. 350789Sahrens * 351789Sahrens * NOTE: '.' and '..' are handled as special cases because 352789Sahrens * no directory entries are actually stored for them. If this is 353789Sahrens * the root of a filesystem, then '.zfs' is also treated as a 354789Sahrens * special pseudo-directory. 355789Sahrens */ 356789Sahrens int 3575331Samw zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags, 3585331Samw int *deflg, pathname_t *rpnp) 359789Sahrens { 360789Sahrens zfs_dirlock_t *dl; 361789Sahrens znode_t *zp; 362789Sahrens int error = 0; 363789Sahrens 364789Sahrens if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { 365789Sahrens *vpp = ZTOV(dzp); 366789Sahrens VN_HOLD(*vpp); 367789Sahrens } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { 368789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 369789Sahrens /* 370789Sahrens * If we are a snapshot mounted under .zfs, return 371789Sahrens * the vp for the snapshot directory. 372789Sahrens */ 3731878Smaybee if (dzp->z_phys->zp_parent == dzp->z_id && 3741878Smaybee zfsvfs->z_parent != zfsvfs) { 375789Sahrens error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir, 3765331Samw "snapshot", vpp, NULL, 0, NULL, kcred, 3775331Samw NULL, NULL, NULL); 378789Sahrens return (error); 379789Sahrens } 380789Sahrens rw_enter(&dzp->z_parent_lock, RW_READER); 381789Sahrens error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp); 382789Sahrens if (error == 0) 383789Sahrens *vpp = ZTOV(zp); 384789Sahrens rw_exit(&dzp->z_parent_lock); 385789Sahrens } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) { 386789Sahrens *vpp = zfsctl_root(dzp); 387789Sahrens } else { 3885331Samw int zf; 3895331Samw 3905331Samw zf = ZEXISTS | ZSHARED; 3915331Samw if (flags & FIGNORECASE) 3925331Samw zf |= ZCILOOK; 3935331Samw 3945331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp); 395789Sahrens if (error == 0) { 396789Sahrens *vpp = ZTOV(zp); 397789Sahrens zfs_dirent_unlock(dl); 398869Sperrin dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ 399789Sahrens } 4005331Samw rpnp = NULL; 401789Sahrens } 402789Sahrens 4035331Samw if ((flags & FIGNORECASE) && rpnp) 4045331Samw (void) strlcpy(rpnp->pn_path, name, rpnp->pn_bufsize); 4055331Samw 406789Sahrens return (error); 407789Sahrens } 408789Sahrens 409789Sahrens static char * 4103461Sahrens zfs_unlinked_hexname(char namebuf[17], uint64_t x) 411789Sahrens { 412789Sahrens char *name = &namebuf[16]; 413789Sahrens const char digits[16] = "0123456789abcdef"; 414789Sahrens 415789Sahrens *name = '\0'; 416789Sahrens do { 417789Sahrens *--name = digits[x & 0xf]; 418789Sahrens x >>= 4; 419789Sahrens } while (x != 0); 420789Sahrens 421789Sahrens return (name); 422789Sahrens } 423789Sahrens 4241544Seschrock /* 4253461Sahrens * unlinked Set (formerly known as the "delete queue") Error Handling 4261544Seschrock * 4273461Sahrens * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we 4281544Seschrock * don't specify the name of the entry that we will be manipulating. We 4291544Seschrock * also fib and say that we won't be adding any new entries to the 4303461Sahrens * unlinked set, even though we might (this is to lower the minimum file 4311544Seschrock * size that can be deleted in a full filesystem). So on the small 4323461Sahrens * chance that the nlink list is using a fat zap (ie. has more than 4331544Seschrock * 2000 entries), we *may* not pre-read a block that's needed. 4341544Seschrock * Therefore it is remotely possible for some of the assertions 4353461Sahrens * regarding the unlinked set below to fail due to i/o error. On a 4361544Seschrock * nondebug system, this will result in the space being leaked. 4371544Seschrock */ 438789Sahrens void 4393461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx) 440789Sahrens { 441789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 442789Sahrens char obj_name[17]; 443789Sahrens int error; 444789Sahrens 4453461Sahrens ASSERT(zp->z_unlinked); 446789Sahrens ASSERT3U(zp->z_phys->zp_links, ==, 0); 447789Sahrens 4483461Sahrens error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj, 4493461Sahrens zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx); 450789Sahrens ASSERT3U(error, ==, 0); 451789Sahrens } 452789Sahrens 453789Sahrens /* 4543461Sahrens * Clean up any znodes that had no links when we either crashed or 4553461Sahrens * (force) umounted the file system. 4563461Sahrens */ 4573461Sahrens void 4583461Sahrens zfs_unlinked_drain(zfsvfs_t *zfsvfs) 4593461Sahrens { 4603461Sahrens zap_cursor_t zc; 4613461Sahrens zap_attribute_t zap; 4623461Sahrens dmu_object_info_t doi; 4633461Sahrens znode_t *zp; 4643461Sahrens int error; 4653461Sahrens 4663461Sahrens /* 4673461Sahrens * Interate over the contents of the unlinked set. 4683461Sahrens */ 4693461Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj); 4703461Sahrens zap_cursor_retrieve(&zc, &zap) == 0; 4713461Sahrens zap_cursor_advance(&zc)) { 4723461Sahrens 4733461Sahrens /* 4743461Sahrens * See what kind of object we have in list 4753461Sahrens */ 4763461Sahrens 4773461Sahrens error = dmu_object_info(zfsvfs->z_os, 4783461Sahrens zap.za_first_integer, &doi); 4793461Sahrens if (error != 0) 4803461Sahrens continue; 4813461Sahrens 4823461Sahrens ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) || 4833461Sahrens (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS)); 4843461Sahrens /* 4853461Sahrens * We need to re-mark these list entries for deletion, 4863461Sahrens * so we pull them back into core and set zp->z_unlinked. 4873461Sahrens */ 4883461Sahrens error = zfs_zget(zfsvfs, zap.za_first_integer, &zp); 4893461Sahrens 4903461Sahrens /* 4913461Sahrens * We may pick up znodes that are already marked for deletion. 4923461Sahrens * This could happen during the purge of an extended attribute 4933461Sahrens * directory. All we need to do is skip over them, since they 4943461Sahrens * are already in the system marked z_unlinked. 4953461Sahrens */ 4963461Sahrens if (error != 0) 4973461Sahrens continue; 4983461Sahrens 4993461Sahrens zp->z_unlinked = B_TRUE; 5003461Sahrens VN_RELE(ZTOV(zp)); 5013461Sahrens } 5023461Sahrens zap_cursor_fini(&zc); 5033461Sahrens } 5043461Sahrens 5053461Sahrens /* 506789Sahrens * Delete the entire contents of a directory. Return a count 507789Sahrens * of the number of entries that could not be deleted. 508789Sahrens * 509789Sahrens * NOTE: this function assumes that the directory is inactive, 510789Sahrens * so there is no need to lock its entries before deletion. 511789Sahrens * Also, it assumes the directory contents is *only* regular 512789Sahrens * files. 513789Sahrens */ 514789Sahrens static int 515789Sahrens zfs_purgedir(znode_t *dzp) 516789Sahrens { 517789Sahrens zap_cursor_t zc; 518789Sahrens zap_attribute_t zap; 519789Sahrens znode_t *xzp; 520789Sahrens dmu_tx_t *tx; 521789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 522789Sahrens zfs_dirlock_t dl; 523789Sahrens int skipped = 0; 524789Sahrens int error; 525789Sahrens 526789Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 527789Sahrens (error = zap_cursor_retrieve(&zc, &zap)) == 0; 528789Sahrens zap_cursor_advance(&zc)) { 5293912Slling error = zfs_zget(zfsvfs, 5303912Slling ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp); 531789Sahrens ASSERT3U(error, ==, 0); 532789Sahrens 533789Sahrens ASSERT((ZTOV(xzp)->v_type == VREG) || 534789Sahrens (ZTOV(xzp)->v_type == VLNK)); 535789Sahrens 536789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 537789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 5381544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); 539789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 5403461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 541789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 542789Sahrens if (error) { 543789Sahrens dmu_tx_abort(tx); 544789Sahrens VN_RELE(ZTOV(xzp)); 545789Sahrens skipped += 1; 546789Sahrens continue; 547789Sahrens } 548789Sahrens bzero(&dl, sizeof (dl)); 549789Sahrens dl.dl_dzp = dzp; 550789Sahrens dl.dl_name = zap.za_name; 551789Sahrens 552789Sahrens error = zfs_link_destroy(&dl, xzp, tx, 0, NULL); 553789Sahrens ASSERT3U(error, ==, 0); 554789Sahrens dmu_tx_commit(tx); 555789Sahrens 556789Sahrens VN_RELE(ZTOV(xzp)); 557789Sahrens } 558885Sahrens zap_cursor_fini(&zc); 559789Sahrens ASSERT(error == ENOENT); 560789Sahrens return (skipped); 561789Sahrens } 562789Sahrens 563789Sahrens void 564789Sahrens zfs_rmnode(znode_t *zp) 565789Sahrens { 566789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 567789Sahrens objset_t *os = zfsvfs->z_os; 568789Sahrens znode_t *xzp = NULL; 569789Sahrens char obj_name[17]; 570789Sahrens dmu_tx_t *tx; 571789Sahrens uint64_t acl_obj; 572789Sahrens int error; 573789Sahrens 574789Sahrens ASSERT(ZTOV(zp)->v_count == 0); 575789Sahrens ASSERT(zp->z_phys->zp_links == 0); 576789Sahrens 577789Sahrens /* 578789Sahrens * If this is an attribute directory, purge its contents. 579789Sahrens */ 5803461Sahrens if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) { 581789Sahrens if (zfs_purgedir(zp) != 0) { 582789Sahrens /* 5833461Sahrens * Not enough space to delete some xattrs. 5843461Sahrens * Leave it on the unlinked set. 585789Sahrens */ 586789Sahrens return; 587789Sahrens } 5883461Sahrens } 589789Sahrens 590789Sahrens /* 5913461Sahrens * If the file has extended attributes, we're going to unlink 5923461Sahrens * the xattr dir. 593789Sahrens */ 594789Sahrens if (zp->z_phys->zp_xattr) { 595789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 596789Sahrens ASSERT(error == 0); 597789Sahrens } 598789Sahrens 599789Sahrens acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 600789Sahrens 601789Sahrens /* 602789Sahrens * Set up the transaction. 603789Sahrens */ 604789Sahrens tx = dmu_tx_create(os); 605789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 6063461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 607789Sahrens if (xzp) { 608789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 6093461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL); 610789Sahrens } 611789Sahrens if (acl_obj) 612789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 613789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 614789Sahrens if (error) { 6153461Sahrens /* 6163461Sahrens * Not enough space to delete the file. Leave it in the 6173461Sahrens * unlinked set, leaking it until the fs is remounted (at 6183461Sahrens * which point we'll call zfs_unlinked_drain() to process it). 6193461Sahrens */ 620789Sahrens dmu_tx_abort(tx); 621789Sahrens return; 622789Sahrens } 623789Sahrens 624789Sahrens if (xzp) { 625789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 626789Sahrens mutex_enter(&xzp->z_lock); 6273461Sahrens xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */ 628789Sahrens xzp->z_phys->zp_links = 0; /* no more links to it */ 629789Sahrens mutex_exit(&xzp->z_lock); 6303461Sahrens zfs_unlinked_add(xzp, tx); 631789Sahrens } 632789Sahrens 6333461Sahrens /* Remove this znode from the unlinked set */ 6343461Sahrens error = zap_remove(os, zfsvfs->z_unlinkedobj, 6353461Sahrens zfs_unlinked_hexname(obj_name, zp->z_id), tx); 636789Sahrens ASSERT3U(error, ==, 0); 637789Sahrens 638789Sahrens zfs_znode_delete(zp, tx); 639789Sahrens 640789Sahrens dmu_tx_commit(tx); 641789Sahrens 642789Sahrens if (xzp) 643789Sahrens VN_RELE(ZTOV(xzp)); 644789Sahrens } 645789Sahrens 6464577Sahrens static uint64_t 6474577Sahrens zfs_dirent(znode_t *zp) 6484577Sahrens { 6494577Sahrens uint64_t de = zp->z_id; 6504577Sahrens if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE) 6514577Sahrens de |= IFTODT((zp)->z_phys->zp_mode) << 60; 6524577Sahrens return (de); 6534577Sahrens } 6544577Sahrens 655789Sahrens /* 6563461Sahrens * Link zp into dl. Can only fail if zp has been unlinked. 657789Sahrens */ 658789Sahrens int 659789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag) 660789Sahrens { 661789Sahrens znode_t *dzp = dl->dl_dzp; 662789Sahrens vnode_t *vp = ZTOV(zp); 6633912Slling uint64_t value; 664789Sahrens int zp_is_dir = (vp->v_type == VDIR); 665789Sahrens int error; 666789Sahrens 667789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 668789Sahrens mutex_enter(&zp->z_lock); 669789Sahrens 670789Sahrens if (!(flag & ZRENAMING)) { 6713461Sahrens if (zp->z_unlinked) { /* no new links to unlinked zp */ 672789Sahrens ASSERT(!(flag & (ZNEW | ZEXISTS))); 673789Sahrens mutex_exit(&zp->z_lock); 674789Sahrens return (ENOENT); 675789Sahrens } 676789Sahrens zp->z_phys->zp_links++; 677789Sahrens } 678789Sahrens zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */ 679789Sahrens 680789Sahrens if (!(flag & ZNEW)) 681789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 682789Sahrens mutex_exit(&zp->z_lock); 683789Sahrens 684789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 685789Sahrens mutex_enter(&dzp->z_lock); 686789Sahrens dzp->z_phys->zp_size++; /* one dirent added */ 687789Sahrens dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */ 688789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 689789Sahrens mutex_exit(&dzp->z_lock); 690789Sahrens 6914577Sahrens value = zfs_dirent(zp); 692789Sahrens error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, 6933912Slling 8, 1, &value, tx); 694789Sahrens ASSERT(error == 0); 695789Sahrens 6961484Sek110237 dnlc_update(ZTOV(dzp), dl->dl_name, vp); 6971484Sek110237 698789Sahrens return (0); 699789Sahrens } 700789Sahrens 701789Sahrens /* 7023461Sahrens * Unlink zp from dl, and mark zp for deletion if this was the last link. 703789Sahrens * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). 7043461Sahrens * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. 7053461Sahrens * If it's non-NULL, we use it to indicate whether the znode needs deletion, 706789Sahrens * and it's the caller's job to do it. 707789Sahrens */ 708789Sahrens int 709789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag, 7103461Sahrens boolean_t *unlinkedp) 711789Sahrens { 712789Sahrens znode_t *dzp = dl->dl_dzp; 713789Sahrens vnode_t *vp = ZTOV(zp); 714789Sahrens int zp_is_dir = (vp->v_type == VDIR); 7153461Sahrens boolean_t unlinked = B_FALSE; 716789Sahrens int error; 717789Sahrens 7181484Sek110237 dnlc_remove(ZTOV(dzp), dl->dl_name); 7191484Sek110237 720789Sahrens if (!(flag & ZRENAMING)) { 721789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 722789Sahrens 723789Sahrens if (vn_vfswlock(vp)) /* prevent new mounts on zp */ 724789Sahrens return (EBUSY); 725789Sahrens 726789Sahrens if (vn_ismntpt(vp)) { /* don't remove mount point */ 727789Sahrens vn_vfsunlock(vp); 728789Sahrens return (EBUSY); 729789Sahrens } 730789Sahrens 731789Sahrens mutex_enter(&zp->z_lock); 732789Sahrens if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */ 733789Sahrens mutex_exit(&zp->z_lock); 734789Sahrens vn_vfsunlock(vp); 735789Sahrens return (EEXIST); 736789Sahrens } 7373713Sahrens if (zp->z_phys->zp_links <= zp_is_dir) { 7383713Sahrens zfs_panic_recover("zfs: link count on %s is %u, " 7393713Sahrens "should be at least %u", 7403713Sahrens zp->z_vnode->v_path ? zp->z_vnode->v_path : 7413713Sahrens "<unknown>", (int)zp->z_phys->zp_links, 7423713Sahrens zp_is_dir + 1); 7433713Sahrens zp->z_phys->zp_links = zp_is_dir + 1; 7443713Sahrens } 745789Sahrens if (--zp->z_phys->zp_links == zp_is_dir) { 7463461Sahrens zp->z_unlinked = B_TRUE; 747789Sahrens zp->z_phys->zp_links = 0; 7483461Sahrens unlinked = B_TRUE; 749789Sahrens } else { 750789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 751789Sahrens } 752789Sahrens mutex_exit(&zp->z_lock); 753789Sahrens vn_vfsunlock(vp); 754789Sahrens } 755789Sahrens 756789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 757789Sahrens mutex_enter(&dzp->z_lock); 758789Sahrens dzp->z_phys->zp_size--; /* one dirent removed */ 759789Sahrens dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */ 760789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 761789Sahrens mutex_exit(&dzp->z_lock); 762789Sahrens 7635331Samw if (zp->z_zfsvfs->z_norm) { 7645331Samw if (((zp->z_zfsvfs->z_case & ZFS_CI_ONLY) && 7655331Samw (flag & ZCIEXACT)) || 7665331Samw ((zp->z_zfsvfs->z_case & ZFS_CI_MIXD) && 7675331Samw !(flag & ZCILOOK))) 7685331Samw error = zap_remove_norm(zp->z_zfsvfs->z_os, 7695331Samw dzp->z_id, dl->dl_name, MT_EXACT, tx); 7705331Samw else 7715331Samw error = zap_remove_norm(zp->z_zfsvfs->z_os, 7725331Samw dzp->z_id, dl->dl_name, MT_FIRST, tx); 7735331Samw } else { 7745331Samw error = zap_remove(zp->z_zfsvfs->z_os, 7755331Samw dzp->z_id, dl->dl_name, tx); 7765331Samw } 777789Sahrens ASSERT(error == 0); 778789Sahrens 7793461Sahrens if (unlinkedp != NULL) 7803461Sahrens *unlinkedp = unlinked; 7813461Sahrens else if (unlinked) 7823461Sahrens zfs_unlinked_add(zp, tx); 783789Sahrens 784789Sahrens return (0); 785789Sahrens } 786789Sahrens 787789Sahrens /* 788789Sahrens * Indicate whether the directory is empty. Works with or without z_lock 789789Sahrens * held, but can only be consider a hint in the latter case. Returns true 790789Sahrens * if only "." and ".." remain and there's no work in progress. 791789Sahrens */ 792789Sahrens boolean_t 793789Sahrens zfs_dirempty(znode_t *dzp) 794789Sahrens { 795789Sahrens return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0); 796789Sahrens } 797789Sahrens 798789Sahrens int 799789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) 800789Sahrens { 801789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 802789Sahrens znode_t *xzp; 803789Sahrens dmu_tx_t *tx; 804789Sahrens int error; 8055331Samw zfs_fuid_info_t *fuidp = NULL; 806789Sahrens 807789Sahrens *xvpp = NULL; 808789Sahrens 8095331Samw if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)) 810789Sahrens return (error); 811789Sahrens 812789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 813789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 8141544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 8155331Samw if (zfsvfs->z_fuid_obj == 0) { 8165331Samw dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 8175331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 8185331Samw dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 8195331Samw } else { 8205331Samw dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 8215331Samw dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, SPA_MAXBLOCKSIZE); 8225331Samw } 823789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 824789Sahrens if (error) { 8252113Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) 8262113Sahrens dmu_tx_wait(tx); 827789Sahrens dmu_tx_abort(tx); 828789Sahrens return (error); 829789Sahrens } 830*5446Sahrens zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp); 831789Sahrens ASSERT(xzp->z_phys->zp_parent == zp->z_id); 832789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 833*5446Sahrens zp->z_phys->zp_xattr = xzp->z_id; 834789Sahrens 8355331Samw (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, 8365331Samw xzp, "", NULL, fuidp, vap); 8375331Samw if (fuidp) 8385331Samw zfs_fuid_info_free(fuidp); 839789Sahrens dmu_tx_commit(tx); 840789Sahrens 841789Sahrens *xvpp = ZTOV(xzp); 842789Sahrens 843789Sahrens return (0); 844789Sahrens } 845789Sahrens 846789Sahrens /* 847789Sahrens * Return a znode for the extended attribute directory for zp. 848789Sahrens * ** If the directory does not already exist, it is created ** 849789Sahrens * 850789Sahrens * IN: zp - znode to obtain attribute directory from 851789Sahrens * cr - credentials of caller 8523280Sck153898 * flags - flags from the VOP_LOOKUP call 853789Sahrens * 854789Sahrens * OUT: xzpp - pointer to extended attribute znode 855789Sahrens * 856789Sahrens * RETURN: 0 on success 857789Sahrens * error number on failure 858789Sahrens */ 859789Sahrens int 8603280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags) 861789Sahrens { 862789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 863789Sahrens znode_t *xzp; 864789Sahrens zfs_dirlock_t *dl; 865789Sahrens vattr_t va; 866789Sahrens int error; 867789Sahrens top: 8685331Samw error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL); 869789Sahrens if (error) 870789Sahrens return (error); 871789Sahrens 872789Sahrens if (xzp != NULL) { 873789Sahrens *xvpp = ZTOV(xzp); 874789Sahrens zfs_dirent_unlock(dl); 875789Sahrens return (0); 876789Sahrens } 877789Sahrens 878789Sahrens ASSERT(zp->z_phys->zp_xattr == 0); 879789Sahrens 8803280Sck153898 if (!(flags & CREATE_XATTR_DIR)) { 8813280Sck153898 zfs_dirent_unlock(dl); 8823280Sck153898 return (ENOENT); 8833280Sck153898 } 8843280Sck153898 885789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 886789Sahrens zfs_dirent_unlock(dl); 887789Sahrens return (EROFS); 888789Sahrens } 889789Sahrens 890789Sahrens /* 891789Sahrens * The ability to 'create' files in an attribute 892789Sahrens * directory comes from the write_xattr permission on the base file. 893789Sahrens * 894789Sahrens * The ability to 'search' an attribute directory requires 895789Sahrens * read_xattr permission on the base file. 896789Sahrens * 897789Sahrens * Once in a directory the ability to read/write attributes 898789Sahrens * is controlled by the permissions on the attribute file. 899789Sahrens */ 900789Sahrens va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; 901789Sahrens va.va_type = VDIR; 9021231Smarks va.va_mode = S_IFDIR | S_ISVTX | 0777; 9035331Samw zfs_fuid_map_ids(zp, &va.va_uid, &va.va_gid); 904789Sahrens 905789Sahrens error = zfs_make_xattrdir(zp, &va, xvpp, cr); 906789Sahrens zfs_dirent_unlock(dl); 907789Sahrens 908789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 9092113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 910789Sahrens goto top; 911789Sahrens } 912789Sahrens 913789Sahrens return (error); 914789Sahrens } 915789Sahrens 916789Sahrens /* 917789Sahrens * Decide whether it is okay to remove within a sticky directory. 918789Sahrens * 919789Sahrens * In sticky directories, write access is not sufficient; 920789Sahrens * you can remove entries from a directory only if: 921789Sahrens * 922789Sahrens * you own the directory, 923789Sahrens * you own the entry, 924789Sahrens * the entry is a plain file and you have write access, 925789Sahrens * or you are privileged (checked in secpolicy...). 926789Sahrens * 927789Sahrens * The function returns 0 if remove access is granted. 928789Sahrens */ 929789Sahrens int 930789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr) 931789Sahrens { 932789Sahrens uid_t uid; 9335331Samw uid_t downer; 9345331Samw uid_t fowner; 9355331Samw zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 936789Sahrens 937789Sahrens if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */ 938789Sahrens return (0); 939789Sahrens 9405331Samw if ((zdp->z_phys->zp_mode & S_ISVTX) == 0) 9415331Samw return (0); 9425331Samw 9435331Samw zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, ZFS_OWNER, &downer); 9445331Samw zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, ZFS_OWNER, &fowner); 9455331Samw 9465331Samw if ((uid = crgetuid(cr)) == downer || uid == fowner || 947789Sahrens (ZTOV(zp)->v_type == VREG && 9485331Samw zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)) 949789Sahrens return (0); 950789Sahrens else 951789Sahrens return (secpolicy_vnode_remove(cr)); 952789Sahrens } 953