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