suballoc.c 66 KB

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