HAMMER 23/many: Recovery, B-Tree, spike, I/O work.
[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.14 2008/01/24 02:14:45 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/sysctl.h>
46 #include <sys/buf.h>
47 #include <sys/buf2.h>
48 #include "hammer.h"
49
50 int hammer_debug_btree;
51 int hammer_debug_tid;
52 int hammer_debug_recover = -1;  /* temporarily disabled */
53 int hammer_count_inodes;
54 int hammer_count_records;
55 int hammer_count_record_datas;
56 int hammer_count_volumes;
57 int hammer_count_supercls;
58 int hammer_count_clusters;
59 int hammer_count_buffers;
60 int hammer_count_nodes;
61 int hammer_count_spikes;
62
63 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
64 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
65            &hammer_debug_btree, 0, "");
66 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
67            &hammer_debug_tid, 0, "");
68 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
69            &hammer_debug_recover, 0, "");
70 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
71            &hammer_count_inodes, 0, "");
72 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
73            &hammer_count_records, 0, "");
74 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
75            &hammer_count_record_datas, 0, "");
76 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
77            &hammer_count_volumes, 0, "");
78 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_supercls, CTLFLAG_RD,
79            &hammer_count_supercls, 0, "");
80 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_clusters, CTLFLAG_RD,
81            &hammer_count_clusters, 0, "");
82 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
83            &hammer_count_buffers, 0, "");
84 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
85            &hammer_count_nodes, 0, "");
86 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_spikes, CTLFLAG_RD,
87            &hammer_count_spikes, 0, "");
88
89 /*
90  * VFS ABI
91  */
92 static void     hammer_free_hmp(struct mount *mp);
93
94 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
95                                 struct ucred *cred);
96 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
97 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
98 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
99                                 struct ucred *cred);
100 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
101 static int      hammer_vfs_init(struct vfsconf *conf);
102
103 static struct vfsops hammer_vfsops = {
104         .vfs_mount      = hammer_vfs_mount,
105         .vfs_unmount    = hammer_vfs_unmount,
106         .vfs_root       = hammer_vfs_root,
107         .vfs_statfs     = hammer_vfs_statfs,
108         .vfs_sync       = hammer_vfs_sync,
109         .vfs_vget       = hammer_vfs_vget,
110         .vfs_init       = hammer_vfs_init
111 };
112
113 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
114
115 VFS_SET(hammer_vfsops, hammer, 0);
116 MODULE_VERSION(hammer, 1);
117
118 static int
119 hammer_vfs_init(struct vfsconf *conf)
120 {
121         hammer_init_alist_config();
122         return(0);
123 }
124
125 static int
126 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
127                  struct ucred *cred)
128 {
129         struct hammer_mount_info info;
130         hammer_mount_t hmp;
131         hammer_volume_t rootvol;
132         struct vnode *rootvp;
133         const char *upath;      /* volume name in userspace */
134         char *path;             /* volume name in system space */
135         int error;
136         int i;
137
138         if ((error = copyin(data, &info, sizeof(info))) != 0)
139                 return (error);
140         if (info.nvolumes <= 0 || info.nvolumes >= 32768)
141                 return (EINVAL);
142
143         /*
144          * Interal mount data structure
145          */
146         if (mp->mnt_flag & MNT_UPDATE) {
147                 hmp = (void *)mp->mnt_data;
148                 KKASSERT(hmp != NULL);
149         } else {
150                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
151                 mp->mnt_data = (qaddr_t)hmp;
152                 hmp->mp = mp;
153                 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
154                 hmp->namekey_iterator = mycpu->gd_time_seconds;
155         }
156         hmp->hflags = info.hflags;
157         if (info.asof) {
158                 mp->mnt_flag |= MNT_RDONLY;
159                 hmp->asof = info.asof;
160         } else {
161                 hmp->asof = HAMMER_MAX_TID;
162         }
163
164         /*
165          * Re-open read-write if originally read-only, or vise-versa XXX
166          */
167         if (mp->mnt_flag & MNT_UPDATE) {
168                 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
169                         kprintf("HAMMER read-write -> read-only XXX\n");
170                         hmp->ronly = 1;
171                 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
172                         kprintf("HAMMER read-only -> read-write XXX\n");
173                         hmp->ronly = 0;
174                 }
175                 return(0);
176         }
177
178         RB_INIT(&hmp->rb_vols_root);
179         RB_INIT(&hmp->rb_inos_root);
180         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
181
182         /*
183          * Load volumes
184          */
185         path = objcache_get(namei_oc, M_WAITOK);
186         hmp->nvolumes = info.nvolumes;
187         for (i = 0; i < info.nvolumes; ++i) {
188                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
189                 if (error == 0)
190                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
191                 if (error == 0)
192                         error = hammer_install_volume(hmp, path);
193                 if (error)
194                         break;
195         }
196         objcache_put(namei_oc, path);
197
198         /*
199          * Make sure we found a root volume
200          */
201         if (error == 0 && hmp->rootvol == NULL) {
202                 kprintf("hammer_mount: No root volume found!\n");
203                 error = EINVAL;
204         }
205         if (error == 0 && hmp->rootcl == NULL) {
206                 kprintf("hammer_mount: No root cluster found!\n");
207                 error = EINVAL;
208         }
209         if (error) {
210                 hammer_free_hmp(mp);
211                 return (error);
212         }
213
214         /*
215          * No errors, setup enough of the mount point so we can lookup the
216          * root vnode.
217          */
218         mp->mnt_iosize_max = MAXPHYS;
219         mp->mnt_kern_flag |= MNTK_FSMID;
220         mp->mnt_stat.f_fsid.val[0] = 0; /* XXX */
221         mp->mnt_stat.f_fsid.val[1] = 0; /* XXX */
222
223         /* 
224          * note: f_iosize is used by vnode_pager_haspage() when constructing
225          * its VOP_BMAP call.
226          */
227         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
228         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
229         vfs_getnewfsid(mp);             /* XXX */
230         mp->mnt_maxsymlinklen = 255;
231         mp->mnt_flag |= MNT_LOCAL;
232
233         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
234         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
235         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
236
237         /*
238          * The root volume's ondisk pointer is only valid if we hold a
239          * reference to it.
240          */
241         rootvol = hammer_get_root_volume(hmp, &error);
242         if (error)
243                 goto done;
244         ksnprintf(mp->mnt_stat.f_mntfromname,
245                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
246                   rootvol->ondisk->vol_name);
247         hammer_rel_volume(rootvol, 0);
248
249         /*
250          * Locate the root directory using the root cluster's B-Tree as a
251          * starting point.  The root directory uses an obj_id of 1.
252          *
253          * FUTURE: Leave the root directory cached referenced but unlocked
254          * in hmp->rootvp (need to flush it on unmount).
255          */
256         error = hammer_vfs_vget(mp, 1, &rootvp);
257         if (error)
258                 goto done;
259         vput(rootvp);
260         /*vn_unlock(hmp->rootvp);*/
261
262 done:
263         /*
264          * Cleanup and return.
265          */
266         if (error)
267                 hammer_free_hmp(mp);
268         return (error);
269 }
270
271 static int
272 hammer_vfs_unmount(struct mount *mp, int mntflags)
273 {
274 #if 0
275         struct hammer_mount *hmp = (void *)mp->mnt_data;
276 #endif
277         int flags;
278         int error;
279
280         /*
281          * Clean out the vnodes
282          */
283         flags = 0;
284         if (mntflags & MNT_FORCE)
285                 flags |= FORCECLOSE;
286         if ((error = vflush(mp, 0, flags)) != 0)
287                 return (error);
288
289         /*
290          * Clean up the internal mount structure and related entities.  This
291          * may issue I/O.
292          */
293         hammer_free_hmp(mp);
294         return(0);
295 }
296
297 /*
298  * Clean up the internal mount structure and disassociate it from the mount.
299  * This may issue I/O.
300  */
301 static void
302 hammer_free_hmp(struct mount *mp)
303 {
304         struct hammer_mount *hmp = (void *)mp->mnt_data;
305
306 #if 0
307         /*
308          * Clean up the root vnode
309          */
310         if (hmp->rootvp) {
311                 vrele(hmp->rootvp);
312                 hmp->rootvp = NULL;
313         }
314 #endif
315
316         /*
317          * Unload & flush inodes
318          */
319         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
320                 hammer_unload_inode, (void *)MNT_WAIT);
321
322         /*
323          * Unload & flush volumes
324          */
325         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
326                 hammer_unload_volume, NULL);
327
328         mp->mnt_data = NULL;
329         mp->mnt_flag &= ~MNT_LOCAL;
330         hmp->mp = NULL;
331         kfree(hmp->zbuf, M_HAMMER);
332         kfree(hmp, M_HAMMER);
333 }
334
335 /*
336  * Return the root vnode for the filesystem.
337  *
338  * HAMMER stores the root vnode in the hammer_mount structure so
339  * getting it is easy.
340  */
341 static int
342 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
343 {
344         struct hammer_mount *hmp = (void *)mp->mnt_data;
345         int error;
346
347         if (hmp->rootcl == NULL)
348                 error = EIO;
349         else
350                 error = hammer_vfs_vget(mp, 1, vpp);
351         return (error);
352 #if 0
353         /* FUTURE - cached root vnode */
354         if ((vp = hmp->rootvp) != NULL) {
355                 vref(vp);
356                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
357                 *vpp = vp;
358                 return (0);
359         } else {
360                 *vpp = NULL;
361                 return (EIO);
362         }
363 #endif
364 }
365
366 static int
367 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
368 {
369         struct hammer_mount *hmp = (void *)mp->mnt_data;
370         hammer_volume_t volume;
371         hammer_volume_ondisk_t ondisk;
372         int error;
373
374         volume = hammer_get_root_volume(hmp, &error);
375         if (error)
376                 return(error);
377
378         ondisk = volume->ondisk;
379
380         mp->mnt_stat.f_bfree = mp->mnt_stat.f_blocks -
381                                 ondisk->vol0_stat_idx_bufs -
382                                 ondisk->vol0_stat_rec_bufs -
383                                 ondisk->vol0_stat_data_bufs;
384         if (mp->mnt_stat.f_bfree < 0)
385                 mp->mnt_stat.f_bfree = 0;
386         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
387         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
388         if (mp->mnt_stat.f_files < 0)
389                 mp->mnt_stat.f_files = 0;
390
391         hammer_rel_volume(volume, 0);
392         *sbp = mp->mnt_stat;
393         return(0);
394 }
395
396 static int
397 hammer_vfs_sync(struct mount *mp, int waitfor)
398 {
399         struct hammer_mount *hmp = (void *)mp->mnt_data;
400         int error;
401
402         error = hammer_sync_hmp(hmp, waitfor);
403         return(error);
404 }
405