suballoc.c 62 KB

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