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