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(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_dev_t *, struct mount *, char *,
181 struct vnode *, struct ucred *);
182 static int hammer2_recovery(hammer2_dev_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_dev_t *hmp);
198 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
200 static void hammer2_update_pmps(hammer2_dev_t *hmp);
201 static void hammer2_write_thread(void *arg);
203 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
204 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
208 * Functions for compression in threads,
209 * from hammer2_vnops.c
211 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
213 const hammer2_inode_data_t *ripdata,
214 hammer2_cluster_t *cparent,
215 hammer2_key_t lbase, int ioflag, int pblksize,
217 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
219 const hammer2_inode_data_t *ripdata,
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 const hammer2_inode_data_t *ripdata,
227 hammer2_cluster_t *cparent,
229 int ioflag, int pblksize, int *errorp,
231 static int test_block_zeros(const char *buf, size_t bytes);
232 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
234 const hammer2_inode_data_t *ripdata,
235 hammer2_cluster_t *cparent,
238 static void hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp,
239 int ioflag, int pblksize, int *errorp,
243 * HAMMER2 vfs operations.
245 static struct vfsops hammer2_vfsops = {
246 .vfs_init = hammer2_vfs_init,
247 .vfs_uninit = hammer2_vfs_uninit,
248 .vfs_sync = hammer2_vfs_sync,
249 .vfs_mount = hammer2_vfs_mount,
250 .vfs_unmount = hammer2_vfs_unmount,
251 .vfs_root = hammer2_vfs_root,
252 .vfs_statfs = hammer2_vfs_statfs,
253 .vfs_statvfs = hammer2_vfs_statvfs,
254 .vfs_vget = hammer2_vfs_vget,
255 .vfs_vptofh = hammer2_vfs_vptofh,
256 .vfs_fhtovp = hammer2_vfs_fhtovp,
257 .vfs_checkexp = hammer2_vfs_checkexp
260 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
262 VFS_SET(hammer2_vfsops, hammer2, 0);
263 MODULE_VERSION(hammer2, 1);
267 hammer2_vfs_init(struct vfsconf *conf)
269 static struct objcache_malloc_args margs_read;
270 static struct objcache_malloc_args margs_write;
276 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
278 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
280 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
284 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
286 margs_read.objsize = 65536;
287 margs_read.mtype = D_BUFFER;
289 margs_write.objsize = 32768;
290 margs_write.mtype = C_BUFFER;
292 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
293 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
294 objcache_malloc_free, &margs_read);
295 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
296 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
297 objcache_malloc_free, &margs_write);
299 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
300 TAILQ_INIT(&hammer2_mntlist);
301 TAILQ_INIT(&hammer2_pfslist);
303 hammer2_limit_dirty_chains = desiredvnodes / 10;
305 hammer2_trans_manage_init();
312 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
314 objcache_destroy(cache_buffer_read);
315 objcache_destroy(cache_buffer_write);
320 * Core PFS allocator. Used to allocate the pmp structure for PFS cluster
321 * mounts and the spmp structure for media (hmp) structures.
326 hammer2_pfsalloc(hammer2_cluster_t *cluster,
327 const hammer2_inode_data_t *ripdata,
328 hammer2_tid_t alloc_tid)
330 hammer2_chain_t *rchain;
336 * Locate or create the PFS based on the cluster id. If ripdata
337 * is NULL this is a spmp which is unique and is always allocated.
340 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
341 if (bcmp(&pmp->pfs_clid, &ripdata->pfs_clid,
342 sizeof(pmp->pfs_clid)) == 0) {
351 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
352 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
353 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
354 lockinit(&pmp->lock, "pfslk", 0, 0);
355 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
356 RB_INIT(&pmp->inum_tree);
357 TAILQ_INIT(&pmp->unlinkq);
358 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
360 /* our first media transaction id */
361 pmp->alloc_tid = alloc_tid + 1;
362 pmp->flush_tid = pmp->alloc_tid;
364 pmp->inode_tid = ripdata->pfs_inum + 1;
365 pmp->pfs_clid = ripdata->pfs_clid;
367 hammer2_mtx_init(&pmp->wthread_mtx, "h2wthr");
368 bioq_init(&pmp->wthread_bioq);
369 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
373 * Create the PFS's root inode.
375 if (pmp->iroot == NULL) {
376 pmp->iroot = hammer2_inode_get(pmp, NULL, NULL);
377 hammer2_inode_ref(pmp->iroot);
378 hammer2_inode_unlock_ex(pmp->iroot, NULL);
382 * Create a primary synchronizer thread for the PFS if necessary.
383 * Single-node masters (including snapshots) have nothing to
384 * synchronize and do not require this thread.
386 * Multi-node masters or any number of soft masters, slaves, copy,
387 * or other PFS types need the thread.
389 if (cluster && ripdata &&
390 (ripdata->pfs_type != HAMMER2_PFSTYPE_MASTER ||
391 ripdata->pfs_nmasters > 1) &&
392 pmp->primary_thr.td == NULL) {
393 hammer2_syncthr_create(&pmp->primary_thr, pmp,
394 hammer2_syncthr_primary);
398 * Update nmasters from any PFS which is part of the cluster.
399 * It is possible that this will result in a value which is too
400 * high. MASTER PFSs are authoritative for pfs_nmasters and will
401 * override this value later on.
403 if (ripdata && pmp->pfs_nmasters < ripdata->pfs_nmasters) {
404 pmp->pfs_nmasters = ripdata->pfs_nmasters;
408 * When a cluster is passed in we must add the cluster's chains
409 * to the PFS's root inode and update pmp->pfs_types[].
411 * At the moment empty spots can develop due to removals or failures.
412 * Ultimately we want to re-fill these spots. XXX
415 hammer2_inode_ref(pmp->iroot);
416 hammer2_mtx_ex(&pmp->iroot->lock);
417 j = pmp->iroot->cluster.nchains;
419 kprintf("add PFS to pmp %p[%d]\n", pmp, j);
421 for (i = 0; i < cluster->nchains; ++i) {
422 if (j == HAMMER2_MAXCLUSTER)
424 rchain = cluster->array[i].chain;
425 KKASSERT(rchain->pmp == NULL);
427 hammer2_chain_ref(rchain);
428 pmp->iroot->cluster.array[j].chain = rchain;
429 pmp->pfs_types[j] = ripdata->pfs_type;
432 * May have to fixup dirty chain tracking. Previous
433 * pmp was NULL so nothing to undo.
435 if (rchain->flags & HAMMER2_CHAIN_MODIFIED)
436 hammer2_pfs_memory_inc(pmp);
439 pmp->iroot->cluster.nchains = j;
440 hammer2_mtx_unlock(&pmp->iroot->lock);
441 hammer2_inode_drop(pmp->iroot);
443 if (i != cluster->nchains) {
444 kprintf("hammer2_mount: cluster full!\n");
445 /* XXX fatal error? */
453 * Destroy a PFS, typically only occurs after the last mount on a device
457 hammer2_pfsfree(hammer2_pfs_t *pmp)
460 * Cleanup our reference on iroot. iroot is (should) not be needed
463 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
465 hammer2_syncthr_delete(&pmp->primary_thr);
468 #if REPORT_REFS_ERRORS
469 if (pmp->iroot->refs != 1)
470 kprintf("PMP->IROOT %p REFS WRONG %d\n",
471 pmp->iroot, pmp->iroot->refs);
473 KKASSERT(pmp->iroot->refs == 1);
475 /* ref for pmp->iroot */
476 hammer2_inode_drop(pmp->iroot);
480 kmalloc_destroy(&pmp->mmsg);
481 kmalloc_destroy(&pmp->minode);
483 kfree(pmp, M_HAMMER2);
487 * Remove all references to hmp from the pfs list. Any PFS which becomes
488 * empty is terminated and freed.
493 hammer2_pfsfree_scan(hammer2_dev_t *hmp)
496 hammer2_cluster_t *cluster;
497 hammer2_chain_t *rchain;
502 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
503 if (pmp->iroot == NULL)
505 if (hmp->spmp == pmp) {
506 kprintf("unmount hmp %p remove spmp %p\n",
512 * Determine if this PFS is affected. If it is we must
513 * freeze all management threads and lock its iroot.
515 * Freezing a management thread forces it idle, operations
516 * in-progress will be aborted and it will have to start
517 * over again when unfrozen, or exit if told to exit.
519 cluster = &pmp->iroot->cluster;
520 for (i = 0; i < cluster->nchains; ++i) {
521 rchain = cluster->array[i].chain;
522 if (rchain == NULL || rchain->hmp != hmp)
526 if (i != cluster->nchains) {
527 hammer2_syncthr_freeze(&pmp->primary_thr);
530 * Lock the inode and clean out matching chains.
531 * Note that we cannot use hammer2_inode_lock_*()
532 * here because that would attempt to validate the
533 * cluster that we are in the middle of ripping
536 * WARNING! We are working directly on the inodes
539 hammer2_mtx_ex(&pmp->iroot->lock);
542 * Remove the chain from matching elements of the PFS.
544 for (i = 0; i < cluster->nchains; ++i) {
545 rchain = cluster->array[i].chain;
546 if (rchain == NULL || rchain->hmp != hmp)
549 cluster->array[i].chain = NULL;
550 pmp->pfs_types[i] = 0;
551 hammer2_chain_drop(rchain);
554 if (cluster->focus == rchain)
555 cluster->focus = NULL;
557 hammer2_mtx_unlock(&pmp->iroot->lock);
558 didfreeze = 1; /* remaster, unfreeze down below */
564 * Cleanup trailing chains. Do not reorder chains (for now).
565 * XXX might remove more than we intended.
568 if (cluster->array[i - 1].chain)
572 cluster->nchains = i;
575 * If the PMP has no elements remaining we can destroy it.
576 * (this will transition management threads from frozen->exit).
578 if (cluster->nchains == 0) {
579 kprintf("unmount hmp %p last ref to PMP=%p\n",
581 hammer2_pfsfree(pmp);
586 * If elements still remain we need to set the REMASTER
587 * flag and unfreeze it.
590 hammer2_syncthr_remaster(&pmp->primary_thr);
591 hammer2_syncthr_unfreeze(&pmp->primary_thr);
597 * Mount or remount HAMMER2 fileystem from physical media
600 * mp mount point structure
606 * mp mount point structure
607 * path path to mount point
608 * data pointer to argument structure in user space
609 * volume volume path (device@LABEL form)
610 * hflags user mount flags
611 * cred user credentials
618 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
621 struct hammer2_mount_info info;
625 hammer2_key_t key_next;
626 hammer2_key_t key_dummy;
629 struct nlookupdata nd;
630 hammer2_chain_t *parent;
631 hammer2_cluster_t *cluster;
632 hammer2_cluster_t *cparent;
633 const hammer2_inode_data_t *ripdata;
634 hammer2_blockref_t bref;
636 char devstr[MNAMELEN];
653 kprintf("hammer2_mount\n");
659 bzero(&info, sizeof(info));
660 info.cluster_fd = -1;
664 * Non-root mount or updating a mount
666 error = copyin(data, &info, sizeof(info));
670 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
674 /* Extract device and label */
676 label = strchr(devstr, '@');
678 ((label + 1) - dev) > done) {
686 if (mp->mnt_flag & MNT_UPDATE) {
688 * Update mount. Note that pmp->iroot->cluster is
689 * an inode-embedded cluster and thus cannot be
692 * XXX HAMMER2 needs to implement NFS export via
696 cluster = &pmp->iroot->cluster;
697 for (i = 0; i < cluster->nchains; ++i) {
698 hmp = cluster->array[i].chain->hmp;
700 error = hammer2_remount(hmp, mp, path,
705 /*hammer2_inode_install_hidden(pmp);*/
714 * Lookup name and verify it refers to a block device.
716 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
718 error = nlookup(&nd);
720 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
724 if (vn_isdisk(devvp, &error))
725 error = vfs_mountedon(devvp);
729 * Determine if the device has already been mounted. After this
730 * check hmp will be non-NULL if we are doing the second or more
731 * hammer2 mounts from the same device.
733 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
734 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
735 if (hmp->devvp == devvp)
740 * Open the device if this isn't a secondary mount and construct
741 * the H2 device mount (hmp).
744 hammer2_chain_t *schain;
747 if (error == 0 && vcount(devvp) > 0)
751 * Now open the device
754 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
755 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
756 error = vinvalbuf(devvp, V_SAVE, 0, 0);
758 error = VOP_OPEN(devvp,
759 ronly ? FREAD : FREAD | FWRITE,
764 if (error && devvp) {
769 lockmgr(&hammer2_mntlk, LK_RELEASE);
772 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
775 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
776 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
777 RB_INIT(&hmp->iotree);
778 spin_init(&hmp->io_spin, "hm2mount_io");
779 spin_init(&hmp->list_spin, "hm2mount_list");
780 TAILQ_INIT(&hmp->flushq);
782 lockinit(&hmp->vollk, "h2vol", 0, 0);
785 * vchain setup. vchain.data is embedded.
786 * vchain.refs is initialized and will never drop to 0.
788 * NOTE! voldata is not yet loaded.
790 hmp->vchain.hmp = hmp;
791 hmp->vchain.refs = 1;
792 hmp->vchain.data = (void *)&hmp->voldata;
793 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
794 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
795 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
797 hammer2_chain_core_init(&hmp->vchain);
798 /* hmp->vchain.u.xxx is left NULL */
801 * fchain setup. fchain.data is embedded.
802 * fchain.refs is initialized and will never drop to 0.
804 * The data is not used but needs to be initialized to
805 * pass assertion muster. We use this chain primarily
806 * as a placeholder for the freemap's top-level RBTREE
807 * so it does not interfere with the volume's topology
810 hmp->fchain.hmp = hmp;
811 hmp->fchain.refs = 1;
812 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
813 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
814 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
815 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
816 hmp->fchain.bref.methods =
817 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
818 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
820 hammer2_chain_core_init(&hmp->fchain);
821 /* hmp->fchain.u.xxx is left NULL */
824 * Install the volume header and initialize fields from
827 error = hammer2_install_volume_header(hmp);
829 hammer2_unmount_helper(mp, NULL, hmp);
830 lockmgr(&hammer2_mntlk, LK_RELEASE);
831 hammer2_vfs_unmount(mp, MNT_FORCE);
836 * Really important to get these right or flush will get
839 hmp->spmp = hammer2_pfsalloc(NULL, NULL,
840 hmp->voldata.mirror_tid);
841 kprintf("alloc spmp %p tid %016jx\n",
842 hmp->spmp, hmp->voldata.mirror_tid);
847 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
848 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
849 hmp->vchain.pmp = spmp;
850 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
851 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
852 hmp->fchain.pmp = spmp;
855 * First locate the super-root inode, which is key 0
856 * relative to the volume header's blockset.
858 * Then locate the root inode by scanning the directory keyspace
859 * represented by the label.
861 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
862 schain = hammer2_chain_lookup(&parent, &key_dummy,
863 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
865 hammer2_chain_lookup_done(parent);
866 if (schain == NULL) {
867 kprintf("hammer2_mount: invalid super-root\n");
868 hammer2_unmount_helper(mp, NULL, hmp);
869 lockmgr(&hammer2_mntlk, LK_RELEASE);
870 hammer2_vfs_unmount(mp, MNT_FORCE);
875 * Sanity-check schain's pmp and finish initialization.
876 * Any chain belonging to the super-root topology should
877 * have a NULL pmp (not even set to spmp).
879 ripdata = &hammer2_chain_rdata(schain)->ipdata;
880 KKASSERT(schain->pmp == NULL);
881 spmp->pfs_clid = ripdata->pfs_clid;
884 * Replace the dummy spmp->iroot with a real one. It's
885 * easier to just do a wholesale replacement than to try
886 * to update the chain and fixup the iroot fields.
888 * The returned inode is locked with the supplied cluster.
890 cluster = hammer2_cluster_from_chain(schain);
891 hammer2_inode_drop(spmp->iroot);
893 spmp->iroot = hammer2_inode_get(spmp, NULL, cluster);
894 spmp->spmp_hmp = hmp;
895 spmp->pfs_types[0] = ripdata->pfs_type;
896 hammer2_inode_ref(spmp->iroot);
897 hammer2_inode_unlock_ex(spmp->iroot, cluster);
899 /* leave spmp->iroot with one ref */
901 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
902 error = hammer2_recovery(hmp);
903 /* XXX do something with error */
905 hammer2_update_pmps(hmp);
906 hammer2_iocom_init(hmp);
909 * Ref the cluster management messaging descriptor. The mount
910 * program deals with the other end of the communications pipe.
912 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
914 hammer2_cluster_reconnect(hmp, fp);
916 kprintf("hammer2_mount: bad cluster_fd!\n");
923 * Lookup the mount point under the media-localized super-root.
924 * Scanning hammer2_pfslist doesn't help us because it represents
925 * PFS cluster ids which can aggregate several named PFSs together.
927 * cluster->pmp will incorrectly point to spmp and must be fixed
930 cparent = hammer2_inode_lock_ex(spmp->iroot);
931 lhc = hammer2_dirhash(label, strlen(label));
932 cluster = hammer2_cluster_lookup(cparent, &key_next,
933 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
936 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
938 hammer2_cluster_rdata(cluster)->ipdata.filename) == 0) {
941 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
943 lhc + HAMMER2_DIRHASH_LOMASK, 0);
945 hammer2_inode_unlock_ex(spmp->iroot, cparent);
948 * PFS could not be found?
950 if (cluster == NULL) {
951 kprintf("hammer2_mount: PFS label not found\n");
952 hammer2_unmount_helper(mp, NULL, hmp);
953 lockmgr(&hammer2_mntlk, LK_RELEASE);
954 hammer2_vfs_unmount(mp, MNT_FORCE);
960 * Acquire the pmp structure (it should have already been allocated
961 * via hammer2_update_pmps() so do not pass cluster in to add to
964 * Check if the cluster has already been mounted. A cluster can
965 * only be mounted once, use null mounts to mount additional copies.
967 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
968 hammer2_cluster_bref(cluster, &bref);
969 pmp = hammer2_pfsalloc(NULL, ripdata, bref.mirror_tid);
970 hammer2_cluster_unlock(cluster);
973 kprintf("hammer2_mount: PFS already mounted!\n");
974 hammer2_unmount_helper(mp, NULL, hmp);
975 lockmgr(&hammer2_mntlk, LK_RELEASE);
976 hammer2_vfs_unmount(mp, MNT_FORCE);
984 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
986 mp->mnt_flag = MNT_LOCAL;
987 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
988 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
991 * required mount structure initializations
993 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
994 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
996 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
997 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1002 mp->mnt_iosize_max = MAXPHYS;
1005 * Connect up mount pointers.
1007 hammer2_mount_helper(mp, pmp);
1009 lockmgr(&hammer2_mntlk, LK_RELEASE);
1012 * A mounted PFS needs a write thread for logical buffers and
1013 * a hidden directory for deletions of open files. These features
1014 * are not used by unmounted PFSs.
1016 * The logical file buffer bio write thread handles things like
1017 * physical block assignment and compression.
1019 pmp->wthread_destroy = 0;
1020 lwkt_create(hammer2_write_thread, pmp,
1021 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
1024 * With the cluster operational install ihidden.
1025 * (only applicable to pfs mounts, not applicable to spmp)
1027 hammer2_inode_install_hidden(pmp);
1033 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1034 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1035 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1037 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
1038 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1039 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1040 copyinstr(path, mp->mnt_stat.f_mntonname,
1041 sizeof(mp->mnt_stat.f_mntonname) - 1,
1045 * Initial statfs to prime mnt_stat.
1047 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1053 * Scan PFSs under the super-root and create hammer2_pfs structures.
1057 hammer2_update_pmps(hammer2_dev_t *hmp)
1059 const hammer2_inode_data_t *ripdata;
1060 hammer2_cluster_t *cparent;
1061 hammer2_cluster_t *cluster;
1062 hammer2_blockref_t bref;
1063 hammer2_pfs_t *spmp;
1065 hammer2_key_t key_next;
1068 * Lookup mount point under the media-localized super-root.
1070 * cluster->pmp will incorrectly point to spmp and must be fixed
1074 cparent = hammer2_inode_lock_ex(spmp->iroot);
1075 cluster = hammer2_cluster_lookup(cparent, &key_next,
1080 if (hammer2_cluster_type(cluster) != HAMMER2_BREF_TYPE_INODE)
1082 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
1083 hammer2_cluster_bref(cluster, &bref);
1084 kprintf("ADD LOCAL PFS: %s\n", ripdata->filename);
1086 pmp = hammer2_pfsalloc(cluster, ripdata, bref.mirror_tid);
1087 cluster = hammer2_cluster_next(cparent, cluster,
1093 hammer2_inode_unlock_ex(spmp->iroot, cparent);
1097 * Handle bioq for strategy write
1101 hammer2_write_thread(void *arg)
1106 hammer2_trans_t trans;
1108 hammer2_inode_t *ip;
1109 hammer2_cluster_t *cparent;
1110 const hammer2_inode_data_t *ripdata;
1111 hammer2_key_t lbase;
1118 hammer2_mtx_ex(&pmp->wthread_mtx);
1119 while (pmp->wthread_destroy == 0) {
1120 if (bioq_first(&pmp->wthread_bioq) == NULL) {
1121 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
1126 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
1128 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
1130 * dummy bio for synchronization. The transaction
1131 * must be reinitialized.
1133 if (bio->bio_buf == NULL) {
1134 bio->bio_flags |= BIO_DONE;
1136 hammer2_trans_done(&trans);
1137 hammer2_trans_init(&trans, pmp,
1138 HAMMER2_TRANS_BUFCACHE);
1143 * else normal bio processing
1145 hammer2_mtx_unlock(&pmp->wthread_mtx);
1147 hammer2_lwinprog_drop(pmp);
1155 * Inode is modified, flush size and mtime changes
1156 * to ensure that the file size remains consistent
1157 * with the buffers being flushed.
1159 * NOTE: The inode_fsync() call only flushes the
1160 * inode's meta-data state, it doesn't try
1161 * to flush underlying buffers or chains.
1163 * NOTE: hammer2_write_file_core() may indirectly
1164 * modify and modsync the inode.
1166 cparent = hammer2_inode_lock_ex(ip);
1167 if (ip->flags & (HAMMER2_INODE_RESIZED |
1168 HAMMER2_INODE_MTIME)) {
1169 hammer2_inode_fsync(&trans, ip, cparent);
1171 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
1172 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
1174 pblksize = hammer2_calc_physical(ip, ripdata, lbase);
1175 hammer2_write_file_core(bp, &trans, ip, ripdata,
1179 /* ripdata can be invalid after call */
1180 hammer2_inode_unlock_ex(ip, cparent);
1182 kprintf("hammer2: error in buffer write\n");
1183 bp->b_flags |= B_ERROR;
1187 hammer2_mtx_ex(&pmp->wthread_mtx);
1189 hammer2_trans_done(&trans);
1191 pmp->wthread_destroy = -1;
1192 wakeup(&pmp->wthread_destroy);
1194 hammer2_mtx_unlock(&pmp->wthread_mtx);
1198 hammer2_bioq_sync(hammer2_pfs_t *pmp)
1200 struct bio sync_bio;
1202 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
1203 hammer2_mtx_ex(&pmp->wthread_mtx);
1204 if (pmp->wthread_destroy == 0 &&
1205 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
1206 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
1207 while ((sync_bio.bio_flags & BIO_DONE) == 0)
1208 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
1210 hammer2_mtx_unlock(&pmp->wthread_mtx);
1214 * Return a chain suitable for I/O, creating the chain if necessary
1215 * and assigning its physical block.
1217 * cparent can wind up being anything.
1221 hammer2_assign_physical(hammer2_trans_t *trans,
1222 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1223 hammer2_key_t lbase, int pblksize, int *errorp)
1225 hammer2_cluster_t *cluster;
1226 hammer2_cluster_t *dparent;
1227 hammer2_key_t key_dummy;
1228 int pradix = hammer2_getradix(pblksize);
1231 * Locate the chain associated with lbase, return a locked chain.
1232 * However, do not instantiate any data reference (which utilizes a
1233 * device buffer) because we will be using direct IO via the
1234 * logical buffer cache buffer.
1237 KKASSERT(pblksize >= HAMMER2_ALLOC_MIN);
1239 dparent = hammer2_cluster_lookup_init(cparent, 0);
1240 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
1242 HAMMER2_LOOKUP_NODATA);
1244 if (cluster == NULL) {
1246 * We found a hole, create a new chain entry.
1248 * NOTE: DATA chains are created without device backing
1249 * store (nor do we want any).
1251 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
1252 lbase, HAMMER2_PBUFRADIX,
1253 HAMMER2_BREF_TYPE_DATA,
1255 if (cluster == NULL) {
1256 hammer2_cluster_lookup_done(dparent);
1257 panic("hammer2_cluster_create: par=%p error=%d\n",
1258 dparent->focus, *errorp);
1261 /*ip->delta_dcount += pblksize;*/
1263 switch (hammer2_cluster_type(cluster)) {
1264 case HAMMER2_BREF_TYPE_INODE:
1266 * The data is embedded in the inode. The
1267 * caller is responsible for marking the inode
1268 * modified and copying the data to the embedded
1272 case HAMMER2_BREF_TYPE_DATA:
1273 if (hammer2_cluster_need_resize(cluster, pblksize)) {
1274 hammer2_cluster_resize(trans, ip,
1277 HAMMER2_MODIFY_OPTDATA);
1281 * DATA buffers must be marked modified whether the
1282 * data is in a logical buffer or not. We also have
1283 * to make this call to fixup the chain data pointers
1284 * after resizing in case this is an encrypted or
1285 * compressed buffer.
1287 hammer2_cluster_modify(trans, cluster,
1288 HAMMER2_MODIFY_OPTDATA);
1291 panic("hammer2_assign_physical: bad type");
1298 * Cleanup. If cluster wound up being the inode itself, i.e.
1299 * the DIRECTDATA case for offset 0, then we need to update cparent.
1300 * The caller expects cparent to not become stale.
1302 hammer2_cluster_lookup_done(dparent);
1303 /* dparent = NULL; safety */
1308 * bio queued from hammer2_vnops.c.
1310 * The core write function which determines which path to take
1311 * depending on compression settings. We also have to locate the
1312 * related clusters so we can calculate and set the check data for
1317 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
1318 hammer2_inode_t *ip,
1319 const hammer2_inode_data_t *ripdata,
1320 hammer2_cluster_t *cparent,
1321 hammer2_key_t lbase, int ioflag, int pblksize,
1324 hammer2_cluster_t *cluster;
1326 switch(HAMMER2_DEC_ALGO(ripdata->comp_algo)) {
1327 case HAMMER2_COMP_NONE:
1329 * We have to assign physical storage to the buffer
1330 * we intend to dirty or write now to avoid deadlocks
1331 * in the strategy code later.
1333 * This can return NOOFFSET for inode-embedded data.
1334 * The strategy code will take care of it in that case.
1336 cluster = hammer2_assign_physical(trans, ip, cparent,
1339 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1340 ripdata->check_algo);
1341 /* ripdata can become invalid */
1343 hammer2_cluster_unlock(cluster);
1345 case HAMMER2_COMP_AUTOZERO:
1347 * Check for zero-fill only
1349 hammer2_zero_check_and_write(bp, trans, ip,
1350 ripdata, cparent, lbase,
1351 ioflag, pblksize, errorp,
1352 ripdata->check_algo);
1354 case HAMMER2_COMP_LZ4:
1355 case HAMMER2_COMP_ZLIB:
1358 * Check for zero-fill and attempt compression.
1360 hammer2_compress_and_write(bp, trans, ip,
1365 ripdata->check_algo);
1371 * Generic function that will perform the compression in compression
1372 * write path. The compression algorithm is determined by the settings
1373 * obtained from inode.
1377 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
1378 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1379 hammer2_cluster_t *cparent,
1380 hammer2_key_t lbase, int ioflag, int pblksize,
1381 int *errorp, int comp_algo, int check_algo)
1383 hammer2_cluster_t *cluster;
1384 hammer2_chain_t *chain;
1386 int comp_block_size;
1390 if (test_block_zeros(bp->b_data, pblksize)) {
1391 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1398 KKASSERT(pblksize / 2 <= 32768);
1400 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1401 z_stream strm_compress;
1405 switch(HAMMER2_DEC_ALGO(comp_algo)) {
1406 case HAMMER2_COMP_LZ4:
1407 comp_buffer = objcache_get(cache_buffer_write,
1409 comp_size = LZ4_compress_limitedOutput(
1411 &comp_buffer[sizeof(int)],
1413 pblksize / 2 - sizeof(int));
1415 * We need to prefix with the size, LZ4
1416 * doesn't do it for us. Add the related
1419 *(int *)comp_buffer = comp_size;
1421 comp_size += sizeof(int);
1423 case HAMMER2_COMP_ZLIB:
1424 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1425 if (comp_level == 0)
1426 comp_level = 6; /* default zlib compression */
1427 else if (comp_level < 6)
1429 else if (comp_level > 9)
1431 ret = deflateInit(&strm_compress, comp_level);
1433 kprintf("HAMMER2 ZLIB: fatal error "
1434 "on deflateInit.\n");
1437 comp_buffer = objcache_get(cache_buffer_write,
1439 strm_compress.next_in = bp->b_data;
1440 strm_compress.avail_in = pblksize;
1441 strm_compress.next_out = comp_buffer;
1442 strm_compress.avail_out = pblksize / 2;
1443 ret = deflate(&strm_compress, Z_FINISH);
1444 if (ret == Z_STREAM_END) {
1445 comp_size = pblksize / 2 -
1446 strm_compress.avail_out;
1450 ret = deflateEnd(&strm_compress);
1453 kprintf("Error: Unknown compression method.\n");
1454 kprintf("Comp_method = %d.\n", comp_algo);
1459 if (comp_size == 0) {
1461 * compression failed or turned off
1463 comp_block_size = pblksize; /* safety */
1464 if (++ip->comp_heuristic > 128)
1465 ip->comp_heuristic = 8;
1468 * compression succeeded
1470 ip->comp_heuristic = 0;
1471 if (comp_size <= 1024) {
1472 comp_block_size = 1024;
1473 } else if (comp_size <= 2048) {
1474 comp_block_size = 2048;
1475 } else if (comp_size <= 4096) {
1476 comp_block_size = 4096;
1477 } else if (comp_size <= 8192) {
1478 comp_block_size = 8192;
1479 } else if (comp_size <= 16384) {
1480 comp_block_size = 16384;
1481 } else if (comp_size <= 32768) {
1482 comp_block_size = 32768;
1484 panic("hammer2: WRITE PATH: "
1485 "Weird comp_size value.");
1487 comp_block_size = pblksize;
1491 cluster = hammer2_assign_physical(trans, ip, cparent,
1492 lbase, comp_block_size,
1497 kprintf("WRITE PATH: An error occurred while "
1498 "assigning physical space.\n");
1499 KKASSERT(cluster == NULL);
1503 if (cluster->ddflag) {
1504 hammer2_inode_data_t *wipdata;
1506 wipdata = hammer2_cluster_modify_ip(trans, ip, cluster, 0);
1507 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1508 KKASSERT(bp->b_loffset == 0);
1509 bcopy(bp->b_data, wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1510 hammer2_cluster_modsync(cluster);
1512 for (i = 0; i < cluster->nchains; ++i) {
1518 chain = cluster->array[i].chain; /* XXX */
1519 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1521 switch(chain->bref.type) {
1522 case HAMMER2_BREF_TYPE_INODE:
1523 panic("hammer2_write_bp: unexpected inode\n");
1525 case HAMMER2_BREF_TYPE_DATA:
1527 * Optimize out the read-before-write
1530 *errorp = hammer2_io_newnz(chain->hmp,
1531 chain->bref.data_off,
1535 hammer2_io_brelse(&dio);
1536 kprintf("hammer2: WRITE PATH: "
1537 "dbp bread error\n");
1540 bdata = hammer2_io_data(dio, chain->bref.data_off);
1543 * When loading the block make sure we don't
1544 * leave garbage after the compressed data.
1547 chain->bref.methods =
1548 HAMMER2_ENC_COMP(comp_algo) +
1549 HAMMER2_ENC_CHECK(check_algo);
1550 bcopy(comp_buffer, bdata, comp_size);
1551 if (comp_size != comp_block_size) {
1552 bzero(bdata + comp_size,
1553 comp_block_size - comp_size);
1556 chain->bref.methods =
1558 HAMMER2_COMP_NONE) +
1559 HAMMER2_ENC_CHECK(check_algo);
1560 bcopy(bp->b_data, bdata, pblksize);
1564 * The flush code doesn't calculate check codes for
1565 * file data (doing so can result in excessive I/O),
1568 hammer2_chain_setcheck(chain, bdata);
1571 * Device buffer is now valid, chain is no longer in
1572 * the initial state.
1574 * (No blockref table worries with file data)
1576 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1578 /* Now write the related bdp. */
1579 if (ioflag & IO_SYNC) {
1581 * Synchronous I/O requested.
1583 hammer2_io_bwrite(&dio);
1585 } else if ((ioflag & IO_DIRECT) &&
1586 loff + n == pblksize) {
1587 hammer2_io_bdwrite(&dio);
1589 } else if (ioflag & IO_ASYNC) {
1590 hammer2_io_bawrite(&dio);
1592 hammer2_io_bdwrite(&dio);
1596 panic("hammer2_write_bp: bad chain type %d\n",
1604 hammer2_cluster_unlock(cluster);
1606 objcache_put(cache_buffer_write, comp_buffer);
1610 * Function that performs zero-checking and writing without compression,
1611 * it corresponds to default zero-checking path.
1615 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1616 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1617 hammer2_cluster_t *cparent,
1618 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp,
1621 hammer2_cluster_t *cluster;
1623 if (test_block_zeros(bp->b_data, pblksize)) {
1624 zero_write(bp, trans, ip, ripdata, cparent, lbase, errorp);
1625 /* ripdata can become invalid */
1627 cluster = hammer2_assign_physical(trans, ip, cparent,
1628 lbase, pblksize, errorp);
1629 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp,
1631 /* ripdata can become invalid */
1633 hammer2_cluster_unlock(cluster);
1638 * A function to test whether a block of data contains only zeros,
1639 * returns TRUE (non-zero) if the block is all zeros.
1643 test_block_zeros(const char *buf, size_t bytes)
1647 for (i = 0; i < bytes; i += sizeof(long)) {
1648 if (*(const long *)(buf + i) != 0)
1655 * Function to "write" a block that contains only zeros.
1659 zero_write(struct buf *bp, hammer2_trans_t *trans,
1660 hammer2_inode_t *ip, const hammer2_inode_data_t *ripdata,
1661 hammer2_cluster_t *cparent,
1662 hammer2_key_t lbase, int *errorp __unused)
1664 hammer2_cluster_t *cluster;
1665 hammer2_key_t key_dummy;
1667 cparent = hammer2_cluster_lookup_init(cparent, 0);
1668 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1669 HAMMER2_LOOKUP_NODATA);
1671 if (cluster->ddflag) {
1672 hammer2_inode_data_t *wipdata;
1674 wipdata = hammer2_cluster_modify_ip(trans, ip,
1676 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1677 KKASSERT(bp->b_loffset == 0);
1678 bzero(wipdata->u.data, HAMMER2_EMBEDDED_BYTES);
1679 hammer2_cluster_modsync(cluster);
1681 hammer2_cluster_delete(trans, cparent, cluster,
1682 HAMMER2_DELETE_PERMANENT);
1684 hammer2_cluster_unlock(cluster);
1686 hammer2_cluster_lookup_done(cparent);
1690 * Function to write the data as it is, without performing any sort of
1691 * compression. This function is used in path without compression and
1692 * default zero-checking path.
1696 hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1697 int pblksize, int *errorp, int check_algo)
1699 hammer2_chain_t *chain;
1700 hammer2_inode_data_t *wipdata;
1706 error = 0; /* XXX TODO below */
1708 for (i = 0; i < cluster->nchains; ++i) {
1709 chain = cluster->array[i].chain; /* XXX */
1710 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1712 switch(chain->bref.type) {
1713 case HAMMER2_BREF_TYPE_INODE:
1714 wipdata = &hammer2_chain_wdata(chain)->ipdata;
1715 KKASSERT(wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA);
1716 KKASSERT(bp->b_loffset == 0);
1717 bcopy(bp->b_data, wipdata->u.data,
1718 HAMMER2_EMBEDDED_BYTES);
1721 case HAMMER2_BREF_TYPE_DATA:
1722 error = hammer2_io_newnz(chain->hmp,
1723 chain->bref.data_off,
1724 chain->bytes, &dio);
1726 hammer2_io_bqrelse(&dio);
1727 kprintf("hammer2: WRITE PATH: "
1728 "dbp bread error\n");
1731 bdata = hammer2_io_data(dio, chain->bref.data_off);
1733 chain->bref.methods = HAMMER2_ENC_COMP(
1734 HAMMER2_COMP_NONE) +
1735 HAMMER2_ENC_CHECK(check_algo);
1736 bcopy(bp->b_data, bdata, chain->bytes);
1739 * The flush code doesn't calculate check codes for
1740 * file data (doing so can result in excessive I/O),
1743 hammer2_chain_setcheck(chain, bdata);
1746 * Device buffer is now valid, chain is no longer in
1747 * the initial state.
1749 * (No blockref table worries with file data)
1751 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1753 if (ioflag & IO_SYNC) {
1755 * Synchronous I/O requested.
1757 hammer2_io_bwrite(&dio);
1759 } else if ((ioflag & IO_DIRECT) &&
1760 loff + n == pblksize) {
1761 hammer2_io_bdwrite(&dio);
1763 } else if (ioflag & IO_ASYNC) {
1764 hammer2_io_bawrite(&dio);
1766 hammer2_io_bdwrite(&dio);
1770 panic("hammer2_write_bp: bad chain type %d\n",
1776 KKASSERT(error == 0); /* XXX TODO */
1783 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path,
1784 struct vnode *devvp, struct ucred *cred)
1788 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1789 error = hammer2_recovery(hmp);
1798 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1809 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1812 * If mount initialization proceeded far enough we must flush
1813 * its vnodes and sync the underlying mount points. Three syncs
1814 * are required to fully flush the filesystem (freemap updates lag
1815 * by one flush, and one extra for safety).
1817 if (mntflags & MNT_FORCE)
1822 error = vflush(mp, 0, flags);
1825 hammer2_vfs_sync(mp, MNT_WAIT);
1826 hammer2_vfs_sync(mp, MNT_WAIT);
1827 hammer2_vfs_sync(mp, MNT_WAIT);
1830 if (pmp->wthread_td) {
1831 hammer2_mtx_ex(&pmp->wthread_mtx);
1832 pmp->wthread_destroy = 1;
1833 wakeup(&pmp->wthread_bioq);
1834 while (pmp->wthread_destroy != -1) {
1835 mtxsleep(&pmp->wthread_destroy,
1836 &pmp->wthread_mtx, 0,
1839 hammer2_mtx_unlock(&pmp->wthread_mtx);
1840 pmp->wthread_td = NULL;
1844 * Cleanup our reference on ihidden.
1847 hammer2_inode_drop(pmp->ihidden);
1848 pmp->ihidden = NULL;
1851 hammer2_unmount_helper(mp, pmp, NULL);
1855 lockmgr(&hammer2_mntlk, LK_RELEASE);
1861 * Mount helper, hook the system mount into our PFS.
1862 * The mount lock is held.
1864 * We must bump the pmp_count on related devices for any
1869 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1871 hammer2_cluster_t *cluster;
1872 hammer2_chain_t *rchain;
1875 mp->mnt_data = (qaddr_t)pmp;
1878 cluster = &pmp->iroot->cluster;
1879 for (i = 0; i < cluster->nchains; ++i) {
1880 rchain = cluster->array[i].chain;
1883 ++rchain->hmp->pmp_count;
1884 kprintf("hammer2_mount hmp=%p ++pmp_count=%d\n",
1885 rchain->hmp, rchain->hmp->pmp_count);
1890 * Mount helper, unhook the system mount from our PFS.
1891 * The mount lock is held.
1893 * If hmp is supplied a mount responsible for being the first to open
1894 * the block device failed and the block device and all PFSs using the
1895 * block device must be cleaned up.
1897 * If pmp is supplied multiple devices might be backing the PFS and each
1898 * must be disconnect. This might not be the last PFS using some of the
1899 * underlying devices. Also, we have to adjust our hmp->pmp_count accounting
1900 * for the devices backing the pmp which is now undergoing an unmount.
1904 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1906 hammer2_cluster_t *cluster;
1907 hammer2_chain_t *rchain;
1908 struct vnode *devvp;
1914 * If no device supplied this is a high-level unmount and we have to
1915 * to disconnect the mount, adjust pmp_count, and locate devices that
1916 * might now have no mounts.
1919 KKASSERT(hmp == NULL);
1920 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1922 mp->mnt_data = NULL;
1924 cluster = &pmp->iroot->cluster;
1925 for (i = 0; i < cluster->nchains; ++i) {
1926 rchain = cluster->array[i].chain;
1929 --rchain->hmp->pmp_count;
1930 kprintf("hammer2_unmount hmp=%p --pmp_count=%d\n",
1931 rchain->hmp, rchain->hmp->pmp_count);
1932 /* scrapping hmp now may invalidate the pmp */
1935 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1936 if (hmp->pmp_count == 0) {
1937 hammer2_unmount_helper(NULL, NULL, hmp);
1945 * Try to terminate the block device. We can't terminate it if
1946 * there are still PFSs referencing it.
1948 kprintf("hammer2_unmount hmp=%p pmp_count=%d\n", hmp, hmp->pmp_count);
1952 hammer2_pfsfree_scan(hmp);
1953 hammer2_dev_exlock(hmp); /* XXX order */
1956 * Cycle the volume data lock as a safety (probably not needed any
1957 * more). To ensure everything is out we need to flush at least
1958 * three times. (1) The running of the unlinkq can dirty the
1959 * filesystem, (2) A normal flush can dirty the freemap, and
1960 * (3) ensure that the freemap is fully synchronized.
1962 * The next mount's recovery scan can clean everything up but we want
1963 * to leave the filesystem in a 100% clean state on a normal unmount.
1966 hammer2_voldata_lock(hmp);
1967 hammer2_voldata_unlock(hmp);
1969 hammer2_iocom_uninit(hmp);
1971 if ((hmp->vchain.flags | hmp->fchain.flags) &
1972 HAMMER2_CHAIN_FLUSH_MASK) {
1973 kprintf("hammer2_unmount: chains left over "
1974 "after final sync\n");
1975 kprintf(" vchain %08x\n", hmp->vchain.flags);
1976 kprintf(" fchain %08x\n", hmp->fchain.flags);
1978 if (hammer2_debug & 0x0010)
1979 Debugger("entered debugger");
1982 KKASSERT(hmp->spmp == NULL);
1985 * Finish up with the device vnode
1987 if ((devvp = hmp->devvp) != NULL) {
1988 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1989 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1991 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1998 * Clear vchain/fchain flags that might prevent final cleanup
2001 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
2002 atomic_clear_int(&hmp->vchain.flags,
2003 HAMMER2_CHAIN_MODIFIED);
2004 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
2005 hammer2_chain_drop(&hmp->vchain);
2007 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
2008 atomic_clear_int(&hmp->vchain.flags,
2009 HAMMER2_CHAIN_UPDATE);
2010 hammer2_chain_drop(&hmp->vchain);
2013 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
2014 atomic_clear_int(&hmp->fchain.flags,
2015 HAMMER2_CHAIN_MODIFIED);
2016 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
2017 hammer2_chain_drop(&hmp->fchain);
2019 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
2020 atomic_clear_int(&hmp->fchain.flags,
2021 HAMMER2_CHAIN_UPDATE);
2022 hammer2_chain_drop(&hmp->fchain);
2026 * Final drop of embedded freemap root chain to
2027 * clean up fchain.core (fchain structure is not
2028 * flagged ALLOCATED so it is cleaned out and then
2031 hammer2_chain_drop(&hmp->fchain);
2034 * Final drop of embedded volume root chain to clean
2035 * up vchain.core (vchain structure is not flagged
2036 * ALLOCATED so it is cleaned out and then left to
2040 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
2042 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
2043 hammer2_dev_unlock(hmp);
2044 hammer2_chain_drop(&hmp->vchain);
2046 hammer2_io_cleanup(hmp, &hmp->iotree);
2047 if (hmp->iofree_count) {
2048 kprintf("io_cleanup: %d I/O's left hanging\n",
2052 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
2053 kmalloc_destroy(&hmp->mchain);
2054 kfree(hmp, M_HAMMER2);
2059 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
2060 ino_t ino, struct vnode **vpp)
2062 kprintf("hammer2_vget\n");
2063 return (EOPNOTSUPP);
2068 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
2071 hammer2_cluster_t *cparent;
2076 if (pmp->iroot == NULL) {
2080 cparent = hammer2_inode_lock_sh(pmp->iroot);
2081 vp = hammer2_igetv(pmp->iroot, cparent, &error);
2082 hammer2_inode_unlock_sh(pmp->iroot, cparent);
2085 kprintf("vnodefail\n");
2094 * XXX incorporate ipdata->inode_quota and data_quota
2098 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
2104 KKASSERT(pmp->iroot->cluster.nchains >= 1);
2105 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
2107 mp->mnt_stat.f_files = pmp->inode_count;
2108 mp->mnt_stat.f_ffree = 0;
2109 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
2110 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
2111 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
2113 *sbp = mp->mnt_stat;
2119 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
2125 KKASSERT(pmp->iroot->cluster.nchains >= 1);
2126 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
2128 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
2129 mp->mnt_vstat.f_files = pmp->inode_count;
2130 mp->mnt_vstat.f_ffree = 0;
2131 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
2132 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
2133 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
2135 *sbp = mp->mnt_vstat;
2140 * Mount-time recovery (RW mounts)
2142 * Updates to the free block table are allowed to lag flushes by one
2143 * transaction. In case of a crash, then on a fresh mount we must do an
2144 * incremental scan of the last committed transaction id and make sure that
2145 * all related blocks have been marked allocated.
2147 * The super-root topology and each PFS has its own transaction id domain,
2148 * so we must track PFS boundary transitions.
2150 struct hammer2_recovery_elm {
2151 TAILQ_ENTRY(hammer2_recovery_elm) entry;
2152 hammer2_chain_t *chain;
2153 hammer2_tid_t sync_tid;
2156 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
2158 struct hammer2_recovery_info {
2159 struct hammer2_recovery_list list;
2163 static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_dev_t *hmp,
2164 hammer2_chain_t *parent,
2165 struct hammer2_recovery_info *info,
2166 hammer2_tid_t sync_tid);
2168 #define HAMMER2_RECOVERY_MAXDEPTH 10
2172 hammer2_recovery(hammer2_dev_t *hmp)
2174 hammer2_trans_t trans;
2175 struct hammer2_recovery_info info;
2176 struct hammer2_recovery_elm *elm;
2177 hammer2_chain_t *parent;
2178 hammer2_tid_t sync_tid;
2180 int cumulative_error = 0;
2182 hammer2_trans_init(&trans, hmp->spmp, 0);
2185 TAILQ_INIT(&info.list);
2187 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
2188 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent,
2190 hammer2_chain_lookup_done(parent);
2192 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
2193 TAILQ_REMOVE(&info.list, elm, entry);
2194 parent = elm->chain;
2195 sync_tid = elm->sync_tid;
2196 kfree(elm, M_HAMMER2);
2198 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
2199 HAMMER2_RESOLVE_NOREF);
2200 error = hammer2_recovery_scan(&trans, hmp, parent,
2202 hammer2_chain_unlock(parent);
2204 cumulative_error = error;
2206 hammer2_trans_done(&trans);
2208 return cumulative_error;
2213 hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_dev_t *hmp,
2214 hammer2_chain_t *parent,
2215 struct hammer2_recovery_info *info,
2216 hammer2_tid_t sync_tid)
2218 const hammer2_inode_data_t *ripdata;
2219 hammer2_chain_t *chain;
2221 int cumulative_error = 0;
2222 int pfs_boundary = 0;
2226 * Adjust freemap to ensure that the block(s) are marked allocated.
2228 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
2229 hammer2_freemap_adjust(trans, hmp, &parent->bref,
2230 HAMMER2_FREEMAP_DORECOVER);
2234 * Check type for recursive scan
2236 switch(parent->bref.type) {
2237 case HAMMER2_BREF_TYPE_VOLUME:
2238 /* data already instantiated */
2240 case HAMMER2_BREF_TYPE_INODE:
2242 * Must instantiate data for DIRECTDATA test and also
2245 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2246 ripdata = &hammer2_chain_rdata(parent)->ipdata;
2247 if (ripdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2248 /* not applicable to recovery scan */
2249 hammer2_chain_unlock(parent);
2252 if ((ripdata->op_flags & HAMMER2_OPFLAG_PFSROOT) &&
2255 sync_tid = parent->bref.mirror_tid - 1;
2256 kprintf("recovery scan PFS synctid %016jx \"%s\"\n",
2257 sync_tid, ripdata->filename);
2260 if ((ripdata->op_flags & HAMMER2_OPFLAG_PFSROOT) == 0) {
2261 kprintf("%*.*s\"%s\"\n", info->depth, info->depth, "", ripdata->filename);
2264 hammer2_chain_unlock(parent);
2266 case HAMMER2_BREF_TYPE_INDIRECT:
2268 * Must instantiate data for recursion
2270 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2271 hammer2_chain_unlock(parent);
2273 case HAMMER2_BREF_TYPE_DATA:
2274 case HAMMER2_BREF_TYPE_FREEMAP:
2275 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2276 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2277 /* not applicable to recovery scan */
2285 * Defer operation if depth limit reached or if we are crossing a
2288 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH || pfs_boundary) {
2289 struct hammer2_recovery_elm *elm;
2291 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2292 elm->chain = parent;
2293 elm->sync_tid = sync_tid;
2294 hammer2_chain_ref(parent);
2295 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2296 /* unlocked by caller */
2303 * Recursive scan of the last flushed transaction only. We are
2304 * doing this without pmp assignments so don't leave the chains
2305 * hanging around after we are done with them.
2308 chain = hammer2_chain_scan(parent, NULL, &cache_index,
2309 HAMMER2_LOOKUP_NODATA);
2311 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2312 if (chain->bref.mirror_tid >= sync_tid) {
2314 error = hammer2_recovery_scan(trans, hmp, chain,
2318 cumulative_error = error;
2320 chain = hammer2_chain_scan(parent, chain, &cache_index,
2321 HAMMER2_LOOKUP_NODATA);
2324 return cumulative_error;
2328 * Sync the entire filesystem; this is called from the filesystem syncer
2329 * process periodically and whenever a user calls sync(1) on the hammer
2332 * Currently is actually called from the syncer! \o/
2334 * This task will have to snapshot the state of the dirty inode chain.
2335 * From that, it will have to make sure all of the inodes on the dirty
2336 * chain have IO initiated. We make sure that io is initiated for the root
2339 * If waitfor is set, we wait for media to acknowledge the new rootblock.
2341 * THINKS: side A vs side B, to have sync not stall all I/O?
2344 hammer2_vfs_sync(struct mount *mp, int waitfor)
2346 struct hammer2_sync_info info;
2347 hammer2_inode_t *iroot;
2348 hammer2_chain_t *chain;
2349 hammer2_chain_t *parent;
2362 KKASSERT(iroot->pmp == pmp);
2365 * We can't acquire locks on existing vnodes while in a transaction
2366 * without risking a deadlock. This assumes that vfsync() can be
2367 * called without the vnode locked (which it can in DragonFly).
2368 * Otherwise we'd have to implement a multi-pass or flag the lock
2369 * failures and retry.
2371 * The reclamation code interlocks with the sync list's token
2372 * (by removing the vnode from the scan list) before unlocking
2373 * the inode, giving us time to ref the inode.
2375 /*flags = VMSC_GETVP;*/
2377 if (waitfor & MNT_LAZY)
2378 flags |= VMSC_ONEPASS;
2381 * Start our flush transaction. This does not return until all
2382 * concurrent transactions have completed and will prevent any
2383 * new transactions from running concurrently, except for the
2384 * buffer cache transactions.
2386 * For efficiency do an async pass before making sure with a
2387 * synchronous pass on all related buffer cache buffers. It
2388 * should theoretically not be possible for any new file buffers
2389 * to be instantiated during this sequence.
2391 hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
2392 HAMMER2_TRANS_PREFLUSH);
2393 hammer2_run_unlinkq(&info.trans, pmp);
2396 info.waitfor = MNT_NOWAIT;
2397 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2398 info.waitfor = MNT_WAIT;
2399 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2402 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2403 * buffer cache flushes which occur during the flush. Device buffers
2408 if (info.error == 0 && (waitfor & MNT_WAIT)) {
2409 info.waitfor = waitfor;
2410 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2414 hammer2_bioq_sync(info.trans.pmp);
2415 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
2420 * Flush all storage elements making up the cluster
2422 * We must also flush any deleted siblings because the super-root
2423 * flush won't do it for us. They all must be staged or the
2424 * super-root flush will not be able to update its block table
2427 * XXX currently done serially instead of concurrently
2429 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2430 chain = iroot->cluster.array[i].chain;
2432 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
2433 hammer2_flush(&info.trans, chain);
2434 hammer2_chain_unlock(chain);
2438 hammer2_trans_done(&info.trans);
2442 * Flush all volume roots to synchronize PFS flushes with the
2443 * storage media. Use a super-root transaction for each one.
2445 * The flush code will detect super-root -> pfs-root chain
2446 * transitions using the last pfs-root flush.
2448 for (i = 0; iroot && i < iroot->cluster.nchains; ++i) {
2449 hammer2_chain_t *tmp;
2451 chain = iroot->cluster.array[i].chain;
2458 * We only have to flush each hmp once
2460 for (j = i - 1; j >= 0; --j) {
2461 if ((tmp = iroot->cluster.array[j].chain) != NULL) {
2462 if (tmp->hmp == hmp)
2468 hammer2_trans_spmp(&info.trans, hmp->spmp);
2471 * Force an update of the XID from the PFS root to the
2472 * topology root. We couldn't do this from the PFS
2473 * transaction because a SPMP transaction is needed.
2474 * This does not modify blocks, instead what it does is
2475 * allow the flush code to find the transition point and
2476 * then update on the way back up.
2478 parent = chain->parent;
2479 KKASSERT(chain->pmp != parent->pmp);
2480 hammer2_chain_setflush(&info.trans, parent);
2483 * Media mounts have two 'roots', vchain for the topology
2484 * and fchain for the free block table. Flush both.
2486 * Note that the topology and free block table are handled
2487 * independently, so the free block table can wind up being
2488 * ahead of the topology. We depend on the bulk free scan
2489 * code to deal with any loose ends.
2491 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2492 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2493 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2495 * This will also modify vchain as a side effect,
2496 * mark vchain as modified now.
2498 hammer2_voldata_modify(hmp);
2499 chain = &hmp->fchain;
2500 hammer2_flush(&info.trans, chain);
2501 KKASSERT(chain == &hmp->fchain);
2503 hammer2_chain_unlock(&hmp->fchain);
2504 hammer2_chain_unlock(&hmp->vchain);
2506 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
2507 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
2508 chain = &hmp->vchain;
2509 hammer2_flush(&info.trans, chain);
2510 KKASSERT(chain == &hmp->vchain);
2515 hammer2_chain_unlock(&hmp->vchain);
2518 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
2519 if ((hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) ||
2521 /* this will also modify vchain as a side effect */
2522 chain = &hmp->fchain;
2523 hammer2_flush(&info.trans, chain);
2524 KKASSERT(chain == &hmp->fchain);
2526 hammer2_chain_unlock(&hmp->fchain);
2532 * We can't safely flush the volume header until we have
2533 * flushed any device buffers which have built up.
2535 * XXX this isn't being incremental
2537 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2538 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2539 vn_unlock(hmp->devvp);
2542 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2543 * volume header needs synchronization via hmp->volsync.
2545 * XXX synchronize the flag & data with only this flush XXX
2548 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2552 * Synchronize the disk before flushing the volume
2556 bp->b_bio1.bio_offset = 0;
2559 bp->b_cmd = BUF_CMD_FLUSH;
2560 bp->b_bio1.bio_done = biodone_sync;
2561 bp->b_bio1.bio_flags |= BIO_SYNC;
2562 vn_strategy(hmp->devvp, &bp->b_bio1);
2563 biowait(&bp->b_bio1, "h2vol");
2567 * Then we can safely flush the version of the
2568 * volume header synchronized by the flush code.
2570 i = hmp->volhdrno + 1;
2571 if (i >= HAMMER2_NUM_VOLHDRS)
2573 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2574 hmp->volsync.volu_size) {
2577 kprintf("sync volhdr %d %jd\n",
2578 i, (intmax_t)hmp->volsync.volu_size);
2579 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2580 HAMMER2_PBUFSIZE, 0, 0);
2581 atomic_clear_int(&hmp->vchain.flags,
2582 HAMMER2_CHAIN_VOLUMESYNC);
2583 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2588 total_error = error;
2591 hammer2_trans_done(&info.trans);
2594 hammer2_trans_done(&info.trans);
2596 return (total_error);
2603 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2605 struct hammer2_sync_info *info = data;
2606 hammer2_inode_t *ip;
2615 if (vp->v_type == VNON || vp->v_type == VBAD) {
2619 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2620 RB_EMPTY(&vp->v_rbdirty_tree)) {
2626 * VOP_FSYNC will start a new transaction so replicate some code
2627 * here to do it inline (see hammer2_vop_fsync()).
2629 * WARNING: The vfsync interacts with the buffer cache and might
2630 * block, we can't hold the inode lock at that time.
2631 * However, we MUST ref ip before blocking to ensure that
2632 * it isn't ripped out from under us (since we do not
2633 * hold a lock on the vnode).
2635 hammer2_inode_ref(ip);
2636 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
2638 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
2640 hammer2_inode_drop(ip);
2644 info->error = error;
2651 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2658 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2659 struct fid *fhp, struct vnode **vpp)
2666 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2667 int *exflagsp, struct ucred **credanonp)
2673 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2674 * header into the HMP
2676 * XXX read four volhdrs and use the one with the highest TID whos CRC
2681 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2682 * nonexistant locations.
2684 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2688 hammer2_install_volume_header(hammer2_dev_t *hmp)
2690 hammer2_volume_data_t *vd;
2692 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2704 * There are up to 4 copies of the volume header (syncs iterate
2705 * between them so there is no single master). We don't trust the
2706 * volu_size field so we don't know precisely how large the filesystem
2707 * is, so depend on the OS to return an error if we go beyond the
2708 * block device's EOF.
2710 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2711 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2712 HAMMER2_VOLUME_BYTES, &bp);
2719 vd = (struct hammer2_volume_data *) bp->b_data;
2720 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2721 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2727 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2728 /* XXX: Reversed-endianness filesystem */
2729 kprintf("hammer2: reverse-endian filesystem detected");
2735 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2736 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2737 HAMMER2_VOLUME_ICRC0_SIZE);
2738 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2739 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2740 HAMMER2_VOLUME_ICRC1_SIZE);
2741 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2742 kprintf("hammer2 volume header crc "
2743 "mismatch copy #%d %08x/%08x\n",
2750 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2759 hmp->volsync = hmp->voldata;
2761 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2762 kprintf("hammer2: using volume header #%d\n",
2767 kprintf("hammer2: no valid volume headers found!\n");
2773 * This handles hysteresis on regular file flushes. Because the BIOs are
2774 * routed to a thread it is possible for an excessive number to build up
2775 * and cause long front-end stalls long before the runningbuffspace limit
2776 * is hit, so we implement hammer2_flush_pipe to control the
2779 * This is a particular problem when compression is used.
2782 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2784 atomic_add_int(&pmp->count_lwinprog, 1);
2788 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2792 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2793 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2794 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2795 atomic_clear_int(&pmp->count_lwinprog,
2796 HAMMER2_LWINPROG_WAITING);
2797 wakeup(&pmp->count_lwinprog);
2802 hammer2_lwinprog_wait(hammer2_pfs_t *pmp)
2807 lwinprog = pmp->count_lwinprog;
2809 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2811 tsleep_interlock(&pmp->count_lwinprog, 0);
2812 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2813 lwinprog = pmp->count_lwinprog;
2814 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2816 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2821 * Manage excessive memory resource use for chain and related
2825 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
2835 * Atomic check condition and wait. Also do an early speedup of
2836 * the syncer to try to avoid hitting the wait.
2839 waiting = pmp->inmem_dirty_chains;
2841 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2843 limit = pmp->mp->mnt_nvnodelistsize / 10;
2844 if (limit < hammer2_limit_dirty_chains)
2845 limit = hammer2_limit_dirty_chains;
2850 if ((int)(ticks - zzticks) > hz) {
2852 kprintf("count %ld %ld\n", count, limit);
2857 * Block if there are too many dirty chains present, wait
2858 * for the flush to clean some out.
2860 if (count > limit) {
2861 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2862 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2864 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2865 speedup_syncer(pmp->mp);
2866 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2869 continue; /* loop on success or fail */
2873 * Try to start an early flush before we are forced to block.
2875 if (count > limit * 7 / 10)
2876 speedup_syncer(pmp->mp);
2882 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
2885 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2890 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
2898 waiting = pmp->inmem_dirty_chains;
2900 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2903 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2908 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2909 wakeup(&pmp->inmem_dirty_chains);
2916 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2918 hammer2_chain_t *scan;
2919 hammer2_chain_t *parent;
2923 kprintf("%*.*s...\n", tab, tab, "");
2928 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2930 chain, chain->bref.type,
2931 chain->bref.key, chain->bref.keybits,
2932 chain->bref.mirror_tid);
2934 kprintf("%*.*s [%08x] (%s) refs=%d\n",
2937 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2938 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
2941 kprintf("%*.*s core [%08x]",
2945 parent = chain->parent;
2947 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
2949 parent, parent->flags, parent->refs);
2950 if (RB_EMPTY(&chain->core.rbtree)) {
2954 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2955 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2956 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2957 kprintf("%*.*s}(%s)\n", tab, tab, "",
2958 chain->data->ipdata.filename);
2960 kprintf("%*.*s}\n", tab, tab, "");