xfs_rtalloc.c 62 KB

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