xfs_extent_busy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * Copyright (c) 2010 David Chinner.
  4. * Copyright (c) 2011 Christoph Hellwig.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it would be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write the Free Software Foundation,
  18. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "xfs.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_shared.h"
  25. #include "xfs_trans_resv.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_extent_busy.h"
  31. #include "xfs_trace.h"
  32. #include "xfs_trans.h"
  33. #include "xfs_log.h"
  34. void
  35. xfs_extent_busy_insert(
  36. struct xfs_trans *tp,
  37. xfs_agnumber_t agno,
  38. xfs_agblock_t bno,
  39. xfs_extlen_t len,
  40. unsigned int flags)
  41. {
  42. struct xfs_extent_busy *new;
  43. struct xfs_extent_busy *busyp;
  44. struct xfs_perag *pag;
  45. struct rb_node **rbp;
  46. struct rb_node *parent = NULL;
  47. new = kmem_zalloc(sizeof(struct xfs_extent_busy), KM_MAYFAIL);
  48. if (!new) {
  49. /*
  50. * No Memory! Since it is now not possible to track the free
  51. * block, make this a synchronous transaction to insure that
  52. * the block is not reused before this transaction commits.
  53. */
  54. trace_xfs_extent_busy_enomem(tp->t_mountp, agno, bno, len);
  55. xfs_trans_set_sync(tp);
  56. return;
  57. }
  58. new->agno = agno;
  59. new->bno = bno;
  60. new->length = len;
  61. INIT_LIST_HEAD(&new->list);
  62. new->flags = flags;
  63. /* trace before insert to be able to see failed inserts */
  64. trace_xfs_extent_busy(tp->t_mountp, agno, bno, len);
  65. pag = xfs_perag_get(tp->t_mountp, new->agno);
  66. spin_lock(&pag->pagb_lock);
  67. rbp = &pag->pagb_tree.rb_node;
  68. while (*rbp) {
  69. parent = *rbp;
  70. busyp = rb_entry(parent, struct xfs_extent_busy, rb_node);
  71. if (new->bno < busyp->bno) {
  72. rbp = &(*rbp)->rb_left;
  73. ASSERT(new->bno + new->length <= busyp->bno);
  74. } else if (new->bno > busyp->bno) {
  75. rbp = &(*rbp)->rb_right;
  76. ASSERT(bno >= busyp->bno + busyp->length);
  77. } else {
  78. ASSERT(0);
  79. }
  80. }
  81. rb_link_node(&new->rb_node, parent, rbp);
  82. rb_insert_color(&new->rb_node, &pag->pagb_tree);
  83. list_add(&new->list, &tp->t_busy);
  84. spin_unlock(&pag->pagb_lock);
  85. xfs_perag_put(pag);
  86. }
  87. /*
  88. * Search for a busy extent within the range of the extent we are about to
  89. * allocate. You need to be holding the busy extent tree lock when calling
  90. * xfs_extent_busy_search(). This function returns 0 for no overlapping busy
  91. * extent, -1 for an overlapping but not exact busy extent, and 1 for an exact
  92. * match. This is done so that a non-zero return indicates an overlap that
  93. * will require a synchronous transaction, but it can still be
  94. * used to distinguish between a partial or exact match.
  95. */
  96. int
  97. xfs_extent_busy_search(
  98. struct xfs_mount *mp,
  99. xfs_agnumber_t agno,
  100. xfs_agblock_t bno,
  101. xfs_extlen_t len)
  102. {
  103. struct xfs_perag *pag;
  104. struct rb_node *rbp;
  105. struct xfs_extent_busy *busyp;
  106. int match = 0;
  107. pag = xfs_perag_get(mp, agno);
  108. spin_lock(&pag->pagb_lock);
  109. rbp = pag->pagb_tree.rb_node;
  110. /* find closest start bno overlap */
  111. while (rbp) {
  112. busyp = rb_entry(rbp, struct xfs_extent_busy, rb_node);
  113. if (bno < busyp->bno) {
  114. /* may overlap, but exact start block is lower */
  115. if (bno + len > busyp->bno)
  116. match = -1;
  117. rbp = rbp->rb_left;
  118. } else if (bno > busyp->bno) {
  119. /* may overlap, but exact start block is higher */
  120. if (bno < busyp->bno + busyp->length)
  121. match = -1;
  122. rbp = rbp->rb_right;
  123. } else {
  124. /* bno matches busyp, length determines exact match */
  125. match = (busyp->length == len) ? 1 : -1;
  126. break;
  127. }
  128. }
  129. spin_unlock(&pag->pagb_lock);
  130. xfs_perag_put(pag);
  131. return match;
  132. }
  133. /*
  134. * The found free extent [fbno, fend] overlaps part or all of the given busy
  135. * extent. If the overlap covers the beginning, the end, or all of the busy
  136. * extent, the overlapping portion can be made unbusy and used for the
  137. * allocation. We can't split a busy extent because we can't modify a
  138. * transaction/CIL context busy list, but we can update an entry's block
  139. * number or length.
  140. *
  141. * Returns true if the extent can safely be reused, or false if the search
  142. * needs to be restarted.
  143. */
  144. STATIC bool
  145. xfs_extent_busy_update_extent(
  146. struct xfs_mount *mp,
  147. struct xfs_perag *pag,
  148. struct xfs_extent_busy *busyp,
  149. xfs_agblock_t fbno,
  150. xfs_extlen_t flen,
  151. bool userdata) __releases(&pag->pagb_lock)
  152. __acquires(&pag->pagb_lock)
  153. {
  154. xfs_agblock_t fend = fbno + flen;
  155. xfs_agblock_t bbno = busyp->bno;
  156. xfs_agblock_t bend = bbno + busyp->length;
  157. /*
  158. * This extent is currently being discarded. Give the thread
  159. * performing the discard a chance to mark the extent unbusy
  160. * and retry.
  161. */
  162. if (busyp->flags & XFS_EXTENT_BUSY_DISCARDED) {
  163. spin_unlock(&pag->pagb_lock);
  164. delay(1);
  165. spin_lock(&pag->pagb_lock);
  166. return false;
  167. }
  168. /*
  169. * If there is a busy extent overlapping a user allocation, we have
  170. * no choice but to force the log and retry the search.
  171. *
  172. * Fortunately this does not happen during normal operation, but
  173. * only if the filesystem is very low on space and has to dip into
  174. * the AGFL for normal allocations.
  175. */
  176. if (userdata)
  177. goto out_force_log;
  178. if (bbno < fbno && bend > fend) {
  179. /*
  180. * Case 1:
  181. * bbno bend
  182. * +BBBBBBBBBBBBBBBBB+
  183. * +---------+
  184. * fbno fend
  185. */
  186. /*
  187. * We would have to split the busy extent to be able to track
  188. * it correct, which we cannot do because we would have to
  189. * modify the list of busy extents attached to the transaction
  190. * or CIL context, which is immutable.
  191. *
  192. * Force out the log to clear the busy extent and retry the
  193. * search.
  194. */
  195. goto out_force_log;
  196. } else if (bbno >= fbno && bend <= fend) {
  197. /*
  198. * Case 2:
  199. * bbno bend
  200. * +BBBBBBBBBBBBBBBBB+
  201. * +-----------------+
  202. * fbno fend
  203. *
  204. * Case 3:
  205. * bbno bend
  206. * +BBBBBBBBBBBBBBBBB+
  207. * +--------------------------+
  208. * fbno fend
  209. *
  210. * Case 4:
  211. * bbno bend
  212. * +BBBBBBBBBBBBBBBBB+
  213. * +--------------------------+
  214. * fbno fend
  215. *
  216. * Case 5:
  217. * bbno bend
  218. * +BBBBBBBBBBBBBBBBB+
  219. * +-----------------------------------+
  220. * fbno fend
  221. *
  222. */
  223. /*
  224. * The busy extent is fully covered by the extent we are
  225. * allocating, and can simply be removed from the rbtree.
  226. * However we cannot remove it from the immutable list
  227. * tracking busy extents in the transaction or CIL context,
  228. * so set the length to zero to mark it invalid.
  229. *
  230. * We also need to restart the busy extent search from the
  231. * tree root, because erasing the node can rearrange the
  232. * tree topology.
  233. */
  234. rb_erase(&busyp->rb_node, &pag->pagb_tree);
  235. busyp->length = 0;
  236. return false;
  237. } else if (fend < bend) {
  238. /*
  239. * Case 6:
  240. * bbno bend
  241. * +BBBBBBBBBBBBBBBBB+
  242. * +---------+
  243. * fbno fend
  244. *
  245. * Case 7:
  246. * bbno bend
  247. * +BBBBBBBBBBBBBBBBB+
  248. * +------------------+
  249. * fbno fend
  250. *
  251. */
  252. busyp->bno = fend;
  253. } else if (bbno < fbno) {
  254. /*
  255. * Case 8:
  256. * bbno bend
  257. * +BBBBBBBBBBBBBBBBB+
  258. * +-------------+
  259. * fbno fend
  260. *
  261. * Case 9:
  262. * bbno bend
  263. * +BBBBBBBBBBBBBBBBB+
  264. * +----------------------+
  265. * fbno fend
  266. */
  267. busyp->length = fbno - busyp->bno;
  268. } else {
  269. ASSERT(0);
  270. }
  271. trace_xfs_extent_busy_reuse(mp, pag->pag_agno, fbno, flen);
  272. return true;
  273. out_force_log:
  274. spin_unlock(&pag->pagb_lock);
  275. xfs_log_force(mp, XFS_LOG_SYNC);
  276. trace_xfs_extent_busy_force(mp, pag->pag_agno, fbno, flen);
  277. spin_lock(&pag->pagb_lock);
  278. return false;
  279. }
  280. /*
  281. * For a given extent [fbno, flen], make sure we can reuse it safely.
  282. */
  283. void
  284. xfs_extent_busy_reuse(
  285. struct xfs_mount *mp,
  286. xfs_agnumber_t agno,
  287. xfs_agblock_t fbno,
  288. xfs_extlen_t flen,
  289. bool userdata)
  290. {
  291. struct xfs_perag *pag;
  292. struct rb_node *rbp;
  293. ASSERT(flen > 0);
  294. pag = xfs_perag_get(mp, agno);
  295. spin_lock(&pag->pagb_lock);
  296. restart:
  297. rbp = pag->pagb_tree.rb_node;
  298. while (rbp) {
  299. struct xfs_extent_busy *busyp =
  300. rb_entry(rbp, struct xfs_extent_busy, rb_node);
  301. xfs_agblock_t bbno = busyp->bno;
  302. xfs_agblock_t bend = bbno + busyp->length;
  303. if (fbno + flen <= bbno) {
  304. rbp = rbp->rb_left;
  305. continue;
  306. } else if (fbno >= bend) {
  307. rbp = rbp->rb_right;
  308. continue;
  309. }
  310. if (!xfs_extent_busy_update_extent(mp, pag, busyp, fbno, flen,
  311. userdata))
  312. goto restart;
  313. }
  314. spin_unlock(&pag->pagb_lock);
  315. xfs_perag_put(pag);
  316. }
  317. /*
  318. * For a given extent [fbno, flen], search the busy extent list to find a
  319. * subset of the extent that is not busy. If *rlen is smaller than
  320. * args->minlen no suitable extent could be found, and the higher level
  321. * code needs to force out the log and retry the allocation.
  322. */
  323. void
  324. xfs_extent_busy_trim(
  325. struct xfs_alloc_arg *args,
  326. xfs_agblock_t bno,
  327. xfs_extlen_t len,
  328. xfs_agblock_t *rbno,
  329. xfs_extlen_t *rlen)
  330. {
  331. xfs_agblock_t fbno;
  332. xfs_extlen_t flen;
  333. struct rb_node *rbp;
  334. ASSERT(len > 0);
  335. spin_lock(&args->pag->pagb_lock);
  336. restart:
  337. fbno = bno;
  338. flen = len;
  339. rbp = args->pag->pagb_tree.rb_node;
  340. while (rbp && flen >= args->minlen) {
  341. struct xfs_extent_busy *busyp =
  342. rb_entry(rbp, struct xfs_extent_busy, rb_node);
  343. xfs_agblock_t fend = fbno + flen;
  344. xfs_agblock_t bbno = busyp->bno;
  345. xfs_agblock_t bend = bbno + busyp->length;
  346. if (fend <= bbno) {
  347. rbp = rbp->rb_left;
  348. continue;
  349. } else if (fbno >= bend) {
  350. rbp = rbp->rb_right;
  351. continue;
  352. }
  353. /*
  354. * If this is a metadata allocation, try to reuse the busy
  355. * extent instead of trimming the allocation.
  356. */
  357. if (!args->userdata &&
  358. !(busyp->flags & XFS_EXTENT_BUSY_DISCARDED)) {
  359. if (!xfs_extent_busy_update_extent(args->mp, args->pag,
  360. busyp, fbno, flen,
  361. false))
  362. goto restart;
  363. continue;
  364. }
  365. if (bbno <= fbno) {
  366. /* start overlap */
  367. /*
  368. * Case 1:
  369. * bbno bend
  370. * +BBBBBBBBBBBBBBBBB+
  371. * +---------+
  372. * fbno fend
  373. *
  374. * Case 2:
  375. * bbno bend
  376. * +BBBBBBBBBBBBBBBBB+
  377. * +-------------+
  378. * fbno fend
  379. *
  380. * Case 3:
  381. * bbno bend
  382. * +BBBBBBBBBBBBBBBBB+
  383. * +-------------+
  384. * fbno fend
  385. *
  386. * Case 4:
  387. * bbno bend
  388. * +BBBBBBBBBBBBBBBBB+
  389. * +-----------------+
  390. * fbno fend
  391. *
  392. * No unbusy region in extent, return failure.
  393. */
  394. if (fend <= bend)
  395. goto fail;
  396. /*
  397. * Case 5:
  398. * bbno bend
  399. * +BBBBBBBBBBBBBBBBB+
  400. * +----------------------+
  401. * fbno fend
  402. *
  403. * Case 6:
  404. * bbno bend
  405. * +BBBBBBBBBBBBBBBBB+
  406. * +--------------------------+
  407. * fbno fend
  408. *
  409. * Needs to be trimmed to:
  410. * +-------+
  411. * fbno fend
  412. */
  413. fbno = bend;
  414. } else if (bend >= fend) {
  415. /* end overlap */
  416. /*
  417. * Case 7:
  418. * bbno bend
  419. * +BBBBBBBBBBBBBBBBB+
  420. * +------------------+
  421. * fbno fend
  422. *
  423. * Case 8:
  424. * bbno bend
  425. * +BBBBBBBBBBBBBBBBB+
  426. * +--------------------------+
  427. * fbno fend
  428. *
  429. * Needs to be trimmed to:
  430. * +-------+
  431. * fbno fend
  432. */
  433. fend = bbno;
  434. } else {
  435. /* middle overlap */
  436. /*
  437. * Case 9:
  438. * bbno bend
  439. * +BBBBBBBBBBBBBBBBB+
  440. * +-----------------------------------+
  441. * fbno fend
  442. *
  443. * Can be trimmed to:
  444. * +-------+ OR +-------+
  445. * fbno fend fbno fend
  446. *
  447. * Backward allocation leads to significant
  448. * fragmentation of directories, which degrades
  449. * directory performance, therefore we always want to
  450. * choose the option that produces forward allocation
  451. * patterns.
  452. * Preferring the lower bno extent will make the next
  453. * request use "fend" as the start of the next
  454. * allocation; if the segment is no longer busy at
  455. * that point, we'll get a contiguous allocation, but
  456. * even if it is still busy, we will get a forward
  457. * allocation.
  458. * We try to avoid choosing the segment at "bend",
  459. * because that can lead to the next allocation
  460. * taking the segment at "fbno", which would be a
  461. * backward allocation. We only use the segment at
  462. * "fbno" if it is much larger than the current
  463. * requested size, because in that case there's a
  464. * good chance subsequent allocations will be
  465. * contiguous.
  466. */
  467. if (bbno - fbno >= args->maxlen) {
  468. /* left candidate fits perfect */
  469. fend = bbno;
  470. } else if (fend - bend >= args->maxlen * 4) {
  471. /* right candidate has enough free space */
  472. fbno = bend;
  473. } else if (bbno - fbno >= args->minlen) {
  474. /* left candidate fits minimum requirement */
  475. fend = bbno;
  476. } else {
  477. goto fail;
  478. }
  479. }
  480. flen = fend - fbno;
  481. }
  482. spin_unlock(&args->pag->pagb_lock);
  483. if (fbno != bno || flen != len) {
  484. trace_xfs_extent_busy_trim(args->mp, args->agno, bno, len,
  485. fbno, flen);
  486. }
  487. *rbno = fbno;
  488. *rlen = flen;
  489. return;
  490. fail:
  491. /*
  492. * Return a zero extent length as failure indications. All callers
  493. * re-check if the trimmed extent satisfies the minlen requirement.
  494. */
  495. spin_unlock(&args->pag->pagb_lock);
  496. trace_xfs_extent_busy_trim(args->mp, args->agno, bno, len, fbno, 0);
  497. *rbno = fbno;
  498. *rlen = 0;
  499. }
  500. STATIC void
  501. xfs_extent_busy_clear_one(
  502. struct xfs_mount *mp,
  503. struct xfs_perag *pag,
  504. struct xfs_extent_busy *busyp)
  505. {
  506. if (busyp->length) {
  507. trace_xfs_extent_busy_clear(mp, busyp->agno, busyp->bno,
  508. busyp->length);
  509. rb_erase(&busyp->rb_node, &pag->pagb_tree);
  510. }
  511. list_del_init(&busyp->list);
  512. kmem_free(busyp);
  513. }
  514. /*
  515. * Remove all extents on the passed in list from the busy extents tree.
  516. * If do_discard is set skip extents that need to be discarded, and mark
  517. * these as undergoing a discard operation instead.
  518. */
  519. void
  520. xfs_extent_busy_clear(
  521. struct xfs_mount *mp,
  522. struct list_head *list,
  523. bool do_discard)
  524. {
  525. struct xfs_extent_busy *busyp, *n;
  526. struct xfs_perag *pag = NULL;
  527. xfs_agnumber_t agno = NULLAGNUMBER;
  528. list_for_each_entry_safe(busyp, n, list, list) {
  529. if (busyp->agno != agno) {
  530. if (pag) {
  531. spin_unlock(&pag->pagb_lock);
  532. xfs_perag_put(pag);
  533. }
  534. pag = xfs_perag_get(mp, busyp->agno);
  535. spin_lock(&pag->pagb_lock);
  536. agno = busyp->agno;
  537. }
  538. if (do_discard && busyp->length &&
  539. !(busyp->flags & XFS_EXTENT_BUSY_SKIP_DISCARD))
  540. busyp->flags = XFS_EXTENT_BUSY_DISCARDED;
  541. else
  542. xfs_extent_busy_clear_one(mp, pag, busyp);
  543. }
  544. if (pag) {
  545. spin_unlock(&pag->pagb_lock);
  546. xfs_perag_put(pag);
  547. }
  548. }
  549. /*
  550. * Callback for list_sort to sort busy extents by the AG they reside in.
  551. */
  552. int
  553. xfs_extent_busy_ag_cmp(
  554. void *priv,
  555. struct list_head *a,
  556. struct list_head *b)
  557. {
  558. return container_of(a, struct xfs_extent_busy, list)->agno -
  559. container_of(b, struct xfs_extent_busy, list)->agno;
  560. }