1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #include <sys/time.h> 29 #include <sys/systm.h> 30 #include <sys/sysmacros.h> 31 #include <sys/resource.h> 32 #include <sys/vfs.h> 33 #include <sys/vnode.h> 34 #include <sys/file.h> 35 #include <sys/mode.h> 36 #include <sys/kmem.h> 37 #include <sys/uio.h> 38 #include <sys/pathname.h> 39 #include <sys/cmn_err.h> 40 #include <sys/errno.h> 41 #include <sys/stat.h> 42 #include <sys/unistd.h> 43 #include <sys/sunddi.h> 44 #include <sys/random.h> 45 #include <sys/policy.h> 46 #include <sys/zfs_dir.h> 47 #include <sys/zfs_acl.h> 48 #include <sys/fs/zfs.h> 49 #include <sys/zap.h> 50 #include <sys/dmu.h> 51 #include <sys/atomic.h> 52 #include <sys/zfs_ctldir.h> 53 #include <sys/zfs_fuid.h> 54 #include <sys/dnlc.h> 55 #include <sys/extdirent.h> 56 57 /* 58 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups 59 * of names after deciding which is the appropriate lookup interface. 60 */ 61 static int 62 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact, 63 boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid) 64 { 65 int error; 66 67 if (zfsvfs->z_norm) { 68 matchtype_t mt = MT_FIRST; 69 boolean_t conflict = B_FALSE; 70 size_t bufsz = 0; 71 char *buf = NULL; 72 73 if (rpnp) { 74 buf = rpnp->pn_buf; 75 bufsz = rpnp->pn_bufsize; 76 } 77 if (exact) 78 mt = MT_EXACT; 79 /* 80 * In the non-mixed case we only expect there would ever 81 * be one match, but we need to use the normalizing lookup. 82 */ 83 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1, 84 zoid, mt, buf, bufsz, &conflict); 85 if (!error && deflags) 86 *deflags = conflict ? ED_CASE_CONFLICT : 0; 87 } else { 88 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid); 89 } 90 *zoid = ZFS_DIRENT_OBJ(*zoid); 91 92 if (error == ENOENT && update) 93 dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE); 94 95 return (error); 96 } 97 98 /* 99 * Lock a directory entry. A dirlock on <dzp, name> protects that name 100 * in dzp's directory zap object. As long as you hold a dirlock, you can 101 * assume two things: (1) dzp cannot be reaped, and (2) no other thread 102 * can change the zap entry for (i.e. link or unlink) this name. 103 * 104 * Input arguments: 105 * dzp - znode for directory 106 * name - name of entry to lock 107 * flag - ZNEW: if the entry already exists, fail with EEXIST. 108 * ZEXISTS: if the entry does not exist, fail with ENOENT. 109 * ZSHARED: allow concurrent access with other ZSHARED callers. 110 * ZXATTR: we want dzp's xattr directory 111 * ZCILOOK: On a mixed sensitivity file system, 112 * this lookup should be case-insensitive. 113 * ZCIEXACT: On a purely case-insensitive file system, 114 * this lookup should be case-sensitive. 115 * ZRENAMING: we are locking for renaming, force narrow locks 116 * ZHAVELOCK: Don't grab the z_name_lock for this call. The 117 * current thread already holds it. 118 * 119 * Output arguments: 120 * zpp - pointer to the znode for the entry (NULL if there isn't one) 121 * dlpp - pointer to the dirlock for this entry (NULL on error) 122 * direntflags - (case-insensitive lookup only) 123 * flags if multiple case-sensitive matches exist in directory 124 * realpnp - (case-insensitive lookup only) 125 * actual name matched within the directory 126 * 127 * Return value: 0 on success or errno on failure. 128 * 129 * NOTE: Always checks for, and rejects, '.' and '..'. 130 * NOTE: For case-insensitive file systems we take wide locks (see below), 131 * but return znode pointers to a single match. 132 */ 133 int 134 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp, 135 int flag, int *direntflags, pathname_t *realpnp) 136 { 137 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 138 zfs_dirlock_t *dl; 139 boolean_t update; 140 boolean_t exact; 141 uint64_t zoid; 142 vnode_t *vp = NULL; 143 int error = 0; 144 int cmpflags; 145 146 *zpp = NULL; 147 *dlpp = NULL; 148 149 /* 150 * Verify that we are not trying to lock '.', '..', or '.zfs' 151 */ 152 if (name[0] == '.' && 153 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) || 154 zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) 155 return (EEXIST); 156 157 /* 158 * Case sensitivity and normalization preferences are set when 159 * the file system is created. These are stored in the 160 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices 161 * affect what vnodes can be cached in the DNLC, how we 162 * perform zap lookups, and the "width" of our dirlocks. 163 * 164 * A normal dirlock locks a single name. Note that with 165 * normalization a name can be composed multiple ways, but 166 * when normalized, these names all compare equal. A wide 167 * dirlock locks multiple names. We need these when the file 168 * system is supporting mixed-mode access. It is sometimes 169 * necessary to lock all case permutations of file name at 170 * once so that simultaneous case-insensitive/case-sensitive 171 * behaves as rationally as possible. 172 */ 173 174 /* 175 * Decide if exact matches should be requested when performing 176 * a zap lookup on file systems supporting case-insensitive 177 * access. 178 */ 179 exact = 180 ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) || 181 ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK)); 182 183 /* 184 * Only look in or update the DNLC if we are looking for the 185 * name on a file system that does not require normalization 186 * or case folding. We can also look there if we happen to be 187 * on a non-normalizing, mixed sensitivity file system IF we 188 * are looking for the exact name. 189 * 190 * Maybe can add TO-UPPERed version of name to dnlc in ci-only 191 * case for performance improvement? 192 */ 193 update = !zfsvfs->z_norm || 194 ((zfsvfs->z_case == ZFS_CASE_MIXED) && 195 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK)); 196 197 /* 198 * ZRENAMING indicates we are in a situation where we should 199 * take narrow locks regardless of the file system's 200 * preferences for normalizing and case folding. This will 201 * prevent us deadlocking trying to grab the same wide lock 202 * twice if the two names happen to be case-insensitive 203 * matches. 204 */ 205 if (flag & ZRENAMING) 206 cmpflags = 0; 207 else 208 cmpflags = zfsvfs->z_norm; 209 210 /* 211 * Wait until there are no locks on this name. 212 * 213 * Don't grab the the lock if it is already held. However, cannot 214 * have both ZSHARED and ZHAVELOCK together. 215 */ 216 ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK)); 217 if (!(flag & ZHAVELOCK)) 218 rw_enter(&dzp->z_name_lock, RW_READER); 219 220 mutex_enter(&dzp->z_lock); 221 for (;;) { 222 if (dzp->z_unlinked) { 223 mutex_exit(&dzp->z_lock); 224 if (!(flag & ZHAVELOCK)) 225 rw_exit(&dzp->z_name_lock); 226 return (ENOENT); 227 } 228 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) { 229 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags, 230 U8_UNICODE_LATEST, &error) == 0) || error != 0) 231 break; 232 } 233 if (error != 0) { 234 mutex_exit(&dzp->z_lock); 235 if (!(flag & ZHAVELOCK)) 236 rw_exit(&dzp->z_name_lock); 237 return (ENOENT); 238 } 239 if (dl == NULL) { 240 /* 241 * Allocate a new dirlock and add it to the list. 242 */ 243 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP); 244 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL); 245 dl->dl_name = name; 246 dl->dl_sharecnt = 0; 247 dl->dl_namelock = 0; 248 dl->dl_namesize = 0; 249 dl->dl_dzp = dzp; 250 dl->dl_next = dzp->z_dirlocks; 251 dzp->z_dirlocks = dl; 252 break; 253 } 254 if ((flag & ZSHARED) && dl->dl_sharecnt != 0) 255 break; 256 cv_wait(&dl->dl_cv, &dzp->z_lock); 257 } 258 259 /* 260 * If the z_name_lock was NOT held for this dirlock record it. 261 */ 262 if (flag & ZHAVELOCK) 263 dl->dl_namelock = 1; 264 265 if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) { 266 /* 267 * We're the second shared reference to dl. Make a copy of 268 * dl_name in case the first thread goes away before we do. 269 * Note that we initialize the new name before storing its 270 * pointer into dl_name, because the first thread may load 271 * dl->dl_name at any time. He'll either see the old value, 272 * which is his, or the new shared copy; either is OK. 273 */ 274 dl->dl_namesize = strlen(dl->dl_name) + 1; 275 name = kmem_alloc(dl->dl_namesize, KM_SLEEP); 276 bcopy(dl->dl_name, name, dl->dl_namesize); 277 dl->dl_name = name; 278 } 279 280 mutex_exit(&dzp->z_lock); 281 282 /* 283 * We have a dirlock on the name. (Note that it is the dirlock, 284 * not the dzp's z_lock, that protects the name in the zap object.) 285 * See if there's an object by this name; if so, put a hold on it. 286 */ 287 if (flag & ZXATTR) { 288 zoid = dzp->z_phys->zp_xattr; 289 error = (zoid == 0 ? ENOENT : 0); 290 } else { 291 if (update) 292 vp = dnlc_lookup(ZTOV(dzp), name); 293 if (vp == DNLC_NO_VNODE) { 294 VN_RELE(vp); 295 error = ENOENT; 296 } else if (vp) { 297 if (flag & ZNEW) { 298 zfs_dirent_unlock(dl); 299 VN_RELE(vp); 300 return (EEXIST); 301 } 302 *dlpp = dl; 303 *zpp = VTOZ(vp); 304 return (0); 305 } else { 306 error = zfs_match_find(zfsvfs, dzp, name, exact, 307 update, direntflags, realpnp, &zoid); 308 } 309 } 310 if (error) { 311 if (error != ENOENT || (flag & ZEXISTS)) { 312 zfs_dirent_unlock(dl); 313 return (error); 314 } 315 } else { 316 if (flag & ZNEW) { 317 zfs_dirent_unlock(dl); 318 return (EEXIST); 319 } 320 error = zfs_zget(zfsvfs, zoid, zpp); 321 if (error) { 322 zfs_dirent_unlock(dl); 323 return (error); 324 } 325 if (!(flag & ZXATTR) && update) 326 dnlc_update(ZTOV(dzp), name, ZTOV(*zpp)); 327 } 328 329 *dlpp = dl; 330 331 return (0); 332 } 333 334 /* 335 * Unlock this directory entry and wake anyone who was waiting for it. 336 */ 337 void 338 zfs_dirent_unlock(zfs_dirlock_t *dl) 339 { 340 znode_t *dzp = dl->dl_dzp; 341 zfs_dirlock_t **prev_dl, *cur_dl; 342 343 mutex_enter(&dzp->z_lock); 344 345 if (!dl->dl_namelock) 346 rw_exit(&dzp->z_name_lock); 347 348 if (dl->dl_sharecnt > 1) { 349 dl->dl_sharecnt--; 350 mutex_exit(&dzp->z_lock); 351 return; 352 } 353 prev_dl = &dzp->z_dirlocks; 354 while ((cur_dl = *prev_dl) != dl) 355 prev_dl = &cur_dl->dl_next; 356 *prev_dl = dl->dl_next; 357 cv_broadcast(&dl->dl_cv); 358 mutex_exit(&dzp->z_lock); 359 360 if (dl->dl_namesize != 0) 361 kmem_free(dl->dl_name, dl->dl_namesize); 362 cv_destroy(&dl->dl_cv); 363 kmem_free(dl, sizeof (*dl)); 364 } 365 366 /* 367 * Look up an entry in a directory. 368 * 369 * NOTE: '.' and '..' are handled as special cases because 370 * no directory entries are actually stored for them. If this is 371 * the root of a filesystem, then '.zfs' is also treated as a 372 * special pseudo-directory. 373 */ 374 int 375 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags, 376 int *deflg, pathname_t *rpnp) 377 { 378 zfs_dirlock_t *dl; 379 znode_t *zp; 380 int error = 0; 381 382 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { 383 *vpp = ZTOV(dzp); 384 VN_HOLD(*vpp); 385 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { 386 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 387 /* 388 * If we are a snapshot mounted under .zfs, return 389 * the vp for the snapshot directory. 390 */ 391 if (dzp->z_phys->zp_parent == dzp->z_id && 392 zfsvfs->z_parent != zfsvfs) { 393 error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir, 394 "snapshot", vpp, NULL, 0, NULL, kcred, 395 NULL, NULL, NULL); 396 return (error); 397 } 398 rw_enter(&dzp->z_parent_lock, RW_READER); 399 error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp); 400 if (error == 0) 401 *vpp = ZTOV(zp); 402 rw_exit(&dzp->z_parent_lock); 403 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) { 404 *vpp = zfsctl_root(dzp); 405 } else { 406 int zf; 407 408 zf = ZEXISTS | ZSHARED; 409 if (flags & FIGNORECASE) 410 zf |= ZCILOOK; 411 412 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp); 413 if (error == 0) { 414 *vpp = ZTOV(zp); 415 zfs_dirent_unlock(dl); 416 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ 417 } 418 rpnp = NULL; 419 } 420 421 if ((flags & FIGNORECASE) && rpnp && !error) 422 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize); 423 424 return (error); 425 } 426 427 /* 428 * unlinked Set (formerly known as the "delete queue") Error Handling 429 * 430 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we 431 * don't specify the name of the entry that we will be manipulating. We 432 * also fib and say that we won't be adding any new entries to the 433 * unlinked set, even though we might (this is to lower the minimum file 434 * size that can be deleted in a full filesystem). So on the small 435 * chance that the nlink list is using a fat zap (ie. has more than 436 * 2000 entries), we *may* not pre-read a block that's needed. 437 * Therefore it is remotely possible for some of the assertions 438 * regarding the unlinked set below to fail due to i/o error. On a 439 * nondebug system, this will result in the space being leaked. 440 */ 441 void 442 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx) 443 { 444 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 445 446 ASSERT(zp->z_unlinked); 447 ASSERT3U(zp->z_phys->zp_links, ==, 0); 448 449 VERIFY3U(0, ==, 450 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); 451 } 452 453 /* 454 * Clean up any znodes that had no links when we either crashed or 455 * (force) umounted the file system. 456 */ 457 void 458 zfs_unlinked_drain(zfsvfs_t *zfsvfs) 459 { 460 zap_cursor_t zc; 461 zap_attribute_t zap; 462 dmu_object_info_t doi; 463 znode_t *zp; 464 int error; 465 466 /* 467 * Interate over the contents of the unlinked set. 468 */ 469 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj); 470 zap_cursor_retrieve(&zc, &zap) == 0; 471 zap_cursor_advance(&zc)) { 472 473 /* 474 * See what kind of object we have in list 475 */ 476 477 error = dmu_object_info(zfsvfs->z_os, 478 zap.za_first_integer, &doi); 479 if (error != 0) 480 continue; 481 482 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) || 483 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS)); 484 /* 485 * We need to re-mark these list entries for deletion, 486 * so we pull them back into core and set zp->z_unlinked. 487 */ 488 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp); 489 490 /* 491 * We may pick up znodes that are already marked for deletion. 492 * This could happen during the purge of an extended attribute 493 * directory. All we need to do is skip over them, since they 494 * are already in the system marked z_unlinked. 495 */ 496 if (error != 0) 497 continue; 498 499 zp->z_unlinked = B_TRUE; 500 VN_RELE(ZTOV(zp)); 501 } 502 zap_cursor_fini(&zc); 503 } 504 505 /* 506 * Delete the entire contents of a directory. Return a count 507 * of the number of entries that could not be deleted. If we encounter 508 * an error, return a count of at least one so that the directory stays 509 * in the unlinked set. 510 * 511 * NOTE: this function assumes that the directory is inactive, 512 * so there is no need to lock its entries before deletion. 513 * Also, it assumes the directory contents is *only* regular 514 * files. 515 */ 516 static int 517 zfs_purgedir(znode_t *dzp) 518 { 519 zap_cursor_t zc; 520 zap_attribute_t zap; 521 znode_t *xzp; 522 dmu_tx_t *tx; 523 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 524 zfs_dirlock_t dl; 525 int skipped = 0; 526 int error; 527 528 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 529 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 530 zap_cursor_advance(&zc)) { 531 error = zfs_zget(zfsvfs, 532 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp); 533 if (error) { 534 skipped += 1; 535 continue; 536 } 537 538 ASSERT((ZTOV(xzp)->v_type == VREG) || 539 (ZTOV(xzp)->v_type == VLNK)); 540 541 tx = dmu_tx_create(zfsvfs->z_os); 542 dmu_tx_hold_bonus(tx, dzp->z_id); 543 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); 544 dmu_tx_hold_bonus(tx, xzp->z_id); 545 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 546 error = dmu_tx_assign(tx, TXG_WAIT); 547 if (error) { 548 dmu_tx_abort(tx); 549 VN_RELE(ZTOV(xzp)); 550 skipped += 1; 551 continue; 552 } 553 bzero(&dl, sizeof (dl)); 554 dl.dl_dzp = dzp; 555 dl.dl_name = zap.za_name; 556 557 error = zfs_link_destroy(&dl, xzp, tx, 0, NULL); 558 if (error) 559 skipped += 1; 560 dmu_tx_commit(tx); 561 562 VN_RELE(ZTOV(xzp)); 563 } 564 zap_cursor_fini(&zc); 565 if (error != ENOENT) 566 skipped += 1; 567 return (skipped); 568 } 569 570 void 571 zfs_rmnode(znode_t *zp) 572 { 573 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 574 objset_t *os = zfsvfs->z_os; 575 znode_t *xzp = NULL; 576 dmu_tx_t *tx; 577 uint64_t acl_obj; 578 int error; 579 580 ASSERT(ZTOV(zp)->v_count == 0); 581 ASSERT(zp->z_phys->zp_links == 0); 582 583 /* 584 * If this is an attribute directory, purge its contents. 585 */ 586 if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) { 587 if (zfs_purgedir(zp) != 0) { 588 /* 589 * Not enough space to delete some xattrs. 590 * Leave it in the unlinked set. 591 */ 592 zfs_znode_dmu_fini(zp); 593 zfs_znode_free(zp); 594 return; 595 } 596 } 597 598 /* 599 * Free up all the data in the file. 600 */ 601 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END); 602 if (error) { 603 /* 604 * Not enough space. Leave the file in the unlinked set. 605 */ 606 zfs_znode_dmu_fini(zp); 607 zfs_znode_free(zp); 608 return; 609 } 610 611 /* 612 * If the file has extended attributes, we're going to unlink 613 * the xattr dir. 614 */ 615 if (zp->z_phys->zp_xattr) { 616 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 617 ASSERT(error == 0); 618 } 619 620 acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj; 621 622 /* 623 * Set up the final transaction. 624 */ 625 tx = dmu_tx_create(os); 626 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 627 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 628 if (xzp) { 629 dmu_tx_hold_bonus(tx, xzp->z_id); 630 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL); 631 } 632 if (acl_obj) 633 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 634 error = dmu_tx_assign(tx, TXG_WAIT); 635 if (error) { 636 /* 637 * Not enough space to delete the file. Leave it in the 638 * unlinked set, leaking it until the fs is remounted (at 639 * which point we'll call zfs_unlinked_drain() to process it). 640 */ 641 dmu_tx_abort(tx); 642 zfs_znode_dmu_fini(zp); 643 zfs_znode_free(zp); 644 goto out; 645 } 646 647 if (xzp) { 648 dmu_buf_will_dirty(xzp->z_dbuf, tx); 649 mutex_enter(&xzp->z_lock); 650 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */ 651 xzp->z_phys->zp_links = 0; /* no more links to it */ 652 mutex_exit(&xzp->z_lock); 653 zfs_unlinked_add(xzp, tx); 654 } 655 656 /* Remove this znode from the unlinked set */ 657 VERIFY3U(0, ==, 658 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); 659 660 zfs_znode_delete(zp, tx); 661 662 dmu_tx_commit(tx); 663 out: 664 if (xzp) 665 VN_RELE(ZTOV(xzp)); 666 } 667 668 static uint64_t 669 zfs_dirent(znode_t *zp) 670 { 671 uint64_t de = zp->z_id; 672 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE) 673 de |= IFTODT((zp)->z_phys->zp_mode) << 60; 674 return (de); 675 } 676 677 /* 678 * Link zp into dl. Can only fail if zp has been unlinked. 679 */ 680 int 681 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag) 682 { 683 znode_t *dzp = dl->dl_dzp; 684 vnode_t *vp = ZTOV(zp); 685 uint64_t value; 686 int zp_is_dir = (vp->v_type == VDIR); 687 int error; 688 689 dmu_buf_will_dirty(zp->z_dbuf, tx); 690 mutex_enter(&zp->z_lock); 691 692 if (!(flag & ZRENAMING)) { 693 if (zp->z_unlinked) { /* no new links to unlinked zp */ 694 ASSERT(!(flag & (ZNEW | ZEXISTS))); 695 mutex_exit(&zp->z_lock); 696 return (ENOENT); 697 } 698 zp->z_phys->zp_links++; 699 } 700 zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */ 701 702 if (!(flag & ZNEW)) 703 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 704 mutex_exit(&zp->z_lock); 705 706 dmu_buf_will_dirty(dzp->z_dbuf, tx); 707 mutex_enter(&dzp->z_lock); 708 dzp->z_phys->zp_size++; /* one dirent added */ 709 dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */ 710 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 711 mutex_exit(&dzp->z_lock); 712 713 value = zfs_dirent(zp); 714 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name, 715 8, 1, &value, tx); 716 ASSERT(error == 0); 717 718 dnlc_update(ZTOV(dzp), dl->dl_name, vp); 719 720 return (0); 721 } 722 723 /* 724 * Unlink zp from dl, and mark zp for deletion if this was the last link. 725 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). 726 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. 727 * If it's non-NULL, we use it to indicate whether the znode needs deletion, 728 * and it's the caller's job to do it. 729 */ 730 int 731 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag, 732 boolean_t *unlinkedp) 733 { 734 znode_t *dzp = dl->dl_dzp; 735 vnode_t *vp = ZTOV(zp); 736 int zp_is_dir = (vp->v_type == VDIR); 737 boolean_t unlinked = B_FALSE; 738 int error; 739 740 dnlc_remove(ZTOV(dzp), dl->dl_name); 741 742 if (!(flag & ZRENAMING)) { 743 dmu_buf_will_dirty(zp->z_dbuf, tx); 744 745 if (vn_vfswlock(vp)) /* prevent new mounts on zp */ 746 return (EBUSY); 747 748 if (vn_ismntpt(vp)) { /* don't remove mount point */ 749 vn_vfsunlock(vp); 750 return (EBUSY); 751 } 752 753 mutex_enter(&zp->z_lock); 754 if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */ 755 mutex_exit(&zp->z_lock); 756 vn_vfsunlock(vp); 757 return (EEXIST); 758 } 759 if (zp->z_phys->zp_links <= zp_is_dir) { 760 zfs_panic_recover("zfs: link count on vnode %p is %u, " 761 "should be at least %u", 762 zp->z_vnode, (int)zp->z_phys->zp_links, 763 zp_is_dir + 1); 764 zp->z_phys->zp_links = zp_is_dir + 1; 765 } 766 if (--zp->z_phys->zp_links == zp_is_dir) { 767 zp->z_unlinked = B_TRUE; 768 zp->z_phys->zp_links = 0; 769 unlinked = B_TRUE; 770 } else { 771 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 772 } 773 mutex_exit(&zp->z_lock); 774 vn_vfsunlock(vp); 775 } 776 777 dmu_buf_will_dirty(dzp->z_dbuf, tx); 778 mutex_enter(&dzp->z_lock); 779 dzp->z_phys->zp_size--; /* one dirent removed */ 780 dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */ 781 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx); 782 mutex_exit(&dzp->z_lock); 783 784 if (zp->z_zfsvfs->z_norm) { 785 if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && 786 (flag & ZCIEXACT)) || 787 ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) && 788 !(flag & ZCILOOK))) 789 error = zap_remove_norm(zp->z_zfsvfs->z_os, 790 dzp->z_id, dl->dl_name, MT_EXACT, tx); 791 else 792 error = zap_remove_norm(zp->z_zfsvfs->z_os, 793 dzp->z_id, dl->dl_name, MT_FIRST, tx); 794 } else { 795 error = zap_remove(zp->z_zfsvfs->z_os, 796 dzp->z_id, dl->dl_name, tx); 797 } 798 ASSERT(error == 0); 799 800 if (unlinkedp != NULL) 801 *unlinkedp = unlinked; 802 else if (unlinked) 803 zfs_unlinked_add(zp, tx); 804 805 return (0); 806 } 807 808 /* 809 * Indicate whether the directory is empty. Works with or without z_lock 810 * held, but can only be consider a hint in the latter case. Returns true 811 * if only "." and ".." remain and there's no work in progress. 812 */ 813 boolean_t 814 zfs_dirempty(znode_t *dzp) 815 { 816 return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0); 817 } 818 819 int 820 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr) 821 { 822 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 823 znode_t *xzp; 824 dmu_tx_t *tx; 825 int error; 826 zfs_acl_ids_t acl_ids; 827 boolean_t fuid_dirtied; 828 829 *xvpp = NULL; 830 831 if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)) 832 return (error); 833 834 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL, 835 &acl_ids)) != 0) 836 return (error); 837 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 838 zfs_acl_ids_free(&acl_ids); 839 return (EDQUOT); 840 } 841 842 tx = dmu_tx_create(zfsvfs->z_os); 843 dmu_tx_hold_bonus(tx, zp->z_id); 844 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 845 fuid_dirtied = zfsvfs->z_fuid_dirty; 846 if (fuid_dirtied) 847 zfs_fuid_txhold(zfsvfs, tx); 848 error = dmu_tx_assign(tx, TXG_NOWAIT); 849 if (error) { 850 zfs_acl_ids_free(&acl_ids); 851 if (error == ERESTART) 852 dmu_tx_wait(tx); 853 dmu_tx_abort(tx); 854 return (error); 855 } 856 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, &acl_ids); 857 858 if (fuid_dirtied) 859 zfs_fuid_sync(zfsvfs, tx); 860 861 ASSERT(xzp->z_phys->zp_parent == zp->z_id); 862 dmu_buf_will_dirty(zp->z_dbuf, tx); 863 zp->z_phys->zp_xattr = xzp->z_id; 864 865 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, 866 xzp, "", NULL, acl_ids.z_fuidp, vap); 867 868 zfs_acl_ids_free(&acl_ids); 869 dmu_tx_commit(tx); 870 871 *xvpp = ZTOV(xzp); 872 873 return (0); 874 } 875 876 /* 877 * Return a znode for the extended attribute directory for zp. 878 * ** If the directory does not already exist, it is created ** 879 * 880 * IN: zp - znode to obtain attribute directory from 881 * cr - credentials of caller 882 * flags - flags from the VOP_LOOKUP call 883 * 884 * OUT: xzpp - pointer to extended attribute znode 885 * 886 * RETURN: 0 on success 887 * error number on failure 888 */ 889 int 890 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags) 891 { 892 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 893 znode_t *xzp; 894 zfs_dirlock_t *dl; 895 vattr_t va; 896 int error; 897 top: 898 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL); 899 if (error) 900 return (error); 901 902 if (xzp != NULL) { 903 *xvpp = ZTOV(xzp); 904 zfs_dirent_unlock(dl); 905 return (0); 906 } 907 908 ASSERT(zp->z_phys->zp_xattr == 0); 909 910 if (!(flags & CREATE_XATTR_DIR)) { 911 zfs_dirent_unlock(dl); 912 return (ENOENT); 913 } 914 915 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 916 zfs_dirent_unlock(dl); 917 return (EROFS); 918 } 919 920 /* 921 * The ability to 'create' files in an attribute 922 * directory comes from the write_xattr permission on the base file. 923 * 924 * The ability to 'search' an attribute directory requires 925 * read_xattr permission on the base file. 926 * 927 * Once in a directory the ability to read/write attributes 928 * is controlled by the permissions on the attribute file. 929 */ 930 va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID; 931 va.va_type = VDIR; 932 va.va_mode = S_IFDIR | S_ISVTX | 0777; 933 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid); 934 935 error = zfs_make_xattrdir(zp, &va, xvpp, cr); 936 zfs_dirent_unlock(dl); 937 938 if (error == ERESTART) { 939 /* NB: we already did dmu_tx_wait() if necessary */ 940 goto top; 941 } 942 943 return (error); 944 } 945 946 /* 947 * Decide whether it is okay to remove within a sticky directory. 948 * 949 * In sticky directories, write access is not sufficient; 950 * you can remove entries from a directory only if: 951 * 952 * you own the directory, 953 * you own the entry, 954 * the entry is a plain file and you have write access, 955 * or you are privileged (checked in secpolicy...). 956 * 957 * The function returns 0 if remove access is granted. 958 */ 959 int 960 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr) 961 { 962 uid_t uid; 963 uid_t downer; 964 uid_t fowner; 965 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 966 967 if (zdp->z_zfsvfs->z_replay) 968 return (0); 969 970 if ((zdp->z_phys->zp_mode & S_ISVTX) == 0) 971 return (0); 972 973 downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER); 974 fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER); 975 976 if ((uid = crgetuid(cr)) == downer || uid == fowner || 977 (ZTOV(zp)->v_type == VREG && 978 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)) 979 return (0); 980 else 981 return (secpolicy_vnode_remove(cr)); 982 } 983