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