rpc.statd(8): Fix warnings and bump WARNS to 6.
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
CommitLineData
47902fef 1/*-
0dea3156 2 * Copyright (c) 2011-2013 The DragonFly Project. All rights reserved.
703720e4
MD
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
0dea3156
MD
51#define REPORT_REFS_ERRORS 1 /* XXX remove me */
52
b7926f31 53struct hammer2_sync_info {
0dea3156 54 hammer2_trans_t trans;
b7926f31
MD
55 int error;
56 int waitfor;
57};
58
e4e20f48
MD
59TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
60static struct hammer2_mntlist hammer2_mntlist;
61static struct lock hammer2_mntlk;
62
37aa19df 63int hammer2_debug;
1a7cfe5a 64int hammer2_cluster_enable = 0; /* XXX temporary until layout ironed out */
99535653 65int hammer2_hardlink_enable = 1;
01eabad4
MD
66long hammer2_iod_file_read;
67long hammer2_iod_meta_read;
68long hammer2_iod_indr_read;
69long hammer2_iod_file_write;
70long hammer2_iod_meta_write;
71long hammer2_iod_indr_write;
9061bde5 72long hammer2_iod_fmap_write;
01eabad4
MD
73long hammer2_iod_volu_write;
74long hammer2_ioa_file_read;
75long hammer2_ioa_meta_read;
76long hammer2_ioa_indr_read;
9061bde5 77long hammer2_ioa_fmap_write;
01eabad4
MD
78long hammer2_ioa_file_write;
79long hammer2_ioa_meta_write;
80long hammer2_ioa_indr_write;
81long hammer2_ioa_volu_write;
37aa19df
MD
82
83SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
84
85SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
86 &hammer2_debug, 0, "");
01eabad4
MD
87SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
88 &hammer2_cluster_enable, 0, "");
e708f8b9
MD
89SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
90 &hammer2_hardlink_enable, 0, "");
01eabad4
MD
91SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
92 &hammer2_iod_file_read, 0, "");
93SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
94 &hammer2_iod_meta_read, 0, "");
95SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
96 &hammer2_iod_indr_read, 0, "");
97SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
98 &hammer2_iod_file_write, 0, "");
99SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
100 &hammer2_iod_meta_write, 0, "");
101SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
102 &hammer2_iod_indr_write, 0, "");
103SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
104 &hammer2_iod_volu_write, 0, "");
105SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
106 &hammer2_ioa_file_read, 0, "");
107SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
108 &hammer2_ioa_meta_read, 0, "");
109SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
110 &hammer2_ioa_indr_read, 0, "");
111SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
112 &hammer2_ioa_file_write, 0, "");
113SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
114 &hammer2_ioa_meta_write, 0, "");
115SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
116 &hammer2_ioa_indr_write, 0, "");
117SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
118 &hammer2_ioa_volu_write, 0, "");
37aa19df 119
b7926f31
MD
120static int hammer2_vfs_init(struct vfsconf *conf);
121static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
122 struct ucred *cred);
123static int hammer2_remount(struct mount *, char *, struct vnode *,
703720e4 124 struct ucred *);
b7926f31
MD
125static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
126static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
127static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
703720e4 128 struct ucred *cred);
b7926f31
MD
129static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
130 struct ucred *cred);
131static int hammer2_vfs_sync(struct mount *mp, int waitfor);
132static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
133 ino_t ino, struct vnode **vpp);
134static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
135 struct fid *fhp, struct vnode **vpp);
136static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
137static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
138 int *exflagsp, struct ucred **credanonp);
139
140static int hammer2_install_volume_header(hammer2_mount_t *hmp);
141static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
142static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
50e4f8f4 143
537d97bc
MD
144static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
145static void hammer2_autodmsg(kdmsg_msg_t *msg);
bfc3a7b1 146
703720e4
MD
147/*
148 * HAMMER2 vfs operations.
149 */
150static struct vfsops hammer2_vfsops = {
066e00cc
MD
151 .vfs_init = hammer2_vfs_init,
152 .vfs_sync = hammer2_vfs_sync,
153 .vfs_mount = hammer2_vfs_mount,
154 .vfs_unmount = hammer2_vfs_unmount,
155 .vfs_root = hammer2_vfs_root,
156 .vfs_statfs = hammer2_vfs_statfs,
157 .vfs_statvfs = hammer2_vfs_statvfs,
158 .vfs_vget = hammer2_vfs_vget,
159 .vfs_vptofh = hammer2_vfs_vptofh,
160 .vfs_fhtovp = hammer2_vfs_fhtovp,
161 .vfs_checkexp = hammer2_vfs_checkexp
703720e4
MD
162};
163
703720e4
MD
164MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
165
166VFS_SET(hammer2_vfsops, hammer2, 0);
167MODULE_VERSION(hammer2, 1);
168
e118c14f
MD
169static
170int
066e00cc 171hammer2_vfs_init(struct vfsconf *conf)
703720e4
MD
172{
173 int error;
174
175 error = 0;
176
0e92b724 177 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
703720e4 178 error = EINVAL;
0e92b724 179 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
703720e4 180 error = EINVAL;
0e92b724 181 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
703720e4
MD
182 error = EINVAL;
183
184 if (error)
185 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
186
e4e20f48
MD
187 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
188 TAILQ_INIT(&hammer2_mntlist);
189
703720e4
MD
190 return (error);
191}
192
193/*
194 * Mount or remount HAMMER2 fileystem from physical media
195 *
196 * mountroot
197 * mp mount point structure
198 * path NULL
199 * data <unused>
200 * cred <unused>
201 *
202 * mount
203 * mp mount point structure
204 * path path to mount point
205 * data pointer to argument structure in user space
206 * volume volume path (device@LABEL form)
207 * hflags user mount flags
208 * cred user credentials
209 *
210 * RETURNS: 0 Success
211 * !0 error number
212 */
e118c14f
MD
213static
214int
066e00cc 215hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
bfc3a7b1 216 struct ucred *cred)
703720e4
MD
217{
218 struct hammer2_mount_info info;
e4e20f48 219 hammer2_pfsmount_t *pmp;
54eb943b 220 hammer2_mount_t *hmp;
7cfa8da5 221 hammer2_key_t lhc;
703720e4
MD
222 struct vnode *devvp;
223 struct nlookupdata nd;
5c23d7f1 224 hammer2_chain_t *parent;
7cfa8da5
MD
225 hammer2_chain_t *schain;
226 hammer2_chain_t *rchain;
3a5aa68f 227 struct file *fp;
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;
3a5aa68f 357
9797e933
MD
358 spin_init(&pmp->inum_spin);
359 RB_INIT(&pmp->inum_tree);
360
26bf1a36 361 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
537d97bc
MD
362 kdmsg_iocom_init(&pmp->iocom, pmp,
363 KDMSG_IOCOMF_AUTOCONN |
364 KDMSG_IOCOMF_AUTOSPAN |
365 KDMSG_IOCOMF_AUTOCIRC,
366 pmp->mmsg, hammer2_rcvdmsg);
e4e20f48
MD
367
368 if (create_hmp) {
369 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
370 hmp->ronly = ronly;
371 hmp->devvp = devvp;
372 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
373 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
374 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
375 }
609a8021 376 ccms_domain_init(&pmp->ccms_dom);
e4e20f48
MD
377 pmp->hmp = hmp;
378 ++hmp->pmp_count;
379 lockmgr(&hammer2_mntlk, LK_RELEASE);
380 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
74d91941 381
f0206a67 382 mp->mnt_flag = MNT_LOCAL;
50e4f8f4
MD
383 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
384
e4e20f48 385 if (create_hmp) {
0dea3156
MD
386 /*
387 * Presetup
388 */
389 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
390 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
d001f460 391 TAILQ_INIT(&hmp->transq);
0dea3156 392
e4e20f48 393 /*
1a7cfe5a 394 * vchain setup. vchain.data is embedded.
e4e20f48
MD
395 * vchain.refs is initialized and will never drop to 0.
396 */
0dea3156 397 hmp->vchain.hmp = hmp;
e4e20f48
MD
398 hmp->vchain.refs = 1;
399 hmp->vchain.data = (void *)&hmp->voldata;
004f88b4 400 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
e4e20f48 401 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
731b2a84 402 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
0dea3156 403 hammer2_chain_core_alloc(&hmp->vchain, NULL);
e4e20f48 404 /* hmp->vchain.u.xxx is left NULL */
232a50f9 405
1a7cfe5a
MD
406 /*
407 * fchain setup. fchain.data is embedded.
408 * fchain.refs is initialized and will never drop to 0.
409 *
410 * The data is not used but needs to be initialized to
411 * pass assertion muster. We use this chain primarily
412 * as a placeholder for the freemap's top-level RBTREE
413 * so it does not interfere with the volume's topology
414 * RBTREE.
415 */
416 hmp->fchain.hmp = hmp;
417 hmp->fchain.refs = 1;
418 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
419 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
420 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
512beabd
MD
421 hmp->fchain.bref.methods =
422 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
423 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
1a7cfe5a 424 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
512beabd 425
1a7cfe5a
MD
426 hammer2_chain_core_alloc(&hmp->fchain, NULL);
427 /* hmp->fchain.u.xxx is left NULL */
428
e4e20f48
MD
429 /*
430 * Install the volume header
431 */
432 error = hammer2_install_volume_header(hmp);
433 if (error) {
434 hammer2_vfs_unmount(mp, MNT_FORCE);
435 return error;
436 }
50e4f8f4 437 }
703720e4
MD
438
439 /*
50e4f8f4 440 * required mount structure initializations
703720e4 441 */
50e4f8f4
MD
442 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
443 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
444
445 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
446 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
447
2910a90c
MD
448 /*
449 * Optional fields
450 */
451 mp->mnt_iosize_max = MAXPHYS;
452
50e4f8f4 453 /*
7cfa8da5
MD
454 * First locate the super-root inode, which is key 0 relative to the
455 * volume header's blockset.
456 *
457 * Then locate the root inode by scanning the directory keyspace
458 * represented by the label.
50e4f8f4 459 */
e4e20f48 460 if (create_hmp) {
0dea3156
MD
461 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
462 schain = hammer2_chain_lookup(&parent,
c667909f 463 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
0dea3156 464 hammer2_chain_lookup_done(parent);
e4e20f48
MD
465 if (schain == NULL) {
466 kprintf("hammer2_mount: invalid super-root\n");
467 hammer2_vfs_unmount(mp, MNT_FORCE);
468 return EINVAL;
469 }
0dea3156
MD
470 hammer2_chain_ref(schain); /* for hmp->schain */
471 hmp->schain = schain; /* left locked for inode_get */
10252dc7 472 hmp->sroot = hammer2_inode_get(hmp, NULL, NULL, schain);
0dea3156 473 hammer2_inode_ref(hmp->sroot); /* for hmp->sroot */
9596b8c4 474 hammer2_inode_unlock_ex(hmp->sroot, schain);
e4e20f48
MD
475 } else {
476 schain = hmp->schain;
7cfa8da5 477 }
5c23d7f1 478
10252dc7 479 /*
0dea3156
MD
480 * schain only has 1 ref now for its hmp->schain assignment.
481 * Setup for lookup (which will lock it).
10252dc7 482 */
0dea3156 483 parent = hammer2_chain_lookup_init(schain, 0);
e4e20f48 484 lhc = hammer2_dirhash(label, strlen(label));
0dea3156 485 rchain = hammer2_chain_lookup(&parent,
c667909f
MD
486 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
487 0);
7cfa8da5
MD
488 while (rchain) {
489 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
5c23d7f1 490 strcmp(label, rchain->data->ipdata.filename) == 0) {
7cfa8da5
MD
491 break;
492 }
0dea3156 493 rchain = hammer2_chain_next(&parent, rchain,
c667909f
MD
494 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
495 0);
7cfa8da5 496 }
0dea3156 497 hammer2_chain_lookup_done(parent);
7cfa8da5 498 if (rchain == NULL) {
e4e20f48 499 kprintf("hammer2_mount: PFS label not found\n");
9c2e0de0 500 hammer2_vfs_unmount(mp, MNT_FORCE);
7cfa8da5
MD
501 return EINVAL;
502 }
e4e20f48 503 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
0dea3156 504 hammer2_chain_unlock(rchain);
e4e20f48
MD
505 kprintf("hammer2_mount: PFS label already mounted!\n");
506 hammer2_vfs_unmount(mp, MNT_FORCE);
4d5318eb 507 return EBUSY;
e4e20f48 508 }
9596b8c4
MD
509 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
510 kprintf("hammer2_mount: PFS label currently recycling\n");
511 hammer2_vfs_unmount(mp, MNT_FORCE);
512 return EBUSY;
513 }
514
e4e20f48 515 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
5c23d7f1 516
476d2aad
MD
517 /*
518 * NOTE: *_get() integrates chain's lock into the inode lock.
519 */
0dea3156 520 hammer2_chain_ref(rchain); /* for pmp->rchain */
476d2aad 521 pmp->rchain = rchain; /* left held & unlocked */
10252dc7 522 pmp->iroot = hammer2_inode_get(hmp, pmp, NULL, rchain);
476d2aad 523 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
9596b8c4 524 hammer2_inode_unlock_ex(pmp->iroot, rchain);
609a8021 525
e4e20f48 526 kprintf("iroot %p\n", pmp->iroot);
703720e4 527
bfc3a7b1
MD
528 /*
529 * Ref the cluster management messaging descriptor. The mount
530 * program deals with the other end of the communications pipe.
531 */
3a5aa68f
MD
532 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
533 if (fp == NULL) {
bfc3a7b1
MD
534 kprintf("hammer2_mount: bad cluster_fd!\n");
535 hammer2_vfs_unmount(mp, MNT_FORCE);
536 return EBADF;
537 }
3a5aa68f 538 hammer2_cluster_reconnect(pmp, fp);
bfc3a7b1
MD
539
540 /*
541 * Finish setup
542 */
f0206a67
VS
543 vfs_getnewfsid(mp);
544 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
545 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
546 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
703720e4 547
54f522df 548 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
703720e4
MD
549 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
550 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
551 copyinstr(path, mp->mnt_stat.f_mntonname,
552 sizeof(mp->mnt_stat.f_mntonname) - 1,
553 &size);
554
bfc3a7b1
MD
555 /*
556 * Initial statfs to prime mnt_stat.
557 */
066e00cc 558 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
703720e4 559
47902fef 560 return 0;
703720e4
MD
561}
562
e118c14f
MD
563static
564int
703720e4
MD
565hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
566 struct ucred *cred)
567{
568 return (0);
569}
570
e118c14f
MD
571static
572int
066e00cc 573hammer2_vfs_unmount(struct mount *mp, int mntflags)
703720e4 574{
e4e20f48 575 hammer2_pfsmount_t *pmp;
54eb943b 576 hammer2_mount_t *hmp;
703720e4 577 int flags;
50e4f8f4 578 int error = 0;
54eb943b 579 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
a02dfba1 580 int dumpcnt;
54eb943b 581 struct vnode *devvp;
703720e4 582
e4e20f48
MD
583 pmp = MPTOPMP(mp);
584 hmp = pmp->hmp;
703720e4
MD
585 flags = 0;
586
587 if (mntflags & MNT_FORCE)
588 flags |= FORCECLOSE;
589
590 hammer2_mount_exlock(hmp);
591
50e4f8f4
MD
592 /*
593 * If mount initialization proceeded far enough we must flush
594 * its vnodes.
595 */
e4e20f48 596 if (pmp->iroot)
50e4f8f4
MD
597 error = vflush(mp, 0, flags);
598
a864c5d9
MD
599 if (error) {
600 hammer2_mount_unlock(hmp);
50e4f8f4 601 return error;
a864c5d9 602 }
703720e4 603
e4e20f48
MD
604 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
605 --hmp->pmp_count;
606 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
607
703720e4 608 /*
2910a90c
MD
609 * Flush any left over chains. The voldata lock is only used
610 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
222d9e22 611 */
2910a90c
MD
612 hammer2_voldata_lock(hmp);
613 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
222d9e22 614 HAMMER2_CHAIN_SUBMODIFIED)) {
0dea3156 615 hammer2_voldata_unlock(hmp, 0);
222d9e22 616 hammer2_vfs_sync(mp, MNT_WAIT);
2910a90c 617 } else {
0dea3156 618 hammer2_voldata_unlock(hmp, 0);
222d9e22 619 }
e4e20f48
MD
620 if (hmp->pmp_count == 0) {
621 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
e4e20f48
MD
622 HAMMER2_CHAIN_SUBMODIFIED)) {
623 kprintf("hammer2_unmount: chains left over after "
624 "final sync\n");
625 if (hammer2_debug & 0x0010)
626 Debugger("entered debugger");
627 }
222d9e22
MD
628 }
629
630 /*
631 * Cleanup the root and super-root chain elements (which should be
632 * clean).
703720e4 633 */
476d2aad 634 if (pmp->iroot) {
0dea3156
MD
635#if REPORT_REFS_ERRORS
636 if (pmp->iroot->refs != 1)
637 kprintf("PMP->IROOT %p REFS WRONG %d\n",
638 pmp->iroot, pmp->iroot->refs);
639#else
476d2aad 640 KKASSERT(pmp->iroot->refs == 1);
0dea3156 641#endif
10252dc7 642 hammer2_inode_drop(pmp->iroot); /* ref for pmp->iroot */
476d2aad
MD
643 pmp->iroot = NULL;
644 }
e4e20f48
MD
645 if (pmp->rchain) {
646 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
0dea3156
MD
647#if REPORT_REFS_ERRORS
648 if (pmp->rchain->refs != 1)
649 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
650 pmp->rchain, pmp->rchain->refs);
651#else
e4e20f48 652 KKASSERT(pmp->rchain->refs == 1);
0dea3156
MD
653#endif
654 hammer2_chain_drop(pmp->rchain);
e4e20f48 655 pmp->rchain = NULL;
7cfa8da5 656 }
609a8021 657 ccms_domain_uninit(&pmp->ccms_dom);
bfc3a7b1
MD
658
659 /*
185ace93 660 * Kill cluster controller
bfc3a7b1 661 */
185ace93 662 kdmsg_iocom_uninit(&pmp->iocom);
bfc3a7b1
MD
663
664 /*
665 * If no PFS's left drop the master hammer2_mount for the device.
666 */
e4e20f48 667 if (hmp->pmp_count == 0) {
10252dc7
MD
668 if (hmp->sroot) {
669 hammer2_inode_drop(hmp->sroot);
670 hmp->sroot = NULL;
671 }
e4e20f48 672 if (hmp->schain) {
0dea3156
MD
673#if REPORT_REFS_ERRORS
674 if (hmp->schain->refs != 1)
675 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
676 hmp->schain, hmp->schain->refs);
677#else
e4e20f48 678 KKASSERT(hmp->schain->refs == 1);
0dea3156
MD
679#endif
680 hammer2_chain_drop(hmp->schain);
e4e20f48
MD
681 hmp->schain = NULL;
682 }
222d9e22 683
e4e20f48
MD
684 /*
685 * Finish up with the device vnode
686 */
687 if ((devvp = hmp->devvp) != NULL) {
688 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
689 hmp->devvp = NULL;
690 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
691 vrele(devvp);
692 devvp = NULL;
693 }
a864c5d9 694
1a7cfe5a
MD
695 /*
696 * Final drop of embedded freemap root chain to clean up
697 * fchain.core (fchain structure is not flagged ALLOCATED
698 * so it is cleaned out and then left to rot).
699 */
700 hammer2_chain_drop(&hmp->fchain);
701
a864c5d9
MD
702 /*
703 * Final drop of embedded volume root chain to clean up
704 * vchain.core (vchain structure is not flagged ALLOCATED
1a7cfe5a 705 * so it is cleaned out and then left to rot).
a864c5d9 706 */
731b2a84 707 dumpcnt = 50;
a864c5d9 708 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
9596b8c4 709 hammer2_mount_unlock(hmp);
a864c5d9 710 hammer2_chain_drop(&hmp->vchain);
9596b8c4
MD
711 } else {
712 hammer2_mount_unlock(hmp);
54eb943b 713 }
703720e4 714
e4e20f48
MD
715 pmp->mp = NULL;
716 pmp->hmp = NULL;
54eb943b 717 mp->mnt_data = NULL;
703720e4 718
26bf1a36
MD
719 kmalloc_destroy(&pmp->mmsg);
720
e4e20f48
MD
721 kfree(pmp, M_HAMMER2);
722 if (hmp->pmp_count == 0) {
723 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
004f88b4
MD
724 kmalloc_destroy(&hmp->minode);
725 kmalloc_destroy(&hmp->mchain);
e4e20f48
MD
726 kfree(hmp, M_HAMMER2);
727 }
728 lockmgr(&hammer2_mntlk, LK_RELEASE);
703720e4
MD
729 return (error);
730}
731
e118c14f
MD
732static
733int
066e00cc 734hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
703720e4
MD
735 ino_t ino, struct vnode **vpp)
736{
737 kprintf("hammer2_vget\n");
738 return (EOPNOTSUPP);
739}
740
e118c14f
MD
741static
742int
066e00cc 743hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
703720e4 744{
e4e20f48 745 hammer2_pfsmount_t *pmp;
a0ed3c24 746 hammer2_mount_t *hmp;
9596b8c4 747 hammer2_chain_t *parent;
703720e4
MD
748 int error;
749 struct vnode *vp;
750
e4e20f48 751 pmp = MPTOPMP(mp);
a0ed3c24
MD
752 hmp = pmp->hmp;
753 hammer2_mount_exlock(hmp);
e4e20f48 754 if (pmp->iroot == NULL) {
703720e4
MD
755 *vpp = NULL;
756 error = EINVAL;
757 } else {
9596b8c4 758 parent = hammer2_inode_lock_sh(pmp->iroot);
e4e20f48 759 vp = hammer2_igetv(pmp->iroot, &error);
9596b8c4 760 hammer2_inode_unlock_sh(pmp->iroot, parent);
703720e4
MD
761 *vpp = vp;
762 if (vp == NULL)
763 kprintf("vnodefail\n");
764 }
a0ed3c24 765 hammer2_mount_unlock(hmp);
703720e4
MD
766
767 return (error);
768}
769
28ee5f14
MD
770/*
771 * Filesystem status
772 *
476d2aad 773 * XXX incorporate ipdata->inode_quota and data_quota
28ee5f14 774 */
e118c14f
MD
775static
776int
066e00cc 777hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
703720e4 778{
28ee5f14 779 hammer2_pfsmount_t *pmp;
54eb943b 780 hammer2_mount_t *hmp;
703720e4 781
28ee5f14 782 pmp = MPTOPMP(mp);
e4e20f48 783 hmp = MPTOHMP(mp);
703720e4 784
476d2aad 785 mp->mnt_stat.f_files = pmp->inode_count;
28ee5f14
MD
786 mp->mnt_stat.f_ffree = 0;
787 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
788 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
789 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
50e4f8f4 790 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
703720e4 791
50e4f8f4 792 *sbp = mp->mnt_stat;
703720e4
MD
793 return (0);
794}
795
e118c14f
MD
796static
797int
066e00cc 798hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
703720e4 799{
28ee5f14 800 hammer2_pfsmount_t *pmp;
50e4f8f4
MD
801 hammer2_mount_t *hmp;
802
28ee5f14 803 pmp = MPTOPMP(mp);
e4e20f48 804 hmp = MPTOHMP(mp);
50e4f8f4 805
88a032af 806 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
476d2aad 807 mp->mnt_vstat.f_files = pmp->inode_count;
28ee5f14
MD
808 mp->mnt_vstat.f_ffree = 0;
809 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
810 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
811 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
812 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
50e4f8f4
MD
813
814 *sbp = mp->mnt_vstat;
815 return (0);
703720e4
MD
816}
817
818/*
819 * Sync the entire filesystem; this is called from the filesystem syncer
820 * process periodically and whenever a user calls sync(1) on the hammer
821 * mountpoint.
822 *
823 * Currently is actually called from the syncer! \o/
824 *
825 * This task will have to snapshot the state of the dirty inode chain.
826 * From that, it will have to make sure all of the inodes on the dirty
827 * chain have IO initiated. We make sure that io is initiated for the root
828 * block.
829 *
830 * If waitfor is set, we wait for media to acknowledge the new rootblock.
831 *
832 * THINKS: side A vs side B, to have sync not stall all I/O?
833 */
e118c14f
MD
834static
835int
066e00cc 836hammer2_vfs_sync(struct mount *mp, int waitfor)
703720e4 837{
b7926f31 838 struct hammer2_sync_info info;
54eb943b 839 hammer2_mount_t *hmp;
b7926f31
MD
840 int flags;
841 int error;
3fc4c63d 842 int i;
a02dfba1
MD
843#if 0
844 int dumpcnt;
845#endif
703720e4 846
e4e20f48 847 hmp = MPTOHMP(mp);
9797e933 848#if 0
a02dfba1
MD
849 if ((waitfor & MNT_LAZY) == 0) {
850 dumpcnt = 50;
851 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
852 }
9797e933 853#endif
703720e4 854
b7926f31
MD
855 flags = VMSC_GETVP;
856 if (waitfor & MNT_LAZY)
857 flags |= VMSC_ONEPASS;
858
1a7cfe5a 859 hammer2_trans_init(&info.trans, hmp, NULL, HAMMER2_TRANS_ISFLUSH);
9596b8c4 860
b7926f31
MD
861 info.error = 0;
862 info.waitfor = MNT_NOWAIT;
863 vmntvnodescan(mp, flags | VMSC_NOWAIT,
864 hammer2_sync_scan1,
865 hammer2_sync_scan2, &info);
866 if (info.error == 0 && (waitfor & MNT_WAIT)) {
867 info.waitfor = waitfor;
868 vmntvnodescan(mp, flags,
869 hammer2_sync_scan1,
870 hammer2_sync_scan2, &info);
871
872 }
e118c14f 873#if 0
b7926f31
MD
874 if (waitfor == MNT_WAIT) {
875 /* XXX */
876 } else {
877 /* XXX */
878 }
e118c14f 879#endif
1a7cfe5a
MD
880 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
881 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
882 HAMMER2_CHAIN_SUBMODIFIED)) {
883 hammer2_chain_flush(&info.trans, &hmp->vchain);
884 }
885 hammer2_chain_unlock(&hmp->vchain);
886
887#if 0
9596b8c4
MD
888 /*
889 * Rollup flush. The fsyncs above basically just flushed
890 * data blocks. The flush below gets all the meta-data.
891 */
1a7cfe5a
MD
892 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
893 if (hmp->fchain.flags & (HAMMER2_CHAIN_MODIFIED |
2910a90c 894 HAMMER2_CHAIN_SUBMODIFIED)) {
1a7cfe5a
MD
895 /* this will modify vchain as a side effect */
896 hammer2_chain_flush(&info.trans, &hmp->fchain);
73e441b9 897 }
1a7cfe5a
MD
898 hammer2_chain_unlock(&hmp->fchain);
899#endif
900
1c9f601e
MD
901
902 error = 0;
903
3fc4c63d
MD
904 /*
905 * We can't safely flush the volume header until we have
906 * flushed any device buffers which have built up.
9596b8c4
MD
907 *
908 * XXX this isn't being incremental
3fc4c63d 909 */
3fc4c63d
MD
910 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
911 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
912 vn_unlock(hmp->devvp);
1c9f601e 913
0dea3156
MD
914 /*
915 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
916 * volume header needs synchronization via hmp->volsync.
917 *
918 * XXX synchronize the flag & data with only this flush XXX
919 */
920 if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
b7926f31
MD
921 struct buf *bp;
922
2910a90c
MD
923 /*
924 * Synchronize the disk before flushing the volume
925 * header.
926 */
b7926f31
MD
927 bp = getpbuf(NULL);
928 bp->b_bio1.bio_offset = 0;
929 bp->b_bufsize = 0;
930 bp->b_bcount = 0;
931 bp->b_cmd = BUF_CMD_FLUSH;
932 bp->b_bio1.bio_done = biodone_sync;
933 bp->b_bio1.bio_flags |= BIO_SYNC;
934 vn_strategy(hmp->devvp, &bp->b_bio1);
935 biowait(&bp->b_bio1, "h2vol");
936 relpbuf(bp, NULL);
937
2910a90c 938 /*
3fc4c63d
MD
939 * Then we can safely flush the version of the volume header
940 * synchronized by the flush code.
2910a90c 941 */
3fc4c63d
MD
942 i = hmp->volhdrno + 1;
943 if (i >= HAMMER2_NUM_VOLHDRS)
944 i = 0;
945 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
946 hmp->volsync.volu_size) {
947 i = 0;
948 }
949 kprintf("sync volhdr %d %jd\n",
950 i, (intmax_t)hmp->volsync.volu_size);
951 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
952 HAMMER2_PBUFSIZE, 0, 0);
0dea3156 953 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
3fc4c63d 954 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
b7926f31 955 bawrite(bp);
3fc4c63d 956 hmp->volhdrno = i;
b7926f31 957 }
d001f460 958 hammer2_trans_done(&info.trans);
b7926f31
MD
959 return (error);
960}
703720e4 961
214f4a77
MD
962/*
963 * Sync passes.
964 *
965 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
966 * won't flush on those flags. The syncer code above will do a
967 * general meta-data flush globally that will catch these flags.
968 */
b7926f31
MD
969static int
970hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
971{
972 hammer2_inode_t *ip;
973
974 ip = VTOI(vp);
975 if (vp->v_type == VNON || ip == NULL ||
9596b8c4 976 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
b7926f31
MD
977 RB_EMPTY(&vp->v_rbdirty_tree))) {
978 return(-1);
979 }
980 return(0);
981}
982
983static int
984hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
985{
986 struct hammer2_sync_info *info = data;
987 hammer2_inode_t *ip;
9596b8c4 988 hammer2_chain_t *parent;
b7926f31
MD
989 int error;
990
991 ip = VTOI(vp);
992 if (vp->v_type == VNON || vp->v_type == VBAD ||
9596b8c4 993 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
476d2aad 994 RB_EMPTY(&vp->v_rbdirty_tree))) {
b7926f31
MD
995 return(0);
996 }
d001f460
MD
997
998 /*
999 * VOP_FSYNC will start a new transaction so replicate some code
1000 * here to do it inline (see hammer2_vop_fsync()).
1001 */
9596b8c4
MD
1002 parent = hammer2_inode_lock_ex(ip);
1003 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
d001f460
MD
1004 if (ip->vp)
1005 vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
9596b8c4
MD
1006 hammer2_chain_flush(&info->trans, parent);
1007 hammer2_inode_unlock_ex(ip, parent);
d001f460
MD
1008 error = 0;
1009#if 0
b7926f31 1010 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
d001f460 1011#endif
b7926f31
MD
1012 if (error)
1013 info->error = error;
1014 return(0);
703720e4
MD
1015}
1016
e118c14f
MD
1017static
1018int
066e00cc 1019hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
703720e4
MD
1020{
1021 return (0);
1022}
1023
e118c14f
MD
1024static
1025int
066e00cc 1026hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
703720e4
MD
1027 struct fid *fhp, struct vnode **vpp)
1028{
1029 return (0);
1030}
1031
e118c14f
MD
1032static
1033int
066e00cc 1034hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
703720e4
MD
1035 int *exflagsp, struct ucred **credanonp)
1036{
1037 return (0);
1038}
50e4f8f4
MD
1039
1040/*
1041 * Support code for hammer2_mount(). Read, verify, and install the volume
1042 * header into the HMP
1043 *
1044 * XXX read four volhdrs and use the one with the highest TID whos CRC
1045 * matches.
1046 *
1047 * XXX check iCRCs.
57381c9e
VS
1048 *
1049 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1050 * nonexistant locations.
1051 *
1052 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
50e4f8f4
MD
1053 */
1054static
1055int
1056hammer2_install_volume_header(hammer2_mount_t *hmp)
1057{
1058 hammer2_volume_data_t *vd;
99924359 1059 struct buf *bp;
60fbd5f4 1060 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
99924359
MD
1061 int error_reported;
1062 int error;
0b3147ba
VS
1063 int valid;
1064 int i;
50e4f8f4 1065
99924359 1066 error_reported = 0;
0b3147ba
VS
1067 error = 0;
1068 valid = 0;
99924359 1069 bp = NULL;
0b3147ba 1070
99924359
MD
1071 /*
1072 * There are up to 4 copies of the volume header (syncs iterate
1073 * between them so there is no single master). We don't trust the
1074 * volu_size field so we don't know precisely how large the filesystem
1075 * is, so depend on the OS to return an error if we go beyond the
1076 * block device's EOF.
1077 */
0b3147ba 1078 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
62efe6ec 1079 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
99924359 1080 HAMMER2_VOLUME_BYTES, &bp);
0b3147ba 1081 if (error) {
99924359
MD
1082 brelse(bp);
1083 bp = NULL;
0b3147ba
VS
1084 continue;
1085 }
1086
88a032af
VS
1087 vd = (struct hammer2_volume_data *) bp->b_data;
1088 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1089 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1090 brelse(bp);
1091 bp = NULL;
0b3147ba 1092 continue;
88a032af
VS
1093 }
1094
1095 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1096 /* XXX: Reversed-endianness filesystem */
1097 kprintf("hammer2: reverse-endian filesystem detected");
1098 brelse(bp);
1099 bp = NULL;
1100 continue;
1101 }
0b3147ba
VS
1102
1103 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
60fbd5f4 1104 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
99924359 1105 HAMMER2_VOLUME_ICRC0_SIZE);
60fbd5f4
VS
1106 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1107 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1108 HAMMER2_VOLUME_ICRC1_SIZE);
1109 if ((crc0 != crc) || (bcrc0 != bcrc)) {
99924359 1110 kprintf("hammer2 volume header crc "
3fc4c63d 1111 "mismatch copy #%d %08x/%08x\n",
60fbd5f4 1112 i, crc0, crc);
99924359
MD
1113 error_reported = 1;
1114 brelse(bp);
1115 bp = NULL;
0b3147ba
VS
1116 continue;
1117 }
4d5318eb 1118 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
99924359
MD
1119 valid = 1;
1120 hmp->voldata = *vd;
3fc4c63d 1121 hmp->volhdrno = i;
0b3147ba 1122 }
99924359
MD
1123 brelse(bp);
1124 bp = NULL;
50e4f8f4 1125 }
0b3147ba 1126 if (valid) {
3fc4c63d 1127 hmp->volsync = hmp->voldata;
57381c9e 1128 error = 0;
3fc4c63d
MD
1129 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1130 kprintf("hammer2: using volume header #%d\n",
1131 hmp->volhdrno);
1132 }
0b3147ba
VS
1133 } else {
1134 error = EINVAL;
99924359 1135 kprintf("hammer2: no valid volume headers found!\n");
0b3147ba 1136 }
0b3147ba 1137 return (error);
50e4f8f4 1138}
0b3147ba 1139
1a34728c
MD
1140/*
1141 * Reconnect using the passed file pointer. The caller must ref the
1142 * fp for us.
1143 */
1144void
1145hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1146{
476d2aad 1147 hammer2_inode_data_t *ipdata;
9596b8c4 1148 hammer2_chain_t *parent;
3a5aa68f 1149 size_t name_len;
70c3c3b7
MD
1150
1151 /*
3a5aa68f
MD
1152 * Closes old comm descriptor, kills threads, cleans up
1153 * states, then installs the new descriptor and creates
1154 * new threads.
1a34728c 1155 */
3a5aa68f 1156 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
bfc3a7b1 1157
9b8b748f 1158 /*
537d97bc
MD
1159 * Setup LNK_CONN fields for autoinitiated state machine
1160 */
9596b8c4
MD
1161 parent = hammer2_inode_lock_ex(pmp->iroot);
1162 ipdata = &parent->data->ipdata;
476d2aad
MD
1163 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1164 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1165 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
537d97bc
MD
1166 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1167 pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1168
1169 /*
1170 * Filter adjustment. Clients do not need visibility into other
1171 * clients (otherwise millions of clients would present a serious
1172 * problem). The fs_label also serves to restrict the namespace.
9b8b748f 1173 */
537d97bc
MD
1174 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1175 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
476d2aad 1176 switch (ipdata->pfs_type) {
537d97bc
MD
1177 case DMSG_PFSTYPE_CLIENT:
1178 pmp->iocom.auto_lnk_conn.peer_mask &=
1179 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1180 break;
1181 default:
1182 break;
1183 }
1184
476d2aad 1185 name_len = ipdata->name_len;
537d97bc
MD
1186 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1187 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
476d2aad 1188 bcopy(ipdata->filename,
537d97bc 1189 pmp->iocom.auto_lnk_conn.fs_label,
ddfbb283 1190 name_len);
537d97bc
MD
1191 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1192
1193 /*
1194 * Setup LNK_SPAN fields for autoinitiated state machine
1195 */
476d2aad
MD
1196 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1197 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1198 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
537d97bc
MD
1199 pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1200 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
476d2aad 1201 name_len = ipdata->name_len;
537d97bc
MD
1202 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1203 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
476d2aad 1204 bcopy(ipdata->filename,
537d97bc
MD
1205 pmp->iocom.auto_lnk_span.fs_label,
1206 name_len);
1207 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
9596b8c4 1208 hammer2_inode_unlock_ex(pmp->iroot, parent);
537d97bc
MD
1209
1210 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
70c3c3b7
MD
1211}
1212
9b8b748f 1213static int
537d97bc 1214hammer2_rcvdmsg(kdmsg_msg_t *msg)
9b8b748f 1215{
5bc5bca2 1216 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
537d97bc 1217 case DMSG_DBG_SHELL:
10c86c4e 1218 /*
ea7c725f 1219 * (non-transaction)
537d97bc 1220 * Execute shell command (not supported atm)
10c86c4e 1221 */
537d97bc 1222 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
8c280d5d 1223 break;
537d97bc 1224 case DMSG_DBG_SHELL | DMSGF_REPLY:
ea7c725f
MD
1225 /*
1226 * (non-transaction)
1227 */
537d97bc
MD
1228 if (msg->aux_data) {
1229 msg->aux_data[msg->aux_size - 1] = 0;
1230 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1231 }
8c280d5d
MD
1232 break;
1233 default:
3b76886b 1234 /*
ea7c725f 1235 * Unsupported message received. We only need to
3b76886b
MD
1236 * reply if it's a transaction in order to close our end.
1237 * Ignore any one-way messages are any further messages
1238 * associated with the transaction.
1239 *
1240 * NOTE: This case also includes DMSG_LNK_ERROR messages
1241 * which might be one-way, replying to those would
1242 * cause an infinite ping-pong.
1243 */
1244 if (msg->any.head.cmd & DMSGF_CREATE)
1245 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
8c280d5d
MD
1246 break;
1247 }
1248 return(0);
1249}
1250
1251/*
537d97bc
MD
1252 * This function is called after KDMSG has automatically handled processing
1253 * of a LNK layer message (typically CONN, SPAN, or CIRC).
8c280d5d 1254 *
537d97bc
MD
1255 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1256 * advertises all available hammer2 super-root volumes.
8c280d5d 1257 */
537d97bc
MD
1258static void
1259hammer2_autodmsg(kdmsg_msg_t *msg)
8c280d5d 1260{
537d97bc 1261 hammer2_pfsmount_t *pmp = msg->iocom->handle;
1a34728c 1262 hammer2_mount_t *hmp = pmp->hmp;
1a34728c 1263 int copyid;
8c280d5d 1264
537d97bc
MD
1265 /*
1266 * We only care about replies to our LNK_CONN auto-request. kdmsg
1267 * has already processed the reply, we use this calback as a shim
1268 * to know when we can advertise available super-root volumes.
1269 */
1270 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1271 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1272 msg->state == NULL) {
1273 return;
1274 }
1275
70c3c3b7
MD
1276 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1277
5bc5bca2 1278 if (msg->any.head.cmd & DMSGF_CREATE) {
537d97bc 1279 kprintf("HAMMER2: VOLDATA DUMP\n");
1a34728c
MD
1280
1281 /*
1282 * Dump the configuration stored in the volume header
1283 */
1284 hammer2_voldata_lock(hmp);
1285 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1286 if (hmp->voldata.copyinfo[copyid].copyid == 0)
1287 continue;
1288 hammer2_volconf_update(pmp, copyid);
1289 }
0dea3156 1290 hammer2_voldata_unlock(hmp, 0);
8c280d5d 1291 }
537d97bc
MD
1292 if ((msg->any.head.cmd & DMSGF_DELETE) &&
1293 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1294 kprintf("HAMMER2: CONN WAS TERMINATED\n");
8c280d5d 1295 }
9b8b748f 1296}
1a34728c
MD
1297
1298/*
1299 * Volume configuration updates are passed onto the userland service
1300 * daemon via the open LNK_CONN transaction.
1301 */
1302void
1303hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1304{
1305 hammer2_mount_t *hmp = pmp->hmp;
3a5aa68f 1306 kdmsg_msg_t *msg;
1a34728c
MD
1307
1308 /* XXX interlock against connection state termination */
3a5aa68f
MD
1309 kprintf("volconf update %p\n", pmp->iocom.conn_state);
1310 if (pmp->iocom.conn_state) {
1a34728c 1311 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
ea7c725f
MD
1312 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1313 DMSG_LNK_VOLCONF, NULL, NULL);
1a34728c
MD
1314 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1315 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1316 msg->any.lnk_volconf.index = index;
3a5aa68f 1317 kdmsg_msg_write(msg);
1a34728c
MD
1318 }
1319}
9797e933
MD
1320
1321void
a02dfba1 1322hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
9797e933
MD
1323{
1324 hammer2_chain_t *scan;
1325
a02dfba1
MD
1326 --*countp;
1327 if (*countp == 0) {
1328 kprintf("%*.*s...\n", tab, tab, "");
1329 return;
1330 }
1331 if (*countp < 0)
1332 return;
8853dfb5 1333 kprintf("%*.*schain[%d] %p.%d [%08x][core=%p] (%s) dl=%p dt=%s refs=%d",
9797e933
MD
1334 tab, tab, "",
1335 chain->index, chain, chain->bref.type, chain->flags,
1336 chain->core,
1337 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1338 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
8853dfb5
MD
1339 chain->next_parent,
1340 (chain->delete_tid == HAMMER2_MAX_TID ? "max" : "fls"),
1341 chain->refs);
9797e933
MD
1342 if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
1343 kprintf("\n");
1344 else
1345 kprintf(" {\n");
1346 RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree) {
a02dfba1 1347 hammer2_dump_chain(scan, tab + 4, countp);
9797e933
MD
1348 }
1349 if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
1350 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
1351 kprintf("%*.*s}(%s)\n", tab, tab, "",
1352 chain->data->ipdata.filename);
1353 else
1354 kprintf("%*.*s}\n", tab, tab, "");
1355 }
1356}