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