suballoc.c 87 KB

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