162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. 462306a36Sopenharmony_ci * Copyright (c) 2010 David Chinner. 562306a36Sopenharmony_ci * Copyright (c) 2011 Christoph Hellwig. 662306a36Sopenharmony_ci * All Rights Reserved. 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci#include "xfs.h" 962306a36Sopenharmony_ci#include "xfs_fs.h" 1062306a36Sopenharmony_ci#include "xfs_format.h" 1162306a36Sopenharmony_ci#include "xfs_log_format.h" 1262306a36Sopenharmony_ci#include "xfs_shared.h" 1362306a36Sopenharmony_ci#include "xfs_trans_resv.h" 1462306a36Sopenharmony_ci#include "xfs_mount.h" 1562306a36Sopenharmony_ci#include "xfs_alloc.h" 1662306a36Sopenharmony_ci#include "xfs_extent_busy.h" 1762306a36Sopenharmony_ci#include "xfs_trace.h" 1862306a36Sopenharmony_ci#include "xfs_trans.h" 1962306a36Sopenharmony_ci#include "xfs_log.h" 2062306a36Sopenharmony_ci#include "xfs_ag.h" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_cistatic void 2362306a36Sopenharmony_cixfs_extent_busy_insert_list( 2462306a36Sopenharmony_ci struct xfs_perag *pag, 2562306a36Sopenharmony_ci xfs_agblock_t bno, 2662306a36Sopenharmony_ci xfs_extlen_t len, 2762306a36Sopenharmony_ci unsigned int flags, 2862306a36Sopenharmony_ci struct list_head *busy_list) 2962306a36Sopenharmony_ci{ 3062306a36Sopenharmony_ci struct xfs_extent_busy *new; 3162306a36Sopenharmony_ci struct xfs_extent_busy *busyp; 3262306a36Sopenharmony_ci struct rb_node **rbp; 3362306a36Sopenharmony_ci struct rb_node *parent = NULL; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci new = kmem_zalloc(sizeof(struct xfs_extent_busy), 0); 3662306a36Sopenharmony_ci new->agno = pag->pag_agno; 3762306a36Sopenharmony_ci new->bno = bno; 3862306a36Sopenharmony_ci new->length = len; 3962306a36Sopenharmony_ci INIT_LIST_HEAD(&new->list); 4062306a36Sopenharmony_ci new->flags = flags; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci /* trace before insert to be able to see failed inserts */ 4362306a36Sopenharmony_ci trace_xfs_extent_busy(pag->pag_mount, pag->pag_agno, bno, len); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 4662306a36Sopenharmony_ci rbp = &pag->pagb_tree.rb_node; 4762306a36Sopenharmony_ci while (*rbp) { 4862306a36Sopenharmony_ci parent = *rbp; 4962306a36Sopenharmony_ci busyp = rb_entry(parent, struct xfs_extent_busy, rb_node); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci if (new->bno < busyp->bno) { 5262306a36Sopenharmony_ci rbp = &(*rbp)->rb_left; 5362306a36Sopenharmony_ci ASSERT(new->bno + new->length <= busyp->bno); 5462306a36Sopenharmony_ci } else if (new->bno > busyp->bno) { 5562306a36Sopenharmony_ci rbp = &(*rbp)->rb_right; 5662306a36Sopenharmony_ci ASSERT(bno >= busyp->bno + busyp->length); 5762306a36Sopenharmony_ci } else { 5862306a36Sopenharmony_ci ASSERT(0); 5962306a36Sopenharmony_ci } 6062306a36Sopenharmony_ci } 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ci rb_link_node(&new->rb_node, parent, rbp); 6362306a36Sopenharmony_ci rb_insert_color(&new->rb_node, &pag->pagb_tree); 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci /* always process discard lists in fifo order */ 6662306a36Sopenharmony_ci list_add_tail(&new->list, busy_list); 6762306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 6862306a36Sopenharmony_ci} 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_civoid 7162306a36Sopenharmony_cixfs_extent_busy_insert( 7262306a36Sopenharmony_ci struct xfs_trans *tp, 7362306a36Sopenharmony_ci struct xfs_perag *pag, 7462306a36Sopenharmony_ci xfs_agblock_t bno, 7562306a36Sopenharmony_ci xfs_extlen_t len, 7662306a36Sopenharmony_ci unsigned int flags) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci xfs_extent_busy_insert_list(pag, bno, len, flags, &tp->t_busy); 7962306a36Sopenharmony_ci} 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_civoid 8262306a36Sopenharmony_cixfs_extent_busy_insert_discard( 8362306a36Sopenharmony_ci struct xfs_perag *pag, 8462306a36Sopenharmony_ci xfs_agblock_t bno, 8562306a36Sopenharmony_ci xfs_extlen_t len, 8662306a36Sopenharmony_ci struct list_head *busy_list) 8762306a36Sopenharmony_ci{ 8862306a36Sopenharmony_ci xfs_extent_busy_insert_list(pag, bno, len, XFS_EXTENT_BUSY_DISCARDED, 8962306a36Sopenharmony_ci busy_list); 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci/* 9362306a36Sopenharmony_ci * Search for a busy extent within the range of the extent we are about to 9462306a36Sopenharmony_ci * allocate. You need to be holding the busy extent tree lock when calling 9562306a36Sopenharmony_ci * xfs_extent_busy_search(). This function returns 0 for no overlapping busy 9662306a36Sopenharmony_ci * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact 9762306a36Sopenharmony_ci * match. This is done so that a non-zero return indicates an overlap that 9862306a36Sopenharmony_ci * will require a synchronous transaction, but it can still be 9962306a36Sopenharmony_ci * used to distinguish between a partial or exact match. 10062306a36Sopenharmony_ci */ 10162306a36Sopenharmony_ciint 10262306a36Sopenharmony_cixfs_extent_busy_search( 10362306a36Sopenharmony_ci struct xfs_mount *mp, 10462306a36Sopenharmony_ci struct xfs_perag *pag, 10562306a36Sopenharmony_ci xfs_agblock_t bno, 10662306a36Sopenharmony_ci xfs_extlen_t len) 10762306a36Sopenharmony_ci{ 10862306a36Sopenharmony_ci struct rb_node *rbp; 10962306a36Sopenharmony_ci struct xfs_extent_busy *busyp; 11062306a36Sopenharmony_ci int match = 0; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci /* find closest start bno overlap */ 11362306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 11462306a36Sopenharmony_ci rbp = pag->pagb_tree.rb_node; 11562306a36Sopenharmony_ci while (rbp) { 11662306a36Sopenharmony_ci busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node); 11762306a36Sopenharmony_ci if (bno < busyp->bno) { 11862306a36Sopenharmony_ci /* may overlap, but exact start block is lower */ 11962306a36Sopenharmony_ci if (bno + len > busyp->bno) 12062306a36Sopenharmony_ci match = -1; 12162306a36Sopenharmony_ci rbp = rbp->rb_left; 12262306a36Sopenharmony_ci } else if (bno > busyp->bno) { 12362306a36Sopenharmony_ci /* may overlap, but exact start block is higher */ 12462306a36Sopenharmony_ci if (bno < busyp->bno + busyp->length) 12562306a36Sopenharmony_ci match = -1; 12662306a36Sopenharmony_ci rbp = rbp->rb_right; 12762306a36Sopenharmony_ci } else { 12862306a36Sopenharmony_ci /* bno matches busyp, length determines exact match */ 12962306a36Sopenharmony_ci match = (busyp->length == len) ? 1 : -1; 13062306a36Sopenharmony_ci break; 13162306a36Sopenharmony_ci } 13262306a36Sopenharmony_ci } 13362306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 13462306a36Sopenharmony_ci return match; 13562306a36Sopenharmony_ci} 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci/* 13862306a36Sopenharmony_ci * The found free extent [fbno, fend] overlaps part or all of the given busy 13962306a36Sopenharmony_ci * extent. If the overlap covers the beginning, the end, or all of the busy 14062306a36Sopenharmony_ci * extent, the overlapping portion can be made unbusy and used for the 14162306a36Sopenharmony_ci * allocation. We can't split a busy extent because we can't modify a 14262306a36Sopenharmony_ci * transaction/CIL context busy list, but we can update an entry's block 14362306a36Sopenharmony_ci * number or length. 14462306a36Sopenharmony_ci * 14562306a36Sopenharmony_ci * Returns true if the extent can safely be reused, or false if the search 14662306a36Sopenharmony_ci * needs to be restarted. 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_ciSTATIC bool 14962306a36Sopenharmony_cixfs_extent_busy_update_extent( 15062306a36Sopenharmony_ci struct xfs_mount *mp, 15162306a36Sopenharmony_ci struct xfs_perag *pag, 15262306a36Sopenharmony_ci struct xfs_extent_busy *busyp, 15362306a36Sopenharmony_ci xfs_agblock_t fbno, 15462306a36Sopenharmony_ci xfs_extlen_t flen, 15562306a36Sopenharmony_ci bool userdata) __releases(&pag->pagb_lock) 15662306a36Sopenharmony_ci __acquires(&pag->pagb_lock) 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci xfs_agblock_t fend = fbno + flen; 15962306a36Sopenharmony_ci xfs_agblock_t bbno = busyp->bno; 16062306a36Sopenharmony_ci xfs_agblock_t bend = bbno + busyp->length; 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci /* 16362306a36Sopenharmony_ci * This extent is currently being discarded. Give the thread 16462306a36Sopenharmony_ci * performing the discard a chance to mark the extent unbusy 16562306a36Sopenharmony_ci * and retry. 16662306a36Sopenharmony_ci */ 16762306a36Sopenharmony_ci if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) { 16862306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 16962306a36Sopenharmony_ci delay(1); 17062306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 17162306a36Sopenharmony_ci return false; 17262306a36Sopenharmony_ci } 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci /* 17562306a36Sopenharmony_ci * If there is a busy extent overlapping a user allocation, we have 17662306a36Sopenharmony_ci * no choice but to force the log and retry the search. 17762306a36Sopenharmony_ci * 17862306a36Sopenharmony_ci * Fortunately this does not happen during normal operation, but 17962306a36Sopenharmony_ci * only if the filesystem is very low on space and has to dip into 18062306a36Sopenharmony_ci * the AGFL for normal allocations. 18162306a36Sopenharmony_ci */ 18262306a36Sopenharmony_ci if (userdata) 18362306a36Sopenharmony_ci goto out_force_log; 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (bbno < fbno && bend > fend) { 18662306a36Sopenharmony_ci /* 18762306a36Sopenharmony_ci * Case 1: 18862306a36Sopenharmony_ci * bbno bend 18962306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 19062306a36Sopenharmony_ci * +---------+ 19162306a36Sopenharmony_ci * fbno fend 19262306a36Sopenharmony_ci */ 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci /* 19562306a36Sopenharmony_ci * We would have to split the busy extent to be able to track 19662306a36Sopenharmony_ci * it correct, which we cannot do because we would have to 19762306a36Sopenharmony_ci * modify the list of busy extents attached to the transaction 19862306a36Sopenharmony_ci * or CIL context, which is immutable. 19962306a36Sopenharmony_ci * 20062306a36Sopenharmony_ci * Force out the log to clear the busy extent and retry the 20162306a36Sopenharmony_ci * search. 20262306a36Sopenharmony_ci */ 20362306a36Sopenharmony_ci goto out_force_log; 20462306a36Sopenharmony_ci } else if (bbno >= fbno && bend <= fend) { 20562306a36Sopenharmony_ci /* 20662306a36Sopenharmony_ci * Case 2: 20762306a36Sopenharmony_ci * bbno bend 20862306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 20962306a36Sopenharmony_ci * +-----------------+ 21062306a36Sopenharmony_ci * fbno fend 21162306a36Sopenharmony_ci * 21262306a36Sopenharmony_ci * Case 3: 21362306a36Sopenharmony_ci * bbno bend 21462306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 21562306a36Sopenharmony_ci * +--------------------------+ 21662306a36Sopenharmony_ci * fbno fend 21762306a36Sopenharmony_ci * 21862306a36Sopenharmony_ci * Case 4: 21962306a36Sopenharmony_ci * bbno bend 22062306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 22162306a36Sopenharmony_ci * +--------------------------+ 22262306a36Sopenharmony_ci * fbno fend 22362306a36Sopenharmony_ci * 22462306a36Sopenharmony_ci * Case 5: 22562306a36Sopenharmony_ci * bbno bend 22662306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 22762306a36Sopenharmony_ci * +-----------------------------------+ 22862306a36Sopenharmony_ci * fbno fend 22962306a36Sopenharmony_ci * 23062306a36Sopenharmony_ci */ 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci /* 23362306a36Sopenharmony_ci * The busy extent is fully covered by the extent we are 23462306a36Sopenharmony_ci * allocating, and can simply be removed from the rbtree. 23562306a36Sopenharmony_ci * However we cannot remove it from the immutable list 23662306a36Sopenharmony_ci * tracking busy extents in the transaction or CIL context, 23762306a36Sopenharmony_ci * so set the length to zero to mark it invalid. 23862306a36Sopenharmony_ci * 23962306a36Sopenharmony_ci * We also need to restart the busy extent search from the 24062306a36Sopenharmony_ci * tree root, because erasing the node can rearrange the 24162306a36Sopenharmony_ci * tree topology. 24262306a36Sopenharmony_ci */ 24362306a36Sopenharmony_ci rb_erase(&busyp->rb_node, &pag->pagb_tree); 24462306a36Sopenharmony_ci busyp->length = 0; 24562306a36Sopenharmony_ci return false; 24662306a36Sopenharmony_ci } else if (fend < bend) { 24762306a36Sopenharmony_ci /* 24862306a36Sopenharmony_ci * Case 6: 24962306a36Sopenharmony_ci * bbno bend 25062306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 25162306a36Sopenharmony_ci * +---------+ 25262306a36Sopenharmony_ci * fbno fend 25362306a36Sopenharmony_ci * 25462306a36Sopenharmony_ci * Case 7: 25562306a36Sopenharmony_ci * bbno bend 25662306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 25762306a36Sopenharmony_ci * +------------------+ 25862306a36Sopenharmony_ci * fbno fend 25962306a36Sopenharmony_ci * 26062306a36Sopenharmony_ci */ 26162306a36Sopenharmony_ci busyp->bno = fend; 26262306a36Sopenharmony_ci busyp->length = bend - fend; 26362306a36Sopenharmony_ci } else if (bbno < fbno) { 26462306a36Sopenharmony_ci /* 26562306a36Sopenharmony_ci * Case 8: 26662306a36Sopenharmony_ci * bbno bend 26762306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 26862306a36Sopenharmony_ci * +-------------+ 26962306a36Sopenharmony_ci * fbno fend 27062306a36Sopenharmony_ci * 27162306a36Sopenharmony_ci * Case 9: 27262306a36Sopenharmony_ci * bbno bend 27362306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 27462306a36Sopenharmony_ci * +----------------------+ 27562306a36Sopenharmony_ci * fbno fend 27662306a36Sopenharmony_ci */ 27762306a36Sopenharmony_ci busyp->length = fbno - busyp->bno; 27862306a36Sopenharmony_ci } else { 27962306a36Sopenharmony_ci ASSERT(0); 28062306a36Sopenharmony_ci } 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen); 28362306a36Sopenharmony_ci return true; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ciout_force_log: 28662306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 28762306a36Sopenharmony_ci xfs_log_force(mp, XFS_LOG_SYNC); 28862306a36Sopenharmony_ci trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen); 28962306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 29062306a36Sopenharmony_ci return false; 29162306a36Sopenharmony_ci} 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci/* 29562306a36Sopenharmony_ci * For a given extent [fbno, flen], make sure we can reuse it safely. 29662306a36Sopenharmony_ci */ 29762306a36Sopenharmony_civoid 29862306a36Sopenharmony_cixfs_extent_busy_reuse( 29962306a36Sopenharmony_ci struct xfs_mount *mp, 30062306a36Sopenharmony_ci struct xfs_perag *pag, 30162306a36Sopenharmony_ci xfs_agblock_t fbno, 30262306a36Sopenharmony_ci xfs_extlen_t flen, 30362306a36Sopenharmony_ci bool userdata) 30462306a36Sopenharmony_ci{ 30562306a36Sopenharmony_ci struct rb_node *rbp; 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ci ASSERT(flen > 0); 30862306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 30962306a36Sopenharmony_cirestart: 31062306a36Sopenharmony_ci rbp = pag->pagb_tree.rb_node; 31162306a36Sopenharmony_ci while (rbp) { 31262306a36Sopenharmony_ci struct xfs_extent_busy *busyp = 31362306a36Sopenharmony_ci rb_entry(rbp, struct xfs_extent_busy, rb_node); 31462306a36Sopenharmony_ci xfs_agblock_t bbno = busyp->bno; 31562306a36Sopenharmony_ci xfs_agblock_t bend = bbno + busyp->length; 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci if (fbno + flen <= bbno) { 31862306a36Sopenharmony_ci rbp = rbp->rb_left; 31962306a36Sopenharmony_ci continue; 32062306a36Sopenharmony_ci } else if (fbno >= bend) { 32162306a36Sopenharmony_ci rbp = rbp->rb_right; 32262306a36Sopenharmony_ci continue; 32362306a36Sopenharmony_ci } 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen, 32662306a36Sopenharmony_ci userdata)) 32762306a36Sopenharmony_ci goto restart; 32862306a36Sopenharmony_ci } 32962306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 33062306a36Sopenharmony_ci} 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci/* 33362306a36Sopenharmony_ci * For a given extent [fbno, flen], search the busy extent list to find a 33462306a36Sopenharmony_ci * subset of the extent that is not busy. If *rlen is smaller than 33562306a36Sopenharmony_ci * args->minlen no suitable extent could be found, and the higher level 33662306a36Sopenharmony_ci * code needs to force out the log and retry the allocation. 33762306a36Sopenharmony_ci * 33862306a36Sopenharmony_ci * Return the current busy generation for the AG if the extent is busy. This 33962306a36Sopenharmony_ci * value can be used to wait for at least one of the currently busy extents 34062306a36Sopenharmony_ci * to be cleared. Note that the busy list is not guaranteed to be empty after 34162306a36Sopenharmony_ci * the gen is woken. The state of a specific extent must always be confirmed 34262306a36Sopenharmony_ci * with another call to xfs_extent_busy_trim() before it can be used. 34362306a36Sopenharmony_ci */ 34462306a36Sopenharmony_cibool 34562306a36Sopenharmony_cixfs_extent_busy_trim( 34662306a36Sopenharmony_ci struct xfs_alloc_arg *args, 34762306a36Sopenharmony_ci xfs_agblock_t *bno, 34862306a36Sopenharmony_ci xfs_extlen_t *len, 34962306a36Sopenharmony_ci unsigned *busy_gen) 35062306a36Sopenharmony_ci{ 35162306a36Sopenharmony_ci xfs_agblock_t fbno; 35262306a36Sopenharmony_ci xfs_extlen_t flen; 35362306a36Sopenharmony_ci struct rb_node *rbp; 35462306a36Sopenharmony_ci bool ret = false; 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci ASSERT(*len > 0); 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci spin_lock(&args->pag->pagb_lock); 35962306a36Sopenharmony_ci fbno = *bno; 36062306a36Sopenharmony_ci flen = *len; 36162306a36Sopenharmony_ci rbp = args->pag->pagb_tree.rb_node; 36262306a36Sopenharmony_ci while (rbp && flen >= args->minlen) { 36362306a36Sopenharmony_ci struct xfs_extent_busy *busyp = 36462306a36Sopenharmony_ci rb_entry(rbp, struct xfs_extent_busy, rb_node); 36562306a36Sopenharmony_ci xfs_agblock_t fend = fbno + flen; 36662306a36Sopenharmony_ci xfs_agblock_t bbno = busyp->bno; 36762306a36Sopenharmony_ci xfs_agblock_t bend = bbno + busyp->length; 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci if (fend <= bbno) { 37062306a36Sopenharmony_ci rbp = rbp->rb_left; 37162306a36Sopenharmony_ci continue; 37262306a36Sopenharmony_ci } else if (fbno >= bend) { 37362306a36Sopenharmony_ci rbp = rbp->rb_right; 37462306a36Sopenharmony_ci continue; 37562306a36Sopenharmony_ci } 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci if (bbno <= fbno) { 37862306a36Sopenharmony_ci /* start overlap */ 37962306a36Sopenharmony_ci 38062306a36Sopenharmony_ci /* 38162306a36Sopenharmony_ci * Case 1: 38262306a36Sopenharmony_ci * bbno bend 38362306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 38462306a36Sopenharmony_ci * +---------+ 38562306a36Sopenharmony_ci * fbno fend 38662306a36Sopenharmony_ci * 38762306a36Sopenharmony_ci * Case 2: 38862306a36Sopenharmony_ci * bbno bend 38962306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 39062306a36Sopenharmony_ci * +-------------+ 39162306a36Sopenharmony_ci * fbno fend 39262306a36Sopenharmony_ci * 39362306a36Sopenharmony_ci * Case 3: 39462306a36Sopenharmony_ci * bbno bend 39562306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 39662306a36Sopenharmony_ci * +-------------+ 39762306a36Sopenharmony_ci * fbno fend 39862306a36Sopenharmony_ci * 39962306a36Sopenharmony_ci * Case 4: 40062306a36Sopenharmony_ci * bbno bend 40162306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 40262306a36Sopenharmony_ci * +-----------------+ 40362306a36Sopenharmony_ci * fbno fend 40462306a36Sopenharmony_ci * 40562306a36Sopenharmony_ci * No unbusy region in extent, return failure. 40662306a36Sopenharmony_ci */ 40762306a36Sopenharmony_ci if (fend <= bend) 40862306a36Sopenharmony_ci goto fail; 40962306a36Sopenharmony_ci 41062306a36Sopenharmony_ci /* 41162306a36Sopenharmony_ci * Case 5: 41262306a36Sopenharmony_ci * bbno bend 41362306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 41462306a36Sopenharmony_ci * +----------------------+ 41562306a36Sopenharmony_ci * fbno fend 41662306a36Sopenharmony_ci * 41762306a36Sopenharmony_ci * Case 6: 41862306a36Sopenharmony_ci * bbno bend 41962306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 42062306a36Sopenharmony_ci * +--------------------------+ 42162306a36Sopenharmony_ci * fbno fend 42262306a36Sopenharmony_ci * 42362306a36Sopenharmony_ci * Needs to be trimmed to: 42462306a36Sopenharmony_ci * +-------+ 42562306a36Sopenharmony_ci * fbno fend 42662306a36Sopenharmony_ci */ 42762306a36Sopenharmony_ci fbno = bend; 42862306a36Sopenharmony_ci } else if (bend >= fend) { 42962306a36Sopenharmony_ci /* end overlap */ 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci /* 43262306a36Sopenharmony_ci * Case 7: 43362306a36Sopenharmony_ci * bbno bend 43462306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 43562306a36Sopenharmony_ci * +------------------+ 43662306a36Sopenharmony_ci * fbno fend 43762306a36Sopenharmony_ci * 43862306a36Sopenharmony_ci * Case 8: 43962306a36Sopenharmony_ci * bbno bend 44062306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 44162306a36Sopenharmony_ci * +--------------------------+ 44262306a36Sopenharmony_ci * fbno fend 44362306a36Sopenharmony_ci * 44462306a36Sopenharmony_ci * Needs to be trimmed to: 44562306a36Sopenharmony_ci * +-------+ 44662306a36Sopenharmony_ci * fbno fend 44762306a36Sopenharmony_ci */ 44862306a36Sopenharmony_ci fend = bbno; 44962306a36Sopenharmony_ci } else { 45062306a36Sopenharmony_ci /* middle overlap */ 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci /* 45362306a36Sopenharmony_ci * Case 9: 45462306a36Sopenharmony_ci * bbno bend 45562306a36Sopenharmony_ci * +BBBBBBBBBBBBBBBBB+ 45662306a36Sopenharmony_ci * +-----------------------------------+ 45762306a36Sopenharmony_ci * fbno fend 45862306a36Sopenharmony_ci * 45962306a36Sopenharmony_ci * Can be trimmed to: 46062306a36Sopenharmony_ci * +-------+ OR +-------+ 46162306a36Sopenharmony_ci * fbno fend fbno fend 46262306a36Sopenharmony_ci * 46362306a36Sopenharmony_ci * Backward allocation leads to significant 46462306a36Sopenharmony_ci * fragmentation of directories, which degrades 46562306a36Sopenharmony_ci * directory performance, therefore we always want to 46662306a36Sopenharmony_ci * choose the option that produces forward allocation 46762306a36Sopenharmony_ci * patterns. 46862306a36Sopenharmony_ci * Preferring the lower bno extent will make the next 46962306a36Sopenharmony_ci * request use "fend" as the start of the next 47062306a36Sopenharmony_ci * allocation; if the segment is no longer busy at 47162306a36Sopenharmony_ci * that point, we'll get a contiguous allocation, but 47262306a36Sopenharmony_ci * even if it is still busy, we will get a forward 47362306a36Sopenharmony_ci * allocation. 47462306a36Sopenharmony_ci * We try to avoid choosing the segment at "bend", 47562306a36Sopenharmony_ci * because that can lead to the next allocation 47662306a36Sopenharmony_ci * taking the segment at "fbno", which would be a 47762306a36Sopenharmony_ci * backward allocation. We only use the segment at 47862306a36Sopenharmony_ci * "fbno" if it is much larger than the current 47962306a36Sopenharmony_ci * requested size, because in that case there's a 48062306a36Sopenharmony_ci * good chance subsequent allocations will be 48162306a36Sopenharmony_ci * contiguous. 48262306a36Sopenharmony_ci */ 48362306a36Sopenharmony_ci if (bbno - fbno >= args->maxlen) { 48462306a36Sopenharmony_ci /* left candidate fits perfect */ 48562306a36Sopenharmony_ci fend = bbno; 48662306a36Sopenharmony_ci } else if (fend - bend >= args->maxlen * 4) { 48762306a36Sopenharmony_ci /* right candidate has enough free space */ 48862306a36Sopenharmony_ci fbno = bend; 48962306a36Sopenharmony_ci } else if (bbno - fbno >= args->minlen) { 49062306a36Sopenharmony_ci /* left candidate fits minimum requirement */ 49162306a36Sopenharmony_ci fend = bbno; 49262306a36Sopenharmony_ci } else { 49362306a36Sopenharmony_ci goto fail; 49462306a36Sopenharmony_ci } 49562306a36Sopenharmony_ci } 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci flen = fend - fbno; 49862306a36Sopenharmony_ci } 49962306a36Sopenharmony_ciout: 50062306a36Sopenharmony_ci 50162306a36Sopenharmony_ci if (fbno != *bno || flen != *len) { 50262306a36Sopenharmony_ci trace_xfs_extent_busy_trim(args->mp, args->agno, *bno, *len, 50362306a36Sopenharmony_ci fbno, flen); 50462306a36Sopenharmony_ci *bno = fbno; 50562306a36Sopenharmony_ci *len = flen; 50662306a36Sopenharmony_ci *busy_gen = args->pag->pagb_gen; 50762306a36Sopenharmony_ci ret = true; 50862306a36Sopenharmony_ci } 50962306a36Sopenharmony_ci spin_unlock(&args->pag->pagb_lock); 51062306a36Sopenharmony_ci return ret; 51162306a36Sopenharmony_cifail: 51262306a36Sopenharmony_ci /* 51362306a36Sopenharmony_ci * Return a zero extent length as failure indications. All callers 51462306a36Sopenharmony_ci * re-check if the trimmed extent satisfies the minlen requirement. 51562306a36Sopenharmony_ci */ 51662306a36Sopenharmony_ci flen = 0; 51762306a36Sopenharmony_ci goto out; 51862306a36Sopenharmony_ci} 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ciSTATIC void 52162306a36Sopenharmony_cixfs_extent_busy_clear_one( 52262306a36Sopenharmony_ci struct xfs_mount *mp, 52362306a36Sopenharmony_ci struct xfs_perag *pag, 52462306a36Sopenharmony_ci struct xfs_extent_busy *busyp) 52562306a36Sopenharmony_ci{ 52662306a36Sopenharmony_ci if (busyp->length) { 52762306a36Sopenharmony_ci trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno, 52862306a36Sopenharmony_ci busyp->length); 52962306a36Sopenharmony_ci rb_erase(&busyp->rb_node, &pag->pagb_tree); 53062306a36Sopenharmony_ci } 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_ci list_del_init(&busyp->list); 53362306a36Sopenharmony_ci kmem_free(busyp); 53462306a36Sopenharmony_ci} 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_cistatic void 53762306a36Sopenharmony_cixfs_extent_busy_put_pag( 53862306a36Sopenharmony_ci struct xfs_perag *pag, 53962306a36Sopenharmony_ci bool wakeup) 54062306a36Sopenharmony_ci __releases(pag->pagb_lock) 54162306a36Sopenharmony_ci{ 54262306a36Sopenharmony_ci if (wakeup) { 54362306a36Sopenharmony_ci pag->pagb_gen++; 54462306a36Sopenharmony_ci wake_up_all(&pag->pagb_wait); 54562306a36Sopenharmony_ci } 54662306a36Sopenharmony_ci 54762306a36Sopenharmony_ci spin_unlock(&pag->pagb_lock); 54862306a36Sopenharmony_ci xfs_perag_put(pag); 54962306a36Sopenharmony_ci} 55062306a36Sopenharmony_ci 55162306a36Sopenharmony_ci/* 55262306a36Sopenharmony_ci * Remove all extents on the passed in list from the busy extents tree. 55362306a36Sopenharmony_ci * If do_discard is set skip extents that need to be discarded, and mark 55462306a36Sopenharmony_ci * these as undergoing a discard operation instead. 55562306a36Sopenharmony_ci */ 55662306a36Sopenharmony_civoid 55762306a36Sopenharmony_cixfs_extent_busy_clear( 55862306a36Sopenharmony_ci struct xfs_mount *mp, 55962306a36Sopenharmony_ci struct list_head *list, 56062306a36Sopenharmony_ci bool do_discard) 56162306a36Sopenharmony_ci{ 56262306a36Sopenharmony_ci struct xfs_extent_busy *busyp, *n; 56362306a36Sopenharmony_ci struct xfs_perag *pag = NULL; 56462306a36Sopenharmony_ci xfs_agnumber_t agno = NULLAGNUMBER; 56562306a36Sopenharmony_ci bool wakeup = false; 56662306a36Sopenharmony_ci 56762306a36Sopenharmony_ci list_for_each_entry_safe(busyp, n, list, list) { 56862306a36Sopenharmony_ci if (busyp->agno != agno) { 56962306a36Sopenharmony_ci if (pag) 57062306a36Sopenharmony_ci xfs_extent_busy_put_pag(pag, wakeup); 57162306a36Sopenharmony_ci agno = busyp->agno; 57262306a36Sopenharmony_ci pag = xfs_perag_get(mp, agno); 57362306a36Sopenharmony_ci spin_lock(&pag->pagb_lock); 57462306a36Sopenharmony_ci wakeup = false; 57562306a36Sopenharmony_ci } 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ci if (do_discard && busyp->length && 57862306a36Sopenharmony_ci !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD)) { 57962306a36Sopenharmony_ci busyp->flags = XFS_EXTENT_BUSY_DISCARDED; 58062306a36Sopenharmony_ci } else { 58162306a36Sopenharmony_ci xfs_extent_busy_clear_one(mp, pag, busyp); 58262306a36Sopenharmony_ci wakeup = true; 58362306a36Sopenharmony_ci } 58462306a36Sopenharmony_ci } 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci if (pag) 58762306a36Sopenharmony_ci xfs_extent_busy_put_pag(pag, wakeup); 58862306a36Sopenharmony_ci} 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci/* 59162306a36Sopenharmony_ci * Flush out all busy extents for this AG. 59262306a36Sopenharmony_ci * 59362306a36Sopenharmony_ci * If the current transaction is holding busy extents, the caller may not want 59462306a36Sopenharmony_ci * to wait for committed busy extents to resolve. If we are being told just to 59562306a36Sopenharmony_ci * try a flush or progress has been made since we last skipped a busy extent, 59662306a36Sopenharmony_ci * return immediately to allow the caller to try again. 59762306a36Sopenharmony_ci * 59862306a36Sopenharmony_ci * If we are freeing extents, we might actually be holding the only free extents 59962306a36Sopenharmony_ci * in the transaction busy list and the log force won't resolve that situation. 60062306a36Sopenharmony_ci * In this case, we must return -EAGAIN to avoid a deadlock by informing the 60162306a36Sopenharmony_ci * caller it needs to commit the busy extents it holds before retrying the 60262306a36Sopenharmony_ci * extent free operation. 60362306a36Sopenharmony_ci */ 60462306a36Sopenharmony_ciint 60562306a36Sopenharmony_cixfs_extent_busy_flush( 60662306a36Sopenharmony_ci struct xfs_trans *tp, 60762306a36Sopenharmony_ci struct xfs_perag *pag, 60862306a36Sopenharmony_ci unsigned busy_gen, 60962306a36Sopenharmony_ci uint32_t alloc_flags) 61062306a36Sopenharmony_ci{ 61162306a36Sopenharmony_ci DEFINE_WAIT (wait); 61262306a36Sopenharmony_ci int error; 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci error = xfs_log_force(tp->t_mountp, XFS_LOG_SYNC); 61562306a36Sopenharmony_ci if (error) 61662306a36Sopenharmony_ci return error; 61762306a36Sopenharmony_ci 61862306a36Sopenharmony_ci /* Avoid deadlocks on uncommitted busy extents. */ 61962306a36Sopenharmony_ci if (!list_empty(&tp->t_busy)) { 62062306a36Sopenharmony_ci if (alloc_flags & XFS_ALLOC_FLAG_TRYFLUSH) 62162306a36Sopenharmony_ci return 0; 62262306a36Sopenharmony_ci 62362306a36Sopenharmony_ci if (busy_gen != READ_ONCE(pag->pagb_gen)) 62462306a36Sopenharmony_ci return 0; 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci if (alloc_flags & XFS_ALLOC_FLAG_FREEING) 62762306a36Sopenharmony_ci return -EAGAIN; 62862306a36Sopenharmony_ci } 62962306a36Sopenharmony_ci 63062306a36Sopenharmony_ci /* Wait for committed busy extents to resolve. */ 63162306a36Sopenharmony_ci do { 63262306a36Sopenharmony_ci prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE); 63362306a36Sopenharmony_ci if (busy_gen != READ_ONCE(pag->pagb_gen)) 63462306a36Sopenharmony_ci break; 63562306a36Sopenharmony_ci schedule(); 63662306a36Sopenharmony_ci } while (1); 63762306a36Sopenharmony_ci 63862306a36Sopenharmony_ci finish_wait(&pag->pagb_wait, &wait); 63962306a36Sopenharmony_ci return 0; 64062306a36Sopenharmony_ci} 64162306a36Sopenharmony_ci 64262306a36Sopenharmony_civoid 64362306a36Sopenharmony_cixfs_extent_busy_wait_all( 64462306a36Sopenharmony_ci struct xfs_mount *mp) 64562306a36Sopenharmony_ci{ 64662306a36Sopenharmony_ci struct xfs_perag *pag; 64762306a36Sopenharmony_ci DEFINE_WAIT (wait); 64862306a36Sopenharmony_ci xfs_agnumber_t agno; 64962306a36Sopenharmony_ci 65062306a36Sopenharmony_ci for_each_perag(mp, agno, pag) { 65162306a36Sopenharmony_ci do { 65262306a36Sopenharmony_ci prepare_to_wait(&pag->pagb_wait, &wait, TASK_KILLABLE); 65362306a36Sopenharmony_ci if (RB_EMPTY_ROOT(&pag->pagb_tree)) 65462306a36Sopenharmony_ci break; 65562306a36Sopenharmony_ci schedule(); 65662306a36Sopenharmony_ci } while (1); 65762306a36Sopenharmony_ci finish_wait(&pag->pagb_wait, &wait); 65862306a36Sopenharmony_ci } 65962306a36Sopenharmony_ci} 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci/* 66262306a36Sopenharmony_ci * Callback for list_sort to sort busy extents by the AG they reside in. 66362306a36Sopenharmony_ci */ 66462306a36Sopenharmony_ciint 66562306a36Sopenharmony_cixfs_extent_busy_ag_cmp( 66662306a36Sopenharmony_ci void *priv, 66762306a36Sopenharmony_ci const struct list_head *l1, 66862306a36Sopenharmony_ci const struct list_head *l2) 66962306a36Sopenharmony_ci{ 67062306a36Sopenharmony_ci struct xfs_extent_busy *b1 = 67162306a36Sopenharmony_ci container_of(l1, struct xfs_extent_busy, list); 67262306a36Sopenharmony_ci struct xfs_extent_busy *b2 = 67362306a36Sopenharmony_ci container_of(l2, struct xfs_extent_busy, list); 67462306a36Sopenharmony_ci s32 diff; 67562306a36Sopenharmony_ci 67662306a36Sopenharmony_ci diff = b1->agno - b2->agno; 67762306a36Sopenharmony_ci if (!diff) 67862306a36Sopenharmony_ci diff = b1->bno - b2->bno; 67962306a36Sopenharmony_ci return diff; 68062306a36Sopenharmony_ci} 681