suballoc.c 73 KB

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