]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_znode.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / zfs_znode.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 /* Portions Copyright 2007 Jeremy Teo */
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/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/refcount.h>
60
61 #include "zfs_prop.h"
62
63 /* Used by fstat(1). */
64 SYSCTL_INT(_debug_sizeof, OID_AUTO, znode, CTLFLAG_RD, 0, sizeof(znode_t),
65     "sizeof(znode_t)");
66
67 /*
68  * Define ZNODE_STATS to turn on statistic gathering. By default, it is only
69  * turned on when DEBUG is also defined.
70  */
71 #ifdef  DEBUG
72 #define ZNODE_STATS
73 #endif  /* DEBUG */
74
75 #ifdef  ZNODE_STATS
76 #define ZNODE_STAT_ADD(stat)                    ((stat)++)
77 #else
78 #define ZNODE_STAT_ADD(stat)                    /* nothing */
79 #endif  /* ZNODE_STATS */
80
81 #define POINTER_IS_VALID(p)     (!((uintptr_t)(p) & 0x3))
82 #define POINTER_INVALIDATE(pp)  (*(pp) = (void *)((uintptr_t)(*(pp)) | 0x1))
83
84 /*
85  * Functions needed for userland (ie: libzpool) are not put under
86  * #ifdef_KERNEL; the rest of the functions have dependencies
87  * (such as VFS logic) that will not compile easily in userland.
88  */
89 #ifdef _KERNEL
90 /*
91  * Needed to close a small window in zfs_znode_move() that allows the zfsvfs to
92  * be freed before it can be safely accessed.
93  */
94 krwlock_t zfsvfs_lock;
95
96 static kmem_cache_t *znode_cache = NULL;
97
98 /*ARGSUSED*/
99 static void
100 znode_evict_error(dmu_buf_t *dbuf, void *user_ptr)
101 {
102 #if 1   /* XXXPJD: From OpenSolaris. */
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 #else   /* XXXPJD */
109         znode_t *zp = user_ptr;
110         vnode_t *vp;
111
112         mutex_enter(&zp->z_lock);
113         zp->z_dbuf = NULL;
114         vp = ZTOV(zp);
115         if (vp == NULL) {
116                 mutex_exit(&zp->z_lock);
117                 zfs_znode_free(zp);
118         } else if (vp->v_count == 0) {
119                 zp->z_vnode = NULL;
120                 vhold(vp);
121                 mutex_exit(&zp->z_lock);
122                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
123                 vrecycle(vp, curthread);
124                 VOP_UNLOCK(vp, 0);
125                 vdrop(vp);
126                 zfs_znode_free(zp);
127         } else {
128                 mutex_exit(&zp->z_lock);
129         }
130 #endif
131 }
132
133 extern struct vop_vector zfs_vnodeops;
134 extern struct vop_vector zfs_fifoops;
135 extern struct vop_vector zfs_shareops;
136
137 /*
138  * XXX: We cannot use this function as a cache constructor, because
139  *      there is one global cache for all file systems and we need
140  *      to pass vfsp here, which is not possible, because argument
141  *      'cdrarg' is defined at kmem_cache_create() time.
142  */
143 static int
144 zfs_znode_cache_constructor(void *buf, void *arg, int kmflags)
145 {
146         znode_t *zp = buf;
147         vnode_t *vp;
148         vfs_t *vfsp = arg;
149         int error;
150
151         POINTER_INVALIDATE(&zp->z_zfsvfs);
152         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
153
154         if (vfsp != NULL) {
155                 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
156                 if (error != 0 && (kmflags & KM_NOSLEEP))
157                         return (-1);
158                 ASSERT(error == 0);
159                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
160                 zp->z_vnode = vp;
161                 vp->v_data = (caddr_t)zp;
162                 VN_LOCK_AREC(vp);
163         } else {
164                 zp->z_vnode = NULL;
165         }
166
167         list_link_init(&zp->z_link_node);
168
169         mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
170         rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
171         rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
172         mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
173
174         mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
175         avl_create(&zp->z_range_avl, zfs_range_compare,
176             sizeof (rl_t), offsetof(rl_t, r_node));
177
178         zp->z_dbuf = NULL;
179         zp->z_dirlocks = NULL;
180         zp->z_acl_cached = NULL;
181         return (0);
182 }
183
184 /*ARGSUSED*/
185 static void
186 zfs_znode_cache_destructor(void *buf, void *arg)
187 {
188         znode_t *zp = buf;
189
190         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
191         ASSERT(ZTOV(zp) == NULL);
192         vn_free(ZTOV(zp));
193         ASSERT(!list_link_active(&zp->z_link_node));
194         mutex_destroy(&zp->z_lock);
195         rw_destroy(&zp->z_parent_lock);
196         rw_destroy(&zp->z_name_lock);
197         mutex_destroy(&zp->z_acl_lock);
198         avl_destroy(&zp->z_range_avl);
199         mutex_destroy(&zp->z_range_lock);
200
201         ASSERT(zp->z_dbuf == NULL);
202         ASSERT(zp->z_dirlocks == NULL);
203         ASSERT(zp->z_acl_cached == NULL);
204 }
205
206 #ifdef  ZNODE_STATS
207 static struct {
208         uint64_t zms_zfsvfs_invalid;
209         uint64_t zms_zfsvfs_recheck1;
210         uint64_t zms_zfsvfs_unmounted;
211         uint64_t zms_zfsvfs_recheck2;
212         uint64_t zms_obj_held;
213         uint64_t zms_vnode_locked;
214         uint64_t zms_not_only_dnlc;
215 } znode_move_stats;
216 #endif  /* ZNODE_STATS */
217
218 #if defined(sun)
219 static void
220 zfs_znode_move_impl(znode_t *ozp, znode_t *nzp)
221 {
222         vnode_t *vp;
223
224         /* Copy fields. */
225         nzp->z_zfsvfs = ozp->z_zfsvfs;
226
227         /* Swap vnodes. */
228         vp = nzp->z_vnode;
229         nzp->z_vnode = ozp->z_vnode;
230         ozp->z_vnode = vp; /* let destructor free the overwritten vnode */
231         ZTOV(ozp)->v_data = ozp;
232         ZTOV(nzp)->v_data = nzp;
233
234         nzp->z_id = ozp->z_id;
235         ASSERT(ozp->z_dirlocks == NULL); /* znode not in use */
236         ASSERT(avl_numnodes(&ozp->z_range_avl) == 0);
237         nzp->z_unlinked = ozp->z_unlinked;
238         nzp->z_atime_dirty = ozp->z_atime_dirty;
239         nzp->z_zn_prefetch = ozp->z_zn_prefetch;
240         nzp->z_blksz = ozp->z_blksz;
241         nzp->z_seq = ozp->z_seq;
242         nzp->z_mapcnt = ozp->z_mapcnt;
243         nzp->z_last_itx = ozp->z_last_itx;
244         nzp->z_gen = ozp->z_gen;
245         nzp->z_sync_cnt = ozp->z_sync_cnt;
246         nzp->z_phys = ozp->z_phys;
247         nzp->z_dbuf = ozp->z_dbuf;
248
249         /*
250          * Since this is just an idle znode and kmem is already dealing with
251          * memory pressure, release any cached ACL.
252          */
253         if (ozp->z_acl_cached) {
254                 zfs_acl_free(ozp->z_acl_cached);
255                 ozp->z_acl_cached = NULL;
256         }
257
258         /* Update back pointers. */
259         (void) dmu_buf_update_user(nzp->z_dbuf, ozp, nzp, &nzp->z_phys,
260             znode_evict_error);
261
262         /*
263          * Invalidate the original znode by clearing fields that provide a
264          * pointer back to the znode. Set the low bit of the vfs pointer to
265          * ensure that zfs_znode_move() recognizes the znode as invalid in any
266          * subsequent callback.
267          */
268         ozp->z_dbuf = NULL;
269         POINTER_INVALIDATE(&ozp->z_zfsvfs);
270 }
271
272 /*ARGSUSED*/
273 static kmem_cbrc_t
274 zfs_znode_move(void *buf, void *newbuf, size_t size, void *arg)
275 {
276         znode_t *ozp = buf, *nzp = newbuf;
277         zfsvfs_t *zfsvfs;
278         vnode_t *vp;
279
280         /*
281          * The znode is on the file system's list of known znodes if the vfs
282          * pointer is valid. We set the low bit of the vfs pointer when freeing
283          * the znode to invalidate it, and the memory patterns written by kmem
284          * (baddcafe and deadbeef) set at least one of the two low bits. A newly
285          * created znode sets the vfs pointer last of all to indicate that the
286          * znode is known and in a valid state to be moved by this function.
287          */
288         zfsvfs = ozp->z_zfsvfs;
289         if (!POINTER_IS_VALID(zfsvfs)) {
290                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_invalid);
291                 return (KMEM_CBRC_DONT_KNOW);
292         }
293
294         /*
295          * Close a small window in which it's possible that the filesystem could
296          * be unmounted and freed, and zfsvfs, though valid in the previous
297          * statement, could point to unrelated memory by the time we try to
298          * prevent the filesystem from being unmounted.
299          */
300         rw_enter(&zfsvfs_lock, RW_WRITER);
301         if (zfsvfs != ozp->z_zfsvfs) {
302                 rw_exit(&zfsvfs_lock);
303                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck1);
304                 return (KMEM_CBRC_DONT_KNOW);
305         }
306
307         /*
308          * If the znode is still valid, then so is the file system. We know that
309          * no valid file system can be freed while we hold zfsvfs_lock, so we
310          * can safely ensure that the filesystem is not and will not be
311          * unmounted. The next statement is equivalent to ZFS_ENTER().
312          */
313         rrw_enter(&zfsvfs->z_teardown_lock, RW_READER, FTAG);
314         if (zfsvfs->z_unmounted) {
315                 ZFS_EXIT(zfsvfs);
316                 rw_exit(&zfsvfs_lock);
317                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_unmounted);
318                 return (KMEM_CBRC_DONT_KNOW);
319         }
320         rw_exit(&zfsvfs_lock);
321
322         mutex_enter(&zfsvfs->z_znodes_lock);
323         /*
324          * Recheck the vfs pointer in case the znode was removed just before
325          * acquiring the lock.
326          */
327         if (zfsvfs != ozp->z_zfsvfs) {
328                 mutex_exit(&zfsvfs->z_znodes_lock);
329                 ZFS_EXIT(zfsvfs);
330                 ZNODE_STAT_ADD(znode_move_stats.zms_zfsvfs_recheck2);
331                 return (KMEM_CBRC_DONT_KNOW);
332         }
333
334         /*
335          * At this point we know that as long as we hold z_znodes_lock, the
336          * znode cannot be freed and fields within the znode can be safely
337          * accessed. Now, prevent a race with zfs_zget().
338          */
339         if (ZFS_OBJ_HOLD_TRYENTER(zfsvfs, ozp->z_id) == 0) {
340                 mutex_exit(&zfsvfs->z_znodes_lock);
341                 ZFS_EXIT(zfsvfs);
342                 ZNODE_STAT_ADD(znode_move_stats.zms_obj_held);
343                 return (KMEM_CBRC_LATER);
344         }
345
346         vp = ZTOV(ozp);
347         if (mutex_tryenter(&vp->v_lock) == 0) {
348                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
349                 mutex_exit(&zfsvfs->z_znodes_lock);
350                 ZFS_EXIT(zfsvfs);
351                 ZNODE_STAT_ADD(znode_move_stats.zms_vnode_locked);
352                 return (KMEM_CBRC_LATER);
353         }
354
355         /* Only move znodes that are referenced _only_ by the DNLC. */
356         if (vp->v_count != 1 || !vn_in_dnlc(vp)) {
357                 mutex_exit(&vp->v_lock);
358                 ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
359                 mutex_exit(&zfsvfs->z_znodes_lock);
360                 ZFS_EXIT(zfsvfs);
361                 ZNODE_STAT_ADD(znode_move_stats.zms_not_only_dnlc);
362                 return (KMEM_CBRC_LATER);
363         }
364
365         /*
366          * The znode is known and in a valid state to move. We're holding the
367          * locks needed to execute the critical section.
368          */
369         zfs_znode_move_impl(ozp, nzp);
370         mutex_exit(&vp->v_lock);
371         ZFS_OBJ_HOLD_EXIT(zfsvfs, ozp->z_id);
372
373         list_link_replace(&ozp->z_link_node, &nzp->z_link_node);
374         mutex_exit(&zfsvfs->z_znodes_lock);
375         ZFS_EXIT(zfsvfs);
376
377         return (KMEM_CBRC_YES);
378 }
379 #endif /* sun */
380
381 void
382 zfs_znode_init(void)
383 {
384         /*
385          * Initialize zcache
386          */
387         rw_init(&zfsvfs_lock, NULL, RW_DEFAULT, NULL);
388         ASSERT(znode_cache == NULL);
389         znode_cache = kmem_cache_create("zfs_znode_cache",
390             sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
391             zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
392 #if defined(sun)
393         kmem_cache_set_move(znode_cache, zfs_znode_move);
394 #endif
395 }
396
397 void
398 zfs_znode_fini(void)
399 {
400         /*
401          * Cleanup zcache
402          */
403         if (znode_cache)
404                 kmem_cache_destroy(znode_cache);
405         znode_cache = NULL;
406         rw_destroy(&zfsvfs_lock);
407 }
408
409 int
410 zfs_create_share_dir(zfsvfs_t *zfsvfs, dmu_tx_t *tx)
411 {
412         zfs_acl_ids_t acl_ids;
413         vattr_t vattr;
414         znode_t *sharezp;
415         vnode_t *vp, vnode;
416         znode_t *zp;
417         int error;
418
419         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
420         vattr.va_type = VDIR;
421         vattr.va_mode = S_IFDIR|0555;
422         vattr.va_uid = crgetuid(kcred);
423         vattr.va_gid = crgetgid(kcred);
424
425         sharezp = kmem_cache_alloc(znode_cache, KM_SLEEP);
426         zfs_znode_cache_constructor(sharezp, zfsvfs->z_parent->z_vfs, 0);
427         sharezp->z_unlinked = 0;
428         sharezp->z_atime_dirty = 0;
429         sharezp->z_zfsvfs = zfsvfs;
430
431         sharezp->z_vnode = &vnode;
432         vnode.v_data = sharezp;
433
434         vp = ZTOV(sharezp);
435         vp->v_type = VDIR;
436
437         VERIFY(0 == zfs_acl_ids_create(sharezp, IS_ROOT_NODE, &vattr,
438             kcred, NULL, &acl_ids));
439         zfs_mknode(sharezp, &vattr, tx, kcred, IS_ROOT_NODE,
440             &zp, 0, &acl_ids);
441         ASSERT3P(zp, ==, sharezp);
442         POINTER_INVALIDATE(&sharezp->z_zfsvfs);
443         error = zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
444             ZFS_SHARES_DIR, 8, 1, &sharezp->z_id, tx);
445         zfsvfs->z_shares_dir = sharezp->z_id;
446
447         zfs_acl_ids_free(&acl_ids);
448         ZTOV(sharezp)->v_data = NULL;
449         ZTOV(sharezp)->v_count = 0;
450         ZTOV(sharezp)->v_holdcnt = 0;
451         zp->z_vnode = NULL;
452         sharezp->z_vnode = NULL;
453         dmu_buf_rele(sharezp->z_dbuf, NULL);
454         sharezp->z_dbuf = NULL;
455         kmem_cache_free(znode_cache, sharezp);
456
457         return (error);
458 }
459
460 /*
461  * define a couple of values we need available
462  * for both 64 and 32 bit environments.
463  */
464 #ifndef NBITSMINOR64
465 #define NBITSMINOR64    32
466 #endif
467 #ifndef MAXMAJ64
468 #define MAXMAJ64        0xffffffffUL
469 #endif
470 #ifndef MAXMIN64
471 #define MAXMIN64        0xffffffffUL
472 #endif
473
474 /*
475  * Create special expldev for ZFS private use.
476  * Can't use standard expldev since it doesn't do
477  * what we want.  The standard expldev() takes a
478  * dev32_t in LP64 and expands it to a long dev_t.
479  * We need an interface that takes a dev32_t in ILP32
480  * and expands it to a long dev_t.
481  */
482 static uint64_t
483 zfs_expldev(dev_t dev)
484 {
485         return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
486 }
487 /*
488  * Special cmpldev for ZFS private use.
489  * Can't use standard cmpldev since it takes
490  * a long dev_t and compresses it to dev32_t in
491  * LP64.  We need to do a compaction of a long dev_t
492  * to a dev32_t in ILP32.
493  */
494 dev_t
495 zfs_cmpldev(uint64_t dev)
496 {
497         return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
498 }
499
500 static void
501 zfs_znode_dmu_init(zfsvfs_t *zfsvfs, znode_t *zp, dmu_buf_t *db)
502 {
503         znode_t         *nzp;
504
505         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs) || (zfsvfs == zp->z_zfsvfs));
506         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zfsvfs, zp->z_id)));
507
508         mutex_enter(&zp->z_lock);
509
510         ASSERT(zp->z_dbuf == NULL);
511         ASSERT(zp->z_acl_cached == NULL);
512         zp->z_dbuf = db;
513         nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_evict_error);
514
515         /*
516          * there should be no
517          * concurrent zgets on this object.
518          */
519         if (nzp != NULL)
520                 panic("existing znode %p for dbuf %p", (void *)nzp, (void *)db);
521
522         /*
523          * Slap on VROOT if we are the root znode
524          */
525         if (zp->z_id == zfsvfs->z_root)
526                 ZTOV(zp)->v_flag |= VROOT;
527
528         mutex_exit(&zp->z_lock);
529         vn_exists(ZTOV(zp));
530 }
531
532 void
533 zfs_znode_dmu_fini(znode_t *zp)
534 {
535         dmu_buf_t *db = zp->z_dbuf;
536         ASSERT(MUTEX_HELD(ZFS_OBJ_MUTEX(zp->z_zfsvfs, zp->z_id)) ||
537             zp->z_unlinked ||
538             RW_WRITE_HELD(&zp->z_zfsvfs->z_teardown_inactive_lock));
539         ASSERT(zp->z_dbuf != NULL);
540         zp->z_dbuf = NULL;
541         VERIFY(zp == dmu_buf_update_user(db, zp, NULL, NULL, NULL));
542         dmu_buf_rele(db, NULL);
543 }
544
545 /*
546  * Construct a new znode/vnode and intialize.
547  *
548  * This does not do a call to dmu_set_user() that is
549  * up to the caller to do, in case you don't want to
550  * return the znode
551  */
552 static znode_t *
553 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, int blksz)
554 {
555         znode_t *zp;
556         vnode_t *vp;
557
558         zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
559         zfs_znode_cache_constructor(zp, zfsvfs->z_parent->z_vfs, 0);
560
561         ASSERT(zp->z_dirlocks == NULL);
562         ASSERT(zp->z_dbuf == NULL);
563         ASSERT(!POINTER_IS_VALID(zp->z_zfsvfs));
564
565         /*
566          * Defer setting z_zfsvfs until the znode is ready to be a candidate for
567          * the zfs_znode_move() callback.
568          */
569         zp->z_phys = NULL;
570         zp->z_unlinked = 0;
571         zp->z_atime_dirty = 0;
572         zp->z_mapcnt = 0;
573         zp->z_last_itx = 0;
574         zp->z_id = db->db_object;
575         zp->z_blksz = blksz;
576         zp->z_seq = 0x7A4653;
577         zp->z_sync_cnt = 0;
578
579         vp = ZTOV(zp);
580 #ifdef TODO
581         vn_reinit(vp);
582 #endif
583
584         zfs_znode_dmu_init(zfsvfs, zp, db);
585
586         zp->z_gen = zp->z_phys->zp_gen;
587
588 #if 0
589         if (vp == NULL)
590                 return (zp);
591 #endif
592
593         vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
594         switch (vp->v_type) {
595         case VDIR:
596                 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
597                 break;
598         case VFIFO:
599                 vp->v_op = &zfs_fifoops;
600                 break;
601         case VREG:
602                 if (zp->z_phys->zp_parent == zfsvfs->z_shares_dir) {
603                         vp->v_op = &zfs_shareops;
604                 }
605                 break;
606         }
607         if (vp->v_type != VFIFO)
608                 VN_LOCK_ASHARE(vp);
609
610         mutex_enter(&zfsvfs->z_znodes_lock);
611         list_insert_tail(&zfsvfs->z_all_znodes, zp);
612         membar_producer();
613         /*
614          * Everything else must be valid before assigning z_zfsvfs makes the
615          * znode eligible for zfs_znode_move().
616          */
617         zp->z_zfsvfs = zfsvfs;
618         mutex_exit(&zfsvfs->z_znodes_lock);
619
620         VFS_HOLD(zfsvfs->z_vfs);
621         return (zp);
622 }
623
624 /*
625  * Create a new DMU object to hold a zfs znode.
626  *
627  *      IN:     dzp     - parent directory for new znode
628  *              vap     - file attributes for new znode
629  *              tx      - dmu transaction id for zap operations
630  *              cr      - credentials of caller
631  *              flag    - flags:
632  *                        IS_ROOT_NODE  - new object will be root
633  *                        IS_XATTR      - new object is an attribute
634  *              bonuslen - length of bonus buffer
635  *              setaclp  - File/Dir initial ACL
636  *              fuidp    - Tracks fuid allocation.
637  *
638  *      OUT:    zpp     - allocated znode
639  *
640  */
641 void
642 zfs_mknode(znode_t *dzp, vattr_t *vap, dmu_tx_t *tx, cred_t *cr,
643     uint_t flag, znode_t **zpp, int bonuslen, zfs_acl_ids_t *acl_ids)
644 {
645         dmu_buf_t       *db;
646         znode_phys_t    *pzp;
647         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
648         timestruc_t     now;
649         uint64_t        gen, obj;
650         int             err;
651
652         ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
653
654         if (zfsvfs->z_replay) {
655                 obj = vap->va_nodeid;
656                 now = vap->va_ctime;            /* see zfs_replay_create() */
657                 gen = vap->va_nblocks;          /* ditto */
658         } else {
659                 obj = 0;
660                 gethrestime(&now);
661                 gen = dmu_tx_get_txg(tx);
662         }
663
664         /*
665          * Create a new DMU object.
666          */
667         /*
668          * There's currently no mechanism for pre-reading the blocks that will
669          * be to needed allocate a new object, so we accept the small chance
670          * that there will be an i/o error and we will fail one of the
671          * assertions below.
672          */
673         if (vap->va_type == VDIR) {
674                 if (zfsvfs->z_replay) {
675                         err = zap_create_claim_norm(zfsvfs->z_os, obj,
676                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
677                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
678                         ASSERT3U(err, ==, 0);
679                 } else {
680                         obj = zap_create_norm(zfsvfs->z_os,
681                             zfsvfs->z_norm, DMU_OT_DIRECTORY_CONTENTS,
682                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
683                 }
684         } else {
685                 if (zfsvfs->z_replay) {
686                         err = dmu_object_claim(zfsvfs->z_os, obj,
687                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
688                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
689                         ASSERT3U(err, ==, 0);
690                 } else {
691                         obj = dmu_object_alloc(zfsvfs->z_os,
692                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
693                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
694                 }
695         }
696
697         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
698         VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, obj, NULL, &db));
699         dmu_buf_will_dirty(db, tx);
700
701         /*
702          * Initialize the znode physical data to zero.
703          */
704         ASSERT(db->db_size >= sizeof (znode_phys_t));
705         bzero(db->db_data, db->db_size);
706         pzp = db->db_data;
707
708         /*
709          * If this is the root, fix up the half-initialized parent pointer
710          * to reference the just-allocated physical data area.
711          */
712         if (flag & IS_ROOT_NODE) {
713                 dzp->z_dbuf = db;
714                 dzp->z_phys = pzp;
715                 dzp->z_id = obj;
716         }
717
718         /*
719          * If parent is an xattr, so am I.
720          */
721         if (dzp->z_phys->zp_flags & ZFS_XATTR)
722                 flag |= IS_XATTR;
723
724         if (vap->va_type == VBLK || vap->va_type == VCHR) {
725                 pzp->zp_rdev = zfs_expldev(vap->va_rdev);
726         }
727
728         if (zfsvfs->z_use_fuids)
729                 pzp->zp_flags = ZFS_ARCHIVE | ZFS_AV_MODIFIED;
730
731         if (vap->va_type == VDIR) {
732                 pzp->zp_size = 2;               /* contents ("." and "..") */
733                 pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
734         }
735
736         pzp->zp_parent = dzp->z_id;
737         if (flag & IS_XATTR)
738                 pzp->zp_flags |= ZFS_XATTR;
739
740         pzp->zp_gen = gen;
741
742         ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
743         ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
744
745         if (vap->va_mask & AT_ATIME) {
746                 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
747         } else {
748                 ZFS_TIME_ENCODE(&now, pzp->zp_atime);
749         }
750
751         if (vap->va_mask & AT_MTIME) {
752                 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
753         } else {
754                 ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
755         }
756
757         pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
758         if (!(flag & IS_ROOT_NODE)) {
759                 *zpp = zfs_znode_alloc(zfsvfs, db, 0);
760         } else {
761                 /*
762                  * If we are creating the root node, the "parent" we
763                  * passed in is the znode for the root.
764                  */
765                 *zpp = dzp;
766         }
767         pzp->zp_uid = acl_ids->z_fuid;
768         pzp->zp_gid = acl_ids->z_fgid;
769         pzp->zp_mode = acl_ids->z_mode;
770         VERIFY(0 == zfs_aclset_common(*zpp, acl_ids->z_aclp, cr, tx));
771         if (vap->va_mask & AT_XVATTR)
772                 zfs_xvattr_set(*zpp, (xvattr_t *)vap);
773         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
774         if (!(flag & IS_ROOT_NODE)) {
775                 vnode_t *vp;
776
777                 vp = ZTOV(*zpp);
778                 vp->v_vflag |= VV_FORCEINSMQ;
779                 err = insmntque(vp, zfsvfs->z_vfs);
780                 vp->v_vflag &= ~VV_FORCEINSMQ;
781                 KASSERT(err == 0, ("insmntque() failed: error %d", err));
782         }
783 }
784
785 void
786 zfs_xvattr_set(znode_t *zp, xvattr_t *xvap)
787 {
788         xoptattr_t *xoap;
789
790         xoap = xva_getxoptattr(xvap);
791         ASSERT(xoap);
792
793         if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
794                 ZFS_TIME_ENCODE(&xoap->xoa_createtime, zp->z_phys->zp_crtime);
795                 XVA_SET_RTN(xvap, XAT_CREATETIME);
796         }
797         if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
798                 ZFS_ATTR_SET(zp, ZFS_READONLY, xoap->xoa_readonly);
799                 XVA_SET_RTN(xvap, XAT_READONLY);
800         }
801         if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
802                 ZFS_ATTR_SET(zp, ZFS_HIDDEN, xoap->xoa_hidden);
803                 XVA_SET_RTN(xvap, XAT_HIDDEN);
804         }
805         if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
806                 ZFS_ATTR_SET(zp, ZFS_SYSTEM, xoap->xoa_system);
807                 XVA_SET_RTN(xvap, XAT_SYSTEM);
808         }
809         if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
810                 ZFS_ATTR_SET(zp, ZFS_ARCHIVE, xoap->xoa_archive);
811                 XVA_SET_RTN(xvap, XAT_ARCHIVE);
812         }
813         if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
814                 ZFS_ATTR_SET(zp, ZFS_IMMUTABLE, xoap->xoa_immutable);
815                 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
816         }
817         if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
818                 ZFS_ATTR_SET(zp, ZFS_NOUNLINK, xoap->xoa_nounlink);
819                 XVA_SET_RTN(xvap, XAT_NOUNLINK);
820         }
821         if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
822                 ZFS_ATTR_SET(zp, ZFS_APPENDONLY, xoap->xoa_appendonly);
823                 XVA_SET_RTN(xvap, XAT_APPENDONLY);
824         }
825         if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
826                 ZFS_ATTR_SET(zp, ZFS_NODUMP, xoap->xoa_nodump);
827                 XVA_SET_RTN(xvap, XAT_NODUMP);
828         }
829         if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
830                 ZFS_ATTR_SET(zp, ZFS_OPAQUE, xoap->xoa_opaque);
831                 XVA_SET_RTN(xvap, XAT_OPAQUE);
832         }
833         if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
834                 ZFS_ATTR_SET(zp, ZFS_AV_QUARANTINED,
835                     xoap->xoa_av_quarantined);
836                 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
837         }
838         if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
839                 ZFS_ATTR_SET(zp, ZFS_AV_MODIFIED, xoap->xoa_av_modified);
840                 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
841         }
842         if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
843                 (void) memcpy(zp->z_phys + 1, xoap->xoa_av_scanstamp,
844                     sizeof (xoap->xoa_av_scanstamp));
845                 zp->z_phys->zp_flags |= ZFS_BONUS_SCANSTAMP;
846                 XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
847         }
848 }
849
850 int
851 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
852 {
853         dmu_object_info_t doi;
854         dmu_buf_t       *db;
855         znode_t         *zp;
856         vnode_t         *vp;
857         int err, first = 1;
858
859         *zpp = NULL;
860 again:
861         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
862
863         err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
864         if (err) {
865                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
866                 return (err);
867         }
868
869         dmu_object_info_from_db(db, &doi);
870         if (doi.doi_bonus_type != DMU_OT_ZNODE ||
871             doi.doi_bonus_size < sizeof (znode_phys_t)) {
872                 dmu_buf_rele(db, NULL);
873                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
874                 return (EINVAL);
875         }
876
877         zp = dmu_buf_get_user(db);
878         if (zp != NULL) {
879                 mutex_enter(&zp->z_lock);
880
881                 /*
882                  * Since we do immediate eviction of the z_dbuf, we
883                  * should never find a dbuf with a znode that doesn't
884                  * know about the dbuf.
885                  */
886                 ASSERT3P(zp->z_dbuf, ==, db);
887                 ASSERT3U(zp->z_id, ==, obj_num);
888                 if (zp->z_unlinked) {
889                         err = ENOENT;
890                 } else {
891                         int dying = 0;
892
893                         vp = ZTOV(zp);
894                         if (vp == NULL)
895                                 dying = 1;
896                         else {
897                                 VN_HOLD(vp);
898                                 if ((vp->v_iflag & VI_DOOMED) != 0) {
899                                         dying = 1;
900                                         /*
901                                          * Don't VN_RELE() vnode here, because
902                                          * it can call vn_lock() which creates
903                                          * LOR between vnode lock and znode
904                                          * lock. We will VN_RELE() the vnode
905                                          * after droping znode lock.
906                                          */
907                                 }
908                         }
909                         if (dying) {
910                                 if (first) {
911                                         ZFS_LOG(1, "dying znode detected (zp=%p)", zp);
912                                         first = 0;
913                                 }
914                                 /*
915                                  * znode is dying so we can't reuse it, we must
916                                  * wait until destruction is completed.
917                                  */
918                                 dmu_buf_rele(db, NULL);
919                                 mutex_exit(&zp->z_lock);
920                                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
921                                 if (vp != NULL)
922                                         VN_RELE(vp);
923                                 tsleep(zp, 0, "zcollide", 1);
924                                 goto again;
925                         }
926                         *zpp = zp;
927                         err = 0;
928                 }
929                 dmu_buf_rele(db, NULL);
930                 mutex_exit(&zp->z_lock);
931                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
932                 return (err);
933         }
934
935         /*
936          * Not found create new znode/vnode
937          * but only if file exists.
938          *
939          * There is a small window where zfs_vget() could
940          * find this object while a file create is still in
941          * progress.  Since a gen number can never be zero
942          * we will check that to determine if its an allocated
943          * file.
944          */
945
946         if (((znode_phys_t *)db->db_data)->zp_gen != 0) {
947                 zp = zfs_znode_alloc(zfsvfs, db, doi.doi_data_block_size);
948                 *zpp = zp;
949                 vp = ZTOV(zp);
950                 vp->v_vflag |= VV_FORCEINSMQ;
951                 err = insmntque(vp, zfsvfs->z_vfs);
952                 vp->v_vflag &= ~VV_FORCEINSMQ;
953                 KASSERT(err == 0, ("insmntque() failed: error %d", err));
954                 VOP_UNLOCK(vp, 0);
955                 err = 0;
956         } else {
957                 dmu_buf_rele(db, NULL);
958                 err = ENOENT;
959         }
960         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
961         return (err);
962 }
963
964 int
965 zfs_rezget(znode_t *zp)
966 {
967         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
968         dmu_object_info_t doi;
969         dmu_buf_t *db;
970         uint64_t obj_num = zp->z_id;
971         int err;
972
973         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
974
975         err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
976         if (err) {
977                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
978                 return (err);
979         }
980
981         dmu_object_info_from_db(db, &doi);
982         if (doi.doi_bonus_type != DMU_OT_ZNODE ||
983             doi.doi_bonus_size < sizeof (znode_phys_t)) {
984                 dmu_buf_rele(db, NULL);
985                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
986                 return (EINVAL);
987         }
988
989         if (((znode_phys_t *)db->db_data)->zp_gen != zp->z_gen) {
990                 dmu_buf_rele(db, NULL);
991                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
992                 return (EIO);
993         }
994
995         mutex_enter(&zp->z_acl_lock);
996         if (zp->z_acl_cached) {
997                 zfs_acl_free(zp->z_acl_cached);
998                 zp->z_acl_cached = NULL;
999         }
1000         mutex_exit(&zp->z_acl_lock);
1001
1002         zfs_znode_dmu_init(zfsvfs, zp, db);
1003         zp->z_unlinked = (zp->z_phys->zp_links == 0);
1004         zp->z_blksz = doi.doi_data_block_size;
1005
1006         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
1007
1008         return (0);
1009 }
1010
1011 void
1012 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
1013 {
1014         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1015         objset_t *os = zfsvfs->z_os;
1016         uint64_t obj = zp->z_id;
1017         uint64_t acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
1018
1019         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj);
1020         if (acl_obj)
1021                 VERIFY(0 == dmu_object_free(os, acl_obj, tx));
1022         VERIFY(0 == dmu_object_free(os, obj, tx));
1023         zfs_znode_dmu_fini(zp);
1024         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj);
1025         zfs_znode_free(zp);
1026 }
1027
1028 void
1029 zfs_zinactive(znode_t *zp)
1030 {
1031         vnode_t *vp = ZTOV(zp);
1032         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1033         uint64_t z_id = zp->z_id;
1034         int vfslocked;
1035
1036         ASSERT(zp->z_dbuf && zp->z_phys);
1037
1038         /*
1039          * Don't allow a zfs_zget() while were trying to release this znode
1040          */
1041         ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
1042
1043         mutex_enter(&zp->z_lock);
1044         VI_LOCK(vp);
1045         if (vp->v_count > 0) {
1046                 /*
1047                  * If the hold count is greater than zero, somebody has
1048                  * obtained a new reference on this znode while we were
1049                  * processing it here, so we are done.
1050                  */
1051                 VI_UNLOCK(vp);
1052                 mutex_exit(&zp->z_lock);
1053                 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1054                 return;
1055         }
1056         VI_UNLOCK(vp);
1057
1058         /*
1059          * If this was the last reference to a file with no links,
1060          * remove the file from the file system.
1061          */
1062         if (zp->z_unlinked) {
1063                 mutex_exit(&zp->z_lock);
1064                 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1065                 ASSERT(vp->v_count == 0);
1066                 vrecycle(vp, curthread);
1067                 vfslocked = VFS_LOCK_GIANT(zfsvfs->z_vfs);
1068                 zfs_rmnode(zp);
1069                 VFS_UNLOCK_GIANT(vfslocked);
1070                 return;
1071         }
1072         mutex_exit(&zp->z_lock);
1073         ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
1074 }
1075
1076 void
1077 zfs_znode_free(znode_t *zp)
1078 {
1079         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1080
1081         ASSERT(ZTOV(zp) == NULL);
1082         mutex_enter(&zfsvfs->z_znodes_lock);
1083         POINTER_INVALIDATE(&zp->z_zfsvfs);
1084         list_remove(&zfsvfs->z_all_znodes, zp);
1085         mutex_exit(&zfsvfs->z_znodes_lock);
1086
1087         if (zp->z_acl_cached) {
1088                 zfs_acl_free(zp->z_acl_cached);
1089                 zp->z_acl_cached = NULL;
1090         }
1091
1092         kmem_cache_free(znode_cache, zp);
1093
1094         VFS_RELE(zfsvfs->z_vfs);
1095 }
1096
1097 void
1098 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1099 {
1100         timestruc_t     now;
1101
1102         ASSERT(MUTEX_HELD(&zp->z_lock));
1103
1104         gethrestime(&now);
1105
1106         if (tx) {
1107                 dmu_buf_will_dirty(zp->z_dbuf, tx);
1108                 zp->z_atime_dirty = 0;
1109                 zp->z_seq++;
1110         } else {
1111                 zp->z_atime_dirty = 1;
1112         }
1113
1114         if (flag & AT_ATIME)
1115                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
1116
1117         if (flag & AT_MTIME) {
1118                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
1119                 if (zp->z_zfsvfs->z_use_fuids)
1120                         zp->z_phys->zp_flags |= (ZFS_ARCHIVE | ZFS_AV_MODIFIED);
1121         }
1122
1123         if (flag & AT_CTIME) {
1124                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
1125                 if (zp->z_zfsvfs->z_use_fuids)
1126                         zp->z_phys->zp_flags |= ZFS_ARCHIVE;
1127         }
1128 }
1129
1130 /*
1131  * Update the requested znode timestamps with the current time.
1132  * If we are in a transaction, then go ahead and mark the znode
1133  * dirty in the transaction so the timestamps will go to disk.
1134  * Otherwise, we will get pushed next time the znode is updated
1135  * in a transaction, or when this znode eventually goes inactive.
1136  *
1137  * Why is this OK?
1138  *  1 - Only the ACCESS time is ever updated outside of a transaction.
1139  *  2 - Multiple consecutive updates will be collapsed into a single
1140  *      znode update by the transaction grouping semantics of the DMU.
1141  */
1142 void
1143 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
1144 {
1145         mutex_enter(&zp->z_lock);
1146         zfs_time_stamper_locked(zp, flag, tx);
1147         mutex_exit(&zp->z_lock);
1148 }
1149
1150 /*
1151  * Grow the block size for a file.
1152  *
1153  *      IN:     zp      - znode of file to free data in.
1154  *              size    - requested block size
1155  *              tx      - open transaction.
1156  *
1157  * NOTE: this function assumes that the znode is write locked.
1158  */
1159 void
1160 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
1161 {
1162         int             error;
1163         u_longlong_t    dummy;
1164
1165         if (size <= zp->z_blksz)
1166                 return;
1167         /*
1168          * If the file size is already greater than the current blocksize,
1169          * we will not grow.  If there is more than one block in a file,
1170          * the blocksize cannot change.
1171          */
1172         if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
1173                 return;
1174
1175         error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
1176             size, 0, tx);
1177         if (error == ENOTSUP)
1178                 return;
1179         ASSERT3U(error, ==, 0);
1180
1181         /* What blocksize did we actually get? */
1182         dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
1183 }
1184
1185 /*
1186  * Increase the file length
1187  *
1188  *      IN:     zp      - znode of file to free data in.
1189  *              end     - new end-of-file
1190  *
1191  *      RETURN: 0 if success
1192  *              error code if failure
1193  */
1194 static int
1195 zfs_extend(znode_t *zp, uint64_t end)
1196 {
1197         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1198         dmu_tx_t *tx;
1199         rl_t *rl;
1200         uint64_t newblksz;
1201         int error;
1202
1203         /*
1204          * We will change zp_size, lock the whole file.
1205          */
1206         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1207
1208         /*
1209          * Nothing to do if file already at desired length.
1210          */
1211         if (end <= zp->z_phys->zp_size) {
1212                 zfs_range_unlock(rl);
1213                 return (0);
1214         }
1215 top:
1216         tx = dmu_tx_create(zfsvfs->z_os);
1217         dmu_tx_hold_bonus(tx, zp->z_id);
1218         if (end > zp->z_blksz &&
1219             (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
1220                 /*
1221                  * We are growing the file past the current block size.
1222                  */
1223                 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
1224                         ASSERT(!ISP2(zp->z_blksz));
1225                         newblksz = MIN(end, SPA_MAXBLOCKSIZE);
1226                 } else {
1227                         newblksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
1228                 }
1229                 dmu_tx_hold_write(tx, zp->z_id, 0, newblksz);
1230         } else {
1231                 newblksz = 0;
1232         }
1233
1234         error = dmu_tx_assign(tx, TXG_NOWAIT);
1235         if (error) {
1236                 if (error == ERESTART) {
1237                         dmu_tx_wait(tx);
1238                         dmu_tx_abort(tx);
1239                         goto top;
1240                 }
1241                 dmu_tx_abort(tx);
1242                 zfs_range_unlock(rl);
1243                 return (error);
1244         }
1245         dmu_buf_will_dirty(zp->z_dbuf, tx);
1246
1247         if (newblksz)
1248                 zfs_grow_blocksize(zp, newblksz, tx);
1249
1250         zp->z_phys->zp_size = end;
1251
1252         zfs_range_unlock(rl);
1253
1254         dmu_tx_commit(tx);
1255
1256         vnode_pager_setsize(ZTOV(zp), end);
1257
1258         return (0);
1259 }
1260
1261 /*
1262  * Free space in a file.
1263  *
1264  *      IN:     zp      - znode of file to free data in.
1265  *              off     - start of section to free.
1266  *              len     - length of section to free.
1267  *
1268  *      RETURN: 0 if success
1269  *              error code if failure
1270  */
1271 static int
1272 zfs_free_range(znode_t *zp, uint64_t off, uint64_t len)
1273 {
1274         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1275         rl_t *rl;
1276         int error;
1277
1278         /*
1279          * Lock the range being freed.
1280          */
1281         rl = zfs_range_lock(zp, off, len, RL_WRITER);
1282
1283         /*
1284          * Nothing to do if file already at desired length.
1285          */
1286         if (off >= zp->z_phys->zp_size) {
1287                 zfs_range_unlock(rl);
1288                 return (0);
1289         }
1290
1291         if (off + len > zp->z_phys->zp_size)
1292                 len = zp->z_phys->zp_size - off;
1293
1294         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, off, len);
1295
1296         if (error == 0) {
1297                 /*
1298                  * In FreeBSD we cannot free block in the middle of a file,
1299                  * but only at the end of a file.
1300                  */
1301                 vnode_pager_setsize(ZTOV(zp), off);
1302         }
1303
1304         zfs_range_unlock(rl);
1305
1306         return (error);
1307 }
1308
1309 /*
1310  * Truncate a file
1311  *
1312  *      IN:     zp      - znode of file to free data in.
1313  *              end     - new end-of-file.
1314  *
1315  *      RETURN: 0 if success
1316  *              error code if failure
1317  */
1318 static int
1319 zfs_trunc(znode_t *zp, uint64_t end)
1320 {
1321         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1322         vnode_t *vp = ZTOV(zp);
1323         dmu_tx_t *tx;
1324         rl_t *rl;
1325         int error;
1326
1327         /*
1328          * We will change zp_size, lock the whole file.
1329          */
1330         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
1331
1332         /*
1333          * Nothing to do if file already at desired length.
1334          */
1335         if (end >= zp->z_phys->zp_size) {
1336                 zfs_range_unlock(rl);
1337                 return (0);
1338         }
1339
1340         error = dmu_free_long_range(zfsvfs->z_os, zp->z_id, end,  -1);
1341         if (error) {
1342                 zfs_range_unlock(rl);
1343                 return (error);
1344         }
1345 top:
1346         tx = dmu_tx_create(zfsvfs->z_os);
1347         dmu_tx_hold_bonus(tx, zp->z_id);
1348         error = dmu_tx_assign(tx, TXG_NOWAIT);
1349         if (error) {
1350                 if (error == ERESTART) {
1351                         dmu_tx_wait(tx);
1352                         dmu_tx_abort(tx);
1353                         goto top;
1354                 }
1355                 dmu_tx_abort(tx);
1356                 zfs_range_unlock(rl);
1357                 return (error);
1358         }
1359         dmu_buf_will_dirty(zp->z_dbuf, tx);
1360
1361         zp->z_phys->zp_size = end;
1362
1363         dmu_tx_commit(tx);
1364
1365         /*
1366          * Clear any mapped pages in the truncated region.  This has to
1367          * happen outside of the transaction to avoid the possibility of
1368          * a deadlock with someone trying to push a page that we are
1369          * about to invalidate.
1370          */
1371         vnode_pager_setsize(vp, end);
1372
1373         zfs_range_unlock(rl);
1374
1375         return (0);
1376 }
1377
1378 /*
1379  * Free space in a file
1380  *
1381  *      IN:     zp      - znode of file to free data in.
1382  *              off     - start of range
1383  *              len     - end of range (0 => EOF)
1384  *              flag    - current file open mode flags.
1385  *              log     - TRUE if this action should be logged
1386  *
1387  *      RETURN: 0 if success
1388  *              error code if failure
1389  */
1390 int
1391 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
1392 {
1393         vnode_t *vp = ZTOV(zp);
1394         dmu_tx_t *tx;
1395         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1396         zilog_t *zilog = zfsvfs->z_log;
1397         int error;
1398
1399         if (off > zp->z_phys->zp_size) {
1400                 error =  zfs_extend(zp, off+len);
1401                 if (error == 0 && log)
1402                         goto log;
1403                 else
1404                         return (error);
1405         }
1406
1407         if (len == 0) {
1408                 error = zfs_trunc(zp, off);
1409         } else {
1410                 if ((error = zfs_free_range(zp, off, len)) == 0 &&
1411                     off + len > zp->z_phys->zp_size)
1412                         error = zfs_extend(zp, off+len);
1413         }
1414         if (error || !log)
1415                 return (error);
1416 log:
1417         tx = dmu_tx_create(zfsvfs->z_os);
1418         dmu_tx_hold_bonus(tx, zp->z_id);
1419         error = dmu_tx_assign(tx, TXG_NOWAIT);
1420         if (error) {
1421                 if (error == ERESTART) {
1422                         dmu_tx_wait(tx);
1423                         dmu_tx_abort(tx);
1424                         goto log;
1425                 }
1426                 dmu_tx_abort(tx);
1427                 return (error);
1428         }
1429
1430         zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
1431         zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
1432
1433         dmu_tx_commit(tx);
1434         return (0);
1435 }
1436
1437 void
1438 zfs_create_fs(objset_t *os, cred_t *cr, nvlist_t *zplprops, dmu_tx_t *tx)
1439 {
1440         zfsvfs_t        zfsvfs;
1441         uint64_t        moid, obj, version;
1442         uint64_t        sense = ZFS_CASE_SENSITIVE;
1443         uint64_t        norm = 0;
1444         nvpair_t        *elem;
1445         int             error;
1446         int             i;
1447         znode_t         *rootzp = NULL;
1448         vnode_t         vnode;
1449         vattr_t         vattr;
1450         znode_t         *zp;
1451         zfs_acl_ids_t   acl_ids;
1452
1453         /*
1454          * First attempt to create master node.
1455          */
1456         /*
1457          * In an empty objset, there are no blocks to read and thus
1458          * there can be no i/o errors (which we assert below).
1459          */
1460         moid = MASTER_NODE_OBJ;
1461         error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
1462             DMU_OT_NONE, 0, tx);
1463         ASSERT(error == 0);
1464
1465         /*
1466          * Set starting attributes.
1467          */
1468         if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_USERSPACE)
1469                 version = ZPL_VERSION;
1470         else if (spa_version(dmu_objset_spa(os)) >= SPA_VERSION_FUID)
1471                 version = ZPL_VERSION_USERSPACE - 1;
1472         else
1473                 version = ZPL_VERSION_FUID - 1;
1474         elem = NULL;
1475         while ((elem = nvlist_next_nvpair(zplprops, elem)) != NULL) {
1476                 /* For the moment we expect all zpl props to be uint64_ts */
1477                 uint64_t val;
1478                 char *name;
1479
1480                 ASSERT(nvpair_type(elem) == DATA_TYPE_UINT64);
1481                 VERIFY(nvpair_value_uint64(elem, &val) == 0);
1482                 name = nvpair_name(elem);
1483                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_VERSION)) == 0) {
1484                         if (val < version)
1485                                 version = val;
1486                 } else {
1487                         error = zap_update(os, moid, name, 8, 1, &val, tx);
1488                 }
1489                 ASSERT(error == 0);
1490                 if (strcmp(name, zfs_prop_to_name(ZFS_PROP_NORMALIZE)) == 0)
1491                         norm = val;
1492                 else if (strcmp(name, zfs_prop_to_name(ZFS_PROP_CASE)) == 0)
1493                         sense = val;
1494         }
1495         ASSERT(version != 0);
1496         error = zap_update(os, moid, ZPL_VERSION_STR, 8, 1, &version, tx);
1497
1498         /*
1499          * Create a delete queue.
1500          */
1501         obj = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
1502
1503         error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &obj, tx);
1504         ASSERT(error == 0);
1505
1506         /*
1507          * Create root znode.  Create minimal znode/vnode/zfsvfs
1508          * to allow zfs_mknode to work.
1509          */
1510         VATTR_NULL(&vattr);
1511         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
1512         vattr.va_type = VDIR;
1513         vattr.va_mode = S_IFDIR|0755;
1514         vattr.va_uid = crgetuid(cr);
1515         vattr.va_gid = crgetgid(cr);
1516
1517         rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
1518         zfs_znode_cache_constructor(rootzp, NULL, 0);
1519         rootzp->z_unlinked = 0;
1520         rootzp->z_atime_dirty = 0;
1521
1522         vnode.v_type = VDIR;
1523         vnode.v_data = rootzp;
1524         rootzp->z_vnode = &vnode;
1525
1526         bzero(&zfsvfs, sizeof (zfsvfs_t));
1527
1528         zfsvfs.z_os = os;
1529         zfsvfs.z_parent = &zfsvfs;
1530         zfsvfs.z_version = version;
1531         zfsvfs.z_use_fuids = USE_FUIDS(version, os);
1532         zfsvfs.z_norm = norm;
1533         /*
1534          * Fold case on file systems that are always or sometimes case
1535          * insensitive.
1536          */
1537         if (sense == ZFS_CASE_INSENSITIVE || sense == ZFS_CASE_MIXED)
1538                 zfsvfs.z_norm |= U8_TEXTPREP_TOUPPER;
1539
1540         mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
1541         list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
1542             offsetof(znode_t, z_link_node));
1543
1544         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1545                 mutex_init(&zfsvfs.z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
1546
1547         ASSERT(!POINTER_IS_VALID(rootzp->z_zfsvfs));
1548         rootzp->z_zfsvfs = &zfsvfs;
1549         VERIFY(0 == zfs_acl_ids_create(rootzp, IS_ROOT_NODE, &vattr,
1550             cr, NULL, &acl_ids));
1551         zfs_mknode(rootzp, &vattr, tx, cr, IS_ROOT_NODE, &zp, 0, &acl_ids);
1552         ASSERT3P(zp, ==, rootzp);
1553         error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &rootzp->z_id, tx);
1554         ASSERT(error == 0);
1555         zfs_acl_ids_free(&acl_ids);
1556         POINTER_INVALIDATE(&rootzp->z_zfsvfs);
1557
1558         dmu_buf_rele(rootzp->z_dbuf, NULL);
1559         rootzp->z_dbuf = NULL;
1560         rootzp->z_vnode = NULL;
1561         kmem_cache_free(znode_cache, rootzp);
1562
1563         /*
1564          * Create shares directory
1565          */
1566
1567         error = zfs_create_share_dir(&zfsvfs, tx);
1568
1569         ASSERT(error == 0);
1570
1571         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
1572                 mutex_destroy(&zfsvfs.z_hold_mtx[i]);
1573 }
1574
1575 #endif /* _KERNEL */
1576 /*
1577  * Given an object number, return its parent object number and whether
1578  * or not the object is an extended attribute directory.
1579  */
1580 static int
1581 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1582 {
1583         dmu_buf_t *db;
1584         dmu_object_info_t doi;
1585         znode_phys_t *zp;
1586         int error;
1587
1588         if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1589                 return (error);
1590
1591         dmu_object_info_from_db(db, &doi);
1592         if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1593             doi.doi_bonus_size < sizeof (znode_phys_t)) {
1594                 dmu_buf_rele(db, FTAG);
1595                 return (EINVAL);
1596         }
1597
1598         zp = db->db_data;
1599         *pobjp = zp->zp_parent;
1600         *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1601             S_ISDIR(zp->zp_mode);
1602         dmu_buf_rele(db, FTAG);
1603
1604         return (0);
1605 }
1606
1607 int
1608 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1609 {
1610         char *path = buf + len - 1;
1611         int error;
1612
1613         *path = '\0';
1614
1615         for (;;) {
1616                 uint64_t pobj;
1617                 char component[MAXNAMELEN + 2];
1618                 size_t complen;
1619                 int is_xattrdir;
1620
1621                 if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1622                     &is_xattrdir)) != 0)
1623                         break;
1624
1625                 if (pobj == obj) {
1626                         if (path[0] != '/')
1627                                 *--path = '/';
1628                         break;
1629                 }
1630
1631                 component[0] = '/';
1632                 if (is_xattrdir) {
1633                         (void) sprintf(component + 1, "<xattrdir>");
1634                 } else {
1635                         error = zap_value_search(osp, pobj, obj,
1636                             ZFS_DIRENT_OBJ(-1ULL), component + 1);
1637                         if (error != 0)
1638                                 break;
1639                 }
1640
1641                 complen = strlen(component);
1642                 path -= complen;
1643                 ASSERT(path >= buf);
1644                 bcopy(component, path, complen);
1645                 obj = pobj;
1646         }
1647
1648         if (error == 0)
1649                 (void) memmove(buf, path, buf + len - path);
1650         return (error);
1651 }