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.29 2008/04/27 00:45:37 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_locks;
52 int hammer_debug_btree;
54 int hammer_debug_recover; /* -1 will disable, +1 will force */
55 int hammer_debug_recover_faults;
56 int hammer_count_inodes;
57 int hammer_count_records;
58 int hammer_count_record_datas;
59 int hammer_count_volumes;
60 int hammer_count_buffers;
61 int hammer_count_nodes;
62 int hammer_count_dirtybufs; /* global */
63 int hammer_limit_dirtybufs = 100; /* per-mount */
64 int64_t hammer_contention_count;
65 int64_t hammer_zone_limit;
67 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
68 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
69 &hammer_debug_general, 0, "");
70 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
71 &hammer_debug_locks, 0, "");
72 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
73 &hammer_debug_btree, 0, "");
74 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
75 &hammer_debug_tid, 0, "");
76 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
77 &hammer_debug_recover, 0, "");
78 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
79 &hammer_debug_recover_faults, 0, "");
81 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufs, CTLFLAG_RW,
82 &hammer_limit_dirtybufs, 0, "");
84 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
85 &hammer_count_inodes, 0, "");
86 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
87 &hammer_count_records, 0, "");
88 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
89 &hammer_count_record_datas, 0, "");
90 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
91 &hammer_count_volumes, 0, "");
92 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
93 &hammer_count_buffers, 0, "");
94 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
95 &hammer_count_nodes, 0, "");
96 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufs, CTLFLAG_RD,
97 &hammer_count_dirtybufs, 0, "");
98 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
99 &hammer_zone_limit, 0, "");
100 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
101 &hammer_contention_count, 0, "");
106 static void hammer_free_hmp(struct mount *mp);
108 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
110 static int hammer_vfs_unmount(struct mount *mp, int mntflags);
111 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
112 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
114 static int hammer_vfs_sync(struct mount *mp, int waitfor);
115 static int hammer_vfs_vget(struct mount *mp, ino_t ino,
117 static int hammer_vfs_init(struct vfsconf *conf);
118 static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
120 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
121 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
122 int *exflagsp, struct ucred **credanonp);
125 static struct vfsops hammer_vfsops = {
126 .vfs_mount = hammer_vfs_mount,
127 .vfs_unmount = hammer_vfs_unmount,
128 .vfs_root = hammer_vfs_root,
129 .vfs_statfs = hammer_vfs_statfs,
130 .vfs_sync = hammer_vfs_sync,
131 .vfs_vget = hammer_vfs_vget,
132 .vfs_init = hammer_vfs_init,
133 .vfs_vptofh = hammer_vfs_vptofh,
134 .vfs_fhtovp = hammer_vfs_fhtovp,
135 .vfs_checkexp = hammer_vfs_checkexp
138 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
140 VFS_SET(hammer_vfsops, hammer, 0);
141 MODULE_VERSION(hammer, 1);
144 hammer_vfs_init(struct vfsconf *conf)
146 /*hammer_init_alist_config();*/
151 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
154 struct hammer_mount_info info;
156 hammer_volume_t rootvol;
157 struct vnode *rootvp;
158 const char *upath; /* volume name in userspace */
159 char *path; /* volume name in system space */
163 if ((error = copyin(data, &info, sizeof(info))) != 0)
165 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
169 * Interal mount data structure
171 if (mp->mnt_flag & MNT_UPDATE) {
172 hmp = (void *)mp->mnt_data;
173 KKASSERT(hmp != NULL);
175 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
176 mp->mnt_data = (qaddr_t)hmp;
178 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
179 hmp->namekey_iterator = mycpu->gd_time_seconds;
180 /*TAILQ_INIT(&hmp->recycle_list);*/
182 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
183 hmp->root_btree_beg.key = -0x8000000000000000LL;
184 hmp->root_btree_beg.create_tid = 1;
185 hmp->root_btree_beg.delete_tid = 1;
186 hmp->root_btree_beg.rec_type = 0;
187 hmp->root_btree_beg.obj_type = 0;
189 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
190 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
191 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
192 hmp->root_btree_end.delete_tid = 0; /* special case */
193 hmp->root_btree_end.rec_type = 0xFFFFU;
194 hmp->root_btree_end.obj_type = 0;
195 lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
197 hmp->sync_lock.refs = 1;
199 TAILQ_INIT(&hmp->flush_list);
201 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
202 hmp->zone_limits[i] =
203 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
205 * Sysctl override for debugging (force the zone
206 * the cycle more quickly then every 2^60 bytes).
208 if (hammer_zone_limit) {
209 hmp->zone_limits[i] =
210 HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
212 hammer_init_holes(hmp, &hmp->holes[i]);
215 hmp->hflags = info.hflags;
217 mp->mnt_flag |= MNT_RDONLY;
218 hmp->asof = info.asof;
220 hmp->asof = HAMMER_MAX_TID;
224 * Re-open read-write if originally read-only, or vise-versa XXX
226 if (mp->mnt_flag & MNT_UPDATE) {
227 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
228 kprintf("HAMMER read-write -> read-only XXX\n");
230 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
231 kprintf("HAMMER read-only -> read-write XXX\n");
237 RB_INIT(&hmp->rb_vols_root);
238 RB_INIT(&hmp->rb_inos_root);
239 RB_INIT(&hmp->rb_nods_root);
240 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
242 TAILQ_INIT(&hmp->volu_list);
243 TAILQ_INIT(&hmp->undo_list);
244 TAILQ_INIT(&hmp->data_list);
245 TAILQ_INIT(&hmp->meta_list);
246 TAILQ_INIT(&hmp->lose_list);
251 path = objcache_get(namei_oc, M_WAITOK);
252 hmp->nvolumes = info.nvolumes;
253 for (i = 0; i < info.nvolumes; ++i) {
254 error = copyin(&info.volumes[i], &upath, sizeof(char *));
256 error = copyinstr(upath, path, MAXPATHLEN, NULL);
258 error = hammer_install_volume(hmp, path);
262 objcache_put(namei_oc, path);
265 * Make sure we found a root volume
267 if (error == 0 && hmp->rootvol == NULL) {
268 kprintf("hammer_mount: No root volume found!\n");
277 * No errors, setup enough of the mount point so we can lookup the
280 mp->mnt_iosize_max = MAXPHYS;
281 mp->mnt_kern_flag |= MNTK_FSMID;
284 * note: f_iosize is used by vnode_pager_haspage() when constructing
287 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
288 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
289 mp->mnt_maxsymlinklen = 255;
290 mp->mnt_flag |= MNT_LOCAL;
292 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
293 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
294 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
297 * The root volume's ondisk pointer is only valid if we hold a
300 rootvol = hammer_get_root_volume(hmp, &error);
305 * Perform any necessary UNDO operations
307 error = hammer_recover(hmp, rootvol);
309 kprintf("Failed to recover HAMMER filesystem on mount\n");
314 * Finish setup now that we have a good root volume
316 ksnprintf(mp->mnt_stat.f_mntfromname,
317 sizeof(mp->mnt_stat.f_mntfromname), "%s",
318 rootvol->ondisk->vol_name);
319 mp->mnt_stat.f_fsid.val[0] =
320 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
321 mp->mnt_stat.f_fsid.val[1] =
322 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
324 hmp->next_tid = rootvol->ondisk->vol0_next_tid;
325 kprintf("on-disk next_tid %016llx\n", hmp->next_tid);
327 hammer_flusher_create(hmp);
330 * Locate the root directory using the root cluster's B-Tree as a
331 * starting point. The root directory uses an obj_id of 1.
333 * FUTURE: Leave the root directory cached referenced but unlocked
334 * in hmp->rootvp (need to flush it on unmount).
336 error = hammer_vfs_vget(mp, 1, &rootvp);
340 /*vn_unlock(hmp->rootvp);*/
343 hammer_rel_volume(rootvol, 0);
346 * Cleanup and return.
354 hammer_vfs_unmount(struct mount *mp, int mntflags)
357 struct hammer_mount *hmp = (void *)mp->mnt_data;
363 * Clean out the vnodes
366 if (mntflags & MNT_FORCE)
368 if ((error = vflush(mp, 0, flags)) != 0)
372 * Clean up the internal mount structure and related entities. This
380 * Clean up the internal mount structure and disassociate it from the mount.
381 * This may issue I/O.
384 hammer_free_hmp(struct mount *mp)
386 struct hammer_mount *hmp = (void *)mp->mnt_data;
391 * Clean up the root vnode
398 hammer_flusher_sync(hmp);
399 hammer_flusher_sync(hmp);
400 hammer_flusher_destroy(hmp);
402 KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
406 * Unload & flush inodes
408 * XXX illegal to call this from here, it can only be done from
411 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
412 hammer_unload_inode, (void *)MNT_WAIT);
415 * Unload & flush volumes
421 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
422 hammer_unload_volume, NULL);
425 mp->mnt_flag &= ~MNT_LOCAL;
427 kfree(hmp->zbuf, M_HAMMER);
428 lockuninit(&hmp->blockmap_lock);
430 for (i = 0; i < HAMMER_MAX_ZONES; ++i)
431 hammer_free_holes(hmp, &hmp->holes[i]);
433 kfree(hmp, M_HAMMER);
437 * Obtain a vnode for the specified inode number. An exclusively locked
441 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
443 struct hammer_transaction trans;
444 struct hammer_mount *hmp = (void *)mp->mnt_data;
445 struct hammer_inode *ip;
448 hammer_simple_transaction(&trans, hmp);
451 * Lookup the requested HAMMER inode. The structure must be
452 * left unlocked while we manipulate the related vnode to avoid
455 ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
460 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
461 hammer_rel_inode(ip, 0);
462 hammer_done_transaction(&trans);
467 * Return the root vnode for the filesystem.
469 * HAMMER stores the root vnode in the hammer_mount structure so
470 * getting it is easy.
473 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
476 struct hammer_mount *hmp = (void *)mp->mnt_data;
480 error = hammer_vfs_vget(mp, 1, vpp);
485 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
487 struct hammer_mount *hmp = (void *)mp->mnt_data;
488 hammer_volume_t volume;
489 hammer_volume_ondisk_t ondisk;
493 volume = hammer_get_root_volume(hmp, &error);
496 ondisk = volume->ondisk;
501 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
502 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
503 hammer_rel_volume(volume, 0);
505 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
506 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
507 if (mp->mnt_stat.f_files < 0)
508 mp->mnt_stat.f_files = 0;
515 hammer_vfs_sync(struct mount *mp, int waitfor)
517 struct hammer_mount *hmp = (void *)mp->mnt_data;
518 return(hammer_sync_hmp(hmp, waitfor));
522 * Convert a vnode to a file handle.
525 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
529 KKASSERT(MAXFIDSZ >= 16);
531 fhp->fid_len = offsetof(struct fid, fid_data[16]);
532 fhp->fid_reserved = 0;
533 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
534 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
540 * Convert a file handle back to a vnode.
543 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
545 struct hammer_transaction trans;
546 struct hammer_inode *ip;
547 struct hammer_inode_info info;
550 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
551 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
553 hammer_simple_transaction(&trans, (void *)mp->mnt_data);
556 * Get/allocate the hammer_inode structure. The structure must be
557 * unlocked while we manipulate the related vnode to avoid a
560 ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
566 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
567 hammer_rel_inode(ip, 0);
568 hammer_done_transaction(&trans);
573 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
574 int *exflagsp, struct ucred **credanonp)
576 hammer_mount_t hmp = (void *)mp->mnt_data;
580 np = vfs_export_lookup(mp, &hmp->export, nam);
582 *exflagsp = np->netc_exflags;
583 *credanonp = &np->netc_anon;
593 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
595 hammer_mount_t hmp = (void *)mp->mnt_data;
599 case MOUNTCTL_SET_EXPORT:
600 error = vfs_export(mp, &hmp->export, export);