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