suballoc.c 76 KB

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