HAMMER 60A/many: Mirroring work
[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.58 2008/07/02 21:57:54 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_io;
51 int hammer_debug_general;
52 int hammer_debug_debug;
53 int hammer_debug_inode;
54 int hammer_debug_locks;
55 int hammer_debug_btree;
56 int hammer_debug_tid;
57 int hammer_debug_recover;               /* -1 will disable, +1 will force */
58 int hammer_debug_recover_faults;
59 int hammer_debug_cluster_enable = 1;    /* enable read clustering by default */
60 int hammer_count_inodes;
61 int hammer_count_iqueued;
62 int hammer_count_reclaiming;
63 int hammer_count_records;
64 int hammer_count_record_datas;
65 int hammer_count_volumes;
66 int hammer_count_buffers;
67 int hammer_count_nodes;
68 int64_t hammer_stats_btree_lookups;
69 int64_t hammer_stats_btree_searches;
70 int64_t hammer_stats_btree_inserts;
71 int64_t hammer_stats_btree_deletes;
72 int64_t hammer_stats_btree_elements;
73 int64_t hammer_stats_btree_splits;
74 int64_t hammer_stats_btree_iterations;
75 int64_t hammer_stats_record_iterations;
76 int hammer_count_dirtybufspace;         /* global */
77 int hammer_count_refedbufs;             /* global */
78 int hammer_count_reservations;
79 int hammer_count_io_running_read;
80 int hammer_count_io_running_write;
81 int hammer_count_io_locked;
82 int hammer_limit_dirtybufspace;         /* per-mount */
83 int hammer_limit_recs;                  /* as a whole XXX */
84 int hammer_limit_iqueued;               /* per-mount */
85 int hammer_bio_count;
86 int hammer_verify_zone;
87 int hammer_write_mode;
88 int64_t hammer_contention_count;
89 int64_t hammer_zone_limit;
90
91 SYSCTL_NODE(_vfs, OID_AUTO, hammer, CTLFLAG_RW, 0, "HAMMER filesystem");
92 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_general, CTLFLAG_RW,
93            &hammer_debug_general, 0, "");
94 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_io, CTLFLAG_RW,
95            &hammer_debug_io, 0, "");
96 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_debug, CTLFLAG_RW,
97            &hammer_debug_debug, 0, "");
98 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_inode, CTLFLAG_RW,
99            &hammer_debug_inode, 0, "");
100 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_locks, CTLFLAG_RW,
101            &hammer_debug_locks, 0, "");
102 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_btree, CTLFLAG_RW,
103            &hammer_debug_btree, 0, "");
104 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_tid, CTLFLAG_RW,
105            &hammer_debug_tid, 0, "");
106 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover, CTLFLAG_RW,
107            &hammer_debug_recover, 0, "");
108 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_recover_faults, CTLFLAG_RW,
109            &hammer_debug_recover_faults, 0, "");
110 SYSCTL_INT(_vfs_hammer, OID_AUTO, debug_cluster_enable, CTLFLAG_RW,
111            &hammer_debug_cluster_enable, 0, "");
112
113 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_dirtybufspace, CTLFLAG_RW,
114            &hammer_limit_dirtybufspace, 0, "");
115 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_recs, CTLFLAG_RW,
116            &hammer_limit_recs, 0, "");
117 SYSCTL_INT(_vfs_hammer, OID_AUTO, limit_iqueued, CTLFLAG_RW,
118            &hammer_limit_iqueued, 0, "");
119
120 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_inodes, CTLFLAG_RD,
121            &hammer_count_inodes, 0, "");
122 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_iqueued, CTLFLAG_RD,
123            &hammer_count_iqueued, 0, "");
124 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reclaiming, CTLFLAG_RD,
125            &hammer_count_reclaiming, 0, "");
126 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_records, CTLFLAG_RD,
127            &hammer_count_records, 0, "");
128 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_record_datas, CTLFLAG_RD,
129            &hammer_count_record_datas, 0, "");
130 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_volumes, CTLFLAG_RD,
131            &hammer_count_volumes, 0, "");
132 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_buffers, CTLFLAG_RD,
133            &hammer_count_buffers, 0, "");
134 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_nodes, CTLFLAG_RD,
135            &hammer_count_nodes, 0, "");
136 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_searches, CTLFLAG_RD,
137            &hammer_stats_btree_searches, 0, "");
138 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_lookups, CTLFLAG_RD,
139            &hammer_stats_btree_lookups, 0, "");
140 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_inserts, CTLFLAG_RD,
141            &hammer_stats_btree_inserts, 0, "");
142 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_deletes, CTLFLAG_RD,
143            &hammer_stats_btree_deletes, 0, "");
144 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_elements, CTLFLAG_RD,
145            &hammer_stats_btree_elements, 0, "");
146 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_splits, CTLFLAG_RD,
147            &hammer_stats_btree_splits, 0, "");
148 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_btree_iterations, CTLFLAG_RD,
149            &hammer_stats_btree_iterations, 0, "");
150 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, stats_record_iterations, CTLFLAG_RD,
151            &hammer_stats_record_iterations, 0, "");
152 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_dirtybufspace, CTLFLAG_RD,
153            &hammer_count_dirtybufspace, 0, "");
154 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_refedbufs, CTLFLAG_RD,
155            &hammer_count_refedbufs, 0, "");
156 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_reservations, CTLFLAG_RD,
157            &hammer_count_reservations, 0, "");
158 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_read, CTLFLAG_RD,
159            &hammer_count_io_running_read, 0, "");
160 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_locked, CTLFLAG_RD,
161            &hammer_count_io_locked, 0, "");
162 SYSCTL_INT(_vfs_hammer, OID_AUTO, count_io_running_write, CTLFLAG_RD,
163            &hammer_count_io_running_write, 0, "");
164 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, zone_limit, CTLFLAG_RW,
165            &hammer_zone_limit, 0, "");
166 SYSCTL_QUAD(_vfs_hammer, OID_AUTO, contention_count, CTLFLAG_RW,
167            &hammer_contention_count, 0, "");
168 SYSCTL_INT(_vfs_hammer, OID_AUTO, verify_zone, CTLFLAG_RW,
169            &hammer_verify_zone, 0, "");
170 SYSCTL_INT(_vfs_hammer, OID_AUTO, write_mode, CTLFLAG_RW,
171            &hammer_write_mode, 0, "");
172
173 KTR_INFO_MASTER(hammer);
174
175 /*
176  * VFS ABI
177  */
178 static void     hammer_free_hmp(struct mount *mp);
179
180 static int      hammer_vfs_mount(struct mount *mp, char *path, caddr_t data,
181                                 struct ucred *cred);
182 static int      hammer_vfs_unmount(struct mount *mp, int mntflags);
183 static int      hammer_vfs_root(struct mount *mp, struct vnode **vpp);
184 static int      hammer_vfs_statfs(struct mount *mp, struct statfs *sbp,
185                                 struct ucred *cred);
186 static int      hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
187                                 struct ucred *cred);
188 static int      hammer_vfs_sync(struct mount *mp, int waitfor);
189 static int      hammer_vfs_vget(struct mount *mp, ino_t ino,
190                                 struct vnode **vpp);
191 static int      hammer_vfs_init(struct vfsconf *conf);
192 static int      hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp,
193                                 struct vnode **vpp);
194 static int      hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp);
195 static int      hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
196                                 int *exflagsp, struct ucred **credanonp);
197
198
199 static struct vfsops hammer_vfsops = {
200         .vfs_mount      = hammer_vfs_mount,
201         .vfs_unmount    = hammer_vfs_unmount,
202         .vfs_root       = hammer_vfs_root,
203         .vfs_statfs     = hammer_vfs_statfs,
204         .vfs_statvfs    = hammer_vfs_statvfs,
205         .vfs_sync       = hammer_vfs_sync,
206         .vfs_vget       = hammer_vfs_vget,
207         .vfs_init       = hammer_vfs_init,
208         .vfs_vptofh     = hammer_vfs_vptofh,
209         .vfs_fhtovp     = hammer_vfs_fhtovp,
210         .vfs_checkexp   = hammer_vfs_checkexp
211 };
212
213 MALLOC_DEFINE(M_HAMMER, "hammer-mount", "hammer mount");
214
215 VFS_SET(hammer_vfsops, hammer, 0);
216 MODULE_VERSION(hammer, 1);
217
218 static int
219 hammer_vfs_init(struct vfsconf *conf)
220 {
221         int n;
222
223         if (hammer_limit_recs == 0) {
224                 hammer_limit_recs = nbuf * 25;
225                 n = kmalloc_limit(M_HAMMER) / 512;
226                 if (hammer_limit_recs > n)
227                         hammer_limit_recs = n;
228         }
229         if (hammer_limit_dirtybufspace == 0) {
230                 hammer_limit_dirtybufspace = hidirtybufspace / 2;
231                 if (hammer_limit_dirtybufspace < 100)
232                         hammer_limit_dirtybufspace = 100;
233         }
234         if (hammer_limit_iqueued == 0)
235                 hammer_limit_iqueued = desiredvnodes / 5;
236         return(0);
237 }
238
239 static int
240 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
241                  struct ucred *cred)
242 {
243         struct hammer_mount_info info;
244         hammer_mount_t hmp;
245         hammer_volume_t rootvol;
246         struct vnode *rootvp;
247         const char *upath;      /* volume name in userspace */
248         char *path;             /* volume name in system space */
249         int error;
250         int i;
251
252         if ((error = copyin(data, &info, sizeof(info))) != 0)
253                 return (error);
254         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
255                 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
256                         return (EINVAL);
257         }
258         if ((info.hflags & HMNT_MASTERID) &&
259             (info.masterid < -1 || info.masterid >= HAMMER_MAX_MASTERS)) {
260                         return (EINVAL);
261         }
262
263         /*
264          * Interal mount data structure
265          */
266         if (mp->mnt_flag & MNT_UPDATE) {
267                 hmp = (void *)mp->mnt_data;
268                 KKASSERT(hmp != NULL);
269         } else {
270                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
271                 mp->mnt_data = (qaddr_t)hmp;
272                 hmp->mp = mp;
273                 hmp->namekey_iterator = mycpu->gd_time_seconds;
274                 /*TAILQ_INIT(&hmp->recycle_list);*/
275
276                 hmp->root_btree_beg.localization = 0x00000000U;
277                 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
278                 hmp->root_btree_beg.key = -0x8000000000000000LL;
279                 hmp->root_btree_beg.create_tid = 1;
280                 hmp->root_btree_beg.delete_tid = 1;
281                 hmp->root_btree_beg.rec_type = 0;
282                 hmp->root_btree_beg.obj_type = 0;
283
284                 hmp->root_btree_end.localization = 0xFFFFFFFFU;
285                 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
286                 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
287                 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
288                 hmp->root_btree_end.delete_tid = 0;   /* special case */
289                 hmp->root_btree_end.rec_type = 0xFFFFU;
290                 hmp->root_btree_end.obj_type = 0;
291
292                 hmp->sync_lock.refs = 1;
293                 hmp->free_lock.refs = 1;
294                 hmp->undo_lock.refs = 1;
295                 hmp->blkmap_lock.refs = 1;
296
297                 TAILQ_INIT(&hmp->flush_list);
298                 TAILQ_INIT(&hmp->delay_list);
299                 TAILQ_INIT(&hmp->objid_cache_list);
300                 TAILQ_INIT(&hmp->undo_lru_list);
301                 TAILQ_INIT(&hmp->reclaim_list);
302         }
303         hmp->hflags &= ~HMNT_USERFLAGS;
304         hmp->hflags |= info.hflags & HMNT_USERFLAGS;
305         if (info.hflags & HMNT_MASTERID)
306                 hmp->masterid = info.masterid;
307         else
308                 hmp->masterid = -1;
309         if (info.asof) {
310                 kprintf("ASOF\n");
311                 mp->mnt_flag |= MNT_RDONLY;
312                 hmp->asof = info.asof;
313         } else {
314                 hmp->asof = HAMMER_MAX_TID;
315         }
316
317         /*
318          * Re-open read-write if originally read-only, or vise-versa.
319          */
320         if (mp->mnt_flag & MNT_UPDATE) {
321                 error = 0;
322                 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
323                         kprintf("HAMMER read-only -> read-write\n");
324                         hmp->ronly = 0;
325                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
326                                 hammer_adjust_volume_mode, NULL);
327                         rootvol = hammer_get_root_volume(hmp, &error);
328                         if (rootvol) {
329                                 hammer_recover_flush_buffers(hmp, rootvol, 1);
330                                 bcopy(rootvol->ondisk->vol0_blockmap,
331                                       hmp->blockmap,
332                                       sizeof(hmp->blockmap));
333                                 hammer_rel_volume(rootvol, 0);
334                         }
335                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
336                                 hammer_reload_inode, NULL);
337                         /* kernel clears MNT_RDONLY */
338                 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
339                         kprintf("HAMMER read-write -> read-only\n");
340                         hmp->ronly = 1; /* messy */
341                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
342                                 hammer_reload_inode, NULL);
343                         hmp->ronly = 0;
344                         hammer_flusher_sync(hmp);
345                         hammer_flusher_sync(hmp);
346                         hammer_flusher_sync(hmp);
347                         hmp->ronly = 1;
348                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
349                                 hammer_adjust_volume_mode, NULL);
350                 }
351                 return(error);
352         }
353
354         RB_INIT(&hmp->rb_vols_root);
355         RB_INIT(&hmp->rb_inos_root);
356         RB_INIT(&hmp->rb_nods_root);
357         RB_INIT(&hmp->rb_undo_root);
358         RB_INIT(&hmp->rb_resv_root);
359         RB_INIT(&hmp->rb_bufs_root);
360         RB_INIT(&hmp->rb_pfsm_root);
361
362         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
363
364         TAILQ_INIT(&hmp->volu_list);
365         TAILQ_INIT(&hmp->undo_list);
366         TAILQ_INIT(&hmp->data_list);
367         TAILQ_INIT(&hmp->meta_list);
368         TAILQ_INIT(&hmp->lose_list);
369
370         /*
371          * Load volumes
372          */
373         path = objcache_get(namei_oc, M_WAITOK);
374         hmp->nvolumes = info.nvolumes;
375         for (i = 0; i < info.nvolumes; ++i) {
376                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
377                 if (error == 0)
378                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
379                 if (error == 0)
380                         error = hammer_install_volume(hmp, path);
381                 if (error)
382                         break;
383         }
384         objcache_put(namei_oc, path);
385
386         /*
387          * Make sure we found a root volume
388          */
389         if (error == 0 && hmp->rootvol == NULL) {
390                 kprintf("hammer_mount: No root volume found!\n");
391                 error = EINVAL;
392         }
393         if (error) {
394                 hammer_free_hmp(mp);
395                 return (error);
396         }
397
398         /*
399          * No errors, setup enough of the mount point so we can lookup the
400          * root vnode.
401          */
402         mp->mnt_iosize_max = MAXPHYS;
403         mp->mnt_kern_flag |= MNTK_FSMID;
404
405         /* 
406          * note: f_iosize is used by vnode_pager_haspage() when constructing
407          * its VOP_BMAP call.
408          */
409         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
410         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
411
412         mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
413         mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
414
415         mp->mnt_maxsymlinklen = 255;
416         mp->mnt_flag |= MNT_LOCAL;
417
418         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
419         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
420         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
421
422         /*
423          * The root volume's ondisk pointer is only valid if we hold a
424          * reference to it.
425          */
426         rootvol = hammer_get_root_volume(hmp, &error);
427         if (error)
428                 goto failed;
429
430         /*
431          * Perform any necessary UNDO operations.  The recovery code does
432          * call hammer_undo_lookup() so we have to pre-cache the blockmap,
433          * and then re-copy it again after recovery is complete.
434          *
435          * If this is a read-only mount the UNDO information is retained
436          * in memory in the form of dirty buffer cache buffers, and not
437          * written back to the media.
438          */
439         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
440               sizeof(hmp->blockmap));
441
442         error = hammer_recover(hmp, rootvol);
443         if (error) {
444                 kprintf("Failed to recover HAMMER filesystem on mount\n");
445                 goto done;
446         }
447
448         /*
449          * Finish setup now that we have a good root volume
450          */
451         ksnprintf(mp->mnt_stat.f_mntfromname,
452                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
453                   rootvol->ondisk->vol_name);
454         mp->mnt_stat.f_fsid.val[0] =
455                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
456         mp->mnt_stat.f_fsid.val[1] =
457                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
458
459         mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
460         mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
461                                      sizeof(mp->mnt_vstat.f_fsid_uuid));
462
463         /*
464          * Certain often-modified fields in the root volume are cached in
465          * the hammer_mount structure so we do not have to generate lots
466          * of little UNDO structures for them.
467          *
468          * Recopy after recovery.  This also has the side effect of
469          * setting our cached undo FIFO's first_offset, which serves to
470          * placemark the FIFO start for the NEXT flush cycle while the
471          * on-disk first_offset represents the LAST flush cycle.
472          */
473         hmp->next_tid = rootvol->ondisk->vol0_next_tid;
474         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
475               sizeof(hmp->blockmap));
476         hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
477
478         hammer_flusher_create(hmp);
479
480         /*
481          * Locate the root directory using the root cluster's B-Tree as a
482          * starting point.  The root directory uses an obj_id of 1.
483          *
484          * FUTURE: Leave the root directory cached referenced but unlocked
485          * in hmp->rootvp (need to flush it on unmount).
486          */
487         error = hammer_vfs_vget(mp, 1, &rootvp);
488         if (error)
489                 goto done;
490         vput(rootvp);
491         /*vn_unlock(hmp->rootvp);*/
492
493 done:
494         hammer_rel_volume(rootvol, 0);
495 failed:
496         /*
497          * Cleanup and return.
498          */
499         if (error)
500                 hammer_free_hmp(mp);
501         return (error);
502 }
503
504 static int
505 hammer_vfs_unmount(struct mount *mp, int mntflags)
506 {
507 #if 0
508         struct hammer_mount *hmp = (void *)mp->mnt_data;
509 #endif
510         int flags;
511         int error;
512
513         /*
514          * Clean out the vnodes
515          */
516         flags = 0;
517         if (mntflags & MNT_FORCE)
518                 flags |= FORCECLOSE;
519         if ((error = vflush(mp, 0, flags)) != 0)
520                 return (error);
521
522         /*
523          * Clean up the internal mount structure and related entities.  This
524          * may issue I/O.
525          */
526         hammer_free_hmp(mp);
527         return(0);
528 }
529
530 /*
531  * Clean up the internal mount structure and disassociate it from the mount.
532  * This may issue I/O.
533  */
534 static void
535 hammer_free_hmp(struct mount *mp)
536 {
537         struct hammer_mount *hmp = (void *)mp->mnt_data;
538
539 #if 0
540         /*
541          * Clean up the root vnode
542          */
543         if (hmp->rootvp) {
544                 vrele(hmp->rootvp);
545                 hmp->rootvp = NULL;
546         }
547 #endif
548         hammer_flusher_sync(hmp);
549         hammer_flusher_sync(hmp);
550         hammer_flusher_destroy(hmp);
551
552         KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
553
554 #if 0
555         /*
556          * Unload & flush inodes
557          *
558          * XXX illegal to call this from here, it can only be done from
559          * the flusher.
560          */
561         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
562                 hammer_unload_inode, (void *)MNT_WAIT);
563
564         /*
565          * Unload & flush volumes
566          */
567 #endif
568         /*
569          * Unload buffers and then volumes
570          */
571         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
572                 hammer_unload_buffer, NULL);
573         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
574                 hammer_unload_volume, NULL);
575
576         mp->mnt_data = NULL;
577         mp->mnt_flag &= ~MNT_LOCAL;
578         hmp->mp = NULL;
579         hammer_destroy_objid_cache(hmp);
580         kfree(hmp, M_HAMMER);
581 }
582
583 /*
584  * Obtain a vnode for the specified inode number.  An exclusively locked
585  * vnode is returned.
586  */
587 int
588 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
589 {
590         struct hammer_transaction trans;
591         struct hammer_mount *hmp = (void *)mp->mnt_data;
592         struct hammer_inode *ip;
593         int error;
594
595         hammer_simple_transaction(&trans, hmp);
596
597         /*
598          * Lookup the requested HAMMER inode.  The structure must be
599          * left unlocked while we manipulate the related vnode to avoid
600          * a deadlock.
601          */
602         ip = hammer_get_inode(&trans, NULL, ino,
603                               hmp->asof, HAMMER_DEF_LOCALIZATION, 
604                               0, &error);
605         if (ip == NULL) {
606                 *vpp = NULL;
607                 return(error);
608         }
609         error = hammer_get_vnode(ip, vpp);
610         hammer_rel_inode(ip, 0);
611         hammer_done_transaction(&trans);
612         return (error);
613 }
614
615 /*
616  * Return the root vnode for the filesystem.
617  *
618  * HAMMER stores the root vnode in the hammer_mount structure so
619  * getting it is easy.
620  */
621 static int
622 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
623 {
624 #if 0
625         struct hammer_mount *hmp = (void *)mp->mnt_data;
626 #endif
627         int error;
628
629         error = hammer_vfs_vget(mp, 1, vpp);
630         return (error);
631 }
632
633 static int
634 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
635 {
636         struct hammer_mount *hmp = (void *)mp->mnt_data;
637         hammer_volume_t volume;
638         hammer_volume_ondisk_t ondisk;
639         int error;
640         int64_t bfree;
641
642         volume = hammer_get_root_volume(hmp, &error);
643         if (error)
644                 return(error);
645         ondisk = volume->ondisk;
646
647         /*
648          * Basic stats
649          */
650         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
651         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
652         hammer_rel_volume(volume, 0);
653
654         mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
655         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
656         if (mp->mnt_stat.f_files < 0)
657                 mp->mnt_stat.f_files = 0;
658
659         *sbp = mp->mnt_stat;
660         return(0);
661 }
662
663 static int
664 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
665 {
666         struct hammer_mount *hmp = (void *)mp->mnt_data;
667         hammer_volume_t volume;
668         hammer_volume_ondisk_t ondisk;
669         int error;
670         int64_t bfree;
671
672         volume = hammer_get_root_volume(hmp, &error);
673         if (error)
674                 return(error);
675         ondisk = volume->ondisk;
676
677         /*
678          * Basic stats
679          */
680         mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
681         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
682         hammer_rel_volume(volume, 0);
683
684         mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
685         mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
686         if (mp->mnt_vstat.f_files < 0)
687                 mp->mnt_vstat.f_files = 0;
688         *sbp = mp->mnt_vstat;
689         return(0);
690 }
691
692 /*
693  * Sync the filesystem.  Currently we have to run it twice, the second
694  * one will advance the undo start index to the end index, so if a crash
695  * occurs no undos will be run on mount.
696  *
697  * We do not sync the filesystem if we are called from a panic.  If we did
698  * we might end up blowing up a sync that was already in progress.
699  */
700 static int
701 hammer_vfs_sync(struct mount *mp, int waitfor)
702 {
703         struct hammer_mount *hmp = (void *)mp->mnt_data;
704         int error;
705
706         if (panicstr == NULL) {
707                 error = hammer_sync_hmp(hmp, waitfor);
708                 if (error == 0)
709                         error = hammer_sync_hmp(hmp, waitfor);
710         } else {
711                 error = EIO;
712         }
713         return (error);
714 }
715
716 /*
717  * Convert a vnode to a file handle.
718  */
719 static int
720 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
721 {
722         hammer_inode_t ip;
723
724         KKASSERT(MAXFIDSZ >= 16);
725         ip = VTOI(vp);
726         fhp->fid_len = offsetof(struct fid, fid_data[16]);
727         fhp->fid_reserved = 0;
728         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
729         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
730         return(0);
731 }
732
733
734 /*
735  * Convert a file handle back to a vnode.
736  */
737 static int
738 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
739 {
740         struct hammer_transaction trans;
741         struct hammer_inode *ip;
742         struct hammer_inode_info info;
743         int error;
744
745         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
746         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
747
748         hammer_simple_transaction(&trans, (void *)mp->mnt_data);
749
750         /*
751          * Get/allocate the hammer_inode structure.  The structure must be
752          * unlocked while we manipulate the related vnode to avoid a
753          * deadlock.
754          */
755         ip = hammer_get_inode(&trans, NULL, info.obj_id,
756                               info.obj_asof, HAMMER_DEF_LOCALIZATION,
757                               0, &error);
758         if (ip == NULL) {
759                 *vpp = NULL;
760                 return(error);
761         }
762         error = hammer_get_vnode(ip, vpp);
763         hammer_rel_inode(ip, 0);
764         hammer_done_transaction(&trans);
765         return (error);
766 }
767
768 static int
769 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
770                     int *exflagsp, struct ucred **credanonp)
771 {
772         hammer_mount_t hmp = (void *)mp->mnt_data;
773         struct netcred *np;
774         int error;
775
776         np = vfs_export_lookup(mp, &hmp->export, nam);
777         if (np) {
778                 *exflagsp = np->netc_exflags;
779                 *credanonp = &np->netc_anon;
780                 error = 0;
781         } else {
782                 error = EACCES;
783         }
784         return (error);
785
786 }
787
788 int
789 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
790 {
791         hammer_mount_t hmp = (void *)mp->mnt_data;
792         int error;
793
794         switch(op) {
795         case MOUNTCTL_SET_EXPORT:
796                 error = vfs_export(mp, &hmp->export, export);
797                 break;
798         default:
799                 error = EOPNOTSUPP;
800                 break;
801         }
802         return(error);
803 }
804