132b800e6SMatthew Dillon /* 2d34788efSMatthew Dillon * Copyright (c) 2011-2015 The DragonFly Project. All rights reserved. 332b800e6SMatthew Dillon * 432b800e6SMatthew Dillon * This code is derived from software contributed to The DragonFly Project 532b800e6SMatthew Dillon * by Matthew Dillon <dillon@dragonflybsd.org> 632b800e6SMatthew Dillon * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 732b800e6SMatthew Dillon * 832b800e6SMatthew Dillon * Redistribution and use in source and binary forms, with or without 932b800e6SMatthew Dillon * modification, are permitted provided that the following conditions 1032b800e6SMatthew Dillon * are met: 1132b800e6SMatthew Dillon * 1232b800e6SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 1332b800e6SMatthew Dillon * notice, this list of conditions and the following disclaimer. 1432b800e6SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 1532b800e6SMatthew Dillon * notice, this list of conditions and the following disclaimer in 1632b800e6SMatthew Dillon * the documentation and/or other materials provided with the 1732b800e6SMatthew Dillon * distribution. 1832b800e6SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its 1932b800e6SMatthew Dillon * contributors may be used to endorse or promote products derived 2032b800e6SMatthew Dillon * from this software without specific, prior written permission. 2132b800e6SMatthew Dillon * 2232b800e6SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2332b800e6SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2432b800e6SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 2532b800e6SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 2632b800e6SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2732b800e6SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 2832b800e6SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 2932b800e6SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 3032b800e6SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 3132b800e6SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 3232b800e6SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3332b800e6SMatthew Dillon * SUCH DAMAGE. 3432b800e6SMatthew Dillon */ 3550456506SMatthew Dillon /* 3650456506SMatthew Dillon * TRANSACTION AND FLUSH HANDLING 3750456506SMatthew Dillon * 3850456506SMatthew Dillon * Deceptively simple but actually fairly difficult to implement properly is 3950456506SMatthew Dillon * how I would describe it. 4050456506SMatthew Dillon * 41da6f36f4SMatthew Dillon * Flushing generally occurs bottom-up but requires a top-down scan to 42da6f36f4SMatthew Dillon * locate chains with MODIFIED and/or UPDATE bits set. The ONFLUSH flag 43da6f36f4SMatthew Dillon * tells how to recurse downward to find these chains. 4450456506SMatthew Dillon */ 4550456506SMatthew Dillon 4632b800e6SMatthew Dillon #include <sys/cdefs.h> 4732b800e6SMatthew Dillon #include <sys/param.h> 4832b800e6SMatthew Dillon #include <sys/systm.h> 4932b800e6SMatthew Dillon #include <sys/types.h> 5032b800e6SMatthew Dillon #include <sys/lock.h> 5132b800e6SMatthew Dillon #include <sys/uuid.h> 5232b800e6SMatthew Dillon 5332b800e6SMatthew Dillon #include "hammer2.h" 5432b800e6SMatthew Dillon 55925e4ad1SMatthew Dillon #define FLUSH_DEBUG 0 56925e4ad1SMatthew Dillon 57a71db85dSMatthew Dillon #define HAMMER2_FLUSH_DEPTH_LIMIT 10 /* stack recursion limit */ 58a71db85dSMatthew Dillon 59a71db85dSMatthew Dillon 6032b800e6SMatthew Dillon /* 6132b800e6SMatthew Dillon * Recursively flush the specified chain. The chain is locked and 6232b800e6SMatthew Dillon * referenced by the caller and will remain so on return. The chain 6332b800e6SMatthew Dillon * will remain referenced throughout but can temporarily lose its 6432b800e6SMatthew Dillon * lock during the recursion to avoid unnecessarily stalling user 6532b800e6SMatthew Dillon * processes. 6632b800e6SMatthew Dillon */ 6732b800e6SMatthew Dillon struct hammer2_flush_info { 680dea3156SMatthew Dillon hammer2_chain_t *parent; 6932b800e6SMatthew Dillon int depth; 700dea3156SMatthew Dillon int diddeferral; 711897c66eSMatthew Dillon int cache_index; 7253f84d31SMatthew Dillon int flags; 73da6f36f4SMatthew Dillon struct h2_flush_list flushq; 74850687d2SMatthew Dillon hammer2_chain_t *debug; 7532b800e6SMatthew Dillon }; 7632b800e6SMatthew Dillon 7732b800e6SMatthew Dillon typedef struct hammer2_flush_info hammer2_flush_info_t; 7832b800e6SMatthew Dillon 798138a154SMatthew Dillon static void hammer2_flush_core(hammer2_flush_info_t *info, 8053f84d31SMatthew Dillon hammer2_chain_t *chain, int flags); 81da6f36f4SMatthew Dillon static int hammer2_flush_recurse(hammer2_chain_t *child, void *data); 8293f3933aSMatthew Dillon 8332b800e6SMatthew Dillon /* 84c603b86bSMatthew Dillon * Any per-pfs transaction initialization goes here. 8550456506SMatthew Dillon */ 8650456506SMatthew Dillon void 87c603b86bSMatthew Dillon hammer2_trans_manage_init(hammer2_pfs_t *pmp) 8850456506SMatthew Dillon { 8950456506SMatthew Dillon } 9050456506SMatthew Dillon 9150456506SMatthew Dillon /* 92d34788efSMatthew Dillon * Transaction support for any modifying operation. Transactions are used 93d34788efSMatthew Dillon * in the pmp layer by the frontend and in the spmp layer by the backend. 94c603b86bSMatthew Dillon * 95c603b86bSMatthew Dillon * 0 - Normal transaction, interlocked against flush 96c603b86bSMatthew Dillon * transaction. 97c603b86bSMatthew Dillon * 98c603b86bSMatthew Dillon * TRANS_ISFLUSH - Flush transaction, interlocked against normal 99c603b86bSMatthew Dillon * transaction. 100c603b86bSMatthew Dillon * 101c603b86bSMatthew Dillon * TRANS_BUFCACHE - Buffer cache transaction, no interlock. 1020dea3156SMatthew Dillon * 10310136ab6SMatthew Dillon * Initializing a new transaction allocates a transaction ID. Typically 10410136ab6SMatthew Dillon * passed a pmp (hmp passed as NULL), indicating a cluster transaction. Can 10510136ab6SMatthew Dillon * be passed a NULL pmp and non-NULL hmp to indicate a transaction on a single 10610136ab6SMatthew Dillon * media target. The latter mode is used by the recovery code. 10710136ab6SMatthew Dillon * 108623d43d4SMatthew Dillon * TWO TRANSACTION IDs can run concurrently, where one is a flush and the 109623d43d4SMatthew Dillon * other is a set of any number of concurrent filesystem operations. We 110623d43d4SMatthew Dillon * can either have <running_fs_ops> + <waiting_flush> + <blocked_fs_ops> 111623d43d4SMatthew Dillon * or we can have <running_flush> + <concurrent_fs_ops>. 1120dea3156SMatthew Dillon * 113623d43d4SMatthew Dillon * During a flush, new fs_ops are only blocked until the fs_ops prior to 114623d43d4SMatthew Dillon * the flush complete. The new fs_ops can then run concurrent with the flush. 115d001f460SMatthew Dillon * 116623d43d4SMatthew Dillon * Buffer-cache transactions operate as fs_ops but never block. A 117623d43d4SMatthew Dillon * buffer-cache flush will run either before or after the current pending 118623d43d4SMatthew Dillon * flush depending on its state. 1190dea3156SMatthew Dillon */ 1200dea3156SMatthew Dillon void 121c603b86bSMatthew Dillon hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags) 1220dea3156SMatthew Dillon { 123c603b86bSMatthew Dillon uint32_t oflags; 124c603b86bSMatthew Dillon uint32_t nflags; 125c603b86bSMatthew Dillon int dowait; 126d001f460SMatthew Dillon 127c603b86bSMatthew Dillon for (;;) { 128c603b86bSMatthew Dillon oflags = pmp->trans.flags; 129c603b86bSMatthew Dillon cpu_ccfence(); 130c603b86bSMatthew Dillon dowait = 0; 131d001f460SMatthew Dillon 132d001f460SMatthew Dillon if (flags & HAMMER2_TRANS_ISFLUSH) { 133d001f460SMatthew Dillon /* 134c603b86bSMatthew Dillon * Requesting flush transaction. Wait for all 135c603b86bSMatthew Dillon * currently running transactions to finish. 136355d67fcSMatthew Dillon */ 137c603b86bSMatthew Dillon if (oflags & HAMMER2_TRANS_MASK) { 138c603b86bSMatthew Dillon nflags = oflags | HAMMER2_TRANS_FPENDING | 139c603b86bSMatthew Dillon HAMMER2_TRANS_WAITING; 140c603b86bSMatthew Dillon dowait = 1; 141c603b86bSMatthew Dillon } else { 142c603b86bSMatthew Dillon nflags = (oflags | flags) + 1; 143c603b86bSMatthew Dillon } 144c603b86bSMatthew Dillon } else if (flags & HAMMER2_TRANS_BUFCACHE) { 145a7720be7SMatthew Dillon /* 146c603b86bSMatthew Dillon * Requesting strategy transaction. Generally 147c603b86bSMatthew Dillon * allowed in all situations unless a flush 148c603b86bSMatthew Dillon * is running without the preflush flag. 149a7720be7SMatthew Dillon */ 150c603b86bSMatthew Dillon if ((oflags & (HAMMER2_TRANS_ISFLUSH | 151c603b86bSMatthew Dillon HAMMER2_TRANS_PREFLUSH)) == 152c603b86bSMatthew Dillon HAMMER2_TRANS_ISFLUSH) { 153c603b86bSMatthew Dillon nflags = oflags | HAMMER2_TRANS_WAITING; 154c603b86bSMatthew Dillon dowait = 1; 155c603b86bSMatthew Dillon } else { 156c603b86bSMatthew Dillon nflags = (oflags | flags) + 1; 157052e0aa0SMatthew Dillon } 158a4dc31e0SMatthew Dillon } else { 159a4dc31e0SMatthew Dillon /* 160c603b86bSMatthew Dillon * Requesting normal transaction. Wait for any 161c603b86bSMatthew Dillon * flush to finish before allowing. 162a4dc31e0SMatthew Dillon */ 163c603b86bSMatthew Dillon if (oflags & HAMMER2_TRANS_ISFLUSH) { 164c603b86bSMatthew Dillon nflags = oflags | HAMMER2_TRANS_WAITING; 165c603b86bSMatthew Dillon dowait = 1; 166c603b86bSMatthew Dillon } else { 167c603b86bSMatthew Dillon nflags = (oflags | flags) + 1; 168c603b86bSMatthew Dillon } 169c603b86bSMatthew Dillon } 170c603b86bSMatthew Dillon if (dowait) 171c603b86bSMatthew Dillon tsleep_interlock(&pmp->trans.sync_wait, 0); 172c603b86bSMatthew Dillon if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) { 173c603b86bSMatthew Dillon if (dowait == 0) 174a4dc31e0SMatthew Dillon break; 175c603b86bSMatthew Dillon tsleep(&pmp->trans.sync_wait, PINTERLOCKED, 176c603b86bSMatthew Dillon "h2trans", hz); 177c603b86bSMatthew Dillon } else { 178c603b86bSMatthew Dillon cpu_pause(); 179a7720be7SMatthew Dillon } 180c603b86bSMatthew Dillon /* retry */ 181c603b86bSMatthew Dillon } 182c603b86bSMatthew Dillon } 183a4dc31e0SMatthew Dillon 184e2163f5bSMatthew Dillon /* 185e2163f5bSMatthew Dillon * Start a sub-transaction, there is no 'subdone' function. This will 18653f84d31SMatthew Dillon * issue a new modify_tid (mtid) for the current transaction, which is a 18753f84d31SMatthew Dillon * CLC (cluster level change) id and not a per-node id. 18853f84d31SMatthew Dillon * 18953f84d31SMatthew Dillon * This function must be called for each XOP when multiple XOPs are run in 19053f84d31SMatthew Dillon * sequence within a transaction. 19153f84d31SMatthew Dillon * 19253f84d31SMatthew Dillon * Callers typically update the inode with the transaction mtid manually 19353f84d31SMatthew Dillon * to enforce sequencing. 194e2163f5bSMatthew Dillon */ 195e2163f5bSMatthew Dillon hammer2_tid_t 196e2163f5bSMatthew Dillon hammer2_trans_sub(hammer2_pfs_t *pmp) 197e2163f5bSMatthew Dillon { 198e2163f5bSMatthew Dillon hammer2_tid_t mtid; 199e2163f5bSMatthew Dillon 200e2163f5bSMatthew Dillon mtid = atomic_fetchadd_64(&pmp->modify_tid, 1); 201e2163f5bSMatthew Dillon 202e2163f5bSMatthew Dillon return (mtid); 203e2163f5bSMatthew Dillon } 204e2163f5bSMatthew Dillon 205e2163f5bSMatthew Dillon /* 206e2163f5bSMatthew Dillon * Clears the PREFLUSH stage, called during a flush transaction after all 207e2163f5bSMatthew Dillon * logical buffer I/O has completed. 208e2163f5bSMatthew Dillon */ 209e2163f5bSMatthew Dillon void 210e2163f5bSMatthew Dillon hammer2_trans_clear_preflush(hammer2_pfs_t *pmp) 211e2163f5bSMatthew Dillon { 212e2163f5bSMatthew Dillon atomic_clear_int(&pmp->trans.flags, HAMMER2_TRANS_PREFLUSH); 213e2163f5bSMatthew Dillon } 214e2163f5bSMatthew Dillon 215c603b86bSMatthew Dillon void 216c603b86bSMatthew Dillon hammer2_trans_done(hammer2_pfs_t *pmp) 217c603b86bSMatthew Dillon { 218c603b86bSMatthew Dillon uint32_t oflags; 219c603b86bSMatthew Dillon uint32_t nflags; 220c603b86bSMatthew Dillon 221c603b86bSMatthew Dillon for (;;) { 222c603b86bSMatthew Dillon oflags = pmp->trans.flags; 223c603b86bSMatthew Dillon cpu_ccfence(); 224c603b86bSMatthew Dillon KKASSERT(oflags & HAMMER2_TRANS_MASK); 225c603b86bSMatthew Dillon if ((oflags & HAMMER2_TRANS_MASK) == 1) { 2268138a154SMatthew Dillon /* 227c603b86bSMatthew Dillon * This was the last transaction 2288138a154SMatthew Dillon */ 229c603b86bSMatthew Dillon nflags = (oflags - 1) & ~(HAMMER2_TRANS_ISFLUSH | 230c603b86bSMatthew Dillon HAMMER2_TRANS_BUFCACHE | 231c603b86bSMatthew Dillon HAMMER2_TRANS_PREFLUSH | 232c603b86bSMatthew Dillon HAMMER2_TRANS_FPENDING | 233c603b86bSMatthew Dillon HAMMER2_TRANS_WAITING); 23450456506SMatthew Dillon } else { 23550456506SMatthew Dillon /* 236c603b86bSMatthew Dillon * Still transactions pending 23750456506SMatthew Dillon */ 238c603b86bSMatthew Dillon nflags = oflags - 1; 239c603b86bSMatthew Dillon } 240c603b86bSMatthew Dillon if (atomic_cmpset_int(&pmp->trans.flags, oflags, nflags)) { 241c603b86bSMatthew Dillon if ((nflags & HAMMER2_TRANS_MASK) == 0 && 242c603b86bSMatthew Dillon (oflags & HAMMER2_TRANS_WAITING)) { 243c603b86bSMatthew Dillon wakeup(&pmp->trans.sync_wait); 244c603b86bSMatthew Dillon } 245c603b86bSMatthew Dillon break; 246c603b86bSMatthew Dillon } else { 247c603b86bSMatthew Dillon cpu_pause(); 248c603b86bSMatthew Dillon } 249c603b86bSMatthew Dillon /* retry */ 250044541cdSMatthew Dillon } 25150456506SMatthew Dillon } 25250456506SMatthew Dillon 253c603b86bSMatthew Dillon /* 254c603b86bSMatthew Dillon * Obtain new, unique inode number (not serialized by caller). 255c603b86bSMatthew Dillon */ 256c603b86bSMatthew Dillon hammer2_tid_t 257c603b86bSMatthew Dillon hammer2_trans_newinum(hammer2_pfs_t *pmp) 258c603b86bSMatthew Dillon { 259c603b86bSMatthew Dillon hammer2_tid_t tid; 260c603b86bSMatthew Dillon 261e2163f5bSMatthew Dillon tid = atomic_fetchadd_64(&pmp->inode_tid, 1); 262c603b86bSMatthew Dillon 263c603b86bSMatthew Dillon return tid; 264a7720be7SMatthew Dillon } 265a7720be7SMatthew Dillon 266c603b86bSMatthew Dillon /* 267c603b86bSMatthew Dillon * Assert that a strategy call is ok here. Strategy calls are legal 268c603b86bSMatthew Dillon * 269c603b86bSMatthew Dillon * (1) In a normal transaction. 270c603b86bSMatthew Dillon * (2) In a flush transaction only if PREFLUSH is also set. 271c603b86bSMatthew Dillon */ 2720dea3156SMatthew Dillon void 2739450e866SMatthew Dillon hammer2_trans_assert_strategy(hammer2_pfs_t *pmp) 274c7916d0bSMatthew Dillon { 275c603b86bSMatthew Dillon KKASSERT((pmp->trans.flags & HAMMER2_TRANS_ISFLUSH) == 0 || 276c603b86bSMatthew Dillon (pmp->trans.flags & HAMMER2_TRANS_PREFLUSH)); 277c7916d0bSMatthew Dillon } 278c7916d0bSMatthew Dillon 279a02dfba1SMatthew Dillon 2800dea3156SMatthew Dillon /* 281eedd52a3SMatthew Dillon * Chains undergoing destruction are removed from the in-memory topology. 282eedd52a3SMatthew Dillon * To avoid getting lost these chains are placed on the delayed flush 283eedd52a3SMatthew Dillon * queue which will properly dispose of them. 284eedd52a3SMatthew Dillon * 285eedd52a3SMatthew Dillon * We do this instead of issuing an immediate flush in order to give 286eedd52a3SMatthew Dillon * recursive deletions (rm -rf, etc) a chance to remove more of the 287eedd52a3SMatthew Dillon * hierarchy, potentially allowing an enormous amount of write I/O to 288eedd52a3SMatthew Dillon * be avoided. 289eedd52a3SMatthew Dillon */ 290eedd52a3SMatthew Dillon void 291c603b86bSMatthew Dillon hammer2_delayed_flush(hammer2_chain_t *chain) 292eedd52a3SMatthew Dillon { 293eedd52a3SMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_DELAYED) == 0) { 294eedd52a3SMatthew Dillon hammer2_spin_ex(&chain->hmp->list_spin); 295eedd52a3SMatthew Dillon if ((chain->flags & (HAMMER2_CHAIN_DELAYED | 296eedd52a3SMatthew Dillon HAMMER2_CHAIN_DEFERRED)) == 0) { 297eedd52a3SMatthew Dillon atomic_set_int(&chain->flags, HAMMER2_CHAIN_DELAYED | 298eedd52a3SMatthew Dillon HAMMER2_CHAIN_DEFERRED); 299eedd52a3SMatthew Dillon TAILQ_INSERT_TAIL(&chain->hmp->flushq, 300eedd52a3SMatthew Dillon chain, flush_node); 301eedd52a3SMatthew Dillon hammer2_chain_ref(chain); 302eedd52a3SMatthew Dillon } 303eedd52a3SMatthew Dillon hammer2_spin_unex(&chain->hmp->list_spin); 304f9f4459eSMatthew Dillon hammer2_voldata_modify(chain->hmp); 305eedd52a3SMatthew Dillon } 306eedd52a3SMatthew Dillon } 307eedd52a3SMatthew Dillon 308eedd52a3SMatthew Dillon /* 3090dea3156SMatthew Dillon * Flush the chain and all modified sub-chains through the specified 31053f84d31SMatthew Dillon * synchronization point, propagating blockref updates back up. As 31153f84d31SMatthew Dillon * part of this propagation, mirror_tid and inode/data usage statistics 31253f84d31SMatthew Dillon * propagates back upward. 3130dea3156SMatthew Dillon * 31453f84d31SMatthew Dillon * modify_tid (clc - cluster level change) is not propagated. 31553f84d31SMatthew Dillon * 31653f84d31SMatthew Dillon * update_tid (clc) is used for validation and is not propagated by this 31753f84d31SMatthew Dillon * function. 3180dea3156SMatthew Dillon * 31932b800e6SMatthew Dillon * This routine can be called from several places but the most important 32053f84d31SMatthew Dillon * is from VFS_SYNC (frontend) via hammer2_inode_xop_flush (backend). 32132b800e6SMatthew Dillon * 322da6f36f4SMatthew Dillon * chain is locked on call and will remain locked on return. The chain's 323da6f36f4SMatthew Dillon * UPDATE flag indicates that its parent's block table (which is not yet 324da6f36f4SMatthew Dillon * part of the flush) should be updated. The chain may be replaced by 325da6f36f4SMatthew Dillon * the call if it was modified. 32632b800e6SMatthew Dillon */ 32732b800e6SMatthew Dillon void 32853f84d31SMatthew Dillon hammer2_flush(hammer2_chain_t *chain, int flags) 32932b800e6SMatthew Dillon { 33032b800e6SMatthew Dillon hammer2_chain_t *scan; 33132b800e6SMatthew Dillon hammer2_flush_info_t info; 332eedd52a3SMatthew Dillon hammer2_dev_t *hmp; 333925e4ad1SMatthew Dillon int loops; 33432b800e6SMatthew Dillon 33532b800e6SMatthew Dillon /* 33632b800e6SMatthew Dillon * Execute the recursive flush and handle deferrals. 33732b800e6SMatthew Dillon * 33832b800e6SMatthew Dillon * Chains can be ridiculously long (thousands deep), so to 33932b800e6SMatthew Dillon * avoid blowing out the kernel stack the recursive flush has a 34032b800e6SMatthew Dillon * depth limit. Elements at the limit are placed on a list 34132b800e6SMatthew Dillon * for re-execution after the stack has been popped. 34232b800e6SMatthew Dillon */ 34332b800e6SMatthew Dillon bzero(&info, sizeof(info)); 344da6f36f4SMatthew Dillon TAILQ_INIT(&info.flushq); 3451897c66eSMatthew Dillon info.cache_index = -1; 34653f84d31SMatthew Dillon info.flags = flags & ~HAMMER2_FLUSH_TOP; 34732b800e6SMatthew Dillon 348da6f36f4SMatthew Dillon /* 349da6f36f4SMatthew Dillon * Calculate parent (can be NULL), if not NULL the flush core 350da6f36f4SMatthew Dillon * expects the parent to be referenced so it can easily lock/unlock 351da6f36f4SMatthew Dillon * it without it getting ripped up. 352da6f36f4SMatthew Dillon */ 353da6f36f4SMatthew Dillon if ((info.parent = chain->parent) != NULL) 354da6f36f4SMatthew Dillon hammer2_chain_ref(info.parent); 355731b2a84SMatthew Dillon 356a7720be7SMatthew Dillon /* 357a7720be7SMatthew Dillon * Extra ref needed because flush_core expects it when replacing 358a7720be7SMatthew Dillon * chain. 359a7720be7SMatthew Dillon */ 360a7720be7SMatthew Dillon hammer2_chain_ref(chain); 361eedd52a3SMatthew Dillon hmp = chain->hmp; 362925e4ad1SMatthew Dillon loops = 0; 363a7720be7SMatthew Dillon 3640dea3156SMatthew Dillon for (;;) { 36532b800e6SMatthew Dillon /* 366eedd52a3SMatthew Dillon * Move hmp->flushq to info.flushq if non-empty so it can 367eedd52a3SMatthew Dillon * be processed. 368eedd52a3SMatthew Dillon */ 369eedd52a3SMatthew Dillon if (TAILQ_FIRST(&hmp->flushq) != NULL) { 370eedd52a3SMatthew Dillon hammer2_spin_ex(&chain->hmp->list_spin); 371eedd52a3SMatthew Dillon TAILQ_CONCAT(&info.flushq, &hmp->flushq, flush_node); 372eedd52a3SMatthew Dillon hammer2_spin_unex(&chain->hmp->list_spin); 373eedd52a3SMatthew Dillon } 374eedd52a3SMatthew Dillon 375eedd52a3SMatthew Dillon /* 3760dea3156SMatthew Dillon * Unwind deep recursions which had been deferred. This 3778138a154SMatthew Dillon * can leave the FLUSH_* bits set for these chains, which 3788138a154SMatthew Dillon * will be handled when we [re]flush chain after the unwind. 37932b800e6SMatthew Dillon */ 380da6f36f4SMatthew Dillon while ((scan = TAILQ_FIRST(&info.flushq)) != NULL) { 38132b800e6SMatthew Dillon KKASSERT(scan->flags & HAMMER2_CHAIN_DEFERRED); 382da6f36f4SMatthew Dillon TAILQ_REMOVE(&info.flushq, scan, flush_node); 383eedd52a3SMatthew Dillon atomic_clear_int(&scan->flags, HAMMER2_CHAIN_DEFERRED | 384eedd52a3SMatthew Dillon HAMMER2_CHAIN_DELAYED); 38532b800e6SMatthew Dillon 38632b800e6SMatthew Dillon /* 38732b800e6SMatthew Dillon * Now that we've popped back up we can do a secondary 38832b800e6SMatthew Dillon * recursion on the deferred elements. 389053e752cSMatthew Dillon * 3908138a154SMatthew Dillon * NOTE: hammer2_flush() may replace scan. 39132b800e6SMatthew Dillon */ 39232b800e6SMatthew Dillon if (hammer2_debug & 0x0040) 393053e752cSMatthew Dillon kprintf("deferred flush %p\n", scan); 3940dea3156SMatthew Dillon hammer2_chain_lock(scan, HAMMER2_RESOLVE_MAYBE); 39553f84d31SMatthew Dillon hammer2_flush(scan, flags & ~HAMMER2_FLUSH_TOP); 3960dea3156SMatthew Dillon hammer2_chain_unlock(scan); 397e513e77eSMatthew Dillon hammer2_chain_drop(scan); /* ref from deferral */ 39832b800e6SMatthew Dillon } 39932b800e6SMatthew Dillon 40032b800e6SMatthew Dillon /* 401925e4ad1SMatthew Dillon * [re]flush chain. 40232b800e6SMatthew Dillon */ 4030dea3156SMatthew Dillon info.diddeferral = 0; 40453f84d31SMatthew Dillon hammer2_flush_core(&info, chain, flags); 40532b800e6SMatthew Dillon 40632b800e6SMatthew Dillon /* 4070dea3156SMatthew Dillon * Only loop if deep recursions have been deferred. 40832b800e6SMatthew Dillon */ 409da6f36f4SMatthew Dillon if (TAILQ_EMPTY(&info.flushq)) 41032b800e6SMatthew Dillon break; 411925e4ad1SMatthew Dillon 412925e4ad1SMatthew Dillon if (++loops % 1000 == 0) { 4138138a154SMatthew Dillon kprintf("hammer2_flush: excessive loops on %p\n", 414925e4ad1SMatthew Dillon chain); 415925e4ad1SMatthew Dillon if (hammer2_debug & 0x100000) 416925e4ad1SMatthew Dillon Debugger("hell4"); 417925e4ad1SMatthew Dillon } 41832b800e6SMatthew Dillon } 419a7720be7SMatthew Dillon hammer2_chain_drop(chain); 420da6f36f4SMatthew Dillon if (info.parent) 421da6f36f4SMatthew Dillon hammer2_chain_drop(info.parent); 42232b800e6SMatthew Dillon } 42332b800e6SMatthew Dillon 424476d2aadSMatthew Dillon /* 425ea155208SMatthew Dillon * This is the core of the chain flushing code. The chain is locked by the 426a7720be7SMatthew Dillon * caller and must also have an extra ref on it by the caller, and remains 4278138a154SMatthew Dillon * locked and will have an extra ref on return. Upon return, the caller can 428da6f36f4SMatthew Dillon * test the UPDATE bit on the child to determine if the parent needs updating. 429a7720be7SMatthew Dillon * 4308138a154SMatthew Dillon * (1) Determine if this node is a candidate for the flush, return if it is 4318138a154SMatthew Dillon * not. fchain and vchain are always candidates for the flush. 4320dea3156SMatthew Dillon * 4338138a154SMatthew Dillon * (2) If we recurse too deep the chain is entered onto the deferral list and 4348138a154SMatthew Dillon * the current flush stack is aborted until after the deferral list is 4358138a154SMatthew Dillon * run. 4368138a154SMatthew Dillon * 4378138a154SMatthew Dillon * (3) Recursively flush live children (rbtree). This can create deferrals. 438da6f36f4SMatthew Dillon * A successful flush clears the MODIFIED and UPDATE bits on the children 439da6f36f4SMatthew Dillon * and typically causes the parent to be marked MODIFIED as the children 440da6f36f4SMatthew Dillon * update the parent's block table. A parent might already be marked 441da6f36f4SMatthew Dillon * MODIFIED due to a deletion (whos blocktable update in the parent is 442da6f36f4SMatthew Dillon * handled by the frontend), or if the parent itself is modified by the 443da6f36f4SMatthew Dillon * frontend for other reasons. 4448138a154SMatthew Dillon * 445da6f36f4SMatthew Dillon * (4) Permanently disconnected sub-trees are cleaned up by the front-end. 446da6f36f4SMatthew Dillon * Deleted-but-open inodes can still be individually flushed via the 447da6f36f4SMatthew Dillon * filesystem syncer. 4488138a154SMatthew Dillon * 449da6f36f4SMatthew Dillon * (5) Note that an unmodified child may still need the block table in its 450da6f36f4SMatthew Dillon * parent updated (e.g. rename/move). The child will have UPDATE set 451da6f36f4SMatthew Dillon * in this case. 4528138a154SMatthew Dillon * 45350456506SMatthew Dillon * WARNING ON BREF MODIFY_TID/MIRROR_TID 454925e4ad1SMatthew Dillon * 455e513e77eSMatthew Dillon * blockref.modify_tid is consistent only within a PFS, and will not be 456e513e77eSMatthew Dillon * consistent during synchronization. mirror_tid is consistent across the 457e513e77eSMatthew Dillon * block device regardless of the PFS. 458476d2aadSMatthew Dillon */ 45932b800e6SMatthew Dillon static void 460da6f36f4SMatthew Dillon hammer2_flush_core(hammer2_flush_info_t *info, hammer2_chain_t *chain, 46153f84d31SMatthew Dillon int flags) 46232b800e6SMatthew Dillon { 463da6f36f4SMatthew Dillon hammer2_chain_t *parent; 464506bd6d1SMatthew Dillon hammer2_dev_t *hmp; 465925e4ad1SMatthew Dillon int diddeferral; 466da6f36f4SMatthew Dillon 467da6f36f4SMatthew Dillon /* 468da6f36f4SMatthew Dillon * (1) Optimize downward recursion to locate nodes needing action. 469da6f36f4SMatthew Dillon * Nothing to do if none of these flags are set. 470da6f36f4SMatthew Dillon */ 471850687d2SMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_FLUSH_MASK) == 0) { 472850687d2SMatthew Dillon if (hammer2_debug & 0x200) { 473850687d2SMatthew Dillon if (info->debug == NULL) 474850687d2SMatthew Dillon info->debug = chain; 475850687d2SMatthew Dillon } else { 476da6f36f4SMatthew Dillon return; 477850687d2SMatthew Dillon } 478850687d2SMatthew Dillon } 47932b800e6SMatthew Dillon 480a5913bdfSMatthew Dillon hmp = chain->hmp; 481925e4ad1SMatthew Dillon diddeferral = info->diddeferral; 482da6f36f4SMatthew Dillon parent = info->parent; /* can be NULL */ 483925e4ad1SMatthew Dillon 4840924b3f8SMatthew Dillon /* 485da6f36f4SMatthew Dillon * Downward search recursion 486ea155208SMatthew Dillon */ 487eedd52a3SMatthew Dillon if (chain->flags & (HAMMER2_CHAIN_DEFERRED | HAMMER2_CHAIN_DELAYED)) { 488da6f36f4SMatthew Dillon /* 489da6f36f4SMatthew Dillon * Already deferred. 490da6f36f4SMatthew Dillon */ 491925e4ad1SMatthew Dillon ++info->diddeferral; 49253f84d31SMatthew Dillon } else if ((chain->flags & HAMMER2_CHAIN_PFSBOUNDARY) && 49353f84d31SMatthew Dillon (flags & HAMMER2_FLUSH_ALL) == 0 && 49453f84d31SMatthew Dillon (flags & HAMMER2_FLUSH_TOP) == 0) { 4959450e866SMatthew Dillon /* 4969450e866SMatthew Dillon * We do not recurse through PFSROOTs. PFSROOT flushes are 4979450e866SMatthew Dillon * handled by the related pmp's (whether mounted or not, 4989450e866SMatthew Dillon * including during recovery). 4999450e866SMatthew Dillon * 5009450e866SMatthew Dillon * But we must still process the PFSROOT chains for block 5019450e866SMatthew Dillon * table updates in their parent (which IS part of our flush). 5029450e866SMatthew Dillon * 5039450e866SMatthew Dillon * Note that the volume root, vchain, does not set this flag. 50453f84d31SMatthew Dillon * Note the logic here requires that this test be done before 50553f84d31SMatthew Dillon * the depth-limit test, else it might become the top on a 50653f84d31SMatthew Dillon * flushq iteration. 5079450e866SMatthew Dillon */ 5089450e866SMatthew Dillon ; 50953f84d31SMatthew Dillon } else if (info->depth == HAMMER2_FLUSH_DEPTH_LIMIT) { 51053f84d31SMatthew Dillon /* 51153f84d31SMatthew Dillon * Recursion depth reached. 51253f84d31SMatthew Dillon */ 51353f84d31SMatthew Dillon KKASSERT((chain->flags & HAMMER2_CHAIN_DELAYED) == 0); 51453f84d31SMatthew Dillon hammer2_chain_ref(chain); 51553f84d31SMatthew Dillon TAILQ_INSERT_TAIL(&info->flushq, chain, flush_node); 51653f84d31SMatthew Dillon atomic_set_int(&chain->flags, HAMMER2_CHAIN_DEFERRED); 51753f84d31SMatthew Dillon ++info->diddeferral; 518da6f36f4SMatthew Dillon } else if (chain->flags & HAMMER2_CHAIN_ONFLUSH) { 5198138a154SMatthew Dillon /* 520da6f36f4SMatthew Dillon * Downward recursion search (actual flush occurs bottom-up). 521da6f36f4SMatthew Dillon * pre-clear ONFLUSH. It can get set again due to races, 522da6f36f4SMatthew Dillon * which we want so the scan finds us again in the next flush. 5238138a154SMatthew Dillon */ 524da6f36f4SMatthew Dillon atomic_clear_int(&chain->flags, HAMMER2_CHAIN_ONFLUSH); 5258138a154SMatthew Dillon info->parent = chain; 52694491fa0SMatthew Dillon hammer2_spin_ex(&chain->core.spin); 527da6f36f4SMatthew Dillon RB_SCAN(hammer2_chain_tree, &chain->core.rbtree, 528da6f36f4SMatthew Dillon NULL, hammer2_flush_recurse, info); 52994491fa0SMatthew Dillon hammer2_spin_unex(&chain->core.spin); 530da6f36f4SMatthew Dillon info->parent = parent; 531da6f36f4SMatthew Dillon if (info->diddeferral) 532c603b86bSMatthew Dillon hammer2_chain_setflush(chain); 5338138a154SMatthew Dillon } 5340924b3f8SMatthew Dillon 53532b800e6SMatthew Dillon /* 536da6f36f4SMatthew Dillon * Now we are in the bottom-up part of the recursion. 537da6f36f4SMatthew Dillon * 538da6f36f4SMatthew Dillon * Do not update chain if lower layers were deferred. 5398138a154SMatthew Dillon */ 540da6f36f4SMatthew Dillon if (info->diddeferral) 5418138a154SMatthew Dillon goto done; 5428138a154SMatthew Dillon 5438138a154SMatthew Dillon /* 544da6f36f4SMatthew Dillon * Propagate the DESTROY flag downwards. This dummies up the flush 545da6f36f4SMatthew Dillon * code and tries to invalidate related buffer cache buffers to 546da6f36f4SMatthew Dillon * avoid the disk write. 547623d43d4SMatthew Dillon */ 548da6f36f4SMatthew Dillon if (parent && (parent->flags & HAMMER2_CHAIN_DESTROY)) 549da6f36f4SMatthew Dillon atomic_set_int(&chain->flags, HAMMER2_CHAIN_DESTROY); 550623d43d4SMatthew Dillon 551623d43d4SMatthew Dillon /* 552da6f36f4SMatthew Dillon * Chain was already modified or has become modified, flush it out. 553da6f36f4SMatthew Dillon */ 554da6f36f4SMatthew Dillon again: 555850687d2SMatthew Dillon if ((hammer2_debug & 0x200) && 556850687d2SMatthew Dillon info->debug && 557850687d2SMatthew Dillon (chain->flags & (HAMMER2_CHAIN_MODIFIED | HAMMER2_CHAIN_UPDATE))) { 558850687d2SMatthew Dillon hammer2_chain_t *scan = chain; 559850687d2SMatthew Dillon 560850687d2SMatthew Dillon kprintf("DISCONNECTED FLUSH %p->%p\n", info->debug, chain); 561850687d2SMatthew Dillon while (scan) { 562850687d2SMatthew Dillon kprintf(" chain %p [%08x] bref=%016jx:%02x\n", 563850687d2SMatthew Dillon scan, scan->flags, 564850687d2SMatthew Dillon scan->bref.key, scan->bref.type); 565850687d2SMatthew Dillon if (scan == info->debug) 566850687d2SMatthew Dillon break; 567850687d2SMatthew Dillon scan = scan->parent; 568850687d2SMatthew Dillon } 569850687d2SMatthew Dillon } 570850687d2SMatthew Dillon 571da6f36f4SMatthew Dillon if (chain->flags & HAMMER2_CHAIN_MODIFIED) { 572da6f36f4SMatthew Dillon /* 573e513e77eSMatthew Dillon * Dispose of the modified bit. 574e513e77eSMatthew Dillon * 5753f4ec3cfSMatthew Dillon * If parent is present, the UPDATE bit should already be set. 576e513e77eSMatthew Dillon * UPDATE should already be set. 577e513e77eSMatthew Dillon * bref.mirror_tid should already be set. 57832b800e6SMatthew Dillon */ 579da6f36f4SMatthew Dillon KKASSERT((chain->flags & HAMMER2_CHAIN_UPDATE) || 5803f4ec3cfSMatthew Dillon chain->parent == NULL); 5813f4ec3cfSMatthew Dillon if (hammer2_debug & 0x800000) { 5823f4ec3cfSMatthew Dillon hammer2_chain_t *pp; 5833f4ec3cfSMatthew Dillon 5843f4ec3cfSMatthew Dillon for (pp = chain; pp->parent; pp = pp->parent) 5853f4ec3cfSMatthew Dillon ; 5863f4ec3cfSMatthew Dillon kprintf("FLUSH CHAIN %p (p=%p pp=%p/%d) TYPE %d FLAGS %08x (%s)\n", 5873f4ec3cfSMatthew Dillon chain, chain->parent, pp, pp->bref.type, 5883f4ec3cfSMatthew Dillon chain->bref.type, chain->flags, 5893f4ec3cfSMatthew Dillon (chain->bref.type == 1 ? (const char *)chain->data->ipdata.filename : "?") 5903f4ec3cfSMatthew Dillon 5913f4ec3cfSMatthew Dillon ); 5923f4ec3cfSMatthew Dillon print_backtrace(10); 5933f4ec3cfSMatthew Dillon } 5940dea3156SMatthew Dillon atomic_clear_int(&chain->flags, HAMMER2_CHAIN_MODIFIED); 595f9f4459eSMatthew Dillon atomic_add_long(&hammer2_count_modified_chains, -1); 5968db69c9fSMatthew Dillon 5978db69c9fSMatthew Dillon /* 598e513e77eSMatthew Dillon * Manage threads waiting for excessive dirty memory to 599e513e77eSMatthew Dillon * be retired. 6008db69c9fSMatthew Dillon */ 601e513e77eSMatthew Dillon if (chain->pmp) 602e513e77eSMatthew Dillon hammer2_pfs_memory_wakeup(chain->pmp); 6038138a154SMatthew Dillon 6043f4ec3cfSMatthew Dillon #if 0 6053f4ec3cfSMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_UPDATE) == 0 && 6063f4ec3cfSMatthew Dillon chain != &hmp->vchain && 6073f4ec3cfSMatthew Dillon chain != &hmp->fchain) { 6088138a154SMatthew Dillon /* 6093f4ec3cfSMatthew Dillon * Set UPDATE bit indicating that the parent block 6103f4ec3cfSMatthew Dillon * table requires updating. 6118138a154SMatthew Dillon */ 612da6f36f4SMatthew Dillon atomic_set_int(&chain->flags, HAMMER2_CHAIN_UPDATE); 6130dea3156SMatthew Dillon } 6143f4ec3cfSMatthew Dillon #endif 6150dea3156SMatthew Dillon 6160dea3156SMatthew Dillon /* 617a71db85dSMatthew Dillon * Issue the flush. This is indirect via the DIO. 6180dea3156SMatthew Dillon * 619a71db85dSMatthew Dillon * NOTE: A DELETED node that reaches this point must be 620a71db85dSMatthew Dillon * flushed for synchronization point consistency. 621a71db85dSMatthew Dillon * 622a71db85dSMatthew Dillon * NOTE: Even though MODIFIED was already set, the related DIO 623a71db85dSMatthew Dillon * might not be dirty due to a system buffer cache 624a71db85dSMatthew Dillon * flush and must be set dirty if we are going to make 625a71db85dSMatthew Dillon * further modifications to the buffer. Chains with 626a71db85dSMatthew Dillon * embedded data don't need this. 6270dea3156SMatthew Dillon */ 628a7720be7SMatthew Dillon if (hammer2_debug & 0x1000) { 629c603b86bSMatthew Dillon kprintf("Flush %p.%d %016jx/%d data=%016jx", 630a7720be7SMatthew Dillon chain, chain->bref.type, 631c603b86bSMatthew Dillon (uintmax_t)chain->bref.key, 632c603b86bSMatthew Dillon chain->bref.keybits, 633c603b86bSMatthew Dillon (uintmax_t)chain->bref.data_off); 634a7720be7SMatthew Dillon } 635a7720be7SMatthew Dillon if (hammer2_debug & 0x2000) { 636a7720be7SMatthew Dillon Debugger("Flush hell"); 637a7720be7SMatthew Dillon } 63810136ab6SMatthew Dillon 63932b800e6SMatthew Dillon /* 640da6f36f4SMatthew Dillon * Update chain CRCs for flush. 64132b800e6SMatthew Dillon * 642da6f36f4SMatthew Dillon * NOTE: Volume headers are NOT flushed here as they require 643da6f36f4SMatthew Dillon * special processing. 64432b800e6SMatthew Dillon */ 64532b800e6SMatthew Dillon switch(chain->bref.type) { 6461a7cfe5aSMatthew Dillon case HAMMER2_BREF_TYPE_FREEMAP: 647a71db85dSMatthew Dillon /* 648e513e77eSMatthew Dillon * Update the volume header's freemap_tid to the 649e513e77eSMatthew Dillon * freemap's flushing mirror_tid. 650e513e77eSMatthew Dillon * 651a71db85dSMatthew Dillon * (note: embedded data, do not call setdirty) 652a71db85dSMatthew Dillon */ 65350456506SMatthew Dillon KKASSERT(hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED); 654e513e77eSMatthew Dillon KKASSERT(chain == &hmp->fchain); 655e513e77eSMatthew Dillon hmp->voldata.freemap_tid = chain->bref.mirror_tid; 6565d37f96dSMatthew Dillon if (hammer2_debug & 0x8000) { 6575d37f96dSMatthew Dillon /* debug only, avoid syslogd loop */ 658e513e77eSMatthew Dillon kprintf("sync freemap mirror_tid %08jx\n", 659e513e77eSMatthew Dillon (intmax_t)chain->bref.mirror_tid); 6605d37f96dSMatthew Dillon } 661e513e77eSMatthew Dillon 662e513e77eSMatthew Dillon /* 663e513e77eSMatthew Dillon * The freemap can be flushed independently of the 664e513e77eSMatthew Dillon * main topology, but for the case where it is 665e513e77eSMatthew Dillon * flushed in the same transaction, and flushed 666e513e77eSMatthew Dillon * before vchain (a case we want to allow for 667e513e77eSMatthew Dillon * performance reasons), make sure modifications 668e513e77eSMatthew Dillon * made during the flush under vchain use a new 669e513e77eSMatthew Dillon * transaction id. 670e513e77eSMatthew Dillon * 671e513e77eSMatthew Dillon * Otherwise the mount recovery code will get confused. 672e513e77eSMatthew Dillon */ 673e513e77eSMatthew Dillon ++hmp->voldata.mirror_tid; 6741a7cfe5aSMatthew Dillon break; 67532b800e6SMatthew Dillon case HAMMER2_BREF_TYPE_VOLUME: 67632b800e6SMatthew Dillon /* 677e513e77eSMatthew Dillon * The free block table is flushed by 678e513e77eSMatthew Dillon * hammer2_vfs_sync() before it flushes vchain. 679e513e77eSMatthew Dillon * We must still hold fchain locked while copying 680e513e77eSMatthew Dillon * voldata to volsync, however. 681a71db85dSMatthew Dillon * 682a71db85dSMatthew Dillon * (note: embedded data, do not call setdirty) 6831a7cfe5aSMatthew Dillon */ 684da6f36f4SMatthew Dillon hammer2_chain_lock(&hmp->fchain, 685da6f36f4SMatthew Dillon HAMMER2_RESOLVE_ALWAYS); 686a6cf1052SMatthew Dillon hammer2_voldata_lock(hmp); 6875d37f96dSMatthew Dillon if (hammer2_debug & 0x8000) { 6885d37f96dSMatthew Dillon /* debug only, avoid syslogd loop */ 689e513e77eSMatthew Dillon kprintf("sync volume mirror_tid %08jx\n", 690da6f36f4SMatthew Dillon (intmax_t)chain->bref.mirror_tid); 6915d37f96dSMatthew Dillon } 6921a7cfe5aSMatthew Dillon 6931a7cfe5aSMatthew Dillon /* 694e513e77eSMatthew Dillon * Update the volume header's mirror_tid to the 695e513e77eSMatthew Dillon * main topology's flushing mirror_tid. It is 696e513e77eSMatthew Dillon * possible that voldata.mirror_tid is already 697e513e77eSMatthew Dillon * beyond bref.mirror_tid due to the bump we made 698e513e77eSMatthew Dillon * above in BREF_TYPE_FREEMAP. 699e513e77eSMatthew Dillon */ 700e513e77eSMatthew Dillon if (hmp->voldata.mirror_tid < chain->bref.mirror_tid) { 701e513e77eSMatthew Dillon hmp->voldata.mirror_tid = 702e513e77eSMatthew Dillon chain->bref.mirror_tid; 703e513e77eSMatthew Dillon } 704e513e77eSMatthew Dillon 705e513e77eSMatthew Dillon /* 706da6f36f4SMatthew Dillon * The volume header is flushed manually by the 707da6f36f4SMatthew Dillon * syncer, not here. All we do here is adjust the 708da6f36f4SMatthew Dillon * crc's. 70932b800e6SMatthew Dillon */ 71032b800e6SMatthew Dillon KKASSERT(chain->data != NULL); 711fdf62707SMatthew Dillon KKASSERT(chain->dio == NULL); 71232b800e6SMatthew Dillon 71332b800e6SMatthew Dillon hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT1]= 71432b800e6SMatthew Dillon hammer2_icrc32( 71532b800e6SMatthew Dillon (char *)&hmp->voldata + 71632b800e6SMatthew Dillon HAMMER2_VOLUME_ICRC1_OFF, 71732b800e6SMatthew Dillon HAMMER2_VOLUME_ICRC1_SIZE); 71832b800e6SMatthew Dillon hmp->voldata.icrc_sects[HAMMER2_VOL_ICRC_SECT0]= 71932b800e6SMatthew Dillon hammer2_icrc32( 72032b800e6SMatthew Dillon (char *)&hmp->voldata + 72132b800e6SMatthew Dillon HAMMER2_VOLUME_ICRC0_OFF, 72232b800e6SMatthew Dillon HAMMER2_VOLUME_ICRC0_SIZE); 72332b800e6SMatthew Dillon hmp->voldata.icrc_volheader = 72432b800e6SMatthew Dillon hammer2_icrc32( 72532b800e6SMatthew Dillon (char *)&hmp->voldata + 72632b800e6SMatthew Dillon HAMMER2_VOLUME_ICRCVH_OFF, 72732b800e6SMatthew Dillon HAMMER2_VOLUME_ICRCVH_SIZE); 728e513e77eSMatthew Dillon 7295d37f96dSMatthew Dillon if (hammer2_debug & 0x8000) { 7305d37f96dSMatthew Dillon /* debug only, avoid syslogd loop */ 731e513e77eSMatthew Dillon kprintf("syncvolhdr %016jx %016jx\n", 732e513e77eSMatthew Dillon hmp->voldata.mirror_tid, 733e513e77eSMatthew Dillon hmp->vchain.bref.mirror_tid); 7345d37f96dSMatthew Dillon } 73532b800e6SMatthew Dillon hmp->volsync = hmp->voldata; 7360dea3156SMatthew Dillon atomic_set_int(&chain->flags, HAMMER2_CHAIN_VOLUMESYNC); 73750456506SMatthew Dillon hammer2_voldata_unlock(hmp); 738a6cf1052SMatthew Dillon hammer2_chain_unlock(&hmp->fchain); 73932b800e6SMatthew Dillon break; 74032b800e6SMatthew Dillon case HAMMER2_BREF_TYPE_DATA: 74132b800e6SMatthew Dillon /* 742da6f36f4SMatthew Dillon * Data elements have already been flushed via the 743da6f36f4SMatthew Dillon * logical file buffer cache. Their hash was set in 744a71db85dSMatthew Dillon * the bref by the vop_write code. Do not re-dirty. 74532b800e6SMatthew Dillon * 746da6f36f4SMatthew Dillon * Make sure any device buffer(s) have been flushed 747da6f36f4SMatthew Dillon * out here (there aren't usually any to flush) XXX. 74832b800e6SMatthew Dillon */ 74932b800e6SMatthew Dillon break; 750512beabdSMatthew Dillon case HAMMER2_BREF_TYPE_INDIRECT: 7511a7cfe5aSMatthew Dillon case HAMMER2_BREF_TYPE_FREEMAP_NODE: 75291caa51cSMatthew Dillon case HAMMER2_BREF_TYPE_FREEMAP_LEAF: 753da6f36f4SMatthew Dillon /* 754da6f36f4SMatthew Dillon * Buffer I/O will be cleaned up when the volume is 755da6f36f4SMatthew Dillon * flushed (but the kernel is free to flush it before 756da6f36f4SMatthew Dillon * then, as well). 757da6f36f4SMatthew Dillon */ 75850456506SMatthew Dillon KKASSERT((chain->flags & HAMMER2_CHAIN_EMBEDDED) == 0); 759a71db85dSMatthew Dillon hammer2_chain_setcheck(chain, chain->data); 76050456506SMatthew Dillon break; 76191caa51cSMatthew Dillon case HAMMER2_BREF_TYPE_INODE: 762a71db85dSMatthew Dillon /* 763a71db85dSMatthew Dillon * NOTE: We must call io_setdirty() to make any late 764a71db85dSMatthew Dillon * changes to the inode data, the system might 765a71db85dSMatthew Dillon * have already flushed the buffer. 766a71db85dSMatthew Dillon */ 767b0f58de8SMatthew Dillon if (chain->data->ipdata.meta.op_flags & 768da6f36f4SMatthew Dillon HAMMER2_OPFLAG_PFSROOT) { 769837bd39bSMatthew Dillon /* 770da6f36f4SMatthew Dillon * non-NULL pmp if mounted as a PFS. We must 77118e8ab5fSMatthew Dillon * sync fields cached in the pmp? XXX 772837bd39bSMatthew Dillon */ 773837bd39bSMatthew Dillon hammer2_inode_data_t *ipdata; 774837bd39bSMatthew Dillon 775a71db85dSMatthew Dillon hammer2_io_setdirty(chain->dio); 776837bd39bSMatthew Dillon ipdata = &chain->data->ipdata; 777e513e77eSMatthew Dillon if (chain->pmp) { 778b0f58de8SMatthew Dillon ipdata->meta.pfs_inum = 779e513e77eSMatthew Dillon chain->pmp->inode_tid; 780e513e77eSMatthew Dillon } 78150456506SMatthew Dillon } else { 78250456506SMatthew Dillon /* can't be mounted as a PFS */ 78350456506SMatthew Dillon } 784b3659de2SMatthew Dillon 785512beabdSMatthew Dillon KKASSERT((chain->flags & HAMMER2_CHAIN_EMBEDDED) == 0); 786a71db85dSMatthew Dillon hammer2_chain_setcheck(chain, chain->data); 7871a7cfe5aSMatthew Dillon break; 78832b800e6SMatthew Dillon default: 78991caa51cSMatthew Dillon KKASSERT(chain->flags & HAMMER2_CHAIN_EMBEDDED); 790da6f36f4SMatthew Dillon panic("hammer2_flush_core: unsupported " 791da6f36f4SMatthew Dillon "embedded bref %d", 79291caa51cSMatthew Dillon chain->bref.type); 79391caa51cSMatthew Dillon /* NOT REACHED */ 79432b800e6SMatthew Dillon } 79532b800e6SMatthew Dillon 79632b800e6SMatthew Dillon /* 797da6f36f4SMatthew Dillon * If the chain was destroyed try to avoid unnecessary I/O. 798da6f36f4SMatthew Dillon * (this only really works if the DIO system buffer is the 799da6f36f4SMatthew Dillon * same size as chain->bytes). 800da6f36f4SMatthew Dillon */ 801125966e8SMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_DESTROY) && 802125966e8SMatthew Dillon (chain->flags & HAMMER2_CHAIN_DEDUP) == 0 && 803125966e8SMatthew Dillon chain->dio) { 804*7d565a4fSMatthew Dillon hammer2_io_setinval(chain->dio, 805*7d565a4fSMatthew Dillon chain->bref.data_off, 806*7d565a4fSMatthew Dillon chain->bytes); 807da6f36f4SMatthew Dillon } 808da6f36f4SMatthew Dillon } 809da6f36f4SMatthew Dillon 810da6f36f4SMatthew Dillon /* 811da6f36f4SMatthew Dillon * If UPDATE is set the parent block table may need to be updated. 812da6f36f4SMatthew Dillon * 813da6f36f4SMatthew Dillon * NOTE: UPDATE may be set on vchain or fchain in which case 814da6f36f4SMatthew Dillon * parent could be NULL. It's easiest to allow the case 815da6f36f4SMatthew Dillon * and test for NULL. parent can also wind up being NULL 816da6f36f4SMatthew Dillon * due to a deletion so we need to handle the case anyway. 817da6f36f4SMatthew Dillon * 818da6f36f4SMatthew Dillon * If no parent exists we can just clear the UPDATE bit. If the 819da6f36f4SMatthew Dillon * chain gets reattached later on the bit will simply get set 820da6f36f4SMatthew Dillon * again. 821da6f36f4SMatthew Dillon */ 8223f4ec3cfSMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_UPDATE) && parent == NULL) 823da6f36f4SMatthew Dillon atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE); 824da6f36f4SMatthew Dillon 825da6f36f4SMatthew Dillon /* 826da6f36f4SMatthew Dillon * The chain may need its blockrefs updated in the parent. This 827da6f36f4SMatthew Dillon * requires some fancy footwork. 828da6f36f4SMatthew Dillon */ 829da6f36f4SMatthew Dillon if (chain->flags & HAMMER2_CHAIN_UPDATE) { 830da6f36f4SMatthew Dillon hammer2_blockref_t *base; 831da6f36f4SMatthew Dillon int count; 832da6f36f4SMatthew Dillon 833da6f36f4SMatthew Dillon /* 834da6f36f4SMatthew Dillon * Both parent and chain must be locked. This requires 835da6f36f4SMatthew Dillon * temporarily unlocking the chain. We have to deal with 836da6f36f4SMatthew Dillon * the case where the chain might be reparented or modified 837da6f36f4SMatthew Dillon * while it was unlocked. 838da6f36f4SMatthew Dillon */ 839da6f36f4SMatthew Dillon hammer2_chain_unlock(chain); 840da6f36f4SMatthew Dillon hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); 841da6f36f4SMatthew Dillon hammer2_chain_lock(chain, HAMMER2_RESOLVE_MAYBE); 842da6f36f4SMatthew Dillon if (chain->parent != parent) { 843eedd52a3SMatthew Dillon kprintf("PARENT MISMATCH ch=%p p=%p/%p\n", 844eedd52a3SMatthew Dillon chain, chain->parent, parent); 845da6f36f4SMatthew Dillon hammer2_chain_unlock(parent); 846da6f36f4SMatthew Dillon goto done; 847da6f36f4SMatthew Dillon } 848da6f36f4SMatthew Dillon 849da6f36f4SMatthew Dillon /* 850da6f36f4SMatthew Dillon * Check race condition. If someone got in and modified 851da6f36f4SMatthew Dillon * it again while it was unlocked, we have to loop up. 852da6f36f4SMatthew Dillon */ 853da6f36f4SMatthew Dillon if (chain->flags & HAMMER2_CHAIN_MODIFIED) { 854da6f36f4SMatthew Dillon hammer2_chain_unlock(parent); 855da6f36f4SMatthew Dillon kprintf("hammer2_flush: chain %p flush-mod race\n", 856da6f36f4SMatthew Dillon chain); 857da6f36f4SMatthew Dillon goto again; 858da6f36f4SMatthew Dillon } 859da6f36f4SMatthew Dillon 860da6f36f4SMatthew Dillon /* 861a6cf1052SMatthew Dillon * Clear UPDATE flag, mark parent modified, update its 862a6cf1052SMatthew Dillon * modify_tid if necessary, and adjust the parent blockmap. 863da6f36f4SMatthew Dillon */ 8643f4ec3cfSMatthew Dillon if (chain->flags & HAMMER2_CHAIN_UPDATE) 865da6f36f4SMatthew Dillon atomic_clear_int(&chain->flags, HAMMER2_CHAIN_UPDATE); 866a6cf1052SMatthew Dillon 867eedd52a3SMatthew Dillon /* 868eedd52a3SMatthew Dillon * (optional code) 869eedd52a3SMatthew Dillon * 870eedd52a3SMatthew Dillon * Avoid actually modifying and updating the parent if it 871eedd52a3SMatthew Dillon * was flagged for destruction. This can greatly reduce 872eedd52a3SMatthew Dillon * disk I/O in large tree removals because the 873eedd52a3SMatthew Dillon * hammer2_io_setinval() call in the upward recursion 874eedd52a3SMatthew Dillon * (see MODIFIED code above) can only handle a few cases. 875eedd52a3SMatthew Dillon */ 876eedd52a3SMatthew Dillon if (parent->flags & HAMMER2_CHAIN_DESTROY) { 877eedd52a3SMatthew Dillon if (parent->bref.modify_tid < chain->bref.modify_tid) { 878eedd52a3SMatthew Dillon parent->bref.modify_tid = 879eedd52a3SMatthew Dillon chain->bref.modify_tid; 880eedd52a3SMatthew Dillon } 881eedd52a3SMatthew Dillon atomic_clear_int(&chain->flags, HAMMER2_CHAIN_BMAPPED | 882eedd52a3SMatthew Dillon HAMMER2_CHAIN_BMAPUPD); 883eedd52a3SMatthew Dillon hammer2_chain_unlock(parent); 884eedd52a3SMatthew Dillon goto skipupdate; 885eedd52a3SMatthew Dillon } 886eedd52a3SMatthew Dillon 887eedd52a3SMatthew Dillon /* 888eedd52a3SMatthew Dillon * We are updating the parent's blockmap, the parent must 889eedd52a3SMatthew Dillon * be set modified. 890eedd52a3SMatthew Dillon */ 8913f01ebaaSMatthew Dillon hammer2_chain_modify(parent, 0, 0, 0); 892a6cf1052SMatthew Dillon if (parent->bref.modify_tid < chain->bref.modify_tid) 893a6cf1052SMatthew Dillon parent->bref.modify_tid = chain->bref.modify_tid; 894da6f36f4SMatthew Dillon 895da6f36f4SMatthew Dillon /* 896da6f36f4SMatthew Dillon * Calculate blockmap pointer 897da6f36f4SMatthew Dillon */ 898da6f36f4SMatthew Dillon switch(parent->bref.type) { 899da6f36f4SMatthew Dillon case HAMMER2_BREF_TYPE_INODE: 900da6f36f4SMatthew Dillon /* 901da6f36f4SMatthew Dillon * Access the inode's block array. However, there is 902da6f36f4SMatthew Dillon * no block array if the inode is flagged DIRECTDATA. 903da6f36f4SMatthew Dillon */ 904da6f36f4SMatthew Dillon if (parent->data && 905b0f58de8SMatthew Dillon (parent->data->ipdata.meta.op_flags & 906da6f36f4SMatthew Dillon HAMMER2_OPFLAG_DIRECTDATA) == 0) { 907da6f36f4SMatthew Dillon base = &parent->data-> 908da6f36f4SMatthew Dillon ipdata.u.blockset.blockref[0]; 909da6f36f4SMatthew Dillon } else { 910da6f36f4SMatthew Dillon base = NULL; 911da6f36f4SMatthew Dillon } 912da6f36f4SMatthew Dillon count = HAMMER2_SET_COUNT; 913da6f36f4SMatthew Dillon break; 914da6f36f4SMatthew Dillon case HAMMER2_BREF_TYPE_INDIRECT: 915da6f36f4SMatthew Dillon case HAMMER2_BREF_TYPE_FREEMAP_NODE: 916da6f36f4SMatthew Dillon if (parent->data) 917da6f36f4SMatthew Dillon base = &parent->data->npdata[0]; 918da6f36f4SMatthew Dillon else 919da6f36f4SMatthew Dillon base = NULL; 920da6f36f4SMatthew Dillon count = parent->bytes / sizeof(hammer2_blockref_t); 921da6f36f4SMatthew Dillon break; 922da6f36f4SMatthew Dillon case HAMMER2_BREF_TYPE_VOLUME: 923da6f36f4SMatthew Dillon base = &chain->hmp->voldata.sroot_blockset.blockref[0]; 924da6f36f4SMatthew Dillon count = HAMMER2_SET_COUNT; 925da6f36f4SMatthew Dillon break; 926da6f36f4SMatthew Dillon case HAMMER2_BREF_TYPE_FREEMAP: 927da6f36f4SMatthew Dillon base = &parent->data->npdata[0]; 928da6f36f4SMatthew Dillon count = HAMMER2_SET_COUNT; 929da6f36f4SMatthew Dillon break; 930da6f36f4SMatthew Dillon default: 931da6f36f4SMatthew Dillon base = NULL; 932da6f36f4SMatthew Dillon count = 0; 933da6f36f4SMatthew Dillon panic("hammer2_flush_core: " 934da6f36f4SMatthew Dillon "unrecognized blockref type: %d", 935da6f36f4SMatthew Dillon parent->bref.type); 936da6f36f4SMatthew Dillon } 937da6f36f4SMatthew Dillon 938da6f36f4SMatthew Dillon /* 939da6f36f4SMatthew Dillon * Blocktable updates 940b3659de2SMatthew Dillon * 941b3659de2SMatthew Dillon * We synchronize pending statistics at this time. Delta 942b3659de2SMatthew Dillon * adjustments designated for the current and upper level 943b3659de2SMatthew Dillon * are synchronized. 944da6f36f4SMatthew Dillon */ 945da6f36f4SMatthew Dillon if (base && (chain->flags & HAMMER2_CHAIN_BMAPUPD)) { 946da6f36f4SMatthew Dillon if (chain->flags & HAMMER2_CHAIN_BMAPPED) { 9470cc33e20SMatthew Dillon hammer2_spin_ex(&parent->core.spin); 948c603b86bSMatthew Dillon hammer2_base_delete(parent, base, count, 949da6f36f4SMatthew Dillon &info->cache_index, chain); 9500cc33e20SMatthew Dillon hammer2_spin_unex(&parent->core.spin); 951b3659de2SMatthew Dillon /* base_delete clears both bits */ 952b3659de2SMatthew Dillon } else { 953b3659de2SMatthew Dillon atomic_clear_int(&chain->flags, 954b3659de2SMatthew Dillon HAMMER2_CHAIN_BMAPUPD); 955da6f36f4SMatthew Dillon } 956da6f36f4SMatthew Dillon } 957da6f36f4SMatthew Dillon if (base && (chain->flags & HAMMER2_CHAIN_BMAPPED) == 0) { 9580cc33e20SMatthew Dillon hammer2_spin_ex(&parent->core.spin); 959c603b86bSMatthew Dillon hammer2_base_insert(parent, base, count, 960da6f36f4SMatthew Dillon &info->cache_index, chain); 9610cc33e20SMatthew Dillon hammer2_spin_unex(&parent->core.spin); 962b3659de2SMatthew Dillon /* base_insert sets BMAPPED */ 963da6f36f4SMatthew Dillon } 964da6f36f4SMatthew Dillon hammer2_chain_unlock(parent); 965da6f36f4SMatthew Dillon } 966eedd52a3SMatthew Dillon skipupdate: 967eedd52a3SMatthew Dillon ; 968da6f36f4SMatthew Dillon 969da6f36f4SMatthew Dillon /* 9708138a154SMatthew Dillon * Final cleanup after flush 9718138a154SMatthew Dillon */ 9728138a154SMatthew Dillon done: 973e513e77eSMatthew Dillon KKASSERT(chain->refs > 0); 974850687d2SMatthew Dillon if (hammer2_debug & 0x200) { 975850687d2SMatthew Dillon if (info->debug == chain) 976850687d2SMatthew Dillon info->debug = NULL; 977850687d2SMatthew Dillon } 9788138a154SMatthew Dillon } 9798138a154SMatthew Dillon 9808138a154SMatthew Dillon /* 981da6f36f4SMatthew Dillon * Flush recursion helper, called from flush_core, calls flush_core. 9820dea3156SMatthew Dillon * 9838138a154SMatthew Dillon * Flushes the children of the caller's chain (info->parent), restricted 9848138a154SMatthew Dillon * by sync_tid. Set info->domodify if the child's blockref must propagate 9858138a154SMatthew Dillon * back up to the parent. 9860dea3156SMatthew Dillon * 9878138a154SMatthew Dillon * Ripouts can move child from rbtree to dbtree or dbq but the caller's 9888138a154SMatthew Dillon * flush scan order prevents any chains from being lost. A child can be 989da6f36f4SMatthew Dillon * executes more than once. 990ea155208SMatthew Dillon * 9918138a154SMatthew Dillon * WARNING! If we do not call hammer2_flush_core() we must update 9928138a154SMatthew Dillon * bref.mirror_tid ourselves to indicate that the flush has 9938138a154SMatthew Dillon * processed the child. 994925e4ad1SMatthew Dillon * 9958138a154SMatthew Dillon * WARNING! parent->core spinlock is held on entry and return. 99632b800e6SMatthew Dillon */ 9970dea3156SMatthew Dillon static int 998da6f36f4SMatthew Dillon hammer2_flush_recurse(hammer2_chain_t *child, void *data) 99932b800e6SMatthew Dillon { 10000dea3156SMatthew Dillon hammer2_flush_info_t *info = data; 10010dea3156SMatthew Dillon hammer2_chain_t *parent = info->parent; 1002925e4ad1SMatthew Dillon 10030dea3156SMatthew Dillon /* 100410136ab6SMatthew Dillon * (child can never be fchain or vchain so a special check isn't 100510136ab6SMatthew Dillon * needed). 1006da6f36f4SMatthew Dillon * 1007a4dc31e0SMatthew Dillon * We must ref the child before unlocking the spinlock. 1008a4dc31e0SMatthew Dillon * 1009a4dc31e0SMatthew Dillon * The caller has added a ref to the parent so we can temporarily 1010a4dc31e0SMatthew Dillon * unlock it in order to lock the child. 1011a4dc31e0SMatthew Dillon */ 1012ea155208SMatthew Dillon hammer2_chain_ref(child); 101394491fa0SMatthew Dillon hammer2_spin_unex(&parent->core.spin); 10140dea3156SMatthew Dillon 10150dea3156SMatthew Dillon hammer2_chain_unlock(parent); 10160dea3156SMatthew Dillon hammer2_chain_lock(child, HAMMER2_RESOLVE_MAYBE); 10170dea3156SMatthew Dillon 101803faa7d5SMatthew Dillon /* 1019e513e77eSMatthew Dillon * Recurse and collect deferral data. We're in the media flush, 1020e513e77eSMatthew Dillon * this can cross PFS boundaries. 102103faa7d5SMatthew Dillon */ 1022da6f36f4SMatthew Dillon if (child->flags & HAMMER2_CHAIN_FLUSH_MASK) { 10230dea3156SMatthew Dillon ++info->depth; 102453f84d31SMatthew Dillon hammer2_flush_core(info, child, info->flags); 10250dea3156SMatthew Dillon --info->depth; 1026850687d2SMatthew Dillon } else if (hammer2_debug & 0x200) { 1027850687d2SMatthew Dillon if (info->debug == NULL) 1028850687d2SMatthew Dillon info->debug = child; 1029850687d2SMatthew Dillon ++info->depth; 103053f84d31SMatthew Dillon hammer2_flush_core(info, child, info->flags); 1031850687d2SMatthew Dillon --info->depth; 1032850687d2SMatthew Dillon if (info->debug == child) 1033850687d2SMatthew Dillon info->debug = NULL; 10348138a154SMatthew Dillon } 10350dea3156SMatthew Dillon 1036a4dc31e0SMatthew Dillon /* 1037a4dc31e0SMatthew Dillon * Relock to continue the loop 1038a4dc31e0SMatthew Dillon */ 1039a4dc31e0SMatthew Dillon hammer2_chain_unlock(child); 1040ea155208SMatthew Dillon hammer2_chain_lock(parent, HAMMER2_RESOLVE_MAYBE); 1041a4dc31e0SMatthew Dillon hammer2_chain_drop(child); 1042a4dc31e0SMatthew Dillon KKASSERT(info->parent == parent); 104394491fa0SMatthew Dillon hammer2_spin_ex(&parent->core.spin); 10440dea3156SMatthew Dillon 10450dea3156SMatthew Dillon return (0); 10460dea3156SMatthew Dillon } 104712ff971cSMatthew Dillon 104812ff971cSMatthew Dillon /* 104953f84d31SMatthew Dillon * flush helper (direct) 105053f84d31SMatthew Dillon * 105153f84d31SMatthew Dillon * Quickly flushes any dirty chains for a device. This will update our 105253f84d31SMatthew Dillon * concept of the volume root but does NOT flush the actual volume root 105353f84d31SMatthew Dillon * and does not flush dirty device buffers. 105453f84d31SMatthew Dillon * 105553f84d31SMatthew Dillon * This function is primarily used by the bulkfree code to allow it to 105653f84d31SMatthew Dillon * create a snapshot for the pass. It doesn't care about any pending 105753f84d31SMatthew Dillon * work (dirty vnodes, dirty inodes, dirty logical buffers) for which blocks 105853f84d31SMatthew Dillon * have not yet been allocated. 105953f84d31SMatthew Dillon */ 106053f84d31SMatthew Dillon void 106153f84d31SMatthew Dillon hammer2_flush_quick(hammer2_dev_t *hmp) 106253f84d31SMatthew Dillon { 106353f84d31SMatthew Dillon hammer2_chain_t *chain; 106453f84d31SMatthew Dillon 106553f84d31SMatthew Dillon hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH); 106653f84d31SMatthew Dillon 106753f84d31SMatthew Dillon hammer2_chain_ref(&hmp->vchain); 106853f84d31SMatthew Dillon hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 106953f84d31SMatthew Dillon if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 107053f84d31SMatthew Dillon chain = &hmp->vchain; 107153f84d31SMatthew Dillon hammer2_flush(chain, HAMMER2_FLUSH_TOP | 107253f84d31SMatthew Dillon HAMMER2_FLUSH_ALL); 107353f84d31SMatthew Dillon KKASSERT(chain == &hmp->vchain); 107453f84d31SMatthew Dillon } 107553f84d31SMatthew Dillon hammer2_chain_unlock(&hmp->vchain); 107653f84d31SMatthew Dillon hammer2_chain_drop(&hmp->vchain); 107753f84d31SMatthew Dillon 107853f84d31SMatthew Dillon hammer2_trans_done(hmp->spmp); /* spmp trans */ 107953f84d31SMatthew Dillon } 108053f84d31SMatthew Dillon 108153f84d31SMatthew Dillon /* 108212ff971cSMatthew Dillon * flush helper (backend threaded) 108312ff971cSMatthew Dillon * 108412ff971cSMatthew Dillon * Flushes core chains, issues disk sync, flushes volume roots. 108512ff971cSMatthew Dillon * 108612ff971cSMatthew Dillon * Primarily called from vfs_sync(). 108712ff971cSMatthew Dillon */ 108812ff971cSMatthew Dillon void 108912ff971cSMatthew Dillon hammer2_inode_xop_flush(hammer2_xop_t *arg, int clindex) 109012ff971cSMatthew Dillon { 109112ff971cSMatthew Dillon hammer2_xop_flush_t *xop = &arg->xop_flush; 109212ff971cSMatthew Dillon hammer2_chain_t *chain; 109312ff971cSMatthew Dillon hammer2_chain_t *parent; 109412ff971cSMatthew Dillon hammer2_dev_t *hmp; 109512ff971cSMatthew Dillon int error = 0; 109612ff971cSMatthew Dillon int total_error = 0; 109712ff971cSMatthew Dillon int j; 109812ff971cSMatthew Dillon 109912ff971cSMatthew Dillon /* 110012ff971cSMatthew Dillon * Flush core chains 110112ff971cSMatthew Dillon */ 110221a90458SMatthew Dillon chain = hammer2_inode_chain(xop->head.ip1, clindex, 110312ff971cSMatthew Dillon HAMMER2_RESOLVE_ALWAYS); 110412ff971cSMatthew Dillon if (chain) { 110512ff971cSMatthew Dillon hmp = chain->hmp; 11063f4ec3cfSMatthew Dillon if ((chain->flags & HAMMER2_CHAIN_FLUSH_MASK) || 11073f4ec3cfSMatthew Dillon TAILQ_FIRST(&hmp->flushq) != NULL) { 110853f84d31SMatthew Dillon hammer2_flush(chain, HAMMER2_FLUSH_TOP); 110912ff971cSMatthew Dillon parent = chain->parent; 111012ff971cSMatthew Dillon KKASSERT(chain->pmp != parent->pmp); 111112ff971cSMatthew Dillon hammer2_chain_setflush(parent); 111212ff971cSMatthew Dillon } 111312ff971cSMatthew Dillon hammer2_chain_unlock(chain); 111412ff971cSMatthew Dillon hammer2_chain_drop(chain); 111512ff971cSMatthew Dillon chain = NULL; 111612ff971cSMatthew Dillon } else { 111712ff971cSMatthew Dillon hmp = NULL; 111812ff971cSMatthew Dillon } 111912ff971cSMatthew Dillon 112012ff971cSMatthew Dillon /* 112112ff971cSMatthew Dillon * Flush volume roots. Avoid replication, we only want to 112212ff971cSMatthew Dillon * flush each hammer2_dev (hmp) once. 112312ff971cSMatthew Dillon */ 112412ff971cSMatthew Dillon for (j = clindex - 1; j >= 0; --j) { 112521a90458SMatthew Dillon if ((chain = xop->head.ip1->cluster.array[j].chain) != NULL) { 112612ff971cSMatthew Dillon if (chain->hmp == hmp) { 112712ff971cSMatthew Dillon chain = NULL; /* safety */ 112812ff971cSMatthew Dillon goto skip; 112912ff971cSMatthew Dillon } 113012ff971cSMatthew Dillon } 113112ff971cSMatthew Dillon } 113212ff971cSMatthew Dillon chain = NULL; /* safety */ 113312ff971cSMatthew Dillon 113412ff971cSMatthew Dillon /* 113512ff971cSMatthew Dillon * spmp transaction. The super-root is never directly mounted so 113612ff971cSMatthew Dillon * there shouldn't be any vnodes, let alone any dirty vnodes 113753f84d31SMatthew Dillon * associated with it, so we shouldn't have to mess around with any 113853f84d31SMatthew Dillon * vnode flushes here. 113912ff971cSMatthew Dillon */ 114012ff971cSMatthew Dillon hammer2_trans_init(hmp->spmp, HAMMER2_TRANS_ISFLUSH); 114112ff971cSMatthew Dillon 114212ff971cSMatthew Dillon /* 114312ff971cSMatthew Dillon * Media mounts have two 'roots', vchain for the topology 114412ff971cSMatthew Dillon * and fchain for the free block table. Flush both. 114512ff971cSMatthew Dillon * 114612ff971cSMatthew Dillon * Note that the topology and free block table are handled 114712ff971cSMatthew Dillon * independently, so the free block table can wind up being 114812ff971cSMatthew Dillon * ahead of the topology. We depend on the bulk free scan 114912ff971cSMatthew Dillon * code to deal with any loose ends. 115012ff971cSMatthew Dillon */ 115112ff971cSMatthew Dillon hammer2_chain_ref(&hmp->vchain); 115212ff971cSMatthew Dillon hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 115312ff971cSMatthew Dillon hammer2_chain_ref(&hmp->fchain); 115412ff971cSMatthew Dillon hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS); 115512ff971cSMatthew Dillon if (hmp->fchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 115612ff971cSMatthew Dillon /* 115712ff971cSMatthew Dillon * This will also modify vchain as a side effect, 115812ff971cSMatthew Dillon * mark vchain as modified now. 115912ff971cSMatthew Dillon */ 116012ff971cSMatthew Dillon hammer2_voldata_modify(hmp); 116112ff971cSMatthew Dillon chain = &hmp->fchain; 116253f84d31SMatthew Dillon hammer2_flush(chain, HAMMER2_FLUSH_TOP); 116312ff971cSMatthew Dillon KKASSERT(chain == &hmp->fchain); 116412ff971cSMatthew Dillon } 116512ff971cSMatthew Dillon hammer2_chain_unlock(&hmp->fchain); 116612ff971cSMatthew Dillon hammer2_chain_unlock(&hmp->vchain); 116712ff971cSMatthew Dillon hammer2_chain_drop(&hmp->fchain); 116812ff971cSMatthew Dillon /* vchain dropped down below */ 116912ff971cSMatthew Dillon 117012ff971cSMatthew Dillon hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS); 117112ff971cSMatthew Dillon if (hmp->vchain.flags & HAMMER2_CHAIN_FLUSH_MASK) { 117212ff971cSMatthew Dillon chain = &hmp->vchain; 117353f84d31SMatthew Dillon hammer2_flush(chain, HAMMER2_FLUSH_TOP); 117412ff971cSMatthew Dillon KKASSERT(chain == &hmp->vchain); 117512ff971cSMatthew Dillon } 117612ff971cSMatthew Dillon hammer2_chain_unlock(&hmp->vchain); 117712ff971cSMatthew Dillon hammer2_chain_drop(&hmp->vchain); 117812ff971cSMatthew Dillon 117912ff971cSMatthew Dillon error = 0; 118012ff971cSMatthew Dillon 118112ff971cSMatthew Dillon /* 118212ff971cSMatthew Dillon * We can't safely flush the volume header until we have 118312ff971cSMatthew Dillon * flushed any device buffers which have built up. 118412ff971cSMatthew Dillon * 118512ff971cSMatthew Dillon * XXX this isn't being incremental 118612ff971cSMatthew Dillon */ 118712ff971cSMatthew Dillon vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY); 118812ff971cSMatthew Dillon error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0); 118912ff971cSMatthew Dillon vn_unlock(hmp->devvp); 119012ff971cSMatthew Dillon 119112ff971cSMatthew Dillon /* 119212ff971cSMatthew Dillon * The flush code sets CHAIN_VOLUMESYNC to indicate that the 119312ff971cSMatthew Dillon * volume header needs synchronization via hmp->volsync. 119412ff971cSMatthew Dillon * 119512ff971cSMatthew Dillon * XXX synchronize the flag & data with only this flush XXX 119612ff971cSMatthew Dillon */ 119712ff971cSMatthew Dillon if (error == 0 && 119812ff971cSMatthew Dillon (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) { 119912ff971cSMatthew Dillon struct buf *bp; 120012ff971cSMatthew Dillon 120112ff971cSMatthew Dillon /* 120212ff971cSMatthew Dillon * Synchronize the disk before flushing the volume 120312ff971cSMatthew Dillon * header. 120412ff971cSMatthew Dillon */ 120512ff971cSMatthew Dillon bp = getpbuf(NULL); 120612ff971cSMatthew Dillon bp->b_bio1.bio_offset = 0; 120712ff971cSMatthew Dillon bp->b_bufsize = 0; 120812ff971cSMatthew Dillon bp->b_bcount = 0; 120912ff971cSMatthew Dillon bp->b_cmd = BUF_CMD_FLUSH; 121012ff971cSMatthew Dillon bp->b_bio1.bio_done = biodone_sync; 121112ff971cSMatthew Dillon bp->b_bio1.bio_flags |= BIO_SYNC; 121212ff971cSMatthew Dillon vn_strategy(hmp->devvp, &bp->b_bio1); 121312ff971cSMatthew Dillon biowait(&bp->b_bio1, "h2vol"); 121412ff971cSMatthew Dillon relpbuf(bp, NULL); 121512ff971cSMatthew Dillon 121612ff971cSMatthew Dillon /* 121712ff971cSMatthew Dillon * Then we can safely flush the version of the 121812ff971cSMatthew Dillon * volume header synchronized by the flush code. 121912ff971cSMatthew Dillon */ 122012ff971cSMatthew Dillon j = hmp->volhdrno + 1; 122112ff971cSMatthew Dillon if (j >= HAMMER2_NUM_VOLHDRS) 122212ff971cSMatthew Dillon j = 0; 122312ff971cSMatthew Dillon if (j * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE > 122412ff971cSMatthew Dillon hmp->volsync.volu_size) { 122512ff971cSMatthew Dillon j = 0; 122612ff971cSMatthew Dillon } 12275d37f96dSMatthew Dillon if (hammer2_debug & 0x8000) { 12285d37f96dSMatthew Dillon /* debug only, avoid syslogd loop */ 122912ff971cSMatthew Dillon kprintf("sync volhdr %d %jd\n", 123012ff971cSMatthew Dillon j, (intmax_t)hmp->volsync.volu_size); 12315d37f96dSMatthew Dillon } 123212ff971cSMatthew Dillon bp = getblk(hmp->devvp, j * HAMMER2_ZONE_BYTES64, 123312ff971cSMatthew Dillon HAMMER2_PBUFSIZE, 0, 0); 123412ff971cSMatthew Dillon atomic_clear_int(&hmp->vchain.flags, 123512ff971cSMatthew Dillon HAMMER2_CHAIN_VOLUMESYNC); 123612ff971cSMatthew Dillon bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE); 123712ff971cSMatthew Dillon bawrite(bp); 123812ff971cSMatthew Dillon hmp->volhdrno = j; 123912ff971cSMatthew Dillon } 124012ff971cSMatthew Dillon if (error) 124112ff971cSMatthew Dillon total_error = error; 124212ff971cSMatthew Dillon 124312ff971cSMatthew Dillon hammer2_trans_done(hmp->spmp); /* spmp trans */ 124412ff971cSMatthew Dillon skip: 124512ff971cSMatthew Dillon error = hammer2_xop_feed(&xop->head, NULL, clindex, total_error); 124612ff971cSMatthew Dillon } 1247