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