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