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 /* 221231Smarks * Copyright 2006 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> 551484Sek110237 #include <sys/dnlc.h> 56789Sahrens 57789Sahrens /* 58789Sahrens * Lock a directory entry. A dirlock on <dzp, name> protects that name 59789Sahrens * in dzp's directory zap object. As long as you hold a dirlock, you can 60789Sahrens * assume two things: (1) dzp cannot be reaped, and (2) no other thread 61789Sahrens * can change the zap entry for (i.e. link or unlink) this name. 62789Sahrens * 63789Sahrens * Input arguments: 64789Sahrens * dzp - znode for directory 65789Sahrens * name - name of entry to lock 66789Sahrens * flag - ZNEW: if the entry already exists, fail with EEXIST. 67789Sahrens * ZEXISTS: if the entry does not exist, fail with ENOENT. 68789Sahrens * ZSHARED: allow concurrent access with other ZSHARED callers. 69789Sahrens * ZXATTR: we want dzp's xattr directory 70789Sahrens * 71789Sahrens * Output arguments: 72789Sahrens * zpp - pointer to the znode for the entry (NULL if there isn't one) 73789Sahrens * dlpp - pointer to the dirlock for this entry (NULL on error) 74789Sahrens * 75789Sahrens * Return value: 0 on success or errno on failure. 76789Sahrens * 77789Sahrens * NOTE: Always checks for, and rejects, '.' and '..'. 78789Sahrens */ 79789Sahrens int 80789Sahrens zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp, 81789Sahrens int flag) 82789Sahrens { 83789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 84789Sahrens zfs_dirlock_t *dl; 85789Sahrens uint64_t zoid; 86789Sahrens int error; 871484Sek110237 vnode_t *vp; 88789Sahrens 89789Sahrens *zpp = NULL; 90789Sahrens *dlpp = NULL; 91789Sahrens 92789Sahrens /* 93789Sahrens * Verify that we are not trying to lock '.', '..', or '.zfs' 94789Sahrens */ 95789Sahrens if (name[0] == '.' && 96789Sahrens (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) || 97789Sahrens zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) 98789Sahrens return (EEXIST); 99789Sahrens 100789Sahrens /* 101789Sahrens * Wait until there are no locks on this name. 102789Sahrens */ 103789Sahrens mutex_enter(&dzp->z_lock); 104789Sahrens for (;;) { 105789Sahrens if (dzp->z_reap) { 106789Sahrens mutex_exit(&dzp->z_lock); 107789Sahrens return (ENOENT); 108789Sahrens } 109789Sahrens for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) 110789Sahrens if (strcmp(name, dl->dl_name) == 0) 111789Sahrens break; 112789Sahrens if (dl == NULL) { 113789Sahrens /* 114789Sahrens * Allocate a new dirlock and add it to the list. 115789Sahrens */ 116789Sahrens dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP); 117789Sahrens cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL); 118789Sahrens dl->dl_name = name; 119789Sahrens dl->dl_sharecnt = 0; 120789Sahrens dl->dl_namesize = 0; 121789Sahrens dl->dl_dzp = dzp; 122789Sahrens dl->dl_next = dzp->z_dirlocks; 123789Sahrens dzp->z_dirlocks = dl; 124789Sahrens break; 125789Sahrens } 126789Sahrens if ((flag & ZSHARED) && dl->dl_sharecnt != 0) 127789Sahrens break; 128789Sahrens cv_wait(&dl->dl_cv, &dzp->z_lock); 129789Sahrens } 130789Sahrens 131789Sahrens if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) { 132789Sahrens /* 133789Sahrens * We're the second shared reference to dl. Make a copy of 134789Sahrens * dl_name in case the first thread goes away before we do. 135789Sahrens * Note that we initialize the new name before storing its 136789Sahrens * pointer into dl_name, because the first thread may load 137789Sahrens * dl->dl_name at any time. He'll either see the old value, 138789Sahrens * which is his, or the new shared copy; either is OK. 139789Sahrens */ 140789Sahrens dl->dl_namesize = strlen(dl->dl_name) + 1; 141789Sahrens name = kmem_alloc(dl->dl_namesize, KM_SLEEP); 142789Sahrens bcopy(dl->dl_name, name, dl->dl_namesize); 143789Sahrens dl->dl_name = name; 144789Sahrens } 145789Sahrens 146789Sahrens mutex_exit(&dzp->z_lock); 147789Sahrens 148789Sahrens /* 149789Sahrens * We have a dirlock on the name. (Note that it is the dirlock, 150789Sahrens * not the dzp's z_lock, that protects the name in the zap object.) 151789Sahrens * See if there's an object by this name; if so, put a hold on it. 152789Sahrens */ 153789Sahrens if (flag & ZXATTR) { 154789Sahrens zoid = dzp->z_phys->zp_xattr; 155789Sahrens error = (zoid == 0 ? ENOENT : 0); 156789Sahrens } else { 1571484Sek110237 vp = dnlc_lookup(ZTOV(dzp), name); 1581484Sek110237 if (vp == DNLC_NO_VNODE) { 1591484Sek110237 VN_RELE(vp); 1601484Sek110237 error = ENOENT; 1611484Sek110237 } else if (vp) { 1621484Sek110237 if (flag & ZNEW) { 1631484Sek110237 zfs_dirent_unlock(dl); 1641484Sek110237 VN_RELE(vp); 1651484Sek110237 return (EEXIST); 1661484Sek110237 } 1671484Sek110237 *dlpp = dl; 1681484Sek110237 *zpp = VTOZ(vp); 1691484Sek110237 return (0); 1701484Sek110237 } else { 1711484Sek110237 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 1721484Sek110237 8, 1, &zoid); 1731484Sek110237 if (error == ENOENT) 1741484Sek110237 dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE); 1751484Sek110237 } 176789Sahrens } 177789Sahrens if (error) { 178789Sahrens if (error != ENOENT || (flag & ZEXISTS)) { 179789Sahrens zfs_dirent_unlock(dl); 180789Sahrens return (error); 181789Sahrens } 182789Sahrens } else { 183789Sahrens if (flag & ZNEW) { 184789Sahrens zfs_dirent_unlock(dl); 185789Sahrens return (EEXIST); 186789Sahrens } 187789Sahrens error = zfs_zget(zfsvfs, zoid, zpp); 188789Sahrens if (error) { 189789Sahrens zfs_dirent_unlock(dl); 190789Sahrens return (error); 191789Sahrens } 1921484Sek110237 if (!(flag & ZXATTR)) 1931484Sek110237 dnlc_update(ZTOV(dzp), name, ZTOV(*zpp)); 194789Sahrens } 195789Sahrens 196789Sahrens *dlpp = dl; 197789Sahrens 198789Sahrens return (0); 199789Sahrens } 200789Sahrens 201789Sahrens /* 202789Sahrens * Unlock this directory entry and wake anyone who was waiting for it. 203789Sahrens */ 204789Sahrens void 205789Sahrens zfs_dirent_unlock(zfs_dirlock_t *dl) 206789Sahrens { 207789Sahrens znode_t *dzp = dl->dl_dzp; 208789Sahrens zfs_dirlock_t **prev_dl, *cur_dl; 209789Sahrens 210789Sahrens mutex_enter(&dzp->z_lock); 211789Sahrens if (dl->dl_sharecnt > 1) { 212789Sahrens dl->dl_sharecnt--; 213789Sahrens mutex_exit(&dzp->z_lock); 214789Sahrens return; 215789Sahrens } 216789Sahrens prev_dl = &dzp->z_dirlocks; 217789Sahrens while ((cur_dl = *prev_dl) != dl) 218789Sahrens prev_dl = &cur_dl->dl_next; 219789Sahrens *prev_dl = dl->dl_next; 220789Sahrens cv_broadcast(&dl->dl_cv); 221789Sahrens mutex_exit(&dzp->z_lock); 222789Sahrens 223789Sahrens if (dl->dl_namesize != 0) 224789Sahrens kmem_free(dl->dl_name, dl->dl_namesize); 225789Sahrens cv_destroy(&dl->dl_cv); 226789Sahrens kmem_free(dl, sizeof (*dl)); 227789Sahrens } 228789Sahrens 229789Sahrens /* 230789Sahrens * Look up an entry in a directory. 231789Sahrens * 232789Sahrens * NOTE: '.' and '..' are handled as special cases because 233789Sahrens * no directory entries are actually stored for them. If this is 234789Sahrens * the root of a filesystem, then '.zfs' is also treated as a 235789Sahrens * special pseudo-directory. 236789Sahrens */ 237789Sahrens int 238789Sahrens zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp) 239789Sahrens { 240789Sahrens zfs_dirlock_t *dl; 241789Sahrens znode_t *zp; 242789Sahrens int error = 0; 243789Sahrens 244789Sahrens if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { 245789Sahrens *vpp = ZTOV(dzp); 246789Sahrens VN_HOLD(*vpp); 247789Sahrens } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { 248789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 249789Sahrens /* 250789Sahrens * If we are a snapshot mounted under .zfs, return 251789Sahrens * the vp for the snapshot directory. 252789Sahrens */ 2531878Smaybee if (dzp->z_phys->zp_parent == dzp->z_id && 2541878Smaybee zfsvfs->z_parent != zfsvfs) { 255789Sahrens error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir, 256789Sahrens "snapshot", vpp, NULL, 0, NULL, kcred); 257789Sahrens return (error); 258789Sahrens } 259789Sahrens rw_enter(&dzp->z_parent_lock, RW_READER); 260789Sahrens error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp); 261789Sahrens if (error == 0) 262789Sahrens *vpp = ZTOV(zp); 263789Sahrens rw_exit(&dzp->z_parent_lock); 264789Sahrens } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) { 265789Sahrens *vpp = zfsctl_root(dzp); 266789Sahrens } else { 267789Sahrens error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS | ZSHARED); 268789Sahrens if (error == 0) { 269789Sahrens *vpp = ZTOV(zp); 270789Sahrens zfs_dirent_unlock(dl); 271869Sperrin dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ 272789Sahrens } 273789Sahrens } 274789Sahrens 275789Sahrens return (error); 276789Sahrens } 277789Sahrens 278789Sahrens static char * 279789Sahrens zfs_dq_hexname(char namebuf[17], uint64_t x) 280789Sahrens { 281789Sahrens char *name = &namebuf[16]; 282789Sahrens const char digits[16] = "0123456789abcdef"; 283789Sahrens 284789Sahrens *name = '\0'; 285789Sahrens do { 286789Sahrens *--name = digits[x & 0xf]; 287789Sahrens x >>= 4; 288789Sahrens } while (x != 0); 289789Sahrens 290789Sahrens return (name); 291789Sahrens } 292789Sahrens 2931544Seschrock /* 2941544Seschrock * Delete Queue Error Handling 2951544Seschrock * 2961544Seschrock * When dealing with the delete queue, we dmu_tx_hold_zap(), but we 2971544Seschrock * don't specify the name of the entry that we will be manipulating. We 2981544Seschrock * also fib and say that we won't be adding any new entries to the 2991544Seschrock * delete queue, even though we might (this is to lower the minimum file 3001544Seschrock * size that can be deleted in a full filesystem). So on the small 3011544Seschrock * chance that the delete queue is using a fat zap (ie. has more than 3021544Seschrock * 2000 entries), we *may* not pre-read a block that's needed. 3031544Seschrock * Therefore it is remotely possible for some of the assertions 3041544Seschrock * regarding the delete queue below to fail due to i/o error. On a 3051544Seschrock * nondebug system, this will result in the space being leaked. 3061544Seschrock */ 3071544Seschrock 308789Sahrens void 309789Sahrens zfs_dq_add(znode_t *zp, dmu_tx_t *tx) 310789Sahrens { 311789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 312789Sahrens char obj_name[17]; 313789Sahrens int error; 314789Sahrens 315789Sahrens ASSERT(zp->z_reap); 316789Sahrens ASSERT3U(zp->z_phys->zp_links, ==, 0); 317789Sahrens 318789Sahrens error = zap_add(zfsvfs->z_os, zfsvfs->z_dqueue, 319789Sahrens zfs_dq_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx); 320789Sahrens ASSERT3U(error, ==, 0); 321789Sahrens } 322789Sahrens 323789Sahrens /* 324789Sahrens * Delete the entire contents of a directory. Return a count 325789Sahrens * of the number of entries that could not be deleted. 326789Sahrens * 327789Sahrens * NOTE: this function assumes that the directory is inactive, 328789Sahrens * so there is no need to lock its entries before deletion. 329789Sahrens * Also, it assumes the directory contents is *only* regular 330789Sahrens * files. 331789Sahrens */ 332789Sahrens static int 333789Sahrens zfs_purgedir(znode_t *dzp) 334789Sahrens { 335789Sahrens zap_cursor_t zc; 336789Sahrens zap_attribute_t zap; 337789Sahrens znode_t *xzp; 338789Sahrens dmu_tx_t *tx; 339789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 340789Sahrens zfs_dirlock_t dl; 341789Sahrens int skipped = 0; 342789Sahrens int error; 343789Sahrens 344789Sahrens ASSERT(dzp->z_active == 0); 345789Sahrens 346789Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 347789Sahrens (error = zap_cursor_retrieve(&zc, &zap)) == 0; 348789Sahrens zap_cursor_advance(&zc)) { 349789Sahrens error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp); 350789Sahrens ASSERT3U(error, ==, 0); 351789Sahrens 352789Sahrens ASSERT((ZTOV(xzp)->v_type == VREG) || 353789Sahrens (ZTOV(xzp)->v_type == VLNK)); 354789Sahrens 355789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 356789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 3571544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); 358789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 3591544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 360789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 361789Sahrens if (error) { 362789Sahrens dmu_tx_abort(tx); 363789Sahrens VN_RELE(ZTOV(xzp)); 364789Sahrens skipped += 1; 365789Sahrens continue; 366789Sahrens } 367789Sahrens bzero(&dl, sizeof (dl)); 368789Sahrens dl.dl_dzp = dzp; 369789Sahrens dl.dl_name = zap.za_name; 370789Sahrens 371789Sahrens error = zfs_link_destroy(&dl, xzp, tx, 0, NULL); 372789Sahrens ASSERT3U(error, ==, 0); 373789Sahrens dmu_tx_commit(tx); 374789Sahrens 375789Sahrens VN_RELE(ZTOV(xzp)); 376789Sahrens } 377885Sahrens zap_cursor_fini(&zc); 378789Sahrens ASSERT(error == ENOENT); 379789Sahrens return (skipped); 380789Sahrens } 381789Sahrens 382789Sahrens /* 383789Sahrens * Special function to requeue the znodes for deletion that were 384789Sahrens * in progress when we either crashed or umounted the file system. 385*2245Smarks * 386*2245Smarks * returns 1 if queue was drained. 387789Sahrens */ 388*2245Smarks static int 389789Sahrens zfs_drain_dq(zfsvfs_t *zfsvfs) 390789Sahrens { 391789Sahrens zap_cursor_t zc; 392789Sahrens zap_attribute_t zap; 393789Sahrens dmu_object_info_t doi; 394789Sahrens znode_t *zp; 395789Sahrens int error; 396789Sahrens 397789Sahrens /* 398789Sahrens * Interate over the contents of the delete queue. 399789Sahrens */ 400789Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_dqueue); 401789Sahrens zap_cursor_retrieve(&zc, &zap) == 0; 402789Sahrens zap_cursor_advance(&zc)) { 403789Sahrens 404789Sahrens /* 405*2245Smarks * Create more threads if necessary to balance the load. 406*2245Smarks * quit if the delete threads have been shut down. 407789Sahrens */ 408789Sahrens if (zfs_delete_thread_target(zfsvfs, -1) != 0) 409*2245Smarks return (0); 410789Sahrens 411789Sahrens /* 412789Sahrens * See what kind of object we have in queue 413789Sahrens */ 414789Sahrens 415789Sahrens error = dmu_object_info(zfsvfs->z_os, 416789Sahrens zap.za_first_integer, &doi); 417789Sahrens if (error != 0) 418789Sahrens continue; 419789Sahrens 420789Sahrens ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) || 421789Sahrens (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS)); 422789Sahrens /* 423789Sahrens * We need to re-mark these queue entries for reaping, 424789Sahrens * so we pull them back into core and set zp->z_reap. 425789Sahrens */ 426789Sahrens error = zfs_zget(zfsvfs, zap.za_first_integer, &zp); 427789Sahrens 428789Sahrens /* 429789Sahrens * We may pick up znodes that are already marked for reaping. 430789Sahrens * This could happen during the purge of an extended attribute 431789Sahrens * directory. All we need to do is skip over them, since they 432*2245Smarks * are already in the system to be processed by the delete 433*2245Smarks * thread(s). 434789Sahrens */ 435789Sahrens if (error != 0) { 436789Sahrens continue; 437789Sahrens } 438*2245Smarks 439789Sahrens zp->z_reap = 1; 440789Sahrens VN_RELE(ZTOV(zp)); 441789Sahrens } 442885Sahrens zap_cursor_fini(&zc); 443*2245Smarks return (1); 444789Sahrens } 445789Sahrens 446789Sahrens void 447789Sahrens zfs_delete_thread(void *arg) 448789Sahrens { 449789Sahrens zfsvfs_t *zfsvfs = arg; 450789Sahrens zfs_delete_t *zd = &zfsvfs->z_delete_head; 451789Sahrens znode_t *zp; 452789Sahrens callb_cpr_t cprinfo; 453*2245Smarks int drained; 454789Sahrens 455789Sahrens CALLB_CPR_INIT(&cprinfo, &zd->z_mutex, callb_generic_cpr, "zfs_delete"); 456789Sahrens 457789Sahrens mutex_enter(&zd->z_mutex); 458789Sahrens 459789Sahrens if (!zd->z_drained && !zd->z_draining) { 460789Sahrens zd->z_draining = B_TRUE; 461789Sahrens mutex_exit(&zd->z_mutex); 462*2245Smarks drained = zfs_drain_dq(zfsvfs); 463789Sahrens mutex_enter(&zd->z_mutex); 464789Sahrens zd->z_draining = B_FALSE; 465*2245Smarks zd->z_drained = drained; 466789Sahrens cv_broadcast(&zd->z_quiesce_cv); 467789Sahrens } 468789Sahrens 469789Sahrens while (zd->z_thread_count <= zd->z_thread_target) { 470789Sahrens zp = list_head(&zd->z_znodes); 471789Sahrens if (zp == NULL) { 472789Sahrens ASSERT(zd->z_znode_count == 0); 473789Sahrens CALLB_CPR_SAFE_BEGIN(&cprinfo); 474789Sahrens cv_wait(&zd->z_cv, &zd->z_mutex); 475789Sahrens CALLB_CPR_SAFE_END(&cprinfo, &zd->z_mutex); 476789Sahrens continue; 477789Sahrens } 478789Sahrens ASSERT(zd->z_znode_count != 0); 479789Sahrens list_remove(&zd->z_znodes, zp); 480789Sahrens if (--zd->z_znode_count == 0) 481789Sahrens cv_broadcast(&zd->z_quiesce_cv); 482789Sahrens mutex_exit(&zd->z_mutex); 483789Sahrens zfs_rmnode(zp); 484789Sahrens (void) zfs_delete_thread_target(zfsvfs, -1); 485789Sahrens mutex_enter(&zd->z_mutex); 486789Sahrens } 487789Sahrens 488789Sahrens ASSERT(zd->z_thread_count != 0); 489789Sahrens if (--zd->z_thread_count == 0) 490789Sahrens cv_broadcast(&zd->z_cv); 491789Sahrens 492789Sahrens CALLB_CPR_EXIT(&cprinfo); /* NB: drops z_mutex */ 493789Sahrens thread_exit(); 494789Sahrens } 495789Sahrens 496789Sahrens static int zfs_work_per_thread_shift = 11; /* 2048 (2^11) per thread */ 497789Sahrens 498789Sahrens /* 499789Sahrens * Set the target number of delete threads to 'nthreads'. 500789Sahrens * If nthreads == -1, choose a number based on current workload. 501789Sahrens * If nthreads == 0, don't return until the threads have exited. 502789Sahrens */ 503789Sahrens int 504789Sahrens zfs_delete_thread_target(zfsvfs_t *zfsvfs, int nthreads) 505789Sahrens { 506789Sahrens zfs_delete_t *zd = &zfsvfs->z_delete_head; 507789Sahrens 508789Sahrens mutex_enter(&zd->z_mutex); 509789Sahrens 510789Sahrens if (nthreads == -1) { 511789Sahrens if (zd->z_thread_target == 0) { 512789Sahrens mutex_exit(&zd->z_mutex); 513789Sahrens return (EBUSY); 514789Sahrens } 515789Sahrens nthreads = zd->z_znode_count >> zfs_work_per_thread_shift; 516789Sahrens nthreads = MIN(nthreads, ncpus << 1); 517789Sahrens nthreads = MAX(nthreads, 1); 518789Sahrens nthreads += !!zd->z_draining; 519789Sahrens } 520789Sahrens 521789Sahrens zd->z_thread_target = nthreads; 522789Sahrens 523789Sahrens while (zd->z_thread_count < zd->z_thread_target) { 524789Sahrens (void) thread_create(NULL, 0, zfs_delete_thread, zfsvfs, 525789Sahrens 0, &p0, TS_RUN, minclsyspri); 526789Sahrens zd->z_thread_count++; 527789Sahrens } 528789Sahrens 529789Sahrens while (zd->z_thread_count > zd->z_thread_target && nthreads == 0) { 530789Sahrens cv_broadcast(&zd->z_cv); 531789Sahrens cv_wait(&zd->z_cv, &zd->z_mutex); 532789Sahrens } 533789Sahrens 534789Sahrens mutex_exit(&zd->z_mutex); 535789Sahrens 536789Sahrens return (0); 537789Sahrens } 538789Sahrens 539789Sahrens /* 540789Sahrens * Wait until everything that's been queued has been deleted. 541789Sahrens */ 542789Sahrens void 543789Sahrens zfs_delete_wait_empty(zfsvfs_t *zfsvfs) 544789Sahrens { 545789Sahrens zfs_delete_t *zd = &zfsvfs->z_delete_head; 546789Sahrens 547789Sahrens mutex_enter(&zd->z_mutex); 548789Sahrens ASSERT(zd->z_thread_target != 0); 549789Sahrens while (!zd->z_drained || zd->z_znode_count != 0) { 550789Sahrens ASSERT(zd->z_thread_target != 0); 551789Sahrens cv_wait(&zd->z_quiesce_cv, &zd->z_mutex); 552789Sahrens } 553789Sahrens mutex_exit(&zd->z_mutex); 554789Sahrens } 555789Sahrens 556789Sahrens void 557789Sahrens zfs_rmnode(znode_t *zp) 558789Sahrens { 559789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 560789Sahrens objset_t *os = zfsvfs->z_os; 561789Sahrens znode_t *xzp = NULL; 562789Sahrens char obj_name[17]; 563789Sahrens dmu_tx_t *tx; 564789Sahrens uint64_t acl_obj; 565789Sahrens int error; 566789Sahrens 567789Sahrens ASSERT(zp->z_active == 0); 568789Sahrens ASSERT(ZTOV(zp)->v_count == 0); 569789Sahrens ASSERT(zp->z_phys->zp_links == 0); 570789Sahrens 571789Sahrens /* 572789Sahrens * If this is an attribute directory, purge its contents. 573789Sahrens */ 574789Sahrens if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) 575789Sahrens if (zfs_purgedir(zp) != 0) { 576789Sahrens zfs_delete_t *delq = &zfsvfs->z_delete_head; 577789Sahrens /* 578789Sahrens * Add this back to the delete list to be retried later. 579789Sahrens * 580789Sahrens * XXX - this could just busy loop on us... 581789Sahrens */ 582789Sahrens mutex_enter(&delq->z_mutex); 583789Sahrens list_insert_tail(&delq->z_znodes, zp); 584789Sahrens delq->z_znode_count++; 585789Sahrens mutex_exit(&delq->z_mutex); 586789Sahrens return; 587789Sahrens } 588789Sahrens 589789Sahrens /* 590789Sahrens * If the file has extended attributes, unlink the xattr dir. 591789Sahrens */ 592789Sahrens if (zp->z_phys->zp_xattr) { 593789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 594789Sahrens ASSERT(error == 0); 595789Sahrens } 596789Sahrens 597789Sahrens acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 598789Sahrens 599789Sahrens /* 600789Sahrens * Set up the transaction. 601789Sahrens */ 602789Sahrens tx = dmu_tx_create(os); 603789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 6041544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 605789Sahrens if (xzp) { 606789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 6071544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, TRUE, NULL); 608789Sahrens } 609789Sahrens if (acl_obj) 610789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 611789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 612789Sahrens if (error) { 613789Sahrens zfs_delete_t *delq = &zfsvfs->z_delete_head; 614789Sahrens 615789Sahrens dmu_tx_abort(tx); 616789Sahrens /* 617789Sahrens * Add this back to the delete list to be retried later. 618789Sahrens * 619789Sahrens * XXX - this could just busy loop on us... 620789Sahrens */ 621789Sahrens mutex_enter(&delq->z_mutex); 622789Sahrens list_insert_tail(&delq->z_znodes, zp); 623789Sahrens delq->z_znode_count++; 624789Sahrens mutex_exit(&delq->z_mutex); 625789Sahrens return; 626789Sahrens } 627789Sahrens 628789Sahrens if (xzp) { 629789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 630789Sahrens mutex_enter(&xzp->z_lock); 631789Sahrens xzp->z_reap = 1; /* mark xzp for deletion */ 632789Sahrens xzp->z_phys->zp_links = 0; /* no more links to it */ 633789Sahrens mutex_exit(&xzp->z_lock); 634789Sahrens zfs_dq_add(xzp, tx); /* add xzp to delete queue */ 635789Sahrens } 636789Sahrens 637789Sahrens /* 638789Sahrens * Remove this znode from delete queue 639789Sahrens */ 640789Sahrens error = zap_remove(os, zfsvfs->z_dqueue, 641789Sahrens zfs_dq_hexname(obj_name, zp->z_id), tx); 642789Sahrens ASSERT3U(error, ==, 0); 643789Sahrens 644789Sahrens zfs_znode_delete(zp, tx); 645789Sahrens 646789Sahrens dmu_tx_commit(tx); 647789Sahrens 648789Sahrens if (xzp) 649789Sahrens VN_RELE(ZTOV(xzp)); 650789Sahrens } 651789Sahrens 652789Sahrens /* 653789Sahrens * Link zp into dl. Can only fail if zp has been reaped. 654789Sahrens */ 655789Sahrens int 656789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag) 657789Sahrens { 658789Sahrens znode_t *dzp = dl->dl_dzp; 659789Sahrens vnode_t *vp = ZTOV(zp); 660789Sahrens int zp_is_dir = (vp->v_type == VDIR); 661789Sahrens int error; 662789Sahrens 663789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 664789Sahrens mutex_enter(&zp->z_lock); 665789Sahrens 666789Sahrens if (!(flag & ZRENAMING)) { 667789Sahrens if (zp->z_reap) { /* no new links to reaped zp */ 668789Sahrens ASSERT(!(flag & (ZNEW | ZEXISTS))); 669789Sahrens mutex_exit(&zp->z_lock); 670789Sahrens return (ENOENT); 671789Sahrens } 672789Sahrens zp->z_phys->zp_links++; 673789Sahrens } 674789Sahrens zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */ 675789Sahrens 676789Sahrens if (!(flag & ZNEW)) 677789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 678789Sahrens mutex_exit(&zp->z_lock); 679789Sahrens 680789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 681789Sahrens mutex_enter(&dzp->z_lock); 682789Sahrens dzp->z_phys->zp_size++; /* one dirent added */ 683789Sahrens dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */ 684789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 685789Sahrens mutex_exit(&dzp->z_lock); 686789Sahrens 687789Sahrens error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, 688789Sahrens 8, 1, &zp->z_id, tx); 689789Sahrens ASSERT(error == 0); 690789Sahrens 6911484Sek110237 dnlc_update(ZTOV(dzp), dl->dl_name, vp); 6921484Sek110237 693789Sahrens return (0); 694789Sahrens } 695789Sahrens 696789Sahrens /* 697789Sahrens * Unlink zp from dl, and mark zp for reaping if this was the last link. 698789Sahrens * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). 699789Sahrens * If 'reaped_ptr' is NULL, we put reaped znodes on the delete queue. 700789Sahrens * If it's non-NULL, we use it to indicate whether the znode needs reaping, 701789Sahrens * and it's the caller's job to do it. 702789Sahrens */ 703789Sahrens int 704789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag, 705789Sahrens int *reaped_ptr) 706789Sahrens { 707789Sahrens znode_t *dzp = dl->dl_dzp; 708789Sahrens vnode_t *vp = ZTOV(zp); 709789Sahrens int zp_is_dir = (vp->v_type == VDIR); 710789Sahrens int reaped = 0; 711789Sahrens int error; 712789Sahrens 7131484Sek110237 dnlc_remove(ZTOV(dzp), dl->dl_name); 7141484Sek110237 715789Sahrens if (!(flag & ZRENAMING)) { 716789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 717789Sahrens 718789Sahrens if (vn_vfswlock(vp)) /* prevent new mounts on zp */ 719789Sahrens return (EBUSY); 720789Sahrens 721789Sahrens if (vn_ismntpt(vp)) { /* don't remove mount point */ 722789Sahrens vn_vfsunlock(vp); 723789Sahrens return (EBUSY); 724789Sahrens } 725789Sahrens 726789Sahrens mutex_enter(&zp->z_lock); 727789Sahrens if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */ 728789Sahrens mutex_exit(&zp->z_lock); 729789Sahrens vn_vfsunlock(vp); 730789Sahrens return (EEXIST); 731789Sahrens } 732789Sahrens ASSERT(zp->z_phys->zp_links > zp_is_dir); 733789Sahrens if (--zp->z_phys->zp_links == zp_is_dir) { 734789Sahrens zp->z_reap = 1; 735789Sahrens zp->z_phys->zp_links = 0; 736789Sahrens reaped = 1; 737789Sahrens } else { 738789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 739789Sahrens } 740789Sahrens mutex_exit(&zp->z_lock); 741789Sahrens vn_vfsunlock(vp); 742789Sahrens } 743789Sahrens 744789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 745789Sahrens mutex_enter(&dzp->z_lock); 746789Sahrens dzp->z_phys->zp_size--; /* one dirent removed */ 747789Sahrens dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */ 748789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 749789Sahrens mutex_exit(&dzp->z_lock); 750789Sahrens 751789Sahrens error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx); 752789Sahrens ASSERT(error == 0); 753789Sahrens 754789Sahrens if (reaped_ptr != NULL) 755789Sahrens *reaped_ptr = reaped; 756789Sahrens else if (reaped) 757789Sahrens zfs_dq_add(zp, tx); 758789Sahrens 759789Sahrens return (0); 760789Sahrens } 761789Sahrens 762789Sahrens /* 763789Sahrens * Indicate whether the directory is empty. Works with or without z_lock 764789Sahrens * held, but can only be consider a hint in the latter case. Returns true 765789Sahrens * if only "." and ".." remain and there's no work in progress. 766789Sahrens */ 767789Sahrens boolean_t 768789Sahrens zfs_dirempty(znode_t *dzp) 769789Sahrens { 770789Sahrens return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0); 771789Sahrens } 772789Sahrens 773789Sahrens int 774789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) 775789Sahrens { 776789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 777789Sahrens znode_t *xzp; 778789Sahrens dmu_tx_t *tx; 779789Sahrens uint64_t xoid; 780789Sahrens int error; 781789Sahrens 782789Sahrens *xvpp = NULL; 783789Sahrens 784789Sahrens if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr)) 785789Sahrens return (error); 786789Sahrens 787789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 788789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 7891544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 790789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 791789Sahrens if (error) { 7922113Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) 7932113Sahrens dmu_tx_wait(tx); 794789Sahrens dmu_tx_abort(tx); 795789Sahrens return (error); 796789Sahrens } 797789Sahrens zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0); 798789Sahrens ASSERT(xzp->z_id == xoid); 799789Sahrens ASSERT(xzp->z_phys->zp_parent == zp->z_id); 800789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 801789Sahrens zp->z_phys->zp_xattr = xoid; 802789Sahrens 803789Sahrens (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, ""); 804789Sahrens dmu_tx_commit(tx); 805789Sahrens 806789Sahrens *xvpp = ZTOV(xzp); 807789Sahrens 808789Sahrens return (0); 809789Sahrens } 810789Sahrens 811789Sahrens /* 812789Sahrens * Return a znode for the extended attribute directory for zp. 813789Sahrens * ** If the directory does not already exist, it is created ** 814789Sahrens * 815789Sahrens * IN: zp - znode to obtain attribute directory from 816789Sahrens * cr - credentials of caller 817789Sahrens * 818789Sahrens * OUT: xzpp - pointer to extended attribute znode 819789Sahrens * 820789Sahrens * RETURN: 0 on success 821789Sahrens * error number on failure 822789Sahrens */ 823789Sahrens int 824789Sahrens zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr) 825789Sahrens { 826789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 827789Sahrens znode_t *xzp; 828789Sahrens zfs_dirlock_t *dl; 829789Sahrens vattr_t va; 830789Sahrens int error; 831789Sahrens top: 832789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR); 833789Sahrens if (error) 834789Sahrens return (error); 835789Sahrens 836789Sahrens if (xzp != NULL) { 837789Sahrens *xvpp = ZTOV(xzp); 838789Sahrens zfs_dirent_unlock(dl); 839789Sahrens return (0); 840789Sahrens } 841789Sahrens 842789Sahrens ASSERT(zp->z_phys->zp_xattr == 0); 843789Sahrens 844789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 845789Sahrens zfs_dirent_unlock(dl); 846789Sahrens return (EROFS); 847789Sahrens } 848789Sahrens 849789Sahrens /* 850789Sahrens * The ability to 'create' files in an attribute 851789Sahrens * directory comes from the write_xattr permission on the base file. 852789Sahrens * 853789Sahrens * The ability to 'search' an attribute directory requires 854789Sahrens * read_xattr permission on the base file. 855789Sahrens * 856789Sahrens * Once in a directory the ability to read/write attributes 857789Sahrens * is controlled by the permissions on the attribute file. 858789Sahrens */ 859789Sahrens va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; 860789Sahrens va.va_type = VDIR; 8611231Smarks va.va_mode = S_IFDIR | S_ISVTX | 0777; 862789Sahrens va.va_uid = (uid_t)zp->z_phys->zp_uid; 863789Sahrens va.va_gid = (gid_t)zp->z_phys->zp_gid; 864789Sahrens 865789Sahrens error = zfs_make_xattrdir(zp, &va, xvpp, cr); 866789Sahrens zfs_dirent_unlock(dl); 867789Sahrens 868789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 8692113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 870789Sahrens goto top; 871789Sahrens } 872789Sahrens 873789Sahrens return (error); 874789Sahrens } 875789Sahrens 876789Sahrens /* 877789Sahrens * Decide whether it is okay to remove within a sticky directory. 878789Sahrens * 879789Sahrens * In sticky directories, write access is not sufficient; 880789Sahrens * you can remove entries from a directory only if: 881789Sahrens * 882789Sahrens * you own the directory, 883789Sahrens * you own the entry, 884789Sahrens * the entry is a plain file and you have write access, 885789Sahrens * or you are privileged (checked in secpolicy...). 886789Sahrens * 887789Sahrens * The function returns 0 if remove access is granted. 888789Sahrens */ 889789Sahrens int 890789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr) 891789Sahrens { 892789Sahrens uid_t uid; 893789Sahrens 894789Sahrens if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */ 895789Sahrens return (0); 896789Sahrens 897789Sahrens if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 || 898789Sahrens (uid = crgetuid(cr)) == zdp->z_phys->zp_uid || 899789Sahrens uid == zp->z_phys->zp_uid || 900789Sahrens (ZTOV(zp)->v_type == VREG && 901789Sahrens zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0)) 902789Sahrens return (0); 903789Sahrens else 904789Sahrens return (secpolicy_vnode_remove(cr)); 905789Sahrens } 906