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