suballoc.c 77 KB

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