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