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/mountctl.h>
51 #include <sys/dirent.h>
55 #include "hammer2_disk.h"
56 #include "hammer2_mount.h"
57 #include "hammer2_lz4.h"
59 #include "zlib/hammer2_zlib.h"
61 #define REPORT_REFS_ERRORS 1 /* XXX remove me */
63 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
65 struct hammer2_sync_info {
71 TAILQ_HEAD(hammer2_mntlist, hammer2_dev);
72 static struct hammer2_mntlist hammer2_mntlist;
74 struct hammer2_pfslist hammer2_pfslist;
75 struct hammer2_pfslist hammer2_spmplist;
76 struct lock hammer2_mntlk;
78 int hammer2_supported_version = HAMMER2_VOL_VERSION_DEFAULT;
80 int hammer2_xopgroups;
81 long hammer2_debug_inode;
82 int hammer2_cluster_meta_read = 1; /* physical read-ahead */
83 int hammer2_cluster_data_read = 4; /* physical read-ahead */
84 int hammer2_cluster_write = 0; /* physical write clustering */
85 int hammer2_dedup_enable = 1;
86 int hammer2_always_compress = 0; /* always try to compress */
87 int hammer2_flush_pipe = 100;
88 int hammer2_dio_count;
89 int hammer2_dio_limit = 256;
90 int hammer2_bulkfree_tps = 5000;
91 int hammer2_worker_rmask = 3;
92 long hammer2_chain_allocs;
93 long hammer2_limit_dirty_chains;
94 long hammer2_limit_dirty_inodes;
95 long hammer2_count_modified_chains;
96 long hammer2_iod_file_read;
97 long hammer2_iod_meta_read;
98 long hammer2_iod_indr_read;
99 long hammer2_iod_fmap_read;
100 long hammer2_iod_volu_read;
101 long hammer2_iod_file_write;
102 long hammer2_iod_file_wembed;
103 long hammer2_iod_file_wzero;
104 long hammer2_iod_file_wdedup;
105 long hammer2_iod_meta_write;
106 long hammer2_iod_indr_write;
107 long hammer2_iod_fmap_write;
108 long hammer2_iod_volu_write;
109 static long hammer2_iod_inode_creates;
110 static long hammer2_iod_inode_deletes;
112 long hammer2_process_icrc32;
113 long hammer2_process_xxhash64;
115 MALLOC_DECLARE(M_HAMMER2_CBUFFER);
116 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
117 "Buffer used for compression.");
119 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
120 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
121 "Buffer used for decompression.");
123 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, supported_version, CTLFLAG_RD,
126 &hammer2_supported_version, 0, "");
127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
128 &hammer2_debug, 0, "");
129 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, debug_inode, CTLFLAG_RW,
130 &hammer2_debug_inode, 0, "");
131 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_meta_read, CTLFLAG_RW,
132 &hammer2_cluster_meta_read, 0, "");
133 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_data_read, CTLFLAG_RW,
134 &hammer2_cluster_data_read, 0, "");
135 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_write, CTLFLAG_RW,
136 &hammer2_cluster_write, 0, "");
137 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dedup_enable, CTLFLAG_RW,
138 &hammer2_dedup_enable, 0, "");
139 SYSCTL_INT(_vfs_hammer2, OID_AUTO, always_compress, CTLFLAG_RW,
140 &hammer2_always_compress, 0, "");
141 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
142 &hammer2_flush_pipe, 0, "");
143 SYSCTL_INT(_vfs_hammer2, OID_AUTO, worker_rmask, CTLFLAG_RW,
144 &hammer2_worker_rmask, 0, "");
145 SYSCTL_INT(_vfs_hammer2, OID_AUTO, bulkfree_tps, CTLFLAG_RW,
146 &hammer2_bulkfree_tps, 0, "");
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, chain_allocs, CTLFLAG_RW,
148 &hammer2_chain_allocs, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
150 &hammer2_limit_dirty_chains, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_inodes, CTLFLAG_RW,
152 &hammer2_limit_dirty_inodes, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, count_modified_chains, CTLFLAG_RW,
154 &hammer2_count_modified_chains, 0, "");
155 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
156 &hammer2_dio_count, 0, "");
157 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_limit, CTLFLAG_RW,
158 &hammer2_dio_limit, 0, "");
160 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
161 &hammer2_iod_file_read, 0, "");
162 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
163 &hammer2_iod_meta_read, 0, "");
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
165 &hammer2_iod_indr_read, 0, "");
166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
167 &hammer2_iod_fmap_read, 0, "");
168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
169 &hammer2_iod_volu_read, 0, "");
171 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
172 &hammer2_iod_file_write, 0, "");
173 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW,
174 &hammer2_iod_file_wembed, 0, "");
175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW,
176 &hammer2_iod_file_wzero, 0, "");
177 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW,
178 &hammer2_iod_file_wdedup, 0, "");
179 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
180 &hammer2_iod_meta_write, 0, "");
181 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
182 &hammer2_iod_indr_write, 0, "");
183 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
184 &hammer2_iod_fmap_write, 0, "");
185 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
186 &hammer2_iod_volu_write, 0, "");
187 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_creates, CTLFLAG_RW,
188 &hammer2_iod_inode_creates, 0, "");
189 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_inode_deletes, CTLFLAG_RW,
190 &hammer2_iod_inode_deletes, 0, "");
192 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_icrc32, CTLFLAG_RW,
193 &hammer2_process_icrc32, 0, "");
194 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, process_xxhash64, CTLFLAG_RW,
195 &hammer2_process_xxhash64, 0, "");
197 static int hammer2_vfs_init(struct vfsconf *conf);
198 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
199 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
201 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
203 static int hammer2_recovery(hammer2_dev_t *hmp);
204 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
205 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
206 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
208 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
210 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
211 struct fid *fhp, struct vnode **vpp);
212 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
213 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
214 int *exflagsp, struct ucred **credanonp);
215 static int hammer2_vfs_modifying(struct mount *mp);
217 static void hammer2_update_pmps(hammer2_dev_t *hmp);
219 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
220 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
222 static int hammer2_fixup_pfses(hammer2_dev_t *hmp);
225 * HAMMER2 vfs operations.
227 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,
241 .vfs_modifying = hammer2_vfs_modifying
244 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
246 VFS_SET(hammer2_vfsops, hammer2, VFCF_MPSAFE);
247 MODULE_VERSION(hammer2, 1);
251 hammer2_vfs_init(struct vfsconf *conf)
253 static struct objcache_malloc_args margs_read;
254 static struct objcache_malloc_args margs_write;
255 static struct objcache_malloc_args margs_vop;
260 kmalloc_raise_limit(M_HAMMER2, 0); /* unlimited */
263 * hammer2_xopgroups must be even and is most optimal if
264 * 2 x ncpus so strategy functions can be queued to the same
267 hammer2_xopgroups = HAMMER2_XOPGROUPS_MIN;
268 if (hammer2_xopgroups < ncpus * 2)
269 hammer2_xopgroups = ncpus * 2;
272 * A large DIO cache is needed to retain dedup enablement masks.
273 * The bulkfree code clears related masks as part of the disk block
274 * recycling algorithm, preventing it from being used for a later
277 * NOTE: A large buffer cache can actually interfere with dedup
278 * operation because we dedup based on media physical buffers
279 * and not logical buffers. Try to make the DIO case large
280 * enough to avoid this problem, but also cap it.
282 hammer2_dio_limit = nbuf * 2;
283 if (hammer2_dio_limit > 100000)
284 hammer2_dio_limit = 100000;
286 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
288 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
290 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
294 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
296 margs_read.objsize = 65536;
297 margs_read.mtype = M_HAMMER2_DEBUFFER;
299 margs_write.objsize = 32768;
300 margs_write.mtype = M_HAMMER2_CBUFFER;
302 margs_vop.objsize = sizeof(hammer2_xop_t);
303 margs_vop.mtype = M_HAMMER2;
306 * Note thaht for the XOPS cache we want backing store allocations
307 * to use M_ZERO. This is not allowed in objcache_get() (to avoid
308 * confusion), so use the backing store function that does it. This
309 * means that initial XOPS objects are zerod but REUSED objects are
310 * not. So we are responsible for cleaning the object up sufficiently
311 * for our needs before objcache_put()ing it back (typically just the
314 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
315 0, 1, NULL, NULL, NULL,
316 objcache_malloc_alloc,
317 objcache_malloc_free,
319 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
320 0, 1, NULL, NULL, NULL,
321 objcache_malloc_alloc,
322 objcache_malloc_free,
324 cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc,
325 0, 1, NULL, NULL, NULL,
326 objcache_malloc_alloc_zero,
327 objcache_malloc_free,
331 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
332 TAILQ_INIT(&hammer2_mntlist);
333 TAILQ_INIT(&hammer2_pfslist);
334 TAILQ_INIT(&hammer2_spmplist);
336 hammer2_limit_dirty_chains = maxvnodes / 10;
337 if (hammer2_limit_dirty_chains > HAMMER2_LIMIT_DIRTY_CHAINS)
338 hammer2_limit_dirty_chains = HAMMER2_LIMIT_DIRTY_CHAINS;
339 if (hammer2_limit_dirty_chains < 1000)
340 hammer2_limit_dirty_chains = 1000;
342 hammer2_limit_dirty_inodes = maxvnodes / 25;
343 if (hammer2_limit_dirty_inodes < 100)
344 hammer2_limit_dirty_inodes = 100;
345 if (hammer2_limit_dirty_inodes > HAMMER2_LIMIT_DIRTY_INODES)
346 hammer2_limit_dirty_inodes = HAMMER2_LIMIT_DIRTY_INODES;
353 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
355 objcache_destroy(cache_buffer_read);
356 objcache_destroy(cache_buffer_write);
357 objcache_destroy(cache_xops);
362 * Core PFS allocator. Used to allocate or reference the pmp structure
363 * for PFS cluster mounts and the spmp structure for media (hmp) structures.
364 * The pmp can be passed in or loaded by this function using the chain and
367 * pmp->modify_tid tracks new modify_tid transaction ids for front-end
368 * transactions. Note that synchronization does not use this field.
369 * (typically frontend operations and synchronization cannot run on the
370 * same PFS node at the same time).
375 hammer2_pfsalloc(hammer2_chain_t *chain,
376 const hammer2_inode_data_t *ripdata,
377 hammer2_tid_t modify_tid, hammer2_dev_t *force_local)
380 hammer2_inode_t *iroot;
388 * Locate or create the PFS based on the cluster id. If ripdata
389 * is NULL this is a spmp which is unique and is always allocated.
391 * If the device is mounted in local mode all PFSs are considered
392 * independent and not part of any cluster (for debugging only).
395 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
396 if (force_local != pmp->force_local)
398 if (force_local == NULL &&
399 bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
400 sizeof(pmp->pfs_clid)) == 0) {
402 } else if (force_local && pmp->pfs_names[0] &&
403 strcmp(pmp->pfs_names[0], ripdata->filename) == 0) {
410 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
411 pmp->force_local = force_local;
412 hammer2_trans_manage_init(pmp);
413 kmalloc_create_obj(&pmp->minode, "HAMMER2-inodes",
414 sizeof(struct hammer2_inode));
415 lockinit(&pmp->lock, "pfslk", 0, 0);
416 lockinit(&pmp->lock_nlink, "h2nlink", 0, 0);
417 spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
418 spin_init(&pmp->xop_spin, "h2xop");
419 spin_init(&pmp->lru_spin, "h2lru");
420 RB_INIT(&pmp->inum_tree);
421 TAILQ_INIT(&pmp->syncq);
422 TAILQ_INIT(&pmp->depq);
423 TAILQ_INIT(&pmp->lru_list);
424 spin_init(&pmp->list_spin, "h2pfsalloc_list");
427 * Save the last media transaction id for the flusher. Set
431 pmp->pfs_clid = ripdata->meta.pfs_clid;
432 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
434 pmp->flags |= HAMMER2_PMPF_SPMP;
435 TAILQ_INSERT_TAIL(&hammer2_spmplist, pmp, mntentry);
439 * The synchronization thread may start too early, make
440 * sure it stays frozen until we are ready to let it go.
444 pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN |
445 HAMMER2_THREAD_REMASTER;
450 * Create the PFS's root inode and any missing XOP helper threads.
452 if ((iroot = pmp->iroot) == NULL) {
453 iroot = hammer2_inode_get(pmp, NULL, 1, -1);
455 iroot->meta = ripdata->meta;
457 hammer2_inode_ref(iroot);
458 hammer2_inode_unlock(iroot);
462 * Stop here if no chain is passed in.
468 * When a chain is passed in we must add it to the PFS's root
469 * inode, update pmp->pfs_types[], and update the syncronization
472 * When forcing local mode, mark the PFS as a MASTER regardless.
474 * At the moment empty spots can develop due to removals or failures.
475 * Ultimately we want to re-fill these spots but doing so might
476 * confused running code. XXX
478 hammer2_inode_ref(iroot);
479 hammer2_mtx_ex(&iroot->lock);
480 j = iroot->cluster.nchains;
482 if (j == HAMMER2_MAXCLUSTER) {
483 kprintf("hammer2_pfsalloc: cluster full!\n");
484 /* XXX fatal error? */
486 KKASSERT(chain->pmp == NULL);
488 hammer2_chain_ref(chain);
489 iroot->cluster.array[j].chain = chain;
491 pmp->pfs_types[j] = HAMMER2_PFSTYPE_MASTER;
493 pmp->pfs_types[j] = ripdata->meta.pfs_type;
494 pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
495 pmp->pfs_hmps[j] = chain->hmp;
496 hammer2_spin_ex(&pmp->inum_spin);
497 pmp->pfs_iroot_blocksets[j] = chain->data->ipdata.u.blockset;
498 hammer2_spin_unex(&pmp->inum_spin);
501 * If the PFS is already mounted we must account
502 * for the mount_count here.
505 ++chain->hmp->mount_count;
508 * May have to fixup dirty chain tracking. Previous
509 * pmp was NULL so nothing to undo.
511 if (chain->flags & HAMMER2_CHAIN_MODIFIED)
512 hammer2_pfs_memory_inc(pmp);
515 iroot->cluster.nchains = j;
518 * Update nmasters from any PFS inode which is part of the cluster.
519 * It is possible that this will result in a value which is too
520 * high. MASTER PFSs are authoritative for pfs_nmasters and will
521 * override this value later on.
523 * (This informs us of masters that might not currently be
524 * discoverable by this mount).
526 if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
527 pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
531 * Count visible masters. Masters are usually added with
532 * ripdata->meta.pfs_nmasters set to 1. This detects when there
533 * are more (XXX and must update the master inodes).
536 for (i = 0; i < iroot->cluster.nchains; ++i) {
537 if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
540 if (pmp->pfs_nmasters < count)
541 pmp->pfs_nmasters = count;
544 * Create missing synchronization and support threads.
546 * Single-node masters (including snapshots) have nothing to
547 * synchronize and do not require this thread.
549 * Multi-node masters or any number of soft masters, slaves, copy,
550 * or other PFS types need the thread.
552 * Each thread is responsible for its particular cluster index.
553 * We use independent threads so stalls or mismatches related to
554 * any given target do not affect other targets.
556 for (i = 0; i < iroot->cluster.nchains; ++i) {
558 * Single-node masters (including snapshots) have nothing
559 * to synchronize and will make direct xops support calls,
560 * thus they do not require this thread.
562 * Note that there can be thousands of snapshots. We do not
563 * want to create thousands of threads.
565 if (pmp->pfs_nmasters <= 1 &&
566 pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) {
571 * Sync support thread
573 if (pmp->sync_thrs[i].td == NULL) {
574 hammer2_thr_create(&pmp->sync_thrs[i], pmp, NULL,
576 hammer2_primary_sync_thread);
581 * Create missing Xop threads
583 * NOTE: We create helper threads for all mounted PFSs or any
584 * PFSs with 2+ nodes (so the sync thread can update them,
585 * even if not mounted).
587 if (pmp->mp || iroot->cluster.nchains >= 2)
588 hammer2_xop_helper_create(pmp);
590 hammer2_mtx_unlock(&iroot->lock);
591 hammer2_inode_drop(iroot);
597 * Deallocate an element of a probed PFS. If destroying and this is a
598 * MASTER, adjust nmasters.
600 * This function does not physically destroy the PFS element in its device
601 * under the super-root (see hammer2_ioctl_pfs_delete()).
604 hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying)
606 hammer2_inode_t *iroot;
607 hammer2_chain_t *chain;
611 * Cleanup our reference on iroot. iroot is (should) not be needed
619 * XXX flush after acquiring the iroot lock.
620 * XXX clean out the cluster index from all inode structures.
622 hammer2_thr_delete(&pmp->sync_thrs[clindex]);
625 * Remove the cluster index from the group. If destroying
626 * the PFS and this is a master, adjust pfs_nmasters.
628 hammer2_mtx_ex(&iroot->lock);
629 chain = iroot->cluster.array[clindex].chain;
630 iroot->cluster.array[clindex].chain = NULL;
632 switch(pmp->pfs_types[clindex]) {
633 case HAMMER2_PFSTYPE_MASTER:
634 if (destroying && pmp->pfs_nmasters > 0)
636 /* XXX adjust ripdata->meta.pfs_nmasters */
641 pmp->pfs_types[clindex] = HAMMER2_PFSTYPE_NONE;
643 hammer2_mtx_unlock(&iroot->lock);
649 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
650 hammer2_chain_drop(chain);
654 * Terminate all XOP threads for the cluster index.
656 if (pmp->xop_groups) {
657 for (j = 0; j < hammer2_xopgroups; ++j) {
659 &pmp->xop_groups[j].thrs[clindex]);
666 * Destroy a PFS, typically only occurs after the last mount on a device
670 hammer2_pfsfree(hammer2_pfs_t *pmp)
672 hammer2_inode_t *iroot;
673 hammer2_chain_t *chain;
674 int chains_still_present = 0;
679 * Cleanup our reference on iroot. iroot is (should) not be needed
682 if (pmp->flags & HAMMER2_PMPF_SPMP)
683 TAILQ_REMOVE(&hammer2_spmplist, pmp, mntentry);
685 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
688 * Cleanup chains remaining on LRU list.
690 hammer2_spin_ex(&pmp->lru_spin);
691 while ((chain = TAILQ_FIRST(&pmp->lru_list)) != NULL) {
692 KKASSERT(chain->flags & HAMMER2_CHAIN_ONLRU);
693 atomic_add_int(&pmp->lru_count, -1);
694 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONLRU);
695 TAILQ_REMOVE(&pmp->lru_list, chain, lru_node);
696 hammer2_chain_ref(chain);
697 hammer2_spin_unex(&pmp->lru_spin);
698 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
699 hammer2_chain_drop(chain);
700 hammer2_spin_ex(&pmp->lru_spin);
702 hammer2_spin_unex(&pmp->lru_spin);
709 for (i = 0; i < iroot->cluster.nchains; ++i) {
710 hammer2_thr_delete(&pmp->sync_thrs[i]);
711 if (pmp->xop_groups) {
712 for (j = 0; j < hammer2_xopgroups; ++j)
714 &pmp->xop_groups[j].thrs[i]);
716 chain = iroot->cluster.array[i].chain;
717 if (chain && !RB_EMPTY(&chain->core.rbtree)) {
718 kprintf("hammer2: Warning pmp %p still "
719 "has active chains\n", pmp);
720 chains_still_present = 1;
723 #if REPORT_REFS_ERRORS
724 if (iroot->refs != 1)
725 kprintf("PMP->IROOT %p REFS WRONG %d\n",
728 KKASSERT(iroot->refs == 1);
731 hammer2_inode_drop(iroot);
736 * Free remaining pmp resources
738 if (chains_still_present) {
739 kprintf("hammer2: cannot free pmp %p, still in use\n", pmp);
741 kmalloc_destroy_obj(&pmp->minode);
742 kfree(pmp, M_HAMMER2);
747 * Remove all references to hmp from the pfs list. Any PFS which becomes
748 * empty is terminated and freed.
753 hammer2_pfsfree_scan(hammer2_dev_t *hmp, int which)
756 hammer2_inode_t *iroot;
757 hammer2_chain_t *rchain;
760 struct hammer2_pfslist *wlist;
763 wlist = &hammer2_pfslist;
765 wlist = &hammer2_spmplist;
767 TAILQ_FOREACH(pmp, wlist, mntentry) {
768 if ((iroot = pmp->iroot) == NULL)
772 * Determine if this PFS is affected. If it is we must
773 * freeze all management threads and lock its iroot.
775 * Freezing a management thread forces it idle, operations
776 * in-progress will be aborted and it will have to start
777 * over again when unfrozen, or exit if told to exit.
779 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
780 if (pmp->pfs_hmps[i] == hmp)
783 if (i == HAMMER2_MAXCLUSTER)
786 hammer2_vfs_sync_pmp(pmp, MNT_WAIT);
789 * Make sure all synchronization threads are locked
792 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
793 if (pmp->pfs_hmps[i] == NULL)
795 hammer2_thr_freeze_async(&pmp->sync_thrs[i]);
796 if (pmp->xop_groups) {
797 for (j = 0; j < hammer2_xopgroups; ++j) {
798 hammer2_thr_freeze_async(
799 &pmp->xop_groups[j].thrs[i]);
803 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
804 if (pmp->pfs_hmps[i] == NULL)
806 hammer2_thr_freeze(&pmp->sync_thrs[i]);
807 if (pmp->xop_groups) {
808 for (j = 0; j < hammer2_xopgroups; ++j) {
810 &pmp->xop_groups[j].thrs[i]);
816 * Lock the inode and clean out matching chains.
817 * Note that we cannot use hammer2_inode_lock_*()
818 * here because that would attempt to validate the
819 * cluster that we are in the middle of ripping
822 * WARNING! We are working directly on the inodes
825 hammer2_mtx_ex(&iroot->lock);
828 * Remove the chain from matching elements of the PFS.
830 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
831 if (pmp->pfs_hmps[i] != hmp)
833 hammer2_thr_delete(&pmp->sync_thrs[i]);
834 if (pmp->xop_groups) {
835 for (j = 0; j < hammer2_xopgroups; ++j) {
837 &pmp->xop_groups[j].thrs[i]);
840 rchain = iroot->cluster.array[i].chain;
841 iroot->cluster.array[i].chain = NULL;
842 pmp->pfs_types[i] = 0;
843 if (pmp->pfs_names[i]) {
844 kfree(pmp->pfs_names[i], M_HAMMER2);
845 pmp->pfs_names[i] = NULL;
848 hammer2_chain_drop(rchain);
850 if (iroot->cluster.focus == rchain)
851 iroot->cluster.focus = NULL;
853 pmp->pfs_hmps[i] = NULL;
855 hammer2_mtx_unlock(&iroot->lock);
858 * Cleanup trailing chains. Gaps may remain.
860 for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) {
861 if (pmp->pfs_hmps[i])
864 iroot->cluster.nchains = i + 1;
867 * If the PMP has no elements remaining we can destroy it.
868 * (this will transition management threads from frozen->exit).
870 if (iroot->cluster.nchains == 0) {
872 * If this was the hmp's spmp, we need to clean
873 * a little more stuff out.
875 if (hmp->spmp == pmp) {
877 hmp->vchain.pmp = NULL;
878 hmp->fchain.pmp = NULL;
882 * Free the pmp and restart the loop
884 KKASSERT(TAILQ_EMPTY(&pmp->syncq));
885 KKASSERT(TAILQ_EMPTY(&pmp->depq));
886 hammer2_pfsfree(pmp);
891 * If elements still remain we need to set the REMASTER
892 * flag and unfreeze it.
894 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
895 if (pmp->pfs_hmps[i] == NULL)
897 hammer2_thr_remaster(&pmp->sync_thrs[i]);
898 hammer2_thr_unfreeze(&pmp->sync_thrs[i]);
899 if (pmp->xop_groups) {
900 for (j = 0; j < hammer2_xopgroups; ++j) {
901 hammer2_thr_remaster(
902 &pmp->xop_groups[j].thrs[i]);
903 hammer2_thr_unfreeze(
904 &pmp->xop_groups[j].thrs[i]);
912 * Mount or remount HAMMER2 fileystem from physical media
915 * mp mount point structure
921 * mp mount point structure
922 * path path to mount point
923 * data pointer to argument structure in user space
924 * volume volume path (device@LABEL form)
925 * hflags user mount flags
926 * cred user credentials
933 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
936 struct hammer2_mount_info info;
939 hammer2_dev_t *hmp, *hmp_tmp;
940 hammer2_dev_t *force_local;
941 hammer2_key_t key_next;
942 hammer2_key_t key_dummy;
944 hammer2_chain_t *parent;
945 hammer2_chain_t *chain;
946 const hammer2_inode_data_t *ripdata;
947 hammer2_blockref_t bref;
948 hammer2_devvp_list_t devvpl;
949 hammer2_devvp_t *e, *e_tmp;
951 char devstr[MNAMELEN];
956 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
964 bzero(&info, sizeof(info));
968 * Non-root mount or updating a mount
970 error = copyin(data, &info, sizeof(info));
975 if (mp->mnt_flag & MNT_UPDATE) {
977 * Update mount. Note that pmp->iroot->cluster is
978 * an inode-embedded cluster and thus cannot be
981 * XXX HAMMER2 needs to implement NFS export via
984 hammer2_cluster_t *cluster;
987 pmp->hflags = info.hflags;
988 cluster = &pmp->iroot->cluster;
989 for (i = 0; i < cluster->nchains; ++i) {
990 if (cluster->array[i].chain == NULL)
992 hmp = cluster->array[i].chain->hmp;
993 error = hammer2_remount(hmp, mp, path, cred);
1005 info.cluster_fd = -1;
1006 ksnprintf(devstr, sizeof(devstr), "%s",
1007 mp->mnt_stat.f_mntfromname);
1008 done = strlen(devstr) + 1;
1009 kprintf("hammer2_mount: root devstr=\"%s\"\n", devstr);
1011 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
1014 kprintf("hammer2_mount: devstr=\"%s\"\n", devstr);
1018 * Extract device and label, automatically mount @BOOT, @ROOT, or @DATA
1019 * if no label specified, based on the partition id. Error out if no
1020 * label or device (with partition id) is specified. This is strictly
1021 * a convenience to match the default label created by newfs_hammer2,
1022 * our preference is that a label always be specified.
1024 * NOTE: We allow 'mount @LABEL <blah>'... that is, a mount command
1025 * that does not specify a device, as long as some H2 label
1026 * has already been mounted from that device. This makes
1027 * mounting snapshots a lot easier.
1030 label = strchr(devstr, '@');
1031 if (label && ((label + 1) - dev) > done) {
1032 kprintf("hammer2_mount: bad label %s/%zd\n", devstr, done);
1035 if (label == NULL || label[1] == 0) {
1039 label = devstr + strlen(devstr);
1041 *label = '\0'; /* clean up trailing @ */
1060 kprintf("hammer2_mount: dev=\"%s\" label=\"%s\" rdonly=%d\n",
1064 * Initialize all device vnodes.
1066 TAILQ_INIT(&devvpl);
1067 error = hammer2_init_devvp(dev, path == NULL, &devvpl);
1069 kprintf("hammer2: failed to initialize devvp in %s\n", dev);
1070 hammer2_cleanup_devvp(&devvpl);
1075 * Determine if the device has already been mounted. After this
1076 * check hmp will be non-NULL if we are doing the second or more
1077 * hammer2 mounts from the same device.
1079 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1080 if (!TAILQ_EMPTY(&devvpl)) {
1082 * Match the device. Due to the way devfs works,
1083 * we may not be able to directly match the vnode pointer,
1084 * so also check to see if the underlying device matches.
1086 TAILQ_FOREACH(hmp_tmp, &hammer2_mntlist, mntentry) {
1087 TAILQ_FOREACH(e_tmp, &hmp_tmp->devvpl, entry) {
1088 int devvp_found = 0;
1089 TAILQ_FOREACH(e, &devvpl, entry) {
1091 if (e_tmp->devvp == e->devvp)
1093 if (e_tmp->devvp->v_rdev &&
1094 e_tmp->devvp->v_rdev == e->devvp->v_rdev)
1101 kprintf("hammer2_mount: hmp=%p matched\n", hmp);
1108 * If no match this may be a fresh H2 mount, make sure
1109 * the device is not mounted on anything else.
1112 TAILQ_FOREACH(e, &devvpl, entry) {
1113 struct vnode *devvp = e->devvp;
1115 error = vfs_mountedon(devvp);
1117 kprintf("hammer2_mount: %s mounted %d\n",
1119 hammer2_cleanup_devvp(&devvpl);
1120 lockmgr(&hammer2_mntlk, LK_RELEASE);
1127 * Match the label to a pmp already probed.
1129 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
1130 for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
1131 if (pmp->pfs_names[i] &&
1132 strcmp(pmp->pfs_names[i], label) == 0) {
1133 hmp = pmp->pfs_hmps[i];
1141 kprintf("hammer2_mount: PFS label \"%s\" not found\n",
1143 hammer2_cleanup_devvp(&devvpl);
1144 lockmgr(&hammer2_mntlk, LK_RELEASE);
1150 * Open the device if this isn't a secondary mount and construct
1151 * the H2 device mount (hmp).
1154 hammer2_chain_t *schain;
1156 hammer2_xop_head_t xop;
1159 * Now open the device
1161 KKASSERT(!TAILQ_EMPTY(&devvpl));
1163 error = hammer2_open_devvp(&devvpl, ronly);
1165 hammer2_close_devvp(&devvpl, ronly);
1166 hammer2_cleanup_devvp(&devvpl);
1167 lockmgr(&hammer2_mntlk, LK_RELEASE);
1173 * Construct volumes and link with device vnodes.
1175 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
1177 error = hammer2_init_volumes(mp, &devvpl, hmp->volumes,
1178 &hmp->voldata, &hmp->devvp);
1180 hammer2_close_devvp(&devvpl, ronly);
1181 hammer2_cleanup_devvp(&devvpl);
1182 lockmgr(&hammer2_mntlk, LK_RELEASE);
1183 kfree(hmp, M_HAMMER2);
1187 kprintf("hammer2: failed to initialize root volume\n");
1188 hammer2_unmount_helper(mp, NULL, hmp);
1189 lockmgr(&hammer2_mntlk, LK_RELEASE);
1190 hammer2_vfs_unmount(mp, MNT_FORCE);
1194 ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
1196 hmp->hflags = info.hflags & HMNT2_DEVFLAGS;
1197 kmalloc_create_obj(&hmp->mchain, "HAMMER2-chains",
1198 sizeof(struct hammer2_chain));
1199 kmalloc_create_obj(&hmp->mio, "HAMMER2-dio",
1200 sizeof(struct hammer2_io));
1201 kmalloc_create(&hmp->mmsg, "HAMMER2-msg");
1202 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
1203 RB_INIT(&hmp->iotree);
1204 spin_init(&hmp->io_spin, "h2mount_io");
1205 spin_init(&hmp->list_spin, "h2mount_list");
1207 lockinit(&hmp->vollk, "h2vol", 0, 0);
1208 lockinit(&hmp->bulklk, "h2bulk", 0, 0);
1209 lockinit(&hmp->bflock, "h2bflk", 0, 0);
1212 * vchain setup. vchain.data is embedded.
1213 * vchain.refs is initialized and will never drop to 0.
1215 * NOTE! voldata is not yet loaded.
1217 hmp->vchain.hmp = hmp;
1218 hmp->vchain.refs = 1;
1219 hmp->vchain.data = (void *)&hmp->voldata;
1220 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
1221 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
1222 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
1223 hammer2_chain_core_init(&hmp->vchain);
1226 * fchain setup. fchain.data is embedded.
1227 * fchain.refs is initialized and will never drop to 0.
1229 * The data is not used but needs to be initialized to
1230 * pass assertion muster. We use this chain primarily
1231 * as a placeholder for the freemap's top-level radix tree
1232 * so it does not interfere with the volume's topology
1235 hmp->fchain.hmp = hmp;
1236 hmp->fchain.refs = 1;
1237 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
1238 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
1239 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
1240 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
1241 hmp->fchain.bref.methods =
1242 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
1243 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
1244 hammer2_chain_core_init(&hmp->fchain);
1247 * Initialize volume header related fields.
1249 KKASSERT(hmp->voldata.magic == HAMMER2_VOLUME_ID_HBO ||
1250 hmp->voldata.magic == HAMMER2_VOLUME_ID_ABO);
1251 hmp->volhdrno = error;
1252 hmp->volsync = hmp->voldata;
1253 hmp->free_reserved = hmp->voldata.allocator_size / 20;
1255 * Must use hmp instead of volume header for these two
1256 * in order to handle volume versions transparently.
1258 if (hmp->voldata.version >= HAMMER2_VOL_VERSION_MULTI_VOLUMES) {
1259 hmp->nvolumes = hmp->voldata.nvolumes;
1260 hmp->total_size = hmp->voldata.total_size;
1263 hmp->total_size = hmp->voldata.volu_size;
1265 KKASSERT(hmp->nvolumes > 0);
1268 * Move devvpl entries to hmp.
1270 TAILQ_INIT(&hmp->devvpl);
1271 while ((e = TAILQ_FIRST(&devvpl)) != NULL) {
1272 TAILQ_REMOVE(&devvpl, e, entry);
1273 TAILQ_INSERT_TAIL(&hmp->devvpl, e, entry);
1275 KKASSERT(TAILQ_EMPTY(&devvpl));
1276 KKASSERT(!TAILQ_EMPTY(&hmp->devvpl));
1279 * Really important to get these right or the flush and
1280 * teardown code will get confused.
1282 hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0, NULL);
1284 spmp->pfs_hmps[0] = hmp;
1287 * Dummy-up vchain and fchain's modify_tid. mirror_tid
1288 * is inherited from the volume header.
1291 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
1292 hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
1293 hmp->vchain.pmp = spmp;
1294 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
1295 hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
1296 hmp->fchain.pmp = spmp;
1299 * First locate the super-root inode, which is key 0
1300 * relative to the volume header's blockset.
1302 * Then locate the root inode by scanning the directory keyspace
1303 * represented by the label.
1305 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1306 schain = hammer2_chain_lookup(&parent, &key_dummy,
1307 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
1309 hammer2_chain_lookup_done(parent);
1310 if (schain == NULL) {
1311 kprintf("hammer2_mount: invalid super-root\n");
1312 hammer2_unmount_helper(mp, NULL, hmp);
1313 lockmgr(&hammer2_mntlk, LK_RELEASE);
1314 hammer2_vfs_unmount(mp, MNT_FORCE);
1317 if (schain->error) {
1318 kprintf("hammer2_mount: error %s reading super-root\n",
1319 hammer2_error_str(schain->error));
1320 hammer2_chain_unlock(schain);
1321 hammer2_chain_drop(schain);
1323 hammer2_unmount_helper(mp, NULL, hmp);
1324 lockmgr(&hammer2_mntlk, LK_RELEASE);
1325 hammer2_vfs_unmount(mp, MNT_FORCE);
1330 * The super-root always uses an inode_tid of 1 when
1333 spmp->inode_tid = 1;
1334 spmp->modify_tid = schain->bref.modify_tid + 1;
1337 * Sanity-check schain's pmp and finish initialization.
1338 * Any chain belonging to the super-root topology should
1339 * have a NULL pmp (not even set to spmp).
1341 ripdata = &schain->data->ipdata;
1342 KKASSERT(schain->pmp == NULL);
1343 spmp->pfs_clid = ripdata->meta.pfs_clid;
1346 * Replace the dummy spmp->iroot with a real one. It's
1347 * easier to just do a wholesale replacement than to try
1348 * to update the chain and fixup the iroot fields.
1350 * The returned inode is locked with the supplied cluster.
1352 hammer2_dummy_xop_from_chain(&xop, schain);
1353 hammer2_inode_drop(spmp->iroot);
1355 spmp->iroot = hammer2_inode_get(spmp, &xop, -1, -1);
1356 spmp->spmp_hmp = hmp;
1357 spmp->pfs_types[0] = ripdata->meta.pfs_type;
1358 spmp->pfs_hmps[0] = hmp;
1359 hammer2_inode_ref(spmp->iroot);
1360 hammer2_inode_unlock(spmp->iroot);
1361 hammer2_cluster_unlock(&xop.cluster);
1362 hammer2_chain_drop(schain);
1363 /* do not call hammer2_cluster_drop() on an embedded cluster */
1364 schain = NULL; /* now invalid */
1365 /* leave spmp->iroot with one ref */
1368 error = hammer2_recovery(hmp);
1370 error |= hammer2_fixup_pfses(hmp);
1371 /* XXX do something with error */
1373 hammer2_update_pmps(hmp);
1374 hammer2_iocom_init(hmp);
1375 hammer2_bulkfree_init(hmp);
1378 * Ref the cluster management messaging descriptor. The mount
1379 * program deals with the other end of the communications pipe.
1381 * Root mounts typically do not supply one.
1383 if (info.cluster_fd >= 0) {
1384 fp = holdfp(curthread, info.cluster_fd, -1);
1386 hammer2_cluster_reconnect(hmp, fp);
1388 kprintf("hammer2_mount: bad cluster_fd!\n");
1393 if (info.hflags & HMNT2_DEVFLAGS) {
1394 kprintf("hammer2_mount: Warning: mount flags pertaining "
1395 "to the whole device may only be specified "
1396 "on the first mount of the device: %08x\n",
1397 info.hflags & HMNT2_DEVFLAGS);
1402 * Force local mount (disassociate all PFSs from their clusters).
1403 * Used primarily for debugging.
1405 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
1408 * Lookup the mount point under the media-localized super-root.
1409 * Scanning hammer2_pfslist doesn't help us because it represents
1410 * PFS cluster ids which can aggregate several named PFSs together.
1412 * cluster->pmp will incorrectly point to spmp and must be fixed
1415 hammer2_inode_lock(spmp->iroot, 0);
1416 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1417 lhc = hammer2_dirhash(label, strlen(label));
1418 chain = hammer2_chain_lookup(&parent, &key_next,
1419 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1422 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1423 strcmp(label, chain->data->ipdata.filename) == 0) {
1426 chain = hammer2_chain_next(&parent, chain, &key_next,
1428 lhc + HAMMER2_DIRHASH_LOMASK,
1432 hammer2_chain_unlock(parent);
1433 hammer2_chain_drop(parent);
1435 hammer2_inode_unlock(spmp->iroot);
1438 * PFS could not be found?
1440 if (chain == NULL) {
1441 hammer2_unmount_helper(mp, NULL, hmp);
1442 lockmgr(&hammer2_mntlk, LK_RELEASE);
1443 hammer2_vfs_unmount(mp, MNT_FORCE);
1446 kprintf("hammer2_mount: PFS label I/O error\n");
1449 kprintf("hammer2_mount: PFS label \"%s\" not found\n",
1456 * Acquire the pmp structure (it should have already been allocated
1457 * via hammer2_update_pmps() so do not pass cluster in to add to
1458 * available chains).
1460 * Check if the cluster has already been mounted. A cluster can
1461 * only be mounted once, use null mounts to mount additional copies.
1464 kprintf("hammer2_mount: PFS label I/O error\n");
1466 ripdata = &chain->data->ipdata;
1468 pmp = hammer2_pfsalloc(NULL, ripdata,
1469 bref.modify_tid, force_local);
1471 hammer2_chain_unlock(chain);
1472 hammer2_chain_drop(chain);
1477 kprintf("hammer2_mount: hmp=%p pmp=%p\n", hmp, pmp);
1480 kprintf("hammer2_mount: PFS already mounted!\n");
1481 hammer2_unmount_helper(mp, NULL, hmp);
1482 lockmgr(&hammer2_mntlk, LK_RELEASE);
1483 hammer2_vfs_unmount(mp, MNT_FORCE);
1488 pmp->hflags = info.hflags;
1489 mp->mnt_flag |= MNT_LOCAL;
1490 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
1491 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
1494 * required mount structure initializations
1496 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1497 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1499 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1500 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1505 mp->mnt_iosize_max = MAXPHYS;
1508 * Connect up mount pointers.
1510 hammer2_mount_helper(mp, pmp);
1511 lockmgr(&hammer2_mntlk, LK_RELEASE);
1517 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1518 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1519 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1522 copyinstr(info.volume, mp->mnt_stat.f_mntfromname,
1523 MNAMELEN - 1, &size);
1524 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1525 } /* else root mount, already in there */
1527 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1529 copyinstr(path, mp->mnt_stat.f_mntonname,
1530 sizeof(mp->mnt_stat.f_mntonname) - 1,
1534 mp->mnt_stat.f_mntonname[0] = '/';
1538 * Initial statfs to prime mnt_stat.
1540 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1546 * Scan PFSs under the super-root and create hammer2_pfs structures.
1550 hammer2_update_pmps(hammer2_dev_t *hmp)
1552 const hammer2_inode_data_t *ripdata;
1553 hammer2_chain_t *parent;
1554 hammer2_chain_t *chain;
1555 hammer2_blockref_t bref;
1556 hammer2_dev_t *force_local;
1557 hammer2_pfs_t *spmp;
1559 hammer2_key_t key_next;
1563 * Force local mount (disassociate all PFSs from their clusters).
1564 * Used primarily for debugging.
1566 force_local = (hmp->hflags & HMNT2_LOCAL) ? hmp : NULL;
1569 * Lookup mount point under the media-localized super-root.
1571 * cluster->pmp will incorrectly point to spmp and must be fixed
1575 hammer2_inode_lock(spmp->iroot, 0);
1576 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1577 chain = hammer2_chain_lookup(&parent, &key_next,
1578 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
1582 kprintf("I/O error scanning PFS labels\n");
1583 } else if (chain->bref.type != HAMMER2_BREF_TYPE_INODE) {
1584 kprintf("Non inode chain type %d under super-root\n",
1587 ripdata = &chain->data->ipdata;
1589 pmp = hammer2_pfsalloc(chain, ripdata,
1590 bref.modify_tid, force_local);
1592 chain = hammer2_chain_next(&parent, chain, &key_next,
1593 key_next, HAMMER2_KEY_MAX,
1597 hammer2_chain_unlock(parent);
1598 hammer2_chain_drop(parent);
1600 hammer2_inode_unlock(spmp->iroot);
1605 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused,
1608 hammer2_volume_t *vol;
1609 struct vnode *devvp;
1610 int i, error, result = 0;
1612 if (!(hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)))
1615 for (i = 0; i < hmp->nvolumes; ++i) {
1616 vol = &hmp->volumes[i];
1617 devvp = vol->dev->devvp;
1619 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1620 VOP_OPEN(devvp, FREAD | FWRITE, FSCRED, NULL);
1623 if (vol->id == HAMMER2_ROOT_VOLUME) {
1624 error = hammer2_recovery(hmp);
1626 error |= hammer2_fixup_pfses(hmp);
1628 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1630 VOP_CLOSE(devvp, FREAD, NULL);
1632 VOP_CLOSE(devvp, FREAD | FWRITE, NULL);
1638 kprintf("hammer2: enable read/write\n");
1647 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1658 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1661 * If mount initialization proceeded far enough we must flush
1662 * its vnodes and sync the underlying mount points. Three syncs
1663 * are required to fully flush the filesystem (freemap updates lag
1664 * by one flush, and one extra for safety).
1666 if (mntflags & MNT_FORCE)
1671 error = vflush(mp, 0, flags);
1674 hammer2_vfs_sync(mp, MNT_WAIT);
1675 hammer2_vfs_sync(mp, MNT_WAIT);
1676 hammer2_vfs_sync(mp, MNT_WAIT);
1680 * Cleanup the frontend support XOPS threads
1682 hammer2_xop_helper_cleanup(pmp);
1685 hammer2_unmount_helper(mp, pmp, NULL);
1689 lockmgr(&hammer2_mntlk, LK_RELEASE);
1695 * Mount helper, hook the system mount into our PFS.
1696 * The mount lock is held.
1698 * We must bump the mount_count on related devices for any
1703 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1705 hammer2_cluster_t *cluster;
1706 hammer2_chain_t *rchain;
1709 mp->mnt_data = (qaddr_t)pmp;
1713 * After pmp->mp is set we have to adjust hmp->mount_count.
1715 cluster = &pmp->iroot->cluster;
1716 for (i = 0; i < cluster->nchains; ++i) {
1717 rchain = cluster->array[i].chain;
1720 ++rchain->hmp->mount_count;
1724 * Create missing Xop threads
1726 hammer2_xop_helper_create(pmp);
1730 * Mount helper, unhook the system mount from our PFS.
1731 * The mount lock is held.
1733 * If hmp is supplied a mount responsible for being the first to open
1734 * the block device failed and the block device and all PFSs using the
1735 * block device must be cleaned up.
1737 * If pmp is supplied multiple devices might be backing the PFS and each
1738 * must be disconnected. This might not be the last PFS using some of the
1739 * underlying devices. Also, we have to adjust our hmp->mount_count
1740 * accounting for the devices backing the pmp which is now undergoing an
1745 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1747 hammer2_cluster_t *cluster;
1748 hammer2_chain_t *rchain;
1753 * If no device supplied this is a high-level unmount and we have to
1754 * to disconnect the mount, adjust mount_count, and locate devices
1755 * that might now have no mounts.
1758 KKASSERT(hmp == NULL);
1759 KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1761 mp->mnt_data = NULL;
1764 * After pmp->mp is cleared we have to account for
1767 cluster = &pmp->iroot->cluster;
1768 for (i = 0; i < cluster->nchains; ++i) {
1769 rchain = cluster->array[i].chain;
1772 --rchain->hmp->mount_count;
1773 /* scrapping hmp now may invalidate the pmp */
1776 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1777 if (hmp->mount_count == 0) {
1778 hammer2_unmount_helper(NULL, NULL, hmp);
1786 * Try to terminate the block device. We can't terminate it if
1787 * there are still PFSs referencing it.
1789 if (hmp->mount_count)
1793 * Decomission the network before we start messing with the
1796 hammer2_iocom_uninit(hmp);
1798 hammer2_bulkfree_uninit(hmp);
1799 hammer2_pfsfree_scan(hmp, 0);
1802 * Cycle the volume data lock as a safety (probably not needed any
1803 * more). To ensure everything is out we need to flush at least
1804 * three times. (1) The running of the sideq can dirty the
1805 * filesystem, (2) A normal flush can dirty the freemap, and
1806 * (3) ensure that the freemap is fully synchronized.
1808 * The next mount's recovery scan can clean everything up but we want
1809 * to leave the filesystem in a 100% clean state on a normal unmount.
1812 hammer2_voldata_lock(hmp);
1813 hammer2_voldata_unlock(hmp);
1817 * Flush whatever is left. Unmounted but modified PFS's might still
1818 * have some dirty chains on them.
1820 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1821 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1823 if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1824 hammer2_voldata_modify(hmp);
1825 hammer2_flush(&hmp->fchain, HAMMER2_FLUSH_TOP |
1828 hammer2_chain_unlock(&hmp->fchain);
1830 if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) {
1831 hammer2_flush(&hmp->vchain, HAMMER2_FLUSH_TOP |
1834 hammer2_chain_unlock(&hmp->vchain);
1836 if ((hmp->vchain.flags | hmp->fchain.flags) &
1837 HAMMER2_CHAIN_FLUSH_MASK) {
1838 kprintf("hammer2_unmount: chains left over after final sync\n");
1839 kprintf(" vchain %08x\n", hmp->vchain.flags);
1840 kprintf(" fchain %08x\n", hmp->fchain.flags);
1842 if (hammer2_debug & 0x0010)
1843 Debugger("entered debugger");
1846 hammer2_pfsfree_scan(hmp, 1);
1848 KKASSERT(hmp->spmp == NULL);
1851 * Finish up with the device vnode
1853 if (!TAILQ_EMPTY(&hmp->devvpl)) {
1854 hammer2_close_devvp(&hmp->devvpl, hmp->ronly);
1855 hammer2_cleanup_devvp(&hmp->devvpl);
1857 KKASSERT(TAILQ_EMPTY(&hmp->devvpl));
1860 * Clear vchain/fchain flags that might prevent final cleanup
1863 if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1864 atomic_add_long(&hammer2_count_modified_chains, -1);
1865 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
1866 hammer2_pfs_memory_wakeup(hmp->vchain.pmp, -1);
1868 if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1869 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_UPDATE);
1872 if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1873 atomic_add_long(&hammer2_count_modified_chains, -1);
1874 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_MODIFIED);
1875 hammer2_pfs_memory_wakeup(hmp->fchain.pmp, -1);
1877 if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1878 atomic_clear_int(&hmp->fchain.flags, HAMMER2_CHAIN_UPDATE);
1882 * Final drop of embedded freemap root chain to
1883 * clean up fchain.core (fchain structure is not
1884 * flagged ALLOCATED so it is cleaned out and then
1887 hammer2_chain_drop(&hmp->fchain);
1890 * Final drop of embedded volume root chain to clean
1891 * up vchain.core (vchain structure is not flagged
1892 * ALLOCATED so it is cleaned out and then left to
1896 hammer2_dump_chain(&hmp->vchain, 0, 0, &dumpcnt, 'v', (u_int)-1);
1898 hammer2_dump_chain(&hmp->fchain, 0, 0, &dumpcnt, 'f', (u_int)-1);
1900 hammer2_chain_drop(&hmp->vchain);
1902 hammer2_io_cleanup(hmp, &hmp->iotree);
1903 if (hmp->iofree_count) {
1904 kprintf("io_cleanup: %d I/O's left hanging\n",
1908 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1909 kmalloc_destroy_obj(&hmp->mchain);
1910 kmalloc_destroy_obj(&hmp->mio);
1911 kmalloc_destroy(&hmp->mmsg);
1912 kfree(hmp, M_HAMMER2);
1916 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1917 ino_t ino, struct vnode **vpp)
1919 hammer2_xop_lookup_t *xop;
1921 hammer2_inode_t *ip;
1925 inum = (hammer2_tid_t)ino & HAMMER2_DIRHASH_USERMSK;
1931 * Easy if we already have it cached
1933 ip = hammer2_inode_lookup(pmp, inum);
1935 hammer2_inode_lock(ip, HAMMER2_RESOLVE_SHARED);
1936 *vpp = hammer2_igetv(ip, &error);
1937 hammer2_inode_unlock(ip);
1938 hammer2_inode_drop(ip); /* from lookup */
1944 * Otherwise we have to find the inode
1946 xop = hammer2_xop_alloc(pmp->iroot, 0);
1948 hammer2_xop_start(&xop->head, &hammer2_lookup_desc);
1949 error = hammer2_xop_collect(&xop->head, 0);
1952 ip = hammer2_inode_get(pmp, &xop->head, -1, -1);
1953 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1956 *vpp = hammer2_igetv(ip, &error);
1957 hammer2_inode_unlock(ip);
1967 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1974 if (pmp->iroot == NULL) {
1975 kprintf("hammer2 (%s): no root inode\n",
1976 mp->mnt_stat.f_mntfromname);
1982 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1984 while (pmp->inode_tid == 0) {
1985 hammer2_xop_ipcluster_t *xop;
1986 const hammer2_inode_meta_t *meta;
1988 xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1989 hammer2_xop_start(&xop->head, &hammer2_ipcluster_desc);
1990 error = hammer2_xop_collect(&xop->head, 0);
1993 meta = &hammer2_xop_gdata(&xop->head)->ipdata.meta;
1994 pmp->iroot->meta = *meta;
1995 pmp->inode_tid = meta->pfs_inum + 1;
1996 hammer2_xop_pdata(&xop->head);
1999 if (pmp->inode_tid < HAMMER2_INODE_START)
2000 pmp->inode_tid = HAMMER2_INODE_START;
2002 xop->head.cluster.focus->bref.modify_tid + 1;
2004 kprintf("PFS: Starting inode %jd\n",
2005 (intmax_t)pmp->inode_tid);
2006 kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
2007 pmp->inode_tid, pmp->modify_tid);
2009 wakeup(&pmp->iroot);
2011 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2014 * Prime the mount info.
2016 hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL);
2023 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2024 hammer2_inode_unlock(pmp->iroot);
2025 error = tsleep(&pmp->iroot, PCATCH, "h2root", hz);
2026 hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
2032 hammer2_inode_unlock(pmp->iroot);
2035 vp = hammer2_igetv(pmp->iroot, &error);
2036 hammer2_inode_unlock(pmp->iroot);
2046 * XXX incorporate ipdata->meta.inode_quota and data_quota
2050 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
2054 hammer2_blockref_t bref;
2059 * NOTE: iroot might not have validated the cluster yet.
2063 bzero(&tmp, sizeof(tmp));
2065 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
2066 hmp = pmp->pfs_hmps[i];
2069 if (pmp->iroot->cluster.array[i].chain)
2070 bref = pmp->iroot->cluster.array[i].chain->bref;
2072 bzero(&bref, sizeof(bref));
2074 tmp.f_files = bref.embed.stats.inode_count;
2076 tmp.f_blocks = hmp->voldata.allocator_size /
2077 mp->mnt_vstat.f_bsize;
2078 tmp.f_bfree = hmp->voldata.allocator_free /
2079 mp->mnt_vstat.f_bsize;
2080 tmp.f_bavail = tmp.f_bfree;
2082 if (cred && cred->cr_uid != 0) {
2086 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize;
2087 tmp.f_blocks -= adj;
2089 tmp.f_bavail -= adj;
2092 mp->mnt_stat.f_blocks = tmp.f_blocks;
2093 mp->mnt_stat.f_bfree = tmp.f_bfree;
2094 mp->mnt_stat.f_bavail = tmp.f_bavail;
2095 mp->mnt_stat.f_files = tmp.f_files;
2096 mp->mnt_stat.f_ffree = tmp.f_ffree;
2098 *sbp = mp->mnt_stat;
2105 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
2109 hammer2_blockref_t bref;
2114 * NOTE: iroot might not have validated the cluster yet.
2117 bzero(&tmp, sizeof(tmp));
2119 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
2120 hmp = pmp->pfs_hmps[i];
2123 if (pmp->iroot->cluster.array[i].chain)
2124 bref = pmp->iroot->cluster.array[i].chain->bref;
2126 bzero(&bref, sizeof(bref));
2128 tmp.f_files = bref.embed.stats.inode_count;
2130 tmp.f_blocks = hmp->voldata.allocator_size /
2131 mp->mnt_vstat.f_bsize;
2132 tmp.f_bfree = hmp->voldata.allocator_free /
2133 mp->mnt_vstat.f_bsize;
2134 tmp.f_bavail = tmp.f_bfree;
2136 if (cred && cred->cr_uid != 0) {
2140 adj = hmp->free_reserved / mp->mnt_vstat.f_bsize;
2141 tmp.f_blocks -= adj;
2143 tmp.f_bavail -= adj;
2146 mp->mnt_vstat.f_blocks = tmp.f_blocks;
2147 mp->mnt_vstat.f_bfree = tmp.f_bfree;
2148 mp->mnt_vstat.f_bavail = tmp.f_bavail;
2149 mp->mnt_vstat.f_files = tmp.f_files;
2150 mp->mnt_vstat.f_ffree = tmp.f_ffree;
2152 *sbp = mp->mnt_vstat;
2158 * Mount-time recovery (RW mounts)
2160 * Updates to the free block table are allowed to lag flushes by one
2161 * transaction. In case of a crash, then on a fresh mount we must do an
2162 * incremental scan of the last committed transaction id and make sure that
2163 * all related blocks have been marked allocated.
2165 struct hammer2_recovery_elm {
2166 TAILQ_ENTRY(hammer2_recovery_elm) entry;
2167 hammer2_chain_t *chain;
2168 hammer2_tid_t sync_tid;
2171 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
2173 struct hammer2_recovery_info {
2174 struct hammer2_recovery_list list;
2179 static int hammer2_recovery_scan(hammer2_dev_t *hmp,
2180 hammer2_chain_t *parent,
2181 struct hammer2_recovery_info *info,
2182 hammer2_tid_t sync_tid);
2184 #define HAMMER2_RECOVERY_MAXDEPTH 10
2188 hammer2_recovery(hammer2_dev_t *hmp)
2190 struct hammer2_recovery_info info;
2191 struct hammer2_recovery_elm *elm;
2192 hammer2_chain_t *parent;
2193 hammer2_tid_t sync_tid;
2194 hammer2_tid_t mirror_tid;
2197 hammer2_trans_init(hmp->spmp, 0);
2199 sync_tid = hmp->voldata.freemap_tid;
2200 mirror_tid = hmp->voldata.mirror_tid;
2202 kprintf("hammer2_mount: \"%s\": ", hmp->devrepname);
2203 if (sync_tid >= mirror_tid) {
2204 kprintf("no recovery needed\n");
2206 kprintf("freemap recovery %016jx-%016jx\n",
2207 sync_tid + 1, mirror_tid);
2210 TAILQ_INIT(&info.list);
2212 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
2213 error = hammer2_recovery_scan(hmp, parent, &info, sync_tid);
2214 hammer2_chain_lookup_done(parent);
2216 while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
2217 TAILQ_REMOVE(&info.list, elm, entry);
2218 parent = elm->chain;
2219 sync_tid = elm->sync_tid;
2220 kfree(elm, M_HAMMER2);
2222 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2223 error |= hammer2_recovery_scan(hmp, parent, &info,
2224 hmp->voldata.freemap_tid);
2225 hammer2_chain_unlock(parent);
2226 hammer2_chain_drop(parent); /* drop elm->chain ref */
2229 hammer2_trans_done(hmp->spmp, 0);
2236 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent,
2237 struct hammer2_recovery_info *info,
2238 hammer2_tid_t sync_tid)
2240 const hammer2_inode_data_t *ripdata;
2241 hammer2_chain_t *chain;
2242 hammer2_blockref_t bref;
2249 * Adjust freemap to ensure that the block(s) are marked allocated.
2251 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
2252 hammer2_freemap_adjust(hmp, &parent->bref,
2253 HAMMER2_FREEMAP_DORECOVER);
2257 * Check type for recursive scan
2259 switch(parent->bref.type) {
2260 case HAMMER2_BREF_TYPE_VOLUME:
2261 /* data already instantiated */
2263 case HAMMER2_BREF_TYPE_INODE:
2265 * Must instantiate data for DIRECTDATA test and also
2268 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2269 ripdata = &parent->data->ipdata;
2270 if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
2271 /* not applicable to recovery scan */
2272 hammer2_chain_unlock(parent);
2275 hammer2_chain_unlock(parent);
2277 case HAMMER2_BREF_TYPE_INDIRECT:
2279 * Must instantiate data for recursion
2281 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
2282 hammer2_chain_unlock(parent);
2284 case HAMMER2_BREF_TYPE_DIRENT:
2285 case HAMMER2_BREF_TYPE_DATA:
2286 case HAMMER2_BREF_TYPE_FREEMAP:
2287 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
2288 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
2289 /* not applicable to recovery scan */
2293 return HAMMER2_ERROR_BADBREF;
2297 * Defer operation if depth limit reached.
2299 if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
2300 struct hammer2_recovery_elm *elm;
2302 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
2303 elm->chain = parent;
2304 elm->sync_tid = sync_tid;
2305 hammer2_chain_ref(parent);
2306 TAILQ_INSERT_TAIL(&info->list, elm, entry);
2307 /* unlocked by caller */
2314 * Recursive scan of the last flushed transaction only. We are
2315 * doing this without pmp assignments so don't leave the chains
2316 * hanging around after we are done with them.
2318 * error Cumulative error this level only
2319 * rup_error Cumulative error for recursion
2320 * tmp_error Specific non-cumulative recursion error
2328 error |= hammer2_chain_scan(parent, &chain, &bref,
2330 HAMMER2_LOOKUP_NODATA);
2333 * Problem during scan or EOF
2341 if (chain == NULL) {
2342 if (bref.mirror_tid > sync_tid) {
2343 hammer2_freemap_adjust(hmp, &bref,
2344 HAMMER2_FREEMAP_DORECOVER);
2350 * This may or may not be a recursive node.
2352 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
2353 if (bref.mirror_tid > sync_tid) {
2355 tmp_error = hammer2_recovery_scan(hmp, chain,
2363 * Flush the recovery at the PFS boundary to stage it for
2364 * the final flush of the super-root topology.
2366 if (tmp_error == 0 &&
2367 (bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
2368 (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
2369 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
2372 rup_error |= tmp_error;
2374 return ((error | rup_error) & ~HAMMER2_ERROR_EOF);
2378 * This fixes up an error introduced in earlier H2 implementations where
2379 * moving a PFS inode into an indirect block wound up causing the
2380 * HAMMER2_BREF_FLAG_PFSROOT flag in the bref to get cleared.
2384 hammer2_fixup_pfses(hammer2_dev_t *hmp)
2386 const hammer2_inode_data_t *ripdata;
2387 hammer2_chain_t *parent;
2388 hammer2_chain_t *chain;
2389 hammer2_key_t key_next;
2390 hammer2_pfs_t *spmp;
2396 * Lookup mount point under the media-localized super-root.
2398 * cluster->pmp will incorrectly point to spmp and must be fixed
2402 hammer2_inode_lock(spmp->iroot, 0);
2403 parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
2404 chain = hammer2_chain_lookup(&parent, &key_next,
2405 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
2408 if (chain->bref.type != HAMMER2_BREF_TYPE_INODE)
2411 kprintf("I/O error scanning PFS labels\n");
2412 error |= chain->error;
2413 } else if ((chain->bref.flags &
2414 HAMMER2_BREF_FLAG_PFSROOT) == 0) {
2417 ripdata = &chain->data->ipdata;
2418 hammer2_trans_init(hmp->spmp, 0);
2419 error2 = hammer2_chain_modify(chain,
2420 chain->bref.modify_tid,
2423 kprintf("hammer2: Correct mis-flagged PFS %s\n",
2425 chain->bref.flags |= HAMMER2_BREF_FLAG_PFSROOT;
2429 hammer2_flush(chain, HAMMER2_FLUSH_TOP |
2431 hammer2_trans_done(hmp->spmp, 0);
2433 chain = hammer2_chain_next(&parent, chain, &key_next,
2434 key_next, HAMMER2_KEY_MAX,
2438 hammer2_chain_unlock(parent);
2439 hammer2_chain_drop(parent);
2441 hammer2_inode_unlock(spmp->iroot);
2447 * Sync a mount point; this is called periodically on a per-mount basis from
2448 * the filesystem syncer, and whenever a user issues a sync.
2451 hammer2_vfs_sync(struct mount *mp, int waitfor)
2455 error = hammer2_vfs_sync_pmp(MPTOPMP(mp), waitfor);
2461 * Because frontend operations lock vnodes before we get a chance to
2462 * lock the related inode, we can't just acquire a vnode lock without
2463 * risking a deadlock. The frontend may be holding a vnode lock while
2464 * also blocked on our SYNCQ flag while trying to get the inode lock.
2466 * To deal with this situation we can check the vnode lock situation
2467 * after locking the inode and perform a work-around.
2470 hammer2_vfs_sync_pmp(hammer2_pfs_t *pmp, int waitfor)
2473 /*hammer2_xop_flush_t *xop;*/
2474 /*struct hammer2_sync_info info;*/
2475 hammer2_inode_t *ip;
2476 hammer2_depend_t *depend;
2477 hammer2_depend_t *depend_next;
2487 * Move all inodes on sideq to syncq. This will clear sideq.
2488 * This should represent all flushable inodes. These inodes
2489 * will already have refs due to being on syncq or sideq. We
2490 * must do this all at once with the spinlock held to ensure that
2491 * all inode dependencies are part of the same flush.
2493 * We should be able to do this asynchronously from frontend
2494 * operations because we will be locking the inodes later on
2495 * to actually flush them, and that will partition any frontend
2496 * op using the same inode. Either it has already locked the
2497 * inode and we will block, or it has not yet locked the inode
2498 * and it will block until we are finished flushing that inode.
2500 * When restarting, only move the inodes flagged as PASS2 from
2501 * SIDEQ to SYNCQ. PASS2 propagation by inode_lock4() and
2502 * inode_depend() are atomic with the spin-lock.
2504 hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH);
2505 #ifdef HAMMER2_DEBUG_SYNC
2506 kprintf("FILESYSTEM SYNC BOUNDARY\n");
2511 * Move inodes from depq to syncq, releasing the related
2512 * depend structures.
2515 #ifdef HAMMER2_DEBUG_SYNC
2516 kprintf("FILESYSTEM SYNC RESTART (%d)\n", dorestart);
2518 hammer2_trans_setflags(pmp, 0/*HAMMER2_TRANS_COPYQ*/);
2519 hammer2_trans_clearflags(pmp, HAMMER2_TRANS_RESCAN);
2522 * Move inodes from depq to syncq. When restarting, only depq's
2523 * marked pass2 are moved.
2525 hammer2_spin_ex(&pmp->list_spin);
2526 depend_next = TAILQ_FIRST(&pmp->depq);
2529 while ((depend = depend_next) != NULL) {
2530 depend_next = TAILQ_NEXT(depend, entry);
2531 if (dorestart && depend->pass2 == 0)
2533 TAILQ_FOREACH(ip, &depend->sideq, entry) {
2534 KKASSERT(ip->flags & HAMMER2_INODE_SIDEQ);
2535 atomic_set_int(&ip->flags, HAMMER2_INODE_SYNCQ);
2536 atomic_clear_int(&ip->flags, HAMMER2_INODE_SIDEQ);
2541 * NOTE: pmp->sideq_count includes both sideq and syncq
2543 TAILQ_CONCAT(&pmp->syncq, &depend->sideq, entry);
2547 TAILQ_REMOVE(&pmp->depq, depend, entry);
2550 hammer2_spin_unex(&pmp->list_spin);
2551 hammer2_trans_clearflags(pmp, /*HAMMER2_TRANS_COPYQ |*/
2552 HAMMER2_TRANS_WAITING);
2556 * sideq_count may have dropped enough to allow us to unstall
2559 hammer2_pfs_memory_wakeup(pmp, 0);
2562 * Now run through all inodes on syncq.
2564 * Flush transactions only interlock with other flush transactions.
2565 * Any conflicting frontend operations will block on the inode, but
2566 * may hold a vnode lock while doing so.
2568 hammer2_spin_ex(&pmp->list_spin);
2569 while ((ip = TAILQ_FIRST(&pmp->syncq)) != NULL) {
2571 * Remove the inode from the SYNCQ, transfer the syncq ref
2572 * to us. We must clear SYNCQ to allow any potential
2573 * front-end deadlock to proceed. We must set PASS2 so
2574 * the dependency code knows what to do.
2578 if (atomic_cmpset_int(&ip->flags,
2580 (pass2 & ~(HAMMER2_INODE_SYNCQ |
2581 HAMMER2_INODE_SYNCQ_WAKEUP)) |
2582 HAMMER2_INODE_SYNCQ_PASS2) == 0) {
2585 TAILQ_REMOVE(&pmp->syncq, ip, entry);
2587 hammer2_spin_unex(&pmp->list_spin);
2590 * Tickle anyone waiting on ip->flags or the hysteresis
2591 * on the dirty inode count.
2593 if (pass2 & HAMMER2_INODE_SYNCQ_WAKEUP)
2595 if (++wakecount >= hammer2_limit_dirty_inodes / 20 + 1) {
2597 hammer2_pfs_memory_wakeup(pmp, 0);
2601 * Relock the inode, and we inherit a ref from the above.
2602 * We will check for a race after we acquire the vnode.
2604 hammer2_mtx_ex(&ip->lock);
2607 * We need the vp in order to vfsync() dirty buffers, so if
2608 * one isn't attached we can skip it.
2610 * Ordering the inode lock and then the vnode lock has the
2611 * potential to deadlock. If we had left SYNCQ set that could
2612 * also deadlock us against the frontend even if we don't hold
2613 * any locks, but the latter is not a problem now since we
2614 * cleared it. igetv will temporarily release the inode lock
2615 * in a safe manner to work-around the deadlock.
2617 * Unfortunately it is still possible to deadlock when the
2618 * frontend obtains multiple inode locks, because all the
2619 * related vnodes are already locked (nor can the vnode locks
2620 * be released and reacquired without messing up RECLAIM and
2621 * INACTIVE sequencing).
2623 * The solution for now is to move the vp back onto SIDEQ
2624 * and set dorestart, which will restart the flush after we
2625 * exhaust the current SYNCQ. Note that additional
2626 * dependencies may build up, so we definitely need to move
2627 * the whole SIDEQ back to SYNCQ when we restart.
2631 if (vget(vp, LK_EXCLUSIVE|LK_NOWAIT)) {
2633 * Failed to get the vnode, requeue the inode
2634 * (PASS2 is already set so it will be found
2635 * again on the restart).
2637 * Then unlock, possibly sleep, and retry
2638 * later. We sleep if PASS2 was *previously*
2639 * set, before we set it again above.
2643 #ifdef HAMMER2_DEBUG_SYNC
2644 kprintf("inum %ld (sync delayed by vnode)\n",
2645 (long)ip->meta.inum);
2647 hammer2_inode_delayed_sideq(ip);
2649 hammer2_mtx_unlock(&ip->lock);
2650 hammer2_inode_drop(ip);
2652 if (pass2 & HAMMER2_INODE_SYNCQ_PASS2) {
2653 tsleep(&dorestart, 0, "h2syndel", 2);
2655 hammer2_spin_ex(&pmp->list_spin);
2663 * If the inode wound up on a SIDEQ again it will already be
2664 * prepped for another PASS2. In this situation if we flush
2665 * it now we will just wind up flushing it again in the same
2666 * syncer run, so we might as well not flush it now.
2668 if (ip->flags & HAMMER2_INODE_SIDEQ) {
2669 hammer2_mtx_unlock(&ip->lock);
2670 hammer2_inode_drop(ip);
2674 hammer2_spin_ex(&pmp->list_spin);
2679 * Ok we have the inode exclusively locked and if vp is
2680 * not NULL that will also be exclusively locked. Do the
2681 * meat of the flush.
2683 * vp token needed for v_rbdirty_tree check / vclrisdirty
2684 * sequencing. Though we hold the vnode exclusively so
2685 * we shouldn't need to hold the token also in this case.
2688 vfsync(vp, MNT_WAIT, 1, NULL, NULL);
2689 bio_track_wait(&vp->v_track_write, 0, 0); /* XXX */
2693 * If the inode has not yet been inserted into the tree
2694 * we must do so. Then sync and flush it. The flush should
2695 * update the parent.
2697 if (ip->flags & HAMMER2_INODE_DELETING) {
2698 #ifdef HAMMER2_DEBUG_SYNC
2699 kprintf("inum %ld destroy\n", (long)ip->meta.inum);
2701 hammer2_inode_chain_des(ip);
2702 atomic_add_long(&hammer2_iod_inode_deletes, 1);
2703 } else if (ip->flags & HAMMER2_INODE_CREATING) {
2704 #ifdef HAMMER2_DEBUG_SYNC
2705 kprintf("inum %ld insert\n", (long)ip->meta.inum);
2707 hammer2_inode_chain_ins(ip);
2708 atomic_add_long(&hammer2_iod_inode_creates, 1);
2710 #ifdef HAMMER2_DEBUG_SYNC
2711 kprintf("inum %ld chain-sync\n", (long)ip->meta.inum);
2715 * Because I kinda messed up the design and index the inodes
2716 * under the root inode, along side the directory entries,
2717 * we can't flush the inode index under the iroot until the
2718 * end. If we do it now we might miss effects created by
2719 * other inodes on the SYNCQ.
2721 * Do a normal (non-FSSYNC) flush instead, which allows the
2722 * vnode code to work the same. We don't want to force iroot
2723 * back onto the SIDEQ, and we also don't want the flush code
2724 * to update pfs_iroot_blocksets until the final flush later.
2726 * XXX at the moment this will likely result in a double-flush
2727 * of the iroot chain.
2729 hammer2_inode_chain_sync(ip);
2730 if (ip == pmp->iroot) {
2731 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP);
2733 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP |
2734 HAMMER2_XOP_FSSYNC);
2737 lwkt_gettoken(&vp->v_token);
2738 if ((ip->flags & (HAMMER2_INODE_MODIFIED |
2739 HAMMER2_INODE_RESIZED |
2740 HAMMER2_INODE_DIRTYDATA)) == 0 &&
2741 RB_EMPTY(&vp->v_rbdirty_tree) &&
2742 !bio_track_active(&vp->v_track_write)) {
2745 hammer2_inode_delayed_sideq(ip);
2747 lwkt_reltoken(&vp->v_token);
2749 vp = NULL; /* safety */
2751 atomic_clear_int(&ip->flags, HAMMER2_INODE_SYNCQ_PASS2);
2752 hammer2_inode_unlock(ip); /* unlock+drop */
2753 /* ip pointer invalid */
2756 * If the inode got dirted after we dropped our locks,
2757 * it will have already been moved back to the SIDEQ.
2759 hammer2_spin_ex(&pmp->list_spin);
2761 hammer2_spin_unex(&pmp->list_spin);
2762 hammer2_pfs_memory_wakeup(pmp, 0);
2764 if (dorestart || (pmp->trans.flags & HAMMER2_TRANS_RESCAN)) {
2765 #ifdef HAMMER2_DEBUG_SYNC
2766 kprintf("FILESYSTEM SYNC STAGE 1 RESTART\n");
2767 /*tsleep(&dorestart, 0, "h2STG1-R", hz*20);*/
2772 #ifdef HAMMER2_DEBUG_SYNC
2773 kprintf("FILESYSTEM SYNC STAGE 2 BEGIN\n");
2774 /*tsleep(&dorestart, 0, "h2STG2", hz*20);*/
2778 * We have to flush the PFS root last, even if it does not appear to
2779 * be dirty, because all the inodes in the PFS are indexed under it.
2780 * The normal flushing of iroot above would only occur if directory
2781 * entries under the root were changed.
2783 * Specifying VOLHDR will cause an additionl flush of hmp->spmp
2784 * for the media making up the cluster.
2786 if ((ip = pmp->iroot) != NULL) {
2787 hammer2_inode_ref(ip);
2788 hammer2_mtx_ex(&ip->lock);
2789 hammer2_inode_chain_sync(ip);
2790 hammer2_inode_chain_flush(ip, HAMMER2_XOP_INODE_STOP |
2791 HAMMER2_XOP_FSSYNC |
2792 HAMMER2_XOP_VOLHDR);
2793 hammer2_inode_unlock(ip); /* unlock+drop */
2795 #ifdef HAMMER2_DEBUG_SYNC
2796 kprintf("FILESYSTEM SYNC STAGE 2 DONE\n");
2802 hammer2_bioq_sync(pmp);
2806 * Generally speaking we now want to flush the media topology from
2807 * the iroot through to the inodes. The flush stops at any inode
2808 * boundary, which allows the frontend to continue running concurrent
2809 * modifying operations on inodes (including kernel flushes of
2810 * buffers) without interfering with the main sync.
2812 * Use the XOP interface to concurrently flush all nodes to
2813 * synchronize the PFSROOT subtopology to the media. A standard
2814 * end-of-scan ENOENT error indicates cluster sufficiency.
2816 * Note that this flush will not be visible on crash recovery until
2817 * we flush the super-root topology in the next loop.
2819 * XXX For now wait for all flushes to complete.
2821 if (mp && (ip = pmp->iroot) != NULL) {
2823 * If unmounting try to flush everything including any
2824 * sub-trees under inodes, just in case there is dangling
2825 * modified data, as a safety. Otherwise just flush up to
2826 * the inodes in this stage.
2828 kprintf("MP & IROOT\n");
2829 #ifdef HAMMER2_DEBUG_SYNC
2830 kprintf("FILESYSTEM SYNC STAGE 3 IROOT BEGIN\n");
2832 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
2833 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING |
2834 HAMMER2_XOP_VOLHDR |
2835 HAMMER2_XOP_FSSYNC |
2836 HAMMER2_XOP_INODE_STOP);
2838 xop = hammer2_xop_alloc(ip, HAMMER2_XOP_MODIFYING |
2839 HAMMER2_XOP_INODE_STOP |
2840 HAMMER2_XOP_VOLHDR |
2841 HAMMER2_XOP_FSSYNC |
2842 HAMMER2_XOP_INODE_STOP);
2844 hammer2_xop_start(&xop->head, &hammer2_inode_flush_desc);
2845 error = hammer2_xop_collect(&xop->head,
2846 HAMMER2_XOP_COLLECT_WAITALL);
2847 hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2848 #ifdef HAMMER2_DEBUG_SYNC
2849 kprintf("FILESYSTEM SYNC STAGE 3 IROOT END\n");
2851 if (error == HAMMER2_ERROR_ENOENT)
2854 error = hammer2_error_to_errno(error);
2859 error = 0; /* XXX */
2860 hammer2_trans_done(pmp, HAMMER2_TRANS_ISFLUSH);
2867 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2869 hammer2_inode_t *ip;
2871 KKASSERT(MAXFIDSZ >= 16);
2873 fhp->fid_len = offsetof(struct fid, fid_data[16]);
2875 ((hammer2_tid_t *)fhp->fid_data)[0] = ip->meta.inum;
2876 ((hammer2_tid_t *)fhp->fid_data)[1] = 0;
2883 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2884 struct fid *fhp, struct vnode **vpp)
2891 inum = ((hammer2_tid_t *)fhp->fid_data)[0] & HAMMER2_DIRHASH_USERMSK;
2894 error = hammer2_vfs_root(mp, vpp);
2896 error = hammer2_vfs_vget(mp, NULL, inum, vpp);
2901 kprintf("fhtovp: %016jx -> %p, %d\n", inum, *vpp, error);
2907 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2908 int *exflagsp, struct ucred **credanonp)
2915 np = vfs_export_lookup(mp, &pmp->export, nam);
2917 *exflagsp = np->netc_exflags;
2918 *credanonp = &np->netc_anon;
2927 * This handles hysteresis on regular file flushes. Because the BIOs are
2928 * routed to a thread it is possible for an excessive number to build up
2929 * and cause long front-end stalls long before the runningbuffspace limit
2930 * is hit, so we implement hammer2_flush_pipe to control the
2933 * This is a particular problem when compression is used.
2936 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2938 atomic_add_int(&pmp->count_lwinprog, 1);
2942 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2946 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2947 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2948 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2949 atomic_clear_int(&pmp->count_lwinprog,
2950 HAMMER2_LWINPROG_WAITING);
2951 wakeup(&pmp->count_lwinprog);
2953 if ((lwinprog & HAMMER2_LWINPROG_WAITING0) &&
2954 (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) {
2955 atomic_clear_int(&pmp->count_lwinprog,
2956 HAMMER2_LWINPROG_WAITING0);
2957 wakeup(&pmp->count_lwinprog);
2962 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe)
2965 int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING :
2966 HAMMER2_LWINPROG_WAITING0;
2969 lwinprog = pmp->count_lwinprog;
2971 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2973 tsleep_interlock(&pmp->count_lwinprog, 0);
2974 atomic_set_int(&pmp->count_lwinprog, lwflag);
2975 lwinprog = pmp->count_lwinprog;
2976 if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2978 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2983 * It is possible for an excessive number of dirty chains or dirty inodes
2984 * to build up. When this occurs we start an asynchronous filesystem sync.
2985 * If the level continues to build up, we stall, waiting for it to drop,
2986 * with some hysteresis.
2988 * This relies on the kernel calling hammer2_vfs_modifying() prior to
2989 * obtaining any vnode locks before making a modifying VOP call.
2992 hammer2_vfs_modifying(struct mount *mp)
2994 if (mp->mnt_flag & MNT_RDONLY)
2996 hammer2_pfs_memory_wait(MPTOPMP(mp));
3002 * Initiate an asynchronous filesystem sync and, with hysteresis,
3003 * stall if the internal data structure count becomes too bloated.
3006 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
3012 if (pmp == NULL || pmp->mp == NULL)
3016 waiting = pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK;
3020 * Start the syncer running at 1/2 the limit
3022 if (waiting > hammer2_limit_dirty_chains / 2 ||
3023 pmp->sideq_count > hammer2_limit_dirty_inodes / 2) {
3024 trigger_syncer(pmp->mp);
3028 * Stall at the limit waiting for the counts to drop.
3029 * This code will typically be woken up once the count
3030 * drops below 3/4 the limit, or in one second.
3032 if (waiting < hammer2_limit_dirty_chains &&
3033 pmp->sideq_count < hammer2_limit_dirty_inodes) {
3037 pcatch = curthread->td_proc ? PCATCH : 0;
3039 tsleep_interlock(&pmp->inmem_dirty_chains, pcatch);
3040 atomic_set_int(&pmp->inmem_dirty_chains,
3041 HAMMER2_DIRTYCHAIN_WAITING);
3042 if (waiting < hammer2_limit_dirty_chains &&
3043 pmp->sideq_count < hammer2_limit_dirty_inodes) {
3046 trigger_syncer(pmp->mp);
3047 error = tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED | pcatch,
3049 if (error == ERESTART)
3055 * Wake up any stalled frontend ops waiting, with hysteresis, using
3059 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp, int count)
3064 waiting = atomic_fetchadd_int(&pmp->inmem_dirty_chains, count);
3065 /* don't need --waiting to test flag */
3067 if ((waiting & HAMMER2_DIRTYCHAIN_WAITING) &&
3068 (pmp->inmem_dirty_chains & HAMMER2_DIRTYCHAIN_MASK) <=
3069 hammer2_limit_dirty_chains * 2 / 3 &&
3070 pmp->sideq_count <= hammer2_limit_dirty_inodes * 2 / 3) {
3071 atomic_clear_int(&pmp->inmem_dirty_chains,
3072 HAMMER2_DIRTYCHAIN_WAITING);
3073 wakeup(&pmp->inmem_dirty_chains);
3079 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
3082 atomic_add_int(&pmp->inmem_dirty_chains, 1);
3087 * Volume header data locks
3090 hammer2_voldata_lock(hammer2_dev_t *hmp)
3092 lockmgr(&hmp->vollk, LK_EXCLUSIVE);
3096 hammer2_voldata_unlock(hammer2_dev_t *hmp)
3098 lockmgr(&hmp->vollk, LK_RELEASE);
3102 hammer2_voldata_modify(hammer2_dev_t *hmp)
3104 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) == 0) {
3105 atomic_add_long(&hammer2_count_modified_chains, 1);
3106 atomic_set_int(&hmp->vchain.flags, HAMMER2_CHAIN_MODIFIED);
3107 hammer2_pfs_memory_inc(hmp->vchain.pmp);
3112 * Returns 0 if the filesystem has tons of free space
3113 * Returns 1 if the filesystem has less than 10% remaining
3114 * Returns 2 if the filesystem has less than 2%/5% (user/root) remaining.
3117 hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred)
3121 hammer2_off_t free_reserved;
3122 hammer2_off_t free_nominal;
3127 if (pmp->free_ticks == 0 || pmp->free_ticks != ticks) {
3128 free_reserved = HAMMER2_SEGSIZE;
3129 free_nominal = 0x7FFFFFFFFFFFFFFFLLU;
3130 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
3131 hmp = pmp->pfs_hmps[i];
3134 if (pmp->pfs_types[i] != HAMMER2_PFSTYPE_MASTER &&
3135 pmp->pfs_types[i] != HAMMER2_PFSTYPE_SOFT_MASTER)
3138 if (free_nominal > hmp->voldata.allocator_free)
3139 free_nominal = hmp->voldata.allocator_free;
3140 if (free_reserved < hmp->free_reserved)
3141 free_reserved = hmp->free_reserved;
3147 pmp->free_reserved = free_reserved;
3148 pmp->free_nominal = free_nominal;
3149 pmp->free_ticks = ticks;
3151 free_reserved = pmp->free_reserved;
3152 free_nominal = pmp->free_nominal;
3154 if (cred && cred->cr_uid != 0) {
3155 if ((int64_t)(free_nominal - bytes) <
3156 (int64_t)free_reserved) {
3160 if ((int64_t)(free_nominal - bytes) <
3161 (int64_t)free_reserved / 2) {
3165 if ((int64_t)(free_nominal - bytes) < (int64_t)free_reserved * 2)
3174 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int bi, int *countp,
3175 char pfx, u_int flags)
3177 hammer2_chain_t *scan;
3178 hammer2_chain_t *parent;
3182 kprintf("%*.*s...\n", tab, tab, "");
3187 kprintf("%*.*s%c-chain %p %s.%-3d %016jx %016jx/%-2d mir=%016jx\n",
3188 tab, tab, "", pfx, chain,
3189 hammer2_bref_type_str(chain->bref.type), bi,
3190 chain->bref.data_off, chain->bref.key, chain->bref.keybits,
3191 chain->bref.mirror_tid);
3193 kprintf("%*.*s [%08x] (%s) refs=%d",
3196 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
3197 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
3200 parent = chain->parent;
3202 kprintf("\n%*.*s p=%p [pflags %08x prefs %d]",
3204 parent, parent->flags, parent->refs);
3205 if (RB_EMPTY(&chain->core.rbtree)) {
3210 RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree) {
3211 if ((scan->flags & flags) || flags == (u_int)-1) {
3212 hammer2_dump_chain(scan, tab + 4, bi, countp,
3217 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
3218 kprintf("%*.*s}(%s)\n", tab, tab, "",
3219 chain->data->ipdata.filename);
3221 kprintf("%*.*s}\n", tab, tab, "");