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