2 * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61 #include "hammer2_lz4.h"
63 #include "zlib/hammer2_zlib.h"
65 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
67 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
69 struct hammer2_sync_info {
70 hammer2_trans_t trans;
75 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
76 TAILQ_HEAD(hammer2_pfslist, hammer2_pfsmount);
77 static struct hammer2_mntlist hammer2_mntlist;
78 static struct hammer2_pfslist hammer2_pfslist;
79 static struct lock hammer2_mntlk;
82 int hammer2_cluster_enable = 1;
83 int hammer2_hardlink_enable = 1;
84 int hammer2_flush_pipe = 100;
85 int hammer2_synchronous_flush = 1;
86 int hammer2_dio_count;
87 long hammer2_limit_dirty_chains;
88 long hammer2_iod_file_read;
89 long hammer2_iod_meta_read;
90 long hammer2_iod_indr_read;
91 long hammer2_iod_fmap_read;
92 long hammer2_iod_volu_read;
93 long hammer2_iod_file_write;
94 long hammer2_iod_meta_write;
95 long hammer2_iod_indr_write;
96 long hammer2_iod_fmap_write;
97 long hammer2_iod_volu_write;
98 long hammer2_ioa_file_read;
99 long hammer2_ioa_meta_read;
100 long hammer2_ioa_indr_read;
101 long hammer2_ioa_fmap_read;
102 long hammer2_ioa_volu_read;
103 long hammer2_ioa_fmap_write;
104 long hammer2_ioa_file_write;
105 long hammer2_ioa_meta_write;
106 long hammer2_ioa_indr_write;
107 long hammer2_ioa_volu_write;
109 MALLOC_DECLARE(C_BUFFER);
110 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
112 MALLOC_DECLARE(D_BUFFER);
113 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
115 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
117 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
118 &hammer2_debug, 0, "");
119 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
120 &hammer2_cluster_enable, 0, "");
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
122 &hammer2_hardlink_enable, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
124 &hammer2_flush_pipe, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
126 &hammer2_synchronous_flush, 0, "");
127 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
128 &hammer2_limit_dirty_chains, 0, "");
129 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
130 &hammer2_dio_count, 0, "");
132 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
133 &hammer2_iod_file_read, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
135 &hammer2_iod_meta_read, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
137 &hammer2_iod_indr_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
139 &hammer2_iod_fmap_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
141 &hammer2_iod_volu_read, 0, "");
143 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
144 &hammer2_iod_file_write, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
146 &hammer2_iod_meta_write, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
148 &hammer2_iod_indr_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
150 &hammer2_iod_fmap_write, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
152 &hammer2_iod_volu_write, 0, "");
154 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
155 &hammer2_ioa_file_read, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
157 &hammer2_ioa_meta_read, 0, "");
158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
159 &hammer2_ioa_indr_read, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
161 &hammer2_ioa_fmap_read, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
163 &hammer2_ioa_volu_read, 0, "");
165 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
166 &hammer2_ioa_file_write, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
168 &hammer2_ioa_meta_write, 0, "");
169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
170 &hammer2_ioa_indr_write, 0, "");
171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
172 &hammer2_ioa_fmap_write, 0, "");
173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
174 &hammer2_ioa_volu_write, 0, "");
176 static int hammer2_vfs_init(struct vfsconf *conf);
177 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
178 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
180 static int hammer2_remount(hammer2_mount_t *, struct mount *, char *,
181 struct vnode *, struct ucred *);
182 static int hammer2_recovery(hammer2_mount_t *hmp);
183 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
184 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
185 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
187 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
189 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
190 ino_t ino, struct vnode **vpp);
191 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
192 struct fid *fhp, struct vnode **vpp);
193 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
194 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
195 int *exflagsp, struct ucred **credanonp);
197 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
198 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
200 static void hammer2_write_thread(void *arg);
202 static void hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp);
203 static void hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp);
206 * Functions for compression in threads,
207 * from hammer2_vnops.c
209 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
211 const hammer2_inode_data_t *ripdata,
212 hammer2_cluster_t *cparent,
213 hammer2_key_t lbase, int ioflag, int pblksize,
215 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
217 const hammer2_inode_data_t *ripdata,
218 hammer2_cluster_t *cparent,
219 hammer2_key_t lbase, int ioflag,
220 int pblksize, int *errorp,
221 int comp_algo, int check_algo);
222 static void hammer2_zero_check_and_write(struct buf *bp,
223 hammer2_trans_t *trans, hammer2_inode_t *ip,
224 const hammer2_inode_data_t *ripdata,
225 hammer2_cluster_t *cparent,
227 int ioflag, int pblksize, int *errorp,
229 static int test_block_zeros(const char *buf, size_t bytes);
230 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
232 const hammer2_inode_data_t *ripdata,
233 hammer2_cluster_t *cparent,
236 static void hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp,
237 int ioflag, int pblksize, int *errorp,
241 * HAMMER2 vfs operations.
243 static struct vfsops hammer2_vfsops = {
244 .vfs_init = hammer2_vfs_init,
245 .vfs_uninit = hammer2_vfs_uninit,
246 .vfs_sync = hammer2_vfs_sync,
247 .vfs_mount = hammer2_vfs_mount,
248 .vfs_unmount = hammer2_vfs_unmount,
249 .vfs_root = hammer2_vfs_root,
250 .vfs_statfs = hammer2_vfs_statfs,
251 .vfs_statvfs = hammer2_vfs_statvfs,
252 .vfs_vget = hammer2_vfs_vget,
253 .vfs_vptofh = hammer2_vfs_vptofh,
254 .vfs_fhtovp = hammer2_vfs_fhtovp,
255 .vfs_checkexp = hammer2_vfs_checkexp
258 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
260 VFS_SET(hammer2_vfsops, hammer2, 0);
261 MODULE_VERSION(hammer2, 1);
265 hammer2_vfs_init(struct vfsconf *conf)
267 static struct objcache_malloc_args margs_read;
268 static struct objcache_malloc_args margs_write;
274 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
276 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
278 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
282 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
284 margs_read.objsize = 65536;
285 margs_read.mtype = D_BUFFER;
287 margs_write.objsize = 32768;
288 margs_write.mtype = C_BUFFER;
290 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
291 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
292 objcache_malloc_free, &margs_read);
293 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
294 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
295 objcache_malloc_free, &margs_write);
297 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
298 TAILQ_INIT(&hammer2_mntlist);
299 TAILQ_INIT(&hammer2_pfslist);
301 hammer2_limit_dirty_chains = desiredvnodes / 10;
303 hammer2_trans_manage_init();
310 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
312 objcache_destroy(cache_buffer_read);
313 objcache_destroy(cache_buffer_write);
318 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
319 * mounts and the spmp structure for media (hmp) structures.
321 static hammer2_pfsmount_t *
322 hammer2_pfsalloc(const hammer2_inode_data_t *ripdata, hammer2_tid_t alloc_tid)
324 hammer2_pfsmount_t *pmp;
326 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
327 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
328 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
329 lockinit(&pmp->lock, "pfslk", 0, 0);
330 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
331 RB_INIT(&pmp->inum_tree);
332 TAILQ_INIT(&pmp->unlinkq);
333 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
335 pmp->alloc_tid = alloc_tid + 1; /* our first media transaction id */
336 pmp->flush_tid = pmp->alloc_tid;
338 pmp->inode_tid = ripdata->pfs_inum + 1;
339 pmp->pfs_clid = ripdata->pfs_clid;
341 hammer2_mtx_init(&pmp->wthread_mtx, "h2wthr");
342 bioq_init(&pmp->wthread_bioq);
348 * Mount or remount HAMMER2 fileystem from physical media
351 * mp mount point structure
357 * mp mount point structure
358 * path path to mount point
359 * data pointer to argument structure in user space
360 * volume volume path (device@LABEL form)
361 * hflags user mount flags
362 * cred user credentials
369 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
372 struct hammer2_mount_info info;
373 hammer2_pfsmount_t *pmp;
374 hammer2_pfsmount_t *spmp;
375 hammer2_mount_t *hmp;
376 hammer2_key_t key_next;
377 hammer2_key_t key_dummy;
380 struct nlookupdata nd;
381 hammer2_chain_t *parent;
382 hammer2_chain_t *rchain;
383 hammer2_cluster_t *cluster;
384 hammer2_cluster_t *cparent;
385 const hammer2_inode_data_t *ripdata;
386 hammer2_blockref_t bref;
388 char devstr[MNAMELEN];
406 kprintf("hammer2_mount\n");
412 bzero(&info, sizeof(info));
413 info.cluster_fd = -1;
417 * Non-root mount or updating a mount
419 error = copyin(data, &info, sizeof(info));
423 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
427 /* Extract device and label */
429 label = strchr(devstr, '@');
431 ((label + 1) - dev) > done) {
439 if (mp->mnt_flag & MNT_UPDATE) {
441 * Update mount. Note that pmp->iroot->cluster is
442 * an inode-embedded cluster and thus cannot be
445 * XXX HAMMER2 needs to implement NFS export via
449 cluster = &pmp->iroot->cluster;
450 for (i = 0; i < cluster->nchains; ++i) {
451 hmp = cluster->array[i].chain->hmp;
453 error = hammer2_remount(hmp, mp, path,
458 /*hammer2_inode_install_hidden(pmp);*/
467 * Lookup name and verify it refers to a block device.
469 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
471 error = nlookup(&nd);
473 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
477 if (vn_isdisk(devvp, &error))
478 error = vfs_mountedon(devvp);
482 * Determine if the device has already been mounted. After this
483 * check hmp will be non-NULL if we are doing the second or more
484 * hammer2 mounts from the same device.
486 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
487 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
488 if (hmp->devvp == devvp)
493 * Open the device if this isn't a secondary mount and construct
494 * the H2 device mount (hmp).
497 hammer2_chain_t *schain;
500 if (error == 0 && vcount(devvp) > 0)
504 * Now open the device
507 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
508 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
509 error = vinvalbuf(devvp, V_SAVE, 0, 0);
511 error = VOP_OPEN(devvp,
512 ronly ? FREAD : FREAD | FWRITE,
517 if (error && devvp) {
522 lockmgr(&hammer2_mntlk, LK_RELEASE);
525 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
528 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
529 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
530 RB_INIT(&hmp->iotree);
531 spin_init(&hmp->io_spin, "hm2mount_io");
532 spin_init(&hmp->list_spin, "hm2mount_list");
533 TAILQ_INIT(&hmp->flushq);
535 lockinit(&hmp->vollk, "h2vol", 0, 0);
538 * vchain setup. vchain.data is embedded.
539 * vchain.refs is initialized and will never drop to 0.
541 * NOTE! voldata is not yet loaded.
543 hmp->vchain.hmp = hmp;
544 hmp->vchain.refs = 1;
545 hmp->vchain.data = (void *)&hmp->voldata;
546 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
547 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
548 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
550 hammer2_chain_core_alloc(NULL, &hmp->vchain);
551 /* hmp->vchain.u.xxx is left NULL */
554 * fchain setup. fchain.data is embedded.
555 * fchain.refs is initialized and will never drop to 0.
557 * The data is not used but needs to be initialized to
558 * pass assertion muster. We use this chain primarily
559 * as a placeholder for the freemap's top-level RBTREE
560 * so it does not interfere with the volume's topology
563 hmp->fchain.hmp = hmp;
564 hmp->fchain.refs = 1;
565 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
566 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
567 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
568 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
569 hmp->fchain.bref.methods =
570 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
571 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
573 hammer2_chain_core_alloc(NULL, &hmp->fchain);
574 /* hmp->fchain.u.xxx is left NULL */
577 * Install the volume header and initialize fields from
580 error = hammer2_install_volume_header(hmp);
583 hammer2_vfs_unmount_hmp1(mp, hmp);
584 hammer2_vfs_unmount_hmp2(mp, hmp);
585 lockmgr(&hammer2_mntlk, LK_RELEASE);
586 hammer2_vfs_unmount(mp, MNT_FORCE);
591 * Really important to get these right or flush will get
594 hmp->spmp = hammer2_pfsalloc(NULL, hmp->voldata.mirror_tid);
595 kprintf("alloc spmp %p tid %016jx\n",
596 hmp->spmp, hmp->voldata.mirror_tid);
601 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
602 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
603 hmp->vchain.pmp = spmp;
604 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
605 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
606 hmp->fchain.pmp = spmp;
609 * First locate the super-root inode, which is key 0
610 * relative to the volume header's blockset.
612 * Then locate the root inode by scanning the directory keyspace
613 * represented by the label.
615 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
616 schain = hammer2_chain_lookup(&parent, &key_dummy,
617 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
618 &cache_index, 0, &ddflag);
619 hammer2_chain_lookup_done(parent);
620 if (schain == NULL) {
621 kprintf("hammer2_mount: invalid super-root\n");
623 hammer2_vfs_unmount_hmp1(mp, hmp);
624 hammer2_vfs_unmount_hmp2(mp, hmp);
625 lockmgr(&hammer2_mntlk, LK_RELEASE);
626 hammer2_vfs_unmount(mp, MNT_FORCE);
631 * Sanity-check schain's pmp, finish initializing spmp.
633 ripdata = &hammer2_chain_rdata(schain)->ipdata;
634 KKASSERT(schain->pmp == spmp);
635 spmp->pfs_clid = ripdata->pfs_clid;
638 * NOTE: inode_get sucks up schain's lock.
640 cluster = hammer2_cluster_from_chain(schain);
641 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
642 spmp->spmp_hmp = hmp;
643 hammer2_inode_ref(spmp->iroot);
644 hammer2_inode_unlock_ex(spmp->iroot, cluster);
646 /* leave spmp->iroot with one ref */
648 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
649 error = hammer2_recovery(hmp);
650 /* XXX do something with error */
654 hammer2_iocom_init(hmp);
657 * Ref the cluster management messaging descriptor. The mount
658 * program deals with the other end of the communications pipe.
660 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
662 hammer2_cluster_reconnect(hmp, fp);
664 kprintf("hammer2_mount: bad cluster_fd!\n");
672 * Lookup mount point under the media-localized super-root.
674 * cluster->pmp will incorrectly point to spmp and must be fixed
677 cparent = hammer2_inode_lock_ex(spmp->iroot);
678 lhc = hammer2_dirhash(label, strlen(label));
679 cluster = hammer2_cluster_lookup(cparent, &key_next,
680 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
683 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
685 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) {
688 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
690 lhc + HAMMER2_DIRHASH_LOMASK, 0);
692 hammer2_inode_unlock_ex(spmp->iroot, cparent);
694 if (cluster == NULL) {
695 kprintf("hammer2_mount: PFS label not found\n");
696 hammer2_vfs_unmount_hmp1(mp, hmp);
697 hammer2_vfs_unmount_hmp2(mp, hmp);
698 lockmgr(&hammer2_mntlk, LK_RELEASE);
699 hammer2_vfs_unmount(mp, MNT_FORCE);
703 for (i = 0; i < cluster->nchains; ++i) {
704 rchain = cluster->array[i].chain;
705 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
706 kprintf("hammer2_mount: PFS label already mounted!\n");
707 hammer2_cluster_unlock(cluster);
708 hammer2_vfs_unmount_hmp1(mp, hmp);
709 hammer2_vfs_unmount_hmp2(mp, hmp);
710 lockmgr(&hammer2_mntlk, LK_RELEASE);
711 hammer2_vfs_unmount(mp, MNT_FORCE);
714 KKASSERT(rchain->pmp == NULL);
716 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
717 kprintf("hammer2_mount: PFS label is recycling\n");
718 hammer2_cluster_unlock(cluster);
719 hammer2_vfs_unmount_hmp1(mp, hmp);
720 hammer2_vfs_unmount_hmp2(mp, hmp);
721 lockmgr(&hammer2_mntlk, LK_RELEASE);
722 hammer2_vfs_unmount(mp, MNT_FORCE);
729 * Check to see if the cluster id is already mounted at the mount
730 * point. If it is, add us to the cluster.
732 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
733 hammer2_cluster_bref(cluster, &bref);
734 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
735 if (pmp->spmp_hmp == NULL &&
736 bcmp(&pmp->pfs_clid, &ripdata->pfs_clid,
737 sizeof(pmp->pfs_clid)) == 0) {
747 * Directly lock the inode->lock, do not run through
748 * hammer2_inode_lock*().
750 hammer2_inode_ref(pmp->iroot);
751 hammer2_mtx_ex(&pmp->iroot->lock);
753 if (pmp->iroot->cluster.nchains + cluster->nchains >
754 HAMMER2_MAXCLUSTER) {
755 kprintf("hammer2_mount: cluster full!\n");
757 hammer2_mtx_unlock(&pmp->iroot->lock);
758 hammer2_inode_drop(pmp->iroot);
760 hammer2_cluster_unlock(cluster);
761 hammer2_vfs_unmount_hmp1(mp, hmp);
762 hammer2_vfs_unmount_hmp2(mp, hmp);
763 lockmgr(&hammer2_mntlk, LK_RELEASE);
764 hammer2_vfs_unmount(mp, MNT_FORCE);
767 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n");
768 j = pmp->iroot->cluster.nchains;
769 for (i = 0; i < cluster->nchains; ++i) {
770 rchain = cluster->array[i].chain;
771 KKASSERT(rchain->pmp == NULL);
773 hammer2_chain_ref(rchain);
774 pmp->iroot->cluster.array[j].chain = rchain;
777 pmp->iroot->cluster.nchains = j;
778 hammer2_mtx_unlock(&pmp->iroot->lock);
779 hammer2_inode_drop(pmp->iroot);
780 hammer2_cluster_unlock(cluster);
781 lockmgr(&hammer2_mntlk, LK_RELEASE);
784 hammer2_inode_install_hidden(pmp);
790 * Block device opened successfully, finish initializing the
793 * From this point on we have to call hammer2_unmount() on failure.
795 pmp = hammer2_pfsalloc(ripdata, bref.mirror_tid);
796 kprintf("PMP mirror_tid is %016jx\n", bref.mirror_tid);
797 for (i = 0; i < cluster->nchains; ++i) {
798 rchain = cluster->array[i].chain;
799 KKASSERT(rchain->pmp == NULL);
801 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
805 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
806 lockmgr(&hammer2_mntlk, LK_RELEASE);
808 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
809 hmp, pmp, hmp->pmp_count);
811 mp->mnt_flag = MNT_LOCAL;
812 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
813 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
816 * required mount structure initializations
818 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
819 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
821 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
822 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
827 mp->mnt_iosize_max = MAXPHYS;
828 mp->mnt_data = (qaddr_t)pmp;
832 * After this point hammer2_vfs_unmount() has visibility on hmp
833 * and manual hmp1/hmp2 calls are not needed on fatal errors.
835 pmp->iroot = hammer2_inode_get(pmp, NULL, cluster);
836 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
837 hammer2_inode_unlock_ex(pmp->iroot, cluster);
840 * The logical file buffer bio write thread handles things
841 * like physical block assignment and compression.
843 * (only applicable to pfs mounts, not applicable to spmp)
845 pmp->wthread_destroy = 0;
846 lwkt_create(hammer2_write_thread, pmp,
847 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
850 * With the cluster operational install ihidden.
851 * (only applicable to pfs mounts, not applicable to spmp)
853 hammer2_inode_install_hidden(pmp);
859 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
860 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
861 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
863 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
864 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
865 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
866 copyinstr(path, mp->mnt_stat.f_mntonname,
867 sizeof(mp->mnt_stat.f_mntonname) - 1,
871 * Initial statfs to prime mnt_stat.
873 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
879 * Handle bioq for strategy write
883 hammer2_write_thread(void *arg)
885 hammer2_pfsmount_t *pmp;
888 hammer2_trans_t trans;
891 hammer2_cluster_t *cparent;
892 hammer2_inode_data_t *wipdata;
900 hammer2_mtx_ex(&pmp->wthread_mtx);
901 while (pmp->wthread_destroy == 0) {
902 if (bioq_first(&pmp->wthread_bioq) == NULL) {
903 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
908 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
910 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
912 * dummy bio for synchronization. The transaction
913 * must be reinitialized.
915 if (bio->bio_buf == NULL) {
916 bio->bio_flags |= BIO_DONE;
918 hammer2_trans_done(&trans);
919 hammer2_trans_init(&trans, pmp,
920 HAMMER2_TRANS_BUFCACHE);
925 * else normal bio processing
927 hammer2_mtx_unlock(&pmp->wthread_mtx);
929 hammer2_lwinprog_drop(pmp);
937 * Inode is modified, flush size and mtime changes
938 * to ensure that the file size remains consistent
939 * with the buffers being flushed.
941 * NOTE: The inode_fsync() call only flushes the
942 * inode's meta-data state, it doesn't try
943 * to flush underlying buffers or chains.
945 cparent = hammer2_inode_lock_ex(ip);
946 if (ip->flags & (HAMMER2_INODE_RESIZED |
947 HAMMER2_INODE_MTIME)) {
948 hammer2_inode_fsync(&trans, ip, cparent);
950 wipdata = hammer2_cluster_modify_ip(&trans, ip,
952 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
954 pblksize = hammer2_calc_physical(ip, wipdata, lbase);
955 hammer2_write_file_core(bp, &trans, ip, wipdata,
959 hammer2_cluster_modsync(cparent);
960 hammer2_inode_unlock_ex(ip, cparent);
962 kprintf("hammer2: error in buffer write\n");
963 bp->b_flags |= B_ERROR;
967 hammer2_mtx_ex(&pmp->wthread_mtx);
969 hammer2_trans_done(&trans);
971 pmp->wthread_destroy = -1;
972 wakeup(&pmp->wthread_destroy);
974 hammer2_mtx_unlock(&pmp->wthread_mtx);
978 hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
982 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
983 hammer2_mtx_ex(&pmp->wthread_mtx);
984 if (pmp->wthread_destroy == 0 &&
985 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
986 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
987 while ((sync_bio.bio_flags & BIO_DONE) == 0)
988 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
990 hammer2_mtx_unlock(&pmp->wthread_mtx);
994 * Return a chain suitable for I/O, creating the chain if necessary
995 * and assigning its physical block.
999 hammer2_assign_physical(hammer2_trans_t *trans,
1000 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1001 hammer2_key_t lbase, int pblksize, int *errorp)
1003 hammer2_cluster_t *cluster;
1004 hammer2_cluster_t *dparent;
1005 hammer2_key_t key_dummy;
1006 int pradix = hammer2_getradix(pblksize);
1010 * Locate the chain associated with lbase, return a locked chain.
1011 * However, do not instantiate any data reference (which utilizes a
1012 * device buffer) because we will be using direct IO via the
1013 * logical buffer cache buffer.
1016 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
1018 dparent = hammer2_cluster_lookup_init(cparent, 0);
1019 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
1021 HAMMER2_LOOKUP_NODATA, &ddflag);
1023 if (cluster == NULL) {
1025 * We found a hole, create a new chain entry.
1027 * NOTE: DATA chains are created without device backing
1028 * store (nor do we want any).
1030 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
1031 lbase, HAMMER2_PBUFRADIX,
1032 HAMMER2_BREF_TYPE_DATA,
1034 if (cluster == NULL) {
1035 hammer2_cluster_lookup_done(dparent);
1036 panic("hammer2_cluster_create: par=%p error=%d\n",
1037 dparent->focus, *errorp);
1040 /*ip->delta_dcount += pblksize;*/
1042 switch (hammer2_cluster_type(cluster)) {
1043 case HAMMER2_BREF_TYPE_INODE:
1045 * The data is embedded in the inode. The
1046 * caller is responsible for marking the inode
1047 * modified and copying the data to the embedded
1051 case HAMMER2_BREF_TYPE_DATA:
1052 if (hammer2_cluster_need_resize(cluster, pblksize)) {
1053 hammer2_cluster_resize(trans, ip,
1056 HAMMER2_MODIFY_OPTDATA);
1060 * DATA buffers must be marked modified whether the
1061 * data is in a logical buffer or not. We also have
1062 * to make this call to fixup the chain data pointers
1063 * after resizing in case this is an encrypted or
1064 * compressed buffer.
1066 hammer2_cluster_modify(trans, cluster,
1067 HAMMER2_MODIFY_OPTDATA);
1070 panic("hammer2_assign_physical: bad type");
1077 * Cleanup. If cluster wound up being the inode itself, i.e.
1078 * the DIRECTDATA case for offset 0, then we need to update cparent.
1079 * The caller expects cparent to not become stale.
1081 hammer2_cluster_lookup_done(dparent);
1082 /* dparent = NULL; safety */
1083 if (cluster && ddflag)
1084 hammer2_cluster_replace_locked(cparent, cluster);
1089 * bio queued from hammer2_vnops.c.
1091 * The core write function which determines which path to take
1092 * depending on compression settings. We also have to locate the
1093 * related clusters so we can calculate and set the check data for
1098 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
1099 hammer2_inode_t *ip,
1100 const hammer2_inode_data_t *ripdata,
1101 hammer2_cluster_t *cparent,
1102 hammer2_key_t lbase, int ioflag, int pblksize,
1105 hammer2_cluster_t *cluster;
1107 switch(HAMMER2_DEC_ALGO(ripdata->comp_algo)) {
1108 case HAMMER2_COMP_NONE:
1110 * We have to assign physical storage to the buffer
1111 * we intend to dirty or write now to avoid deadlocks
1112 * in the strategy code later.
1114 * This can return NOOFFSET for inode-embedded data.
1115 * The strategy code will take care of it in that case.
1117 cluster = hammer2_assign_physical(trans, ip, cparent,
1120 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1121 ripdata->check_algo);
1123 hammer2_cluster_unlock(cluster);
1125 case HAMMER2_COMP_AUTOZERO:
1127 * Check for zero-fill only
1129 hammer2_zero_check_and_write(bp, trans, ip,
1130 ripdata, cparent, lbase,
1131 ioflag, pblksize, errorp,
1132 ripdata->check_algo);
1134 case HAMMER2_COMP_LZ4:
1135 case HAMMER2_COMP_ZLIB:
1138 * Check for zero-fill and attempt compression.
1140 hammer2_compress_and_write(bp, trans, ip,
1145 ripdata->check_algo);
1151 * Generic function that will perform the compression in compression
1152 * write path. The compression algorithm is determined by the settings
1153 * obtained from inode.
1157 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
1158 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1159 hammer2_cluster_t *cparent,
1160 hammer2_key_t lbase, int ioflag, int pblksize,
1161 int *errorp, int comp_algo, int check_algo)
1163 hammer2_cluster_t *cluster;
1164 hammer2_chain_t *chain;
1166 int comp_block_size;
1170 if (test_block_zeros(bp->b_data, pblksize)) {
1171 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1178 KKASSERT(pblksize / 2 <= 32768);
1180 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1181 z_stream strm_compress;
1185 switch(HAMMER2_DEC_ALGO(comp_algo)) {
1186 case HAMMER2_COMP_LZ4:
1187 comp_buffer = objcache_get(cache_buffer_write,
1189 comp_size = LZ4_compress_limitedOutput(
1191 &comp_buffer[sizeof(int)],
1193 pblksize / 2 - sizeof(int));
1195 * We need to prefix with the size, LZ4
1196 * doesn't do it for us. Add the related
1199 *(int *)comp_buffer = comp_size;
1201 comp_size += sizeof(int);
1203 case HAMMER2_COMP_ZLIB:
1204 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1205 if (comp_level == 0)
1206 comp_level = 6; /* default zlib compression */
1207 else if (comp_level < 6)
1209 else if (comp_level > 9)
1211 ret = deflateInit(&strm_compress, comp_level);
1213 kprintf("HAMMER2 ZLIB: fatal error "
1214 "on deflateInit.\n");
1217 comp_buffer = objcache_get(cache_buffer_write,
1219 strm_compress.next_in = bp->b_data;
1220 strm_compress.avail_in = pblksize;
1221 strm_compress.next_out = comp_buffer;
1222 strm_compress.avail_out = pblksize / 2;
1223 ret = deflate(&strm_compress, Z_FINISH);
1224 if (ret == Z_STREAM_END) {
1225 comp_size = pblksize / 2 -
1226 strm_compress.avail_out;
1230 ret = deflateEnd(&strm_compress);
1233 kprintf("Error: Unknown compression method.\n");
1234 kprintf("Comp_method = %d.\n", comp_algo);
1239 if (comp_size == 0) {
1241 * compression failed or turned off
1243 comp_block_size = pblksize; /* safety */
1244 if (++ip->comp_heuristic > 128)
1245 ip->comp_heuristic = 8;
1248 * compression succeeded
1250 ip->comp_heuristic = 0;
1251 if (comp_size <= 1024) {
1252 comp_block_size = 1024;
1253 } else if (comp_size <= 2048) {
1254 comp_block_size = 2048;
1255 } else if (comp_size <= 4096) {
1256 comp_block_size = 4096;
1257 } else if (comp_size <= 8192) {
1258 comp_block_size = 8192;
1259 } else if (comp_size <= 16384) {
1260 comp_block_size = 16384;
1261 } else if (comp_size <= 32768) {
1262 comp_block_size = 32768;
1264 panic("hammer2: WRITE PATH: "
1265 "Weird comp_size value.");
1267 comp_block_size = pblksize;
1271 cluster = hammer2_assign_physical(trans, ip, cparent,
1272 lbase, comp_block_size,
1277 kprintf("WRITE PATH: An error occurred while "
1278 "assigning physical space.\n");
1279 KKASSERT(cluster == NULL);
1283 for (i = 0; i < cluster->nchains; ++i) {
1284 hammer2_inode_data_t *wipdata;
1288 chain = cluster->array[i].chain; /* XXX */
1289 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1291 switch(chain->bref.type) {
1292 case HAMMER2_BREF_TYPE_INODE:
1293 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1294 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1295 KKASSERT(bp->b_loffset == 0);
1296 bcopy(bp->b_data, wipdata->u.data,
1297 HAMMER2_EMBEDDED_BYTES);
1299 case HAMMER2_BREF_TYPE_DATA:
1301 * Optimize out the read-before-write
1304 *errorp = hammer2_io_newnz(chain->hmp,
1305 chain->bref.data_off,
1309 hammer2_io_brelse(&dio);
1310 kprintf("hammer2: WRITE PATH: "
1311 "dbp bread error\n");
1314 bdata = hammer2_io_data(dio, chain->bref.data_off);
1317 * When loading the block make sure we don't
1318 * leave garbage after the compressed data.
1321 chain->bref.methods =
1322 HAMMER2_ENC_COMP(comp_algo) +
1323 HAMMER2_ENC_CHECK(check_algo);
1324 bcopy(comp_buffer, bdata, comp_size);
1325 if (comp_size != comp_block_size) {
1326 bzero(bdata + comp_size,
1327 comp_block_size - comp_size);
1330 chain->bref.methods =
1332 HAMMER2_COMP_NONE) +
1333 HAMMER2_ENC_CHECK(check_algo);
1334 bcopy(bp->b_data, bdata, pblksize);
1338 * The flush code doesn't calculate check codes for
1339 * file data (doing so can result in excessive I/O),
1342 hammer2_chain_setcheck(chain, bdata);
1345 * Device buffer is now valid, chain is no longer in
1346 * the initial state.
1348 * (No blockref table worries with file data)
1350 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1352 /* Now write the related bdp. */
1353 if (ioflag & IO_SYNC) {
1355 * Synchronous I/O requested.
1357 hammer2_io_bwrite(&dio);
1359 } else if ((ioflag & IO_DIRECT) &&
1360 loff + n == pblksize) {
1361 hammer2_io_bdwrite(&dio);
1363 } else if (ioflag & IO_ASYNC) {
1364 hammer2_io_bawrite(&dio);
1366 hammer2_io_bdwrite(&dio);
1370 panic("hammer2_write_bp: bad chain type %d\n",
1378 hammer2_cluster_unlock(cluster);
1380 objcache_put(cache_buffer_write, comp_buffer);
1384 * Function that performs zero-checking and writing without compression,
1385 * it corresponds to default zero-checking path.
1389 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1390 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1391 hammer2_cluster_t *cparent,
1392 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp,
1395 hammer2_cluster_t *cluster;
1397 if (test_block_zeros(bp->b_data, pblksize)) {
1398 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1400 cluster = hammer2_assign_physical(trans, ip, cparent,
1401 lbase, pblksize, errorp);
1402 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1405 hammer2_cluster_unlock(cluster);
1410 * A function to test whether a block of data contains only zeros,
1411 * returns TRUE (non-zero) if the block is all zeros.
1415 test_block_zeros(const char *buf, size_t bytes)
1419 for (i = 0; i < bytes; i += sizeof(long)) {
1420 if (*(const long *)(buf + i) != 0)
1427 * Function to "write" a block that contains only zeros.
1431 zero_write(struct buf *bp, hammer2_trans_t *trans,
1432 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1433 hammer2_cluster_t *cparent,
1434 hammer2_key_t lbase, int *errorp __unused)
1436 hammer2_cluster_t *cluster;
1437 hammer2_media_data_t *data;
1438 hammer2_key_t key_dummy;
1441 cparent = hammer2_cluster_lookup_init(cparent, 0);
1442 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1443 HAMMER2_LOOKUP_NODATA, &ddflag);
1445 data = hammer2_cluster_wdata(cluster);
1448 KKASSERT(cluster->focus->flags &
1449 HAMMER2_CHAIN_MODIFIED);
1450 bzero(data->ipdata.u.data, HAMMER2_EMBEDDED_BYTES);
1451 hammer2_cluster_modsync(cluster);
1453 hammer2_cluster_delete(trans, cparent, cluster,
1454 HAMMER2_DELETE_PERMANENT);
1456 hammer2_cluster_unlock(cluster);
1458 hammer2_cluster_lookup_done(cparent);
1462 * Function to write the data as it is, without performing any sort of
1463 * compression. This function is used in path without compression and
1464 * default zero-checking path.
1468 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1469 int pblksize, int *errorp, int check_algo)
1471 hammer2_chain_t *chain;
1472 hammer2_inode_data_t *wipdata;
1478 error = 0; /* XXX TODO below */
1480 for (i = 0; i < cluster->nchains; ++i) {
1481 chain = cluster->array[i].chain; /* XXX */
1482 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1484 switch(chain->bref.type) {
1485 case HAMMER2_BREF_TYPE_INODE:
1486 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1487 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1488 KKASSERT(bp->b_loffset == 0);
1489 bcopy(bp->b_data, wipdata->u.data,
1490 HAMMER2_EMBEDDED_BYTES);
1493 case HAMMER2_BREF_TYPE_DATA:
1494 error = hammer2_io_newnz(chain->hmp,
1495 chain->bref.data_off,
1496 chain->bytes, &dio);
1498 hammer2_io_bqrelse(&dio);
1499 kprintf("hammer2: WRITE PATH: "
1500 "dbp bread error\n");
1503 bdata = hammer2_io_data(dio, chain->bref.data_off);
1505 chain->bref.methods = HAMMER2_ENC_COMP(
1506 HAMMER2_COMP_NONE) +
1507 HAMMER2_ENC_CHECK(check_algo);
1508 bcopy(bp->b_data, bdata, chain->bytes);
1511 * The flush code doesn't calculate check codes for
1512 * file data (doing so can result in excessive I/O),
1515 hammer2_chain_setcheck(chain, bdata);
1518 * Device buffer is now valid, chain is no longer in
1519 * the initial state.
1521 * (No blockref table worries with file data)
1523 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1525 if (ioflag & IO_SYNC) {
1527 * Synchronous I/O requested.
1529 hammer2_io_bwrite(&dio);
1531 } else if ((ioflag & IO_DIRECT) &&
1532 loff + n == pblksize) {
1533 hammer2_io_bdwrite(&dio);
1535 } else if (ioflag & IO_ASYNC) {
1536 hammer2_io_bawrite(&dio);
1538 hammer2_io_bdwrite(&dio);
1542 panic("hammer2_write_bp: bad chain type %d\n",
1548 KKASSERT(error == 0); /* XXX TODO */
1555 hammer2_remount(hammer2_mount_t *hmp, struct mount *mp, char *path,
1556 struct vnode *devvp, struct ucred *cred)
1560 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1561 error = hammer2_recovery(hmp);
1570 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1572 hammer2_pfsmount_t *pmp;
1573 hammer2_mount_t *hmp;
1574 hammer2_chain_t *rchain;
1575 hammer2_cluster_t *cluster;
1585 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1586 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
1589 * If mount initialization proceeded far enough we must flush
1592 if (mntflags & MNT_FORCE)
1597 error = vflush(mp, 0, flags);
1602 if (pmp->wthread_td) {
1603 hammer2_mtx_ex(&pmp->wthread_mtx);
1604 pmp->wthread_destroy = 1;
1605 wakeup(&pmp->wthread_bioq);
1606 while (pmp->wthread_destroy != -1) {
1607 mtxsleep(&pmp->wthread_destroy,
1608 &pmp->wthread_mtx, 0,
1611 hammer2_mtx_unlock(&pmp->wthread_mtx);
1612 pmp->wthread_td = NULL;
1616 * Cleanup our reference on ihidden.
1619 hammer2_inode_drop(pmp->ihidden);
1620 pmp->ihidden = NULL;
1624 * Cleanup our reference on iroot. iroot is (should) not be needed
1625 * by the flush code.
1628 cluster = &pmp->iroot->cluster;
1629 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1630 rchain = pmp->iroot->cluster.array[i].chain;
1634 hammer2_vfs_unmount_hmp1(mp, hmp);
1636 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
1637 #if REPORT_REFS_ERRORS
1638 if (rchain->refs != 1)
1639 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1640 rchain, rchain->refs);
1642 KKASSERT(rchain->refs == 1);
1644 hammer2_chain_drop(rchain);
1645 cluster->array[i].chain = NULL;
1646 hammer2_vfs_unmount_hmp2(mp, hmp);
1648 cluster->focus = NULL;
1650 #if REPORT_REFS_ERRORS
1651 if (pmp->iroot->refs != 1)
1652 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1653 pmp->iroot, pmp->iroot->refs);
1655 KKASSERT(pmp->iroot->refs == 1);
1657 /* ref for pmp->iroot */
1658 hammer2_inode_drop(pmp->iroot);
1663 mp->mnt_data = NULL;
1665 kmalloc_destroy(&pmp->mmsg);
1666 kmalloc_destroy(&pmp->minode);
1668 kfree(pmp, M_HAMMER2);
1672 lockmgr(&hammer2_mntlk, LK_RELEASE);
1679 hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp)
1681 hammer2_mount_exlock(hmp);
1684 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
1687 * Cycle the volume data lock as a safety (probably not needed any
1688 * more). To ensure everything is out we need to flush at least
1689 * three times. (1) The running of the unlinkq can dirty the
1690 * filesystem, (2) A normal flush can dirty the freemap, and
1691 * (3) ensure that the freemap is fully synchronized.
1693 * The next mount's recovery scan can clean everything up but we want
1694 * to leave the filesystem in a 100% clean state on a normal unmount.
1696 hammer2_voldata_lock(hmp);
1697 hammer2_voldata_unlock(hmp);
1699 hammer2_vfs_sync(mp, MNT_WAIT);
1700 hammer2_vfs_sync(mp, MNT_WAIT);
1701 hammer2_vfs_sync(mp, MNT_WAIT);
1705 * XXX chain depend deadlock?
1707 hammer2_iocom_uninit(hmp);
1709 if (hmp->pmp_count == 0) {
1710 if ((hmp->vchain.flags | hmp->fchain.flags) &
1711 HAMMER2_CHAIN_FLUSH_MASK) {
1712 kprintf("hammer2_unmount: chains left over "
1713 "after final sync\n");
1714 kprintf(" vchain %08x\n", hmp->vchain.flags);
1715 kprintf(" fchain %08x\n", hmp->fchain.flags);
1717 if (hammer2_debug & 0x0010)
1718 Debugger("entered debugger");
1725 hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp)
1727 hammer2_pfsmount_t *spmp;
1728 struct vnode *devvp;
1730 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1733 * If no PFS's left drop the master hammer2_mount for the
1736 if (hmp->pmp_count == 0) {
1738 * Clean up SPMP and the super-root inode
1743 hammer2_inode_drop(spmp->iroot);
1747 kmalloc_destroy(&spmp->mmsg);
1748 kmalloc_destroy(&spmp->minode);
1749 kfree(spmp, M_HAMMER2);
1753 * Finish up with the device vnode
1755 if ((devvp = hmp->devvp) != NULL) {
1756 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1757 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1759 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1766 * Clear vchain/fchain flags that might prevent final cleanup
1769 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1770 atomic_clear_int(&hmp->vchain.flags,
1771 HAMMER2_CHAIN_MODIFIED);
1772 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1773 hammer2_chain_drop(&hmp->vchain);
1775 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1776 atomic_clear_int(&hmp->vchain.flags,
1777 HAMMER2_CHAIN_UPDATE);
1778 hammer2_chain_drop(&hmp->vchain);
1781 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1782 atomic_clear_int(&hmp->fchain.flags,
1783 HAMMER2_CHAIN_MODIFIED);
1784 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1785 hammer2_chain_drop(&hmp->fchain);
1787 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1788 atomic_clear_int(&hmp->fchain.flags,
1789 HAMMER2_CHAIN_UPDATE);
1790 hammer2_chain_drop(&hmp->fchain);
1794 * Final drop of embedded freemap root chain to
1795 * clean up fchain.core (fchain structure is not
1796 * flagged ALLOCATED so it is cleaned out and then
1799 hammer2_chain_drop(&hmp->fchain);
1802 * Final drop of embedded volume root chain to clean
1803 * up vchain.core (vchain structure is not flagged
1804 * ALLOCATED so it is cleaned out and then left to
1808 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1810 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1811 hammer2_mount_unlock(hmp);
1812 hammer2_chain_drop(&hmp->vchain);
1814 hammer2_io_cleanup(hmp, &hmp->iotree);
1815 if (hmp->iofree_count) {
1816 kprintf("io_cleanup: %d I/O's left hanging\n",
1820 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1821 kmalloc_destroy(&hmp->mchain);
1822 kfree(hmp, M_HAMMER2);
1824 hammer2_mount_unlock(hmp);
1830 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1831 ino_t ino, struct vnode **vpp)
1833 kprintf("hammer2_vget\n");
1834 return (EOPNOTSUPP);
1839 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1841 hammer2_pfsmount_t *pmp;
1842 hammer2_cluster_t *cparent;
1847 if (pmp->iroot == NULL) {
1851 cparent = hammer2_inode_lock_sh(pmp->iroot);
1852 vp = hammer2_igetv(pmp->iroot, cparent, &error);
1853 hammer2_inode_unlock_sh(pmp->iroot, cparent);
1856 kprintf("vnodefail\n");
1865 * XXX incorporate ipdata->inode_quota and data_quota
1869 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1871 hammer2_pfsmount_t *pmp;
1872 hammer2_mount_t *hmp;
1875 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1876 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1878 mp->mnt_stat.f_files = pmp->inode_count;
1879 mp->mnt_stat.f_ffree = 0;
1880 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1881 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1882 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1884 *sbp = mp->mnt_stat;
1890 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1892 hammer2_pfsmount_t *pmp;
1893 hammer2_mount_t *hmp;
1896 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1897 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1899 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1900 mp->mnt_vstat.f_files = pmp->inode_count;
1901 mp->mnt_vstat.f_ffree = 0;
1902 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1903 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1904 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1906 *sbp = mp->mnt_vstat;
1911 * Mount-time recovery (RW mounts)
1913 * Updates to the free block table are allowed to lag flushes by one
1914 * transaction. In case of a crash, then on a fresh mount we must do an
1915 * incremental scan of the last committed transaction id and make sure that
1916 * all related blocks have been marked allocated.
1918 * The super-root topology and each PFS has its own transaction id domain,
1919 * so we must track PFS boundary transitions.
1921 struct hammer2_recovery_elm {
1922 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1923 hammer2_chain_t *chain;
1924 hammer2_tid_t sync_tid;
1927 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1929 struct hammer2_recovery_info {
1930 struct hammer2_recovery_list list;
1934 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1935 hammer2_chain_t *parent,
1936 struct hammer2_recovery_info *info,
1937 hammer2_tid_t sync_tid);
1939 #define HAMMER2_RECOVERY_MAXDEPTH 10
1943 hammer2_recovery(hammer2_mount_t *hmp)
1945 hammer2_trans_t trans;
1946 struct hammer2_recovery_info info;
1947 struct hammer2_recovery_elm *elm;
1948 hammer2_chain_t *parent;
1949 hammer2_tid_t sync_tid;
1951 int cumulative_error = 0;
1953 hammer2_trans_init(&trans, hmp->spmp, 0);
1956 TAILQ_INIT(&info.list);
1958 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1959 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent,
1961 hammer2_chain_lookup_done(parent);
1963 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1964 TAILQ_REMOVE(&info.list, elm, entry);
1965 parent = elm->chain;
1966 sync_tid = elm->sync_tid;
1967 kfree(elm, M_HAMMER2);
1969 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1970 HAMMER2_RESOLVE_NOREF);
1971 error = hammer2_recovery_scan(&trans, hmp, parent,
1973 hammer2_chain_unlock(parent);
1975 cumulative_error = error;
1977 hammer2_trans_done(&trans);
1979 return cumulative_error;
1984 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1985 hammer2_chain_t *parent,
1986 struct hammer2_recovery_info *info,
1987 hammer2_tid_t sync_tid)
1989 const hammer2_inode_data_t *ripdata;
1990 hammer2_chain_t *chain;
1992 int cumulative_error = 0;
1993 int pfs_boundary = 0;
1997 * Adjust freemap to ensure that the block(s) are marked allocated.
1999 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
2000 hammer2_freemap_adjust(trans, hmp, &parent->bref,
2001 HAMMER2_FREEMAP_DORECOVER);
2005 * Check type for recursive scan
2007 switch(parent->bref.type) {
2008 case HAMMER2_BREF_TYPE_VOLUME:
2009 /* data already instantiated */
2011 case HAMMER2_BREF_TYPE_INODE:
2013 * Must instantiate data for DIRECTDATA test and also
2016 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2017 ripdata = &hammer2_chain_rdata(parent)->ipdata;
2018 if (ripdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2019 /* not applicable to recovery scan */
2020 hammer2_chain_unlock(parent);
2023 if ((ripdata->op_flags & HAMMER2_OPFLAG_PFSROOT) &&
2026 sync_tid = parent->bref.mirror_tid - 1;
2028 hammer2_chain_unlock(parent);
2030 case HAMMER2_BREF_TYPE_INDIRECT:
2032 * Must instantiate data for recursion
2034 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2035 hammer2_chain_unlock(parent);
2037 case HAMMER2_BREF_TYPE_DATA:
2038 case HAMMER2_BREF_TYPE_FREEMAP:
2039 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2040 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2041 /* not applicable to recovery scan */
2049 * Defer operation if depth limit reached or if we are crossing a
2052 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH || pfs_boundary) {
2053 struct hammer2_recovery_elm *elm;
2055 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2056 elm->chain = parent;
2057 elm->sync_tid = sync_tid;
2058 hammer2_chain_ref(parent);
2059 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2060 /* unlocked by caller */
2067 * Recursive scan of the last flushed transaction only. We are
2068 * doing this without pmp assignments so don't leave the chains
2069 * hanging around after we are done with them.
2072 chain = hammer2_chain_scan(parent, NULL, &cache_index,
2073 HAMMER2_LOOKUP_NODATA);
2075 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2076 if (chain->bref.mirror_tid >= sync_tid) {
2078 error = hammer2_recovery_scan(trans, hmp, chain,
2082 cumulative_error = error;
2084 chain = hammer2_chain_scan(parent, chain, &cache_index,
2085 HAMMER2_LOOKUP_NODATA);
2088 return cumulative_error;
2092 * Sync the entire filesystem; this is called from the filesystem syncer
2093 * process periodically and whenever a user calls sync(1) on the hammer
2096 * Currently is actually called from the syncer! \o/
2098 * This task will have to snapshot the state of the dirty inode chain.
2099 * From that, it will have to make sure all of the inodes on the dirty
2100 * chain have IO initiated. We make sure that io is initiated for the root
2103 * If waitfor is set, we wait for media to acknowledge the new rootblock.
2105 * THINKS: side A vs side B, to have sync not stall all I/O?
2108 hammer2_vfs_sync(struct mount *mp, int waitfor)
2110 struct hammer2_sync_info info;
2111 hammer2_inode_t *iroot;
2112 hammer2_chain_t *chain;
2113 hammer2_chain_t *parent;
2114 hammer2_pfsmount_t *pmp;
2115 hammer2_mount_t *hmp;
2126 KKASSERT(iroot->pmp == pmp);
2129 * We can't acquire locks on existing vnodes while in a transaction
2130 * without risking a deadlock. This assumes that vfsync() can be
2131 * called without the vnode locked (which it can in DragonFly).
2132 * Otherwise we'd have to implement a multi-pass or flag the lock
2133 * failures and retry.
2135 * The reclamation code interlocks with the sync list's token
2136 * (by removing the vnode from the scan list) before unlocking
2137 * the inode, giving us time to ref the inode.
2139 /*flags = VMSC_GETVP;*/
2141 if (waitfor & MNT_LAZY)
2142 flags |= VMSC_ONEPASS;
2145 * Start our flush transaction. This does not return until all
2146 * concurrent transactions have completed and will prevent any
2147 * new transactions from running concurrently, except for the
2148 * buffer cache transactions.
2150 * For efficiency do an async pass before making sure with a
2151 * synchronous pass on all related buffer cache buffers. It
2152 * should theoretically not be possible for any new file buffers
2153 * to be instantiated during this sequence.
2155 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
2156 HAMMER2_TRANS_PREFLUSH);
2157 hammer2_run_unlinkq(&info.trans, pmp);
2160 info.waitfor = MNT_NOWAIT;
2161 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2162 info.waitfor = MNT_WAIT;
2163 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2166 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2167 * buffer cache flushes which occur during the flush. Device buffers
2172 if (info.error == 0 && (waitfor & MNT_WAIT)) {
2173 info.waitfor = waitfor;
2174 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2178 hammer2_bioq_sync(info.trans.pmp);
2179 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
2184 * Flush all storage elements making up the cluster
2186 * We must also flush any deleted siblings because the super-root
2187 * flush won't do it for us. They all must be staged or the
2188 * super-root flush will not be able to update its block table
2191 * XXX currently done serially instead of concurrently
2193 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2194 chain = iroot->cluster.array[i].chain;
2196 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
2197 hammer2_flush(&info.trans, chain);
2198 hammer2_chain_unlock(chain);
2202 hammer2_trans_done(&info.trans);
2206 * Flush all volume roots to synchronize PFS flushes with the
2207 * storage media. Use a super-root transaction for each one.
2209 * The flush code will detect super-root -> pfs-root chain
2210 * transitions using the last pfs-root flush.
2212 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2213 hammer2_chain_t *tmp;
2215 chain = iroot->cluster.array[i].chain;
2222 * We only have to flush each hmp once
2224 for (j = i - 1; j >= 0; --j) {
2225 if ((tmp = iroot->cluster.array[j].chain) != NULL) {
2226 if (tmp->hmp == hmp)
2232 hammer2_trans_spmp(&info.trans, hmp->spmp);
2235 * Force an update of the XID from the PFS root to the
2236 * topology root. We couldn't do this from the PFS
2237 * transaction because a SPMP transaction is needed.
2238 * This does not modify blocks, instead what it does is
2239 * allow the flush code to find the transition point and
2240 * then update on the way back up.
2242 parent = chain->parent;
2243 KKASSERT(chain->pmp != parent->pmp);
2244 hammer2_chain_setflush(&info.trans, parent);
2247 * Media mounts have two 'roots', vchain for the topology
2248 * and fchain for the free block table. Flush both.
2250 * Note that the topology and free block table are handled
2251 * independently, so the free block table can wind up being
2252 * ahead of the topology. We depend on the bulk free scan
2253 * code to deal with any loose ends.
2255 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2256 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2257 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2259 * This will also modify vchain as a side effect,
2260 * mark vchain as modified now.
2262 hammer2_voldata_modify(hmp);
2263 chain = &hmp->fchain;
2264 hammer2_flush(&info.trans, chain);
2265 KKASSERT(chain == &hmp->fchain);
2267 hammer2_chain_unlock(&hmp->fchain);
2268 hammer2_chain_unlock(&hmp->vchain);
2270 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2271 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2272 chain = &hmp->vchain;
2273 hammer2_flush(&info.trans, chain);
2274 KKASSERT(chain == &hmp->vchain);
2279 hammer2_chain_unlock(&hmp->vchain);
2282 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2283 if ((hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) ||
2285 /* this will also modify vchain as a side effect */
2286 chain = &hmp->fchain;
2287 hammer2_flush(&info.trans, chain);
2288 KKASSERT(chain == &hmp->fchain);
2290 hammer2_chain_unlock(&hmp->fchain);
2296 * We can't safely flush the volume header until we have
2297 * flushed any device buffers which have built up.
2299 * XXX this isn't being incremental
2301 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2302 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2303 vn_unlock(hmp->devvp);
2306 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2307 * volume header needs synchronization via hmp->volsync.
2309 * XXX synchronize the flag & data with only this flush XXX
2312 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2316 * Synchronize the disk before flushing the volume
2320 bp->b_bio1.bio_offset = 0;
2323 bp->b_cmd = BUF_CMD_FLUSH;
2324 bp->b_bio1.bio_done = biodone_sync;
2325 bp->b_bio1.bio_flags |= BIO_SYNC;
2326 vn_strategy(hmp->devvp, &bp->b_bio1);
2327 biowait(&bp->b_bio1, "h2vol");
2331 * Then we can safely flush the version of the
2332 * volume header synchronized by the flush code.
2334 i = hmp->volhdrno + 1;
2335 if (i >= HAMMER2_NUM_VOLHDRS)
2337 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2338 hmp->volsync.volu_size) {
2341 kprintf("sync volhdr %d %jd\n",
2342 i, (intmax_t)hmp->volsync.volu_size);
2343 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2344 HAMMER2_PBUFSIZE, 0, 0);
2345 atomic_clear_int(&hmp->vchain.flags,
2346 HAMMER2_CHAIN_VOLUMESYNC);
2347 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2352 total_error = error;
2355 hammer2_trans_done(&info.trans);
2358 hammer2_trans_done(&info.trans);
2360 return (total_error);
2367 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2369 struct hammer2_sync_info *info = data;
2370 hammer2_inode_t *ip;
2379 if (vp->v_type == VNON || vp->v_type == VBAD) {
2383 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2384 RB_EMPTY(&vp->v_rbdirty_tree)) {
2390 * VOP_FSYNC will start a new transaction so replicate some code
2391 * here to do it inline (see hammer2_vop_fsync()).
2393 * WARNING: The vfsync interacts with the buffer cache and might
2394 * block, we can't hold the inode lock at that time.
2395 * However, we MUST ref ip before blocking to ensure that
2396 * it isn't ripped out from under us (since we do not
2397 * hold a lock on the vnode).
2399 hammer2_inode_ref(ip);
2400 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
2402 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
2404 hammer2_inode_drop(ip);
2408 info->error = error;
2415 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2422 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2423 struct fid *fhp, struct vnode **vpp)
2430 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2431 int *exflagsp, struct ucred **credanonp)
2437 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2438 * header into the HMP
2440 * XXX read four volhdrs and use the one with the highest TID whos CRC
2445 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2446 * nonexistant locations.
2448 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2452 hammer2_install_volume_header(hammer2_mount_t *hmp)
2454 hammer2_volume_data_t *vd;
2456 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2468 * There are up to 4 copies of the volume header (syncs iterate
2469 * between them so there is no single master). We don't trust the
2470 * volu_size field so we don't know precisely how large the filesystem
2471 * is, so depend on the OS to return an error if we go beyond the
2472 * block device's EOF.
2474 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2475 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2476 HAMMER2_VOLUME_BYTES, &bp);
2483 vd = (struct hammer2_volume_data *) bp->b_data;
2484 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2485 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2491 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2492 /* XXX: Reversed-endianness filesystem */
2493 kprintf("hammer2: reverse-endian filesystem detected");
2499 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2500 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2501 HAMMER2_VOLUME_ICRC0_SIZE);
2502 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2503 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2504 HAMMER2_VOLUME_ICRC1_SIZE);
2505 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2506 kprintf("hammer2 volume header crc "
2507 "mismatch copy #%d %08x/%08x\n",
2514 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2523 hmp->volsync = hmp->voldata;
2525 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2526 kprintf("hammer2: using volume header #%d\n",
2531 kprintf("hammer2: no valid volume headers found!\n");
2537 * This handles hysteresis on regular file flushes. Because the BIOs are
2538 * routed to a thread it is possible for an excessive number to build up
2539 * and cause long front-end stalls long before the runningbuffspace limit
2540 * is hit, so we implement hammer2_flush_pipe to control the
2543 * This is a particular problem when compression is used.
2546 hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp)
2548 atomic_add_int(&pmp->count_lwinprog, 1);
2552 hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp)
2556 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2557 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2558 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2559 atomic_clear_int(&pmp->count_lwinprog,
2560 HAMMER2_LWINPROG_WAITING);
2561 wakeup(&pmp->count_lwinprog);
2566 hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp)
2571 lwinprog = pmp->count_lwinprog;
2573 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2575 tsleep_interlock(&pmp->count_lwinprog, 0);
2576 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2577 lwinprog = pmp->count_lwinprog;
2578 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2580 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2585 * Manage excessive memory resource use for chain and related
2589 hammer2_pfs_memory_wait(hammer2_pfsmount_t *pmp)
2599 * Atomic check condition and wait. Also do an early speedup of
2600 * the syncer to try to avoid hitting the wait.
2603 waiting = pmp->inmem_dirty_chains;
2605 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2607 limit = pmp->mp->mnt_nvnodelistsize / 10;
2608 if (limit < hammer2_limit_dirty_chains)
2609 limit = hammer2_limit_dirty_chains;
2614 if ((int)(ticks - zzticks) > hz) {
2616 kprintf("count %ld %ld\n", count, limit);
2621 * Block if there are too many dirty chains present, wait
2622 * for the flush to clean some out.
2624 if (count > limit) {
2625 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2626 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2628 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2629 speedup_syncer(pmp->mp);
2630 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2633 continue; /* loop on success or fail */
2637 * Try to start an early flush before we are forced to block.
2639 if (count > limit * 7 / 10)
2640 speedup_syncer(pmp->mp);
2646 hammer2_pfs_memory_inc(hammer2_pfsmount_t *pmp)
2649 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2654 hammer2_pfs_memory_wakeup(hammer2_pfsmount_t *pmp)
2662 waiting = pmp->inmem_dirty_chains;
2664 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2667 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2672 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2673 wakeup(&pmp->inmem_dirty_chains);
2680 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2682 hammer2_chain_t *scan;
2683 hammer2_chain_t *parent;
2687 kprintf("%*.*s...\n", tab, tab, "");
2692 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2694 chain, chain->bref.type,
2695 chain->bref.key, chain->bref.keybits,
2696 chain->bref.mirror_tid);
2698 kprintf("%*.*s [%08x] (%s) refs=%d\n",
2701 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2702 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2705 kprintf("%*.*s core [%08x]",
2709 parent = chain->parent;
2711 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2713 parent, parent->flags, parent->refs);
2714 if (RB_EMPTY(&chain->core.rbtree)) {
2718 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2719 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2720 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2721 kprintf("%*.*s}(%s)\n", tab, tab, "",
2722 chain->data->ipdata.filename);
2724 kprintf("%*.*s}\n", tab, tab, "");