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