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