xfs_rtalloc.c 61 KB

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