suballoc.c 73 KB

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