2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
67 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
69 struct hammer2_sync_info {
70 hammer2_trans_t trans;
75 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
76 TAILQ_HEAD(hammer2_pfslist, hammer2_pfsmount);
77 static struct hammer2_mntlist hammer2_mntlist;
78 static struct hammer2_pfslist hammer2_pfslist;
79 static struct lock hammer2_mntlk;
82 int hammer2_cluster_enable = 1;
83 int hammer2_hardlink_enable = 1;
84 int hammer2_flush_pipe = 100;
85 int hammer2_synchronous_flush = 1;
86 int hammer2_dio_count;
87 long hammer2_limit_dirty_chains;
88 long hammer2_iod_file_read;
89 long hammer2_iod_meta_read;
90 long hammer2_iod_indr_read;
91 long hammer2_iod_fmap_read;
92 long hammer2_iod_volu_read;
93 long hammer2_iod_file_write;
94 long hammer2_iod_meta_write;
95 long hammer2_iod_indr_write;
96 long hammer2_iod_fmap_write;
97 long hammer2_iod_volu_write;
98 long hammer2_ioa_file_read;
99 long hammer2_ioa_meta_read;
100 long hammer2_ioa_indr_read;
101 long hammer2_ioa_fmap_read;
102 long hammer2_ioa_volu_read;
103 long hammer2_ioa_fmap_write;
104 long hammer2_ioa_file_write;
105 long hammer2_ioa_meta_write;
106 long hammer2_ioa_indr_write;
107 long hammer2_ioa_volu_write;
109 MALLOC_DECLARE(C_BUFFER);
110 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
112 MALLOC_DECLARE(D_BUFFER);
113 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
115 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
117 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
118 &hammer2_debug, 0, "");
119 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
120 &hammer2_cluster_enable, 0, "");
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
122 &hammer2_hardlink_enable, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
124 &hammer2_flush_pipe, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
126 &hammer2_synchronous_flush, 0, "");
127 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
128 &hammer2_limit_dirty_chains, 0, "");
129 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
130 &hammer2_dio_count, 0, "");
132 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
133 &hammer2_iod_file_read, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
135 &hammer2_iod_meta_read, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
137 &hammer2_iod_indr_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
139 &hammer2_iod_fmap_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
141 &hammer2_iod_volu_read, 0, "");
143 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
144 &hammer2_iod_file_write, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
146 &hammer2_iod_meta_write, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
148 &hammer2_iod_indr_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
150 &hammer2_iod_fmap_write, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
152 &hammer2_iod_volu_write, 0, "");
154 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
155 &hammer2_ioa_file_read, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
157 &hammer2_ioa_meta_read, 0, "");
158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
159 &hammer2_ioa_indr_read, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
161 &hammer2_ioa_fmap_read, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
163 &hammer2_ioa_volu_read, 0, "");
165 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
166 &hammer2_ioa_file_write, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
168 &hammer2_ioa_meta_write, 0, "");
169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
170 &hammer2_ioa_indr_write, 0, "");
171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
172 &hammer2_ioa_fmap_write, 0, "");
173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
174 &hammer2_ioa_volu_write, 0, "");
176 static int hammer2_vfs_init(struct vfsconf *conf);
177 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
178 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
180 static int hammer2_remount(hammer2_mount_t *, struct mount *, char *,
181 struct vnode *, struct ucred *);
182 static int hammer2_recovery(hammer2_mount_t *hmp);
183 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
184 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
185 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
187 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
189 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
190 ino_t ino, struct vnode **vpp);
191 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
192 struct fid *fhp, struct vnode **vpp);
193 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
194 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
195 int *exflagsp, struct ucred **credanonp);
197 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
198 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
200 static void hammer2_write_thread(void *arg);
202 static void hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp);
203 static void hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp);
206 * Functions for compression in threads,
207 * from hammer2_vnops.c
209 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
211 const hammer2_inode_data_t *ripdata,
212 hammer2_cluster_t *cparent,
213 hammer2_key_t lbase, int ioflag, int pblksize,
215 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
217 const hammer2_inode_data_t *ripdata,
218 hammer2_cluster_t *cparent,
219 hammer2_key_t lbase, int ioflag,
220 int pblksize, int *errorp,
221 int comp_algo, int check_algo);
222 static void hammer2_zero_check_and_write(struct buf *bp,
223 hammer2_trans_t *trans, hammer2_inode_t *ip,
224 const hammer2_inode_data_t *ripdata,
225 hammer2_cluster_t *cparent,
227 int ioflag, int pblksize, int *errorp,
229 static int test_block_zeros(const char *buf, size_t bytes);
230 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
232 const hammer2_inode_data_t *ripdata,
233 hammer2_cluster_t *cparent,
236 static void hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp,
237 int ioflag, int pblksize, int *errorp,
241 * HAMMER2 vfs operations.
243 static struct vfsops hammer2_vfsops = {
244 .vfs_init = hammer2_vfs_init,
245 .vfs_uninit = hammer2_vfs_uninit,
246 .vfs_sync = hammer2_vfs_sync,
247 .vfs_mount = hammer2_vfs_mount,
248 .vfs_unmount = hammer2_vfs_unmount,
249 .vfs_root = hammer2_vfs_root,
250 .vfs_statfs = hammer2_vfs_statfs,
251 .vfs_statvfs = hammer2_vfs_statvfs,
252 .vfs_vget = hammer2_vfs_vget,
253 .vfs_vptofh = hammer2_vfs_vptofh,
254 .vfs_fhtovp = hammer2_vfs_fhtovp,
255 .vfs_checkexp = hammer2_vfs_checkexp
258 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
260 VFS_SET(hammer2_vfsops, hammer2, 0);
261 MODULE_VERSION(hammer2, 1);
265 hammer2_vfs_init(struct vfsconf *conf)
267 static struct objcache_malloc_args margs_read;
268 static struct objcache_malloc_args margs_write;
274 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
276 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
278 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
282 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
284 margs_read.objsize = 65536;
285 margs_read.mtype = D_BUFFER;
287 margs_write.objsize = 32768;
288 margs_write.mtype = C_BUFFER;
290 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
291 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
292 objcache_malloc_free, &margs_read);
293 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
294 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
295 objcache_malloc_free, &margs_write);
297 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
298 TAILQ_INIT(&hammer2_mntlist);
299 TAILQ_INIT(&hammer2_pfslist);
301 hammer2_limit_dirty_chains = desiredvnodes / 10;
303 hammer2_trans_manage_init();
310 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
312 objcache_destroy(cache_buffer_read);
313 objcache_destroy(cache_buffer_write);
318 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
319 * mounts and the spmp structure for media (hmp) structures.
321 static hammer2_pfsmount_t *
322 hammer2_pfsalloc(const hammer2_inode_data_t *ripdata, hammer2_tid_t alloc_tid)
324 hammer2_pfsmount_t *pmp;
326 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
327 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
328 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
329 lockinit(&pmp->lock, "pfslk", 0, 0);
330 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
331 RB_INIT(&pmp->inum_tree);
332 TAILQ_INIT(&pmp->unlinkq);
333 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
335 pmp->alloc_tid = alloc_tid + 1; /* our first media transaction id */
336 pmp->flush_tid = pmp->alloc_tid;
338 pmp->inode_tid = ripdata->pfs_inum + 1;
339 pmp->pfs_clid = ripdata->pfs_clid;
341 hammer2_mtx_init(&pmp->wthread_mtx, "h2wthr");
342 bioq_init(&pmp->wthread_bioq);
348 * Mount or remount HAMMER2 fileystem from physical media
351 * mp mount point structure
357 * mp mount point structure
358 * path path to mount point
359 * data pointer to argument structure in user space
360 * volume volume path (device@LABEL form)
361 * hflags user mount flags
362 * cred user credentials
369 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
372 struct hammer2_mount_info info;
373 hammer2_pfsmount_t *pmp;
374 hammer2_pfsmount_t *spmp;
375 hammer2_mount_t *hmp;
376 hammer2_key_t key_next;
377 hammer2_key_t key_dummy;
380 struct nlookupdata nd;
381 hammer2_chain_t *parent;
382 hammer2_chain_t *rchain;
383 hammer2_cluster_t *cluster;
384 hammer2_cluster_t *cparent;
385 const hammer2_inode_data_t *ripdata;
386 hammer2_blockref_t bref;
388 char devstr[MNAMELEN];
406 kprintf("hammer2_mount\n");
412 bzero(&info, sizeof(info));
413 info.cluster_fd = -1;
417 * Non-root mount or updating a mount
419 error = copyin(data, &info, sizeof(info));
423 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
427 /* Extract device and label */
429 label = strchr(devstr, '@');
431 ((label + 1) - dev) > done) {
439 if (mp->mnt_flag & MNT_UPDATE) {
441 /* HAMMER2 implements NFS export via mountctl */
443 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
444 hmp = pmp->iroot->cluster.array[i]->hmp;
446 error = hammer2_remount(hmp, mp, path,
451 /*hammer2_inode_install_hidden(pmp);*/
460 * Lookup name and verify it refers to a block device.
462 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
464 error = nlookup(&nd);
466 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
470 if (vn_isdisk(devvp, &error))
471 error = vfs_mountedon(devvp);
475 * Determine if the device has already been mounted. After this
476 * check hmp will be non-NULL if we are doing the second or more
477 * hammer2 mounts from the same device.
479 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
480 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
481 if (hmp->devvp == devvp)
486 * Open the device if this isn't a secondary mount and construct
487 * the H2 device mount (hmp).
490 hammer2_chain_t *schain;
493 if (error == 0 && vcount(devvp) > 0)
497 * Now open the device
500 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
501 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
502 error = vinvalbuf(devvp, V_SAVE, 0, 0);
504 error = VOP_OPEN(devvp,
505 ronly ? FREAD : FREAD | FWRITE,
510 if (error && devvp) {
515 lockmgr(&hammer2_mntlk, LK_RELEASE);
518 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
521 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
522 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
523 RB_INIT(&hmp->iotree);
524 spin_init(&hmp->io_spin, "hm2mount_io");
525 spin_init(&hmp->list_spin, "hm2mount_list");
526 TAILQ_INIT(&hmp->flushq);
528 lockinit(&hmp->vollk, "h2vol", 0, 0);
531 * vchain setup. vchain.data is embedded.
532 * vchain.refs is initialized and will never drop to 0.
534 * NOTE! voldata is not yet loaded.
536 hmp->vchain.hmp = hmp;
537 hmp->vchain.refs = 1;
538 hmp->vchain.data = (void *)&hmp->voldata;
539 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
540 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
541 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
543 hammer2_chain_core_alloc(NULL, &hmp->vchain);
544 /* hmp->vchain.u.xxx is left NULL */
547 * fchain setup. fchain.data is embedded.
548 * fchain.refs is initialized and will never drop to 0.
550 * The data is not used but needs to be initialized to
551 * pass assertion muster. We use this chain primarily
552 * as a placeholder for the freemap's top-level RBTREE
553 * so it does not interfere with the volume's topology
556 hmp->fchain.hmp = hmp;
557 hmp->fchain.refs = 1;
558 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
559 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
560 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
561 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
562 hmp->fchain.bref.methods =
563 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
564 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
566 hammer2_chain_core_alloc(NULL, &hmp->fchain);
567 /* hmp->fchain.u.xxx is left NULL */
570 * Install the volume header and initialize fields from
573 error = hammer2_install_volume_header(hmp);
576 hammer2_vfs_unmount_hmp1(mp, hmp);
577 hammer2_vfs_unmount_hmp2(mp, hmp);
578 lockmgr(&hammer2_mntlk, LK_RELEASE);
579 hammer2_vfs_unmount(mp, MNT_FORCE);
584 * Really important to get these right or flush will get
587 hmp->spmp = hammer2_pfsalloc(NULL, hmp->voldata.mirror_tid);
588 kprintf("alloc spmp %p tid %016jx\n",
589 hmp->spmp, hmp->voldata.mirror_tid);
594 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
595 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
596 hmp->vchain.pmp = spmp;
597 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
598 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
599 hmp->fchain.pmp = spmp;
602 * First locate the super-root inode, which is key 0
603 * relative to the volume header's blockset.
605 * Then locate the root inode by scanning the directory keyspace
606 * represented by the label.
608 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
609 schain = hammer2_chain_lookup(&parent, &key_dummy,
610 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
611 &cache_index, 0, &ddflag);
612 hammer2_chain_lookup_done(parent);
613 if (schain == NULL) {
614 kprintf("hammer2_mount: invalid super-root\n");
616 hammer2_vfs_unmount_hmp1(mp, hmp);
617 hammer2_vfs_unmount_hmp2(mp, hmp);
618 lockmgr(&hammer2_mntlk, LK_RELEASE);
619 hammer2_vfs_unmount(mp, MNT_FORCE);
624 * Sanity-check schain's pmp, finish initializing spmp.
626 ripdata = &hammer2_chain_rdata(schain)->ipdata;
627 KKASSERT(schain->pmp == spmp);
628 spmp->pfs_clid = ripdata->pfs_clid;
631 * NOTE: inode_get sucks up schain's lock.
633 cluster = hammer2_cluster_from_chain(schain);
634 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
635 spmp->spmp_hmp = hmp;
636 hammer2_inode_ref(spmp->iroot);
637 hammer2_inode_unlock_ex(spmp->iroot, cluster);
639 /* leave spmp->iroot with one ref */
641 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
642 error = hammer2_recovery(hmp);
643 /* XXX do something with error */
647 hammer2_iocom_init(hmp);
650 * Ref the cluster management messaging descriptor. The mount
651 * program deals with the other end of the communications pipe.
653 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
655 hammer2_cluster_reconnect(hmp, fp);
657 kprintf("hammer2_mount: bad cluster_fd!\n");
665 * Lookup mount point under the media-localized super-root.
667 * cluster->pmp will incorrectly point to spmp and must be fixed
670 cparent = hammer2_inode_lock_ex(spmp->iroot);
671 lhc = hammer2_dirhash(label, strlen(label));
672 cluster = hammer2_cluster_lookup(cparent, &key_next,
673 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
676 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
678 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) {
681 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
683 lhc + HAMMER2_DIRHASH_LOMASK, 0);
685 hammer2_inode_unlock_ex(spmp->iroot, cparent);
687 if (cluster == NULL) {
688 kprintf("hammer2_mount: PFS label not found\n");
689 hammer2_vfs_unmount_hmp1(mp, hmp);
690 hammer2_vfs_unmount_hmp2(mp, hmp);
691 lockmgr(&hammer2_mntlk, LK_RELEASE);
692 hammer2_vfs_unmount(mp, MNT_FORCE);
696 for (i = 0; i < cluster->nchains; ++i) {
697 rchain = cluster->array[i];
698 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
699 kprintf("hammer2_mount: PFS label already mounted!\n");
700 hammer2_cluster_unlock(cluster);
701 hammer2_vfs_unmount_hmp1(mp, hmp);
702 hammer2_vfs_unmount_hmp2(mp, hmp);
703 lockmgr(&hammer2_mntlk, LK_RELEASE);
704 hammer2_vfs_unmount(mp, MNT_FORCE);
707 KKASSERT(rchain->pmp == NULL);
709 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
710 kprintf("hammer2_mount: PFS label is recycling\n");
711 hammer2_cluster_unlock(cluster);
712 hammer2_vfs_unmount_hmp1(mp, hmp);
713 hammer2_vfs_unmount_hmp2(mp, hmp);
714 lockmgr(&hammer2_mntlk, LK_RELEASE);
715 hammer2_vfs_unmount(mp, MNT_FORCE);
722 * Check to see if the cluster id is already mounted at the mount
723 * point. If it is, add us to the cluster.
725 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
726 hammer2_cluster_bref(cluster, &bref);
727 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
728 if (pmp->spmp_hmp == NULL &&
729 bcmp(&pmp->pfs_clid, &ripdata->pfs_clid,
730 sizeof(pmp->pfs_clid)) == 0) {
740 * Directly lock the inode->lock, do not run through
741 * hammer2_inode_lock*().
743 hammer2_inode_ref(pmp->iroot);
744 hammer2_mtx_ex(&pmp->iroot->lock, "h2ino");
746 if (pmp->iroot->cluster.nchains + cluster->nchains >
747 HAMMER2_MAXCLUSTER) {
748 kprintf("hammer2_mount: cluster full!\n");
750 hammer2_mtx_unlock(&pmp->iroot->lock);
751 hammer2_inode_drop(pmp->iroot);
753 hammer2_cluster_unlock(cluster);
754 hammer2_vfs_unmount_hmp1(mp, hmp);
755 hammer2_vfs_unmount_hmp2(mp, hmp);
756 lockmgr(&hammer2_mntlk, LK_RELEASE);
757 hammer2_vfs_unmount(mp, MNT_FORCE);
760 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n");
761 j = pmp->iroot->cluster.nchains;
762 for (i = 0; i < cluster->nchains; ++i) {
763 rchain = cluster->array[i];
764 KKASSERT(rchain->pmp == NULL);
766 hammer2_chain_ref(cluster->array[i]);
767 pmp->iroot->cluster.array[j] = cluster->array[i];
770 pmp->iroot->cluster.nchains = j;
771 hammer2_mtx_unlock(&pmp->iroot->lock);
772 hammer2_inode_drop(pmp->iroot);
773 hammer2_cluster_unlock(cluster);
774 lockmgr(&hammer2_mntlk, LK_RELEASE);
777 hammer2_inode_install_hidden(pmp);
783 * Block device opened successfully, finish initializing the
786 * From this point on we have to call hammer2_unmount() on failure.
788 pmp = hammer2_pfsalloc(ripdata, bref.mirror_tid);
789 kprintf("PMP mirror_tid is %016jx\n", bref.mirror_tid);
790 for (i = 0; i < cluster->nchains; ++i) {
791 rchain = cluster->array[i];
792 KKASSERT(rchain->pmp == NULL);
794 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
798 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
799 lockmgr(&hammer2_mntlk, LK_RELEASE);
801 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
802 hmp, pmp, hmp->pmp_count);
804 mp->mnt_flag = MNT_LOCAL;
805 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
806 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
809 * required mount structure initializations
811 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
812 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
814 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
815 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
820 mp->mnt_iosize_max = MAXPHYS;
821 mp->mnt_data = (qaddr_t)pmp;
825 * After this point hammer2_vfs_unmount() has visibility on hmp
826 * and manual hmp1/hmp2 calls are not needed on fatal errors.
828 pmp->iroot = hammer2_inode_get(pmp, NULL, cluster);
829 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
830 hammer2_inode_unlock_ex(pmp->iroot, cluster);
833 * The logical file buffer bio write thread handles things
834 * like physical block assignment and compression.
836 * (only applicable to pfs mounts, not applicable to spmp)
838 pmp->wthread_destroy = 0;
839 lwkt_create(hammer2_write_thread, pmp,
840 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
843 * With the cluster operational install ihidden.
844 * (only applicable to pfs mounts, not applicable to spmp)
846 hammer2_inode_install_hidden(pmp);
852 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
853 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
854 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
856 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
857 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
858 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
859 copyinstr(path, mp->mnt_stat.f_mntonname,
860 sizeof(mp->mnt_stat.f_mntonname) - 1,
864 * Initial statfs to prime mnt_stat.
866 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
872 * Handle bioq for strategy write
876 hammer2_write_thread(void *arg)
878 hammer2_pfsmount_t *pmp;
881 hammer2_trans_t trans;
884 hammer2_cluster_t *cparent;
885 hammer2_inode_data_t *wipdata;
893 hammer2_mtx_ex(&pmp->wthread_mtx, "h2wth");
894 while (pmp->wthread_destroy == 0) {
895 if (bioq_first(&pmp->wthread_bioq) == NULL) {
896 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
901 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
903 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
905 * dummy bio for synchronization. The transaction
906 * must be reinitialized.
908 if (bio->bio_buf == NULL) {
909 bio->bio_flags |= BIO_DONE;
911 hammer2_trans_done(&trans);
912 hammer2_trans_init(&trans, pmp,
913 HAMMER2_TRANS_BUFCACHE);
918 * else normal bio processing
920 hammer2_mtx_unlock(&pmp->wthread_mtx);
922 hammer2_lwinprog_drop(pmp);
930 * Inode is modified, flush size and mtime changes
931 * to ensure that the file size remains consistent
932 * with the buffers being flushed.
934 * NOTE: The inode_fsync() call only flushes the
935 * inode's meta-data state, it doesn't try
936 * to flush underlying buffers or chains.
938 cparent = hammer2_inode_lock_ex(ip);
939 if (ip->flags & (HAMMER2_INODE_RESIZED |
940 HAMMER2_INODE_MTIME)) {
941 hammer2_inode_fsync(&trans, ip, cparent);
943 wipdata = hammer2_cluster_modify_ip(&trans, ip,
945 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
947 pblksize = hammer2_calc_physical(ip, wipdata, lbase);
948 hammer2_write_file_core(bp, &trans, ip, wipdata,
952 hammer2_cluster_modsync(cparent);
953 hammer2_inode_unlock_ex(ip, cparent);
955 kprintf("hammer2: error in buffer write\n");
956 bp->b_flags |= B_ERROR;
960 hammer2_mtx_ex(&pmp->wthread_mtx, "h2wth");
962 hammer2_trans_done(&trans);
964 pmp->wthread_destroy = -1;
965 wakeup(&pmp->wthread_destroy);
967 hammer2_mtx_unlock(&pmp->wthread_mtx);
971 hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
975 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
976 hammer2_mtx_ex(&pmp->wthread_mtx, "h2wth");
977 if (pmp->wthread_destroy == 0 &&
978 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
979 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
980 while ((sync_bio.bio_flags & BIO_DONE) == 0)
981 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
983 hammer2_mtx_unlock(&pmp->wthread_mtx);
987 * Return a chain suitable for I/O, creating the chain if necessary
988 * and assigning its physical block.
992 hammer2_assign_physical(hammer2_trans_t *trans,
993 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
994 hammer2_key_t lbase, int pblksize, int *errorp)
996 hammer2_cluster_t *cluster;
997 hammer2_cluster_t *dparent;
998 hammer2_key_t key_dummy;
999 int pradix = hammer2_getradix(pblksize);
1003 * Locate the chain associated with lbase, return a locked chain.
1004 * However, do not instantiate any data reference (which utilizes a
1005 * device buffer) because we will be using direct IO via the
1006 * logical buffer cache buffer.
1009 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
1011 dparent = hammer2_cluster_lookup_init(cparent, 0);
1012 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
1014 HAMMER2_LOOKUP_NODATA, &ddflag);
1016 if (cluster == NULL) {
1018 * We found a hole, create a new chain entry.
1020 * NOTE: DATA chains are created without device backing
1021 * store (nor do we want any).
1023 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
1024 lbase, HAMMER2_PBUFRADIX,
1025 HAMMER2_BREF_TYPE_DATA,
1027 if (cluster == NULL) {
1028 hammer2_cluster_lookup_done(dparent);
1029 panic("hammer2_cluster_create: par=%p error=%d\n",
1030 dparent->focus, *errorp);
1033 /*ip->delta_dcount += pblksize;*/
1035 switch (hammer2_cluster_type(cluster)) {
1036 case HAMMER2_BREF_TYPE_INODE:
1038 * The data is embedded in the inode. The
1039 * caller is responsible for marking the inode
1040 * modified and copying the data to the embedded
1044 case HAMMER2_BREF_TYPE_DATA:
1045 if (hammer2_cluster_need_resize(cluster, pblksize)) {
1046 hammer2_cluster_resize(trans, ip,
1049 HAMMER2_MODIFY_OPTDATA);
1053 * DATA buffers must be marked modified whether the
1054 * data is in a logical buffer or not. We also have
1055 * to make this call to fixup the chain data pointers
1056 * after resizing in case this is an encrypted or
1057 * compressed buffer.
1059 hammer2_cluster_modify(trans, cluster,
1060 HAMMER2_MODIFY_OPTDATA);
1063 panic("hammer2_assign_physical: bad type");
1070 * Cleanup. If cluster wound up being the inode itself, i.e.
1071 * the DIRECTDATA case for offset 0, then we need to update cparent.
1072 * The caller expects cparent to not become stale.
1074 hammer2_cluster_lookup_done(dparent);
1075 /* dparent = NULL; safety */
1076 if (cluster && ddflag)
1077 hammer2_cluster_replace_locked(cparent, cluster);
1082 * bio queued from hammer2_vnops.c.
1084 * The core write function which determines which path to take
1085 * depending on compression settings. We also have to locate the
1086 * related clusters so we can calculate and set the check data for
1091 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
1092 hammer2_inode_t *ip,
1093 const hammer2_inode_data_t *ripdata,
1094 hammer2_cluster_t *cparent,
1095 hammer2_key_t lbase, int ioflag, int pblksize,
1098 hammer2_cluster_t *cluster;
1100 switch(HAMMER2_DEC_ALGO(ripdata->comp_algo)) {
1101 case HAMMER2_COMP_NONE:
1103 * We have to assign physical storage to the buffer
1104 * we intend to dirty or write now to avoid deadlocks
1105 * in the strategy code later.
1107 * This can return NOOFFSET for inode-embedded data.
1108 * The strategy code will take care of it in that case.
1110 cluster = hammer2_assign_physical(trans, ip, cparent,
1113 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1114 ripdata->check_algo);
1116 hammer2_cluster_unlock(cluster);
1118 case HAMMER2_COMP_AUTOZERO:
1120 * Check for zero-fill only
1122 hammer2_zero_check_and_write(bp, trans, ip,
1123 ripdata, cparent, lbase,
1124 ioflag, pblksize, errorp,
1125 ripdata->check_algo);
1127 case HAMMER2_COMP_LZ4:
1128 case HAMMER2_COMP_ZLIB:
1131 * Check for zero-fill and attempt compression.
1133 hammer2_compress_and_write(bp, trans, ip,
1138 ripdata->check_algo);
1144 * Generic function that will perform the compression in compression
1145 * write path. The compression algorithm is determined by the settings
1146 * obtained from inode.
1150 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
1151 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1152 hammer2_cluster_t *cparent,
1153 hammer2_key_t lbase, int ioflag, int pblksize,
1154 int *errorp, int comp_algo, int check_algo)
1156 hammer2_cluster_t *cluster;
1157 hammer2_chain_t *chain;
1159 int comp_block_size;
1163 if (test_block_zeros(bp->b_data, pblksize)) {
1164 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1171 KKASSERT(pblksize / 2 <= 32768);
1173 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1174 z_stream strm_compress;
1178 switch(HAMMER2_DEC_ALGO(comp_algo)) {
1179 case HAMMER2_COMP_LZ4:
1180 comp_buffer = objcache_get(cache_buffer_write,
1182 comp_size = LZ4_compress_limitedOutput(
1184 &comp_buffer[sizeof(int)],
1186 pblksize / 2 - sizeof(int));
1188 * We need to prefix with the size, LZ4
1189 * doesn't do it for us. Add the related
1192 *(int *)comp_buffer = comp_size;
1194 comp_size += sizeof(int);
1196 case HAMMER2_COMP_ZLIB:
1197 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1198 if (comp_level == 0)
1199 comp_level = 6; /* default zlib compression */
1200 else if (comp_level < 6)
1202 else if (comp_level > 9)
1204 ret = deflateInit(&strm_compress, comp_level);
1206 kprintf("HAMMER2 ZLIB: fatal error "
1207 "on deflateInit.\n");
1210 comp_buffer = objcache_get(cache_buffer_write,
1212 strm_compress.next_in = bp->b_data;
1213 strm_compress.avail_in = pblksize;
1214 strm_compress.next_out = comp_buffer;
1215 strm_compress.avail_out = pblksize / 2;
1216 ret = deflate(&strm_compress, Z_FINISH);
1217 if (ret == Z_STREAM_END) {
1218 comp_size = pblksize / 2 -
1219 strm_compress.avail_out;
1223 ret = deflateEnd(&strm_compress);
1226 kprintf("Error: Unknown compression method.\n");
1227 kprintf("Comp_method = %d.\n", comp_algo);
1232 if (comp_size == 0) {
1234 * compression failed or turned off
1236 comp_block_size = pblksize; /* safety */
1237 if (++ip->comp_heuristic > 128)
1238 ip->comp_heuristic = 8;
1241 * compression succeeded
1243 ip->comp_heuristic = 0;
1244 if (comp_size <= 1024) {
1245 comp_block_size = 1024;
1246 } else if (comp_size <= 2048) {
1247 comp_block_size = 2048;
1248 } else if (comp_size <= 4096) {
1249 comp_block_size = 4096;
1250 } else if (comp_size <= 8192) {
1251 comp_block_size = 8192;
1252 } else if (comp_size <= 16384) {
1253 comp_block_size = 16384;
1254 } else if (comp_size <= 32768) {
1255 comp_block_size = 32768;
1257 panic("hammer2: WRITE PATH: "
1258 "Weird comp_size value.");
1260 comp_block_size = pblksize;
1264 cluster = hammer2_assign_physical(trans, ip, cparent,
1265 lbase, comp_block_size,
1270 kprintf("WRITE PATH: An error occurred while "
1271 "assigning physical space.\n");
1272 KKASSERT(cluster == NULL);
1276 for (i = 0; i < cluster->nchains; ++i) {
1277 hammer2_inode_data_t *wipdata;
1281 chain = cluster->array[i]; /* XXX */
1282 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1284 switch(chain->bref.type) {
1285 case HAMMER2_BREF_TYPE_INODE:
1286 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1287 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1288 KKASSERT(bp->b_loffset == 0);
1289 bcopy(bp->b_data, wipdata->u.data,
1290 HAMMER2_EMBEDDED_BYTES);
1292 case HAMMER2_BREF_TYPE_DATA:
1294 * Optimize out the read-before-write
1297 *errorp = hammer2_io_newnz(chain->hmp,
1298 chain->bref.data_off,
1302 hammer2_io_brelse(&dio);
1303 kprintf("hammer2: WRITE PATH: "
1304 "dbp bread error\n");
1307 bdata = hammer2_io_data(dio, chain->bref.data_off);
1310 * When loading the block make sure we don't
1311 * leave garbage after the compressed data.
1314 chain->bref.methods =
1315 HAMMER2_ENC_COMP(comp_algo) +
1316 HAMMER2_ENC_CHECK(check_algo);
1317 bcopy(comp_buffer, bdata, comp_size);
1318 if (comp_size != comp_block_size) {
1319 bzero(bdata + comp_size,
1320 comp_block_size - comp_size);
1323 chain->bref.methods =
1325 HAMMER2_COMP_NONE) +
1326 HAMMER2_ENC_CHECK(check_algo);
1327 bcopy(bp->b_data, bdata, pblksize);
1331 * The flush code doesn't calculate check codes for
1332 * file data (doing so can result in excessive I/O),
1335 hammer2_chain_setcheck(chain, bdata);
1338 * Device buffer is now valid, chain is no longer in
1339 * the initial state.
1341 * (No blockref table worries with file data)
1343 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1345 /* Now write the related bdp. */
1346 if (ioflag & IO_SYNC) {
1348 * Synchronous I/O requested.
1350 hammer2_io_bwrite(&dio);
1352 } else if ((ioflag & IO_DIRECT) &&
1353 loff + n == pblksize) {
1354 hammer2_io_bdwrite(&dio);
1356 } else if (ioflag & IO_ASYNC) {
1357 hammer2_io_bawrite(&dio);
1359 hammer2_io_bdwrite(&dio);
1363 panic("hammer2_write_bp: bad chain type %d\n",
1371 hammer2_cluster_unlock(cluster);
1373 objcache_put(cache_buffer_write, comp_buffer);
1377 * Function that performs zero-checking and writing without compression,
1378 * it corresponds to default zero-checking path.
1382 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1383 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1384 hammer2_cluster_t *cparent,
1385 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp,
1388 hammer2_cluster_t *cluster;
1390 if (test_block_zeros(bp->b_data, pblksize)) {
1391 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1393 cluster = hammer2_assign_physical(trans, ip, cparent,
1394 lbase, pblksize, errorp);
1395 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1398 hammer2_cluster_unlock(cluster);
1403 * A function to test whether a block of data contains only zeros,
1404 * returns TRUE (non-zero) if the block is all zeros.
1408 test_block_zeros(const char *buf, size_t bytes)
1412 for (i = 0; i < bytes; i += sizeof(long)) {
1413 if (*(const long *)(buf + i) != 0)
1420 * Function to "write" a block that contains only zeros.
1424 zero_write(struct buf *bp, hammer2_trans_t *trans,
1425 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1426 hammer2_cluster_t *cparent,
1427 hammer2_key_t lbase, int *errorp __unused)
1429 hammer2_cluster_t *cluster;
1430 hammer2_media_data_t *data;
1431 hammer2_key_t key_dummy;
1434 cparent = hammer2_cluster_lookup_init(cparent, 0);
1435 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1436 HAMMER2_LOOKUP_NODATA, &ddflag);
1438 data = hammer2_cluster_wdata(cluster);
1441 KKASSERT(cluster->focus->flags &
1442 HAMMER2_CHAIN_MODIFIED);
1443 bzero(data->ipdata.u.data, HAMMER2_EMBEDDED_BYTES);
1444 hammer2_cluster_modsync(cluster);
1446 hammer2_cluster_delete(trans, cparent, cluster,
1447 HAMMER2_DELETE_PERMANENT);
1449 hammer2_cluster_unlock(cluster);
1451 hammer2_cluster_lookup_done(cparent);
1455 * Function to write the data as it is, without performing any sort of
1456 * compression. This function is used in path without compression and
1457 * default zero-checking path.
1461 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1462 int pblksize, int *errorp, int check_algo)
1464 hammer2_chain_t *chain;
1465 hammer2_inode_data_t *wipdata;
1471 error = 0; /* XXX TODO below */
1473 for (i = 0; i < cluster->nchains; ++i) {
1474 chain = cluster->array[i]; /* XXX */
1475 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1477 switch(chain->bref.type) {
1478 case HAMMER2_BREF_TYPE_INODE:
1479 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1480 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1481 KKASSERT(bp->b_loffset == 0);
1482 bcopy(bp->b_data, wipdata->u.data,
1483 HAMMER2_EMBEDDED_BYTES);
1486 case HAMMER2_BREF_TYPE_DATA:
1487 error = hammer2_io_newnz(chain->hmp,
1488 chain->bref.data_off,
1489 chain->bytes, &dio);
1491 hammer2_io_bqrelse(&dio);
1492 kprintf("hammer2: WRITE PATH: "
1493 "dbp bread error\n");
1496 bdata = hammer2_io_data(dio, chain->bref.data_off);
1498 chain->bref.methods = HAMMER2_ENC_COMP(
1499 HAMMER2_COMP_NONE) +
1500 HAMMER2_ENC_CHECK(check_algo);
1501 bcopy(bp->b_data, bdata, chain->bytes);
1504 * The flush code doesn't calculate check codes for
1505 * file data (doing so can result in excessive I/O),
1508 hammer2_chain_setcheck(chain, bdata);
1511 * Device buffer is now valid, chain is no longer in
1512 * the initial state.
1514 * (No blockref table worries with file data)
1516 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1518 if (ioflag & IO_SYNC) {
1520 * Synchronous I/O requested.
1522 hammer2_io_bwrite(&dio);
1524 } else if ((ioflag & IO_DIRECT) &&
1525 loff + n == pblksize) {
1526 hammer2_io_bdwrite(&dio);
1528 } else if (ioflag & IO_ASYNC) {
1529 hammer2_io_bawrite(&dio);
1531 hammer2_io_bdwrite(&dio);
1535 panic("hammer2_write_bp: bad chain type %d\n",
1541 KKASSERT(error == 0); /* XXX TODO */
1548 hammer2_remount(hammer2_mount_t *hmp, struct mount *mp, char *path,
1549 struct vnode *devvp, struct ucred *cred)
1553 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1554 error = hammer2_recovery(hmp);
1563 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1565 hammer2_pfsmount_t *pmp;
1566 hammer2_mount_t *hmp;
1567 hammer2_chain_t *rchain;
1568 hammer2_cluster_t *cluster;
1578 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1579 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
1582 * If mount initialization proceeded far enough we must flush
1585 if (mntflags & MNT_FORCE)
1590 error = vflush(mp, 0, flags);
1595 if (pmp->wthread_td) {
1596 hammer2_mtx_ex(&pmp->wthread_mtx, "h2wth");
1597 pmp->wthread_destroy = 1;
1598 wakeup(&pmp->wthread_bioq);
1599 while (pmp->wthread_destroy != -1) {
1600 mtxsleep(&pmp->wthread_destroy,
1601 &pmp->wthread_mtx, 0,
1604 hammer2_mtx_unlock(&pmp->wthread_mtx);
1605 pmp->wthread_td = NULL;
1609 * Cleanup our reference on ihidden.
1612 hammer2_inode_drop(pmp->ihidden);
1613 pmp->ihidden = NULL;
1617 * Cleanup our reference on iroot. iroot is (should) not be needed
1618 * by the flush code.
1621 cluster = &pmp->iroot->cluster;
1622 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1623 rchain = pmp->iroot->cluster.array[i];
1627 hammer2_vfs_unmount_hmp1(mp, hmp);
1629 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
1630 #if REPORT_REFS_ERRORS
1631 if (rchain->refs != 1)
1632 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1633 rchain, rchain->refs);
1635 KKASSERT(rchain->refs == 1);
1637 hammer2_chain_drop(rchain);
1638 cluster->array[i] = NULL;
1639 hammer2_vfs_unmount_hmp2(mp, hmp);
1641 cluster->focus = NULL;
1643 #if REPORT_REFS_ERRORS
1644 if (pmp->iroot->refs != 1)
1645 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1646 pmp->iroot, pmp->iroot->refs);
1648 KKASSERT(pmp->iroot->refs == 1);
1650 /* ref for pmp->iroot */
1651 hammer2_inode_drop(pmp->iroot);
1656 mp->mnt_data = NULL;
1658 kmalloc_destroy(&pmp->mmsg);
1659 kmalloc_destroy(&pmp->minode);
1661 kfree(pmp, M_HAMMER2);
1665 lockmgr(&hammer2_mntlk, LK_RELEASE);
1672 hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp)
1674 hammer2_mount_exlock(hmp);
1677 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
1680 * Cycle the volume data lock as a safety (probably not needed any
1681 * more). To ensure everything is out we need to flush at least
1682 * three times. (1) The running of the unlinkq can dirty the
1683 * filesystem, (2) A normal flush can dirty the freemap, and
1684 * (3) ensure that the freemap is fully synchronized.
1686 * The next mount's recovery scan can clean everything up but we want
1687 * to leave the filesystem in a 100% clean state on a normal unmount.
1689 hammer2_voldata_lock(hmp);
1690 hammer2_voldata_unlock(hmp);
1692 hammer2_vfs_sync(mp, MNT_WAIT);
1693 hammer2_vfs_sync(mp, MNT_WAIT);
1694 hammer2_vfs_sync(mp, MNT_WAIT);
1698 * XXX chain depend deadlock?
1700 hammer2_iocom_uninit(hmp);
1702 if (hmp->pmp_count == 0) {
1703 if ((hmp->vchain.flags | hmp->fchain.flags) &
1704 HAMMER2_CHAIN_FLUSH_MASK) {
1705 kprintf("hammer2_unmount: chains left over "
1706 "after final sync\n");
1707 kprintf(" vchain %08x\n", hmp->vchain.flags);
1708 kprintf(" fchain %08x\n", hmp->fchain.flags);
1710 if (hammer2_debug & 0x0010)
1711 Debugger("entered debugger");
1718 hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp)
1720 hammer2_pfsmount_t *spmp;
1721 struct vnode *devvp;
1723 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1726 * If no PFS's left drop the master hammer2_mount for the
1729 if (hmp->pmp_count == 0) {
1731 * Clean up SPMP and the super-root inode
1736 hammer2_inode_drop(spmp->iroot);
1740 kmalloc_destroy(&spmp->mmsg);
1741 kmalloc_destroy(&spmp->minode);
1742 kfree(spmp, M_HAMMER2);
1746 * Finish up with the device vnode
1748 if ((devvp = hmp->devvp) != NULL) {
1749 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1750 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1752 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1759 * Clear vchain/fchain flags that might prevent final cleanup
1762 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1763 atomic_clear_int(&hmp->vchain.flags,
1764 HAMMER2_CHAIN_MODIFIED);
1765 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1766 hammer2_chain_drop(&hmp->vchain);
1768 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1769 atomic_clear_int(&hmp->vchain.flags,
1770 HAMMER2_CHAIN_UPDATE);
1771 hammer2_chain_drop(&hmp->vchain);
1774 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1775 atomic_clear_int(&hmp->fchain.flags,
1776 HAMMER2_CHAIN_MODIFIED);
1777 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1778 hammer2_chain_drop(&hmp->fchain);
1780 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1781 atomic_clear_int(&hmp->fchain.flags,
1782 HAMMER2_CHAIN_UPDATE);
1783 hammer2_chain_drop(&hmp->fchain);
1787 * Final drop of embedded freemap root chain to
1788 * clean up fchain.core (fchain structure is not
1789 * flagged ALLOCATED so it is cleaned out and then
1792 hammer2_chain_drop(&hmp->fchain);
1795 * Final drop of embedded volume root chain to clean
1796 * up vchain.core (vchain structure is not flagged
1797 * ALLOCATED so it is cleaned out and then left to
1801 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1803 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1804 hammer2_mount_unlock(hmp);
1805 hammer2_chain_drop(&hmp->vchain);
1807 hammer2_io_cleanup(hmp, &hmp->iotree);
1808 if (hmp->iofree_count) {
1809 kprintf("io_cleanup: %d I/O's left hanging\n",
1813 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1814 kmalloc_destroy(&hmp->mchain);
1815 kfree(hmp, M_HAMMER2);
1817 hammer2_mount_unlock(hmp);
1823 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1824 ino_t ino, struct vnode **vpp)
1826 kprintf("hammer2_vget\n");
1827 return (EOPNOTSUPP);
1832 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1834 hammer2_pfsmount_t *pmp;
1835 hammer2_cluster_t *cparent;
1840 if (pmp->iroot == NULL) {
1844 cparent = hammer2_inode_lock_sh(pmp->iroot);
1845 vp = hammer2_igetv(pmp->iroot, cparent, &error);
1846 hammer2_inode_unlock_sh(pmp->iroot, cparent);
1849 kprintf("vnodefail\n");
1858 * XXX incorporate ipdata->inode_quota and data_quota
1862 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1864 hammer2_pfsmount_t *pmp;
1865 hammer2_mount_t *hmp;
1868 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1869 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1871 mp->mnt_stat.f_files = pmp->inode_count;
1872 mp->mnt_stat.f_ffree = 0;
1873 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1874 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1875 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1877 *sbp = mp->mnt_stat;
1883 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1885 hammer2_pfsmount_t *pmp;
1886 hammer2_mount_t *hmp;
1889 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1890 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1892 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1893 mp->mnt_vstat.f_files = pmp->inode_count;
1894 mp->mnt_vstat.f_ffree = 0;
1895 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1896 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1897 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1899 *sbp = mp->mnt_vstat;
1904 * Mount-time recovery (RW mounts)
1906 * Updates to the free block table are allowed to lag flushes by one
1907 * transaction. In case of a crash, then on a fresh mount we must do an
1908 * incremental scan of the last committed transaction id and make sure that
1909 * all related blocks have been marked allocated.
1911 * The super-root topology and each PFS has its own transaction id domain,
1912 * so we must track PFS boundary transitions.
1914 struct hammer2_recovery_elm {
1915 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1916 hammer2_chain_t *chain;
1917 hammer2_tid_t sync_tid;
1920 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1922 struct hammer2_recovery_info {
1923 struct hammer2_recovery_list list;
1927 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1928 hammer2_chain_t *parent,
1929 struct hammer2_recovery_info *info,
1930 hammer2_tid_t sync_tid);
1932 #define HAMMER2_RECOVERY_MAXDEPTH 10
1936 hammer2_recovery(hammer2_mount_t *hmp)
1938 hammer2_trans_t trans;
1939 struct hammer2_recovery_info info;
1940 struct hammer2_recovery_elm *elm;
1941 hammer2_chain_t *parent;
1942 hammer2_tid_t sync_tid;
1944 int cumulative_error = 0;
1946 hammer2_trans_init(&trans, hmp->spmp, 0);
1949 TAILQ_INIT(&info.list);
1951 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1952 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent,
1954 hammer2_chain_lookup_done(parent);
1956 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1957 TAILQ_REMOVE(&info.list, elm, entry);
1958 parent = elm->chain;
1959 sync_tid = elm->sync_tid;
1960 kfree(elm, M_HAMMER2);
1962 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1963 HAMMER2_RESOLVE_NOREF);
1964 error = hammer2_recovery_scan(&trans, hmp, parent,
1966 hammer2_chain_unlock(parent);
1968 cumulative_error = error;
1970 hammer2_trans_done(&trans);
1972 return cumulative_error;
1977 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1978 hammer2_chain_t *parent,
1979 struct hammer2_recovery_info *info,
1980 hammer2_tid_t sync_tid)
1982 const hammer2_inode_data_t *ripdata;
1983 hammer2_chain_t *chain;
1985 int cumulative_error = 0;
1986 int pfs_boundary = 0;
1990 * Adjust freemap to ensure that the block(s) are marked allocated.
1992 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1993 hammer2_freemap_adjust(trans, hmp, &parent->bref,
1994 HAMMER2_FREEMAP_DORECOVER);
1998 * Check type for recursive scan
2000 switch(parent->bref.type) {
2001 case HAMMER2_BREF_TYPE_VOLUME:
2002 /* data already instantiated */
2004 case HAMMER2_BREF_TYPE_INODE:
2006 * Must instantiate data for DIRECTDATA test and also
2009 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2010 ripdata = &hammer2_chain_rdata(parent)->ipdata;
2011 if (ripdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2012 /* not applicable to recovery scan */
2013 hammer2_chain_unlock(parent);
2016 if ((ripdata->op_flags & HAMMER2_OPFLAG_PFSROOT) &&
2019 sync_tid = parent->bref.mirror_tid - 1;
2021 hammer2_chain_unlock(parent);
2023 case HAMMER2_BREF_TYPE_INDIRECT:
2025 * Must instantiate data for recursion
2027 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2028 hammer2_chain_unlock(parent);
2030 case HAMMER2_BREF_TYPE_DATA:
2031 case HAMMER2_BREF_TYPE_FREEMAP:
2032 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2033 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2034 /* not applicable to recovery scan */
2042 * Defer operation if depth limit reached or if we are crossing a
2045 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH || pfs_boundary) {
2046 struct hammer2_recovery_elm *elm;
2048 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2049 elm->chain = parent;
2050 elm->sync_tid = sync_tid;
2051 hammer2_chain_ref(parent);
2052 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2053 /* unlocked by caller */
2060 * Recursive scan of the last flushed transaction only. We are
2061 * doing this without pmp assignments so don't leave the chains
2062 * hanging around after we are done with them.
2065 chain = hammer2_chain_scan(parent, NULL, &cache_index,
2066 HAMMER2_LOOKUP_NODATA);
2068 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2069 if (chain->bref.mirror_tid >= sync_tid) {
2071 error = hammer2_recovery_scan(trans, hmp, chain,
2075 cumulative_error = error;
2077 chain = hammer2_chain_scan(parent, chain, &cache_index,
2078 HAMMER2_LOOKUP_NODATA);
2081 return cumulative_error;
2085 * Sync the entire filesystem; this is called from the filesystem syncer
2086 * process periodically and whenever a user calls sync(1) on the hammer
2089 * Currently is actually called from the syncer! \o/
2091 * This task will have to snapshot the state of the dirty inode chain.
2092 * From that, it will have to make sure all of the inodes on the dirty
2093 * chain have IO initiated. We make sure that io is initiated for the root
2096 * If waitfor is set, we wait for media to acknowledge the new rootblock.
2098 * THINKS: side A vs side B, to have sync not stall all I/O?
2101 hammer2_vfs_sync(struct mount *mp, int waitfor)
2103 struct hammer2_sync_info info;
2104 hammer2_inode_t *iroot;
2105 hammer2_chain_t *chain;
2106 hammer2_chain_t *parent;
2107 hammer2_pfsmount_t *pmp;
2108 hammer2_mount_t *hmp;
2119 KKASSERT(iroot->pmp == pmp);
2122 * We can't acquire locks on existing vnodes while in a transaction
2123 * without risking a deadlock. This assumes that vfsync() can be
2124 * called without the vnode locked (which it can in DragonFly).
2125 * Otherwise we'd have to implement a multi-pass or flag the lock
2126 * failures and retry.
2128 * The reclamation code interlocks with the sync list's token
2129 * (by removing the vnode from the scan list) before unlocking
2130 * the inode, giving us time to ref the inode.
2132 /*flags = VMSC_GETVP;*/
2134 if (waitfor & MNT_LAZY)
2135 flags |= VMSC_ONEPASS;
2138 * Start our flush transaction. This does not return until all
2139 * concurrent transactions have completed and will prevent any
2140 * new transactions from running concurrently, except for the
2141 * buffer cache transactions.
2143 * For efficiency do an async pass before making sure with a
2144 * synchronous pass on all related buffer cache buffers. It
2145 * should theoretically not be possible for any new file buffers
2146 * to be instantiated during this sequence.
2148 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
2149 HAMMER2_TRANS_PREFLUSH);
2150 hammer2_run_unlinkq(&info.trans, pmp);
2153 info.waitfor = MNT_NOWAIT;
2154 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2155 info.waitfor = MNT_WAIT;
2156 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2159 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2160 * buffer cache flushes which occur during the flush. Device buffers
2165 if (info.error == 0 && (waitfor & MNT_WAIT)) {
2166 info.waitfor = waitfor;
2167 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2171 hammer2_bioq_sync(info.trans.pmp);
2172 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
2177 * Flush all storage elements making up the cluster
2179 * We must also flush any deleted siblings because the super-root
2180 * flush won't do it for us. They all must be staged or the
2181 * super-root flush will not be able to update its block table
2184 * XXX currently done serially instead of concurrently
2186 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2187 chain = iroot->cluster.array[i];
2189 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
2190 hammer2_flush(&info.trans, chain);
2191 hammer2_chain_unlock(chain);
2195 hammer2_trans_done(&info.trans);
2199 * Flush all volume roots to synchronize PFS flushes with the
2200 * storage media. Use a super-root transaction for each one.
2202 * The flush code will detect super-root -> pfs-root chain
2203 * transitions using the last pfs-root flush.
2205 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2206 chain = iroot->cluster.array[i];
2213 * We only have to flush each hmp once
2215 for (j = i - 1; j >= 0; --j) {
2216 if (iroot->cluster.array[j] &&
2217 iroot->cluster.array[j]->hmp == hmp)
2222 hammer2_trans_spmp(&info.trans, hmp->spmp);
2225 * Force an update of the XID from the PFS root to the
2226 * topology root. We couldn't do this from the PFS
2227 * transaction because a SPMP transaction is needed.
2228 * This does not modify blocks, instead what it does is
2229 * allow the flush code to find the transition point and
2230 * then update on the way back up.
2232 parent = chain->parent;
2233 KKASSERT(chain->pmp != parent->pmp);
2234 hammer2_chain_setflush(&info.trans, parent);
2237 * Media mounts have two 'roots', vchain for the topology
2238 * and fchain for the free block table. Flush both.
2240 * Note that the topology and free block table are handled
2241 * independently, so the free block table can wind up being
2242 * ahead of the topology. We depend on the bulk free scan
2243 * code to deal with any loose ends.
2245 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2246 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2247 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2249 * This will also modify vchain as a side effect,
2250 * mark vchain as modified now.
2252 hammer2_voldata_modify(hmp);
2253 chain = &hmp->fchain;
2254 hammer2_flush(&info.trans, chain);
2255 KKASSERT(chain == &hmp->fchain);
2257 hammer2_chain_unlock(&hmp->fchain);
2258 hammer2_chain_unlock(&hmp->vchain);
2260 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2261 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2262 chain = &hmp->vchain;
2263 hammer2_flush(&info.trans, chain);
2264 KKASSERT(chain == &hmp->vchain);
2269 hammer2_chain_unlock(&hmp->vchain);
2272 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2273 if ((hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) ||
2275 /* this will also modify vchain as a side effect */
2276 chain = &hmp->fchain;
2277 hammer2_flush(&info.trans, chain);
2278 KKASSERT(chain == &hmp->fchain);
2280 hammer2_chain_unlock(&hmp->fchain);
2286 * We can't safely flush the volume header until we have
2287 * flushed any device buffers which have built up.
2289 * XXX this isn't being incremental
2291 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2292 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2293 vn_unlock(hmp->devvp);
2296 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2297 * volume header needs synchronization via hmp->volsync.
2299 * XXX synchronize the flag & data with only this flush XXX
2302 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2306 * Synchronize the disk before flushing the volume
2310 bp->b_bio1.bio_offset = 0;
2313 bp->b_cmd = BUF_CMD_FLUSH;
2314 bp->b_bio1.bio_done = biodone_sync;
2315 bp->b_bio1.bio_flags |= BIO_SYNC;
2316 vn_strategy(hmp->devvp, &bp->b_bio1);
2317 biowait(&bp->b_bio1, "h2vol");
2321 * Then we can safely flush the version of the
2322 * volume header synchronized by the flush code.
2324 i = hmp->volhdrno + 1;
2325 if (i >= HAMMER2_NUM_VOLHDRS)
2327 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2328 hmp->volsync.volu_size) {
2331 kprintf("sync volhdr %d %jd\n",
2332 i, (intmax_t)hmp->volsync.volu_size);
2333 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2334 HAMMER2_PBUFSIZE, 0, 0);
2335 atomic_clear_int(&hmp->vchain.flags,
2336 HAMMER2_CHAIN_VOLUMESYNC);
2337 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2342 total_error = error;
2345 hammer2_trans_done(&info.trans);
2348 hammer2_trans_done(&info.trans);
2350 return (total_error);
2357 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2359 struct hammer2_sync_info *info = data;
2360 hammer2_inode_t *ip;
2369 if (vp->v_type == VNON || vp->v_type == VBAD) {
2373 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2374 RB_EMPTY(&vp->v_rbdirty_tree)) {
2380 * VOP_FSYNC will start a new transaction so replicate some code
2381 * here to do it inline (see hammer2_vop_fsync()).
2383 * WARNING: The vfsync interacts with the buffer cache and might
2384 * block, we can't hold the inode lock at that time.
2385 * However, we MUST ref ip before blocking to ensure that
2386 * it isn't ripped out from under us (since we do not
2387 * hold a lock on the vnode).
2389 hammer2_inode_ref(ip);
2390 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
2392 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
2394 hammer2_inode_drop(ip);
2398 info->error = error;
2405 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2412 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2413 struct fid *fhp, struct vnode **vpp)
2420 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2421 int *exflagsp, struct ucred **credanonp)
2427 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2428 * header into the HMP
2430 * XXX read four volhdrs and use the one with the highest TID whos CRC
2435 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2436 * nonexistant locations.
2438 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2442 hammer2_install_volume_header(hammer2_mount_t *hmp)
2444 hammer2_volume_data_t *vd;
2446 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2458 * There are up to 4 copies of the volume header (syncs iterate
2459 * between them so there is no single master). We don't trust the
2460 * volu_size field so we don't know precisely how large the filesystem
2461 * is, so depend on the OS to return an error if we go beyond the
2462 * block device's EOF.
2464 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2465 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2466 HAMMER2_VOLUME_BYTES, &bp);
2473 vd = (struct hammer2_volume_data *) bp->b_data;
2474 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2475 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2481 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2482 /* XXX: Reversed-endianness filesystem */
2483 kprintf("hammer2: reverse-endian filesystem detected");
2489 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2490 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2491 HAMMER2_VOLUME_ICRC0_SIZE);
2492 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2493 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2494 HAMMER2_VOLUME_ICRC1_SIZE);
2495 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2496 kprintf("hammer2 volume header crc "
2497 "mismatch copy #%d %08x/%08x\n",
2504 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2513 hmp->volsync = hmp->voldata;
2515 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2516 kprintf("hammer2: using volume header #%d\n",
2521 kprintf("hammer2: no valid volume headers found!\n");
2527 * This handles hysteresis on regular file flushes. Because the BIOs are
2528 * routed to a thread it is possible for an excessive number to build up
2529 * and cause long front-end stalls long before the runningbuffspace limit
2530 * is hit, so we implement hammer2_flush_pipe to control the
2533 * This is a particular problem when compression is used.
2536 hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp)
2538 atomic_add_int(&pmp->count_lwinprog, 1);
2542 hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp)
2546 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2547 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2548 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2549 atomic_clear_int(&pmp->count_lwinprog,
2550 HAMMER2_LWINPROG_WAITING);
2551 wakeup(&pmp->count_lwinprog);
2556 hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp)
2561 lwinprog = pmp->count_lwinprog;
2563 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2565 tsleep_interlock(&pmp->count_lwinprog, 0);
2566 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2567 lwinprog = pmp->count_lwinprog;
2568 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2570 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2575 * Manage excessive memory resource use for chain and related
2579 hammer2_pfs_memory_wait(hammer2_pfsmount_t *pmp)
2589 * Atomic check condition and wait. Also do an early speedup of
2590 * the syncer to try to avoid hitting the wait.
2593 waiting = pmp->inmem_dirty_chains;
2595 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2597 limit = pmp->mp->mnt_nvnodelistsize / 10;
2598 if (limit < hammer2_limit_dirty_chains)
2599 limit = hammer2_limit_dirty_chains;
2604 if ((int)(ticks - zzticks) > hz) {
2606 kprintf("count %ld %ld\n", count, limit);
2611 * Block if there are too many dirty chains present, wait
2612 * for the flush to clean some out.
2614 if (count > limit) {
2615 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2616 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2618 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2619 speedup_syncer(pmp->mp);
2620 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2623 continue; /* loop on success or fail */
2627 * Try to start an early flush before we are forced to block.
2629 if (count > limit * 7 / 10)
2630 speedup_syncer(pmp->mp);
2636 hammer2_pfs_memory_inc(hammer2_pfsmount_t *pmp)
2639 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2644 hammer2_pfs_memory_wakeup(hammer2_pfsmount_t *pmp)
2652 waiting = pmp->inmem_dirty_chains;
2654 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2657 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2662 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2663 wakeup(&pmp->inmem_dirty_chains);
2670 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2672 hammer2_chain_t *scan;
2673 hammer2_chain_t *parent;
2677 kprintf("%*.*s...\n", tab, tab, "");
2682 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2684 chain, chain->bref.type,
2685 chain->bref.key, chain->bref.keybits,
2686 chain->bref.mirror_tid);
2688 kprintf("%*.*s [%08x] (%s) refs=%d\n",
2691 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2692 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2695 kprintf("%*.*s core [%08x]",
2699 parent = chain->parent;
2701 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2703 parent, parent->flags, parent->refs);
2704 if (RB_EMPTY(&chain->core.rbtree)) {
2708 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2709 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2710 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2711 kprintf("%*.*s}(%s)\n", tab, tab, "",
2712 chain->data->ipdata.filename);
2714 kprintf("%*.*s}\n", tab, tab, "");