2 * Copyright (c) 2007 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.20 2008/02/20 00:55:51 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_btree;
53 int hammer_debug_recover; /* -1 will disable, +1 will force */
54 int hammer_debug_recover_faults;
55 int hammer_count_inodes;
56 int hammer_count_records;
57 int hammer_count_record_datas;
58 int hammer_count_volumes;
59 int hammer_count_buffers;
60 int hammer_count_nodes;
62 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
63 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
64 &hammer_debug_general, 0, "");
65 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
66 &hammer_debug_btree, 0, "");
67 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
68 &hammer_debug_tid, 0, "");
69 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
70 &hammer_debug_recover, 0, "");
71 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
72 &hammer_debug_recover_faults, 0, "");
73 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
74 &hammer_count_inodes, 0, "");
75 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
76 &hammer_count_records, 0, "");
77 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
78 &hammer_count_record_datas, 0, "");
79 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
80 &hammer_count_volumes, 0, "");
81 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
82 &hammer_count_buffers, 0, "");
83 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
84 &hammer_count_nodes, 0, "");
89 static void hammer_free_hmp(struct mount *mp);
91 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
93 static int hammer_vfs_unmount(struct mount *mp, int mntflags);
94 static int hammer_vfs_root(struct mount *mp, struct vnode **vpp);
95 static int hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
97 static int hammer_vfs_sync(struct mount *mp, int waitfor);
98 static int hammer_vfs_vget(struct mount *mp, ino_t ino,
100 static int hammer_vfs_init(struct vfsconf *conf);
101 static int hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
103 static int hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
104 static int hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
105 int *exflagsp, struct ucred **credanonp);
108 static struct vfsops hammer_vfsops = {
109 .vfs_mount = hammer_vfs_mount,
110 .vfs_unmount = hammer_vfs_unmount,
111 .vfs_root = hammer_vfs_root,
112 .vfs_statfs = hammer_vfs_statfs,
113 .vfs_sync = hammer_vfs_sync,
114 .vfs_vget = hammer_vfs_vget,
115 .vfs_init = hammer_vfs_init,
116 .vfs_vptofh = hammer_vfs_vptofh,
117 .vfs_fhtovp = hammer_vfs_fhtovp,
118 .vfs_checkexp = hammer_vfs_checkexp
121 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
123 VFS_SET(hammer_vfsops, hammer, 0);
124 MODULE_VERSION(hammer, 1);
127 hammer_vfs_init(struct vfsconf *conf)
129 /*hammer_init_alist_config();*/
134 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
137 struct hammer_mount_info info;
139 hammer_volume_t rootvol;
140 struct vnode *rootvp;
141 const char *upath; /* volume name in userspace */
142 char *path; /* volume name in system space */
146 if ((error = copyin(data, &info, sizeof(info))) != 0)
148 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
152 * Interal mount data structure
154 if (mp->mnt_flag & MNT_UPDATE) {
155 hmp = (void *)mp->mnt_data;
156 KKASSERT(hmp != NULL);
158 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
159 mp->mnt_data = (qaddr_t)hmp;
161 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
162 hmp->namekey_iterator = mycpu->gd_time_seconds;
163 /*TAILQ_INIT(&hmp->recycle_list);*/
165 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
166 hmp->root_btree_beg.key = -0x8000000000000000LL;
167 hmp->root_btree_beg.create_tid = 1;
168 hmp->root_btree_beg.delete_tid = 1;
169 hmp->root_btree_beg.rec_type = 0;
170 hmp->root_btree_beg.obj_type = 0;
172 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
173 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
174 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
175 hmp->root_btree_end.delete_tid = 0; /* special case */
176 hmp->root_btree_end.rec_type = 0xFFFFU;
177 hmp->root_btree_end.obj_type = 0;
178 lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
180 hmp->hflags = info.hflags;
182 mp->mnt_flag |= MNT_RDONLY;
183 hmp->asof = info.asof;
185 hmp->asof = HAMMER_MAX_TID;
189 * Re-open read-write if originally read-only, or vise-versa XXX
191 if (mp->mnt_flag & MNT_UPDATE) {
192 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
193 kprintf("HAMMER read-write -> read-only XXX\n");
195 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
196 kprintf("HAMMER read-only -> read-write XXX\n");
202 RB_INIT(&hmp->rb_vols_root);
203 RB_INIT(&hmp->rb_inos_root);
204 RB_INIT(&hmp->rb_nods_root);
205 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
210 path = objcache_get(namei_oc, M_WAITOK);
211 hmp->nvolumes = info.nvolumes;
212 for (i = 0; i < info.nvolumes; ++i) {
213 error = copyin(&info.volumes[i], &upath, sizeof(char *));
215 error = copyinstr(upath, path, MAXPATHLEN, NULL);
217 error = hammer_install_volume(hmp, path);
221 objcache_put(namei_oc, path);
224 * Make sure we found a root volume
226 if (error == 0 && hmp->rootvol == NULL) {
227 kprintf("hammer_mount: No root volume found!\n");
236 * No errors, setup enough of the mount point so we can lookup the
239 mp->mnt_iosize_max = MAXPHYS;
240 mp->mnt_kern_flag |= MNTK_FSMID;
243 * note: f_iosize is used by vnode_pager_haspage() when constructing
246 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
247 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
248 mp->mnt_maxsymlinklen = 255;
249 mp->mnt_flag |= MNT_LOCAL;
251 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
252 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
253 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
256 * The root volume's ondisk pointer is only valid if we hold a
259 rootvol = hammer_get_root_volume(hmp, &error);
262 ksnprintf(mp->mnt_stat.f_mntfromname,
263 sizeof(mp->mnt_stat.f_mntfromname), "%s",
264 rootvol->ondisk->vol_name);
265 mp->mnt_stat.f_fsid.val[0] =
266 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
267 mp->mnt_stat.f_fsid.val[1] =
268 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
269 hammer_rel_volume(rootvol, 0);
272 * Locate the root directory using the root cluster's B-Tree as a
273 * starting point. The root directory uses an obj_id of 1.
275 * FUTURE: Leave the root directory cached referenced but unlocked
276 * in hmp->rootvp (need to flush it on unmount).
278 error = hammer_vfs_vget(mp, 1, &rootvp);
282 /*vn_unlock(hmp->rootvp);*/
286 * Cleanup and return.
294 hammer_vfs_unmount(struct mount *mp, int mntflags)
297 struct hammer_mount *hmp = (void *)mp->mnt_data;
303 * Clean out the vnodes
306 if (mntflags & MNT_FORCE)
308 if ((error = vflush(mp, 0, flags)) != 0)
312 * Clean up the internal mount structure and related entities. This
320 * Clean up the internal mount structure and disassociate it from the mount.
321 * This may issue I/O.
324 hammer_free_hmp(struct mount *mp)
326 struct hammer_mount *hmp = (void *)mp->mnt_data;
330 * Clean up the root vnode
339 * Unload & flush inodes
341 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
342 hammer_unload_inode, (void *)MNT_WAIT);
345 * Unload & flush volumes
347 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
348 hammer_unload_volume, NULL);
351 mp->mnt_flag &= ~MNT_LOCAL;
353 kfree(hmp->zbuf, M_HAMMER);
354 lockuninit(&hmp->blockmap_lock);
355 kfree(hmp, M_HAMMER);
359 * Obtain a vnode for the specified inode number. An exclusively locked
363 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
365 struct hammer_mount *hmp = (void *)mp->mnt_data;
366 struct hammer_inode *ip;
370 * Get/allocate the hammer_inode structure. The structure must be
371 * unlocked while we manipulate the related vnode to avoid a
374 ip = hammer_get_inode(hmp, NULL, ino, hmp->asof, 0, &error);
379 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
380 hammer_rel_inode(ip, 0);
385 * Return the root vnode for the filesystem.
387 * HAMMER stores the root vnode in the hammer_mount structure so
388 * getting it is easy.
391 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
394 struct hammer_mount *hmp = (void *)mp->mnt_data;
398 error = hammer_vfs_vget(mp, 1, vpp);
403 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
405 struct hammer_mount *hmp = (void *)mp->mnt_data;
406 hammer_volume_t volume;
407 hammer_volume_ondisk_t ondisk;
411 volume = hammer_get_root_volume(hmp, &error);
414 ondisk = volume->ondisk;
419 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
420 bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
421 hammer_rel_volume(volume, 0);
423 mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
424 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
425 if (mp->mnt_stat.f_files < 0)
426 mp->mnt_stat.f_files = 0;
433 hammer_vfs_sync(struct mount *mp, int waitfor)
435 struct hammer_mount *hmp = (void *)mp->mnt_data;
438 error = hammer_sync_hmp(hmp, waitfor);
443 * Convert a vnode to a file handle.
446 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
450 KKASSERT(MAXFIDSZ >= 16);
452 fhp->fid_len = offsetof(struct fid, fid_data[16]);
453 fhp->fid_reserved = 0;
454 bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
455 bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
461 * Convert a file handle back to a vnode.
464 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
466 struct hammer_mount *hmp = (void *)mp->mnt_data;
467 struct hammer_inode *ip;
468 struct hammer_inode_info info;
471 bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
472 bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
475 * Get/allocate the hammer_inode structure. The structure must be
476 * unlocked while we manipulate the related vnode to avoid a
479 ip = hammer_get_inode(hmp, NULL, info.obj_id, info.obj_asof, 0, &error);
484 error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
485 hammer_rel_inode(ip, 0);
490 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
491 int *exflagsp, struct ucred **credanonp)
493 hammer_mount_t hmp = (void *)mp->mnt_data;
497 np = vfs_export_lookup(mp, &hmp->export, nam);
499 *exflagsp = np->netc_exflags;
500 *credanonp = &np->netc_anon;
510 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
512 hammer_mount_t hmp = (void *)mp->mnt_data;
516 case MOUNTCTL_SET_EXPORT:
517 error = vfs_export(mp, &hmp->export, export);