xfs_rtbitmap.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_bit.h"
  13. #include "xfs_mount.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_bmap_util.h"
  17. #include "xfs_bmap_btree.h"
  18. #include "xfs_alloc.h"
  19. #include "xfs_error.h"
  20. #include "xfs_trans.h"
  21. #include "xfs_trans_space.h"
  22. #include "xfs_trace.h"
  23. #include "xfs_buf.h"
  24. #include "xfs_icache.h"
  25. #include "xfs_rtalloc.h"
  26. /*
  27. * Realtime allocator bitmap functions shared with userspace.
  28. */
  29. /*
  30. * Real time buffers need verifiers to avoid runtime warnings during IO.
  31. * We don't have anything to verify, however, so these are just dummy
  32. * operations.
  33. */
  34. static void
  35. xfs_rtbuf_verify_read(
  36. struct xfs_buf *bp)
  37. {
  38. return;
  39. }
  40. static void
  41. xfs_rtbuf_verify_write(
  42. struct xfs_buf *bp)
  43. {
  44. return;
  45. }
  46. const struct xfs_buf_ops xfs_rtbuf_ops = {
  47. .name = "rtbuf",
  48. .verify_read = xfs_rtbuf_verify_read,
  49. .verify_write = xfs_rtbuf_verify_write,
  50. };
  51. /*
  52. * Get a buffer for the bitmap or summary file block specified.
  53. * The buffer is returned read and locked.
  54. */
  55. int
  56. xfs_rtbuf_get(
  57. xfs_mount_t *mp, /* file system mount structure */
  58. xfs_trans_t *tp, /* transaction pointer */
  59. xfs_rtblock_t block, /* block number in bitmap or summary */
  60. int issum, /* is summary not bitmap */
  61. xfs_buf_t **bpp) /* output: buffer for the block */
  62. {
  63. xfs_buf_t *bp; /* block buffer, result */
  64. xfs_inode_t *ip; /* bitmap or summary inode */
  65. xfs_bmbt_irec_t map;
  66. int nmap = 1;
  67. int error; /* error value */
  68. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  69. error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
  70. if (error)
  71. return error;
  72. if (nmap == 0 || !xfs_bmap_is_real_extent(&map))
  73. return -EFSCORRUPTED;
  74. ASSERT(map.br_startblock != NULLFSBLOCK);
  75. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  76. XFS_FSB_TO_DADDR(mp, map.br_startblock),
  77. mp->m_bsize, 0, &bp, &xfs_rtbuf_ops);
  78. if (error)
  79. return error;
  80. xfs_trans_buf_set_type(tp, bp, issum ? XFS_BLFT_RTSUMMARY_BUF
  81. : XFS_BLFT_RTBITMAP_BUF);
  82. *bpp = bp;
  83. return 0;
  84. }
  85. /*
  86. * Searching backward from start to limit, find the first block whose
  87. * allocated/free state is different from start's.
  88. */
  89. int
  90. xfs_rtfind_back(
  91. xfs_mount_t *mp, /* file system mount point */
  92. xfs_trans_t *tp, /* transaction pointer */
  93. xfs_rtblock_t start, /* starting block to look at */
  94. xfs_rtblock_t limit, /* last block to look at */
  95. xfs_rtblock_t *rtblock) /* out: start block found */
  96. {
  97. xfs_rtword_t *b; /* current word in buffer */
  98. int bit; /* bit number in the word */
  99. xfs_rtblock_t block; /* bitmap block number */
  100. xfs_buf_t *bp; /* buf for the block */
  101. xfs_rtword_t *bufp; /* starting word in buffer */
  102. int error; /* error value */
  103. xfs_rtblock_t firstbit; /* first useful bit in the word */
  104. xfs_rtblock_t i; /* current bit number rel. to start */
  105. xfs_rtblock_t len; /* length of inspected area */
  106. xfs_rtword_t mask; /* mask of relevant bits for value */
  107. xfs_rtword_t want; /* mask for "good" values */
  108. xfs_rtword_t wdiff; /* difference from wanted value */
  109. int word; /* word number in the buffer */
  110. /*
  111. * Compute and read in starting bitmap block for starting block.
  112. */
  113. block = XFS_BITTOBLOCK(mp, start);
  114. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  115. if (error) {
  116. return error;
  117. }
  118. bufp = bp->b_addr;
  119. /*
  120. * Get the first word's index & point to it.
  121. */
  122. word = XFS_BITTOWORD(mp, start);
  123. b = &bufp[word];
  124. bit = (int)(start & (XFS_NBWORD - 1));
  125. len = start - limit + 1;
  126. /*
  127. * Compute match value, based on the bit at start: if 1 (free)
  128. * then all-ones, else all-zeroes.
  129. */
  130. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  131. /*
  132. * If the starting position is not word-aligned, deal with the
  133. * partial word.
  134. */
  135. if (bit < XFS_NBWORD - 1) {
  136. /*
  137. * Calculate first (leftmost) bit number to look at,
  138. * and mask for all the relevant bits in this word.
  139. */
  140. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  141. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  142. firstbit;
  143. /*
  144. * Calculate the difference between the value there
  145. * and what we're looking for.
  146. */
  147. if ((wdiff = (*b ^ want) & mask)) {
  148. /*
  149. * Different. Mark where we are and return.
  150. */
  151. xfs_trans_brelse(tp, bp);
  152. i = bit - XFS_RTHIBIT(wdiff);
  153. *rtblock = start - i + 1;
  154. return 0;
  155. }
  156. i = bit - firstbit + 1;
  157. /*
  158. * Go on to previous block if that's where the previous word is
  159. * and we need the previous word.
  160. */
  161. if (--word == -1 && i < len) {
  162. /*
  163. * If done with this block, get the previous one.
  164. */
  165. xfs_trans_brelse(tp, bp);
  166. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  167. if (error) {
  168. return error;
  169. }
  170. bufp = bp->b_addr;
  171. word = XFS_BLOCKWMASK(mp);
  172. b = &bufp[word];
  173. } else {
  174. /*
  175. * Go on to the previous word in the buffer.
  176. */
  177. b--;
  178. }
  179. } else {
  180. /*
  181. * Starting on a word boundary, no partial word.
  182. */
  183. i = 0;
  184. }
  185. /*
  186. * Loop over whole words in buffers. When we use up one buffer
  187. * we move on to the previous one.
  188. */
  189. while (len - i >= XFS_NBWORD) {
  190. /*
  191. * Compute difference between actual and desired value.
  192. */
  193. if ((wdiff = *b ^ want)) {
  194. /*
  195. * Different, mark where we are and return.
  196. */
  197. xfs_trans_brelse(tp, bp);
  198. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  199. *rtblock = start - i + 1;
  200. return 0;
  201. }
  202. i += XFS_NBWORD;
  203. /*
  204. * Go on to previous block if that's where the previous word is
  205. * and we need the previous word.
  206. */
  207. if (--word == -1 && i < len) {
  208. /*
  209. * If done with this block, get the previous one.
  210. */
  211. xfs_trans_brelse(tp, bp);
  212. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  213. if (error) {
  214. return error;
  215. }
  216. bufp = bp->b_addr;
  217. word = XFS_BLOCKWMASK(mp);
  218. b = &bufp[word];
  219. } else {
  220. /*
  221. * Go on to the previous word in the buffer.
  222. */
  223. b--;
  224. }
  225. }
  226. /*
  227. * If not ending on a word boundary, deal with the last
  228. * (partial) word.
  229. */
  230. if (len - i) {
  231. /*
  232. * Calculate first (leftmost) bit number to look at,
  233. * and mask for all the relevant bits in this word.
  234. */
  235. firstbit = XFS_NBWORD - (len - i);
  236. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  237. /*
  238. * Compute difference between actual and desired value.
  239. */
  240. if ((wdiff = (*b ^ want) & mask)) {
  241. /*
  242. * Different, mark where we are and return.
  243. */
  244. xfs_trans_brelse(tp, bp);
  245. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  246. *rtblock = start - i + 1;
  247. return 0;
  248. } else
  249. i = len;
  250. }
  251. /*
  252. * No match, return that we scanned the whole area.
  253. */
  254. xfs_trans_brelse(tp, bp);
  255. *rtblock = start - i + 1;
  256. return 0;
  257. }
  258. /*
  259. * Searching forward from start to limit, find the first block whose
  260. * allocated/free state is different from start's.
  261. */
  262. int
  263. xfs_rtfind_forw(
  264. xfs_mount_t *mp, /* file system mount point */
  265. xfs_trans_t *tp, /* transaction pointer */
  266. xfs_rtblock_t start, /* starting block to look at */
  267. xfs_rtblock_t limit, /* last block to look at */
  268. xfs_rtblock_t *rtblock) /* out: start block found */
  269. {
  270. xfs_rtword_t *b; /* current word in buffer */
  271. int bit; /* bit number in the word */
  272. xfs_rtblock_t block; /* bitmap block number */
  273. xfs_buf_t *bp; /* buf for the block */
  274. xfs_rtword_t *bufp; /* starting word in buffer */
  275. int error; /* error value */
  276. xfs_rtblock_t i; /* current bit number rel. to start */
  277. xfs_rtblock_t lastbit; /* last useful bit in the word */
  278. xfs_rtblock_t len; /* length of inspected area */
  279. xfs_rtword_t mask; /* mask of relevant bits for value */
  280. xfs_rtword_t want; /* mask for "good" values */
  281. xfs_rtword_t wdiff; /* difference from wanted value */
  282. int word; /* word number in the buffer */
  283. /*
  284. * Compute and read in starting bitmap block for starting block.
  285. */
  286. block = XFS_BITTOBLOCK(mp, start);
  287. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  288. if (error) {
  289. return error;
  290. }
  291. bufp = bp->b_addr;
  292. /*
  293. * Get the first word's index & point to it.
  294. */
  295. word = XFS_BITTOWORD(mp, start);
  296. b = &bufp[word];
  297. bit = (int)(start & (XFS_NBWORD - 1));
  298. len = limit - start + 1;
  299. /*
  300. * Compute match value, based on the bit at start: if 1 (free)
  301. * then all-ones, else all-zeroes.
  302. */
  303. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  304. /*
  305. * If the starting position is not word-aligned, deal with the
  306. * partial word.
  307. */
  308. if (bit) {
  309. /*
  310. * Calculate last (rightmost) bit number to look at,
  311. * and mask for all the relevant bits in this word.
  312. */
  313. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  314. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  315. /*
  316. * Calculate the difference between the value there
  317. * and what we're looking for.
  318. */
  319. if ((wdiff = (*b ^ want) & mask)) {
  320. /*
  321. * Different. Mark where we are and return.
  322. */
  323. xfs_trans_brelse(tp, bp);
  324. i = XFS_RTLOBIT(wdiff) - bit;
  325. *rtblock = start + i - 1;
  326. return 0;
  327. }
  328. i = lastbit - bit;
  329. /*
  330. * Go on to next block if that's where the next word is
  331. * and we need the next word.
  332. */
  333. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  334. /*
  335. * If done with this block, get the previous one.
  336. */
  337. xfs_trans_brelse(tp, bp);
  338. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  339. if (error) {
  340. return error;
  341. }
  342. b = bufp = bp->b_addr;
  343. word = 0;
  344. } else {
  345. /*
  346. * Go on to the previous word in the buffer.
  347. */
  348. b++;
  349. }
  350. } else {
  351. /*
  352. * Starting on a word boundary, no partial word.
  353. */
  354. i = 0;
  355. }
  356. /*
  357. * Loop over whole words in buffers. When we use up one buffer
  358. * we move on to the next one.
  359. */
  360. while (len - i >= XFS_NBWORD) {
  361. /*
  362. * Compute difference between actual and desired value.
  363. */
  364. if ((wdiff = *b ^ want)) {
  365. /*
  366. * Different, mark where we are and return.
  367. */
  368. xfs_trans_brelse(tp, bp);
  369. i += XFS_RTLOBIT(wdiff);
  370. *rtblock = start + i - 1;
  371. return 0;
  372. }
  373. i += XFS_NBWORD;
  374. /*
  375. * Go on to next block if that's where the next word is
  376. * and we need the next word.
  377. */
  378. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  379. /*
  380. * If done with this block, get the next one.
  381. */
  382. xfs_trans_brelse(tp, bp);
  383. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  384. if (error) {
  385. return error;
  386. }
  387. b = bufp = bp->b_addr;
  388. word = 0;
  389. } else {
  390. /*
  391. * Go on to the next word in the buffer.
  392. */
  393. b++;
  394. }
  395. }
  396. /*
  397. * If not ending on a word boundary, deal with the last
  398. * (partial) word.
  399. */
  400. if ((lastbit = len - i)) {
  401. /*
  402. * Calculate mask for all the relevant bits in this word.
  403. */
  404. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  405. /*
  406. * Compute difference between actual and desired value.
  407. */
  408. if ((wdiff = (*b ^ want) & mask)) {
  409. /*
  410. * Different, mark where we are and return.
  411. */
  412. xfs_trans_brelse(tp, bp);
  413. i += XFS_RTLOBIT(wdiff);
  414. *rtblock = start + i - 1;
  415. return 0;
  416. } else
  417. i = len;
  418. }
  419. /*
  420. * No match, return that we scanned the whole area.
  421. */
  422. xfs_trans_brelse(tp, bp);
  423. *rtblock = start + i - 1;
  424. return 0;
  425. }
  426. /*
  427. * Read and/or modify the summary information for a given extent size,
  428. * bitmap block combination.
  429. * Keeps track of a current summary block, so we don't keep reading
  430. * it from the buffer cache.
  431. *
  432. * Summary information is returned in *sum if specified.
  433. * If no delta is specified, returns summary only.
  434. */
  435. int
  436. xfs_rtmodify_summary_int(
  437. xfs_mount_t *mp, /* file system mount structure */
  438. xfs_trans_t *tp, /* transaction pointer */
  439. int log, /* log2 of extent size */
  440. xfs_rtblock_t bbno, /* bitmap block number */
  441. int delta, /* change to make to summary info */
  442. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  443. xfs_fsblock_t *rsb, /* in/out: summary block number */
  444. xfs_suminfo_t *sum) /* out: summary info for this block */
  445. {
  446. xfs_buf_t *bp; /* buffer for the summary block */
  447. int error; /* error value */
  448. xfs_fsblock_t sb; /* summary fsblock */
  449. int so; /* index into the summary file */
  450. xfs_suminfo_t *sp; /* pointer to returned data */
  451. /*
  452. * Compute entry number in the summary file.
  453. */
  454. so = XFS_SUMOFFS(mp, log, bbno);
  455. /*
  456. * Compute the block number in the summary file.
  457. */
  458. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  459. /*
  460. * If we have an old buffer, and the block number matches, use that.
  461. */
  462. if (*rbpp && *rsb == sb)
  463. bp = *rbpp;
  464. /*
  465. * Otherwise we have to get the buffer.
  466. */
  467. else {
  468. /*
  469. * If there was an old one, get rid of it first.
  470. */
  471. if (*rbpp)
  472. xfs_trans_brelse(tp, *rbpp);
  473. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  474. if (error) {
  475. return error;
  476. }
  477. /*
  478. * Remember this buffer and block for the next call.
  479. */
  480. *rbpp = bp;
  481. *rsb = sb;
  482. }
  483. /*
  484. * Point to the summary information, modify/log it, and/or copy it out.
  485. */
  486. sp = XFS_SUMPTR(mp, bp, so);
  487. if (delta) {
  488. uint first = (uint)((char *)sp - (char *)bp->b_addr);
  489. *sp += delta;
  490. xfs_trans_log_buf(tp, bp, first, first + sizeof(*sp) - 1);
  491. }
  492. if (sum)
  493. *sum = *sp;
  494. return 0;
  495. }
  496. int
  497. xfs_rtmodify_summary(
  498. xfs_mount_t *mp, /* file system mount structure */
  499. xfs_trans_t *tp, /* transaction pointer */
  500. int log, /* log2 of extent size */
  501. xfs_rtblock_t bbno, /* bitmap block number */
  502. int delta, /* change to make to summary info */
  503. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  504. xfs_fsblock_t *rsb) /* in/out: summary block number */
  505. {
  506. return xfs_rtmodify_summary_int(mp, tp, log, bbno,
  507. delta, rbpp, rsb, NULL);
  508. }
  509. /*
  510. * Set the given range of bitmap bits to the given value.
  511. * Do whatever I/O and logging is required.
  512. */
  513. int
  514. xfs_rtmodify_range(
  515. xfs_mount_t *mp, /* file system mount point */
  516. xfs_trans_t *tp, /* transaction pointer */
  517. xfs_rtblock_t start, /* starting block to modify */
  518. xfs_extlen_t len, /* length of extent to modify */
  519. int val) /* 1 for free, 0 for allocated */
  520. {
  521. xfs_rtword_t *b; /* current word in buffer */
  522. int bit; /* bit number in the word */
  523. xfs_rtblock_t block; /* bitmap block number */
  524. xfs_buf_t *bp; /* buf for the block */
  525. xfs_rtword_t *bufp; /* starting word in buffer */
  526. int error; /* error value */
  527. xfs_rtword_t *first; /* first used word in the buffer */
  528. int i; /* current bit number rel. to start */
  529. int lastbit; /* last useful bit in word */
  530. xfs_rtword_t mask; /* mask o frelevant bits for value */
  531. int word; /* word number in the buffer */
  532. /*
  533. * Compute starting bitmap block number.
  534. */
  535. block = XFS_BITTOBLOCK(mp, start);
  536. /*
  537. * Read the bitmap block, and point to its data.
  538. */
  539. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  540. if (error) {
  541. return error;
  542. }
  543. bufp = bp->b_addr;
  544. /*
  545. * Compute the starting word's address, and starting bit.
  546. */
  547. word = XFS_BITTOWORD(mp, start);
  548. first = b = &bufp[word];
  549. bit = (int)(start & (XFS_NBWORD - 1));
  550. /*
  551. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  552. */
  553. val = -val;
  554. /*
  555. * If not starting on a word boundary, deal with the first
  556. * (partial) word.
  557. */
  558. if (bit) {
  559. /*
  560. * Compute first bit not changed and mask of relevant bits.
  561. */
  562. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  563. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  564. /*
  565. * Set/clear the active bits.
  566. */
  567. if (val)
  568. *b |= mask;
  569. else
  570. *b &= ~mask;
  571. i = lastbit - bit;
  572. /*
  573. * Go on to the next block if that's where the next word is
  574. * and we need the next word.
  575. */
  576. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  577. /*
  578. * Log the changed part of this block.
  579. * Get the next one.
  580. */
  581. xfs_trans_log_buf(tp, bp,
  582. (uint)((char *)first - (char *)bufp),
  583. (uint)((char *)b - (char *)bufp));
  584. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  585. if (error) {
  586. return error;
  587. }
  588. first = b = bufp = bp->b_addr;
  589. word = 0;
  590. } else {
  591. /*
  592. * Go on to the next word in the buffer
  593. */
  594. b++;
  595. }
  596. } else {
  597. /*
  598. * Starting on a word boundary, no partial word.
  599. */
  600. i = 0;
  601. }
  602. /*
  603. * Loop over whole words in buffers. When we use up one buffer
  604. * we move on to the next one.
  605. */
  606. while (len - i >= XFS_NBWORD) {
  607. /*
  608. * Set the word value correctly.
  609. */
  610. *b = val;
  611. i += XFS_NBWORD;
  612. /*
  613. * Go on to the next block if that's where the next word is
  614. * and we need the next word.
  615. */
  616. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  617. /*
  618. * Log the changed part of this block.
  619. * Get the next one.
  620. */
  621. xfs_trans_log_buf(tp, bp,
  622. (uint)((char *)first - (char *)bufp),
  623. (uint)((char *)b - (char *)bufp));
  624. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  625. if (error) {
  626. return error;
  627. }
  628. first = b = bufp = bp->b_addr;
  629. word = 0;
  630. } else {
  631. /*
  632. * Go on to the next word in the buffer
  633. */
  634. b++;
  635. }
  636. }
  637. /*
  638. * If not ending on a word boundary, deal with the last
  639. * (partial) word.
  640. */
  641. if ((lastbit = len - i)) {
  642. /*
  643. * Compute a mask of relevant bits.
  644. */
  645. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  646. /*
  647. * Set/clear the active bits.
  648. */
  649. if (val)
  650. *b |= mask;
  651. else
  652. *b &= ~mask;
  653. b++;
  654. }
  655. /*
  656. * Log any remaining changed bytes.
  657. */
  658. if (b > first)
  659. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  660. (uint)((char *)b - (char *)bufp - 1));
  661. return 0;
  662. }
  663. /*
  664. * Mark an extent specified by start and len freed.
  665. * Updates all the summary information as well as the bitmap.
  666. */
  667. int
  668. xfs_rtfree_range(
  669. xfs_mount_t *mp, /* file system mount point */
  670. xfs_trans_t *tp, /* transaction pointer */
  671. xfs_rtblock_t start, /* starting block to free */
  672. xfs_extlen_t len, /* length to free */
  673. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  674. xfs_fsblock_t *rsb) /* in/out: summary block number */
  675. {
  676. xfs_rtblock_t end; /* end of the freed extent */
  677. int error; /* error value */
  678. xfs_rtblock_t postblock; /* first block freed > end */
  679. xfs_rtblock_t preblock; /* first block freed < start */
  680. end = start + len - 1;
  681. /*
  682. * Modify the bitmap to mark this extent freed.
  683. */
  684. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  685. if (error) {
  686. return error;
  687. }
  688. /*
  689. * Assume we're freeing out of the middle of an allocated extent.
  690. * We need to find the beginning and end of the extent so we can
  691. * properly update the summary.
  692. */
  693. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  694. if (error) {
  695. return error;
  696. }
  697. /*
  698. * Find the next allocated block (end of allocated extent).
  699. */
  700. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  701. &postblock);
  702. if (error)
  703. return error;
  704. /*
  705. * If there are blocks not being freed at the front of the
  706. * old extent, add summary data for them to be allocated.
  707. */
  708. if (preblock < start) {
  709. error = xfs_rtmodify_summary(mp, tp,
  710. XFS_RTBLOCKLOG(start - preblock),
  711. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  712. if (error) {
  713. return error;
  714. }
  715. }
  716. /*
  717. * If there are blocks not being freed at the end of the
  718. * old extent, add summary data for them to be allocated.
  719. */
  720. if (postblock > end) {
  721. error = xfs_rtmodify_summary(mp, tp,
  722. XFS_RTBLOCKLOG(postblock - end),
  723. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  724. if (error) {
  725. return error;
  726. }
  727. }
  728. /*
  729. * Increment the summary information corresponding to the entire
  730. * (new) free extent.
  731. */
  732. error = xfs_rtmodify_summary(mp, tp,
  733. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  734. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  735. return error;
  736. }
  737. /*
  738. * Check that the given range is either all allocated (val = 0) or
  739. * all free (val = 1).
  740. */
  741. int
  742. xfs_rtcheck_range(
  743. xfs_mount_t *mp, /* file system mount point */
  744. xfs_trans_t *tp, /* transaction pointer */
  745. xfs_rtblock_t start, /* starting block number of extent */
  746. xfs_extlen_t len, /* length of extent */
  747. int val, /* 1 for free, 0 for allocated */
  748. xfs_rtblock_t *new, /* out: first block not matching */
  749. int *stat) /* out: 1 for matches, 0 for not */
  750. {
  751. xfs_rtword_t *b; /* current word in buffer */
  752. int bit; /* bit number in the word */
  753. xfs_rtblock_t block; /* bitmap block number */
  754. xfs_buf_t *bp; /* buf for the block */
  755. xfs_rtword_t *bufp; /* starting word in buffer */
  756. int error; /* error value */
  757. xfs_rtblock_t i; /* current bit number rel. to start */
  758. xfs_rtblock_t lastbit; /* last useful bit in word */
  759. xfs_rtword_t mask; /* mask of relevant bits for value */
  760. xfs_rtword_t wdiff; /* difference from wanted value */
  761. int word; /* word number in the buffer */
  762. /*
  763. * Compute starting bitmap block number
  764. */
  765. block = XFS_BITTOBLOCK(mp, start);
  766. /*
  767. * Read the bitmap block.
  768. */
  769. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  770. if (error) {
  771. return error;
  772. }
  773. bufp = bp->b_addr;
  774. /*
  775. * Compute the starting word's address, and starting bit.
  776. */
  777. word = XFS_BITTOWORD(mp, start);
  778. b = &bufp[word];
  779. bit = (int)(start & (XFS_NBWORD - 1));
  780. /*
  781. * 0 (allocated) => all zero's; 1 (free) => all one's.
  782. */
  783. val = -val;
  784. /*
  785. * If not starting on a word boundary, deal with the first
  786. * (partial) word.
  787. */
  788. if (bit) {
  789. /*
  790. * Compute first bit not examined.
  791. */
  792. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  793. /*
  794. * Mask of relevant bits.
  795. */
  796. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  797. /*
  798. * Compute difference between actual and desired value.
  799. */
  800. if ((wdiff = (*b ^ val) & mask)) {
  801. /*
  802. * Different, compute first wrong bit and return.
  803. */
  804. xfs_trans_brelse(tp, bp);
  805. i = XFS_RTLOBIT(wdiff) - bit;
  806. *new = start + i;
  807. *stat = 0;
  808. return 0;
  809. }
  810. i = lastbit - bit;
  811. /*
  812. * Go on to next block if that's where the next word is
  813. * and we need the next word.
  814. */
  815. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  816. /*
  817. * If done with this block, get the next one.
  818. */
  819. xfs_trans_brelse(tp, bp);
  820. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  821. if (error) {
  822. return error;
  823. }
  824. b = bufp = bp->b_addr;
  825. word = 0;
  826. } else {
  827. /*
  828. * Go on to the next word in the buffer.
  829. */
  830. b++;
  831. }
  832. } else {
  833. /*
  834. * Starting on a word boundary, no partial word.
  835. */
  836. i = 0;
  837. }
  838. /*
  839. * Loop over whole words in buffers. When we use up one buffer
  840. * we move on to the next one.
  841. */
  842. while (len - i >= XFS_NBWORD) {
  843. /*
  844. * Compute difference between actual and desired value.
  845. */
  846. if ((wdiff = *b ^ val)) {
  847. /*
  848. * Different, compute first wrong bit and return.
  849. */
  850. xfs_trans_brelse(tp, bp);
  851. i += XFS_RTLOBIT(wdiff);
  852. *new = start + i;
  853. *stat = 0;
  854. return 0;
  855. }
  856. i += XFS_NBWORD;
  857. /*
  858. * Go on to next block if that's where the next word is
  859. * and we need the next word.
  860. */
  861. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  862. /*
  863. * If done with this block, get the next one.
  864. */
  865. xfs_trans_brelse(tp, bp);
  866. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  867. if (error) {
  868. return error;
  869. }
  870. b = bufp = bp->b_addr;
  871. word = 0;
  872. } else {
  873. /*
  874. * Go on to the next word in the buffer.
  875. */
  876. b++;
  877. }
  878. }
  879. /*
  880. * If not ending on a word boundary, deal with the last
  881. * (partial) word.
  882. */
  883. if ((lastbit = len - i)) {
  884. /*
  885. * Mask of relevant bits.
  886. */
  887. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  888. /*
  889. * Compute difference between actual and desired value.
  890. */
  891. if ((wdiff = (*b ^ val) & mask)) {
  892. /*
  893. * Different, compute first wrong bit and return.
  894. */
  895. xfs_trans_brelse(tp, bp);
  896. i += XFS_RTLOBIT(wdiff);
  897. *new = start + i;
  898. *stat = 0;
  899. return 0;
  900. } else
  901. i = len;
  902. }
  903. /*
  904. * Successful, return.
  905. */
  906. xfs_trans_brelse(tp, bp);
  907. *new = start + i;
  908. *stat = 1;
  909. return 0;
  910. }
  911. #ifdef DEBUG
  912. /*
  913. * Check that the given extent (block range) is allocated already.
  914. */
  915. STATIC int /* error */
  916. xfs_rtcheck_alloc_range(
  917. xfs_mount_t *mp, /* file system mount point */
  918. xfs_trans_t *tp, /* transaction pointer */
  919. xfs_rtblock_t bno, /* starting block number of extent */
  920. xfs_extlen_t len) /* length of extent */
  921. {
  922. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  923. int stat;
  924. int error;
  925. error = xfs_rtcheck_range(mp, tp, bno, len, 0, &new, &stat);
  926. if (error)
  927. return error;
  928. ASSERT(stat);
  929. return 0;
  930. }
  931. #else
  932. #define xfs_rtcheck_alloc_range(m,t,b,l) (0)
  933. #endif
  934. /*
  935. * Free an extent in the realtime subvolume. Length is expressed in
  936. * realtime extents, as is the block number.
  937. */
  938. int /* error */
  939. xfs_rtfree_extent(
  940. xfs_trans_t *tp, /* transaction pointer */
  941. xfs_rtblock_t bno, /* starting block number to free */
  942. xfs_extlen_t len) /* length of extent freed */
  943. {
  944. int error; /* error value */
  945. xfs_mount_t *mp; /* file system mount structure */
  946. xfs_fsblock_t sb; /* summary file block number */
  947. xfs_buf_t *sumbp = NULL; /* summary file block buffer */
  948. mp = tp->t_mountp;
  949. ASSERT(mp->m_rbmip->i_itemp != NULL);
  950. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  951. error = xfs_rtcheck_alloc_range(mp, tp, bno, len);
  952. if (error)
  953. return error;
  954. /*
  955. * Free the range of realtime blocks.
  956. */
  957. error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
  958. if (error) {
  959. return error;
  960. }
  961. /*
  962. * Mark more blocks free in the superblock.
  963. */
  964. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
  965. /*
  966. * If we've now freed all the blocks, reset the file sequence
  967. * number to 0.
  968. */
  969. if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
  970. mp->m_sb.sb_rextents) {
  971. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
  972. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  973. *(uint64_t *)&VFS_I(mp->m_rbmip)->i_atime = 0;
  974. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  975. }
  976. return 0;
  977. }
  978. /* Find all the free records within a given range. */
  979. int
  980. xfs_rtalloc_query_range(
  981. struct xfs_trans *tp,
  982. struct xfs_rtalloc_rec *low_rec,
  983. struct xfs_rtalloc_rec *high_rec,
  984. xfs_rtalloc_query_range_fn fn,
  985. void *priv)
  986. {
  987. struct xfs_rtalloc_rec rec;
  988. struct xfs_mount *mp = tp->t_mountp;
  989. xfs_rtblock_t rtstart;
  990. xfs_rtblock_t rtend;
  991. int is_free;
  992. int error = 0;
  993. if (low_rec->ar_startext > high_rec->ar_startext)
  994. return -EINVAL;
  995. if (low_rec->ar_startext >= mp->m_sb.sb_rextents ||
  996. low_rec->ar_startext == high_rec->ar_startext)
  997. return 0;
  998. high_rec->ar_startext = min(high_rec->ar_startext,
  999. mp->m_sb.sb_rextents - 1);
  1000. /* Iterate the bitmap, looking for discrepancies. */
  1001. rtstart = low_rec->ar_startext;
  1002. while (rtstart <= high_rec->ar_startext) {
  1003. /* Is the first block free? */
  1004. error = xfs_rtcheck_range(mp, tp, rtstart, 1, 1, &rtend,
  1005. &is_free);
  1006. if (error)
  1007. break;
  1008. /* How long does the extent go for? */
  1009. error = xfs_rtfind_forw(mp, tp, rtstart,
  1010. high_rec->ar_startext, &rtend);
  1011. if (error)
  1012. break;
  1013. if (is_free) {
  1014. rec.ar_startext = rtstart;
  1015. rec.ar_extcount = rtend - rtstart + 1;
  1016. error = fn(tp, &rec, priv);
  1017. if (error)
  1018. break;
  1019. }
  1020. rtstart = rtend + 1;
  1021. }
  1022. return error;
  1023. }
  1024. /* Find all the free records. */
  1025. int
  1026. xfs_rtalloc_query_all(
  1027. struct xfs_trans *tp,
  1028. xfs_rtalloc_query_range_fn fn,
  1029. void *priv)
  1030. {
  1031. struct xfs_rtalloc_rec keys[2];
  1032. keys[0].ar_startext = 0;
  1033. keys[1].ar_startext = tp->t_mountp->m_sb.sb_rextents - 1;
  1034. keys[0].ar_extcount = keys[1].ar_extcount = 0;
  1035. return xfs_rtalloc_query_range(tp, &keys[0], &keys[1], fn, priv);
  1036. }
  1037. /* Is the given extent all free? */
  1038. int
  1039. xfs_rtalloc_extent_is_free(
  1040. struct xfs_mount *mp,
  1041. struct xfs_trans *tp,
  1042. xfs_rtblock_t start,
  1043. xfs_extlen_t len,
  1044. bool *is_free)
  1045. {
  1046. xfs_rtblock_t end;
  1047. int matches;
  1048. int error;
  1049. error = xfs_rtcheck_range(mp, tp, start, len, 1, &end, &matches);
  1050. if (error)
  1051. return error;
  1052. *is_free = matches;
  1053. return 0;
  1054. }