suballoc.c 54 KB

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