]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_znode.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25
26 /* Portions Copyright 2007 Jeremy Teo */
27
28 #pragma ident   "%Z%%M% %I%     %E% SMI"
29
30 #ifdef _KERNEL
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/time.h>
34 #include <sys/systm.h>
35 #include <sys/sysmacros.h>
36 #include <sys/resource.h>
37 #include <sys/mntent.h>
38 #include <sys/vfs.h>
39 #include <sys/vnode.h>
40 #include <sys/file.h>
41 #include <sys/kmem.h>
42 #include <sys/cmn_err.h>
43 #include <sys/errno.h>
44 #include <sys/unistd.h>
45 #include <sys/atomic.h>
46 #include <sys/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/zfs_ioctl.h>
49 #include <sys/zfs_rlock.h>
50 #include <sys/fs/zfs.h>
51 #endif /* _KERNEL */
52
53 #include <sys/dmu.h>
54 #include <sys/refcount.h>
55 #include <sys/stat.h>
56 #include <sys/zap.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/refcount.h>
59
60 /*
61  * Functions needed for userland (ie: libzpool) are not put under
62  * #ifdef_KERNEL; the rest of the functions have dependencies
63  * (such as VFS logic) that will not compile easily in userland.
64  */
65 #ifdef _KERNEL
66 struct kmem_cache *znode_cache = NULL;
67
68 /*ARGSUSED*/
69 static void
70 znode_pageout_func(dmu_buf_t *dbuf, void *user_ptr)
71 {
72         znode_t *zp = user_ptr;
73         vnode_t *vp;
74
75         mutex_enter(&zp->z_lock);
76         vp = ZTOV(zp);
77         if (vp == NULL) {
78                 mutex_exit(&zp->z_lock);
79                 zfs_znode_free(zp);
80         } else if (vp->v_count == 0) {
81                 ZTOV(zp) = NULL;
82                 vhold(vp);
83                 mutex_exit(&zp->z_lock);
84                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
85                 vrecycle(vp, curthread);
86                 VOP_UNLOCK(vp, 0, curthread);
87                 vdrop(vp);
88                 zfs_znode_free(zp);
89         } else {
90                 /* signal force unmount that this znode can be freed */
91                 zp->z_dbuf = NULL;
92                 mutex_exit(&zp->z_lock);
93         }
94 }
95
96 extern struct vop_vector zfs_vnodeops;
97 extern struct vop_vector zfs_fifoops;
98
99 /*
100  * XXX: We cannot use this function as a cache constructor, because
101  *      there is one global cache for all file systems and we need
102  *      to pass vfsp here, which is not possible, because argument
103  *      'cdrarg' is defined at kmem_cache_create() time.
104  */
105 static int
106 zfs_znode_cache_constructor(void *buf, void *cdrarg, int kmflags)
107 {
108         znode_t *zp = buf;
109         vnode_t *vp;
110         vfs_t *vfsp = cdrarg;
111         int error;
112
113         if (cdrarg != NULL) {
114                 error = getnewvnode("zfs", vfsp, &zfs_vnodeops, &vp);
115                 ASSERT(error == 0);
116                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
117                 zp->z_vnode = vp;
118                 vp->v_data = (caddr_t)zp;
119                 vp->v_vnlock->lk_flags |= LK_CANRECURSE;
120                 vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
121         } else {
122                 zp->z_vnode = NULL;
123         }
124         mutex_init(&zp->z_lock, NULL, MUTEX_DEFAULT, NULL);
125         rw_init(&zp->z_map_lock, NULL, RW_DEFAULT, NULL);
126         rw_init(&zp->z_parent_lock, NULL, RW_DEFAULT, NULL);
127         rw_init(&zp->z_name_lock, NULL, RW_DEFAULT, NULL);
128         mutex_init(&zp->z_acl_lock, NULL, MUTEX_DEFAULT, NULL);
129
130         mutex_init(&zp->z_range_lock, NULL, MUTEX_DEFAULT, NULL);
131         avl_create(&zp->z_range_avl, zfs_range_compare,
132             sizeof (rl_t), offsetof(rl_t, r_node));
133
134         zp->z_dbuf_held = 0;
135         zp->z_dirlocks = 0;
136         return (0);
137 }
138
139 /*ARGSUSED*/
140 static void
141 zfs_znode_cache_destructor(void *buf, void *cdarg)
142 {
143         znode_t *zp = buf;
144
145         ASSERT(zp->z_dirlocks == 0);
146         mutex_destroy(&zp->z_lock);
147         rw_destroy(&zp->z_map_lock);
148         rw_destroy(&zp->z_parent_lock);
149         rw_destroy(&zp->z_name_lock);
150         mutex_destroy(&zp->z_acl_lock);
151         mutex_destroy(&zp->z_range_lock);
152         avl_destroy(&zp->z_range_avl);
153
154         ASSERT(zp->z_dbuf_held == 0);
155 }
156
157 void
158 zfs_znode_init(void)
159 {
160         /*
161          * Initialize zcache
162          */
163         ASSERT(znode_cache == NULL);
164         znode_cache = kmem_cache_create("zfs_znode_cache",
165             sizeof (znode_t), 0, /* zfs_znode_cache_constructor */ NULL,
166             zfs_znode_cache_destructor, NULL, NULL, NULL, 0);
167 }
168
169 void
170 zfs_znode_fini(void)
171 {
172         /*
173          * Cleanup zcache
174          */
175         if (znode_cache)
176                 kmem_cache_destroy(znode_cache);
177         znode_cache = NULL;
178 }
179
180 /*
181  * zfs_init_fs - Initialize the zfsvfs struct and the file system
182  *      incore "master" object.  Verify version compatibility.
183  */
184 int
185 zfs_init_fs(zfsvfs_t *zfsvfs, znode_t **zpp, cred_t *cr)
186 {
187         objset_t        *os = zfsvfs->z_os;
188         uint64_t        version = ZPL_VERSION;
189         int             i, error;
190         dmu_object_info_t doi;
191         uint64_t fsid_guid;
192
193         *zpp = NULL;
194
195         /*
196          * XXX - hack to auto-create the pool root filesystem at
197          * the first attempted mount.
198          */
199         if (dmu_object_info(os, MASTER_NODE_OBJ, &doi) == ENOENT) {
200                 dmu_tx_t *tx = dmu_tx_create(os);
201
202                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* master */
203                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, NULL); /* del queue */
204                 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); /* root node */
205                 error = dmu_tx_assign(tx, TXG_WAIT);
206                 ASSERT3U(error, ==, 0);
207                 zfs_create_fs(os, cr, tx);
208                 dmu_tx_commit(tx);
209         }
210
211         error = zap_lookup(os, MASTER_NODE_OBJ, ZPL_VERSION_OBJ, 8, 1,
212             &version);
213         if (error) {
214                 return (error);
215         } else if (version != ZPL_VERSION) {
216                 (void) printf("Mismatched versions:  File system "
217                     "is version %lld on-disk format, which is "
218                     "incompatible with this software version %lld!",
219                     (u_longlong_t)version, ZPL_VERSION);
220                 return (ENOTSUP);
221         }
222
223         /*
224          * The fsid is 64 bits, composed of an 8-bit fs type, which
225          * separates our fsid from any other filesystem types, and a
226          * 56-bit objset unique ID.  The objset unique ID is unique to
227          * all objsets open on this system, provided by unique_create().
228          * The 8-bit fs type must be put in the low bits of fsid[1]
229          * because that's where other Solaris filesystems put it.
230          */
231         fsid_guid = dmu_objset_fsid_guid(os);
232         ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
233         zfsvfs->z_vfs->vfs_fsid.val[0] = fsid_guid;
234         zfsvfs->z_vfs->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
235             zfsvfs->z_vfs->mnt_vfc->vfc_typenum & 0xFF;
236
237         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
238             &zfsvfs->z_root);
239         if (error)
240                 return (error);
241         ASSERT(zfsvfs->z_root != 0);
242
243         /*
244          * Create the per mount vop tables.
245          */
246
247         /*
248          * Initialize zget mutex's
249          */
250         for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
251                 mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
252
253         error = zfs_zget(zfsvfs, zfsvfs->z_root, zpp);
254         if (error)
255                 return (error);
256         ASSERT3U((*zpp)->z_id, ==, zfsvfs->z_root);
257
258         error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
259             &zfsvfs->z_unlinkedobj);
260         if (error)
261                 return (error);
262
263         return (0);
264 }
265
266 /*
267  * define a couple of values we need available
268  * for both 64 and 32 bit environments.
269  */
270 #ifndef NBITSMINOR64
271 #define NBITSMINOR64    32
272 #endif
273 #ifndef MAXMAJ64
274 #define MAXMAJ64        0xffffffffUL
275 #endif
276 #ifndef MAXMIN64
277 #define MAXMIN64        0xffffffffUL
278 #endif
279 #ifndef major
280 #define major(x)        ((int)(((u_int)(x) >> 8)&0xff)) /* major number */
281 #endif
282 #ifndef minor
283 #define minor(x)        ((int)((x)&0xffff00ff))         /* minor number */
284 #endif
285
286 /*
287  * Create special expldev for ZFS private use.
288  * Can't use standard expldev since it doesn't do
289  * what we want.  The standard expldev() takes a
290  * dev32_t in LP64 and expands it to a long dev_t.
291  * We need an interface that takes a dev32_t in ILP32
292  * and expands it to a long dev_t.
293  */
294 static uint64_t
295 zfs_expldev(dev_t dev)
296 {
297         return (((uint64_t)major(dev) << NBITSMINOR64) | minor(dev));
298 }
299 /*
300  * Special cmpldev for ZFS private use.
301  * Can't use standard cmpldev since it takes
302  * a long dev_t and compresses it to dev32_t in
303  * LP64.  We need to do a compaction of a long dev_t
304  * to a dev32_t in ILP32.
305  */
306 dev_t
307 zfs_cmpldev(uint64_t dev)
308 {
309         return (makedev((dev >> NBITSMINOR64), (dev & MAXMIN64)));
310 }
311
312 /*
313  * Construct a new znode/vnode and intialize.
314  *
315  * This does not do a call to dmu_set_user() that is
316  * up to the caller to do, in case you don't want to
317  * return the znode
318  */
319 static znode_t *
320 zfs_znode_alloc(zfsvfs_t *zfsvfs, dmu_buf_t *db, uint64_t obj_num, int blksz)
321 {
322         znode_t *zp;
323         vnode_t *vp;
324         int error;
325
326         zp = kmem_cache_alloc(znode_cache, KM_SLEEP);
327         zfs_znode_cache_constructor(zp, zfsvfs->z_vfs, 0);
328
329         ASSERT(zp->z_dirlocks == NULL);
330
331         zp->z_phys = db->db_data;
332         zp->z_zfsvfs = zfsvfs;
333         zp->z_unlinked = 0;
334         zp->z_atime_dirty = 0;
335         zp->z_dbuf_held = 0;
336         zp->z_mapcnt = 0;
337         zp->z_last_itx = 0;
338         zp->z_dbuf = db;
339         zp->z_id = obj_num;
340         zp->z_blksz = blksz;
341         zp->z_seq = 0x7A4653;
342         zp->z_sync_cnt = 0;
343
344         mutex_enter(&zfsvfs->z_znodes_lock);
345         list_insert_tail(&zfsvfs->z_all_znodes, zp);
346         mutex_exit(&zfsvfs->z_znodes_lock);
347
348         vp = ZTOV(zp);
349         if (vp == NULL)
350                 return (zp);
351
352         vp->v_vflag |= VV_FORCEINSMQ;
353         error = insmntque(vp, zfsvfs->z_vfs);
354         vp->v_vflag &= ~VV_FORCEINSMQ;
355         KASSERT(error == 0, ("insmntque() failed: error %d", error));
356
357         vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
358         switch (vp->v_type) {
359         case VDIR:
360                 zp->z_zn_prefetch = B_TRUE; /* z_prefetch default is enabled */
361                 break;
362         case VFIFO:
363                 vp->v_op = &zfs_fifoops;
364                 break;
365         }
366
367         return (zp);
368 }
369
370 static void
371 zfs_znode_dmu_init(znode_t *zp)
372 {
373         znode_t         *nzp;
374         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
375         dmu_buf_t       *db = zp->z_dbuf;
376
377         mutex_enter(&zp->z_lock);
378
379         nzp = dmu_buf_set_user_ie(db, zp, &zp->z_phys, znode_pageout_func);
380
381         /*
382          * there should be no
383          * concurrent zgets on this object.
384          */
385         ASSERT3P(nzp, ==, NULL);
386
387         /*
388          * Slap on VROOT if we are the root znode
389          */
390         if (zp->z_id == zfsvfs->z_root) {
391                 ZTOV(zp)->v_flag |= VROOT;
392         }
393
394         ASSERT(zp->z_dbuf_held == 0);
395         zp->z_dbuf_held = 1;
396         VFS_HOLD(zfsvfs->z_vfs);
397         mutex_exit(&zp->z_lock);
398 }
399
400 /*
401  * Create a new DMU object to hold a zfs znode.
402  *
403  *      IN:     dzp     - parent directory for new znode
404  *              vap     - file attributes for new znode
405  *              tx      - dmu transaction id for zap operations
406  *              cr      - credentials of caller
407  *              flag    - flags:
408  *                        IS_ROOT_NODE  - new object will be root
409  *                        IS_XATTR      - new object is an attribute
410  *                        IS_REPLAY     - intent log replay
411  *
412  *      OUT:    oid     - ID of created object
413  *
414  */
415 void
416 zfs_mknode(znode_t *dzp, vattr_t *vap, uint64_t *oid, dmu_tx_t *tx, cred_t *cr,
417         uint_t flag, znode_t **zpp, int bonuslen)
418 {
419         dmu_buf_t       *dbp;
420         znode_phys_t    *pzp;
421         znode_t         *zp;
422         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
423         timestruc_t     now;
424         uint64_t        gen;
425         int             err;
426
427         ASSERT(vap && (vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
428
429         if (zfsvfs->z_assign >= TXG_INITIAL) {          /* ZIL replay */
430                 *oid = vap->va_nodeid;
431                 flag |= IS_REPLAY;
432                 now = vap->va_ctime;            /* see zfs_replay_create() */
433                 gen = vap->va_nblocks;          /* ditto */
434         } else {
435                 *oid = 0;
436                 gethrestime(&now);
437                 gen = dmu_tx_get_txg(tx);
438         }
439
440         /*
441          * Create a new DMU object.
442          */
443         /*
444          * There's currently no mechanism for pre-reading the blocks that will
445          * be to needed allocate a new object, so we accept the small chance
446          * that there will be an i/o error and we will fail one of the
447          * assertions below.
448          */
449         if (vap->va_type == VDIR) {
450                 if (flag & IS_REPLAY) {
451                         err = zap_create_claim(zfsvfs->z_os, *oid,
452                             DMU_OT_DIRECTORY_CONTENTS,
453                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
454                         ASSERT3U(err, ==, 0);
455                 } else {
456                         *oid = zap_create(zfsvfs->z_os,
457                             DMU_OT_DIRECTORY_CONTENTS,
458                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
459                 }
460         } else {
461                 if (flag & IS_REPLAY) {
462                         err = dmu_object_claim(zfsvfs->z_os, *oid,
463                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
464                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
465                         ASSERT3U(err, ==, 0);
466                 } else {
467                         *oid = dmu_object_alloc(zfsvfs->z_os,
468                             DMU_OT_PLAIN_FILE_CONTENTS, 0,
469                             DMU_OT_ZNODE, sizeof (znode_phys_t) + bonuslen, tx);
470                 }
471         }
472         VERIFY(0 == dmu_bonus_hold(zfsvfs->z_os, *oid, NULL, &dbp));
473         dmu_buf_will_dirty(dbp, tx);
474
475         /*
476          * Initialize the znode physical data to zero.
477          */
478         ASSERT(dbp->db_size >= sizeof (znode_phys_t));
479         bzero(dbp->db_data, dbp->db_size);
480         pzp = dbp->db_data;
481
482         /*
483          * If this is the root, fix up the half-initialized parent pointer
484          * to reference the just-allocated physical data area.
485          */
486         if (flag & IS_ROOT_NODE) {
487                 dzp->z_phys = pzp;
488                 dzp->z_id = *oid;
489         }
490
491         /*
492          * If parent is an xattr, so am I.
493          */
494         if (dzp->z_phys->zp_flags & ZFS_XATTR)
495                 flag |= IS_XATTR;
496
497         if (vap->va_type == VBLK || vap->va_type == VCHR) {
498                 pzp->zp_rdev = zfs_expldev(vap->va_rdev);
499         }
500
501         if (vap->va_type == VDIR) {
502                 pzp->zp_size = 2;               /* contents ("." and "..") */
503                 pzp->zp_links = (flag & (IS_ROOT_NODE | IS_XATTR)) ? 2 : 1;
504         }
505
506         pzp->zp_parent = dzp->z_id;
507         if (flag & IS_XATTR)
508                 pzp->zp_flags |= ZFS_XATTR;
509
510         pzp->zp_gen = gen;
511
512         ZFS_TIME_ENCODE(&now, pzp->zp_crtime);
513         ZFS_TIME_ENCODE(&now, pzp->zp_ctime);
514
515         if (vap->va_mask & AT_ATIME) {
516                 ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
517         } else {
518                 ZFS_TIME_ENCODE(&now, pzp->zp_atime);
519         }
520
521         if (vap->va_mask & AT_MTIME) {
522                 ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
523         } else {
524                 ZFS_TIME_ENCODE(&now, pzp->zp_mtime);
525         }
526
527         pzp->zp_mode = MAKEIMODE(vap->va_type, vap->va_mode);
528         zp = zfs_znode_alloc(zfsvfs, dbp, *oid, 0);
529
530         zfs_perm_init(zp, dzp, flag, vap, tx, cr);
531
532         if (zpp) {
533                 kmutex_t *hash_mtx = ZFS_OBJ_MUTEX(zp);
534
535                 mutex_enter(hash_mtx);
536                 zfs_znode_dmu_init(zp);
537                 mutex_exit(hash_mtx);
538
539                 *zpp = zp;
540         } else {
541                 if (ZTOV(zp) != NULL) {
542                         ZTOV(zp)->v_count = 0;
543                         VOP_UNLOCK(ZTOV(zp), 0, curthread);
544                 }
545                 dmu_buf_rele(dbp, NULL);
546                 zfs_znode_free(zp);
547         }
548 }
549
550 int
551 zfs_zget(zfsvfs_t *zfsvfs, uint64_t obj_num, znode_t **zpp)
552 {
553         dmu_object_info_t doi;
554         dmu_buf_t       *db;
555         znode_t         *zp;
556         vnode_t         *vp;
557         int err;
558
559         *zpp = NULL;
560
561         ZFS_OBJ_HOLD_ENTER(zfsvfs, obj_num);
562
563         err = dmu_bonus_hold(zfsvfs->z_os, obj_num, NULL, &db);
564         if (err) {
565                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
566                 return (err);
567         }
568
569         dmu_object_info_from_db(db, &doi);
570         if (doi.doi_bonus_type != DMU_OT_ZNODE ||
571             doi.doi_bonus_size < sizeof (znode_phys_t)) {
572                 dmu_buf_rele(db, NULL);
573                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
574                 return (EINVAL);
575         }
576
577         ASSERT(db->db_object == obj_num);
578         ASSERT(db->db_offset == -1);
579         ASSERT(db->db_data != NULL);
580
581         zp = dmu_buf_get_user(db);
582
583         if (zp != NULL) {
584                 mutex_enter(&zp->z_lock);
585
586                 ASSERT3U(zp->z_id, ==, obj_num);
587                 if (zp->z_unlinked) {
588                         dmu_buf_rele(db, NULL);
589                         mutex_exit(&zp->z_lock);
590                         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
591                         return (ENOENT);
592                 } else if (zp->z_dbuf_held) {
593                         dmu_buf_rele(db, NULL);
594                 } else {
595                         zp->z_dbuf_held = 1;
596                         VFS_HOLD(zfsvfs->z_vfs);
597                 }
598
599                 if (ZTOV(zp) != NULL)
600                         VN_HOLD(ZTOV(zp));
601                 else {
602                         err = getnewvnode("zfs", zfsvfs->z_vfs, &zfs_vnodeops,
603                             &zp->z_vnode);
604                         ASSERT(err == 0);
605                         vp = ZTOV(zp);
606                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
607                         vp->v_data = (caddr_t)zp;
608                         vp->v_vnlock->lk_flags |= LK_CANRECURSE;
609                         vp->v_vnlock->lk_flags &= ~LK_NOSHARE;
610                         vp->v_type = IFTOVT((mode_t)zp->z_phys->zp_mode);
611                         if (vp->v_type == VDIR)
612                                 zp->z_zn_prefetch = B_TRUE;     /* z_prefetch default is enabled */
613                         vp->v_vflag |= VV_FORCEINSMQ;
614                         err = insmntque(vp, zfsvfs->z_vfs);
615                         vp->v_vflag &= ~VV_FORCEINSMQ;
616                         KASSERT(err == 0, ("insmntque() failed: error %d", err));
617                         VOP_UNLOCK(vp, 0, curthread);
618                 }
619                 mutex_exit(&zp->z_lock);
620                 ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
621                 *zpp = zp;
622                 return (0);
623         }
624
625         /*
626          * Not found create new znode/vnode
627          */
628         zp = zfs_znode_alloc(zfsvfs, db, obj_num, doi.doi_data_block_size);
629         ASSERT3U(zp->z_id, ==, obj_num);
630         zfs_znode_dmu_init(zp);
631         ZFS_OBJ_HOLD_EXIT(zfsvfs, obj_num);
632         *zpp = zp;
633         if ((vp = ZTOV(zp)) != NULL)
634                 VOP_UNLOCK(vp, 0, curthread);
635         return (0);
636 }
637
638 void
639 zfs_znode_delete(znode_t *zp, dmu_tx_t *tx)
640 {
641         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
642         int error;
643
644         ZFS_OBJ_HOLD_ENTER(zfsvfs, zp->z_id);
645         if (zp->z_phys->zp_acl.z_acl_extern_obj) {
646                 error = dmu_object_free(zfsvfs->z_os,
647                     zp->z_phys->zp_acl.z_acl_extern_obj, tx);
648                 ASSERT3U(error, ==, 0);
649         }
650         error = dmu_object_free(zfsvfs->z_os, zp->z_id, tx);
651         ASSERT3U(error, ==, 0);
652         zp->z_dbuf_held = 0;
653         ZFS_OBJ_HOLD_EXIT(zfsvfs, zp->z_id);
654         dmu_buf_rele(zp->z_dbuf, NULL);
655 }
656
657 void
658 zfs_zinactive(znode_t *zp)
659 {
660         vnode_t *vp = ZTOV(zp);
661         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
662         uint64_t z_id = zp->z_id;
663
664         ASSERT(zp->z_dbuf_held && zp->z_phys);
665
666         /*
667          * Don't allow a zfs_zget() while were trying to release this znode
668          */
669         ZFS_OBJ_HOLD_ENTER(zfsvfs, z_id);
670
671         mutex_enter(&zp->z_lock);
672         VI_LOCK(vp);
673         if (vp->v_count > 0) {
674                 /*
675                  * If the hold count is greater than zero, somebody has
676                  * obtained a new reference on this znode while we were
677                  * processing it here, so we are done.
678                  */
679                 VI_UNLOCK(vp);
680                 mutex_exit(&zp->z_lock);
681                 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
682                 return;
683         }
684         VI_UNLOCK(vp);
685
686         /*
687          * If this was the last reference to a file with no links,
688          * remove the file from the file system.
689          */
690         if (zp->z_unlinked) {
691                 ZTOV(zp) = NULL;
692                 mutex_exit(&zp->z_lock);
693                 ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
694                 ASSERT(vp->v_count == 0);
695                 vrecycle(vp, curthread);
696                 zfs_rmnode(zp);
697                 VFS_RELE(zfsvfs->z_vfs);
698                 return;
699         }
700         ASSERT(zp->z_phys);
701         ASSERT(zp->z_dbuf_held);
702         mutex_exit(&zp->z_lock);
703         ZFS_OBJ_HOLD_EXIT(zfsvfs, z_id);
704 }
705
706 void
707 zfs_znode_free(znode_t *zp)
708 {
709         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
710
711         mutex_enter(&zfsvfs->z_znodes_lock);
712         list_remove(&zfsvfs->z_all_znodes, zp);
713         mutex_exit(&zfsvfs->z_znodes_lock);
714
715         kmem_cache_free(znode_cache, zp);
716 }
717
718 void
719 zfs_time_stamper_locked(znode_t *zp, uint_t flag, dmu_tx_t *tx)
720 {
721         timestruc_t     now;
722
723         ASSERT(MUTEX_HELD(&zp->z_lock));
724
725         gethrestime(&now);
726
727         if (tx) {
728                 dmu_buf_will_dirty(zp->z_dbuf, tx);
729                 zp->z_atime_dirty = 0;
730                 zp->z_seq++;
731         } else {
732                 zp->z_atime_dirty = 1;
733         }
734
735         if (flag & AT_ATIME)
736                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_atime);
737
738         if (flag & AT_MTIME)
739                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_mtime);
740
741         if (flag & AT_CTIME)
742                 ZFS_TIME_ENCODE(&now, zp->z_phys->zp_ctime);
743 }
744
745 /*
746  * Update the requested znode timestamps with the current time.
747  * If we are in a transaction, then go ahead and mark the znode
748  * dirty in the transaction so the timestamps will go to disk.
749  * Otherwise, we will get pushed next time the znode is updated
750  * in a transaction, or when this znode eventually goes inactive.
751  *
752  * Why is this OK?
753  *  1 - Only the ACCESS time is ever updated outside of a transaction.
754  *  2 - Multiple consecutive updates will be collapsed into a single
755  *      znode update by the transaction grouping semantics of the DMU.
756  */
757 void
758 zfs_time_stamper(znode_t *zp, uint_t flag, dmu_tx_t *tx)
759 {
760         mutex_enter(&zp->z_lock);
761         zfs_time_stamper_locked(zp, flag, tx);
762         mutex_exit(&zp->z_lock);
763 }
764
765 /*
766  * Grow the block size for a file.
767  *
768  *      IN:     zp      - znode of file to free data in.
769  *              size    - requested block size
770  *              tx      - open transaction.
771  *
772  * NOTE: this function assumes that the znode is write locked.
773  */
774 void
775 zfs_grow_blocksize(znode_t *zp, uint64_t size, dmu_tx_t *tx)
776 {
777         int             error;
778         u_longlong_t    dummy;
779
780         if (size <= zp->z_blksz)
781                 return;
782         /*
783          * If the file size is already greater than the current blocksize,
784          * we will not grow.  If there is more than one block in a file,
785          * the blocksize cannot change.
786          */
787         if (zp->z_blksz && zp->z_phys->zp_size > zp->z_blksz)
788                 return;
789
790         error = dmu_object_set_blocksize(zp->z_zfsvfs->z_os, zp->z_id,
791             size, 0, tx);
792         if (error == ENOTSUP)
793                 return;
794         ASSERT3U(error, ==, 0);
795
796         /* What blocksize did we actually get? */
797         dmu_object_size_from_db(zp->z_dbuf, &zp->z_blksz, &dummy);
798 }
799
800 /*
801  * Free space in a file.
802  *
803  *      IN:     zp      - znode of file to free data in.
804  *              off     - start of section to free.
805  *              len     - length of section to free (0 => to EOF).
806  *              flag    - current file open mode flags.
807  *
808  *      RETURN: 0 if success
809  *              error code if failure
810  */
811 int
812 zfs_freesp(znode_t *zp, uint64_t off, uint64_t len, int flag, boolean_t log)
813 {
814         vnode_t *vp = ZTOV(zp);
815         dmu_tx_t *tx;
816         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
817         zilog_t *zilog = zfsvfs->z_log;
818         rl_t *rl;
819         uint64_t end = off + len;
820         uint64_t size, new_blksz;
821         int error;
822
823         if (ZTOV(zp)->v_type == VFIFO)
824                 return (0);
825
826         /*
827          * If we will change zp_size then lock the whole file,
828          * otherwise just lock the range being freed.
829          */
830         if (len == 0 || off + len > zp->z_phys->zp_size) {
831                 rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
832         } else {
833                 rl = zfs_range_lock(zp, off, len, RL_WRITER);
834                 /* recheck, in case zp_size changed */
835                 if (off + len > zp->z_phys->zp_size) {
836                         /* lost race: file size changed, lock whole file */
837                         zfs_range_unlock(rl);
838                         rl = zfs_range_lock(zp, 0, UINT64_MAX, RL_WRITER);
839                 }
840         }
841
842         /*
843          * Nothing to do if file already at desired length.
844          */
845         size = zp->z_phys->zp_size;
846         if (len == 0 && size == off && off != 0) {
847                 zfs_range_unlock(rl);
848                 return (0);
849         }
850
851         tx = dmu_tx_create(zfsvfs->z_os);
852         dmu_tx_hold_bonus(tx, zp->z_id);
853         new_blksz = 0;
854         if (end > size &&
855             (!ISP2(zp->z_blksz) || zp->z_blksz < zfsvfs->z_max_blksz)) {
856                 /*
857                  * We are growing the file past the current block size.
858                  */
859                 if (zp->z_blksz > zp->z_zfsvfs->z_max_blksz) {
860                         ASSERT(!ISP2(zp->z_blksz));
861                         new_blksz = MIN(end, SPA_MAXBLOCKSIZE);
862                 } else {
863                         new_blksz = MIN(end, zp->z_zfsvfs->z_max_blksz);
864                 }
865                 dmu_tx_hold_write(tx, zp->z_id, 0, MIN(end, new_blksz));
866         } else if (off < size) {
867                 /*
868                  * If len == 0, we are truncating the file.
869                  */
870                 dmu_tx_hold_free(tx, zp->z_id, off, len ? len : DMU_OBJECT_END);
871         }
872
873         error = dmu_tx_assign(tx, zfsvfs->z_assign);
874         if (error) {
875                 if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
876                         dmu_tx_wait(tx);
877                 dmu_tx_abort(tx);
878                 zfs_range_unlock(rl);
879                 return (error);
880         }
881
882         if (new_blksz)
883                 zfs_grow_blocksize(zp, new_blksz, tx);
884
885         if (end > size || len == 0)
886                 zp->z_phys->zp_size = end;
887
888         if (off < size) {
889                 objset_t *os = zfsvfs->z_os;
890                 uint64_t rlen = len;
891
892                 if (len == 0)
893                         rlen = -1;
894                 else if (end > size)
895                         rlen = size - off;
896                 VERIFY(0 == dmu_free_range(os, zp->z_id, off, rlen, tx));
897         }
898
899         if (log) {
900                 zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
901                 zfs_log_truncate(zilog, tx, TX_TRUNCATE, zp, off, len);
902         }
903
904         zfs_range_unlock(rl);
905
906         dmu_tx_commit(tx);
907
908         /*
909          * Clear any mapped pages in the truncated region.  This has to
910          * happen outside of the transaction to avoid the possibility of
911          * a deadlock with someone trying to push a page that we are
912          * about to invalidate.
913          */
914         rw_enter(&zp->z_map_lock, RW_WRITER);
915         if (end > size)
916                 vnode_pager_setsize(vp, end);
917         else if (len == 0) {
918 #if 0
919                 error = vtruncbuf(vp, curthread->td_ucred, curthread, end, PAGE_SIZE);
920 #else
921                 error = vinvalbuf(vp, V_SAVE, curthread, 0, 0);
922                 vnode_pager_setsize(vp, end);
923 #endif
924         }
925         rw_exit(&zp->z_map_lock);
926
927         return (0);
928 }
929
930 void
931 zfs_create_fs(objset_t *os, cred_t *cr, dmu_tx_t *tx)
932 {
933         zfsvfs_t        zfsvfs;
934         uint64_t        moid, doid, roid = 0;
935         uint64_t        version = ZPL_VERSION;
936         int             error;
937         znode_t         *rootzp = NULL;
938         vattr_t         vattr;
939
940         /*
941          * First attempt to create master node.
942          */
943         /*
944          * In an empty objset, there are no blocks to read and thus
945          * there can be no i/o errors (which we assert below).
946          */
947         moid = MASTER_NODE_OBJ;
948         error = zap_create_claim(os, moid, DMU_OT_MASTER_NODE,
949             DMU_OT_NONE, 0, tx);
950         ASSERT(error == 0);
951
952         /*
953          * Set starting attributes.
954          */
955
956         error = zap_update(os, moid, ZPL_VERSION_OBJ, 8, 1, &version, tx);
957         ASSERT(error == 0);
958
959         /*
960          * Create a delete queue.
961          */
962         doid = zap_create(os, DMU_OT_UNLINKED_SET, DMU_OT_NONE, 0, tx);
963
964         error = zap_add(os, moid, ZFS_UNLINKED_SET, 8, 1, &doid, tx);
965         ASSERT(error == 0);
966
967         /*
968          * Create root znode.  Create minimal znode/vnode/zfsvfs
969          * to allow zfs_mknode to work.
970          */
971         vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE;
972         vattr.va_type = VDIR;
973         vattr.va_mode = S_IFDIR|0755;
974         vattr.va_uid = UID_ROOT;
975         vattr.va_gid = GID_WHEEL;
976
977         rootzp = kmem_cache_alloc(znode_cache, KM_SLEEP);
978         zfs_znode_cache_constructor(rootzp, NULL, 0);
979         rootzp->z_zfsvfs = &zfsvfs;
980         rootzp->z_unlinked = 0;
981         rootzp->z_atime_dirty = 0;
982         rootzp->z_dbuf_held = 0;
983
984         bzero(&zfsvfs, sizeof (zfsvfs_t));
985
986         zfsvfs.z_os = os;
987         zfsvfs.z_assign = TXG_NOWAIT;
988         zfsvfs.z_parent = &zfsvfs;
989
990         mutex_init(&zfsvfs.z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
991         list_create(&zfsvfs.z_all_znodes, sizeof (znode_t),
992             offsetof(znode_t, z_link_node));
993
994         zfs_mknode(rootzp, &vattr, &roid, tx, cr, IS_ROOT_NODE, NULL, 0);
995         ASSERT3U(rootzp->z_id, ==, roid);
996         error = zap_add(os, moid, ZFS_ROOT_OBJ, 8, 1, &roid, tx);
997         ASSERT(error == 0);
998
999         mutex_destroy(&zfsvfs.z_znodes_lock);
1000         kmem_cache_free(znode_cache, rootzp);
1001 }
1002 #endif /* _KERNEL */
1003
1004 /*
1005  * Given an object number, return its parent object number and whether
1006  * or not the object is an extended attribute directory.
1007  */
1008 static int
1009 zfs_obj_to_pobj(objset_t *osp, uint64_t obj, uint64_t *pobjp, int *is_xattrdir)
1010 {
1011         dmu_buf_t *db;
1012         dmu_object_info_t doi;
1013         znode_phys_t *zp;
1014         int error;
1015
1016         if ((error = dmu_bonus_hold(osp, obj, FTAG, &db)) != 0)
1017                 return (error);
1018
1019         dmu_object_info_from_db(db, &doi);
1020         if (doi.doi_bonus_type != DMU_OT_ZNODE ||
1021             doi.doi_bonus_size < sizeof (znode_phys_t)) {
1022                 dmu_buf_rele(db, FTAG);
1023                 return (EINVAL);
1024         }
1025
1026         zp = db->db_data;
1027         *pobjp = zp->zp_parent;
1028         *is_xattrdir = ((zp->zp_flags & ZFS_XATTR) != 0) &&
1029             S_ISDIR(zp->zp_mode);
1030         dmu_buf_rele(db, FTAG);
1031
1032         return (0);
1033 }
1034
1035 int
1036 zfs_obj_to_path(objset_t *osp, uint64_t obj, char *buf, int len)
1037 {
1038         char *path = buf + len - 1;
1039         int error;
1040
1041         *path = '\0';
1042
1043         for (;;) {
1044                 uint64_t pobj;
1045                 char component[MAXNAMELEN + 2];
1046                 size_t complen;
1047                 int is_xattrdir;
1048
1049                 if ((error = zfs_obj_to_pobj(osp, obj, &pobj,
1050                     &is_xattrdir)) != 0)
1051                         break;
1052
1053                 if (pobj == obj) {
1054                         if (path[0] != '/')
1055                                 *--path = '/';
1056                         break;
1057                 }
1058
1059                 component[0] = '/';
1060                 if (is_xattrdir) {
1061                         (void) sprintf(component + 1, "<xattrdir>");
1062                 } else {
1063                         error = zap_value_search(osp, pobj, obj, component + 1);
1064                         if (error != 0)
1065                                 break;
1066                 }
1067
1068                 complen = strlen(component);
1069                 path -= complen;
1070                 ASSERT(path >= buf);
1071                 bcopy(component, path, complen);
1072                 obj = pobj;
1073         }
1074
1075         if (error == 0)
1076                 (void) memmove(buf, path, buf + len - path);
1077         return (error);
1078 }