xfs_rtalloc.c 62 KB

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