suballoc.c 49 KB

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