HAMMER 33/many: Expand transaction processing, fix bug in B-Tree
[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.23 2008/03/19 20:18:17 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_general;
51 int hammer_debug_btree;
52 int hammer_debug_tid;
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;
61 int64_t hammer_zone_limit;
62
63 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
64 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
65            &hammer_debug_general, 0, "");
66 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
67            &hammer_debug_btree, 0, "");
68 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
69            &hammer_debug_tid, 0, "");
70 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
71            &hammer_debug_recover, 0, "");
72 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
73            &hammer_debug_recover_faults, 0, "");
74 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
75            &hammer_count_inodes, 0, "");
76 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
77            &hammer_count_records, 0, "");
78 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
79            &hammer_count_record_datas, 0, "");
80 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
81            &hammer_count_volumes, 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_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
87            &hammer_zone_limit, 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_vget(struct mount *mp, ino_t ino,
102                                 struct vnode **vpp);
103 static int      hammer_vfs_init(struct vfsconf *conf);
104 static int      hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
105                                 struct vnode **vpp);
106 static int      hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
107 static int      hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
108                                 int *exflagsp, struct ucred **credanonp);
109
110
111 static struct vfsops hammer_vfsops = {
112         .vfs_mount      = hammer_vfs_mount,
113         .vfs_unmount    = hammer_vfs_unmount,
114         .vfs_root       = hammer_vfs_root,
115         .vfs_statfs     = hammer_vfs_statfs,
116         .vfs_sync       = hammer_vfs_sync,
117         .vfs_vget       = hammer_vfs_vget,
118         .vfs_init       = hammer_vfs_init,
119         .vfs_vptofh     = hammer_vfs_vptofh,
120         .vfs_fhtovp     = hammer_vfs_fhtovp,
121         .vfs_checkexp   = hammer_vfs_checkexp
122 };
123
124 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
125
126 VFS_SET(hammer_vfsops, hammer, 0);
127 MODULE_VERSION(hammer, 1);
128
129 static int
130 hammer_vfs_init(struct vfsconf *conf)
131 {
132         /*hammer_init_alist_config();*/
133         return(0);
134 }
135
136 static int
137 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
138                  struct ucred *cred)
139 {
140         struct hammer_mount_info info;
141         hammer_mount_t hmp;
142         hammer_volume_t rootvol;
143         struct vnode *rootvp;
144         const char *upath;      /* volume name in userspace */
145         char *path;             /* volume name in system space */
146         int error;
147         int i;
148
149         if ((error = copyin(data, &info, sizeof(info))) != 0)
150                 return (error);
151         if (info.nvolumes <= 0 || info.nvolumes >= 32768)
152                 return (EINVAL);
153
154         /*
155          * Interal mount data structure
156          */
157         if (mp->mnt_flag & MNT_UPDATE) {
158                 hmp = (void *)mp->mnt_data;
159                 KKASSERT(hmp != NULL);
160         } else {
161                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
162                 mp->mnt_data = (qaddr_t)hmp;
163                 hmp->mp = mp;
164                 hmp->zbuf = kmalloc(HAMMER_BUFSIZE, M_HAMMER, M_WAITOK|M_ZERO);
165                 hmp->namekey_iterator = mycpu->gd_time_seconds;
166                 /*TAILQ_INIT(&hmp->recycle_list);*/
167
168                 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
169                 hmp->root_btree_beg.key = -0x8000000000000000LL;
170                 hmp->root_btree_beg.create_tid = 1;
171                 hmp->root_btree_beg.delete_tid = 1;
172                 hmp->root_btree_beg.rec_type = 0;
173                 hmp->root_btree_beg.obj_type = 0;
174
175                 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
176                 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
177                 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
178                 hmp->root_btree_end.delete_tid = 0;   /* special case */
179                 hmp->root_btree_end.rec_type = 0xFFFFU;
180                 hmp->root_btree_end.obj_type = 0;
181                 lockinit(&hmp->blockmap_lock, "blkmap", 0, 0);
182
183                 for (i = 0; i < HAMMER_MAX_ZONES; ++i) {
184                         hmp->zone_limits[i] =
185                                 HAMMER_ZONE_ENCODE(i, HAMMER_ZONE_LIMIT);
186                         /*
187                          * Sysctl override for debugging (force the zone
188                          * the cycle more quickly then every 2^60 bytes).
189                          */
190                         if (hammer_zone_limit) {
191                                 hmp->zone_limits[i] =
192                                     HAMMER_ZONE_ENCODE(i, hammer_zone_limit);
193                         }
194                         hammer_init_holes(hmp, &hmp->holes[i]);
195                 }
196         }
197         hmp->hflags = info.hflags;
198         if (info.asof) {
199                 mp->mnt_flag |= MNT_RDONLY;
200                 hmp->asof = info.asof;
201         } else {
202                 hmp->asof = HAMMER_MAX_TID;
203         }
204
205         /*
206          * Re-open read-write if originally read-only, or vise-versa XXX
207          */
208         if (mp->mnt_flag & MNT_UPDATE) {
209                 if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
210                         kprintf("HAMMER read-write -> read-only XXX\n");
211                         hmp->ronly = 1;
212                 } else if (hmp->ronly && (mp->mnt_flag & MNT_RDONLY) == 0) {
213                         kprintf("HAMMER read-only -> read-write XXX\n");
214                         hmp->ronly = 0;
215                 }
216                 return(0);
217         }
218
219         RB_INIT(&hmp->rb_vols_root);
220         RB_INIT(&hmp->rb_inos_root);
221         RB_INIT(&hmp->rb_nods_root);
222         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
223
224         /*
225          * Load volumes
226          */
227         path = objcache_get(namei_oc, M_WAITOK);
228         hmp->nvolumes = info.nvolumes;
229         for (i = 0; i < info.nvolumes; ++i) {
230                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
231                 if (error == 0)
232                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
233                 if (error == 0)
234                         error = hammer_install_volume(hmp, path);
235                 if (error)
236                         break;
237         }
238         objcache_put(namei_oc, path);
239
240         /*
241          * Make sure we found a root volume
242          */
243         if (error == 0 && hmp->rootvol == NULL) {
244                 kprintf("hammer_mount: No root volume found!\n");
245                 error = EINVAL;
246         }
247         if (error) {
248                 hammer_free_hmp(mp);
249                 return (error);
250         }
251
252         /*
253          * No errors, setup enough of the mount point so we can lookup the
254          * root vnode.
255          */
256         mp->mnt_iosize_max = MAXPHYS;
257         mp->mnt_kern_flag |= MNTK_FSMID;
258
259         /* 
260          * note: f_iosize is used by vnode_pager_haspage() when constructing
261          * its VOP_BMAP call.
262          */
263         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
264         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
265         mp->mnt_maxsymlinklen = 255;
266         mp->mnt_flag |= MNT_LOCAL;
267
268         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
269         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
270         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
271
272         /*
273          * The root volume's ondisk pointer is only valid if we hold a
274          * reference to it.
275          */
276         rootvol = hammer_get_root_volume(hmp, &error);
277         if (error)
278                 goto done;
279         ksnprintf(mp->mnt_stat.f_mntfromname,
280                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
281                   rootvol->ondisk->vol_name);
282         mp->mnt_stat.f_fsid.val[0] =
283                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
284         mp->mnt_stat.f_fsid.val[1] =
285                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
286         hammer_rel_volume(rootvol, 0);
287
288         /*
289          * Locate the root directory using the root cluster's B-Tree as a
290          * starting point.  The root directory uses an obj_id of 1.
291          *
292          * FUTURE: Leave the root directory cached referenced but unlocked
293          * in hmp->rootvp (need to flush it on unmount).
294          */
295         error = hammer_vfs_vget(mp, 1, &rootvp);
296         if (error)
297                 goto done;
298         vput(rootvp);
299         /*vn_unlock(hmp->rootvp);*/
300
301 done:
302         /*
303          * Cleanup and return.
304          */
305         if (error)
306                 hammer_free_hmp(mp);
307         return (error);
308 }
309
310 static int
311 hammer_vfs_unmount(struct mount *mp, int mntflags)
312 {
313 #if 0
314         struct hammer_mount *hmp = (void *)mp->mnt_data;
315 #endif
316         int flags;
317         int error;
318
319         /*
320          * Clean out the vnodes
321          */
322         flags = 0;
323         if (mntflags & MNT_FORCE)
324                 flags |= FORCECLOSE;
325         if ((error = vflush(mp, 0, flags)) != 0)
326                 return (error);
327
328         /*
329          * Clean up the internal mount structure and related entities.  This
330          * may issue I/O.
331          */
332         hammer_free_hmp(mp);
333         return(0);
334 }
335
336 /*
337  * Clean up the internal mount structure and disassociate it from the mount.
338  * This may issue I/O.
339  */
340 static void
341 hammer_free_hmp(struct mount *mp)
342 {
343         struct hammer_mount *hmp = (void *)mp->mnt_data;
344         int i;
345
346 #if 0
347         /*
348          * Clean up the root vnode
349          */
350         if (hmp->rootvp) {
351                 vrele(hmp->rootvp);
352                 hmp->rootvp = NULL;
353         }
354 #endif
355
356         /*
357          * Unload & flush inodes
358          */
359         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
360                 hammer_unload_inode, (void *)MNT_WAIT);
361
362         /*
363          * Unload & flush volumes
364          */
365         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
366                 hammer_unload_volume, NULL);
367
368         mp->mnt_data = NULL;
369         mp->mnt_flag &= ~MNT_LOCAL;
370         hmp->mp = NULL;
371         kfree(hmp->zbuf, M_HAMMER);
372         lockuninit(&hmp->blockmap_lock);
373
374         for (i = 0; i < HAMMER_MAX_ZONES; ++i)
375                 hammer_free_holes(hmp, &hmp->holes[i]);
376
377         kfree(hmp, M_HAMMER);
378 }
379
380 /*
381  * Obtain a vnode for the specified inode number.  An exclusively locked
382  * vnode is returned.
383  */
384 int
385 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
386 {
387         struct hammer_transaction trans;
388         struct hammer_mount *hmp = (void *)mp->mnt_data;
389         struct hammer_inode *ip;
390         int error;
391
392         hammer_simple_transaction(&trans, hmp);
393
394         /*
395          * Lookup the requested HAMMER inode.  The structure must be
396          * left unlocked while we manipulate the related vnode to avoid
397          * a deadlock.
398          */
399         ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
400         if (ip == NULL) {
401                 *vpp = NULL;
402                 return(error);
403         }
404         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
405         hammer_rel_inode(ip, 0);
406         hammer_commit_transaction(&trans);
407         return (error);
408 }
409
410 /*
411  * Return the root vnode for the filesystem.
412  *
413  * HAMMER stores the root vnode in the hammer_mount structure so
414  * getting it is easy.
415  */
416 static int
417 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
418 {
419 #if 0
420         struct hammer_mount *hmp = (void *)mp->mnt_data;
421 #endif
422         int error;
423
424         error = hammer_vfs_vget(mp, 1, vpp);
425         return (error);
426 }
427
428 static int
429 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
430 {
431         struct hammer_mount *hmp = (void *)mp->mnt_data;
432         hammer_volume_t volume;
433         hammer_volume_ondisk_t ondisk;
434         int error;
435         int64_t bfree;
436
437         volume = hammer_get_root_volume(hmp, &error);
438         if (error)
439                 return(error);
440         ondisk = volume->ondisk;
441
442         /*
443          * Basic stats
444          */
445         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
446         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
447         hammer_rel_volume(volume, 0);
448
449         mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
450         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
451         if (mp->mnt_stat.f_files < 0)
452                 mp->mnt_stat.f_files = 0;
453
454         *sbp = mp->mnt_stat;
455         return(0);
456 }
457
458 static int
459 hammer_vfs_sync(struct mount *mp, int waitfor)
460 {
461         struct hammer_mount *hmp = (void *)mp->mnt_data;
462         int error;
463
464         error = hammer_sync_hmp(hmp, waitfor);
465         return(error);
466 }
467
468 /*
469  * Convert a vnode to a file handle.
470  */
471 static int
472 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
473 {
474         hammer_inode_t ip;
475
476         KKASSERT(MAXFIDSZ >= 16);
477         ip = VTOI(vp);
478         fhp->fid_len = offsetof(struct fid, fid_data[16]);
479         fhp->fid_reserved = 0;
480         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
481         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
482         return(0);
483 }
484
485
486 /*
487  * Convert a file handle back to a vnode.
488  */
489 static int
490 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
491 {
492         struct hammer_transaction trans;
493         struct hammer_inode *ip;
494         struct hammer_inode_info info;
495         int error;
496
497         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
498         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
499
500         hammer_simple_transaction(&trans, (void *)mp->mnt_data);
501
502         /*
503          * Get/allocate the hammer_inode structure.  The structure must be
504          * unlocked while we manipulate the related vnode to avoid a
505          * deadlock.
506          */
507         ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
508                               0, &error);
509         if (ip == NULL) {
510                 *vpp = NULL;
511                 return(error);
512         }
513         error = hammer_get_vnode(ip, LK_EXCLUSIVE, vpp);
514         hammer_rel_inode(ip, 0);
515         hammer_commit_transaction(&trans);
516         return (error);
517 }
518
519 static int
520 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
521                     int *exflagsp, struct ucred **credanonp)
522 {
523         hammer_mount_t hmp = (void *)mp->mnt_data;
524         struct netcred *np;
525         int error;
526
527         np = vfs_export_lookup(mp, &hmp->export, nam);
528         if (np) {
529                 *exflagsp = np->netc_exflags;
530                 *credanonp = &np->netc_anon;
531                 error = 0;
532         } else {
533                 error = EACCES;
534         }
535         return (error);
536
537 }
538
539 int
540 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
541 {
542         hammer_mount_t hmp = (void *)mp->mnt_data;
543         int error;
544
545         switch(op) {
546         case MOUNTCTL_SET_EXPORT:
547                 error = vfs_export(mp, &hmp->export, export);
548                 break;
549         default:
550                 error = EOPNOTSUPP;
551                 break;
552         }
553         return(error);
554 }
555