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_dev);
76 TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
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(M_HAMMER2_CBUFFER);
110 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
111 "Buffer used for compression.");
113 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
114 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
115 "Buffer used for decompression.");
117 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
119 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
120 &hammer2_debug, 0, "");
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
122 &hammer2_cluster_enable, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
124 &hammer2_hardlink_enable, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
126 &hammer2_flush_pipe, 0, "");
127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
128 &hammer2_synchronous_flush, 0, "");
129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
130 &hammer2_limit_dirty_chains, 0, "");
131 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
132 &hammer2_dio_count, 0, "");
134 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
135 &hammer2_iod_file_read, 0, "");
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
137 &hammer2_iod_meta_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
139 &hammer2_iod_indr_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
141 &hammer2_iod_fmap_read, 0, "");
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
143 &hammer2_iod_volu_read, 0, "");
145 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
146 &hammer2_iod_file_write, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
148 &hammer2_iod_meta_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
150 &hammer2_iod_indr_write, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
152 &hammer2_iod_fmap_write, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
154 &hammer2_iod_volu_write, 0, "");
156 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
157 &hammer2_ioa_file_read, 0, "");
158 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
159 &hammer2_ioa_meta_read, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
161 &hammer2_ioa_indr_read, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
163 &hammer2_ioa_fmap_read, 0, "");
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
165 &hammer2_ioa_volu_read, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
168 &hammer2_ioa_file_write, 0, "");
169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
170 &hammer2_ioa_meta_write, 0, "");
171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
172 &hammer2_ioa_indr_write, 0, "");
173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
174 &hammer2_ioa_fmap_write, 0, "");
175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
176 &hammer2_ioa_volu_write, 0, "");
178 static int hammer2_vfs_init(struct vfsconf *conf);
179 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
180 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
182 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
183 struct vnode *, struct ucred *);
184 static int hammer2_recovery(hammer2_dev_t *hmp);
185 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
186 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
187 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
189 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
191 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
192 ino_t ino, struct vnode **vpp);
193 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
194 struct fid *fhp, struct vnode **vpp);
195 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
196 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
197 int *exflagsp, struct ucred **credanonp);
199 static int hammer2_install_volume_header(hammer2_dev_t *hmp);
200 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
202 static void hammer2_update_pmps(hammer2_dev_t *hmp);
203 static void hammer2_write_thread(void *arg);
205 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
206 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
210 * Functions for compression in threads,
211 * from hammer2_vnops.c
213 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
215 hammer2_cluster_t *cparent,
216 hammer2_key_t lbase, int ioflag, int pblksize,
218 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
220 hammer2_cluster_t *cparent,
221 hammer2_key_t lbase, int ioflag,
222 int pblksize, int *errorp,
223 int comp_algo, int check_algo);
224 static void hammer2_zero_check_and_write(struct buf *bp,
225 hammer2_trans_t *trans, hammer2_inode_t *ip,
226 hammer2_cluster_t *cparent,
228 int ioflag, int pblksize, int *errorp,
230 static int test_block_zeros(const char *buf, size_t bytes);
231 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
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;
269 static struct objcache_malloc_args margs_vop;
275 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
277 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
279 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
283 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
285 margs_read.objsize = 65536;
286 margs_read.mtype = M_HAMMER2_DEBUFFER;
288 margs_write.objsize = 32768;
289 margs_write.mtype = M_HAMMER2_CBUFFER;
291 margs_vop.objsize = sizeof(hammer2_vop_info_t);
292 margs_vop.mtype = M_HAMMER2;
294 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
295 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
296 objcache_malloc_free, &margs_read);
297 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
298 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
299 objcache_malloc_free, &margs_write);
300 cache_vop_info = objcache_create(margs_vop.mtype->ks_shortdesc,
301 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
302 objcache_malloc_free, &margs_vop);
305 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
306 TAILQ_INIT(&hammer2_mntlist);
307 TAILQ_INIT(&hammer2_pfslist);
309 hammer2_limit_dirty_chains = desiredvnodes / 10;
316 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
318 objcache_destroy(cache_buffer_read);
319 objcache_destroy(cache_buffer_write);
320 objcache_destroy(cache_vop_info);
325 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
326 * mounts and the spmp structure for media (hmp) structures.
328 * pmp->modify_tid tracks new modify_tid transaction ids for front-end
329 * transactions. Note that synchronization does not use this field.
330 * (typically frontend operations and synchronization cannot run on the
331 * same PFS node at the same time).
336 hammer2_pfsalloc(hammer2_cluster_t *cluster,
337 const hammer2_inode_data_t *ripdata,
338 hammer2_tid_t modify_tid)
340 hammer2_chain_t *rchain;
341 hammer2_inode_t *iroot;
348 * Locate or create the PFS based on the cluster id. If ripdata
349 * is NULL this is a spmp which is unique and is always allocated.
352 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
353 if (bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
354 sizeof(pmp->pfs_clid)) == 0) {
363 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
364 hammer2_trans_manage_init(&pmp->tmanage);
365 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
366 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
367 lockinit(&pmp->lock, "pfslk", 0, 0);
368 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
369 RB_INIT(&pmp->inum_tree);
370 TAILQ_INIT(&pmp->unlinkq);
371 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
374 * Save the last media transaction id for the flusher. Set
378 pmp->pfs_clid = ripdata->meta.pfs_clid;
379 hammer2_mtx_init(&pmp->wthread_mtx, "h2wthr");
380 bioq_init(&pmp->wthread_bioq);
381 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
384 * The synchronization thread may start too early, make
385 * sure it stays frozen until we are ready to let it go.
389 pmp->primary_thr.flags = HAMMER2_SYNCTHR_FROZEN |
390 HAMMER2_SYNCTHR_REMASTER;
395 * Create the PFS's root inode.
397 if ((iroot = pmp->iroot) == NULL) {
398 iroot = hammer2_inode_get(pmp, NULL, NULL);
400 hammer2_inode_ref(iroot);
401 hammer2_inode_unlock(iroot, NULL);
405 * Stop here if no cluster is passed in.
411 * When a cluster is passed in we must add the cluster's chains
412 * to the PFS's root inode, update pmp->pfs_types[], and update
413 * the syncronization threads.
415 * At the moment empty spots can develop due to removals or failures.
416 * Ultimately we want to re-fill these spots but doing so might
417 * confused running code. XXX
419 hammer2_inode_ref(iroot);
420 hammer2_mtx_ex(&iroot->lock);
421 j = iroot->cluster.nchains;
423 kprintf("add PFS to pmp %p[%d]\n", pmp, j);
425 for (i = 0; i < cluster->nchains; ++i) {
426 if (j == HAMMER2_MAXCLUSTER)
428 rchain = cluster->array[i].chain;
429 KKASSERT(rchain->pmp == NULL);
431 hammer2_chain_ref(rchain);
432 iroot->cluster.array[j].chain = rchain;
433 pmp->pfs_types[j] = ripdata->meta.pfs_type;
434 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
437 * If the PFS is already mounted we must account
438 * for the mount_count here.
441 ++rchain->hmp->mount_count;
444 * May have to fixup dirty chain tracking. Previous
445 * pmp was NULL so nothing to undo.
447 if (rchain->flags & HAMMER2_CHAIN_MODIFIED)
448 hammer2_pfs_memory_inc(pmp);
451 iroot->cluster.nchains = j;
453 if (i != cluster->nchains) {
454 kprintf("hammer2_mount: cluster full!\n");
455 /* XXX fatal error? */
459 * Update nmasters from any PFS inode which is part of the cluster.
460 * It is possible that this will result in a value which is too
461 * high. MASTER PFSs are authoritative for pfs_nmasters and will
462 * override this value later on.
464 * (This informs us of masters that might not currently be
465 * discoverable by this mount).
467 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
468 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
472 * Count visible masters. Masters are usually added with
473 * ripdata->meta.pfs_nmasters set to 1. This detects when there
474 * are more (XXX and must update the master inodes).
477 for (i = 0; i < iroot->cluster.nchains; ++i) {
478 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
481 if (pmp->pfs_nmasters < count)
482 pmp->pfs_nmasters = count;
485 * Create missing synchronization threads.
487 * Single-node masters (including snapshots) have nothing to
488 * synchronize and do not require this thread.
490 * Multi-node masters or any number of soft masters, slaves, copy,
491 * or other PFS types need the thread.
493 * Each thread is responsible for its particular cluster index.
494 * We use independent threads so stalls or mismatches related to
495 * any given target do not affect other targets.
497 for (i = 0; i < iroot->cluster.nchains; ++i) {
498 if (pmp->sync_thrs[i].td)
500 if ((pmp->pfs_nmasters > 1 &&
501 (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)) ||
502 pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER) {
503 hammer2_syncthr_create(&pmp->sync_thrs[i], pmp, i,
504 hammer2_syncthr_primary);
508 hammer2_mtx_unlock(&iroot->lock);
509 hammer2_inode_drop(iroot);
515 * Destroy a PFS, typically only occurs after the last mount on a device
519 hammer2_pfsfree(hammer2_pfs_t *pmp)
521 hammer2_inode_t *iroot;
525 * Cleanup our reference on iroot. iroot is (should) not be needed
528 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
532 for (i = 0; i < iroot->cluster.nchains; ++i)
533 hammer2_syncthr_delete(&pmp->sync_thrs[i]);
534 #if REPORT_REFS_ERRORS
535 if (pmp->iroot->refs != 1)
536 kprintf("PMP->IROOT %p REFS WRONG %d\n",
537 pmp->iroot, pmp->iroot->refs);
539 KKASSERT(pmp->iroot->refs == 1);
541 /* ref for pmp->iroot */
542 hammer2_inode_drop(pmp->iroot);
546 kmalloc_destroy(&pmp->mmsg);
547 kmalloc_destroy(&pmp->minode);
549 kfree(pmp, M_HAMMER2);
553 * Remove all references to hmp from the pfs list. Any PFS which becomes
554 * empty is terminated and freed.
559 hammer2_pfsfree_scan(hammer2_dev_t *hmp)
562 hammer2_inode_t *iroot;
563 hammer2_cluster_t *cluster;
564 hammer2_chain_t *rchain;
569 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
570 if ((iroot = pmp->iroot) == NULL)
572 if (hmp->spmp == pmp) {
573 kprintf("unmount hmp %p remove spmp %p\n",
579 * Determine if this PFS is affected. If it is we must
580 * freeze all management threads and lock its iroot.
582 * Freezing a management thread forces it idle, operations
583 * in-progress will be aborted and it will have to start
584 * over again when unfrozen, or exit if told to exit.
586 cluster = &iroot->cluster;
587 for (i = 0; i < cluster->nchains; ++i) {
588 rchain = cluster->array[i].chain;
589 if (rchain == NULL || rchain->hmp != hmp)
593 if (i != cluster->nchains) {
595 * Make sure all synchronization threads are locked
598 for (i = 0; i < iroot->cluster.nchains; ++i)
599 hammer2_syncthr_freeze(&pmp->sync_thrs[i]);
602 * Lock the inode and clean out matching chains.
603 * Note that we cannot use hammer2_inode_lock_*()
604 * here because that would attempt to validate the
605 * cluster that we are in the middle of ripping
608 * WARNING! We are working directly on the inodes
611 hammer2_mtx_ex(&iroot->lock);
614 * Remove the chain from matching elements of the PFS.
616 for (i = 0; i < cluster->nchains; ++i) {
617 rchain = cluster->array[i].chain;
618 if (rchain == NULL || rchain->hmp != hmp)
620 hammer2_syncthr_delete(&pmp->sync_thrs[i]);
621 rchain = cluster->array[i].chain;
622 cluster->array[i].chain = NULL;
623 pmp->pfs_types[i] = 0;
624 if (pmp->pfs_names[i]) {
625 kfree(pmp->pfs_names[i], M_HAMMER2);
626 pmp->pfs_names[i] = NULL;
628 hammer2_chain_drop(rchain);
631 if (cluster->focus == rchain)
632 cluster->focus = NULL;
634 hammer2_mtx_unlock(&iroot->lock);
635 didfreeze = 1; /* remaster, unfreeze down below */
641 * Cleanup trailing chains. Do not reorder chains (for now).
642 * XXX might remove more than we intended.
645 if (cluster->array[i - 1].chain)
649 cluster->nchains = i;
652 * If the PMP has no elements remaining we can destroy it.
653 * (this will transition management threads from frozen->exit).
655 if (cluster->nchains == 0) {
656 kprintf("unmount hmp %p last ref to PMP=%p\n",
658 hammer2_pfsfree(pmp);
663 * If elements still remain we need to set the REMASTER
664 * flag and unfreeze it.
667 for (i = 0; i < iroot->cluster.nchains; ++i) {
668 hammer2_syncthr_remaster(&pmp->sync_thrs[i]);
669 hammer2_syncthr_unfreeze(&pmp->sync_thrs[i]);
676 * Mount or remount HAMMER2 fileystem from physical media
679 * mp mount point structure
685 * mp mount point structure
686 * path path to mount point
687 * data pointer to argument structure in user space
688 * volume volume path (device@LABEL form)
689 * hflags user mount flags
690 * cred user credentials
697 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
700 struct hammer2_mount_info info;
704 hammer2_key_t key_next;
705 hammer2_key_t key_dummy;
708 struct nlookupdata nd;
709 hammer2_chain_t *parent;
710 hammer2_cluster_t *cluster;
711 hammer2_cluster_t *cparent;
712 const hammer2_inode_data_t *ripdata;
713 hammer2_blockref_t bref;
715 char devstr[MNAMELEN];
732 kprintf("hammer2_mount\n");
738 bzero(&info, sizeof(info));
739 info.cluster_fd = -1;
743 * Non-root mount or updating a mount
745 error = copyin(data, &info, sizeof(info));
749 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
753 /* Extract device and label */
755 label = strchr(devstr, '@');
757 ((label + 1) - dev) > done) {
765 if (mp->mnt_flag & MNT_UPDATE) {
767 * Update mount. Note that pmp->iroot->cluster is
768 * an inode-embedded cluster and thus cannot be
771 * XXX HAMMER2 needs to implement NFS export via
775 cluster = &pmp->iroot->cluster;
776 for (i = 0; i < cluster->nchains; ++i) {
777 if (cluster->array[i].chain == NULL)
779 hmp = cluster->array[i].chain->hmp;
781 error = hammer2_remount(hmp, mp, path,
786 /*hammer2_inode_install_hidden(pmp);*/
795 * Lookup name and verify it refers to a block device.
797 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
799 error = nlookup(&nd);
801 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
805 if (vn_isdisk(devvp, &error))
806 error = vfs_mountedon(devvp);
810 * Determine if the device has already been mounted. After this
811 * check hmp will be non-NULL if we are doing the second or more
812 * hammer2 mounts from the same device.
814 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
815 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
816 if (hmp->devvp == devvp)
821 * Open the device if this isn't a secondary mount and construct
822 * the H2 device mount (hmp).
825 hammer2_chain_t *schain;
828 if (error == 0 && vcount(devvp) > 0)
832 * Now open the device
835 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
836 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
837 error = vinvalbuf(devvp, V_SAVE, 0, 0);
839 error = VOP_OPEN(devvp,
840 ronly ? FREAD : FREAD | FWRITE,
845 if (error && devvp) {
850 lockmgr(&hammer2_mntlk, LK_RELEASE);
853 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
854 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
857 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
858 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
859 RB_INIT(&hmp->iotree);
860 spin_init(&hmp->io_spin, "hm2mount_io");
861 spin_init(&hmp->list_spin, "hm2mount_list");
862 TAILQ_INIT(&hmp->flushq);
864 lockinit(&hmp->vollk, "h2vol", 0, 0);
867 * vchain setup. vchain.data is embedded.
868 * vchain.refs is initialized and will never drop to 0.
870 * NOTE! voldata is not yet loaded.
872 hmp->vchain.hmp = hmp;
873 hmp->vchain.refs = 1;
874 hmp->vchain.data = (void *)&hmp->voldata;
875 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
876 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
877 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
879 hammer2_chain_core_init(&hmp->vchain);
880 /* hmp->vchain.u.xxx is left NULL */
883 * fchain setup. fchain.data is embedded.
884 * fchain.refs is initialized and will never drop to 0.
886 * The data is not used but needs to be initialized to
887 * pass assertion muster. We use this chain primarily
888 * as a placeholder for the freemap's top-level RBTREE
889 * so it does not interfere with the volume's topology
892 hmp->fchain.hmp = hmp;
893 hmp->fchain.refs = 1;
894 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
895 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
896 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
897 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
898 hmp->fchain.bref.methods =
899 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
900 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
902 hammer2_chain_core_init(&hmp->fchain);
903 /* hmp->fchain.u.xxx is left NULL */
906 * Install the volume header and initialize fields from
909 error = hammer2_install_volume_header(hmp);
911 hammer2_unmount_helper(mp, NULL, hmp);
912 lockmgr(&hammer2_mntlk, LK_RELEASE);
913 hammer2_vfs_unmount(mp, MNT_FORCE);
918 * Really important to get these right or flush will get
921 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0);
922 kprintf("alloc spmp %p tid %016jx\n",
923 hmp->spmp, hmp->voldata.mirror_tid);
927 * Dummy-up vchain and fchain's modify_tid. mirror_tid
928 * is inherited from the volume header.
931 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
932 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
933 hmp->vchain.pmp = spmp;
934 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
935 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
936 hmp->fchain.pmp = spmp;
939 * First locate the super-root inode, which is key 0
940 * relative to the volume header's blockset.
942 * Then locate the root inode by scanning the directory keyspace
943 * represented by the label.
945 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
946 schain = hammer2_chain_lookup(&parent, &key_dummy,
947 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
949 hammer2_chain_lookup_done(parent);
950 if (schain == NULL) {
951 kprintf("hammer2_mount: invalid super-root\n");
952 hammer2_unmount_helper(mp, NULL, hmp);
953 lockmgr(&hammer2_mntlk, LK_RELEASE);
954 hammer2_vfs_unmount(mp, MNT_FORCE);
958 kprintf("hammer2_mount: error %s reading super-root\n",
959 hammer2_error_str(schain->error));
960 hammer2_chain_unlock(schain);
961 hammer2_chain_drop(schain);
963 hammer2_unmount_helper(mp, NULL, hmp);
964 lockmgr(&hammer2_mntlk, LK_RELEASE);
965 hammer2_vfs_unmount(mp, MNT_FORCE);
970 * The super-root always uses an inode_tid of 1 when
974 spmp->modify_tid = schain->bref.modify_tid;
977 * Sanity-check schain's pmp and finish initialization.
978 * Any chain belonging to the super-root topology should
979 * have a NULL pmp (not even set to spmp).
981 ripdata = &hammer2_chain_rdata(schain)->ipdata;
982 KKASSERT(schain->pmp == NULL);
983 spmp->pfs_clid = ripdata->meta.pfs_clid;
986 * Replace the dummy spmp->iroot with a real one. It's
987 * easier to just do a wholesale replacement than to try
988 * to update the chain and fixup the iroot fields.
990 * The returned inode is locked with the supplied cluster.
992 cluster = hammer2_cluster_from_chain(schain);
993 hammer2_inode_drop(spmp->iroot);
995 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
996 spmp->spmp_hmp = hmp;
997 spmp->pfs_types[0] = ripdata->meta.pfs_type;
998 hammer2_inode_ref(spmp->iroot);
999 hammer2_inode_unlock(spmp->iroot, cluster);
1001 /* leave spmp->iroot with one ref */
1003 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1004 error = hammer2_recovery(hmp);
1005 /* XXX do something with error */
1007 hammer2_update_pmps(hmp);
1008 hammer2_iocom_init(hmp);
1011 * Ref the cluster management messaging descriptor. The mount
1012 * program deals with the other end of the communications pipe.
1014 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
1016 hammer2_cluster_reconnect(hmp, fp);
1018 kprintf("hammer2_mount: bad cluster_fd!\n");
1025 * Lookup the mount point under the media-localized super-root.
1026 * Scanning hammer2_pfslist doesn't help us because it represents
1027 * PFS cluster ids which can aggregate several named PFSs together.
1029 * cluster->pmp will incorrectly point to spmp and must be fixed
1032 cparent = hammer2_inode_lock(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1033 lhc = hammer2_dirhash(label, strlen(label));
1034 cluster = hammer2_cluster_lookup(cparent, &key_next,
1035 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1038 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
1040 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) {
1043 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1045 lhc + HAMMER2_DIRHASH_LOMASK, 0);
1047 hammer2_inode_unlock(spmp->iroot, cparent);
1050 * PFS could not be found?
1052 if (cluster == NULL) {
1053 kprintf("hammer2_mount: PFS label not found\n");
1054 hammer2_unmount_helper(mp, NULL, hmp);
1055 lockmgr(&hammer2_mntlk, LK_RELEASE);
1056 hammer2_vfs_unmount(mp, MNT_FORCE);
1062 * Acquire the pmp structure (it should have already been allocated
1063 * via hammer2_update_pmps() so do not pass cluster in to add to
1064 * available chains).
1066 * Check if the cluster has already been mounted. A cluster can
1067 * only be mounted once, use null mounts to mount additional copies.
1069 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1070 hammer2_cluster_bref(cluster, &bref);
1071 pmp = hammer2_pfsalloc(NULL, ripdata, bref.modify_tid);
1072 hammer2_cluster_unlock(cluster);
1073 hammer2_cluster_drop(cluster);
1076 kprintf("hammer2_mount: PFS already mounted!\n");
1077 hammer2_unmount_helper(mp, NULL, hmp);
1078 lockmgr(&hammer2_mntlk, LK_RELEASE);
1079 hammer2_vfs_unmount(mp, MNT_FORCE);
1087 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
1089 mp->mnt_flag = MNT_LOCAL;
1090 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
1091 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
1094 * required mount structure initializations
1096 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1097 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1099 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1100 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1105 mp->mnt_iosize_max = MAXPHYS;
1108 * Connect up mount pointers.
1110 hammer2_mount_helper(mp, pmp);
1112 lockmgr(&hammer2_mntlk, LK_RELEASE);
1115 * A mounted PFS needs a write thread for logical buffers and
1116 * a hidden directory for deletions of open files. These features
1117 * are not used by unmounted PFSs.
1119 * The logical file buffer bio write thread handles things like
1120 * physical block assignment and compression.
1122 pmp->wthread_destroy = 0;
1123 lwkt_create(hammer2_write_thread, pmp,
1124 &pmp->wthread_td, NULL, 0, -1, "h2pfs-%s", label);
1127 * With the cluster operational install ihidden.
1128 * (only applicable to pfs mounts, not applicable to spmp)
1130 hammer2_inode_install_hidden(pmp);
1136 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1137 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1138 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1140 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
1141 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1142 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1143 copyinstr(path, mp->mnt_stat.f_mntonname,
1144 sizeof(mp->mnt_stat.f_mntonname) - 1,
1148 * Initial statfs to prime mnt_stat.
1150 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1156 * Scan PFSs under the super-root and create hammer2_pfs structures.
1160 hammer2_update_pmps(hammer2_dev_t *hmp)
1162 const hammer2_inode_data_t *ripdata;
1163 hammer2_cluster_t *cparent;
1164 hammer2_cluster_t *cluster;
1165 hammer2_blockref_t bref;
1166 hammer2_pfs_t *spmp;
1168 hammer2_key_t key_next;
1171 * Lookup mount point under the media-localized super-root.
1173 * cluster->pmp will incorrectly point to spmp and must be fixed
1177 cparent = hammer2_inode_lock(spmp->iroot, HAMMER2_RESOLVE_ALWAYS);
1178 cluster = hammer2_cluster_lookup(cparent, &key_next,
1183 if (hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE)
1185 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1186 hammer2_cluster_bref(cluster, &bref);
1187 kprintf("ADD LOCAL PFS: %s\n", ripdata->filename);
1189 pmp = hammer2_pfsalloc(cluster, ripdata, bref.modify_tid);
1190 cluster = hammer2_cluster_next(cparent, cluster,
1196 hammer2_inode_unlock(spmp->iroot, cparent);
1200 * Handle bioq for strategy write
1204 hammer2_write_thread(void *arg)
1209 hammer2_trans_t trans;
1211 hammer2_inode_t *ip;
1212 hammer2_cluster_t *cparent;
1213 hammer2_key_t lbase;
1220 hammer2_mtx_ex(&pmp->wthread_mtx);
1223 * Wait for work. Break out and destroy the thread only if
1224 * requested and no work remains.
1226 if (bioq_first(&pmp->wthread_bioq) == NULL) {
1227 if (pmp->wthread_destroy)
1229 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
1235 * Special transaction for logical buffer cache writes.
1237 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
1239 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
1241 * dummy bio for synchronization. The transaction
1242 * must be terminated.
1244 if (bio->bio_buf == NULL) {
1245 bio->bio_flags |= BIO_DONE;
1246 /* bio will become invalid after DONE set */
1252 * else normal bio processing
1254 hammer2_mtx_unlock(&pmp->wthread_mtx);
1256 hammer2_lwinprog_drop(pmp);
1264 * Inode is modified, flush size and mtime changes
1265 * to ensure that the file size remains consistent
1266 * with the buffers being flushed.
1268 * NOTE: The inode_fsync() call only flushes the
1269 * inode's meta-data state, it doesn't try
1270 * to flush underlying buffers or chains.
1272 * NOTE: hammer2_write_file_core() may indirectly
1273 * modify and modsync the inode.
1275 cparent = hammer2_inode_lock(ip,
1276 HAMMER2_RESOLVE_ALWAYS);
1277 if (ip->flags & (HAMMER2_INODE_RESIZED |
1278 HAMMER2_INODE_MTIME)) {
1279 hammer2_inode_fsync(&trans, ip, cparent);
1281 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
1283 pblksize = hammer2_calc_physical(ip, lbase);
1284 hammer2_write_file_core(bp, &trans, ip,
1288 hammer2_inode_unlock(ip, cparent);
1290 kprintf("hammer2: error in buffer write\n");
1291 bp->b_flags |= B_ERROR;
1295 hammer2_mtx_ex(&pmp->wthread_mtx);
1297 hammer2_trans_done(&trans);
1299 pmp->wthread_destroy = -1;
1300 wakeup(&pmp->wthread_destroy);
1302 hammer2_mtx_unlock(&pmp->wthread_mtx);
1306 hammer2_bioq_sync(hammer2_pfs_t *pmp)
1308 struct bio sync_bio;
1310 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
1311 hammer2_mtx_ex(&pmp->wthread_mtx);
1312 if (pmp->wthread_destroy == 0 &&
1313 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
1314 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
1315 while ((sync_bio.bio_flags & BIO_DONE) == 0)
1316 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
1318 hammer2_mtx_unlock(&pmp->wthread_mtx);
1322 * Return a chain suitable for I/O, creating the chain if necessary
1323 * and assigning its physical block. The cluster will be in a modified
1326 * cparent can wind up being anything.
1328 * NOTE: Special case for data embedded in inode.
1332 hammer2_assign_physical(hammer2_trans_t *trans,
1333 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1334 hammer2_key_t lbase, int pblksize, int *errorp)
1336 hammer2_cluster_t *cluster;
1337 hammer2_cluster_t *dparent;
1338 hammer2_key_t key_dummy;
1339 int pradix = hammer2_getradix(pblksize);
1342 * Locate the chain associated with lbase, return a locked chain.
1343 * However, do not instantiate any data reference (which utilizes a
1344 * device buffer) because we will be using direct IO via the
1345 * logical buffer cache buffer.
1348 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
1350 dparent = hammer2_cluster_lookup_init(cparent, 0);
1351 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
1353 HAMMER2_LOOKUP_NODATA);
1355 if (cluster == NULL) {
1357 * We found a hole, create a new chain entry.
1359 * NOTE: DATA chains are created without device backing
1360 * store (nor do we want any).
1362 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
1363 lbase, HAMMER2_PBUFRADIX,
1364 HAMMER2_BREF_TYPE_DATA,
1366 if (cluster == NULL) {
1367 hammer2_cluster_lookup_done(dparent);
1368 panic("hammer2_cluster_create: par=%p error=%d\n",
1369 dparent->focus, *errorp);
1372 /*ip->delta_dcount += pblksize;*/
1374 switch (hammer2_cluster_type(cluster)) {
1375 case HAMMER2_BREF_TYPE_INODE:
1377 * The data is embedded in the inode, which requires
1378 * a bit more finess.
1380 hammer2_cluster_modify_ip(trans, ip, cluster, 0);
1382 case HAMMER2_BREF_TYPE_DATA:
1383 if (hammer2_cluster_need_resize(cluster, pblksize)) {
1384 hammer2_cluster_resize(trans, ip,
1387 HAMMER2_MODIFY_OPTDATA);
1391 * DATA buffers must be marked modified whether the
1392 * data is in a logical buffer or not. We also have
1393 * to make this call to fixup the chain data pointers
1394 * after resizing in case this is an encrypted or
1395 * compressed buffer.
1397 hammer2_cluster_modify(trans, cluster,
1398 HAMMER2_MODIFY_OPTDATA);
1401 panic("hammer2_assign_physical: bad type");
1408 * Cleanup. If cluster wound up being the inode itself, i.e.
1409 * the DIRECTDATA case for offset 0, then we need to update cparent.
1410 * The caller expects cparent to not become stale.
1412 hammer2_cluster_lookup_done(dparent);
1413 /* dparent = NULL; safety */
1418 * bio queued from hammer2_vnops.c.
1420 * The core write function which determines which path to take
1421 * depending on compression settings. We also have to locate the
1422 * related clusters so we can calculate and set the check data for
1427 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
1428 hammer2_inode_t *ip,
1429 hammer2_cluster_t *cparent,
1430 hammer2_key_t lbase, int ioflag, int pblksize,
1433 hammer2_cluster_t *cluster;
1435 switch(HAMMER2_DEC_ALGO(ip->meta.comp_algo)) {
1436 case HAMMER2_COMP_NONE:
1438 * We have to assign physical storage to the buffer
1439 * we intend to dirty or write now to avoid deadlocks
1440 * in the strategy code later.
1442 * This can return NOOFFSET for inode-embedded data.
1443 * The strategy code will take care of it in that case.
1445 cluster = hammer2_assign_physical(trans, ip, cparent,
1448 if (cluster->ddflag) {
1449 hammer2_inode_data_t *wipdata;
1451 wipdata = hammer2_cluster_modify_ip(trans, ip,
1453 KKASSERT(wipdata->meta.op_flags &
1454 HAMMER2_OPFLAG_DIRECTDATA);
1455 KKASSERT(bp->b_loffset == 0);
1456 bcopy(bp->b_data, wipdata->u.data,
1457 HAMMER2_EMBEDDED_BYTES);
1458 hammer2_cluster_modsync(cluster);
1460 hammer2_write_bp(cluster, bp, ioflag, pblksize,
1461 errorp, ip->meta.check_algo);
1464 hammer2_cluster_unlock(cluster);
1465 hammer2_cluster_drop(cluster);
1468 case HAMMER2_COMP_AUTOZERO:
1470 * Check for zero-fill only
1472 hammer2_zero_check_and_write(bp, trans, ip,
1474 ioflag, pblksize, errorp,
1475 ip->meta.check_algo);
1477 case HAMMER2_COMP_LZ4:
1478 case HAMMER2_COMP_ZLIB:
1481 * Check for zero-fill and attempt compression.
1483 hammer2_compress_and_write(bp, trans, ip,
1488 ip->meta.check_algo);
1494 * Generic function that will perform the compression in compression
1495 * write path. The compression algorithm is determined by the settings
1496 * obtained from inode.
1500 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
1501 hammer2_inode_t *ip,
1502 hammer2_cluster_t *cparent,
1503 hammer2_key_t lbase, int ioflag, int pblksize,
1504 int *errorp, int comp_algo, int check_algo)
1506 hammer2_cluster_t *cluster;
1507 hammer2_chain_t *chain;
1509 int comp_block_size;
1513 if (test_block_zeros(bp->b_data, pblksize)) {
1514 zero_write(bp, trans, ip, cparent, lbase, errorp);
1521 KKASSERT(pblksize / 2 <= 32768);
1523 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1524 z_stream strm_compress;
1528 switch(HAMMER2_DEC_ALGO(comp_algo)) {
1529 case HAMMER2_COMP_LZ4:
1530 comp_buffer = objcache_get(cache_buffer_write,
1532 comp_size = LZ4_compress_limitedOutput(
1534 &comp_buffer[sizeof(int)],
1536 pblksize / 2 - sizeof(int));
1538 * We need to prefix with the size, LZ4
1539 * doesn't do it for us. Add the related
1542 *(int *)comp_buffer = comp_size;
1544 comp_size += sizeof(int);
1546 case HAMMER2_COMP_ZLIB:
1547 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1548 if (comp_level == 0)
1549 comp_level = 6; /* default zlib compression */
1550 else if (comp_level < 6)
1552 else if (comp_level > 9)
1554 ret = deflateInit(&strm_compress, comp_level);
1556 kprintf("HAMMER2 ZLIB: fatal error "
1557 "on deflateInit.\n");
1560 comp_buffer = objcache_get(cache_buffer_write,
1562 strm_compress.next_in = bp->b_data;
1563 strm_compress.avail_in = pblksize;
1564 strm_compress.next_out = comp_buffer;
1565 strm_compress.avail_out = pblksize / 2;
1566 ret = deflate(&strm_compress, Z_FINISH);
1567 if (ret == Z_STREAM_END) {
1568 comp_size = pblksize / 2 -
1569 strm_compress.avail_out;
1573 ret = deflateEnd(&strm_compress);
1576 kprintf("Error: Unknown compression method.\n");
1577 kprintf("Comp_method = %d.\n", comp_algo);
1582 if (comp_size == 0) {
1584 * compression failed or turned off
1586 comp_block_size = pblksize; /* safety */
1587 if (++ip->comp_heuristic > 128)
1588 ip->comp_heuristic = 8;
1591 * compression succeeded
1593 ip->comp_heuristic = 0;
1594 if (comp_size <= 1024) {
1595 comp_block_size = 1024;
1596 } else if (comp_size <= 2048) {
1597 comp_block_size = 2048;
1598 } else if (comp_size <= 4096) {
1599 comp_block_size = 4096;
1600 } else if (comp_size <= 8192) {
1601 comp_block_size = 8192;
1602 } else if (comp_size <= 16384) {
1603 comp_block_size = 16384;
1604 } else if (comp_size <= 32768) {
1605 comp_block_size = 32768;
1607 panic("hammer2: WRITE PATH: "
1608 "Weird comp_size value.");
1610 comp_block_size = pblksize;
1614 cluster = hammer2_assign_physical(trans, ip, cparent,
1615 lbase, comp_block_size,
1618 kprintf("WRITE PATH: An error occurred while "
1619 "assigning physical space.\n");
1620 KKASSERT(cluster == NULL);
1624 if (cluster->ddflag) {
1625 hammer2_inode_data_t *wipdata;
1627 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1628 KKASSERT(wipdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1629 KKASSERT(bp->b_loffset == 0);
1630 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1631 hammer2_cluster_modsync(cluster);
1633 for (i = 0; i < cluster->nchains; ++i) {
1639 if ((cluster->array[i].flags & HAMMER2_CITEM_FEMOD) == 0)
1641 chain = cluster->array[i].chain; /* XXX */
1644 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1646 switch(chain->bref.type) {
1647 case HAMMER2_BREF_TYPE_INODE:
1648 panic("hammer2_write_bp: unexpected inode\n");
1650 case HAMMER2_BREF_TYPE_DATA:
1652 * Optimize out the read-before-write
1655 *errorp = hammer2_io_newnz(chain->hmp,
1656 chain->bref.data_off,
1660 hammer2_io_brelse(&dio);
1661 kprintf("hammer2: WRITE PATH: "
1662 "dbp bread error\n");
1665 bdata = hammer2_io_data(dio, chain->bref.data_off);
1668 * When loading the block make sure we don't
1669 * leave garbage after the compressed data.
1672 chain->bref.methods =
1673 HAMMER2_ENC_COMP(comp_algo) +
1674 HAMMER2_ENC_CHECK(check_algo);
1675 bcopy(comp_buffer, bdata, comp_size);
1676 if (comp_size != comp_block_size) {
1677 bzero(bdata + comp_size,
1678 comp_block_size - comp_size);
1681 chain->bref.methods =
1683 HAMMER2_COMP_NONE) +
1684 HAMMER2_ENC_CHECK(check_algo);
1685 bcopy(bp->b_data, bdata, pblksize);
1689 * The flush code doesn't calculate check codes for
1690 * file data (doing so can result in excessive I/O),
1693 hammer2_chain_setcheck(chain, bdata);
1696 * Device buffer is now valid, chain is no longer in
1697 * the initial state.
1699 * (No blockref table worries with file data)
1701 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1703 /* Now write the related bdp. */
1704 if (ioflag & IO_SYNC) {
1706 * Synchronous I/O requested.
1708 hammer2_io_bwrite(&dio);
1710 } else if ((ioflag & IO_DIRECT) &&
1711 loff + n == pblksize) {
1712 hammer2_io_bdwrite(&dio);
1714 } else if (ioflag & IO_ASYNC) {
1715 hammer2_io_bawrite(&dio);
1717 hammer2_io_bdwrite(&dio);
1721 panic("hammer2_write_bp: bad chain type %d\n",
1729 hammer2_cluster_unlock(cluster);
1730 hammer2_cluster_drop(cluster);
1733 objcache_put(cache_buffer_write, comp_buffer);
1737 * Function that performs zero-checking and writing without compression,
1738 * it corresponds to default zero-checking path.
1742 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1743 hammer2_inode_t *ip,
1744 hammer2_cluster_t *cparent,
1745 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp,
1748 hammer2_cluster_t *cluster;
1750 if (test_block_zeros(bp->b_data, pblksize)) {
1751 zero_write(bp, trans, ip, cparent, lbase, errorp);
1753 cluster = hammer2_assign_physical(trans, ip, cparent,
1754 lbase, pblksize, errorp);
1755 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1758 hammer2_cluster_unlock(cluster);
1759 hammer2_cluster_drop(cluster);
1765 * A function to test whether a block of data contains only zeros,
1766 * returns TRUE (non-zero) if the block is all zeros.
1770 test_block_zeros(const char *buf, size_t bytes)
1774 for (i = 0; i < bytes; i += sizeof(long)) {
1775 if (*(const long *)(buf + i) != 0)
1782 * Function to "write" a block that contains only zeros.
1786 zero_write(struct buf *bp, hammer2_trans_t *trans,
1787 hammer2_inode_t *ip,
1788 hammer2_cluster_t *cparent,
1789 hammer2_key_t lbase, int *errorp __unused)
1791 hammer2_cluster_t *cluster;
1792 hammer2_key_t key_dummy;
1794 cparent = hammer2_cluster_lookup_init(cparent, 0);
1795 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1796 HAMMER2_LOOKUP_NODATA);
1798 if (cluster->ddflag) {
1799 hammer2_inode_data_t *wipdata;
1801 wipdata = hammer2_cluster_modify_ip(trans, ip,
1803 KKASSERT(wipdata->meta.op_flags &
1804 HAMMER2_OPFLAG_DIRECTDATA);
1805 KKASSERT(bp->b_loffset == 0);
1806 bzero(wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1807 hammer2_cluster_modsync(cluster);
1809 hammer2_cluster_delete(trans, cparent, cluster,
1810 HAMMER2_DELETE_PERMANENT);
1812 hammer2_cluster_unlock(cluster);
1813 hammer2_cluster_drop(cluster);
1815 hammer2_cluster_lookup_done(cparent);
1819 * Function to write the data as it is, without performing any sort of
1820 * compression. This function is used in path without compression and
1821 * default zero-checking path.
1825 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1826 int pblksize, int *errorp, int check_algo)
1828 hammer2_chain_t *chain;
1829 hammer2_inode_data_t *wipdata;
1835 error = 0; /* XXX TODO below */
1837 for (i = 0; i < cluster->nchains; ++i) {
1838 if ((cluster->array[i].flags & HAMMER2_CITEM_FEMOD) == 0)
1840 chain = cluster->array[i].chain; /* XXX */
1843 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1845 switch(chain->bref.type) {
1846 case HAMMER2_BREF_TYPE_INODE:
1847 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1848 KKASSERT(wipdata->meta.op_flags &
1849 HAMMER2_OPFLAG_DIRECTDATA);
1850 KKASSERT(bp->b_loffset == 0);
1851 bcopy(bp->b_data, wipdata->u.data,
1852 HAMMER2_EMBEDDED_BYTES);
1855 case HAMMER2_BREF_TYPE_DATA:
1856 error = hammer2_io_newnz(chain->hmp,
1857 chain->bref.data_off,
1858 chain->bytes, &dio);
1860 hammer2_io_bqrelse(&dio);
1861 kprintf("hammer2: WRITE PATH: "
1862 "dbp bread error\n");
1865 bdata = hammer2_io_data(dio, chain->bref.data_off);
1867 chain->bref.methods = HAMMER2_ENC_COMP(
1868 HAMMER2_COMP_NONE) +
1869 HAMMER2_ENC_CHECK(check_algo);
1870 bcopy(bp->b_data, bdata, chain->bytes);
1873 * The flush code doesn't calculate check codes for
1874 * file data (doing so can result in excessive I/O),
1877 hammer2_chain_setcheck(chain, bdata);
1880 * Device buffer is now valid, chain is no longer in
1881 * the initial state.
1883 * (No blockref table worries with file data)
1885 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1887 if (ioflag & IO_SYNC) {
1889 * Synchronous I/O requested.
1891 hammer2_io_bwrite(&dio);
1893 } else if ((ioflag & IO_DIRECT) &&
1894 loff + n == pblksize) {
1895 hammer2_io_bdwrite(&dio);
1897 } else if (ioflag & IO_ASYNC) {
1898 hammer2_io_bawrite(&dio);
1900 hammer2_io_bdwrite(&dio);
1904 panic("hammer2_write_bp: bad chain type %d\n",
1910 KKASSERT(error == 0); /* XXX TODO */
1917 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path,
1918 struct vnode *devvp, struct ucred *cred)
1922 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1923 error = hammer2_recovery(hmp);
1932 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1943 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1946 * If mount initialization proceeded far enough we must flush
1947 * its vnodes and sync the underlying mount points. Three syncs
1948 * are required to fully flush the filesystem (freemap updates lag
1949 * by one flush, and one extra for safety).
1951 if (mntflags & MNT_FORCE)
1956 error = vflush(mp, 0, flags);
1959 hammer2_vfs_sync(mp, MNT_WAIT);
1960 hammer2_vfs_sync(mp, MNT_WAIT);
1961 hammer2_vfs_sync(mp, MNT_WAIT);
1964 if (pmp->wthread_td) {
1965 hammer2_mtx_ex(&pmp->wthread_mtx);
1966 pmp->wthread_destroy = 1;
1967 wakeup(&pmp->wthread_bioq);
1968 while (pmp->wthread_destroy != -1) {
1969 mtxsleep(&pmp->wthread_destroy,
1970 &pmp->wthread_mtx, 0,
1973 hammer2_mtx_unlock(&pmp->wthread_mtx);
1974 pmp->wthread_td = NULL;
1978 * Cleanup our reference on ihidden.
1981 hammer2_inode_drop(pmp->ihidden);
1982 pmp->ihidden = NULL;
1985 hammer2_unmount_helper(mp, pmp, NULL);
1989 lockmgr(&hammer2_mntlk, LK_RELEASE);
1995 * Mount helper, hook the system mount into our PFS.
1996 * The mount lock is held.
1998 * We must bump the mount_count on related devices for any
2003 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
2005 hammer2_cluster_t *cluster;
2006 hammer2_chain_t *rchain;
2009 mp->mnt_data = (qaddr_t)pmp;
2013 * After pmp->mp is set we have to adjust hmp->mount_count.
2015 cluster = &pmp->iroot->cluster;
2016 for (i = 0; i < cluster->nchains; ++i) {
2017 rchain = cluster->array[i].chain;
2020 ++rchain->hmp->mount_count;
2021 kprintf("hammer2_mount hmp=%p ++mount_count=%d\n",
2022 rchain->hmp, rchain->hmp->mount_count);
2027 * Mount helper, unhook the system mount from our PFS.
2028 * The mount lock is held.
2030 * If hmp is supplied a mount responsible for being the first to open
2031 * the block device failed and the block device and all PFSs using the
2032 * block device must be cleaned up.
2034 * If pmp is supplied multiple devices might be backing the PFS and each
2035 * must be disconnect. This might not be the last PFS using some of the
2036 * underlying devices. Also, we have to adjust our hmp->mount_count
2037 * accounting for the devices backing the pmp which is now undergoing an
2042 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
2044 hammer2_cluster_t *cluster;
2045 hammer2_chain_t *rchain;
2046 struct vnode *devvp;
2052 * If no device supplied this is a high-level unmount and we have to
2053 * to disconnect the mount, adjust mount_count, and locate devices
2054 * that might now have no mounts.
2057 KKASSERT(hmp == NULL);
2058 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
2060 mp->mnt_data = NULL;
2063 * After pmp->mp is cleared we have to account for
2066 cluster = &pmp->iroot->cluster;
2067 for (i = 0; i < cluster->nchains; ++i) {
2068 rchain = cluster->array[i].chain;
2071 --rchain->hmp->mount_count;
2072 kprintf("hammer2_unmount hmp=%p --mount_count=%d\n",
2073 rchain->hmp, rchain->hmp->mount_count);
2074 /* scrapping hmp now may invalidate the pmp */
2077 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
2078 if (hmp->mount_count == 0) {
2079 hammer2_unmount_helper(NULL, NULL, hmp);
2087 * Try to terminate the block device. We can't terminate it if
2088 * there are still PFSs referencing it.
2090 kprintf("hammer2_unmount hmp=%p mount_count=%d\n",
2091 hmp, hmp->mount_count);
2092 if (hmp->mount_count)
2095 hammer2_pfsfree_scan(hmp);
2096 hammer2_dev_exlock(hmp); /* XXX order */
2099 * Cycle the volume data lock as a safety (probably not needed any
2100 * more). To ensure everything is out we need to flush at least
2101 * three times. (1) The running of the unlinkq can dirty the
2102 * filesystem, (2) A normal flush can dirty the freemap, and
2103 * (3) ensure that the freemap is fully synchronized.
2105 * The next mount's recovery scan can clean everything up but we want
2106 * to leave the filesystem in a 100% clean state on a normal unmount.
2109 hammer2_voldata_lock(hmp);
2110 hammer2_voldata_unlock(hmp);
2112 hammer2_iocom_uninit(hmp);
2114 if ((hmp->vchain.flags | hmp->fchain.flags) &
2115 HAMMER2_CHAIN_FLUSH_MASK) {
2116 kprintf("hammer2_unmount: chains left over "
2117 "after final sync\n");
2118 kprintf(" vchain %08x\n", hmp->vchain.flags);
2119 kprintf(" fchain %08x\n", hmp->fchain.flags);
2121 if (hammer2_debug & 0x0010)
2122 Debugger("entered debugger");
2125 KKASSERT(hmp->spmp == NULL);
2128 * Finish up with the device vnode
2130 if ((devvp = hmp->devvp) != NULL) {
2131 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
2132 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
2134 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
2141 * Clear vchain/fchain flags that might prevent final cleanup
2144 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
2145 atomic_clear_int(&hmp->vchain.flags,
2146 HAMMER2_CHAIN_MODIFIED);
2147 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
2148 hammer2_chain_drop(&hmp->vchain);
2150 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
2151 atomic_clear_int(&hmp->vchain.flags,
2152 HAMMER2_CHAIN_UPDATE);
2153 hammer2_chain_drop(&hmp->vchain);
2156 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
2157 atomic_clear_int(&hmp->fchain.flags,
2158 HAMMER2_CHAIN_MODIFIED);
2159 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
2160 hammer2_chain_drop(&hmp->fchain);
2162 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
2163 atomic_clear_int(&hmp->fchain.flags,
2164 HAMMER2_CHAIN_UPDATE);
2165 hammer2_chain_drop(&hmp->fchain);
2169 * Final drop of embedded freemap root chain to
2170 * clean up fchain.core (fchain structure is not
2171 * flagged ALLOCATED so it is cleaned out and then
2174 hammer2_chain_drop(&hmp->fchain);
2177 * Final drop of embedded volume root chain to clean
2178 * up vchain.core (vchain structure is not flagged
2179 * ALLOCATED so it is cleaned out and then left to
2183 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
2185 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
2186 hammer2_dev_unlock(hmp);
2187 hammer2_chain_drop(&hmp->vchain);
2189 hammer2_io_cleanup(hmp, &hmp->iotree);
2190 if (hmp->iofree_count) {
2191 kprintf("io_cleanup: %d I/O's left hanging\n",
2195 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
2196 kmalloc_destroy(&hmp->mchain);
2197 kfree(hmp, M_HAMMER2);
2202 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
2203 ino_t ino, struct vnode **vpp)
2205 kprintf("hammer2_vget\n");
2206 return (EOPNOTSUPP);
2211 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
2214 hammer2_cluster_t *cparent;
2219 if (pmp->iroot == NULL) {
2223 cparent = hammer2_inode_lock(pmp->iroot,
2224 HAMMER2_RESOLVE_ALWAYS |
2225 HAMMER2_RESOLVE_SHARED);
2226 vp = hammer2_igetv(pmp->iroot, cparent, &error);
2227 hammer2_inode_unlock(pmp->iroot, cparent);
2230 kprintf("vnodefail\n");
2239 * XXX incorporate ipdata->meta.inode_quota and data_quota
2243 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
2247 hammer2_blockref_t bref;
2250 KKASSERT(pmp->iroot->cluster.nchains >= 1);
2251 hmp = pmp->iroot->cluster.focus->hmp; /* iroot retains focus */
2252 bref = pmp->iroot->cluster.focus->bref; /* no lock */
2254 mp->mnt_stat.f_files = bref.inode_count;
2255 mp->mnt_stat.f_ffree = 0;
2256 mp->mnt_stat.f_blocks = (bref.data_count +
2257 hmp->voldata.allocator_free) /
2258 mp->mnt_vstat.f_bsize;
2259 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free /
2260 mp->mnt_vstat.f_bsize;
2261 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
2263 *sbp = mp->mnt_stat;
2269 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
2273 hammer2_blockref_t bref;
2276 KKASSERT(pmp->iroot->cluster.nchains >= 1);
2277 hmp = pmp->iroot->cluster.focus->hmp; /* iroot retains focus */
2278 bref = pmp->iroot->cluster.focus->bref; /* no lock */
2280 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
2281 mp->mnt_vstat.f_files = bref.inode_count;
2282 mp->mnt_vstat.f_ffree = 0;
2283 mp->mnt_vstat.f_blocks = (bref.data_count +
2284 hmp->voldata.allocator_free) /
2285 mp->mnt_vstat.f_bsize;
2286 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free /
2287 mp->mnt_vstat.f_bsize;
2288 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
2290 *sbp = mp->mnt_vstat;
2295 * Mount-time recovery (RW mounts)
2297 * Updates to the free block table are allowed to lag flushes by one
2298 * transaction. In case of a crash, then on a fresh mount we must do an
2299 * incremental scan of the last committed transaction id and make sure that
2300 * all related blocks have been marked allocated.
2302 * The super-root topology and each PFS has its own transaction id domain,
2303 * so we must track PFS boundary transitions.
2305 struct hammer2_recovery_elm {
2306 TAILQ_ENTRY(hammer2_recovery_elm) entry;
2307 hammer2_chain_t *chain;
2308 hammer2_tid_t sync_tid;
2311 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
2313 struct hammer2_recovery_info {
2314 struct hammer2_recovery_list list;
2318 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_dev_t *hmp,
2319 hammer2_chain_t *parent,
2320 struct hammer2_recovery_info *info,
2321 hammer2_tid_t sync_tid);
2323 #define HAMMER2_RECOVERY_MAXDEPTH 10
2327 hammer2_recovery(hammer2_dev_t *hmp)
2329 hammer2_trans_t trans;
2330 struct hammer2_recovery_info info;
2331 struct hammer2_recovery_elm *elm;
2332 hammer2_chain_t *parent;
2333 hammer2_tid_t sync_tid;
2334 hammer2_tid_t mirror_tid;
2336 int cumulative_error = 0;
2338 hammer2_trans_init(&trans, hmp->spmp, 0);
2340 sync_tid = hmp->voldata.freemap_tid;
2341 mirror_tid = hmp->voldata.mirror_tid;
2343 kprintf("hammer2 mount \"%s\": ", hmp->devrepname);
2344 if (sync_tid >= mirror_tid) {
2345 kprintf(" no recovery needed\n");
2347 kprintf(" freemap recovery %016jx-%016jx\n",
2348 sync_tid + 1, mirror_tid);
2351 TAILQ_INIT(&info.list);
2353 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
2354 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent,
2356 hammer2_chain_lookup_done(parent);
2358 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
2359 TAILQ_REMOVE(&info.list, elm, entry);
2360 parent = elm->chain;
2361 sync_tid = elm->sync_tid;
2362 kfree(elm, M_HAMMER2);
2364 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2365 error = hammer2_recovery_scan(&trans, hmp, parent,
2367 hmp->voldata.freemap_tid);
2368 hammer2_chain_unlock(parent);
2369 hammer2_chain_drop(parent); /* drop elm->chain ref */
2371 cumulative_error = error;
2373 hammer2_trans_done(&trans);
2375 return cumulative_error;
2380 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_dev_t *hmp,
2381 hammer2_chain_t *parent,
2382 struct hammer2_recovery_info *info,
2383 hammer2_tid_t sync_tid)
2385 const hammer2_inode_data_t *ripdata;
2386 hammer2_chain_t *chain;
2388 int cumulative_error = 0;
2392 * Adjust freemap to ensure that the block(s) are marked allocated.
2394 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
2395 hammer2_freemap_adjust(trans, hmp, &parent->bref,
2396 HAMMER2_FREEMAP_DORECOVER);
2400 * Check type for recursive scan
2402 switch(parent->bref.type) {
2403 case HAMMER2_BREF_TYPE_VOLUME:
2404 /* data already instantiated */
2406 case HAMMER2_BREF_TYPE_INODE:
2408 * Must instantiate data for DIRECTDATA test and also
2411 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2412 ripdata = &hammer2_chain_rdata(parent)->ipdata;
2413 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2414 /* not applicable to recovery scan */
2415 hammer2_chain_unlock(parent);
2418 hammer2_chain_unlock(parent);
2420 case HAMMER2_BREF_TYPE_INDIRECT:
2422 * Must instantiate data for recursion
2424 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2425 hammer2_chain_unlock(parent);
2427 case HAMMER2_BREF_TYPE_DATA:
2428 case HAMMER2_BREF_TYPE_FREEMAP:
2429 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2430 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2431 /* not applicable to recovery scan */
2439 * Defer operation if depth limit reached or if we are crossing a
2442 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
2443 struct hammer2_recovery_elm *elm;
2445 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2446 elm->chain = parent;
2447 elm->sync_tid = sync_tid;
2448 hammer2_chain_ref(parent);
2449 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2450 /* unlocked by caller */
2457 * Recursive scan of the last flushed transaction only. We are
2458 * doing this without pmp assignments so don't leave the chains
2459 * hanging around after we are done with them.
2462 chain = hammer2_chain_scan(parent, NULL, &cache_index,
2463 HAMMER2_LOOKUP_NODATA);
2465 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2466 if (chain->bref.mirror_tid > sync_tid) {
2468 error = hammer2_recovery_scan(trans, hmp, chain,
2472 cumulative_error = error;
2476 * Flush the recovery at the PFS boundary to stage it for
2477 * the final flush of the super-root topology.
2479 if ((chain->bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
2480 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
2481 hammer2_flush(trans, chain, 1);
2483 chain = hammer2_chain_scan(parent, chain, &cache_index,
2484 HAMMER2_LOOKUP_NODATA);
2487 return cumulative_error;
2491 * Sync a mount point; this is called on a per-mount basis from the
2492 * filesystem syncer process periodically and whenever a user issues
2496 hammer2_vfs_sync(struct mount *mp, int waitfor)
2498 struct hammer2_sync_info info;
2499 hammer2_inode_t *iroot;
2500 hammer2_chain_t *chain;
2501 hammer2_chain_t *parent;
2513 KKASSERT(iroot->pmp == pmp);
2516 * We can't acquire locks on existing vnodes while in a transaction
2517 * without risking a deadlock. This assumes that vfsync() can be
2518 * called without the vnode locked (which it can in DragonFly).
2519 * Otherwise we'd have to implement a multi-pass or flag the lock
2520 * failures and retry.
2522 * The reclamation code interlocks with the sync list's token
2523 * (by removing the vnode from the scan list) before unlocking
2524 * the inode, giving us time to ref the inode.
2526 /*flags = VMSC_GETVP;*/
2528 if (waitfor & MNT_LAZY)
2529 flags |= VMSC_ONEPASS;
2533 * Preflush the vnodes using a normal transaction before interlocking
2534 * with a flush transaction.
2536 hammer2_trans_init(&info.trans, pmp, 0);
2538 info.waitfor = MNT_NOWAIT;
2539 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2540 hammer2_trans_done(&info.trans);
2544 * Start our flush transaction. This does not return until all
2545 * concurrent transactions have completed and will prevent any
2546 * new transactions from running concurrently, except for the
2547 * buffer cache transactions.
2549 * For efficiency do an async pass before making sure with a
2550 * synchronous pass on all related buffer cache buffers. It
2551 * should theoretically not be possible for any new file buffers
2552 * to be instantiated during this sequence.
2554 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
2555 HAMMER2_TRANS_PREFLUSH);
2556 hammer2_run_unlinkq(&info.trans, pmp);
2559 info.waitfor = MNT_NOWAIT;
2560 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2561 info.waitfor = MNT_WAIT;
2562 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2565 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2566 * buffer cache flushes which occur during the flush. Device buffers
2569 hammer2_bioq_sync(info.trans.pmp);
2570 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
2575 * Flush all nodes to synchronize the PFSROOT subtopology to the media.
2577 * Note that this flush will not be visible on crash recovery until
2578 * we flush the super-root topology in the next loop.
2580 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2581 chain = iroot->cluster.array[i].chain;
2585 hammer2_chain_ref(chain);
2586 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
2587 if (chain->flags & HAMMER2_CHAIN_FLUSH_MASK) {
2588 hammer2_flush(&info.trans, chain, 1);
2589 parent = chain->parent;
2590 KKASSERT(chain->pmp != parent->pmp);
2591 hammer2_chain_setflush(&info.trans, parent);
2593 hammer2_chain_unlock(chain);
2594 hammer2_chain_drop(chain);
2596 hammer2_trans_done(&info.trans);
2599 * Flush all volume roots to synchronize PFS flushes with the
2600 * storage media volume header. This will flush the freemap and
2601 * the superroot topology but stops when it reaches a PFSROOT
2602 * (which we already flushed above).
2604 * This is the last step which connects the volume root to the
2605 * PFSROOT dirs flushed above.
2607 * Each spmp (representing the hmp's super-root) requires its own
2610 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2611 hammer2_chain_t *tmp;
2613 chain = iroot->cluster.array[i].chain;
2620 * We only have to flush each hmp once
2622 for (j = i - 1; j >= 0; --j) {
2623 if ((tmp = iroot->cluster.array[j].chain) != NULL) {
2624 if (tmp->hmp == hmp)
2632 * spmp transaction. The super-root is never directly
2633 * mounted so there shouldn't be any vnodes, let alone any
2634 * dirty vnodes associated with it.
2636 hammer2_trans_init(&info.trans, hmp->spmp,
2637 HAMMER2_TRANS_ISFLUSH);
2640 * Media mounts have two 'roots', vchain for the topology
2641 * and fchain for the free block table. Flush both.
2643 * Note that the topology and free block table are handled
2644 * independently, so the free block table can wind up being
2645 * ahead of the topology. We depend on the bulk free scan
2646 * code to deal with any loose ends.
2648 hammer2_chain_ref(&hmp->vchain);
2649 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2650 hammer2_chain_ref(&hmp->fchain);
2651 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2652 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2654 * This will also modify vchain as a side effect,
2655 * mark vchain as modified now.
2657 hammer2_voldata_modify(hmp);
2658 chain = &hmp->fchain;
2659 hammer2_flush(&info.trans, chain, 1);
2660 KKASSERT(chain == &hmp->fchain);
2662 hammer2_chain_unlock(&hmp->fchain);
2663 hammer2_chain_unlock(&hmp->vchain);
2664 hammer2_chain_drop(&hmp->fchain);
2665 /* vchain dropped down below */
2667 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2668 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2669 chain = &hmp->vchain;
2670 hammer2_flush(&info.trans, chain, 1);
2671 KKASSERT(chain == &hmp->vchain);
2673 hammer2_chain_unlock(&hmp->vchain);
2674 hammer2_chain_drop(&hmp->vchain);
2679 * We can't safely flush the volume header until we have
2680 * flushed any device buffers which have built up.
2682 * XXX this isn't being incremental
2684 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2685 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2686 vn_unlock(hmp->devvp);
2689 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2690 * volume header needs synchronization via hmp->volsync.
2692 * XXX synchronize the flag & data with only this flush XXX
2695 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2699 * Synchronize the disk before flushing the volume
2703 bp->b_bio1.bio_offset = 0;
2706 bp->b_cmd = BUF_CMD_FLUSH;
2707 bp->b_bio1.bio_done = biodone_sync;
2708 bp->b_bio1.bio_flags |= BIO_SYNC;
2709 vn_strategy(hmp->devvp, &bp->b_bio1);
2710 biowait(&bp->b_bio1, "h2vol");
2714 * Then we can safely flush the version of the
2715 * volume header synchronized by the flush code.
2717 i = hmp->volhdrno + 1;
2718 if (i >= HAMMER2_NUM_VOLHDRS)
2720 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2721 hmp->volsync.volu_size) {
2724 kprintf("sync volhdr %d %jd\n",
2725 i, (intmax_t)hmp->volsync.volu_size);
2726 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2727 HAMMER2_PBUFSIZE, 0, 0);
2728 atomic_clear_int(&hmp->vchain.flags,
2729 HAMMER2_CHAIN_VOLUMESYNC);
2730 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2735 total_error = error;
2737 hammer2_trans_done(&info.trans); /* spmp trans */
2739 return (total_error);
2746 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2748 struct hammer2_sync_info *info = data;
2749 hammer2_inode_t *ip;
2753 * Degenerate cases. Note that ip == NULL typically means the
2754 * syncer vnode itself and we don't want to vclrisdirty() in that
2761 if (vp->v_type == VNON || vp->v_type == VBAD) {
2767 * VOP_FSYNC will start a new transaction so replicate some code
2768 * here to do it inline (see hammer2_vop_fsync()).
2770 * WARNING: The vfsync interacts with the buffer cache and might
2771 * block, we can't hold the inode lock at that time.
2772 * However, we MUST ref ip before blocking to ensure that
2773 * it isn't ripped out from under us (since we do not
2774 * hold a lock on the vnode).
2776 hammer2_inode_ref(ip);
2777 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
2778 if ((ip->flags & HAMMER2_INODE_MODIFIED) ||
2779 !RB_EMPTY(&vp->v_rbdirty_tree)) {
2780 vfsync(vp, info->waitfor, 1, NULL, NULL);
2782 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2783 RB_EMPTY(&vp->v_rbdirty_tree)) {
2787 hammer2_inode_drop(ip);
2791 info->error = error;
2798 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2805 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2806 struct fid *fhp, struct vnode **vpp)
2813 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2814 int *exflagsp, struct ucred **credanonp)
2820 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2821 * header into the HMP
2823 * XXX read four volhdrs and use the one with the highest TID whos CRC
2828 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2829 * nonexistant locations.
2831 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2835 hammer2_install_volume_header(hammer2_dev_t *hmp)
2837 hammer2_volume_data_t *vd;
2839 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2851 * There are up to 4 copies of the volume header (syncs iterate
2852 * between them so there is no single master). We don't trust the
2853 * volu_size field so we don't know precisely how large the filesystem
2854 * is, so depend on the OS to return an error if we go beyond the
2855 * block device's EOF.
2857 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2858 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2859 HAMMER2_VOLUME_BYTES, &bp);
2866 vd = (struct hammer2_volume_data *) bp->b_data;
2867 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2868 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2874 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2875 /* XXX: Reversed-endianness filesystem */
2876 kprintf("hammer2: reverse-endian filesystem detected");
2882 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2883 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2884 HAMMER2_VOLUME_ICRC0_SIZE);
2885 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2886 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2887 HAMMER2_VOLUME_ICRC1_SIZE);
2888 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2889 kprintf("hammer2 volume header crc "
2890 "mismatch copy #%d %08x/%08x\n",
2897 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2906 hmp->volsync = hmp->voldata;
2908 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2909 kprintf("hammer2: using volume header #%d\n",
2914 kprintf("hammer2: no valid volume headers found!\n");
2920 * This handles hysteresis on regular file flushes. Because the BIOs are
2921 * routed to a thread it is possible for an excessive number to build up
2922 * and cause long front-end stalls long before the runningbuffspace limit
2923 * is hit, so we implement hammer2_flush_pipe to control the
2926 * This is a particular problem when compression is used.
2929 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2931 atomic_add_int(&pmp->count_lwinprog, 1);
2935 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2939 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2940 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2941 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2942 atomic_clear_int(&pmp->count_lwinprog,
2943 HAMMER2_LWINPROG_WAITING);
2944 wakeup(&pmp->count_lwinprog);
2949 hammer2_lwinprog_wait(hammer2_pfs_t *pmp)
2954 lwinprog = pmp->count_lwinprog;
2956 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2958 tsleep_interlock(&pmp->count_lwinprog, 0);
2959 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2960 lwinprog = pmp->count_lwinprog;
2961 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2963 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2968 * Manage excessive memory resource use for chain and related
2972 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
2982 * Atomic check condition and wait. Also do an early speedup of
2983 * the syncer to try to avoid hitting the wait.
2986 waiting = pmp->inmem_dirty_chains;
2988 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2990 limit = pmp->mp->mnt_nvnodelistsize / 10;
2991 if (limit < hammer2_limit_dirty_chains)
2992 limit = hammer2_limit_dirty_chains;
2997 if ((int)(ticks - zzticks) > hz) {
2999 kprintf("count %ld %ld\n", count, limit);
3004 * Block if there are too many dirty chains present, wait
3005 * for the flush to clean some out.
3007 if (count > limit) {
3008 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
3009 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
3011 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
3012 speedup_syncer(pmp->mp);
3013 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
3016 continue; /* loop on success or fail */
3020 * Try to start an early flush before we are forced to block.
3022 if (count > limit * 7 / 10)
3023 speedup_syncer(pmp->mp);
3029 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
3032 atomic_add_int(&pmp->inmem_dirty_chains, 1);
3037 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
3045 waiting = pmp->inmem_dirty_chains;
3047 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
3050 ~HAMMER2_DIRTYCHAIN_WAITING)) {
3055 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
3056 wakeup(&pmp->inmem_dirty_chains);
3063 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
3065 hammer2_chain_t *scan;
3066 hammer2_chain_t *parent;
3070 kprintf("%*.*s...\n", tab, tab, "");
3075 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
3077 chain, chain->bref.type,
3078 chain->bref.key, chain->bref.keybits,
3079 chain->bref.mirror_tid);
3081 kprintf("%*.*s [%08x] (%s) refs=%d",
3084 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
3085 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
3088 parent = chain->parent;
3090 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
3092 parent, parent->flags, parent->refs);
3093 if (RB_EMPTY(&chain->core.rbtree)) {
3097 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
3098 hammer2_dump_chain(scan, tab + 4, countp, 'a');
3099 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
3100 kprintf("%*.*s}(%s)\n", tab, tab, "",
3101 chain->data->ipdata.filename);
3103 kprintf("%*.*s}\n", tab, tab, "");