suballoc.c 77 KB

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