suballoc.c 65 KB

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