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