hammer2 - wire up msg transaction state machine
[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         kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
355         lockinit(&pmp->msglk, "h2msg", 0, 0);
356         TAILQ_INIT(&pmp->msgq);
357         RB_INIT(&pmp->staterd_tree);
358         RB_INIT(&pmp->statewr_tree);
359
360         if (create_hmp) {
361                 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
362                 hmp->ronly = ronly;
363                 hmp->devvp = devvp;
364                 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
365                 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
366                 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
367         }
368         ccms_domain_init(&pmp->ccms_dom);
369         pmp->hmp = hmp;
370         ++hmp->pmp_count;
371         lockmgr(&hammer2_mntlk, LK_RELEASE);
372         kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
373         
374         mp->mnt_flag = MNT_LOCAL;
375         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
376
377         if (create_hmp) {
378                 /*
379                  * vchain setup. vchain.data is special cased to NULL.
380                  * vchain.refs is initialized and will never drop to 0.
381                  */
382                 hmp->vchain.refs = 1;
383                 hmp->vchain.data = (void *)&hmp->voldata;
384                 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
385                 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
386                 hmp->vchain.bref_flush = hmp->vchain.bref;
387                 ccms_cst_init(&hmp->vchain.cst, NULL);
388                 /* hmp->vchain.u.xxx is left NULL */
389                 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
390                 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
391
392                 /*
393                  * Install the volume header
394                  */
395                 error = hammer2_install_volume_header(hmp);
396                 if (error) {
397                         hammer2_vfs_unmount(mp, MNT_FORCE);
398                         return error;
399                 }
400         }
401
402         /*
403          * required mount structure initializations
404          */
405         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
406         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
407
408         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
409         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
410
411         /*
412          * Optional fields
413          */
414         mp->mnt_iosize_max = MAXPHYS;
415
416         /*
417          * First locate the super-root inode, which is key 0 relative to the
418          * volume header's blockset.
419          *
420          * Then locate the root inode by scanning the directory keyspace
421          * represented by the label.
422          */
423         if (create_hmp) {
424                 parent = &hmp->vchain;
425                 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
426                 schain = hammer2_chain_lookup(hmp, &parent,
427                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
428                 hammer2_chain_unlock(hmp, parent);
429                 if (schain == NULL) {
430                         kprintf("hammer2_mount: invalid super-root\n");
431                         hammer2_vfs_unmount(mp, MNT_FORCE);
432                         return EINVAL;
433                 }
434                 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
435                 hmp->schain = schain;           /* left locked */
436         } else {
437                 schain = hmp->schain;
438                 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
439         }
440
441         parent = schain;
442         lhc = hammer2_dirhash(label, strlen(label));
443         rchain = hammer2_chain_lookup(hmp, &parent,
444                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
445                                       0);
446         while (rchain) {
447                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
448                     rchain->u.ip &&
449                     strcmp(label, rchain->data->ipdata.filename) == 0) {
450                         break;
451                 }
452                 rchain = hammer2_chain_next(hmp, &parent, rchain,
453                                             lhc, lhc + HAMMER2_DIRHASH_LOMASK,
454                                             0);
455         }
456         hammer2_chain_unlock(hmp, parent);
457         if (rchain == NULL) {
458                 kprintf("hammer2_mount: PFS label not found\n");
459                 hammer2_vfs_unmount(mp, MNT_FORCE);
460                 return EINVAL;
461         }
462         if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
463                 hammer2_chain_unlock(hmp, rchain);
464                 kprintf("hammer2_mount: PFS label already mounted!\n");
465                 hammer2_vfs_unmount(mp, MNT_FORCE);
466                 return EBUSY;
467         }
468         atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
469
470         hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
471         hammer2_chain_unlock(hmp, rchain);
472         pmp->rchain = rchain;           /* left held & unlocked */
473         pmp->iroot = rchain->u.ip;      /* implied hold from rchain */
474         pmp->iroot->pmp = pmp;
475
476         kprintf("iroot %p\n", pmp->iroot);
477
478         /*
479          * Ref the cluster management messaging descriptor.  The mount
480          * program deals with the other end of the communications pipe.
481          */
482         pmp->msg_fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
483         if (pmp->msg_fp == NULL) {
484                 kprintf("hammer2_mount: bad cluster_fd!\n");
485                 hammer2_vfs_unmount(mp, MNT_FORCE);
486                 return EBADF;
487         }
488         lwkt_create(hammer2_cluster_thread_rd, pmp, &pmp->msgrd_td,
489                     NULL, 0, -1, "hammer2-msgrd");
490         lwkt_create(hammer2_cluster_thread_wr, pmp, &pmp->msgwr_td,
491                     NULL, 0, -1, "hammer2-msgwr");
492
493         /*
494          * Finish setup
495          */
496         vfs_getnewfsid(mp);
497         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
498         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
499         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
500
501         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
502         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
503         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
504         copyinstr(path, mp->mnt_stat.f_mntonname,
505                   sizeof(mp->mnt_stat.f_mntonname) - 1,
506                   &size);
507
508         /*
509          * Initial statfs to prime mnt_stat.
510          */
511         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
512
513         return 0;
514 }
515
516 static
517 int
518 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
519                 struct ucred *cred)
520 {
521         return (0);
522 }
523
524 static
525 int
526 hammer2_vfs_unmount(struct mount *mp, int mntflags)
527 {
528         hammer2_pfsmount_t *pmp;
529         hammer2_mount_t *hmp;
530         int flags;
531         int error = 0;
532         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
533         struct vnode *devvp;
534
535         pmp = MPTOPMP(mp);
536         hmp = pmp->hmp;
537         flags = 0;
538
539         if (mntflags & MNT_FORCE)
540                 flags |= FORCECLOSE;
541
542         hammer2_mount_exlock(hmp);
543
544         /*
545          * If mount initialization proceeded far enough we must flush
546          * its vnodes.
547          */
548         if (pmp->iroot)
549                 error = vflush(mp, 0, flags);
550
551         if (error)
552                 return error;
553
554         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
555         --hmp->pmp_count;
556         kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
557
558         /*
559          * Flush any left over chains.  The voldata lock is only used
560          * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
561          */
562         hammer2_voldata_lock(hmp);
563         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
564                                  HAMMER2_CHAIN_MODIFIED_AUX |
565                                  HAMMER2_CHAIN_SUBMODIFIED)) {
566                 hammer2_voldata_unlock(hmp);
567                 hammer2_vfs_sync(mp, MNT_WAIT);
568         } else {
569                 hammer2_voldata_unlock(hmp);
570         }
571         if (hmp->pmp_count == 0) {
572                 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
573                                          HAMMER2_CHAIN_MODIFIED_AUX |
574                                          HAMMER2_CHAIN_SUBMODIFIED)) {
575                         kprintf("hammer2_unmount: chains left over after "
576                                 "final sync\n");
577                         if (hammer2_debug & 0x0010)
578                                 Debugger("entered debugger");
579                 }
580         }
581
582         /*
583          * Cleanup the root and super-root chain elements (which should be
584          * clean).
585          */
586         pmp->iroot = NULL;
587         if (pmp->rchain) {
588                 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
589                 KKASSERT(pmp->rchain->refs == 1);
590                 hammer2_chain_drop(hmp, pmp->rchain);
591                 pmp->rchain = NULL;
592         }
593         ccms_domain_uninit(&pmp->ccms_dom);
594
595         /*
596          * Ask the cluster controller to go away
597          */
598         atomic_set_int(&pmp->msg_ctl, HAMMER2_CLUSTERCTL_KILL);
599         while (pmp->msgrd_td || pmp->msgwr_td) {
600                 wakeup(&pmp->msg_ctl);
601                 tsleep(pmp, 0, "clstrkl", hz);
602         }
603
604         /*
605          * Drop communications descriptor
606          */
607         if (pmp->msg_fp) {
608                 fdrop(pmp->msg_fp);
609                 pmp->msg_fp = NULL;
610         }
611
612         /*
613          * If no PFS's left drop the master hammer2_mount for the device.
614          */
615         if (hmp->pmp_count == 0) {
616                 if (hmp->schain) {
617                         KKASSERT(hmp->schain->refs == 1);
618                         hammer2_chain_drop(hmp, hmp->schain);
619                         hmp->schain = NULL;
620                 }
621
622                 /*
623                  * Finish up with the device vnode
624                  */
625                 if ((devvp = hmp->devvp) != NULL) {
626                         vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
627                         hmp->devvp = NULL;
628                         VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
629                         vrele(devvp);
630                         devvp = NULL;
631                 }
632         }
633         hammer2_mount_unlock(hmp);
634
635         pmp->mp = NULL;
636         pmp->hmp = NULL;
637         mp->mnt_data = NULL;
638
639         kmalloc_destroy(&pmp->mmsg);
640
641         kfree(pmp, M_HAMMER2);
642         if (hmp->pmp_count == 0) {
643                 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
644                 kmalloc_destroy(&hmp->minode);
645                 kmalloc_destroy(&hmp->mchain);
646                 kfree(hmp, M_HAMMER2);
647         }
648         lockmgr(&hammer2_mntlk, LK_RELEASE);
649         return (error);
650 }
651
652 static
653 int
654 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
655              ino_t ino, struct vnode **vpp)
656 {
657         kprintf("hammer2_vget\n");
658         return (EOPNOTSUPP);
659 }
660
661 static
662 int
663 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
664 {
665         hammer2_pfsmount_t *pmp;
666         hammer2_mount_t *hmp;
667         int error;
668         struct vnode *vp;
669
670         pmp = MPTOPMP(mp);
671         hmp = pmp->hmp;
672         hammer2_mount_exlock(hmp);
673         if (pmp->iroot == NULL) {
674                 *vpp = NULL;
675                 error = EINVAL;
676         } else {
677                 hammer2_chain_lock(hmp, &pmp->iroot->chain,
678                                    HAMMER2_RESOLVE_ALWAYS |
679                                    HAMMER2_RESOLVE_SHARED);
680                 vp = hammer2_igetv(pmp->iroot, &error);
681                 hammer2_chain_unlock(hmp, &pmp->iroot->chain);
682                 *vpp = vp;
683                 if (vp == NULL)
684                         kprintf("vnodefail\n");
685         }
686         hammer2_mount_unlock(hmp);
687
688         return (error);
689 }
690
691 /*
692  * Filesystem status
693  *
694  * XXX incorporate pmp->iroot->ip_data.inode_quota and data_quota
695  */
696 static
697 int
698 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
699 {
700         hammer2_pfsmount_t *pmp;
701         hammer2_mount_t *hmp;
702
703         pmp = MPTOPMP(mp);
704         hmp = MPTOHMP(mp);
705
706         mp->mnt_stat.f_files = pmp->iroot->ip_data.inode_count +
707                                pmp->iroot->delta_icount;
708         mp->mnt_stat.f_ffree = 0;
709         mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
710         mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
711                                 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
712         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
713
714         *sbp = mp->mnt_stat;
715         return (0);
716 }
717
718 static
719 int
720 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
721 {
722         hammer2_pfsmount_t *pmp;
723         hammer2_mount_t *hmp;
724
725         pmp = MPTOPMP(mp);
726         hmp = MPTOHMP(mp);
727
728         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
729         mp->mnt_vstat.f_files = pmp->iroot->ip_data.inode_count +
730                                 pmp->iroot->delta_icount;
731         mp->mnt_vstat.f_ffree = 0;
732         mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
733         mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
734                                  hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
735         mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
736
737         *sbp = mp->mnt_vstat;
738         return (0);
739 }
740
741 /*
742  * Sync the entire filesystem; this is called from the filesystem syncer
743  * process periodically and whenever a user calls sync(1) on the hammer
744  * mountpoint.
745  *
746  * Currently is actually called from the syncer! \o/
747  *
748  * This task will have to snapshot the state of the dirty inode chain.
749  * From that, it will have to make sure all of the inodes on the dirty
750  * chain have IO initiated. We make sure that io is initiated for the root
751  * block.
752  *
753  * If waitfor is set, we wait for media to acknowledge the new rootblock.
754  *
755  * THINKS: side A vs side B, to have sync not stall all I/O?
756  */
757 static
758 int
759 hammer2_vfs_sync(struct mount *mp, int waitfor)
760 {
761         struct hammer2_sync_info info;
762         hammer2_mount_t *hmp;
763         int flags;
764         int error;
765         int haswork;
766
767         hmp = MPTOHMP(mp);
768
769         flags = VMSC_GETVP;
770         if (waitfor & MNT_LAZY)
771                 flags |= VMSC_ONEPASS;
772
773         info.error = 0;
774         info.waitfor = MNT_NOWAIT;
775         vmntvnodescan(mp, flags | VMSC_NOWAIT,
776                       hammer2_sync_scan1,
777                       hammer2_sync_scan2, &info);
778         if (info.error == 0 && (waitfor & MNT_WAIT)) {
779                 info.waitfor = waitfor;
780                     vmntvnodescan(mp, flags,
781                                   hammer2_sync_scan1,
782                                   hammer2_sync_scan2, &info);
783
784         }
785 #if 0
786         if (waitfor == MNT_WAIT) {
787                 /* XXX */
788         } else {
789                 /* XXX */
790         }
791 #endif
792         hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
793         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
794                                  HAMMER2_CHAIN_MODIFIED_AUX |
795                                  HAMMER2_CHAIN_SUBMODIFIED)) {
796                 hammer2_chain_flush(hmp, &hmp->vchain, 0);
797                 haswork = 1;
798         } else {
799                 haswork = 0;
800         }
801         hammer2_chain_unlock(hmp, &hmp->vchain);
802
803         error = 0;
804
805         if ((waitfor & MNT_LAZY) == 0) {
806                 waitfor = MNT_NOWAIT;
807                 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
808                 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
809                 vn_unlock(hmp->devvp);
810         }
811
812         if (error == 0 && haswork) {
813                 struct buf *bp;
814
815                 /*
816                  * Synchronize the disk before flushing the volume
817                  * header.
818                  */
819                 bp = getpbuf(NULL);
820                 bp->b_bio1.bio_offset = 0;
821                 bp->b_bufsize = 0;
822                 bp->b_bcount = 0;
823                 bp->b_cmd = BUF_CMD_FLUSH;
824                 bp->b_bio1.bio_done = biodone_sync;
825                 bp->b_bio1.bio_flags |= BIO_SYNC;
826                 vn_strategy(hmp->devvp, &bp->b_bio1);
827                 biowait(&bp->b_bio1, "h2vol");
828                 relpbuf(bp, NULL);
829
830                 /*
831                  * Then we can safely flush the volume header.  Volume
832                  * data is locked separately to prevent ioctl functions
833                  * from deadlocking due to a configuration issue.
834                  */
835                 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
836                 hammer2_voldata_lock(hmp);
837                 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
838                 hammer2_voldata_unlock(hmp);
839                 bawrite(bp);
840         }
841         return (error);
842 }
843
844 /*
845  * Sync passes.
846  *
847  * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
848  *       won't flush on those flags.  The syncer code above will do a
849  *       general meta-data flush globally that will catch these flags.
850  */
851 static int
852 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
853 {
854         hammer2_inode_t *ip;
855
856         ip = VTOI(vp);
857         if (vp->v_type == VNON || ip == NULL ||
858             ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
859                                  HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
860              RB_EMPTY(&vp->v_rbdirty_tree))) {
861                 return(-1);
862         }
863         return(0);
864 }
865
866 static int
867 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
868 {
869         struct hammer2_sync_info *info = data;
870         hammer2_inode_t *ip;
871         int error;
872
873         ip = VTOI(vp);
874         if (vp->v_type == VNON || vp->v_type == VBAD ||
875             ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
876                                  HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
877             RB_EMPTY(&vp->v_rbdirty_tree))) {
878                 return(0);
879         }
880         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
881         if (error)
882                 info->error = error;
883         return(0);
884 }
885
886 static
887 int
888 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
889 {
890         return (0);
891 }
892
893 static
894 int
895 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
896                struct fid *fhp, struct vnode **vpp)
897 {
898         return (0);
899 }
900
901 static
902 int
903 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
904                  int *exflagsp, struct ucred **credanonp)
905 {
906         return (0);
907 }
908
909 /*
910  * Support code for hammer2_mount().  Read, verify, and install the volume
911  * header into the HMP
912  *
913  * XXX read four volhdrs and use the one with the highest TID whos CRC
914  *     matches.
915  *
916  * XXX check iCRCs.
917  *
918  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
919  *     nonexistant locations.
920  *
921  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
922  */
923 static
924 int
925 hammer2_install_volume_header(hammer2_mount_t *hmp)
926 {
927         hammer2_volume_data_t *vd;
928         struct buf *bp;
929         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
930         int error_reported;
931         int error;
932         int valid;
933         int i;
934
935         error_reported = 0;
936         error = 0;
937         valid = 0;
938         bp = NULL;
939
940         /*
941          * There are up to 4 copies of the volume header (syncs iterate
942          * between them so there is no single master).  We don't trust the
943          * volu_size field so we don't know precisely how large the filesystem
944          * is, so depend on the OS to return an error if we go beyond the
945          * block device's EOF.
946          */
947         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
948                 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
949                               HAMMER2_VOLUME_BYTES, &bp);
950                 if (error) {
951                         brelse(bp);
952                         bp = NULL;
953                         continue;
954                 }
955
956                 vd = (struct hammer2_volume_data *) bp->b_data;
957                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
958                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
959                         brelse(bp);
960                         bp = NULL;
961                         continue;
962                 }
963
964                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
965                         /* XXX: Reversed-endianness filesystem */
966                         kprintf("hammer2: reverse-endian filesystem detected");
967                         brelse(bp);
968                         bp = NULL;
969                         continue;
970                 }
971
972                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
973                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
974                                       HAMMER2_VOLUME_ICRC0_SIZE);
975                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
976                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
977                                        HAMMER2_VOLUME_ICRC1_SIZE);
978                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
979                         kprintf("hammer2 volume header crc "
980                                 "mismatch copy #%d\t%08x %08x",
981                                 i, crc0, crc);
982                         error_reported = 1;
983                         brelse(bp);
984                         bp = NULL;
985                         continue;
986                 }
987                 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
988                         valid = 1;
989                         hmp->voldata = *vd;
990                 }
991                 brelse(bp);
992                 bp = NULL;
993         }
994         if (valid) {
995                 error = 0;
996                 if (error_reported)
997                         kprintf("hammer2: a valid volume header was found\n");
998         } else {
999                 error = EINVAL;
1000                 kprintf("hammer2: no valid volume headers found!\n");
1001         }
1002         return (error);
1003 }
1004
1005 /*
1006  * Cluster controller thread.  Perform messaging functions.  We have one
1007  * thread for the reader and one for the writer.  The writer handles
1008  * shutdown requests (which should break the reader thread).
1009  */
1010 static
1011 void
1012 hammer2_cluster_thread_rd(void *arg)
1013 {
1014         hammer2_pfsmount_t *pmp = arg;
1015         hammer2_msg_hdr_t hdr;
1016         hammer2_msg_t *msg;
1017         hammer2_state_t *state;
1018         size_t hbytes;
1019         int error = 0;
1020
1021         while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0) {
1022                 /*
1023                  * Retrieve the message from the pipe or socket.
1024                  */
1025                 error = fp_read(pmp->msg_fp, &hdr, sizeof(hdr),
1026                                 NULL, 1, UIO_SYSSPACE);
1027                 if (error)
1028                         break;
1029                 if (hdr.magic != HAMMER2_MSGHDR_MAGIC) {
1030                         kprintf("hammer2: msgrd: bad magic: %04x\n",
1031                                 hdr.magic);
1032                         error = EINVAL;
1033                         break;
1034                 }
1035                 hbytes = (hdr.cmd & HAMMER2_MSGF_SIZE) * HAMMER2_MSG_ALIGN;
1036                 if (hbytes < sizeof(hdr) || hbytes > HAMMER2_MSGAUX_MAX) {
1037                         kprintf("hammer2: msgrd: bad header size %zd\n",
1038                                 hbytes);
1039                         error = EINVAL;
1040                         break;
1041                 }
1042                 msg = kmalloc(offsetof(struct hammer2_msg, any) + hbytes,
1043                               pmp->mmsg, M_WAITOK | M_ZERO);
1044                 msg->any.head = hdr;
1045                 msg->hdr_size = hbytes;
1046                 if (hbytes > sizeof(hdr)) {
1047                         error = fp_read(pmp->msg_fp, &msg->any.head + 1,
1048                                         hbytes - sizeof(hdr),
1049                                         NULL, 1, UIO_SYSSPACE);
1050                         if (error) {
1051                                 kprintf("hammer2: short msg received\n");
1052                                 error = EINVAL;
1053                                 break;
1054                         }
1055                 }
1056                 msg->aux_size = hdr.aux_bytes * HAMMER2_MSG_ALIGN;
1057                 if (msg->aux_size > HAMMER2_MSGAUX_MAX) {
1058                         kprintf("hammer2: illegal msg payload size %zd\n",
1059                                 msg->aux_size);
1060                         error = EINVAL;
1061                         break;
1062                 }
1063                 if (msg->aux_size) {
1064                         msg->aux_data = kmalloc(msg->aux_size, pmp->mmsg,
1065                                                 M_WAITOK | M_ZERO);
1066                         error = fp_read(pmp->msg_fp, msg->aux_data,
1067                                         msg->aux_size,
1068                                         NULL, 1, UIO_SYSSPACE);
1069                         if (error) {
1070                                 kprintf("hammer2: short msg "
1071                                         "payload received\n");
1072                                 break;
1073                         }
1074                 }
1075
1076                 /*
1077                  * State machine tracking, state assignment for msg,
1078                  * returns error and discard status.  Errors are fatal
1079                  * to the connection except for EALREADY which forces
1080                  * a discard without execution.
1081                  */
1082                 error = hammer2_state_msgrx(pmp, msg);
1083                 if (error) {
1084                         hammer2_msg_free(pmp, msg);
1085                         if (error == EALREADY)
1086                                 error = 0;
1087                 } else {
1088                         error = hammer2_msg_execute(pmp, msg);
1089                         hammer2_state_cleanuprx(pmp, msg);
1090                 }
1091                 msg = NULL;
1092         }
1093
1094         if (error)
1095                 kprintf("hammer2: msg read failed error %d\n", error);
1096
1097         lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1098         if (msg) {
1099                 if (msg->state && msg->state->msg == msg)
1100                         msg->state->msg = NULL;
1101                 hammer2_msg_free(pmp, msg);
1102         }
1103
1104         if ((state = pmp->freerd_state) != NULL) {
1105                 pmp->freerd_state = NULL;
1106                 hammer2_state_free(state);
1107         }
1108
1109         while ((state = RB_ROOT(&pmp->staterd_tree)) != NULL) {
1110                 RB_REMOVE(hammer2_state_tree, &pmp->staterd_tree, state);
1111                 hammer2_state_free(state);
1112         }
1113         lockmgr(&pmp->msglk, LK_RELEASE);
1114
1115         fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1116         pmp->msgrd_td = NULL;
1117         /* pmp can be ripped out from under us at this point */
1118         wakeup(pmp);
1119         lwkt_exit();
1120 }
1121
1122 static
1123 void
1124 hammer2_cluster_thread_wr(void *arg)
1125 {
1126         hammer2_pfsmount_t *pmp = arg;
1127         hammer2_msg_t *msg = NULL;
1128         hammer2_state_t *state;
1129         ssize_t res;
1130         int error = 0;
1131
1132         lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1133         while ((pmp->msg_ctl & HAMMER2_CLUSTERCTL_KILL) == 0 && error == 0) {
1134                 lksleep(&pmp->msg_ctl, &pmp->msglk, 0, "msgwr", hz);
1135                 while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1136                         /*
1137                          * Remove msg from the transmit queue and do
1138                          * persist and half-closed state handling.
1139                          */
1140                         TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1141                         lockmgr(&pmp->msglk, LK_RELEASE);
1142
1143                         error = hammer2_state_msgtx(pmp, msg);
1144                         if (error == EALREADY) {
1145                                 error = 0;
1146                                 hammer2_msg_free(pmp, msg);
1147                                 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1148                                 continue;
1149                         }
1150                         if (error)
1151                                 break;
1152
1153                         /*
1154                          * Dump the message to the pipe or socket.
1155                          */
1156                         error = fp_write(pmp->msg_fp, &msg->any, msg->hdr_size,
1157                                          &res, UIO_SYSSPACE);
1158                         if (error || res != msg->hdr_size) {
1159                                 if (error == 0)
1160                                         error = EINVAL;
1161                                 lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1162                                 break;
1163                         }
1164                         if (msg->aux_size) {
1165                                 error = fp_write(pmp->msg_fp,
1166                                                  msg->aux_data, msg->aux_size,
1167                                                  &res, UIO_SYSSPACE);
1168                                 if (error || res != msg->aux_size) {
1169                                         if (error == 0)
1170                                                 error = EINVAL;
1171                                         lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1172                                         break;
1173                                 }
1174                         }
1175                         hammer2_state_cleanuptx(pmp, msg);
1176                         lockmgr(&pmp->msglk, LK_EXCLUSIVE);
1177                 }
1178         }
1179
1180         /*
1181          * Cleanup messages pending transmission and release msgq lock.
1182          */
1183         if (error)
1184                 kprintf("hammer2: msg write failed error %d\n", error);
1185
1186         if (msg) {
1187                 if (msg->state && msg->state->msg == msg)
1188                         msg->state->msg = NULL;
1189                 hammer2_msg_free(pmp, msg);
1190         }
1191
1192         while ((msg = TAILQ_FIRST(&pmp->msgq)) != NULL) {
1193                 TAILQ_REMOVE(&pmp->msgq, msg, qentry);
1194                 if (msg->state && msg->state->msg == msg)
1195                         msg->state->msg = NULL;
1196                 hammer2_msg_free(pmp, msg);
1197         }
1198
1199         if ((state = pmp->freewr_state) != NULL) {
1200                 pmp->freewr_state = NULL;
1201                 hammer2_state_free(state);
1202         }
1203
1204         while ((state = RB_ROOT(&pmp->statewr_tree)) != NULL) {
1205                 RB_REMOVE(hammer2_state_tree, &pmp->statewr_tree, state);
1206                 hammer2_state_free(state);
1207         }
1208         lockmgr(&pmp->msglk, LK_RELEASE);
1209
1210         /*
1211          * Cleanup descriptor, be sure the read size is shutdown so the
1212          * (probably blocked) read operations returns an error.
1213          *
1214          * pmp can be ripped out from under us once msgwr_td is set to NULL.
1215          */
1216         fp_shutdown(pmp->msg_fp, SHUT_RDWR);
1217         pmp->msgwr_td = NULL;
1218         wakeup(pmp);
1219         lwkt_exit();
1220 }