suballoc.c 48 KB

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