]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_znode.c
Fix zil_commit() NULL dereference
[mirror_zfs.git] / module / 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) 2013 by Delphix. All rights reserved.
24 */
25
26 /* Portions Copyright 2007 Jeremy Teo */
27
28 #ifdef _KERNEL
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/systm.h>
33 #include <sys/sysmacros.h>
34 #include <sys/resource.h>
35 #include <sys/mntent.h>
36 #include <sys/mkdev.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/vfs.h>
40 #include <sys/vfs_opreg.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/mode.h>
47 #include <sys/atomic.h>
48 #include <vm/pvn.h>
49 #include "fs/fs_subr.h"
50 #include <sys/zfs_dir.h>
51 #include <sys/zfs_acl.h>
52 #include <sys/zfs_ioctl.h>
53 #include <sys/zfs_rlock.h>
54 #include <sys/zfs_fuid.h>
55 #include <sys/zfs_vnops.h>
56 #include <sys/zfs_ctldir.h>
57 #include <sys/dnode.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/kidmap.h>
60 #include <sys/zpl.h>
61 #endif /* _KERNEL */
62
63 #include <sys/dmu.h>
64 #include <sys/refcount.h>
65 #include <sys/stat.h>
66 #include <sys/zap.h>
67 #include <sys/zfs_znode.h>
68 #include <sys/sa.h>
69 #include <sys/zfs_sa.h>
70 #include <sys/zfs_stat.h>
71
72 #include "zfs_prop.h"
73 #include "zfs_comutil.h"
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 static kmem_cache_t *znode_cache = NULL;
97
98 /*ARGSUSED*/
99 static int
100 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
101 {
102 znode_t *zp = buf;
103
104 inode_init_once(ZTOI(zp));
105 list_link_init(&zp->z_link_node);
106
107 mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
108 rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
109 rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
110 mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
111 rw_init(&zp->z_xattr_lock, NULL, RW_DEFAULT, NULL);
112
113 mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
114 avl_create(&zp->z_range_avl, zfs_range_compare,
115 sizeof (rl_t), offsetof(rl_t, r_node));
116
117 zp->z_dirlocks = NULL;
118 zp->z_acl_cached = NULL;
119 zp->z_xattr_cached = NULL;
120 zp->z_xattr_parent = NULL;
121 zp->z_moved = 0;
122 return (0);
123 }
124
125 /*ARGSUSED*/
126 static void
127 zfs_znode_cache_destructor(void *buf, void *arg)
128 {
129 znode_t *zp = buf;
130
131 ASSERT(!list_link_active(&zp->z_link_node));
132 mutex_destroy(&zp->z_lock);
133 rw_destroy(&zp->z_parent_lock);
134 rw_destroy(&zp->z_name_lock);
135 mutex_destroy(&zp->z_acl_lock);
136 rw_destroy(&zp->z_xattr_lock);
137 avl_destroy(&zp->z_range_avl);
138 mutex_destroy(&zp->z_range_lock);
139
140 ASSERT(zp->z_dirlocks == NULL);
141 ASSERT(zp->z_acl_cached == NULL);
142 ASSERT(zp->z_xattr_cached == NULL);
143 ASSERT(zp->z_xattr_parent == NULL);
144 }
145
146 void
147 zfs_znode_init(void)
148 {
149 /*
150 * Initialize zcache
151 */
152 ASSERT(znode_cache == NULL);
153 znode_cache = kmem_cache_create("zfs_znode_cache",
154 sizeof (znode_t), 0, zfs_znode_cache_constructor,
155 zfs_znode_cache_destructor, NULL, NULL, NULL, KMC_KMEM);
156 }
157
158 void
159 zfs_znode_fini(void)
160 {
161 /*
162 * Cleanup zcache
163 */
164 if (znode_cache)
165 kmem_cache_destroy(znode_cache);
166 znode_cache = NULL;
167 }
168
169 int
170 zfs_create_share_dir(zfs_sb_t *zsb, dmu_tx_t *tx)
171 {
172 #ifdef HAVE_SMB_SHARE
173 zfs_acl_ids_t acl_ids;
174 vattr_t vattr;
175 znode_t *sharezp;
176 vnode_t *vp;
177 znode_t *zp;
178 int error;
179
180 vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
181 vattr.va_mode = S_IFDIR | 0555;
182 vattr.va_uid = crgetuid(kcred);
183 vattr.va_gid = crgetgid(kcred);
184
185 sharezp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
186 sharezp->z_moved = 0;
187 sharezp->z_unlinked = 0;
188 sharezp->z_atime_dirty = 0;
189 sharezp->z_zfsvfs = zfsvfs;
190 sharezp->z_is_sa = zfsvfs->z_use_sa;
191
192 vp = ZTOV(sharezp);
193 vn_reinit(vp);
194 vp->v_type = VDIR;
195
196 VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
197 kcred, NULL, &acl_ids));
198 zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
199 ASSERT3P(zp, ==, sharezp);
200 ASSERT(!vn_in_dnlc(ZTOV(sharezp))); /* not valid to move */
201 POINTER_INVALIDATE(&sharezp->z_zfsvfs);
202 error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
203 ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
204 zfsvfs->z_shares_dir = sharezp->z_id;
205
206 zfs_acl_ids_free(&acl_ids);
207 // ZTOV(sharezp)->v_count = 0;
208 sa_handle_destroy(sharezp->z_sa_hdl);
209 kmem_cache_free(znode_cache, sharezp);
210
211 return (error);
212 #else
213 return (0);
214 #endif /* HAVE_SMB_SHARE */
215 }
216
217 static void
218 zfs_znode_sa_init(zfs_sb_t *zsb, znode_t *zp,
219 dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
220 {
221 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zsb, zp->z_id)));
222
223 mutex_enter(&zp->z_lock);
224
225 ASSERT(zp->z_sa_hdl == NULL);
226 ASSERT(zp->z_acl_cached == NULL);
227 if (sa_hdl == NULL) {
228 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, zp,
229 SA_HDL_SHARED, &zp->z_sa_hdl));
230 } else {
231 zp->z_sa_hdl = sa_hdl;
232 sa_set_userp(sa_hdl, zp);
233 }
234
235 zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
236
237 mutex_exit(&zp->z_lock);
238 }
239
240 void
241 zfs_znode_dmu_fini(znode_t *zp)
242 {
243 ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(ZTOZSB(zp), zp->z_id)) ||
244 zp->z_unlinked ||
245 RW_WRITE_HELD(&ZTOZSB(zp)->z_teardown_inactive_lock));
246
247 sa_handle_destroy(zp->z_sa_hdl);
248 zp->z_sa_hdl = NULL;
249 }
250
251 /*
252 * Called by new_inode() to allocate a new inode.
253 */
254 int
255 zfs_inode_alloc(struct super_block *sb, struct inode **ip)
256 {
257 znode_t *zp;
258
259 zp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
260 *ip = ZTOI(zp);
261
262 return (0);
263 }
264
265 /*
266 * Called in multiple places when an inode should be destroyed.
267 */
268 void
269 zfs_inode_destroy(struct inode *ip)
270 {
271 znode_t *zp = ITOZ(ip);
272 zfs_sb_t *zsb = ZTOZSB(zp);
273
274 if (zfsctl_is_node(ip))
275 zfsctl_inode_destroy(ip);
276
277 mutex_enter(&zsb->z_znodes_lock);
278 if (list_link_active(&zp->z_link_node)) {
279 list_remove(&zsb->z_all_znodes, zp);
280 zsb->z_nr_znodes--;
281 }
282 mutex_exit(&zsb->z_znodes_lock);
283
284 if (zp->z_acl_cached) {
285 zfs_acl_free(zp->z_acl_cached);
286 zp->z_acl_cached = NULL;
287 }
288
289 if (zp->z_xattr_cached) {
290 nvlist_free(zp->z_xattr_cached);
291 zp->z_xattr_cached = NULL;
292 }
293
294 if (zp->z_xattr_parent) {
295 iput(ZTOI(zp->z_xattr_parent));
296 zp->z_xattr_parent = NULL;
297 }
298
299 kmem_cache_free(znode_cache, zp);
300 }
301
302 static void
303 zfs_inode_set_ops(zfs_sb_t *zsb, struct inode *ip)
304 {
305 uint64_t rdev = 0;
306
307 switch (ip->i_mode & S_IFMT) {
308 case S_IFREG:
309 ip->i_op = &zpl_inode_operations;
310 ip->i_fop = &zpl_file_operations;
311 ip->i_mapping->a_ops = &zpl_address_space_operations;
312 break;
313
314 case S_IFDIR:
315 ip->i_op = &zpl_dir_inode_operations;
316 ip->i_fop = &zpl_dir_file_operations;
317 ITOZ(ip)->z_zn_prefetch = B_TRUE;
318 break;
319
320 case S_IFLNK:
321 ip->i_op = &zpl_symlink_inode_operations;
322 break;
323
324 /*
325 * rdev is only stored in a SA only for device files.
326 */
327 case S_IFCHR:
328 case S_IFBLK:
329 VERIFY(sa_lookup(ITOZ(ip)->z_sa_hdl, SA_ZPL_RDEV(zsb),
330 &rdev, sizeof (rdev)) == 0);
331 /*FALLTHROUGH*/
332 case S_IFIFO:
333 case S_IFSOCK:
334 init_special_inode(ip, ip->i_mode, rdev);
335 ip->i_op = &zpl_special_inode_operations;
336 break;
337
338 default:
339 printk("ZFS: Invalid mode: 0x%x\n", ip->i_mode);
340 VERIFY(0);
341 }
342 }
343
344 /*
345 * Construct a znode+inode and initialize.
346 *
347 * This does not do a call to dmu_set_user() that is
348 * up to the caller to do, in case you don't want to
349 * return the znode
350 */
351 static znode_t *
352 zfs_znode_alloc(zfs_sb_t *zsb, dmu_buf_t *db, int blksz,
353 dmu_object_type_t obj_type, uint64_t obj, sa_handle_t *hdl,
354 struct inode *dip)
355 {
356 znode_t *zp;
357 struct inode *ip;
358 uint64_t mode;
359 uint64_t parent;
360 sa_bulk_attr_t bulk[9];
361 int count = 0;
362
363 ASSERT(zsb != NULL);
364
365 ip = new_inode(zsb->z_sb);
366 if (ip == NULL)
367 return (NULL);
368
369 zp = ITOZ(ip);
370 ASSERT(zp->z_dirlocks == NULL);
371 ASSERT3P(zp->z_acl_cached, ==, NULL);
372 ASSERT3P(zp->z_xattr_cached, ==, NULL);
373 ASSERT3P(zp->z_xattr_parent, ==, NULL);
374 zp->z_moved = 0;
375 zp->z_sa_hdl = NULL;
376 zp->z_unlinked = 0;
377 zp->z_atime_dirty = 0;
378 zp->z_mapcnt = 0;
379 zp->z_id = db->db_object;
380 zp->z_blksz = blksz;
381 zp->z_seq = 0x7A4653;
382 zp->z_sync_cnt = 0;
383 zp->z_is_zvol = B_FALSE;
384 zp->z_is_mapped = B_FALSE;
385 zp->z_is_ctldir = B_FALSE;
386 zp->z_is_stale = B_FALSE;
387
388 zfs_znode_sa_init(zsb, zp, db, obj_type, hdl);
389
390 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, &mode, 8);
391 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL, &zp->z_gen, 8);
392 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL, &zp->z_size, 8);
393 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL, &zp->z_links, 8);
394 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
395 &zp->z_pflags, 8);
396 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
397 &parent, 8);
398 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
399 &zp->z_atime, 16);
400 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, &zp->z_uid, 8);
401 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL, &zp->z_gid, 8);
402
403 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
404 if (hdl == NULL)
405 sa_handle_destroy(zp->z_sa_hdl);
406
407 goto error;
408 }
409
410 zp->z_mode = mode;
411
412 /*
413 * xattr znodes hold a reference on their unique parent
414 */
415 if (dip && zp->z_pflags & ZFS_XATTR) {
416 igrab(dip);
417 zp->z_xattr_parent = ITOZ(dip);
418 }
419
420 ip->i_ino = obj;
421 zfs_inode_update(zp);
422 zfs_inode_set_ops(zsb, ip);
423
424 /*
425 * The only way insert_inode_locked() can fail is if the ip->i_ino
426 * number is already hashed for this super block. This can never
427 * happen because the inode numbers map 1:1 with the object numbers.
428 *
429 * The one exception is rolling back a mounted file system, but in
430 * this case all the active inode are unhashed during the rollback.
431 */
432 VERIFY3S(insert_inode_locked(ip), ==, 0);
433
434 mutex_enter(&zsb->z_znodes_lock);
435 list_insert_tail(&zsb->z_all_znodes, zp);
436 zsb->z_nr_znodes++;
437 membar_producer();
438 mutex_exit(&zsb->z_znodes_lock);
439
440 unlock_new_inode(ip);
441 return (zp);
442
443 error:
444 unlock_new_inode(ip);
445 iput(ip);
446 return (NULL);
447 }
448
449 void
450 zfs_set_inode_flags(znode_t *zp, struct inode *ip)
451 {
452 /*
453 * Linux and Solaris have different sets of file attributes, so we
454 * restrict this conversion to the intersection of the two.
455 */
456
457 if (zp->z_pflags & ZFS_IMMUTABLE)
458 ip->i_flags |= S_IMMUTABLE;
459 else
460 ip->i_flags &= ~S_IMMUTABLE;
461
462 if (zp->z_pflags & ZFS_APPENDONLY)
463 ip->i_flags |= S_APPEND;
464 else
465 ip->i_flags &= ~S_APPEND;
466 }
467
468 /*
469 * Update the embedded inode given the znode. We should work toward
470 * eliminating this function as soon as possible by removing values
471 * which are duplicated between the znode and inode. If the generic
472 * inode has the correct field it should be used, and the ZFS code
473 * updated to access the inode. This can be done incrementally.
474 */
475 void
476 zfs_inode_update(znode_t *zp)
477 {
478 zfs_sb_t *zsb;
479 struct inode *ip;
480 uint32_t blksize;
481 uint64_t atime[2], mtime[2], ctime[2];
482
483 ASSERT(zp != NULL);
484 zsb = ZTOZSB(zp);
485 ip = ZTOI(zp);
486
487 /* Skip .zfs control nodes which do not exist on disk. */
488 if (zfsctl_is_node(ip))
489 return;
490
491 sa_lookup(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), &atime, 16);
492 sa_lookup(zp->z_sa_hdl, SA_ZPL_MTIME(zsb), &mtime, 16);
493 sa_lookup(zp->z_sa_hdl, SA_ZPL_CTIME(zsb), &ctime, 16);
494
495 spin_lock(&ip->i_lock);
496 ip->i_generation = zp->z_gen;
497 ip->i_uid = SUID_TO_KUID(zp->z_uid);
498 ip->i_gid = SGID_TO_KGID(zp->z_gid);
499 set_nlink(ip, zp->z_links);
500 ip->i_mode = zp->z_mode;
501 zfs_set_inode_flags(zp, ip);
502 ip->i_blkbits = SPA_MINBLOCKSHIFT;
503 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &blksize,
504 (u_longlong_t *)&ip->i_blocks);
505
506 ZFS_TIME_DECODE(&ip->i_atime, atime);
507 ZFS_TIME_DECODE(&ip->i_mtime, mtime);
508 ZFS_TIME_DECODE(&ip->i_ctime, ctime);
509
510 i_size_write(ip, zp->z_size);
511 spin_unlock(&ip->i_lock);
512 }
513
514 /*
515 * Safely mark an inode dirty. Inodes which are part of a read-only
516 * file system or snapshot may not be dirtied.
517 */
518 void
519 zfs_mark_inode_dirty(struct inode *ip)
520 {
521 zfs_sb_t *zsb = ITOZSB(ip);
522
523 if (zfs_is_readonly(zsb) || dmu_objset_is_snapshot(zsb->z_os))
524 return;
525
526 mark_inode_dirty(ip);
527 }
528
529 static uint64_t empty_xattr;
530 static uint64_t pad[4];
531 static zfs_acl_phys_t acl_phys;
532 /*
533 * Create a new DMU object to hold a zfs znode.
534 *
535 * IN: dzp - parent directory for new znode
536 * vap - file attributes for new znode
537 * tx - dmu transaction id for zap operations
538 * cr - credentials of caller
539 * flag - flags:
540 * IS_ROOT_NODE - new object will be root
541 * IS_XATTR - new object is an attribute
542 * bonuslen - length of bonus buffer
543 * setaclp - File/Dir initial ACL
544 * fuidp - Tracks fuid allocation.
545 *
546 * OUT: zpp - allocated znode
547 *
548 */
549 void
550 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
551 uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
552 {
553 uint64_t crtime[2], atime[2], mtime[2], ctime[2];
554 uint64_t mode, size, links, parent, pflags;
555 uint64_t dzp_pflags = 0;
556 uint64_t rdev = 0;
557 zfs_sb_t *zsb = ZTOZSB(dzp);
558 dmu_buf_t *db;
559 timestruc_t now;
560 uint64_t gen, obj;
561 int err;
562 int bonuslen;
563 sa_handle_t *sa_hdl;
564 dmu_object_type_t obj_type;
565 sa_bulk_attr_t *sa_attrs;
566 int cnt = 0;
567 zfs_acl_locator_cb_t locate = { 0 };
568
569 if (zsb->z_replay) {
570 obj = vap->va_nodeid;
571 now = vap->va_ctime; /* see zfs_replay_create() */
572 gen = vap->va_nblocks; /* ditto */
573 } else {
574 obj = 0;
575 gethrestime(&now);
576 gen = dmu_tx_get_txg(tx);
577 }
578
579 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
580 bonuslen = (obj_type == DMU_OT_SA) ?
581 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
582
583 /*
584 * Create a new DMU object.
585 */
586 /*
587 * There's currently no mechanism for pre-reading the blocks that will
588 * be needed to allocate a new object, so we accept the small chance
589 * that there will be an i/o error and we will fail one of the
590 * assertions below.
591 */
592 if (S_ISDIR(vap->va_mode)) {
593 if (zsb->z_replay) {
594 err = zap_create_claim_norm(zsb->z_os, obj,
595 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
596 obj_type, bonuslen, tx);
597 ASSERT0(err);
598 } else {
599 obj = zap_create_norm(zsb->z_os,
600 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
601 obj_type, bonuslen, tx);
602 }
603 } else {
604 if (zsb->z_replay) {
605 err = dmu_object_claim(zsb->z_os, obj,
606 DMU_OT_PLAIN_FILE_CONTENTS, 0,
607 obj_type, bonuslen, tx);
608 ASSERT0(err);
609 } else {
610 obj = dmu_object_alloc(zsb->z_os,
611 DMU_OT_PLAIN_FILE_CONTENTS, 0,
612 obj_type, bonuslen, tx);
613 }
614 }
615
616 ZFS_OBJ_HOLD_ENTER(zsb, obj);
617 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
618
619 /*
620 * If this is the root, fix up the half-initialized parent pointer
621 * to reference the just-allocated physical data area.
622 */
623 if (flag & IS_ROOT_NODE) {
624 dzp->z_id = obj;
625 } else {
626 dzp_pflags = dzp->z_pflags;
627 }
628
629 /*
630 * If parent is an xattr, so am I.
631 */
632 if (dzp_pflags & ZFS_XATTR) {
633 flag |= IS_XATTR;
634 }
635
636 if (zsb->z_use_fuids)
637 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
638 else
639 pflags = 0;
640
641 if (S_ISDIR(vap->va_mode)) {
642 size = 2; /* contents ("." and "..") */
643 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
644 } else {
645 size = links = 0;
646 }
647
648 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
649 rdev = vap->va_rdev;
650
651 parent = dzp->z_id;
652 mode = acl_ids->z_mode;
653 if (flag & IS_XATTR)
654 pflags |= ZFS_XATTR;
655
656 /*
657 * No execs denied will be deterimed when zfs_mode_compute() is called.
658 */
659 pflags |= acl_ids->z_aclp->z_hints &
660 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
661 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
662
663 ZFS_TIME_ENCODE(&now, crtime);
664 ZFS_TIME_ENCODE(&now, ctime);
665
666 if (vap->va_mask & ATTR_ATIME) {
667 ZFS_TIME_ENCODE(&vap->va_atime, atime);
668 } else {
669 ZFS_TIME_ENCODE(&now, atime);
670 }
671
672 if (vap->va_mask & ATTR_MTIME) {
673 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
674 } else {
675 ZFS_TIME_ENCODE(&now, mtime);
676 }
677
678 /* Now add in all of the "SA" attributes */
679 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
680 &sa_hdl));
681
682 /*
683 * Setup the array of attributes to be replaced/set on the new file
684 *
685 * order for DMU_OT_ZNODE is critical since it needs to be constructed
686 * in the old znode_phys_t format. Don't change this ordering
687 */
688 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_PUSHPAGE);
689
690 if (obj_type == DMU_OT_ZNODE) {
691 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
692 NULL, &atime, 16);
693 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
694 NULL, &mtime, 16);
695 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
696 NULL, &ctime, 16);
697 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
698 NULL, &crtime, 16);
699 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
700 NULL, &gen, 8);
701 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
702 NULL, &mode, 8);
703 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
704 NULL, &size, 8);
705 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
706 NULL, &parent, 8);
707 } else {
708 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
709 NULL, &mode, 8);
710 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
711 NULL, &size, 8);
712 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
713 NULL, &gen, 8);
714 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
715 NULL, &acl_ids->z_fuid, 8);
716 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
717 NULL, &acl_ids->z_fgid, 8);
718 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
719 NULL, &parent, 8);
720 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
721 NULL, &pflags, 8);
722 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
723 NULL, &atime, 16);
724 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
725 NULL, &mtime, 16);
726 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
727 NULL, &ctime, 16);
728 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
729 NULL, &crtime, 16);
730 }
731
732 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
733
734 if (obj_type == DMU_OT_ZNODE) {
735 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
736 &empty_xattr, 8);
737 }
738 if (obj_type == DMU_OT_ZNODE ||
739 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
740 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
741 NULL, &rdev, 8);
742 }
743 if (obj_type == DMU_OT_ZNODE) {
744 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
745 NULL, &pflags, 8);
746 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
747 &acl_ids->z_fuid, 8);
748 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
749 &acl_ids->z_fgid, 8);
750 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
751 sizeof (uint64_t) * 4);
752 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
753 &acl_phys, sizeof (zfs_acl_phys_t));
754 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
755 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
756 &acl_ids->z_aclp->z_acl_count, 8);
757 locate.cb_aclp = acl_ids->z_aclp;
758 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
759 zfs_acl_data_locator, &locate,
760 acl_ids->z_aclp->z_acl_bytes);
761 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
762 acl_ids->z_fuid, acl_ids->z_fgid);
763 }
764
765 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
766
767 if (!(flag & IS_ROOT_NODE)) {
768 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
769 ZTOI(dzp));
770 VERIFY(*zpp != NULL);
771 VERIFY(dzp != NULL);
772 } else {
773 /*
774 * If we are creating the root node, the "parent" we
775 * passed in is the znode for the root.
776 */
777 *zpp = dzp;
778
779 (*zpp)->z_sa_hdl = sa_hdl;
780 }
781
782 (*zpp)->z_pflags = pflags;
783 (*zpp)->z_mode = mode;
784
785 if (obj_type == DMU_OT_ZNODE ||
786 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
787 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
788 ASSERT0(err);
789 }
790 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
791 ZFS_OBJ_HOLD_EXIT(zsb, obj);
792 }
793
794 /*
795 * Update in-core attributes. It is assumed the caller will be doing an
796 * sa_bulk_update to push the changes out.
797 */
798 void
799 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
800 {
801 xoptattr_t *xoap;
802
803 xoap = xva_getxoptattr(xvap);
804 ASSERT(xoap);
805
806 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
807 uint64_t times[2];
808 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
809 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
810 &times, sizeof (times), tx);
811 XVA_SET_RTN(xvap, XAT_CREATETIME);
812 }
813 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
814 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
815 zp->z_pflags, tx);
816 XVA_SET_RTN(xvap, XAT_READONLY);
817 }
818 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
819 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
820 zp->z_pflags, tx);
821 XVA_SET_RTN(xvap, XAT_HIDDEN);
822 }
823 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
824 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
825 zp->z_pflags, tx);
826 XVA_SET_RTN(xvap, XAT_SYSTEM);
827 }
828 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
829 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
830 zp->z_pflags, tx);
831 XVA_SET_RTN(xvap, XAT_ARCHIVE);
832 }
833 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
834 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
835 zp->z_pflags, tx);
836 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
837 }
838 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
839 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
840 zp->z_pflags, tx);
841 XVA_SET_RTN(xvap, XAT_NOUNLINK);
842 }
843 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
844 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
845 zp->z_pflags, tx);
846 XVA_SET_RTN(xvap, XAT_APPENDONLY);
847 }
848 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
849 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
850 zp->z_pflags, tx);
851 XVA_SET_RTN(xvap, XAT_NODUMP);
852 }
853 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
854 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
855 zp->z_pflags, tx);
856 XVA_SET_RTN(xvap, XAT_OPAQUE);
857 }
858 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
859 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
860 xoap->xoa_av_quarantined, zp->z_pflags, tx);
861 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
862 }
863 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
864 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
865 zp->z_pflags, tx);
866 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
867 }
868 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
869 zfs_sa_set_scanstamp(zp, xvap, tx);
870 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
871 }
872 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
873 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
874 zp->z_pflags, tx);
875 XVA_SET_RTN(xvap, XAT_REPARSE);
876 }
877 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
878 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
879 zp->z_pflags, tx);
880 XVA_SET_RTN(xvap, XAT_OFFLINE);
881 }
882 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
883 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
884 zp->z_pflags, tx);
885 XVA_SET_RTN(xvap, XAT_SPARSE);
886 }
887 }
888
889 int
890 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
891 {
892 dmu_object_info_t doi;
893 dmu_buf_t *db;
894 znode_t *zp;
895 int err;
896 sa_handle_t *hdl;
897
898 *zpp = NULL;
899
900 again:
901 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
902
903 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
904 if (err) {
905 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
906 return (err);
907 }
908
909 dmu_object_info_from_db(db, &doi);
910 if (doi.doi_bonus_type != DMU_OT_SA &&
911 (doi.doi_bonus_type != DMU_OT_ZNODE ||
912 (doi.doi_bonus_type == DMU_OT_ZNODE &&
913 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
914 sa_buf_rele(db, NULL);
915 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
916 return (SET_ERROR(EINVAL));
917 }
918
919 hdl = dmu_buf_get_user(db);
920 if (hdl != NULL) {
921 zp = sa_get_userdata(hdl);
922
923
924 /*
925 * Since "SA" does immediate eviction we
926 * should never find a sa handle that doesn't
927 * know about the znode.
928 */
929
930 ASSERT3P(zp, !=, NULL);
931
932 mutex_enter(&zp->z_lock);
933 ASSERT3U(zp->z_id, ==, obj_num);
934 if (zp->z_unlinked) {
935 err = SET_ERROR(ENOENT);
936 } else {
937 /*
938 * If igrab() returns NULL the VFS has independently
939 * determined the inode should be evicted and has
940 * called iput_final() to start the eviction process.
941 * The SA handle is still valid but because the VFS
942 * requires that the eviction succeed we must drop
943 * our locks and references to allow the eviction to
944 * complete. The zfs_zget() may then be retried.
945 *
946 * This unlikely case could be optimized by registering
947 * a sops->drop_inode() callback. The callback would
948 * need to detect the active SA hold thereby informing
949 * the VFS that this inode should not be evicted.
950 */
951 if (igrab(ZTOI(zp)) == NULL) {
952 mutex_exit(&zp->z_lock);
953 sa_buf_rele(db, NULL);
954 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
955 goto again;
956 }
957 *zpp = zp;
958 err = 0;
959 }
960 mutex_exit(&zp->z_lock);
961 sa_buf_rele(db, NULL);
962 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
963 return (err);
964 }
965
966 /*
967 * Not found create new znode/vnode but only if file exists.
968 *
969 * There is a small window where zfs_vget() could
970 * find this object while a file create is still in
971 * progress. This is checked for in zfs_znode_alloc()
972 *
973 * if zfs_znode_alloc() fails it will drop the hold on the
974 * bonus buffer.
975 */
976 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
977 doi.doi_bonus_type, obj_num, NULL, NULL);
978 if (zp == NULL) {
979 err = SET_ERROR(ENOENT);
980 } else {
981 *zpp = zp;
982 }
983 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
984 return (err);
985 }
986
987 int
988 zfs_rezget(znode_t *zp)
989 {
990 zfs_sb_t *zsb = ZTOZSB(zp);
991 dmu_object_info_t doi;
992 dmu_buf_t *db;
993 uint64_t obj_num = zp->z_id;
994 uint64_t mode;
995 sa_bulk_attr_t bulk[8];
996 int err;
997 int count = 0;
998 uint64_t gen;
999
1000 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
1001
1002 mutex_enter(&zp->z_acl_lock);
1003 if (zp->z_acl_cached) {
1004 zfs_acl_free(zp->z_acl_cached);
1005 zp->z_acl_cached = NULL;
1006 }
1007 mutex_exit(&zp->z_acl_lock);
1008
1009 rw_enter(&zp->z_xattr_lock, RW_WRITER);
1010 if (zp->z_xattr_cached) {
1011 nvlist_free(zp->z_xattr_cached);
1012 zp->z_xattr_cached = NULL;
1013 }
1014
1015 if (zp->z_xattr_parent) {
1016 iput(ZTOI(zp->z_xattr_parent));
1017 zp->z_xattr_parent = NULL;
1018 }
1019 rw_exit(&zp->z_xattr_lock);
1020
1021 ASSERT(zp->z_sa_hdl == NULL);
1022 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
1023 if (err) {
1024 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1025 return (err);
1026 }
1027
1028 dmu_object_info_from_db(db, &doi);
1029 if (doi.doi_bonus_type != DMU_OT_SA &&
1030 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1031 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1032 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1033 sa_buf_rele(db, NULL);
1034 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1035 return (SET_ERROR(EINVAL));
1036 }
1037
1038 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
1039
1040 /* reload cached values */
1041 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
1042 &gen, sizeof (gen));
1043 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
1044 &zp->z_size, sizeof (zp->z_size));
1045 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
1046 &zp->z_links, sizeof (zp->z_links));
1047 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
1048 &zp->z_pflags, sizeof (zp->z_pflags));
1049 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
1050 &zp->z_atime, sizeof (zp->z_atime));
1051 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
1052 &zp->z_uid, sizeof (zp->z_uid));
1053 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
1054 &zp->z_gid, sizeof (zp->z_gid));
1055 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
1056 &mode, sizeof (mode));
1057
1058 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1059 zfs_znode_dmu_fini(zp);
1060 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1061 return (SET_ERROR(EIO));
1062 }
1063
1064 zp->z_mode = mode;
1065
1066 if (gen != zp->z_gen) {
1067 zfs_znode_dmu_fini(zp);
1068 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1069 return (SET_ERROR(EIO));
1070 }
1071
1072 zp->z_unlinked = (zp->z_links == 0);
1073 zp->z_blksz = doi.doi_data_block_size;
1074 zfs_inode_update(zp);
1075
1076 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1077
1078 return (0);
1079 }
1080
1081 void
1082 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1083 {
1084 zfs_sb_t *zsb = ZTOZSB(zp);
1085 objset_t *os = zsb->z_os;
1086 uint64_t obj = zp->z_id;
1087 uint64_t acl_obj = zfs_external_acl(zp);
1088
1089 ZFS_OBJ_HOLD_ENTER(zsb, obj);
1090 if (acl_obj) {
1091 VERIFY(!zp->z_is_sa);
1092 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1093 }
1094 VERIFY(0 == dmu_object_free(os, obj, tx));
1095 zfs_znode_dmu_fini(zp);
1096 ZFS_OBJ_HOLD_EXIT(zsb, obj);
1097 }
1098
1099 void
1100 zfs_zinactive(znode_t *zp)
1101 {
1102 zfs_sb_t *zsb = ZTOZSB(zp);
1103 uint64_t z_id = zp->z_id;
1104 boolean_t drop_mutex = 0;
1105
1106 ASSERT(zp->z_sa_hdl);
1107
1108 /*
1109 * Don't allow a zfs_zget() while were trying to release this znode.
1110 *
1111 * Linux allows direct memory reclaim which means that any KM_SLEEP
1112 * allocation may trigger inode eviction. This can lead to a deadlock
1113 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1114 * zfs_zinactive() call path. To avoid this deadlock the process
1115 * must not reacquire the mutex when it is already holding it.
1116 */
1117 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1118 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1119 drop_mutex = 1;
1120 }
1121
1122 mutex_enter(&zp->z_lock);
1123
1124 /*
1125 * If this was the last reference to a file with no links,
1126 * remove the file from the file system.
1127 */
1128 if (zp->z_unlinked) {
1129 mutex_exit(&zp->z_lock);
1130
1131 if (drop_mutex)
1132 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1133
1134 zfs_rmnode(zp);
1135 return;
1136 }
1137
1138 mutex_exit(&zp->z_lock);
1139 zfs_znode_dmu_fini(zp);
1140
1141 if (drop_mutex)
1142 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1143 }
1144
1145 static inline int
1146 zfs_compare_timespec(struct timespec *t1, struct timespec *t2)
1147 {
1148 if (t1->tv_sec < t2->tv_sec)
1149 return (-1);
1150
1151 if (t1->tv_sec > t2->tv_sec)
1152 return (1);
1153
1154 return (t1->tv_nsec - t2->tv_nsec);
1155 }
1156
1157 /*
1158 * Determine whether the znode's atime must be updated. The logic mostly
1159 * duplicates the Linux kernel's relatime_need_update() functionality.
1160 * This function is only called if the underlying filesystem actually has
1161 * atime updates enabled.
1162 */
1163 static inline boolean_t
1164 zfs_atime_need_update(znode_t *zp, timestruc_t *now)
1165 {
1166 if (!ZTOZSB(zp)->z_relatime)
1167 return (B_TRUE);
1168
1169 /*
1170 * In relatime mode, only update the atime if the previous atime
1171 * is earlier than either the ctime or mtime or if at least a day
1172 * has passed since the last update of atime.
1173 */
1174 if (zfs_compare_timespec(&ZTOI(zp)->i_mtime, &ZTOI(zp)->i_atime) >= 0)
1175 return (B_TRUE);
1176
1177 if (zfs_compare_timespec(&ZTOI(zp)->i_ctime, &ZTOI(zp)->i_atime) >= 0)
1178 return (B_TRUE);
1179
1180 if ((long)now->tv_sec - ZTOI(zp)->i_atime.tv_sec >= 24*60*60)
1181 return (B_TRUE);
1182
1183 return (B_FALSE);
1184 }
1185
1186 /*
1187 * Prepare to update znode time stamps.
1188 *
1189 * IN: zp - znode requiring timestamp update
1190 * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags
1191 * have_tx - true of caller is creating a new txg
1192 *
1193 * OUT: zp - new atime (via underlying inode's i_atime)
1194 * mtime - new mtime
1195 * ctime - new ctime
1196 *
1197 * NOTE: The arguments are somewhat redundant. The following condition
1198 * is always true:
1199 *
1200 * have_tx == !(flag & ATTR_ATIME)
1201 */
1202 void
1203 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1204 uint64_t ctime[2], boolean_t have_tx)
1205 {
1206 timestruc_t now;
1207
1208 ASSERT(have_tx == !(flag & ATTR_ATIME));
1209 gethrestime(&now);
1210
1211 /*
1212 * NOTE: The following test intentionally does not update z_atime_dirty
1213 * in the case where an ATIME update has been requested but for which
1214 * the update is omitted due to relatime logic. The rationale being
1215 * that if the flag was set somewhere else, we should leave it alone
1216 * here.
1217 */
1218 if (flag & ATTR_ATIME) {
1219 if (zfs_atime_need_update(zp, &now)) {
1220 ZFS_TIME_ENCODE(&now, zp->z_atime);
1221 ZTOI(zp)->i_atime.tv_sec = zp->z_atime[0];
1222 ZTOI(zp)->i_atime.tv_nsec = zp->z_atime[1];
1223 zp->z_atime_dirty = 1;
1224 }
1225 } else {
1226 zp->z_atime_dirty = 0;
1227 zp->z_seq++;
1228 }
1229
1230 if (flag & ATTR_MTIME) {
1231 ZFS_TIME_ENCODE(&now, mtime);
1232 if (ZTOZSB(zp)->z_use_fuids) {
1233 zp->z_pflags |= (ZFS_ARCHIVE |
1234 ZFS_AV_MODIFIED);
1235 }
1236 }
1237
1238 if (flag & ATTR_CTIME) {
1239 ZFS_TIME_ENCODE(&now, ctime);
1240 if (ZTOZSB(zp)->z_use_fuids)
1241 zp->z_pflags |= ZFS_ARCHIVE;
1242 }
1243 }
1244
1245 /*
1246 * Grow the block size for a file.
1247 *
1248 * IN: zp - znode of file to free data in.
1249 * size - requested block size
1250 * tx - open transaction.
1251 *
1252 * NOTE: this function assumes that the znode is write locked.
1253 */
1254 void
1255 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1256 {
1257 int error;
1258 u_longlong_t dummy;
1259
1260 if (size <= zp->z_blksz)
1261 return;
1262 /*
1263 * If the file size is already greater than the current blocksize,
1264 * we will not grow. If there is more than one block in a file,
1265 * the blocksize cannot change.
1266 */
1267 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1268 return;
1269
1270 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1271 size, 0, tx);
1272
1273 if (error == ENOTSUP)
1274 return;
1275 ASSERT0(error);
1276
1277 /* What blocksize did we actually get? */
1278 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1279 }
1280
1281 /*
1282 * Increase the file length
1283 *
1284 * IN: zp - znode of file to free data in.
1285 * end - new end-of-file
1286 *
1287 * RETURN: 0 on success, error code on failure
1288 */
1289 static int
1290 zfs_extend(znode_t *zp, uint64_t end)
1291 {
1292 zfs_sb_t *zsb = ZTOZSB(zp);
1293 dmu_tx_t *tx;
1294 rl_t *rl;
1295 uint64_t newblksz;
1296 int error;
1297
1298 /*
1299 * We will change zp_size, lock the whole file.
1300 */
1301 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1302
1303 /*
1304 * Nothing to do if file already at desired length.
1305 */
1306 if (end <= zp->z_size) {
1307 zfs_range_unlock(rl);
1308 return (0);
1309 }
1310 tx = dmu_tx_create(zsb->z_os);
1311 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1312 zfs_sa_upgrade_txholds(tx, zp);
1313 if (end > zp->z_blksz &&
1314 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1315 /*
1316 * We are growing the file past the current block size.
1317 */
1318 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1319 ASSERT(!ISP2(zp->z_blksz));
1320 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1321 } else {
1322 newblksz = MIN(end, ZTOZSB(zp)->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_range_unlock(rl);
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(ZTOZSB(zp)),
1342 &zp->z_size, sizeof (zp->z_size), tx));
1343
1344 zfs_range_unlock(rl);
1345
1346 dmu_tx_commit(tx);
1347
1348 return (0);
1349 }
1350
1351 /*
1352 * Free space in a file.
1353 *
1354 * IN: zp - znode of file to free data in.
1355 * off - start of section to free.
1356 * len - length of section to free.
1357 *
1358 * RETURN: 0 on success, error code on failure
1359 */
1360 static int
1361 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1362 {
1363 zfs_sb_t *zsb = ZTOZSB(zp);
1364 rl_t *rl;
1365 int error;
1366
1367 /*
1368 * Lock the range being freed.
1369 */
1370 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1371
1372 /*
1373 * Nothing to do if file already at desired length.
1374 */
1375 if (off >= zp->z_size) {
1376 zfs_range_unlock(rl);
1377 return (0);
1378 }
1379
1380 if (off + len > zp->z_size)
1381 len = zp->z_size - off;
1382
1383 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1384
1385 zfs_range_unlock(rl);
1386
1387 return (error);
1388 }
1389
1390 /*
1391 * Truncate a file
1392 *
1393 * IN: zp - znode of file to free data in.
1394 * end - new end-of-file.
1395 *
1396 * RETURN: 0 on success, error code on failure
1397 */
1398 static int
1399 zfs_trunc(znode_t *zp, uint64_t end)
1400 {
1401 zfs_sb_t *zsb = ZTOZSB(zp);
1402 dmu_tx_t *tx;
1403 rl_t *rl;
1404 int error;
1405 sa_bulk_attr_t bulk[2];
1406 int count = 0;
1407
1408 /*
1409 * We will change zp_size, lock the whole file.
1410 */
1411 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1412
1413 /*
1414 * Nothing to do if file already at desired length.
1415 */
1416 if (end >= zp->z_size) {
1417 zfs_range_unlock(rl);
1418 return (0);
1419 }
1420
1421 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1422 if (error) {
1423 zfs_range_unlock(rl);
1424 return (error);
1425 }
1426 top:
1427 tx = dmu_tx_create(zsb->z_os);
1428 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1429 zfs_sa_upgrade_txholds(tx, zp);
1430 error = dmu_tx_assign(tx, TXG_NOWAIT);
1431 if (error) {
1432 if (error == ERESTART) {
1433 dmu_tx_wait(tx);
1434 dmu_tx_abort(tx);
1435 goto top;
1436 }
1437 dmu_tx_abort(tx);
1438 zfs_range_unlock(rl);
1439 return (error);
1440 }
1441
1442 zp->z_size = end;
1443 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1444 NULL, &zp->z_size, sizeof (zp->z_size));
1445
1446 if (end == 0) {
1447 zp->z_pflags &= ~ZFS_SPARSE;
1448 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1449 NULL, &zp->z_pflags, 8);
1450 }
1451 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1452
1453 dmu_tx_commit(tx);
1454
1455 zfs_range_unlock(rl);
1456
1457 return (0);
1458 }
1459
1460 /*
1461 * Free space in a file
1462 *
1463 * IN: zp - znode of file to free data in.
1464 * off - start of range
1465 * len - end of range (0 => EOF)
1466 * flag - current file open mode flags.
1467 * log - TRUE if this action should be logged
1468 *
1469 * RETURN: 0 on success, error code on failure
1470 */
1471 int
1472 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1473 {
1474 struct inode *ip = ZTOI(zp);
1475 dmu_tx_t *tx;
1476 zfs_sb_t *zsb = ZTOZSB(zp);
1477 zilog_t *zilog = zsb->z_log;
1478 uint64_t mode;
1479 uint64_t mtime[2], ctime[2];
1480 sa_bulk_attr_t bulk[3];
1481 int count = 0;
1482 int error;
1483
1484 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1485 sizeof (mode))) != 0)
1486 return (error);
1487
1488 if (off > zp->z_size) {
1489 error = zfs_extend(zp, off+len);
1490 if (error == 0 && log)
1491 goto log;
1492 else
1493 return (error);
1494 }
1495
1496 /*
1497 * Check for any locks in the region to be freed.
1498 */
1499 if (ip->i_flock && mandatory_lock(ip)) {
1500 uint64_t length = (len ? len : zp->z_size - off);
1501 if (!lock_may_write(ip, off, length))
1502 return (SET_ERROR(EAGAIN));
1503 }
1504
1505 if (len == 0) {
1506 error = zfs_trunc(zp, off);
1507 } else {
1508 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1509 off + len > zp->z_size)
1510 error = zfs_extend(zp, off+len);
1511 }
1512 if (error || !log)
1513 return (error);
1514 log:
1515 tx = dmu_tx_create(zsb->z_os);
1516 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1517 zfs_sa_upgrade_txholds(tx, zp);
1518 error = dmu_tx_assign(tx, TXG_WAIT);
1519 if (error) {
1520 dmu_tx_abort(tx);
1521 return (error);
1522 }
1523
1524 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1525 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1526 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1527 NULL, &zp->z_pflags, 8);
1528 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1529 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1530 ASSERT(error == 0);
1531
1532 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1533
1534 dmu_tx_commit(tx);
1535 zfs_inode_update(zp);
1536 return (0);
1537 }
1538
1539 void
1540 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1541 {
1542 struct super_block *sb;
1543 zfs_sb_t *zsb;
1544 uint64_t moid, obj, sa_obj, version;
1545 uint64_t sense = ZFS_CASE_SENSITIVE;
1546 uint64_t norm = 0;
1547 nvpair_t *elem;
1548 int error;
1549 int i;
1550 znode_t *rootzp = NULL;
1551 vattr_t vattr;
1552 znode_t *zp;
1553 zfs_acl_ids_t acl_ids;
1554
1555 /*
1556 * First attempt to create master node.
1557 */
1558 /*
1559 * In an empty objset, there are no blocks to read and thus
1560 * there can be no i/o errors (which we assert below).
1561 */
1562 moid = MASTER_NODE_OBJ;
1563 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1564 DMU_OT_NONE, 0, tx);
1565 ASSERT(error == 0);
1566
1567 /*
1568 * Set starting attributes.
1569 */
1570 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1571 elem = NULL;
1572 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1573 /* For the moment we expect all zpl props to be uint64_ts */
1574 uint64_t val;
1575 char *name;
1576
1577 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1578 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1579 name = nvpair_name(elem);
1580 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1581 if (val < version)
1582 version = val;
1583 } else {
1584 error = zap_update(os, moid, name, 8, 1, &val, tx);
1585 }
1586 ASSERT(error == 0);
1587 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1588 norm = val;
1589 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1590 sense = val;
1591 }
1592 ASSERT(version != 0);
1593 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1594
1595 /*
1596 * Create zap object used for SA attribute registration
1597 */
1598
1599 if (version >= ZPL_VERSION_SA) {
1600 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1601 DMU_OT_NONE, 0, tx);
1602 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1603 ASSERT(error == 0);
1604 } else {
1605 sa_obj = 0;
1606 }
1607 /*
1608 * Create a delete queue.
1609 */
1610 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1611
1612 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1613 ASSERT(error == 0);
1614
1615 /*
1616 * Create root znode. Create minimal znode/inode/zsb/sb
1617 * to allow zfs_mknode to work.
1618 */
1619 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1620 vattr.va_mode = S_IFDIR|0755;
1621 vattr.va_uid = crgetuid(cr);
1622 vattr.va_gid = crgetgid(cr);
1623
1624 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
1625 rootzp->z_moved = 0;
1626 rootzp->z_unlinked = 0;
1627 rootzp->z_atime_dirty = 0;
1628 rootzp->z_is_sa = USE_SA(version, os);
1629
1630 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
1631 zsb->z_os = os;
1632 zsb->z_parent = zsb;
1633 zsb->z_version = version;
1634 zsb->z_use_fuids = USE_FUIDS(version, os);
1635 zsb->z_use_sa = USE_SA(version, os);
1636 zsb->z_norm = norm;
1637
1638 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
1639 sb->s_fs_info = zsb;
1640
1641 ZTOI(rootzp)->i_sb = sb;
1642
1643 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1644 &zsb->z_attr_table);
1645
1646 ASSERT(error == 0);
1647
1648 /*
1649 * Fold case on file systems that are always or sometimes case
1650 * insensitive.
1651 */
1652 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1653 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1654
1655 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1656 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1657 offsetof(znode_t, z_link_node));
1658
1659 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1660 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1661
1662 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1663 cr, NULL, &acl_ids));
1664 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1665 ASSERT3P(zp, ==, rootzp);
1666 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1667 ASSERT(error == 0);
1668 zfs_acl_ids_free(&acl_ids);
1669
1670 atomic_set(&ZTOI(rootzp)->i_count, 0);
1671 sa_handle_destroy(rootzp->z_sa_hdl);
1672 kmem_cache_free(znode_cache, rootzp);
1673
1674 /*
1675 * Create shares directory
1676 */
1677 error = zfs_create_share_dir(zsb, tx);
1678 ASSERT(error == 0);
1679
1680 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1681 mutex_destroy(&zsb->z_hold_mtx[i]);
1682
1683 kmem_free(sb, sizeof (struct super_block));
1684 kmem_free(zsb, sizeof (zfs_sb_t));
1685 }
1686 #endif /* _KERNEL */
1687
1688 static int
1689 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1690 {
1691 uint64_t sa_obj = 0;
1692 int error;
1693
1694 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1695 if (error != 0 && error != ENOENT)
1696 return (error);
1697
1698 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1699 return (error);
1700 }
1701
1702 static int
1703 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1704 dmu_buf_t **db, void *tag)
1705 {
1706 dmu_object_info_t doi;
1707 int error;
1708
1709 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1710 return (error);
1711
1712 dmu_object_info_from_db(*db, &doi);
1713 if ((doi.doi_bonus_type != DMU_OT_SA &&
1714 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1715 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1716 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1717 sa_buf_rele(*db, tag);
1718 return (SET_ERROR(ENOTSUP));
1719 }
1720
1721 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1722 if (error != 0) {
1723 sa_buf_rele(*db, tag);
1724 return (error);
1725 }
1726
1727 return (0);
1728 }
1729
1730 void
1731 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1732 {
1733 sa_handle_destroy(hdl);
1734 sa_buf_rele(db, tag);
1735 }
1736
1737 /*
1738 * Given an object number, return its parent object number and whether
1739 * or not the object is an extended attribute directory.
1740 */
1741 static int
1742 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1743 int *is_xattrdir)
1744 {
1745 uint64_t parent;
1746 uint64_t pflags;
1747 uint64_t mode;
1748 sa_bulk_attr_t bulk[3];
1749 int count = 0;
1750 int error;
1751
1752 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1753 &parent, sizeof (parent));
1754 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1755 &pflags, sizeof (pflags));
1756 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1757 &mode, sizeof (mode));
1758
1759 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1760 return (error);
1761
1762 *pobjp = parent;
1763 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1764
1765 return (0);
1766 }
1767
1768 /*
1769 * Given an object number, return some zpl level statistics
1770 */
1771 static int
1772 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1773 zfs_stat_t *sb)
1774 {
1775 sa_bulk_attr_t bulk[4];
1776 int count = 0;
1777
1778 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1779 &sb->zs_mode, sizeof (sb->zs_mode));
1780 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1781 &sb->zs_gen, sizeof (sb->zs_gen));
1782 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1783 &sb->zs_links, sizeof (sb->zs_links));
1784 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1785 &sb->zs_ctime, sizeof (sb->zs_ctime));
1786
1787 return (sa_bulk_lookup(hdl, bulk, count));
1788 }
1789
1790 static int
1791 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1792 sa_attr_type_t *sa_table, char *buf, int len)
1793 {
1794 sa_handle_t *sa_hdl;
1795 sa_handle_t *prevhdl = NULL;
1796 dmu_buf_t *prevdb = NULL;
1797 dmu_buf_t *sa_db = NULL;
1798 char *path = buf + len - 1;
1799 int error;
1800
1801 *path = '\0';
1802 sa_hdl = hdl;
1803
1804 for (;;) {
1805 uint64_t pobj = 0;
1806 char component[MAXNAMELEN + 2];
1807 size_t complen;
1808 int is_xattrdir = 0;
1809
1810 if (prevdb)
1811 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1812
1813 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1814 &is_xattrdir)) != 0)
1815 break;
1816
1817 if (pobj == obj) {
1818 if (path[0] != '/')
1819 *--path = '/';
1820 break;
1821 }
1822
1823 component[0] = '/';
1824 if (is_xattrdir) {
1825 (void) sprintf(component + 1, "<xattrdir>");
1826 } else {
1827 error = zap_value_search(osp, pobj, obj,
1828 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1829 if (error != 0)
1830 break;
1831 }
1832
1833 complen = strlen(component);
1834 path -= complen;
1835 ASSERT(path >= buf);
1836 bcopy(component, path, complen);
1837 obj = pobj;
1838
1839 if (sa_hdl != hdl) {
1840 prevhdl = sa_hdl;
1841 prevdb = sa_db;
1842 }
1843 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1844 if (error != 0) {
1845 sa_hdl = prevhdl;
1846 sa_db = prevdb;
1847 break;
1848 }
1849 }
1850
1851 if (sa_hdl != NULL && sa_hdl != hdl) {
1852 ASSERT(sa_db != NULL);
1853 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1854 }
1855
1856 if (error == 0)
1857 (void) memmove(buf, path, buf + len - path);
1858
1859 return (error);
1860 }
1861
1862 int
1863 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1864 {
1865 sa_attr_type_t *sa_table;
1866 sa_handle_t *hdl;
1867 dmu_buf_t *db;
1868 int error;
1869
1870 error = zfs_sa_setup(osp, &sa_table);
1871 if (error != 0)
1872 return (error);
1873
1874 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1875 if (error != 0)
1876 return (error);
1877
1878 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1879
1880 zfs_release_sa_handle(hdl, db, FTAG);
1881 return (error);
1882 }
1883
1884 int
1885 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1886 char *buf, int len)
1887 {
1888 char *path = buf + len - 1;
1889 sa_attr_type_t *sa_table;
1890 sa_handle_t *hdl;
1891 dmu_buf_t *db;
1892 int error;
1893
1894 *path = '\0';
1895
1896 error = zfs_sa_setup(osp, &sa_table);
1897 if (error != 0)
1898 return (error);
1899
1900 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1901 if (error != 0)
1902 return (error);
1903
1904 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1905 if (error != 0) {
1906 zfs_release_sa_handle(hdl, db, FTAG);
1907 return (error);
1908 }
1909
1910 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1911
1912 zfs_release_sa_handle(hdl, db, FTAG);
1913 return (error);
1914 }
1915
1916 #if defined(_KERNEL) && defined(HAVE_SPL)
1917 EXPORT_SYMBOL(zfs_create_fs);
1918 EXPORT_SYMBOL(zfs_obj_to_path);
1919 #endif