HAMMER 8/many - A-list, B-Tree fixes. As-of queries
[dragonfly.git] / sys / vfs / hammer / hammer_vfsops.c
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:
10  * 
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.
20  * 
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.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_vfsops.c,v 1.7 2007/11/27 07:48:52 dillon Exp $
35  */
36
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/buf.h>
46 #include <sys/buf2.h>
47 #include "hammer.h"
48
49 /*
50  * VFS ABI
51  */
52 static void     hammer_free_hmp(struct mount *mp);
53
54 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
55                                 struct ucred *cred);
56 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
57 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
58 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
59                                 struct ucred *cred);
60 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
61 static int      hammer_vfs_init(struct vfsconf *conf);
62
63 static struct vfsops hammer_vfsops = {
64         .vfs_mount      = hammer_vfs_mount,
65         .vfs_unmount    = hammer_vfs_unmount,
66         .vfs_root       = hammer_vfs_root,
67         .vfs_statfs     = hammer_vfs_statfs,
68         .vfs_sync       = hammer_vfs_sync,
69         .vfs_vget       = hammer_vfs_vget,
70         .vfs_init       = hammer_vfs_init
71 };
72
73 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
74
75 VFS_SET(hammer_vfsops, hammer, 0);
76 MODULE_VERSION(hammer, 1);
77
78 static int
79 hammer_vfs_init(struct vfsconf *conf)
80 {
81         hammer_init_alist_config();
82         return(0);
83 }
84
85 static int
86 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
87                  struct ucred *cred)
88 {
89         struct hammer_mount_info info;
90         hammer_mount_t hmp;
91         hammer_volume_t rootvol;
92         struct vnode *rootvp;
93         const char *upath;      /* volume name in userspace */
94         char *path;             /* volume name in system space */
95         int error;
96         int i;
97
98         if ((error = copyin(data, &info, sizeof(info))) != 0)
99                 return (error);
100         if (info.nvolumes <= 0 || info.nvolumes >= 32768)
101                 return (EINVAL);
102
103         /*
104          * Interal mount data structure
105          */
106         hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
107         mp->mnt_data = (qaddr_t)hmp;
108         hmp->mp = mp;
109         if (info.asof) {
110                 mp->mnt_flag |= MNT_RDONLY;
111                 hmp->asof = info.asof;
112         } else {
113                 hmp->asof = HAMMER_MAX_TID;
114         }
115         hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK | M_ZERO);
116         hmp->namekey_iterator = mycpu->gd_time_seconds;
117         RB_INIT(&hmp->rb_vols_root);
118         RB_INIT(&hmp->rb_inos_root);
119
120         /*
121          * Load volumes
122          */
123         path = objcache_get(namei_oc, M_WAITOK);
124         for (i = 0; i < info.nvolumes; ++i) {
125                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
126                 if (error == 0)
127                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
128                 if (error == 0)
129                         error = hammer_install_volume(hmp, path);
130                 if (error)
131                         break;
132         }
133         objcache_put(namei_oc, path);
134
135         /*
136          * Make sure we found a root volume
137          */
138         if (error == 0 && hmp->rootvol == NULL) {
139                 kprintf("hammer_mount: No root volume found!\n");
140                 error = EINVAL;
141         }
142         if (error == 0 && hmp->rootcl == NULL) {
143                 kprintf("hammer_mount: No root cluster found!\n");
144                 error = EINVAL;
145         }
146         if (error) {
147                 hammer_free_hmp(mp);
148                 return (error);
149         }
150
151         /*
152          * No errors, setup enough of the mount point so we can lookup the
153          * root vnode.
154          */
155         mp->mnt_iosize_max = MAXPHYS;
156         mp->mnt_kern_flag |= MNTK_FSMID;
157         mp->mnt_stat.f_fsid.val[0] = 0; /* XXX */
158         mp->mnt_stat.f_fsid.val[1] = 0; /* XXX */
159
160         /* 
161          * note: f_iosize is used by vnode_pager_haspage() when constructing
162          * its VOP_BMAP call.
163          */
164         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
165         vfs_getnewfsid(mp);             /* XXX */
166         mp->mnt_maxsymlinklen = 255;
167         mp->mnt_flag |= MNT_LOCAL;
168
169         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
170
171         /*
172          * The root volume's ondisk pointer is only valid if we hold a
173          * reference to it.
174          */
175         rootvol = hammer_get_root_volume(hmp, &error);
176         if (error)
177                 goto done;
178         ksnprintf(mp->mnt_stat.f_mntfromname,
179                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
180                   rootvol->ondisk->vol_name);
181         hammer_rel_volume(rootvol, 0);
182
183         /*
184          * Locate the root directory using the root cluster's B-Tree as a
185          * starting point.  The root directory uses an obj_id of 1.
186          *
187          * FUTURE: Leave the root directory cached referenced but unlocked
188          * in hmp->rootvp (need to flush it on unmount).
189          */
190         error = hammer_vfs_vget(mp, 1, &rootvp);
191         if (error)
192                 goto done;
193         vput(rootvp);
194         /*vn_unlock(hmp->rootvp);*/
195
196 done:
197         /*
198          * Cleanup and return.
199          */
200         if (error)
201                 hammer_free_hmp(mp);
202         return (error);
203 }
204
205 static int
206 hammer_vfs_unmount(struct mount *mp, int mntflags)
207 {
208 #if 0
209         struct hammer_mount *hmp = (void *)mp->mnt_data;
210 #endif
211         int flags;
212         int error;
213
214         /*
215          * Clean out the vnodes
216          */
217         flags = 0;
218         if (mntflags & MNT_FORCE)
219                 flags |= FORCECLOSE;
220         if ((error = vflush(mp, 0, flags)) != 0)
221                 return (error);
222
223         /*
224          * Clean up the internal mount structure and related entities.  This
225          * may issue I/O.
226          */
227         hammer_free_hmp(mp);
228         return(0);
229 }
230
231 /*
232  * Clean up the internal mount structure and disassociate it from the mount.
233  * This may issue I/O.
234  */
235 static void
236 hammer_free_hmp(struct mount *mp)
237 {
238         struct hammer_mount *hmp = (void *)mp->mnt_data;
239
240 #if 0
241         /*
242          * Clean up the root vnode
243          */
244         if (hmp->rootvp) {
245                 vrele(hmp->rootvp);
246                 hmp->rootvp = NULL;
247         }
248 #endif
249
250         /*
251          * Unload & flush inodes
252          */
253         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
254                 hammer_unload_inode, NULL);
255
256         /*
257          * Unload & flush volumes
258          */
259         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
260                 hammer_unload_volume, NULL);
261
262         mp->mnt_data = NULL;
263         mp->mnt_flag &= ~MNT_LOCAL;
264         hmp->mp = NULL;
265         kfree(hmp->zbuf, M_HAMMER);
266         kfree(hmp, M_HAMMER);
267 }
268
269 /*
270  * Return the root vnode for the filesystem.
271  *
272  * HAMMER stores the root vnode in the hammer_mount structure so
273  * getting it is easy.
274  */
275 static int
276 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
277 {
278         struct hammer_mount *hmp = (void *)mp->mnt_data;
279         int error;
280
281         if (hmp->rootcl == NULL)
282                 error = EIO;
283         else
284                 error = hammer_vfs_vget(mp, 1, vpp);
285         return (error);
286 #if 0
287         /* FUTURE - cached root vnode */
288         if ((vp = hmp->rootvp) != NULL) {
289                 vref(vp);
290                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
291                 *vpp = vp;
292                 return (0);
293         } else {
294                 *vpp = NULL;
295                 return (EIO);
296         }
297 #endif
298 }
299
300 static int
301 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
302 {
303         *sbp = mp->mnt_stat;
304         return(0);
305 }
306
307 static int
308 hammer_vfs_sync(struct mount *mp, int waitfor)
309 {
310         return(0);
311 }
312