xfs_rtalloc.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  1. /*
  2. * Copyright (c) 2000-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_format.h"
  21. #include "xfs_shared.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_dinode.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_alloc.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_rtalloc.h"
  35. #include "xfs_fsops.h"
  36. #include "xfs_error.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_trans_space.h"
  39. #include "xfs_trace.h"
  40. #include "xfs_buf.h"
  41. #include "xfs_icache.h"
  42. /*
  43. * Prototypes for internal functions.
  44. */
  45. STATIC int xfs_rtallocate_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  46. xfs_extlen_t, xfs_buf_t **, xfs_fsblock_t *);
  47. STATIC int xfs_rtany_summary(xfs_mount_t *, xfs_trans_t *, int, int,
  48. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, int *);
  49. STATIC int xfs_rtcheck_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  50. xfs_extlen_t, int, xfs_rtblock_t *, int *);
  51. STATIC int xfs_rtfind_back(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  52. xfs_rtblock_t, xfs_rtblock_t *);
  53. STATIC int xfs_rtfind_forw(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  54. xfs_rtblock_t, xfs_rtblock_t *);
  55. STATIC int xfs_rtget_summary( xfs_mount_t *, xfs_trans_t *, int,
  56. xfs_rtblock_t, xfs_buf_t **, xfs_fsblock_t *, xfs_suminfo_t *);
  57. STATIC int xfs_rtmodify_range(xfs_mount_t *, xfs_trans_t *, xfs_rtblock_t,
  58. xfs_extlen_t, int);
  59. STATIC int xfs_rtmodify_summary(xfs_mount_t *, xfs_trans_t *, int,
  60. xfs_rtblock_t, int, xfs_buf_t **, xfs_fsblock_t *);
  61. /*
  62. * Internal functions.
  63. */
  64. /*
  65. * Allocate space to the bitmap or summary file, and zero it, for growfs.
  66. */
  67. STATIC int /* error */
  68. xfs_growfs_rt_alloc(
  69. xfs_mount_t *mp, /* file system mount point */
  70. xfs_extlen_t oblocks, /* old count of blocks */
  71. xfs_extlen_t nblocks, /* new count of blocks */
  72. xfs_inode_t *ip) /* inode (bitmap/summary) */
  73. {
  74. xfs_fileoff_t bno; /* block number in file */
  75. xfs_buf_t *bp; /* temporary buffer for zeroing */
  76. int committed; /* transaction committed flag */
  77. xfs_daddr_t d; /* disk block address */
  78. int error; /* error return value */
  79. xfs_fsblock_t firstblock; /* first block allocated in xaction */
  80. xfs_bmap_free_t flist; /* list of freed blocks */
  81. xfs_fsblock_t fsbno; /* filesystem block for bno */
  82. xfs_bmbt_irec_t map; /* block map output */
  83. int nmap; /* number of block maps */
  84. int resblks; /* space reservation */
  85. /*
  86. * Allocate space to the file, as necessary.
  87. */
  88. while (oblocks < nblocks) {
  89. int cancelflags = 0;
  90. xfs_trans_t *tp;
  91. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ALLOC);
  92. resblks = XFS_GROWFSRT_SPACE_RES(mp, nblocks - oblocks);
  93. /*
  94. * Reserve space & log for one extent added to the file.
  95. */
  96. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growdata,
  97. resblks, 0);
  98. if (error)
  99. goto error_cancel;
  100. cancelflags = XFS_TRANS_RELEASE_LOG_RES;
  101. /*
  102. * Lock the inode.
  103. */
  104. xfs_ilock(ip, XFS_ILOCK_EXCL);
  105. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  106. xfs_bmap_init(&flist, &firstblock);
  107. /*
  108. * Allocate blocks to the bitmap file.
  109. */
  110. nmap = 1;
  111. cancelflags |= XFS_TRANS_ABORT;
  112. error = xfs_bmapi_write(tp, ip, oblocks, nblocks - oblocks,
  113. XFS_BMAPI_METADATA, &firstblock,
  114. resblks, &map, &nmap, &flist);
  115. if (!error && nmap < 1)
  116. error = XFS_ERROR(ENOSPC);
  117. if (error)
  118. goto error_cancel;
  119. /*
  120. * Free any blocks freed up in the transaction, then commit.
  121. */
  122. error = xfs_bmap_finish(&tp, &flist, &committed);
  123. if (error)
  124. goto error_cancel;
  125. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  126. if (error)
  127. goto error;
  128. /*
  129. * Now we need to clear the allocated blocks.
  130. * Do this one block per transaction, to keep it simple.
  131. */
  132. cancelflags = 0;
  133. for (bno = map.br_startoff, fsbno = map.br_startblock;
  134. bno < map.br_startoff + map.br_blockcount;
  135. bno++, fsbno++) {
  136. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_ZERO);
  137. /*
  138. * Reserve log for one block zeroing.
  139. */
  140. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtzero,
  141. 0, 0);
  142. if (error)
  143. goto error_cancel;
  144. /*
  145. * Lock the bitmap inode.
  146. */
  147. xfs_ilock(ip, XFS_ILOCK_EXCL);
  148. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  149. /*
  150. * Get a buffer for the block.
  151. */
  152. d = XFS_FSB_TO_DADDR(mp, fsbno);
  153. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  154. mp->m_bsize, 0);
  155. if (bp == NULL) {
  156. error = XFS_ERROR(EIO);
  157. error_cancel:
  158. xfs_trans_cancel(tp, cancelflags);
  159. goto error;
  160. }
  161. memset(bp->b_addr, 0, mp->m_sb.sb_blocksize);
  162. xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
  163. /*
  164. * Commit the transaction.
  165. */
  166. error = xfs_trans_commit(tp, 0);
  167. if (error)
  168. goto error;
  169. }
  170. /*
  171. * Go on to the next extent, if any.
  172. */
  173. oblocks = map.br_startoff + map.br_blockcount;
  174. }
  175. return 0;
  176. error:
  177. return error;
  178. }
  179. /*
  180. * Attempt to allocate an extent minlen<=len<=maxlen starting from
  181. * bitmap block bbno. If we don't get maxlen then use prod to trim
  182. * the length, if given. Returns error; returns starting block in *rtblock.
  183. * The lengths are all in rtextents.
  184. */
  185. STATIC int /* error */
  186. xfs_rtallocate_extent_block(
  187. xfs_mount_t *mp, /* file system mount point */
  188. xfs_trans_t *tp, /* transaction pointer */
  189. xfs_rtblock_t bbno, /* bitmap block number */
  190. xfs_extlen_t minlen, /* minimum length to allocate */
  191. xfs_extlen_t maxlen, /* maximum length to allocate */
  192. xfs_extlen_t *len, /* out: actual length allocated */
  193. xfs_rtblock_t *nextp, /* out: next block to try */
  194. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  195. xfs_fsblock_t *rsb, /* in/out: summary block number */
  196. xfs_extlen_t prod, /* extent product factor */
  197. xfs_rtblock_t *rtblock) /* out: start block allocated */
  198. {
  199. xfs_rtblock_t besti; /* best rtblock found so far */
  200. xfs_rtblock_t bestlen; /* best length found so far */
  201. xfs_rtblock_t end; /* last rtblock in chunk */
  202. int error; /* error value */
  203. xfs_rtblock_t i; /* current rtblock trying */
  204. xfs_rtblock_t next; /* next rtblock to try */
  205. int stat; /* status from internal calls */
  206. /*
  207. * Loop over all the extents starting in this bitmap block,
  208. * looking for one that's long enough.
  209. */
  210. for (i = XFS_BLOCKTOBIT(mp, bbno), besti = -1, bestlen = 0,
  211. end = XFS_BLOCKTOBIT(mp, bbno + 1) - 1;
  212. i <= end;
  213. i++) {
  214. /*
  215. * See if there's a free extent of maxlen starting at i.
  216. * If it's not so then next will contain the first non-free.
  217. */
  218. error = xfs_rtcheck_range(mp, tp, i, maxlen, 1, &next, &stat);
  219. if (error) {
  220. return error;
  221. }
  222. if (stat) {
  223. /*
  224. * i for maxlen is all free, allocate and return that.
  225. */
  226. error = xfs_rtallocate_range(mp, tp, i, maxlen, rbpp,
  227. rsb);
  228. if (error) {
  229. return error;
  230. }
  231. *len = maxlen;
  232. *rtblock = i;
  233. return 0;
  234. }
  235. /*
  236. * In the case where we have a variable-sized allocation
  237. * request, figure out how big this free piece is,
  238. * and if it's big enough for the minimum, and the best
  239. * so far, remember it.
  240. */
  241. if (minlen < maxlen) {
  242. xfs_rtblock_t thislen; /* this extent size */
  243. thislen = next - i;
  244. if (thislen >= minlen && thislen > bestlen) {
  245. besti = i;
  246. bestlen = thislen;
  247. }
  248. }
  249. /*
  250. * If not done yet, find the start of the next free space.
  251. */
  252. if (next < end) {
  253. error = xfs_rtfind_forw(mp, tp, next, end, &i);
  254. if (error) {
  255. return error;
  256. }
  257. } else
  258. break;
  259. }
  260. /*
  261. * Searched the whole thing & didn't find a maxlen free extent.
  262. */
  263. if (minlen < maxlen && besti != -1) {
  264. xfs_extlen_t p; /* amount to trim length by */
  265. /*
  266. * If size should be a multiple of prod, make that so.
  267. */
  268. if (prod > 1 && (p = do_mod(bestlen, prod)))
  269. bestlen -= p;
  270. /*
  271. * Allocate besti for bestlen & return that.
  272. */
  273. error = xfs_rtallocate_range(mp, tp, besti, bestlen, rbpp, rsb);
  274. if (error) {
  275. return error;
  276. }
  277. *len = bestlen;
  278. *rtblock = besti;
  279. return 0;
  280. }
  281. /*
  282. * Allocation failed. Set *nextp to the next block to try.
  283. */
  284. *nextp = next;
  285. *rtblock = NULLRTBLOCK;
  286. return 0;
  287. }
  288. /*
  289. * Allocate an extent of length minlen<=len<=maxlen, starting at block
  290. * bno. If we don't get maxlen then use prod to trim the length, if given.
  291. * Returns error; returns starting block in *rtblock.
  292. * The lengths are all in rtextents.
  293. */
  294. STATIC int /* error */
  295. xfs_rtallocate_extent_exact(
  296. xfs_mount_t *mp, /* file system mount point */
  297. xfs_trans_t *tp, /* transaction pointer */
  298. xfs_rtblock_t bno, /* starting block number to allocate */
  299. xfs_extlen_t minlen, /* minimum length to allocate */
  300. xfs_extlen_t maxlen, /* maximum length to allocate */
  301. xfs_extlen_t *len, /* out: actual length allocated */
  302. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  303. xfs_fsblock_t *rsb, /* in/out: summary block number */
  304. xfs_extlen_t prod, /* extent product factor */
  305. xfs_rtblock_t *rtblock) /* out: start block allocated */
  306. {
  307. int error; /* error value */
  308. xfs_extlen_t i; /* extent length trimmed due to prod */
  309. int isfree; /* extent is free */
  310. xfs_rtblock_t next; /* next block to try (dummy) */
  311. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  312. /*
  313. * Check if the range in question (for maxlen) is free.
  314. */
  315. error = xfs_rtcheck_range(mp, tp, bno, maxlen, 1, &next, &isfree);
  316. if (error) {
  317. return error;
  318. }
  319. if (isfree) {
  320. /*
  321. * If it is, allocate it and return success.
  322. */
  323. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  324. if (error) {
  325. return error;
  326. }
  327. *len = maxlen;
  328. *rtblock = bno;
  329. return 0;
  330. }
  331. /*
  332. * If not, allocate what there is, if it's at least minlen.
  333. */
  334. maxlen = next - bno;
  335. if (maxlen < minlen) {
  336. /*
  337. * Failed, return failure status.
  338. */
  339. *rtblock = NULLRTBLOCK;
  340. return 0;
  341. }
  342. /*
  343. * Trim off tail of extent, if prod is specified.
  344. */
  345. if (prod > 1 && (i = maxlen % prod)) {
  346. maxlen -= i;
  347. if (maxlen < minlen) {
  348. /*
  349. * Now we can't do it, return failure status.
  350. */
  351. *rtblock = NULLRTBLOCK;
  352. return 0;
  353. }
  354. }
  355. /*
  356. * Allocate what we can and return it.
  357. */
  358. error = xfs_rtallocate_range(mp, tp, bno, maxlen, rbpp, rsb);
  359. if (error) {
  360. return error;
  361. }
  362. *len = maxlen;
  363. *rtblock = bno;
  364. return 0;
  365. }
  366. /*
  367. * Allocate an extent of length minlen<=len<=maxlen, starting as near
  368. * to bno as possible. If we don't get maxlen then use prod to trim
  369. * the length, if given. The lengths are all in rtextents.
  370. */
  371. STATIC int /* error */
  372. xfs_rtallocate_extent_near(
  373. xfs_mount_t *mp, /* file system mount point */
  374. xfs_trans_t *tp, /* transaction pointer */
  375. xfs_rtblock_t bno, /* starting block number to allocate */
  376. xfs_extlen_t minlen, /* minimum length to allocate */
  377. xfs_extlen_t maxlen, /* maximum length to allocate */
  378. xfs_extlen_t *len, /* out: actual length allocated */
  379. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  380. xfs_fsblock_t *rsb, /* in/out: summary block number */
  381. xfs_extlen_t prod, /* extent product factor */
  382. xfs_rtblock_t *rtblock) /* out: start block allocated */
  383. {
  384. int any; /* any useful extents from summary */
  385. xfs_rtblock_t bbno; /* bitmap block number */
  386. int error; /* error value */
  387. int i; /* bitmap block offset (loop control) */
  388. int j; /* secondary loop control */
  389. int log2len; /* log2 of minlen */
  390. xfs_rtblock_t n; /* next block to try */
  391. xfs_rtblock_t r; /* result block */
  392. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  393. /*
  394. * If the block number given is off the end, silently set it to
  395. * the last block.
  396. */
  397. if (bno >= mp->m_sb.sb_rextents)
  398. bno = mp->m_sb.sb_rextents - 1;
  399. /*
  400. * Try the exact allocation first.
  401. */
  402. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen, len,
  403. rbpp, rsb, prod, &r);
  404. if (error) {
  405. return error;
  406. }
  407. /*
  408. * If the exact allocation worked, return that.
  409. */
  410. if (r != NULLRTBLOCK) {
  411. *rtblock = r;
  412. return 0;
  413. }
  414. bbno = XFS_BITTOBLOCK(mp, bno);
  415. i = 0;
  416. ASSERT(minlen != 0);
  417. log2len = xfs_highbit32(minlen);
  418. /*
  419. * Loop over all bitmap blocks (bbno + i is current block).
  420. */
  421. for (;;) {
  422. /*
  423. * Get summary information of extents of all useful levels
  424. * starting in this bitmap block.
  425. */
  426. error = xfs_rtany_summary(mp, tp, log2len, mp->m_rsumlevels - 1,
  427. bbno + i, rbpp, rsb, &any);
  428. if (error) {
  429. return error;
  430. }
  431. /*
  432. * If there are any useful extents starting here, try
  433. * allocating one.
  434. */
  435. if (any) {
  436. /*
  437. * On the positive side of the starting location.
  438. */
  439. if (i >= 0) {
  440. /*
  441. * Try to allocate an extent starting in
  442. * this block.
  443. */
  444. error = xfs_rtallocate_extent_block(mp, tp,
  445. bbno + i, minlen, maxlen, len, &n, rbpp,
  446. rsb, prod, &r);
  447. if (error) {
  448. return error;
  449. }
  450. /*
  451. * If it worked, return it.
  452. */
  453. if (r != NULLRTBLOCK) {
  454. *rtblock = r;
  455. return 0;
  456. }
  457. }
  458. /*
  459. * On the negative side of the starting location.
  460. */
  461. else { /* i < 0 */
  462. /*
  463. * Loop backwards through the bitmap blocks from
  464. * the starting point-1 up to where we are now.
  465. * There should be an extent which ends in this
  466. * bitmap block and is long enough.
  467. */
  468. for (j = -1; j > i; j--) {
  469. /*
  470. * Grab the summary information for
  471. * this bitmap block.
  472. */
  473. error = xfs_rtany_summary(mp, tp,
  474. log2len, mp->m_rsumlevels - 1,
  475. bbno + j, rbpp, rsb, &any);
  476. if (error) {
  477. return error;
  478. }
  479. /*
  480. * If there's no extent given in the
  481. * summary that means the extent we
  482. * found must carry over from an
  483. * earlier block. If there is an
  484. * extent given, we've already tried
  485. * that allocation, don't do it again.
  486. */
  487. if (any)
  488. continue;
  489. error = xfs_rtallocate_extent_block(mp,
  490. tp, bbno + j, minlen, maxlen,
  491. len, &n, rbpp, rsb, prod, &r);
  492. if (error) {
  493. return error;
  494. }
  495. /*
  496. * If it works, return the extent.
  497. */
  498. if (r != NULLRTBLOCK) {
  499. *rtblock = r;
  500. return 0;
  501. }
  502. }
  503. /*
  504. * There weren't intervening bitmap blocks
  505. * with a long enough extent, or the
  506. * allocation didn't work for some reason
  507. * (i.e. it's a little * too short).
  508. * Try to allocate from the summary block
  509. * that we found.
  510. */
  511. error = xfs_rtallocate_extent_block(mp, tp,
  512. bbno + i, minlen, maxlen, len, &n, rbpp,
  513. rsb, prod, &r);
  514. if (error) {
  515. return error;
  516. }
  517. /*
  518. * If it works, return the extent.
  519. */
  520. if (r != NULLRTBLOCK) {
  521. *rtblock = r;
  522. return 0;
  523. }
  524. }
  525. }
  526. /*
  527. * Loop control. If we were on the positive side, and there's
  528. * still more blocks on the negative side, go there.
  529. */
  530. if (i > 0 && (int)bbno - i >= 0)
  531. i = -i;
  532. /*
  533. * If positive, and no more negative, but there are more
  534. * positive, go there.
  535. */
  536. else if (i > 0 && (int)bbno + i < mp->m_sb.sb_rbmblocks - 1)
  537. i++;
  538. /*
  539. * If negative or 0 (just started), and there are positive
  540. * blocks to go, go there. The 0 case moves to block 1.
  541. */
  542. else if (i <= 0 && (int)bbno - i < mp->m_sb.sb_rbmblocks - 1)
  543. i = 1 - i;
  544. /*
  545. * If negative or 0 and there are more negative blocks,
  546. * go there.
  547. */
  548. else if (i <= 0 && (int)bbno + i > 0)
  549. i--;
  550. /*
  551. * Must be done. Return failure.
  552. */
  553. else
  554. break;
  555. }
  556. *rtblock = NULLRTBLOCK;
  557. return 0;
  558. }
  559. /*
  560. * Allocate an extent of length minlen<=len<=maxlen, with no position
  561. * specified. If we don't get maxlen then use prod to trim
  562. * the length, if given. The lengths are all in rtextents.
  563. */
  564. STATIC int /* error */
  565. xfs_rtallocate_extent_size(
  566. xfs_mount_t *mp, /* file system mount point */
  567. xfs_trans_t *tp, /* transaction pointer */
  568. xfs_extlen_t minlen, /* minimum length to allocate */
  569. xfs_extlen_t maxlen, /* maximum length to allocate */
  570. xfs_extlen_t *len, /* out: actual length allocated */
  571. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  572. xfs_fsblock_t *rsb, /* in/out: summary block number */
  573. xfs_extlen_t prod, /* extent product factor */
  574. xfs_rtblock_t *rtblock) /* out: start block allocated */
  575. {
  576. int error; /* error value */
  577. int i; /* bitmap block number */
  578. int l; /* level number (loop control) */
  579. xfs_rtblock_t n; /* next block to be tried */
  580. xfs_rtblock_t r; /* result block number */
  581. xfs_suminfo_t sum; /* summary information for extents */
  582. ASSERT(minlen % prod == 0 && maxlen % prod == 0);
  583. ASSERT(maxlen != 0);
  584. /*
  585. * Loop over all the levels starting with maxlen.
  586. * At each level, look at all the bitmap blocks, to see if there
  587. * are extents starting there that are long enough (>= maxlen).
  588. * Note, only on the initial level can the allocation fail if
  589. * the summary says there's an extent.
  590. */
  591. for (l = xfs_highbit32(maxlen); l < mp->m_rsumlevels; l++) {
  592. /*
  593. * Loop over all the bitmap blocks.
  594. */
  595. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  596. /*
  597. * Get the summary for this level/block.
  598. */
  599. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  600. &sum);
  601. if (error) {
  602. return error;
  603. }
  604. /*
  605. * Nothing there, on to the next block.
  606. */
  607. if (!sum)
  608. continue;
  609. /*
  610. * Try allocating the extent.
  611. */
  612. error = xfs_rtallocate_extent_block(mp, tp, i, maxlen,
  613. maxlen, len, &n, rbpp, rsb, prod, &r);
  614. if (error) {
  615. return error;
  616. }
  617. /*
  618. * If it worked, return that.
  619. */
  620. if (r != NULLRTBLOCK) {
  621. *rtblock = r;
  622. return 0;
  623. }
  624. /*
  625. * If the "next block to try" returned from the
  626. * allocator is beyond the next bitmap block,
  627. * skip to that bitmap block.
  628. */
  629. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  630. i = XFS_BITTOBLOCK(mp, n) - 1;
  631. }
  632. }
  633. /*
  634. * Didn't find any maxlen blocks. Try smaller ones, unless
  635. * we're asking for a fixed size extent.
  636. */
  637. if (minlen > --maxlen) {
  638. *rtblock = NULLRTBLOCK;
  639. return 0;
  640. }
  641. ASSERT(minlen != 0);
  642. ASSERT(maxlen != 0);
  643. /*
  644. * Loop over sizes, from maxlen down to minlen.
  645. * This time, when we do the allocations, allow smaller ones
  646. * to succeed.
  647. */
  648. for (l = xfs_highbit32(maxlen); l >= xfs_highbit32(minlen); l--) {
  649. /*
  650. * Loop over all the bitmap blocks, try an allocation
  651. * starting in that block.
  652. */
  653. for (i = 0; i < mp->m_sb.sb_rbmblocks; i++) {
  654. /*
  655. * Get the summary information for this level/block.
  656. */
  657. error = xfs_rtget_summary(mp, tp, l, i, rbpp, rsb,
  658. &sum);
  659. if (error) {
  660. return error;
  661. }
  662. /*
  663. * If nothing there, go on to next.
  664. */
  665. if (!sum)
  666. continue;
  667. /*
  668. * Try the allocation. Make sure the specified
  669. * minlen/maxlen are in the possible range for
  670. * this summary level.
  671. */
  672. error = xfs_rtallocate_extent_block(mp, tp, i,
  673. XFS_RTMAX(minlen, 1 << l),
  674. XFS_RTMIN(maxlen, (1 << (l + 1)) - 1),
  675. len, &n, rbpp, rsb, prod, &r);
  676. if (error) {
  677. return error;
  678. }
  679. /*
  680. * If it worked, return that extent.
  681. */
  682. if (r != NULLRTBLOCK) {
  683. *rtblock = r;
  684. return 0;
  685. }
  686. /*
  687. * If the "next block to try" returned from the
  688. * allocator is beyond the next bitmap block,
  689. * skip to that bitmap block.
  690. */
  691. if (XFS_BITTOBLOCK(mp, n) > i + 1)
  692. i = XFS_BITTOBLOCK(mp, n) - 1;
  693. }
  694. }
  695. /*
  696. * Got nothing, return failure.
  697. */
  698. *rtblock = NULLRTBLOCK;
  699. return 0;
  700. }
  701. /*
  702. * Mark an extent specified by start and len allocated.
  703. * Updates all the summary information as well as the bitmap.
  704. */
  705. STATIC int /* error */
  706. xfs_rtallocate_range(
  707. xfs_mount_t *mp, /* file system mount point */
  708. xfs_trans_t *tp, /* transaction pointer */
  709. xfs_rtblock_t start, /* start block to allocate */
  710. xfs_extlen_t len, /* length to allocate */
  711. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  712. xfs_fsblock_t *rsb) /* in/out: summary block number */
  713. {
  714. xfs_rtblock_t end; /* end of the allocated extent */
  715. int error; /* error value */
  716. xfs_rtblock_t postblock = 0; /* first block allocated > end */
  717. xfs_rtblock_t preblock = 0; /* first block allocated < start */
  718. end = start + len - 1;
  719. /*
  720. * Assume we're allocating out of the middle of a free extent.
  721. * We need to find the beginning and end of the extent so we can
  722. * properly update the summary.
  723. */
  724. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  725. if (error) {
  726. return error;
  727. }
  728. /*
  729. * Find the next allocated block (end of free extent).
  730. */
  731. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  732. &postblock);
  733. if (error) {
  734. return error;
  735. }
  736. /*
  737. * Decrement the summary information corresponding to the entire
  738. * (old) free extent.
  739. */
  740. error = xfs_rtmodify_summary(mp, tp,
  741. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  742. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  743. if (error) {
  744. return error;
  745. }
  746. /*
  747. * If there are blocks not being allocated at the front of the
  748. * old extent, add summary data for them to be free.
  749. */
  750. if (preblock < start) {
  751. error = xfs_rtmodify_summary(mp, tp,
  752. XFS_RTBLOCKLOG(start - preblock),
  753. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  754. if (error) {
  755. return error;
  756. }
  757. }
  758. /*
  759. * If there are blocks not being allocated at the end of the
  760. * old extent, add summary data for them to be free.
  761. */
  762. if (postblock > end) {
  763. error = xfs_rtmodify_summary(mp, tp,
  764. XFS_RTBLOCKLOG(postblock - end),
  765. XFS_BITTOBLOCK(mp, end + 1), 1, rbpp, rsb);
  766. if (error) {
  767. return error;
  768. }
  769. }
  770. /*
  771. * Modify the bitmap to mark this extent allocated.
  772. */
  773. error = xfs_rtmodify_range(mp, tp, start, len, 0);
  774. return error;
  775. }
  776. /*
  777. * Return whether there are any free extents in the size range given
  778. * by low and high, for the bitmap block bbno.
  779. */
  780. STATIC int /* error */
  781. xfs_rtany_summary(
  782. xfs_mount_t *mp, /* file system mount structure */
  783. xfs_trans_t *tp, /* transaction pointer */
  784. int low, /* low log2 extent size */
  785. int high, /* high log2 extent size */
  786. xfs_rtblock_t bbno, /* bitmap block number */
  787. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  788. xfs_fsblock_t *rsb, /* in/out: summary block number */
  789. int *stat) /* out: any good extents here? */
  790. {
  791. int error; /* error value */
  792. int log; /* loop counter, log2 of ext. size */
  793. xfs_suminfo_t sum; /* summary data */
  794. /*
  795. * Loop over logs of extent sizes. Order is irrelevant.
  796. */
  797. for (log = low; log <= high; log++) {
  798. /*
  799. * Get one summary datum.
  800. */
  801. error = xfs_rtget_summary(mp, tp, log, bbno, rbpp, rsb, &sum);
  802. if (error) {
  803. return error;
  804. }
  805. /*
  806. * If there are any, return success.
  807. */
  808. if (sum) {
  809. *stat = 1;
  810. return 0;
  811. }
  812. }
  813. /*
  814. * Found nothing, return failure.
  815. */
  816. *stat = 0;
  817. return 0;
  818. }
  819. /*
  820. * Get a buffer for the bitmap or summary file block specified.
  821. * The buffer is returned read and locked.
  822. */
  823. STATIC int /* error */
  824. xfs_rtbuf_get(
  825. xfs_mount_t *mp, /* file system mount structure */
  826. xfs_trans_t *tp, /* transaction pointer */
  827. xfs_rtblock_t block, /* block number in bitmap or summary */
  828. int issum, /* is summary not bitmap */
  829. xfs_buf_t **bpp) /* output: buffer for the block */
  830. {
  831. xfs_buf_t *bp; /* block buffer, result */
  832. xfs_inode_t *ip; /* bitmap or summary inode */
  833. xfs_bmbt_irec_t map;
  834. int nmap = 1;
  835. int error; /* error value */
  836. ip = issum ? mp->m_rsumip : mp->m_rbmip;
  837. error = xfs_bmapi_read(ip, block, 1, &map, &nmap, XFS_DATA_FORK);
  838. if (error)
  839. return error;
  840. ASSERT(map.br_startblock != NULLFSBLOCK);
  841. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  842. XFS_FSB_TO_DADDR(mp, map.br_startblock),
  843. mp->m_bsize, 0, &bp, NULL);
  844. if (error)
  845. return error;
  846. ASSERT(!xfs_buf_geterror(bp));
  847. *bpp = bp;
  848. return 0;
  849. }
  850. #ifdef DEBUG
  851. /*
  852. * Check that the given extent (block range) is allocated already.
  853. */
  854. STATIC int /* error */
  855. xfs_rtcheck_alloc_range(
  856. xfs_mount_t *mp, /* file system mount point */
  857. xfs_trans_t *tp, /* transaction pointer */
  858. xfs_rtblock_t bno, /* starting block number of extent */
  859. xfs_extlen_t len, /* length of extent */
  860. int *stat) /* out: 1 for allocated, 0 for not */
  861. {
  862. xfs_rtblock_t new; /* dummy for xfs_rtcheck_range */
  863. return xfs_rtcheck_range(mp, tp, bno, len, 0, &new, stat);
  864. }
  865. #endif
  866. /*
  867. * Check that the given range is either all allocated (val = 0) or
  868. * all free (val = 1).
  869. */
  870. STATIC int /* error */
  871. xfs_rtcheck_range(
  872. xfs_mount_t *mp, /* file system mount point */
  873. xfs_trans_t *tp, /* transaction pointer */
  874. xfs_rtblock_t start, /* starting block number of extent */
  875. xfs_extlen_t len, /* length of extent */
  876. int val, /* 1 for free, 0 for allocated */
  877. xfs_rtblock_t *new, /* out: first block not matching */
  878. int *stat) /* out: 1 for matches, 0 for not */
  879. {
  880. xfs_rtword_t *b; /* current word in buffer */
  881. int bit; /* bit number in the word */
  882. xfs_rtblock_t block; /* bitmap block number */
  883. xfs_buf_t *bp; /* buf for the block */
  884. xfs_rtword_t *bufp; /* starting word in buffer */
  885. int error; /* error value */
  886. xfs_rtblock_t i; /* current bit number rel. to start */
  887. xfs_rtblock_t lastbit; /* last useful bit in word */
  888. xfs_rtword_t mask; /* mask of relevant bits for value */
  889. xfs_rtword_t wdiff; /* difference from wanted value */
  890. int word; /* word number in the buffer */
  891. /*
  892. * Compute starting bitmap block number
  893. */
  894. block = XFS_BITTOBLOCK(mp, start);
  895. /*
  896. * Read the bitmap block.
  897. */
  898. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  899. if (error) {
  900. return error;
  901. }
  902. bufp = bp->b_addr;
  903. /*
  904. * Compute the starting word's address, and starting bit.
  905. */
  906. word = XFS_BITTOWORD(mp, start);
  907. b = &bufp[word];
  908. bit = (int)(start & (XFS_NBWORD - 1));
  909. /*
  910. * 0 (allocated) => all zero's; 1 (free) => all one's.
  911. */
  912. val = -val;
  913. /*
  914. * If not starting on a word boundary, deal with the first
  915. * (partial) word.
  916. */
  917. if (bit) {
  918. /*
  919. * Compute first bit not examined.
  920. */
  921. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  922. /*
  923. * Mask of relevant bits.
  924. */
  925. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  926. /*
  927. * Compute difference between actual and desired value.
  928. */
  929. if ((wdiff = (*b ^ val) & mask)) {
  930. /*
  931. * Different, compute first wrong bit and return.
  932. */
  933. xfs_trans_brelse(tp, bp);
  934. i = XFS_RTLOBIT(wdiff) - bit;
  935. *new = start + i;
  936. *stat = 0;
  937. return 0;
  938. }
  939. i = lastbit - bit;
  940. /*
  941. * Go on to next block if that's where the next word is
  942. * and we need the next word.
  943. */
  944. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  945. /*
  946. * If done with this block, get the next one.
  947. */
  948. xfs_trans_brelse(tp, bp);
  949. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  950. if (error) {
  951. return error;
  952. }
  953. b = bufp = bp->b_addr;
  954. word = 0;
  955. } else {
  956. /*
  957. * Go on to the next word in the buffer.
  958. */
  959. b++;
  960. }
  961. } else {
  962. /*
  963. * Starting on a word boundary, no partial word.
  964. */
  965. i = 0;
  966. }
  967. /*
  968. * Loop over whole words in buffers. When we use up one buffer
  969. * we move on to the next one.
  970. */
  971. while (len - i >= XFS_NBWORD) {
  972. /*
  973. * Compute difference between actual and desired value.
  974. */
  975. if ((wdiff = *b ^ val)) {
  976. /*
  977. * Different, compute first wrong bit and return.
  978. */
  979. xfs_trans_brelse(tp, bp);
  980. i += XFS_RTLOBIT(wdiff);
  981. *new = start + i;
  982. *stat = 0;
  983. return 0;
  984. }
  985. i += XFS_NBWORD;
  986. /*
  987. * Go on to next block if that's where the next word is
  988. * and we need the next word.
  989. */
  990. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  991. /*
  992. * If done with this block, get the next one.
  993. */
  994. xfs_trans_brelse(tp, bp);
  995. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  996. if (error) {
  997. return error;
  998. }
  999. b = bufp = bp->b_addr;
  1000. word = 0;
  1001. } else {
  1002. /*
  1003. * Go on to the next word in the buffer.
  1004. */
  1005. b++;
  1006. }
  1007. }
  1008. /*
  1009. * If not ending on a word boundary, deal with the last
  1010. * (partial) word.
  1011. */
  1012. if ((lastbit = len - i)) {
  1013. /*
  1014. * Mask of relevant bits.
  1015. */
  1016. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1017. /*
  1018. * Compute difference between actual and desired value.
  1019. */
  1020. if ((wdiff = (*b ^ val) & mask)) {
  1021. /*
  1022. * Different, compute first wrong bit and return.
  1023. */
  1024. xfs_trans_brelse(tp, bp);
  1025. i += XFS_RTLOBIT(wdiff);
  1026. *new = start + i;
  1027. *stat = 0;
  1028. return 0;
  1029. } else
  1030. i = len;
  1031. }
  1032. /*
  1033. * Successful, return.
  1034. */
  1035. xfs_trans_brelse(tp, bp);
  1036. *new = start + i;
  1037. *stat = 1;
  1038. return 0;
  1039. }
  1040. /*
  1041. * Copy and transform the summary file, given the old and new
  1042. * parameters in the mount structures.
  1043. */
  1044. STATIC int /* error */
  1045. xfs_rtcopy_summary(
  1046. xfs_mount_t *omp, /* old file system mount point */
  1047. xfs_mount_t *nmp, /* new file system mount point */
  1048. xfs_trans_t *tp) /* transaction pointer */
  1049. {
  1050. xfs_rtblock_t bbno; /* bitmap block number */
  1051. xfs_buf_t *bp; /* summary buffer */
  1052. int error; /* error return value */
  1053. int log; /* summary level number (log length) */
  1054. xfs_suminfo_t sum; /* summary data */
  1055. xfs_fsblock_t sumbno; /* summary block number */
  1056. bp = NULL;
  1057. for (log = omp->m_rsumlevels - 1; log >= 0; log--) {
  1058. for (bbno = omp->m_sb.sb_rbmblocks - 1;
  1059. (xfs_srtblock_t)bbno >= 0;
  1060. bbno--) {
  1061. error = xfs_rtget_summary(omp, tp, log, bbno, &bp,
  1062. &sumbno, &sum);
  1063. if (error)
  1064. return error;
  1065. if (sum == 0)
  1066. continue;
  1067. error = xfs_rtmodify_summary(omp, tp, log, bbno, -sum,
  1068. &bp, &sumbno);
  1069. if (error)
  1070. return error;
  1071. error = xfs_rtmodify_summary(nmp, tp, log, bbno, sum,
  1072. &bp, &sumbno);
  1073. if (error)
  1074. return error;
  1075. ASSERT(sum > 0);
  1076. }
  1077. }
  1078. return 0;
  1079. }
  1080. /*
  1081. * Searching backward from start to limit, find the first block whose
  1082. * allocated/free state is different from start's.
  1083. */
  1084. STATIC int /* error */
  1085. xfs_rtfind_back(
  1086. xfs_mount_t *mp, /* file system mount point */
  1087. xfs_trans_t *tp, /* transaction pointer */
  1088. xfs_rtblock_t start, /* starting block to look at */
  1089. xfs_rtblock_t limit, /* last block to look at */
  1090. xfs_rtblock_t *rtblock) /* out: start block found */
  1091. {
  1092. xfs_rtword_t *b; /* current word in buffer */
  1093. int bit; /* bit number in the word */
  1094. xfs_rtblock_t block; /* bitmap block number */
  1095. xfs_buf_t *bp; /* buf for the block */
  1096. xfs_rtword_t *bufp; /* starting word in buffer */
  1097. int error; /* error value */
  1098. xfs_rtblock_t firstbit; /* first useful bit in the word */
  1099. xfs_rtblock_t i; /* current bit number rel. to start */
  1100. xfs_rtblock_t len; /* length of inspected area */
  1101. xfs_rtword_t mask; /* mask of relevant bits for value */
  1102. xfs_rtword_t want; /* mask for "good" values */
  1103. xfs_rtword_t wdiff; /* difference from wanted value */
  1104. int word; /* word number in the buffer */
  1105. /*
  1106. * Compute and read in starting bitmap block for starting block.
  1107. */
  1108. block = XFS_BITTOBLOCK(mp, start);
  1109. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1110. if (error) {
  1111. return error;
  1112. }
  1113. bufp = bp->b_addr;
  1114. /*
  1115. * Get the first word's index & point to it.
  1116. */
  1117. word = XFS_BITTOWORD(mp, start);
  1118. b = &bufp[word];
  1119. bit = (int)(start & (XFS_NBWORD - 1));
  1120. len = start - limit + 1;
  1121. /*
  1122. * Compute match value, based on the bit at start: if 1 (free)
  1123. * then all-ones, else all-zeroes.
  1124. */
  1125. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1126. /*
  1127. * If the starting position is not word-aligned, deal with the
  1128. * partial word.
  1129. */
  1130. if (bit < XFS_NBWORD - 1) {
  1131. /*
  1132. * Calculate first (leftmost) bit number to look at,
  1133. * and mask for all the relevant bits in this word.
  1134. */
  1135. firstbit = XFS_RTMAX((xfs_srtblock_t)(bit - len + 1), 0);
  1136. mask = (((xfs_rtword_t)1 << (bit - firstbit + 1)) - 1) <<
  1137. firstbit;
  1138. /*
  1139. * Calculate the difference between the value there
  1140. * and what we're looking for.
  1141. */
  1142. if ((wdiff = (*b ^ want) & mask)) {
  1143. /*
  1144. * Different. Mark where we are and return.
  1145. */
  1146. xfs_trans_brelse(tp, bp);
  1147. i = bit - XFS_RTHIBIT(wdiff);
  1148. *rtblock = start - i + 1;
  1149. return 0;
  1150. }
  1151. i = bit - firstbit + 1;
  1152. /*
  1153. * Go on to previous block if that's where the previous word is
  1154. * and we need the previous word.
  1155. */
  1156. if (--word == -1 && i < len) {
  1157. /*
  1158. * If done with this block, get the previous one.
  1159. */
  1160. xfs_trans_brelse(tp, bp);
  1161. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1162. if (error) {
  1163. return error;
  1164. }
  1165. bufp = bp->b_addr;
  1166. word = XFS_BLOCKWMASK(mp);
  1167. b = &bufp[word];
  1168. } else {
  1169. /*
  1170. * Go on to the previous word in the buffer.
  1171. */
  1172. b--;
  1173. }
  1174. } else {
  1175. /*
  1176. * Starting on a word boundary, no partial word.
  1177. */
  1178. i = 0;
  1179. }
  1180. /*
  1181. * Loop over whole words in buffers. When we use up one buffer
  1182. * we move on to the previous one.
  1183. */
  1184. while (len - i >= XFS_NBWORD) {
  1185. /*
  1186. * Compute difference between actual and desired value.
  1187. */
  1188. if ((wdiff = *b ^ want)) {
  1189. /*
  1190. * Different, mark where we are and return.
  1191. */
  1192. xfs_trans_brelse(tp, bp);
  1193. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1194. *rtblock = start - i + 1;
  1195. return 0;
  1196. }
  1197. i += XFS_NBWORD;
  1198. /*
  1199. * Go on to previous block if that's where the previous word is
  1200. * and we need the previous word.
  1201. */
  1202. if (--word == -1 && i < len) {
  1203. /*
  1204. * If done with this block, get the previous one.
  1205. */
  1206. xfs_trans_brelse(tp, bp);
  1207. error = xfs_rtbuf_get(mp, tp, --block, 0, &bp);
  1208. if (error) {
  1209. return error;
  1210. }
  1211. bufp = bp->b_addr;
  1212. word = XFS_BLOCKWMASK(mp);
  1213. b = &bufp[word];
  1214. } else {
  1215. /*
  1216. * Go on to the previous word in the buffer.
  1217. */
  1218. b--;
  1219. }
  1220. }
  1221. /*
  1222. * If not ending on a word boundary, deal with the last
  1223. * (partial) word.
  1224. */
  1225. if (len - i) {
  1226. /*
  1227. * Calculate first (leftmost) bit number to look at,
  1228. * and mask for all the relevant bits in this word.
  1229. */
  1230. firstbit = XFS_NBWORD - (len - i);
  1231. mask = (((xfs_rtword_t)1 << (len - i)) - 1) << firstbit;
  1232. /*
  1233. * Compute difference between actual and desired value.
  1234. */
  1235. if ((wdiff = (*b ^ want) & mask)) {
  1236. /*
  1237. * Different, mark where we are and return.
  1238. */
  1239. xfs_trans_brelse(tp, bp);
  1240. i += XFS_NBWORD - 1 - XFS_RTHIBIT(wdiff);
  1241. *rtblock = start - i + 1;
  1242. return 0;
  1243. } else
  1244. i = len;
  1245. }
  1246. /*
  1247. * No match, return that we scanned the whole area.
  1248. */
  1249. xfs_trans_brelse(tp, bp);
  1250. *rtblock = start - i + 1;
  1251. return 0;
  1252. }
  1253. /*
  1254. * Searching forward from start to limit, find the first block whose
  1255. * allocated/free state is different from start's.
  1256. */
  1257. STATIC int /* error */
  1258. xfs_rtfind_forw(
  1259. xfs_mount_t *mp, /* file system mount point */
  1260. xfs_trans_t *tp, /* transaction pointer */
  1261. xfs_rtblock_t start, /* starting block to look at */
  1262. xfs_rtblock_t limit, /* last block to look at */
  1263. xfs_rtblock_t *rtblock) /* out: start block found */
  1264. {
  1265. xfs_rtword_t *b; /* current word in buffer */
  1266. int bit; /* bit number in the word */
  1267. xfs_rtblock_t block; /* bitmap block number */
  1268. xfs_buf_t *bp; /* buf for the block */
  1269. xfs_rtword_t *bufp; /* starting word in buffer */
  1270. int error; /* error value */
  1271. xfs_rtblock_t i; /* current bit number rel. to start */
  1272. xfs_rtblock_t lastbit; /* last useful bit in the word */
  1273. xfs_rtblock_t len; /* length of inspected area */
  1274. xfs_rtword_t mask; /* mask of relevant bits for value */
  1275. xfs_rtword_t want; /* mask for "good" values */
  1276. xfs_rtword_t wdiff; /* difference from wanted value */
  1277. int word; /* word number in the buffer */
  1278. /*
  1279. * Compute and read in starting bitmap block for starting block.
  1280. */
  1281. block = XFS_BITTOBLOCK(mp, start);
  1282. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1283. if (error) {
  1284. return error;
  1285. }
  1286. bufp = bp->b_addr;
  1287. /*
  1288. * Get the first word's index & point to it.
  1289. */
  1290. word = XFS_BITTOWORD(mp, start);
  1291. b = &bufp[word];
  1292. bit = (int)(start & (XFS_NBWORD - 1));
  1293. len = limit - start + 1;
  1294. /*
  1295. * Compute match value, based on the bit at start: if 1 (free)
  1296. * then all-ones, else all-zeroes.
  1297. */
  1298. want = (*b & ((xfs_rtword_t)1 << bit)) ? -1 : 0;
  1299. /*
  1300. * If the starting position is not word-aligned, deal with the
  1301. * partial word.
  1302. */
  1303. if (bit) {
  1304. /*
  1305. * Calculate last (rightmost) bit number to look at,
  1306. * and mask for all the relevant bits in this word.
  1307. */
  1308. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1309. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1310. /*
  1311. * Calculate the difference between the value there
  1312. * and what we're looking for.
  1313. */
  1314. if ((wdiff = (*b ^ want) & mask)) {
  1315. /*
  1316. * Different. Mark where we are and return.
  1317. */
  1318. xfs_trans_brelse(tp, bp);
  1319. i = XFS_RTLOBIT(wdiff) - bit;
  1320. *rtblock = start + i - 1;
  1321. return 0;
  1322. }
  1323. i = lastbit - bit;
  1324. /*
  1325. * Go on to next block if that's where the next word is
  1326. * and we need the next word.
  1327. */
  1328. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1329. /*
  1330. * If done with this block, get the previous one.
  1331. */
  1332. xfs_trans_brelse(tp, bp);
  1333. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1334. if (error) {
  1335. return error;
  1336. }
  1337. b = bufp = bp->b_addr;
  1338. word = 0;
  1339. } else {
  1340. /*
  1341. * Go on to the previous word in the buffer.
  1342. */
  1343. b++;
  1344. }
  1345. } else {
  1346. /*
  1347. * Starting on a word boundary, no partial word.
  1348. */
  1349. i = 0;
  1350. }
  1351. /*
  1352. * Loop over whole words in buffers. When we use up one buffer
  1353. * we move on to the next one.
  1354. */
  1355. while (len - i >= XFS_NBWORD) {
  1356. /*
  1357. * Compute difference between actual and desired value.
  1358. */
  1359. if ((wdiff = *b ^ want)) {
  1360. /*
  1361. * Different, mark where we are and return.
  1362. */
  1363. xfs_trans_brelse(tp, bp);
  1364. i += XFS_RTLOBIT(wdiff);
  1365. *rtblock = start + i - 1;
  1366. return 0;
  1367. }
  1368. i += XFS_NBWORD;
  1369. /*
  1370. * Go on to next block if that's where the next word is
  1371. * and we need the next word.
  1372. */
  1373. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1374. /*
  1375. * If done with this block, get the next one.
  1376. */
  1377. xfs_trans_brelse(tp, bp);
  1378. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1379. if (error) {
  1380. return error;
  1381. }
  1382. b = bufp = bp->b_addr;
  1383. word = 0;
  1384. } else {
  1385. /*
  1386. * Go on to the next word in the buffer.
  1387. */
  1388. b++;
  1389. }
  1390. }
  1391. /*
  1392. * If not ending on a word boundary, deal with the last
  1393. * (partial) word.
  1394. */
  1395. if ((lastbit = len - i)) {
  1396. /*
  1397. * Calculate mask for all the relevant bits in this word.
  1398. */
  1399. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1400. /*
  1401. * Compute difference between actual and desired value.
  1402. */
  1403. if ((wdiff = (*b ^ want) & mask)) {
  1404. /*
  1405. * Different, mark where we are and return.
  1406. */
  1407. xfs_trans_brelse(tp, bp);
  1408. i += XFS_RTLOBIT(wdiff);
  1409. *rtblock = start + i - 1;
  1410. return 0;
  1411. } else
  1412. i = len;
  1413. }
  1414. /*
  1415. * No match, return that we scanned the whole area.
  1416. */
  1417. xfs_trans_brelse(tp, bp);
  1418. *rtblock = start + i - 1;
  1419. return 0;
  1420. }
  1421. /*
  1422. * Mark an extent specified by start and len freed.
  1423. * Updates all the summary information as well as the bitmap.
  1424. */
  1425. STATIC int /* error */
  1426. xfs_rtfree_range(
  1427. xfs_mount_t *mp, /* file system mount point */
  1428. xfs_trans_t *tp, /* transaction pointer */
  1429. xfs_rtblock_t start, /* starting block to free */
  1430. xfs_extlen_t len, /* length to free */
  1431. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1432. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1433. {
  1434. xfs_rtblock_t end; /* end of the freed extent */
  1435. int error; /* error value */
  1436. xfs_rtblock_t postblock; /* first block freed > end */
  1437. xfs_rtblock_t preblock; /* first block freed < start */
  1438. end = start + len - 1;
  1439. /*
  1440. * Modify the bitmap to mark this extent freed.
  1441. */
  1442. error = xfs_rtmodify_range(mp, tp, start, len, 1);
  1443. if (error) {
  1444. return error;
  1445. }
  1446. /*
  1447. * Assume we're freeing out of the middle of an allocated extent.
  1448. * We need to find the beginning and end of the extent so we can
  1449. * properly update the summary.
  1450. */
  1451. error = xfs_rtfind_back(mp, tp, start, 0, &preblock);
  1452. if (error) {
  1453. return error;
  1454. }
  1455. /*
  1456. * Find the next allocated block (end of allocated extent).
  1457. */
  1458. error = xfs_rtfind_forw(mp, tp, end, mp->m_sb.sb_rextents - 1,
  1459. &postblock);
  1460. if (error)
  1461. return error;
  1462. /*
  1463. * If there are blocks not being freed at the front of the
  1464. * old extent, add summary data for them to be allocated.
  1465. */
  1466. if (preblock < start) {
  1467. error = xfs_rtmodify_summary(mp, tp,
  1468. XFS_RTBLOCKLOG(start - preblock),
  1469. XFS_BITTOBLOCK(mp, preblock), -1, rbpp, rsb);
  1470. if (error) {
  1471. return error;
  1472. }
  1473. }
  1474. /*
  1475. * If there are blocks not being freed at the end of the
  1476. * old extent, add summary data for them to be allocated.
  1477. */
  1478. if (postblock > end) {
  1479. error = xfs_rtmodify_summary(mp, tp,
  1480. XFS_RTBLOCKLOG(postblock - end),
  1481. XFS_BITTOBLOCK(mp, end + 1), -1, rbpp, rsb);
  1482. if (error) {
  1483. return error;
  1484. }
  1485. }
  1486. /*
  1487. * Increment the summary information corresponding to the entire
  1488. * (new) free extent.
  1489. */
  1490. error = xfs_rtmodify_summary(mp, tp,
  1491. XFS_RTBLOCKLOG(postblock + 1 - preblock),
  1492. XFS_BITTOBLOCK(mp, preblock), 1, rbpp, rsb);
  1493. return error;
  1494. }
  1495. /*
  1496. * Read and return the summary information for a given extent size,
  1497. * bitmap block combination.
  1498. * Keeps track of a current summary block, so we don't keep reading
  1499. * it from the buffer cache.
  1500. */
  1501. STATIC int /* error */
  1502. xfs_rtget_summary(
  1503. xfs_mount_t *mp, /* file system mount structure */
  1504. xfs_trans_t *tp, /* transaction pointer */
  1505. int log, /* log2 of extent size */
  1506. xfs_rtblock_t bbno, /* bitmap block number */
  1507. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1508. xfs_fsblock_t *rsb, /* in/out: summary block number */
  1509. xfs_suminfo_t *sum) /* out: summary info for this block */
  1510. {
  1511. xfs_buf_t *bp; /* buffer for summary block */
  1512. int error; /* error value */
  1513. xfs_fsblock_t sb; /* summary fsblock */
  1514. int so; /* index into the summary file */
  1515. xfs_suminfo_t *sp; /* pointer to returned data */
  1516. /*
  1517. * Compute entry number in the summary file.
  1518. */
  1519. so = XFS_SUMOFFS(mp, log, bbno);
  1520. /*
  1521. * Compute the block number in the summary file.
  1522. */
  1523. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1524. /*
  1525. * If we have an old buffer, and the block number matches, use that.
  1526. */
  1527. if (rbpp && *rbpp && *rsb == sb)
  1528. bp = *rbpp;
  1529. /*
  1530. * Otherwise we have to get the buffer.
  1531. */
  1532. else {
  1533. /*
  1534. * If there was an old one, get rid of it first.
  1535. */
  1536. if (rbpp && *rbpp)
  1537. xfs_trans_brelse(tp, *rbpp);
  1538. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1539. if (error) {
  1540. return error;
  1541. }
  1542. /*
  1543. * Remember this buffer and block for the next call.
  1544. */
  1545. if (rbpp) {
  1546. *rbpp = bp;
  1547. *rsb = sb;
  1548. }
  1549. }
  1550. /*
  1551. * Point to the summary information & copy it out.
  1552. */
  1553. sp = XFS_SUMPTR(mp, bp, so);
  1554. *sum = *sp;
  1555. /*
  1556. * Drop the buffer if we're not asked to remember it.
  1557. */
  1558. if (!rbpp)
  1559. xfs_trans_brelse(tp, bp);
  1560. return 0;
  1561. }
  1562. /*
  1563. * Set the given range of bitmap bits to the given value.
  1564. * Do whatever I/O and logging is required.
  1565. */
  1566. STATIC int /* error */
  1567. xfs_rtmodify_range(
  1568. xfs_mount_t *mp, /* file system mount point */
  1569. xfs_trans_t *tp, /* transaction pointer */
  1570. xfs_rtblock_t start, /* starting block to modify */
  1571. xfs_extlen_t len, /* length of extent to modify */
  1572. int val) /* 1 for free, 0 for allocated */
  1573. {
  1574. xfs_rtword_t *b; /* current word in buffer */
  1575. int bit; /* bit number in the word */
  1576. xfs_rtblock_t block; /* bitmap block number */
  1577. xfs_buf_t *bp; /* buf for the block */
  1578. xfs_rtword_t *bufp; /* starting word in buffer */
  1579. int error; /* error value */
  1580. xfs_rtword_t *first; /* first used word in the buffer */
  1581. int i; /* current bit number rel. to start */
  1582. int lastbit; /* last useful bit in word */
  1583. xfs_rtword_t mask; /* mask o frelevant bits for value */
  1584. int word; /* word number in the buffer */
  1585. /*
  1586. * Compute starting bitmap block number.
  1587. */
  1588. block = XFS_BITTOBLOCK(mp, start);
  1589. /*
  1590. * Read the bitmap block, and point to its data.
  1591. */
  1592. error = xfs_rtbuf_get(mp, tp, block, 0, &bp);
  1593. if (error) {
  1594. return error;
  1595. }
  1596. bufp = bp->b_addr;
  1597. /*
  1598. * Compute the starting word's address, and starting bit.
  1599. */
  1600. word = XFS_BITTOWORD(mp, start);
  1601. first = b = &bufp[word];
  1602. bit = (int)(start & (XFS_NBWORD - 1));
  1603. /*
  1604. * 0 (allocated) => all zeroes; 1 (free) => all ones.
  1605. */
  1606. val = -val;
  1607. /*
  1608. * If not starting on a word boundary, deal with the first
  1609. * (partial) word.
  1610. */
  1611. if (bit) {
  1612. /*
  1613. * Compute first bit not changed and mask of relevant bits.
  1614. */
  1615. lastbit = XFS_RTMIN(bit + len, XFS_NBWORD);
  1616. mask = (((xfs_rtword_t)1 << (lastbit - bit)) - 1) << bit;
  1617. /*
  1618. * Set/clear the active bits.
  1619. */
  1620. if (val)
  1621. *b |= mask;
  1622. else
  1623. *b &= ~mask;
  1624. i = lastbit - bit;
  1625. /*
  1626. * Go on to the next block if that's where the next word is
  1627. * and we need the next word.
  1628. */
  1629. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1630. /*
  1631. * Log the changed part of this block.
  1632. * Get the next one.
  1633. */
  1634. xfs_trans_log_buf(tp, bp,
  1635. (uint)((char *)first - (char *)bufp),
  1636. (uint)((char *)b - (char *)bufp));
  1637. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1638. if (error) {
  1639. return error;
  1640. }
  1641. first = b = bufp = bp->b_addr;
  1642. word = 0;
  1643. } else {
  1644. /*
  1645. * Go on to the next word in the buffer
  1646. */
  1647. b++;
  1648. }
  1649. } else {
  1650. /*
  1651. * Starting on a word boundary, no partial word.
  1652. */
  1653. i = 0;
  1654. }
  1655. /*
  1656. * Loop over whole words in buffers. When we use up one buffer
  1657. * we move on to the next one.
  1658. */
  1659. while (len - i >= XFS_NBWORD) {
  1660. /*
  1661. * Set the word value correctly.
  1662. */
  1663. *b = val;
  1664. i += XFS_NBWORD;
  1665. /*
  1666. * Go on to the next block if that's where the next word is
  1667. * and we need the next word.
  1668. */
  1669. if (++word == XFS_BLOCKWSIZE(mp) && i < len) {
  1670. /*
  1671. * Log the changed part of this block.
  1672. * Get the next one.
  1673. */
  1674. xfs_trans_log_buf(tp, bp,
  1675. (uint)((char *)first - (char *)bufp),
  1676. (uint)((char *)b - (char *)bufp));
  1677. error = xfs_rtbuf_get(mp, tp, ++block, 0, &bp);
  1678. if (error) {
  1679. return error;
  1680. }
  1681. first = b = bufp = bp->b_addr;
  1682. word = 0;
  1683. } else {
  1684. /*
  1685. * Go on to the next word in the buffer
  1686. */
  1687. b++;
  1688. }
  1689. }
  1690. /*
  1691. * If not ending on a word boundary, deal with the last
  1692. * (partial) word.
  1693. */
  1694. if ((lastbit = len - i)) {
  1695. /*
  1696. * Compute a mask of relevant bits.
  1697. */
  1698. bit = 0;
  1699. mask = ((xfs_rtword_t)1 << lastbit) - 1;
  1700. /*
  1701. * Set/clear the active bits.
  1702. */
  1703. if (val)
  1704. *b |= mask;
  1705. else
  1706. *b &= ~mask;
  1707. b++;
  1708. }
  1709. /*
  1710. * Log any remaining changed bytes.
  1711. */
  1712. if (b > first)
  1713. xfs_trans_log_buf(tp, bp, (uint)((char *)first - (char *)bufp),
  1714. (uint)((char *)b - (char *)bufp - 1));
  1715. return 0;
  1716. }
  1717. /*
  1718. * Read and modify the summary information for a given extent size,
  1719. * bitmap block combination.
  1720. * Keeps track of a current summary block, so we don't keep reading
  1721. * it from the buffer cache.
  1722. */
  1723. STATIC int /* error */
  1724. xfs_rtmodify_summary(
  1725. xfs_mount_t *mp, /* file system mount point */
  1726. xfs_trans_t *tp, /* transaction pointer */
  1727. int log, /* log2 of extent size */
  1728. xfs_rtblock_t bbno, /* bitmap block number */
  1729. int delta, /* change to make to summary info */
  1730. xfs_buf_t **rbpp, /* in/out: summary block buffer */
  1731. xfs_fsblock_t *rsb) /* in/out: summary block number */
  1732. {
  1733. xfs_buf_t *bp; /* buffer for the summary block */
  1734. int error; /* error value */
  1735. xfs_fsblock_t sb; /* summary fsblock */
  1736. int so; /* index into the summary file */
  1737. xfs_suminfo_t *sp; /* pointer to returned data */
  1738. /*
  1739. * Compute entry number in the summary file.
  1740. */
  1741. so = XFS_SUMOFFS(mp, log, bbno);
  1742. /*
  1743. * Compute the block number in the summary file.
  1744. */
  1745. sb = XFS_SUMOFFSTOBLOCK(mp, so);
  1746. /*
  1747. * If we have an old buffer, and the block number matches, use that.
  1748. */
  1749. if (rbpp && *rbpp && *rsb == sb)
  1750. bp = *rbpp;
  1751. /*
  1752. * Otherwise we have to get the buffer.
  1753. */
  1754. else {
  1755. /*
  1756. * If there was an old one, get rid of it first.
  1757. */
  1758. if (rbpp && *rbpp)
  1759. xfs_trans_brelse(tp, *rbpp);
  1760. error = xfs_rtbuf_get(mp, tp, sb, 1, &bp);
  1761. if (error) {
  1762. return error;
  1763. }
  1764. /*
  1765. * Remember this buffer and block for the next call.
  1766. */
  1767. if (rbpp) {
  1768. *rbpp = bp;
  1769. *rsb = sb;
  1770. }
  1771. }
  1772. /*
  1773. * Point to the summary information, modify and log it.
  1774. */
  1775. sp = XFS_SUMPTR(mp, bp, so);
  1776. *sp += delta;
  1777. xfs_trans_log_buf(tp, bp, (uint)((char *)sp - (char *)bp->b_addr),
  1778. (uint)((char *)sp - (char *)bp->b_addr + sizeof(*sp) - 1));
  1779. return 0;
  1780. }
  1781. /*
  1782. * Visible (exported) functions.
  1783. */
  1784. /*
  1785. * Grow the realtime area of the filesystem.
  1786. */
  1787. int
  1788. xfs_growfs_rt(
  1789. xfs_mount_t *mp, /* mount point for filesystem */
  1790. xfs_growfs_rt_t *in) /* growfs rt input struct */
  1791. {
  1792. xfs_rtblock_t bmbno; /* bitmap block number */
  1793. xfs_buf_t *bp; /* temporary buffer */
  1794. int error; /* error return value */
  1795. xfs_mount_t *nmp; /* new (fake) mount structure */
  1796. xfs_drfsbno_t nrblocks; /* new number of realtime blocks */
  1797. xfs_extlen_t nrbmblocks; /* new number of rt bitmap blocks */
  1798. xfs_drtbno_t nrextents; /* new number of realtime extents */
  1799. uint8_t nrextslog; /* new log2 of sb_rextents */
  1800. xfs_extlen_t nrsumblocks; /* new number of summary blocks */
  1801. uint nrsumlevels; /* new rt summary levels */
  1802. uint nrsumsize; /* new size of rt summary, bytes */
  1803. xfs_sb_t *nsbp; /* new superblock */
  1804. xfs_extlen_t rbmblocks; /* current number of rt bitmap blocks */
  1805. xfs_extlen_t rsumblocks; /* current number of rt summary blks */
  1806. xfs_sb_t *sbp; /* old superblock */
  1807. xfs_fsblock_t sumbno; /* summary block number */
  1808. sbp = &mp->m_sb;
  1809. /*
  1810. * Initial error checking.
  1811. */
  1812. if (!capable(CAP_SYS_ADMIN))
  1813. return XFS_ERROR(EPERM);
  1814. if (mp->m_rtdev_targp == NULL || mp->m_rbmip == NULL ||
  1815. (nrblocks = in->newblocks) <= sbp->sb_rblocks ||
  1816. (sbp->sb_rblocks && (in->extsize != sbp->sb_rextsize)))
  1817. return XFS_ERROR(EINVAL);
  1818. if ((error = xfs_sb_validate_fsb_count(sbp, nrblocks)))
  1819. return error;
  1820. /*
  1821. * Read in the last block of the device, make sure it exists.
  1822. */
  1823. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  1824. XFS_FSB_TO_BB(mp, nrblocks - 1),
  1825. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  1826. if (!bp)
  1827. return EIO;
  1828. if (bp->b_error) {
  1829. error = bp->b_error;
  1830. xfs_buf_relse(bp);
  1831. return error;
  1832. }
  1833. xfs_buf_relse(bp);
  1834. /*
  1835. * Calculate new parameters. These are the final values to be reached.
  1836. */
  1837. nrextents = nrblocks;
  1838. do_div(nrextents, in->extsize);
  1839. nrbmblocks = howmany_64(nrextents, NBBY * sbp->sb_blocksize);
  1840. nrextslog = xfs_highbit32(nrextents);
  1841. nrsumlevels = nrextslog + 1;
  1842. nrsumsize = (uint)sizeof(xfs_suminfo_t) * nrsumlevels * nrbmblocks;
  1843. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1844. nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1845. /*
  1846. * New summary size can't be more than half the size of
  1847. * the log. This prevents us from getting a log overflow,
  1848. * since we'll log basically the whole summary file at once.
  1849. */
  1850. if (nrsumblocks > (mp->m_sb.sb_logblocks >> 1))
  1851. return XFS_ERROR(EINVAL);
  1852. /*
  1853. * Get the old block counts for bitmap and summary inodes.
  1854. * These can't change since other growfs callers are locked out.
  1855. */
  1856. rbmblocks = XFS_B_TO_FSB(mp, mp->m_rbmip->i_d.di_size);
  1857. rsumblocks = XFS_B_TO_FSB(mp, mp->m_rsumip->i_d.di_size);
  1858. /*
  1859. * Allocate space to the bitmap and summary files, as necessary.
  1860. */
  1861. error = xfs_growfs_rt_alloc(mp, rbmblocks, nrbmblocks, mp->m_rbmip);
  1862. if (error)
  1863. return error;
  1864. error = xfs_growfs_rt_alloc(mp, rsumblocks, nrsumblocks, mp->m_rsumip);
  1865. if (error)
  1866. return error;
  1867. /*
  1868. * Allocate a new (fake) mount/sb.
  1869. */
  1870. nmp = kmem_alloc(sizeof(*nmp), KM_SLEEP);
  1871. /*
  1872. * Loop over the bitmap blocks.
  1873. * We will do everything one bitmap block at a time.
  1874. * Skip the current block if it is exactly full.
  1875. * This also deals with the case where there were no rtextents before.
  1876. */
  1877. for (bmbno = sbp->sb_rbmblocks -
  1878. ((sbp->sb_rextents & ((1 << mp->m_blkbit_log) - 1)) != 0);
  1879. bmbno < nrbmblocks;
  1880. bmbno++) {
  1881. xfs_trans_t *tp;
  1882. int cancelflags = 0;
  1883. *nmp = *mp;
  1884. nsbp = &nmp->m_sb;
  1885. /*
  1886. * Calculate new sb and mount fields for this round.
  1887. */
  1888. nsbp->sb_rextsize = in->extsize;
  1889. nsbp->sb_rbmblocks = bmbno + 1;
  1890. nsbp->sb_rblocks =
  1891. XFS_RTMIN(nrblocks,
  1892. nsbp->sb_rbmblocks * NBBY *
  1893. nsbp->sb_blocksize * nsbp->sb_rextsize);
  1894. nsbp->sb_rextents = nsbp->sb_rblocks;
  1895. do_div(nsbp->sb_rextents, nsbp->sb_rextsize);
  1896. ASSERT(nsbp->sb_rextents != 0);
  1897. nsbp->sb_rextslog = xfs_highbit32(nsbp->sb_rextents);
  1898. nrsumlevels = nmp->m_rsumlevels = nsbp->sb_rextslog + 1;
  1899. nrsumsize =
  1900. (uint)sizeof(xfs_suminfo_t) * nrsumlevels *
  1901. nsbp->sb_rbmblocks;
  1902. nrsumblocks = XFS_B_TO_FSB(mp, nrsumsize);
  1903. nmp->m_rsumsize = nrsumsize = XFS_FSB_TO_B(mp, nrsumblocks);
  1904. /*
  1905. * Start a transaction, get the log reservation.
  1906. */
  1907. tp = xfs_trans_alloc(mp, XFS_TRANS_GROWFSRT_FREE);
  1908. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_growrtfree,
  1909. 0, 0);
  1910. if (error)
  1911. goto error_cancel;
  1912. /*
  1913. * Lock out other callers by grabbing the bitmap inode lock.
  1914. */
  1915. xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
  1916. xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
  1917. /*
  1918. * Update the bitmap inode's size.
  1919. */
  1920. mp->m_rbmip->i_d.di_size =
  1921. nsbp->sb_rbmblocks * nsbp->sb_blocksize;
  1922. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  1923. cancelflags |= XFS_TRANS_ABORT;
  1924. /*
  1925. * Get the summary inode into the transaction.
  1926. */
  1927. xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL);
  1928. xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
  1929. /*
  1930. * Update the summary inode's size.
  1931. */
  1932. mp->m_rsumip->i_d.di_size = nmp->m_rsumsize;
  1933. xfs_trans_log_inode(tp, mp->m_rsumip, XFS_ILOG_CORE);
  1934. /*
  1935. * Copy summary data from old to new sizes.
  1936. * Do this when the real size (not block-aligned) changes.
  1937. */
  1938. if (sbp->sb_rbmblocks != nsbp->sb_rbmblocks ||
  1939. mp->m_rsumlevels != nmp->m_rsumlevels) {
  1940. error = xfs_rtcopy_summary(mp, nmp, tp);
  1941. if (error)
  1942. goto error_cancel;
  1943. }
  1944. /*
  1945. * Update superblock fields.
  1946. */
  1947. if (nsbp->sb_rextsize != sbp->sb_rextsize)
  1948. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSIZE,
  1949. nsbp->sb_rextsize - sbp->sb_rextsize);
  1950. if (nsbp->sb_rbmblocks != sbp->sb_rbmblocks)
  1951. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBMBLOCKS,
  1952. nsbp->sb_rbmblocks - sbp->sb_rbmblocks);
  1953. if (nsbp->sb_rblocks != sbp->sb_rblocks)
  1954. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RBLOCKS,
  1955. nsbp->sb_rblocks - sbp->sb_rblocks);
  1956. if (nsbp->sb_rextents != sbp->sb_rextents)
  1957. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTENTS,
  1958. nsbp->sb_rextents - sbp->sb_rextents);
  1959. if (nsbp->sb_rextslog != sbp->sb_rextslog)
  1960. xfs_trans_mod_sb(tp, XFS_TRANS_SB_REXTSLOG,
  1961. nsbp->sb_rextslog - sbp->sb_rextslog);
  1962. /*
  1963. * Free new extent.
  1964. */
  1965. bp = NULL;
  1966. error = xfs_rtfree_range(nmp, tp, sbp->sb_rextents,
  1967. nsbp->sb_rextents - sbp->sb_rextents, &bp, &sumbno);
  1968. if (error) {
  1969. error_cancel:
  1970. xfs_trans_cancel(tp, cancelflags);
  1971. break;
  1972. }
  1973. /*
  1974. * Mark more blocks free in the superblock.
  1975. */
  1976. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS,
  1977. nsbp->sb_rextents - sbp->sb_rextents);
  1978. /*
  1979. * Update mp values into the real mp structure.
  1980. */
  1981. mp->m_rsumlevels = nrsumlevels;
  1982. mp->m_rsumsize = nrsumsize;
  1983. error = xfs_trans_commit(tp, 0);
  1984. if (error)
  1985. break;
  1986. }
  1987. /*
  1988. * Free the fake mp structure.
  1989. */
  1990. kmem_free(nmp);
  1991. return error;
  1992. }
  1993. /*
  1994. * Allocate an extent in the realtime subvolume, with the usual allocation
  1995. * parameters. The length units are all in realtime extents, as is the
  1996. * result block number.
  1997. */
  1998. int /* error */
  1999. xfs_rtallocate_extent(
  2000. xfs_trans_t *tp, /* transaction pointer */
  2001. xfs_rtblock_t bno, /* starting block number to allocate */
  2002. xfs_extlen_t minlen, /* minimum length to allocate */
  2003. xfs_extlen_t maxlen, /* maximum length to allocate */
  2004. xfs_extlen_t *len, /* out: actual length allocated */
  2005. xfs_alloctype_t type, /* allocation type XFS_ALLOCTYPE... */
  2006. int wasdel, /* was a delayed allocation extent */
  2007. xfs_extlen_t prod, /* extent product factor */
  2008. xfs_rtblock_t *rtblock) /* out: start block allocated */
  2009. {
  2010. xfs_mount_t *mp = tp->t_mountp;
  2011. int error; /* error value */
  2012. xfs_rtblock_t r; /* result allocated block */
  2013. xfs_fsblock_t sb; /* summary file block number */
  2014. xfs_buf_t *sumbp; /* summary file block buffer */
  2015. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2016. ASSERT(minlen > 0 && minlen <= maxlen);
  2017. /*
  2018. * If prod is set then figure out what to do to minlen and maxlen.
  2019. */
  2020. if (prod > 1) {
  2021. xfs_extlen_t i;
  2022. if ((i = maxlen % prod))
  2023. maxlen -= i;
  2024. if ((i = minlen % prod))
  2025. minlen += prod - i;
  2026. if (maxlen < minlen) {
  2027. *rtblock = NULLRTBLOCK;
  2028. return 0;
  2029. }
  2030. }
  2031. sumbp = NULL;
  2032. /*
  2033. * Allocate by size, or near another block, or exactly at some block.
  2034. */
  2035. switch (type) {
  2036. case XFS_ALLOCTYPE_ANY_AG:
  2037. error = xfs_rtallocate_extent_size(mp, tp, minlen, maxlen, len,
  2038. &sumbp, &sb, prod, &r);
  2039. break;
  2040. case XFS_ALLOCTYPE_NEAR_BNO:
  2041. error = xfs_rtallocate_extent_near(mp, tp, bno, minlen, maxlen,
  2042. len, &sumbp, &sb, prod, &r);
  2043. break;
  2044. case XFS_ALLOCTYPE_THIS_BNO:
  2045. error = xfs_rtallocate_extent_exact(mp, tp, bno, minlen, maxlen,
  2046. len, &sumbp, &sb, prod, &r);
  2047. break;
  2048. default:
  2049. error = EIO;
  2050. ASSERT(0);
  2051. }
  2052. if (error)
  2053. return error;
  2054. /*
  2055. * If it worked, update the superblock.
  2056. */
  2057. if (r != NULLRTBLOCK) {
  2058. long slen = (long)*len;
  2059. ASSERT(*len >= minlen && *len <= maxlen);
  2060. if (wasdel)
  2061. xfs_trans_mod_sb(tp, XFS_TRANS_SB_RES_FREXTENTS, -slen);
  2062. else
  2063. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, -slen);
  2064. }
  2065. *rtblock = r;
  2066. return 0;
  2067. }
  2068. /*
  2069. * Free an extent in the realtime subvolume. Length is expressed in
  2070. * realtime extents, as is the block number.
  2071. */
  2072. int /* error */
  2073. xfs_rtfree_extent(
  2074. xfs_trans_t *tp, /* transaction pointer */
  2075. xfs_rtblock_t bno, /* starting block number to free */
  2076. xfs_extlen_t len) /* length of extent freed */
  2077. {
  2078. int error; /* error value */
  2079. xfs_mount_t *mp; /* file system mount structure */
  2080. xfs_fsblock_t sb; /* summary file block number */
  2081. xfs_buf_t *sumbp; /* summary file block buffer */
  2082. mp = tp->t_mountp;
  2083. ASSERT(mp->m_rbmip->i_itemp != NULL);
  2084. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2085. #ifdef DEBUG
  2086. /*
  2087. * Check to see that this whole range is currently allocated.
  2088. */
  2089. {
  2090. int stat; /* result from checking range */
  2091. error = xfs_rtcheck_alloc_range(mp, tp, bno, len, &stat);
  2092. if (error) {
  2093. return error;
  2094. }
  2095. ASSERT(stat);
  2096. }
  2097. #endif
  2098. sumbp = NULL;
  2099. /*
  2100. * Free the range of realtime blocks.
  2101. */
  2102. error = xfs_rtfree_range(mp, tp, bno, len, &sumbp, &sb);
  2103. if (error) {
  2104. return error;
  2105. }
  2106. /*
  2107. * Mark more blocks free in the superblock.
  2108. */
  2109. xfs_trans_mod_sb(tp, XFS_TRANS_SB_FREXTENTS, (long)len);
  2110. /*
  2111. * If we've now freed all the blocks, reset the file sequence
  2112. * number to 0.
  2113. */
  2114. if (tp->t_frextents_delta + mp->m_sb.sb_frextents ==
  2115. mp->m_sb.sb_rextents) {
  2116. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM))
  2117. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2118. *(__uint64_t *)&mp->m_rbmip->i_d.di_atime = 0;
  2119. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  2120. }
  2121. return 0;
  2122. }
  2123. /*
  2124. * Initialize realtime fields in the mount structure.
  2125. */
  2126. int /* error */
  2127. xfs_rtmount_init(
  2128. xfs_mount_t *mp) /* file system mount structure */
  2129. {
  2130. xfs_buf_t *bp; /* buffer for last block of subvolume */
  2131. xfs_daddr_t d; /* address of last block of subvolume */
  2132. xfs_sb_t *sbp; /* filesystem superblock copy in mount */
  2133. sbp = &mp->m_sb;
  2134. if (sbp->sb_rblocks == 0)
  2135. return 0;
  2136. if (mp->m_rtdev_targp == NULL) {
  2137. xfs_warn(mp,
  2138. "Filesystem has a realtime volume, use rtdev=device option");
  2139. return XFS_ERROR(ENODEV);
  2140. }
  2141. mp->m_rsumlevels = sbp->sb_rextslog + 1;
  2142. mp->m_rsumsize =
  2143. (uint)sizeof(xfs_suminfo_t) * mp->m_rsumlevels *
  2144. sbp->sb_rbmblocks;
  2145. mp->m_rsumsize = roundup(mp->m_rsumsize, sbp->sb_blocksize);
  2146. mp->m_rbmip = mp->m_rsumip = NULL;
  2147. /*
  2148. * Check that the realtime section is an ok size.
  2149. */
  2150. d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
  2151. if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_rblocks) {
  2152. xfs_warn(mp, "realtime mount -- %llu != %llu",
  2153. (unsigned long long) XFS_BB_TO_FSB(mp, d),
  2154. (unsigned long long) mp->m_sb.sb_rblocks);
  2155. return XFS_ERROR(EFBIG);
  2156. }
  2157. bp = xfs_buf_read_uncached(mp->m_rtdev_targp,
  2158. d - XFS_FSB_TO_BB(mp, 1),
  2159. XFS_FSB_TO_BB(mp, 1), 0, NULL);
  2160. if (!bp || bp->b_error) {
  2161. xfs_warn(mp, "realtime device size check failed");
  2162. if (bp)
  2163. xfs_buf_relse(bp);
  2164. return EIO;
  2165. }
  2166. xfs_buf_relse(bp);
  2167. return 0;
  2168. }
  2169. /*
  2170. * Get the bitmap and summary inodes into the mount structure
  2171. * at mount time.
  2172. */
  2173. int /* error */
  2174. xfs_rtmount_inodes(
  2175. xfs_mount_t *mp) /* file system mount structure */
  2176. {
  2177. int error; /* error return value */
  2178. xfs_sb_t *sbp;
  2179. sbp = &mp->m_sb;
  2180. if (sbp->sb_rbmino == NULLFSINO)
  2181. return 0;
  2182. error = xfs_iget(mp, NULL, sbp->sb_rbmino, 0, 0, &mp->m_rbmip);
  2183. if (error)
  2184. return error;
  2185. ASSERT(mp->m_rbmip != NULL);
  2186. ASSERT(sbp->sb_rsumino != NULLFSINO);
  2187. error = xfs_iget(mp, NULL, sbp->sb_rsumino, 0, 0, &mp->m_rsumip);
  2188. if (error) {
  2189. IRELE(mp->m_rbmip);
  2190. return error;
  2191. }
  2192. ASSERT(mp->m_rsumip != NULL);
  2193. return 0;
  2194. }
  2195. void
  2196. xfs_rtunmount_inodes(
  2197. struct xfs_mount *mp)
  2198. {
  2199. if (mp->m_rbmip)
  2200. IRELE(mp->m_rbmip);
  2201. if (mp->m_rsumip)
  2202. IRELE(mp->m_rsumip);
  2203. }
  2204. /*
  2205. * Pick an extent for allocation at the start of a new realtime file.
  2206. * Use the sequence number stored in the atime field of the bitmap inode.
  2207. * Translate this to a fraction of the rtextents, and return the product
  2208. * of rtextents and the fraction.
  2209. * The fraction sequence is 0, 1/2, 1/4, 3/4, 1/8, ..., 7/8, 1/16, ...
  2210. */
  2211. int /* error */
  2212. xfs_rtpick_extent(
  2213. xfs_mount_t *mp, /* file system mount point */
  2214. xfs_trans_t *tp, /* transaction pointer */
  2215. xfs_extlen_t len, /* allocation length (rtextents) */
  2216. xfs_rtblock_t *pick) /* result rt extent */
  2217. {
  2218. xfs_rtblock_t b; /* result block */
  2219. int log2; /* log of sequence number */
  2220. __uint64_t resid; /* residual after log removed */
  2221. __uint64_t seq; /* sequence number of file creation */
  2222. __uint64_t *seqp; /* pointer to seqno in inode */
  2223. ASSERT(xfs_isilocked(mp->m_rbmip, XFS_ILOCK_EXCL));
  2224. seqp = (__uint64_t *)&mp->m_rbmip->i_d.di_atime;
  2225. if (!(mp->m_rbmip->i_d.di_flags & XFS_DIFLAG_NEWRTBM)) {
  2226. mp->m_rbmip->i_d.di_flags |= XFS_DIFLAG_NEWRTBM;
  2227. *seqp = 0;
  2228. }
  2229. seq = *seqp;
  2230. if ((log2 = xfs_highbit64(seq)) == -1)
  2231. b = 0;
  2232. else {
  2233. resid = seq - (1ULL << log2);
  2234. b = (mp->m_sb.sb_rextents * ((resid << 1) + 1ULL)) >>
  2235. (log2 + 1);
  2236. if (b >= mp->m_sb.sb_rextents)
  2237. b = do_mod(b, mp->m_sb.sb_rextents);
  2238. if (b + len > mp->m_sb.sb_rextents)
  2239. b = mp->m_sb.sb_rextents - len;
  2240. }
  2241. *seqp = seq + 1;
  2242. xfs_trans_log_inode(tp, mp->m_rbmip, XFS_ILOG_CORE);
  2243. *pick = b;
  2244. return 0;
  2245. }