suballoc.c 49 KB

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