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