hammer2 - hardlink stabilization pass
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
CommitLineData
47902fef 1/*-
703720e4
MD
2 * Copyright (c) 2011, 2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
703720e4
MD
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/nlookup.h>
38#include <sys/vnode.h>
39#include <sys/mount.h>
40#include <sys/fcntl.h>
41#include <sys/buf.h>
42#include <sys/uuid.h>
a74bc66c 43#include <sys/vfsops.h>
37aa19df 44#include <sys/sysctl.h>
703720e4
MD
45
46#include "hammer2.h"
47#include "hammer2_disk.h"
48#include "hammer2_mount.h"
49
b7926f31
MD
50struct hammer2_sync_info {
51 int error;
52 int waitfor;
53};
54
e4e20f48
MD
55TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
56static struct hammer2_mntlist hammer2_mntlist;
57static struct lock hammer2_mntlk;
58
37aa19df 59int hammer2_debug;
01eabad4 60int hammer2_cluster_enable = 1;
99535653 61int hammer2_hardlink_enable = 1;
01eabad4
MD
62long hammer2_iod_file_read;
63long hammer2_iod_meta_read;
64long hammer2_iod_indr_read;
65long hammer2_iod_file_write;
66long hammer2_iod_meta_write;
67long hammer2_iod_indr_write;
68long hammer2_iod_volu_write;
69long hammer2_ioa_file_read;
70long hammer2_ioa_meta_read;
71long hammer2_ioa_indr_read;
72long hammer2_ioa_file_write;
73long hammer2_ioa_meta_write;
74long hammer2_ioa_indr_write;
75long hammer2_ioa_volu_write;
37aa19df
MD
76
77SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
78
79SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
80 &hammer2_debug, 0, "");
01eabad4
MD
81SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
82 &hammer2_cluster_enable, 0, "");
e708f8b9
MD
83SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
84 &hammer2_hardlink_enable, 0, "");
01eabad4
MD
85SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
86 &hammer2_iod_file_read, 0, "");
87SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
88 &hammer2_iod_meta_read, 0, "");
89SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
90 &hammer2_iod_indr_read, 0, "");
91SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
92 &hammer2_iod_file_write, 0, "");
93SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
94 &hammer2_iod_meta_write, 0, "");
95SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
96 &hammer2_iod_indr_write, 0, "");
97SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
98 &hammer2_iod_volu_write, 0, "");
99SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
100 &hammer2_ioa_file_read, 0, "");
101SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
102 &hammer2_ioa_meta_read, 0, "");
103SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
104 &hammer2_ioa_indr_read, 0, "");
105SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
106 &hammer2_ioa_file_write, 0, "");
107SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
108 &hammer2_ioa_meta_write, 0, "");
109SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
110 &hammer2_ioa_indr_write, 0, "");
111SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
112 &hammer2_ioa_volu_write, 0, "");
37aa19df 113
b7926f31
MD
114static int hammer2_vfs_init(struct vfsconf *conf);
115static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
116 struct ucred *cred);
117static int hammer2_remount(struct mount *, char *, struct vnode *,
703720e4 118 struct ucred *);
b7926f31
MD
119static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
120static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
121static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
703720e4 122 struct ucred *cred);
b7926f31
MD
123static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
124 struct ucred *cred);
125static int hammer2_vfs_sync(struct mount *mp, int waitfor);
126static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
127 ino_t ino, struct vnode **vpp);
128static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
129 struct fid *fhp, struct vnode **vpp);
130static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
131static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
132 int *exflagsp, struct ucred **credanonp);
133
134static int hammer2_install_volume_header(hammer2_mount_t *hmp);
135static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
136static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
50e4f8f4 137
703720e4
MD
138/*
139 * HAMMER2 vfs operations.
140 */
141static struct vfsops hammer2_vfsops = {
066e00cc
MD
142 .vfs_init = hammer2_vfs_init,
143 .vfs_sync = hammer2_vfs_sync,
144 .vfs_mount = hammer2_vfs_mount,
145 .vfs_unmount = hammer2_vfs_unmount,
146 .vfs_root = hammer2_vfs_root,
147 .vfs_statfs = hammer2_vfs_statfs,
148 .vfs_statvfs = hammer2_vfs_statvfs,
149 .vfs_vget = hammer2_vfs_vget,
150 .vfs_vptofh = hammer2_vfs_vptofh,
151 .vfs_fhtovp = hammer2_vfs_fhtovp,
152 .vfs_checkexp = hammer2_vfs_checkexp
703720e4
MD
153};
154
703720e4
MD
155MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
156
157VFS_SET(hammer2_vfsops, hammer2, 0);
158MODULE_VERSION(hammer2, 1);
159
e118c14f
MD
160static
161int
066e00cc 162hammer2_vfs_init(struct vfsconf *conf)
703720e4
MD
163{
164 int error;
165
166 error = 0;
167
0e92b724 168 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
703720e4 169 error = EINVAL;
0e92b724 170 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
703720e4 171 error = EINVAL;
0e92b724 172 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
703720e4 173 error = EINVAL;
0e92b724 174 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
703720e4
MD
175 error = EINVAL;
176
177 if (error)
178 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
179
e4e20f48
MD
180 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
181 TAILQ_INIT(&hammer2_mntlist);
182
703720e4
MD
183 return (error);
184}
185
186/*
187 * Mount or remount HAMMER2 fileystem from physical media
188 *
189 * mountroot
190 * mp mount point structure
191 * path NULL
192 * data <unused>
193 * cred <unused>
194 *
195 * mount
196 * mp mount point structure
197 * path path to mount point
198 * data pointer to argument structure in user space
199 * volume volume path (device@LABEL form)
200 * hflags user mount flags
201 * cred user credentials
202 *
203 * RETURNS: 0 Success
204 * !0 error number
205 */
e118c14f
MD
206static
207int
066e00cc 208hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
703720e4
MD
209 struct ucred *cred)
210{
211 struct hammer2_mount_info info;
e4e20f48 212 hammer2_pfsmount_t *pmp;
54eb943b 213 hammer2_mount_t *hmp;
7cfa8da5 214 hammer2_key_t lhc;
703720e4
MD
215 struct vnode *devvp;
216 struct nlookupdata nd;
5c23d7f1 217 hammer2_chain_t *parent;
7cfa8da5
MD
218 hammer2_chain_t *schain;
219 hammer2_chain_t *rchain;
703720e4
MD
220 char devstr[MNAMELEN];
221 size_t size;
222 size_t done;
50e4f8f4
MD
223 char *dev;
224 char *label;
54eb943b 225 int ronly = 1;
e4e20f48 226 int create_hmp;
703720e4 227 int error;
703720e4
MD
228
229 hmp = NULL;
e4e20f48 230 pmp = NULL;
50e4f8f4
MD
231 dev = NULL;
232 label = NULL;
703720e4
MD
233 devvp = NULL;
234
235 kprintf("hammer2_mount\n");
236
237 if (path == NULL) {
238 /*
239 * Root mount
240 */
703720e4
MD
241 return (EOPNOTSUPP);
242 } else {
243 /*
244 * Non-root mount or updating a mount
245 */
703720e4
MD
246 error = copyin(data, &info, sizeof(info));
247 if (error)
248 return (error);
249
250 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
251 if (error)
252 return (error);
253
254 /* Extract device and label */
255 dev = devstr;
256 label = strchr(devstr, '@');
257 if (label == NULL ||
50e4f8f4 258 ((label + 1) - dev) > done) {
703720e4 259 return (EINVAL);
50e4f8f4 260 }
703720e4
MD
261 *label = '\0';
262 label++;
263 if (*label == '\0')
264 return (EINVAL);
265
266 if (mp->mnt_flag & MNT_UPDATE) {
267 /* Update mount */
268 /* HAMMER2 implements NFS export via mountctl */
e4e20f48 269 hmp = MPTOHMP(mp);
54eb943b
MD
270 devvp = hmp->devvp;
271 error = hammer2_remount(mp, path, devvp, cred);
272 return error;
703720e4
MD
273 }
274 }
275
276 /*
277 * New non-root mount
278 */
279 /* Lookup name and verify it refers to a block device */
280 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
54eb943b
MD
281 if (error == 0)
282 error = nlookup(&nd);
283 if (error == 0)
284 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
703720e4
MD
285 nlookup_done(&nd);
286
54eb943b
MD
287 if (error == 0) {
288 if (vn_isdisk(devvp, &error))
289 error = vfs_mountedon(devvp);
703720e4
MD
290 }
291
292 /*
e4e20f48
MD
293 * Determine if the device has already been mounted. After this
294 * check hmp will be non-NULL if we are doing the second or more
295 * hammer2 mounts from the same device.
703720e4 296 */
e4e20f48
MD
297 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
298 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
299 if (hmp->devvp == devvp)
300 break;
301 }
302
303 /*
304 * Open the device if this isn't a secondary mount
305 */
306 if (hmp) {
307 create_hmp = 0;
308 } else {
309 create_hmp = 1;
310 if (error == 0 && vcount(devvp) > 0)
311 error = EBUSY;
312
313 /*
314 * Now open the device
315 */
54eb943b 316 if (error == 0) {
e4e20f48
MD
317 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
318 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
319 error = vinvalbuf(devvp, V_SAVE, 0, 0);
320 if (error == 0) {
321 error = VOP_OPEN(devvp,
322 ronly ? FREAD : FREAD | FWRITE,
323 FSCRED, NULL);
324 }
325 vn_unlock(devvp);
326 }
327 if (error && devvp) {
328 vrele(devvp);
329 devvp = NULL;
330 }
331 if (error) {
332 lockmgr(&hammer2_mntlk, LK_RELEASE);
333 return error;
54eb943b 334 }
703720e4
MD
335 }
336
337 /*
54eb943b
MD
338 * Block device opened successfully, finish initializing the
339 * mount structure.
340 *
341 * From this point on we have to call hammer2_unmount() on failure.
703720e4 342 */
e4e20f48
MD
343 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
344 mp->mnt_data = (qaddr_t)pmp;
345 pmp->mp = mp;
346
347 if (create_hmp) {
348 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
349 hmp->ronly = ronly;
350 hmp->devvp = devvp;
351 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
352 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
353 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
354 }
355 pmp->hmp = hmp;
356 ++hmp->pmp_count;
357 lockmgr(&hammer2_mntlk, LK_RELEASE);
358 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
74d91941 359
f0206a67 360 mp->mnt_flag = MNT_LOCAL;
50e4f8f4
MD
361 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
362
e4e20f48
MD
363 if (create_hmp) {
364 /*
365 * vchain setup. vchain.data is special cased to NULL.
366 * vchain.refs is initialized and will never drop to 0.
367 */
368 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
369 hmp->vchain.refs = 1;
370 hmp->vchain.data = (void *)&hmp->voldata;
371 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
372 /* hmp->vchain.u.xxx is left NULL */
373 lockinit(&hmp->vchain.lk, "volume", 0, LK_CANRECURSE);
374 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
375 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
232a50f9 376
e4e20f48
MD
377 /*
378 * Install the volume header
379 */
380 error = hammer2_install_volume_header(hmp);
381 if (error) {
382 hammer2_vfs_unmount(mp, MNT_FORCE);
383 return error;
384 }
50e4f8f4 385 }
703720e4
MD
386
387 /*
50e4f8f4 388 * required mount structure initializations
703720e4 389 */
50e4f8f4
MD
390 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
391 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
392
393 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
394 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
395
2910a90c
MD
396 /*
397 * Optional fields
398 */
399 mp->mnt_iosize_max = MAXPHYS;
400
50e4f8f4 401 /*
7cfa8da5
MD
402 * First locate the super-root inode, which is key 0 relative to the
403 * volume header's blockset.
404 *
405 * Then locate the root inode by scanning the directory keyspace
406 * represented by the label.
50e4f8f4 407 */
e4e20f48
MD
408 if (create_hmp) {
409 parent = &hmp->vchain;
410 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
411 schain = hammer2_chain_lookup(hmp, &parent,
c667909f 412 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
e4e20f48
MD
413 hammer2_chain_unlock(hmp, parent);
414 if (schain == NULL) {
415 kprintf("hammer2_mount: invalid super-root\n");
416 hammer2_vfs_unmount(mp, MNT_FORCE);
417 return EINVAL;
418 }
419 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
420 hmp->schain = schain; /* left locked */
421 } else {
422 schain = hmp->schain;
423 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
7cfa8da5 424 }
5c23d7f1
MD
425
426 parent = schain;
e4e20f48 427 lhc = hammer2_dirhash(label, strlen(label));
5c23d7f1 428 rchain = hammer2_chain_lookup(hmp, &parent,
c667909f
MD
429 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
430 0);
7cfa8da5
MD
431 while (rchain) {
432 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
433 rchain->u.ip &&
5c23d7f1 434 strcmp(label, rchain->data->ipdata.filename) == 0) {
7cfa8da5
MD
435 break;
436 }
5c23d7f1 437 rchain = hammer2_chain_next(hmp, &parent, rchain,
c667909f
MD
438 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
439 0);
7cfa8da5 440 }
01eabad4 441 hammer2_chain_unlock(hmp, parent);
7cfa8da5 442 if (rchain == NULL) {
e4e20f48 443 kprintf("hammer2_mount: PFS label not found\n");
9c2e0de0 444 hammer2_vfs_unmount(mp, MNT_FORCE);
7cfa8da5
MD
445 return EINVAL;
446 }
e4e20f48
MD
447 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
448 hammer2_chain_unlock(hmp, rchain);
449 kprintf("hammer2_mount: PFS label already mounted!\n");
450 hammer2_vfs_unmount(mp, MNT_FORCE);
4d5318eb 451 return EBUSY;
e4e20f48
MD
452 }
453 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
5c23d7f1 454
e4e20f48
MD
455 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
456 hammer2_chain_unlock(hmp, rchain);
457 pmp->rchain = rchain; /* left held & unlocked */
458 pmp->iroot = rchain->u.ip; /* implied hold from rchain */
459 pmp->iroot->pmp = pmp;
460 kprintf("iroot %p\n", pmp->iroot);
703720e4 461
f0206a67
VS
462 vfs_getnewfsid(mp);
463 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
464 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
465 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
703720e4 466
54f522df 467 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
703720e4
MD
468 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
469 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
470 copyinstr(path, mp->mnt_stat.f_mntonname,
471 sizeof(mp->mnt_stat.f_mntonname) - 1,
472 &size);
473
066e00cc 474 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
703720e4 475
47902fef 476 return 0;
703720e4
MD
477}
478
e118c14f
MD
479static
480int
703720e4
MD
481hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
482 struct ucred *cred)
483{
484 return (0);
485}
486
e118c14f
MD
487static
488int
066e00cc 489hammer2_vfs_unmount(struct mount *mp, int mntflags)
703720e4 490{
e4e20f48 491 hammer2_pfsmount_t *pmp;
54eb943b 492 hammer2_mount_t *hmp;
703720e4 493 int flags;
50e4f8f4 494 int error = 0;
54eb943b
MD
495 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
496 struct vnode *devvp;
703720e4 497
e4e20f48
MD
498 pmp = MPTOPMP(mp);
499 hmp = pmp->hmp;
703720e4
MD
500 flags = 0;
501
502 if (mntflags & MNT_FORCE)
503 flags |= FORCECLOSE;
504
505 hammer2_mount_exlock(hmp);
506
50e4f8f4
MD
507 /*
508 * If mount initialization proceeded far enough we must flush
509 * its vnodes.
510 */
e4e20f48 511 if (pmp->iroot)
50e4f8f4
MD
512 error = vflush(mp, 0, flags);
513
514 if (error)
515 return error;
703720e4 516
e4e20f48
MD
517 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
518 --hmp->pmp_count;
519 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
520
703720e4 521 /*
2910a90c
MD
522 * Flush any left over chains. The voldata lock is only used
523 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
222d9e22 524 */
2910a90c
MD
525 hammer2_voldata_lock(hmp);
526 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
527 HAMMER2_CHAIN_MODIFIED_AUX |
222d9e22 528 HAMMER2_CHAIN_SUBMODIFIED)) {
2910a90c 529 hammer2_voldata_unlock(hmp);
222d9e22 530 hammer2_vfs_sync(mp, MNT_WAIT);
2910a90c
MD
531 } else {
532 hammer2_voldata_unlock(hmp);
222d9e22 533 }
e4e20f48
MD
534 if (hmp->pmp_count == 0) {
535 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
536 HAMMER2_CHAIN_MODIFIED_AUX |
537 HAMMER2_CHAIN_SUBMODIFIED)) {
538 kprintf("hammer2_unmount: chains left over after "
539 "final sync\n");
540 if (hammer2_debug & 0x0010)
541 Debugger("entered debugger");
542 }
222d9e22
MD
543 }
544
545 /*
546 * Cleanup the root and super-root chain elements (which should be
547 * clean).
703720e4 548 */
e4e20f48
MD
549 pmp->iroot = NULL;
550 if (pmp->rchain) {
551 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
552 KKASSERT(pmp->rchain->refs == 1);
553 hammer2_chain_drop(hmp, pmp->rchain);
554 pmp->rchain = NULL;
7cfa8da5 555 }
e4e20f48
MD
556 if (hmp->pmp_count == 0) {
557 if (hmp->schain) {
558 KKASSERT(hmp->schain->refs == 1);
559 hammer2_chain_drop(hmp, hmp->schain);
560 hmp->schain = NULL;
561 }
222d9e22 562
e4e20f48
MD
563 /*
564 * Finish up with the device vnode
565 */
566 if ((devvp = hmp->devvp) != NULL) {
567 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
568 hmp->devvp = NULL;
569 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
570 vrele(devvp);
571 devvp = NULL;
572 }
573 kmalloc_destroy(&hmp->minode);
574 kmalloc_destroy(&hmp->mchain);
54eb943b 575 }
54eb943b 576 hammer2_mount_unlock(hmp);
703720e4 577
e4e20f48
MD
578 pmp->mp = NULL;
579 pmp->hmp = NULL;
54eb943b 580 mp->mnt_data = NULL;
703720e4 581
e4e20f48
MD
582 kfree(pmp, M_HAMMER2);
583 if (hmp->pmp_count == 0) {
584 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
585 kfree(hmp, M_HAMMER2);
586 }
587 lockmgr(&hammer2_mntlk, LK_RELEASE);
703720e4
MD
588 return (error);
589}
590
e118c14f
MD
591static
592int
066e00cc 593hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
703720e4
MD
594 ino_t ino, struct vnode **vpp)
595{
596 kprintf("hammer2_vget\n");
597 return (EOPNOTSUPP);
598}
599
e118c14f
MD
600static
601int
066e00cc 602hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
703720e4 603{
e4e20f48 604 hammer2_pfsmount_t *pmp;
703720e4
MD
605 int error;
606 struct vnode *vp;
607
e4e20f48
MD
608 pmp = MPTOPMP(mp);
609 hammer2_mount_exlock(pmp->hmp);
610 if (pmp->iroot == NULL) {
703720e4
MD
611 *vpp = NULL;
612 error = EINVAL;
613 } else {
e4e20f48 614 vp = hammer2_igetv(pmp->iroot, &error);
703720e4
MD
615 *vpp = vp;
616 if (vp == NULL)
617 kprintf("vnodefail\n");
618 }
e4e20f48 619 hammer2_mount_unlock(pmp->hmp);
703720e4
MD
620
621 return (error);
622}
623
e118c14f
MD
624static
625int
066e00cc 626hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
703720e4 627{
54eb943b 628 hammer2_mount_t *hmp;
703720e4 629
e4e20f48 630 hmp = MPTOHMP(mp);
703720e4 631
50e4f8f4
MD
632 mp->mnt_stat.f_files = 10;
633 mp->mnt_stat.f_bfree = 10;
634 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
703720e4 635
50e4f8f4 636 *sbp = mp->mnt_stat;
703720e4
MD
637 return (0);
638}
639
e118c14f
MD
640static
641int
066e00cc 642hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
703720e4 643{
50e4f8f4
MD
644 hammer2_mount_t *hmp;
645
e4e20f48 646 hmp = MPTOHMP(mp);
50e4f8f4 647
88a032af
VS
648 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
649 mp->mnt_vstat.f_files = 0;
50e4f8f4
MD
650 mp->mnt_vstat.f_bavail = mp->mnt_stat.f_bfree;
651
652 *sbp = mp->mnt_vstat;
653 return (0);
703720e4
MD
654}
655
656/*
657 * Sync the entire filesystem; this is called from the filesystem syncer
658 * process periodically and whenever a user calls sync(1) on the hammer
659 * mountpoint.
660 *
661 * Currently is actually called from the syncer! \o/
662 *
663 * This task will have to snapshot the state of the dirty inode chain.
664 * From that, it will have to make sure all of the inodes on the dirty
665 * chain have IO initiated. We make sure that io is initiated for the root
666 * block.
667 *
668 * If waitfor is set, we wait for media to acknowledge the new rootblock.
669 *
670 * THINKS: side A vs side B, to have sync not stall all I/O?
671 */
e118c14f
MD
672static
673int
066e00cc 674hammer2_vfs_sync(struct mount *mp, int waitfor)
703720e4 675{
b7926f31 676 struct hammer2_sync_info info;
54eb943b 677 hammer2_mount_t *hmp;
b7926f31
MD
678 int flags;
679 int error;
73e441b9 680 int haswork;
703720e4 681
e4e20f48 682 hmp = MPTOHMP(mp);
703720e4 683
b7926f31
MD
684 flags = VMSC_GETVP;
685 if (waitfor & MNT_LAZY)
686 flags |= VMSC_ONEPASS;
687
688 info.error = 0;
689 info.waitfor = MNT_NOWAIT;
690 vmntvnodescan(mp, flags | VMSC_NOWAIT,
691 hammer2_sync_scan1,
692 hammer2_sync_scan2, &info);
693 if (info.error == 0 && (waitfor & MNT_WAIT)) {
694 info.waitfor = waitfor;
695 vmntvnodescan(mp, flags,
696 hammer2_sync_scan1,
697 hammer2_sync_scan2, &info);
698
699 }
e118c14f 700#if 0
b7926f31
MD
701 if (waitfor == MNT_WAIT) {
702 /* XXX */
703 } else {
704 /* XXX */
705 }
e118c14f 706#endif
01eabad4 707 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2910a90c
MD
708 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
709 HAMMER2_CHAIN_MODIFIED_AUX |
710 HAMMER2_CHAIN_SUBMODIFIED)) {
4d5318eb 711 hammer2_chain_flush(hmp, &hmp->vchain, 0);
73e441b9
MD
712 haswork = 1;
713 } else {
714 haswork = 0;
715 }
b7926f31 716 hammer2_chain_unlock(hmp, &hmp->vchain);
1c9f601e
MD
717
718 error = 0;
719
720 if ((waitfor & MNT_LAZY) == 0) {
721 waitfor = MNT_NOWAIT;
722 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
723 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
724 vn_unlock(hmp->devvp);
725 }
726
73e441b9 727 if (error == 0 && haswork) {
b7926f31
MD
728 struct buf *bp;
729
2910a90c
MD
730 /*
731 * Synchronize the disk before flushing the volume
732 * header.
733 */
b7926f31
MD
734 bp = getpbuf(NULL);
735 bp->b_bio1.bio_offset = 0;
736 bp->b_bufsize = 0;
737 bp->b_bcount = 0;
738 bp->b_cmd = BUF_CMD_FLUSH;
739 bp->b_bio1.bio_done = biodone_sync;
740 bp->b_bio1.bio_flags |= BIO_SYNC;
741 vn_strategy(hmp->devvp, &bp->b_bio1);
742 biowait(&bp->b_bio1, "h2vol");
743 relpbuf(bp, NULL);
744
2910a90c
MD
745 /*
746 * Then we can safely flush the volume header. Volume
747 * data is locked separately to prevent ioctl functions
748 * from deadlocking due to a configuration issue.
749 */
b7926f31 750 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
2910a90c 751 hammer2_voldata_lock(hmp);
b7926f31 752 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
2910a90c 753 hammer2_voldata_unlock(hmp);
b7926f31
MD
754 bawrite(bp);
755 }
756 return (error);
757}
703720e4 758
214f4a77
MD
759/*
760 * Sync passes.
761 *
762 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
763 * won't flush on those flags. The syncer code above will do a
764 * general meta-data flush globally that will catch these flags.
765 */
b7926f31
MD
766static int
767hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
768{
769 hammer2_inode_t *ip;
770
771 ip = VTOI(vp);
772 if (vp->v_type == VNON || ip == NULL ||
2910a90c 773 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
214f4a77 774 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
b7926f31
MD
775 RB_EMPTY(&vp->v_rbdirty_tree))) {
776 return(-1);
777 }
778 return(0);
779}
780
781static int
782hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
783{
784 struct hammer2_sync_info *info = data;
785 hammer2_inode_t *ip;
786 int error;
787
788 ip = VTOI(vp);
789 if (vp->v_type == VNON || vp->v_type == VBAD ||
2910a90c 790 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
214f4a77
MD
791 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
792 RB_EMPTY(&vp->v_rbdirty_tree))) {
b7926f31
MD
793 return(0);
794 }
795 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
796 if (error)
797 info->error = error;
798 return(0);
703720e4
MD
799}
800
e118c14f
MD
801static
802int
066e00cc 803hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
703720e4
MD
804{
805 return (0);
806}
807
e118c14f
MD
808static
809int
066e00cc 810hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
703720e4
MD
811 struct fid *fhp, struct vnode **vpp)
812{
813 return (0);
814}
815
e118c14f
MD
816static
817int
066e00cc 818hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
703720e4
MD
819 int *exflagsp, struct ucred **credanonp)
820{
821 return (0);
822}
50e4f8f4
MD
823
824/*
825 * Support code for hammer2_mount(). Read, verify, and install the volume
826 * header into the HMP
827 *
828 * XXX read four volhdrs and use the one with the highest TID whos CRC
829 * matches.
830 *
831 * XXX check iCRCs.
57381c9e
VS
832 *
833 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
834 * nonexistant locations.
835 *
836 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
50e4f8f4
MD
837 */
838static
839int
840hammer2_install_volume_header(hammer2_mount_t *hmp)
841{
842 hammer2_volume_data_t *vd;
99924359 843 struct buf *bp;
60fbd5f4 844 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
99924359
MD
845 int error_reported;
846 int error;
0b3147ba
VS
847 int valid;
848 int i;
50e4f8f4 849
99924359 850 error_reported = 0;
0b3147ba
VS
851 error = 0;
852 valid = 0;
99924359 853 bp = NULL;
0b3147ba 854
99924359
MD
855 /*
856 * There are up to 4 copies of the volume header (syncs iterate
857 * between them so there is no single master). We don't trust the
858 * volu_size field so we don't know precisely how large the filesystem
859 * is, so depend on the OS to return an error if we go beyond the
860 * block device's EOF.
861 */
0b3147ba 862 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
62efe6ec 863 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
99924359 864 HAMMER2_VOLUME_BYTES, &bp);
0b3147ba 865 if (error) {
99924359
MD
866 brelse(bp);
867 bp = NULL;
0b3147ba
VS
868 continue;
869 }
870
88a032af
VS
871 vd = (struct hammer2_volume_data *) bp->b_data;
872 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
873 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
874 brelse(bp);
875 bp = NULL;
0b3147ba 876 continue;
88a032af
VS
877 }
878
879 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
880 /* XXX: Reversed-endianness filesystem */
881 kprintf("hammer2: reverse-endian filesystem detected");
882 brelse(bp);
883 bp = NULL;
884 continue;
885 }
0b3147ba
VS
886
887 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
60fbd5f4 888 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
99924359 889 HAMMER2_VOLUME_ICRC0_SIZE);
60fbd5f4
VS
890 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
891 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
892 HAMMER2_VOLUME_ICRC1_SIZE);
893 if ((crc0 != crc) || (bcrc0 != bcrc)) {
99924359
MD
894 kprintf("hammer2 volume header crc "
895 "mismatch copy #%d\t%08x %08x",
60fbd5f4 896 i, crc0, crc);
99924359
MD
897 error_reported = 1;
898 brelse(bp);
899 bp = NULL;
0b3147ba
VS
900 continue;
901 }
4d5318eb 902 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
99924359
MD
903 valid = 1;
904 hmp->voldata = *vd;
0b3147ba 905 }
99924359
MD
906 brelse(bp);
907 bp = NULL;
50e4f8f4 908 }
0b3147ba 909 if (valid) {
57381c9e 910 error = 0;
99924359
MD
911 if (error_reported)
912 kprintf("hammer2: a valid volume header was found\n");
0b3147ba
VS
913 } else {
914 error = EINVAL;
99924359 915 kprintf("hammer2: no valid volume headers found!\n");
0b3147ba 916 }
0b3147ba 917 return (error);
50e4f8f4 918}
0b3147ba 919