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