]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_znode.c
Add zfs_iput_async() interface
[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 zfs_iput_async(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 bonuslen;
562 sa_handle_t *sa_hdl;
563 dmu_object_type_t obj_type;
564 sa_bulk_attr_t *sa_attrs;
565 int cnt = 0;
566 zfs_acl_locator_cb_t locate = { 0 };
567
568 if (zsb->z_replay) {
569 obj = vap->va_nodeid;
570 now = vap->va_ctime; /* see zfs_replay_create() */
571 gen = vap->va_nblocks; /* ditto */
572 } else {
573 obj = 0;
574 gethrestime(&now);
575 gen = dmu_tx_get_txg(tx);
576 }
577
578 obj_type = zsb->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
579 bonuslen = (obj_type == DMU_OT_SA) ?
580 DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
581
582 /*
583 * Create a new DMU object.
584 */
585 /*
586 * There's currently no mechanism for pre-reading the blocks that will
587 * be needed to allocate a new object, so we accept the small chance
588 * that there will be an i/o error and we will fail one of the
589 * assertions below.
590 */
591 if (S_ISDIR(vap->va_mode)) {
592 if (zsb->z_replay) {
593 VERIFY0(zap_create_claim_norm(zsb->z_os, obj,
594 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
595 obj_type, bonuslen, tx));
596 } else {
597 obj = zap_create_norm(zsb->z_os,
598 zsb->z_norm, DMU_OT_DIRECTORY_CONTENTS,
599 obj_type, bonuslen, tx);
600 }
601 } else {
602 if (zsb->z_replay) {
603 VERIFY0(dmu_object_claim(zsb->z_os, obj,
604 DMU_OT_PLAIN_FILE_CONTENTS, 0,
605 obj_type, bonuslen, tx));
606 } else {
607 obj = dmu_object_alloc(zsb->z_os,
608 DMU_OT_PLAIN_FILE_CONTENTS, 0,
609 obj_type, bonuslen, tx);
610 }
611 }
612
613 ZFS_OBJ_HOLD_ENTER(zsb, obj);
614 VERIFY(0 == sa_buf_hold(zsb->z_os, obj, NULL, &db));
615
616 /*
617 * If this is the root, fix up the half-initialized parent pointer
618 * to reference the just-allocated physical data area.
619 */
620 if (flag & IS_ROOT_NODE) {
621 dzp->z_id = obj;
622 } else {
623 dzp_pflags = dzp->z_pflags;
624 }
625
626 /*
627 * If parent is an xattr, so am I.
628 */
629 if (dzp_pflags & ZFS_XATTR) {
630 flag |= IS_XATTR;
631 }
632
633 if (zsb->z_use_fuids)
634 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
635 else
636 pflags = 0;
637
638 if (S_ISDIR(vap->va_mode)) {
639 size = 2; /* contents ("." and "..") */
640 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
641 } else {
642 size = links = 0;
643 }
644
645 if (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))
646 rdev = vap->va_rdev;
647
648 parent = dzp->z_id;
649 mode = acl_ids->z_mode;
650 if (flag & IS_XATTR)
651 pflags |= ZFS_XATTR;
652
653 /*
654 * No execs denied will be deterimed when zfs_mode_compute() is called.
655 */
656 pflags |= acl_ids->z_aclp->z_hints &
657 (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
658 ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
659
660 ZFS_TIME_ENCODE(&now, crtime);
661 ZFS_TIME_ENCODE(&now, ctime);
662
663 if (vap->va_mask & ATTR_ATIME) {
664 ZFS_TIME_ENCODE(&vap->va_atime, atime);
665 } else {
666 ZFS_TIME_ENCODE(&now, atime);
667 }
668
669 if (vap->va_mask & ATTR_MTIME) {
670 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
671 } else {
672 ZFS_TIME_ENCODE(&now, mtime);
673 }
674
675 /* Now add in all of the "SA" attributes */
676 VERIFY(0 == sa_handle_get_from_db(zsb->z_os, db, NULL, SA_HDL_SHARED,
677 &sa_hdl));
678
679 /*
680 * Setup the array of attributes to be replaced/set on the new file
681 *
682 * order for DMU_OT_ZNODE is critical since it needs to be constructed
683 * in the old znode_phys_t format. Don't change this ordering
684 */
685 sa_attrs = kmem_alloc(sizeof (sa_bulk_attr_t) * ZPL_END, KM_PUSHPAGE);
686
687 if (obj_type == DMU_OT_ZNODE) {
688 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
689 NULL, &atime, 16);
690 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
691 NULL, &mtime, 16);
692 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
693 NULL, &ctime, 16);
694 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
695 NULL, &crtime, 16);
696 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
697 NULL, &gen, 8);
698 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
699 NULL, &mode, 8);
700 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
701 NULL, &size, 8);
702 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
703 NULL, &parent, 8);
704 } else {
705 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zsb),
706 NULL, &mode, 8);
707 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zsb),
708 NULL, &size, 8);
709 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zsb),
710 NULL, &gen, 8);
711 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb),
712 NULL, &acl_ids->z_fuid, 8);
713 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb),
714 NULL, &acl_ids->z_fgid, 8);
715 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zsb),
716 NULL, &parent, 8);
717 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
718 NULL, &pflags, 8);
719 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zsb),
720 NULL, &atime, 16);
721 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zsb),
722 NULL, &mtime, 16);
723 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zsb),
724 NULL, &ctime, 16);
725 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zsb),
726 NULL, &crtime, 16);
727 }
728
729 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zsb), NULL, &links, 8);
730
731 if (obj_type == DMU_OT_ZNODE) {
732 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zsb), NULL,
733 &empty_xattr, 8);
734 }
735 if (obj_type == DMU_OT_ZNODE ||
736 (S_ISBLK(vap->va_mode) || S_ISCHR(vap->va_mode))) {
737 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zsb),
738 NULL, &rdev, 8);
739 }
740 if (obj_type == DMU_OT_ZNODE) {
741 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zsb),
742 NULL, &pflags, 8);
743 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zsb), NULL,
744 &acl_ids->z_fuid, 8);
745 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zsb), NULL,
746 &acl_ids->z_fgid, 8);
747 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zsb), NULL, pad,
748 sizeof (uint64_t) * 4);
749 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zsb), NULL,
750 &acl_phys, sizeof (zfs_acl_phys_t));
751 } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
752 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zsb), NULL,
753 &acl_ids->z_aclp->z_acl_count, 8);
754 locate.cb_aclp = acl_ids->z_aclp;
755 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zsb),
756 zfs_acl_data_locator, &locate,
757 acl_ids->z_aclp->z_acl_bytes);
758 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
759 acl_ids->z_fuid, acl_ids->z_fgid);
760 }
761
762 VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
763
764 if (!(flag & IS_ROOT_NODE)) {
765 *zpp = zfs_znode_alloc(zsb, db, 0, obj_type, obj, sa_hdl,
766 ZTOI(dzp));
767 VERIFY(*zpp != NULL);
768 VERIFY(dzp != NULL);
769 } else {
770 /*
771 * If we are creating the root node, the "parent" we
772 * passed in is the znode for the root.
773 */
774 *zpp = dzp;
775
776 (*zpp)->z_sa_hdl = sa_hdl;
777 }
778
779 (*zpp)->z_pflags = pflags;
780 (*zpp)->z_mode = mode;
781
782 if (obj_type == DMU_OT_ZNODE ||
783 acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
784 VERIFY0(zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
785 }
786 kmem_free(sa_attrs, sizeof (sa_bulk_attr_t) * ZPL_END);
787 ZFS_OBJ_HOLD_EXIT(zsb, obj);
788 }
789
790 /*
791 * Update in-core attributes. It is assumed the caller will be doing an
792 * sa_bulk_update to push the changes out.
793 */
794 void
795 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
796 {
797 xoptattr_t *xoap;
798
799 xoap = xva_getxoptattr(xvap);
800 ASSERT(xoap);
801
802 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
803 uint64_t times[2];
804 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
805 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(ZTOZSB(zp)),
806 &times, sizeof (times), tx);
807 XVA_SET_RTN(xvap, XAT_CREATETIME);
808 }
809 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
810 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
811 zp->z_pflags, tx);
812 XVA_SET_RTN(xvap, XAT_READONLY);
813 }
814 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
815 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
816 zp->z_pflags, tx);
817 XVA_SET_RTN(xvap, XAT_HIDDEN);
818 }
819 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
820 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
821 zp->z_pflags, tx);
822 XVA_SET_RTN(xvap, XAT_SYSTEM);
823 }
824 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
825 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
826 zp->z_pflags, tx);
827 XVA_SET_RTN(xvap, XAT_ARCHIVE);
828 }
829 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
830 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
831 zp->z_pflags, tx);
832 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
833 }
834 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
835 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
836 zp->z_pflags, tx);
837 XVA_SET_RTN(xvap, XAT_NOUNLINK);
838 }
839 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
840 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
841 zp->z_pflags, tx);
842 XVA_SET_RTN(xvap, XAT_APPENDONLY);
843 }
844 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
845 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
846 zp->z_pflags, tx);
847 XVA_SET_RTN(xvap, XAT_NODUMP);
848 }
849 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
850 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
851 zp->z_pflags, tx);
852 XVA_SET_RTN(xvap, XAT_OPAQUE);
853 }
854 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
855 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
856 xoap->xoa_av_quarantined, zp->z_pflags, tx);
857 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
858 }
859 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
860 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
861 zp->z_pflags, tx);
862 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
863 }
864 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
865 zfs_sa_set_scanstamp(zp, xvap, tx);
866 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
867 }
868 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
869 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
870 zp->z_pflags, tx);
871 XVA_SET_RTN(xvap, XAT_REPARSE);
872 }
873 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
874 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
875 zp->z_pflags, tx);
876 XVA_SET_RTN(xvap, XAT_OFFLINE);
877 }
878 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
879 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
880 zp->z_pflags, tx);
881 XVA_SET_RTN(xvap, XAT_SPARSE);
882 }
883 }
884
885 int
886 zfs_zget(zfs_sb_t *zsb, uint64_t obj_num, znode_t **zpp)
887 {
888 dmu_object_info_t doi;
889 dmu_buf_t *db;
890 znode_t *zp;
891 int err;
892 sa_handle_t *hdl;
893
894 *zpp = NULL;
895
896 again:
897 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
898
899 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
900 if (err) {
901 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
902 return (err);
903 }
904
905 dmu_object_info_from_db(db, &doi);
906 if (doi.doi_bonus_type != DMU_OT_SA &&
907 (doi.doi_bonus_type != DMU_OT_ZNODE ||
908 (doi.doi_bonus_type == DMU_OT_ZNODE &&
909 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
910 sa_buf_rele(db, NULL);
911 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
912 return (SET_ERROR(EINVAL));
913 }
914
915 hdl = dmu_buf_get_user(db);
916 if (hdl != NULL) {
917 zp = sa_get_userdata(hdl);
918
919
920 /*
921 * Since "SA" does immediate eviction we
922 * should never find a sa handle that doesn't
923 * know about the znode.
924 */
925
926 ASSERT3P(zp, !=, NULL);
927
928 mutex_enter(&zp->z_lock);
929 ASSERT3U(zp->z_id, ==, obj_num);
930 if (zp->z_unlinked) {
931 err = SET_ERROR(ENOENT);
932 } else {
933 /*
934 * If igrab() returns NULL the VFS has independently
935 * determined the inode should be evicted and has
936 * called iput_final() to start the eviction process.
937 * The SA handle is still valid but because the VFS
938 * requires that the eviction succeed we must drop
939 * our locks and references to allow the eviction to
940 * complete. The zfs_zget() may then be retried.
941 *
942 * This unlikely case could be optimized by registering
943 * a sops->drop_inode() callback. The callback would
944 * need to detect the active SA hold thereby informing
945 * the VFS that this inode should not be evicted.
946 */
947 if (igrab(ZTOI(zp)) == NULL) {
948 mutex_exit(&zp->z_lock);
949 sa_buf_rele(db, NULL);
950 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
951 goto again;
952 }
953 *zpp = zp;
954 err = 0;
955 }
956 mutex_exit(&zp->z_lock);
957 sa_buf_rele(db, NULL);
958 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
959 return (err);
960 }
961
962 /*
963 * Not found create new znode/vnode but only if file exists.
964 *
965 * There is a small window where zfs_vget() could
966 * find this object while a file create is still in
967 * progress. This is checked for in zfs_znode_alloc()
968 *
969 * if zfs_znode_alloc() fails it will drop the hold on the
970 * bonus buffer.
971 */
972 zp = zfs_znode_alloc(zsb, db, doi.doi_data_block_size,
973 doi.doi_bonus_type, obj_num, NULL, NULL);
974 if (zp == NULL) {
975 err = SET_ERROR(ENOENT);
976 } else {
977 *zpp = zp;
978 }
979 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
980 return (err);
981 }
982
983 int
984 zfs_rezget(znode_t *zp)
985 {
986 zfs_sb_t *zsb = ZTOZSB(zp);
987 dmu_object_info_t doi;
988 dmu_buf_t *db;
989 uint64_t obj_num = zp->z_id;
990 uint64_t mode;
991 sa_bulk_attr_t bulk[8];
992 int err;
993 int count = 0;
994 uint64_t gen;
995
996 ZFS_OBJ_HOLD_ENTER(zsb, obj_num);
997
998 mutex_enter(&zp->z_acl_lock);
999 if (zp->z_acl_cached) {
1000 zfs_acl_free(zp->z_acl_cached);
1001 zp->z_acl_cached = NULL;
1002 }
1003 mutex_exit(&zp->z_acl_lock);
1004
1005 rw_enter(&zp->z_xattr_lock, RW_WRITER);
1006 if (zp->z_xattr_cached) {
1007 nvlist_free(zp->z_xattr_cached);
1008 zp->z_xattr_cached = NULL;
1009 }
1010
1011 if (zp->z_xattr_parent) {
1012 iput(ZTOI(zp->z_xattr_parent));
1013 zp->z_xattr_parent = NULL;
1014 }
1015 rw_exit(&zp->z_xattr_lock);
1016
1017 ASSERT(zp->z_sa_hdl == NULL);
1018 err = sa_buf_hold(zsb->z_os, obj_num, NULL, &db);
1019 if (err) {
1020 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1021 return (err);
1022 }
1023
1024 dmu_object_info_from_db(db, &doi);
1025 if (doi.doi_bonus_type != DMU_OT_SA &&
1026 (doi.doi_bonus_type != DMU_OT_ZNODE ||
1027 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1028 doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1029 sa_buf_rele(db, NULL);
1030 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1031 return (SET_ERROR(EINVAL));
1032 }
1033
1034 zfs_znode_sa_init(zsb, zp, db, doi.doi_bonus_type, NULL);
1035
1036 /* reload cached values */
1037 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zsb), NULL,
1038 &gen, sizeof (gen));
1039 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
1040 &zp->z_size, sizeof (zp->z_size));
1041 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
1042 &zp->z_links, sizeof (zp->z_links));
1043 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
1044 &zp->z_pflags, sizeof (zp->z_pflags));
1045 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL,
1046 &zp->z_atime, sizeof (zp->z_atime));
1047 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL,
1048 &zp->z_uid, sizeof (zp->z_uid));
1049 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), NULL,
1050 &zp->z_gid, sizeof (zp->z_gid));
1051 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL,
1052 &mode, sizeof (mode));
1053
1054 if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1055 zfs_znode_dmu_fini(zp);
1056 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1057 return (SET_ERROR(EIO));
1058 }
1059
1060 zp->z_mode = mode;
1061
1062 if (gen != zp->z_gen) {
1063 zfs_znode_dmu_fini(zp);
1064 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1065 return (SET_ERROR(EIO));
1066 }
1067
1068 zp->z_unlinked = (zp->z_links == 0);
1069 zp->z_blksz = doi.doi_data_block_size;
1070 zfs_inode_update(zp);
1071
1072 ZFS_OBJ_HOLD_EXIT(zsb, obj_num);
1073
1074 return (0);
1075 }
1076
1077 void
1078 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1079 {
1080 zfs_sb_t *zsb = ZTOZSB(zp);
1081 objset_t *os = zsb->z_os;
1082 uint64_t obj = zp->z_id;
1083 uint64_t acl_obj = zfs_external_acl(zp);
1084
1085 ZFS_OBJ_HOLD_ENTER(zsb, obj);
1086 if (acl_obj) {
1087 VERIFY(!zp->z_is_sa);
1088 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1089 }
1090 VERIFY(0 == dmu_object_free(os, obj, tx));
1091 zfs_znode_dmu_fini(zp);
1092 ZFS_OBJ_HOLD_EXIT(zsb, obj);
1093 }
1094
1095 void
1096 zfs_zinactive(znode_t *zp)
1097 {
1098 zfs_sb_t *zsb = ZTOZSB(zp);
1099 uint64_t z_id = zp->z_id;
1100 boolean_t drop_mutex = 0;
1101
1102 ASSERT(zp->z_sa_hdl);
1103
1104 /*
1105 * Don't allow a zfs_zget() while were trying to release this znode.
1106 *
1107 * Linux allows direct memory reclaim which means that any KM_SLEEP
1108 * allocation may trigger inode eviction. This can lead to a deadlock
1109 * through the ->shrink_icache_memory()->evict()->zfs_inactive()->
1110 * zfs_zinactive() call path. To avoid this deadlock the process
1111 * must not reacquire the mutex when it is already holding it.
1112 */
1113 if (!ZFS_OBJ_HOLD_OWNED(zsb, z_id)) {
1114 ZFS_OBJ_HOLD_ENTER(zsb, z_id);
1115 drop_mutex = 1;
1116 }
1117
1118 mutex_enter(&zp->z_lock);
1119
1120 /*
1121 * If this was the last reference to a file with no links,
1122 * remove the file from the file system.
1123 */
1124 if (zp->z_unlinked) {
1125 mutex_exit(&zp->z_lock);
1126
1127 if (drop_mutex)
1128 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1129
1130 zfs_rmnode(zp);
1131 return;
1132 }
1133
1134 mutex_exit(&zp->z_lock);
1135 zfs_znode_dmu_fini(zp);
1136
1137 if (drop_mutex)
1138 ZFS_OBJ_HOLD_EXIT(zsb, z_id);
1139 }
1140
1141 static inline int
1142 zfs_compare_timespec(struct timespec *t1, struct timespec *t2)
1143 {
1144 if (t1->tv_sec < t2->tv_sec)
1145 return (-1);
1146
1147 if (t1->tv_sec > t2->tv_sec)
1148 return (1);
1149
1150 return (t1->tv_nsec - t2->tv_nsec);
1151 }
1152
1153 /*
1154 * Determine whether the znode's atime must be updated. The logic mostly
1155 * duplicates the Linux kernel's relatime_need_update() functionality.
1156 * This function is only called if the underlying filesystem actually has
1157 * atime updates enabled.
1158 */
1159 static inline boolean_t
1160 zfs_atime_need_update(znode_t *zp, timestruc_t *now)
1161 {
1162 if (!ZTOZSB(zp)->z_relatime)
1163 return (B_TRUE);
1164
1165 /*
1166 * In relatime mode, only update the atime if the previous atime
1167 * is earlier than either the ctime or mtime or if at least a day
1168 * has passed since the last update of atime.
1169 */
1170 if (zfs_compare_timespec(&ZTOI(zp)->i_mtime, &ZTOI(zp)->i_atime) >= 0)
1171 return (B_TRUE);
1172
1173 if (zfs_compare_timespec(&ZTOI(zp)->i_ctime, &ZTOI(zp)->i_atime) >= 0)
1174 return (B_TRUE);
1175
1176 if ((long)now->tv_sec - ZTOI(zp)->i_atime.tv_sec >= 24*60*60)
1177 return (B_TRUE);
1178
1179 return (B_FALSE);
1180 }
1181
1182 /*
1183 * Prepare to update znode time stamps.
1184 *
1185 * IN: zp - znode requiring timestamp update
1186 * flag - ATTR_MTIME, ATTR_CTIME, ATTR_ATIME flags
1187 * have_tx - true of caller is creating a new txg
1188 *
1189 * OUT: zp - new atime (via underlying inode's i_atime)
1190 * mtime - new mtime
1191 * ctime - new ctime
1192 *
1193 * NOTE: The arguments are somewhat redundant. The following condition
1194 * is always true:
1195 *
1196 * have_tx == !(flag & ATTR_ATIME)
1197 */
1198 void
1199 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1200 uint64_t ctime[2], boolean_t have_tx)
1201 {
1202 timestruc_t now;
1203
1204 ASSERT(have_tx == !(flag & ATTR_ATIME));
1205 gethrestime(&now);
1206
1207 /*
1208 * NOTE: The following test intentionally does not update z_atime_dirty
1209 * in the case where an ATIME update has been requested but for which
1210 * the update is omitted due to relatime logic. The rationale being
1211 * that if the flag was set somewhere else, we should leave it alone
1212 * here.
1213 */
1214 if (flag & ATTR_ATIME) {
1215 if (zfs_atime_need_update(zp, &now)) {
1216 ZFS_TIME_ENCODE(&now, zp->z_atime);
1217 ZTOI(zp)->i_atime.tv_sec = zp->z_atime[0];
1218 ZTOI(zp)->i_atime.tv_nsec = zp->z_atime[1];
1219 zp->z_atime_dirty = 1;
1220 }
1221 } else {
1222 zp->z_atime_dirty = 0;
1223 zp->z_seq++;
1224 }
1225
1226 if (flag & ATTR_MTIME) {
1227 ZFS_TIME_ENCODE(&now, mtime);
1228 if (ZTOZSB(zp)->z_use_fuids) {
1229 zp->z_pflags |= (ZFS_ARCHIVE |
1230 ZFS_AV_MODIFIED);
1231 }
1232 }
1233
1234 if (flag & ATTR_CTIME) {
1235 ZFS_TIME_ENCODE(&now, ctime);
1236 if (ZTOZSB(zp)->z_use_fuids)
1237 zp->z_pflags |= ZFS_ARCHIVE;
1238 }
1239 }
1240
1241 /*
1242 * Grow the block size for a file.
1243 *
1244 * IN: zp - znode of file to free data in.
1245 * size - requested block size
1246 * tx - open transaction.
1247 *
1248 * NOTE: this function assumes that the znode is write locked.
1249 */
1250 void
1251 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1252 {
1253 int error;
1254 u_longlong_t dummy;
1255
1256 if (size <= zp->z_blksz)
1257 return;
1258 /*
1259 * If the file size is already greater than the current blocksize,
1260 * we will not grow. If there is more than one block in a file,
1261 * the blocksize cannot change.
1262 */
1263 if (zp->z_blksz && zp->z_size > zp->z_blksz)
1264 return;
1265
1266 error = dmu_object_set_blocksize(ZTOZSB(zp)->z_os, zp->z_id,
1267 size, 0, tx);
1268
1269 if (error == ENOTSUP)
1270 return;
1271 ASSERT0(error);
1272
1273 /* What blocksize did we actually get? */
1274 dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1275 }
1276
1277 /*
1278 * Increase the file length
1279 *
1280 * IN: zp - znode of file to free data in.
1281 * end - new end-of-file
1282 *
1283 * RETURN: 0 on success, error code on failure
1284 */
1285 static int
1286 zfs_extend(znode_t *zp, uint64_t end)
1287 {
1288 zfs_sb_t *zsb = ZTOZSB(zp);
1289 dmu_tx_t *tx;
1290 rl_t *rl;
1291 uint64_t newblksz;
1292 int error;
1293
1294 /*
1295 * We will change zp_size, lock the whole file.
1296 */
1297 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1298
1299 /*
1300 * Nothing to do if file already at desired length.
1301 */
1302 if (end <= zp->z_size) {
1303 zfs_range_unlock(rl);
1304 return (0);
1305 }
1306 tx = dmu_tx_create(zsb->z_os);
1307 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1308 zfs_sa_upgrade_txholds(tx, zp);
1309 if (end > zp->z_blksz &&
1310 (!ISP2(zp->z_blksz) || zp->z_blksz < zsb->z_max_blksz)) {
1311 /*
1312 * We are growing the file past the current block size.
1313 */
1314 if (zp->z_blksz > ZTOZSB(zp)->z_max_blksz) {
1315 ASSERT(!ISP2(zp->z_blksz));
1316 newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1317 } else {
1318 newblksz = MIN(end, ZTOZSB(zp)->z_max_blksz);
1319 }
1320 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1321 } else {
1322 newblksz = 0;
1323 }
1324
1325 error = dmu_tx_assign(tx, TXG_WAIT);
1326 if (error) {
1327 dmu_tx_abort(tx);
1328 zfs_range_unlock(rl);
1329 return (error);
1330 }
1331
1332 if (newblksz)
1333 zfs_grow_blocksize(zp, newblksz, tx);
1334
1335 zp->z_size = end;
1336
1337 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(ZTOZSB(zp)),
1338 &zp->z_size, sizeof (zp->z_size), tx));
1339
1340 zfs_range_unlock(rl);
1341
1342 dmu_tx_commit(tx);
1343
1344 return (0);
1345 }
1346
1347 /*
1348 * Free space in a file.
1349 *
1350 * IN: zp - znode of file to free data in.
1351 * off - start of section to free.
1352 * len - length of section to free.
1353 *
1354 * RETURN: 0 on success, error code on failure
1355 */
1356 static int
1357 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1358 {
1359 zfs_sb_t *zsb = ZTOZSB(zp);
1360 rl_t *rl;
1361 int error;
1362
1363 /*
1364 * Lock the range being freed.
1365 */
1366 rl = zfs_range_lock(zp, off, len, RL_WRITER);
1367
1368 /*
1369 * Nothing to do if file already at desired length.
1370 */
1371 if (off >= zp->z_size) {
1372 zfs_range_unlock(rl);
1373 return (0);
1374 }
1375
1376 if (off + len > zp->z_size)
1377 len = zp->z_size - off;
1378
1379 error = dmu_free_long_range(zsb->z_os, zp->z_id, off, len);
1380
1381 zfs_range_unlock(rl);
1382
1383 return (error);
1384 }
1385
1386 /*
1387 * Truncate a file
1388 *
1389 * IN: zp - znode of file to free data in.
1390 * end - new end-of-file.
1391 *
1392 * RETURN: 0 on success, error code on failure
1393 */
1394 static int
1395 zfs_trunc(znode_t *zp, uint64_t end)
1396 {
1397 zfs_sb_t *zsb = ZTOZSB(zp);
1398 dmu_tx_t *tx;
1399 rl_t *rl;
1400 int error;
1401 sa_bulk_attr_t bulk[2];
1402 int count = 0;
1403
1404 /*
1405 * We will change zp_size, lock the whole file.
1406 */
1407 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1408
1409 /*
1410 * Nothing to do if file already at desired length.
1411 */
1412 if (end >= zp->z_size) {
1413 zfs_range_unlock(rl);
1414 return (0);
1415 }
1416
1417 error = dmu_free_long_range(zsb->z_os, zp->z_id, end, -1);
1418 if (error) {
1419 zfs_range_unlock(rl);
1420 return (error);
1421 }
1422 tx = dmu_tx_create(zsb->z_os);
1423 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1424 zfs_sa_upgrade_txholds(tx, zp);
1425 error = dmu_tx_assign(tx, TXG_WAIT);
1426 if (error) {
1427 dmu_tx_abort(tx);
1428 zfs_range_unlock(rl);
1429 return (error);
1430 }
1431
1432 zp->z_size = end;
1433 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
1434 NULL, &zp->z_size, sizeof (zp->z_size));
1435
1436 if (end == 0) {
1437 zp->z_pflags &= ~ZFS_SPARSE;
1438 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1439 NULL, &zp->z_pflags, 8);
1440 }
1441 VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1442
1443 dmu_tx_commit(tx);
1444
1445 zfs_range_unlock(rl);
1446
1447 return (0);
1448 }
1449
1450 /*
1451 * Free space in a file
1452 *
1453 * IN: zp - znode of file to free data in.
1454 * off - start of range
1455 * len - end of range (0 => EOF)
1456 * flag - current file open mode flags.
1457 * log - TRUE if this action should be logged
1458 *
1459 * RETURN: 0 on success, error code on failure
1460 */
1461 int
1462 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1463 {
1464 struct inode *ip = ZTOI(zp);
1465 dmu_tx_t *tx;
1466 zfs_sb_t *zsb = ZTOZSB(zp);
1467 zilog_t *zilog = zsb->z_log;
1468 uint64_t mode;
1469 uint64_t mtime[2], ctime[2];
1470 sa_bulk_attr_t bulk[3];
1471 int count = 0;
1472 int error;
1473
1474 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zsb), &mode,
1475 sizeof (mode))) != 0)
1476 return (error);
1477
1478 if (off > zp->z_size) {
1479 error = zfs_extend(zp, off+len);
1480 if (error == 0 && log)
1481 goto log;
1482 else
1483 return (error);
1484 }
1485
1486 /*
1487 * Check for any locks in the region to be freed.
1488 */
1489 if (ip->i_flock && mandatory_lock(ip)) {
1490 uint64_t length = (len ? len : zp->z_size - off);
1491 if (!lock_may_write(ip, off, length))
1492 return (SET_ERROR(EAGAIN));
1493 }
1494
1495 if (len == 0) {
1496 error = zfs_trunc(zp, off);
1497 } else {
1498 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1499 off + len > zp->z_size)
1500 error = zfs_extend(zp, off+len);
1501 }
1502 if (error || !log)
1503 return (error);
1504 log:
1505 tx = dmu_tx_create(zsb->z_os);
1506 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1507 zfs_sa_upgrade_txholds(tx, zp);
1508 error = dmu_tx_assign(tx, TXG_WAIT);
1509 if (error) {
1510 dmu_tx_abort(tx);
1511 return (error);
1512 }
1513
1514 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, mtime, 16);
1515 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, ctime, 16);
1516 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
1517 NULL, &zp->z_pflags, 8);
1518 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1519 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1520 ASSERT(error == 0);
1521
1522 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1523
1524 dmu_tx_commit(tx);
1525 zfs_inode_update(zp);
1526 return (0);
1527 }
1528
1529 void
1530 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1531 {
1532 struct super_block *sb;
1533 zfs_sb_t *zsb;
1534 uint64_t moid, obj, sa_obj, version;
1535 uint64_t sense = ZFS_CASE_SENSITIVE;
1536 uint64_t norm = 0;
1537 nvpair_t *elem;
1538 int error;
1539 int i;
1540 znode_t *rootzp = NULL;
1541 vattr_t vattr;
1542 znode_t *zp;
1543 zfs_acl_ids_t acl_ids;
1544
1545 /*
1546 * First attempt to create master node.
1547 */
1548 /*
1549 * In an empty objset, there are no blocks to read and thus
1550 * there can be no i/o errors (which we assert below).
1551 */
1552 moid = MASTER_NODE_OBJ;
1553 error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1554 DMU_OT_NONE, 0, tx);
1555 ASSERT(error == 0);
1556
1557 /*
1558 * Set starting attributes.
1559 */
1560 version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1561 elem = NULL;
1562 while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1563 /* For the moment we expect all zpl props to be uint64_ts */
1564 uint64_t val;
1565 char *name;
1566
1567 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1568 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1569 name = nvpair_name(elem);
1570 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1571 if (val < version)
1572 version = val;
1573 } else {
1574 error = zap_update(os, moid, name, 8, 1, &val, tx);
1575 }
1576 ASSERT(error == 0);
1577 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1578 norm = val;
1579 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1580 sense = val;
1581 }
1582 ASSERT(version != 0);
1583 error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1584
1585 /*
1586 * Create zap object used for SA attribute registration
1587 */
1588
1589 if (version >= ZPL_VERSION_SA) {
1590 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1591 DMU_OT_NONE, 0, tx);
1592 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1593 ASSERT(error == 0);
1594 } else {
1595 sa_obj = 0;
1596 }
1597 /*
1598 * Create a delete queue.
1599 */
1600 obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1601
1602 error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1603 ASSERT(error == 0);
1604
1605 /*
1606 * Create root znode. Create minimal znode/inode/zsb/sb
1607 * to allow zfs_mknode to work.
1608 */
1609 vattr.va_mask = ATTR_MODE|ATTR_UID|ATTR_GID;
1610 vattr.va_mode = S_IFDIR|0755;
1611 vattr.va_uid = crgetuid(cr);
1612 vattr.va_gid = crgetgid(cr);
1613
1614 rootzp = kmem_cache_alloc(znode_cache, KM_PUSHPAGE);
1615 rootzp->z_moved = 0;
1616 rootzp->z_unlinked = 0;
1617 rootzp->z_atime_dirty = 0;
1618 rootzp->z_is_sa = USE_SA(version, os);
1619
1620 zsb = kmem_zalloc(sizeof (zfs_sb_t), KM_PUSHPAGE | KM_NODEBUG);
1621 zsb->z_os = os;
1622 zsb->z_parent = zsb;
1623 zsb->z_version = version;
1624 zsb->z_use_fuids = USE_FUIDS(version, os);
1625 zsb->z_use_sa = USE_SA(version, os);
1626 zsb->z_norm = norm;
1627
1628 sb = kmem_zalloc(sizeof (struct super_block), KM_PUSHPAGE);
1629 sb->s_fs_info = zsb;
1630
1631 ZTOI(rootzp)->i_sb = sb;
1632
1633 error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1634 &zsb->z_attr_table);
1635
1636 ASSERT(error == 0);
1637
1638 /*
1639 * Fold case on file systems that are always or sometimes case
1640 * insensitive.
1641 */
1642 if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1643 zsb->z_norm |= U8_TEXTPREP_TOUPPER;
1644
1645 mutex_init(&zsb->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1646 list_create(&zsb->z_all_znodes, sizeof (znode_t),
1647 offsetof(znode_t, z_link_node));
1648
1649 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1650 mutex_init(&zsb->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1651
1652 VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1653 cr, NULL, &acl_ids));
1654 zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1655 ASSERT3P(zp, ==, rootzp);
1656 error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1657 ASSERT(error == 0);
1658 zfs_acl_ids_free(&acl_ids);
1659
1660 atomic_set(&ZTOI(rootzp)->i_count, 0);
1661 sa_handle_destroy(rootzp->z_sa_hdl);
1662 kmem_cache_free(znode_cache, rootzp);
1663
1664 /*
1665 * Create shares directory
1666 */
1667 error = zfs_create_share_dir(zsb, tx);
1668 ASSERT(error == 0);
1669
1670 for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1671 mutex_destroy(&zsb->z_hold_mtx[i]);
1672
1673 kmem_free(sb, sizeof (struct super_block));
1674 kmem_free(zsb, sizeof (zfs_sb_t));
1675 }
1676 #endif /* _KERNEL */
1677
1678 static int
1679 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1680 {
1681 uint64_t sa_obj = 0;
1682 int error;
1683
1684 error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1685 if (error != 0 && error != ENOENT)
1686 return (error);
1687
1688 error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1689 return (error);
1690 }
1691
1692 static int
1693 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1694 dmu_buf_t **db, void *tag)
1695 {
1696 dmu_object_info_t doi;
1697 int error;
1698
1699 if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1700 return (error);
1701
1702 dmu_object_info_from_db(*db, &doi);
1703 if ((doi.doi_bonus_type != DMU_OT_SA &&
1704 doi.doi_bonus_type != DMU_OT_ZNODE) ||
1705 (doi.doi_bonus_type == DMU_OT_ZNODE &&
1706 doi.doi_bonus_size < sizeof (znode_phys_t))) {
1707 sa_buf_rele(*db, tag);
1708 return (SET_ERROR(ENOTSUP));
1709 }
1710
1711 error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
1712 if (error != 0) {
1713 sa_buf_rele(*db, tag);
1714 return (error);
1715 }
1716
1717 return (0);
1718 }
1719
1720 void
1721 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
1722 {
1723 sa_handle_destroy(hdl);
1724 sa_buf_rele(db, tag);
1725 }
1726
1727 /*
1728 * Given an object number, return its parent object number and whether
1729 * or not the object is an extended attribute directory.
1730 */
1731 static int
1732 zfs_obj_to_pobj(sa_handle_t *hdl, sa_attr_type_t *sa_table, uint64_t *pobjp,
1733 int *is_xattrdir)
1734 {
1735 uint64_t parent;
1736 uint64_t pflags;
1737 uint64_t mode;
1738 sa_bulk_attr_t bulk[3];
1739 int count = 0;
1740 int error;
1741
1742 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
1743 &parent, sizeof (parent));
1744 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
1745 &pflags, sizeof (pflags));
1746 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1747 &mode, sizeof (mode));
1748
1749 if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
1750 return (error);
1751
1752 *pobjp = parent;
1753 *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
1754
1755 return (0);
1756 }
1757
1758 /*
1759 * Given an object number, return some zpl level statistics
1760 */
1761 static int
1762 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
1763 zfs_stat_t *sb)
1764 {
1765 sa_bulk_attr_t bulk[4];
1766 int count = 0;
1767
1768 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
1769 &sb->zs_mode, sizeof (sb->zs_mode));
1770 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
1771 &sb->zs_gen, sizeof (sb->zs_gen));
1772 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
1773 &sb->zs_links, sizeof (sb->zs_links));
1774 SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
1775 &sb->zs_ctime, sizeof (sb->zs_ctime));
1776
1777 return (sa_bulk_lookup(hdl, bulk, count));
1778 }
1779
1780 static int
1781 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
1782 sa_attr_type_t *sa_table, char *buf, int len)
1783 {
1784 sa_handle_t *sa_hdl;
1785 sa_handle_t *prevhdl = NULL;
1786 dmu_buf_t *prevdb = NULL;
1787 dmu_buf_t *sa_db = NULL;
1788 char *path = buf + len - 1;
1789 int error;
1790
1791 *path = '\0';
1792 sa_hdl = hdl;
1793
1794 for (;;) {
1795 uint64_t pobj = 0;
1796 char component[MAXNAMELEN + 2];
1797 size_t complen;
1798 int is_xattrdir = 0;
1799
1800 if (prevdb)
1801 zfs_release_sa_handle(prevhdl, prevdb, FTAG);
1802
1803 if ((error = zfs_obj_to_pobj(sa_hdl, sa_table, &pobj,
1804 &is_xattrdir)) != 0)
1805 break;
1806
1807 if (pobj == obj) {
1808 if (path[0] != '/')
1809 *--path = '/';
1810 break;
1811 }
1812
1813 component[0] = '/';
1814 if (is_xattrdir) {
1815 (void) sprintf(component + 1, "<xattrdir>");
1816 } else {
1817 error = zap_value_search(osp, pobj, obj,
1818 ZFS_DIRENT_OBJ(-1ULL), component + 1);
1819 if (error != 0)
1820 break;
1821 }
1822
1823 complen = strlen(component);
1824 path -= complen;
1825 ASSERT(path >= buf);
1826 bcopy(component, path, complen);
1827 obj = pobj;
1828
1829 if (sa_hdl != hdl) {
1830 prevhdl = sa_hdl;
1831 prevdb = sa_db;
1832 }
1833 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
1834 if (error != 0) {
1835 sa_hdl = prevhdl;
1836 sa_db = prevdb;
1837 break;
1838 }
1839 }
1840
1841 if (sa_hdl != NULL && sa_hdl != hdl) {
1842 ASSERT(sa_db != NULL);
1843 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
1844 }
1845
1846 if (error == 0)
1847 (void) memmove(buf, path, buf + len - path);
1848
1849 return (error);
1850 }
1851
1852 int
1853 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1854 {
1855 sa_attr_type_t *sa_table;
1856 sa_handle_t *hdl;
1857 dmu_buf_t *db;
1858 int error;
1859
1860 error = zfs_sa_setup(osp, &sa_table);
1861 if (error != 0)
1862 return (error);
1863
1864 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1865 if (error != 0)
1866 return (error);
1867
1868 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1869
1870 zfs_release_sa_handle(hdl, db, FTAG);
1871 return (error);
1872 }
1873
1874 int
1875 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
1876 char *buf, int len)
1877 {
1878 char *path = buf + len - 1;
1879 sa_attr_type_t *sa_table;
1880 sa_handle_t *hdl;
1881 dmu_buf_t *db;
1882 int error;
1883
1884 *path = '\0';
1885
1886 error = zfs_sa_setup(osp, &sa_table);
1887 if (error != 0)
1888 return (error);
1889
1890 error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
1891 if (error != 0)
1892 return (error);
1893
1894 error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
1895 if (error != 0) {
1896 zfs_release_sa_handle(hdl, db, FTAG);
1897 return (error);
1898 }
1899
1900 error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
1901
1902 zfs_release_sa_handle(hdl, db, FTAG);
1903 return (error);
1904 }
1905
1906 #if defined(_KERNEL) && defined(HAVE_SPL)
1907 EXPORT_SYMBOL(zfs_create_fs);
1908 EXPORT_SYMBOL(zfs_obj_to_path);
1909 #endif