xfs_rtalloc.c 65 KB

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