suballoc.c 48 KB

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