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