xfs_rtalloc.c 61 KB

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