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 https://opensource.org/licenses/CDDL-1.0. 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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2013, 2016 by Delphix. All rights reserved. 25 * Copyright 2017 Nexenta Systems, Inc. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/systm.h> 32 #include <sys/sysmacros.h> 33 #include <sys/resource.h> 34 #include <sys/vfs.h> 35 #include <sys/vnode.h> 36 #include <sys/file.h> 37 #include <sys/kmem.h> 38 #include <sys/uio.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/condvar.h> 47 #include <sys/callb.h> 48 #include <sys/smp.h> 49 #include <sys/zfs_dir.h> 50 #include <sys/zfs_acl.h> 51 #include <sys/fs/zfs.h> 52 #include <sys/zap.h> 53 #include <sys/dmu.h> 54 #include <sys/atomic.h> 55 #include <sys/zfs_ctldir.h> 56 #include <sys/zfs_fuid.h> 57 #include <sys/sa.h> 58 #include <sys/zfs_sa.h> 59 #include <sys/dmu_objset.h> 60 #include <sys/dsl_dir.h> 61 62 #include <sys/ccompat.h> 63 64 /* 65 * zfs_match_find() is used by zfs_dirent_lookup() to perform zap lookups 66 * of names after deciding which is the appropriate lookup interface. 67 */ 68 static int 69 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name, 70 matchtype_t mt, uint64_t *zoid) 71 { 72 int error; 73 74 if (zfsvfs->z_norm) { 75 76 /* 77 * In the non-mixed case we only expect there would ever 78 * be one match, but we need to use the normalizing lookup. 79 */ 80 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1, 81 zoid, mt, NULL, 0, NULL); 82 } else { 83 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid); 84 } 85 *zoid = ZFS_DIRENT_OBJ(*zoid); 86 87 return (error); 88 } 89 90 /* 91 * Look up a directory entry under a locked vnode. 92 * dvp being locked gives us a guarantee that there are no concurrent 93 * modification of the directory and, thus, if a node can be found in 94 * the directory, then it must not be unlinked. 95 * 96 * Input arguments: 97 * dzp - znode for directory 98 * name - name of entry to lock 99 * flag - ZNEW: if the entry already exists, fail with EEXIST. 100 * ZEXISTS: if the entry does not exist, fail with ENOENT. 101 * ZXATTR: we want dzp's xattr directory 102 * 103 * Output arguments: 104 * zpp - pointer to the znode for the entry (NULL if there isn't one) 105 * 106 * Return value: 0 on success or errno on failure. 107 * 108 * NOTE: Always checks for, and rejects, '.' and '..'. 109 */ 110 int 111 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag) 112 { 113 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 114 znode_t *zp; 115 matchtype_t mt = 0; 116 uint64_t zoid; 117 int error = 0; 118 119 if (zfsvfs->z_replay == B_FALSE) 120 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__); 121 122 *zpp = NULL; 123 124 /* 125 * Verify that we are not trying to lock '.', '..', or '.zfs' 126 */ 127 if (name[0] == '.' && 128 (((name[1] == '\0') || (name[1] == '.' && name[2] == '\0')) || 129 (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0))) 130 return (SET_ERROR(EEXIST)); 131 132 /* 133 * Case sensitivity and normalization preferences are set when 134 * the file system is created. These are stored in the 135 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices 136 * affect how we perform zap lookups. 137 * 138 * When matching we may need to normalize & change case according to 139 * FS settings. 140 * 141 * Note that a normalized match is necessary for a case insensitive 142 * filesystem when the lookup request is not exact because normalization 143 * can fold case independent of normalizing code point sequences. 144 * 145 * See the table above zfs_dropname(). 146 */ 147 if (zfsvfs->z_norm != 0) { 148 mt = MT_NORMALIZE; 149 150 /* 151 * Determine if the match needs to honor the case specified in 152 * lookup, and if so keep track of that so that during 153 * normalization we don't fold case. 154 */ 155 if (zfsvfs->z_case == ZFS_CASE_MIXED) { 156 mt |= MT_MATCH_CASE; 157 } 158 } 159 160 /* 161 * Only look in or update the DNLC if we are looking for the 162 * name on a file system that does not require normalization 163 * or case folding. We can also look there if we happen to be 164 * on a non-normalizing, mixed sensitivity file system IF we 165 * are looking for the exact name. 166 * 167 * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE 168 * because in that case MT_EXACT and MT_FIRST should produce exactly 169 * the same result. 170 */ 171 172 if (dzp->z_unlinked && !(flag & ZXATTR)) 173 return (ENOENT); 174 if (flag & ZXATTR) { 175 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid, 176 sizeof (zoid)); 177 if (error == 0) 178 error = (zoid == 0 ? ENOENT : 0); 179 } else { 180 error = zfs_match_find(zfsvfs, dzp, name, mt, &zoid); 181 } 182 if (error) { 183 if (error != ENOENT || (flag & ZEXISTS)) { 184 return (error); 185 } 186 } else { 187 if (flag & ZNEW) { 188 return (SET_ERROR(EEXIST)); 189 } 190 error = zfs_zget(zfsvfs, zoid, &zp); 191 if (error) 192 return (error); 193 ASSERT(!zp->z_unlinked); 194 *zpp = zp; 195 } 196 197 return (0); 198 } 199 200 static int 201 zfs_dd_lookup(znode_t *dzp, znode_t **zpp) 202 { 203 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 204 znode_t *zp; 205 uint64_t parent; 206 int error; 207 208 #ifdef ZFS_DEBUG 209 if (zfsvfs->z_replay == B_FALSE) 210 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__); 211 #endif 212 if (dzp->z_unlinked) 213 return (ENOENT); 214 215 if ((error = sa_lookup(dzp->z_sa_hdl, 216 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0) 217 return (error); 218 219 error = zfs_zget(zfsvfs, parent, &zp); 220 if (error == 0) 221 *zpp = zp; 222 return (error); 223 } 224 225 int 226 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp) 227 { 228 zfsvfs_t *zfsvfs __unused = dzp->z_zfsvfs; 229 znode_t *zp = NULL; 230 int error = 0; 231 232 #ifdef ZFS_DEBUG 233 if (zfsvfs->z_replay == B_FALSE) 234 ASSERT_VOP_LOCKED(ZTOV(dzp), __func__); 235 #endif 236 if (dzp->z_unlinked) 237 return (SET_ERROR(ENOENT)); 238 239 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) { 240 *zpp = dzp; 241 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) { 242 error = zfs_dd_lookup(dzp, &zp); 243 if (error == 0) 244 *zpp = zp; 245 } else { 246 error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS); 247 if (error == 0) { 248 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */ 249 *zpp = zp; 250 } 251 } 252 return (error); 253 } 254 255 /* 256 * unlinked Set (formerly known as the "delete queue") Error Handling 257 * 258 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we 259 * don't specify the name of the entry that we will be manipulating. We 260 * also fib and say that we won't be adding any new entries to the 261 * unlinked set, even though we might (this is to lower the minimum file 262 * size that can be deleted in a full filesystem). So on the small 263 * chance that the nlink list is using a fat zap (ie. has more than 264 * 2000 entries), we *may* not pre-read a block that's needed. 265 * Therefore it is remotely possible for some of the assertions 266 * regarding the unlinked set below to fail due to i/o error. On a 267 * nondebug system, this will result in the space being leaked. 268 */ 269 void 270 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx) 271 { 272 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 273 274 ASSERT(zp->z_unlinked); 275 ASSERT3U(zp->z_links, ==, 0); 276 277 VERIFY0(zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); 278 279 dataset_kstats_update_nunlinks_kstat(&zfsvfs->z_kstat, 1); 280 } 281 282 /* 283 * Clean up any znodes that had no links when we either crashed or 284 * (force) umounted the file system. 285 */ 286 void 287 zfs_unlinked_drain(zfsvfs_t *zfsvfs) 288 { 289 zap_cursor_t zc; 290 zap_attribute_t zap; 291 dmu_object_info_t doi; 292 znode_t *zp; 293 dmu_tx_t *tx; 294 int error; 295 296 /* 297 * Iterate over the contents of the unlinked set. 298 */ 299 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj); 300 zap_cursor_retrieve(&zc, &zap) == 0; 301 zap_cursor_advance(&zc)) { 302 303 /* 304 * See what kind of object we have in list 305 */ 306 307 error = dmu_object_info(zfsvfs->z_os, 308 zap.za_first_integer, &doi); 309 if (error != 0) 310 continue; 311 312 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) || 313 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS)); 314 /* 315 * We need to re-mark these list entries for deletion, 316 * so we pull them back into core and set zp->z_unlinked. 317 */ 318 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp); 319 320 /* 321 * We may pick up znodes that are already marked for deletion. 322 * This could happen during the purge of an extended attribute 323 * directory. All we need to do is skip over them, since they 324 * are already in the system marked z_unlinked. 325 */ 326 if (error != 0) 327 continue; 328 329 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY); 330 331 /* 332 * Due to changes in zfs_rmnode we need to make sure the 333 * link count is set to zero here. 334 */ 335 if (zp->z_links != 0) { 336 tx = dmu_tx_create(zfsvfs->z_os); 337 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 338 error = dmu_tx_assign(tx, TXG_WAIT); 339 if (error != 0) { 340 dmu_tx_abort(tx); 341 vput(ZTOV(zp)); 342 continue; 343 } 344 zp->z_links = 0; 345 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 346 &zp->z_links, sizeof (zp->z_links), tx)); 347 dmu_tx_commit(tx); 348 } 349 350 zp->z_unlinked = B_TRUE; 351 vput(ZTOV(zp)); 352 } 353 zap_cursor_fini(&zc); 354 } 355 356 /* 357 * Delete the entire contents of a directory. Return a count 358 * of the number of entries that could not be deleted. If we encounter 359 * an error, return a count of at least one so that the directory stays 360 * in the unlinked set. 361 * 362 * NOTE: this function assumes that the directory is inactive, 363 * so there is no need to lock its entries before deletion. 364 * Also, it assumes the directory contents is *only* regular 365 * files. 366 */ 367 static int 368 zfs_purgedir(znode_t *dzp) 369 { 370 zap_cursor_t zc; 371 zap_attribute_t zap; 372 znode_t *xzp; 373 dmu_tx_t *tx; 374 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 375 int skipped = 0; 376 int error; 377 378 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); 379 (error = zap_cursor_retrieve(&zc, &zap)) == 0; 380 zap_cursor_advance(&zc)) { 381 error = zfs_zget(zfsvfs, 382 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp); 383 if (error) { 384 skipped += 1; 385 continue; 386 } 387 388 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY); 389 ASSERT((ZTOV(xzp)->v_type == VREG) || 390 (ZTOV(xzp)->v_type == VLNK)); 391 392 tx = dmu_tx_create(zfsvfs->z_os); 393 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 394 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name); 395 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 396 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 397 /* Is this really needed ? */ 398 zfs_sa_upgrade_txholds(tx, xzp); 399 dmu_tx_mark_netfree(tx); 400 error = dmu_tx_assign(tx, TXG_WAIT); 401 if (error) { 402 dmu_tx_abort(tx); 403 vput(ZTOV(xzp)); 404 skipped += 1; 405 continue; 406 } 407 408 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL); 409 if (error) 410 skipped += 1; 411 dmu_tx_commit(tx); 412 413 vput(ZTOV(xzp)); 414 } 415 zap_cursor_fini(&zc); 416 if (error != ENOENT) 417 skipped += 1; 418 return (skipped); 419 } 420 421 extern taskq_t *zfsvfs_taskq; 422 423 void 424 zfs_rmnode(znode_t *zp) 425 { 426 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 427 objset_t *os = zfsvfs->z_os; 428 dmu_tx_t *tx; 429 uint64_t acl_obj; 430 uint64_t xattr_obj; 431 uint64_t count; 432 int error; 433 434 ASSERT3U(zp->z_links, ==, 0); 435 if (zfsvfs->z_replay == B_FALSE) 436 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__); 437 438 /* 439 * If this is an attribute directory, purge its contents. 440 */ 441 if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR && 442 (zp->z_pflags & ZFS_XATTR)) { 443 if (zfs_purgedir(zp) != 0) { 444 /* 445 * Not enough space to delete some xattrs. 446 * Leave it in the unlinked set. 447 */ 448 zfs_znode_dmu_fini(zp); 449 zfs_znode_free(zp); 450 return; 451 } 452 } else { 453 /* 454 * Free up all the data in the file. We don't do this for 455 * XATTR directories because we need truncate and remove to be 456 * in the same tx, like in zfs_znode_delete(). Otherwise, if 457 * we crash here we'll end up with an inconsistent truncated 458 * zap object in the delete queue. Note a truncated file is 459 * harmless since it only contains user data. 460 */ 461 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END); 462 if (error) { 463 /* 464 * Not enough space or we were interrupted by unmount. 465 * Leave the file in the unlinked set. 466 */ 467 zfs_znode_dmu_fini(zp); 468 zfs_znode_free(zp); 469 return; 470 } 471 } 472 473 /* 474 * If the file has extended attributes, we're going to unlink 475 * the xattr dir. 476 */ 477 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 478 &xattr_obj, sizeof (xattr_obj)); 479 if (error) 480 xattr_obj = 0; 481 482 acl_obj = zfs_external_acl(zp); 483 484 /* 485 * Set up the final transaction. 486 */ 487 tx = dmu_tx_create(os); 488 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 489 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 490 if (xattr_obj) 491 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL); 492 if (acl_obj) 493 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 494 495 zfs_sa_upgrade_txholds(tx, zp); 496 error = dmu_tx_assign(tx, TXG_WAIT); 497 if (error) { 498 /* 499 * Not enough space to delete the file. Leave it in the 500 * unlinked set, leaking it until the fs is remounted (at 501 * which point we'll call zfs_unlinked_drain() to process it). 502 */ 503 dmu_tx_abort(tx); 504 zfs_znode_dmu_fini(zp); 505 zfs_znode_free(zp); 506 return; 507 } 508 509 /* 510 * FreeBSD's implementation of zfs_zget requires a vnode to back it. 511 * This means that we could end up calling into getnewvnode while 512 * calling zfs_rmnode as a result of a prior call to getnewvnode 513 * trying to clear vnodes out of the cache. If this repeats we can 514 * recurse enough that we overflow our stack. To avoid this, we 515 * avoid calling zfs_zget on the xattr znode and instead simply add 516 * it to the unlinked set and schedule a call to zfs_unlinked_drain. 517 */ 518 if (xattr_obj) { 519 /* Add extended attribute directory to the unlinked set. */ 520 VERIFY3U(0, ==, 521 zap_add_int(os, zfsvfs->z_unlinkedobj, xattr_obj, tx)); 522 } 523 524 mutex_enter(&os->os_dsl_dataset->ds_dir->dd_activity_lock); 525 526 /* Remove this znode from the unlinked set */ 527 VERIFY3U(0, ==, 528 zap_remove_int(os, zfsvfs->z_unlinkedobj, zp->z_id, tx)); 529 530 if (zap_count(os, zfsvfs->z_unlinkedobj, &count) == 0 && count == 0) { 531 cv_broadcast(&os->os_dsl_dataset->ds_dir->dd_activity_cv); 532 } 533 534 mutex_exit(&os->os_dsl_dataset->ds_dir->dd_activity_lock); 535 536 dataset_kstats_update_nunlinked_kstat(&zfsvfs->z_kstat, 1); 537 538 zfs_znode_delete(zp, tx); 539 540 dmu_tx_commit(tx); 541 542 if (xattr_obj) { 543 /* 544 * We're using the FreeBSD taskqueue API here instead of 545 * the Solaris taskq API since the FreeBSD API allows for a 546 * task to be enqueued multiple times but executed once. 547 */ 548 taskqueue_enqueue(zfsvfs_taskq->tq_queue, 549 &zfsvfs->z_unlinked_drain_task); 550 } 551 } 552 553 static uint64_t 554 zfs_dirent(znode_t *zp, uint64_t mode) 555 { 556 uint64_t de = zp->z_id; 557 558 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE) 559 de |= IFTODT(mode) << 60; 560 return (de); 561 } 562 563 /* 564 * Link zp into dzp. Can only fail if zp has been unlinked. 565 */ 566 int 567 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, 568 int flag) 569 { 570 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 571 vnode_t *vp = ZTOV(zp); 572 uint64_t value; 573 int zp_is_dir = (vp->v_type == VDIR); 574 sa_bulk_attr_t bulk[5]; 575 uint64_t mtime[2], ctime[2]; 576 int count = 0; 577 int error; 578 579 if (zfsvfs->z_replay == B_FALSE) { 580 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__); 581 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__); 582 } 583 if (zp_is_dir) { 584 if (dzp->z_links >= ZFS_LINK_MAX) 585 return (SET_ERROR(EMLINK)); 586 } 587 if (!(flag & ZRENAMING)) { 588 if (zp->z_unlinked) { /* no new links to unlinked zp */ 589 ASSERT(!(flag & (ZNEW | ZEXISTS))); 590 return (SET_ERROR(ENOENT)); 591 } 592 if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) { 593 return (SET_ERROR(EMLINK)); 594 } 595 zp->z_links++; 596 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 597 &zp->z_links, sizeof (zp->z_links)); 598 599 } else { 600 ASSERT(!zp->z_unlinked); 601 } 602 value = zfs_dirent(zp, zp->z_mode); 603 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name, 604 8, 1, &value, tx); 605 606 /* 607 * zap_add could fail to add the entry if it exceeds the capacity of the 608 * leaf-block and zap_leaf_split() failed to help. 609 * The caller of this routine is responsible for failing the transaction 610 * which will rollback the SA updates done above. 611 */ 612 if (error != 0) { 613 if (!(flag & ZRENAMING) && !(flag & ZNEW)) 614 zp->z_links--; 615 return (error); 616 } 617 618 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, 619 &dzp->z_id, sizeof (dzp->z_id)); 620 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 621 &zp->z_pflags, sizeof (zp->z_pflags)); 622 623 if (!(flag & ZNEW)) { 624 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 625 ctime, sizeof (ctime)); 626 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, 627 ctime); 628 } 629 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 630 ASSERT0(error); 631 632 dzp->z_size++; 633 dzp->z_links += zp_is_dir; 634 count = 0; 635 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 636 &dzp->z_size, sizeof (dzp->z_size)); 637 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 638 &dzp->z_links, sizeof (dzp->z_links)); 639 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 640 mtime, sizeof (mtime)); 641 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 642 ctime, sizeof (ctime)); 643 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 644 &dzp->z_pflags, sizeof (dzp->z_pflags)); 645 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime); 646 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); 647 ASSERT0(error); 648 return (0); 649 } 650 651 /* 652 * The match type in the code for this function should conform to: 653 * 654 * ------------------------------------------------------------------------ 655 * fs type | z_norm | lookup type | match type 656 * ---------|-------------|-------------|---------------------------------- 657 * CS !norm | 0 | 0 | 0 (exact) 658 * CS norm | formX | 0 | MT_NORMALIZE 659 * CI !norm | upper | !ZCIEXACT | MT_NORMALIZE 660 * CI !norm | upper | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE 661 * CI norm | upper|formX | !ZCIEXACT | MT_NORMALIZE 662 * CI norm | upper|formX | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE 663 * CM !norm | upper | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE 664 * CM !norm | upper | ZCILOOK | MT_NORMALIZE 665 * CM norm | upper|formX | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE 666 * CM norm | upper|formX | ZCILOOK | MT_NORMALIZE 667 * 668 * Abbreviations: 669 * CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed 670 * upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER) 671 * formX = unicode normalization form set on fs creation 672 */ 673 static int 674 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, 675 int flag) 676 { 677 int error; 678 679 if (zp->z_zfsvfs->z_norm) { 680 matchtype_t mt = MT_NORMALIZE; 681 682 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) { 683 mt |= MT_MATCH_CASE; 684 } 685 686 error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id, 687 name, mt, tx); 688 } else { 689 error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx); 690 } 691 692 return (error); 693 } 694 695 /* 696 * Unlink zp from dzp, and mark zp for deletion if this was the last link. 697 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST). 698 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list. 699 * If it's non-NULL, we use it to indicate whether the znode needs deletion, 700 * and it's the caller's job to do it. 701 */ 702 int 703 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx, 704 int flag, boolean_t *unlinkedp) 705 { 706 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 707 vnode_t *vp = ZTOV(zp); 708 int zp_is_dir = (vp->v_type == VDIR); 709 boolean_t unlinked = B_FALSE; 710 sa_bulk_attr_t bulk[5]; 711 uint64_t mtime[2], ctime[2]; 712 int count = 0; 713 int error; 714 715 if (zfsvfs->z_replay == B_FALSE) { 716 ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__); 717 ASSERT_VOP_ELOCKED(ZTOV(zp), __func__); 718 } 719 if (!(flag & ZRENAMING)) { 720 721 if (zp_is_dir && !zfs_dirempty(zp)) 722 return (SET_ERROR(ENOTEMPTY)); 723 724 /* 725 * If we get here, we are going to try to remove the object. 726 * First try removing the name from the directory; if that 727 * fails, return the error. 728 */ 729 error = zfs_dropname(dzp, name, zp, tx, flag); 730 if (error != 0) { 731 return (error); 732 } 733 734 if (zp->z_links <= zp_is_dir) { 735 zfs_panic_recover("zfs: link count on vnode %p is %u, " 736 "should be at least %u", zp->z_vnode, 737 (int)zp->z_links, 738 zp_is_dir + 1); 739 zp->z_links = zp_is_dir + 1; 740 } 741 if (--zp->z_links == zp_is_dir) { 742 zp->z_unlinked = B_TRUE; 743 zp->z_links = 0; 744 unlinked = B_TRUE; 745 } else { 746 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), 747 NULL, &ctime, sizeof (ctime)); 748 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 749 NULL, &zp->z_pflags, sizeof (zp->z_pflags)); 750 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, 751 ctime); 752 } 753 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), 754 NULL, &zp->z_links, sizeof (zp->z_links)); 755 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 756 count = 0; 757 ASSERT0(error); 758 } else { 759 ASSERT(!zp->z_unlinked); 760 error = zfs_dropname(dzp, name, zp, tx, flag); 761 if (error != 0) 762 return (error); 763 } 764 765 dzp->z_size--; /* one dirent removed */ 766 dzp->z_links -= zp_is_dir; /* ".." link from zp */ 767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), 768 NULL, &dzp->z_links, sizeof (dzp->z_links)); 769 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), 770 NULL, &dzp->z_size, sizeof (dzp->z_size)); 771 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), 772 NULL, ctime, sizeof (ctime)); 773 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 774 NULL, mtime, sizeof (mtime)); 775 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 776 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags)); 777 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime); 778 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx); 779 ASSERT0(error); 780 781 if (unlinkedp != NULL) 782 *unlinkedp = unlinked; 783 else if (unlinked) 784 zfs_unlinked_add(zp, tx); 785 786 return (0); 787 } 788 789 /* 790 * Indicate whether the directory is empty. 791 */ 792 boolean_t 793 zfs_dirempty(znode_t *dzp) 794 { 795 return (dzp->z_size == 2); 796 } 797 798 int 799 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, znode_t **xvpp, cred_t *cr) 800 { 801 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 802 znode_t *xzp; 803 dmu_tx_t *tx; 804 int error; 805 zfs_acl_ids_t acl_ids; 806 boolean_t fuid_dirtied; 807 uint64_t parent __maybe_unused; 808 809 *xvpp = NULL; 810 811 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL, 812 &acl_ids)) != 0) 813 return (error); 814 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, 0)) { 815 zfs_acl_ids_free(&acl_ids); 816 return (SET_ERROR(EDQUOT)); 817 } 818 819 getnewvnode_reserve_(); 820 821 tx = dmu_tx_create(zfsvfs->z_os); 822 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 823 ZFS_SA_BASE_ATTR_SIZE); 824 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 825 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 826 fuid_dirtied = zfsvfs->z_fuid_dirty; 827 if (fuid_dirtied) 828 zfs_fuid_txhold(zfsvfs, tx); 829 error = dmu_tx_assign(tx, TXG_WAIT); 830 if (error) { 831 zfs_acl_ids_free(&acl_ids); 832 dmu_tx_abort(tx); 833 getnewvnode_drop_reserve(); 834 return (error); 835 } 836 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids); 837 838 if (fuid_dirtied) 839 zfs_fuid_sync(zfsvfs, tx); 840 841 ASSERT0(sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), &parent, 842 sizeof (parent))); 843 ASSERT3U(parent, ==, zp->z_id); 844 845 VERIFY0(sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id, 846 sizeof (xzp->z_id), tx)); 847 848 zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "", NULL, 849 acl_ids.z_fuidp, vap); 850 851 zfs_acl_ids_free(&acl_ids); 852 dmu_tx_commit(tx); 853 854 getnewvnode_drop_reserve(); 855 856 *xvpp = xzp; 857 858 return (0); 859 } 860 861 /* 862 * Return a znode for the extended attribute directory for zp. 863 * ** If the directory does not already exist, it is created ** 864 * 865 * IN: zp - znode to obtain attribute directory from 866 * cr - credentials of caller 867 * flags - flags from the VOP_LOOKUP call 868 * 869 * OUT: xzpp - pointer to extended attribute znode 870 * 871 * RETURN: 0 on success 872 * error number on failure 873 */ 874 int 875 zfs_get_xattrdir(znode_t *zp, znode_t **xzpp, cred_t *cr, int flags) 876 { 877 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 878 znode_t *xzp; 879 vattr_t va; 880 int error; 881 top: 882 error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR); 883 if (error) 884 return (error); 885 886 if (xzp != NULL) { 887 *xzpp = xzp; 888 return (0); 889 } 890 891 892 if (!(flags & CREATE_XATTR_DIR)) 893 return (SET_ERROR(ENOATTR)); 894 895 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 896 return (SET_ERROR(EROFS)); 897 } 898 899 /* 900 * The ability to 'create' files in an attribute 901 * directory comes from the write_xattr permission on the base file. 902 * 903 * The ability to 'search' an attribute directory requires 904 * read_xattr permission on the base file. 905 * 906 * Once in a directory the ability to read/write attributes 907 * is controlled by the permissions on the attribute file. 908 */ 909 va.va_mask = AT_MODE | AT_UID | AT_GID; 910 va.va_type = VDIR; 911 va.va_mode = S_IFDIR | S_ISVTX | 0777; 912 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid); 913 914 error = zfs_make_xattrdir(zp, &va, xzpp, cr); 915 916 if (error == ERESTART) { 917 /* NB: we already did dmu_tx_wait() if necessary */ 918 goto top; 919 } 920 if (error == 0) 921 VOP_UNLOCK1(ZTOV(*xzpp)); 922 923 return (error); 924 } 925 926 /* 927 * Decide whether it is okay to remove within a sticky directory. 928 * 929 * In sticky directories, write access is not sufficient; 930 * you can remove entries from a directory only if: 931 * 932 * you own the directory, 933 * you own the entry, 934 * the entry is a plain file and you have write access, 935 * or you are privileged (checked in secpolicy...). 936 * 937 * The function returns 0 if remove access is granted. 938 */ 939 int 940 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr) 941 { 942 uid_t uid; 943 uid_t downer; 944 uid_t fowner; 945 zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 946 947 if (zdp->z_zfsvfs->z_replay) 948 return (0); 949 950 if ((zdp->z_mode & S_ISVTX) == 0) 951 return (0); 952 953 downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER); 954 fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER); 955 956 if ((uid = crgetuid(cr)) == downer || uid == fowner || 957 (ZTOV(zp)->v_type == VREG && 958 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)) 959 return (0); 960 else 961 return (secpolicy_vnode_remove(ZTOV(zp), cr)); 962 } 963