suballoc.c 77 KB

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