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