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