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