80e983a95aa3b5a5f728ccfa72bdc58f48799865
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
1 /*-
2  * Copyright (c) 2011, 2012 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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
41 #include <sys/buf.h>
42 #include <sys/uuid.h>
43 #include <sys/vfsops.h>
44 #include <sys/sysctl.h>
45
46 #include "hammer2.h"
47 #include "hammer2_disk.h"
48 #include "hammer2_mount.h"
49
50 struct hammer2_sync_info {
51         int error;
52         int waitfor;
53 };
54
55 int hammer2_debug;
56 int hammer2_cluster_enable = 1;
57 long hammer2_iod_file_read;
58 long hammer2_iod_meta_read;
59 long hammer2_iod_indr_read;
60 long hammer2_iod_file_write;
61 long hammer2_iod_meta_write;
62 long hammer2_iod_indr_write;
63 long hammer2_iod_volu_write;
64 long hammer2_ioa_file_read;
65 long hammer2_ioa_meta_read;
66 long hammer2_ioa_indr_read;
67 long hammer2_ioa_file_write;
68 long hammer2_ioa_meta_write;
69 long hammer2_ioa_indr_write;
70 long hammer2_ioa_volu_write;
71
72 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
73
74 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
75            &hammer2_debug, 0, "");
76 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
77            &hammer2_cluster_enable, 0, "");
78 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
79            &hammer2_iod_file_read, 0, "");
80 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
81            &hammer2_iod_meta_read, 0, "");
82 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
83            &hammer2_iod_indr_read, 0, "");
84 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
85            &hammer2_iod_file_write, 0, "");
86 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
87            &hammer2_iod_meta_write, 0, "");
88 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
89            &hammer2_iod_indr_write, 0, "");
90 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
91            &hammer2_iod_volu_write, 0, "");
92 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
93            &hammer2_ioa_file_read, 0, "");
94 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
95            &hammer2_ioa_meta_read, 0, "");
96 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
97            &hammer2_ioa_indr_read, 0, "");
98 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
99            &hammer2_ioa_file_write, 0, "");
100 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
101            &hammer2_ioa_meta_write, 0, "");
102 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
103            &hammer2_ioa_indr_write, 0, "");
104 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
105            &hammer2_ioa_volu_write, 0, "");
106
107 static int hammer2_vfs_init(struct vfsconf *conf);
108 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
109                                 struct ucred *cred);
110 static int hammer2_remount(struct mount *, char *, struct vnode *,
111                                 struct ucred *);
112 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
113 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
114 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
115                                 struct ucred *cred);
116 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
117                                 struct ucred *cred);
118 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
119 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
120                                 ino_t ino, struct vnode **vpp);
121 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
122                                 struct fid *fhp, struct vnode **vpp);
123 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
124 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
125                                 int *exflagsp, struct ucred **credanonp);
126
127 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
128 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
129 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
130
131 /*
132  * HAMMER2 vfs operations.
133  */
134 static struct vfsops hammer2_vfsops = {
135         .vfs_init       = hammer2_vfs_init,
136         .vfs_sync       = hammer2_vfs_sync,
137         .vfs_mount      = hammer2_vfs_mount,
138         .vfs_unmount    = hammer2_vfs_unmount,
139         .vfs_root       = hammer2_vfs_root,
140         .vfs_statfs     = hammer2_vfs_statfs,
141         .vfs_statvfs    = hammer2_vfs_statvfs,
142         .vfs_vget       = hammer2_vfs_vget,
143         .vfs_vptofh     = hammer2_vfs_vptofh,
144         .vfs_fhtovp     = hammer2_vfs_fhtovp,
145         .vfs_checkexp   = hammer2_vfs_checkexp
146 };
147
148 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
149
150 VFS_SET(hammer2_vfsops, hammer2, 0);
151 MODULE_VERSION(hammer2, 1);
152
153 static
154 int
155 hammer2_vfs_init(struct vfsconf *conf)
156 {
157         int error;
158
159         error = 0;
160
161         if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
162                 error = EINVAL;
163         if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
164                 error = EINVAL;
165         if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
166                 error = EINVAL;
167         if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
168                 error = EINVAL;
169
170         if (error)
171                 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
172
173         return (error);
174 }
175
176 /*
177  * Mount or remount HAMMER2 fileystem from physical media
178  *
179  *      mountroot
180  *              mp              mount point structure
181  *              path            NULL
182  *              data            <unused>
183  *              cred            <unused>
184  *
185  *      mount
186  *              mp              mount point structure
187  *              path            path to mount point
188  *              data            pointer to argument structure in user space
189  *                      volume  volume path (device@LABEL form)
190  *                      hflags  user mount flags
191  *              cred            user credentials
192  *
193  * RETURNS:     0       Success
194  *              !0      error number
195  */
196 static
197 int
198 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
199               struct ucred *cred)
200 {
201         struct hammer2_mount_info info;
202         hammer2_mount_t *hmp;
203         hammer2_key_t lhc;
204         struct vnode *devvp;
205         struct nlookupdata nd;
206         hammer2_chain_t *parent;
207         hammer2_chain_t *schain;
208         hammer2_chain_t *rchain;
209         char devstr[MNAMELEN];
210         size_t size;
211         size_t done;
212         char *dev;
213         char *label;
214         int ronly = 1;
215         int error;
216
217         hmp = NULL;
218         dev = NULL;
219         label = NULL;
220         devvp = NULL;
221
222         kprintf("hammer2_mount\n");
223
224         if (path == NULL) {
225                 /*
226                  * Root mount
227                  */
228                 return (EOPNOTSUPP);
229         } else {
230                 /*
231                  * Non-root mount or updating a mount
232                  */
233                 error = copyin(data, &info, sizeof(info));
234                 if (error)
235                         return (error);
236
237                 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
238                 if (error)
239                         return (error);
240
241                 /* Extract device and label */
242                 dev = devstr;
243                 label = strchr(devstr, '@');
244                 if (label == NULL ||
245                     ((label + 1) - dev) > done) {
246                         return (EINVAL);
247                 }
248                 *label = '\0';
249                 label++;
250                 if (*label == '\0')
251                         return (EINVAL);
252
253                 if (mp->mnt_flag & MNT_UPDATE) {
254                         /* Update mount */
255                         /* HAMMER2 implements NFS export via mountctl */
256                         hmp = MPTOH2(mp);
257                         devvp = hmp->devvp;
258                         error = hammer2_remount(mp, path, devvp, cred);
259                         return error;
260                 }
261         }
262
263         /*
264          * New non-root mount
265          */
266         /* Lookup name and verify it refers to a block device */
267         error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
268         if (error == 0)
269                 error = nlookup(&nd);
270         if (error == 0)
271                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
272         nlookup_done(&nd);
273
274         if (error == 0) {
275                 if (vn_isdisk(devvp, &error))
276                         error = vfs_mountedon(devvp);
277         }
278         if (error == 0 && vcount(devvp) > 0)
279                 error = EBUSY;
280
281         /*
282          * Now open the device
283          */
284         if (error == 0) {
285                 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
286                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
287                 error = vinvalbuf(devvp, V_SAVE, 0, 0);
288                 if (error == 0) {
289                         error = VOP_OPEN(devvp, ronly ? FREAD : FREAD | FWRITE,
290                                          FSCRED, NULL);
291                 }
292                 vn_unlock(devvp);
293         }
294         if (error && devvp) {
295                 vrele(devvp);
296                 devvp = NULL;
297         }
298         if (error)
299                 return error;
300
301         /*
302          * Block device opened successfully, finish initializing the
303          * mount structure.
304          *
305          * From this point on we have to call hammer2_unmount() on failure.
306          */
307         hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
308         mp->mnt_data = (qaddr_t)hmp;
309         hmp->mp = mp;
310         hmp->ronly = ronly;
311         hmp->devvp = devvp;
312         kmalloc_create(&hmp->minode, "HAMMER2-inodes");
313         kmalloc_create(&hmp->mchain, "HAMMER2-chains");
314         
315         mp->mnt_flag = MNT_LOCAL;
316         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
317
318         /*
319          * vchain setup. vchain.data is special cased to NULL.  vchain.refs
320          * is initialized and will never drop to 0.
321          */
322         hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
323         hmp->vchain.refs = 1;
324         hmp->vchain.data = (void *)&hmp->voldata;
325         hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
326         /* hmp->vchain.u.xxx is left NULL */
327         lockinit(&hmp->vchain.lk, "volume", 0, LK_CANRECURSE);
328         lockinit(&hmp->alloclk, "h2alloc", 0, 0);
329
330         /*
331          * Install the volume header
332          */
333         error = hammer2_install_volume_header(hmp);
334         if (error) {
335                 hammer2_vfs_unmount(mp, MNT_FORCE);
336                 return error;
337         }
338
339         /*
340          * required mount structure initializations
341          */
342         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
343         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
344
345         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
346         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
347
348         /*
349          * First locate the super-root inode, which is key 0 relative to the
350          * volume header's blockset.
351          *
352          * Then locate the root inode by scanning the directory keyspace
353          * represented by the label.
354          */
355         lhc = hammer2_dirhash(label, strlen(label));
356         parent = &hmp->vchain;
357         hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
358         schain = hammer2_chain_lookup(hmp, &parent,
359                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
360         hammer2_chain_unlock(hmp, parent);
361         if (schain == NULL) {
362                 kprintf("hammer2_mount: invalid super-root\n");
363                 hammer2_vfs_unmount(mp, MNT_FORCE);
364                 return EINVAL;
365         }
366
367         hammer2_chain_ref(hmp, schain); /* for hmp->schain */
368         parent = schain;
369         rchain = hammer2_chain_lookup(hmp, &parent,
370                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
371                                       0);
372         while (rchain) {
373                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
374                     rchain->u.ip &&
375                     strcmp(label, rchain->data->ipdata.filename) == 0) {
376                         break;
377                 }
378                 rchain = hammer2_chain_next(hmp, &parent, rchain,
379                                             lhc, lhc + HAMMER2_DIRHASH_LOMASK,
380                                             0);
381         }
382         hammer2_chain_unlock(hmp, parent);
383         if (rchain == NULL) {
384                 kprintf("hammer2_mount: root label not found\n");
385                 hammer2_chain_drop(hmp, schain);
386                 hammer2_vfs_unmount(mp, MNT_FORCE);
387                 return EINVAL;
388         }
389         hammer2_chain_ref(hmp, rchain); /* for hmp->rchain */
390         hammer2_chain_unlock(hmp, rchain);
391
392         hmp->schain = schain;           /* left held & unlocked */
393         hmp->rchain = rchain;           /* left held & unlocked */
394         hmp->iroot = rchain->u.ip;      /* implied hold from rchain */
395         kprintf("iroot %p\n", rchain->u.ip);
396
397         vfs_getnewfsid(mp);
398         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
399         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
400         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
401
402         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
403         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
404         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
405         copyinstr(path, mp->mnt_stat.f_mntonname,
406                   sizeof(mp->mnt_stat.f_mntonname) - 1,
407                   &size);
408
409         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
410
411         return 0;
412 }
413
414 static
415 int
416 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
417                 struct ucred *cred)
418 {
419         return (0);
420 }
421
422 static
423 int
424 hammer2_vfs_unmount(struct mount *mp, int mntflags)
425 {
426         hammer2_mount_t *hmp;
427         int flags;
428         int error = 0;
429         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
430         struct vnode *devvp;
431
432         kprintf("hammer2_unmount\n");
433
434         hmp = MPTOH2(mp);
435         flags = 0;
436
437         if (mntflags & MNT_FORCE)
438                 flags |= FORCECLOSE;
439
440         hammer2_mount_exlock(hmp);
441
442         /*
443          * If mount initialization proceeded far enough we must flush
444          * its vnodes.
445          */
446         if (hmp->iroot)
447                 error = vflush(mp, 0, flags);
448
449         if (error)
450                 return error;
451
452         /*
453          * Flush any left over chains
454          */
455         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED1 |
456                                  HAMMER2_CHAIN_SUBMODIFIED)) {
457                 hammer2_vfs_sync(mp, MNT_WAIT);
458         }
459         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED1 |
460                                  HAMMER2_CHAIN_SUBMODIFIED)) {
461                 kprintf("hammer2_unmount: chains left over after final sync\n");
462                 if (hammer2_debug & 0x0010)
463                         Debugger("entered debugger");
464         }
465
466         /*
467          * Cleanup the root and super-root chain elements (which should be
468          * clean).
469          */
470         hmp->iroot = NULL;
471         if (hmp->rchain) {
472                 KKASSERT(hmp->rchain->refs == 1);
473                 hammer2_chain_drop(hmp, hmp->rchain);
474                 hmp->rchain = NULL;
475         }
476         if (hmp->schain) {
477                 KKASSERT(hmp->schain->refs == 1);
478                 hammer2_chain_drop(hmp, hmp->schain);
479                 hmp->schain = NULL;
480         }
481
482         /*
483          * Finish up with the device vnode
484          */
485         if ((devvp = hmp->devvp) != NULL) {
486                 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
487                 hmp->devvp = NULL;
488                 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
489                 vrele(devvp);
490                 devvp = NULL;
491         }
492
493         kmalloc_destroy(&hmp->minode);
494         kmalloc_destroy(&hmp->mchain);
495
496         hammer2_mount_unlock(hmp);
497
498         mp->mnt_data = NULL;
499         hmp->mp = NULL;
500         kfree(hmp, M_HAMMER2);
501
502         return (error);
503 }
504
505 static
506 int
507 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
508              ino_t ino, struct vnode **vpp)
509 {
510         kprintf("hammer2_vget\n");
511         return (EOPNOTSUPP);
512 }
513
514 static
515 int
516 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
517 {
518         hammer2_mount_t *hmp;
519         int error;
520         struct vnode *vp;
521
522         hmp = MPTOH2(mp);
523         hammer2_mount_exlock(hmp);
524         if (hmp->iroot == NULL) {
525                 *vpp = NULL;
526                 error = EINVAL;
527         } else {
528                 vp = hammer2_igetv(hmp->iroot, &error);
529                 *vpp = vp;
530                 if (vp == NULL)
531                         kprintf("vnodefail\n");
532         }
533         hammer2_mount_unlock(hmp);
534
535         return (error);
536 }
537
538 static
539 int
540 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
541 {
542         hammer2_mount_t *hmp;
543
544         hmp = MPTOH2(mp);
545
546         mp->mnt_stat.f_files = 10;
547         mp->mnt_stat.f_bfree = 10;
548         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
549
550         *sbp = mp->mnt_stat;
551         return (0);
552 }
553
554 static
555 int
556 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
557 {
558         hammer2_mount_t *hmp;
559
560         hmp = MPTOH2(mp);
561
562         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
563         mp->mnt_vstat.f_files = 0;
564         mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
565
566         *sbp = mp->mnt_vstat;
567         return (0);
568 }
569
570 /*
571  * Sync the entire filesystem; this is called from the filesystem syncer
572  * process periodically and whenever a user calls sync(1) on the hammer
573  * mountpoint.
574  *
575  * Currently is actually called from the syncer! \o/
576  *
577  * This task will have to snapshot the state of the dirty inode chain.
578  * From that, it will have to make sure all of the inodes on the dirty
579  * chain have IO initiated. We make sure that io is initiated for the root
580  * block.
581  *
582  * If waitfor is set, we wait for media to acknowledge the new rootblock.
583  *
584  * THINKS: side A vs side B, to have sync not stall all I/O?
585  */
586 static
587 int
588 hammer2_vfs_sync(struct mount *mp, int waitfor)
589 {
590         struct hammer2_sync_info info;
591         hammer2_mount_t *hmp;
592         int flags;
593         int error;
594         int haswork;
595
596         hmp = MPTOH2(mp);
597
598         flags = VMSC_GETVP;
599         if (waitfor & MNT_LAZY)
600                 flags |= VMSC_ONEPASS;
601
602         info.error = 0;
603         info.waitfor = MNT_NOWAIT;
604         vmntvnodescan(mp, flags | VMSC_NOWAIT,
605                       hammer2_sync_scan1,
606                       hammer2_sync_scan2, &info);
607         if (info.error == 0 && (waitfor & MNT_WAIT)) {
608                 info.waitfor = waitfor;
609                     vmntvnodescan(mp, flags,
610                                   hammer2_sync_scan1,
611                                   hammer2_sync_scan2, &info);
612
613         }
614 #if 0
615         if (waitfor == MNT_WAIT) {
616                 /* XXX */
617         } else {
618                 /* XXX */
619         }
620 #endif
621         hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
622         if (hmp->vchain.flags &
623             (HAMMER2_CHAIN_MODIFIED1 | HAMMER2_CHAIN_SUBMODIFIED)) {
624                 hammer2_chain_flush(hmp, &hmp->vchain);
625                 haswork = 1;
626         } else {
627                 haswork = 0;
628         }
629         hammer2_chain_unlock(hmp, &hmp->vchain);
630
631         error = 0;
632
633         if ((waitfor & MNT_LAZY) == 0) {
634                 waitfor = MNT_NOWAIT;
635                 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
636                 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
637                 vn_unlock(hmp->devvp);
638         }
639
640         if (error == 0 && haswork) {
641                 struct buf *bp;
642
643                 kprintf("synchronize disk\n");
644
645                 bp = getpbuf(NULL);
646                 bp->b_bio1.bio_offset = 0;
647                 bp->b_bufsize = 0;
648                 bp->b_bcount = 0;
649                 bp->b_cmd = BUF_CMD_FLUSH;
650                 bp->b_bio1.bio_done = biodone_sync;
651                 bp->b_bio1.bio_flags |= BIO_SYNC;
652                 vn_strategy(hmp->devvp, &bp->b_bio1);
653                 biowait(&bp->b_bio1, "h2vol");
654                 relpbuf(bp, NULL);
655
656                 kprintf("flush volume header\n");
657
658                 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
659                 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
660                 bawrite(bp);
661         }
662         return (error);
663 }
664
665 /*
666  * Sync passes.
667  *
668  * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
669  *       won't flush on those flags.  The syncer code above will do a
670  *       general meta-data flush globally that will catch these flags.
671  */
672 static int
673 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
674 {
675         hammer2_inode_t *ip;
676
677         ip = VTOI(vp);
678         if (vp->v_type == VNON || ip == NULL ||
679             ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED1 |
680                                  HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
681              RB_EMPTY(&vp->v_rbdirty_tree))) {
682                 return(-1);
683         }
684         return(0);
685 }
686
687 static int
688 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
689 {
690         struct hammer2_sync_info *info = data;
691         hammer2_inode_t *ip;
692         int error;
693
694         ip = VTOI(vp);
695         if (vp->v_type == VNON || vp->v_type == VBAD ||
696             ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED1 |
697                                  HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
698             RB_EMPTY(&vp->v_rbdirty_tree))) {
699                 return(0);
700         }
701         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
702         if (error)
703                 info->error = error;
704         return(0);
705 }
706
707 static
708 int
709 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
710 {
711         return (0);
712 }
713
714 static
715 int
716 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
717                struct fid *fhp, struct vnode **vpp)
718 {
719         return (0);
720 }
721
722 static
723 int
724 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
725                  int *exflagsp, struct ucred **credanonp)
726 {
727         return (0);
728 }
729
730 /*
731  * Support code for hammer2_mount().  Read, verify, and install the volume
732  * header into the HMP
733  *
734  * XXX read four volhdrs and use the one with the highest TID whos CRC
735  *     matches.
736  *
737  * XXX check iCRCs.
738  *
739  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
740  *     nonexistant locations.
741  *
742  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
743  */
744 static
745 int
746 hammer2_install_volume_header(hammer2_mount_t *hmp)
747 {
748         hammer2_volume_data_t *vd;
749         struct buf *bp;
750         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
751         int error_reported;
752         int error;
753         int valid;
754         int i;
755
756         error_reported = 0;
757         error = 0;
758         valid = 0;
759         bp = NULL;
760
761         /*
762          * There are up to 4 copies of the volume header (syncs iterate
763          * between them so there is no single master).  We don't trust the
764          * volu_size field so we don't know precisely how large the filesystem
765          * is, so depend on the OS to return an error if we go beyond the
766          * block device's EOF.
767          */
768         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
769                 error = bread(hmp->devvp, i * HAMMER2_RESERVE_BYTES64, 
770                               HAMMER2_VOLUME_BYTES, &bp);
771                 if (error) {
772                         brelse(bp);
773                         bp = NULL;
774                         continue;
775                 }
776
777                 vd = (struct hammer2_volume_data *) bp->b_data;
778                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
779                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
780                         brelse(bp);
781                         bp = NULL;
782                         continue;
783                 }
784
785                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
786                         /* XXX: Reversed-endianness filesystem */
787                         kprintf("hammer2: reverse-endian filesystem detected");
788                         brelse(bp);
789                         bp = NULL;
790                         continue;
791                 }
792
793                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
794                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
795                                       HAMMER2_VOLUME_ICRC0_SIZE);
796                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
797                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
798                                        HAMMER2_VOLUME_ICRC1_SIZE);
799                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
800                         kprintf("hammer2 volume header crc "
801                                 "mismatch copy #%d\t%08x %08x",
802                                 i, crc0, crc);
803                         error_reported = 1;
804                         brelse(bp);
805                         bp = NULL;
806                         continue;
807                 }
808                 if (valid == 0 || hmp->voldata.last_tid < vd->last_tid) {
809                         valid = 1;
810                         hmp->voldata = *vd;
811                 }
812                 brelse(bp);
813                 bp = NULL;
814         }
815         if (valid) {
816                 error = 0;
817                 if (error_reported)
818                         kprintf("hammer2: a valid volume header was found\n");
819         } else {
820                 error = EINVAL;
821                 kprintf("hammer2: no valid volume headers found!\n");
822         }
823         return (error);
824 }
825