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