suballoc.c 65 KB

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