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