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