xfs_rtalloc.c 62 KB

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