suballoc.c 65 KB

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