suballoc.c 46 KB

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