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