suballoc.c 53 KB

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