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