xfs_rtalloc.c 62 KB

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