suballoc.c 70 KB

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