HAMMER Utilities: Sync with 56D
[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.50 2008/06/20 05:38:26 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_dirtybufs;             /* 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_dirtybufs;             /* 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_dirtybufs, CTLFLAG_RW,
114            &hammer_limit_dirtybufs, 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_dirtybufs, CTLFLAG_RD,
153            &hammer_count_dirtybufs, 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         if (hammer_limit_recs == 0)             /* XXX TODO */
222                 hammer_limit_recs = nbuf * 25;
223         if (hammer_limit_dirtybufs == 0) {
224                 hammer_limit_dirtybufs = hidirtybuffers / 2;
225                 if (hammer_limit_dirtybufs < 100)
226                         hammer_limit_dirtybufs = 100;
227         }
228         if (hammer_limit_iqueued == 0)
229                 hammer_limit_iqueued = desiredvnodes / 5;
230         return(0);
231 }
232
233 static int
234 hammer_vfs_mount(struct mount *mp, char *mntpt, caddr_t data,
235                  struct ucred *cred)
236 {
237         struct hammer_mount_info info;
238         hammer_mount_t hmp;
239         hammer_volume_t rootvol;
240         struct vnode *rootvp;
241         const char *upath;      /* volume name in userspace */
242         char *path;             /* volume name in system space */
243         int error;
244         int i;
245
246         if ((error = copyin(data, &info, sizeof(info))) != 0)
247                 return (error);
248         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
249                 if (info.nvolumes <= 0 || info.nvolumes >= 32768)
250                         return (EINVAL);
251         }
252
253         /*
254          * Interal mount data structure
255          */
256         if (mp->mnt_flag & MNT_UPDATE) {
257                 hmp = (void *)mp->mnt_data;
258                 KKASSERT(hmp != NULL);
259         } else {
260                 hmp = kmalloc(sizeof(*hmp), M_HAMMER, M_WAITOK | M_ZERO);
261                 mp->mnt_data = (qaddr_t)hmp;
262                 hmp->mp = mp;
263                 hmp->namekey_iterator = mycpu->gd_time_seconds;
264                 /*TAILQ_INIT(&hmp->recycle_list);*/
265
266                 hmp->root_btree_beg.localization = HAMMER_MIN_LOCALIZATION;
267                 hmp->root_btree_beg.obj_id = -0x8000000000000000LL;
268                 hmp->root_btree_beg.key = -0x8000000000000000LL;
269                 hmp->root_btree_beg.create_tid = 1;
270                 hmp->root_btree_beg.delete_tid = 1;
271                 hmp->root_btree_beg.rec_type = 0;
272                 hmp->root_btree_beg.obj_type = 0;
273
274                 hmp->root_btree_end.localization = HAMMER_MAX_LOCALIZATION;
275                 hmp->root_btree_end.obj_id = 0x7FFFFFFFFFFFFFFFLL;
276                 hmp->root_btree_end.key = 0x7FFFFFFFFFFFFFFFLL;
277                 hmp->root_btree_end.create_tid = 0xFFFFFFFFFFFFFFFFULL;
278                 hmp->root_btree_end.delete_tid = 0;   /* special case */
279                 hmp->root_btree_end.rec_type = 0xFFFFU;
280                 hmp->root_btree_end.obj_type = 0;
281
282                 hmp->sync_lock.refs = 1;
283                 hmp->free_lock.refs = 1;
284                 hmp->undo_lock.refs = 1;
285                 hmp->blkmap_lock.refs = 1;
286
287                 TAILQ_INIT(&hmp->flush_list);
288                 TAILQ_INIT(&hmp->delay_list);
289                 TAILQ_INIT(&hmp->objid_cache_list);
290                 TAILQ_INIT(&hmp->undo_lru_list);
291                 TAILQ_INIT(&hmp->reclaim_list);
292         }
293         hmp->hflags &= ~HMNT_USERFLAGS;
294         hmp->hflags |= info.hflags & HMNT_USERFLAGS;
295         if (info.asof) {
296                 kprintf("ASOF\n");
297                 mp->mnt_flag |= MNT_RDONLY;
298                 hmp->asof = info.asof;
299         } else {
300                 hmp->asof = HAMMER_MAX_TID;
301         }
302
303         /*
304          * Re-open read-write if originally read-only, or vise-versa.
305          */
306         if (mp->mnt_flag & MNT_UPDATE) {
307                 error = 0;
308                 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
309                         kprintf("HAMMER read-only -> read-write\n");
310                         hmp->ronly = 0;
311                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
312                                 hammer_adjust_volume_mode, NULL);
313                         rootvol = hammer_get_root_volume(hmp, &error);
314                         if (rootvol) {
315                                 hammer_recover_flush_buffers(hmp, rootvol);
316                                 bcopy(rootvol->ondisk->vol0_blockmap,
317                                       hmp->blockmap,
318                                       sizeof(hmp->blockmap));
319                                 hammer_rel_volume(rootvol, 0);
320                         }
321                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
322                                 hammer_reload_inode, NULL);
323                         /* kernel clears MNT_RDONLY */
324                 } else if (hmp->ronly == 0 && (mp->mnt_flag & MNT_RDONLY)) {
325                         kprintf("HAMMER read-write -> read-only\n");
326                         hmp->ronly = 1; /* messy */
327                         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
328                                 hammer_reload_inode, NULL);
329                         hmp->ronly = 0;
330                         hammer_flusher_sync(hmp);
331                         hammer_flusher_sync(hmp);
332                         hammer_flusher_sync(hmp);
333                         hmp->ronly = 1;
334                         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
335                                 hammer_adjust_volume_mode, NULL);
336                 }
337                 return(error);
338         }
339
340         RB_INIT(&hmp->rb_vols_root);
341         RB_INIT(&hmp->rb_inos_root);
342         RB_INIT(&hmp->rb_nods_root);
343         RB_INIT(&hmp->rb_undo_root);
344         RB_INIT(&hmp->rb_resv_root);
345         RB_INIT(&hmp->rb_bufs_root);
346
347         hmp->ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
348
349         TAILQ_INIT(&hmp->volu_list);
350         TAILQ_INIT(&hmp->undo_list);
351         TAILQ_INIT(&hmp->data_list);
352         TAILQ_INIT(&hmp->meta_list);
353         TAILQ_INIT(&hmp->lose_list);
354
355         /*
356          * Load volumes
357          */
358         path = objcache_get(namei_oc, M_WAITOK);
359         hmp->nvolumes = info.nvolumes;
360         for (i = 0; i < info.nvolumes; ++i) {
361                 error = copyin(&info.volumes[i], &upath, sizeof(char *));
362                 if (error == 0)
363                         error = copyinstr(upath, path, MAXPATHLEN, NULL);
364                 if (error == 0)
365                         error = hammer_install_volume(hmp, path);
366                 if (error)
367                         break;
368         }
369         objcache_put(namei_oc, path);
370
371         /*
372          * Make sure we found a root volume
373          */
374         if (error == 0 && hmp->rootvol == NULL) {
375                 kprintf("hammer_mount: No root volume found!\n");
376                 error = EINVAL;
377         }
378         if (error) {
379                 hammer_free_hmp(mp);
380                 return (error);
381         }
382
383         /*
384          * No errors, setup enough of the mount point so we can lookup the
385          * root vnode.
386          */
387         mp->mnt_iosize_max = MAXPHYS;
388         mp->mnt_kern_flag |= MNTK_FSMID;
389
390         /* 
391          * note: f_iosize is used by vnode_pager_haspage() when constructing
392          * its VOP_BMAP call.
393          */
394         mp->mnt_stat.f_iosize = HAMMER_BUFSIZE;
395         mp->mnt_stat.f_bsize = HAMMER_BUFSIZE;
396
397         mp->mnt_vstat.f_frsize = HAMMER_BUFSIZE;
398         mp->mnt_vstat.f_bsize = HAMMER_BUFSIZE;
399
400         mp->mnt_maxsymlinklen = 255;
401         mp->mnt_flag |= MNT_LOCAL;
402
403         vfs_add_vnodeops(mp, &hammer_vnode_vops, &mp->mnt_vn_norm_ops);
404         vfs_add_vnodeops(mp, &hammer_spec_vops, &mp->mnt_vn_spec_ops);
405         vfs_add_vnodeops(mp, &hammer_fifo_vops, &mp->mnt_vn_fifo_ops);
406
407         /*
408          * The root volume's ondisk pointer is only valid if we hold a
409          * reference to it.
410          */
411         rootvol = hammer_get_root_volume(hmp, &error);
412         if (error)
413                 goto failed;
414
415         /*
416          * Perform any necessary UNDO operations.  The recovery code does
417          * call hammer_undo_lookup() so we have to pre-cache the blockmap,
418          * and then re-copy it again after recovery is complete.
419          *
420          * If this is a read-only mount the UNDO information is retained
421          * in memory in the form of dirty buffer cache buffers, and not
422          * written back to the media.
423          */
424         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
425               sizeof(hmp->blockmap));
426
427         error = hammer_recover(hmp, rootvol);
428         if (error) {
429                 kprintf("Failed to recover HAMMER filesystem on mount\n");
430                 goto done;
431         }
432
433         /*
434          * Finish setup now that we have a good root volume
435          */
436         ksnprintf(mp->mnt_stat.f_mntfromname,
437                   sizeof(mp->mnt_stat.f_mntfromname), "%s",
438                   rootvol->ondisk->vol_name);
439         mp->mnt_stat.f_fsid.val[0] =
440                 crc32((char *)&rootvol->ondisk->vol_fsid + 0, 8);
441         mp->mnt_stat.f_fsid.val[1] =
442                 crc32((char *)&rootvol->ondisk->vol_fsid + 8, 8);
443
444         mp->mnt_vstat.f_fsid_uuid = rootvol->ondisk->vol_fsid;
445         mp->mnt_vstat.f_fsid = crc32(&mp->mnt_vstat.f_fsid_uuid,
446                                      sizeof(mp->mnt_vstat.f_fsid_uuid));
447
448         /*
449          * Certain often-modified fields in the root volume are cached in
450          * the hammer_mount structure so we do not have to generate lots
451          * of little UNDO structures for them.
452          *
453          * Recopy after recovery.  This also has the side effect of
454          * setting our cached undo FIFO's first_offset, which serves to
455          * placemark the FIFO start for the NEXT flush cycle while the
456          * on-disk first_offset represents the LAST flush cycle.
457          */
458         hmp->next_tid = rootvol->ondisk->vol0_next_tid;
459         bcopy(rootvol->ondisk->vol0_blockmap, hmp->blockmap,
460               sizeof(hmp->blockmap));
461         hmp->copy_stat_freebigblocks = rootvol->ondisk->vol0_stat_freebigblocks;
462
463         hammer_flusher_create(hmp);
464
465         /*
466          * Locate the root directory using the root cluster's B-Tree as a
467          * starting point.  The root directory uses an obj_id of 1.
468          *
469          * FUTURE: Leave the root directory cached referenced but unlocked
470          * in hmp->rootvp (need to flush it on unmount).
471          */
472         error = hammer_vfs_vget(mp, 1, &rootvp);
473         if (error)
474                 goto done;
475         vput(rootvp);
476         /*vn_unlock(hmp->rootvp);*/
477
478 done:
479         hammer_rel_volume(rootvol, 0);
480 failed:
481         /*
482          * Cleanup and return.
483          */
484         if (error)
485                 hammer_free_hmp(mp);
486         return (error);
487 }
488
489 static int
490 hammer_vfs_unmount(struct mount *mp, int mntflags)
491 {
492 #if 0
493         struct hammer_mount *hmp = (void *)mp->mnt_data;
494 #endif
495         int flags;
496         int error;
497
498         /*
499          * Clean out the vnodes
500          */
501         flags = 0;
502         if (mntflags & MNT_FORCE)
503                 flags |= FORCECLOSE;
504         if ((error = vflush(mp, 0, flags)) != 0)
505                 return (error);
506
507         /*
508          * Clean up the internal mount structure and related entities.  This
509          * may issue I/O.
510          */
511         hammer_free_hmp(mp);
512         return(0);
513 }
514
515 /*
516  * Clean up the internal mount structure and disassociate it from the mount.
517  * This may issue I/O.
518  */
519 static void
520 hammer_free_hmp(struct mount *mp)
521 {
522         struct hammer_mount *hmp = (void *)mp->mnt_data;
523
524 #if 0
525         /*
526          * Clean up the root vnode
527          */
528         if (hmp->rootvp) {
529                 vrele(hmp->rootvp);
530                 hmp->rootvp = NULL;
531         }
532 #endif
533         hammer_flusher_sync(hmp);
534         hammer_flusher_sync(hmp);
535         hammer_flusher_destroy(hmp);
536
537         KKASSERT(RB_EMPTY(&hmp->rb_inos_root));
538
539 #if 0
540         /*
541          * Unload & flush inodes
542          *
543          * XXX illegal to call this from here, it can only be done from
544          * the flusher.
545          */
546         RB_SCAN(hammer_ino_rb_tree, &hmp->rb_inos_root, NULL,
547                 hammer_unload_inode, (void *)MNT_WAIT);
548
549         /*
550          * Unload & flush volumes
551          */
552 #endif
553         /*
554          * Unload buffers and then volumes
555          */
556         RB_SCAN(hammer_buf_rb_tree, &hmp->rb_bufs_root, NULL,
557                 hammer_unload_buffer, NULL);
558         RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
559                 hammer_unload_volume, NULL);
560
561         mp->mnt_data = NULL;
562         mp->mnt_flag &= ~MNT_LOCAL;
563         hmp->mp = NULL;
564         hammer_destroy_objid_cache(hmp);
565         kfree(hmp, M_HAMMER);
566 }
567
568 /*
569  * Obtain a vnode for the specified inode number.  An exclusively locked
570  * vnode is returned.
571  */
572 int
573 hammer_vfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
574 {
575         struct hammer_transaction trans;
576         struct hammer_mount *hmp = (void *)mp->mnt_data;
577         struct hammer_inode *ip;
578         int error;
579
580         hammer_simple_transaction(&trans, hmp);
581
582         /*
583          * Lookup the requested HAMMER inode.  The structure must be
584          * left unlocked while we manipulate the related vnode to avoid
585          * a deadlock.
586          */
587         ip = hammer_get_inode(&trans, NULL, ino, hmp->asof, 0, &error);
588         if (ip == NULL) {
589                 *vpp = NULL;
590                 return(error);
591         }
592         error = hammer_get_vnode(ip, vpp);
593         hammer_rel_inode(ip, 0);
594         hammer_done_transaction(&trans);
595         return (error);
596 }
597
598 /*
599  * Return the root vnode for the filesystem.
600  *
601  * HAMMER stores the root vnode in the hammer_mount structure so
602  * getting it is easy.
603  */
604 static int
605 hammer_vfs_root(struct mount *mp, struct vnode **vpp)
606 {
607 #if 0
608         struct hammer_mount *hmp = (void *)mp->mnt_data;
609 #endif
610         int error;
611
612         error = hammer_vfs_vget(mp, 1, vpp);
613         return (error);
614 }
615
616 static int
617 hammer_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
618 {
619         struct hammer_mount *hmp = (void *)mp->mnt_data;
620         hammer_volume_t volume;
621         hammer_volume_ondisk_t ondisk;
622         int error;
623         int64_t bfree;
624
625         volume = hammer_get_root_volume(hmp, &error);
626         if (error)
627                 return(error);
628         ondisk = volume->ondisk;
629
630         /*
631          * Basic stats
632          */
633         mp->mnt_stat.f_files = ondisk->vol0_stat_inodes;
634         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
635         hammer_rel_volume(volume, 0);
636
637         mp->mnt_stat.f_bfree = bfree / HAMMER_BUFSIZE;
638         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
639         if (mp->mnt_stat.f_files < 0)
640                 mp->mnt_stat.f_files = 0;
641
642         *sbp = mp->mnt_stat;
643         return(0);
644 }
645
646 static int
647 hammer_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
648 {
649         struct hammer_mount *hmp = (void *)mp->mnt_data;
650         hammer_volume_t volume;
651         hammer_volume_ondisk_t ondisk;
652         int error;
653         int64_t bfree;
654
655         volume = hammer_get_root_volume(hmp, &error);
656         if (error)
657                 return(error);
658         ondisk = volume->ondisk;
659
660         /*
661          * Basic stats
662          */
663         mp->mnt_vstat.f_files = ondisk->vol0_stat_inodes;
664         bfree = ondisk->vol0_stat_freebigblocks * HAMMER_LARGEBLOCK_SIZE;
665         hammer_rel_volume(volume, 0);
666
667         mp->mnt_vstat.f_bfree = bfree / HAMMER_BUFSIZE;
668         mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
669         if (mp->mnt_vstat.f_files < 0)
670                 mp->mnt_vstat.f_files = 0;
671         *sbp = mp->mnt_vstat;
672         return(0);
673 }
674
675 /*
676  * Sync the filesystem.  Currently we have to run it twice, the second
677  * one will advance the undo start index to the end index, so if a crash
678  * occurs no undos will be run on mount.
679  *
680  * We do not sync the filesystem if we are called from a panic.  If we did
681  * we might end up blowing up a sync that was already in progress.
682  */
683 static int
684 hammer_vfs_sync(struct mount *mp, int waitfor)
685 {
686         struct hammer_mount *hmp = (void *)mp->mnt_data;
687         int error;
688
689         if (panicstr == NULL) {
690                 error = hammer_sync_hmp(hmp, waitfor);
691                 if (error == 0)
692                         error = hammer_sync_hmp(hmp, waitfor);
693         } else {
694                 error = EIO;
695         }
696         return (error);
697 }
698
699 /*
700  * Convert a vnode to a file handle.
701  */
702 static int
703 hammer_vfs_vptofh(struct vnode *vp, struct fid *fhp)
704 {
705         hammer_inode_t ip;
706
707         KKASSERT(MAXFIDSZ >= 16);
708         ip = VTOI(vp);
709         fhp->fid_len = offsetof(struct fid, fid_data[16]);
710         fhp->fid_reserved = 0;
711         bcopy(&ip->obj_id, fhp->fid_data + 0, sizeof(ip->obj_id));
712         bcopy(&ip->obj_asof, fhp->fid_data + 8, sizeof(ip->obj_asof));
713         return(0);
714 }
715
716
717 /*
718  * Convert a file handle back to a vnode.
719  */
720 static int
721 hammer_vfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
722 {
723         struct hammer_transaction trans;
724         struct hammer_inode *ip;
725         struct hammer_inode_info info;
726         int error;
727
728         bcopy(fhp->fid_data + 0, &info.obj_id, sizeof(info.obj_id));
729         bcopy(fhp->fid_data + 8, &info.obj_asof, sizeof(info.obj_asof));
730
731         hammer_simple_transaction(&trans, (void *)mp->mnt_data);
732
733         /*
734          * Get/allocate the hammer_inode structure.  The structure must be
735          * unlocked while we manipulate the related vnode to avoid a
736          * deadlock.
737          */
738         ip = hammer_get_inode(&trans, NULL, info.obj_id, info.obj_asof,
739                               0, &error);
740         if (ip == NULL) {
741                 *vpp = NULL;
742                 return(error);
743         }
744         error = hammer_get_vnode(ip, vpp);
745         hammer_rel_inode(ip, 0);
746         hammer_done_transaction(&trans);
747         return (error);
748 }
749
750 static int
751 hammer_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
752                     int *exflagsp, struct ucred **credanonp)
753 {
754         hammer_mount_t hmp = (void *)mp->mnt_data;
755         struct netcred *np;
756         int error;
757
758         np = vfs_export_lookup(mp, &hmp->export, nam);
759         if (np) {
760                 *exflagsp = np->netc_exflags;
761                 *credanonp = &np->netc_anon;
762                 error = 0;
763         } else {
764                 error = EACCES;
765         }
766         return (error);
767
768 }
769
770 int
771 hammer_vfs_export(struct mount *mp, int op, const struct export_args *export)
772 {
773         hammer_mount_t hmp = (void *)mp->mnt_data;
774         int error;
775
776         switch(op) {
777         case MOUNTCTL_SET_EXPORT:
778                 error = vfs_export(mp, &hmp->export, export);
779                 break;
780         default:
781                 error = EOPNOTSUPP;
782                 break;
783         }
784         return(error);
785 }
786