xfs_rtalloc.c 65 KB

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