suballoc.c 75 KB

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