suballoc.c 72 KB

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