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