xfs_rtalloc.c 62 KB

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