hammer2 - dmsg blockdev work
[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"
50
b7926f31
MD
51struct hammer2_sync_info {
52 int error;
53 int waitfor;
54};
55
e4e20f48
MD
56TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
57static struct hammer2_mntlist hammer2_mntlist;
58static struct lock hammer2_mntlk;
59
37aa19df 60int hammer2_debug;
01eabad4 61int hammer2_cluster_enable = 1;
99535653 62int hammer2_hardlink_enable = 1;
01eabad4
MD
63long hammer2_iod_file_read;
64long hammer2_iod_meta_read;
65long hammer2_iod_indr_read;
66long hammer2_iod_file_write;
67long hammer2_iod_meta_write;
68long hammer2_iod_indr_write;
69long hammer2_iod_volu_write;
70long hammer2_ioa_file_read;
71long hammer2_ioa_meta_read;
72long hammer2_ioa_indr_read;
73long hammer2_ioa_file_write;
74long hammer2_ioa_meta_write;
75long hammer2_ioa_indr_write;
76long hammer2_ioa_volu_write;
37aa19df
MD
77
78SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
79
80SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
81 &hammer2_debug, 0, "");
01eabad4
MD
82SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
83 &hammer2_cluster_enable, 0, "");
e708f8b9
MD
84SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
85 &hammer2_hardlink_enable, 0, "");
01eabad4
MD
86SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
87 &hammer2_iod_file_read, 0, "");
88SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
89 &hammer2_iod_meta_read, 0, "");
90SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
91 &hammer2_iod_indr_read, 0, "");
92SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
93 &hammer2_iod_file_write, 0, "");
94SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
95 &hammer2_iod_meta_write, 0, "");
96SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
97 &hammer2_iod_indr_write, 0, "");
98SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
99 &hammer2_iod_volu_write, 0, "");
100SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
101 &hammer2_ioa_file_read, 0, "");
102SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
103 &hammer2_ioa_meta_read, 0, "");
104SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
105 &hammer2_ioa_indr_read, 0, "");
106SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
107 &hammer2_ioa_file_write, 0, "");
108SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
109 &hammer2_ioa_meta_write, 0, "");
110SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
111 &hammer2_ioa_indr_write, 0, "");
112SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
113 &hammer2_ioa_volu_write, 0, "");
37aa19df 114
b7926f31
MD
115static int hammer2_vfs_init(struct vfsconf *conf);
116static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
117 struct ucred *cred);
118static int hammer2_remount(struct mount *, char *, struct vnode *,
703720e4 119 struct ucred *);
b7926f31
MD
120static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
121static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
122static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
703720e4 123 struct ucred *cred);
b7926f31
MD
124static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
125 struct ucred *cred);
126static int hammer2_vfs_sync(struct mount *mp, int waitfor);
127static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
128 ino_t ino, struct vnode **vpp);
129static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
130 struct fid *fhp, struct vnode **vpp);
131static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
132static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
133 int *exflagsp, struct ucred **credanonp);
134
135static int hammer2_install_volume_header(hammer2_mount_t *hmp);
136static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
137static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
50e4f8f4 138
3a5aa68f
MD
139static int hammer2_msg_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
140static int hammer2_msg_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
141static int hammer2_msg_lnk_rcvmsg(kdmsg_msg_t *msg);
bfc3a7b1 142
703720e4
MD
143/*
144 * HAMMER2 vfs operations.
145 */
146static struct vfsops hammer2_vfsops = {
066e00cc
MD
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
703720e4
MD
158};
159
703720e4
MD
160MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
161
162VFS_SET(hammer2_vfsops, hammer2, 0);
163MODULE_VERSION(hammer2, 1);
164
e118c14f
MD
165static
166int
066e00cc 167hammer2_vfs_init(struct vfsconf *conf)
703720e4
MD
168{
169 int error;
170
171 error = 0;
172
0e92b724 173 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
703720e4 174 error = EINVAL;
0e92b724 175 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
703720e4 176 error = EINVAL;
0e92b724 177 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
703720e4 178 error = EINVAL;
0e92b724 179 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
703720e4
MD
180 error = EINVAL;
181
182 if (error)
183 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
184
e4e20f48
MD
185 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
186 TAILQ_INIT(&hammer2_mntlist);
187
703720e4
MD
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 */
e118c14f
MD
211static
212int
066e00cc 213hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
bfc3a7b1 214 struct ucred *cred)
703720e4
MD
215{
216 struct hammer2_mount_info info;
e4e20f48 217 hammer2_pfsmount_t *pmp;
54eb943b 218 hammer2_mount_t *hmp;
7cfa8da5 219 hammer2_key_t lhc;
703720e4
MD
220 struct vnode *devvp;
221 struct nlookupdata nd;
5c23d7f1 222 hammer2_chain_t *parent;
7cfa8da5
MD
223 hammer2_chain_t *schain;
224 hammer2_chain_t *rchain;
3a5aa68f 225 struct file *fp;
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;
3a5aa68f 355
26bf1a36 356 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
3a5aa68f 357 kdmsg_iocom_init(&pmp->iocom, pmp, pmp->mmsg,
3a5aa68f
MD
358 hammer2_msg_lnk_rcvmsg,
359 hammer2_msg_dbg_rcvmsg,
360 hammer2_msg_adhoc_input);
e4e20f48
MD
361
362 if (create_hmp) {
363 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
364 hmp->ronly = ronly;
365 hmp->devvp = devvp;
366 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
367 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
368 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
369 }
609a8021 370 ccms_domain_init(&pmp->ccms_dom);
e4e20f48
MD
371 pmp->hmp = hmp;
372 ++hmp->pmp_count;
373 lockmgr(&hammer2_mntlk, LK_RELEASE);
374 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
74d91941 375
f0206a67 376 mp->mnt_flag = MNT_LOCAL;
50e4f8f4
MD
377 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
378
e4e20f48
MD
379 if (create_hmp) {
380 /*
381 * vchain setup. vchain.data is special cased to NULL.
382 * vchain.refs is initialized and will never drop to 0.
383 */
e4e20f48
MD
384 hmp->vchain.refs = 1;
385 hmp->vchain.data = (void *)&hmp->voldata;
004f88b4 386 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
e4e20f48 387 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
004f88b4 388 hmp->vchain.bref_flush = hmp->vchain.bref;
46558838 389 ccms_cst_init(&hmp->vchain.cst, NULL);
e4e20f48 390 /* hmp->vchain.u.xxx is left NULL */
e4e20f48
MD
391 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
392 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
232a50f9 393
e4e20f48
MD
394 /*
395 * Install the volume header
396 */
397 error = hammer2_install_volume_header(hmp);
398 if (error) {
399 hammer2_vfs_unmount(mp, MNT_FORCE);
400 return error;
401 }
50e4f8f4 402 }
703720e4
MD
403
404 /*
50e4f8f4 405 * required mount structure initializations
703720e4 406 */
50e4f8f4
MD
407 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
408 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
409
410 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
411 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
412
2910a90c
MD
413 /*
414 * Optional fields
415 */
416 mp->mnt_iosize_max = MAXPHYS;
417
50e4f8f4 418 /*
7cfa8da5
MD
419 * First locate the super-root inode, which is key 0 relative to the
420 * volume header's blockset.
421 *
422 * Then locate the root inode by scanning the directory keyspace
423 * represented by the label.
50e4f8f4 424 */
e4e20f48
MD
425 if (create_hmp) {
426 parent = &hmp->vchain;
427 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
428 schain = hammer2_chain_lookup(hmp, &parent,
c667909f 429 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
e4e20f48
MD
430 hammer2_chain_unlock(hmp, parent);
431 if (schain == NULL) {
432 kprintf("hammer2_mount: invalid super-root\n");
433 hammer2_vfs_unmount(mp, MNT_FORCE);
434 return EINVAL;
435 }
436 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
437 hmp->schain = schain; /* left locked */
438 } else {
439 schain = hmp->schain;
440 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
7cfa8da5 441 }
5c23d7f1
MD
442
443 parent = schain;
e4e20f48 444 lhc = hammer2_dirhash(label, strlen(label));
5c23d7f1 445 rchain = hammer2_chain_lookup(hmp, &parent,
c667909f
MD
446 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
447 0);
7cfa8da5
MD
448 while (rchain) {
449 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
450 rchain->u.ip &&
5c23d7f1 451 strcmp(label, rchain->data->ipdata.filename) == 0) {
7cfa8da5
MD
452 break;
453 }
5c23d7f1 454 rchain = hammer2_chain_next(hmp, &parent, rchain,
c667909f
MD
455 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
456 0);
7cfa8da5 457 }
01eabad4 458 hammer2_chain_unlock(hmp, parent);
7cfa8da5 459 if (rchain == NULL) {
e4e20f48 460 kprintf("hammer2_mount: PFS label not found\n");
9c2e0de0 461 hammer2_vfs_unmount(mp, MNT_FORCE);
7cfa8da5
MD
462 return EINVAL;
463 }
e4e20f48
MD
464 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
465 hammer2_chain_unlock(hmp, rchain);
466 kprintf("hammer2_mount: PFS label already mounted!\n");
467 hammer2_vfs_unmount(mp, MNT_FORCE);
4d5318eb 468 return EBUSY;
e4e20f48
MD
469 }
470 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
5c23d7f1 471
e4e20f48
MD
472 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
473 hammer2_chain_unlock(hmp, rchain);
474 pmp->rchain = rchain; /* left held & unlocked */
475 pmp->iroot = rchain->u.ip; /* implied hold from rchain */
476 pmp->iroot->pmp = pmp;
609a8021 477
e4e20f48 478 kprintf("iroot %p\n", pmp->iroot);
703720e4 479
bfc3a7b1
MD
480 /*
481 * Ref the cluster management messaging descriptor. The mount
482 * program deals with the other end of the communications pipe.
483 */
3a5aa68f
MD
484 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
485 if (fp == NULL) {
bfc3a7b1
MD
486 kprintf("hammer2_mount: bad cluster_fd!\n");
487 hammer2_vfs_unmount(mp, MNT_FORCE);
488 return EBADF;
489 }
3a5aa68f 490 hammer2_cluster_reconnect(pmp, fp);
bfc3a7b1
MD
491
492 /*
493 * Finish setup
494 */
f0206a67
VS
495 vfs_getnewfsid(mp);
496 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
497 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
498 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
703720e4 499
54f522df 500 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
703720e4
MD
501 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
502 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
503 copyinstr(path, mp->mnt_stat.f_mntonname,
504 sizeof(mp->mnt_stat.f_mntonname) - 1,
505 &size);
506
bfc3a7b1
MD
507 /*
508 * Initial statfs to prime mnt_stat.
509 */
066e00cc 510 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
703720e4 511
47902fef 512 return 0;
703720e4
MD
513}
514
e118c14f
MD
515static
516int
703720e4
MD
517hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
518 struct ucred *cred)
519{
520 return (0);
521}
522
e118c14f
MD
523static
524int
066e00cc 525hammer2_vfs_unmount(struct mount *mp, int mntflags)
703720e4 526{
e4e20f48 527 hammer2_pfsmount_t *pmp;
54eb943b 528 hammer2_mount_t *hmp;
703720e4 529 int flags;
50e4f8f4 530 int error = 0;
54eb943b
MD
531 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
532 struct vnode *devvp;
703720e4 533
e4e20f48
MD
534 pmp = MPTOPMP(mp);
535 hmp = pmp->hmp;
703720e4
MD
536 flags = 0;
537
538 if (mntflags & MNT_FORCE)
539 flags |= FORCECLOSE;
540
541 hammer2_mount_exlock(hmp);
542
50e4f8f4
MD
543 /*
544 * If mount initialization proceeded far enough we must flush
545 * its vnodes.
546 */
e4e20f48 547 if (pmp->iroot)
50e4f8f4
MD
548 error = vflush(mp, 0, flags);
549
550 if (error)
551 return error;
703720e4 552
e4e20f48
MD
553 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
554 --hmp->pmp_count;
555 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
556
703720e4 557 /*
2910a90c
MD
558 * Flush any left over chains. The voldata lock is only used
559 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
222d9e22 560 */
2910a90c
MD
561 hammer2_voldata_lock(hmp);
562 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
563 HAMMER2_CHAIN_MODIFIED_AUX |
222d9e22 564 HAMMER2_CHAIN_SUBMODIFIED)) {
2910a90c 565 hammer2_voldata_unlock(hmp);
222d9e22 566 hammer2_vfs_sync(mp, MNT_WAIT);
2910a90c
MD
567 } else {
568 hammer2_voldata_unlock(hmp);
222d9e22 569 }
e4e20f48
MD
570 if (hmp->pmp_count == 0) {
571 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
572 HAMMER2_CHAIN_MODIFIED_AUX |
573 HAMMER2_CHAIN_SUBMODIFIED)) {
574 kprintf("hammer2_unmount: chains left over after "
575 "final sync\n");
576 if (hammer2_debug & 0x0010)
577 Debugger("entered debugger");
578 }
222d9e22
MD
579 }
580
581 /*
582 * Cleanup the root and super-root chain elements (which should be
583 * clean).
703720e4 584 */
e4e20f48
MD
585 pmp->iroot = NULL;
586 if (pmp->rchain) {
587 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
588 KKASSERT(pmp->rchain->refs == 1);
589 hammer2_chain_drop(hmp, pmp->rchain);
590 pmp->rchain = NULL;
7cfa8da5 591 }
609a8021 592 ccms_domain_uninit(&pmp->ccms_dom);
bfc3a7b1
MD
593
594 /*
185ace93 595 * Kill cluster controller
bfc3a7b1 596 */
185ace93 597 kdmsg_iocom_uninit(&pmp->iocom);
bfc3a7b1
MD
598
599 /*
600 * If no PFS's left drop the master hammer2_mount for the device.
601 */
e4e20f48
MD
602 if (hmp->pmp_count == 0) {
603 if (hmp->schain) {
604 KKASSERT(hmp->schain->refs == 1);
605 hammer2_chain_drop(hmp, hmp->schain);
606 hmp->schain = NULL;
607 }
222d9e22 608
e4e20f48
MD
609 /*
610 * Finish up with the device vnode
611 */
612 if ((devvp = hmp->devvp) != NULL) {
613 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
614 hmp->devvp = NULL;
615 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
616 vrele(devvp);
617 devvp = NULL;
618 }
54eb943b 619 }
54eb943b 620 hammer2_mount_unlock(hmp);
703720e4 621
e4e20f48
MD
622 pmp->mp = NULL;
623 pmp->hmp = NULL;
54eb943b 624 mp->mnt_data = NULL;
703720e4 625
26bf1a36
MD
626 kmalloc_destroy(&pmp->mmsg);
627
e4e20f48
MD
628 kfree(pmp, M_HAMMER2);
629 if (hmp->pmp_count == 0) {
630 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
004f88b4
MD
631 kmalloc_destroy(&hmp->minode);
632 kmalloc_destroy(&hmp->mchain);
e4e20f48
MD
633 kfree(hmp, M_HAMMER2);
634 }
635 lockmgr(&hammer2_mntlk, LK_RELEASE);
703720e4
MD
636 return (error);
637}
638
e118c14f
MD
639static
640int
066e00cc 641hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
703720e4
MD
642 ino_t ino, struct vnode **vpp)
643{
644 kprintf("hammer2_vget\n");
645 return (EOPNOTSUPP);
646}
647
e118c14f
MD
648static
649int
066e00cc 650hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
703720e4 651{
e4e20f48 652 hammer2_pfsmount_t *pmp;
a0ed3c24 653 hammer2_mount_t *hmp;
703720e4
MD
654 int error;
655 struct vnode *vp;
656
e4e20f48 657 pmp = MPTOPMP(mp);
a0ed3c24
MD
658 hmp = pmp->hmp;
659 hammer2_mount_exlock(hmp);
e4e20f48 660 if (pmp->iroot == NULL) {
703720e4
MD
661 *vpp = NULL;
662 error = EINVAL;
663 } else {
a0ed3c24
MD
664 hammer2_chain_lock(hmp, &pmp->iroot->chain,
665 HAMMER2_RESOLVE_ALWAYS |
666 HAMMER2_RESOLVE_SHARED);
e4e20f48 667 vp = hammer2_igetv(pmp->iroot, &error);
a0ed3c24 668 hammer2_chain_unlock(hmp, &pmp->iroot->chain);
703720e4
MD
669 *vpp = vp;
670 if (vp == NULL)
671 kprintf("vnodefail\n");
672 }
a0ed3c24 673 hammer2_mount_unlock(hmp);
703720e4
MD
674
675 return (error);
676}
677
28ee5f14
MD
678/*
679 * Filesystem status
680 *
681 * XXX incorporate pmp->iroot->ip_data.inode_quota and data_quota
682 */
e118c14f
MD
683static
684int
066e00cc 685hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
703720e4 686{
28ee5f14 687 hammer2_pfsmount_t *pmp;
54eb943b 688 hammer2_mount_t *hmp;
703720e4 689
28ee5f14 690 pmp = MPTOPMP(mp);
e4e20f48 691 hmp = MPTOHMP(mp);
703720e4 692
28ee5f14
MD
693 mp->mnt_stat.f_files = pmp->iroot->ip_data.inode_count +
694 pmp->iroot->delta_icount;
695 mp->mnt_stat.f_ffree = 0;
696 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
697 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
698 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
50e4f8f4 699 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
703720e4 700
50e4f8f4 701 *sbp = mp->mnt_stat;
703720e4
MD
702 return (0);
703}
704
e118c14f
MD
705static
706int
066e00cc 707hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
703720e4 708{
28ee5f14 709 hammer2_pfsmount_t *pmp;
50e4f8f4
MD
710 hammer2_mount_t *hmp;
711
28ee5f14 712 pmp = MPTOPMP(mp);
e4e20f48 713 hmp = MPTOHMP(mp);
50e4f8f4 714
88a032af 715 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
28ee5f14
MD
716 mp->mnt_vstat.f_files = pmp->iroot->ip_data.inode_count +
717 pmp->iroot->delta_icount;
718 mp->mnt_vstat.f_ffree = 0;
719 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
720 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
721 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
722 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
50e4f8f4
MD
723
724 *sbp = mp->mnt_vstat;
725 return (0);
703720e4
MD
726}
727
728/*
729 * Sync the entire filesystem; this is called from the filesystem syncer
730 * process periodically and whenever a user calls sync(1) on the hammer
731 * mountpoint.
732 *
733 * Currently is actually called from the syncer! \o/
734 *
735 * This task will have to snapshot the state of the dirty inode chain.
736 * From that, it will have to make sure all of the inodes on the dirty
737 * chain have IO initiated. We make sure that io is initiated for the root
738 * block.
739 *
740 * If waitfor is set, we wait for media to acknowledge the new rootblock.
741 *
742 * THINKS: side A vs side B, to have sync not stall all I/O?
743 */
e118c14f
MD
744static
745int
066e00cc 746hammer2_vfs_sync(struct mount *mp, int waitfor)
703720e4 747{
b7926f31 748 struct hammer2_sync_info info;
54eb943b 749 hammer2_mount_t *hmp;
b7926f31
MD
750 int flags;
751 int error;
73e441b9 752 int haswork;
703720e4 753
e4e20f48 754 hmp = MPTOHMP(mp);
703720e4 755
b7926f31
MD
756 flags = VMSC_GETVP;
757 if (waitfor & MNT_LAZY)
758 flags |= VMSC_ONEPASS;
759
760 info.error = 0;
761 info.waitfor = MNT_NOWAIT;
762 vmntvnodescan(mp, flags | VMSC_NOWAIT,
763 hammer2_sync_scan1,
764 hammer2_sync_scan2, &info);
765 if (info.error == 0 && (waitfor & MNT_WAIT)) {
766 info.waitfor = waitfor;
767 vmntvnodescan(mp, flags,
768 hammer2_sync_scan1,
769 hammer2_sync_scan2, &info);
770
771 }
e118c14f 772#if 0
b7926f31
MD
773 if (waitfor == MNT_WAIT) {
774 /* XXX */
775 } else {
776 /* XXX */
777 }
e118c14f 778#endif
01eabad4 779 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2910a90c
MD
780 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
781 HAMMER2_CHAIN_MODIFIED_AUX |
782 HAMMER2_CHAIN_SUBMODIFIED)) {
4d5318eb 783 hammer2_chain_flush(hmp, &hmp->vchain, 0);
73e441b9
MD
784 haswork = 1;
785 } else {
786 haswork = 0;
787 }
b7926f31 788 hammer2_chain_unlock(hmp, &hmp->vchain);
1c9f601e
MD
789
790 error = 0;
791
792 if ((waitfor & MNT_LAZY) == 0) {
793 waitfor = MNT_NOWAIT;
794 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
795 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
796 vn_unlock(hmp->devvp);
797 }
798
73e441b9 799 if (error == 0 && haswork) {
b7926f31
MD
800 struct buf *bp;
801
2910a90c
MD
802 /*
803 * Synchronize the disk before flushing the volume
804 * header.
805 */
b7926f31
MD
806 bp = getpbuf(NULL);
807 bp->b_bio1.bio_offset = 0;
808 bp->b_bufsize = 0;
809 bp->b_bcount = 0;
810 bp->b_cmd = BUF_CMD_FLUSH;
811 bp->b_bio1.bio_done = biodone_sync;
812 bp->b_bio1.bio_flags |= BIO_SYNC;
813 vn_strategy(hmp->devvp, &bp->b_bio1);
814 biowait(&bp->b_bio1, "h2vol");
815 relpbuf(bp, NULL);
816
2910a90c
MD
817 /*
818 * Then we can safely flush the volume header. Volume
819 * data is locked separately to prevent ioctl functions
820 * from deadlocking due to a configuration issue.
821 */
b7926f31 822 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
2910a90c 823 hammer2_voldata_lock(hmp);
b7926f31 824 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
2910a90c 825 hammer2_voldata_unlock(hmp);
b7926f31
MD
826 bawrite(bp);
827 }
828 return (error);
829}
703720e4 830
214f4a77
MD
831/*
832 * Sync passes.
833 *
834 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
835 * won't flush on those flags. The syncer code above will do a
836 * general meta-data flush globally that will catch these flags.
837 */
b7926f31
MD
838static int
839hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
840{
841 hammer2_inode_t *ip;
842
843 ip = VTOI(vp);
844 if (vp->v_type == VNON || ip == NULL ||
2910a90c 845 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
214f4a77 846 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
b7926f31
MD
847 RB_EMPTY(&vp->v_rbdirty_tree))) {
848 return(-1);
849 }
850 return(0);
851}
852
853static int
854hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
855{
856 struct hammer2_sync_info *info = data;
857 hammer2_inode_t *ip;
858 int error;
859
860 ip = VTOI(vp);
861 if (vp->v_type == VNON || vp->v_type == VBAD ||
2910a90c 862 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
214f4a77
MD
863 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
864 RB_EMPTY(&vp->v_rbdirty_tree))) {
b7926f31
MD
865 return(0);
866 }
867 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
868 if (error)
869 info->error = error;
870 return(0);
703720e4
MD
871}
872
e118c14f
MD
873static
874int
066e00cc 875hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
703720e4
MD
876{
877 return (0);
878}
879
e118c14f
MD
880static
881int
066e00cc 882hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
703720e4
MD
883 struct fid *fhp, struct vnode **vpp)
884{
885 return (0);
886}
887
e118c14f
MD
888static
889int
066e00cc 890hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
703720e4
MD
891 int *exflagsp, struct ucred **credanonp)
892{
893 return (0);
894}
50e4f8f4
MD
895
896/*
897 * Support code for hammer2_mount(). Read, verify, and install the volume
898 * header into the HMP
899 *
900 * XXX read four volhdrs and use the one with the highest TID whos CRC
901 * matches.
902 *
903 * XXX check iCRCs.
57381c9e
VS
904 *
905 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
906 * nonexistant locations.
907 *
908 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
50e4f8f4
MD
909 */
910static
911int
912hammer2_install_volume_header(hammer2_mount_t *hmp)
913{
914 hammer2_volume_data_t *vd;
99924359 915 struct buf *bp;
60fbd5f4 916 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
99924359
MD
917 int error_reported;
918 int error;
0b3147ba
VS
919 int valid;
920 int i;
50e4f8f4 921
99924359 922 error_reported = 0;
0b3147ba
VS
923 error = 0;
924 valid = 0;
99924359 925 bp = NULL;
0b3147ba 926
99924359
MD
927 /*
928 * There are up to 4 copies of the volume header (syncs iterate
929 * between them so there is no single master). We don't trust the
930 * volu_size field so we don't know precisely how large the filesystem
931 * is, so depend on the OS to return an error if we go beyond the
932 * block device's EOF.
933 */
0b3147ba 934 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
62efe6ec 935 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
99924359 936 HAMMER2_VOLUME_BYTES, &bp);
0b3147ba 937 if (error) {
99924359
MD
938 brelse(bp);
939 bp = NULL;
0b3147ba
VS
940 continue;
941 }
942
88a032af
VS
943 vd = (struct hammer2_volume_data *) bp->b_data;
944 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
945 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
946 brelse(bp);
947 bp = NULL;
0b3147ba 948 continue;
88a032af
VS
949 }
950
951 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
952 /* XXX: Reversed-endianness filesystem */
953 kprintf("hammer2: reverse-endian filesystem detected");
954 brelse(bp);
955 bp = NULL;
956 continue;
957 }
0b3147ba
VS
958
959 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
60fbd5f4 960 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
99924359 961 HAMMER2_VOLUME_ICRC0_SIZE);
60fbd5f4
VS
962 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
963 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
964 HAMMER2_VOLUME_ICRC1_SIZE);
965 if ((crc0 != crc) || (bcrc0 != bcrc)) {
99924359
MD
966 kprintf("hammer2 volume header crc "
967 "mismatch copy #%d\t%08x %08x",
60fbd5f4 968 i, crc0, crc);
99924359
MD
969 error_reported = 1;
970 brelse(bp);
971 bp = NULL;
0b3147ba
VS
972 continue;
973 }
4d5318eb 974 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
99924359
MD
975 valid = 1;
976 hmp->voldata = *vd;
0b3147ba 977 }
99924359
MD
978 brelse(bp);
979 bp = NULL;
50e4f8f4 980 }
0b3147ba 981 if (valid) {
57381c9e 982 error = 0;
99924359
MD
983 if (error_reported)
984 kprintf("hammer2: a valid volume header was found\n");
0b3147ba
VS
985 } else {
986 error = EINVAL;
99924359 987 kprintf("hammer2: no valid volume headers found!\n");
0b3147ba 988 }
0b3147ba 989 return (error);
50e4f8f4 990}
0b3147ba 991
1a34728c
MD
992/*
993 * Reconnect using the passed file pointer. The caller must ref the
994 * fp for us.
995 */
996void
997hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
998{
3a5aa68f
MD
999 kdmsg_msg_t *msg;
1000 size_t name_len;
70c3c3b7
MD
1001
1002 /*
3a5aa68f
MD
1003 * Closes old comm descriptor, kills threads, cleans up
1004 * states, then installs the new descriptor and creates
1005 * new threads.
1a34728c 1006 */
3a5aa68f 1007 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
bfc3a7b1 1008
9b8b748f 1009 /*
8c280d5d
MD
1010 * Open a LNK_CONN transaction indicating that we want to take part
1011 * in the spanning tree algorithm. Filter explicitly on the PFS
1012 * info in the iroot.
1013 *
1014 * We do not transmit our (only) LNK_SPAN until the other end has
1015 * acknowledged our link connection request.
42e2a62e 1016 *
8c280d5d
MD
1017 * The transaction remains fully open for the duration of the
1018 * connection.
9b8b748f 1019 */
3a5aa68f 1020 msg = kdmsg_msg_alloc(&pmp->iocom.router, DMSG_LNK_CONN | DMSGF_CREATE,
1a34728c 1021 hammer2_msg_conn_reply, pmp);
8c280d5d
MD
1022 msg->any.lnk_conn.pfs_clid = pmp->iroot->ip_data.pfs_clid;
1023 msg->any.lnk_conn.pfs_fsid = pmp->iroot->ip_data.pfs_fsid;
1024 msg->any.lnk_conn.pfs_type = pmp->iroot->ip_data.pfs_type;
5bc5bca2 1025 msg->any.lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
2063f4d7
MD
1026 msg->any.lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1027 msg->any.lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
42e2a62e 1028 name_len = pmp->iroot->ip_data.name_len;
ddfbb283
MD
1029 if (name_len >= sizeof(msg->any.lnk_conn.fs_label))
1030 name_len = sizeof(msg->any.lnk_conn.fs_label) - 1;
1031 bcopy(pmp->iroot->ip_data.filename,
1032 msg->any.lnk_conn.fs_label,
1033 name_len);
3a5aa68f 1034 pmp->iocom.conn_state = msg->state;
ddfbb283 1035 msg->any.lnk_conn.fs_label[name_len] = 0;
3a5aa68f 1036 kdmsg_msg_write(msg);
70c3c3b7
MD
1037}
1038
9b8b748f 1039static int
3a5aa68f 1040hammer2_msg_lnk_rcvmsg(kdmsg_msg_t *msg)
9b8b748f 1041{
5bc5bca2
MD
1042 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1043 case DMSG_LNK_CONN | DMSGF_CREATE:
10c86c4e
MD
1044 /*
1045 * reply & leave trans open
1046 */
8c280d5d 1047 kprintf("CONN RECEIVE - (just ignore it)\n");
3a5aa68f 1048 kdmsg_msg_result(msg, 0);
8c280d5d 1049 break;
5bc5bca2 1050 case DMSG_LNK_SPAN | DMSGF_CREATE:
8c280d5d
MD
1051 kprintf("SPAN RECEIVE - ADDED FROM CLUSTER\n");
1052 break;
5bc5bca2 1053 case DMSG_LNK_SPAN | DMSGF_DELETE:
8c280d5d
MD
1054 kprintf("SPAN RECEIVE - DELETED FROM CLUSTER\n");
1055 break;
1056 default:
1057 break;
1058 }
1059 return(0);
1060}
1061
1062/*
1063 * This function is called when the other end replies to our LNK_CONN
1064 * request.
1065 *
1066 * We transmit our (single) SPAN on the initial reply, leaving that
1067 * transaction open too.
1068 */
1069static int
3a5aa68f 1070hammer2_msg_conn_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
8c280d5d
MD
1071{
1072 hammer2_pfsmount_t *pmp = state->any.pmp;
1a34728c 1073 hammer2_mount_t *hmp = pmp->hmp;
3a5aa68f 1074 kdmsg_msg_t *rmsg;
8c280d5d 1075 size_t name_len;
1a34728c 1076 int copyid;
8c280d5d 1077
70c3c3b7
MD
1078 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1079
5bc5bca2 1080 if (msg->any.head.cmd & DMSGF_CREATE) {
8c280d5d 1081 kprintf("LNK_CONN transaction replied to, initiate SPAN\n");
3a5aa68f
MD
1082 rmsg = kdmsg_msg_alloc(&pmp->iocom.router,
1083 DMSG_LNK_SPAN | DMSGF_CREATE,
1084 hammer2_msg_span_reply, pmp);
70c3c3b7
MD
1085 rmsg->any.lnk_span.pfs_clid = pmp->iroot->ip_data.pfs_clid;
1086 rmsg->any.lnk_span.pfs_fsid = pmp->iroot->ip_data.pfs_fsid;
1087 rmsg->any.lnk_span.pfs_type = pmp->iroot->ip_data.pfs_type;
2063f4d7 1088 rmsg->any.lnk_span.peer_type = pmp->hmp->voldata.peer_type;
5bc5bca2 1089 rmsg->any.lnk_span.proto_version = DMSG_SPAN_PROTO_1;
8c280d5d 1090 name_len = pmp->iroot->ip_data.name_len;
ddfbb283
MD
1091 if (name_len >= sizeof(rmsg->any.lnk_span.fs_label))
1092 name_len = sizeof(rmsg->any.lnk_span.fs_label) - 1;
8c280d5d 1093 bcopy(pmp->iroot->ip_data.filename,
ddfbb283 1094 rmsg->any.lnk_span.fs_label,
8c280d5d 1095 name_len);
ddfbb283 1096 rmsg->any.lnk_span.fs_label[name_len] = 0;
3a5aa68f 1097 kdmsg_msg_write(rmsg);
1a34728c
MD
1098
1099 /*
1100 * Dump the configuration stored in the volume header
1101 */
1102 hammer2_voldata_lock(hmp);
1103 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1104 if (hmp->voldata.copyinfo[copyid].copyid == 0)
1105 continue;
1106 hammer2_volconf_update(pmp, copyid);
1107 }
1108 hammer2_voldata_unlock(hmp);
8c280d5d 1109 }
5bc5bca2
MD
1110 if ((state->txcmd & DMSGF_DELETE) == 0 &&
1111 (msg->any.head.cmd & DMSGF_DELETE)) {
8c280d5d 1112 kprintf("LNK_CONN transaction terminated by remote\n");
3a5aa68f
MD
1113 pmp->iocom.conn_state = NULL;
1114 kdmsg_msg_reply(msg, 0);
8c280d5d
MD
1115 }
1116 return(0);
1117}
1118
70c3c3b7
MD
1119/*
1120 * Remote terminated our span transaction. We have to terminate our side.
1121 */
8c280d5d 1122static int
3a5aa68f 1123hammer2_msg_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
8c280d5d 1124{
5bc5bca2 1125 /*hammer2_pfsmount_t *pmp = state->any.pmp;*/
8c280d5d 1126
5bc5bca2
MD
1127 kprintf("SPAN REPLY - Our sent span was terminated by the "
1128 "remote %08x state %p\n", msg->any.head.cmd, state);
1129 if ((state->txcmd & DMSGF_DELETE) == 0 &&
1130 (msg->any.head.cmd & DMSGF_DELETE)) {
3a5aa68f 1131 kdmsg_msg_reply(msg, 0);
70c3c3b7 1132 }
9b8b748f
MD
1133 return(0);
1134}
1a34728c
MD
1135
1136/*
1137 * Volume configuration updates are passed onto the userland service
1138 * daemon via the open LNK_CONN transaction.
1139 */
1140void
1141hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1142{
1143 hammer2_mount_t *hmp = pmp->hmp;
3a5aa68f 1144 kdmsg_msg_t *msg;
1a34728c
MD
1145
1146 /* XXX interlock against connection state termination */
3a5aa68f
MD
1147 kprintf("volconf update %p\n", pmp->iocom.conn_state);
1148 if (pmp->iocom.conn_state) {
1a34728c 1149 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
3a5aa68f 1150 msg = kdmsg_msg_alloc(&pmp->iocom.router, DMSG_LNK_VOLCONF,
1a34728c 1151 NULL, NULL);
3a5aa68f 1152 msg->state = pmp->iocom.conn_state;
1a34728c
MD
1153 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1154 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1155 msg->any.lnk_volconf.index = index;
3a5aa68f 1156 kdmsg_msg_write(msg);
1a34728c
MD
1157 }
1158}