xfs_rtalloc.c 65 KB

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