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> 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 (;;) { 1053461Sahrens if (dzp->z_unlinked) { 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 * 2793461Sahrens zfs_unlinked_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 /* 2943461Sahrens * unlinked Set (formerly known as the "delete queue") Error Handling 2951544Seschrock * 2963461Sahrens * When dealing with the unlinked set, 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 2993461Sahrens * unlinked set, 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 3013461Sahrens * chance that the nlink list 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 3043461Sahrens * regarding the unlinked set below to fail due to i/o error. On a 3051544Seschrock * nondebug system, this will result in the space being leaked. 3061544Seschrock */ 307789Sahrens void 3083461Sahrens zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx) 309789Sahrens { 310789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 311789Sahrens char obj_name[17]; 312789Sahrens int error; 313789Sahrens 3143461Sahrens ASSERT(zp->z_unlinked); 315789Sahrens ASSERT3U(zp->z_phys->zp_links, ==, 0); 316789Sahrens 3173461Sahrens error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj, 3183461Sahrens zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx); 319789Sahrens ASSERT3U(error, ==, 0); 320789Sahrens } 321789Sahrens 322789Sahrens /* 3233461Sahrens * Clean up any znodes that had no links when we either crashed or 3243461Sahrens * (force) umounted the file system. 3253461Sahrens */ 3263461Sahrens void 3273461Sahrens zfs_unlinked_drain(zfsvfs_t *zfsvfs) 3283461Sahrens { 3293461Sahrens zap_cursor_t zc; 3303461Sahrens zap_attribute_t zap; 3313461Sahrens dmu_object_info_t doi; 3323461Sahrens znode_t *zp; 3333461Sahrens int error; 3343461Sahrens 3353461Sahrens /* 3363461Sahrens * Interate over the contents of the unlinked set. 3373461Sahrens */ 3383461Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj); 3393461Sahrens zap_cursor_retrieve(&zc, &zap) == 0; 3403461Sahrens zap_cursor_advance(&zc)) { 3413461Sahrens 3423461Sahrens /* 3433461Sahrens * See what kind of object we have in list 3443461Sahrens */ 3453461Sahrens 3463461Sahrens error = dmu_object_info(zfsvfs->z_os, 3473461Sahrens zap.za_first_integer, &doi); 3483461Sahrens if (error != 0) 3493461Sahrens continue; 3503461Sahrens 3513461Sahrens ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) || 3523461Sahrens (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS)); 3533461Sahrens /* 3543461Sahrens * We need to re-mark these list entries for deletion, 3553461Sahrens * so we pull them back into core and set zp->z_unlinked. 3563461Sahrens */ 3573461Sahrens error = zfs_zget(zfsvfs, zap.za_first_integer, &zp); 3583461Sahrens 3593461Sahrens /* 3603461Sahrens * We may pick up znodes that are already marked for deletion. 3613461Sahrens * This could happen during the purge of an extended attribute 3623461Sahrens * directory. All we need to do is skip over them, since they 3633461Sahrens * are already in the system marked z_unlinked. 3643461Sahrens */ 3653461Sahrens if (error != 0) 3663461Sahrens continue; 3673461Sahrens 3683461Sahrens zp->z_unlinked = B_TRUE; 3693461Sahrens VN_RELE(ZTOV(zp)); 3703461Sahrens } 3713461Sahrens zap_cursor_fini(&zc); 3723461Sahrens } 3733461Sahrens 3743461Sahrens /* 375789Sahrens * Delete the entire contents of a directory. Return a count 376789Sahrens * of the number of entries that could not be deleted. 377789Sahrens * 378789Sahrens * NOTE: this function assumes that the directory is inactive, 379789Sahrens * so there is no need to lock its entries before deletion. 380789Sahrens * Also, it assumes the directory contents is *only* regular 381789Sahrens * files. 382789Sahrens */ 383789Sahrens static int 384789Sahrens zfs_purgedir(znode_t *dzp) 385789Sahrens { 386789Sahrens zap_cursor_t zc; 387789Sahrens zap_attribute_t zap; 388789Sahrens znode_t *xzp; 389789Sahrens dmu_tx_t *tx; 390789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 391789Sahrens zfs_dirlock_t dl; 392789Sahrens int skipped = 0; 393789Sahrens int error; 394789Sahrens 395789Sahrens for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 396789Sahrens (error = zap_cursor_retrieve(&zc, &zap)) == 0; 397789Sahrens zap_cursor_advance(&zc)) { 398789Sahrens error = zfs_zget(zfsvfs, zap.za_first_integer, &xzp); 399789Sahrens ASSERT3U(error, ==, 0); 400789Sahrens 401789Sahrens ASSERT((ZTOV(xzp)->v_type == VREG) || 402789Sahrens (ZTOV(xzp)->v_type == VLNK)); 403789Sahrens 404789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 405789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 4061544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); 407789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 4083461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 409789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 410789Sahrens if (error) { 411789Sahrens dmu_tx_abort(tx); 412789Sahrens VN_RELE(ZTOV(xzp)); 413789Sahrens skipped += 1; 414789Sahrens continue; 415789Sahrens } 416789Sahrens bzero(&dl, sizeof (dl)); 417789Sahrens dl.dl_dzp = dzp; 418789Sahrens dl.dl_name = zap.za_name; 419789Sahrens 420789Sahrens error = zfs_link_destroy(&dl, xzp, tx, 0, NULL); 421789Sahrens ASSERT3U(error, ==, 0); 422789Sahrens dmu_tx_commit(tx); 423789Sahrens 424789Sahrens VN_RELE(ZTOV(xzp)); 425789Sahrens } 426885Sahrens zap_cursor_fini(&zc); 427789Sahrens ASSERT(error == ENOENT); 428789Sahrens return (skipped); 429789Sahrens } 430789Sahrens 431789Sahrens void 432789Sahrens zfs_rmnode(znode_t *zp) 433789Sahrens { 434789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 435789Sahrens objset_t *os = zfsvfs->z_os; 436789Sahrens znode_t *xzp = NULL; 437789Sahrens char obj_name[17]; 438789Sahrens dmu_tx_t *tx; 439789Sahrens uint64_t acl_obj; 440789Sahrens int error; 441789Sahrens 442789Sahrens ASSERT(ZTOV(zp)->v_count == 0); 443789Sahrens ASSERT(zp->z_phys->zp_links == 0); 444789Sahrens 445789Sahrens /* 446789Sahrens * If this is an attribute directory, purge its contents. 447789Sahrens */ 4483461Sahrens if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) { 449789Sahrens if (zfs_purgedir(zp) != 0) { 450789Sahrens /* 4513461Sahrens * Not enough space to delete some xattrs. 4523461Sahrens * Leave it on the unlinked set. 453789Sahrens */ 454789Sahrens return; 455789Sahrens } 4563461Sahrens } 457789Sahrens 458789Sahrens /* 4593461Sahrens * If the file has extended attributes, we're going to unlink 4603461Sahrens * the xattr dir. 461789Sahrens */ 462789Sahrens if (zp->z_phys->zp_xattr) { 463789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 464789Sahrens ASSERT(error == 0); 465789Sahrens } 466789Sahrens 467789Sahrens acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 468789Sahrens 469789Sahrens /* 470789Sahrens * Set up the transaction. 471789Sahrens */ 472789Sahrens tx = dmu_tx_create(os); 473789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 4743461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 475789Sahrens if (xzp) { 476789Sahrens dmu_tx_hold_bonus(tx, xzp->z_id); 4773461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL); 478789Sahrens } 479789Sahrens if (acl_obj) 480789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 481789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 482789Sahrens if (error) { 4833461Sahrens /* 4843461Sahrens * Not enough space to delete the file. Leave it in the 4853461Sahrens * unlinked set, leaking it until the fs is remounted (at 4863461Sahrens * which point we'll call zfs_unlinked_drain() to process it). 4873461Sahrens */ 488789Sahrens dmu_tx_abort(tx); 489789Sahrens return; 490789Sahrens } 491789Sahrens 492789Sahrens if (xzp) { 493789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 494789Sahrens mutex_enter(&xzp->z_lock); 4953461Sahrens xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */ 496789Sahrens xzp->z_phys->zp_links = 0; /* no more links to it */ 497789Sahrens mutex_exit(&xzp->z_lock); 4983461Sahrens zfs_unlinked_add(xzp, tx); 499789Sahrens } 500789Sahrens 5013461Sahrens /* Remove this znode from the unlinked set */ 5023461Sahrens error = zap_remove(os, zfsvfs->z_unlinkedobj, 5033461Sahrens zfs_unlinked_hexname(obj_name, zp->z_id), tx); 504789Sahrens ASSERT3U(error, ==, 0); 505789Sahrens 506789Sahrens zfs_znode_delete(zp, tx); 507789Sahrens 508789Sahrens dmu_tx_commit(tx); 509789Sahrens 510789Sahrens if (xzp) 511789Sahrens VN_RELE(ZTOV(xzp)); 512789Sahrens } 513789Sahrens 514789Sahrens /* 5153461Sahrens * Link zp into dl. Can only fail if zp has been unlinked. 516789Sahrens */ 517789Sahrens int 518789Sahrens zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag) 519789Sahrens { 520789Sahrens znode_t *dzp = dl->dl_dzp; 521789Sahrens vnode_t *vp = ZTOV(zp); 522789Sahrens int zp_is_dir = (vp->v_type == VDIR); 523789Sahrens int error; 524789Sahrens 525789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 526789Sahrens mutex_enter(&zp->z_lock); 527789Sahrens 528789Sahrens if (!(flag & ZRENAMING)) { 5293461Sahrens if (zp->z_unlinked) { /* no new links to unlinked zp */ 530789Sahrens ASSERT(!(flag & (ZNEW | ZEXISTS))); 531789Sahrens mutex_exit(&zp->z_lock); 532789Sahrens return (ENOENT); 533789Sahrens } 534789Sahrens zp->z_phys->zp_links++; 535789Sahrens } 536789Sahrens zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */ 537789Sahrens 538789Sahrens if (!(flag & ZNEW)) 539789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 540789Sahrens mutex_exit(&zp->z_lock); 541789Sahrens 542789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 543789Sahrens mutex_enter(&dzp->z_lock); 544789Sahrens dzp->z_phys->zp_size++; /* one dirent added */ 545789Sahrens dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */ 546789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 547789Sahrens mutex_exit(&dzp->z_lock); 548789Sahrens 549789Sahrens error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, 550789Sahrens 8, 1, &zp->z_id, tx); 551789Sahrens ASSERT(error == 0); 552789Sahrens 5531484Sek110237 dnlc_update(ZTOV(dzp), dl->dl_name, vp); 5541484Sek110237 555789Sahrens return (0); 556789Sahrens } 557789Sahrens 558789Sahrens /* 5593461Sahrens * Unlink zp from dl, and mark zp for deletion if this was the last link. 560789Sahrens * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). 5613461Sahrens * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. 5623461Sahrens * If it's non-NULL, we use it to indicate whether the znode needs deletion, 563789Sahrens * and it's the caller's job to do it. 564789Sahrens */ 565789Sahrens int 566789Sahrens zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag, 5673461Sahrens boolean_t *unlinkedp) 568789Sahrens { 569789Sahrens znode_t *dzp = dl->dl_dzp; 570789Sahrens vnode_t *vp = ZTOV(zp); 571789Sahrens int zp_is_dir = (vp->v_type == VDIR); 5723461Sahrens boolean_t unlinked = B_FALSE; 573789Sahrens int error; 574789Sahrens 5751484Sek110237 dnlc_remove(ZTOV(dzp), dl->dl_name); 5761484Sek110237 577789Sahrens if (!(flag & ZRENAMING)) { 578789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 579789Sahrens 580789Sahrens if (vn_vfswlock(vp)) /* prevent new mounts on zp */ 581789Sahrens return (EBUSY); 582789Sahrens 583789Sahrens if (vn_ismntpt(vp)) { /* don't remove mount point */ 584789Sahrens vn_vfsunlock(vp); 585789Sahrens return (EBUSY); 586789Sahrens } 587789Sahrens 588789Sahrens mutex_enter(&zp->z_lock); 589789Sahrens if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */ 590789Sahrens mutex_exit(&zp->z_lock); 591789Sahrens vn_vfsunlock(vp); 592789Sahrens return (EEXIST); 593789Sahrens } 594*3713Sahrens if (zp->z_phys->zp_links <= zp_is_dir) { 595*3713Sahrens zfs_panic_recover("zfs: link count on %s is %u, " 596*3713Sahrens "should be at least %u", 597*3713Sahrens zp->z_vnode->v_path ? zp->z_vnode->v_path : 598*3713Sahrens "<unknown>", (int)zp->z_phys->zp_links, 599*3713Sahrens zp_is_dir + 1); 600*3713Sahrens zp->z_phys->zp_links = zp_is_dir + 1; 601*3713Sahrens } 602789Sahrens if (--zp->z_phys->zp_links == zp_is_dir) { 6033461Sahrens zp->z_unlinked = B_TRUE; 604789Sahrens zp->z_phys->zp_links = 0; 6053461Sahrens unlinked = B_TRUE; 606789Sahrens } else { 607789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 608789Sahrens } 609789Sahrens mutex_exit(&zp->z_lock); 610789Sahrens vn_vfsunlock(vp); 611789Sahrens } 612789Sahrens 613789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 614789Sahrens mutex_enter(&dzp->z_lock); 615789Sahrens dzp->z_phys->zp_size--; /* one dirent removed */ 616789Sahrens dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */ 617789Sahrens zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 618789Sahrens mutex_exit(&dzp->z_lock); 619789Sahrens 620789Sahrens error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, tx); 621789Sahrens ASSERT(error == 0); 622789Sahrens 6233461Sahrens if (unlinkedp != NULL) 6243461Sahrens *unlinkedp = unlinked; 6253461Sahrens else if (unlinked) 6263461Sahrens zfs_unlinked_add(zp, tx); 627789Sahrens 628789Sahrens return (0); 629789Sahrens } 630789Sahrens 631789Sahrens /* 632789Sahrens * Indicate whether the directory is empty. Works with or without z_lock 633789Sahrens * held, but can only be consider a hint in the latter case. Returns true 634789Sahrens * if only "." and ".." remain and there's no work in progress. 635789Sahrens */ 636789Sahrens boolean_t 637789Sahrens zfs_dirempty(znode_t *dzp) 638789Sahrens { 639789Sahrens return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0); 640789Sahrens } 641789Sahrens 642789Sahrens int 643789Sahrens zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) 644789Sahrens { 645789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 646789Sahrens znode_t *xzp; 647789Sahrens dmu_tx_t *tx; 648789Sahrens uint64_t xoid; 649789Sahrens int error; 650789Sahrens 651789Sahrens *xvpp = NULL; 652789Sahrens 653789Sahrens if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, cr)) 654789Sahrens return (error); 655789Sahrens 656789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 657789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 6581544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 659789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 660789Sahrens if (error) { 6612113Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) 6622113Sahrens dmu_tx_wait(tx); 663789Sahrens dmu_tx_abort(tx); 664789Sahrens return (error); 665789Sahrens } 666789Sahrens zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0); 667789Sahrens ASSERT(xzp->z_id == xoid); 668789Sahrens ASSERT(xzp->z_phys->zp_parent == zp->z_id); 669789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 670789Sahrens zp->z_phys->zp_xattr = xoid; 671789Sahrens 672789Sahrens (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, ""); 673789Sahrens dmu_tx_commit(tx); 674789Sahrens 675789Sahrens *xvpp = ZTOV(xzp); 676789Sahrens 677789Sahrens return (0); 678789Sahrens } 679789Sahrens 680789Sahrens /* 681789Sahrens * Return a znode for the extended attribute directory for zp. 682789Sahrens * ** If the directory does not already exist, it is created ** 683789Sahrens * 684789Sahrens * IN: zp - znode to obtain attribute directory from 685789Sahrens * cr - credentials of caller 6863280Sck153898 * flags - flags from the VOP_LOOKUP call 687789Sahrens * 688789Sahrens * OUT: xzpp - pointer to extended attribute znode 689789Sahrens * 690789Sahrens * RETURN: 0 on success 691789Sahrens * error number on failure 692789Sahrens */ 693789Sahrens int 6943280Sck153898 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags) 695789Sahrens { 696789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 697789Sahrens znode_t *xzp; 698789Sahrens zfs_dirlock_t *dl; 699789Sahrens vattr_t va; 700789Sahrens int error; 701789Sahrens top: 702789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR); 703789Sahrens if (error) 704789Sahrens return (error); 705789Sahrens 706789Sahrens if (xzp != NULL) { 707789Sahrens *xvpp = ZTOV(xzp); 708789Sahrens zfs_dirent_unlock(dl); 709789Sahrens return (0); 710789Sahrens } 711789Sahrens 712789Sahrens ASSERT(zp->z_phys->zp_xattr == 0); 713789Sahrens 7143280Sck153898 if (!(flags & CREATE_XATTR_DIR)) { 7153280Sck153898 zfs_dirent_unlock(dl); 7163280Sck153898 return (ENOENT); 7173280Sck153898 } 7183280Sck153898 719789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 720789Sahrens zfs_dirent_unlock(dl); 721789Sahrens return (EROFS); 722789Sahrens } 723789Sahrens 724789Sahrens /* 725789Sahrens * The ability to 'create' files in an attribute 726789Sahrens * directory comes from the write_xattr permission on the base file. 727789Sahrens * 728789Sahrens * The ability to 'search' an attribute directory requires 729789Sahrens * read_xattr permission on the base file. 730789Sahrens * 731789Sahrens * Once in a directory the ability to read/write attributes 732789Sahrens * is controlled by the permissions on the attribute file. 733789Sahrens */ 734789Sahrens va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; 735789Sahrens va.va_type = VDIR; 7361231Smarks va.va_mode = S_IFDIR | S_ISVTX | 0777; 737789Sahrens va.va_uid = (uid_t)zp->z_phys->zp_uid; 738789Sahrens va.va_gid = (gid_t)zp->z_phys->zp_gid; 739789Sahrens 740789Sahrens error = zfs_make_xattrdir(zp, &va, xvpp, cr); 741789Sahrens zfs_dirent_unlock(dl); 742789Sahrens 743789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 7442113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 745789Sahrens goto top; 746789Sahrens } 747789Sahrens 748789Sahrens return (error); 749789Sahrens } 750789Sahrens 751789Sahrens /* 752789Sahrens * Decide whether it is okay to remove within a sticky directory. 753789Sahrens * 754789Sahrens * In sticky directories, write access is not sufficient; 755789Sahrens * you can remove entries from a directory only if: 756789Sahrens * 757789Sahrens * you own the directory, 758789Sahrens * you own the entry, 759789Sahrens * the entry is a plain file and you have write access, 760789Sahrens * or you are privileged (checked in secpolicy...). 761789Sahrens * 762789Sahrens * The function returns 0 if remove access is granted. 763789Sahrens */ 764789Sahrens int 765789Sahrens zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr) 766789Sahrens { 767789Sahrens uid_t uid; 768789Sahrens 769789Sahrens if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL) /* ZIL replay */ 770789Sahrens return (0); 771789Sahrens 772789Sahrens if ((zdp->z_phys->zp_mode & S_ISVTX) == 0 || 773789Sahrens (uid = crgetuid(cr)) == zdp->z_phys->zp_uid || 774789Sahrens uid == zp->z_phys->zp_uid || 775789Sahrens (ZTOV(zp)->v_type == VREG && 776789Sahrens zfs_zaccess(zp, ACE_WRITE_DATA, cr) == 0)) 777789Sahrens return (0); 778789Sahrens else 779789Sahrens return (secpolicy_vnode_remove(cr)); 780789Sahrens } 781