PageRenderTime 61ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/xfs/xfs_alloc.c

https://bitbucket.org/digetx/picasso-kernel
C | 2558 lines | 1782 code | 180 blank | 596 comment | 457 complexity | 644457ef9a0b9eea7f668a1148b5a7ae MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_bmap_btree.h"
  28. #include "xfs_alloc_btree.h"
  29. #include "xfs_ialloc_btree.h"
  30. #include "xfs_dinode.h"
  31. #include "xfs_inode.h"
  32. #include "xfs_btree.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_extent_busy.h"
  35. #include "xfs_error.h"
  36. #include "xfs_trace.h"
  37. struct workqueue_struct *xfs_alloc_wq;
  38. #define XFS_ABSDIFF(a,b) (((a) <= (b)) ? ((b) - (a)) : ((a) - (b)))
  39. #define XFSA_FIXUP_BNO_OK 1
  40. #define XFSA_FIXUP_CNT_OK 2
  41. STATIC int xfs_alloc_ag_vextent_exact(xfs_alloc_arg_t *);
  42. STATIC int xfs_alloc_ag_vextent_near(xfs_alloc_arg_t *);
  43. STATIC int xfs_alloc_ag_vextent_size(xfs_alloc_arg_t *);
  44. STATIC int xfs_alloc_ag_vextent_small(xfs_alloc_arg_t *,
  45. xfs_btree_cur_t *, xfs_agblock_t *, xfs_extlen_t *, int *);
  46. /*
  47. * Lookup the record equal to [bno, len] in the btree given by cur.
  48. */
  49. STATIC int /* error */
  50. xfs_alloc_lookup_eq(
  51. struct xfs_btree_cur *cur, /* btree cursor */
  52. xfs_agblock_t bno, /* starting block of extent */
  53. xfs_extlen_t len, /* length of extent */
  54. int *stat) /* success/failure */
  55. {
  56. cur->bc_rec.a.ar_startblock = bno;
  57. cur->bc_rec.a.ar_blockcount = len;
  58. return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
  59. }
  60. /*
  61. * Lookup the first record greater than or equal to [bno, len]
  62. * in the btree given by cur.
  63. */
  64. int /* error */
  65. xfs_alloc_lookup_ge(
  66. struct xfs_btree_cur *cur, /* btree cursor */
  67. xfs_agblock_t bno, /* starting block of extent */
  68. xfs_extlen_t len, /* length of extent */
  69. int *stat) /* success/failure */
  70. {
  71. cur->bc_rec.a.ar_startblock = bno;
  72. cur->bc_rec.a.ar_blockcount = len;
  73. return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
  74. }
  75. /*
  76. * Lookup the first record less than or equal to [bno, len]
  77. * in the btree given by cur.
  78. */
  79. int /* error */
  80. xfs_alloc_lookup_le(
  81. struct xfs_btree_cur *cur, /* btree cursor */
  82. xfs_agblock_t bno, /* starting block of extent */
  83. xfs_extlen_t len, /* length of extent */
  84. int *stat) /* success/failure */
  85. {
  86. cur->bc_rec.a.ar_startblock = bno;
  87. cur->bc_rec.a.ar_blockcount = len;
  88. return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat);
  89. }
  90. /*
  91. * Update the record referred to by cur to the value given
  92. * by [bno, len].
  93. * This either works (return 0) or gets an EFSCORRUPTED error.
  94. */
  95. STATIC int /* error */
  96. xfs_alloc_update(
  97. struct xfs_btree_cur *cur, /* btree cursor */
  98. xfs_agblock_t bno, /* starting block of extent */
  99. xfs_extlen_t len) /* length of extent */
  100. {
  101. union xfs_btree_rec rec;
  102. rec.alloc.ar_startblock = cpu_to_be32(bno);
  103. rec.alloc.ar_blockcount = cpu_to_be32(len);
  104. return xfs_btree_update(cur, &rec);
  105. }
  106. /*
  107. * Get the data from the pointed-to record.
  108. */
  109. int /* error */
  110. xfs_alloc_get_rec(
  111. struct xfs_btree_cur *cur, /* btree cursor */
  112. xfs_agblock_t *bno, /* output: starting block of extent */
  113. xfs_extlen_t *len, /* output: length of extent */
  114. int *stat) /* output: success/failure */
  115. {
  116. union xfs_btree_rec *rec;
  117. int error;
  118. error = xfs_btree_get_rec(cur, &rec, stat);
  119. if (!error && *stat == 1) {
  120. *bno = be32_to_cpu(rec->alloc.ar_startblock);
  121. *len = be32_to_cpu(rec->alloc.ar_blockcount);
  122. }
  123. return error;
  124. }
  125. /*
  126. * Compute aligned version of the found extent.
  127. * Takes alignment and min length into account.
  128. */
  129. STATIC void
  130. xfs_alloc_compute_aligned(
  131. xfs_alloc_arg_t *args, /* allocation argument structure */
  132. xfs_agblock_t foundbno, /* starting block in found extent */
  133. xfs_extlen_t foundlen, /* length in found extent */
  134. xfs_agblock_t *resbno, /* result block number */
  135. xfs_extlen_t *reslen) /* result length */
  136. {
  137. xfs_agblock_t bno;
  138. xfs_extlen_t len;
  139. /* Trim busy sections out of found extent */
  140. xfs_extent_busy_trim(args, foundbno, foundlen, &bno, &len);
  141. if (args->alignment > 1 && len >= args->minlen) {
  142. xfs_agblock_t aligned_bno = roundup(bno, args->alignment);
  143. xfs_extlen_t diff = aligned_bno - bno;
  144. *resbno = aligned_bno;
  145. *reslen = diff >= len ? 0 : len - diff;
  146. } else {
  147. *resbno = bno;
  148. *reslen = len;
  149. }
  150. }
  151. /*
  152. * Compute best start block and diff for "near" allocations.
  153. * freelen >= wantlen already checked by caller.
  154. */
  155. STATIC xfs_extlen_t /* difference value (absolute) */
  156. xfs_alloc_compute_diff(
  157. xfs_agblock_t wantbno, /* target starting block */
  158. xfs_extlen_t wantlen, /* target length */
  159. xfs_extlen_t alignment, /* target alignment */
  160. xfs_agblock_t freebno, /* freespace's starting block */
  161. xfs_extlen_t freelen, /* freespace's length */
  162. xfs_agblock_t *newbnop) /* result: best start block from free */
  163. {
  164. xfs_agblock_t freeend; /* end of freespace extent */
  165. xfs_agblock_t newbno1; /* return block number */
  166. xfs_agblock_t newbno2; /* other new block number */
  167. xfs_extlen_t newlen1=0; /* length with newbno1 */
  168. xfs_extlen_t newlen2=0; /* length with newbno2 */
  169. xfs_agblock_t wantend; /* end of target extent */
  170. ASSERT(freelen >= wantlen);
  171. freeend = freebno + freelen;
  172. wantend = wantbno + wantlen;
  173. if (freebno >= wantbno) {
  174. if ((newbno1 = roundup(freebno, alignment)) >= freeend)
  175. newbno1 = NULLAGBLOCK;
  176. } else if (freeend >= wantend && alignment > 1) {
  177. newbno1 = roundup(wantbno, alignment);
  178. newbno2 = newbno1 - alignment;
  179. if (newbno1 >= freeend)
  180. newbno1 = NULLAGBLOCK;
  181. else
  182. newlen1 = XFS_EXTLEN_MIN(wantlen, freeend - newbno1);
  183. if (newbno2 < freebno)
  184. newbno2 = NULLAGBLOCK;
  185. else
  186. newlen2 = XFS_EXTLEN_MIN(wantlen, freeend - newbno2);
  187. if (newbno1 != NULLAGBLOCK && newbno2 != NULLAGBLOCK) {
  188. if (newlen1 < newlen2 ||
  189. (newlen1 == newlen2 &&
  190. XFS_ABSDIFF(newbno1, wantbno) >
  191. XFS_ABSDIFF(newbno2, wantbno)))
  192. newbno1 = newbno2;
  193. } else if (newbno2 != NULLAGBLOCK)
  194. newbno1 = newbno2;
  195. } else if (freeend >= wantend) {
  196. newbno1 = wantbno;
  197. } else if (alignment > 1) {
  198. newbno1 = roundup(freeend - wantlen, alignment);
  199. if (newbno1 > freeend - wantlen &&
  200. newbno1 - alignment >= freebno)
  201. newbno1 -= alignment;
  202. else if (newbno1 >= freeend)
  203. newbno1 = NULLAGBLOCK;
  204. } else
  205. newbno1 = freeend - wantlen;
  206. *newbnop = newbno1;
  207. return newbno1 == NULLAGBLOCK ? 0 : XFS_ABSDIFF(newbno1, wantbno);
  208. }
  209. /*
  210. * Fix up the length, based on mod and prod.
  211. * len should be k * prod + mod for some k.
  212. * If len is too small it is returned unchanged.
  213. * If len hits maxlen it is left alone.
  214. */
  215. STATIC void
  216. xfs_alloc_fix_len(
  217. xfs_alloc_arg_t *args) /* allocation argument structure */
  218. {
  219. xfs_extlen_t k;
  220. xfs_extlen_t rlen;
  221. ASSERT(args->mod < args->prod);
  222. rlen = args->len;
  223. ASSERT(rlen >= args->minlen);
  224. ASSERT(rlen <= args->maxlen);
  225. if (args->prod <= 1 || rlen < args->mod || rlen == args->maxlen ||
  226. (args->mod == 0 && rlen < args->prod))
  227. return;
  228. k = rlen % args->prod;
  229. if (k == args->mod)
  230. return;
  231. if (k > args->mod) {
  232. if ((int)(rlen = rlen - k - args->mod) < (int)args->minlen)
  233. return;
  234. } else {
  235. if ((int)(rlen = rlen - args->prod - (args->mod - k)) <
  236. (int)args->minlen)
  237. return;
  238. }
  239. ASSERT(rlen >= args->minlen);
  240. ASSERT(rlen <= args->maxlen);
  241. args->len = rlen;
  242. }
  243. /*
  244. * Fix up length if there is too little space left in the a.g.
  245. * Return 1 if ok, 0 if too little, should give up.
  246. */
  247. STATIC int
  248. xfs_alloc_fix_minleft(
  249. xfs_alloc_arg_t *args) /* allocation argument structure */
  250. {
  251. xfs_agf_t *agf; /* a.g. freelist header */
  252. int diff; /* free space difference */
  253. if (args->minleft == 0)
  254. return 1;
  255. agf = XFS_BUF_TO_AGF(args->agbp);
  256. diff = be32_to_cpu(agf->agf_freeblks)
  257. - args->len - args->minleft;
  258. if (diff >= 0)
  259. return 1;
  260. args->len += diff; /* shrink the allocated space */
  261. if (args->len >= args->minlen)
  262. return 1;
  263. args->agbno = NULLAGBLOCK;
  264. return 0;
  265. }
  266. /*
  267. * Update the two btrees, logically removing from freespace the extent
  268. * starting at rbno, rlen blocks. The extent is contained within the
  269. * actual (current) free extent fbno for flen blocks.
  270. * Flags are passed in indicating whether the cursors are set to the
  271. * relevant records.
  272. */
  273. STATIC int /* error code */
  274. xfs_alloc_fixup_trees(
  275. xfs_btree_cur_t *cnt_cur, /* cursor for by-size btree */
  276. xfs_btree_cur_t *bno_cur, /* cursor for by-block btree */
  277. xfs_agblock_t fbno, /* starting block of free extent */
  278. xfs_extlen_t flen, /* length of free extent */
  279. xfs_agblock_t rbno, /* starting block of returned extent */
  280. xfs_extlen_t rlen, /* length of returned extent */
  281. int flags) /* flags, XFSA_FIXUP_... */
  282. {
  283. int error; /* error code */
  284. int i; /* operation results */
  285. xfs_agblock_t nfbno1; /* first new free startblock */
  286. xfs_agblock_t nfbno2; /* second new free startblock */
  287. xfs_extlen_t nflen1=0; /* first new free length */
  288. xfs_extlen_t nflen2=0; /* second new free length */
  289. /*
  290. * Look up the record in the by-size tree if necessary.
  291. */
  292. if (flags & XFSA_FIXUP_CNT_OK) {
  293. #ifdef DEBUG
  294. if ((error = xfs_alloc_get_rec(cnt_cur, &nfbno1, &nflen1, &i)))
  295. return error;
  296. XFS_WANT_CORRUPTED_RETURN(
  297. i == 1 && nfbno1 == fbno && nflen1 == flen);
  298. #endif
  299. } else {
  300. if ((error = xfs_alloc_lookup_eq(cnt_cur, fbno, flen, &i)))
  301. return error;
  302. XFS_WANT_CORRUPTED_RETURN(i == 1);
  303. }
  304. /*
  305. * Look up the record in the by-block tree if necessary.
  306. */
  307. if (flags & XFSA_FIXUP_BNO_OK) {
  308. #ifdef DEBUG
  309. if ((error = xfs_alloc_get_rec(bno_cur, &nfbno1, &nflen1, &i)))
  310. return error;
  311. XFS_WANT_CORRUPTED_RETURN(
  312. i == 1 && nfbno1 == fbno && nflen1 == flen);
  313. #endif
  314. } else {
  315. if ((error = xfs_alloc_lookup_eq(bno_cur, fbno, flen, &i)))
  316. return error;
  317. XFS_WANT_CORRUPTED_RETURN(i == 1);
  318. }
  319. #ifdef DEBUG
  320. if (bno_cur->bc_nlevels == 1 && cnt_cur->bc_nlevels == 1) {
  321. struct xfs_btree_block *bnoblock;
  322. struct xfs_btree_block *cntblock;
  323. bnoblock = XFS_BUF_TO_BLOCK(bno_cur->bc_bufs[0]);
  324. cntblock = XFS_BUF_TO_BLOCK(cnt_cur->bc_bufs[0]);
  325. XFS_WANT_CORRUPTED_RETURN(
  326. bnoblock->bb_numrecs == cntblock->bb_numrecs);
  327. }
  328. #endif
  329. /*
  330. * Deal with all four cases: the allocated record is contained
  331. * within the freespace record, so we can have new freespace
  332. * at either (or both) end, or no freespace remaining.
  333. */
  334. if (rbno == fbno && rlen == flen)
  335. nfbno1 = nfbno2 = NULLAGBLOCK;
  336. else if (rbno == fbno) {
  337. nfbno1 = rbno + rlen;
  338. nflen1 = flen - rlen;
  339. nfbno2 = NULLAGBLOCK;
  340. } else if (rbno + rlen == fbno + flen) {
  341. nfbno1 = fbno;
  342. nflen1 = flen - rlen;
  343. nfbno2 = NULLAGBLOCK;
  344. } else {
  345. nfbno1 = fbno;
  346. nflen1 = rbno - fbno;
  347. nfbno2 = rbno + rlen;
  348. nflen2 = (fbno + flen) - nfbno2;
  349. }
  350. /*
  351. * Delete the entry from the by-size btree.
  352. */
  353. if ((error = xfs_btree_delete(cnt_cur, &i)))
  354. return error;
  355. XFS_WANT_CORRUPTED_RETURN(i == 1);
  356. /*
  357. * Add new by-size btree entry(s).
  358. */
  359. if (nfbno1 != NULLAGBLOCK) {
  360. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno1, nflen1, &i)))
  361. return error;
  362. XFS_WANT_CORRUPTED_RETURN(i == 0);
  363. if ((error = xfs_btree_insert(cnt_cur, &i)))
  364. return error;
  365. XFS_WANT_CORRUPTED_RETURN(i == 1);
  366. }
  367. if (nfbno2 != NULLAGBLOCK) {
  368. if ((error = xfs_alloc_lookup_eq(cnt_cur, nfbno2, nflen2, &i)))
  369. return error;
  370. XFS_WANT_CORRUPTED_RETURN(i == 0);
  371. if ((error = xfs_btree_insert(cnt_cur, &i)))
  372. return error;
  373. XFS_WANT_CORRUPTED_RETURN(i == 1);
  374. }
  375. /*
  376. * Fix up the by-block btree entry(s).
  377. */
  378. if (nfbno1 == NULLAGBLOCK) {
  379. /*
  380. * No remaining freespace, just delete the by-block tree entry.
  381. */
  382. if ((error = xfs_btree_delete(bno_cur, &i)))
  383. return error;
  384. XFS_WANT_CORRUPTED_RETURN(i == 1);
  385. } else {
  386. /*
  387. * Update the by-block entry to start later|be shorter.
  388. */
  389. if ((error = xfs_alloc_update(bno_cur, nfbno1, nflen1)))
  390. return error;
  391. }
  392. if (nfbno2 != NULLAGBLOCK) {
  393. /*
  394. * 2 resulting free entries, need to add one.
  395. */
  396. if ((error = xfs_alloc_lookup_eq(bno_cur, nfbno2, nflen2, &i)))
  397. return error;
  398. XFS_WANT_CORRUPTED_RETURN(i == 0);
  399. if ((error = xfs_btree_insert(bno_cur, &i)))
  400. return error;
  401. XFS_WANT_CORRUPTED_RETURN(i == 1);
  402. }
  403. return 0;
  404. }
  405. static void
  406. xfs_agfl_verify(
  407. struct xfs_buf *bp)
  408. {
  409. #ifdef WHEN_CRCS_COME_ALONG
  410. /*
  411. * we cannot actually do any verification of the AGFL because mkfs does
  412. * not initialise the AGFL to zero or NULL. Hence the only valid part of
  413. * the AGFL is what the AGF says is active. We can't get to the AGF, so
  414. * we can't verify just those entries are valid.
  415. *
  416. * This problem goes away when the CRC format change comes along as that
  417. * requires the AGFL to be initialised by mkfs. At that point, we can
  418. * verify the blocks in the agfl -active or not- lie within the bounds
  419. * of the AG. Until then, just leave this check ifdef'd out.
  420. */
  421. struct xfs_mount *mp = bp->b_target->bt_mount;
  422. struct xfs_agfl *agfl = XFS_BUF_TO_AGFL(bp);
  423. int agfl_ok = 1;
  424. int i;
  425. for (i = 0; i < XFS_AGFL_SIZE(mp); i++) {
  426. if (be32_to_cpu(agfl->agfl_bno[i]) == NULLAGBLOCK ||
  427. be32_to_cpu(agfl->agfl_bno[i]) >= mp->m_sb.sb_agblocks)
  428. agfl_ok = 0;
  429. }
  430. if (!agfl_ok) {
  431. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, agfl);
  432. xfs_buf_ioerror(bp, EFSCORRUPTED);
  433. }
  434. #endif
  435. }
  436. static void
  437. xfs_agfl_write_verify(
  438. struct xfs_buf *bp)
  439. {
  440. xfs_agfl_verify(bp);
  441. }
  442. static void
  443. xfs_agfl_read_verify(
  444. struct xfs_buf *bp)
  445. {
  446. xfs_agfl_verify(bp);
  447. }
  448. const struct xfs_buf_ops xfs_agfl_buf_ops = {
  449. .verify_read = xfs_agfl_read_verify,
  450. .verify_write = xfs_agfl_write_verify,
  451. };
  452. /*
  453. * Read in the allocation group free block array.
  454. */
  455. STATIC int /* error */
  456. xfs_alloc_read_agfl(
  457. xfs_mount_t *mp, /* mount point structure */
  458. xfs_trans_t *tp, /* transaction pointer */
  459. xfs_agnumber_t agno, /* allocation group number */
  460. xfs_buf_t **bpp) /* buffer for the ag free block array */
  461. {
  462. xfs_buf_t *bp; /* return value */
  463. int error;
  464. ASSERT(agno != NULLAGNUMBER);
  465. error = xfs_trans_read_buf(
  466. mp, tp, mp->m_ddev_targp,
  467. XFS_AG_DADDR(mp, agno, XFS_AGFL_DADDR(mp)),
  468. XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_agfl_buf_ops);
  469. if (error)
  470. return error;
  471. ASSERT(!xfs_buf_geterror(bp));
  472. xfs_buf_set_ref(bp, XFS_AGFL_REF);
  473. *bpp = bp;
  474. return 0;
  475. }
  476. STATIC int
  477. xfs_alloc_update_counters(
  478. struct xfs_trans *tp,
  479. struct xfs_perag *pag,
  480. struct xfs_buf *agbp,
  481. long len)
  482. {
  483. struct xfs_agf *agf = XFS_BUF_TO_AGF(agbp);
  484. pag->pagf_freeblks += len;
  485. be32_add_cpu(&agf->agf_freeblks, len);
  486. xfs_trans_agblocks_delta(tp, len);
  487. if (unlikely(be32_to_cpu(agf->agf_freeblks) >
  488. be32_to_cpu(agf->agf_length)))
  489. return EFSCORRUPTED;
  490. xfs_alloc_log_agf(tp, agbp, XFS_AGF_FREEBLKS);
  491. return 0;
  492. }
  493. /*
  494. * Allocation group level functions.
  495. */
  496. /*
  497. * Allocate a variable extent in the allocation group agno.
  498. * Type and bno are used to determine where in the allocation group the
  499. * extent will start.
  500. * Extent's length (returned in *len) will be between minlen and maxlen,
  501. * and of the form k * prod + mod unless there's nothing that large.
  502. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  503. */
  504. STATIC int /* error */
  505. xfs_alloc_ag_vextent(
  506. xfs_alloc_arg_t *args) /* argument structure for allocation */
  507. {
  508. int error=0;
  509. ASSERT(args->minlen > 0);
  510. ASSERT(args->maxlen > 0);
  511. ASSERT(args->minlen <= args->maxlen);
  512. ASSERT(args->mod < args->prod);
  513. ASSERT(args->alignment > 0);
  514. /*
  515. * Branch to correct routine based on the type.
  516. */
  517. args->wasfromfl = 0;
  518. switch (args->type) {
  519. case XFS_ALLOCTYPE_THIS_AG:
  520. error = xfs_alloc_ag_vextent_size(args);
  521. break;
  522. case XFS_ALLOCTYPE_NEAR_BNO:
  523. error = xfs_alloc_ag_vextent_near(args);
  524. break;
  525. case XFS_ALLOCTYPE_THIS_BNO:
  526. error = xfs_alloc_ag_vextent_exact(args);
  527. break;
  528. default:
  529. ASSERT(0);
  530. /* NOTREACHED */
  531. }
  532. if (error || args->agbno == NULLAGBLOCK)
  533. return error;
  534. ASSERT(args->len >= args->minlen);
  535. ASSERT(args->len <= args->maxlen);
  536. ASSERT(!args->wasfromfl || !args->isfl);
  537. ASSERT(args->agbno % args->alignment == 0);
  538. if (!args->wasfromfl) {
  539. error = xfs_alloc_update_counters(args->tp, args->pag,
  540. args->agbp,
  541. -((long)(args->len)));
  542. if (error)
  543. return error;
  544. ASSERT(!xfs_extent_busy_search(args->mp, args->agno,
  545. args->agbno, args->len));
  546. }
  547. if (!args->isfl) {
  548. xfs_trans_mod_sb(args->tp, args->wasdel ?
  549. XFS_TRANS_SB_RES_FDBLOCKS :
  550. XFS_TRANS_SB_FDBLOCKS,
  551. -((long)(args->len)));
  552. }
  553. XFS_STATS_INC(xs_allocx);
  554. XFS_STATS_ADD(xs_allocb, args->len);
  555. return error;
  556. }
  557. /*
  558. * Allocate a variable extent at exactly agno/bno.
  559. * Extent's length (returned in *len) will be between minlen and maxlen,
  560. * and of the form k * prod + mod unless there's nothing that large.
  561. * Return the starting a.g. block (bno), or NULLAGBLOCK if we can't do it.
  562. */
  563. STATIC int /* error */
  564. xfs_alloc_ag_vextent_exact(
  565. xfs_alloc_arg_t *args) /* allocation argument structure */
  566. {
  567. xfs_btree_cur_t *bno_cur;/* by block-number btree cursor */
  568. xfs_btree_cur_t *cnt_cur;/* by count btree cursor */
  569. int error;
  570. xfs_agblock_t fbno; /* start block of found extent */
  571. xfs_extlen_t flen; /* length of found extent */
  572. xfs_agblock_t tbno; /* start block of trimmed extent */
  573. xfs_extlen_t tlen; /* length of trimmed extent */
  574. xfs_agblock_t tend; /* end block of trimmed extent */
  575. int i; /* success/failure of operation */
  576. ASSERT(args->alignment == 1);
  577. /*
  578. * Allocate/initialize a cursor for the by-number freespace btree.
  579. */
  580. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  581. args->agno, XFS_BTNUM_BNO);
  582. /*
  583. * Lookup bno and minlen in the btree (minlen is irrelevant, really).
  584. * Look for the closest free block <= bno, it must contain bno
  585. * if any free block does.
  586. */
  587. error = xfs_alloc_lookup_le(bno_cur, args->agbno, args->minlen, &i);
  588. if (error)
  589. goto error0;
  590. if (!i)
  591. goto not_found;
  592. /*
  593. * Grab the freespace record.
  594. */
  595. error = xfs_alloc_get_rec(bno_cur, &fbno, &flen, &i);
  596. if (error)
  597. goto error0;
  598. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  599. ASSERT(fbno <= args->agbno);
  600. /*
  601. * Check for overlapping busy extents.
  602. */
  603. xfs_extent_busy_trim(args, fbno, flen, &tbno, &tlen);
  604. /*
  605. * Give up if the start of the extent is busy, or the freespace isn't
  606. * long enough for the minimum request.
  607. */
  608. if (tbno > args->agbno)
  609. goto not_found;
  610. if (tlen < args->minlen)
  611. goto not_found;
  612. tend = tbno + tlen;
  613. if (tend < args->agbno + args->minlen)
  614. goto not_found;
  615. /*
  616. * End of extent will be smaller of the freespace end and the
  617. * maximal requested end.
  618. *
  619. * Fix the length according to mod and prod if given.
  620. */
  621. args->len = XFS_AGBLOCK_MIN(tend, args->agbno + args->maxlen)
  622. - args->agbno;
  623. xfs_alloc_fix_len(args);
  624. if (!xfs_alloc_fix_minleft(args))
  625. goto not_found;
  626. ASSERT(args->agbno + args->len <= tend);
  627. /*
  628. * We are allocating agbno for args->len
  629. * Allocate/initialize a cursor for the by-size btree.
  630. */
  631. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  632. args->agno, XFS_BTNUM_CNT);
  633. ASSERT(args->agbno + args->len <=
  634. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  635. error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen, args->agbno,
  636. args->len, XFSA_FIXUP_BNO_OK);
  637. if (error) {
  638. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  639. goto error0;
  640. }
  641. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  642. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  643. args->wasfromfl = 0;
  644. trace_xfs_alloc_exact_done(args);
  645. return 0;
  646. not_found:
  647. /* Didn't find it, return null. */
  648. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  649. args->agbno = NULLAGBLOCK;
  650. trace_xfs_alloc_exact_notfound(args);
  651. return 0;
  652. error0:
  653. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  654. trace_xfs_alloc_exact_error(args);
  655. return error;
  656. }
  657. /*
  658. * Search the btree in a given direction via the search cursor and compare
  659. * the records found against the good extent we've already found.
  660. */
  661. STATIC int
  662. xfs_alloc_find_best_extent(
  663. struct xfs_alloc_arg *args, /* allocation argument structure */
  664. struct xfs_btree_cur **gcur, /* good cursor */
  665. struct xfs_btree_cur **scur, /* searching cursor */
  666. xfs_agblock_t gdiff, /* difference for search comparison */
  667. xfs_agblock_t *sbno, /* extent found by search */
  668. xfs_extlen_t *slen, /* extent length */
  669. xfs_agblock_t *sbnoa, /* aligned extent found by search */
  670. xfs_extlen_t *slena, /* aligned extent length */
  671. int dir) /* 0 = search right, 1 = search left */
  672. {
  673. xfs_agblock_t new;
  674. xfs_agblock_t sdiff;
  675. int error;
  676. int i;
  677. /* The good extent is perfect, no need to search. */
  678. if (!gdiff)
  679. goto out_use_good;
  680. /*
  681. * Look until we find a better one, run out of space or run off the end.
  682. */
  683. do {
  684. error = xfs_alloc_get_rec(*scur, sbno, slen, &i);
  685. if (error)
  686. goto error0;
  687. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  688. xfs_alloc_compute_aligned(args, *sbno, *slen, sbnoa, slena);
  689. /*
  690. * The good extent is closer than this one.
  691. */
  692. if (!dir) {
  693. if (*sbnoa >= args->agbno + gdiff)
  694. goto out_use_good;
  695. } else {
  696. if (*sbnoa <= args->agbno - gdiff)
  697. goto out_use_good;
  698. }
  699. /*
  700. * Same distance, compare length and pick the best.
  701. */
  702. if (*slena >= args->minlen) {
  703. args->len = XFS_EXTLEN_MIN(*slena, args->maxlen);
  704. xfs_alloc_fix_len(args);
  705. sdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  706. args->alignment, *sbnoa,
  707. *slena, &new);
  708. /*
  709. * Choose closer size and invalidate other cursor.
  710. */
  711. if (sdiff < gdiff)
  712. goto out_use_search;
  713. goto out_use_good;
  714. }
  715. if (!dir)
  716. error = xfs_btree_increment(*scur, 0, &i);
  717. else
  718. error = xfs_btree_decrement(*scur, 0, &i);
  719. if (error)
  720. goto error0;
  721. } while (i);
  722. out_use_good:
  723. xfs_btree_del_cursor(*scur, XFS_BTREE_NOERROR);
  724. *scur = NULL;
  725. return 0;
  726. out_use_search:
  727. xfs_btree_del_cursor(*gcur, XFS_BTREE_NOERROR);
  728. *gcur = NULL;
  729. return 0;
  730. error0:
  731. /* caller invalidates cursors */
  732. return error;
  733. }
  734. /*
  735. * Allocate a variable extent near bno in the allocation group agno.
  736. * Extent's length (returned in len) will be between minlen and maxlen,
  737. * and of the form k * prod + mod unless there's nothing that large.
  738. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  739. */
  740. STATIC int /* error */
  741. xfs_alloc_ag_vextent_near(
  742. xfs_alloc_arg_t *args) /* allocation argument structure */
  743. {
  744. xfs_btree_cur_t *bno_cur_gt; /* cursor for bno btree, right side */
  745. xfs_btree_cur_t *bno_cur_lt; /* cursor for bno btree, left side */
  746. xfs_btree_cur_t *cnt_cur; /* cursor for count btree */
  747. xfs_agblock_t gtbno; /* start bno of right side entry */
  748. xfs_agblock_t gtbnoa; /* aligned ... */
  749. xfs_extlen_t gtdiff; /* difference to right side entry */
  750. xfs_extlen_t gtlen; /* length of right side entry */
  751. xfs_extlen_t gtlena; /* aligned ... */
  752. xfs_agblock_t gtnew; /* useful start bno of right side */
  753. int error; /* error code */
  754. int i; /* result code, temporary */
  755. int j; /* result code, temporary */
  756. xfs_agblock_t ltbno; /* start bno of left side entry */
  757. xfs_agblock_t ltbnoa; /* aligned ... */
  758. xfs_extlen_t ltdiff; /* difference to left side entry */
  759. xfs_extlen_t ltlen; /* length of left side entry */
  760. xfs_extlen_t ltlena; /* aligned ... */
  761. xfs_agblock_t ltnew; /* useful start bno of left side */
  762. xfs_extlen_t rlen; /* length of returned extent */
  763. int forced = 0;
  764. #if defined(DEBUG) && defined(__KERNEL__)
  765. /*
  766. * Randomly don't execute the first algorithm.
  767. */
  768. int dofirst; /* set to do first algorithm */
  769. dofirst = random32() & 1;
  770. #endif
  771. restart:
  772. bno_cur_lt = NULL;
  773. bno_cur_gt = NULL;
  774. ltlen = 0;
  775. gtlena = 0;
  776. ltlena = 0;
  777. /*
  778. * Get a cursor for the by-size btree.
  779. */
  780. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  781. args->agno, XFS_BTNUM_CNT);
  782. /*
  783. * See if there are any free extents as big as maxlen.
  784. */
  785. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0, args->maxlen, &i)))
  786. goto error0;
  787. /*
  788. * If none, then pick up the last entry in the tree unless the
  789. * tree is empty.
  790. */
  791. if (!i) {
  792. if ((error = xfs_alloc_ag_vextent_small(args, cnt_cur, &ltbno,
  793. &ltlen, &i)))
  794. goto error0;
  795. if (i == 0 || ltlen == 0) {
  796. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  797. trace_xfs_alloc_near_noentry(args);
  798. return 0;
  799. }
  800. ASSERT(i == 1);
  801. }
  802. args->wasfromfl = 0;
  803. /*
  804. * First algorithm.
  805. * If the requested extent is large wrt the freespaces available
  806. * in this a.g., then the cursor will be pointing to a btree entry
  807. * near the right edge of the tree. If it's in the last btree leaf
  808. * block, then we just examine all the entries in that block
  809. * that are big enough, and pick the best one.
  810. * This is written as a while loop so we can break out of it,
  811. * but we never loop back to the top.
  812. */
  813. while (xfs_btree_islastblock(cnt_cur, 0)) {
  814. xfs_extlen_t bdiff;
  815. int besti=0;
  816. xfs_extlen_t blen=0;
  817. xfs_agblock_t bnew=0;
  818. #if defined(DEBUG) && defined(__KERNEL__)
  819. if (!dofirst)
  820. break;
  821. #endif
  822. /*
  823. * Start from the entry that lookup found, sequence through
  824. * all larger free blocks. If we're actually pointing at a
  825. * record smaller than maxlen, go to the start of this block,
  826. * and skip all those smaller than minlen.
  827. */
  828. if (ltlen || args->alignment > 1) {
  829. cnt_cur->bc_ptrs[0] = 1;
  830. do {
  831. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno,
  832. &ltlen, &i)))
  833. goto error0;
  834. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  835. if (ltlen >= args->minlen)
  836. break;
  837. if ((error = xfs_btree_increment(cnt_cur, 0, &i)))
  838. goto error0;
  839. } while (i);
  840. ASSERT(ltlen >= args->minlen);
  841. if (!i)
  842. break;
  843. }
  844. i = cnt_cur->bc_ptrs[0];
  845. for (j = 1, blen = 0, bdiff = 0;
  846. !error && j && (blen < args->maxlen || bdiff > 0);
  847. error = xfs_btree_increment(cnt_cur, 0, &j)) {
  848. /*
  849. * For each entry, decide if it's better than
  850. * the previous best entry.
  851. */
  852. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  853. goto error0;
  854. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  855. xfs_alloc_compute_aligned(args, ltbno, ltlen,
  856. &ltbnoa, &ltlena);
  857. if (ltlena < args->minlen)
  858. continue;
  859. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  860. xfs_alloc_fix_len(args);
  861. ASSERT(args->len >= args->minlen);
  862. if (args->len < blen)
  863. continue;
  864. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  865. args->alignment, ltbnoa, ltlena, &ltnew);
  866. if (ltnew != NULLAGBLOCK &&
  867. (args->len > blen || ltdiff < bdiff)) {
  868. bdiff = ltdiff;
  869. bnew = ltnew;
  870. blen = args->len;
  871. besti = cnt_cur->bc_ptrs[0];
  872. }
  873. }
  874. /*
  875. * It didn't work. We COULD be in a case where
  876. * there's a good record somewhere, so try again.
  877. */
  878. if (blen == 0)
  879. break;
  880. /*
  881. * Point at the best entry, and retrieve it again.
  882. */
  883. cnt_cur->bc_ptrs[0] = besti;
  884. if ((error = xfs_alloc_get_rec(cnt_cur, &ltbno, &ltlen, &i)))
  885. goto error0;
  886. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  887. ASSERT(ltbno + ltlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  888. args->len = blen;
  889. if (!xfs_alloc_fix_minleft(args)) {
  890. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  891. trace_xfs_alloc_near_nominleft(args);
  892. return 0;
  893. }
  894. blen = args->len;
  895. /*
  896. * We are allocating starting at bnew for blen blocks.
  897. */
  898. args->agbno = bnew;
  899. ASSERT(bnew >= ltbno);
  900. ASSERT(bnew + blen <= ltbno + ltlen);
  901. /*
  902. * Set up a cursor for the by-bno tree.
  903. */
  904. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp,
  905. args->agbp, args->agno, XFS_BTNUM_BNO);
  906. /*
  907. * Fix up the btree entries.
  908. */
  909. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno,
  910. ltlen, bnew, blen, XFSA_FIXUP_CNT_OK)))
  911. goto error0;
  912. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  913. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  914. trace_xfs_alloc_near_first(args);
  915. return 0;
  916. }
  917. /*
  918. * Second algorithm.
  919. * Search in the by-bno tree to the left and to the right
  920. * simultaneously, until in each case we find a space big enough,
  921. * or run into the edge of the tree. When we run into the edge,
  922. * we deallocate that cursor.
  923. * If both searches succeed, we compare the two spaces and pick
  924. * the better one.
  925. * With alignment, it's possible for both to fail; the upper
  926. * level algorithm that picks allocation groups for allocations
  927. * is not supposed to do this.
  928. */
  929. /*
  930. * Allocate and initialize the cursor for the leftward search.
  931. */
  932. bno_cur_lt = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  933. args->agno, XFS_BTNUM_BNO);
  934. /*
  935. * Lookup <= bno to find the leftward search's starting point.
  936. */
  937. if ((error = xfs_alloc_lookup_le(bno_cur_lt, args->agbno, args->maxlen, &i)))
  938. goto error0;
  939. if (!i) {
  940. /*
  941. * Didn't find anything; use this cursor for the rightward
  942. * search.
  943. */
  944. bno_cur_gt = bno_cur_lt;
  945. bno_cur_lt = NULL;
  946. }
  947. /*
  948. * Found something. Duplicate the cursor for the rightward search.
  949. */
  950. else if ((error = xfs_btree_dup_cursor(bno_cur_lt, &bno_cur_gt)))
  951. goto error0;
  952. /*
  953. * Increment the cursor, so we will point at the entry just right
  954. * of the leftward entry if any, or to the leftmost entry.
  955. */
  956. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  957. goto error0;
  958. if (!i) {
  959. /*
  960. * It failed, there are no rightward entries.
  961. */
  962. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_NOERROR);
  963. bno_cur_gt = NULL;
  964. }
  965. /*
  966. * Loop going left with the leftward cursor, right with the
  967. * rightward cursor, until either both directions give up or
  968. * we find an entry at least as big as minlen.
  969. */
  970. do {
  971. if (bno_cur_lt) {
  972. if ((error = xfs_alloc_get_rec(bno_cur_lt, &ltbno, &ltlen, &i)))
  973. goto error0;
  974. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  975. xfs_alloc_compute_aligned(args, ltbno, ltlen,
  976. &ltbnoa, &ltlena);
  977. if (ltlena >= args->minlen)
  978. break;
  979. if ((error = xfs_btree_decrement(bno_cur_lt, 0, &i)))
  980. goto error0;
  981. if (!i) {
  982. xfs_btree_del_cursor(bno_cur_lt,
  983. XFS_BTREE_NOERROR);
  984. bno_cur_lt = NULL;
  985. }
  986. }
  987. if (bno_cur_gt) {
  988. if ((error = xfs_alloc_get_rec(bno_cur_gt, &gtbno, &gtlen, &i)))
  989. goto error0;
  990. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  991. xfs_alloc_compute_aligned(args, gtbno, gtlen,
  992. &gtbnoa, &gtlena);
  993. if (gtlena >= args->minlen)
  994. break;
  995. if ((error = xfs_btree_increment(bno_cur_gt, 0, &i)))
  996. goto error0;
  997. if (!i) {
  998. xfs_btree_del_cursor(bno_cur_gt,
  999. XFS_BTREE_NOERROR);
  1000. bno_cur_gt = NULL;
  1001. }
  1002. }
  1003. } while (bno_cur_lt || bno_cur_gt);
  1004. /*
  1005. * Got both cursors still active, need to find better entry.
  1006. */
  1007. if (bno_cur_lt && bno_cur_gt) {
  1008. if (ltlena >= args->minlen) {
  1009. /*
  1010. * Left side is good, look for a right side entry.
  1011. */
  1012. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1013. xfs_alloc_fix_len(args);
  1014. ltdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1015. args->alignment, ltbnoa, ltlena, &ltnew);
  1016. error = xfs_alloc_find_best_extent(args,
  1017. &bno_cur_lt, &bno_cur_gt,
  1018. ltdiff, &gtbno, &gtlen,
  1019. &gtbnoa, &gtlena,
  1020. 0 /* search right */);
  1021. } else {
  1022. ASSERT(gtlena >= args->minlen);
  1023. /*
  1024. * Right side is good, look for a left side entry.
  1025. */
  1026. args->len = XFS_EXTLEN_MIN(gtlena, args->maxlen);
  1027. xfs_alloc_fix_len(args);
  1028. gtdiff = xfs_alloc_compute_diff(args->agbno, args->len,
  1029. args->alignment, gtbnoa, gtlena, &gtnew);
  1030. error = xfs_alloc_find_best_extent(args,
  1031. &bno_cur_gt, &bno_cur_lt,
  1032. gtdiff, &ltbno, &ltlen,
  1033. &ltbnoa, &ltlena,
  1034. 1 /* search left */);
  1035. }
  1036. if (error)
  1037. goto error0;
  1038. }
  1039. /*
  1040. * If we couldn't get anything, give up.
  1041. */
  1042. if (bno_cur_lt == NULL && bno_cur_gt == NULL) {
  1043. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1044. if (!forced++) {
  1045. trace_xfs_alloc_near_busy(args);
  1046. xfs_log_force(args->mp, XFS_LOG_SYNC);
  1047. goto restart;
  1048. }
  1049. trace_xfs_alloc_size_neither(args);
  1050. args->agbno = NULLAGBLOCK;
  1051. return 0;
  1052. }
  1053. /*
  1054. * At this point we have selected a freespace entry, either to the
  1055. * left or to the right. If it's on the right, copy all the
  1056. * useful variables to the "left" set so we only have one
  1057. * copy of this code.
  1058. */
  1059. if (bno_cur_gt) {
  1060. bno_cur_lt = bno_cur_gt;
  1061. bno_cur_gt = NULL;
  1062. ltbno = gtbno;
  1063. ltbnoa = gtbnoa;
  1064. ltlen = gtlen;
  1065. ltlena = gtlena;
  1066. j = 1;
  1067. } else
  1068. j = 0;
  1069. /*
  1070. * Fix up the length and compute the useful address.
  1071. */
  1072. args->len = XFS_EXTLEN_MIN(ltlena, args->maxlen);
  1073. xfs_alloc_fix_len(args);
  1074. if (!xfs_alloc_fix_minleft(args)) {
  1075. trace_xfs_alloc_near_nominleft(args);
  1076. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1077. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1078. return 0;
  1079. }
  1080. rlen = args->len;
  1081. (void)xfs_alloc_compute_diff(args->agbno, rlen, args->alignment,
  1082. ltbnoa, ltlena, &ltnew);
  1083. ASSERT(ltnew >= ltbno);
  1084. ASSERT(ltnew + rlen <= ltbnoa + ltlena);
  1085. ASSERT(ltnew + rlen <= be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length));
  1086. args->agbno = ltnew;
  1087. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur_lt, ltbno, ltlen,
  1088. ltnew, rlen, XFSA_FIXUP_BNO_OK)))
  1089. goto error0;
  1090. if (j)
  1091. trace_xfs_alloc_near_greater(args);
  1092. else
  1093. trace_xfs_alloc_near_lesser(args);
  1094. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1095. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_NOERROR);
  1096. return 0;
  1097. error0:
  1098. trace_xfs_alloc_near_error(args);
  1099. if (cnt_cur != NULL)
  1100. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1101. if (bno_cur_lt != NULL)
  1102. xfs_btree_del_cursor(bno_cur_lt, XFS_BTREE_ERROR);
  1103. if (bno_cur_gt != NULL)
  1104. xfs_btree_del_cursor(bno_cur_gt, XFS_BTREE_ERROR);
  1105. return error;
  1106. }
  1107. /*
  1108. * Allocate a variable extent anywhere in the allocation group agno.
  1109. * Extent's length (returned in len) will be between minlen and maxlen,
  1110. * and of the form k * prod + mod unless there's nothing that large.
  1111. * Return the starting a.g. block, or NULLAGBLOCK if we can't do it.
  1112. */
  1113. STATIC int /* error */
  1114. xfs_alloc_ag_vextent_size(
  1115. xfs_alloc_arg_t *args) /* allocation argument structure */
  1116. {
  1117. xfs_btree_cur_t *bno_cur; /* cursor for bno btree */
  1118. xfs_btree_cur_t *cnt_cur; /* cursor for cnt btree */
  1119. int error; /* error result */
  1120. xfs_agblock_t fbno; /* start of found freespace */
  1121. xfs_extlen_t flen; /* length of found freespace */
  1122. int i; /* temp status variable */
  1123. xfs_agblock_t rbno; /* returned block number */
  1124. xfs_extlen_t rlen; /* length of returned extent */
  1125. int forced = 0;
  1126. restart:
  1127. /*
  1128. * Allocate and initialize a cursor for the by-size btree.
  1129. */
  1130. cnt_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1131. args->agno, XFS_BTNUM_CNT);
  1132. bno_cur = NULL;
  1133. /*
  1134. * Look for an entry >= maxlen+alignment-1 blocks.
  1135. */
  1136. if ((error = xfs_alloc_lookup_ge(cnt_cur, 0,
  1137. args->maxlen + args->alignment - 1, &i)))
  1138. goto error0;
  1139. /*
  1140. * If none or we have busy extents that we cannot allocate from, then
  1141. * we have to settle for a smaller extent. In the case that there are
  1142. * no large extents, this will return the last entry in the tree unless
  1143. * the tree is empty. In the case that there are only busy large
  1144. * extents, this will return the largest small extent unless there
  1145. * are no smaller extents available.
  1146. */
  1147. if (!i || forced > 1) {
  1148. error = xfs_alloc_ag_vextent_small(args, cnt_cur,
  1149. &fbno, &flen, &i);
  1150. if (error)
  1151. goto error0;
  1152. if (i == 0 || flen == 0) {
  1153. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1154. trace_xfs_alloc_size_noentry(args);
  1155. return 0;
  1156. }
  1157. ASSERT(i == 1);
  1158. xfs_alloc_compute_aligned(args, fbno, flen, &rbno, &rlen);
  1159. } else {
  1160. /*
  1161. * Search for a non-busy extent that is large enough.
  1162. * If we are at low space, don't check, or if we fall of
  1163. * the end of the btree, turn off the busy check and
  1164. * restart.
  1165. */
  1166. for (;;) {
  1167. error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen, &i);
  1168. if (error)
  1169. goto error0;
  1170. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1171. xfs_alloc_compute_aligned(args, fbno, flen,
  1172. &rbno, &rlen);
  1173. if (rlen >= args->maxlen)
  1174. break;
  1175. error = xfs_btree_increment(cnt_cur, 0, &i);
  1176. if (error)
  1177. goto error0;
  1178. if (i == 0) {
  1179. /*
  1180. * Our only valid extents must have been busy.
  1181. * Make it unbusy by forcing the log out and
  1182. * retrying. If we've been here before, forcing
  1183. * the log isn't making the extents available,
  1184. * which means they have probably been freed in
  1185. * this transaction. In that case, we have to
  1186. * give up on them and we'll attempt a minlen
  1187. * allocation the next time around.
  1188. */
  1189. xfs_btree_del_cursor(cnt_cur,
  1190. XFS_BTREE_NOERROR);
  1191. trace_xfs_alloc_size_busy(args);
  1192. if (!forced++)
  1193. xfs_log_force(args->mp, XFS_LOG_SYNC);
  1194. goto restart;
  1195. }
  1196. }
  1197. }
  1198. /*
  1199. * In the first case above, we got the last entry in the
  1200. * by-size btree. Now we check to see if the space hits maxlen
  1201. * once aligned; if not, we search left for something better.
  1202. * This can't happen in the second case above.
  1203. */
  1204. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1205. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1206. (rlen <= flen && rbno + rlen <= fbno + flen), error0);
  1207. if (rlen < args->maxlen) {
  1208. xfs_agblock_t bestfbno;
  1209. xfs_extlen_t bestflen;
  1210. xfs_agblock_t bestrbno;
  1211. xfs_extlen_t bestrlen;
  1212. bestrlen = rlen;
  1213. bestrbno = rbno;
  1214. bestflen = flen;
  1215. bestfbno = fbno;
  1216. for (;;) {
  1217. if ((error = xfs_btree_decrement(cnt_cur, 0, &i)))
  1218. goto error0;
  1219. if (i == 0)
  1220. break;
  1221. if ((error = xfs_alloc_get_rec(cnt_cur, &fbno, &flen,
  1222. &i)))
  1223. goto error0;
  1224. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1225. if (flen < bestrlen)
  1226. break;
  1227. xfs_alloc_compute_aligned(args, fbno, flen,
  1228. &rbno, &rlen);
  1229. rlen = XFS_EXTLEN_MIN(args->maxlen, rlen);
  1230. XFS_WANT_CORRUPTED_GOTO(rlen == 0 ||
  1231. (rlen <= flen && rbno + rlen <= fbno + flen),
  1232. error0);
  1233. if (rlen > bestrlen) {
  1234. bestrlen = rlen;
  1235. bestrbno = rbno;
  1236. bestflen = flen;
  1237. bestfbno = fbno;
  1238. if (rlen == args->maxlen)
  1239. break;
  1240. }
  1241. }
  1242. if ((error = xfs_alloc_lookup_eq(cnt_cur, bestfbno, bestflen,
  1243. &i)))
  1244. goto error0;
  1245. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1246. rlen = bestrlen;
  1247. rbno = bestrbno;
  1248. flen = bestflen;
  1249. fbno = bestfbno;
  1250. }
  1251. args->wasfromfl = 0;
  1252. /*
  1253. * Fix up the length.
  1254. */
  1255. args->len = rlen;
  1256. if (rlen < args->minlen) {
  1257. if (!forced++) {
  1258. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1259. trace_xfs_alloc_size_busy(args);
  1260. xfs_log_force(args->mp, XFS_LOG_SYNC);
  1261. goto restart;
  1262. }
  1263. goto out_nominleft;
  1264. }
  1265. xfs_alloc_fix_len(args);
  1266. if (!xfs_alloc_fix_minleft(args))
  1267. goto out_nominleft;
  1268. rlen = args->len;
  1269. XFS_WANT_CORRUPTED_GOTO(rlen <= flen, error0);
  1270. /*
  1271. * Allocate and initialize a cursor for the by-block tree.
  1272. */
  1273. bno_cur = xfs_allocbt_init_cursor(args->mp, args->tp, args->agbp,
  1274. args->agno, XFS_BTNUM_BNO);
  1275. if ((error = xfs_alloc_fixup_trees(cnt_cur, bno_cur, fbno, flen,
  1276. rbno, rlen, XFSA_FIXUP_CNT_OK)))
  1277. goto error0;
  1278. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1279. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1280. cnt_cur = bno_cur = NULL;
  1281. args->len = rlen;
  1282. args->agbno = rbno;
  1283. XFS_WANT_CORRUPTED_GOTO(
  1284. args->agbno + args->len <=
  1285. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1286. error0);
  1287. trace_xfs_alloc_size_done(args);
  1288. return 0;
  1289. error0:
  1290. trace_xfs_alloc_size_error(args);
  1291. if (cnt_cur)
  1292. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1293. if (bno_cur)
  1294. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1295. return error;
  1296. out_nominleft:
  1297. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1298. trace_xfs_alloc_size_nominleft(args);
  1299. args->agbno = NULLAGBLOCK;
  1300. return 0;
  1301. }
  1302. /*
  1303. * Deal with the case where only small freespaces remain.
  1304. * Either return the contents of the last freespace record,
  1305. * or allocate space from the freelist if there is nothing in the tree.
  1306. */
  1307. STATIC int /* error */
  1308. xfs_alloc_ag_vextent_small(
  1309. xfs_alloc_arg_t *args, /* allocation argument structure */
  1310. xfs_btree_cur_t *ccur, /* by-size cursor */
  1311. xfs_agblock_t *fbnop, /* result block number */
  1312. xfs_extlen_t *flenp, /* result length */
  1313. int *stat) /* status: 0-freelist, 1-normal/none */
  1314. {
  1315. int error;
  1316. xfs_agblock_t fbno;
  1317. xfs_extlen_t flen;
  1318. int i;
  1319. if ((error = xfs_btree_decrement(ccur, 0, &i)))
  1320. goto error0;
  1321. if (i) {
  1322. if ((error = xfs_alloc_get_rec(ccur, &fbno, &flen, &i)))
  1323. goto error0;
  1324. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1325. }
  1326. /*
  1327. * Nothing in the btree, try the freelist. Make sure
  1328. * to respect minleft even when pulling from the
  1329. * freelist.
  1330. */
  1331. else if (args->minlen == 1 && args->alignment == 1 && !args->isfl &&
  1332. (be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_flcount)
  1333. > args->minleft)) {
  1334. error = xfs_alloc_get_freelist(args->tp, args->agbp, &fbno, 0);
  1335. if (error)
  1336. goto error0;
  1337. if (fbno != NULLAGBLOCK) {
  1338. xfs_extent_busy_reuse(args->mp, args->agno, fbno, 1,
  1339. args->userdata);
  1340. if (args->userdata) {
  1341. xfs_buf_t *bp;
  1342. bp = xfs_btree_get_bufs(args->mp, args->tp,
  1343. args->agno, fbno, 0);
  1344. xfs_trans_binval(args->tp, bp);
  1345. }
  1346. args->len = 1;
  1347. args->agbno = fbno;
  1348. XFS_WANT_CORRUPTED_GOTO(
  1349. args->agbno + args->len <=
  1350. be32_to_cpu(XFS_BUF_TO_AGF(args->agbp)->agf_length),
  1351. error0);
  1352. args->wasfromfl = 1;
  1353. trace_xfs_alloc_small_freelist(args);
  1354. *stat = 0;
  1355. return 0;
  1356. }
  1357. /*
  1358. * Nothing in the freelist.
  1359. */
  1360. else
  1361. flen = 0;
  1362. }
  1363. /*
  1364. * Can't allocate from the freelist for some reason.
  1365. */
  1366. else {
  1367. fbno = NULLAGBLOCK;
  1368. flen = 0;
  1369. }
  1370. /*
  1371. * Can't do the allocation, give up.
  1372. */
  1373. if (flen < args->minlen) {
  1374. args->agbno = NULLAGBLOCK;
  1375. trace_xfs_alloc_small_notenough(args);
  1376. flen = 0;
  1377. }
  1378. *fbnop = fbno;
  1379. *flenp = flen;
  1380. *stat = 1;
  1381. trace_xfs_alloc_small_done(args);
  1382. return 0;
  1383. error0:
  1384. trace_xfs_alloc_small_error(args);
  1385. return error;
  1386. }
  1387. /*
  1388. * Free the extent starting at agno/bno for length.
  1389. */
  1390. STATIC int /* error */
  1391. xfs_free_ag_extent(
  1392. xfs_trans_t *tp, /* transaction pointer */
  1393. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  1394. xfs_agnumber_t agno, /* allocation group number */
  1395. xfs_agblock_t bno, /* starting block number */
  1396. xfs_extlen_t len, /* length of extent */
  1397. int isfl) /* set if is freelist blocks - no sb acctg */
  1398. {
  1399. xfs_btree_cur_t *bno_cur; /* cursor for by-block btree */
  1400. xfs_btree_cur_t *cnt_cur; /* cursor for by-size btree */
  1401. int error; /* error return value */
  1402. xfs_agblock_t gtbno; /* start of right neighbor block */
  1403. xfs_extlen_t gtlen; /* length of right neighbor block */
  1404. int haveleft; /* have a left neighbor block */
  1405. int haveright; /* have a right neighbor block */
  1406. int i; /* temp, result code */
  1407. xfs_agblock_t ltbno; /* start of left neighbor block */
  1408. xfs_extlen_t ltlen; /* length of left neighbor block */
  1409. xfs_mount_t *mp; /* mount point struct for filesystem */
  1410. xfs_agblock_t nbno; /* new starting block of freespace */
  1411. xfs_extlen_t nlen; /* new length of freespace */
  1412. xfs_perag_t *pag; /* per allocation group data */
  1413. mp = tp->t_mountp;
  1414. /*
  1415. * Allocate and initialize a cursor for the by-block btree.
  1416. */
  1417. bno_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_BNO);
  1418. cnt_cur = NULL;
  1419. /*
  1420. * Look for a neighboring block on the left (lower block numbers)
  1421. * that is contiguous with this space.
  1422. */
  1423. if ((error = xfs_alloc_lookup_le(bno_cur, bno, len, &haveleft)))
  1424. goto error0;
  1425. if (haveleft) {
  1426. /*
  1427. * There is a block to our left.
  1428. */
  1429. if ((error = xfs_alloc_get_rec(bno_cur, &ltbno, &ltlen, &i)))
  1430. goto error0;
  1431. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1432. /*
  1433. * It's not contiguous, though.
  1434. */
  1435. if (ltbno + ltlen < bno)
  1436. haveleft = 0;
  1437. else {
  1438. /*
  1439. * If this failure happens the request to free this
  1440. * space was invalid, it's (partly) already free.
  1441. * Very bad.
  1442. */
  1443. XFS_WANT_CORRUPTED_GOTO(ltbno + ltlen <= bno, error0);
  1444. }
  1445. }
  1446. /*
  1447. * Look for a neighboring block on the right (higher block numbers)
  1448. * that is contiguous with this space.
  1449. */
  1450. if ((error = xfs_btree_increment(bno_cur, 0, &haveright)))
  1451. goto error0;
  1452. if (haveright) {
  1453. /*
  1454. * There is a block to our right.
  1455. */
  1456. if ((error = xfs_alloc_get_rec(bno_cur, &gtbno, &gtlen, &i)))
  1457. goto error0;
  1458. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1459. /*
  1460. * It's not contiguous, though.
  1461. */
  1462. if (bno + len < gtbno)
  1463. haveright = 0;
  1464. else {
  1465. /*
  1466. * If this failure happens the request to free this
  1467. * space was invalid, it's (partly) already free.
  1468. * Very bad.
  1469. */
  1470. XFS_WANT_CORRUPTED_GOTO(gtbno >= bno + len, error0);
  1471. }
  1472. }
  1473. /*
  1474. * Now allocate and initialize a cursor for the by-size tree.
  1475. */
  1476. cnt_cur = xfs_allocbt_init_cursor(mp, tp, agbp, agno, XFS_BTNUM_CNT);
  1477. /*
  1478. * Have both left and right contiguous neighbors.
  1479. * Merge all three into a single free block.
  1480. */
  1481. if (haveleft && haveright) {
  1482. /*
  1483. * Delete the old by-size entry on the left.
  1484. */
  1485. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1486. goto error0;
  1487. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1488. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1489. goto error0;
  1490. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1491. /*
  1492. * Delete the old by-size entry on the right.
  1493. */
  1494. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1495. goto error0;
  1496. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1497. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1498. goto error0;
  1499. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1500. /*
  1501. * Delete the old by-block entry for the right block.
  1502. */
  1503. if ((error = xfs_btree_delete(bno_cur, &i)))
  1504. goto error0;
  1505. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1506. /*
  1507. * Move the by-block cursor back to the left neighbor.
  1508. */
  1509. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1510. goto error0;
  1511. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1512. #ifdef DEBUG
  1513. /*
  1514. * Check that this is the right record: delete didn't
  1515. * mangle the cursor.
  1516. */
  1517. {
  1518. xfs_agblock_t xxbno;
  1519. xfs_extlen_t xxlen;
  1520. if ((error = xfs_alloc_get_rec(bno_cur, &xxbno, &xxlen,
  1521. &i)))
  1522. goto error0;
  1523. XFS_WANT_CORRUPTED_GOTO(
  1524. i == 1 && xxbno == ltbno && xxlen == ltlen,
  1525. error0);
  1526. }
  1527. #endif
  1528. /*
  1529. * Update remaining by-block entry to the new, joined block.
  1530. */
  1531. nbno = ltbno;
  1532. nlen = len + ltlen + gtlen;
  1533. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1534. goto error0;
  1535. }
  1536. /*
  1537. * Have only a left contiguous neighbor.
  1538. * Merge it together with the new freespace.
  1539. */
  1540. else if (haveleft) {
  1541. /*
  1542. * Delete the old by-size entry on the left.
  1543. */
  1544. if ((error = xfs_alloc_lookup_eq(cnt_cur, ltbno, ltlen, &i)))
  1545. goto error0;
  1546. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1547. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1548. goto error0;
  1549. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1550. /*
  1551. * Back up the by-block cursor to the left neighbor, and
  1552. * update its length.
  1553. */
  1554. if ((error = xfs_btree_decrement(bno_cur, 0, &i)))
  1555. goto error0;
  1556. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1557. nbno = ltbno;
  1558. nlen = len + ltlen;
  1559. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1560. goto error0;
  1561. }
  1562. /*
  1563. * Have only a right contiguous neighbor.
  1564. * Merge it together with the new freespace.
  1565. */
  1566. else if (haveright) {
  1567. /*
  1568. * Delete the old by-size entry on the right.
  1569. */
  1570. if ((error = xfs_alloc_lookup_eq(cnt_cur, gtbno, gtlen, &i)))
  1571. goto error0;
  1572. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1573. if ((error = xfs_btree_delete(cnt_cur, &i)))
  1574. goto error0;
  1575. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1576. /*
  1577. * Update the starting block and length of the right
  1578. * neighbor in the by-block tree.
  1579. */
  1580. nbno = bno;
  1581. nlen = len + gtlen;
  1582. if ((error = xfs_alloc_update(bno_cur, nbno, nlen)))
  1583. goto error0;
  1584. }
  1585. /*
  1586. * No contiguous neighbors.
  1587. * Insert the new freespace into the by-block tree.
  1588. */
  1589. else {
  1590. nbno = bno;
  1591. nlen = len;
  1592. if ((error = xfs_btree_insert(bno_cur, &i)))
  1593. goto error0;
  1594. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1595. }
  1596. xfs_btree_del_cursor(bno_cur, XFS_BTREE_NOERROR);
  1597. bno_cur = NULL;
  1598. /*
  1599. * In all cases we need to insert the new freespace in the by-size tree.
  1600. */
  1601. if ((error = xfs_alloc_lookup_eq(cnt_cur, nbno, nlen, &i)))
  1602. goto error0;
  1603. XFS_WANT_CORRUPTED_GOTO(i == 0, error0);
  1604. if ((error = xfs_btree_insert(cnt_cur, &i)))
  1605. goto error0;
  1606. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1607. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_NOERROR);
  1608. cnt_cur = NULL;
  1609. /*
  1610. * Update the freespace totals in the ag and superblock.
  1611. */
  1612. pag = xfs_perag_get(mp, agno);
  1613. error = xfs_alloc_update_counters(tp, pag, agbp, len);
  1614. xfs_perag_put(pag);
  1615. if (error)
  1616. goto error0;
  1617. if (!isfl)
  1618. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (long)len);
  1619. XFS_STATS_INC(xs_freex);
  1620. XFS_STATS_ADD(xs_freeb, len);
  1621. trace_xfs_free_extent(mp, agno, bno, len, isfl, haveleft, haveright);
  1622. return 0;
  1623. error0:
  1624. trace_xfs_free_extent(mp, agno, bno, len, isfl, -1, -1);
  1625. if (bno_cur)
  1626. xfs_btree_del_cursor(bno_cur, XFS_BTREE_ERROR);
  1627. if (cnt_cur)
  1628. xfs_btree_del_cursor(cnt_cur, XFS_BTREE_ERROR);
  1629. return error;
  1630. }
  1631. /*
  1632. * Visible (exported) allocation/free functions.
  1633. * Some of these are used just by xfs_alloc_btree.c and this file.
  1634. */
  1635. /*
  1636. * Compute and fill in value of m_ag_maxlevels.
  1637. */
  1638. void
  1639. xfs_alloc_compute_maxlevels(
  1640. xfs_mount_t *mp) /* file system mount structure */
  1641. {
  1642. int level;
  1643. uint maxblocks;
  1644. uint maxleafents;
  1645. int minleafrecs;
  1646. int minnoderecs;
  1647. maxleafents = (mp->m_sb.sb_agblocks + 1) / 2;
  1648. minleafrecs = mp->m_alloc_mnr[0];
  1649. minnoderecs = mp->m_alloc_mnr[1];
  1650. maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
  1651. for (level = 1; maxblocks > 1; level++)
  1652. maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
  1653. mp->m_ag_maxlevels = level;
  1654. }
  1655. /*
  1656. * Find the length of the longest extent in an AG.
  1657. */
  1658. xfs_extlen_t
  1659. xfs_alloc_longest_free_extent(
  1660. struct xfs_mount *mp,
  1661. struct xfs_perag *pag)
  1662. {
  1663. xfs_extlen_t need, delta = 0;
  1664. need = XFS_MIN_FREELIST_PAG(pag, mp);
  1665. if (need > pag->pagf_flcount)
  1666. delta = need - pag->pagf_flcount;
  1667. if (pag->pagf_longest > delta)
  1668. return pag->pagf_longest - delta;
  1669. return pag->pagf_flcount > 0 || pag->pagf_longest > 0;
  1670. }
  1671. /*
  1672. * Decide whether to use this allocation group for this allocation.
  1673. * If so, fix up the btree freelist's size.
  1674. */
  1675. STATIC int /* error */
  1676. xfs_alloc_fix_freelist(
  1677. xfs_alloc_arg_t *args, /* allocation argument structure */
  1678. int flags) /* XFS_ALLOC_FLAG_... */
  1679. {
  1680. xfs_buf_t *agbp; /* agf buffer pointer */
  1681. xfs_agf_t *agf; /* a.g. freespace structure pointer */
  1682. xfs_buf_t *agflbp;/* agfl buffer pointer */
  1683. xfs_agblock_t bno; /* freelist block */
  1684. xfs_extlen_t delta; /* new blocks needed in freelist */
  1685. int error; /* error result code */
  1686. xfs_extlen_t longest;/* longest extent in allocation group */
  1687. xfs_mount_t *mp; /* file system mount point structure */
  1688. xfs_extlen_t need; /* total blocks needed in freelist */
  1689. xfs_perag_t *pag; /* per-ag information structure */
  1690. xfs_alloc_arg_t targs; /* local allocation arguments */
  1691. xfs_trans_t *tp; /* transaction pointer */
  1692. mp = args->mp;
  1693. pag = args->pag;
  1694. tp = args->tp;
  1695. if (!pag->pagf_init) {
  1696. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1697. &agbp)))
  1698. return error;
  1699. if (!pag->pagf_init) {
  1700. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  1701. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1702. args->agbp = NULL;
  1703. return 0;
  1704. }
  1705. } else
  1706. agbp = NULL;
  1707. /*
  1708. * If this is a metadata preferred pag and we are user data
  1709. * then try somewhere else if we are not being asked to
  1710. * try harder at this point
  1711. */
  1712. if (pag->pagf_metadata && args->userdata &&
  1713. (flags & XFS_ALLOC_FLAG_TRYLOCK)) {
  1714. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1715. args->agbp = NULL;
  1716. return 0;
  1717. }
  1718. if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
  1719. /*
  1720. * If it looks like there isn't a long enough extent, or enough
  1721. * total blocks, reject it.
  1722. */
  1723. need = XFS_MIN_FREELIST_PAG(pag, mp);
  1724. longest = xfs_alloc_longest_free_extent(mp, pag);
  1725. if ((args->minlen + args->alignment + args->minalignslop - 1) >
  1726. longest ||
  1727. ((int)(pag->pagf_freeblks + pag->pagf_flcount -
  1728. need - args->total) < (int)args->minleft)) {
  1729. if (agbp)
  1730. xfs_trans_brelse(tp, agbp);
  1731. args->agbp = NULL;
  1732. return 0;
  1733. }
  1734. }
  1735. /*
  1736. * Get the a.g. freespace buffer.
  1737. * Can fail if we're not blocking on locks, and it's held.
  1738. */
  1739. if (agbp == NULL) {
  1740. if ((error = xfs_alloc_read_agf(mp, tp, args->agno, flags,
  1741. &agbp)))
  1742. return error;
  1743. if (agbp == NULL) {
  1744. ASSERT(flags & XFS_ALLOC_FLAG_TRYLOCK);
  1745. ASSERT(!(flags & XFS_ALLOC_FLAG_FREEING));
  1746. args->agbp = NULL;
  1747. return 0;
  1748. }
  1749. }
  1750. /*
  1751. * Figure out how many blocks we should have in the freelist.
  1752. */
  1753. agf = XFS_BUF_TO_AGF(agbp);
  1754. need = XFS_MIN_FREELIST(agf, mp);
  1755. /*
  1756. * If there isn't enough total or single-extent, reject it.
  1757. */
  1758. if (!(flags & XFS_ALLOC_FLAG_FREEING)) {
  1759. delta = need > be32_to_cpu(agf->agf_flcount) ?
  1760. (need - be32_to_cpu(agf->agf_flcount)) : 0;
  1761. longest = be32_to_cpu(agf->agf_longest);
  1762. longest = (longest > delta) ? (longest - delta) :
  1763. (be32_to_cpu(agf->agf_flcount) > 0 || longest > 0);
  1764. if ((args->minlen + args->alignment + args->minalignslop - 1) >
  1765. longest ||
  1766. ((int)(be32_to_cpu(agf->agf_freeblks) +
  1767. be32_to_cpu(agf->agf_flcount) - need - args->total) <
  1768. (int)args->minleft)) {
  1769. xfs_trans_brelse(tp, agbp);
  1770. args->agbp = NULL;
  1771. return 0;
  1772. }
  1773. }
  1774. /*
  1775. * Make the freelist shorter if it's too long.
  1776. */
  1777. while (be32_to_cpu(agf->agf_flcount) > need) {
  1778. xfs_buf_t *bp;
  1779. error = xfs_alloc_get_freelist(tp, agbp, &bno, 0);
  1780. if (error)
  1781. return error;
  1782. if ((error = xfs_free_ag_extent(tp, agbp, args->agno, bno, 1, 1)))
  1783. return error;
  1784. bp = xfs_btree_get_bufs(mp, tp, args->agno, bno, 0);
  1785. xfs_trans_binval(tp, bp);
  1786. }
  1787. /*
  1788. * Initialize the args structure.
  1789. */
  1790. memset(&targs, 0, sizeof(targs));
  1791. targs.tp = tp;
  1792. targs.mp = mp;
  1793. targs.agbp = agbp;
  1794. targs.agno = args->agno;
  1795. targs.mod = targs.minleft = targs.wasdel = targs.userdata =
  1796. targs.minalignslop = 0;
  1797. targs.alignment = targs.minlen = targs.prod = targs.isfl = 1;
  1798. targs.type = XFS_ALLOCTYPE_THIS_AG;
  1799. targs.pag = pag;
  1800. if ((error = xfs_alloc_read_agfl(mp, tp, targs.agno, &agflbp)))
  1801. return error;
  1802. /*
  1803. * Make the freelist longer if it's too short.
  1804. */
  1805. while (be32_to_cpu(agf->agf_flcount) < need) {
  1806. targs.agbno = 0;
  1807. targs.maxlen = need - be32_to_cpu(agf->agf_flcount);
  1808. /*
  1809. * Allocate as many blocks as possible at once.
  1810. */
  1811. if ((error = xfs_alloc_ag_vextent(&targs))) {
  1812. xfs_trans_brelse(tp, agflbp);
  1813. return error;
  1814. }
  1815. /*
  1816. * Stop if we run out. Won't happen if callers are obeying
  1817. * the restrictions correctly. Can happen for free calls
  1818. * on a completely full ag.
  1819. */
  1820. if (targs.agbno == NULLAGBLOCK) {
  1821. if (flags & XFS_ALLOC_FLAG_FREEING)
  1822. break;
  1823. xfs_trans_brelse(tp, agflbp);
  1824. args->agbp = NULL;
  1825. return 0;
  1826. }
  1827. /*
  1828. * Put each allocated block on the list.
  1829. */
  1830. for (bno = targs.agbno; bno < targs.agbno + targs.len; bno++) {
  1831. error = xfs_alloc_put_freelist(tp, agbp,
  1832. agflbp, bno, 0);
  1833. if (error)
  1834. return error;
  1835. }
  1836. }
  1837. xfs_trans_brelse(tp, agflbp);
  1838. args->agbp = agbp;
  1839. return 0;
  1840. }
  1841. /*
  1842. * Get a block from the freelist.
  1843. * Returns with the buffer for the block gotten.
  1844. */
  1845. int /* error */
  1846. xfs_alloc_get_freelist(
  1847. xfs_trans_t *tp, /* transaction pointer */
  1848. xfs_buf_t *agbp, /* buffer containing the agf structure */
  1849. xfs_agblock_t *bnop, /* block address retrieved from freelist */
  1850. int btreeblk) /* destination is a AGF btree */
  1851. {
  1852. xfs_agf_t *agf; /* a.g. freespace structure */
  1853. xfs_agfl_t *agfl; /* a.g. freelist structure */
  1854. xfs_buf_t *agflbp;/* buffer for a.g. freelist structure */
  1855. xfs_agblock_t bno; /* block number returned */
  1856. int error;
  1857. int logflags;
  1858. xfs_mount_t *mp; /* mount structure */
  1859. xfs_perag_t *pag; /* per allocation group data */
  1860. agf = XFS_BUF_TO_AGF(agbp);
  1861. /*
  1862. * Freelist is empty, give up.
  1863. */
  1864. if (!agf->agf_flcount) {
  1865. *bnop = NULLAGBLOCK;
  1866. return 0;
  1867. }
  1868. /*
  1869. * Read the array of free blocks.
  1870. */
  1871. mp = tp->t_mountp;
  1872. if ((error = xfs_alloc_read_agfl(mp, tp,
  1873. be32_to_cpu(agf->agf_seqno), &agflbp)))
  1874. return error;
  1875. agfl = XFS_BUF_TO_AGFL(agflbp);
  1876. /*
  1877. * Get the block number and update the data structures.
  1878. */
  1879. bno = be32_to_cpu(agfl->agfl_bno[be32_to_cpu(agf->agf_flfirst)]);
  1880. be32_add_cpu(&agf->agf_flfirst, 1);
  1881. xfs_trans_brelse(tp, agflbp);
  1882. if (be32_to_cpu(agf->agf_flfirst) == XFS_AGFL_SIZE(mp))
  1883. agf->agf_flfirst = 0;
  1884. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  1885. be32_add_cpu(&agf->agf_flcount, -1);
  1886. xfs_trans_agflist_delta(tp, -1);
  1887. pag->pagf_flcount--;
  1888. xfs_perag_put(pag);
  1889. logflags = XFS_AGF_FLFIRST | XFS_AGF_FLCOUNT;
  1890. if (btreeblk) {
  1891. be32_add_cpu(&agf->agf_btreeblks, 1);
  1892. pag->pagf_btreeblks++;
  1893. logflags |= XFS_AGF_BTREEBLKS;
  1894. }
  1895. xfs_alloc_log_agf(tp, agbp, logflags);
  1896. *bnop = bno;
  1897. return 0;
  1898. }
  1899. /*
  1900. * Log the given fields from the agf structure.
  1901. */
  1902. void
  1903. xfs_alloc_log_agf(
  1904. xfs_trans_t *tp, /* transaction pointer */
  1905. xfs_buf_t *bp, /* buffer for a.g. freelist header */
  1906. int fields) /* mask of fields to be logged (XFS_AGF_...) */
  1907. {
  1908. int first; /* first byte offset */
  1909. int last; /* last byte offset */
  1910. static const short offsets[] = {
  1911. offsetof(xfs_agf_t, agf_magicnum),
  1912. offsetof(xfs_agf_t, agf_versionnum),
  1913. offsetof(xfs_agf_t, agf_seqno),
  1914. offsetof(xfs_agf_t, agf_length),
  1915. offsetof(xfs_agf_t, agf_roots[0]),
  1916. offsetof(xfs_agf_t, agf_levels[0]),
  1917. offsetof(xfs_agf_t, agf_flfirst),
  1918. offsetof(xfs_agf_t, agf_fllast),
  1919. offsetof(xfs_agf_t, agf_flcount),
  1920. offsetof(xfs_agf_t, agf_freeblks),
  1921. offsetof(xfs_agf_t, agf_longest),
  1922. offsetof(xfs_agf_t, agf_btreeblks),
  1923. sizeof(xfs_agf_t)
  1924. };
  1925. trace_xfs_agf(tp->t_mountp, XFS_BUF_TO_AGF(bp), fields, _RET_IP_);
  1926. xfs_btree_offsets(fields, offsets, XFS_AGF_NUM_BITS, &first, &last);
  1927. xfs_trans_log_buf(tp, bp, (uint)first, (uint)last);
  1928. }
  1929. /*
  1930. * Interface for inode allocation to force the pag data to be initialized.
  1931. */
  1932. int /* error */
  1933. xfs_alloc_pagf_init(
  1934. xfs_mount_t *mp, /* file system mount structure */
  1935. xfs_trans_t *tp, /* transaction pointer */
  1936. xfs_agnumber_t agno, /* allocation group number */
  1937. int flags) /* XFS_ALLOC_FLAGS_... */
  1938. {
  1939. xfs_buf_t *bp;
  1940. int error;
  1941. if ((error = xfs_alloc_read_agf(mp, tp, agno, flags, &bp)))
  1942. return error;
  1943. if (bp)
  1944. xfs_trans_brelse(tp, bp);
  1945. return 0;
  1946. }
  1947. /*
  1948. * Put the block on the freelist for the allocation group.
  1949. */
  1950. int /* error */
  1951. xfs_alloc_put_freelist(
  1952. xfs_trans_t *tp, /* transaction pointer */
  1953. xfs_buf_t *agbp, /* buffer for a.g. freelist header */
  1954. xfs_buf_t *agflbp,/* buffer for a.g. free block array */
  1955. xfs_agblock_t bno, /* block being freed */
  1956. int btreeblk) /* block came from a AGF btree */
  1957. {
  1958. xfs_agf_t *agf; /* a.g. freespace structure */
  1959. xfs_agfl_t *agfl; /* a.g. free block array */
  1960. __be32 *blockp;/* pointer to array entry */
  1961. int error;
  1962. int logflags;
  1963. xfs_mount_t *mp; /* mount structure */
  1964. xfs_perag_t *pag; /* per allocation group data */
  1965. agf = XFS_BUF_TO_AGF(agbp);
  1966. mp = tp->t_mountp;
  1967. if (!agflbp && (error = xfs_alloc_read_agfl(mp, tp,
  1968. be32_to_cpu(agf->agf_seqno), &agflbp)))
  1969. return error;
  1970. agfl = XFS_BUF_TO_AGFL(agflbp);
  1971. be32_add_cpu(&agf->agf_fllast, 1);
  1972. if (be32_to_cpu(agf->agf_fllast) == XFS_AGFL_SIZE(mp))
  1973. agf->agf_fllast = 0;
  1974. pag = xfs_perag_get(mp, be32_to_cpu(agf->agf_seqno));
  1975. be32_add_cpu(&agf->agf_flcount, 1);
  1976. xfs_trans_agflist_delta(tp, 1);
  1977. pag->pagf_flcount++;
  1978. logflags = XFS_AGF_FLLAST | XFS_AGF_FLCOUNT;
  1979. if (btreeblk) {
  1980. be32_add_cpu(&agf->agf_btreeblks, -1);
  1981. pag->pagf_btreeblks--;
  1982. logflags |= XFS_AGF_BTREEBLKS;
  1983. }
  1984. xfs_perag_put(pag);
  1985. xfs_alloc_log_agf(tp, agbp, logflags);
  1986. ASSERT(be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp));
  1987. blockp = &agfl->agfl_bno[be32_to_cpu(agf->agf_fllast)];
  1988. *blockp = cpu_to_be32(bno);
  1989. xfs_alloc_log_agf(tp, agbp, logflags);
  1990. xfs_trans_log_buf(tp, agflbp,
  1991. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl),
  1992. (int)((xfs_caddr_t)blockp - (xfs_caddr_t)agfl +
  1993. sizeof(xfs_agblock_t) - 1));
  1994. return 0;
  1995. }
  1996. static void
  1997. xfs_agf_verify(
  1998. struct xfs_buf *bp)
  1999. {
  2000. struct xfs_mount *mp = bp->b_target->bt_mount;
  2001. struct xfs_agf *agf;
  2002. int agf_ok;
  2003. agf = XFS_BUF_TO_AGF(bp);
  2004. agf_ok = agf->agf_magicnum == cpu_to_be32(XFS_AGF_MAGIC) &&
  2005. XFS_AGF_GOOD_VERSION(be32_to_cpu(agf->agf_versionnum)) &&
  2006. be32_to_cpu(agf->agf_freeblks) <= be32_to_cpu(agf->agf_length) &&
  2007. be32_to_cpu(agf->agf_flfirst) < XFS_AGFL_SIZE(mp) &&
  2008. be32_to_cpu(agf->agf_fllast) < XFS_AGFL_SIZE(mp) &&
  2009. be32_to_cpu(agf->agf_flcount) <= XFS_AGFL_SIZE(mp);
  2010. /*
  2011. * during growfs operations, the perag is not fully initialised,
  2012. * so we can't use it for any useful checking. growfs ensures we can't
  2013. * use it by using uncached buffers that don't have the perag attached
  2014. * so we can detect and avoid this problem.
  2015. */
  2016. if (bp->b_pag)
  2017. agf_ok = agf_ok && be32_to_cpu(agf->agf_seqno) ==
  2018. bp->b_pag->pag_agno;
  2019. if (xfs_sb_version_haslazysbcount(&mp->m_sb))
  2020. agf_ok = agf_ok && be32_to_cpu(agf->agf_btreeblks) <=
  2021. be32_to_cpu(agf->agf_length);
  2022. if (unlikely(XFS_TEST_ERROR(!agf_ok, mp, XFS_ERRTAG_ALLOC_READ_AGF,
  2023. XFS_RANDOM_ALLOC_READ_AGF))) {
  2024. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, agf);
  2025. xfs_buf_ioerror(bp, EFSCORRUPTED);
  2026. }
  2027. }
  2028. static void
  2029. xfs_agf_read_verify(
  2030. struct xfs_buf *bp)
  2031. {
  2032. xfs_agf_verify(bp);
  2033. }
  2034. static void
  2035. xfs_agf_write_verify(
  2036. struct xfs_buf *bp)
  2037. {
  2038. xfs_agf_verify(bp);
  2039. }
  2040. const struct xfs_buf_ops xfs_agf_buf_ops = {
  2041. .verify_read = xfs_agf_read_verify,
  2042. .verify_write = xfs_agf_write_verify,
  2043. };
  2044. /*
  2045. * Read in the allocation group header (free/alloc section).
  2046. */
  2047. int /* error */
  2048. xfs_read_agf(
  2049. struct xfs_mount *mp, /* mount point structure */
  2050. struct xfs_trans *tp, /* transaction pointer */
  2051. xfs_agnumber_t agno, /* allocation group number */
  2052. int flags, /* XFS_BUF_ */
  2053. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2054. {
  2055. int error;
  2056. ASSERT(agno != NULLAGNUMBER);
  2057. error = xfs_trans_read_buf(
  2058. mp, tp, mp->m_ddev_targp,
  2059. XFS_AG_DADDR(mp, agno, XFS_AGF_DADDR(mp)),
  2060. XFS_FSS_TO_BB(mp, 1), flags, bpp, &xfs_agf_buf_ops);
  2061. if (error)
  2062. return error;
  2063. if (!*bpp)
  2064. return 0;
  2065. ASSERT(!(*bpp)->b_error);
  2066. xfs_buf_set_ref(*bpp, XFS_AGF_REF);
  2067. return 0;
  2068. }
  2069. /*
  2070. * Read in the allocation group header (free/alloc section).
  2071. */
  2072. int /* error */
  2073. xfs_alloc_read_agf(
  2074. struct xfs_mount *mp, /* mount point structure */
  2075. struct xfs_trans *tp, /* transaction pointer */
  2076. xfs_agnumber_t agno, /* allocation group number */
  2077. int flags, /* XFS_ALLOC_FLAG_... */
  2078. struct xfs_buf **bpp) /* buffer for the ag freelist header */
  2079. {
  2080. struct xfs_agf *agf; /* ag freelist header */
  2081. struct xfs_perag *pag; /* per allocation group data */
  2082. int error;
  2083. ASSERT(agno != NULLAGNUMBER);
  2084. error = xfs_read_agf(mp, tp, agno,
  2085. (flags & XFS_ALLOC_FLAG_TRYLOCK) ? XBF_TRYLOCK : 0,
  2086. bpp);
  2087. if (error)
  2088. return error;
  2089. if (!*bpp)
  2090. return 0;
  2091. ASSERT(!(*bpp)->b_error);
  2092. agf = XFS_BUF_TO_AGF(*bpp);
  2093. pag = xfs_perag_get(mp, agno);
  2094. if (!pag->pagf_init) {
  2095. pag->pagf_freeblks = be32_to_cpu(agf->agf_freeblks);
  2096. pag->pagf_btreeblks = be32_to_cpu(agf->agf_btreeblks);
  2097. pag->pagf_flcount = be32_to_cpu(agf->agf_flcount);
  2098. pag->pagf_longest = be32_to_cpu(agf->agf_longest);
  2099. pag->pagf_levels[XFS_BTNUM_BNOi] =
  2100. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]);
  2101. pag->pagf_levels[XFS_BTNUM_CNTi] =
  2102. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]);
  2103. spin_lock_init(&pag->pagb_lock);
  2104. pag->pagb_count = 0;
  2105. pag->pagb_tree = RB_ROOT;
  2106. pag->pagf_init = 1;
  2107. }
  2108. #ifdef DEBUG
  2109. else if (!XFS_FORCED_SHUTDOWN(mp)) {
  2110. ASSERT(pag->pagf_freeblks == be32_to_cpu(agf->agf_freeblks));
  2111. ASSERT(pag->pagf_btreeblks == be32_to_cpu(agf->agf_btreeblks));
  2112. ASSERT(pag->pagf_flcount == be32_to_cpu(agf->agf_flcount));
  2113. ASSERT(pag->pagf_longest == be32_to_cpu(agf->agf_longest));
  2114. ASSERT(pag->pagf_levels[XFS_BTNUM_BNOi] ==
  2115. be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]));
  2116. ASSERT(pag->pagf_levels[XFS_BTNUM_CNTi] ==
  2117. be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
  2118. }
  2119. #endif
  2120. xfs_perag_put(pag);
  2121. return 0;
  2122. }
  2123. /*
  2124. * Allocate an extent (variable-size).
  2125. * Depending on the allocation type, we either look in a single allocation
  2126. * group or loop over the allocation groups to find the result.
  2127. */
  2128. int /* error */
  2129. xfs_alloc_vextent(
  2130. xfs_alloc_arg_t *args) /* allocation argument structure */
  2131. {
  2132. xfs_agblock_t agsize; /* allocation group size */
  2133. int error;
  2134. int flags; /* XFS_ALLOC_FLAG_... locking flags */
  2135. xfs_extlen_t minleft;/* minimum left value, temp copy */
  2136. xfs_mount_t *mp; /* mount structure pointer */
  2137. xfs_agnumber_t sagno; /* starting allocation group number */
  2138. xfs_alloctype_t type; /* input allocation type */
  2139. int bump_rotor = 0;
  2140. int no_min = 0;
  2141. xfs_agnumber_t rotorstep = xfs_rotorstep; /* inode32 agf stepper */
  2142. mp = args->mp;
  2143. type = args->otype = args->type;
  2144. args->agbno = NULLAGBLOCK;
  2145. /*
  2146. * Just fix this up, for the case where the last a.g. is shorter
  2147. * (or there's only one a.g.) and the caller couldn't easily figure
  2148. * that out (xfs_bmap_alloc).
  2149. */
  2150. agsize = mp->m_sb.sb_agblocks;
  2151. if (args->maxlen > agsize)
  2152. args->maxlen = agsize;
  2153. if (args->alignment == 0)
  2154. args->alignment = 1;
  2155. ASSERT(XFS_FSB_TO_AGNO(mp, args->fsbno) < mp->m_sb.sb_agcount);
  2156. ASSERT(XFS_FSB_TO_AGBNO(mp, args->fsbno) < agsize);
  2157. ASSERT(args->minlen <= args->maxlen);
  2158. ASSERT(args->minlen <= agsize);
  2159. ASSERT(args->mod < args->prod);
  2160. if (XFS_FSB_TO_AGNO(mp, args->fsbno) >= mp->m_sb.sb_agcount ||
  2161. XFS_FSB_TO_AGBNO(mp, args->fsbno) >= agsize ||
  2162. args->minlen > args->maxlen || args->minlen > agsize ||
  2163. args->mod >= args->prod) {
  2164. args->fsbno = NULLFSBLOCK;
  2165. trace_xfs_alloc_vextent_badargs(args);
  2166. return 0;
  2167. }
  2168. minleft = args->minleft;
  2169. switch (type) {
  2170. case XFS_ALLOCTYPE_THIS_AG:
  2171. case XFS_ALLOCTYPE_NEAR_BNO:
  2172. case XFS_ALLOCTYPE_THIS_BNO:
  2173. /*
  2174. * These three force us into a single a.g.
  2175. */
  2176. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2177. args->pag = xfs_perag_get(mp, args->agno);
  2178. args->minleft = 0;
  2179. error = xfs_alloc_fix_freelist(args, 0);
  2180. args->minleft = minleft;
  2181. if (error) {
  2182. trace_xfs_alloc_vextent_nofix(args);
  2183. goto error0;
  2184. }
  2185. if (!args->agbp) {
  2186. trace_xfs_alloc_vextent_noagbp(args);
  2187. break;
  2188. }
  2189. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2190. if ((error = xfs_alloc_ag_vextent(args)))
  2191. goto error0;
  2192. break;
  2193. case XFS_ALLOCTYPE_START_BNO:
  2194. /*
  2195. * Try near allocation first, then anywhere-in-ag after
  2196. * the first a.g. fails.
  2197. */
  2198. if ((args->userdata == XFS_ALLOC_INITIAL_USER_DATA) &&
  2199. (mp->m_flags & XFS_MOUNT_32BITINODES)) {
  2200. args->fsbno = XFS_AGB_TO_FSB(mp,
  2201. ((mp->m_agfrotor / rotorstep) %
  2202. mp->m_sb.sb_agcount), 0);
  2203. bump_rotor = 1;
  2204. }
  2205. args->agbno = XFS_FSB_TO_AGBNO(mp, args->fsbno);
  2206. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2207. /* FALLTHROUGH */
  2208. case XFS_ALLOCTYPE_ANY_AG:
  2209. case XFS_ALLOCTYPE_START_AG:
  2210. case XFS_ALLOCTYPE_FIRST_AG:
  2211. /*
  2212. * Rotate through the allocation groups looking for a winner.
  2213. */
  2214. if (type == XFS_ALLOCTYPE_ANY_AG) {
  2215. /*
  2216. * Start with the last place we left off.
  2217. */
  2218. args->agno = sagno = (mp->m_agfrotor / rotorstep) %
  2219. mp->m_sb.sb_agcount;
  2220. args->type = XFS_ALLOCTYPE_THIS_AG;
  2221. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2222. } else if (type == XFS_ALLOCTYPE_FIRST_AG) {
  2223. /*
  2224. * Start with allocation group given by bno.
  2225. */
  2226. args->agno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2227. args->type = XFS_ALLOCTYPE_THIS_AG;
  2228. sagno = 0;
  2229. flags = 0;
  2230. } else {
  2231. if (type == XFS_ALLOCTYPE_START_AG)
  2232. args->type = XFS_ALLOCTYPE_THIS_AG;
  2233. /*
  2234. * Start with the given allocation group.
  2235. */
  2236. args->agno = sagno = XFS_FSB_TO_AGNO(mp, args->fsbno);
  2237. flags = XFS_ALLOC_FLAG_TRYLOCK;
  2238. }
  2239. /*
  2240. * Loop over allocation groups twice; first time with
  2241. * trylock set, second time without.
  2242. */
  2243. for (;;) {
  2244. args->pag = xfs_perag_get(mp, args->agno);
  2245. if (no_min) args->minleft = 0;
  2246. error = xfs_alloc_fix_freelist(args, flags);
  2247. args->minleft = minleft;
  2248. if (error) {
  2249. trace_xfs_alloc_vextent_nofix(args);
  2250. goto error0;
  2251. }
  2252. /*
  2253. * If we get a buffer back then the allocation will fly.
  2254. */
  2255. if (args->agbp) {
  2256. if ((error = xfs_alloc_ag_vextent(args)))
  2257. goto error0;
  2258. break;
  2259. }
  2260. trace_xfs_alloc_vextent_loopfailed(args);
  2261. /*
  2262. * Didn't work, figure out the next iteration.
  2263. */
  2264. if (args->agno == sagno &&
  2265. type == XFS_ALLOCTYPE_START_BNO)
  2266. args->type = XFS_ALLOCTYPE_THIS_AG;
  2267. /*
  2268. * For the first allocation, we can try any AG to get
  2269. * space. However, if we already have allocated a
  2270. * block, we don't want to try AGs whose number is below
  2271. * sagno. Otherwise, we may end up with out-of-order
  2272. * locking of AGF, which might cause deadlock.
  2273. */
  2274. if (++(args->agno) == mp->m_sb.sb_agcount) {
  2275. if (args->firstblock != NULLFSBLOCK)
  2276. args->agno = sagno;
  2277. else
  2278. args->agno = 0;
  2279. }
  2280. /*
  2281. * Reached the starting a.g., must either be done
  2282. * or switch to non-trylock mode.
  2283. */
  2284. if (args->agno == sagno) {
  2285. if (no_min == 1) {
  2286. args->agbno = NULLAGBLOCK;
  2287. trace_xfs_alloc_vextent_allfailed(args);
  2288. break;
  2289. }
  2290. if (flags == 0) {
  2291. no_min = 1;
  2292. } else {
  2293. flags = 0;
  2294. if (type == XFS_ALLOCTYPE_START_BNO) {
  2295. args->agbno = XFS_FSB_TO_AGBNO(mp,
  2296. args->fsbno);
  2297. args->type = XFS_ALLOCTYPE_NEAR_BNO;
  2298. }
  2299. }
  2300. }
  2301. xfs_perag_put(args->pag);
  2302. }
  2303. if (bump_rotor || (type == XFS_ALLOCTYPE_ANY_AG)) {
  2304. if (args->agno == sagno)
  2305. mp->m_agfrotor = (mp->m_agfrotor + 1) %
  2306. (mp->m_sb.sb_agcount * rotorstep);
  2307. else
  2308. mp->m_agfrotor = (args->agno * rotorstep + 1) %
  2309. (mp->m_sb.sb_agcount * rotorstep);
  2310. }
  2311. break;
  2312. default:
  2313. ASSERT(0);
  2314. /* NOTREACHED */
  2315. }
  2316. if (args->agbno == NULLAGBLOCK)
  2317. args->fsbno = NULLFSBLOCK;
  2318. else {
  2319. args->fsbno = XFS_AGB_TO_FSB(mp, args->agno, args->agbno);
  2320. #ifdef DEBUG
  2321. ASSERT(args->len >= args->minlen);
  2322. ASSERT(args->len <= args->maxlen);
  2323. ASSERT(args->agbno % args->alignment == 0);
  2324. XFS_AG_CHECK_DADDR(mp, XFS_FSB_TO_DADDR(mp, args->fsbno),
  2325. args->len);
  2326. #endif
  2327. }
  2328. xfs_perag_put(args->pag);
  2329. return 0;
  2330. error0:
  2331. xfs_perag_put(args->pag);
  2332. return error;
  2333. }
  2334. /*
  2335. * Free an extent.
  2336. * Just break up the extent address and hand off to xfs_free_ag_extent
  2337. * after fixing up the freelist.
  2338. */
  2339. int /* error */
  2340. xfs_free_extent(
  2341. xfs_trans_t *tp, /* transaction pointer */
  2342. xfs_fsblock_t bno, /* starting block number of extent */
  2343. xfs_extlen_t len) /* length of extent */
  2344. {
  2345. xfs_alloc_arg_t args;
  2346. int error;
  2347. ASSERT(len != 0);
  2348. memset(&args, 0, sizeof(xfs_alloc_arg_t));
  2349. args.tp = tp;
  2350. args.mp = tp->t_mountp;
  2351. /*
  2352. * validate that the block number is legal - the enables us to detect
  2353. * and handle a silent filesystem corruption rather than crashing.
  2354. */
  2355. args.agno = XFS_FSB_TO_AGNO(args.mp, bno);
  2356. if (args.agno >= args.mp->m_sb.sb_agcount)
  2357. return EFSCORRUPTED;
  2358. args.agbno = XFS_FSB_TO_AGBNO(args.mp, bno);
  2359. if (args.agbno >= args.mp->m_sb.sb_agblocks)
  2360. return EFSCORRUPTED;
  2361. args.pag = xfs_perag_get(args.mp, args.agno);
  2362. ASSERT(args.pag);
  2363. error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
  2364. if (error)
  2365. goto error0;
  2366. /* validate the extent size is legal now we have the agf locked */
  2367. if (args.agbno + len >
  2368. be32_to_cpu(XFS_BUF_TO_AGF(args.agbp)->agf_length)) {
  2369. error = EFSCORRUPTED;
  2370. goto error0;
  2371. }
  2372. error = xfs_free_ag_extent(tp, args.agbp, args.agno, args.agbno, len, 0);
  2373. if (!error)
  2374. xfs_extent_busy_insert(tp, args.agno, args.agbno, len, 0);
  2375. error0:
  2376. xfs_perag_put(args.pag);
  2377. return error;
  2378. }