xfs_rtalloc.c 62 KB

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