2 * Copyright (c) 2011-2013 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 static struct hammer2_mntlist hammer2_mntlist;
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 long hammer2_iod_file_read;
86 long hammer2_iod_meta_read;
87 long hammer2_iod_indr_read;
88 long hammer2_iod_fmap_read;
89 long hammer2_iod_volu_read;
90 long hammer2_iod_file_write;
91 long hammer2_iod_meta_write;
92 long hammer2_iod_indr_write;
93 long hammer2_iod_fmap_write;
94 long hammer2_iod_volu_write;
95 long hammer2_ioa_file_read;
96 long hammer2_ioa_meta_read;
97 long hammer2_ioa_indr_read;
98 long hammer2_ioa_fmap_read;
99 long hammer2_ioa_volu_read;
100 long hammer2_ioa_fmap_write;
101 long hammer2_ioa_file_write;
102 long hammer2_ioa_meta_write;
103 long hammer2_ioa_indr_write;
104 long hammer2_ioa_volu_write;
106 MALLOC_DECLARE(C_BUFFER);
107 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
109 MALLOC_DECLARE(D_BUFFER);
110 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
112 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
114 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
115 &hammer2_debug, 0, "");
116 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
117 &hammer2_cluster_enable, 0, "");
118 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
119 &hammer2_hardlink_enable, 0, "");
120 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
121 &hammer2_flush_pipe, 0, "");
123 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
124 &hammer2_iod_file_read, 0, "");
125 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
126 &hammer2_iod_meta_read, 0, "");
127 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
128 &hammer2_iod_indr_read, 0, "");
129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
130 &hammer2_iod_fmap_read, 0, "");
131 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
132 &hammer2_iod_volu_read, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
135 &hammer2_iod_file_write, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
137 &hammer2_iod_meta_write, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
139 &hammer2_iod_indr_write, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
141 &hammer2_iod_fmap_write, 0, "");
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
143 &hammer2_iod_volu_write, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
146 &hammer2_ioa_file_read, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
148 &hammer2_ioa_meta_read, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
150 &hammer2_ioa_indr_read, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
152 &hammer2_ioa_fmap_read, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
154 &hammer2_ioa_volu_read, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
157 &hammer2_ioa_file_write, 0, "");
158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
159 &hammer2_ioa_meta_write, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
161 &hammer2_ioa_indr_write, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
163 &hammer2_ioa_fmap_write, 0, "");
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
165 &hammer2_ioa_volu_write, 0, "");
167 static int hammer2_vfs_init(struct vfsconf *conf);
168 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
169 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
171 static int hammer2_remount(hammer2_mount_t *, char *, struct vnode *,
173 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
174 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
175 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
177 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
179 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
180 ino_t ino, struct vnode **vpp);
181 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
182 struct fid *fhp, struct vnode **vpp);
183 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
184 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
185 int *exflagsp, struct ucred **credanonp);
187 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
188 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
190 static void hammer2_write_thread(void *arg);
193 * Functions for compression in threads,
194 * from hammer2_vnops.c
196 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
198 hammer2_inode_data_t *ipdata,
199 hammer2_chain_t **parentp,
200 hammer2_key_t lbase, int ioflag, int pblksize,
202 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
204 hammer2_inode_data_t *ipdata,
205 hammer2_chain_t **parentp,
206 hammer2_key_t lbase, int ioflag,
207 int pblksize, int *errorp, int comp_algo);
208 static void hammer2_zero_check_and_write(struct buf *bp,
209 hammer2_trans_t *trans, hammer2_inode_t *ip,
210 hammer2_inode_data_t *ipdata,
211 hammer2_chain_t **parentp,
213 int ioflag, int pblksize, int *errorp);
214 static int test_block_zeros(const char *buf, size_t bytes);
215 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
217 hammer2_inode_data_t *ipdata,
218 hammer2_chain_t **parentp,
221 static void hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp,
222 int ioflag, int pblksize, int *errorp);
224 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
225 static void hammer2_autodmsg(kdmsg_msg_t *msg);
229 * HAMMER2 vfs operations.
231 static struct vfsops hammer2_vfsops = {
232 .vfs_init = hammer2_vfs_init,
233 .vfs_uninit = hammer2_vfs_uninit,
234 .vfs_sync = hammer2_vfs_sync,
235 .vfs_mount = hammer2_vfs_mount,
236 .vfs_unmount = hammer2_vfs_unmount,
237 .vfs_root = hammer2_vfs_root,
238 .vfs_statfs = hammer2_vfs_statfs,
239 .vfs_statvfs = hammer2_vfs_statvfs,
240 .vfs_vget = hammer2_vfs_vget,
241 .vfs_vptofh = hammer2_vfs_vptofh,
242 .vfs_fhtovp = hammer2_vfs_fhtovp,
243 .vfs_checkexp = hammer2_vfs_checkexp
246 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
248 VFS_SET(hammer2_vfsops, hammer2, 0);
249 MODULE_VERSION(hammer2, 1);
253 hammer2_vfs_init(struct vfsconf *conf)
255 static struct objcache_malloc_args margs_read;
256 static struct objcache_malloc_args margs_write;
262 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
264 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
266 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
270 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
272 margs_read.objsize = 65536;
273 margs_read.mtype = D_BUFFER;
275 margs_write.objsize = 32768;
276 margs_write.mtype = C_BUFFER;
278 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
279 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
280 objcache_malloc_free, &margs_read);
281 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
282 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
283 objcache_malloc_free, &margs_write);
285 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
286 TAILQ_INIT(&hammer2_mntlist);
293 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
295 objcache_destroy(cache_buffer_read);
296 objcache_destroy(cache_buffer_write);
301 * Mount or remount HAMMER2 fileystem from physical media
304 * mp mount point structure
310 * mp mount point structure
311 * path path to mount point
312 * data pointer to argument structure in user space
313 * volume volume path (device@LABEL form)
314 * hflags user mount flags
315 * cred user credentials
322 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
325 struct hammer2_mount_info info;
326 hammer2_pfsmount_t *pmp;
327 hammer2_mount_t *hmp;
328 hammer2_key_t key_next;
329 hammer2_key_t key_dummy;
332 struct nlookupdata nd;
333 hammer2_chain_t *parent;
334 hammer2_chain_t *schain;
335 hammer2_chain_t *rchain;
337 char devstr[MNAMELEN];
354 kprintf("hammer2_mount\n");
360 bzero(&info, sizeof(info));
361 info.cluster_fd = -1;
365 * Non-root mount or updating a mount
367 error = copyin(data, &info, sizeof(info));
371 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
375 /* Extract device and label */
377 label = strchr(devstr, '@');
379 ((label + 1) - dev) > done) {
387 if (mp->mnt_flag & MNT_UPDATE) {
389 /* HAMMER2 implements NFS export via mountctl */
391 for (i = 0; i < pmp->cluster.nchains; ++i) {
392 hmp = pmp->cluster.chains[i]->hmp;
394 error = hammer2_remount(hmp, path, devvp, cred);
405 * Lookup name and verify it refers to a block device.
407 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
409 error = nlookup(&nd);
411 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
415 if (vn_isdisk(devvp, &error))
416 error = vfs_mountedon(devvp);
420 * Determine if the device has already been mounted. After this
421 * check hmp will be non-NULL if we are doing the second or more
422 * hammer2 mounts from the same device.
424 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
425 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
426 if (hmp->devvp == devvp)
431 * Open the device if this isn't a secondary mount and construct
432 * the H2 device mount (hmp).
435 if (error == 0 && vcount(devvp) > 0)
439 * Now open the device
442 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
443 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
444 error = vinvalbuf(devvp, V_SAVE, 0, 0);
446 error = VOP_OPEN(devvp,
447 ronly ? FREAD : FREAD | FWRITE,
452 if (error && devvp) {
457 lockmgr(&hammer2_mntlk, LK_RELEASE);
460 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
463 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
464 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
465 RB_INIT(&hmp->iotree);
467 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
468 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
469 TAILQ_INIT(&hmp->transq);
472 * vchain setup. vchain.data is embedded.
473 * vchain.refs is initialized and will never drop to 0.
475 hmp->vchain.hmp = hmp;
476 hmp->vchain.refs = 1;
477 hmp->vchain.data = (void *)&hmp->voldata;
478 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
479 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
480 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
481 hammer2_chain_core_alloc(NULL, &hmp->vchain, NULL);
482 /* hmp->vchain.u.xxx is left NULL */
485 * fchain setup. fchain.data is embedded.
486 * fchain.refs is initialized and will never drop to 0.
488 * The data is not used but needs to be initialized to
489 * pass assertion muster. We use this chain primarily
490 * as a placeholder for the freemap's top-level RBTREE
491 * so it does not interfere with the volume's topology
494 hmp->fchain.hmp = hmp;
495 hmp->fchain.refs = 1;
496 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
497 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
498 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
499 hmp->fchain.bref.methods =
500 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
501 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
502 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
504 hammer2_chain_core_alloc(NULL, &hmp->fchain, NULL);
505 /* hmp->fchain.u.xxx is left NULL */
508 * Install the volume header
510 error = hammer2_install_volume_header(hmp);
512 hammer2_vfs_unmount(mp, MNT_FORCE);
516 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
517 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
520 * First locate the super-root inode, which is key 0
521 * relative to the volume header's blockset.
523 * Then locate the root inode by scanning the directory keyspace
524 * represented by the label.
526 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
527 schain = hammer2_chain_lookup(&parent, &key_dummy,
528 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
530 hammer2_chain_lookup_done(parent);
531 if (schain == NULL) {
532 kprintf("hammer2_mount: invalid super-root\n");
533 hammer2_vfs_unmount(mp, MNT_FORCE);
538 * NOTE: inode_get sucks up schain's lock.
540 atomic_set_int(&schain->flags, HAMMER2_CHAIN_PFSROOT);
541 hmp->sroot = hammer2_inode_get(NULL, NULL, schain);
542 hammer2_inode_ref(hmp->sroot);
543 hammer2_inode_unlock_ex(hmp->sroot, schain);
545 /* leave hmp->sroot with one ref */
549 * Block device opened successfully, finish initializing the
552 * From this point on we have to call hammer2_unmount() on failure.
554 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
556 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
557 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
559 spin_init(&pmp->inum_spin);
560 RB_INIT(&pmp->inum_tree);
562 kdmsg_iocom_init(&pmp->iocom, pmp,
563 KDMSG_IOCOMF_AUTOCONN |
564 KDMSG_IOCOMF_AUTOSPAN |
565 KDMSG_IOCOMF_AUTOCIRC,
566 pmp->mmsg, hammer2_rcvdmsg);
568 ccms_domain_init(&pmp->ccms_dom);
570 lockmgr(&hammer2_mntlk, LK_RELEASE);
571 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
572 hmp, pmp, hmp->pmp_count);
574 mp->mnt_flag = MNT_LOCAL;
575 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
576 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
579 * required mount structure initializations
581 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
582 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
584 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
585 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
590 mp->mnt_iosize_max = MAXPHYS;
591 mp->mnt_data = (qaddr_t)pmp;
595 * Lookup mount point under the media-localized super-root.
597 parent = hammer2_inode_lock_ex(hmp->sroot);
598 lhc = hammer2_dirhash(label, strlen(label));
599 rchain = hammer2_chain_lookup(&parent, &key_next,
600 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
603 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
604 strcmp(label, rchain->data->ipdata.filename) == 0) {
607 rchain = hammer2_chain_next(&parent, rchain, &key_next,
609 lhc + HAMMER2_DIRHASH_LOMASK,
612 hammer2_inode_unlock_ex(hmp->sroot, parent);
614 if (rchain == NULL) {
615 kprintf("hammer2_mount: PFS label not found\n");
617 hammer2_vfs_unmount(mp, MNT_FORCE);
620 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
621 hammer2_chain_unlock(rchain);
622 kprintf("hammer2_mount: PFS label already mounted!\n");
624 hammer2_vfs_unmount(mp, MNT_FORCE);
628 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
629 kprintf("hammer2_mount: PFS label currently recycling\n");
631 hammer2_vfs_unmount(mp, MNT_FORCE);
636 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
639 * NOTE: *_get() integrates chain's lock into the inode lock.
641 hammer2_chain_ref(rchain); /* for pmp->rchain */
642 pmp->cluster.nchains = 1;
643 pmp->cluster.chains[0] = rchain;
644 pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
645 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
647 KKASSERT(rchain->pmp == NULL); /* tracking pmp for rchain */
649 atomic_add_long(&pmp->inmem_chains, 1);
651 hammer2_inode_unlock_ex(pmp->iroot, rchain);
653 kprintf("iroot %p\n", pmp->iroot);
656 * The logical file buffer bio write thread handles things
657 * like physical block assignment and compression.
659 mtx_init(&pmp->wthread_mtx);
660 bioq_init(&pmp->wthread_bioq);
661 pmp->wthread_destroy = 0;
662 lwkt_create(hammer2_write_thread, pmp,
663 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
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 kprintf("hammer2_mount: bad cluster_fd!\n");
672 hammer2_vfs_unmount(mp, MNT_FORCE);
675 hammer2_cluster_reconnect(pmp, fp);
681 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
682 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
683 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
685 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
686 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
687 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
688 copyinstr(path, mp->mnt_stat.f_mntonname,
689 sizeof(mp->mnt_stat.f_mntonname) - 1,
693 * Initial statfs to prime mnt_stat.
695 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
701 * Handle bioq for strategy write
705 hammer2_write_thread(void *arg)
707 hammer2_pfsmount_t *pmp;
710 hammer2_trans_t trans;
713 hammer2_chain_t *parent;
714 hammer2_chain_t **parentp;
715 hammer2_inode_data_t *ipdata;
723 mtx_lock(&pmp->wthread_mtx);
724 while (pmp->wthread_destroy == 0) {
725 if (bioq_first(&pmp->wthread_bioq) == NULL) {
726 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
732 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
734 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
736 * dummy bio for synchronization. The transaction
737 * must be reinitialized.
739 if (bio->bio_buf == NULL) {
740 bio->bio_flags |= BIO_DONE;
742 hammer2_trans_done(&trans);
743 hammer2_trans_init(&trans, pmp,
744 HAMMER2_TRANS_BUFCACHE);
749 * else normal bio processing
751 mtx_unlock(&pmp->wthread_mtx);
753 hammer2_lwinprog_drop(pmp);
761 * Inode is modified, flush size and mtime changes
762 * to ensure that the file size remains consistent
763 * with the buffers being flushed.
765 parent = hammer2_inode_lock_ex(ip);
766 if (ip->flags & (HAMMER2_INODE_RESIZED |
767 HAMMER2_INODE_MTIME)) {
768 hammer2_inode_fsync(&trans, ip, parentp);
770 ipdata = hammer2_chain_modify_ip(&trans, ip,
772 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
774 pblksize = hammer2_calc_physical(ip, lbase);
775 hammer2_write_file_core(bp, &trans, ip, ipdata,
779 hammer2_inode_unlock_ex(ip, parent);
781 kprintf("hammer2: error in buffer write\n");
782 bp->b_flags |= B_ERROR;
786 mtx_lock(&pmp->wthread_mtx);
788 hammer2_trans_done(&trans);
790 pmp->wthread_destroy = -1;
791 wakeup(&pmp->wthread_destroy);
793 mtx_unlock(&pmp->wthread_mtx);
797 hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
801 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
802 mtx_lock(&pmp->wthread_mtx);
803 if (pmp->wthread_destroy == 0) {
804 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
805 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
806 wakeup(&pmp->wthread_bioq);
808 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
810 while ((sync_bio.bio_flags & BIO_DONE) == 0)
811 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
813 mtx_unlock(&pmp->wthread_mtx);
817 * Return a chain suitable for I/O, creating the chain if necessary
818 * and assigning its physical block.
822 hammer2_assign_physical(hammer2_trans_t *trans,
823 hammer2_inode_t *ip, hammer2_chain_t **parentp,
824 hammer2_key_t lbase, int pblksize, int *errorp)
826 hammer2_chain_t *parent;
827 hammer2_chain_t *chain;
829 hammer2_key_t key_dummy;
830 int pradix = hammer2_getradix(pblksize);
831 int cache_index = -1;
834 * Locate the chain associated with lbase, return a locked chain.
835 * However, do not instantiate any data reference (which utilizes a
836 * device buffer) because we will be using direct IO via the
837 * logical buffer cache buffer.
840 KKASSERT(pblksize >= HAMMER2_MIN_ALLOC);
843 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); /* extra lock */
844 chain = hammer2_chain_lookup(&parent, &key_dummy,
846 &cache_index, HAMMER2_LOOKUP_NODATA);
850 * We found a hole, create a new chain entry.
852 * NOTE: DATA chains are created without device backing
853 * store (nor do we want any).
855 *errorp = hammer2_chain_create(trans, &parent, &chain,
856 lbase, HAMMER2_PBUFRADIX,
857 HAMMER2_BREF_TYPE_DATA,
860 hammer2_chain_lookup_done(parent);
861 panic("hammer2_chain_create: par=%p error=%d\n",
866 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
867 /*ip->delta_dcount += pblksize;*/
869 switch (chain->bref.type) {
870 case HAMMER2_BREF_TYPE_INODE:
872 * The data is embedded in the inode. The
873 * caller is responsible for marking the inode
874 * modified and copying the data to the embedded
879 case HAMMER2_BREF_TYPE_DATA:
880 if (chain->bytes != pblksize) {
881 hammer2_chain_resize(trans, ip,
884 HAMMER2_MODIFY_OPTDATA);
886 hammer2_chain_modify(trans, &chain,
887 HAMMER2_MODIFY_OPTDATA);
888 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
891 panic("hammer2_assign_physical: bad type");
899 * Cleanup. If chain wound up being the inode (i.e. DIRECTDATA),
900 * we might have to replace *parentp.
902 hammer2_chain_lookup_done(parent);
904 if (*parentp != chain &&
905 (*parentp)->core == chain->core) {
907 *parentp = chain; /* eats lock */
908 hammer2_chain_unlock(parent);
909 hammer2_chain_lock(chain, 0); /* need another */
911 /* else chain already locked for return */
917 * From hammer2_vnops.c.
918 * The core write function which determines which path to take
919 * depending on compression settings.
923 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
924 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
925 hammer2_chain_t **parentp,
926 hammer2_key_t lbase, int ioflag, int pblksize,
929 hammer2_chain_t *chain;
931 switch(HAMMER2_DEC_COMP(ipdata->comp_algo)) {
932 case HAMMER2_COMP_NONE:
934 * We have to assign physical storage to the buffer
935 * we intend to dirty or write now to avoid deadlocks
936 * in the strategy code later.
938 * This can return NOOFFSET for inode-embedded data.
939 * The strategy code will take care of it in that case.
941 chain = hammer2_assign_physical(trans, ip, parentp,
944 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
946 hammer2_chain_unlock(chain);
948 case HAMMER2_COMP_AUTOZERO:
950 * Check for zero-fill only
952 hammer2_zero_check_and_write(bp, trans, ip,
953 ipdata, parentp, lbase,
954 ioflag, pblksize, errorp);
956 case HAMMER2_COMP_LZ4:
957 case HAMMER2_COMP_ZLIB:
960 * Check for zero-fill and attempt compression.
962 hammer2_compress_and_write(bp, trans, ip,
969 ipdata = &ip->chain->data->ipdata; /* reload */
973 * From hammer2_vnops.c
974 * Generic function that will perform the compression in compression
975 * write path. The compression algorithm is determined by the settings
976 * obtained from inode.
980 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
981 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
982 hammer2_chain_t **parentp,
983 hammer2_key_t lbase, int ioflag, int pblksize,
984 int *errorp, int comp_algo)
986 hammer2_chain_t *chain;
991 if (test_block_zeros(bp->b_data, pblksize)) {
992 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
999 KKASSERT(pblksize / 2 <= 32768);
1001 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1002 z_stream strm_compress;
1006 switch(HAMMER2_DEC_COMP(comp_algo)) {
1007 case HAMMER2_COMP_LZ4:
1008 comp_buffer = objcache_get(cache_buffer_write,
1010 comp_size = LZ4_compress_limitedOutput(
1012 &comp_buffer[sizeof(int)],
1014 pblksize / 2 - sizeof(int));
1016 * We need to prefix with the size, LZ4
1017 * doesn't do it for us. Add the related
1020 *(int *)comp_buffer = comp_size;
1022 comp_size += sizeof(int);
1024 case HAMMER2_COMP_ZLIB:
1025 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1026 if (comp_level == 0)
1027 comp_level = 6; /* default zlib compression */
1028 else if (comp_level < 6)
1030 else if (comp_level > 9)
1032 ret = deflateInit(&strm_compress, comp_level);
1034 kprintf("HAMMER2 ZLIB: fatal error "
1035 "on deflateInit.\n");
1038 comp_buffer = objcache_get(cache_buffer_write,
1040 strm_compress.next_in = bp->b_data;
1041 strm_compress.avail_in = pblksize;
1042 strm_compress.next_out = comp_buffer;
1043 strm_compress.avail_out = pblksize / 2;
1044 ret = deflate(&strm_compress, Z_FINISH);
1045 if (ret == Z_STREAM_END) {
1046 comp_size = pblksize / 2 -
1047 strm_compress.avail_out;
1051 ret = deflateEnd(&strm_compress);
1054 kprintf("Error: Unknown compression method.\n");
1055 kprintf("Comp_method = %d.\n", comp_algo);
1060 if (comp_size == 0) {
1062 * compression failed or turned off
1064 comp_block_size = pblksize; /* safety */
1065 if (++ip->comp_heuristic > 128)
1066 ip->comp_heuristic = 8;
1069 * compression succeeded
1071 ip->comp_heuristic = 0;
1072 if (comp_size <= 1024) {
1073 comp_block_size = 1024;
1074 } else if (comp_size <= 2048) {
1075 comp_block_size = 2048;
1076 } else if (comp_size <= 4096) {
1077 comp_block_size = 4096;
1078 } else if (comp_size <= 8192) {
1079 comp_block_size = 8192;
1080 } else if (comp_size <= 16384) {
1081 comp_block_size = 16384;
1082 } else if (comp_size <= 32768) {
1083 comp_block_size = 32768;
1085 panic("hammer2: WRITE PATH: "
1086 "Weird comp_size value.");
1088 comp_block_size = pblksize;
1092 chain = hammer2_assign_physical(trans, ip, parentp,
1093 lbase, comp_block_size,
1095 ipdata = &ip->chain->data->ipdata; /* RELOAD */
1098 kprintf("WRITE PATH: An error occurred while "
1099 "assigning physical space.\n");
1100 KKASSERT(chain == NULL);
1102 /* Get device offset */
1107 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1109 switch(chain->bref.type) {
1110 case HAMMER2_BREF_TYPE_INODE:
1111 KKASSERT(chain->data->ipdata.op_flags &
1112 HAMMER2_OPFLAG_DIRECTDATA);
1113 KKASSERT(bp->b_loffset == 0);
1114 bcopy(bp->b_data, chain->data->ipdata.u.data,
1115 HAMMER2_EMBEDDED_BYTES);
1117 case HAMMER2_BREF_TYPE_DATA:
1118 temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1121 * Optimize out the read-before-write
1124 *errorp = hammer2_io_newnz(chain->hmp,
1125 chain->bref.data_off,
1129 hammer2_io_brelse(&dio);
1130 kprintf("hammer2: WRITE PATH: "
1131 "dbp bread error\n");
1134 bdata = hammer2_io_data(dio, chain->bref.data_off);
1137 * When loading the block make sure we don't
1138 * leave garbage after the compressed data.
1141 chain->bref.methods =
1142 HAMMER2_ENC_COMP(comp_algo) +
1143 HAMMER2_ENC_CHECK(temp_check);
1144 bcopy(comp_buffer, bdata, comp_size);
1145 if (comp_size != comp_block_size) {
1146 bzero(bdata + comp_size,
1147 comp_block_size - comp_size);
1150 chain->bref.methods =
1152 HAMMER2_COMP_NONE) +
1153 HAMMER2_ENC_CHECK(temp_check);
1154 bcopy(bp->b_data, bdata, pblksize);
1158 * Device buffer is now valid, chain is no
1159 * longer in the initial state.
1161 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1163 /* Now write the related bdp. */
1164 if (ioflag & IO_SYNC) {
1166 * Synchronous I/O requested.
1168 hammer2_io_bwrite(&dio);
1170 } else if ((ioflag & IO_DIRECT) &&
1171 loff + n == pblksize) {
1172 hammer2_io_bdwrite(&dio);
1174 } else if (ioflag & IO_ASYNC) {
1175 hammer2_io_bawrite(&dio);
1177 hammer2_io_bdwrite(&dio);
1181 panic("hammer2_write_bp: bad chain type %d\n",
1187 hammer2_chain_unlock(chain);
1190 objcache_put(cache_buffer_write, comp_buffer);
1194 * Function that performs zero-checking and writing without compression,
1195 * it corresponds to default zero-checking path.
1199 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1200 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
1201 hammer2_chain_t **parentp,
1202 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp)
1204 hammer2_chain_t *chain;
1206 if (test_block_zeros(bp->b_data, pblksize)) {
1207 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
1209 chain = hammer2_assign_physical(trans, ip, parentp,
1210 lbase, pblksize, errorp);
1211 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
1213 hammer2_chain_unlock(chain);
1218 * A function to test whether a block of data contains only zeros,
1219 * returns TRUE (non-zero) if the block is all zeros.
1223 test_block_zeros(const char *buf, size_t bytes)
1227 for (i = 0; i < bytes; i += sizeof(long)) {
1228 if (*(const long *)(buf + i) != 0)
1235 * Function to "write" a block that contains only zeros.
1239 zero_write(struct buf *bp, hammer2_trans_t *trans, hammer2_inode_t *ip,
1240 hammer2_inode_data_t *ipdata, hammer2_chain_t **parentp,
1241 hammer2_key_t lbase, int *errorp __unused)
1243 hammer2_chain_t *parent;
1244 hammer2_chain_t *chain;
1245 hammer2_key_t key_dummy;
1246 int cache_index = -1;
1248 parent = hammer2_chain_lookup_init(*parentp, 0);
1250 chain = hammer2_chain_lookup(&parent, &key_dummy, lbase, lbase,
1251 &cache_index, HAMMER2_LOOKUP_NODATA);
1253 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1254 bzero(chain->data->ipdata.u.data,
1255 HAMMER2_EMBEDDED_BYTES);
1257 hammer2_chain_delete(trans, chain, 0);
1259 hammer2_chain_unlock(chain);
1261 hammer2_chain_lookup_done(parent);
1265 * Function to write the data as it is, without performing any sort of
1266 * compression. This function is used in path without compression and
1267 * default zero-checking path.
1271 hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp, int ioflag,
1272 int pblksize, int *errorp)
1277 int temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1279 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1281 switch(chain->bref.type) {
1282 case HAMMER2_BREF_TYPE_INODE:
1283 KKASSERT(chain->data->ipdata.op_flags &
1284 HAMMER2_OPFLAG_DIRECTDATA);
1285 KKASSERT(bp->b_loffset == 0);
1286 bcopy(bp->b_data, chain->data->ipdata.u.data,
1287 HAMMER2_EMBEDDED_BYTES);
1290 case HAMMER2_BREF_TYPE_DATA:
1291 error = hammer2_io_newnz(chain->hmp, chain->bref.data_off,
1292 chain->bytes, &dio);
1294 hammer2_io_bqrelse(&dio);
1295 kprintf("hammer2: WRITE PATH: dbp bread error\n");
1298 bdata = hammer2_io_data(dio, chain->bref.data_off);
1300 chain->bref.methods = HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
1301 HAMMER2_ENC_CHECK(temp_check);
1302 bcopy(bp->b_data, bdata, chain->bytes);
1305 * Device buffer is now valid, chain is no
1306 * longer in the initial state.
1308 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1310 if (ioflag & IO_SYNC) {
1312 * Synchronous I/O requested.
1314 hammer2_io_bwrite(&dio);
1316 } else if ((ioflag & IO_DIRECT) && loff + n == pblksize) {
1317 hammer2_io_bdwrite(&dio);
1319 } else if (ioflag & IO_ASYNC) {
1320 hammer2_io_bawrite(&dio);
1322 hammer2_io_bdwrite(&dio);
1326 panic("hammer2_write_bp: bad chain type %d\n",
1337 hammer2_remount(hammer2_mount_t *hmp, char *path, struct vnode *devvp,
1345 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1347 hammer2_pfsmount_t *pmp;
1348 hammer2_mount_t *hmp;
1349 hammer2_chain_t *rchain;
1352 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1355 struct vnode *devvp;
1359 ccms_domain_uninit(&pmp->ccms_dom);
1360 kdmsg_iocom_uninit(&pmp->iocom); /* XXX chain dependency */
1362 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1365 * If mount initialization proceeded far enough we must flush
1368 if (mntflags & MNT_FORCE)
1373 error = vflush(mp, 0, flags);
1378 if (pmp->wthread_td) {
1379 mtx_lock(&pmp->wthread_mtx);
1380 pmp->wthread_destroy = 1;
1381 wakeup(&pmp->wthread_bioq);
1382 while (pmp->wthread_destroy != -1) {
1383 mtxsleep(&pmp->wthread_destroy,
1384 &pmp->wthread_mtx, 0,
1387 mtx_unlock(&pmp->wthread_mtx);
1388 pmp->wthread_td = NULL;
1391 for (i = 0; i < pmp->cluster.nchains; ++i) {
1392 hmp = pmp->cluster.chains[i]->hmp;
1394 hammer2_mount_exlock(hmp);
1397 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n",
1398 hmp, hmp->pmp_count);
1401 * Flush any left over chains. The voldata lock is only used
1402 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
1404 hammer2_voldata_lock(hmp);
1405 if (((hmp->vchain.flags | hmp->fchain.flags) &
1406 HAMMER2_CHAIN_MODIFIED) ||
1407 hmp->vchain.core->update_hi > hmp->voldata.mirror_tid ||
1408 hmp->fchain.core->update_hi > hmp->voldata.freemap_tid) {
1409 hammer2_voldata_unlock(hmp, 0);
1410 hammer2_vfs_sync(mp, MNT_WAIT);
1411 hammer2_vfs_sync(mp, MNT_WAIT);
1413 hammer2_voldata_unlock(hmp, 0);
1415 if (hmp->pmp_count == 0) {
1416 if (((hmp->vchain.flags | hmp->fchain.flags) &
1417 HAMMER2_CHAIN_MODIFIED) ||
1418 (hmp->vchain.core->update_hi >
1419 hmp->voldata.mirror_tid) ||
1420 (hmp->fchain.core->update_hi >
1421 hmp->voldata.freemap_tid)) {
1422 kprintf("hammer2_unmount: chains left over "
1423 "after final sync\n");
1424 if (hammer2_debug & 0x0010)
1425 Debugger("entered debugger");
1430 * Cleanup the root and super-root chain elements
1431 * (which should be clean).
1434 #if REPORT_REFS_ERRORS
1435 if (pmp->iroot->refs != 1)
1436 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1437 pmp->iroot, pmp->iroot->refs);
1439 KKASSERT(pmp->iroot->refs == 1);
1441 /* ref for pmp->iroot */
1442 hammer2_inode_drop(pmp->iroot);
1446 rchain = pmp->cluster.chains[i];
1448 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
1449 #if REPORT_REFS_ERRORS
1450 if (rchain->refs != 1)
1451 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1452 rchain, rchain->refs);
1454 KKASSERT(rchain->refs == 1);
1456 hammer2_chain_drop(rchain);
1457 pmp->cluster.chains[i] = NULL;
1461 * If no PFS's left drop the master hammer2_mount for the
1464 if (hmp->pmp_count == 0) {
1466 hammer2_inode_drop(hmp->sroot);
1471 * Finish up with the device vnode
1473 if ((devvp = hmp->devvp) != NULL) {
1474 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1475 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1478 (ronly ? FREAD : FREAD|FWRITE));
1485 * Final drop of embedded freemap root chain to
1486 * clean up fchain.core (fchain structure is not
1487 * flagged ALLOCATED so it is cleaned out and then
1490 hammer2_chain_drop(&hmp->fchain);
1493 * Final drop of embedded volume root chain to clean
1494 * up vchain.core (vchain structure is not flagged
1495 * ALLOCATED so it is cleaned out and then left to
1499 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
1501 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt);
1502 hammer2_mount_unlock(hmp);
1503 hammer2_chain_drop(&hmp->vchain);
1505 hammer2_io_cleanup(hmp, &hmp->iotree);
1506 if (hmp->iofree_count) {
1507 kprintf("io_cleanup: %d I/O's left hanging\n",
1511 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1512 kmalloc_destroy(&hmp->mchain);
1513 kfree(hmp, M_HAMMER2);
1515 hammer2_mount_unlock(hmp);
1520 mp->mnt_data = NULL;
1522 kmalloc_destroy(&pmp->mmsg);
1523 kmalloc_destroy(&pmp->minode);
1525 kfree(pmp, M_HAMMER2);
1529 lockmgr(&hammer2_mntlk, LK_RELEASE);
1536 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1537 ino_t ino, struct vnode **vpp)
1539 kprintf("hammer2_vget\n");
1540 return (EOPNOTSUPP);
1545 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1547 hammer2_pfsmount_t *pmp;
1548 hammer2_chain_t *parent;
1553 if (pmp->iroot == NULL) {
1557 parent = hammer2_inode_lock_sh(pmp->iroot);
1558 vp = hammer2_igetv(pmp->iroot, &error);
1559 hammer2_inode_unlock_sh(pmp->iroot, parent);
1562 kprintf("vnodefail\n");
1571 * XXX incorporate ipdata->inode_quota and data_quota
1575 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1577 hammer2_pfsmount_t *pmp;
1578 hammer2_mount_t *hmp;
1581 KKASSERT(pmp->cluster.nchains >= 1);
1582 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
1584 mp->mnt_stat.f_files = pmp->inode_count;
1585 mp->mnt_stat.f_ffree = 0;
1586 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1587 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1588 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1590 *sbp = mp->mnt_stat;
1596 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1598 hammer2_pfsmount_t *pmp;
1599 hammer2_mount_t *hmp;
1602 KKASSERT(pmp->cluster.nchains >= 1);
1603 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
1605 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1606 mp->mnt_vstat.f_files = pmp->inode_count;
1607 mp->mnt_vstat.f_ffree = 0;
1608 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1609 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1610 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1612 *sbp = mp->mnt_vstat;
1617 * Sync the entire filesystem; this is called from the filesystem syncer
1618 * process periodically and whenever a user calls sync(1) on the hammer
1621 * Currently is actually called from the syncer! \o/
1623 * This task will have to snapshot the state of the dirty inode chain.
1624 * From that, it will have to make sure all of the inodes on the dirty
1625 * chain have IO initiated. We make sure that io is initiated for the root
1628 * If waitfor is set, we wait for media to acknowledge the new rootblock.
1630 * THINKS: side A vs side B, to have sync not stall all I/O?
1633 hammer2_vfs_sync(struct mount *mp, int waitfor)
1635 struct hammer2_sync_info info;
1636 hammer2_chain_t *chain;
1637 hammer2_pfsmount_t *pmp;
1638 hammer2_mount_t *hmp;
1648 * We can't acquire locks on existing vnodes while in a transaction
1649 * without risking a deadlock. This assumes that vfsync() can be
1650 * called without the vnode locked (which it can in DragonFly).
1651 * Otherwise we'd have to implement a multi-pass or flag the lock
1652 * failures and retry.
1654 * The reclamation code interlocks with the sync list's token
1655 * (by removing the vnode from the scan list) before unlocking
1656 * the inode, giving us time to ref the inode.
1658 /*flags = VMSC_GETVP;*/
1660 if (waitfor & MNT_LAZY)
1661 flags |= VMSC_ONEPASS;
1664 * Initialize a normal transaction and sync everything out, then
1665 * wait for pending I/O to finish (so it gets a transaction id
1666 * that the meta-data flush will catch).
1668 hammer2_trans_init(&info.trans, pmp, 0);
1670 info.waitfor = MNT_NOWAIT;
1671 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1673 if (info.error == 0 && (waitfor & MNT_WAIT)) {
1674 info.waitfor = waitfor;
1675 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
1678 hammer2_trans_done(&info.trans);
1679 hammer2_bioq_sync(info.trans.pmp);
1682 * Start the flush transaction and flush all meta-data.
1684 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH);
1687 for (i = 0; i < pmp->cluster.nchains; ++i) {
1688 hmp = pmp->cluster.chains[i]->hmp;
1691 * Media mounts have two 'roots', vchain for the topology
1692 * and fchain for the free block table. Flush both.
1694 * Note that the topology and free block table are handled
1695 * independently, so the free block table can wind up being
1696 * ahead of the topology. We depend on the bulk free scan
1697 * code to deal with any loose ends.
1699 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1700 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) ||
1701 hmp->vchain.core->update_hi > hmp->voldata.mirror_tid) {
1702 chain = &hmp->vchain;
1703 hammer2_chain_flush(&info.trans, &chain);
1704 KKASSERT(chain == &hmp->vchain);
1705 hmp->voldata.mirror_tid = chain->bref.mirror_tid;
1710 hammer2_chain_unlock(&hmp->vchain);
1712 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1713 if ((hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) ||
1714 hmp->fchain.core->update_hi > hmp->voldata.freemap_tid ||
1716 /* this will also modify vchain as a side effect */
1717 chain = &hmp->fchain;
1718 hammer2_chain_flush(&info.trans, &chain);
1719 KKASSERT(chain == &hmp->fchain);
1720 hmp->voldata.freemap_tid = chain->bref.mirror_tid;
1722 hammer2_chain_unlock(&hmp->fchain);
1727 * We can't safely flush the volume header until we have
1728 * flushed any device buffers which have built up.
1730 * XXX this isn't being incremental
1732 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
1733 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
1734 vn_unlock(hmp->devvp);
1737 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
1738 * volume header needs synchronization via hmp->volsync.
1740 * XXX synchronize the flag & data with only this flush XXX
1743 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
1747 * Synchronize the disk before flushing the volume
1751 bp->b_bio1.bio_offset = 0;
1754 bp->b_cmd = BUF_CMD_FLUSH;
1755 bp->b_bio1.bio_done = biodone_sync;
1756 bp->b_bio1.bio_flags |= BIO_SYNC;
1757 vn_strategy(hmp->devvp, &bp->b_bio1);
1758 biowait(&bp->b_bio1, "h2vol");
1762 * Then we can safely flush the version of the
1763 * volume header synchronized by the flush code.
1765 i = hmp->volhdrno + 1;
1766 if (i >= HAMMER2_NUM_VOLHDRS)
1768 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
1769 hmp->volsync.volu_size) {
1772 kprintf("sync volhdr %d %jd\n",
1773 i, (intmax_t)hmp->volsync.volu_size);
1774 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1775 HAMMER2_PBUFSIZE, 0, 0);
1776 atomic_clear_int(&hmp->vchain.flags,
1777 HAMMER2_CHAIN_VOLUMESYNC);
1778 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
1783 total_error = error;
1785 hammer2_trans_done(&info.trans);
1787 return (total_error);
1793 * NOTE: We don't test update_lo/update_hi or MOVED here because the fsync
1794 * code won't flush on those flags. The syncer code above will do a
1795 * general meta-data flush globally that will catch these flags.
1799 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1801 struct hammer2_sync_info *info = data;
1802 hammer2_inode_t *ip;
1811 if (vp->v_type == VNON || vp->v_type == VBAD) {
1815 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
1816 RB_EMPTY(&vp->v_rbdirty_tree)) {
1822 * VOP_FSYNC will start a new transaction so replicate some code
1823 * here to do it inline (see hammer2_vop_fsync()).
1825 * WARNING: The vfsync interacts with the buffer cache and might
1826 * block, we can't hold the inode lock at that time.
1827 * However, we MUST ref ip before blocking to ensure that
1828 * it isn't ripped out from under us (since we do not
1829 * hold a lock on the vnode).
1831 hammer2_inode_ref(ip);
1832 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1834 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
1838 * XXX this interferes with flush operations mainly because the
1839 * same transaction id is being used by asynchronous buffer
1840 * operations above and can be reordered after the flush
1843 parent = hammer2_inode_lock_ex(ip);
1844 hammer2_chain_flush(&info->trans, &parent);
1845 hammer2_inode_unlock_ex(ip, parent);
1847 hammer2_inode_drop(ip);
1850 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
1853 info->error = error;
1859 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1866 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1867 struct fid *fhp, struct vnode **vpp)
1874 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1875 int *exflagsp, struct ucred **credanonp)
1881 * Support code for hammer2_mount(). Read, verify, and install the volume
1882 * header into the HMP
1884 * XXX read four volhdrs and use the one with the highest TID whos CRC
1889 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1890 * nonexistant locations.
1892 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
1896 hammer2_install_volume_header(hammer2_mount_t *hmp)
1898 hammer2_volume_data_t *vd;
1900 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
1912 * There are up to 4 copies of the volume header (syncs iterate
1913 * between them so there is no single master). We don't trust the
1914 * volu_size field so we don't know precisely how large the filesystem
1915 * is, so depend on the OS to return an error if we go beyond the
1916 * block device's EOF.
1918 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1919 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1920 HAMMER2_VOLUME_BYTES, &bp);
1927 vd = (struct hammer2_volume_data *) bp->b_data;
1928 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1929 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1935 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1936 /* XXX: Reversed-endianness filesystem */
1937 kprintf("hammer2: reverse-endian filesystem detected");
1943 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1944 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1945 HAMMER2_VOLUME_ICRC0_SIZE);
1946 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1947 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1948 HAMMER2_VOLUME_ICRC1_SIZE);
1949 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1950 kprintf("hammer2 volume header crc "
1951 "mismatch copy #%d %08x/%08x\n",
1958 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1967 hmp->volsync = hmp->voldata;
1969 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1970 kprintf("hammer2: using volume header #%d\n",
1975 kprintf("hammer2: no valid volume headers found!\n");
1981 * Reconnect using the passed file pointer. The caller must ref the
1985 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1987 hammer2_inode_data_t *ipdata;
1988 hammer2_chain_t *parent;
1989 hammer2_mount_t *hmp;
1992 hmp = pmp->cluster.chains[0]->hmp; /* XXX */
1995 * Closes old comm descriptor, kills threads, cleans up
1996 * states, then installs the new descriptor and creates
1999 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
2002 * Setup LNK_CONN fields for autoinitiated state machine
2004 parent = hammer2_inode_lock_ex(pmp->iroot);
2005 ipdata = &parent->data->ipdata;
2006 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
2007 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
2008 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
2009 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
2010 pmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
2013 * Filter adjustment. Clients do not need visibility into other
2014 * clients (otherwise millions of clients would present a serious
2015 * problem). The fs_label also serves to restrict the namespace.
2017 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
2018 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
2019 switch (ipdata->pfs_type) {
2020 case DMSG_PFSTYPE_CLIENT:
2021 pmp->iocom.auto_lnk_conn.peer_mask &=
2022 ~(1LLU << DMSG_PFSTYPE_CLIENT);
2028 name_len = ipdata->name_len;
2029 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
2030 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
2031 bcopy(ipdata->filename,
2032 pmp->iocom.auto_lnk_conn.fs_label,
2034 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
2037 * Setup LNK_SPAN fields for autoinitiated state machine
2039 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
2040 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
2041 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
2042 pmp->iocom.auto_lnk_span.peer_type = hmp->voldata.peer_type;
2043 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
2044 name_len = ipdata->name_len;
2045 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
2046 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
2047 bcopy(ipdata->filename,
2048 pmp->iocom.auto_lnk_span.fs_label,
2050 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
2051 hammer2_inode_unlock_ex(pmp->iroot, parent);
2053 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
2057 hammer2_rcvdmsg(kdmsg_msg_t *msg)
2059 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
2060 case DMSG_DBG_SHELL:
2063 * Execute shell command (not supported atm)
2065 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2067 case DMSG_DBG_SHELL | DMSGF_REPLY:
2071 if (msg->aux_data) {
2072 msg->aux_data[msg->aux_size - 1] = 0;
2073 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
2078 * Unsupported message received. We only need to
2079 * reply if it's a transaction in order to close our end.
2080 * Ignore any one-way messages are any further messages
2081 * associated with the transaction.
2083 * NOTE: This case also includes DMSG_LNK_ERROR messages
2084 * which might be one-way, replying to those would
2085 * cause an infinite ping-pong.
2087 if (msg->any.head.cmd & DMSGF_CREATE)
2088 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2095 * This function is called after KDMSG has automatically handled processing
2096 * of a LNK layer message (typically CONN, SPAN, or CIRC).
2098 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
2099 * advertises all available hammer2 super-root volumes.
2102 hammer2_autodmsg(kdmsg_msg_t *msg)
2104 hammer2_pfsmount_t *pmp = msg->iocom->handle;
2105 hammer2_mount_t *hmp = pmp->cluster.chains[0]->hmp; /* XXX */
2109 * We only care about replies to our LNK_CONN auto-request. kdmsg
2110 * has already processed the reply, we use this calback as a shim
2111 * to know when we can advertise available super-root volumes.
2113 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
2114 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
2115 msg->state == NULL) {
2119 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
2121 if (msg->any.head.cmd & DMSGF_CREATE) {
2122 kprintf("HAMMER2: VOLDATA DUMP\n");
2125 * Dump the configuration stored in the volume header
2127 hammer2_voldata_lock(hmp);
2128 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
2129 if (hmp->voldata.copyinfo[copyid].copyid == 0)
2131 hammer2_volconf_update(pmp, copyid);
2133 hammer2_voldata_unlock(hmp, 0);
2135 if ((msg->any.head.cmd & DMSGF_DELETE) &&
2136 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
2137 kprintf("HAMMER2: CONN WAS TERMINATED\n");
2142 * Volume configuration updates are passed onto the userland service
2143 * daemon via the open LNK_CONN transaction.
2146 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
2148 hammer2_mount_t *hmp = pmp->cluster.chains[0]->hmp; /* XXX */
2151 /* XXX interlock against connection state termination */
2152 kprintf("volconf update %p\n", pmp->iocom.conn_state);
2153 if (pmp->iocom.conn_state) {
2154 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
2155 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
2156 DMSG_LNK_VOLCONF, NULL, NULL);
2157 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
2158 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
2159 msg->any.lnk_volconf.index = index;
2160 kdmsg_msg_write(msg);
2165 * This handles hysteresis on regular file flushes. Because the BIOs are
2166 * routed to a thread it is possible for an excessive number to build up
2167 * and cause long front-end stalls long before the runningbuffspace limit
2168 * is hit, so we implement hammer2_flush_pipe to control the
2171 * This is a particular problem when compression is used.
2174 hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp)
2176 atomic_add_int(&pmp->count_lwinprog, 1);
2180 hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp)
2184 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2185 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2186 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2187 atomic_clear_int(&pmp->count_lwinprog,
2188 HAMMER2_LWINPROG_WAITING);
2189 wakeup(&pmp->count_lwinprog);
2194 hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp)
2199 lwinprog = pmp->count_lwinprog;
2201 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2203 tsleep_interlock(&pmp->count_lwinprog, 0);
2204 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2205 lwinprog = pmp->count_lwinprog;
2206 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2208 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2213 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
2215 hammer2_chain_layer_t *layer;
2216 hammer2_chain_t *scan;
2217 hammer2_chain_t *first_parent;
2221 kprintf("%*.*s...\n", tab, tab, "");
2226 first_parent = chain->core ? TAILQ_FIRST(&chain->core->ownerq) : NULL;
2227 kprintf("%*.*schain %p.%d %016jx/%d mir=%016jx\n",
2229 chain, chain->bref.type,
2230 chain->bref.key, chain->bref.keybits,
2231 chain->bref.mirror_tid);
2233 kprintf("%*.*s [%08x] (%s) dt=%016jx refs=%d\n",
2236 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2237 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2241 kprintf("%*.*s core %p [%08x] lo=%08jx hi=%08jx fp=%p np=%p",
2243 chain->core, (chain->core ? chain->core->flags : 0),
2244 (chain->core ? chain->core->update_lo : -1),
2245 (chain->core ? chain->core->update_hi : -1),
2247 (first_parent ? TAILQ_NEXT(chain, core_entry) : NULL));
2250 kprintf(" [fpflags %08x fprefs %d\n",
2251 first_parent->flags,
2252 first_parent->refs);
2253 if (chain->core == NULL || TAILQ_EMPTY(&chain->core->layerq))
2258 TAILQ_FOREACH(layer, &chain->core->layerq, entry) {
2259 RB_FOREACH(scan, hammer2_chain_tree, &layer->rbtree) {
2260 hammer2_dump_chain(scan, tab + 4, countp);
2264 if (chain->core && !TAILQ_EMPTY(&chain->core->layerq)) {
2265 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2266 kprintf("%*.*s}(%s)\n", tab, tab, "",
2267 chain->data->ipdata.filename);
2269 kprintf("%*.*s}\n", tab, tab, "");