2 * Copyright (c) 2011-2014 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"
63 #include "hammer2_lz4.h"
65 #include "zlib/hammer2_zlib.h"
67 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
69 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
71 struct hammer2_sync_info {
72 hammer2_trans_t trans;
77 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
78 TAILQ_HEAD(hammer2_pfslist, hammer2_pfsmount);
79 static struct hammer2_mntlist hammer2_mntlist;
80 static struct hammer2_pfslist hammer2_pfslist;
81 static struct lock hammer2_mntlk;
84 int hammer2_cluster_enable = 1;
85 int hammer2_hardlink_enable = 1;
86 int hammer2_flush_pipe = 100;
87 int hammer2_synchronous_flush = 1;
88 int hammer2_dio_count;
89 long hammer2_limit_dirty_chains;
90 long hammer2_iod_file_read;
91 long hammer2_iod_meta_read;
92 long hammer2_iod_indr_read;
93 long hammer2_iod_fmap_read;
94 long hammer2_iod_volu_read;
95 long hammer2_iod_file_write;
96 long hammer2_iod_meta_write;
97 long hammer2_iod_indr_write;
98 long hammer2_iod_fmap_write;
99 long hammer2_iod_volu_write;
100 long hammer2_ioa_file_read;
101 long hammer2_ioa_meta_read;
102 long hammer2_ioa_indr_read;
103 long hammer2_ioa_fmap_read;
104 long hammer2_ioa_volu_read;
105 long hammer2_ioa_fmap_write;
106 long hammer2_ioa_file_write;
107 long hammer2_ioa_meta_write;
108 long hammer2_ioa_indr_write;
109 long hammer2_ioa_volu_write;
111 MALLOC_DECLARE(C_BUFFER);
112 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
114 MALLOC_DECLARE(D_BUFFER);
115 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
117 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
119 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
120 &hammer2_debug, 0, "");
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
122 &hammer2_cluster_enable, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
124 &hammer2_hardlink_enable, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
126 &hammer2_flush_pipe, 0, "");
127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
128 &hammer2_synchronous_flush, 0, "");
129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
130 &hammer2_limit_dirty_chains, 0, "");
131 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
132 &hammer2_dio_count, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
135 &hammer2_iod_file_read, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
137 &hammer2_iod_meta_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
139 &hammer2_iod_indr_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
141 &hammer2_iod_fmap_read, 0, "");
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
143 &hammer2_iod_volu_read, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
146 &hammer2_iod_file_write, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
148 &hammer2_iod_meta_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
150 &hammer2_iod_indr_write, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
152 &hammer2_iod_fmap_write, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
154 &hammer2_iod_volu_write, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
157 &hammer2_ioa_file_read, 0, "");
158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
159 &hammer2_ioa_meta_read, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
161 &hammer2_ioa_indr_read, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
163 &hammer2_ioa_fmap_read, 0, "");
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
165 &hammer2_ioa_volu_read, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
168 &hammer2_ioa_file_write, 0, "");
169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
170 &hammer2_ioa_meta_write, 0, "");
171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
172 &hammer2_ioa_indr_write, 0, "");
173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
174 &hammer2_ioa_fmap_write, 0, "");
175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
176 &hammer2_ioa_volu_write, 0, "");
178 static int hammer2_vfs_init(struct vfsconf *conf);
179 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
180 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
182 static int hammer2_remount(hammer2_mount_t *, struct mount *, char *,
183 struct vnode *, struct ucred *);
184 static int hammer2_recovery(hammer2_mount_t *hmp);
185 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
186 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
187 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
189 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
191 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
192 ino_t ino, struct vnode **vpp);
193 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
194 struct fid *fhp, struct vnode **vpp);
195 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
196 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
197 int *exflagsp, struct ucred **credanonp);
199 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
200 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
202 static void hammer2_write_thread(void *arg);
204 static void hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp);
205 static void hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp);
208 * Functions for compression in threads,
209 * from hammer2_vnops.c
211 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
213 hammer2_inode_data_t *ipdata,
214 hammer2_cluster_t *cparent,
215 hammer2_key_t lbase, int ioflag, int pblksize,
217 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
219 const hammer2_inode_data_t *ipdata,
220 hammer2_cluster_t *cparent,
221 hammer2_key_t lbase, int ioflag,
222 int pblksize, int *errorp, int comp_algo);
223 static void hammer2_zero_check_and_write(struct buf *bp,
224 hammer2_trans_t *trans, hammer2_inode_t *ip,
225 const hammer2_inode_data_t *ipdata,
226 hammer2_cluster_t *cparent,
228 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 *ipdata,
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);
239 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
240 static void hammer2_autodmsg(kdmsg_msg_t *msg);
241 static int hammer2_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg);
245 * HAMMER2 vfs operations.
247 static struct vfsops hammer2_vfsops = {
248 .vfs_init = hammer2_vfs_init,
249 .vfs_uninit = hammer2_vfs_uninit,
250 .vfs_sync = hammer2_vfs_sync,
251 .vfs_mount = hammer2_vfs_mount,
252 .vfs_unmount = hammer2_vfs_unmount,
253 .vfs_root = hammer2_vfs_root,
254 .vfs_statfs = hammer2_vfs_statfs,
255 .vfs_statvfs = hammer2_vfs_statvfs,
256 .vfs_vget = hammer2_vfs_vget,
257 .vfs_vptofh = hammer2_vfs_vptofh,
258 .vfs_fhtovp = hammer2_vfs_fhtovp,
259 .vfs_checkexp = hammer2_vfs_checkexp
262 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
264 VFS_SET(hammer2_vfsops, hammer2, 0);
265 MODULE_VERSION(hammer2, 1);
269 hammer2_vfs_init(struct vfsconf *conf)
271 static struct objcache_malloc_args margs_read;
272 static struct objcache_malloc_args margs_write;
278 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
280 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
282 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
286 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
288 margs_read.objsize = 65536;
289 margs_read.mtype = D_BUFFER;
291 margs_write.objsize = 32768;
292 margs_write.mtype = C_BUFFER;
294 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
295 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
296 objcache_malloc_free, &margs_read);
297 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
298 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
299 objcache_malloc_free, &margs_write);
301 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
302 TAILQ_INIT(&hammer2_mntlist);
303 TAILQ_INIT(&hammer2_pfslist);
305 hammer2_limit_dirty_chains = desiredvnodes / 10;
307 hammer2_trans_manage_init();
314 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
316 objcache_destroy(cache_buffer_read);
317 objcache_destroy(cache_buffer_write);
322 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
323 * mounts and the spmp structure for media (hmp) structures.
325 static hammer2_pfsmount_t *
326 hammer2_pfsalloc(const hammer2_inode_data_t *ipdata, hammer2_tid_t alloc_tid)
328 hammer2_pfsmount_t *pmp;
330 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
331 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
332 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
333 lockinit(&pmp->lock, "pfslk", 0, 0);
334 spin_init(&pmp->inum_spin);
335 RB_INIT(&pmp->inum_tree);
336 TAILQ_INIT(&pmp->unlinkq);
337 spin_init(&pmp->list_spin);
339 pmp->alloc_tid = alloc_tid + 1; /* our first media transaction id */
340 pmp->flush_tid = pmp->alloc_tid;
342 pmp->inode_tid = ipdata->pfs_inum + 1;
343 pmp->pfs_clid = ipdata->pfs_clid;
345 mtx_init(&pmp->wthread_mtx);
346 bioq_init(&pmp->wthread_bioq);
352 * Mount or remount HAMMER2 fileystem from physical media
355 * mp mount point structure
361 * mp mount point structure
362 * path path to mount point
363 * data pointer to argument structure in user space
364 * volume volume path (device@LABEL form)
365 * hflags user mount flags
366 * cred user credentials
373 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
376 struct hammer2_mount_info info;
377 hammer2_pfsmount_t *pmp;
378 hammer2_pfsmount_t *spmp;
379 hammer2_mount_t *hmp;
380 hammer2_key_t key_next;
381 hammer2_key_t key_dummy;
384 struct nlookupdata nd;
385 hammer2_chain_t *parent;
386 hammer2_chain_t *rchain;
387 hammer2_cluster_t *cluster;
388 hammer2_cluster_t *cparent;
389 const hammer2_inode_data_t *ipdata;
390 hammer2_blockref_t bref;
392 char devstr[MNAMELEN];
410 kprintf("hammer2_mount\n");
416 bzero(&info, sizeof(info));
417 info.cluster_fd = -1;
421 * Non-root mount or updating a mount
423 error = copyin(data, &info, sizeof(info));
427 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
431 /* Extract device and label */
433 label = strchr(devstr, '@');
435 ((label + 1) - dev) > done) {
443 if (mp->mnt_flag & MNT_UPDATE) {
445 /* HAMMER2 implements NFS export via mountctl */
447 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
448 hmp = pmp->iroot->cluster.array[i]->hmp;
450 error = hammer2_remount(hmp, mp, path,
455 /*hammer2_inode_install_hidden(pmp);*/
464 * Lookup name and verify it refers to a block device.
466 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
468 error = nlookup(&nd);
470 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
474 if (vn_isdisk(devvp, &error))
475 error = vfs_mountedon(devvp);
479 * Determine if the device has already been mounted. After this
480 * check hmp will be non-NULL if we are doing the second or more
481 * hammer2 mounts from the same device.
483 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
484 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
485 if (hmp->devvp == devvp)
490 * Open the device if this isn't a secondary mount and construct
491 * the H2 device mount (hmp).
494 hammer2_chain_t *schain;
497 if (error == 0 && vcount(devvp) > 0)
501 * Now open the device
504 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
505 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
506 error = vinvalbuf(devvp, V_SAVE, 0, 0);
508 error = VOP_OPEN(devvp,
509 ronly ? FREAD : FREAD | FWRITE,
514 if (error && devvp) {
519 lockmgr(&hammer2_mntlk, LK_RELEASE);
522 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
525 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
526 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
527 RB_INIT(&hmp->iotree);
528 spin_init(&hmp->io_spin);
529 spin_init(&hmp->list_spin);
530 TAILQ_INIT(&hmp->flushq);
532 lockinit(&hmp->vollk, "h2vol", 0, 0);
535 * vchain setup. vchain.data is embedded.
536 * vchain.refs is initialized and will never drop to 0.
538 * NOTE! voldata is not yet loaded.
540 hmp->vchain.hmp = hmp;
541 hmp->vchain.refs = 1;
542 hmp->vchain.data = (void *)&hmp->voldata;
543 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
544 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
545 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
547 hammer2_chain_core_alloc(NULL, &hmp->vchain);
548 /* hmp->vchain.u.xxx is left NULL */
551 * fchain setup. fchain.data is embedded.
552 * fchain.refs is initialized and will never drop to 0.
554 * The data is not used but needs to be initialized to
555 * pass assertion muster. We use this chain primarily
556 * as a placeholder for the freemap's top-level RBTREE
557 * so it does not interfere with the volume's topology
560 hmp->fchain.hmp = hmp;
561 hmp->fchain.refs = 1;
562 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
563 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
564 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
565 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
566 hmp->fchain.bref.methods =
567 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
568 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
570 hammer2_chain_core_alloc(NULL, &hmp->fchain);
571 /* hmp->fchain.u.xxx is left NULL */
574 * Install the volume header and initialize fields from
577 error = hammer2_install_volume_header(hmp);
580 hammer2_vfs_unmount_hmp1(mp, hmp);
581 hammer2_vfs_unmount_hmp2(mp, hmp);
582 lockmgr(&hammer2_mntlk, LK_RELEASE);
583 hammer2_vfs_unmount(mp, MNT_FORCE);
588 * Really important to get these right or flush will get
591 hmp->spmp = hammer2_pfsalloc(NULL, hmp->voldata.mirror_tid);
592 kprintf("alloc spmp %p tid %016jx\n",
593 hmp->spmp, hmp->voldata.mirror_tid);
598 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
599 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
600 hmp->vchain.pmp = spmp;
601 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
602 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
603 hmp->fchain.pmp = spmp;
606 * First locate the super-root inode, which is key 0
607 * relative to the volume header's blockset.
609 * Then locate the root inode by scanning the directory keyspace
610 * represented by the label.
612 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
613 schain = hammer2_chain_lookup(&parent, &key_dummy,
614 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
615 &cache_index, 0, &ddflag);
616 hammer2_chain_lookup_done(parent);
617 if (schain == NULL) {
618 kprintf("hammer2_mount: invalid super-root\n");
620 hammer2_vfs_unmount_hmp1(mp, hmp);
621 hammer2_vfs_unmount_hmp2(mp, hmp);
622 lockmgr(&hammer2_mntlk, LK_RELEASE);
623 hammer2_vfs_unmount(mp, MNT_FORCE);
628 * Sanity-check schain's pmp, finish initializing spmp.
630 KKASSERT(schain->pmp == spmp);
631 spmp->pfs_clid = schain->data->ipdata.pfs_clid;
634 * NOTE: The CHAIN_PFSROOT is not set on the super-root inode.
635 * NOTE: inode_get sucks up schain's lock.
637 cluster = hammer2_cluster_from_chain(schain);
638 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
639 spmp->spmp_hmp = hmp;
640 hammer2_inode_ref(spmp->iroot);
641 hammer2_inode_unlock_ex(spmp->iroot, cluster);
643 /* leave spmp->iroot with one ref */
645 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
646 error = hammer2_recovery(hmp);
647 /* XXX do something with error */
652 * XXX RDONLY stuff is totally broken FIXME XXX
655 * Automatic handling of received LNK_SPAN
656 * Automatic handling of received LNK_CIRC
657 * No automatic LNK_SPAN generation - we do this ourselves
658 * No automatic LNK_CIRC generation - we do this ourselves
660 kdmsg_iocom_init(&hmp->iocom, hmp,
661 KDMSG_IOCOMF_AUTOCONN |
662 KDMSG_IOCOMF_AUTORXSPAN,
663 hmp->mchain, hammer2_rcvdmsg);
666 * Ref the cluster management messaging descriptor. The mount
667 * program deals with the other end of the communications pipe.
669 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
671 hammer2_cluster_reconnect(hmp, fp);
673 kprintf("hammer2_mount: bad cluster_fd!\n");
681 * Lookup mount point under the media-localized super-root.
683 * cluster->pmp will incorrectly point to spmp and must be fixed
686 cparent = hammer2_inode_lock_ex(spmp->iroot);
687 lhc = hammer2_dirhash(label, strlen(label));
688 cluster = hammer2_cluster_lookup(cparent, &key_next,
689 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
692 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
694 hammer2_cluster_data(cluster)->ipdata.filename) == 0) {
697 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
699 lhc + HAMMER2_DIRHASH_LOMASK, 0);
701 hammer2_inode_unlock_ex(spmp->iroot, cparent);
703 if (cluster == NULL) {
704 kprintf("hammer2_mount: PFS label not found\n");
705 hammer2_vfs_unmount_hmp1(mp, hmp);
706 hammer2_vfs_unmount_hmp2(mp, hmp);
707 lockmgr(&hammer2_mntlk, LK_RELEASE);
708 hammer2_vfs_unmount(mp, MNT_FORCE);
712 for (i = 0; i < cluster->nchains; ++i) {
713 rchain = cluster->array[i];
714 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
715 kprintf("hammer2_mount: PFS label already mounted!\n");
716 hammer2_cluster_unlock(cluster);
717 hammer2_vfs_unmount_hmp1(mp, hmp);
718 hammer2_vfs_unmount_hmp2(mp, hmp);
719 lockmgr(&hammer2_mntlk, LK_RELEASE);
720 hammer2_vfs_unmount(mp, MNT_FORCE);
723 KKASSERT(rchain->pmp == NULL);
725 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
726 kprintf("hammer2_mount: PFS label is recycling\n");
727 hammer2_cluster_unlock(cluster);
728 hammer2_vfs_unmount_hmp1(mp, hmp);
729 hammer2_vfs_unmount_hmp2(mp, hmp);
730 lockmgr(&hammer2_mntlk, LK_RELEASE);
731 hammer2_vfs_unmount(mp, MNT_FORCE);
738 * Check to see if the cluster id is already mounted at the mount
739 * point. If it is, add us to the cluster.
741 ipdata = &hammer2_cluster_data(cluster)->ipdata;
742 hammer2_cluster_bref(cluster, &bref);
743 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
744 if (pmp->spmp_hmp == NULL &&
745 bcmp(&pmp->pfs_clid, &ipdata->pfs_clid,
746 sizeof(pmp->pfs_clid)) == 0) {
755 hammer2_inode_ref(pmp->iroot);
756 ccms_thread_lock(&pmp->iroot->topo_cst, CCMS_STATE_EXCLUSIVE);
758 if (pmp->iroot->cluster.nchains + cluster->nchains >
759 HAMMER2_MAXCLUSTER) {
760 kprintf("hammer2_mount: cluster full!\n");
762 ccms_thread_unlock(&pmp->iroot->topo_cst);
763 hammer2_inode_drop(pmp->iroot);
765 hammer2_cluster_unlock(cluster);
766 hammer2_vfs_unmount_hmp1(mp, hmp);
767 hammer2_vfs_unmount_hmp2(mp, hmp);
768 lockmgr(&hammer2_mntlk, LK_RELEASE);
769 hammer2_vfs_unmount(mp, MNT_FORCE);
772 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n");
773 j = pmp->iroot->cluster.nchains;
774 for (i = 0; i < cluster->nchains; ++i) {
775 rchain = cluster->array[i];
776 KKASSERT(rchain->pmp == NULL);
778 hammer2_chain_ref(cluster->array[i]);
779 pmp->iroot->cluster.array[j] = cluster->array[i];
782 pmp->iroot->cluster.nchains = j;
783 ccms_thread_unlock(&pmp->iroot->topo_cst);
784 hammer2_inode_drop(pmp->iroot);
785 hammer2_cluster_unlock(cluster);
786 lockmgr(&hammer2_mntlk, LK_RELEASE);
789 hammer2_inode_install_hidden(pmp);
795 * Block device opened successfully, finish initializing the
798 * From this point on we have to call hammer2_unmount() on failure.
800 pmp = hammer2_pfsalloc(ipdata, bref.mirror_tid);
801 kprintf("PMP mirror_tid is %016jx\n", bref.mirror_tid);
802 for (i = 0; i < cluster->nchains; ++i) {
803 rchain = cluster->array[i];
804 KKASSERT(rchain->pmp == NULL);
806 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
810 ccms_domain_init(&pmp->ccms_dom);
811 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
812 lockmgr(&hammer2_mntlk, LK_RELEASE);
814 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
815 hmp, pmp, hmp->pmp_count);
817 mp->mnt_flag = MNT_LOCAL;
818 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
819 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
822 * required mount structure initializations
824 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
825 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
827 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
828 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
833 mp->mnt_iosize_max = MAXPHYS;
834 mp->mnt_data = (qaddr_t)pmp;
838 * After this point hammer2_vfs_unmount() has visibility on hmp
839 * and manual hmp1/hmp2 calls are not needed on fatal errors.
841 pmp->iroot = hammer2_inode_get(pmp, NULL, cluster);
842 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
843 hammer2_inode_unlock_ex(pmp->iroot, cluster);
846 * The logical file buffer bio write thread handles things
847 * like physical block assignment and compression.
849 * (only applicable to pfs mounts, not applicable to spmp)
851 pmp->wthread_destroy = 0;
852 lwkt_create(hammer2_write_thread, pmp,
853 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
856 * With the cluster operational install ihidden.
857 * (only applicable to pfs mounts, not applicable to spmp)
859 hammer2_inode_install_hidden(pmp);
865 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
866 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
867 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
869 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
870 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
871 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
872 copyinstr(path, mp->mnt_stat.f_mntonname,
873 sizeof(mp->mnt_stat.f_mntonname) - 1,
877 * Initial statfs to prime mnt_stat.
879 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
885 * Handle bioq for strategy write
889 hammer2_write_thread(void *arg)
891 hammer2_pfsmount_t *pmp;
894 hammer2_trans_t trans;
897 hammer2_cluster_t *cparent;
898 hammer2_inode_data_t *wipdata;
906 mtx_lock(&pmp->wthread_mtx);
907 while (pmp->wthread_destroy == 0) {
908 if (bioq_first(&pmp->wthread_bioq) == NULL) {
909 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
914 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
916 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
918 * dummy bio for synchronization. The transaction
919 * must be reinitialized.
921 if (bio->bio_buf == NULL) {
922 bio->bio_flags |= BIO_DONE;
924 hammer2_trans_done(&trans);
925 hammer2_trans_init(&trans, pmp,
926 HAMMER2_TRANS_BUFCACHE);
931 * else normal bio processing
933 mtx_unlock(&pmp->wthread_mtx);
935 hammer2_lwinprog_drop(pmp);
943 * Inode is modified, flush size and mtime changes
944 * to ensure that the file size remains consistent
945 * with the buffers being flushed.
947 * NOTE: The inode_fsync() call only flushes the
948 * inode's meta-data state, it doesn't try
949 * to flush underlying buffers or chains.
951 cparent = hammer2_inode_lock_ex(ip);
952 if (ip->flags & (HAMMER2_INODE_RESIZED |
953 HAMMER2_INODE_MTIME)) {
954 hammer2_inode_fsync(&trans, ip, cparent);
956 wipdata = hammer2_cluster_modify_ip(&trans, ip,
958 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
960 pblksize = hammer2_calc_physical(ip, wipdata, lbase);
961 hammer2_write_file_core(bp, &trans, ip, wipdata,
965 hammer2_cluster_modsync(cparent);
966 hammer2_inode_unlock_ex(ip, cparent);
968 kprintf("hammer2: error in buffer write\n");
969 bp->b_flags |= B_ERROR;
973 mtx_lock(&pmp->wthread_mtx);
975 hammer2_trans_done(&trans);
977 pmp->wthread_destroy = -1;
978 wakeup(&pmp->wthread_destroy);
980 mtx_unlock(&pmp->wthread_mtx);
984 hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
988 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
989 mtx_lock(&pmp->wthread_mtx);
990 if (pmp->wthread_destroy == 0 &&
991 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
992 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
993 while ((sync_bio.bio_flags & BIO_DONE) == 0)
994 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
996 mtx_unlock(&pmp->wthread_mtx);
1000 * Return a chain suitable for I/O, creating the chain if necessary
1001 * and assigning its physical block.
1005 hammer2_assign_physical(hammer2_trans_t *trans,
1006 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1007 hammer2_key_t lbase, int pblksize, int *errorp)
1009 hammer2_cluster_t *cluster;
1010 hammer2_cluster_t *dparent;
1011 hammer2_key_t key_dummy;
1012 int pradix = hammer2_getradix(pblksize);
1016 * Locate the chain associated with lbase, return a locked chain.
1017 * However, do not instantiate any data reference (which utilizes a
1018 * device buffer) because we will be using direct IO via the
1019 * logical buffer cache buffer.
1022 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
1024 dparent = hammer2_cluster_lookup_init(cparent, 0);
1025 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
1027 HAMMER2_LOOKUP_NODATA, &ddflag);
1029 if (cluster == NULL) {
1031 * We found a hole, create a new chain entry.
1033 * NOTE: DATA chains are created without device backing
1034 * store (nor do we want any).
1036 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
1037 lbase, HAMMER2_PBUFRADIX,
1038 HAMMER2_BREF_TYPE_DATA,
1040 if (cluster == NULL) {
1041 hammer2_cluster_lookup_done(dparent);
1042 panic("hammer2_cluster_create: par=%p error=%d\n",
1043 dparent->focus, *errorp);
1046 /*ip->delta_dcount += pblksize;*/
1048 switch (hammer2_cluster_type(cluster)) {
1049 case HAMMER2_BREF_TYPE_INODE:
1051 * The data is embedded in the inode. The
1052 * caller is responsible for marking the inode
1053 * modified and copying the data to the embedded
1057 case HAMMER2_BREF_TYPE_DATA:
1058 if (hammer2_cluster_bytes(cluster) != pblksize) {
1059 hammer2_cluster_resize(trans, ip,
1062 HAMMER2_MODIFY_OPTDATA);
1064 hammer2_cluster_modify(trans, cluster,
1065 HAMMER2_MODIFY_OPTDATA);
1068 panic("hammer2_assign_physical: bad type");
1075 * Cleanup. If cluster wound up being the inode itself, i.e.
1076 * the DIRECTDATA case for offset 0, then we need to update cparent.
1077 * The caller expects cparent to not become stale.
1079 hammer2_cluster_lookup_done(dparent);
1080 /* dparent = NULL; safety */
1081 if (cluster && ddflag)
1082 hammer2_cluster_replace_locked(cparent, cluster);
1087 * From hammer2_vnops.c.
1088 * The core write function which determines which path to take
1089 * depending on compression settings.
1093 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
1094 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
1095 hammer2_cluster_t *cparent,
1096 hammer2_key_t lbase, int ioflag, int pblksize,
1099 hammer2_cluster_t *cluster;
1101 switch(HAMMER2_DEC_COMP(ipdata->comp_algo)) {
1102 case HAMMER2_COMP_NONE:
1104 * We have to assign physical storage to the buffer
1105 * we intend to dirty or write now to avoid deadlocks
1106 * in the strategy code later.
1108 * This can return NOOFFSET for inode-embedded data.
1109 * The strategy code will take care of it in that case.
1111 cluster = hammer2_assign_physical(trans, ip, cparent,
1114 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp);
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 ipdata, cparent, lbase,
1124 ioflag, pblksize, errorp);
1126 case HAMMER2_COMP_LZ4:
1127 case HAMMER2_COMP_ZLIB:
1130 * Check for zero-fill and attempt compression.
1132 hammer2_compress_and_write(bp, trans, ip,
1142 * Generic function that will perform the compression in compression
1143 * write path. The compression algorithm is determined by the settings
1144 * obtained from inode.
1148 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
1149 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
1150 hammer2_cluster_t *cparent,
1151 hammer2_key_t lbase, int ioflag, int pblksize,
1152 int *errorp, int comp_algo)
1154 hammer2_cluster_t *cluster;
1155 hammer2_chain_t *chain;
1157 int comp_block_size;
1161 if (test_block_zeros(bp->b_data, pblksize)) {
1162 zero_write(bp, trans, ip, ipdata, cparent, lbase, errorp);
1169 KKASSERT(pblksize / 2 <= 32768);
1171 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1172 z_stream strm_compress;
1176 switch(HAMMER2_DEC_COMP(comp_algo)) {
1177 case HAMMER2_COMP_LZ4:
1178 comp_buffer = objcache_get(cache_buffer_write,
1180 comp_size = LZ4_compress_limitedOutput(
1182 &comp_buffer[sizeof(int)],
1184 pblksize / 2 - sizeof(int));
1186 * We need to prefix with the size, LZ4
1187 * doesn't do it for us. Add the related
1190 *(int *)comp_buffer = comp_size;
1192 comp_size += sizeof(int);
1194 case HAMMER2_COMP_ZLIB:
1195 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1196 if (comp_level == 0)
1197 comp_level = 6; /* default zlib compression */
1198 else if (comp_level < 6)
1200 else if (comp_level > 9)
1202 ret = deflateInit(&strm_compress, comp_level);
1204 kprintf("HAMMER2 ZLIB: fatal error "
1205 "on deflateInit.\n");
1208 comp_buffer = objcache_get(cache_buffer_write,
1210 strm_compress.next_in = bp->b_data;
1211 strm_compress.avail_in = pblksize;
1212 strm_compress.next_out = comp_buffer;
1213 strm_compress.avail_out = pblksize / 2;
1214 ret = deflate(&strm_compress, Z_FINISH);
1215 if (ret == Z_STREAM_END) {
1216 comp_size = pblksize / 2 -
1217 strm_compress.avail_out;
1221 ret = deflateEnd(&strm_compress);
1224 kprintf("Error: Unknown compression method.\n");
1225 kprintf("Comp_method = %d.\n", comp_algo);
1230 if (comp_size == 0) {
1232 * compression failed or turned off
1234 comp_block_size = pblksize; /* safety */
1235 if (++ip->comp_heuristic > 128)
1236 ip->comp_heuristic = 8;
1239 * compression succeeded
1241 ip->comp_heuristic = 0;
1242 if (comp_size <= 1024) {
1243 comp_block_size = 1024;
1244 } else if (comp_size <= 2048) {
1245 comp_block_size = 2048;
1246 } else if (comp_size <= 4096) {
1247 comp_block_size = 4096;
1248 } else if (comp_size <= 8192) {
1249 comp_block_size = 8192;
1250 } else if (comp_size <= 16384) {
1251 comp_block_size = 16384;
1252 } else if (comp_size <= 32768) {
1253 comp_block_size = 32768;
1255 panic("hammer2: WRITE PATH: "
1256 "Weird comp_size value.");
1258 comp_block_size = pblksize;
1262 cluster = hammer2_assign_physical(trans, ip, cparent,
1263 lbase, comp_block_size,
1265 ipdata = &hammer2_cluster_data(cparent)->ipdata;
1268 kprintf("WRITE PATH: An error occurred while "
1269 "assigning physical space.\n");
1270 KKASSERT(cluster == NULL);
1274 for (i = 0; i < cluster->nchains; ++i) {
1279 chain = cluster->array[i];
1280 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1282 switch(chain->bref.type) {
1283 case HAMMER2_BREF_TYPE_INODE:
1284 KKASSERT(chain->data->ipdata.op_flags &
1285 HAMMER2_OPFLAG_DIRECTDATA);
1286 KKASSERT(bp->b_loffset == 0);
1287 bcopy(bp->b_data, chain->data->ipdata.u.data,
1288 HAMMER2_EMBEDDED_BYTES);
1290 case HAMMER2_BREF_TYPE_DATA:
1291 temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
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(temp_check);
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(temp_check);
1327 bcopy(bp->b_data, bdata, pblksize);
1331 * Device buffer is now valid, chain is no longer in
1332 * the initial state.
1334 * (No blockref table worries with file data)
1336 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1338 /* Now write the related bdp. */
1339 if (ioflag & IO_SYNC) {
1341 * Synchronous I/O requested.
1343 hammer2_io_bwrite(&dio);
1345 } else if ((ioflag & IO_DIRECT) &&
1346 loff + n == pblksize) {
1347 hammer2_io_bdwrite(&dio);
1349 } else if (ioflag & IO_ASYNC) {
1350 hammer2_io_bawrite(&dio);
1352 hammer2_io_bdwrite(&dio);
1356 panic("hammer2_write_bp: bad chain type %d\n",
1364 hammer2_cluster_unlock(cluster);
1366 objcache_put(cache_buffer_write, comp_buffer);
1370 * Function that performs zero-checking and writing without compression,
1371 * it corresponds to default zero-checking path.
1375 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1376 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
1377 hammer2_cluster_t *cparent,
1378 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp)
1380 hammer2_cluster_t *cluster;
1382 if (test_block_zeros(bp->b_data, pblksize)) {
1383 zero_write(bp, trans, ip, ipdata, cparent, lbase, errorp);
1385 cluster = hammer2_assign_physical(trans, ip, cparent,
1386 lbase, pblksize, errorp);
1387 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp);
1389 hammer2_cluster_unlock(cluster);
1394 * A function to test whether a block of data contains only zeros,
1395 * returns TRUE (non-zero) if the block is all zeros.
1399 test_block_zeros(const char *buf, size_t bytes)
1403 for (i = 0; i < bytes; i += sizeof(long)) {
1404 if (*(const long *)(buf + i) != 0)
1411 * Function to "write" a block that contains only zeros.
1415 zero_write(struct buf *bp, hammer2_trans_t *trans,
1416 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
1417 hammer2_cluster_t *cparent,
1418 hammer2_key_t lbase, int *errorp __unused)
1420 hammer2_cluster_t *cluster;
1421 hammer2_media_data_t *data;
1422 hammer2_key_t key_dummy;
1425 cparent = hammer2_cluster_lookup_init(cparent, 0);
1426 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1427 HAMMER2_LOOKUP_NODATA, &ddflag);
1429 data = hammer2_cluster_wdata(cluster);
1432 KKASSERT(cluster->focus->flags &
1433 HAMMER2_CHAIN_MODIFIED);
1434 bzero(data->ipdata.u.data, HAMMER2_EMBEDDED_BYTES);
1435 hammer2_cluster_modsync(cluster);
1437 hammer2_cluster_delete(trans, cparent, cluster,
1438 HAMMER2_DELETE_PERMANENT);
1440 hammer2_cluster_unlock(cluster);
1442 hammer2_cluster_lookup_done(cparent);
1446 * Function to write the data as it is, without performing any sort of
1447 * compression. This function is used in path without compression and
1448 * default zero-checking path.
1452 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1453 int pblksize, int *errorp)
1455 hammer2_chain_t *chain;
1462 error = 0; /* XXX TODO below */
1464 for (i = 0; i < cluster->nchains; ++i) {
1465 chain = cluster->array[i];
1467 temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1469 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1471 switch(chain->bref.type) {
1472 case HAMMER2_BREF_TYPE_INODE:
1473 KKASSERT(chain->data->ipdata.op_flags &
1474 HAMMER2_OPFLAG_DIRECTDATA);
1475 KKASSERT(bp->b_loffset == 0);
1476 bcopy(bp->b_data, chain->data->ipdata.u.data,
1477 HAMMER2_EMBEDDED_BYTES);
1480 case HAMMER2_BREF_TYPE_DATA:
1481 error = hammer2_io_newnz(chain->hmp,
1482 chain->bref.data_off,
1483 chain->bytes, &dio);
1485 hammer2_io_bqrelse(&dio);
1486 kprintf("hammer2: WRITE PATH: "
1487 "dbp bread error\n");
1490 bdata = hammer2_io_data(dio, chain->bref.data_off);
1492 chain->bref.methods = HAMMER2_ENC_COMP(
1493 HAMMER2_COMP_NONE) +
1494 HAMMER2_ENC_CHECK(temp_check);
1495 bcopy(bp->b_data, bdata, chain->bytes);
1498 * Device buffer is now valid, chain is no longer in
1499 * the initial state.
1501 * (No blockref table worries with file data)
1503 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1505 if (ioflag & IO_SYNC) {
1507 * Synchronous I/O requested.
1509 hammer2_io_bwrite(&dio);
1511 } else if ((ioflag & IO_DIRECT) &&
1512 loff + n == pblksize) {
1513 hammer2_io_bdwrite(&dio);
1515 } else if (ioflag & IO_ASYNC) {
1516 hammer2_io_bawrite(&dio);
1518 hammer2_io_bdwrite(&dio);
1522 panic("hammer2_write_bp: bad chain type %d\n",
1528 KKASSERT(error == 0); /* XXX TODO */
1535 hammer2_remount(hammer2_mount_t *hmp, struct mount *mp, char *path,
1536 struct vnode *devvp, struct ucred *cred)
1540 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1541 error = hammer2_recovery(hmp);
1550 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1552 hammer2_pfsmount_t *pmp;
1553 hammer2_mount_t *hmp;
1554 hammer2_chain_t *rchain;
1555 hammer2_cluster_t *cluster;
1565 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1566 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
1569 * If mount initialization proceeded far enough we must flush
1572 if (mntflags & MNT_FORCE)
1577 error = vflush(mp, 0, flags);
1582 ccms_domain_uninit(&pmp->ccms_dom);
1584 if (pmp->wthread_td) {
1585 mtx_lock(&pmp->wthread_mtx);
1586 pmp->wthread_destroy = 1;
1587 wakeup(&pmp->wthread_bioq);
1588 while (pmp->wthread_destroy != -1) {
1589 mtxsleep(&pmp->wthread_destroy,
1590 &pmp->wthread_mtx, 0,
1593 mtx_unlock(&pmp->wthread_mtx);
1594 pmp->wthread_td = NULL;
1598 * Cleanup our reference on ihidden.
1601 hammer2_inode_drop(pmp->ihidden);
1602 pmp->ihidden = NULL;
1606 * Cleanup our reference on iroot. iroot is (should) not be needed
1607 * by the flush code.
1610 cluster = &pmp->iroot->cluster;
1611 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1612 rchain = pmp->iroot->cluster.array[i];
1616 hammer2_vfs_unmount_hmp1(mp, hmp);
1618 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
1619 #if REPORT_REFS_ERRORS
1620 if (rchain->refs != 1)
1621 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1622 rchain, rchain->refs);
1624 KKASSERT(rchain->refs == 1);
1626 hammer2_chain_drop(rchain);
1627 cluster->array[i] = NULL;
1628 hammer2_vfs_unmount_hmp2(mp, hmp);
1630 cluster->focus = NULL;
1632 #if REPORT_REFS_ERRORS
1633 if (pmp->iroot->refs != 1)
1634 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1635 pmp->iroot, pmp->iroot->refs);
1637 KKASSERT(pmp->iroot->refs == 1);
1639 /* ref for pmp->iroot */
1640 hammer2_inode_drop(pmp->iroot);
1645 mp->mnt_data = NULL;
1647 kmalloc_destroy(&pmp->mmsg);
1648 kmalloc_destroy(&pmp->minode);
1650 kfree(pmp, M_HAMMER2);
1654 lockmgr(&hammer2_mntlk, LK_RELEASE);
1661 hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp)
1663 hammer2_mount_exlock(hmp);
1666 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
1668 kdmsg_iocom_uninit(&hmp->iocom); /* XXX chain depend deadlck? */
1671 * Cycle the volume data lock as a safety (probably not needed any
1672 * more). To ensure everything is out we need to flush at least
1673 * three times. (1) The running of the unlinkq can dirty the
1674 * filesystem, (2) A normal flush can dirty the freemap, and
1675 * (3) ensure that the freemap is fully synchronized.
1677 * The next mount's recovery scan can clean everything up but we want
1678 * to leave the filesystem in a 100% clean state on a normal unmount.
1680 hammer2_voldata_lock(hmp);
1681 hammer2_voldata_unlock(hmp);
1682 hammer2_vfs_sync(mp, MNT_WAIT);
1683 hammer2_vfs_sync(mp, MNT_WAIT);
1684 hammer2_vfs_sync(mp, MNT_WAIT);
1686 if (hmp->pmp_count == 0) {
1687 if ((hmp->vchain.flags | hmp->fchain.flags) &
1688 HAMMER2_CHAIN_FLUSH_MASK) {
1689 kprintf("hammer2_unmount: chains left over "
1690 "after final sync\n");
1691 kprintf(" vchain %08x\n", hmp->vchain.flags);
1692 kprintf(" fchain %08x\n", hmp->fchain.flags);
1694 if (hammer2_debug & 0x0010)
1695 Debugger("entered debugger");
1702 hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp)
1704 hammer2_pfsmount_t *spmp;
1705 struct vnode *devvp;
1707 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1710 * If no PFS's left drop the master hammer2_mount for the
1713 if (hmp->pmp_count == 0) {
1715 * Clean up SPMP and the super-root inode
1720 hammer2_inode_drop(spmp->iroot);
1724 kmalloc_destroy(&spmp->mmsg);
1725 kmalloc_destroy(&spmp->minode);
1726 kfree(spmp, M_HAMMER2);
1730 * Finish up with the device vnode
1732 if ((devvp = hmp->devvp) != NULL) {
1733 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1734 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1736 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1743 * Clear vchain/fchain flags that might prevent final cleanup
1746 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1747 atomic_clear_int(&hmp->vchain.flags,
1748 HAMMER2_CHAIN_MODIFIED);
1749 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1750 hammer2_chain_drop(&hmp->vchain);
1752 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1753 atomic_clear_int(&hmp->vchain.flags,
1754 HAMMER2_CHAIN_UPDATE);
1755 hammer2_chain_drop(&hmp->vchain);
1758 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1759 atomic_clear_int(&hmp->fchain.flags,
1760 HAMMER2_CHAIN_MODIFIED);
1761 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1762 hammer2_chain_drop(&hmp->fchain);
1764 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1765 atomic_clear_int(&hmp->fchain.flags,
1766 HAMMER2_CHAIN_UPDATE);
1767 hammer2_chain_drop(&hmp->fchain);
1771 * Final drop of embedded freemap root chain to
1772 * clean up fchain.core (fchain structure is not
1773 * flagged ALLOCATED so it is cleaned out and then
1776 hammer2_chain_drop(&hmp->fchain);
1779 * Final drop of embedded volume root chain to clean
1780 * up vchain.core (vchain structure is not flagged
1781 * ALLOCATED so it is cleaned out and then left to
1785 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1787 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1788 hammer2_mount_unlock(hmp);
1789 hammer2_chain_drop(&hmp->vchain);
1791 hammer2_io_cleanup(hmp, &hmp->iotree);
1792 if (hmp->iofree_count) {
1793 kprintf("io_cleanup: %d I/O's left hanging\n",
1797 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1798 kmalloc_destroy(&hmp->mchain);
1799 kfree(hmp, M_HAMMER2);
1801 hammer2_mount_unlock(hmp);
1807 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1808 ino_t ino, struct vnode **vpp)
1810 kprintf("hammer2_vget\n");
1811 return (EOPNOTSUPP);
1816 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1818 hammer2_pfsmount_t *pmp;
1819 hammer2_cluster_t *cparent;
1824 if (pmp->iroot == NULL) {
1828 cparent = hammer2_inode_lock_sh(pmp->iroot);
1829 vp = hammer2_igetv(pmp->iroot, cparent, &error);
1830 hammer2_inode_unlock_sh(pmp->iroot, cparent);
1833 kprintf("vnodefail\n");
1842 * XXX incorporate ipdata->inode_quota and data_quota
1846 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1848 hammer2_pfsmount_t *pmp;
1849 hammer2_mount_t *hmp;
1852 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1853 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1855 mp->mnt_stat.f_files = pmp->inode_count;
1856 mp->mnt_stat.f_ffree = 0;
1857 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1858 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1859 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1861 *sbp = mp->mnt_stat;
1867 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1869 hammer2_pfsmount_t *pmp;
1870 hammer2_mount_t *hmp;
1873 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1874 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1876 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1877 mp->mnt_vstat.f_files = pmp->inode_count;
1878 mp->mnt_vstat.f_ffree = 0;
1879 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1880 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1881 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1883 *sbp = mp->mnt_vstat;
1888 * Mount-time recovery (RW mounts)
1890 * Updates to the free block table are allowed to lag flushes by one
1891 * transaction. In case of a crash, then on a fresh mount we must do an
1892 * incremental scan of the last committed transaction id and make sure that
1893 * all related blocks have been marked allocated.
1895 * The super-root topology and each PFS has its own transaction id domain,
1896 * so we must track PFS boundary transitions.
1898 struct hammer2_recovery_elm {
1899 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1900 hammer2_chain_t *chain;
1901 hammer2_tid_t sync_tid;
1904 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1906 struct hammer2_recovery_info {
1907 struct hammer2_recovery_list list;
1911 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1912 hammer2_chain_t *parent,
1913 struct hammer2_recovery_info *info,
1914 hammer2_tid_t sync_tid);
1916 #define HAMMER2_RECOVERY_MAXDEPTH 10
1920 hammer2_recovery(hammer2_mount_t *hmp)
1922 hammer2_trans_t trans;
1923 struct hammer2_recovery_info info;
1924 struct hammer2_recovery_elm *elm;
1925 hammer2_chain_t *parent;
1926 hammer2_tid_t sync_tid;
1928 int cumulative_error = 0;
1930 hammer2_trans_init(&trans, hmp->spmp, 0);
1933 TAILQ_INIT(&info.list);
1935 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1936 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent,
1938 hammer2_chain_lookup_done(parent);
1940 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1941 TAILQ_REMOVE(&info.list, elm, entry);
1942 parent = elm->chain;
1943 sync_tid = elm->sync_tid;
1944 kfree(elm, M_HAMMER2);
1946 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1947 HAMMER2_RESOLVE_NOREF);
1948 error = hammer2_recovery_scan(&trans, hmp, parent,
1950 hammer2_chain_unlock(parent);
1952 cumulative_error = error;
1954 hammer2_trans_done(&trans);
1956 return cumulative_error;
1961 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1962 hammer2_chain_t *parent,
1963 struct hammer2_recovery_info *info,
1964 hammer2_tid_t sync_tid)
1966 hammer2_chain_t *chain;
1968 int cumulative_error = 0;
1969 int pfs_boundary = 0;
1973 * Adjust freemap to ensure that the block(s) are marked allocated.
1975 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1976 hammer2_freemap_adjust(trans, hmp, &parent->bref,
1977 HAMMER2_FREEMAP_DORECOVER);
1981 * Check type for recursive scan
1983 switch(parent->bref.type) {
1984 case HAMMER2_BREF_TYPE_VOLUME:
1985 /* data already instantiated */
1987 case HAMMER2_BREF_TYPE_INODE:
1989 * Must instantiate data for DIRECTDATA test and also
1992 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1993 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1994 /* not applicable to recovery scan */
1995 hammer2_chain_unlock(parent);
1998 if ((parent->data->ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) &&
2001 sync_tid = parent->bref.mirror_tid - 1;
2003 hammer2_chain_unlock(parent);
2005 case HAMMER2_BREF_TYPE_INDIRECT:
2007 * Must instantiate data for recursion
2009 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2010 hammer2_chain_unlock(parent);
2012 case HAMMER2_BREF_TYPE_DATA:
2013 case HAMMER2_BREF_TYPE_FREEMAP:
2014 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2015 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2016 /* not applicable to recovery scan */
2024 * Defer operation if depth limit reached or if we are crossing a
2027 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH || pfs_boundary) {
2028 struct hammer2_recovery_elm *elm;
2030 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2031 elm->chain = parent;
2032 elm->sync_tid = sync_tid;
2033 hammer2_chain_ref(parent);
2034 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2035 /* unlocked by caller */
2042 * Recursive scan of the last flushed transaction only. We are
2043 * doing this without pmp assignments so don't leave the chains
2044 * hanging around after we are done with them.
2047 chain = hammer2_chain_scan(parent, NULL, &cache_index,
2048 HAMMER2_LOOKUP_NODATA);
2050 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2051 if (chain->bref.mirror_tid >= sync_tid) {
2053 error = hammer2_recovery_scan(trans, hmp, chain,
2057 cumulative_error = error;
2059 chain = hammer2_chain_scan(parent, chain, &cache_index,
2060 HAMMER2_LOOKUP_NODATA);
2063 return cumulative_error;
2067 * Sync the entire filesystem; this is called from the filesystem syncer
2068 * process periodically and whenever a user calls sync(1) on the hammer
2071 * Currently is actually called from the syncer! \o/
2073 * This task will have to snapshot the state of the dirty inode chain.
2074 * From that, it will have to make sure all of the inodes on the dirty
2075 * chain have IO initiated. We make sure that io is initiated for the root
2078 * If waitfor is set, we wait for media to acknowledge the new rootblock.
2080 * THINKS: side A vs side B, to have sync not stall all I/O?
2083 hammer2_vfs_sync(struct mount *mp, int waitfor)
2085 struct hammer2_sync_info info;
2086 hammer2_inode_t *iroot;
2087 hammer2_chain_t *chain;
2088 hammer2_chain_t *parent;
2089 hammer2_pfsmount_t *pmp;
2090 hammer2_mount_t *hmp;
2101 KKASSERT(iroot->pmp == pmp);
2104 * We can't acquire locks on existing vnodes while in a transaction
2105 * without risking a deadlock. This assumes that vfsync() can be
2106 * called without the vnode locked (which it can in DragonFly).
2107 * Otherwise we'd have to implement a multi-pass or flag the lock
2108 * failures and retry.
2110 * The reclamation code interlocks with the sync list's token
2111 * (by removing the vnode from the scan list) before unlocking
2112 * the inode, giving us time to ref the inode.
2114 /*flags = VMSC_GETVP;*/
2116 if (waitfor & MNT_LAZY)
2117 flags |= VMSC_ONEPASS;
2120 * Start our flush transaction. This does not return until all
2121 * concurrent transactions have completed and will prevent any
2122 * new transactions from running concurrently, except for the
2123 * buffer cache transactions.
2125 * For efficiency do an async pass before making sure with a
2126 * synchronous pass on all related buffer cache buffers. It
2127 * should theoretically not be possible for any new file buffers
2128 * to be instantiated during this sequence.
2130 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
2131 HAMMER2_TRANS_PREFLUSH);
2132 hammer2_run_unlinkq(&info.trans, pmp);
2135 info.waitfor = MNT_NOWAIT;
2136 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2137 info.waitfor = MNT_WAIT;
2138 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2141 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2142 * buffer cache flushes which occur during the flush. Device buffers
2147 if (info.error == 0 && (waitfor & MNT_WAIT)) {
2148 info.waitfor = waitfor;
2149 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2153 hammer2_bioq_sync(info.trans.pmp);
2154 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
2159 * Flush all storage elements making up the cluster
2161 * We must also flush any deleted siblings because the super-root
2162 * flush won't do it for us. They all must be staged or the
2163 * super-root flush will not be able to update its block table
2166 * XXX currently done serially instead of concurrently
2168 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2169 chain = iroot->cluster.array[i];
2171 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
2172 hammer2_flush(&info.trans, chain);
2173 hammer2_chain_unlock(chain);
2177 hammer2_trans_done(&info.trans);
2181 * Flush all volume roots to synchronize PFS flushes with the
2182 * storage media. Use a super-root transaction for each one.
2184 * The flush code will detect super-root -> pfs-root chain
2185 * transitions using the last pfs-root flush.
2187 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2188 chain = iroot->cluster.array[i];
2195 * We only have to flush each hmp once
2197 for (j = i - 1; j >= 0; --j) {
2198 if (iroot->cluster.array[j] &&
2199 iroot->cluster.array[j]->hmp == hmp)
2204 hammer2_trans_spmp(&info.trans, hmp->spmp);
2207 * Force an update of the XID from the PFS root to the
2208 * topology root. We couldn't do this from the PFS
2209 * transaction because a SPMP transaction is needed.
2210 * This does not modify blocks, instead what it does is
2211 * allow the flush code to find the transition point and
2212 * then update on the way back up.
2214 parent = chain->parent;
2215 KKASSERT(chain->pmp != parent->pmp);
2216 hammer2_chain_setflush(&info.trans, parent);
2219 * Media mounts have two 'roots', vchain for the topology
2220 * and fchain for the free block table. Flush both.
2222 * Note that the topology and free block table are handled
2223 * independently, so the free block table can wind up being
2224 * ahead of the topology. We depend on the bulk free scan
2225 * code to deal with any loose ends.
2227 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2228 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2229 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2231 * This will also modify vchain as a side effect,
2232 * mark vchain as modified now.
2234 hammer2_voldata_modify(hmp);
2235 chain = &hmp->fchain;
2236 hammer2_flush(&info.trans, chain);
2237 KKASSERT(chain == &hmp->fchain);
2239 hammer2_chain_unlock(&hmp->fchain);
2240 hammer2_chain_unlock(&hmp->vchain);
2242 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2243 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2244 chain = &hmp->vchain;
2245 hammer2_flush(&info.trans, chain);
2246 KKASSERT(chain == &hmp->vchain);
2251 hammer2_chain_unlock(&hmp->vchain);
2254 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2255 if ((hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) ||
2257 /* this will also modify vchain as a side effect */
2258 chain = &hmp->fchain;
2259 hammer2_flush(&info.trans, chain);
2260 KKASSERT(chain == &hmp->fchain);
2262 hammer2_chain_unlock(&hmp->fchain);
2268 * We can't safely flush the volume header until we have
2269 * flushed any device buffers which have built up.
2271 * XXX this isn't being incremental
2273 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2274 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2275 vn_unlock(hmp->devvp);
2278 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2279 * volume header needs synchronization via hmp->volsync.
2281 * XXX synchronize the flag & data with only this flush XXX
2284 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2288 * Synchronize the disk before flushing the volume
2292 bp->b_bio1.bio_offset = 0;
2295 bp->b_cmd = BUF_CMD_FLUSH;
2296 bp->b_bio1.bio_done = biodone_sync;
2297 bp->b_bio1.bio_flags |= BIO_SYNC;
2298 vn_strategy(hmp->devvp, &bp->b_bio1);
2299 biowait(&bp->b_bio1, "h2vol");
2303 * Then we can safely flush the version of the
2304 * volume header synchronized by the flush code.
2306 i = hmp->volhdrno + 1;
2307 if (i >= HAMMER2_NUM_VOLHDRS)
2309 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2310 hmp->volsync.volu_size) {
2313 kprintf("sync volhdr %d %jd\n",
2314 i, (intmax_t)hmp->volsync.volu_size);
2315 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2316 HAMMER2_PBUFSIZE, 0, 0);
2317 atomic_clear_int(&hmp->vchain.flags,
2318 HAMMER2_CHAIN_VOLUMESYNC);
2319 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2324 total_error = error;
2327 hammer2_trans_done(&info.trans);
2330 hammer2_trans_done(&info.trans);
2332 return (total_error);
2339 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2341 struct hammer2_sync_info *info = data;
2342 hammer2_inode_t *ip;
2351 if (vp->v_type == VNON || vp->v_type == VBAD) {
2355 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2356 RB_EMPTY(&vp->v_rbdirty_tree)) {
2362 * VOP_FSYNC will start a new transaction so replicate some code
2363 * here to do it inline (see hammer2_vop_fsync()).
2365 * WARNING: The vfsync interacts with the buffer cache and might
2366 * block, we can't hold the inode lock at that time.
2367 * However, we MUST ref ip before blocking to ensure that
2368 * it isn't ripped out from under us (since we do not
2369 * hold a lock on the vnode).
2371 hammer2_inode_ref(ip);
2372 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
2374 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
2376 hammer2_inode_drop(ip);
2380 info->error = error;
2387 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2394 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2395 struct fid *fhp, struct vnode **vpp)
2402 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2403 int *exflagsp, struct ucred **credanonp)
2409 * Support code for hammer2_mount(). Read, verify, and install the volume
2410 * header into the HMP
2412 * XXX read four volhdrs and use the one with the highest TID whos CRC
2417 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2418 * nonexistant locations.
2420 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2424 hammer2_install_volume_header(hammer2_mount_t *hmp)
2426 hammer2_volume_data_t *vd;
2428 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2440 * There are up to 4 copies of the volume header (syncs iterate
2441 * between them so there is no single master). We don't trust the
2442 * volu_size field so we don't know precisely how large the filesystem
2443 * is, so depend on the OS to return an error if we go beyond the
2444 * block device's EOF.
2446 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2447 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2448 HAMMER2_VOLUME_BYTES, &bp);
2455 vd = (struct hammer2_volume_data *) bp->b_data;
2456 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2457 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2463 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2464 /* XXX: Reversed-endianness filesystem */
2465 kprintf("hammer2: reverse-endian filesystem detected");
2471 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2472 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2473 HAMMER2_VOLUME_ICRC0_SIZE);
2474 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2475 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2476 HAMMER2_VOLUME_ICRC1_SIZE);
2477 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2478 kprintf("hammer2 volume header crc "
2479 "mismatch copy #%d %08x/%08x\n",
2486 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2495 hmp->volsync = hmp->voldata;
2497 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2498 kprintf("hammer2: using volume header #%d\n",
2503 kprintf("hammer2: no valid volume headers found!\n");
2509 * Reconnect using the passed file pointer. The caller must ref the
2513 hammer2_cluster_reconnect(hammer2_mount_t *hmp, struct file *fp)
2516 const char *name = "disk-volume";
2519 * Closes old comm descriptor, kills threads, cleans up
2520 * states, then installs the new descriptor and creates
2523 kdmsg_iocom_reconnect(&hmp->iocom, fp, "hammer2");
2526 * Setup LNK_CONN fields for autoinitiated state machine. We
2527 * will use SPANs to advertise multiple PFSs so only pass the
2528 * fsid and HAMMER2_PFSTYPE_SUPROOT for the AUTOCONN.
2530 * We are not initiating a LNK_SPAN so we do not have to set-up
2531 * iocom.auto_lnk_span.
2533 bzero(&hmp->iocom.auto_lnk_conn.pfs_clid,
2534 sizeof(hmp->iocom.auto_lnk_conn.pfs_clid));
2535 hmp->iocom.auto_lnk_conn.pfs_fsid = hmp->voldata.fsid;
2536 hmp->iocom.auto_lnk_conn.pfs_type = HAMMER2_PFSTYPE_SUPROOT;
2537 hmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
2539 hmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
2541 hmp->iocom.auto_lnk_conn.peer_type = DMSG_PEER_HAMMER2;
2544 * Filter adjustment. Clients do not need visibility into other
2545 * clients (otherwise millions of clients would present a serious
2546 * problem). The fs_label also serves to restrict the namespace.
2548 hmp->iocom.auto_lnk_conn.peer_mask = 1LLU << DMSG_PEER_HAMMER2;
2549 hmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
2552 switch (ipdata->pfs_type) {
2553 case DMSG_PFSTYPE_CLIENT:
2554 hmp->iocom.auto_lnk_conn.peer_mask &=
2555 ~(1LLU << DMSG_PFSTYPE_CLIENT);
2562 name_len = strlen(name);
2563 if (name_len >= sizeof(hmp->iocom.auto_lnk_conn.fs_label))
2564 name_len = sizeof(hmp->iocom.auto_lnk_conn.fs_label) - 1;
2565 bcopy(name, hmp->iocom.auto_lnk_conn.fs_label, name_len);
2566 hmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
2568 kdmsg_iocom_autoinitiate(&hmp->iocom, hammer2_autodmsg);
2572 hammer2_rcvdmsg(kdmsg_msg_t *msg)
2574 kprintf("RCVMSG %08x\n", msg->tcmd);
2577 case DMSG_DBG_SHELL:
2580 * Execute shell command (not supported atm)
2582 kdmsg_msg_result(msg, DMSG_ERR_NOSUPP);
2584 case DMSG_DBG_SHELL | DMSGF_REPLY:
2588 if (msg->aux_data) {
2589 msg->aux_data[msg->aux_size - 1] = 0;
2590 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
2595 * Unsupported message received. We only need to
2596 * reply if it's a transaction in order to close our end.
2597 * Ignore any one-way messages or any further messages
2598 * associated with the transaction.
2600 * NOTE: This case also includes DMSG_LNK_ERROR messages
2601 * which might be one-way, replying to those would
2602 * cause an infinite ping-pong.
2604 if (msg->any.head.cmd & DMSGF_CREATE)
2605 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2612 * This function is called after KDMSG has automatically handled processing
2613 * of a LNK layer message (typically CONN, SPAN, or CIRC).
2615 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
2616 * advertises all available hammer2 super-root volumes.
2618 static void hammer2_update_spans(hammer2_mount_t *hmp, kdmsg_state_t *state);
2621 hammer2_autodmsg(kdmsg_msg_t *msg)
2623 hammer2_mount_t *hmp = msg->state->iocom->handle;
2626 kprintf("RCAMSG %08x\n", msg->tcmd);
2629 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY:
2630 case DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_DELETE | DMSGF_REPLY:
2631 if (msg->any.head.cmd & DMSGF_CREATE) {
2632 kprintf("HAMMER2: VOLDATA DUMP\n");
2635 * Dump the configuration stored in the volume header.
2636 * This will typically be import/export access rights,
2637 * master encryption keys (encrypted), etc.
2639 hammer2_voldata_lock(hmp);
2641 while (copyid < HAMMER2_COPYID_COUNT) {
2642 if (hmp->voldata.copyinfo[copyid].copyid)
2643 hammer2_volconf_update(hmp, copyid);
2646 hammer2_voldata_unlock(hmp);
2648 kprintf("HAMMER2: INITIATE SPANs\n");
2649 hammer2_update_spans(hmp, msg->state);
2651 if ((msg->any.head.cmd & DMSGF_DELETE) &&
2652 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
2653 kprintf("HAMMER2: CONN WAS TERMINATED\n");
2662 * Update LNK_SPAN state
2665 hammer2_update_spans(hammer2_mount_t *hmp, kdmsg_state_t *state)
2667 const hammer2_inode_data_t *ipdata;
2668 hammer2_cluster_t *cparent;
2669 hammer2_cluster_t *cluster;
2670 hammer2_pfsmount_t *spmp;
2671 hammer2_key_t key_next;
2677 * Lookup mount point under the media-localized super-root.
2679 * cluster->pmp will incorrectly point to spmp and must be fixed
2683 cparent = hammer2_inode_lock_ex(spmp->iroot);
2684 cluster = hammer2_cluster_lookup(cparent, &key_next,
2689 if (hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE)
2691 ipdata = &hammer2_cluster_data(cluster)->ipdata;
2692 kprintf("UPDATE SPANS: %s\n", ipdata->filename);
2694 rmsg = kdmsg_msg_alloc(state, DMSG_LNK_SPAN | DMSGF_CREATE,
2695 hammer2_lnk_span_reply, NULL);
2696 rmsg->any.lnk_span.pfs_clid = ipdata->pfs_clid;
2697 rmsg->any.lnk_span.pfs_fsid = ipdata->pfs_fsid;
2698 rmsg->any.lnk_span.pfs_type = ipdata->pfs_type;
2699 rmsg->any.lnk_span.peer_type = DMSG_PEER_HAMMER2;
2700 rmsg->any.lnk_span.proto_version = DMSG_SPAN_PROTO_1;
2701 name_len = ipdata->name_len;
2702 if (name_len >= sizeof(rmsg->any.lnk_span.fs_label))
2703 name_len = sizeof(rmsg->any.lnk_span.fs_label) - 1;
2704 bcopy(ipdata->filename, rmsg->any.lnk_span.fs_label, name_len);
2706 kdmsg_msg_write(rmsg);
2708 cluster = hammer2_cluster_next(cparent, cluster,
2714 hammer2_inode_unlock_ex(spmp->iroot, cparent);
2719 hammer2_lnk_span_reply(kdmsg_state_t *state, kdmsg_msg_t *msg)
2721 if ((state->txcmd & DMSGF_DELETE) == 0 &&
2722 (msg->any.head.cmd & DMSGF_DELETE)) {
2723 kdmsg_msg_reply(msg, 0);
2729 * Volume configuration updates are passed onto the userland service
2730 * daemon via the open LNK_CONN transaction.
2733 hammer2_volconf_update(hammer2_mount_t *hmp, int index)
2737 /* XXX interlock against connection state termination */
2738 kprintf("volconf update %p\n", hmp->iocom.conn_state);
2739 if (hmp->iocom.conn_state) {
2740 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
2741 msg = kdmsg_msg_alloc(hmp->iocom.conn_state,
2742 DMSG_LNK_HAMMER2_VOLCONF,
2744 H2_LNK_VOLCONF(msg)->copy = hmp->voldata.copyinfo[index];
2745 H2_LNK_VOLCONF(msg)->mediaid = hmp->voldata.fsid;
2746 H2_LNK_VOLCONF(msg)->index = index;
2747 kdmsg_msg_write(msg);
2752 * This handles hysteresis on regular file flushes. Because the BIOs are
2753 * routed to a thread it is possible for an excessive number to build up
2754 * and cause long front-end stalls long before the runningbuffspace limit
2755 * is hit, so we implement hammer2_flush_pipe to control the
2758 * This is a particular problem when compression is used.
2761 hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp)
2763 atomic_add_int(&pmp->count_lwinprog, 1);
2767 hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp)
2771 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2772 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2773 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2774 atomic_clear_int(&pmp->count_lwinprog,
2775 HAMMER2_LWINPROG_WAITING);
2776 wakeup(&pmp->count_lwinprog);
2781 hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp)
2786 lwinprog = pmp->count_lwinprog;
2788 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2790 tsleep_interlock(&pmp->count_lwinprog, 0);
2791 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2792 lwinprog = pmp->count_lwinprog;
2793 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2795 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2800 * Manage excessive memory resource use for chain and related
2804 hammer2_pfs_memory_wait(hammer2_pfsmount_t *pmp)
2814 * Atomic check condition and wait. Also do an early speedup of
2815 * the syncer to try to avoid hitting the wait.
2818 waiting = pmp->inmem_dirty_chains;
2820 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2822 limit = pmp->mp->mnt_nvnodelistsize / 10;
2823 if (limit < hammer2_limit_dirty_chains)
2824 limit = hammer2_limit_dirty_chains;
2829 if ((int)(ticks - zzticks) > hz) {
2831 kprintf("count %ld %ld\n", count, limit);
2836 * Block if there are too many dirty chains present, wait
2837 * for the flush to clean some out.
2839 if (count > limit) {
2840 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2841 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2843 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2844 speedup_syncer(pmp->mp);
2845 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2848 continue; /* loop on success or fail */
2852 * Try to start an early flush before we are forced to block.
2854 if (count > limit * 7 / 10)
2855 speedup_syncer(pmp->mp);
2861 hammer2_pfs_memory_inc(hammer2_pfsmount_t *pmp)
2864 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2869 hammer2_pfs_memory_wakeup(hammer2_pfsmount_t *pmp)
2877 waiting = pmp->inmem_dirty_chains;
2879 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2882 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2887 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2888 wakeup(&pmp->inmem_dirty_chains);
2895 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2897 hammer2_chain_t *scan;
2898 hammer2_chain_t *parent;
2902 kprintf("%*.*s...\n", tab, tab, "");
2907 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2909 chain, chain->bref.type,
2910 chain->bref.key, chain->bref.keybits,
2911 chain->bref.mirror_tid);
2913 kprintf("%*.*s [%08x] (%s) refs=%d\n",
2916 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2917 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2920 kprintf("%*.*s core [%08x]",
2924 parent = chain->parent;
2926 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2928 parent, parent->flags, parent->refs);
2929 if (RB_EMPTY(&chain->core.rbtree)) {
2933 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2934 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2935 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2936 kprintf("%*.*s}(%s)\n", tab, tab, "",
2937 chain->data->ipdata.filename);
2939 kprintf("%*.*s}\n", tab, tab, "");