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