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