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