PageRenderTime 31ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full file

  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_longe

Large files files are truncated, but you can click here to view the full file