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