suballoc.c 76 KB

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