xfs_rtalloc.c 62 KB

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