xfs_rtalloc.c 61 KB

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