xfs_rtalloc.c 61 KB

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