suballoc.c 63 KB

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