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