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.32 2008/05/03 20:21:20 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;
203 TAILQ_INIT(&hmp->flush_list);
204 TAILQ_INIT(&hmp->objid_cache_list);
205 TAILQ_INIT(&hmp->undo_lru_list);
208 * Set default zone limits. This value can be reduced
209 * further by the zone limit specified in the root volume.
211 * The sysctl can force a small zone limit for debugging
214 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
215 hmp->zone_limits[i] =
216 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
218 if (hammer_zone_limit) {
219 hmp->zone_limits[i] =
220 HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
222 hammer_init_holes(hmp, &hmp->holes[i]);
225 hmp->hflags = info.hflags;
227 mp->mnt_flag |= MNT_RDONLY;
228 hmp->asof = info.asof;
230 hmp->asof = HAMMER_MAX_TID;
234 * Re-open read-write if originally read-only, or vise-versa XXX
236 if (mp->mnt_flag & MNT_UPDATE) {
237 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
238 kprintf("HAMMER read-write -> read-only XXX\n");
240 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
241 kprintf("HAMMER read-only -> read-write XXX\n");
247 RB_INIT(&hmp->rb_vols_root);
248 RB_INIT(&hmp->rb_inos_root);
249 RB_INIT(&hmp->rb_nods_root);
250 RB_INIT(&hmp->rb_undo_root);
251 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
253 TAILQ_INIT(&hmp->volu_list);
254 TAILQ_INIT(&hmp->undo_list);
255 TAILQ_INIT(&hmp->data_list);
256 TAILQ_INIT(&hmp->meta_list);
257 TAILQ_INIT(&hmp->lose_list);
262 path = objcache_get(namei_oc, M_WAITOK);
263 hmp->nvolumes = info.nvolumes;
264 for (i = 0; i < info.nvolumes; ++i) {
265 error = copyin(&info.volumes[i], &upath, sizeof(char *));
267 error = copyinstr(upath, path, MAXPATHLEN, NULL);
269 error = hammer_install_volume(hmp, path);
273 objcache_put(namei_oc, path);
276 * Make sure we found a root volume
278 if (error == 0 && hmp->rootvol == NULL) {
279 kprintf("hammer_mount: No root volume found!\n");
288 * No errors, setup enough of the mount point so we can lookup the
291 mp->mnt_iosize_max = MAXPHYS;
292 mp->mnt_kern_flag |= MNTK_FSMID;
295 * note: f_iosize is used by vnode_pager_haspage() when constructing
298 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
299 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
300 mp->mnt_maxsymlinklen = 255;
301 mp->mnt_flag |= MNT_LOCAL;
303 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
304 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
305 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
308 * The root volume's ondisk pointer is only valid if we hold a
311 rootvol = hammer_get_root_volume(hmp, &error);
316 * Perform any necessary UNDO operations. The recover code does
317 * call hammer_undo_lookup() so we have to pre-cache the blockmap,
318 * and then re-copy it again after recovery is complete.
320 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
321 sizeof(hmp->blockmap));
322 error = hammer_recover(hmp, rootvol);
324 kprintf("Failed to recover HAMMER filesystem on mount\n");
329 * Finish setup now that we have a good root volume
331 ksnprintf(mp->mnt_stat.f_mntfromname,
332 sizeof(mp->mnt_stat.f_mntfromname), "%s",
333 rootvol->ondisk->vol_name);
334 mp->mnt_stat.f_fsid.val[0] =
335 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
336 mp->mnt_stat.f_fsid.val[1] =
337 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
340 * Certain often-modified fields in the root volume are cached in
341 * the hammer_mount structure so we do not have to generate lots
342 * of little UNDO structures for them.
344 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
345 bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
346 sizeof(hmp->blockmap));
349 * Use the zone limit set by newfs_hammer, or the zone limit set by
350 * sysctl (for debugging), whichever is smaller.
352 if (rootvol->ondisk->vol0_zone_limit) {
353 hammer_off_t vol0_zone_limit;
355 vol0_zone_limit = rootvol->ondisk->vol0_zone_limit;
356 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
357 if (hmp->zone_limits[i] > vol0_zone_limit)
358 hmp->zone_limits[i] = vol0_zone_limit;
362 hammer_flusher_create(hmp);
365 * Locate the root directory using the root cluster's B-Tree as a
366 * starting point. The root directory uses an obj_id of 1.
368 * FUTURE: Leave the root directory cached referenced but unlocked
369 * in hmp->rootvp (need to flush it on unmount).
371 error = hammer_vfs_vget(mp, 1, &rootvp);
375 /*vn_unlock(hmp->rootvp);*/
378 hammer_rel_volume(rootvol, 0);
381 * Cleanup and return.
389 hammer_vfs_unmount(struct mount *mp, int mntflags)
392 struct hammer_mount *hmp = (void *)mp->mnt_data;
398 * Clean out the vnodes
401 if (mntflags & MNT_FORCE)
403 if ((error = vflush(mp, 0, flags)) != 0)
407 * Clean up the internal mount structure and related entities. This
415 * Clean up the internal mount structure and disassociate it from the mount.
416 * This may issue I/O.
419 hammer_free_hmp(struct mount *mp)
421 struct hammer_mount *hmp = (void *)mp->mnt_data;
426 * Clean up the root vnode
433 hammer_flusher_sync(hmp);
434 hammer_flusher_sync(hmp);
435 hammer_flusher_destroy(hmp);
437 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
441 * Unload & flush inodes
443 * XXX illegal to call this from here, it can only be done from
446 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
447 hammer_unload_inode, (void *)MNT_WAIT);
450 * Unload & flush volumes
456 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
457 hammer_unload_volume, NULL);
460 mp->mnt_flag &= ~MNT_LOCAL;
462 hammer_destroy_objid_cache(hmp);
463 kfree(hmp->zbuf, M_HAMMER);
464 lockuninit(&hmp->blockmap_lock);
466 for (i = 0; i < HAMMER_MAX_ZONES; ++i)
467 hammer_free_holes(hmp, &hmp->holes[i]);
469 kfree(hmp, M_HAMMER);
473 * Obtain a vnode for the specified inode number. An exclusively locked
477 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
479 struct hammer_transaction trans;
480 struct hammer_mount *hmp = (void *)mp->mnt_data;
481 struct hammer_inode *ip;
484 hammer_simple_transaction(&trans, hmp);
487 * Lookup the requested HAMMER inode. The structure must be
488 * left unlocked while we manipulate the related vnode to avoid
491 ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
496 error = hammer_get_vnode(ip, vpp);
497 hammer_rel_inode(ip, 0);
498 hammer_done_transaction(&trans);
503 * Return the root vnode for the filesystem.
505 * HAMMER stores the root vnode in the hammer_mount structure so
506 * getting it is easy.
509 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
512 struct hammer_mount *hmp = (void *)mp->mnt_data;
516 error = hammer_vfs_vget(mp, 1, vpp);
521 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
523 struct hammer_mount *hmp = (void *)mp->mnt_data;
524 hammer_volume_t volume;
525 hammer_volume_ondisk_t ondisk;
529 volume = hammer_get_root_volume(hmp, &error);
532 ondisk = volume->ondisk;
537 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
538 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
539 hammer_rel_volume(volume, 0);
541 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
542 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
543 if (mp->mnt_stat.f_files < 0)
544 mp->mnt_stat.f_files = 0;
551 * Sync the filesystem. Currently we have to run it twice, the second
552 * one will advance the undo start index to the end index, so if a crash
553 * occurs no undos will be run on mount.
556 hammer_vfs_sync(struct mount *mp, int waitfor)
558 struct hammer_mount *hmp = (void *)mp->mnt_data;
561 error = hammer_sync_hmp(hmp, waitfor);
563 error = hammer_sync_hmp(hmp, waitfor);
568 * Convert a vnode to a file handle.
571 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
575 KKASSERT(MAXFIDSZ >= 16);
577 fhp->fid_len = offsetof(struct fid, fid_data[16]);
578 fhp->fid_reserved = 0;
579 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
580 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
586 * Convert a file handle back to a vnode.
589 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
591 struct hammer_transaction trans;
592 struct hammer_inode *ip;
593 struct hammer_inode_info info;
596 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
597 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
599 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
602 * Get/allocate the hammer_inode structure. The structure must be
603 * unlocked while we manipulate the related vnode to avoid a
606 ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
612 error = hammer_get_vnode(ip, vpp);
613 hammer_rel_inode(ip, 0);
614 hammer_done_transaction(&trans);
619 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
620 int *exflagsp, struct ucred **credanonp)
622 hammer_mount_t hmp = (void *)mp->mnt_data;
626 np = vfs_export_lookup(mp, &hmp->export, nam);
628 *exflagsp = np->netc_exflags;
629 *credanonp = &np->netc_anon;
639 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
641 hammer_mount_t hmp = (void *)mp->mnt_data;
645 case MOUNTCTL_SET_EXPORT:
646 error = vfs_export(mp, &hmp->export, export);