suballoc.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * suballoc.c
  5. *
  6. * metadata alloc and free
  7. * Inspired by ext3 block groups.
  8. *
  9. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "dlmglue.h"
  35. #include "inode.h"
  36. #include "journal.h"
  37. #include "localalloc.h"
  38. #include "suballoc.h"
  39. #include "super.h"
  40. #include "sysfile.h"
  41. #include "uptodate.h"
  42. #include "buffer_head_io.h"
  43. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
  44. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
  45. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
  46. static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
  47. struct inode *alloc_inode,
  48. struct buffer_head *bg_bh,
  49. u64 group_blkno,
  50. u16 my_chain,
  51. struct ocfs2_chain_list *cl);
  52. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  53. struct inode *alloc_inode,
  54. struct buffer_head *bh);
  55. static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
  56. struct ocfs2_alloc_context *ac);
  57. static int ocfs2_cluster_group_search(struct inode *inode,
  58. struct buffer_head *group_bh,
  59. u32 bits_wanted, u32 min_bits,
  60. u16 *bit_off, u16 *bits_found);
  61. static int ocfs2_block_group_search(struct inode *inode,
  62. struct buffer_head *group_bh,
  63. u32 bits_wanted, u32 min_bits,
  64. u16 *bit_off, u16 *bits_found);
  65. static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  66. struct ocfs2_alloc_context *ac,
  67. u32 bits_wanted,
  68. u32 min_bits,
  69. u16 *bit_off,
  70. unsigned int *num_bits,
  71. u64 *bg_blkno);
  72. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  73. int nr);
  74. static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *handle,
  75. struct inode *alloc_inode,
  76. struct ocfs2_group_desc *bg,
  77. struct buffer_head *group_bh,
  78. unsigned int bit_off,
  79. unsigned int num_bits);
  80. static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
  81. struct inode *alloc_inode,
  82. struct ocfs2_group_desc *bg,
  83. struct buffer_head *group_bh,
  84. unsigned int bit_off,
  85. unsigned int num_bits);
  86. static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
  87. struct inode *alloc_inode,
  88. struct buffer_head *fe_bh,
  89. struct buffer_head *bg_bh,
  90. struct buffer_head *prev_bg_bh,
  91. u16 chain);
  92. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  93. u32 wanted);
  94. static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
  95. struct inode *alloc_inode,
  96. struct buffer_head *alloc_bh,
  97. unsigned int start_bit,
  98. u64 bg_blkno,
  99. unsigned int count);
  100. static inline u64 ocfs2_which_suballoc_group(u64 block,
  101. unsigned int bit);
  102. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  103. u64 bg_blkno,
  104. u16 bg_bit_off);
  105. static inline u64 ocfs2_which_cluster_group(struct inode *inode,
  106. u32 cluster);
  107. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  108. u64 data_blkno,
  109. u64 *bg_blkno,
  110. u16 *bg_bit_off);
  111. void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
  112. {
  113. if (ac->ac_inode)
  114. iput(ac->ac_inode);
  115. if (ac->ac_bh)
  116. brelse(ac->ac_bh);
  117. kfree(ac);
  118. }
  119. static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
  120. {
  121. return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
  122. }
  123. /* somewhat more expensive than our other checks, so use sparingly. */
  124. static int ocfs2_check_group_descriptor(struct super_block *sb,
  125. struct ocfs2_dinode *di,
  126. struct ocfs2_group_desc *gd)
  127. {
  128. unsigned int max_bits;
  129. if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
  130. OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
  131. return -EIO;
  132. }
  133. if (di->i_blkno != gd->bg_parent_dinode) {
  134. ocfs2_error(sb, "Group descriptor # %llu has bad parent "
  135. "pointer (%llu, expected %llu)",
  136. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  137. (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
  138. (unsigned long long)le64_to_cpu(di->i_blkno));
  139. return -EIO;
  140. }
  141. max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
  142. if (le16_to_cpu(gd->bg_bits) > max_bits) {
  143. ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
  144. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  145. le16_to_cpu(gd->bg_bits));
  146. return -EIO;
  147. }
  148. if (le16_to_cpu(gd->bg_chain) >=
  149. le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
  150. ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
  151. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  152. le16_to_cpu(gd->bg_chain));
  153. return -EIO;
  154. }
  155. if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
  156. ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
  157. "claims that %u are free",
  158. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  159. le16_to_cpu(gd->bg_bits),
  160. le16_to_cpu(gd->bg_free_bits_count));
  161. return -EIO;
  162. }
  163. if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
  164. ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
  165. "max bitmap bits of %u",
  166. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  167. le16_to_cpu(gd->bg_bits),
  168. 8 * le16_to_cpu(gd->bg_size));
  169. return -EIO;
  170. }
  171. return 0;
  172. }
  173. static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
  174. struct inode *alloc_inode,
  175. struct buffer_head *bg_bh,
  176. u64 group_blkno,
  177. u16 my_chain,
  178. struct ocfs2_chain_list *cl)
  179. {
  180. int status = 0;
  181. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  182. struct super_block * sb = alloc_inode->i_sb;
  183. mlog_entry_void();
  184. if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
  185. ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
  186. "b_blocknr (%llu)",
  187. (unsigned long long)group_blkno,
  188. (unsigned long long) bg_bh->b_blocknr);
  189. status = -EIO;
  190. goto bail;
  191. }
  192. status = ocfs2_journal_access(handle,
  193. alloc_inode,
  194. bg_bh,
  195. OCFS2_JOURNAL_ACCESS_CREATE);
  196. if (status < 0) {
  197. mlog_errno(status);
  198. goto bail;
  199. }
  200. memset(bg, 0, sb->s_blocksize);
  201. strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
  202. bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  203. bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
  204. bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  205. bg->bg_chain = cpu_to_le16(my_chain);
  206. bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
  207. bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
  208. bg->bg_blkno = cpu_to_le64(group_blkno);
  209. /* set the 1st bit in the bitmap to account for the descriptor block */
  210. ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
  211. bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
  212. status = ocfs2_journal_dirty(handle, bg_bh);
  213. if (status < 0)
  214. mlog_errno(status);
  215. /* There is no need to zero out or otherwise initialize the
  216. * other blocks in a group - All valid FS metadata in a block
  217. * group stores the superblock fs_generation value at
  218. * allocation time. */
  219. bail:
  220. mlog_exit(status);
  221. return status;
  222. }
  223. static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
  224. {
  225. u16 curr, best;
  226. best = curr = 0;
  227. while (curr < le16_to_cpu(cl->cl_count)) {
  228. if (le32_to_cpu(cl->cl_recs[best].c_total) >
  229. le32_to_cpu(cl->cl_recs[curr].c_total))
  230. best = curr;
  231. curr++;
  232. }
  233. return best;
  234. }
  235. /*
  236. * We expect the block group allocator to already be locked.
  237. */
  238. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  239. struct inode *alloc_inode,
  240. struct buffer_head *bh)
  241. {
  242. int status, credits;
  243. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  244. struct ocfs2_chain_list *cl;
  245. struct ocfs2_alloc_context *ac = NULL;
  246. struct ocfs2_journal_handle *handle = NULL;
  247. u32 bit_off, num_bits;
  248. u16 alloc_rec;
  249. u64 bg_blkno;
  250. struct buffer_head *bg_bh = NULL;
  251. struct ocfs2_group_desc *bg;
  252. BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
  253. mlog_entry_void();
  254. handle = ocfs2_alloc_handle(osb);
  255. if (!handle) {
  256. status = -ENOMEM;
  257. mlog_errno(status);
  258. goto bail;
  259. }
  260. cl = &fe->id2.i_chain;
  261. status = ocfs2_reserve_clusters(osb,
  262. handle,
  263. le16_to_cpu(cl->cl_cpg),
  264. &ac);
  265. if (status < 0) {
  266. if (status != -ENOSPC)
  267. mlog_errno(status);
  268. goto bail;
  269. }
  270. credits = ocfs2_calc_group_alloc_credits(osb->sb,
  271. le16_to_cpu(cl->cl_cpg));
  272. handle = ocfs2_start_trans(osb, handle, credits);
  273. if (IS_ERR(handle)) {
  274. status = PTR_ERR(handle);
  275. handle = NULL;
  276. mlog_errno(status);
  277. goto bail;
  278. }
  279. status = ocfs2_claim_clusters(osb,
  280. handle,
  281. ac,
  282. le16_to_cpu(cl->cl_cpg),
  283. &bit_off,
  284. &num_bits);
  285. if (status < 0) {
  286. if (status != -ENOSPC)
  287. mlog_errno(status);
  288. goto bail;
  289. }
  290. alloc_rec = ocfs2_find_smallest_chain(cl);
  291. /* setup the group */
  292. bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  293. mlog(0, "new descriptor, record %u, at block %llu\n",
  294. alloc_rec, (unsigned long long)bg_blkno);
  295. bg_bh = sb_getblk(osb->sb, bg_blkno);
  296. if (!bg_bh) {
  297. status = -EIO;
  298. mlog_errno(status);
  299. goto bail;
  300. }
  301. ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
  302. status = ocfs2_block_group_fill(handle,
  303. alloc_inode,
  304. bg_bh,
  305. bg_blkno,
  306. alloc_rec,
  307. cl);
  308. if (status < 0) {
  309. mlog_errno(status);
  310. goto bail;
  311. }
  312. bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  313. status = ocfs2_journal_access(handle, alloc_inode,
  314. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  315. if (status < 0) {
  316. mlog_errno(status);
  317. goto bail;
  318. }
  319. le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
  320. le16_to_cpu(bg->bg_free_bits_count));
  321. le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
  322. cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
  323. if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
  324. le16_add_cpu(&cl->cl_next_free_rec, 1);
  325. le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
  326. le16_to_cpu(bg->bg_free_bits_count));
  327. le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
  328. le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
  329. status = ocfs2_journal_dirty(handle, bh);
  330. if (status < 0) {
  331. mlog_errno(status);
  332. goto bail;
  333. }
  334. spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
  335. OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  336. fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
  337. le32_to_cpu(fe->i_clusters)));
  338. spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
  339. i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
  340. alloc_inode->i_blocks =
  341. ocfs2_align_bytes_to_sectors(i_size_read(alloc_inode));
  342. status = 0;
  343. bail:
  344. if (handle)
  345. ocfs2_commit_trans(handle);
  346. if (ac)
  347. ocfs2_free_alloc_context(ac);
  348. if (bg_bh)
  349. brelse(bg_bh);
  350. mlog_exit(status);
  351. return status;
  352. }
  353. static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
  354. struct ocfs2_alloc_context *ac)
  355. {
  356. int status;
  357. u32 bits_wanted = ac->ac_bits_wanted;
  358. struct inode *alloc_inode = ac->ac_inode;
  359. struct buffer_head *bh = NULL;
  360. struct ocfs2_journal_handle *handle = ac->ac_handle;
  361. struct ocfs2_dinode *fe;
  362. u32 free_bits;
  363. mlog_entry_void();
  364. BUG_ON(handle->flags & OCFS2_HANDLE_STARTED);
  365. ocfs2_handle_add_inode(handle, alloc_inode);
  366. status = ocfs2_meta_lock(alloc_inode, handle, &bh, 1);
  367. if (status < 0) {
  368. mlog_errno(status);
  369. goto bail;
  370. }
  371. fe = (struct ocfs2_dinode *) bh->b_data;
  372. if (!OCFS2_IS_VALID_DINODE(fe)) {
  373. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  374. status = -EIO;
  375. goto bail;
  376. }
  377. if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
  378. ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
  379. (unsigned long long)le64_to_cpu(fe->i_blkno));
  380. status = -EIO;
  381. goto bail;
  382. }
  383. free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
  384. le32_to_cpu(fe->id1.bitmap1.i_used);
  385. if (bits_wanted > free_bits) {
  386. /* cluster bitmap never grows */
  387. if (ocfs2_is_cluster_bitmap(alloc_inode)) {
  388. mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
  389. bits_wanted, free_bits);
  390. status = -ENOSPC;
  391. goto bail;
  392. }
  393. status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
  394. if (status < 0) {
  395. if (status != -ENOSPC)
  396. mlog_errno(status);
  397. goto bail;
  398. }
  399. atomic_inc(&osb->alloc_stats.bg_extends);
  400. /* You should never ask for this much metadata */
  401. BUG_ON(bits_wanted >
  402. (le32_to_cpu(fe->id1.bitmap1.i_total)
  403. - le32_to_cpu(fe->id1.bitmap1.i_used)));
  404. }
  405. get_bh(bh);
  406. ac->ac_bh = bh;
  407. bail:
  408. if (bh)
  409. brelse(bh);
  410. mlog_exit(status);
  411. return status;
  412. }
  413. int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
  414. struct ocfs2_journal_handle *handle,
  415. struct ocfs2_dinode *fe,
  416. struct ocfs2_alloc_context **ac)
  417. {
  418. int status;
  419. struct inode *alloc_inode = NULL;
  420. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  421. if (!(*ac)) {
  422. status = -ENOMEM;
  423. mlog_errno(status);
  424. goto bail;
  425. }
  426. (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
  427. (*ac)->ac_handle = handle;
  428. (*ac)->ac_which = OCFS2_AC_USE_META;
  429. #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
  430. alloc_inode = ocfs2_get_system_file_inode(osb,
  431. EXTENT_ALLOC_SYSTEM_INODE,
  432. 0);
  433. #else
  434. alloc_inode = ocfs2_get_system_file_inode(osb,
  435. EXTENT_ALLOC_SYSTEM_INODE,
  436. osb->slot_num);
  437. #endif
  438. if (!alloc_inode) {
  439. status = -ENOMEM;
  440. mlog_errno(status);
  441. goto bail;
  442. }
  443. (*ac)->ac_inode = igrab(alloc_inode);
  444. (*ac)->ac_group_search = ocfs2_block_group_search;
  445. status = ocfs2_reserve_suballoc_bits(osb, (*ac));
  446. if (status < 0) {
  447. if (status != -ENOSPC)
  448. mlog_errno(status);
  449. goto bail;
  450. }
  451. status = 0;
  452. bail:
  453. if ((status < 0) && *ac) {
  454. ocfs2_free_alloc_context(*ac);
  455. *ac = NULL;
  456. }
  457. if (alloc_inode)
  458. iput(alloc_inode);
  459. mlog_exit(status);
  460. return status;
  461. }
  462. int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
  463. struct ocfs2_journal_handle *handle,
  464. struct ocfs2_alloc_context **ac)
  465. {
  466. int status;
  467. struct inode *alloc_inode = NULL;
  468. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  469. if (!(*ac)) {
  470. status = -ENOMEM;
  471. mlog_errno(status);
  472. goto bail;
  473. }
  474. (*ac)->ac_bits_wanted = 1;
  475. (*ac)->ac_handle = handle;
  476. (*ac)->ac_which = OCFS2_AC_USE_INODE;
  477. alloc_inode = ocfs2_get_system_file_inode(osb,
  478. INODE_ALLOC_SYSTEM_INODE,
  479. osb->slot_num);
  480. if (!alloc_inode) {
  481. status = -ENOMEM;
  482. mlog_errno(status);
  483. goto bail;
  484. }
  485. (*ac)->ac_inode = igrab(alloc_inode);
  486. (*ac)->ac_group_search = ocfs2_block_group_search;
  487. status = ocfs2_reserve_suballoc_bits(osb, *ac);
  488. if (status < 0) {
  489. if (status != -ENOSPC)
  490. mlog_errno(status);
  491. goto bail;
  492. }
  493. status = 0;
  494. bail:
  495. if ((status < 0) && *ac) {
  496. ocfs2_free_alloc_context(*ac);
  497. *ac = NULL;
  498. }
  499. if (alloc_inode)
  500. iput(alloc_inode);
  501. mlog_exit(status);
  502. return status;
  503. }
  504. /* local alloc code has to do the same thing, so rather than do this
  505. * twice.. */
  506. int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  507. struct ocfs2_alloc_context *ac)
  508. {
  509. int status;
  510. ac->ac_inode = ocfs2_get_system_file_inode(osb,
  511. GLOBAL_BITMAP_SYSTEM_INODE,
  512. OCFS2_INVALID_SLOT);
  513. if (!ac->ac_inode) {
  514. status = -EINVAL;
  515. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  516. goto bail;
  517. }
  518. ac->ac_which = OCFS2_AC_USE_MAIN;
  519. ac->ac_group_search = ocfs2_cluster_group_search;
  520. status = ocfs2_reserve_suballoc_bits(osb, ac);
  521. if (status < 0 && status != -ENOSPC)
  522. mlog_errno(status);
  523. bail:
  524. return status;
  525. }
  526. /* Callers don't need to care which bitmap (local alloc or main) to
  527. * use so we figure it out for them, but unfortunately this clutters
  528. * things a bit. */
  529. int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  530. struct ocfs2_journal_handle *handle,
  531. u32 bits_wanted,
  532. struct ocfs2_alloc_context **ac)
  533. {
  534. int status;
  535. mlog_entry_void();
  536. BUG_ON(!handle);
  537. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  538. if (!(*ac)) {
  539. status = -ENOMEM;
  540. mlog_errno(status);
  541. goto bail;
  542. }
  543. (*ac)->ac_bits_wanted = bits_wanted;
  544. (*ac)->ac_handle = handle;
  545. status = -ENOSPC;
  546. if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
  547. status = ocfs2_reserve_local_alloc_bits(osb,
  548. handle,
  549. bits_wanted,
  550. *ac);
  551. if ((status < 0) && (status != -ENOSPC)) {
  552. mlog_errno(status);
  553. goto bail;
  554. } else if (status == -ENOSPC) {
  555. /* reserve_local_bits will return enospc with
  556. * the local alloc inode still locked, so we
  557. * can change this safely here. */
  558. mlog(0, "Disabling local alloc\n");
  559. /* We set to OCFS2_LA_DISABLED so that umount
  560. * can clean up what's left of the local
  561. * allocation */
  562. osb->local_alloc_state = OCFS2_LA_DISABLED;
  563. }
  564. }
  565. if (status == -ENOSPC) {
  566. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  567. if (status < 0) {
  568. if (status != -ENOSPC)
  569. mlog_errno(status);
  570. goto bail;
  571. }
  572. }
  573. status = 0;
  574. bail:
  575. if ((status < 0) && *ac) {
  576. ocfs2_free_alloc_context(*ac);
  577. *ac = NULL;
  578. }
  579. mlog_exit(status);
  580. return status;
  581. }
  582. /*
  583. * More or less lifted from ext3. I'll leave their description below:
  584. *
  585. * "For ext3 allocations, we must not reuse any blocks which are
  586. * allocated in the bitmap buffer's "last committed data" copy. This
  587. * prevents deletes from freeing up the page for reuse until we have
  588. * committed the delete transaction.
  589. *
  590. * If we didn't do this, then deleting something and reallocating it as
  591. * data would allow the old block to be overwritten before the
  592. * transaction committed (because we force data to disk before commit).
  593. * This would lead to corruption if we crashed between overwriting the
  594. * data and committing the delete.
  595. *
  596. * @@@ We may want to make this allocation behaviour conditional on
  597. * data-writes at some point, and disable it for metadata allocations or
  598. * sync-data inodes."
  599. *
  600. * Note: OCFS2 already does this differently for metadata vs data
  601. * allocations, as those bitmaps are seperate and undo access is never
  602. * called on a metadata group descriptor.
  603. */
  604. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  605. int nr)
  606. {
  607. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  608. if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  609. return 0;
  610. if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
  611. return 1;
  612. bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
  613. return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  614. }
  615. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  616. struct buffer_head *bg_bh,
  617. unsigned int bits_wanted,
  618. unsigned int total_bits,
  619. u16 *bit_off,
  620. u16 *bits_found)
  621. {
  622. void *bitmap;
  623. u16 best_offset, best_size;
  624. int offset, start, found, status = 0;
  625. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  626. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  627. OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
  628. return -EIO;
  629. }
  630. found = start = best_offset = best_size = 0;
  631. bitmap = bg->bg_bitmap;
  632. while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  633. if (offset == total_bits)
  634. break;
  635. if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  636. /* We found a zero, but we can't use it as it
  637. * hasn't been put to disk yet! */
  638. found = 0;
  639. start = offset + 1;
  640. } else if (offset == start) {
  641. /* we found a zero */
  642. found++;
  643. /* move start to the next bit to test */
  644. start++;
  645. } else {
  646. /* got a zero after some ones */
  647. found = 1;
  648. start = offset + 1;
  649. }
  650. if (found > best_size) {
  651. best_size = found;
  652. best_offset = start - found;
  653. }
  654. /* we got everything we needed */
  655. if (found == bits_wanted) {
  656. /* mlog(0, "Found it all!\n"); */
  657. break;
  658. }
  659. }
  660. /* XXX: I think the first clause is equivalent to the second
  661. * - jlbec */
  662. if (found == bits_wanted) {
  663. *bit_off = start - found;
  664. *bits_found = found;
  665. } else if (best_size) {
  666. *bit_off = best_offset;
  667. *bits_found = best_size;
  668. } else {
  669. status = -ENOSPC;
  670. /* No error log here -- see the comment above
  671. * ocfs2_test_bg_bit_allocatable */
  672. }
  673. return status;
  674. }
  675. static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *handle,
  676. struct inode *alloc_inode,
  677. struct ocfs2_group_desc *bg,
  678. struct buffer_head *group_bh,
  679. unsigned int bit_off,
  680. unsigned int num_bits)
  681. {
  682. int status;
  683. void *bitmap = bg->bg_bitmap;
  684. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  685. mlog_entry_void();
  686. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  687. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  688. status = -EIO;
  689. goto bail;
  690. }
  691. BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  692. mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
  693. num_bits);
  694. if (ocfs2_is_cluster_bitmap(alloc_inode))
  695. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  696. status = ocfs2_journal_access(handle,
  697. alloc_inode,
  698. group_bh,
  699. journal_type);
  700. if (status < 0) {
  701. mlog_errno(status);
  702. goto bail;
  703. }
  704. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  705. while(num_bits--)
  706. ocfs2_set_bit(bit_off++, bitmap);
  707. status = ocfs2_journal_dirty(handle,
  708. group_bh);
  709. if (status < 0) {
  710. mlog_errno(status);
  711. goto bail;
  712. }
  713. bail:
  714. mlog_exit(status);
  715. return status;
  716. }
  717. /* find the one with the most empty bits */
  718. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  719. {
  720. u16 curr, best;
  721. BUG_ON(!cl->cl_next_free_rec);
  722. best = curr = 0;
  723. while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  724. if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  725. le32_to_cpu(cl->cl_recs[best].c_free))
  726. best = curr;
  727. curr++;
  728. }
  729. BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  730. return best;
  731. }
  732. static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
  733. struct inode *alloc_inode,
  734. struct buffer_head *fe_bh,
  735. struct buffer_head *bg_bh,
  736. struct buffer_head *prev_bg_bh,
  737. u16 chain)
  738. {
  739. int status;
  740. /* there is a really tiny chance the journal calls could fail,
  741. * but we wouldn't want inconsistent blocks in *any* case. */
  742. u64 fe_ptr, bg_ptr, prev_bg_ptr;
  743. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  744. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  745. struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
  746. if (!OCFS2_IS_VALID_DINODE(fe)) {
  747. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  748. status = -EIO;
  749. goto out;
  750. }
  751. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  752. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  753. status = -EIO;
  754. goto out;
  755. }
  756. if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
  757. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
  758. status = -EIO;
  759. goto out;
  760. }
  761. mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
  762. (unsigned long long)fe->i_blkno, chain,
  763. (unsigned long long)bg->bg_blkno,
  764. (unsigned long long)prev_bg->bg_blkno);
  765. fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  766. bg_ptr = le64_to_cpu(bg->bg_next_group);
  767. prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
  768. status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
  769. OCFS2_JOURNAL_ACCESS_WRITE);
  770. if (status < 0) {
  771. mlog_errno(status);
  772. goto out_rollback;
  773. }
  774. prev_bg->bg_next_group = bg->bg_next_group;
  775. status = ocfs2_journal_dirty(handle, prev_bg_bh);
  776. if (status < 0) {
  777. mlog_errno(status);
  778. goto out_rollback;
  779. }
  780. status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
  781. OCFS2_JOURNAL_ACCESS_WRITE);
  782. if (status < 0) {
  783. mlog_errno(status);
  784. goto out_rollback;
  785. }
  786. bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  787. status = ocfs2_journal_dirty(handle, bg_bh);
  788. if (status < 0) {
  789. mlog_errno(status);
  790. goto out_rollback;
  791. }
  792. status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
  793. OCFS2_JOURNAL_ACCESS_WRITE);
  794. if (status < 0) {
  795. mlog_errno(status);
  796. goto out_rollback;
  797. }
  798. fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  799. status = ocfs2_journal_dirty(handle, fe_bh);
  800. if (status < 0) {
  801. mlog_errno(status);
  802. goto out_rollback;
  803. }
  804. status = 0;
  805. out_rollback:
  806. if (status < 0) {
  807. fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  808. bg->bg_next_group = cpu_to_le64(bg_ptr);
  809. prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  810. }
  811. out:
  812. mlog_exit(status);
  813. return status;
  814. }
  815. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  816. u32 wanted)
  817. {
  818. return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  819. }
  820. /* return 0 on success, -ENOSPC to keep searching and any other < 0
  821. * value on error. */
  822. static int ocfs2_cluster_group_search(struct inode *inode,
  823. struct buffer_head *group_bh,
  824. u32 bits_wanted, u32 min_bits,
  825. u16 *bit_off, u16 *bits_found)
  826. {
  827. int search = -ENOSPC;
  828. int ret;
  829. struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
  830. u16 tmp_off, tmp_found;
  831. unsigned int max_bits, gd_cluster_off;
  832. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  833. if (gd->bg_free_bits_count) {
  834. max_bits = le16_to_cpu(gd->bg_bits);
  835. /* Tail groups in cluster bitmaps which aren't cpg
  836. * aligned are prone to partial extention by a failed
  837. * fs resize. If the file system resize never got to
  838. * update the dinode cluster count, then we don't want
  839. * to trust any clusters past it, regardless of what
  840. * the group descriptor says. */
  841. gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
  842. le64_to_cpu(gd->bg_blkno));
  843. if ((gd_cluster_off + max_bits) >
  844. OCFS2_I(inode)->ip_clusters) {
  845. max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
  846. mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
  847. (unsigned long long)le64_to_cpu(gd->bg_blkno),
  848. le16_to_cpu(gd->bg_bits),
  849. OCFS2_I(inode)->ip_clusters, max_bits);
  850. }
  851. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  852. group_bh, bits_wanted,
  853. max_bits,
  854. &tmp_off, &tmp_found);
  855. if (ret)
  856. return ret;
  857. /* ocfs2_block_group_find_clear_bits() might
  858. * return success, but we still want to return
  859. * -ENOSPC unless it found the minimum number
  860. * of bits. */
  861. if (min_bits <= tmp_found) {
  862. *bit_off = tmp_off;
  863. *bits_found = tmp_found;
  864. search = 0; /* success */
  865. }
  866. }
  867. return search;
  868. }
  869. static int ocfs2_block_group_search(struct inode *inode,
  870. struct buffer_head *group_bh,
  871. u32 bits_wanted, u32 min_bits,
  872. u16 *bit_off, u16 *bits_found)
  873. {
  874. int ret = -ENOSPC;
  875. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  876. BUG_ON(min_bits != 1);
  877. BUG_ON(ocfs2_is_cluster_bitmap(inode));
  878. if (bg->bg_free_bits_count)
  879. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  880. group_bh, bits_wanted,
  881. le16_to_cpu(bg->bg_bits),
  882. bit_off, bits_found);
  883. return ret;
  884. }
  885. static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
  886. struct ocfs2_journal_handle *handle,
  887. struct buffer_head *di_bh,
  888. u32 num_bits,
  889. u16 chain)
  890. {
  891. int ret;
  892. u32 tmp_used;
  893. struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  894. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
  895. ret = ocfs2_journal_access(handle, inode, di_bh,
  896. OCFS2_JOURNAL_ACCESS_WRITE);
  897. if (ret < 0) {
  898. mlog_errno(ret);
  899. goto out;
  900. }
  901. tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  902. di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  903. le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
  904. ret = ocfs2_journal_dirty(handle, di_bh);
  905. if (ret < 0)
  906. mlog_errno(ret);
  907. out:
  908. return ret;
  909. }
  910. static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
  911. u32 bits_wanted,
  912. u32 min_bits,
  913. u16 *bit_off,
  914. unsigned int *num_bits,
  915. u64 gd_blkno,
  916. u16 *bits_left)
  917. {
  918. int ret;
  919. u16 found;
  920. struct buffer_head *group_bh = NULL;
  921. struct ocfs2_group_desc *gd;
  922. struct inode *alloc_inode = ac->ac_inode;
  923. struct ocfs2_journal_handle *handle = ac->ac_handle;
  924. ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
  925. &group_bh, OCFS2_BH_CACHED, alloc_inode);
  926. if (ret < 0) {
  927. mlog_errno(ret);
  928. return ret;
  929. }
  930. gd = (struct ocfs2_group_desc *) group_bh->b_data;
  931. if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
  932. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
  933. ret = -EIO;
  934. goto out;
  935. }
  936. ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
  937. bit_off, &found);
  938. if (ret < 0) {
  939. if (ret != -ENOSPC)
  940. mlog_errno(ret);
  941. goto out;
  942. }
  943. *num_bits = found;
  944. ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
  945. *num_bits,
  946. le16_to_cpu(gd->bg_chain));
  947. if (ret < 0) {
  948. mlog_errno(ret);
  949. goto out;
  950. }
  951. ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
  952. *bit_off, *num_bits);
  953. if (ret < 0)
  954. mlog_errno(ret);
  955. *bits_left = le16_to_cpu(gd->bg_free_bits_count);
  956. out:
  957. brelse(group_bh);
  958. return ret;
  959. }
  960. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  961. u32 bits_wanted,
  962. u32 min_bits,
  963. u16 *bit_off,
  964. unsigned int *num_bits,
  965. u64 *bg_blkno,
  966. u16 *bits_left)
  967. {
  968. int status;
  969. u16 chain, tmp_bits;
  970. u32 tmp_used;
  971. u64 next_group;
  972. struct ocfs2_journal_handle *handle = ac->ac_handle;
  973. struct inode *alloc_inode = ac->ac_inode;
  974. struct buffer_head *group_bh = NULL;
  975. struct buffer_head *prev_group_bh = NULL;
  976. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  977. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  978. struct ocfs2_group_desc *bg;
  979. chain = ac->ac_chain;
  980. mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
  981. bits_wanted, chain,
  982. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
  983. status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
  984. le64_to_cpu(cl->cl_recs[chain].c_blkno),
  985. &group_bh, OCFS2_BH_CACHED, alloc_inode);
  986. if (status < 0) {
  987. mlog_errno(status);
  988. goto bail;
  989. }
  990. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  991. status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
  992. if (status) {
  993. mlog_errno(status);
  994. goto bail;
  995. }
  996. status = -ENOSPC;
  997. /* for now, the chain search is a bit simplistic. We just use
  998. * the 1st group with any empty bits. */
  999. while ((status = ac->ac_group_search(alloc_inode, group_bh,
  1000. bits_wanted, min_bits, bit_off,
  1001. &tmp_bits)) == -ENOSPC) {
  1002. if (!bg->bg_next_group)
  1003. break;
  1004. if (prev_group_bh) {
  1005. brelse(prev_group_bh);
  1006. prev_group_bh = NULL;
  1007. }
  1008. next_group = le64_to_cpu(bg->bg_next_group);
  1009. prev_group_bh = group_bh;
  1010. group_bh = NULL;
  1011. status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
  1012. next_group, &group_bh,
  1013. OCFS2_BH_CACHED, alloc_inode);
  1014. if (status < 0) {
  1015. mlog_errno(status);
  1016. goto bail;
  1017. }
  1018. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  1019. status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
  1020. if (status) {
  1021. mlog_errno(status);
  1022. goto bail;
  1023. }
  1024. }
  1025. if (status < 0) {
  1026. if (status != -ENOSPC)
  1027. mlog_errno(status);
  1028. goto bail;
  1029. }
  1030. mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
  1031. tmp_bits, (unsigned long long)bg->bg_blkno);
  1032. *num_bits = tmp_bits;
  1033. BUG_ON(*num_bits == 0);
  1034. /*
  1035. * Keep track of previous block descriptor read. When
  1036. * we find a target, if we have read more than X
  1037. * number of descriptors, and the target is reasonably
  1038. * empty, relink him to top of his chain.
  1039. *
  1040. * We've read 0 extra blocks and only send one more to
  1041. * the transaction, yet the next guy to search has a
  1042. * much easier time.
  1043. *
  1044. * Do this *after* figuring out how many bits we're taking out
  1045. * of our target group.
  1046. */
  1047. if (ac->ac_allow_chain_relink &&
  1048. (prev_group_bh) &&
  1049. (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
  1050. status = ocfs2_relink_block_group(handle, alloc_inode,
  1051. ac->ac_bh, group_bh,
  1052. prev_group_bh, chain);
  1053. if (status < 0) {
  1054. mlog_errno(status);
  1055. goto bail;
  1056. }
  1057. }
  1058. /* Ok, claim our bits now: set the info on dinode, chainlist
  1059. * and then the group */
  1060. status = ocfs2_journal_access(handle,
  1061. alloc_inode,
  1062. ac->ac_bh,
  1063. OCFS2_JOURNAL_ACCESS_WRITE);
  1064. if (status < 0) {
  1065. mlog_errno(status);
  1066. goto bail;
  1067. }
  1068. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1069. fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
  1070. le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
  1071. status = ocfs2_journal_dirty(handle,
  1072. ac->ac_bh);
  1073. if (status < 0) {
  1074. mlog_errno(status);
  1075. goto bail;
  1076. }
  1077. status = ocfs2_block_group_set_bits(handle,
  1078. alloc_inode,
  1079. bg,
  1080. group_bh,
  1081. *bit_off,
  1082. *num_bits);
  1083. if (status < 0) {
  1084. mlog_errno(status);
  1085. goto bail;
  1086. }
  1087. mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
  1088. (unsigned long long)fe->i_blkno);
  1089. *bg_blkno = le64_to_cpu(bg->bg_blkno);
  1090. *bits_left = le16_to_cpu(bg->bg_free_bits_count);
  1091. bail:
  1092. if (group_bh)
  1093. brelse(group_bh);
  1094. if (prev_group_bh)
  1095. brelse(prev_group_bh);
  1096. mlog_exit(status);
  1097. return status;
  1098. }
  1099. /* will give out up to bits_wanted contiguous bits. */
  1100. static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  1101. struct ocfs2_alloc_context *ac,
  1102. u32 bits_wanted,
  1103. u32 min_bits,
  1104. u16 *bit_off,
  1105. unsigned int *num_bits,
  1106. u64 *bg_blkno)
  1107. {
  1108. int status;
  1109. u16 victim, i;
  1110. u16 bits_left = 0;
  1111. u64 hint_blkno = ac->ac_last_group;
  1112. struct ocfs2_chain_list *cl;
  1113. struct ocfs2_dinode *fe;
  1114. mlog_entry_void();
  1115. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1116. BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  1117. BUG_ON(!ac->ac_bh);
  1118. fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  1119. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1120. OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
  1121. status = -EIO;
  1122. goto bail;
  1123. }
  1124. if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  1125. le32_to_cpu(fe->id1.bitmap1.i_total)) {
  1126. ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
  1127. "bits but only %u total.",
  1128. (unsigned long long)le64_to_cpu(fe->i_blkno),
  1129. le32_to_cpu(fe->id1.bitmap1.i_used),
  1130. le32_to_cpu(fe->id1.bitmap1.i_total));
  1131. status = -EIO;
  1132. goto bail;
  1133. }
  1134. if (hint_blkno) {
  1135. /* Attempt to short-circuit the usual search mechanism
  1136. * by jumping straight to the most recently used
  1137. * allocation group. This helps us mantain some
  1138. * contiguousness across allocations. */
  1139. status = ocfs2_search_one_group(ac, bits_wanted, min_bits,
  1140. bit_off, num_bits,
  1141. hint_blkno, &bits_left);
  1142. if (!status) {
  1143. /* Be careful to update *bg_blkno here as the
  1144. * caller is expecting it to be filled in, and
  1145. * ocfs2_search_one_group() won't do that for
  1146. * us. */
  1147. *bg_blkno = hint_blkno;
  1148. goto set_hint;
  1149. }
  1150. if (status < 0 && status != -ENOSPC) {
  1151. mlog_errno(status);
  1152. goto bail;
  1153. }
  1154. }
  1155. cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  1156. victim = ocfs2_find_victim_chain(cl);
  1157. ac->ac_chain = victim;
  1158. ac->ac_allow_chain_relink = 1;
  1159. status = ocfs2_search_chain(ac, bits_wanted, min_bits, bit_off,
  1160. num_bits, bg_blkno, &bits_left);
  1161. if (!status)
  1162. goto set_hint;
  1163. if (status < 0 && status != -ENOSPC) {
  1164. mlog_errno(status);
  1165. goto bail;
  1166. }
  1167. mlog(0, "Search of victim chain %u came up with nothing, "
  1168. "trying all chains now.\n", victim);
  1169. /* If we didn't pick a good victim, then just default to
  1170. * searching each chain in order. Don't allow chain relinking
  1171. * because we only calculate enough journal credits for one
  1172. * relink per alloc. */
  1173. ac->ac_allow_chain_relink = 0;
  1174. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  1175. if (i == victim)
  1176. continue;
  1177. if (!cl->cl_recs[i].c_free)
  1178. continue;
  1179. ac->ac_chain = i;
  1180. status = ocfs2_search_chain(ac, bits_wanted, min_bits,
  1181. bit_off, num_bits, bg_blkno,
  1182. &bits_left);
  1183. if (!status)
  1184. break;
  1185. if (status < 0 && status != -ENOSPC) {
  1186. mlog_errno(status);
  1187. goto bail;
  1188. }
  1189. }
  1190. set_hint:
  1191. if (status != -ENOSPC) {
  1192. /* If the next search of this group is not likely to
  1193. * yield a suitable extent, then we reset the last
  1194. * group hint so as to not waste a disk read */
  1195. if (bits_left < min_bits)
  1196. ac->ac_last_group = 0;
  1197. else
  1198. ac->ac_last_group = *bg_blkno;
  1199. }
  1200. bail:
  1201. mlog_exit(status);
  1202. return status;
  1203. }
  1204. int ocfs2_claim_metadata(struct ocfs2_super *osb,
  1205. struct ocfs2_journal_handle *handle,
  1206. struct ocfs2_alloc_context *ac,
  1207. u32 bits_wanted,
  1208. u16 *suballoc_bit_start,
  1209. unsigned int *num_bits,
  1210. u64 *blkno_start)
  1211. {
  1212. int status;
  1213. u64 bg_blkno;
  1214. BUG_ON(!ac);
  1215. BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  1216. BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
  1217. BUG_ON(ac->ac_handle != handle);
  1218. status = ocfs2_claim_suballoc_bits(osb,
  1219. ac,
  1220. bits_wanted,
  1221. 1,
  1222. suballoc_bit_start,
  1223. num_bits,
  1224. &bg_blkno);
  1225. if (status < 0) {
  1226. mlog_errno(status);
  1227. goto bail;
  1228. }
  1229. atomic_inc(&osb->alloc_stats.bg_allocs);
  1230. *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
  1231. ac->ac_bits_given += (*num_bits);
  1232. status = 0;
  1233. bail:
  1234. mlog_exit(status);
  1235. return status;
  1236. }
  1237. int ocfs2_claim_new_inode(struct ocfs2_super *osb,
  1238. struct ocfs2_journal_handle *handle,
  1239. struct ocfs2_alloc_context *ac,
  1240. u16 *suballoc_bit,
  1241. u64 *fe_blkno)
  1242. {
  1243. int status;
  1244. unsigned int num_bits;
  1245. u64 bg_blkno;
  1246. mlog_entry_void();
  1247. BUG_ON(!ac);
  1248. BUG_ON(ac->ac_bits_given != 0);
  1249. BUG_ON(ac->ac_bits_wanted != 1);
  1250. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1251. BUG_ON(ac->ac_handle != handle);
  1252. status = ocfs2_claim_suballoc_bits(osb,
  1253. ac,
  1254. 1,
  1255. 1,
  1256. suballoc_bit,
  1257. &num_bits,
  1258. &bg_blkno);
  1259. if (status < 0) {
  1260. mlog_errno(status);
  1261. goto bail;
  1262. }
  1263. atomic_inc(&osb->alloc_stats.bg_allocs);
  1264. BUG_ON(num_bits != 1);
  1265. *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
  1266. ac->ac_bits_given++;
  1267. status = 0;
  1268. bail:
  1269. mlog_exit(status);
  1270. return status;
  1271. }
  1272. /* translate a group desc. blkno and it's bitmap offset into
  1273. * disk cluster offset. */
  1274. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  1275. u64 bg_blkno,
  1276. u16 bg_bit_off)
  1277. {
  1278. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1279. u32 cluster = 0;
  1280. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1281. if (bg_blkno != osb->first_cluster_group_blkno)
  1282. cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  1283. cluster += (u32) bg_bit_off;
  1284. return cluster;
  1285. }
  1286. /* given a cluster offset, calculate which block group it belongs to
  1287. * and return that block offset. */
  1288. static inline u64 ocfs2_which_cluster_group(struct inode *inode,
  1289. u32 cluster)
  1290. {
  1291. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1292. u32 group_no;
  1293. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1294. group_no = cluster / osb->bitmap_cpg;
  1295. if (!group_no)
  1296. return osb->first_cluster_group_blkno;
  1297. return ocfs2_clusters_to_blocks(inode->i_sb,
  1298. group_no * osb->bitmap_cpg);
  1299. }
  1300. /* given the block number of a cluster start, calculate which cluster
  1301. * group and descriptor bitmap offset that corresponds to. */
  1302. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  1303. u64 data_blkno,
  1304. u64 *bg_blkno,
  1305. u16 *bg_bit_off)
  1306. {
  1307. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1308. u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  1309. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1310. *bg_blkno = ocfs2_which_cluster_group(inode,
  1311. data_cluster);
  1312. if (*bg_blkno == osb->first_cluster_group_blkno)
  1313. *bg_bit_off = (u16) data_cluster;
  1314. else
  1315. *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  1316. data_blkno - *bg_blkno);
  1317. }
  1318. /*
  1319. * min_bits - minimum contiguous chunk from this total allocation we
  1320. * can handle. set to what we asked for originally for a full
  1321. * contig. allocation, set to '1' to indicate we can deal with extents
  1322. * of any size.
  1323. */
  1324. int ocfs2_claim_clusters(struct ocfs2_super *osb,
  1325. struct ocfs2_journal_handle *handle,
  1326. struct ocfs2_alloc_context *ac,
  1327. u32 min_clusters,
  1328. u32 *cluster_start,
  1329. u32 *num_clusters)
  1330. {
  1331. int status;
  1332. unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  1333. u64 bg_blkno = 0;
  1334. u16 bg_bit_off;
  1335. mlog_entry_void();
  1336. BUG_ON(!ac);
  1337. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1338. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  1339. && ac->ac_which != OCFS2_AC_USE_MAIN);
  1340. BUG_ON(ac->ac_handle != handle);
  1341. if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  1342. status = ocfs2_claim_local_alloc_bits(osb,
  1343. handle,
  1344. ac,
  1345. bits_wanted,
  1346. cluster_start,
  1347. num_clusters);
  1348. if (!status)
  1349. atomic_inc(&osb->alloc_stats.local_data);
  1350. } else {
  1351. if (min_clusters > (osb->bitmap_cpg - 1)) {
  1352. /* The only paths asking for contiguousness
  1353. * should know about this already. */
  1354. mlog(ML_ERROR, "minimum allocation requested exceeds "
  1355. "group bitmap size!");
  1356. status = -ENOSPC;
  1357. goto bail;
  1358. }
  1359. /* clamp the current request down to a realistic size. */
  1360. if (bits_wanted > (osb->bitmap_cpg - 1))
  1361. bits_wanted = osb->bitmap_cpg - 1;
  1362. status = ocfs2_claim_suballoc_bits(osb,
  1363. ac,
  1364. bits_wanted,
  1365. min_clusters,
  1366. &bg_bit_off,
  1367. num_clusters,
  1368. &bg_blkno);
  1369. if (!status) {
  1370. *cluster_start =
  1371. ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  1372. bg_blkno,
  1373. bg_bit_off);
  1374. atomic_inc(&osb->alloc_stats.bitmap_data);
  1375. }
  1376. }
  1377. if (status < 0) {
  1378. if (status != -ENOSPC)
  1379. mlog_errno(status);
  1380. goto bail;
  1381. }
  1382. ac->ac_bits_given += *num_clusters;
  1383. bail:
  1384. mlog_exit(status);
  1385. return status;
  1386. }
  1387. static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
  1388. struct inode *alloc_inode,
  1389. struct ocfs2_group_desc *bg,
  1390. struct buffer_head *group_bh,
  1391. unsigned int bit_off,
  1392. unsigned int num_bits)
  1393. {
  1394. int status;
  1395. unsigned int tmp;
  1396. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  1397. struct ocfs2_group_desc *undo_bg = NULL;
  1398. mlog_entry_void();
  1399. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  1400. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  1401. status = -EIO;
  1402. goto bail;
  1403. }
  1404. mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
  1405. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1406. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  1407. status = ocfs2_journal_access(handle, alloc_inode, group_bh,
  1408. journal_type);
  1409. if (status < 0) {
  1410. mlog_errno(status);
  1411. goto bail;
  1412. }
  1413. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1414. undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
  1415. tmp = num_bits;
  1416. while(tmp--) {
  1417. ocfs2_clear_bit((bit_off + tmp),
  1418. (unsigned long *) bg->bg_bitmap);
  1419. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1420. ocfs2_set_bit(bit_off + tmp,
  1421. (unsigned long *) undo_bg->bg_bitmap);
  1422. }
  1423. le16_add_cpu(&bg->bg_free_bits_count, num_bits);
  1424. status = ocfs2_journal_dirty(handle, group_bh);
  1425. if (status < 0)
  1426. mlog_errno(status);
  1427. bail:
  1428. return status;
  1429. }
  1430. /*
  1431. * expects the suballoc inode to already be locked.
  1432. */
  1433. static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
  1434. struct inode *alloc_inode,
  1435. struct buffer_head *alloc_bh,
  1436. unsigned int start_bit,
  1437. u64 bg_blkno,
  1438. unsigned int count)
  1439. {
  1440. int status = 0;
  1441. u32 tmp_used;
  1442. struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  1443. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  1444. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  1445. struct buffer_head *group_bh = NULL;
  1446. struct ocfs2_group_desc *group;
  1447. mlog_entry_void();
  1448. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1449. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  1450. status = -EIO;
  1451. goto bail;
  1452. }
  1453. BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
  1454. mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
  1455. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
  1456. (unsigned long long)bg_blkno, start_bit);
  1457. status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
  1458. alloc_inode);
  1459. if (status < 0) {
  1460. mlog_errno(status);
  1461. goto bail;
  1462. }
  1463. group = (struct ocfs2_group_desc *) group_bh->b_data;
  1464. status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
  1465. if (status) {
  1466. mlog_errno(status);
  1467. goto bail;
  1468. }
  1469. BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  1470. status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  1471. group, group_bh,
  1472. start_bit, count);
  1473. if (status < 0) {
  1474. mlog_errno(status);
  1475. goto bail;
  1476. }
  1477. status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
  1478. OCFS2_JOURNAL_ACCESS_WRITE);
  1479. if (status < 0) {
  1480. mlog_errno(status);
  1481. goto bail;
  1482. }
  1483. le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  1484. count);
  1485. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1486. fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
  1487. status = ocfs2_journal_dirty(handle, alloc_bh);
  1488. if (status < 0) {
  1489. mlog_errno(status);
  1490. goto bail;
  1491. }
  1492. bail:
  1493. if (group_bh)
  1494. brelse(group_bh);
  1495. mlog_exit(status);
  1496. return status;
  1497. }
  1498. static inline u64 ocfs2_which_suballoc_group(u64 block, unsigned int bit)
  1499. {
  1500. u64 group = block - (u64) bit;
  1501. return group;
  1502. }
  1503. int ocfs2_free_dinode(struct ocfs2_journal_handle *handle,
  1504. struct inode *inode_alloc_inode,
  1505. struct buffer_head *inode_alloc_bh,
  1506. struct ocfs2_dinode *di)
  1507. {
  1508. u64 blk = le64_to_cpu(di->i_blkno);
  1509. u16 bit = le16_to_cpu(di->i_suballoc_bit);
  1510. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1511. return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  1512. inode_alloc_bh, bit, bg_blkno, 1);
  1513. }
  1514. int ocfs2_free_extent_block(struct ocfs2_journal_handle *handle,
  1515. struct inode *eb_alloc_inode,
  1516. struct buffer_head *eb_alloc_bh,
  1517. struct ocfs2_extent_block *eb)
  1518. {
  1519. u64 blk = le64_to_cpu(eb->h_blkno);
  1520. u16 bit = le16_to_cpu(eb->h_suballoc_bit);
  1521. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1522. return ocfs2_free_suballoc_bits(handle, eb_alloc_inode, eb_alloc_bh,
  1523. bit, bg_blkno, 1);
  1524. }
  1525. int ocfs2_free_clusters(struct ocfs2_journal_handle *handle,
  1526. struct inode *bitmap_inode,
  1527. struct buffer_head *bitmap_bh,
  1528. u64 start_blk,
  1529. unsigned int num_clusters)
  1530. {
  1531. int status;
  1532. u16 bg_start_bit;
  1533. u64 bg_blkno;
  1534. struct ocfs2_dinode *fe;
  1535. /* You can't ever have a contiguous set of clusters
  1536. * bigger than a block group bitmap so we never have to worry
  1537. * about looping on them. */
  1538. mlog_entry_void();
  1539. /* This is expensive. We can safely remove once this stuff has
  1540. * gotten tested really well. */
  1541. BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  1542. fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  1543. ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  1544. &bg_start_bit);
  1545. mlog(0, "want to free %u clusters starting at block %llu\n",
  1546. num_clusters, (unsigned long long)start_blk);
  1547. mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
  1548. (unsigned long long)bg_blkno, bg_start_bit);
  1549. status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  1550. bg_start_bit, bg_blkno,
  1551. num_clusters);
  1552. if (status < 0)
  1553. mlog_errno(status);
  1554. mlog_exit(status);
  1555. return status;
  1556. }
  1557. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  1558. {
  1559. printk("Block Group:\n");
  1560. printk("bg_signature: %s\n", bg->bg_signature);
  1561. printk("bg_size: %u\n", bg->bg_size);
  1562. printk("bg_bits: %u\n", bg->bg_bits);
  1563. printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
  1564. printk("bg_chain: %u\n", bg->bg_chain);
  1565. printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
  1566. printk("bg_next_group: %llu\n",
  1567. (unsigned long long)bg->bg_next_group);
  1568. printk("bg_parent_dinode: %llu\n",
  1569. (unsigned long long)bg->bg_parent_dinode);
  1570. printk("bg_blkno: %llu\n",
  1571. (unsigned long long)bg->bg_blkno);
  1572. }
  1573. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  1574. {
  1575. int i;
  1576. printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
  1577. printk("i_signature: %s\n", fe->i_signature);
  1578. printk("i_size: %llu\n",
  1579. (unsigned long long)fe->i_size);
  1580. printk("i_clusters: %u\n", fe->i_clusters);
  1581. printk("i_generation: %u\n",
  1582. le32_to_cpu(fe->i_generation));
  1583. printk("id1.bitmap1.i_used: %u\n",
  1584. le32_to_cpu(fe->id1.bitmap1.i_used));
  1585. printk("id1.bitmap1.i_total: %u\n",
  1586. le32_to_cpu(fe->id1.bitmap1.i_total));
  1587. printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
  1588. printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
  1589. printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
  1590. printk("id2.i_chain.cl_next_free_rec: %u\n",
  1591. fe->id2.i_chain.cl_next_free_rec);
  1592. for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  1593. printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
  1594. fe->id2.i_chain.cl_recs[i].c_free);
  1595. printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
  1596. fe->id2.i_chain.cl_recs[i].c_total);
  1597. printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
  1598. (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
  1599. }
  1600. }