xfs_rtalloc.c 66 KB

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