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