Remove tcsh emacs script which is obsolete due to the latest tcsh import.
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
blobcfa43ca15a0331afb64e183d25bd35b7e0dfd1d7
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.13 2008/01/01 01:00:03 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>
46 #include <sys/buf.h>
47 #include <sys/buf2.h>
48 #include "hammer.h"
50 int hammer_debug_btree;
51 int hammer_debug_tid;
52 int hammer_count_inodes;
53 int hammer_count_records;
54 int hammer_count_record_datas;
55 int hammer_count_volumes;
56 int hammer_count_supercls;
57 int hammer_count_clusters;
58 int hammer_count_buffers;
59 int hammer_count_nodes;
60 int hammer_count_spikes;
62 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
63 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
64 &hammer_debug_btree, 0, "");
65 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
66 &hammer_debug_tid, 0, "");
67 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
68 &hammer_count_inodes, 0, "");
69 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
70 &hammer_count_records, 0, "");
71 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
72 &hammer_count_record_datas, 0, "");
73 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
74 &hammer_count_volumes, 0, "");
75 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_supercls, CTLFLAG_RD,
76 &hammer_count_supercls, 0, "");
77 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_clusters, CTLFLAG_RD,
78 &hammer_count_clusters, 0, "");
79 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
80 &hammer_count_buffers, 0, "");
81 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
82 &hammer_count_nodes, 0, "");
83 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_spikes, CTLFLAG_RD,
84 &hammer_count_spikes, 0, "");
87 * VFS ABI
89 static void hammer_free_hmp(struct mount *mp);
91 static int hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
92 struct ucred *cred);
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,
96 struct ucred *cred);
97 static int hammer_vfs_sync(struct mount *mp, int waitfor);
98 static int hammer_vfs_init(struct vfsconf *conf);
100 static struct vfsops hammer_vfsops = {
101 .vfs_mount = hammer_vfs_mount,
102 .vfs_unmount = hammer_vfs_unmount,
103 .vfs_root = hammer_vfs_root,
104 .vfs_statfs = hammer_vfs_statfs,
105 .vfs_sync = hammer_vfs_sync,
106 .vfs_vget = hammer_vfs_vget,
107 .vfs_init = hammer_vfs_init
110 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
112 VFS_SET(hammer_vfsops, hammer, 0);
113 MODULE_VERSION(hammer, 1);
115 static int
116 hammer_vfs_init(struct vfsconf *conf)
118 hammer_init_alist_config();
119 return(0);
122 static int
123 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
124 struct ucred *cred)
126 struct hammer_mount_info info;
127 hammer_mount_t hmp;
128 hammer_volume_t rootvol;
129 struct vnode *rootvp;
130 const char *upath; /* volume name in userspace */
131 char *path; /* volume name in system space */
132 int error;
133 int i;
135 if ((error = copyin(data, &info, sizeof(info))) != 0)
136 return (error);
137 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
138 return (EINVAL);
141 * Interal mount data structure
143 if (mp->mnt_flag & MNT_UPDATE) {
144 hmp = (void *)mp->mnt_data;
145 KKASSERT(hmp != NULL);
146 } else {
147 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
148 mp->mnt_data = (qaddr_t)hmp;
149 hmp->mp = mp;
150 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
151 hmp->namekey_iterator = mycpu->gd_time_seconds;
153 hmp->hflags = info.hflags;
154 if (info.asof) {
155 mp->mnt_flag |= MNT_RDONLY;
156 hmp->asof = info.asof;
157 } else {
158 hmp->asof = HAMMER_MAX_TID;
162 * Re-open read-write if originally read-only, or vise-versa XXX
164 if (mp->mnt_flag & MNT_UPDATE) {
165 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
166 kprintf("HAMMER read-write -> read-only XXX\n");
167 hmp->ronly = 1;
168 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
169 kprintf("HAMMER read-only -> read-write XXX\n");
170 hmp->ronly = 0;
172 return(0);
175 RB_INIT(&hmp->rb_vols_root);
176 RB_INIT(&hmp->rb_inos_root);
177 hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
180 * Load volumes
182 path = objcache_get(namei_oc, M_WAITOK);
183 hmp->nvolumes = info.nvolumes;
184 for (i = 0; i < info.nvolumes; ++i) {
185 error = copyin(&info.volumes[i], &upath, sizeof(char *));
186 if (error == 0)
187 error = copyinstr(upath, path, MAXPATHLEN, NULL);
188 if (error == 0)
189 error = hammer_install_volume(hmp, path);
190 if (error)
191 break;
193 objcache_put(namei_oc, path);
196 * Make sure we found a root volume
198 if (error == 0 && hmp->rootvol == NULL) {
199 kprintf("hammer_mount: No root volume found!\n");
200 error = EINVAL;
202 if (error == 0 && hmp->rootcl == NULL) {
203 kprintf("hammer_mount: No root cluster found!\n");
204 error = EINVAL;
206 if (error) {
207 hammer_free_hmp(mp);
208 return (error);
212 * No errors, setup enough of the mount point so we can lookup the
213 * root vnode.
215 mp->mnt_iosize_max = MAXPHYS;
216 mp->mnt_kern_flag |= MNTK_FSMID;
217 mp->mnt_stat.f_fsid.val[0] = 0; /* XXX */
218 mp->mnt_stat.f_fsid.val[1] = 0; /* XXX */
221 * note: f_iosize is used by vnode_pager_haspage() when constructing
222 * its VOP_BMAP call.
224 mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
225 mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
226 vfs_getnewfsid(mp); /* XXX */
227 mp->mnt_maxsymlinklen = 255;
228 mp->mnt_flag |= MNT_LOCAL;
230 vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
231 vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
232 vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
235 * The root volume's ondisk pointer is only valid if we hold a
236 * reference to it.
238 rootvol = hammer_get_root_volume(hmp, &error);
239 if (error)
240 goto done;
241 ksnprintf(mp->mnt_stat.f_mntfromname,
242 sizeof(mp->mnt_stat.f_mntfromname), "%s",
243 rootvol->ondisk->vol_name);
244 hammer_rel_volume(rootvol, 0);
247 * Locate the root directory using the root cluster's B-Tree as a
248 * starting point. The root directory uses an obj_id of 1.
250 * FUTURE: Leave the root directory cached referenced but unlocked
251 * in hmp->rootvp (need to flush it on unmount).
253 error = hammer_vfs_vget(mp, 1, &rootvp);
254 if (error)
255 goto done;
256 vput(rootvp);
257 /*vn_unlock(hmp->rootvp);*/
259 done:
261 * Cleanup and return.
263 if (error)
264 hammer_free_hmp(mp);
265 return (error);
268 static int
269 hammer_vfs_unmount(struct mount *mp, int mntflags)
271 #if 0
272 struct hammer_mount *hmp = (void *)mp->mnt_data;
273 #endif
274 int flags;
275 int error;
278 * Clean out the vnodes
280 flags = 0;
281 if (mntflags & MNT_FORCE)
282 flags |= FORCECLOSE;
283 if ((error = vflush(mp, 0, flags)) != 0)
284 return (error);
287 * Clean up the internal mount structure and related entities. This
288 * may issue I/O.
290 hammer_free_hmp(mp);
291 return(0);
295 * Clean up the internal mount structure and disassociate it from the mount.
296 * This may issue I/O.
298 static void
299 hammer_free_hmp(struct mount *mp)
301 struct hammer_mount *hmp = (void *)mp->mnt_data;
303 #if 0
305 * Clean up the root vnode
307 if (hmp->rootvp) {
308 vrele(hmp->rootvp);
309 hmp->rootvp = NULL;
311 #endif
314 * Unload & flush inodes
316 RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
317 hammer_unload_inode, (void *)MNT_WAIT);
320 * Unload & flush volumes
322 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
323 hammer_unload_volume, NULL);
325 mp->mnt_data = NULL;
326 mp->mnt_flag &= ~MNT_LOCAL;
327 hmp->mp = NULL;
328 kfree(hmp->zbuf, M_HAMMER);
329 kfree(hmp, M_HAMMER);
333 * Return the root vnode for the filesystem.
335 * HAMMER stores the root vnode in the hammer_mount structure so
336 * getting it is easy.
338 static int
339 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
341 struct hammer_mount *hmp = (void *)mp->mnt_data;
342 int error;
344 if (hmp->rootcl == NULL)
345 error = EIO;
346 else
347 error = hammer_vfs_vget(mp, 1, vpp);
348 return (error);
349 #if 0
350 /* FUTURE - cached root vnode */
351 if ((vp = hmp->rootvp) != NULL) {
352 vref(vp);
353 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
354 *vpp = vp;
355 return (0);
356 } else {
357 *vpp = NULL;
358 return (EIO);
360 #endif
363 static int
364 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
366 struct hammer_mount *hmp = (void *)mp->mnt_data;
367 hammer_volume_t volume;
368 hammer_volume_ondisk_t ondisk;
369 int error;
371 volume = hammer_get_root_volume(hmp, &error);
372 if (error)
373 return(error);
375 ondisk = volume->ondisk;
377 mp->mnt_stat.f_bfree = mp->mnt_stat.f_blocks -
378 ondisk->vol0_stat_idx_bufs -
379 ondisk->vol0_stat_rec_bufs -
380 ondisk->vol0_stat_data_bufs;
381 if (mp->mnt_stat.f_bfree < 0)
382 mp->mnt_stat.f_bfree = 0;
383 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
384 mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
385 if (mp->mnt_stat.f_files < 0)
386 mp->mnt_stat.f_files = 0;
388 hammer_rel_volume(volume, 0);
389 *sbp = mp->mnt_stat;
390 return(0);
393 static int
394 hammer_vfs_sync(struct mount *mp, int waitfor)
396 struct hammer_mount *hmp = (void *)mp->mnt_data;
397 int error;
399 error = hammer_sync_hmp(hmp, waitfor);
400 return(error);