suballoc.c 77 KB

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