]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_znode.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_znode.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2013 by Delphix. All rights reserved.
24  */
25
26 /* Portions Copyright 2007 Jeremy Teo */
27 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
28
29 #ifdef _KERNEL
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
36 #include <sys/mntent.h>
37 #include <sys/u8_textprep.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/vfs.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/atomic.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_rlock.h>
50 #include <sys/zfs_fuid.h>
51 #include <sys/dnode.h>
52 #include <sys/fs/zfs.h>
53 #include <sys/kidmap.h>
54 #endif /* _KERNEL */
55
56 #include <sys/dmu.h>
57 #include <sys/refcount.h>
58 #include <sys/stat.h>
59 #include <sys/zap.h>
60 #include <sys/zfs_znode.h>
61 #include <sys/sa.h>
62 #include <sys/zfs_sa.h>
63 #include <sys/zfs_stat.h>
64 #include <sys/refcount.h>
65
66 #include "zfs_prop.h"
67 #include "zfs_comutil.h"
68
69 /* Used by fstat(1). */
70 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
71     "sizeof(znode_t)");
72
73 /*
74  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
75  * turned on when DEBUG is also defined.
76  */
77 #ifdef  DEBUG
78 #define ZNODE_STATS
79 #endif  /* DEBUG */
80
81 #ifdef  ZNODE_STATS
82 #define ZNODE_STAT_ADD(stat)                    ((stat)++)
83 #else
84 #define ZNODE_STAT_ADD(stat)                    /* nothing */
85 #endif  /* ZNODE_STATS */
86
87 /*
88  * Functions needed for userland (ie: libzpool) are not put under
89  * #ifdef_KERNEL; the rest of the functions have dependencies
90  * (such as VFS logic) that will not compile easily in userland.
91  */
92 #ifdef _KERNEL
93 /*
94  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
95  * be freed before it can be safely accessed.
96  */
97 krwlock_t zfsvfs_lock;
98
99 static kmem_cache_t *znode_cache = NULL;
100
101 /*ARGSUSED*/
102 static void
103 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
104 {
105         /*
106          * We should never drop all dbuf refs without first clearing
107          * the eviction callback.
108          */
109         panic("evicting znode %p\n", user_ptr);
110 }
111
112 extern struct vop_vector zfs_vnodeops;
113 extern struct vop_vector zfs_fifoops;
114 extern struct vop_vector zfs_shareops;
115
116 /*
117  * XXX: We cannot use this function as a cache constructor, because
118  *      there is one global cache for all file systems and we need
119  *      to pass vfsp here, which is not possible, because argument
120  *      'cdrarg' is defined at kmem_cache_create() time.
121  */
122 /*ARGSUSED*/
123 static int
124 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
125 {
126         znode_t *zp = buf;
127         vnode_t *vp;
128         vfs_t *vfsp = arg;
129         int error;
130
131         POINTER_INVALIDATE(&zp->z_zfsvfs);
132         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
133
134         if (vfsp != NULL) {
135                 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
136                 if (error != 0 && (kmflags & KM_NOSLEEP))
137                         return (-1);
138                 ASSERT(error == 0);
139                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
140                 zp->z_vnode = vp;
141                 vp->v_data = (caddr_t)zp;
142                 VN_LOCK_AREC(vp);
143                 VN_LOCK_ASHARE(vp);
144         } else {
145                 zp->z_vnode = NULL;
146         }
147
148         list_link_init(&zp->z_link_node);
149
150         mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
151         rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
152         rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
153         mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
154
155         mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
156         avl_create(&zp->z_range_avl, zfs_range_compare,
157             sizeof (rl_t), offsetof(rl_t, r_node));
158
159         zp->z_dirlocks = NULL;
160         zp->z_acl_cached = NULL;
161         zp->z_moved = 0;
162         return (0);
163 }
164
165 /*ARGSUSED*/
166 static void
167 zfs_znode_cache_destructor(void *buf, void *arg)
168 {
169         znode_t *zp = buf;
170
171         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
172         ASSERT(ZTOV(zp) == NULL);
173         vn_free(ZTOV(zp));
174         ASSERT(!list_link_active(&zp->z_link_node));
175         mutex_destroy(&zp->z_lock);
176         rw_destroy(&zp->z_parent_lock);
177         rw_destroy(&zp->z_name_lock);
178         mutex_destroy(&zp->z_acl_lock);
179         avl_destroy(&zp->z_range_avl);
180         mutex_destroy(&zp->z_range_lock);
181
182         ASSERT(zp->z_dirlocks == NULL);
183         ASSERT(zp->z_acl_cached == NULL);
184 }
185
186 #ifdef  ZNODE_STATS
187 static struct {
188         uint64_t zms_zfsvfs_invalid;
189         uint64_t zms_zfsvfs_recheck1;
190         uint64_t zms_zfsvfs_unmounted;
191         uint64_t zms_zfsvfs_recheck2;
192         uint64_t zms_obj_held;
193         uint64_t zms_vnode_locked;
194         uint64_t zms_not_only_dnlc;
195 } znode_move_stats;
196 #endif  /* ZNODE_STATS */
197
198 #ifdef sun
199 static void
200 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
201 {
202         vnode_t *vp;
203
204         /* Copy fields. */
205         nzp->z_zfsvfs = ozp->z_zfsvfs;
206
207         /* Swap vnodes. */
208         vp = nzp->z_vnode;
209         nzp->z_vnode = ozp->z_vnode;
210         ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
211         ZTOV(ozp)->v_data = ozp;
212         ZTOV(nzp)->v_data = nzp;
213
214         nzp->z_id = ozp->z_id;
215         ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
216         ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
217         nzp->z_unlinked = ozp->z_unlinked;
218         nzp->z_atime_dirty = ozp->z_atime_dirty;
219         nzp->z_zn_prefetch = ozp->z_zn_prefetch;
220         nzp->z_blksz = ozp->z_blksz;
221         nzp->z_seq = ozp->z_seq;
222         nzp->z_mapcnt = ozp->z_mapcnt;
223         nzp->z_gen = ozp->z_gen;
224         nzp->z_sync_cnt = ozp->z_sync_cnt;
225         nzp->z_is_sa = ozp->z_is_sa;
226         nzp->z_sa_hdl = ozp->z_sa_hdl;
227         bcopy(ozp->z_atime, nzp->z_atime, sizeof (uint64_t) * 2);
228         nzp->z_links = ozp->z_links;
229         nzp->z_size = ozp->z_size;
230         nzp->z_pflags = ozp->z_pflags;
231         nzp->z_uid = ozp->z_uid;
232         nzp->z_gid = ozp->z_gid;
233         nzp->z_mode = ozp->z_mode;
234
235         /*
236          * Since this is just an idle znode and kmem is already dealing with
237          * memory pressure, release any cached ACL.
238          */
239         if (ozp->z_acl_cached) {
240                 zfs_acl_free(ozp->z_acl_cached);
241                 ozp->z_acl_cached = NULL;
242         }
243
244         sa_set_userp(nzp->z_sa_hdl, nzp);
245
246         /*
247          * Invalidate the original znode by clearing fields that provide a
248          * pointer back to the znode. Set the low bit of the vfs pointer to
249          * ensure that zfs_znode_move() recognizes the znode as invalid in any
250          * subsequent callback.
251          */
252         ozp->z_sa_hdl = NULL;
253         POINTER_INVALIDATE(&ozp->z_zfsvfs);
254
255         /*
256          * Mark the znode.
257          */
258         nzp->z_moved = 1;
259         ozp->z_moved = (uint8_t)-1;
260 }
261
262 /*ARGSUSED*/
263 static kmem_cbrc_t
264 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
265 {
266         znode_t *ozp = buf, *nzp = newbuf;
267         zfsvfs_t *zfsvfs;
268         vnode_t *vp;
269
270         /*
271          * The znode is on the file system's list of known znodes if the vfs
272          * pointer is valid. We set the low bit of the vfs pointer when freeing
273          * the znode to invalidate it, and the memory patterns written by kmem
274          * (baddcafe and deadbeef) set at least one of the two low bits. A newly
275          * created znode sets the vfs pointer last of all to indicate that the
276          * znode is known and in a valid state to be moved by this function.
277          */
278         zfsvfs = ozp->z_zfsvfs;
279         if (!POINTER_IS_VALID(zfsvfs)) {
280                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
281                 return (KMEM_CBRC_DONT_KNOW);
282         }
283
284         /*
285          * Close a small window in which it's possible that the filesystem could
286          * be unmounted and freed, and zfsvfs, though valid in the previous
287          * statement, could point to unrelated memory by the time we try to
288          * prevent the filesystem from being unmounted.
289          */
290         rw_enter(&zfsvfs_lock, RW_WRITER);
291         if (zfsvfs != ozp->z_zfsvfs) {
292                 rw_exit(&zfsvfs_lock);
293                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
294                 return (KMEM_CBRC_DONT_KNOW);
295         }
296
297         /*
298          * If the znode is still valid, then so is the file system. We know that
299          * no valid file system can be freed while we hold zfsvfs_lock, so we
300          * can safely ensure that the filesystem is not and will not be
301          * unmounted. The next statement is equivalent to ZFS_ENTER().
302          */
303         rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
304         if (zfsvfs->z_unmounted) {
305                 ZFS_EXIT(zfsvfs);
306                 rw_exit(&zfsvfs_lock);
307                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
308                 return (KMEM_CBRC_DONT_KNOW);
309         }
310         rw_exit(&zfsvfs_lock);
311
312         mutex_enter(&zfsvfs->z_znodes_lock);
313         /*
314          * Recheck the vfs pointer in case the znode was removed just before
315          * acquiring the lock.
316          */
317         if (zfsvfs != ozp->z_zfsvfs) {
318                 mutex_exit(&zfsvfs->z_znodes_lock);
319                 ZFS_EXIT(zfsvfs);
320                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
321                 return (KMEM_CBRC_DONT_KNOW);
322         }
323
324         /*
325          * At this point we know that as long as we hold z_znodes_lock, the
326          * znode cannot be freed and fields within the znode can be safely
327          * accessed. Now, prevent a race with zfs_zget().
328          */
329         if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
330                 mutex_exit(&zfsvfs->z_znodes_lock);
331                 ZFS_EXIT(zfsvfs);
332                 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
333                 return (KMEM_CBRC_LATER);
334         }
335
336         vp = ZTOV(ozp);
337         if (mutex_tryenter(&vp->v_lock) == 0) {
338                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
339                 mutex_exit(&zfsvfs->z_znodes_lock);
340                 ZFS_EXIT(zfsvfs);
341                 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
342                 return (KMEM_CBRC_LATER);
343         }
344
345         /* Only move znodes that are referenced _only_ by the DNLC. */
346         if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
347                 mutex_exit(&vp->v_lock);
348                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
349                 mutex_exit(&zfsvfs->z_znodes_lock);
350                 ZFS_EXIT(zfsvfs);
351                 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
352                 return (KMEM_CBRC_LATER);
353         }
354
355         /*
356          * The znode is known and in a valid state to move. We're holding the
357          * locks needed to execute the critical section.
358          */
359         zfs_znode_move_impl(ozp, nzp);
360         mutex_exit(&vp->v_lock);
361         ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
362
363         list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
364         mutex_exit(&zfsvfs->z_znodes_lock);
365         ZFS_EXIT(zfsvfs);
366
367         return (KMEM_CBRC_YES);
368 }
369 #endif /* sun */
370
371 void
372 zfs_znode_init(void)
373 {
374         /*
375          * Initialize zcache
376          */
377         rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
378         ASSERT(znode_cache == NULL);
379         znode_cache = kmem_cache_create("zfs_znode_cache",
380             sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
381             zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
382         kmem_cache_set_move(znode_cache, zfs_znode_move);
383 }
384
385 void
386 zfs_znode_fini(void)
387 {
388 #ifdef sun
389         /*
390          * Cleanup vfs & vnode ops
391          */
392         zfs_remove_op_tables();
393 #endif  /* sun */
394
395         /*
396          * Cleanup zcache
397          */
398         if (znode_cache)
399                 kmem_cache_destroy(znode_cache);
400         znode_cache = NULL;
401         rw_destroy(&zfsvfs_lock);
402 }
403
404 #ifdef sun
405 struct vnodeops *zfs_dvnodeops;
406 struct vnodeops *zfs_fvnodeops;
407 struct vnodeops *zfs_symvnodeops;
408 struct vnodeops *zfs_xdvnodeops;
409 struct vnodeops *zfs_evnodeops;
410 struct vnodeops *zfs_sharevnodeops;
411
412 void
413 zfs_remove_op_tables()
414 {
415         /*
416          * Remove vfs ops
417          */
418         ASSERT(zfsfstype);
419         (void) vfs_freevfsops_by_type(zfsfstype);
420         zfsfstype = 0;
421
422         /*
423          * Remove vnode ops
424          */
425         if (zfs_dvnodeops)
426                 vn_freevnodeops(zfs_dvnodeops);
427         if (zfs_fvnodeops)
428                 vn_freevnodeops(zfs_fvnodeops);
429         if (zfs_symvnodeops)
430                 vn_freevnodeops(zfs_symvnodeops);
431         if (zfs_xdvnodeops)
432                 vn_freevnodeops(zfs_xdvnodeops);
433         if (zfs_evnodeops)
434                 vn_freevnodeops(zfs_evnodeops);
435         if (zfs_sharevnodeops)
436                 vn_freevnodeops(zfs_sharevnodeops);
437
438         zfs_dvnodeops = NULL;
439         zfs_fvnodeops = NULL;
440         zfs_symvnodeops = NULL;
441         zfs_xdvnodeops = NULL;
442         zfs_evnodeops = NULL;
443         zfs_sharevnodeops = NULL;
444 }
445
446 extern const fs_operation_def_t zfs_dvnodeops_template[];
447 extern const fs_operation_def_t zfs_fvnodeops_template[];
448 extern const fs_operation_def_t zfs_xdvnodeops_template[];
449 extern const fs_operation_def_t zfs_symvnodeops_template[];
450 extern const fs_operation_def_t zfs_evnodeops_template[];
451 extern const fs_operation_def_t zfs_sharevnodeops_template[];
452
453 int
454 zfs_create_op_tables()
455 {
456         int error;
457
458         /*
459          * zfs_dvnodeops can be set if mod_remove() calls mod_installfs()
460          * due to a failure to remove the the 2nd modlinkage (zfs_modldrv).
461          * In this case we just return as the ops vectors are already set up.
462          */
463         if (zfs_dvnodeops)
464                 return (0);
465
466         error = vn_make_ops(MNTTYPE_ZFS, zfs_dvnodeops_template,
467             &zfs_dvnodeops);
468         if (error)
469                 return (error);
470
471         error = vn_make_ops(MNTTYPE_ZFS, zfs_fvnodeops_template,
472             &zfs_fvnodeops);
473         if (error)
474                 return (error);
475
476         error = vn_make_ops(MNTTYPE_ZFS, zfs_symvnodeops_template,
477             &zfs_symvnodeops);
478         if (error)
479                 return (error);
480
481         error = vn_make_ops(MNTTYPE_ZFS, zfs_xdvnodeops_template,
482             &zfs_xdvnodeops);
483         if (error)
484                 return (error);
485
486         error = vn_make_ops(MNTTYPE_ZFS, zfs_evnodeops_template,
487             &zfs_evnodeops);
488         if (error)
489                 return (error);
490
491         error = vn_make_ops(MNTTYPE_ZFS, zfs_sharevnodeops_template,
492             &zfs_sharevnodeops);
493
494         return (error);
495 }
496 #endif  /* sun */
497
498 int
499 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
500 {
501         zfs_acl_ids_t acl_ids;
502         vattr_t vattr;
503         znode_t *sharezp;
504         vnode_t *vp, vnode;
505         znode_t *zp;
506         int error;
507
508         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
509         vattr.va_type = VDIR;
510         vattr.va_mode = S_IFDIR|0555;
511         vattr.va_uid = crgetuid(kcred);
512         vattr.va_gid = crgetgid(kcred);
513
514         sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
515         zfs_znode_cache_constructor(sharezp, zfsvfs->z_parent->z_vfs, 0);
516         ASSERT(!POINTER_IS_VALID(sharezp->z_zfsvfs));
517         sharezp->z_moved = 0;
518         sharezp->z_unlinked = 0;
519         sharezp->z_atime_dirty = 0;
520         sharezp->z_zfsvfs = zfsvfs;
521         sharezp->z_is_sa = zfsvfs->z_use_sa;
522
523         sharezp->z_vnode = &vnode;
524         vnode.v_data = sharezp;
525
526         vp = ZTOV(sharezp);
527         vp->v_type = VDIR;
528
529         VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
530             kcred, NULL, &acl_ids));
531         zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE, &zp, &acl_ids);
532         ASSERT3P(zp, ==, sharezp);
533         POINTER_INVALIDATE(&sharezp->z_zfsvfs);
534         error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
535             ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
536         zfsvfs->z_shares_dir = sharezp->z_id;
537
538         zfs_acl_ids_free(&acl_ids);
539         ZTOV(sharezp)->v_data = NULL;
540         ZTOV(sharezp)->v_count = 0;
541         ZTOV(sharezp)->v_holdcnt = 0;
542         zp->z_vnode = NULL;
543         sa_handle_destroy(sharezp->z_sa_hdl);
544         sharezp->z_vnode = NULL;
545         kmem_cache_free(znode_cache, sharezp);
546
547         return (error);
548 }
549
550 /*
551  * define a couple of values we need available
552  * for both 64 and 32 bit environments.
553  */
554 #ifndef NBITSMINOR64
555 #define NBITSMINOR64    32
556 #endif
557 #ifndef MAXMAJ64
558 #define MAXMAJ64        0xffffffffUL
559 #endif
560 #ifndef MAXMIN64
561 #define MAXMIN64        0xffffffffUL
562 #endif
563
564 /*
565  * Create special expldev for ZFS private use.
566  * Can't use standard expldev since it doesn't do
567  * what we want.  The standard expldev() takes a
568  * dev32_t in LP64 and expands it to a long dev_t.
569  * We need an interface that takes a dev32_t in ILP32
570  * and expands it to a long dev_t.
571  */
572 static uint64_t
573 zfs_expldev(dev_t dev)
574 {
575         return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
576 }
577 /*
578  * Special cmpldev for ZFS private use.
579  * Can't use standard cmpldev since it takes
580  * a long dev_t and compresses it to dev32_t in
581  * LP64.  We need to do a compaction of a long dev_t
582  * to a dev32_t in ILP32.
583  */
584 dev_t
585 zfs_cmpldev(uint64_t dev)
586 {
587         return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
588 }
589
590 static void
591 zfs_znode_sa_init(zfsvfs_t *zfsvfs, znode_t *zp,
592     dmu_buf_t *db, dmu_object_type_t obj_type, sa_handle_t *sa_hdl)
593 {
594         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
595         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
596
597         mutex_enter(&zp->z_lock);
598
599         ASSERT(zp->z_sa_hdl == NULL);
600         ASSERT(zp->z_acl_cached == NULL);
601         if (sa_hdl == NULL) {
602                 VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, zp,
603                     SA_HDL_SHARED, &zp->z_sa_hdl));
604         } else {
605                 zp->z_sa_hdl = sa_hdl;
606                 sa_set_userp(sa_hdl, zp);
607         }
608
609         zp->z_is_sa = (obj_type == DMU_OT_SA) ? B_TRUE : B_FALSE;
610
611         /*
612          * Slap on VROOT if we are the root znode
613          */
614         if (zp->z_id == zfsvfs->z_root)
615                 ZTOV(zp)->v_flag |= VROOT;
616
617         mutex_exit(&zp->z_lock);
618         vn_exists(ZTOV(zp));
619 }
620
621 void
622 zfs_znode_dmu_fini(znode_t *zp)
623 {
624         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
625             zp->z_unlinked ||
626             RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
627
628         sa_handle_destroy(zp->z_sa_hdl);
629         zp->z_sa_hdl = NULL;
630 }
631
632 static void
633 zfs_vnode_forget(vnode_t *vp)
634 {
635
636         /* copied from insmntque_stddtr */
637         vp->v_data = NULL;
638         vp->v_op = &dead_vnodeops;
639         vgone(vp);
640         vput(vp);
641 }
642
643 /*
644  * Construct a new znode/vnode and intialize.
645  *
646  * This does not do a call to dmu_set_user() that is
647  * up to the caller to do, in case you don't want to
648  * return the znode
649  */
650 static znode_t *
651 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz,
652     dmu_object_type_t obj_type, sa_handle_t *hdl)
653 {
654         znode_t *zp;
655         vnode_t *vp;
656         uint64_t mode;
657         uint64_t parent;
658         sa_bulk_attr_t bulk[9];
659         int count = 0;
660
661         zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
662         zfs_znode_cache_constructor(zp, zfsvfs->z_parent->z_vfs, 0);
663
664         ASSERT(zp->z_dirlocks == NULL);
665         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
666         zp->z_moved = 0;
667
668         /*
669          * Defer setting z_zfsvfs until the znode is ready to be a candidate for
670          * the zfs_znode_move() callback.
671          */
672         zp->z_sa_hdl = NULL;
673         zp->z_unlinked = 0;
674         zp->z_atime_dirty = 0;
675         zp->z_mapcnt = 0;
676         zp->z_id = db->db_object;
677         zp->z_blksz = blksz;
678         zp->z_seq = 0x7A4653;
679         zp->z_sync_cnt = 0;
680
681         vp = ZTOV(zp);
682
683         zfs_znode_sa_init(zfsvfs, zp, db, obj_type, hdl);
684
685         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8);
686         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL, &zp->z_gen, 8);
687         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
688             &zp->z_size, 8);
689         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
690             &zp->z_links, 8);
691         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
692             &zp->z_pflags, 8);
693         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL, &parent, 8);
694         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
695             &zp->z_atime, 16);
696         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
697             &zp->z_uid, 8);
698         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
699             &zp->z_gid, 8);
700
701         if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count) != 0 || zp->z_gen == 0) {
702                 if (hdl == NULL)
703                         sa_handle_destroy(zp->z_sa_hdl);
704                 zfs_vnode_forget(vp);
705                 zp->z_vnode = NULL;
706                 kmem_cache_free(znode_cache, zp);
707                 return (NULL);
708         }
709
710         zp->z_mode = mode;
711
712         vp->v_type = IFTOVT((mode_t)mode);
713
714         switch (vp->v_type) {
715         case VDIR:
716                 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
717                 break;
718 #ifdef sun
719         case VBLK:
720         case VCHR:
721                 {
722                         uint64_t rdev;
723                         VERIFY(sa_lookup(zp->z_sa_hdl, SA_ZPL_RDEV(zfsvfs),
724                             &rdev, sizeof (rdev)) == 0);
725
726                         vp->v_rdev = zfs_cmpldev(rdev);
727                 }
728                 break;
729 #endif  /* sun */
730         case VFIFO:
731 #ifdef sun
732         case VSOCK:
733         case VDOOR:
734 #endif  /* sun */
735                 vp->v_op = &zfs_fifoops;
736                 break;
737         case VREG:
738                 if (parent == zfsvfs->z_shares_dir) {
739                         ASSERT(zp->z_uid == 0 && zp->z_gid == 0);
740                         vp->v_op = &zfs_shareops;
741                 }
742                 break;
743 #ifdef sun
744         case VLNK:
745                 vn_setops(vp, zfs_symvnodeops);
746                 break;
747         default:
748                 vn_setops(vp, zfs_evnodeops);
749                 break;
750 #endif  /* sun */
751         }
752         if (vp->v_type != VFIFO)
753                 VN_LOCK_ASHARE(vp);
754
755         mutex_enter(&zfsvfs->z_znodes_lock);
756         list_insert_tail(&zfsvfs->z_all_znodes, zp);
757         membar_producer();
758         /*
759          * Everything else must be valid before assigning z_zfsvfs makes the
760          * znode eligible for zfs_znode_move().
761          */
762         zp->z_zfsvfs = zfsvfs;
763         mutex_exit(&zfsvfs->z_znodes_lock);
764
765         VFS_HOLD(zfsvfs->z_vfs);
766         return (zp);
767 }
768
769 static uint64_t empty_xattr;
770 static uint64_t pad[4];
771 static zfs_acl_phys_t acl_phys;
772 /*
773  * Create a new DMU object to hold a zfs znode.
774  *
775  *      IN:     dzp     - parent directory for new znode
776  *              vap     - file attributes for new znode
777  *              tx      - dmu transaction id for zap operations
778  *              cr      - credentials of caller
779  *              flag    - flags:
780  *                        IS_ROOT_NODE  - new object will be root
781  *                        IS_XATTR      - new object is an attribute
782  *              bonuslen - length of bonus buffer
783  *              setaclp  - File/Dir initial ACL
784  *              fuidp    - Tracks fuid allocation.
785  *
786  *      OUT:    zpp     - allocated znode
787  *
788  */
789 void
790 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
791     uint_t flag, znode_t **zpp, zfs_acl_ids_t *acl_ids)
792 {
793         uint64_t        crtime[2], atime[2], mtime[2], ctime[2];
794         uint64_t        mode, size, links, parent, pflags;
795         uint64_t        dzp_pflags = 0;
796         uint64_t        rdev = 0;
797         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
798         dmu_buf_t       *db;
799         timestruc_t     now;
800         uint64_t        gen, obj;
801         int             err;
802         int             bonuslen;
803         sa_handle_t     *sa_hdl;
804         dmu_object_type_t obj_type;
805         sa_bulk_attr_t  sa_attrs[ZPL_END];
806         int             cnt = 0;
807         zfs_acl_locator_cb_t locate = { 0 };
808
809         ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
810
811         if (zfsvfs->z_replay) {
812                 obj = vap->va_nodeid;
813                 now = vap->va_ctime;            /* see zfs_replay_create() */
814                 gen = vap->va_nblocks;          /* ditto */
815         } else {
816                 obj = 0;
817                 gethrestime(&now);
818                 gen = dmu_tx_get_txg(tx);
819         }
820
821         obj_type = zfsvfs->z_use_sa ? DMU_OT_SA : DMU_OT_ZNODE;
822         bonuslen = (obj_type == DMU_OT_SA) ?
823             DN_MAX_BONUSLEN : ZFS_OLD_ZNODE_PHYS_SIZE;
824
825         /*
826          * Create a new DMU object.
827          */
828         /*
829          * There's currently no mechanism for pre-reading the blocks that will
830          * be needed to allocate a new object, so we accept the small chance
831          * that there will be an i/o error and we will fail one of the
832          * assertions below.
833          */
834         if (vap->va_type == VDIR) {
835                 if (zfsvfs->z_replay) {
836                         err = zap_create_claim_norm(zfsvfs->z_os, obj,
837                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
838                             obj_type, bonuslen, tx);
839                         ASSERT0(err);
840                 } else {
841                         obj = zap_create_norm(zfsvfs->z_os,
842                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
843                             obj_type, bonuslen, tx);
844                 }
845         } else {
846                 if (zfsvfs->z_replay) {
847                         err = dmu_object_claim(zfsvfs->z_os, obj,
848                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
849                             obj_type, bonuslen, tx);
850                         ASSERT0(err);
851                 } else {
852                         obj = dmu_object_alloc(zfsvfs->z_os,
853                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
854                             obj_type, bonuslen, tx);
855                 }
856         }
857
858         getnewvnode_reserve(1);
859         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
860         VERIFY(0 == sa_buf_hold(zfsvfs->z_os, obj, NULL, &db));
861
862         /*
863          * If this is the root, fix up the half-initialized parent pointer
864          * to reference the just-allocated physical data area.
865          */
866         if (flag & IS_ROOT_NODE) {
867                 dzp->z_id = obj;
868         } else {
869                 dzp_pflags = dzp->z_pflags;
870         }
871
872         /*
873          * If parent is an xattr, so am I.
874          */
875         if (dzp_pflags & ZFS_XATTR) {
876                 flag |= IS_XATTR;
877         }
878
879         if (zfsvfs->z_use_fuids)
880                 pflags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
881         else
882                 pflags = 0;
883
884         if (vap->va_type == VDIR) {
885                 size = 2;               /* contents ("." and "..") */
886                 links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
887         } else {
888                 size = links = 0;
889         }
890
891         if (vap->va_type == VBLK || vap->va_type == VCHR) {
892                 rdev = zfs_expldev(vap->va_rdev);
893         }
894
895         parent = dzp->z_id;
896         mode = acl_ids->z_mode;
897         if (flag & IS_XATTR)
898                 pflags |= ZFS_XATTR;
899
900         /*
901          * No execs denied will be deterimed when zfs_mode_compute() is called.
902          */
903         pflags |= acl_ids->z_aclp->z_hints &
904             (ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|ZFS_ACL_AUTO_INHERIT|
905             ZFS_ACL_DEFAULTED|ZFS_ACL_PROTECTED);
906
907         ZFS_TIME_ENCODE(&now, crtime);
908         ZFS_TIME_ENCODE(&now, ctime);
909
910         if (vap->va_mask & AT_ATIME) {
911                 ZFS_TIME_ENCODE(&vap->va_atime, atime);
912         } else {
913                 ZFS_TIME_ENCODE(&now, atime);
914         }
915
916         if (vap->va_mask & AT_MTIME) {
917                 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
918         } else {
919                 ZFS_TIME_ENCODE(&now, mtime);
920         }
921
922         /* Now add in all of the "SA" attributes */
923         VERIFY(0 == sa_handle_get_from_db(zfsvfs->z_os, db, NULL, SA_HDL_SHARED,
924             &sa_hdl));
925
926         /*
927          * Setup the array of attributes to be replaced/set on the new file
928          *
929          * order for  DMU_OT_ZNODE is critical since it needs to be constructed
930          * in the old znode_phys_t format.  Don't change this ordering
931          */
932
933         if (obj_type == DMU_OT_ZNODE) {
934                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
935                     NULL, &atime, 16);
936                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
937                     NULL, &mtime, 16);
938                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
939                     NULL, &ctime, 16);
940                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
941                     NULL, &crtime, 16);
942                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
943                     NULL, &gen, 8);
944                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
945                     NULL, &mode, 8);
946                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
947                     NULL, &size, 8);
948                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
949                     NULL, &parent, 8);
950         } else {
951                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MODE(zfsvfs),
952                     NULL, &mode, 8);
953                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_SIZE(zfsvfs),
954                     NULL, &size, 8);
955                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GEN(zfsvfs),
956                     NULL, &gen, 8);
957                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
958                     &acl_ids->z_fuid, 8);
959                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
960                     &acl_ids->z_fgid, 8);
961                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PARENT(zfsvfs),
962                     NULL, &parent, 8);
963                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
964                     NULL, &pflags, 8);
965                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ATIME(zfsvfs),
966                     NULL, &atime, 16);
967                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_MTIME(zfsvfs),
968                     NULL, &mtime, 16);
969                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CTIME(zfsvfs),
970                     NULL, &ctime, 16);
971                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_CRTIME(zfsvfs),
972                     NULL, &crtime, 16);
973         }
974
975         SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_LINKS(zfsvfs), NULL, &links, 8);
976
977         if (obj_type == DMU_OT_ZNODE) {
978                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_XATTR(zfsvfs), NULL,
979                     &empty_xattr, 8);
980         }
981         if (obj_type == DMU_OT_ZNODE ||
982             (vap->va_type == VBLK || vap->va_type == VCHR)) {
983                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_RDEV(zfsvfs),
984                     NULL, &rdev, 8);
985
986         }
987         if (obj_type == DMU_OT_ZNODE) {
988                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_FLAGS(zfsvfs),
989                     NULL, &pflags, 8);
990                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_UID(zfsvfs), NULL,
991                     &acl_ids->z_fuid, 8);
992                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_GID(zfsvfs), NULL,
993                     &acl_ids->z_fgid, 8);
994                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_PAD(zfsvfs), NULL, pad,
995                     sizeof (uint64_t) * 4);
996                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_ZNODE_ACL(zfsvfs), NULL,
997                     &acl_phys, sizeof (zfs_acl_phys_t));
998         } else if (acl_ids->z_aclp->z_version >= ZFS_ACL_VERSION_FUID) {
999                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_COUNT(zfsvfs), NULL,
1000                     &acl_ids->z_aclp->z_acl_count, 8);
1001                 locate.cb_aclp = acl_ids->z_aclp;
1002                 SA_ADD_BULK_ATTR(sa_attrs, cnt, SA_ZPL_DACL_ACES(zfsvfs),
1003                     zfs_acl_data_locator, &locate,
1004                     acl_ids->z_aclp->z_acl_bytes);
1005                 mode = zfs_mode_compute(mode, acl_ids->z_aclp, &pflags,
1006                     acl_ids->z_fuid, acl_ids->z_fgid);
1007         }
1008
1009         VERIFY(sa_replace_all_by_template(sa_hdl, sa_attrs, cnt, tx) == 0);
1010
1011         if (!(flag & IS_ROOT_NODE)) {
1012                 *zpp = zfs_znode_alloc(zfsvfs, db, 0, obj_type, sa_hdl);
1013                 ASSERT(*zpp != NULL);
1014         } else {
1015                 /*
1016                  * If we are creating the root node, the "parent" we
1017                  * passed in is the znode for the root.
1018                  */
1019                 *zpp = dzp;
1020
1021                 (*zpp)->z_sa_hdl = sa_hdl;
1022         }
1023
1024         (*zpp)->z_pflags = pflags;
1025         (*zpp)->z_mode = mode;
1026
1027         if (vap->va_mask & AT_XVATTR)
1028                 zfs_xvattr_set(*zpp, (xvattr_t *)vap, tx);
1029
1030         if (obj_type == DMU_OT_ZNODE ||
1031             acl_ids->z_aclp->z_version < ZFS_ACL_VERSION_FUID) {
1032                 err = zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx);
1033                 ASSERT0(err);
1034         }
1035         if (!(flag & IS_ROOT_NODE)) {
1036                 vnode_t *vp;
1037
1038                 vp = ZTOV(*zpp);
1039                 vp->v_vflag |= VV_FORCEINSMQ;
1040                 err = insmntque(vp, zfsvfs->z_vfs);
1041                 vp->v_vflag &= ~VV_FORCEINSMQ;
1042                 KASSERT(err == 0, ("insmntque() failed: error %d", err));
1043         }
1044         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1045         getnewvnode_drop_reserve();
1046 }
1047
1048 /*
1049  * Update in-core attributes.  It is assumed the caller will be doing an
1050  * sa_bulk_update to push the changes out.
1051  */
1052 void
1053 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap, dmu_tx_t *tx)
1054 {
1055         xoptattr_t *xoap;
1056
1057         xoap = xva_getxoptattr(xvap);
1058         ASSERT(xoap);
1059
1060         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1061                 uint64_t times[2];
1062                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, times);
1063                 (void) sa_update(zp->z_sa_hdl, SA_ZPL_CRTIME(zp->z_zfsvfs),
1064                     &times, sizeof (times), tx);
1065                 XVA_SET_RTN(xvap, XAT_CREATETIME);
1066         }
1067         if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
1068                 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly,
1069                     zp->z_pflags, tx);
1070                 XVA_SET_RTN(xvap, XAT_READONLY);
1071         }
1072         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
1073                 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden,
1074                     zp->z_pflags, tx);
1075                 XVA_SET_RTN(xvap, XAT_HIDDEN);
1076         }
1077         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
1078                 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system,
1079                     zp->z_pflags, tx);
1080                 XVA_SET_RTN(xvap, XAT_SYSTEM);
1081         }
1082         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
1083                 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive,
1084                     zp->z_pflags, tx);
1085                 XVA_SET_RTN(xvap, XAT_ARCHIVE);
1086         }
1087         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
1088                 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable,
1089                     zp->z_pflags, tx);
1090                 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
1091         }
1092         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
1093                 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink,
1094                     zp->z_pflags, tx);
1095                 XVA_SET_RTN(xvap, XAT_NOUNLINK);
1096         }
1097         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
1098                 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly,
1099                     zp->z_pflags, tx);
1100                 XVA_SET_RTN(xvap, XAT_APPENDONLY);
1101         }
1102         if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
1103                 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump,
1104                     zp->z_pflags, tx);
1105                 XVA_SET_RTN(xvap, XAT_NODUMP);
1106         }
1107         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
1108                 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque,
1109                     zp->z_pflags, tx);
1110                 XVA_SET_RTN(xvap, XAT_OPAQUE);
1111         }
1112         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1113                 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
1114                     xoap->xoa_av_quarantined, zp->z_pflags, tx);
1115                 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
1116         }
1117         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
1118                 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified,
1119                     zp->z_pflags, tx);
1120                 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
1121         }
1122         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1123                 zfs_sa_set_scanstamp(zp, xvap, tx);
1124                 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
1125         }
1126         if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
1127                 ZFS_ATTR_SET(zp, ZFS_REPARSE, xoap->xoa_reparse,
1128                     zp->z_pflags, tx);
1129                 XVA_SET_RTN(xvap, XAT_REPARSE);
1130         }
1131         if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
1132                 ZFS_ATTR_SET(zp, ZFS_OFFLINE, xoap->xoa_offline,
1133                     zp->z_pflags, tx);
1134                 XVA_SET_RTN(xvap, XAT_OFFLINE);
1135         }
1136         if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1137                 ZFS_ATTR_SET(zp, ZFS_SPARSE, xoap->xoa_sparse,
1138                     zp->z_pflags, tx);
1139                 XVA_SET_RTN(xvap, XAT_SPARSE);
1140         }
1141 }
1142
1143 int
1144 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
1145 {
1146         dmu_object_info_t doi;
1147         dmu_buf_t       *db;
1148         znode_t         *zp;
1149         vnode_t         *vp;
1150         sa_handle_t     *hdl;
1151         struct thread   *td;
1152         int locked;
1153         int err;
1154
1155         td = curthread;
1156         getnewvnode_reserve(1);
1157 again:
1158         *zpp = NULL;
1159         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1160
1161         err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1162         if (err) {
1163                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1164                 getnewvnode_drop_reserve();
1165                 return (err);
1166         }
1167
1168         dmu_object_info_from_db(db, &doi);
1169         if (doi.doi_bonus_type != DMU_OT_SA &&
1170             (doi.doi_bonus_type != DMU_OT_ZNODE ||
1171             (doi.doi_bonus_type == DMU_OT_ZNODE &&
1172             doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1173                 sa_buf_rele(db, NULL);
1174                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1175 #ifdef __FreeBSD__
1176                 getnewvnode_drop_reserve();
1177 #endif
1178                 return (SET_ERROR(EINVAL));
1179         }
1180
1181         hdl = dmu_buf_get_user(db);
1182         if (hdl != NULL) {
1183                 zp  = sa_get_userdata(hdl);
1184
1185
1186                 /*
1187                  * Since "SA" does immediate eviction we
1188                  * should never find a sa handle that doesn't
1189                  * know about the znode.
1190                  */
1191
1192                 ASSERT3P(zp, !=, NULL);
1193
1194                 mutex_enter(&zp->z_lock);
1195                 ASSERT3U(zp->z_id, ==, obj_num);
1196                 if (zp->z_unlinked) {
1197                         err = SET_ERROR(ENOENT);
1198                 } else {
1199                         vp = ZTOV(zp);
1200                         *zpp = zp;
1201                         err = 0;
1202                 }
1203                 sa_buf_rele(db, NULL);
1204
1205                 /* Don't let the vnode disappear after ZFS_OBJ_HOLD_EXIT. */
1206                 if (err == 0)
1207                         VN_HOLD(vp);
1208
1209                 mutex_exit(&zp->z_lock);
1210                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1211
1212                 if (err == 0) {
1213                         locked = VOP_ISLOCKED(vp);
1214                         VI_LOCK(vp);
1215                         if ((vp->v_iflag & VI_DOOMED) != 0 &&
1216                             locked != LK_EXCLUSIVE) {
1217                                 /*
1218                                  * The vnode is doomed and this thread doesn't
1219                                  * hold the exclusive lock on it, so the vnode
1220                                  * must be being reclaimed by another thread.
1221                                  * Otherwise the doomed vnode is being reclaimed
1222                                  * by this thread and zfs_zget is called from
1223                                  * ZIL internals.
1224                                  */
1225                                 VI_UNLOCK(vp);
1226                                 VN_RELE(vp);
1227                                 goto again;
1228                         }
1229                         VI_UNLOCK(vp);
1230                 }
1231                 getnewvnode_drop_reserve();
1232                 return (err);
1233         }
1234
1235         /*
1236          * Not found create new znode/vnode
1237          * but only if file exists.
1238          *
1239          * There is a small window where zfs_vget() could
1240          * find this object while a file create is still in
1241          * progress.  This is checked for in zfs_znode_alloc()
1242          *
1243          * if zfs_znode_alloc() fails it will drop the hold on the
1244          * bonus buffer.
1245          */
1246         zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size,
1247             doi.doi_bonus_type, NULL);
1248         if (zp == NULL) {
1249                 err = SET_ERROR(ENOENT);
1250         } else {
1251                 *zpp = zp;
1252         }
1253         if (err == 0) {
1254                 vnode_t *vp = ZTOV(zp);
1255
1256                 err = insmntque(vp, zfsvfs->z_vfs);
1257                 if (err == 0)
1258                         VOP_UNLOCK(vp, 0);
1259                 else {
1260                         zp->z_vnode = NULL;
1261                         zfs_znode_dmu_fini(zp);
1262                         zfs_znode_free(zp);
1263                         *zpp = NULL;
1264                 }
1265         }
1266         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1267         getnewvnode_drop_reserve();
1268         return (err);
1269 }
1270
1271 int
1272 zfs_rezget(znode_t *zp)
1273 {
1274         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1275         dmu_object_info_t doi;
1276         dmu_buf_t *db;
1277         vnode_t *vp;
1278         uint64_t obj_num = zp->z_id;
1279         uint64_t mode, size;
1280         sa_bulk_attr_t bulk[8];
1281         int err;
1282         int count = 0;
1283         uint64_t gen;
1284
1285         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
1286
1287         mutex_enter(&zp->z_acl_lock);
1288         if (zp->z_acl_cached) {
1289                 zfs_acl_free(zp->z_acl_cached);
1290                 zp->z_acl_cached = NULL;
1291         }
1292
1293         mutex_exit(&zp->z_acl_lock);
1294         ASSERT(zp->z_sa_hdl == NULL);
1295         err = sa_buf_hold(zfsvfs->z_os, obj_num, NULL, &db);
1296         if (err) {
1297                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1298                 return (err);
1299         }
1300
1301         dmu_object_info_from_db(db, &doi);
1302         if (doi.doi_bonus_type != DMU_OT_SA &&
1303             (doi.doi_bonus_type != DMU_OT_ZNODE ||
1304             (doi.doi_bonus_type == DMU_OT_ZNODE &&
1305             doi.doi_bonus_size < sizeof (znode_phys_t)))) {
1306                 sa_buf_rele(db, NULL);
1307                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1308                 return (SET_ERROR(EINVAL));
1309         }
1310
1311         zfs_znode_sa_init(zfsvfs, zp, db, doi.doi_bonus_type, NULL);
1312         size = zp->z_size;
1313
1314         /* reload cached values */
1315         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GEN(zfsvfs), NULL,
1316             &gen, sizeof (gen));
1317         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
1318             &zp->z_size, sizeof (zp->z_size));
1319         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
1320             &zp->z_links, sizeof (zp->z_links));
1321         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
1322             &zp->z_pflags, sizeof (zp->z_pflags));
1323         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
1324             &zp->z_atime, sizeof (zp->z_atime));
1325         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
1326             &zp->z_uid, sizeof (zp->z_uid));
1327         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL,
1328             &zp->z_gid, sizeof (zp->z_gid));
1329         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
1330             &mode, sizeof (mode));
1331
1332         if (sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) {
1333                 zfs_znode_dmu_fini(zp);
1334                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1335                 return (SET_ERROR(EIO));
1336         }
1337
1338         zp->z_mode = mode;
1339
1340         if (gen != zp->z_gen) {
1341                 zfs_znode_dmu_fini(zp);
1342                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1343                 return (SET_ERROR(EIO));
1344         }
1345
1346         /*
1347          * XXXPJD: Not sure how is that possible, but under heavy
1348          * zfs recv -F load it happens that z_gen is the same, but
1349          * vnode type is different than znode type. This would mean
1350          * that for example regular file was replaced with directory
1351          * which has the same object number.
1352          */
1353         vp = ZTOV(zp);
1354         if (vp != NULL &&
1355             vp->v_type != IFTOVT((mode_t)zp->z_mode)) {
1356                 zfs_znode_dmu_fini(zp);
1357                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1358                 return (EIO);
1359         }
1360
1361         zp->z_unlinked = (zp->z_links == 0);
1362         zp->z_blksz = doi.doi_data_block_size;
1363         if (vp != NULL) {
1364                 vn_pages_remove(vp, 0, 0);
1365                 if (zp->z_size != size)
1366                         vnode_pager_setsize(vp, zp->z_size);
1367         }
1368
1369         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1370
1371         return (0);
1372 }
1373
1374 void
1375 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1376 {
1377         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1378         objset_t *os = zfsvfs->z_os;
1379         uint64_t obj = zp->z_id;
1380         uint64_t acl_obj = zfs_external_acl(zp);
1381
1382         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1383         if (acl_obj) {
1384                 VERIFY(!zp->z_is_sa);
1385                 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1386         }
1387         VERIFY(0 == dmu_object_free(os, obj, tx));
1388         zfs_znode_dmu_fini(zp);
1389         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1390         zfs_znode_free(zp);
1391 }
1392
1393 void
1394 zfs_zinactive(znode_t *zp)
1395 {
1396         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1397         uint64_t z_id = zp->z_id;
1398
1399         ASSERT(zp->z_sa_hdl);
1400
1401         /*
1402          * Don't allow a zfs_zget() while were trying to release this znode
1403          */
1404         ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1405
1406         mutex_enter(&zp->z_lock);
1407
1408         /*
1409          * If this was the last reference to a file with no links,
1410          * remove the file from the file system.
1411          */
1412         if (zp->z_unlinked) {
1413                 mutex_exit(&zp->z_lock);
1414                 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1415                 zfs_rmnode(zp);
1416                 return;
1417         }
1418
1419         mutex_exit(&zp->z_lock);
1420         zfs_znode_dmu_fini(zp);
1421         ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1422         zfs_znode_free(zp);
1423 }
1424
1425 void
1426 zfs_znode_free(znode_t *zp)
1427 {
1428         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1429
1430         ASSERT(zp->z_sa_hdl == NULL);
1431         zp->z_vnode = NULL;
1432         mutex_enter(&zfsvfs->z_znodes_lock);
1433         POINTER_INVALIDATE(&zp->z_zfsvfs);
1434         list_remove(&zfsvfs->z_all_znodes, zp);
1435         mutex_exit(&zfsvfs->z_znodes_lock);
1436
1437         if (zp->z_acl_cached) {
1438                 zfs_acl_free(zp->z_acl_cached);
1439                 zp->z_acl_cached = NULL;
1440         }
1441
1442         kmem_cache_free(znode_cache, zp);
1443
1444         VFS_RELE(zfsvfs->z_vfs);
1445 }
1446
1447 void
1448 zfs_tstamp_update_setup(znode_t *zp, uint_t flag, uint64_t mtime[2],
1449     uint64_t ctime[2], boolean_t have_tx)
1450 {
1451         timestruc_t     now;
1452
1453         gethrestime(&now);
1454
1455         if (have_tx) {  /* will sa_bulk_update happen really soon? */
1456                 zp->z_atime_dirty = 0;
1457                 zp->z_seq++;
1458         } else {
1459                 zp->z_atime_dirty = 1;
1460         }
1461
1462         if (flag & AT_ATIME) {
1463                 ZFS_TIME_ENCODE(&now, zp->z_atime);
1464         }
1465
1466         if (flag & AT_MTIME) {
1467                 ZFS_TIME_ENCODE(&now, mtime);
1468                 if (zp->z_zfsvfs->z_use_fuids) {
1469                         zp->z_pflags |= (ZFS_ARCHIVE |
1470                             ZFS_AV_MODIFIED);
1471                 }
1472         }
1473
1474         if (flag & AT_CTIME) {
1475                 ZFS_TIME_ENCODE(&now, ctime);
1476                 if (zp->z_zfsvfs->z_use_fuids)
1477                         zp->z_pflags |= ZFS_ARCHIVE;
1478         }
1479 }
1480
1481 /*
1482  * Grow the block size for a file.
1483  *
1484  *      IN:     zp      - znode of file to free data in.
1485  *              size    - requested block size
1486  *              tx      - open transaction.
1487  *
1488  * NOTE: this function assumes that the znode is write locked.
1489  */
1490 void
1491 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1492 {
1493         int             error;
1494         u_longlong_t    dummy;
1495
1496         if (size <= zp->z_blksz)
1497                 return;
1498         /*
1499          * If the file size is already greater than the current blocksize,
1500          * we will not grow.  If there is more than one block in a file,
1501          * the blocksize cannot change.
1502          */
1503         if (zp->z_blksz && zp->z_size > zp->z_blksz)
1504                 return;
1505
1506         error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1507             size, 0, tx);
1508
1509         if (error == ENOTSUP)
1510                 return;
1511         ASSERT0(error);
1512
1513         /* What blocksize did we actually get? */
1514         dmu_object_size_from_db(sa_get_db(zp->z_sa_hdl), &zp->z_blksz, &dummy);
1515 }
1516
1517 #ifdef sun
1518 /*
1519  * This is a dummy interface used when pvn_vplist_dirty() should *not*
1520  * be calling back into the fs for a putpage().  E.g.: when truncating
1521  * a file, the pages being "thrown away* don't need to be written out.
1522  */
1523 /* ARGSUSED */
1524 static int
1525 zfs_no_putpage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
1526     int flags, cred_t *cr)
1527 {
1528         ASSERT(0);
1529         return (0);
1530 }
1531 #endif  /* sun */
1532
1533 /*
1534  * Increase the file length
1535  *
1536  *      IN:     zp      - znode of file to free data in.
1537  *              end     - new end-of-file
1538  *
1539  *      RETURN: 0 on success, error code on failure
1540  */
1541 static int
1542 zfs_extend(znode_t *zp, uint64_t end)
1543 {
1544         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1545         dmu_tx_t *tx;
1546         rl_t *rl;
1547         uint64_t newblksz;
1548         int error;
1549
1550         /*
1551          * We will change zp_size, lock the whole file.
1552          */
1553         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1554
1555         /*
1556          * Nothing to do if file already at desired length.
1557          */
1558         if (end <= zp->z_size) {
1559                 zfs_range_unlock(rl);
1560                 return (0);
1561         }
1562 top:
1563         tx = dmu_tx_create(zfsvfs->z_os);
1564         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1565         zfs_sa_upgrade_txholds(tx, zp);
1566         if (end > zp->z_blksz &&
1567             (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1568                 /*
1569                  * We are growing the file past the current block size.
1570                  */
1571                 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1572                         ASSERT(!ISP2(zp->z_blksz));
1573                         newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1574                 } else {
1575                         newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1576                 }
1577                 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1578         } else {
1579                 newblksz = 0;
1580         }
1581
1582         error = dmu_tx_assign(tx, TXG_NOWAIT);
1583         if (error) {
1584                 if (error == ERESTART) {
1585                         dmu_tx_wait(tx);
1586                         dmu_tx_abort(tx);
1587                         goto top;
1588                 }
1589                 dmu_tx_abort(tx);
1590                 zfs_range_unlock(rl);
1591                 return (error);
1592         }
1593
1594         if (newblksz)
1595                 zfs_grow_blocksize(zp, newblksz, tx);
1596
1597         zp->z_size = end;
1598
1599         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zp->z_zfsvfs),
1600             &zp->z_size, sizeof (zp->z_size), tx));
1601
1602         vnode_pager_setsize(ZTOV(zp), end);
1603
1604         zfs_range_unlock(rl);
1605
1606         dmu_tx_commit(tx);
1607
1608         return (0);
1609 }
1610
1611 /*
1612  * Free space in a file.
1613  *
1614  *      IN:     zp      - znode of file to free data in.
1615  *              off     - start of section to free.
1616  *              len     - length of section to free.
1617  *
1618  *      RETURN: 0 on success, error code on failure
1619  */
1620 static int
1621 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1622 {
1623         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1624         rl_t *rl;
1625         int error;
1626
1627         /*
1628          * Lock the range being freed.
1629          */
1630         rl = zfs_range_lock(zp, off, len, RL_WRITER);
1631
1632         /*
1633          * Nothing to do if file already at desired length.
1634          */
1635         if (off >= zp->z_size) {
1636                 zfs_range_unlock(rl);
1637                 return (0);
1638         }
1639
1640         if (off + len > zp->z_size)
1641                 len = zp->z_size - off;
1642
1643         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1644
1645         if (error == 0) {
1646                 /*
1647                  * In FreeBSD we cannot free block in the middle of a file,
1648                  * but only at the end of a file, so this code path should
1649                  * never happen.
1650                  */
1651                 vnode_pager_setsize(ZTOV(zp), off);
1652         }
1653
1654         zfs_range_unlock(rl);
1655
1656         return (error);
1657 }
1658
1659 /*
1660  * Truncate a file
1661  *
1662  *      IN:     zp      - znode of file to free data in.
1663  *              end     - new end-of-file.
1664  *
1665  *      RETURN: 0 on success, error code on failure
1666  */
1667 static int
1668 zfs_trunc(znode_t *zp, uint64_t end)
1669 {
1670         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1671         vnode_t *vp = ZTOV(zp);
1672         dmu_tx_t *tx;
1673         rl_t *rl;
1674         int error;
1675         sa_bulk_attr_t bulk[2];
1676         int count = 0;
1677
1678         /*
1679          * We will change zp_size, lock the whole file.
1680          */
1681         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1682
1683         /*
1684          * Nothing to do if file already at desired length.
1685          */
1686         if (end >= zp->z_size) {
1687                 zfs_range_unlock(rl);
1688                 return (0);
1689         }
1690
1691         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1692         if (error) {
1693                 zfs_range_unlock(rl);
1694                 return (error);
1695         }
1696 top:
1697         tx = dmu_tx_create(zfsvfs->z_os);
1698         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1699         zfs_sa_upgrade_txholds(tx, zp);
1700         error = dmu_tx_assign(tx, TXG_NOWAIT);
1701         if (error) {
1702                 if (error == ERESTART) {
1703                         dmu_tx_wait(tx);
1704                         dmu_tx_abort(tx);
1705                         goto top;
1706                 }
1707                 dmu_tx_abort(tx);
1708                 zfs_range_unlock(rl);
1709                 return (error);
1710         }
1711
1712         zp->z_size = end;
1713         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1714             NULL, &zp->z_size, sizeof (zp->z_size));
1715
1716         if (end == 0) {
1717                 zp->z_pflags &= ~ZFS_SPARSE;
1718                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1719                     NULL, &zp->z_pflags, 8);
1720         }
1721         VERIFY(sa_bulk_update(zp->z_sa_hdl, bulk, count, tx) == 0);
1722
1723         dmu_tx_commit(tx);
1724
1725         /*
1726          * Clear any mapped pages in the truncated region.  This has to
1727          * happen outside of the transaction to avoid the possibility of
1728          * a deadlock with someone trying to push a page that we are
1729          * about to invalidate.
1730          */
1731         vnode_pager_setsize(vp, end);
1732
1733         zfs_range_unlock(rl);
1734
1735         return (0);
1736 }
1737
1738 /*
1739  * Free space in a file
1740  *
1741  *      IN:     zp      - znode of file to free data in.
1742  *              off     - start of range
1743  *              len     - end of range (0 => EOF)
1744  *              flag    - current file open mode flags.
1745  *              log     - TRUE if this action should be logged
1746  *
1747  *      RETURN: 0 on success, error code on failure
1748  */
1749 int
1750 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1751 {
1752         vnode_t *vp = ZTOV(zp);
1753         dmu_tx_t *tx;
1754         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1755         zilog_t *zilog = zfsvfs->z_log;
1756         uint64_t mode;
1757         uint64_t mtime[2], ctime[2];
1758         sa_bulk_attr_t bulk[3];
1759         int count = 0;
1760         int error;
1761
1762         if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), &mode,
1763             sizeof (mode))) != 0)
1764                 return (error);
1765
1766         if (off > zp->z_size) {
1767                 error =  zfs_extend(zp, off+len);
1768                 if (error == 0 && log)
1769                         goto log;
1770                 else
1771                         return (error);
1772         }
1773
1774         /*
1775          * Check for any locks in the region to be freed.
1776          */
1777
1778         if (MANDLOCK(vp, (mode_t)mode)) {
1779                 uint64_t length = (len ? len : zp->z_size - off);
1780                 if (error = chklock(vp, FWRITE, off, length, flag, NULL))
1781                         return (error);
1782         }
1783
1784         if (len == 0) {
1785                 error = zfs_trunc(zp, off);
1786         } else {
1787                 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1788                     off + len > zp->z_size)
1789                         error = zfs_extend(zp, off+len);
1790         }
1791         if (error || !log)
1792                 return (error);
1793 log:
1794         tx = dmu_tx_create(zfsvfs->z_os);
1795         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1796         zfs_sa_upgrade_txholds(tx, zp);
1797         error = dmu_tx_assign(tx, TXG_NOWAIT);
1798         if (error) {
1799                 if (error == ERESTART) {
1800                         dmu_tx_wait(tx);
1801                         dmu_tx_abort(tx);
1802                         goto log;
1803                 }
1804                 dmu_tx_abort(tx);
1805                 return (error);
1806         }
1807
1808         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, mtime, 16);
1809         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, ctime, 16);
1810         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1811             NULL, &zp->z_pflags, 8);
1812         zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
1813         error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
1814         ASSERT(error == 0);
1815
1816         zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1817
1818         dmu_tx_commit(tx);
1819         return (0);
1820 }
1821
1822 void
1823 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1824 {
1825         zfsvfs_t        zfsvfs;
1826         uint64_t        moid, obj, sa_obj, version;
1827         uint64_t        sense = ZFS_CASE_SENSITIVE;
1828         uint64_t        norm = 0;
1829         nvpair_t        *elem;
1830         int             error;
1831         int             i;
1832         znode_t         *rootzp = NULL;
1833         vnode_t         vnode;
1834         vattr_t         vattr;
1835         znode_t         *zp;
1836         zfs_acl_ids_t   acl_ids;
1837
1838         /*
1839          * First attempt to create master node.
1840          */
1841         /*
1842          * In an empty objset, there are no blocks to read and thus
1843          * there can be no i/o errors (which we assert below).
1844          */
1845         moid = MASTER_NODE_OBJ;
1846         error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1847             DMU_OT_NONE, 0, tx);
1848         ASSERT(error == 0);
1849
1850         /*
1851          * Set starting attributes.
1852          */
1853         version = zfs_zpl_version_map(spa_version(dmu_objset_spa(os)));
1854         elem = NULL;
1855         while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1856                 /* For the moment we expect all zpl props to be uint64_ts */
1857                 uint64_t val;
1858                 char *name;
1859
1860                 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1861                 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1862                 name = nvpair_name(elem);
1863                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1864                         if (val < version)
1865                                 version = val;
1866                 } else {
1867                         error = zap_update(os, moid, name, 8, 1, &val, tx);
1868                 }
1869                 ASSERT(error == 0);
1870                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1871                         norm = val;
1872                 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1873                         sense = val;
1874         }
1875         ASSERT(version != 0);
1876         error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1877
1878         /*
1879          * Create zap object used for SA attribute registration
1880          */
1881
1882         if (version >= ZPL_VERSION_SA) {
1883                 sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
1884                     DMU_OT_NONE, 0, tx);
1885                 error = zap_add(os, moid, ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
1886                 ASSERT(error == 0);
1887         } else {
1888                 sa_obj = 0;
1889         }
1890         /*
1891          * Create a delete queue.
1892          */
1893         obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1894
1895         error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1896         ASSERT(error == 0);
1897
1898         /*
1899          * Create root znode.  Create minimal znode/vnode/zfsvfs
1900          * to allow zfs_mknode to work.
1901          */
1902         VATTR_NULL(&vattr);
1903         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1904         vattr.va_type = VDIR;
1905         vattr.va_mode = S_IFDIR|0755;
1906         vattr.va_uid = crgetuid(cr);
1907         vattr.va_gid = crgetgid(cr);
1908
1909         bzero(&zfsvfs, sizeof (zfsvfs_t));
1910
1911         rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1912         zfs_znode_cache_constructor(rootzp, NULL, 0);
1913         ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1914         rootzp->z_moved = 0;
1915         rootzp->z_unlinked = 0;
1916         rootzp->z_atime_dirty = 0;
1917         rootzp->z_is_sa = USE_SA(version, os);
1918
1919         vnode.v_type = VDIR;
1920         vnode.v_data = rootzp;
1921         rootzp->z_vnode = &vnode;
1922
1923         zfsvfs.z_os = os;
1924         zfsvfs.z_parent = &zfsvfs;
1925         zfsvfs.z_version = version;
1926         zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1927         zfsvfs.z_use_sa = USE_SA(version, os);
1928         zfsvfs.z_norm = norm;
1929
1930         error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
1931             &zfsvfs.z_attr_table);
1932
1933         ASSERT(error == 0);
1934
1935         /*
1936          * Fold case on file systems that are always or sometimes case
1937          * insensitive.
1938          */
1939         if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1940                 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1941
1942         mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1943         list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1944             offsetof(znode_t, z_link_node));
1945
1946         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1947                 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1948
1949         rootzp->z_zfsvfs = &zfsvfs;
1950         VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1951             cr, NULL, &acl_ids));
1952         zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, &acl_ids);
1953         ASSERT3P(zp, ==, rootzp);
1954         error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1955         ASSERT(error == 0);
1956         zfs_acl_ids_free(&acl_ids);
1957         POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1958
1959         sa_handle_destroy(rootzp->z_sa_hdl);
1960         rootzp->z_vnode = NULL;
1961         kmem_cache_free(znode_cache, rootzp);
1962
1963         /*
1964          * Create shares directory
1965          */
1966
1967         error = zfs_create_share_dir(&zfsvfs, tx);
1968
1969         ASSERT(error == 0);
1970
1971         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1972                 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1973 }
1974
1975 #endif /* _KERNEL */
1976
1977 static int
1978 zfs_sa_setup(objset_t *osp, sa_attr_type_t **sa_table)
1979 {
1980         uint64_t sa_obj = 0;
1981         int error;
1982
1983         error = zap_lookup(osp, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1, &sa_obj);
1984         if (error != 0 && error != ENOENT)
1985                 return (error);
1986
1987         error = sa_setup(osp, sa_obj, zfs_attr_table, ZPL_END, sa_table);
1988         return (error);
1989 }
1990
1991 static int
1992 zfs_grab_sa_handle(objset_t *osp, uint64_t obj, sa_handle_t **hdlp,
1993     dmu_buf_t **db, void *tag)
1994 {
1995         dmu_object_info_t doi;
1996         int error;
1997
1998         if ((error = sa_buf_hold(osp, obj, tag, db)) != 0)
1999                 return (error);
2000
2001         dmu_object_info_from_db(*db, &doi);
2002         if ((doi.doi_bonus_type != DMU_OT_SA &&
2003             doi.doi_bonus_type != DMU_OT_ZNODE) ||
2004             doi.doi_bonus_type == DMU_OT_ZNODE &&
2005             doi.doi_bonus_size < sizeof (znode_phys_t)) {
2006                 sa_buf_rele(*db, tag);
2007                 return (SET_ERROR(ENOTSUP));
2008         }
2009
2010         error = sa_handle_get(osp, obj, NULL, SA_HDL_PRIVATE, hdlp);
2011         if (error != 0) {
2012                 sa_buf_rele(*db, tag);
2013                 return (error);
2014         }
2015
2016         return (0);
2017 }
2018
2019 void
2020 zfs_release_sa_handle(sa_handle_t *hdl, dmu_buf_t *db, void *tag)
2021 {
2022         sa_handle_destroy(hdl);
2023         sa_buf_rele(db, tag);
2024 }
2025
2026 /*
2027  * Given an object number, return its parent object number and whether
2028  * or not the object is an extended attribute directory.
2029  */
2030 static int
2031 zfs_obj_to_pobj(objset_t *osp, sa_handle_t *hdl, sa_attr_type_t *sa_table,
2032     uint64_t *pobjp, int *is_xattrdir)
2033 {
2034         uint64_t parent;
2035         uint64_t pflags;
2036         uint64_t mode;
2037         uint64_t parent_mode;
2038         sa_bulk_attr_t bulk[3];
2039         sa_handle_t *sa_hdl;
2040         dmu_buf_t *sa_db;
2041         int count = 0;
2042         int error;
2043
2044         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_PARENT], NULL,
2045             &parent, sizeof (parent));
2046         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_FLAGS], NULL,
2047             &pflags, sizeof (pflags));
2048         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2049             &mode, sizeof (mode));
2050
2051         if ((error = sa_bulk_lookup(hdl, bulk, count)) != 0)
2052                 return (error);
2053
2054         /*
2055          * When a link is removed its parent pointer is not changed and will
2056          * be invalid.  There are two cases where a link is removed but the
2057          * file stays around, when it goes to the delete queue and when there
2058          * are additional links.
2059          */
2060         error = zfs_grab_sa_handle(osp, parent, &sa_hdl, &sa_db, FTAG);
2061         if (error != 0)
2062                 return (error);
2063
2064         error = sa_lookup(sa_hdl, ZPL_MODE, &parent_mode, sizeof (parent_mode));
2065         zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2066         if (error != 0)
2067                 return (error);
2068
2069         *is_xattrdir = ((pflags & ZFS_XATTR) != 0) && S_ISDIR(mode);
2070
2071         /*
2072          * Extended attributes can be applied to files, directories, etc.
2073          * Otherwise the parent must be a directory.
2074          */
2075         if (!*is_xattrdir && !S_ISDIR(parent_mode))
2076                 return (SET_ERROR(EINVAL));
2077
2078         *pobjp = parent;
2079
2080         return (0);
2081 }
2082
2083 /*
2084  * Given an object number, return some zpl level statistics
2085  */
2086 static int
2087 zfs_obj_to_stats_impl(sa_handle_t *hdl, sa_attr_type_t *sa_table,
2088     zfs_stat_t *sb)
2089 {
2090         sa_bulk_attr_t bulk[4];
2091         int count = 0;
2092
2093         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_MODE], NULL,
2094             &sb->zs_mode, sizeof (sb->zs_mode));
2095         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_GEN], NULL,
2096             &sb->zs_gen, sizeof (sb->zs_gen));
2097         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_LINKS], NULL,
2098             &sb->zs_links, sizeof (sb->zs_links));
2099         SA_ADD_BULK_ATTR(bulk, count, sa_table[ZPL_CTIME], NULL,
2100             &sb->zs_ctime, sizeof (sb->zs_ctime));
2101
2102         return (sa_bulk_lookup(hdl, bulk, count));
2103 }
2104
2105 static int
2106 zfs_obj_to_path_impl(objset_t *osp, uint64_t obj, sa_handle_t *hdl,
2107     sa_attr_type_t *sa_table, char *buf, int len)
2108 {
2109         sa_handle_t *sa_hdl;
2110         sa_handle_t *prevhdl = NULL;
2111         dmu_buf_t *prevdb = NULL;
2112         dmu_buf_t *sa_db = NULL;
2113         char *path = buf + len - 1;
2114         int error;
2115
2116         *path = '\0';
2117         sa_hdl = hdl;
2118
2119         for (;;) {
2120                 uint64_t pobj;
2121                 char component[MAXNAMELEN + 2];
2122                 size_t complen;
2123                 int is_xattrdir;
2124
2125                 if (prevdb)
2126                         zfs_release_sa_handle(prevhdl, prevdb, FTAG);
2127
2128                 if ((error = zfs_obj_to_pobj(osp, sa_hdl, sa_table, &pobj,
2129                     &is_xattrdir)) != 0)
2130                         break;
2131
2132                 if (pobj == obj) {
2133                         if (path[0] != '/')
2134                                 *--path = '/';
2135                         break;
2136                 }
2137
2138                 component[0] = '/';
2139                 if (is_xattrdir) {
2140                         (void) sprintf(component + 1, "<xattrdir>");
2141                 } else {
2142                         error = zap_value_search(osp, pobj, obj,
2143                             ZFS_DIRENT_OBJ(-1ULL), component + 1);
2144                         if (error != 0)
2145                                 break;
2146                 }
2147
2148                 complen = strlen(component);
2149                 path -= complen;
2150                 ASSERT(path >= buf);
2151                 bcopy(component, path, complen);
2152                 obj = pobj;
2153
2154                 if (sa_hdl != hdl) {
2155                         prevhdl = sa_hdl;
2156                         prevdb = sa_db;
2157                 }
2158                 error = zfs_grab_sa_handle(osp, obj, &sa_hdl, &sa_db, FTAG);
2159                 if (error != 0) {
2160                         sa_hdl = prevhdl;
2161                         sa_db = prevdb;
2162                         break;
2163                 }
2164         }
2165
2166         if (sa_hdl != NULL && sa_hdl != hdl) {
2167                 ASSERT(sa_db != NULL);
2168                 zfs_release_sa_handle(sa_hdl, sa_db, FTAG);
2169         }
2170
2171         if (error == 0)
2172                 (void) memmove(buf, path, buf + len - path);
2173
2174         return (error);
2175 }
2176
2177 int
2178 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
2179 {
2180         sa_attr_type_t *sa_table;
2181         sa_handle_t *hdl;
2182         dmu_buf_t *db;
2183         int error;
2184
2185         error = zfs_sa_setup(osp, &sa_table);
2186         if (error != 0)
2187                 return (error);
2188
2189         error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2190         if (error != 0)
2191                 return (error);
2192
2193         error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2194
2195         zfs_release_sa_handle(hdl, db, FTAG);
2196         return (error);
2197 }
2198
2199 int
2200 zfs_obj_to_stats(objset_t *osp, uint64_t obj, zfs_stat_t *sb,
2201     char *buf, int len)
2202 {
2203         char *path = buf + len - 1;
2204         sa_attr_type_t *sa_table;
2205         sa_handle_t *hdl;
2206         dmu_buf_t *db;
2207         int error;
2208
2209         *path = '\0';
2210
2211         error = zfs_sa_setup(osp, &sa_table);
2212         if (error != 0)
2213                 return (error);
2214
2215         error = zfs_grab_sa_handle(osp, obj, &hdl, &db, FTAG);
2216         if (error != 0)
2217                 return (error);
2218
2219         error = zfs_obj_to_stats_impl(hdl, sa_table, sb);
2220         if (error != 0) {
2221                 zfs_release_sa_handle(hdl, db, FTAG);
2222                 return (error);
2223         }
2224
2225         error = zfs_obj_to_path_impl(osp, obj, hdl, sa_table, buf, len);
2226
2227         zfs_release_sa_handle(hdl, db, FTAG);
2228         return (error);
2229 }