2 * Copyright (c) 2011-2018 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 {
75 TAILQ_HEAD(hammer2_mntlist, hammer2_dev);
76 static struct hammer2_mntlist hammer2_mntlist;
78 struct hammer2_pfslist hammer2_pfslist;
79 struct hammer2_pfslist hammer2_spmplist;
80 struct lock hammer2_mntlk;
82 int hammer2_supported_version = HAMMER2_VOL_VERSION_DEFAULT;
84 int hammer2_cluster_meta_read = 1; /* physical read-ahead */
85 int hammer2_cluster_data_read = 4; /* physical read-ahead */
86 int hammer2_cluster_write = 0; /* physical write clustering */
87 int hammer2_dedup_enable = 1;
88 int hammer2_always_compress = 0; /* always try to compress */
89 int hammer2_inval_enable = 0;
90 int hammer2_flush_pipe = 100;
91 int hammer2_dio_count;
92 int hammer2_dio_limit = 256;
93 int hammer2_bulkfree_tps = 5000;
94 long hammer2_chain_allocs;
95 long hammer2_chain_frees;
96 long hammer2_limit_dirty_chains;
97 long hammer2_limit_dirty_inodes;
98 long hammer2_count_modified_chains;
99 long hammer2_iod_invals;
100 long hammer2_iod_file_read;
101 long hammer2_iod_meta_read;
102 long hammer2_iod_indr_read;
103 long hammer2_iod_fmap_read;
104 long hammer2_iod_volu_read;
105 long hammer2_iod_file_write;
106 long hammer2_iod_file_wembed;
107 long hammer2_iod_file_wzero;
108 long hammer2_iod_file_wdedup;
109 long hammer2_iod_meta_write;
110 long hammer2_iod_indr_write;
111 long hammer2_iod_fmap_write;
112 long hammer2_iod_volu_write;
114 MALLOC_DECLARE(M_HAMMER2_CBUFFER);
115 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
116 "Buffer used for compression.");
118 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
119 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
120 "Buffer used for decompression.");
122 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
124 SYSCTL_INT(_vfs_hammer2, OID_AUTO, supported_version, CTLFLAG_RD,
125 &hammer2_supported_version, 0, "");
126 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
127 &hammer2_debug, 0, "");
128 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_meta_read, CTLFLAG_RW,
129 &hammer2_cluster_meta_read, 0, "");
130 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_data_read, CTLFLAG_RW,
131 &hammer2_cluster_data_read, 0, "");
132 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_write, CTLFLAG_RW,
133 &hammer2_cluster_write, 0, "");
134 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dedup_enable, CTLFLAG_RW,
135 &hammer2_dedup_enable, 0, "");
136 SYSCTL_INT(_vfs_hammer2, OID_AUTO, always_compress, CTLFLAG_RW,
137 &hammer2_always_compress, 0, "");
138 SYSCTL_INT(_vfs_hammer2, OID_AUTO, inval_enable, CTLFLAG_RW,
139 &hammer2_inval_enable, 0, "");
140 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
141 &hammer2_flush_pipe, 0, "");
142 SYSCTL_INT(_vfs_hammer2, OID_AUTO, bulkfree_tps, CTLFLAG_RW,
143 &hammer2_bulkfree_tps, 0, "");
144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_allocs, CTLFLAG_RW,
145 &hammer2_chain_allocs, 0, "");
146 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_frees, CTLFLAG_RW,
147 &hammer2_chain_frees, 0, "");
148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
149 &hammer2_limit_dirty_chains, 0, "");
150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_inodes, CTLFLAG_RW,
151 &hammer2_limit_dirty_inodes, 0, "");
152 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, count_modified_chains, CTLFLAG_RW,
153 &hammer2_count_modified_chains, 0, "");
154 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
155 &hammer2_dio_count, 0, "");
156 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_limit, CTLFLAG_RW,
157 &hammer2_dio_limit, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_invals, CTLFLAG_RW,
160 &hammer2_iod_invals, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
162 &hammer2_iod_file_read, 0, "");
163 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
164 &hammer2_iod_meta_read, 0, "");
165 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
166 &hammer2_iod_indr_read, 0, "");
167 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
168 &hammer2_iod_fmap_read, 0, "");
169 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
170 &hammer2_iod_volu_read, 0, "");
172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
173 &hammer2_iod_file_write, 0, "");
174 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW,
175 &hammer2_iod_file_wembed, 0, "");
176 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW,
177 &hammer2_iod_file_wzero, 0, "");
178 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW,
179 &hammer2_iod_file_wdedup, 0, "");
180 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
181 &hammer2_iod_meta_write, 0, "");
182 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
183 &hammer2_iod_indr_write, 0, "");
184 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
185 &hammer2_iod_fmap_write, 0, "");
186 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
187 &hammer2_iod_volu_write, 0, "");
189 long hammer2_process_icrc32;
190 long hammer2_process_xxhash64;
191 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_icrc32, CTLFLAG_RW,
192 &hammer2_process_icrc32, 0, "");
193 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_xxhash64, CTLFLAG_RW,
194 &hammer2_process_xxhash64, 0, "");
196 static int hammer2_vfs_init(struct vfsconf *conf);
197 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
198 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
200 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
201 struct vnode *, struct ucred *);
202 static int hammer2_recovery(hammer2_dev_t *hmp);
203 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
204 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
205 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
207 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
209 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
210 struct fid *fhp, struct vnode **vpp);
211 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
212 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
213 int *exflagsp, struct ucred **credanonp);
215 static int hammer2_install_volume_header(hammer2_dev_t *hmp);
216 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
218 static void hammer2_update_pmps(hammer2_dev_t *hmp);
220 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
221 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
223 static int hammer2_fixup_pfses(hammer2_dev_t *hmp);
226 * HAMMER2 vfs operations.
228 static struct vfsops hammer2_vfsops = {
229 .vfs_init = hammer2_vfs_init,
230 .vfs_uninit = hammer2_vfs_uninit,
231 .vfs_sync = hammer2_vfs_sync,
232 .vfs_mount = hammer2_vfs_mount,
233 .vfs_unmount = hammer2_vfs_unmount,
234 .vfs_root = hammer2_vfs_root,
235 .vfs_statfs = hammer2_vfs_statfs,
236 .vfs_statvfs = hammer2_vfs_statvfs,
237 .vfs_vget = hammer2_vfs_vget,
238 .vfs_vptofh = hammer2_vfs_vptofh,
239 .vfs_fhtovp = hammer2_vfs_fhtovp,
240 .vfs_checkexp = hammer2_vfs_checkexp
243 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
245 VFS_SET(hammer2_vfsops, hammer2, VFCF_MPSAFE);
246 MODULE_VERSION(hammer2, 1);
250 hammer2_vfs_init(struct vfsconf *conf)
252 static struct objcache_malloc_args margs_read;
253 static struct objcache_malloc_args margs_write;
254 static struct objcache_malloc_args margs_vop;
261 * A large DIO cache is needed to retain dedup enablement masks.
262 * The bulkfree code clears related masks as part of the disk block
263 * recycling algorithm, preventing it from being used for a later
266 * NOTE: A large buffer cache can actually interfere with dedup
267 * operation because we dedup based on media physical buffers
268 * and not logical buffers. Try to make the DIO case large
269 * enough to avoid this problem, but also cap it.
271 hammer2_dio_limit = nbuf * 2;
272 if (hammer2_dio_limit > 100000)
273 hammer2_dio_limit = 100000;
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_xop_t);
292 margs_vop.mtype = M_HAMMER2;
295 * Note thaht for the XOPS cache we want backing store allocations
296 * to use M_ZERO. This is not allowed in objcache_get() (to avoid
297 * confusion), so use the backing store function that does it. This
298 * means that initial XOPS objects are zerod but REUSED objects are
299 * not. So we are responsible for cleaning the object up sufficiently
300 * for our needs before objcache_put()ing it back (typically just the
303 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
304 0, 1, NULL, NULL, NULL,
305 objcache_malloc_alloc,
306 objcache_malloc_free,
308 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
309 0, 1, NULL, NULL, NULL,
310 objcache_malloc_alloc,
311 objcache_malloc_free,
313 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc,
314 0, 1, NULL, NULL, NULL,
315 objcache_malloc_alloc_zero,
316 objcache_malloc_free,
320 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
321 TAILQ_INIT(&hammer2_mntlist);
322 TAILQ_INIT(&hammer2_pfslist);
323 TAILQ_INIT(&hammer2_spmplist);
325 hammer2_limit_dirty_chains = maxvnodes / 10;
326 if (hammer2_limit_dirty_chains > HAMMER2_LIMIT_DIRTY_CHAINS)
327 hammer2_limit_dirty_chains = HAMMER2_LIMIT_DIRTY_CHAINS;
329 hammer2_limit_dirty_inodes = maxvnodes / 100;
330 if (hammer2_limit_dirty_inodes < 100)
331 hammer2_limit_dirty_inodes = 100;
332 if (hammer2_limit_dirty_inodes > HAMMER2_LIMIT_DIRTY_INODES)
333 hammer2_limit_dirty_inodes = HAMMER2_LIMIT_DIRTY_INODES;
340 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
342 objcache_destroy(cache_buffer_read);
343 objcache_destroy(cache_buffer_write);
344 objcache_destroy(cache_xops);
349 * Core PFS allocator. Used to allocate or reference the pmp structure
350 * for PFS cluster mounts and the spmp structure for media (hmp) structures.
351 * The pmp can be passed in or loaded by this function using the chain and
354 * pmp->modify_tid tracks new modify_tid transaction ids for front-end
355 * transactions. Note that synchronization does not use this field.
356 * (typically frontend operations and synchronization cannot run on the
357 * same PFS node at the same time).
362 hammer2_pfsalloc(hammer2_chain_t *chain,
363 const hammer2_inode_data_t *ripdata,
364 hammer2_tid_t modify_tid, hammer2_dev_t *force_local)
367 hammer2_inode_t *iroot;
375 * Locate or create the PFS based on the cluster id. If ripdata
376 * is NULL this is a spmp which is unique and is always allocated.
378 * If the device is mounted in local mode all PFSs are considered
379 * independent and not part of any cluster (for debugging only).
382 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
383 if (force_local != pmp->force_local)
385 if (force_local == NULL &&
386 bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
387 sizeof(pmp->pfs_clid)) == 0) {
389 } else if (force_local && pmp->pfs_names[0] &&
390 strcmp(pmp->pfs_names[0], ripdata->filename) == 0) {
397 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
398 pmp->force_local = force_local;
399 hammer2_trans_manage_init(pmp);
400 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
401 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
402 lockinit(&pmp->lock, "pfslk", 0, 0);
403 lockinit(&pmp->lock_nlink, "h2nlink", 0, 0);
404 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
405 spin_init(&pmp->xop_spin, "h2xop");
406 spin_init(&pmp->lru_spin, "h2lru");
407 RB_INIT(&pmp->inum_tree);
408 TAILQ_INIT(&pmp->sideq);
409 TAILQ_INIT(&pmp->lru_list);
410 spin_init(&pmp->list_spin, "hm2pfsalloc_list");
413 * Distribute backend operations to threads
415 for (i = 0; i < HAMMER2_XOPGROUPS; ++i)
416 hammer2_xop_group_init(pmp, &pmp->xop_groups[i]);
419 * Save the last media transaction id for the flusher. Set
423 pmp->pfs_clid = ripdata->meta.pfs_clid;
424 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
426 pmp->flags |= HAMMER2_PMPF_SPMP;
427 TAILQ_INSERT_TAIL(&hammer2_spmplist, pmp, mntentry);
431 * The synchronization thread may start too early, make
432 * sure it stays frozen until we are ready to let it go.
436 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN |
437 HAMMER2_THREAD_REMASTER;
442 * Create the PFS's root inode and any missing XOP helper threads.
444 if ((iroot = pmp->iroot) == NULL) {
445 iroot = hammer2_inode_get(pmp, NULL, NULL, -1);
447 iroot->meta = ripdata->meta;
449 hammer2_inode_ref(iroot);
450 hammer2_inode_unlock(iroot);
454 * Stop here if no chain is passed in.
460 * When a chain is passed in we must add it to the PFS's root
461 * inode, update pmp->pfs_types[], and update the syncronization
464 * When forcing local mode, mark the PFS as a MASTER regardless.
466 * At the moment empty spots can develop due to removals or failures.
467 * Ultimately we want to re-fill these spots but doing so might
468 * confused running code. XXX
470 hammer2_inode_ref(iroot);
471 hammer2_mtx_ex(&iroot->lock);
472 j = iroot->cluster.nchains;
474 if (j == HAMMER2_MAXCLUSTER) {
475 kprintf("hammer2_mount: cluster full!\n");
476 /* XXX fatal error? */
478 KKASSERT(chain->pmp == NULL);
480 hammer2_chain_ref(chain);
481 iroot->cluster.array[j].chain = chain;
483 pmp->pfs_types[j] = HAMMER2_PFSTYPE_MASTER;
485 pmp->pfs_types[j] = ripdata->meta.pfs_type;
486 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
487 pmp->pfs_hmps[j] = chain->hmp;
490 * If the PFS is already mounted we must account
491 * for the mount_count here.
494 ++chain->hmp->mount_count;
497 * May have to fixup dirty chain tracking. Previous
498 * pmp was NULL so nothing to undo.
500 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
501 hammer2_pfs_memory_inc(pmp);
504 iroot->cluster.nchains = j;
507 * Update nmasters from any PFS inode which is part of the cluster.
508 * It is possible that this will result in a value which is too
509 * high. MASTER PFSs are authoritative for pfs_nmasters and will
510 * override this value later on.
512 * (This informs us of masters that might not currently be
513 * discoverable by this mount).
515 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
516 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
520 * Count visible masters. Masters are usually added with
521 * ripdata->meta.pfs_nmasters set to 1. This detects when there
522 * are more (XXX and must update the master inodes).
525 for (i = 0; i < iroot->cluster.nchains; ++i) {
526 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
529 if (pmp->pfs_nmasters < count)
530 pmp->pfs_nmasters = count;
533 * Create missing synchronization and support threads.
535 * Single-node masters (including snapshots) have nothing to
536 * synchronize and do not require this thread.
538 * Multi-node masters or any number of soft masters, slaves, copy,
539 * or other PFS types need the thread.
541 * Each thread is responsible for its particular cluster index.
542 * We use independent threads so stalls or mismatches related to
543 * any given target do not affect other targets.
545 for (i = 0; i < iroot->cluster.nchains; ++i) {
547 * Single-node masters (including snapshots) have nothing
548 * to synchronize and will make direct xops support calls,
549 * thus they do not require this thread.
551 * Note that there can be thousands of snapshots. We do not
552 * want to create thousands of threads.
554 if (pmp->pfs_nmasters <= 1 &&
555 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) {
560 * Sync support thread
562 if (pmp->sync_thrs[i].td == NULL) {
563 hammer2_thr_create(&pmp->sync_thrs[i], pmp, NULL,
565 hammer2_primary_sync_thread);
570 * Create missing Xop threads
572 * NOTE: We create helper threads for all mounted PFSs or any
573 * PFSs with 2+ nodes (so the sync thread can update them,
574 * even if not mounted).
576 if (pmp->mp || iroot->cluster.nchains >= 2)
577 hammer2_xop_helper_create(pmp);
579 hammer2_mtx_unlock(&iroot->lock);
580 hammer2_inode_drop(iroot);
586 * Deallocate an element of a probed PFS. If destroying and this is a
587 * MASTER, adjust nmasters.
589 * This function does not physically destroy the PFS element in its device
590 * under the super-root (see hammer2_ioctl_pfs_delete()).
593 hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying)
595 hammer2_inode_t *iroot;
596 hammer2_chain_t *chain;
600 * Cleanup our reference on iroot. iroot is (should) not be needed
608 * XXX flush after acquiring the iroot lock.
609 * XXX clean out the cluster index from all inode structures.
611 hammer2_thr_delete(&pmp->sync_thrs[clindex]);
614 * Remove the cluster index from the group. If destroying
615 * the PFS and this is a master, adjust pfs_nmasters.
617 hammer2_mtx_ex(&iroot->lock);
618 chain = iroot->cluster.array[clindex].chain;
619 iroot->cluster.array[clindex].chain = NULL;
621 switch(pmp->pfs_types[clindex]) {
622 case HAMMER2_PFSTYPE_MASTER:
623 if (destroying && pmp->pfs_nmasters > 0)
625 /* XXX adjust ripdata->meta.pfs_nmasters */
630 pmp->pfs_types[clindex] = HAMMER2_PFSTYPE_NONE;
632 hammer2_mtx_unlock(&iroot->lock);
638 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
639 hammer2_chain_drop(chain);
643 * Terminate all XOP threads for the cluster index.
645 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
646 hammer2_thr_delete(&pmp->xop_groups[j].thrs[clindex]);
651 * Destroy a PFS, typically only occurs after the last mount on a device
655 hammer2_pfsfree(hammer2_pfs_t *pmp)
657 hammer2_inode_t *iroot;
658 hammer2_chain_t *chain;
663 * Cleanup our reference on iroot. iroot is (should) not be needed
666 if (pmp->flags & HAMMER2_PMPF_SPMP)
667 TAILQ_REMOVE(&hammer2_spmplist, pmp, mntentry);
669 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
673 for (i = 0; i < iroot->cluster.nchains; ++i) {
674 hammer2_thr_delete(&pmp->sync_thrs[i]);
675 for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
676 hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
678 #if REPORT_REFS_ERRORS
679 if (pmp->iroot->refs != 1)
680 kprintf("PMP->IROOT %p REFS WRONG %d\n",
681 pmp->iroot, pmp->iroot->refs);
683 KKASSERT(pmp->iroot->refs == 1);
685 /* ref for pmp->iroot */
686 hammer2_inode_drop(pmp->iroot);
691 * Cleanup chains remaining on LRU list.
693 hammer2_spin_ex(&pmp->lru_spin);
694 while ((chain = TAILQ_FIRST(&pmp->lru_list)) != NULL) {
695 KKASSERT(chain->flags & HAMMER2_CHAIN_ONLRU);
696 atomic_add_int(&pmp->lru_count, -1);
697 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
698 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
699 hammer2_chain_ref(chain);
700 hammer2_spin_unex(&pmp->lru_spin);
701 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
702 hammer2_chain_drop(chain);
703 hammer2_spin_ex(&pmp->lru_spin);
705 hammer2_spin_unex(&pmp->lru_spin);
708 * Free remaining pmp resources
710 kmalloc_destroy(&pmp->mmsg);
711 kmalloc_destroy(&pmp->minode);
713 kfree(pmp, M_HAMMER2);
717 * Remove all references to hmp from the pfs list. Any PFS which becomes
718 * empty is terminated and freed.
723 hammer2_pfsfree_scan(hammer2_dev_t *hmp, int which)
726 hammer2_inode_t *iroot;
727 hammer2_chain_t *rchain;
731 struct hammer2_pfslist *wlist;
734 wlist = &hammer2_pfslist;
736 wlist = &hammer2_spmplist;
738 TAILQ_FOREACH(pmp, wlist, mntentry) {
739 if ((iroot = pmp->iroot) == NULL)
741 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH);
742 hammer2_inode_run_sideq(pmp, 1);
743 hammer2_bioq_sync(pmp);
744 hammer2_trans_done(pmp, 0);
747 * Determine if this PFS is affected. If it is we must
748 * freeze all management threads and lock its iroot.
750 * Freezing a management thread forces it idle, operations
751 * in-progress will be aborted and it will have to start
752 * over again when unfrozen, or exit if told to exit.
754 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
755 if (pmp->pfs_hmps[i] == hmp)
758 if (i != HAMMER2_MAXCLUSTER) {
760 * Make sure all synchronization threads are locked
763 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
764 if (pmp->pfs_hmps[i] == NULL)
766 hammer2_thr_freeze_async(&pmp->sync_thrs[i]);
767 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
768 hammer2_thr_freeze_async(
769 &pmp->xop_groups[j].thrs[i]);
772 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
773 if (pmp->pfs_hmps[i] == NULL)
775 hammer2_thr_freeze(&pmp->sync_thrs[i]);
776 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
778 &pmp->xop_groups[j].thrs[i]);
783 * Lock the inode and clean out matching chains.
784 * Note that we cannot use hammer2_inode_lock_*()
785 * here because that would attempt to validate the
786 * cluster that we are in the middle of ripping
789 * WARNING! We are working directly on the inodes
792 hammer2_mtx_ex(&iroot->lock);
795 * Remove the chain from matching elements of the PFS.
797 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
798 if (pmp->pfs_hmps[i] != hmp)
800 hammer2_thr_delete(&pmp->sync_thrs[i]);
801 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
803 &pmp->xop_groups[j].thrs[i]);
805 rchain = iroot->cluster.array[i].chain;
806 iroot->cluster.array[i].chain = NULL;
807 pmp->pfs_types[i] = 0;
808 if (pmp->pfs_names[i]) {
809 kfree(pmp->pfs_names[i], M_HAMMER2);
810 pmp->pfs_names[i] = NULL;
813 hammer2_chain_drop(rchain);
815 if (iroot->cluster.focus == rchain)
816 iroot->cluster.focus = NULL;
818 pmp->pfs_hmps[i] = NULL;
820 hammer2_mtx_unlock(&iroot->lock);
821 didfreeze = 1; /* remaster, unfreeze down below */
827 * Cleanup trailing chains. Gaps may remain.
829 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) {
830 if (pmp->pfs_hmps[i])
833 iroot->cluster.nchains = i + 1;
836 * If the PMP has no elements remaining we can destroy it.
837 * (this will transition management threads from frozen->exit).
839 if (iroot->cluster.nchains == 0) {
841 * If this was the hmp's spmp, we need to clean
842 * a little more stuff out.
844 if (hmp->spmp == pmp) {
846 hmp->vchain.pmp = NULL;
847 hmp->fchain.pmp = NULL;
851 * Free the pmp and restart the loop
853 hammer2_pfsfree(pmp);
858 * If elements still remain we need to set the REMASTER
859 * flag and unfreeze it.
862 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
863 if (pmp->pfs_hmps[i] == NULL)
865 hammer2_thr_remaster(&pmp->sync_thrs[i]);
866 hammer2_thr_unfreeze(&pmp->sync_thrs[i]);
867 for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
868 hammer2_thr_remaster(
869 &pmp->xop_groups[j].thrs[i]);
870 hammer2_thr_unfreeze(
871 &pmp->xop_groups[j].thrs[i]);
879 * Mount or remount HAMMER2 fileystem from physical media
882 * mp mount point structure
888 * mp mount point structure
889 * path path to mount point
890 * data pointer to argument structure in user space
891 * volume volume path (device@LABEL form)
892 * hflags user mount flags
893 * cred user credentials
900 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
903 struct hammer2_mount_info info;
907 hammer2_dev_t *force_local;
908 hammer2_key_t key_next;
909 hammer2_key_t key_dummy;
912 struct nlookupdata nd;
913 hammer2_chain_t *parent;
914 hammer2_chain_t *chain;
915 const hammer2_inode_data_t *ripdata;
916 hammer2_blockref_t bref;
918 char devstr[MNAMELEN];
937 bzero(&info, sizeof(info));
938 info.cluster_fd = -1;
939 ksnprintf(devstr, sizeof(devstr), "%s",
940 mp->mnt_stat.f_mntfromname);
941 kprintf("hammer2_mount: root '%s'\n", devstr);
942 done = strlen(devstr) + 1;
945 * Non-root mount or updating a mount
947 error = copyin(data, &info, sizeof(info));
951 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
954 kprintf("hammer2_mount: '%s'\n", devstr);
958 * Extract device and label, automatically mount @BOOT, @ROOT, or @DATA
959 * if no label specified, based on the partition id. Error out if no
960 * label or device (with partition id) is specified. This is strictly
961 * a convenience to match the default label created by newfs_hammer2,
962 * our preference is that a label always be specified.
964 * NOTE: We allow 'mount @LABEL <blah>'... that is, a mount command
965 * that does not specify a device, as long as some H2 label
966 * has already been mounted from that device. This makes
967 * mounting snapshots a lot easier.
970 label = strchr(devstr, '@');
971 if (label && ((label + 1) - dev) > done) {
972 kprintf("hammer2: mount: bad label %s/%zd\n",
976 if (label == NULL || label[1] == 0) {
980 label = devstr + strlen(devstr);
982 *label = '\0'; /* clean up trailing @ */
1001 kprintf("hammer2_mount: dev=\"%s\" label=\"%s\" rdonly=%d\n",
1002 dev, label, (mp->mnt_flag & MNT_RDONLY));
1004 if (mp->mnt_flag & MNT_UPDATE) {
1006 * Update mount. Note that pmp->iroot->cluster is
1007 * an inode-embedded cluster and thus cannot be
1010 * XXX HAMMER2 needs to implement NFS export via
1013 hammer2_cluster_t *cluster;
1016 pmp->hflags = info.hflags;
1017 cluster = &pmp->iroot->cluster;
1018 for (i = 0; i < cluster->nchains; ++i) {
1019 if (cluster->array[i].chain == NULL)
1021 hmp = cluster->array[i].chain->hmp;
1023 error = hammer2_remount(hmp, mp, path,
1035 * If a path is specified and dev is not an empty string, lookup the
1036 * name and verify that it referes to a block device.
1038 * If a path is specified and dev is an empty string we fall through
1039 * and locate the label in the hmp search.
1041 if (path && *dev != 0) {
1042 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
1044 error = nlookup(&nd);
1046 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
1048 } else if (path == NULL) {
1050 cdev_t cdev = kgetdiskbyname(dev);
1051 error = bdevvp(cdev, &devvp);
1053 kprintf("hammer2: cannot find '%s'\n", dev);
1056 * We will locate the hmp using the label in the hmp loop.
1062 * Make sure its a block device. Do not check to see if it is
1063 * already mounted until we determine that its a fresh H2 device.
1065 if (error == 0 && devvp) {
1066 vn_isdisk(devvp, &error);
1070 * Determine if the device has already been mounted. After this
1071 * check hmp will be non-NULL if we are doing the second or more
1072 * hammer2 mounts from the same device.
1074 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1077 * Match the device. Due to the way devfs works,
1078 * we may not be able to directly match the vnode pointer,
1079 * so also check to see if the underlying device matches.
1081 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1082 if (hmp->devvp == devvp)
1084 if (devvp->v_rdev &&
1085 hmp->devvp->v_rdev == devvp->v_rdev) {
1091 * If no match this may be a fresh H2 mount, make sure
1092 * the device is not mounted on anything else.
1095 error = vfs_mountedon(devvp);
1096 } else if (error == 0) {
1098 * Match the label to a pmp already probed.
1100 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
1101 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
1102 if (pmp->pfs_names[i] &&
1103 strcmp(pmp->pfs_names[i], label) == 0) {
1104 hmp = pmp->pfs_hmps[i];
1116 * Open the device if this isn't a secondary mount and construct
1117 * the H2 device mount (hmp).
1120 hammer2_chain_t *schain;
1122 hammer2_xop_head_t xop;
1124 if (error == 0 && vcount(devvp) > 0) {
1125 kprintf("Primary device already has references\n");
1130 * Now open the device
1133 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1134 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1135 error = vinvalbuf(devvp, V_SAVE, 0, 0);
1137 error = VOP_OPEN(devvp,
1138 (ronly ? FREAD : FREAD | FWRITE),
1143 if (error && devvp) {
1148 lockmgr(&hammer2_mntlk, LK_RELEASE);
1151 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
1152 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
1155 hmp->hflags = info.hflags & HMNT2_DEVFLAGS;
1156 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
1157 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
1158 RB_INIT(&hmp->iotree);
1159 spin_init(&hmp->io_spin, "hm2mount_io");
1160 spin_init(&hmp->list_spin, "hm2mount_list");
1161 TAILQ_INIT(&hmp->flushq);
1163 lockinit(&hmp->vollk, "h2vol", 0, 0);
1164 lockinit(&hmp->bulklk, "h2bulk", 0, 0);
1165 lockinit(&hmp->bflock, "h2bflk", 0, 0);
1168 * vchain setup. vchain.data is embedded.
1169 * vchain.refs is initialized and will never drop to 0.
1171 * NOTE! voldata is not yet loaded.
1173 hmp->vchain.hmp = hmp;
1174 hmp->vchain.refs = 1;
1175 hmp->vchain.data = (void *)&hmp->voldata;
1176 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
1177 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
1178 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
1180 hammer2_chain_core_init(&hmp->vchain);
1181 /* hmp->vchain.u.xxx is left NULL */
1184 * fchain setup. fchain.data is embedded.
1185 * fchain.refs is initialized and will never drop to 0.
1187 * The data is not used but needs to be initialized to
1188 * pass assertion muster. We use this chain primarily
1189 * as a placeholder for the freemap's top-level RBTREE
1190 * so it does not interfere with the volume's topology
1193 hmp->fchain.hmp = hmp;
1194 hmp->fchain.refs = 1;
1195 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
1196 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
1197 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
1198 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
1199 hmp->fchain.bref.methods =
1200 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
1201 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
1203 hammer2_chain_core_init(&hmp->fchain);
1204 /* hmp->fchain.u.xxx is left NULL */
1207 * Install the volume header and initialize fields from
1210 error = hammer2_install_volume_header(hmp);
1212 hammer2_unmount_helper(mp, NULL, hmp);
1213 lockmgr(&hammer2_mntlk, LK_RELEASE);
1214 hammer2_vfs_unmount(mp, MNT_FORCE);
1219 * Really important to get these right or flush will get
1222 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0, NULL);
1226 * Dummy-up vchain and fchain's modify_tid. mirror_tid
1227 * is inherited from the volume header.
1230 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
1231 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
1232 hmp->vchain.pmp = spmp;
1233 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
1234 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
1235 hmp->fchain.pmp = spmp;
1238 * First locate the super-root inode, which is key 0
1239 * relative to the volume header's blockset.
1241 * Then locate the root inode by scanning the directory keyspace
1242 * represented by the label.
1244 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1245 schain = hammer2_chain_lookup(&parent, &key_dummy,
1246 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
1248 hammer2_chain_lookup_done(parent);
1249 if (schain == NULL) {
1250 kprintf("hammer2_mount: invalid super-root\n");
1251 hammer2_unmount_helper(mp, NULL, hmp);
1252 lockmgr(&hammer2_mntlk, LK_RELEASE);
1253 hammer2_vfs_unmount(mp, MNT_FORCE);
1256 if (schain->error) {
1257 kprintf("hammer2_mount: error %s reading super-root\n",
1258 hammer2_error_str(schain->error));
1259 hammer2_chain_unlock(schain);
1260 hammer2_chain_drop(schain);
1262 hammer2_unmount_helper(mp, NULL, hmp);
1263 lockmgr(&hammer2_mntlk, LK_RELEASE);
1264 hammer2_vfs_unmount(mp, MNT_FORCE);
1269 * The super-root always uses an inode_tid of 1 when
1272 spmp->inode_tid = 1;
1273 spmp->modify_tid = schain->bref.modify_tid + 1;
1276 * Sanity-check schain's pmp and finish initialization.
1277 * Any chain belonging to the super-root topology should
1278 * have a NULL pmp (not even set to spmp).
1280 ripdata = &hammer2_chain_rdata(schain)->ipdata;
1281 KKASSERT(schain->pmp == NULL);
1282 spmp->pfs_clid = ripdata->meta.pfs_clid;
1285 * Replace the dummy spmp->iroot with a real one. It's
1286 * easier to just do a wholesale replacement than to try
1287 * to update the chain and fixup the iroot fields.
1289 * The returned inode is locked with the supplied cluster.
1291 hammer2_dummy_xop_from_chain(&xop, schain);
1292 hammer2_inode_drop(spmp->iroot);
1294 spmp->iroot = hammer2_inode_get(spmp, NULL, &xop, -1);
1295 spmp->spmp_hmp = hmp;
1296 spmp->pfs_types[0] = ripdata->meta.pfs_type;
1297 spmp->pfs_hmps[0] = hmp;
1298 hammer2_inode_ref(spmp->iroot);
1299 hammer2_inode_unlock(spmp->iroot);
1300 hammer2_cluster_unlock(&xop.cluster);
1301 hammer2_chain_drop(schain);
1302 /* do not call hammer2_cluster_drop() on an embedded cluster */
1303 schain = NULL; /* now invalid */
1304 /* leave spmp->iroot with one ref */
1306 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1307 error = hammer2_recovery(hmp);
1309 error |= hammer2_fixup_pfses(hmp);
1310 /* XXX do something with error */
1312 hammer2_update_pmps(hmp);
1313 hammer2_iocom_init(hmp);
1314 hammer2_bulkfree_init(hmp);
1317 * Ref the cluster management messaging descriptor. The mount
1318 * program deals with the other end of the communications pipe.
1320 * Root mounts typically do not supply one.
1322 if (info.cluster_fd >= 0) {
1323 fp = holdfp(curthread, info.cluster_fd, -1);
1325 hammer2_cluster_reconnect(hmp, fp);
1327 kprintf("hammer2_mount: bad cluster_fd!\n");
1332 if (info.hflags & HMNT2_DEVFLAGS) {
1333 kprintf("hammer2: Warning: mount flags pertaining "
1334 "to the whole device may only be specified "
1335 "on the first mount of the device: %08x\n",
1336 info.hflags & HMNT2_DEVFLAGS);
1341 * Force local mount (disassociate all PFSs from their clusters).
1342 * Used primarily for debugging.
1344 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
1347 * Lookup the mount point under the media-localized super-root.
1348 * Scanning hammer2_pfslist doesn't help us because it represents
1349 * PFS cluster ids which can aggregate several named PFSs together.
1351 * cluster->pmp will incorrectly point to spmp and must be fixed
1354 hammer2_inode_lock(spmp->iroot, 0);
1355 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1356 lhc = hammer2_dirhash(label, strlen(label));
1357 chain = hammer2_chain_lookup(&parent, &key_next,
1358 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1361 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1362 strcmp(label, chain->data->ipdata.filename) == 0) {
1365 chain = hammer2_chain_next(&parent, chain, &key_next,
1367 lhc + HAMMER2_DIRHASH_LOMASK,
1371 hammer2_chain_unlock(parent);
1372 hammer2_chain_drop(parent);
1374 hammer2_inode_unlock(spmp->iroot);
1377 * PFS could not be found?
1379 if (chain == NULL) {
1381 kprintf("hammer2_mount: PFS label I/O error\n");
1383 kprintf("hammer2_mount: PFS label not found\n");
1384 hammer2_unmount_helper(mp, NULL, hmp);
1385 lockmgr(&hammer2_mntlk, LK_RELEASE);
1386 hammer2_vfs_unmount(mp, MNT_FORCE);
1392 * Acquire the pmp structure (it should have already been allocated
1393 * via hammer2_update_pmps() so do not pass cluster in to add to
1394 * available chains).
1396 * Check if the cluster has already been mounted. A cluster can
1397 * only be mounted once, use null mounts to mount additional copies.
1400 kprintf("hammer2_mount: PFS label I/O error\n");
1402 ripdata = &chain->data->ipdata;
1404 pmp = hammer2_pfsalloc(NULL, ripdata,
1405 bref.modify_tid, force_local);
1407 hammer2_chain_unlock(chain);
1408 hammer2_chain_drop(chain);
1413 kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
1416 kprintf("hammer2_mount: PFS already mounted!\n");
1417 hammer2_unmount_helper(mp, NULL, hmp);
1418 lockmgr(&hammer2_mntlk, LK_RELEASE);
1419 hammer2_vfs_unmount(mp, MNT_FORCE);
1424 pmp->hflags = info.hflags;
1425 mp->mnt_flag |= MNT_LOCAL;
1426 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
1427 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
1430 * required mount structure initializations
1432 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1433 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1435 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1436 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1441 mp->mnt_iosize_max = MAXPHYS;
1444 * Connect up mount pointers.
1446 hammer2_mount_helper(mp, pmp);
1448 lockmgr(&hammer2_mntlk, LK_RELEASE);
1454 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1455 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1456 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1459 copyinstr(info.volume, mp->mnt_stat.f_mntfromname,
1460 MNAMELEN - 1, &size);
1461 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1462 } /* else root mount, already in there */
1464 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1466 copyinstr(path, mp->mnt_stat.f_mntonname,
1467 sizeof(mp->mnt_stat.f_mntonname) - 1,
1471 mp->mnt_stat.f_mntonname[0] = '/';
1475 * Initial statfs to prime mnt_stat.
1477 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1483 * Scan PFSs under the super-root and create hammer2_pfs structures.
1487 hammer2_update_pmps(hammer2_dev_t *hmp)
1489 const hammer2_inode_data_t *ripdata;
1490 hammer2_chain_t *parent;
1491 hammer2_chain_t *chain;
1492 hammer2_blockref_t bref;
1493 hammer2_dev_t *force_local;
1494 hammer2_pfs_t *spmp;
1496 hammer2_key_t key_next;
1500 * Force local mount (disassociate all PFSs from their clusters).
1501 * Used primarily for debugging.
1503 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
1506 * Lookup mount point under the media-localized super-root.
1508 * cluster->pmp will incorrectly point to spmp and must be fixed
1512 hammer2_inode_lock(spmp->iroot, 0);
1513 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1514 chain = hammer2_chain_lookup(&parent, &key_next,
1515 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
1518 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE)
1521 kprintf("I/O error scanning PFS labels\n");
1523 ripdata = &chain->data->ipdata;
1526 pmp = hammer2_pfsalloc(chain, ripdata,
1527 bref.modify_tid, force_local);
1529 chain = hammer2_chain_next(&parent, chain, &key_next,
1530 key_next, HAMMER2_KEY_MAX,
1534 hammer2_chain_unlock(parent);
1535 hammer2_chain_drop(parent);
1537 hammer2_inode_unlock(spmp->iroot);
1542 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused,
1543 struct vnode *devvp, struct ucred *cred)
1547 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1548 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1549 VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL);
1551 error = hammer2_recovery(hmp);
1553 error |= hammer2_fixup_pfses(hmp);
1554 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1556 VOP_CLOSE(devvp, FREAD, NULL);
1559 VOP_CLOSE(devvp, FREAD | FWRITE, NULL);
1570 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1581 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1584 * If mount initialization proceeded far enough we must flush
1585 * its vnodes and sync the underlying mount points. Three syncs
1586 * are required to fully flush the filesystem (freemap updates lag
1587 * by one flush, and one extra for safety).
1589 if (mntflags & MNT_FORCE)
1594 error = vflush(mp, 0, flags);
1597 hammer2_vfs_sync(mp, MNT_WAIT);
1598 hammer2_vfs_sync(mp, MNT_WAIT);
1599 hammer2_vfs_sync(mp, MNT_WAIT);
1603 * Cleanup the frontend support XOPS threads
1605 hammer2_xop_helper_cleanup(pmp);
1608 hammer2_unmount_helper(mp, pmp, NULL);
1612 lockmgr(&hammer2_mntlk, LK_RELEASE);
1618 * Mount helper, hook the system mount into our PFS.
1619 * The mount lock is held.
1621 * We must bump the mount_count on related devices for any
1626 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1628 hammer2_cluster_t *cluster;
1629 hammer2_chain_t *rchain;
1632 mp->mnt_data = (qaddr_t)pmp;
1636 * After pmp->mp is set we have to adjust hmp->mount_count.
1638 cluster = &pmp->iroot->cluster;
1639 for (i = 0; i < cluster->nchains; ++i) {
1640 rchain = cluster->array[i].chain;
1643 ++rchain->hmp->mount_count;
1647 * Create missing Xop threads
1649 hammer2_xop_helper_create(pmp);
1653 * Mount helper, unhook the system mount from our PFS.
1654 * The mount lock is held.
1656 * If hmp is supplied a mount responsible for being the first to open
1657 * the block device failed and the block device and all PFSs using the
1658 * block device must be cleaned up.
1660 * If pmp is supplied multiple devices might be backing the PFS and each
1661 * must be disconnected. This might not be the last PFS using some of the
1662 * underlying devices. Also, we have to adjust our hmp->mount_count
1663 * accounting for the devices backing the pmp which is now undergoing an
1668 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1670 hammer2_cluster_t *cluster;
1671 hammer2_chain_t *rchain;
1672 struct vnode *devvp;
1678 * If no device supplied this is a high-level unmount and we have to
1679 * to disconnect the mount, adjust mount_count, and locate devices
1680 * that might now have no mounts.
1683 KKASSERT(hmp == NULL);
1684 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1686 mp->mnt_data = NULL;
1689 * After pmp->mp is cleared we have to account for
1692 cluster = &pmp->iroot->cluster;
1693 for (i = 0; i < cluster->nchains; ++i) {
1694 rchain = cluster->array[i].chain;
1697 --rchain->hmp->mount_count;
1698 /* scrapping hmp now may invalidate the pmp */
1701 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1702 if (hmp->mount_count == 0) {
1703 hammer2_unmount_helper(NULL, NULL, hmp);
1711 * Try to terminate the block device. We can't terminate it if
1712 * there are still PFSs referencing it.
1714 if (hmp->mount_count)
1718 * Decomission the network before we start messing with the
1721 hammer2_iocom_uninit(hmp);
1723 hammer2_bulkfree_uninit(hmp);
1724 hammer2_pfsfree_scan(hmp, 0);
1725 hammer2_dev_exlock(hmp); /* XXX order */
1728 * Cycle the volume data lock as a safety (probably not needed any
1729 * more). To ensure everything is out we need to flush at least
1730 * three times. (1) The running of the sideq can dirty the
1731 * filesystem, (2) A normal flush can dirty the freemap, and
1732 * (3) ensure that the freemap is fully synchronized.
1734 * The next mount's recovery scan can clean everything up but we want
1735 * to leave the filesystem in a 100% clean state on a normal unmount.
1738 hammer2_voldata_lock(hmp);
1739 hammer2_voldata_unlock(hmp);
1743 * Flush whatever is left. Unmounted but modified PFS's might still
1744 * have some dirty chains on them.
1746 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1747 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1749 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1750 hammer2_voldata_modify(hmp);
1751 hammer2_flush(&hmp->fchain, HAMMER2_FLUSH_TOP |
1754 hammer2_chain_unlock(&hmp->fchain);
1756 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1757 hammer2_flush(&hmp->vchain, HAMMER2_FLUSH_TOP |
1760 hammer2_chain_unlock(&hmp->vchain);
1762 if ((hmp->vchain.flags | hmp->fchain.flags) &
1763 HAMMER2_CHAIN_FLUSH_MASK) {
1764 kprintf("hammer2_unmount: chains left over "
1765 "after final sync\n");
1766 kprintf(" vchain %08x\n", hmp->vchain.flags);
1767 kprintf(" fchain %08x\n", hmp->fchain.flags);
1769 if (hammer2_debug & 0x0010)
1770 Debugger("entered debugger");
1773 hammer2_pfsfree_scan(hmp, 1);
1775 KKASSERT(hmp->spmp == NULL);
1778 * Finish up with the device vnode
1780 if ((devvp = hmp->devvp) != NULL) {
1782 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1783 kprintf("hammer2_unmount(A): devvp %s rbdirty %p ronly=%d\n",
1784 hmp->devrepname, RB_ROOT(&devvp->v_rbdirty_tree),
1786 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1787 kprintf("hammer2_unmount(B): devvp %s rbdirty %p\n",
1788 hmp->devrepname, RB_ROOT(&devvp->v_rbdirty_tree));
1790 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1797 * Clear vchain/fchain flags that might prevent final cleanup
1800 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1801 atomic_add_long(&hammer2_count_modified_chains, -1);
1802 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1803 hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1805 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1806 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_UPDATE);
1809 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1810 atomic_add_long(&hammer2_count_modified_chains, -1);
1811 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_MODIFIED);
1812 hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1814 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1815 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_UPDATE);
1819 * Final drop of embedded freemap root chain to
1820 * clean up fchain.core (fchain structure is not
1821 * flagged ALLOCATED so it is cleaned out and then
1824 hammer2_chain_drop(&hmp->fchain);
1827 * Final drop of embedded volume root chain to clean
1828 * up vchain.core (vchain structure is not flagged
1829 * ALLOCATED so it is cleaned out and then left to
1833 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v', (u_int)-1);
1835 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f', (u_int)-1);
1836 hammer2_dev_unlock(hmp);
1837 hammer2_chain_drop(&hmp->vchain);
1839 hammer2_io_cleanup(hmp, &hmp->iotree);
1840 if (hmp->iofree_count) {
1841 kprintf("io_cleanup: %d I/O's left hanging\n",
1845 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1846 kmalloc_destroy(&hmp->mchain);
1847 kfree(hmp, M_HAMMER2);
1851 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1852 ino_t ino, struct vnode **vpp)
1854 hammer2_xop_lookup_t *xop;
1856 hammer2_inode_t *ip;
1860 inum = (hammer2_tid_t)ino & HAMMER2_DIRHASH_USERMSK;
1866 * Easy if we already have it cached
1868 ip = hammer2_inode_lookup(pmp, inum);
1870 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
1871 *vpp = hammer2_igetv(ip, &error);
1872 hammer2_inode_unlock(ip);
1873 hammer2_inode_drop(ip); /* from lookup */
1879 * Otherwise we have to find the inode
1881 xop = hammer2_xop_alloc(pmp->iroot, 0);
1883 hammer2_xop_start(&xop->head, &hammer2_lookup_desc);
1884 error = hammer2_xop_collect(&xop->head, 0);
1887 ip = hammer2_inode_get(pmp, NULL, &xop->head, -1);
1888 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1891 *vpp = hammer2_igetv(ip, &error);
1892 hammer2_inode_unlock(ip);
1902 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1909 if (pmp->iroot == NULL) {
1910 kprintf("hammer2 (%s): no root inode\n",
1911 mp->mnt_stat.f_mntfromname);
1917 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1919 while (pmp->inode_tid == 0) {
1920 hammer2_xop_ipcluster_t *xop;
1921 const hammer2_inode_meta_t *meta;
1923 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1924 hammer2_xop_start(&xop->head, &hammer2_ipcluster_desc);
1925 error = hammer2_xop_collect(&xop->head, 0);
1928 meta = &hammer2_xop_gdata(&xop->head)->ipdata.meta;
1929 pmp->iroot->meta = *meta;
1930 pmp->inode_tid = meta->pfs_inum + 1;
1931 hammer2_xop_pdata(&xop->head);
1934 if (pmp->inode_tid < HAMMER2_INODE_START)
1935 pmp->inode_tid = HAMMER2_INODE_START;
1937 xop->head.cluster.focus->bref.modify_tid + 1;
1939 kprintf("PFS: Starting inode %jd\n",
1940 (intmax_t)pmp->inode_tid);
1941 kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
1942 pmp->inode_tid, pmp->modify_tid);
1944 wakeup(&pmp->iroot);
1946 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1949 * Prime the mount info.
1951 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL);
1958 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1959 hammer2_inode_unlock(pmp->iroot);
1960 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz);
1961 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1967 hammer2_inode_unlock(pmp->iroot);
1970 vp = hammer2_igetv(pmp->iroot, &error);
1971 hammer2_inode_unlock(pmp->iroot);
1981 * XXX incorporate ipdata->meta.inode_quota and data_quota
1985 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1989 hammer2_blockref_t bref;
1994 * NOTE: iroot might not have validated the cluster yet.
1998 bzero(&tmp, sizeof(tmp));
2000 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
2001 hmp = pmp->pfs_hmps[i];
2004 if (pmp->iroot->cluster.array[i].chain)
2005 bref = pmp->iroot->cluster.array[i].chain->bref;
2007 bzero(&bref, sizeof(bref));
2009 tmp.f_files = bref.embed.stats.inode_count;
2011 tmp.f_blocks = hmp->voldata.allocator_size /
2012 mp->mnt_vstat.f_bsize;
2013 tmp.f_bfree = hmp->voldata.allocator_free /
2014 mp->mnt_vstat.f_bsize;
2015 tmp.f_bavail = tmp.f_bfree;
2017 if (cred && cred->cr_uid != 0) {
2021 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize;
2022 tmp.f_blocks -= adj;
2024 tmp.f_bavail -= adj;
2027 mp->mnt_stat.f_blocks = tmp.f_blocks;
2028 mp->mnt_stat.f_bfree = tmp.f_bfree;
2029 mp->mnt_stat.f_bavail = tmp.f_bavail;
2030 mp->mnt_stat.f_files = tmp.f_files;
2031 mp->mnt_stat.f_ffree = tmp.f_ffree;
2033 *sbp = mp->mnt_stat;
2040 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
2044 hammer2_blockref_t bref;
2049 * NOTE: iroot might not have validated the cluster yet.
2052 bzero(&tmp, sizeof(tmp));
2054 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
2055 hmp = pmp->pfs_hmps[i];
2058 if (pmp->iroot->cluster.array[i].chain)
2059 bref = pmp->iroot->cluster.array[i].chain->bref;
2061 bzero(&bref, sizeof(bref));
2063 tmp.f_files = bref.embed.stats.inode_count;
2065 tmp.f_blocks = hmp->voldata.allocator_size /
2066 mp->mnt_vstat.f_bsize;
2067 tmp.f_bfree = hmp->voldata.allocator_free /
2068 mp->mnt_vstat.f_bsize;
2069 tmp.f_bavail = tmp.f_bfree;
2071 if (cred && cred->cr_uid != 0) {
2075 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize;
2076 tmp.f_blocks -= adj;
2078 tmp.f_bavail -= adj;
2081 mp->mnt_vstat.f_blocks = tmp.f_blocks;
2082 mp->mnt_vstat.f_bfree = tmp.f_bfree;
2083 mp->mnt_vstat.f_bavail = tmp.f_bavail;
2084 mp->mnt_vstat.f_files = tmp.f_files;
2085 mp->mnt_vstat.f_ffree = tmp.f_ffree;
2087 *sbp = mp->mnt_vstat;
2093 * Mount-time recovery (RW mounts)
2095 * Updates to the free block table are allowed to lag flushes by one
2096 * transaction. In case of a crash, then on a fresh mount we must do an
2097 * incremental scan of the last committed transaction id and make sure that
2098 * all related blocks have been marked allocated.
2100 * The super-root topology and each PFS has its own transaction id domain,
2101 * so we must track PFS boundary transitions.
2103 struct hammer2_recovery_elm {
2104 TAILQ_ENTRY(hammer2_recovery_elm) entry;
2105 hammer2_chain_t *chain;
2106 hammer2_tid_t sync_tid;
2109 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
2111 struct hammer2_recovery_info {
2112 struct hammer2_recovery_list list;
2117 static int hammer2_recovery_scan(hammer2_dev_t *hmp,
2118 hammer2_chain_t *parent,
2119 struct hammer2_recovery_info *info,
2120 hammer2_tid_t sync_tid);
2122 #define HAMMER2_RECOVERY_MAXDEPTH 10
2126 hammer2_recovery(hammer2_dev_t *hmp)
2128 struct hammer2_recovery_info info;
2129 struct hammer2_recovery_elm *elm;
2130 hammer2_chain_t *parent;
2131 hammer2_tid_t sync_tid;
2132 hammer2_tid_t mirror_tid;
2135 hammer2_trans_init(hmp->spmp, 0);
2137 sync_tid = hmp->voldata.freemap_tid;
2138 mirror_tid = hmp->voldata.mirror_tid;
2140 kprintf("hammer2 mount \"%s\": ", hmp->devrepname);
2141 if (sync_tid >= mirror_tid) {
2142 kprintf(" no recovery needed\n");
2144 kprintf(" freemap recovery %016jx-%016jx\n",
2145 sync_tid + 1, mirror_tid);
2148 TAILQ_INIT(&info.list);
2150 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
2151 error = hammer2_recovery_scan(hmp, parent, &info, sync_tid);
2152 hammer2_chain_lookup_done(parent);
2154 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
2155 TAILQ_REMOVE(&info.list, elm, entry);
2156 parent = elm->chain;
2157 sync_tid = elm->sync_tid;
2158 kfree(elm, M_HAMMER2);
2160 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2161 error |= hammer2_recovery_scan(hmp, parent, &info,
2162 hmp->voldata.freemap_tid);
2163 hammer2_chain_unlock(parent);
2164 hammer2_chain_drop(parent); /* drop elm->chain ref */
2167 hammer2_trans_done(hmp->spmp, 0);
2174 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent,
2175 struct hammer2_recovery_info *info,
2176 hammer2_tid_t sync_tid)
2178 const hammer2_inode_data_t *ripdata;
2179 hammer2_chain_t *chain;
2180 hammer2_blockref_t bref;
2187 * Adjust freemap to ensure that the block(s) are marked allocated.
2189 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
2190 hammer2_freemap_adjust(hmp, &parent->bref,
2191 HAMMER2_FREEMAP_DORECOVER);
2195 * Check type for recursive scan
2197 switch(parent->bref.type) {
2198 case HAMMER2_BREF_TYPE_VOLUME:
2199 /* data already instantiated */
2201 case HAMMER2_BREF_TYPE_INODE:
2203 * Must instantiate data for DIRECTDATA test and also
2206 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2207 ripdata = &hammer2_chain_rdata(parent)->ipdata;
2208 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2209 /* not applicable to recovery scan */
2210 hammer2_chain_unlock(parent);
2213 hammer2_chain_unlock(parent);
2215 case HAMMER2_BREF_TYPE_INDIRECT:
2217 * Must instantiate data for recursion
2219 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2220 hammer2_chain_unlock(parent);
2222 case HAMMER2_BREF_TYPE_DIRENT:
2223 case HAMMER2_BREF_TYPE_DATA:
2224 case HAMMER2_BREF_TYPE_FREEMAP:
2225 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2226 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2227 /* not applicable to recovery scan */
2231 return HAMMER2_ERROR_BADBREF;
2235 * Defer operation if depth limit reached or if we are crossing a
2238 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
2239 struct hammer2_recovery_elm *elm;
2241 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2242 elm->chain = parent;
2243 elm->sync_tid = sync_tid;
2244 hammer2_chain_ref(parent);
2245 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2246 /* unlocked by caller */
2253 * Recursive scan of the last flushed transaction only. We are
2254 * doing this without pmp assignments so don't leave the chains
2255 * hanging around after we are done with them.
2257 * error Cumulative error this level only
2258 * rup_error Cumulative error for recursion
2259 * tmp_error Specific non-cumulative recursion error
2267 error |= hammer2_chain_scan(parent, &chain, &bref,
2269 HAMMER2_LOOKUP_NODATA);
2272 * Problem during scan or EOF
2280 if (chain == NULL) {
2281 if (bref.mirror_tid > sync_tid) {
2282 hammer2_freemap_adjust(hmp, &bref,
2283 HAMMER2_FREEMAP_DORECOVER);
2289 * This may or may not be a recursive node.
2291 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2292 if (bref.mirror_tid > sync_tid) {
2294 tmp_error = hammer2_recovery_scan(hmp, chain,
2302 * Flush the recovery at the PFS boundary to stage it for
2303 * the final flush of the super-root topology.
2305 if (tmp_error == 0 &&
2306 (bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
2307 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
2308 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
2311 rup_error |= tmp_error;
2313 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
2317 * This fixes up an error introduced in earlier H2 implementations where
2318 * moving a PFS inode into an indirect block wound up causing the
2319 * HAMMER2_BREF_FLAG_PFSROOT flag in the bref to get cleared.
2323 hammer2_fixup_pfses(hammer2_dev_t *hmp)
2325 const hammer2_inode_data_t *ripdata;
2326 hammer2_chain_t *parent;
2327 hammer2_chain_t *chain;
2328 hammer2_key_t key_next;
2329 hammer2_pfs_t *spmp;
2335 * Lookup mount point under the media-localized super-root.
2337 * cluster->pmp will incorrectly point to spmp and must be fixed
2341 hammer2_inode_lock(spmp->iroot, 0);
2342 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
2343 chain = hammer2_chain_lookup(&parent, &key_next,
2344 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
2347 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE)
2350 kprintf("I/O error scanning PFS labels\n");
2351 error |= chain->error;
2352 } else if ((chain->bref.flags &
2353 HAMMER2_BREF_FLAG_PFSROOT) == 0) {
2356 ripdata = &chain->data->ipdata;
2357 hammer2_trans_init(hmp->spmp, 0);
2358 error2 = hammer2_chain_modify(chain,
2359 chain->bref.modify_tid,
2362 kprintf("hammer2: Correct mis-flagged PFS %s\n",
2364 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2368 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
2370 hammer2_trans_done(hmp->spmp, 0);
2372 chain = hammer2_chain_next(&parent, chain, &key_next,
2373 key_next, HAMMER2_KEY_MAX,
2377 hammer2_chain_unlock(parent);
2378 hammer2_chain_drop(parent);
2380 hammer2_inode_unlock(spmp->iroot);
2386 * Sync a mount point; this is called periodically on a per-mount basis from
2387 * the filesystem syncer, and whenever a user issues a sync.
2390 hammer2_vfs_sync(struct mount *mp, int waitfor)
2392 hammer2_xop_flush_t *xop;
2393 struct hammer2_sync_info info;
2394 hammer2_inode_t *iroot;
2402 KKASSERT(iroot->pmp == pmp);
2405 * We can't acquire locks on existing vnodes while in a transaction
2406 * without risking a deadlock. This assumes that vfsync() can be
2407 * called without the vnode locked (which it can in DragonFly).
2408 * Otherwise we'd have to implement a multi-pass or flag the lock
2409 * failures and retry.
2411 * The reclamation code interlocks with the sync list's token
2412 * (by removing the vnode from the scan list) before unlocking
2413 * the inode, giving us time to ref the inode.
2415 /*flags = VMSC_GETVP;*/
2417 if (waitfor & MNT_LAZY)
2418 flags |= VMSC_ONEPASS;
2421 * Flush vnodes individually using a normal transaction to avoid
2422 * stalling any concurrent operations. This will flush the related
2423 * buffer cache buffers and inodes to the media.
2425 * For efficiency do an async pass before making sure with a
2426 * synchronous pass on all related buffer cache buffers.
2428 hammer2_trans_init(pmp, 0);
2432 info.waitfor = MNT_NOWAIT;
2434 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2437 * Now do two passes making sure we get everything. The first pass
2438 * vfsync()s dirty vnodes. The second pass waits for their I/O's
2439 * to finish and cleans up the dirty flag on the vnode.
2442 info.waitfor = MNT_WAIT;
2443 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2446 info.waitfor = MNT_WAIT;
2447 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2450 * We must also run the sideq to handle any disconnected inodes
2451 * as the vnode scan will not see these.
2453 hammer2_inode_run_sideq(pmp, 1);
2454 hammer2_trans_done(pmp, 0);
2457 * Start our flush transaction and flush the root topology down to
2458 * the inodes, but not the inodes themselves (which we already flushed
2459 * above). Any concurrent activity effecting inode contents will not
2461 * The flush sequence will
2463 * NOTE! It is still possible for the paging code to push pages
2464 * out via a UIO_NOCOPY hammer2_vop_write() during the main
2467 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH);
2470 * sync dirty vnodes again while in the flush transaction. This is
2471 * currently an expensive shim to makre sure the logical topology is
2472 * completely consistent before we flush the volume header.
2475 info.waitfor = MNT_WAIT;
2476 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2479 info.waitfor = MNT_WAIT;
2480 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2483 * Use the XOP interface to concurrently flush all nodes to
2484 * synchronize the PFSROOT subtopology to the media. A standard
2485 * end-of-scan ENOENT error indicates cluster sufficiency.
2487 * Note that this flush will not be visible on crash recovery until
2488 * we flush the super-root topology in the next loop.
2490 * XXX For now wait for all flushes to complete.
2494 * If unmounting try to flush everything including any
2495 * sub-trees under inodes, just in case there is dangling
2496 * modified data, as a safety. Otherwise just flush up to
2497 * the inodes in this stage.
2499 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
2500 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING |
2501 HAMMER2_XOP_VOLHDR);
2503 xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING |
2504 HAMMER2_XOP_INODE_STOP |
2505 HAMMER2_XOP_VOLHDR);
2507 hammer2_xop_start(&xop->head, &hammer2_inode_flush_desc);
2508 error = hammer2_xop_collect(&xop->head,
2509 HAMMER2_XOP_COLLECT_WAITALL);
2510 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2511 if (error == HAMMER2_ERROR_ENOENT)
2514 error = hammer2_error_to_errno(error);
2518 hammer2_trans_done(pmp, 0);
2526 * Note that we ignore the tranasction mtid we got above. Instead,
2527 * each vfsync below will ultimately get its own via TRANS_BUFCACHE
2530 * WARNING! The frontend might be waiting on chnmem (limit_dirty_chains)
2531 * while holding a vnode locked. When this situation occurs we cannot
2532 * safely test whether it is ok to clear the dirty bit on the vnode.
2533 * However, we can still flush the inode's topology.
2536 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2538 struct hammer2_sync_info *info = data;
2539 hammer2_inode_t *ip;
2543 * Degenerate cases. Note that ip == NULL typically means the
2544 * syncer vnode itself and we don't want to vclrisdirty() in that
2551 if (vp->v_type == VNON || vp->v_type == VBAD) {
2557 * Synchronize the buffer cche and inode meta-data to the backing
2560 * vfsync is not necessarily synchronous, so it is best NOT to try
2561 * to flush the backing topology to media at this point.
2563 hammer2_inode_ref(ip);
2564 if ((ip->flags & (HAMMER2_INODE_RESIZED|HAMMER2_INODE_MODIFIED)) ||
2565 !RB_EMPTY(&vp->v_rbdirty_tree)) {
2566 if (info->pass == 1)
2567 vfsync(vp, info->waitfor, 1, NULL, NULL);
2569 bio_track_wait(&vp->v_track_write, 0, 0);
2571 if (info->pass == 2 && (vp->v_flag & VISDIRTY)) {
2573 * v_token is needed to interlock v_rbdirty_tree.
2575 lwkt_gettoken(&vp->v_token);
2576 hammer2_inode_lock(ip, 0);
2577 hammer2_inode_chain_sync(ip);
2578 hammer2_inode_chain_flush(ip);
2579 if ((ip->flags & (HAMMER2_INODE_MODIFIED |
2580 HAMMER2_INODE_RESIZED |
2581 HAMMER2_INODE_DIRTYDATA)) == 0 &&
2582 RB_EMPTY(&vp->v_rbdirty_tree) &&
2583 !bio_track_active(&vp->v_track_write)) {
2586 hammer2_inode_unlock(ip);
2587 lwkt_reltoken(&vp->v_token);
2589 hammer2_inode_drop(ip);
2593 info->error = error;
2600 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2602 hammer2_inode_t *ip;
2604 KKASSERT(MAXFIDSZ >= 16);
2606 fhp->fid_len = offsetof(struct fid, fid_data[16]);
2608 ((hammer2_tid_t *)fhp->fid_data)[0] = ip->meta.inum;
2609 ((hammer2_tid_t *)fhp->fid_data)[1] = 0;
2616 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2617 struct fid *fhp, struct vnode **vpp)
2624 inum = ((hammer2_tid_t *)fhp->fid_data)[0] & HAMMER2_DIRHASH_USERMSK;
2627 error = hammer2_vfs_root(mp, vpp);
2629 error = hammer2_vfs_vget(mp, NULL, inum, vpp);
2634 kprintf("fhtovp: %016jx -> %p, %d\n", inum, *vpp, error);
2640 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2641 int *exflagsp, struct ucred **credanonp)
2648 np = vfs_export_lookup(mp, &pmp->export, nam);
2650 *exflagsp = np->netc_exflags;
2651 *credanonp = &np->netc_anon;
2660 * Support code for hammer2_vfs_mount(). Read, verify, and install the volume
2661 * header into the HMP
2663 * XXX read four volhdrs and use the one with the highest TID whos CRC
2668 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2669 * nonexistant locations.
2671 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2675 hammer2_install_volume_header(hammer2_dev_t *hmp)
2677 hammer2_volume_data_t *vd;
2679 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2691 * There are up to 4 copies of the volume header (syncs iterate
2692 * between them so there is no single master). We don't trust the
2693 * volu_size field so we don't know precisely how large the filesystem
2694 * is, so depend on the OS to return an error if we go beyond the
2695 * block device's EOF.
2697 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2698 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2699 HAMMER2_VOLUME_BYTES, &bp);
2706 vd = (struct hammer2_volume_data *) bp->b_data;
2707 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2708 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2714 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2715 /* XXX: Reversed-endianness filesystem */
2716 kprintf("hammer2: reverse-endian filesystem detected");
2722 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2723 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2724 HAMMER2_VOLUME_ICRC0_SIZE);
2725 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2726 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2727 HAMMER2_VOLUME_ICRC1_SIZE);
2728 if ((crc0 != crc) || (bcrc0 != bcrc)) {
2729 kprintf("hammer2 volume header crc "
2730 "mismatch copy #%d %08x/%08x\n",
2737 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2746 hmp->volsync = hmp->voldata;
2747 hmp->free_reserved = hmp->voldata.allocator_size / 20;
2749 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2750 kprintf("hammer2: using volume header #%d\n",
2755 kprintf("hammer2: no valid volume headers found!\n");
2761 * This handles hysteresis on regular file flushes. Because the BIOs are
2762 * routed to a thread it is possible for an excessive number to build up
2763 * and cause long front-end stalls long before the runningbuffspace limit
2764 * is hit, so we implement hammer2_flush_pipe to control the
2767 * This is a particular problem when compression is used.
2770 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2772 atomic_add_int(&pmp->count_lwinprog, 1);
2776 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2780 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2781 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2782 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2783 atomic_clear_int(&pmp->count_lwinprog,
2784 HAMMER2_LWINPROG_WAITING);
2785 wakeup(&pmp->count_lwinprog);
2787 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) &&
2788 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) {
2789 atomic_clear_int(&pmp->count_lwinprog,
2790 HAMMER2_LWINPROG_WAITING0);
2791 wakeup(&pmp->count_lwinprog);
2796 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe)
2799 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING :
2800 HAMMER2_LWINPROG_WAITING0;
2803 lwinprog = pmp->count_lwinprog;
2805 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2807 tsleep_interlock(&pmp->count_lwinprog, 0);
2808 atomic_set_int(&pmp->count_lwinprog, lwflag);
2809 lwinprog = pmp->count_lwinprog;
2810 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2812 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2817 * Attempt to proactively fsync dirty vnodes if we have too many. This
2818 * solves an issue where the kernel syncer thread can get seriously behind
2819 * when multiple user processes/threads are furiously modifying inodes.
2820 * This situation can occur on slow storage and is only limited by
2821 * kern.maxvnodes without the moderation code below. It is made worse
2822 * when the device buffers underlying the modified inodes (which are clean)
2823 * get evicted before the flush can occur, forcing a re-read.
2825 * We do not want sysads to feel that they have to torpedo kern.maxvnodes
2826 * to solve this problem, so we implement vfs.hammer2.limit_dirty_inodes
2827 * (per-mount-basis) and default it to something reasonable.
2830 hammer2_pfs_moderate(hammer2_inode_t *ip, int always_moderate)
2832 hammer2_pfs_t *pmp = ip->pmp;
2833 struct mount *mp = pmp->mp;
2835 if (mp && vn_syncer_count(mp) > hammer2_limit_dirty_inodes) {
2841 * Manage excessive memory resource use for chain and related
2844 * Called without any inode locks or transaction locks. VNodes
2845 * might be locked by the kernel in the call stack.
2848 hammer2_pfs_memory_wait(hammer2_inode_t *ip, int always_moderate)
2850 hammer2_pfs_t *pmp = ip->pmp;
2859 * Moderate the number of dirty inodes
2861 hammer2_pfs_moderate(ip, always_moderate);
2864 * Atomic check condition and wait. Also do an early speedup of
2865 * the syncer to try to avoid hitting the wait.
2868 waiting = pmp->inmem_dirty_chains;
2870 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2872 limit = pmp->mp->mnt_nvnodelistsize / 10;
2873 if (limit < hammer2_limit_dirty_chains)
2874 limit = hammer2_limit_dirty_chains;
2879 if ((int)(ticks - zzticks) > hz) {
2881 kprintf("count %ld %ld\n", count, limit);
2886 * Block if there are too many dirty chains present, wait
2887 * for the flush to clean some out.
2889 if (count > limit) {
2890 hammer2_pfs_moderate(ip, always_moderate);
2891 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2892 if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2894 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2895 if (ticks != pmp->speedup_ticks) {
2896 pmp->speedup_ticks = ticks;
2897 speedup_syncer(pmp->mp);
2899 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2902 continue; /* loop on success or fail */
2906 * Try to start an early flush before we are forced to block.
2908 if (count > limit * 5 / 10 &&
2909 ticks != pmp->speedup_ticks) {
2910 pmp->speedup_ticks = ticks;
2911 speedup_syncer(pmp->mp);
2918 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
2921 atomic_add_int(&pmp->inmem_dirty_chains, 1);
2926 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
2931 waiting = atomic_fetchadd_int(&pmp->inmem_dirty_chains, -1);
2932 /* don't need --waiting to test flag */
2933 if (waiting & HAMMER2_DIRTYCHAIN_WAITING) {
2934 atomic_clear_int(&pmp->inmem_dirty_chains,
2935 HAMMER2_DIRTYCHAIN_WAITING);
2936 wakeup(&pmp->inmem_dirty_chains);
2942 * Returns 0 if the filesystem has tons of free space
2943 * Returns 1 if the filesystem has less than 10% remaining
2944 * Returns 2 if the filesystem has less than 2%/5% (user/root) remaining.
2947 hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred)
2951 hammer2_off_t free_reserved;
2952 hammer2_off_t free_nominal;
2957 if (pmp->free_ticks == 0 || pmp->free_ticks != ticks) {
2958 free_reserved = HAMMER2_SEGSIZE;
2959 free_nominal = 0x7FFFFFFFFFFFFFFFLLU;
2960 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
2961 hmp = pmp->pfs_hmps[i];
2964 if (pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER &&
2965 pmp->pfs_types[i] != HAMMER2_PFSTYPE_SOFT_MASTER)
2968 if (free_nominal > hmp->voldata.allocator_free)
2969 free_nominal = hmp->voldata.allocator_free;
2970 if (free_reserved < hmp->free_reserved)
2971 free_reserved = hmp->free_reserved;
2977 pmp->free_reserved = free_reserved;
2978 pmp->free_nominal = free_nominal;
2979 pmp->free_ticks = ticks;
2981 free_reserved = pmp->free_reserved;
2982 free_nominal = pmp->free_nominal;
2984 if (cred && cred->cr_uid != 0) {
2985 if ((int64_t)(free_nominal - bytes) <
2986 (int64_t)free_reserved) {
2990 if ((int64_t)(free_nominal - bytes) <
2991 (int64_t)free_reserved / 2) {
2995 if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved * 2)
3004 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx,
3007 hammer2_chain_t *scan;
3008 hammer2_chain_t *parent;
3012 kprintf("%*.*s...\n", tab, tab, "");
3017 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
3019 chain, chain->bref.type,
3020 chain->bref.key, chain->bref.keybits,
3021 chain->bref.mirror_tid);
3023 kprintf("%*.*s [%08x] (%s) refs=%d",
3026 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
3027 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
3030 parent = chain->parent;
3032 kprintf("\n%*.*s p=%p [pflags %08x prefs %d",
3034 parent, parent->flags, parent->refs);
3035 if (RB_EMPTY(&chain->core.rbtree)) {
3039 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) {
3040 if ((scan->flags & flags) || flags == (u_int)-1) {
3041 hammer2_dump_chain(scan, tab + 4, countp, 'a',
3045 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
3046 kprintf("%*.*s}(%s)\n", tab, tab, "",
3047 chain->data->ipdata.filename);
3049 kprintf("%*.*s}\n", tab, tab, "");