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