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