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) 2012, 2014 by Delphix. All rights reserved. 24 * Copyright (c) 2014 Integros [integros.com] 25 */ 26 27 /* Portions Copyright 2007 Jeremy Teo */ 28 /* Portions Copyright 2011 Martin Matuska <mm (at) FreeBSD.org> */ 29 30 #ifdef _KERNEL 31 #include <sys/types.h> 32 #include <sys/param.h> 33 #include <sys/time.h> 34 #include <sys/systm.h> 35 #include <sys/sysmacros.h> 36 #include <sys/resource.h> 37 #include <sys/mntent.h> 38 #include <sys/u8_textprep.h> 39 #include <sys/dsl_dataset.h> 40 #include <sys/vfs.h> 41 #include <sys/vnode.h> 42 #include <sys/file.h> 43 #include <sys/kmem.h> 44 #include <sys/errno.h> 45 #include <sys/unistd.h> 46 #include <sys/atomic.h> 47 #include <sys/zfs_dir.h> 48 #include <sys/zfs_acl.h> 49 #include <sys/zfs_ioctl.h> 50 #include <sys/zfs_rlock.h> 51 #include <sys/zfs_fuid.h> 52 #include <sys/dnode.h> 53 #include <sys/fs/zfs.h> 54 #include <sys/kidmap.h> 55 56 #ifdef __NetBSD__ 57 #include <sys/zfs_ctldir.h> 58 #include <miscfs/specfs/specdev.h> 59 60 extern int (**zfs_vnodeop_p)(void *); 61 extern int (**zfs_fifoop_p)(void *); 62 extern int (**zfs_specop_p)(void *); 63 64 #endif 65 #endif /* _KERNEL */ 66 67 #include <sys/dmu.h> 68 #include <sys/dmu_objset.h> 69 #include <sys/refcount.h> 70 #include <sys/stat.h> 71 #include <sys/zap.h> 72 #include <sys/zfs_znode.h> 73 #include <sys/sa.h> 74 #include <sys/zfs_sa.h> 75 #include <sys/zfs_stat.h> 76 #include <sys/refcount.h> 77 78 #include "zfs_prop.h" 79 #include "zfs_comutil.h" 80 81 /* Used by fstat(1). */ 82 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 83 SYSCTL_NULL_INT_PTR, sizeof(znode_t), "sizeof(znode_t)"); 84 85 /* 86 * Define ZNODE_STATS to turn on statistic gathering. By default, it is only 87 * turned on when DEBUG is also defined. 88 */ 89 #ifdef DEBUG 90 #define ZNODE_STATS 91 #endif /* DEBUG */ 92 93 #ifdef ZNODE_STATS 94 #define ZNODE_STAT_ADD(stat) ((stat)++) 95 #else 96 #define ZNODE_STAT_ADD(stat) /* nothing */ 97 #endif /* ZNODE_STATS */ 98 99 /* 100 * Functions needed for userland (ie: libzpool) are not put under 101 * #ifdef_KERNEL; the rest of the functions have dependencies 102 * (such as VFS logic) that will not compile easily in userland. 103 */ 104 #ifdef _KERNEL 105 /* 106 * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to 107 * be freed before it can be safely accessed. 108 */ 109 krwlock_t zfsvfs_lock; 110 111 static kmem_cache_t *znode_cache = NULL; 112 113 /*ARGSUSED*/ 114 static void 115 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr) 116 { 117 /* 118 * We should never drop all dbuf refs without first clearing 119 * the eviction callback. 120 */ 121 panic("evicting znode %p\n", user_ptr); 122 } 123 124 extern struct vop_vector zfs_vnodeops; 125 extern struct vop_vector zfs_fifoops; 126 extern struct vop_vector zfs_shareops; 127 128 static int 129 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags) 130 { 131 znode_t *zp = buf; 132 133 POINTER_INVALIDATE(&zp->z_zfsvfs); 134 135 list_link_init(&zp->z_link_node); 136 137 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL); 138 139 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL); 140 avl_create(&zp->z_range_avl, zfs_range_compare, 141 sizeof (rl_t), offsetof(rl_t, r_node)); 142 143 zp->z_acl_cached = NULL; 144 zp->z_vnode = NULL; 145 zp->z_moved = 0; 146 return (0); 147 } 148 149 /*ARGSUSED*/ 150 static void 151 zfs_znode_cache_destructor(void *buf, void *arg) 152 { 153 znode_t *zp = buf; 154 155 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 156 ASSERT(ZTOV(zp) == NULL); 157 #ifndef __NetBSD__ 158 vn_free(ZTOV(zp)); 159 #endif 160 ASSERT(!list_link_active(&zp->z_link_node)); 161 mutex_destroy(&zp->z_acl_lock); 162 avl_destroy(&zp->z_range_avl); 163 mutex_destroy(&zp->z_range_lock); 164 165 ASSERT(zp->z_acl_cached == NULL); 166 } 167 168 #ifdef ZNODE_STATS 169 static struct { 170 uint64_t zms_zfsvfs_invalid; 171 uint64_t zms_zfsvfs_recheck1; 172 uint64_t zms_zfsvfs_unmounted; 173 uint64_t zms_zfsvfs_recheck2; 174 uint64_t zms_obj_held; 175 uint64_t zms_vnode_locked; 176 uint64_t zms_not_only_dnlc; 177 } znode_move_stats; 178 #endif /* ZNODE_STATS */ 179 180 #ifdef illumos 181 static void 182 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp) 183 { 184 vnode_t *vp; 185 186 /* Copy fields. */ 187 nzp->z_zfsvfs = ozp->z_zfsvfs; 188 189 /* Swap vnodes. */ 190 vp = nzp->z_vnode; 191 nzp->z_vnode = ozp->z_vnode; 192 ozp->z_vnode = vp; /* let destructor free the overwritten vnode */ 193 ZTOV(ozp)->v_data = ozp; 194 ZTOV(nzp)->v_data = nzp; 195 196 nzp->z_id = ozp->z_id; 197 ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */ 198 ASSERT(avl_numnodes(&ozp->z_range_avl) == 0); 199 nzp->z_unlinked = ozp->z_unlinked; 200 nzp->z_atime_dirty = ozp->z_atime_dirty; 201 nzp->z_zn_prefetch = ozp->z_zn_prefetch; 202 nzp->z_blksz = ozp->z_blksz; 203 nzp->z_seq = ozp->z_seq; 204 nzp->z_mapcnt = ozp->z_mapcnt; 205 nzp->z_gen = ozp->z_gen; 206 nzp->z_sync_cnt = ozp->z_sync_cnt; 207 nzp->z_is_sa = ozp->z_is_sa; 208 nzp->z_sa_hdl = ozp->z_sa_hdl; 209 bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2); 210 nzp->z_links = ozp->z_links; 211 nzp->z_size = ozp->z_size; 212 nzp->z_pflags = ozp->z_pflags; 213 nzp->z_uid = ozp->z_uid; 214 nzp->z_gid = ozp->z_gid; 215 nzp->z_mode = ozp->z_mode; 216 217 /* 218 * Since this is just an idle znode and kmem is already dealing with 219 * memory pressure, release any cached ACL. 220 */ 221 if (ozp->z_acl_cached) { 222 zfs_acl_free(ozp->z_acl_cached); 223 ozp->z_acl_cached = NULL; 224 } 225 226 sa_set_userp(nzp->z_sa_hdl, nzp); 227 228 /* 229 * Invalidate the original znode by clearing fields that provide a 230 * pointer back to the znode. Set the low bit of the vfs pointer to 231 * ensure that zfs_znode_move() recognizes the znode as invalid in any 232 * subsequent callback. 233 */ 234 ozp->z_sa_hdl = NULL; 235 POINTER_INVALIDATE(&ozp->z_zfsvfs); 236 237 /* 238 * Mark the znode. 239 */ 240 nzp->z_moved = 1; 241 ozp->z_moved = (uint8_t)-1; 242 } 243 244 /*ARGSUSED*/ 245 static kmem_cbrc_t 246 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg) 247 { 248 znode_t *ozp = buf, *nzp = newbuf; 249 zfsvfs_t *zfsvfs; 250 vnode_t *vp; 251 252 /* 253 * The znode is on the file system's list of known znodes if the vfs 254 * pointer is valid. We set the low bit of the vfs pointer when freeing 255 * the znode to invalidate it, and the memory patterns written by kmem 256 * (baddcafe and deadbeef) set at least one of the two low bits. A newly 257 * created znode sets the vfs pointer last of all to indicate that the 258 * znode is known and in a valid state to be moved by this function. 259 */ 260 zfsvfs = ozp->z_zfsvfs; 261 if (!POINTER_IS_VALID(zfsvfs)) { 262 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid); 263 return (KMEM_CBRC_DONT_KNOW); 264 } 265 266 /* 267 * Close a small window in which it's possible that the filesystem could 268 * be unmounted and freed, and zfsvfs, though valid in the previous 269 * statement, could point to unrelated memory by the time we try to 270 * prevent the filesystem from being unmounted. 271 */ 272 rw_enter(&zfsvfs_lock, RW_WRITER); 273 if (zfsvfs != ozp->z_zfsvfs) { 274 rw_exit(&zfsvfs_lock); 275 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1); 276 return (KMEM_CBRC_DONT_KNOW); 277 } 278 279 /* 280 * If the znode is still valid, then so is the file system. We know that 281 * no valid file system can be freed while we hold zfsvfs_lock, so we 282 * can safely ensure that the filesystem is not and will not be 283 * unmounted. The next statement is equivalent to ZFS_ENTER(). 284 */ 285 rrm_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG); 286 if (zfsvfs->z_unmounted) { 287 ZFS_EXIT(zfsvfs); 288 rw_exit(&zfsvfs_lock); 289 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted); 290 return (KMEM_CBRC_DONT_KNOW); 291 } 292 rw_exit(&zfsvfs_lock); 293 294 mutex_enter(&zfsvfs->z_znodes_lock); 295 /* 296 * Recheck the vfs pointer in case the znode was removed just before 297 * acquiring the lock. 298 */ 299 if (zfsvfs != ozp->z_zfsvfs) { 300 mutex_exit(&zfsvfs->z_znodes_lock); 301 ZFS_EXIT(zfsvfs); 302 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2); 303 return (KMEM_CBRC_DONT_KNOW); 304 } 305 306 /* 307 * At this point we know that as long as we hold z_znodes_lock, the 308 * znode cannot be freed and fields within the znode can be safely 309 * accessed. Now, prevent a race with zfs_zget(). 310 */ 311 if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) { 312 mutex_exit(&zfsvfs->z_znodes_lock); 313 ZFS_EXIT(zfsvfs); 314 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held); 315 return (KMEM_CBRC_LATER); 316 } 317 318 vp = ZTOV(ozp); 319 if (mutex_tryenter(&vp->v_lock) == 0) { 320 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 321 mutex_exit(&zfsvfs->z_znodes_lock); 322 ZFS_EXIT(zfsvfs); 323 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked); 324 return (KMEM_CBRC_LATER); 325 } 326 327 /* Only move znodes that are referenced _only_ by the DNLC. */ 328 if (vp->v_count != 1 || !vn_in_dnlc(vp)) { 329 mutex_exit(&vp->v_lock); 330 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 331 mutex_exit(&zfsvfs->z_znodes_lock); 332 ZFS_EXIT(zfsvfs); 333 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc); 334 return (KMEM_CBRC_LATER); 335 } 336 337 /* 338 * The znode is known and in a valid state to move. We're holding the 339 * locks needed to execute the critical section. 340 */ 341 zfs_znode_move_impl(ozp, nzp); 342 mutex_exit(&vp->v_lock); 343 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id); 344 345 list_link_replace(&ozp->z_link_node, &nzp->z_link_node); 346 mutex_exit(&zfsvfs->z_znodes_lock); 347 ZFS_EXIT(zfsvfs); 348 349 return (KMEM_CBRC_YES); 350 } 351 #endif /* illumos */ 352 353 void 354 zfs_znode_init(void) 355 { 356 /* 357 * Initialize zcache 358 */ 359 rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL); 360 ASSERT(znode_cache == NULL); 361 znode_cache = kmem_cache_create("zfs_znode_cache", 362 sizeof (znode_t), 0, zfs_znode_cache_constructor, 363 zfs_znode_cache_destructor, NULL, NULL, NULL, 0); 364 kmem_cache_set_move(znode_cache, zfs_znode_move); 365 } 366 367 void 368 zfs_znode_fini(void) 369 { 370 #ifdef illumos 371 /* 372 * Cleanup vfs & vnode ops 373 */ 374 zfs_remove_op_tables(); 375 #endif 376 377 /* 378 * Cleanup zcache 379 */ 380 if (znode_cache) 381 kmem_cache_destroy(znode_cache); 382 znode_cache = NULL; 383 rw_destroy(&zfsvfs_lock); 384 } 385 386 #ifdef illumos 387 struct vnodeops *zfs_dvnodeops; 388 struct vnodeops *zfs_fvnodeops; 389 struct vnodeops *zfs_symvnodeops; 390 struct vnodeops *zfs_xdvnodeops; 391 struct vnodeops *zfs_evnodeops; 392 struct vnodeops *zfs_sharevnodeops; 393 394 void 395 zfs_remove_op_tables() 396 { 397 /* 398 * Remove vfs ops 399 */ 400 ASSERT(zfsfstype); 401 (void) vfs_freevfsops_by_type(zfsfstype); 402 zfsfstype = 0; 403 404 /* 405 * Remove vnode ops 406 */ 407 if (zfs_dvnodeops) 408 vn_freevnodeops(zfs_dvnodeops); 409 if (zfs_fvnodeops) 410 vn_freevnodeops(zfs_fvnodeops); 411 if (zfs_symvnodeops) 412 vn_freevnodeops(zfs_symvnodeops); 413 if (zfs_xdvnodeops) 414 vn_freevnodeops(zfs_xdvnodeops); 415 if (zfs_evnodeops) 416 vn_freevnodeops(zfs_evnodeops); 417 if (zfs_sharevnodeops) 418 vn_freevnodeops(zfs_sharevnodeops); 419 420 zfs_dvnodeops = NULL; 421 zfs_fvnodeops = NULL; 422 zfs_symvnodeops = NULL; 423 zfs_xdvnodeops = NULL; 424 zfs_evnodeops = NULL; 425 zfs_sharevnodeops = NULL; 426 } 427 428 extern const fs_operation_def_t zfs_dvnodeops_template[]; 429 extern const fs_operation_def_t zfs_fvnodeops_template[]; 430 extern const fs_operation_def_t zfs_xdvnodeops_template[]; 431 extern const fs_operation_def_t zfs_symvnodeops_template[]; 432 extern const fs_operation_def_t zfs_evnodeops_template[]; 433 extern const fs_operation_def_t zfs_sharevnodeops_template[]; 434 435 int 436 zfs_create_op_tables() 437 { 438 int error; 439 440 /* 441 * zfs_dvnodeops can be set if mod_remove() calls mod_installfs() 442 * due to a failure to remove the the 2nd modlinkage (zfs_modldrv). 443 * In this case we just return as the ops vectors are already set up. 444 */ 445 if (zfs_dvnodeops) 446 return (0); 447 448 error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template, 449 &zfs_dvnodeops); 450 if (error) 451 return (error); 452 453 error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template, 454 &zfs_fvnodeops); 455 if (error) 456 return (error); 457 458 error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template, 459 &zfs_symvnodeops); 460 if (error) 461 return (error); 462 463 error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template, 464 &zfs_xdvnodeops); 465 if (error) 466 return (error); 467 468 error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template, 469 &zfs_evnodeops); 470 if (error) 471 return (error); 472 473 error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template, 474 &zfs_sharevnodeops); 475 476 return (error); 477 } 478 #endif /* illumos */ 479 480 int 481 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx) 482 { 483 zfs_acl_ids_t acl_ids; 484 vattr_t vattr; 485 znode_t *sharezp; 486 znode_t *zp; 487 int error; 488 489 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 490 vattr.va_type = VDIR; 491 vattr.va_mode = S_IFDIR|0555; 492 vattr.va_uid = crgetuid(kcred); 493 vattr.va_gid = crgetgid(kcred); 494 495 sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP); 496 ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs)); 497 sharezp->z_moved = 0; 498 sharezp->z_unlinked = 0; 499 sharezp->z_atime_dirty = 0; 500 sharezp->z_zfsvfs = zfsvfs; 501 sharezp->z_is_sa = zfsvfs->z_use_sa; 502 503 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr, 504 kcred, NULL, &acl_ids)); 505 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids); 506 ASSERT3P(zp, ==, sharezp); 507 POINTER_INVALIDATE(&sharezp->z_zfsvfs); 508 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ, 509 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx); 510 zfsvfs->z_shares_dir = sharezp->z_id; 511 512 zfs_acl_ids_free(&acl_ids); 513 sa_handle_destroy(sharezp->z_sa_hdl); 514 kmem_cache_free(znode_cache, sharezp); 515 516 return (error); 517 } 518 519 /* 520 * define a couple of values we need available 521 * for both 64 and 32 bit environments. 522 */ 523 #ifndef NBITSMINOR64 524 #define NBITSMINOR64 32 525 #endif 526 #ifndef MAXMAJ64 527 #define MAXMAJ64 0xffffffffUL 528 #endif 529 #ifndef MAXMIN64 530 #define MAXMIN64 0xffffffffUL 531 #endif 532 533 /* 534 * Create special expldev for ZFS private use. 535 * Can't use standard expldev since it doesn't do 536 * what we want. The standard expldev() takes a 537 * dev32_t in LP64 and expands it to a long dev_t. 538 * We need an interface that takes a dev32_t in ILP32 539 * and expands it to a long dev_t. 540 */ 541 static uint64_t 542 zfs_expldev(dev_t dev) 543 { 544 return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev)); 545 } 546 /* 547 * Special cmpldev for ZFS private use. 548 * Can't use standard cmpldev since it takes 549 * a long dev_t and compresses it to dev32_t in 550 * LP64. We need to do a compaction of a long dev_t 551 * to a dev32_t in ILP32. 552 */ 553 dev_t 554 zfs_cmpldev(uint64_t dev) 555 { 556 return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64))); 557 } 558 559 static void 560 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp, 561 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl) 562 { 563 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs)); 564 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id))); 565 566 ASSERT(zp->z_sa_hdl == NULL); 567 ASSERT(zp->z_acl_cached == NULL); 568 if (sa_hdl == NULL) { 569 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp, 570 SA_HDL_SHARED, &zp->z_sa_hdl)); 571 } else { 572 zp->z_sa_hdl = sa_hdl; 573 sa_set_userp(sa_hdl, zp); 574 } 575 576 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE; 577 578 /* 579 * Slap on VROOT if we are the root znode unless we are the root 580 * node of a snapshot mounted under .zfs. 581 */ 582 if (zp->z_id == zfsvfs->z_root && zfsvfs->z_parent == zfsvfs) 583 ZTOV(zp)->v_flag |= VROOT; 584 585 vn_exists(ZTOV(zp)); 586 } 587 588 void 589 zfs_znode_dmu_fini(znode_t *zp) 590 { 591 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) || 592 zp->z_unlinked || 593 RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock)); 594 595 sa_handle_destroy(zp->z_sa_hdl); 596 zp->z_sa_hdl = NULL; 597 } 598 599 #ifdef __FreeBSD__ 600 static void 601 zfs_vnode_forget(vnode_t *vp) 602 { 603 604 /* copied from insmntque_stddtr */ 605 vp->v_data = NULL; 606 vp->v_op = &dead_vnodeops; 607 vgone(vp); 608 vput(vp); 609 } 610 #endif /* __FreeBSD__ */ 611 612 /* 613 * Construct a new znode/vnode and intialize. 614 * 615 * This does not do a call to dmu_set_user() that is 616 * up to the caller to do, in case you don't want to 617 * return the znode 618 */ 619 static znode_t * 620 #ifdef __NetBSD__ 621 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz, 622 dmu_object_type_t obj_type, sa_handle_t *hdl, vnode_t *vp) 623 #else 624 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz, 625 dmu_object_type_t obj_type, sa_handle_t *hdl) 626 #endif 627 { 628 znode_t *zp; 629 #ifndef __NetBSD__ 630 vnode_t *vp; 631 #endif 632 uint64_t mode; 633 uint64_t parent; 634 sa_bulk_attr_t bulk[9]; 635 int count = 0; 636 int error; 637 638 zp = kmem_cache_alloc(znode_cache, KM_SLEEP); 639 640 #ifndef __NetBSD__ 641 KASSERT(curthread->td_vp_reserv > 0, 642 ("zfs_znode_alloc: getnewvnode without any vnodes reserved")); 643 error = getnewvnode("zfs", zfsvfs->z_parent->z_vfs, &zfs_vnodeops, &vp); 644 if (error != 0) { 645 kmem_cache_free(znode_cache, zp); 646 return (NULL); 647 } 648 #endif 649 zp->z_vnode = vp; 650 vp->v_data = zp; 651 652 ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs)); 653 zp->z_moved = 0; 654 655 /* 656 * Defer setting z_zfsvfs until the znode is ready to be a candidate for 657 * the zfs_znode_move() callback. 658 */ 659 zp->z_sa_hdl = NULL; 660 zp->z_unlinked = 0; 661 zp->z_atime_dirty = 0; 662 zp->z_mapcnt = 0; 663 zp->z_id = db->db_object; 664 zp->z_blksz = blksz; 665 zp->z_seq = 0x7A4653; 666 zp->z_sync_cnt = 0; 667 668 #ifdef __NetBSD__ 669 vp->v_op = zfs_vnodeop_p; 670 vp->v_tag = VT_ZFS; 671 zp->z_lockf = NULL; 672 #endif 673 674 vp = ZTOV(zp); 675 676 zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl); 677 678 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 679 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8); 680 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 681 &zp->z_size, 8); 682 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 683 &zp->z_links, 8); 684 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 685 &zp->z_pflags, 8); 686 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8); 687 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 688 &zp->z_atime, 16); 689 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 690 &zp->z_uid, 8); 691 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 692 &zp->z_gid, 8); 693 694 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) { 695 if (hdl == NULL) 696 sa_handle_destroy(zp->z_sa_hdl); 697 #ifndef __NetBSD__ 698 zfs_vnode_forget(vp); 699 #endif 700 zp->z_vnode = NULL; 701 kmem_cache_free(znode_cache, zp); 702 return (NULL); 703 } 704 705 zp->z_mode = mode; 706 707 vp->v_type = IFTOVT((mode_t)mode); 708 709 switch (vp->v_type) { 710 case VDIR: 711 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */ 712 break; 713 #if defined(illumos) || defined(__NetBSD__) 714 case VBLK: 715 case VCHR: 716 { 717 uint64_t rdev; 718 VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs), 719 &rdev, sizeof (rdev)) == 0); 720 721 #ifdef illumos 722 vp->v_rdev = zfs_cmpldev(rdev); 723 #else 724 vp->v_op = zfs_specop_p; 725 spec_node_init(vp, zfs_cmpldev(rdev)); 726 #endif 727 } 728 break; 729 #endif 730 case VFIFO: 731 #ifdef __NetBSD__ 732 vp->v_op = zfs_fifoop_p; 733 break; 734 #else /* __NetBSD__ */ 735 #ifdef illumos 736 case VSOCK: 737 case VDOOR: 738 #endif 739 vp->v_op = &zfs_fifoops; 740 break; 741 case VREG: 742 if (parent == zfsvfs->z_shares_dir) { 743 ASSERT(zp->z_uid == 0 && zp->z_gid == 0); 744 vp->v_op = &zfs_shareops; 745 } 746 break; 747 #ifdef illumos 748 case VLNK: 749 vn_setops(vp, zfs_symvnodeops); 750 break; 751 default: 752 vn_setops(vp, zfs_evnodeops); 753 break; 754 #endif 755 #endif /* __NetBSD__ */ 756 } 757 758 #ifdef __NetBSD__ 759 extern const struct genfs_ops zfs_genfsops; 760 genfs_node_init(vp, &zfs_genfsops); 761 uvm_vnp_setsize(vp, zp->z_size); 762 #endif 763 764 mutex_enter(&zfsvfs->z_znodes_lock); 765 list_insert_tail(&zfsvfs->z_all_znodes, zp); 766 membar_producer(); 767 /* 768 * Everything else must be valid before assigning z_zfsvfs makes the 769 * znode eligible for zfs_znode_move(). 770 */ 771 zp->z_zfsvfs = zfsvfs; 772 mutex_exit(&zfsvfs->z_znodes_lock); 773 774 #ifndef __NetBSD__ 775 /* 776 * Acquire vnode lock before making it available to the world. 777 */ 778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 779 VN_LOCK_AREC(vp); 780 if (vp->v_type != VFIFO) 781 VN_LOCK_ASHARE(vp); 782 #endif 783 784 #if defined(illumos) || defined(__NetBSD__) 785 VFS_HOLD(zfsvfs->z_vfs); 786 #endif 787 return (zp); 788 } 789 790 static uint64_t empty_xattr; 791 static uint64_t pad[4]; 792 static zfs_acl_phys_t acl_phys; 793 /* 794 * Create a new DMU object to hold a zfs znode. 795 * 796 * IN: dzp - parent directory for new znode 797 * vap - file attributes for new znode 798 * tx - dmu transaction id for zap operations 799 * cr - credentials of caller 800 * flag - flags: 801 * IS_ROOT_NODE - new object will be root 802 * IS_XATTR - new object is an attribute 803 * bonuslen - length of bonus buffer 804 * setaclp - File/Dir initial ACL 805 * fuidp - Tracks fuid allocation. 806 * 807 * OUT: zpp - allocated znode 808 * 809 */ 810 #ifdef __NetBSD__ 811 struct zfs_newvnode_args { 812 dmu_tx_t *tx; 813 uint_t flag; 814 zfs_acl_ids_t *acl_ids; 815 }; 816 817 static void 818 zfs_mknode1(znode_t *, vattr_t *, dmu_tx_t *, cred_t *, 819 uint_t, znode_t **, zfs_acl_ids_t *, vnode_t *); 820 821 int 822 zfs_loadvnode(struct mount *mp, struct vnode *vp, 823 const void *key, size_t key_len, const void **new_key) 824 { 825 int err, blksz; 826 uint64_t obj_num; 827 zfsvfs_t *zfsvfs; 828 dmu_buf_t *db; 829 dmu_object_info_t doi; 830 dmu_object_type_t obj_type; 831 sa_handle_t *hdl; 832 znode_t *zp; 833 834 if (key_len != sizeof(obj_num)) 835 return zfsctl_loadvnode(mp, vp, key, key_len, new_key); 836 837 memcpy(&obj_num, key, key_len); 838 839 zfsvfs = mp->mnt_data; 840 841 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 842 843 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 844 if (err) { 845 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 846 return (SET_ERROR(err)); 847 } 848 849 dmu_object_info_from_db(db, &doi); 850 if (doi.doi_bonus_type != DMU_OT_SA && 851 (doi.doi_bonus_type != DMU_OT_ZNODE || 852 (doi.doi_bonus_type == DMU_OT_ZNODE && 853 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 854 sa_buf_rele(db, NULL); 855 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 856 return (SET_ERROR(EINVAL)); 857 } 858 blksz = doi.doi_data_block_size; 859 obj_type = doi.doi_bonus_type; 860 hdl = dmu_buf_get_user(db); 861 862 if (hdl != NULL) { 863 sa_buf_rele(db, NULL); 864 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 865 return (SET_ERROR(ENOENT)); 866 } 867 868 zp = zfs_znode_alloc(zfsvfs, db, blksz, obj_type, hdl, vp); 869 if (zp == NULL) { 870 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 871 return (SET_ERROR(ENOENT)); 872 } 873 ASSERT(zp == VTOZ(vp)); 874 if (zfsvfs->z_use_namecache) 875 cache_enter_id(vp, zp->z_mode, zp->z_uid, zp->z_gid, true); 876 877 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 878 879 *new_key = &zp->z_id; 880 881 return 0; 882 } 883 884 int 885 zfs_newvnode(struct mount *mp, vnode_t *dvp, vnode_t *vp, vattr_t *vap, 886 cred_t *cr, void *extra, size_t *key_len, const void **new_key) 887 { 888 struct zfs_newvnode_args *args = extra; 889 znode_t *zp, *dzp = VTOZ(dvp); 890 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 891 dmu_tx_t *tx = args->tx; 892 uint_t flag = args->flag; 893 zfs_acl_ids_t *acl_ids = args->acl_ids; 894 895 zfs_mknode1(dzp, vap, tx, cr, flag, &zp, acl_ids, vp); 896 ASSERT(zp == VTOZ(vp)); 897 if (zfsvfs->z_use_namecache) 898 cache_enter_id(vp, zp->z_mode, zp->z_uid, zp->z_gid, true); 899 900 *key_len = sizeof(zp->z_id); 901 *new_key = &zp->z_id; 902 903 return 0; 904 } 905 906 void 907 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 908 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) 909 { 910 vnode_t *vp, *dvp = ZTOV(dzp); 911 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 912 struct zfs_newvnode_args args = { tx, flag, acl_ids }; 913 914 if (flag & IS_ROOT_NODE) 915 return zfs_mknode1(dzp, vap, tx, cr, flag, zpp, acl_ids, NULL); 916 917 VERIFY(vcache_new(zfsvfs->z_vfs, dvp, vap, cr, &args, &vp) == 0); 918 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 919 *zpp = VTOZ(vp); 920 } 921 922 static void 923 zfs_mknode1(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 924 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids, vnode_t *vp) 925 #else 926 void 927 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr, 928 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids) 929 #endif 930 { 931 uint64_t crtime[2], atime[2], mtime[2], ctime[2]; 932 uint64_t mode, size, links, parent, pflags; 933 uint64_t dzp_pflags = 0; 934 uint64_t rdev = 0; 935 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 936 dmu_buf_t *db; 937 timestruc_t now; 938 uint64_t gen, obj; 939 int err; 940 int bonuslen; 941 sa_handle_t *sa_hdl; 942 dmu_object_type_t obj_type; 943 sa_bulk_attr_t sa_attrs[ZPL_END]; 944 int cnt = 0; 945 zfs_acl_locator_cb_t locate = { 0 }; 946 947 ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE)); 948 949 if (zfsvfs->z_replay) { 950 obj = vap->va_nodeid; 951 now = vap->va_ctime; /* see zfs_replay_create() */ 952 gen = vap->va_nblocks; /* ditto */ 953 } else { 954 obj = 0; 955 vfs_timestamp(&now); 956 gen = dmu_tx_get_txg(tx); 957 } 958 959 obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE; 960 bonuslen = (obj_type == DMU_OT_SA) ? 961 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE; 962 963 /* 964 * Create a new DMU object. 965 */ 966 /* 967 * There's currently no mechanism for pre-reading the blocks that will 968 * be needed to allocate a new object, so we accept the small chance 969 * that there will be an i/o error and we will fail one of the 970 * assertions below. 971 */ 972 if (vap->va_type == VDIR) { 973 if (zfsvfs->z_replay) { 974 VERIFY0(zap_create_claim_norm(zfsvfs->z_os, obj, 975 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 976 obj_type, bonuslen, tx)); 977 } else { 978 obj = zap_create_norm(zfsvfs->z_os, 979 zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS, 980 obj_type, bonuslen, tx); 981 } 982 } else { 983 if (zfsvfs->z_replay) { 984 VERIFY0(dmu_object_claim(zfsvfs->z_os, obj, 985 DMU_OT_PLAIN_FILE_CONTENTS, 0, 986 obj_type, bonuslen, tx)); 987 } else { 988 obj = dmu_object_alloc(zfsvfs->z_os, 989 DMU_OT_PLAIN_FILE_CONTENTS, 0, 990 obj_type, bonuslen, tx); 991 } 992 } 993 994 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 995 VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db)); 996 997 /* 998 * If this is the root, fix up the half-initialized parent pointer 999 * to reference the just-allocated physical data area. 1000 */ 1001 if (flag & IS_ROOT_NODE) { 1002 dzp->z_id = obj; 1003 } else { 1004 dzp_pflags = dzp->z_pflags; 1005 } 1006 1007 /* 1008 * If parent is an xattr, so am I. 1009 */ 1010 if (dzp_pflags & ZFS_XATTR) { 1011 flag |= IS_XATTR; 1012 } 1013 1014 if (zfsvfs->z_use_fuids) 1015 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED; 1016 else 1017 pflags = 0; 1018 1019 if (vap->va_type == VDIR) { 1020 size = 2; /* contents ("." and "..") */ 1021 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1; 1022 } else { 1023 size = links = 0; 1024 } 1025 1026 if (vap->va_type == VBLK || vap->va_type == VCHR) { 1027 rdev = zfs_expldev(vap->va_rdev); 1028 } 1029 1030 parent = dzp->z_id; 1031 mode = acl_ids->z_mode; 1032 if (flag & IS_XATTR) 1033 pflags |= ZFS_XATTR; 1034 1035 /* 1036 * No execs denied will be deterimed when zfs_mode_compute() is called. 1037 */ 1038 pflags |= acl_ids->z_aclp->z_hints & 1039 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT| 1040 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED); 1041 1042 ZFS_TIME_ENCODE(&now, crtime); 1043 ZFS_TIME_ENCODE(&now, ctime); 1044 1045 if (vap->va_mask & AT_ATIME) { 1046 ZFS_TIME_ENCODE(&vap->va_atime, atime); 1047 } else { 1048 ZFS_TIME_ENCODE(&now, atime); 1049 } 1050 1051 if (vap->va_mask & AT_MTIME) { 1052 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 1053 } else { 1054 ZFS_TIME_ENCODE(&now, mtime); 1055 } 1056 1057 /* Now add in all of the "SA" attributes */ 1058 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED, 1059 &sa_hdl)); 1060 1061 /* 1062 * Setup the array of attributes to be replaced/set on the new file 1063 * 1064 * order for DMU_OT_ZNODE is critical since it needs to be constructed 1065 * in the old znode_phys_t format. Don't change this ordering 1066 */ 1067 1068 if (obj_type == DMU_OT_ZNODE) { 1069 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 1070 NULL, &atime, 16); 1071 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 1072 NULL, &mtime, 16); 1073 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 1074 NULL, &ctime, 16); 1075 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 1076 NULL, &crtime, 16); 1077 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 1078 NULL, &gen, 8); 1079 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 1080 NULL, &mode, 8); 1081 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 1082 NULL, &size, 8); 1083 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 1084 NULL, &parent, 8); 1085 } else { 1086 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs), 1087 NULL, &mode, 8); 1088 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs), 1089 NULL, &size, 8); 1090 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs), 1091 NULL, &gen, 8); 1092 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 1093 &acl_ids->z_fuid, 8); 1094 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 1095 &acl_ids->z_fgid, 8); 1096 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs), 1097 NULL, &parent, 8); 1098 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 1099 NULL, &pflags, 8); 1100 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs), 1101 NULL, &atime, 16); 1102 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs), 1103 NULL, &mtime, 16); 1104 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs), 1105 NULL, &ctime, 16); 1106 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs), 1107 NULL, &crtime, 16); 1108 } 1109 1110 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8); 1111 1112 if (obj_type == DMU_OT_ZNODE) { 1113 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL, 1114 &empty_xattr, 8); 1115 } 1116 if (obj_type == DMU_OT_ZNODE || 1117 (vap->va_type == VBLK || vap->va_type == VCHR)) { 1118 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs), 1119 NULL, &rdev, 8); 1120 1121 } 1122 if (obj_type == DMU_OT_ZNODE) { 1123 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs), 1124 NULL, &pflags, 8); 1125 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL, 1126 &acl_ids->z_fuid, 8); 1127 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL, 1128 &acl_ids->z_fgid, 8); 1129 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad, 1130 sizeof (uint64_t) * 4); 1131 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL, 1132 &acl_phys, sizeof (zfs_acl_phys_t)); 1133 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) { 1134 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL, 1135 &acl_ids->z_aclp->z_acl_count, 8); 1136 locate.cb_aclp = acl_ids->z_aclp; 1137 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs), 1138 zfs_acl_data_locator, &locate, 1139 acl_ids->z_aclp->z_acl_bytes); 1140 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags, 1141 acl_ids->z_fuid, acl_ids->z_fgid); 1142 } 1143 1144 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0); 1145 1146 if (!(flag & IS_ROOT_NODE)) { 1147 #ifdef __NetBSD__ 1148 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl, vp); 1149 #else 1150 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl); 1151 #endif 1152 ASSERT(*zpp != NULL); 1153 } else { 1154 /* 1155 * If we are creating the root node, the "parent" we 1156 * passed in is the znode for the root. 1157 */ 1158 *zpp = dzp; 1159 1160 (*zpp)->z_sa_hdl = sa_hdl; 1161 } 1162 1163 (*zpp)->z_pflags = pflags; 1164 (*zpp)->z_mode = mode; 1165 1166 if (vap->va_mask & AT_XVATTR) 1167 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx); 1168 1169 if (obj_type == DMU_OT_ZNODE || 1170 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) { 1171 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx)); 1172 } 1173 #ifndef __NetBSD__ 1174 if (!(flag & IS_ROOT_NODE)) { 1175 vnode_t *vp; 1176 1177 vp = ZTOV(*zpp); 1178 vp->v_vflag |= VV_FORCEINSMQ; 1179 err = insmntque(vp, zfsvfs->z_vfs); 1180 vp->v_vflag &= ~VV_FORCEINSMQ; 1181 KASSERT(err == 0, ("insmntque() failed: error %d", err)); 1182 } 1183 #endif 1184 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1185 } 1186 1187 /* 1188 * Update in-core attributes. It is assumed the caller will be doing an 1189 * sa_bulk_update to push the changes out. 1190 */ 1191 void 1192 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx) 1193 { 1194 xoptattr_t *xoap; 1195 1196 xoap = xva_getxoptattr(xvap); 1197 ASSERT(xoap); 1198 1199 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 1200 uint64_t times[2]; 1201 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times); 1202 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs), 1203 ×, sizeof (times), tx); 1204 XVA_SET_RTN(xvap, XAT_CREATETIME); 1205 } 1206 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 1207 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly, 1208 zp->z_pflags, tx); 1209 XVA_SET_RTN(xvap, XAT_READONLY); 1210 } 1211 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 1212 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden, 1213 zp->z_pflags, tx); 1214 XVA_SET_RTN(xvap, XAT_HIDDEN); 1215 } 1216 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 1217 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system, 1218 zp->z_pflags, tx); 1219 XVA_SET_RTN(xvap, XAT_SYSTEM); 1220 } 1221 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 1222 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive, 1223 zp->z_pflags, tx); 1224 XVA_SET_RTN(xvap, XAT_ARCHIVE); 1225 } 1226 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 1227 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable, 1228 zp->z_pflags, tx); 1229 XVA_SET_RTN(xvap, XAT_IMMUTABLE); 1230 } 1231 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 1232 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink, 1233 zp->z_pflags, tx); 1234 XVA_SET_RTN(xvap, XAT_NOUNLINK); 1235 } 1236 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 1237 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly, 1238 zp->z_pflags, tx); 1239 XVA_SET_RTN(xvap, XAT_APPENDONLY); 1240 } 1241 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 1242 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump, 1243 zp->z_pflags, tx); 1244 XVA_SET_RTN(xvap, XAT_NODUMP); 1245 } 1246 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 1247 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque, 1248 zp->z_pflags, tx); 1249 XVA_SET_RTN(xvap, XAT_OPAQUE); 1250 } 1251 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 1252 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED, 1253 xoap->xoa_av_quarantined, zp->z_pflags, tx); 1254 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 1255 } 1256 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 1257 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified, 1258 zp->z_pflags, tx); 1259 XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 1260 } 1261 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 1262 zfs_sa_set_scanstamp(zp, xvap, tx); 1263 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 1264 } 1265 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 1266 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse, 1267 zp->z_pflags, tx); 1268 XVA_SET_RTN(xvap, XAT_REPARSE); 1269 } 1270 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { 1271 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline, 1272 zp->z_pflags, tx); 1273 XVA_SET_RTN(xvap, XAT_OFFLINE); 1274 } 1275 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { 1276 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse, 1277 zp->z_pflags, tx); 1278 XVA_SET_RTN(xvap, XAT_SPARSE); 1279 } 1280 } 1281 1282 #ifdef __NetBSD__ 1283 1284 int 1285 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 1286 { 1287 int error; 1288 vnode_t *vp; 1289 1290 error = vcache_get(zfsvfs->z_vfs, &obj_num, sizeof(obj_num), &vp); 1291 if (error == 0) 1292 *zpp = VTOZ(vp); 1293 1294 return error; 1295 } 1296 1297 #else /* __NetBSD__ */ 1298 1299 int 1300 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp) 1301 { 1302 dmu_object_info_t doi; 1303 dmu_buf_t *db; 1304 znode_t *zp; 1305 vnode_t *vp; 1306 sa_handle_t *hdl; 1307 struct thread *td; 1308 int locked; 1309 int err; 1310 1311 td = curthread; 1312 getnewvnode_reserve(1); 1313 again: 1314 *zpp = NULL; 1315 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1316 1317 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1318 if (err) { 1319 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1320 getnewvnode_drop_reserve(); 1321 return (err); 1322 } 1323 1324 dmu_object_info_from_db(db, &doi); 1325 if (doi.doi_bonus_type != DMU_OT_SA && 1326 (doi.doi_bonus_type != DMU_OT_ZNODE || 1327 (doi.doi_bonus_type == DMU_OT_ZNODE && 1328 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1329 sa_buf_rele(db, NULL); 1330 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1331 #ifdef __FreeBSD__ 1332 getnewvnode_drop_reserve(); 1333 #endif 1334 return (SET_ERROR(EINVAL)); 1335 } 1336 1337 hdl = dmu_buf_get_user(db); 1338 if (hdl != NULL) { 1339 zp = sa_get_userdata(hdl); 1340 1341 /* 1342 * Since "SA" does immediate eviction we 1343 * should never find a sa handle that doesn't 1344 * know about the znode. 1345 */ 1346 ASSERT3P(zp, !=, NULL); 1347 ASSERT3U(zp->z_id, ==, obj_num); 1348 *zpp = zp; 1349 vp = ZTOV(zp); 1350 1351 /* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */ 1352 VN_HOLD(vp); 1353 1354 sa_buf_rele(db, NULL); 1355 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1356 1357 locked = VOP_ISLOCKED(vp); 1358 VI_LOCK(vp); 1359 if ((vp->v_iflag & VI_DOOMED) != 0 && 1360 locked != LK_EXCLUSIVE) { 1361 /* 1362 * The vnode is doomed and this thread doesn't 1363 * hold the exclusive lock on it, so the vnode 1364 * must be being reclaimed by another thread. 1365 * Otherwise the doomed vnode is being reclaimed 1366 * by this thread and zfs_zget is called from 1367 * ZIL internals. 1368 */ 1369 VI_UNLOCK(vp); 1370 1371 /* 1372 * XXX vrele() locks the vnode when the last reference 1373 * is dropped. Although in this case the vnode is 1374 * doomed / dead and so no inactivation is required, 1375 * the vnode lock is still acquired. That could result 1376 * in a LOR with z_teardown_lock if another thread holds 1377 * the vnode's lock and tries to take z_teardown_lock. 1378 * But that is only possible if the other thread peforms 1379 * a ZFS vnode operation on the vnode. That either 1380 * should not happen if the vnode is dead or the thread 1381 * should also have a refrence to the vnode and thus 1382 * our reference is not last. 1383 */ 1384 VN_RELE(vp); 1385 goto again; 1386 } 1387 VI_UNLOCK(vp); 1388 getnewvnode_drop_reserve(); 1389 return (0); 1390 } 1391 1392 /* 1393 * Not found create new znode/vnode 1394 * but only if file exists. 1395 * 1396 * There is a small window where zfs_vget() could 1397 * find this object while a file create is still in 1398 * progress. This is checked for in zfs_znode_alloc() 1399 * 1400 * if zfs_znode_alloc() fails it will drop the hold on the 1401 * bonus buffer. 1402 */ 1403 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size, 1404 doi.doi_bonus_type, NULL); 1405 if (zp == NULL) { 1406 err = SET_ERROR(ENOENT); 1407 } else { 1408 *zpp = zp; 1409 } 1410 if (err == 0) { 1411 vnode_t *vp = ZTOV(zp); 1412 1413 err = insmntque(vp, zfsvfs->z_vfs); 1414 if (err == 0) { 1415 vp->v_hash = obj_num; 1416 VOP_UNLOCK(vp, 0); 1417 } else { 1418 zp->z_vnode = NULL; 1419 zfs_znode_dmu_fini(zp); 1420 zfs_znode_free(zp); 1421 *zpp = NULL; 1422 } 1423 } 1424 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1425 getnewvnode_drop_reserve(); 1426 return (err); 1427 } 1428 1429 #endif /* __NetBSD__ */ 1430 1431 int 1432 zfs_rezget(znode_t *zp) 1433 { 1434 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1435 dmu_object_info_t doi; 1436 dmu_buf_t *db; 1437 vnode_t *vp; 1438 uint64_t obj_num = zp->z_id; 1439 uint64_t mode, size; 1440 sa_bulk_attr_t bulk[8]; 1441 int err; 1442 int count = 0; 1443 uint64_t gen; 1444 1445 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num); 1446 1447 mutex_enter(&zp->z_acl_lock); 1448 if (zp->z_acl_cached) { 1449 zfs_acl_free(zp->z_acl_cached); 1450 zp->z_acl_cached = NULL; 1451 } 1452 1453 mutex_exit(&zp->z_acl_lock); 1454 ASSERT(zp->z_sa_hdl == NULL); 1455 err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db); 1456 if (err) { 1457 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1458 return (err); 1459 } 1460 1461 dmu_object_info_from_db(db, &doi); 1462 if (doi.doi_bonus_type != DMU_OT_SA && 1463 (doi.doi_bonus_type != DMU_OT_ZNODE || 1464 (doi.doi_bonus_type == DMU_OT_ZNODE && 1465 doi.doi_bonus_size < sizeof (znode_phys_t)))) { 1466 sa_buf_rele(db, NULL); 1467 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1468 return (SET_ERROR(EINVAL)); 1469 } 1470 1471 zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL); 1472 size = zp->z_size; 1473 1474 /* reload cached values */ 1475 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, 1476 &gen, sizeof (gen)); 1477 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 1478 &zp->z_size, sizeof (zp->z_size)); 1479 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL, 1480 &zp->z_links, sizeof (zp->z_links)); 1481 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 1482 &zp->z_pflags, sizeof (zp->z_pflags)); 1483 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 1484 &zp->z_atime, sizeof (zp->z_atime)); 1485 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 1486 &zp->z_uid, sizeof (zp->z_uid)); 1487 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 1488 &zp->z_gid, sizeof (zp->z_gid)); 1489 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 1490 &mode, sizeof (mode)); 1491 1492 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) { 1493 zfs_znode_dmu_fini(zp); 1494 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1495 return (SET_ERROR(EIO)); 1496 } 1497 1498 zp->z_mode = mode; 1499 1500 if (gen != zp->z_gen) { 1501 zfs_znode_dmu_fini(zp); 1502 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1503 return (SET_ERROR(EIO)); 1504 } 1505 1506 /* 1507 * It is highly improbable but still quite possible that two 1508 * objects in different datasets are created with the same 1509 * object numbers and in transaction groups with the same 1510 * numbers. znodes corresponding to those objects would 1511 * have the same z_id and z_gen, but their other attributes 1512 * may be different. 1513 * zfs recv -F may replace one of such objects with the other. 1514 * As a result file properties recorded in the replaced 1515 * object's vnode may no longer match the received object's 1516 * properties. At present the only cached property is the 1517 * files type recorded in v_type. 1518 * So, handle this case by leaving the old vnode and znode 1519 * disassociated from the actual object. A new vnode and a 1520 * znode will be created if the object is accessed 1521 * (e.g. via a look-up). The old vnode and znode will be 1522 * recycled when the last vnode reference is dropped. 1523 */ 1524 vp = ZTOV(zp); 1525 if (vp->v_type != IFTOVT((mode_t)zp->z_mode)) { 1526 zfs_znode_dmu_fini(zp); 1527 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1528 return (EIO); 1529 } 1530 1531 zp->z_unlinked = (zp->z_links == 0); 1532 zp->z_blksz = doi.doi_data_block_size; 1533 #ifdef __NetBSD__ 1534 rw_enter(vp->v_uobj.vmobjlock, RW_WRITER); 1535 (void)VOP_PUTPAGES(vp, 0, 0, PGO_ALLPAGES|PGO_FREE|PGO_SYNCIO); 1536 #else 1537 vn_pages_remove(vp, 0, 0); 1538 #endif 1539 if (zp->z_size != size) 1540 vnode_pager_setsize(vp, zp->z_size); 1541 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num); 1542 1543 return (0); 1544 } 1545 1546 void 1547 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx) 1548 { 1549 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1550 objset_t *os = zfsvfs->z_os; 1551 uint64_t obj = zp->z_id; 1552 uint64_t acl_obj = zfs_external_acl(zp); 1553 1554 ZFS_OBJ_HOLD_ENTER(zfsvfs, obj); 1555 if (acl_obj) { 1556 VERIFY(!zp->z_is_sa); 1557 VERIFY(0 == dmu_object_free(os, acl_obj, tx)); 1558 } 1559 VERIFY(0 == dmu_object_free(os, obj, tx)); 1560 zfs_znode_dmu_fini(zp); 1561 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj); 1562 zfs_znode_free(zp); 1563 } 1564 1565 void 1566 zfs_zinactive(znode_t *zp) 1567 { 1568 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1569 uint64_t z_id = zp->z_id; 1570 1571 ASSERT(zp->z_sa_hdl); 1572 1573 /* 1574 * Don't allow a zfs_zget() while were trying to release this znode 1575 */ 1576 ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id); 1577 1578 /* 1579 * If this was the last reference to a file with no links, 1580 * remove the file from the file system. 1581 */ 1582 if (zp->z_unlinked) { 1583 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1584 zfs_rmnode(zp); 1585 return; 1586 } 1587 1588 zfs_znode_dmu_fini(zp); 1589 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id); 1590 zfs_znode_free(zp); 1591 } 1592 1593 void 1594 zfs_znode_free(znode_t *zp) 1595 { 1596 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1597 1598 #ifdef __NetBSD__ 1599 struct vnode *vp = ZTOV(zp); 1600 1601 genfs_node_destroy(vp); 1602 1603 /* 1604 * Interlock with zfs_sync(). 1605 */ 1606 mutex_enter(vp->v_interlock); 1607 vp->v_data = NULL; 1608 mutex_exit(vp->v_interlock); 1609 #endif 1610 1611 ASSERT(zp->z_sa_hdl == NULL); 1612 zp->z_vnode = NULL; 1613 mutex_enter(&zfsvfs->z_znodes_lock); 1614 POINTER_INVALIDATE(&zp->z_zfsvfs); 1615 list_remove(&zfsvfs->z_all_znodes, zp); 1616 mutex_exit(&zfsvfs->z_znodes_lock); 1617 1618 if (zp->z_acl_cached) { 1619 zfs_acl_free(zp->z_acl_cached); 1620 zp->z_acl_cached = NULL; 1621 } 1622 1623 kmem_cache_free(znode_cache, zp); 1624 1625 #ifdef illumos 1626 VFS_RELE(zfsvfs->z_vfs); 1627 #endif 1628 } 1629 1630 void 1631 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2], 1632 uint64_t ctime[2], boolean_t have_tx) 1633 { 1634 timestruc_t now; 1635 1636 vfs_timestamp(&now); 1637 1638 if (have_tx) { /* will sa_bulk_update happen really soon? */ 1639 zp->z_atime_dirty = 0; 1640 zp->z_seq++; 1641 } else { 1642 zp->z_atime_dirty = 1; 1643 } 1644 1645 if (flag & AT_ATIME) { 1646 ZFS_TIME_ENCODE(&now, zp->z_atime); 1647 } 1648 1649 if (flag & AT_MTIME) { 1650 ZFS_TIME_ENCODE(&now, mtime); 1651 if (zp->z_zfsvfs->z_use_fuids) { 1652 zp->z_pflags |= (ZFS_ARCHIVE | 1653 ZFS_AV_MODIFIED); 1654 } 1655 } 1656 1657 if (flag & AT_CTIME) { 1658 ZFS_TIME_ENCODE(&now, ctime); 1659 if (zp->z_zfsvfs->z_use_fuids) 1660 zp->z_pflags |= ZFS_ARCHIVE; 1661 } 1662 } 1663 1664 /* 1665 * Grow the block size for a file. 1666 * 1667 * IN: zp - znode of file to free data in. 1668 * size - requested block size 1669 * tx - open transaction. 1670 * 1671 * NOTE: this function assumes that the znode is write locked. 1672 */ 1673 void 1674 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx) 1675 { 1676 int error; 1677 u_longlong_t dummy; 1678 1679 if (size <= zp->z_blksz) 1680 return; 1681 /* 1682 * If the file size is already greater than the current blocksize, 1683 * we will not grow. If there is more than one block in a file, 1684 * the blocksize cannot change. 1685 */ 1686 if (zp->z_blksz && zp->z_size > zp->z_blksz) 1687 return; 1688 1689 error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id, 1690 size, 0, tx); 1691 1692 if (error == ENOTSUP) 1693 return; 1694 ASSERT0(error); 1695 1696 /* What blocksize did we actually get? */ 1697 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy); 1698 } 1699 1700 #ifdef illumos 1701 /* 1702 * This is a dummy interface used when pvn_vplist_dirty() should *not* 1703 * be calling back into the fs for a putpage(). E.g.: when truncating 1704 * a file, the pages being "thrown away* don't need to be written out. 1705 */ 1706 /* ARGSUSED */ 1707 static int 1708 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp, 1709 int flags, cred_t *cr) 1710 { 1711 ASSERT(0); 1712 return (0); 1713 } 1714 #endif 1715 1716 /* 1717 * Increase the file length 1718 * 1719 * IN: zp - znode of file to free data in. 1720 * end - new end-of-file 1721 * 1722 * RETURN: 0 on success, error code on failure 1723 */ 1724 static int 1725 zfs_extend(znode_t *zp, uint64_t end) 1726 { 1727 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1728 dmu_tx_t *tx; 1729 rl_t *rl; 1730 uint64_t newblksz; 1731 int error; 1732 1733 /* 1734 * We will change zp_size, lock the whole file. 1735 */ 1736 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1737 1738 /* 1739 * Nothing to do if file already at desired length. 1740 */ 1741 if (end <= zp->z_size) { 1742 zfs_range_unlock(rl); 1743 return (0); 1744 } 1745 tx = dmu_tx_create(zfsvfs->z_os); 1746 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1747 zfs_sa_upgrade_txholds(tx, zp); 1748 if (end > zp->z_blksz && 1749 (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) { 1750 /* 1751 * We are growing the file past the current block size. 1752 */ 1753 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) { 1754 /* 1755 * File's blocksize is already larger than the 1756 * "recordsize" property. Only let it grow to 1757 * the next power of 2. 1758 */ 1759 ASSERT(!ISP2(zp->z_blksz)); 1760 newblksz = MIN(end, 1 << highbit64(zp->z_blksz)); 1761 } else { 1762 newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz); 1763 } 1764 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz); 1765 } else { 1766 newblksz = 0; 1767 } 1768 1769 error = dmu_tx_assign(tx, TXG_WAIT); 1770 if (error) { 1771 dmu_tx_abort(tx); 1772 zfs_range_unlock(rl); 1773 return (error); 1774 } 1775 1776 if (newblksz) 1777 zfs_grow_blocksize(zp, newblksz, tx); 1778 1779 zp->z_size = end; 1780 1781 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs), 1782 &zp->z_size, sizeof (zp->z_size), tx)); 1783 1784 vnode_pager_setsize(ZTOV(zp), end); 1785 1786 zfs_range_unlock(rl); 1787 1788 dmu_tx_commit(tx); 1789 1790 return (0); 1791 } 1792 1793 /* 1794 * Free space in a file. 1795 * 1796 * IN: zp - znode of file to free data in. 1797 * off - start of section to free. 1798 * len - length of section to free. 1799 * 1800 * RETURN: 0 on success, error code on failure 1801 */ 1802 static int 1803 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len) 1804 { 1805 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1806 rl_t *rl; 1807 int error; 1808 1809 /* 1810 * Lock the range being freed. 1811 */ 1812 rl = zfs_range_lock(zp, off, len, RL_WRITER); 1813 1814 /* 1815 * Nothing to do if file already at desired length. 1816 */ 1817 if (off >= zp->z_size) { 1818 zfs_range_unlock(rl); 1819 return (0); 1820 } 1821 1822 if (off + len > zp->z_size) 1823 len = zp->z_size - off; 1824 1825 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len); 1826 1827 if (error == 0) { 1828 /* 1829 * In FreeBSD we cannot free block in the middle of a file, 1830 * but only at the end of a file, so this code path should 1831 * never happen. 1832 */ 1833 vnode_pager_setsize(ZTOV(zp), off); 1834 } 1835 1836 zfs_range_unlock(rl); 1837 1838 return (error); 1839 } 1840 1841 /* 1842 * Truncate a file 1843 * 1844 * IN: zp - znode of file to free data in. 1845 * end - new end-of-file. 1846 * 1847 * RETURN: 0 on success, error code on failure 1848 */ 1849 static int 1850 zfs_trunc(znode_t *zp, uint64_t end) 1851 { 1852 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1853 vnode_t *vp = ZTOV(zp); 1854 dmu_tx_t *tx; 1855 rl_t *rl; 1856 int error; 1857 sa_bulk_attr_t bulk[2]; 1858 int count = 0; 1859 1860 /* 1861 * We will change zp_size, lock the whole file. 1862 */ 1863 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER); 1864 1865 /* 1866 * Nothing to do if file already at desired length. 1867 */ 1868 if (end >= zp->z_size) { 1869 zfs_range_unlock(rl); 1870 return (0); 1871 } 1872 1873 error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end, -1); 1874 if (error) { 1875 zfs_range_unlock(rl); 1876 return (error); 1877 } 1878 tx = dmu_tx_create(zfsvfs->z_os); 1879 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1880 zfs_sa_upgrade_txholds(tx, zp); 1881 dmu_tx_mark_netfree(tx); 1882 error = dmu_tx_assign(tx, TXG_WAIT); 1883 if (error) { 1884 dmu_tx_abort(tx); 1885 zfs_range_unlock(rl); 1886 return (error); 1887 } 1888 1889 zp->z_size = end; 1890 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), 1891 NULL, &zp->z_size, sizeof (zp->z_size)); 1892 1893 if (end == 0) { 1894 zp->z_pflags &= ~ZFS_SPARSE; 1895 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1896 NULL, &zp->z_pflags, 8); 1897 } 1898 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0); 1899 1900 dmu_tx_commit(tx); 1901 1902 /* 1903 * Clear any mapped pages in the truncated region. This has to 1904 * happen outside of the transaction to avoid the possibility of 1905 * a deadlock with someone trying to push a page that we are 1906 * about to invalidate. 1907 */ 1908 vnode_pager_setsize(vp, end); 1909 1910 zfs_range_unlock(rl); 1911 1912 return (0); 1913 } 1914 1915 /* 1916 * Free space in a file 1917 * 1918 * IN: zp - znode of file to free data in. 1919 * off - start of range 1920 * len - end of range (0 => EOF) 1921 * flag - current file open mode flags. 1922 * log - TRUE if this action should be logged 1923 * 1924 * RETURN: 0 on success, error code on failure 1925 */ 1926 int 1927 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log) 1928 { 1929 vnode_t *vp = ZTOV(zp); 1930 dmu_tx_t *tx; 1931 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1932 zilog_t *zilog = zfsvfs->z_log; 1933 uint64_t mode; 1934 uint64_t mtime[2], ctime[2]; 1935 sa_bulk_attr_t bulk[3]; 1936 int count = 0; 1937 int error; 1938 1939 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode, 1940 sizeof (mode))) != 0) 1941 return (error); 1942 1943 if (off > zp->z_size) { 1944 error = zfs_extend(zp, off+len); 1945 if (error == 0 && log) 1946 goto log; 1947 else 1948 return (error); 1949 } 1950 1951 /* 1952 * Check for any locks in the region to be freed. 1953 */ 1954 1955 if (MANDLOCK(vp, (mode_t)mode)) { 1956 uint64_t length = (len ? len : zp->z_size - off); 1957 if (error = chklock(vp, FWRITE, off, length, flag, NULL)) 1958 return (error); 1959 } 1960 1961 if (len == 0) { 1962 error = zfs_trunc(zp, off); 1963 } else { 1964 if ((error = zfs_free_range(zp, off, len)) == 0 && 1965 off + len > zp->z_size) 1966 error = zfs_extend(zp, off+len); 1967 } 1968 if (error || !log) 1969 return (error); 1970 log: 1971 tx = dmu_tx_create(zfsvfs->z_os); 1972 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1973 zfs_sa_upgrade_txholds(tx, zp); 1974 error = dmu_tx_assign(tx, TXG_WAIT); 1975 if (error) { 1976 dmu_tx_abort(tx); 1977 return (error); 1978 } 1979 1980 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16); 1981 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16); 1982 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), 1983 NULL, &zp->z_pflags, 8); 1984 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE); 1985 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1986 ASSERT(error == 0); 1987 1988 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len); 1989 1990 dmu_tx_commit(tx); 1991 return (0); 1992 } 1993 1994 void 1995 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx) 1996 { 1997 uint64_t moid, obj, sa_obj, version; 1998 uint64_t sense = ZFS_CASE_SENSITIVE; 1999 uint64_t norm = 0; 2000 nvpair_t *elem; 2001 int error; 2002 int i; 2003 znode_t *rootzp = NULL; 2004 zfsvfs_t *zfsvfs; 2005 vattr_t vattr; 2006 znode_t *zp; 2007 zfs_acl_ids_t acl_ids; 2008 2009 /* 2010 * First attempt to create master node. 2011 */ 2012 /* 2013 * In an empty objset, there are no blocks to read and thus 2014 * there can be no i/o errors (which we assert below). 2015 */ 2016 moid = MASTER_NODE_OBJ; 2017 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE, 2018 DMU_OT_NONE, 0, tx); 2019 ASSERT(error == 0); 2020 2021 /* 2022 * Set starting attributes. 2023 */ 2024 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os))); 2025 elem = NULL; 2026 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) { 2027 /* For the moment we expect all zpl props to be uint64_ts */ 2028 uint64_t val; 2029 char *name; 2030 2031 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64); 2032 VERIFY(nvpair_value_uint64(elem, &val) == 0); 2033 name = nvpair_name(elem); 2034 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) { 2035 if (val < version) 2036 version = val; 2037 } else { 2038 error = zap_update(os, moid, name, 8, 1, &val, tx); 2039 } 2040 ASSERT(error == 0); 2041 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0) 2042 norm = val; 2043 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0) 2044 sense = val; 2045 } 2046 ASSERT(version != 0); 2047 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx); 2048 2049 /* 2050 * Create zap object used for SA attribute registration 2051 */ 2052 2053 if (version >= ZPL_VERSION_SA) { 2054 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE, 2055 DMU_OT_NONE, 0, tx); 2056 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx); 2057 ASSERT(error == 0); 2058 } else { 2059 sa_obj = 0; 2060 } 2061 /* 2062 * Create a delete queue. 2063 */ 2064 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx); 2065 2066 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx); 2067 ASSERT(error == 0); 2068 2069 /* 2070 * Create root znode. Create minimal znode/vnode/zfsvfs 2071 * to allow zfs_mknode to work. 2072 */ 2073 VATTR_NULL(&vattr); 2074 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; 2075 vattr.va_type = VDIR; 2076 vattr.va_mode = S_IFDIR|0755; 2077 vattr.va_uid = crgetuid(cr); 2078 vattr.va_gid = crgetgid(cr); 2079 2080 zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP); 2081 2082 rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP); 2083 ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs)); 2084 rootzp->z_moved = 0; 2085 rootzp->z_unlinked = 0; 2086 rootzp->z_atime_dirty = 0; 2087 rootzp->z_is_sa = USE_SA(version, os); 2088 2089 zfsvfs->z_os = os; 2090 zfsvfs->z_parent = zfsvfs; 2091 zfsvfs->z_version = version; 2092 zfsvfs->z_use_fuids = USE_FUIDS(version, os); 2093 zfsvfs->z_use_sa = USE_SA(version, os); 2094 zfsvfs->z_norm = norm; 2095 2096 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END, 2097 &zfsvfs->z_attr_table); 2098 2099 ASSERT(error == 0); 2100 2101 /* 2102 * Fold case on file systems that are always or sometimes case 2103 * insensitive. 2104 */ 2105 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED) 2106 zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER; 2107 2108 mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL); 2109 list_create(&zfsvfs->z_all_znodes, sizeof (znode_t), 2110 offsetof(znode_t, z_link_node)); 2111 2112 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 2113 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL); 2114 2115 rootzp->z_zfsvfs = zfsvfs; 2116 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr, 2117 cr, NULL, &acl_ids)); 2118 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids); 2119 ASSERT3P(zp, ==, rootzp); 2120 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx); 2121 ASSERT(error == 0); 2122 zfs_acl_ids_free(&acl_ids); 2123 POINTER_INVALIDATE(&rootzp->z_zfsvfs); 2124 2125 sa_handle_destroy(rootzp->z_sa_hdl); 2126 kmem_cache_free(znode_cache, rootzp); 2127 2128 /* 2129 * Create shares directory 2130 */ 2131 2132 error = zfs_create_share_dir(zfsvfs, tx); 2133 2134 ASSERT(error == 0); 2135 2136 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++) 2137 mutex_destroy(&zfsvfs->z_hold_mtx[i]); 2138 mutex_destroy(&zfsvfs->z_znodes_lock); 2139 kmem_free(zfsvfs, sizeof (zfsvfs_t)); 2140 } 2141 #endif /* _KERNEL */ 2142 2143 static int 2144 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table) 2145 { 2146 uint64_t sa_obj = 0; 2147 int error; 2148 2149 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj); 2150 if (error != 0 && error != ENOENT) 2151 return (error); 2152 2153 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table); 2154 return (error); 2155 } 2156 2157 static int 2158 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp, 2159 dmu_buf_t **db, void *tag) 2160 { 2161 dmu_object_info_t doi; 2162 int error; 2163 2164 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0) 2165 return (error); 2166 2167 dmu_object_info_from_db(*db, &doi); 2168 if ((doi.doi_bonus_type != DMU_OT_SA && 2169 doi.doi_bonus_type != DMU_OT_ZNODE) || 2170 doi.doi_bonus_type == DMU_OT_ZNODE && 2171 doi.doi_bonus_size < sizeof (znode_phys_t)) { 2172 sa_buf_rele(*db, tag); 2173 return (SET_ERROR(ENOTSUP)); 2174 } 2175 2176 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp); 2177 if (error != 0) { 2178 sa_buf_rele(*db, tag); 2179 return (error); 2180 } 2181 2182 return (0); 2183 } 2184 2185 void 2186 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag) 2187 { 2188 sa_handle_destroy(hdl); 2189 sa_buf_rele(db, tag); 2190 } 2191 2192 /* 2193 * Given an object number, return its parent object number and whether 2194 * or not the object is an extended attribute directory. 2195 */ 2196 static int 2197 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table, 2198 uint64_t *pobjp, int *is_xattrdir) 2199 { 2200 uint64_t parent; 2201 uint64_t pflags; 2202 uint64_t mode; 2203 uint64_t parent_mode; 2204 sa_bulk_attr_t bulk[3]; 2205 sa_handle_t *sa_hdl; 2206 dmu_buf_t *sa_db; 2207 int count = 0; 2208 int error; 2209 2210 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL, 2211 &parent, sizeof (parent)); 2212 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL, 2213 &pflags, sizeof (pflags)); 2214 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 2215 &mode, sizeof (mode)); 2216 2217 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0) 2218 return (error); 2219 2220 /* 2221 * When a link is removed its parent pointer is not changed and will 2222 * be invalid. There are two cases where a link is removed but the 2223 * file stays around, when it goes to the delete queue and when there 2224 * are additional links. 2225 */ 2226 error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG); 2227 if (error != 0) 2228 return (error); 2229 2230 error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode)); 2231 zfs_release_sa_handle(sa_hdl, sa_db, FTAG); 2232 if (error != 0) 2233 return (error); 2234 2235 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode); 2236 2237 /* 2238 * Extended attributes can be applied to files, directories, etc. 2239 * Otherwise the parent must be a directory. 2240 */ 2241 if (!*is_xattrdir && !S_ISDIR(parent_mode)) 2242 return (SET_ERROR(EINVAL)); 2243 2244 *pobjp = parent; 2245 2246 return (0); 2247 } 2248 2249 /* 2250 * Given an object number, return some zpl level statistics 2251 */ 2252 static int 2253 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table, 2254 zfs_stat_t *sb) 2255 { 2256 sa_bulk_attr_t bulk[4]; 2257 int count = 0; 2258 2259 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL, 2260 &sb->zs_mode, sizeof (sb->zs_mode)); 2261 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL, 2262 &sb->zs_gen, sizeof (sb->zs_gen)); 2263 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL, 2264 &sb->zs_links, sizeof (sb->zs_links)); 2265 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL, 2266 &sb->zs_ctime, sizeof (sb->zs_ctime)); 2267 2268 return (sa_bulk_lookup(hdl, bulk, count)); 2269 } 2270 2271 static int 2272 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl, 2273 sa_attr_type_t *sa_table, char *buf, int len) 2274 { 2275 sa_handle_t *sa_hdl; 2276 sa_handle_t *prevhdl = NULL; 2277 dmu_buf_t *prevdb = NULL; 2278 dmu_buf_t *sa_db = NULL; 2279 char *path = buf + len - 1; 2280 int error; 2281 2282 *path = '\0'; 2283 sa_hdl = hdl; 2284 2285 for (;;) { 2286 uint64_t pobj; 2287 char component[MAXNAMELEN + 2]; 2288 size_t complen; 2289 int is_xattrdir; 2290 2291 if (prevdb) 2292 zfs_release_sa_handle(prevhdl, prevdb, FTAG); 2293 2294 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj, 2295 &is_xattrdir)) != 0) 2296 break; 2297 2298 if (pobj == obj) { 2299 if (path[0] != '/') 2300 *--path = '/'; 2301 break; 2302 } 2303 2304 component[0] = '/'; 2305 if (is_xattrdir) { 2306 (void) sprintf(component + 1, "<xattrdir>"); 2307 } else { 2308 error = zap_value_search(osp, pobj, obj, 2309 ZFS_DIRENT_OBJ(-1ULL), component + 1); 2310 if (error != 0) 2311 break; 2312 } 2313 2314 complen = strlen(component); 2315 path -= complen; 2316 ASSERT(path >= buf); 2317 bcopy(component, path, complen); 2318 obj = pobj; 2319 2320 if (sa_hdl != hdl) { 2321 prevhdl = sa_hdl; 2322 prevdb = sa_db; 2323 } 2324 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG); 2325 if (error != 0) { 2326 sa_hdl = prevhdl; 2327 sa_db = prevdb; 2328 break; 2329 } 2330 } 2331 2332 if (sa_hdl != NULL && sa_hdl != hdl) { 2333 ASSERT(sa_db != NULL); 2334 zfs_release_sa_handle(sa_hdl, sa_db, FTAG); 2335 } 2336 2337 if (error == 0) 2338 (void) memmove(buf, path, buf + len - path); 2339 2340 return (error); 2341 } 2342 2343 int 2344 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len) 2345 { 2346 sa_attr_type_t *sa_table; 2347 sa_handle_t *hdl; 2348 dmu_buf_t *db; 2349 int error; 2350 2351 error = zfs_sa_setup(osp, &sa_table); 2352 if (error != 0) 2353 return (error); 2354 2355 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2356 if (error != 0) 2357 return (error); 2358 2359 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2360 2361 zfs_release_sa_handle(hdl, db, FTAG); 2362 return (error); 2363 } 2364 2365 int 2366 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb, 2367 char *buf, int len) 2368 { 2369 char *path = buf + len - 1; 2370 sa_attr_type_t *sa_table; 2371 sa_handle_t *hdl; 2372 dmu_buf_t *db; 2373 int error; 2374 2375 *path = '\0'; 2376 2377 error = zfs_sa_setup(osp, &sa_table); 2378 if (error != 0) 2379 return (error); 2380 2381 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG); 2382 if (error != 0) 2383 return (error); 2384 2385 error = zfs_obj_to_stats_impl(hdl, sa_table, sb); 2386 if (error != 0) { 2387 zfs_release_sa_handle(hdl, db, FTAG); 2388 return (error); 2389 } 2390 2391 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len); 2392 2393 zfs_release_sa_handle(hdl, db, FTAG); 2394 return (error); 2395 } 2396 2397 #ifdef _KERNEL 2398 int 2399 zfs_znode_parent_and_name(znode_t *zp, znode_t **dzpp, char *buf) 2400 { 2401 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2402 uint64_t parent; 2403 int is_xattrdir; 2404 int err; 2405 2406 /* Extended attributes should not be visible as regular files. */ 2407 if ((zp->z_pflags & ZFS_XATTR) != 0) 2408 return (SET_ERROR(EINVAL)); 2409 2410 err = zfs_obj_to_pobj(zfsvfs->z_os, zp->z_sa_hdl, zfsvfs->z_attr_table, 2411 &parent, &is_xattrdir); 2412 if (err != 0) 2413 return (err); 2414 ASSERT0(is_xattrdir); 2415 2416 /* No name as this is a root object. */ 2417 if (parent == zp->z_id) 2418 return (SET_ERROR(EINVAL)); 2419 2420 err = zap_value_search(zfsvfs->z_os, parent, zp->z_id, 2421 ZFS_DIRENT_OBJ(-1ULL), buf); 2422 if (err != 0) 2423 return (err); 2424 err = zfs_zget(zfsvfs, parent, dzpp); 2425 return (err); 2426 } 2427 #endif /* _KERNEL */ 2428