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