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