2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.33 2008/05/04 09:06:45 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/vnode.h>
41 #include <sys/mount.h>
42 #include <sys/malloc.h>
43 #include <sys/nlookup.h>
44 #include <sys/fcntl.h>
45 #include <sys/sysctl.h>
50 int hammer_debug_general;
51 int hammer_debug_inode;
52 int hammer_debug_locks;
53 int hammer_debug_btree;
55 int hammer_debug_recover; /* -1 will disable, +1 will force */
56 int hammer_debug_recover_faults;
57 int hammer_count_inodes;
58 int hammer_count_records;
59 int hammer_count_record_datas;
60 int hammer_count_volumes;
61 int hammer_count_buffers;
62 int hammer_count_nodes;
63 int hammer_count_dirtybufs; /* global */
64 int hammer_limit_dirtybufs = 100; /* per-mount */
66 int64_t hammer_contention_count;
67 int64_t hammer_zone_limit;
69 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
70 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
71 &hammer_debug_general, 0, "");
72 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
73 &hammer_debug_inode, 0, "");
74 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
75 &hammer_debug_locks, 0, "");
76 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
77 &hammer_debug_btree, 0, "");
78 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
79 &hammer_debug_tid, 0, "");
80 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
81 &hammer_debug_recover, 0, "");
82 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
83 &hammer_debug_recover_faults, 0, "");
85 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW,
86 &hammer_limit_dirtybufs, 0, "");
88 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
89 &hammer_count_inodes, 0, "");
90 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
91 &hammer_count_records, 0, "");
92 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
93 &hammer_count_record_datas, 0, "");
94 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
95 &hammer_count_volumes, 0, "");
96 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
97 &hammer_count_buffers, 0, "");
98 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
99 &hammer_count_nodes, 0, "");
100 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD,
101 &hammer_count_dirtybufs, 0, "");
102 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
103 &hammer_zone_limit, 0, "");
104 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
105 &hammer_contention_count, 0, "");
110 static void hammer_free_hmp(struct mount *mp);
112 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
114 static int hammer_vfs_unmount(struct mount *mp, int mntflags);
115 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
116 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
118 static int hammer_vfs_sync(struct mount *mp, int waitfor);
119 static int hammer_vfs_vget(struct mount *mp, ino_t ino,
121 static int hammer_vfs_init(struct vfsconf *conf);
122 static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
124 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
125 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
126 int *exflagsp, struct ucred **credanonp);
129 static struct vfsops hammer_vfsops = {
130 .vfs_mount = hammer_vfs_mount,
131 .vfs_unmount = hammer_vfs_unmount,
132 .vfs_root = hammer_vfs_root,
133 .vfs_statfs = hammer_vfs_statfs,
134 .vfs_sync = hammer_vfs_sync,
135 .vfs_vget = hammer_vfs_vget,
136 .vfs_init = hammer_vfs_init,
137 .vfs_vptofh = hammer_vfs_vptofh,
138 .vfs_fhtovp = hammer_vfs_fhtovp,
139 .vfs_checkexp = hammer_vfs_checkexp
142 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
144 VFS_SET(hammer_vfsops, hammer, 0);
145 MODULE_VERSION(hammer, 1);
148 hammer_vfs_init(struct vfsconf *conf)
150 /*hammer_init_alist_config();*/
155 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
158 struct hammer_mount_info info;
160 hammer_volume_t rootvol;
161 struct vnode *rootvp;
162 const char *upath; /* volume name in userspace */
163 char *path; /* volume name in system space */
167 if ((error = copyin(data, &info, sizeof(info))) != 0)
169 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
173 * Interal mount data structure
175 if (mp->mnt_flag & MNT_UPDATE) {
176 hmp = (void *)mp->mnt_data;
177 KKASSERT(hmp != NULL);
179 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
180 mp->mnt_data = (qaddr_t)hmp;
182 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
183 hmp->namekey_iterator = mycpu->gd_time_seconds;
184 /*TAILQ_INIT(&hmp->recycle_list);*/
186 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
187 hmp->root_btree_beg.key = -0x8000000000000000LL;
188 hmp->root_btree_beg.create_tid = 1;
189 hmp->root_btree_beg.delete_tid = 1;
190 hmp->root_btree_beg.rec_type = 0;
191 hmp->root_btree_beg.obj_type = 0;
193 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
194 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
195 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
196 hmp->root_btree_end.delete_tid = 0; /* special case */
197 hmp->root_btree_end.rec_type = 0xFFFFU;
198 hmp->root_btree_end.obj_type = 0;
199 lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
201 hmp->sync_lock.refs = 1;
202 hmp->free_lock.refs = 1;
204 TAILQ_INIT(&hmp->flush_list);
205 TAILQ_INIT(&hmp->objid_cache_list);
206 TAILQ_INIT(&hmp->undo_lru_list);
209 * Set default zone limits. This value can be reduced
210 * further by the zone limit specified in the root volume.
212 * The sysctl can force a small zone limit for debugging
215 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
216 hmp->zone_limits[i] =
217 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
219 if (hammer_zone_limit) {
220 hmp->zone_limits[i] =
221 HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
223 hammer_init_holes(hmp, &hmp->holes[i]);
226 hmp->hflags = info.hflags;
228 mp->mnt_flag |= MNT_RDONLY;
229 hmp->asof = info.asof;
231 hmp->asof = HAMMER_MAX_TID;
235 * Re-open read-write if originally read-only, or vise-versa XXX
237 if (mp->mnt_flag & MNT_UPDATE) {
238 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
239 kprintf("HAMMER read-write -> read-only XXX\n");
241 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
242 kprintf("HAMMER read-only -> read-write XXX\n");
248 RB_INIT(&hmp->rb_vols_root);
249 RB_INIT(&hmp->rb_inos_root);
250 RB_INIT(&hmp->rb_nods_root);
251 RB_INIT(&hmp->rb_undo_root);
252 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
254 TAILQ_INIT(&hmp->volu_list);
255 TAILQ_INIT(&hmp->undo_list);
256 TAILQ_INIT(&hmp->data_list);
257 TAILQ_INIT(&hmp->meta_list);
258 TAILQ_INIT(&hmp->lose_list);
263 path = objcache_get(namei_oc, M_WAITOK);
264 hmp->nvolumes = info.nvolumes;
265 for (i = 0; i < info.nvolumes; ++i) {
266 error = copyin(&info.volumes[i], &upath, sizeof(char *));
268 error = copyinstr(upath, path, MAXPATHLEN, NULL);
270 error = hammer_install_volume(hmp, path);
274 objcache_put(namei_oc, path);
277 * Make sure we found a root volume
279 if (error == 0 && hmp->rootvol == NULL) {
280 kprintf("hammer_mount: No root volume found!\n");
289 * No errors, setup enough of the mount point so we can lookup the
292 mp->mnt_iosize_max = MAXPHYS;
293 mp->mnt_kern_flag |= MNTK_FSMID;
296 * note: f_iosize is used by vnode_pager_haspage() when constructing
299 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
300 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
301 mp->mnt_maxsymlinklen = 255;
302 mp->mnt_flag |= MNT_LOCAL;
304 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
305 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
306 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
309 * The root volume's ondisk pointer is only valid if we hold a
312 rootvol = hammer_get_root_volume(hmp, &error);
317 * Perform any necessary UNDO operations. The recover code does
318 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
319 * and then re-copy it again after recovery is complete.
321 * The recover code will load hmp->flusher_undo_start.
323 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
324 sizeof(hmp->blockmap));
326 error = hammer_recover(hmp, rootvol);
328 kprintf("Failed to recover HAMMER filesystem on mount\n");
333 * Finish setup now that we have a good root volume
335 ksnprintf(mp->mnt_stat.f_mntfromname,
336 sizeof(mp->mnt_stat.f_mntfromname), "%s",
337 rootvol->ondisk->vol_name);
338 mp->mnt_stat.f_fsid.val[0] =
339 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
340 mp->mnt_stat.f_fsid.val[1] =
341 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
344 * Certain often-modified fields in the root volume are cached in
345 * the hammer_mount structure so we do not have to generate lots
346 * of little UNDO structures for them.
348 * Recopy after recovery.
350 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
351 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
352 sizeof(hmp->blockmap));
355 * Use the zone limit set by newfs_hammer, or the zone limit set by
356 * sysctl (for debugging), whichever is smaller.
358 if (rootvol->ondisk->vol0_zone_limit) {
359 hammer_off_t vol0_zone_limit;
361 vol0_zone_limit = rootvol->ondisk->vol0_zone_limit;
362 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
363 if (hmp->zone_limits[i] > vol0_zone_limit)
364 hmp->zone_limits[i] = vol0_zone_limit;
368 hammer_flusher_create(hmp);
371 * Locate the root directory using the root cluster's B-Tree as a
372 * starting point. The root directory uses an obj_id of 1.
374 * FUTURE: Leave the root directory cached referenced but unlocked
375 * in hmp->rootvp (need to flush it on unmount).
377 error = hammer_vfs_vget(mp, 1, &rootvp);
381 /*vn_unlock(hmp->rootvp);*/
384 hammer_rel_volume(rootvol, 0);
387 * Cleanup and return.
395 hammer_vfs_unmount(struct mount *mp, int mntflags)
398 struct hammer_mount *hmp = (void *)mp->mnt_data;
404 * Clean out the vnodes
407 if (mntflags & MNT_FORCE)
409 if ((error = vflush(mp, 0, flags)) != 0)
413 * Clean up the internal mount structure and related entities. This
421 * Clean up the internal mount structure and disassociate it from the mount.
422 * This may issue I/O.
425 hammer_free_hmp(struct mount *mp)
427 struct hammer_mount *hmp = (void *)mp->mnt_data;
432 * Clean up the root vnode
439 hammer_flusher_sync(hmp);
440 hammer_flusher_sync(hmp);
441 hammer_flusher_destroy(hmp);
443 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
447 * Unload & flush inodes
449 * XXX illegal to call this from here, it can only be done from
452 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
453 hammer_unload_inode, (void *)MNT_WAIT);
456 * Unload & flush volumes
462 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
463 hammer_unload_volume, NULL);
466 mp->mnt_flag &= ~MNT_LOCAL;
468 hammer_destroy_objid_cache(hmp);
469 kfree(hmp->zbuf, M_HAMMER);
470 lockuninit(&hmp->blockmap_lock);
472 for (i = 0; i < HAMMER_MAX_ZONES; ++i)
473 hammer_free_holes(hmp, &hmp->holes[i]);
475 kfree(hmp, M_HAMMER);
479 * Obtain a vnode for the specified inode number. An exclusively locked
483 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
485 struct hammer_transaction trans;
486 struct hammer_mount *hmp = (void *)mp->mnt_data;
487 struct hammer_inode *ip;
490 hammer_simple_transaction(&trans, hmp);
493 * Lookup the requested HAMMER inode. The structure must be
494 * left unlocked while we manipulate the related vnode to avoid
497 ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
502 error = hammer_get_vnode(ip, vpp);
503 hammer_rel_inode(ip, 0);
504 hammer_done_transaction(&trans);
509 * Return the root vnode for the filesystem.
511 * HAMMER stores the root vnode in the hammer_mount structure so
512 * getting it is easy.
515 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
518 struct hammer_mount *hmp = (void *)mp->mnt_data;
522 error = hammer_vfs_vget(mp, 1, vpp);
527 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
529 struct hammer_mount *hmp = (void *)mp->mnt_data;
530 hammer_volume_t volume;
531 hammer_volume_ondisk_t ondisk;
535 volume = hammer_get_root_volume(hmp, &error);
538 ondisk = volume->ondisk;
543 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
544 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
545 hammer_rel_volume(volume, 0);
547 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
548 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
549 if (mp->mnt_stat.f_files < 0)
550 mp->mnt_stat.f_files = 0;
557 * Sync the filesystem. Currently we have to run it twice, the second
558 * one will advance the undo start index to the end index, so if a crash
559 * occurs no undos will be run on mount.
562 hammer_vfs_sync(struct mount *mp, int waitfor)
564 struct hammer_mount *hmp = (void *)mp->mnt_data;
567 error = hammer_sync_hmp(hmp, waitfor);
569 error = hammer_sync_hmp(hmp, waitfor);
574 * Convert a vnode to a file handle.
577 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
581 KKASSERT(MAXFIDSZ >= 16);
583 fhp->fid_len = offsetof(struct fid, fid_data[16]);
584 fhp->fid_reserved = 0;
585 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
586 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
592 * Convert a file handle back to a vnode.
595 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
597 struct hammer_transaction trans;
598 struct hammer_inode *ip;
599 struct hammer_inode_info info;
602 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
603 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
605 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
608 * Get/allocate the hammer_inode structure. The structure must be
609 * unlocked while we manipulate the related vnode to avoid a
612 ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
618 error = hammer_get_vnode(ip, vpp);
619 hammer_rel_inode(ip, 0);
620 hammer_done_transaction(&trans);
625 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
626 int *exflagsp, struct ucred **credanonp)
628 hammer_mount_t hmp = (void *)mp->mnt_data;
632 np = vfs_export_lookup(mp, &hmp->export, nam);
634 *exflagsp = np->netc_exflags;
635 *credanonp = &np->netc_anon;
645 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
647 hammer_mount_t hmp = (void *)mp->mnt_data;
651 case MOUNTCTL_SET_EXPORT:
652 error = vfs_export(mp, &hmp->export, export);