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 long hammer2_iod_file_read;
85 long hammer2_iod_meta_read;
86 long hammer2_iod_indr_read;
87 long hammer2_iod_fmap_read;
88 long hammer2_iod_volu_read;
89 long hammer2_iod_file_write;
90 long hammer2_iod_meta_write;
91 long hammer2_iod_indr_write;
92 long hammer2_iod_fmap_write;
93 long hammer2_iod_volu_write;
94 long hammer2_ioa_file_read;
95 long hammer2_ioa_meta_read;
96 long hammer2_ioa_indr_read;
97 long hammer2_ioa_fmap_read;
98 long hammer2_ioa_volu_read;
99 long hammer2_ioa_fmap_write;
100 long hammer2_ioa_file_write;
101 long hammer2_ioa_meta_write;
102 long hammer2_ioa_indr_write;
103 long hammer2_ioa_volu_write;
105 MALLOC_DECLARE(C_BUFFER);
106 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
108 MALLOC_DECLARE(D_BUFFER);
109 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
111 MALLOC_DECLARE(W_BIOQUEUE);
112 MALLOC_DEFINE(W_BIOQUEUE, "wbioqueue", "Writing bio queue.");
114 MALLOC_DECLARE(W_MTX);
115 MALLOC_DEFINE(W_MTX, "wmutex", "Mutex for write thread.");
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, "");
126 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
127 &hammer2_iod_file_read, 0, "");
128 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
129 &hammer2_iod_meta_read, 0, "");
130 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
131 &hammer2_iod_indr_read, 0, "");
132 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
133 &hammer2_iod_fmap_read, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
135 &hammer2_iod_volu_read, 0, "");
137 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
138 &hammer2_iod_file_write, 0, "");
139 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
140 &hammer2_iod_meta_write, 0, "");
141 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
142 &hammer2_iod_indr_write, 0, "");
143 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
144 &hammer2_iod_fmap_write, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
146 &hammer2_iod_volu_write, 0, "");
148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
149 &hammer2_ioa_file_read, 0, "");
150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
151 &hammer2_ioa_meta_read, 0, "");
152 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
153 &hammer2_ioa_indr_read, 0, "");
154 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
155 &hammer2_ioa_fmap_read, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
157 &hammer2_ioa_volu_read, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
160 &hammer2_ioa_file_write, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
162 &hammer2_ioa_meta_write, 0, "");
163 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
164 &hammer2_ioa_indr_write, 0, "");
165 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
166 &hammer2_ioa_fmap_write, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
168 &hammer2_ioa_volu_write, 0, "");
170 static int hammer2_vfs_init(struct vfsconf *conf);
171 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
172 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
174 static int hammer2_remount(struct mount *, char *, struct vnode *,
176 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
177 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
178 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
180 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
182 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
183 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
184 ino_t ino, struct vnode **vpp);
185 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
186 struct fid *fhp, struct vnode **vpp);
187 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
188 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
189 int *exflagsp, struct ucred **credanonp);
191 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
192 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
193 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
195 static void hammer2_write_thread(void *arg);
198 * Functions for compression in threads,
199 * from hammer2_vnops.c
201 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
203 hammer2_inode_data_t *ipdata,
204 hammer2_chain_t **parentp,
205 hammer2_key_t lbase, int ioflag, int pblksize,
207 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
209 hammer2_inode_data_t *ipdata,
210 hammer2_chain_t **parentp,
211 hammer2_key_t lbase, int ioflag,
212 int pblksize, int *errorp, int comp_method);
213 static void hammer2_zero_check_and_write(struct buf *bp,
214 hammer2_trans_t *trans, hammer2_inode_t *ip,
215 hammer2_inode_data_t *ipdata,
216 hammer2_chain_t **parentp,
218 int ioflag, int pblksize, int *errorp);
219 static int test_block_not_zeros(char *buf, size_t bytes);
220 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
222 hammer2_inode_data_t *ipdata,
223 hammer2_chain_t **parentp,
226 static void hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp,
227 int ioflag, int pblksize, int *errorp);
229 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
230 static void hammer2_autodmsg(kdmsg_msg_t *msg);
234 * HAMMER2 vfs operations.
236 static struct vfsops hammer2_vfsops = {
237 .vfs_init = hammer2_vfs_init,
238 .vfs_uninit = hammer2_vfs_uninit,
239 .vfs_sync = hammer2_vfs_sync,
240 .vfs_mount = hammer2_vfs_mount,
241 .vfs_unmount = hammer2_vfs_unmount,
242 .vfs_root = hammer2_vfs_root,
243 .vfs_statfs = hammer2_vfs_statfs,
244 .vfs_statvfs = hammer2_vfs_statvfs,
245 .vfs_vget = hammer2_vfs_vget,
246 .vfs_vptofh = hammer2_vfs_vptofh,
247 .vfs_fhtovp = hammer2_vfs_fhtovp,
248 .vfs_checkexp = hammer2_vfs_checkexp
251 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
253 VFS_SET(hammer2_vfsops, hammer2, 0);
254 MODULE_VERSION(hammer2, 1);
258 hammer2_vfs_init(struct vfsconf *conf)
260 static struct objcache_malloc_args margs_read;
261 static struct objcache_malloc_args margs_write;
267 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
269 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
271 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
275 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
277 margs_read.objsize = 65536;
278 margs_read.mtype = D_BUFFER;
280 margs_write.objsize = 32768;
281 margs_write.mtype = C_BUFFER;
283 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
284 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
285 objcache_malloc_free, &margs_read);
286 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
287 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
288 objcache_malloc_free, &margs_write);
290 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
291 TAILQ_INIT(&hammer2_mntlist);
298 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
300 objcache_destroy(cache_buffer_read);
301 objcache_destroy(cache_buffer_write);
306 * Mount or remount HAMMER2 fileystem from physical media
309 * mp mount point structure
315 * mp mount point structure
316 * path path to mount point
317 * data pointer to argument structure in user space
318 * volume volume path (device@LABEL form)
319 * hflags user mount flags
320 * cred user credentials
327 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
330 struct hammer2_mount_info info;
331 hammer2_pfsmount_t *pmp;
332 hammer2_mount_t *hmp;
335 struct nlookupdata nd;
336 hammer2_chain_t *parent;
337 hammer2_chain_t *schain;
338 hammer2_chain_t *rchain;
340 char devstr[MNAMELEN];
355 kprintf("hammer2_mount\n");
361 bzero(&info, sizeof(info));
362 info.cluster_fd = -1;
366 * Non-root mount or updating a mount
368 error = copyin(data, &info, sizeof(info));
372 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
376 /* Extract device and label */
378 label = strchr(devstr, '@');
380 ((label + 1) - dev) > done) {
388 if (mp->mnt_flag & MNT_UPDATE) {
390 /* HAMMER2 implements NFS export via mountctl */
393 error = hammer2_remount(mp, path, devvp, cred);
401 * Lookup name and verify it refers to a block device.
403 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
405 error = nlookup(&nd);
407 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
411 if (vn_isdisk(devvp, &error))
412 error = vfs_mountedon(devvp);
416 * Determine if the device has already been mounted. After this
417 * check hmp will be non-NULL if we are doing the second or more
418 * hammer2 mounts from the same device.
420 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
421 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
422 if (hmp->devvp == devvp)
427 * Open the device if this isn't a secondary mount and construct
428 * the H2 device mount (hmp).
431 if (error == 0 && vcount(devvp) > 0)
435 * Now open the device
438 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
439 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
440 error = vinvalbuf(devvp, V_SAVE, 0, 0);
442 error = VOP_OPEN(devvp,
443 ronly ? FREAD : FREAD | FWRITE,
448 if (error && devvp) {
453 lockmgr(&hammer2_mntlk, LK_RELEASE);
456 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
459 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
460 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
462 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
463 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
464 TAILQ_INIT(&hmp->transq);
467 * vchain setup. vchain.data is embedded.
468 * vchain.refs is initialized and will never drop to 0.
470 hmp->vchain.hmp = hmp;
471 hmp->vchain.refs = 1;
472 hmp->vchain.data = (void *)&hmp->voldata;
473 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
474 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
475 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
476 hammer2_chain_core_alloc(&hmp->vchain, NULL);
477 /* hmp->vchain.u.xxx is left NULL */
480 * fchain setup. fchain.data is embedded.
481 * fchain.refs is initialized and will never drop to 0.
483 * The data is not used but needs to be initialized to
484 * pass assertion muster. We use this chain primarily
485 * as a placeholder for the freemap's top-level RBTREE
486 * so it does not interfere with the volume's topology
489 hmp->fchain.hmp = hmp;
490 hmp->fchain.refs = 1;
491 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
492 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
493 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
494 hmp->fchain.bref.methods =
495 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
496 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
497 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
499 hammer2_chain_core_alloc(&hmp->fchain, NULL);
500 /* hmp->fchain.u.xxx is left NULL */
503 * Install the volume header
505 error = hammer2_install_volume_header(hmp);
507 hammer2_vfs_unmount(mp, MNT_FORCE);
512 * First locate the super-root inode, which is key 0
513 * relative to the volume header's blockset.
515 * Then locate the root inode by scanning the directory keyspace
516 * represented by the label.
518 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
519 schain = hammer2_chain_lookup(&parent,
520 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
521 hammer2_chain_lookup_done(parent);
522 if (schain == NULL) {
523 kprintf("hammer2_mount: invalid super-root\n");
524 hammer2_vfs_unmount(mp, MNT_FORCE);
527 hammer2_chain_ref(schain); /* for hmp->schain */
528 hmp->schain = schain; /* left locked for inode_get */
529 hmp->sroot = hammer2_inode_get(NULL, NULL, schain);
530 hammer2_inode_ref(hmp->sroot); /* for hmp->sroot */
531 hammer2_inode_unlock_ex(hmp->sroot, schain);
534 mtx_init(&hmp->wthread_mtx);
535 bioq_init(&hmp->wthread_bioq);
536 hmp->wthread_destroy = 0;
541 lwkt_create(hammer2_write_thread, hmp,
542 NULL, NULL, 0, -1, "hammer2-write");
546 * Block device opened successfully, finish initializing the
549 * From this point on we have to call hammer2_unmount() on failure.
551 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
552 pmp->mount_cluster = kmalloc(sizeof(hammer2_cluster_t), M_HAMMER2,
554 pmp->cluster = pmp->mount_cluster;
556 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
557 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
559 pmp->mount_cluster->hmp = hmp;
560 spin_init(&pmp->inum_spin);
561 RB_INIT(&pmp->inum_tree);
563 kdmsg_iocom_init(&pmp->iocom, pmp,
564 KDMSG_IOCOMF_AUTOCONN |
565 KDMSG_IOCOMF_AUTOSPAN |
566 KDMSG_IOCOMF_AUTOCIRC,
567 pmp->mmsg, hammer2_rcvdmsg);
569 ccms_domain_init(&pmp->ccms_dom);
571 lockmgr(&hammer2_mntlk, LK_RELEASE);
572 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
573 hmp, pmp, hmp->pmp_count);
575 mp->mnt_flag = MNT_LOCAL;
576 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
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 * schain only has 1 ref now for its hmp->schain assignment.
596 * Setup for lookup (which will lock it).
598 parent = hammer2_chain_lookup_init(hmp->schain, 0);
599 lhc = hammer2_dirhash(label, strlen(label));
600 rchain = hammer2_chain_lookup(&parent,
601 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
604 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
605 strcmp(label, rchain->data->ipdata.filename) == 0) {
608 rchain = hammer2_chain_next(&parent, rchain,
609 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
612 hammer2_chain_lookup_done(parent);
613 if (rchain == NULL) {
614 kprintf("hammer2_mount: PFS label not found\n");
615 hammer2_vfs_unmount(mp, MNT_FORCE);
618 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
619 hammer2_chain_unlock(rchain);
620 kprintf("hammer2_mount: PFS label already mounted!\n");
621 hammer2_vfs_unmount(mp, MNT_FORCE);
624 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
625 kprintf("hammer2_mount: PFS label currently recycling\n");
626 hammer2_vfs_unmount(mp, MNT_FORCE);
630 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
633 * NOTE: *_get() integrates chain's lock into the inode lock.
635 hammer2_chain_ref(rchain); /* for pmp->rchain */
636 pmp->mount_cluster->rchain = rchain; /* left held & unlocked */
637 pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
638 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
640 KKASSERT(rchain->pmp == NULL); /* tracking pmp for rchain */
642 atomic_add_long(&pmp->inmem_chains, 1);
644 hammer2_inode_unlock_ex(pmp->iroot, rchain);
646 kprintf("iroot %p\n", pmp->iroot);
649 * Ref the cluster management messaging descriptor. The mount
650 * program deals with the other end of the communications pipe.
652 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
654 kprintf("hammer2_mount: bad cluster_fd!\n");
655 hammer2_vfs_unmount(mp, MNT_FORCE);
658 hammer2_cluster_reconnect(pmp, fp);
664 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
665 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
666 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
668 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
669 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
670 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
671 copyinstr(path, mp->mnt_stat.f_mntonname,
672 sizeof(mp->mnt_stat.f_mntonname) - 1,
676 * Initial statfs to prime mnt_stat.
678 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
684 * Handle bioq for strategy write
688 hammer2_write_thread(void *arg)
690 hammer2_mount_t* hmp;
693 hammer2_trans_t trans;
695 hammer2_inode_t *last_ip;
697 hammer2_chain_t *parent;
698 hammer2_chain_t **parentp;
699 hammer2_inode_data_t *ipdata;
707 mtx_lock(&hmp->wthread_mtx);
708 while (hmp->wthread_destroy == 0) {
709 if (bioq_first(&hmp->wthread_bioq) == NULL) {
710 mtxsleep(&hmp->wthread_bioq, &hmp->wthread_mtx,
717 while ((bio = bioq_takefirst(&hmp->wthread_bioq)) != NULL) {
718 mtx_unlock(&hmp->wthread_mtx);
726 * Cache transaction for multi-buffer flush efficiency.
727 * Lock the ip separately for each buffer to allow
728 * interleaving with frontend writes.
732 hammer2_trans_done(&trans);
733 hammer2_trans_init(&trans, ip->pmp,
734 HAMMER2_TRANS_BUFCACHE);
737 parent = hammer2_inode_lock_ex(ip);
740 * Inode is modified, flush size and mtime changes
741 * to ensure that the file size remains consistent
742 * with the buffers being flushed.
744 if (ip->flags & (HAMMER2_INODE_RESIZED |
745 HAMMER2_INODE_MTIME)) {
746 hammer2_inode_fsync(&trans, ip, parentp);
748 ipdata = hammer2_chain_modify_ip(&trans, ip,
750 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
752 pblksize = hammer2_calc_physical(ip, lbase);
753 hammer2_write_file_core(bp, &trans, ip, ipdata,
757 hammer2_inode_unlock_ex(ip, parent);
759 kprintf("hammer2: error in buffer write\n");
760 bp->b_flags |= B_ERROR;
764 mtx_lock(&hmp->wthread_mtx);
768 * Clean out transaction cache
771 hammer2_trans_done(&trans);
773 hmp->wthread_destroy = -1;
774 wakeup(&hmp->wthread_destroy);
776 mtx_unlock(&hmp->wthread_mtx);
780 * From hammer2_vnops.c.
781 * Physical block assignement function.
785 hammer2_assign_physical(hammer2_trans_t *trans,
786 hammer2_inode_t *ip, hammer2_chain_t **parentp,
787 hammer2_key_t lbase, int pblksize, int *errorp)
789 hammer2_chain_t *parent;
790 hammer2_chain_t *chain;
792 int pradix = hammer2_getradix(pblksize);
795 * Locate the chain associated with lbase, return a locked chain.
796 * However, do not instantiate any data reference (which utilizes a
797 * device buffer) because we will be using direct IO via the
798 * logical buffer cache buffer.
803 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); /* extra lock */
804 chain = hammer2_chain_lookup(&parent,
806 HAMMER2_LOOKUP_NODATA);
810 * We found a hole, create a new chain entry.
812 * NOTE: DATA chains are created without device backing
813 * store (nor do we want any).
815 *errorp = hammer2_chain_create(trans, &parent, &chain,
816 lbase, HAMMER2_PBUFRADIX,
817 HAMMER2_BREF_TYPE_DATA,
820 hammer2_chain_lookup_done(parent);
821 panic("hammer2_chain_create: par=%p error=%d\n",
826 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
827 /*ip->delta_dcount += pblksize;*/
829 switch (chain->bref.type) {
830 case HAMMER2_BREF_TYPE_INODE:
832 * The data is embedded in the inode. The
833 * caller is responsible for marking the inode
834 * modified and copying the data to the embedded
839 case HAMMER2_BREF_TYPE_DATA:
840 if (chain->bytes != pblksize) {
841 hammer2_chain_resize(trans, ip,
844 HAMMER2_MODIFY_OPTDATA);
846 hammer2_chain_modify(trans, &chain,
847 HAMMER2_MODIFY_OPTDATA);
848 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
851 panic("hammer2_assign_physical: bad type");
859 * Cleanup. If chain wound up being the inode (i.e. DIRECTDATA),
860 * we might have to replace *parentp.
862 hammer2_chain_lookup_done(parent);
864 if (*parentp != chain &&
865 (*parentp)->core == chain->core) {
867 *parentp = chain; /* eats lock */
868 hammer2_chain_unlock(parent);
869 hammer2_chain_lock(chain, 0); /* need another */
871 /* else chain already locked for return */
877 * From hammer2_vnops.c.
878 * The core write function which determines which path to take
879 * depending on compression settings.
883 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
884 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
885 hammer2_chain_t **parentp,
886 hammer2_key_t lbase, int ioflag, int pblksize,
889 hammer2_chain_t *chain;
890 if (ipdata->comp_algo > HAMMER2_COMP_AUTOZERO) {
891 hammer2_compress_and_write(bp, trans, ip,
896 } else if (ipdata->comp_algo == HAMMER2_COMP_AUTOZERO) {
897 hammer2_zero_check_and_write(bp, trans, ip,
898 ipdata, parentp, lbase,
899 ioflag, pblksize, errorp);
902 * We have to assign physical storage to the buffer
903 * we intend to dirty or write now to avoid deadlocks
904 * in the strategy code later.
906 * This can return NOOFFSET for inode-embedded data.
907 * The strategy code will take care of it in that case.
909 chain = hammer2_assign_physical(trans, ip, parentp,
912 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
914 hammer2_chain_unlock(chain);
916 ipdata = &ip->chain->data->ipdata; /* reload */
920 * From hammer2_vnops.c
921 * Generic function that will perform the compression in compression
922 * write path. The compression algorithm is determined by the settings
923 * obtained from inode.
927 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
928 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
929 hammer2_chain_t **parentp,
930 hammer2_key_t lbase, int ioflag, int pblksize,
931 int *errorp, int comp_method)
933 hammer2_chain_t *chain;
935 if (test_block_not_zeros(bp->b_data, pblksize)) {
942 KKASSERT(pblksize / 2 <= 32768);
944 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
945 if ((comp_method & 0x0F) == HAMMER2_COMP_LZ4) {
946 comp_buffer = objcache_get(cache_buffer_write,
948 comp_size = LZ4_compress_limitedOutput(
950 &comp_buffer[sizeof(int)],
952 pblksize / 2 - sizeof(int));
954 * We need to prefix with the size, LZ4
955 * doesn't do it for us. Add the related
958 *(int *)comp_buffer = comp_size;
960 comp_size += sizeof(int);
961 } else if ((comp_method & 0x0F) == HAMMER2_COMP_ZLIB) {
962 int comp_level = (comp_method >> 4) & 0x0F;
963 z_stream strm_compress;
966 ret = deflateInit(&strm_compress, comp_level);
968 kprintf("HAMMER2 ZLIB: fatal error "
969 "on deflateInit.\n");
971 comp_buffer = objcache_get(cache_buffer_write,
973 strm_compress.next_in = bp->b_data;
974 strm_compress.avail_in = pblksize;
975 strm_compress.next_out = comp_buffer;
976 strm_compress.avail_out = pblksize / 2;
977 ret = deflate(&strm_compress, Z_FINISH);
978 if (ret == Z_STREAM_END) {
979 comp_size = pblksize / 2 -
980 strm_compress.avail_out;
984 ret = deflateEnd(&strm_compress);
986 kprintf("Error: Unknown compression method.\n");
987 kprintf("Comp_method = %d.\n", comp_method);
991 if (comp_size == 0) {
993 * compression failed or turned off
995 comp_block_size = pblksize; /* safety */
996 if (++ip->comp_heuristic > 128)
997 ip->comp_heuristic = 8;
1000 * compression succeeded
1002 ip->comp_heuristic = 0;
1003 if (comp_size <= 1024) {
1004 comp_block_size = 1024;
1005 } else if (comp_size <= 2048) {
1006 comp_block_size = 2048;
1007 } else if (comp_size <= 4096) {
1008 comp_block_size = 4096;
1009 } else if (comp_size <= 8192) {
1010 comp_block_size = 8192;
1011 } else if (comp_size <= 16384) {
1012 comp_block_size = 16384;
1013 } else if (comp_size <= 32768) {
1014 comp_block_size = 32768;
1016 panic("hammer2: WRITE PATH: "
1017 "Weird comp_size value.");
1019 comp_block_size = pblksize;
1023 chain = hammer2_assign_physical(trans, ip, parentp,
1024 lbase, comp_block_size,
1026 ipdata = &ip->chain->data->ipdata; /* RELOAD */
1029 kprintf("WRITE PATH: An error occurred while "
1030 "assigning physical space.\n");
1031 KKASSERT(chain == NULL);
1033 /* Get device offset */
1034 hammer2_off_t pbase;
1035 hammer2_off_t pmask;
1042 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1044 switch(chain->bref.type) {
1045 case HAMMER2_BREF_TYPE_INODE:
1046 KKASSERT(chain->data->ipdata.op_flags &
1047 HAMMER2_OPFLAG_DIRECTDATA);
1048 KKASSERT(bp->b_loffset == 0);
1049 bcopy(bp->b_data, chain->data->ipdata.u.data,
1050 HAMMER2_EMBEDDED_BYTES);
1052 case HAMMER2_BREF_TYPE_DATA:
1053 psize = hammer2_devblksize(chain->bytes);
1054 pmask = (hammer2_off_t)psize - 1;
1055 pbase = chain->bref.data_off & ~pmask;
1056 boff = chain->bref.data_off &
1057 (HAMMER2_OFF_MASK & pmask);
1058 peof = (pbase + HAMMER2_SEGMASK64) &
1060 temp_check = HAMMER2_DEC_CHECK(
1061 chain->bref.methods);
1064 * Optimize out the read-before-write
1067 if (comp_block_size == psize) {
1068 dbp = getblk(chain->hmp->devvp, pbase,
1071 *errorp = bread(chain->hmp->devvp,
1072 pbase, psize, &dbp);
1074 kprintf("hammer2: WRITE PATH: "
1075 "dbp bread error\n");
1081 * When loading the block make sure we don't
1082 * leave garbage after the compressed data.
1085 chain->bref.methods =
1086 HAMMER2_ENC_COMP(comp_method) +
1087 HAMMER2_ENC_CHECK(temp_check);
1088 bcopy(comp_buffer, dbp->b_data + boff,
1090 if (comp_size != comp_block_size) {
1091 bzero(dbp->b_data + boff +
1097 chain->bref.methods =
1099 HAMMER2_COMP_NONE) +
1100 HAMMER2_ENC_CHECK(temp_check);
1101 bcopy(bp->b_data, dbp->b_data + boff,
1106 * Device buffer is now valid, chain is no
1107 * longer in the initial state.
1109 atomic_clear_int(&chain->flags,
1110 HAMMER2_CHAIN_INITIAL);
1112 /* Now write the related bdp. */
1113 if (ioflag & IO_SYNC) {
1115 * Synchronous I/O requested.
1119 } else if ((ioflag & IO_DIRECT) &&
1120 loff + n == pblksize) {
1123 } else if (ioflag & IO_ASYNC) {
1125 } else if (hammer2_cluster_enable) {
1126 cluster_write(dbp, peof,
1134 panic("hammer2_write_bp: bad chain type %d\n",
1140 hammer2_chain_unlock(chain);
1143 objcache_put(cache_buffer_write, comp_buffer);
1145 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
1150 * Function that performs zero-checking and writing without compression,
1151 * it corresponds to default zero-checking path.
1155 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1156 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
1157 hammer2_chain_t **parentp,
1158 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp)
1160 hammer2_chain_t *chain;
1162 if (test_block_not_zeros(bp->b_data, pblksize)) {
1163 chain = hammer2_assign_physical(trans, ip, parentp,
1164 lbase, pblksize, errorp);
1165 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
1167 hammer2_chain_unlock(chain);
1169 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
1174 * A function to test whether a block of data contains only zeros,
1175 * returns 0 in that case or returns 1 otherwise.
1179 test_block_not_zeros(char *buf, size_t bytes)
1183 for (i = 0; i < bytes; i += sizeof(long)) {
1184 if (*(long *)(buf + i) != 0)
1191 * Function to "write" a block that contains only zeros.
1195 zero_write(struct buf *bp, hammer2_trans_t *trans, hammer2_inode_t *ip,
1196 hammer2_inode_data_t *ipdata, hammer2_chain_t **parentp,
1197 hammer2_key_t lbase, int *errorp __unused)
1199 hammer2_chain_t *parent;
1200 hammer2_chain_t *chain;
1202 parent = hammer2_chain_lookup_init(*parentp, 0);
1204 chain = hammer2_chain_lookup(&parent, lbase, lbase,
1205 HAMMER2_LOOKUP_NODATA);
1207 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1208 bzero(chain->data->ipdata.u.data,
1209 HAMMER2_EMBEDDED_BYTES);
1211 hammer2_chain_delete(trans, chain, 0);
1213 hammer2_chain_unlock(chain);
1215 hammer2_chain_lookup_done(parent);
1219 * Function to write the data as it is, without performing any sort of
1220 * compression. This function is used in path without compression and
1221 * default zero-checking path.
1225 hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp, int ioflag,
1226 int pblksize, int *errorp)
1228 hammer2_off_t pbase;
1229 hammer2_off_t pmask;
1235 int temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1237 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1239 switch(chain->bref.type) {
1240 case HAMMER2_BREF_TYPE_INODE:
1241 KKASSERT(chain->data->ipdata.op_flags &
1242 HAMMER2_OPFLAG_DIRECTDATA);
1243 KKASSERT(bp->b_loffset == 0);
1244 bcopy(bp->b_data, chain->data->ipdata.u.data,
1245 HAMMER2_EMBEDDED_BYTES);
1248 case HAMMER2_BREF_TYPE_DATA:
1249 psize = hammer2_devblksize(chain->bytes);
1250 pmask = (hammer2_off_t)psize - 1;
1251 pbase = chain->bref.data_off & ~pmask;
1252 boff = chain->bref.data_off & (HAMMER2_OFF_MASK & pmask);
1253 peof = (pbase + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64;
1255 if (psize == pblksize) {
1256 dbp = getblk(chain->hmp->devvp, pbase,
1260 error = bread(chain->hmp->devvp, pbase, psize, &dbp);
1262 kprintf("hammer2: WRITE PATH: "
1263 "dbp bread error\n");
1268 chain->bref.methods = HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
1269 HAMMER2_ENC_CHECK(temp_check);
1270 bcopy(bp->b_data, dbp->b_data + boff, chain->bytes);
1273 * Device buffer is now valid, chain is no
1274 * longer in the initial state.
1276 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1278 if (ioflag & IO_SYNC) {
1280 * Synchronous I/O requested.
1284 } else if ((ioflag & IO_DIRECT) && loff + n == pblksize) {
1287 } else if (ioflag & IO_ASYNC) {
1289 } else if (hammer2_cluster_enable) {
1290 cluster_write(dbp, peof, HAMMER2_PBUFSIZE, 4/*XXX*/);
1296 panic("hammer2_write_bp: bad chain type %d\n",
1307 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
1315 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1317 hammer2_pfsmount_t *pmp;
1318 hammer2_mount_t *hmp;
1319 hammer2_cluster_t *cluster;
1322 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1324 struct vnode *devvp;
1327 cluster = pmp->mount_cluster;
1331 if (mntflags & MNT_FORCE)
1332 flags |= FORCECLOSE;
1334 hammer2_mount_exlock(hmp);
1337 * If mount initialization proceeded far enough we must flush
1341 error = vflush(mp, 0, flags);
1344 hammer2_mount_unlock(hmp);
1348 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1350 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
1353 * Flush any left over chains. The voldata lock is only used
1354 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
1356 hammer2_voldata_lock(hmp);
1357 if ((hmp->vchain.flags | hmp->fchain.flags) &
1358 (HAMMER2_CHAIN_MODIFIED | HAMMER2_CHAIN_SUBMODIFIED)) {
1359 hammer2_voldata_unlock(hmp, 0);
1360 hammer2_vfs_sync(mp, MNT_WAIT);
1361 hammer2_vfs_sync(mp, MNT_WAIT);
1363 hammer2_voldata_unlock(hmp, 0);
1365 if (hmp->pmp_count == 0) {
1366 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
1367 HAMMER2_CHAIN_SUBMODIFIED)) {
1368 kprintf("hammer2_unmount: chains left over after "
1370 if (hammer2_debug & 0x0010)
1371 Debugger("entered debugger");
1376 * Cleanup the root and super-root chain elements (which should be
1380 #if REPORT_REFS_ERRORS
1381 if (pmp->iroot->refs != 1)
1382 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1383 pmp->iroot, pmp->iroot->refs);
1385 KKASSERT(pmp->iroot->refs == 1);
1387 hammer2_inode_drop(pmp->iroot); /* ref for pmp->iroot */
1390 if (cluster->rchain) {
1391 atomic_clear_int(&cluster->rchain->flags,
1392 HAMMER2_CHAIN_MOUNTED);
1393 #if REPORT_REFS_ERRORS
1394 if (cluster->rchain->refs != 1)
1395 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1396 cluster->rchain, cluster->rchain->refs);
1398 KKASSERT(cluster->rchain->refs == 1);
1400 hammer2_chain_drop(cluster->rchain);
1401 cluster->rchain = NULL;
1403 ccms_domain_uninit(&pmp->ccms_dom);
1406 * Kill cluster controller
1408 kdmsg_iocom_uninit(&pmp->iocom);
1411 * If no PFS's left drop the master hammer2_mount for the device.
1413 if (hmp->pmp_count == 0) {
1415 hammer2_inode_drop(hmp->sroot);
1419 #if REPORT_REFS_ERRORS
1420 if (hmp->schain->refs != 1)
1421 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
1422 hmp->schain, hmp->schain->refs);
1424 KKASSERT(hmp->schain->refs == 1);
1426 hammer2_chain_drop(hmp->schain);
1431 * Finish up with the device vnode
1433 if ((devvp = hmp->devvp) != NULL) {
1434 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1436 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
1442 * Final drop of embedded freemap root chain to clean up
1443 * fchain.core (fchain structure is not flagged ALLOCATED
1444 * so it is cleaned out and then left to rot).
1446 hammer2_chain_drop(&hmp->fchain);
1449 * Final drop of embedded volume root chain to clean up
1450 * vchain.core (vchain structure is not flagged ALLOCATED
1451 * so it is cleaned out and then left to rot).
1454 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
1455 hammer2_mount_unlock(hmp);
1456 hammer2_chain_drop(&hmp->vchain);
1458 hammer2_mount_unlock(hmp);
1462 mp->mnt_data = NULL;
1464 pmp->mount_cluster = NULL;
1465 pmp->cluster = NULL; /* XXX */
1467 kmalloc_destroy(&pmp->mmsg);
1468 kmalloc_destroy(&pmp->minode);
1470 cluster->hmp = NULL;
1472 kfree(cluster, M_HAMMER2);
1473 kfree(pmp, M_HAMMER2);
1474 if (hmp->pmp_count == 0) {
1475 mtx_lock(&hmp->wthread_mtx);
1476 hmp->wthread_destroy = 1;
1477 wakeup(&hmp->wthread_bioq);
1478 while (hmp->wthread_destroy != -1) {
1479 mtxsleep(&hmp->wthread_destroy, &hmp->wthread_mtx, 0,
1482 mtx_unlock(&hmp->wthread_mtx);
1484 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1485 kmalloc_destroy(&hmp->mchain);
1486 kfree(hmp, M_HAMMER2);
1488 lockmgr(&hammer2_mntlk, LK_RELEASE);
1495 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1496 ino_t ino, struct vnode **vpp)
1498 kprintf("hammer2_vget\n");
1499 return (EOPNOTSUPP);
1504 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1506 hammer2_pfsmount_t *pmp;
1507 hammer2_chain_t *parent;
1512 if (pmp->iroot == NULL) {
1516 parent = hammer2_inode_lock_sh(pmp->iroot);
1517 vp = hammer2_igetv(pmp->iroot, &error);
1518 hammer2_inode_unlock_sh(pmp->iroot, parent);
1521 kprintf("vnodefail\n");
1530 * XXX incorporate ipdata->inode_quota and data_quota
1534 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1536 hammer2_pfsmount_t *pmp;
1537 hammer2_mount_t *hmp;
1542 mp->mnt_stat.f_files = pmp->inode_count;
1543 mp->mnt_stat.f_ffree = 0;
1544 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1545 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1546 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1548 *sbp = mp->mnt_stat;
1554 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1556 hammer2_pfsmount_t *pmp;
1557 hammer2_mount_t *hmp;
1562 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1563 mp->mnt_vstat.f_files = pmp->inode_count;
1564 mp->mnt_vstat.f_ffree = 0;
1565 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1566 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1567 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1569 *sbp = mp->mnt_vstat;
1574 * Sync the entire filesystem; this is called from the filesystem syncer
1575 * process periodically and whenever a user calls sync(1) on the hammer
1578 * Currently is actually called from the syncer! \o/
1580 * This task will have to snapshot the state of the dirty inode chain.
1581 * From that, it will have to make sure all of the inodes on the dirty
1582 * chain have IO initiated. We make sure that io is initiated for the root
1585 * If waitfor is set, we wait for media to acknowledge the new rootblock.
1587 * THINKS: side A vs side B, to have sync not stall all I/O?
1591 hammer2_vfs_sync(struct mount *mp, int waitfor)
1593 struct hammer2_sync_info info;
1594 hammer2_pfsmount_t *pmp;
1595 hammer2_cluster_t *cluster;
1596 hammer2_mount_t *hmp;
1604 * We can't acquire locks on existing vnodes while in a transaction
1605 * without risking a deadlock. This assumes that vfsync() can be
1606 * called without the vnode locked (which it can in DragonFly).
1607 * Otherwise we'd have to implement a multi-pass or flag the lock
1608 * failures and retry.
1610 /*flags = VMSC_GETVP;*/
1612 if (waitfor & MNT_LAZY)
1613 flags |= VMSC_ONEPASS;
1615 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH);
1618 info.waitfor = MNT_NOWAIT;
1619 vmntvnodescan(mp, flags | VMSC_NOWAIT,
1621 hammer2_sync_scan2, &info);
1622 if (info.error == 0 && (waitfor & MNT_WAIT)) {
1623 info.waitfor = waitfor;
1624 vmntvnodescan(mp, flags,
1626 hammer2_sync_scan2, &info);
1630 if (waitfor == MNT_WAIT) {
1637 cluster = pmp->cluster;
1640 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1641 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
1642 HAMMER2_CHAIN_SUBMODIFIED)) {
1643 hammer2_chain_flush(&info.trans, &hmp->vchain);
1645 hammer2_chain_unlock(&hmp->vchain);
1649 * Rollup flush. The fsyncs above basically just flushed
1650 * data blocks. The flush below gets all the meta-data.
1652 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1653 if (hmp->fchain.flags & (HAMMER2_CHAIN_MODIFIED |
1654 HAMMER2_CHAIN_SUBMODIFIED)) {
1655 /* this will modify vchain as a side effect */
1656 hammer2_chain_flush(&info.trans, &hmp->fchain);
1658 hammer2_chain_unlock(&hmp->fchain);
1665 * We can't safely flush the volume header until we have
1666 * flushed any device buffers which have built up.
1668 * XXX this isn't being incremental
1670 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
1671 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
1672 vn_unlock(hmp->devvp);
1675 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
1676 * volume header needs synchronization via hmp->volsync.
1678 * XXX synchronize the flag & data with only this flush XXX
1680 if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
1684 * Synchronize the disk before flushing the volume
1688 bp->b_bio1.bio_offset = 0;
1691 bp->b_cmd = BUF_CMD_FLUSH;
1692 bp->b_bio1.bio_done = biodone_sync;
1693 bp->b_bio1.bio_flags |= BIO_SYNC;
1694 vn_strategy(hmp->devvp, &bp->b_bio1);
1695 biowait(&bp->b_bio1, "h2vol");
1699 * Then we can safely flush the version of the volume header
1700 * synchronized by the flush code.
1702 i = hmp->volhdrno + 1;
1703 if (i >= HAMMER2_NUM_VOLHDRS)
1705 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
1706 hmp->volsync.volu_size) {
1709 kprintf("sync volhdr %d %jd\n",
1710 i, (intmax_t)hmp->volsync.volu_size);
1711 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1712 HAMMER2_PBUFSIZE, 0, 0);
1713 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
1714 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
1718 hammer2_trans_done(&info.trans);
1725 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
1726 * won't flush on those flags. The syncer code above will do a
1727 * general meta-data flush globally that will catch these flags.
1730 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1732 hammer2_inode_t *ip;
1735 if (vp->v_type == VNON || ip == NULL ||
1736 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
1737 RB_EMPTY(&vp->v_rbdirty_tree))) {
1744 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1746 struct hammer2_sync_info *info = data;
1747 hammer2_inode_t *ip;
1748 hammer2_chain_t *parent;
1752 if (vp->v_type == VNON || vp->v_type == VBAD ||
1753 ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
1754 RB_EMPTY(&vp->v_rbdirty_tree))) {
1759 * VOP_FSYNC will start a new transaction so replicate some code
1760 * here to do it inline (see hammer2_vop_fsync()).
1762 * WARNING: The vfsync interacts with the buffer cache and might
1763 * block, we can't hold the inode lock at that time.
1765 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1767 vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
1768 parent = hammer2_inode_lock_ex(ip);
1769 hammer2_chain_flush(&info->trans, parent);
1770 hammer2_inode_unlock_ex(ip, parent);
1773 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
1776 info->error = error;
1782 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1789 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1790 struct fid *fhp, struct vnode **vpp)
1797 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1798 int *exflagsp, struct ucred **credanonp)
1804 * Support code for hammer2_mount(). Read, verify, and install the volume
1805 * header into the HMP
1807 * XXX read four volhdrs and use the one with the highest TID whos CRC
1812 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1813 * nonexistant locations.
1815 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
1819 hammer2_install_volume_header(hammer2_mount_t *hmp)
1821 hammer2_volume_data_t *vd;
1823 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
1835 * There are up to 4 copies of the volume header (syncs iterate
1836 * between them so there is no single master). We don't trust the
1837 * volu_size field so we don't know precisely how large the filesystem
1838 * is, so depend on the OS to return an error if we go beyond the
1839 * block device's EOF.
1841 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1842 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1843 HAMMER2_VOLUME_BYTES, &bp);
1850 vd = (struct hammer2_volume_data *) bp->b_data;
1851 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1852 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1858 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1859 /* XXX: Reversed-endianness filesystem */
1860 kprintf("hammer2: reverse-endian filesystem detected");
1866 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1867 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1868 HAMMER2_VOLUME_ICRC0_SIZE);
1869 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1870 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1871 HAMMER2_VOLUME_ICRC1_SIZE);
1872 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1873 kprintf("hammer2 volume header crc "
1874 "mismatch copy #%d %08x/%08x\n",
1881 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1890 hmp->volsync = hmp->voldata;
1892 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1893 kprintf("hammer2: using volume header #%d\n",
1898 kprintf("hammer2: no valid volume headers found!\n");
1904 * Reconnect using the passed file pointer. The caller must ref the
1908 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1910 hammer2_inode_data_t *ipdata;
1911 hammer2_chain_t *parent;
1912 hammer2_mount_t *hmp;
1915 hmp = pmp->mount_cluster->hmp;
1918 * Closes old comm descriptor, kills threads, cleans up
1919 * states, then installs the new descriptor and creates
1922 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1925 * Setup LNK_CONN fields for autoinitiated state machine
1927 parent = hammer2_inode_lock_ex(pmp->iroot);
1928 ipdata = &parent->data->ipdata;
1929 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1930 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1931 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1932 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1933 pmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
1936 * Filter adjustment. Clients do not need visibility into other
1937 * clients (otherwise millions of clients would present a serious
1938 * problem). The fs_label also serves to restrict the namespace.
1940 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1941 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1942 switch (ipdata->pfs_type) {
1943 case DMSG_PFSTYPE_CLIENT:
1944 pmp->iocom.auto_lnk_conn.peer_mask &=
1945 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1951 name_len = ipdata->name_len;
1952 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1953 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1954 bcopy(ipdata->filename,
1955 pmp->iocom.auto_lnk_conn.fs_label,
1957 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1960 * Setup LNK_SPAN fields for autoinitiated state machine
1962 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1963 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1964 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1965 pmp->iocom.auto_lnk_span.peer_type = hmp->voldata.peer_type;
1966 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1967 name_len = ipdata->name_len;
1968 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1969 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1970 bcopy(ipdata->filename,
1971 pmp->iocom.auto_lnk_span.fs_label,
1973 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1974 hammer2_inode_unlock_ex(pmp->iroot, parent);
1976 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1980 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1982 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1983 case DMSG_DBG_SHELL:
1986 * Execute shell command (not supported atm)
1988 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1990 case DMSG_DBG_SHELL | DMSGF_REPLY:
1994 if (msg->aux_data) {
1995 msg->aux_data[msg->aux_size - 1] = 0;
1996 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
2001 * Unsupported message received. We only need to
2002 * reply if it's a transaction in order to close our end.
2003 * Ignore any one-way messages are any further messages
2004 * associated with the transaction.
2006 * NOTE: This case also includes DMSG_LNK_ERROR messages
2007 * which might be one-way, replying to those would
2008 * cause an infinite ping-pong.
2010 if (msg->any.head.cmd & DMSGF_CREATE)
2011 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2018 * This function is called after KDMSG has automatically handled processing
2019 * of a LNK layer message (typically CONN, SPAN, or CIRC).
2021 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
2022 * advertises all available hammer2 super-root volumes.
2025 hammer2_autodmsg(kdmsg_msg_t *msg)
2027 hammer2_pfsmount_t *pmp = msg->iocom->handle;
2028 hammer2_mount_t *hmp = pmp->mount_cluster->hmp;
2032 * We only care about replies to our LNK_CONN auto-request. kdmsg
2033 * has already processed the reply, we use this calback as a shim
2034 * to know when we can advertise available super-root volumes.
2036 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
2037 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
2038 msg->state == NULL) {
2042 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
2044 if (msg->any.head.cmd & DMSGF_CREATE) {
2045 kprintf("HAMMER2: VOLDATA DUMP\n");
2048 * Dump the configuration stored in the volume header
2050 hammer2_voldata_lock(hmp);
2051 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
2052 if (hmp->voldata.copyinfo[copyid].copyid == 0)
2054 hammer2_volconf_update(pmp, copyid);
2056 hammer2_voldata_unlock(hmp, 0);
2058 if ((msg->any.head.cmd & DMSGF_DELETE) &&
2059 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
2060 kprintf("HAMMER2: CONN WAS TERMINATED\n");
2065 * Volume configuration updates are passed onto the userland service
2066 * daemon via the open LNK_CONN transaction.
2069 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
2071 hammer2_mount_t *hmp = pmp->mount_cluster->hmp;
2074 /* XXX interlock against connection state termination */
2075 kprintf("volconf update %p\n", pmp->iocom.conn_state);
2076 if (pmp->iocom.conn_state) {
2077 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
2078 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
2079 DMSG_LNK_VOLCONF, NULL, NULL);
2080 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
2081 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
2082 msg->any.lnk_volconf.index = index;
2083 kdmsg_msg_write(msg);
2088 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
2090 hammer2_chain_t *scan;
2094 kprintf("%*.*s...\n", tab, tab, "");
2099 kprintf("%*.*schain[%d] %p.%d [%08x][core=%p] (%s) dl=%p dt=%s refs=%d",
2101 chain->index, chain, chain->bref.type, chain->flags,
2103 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2104 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2106 (chain->delete_tid == HAMMER2_MAX_TID ? "max" : "fls"),
2108 if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
2112 RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree) {
2113 hammer2_dump_chain(scan, tab + 4, countp);
2115 if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
2116 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2117 kprintf("%*.*s}(%s)\n", tab, tab, "",
2118 chain->data->ipdata.filename);
2120 kprintf("%*.*s}\n", tab, tab, "");