xfs_rtalloc.c 62 KB

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