suballoc.c 65 KB

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