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