suballoc.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
  44. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
  45. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
  46. static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
  47. struct inode *alloc_inode,
  48. struct buffer_head *bg_bh,
  49. u64 group_blkno,
  50. u16 my_chain,
  51. struct ocfs2_chain_list *cl);
  52. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  53. struct inode *alloc_inode,
  54. struct buffer_head *bh);
  55. static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
  56. struct ocfs2_alloc_context *ac);
  57. static int ocfs2_cluster_group_search(struct inode *inode,
  58. struct buffer_head *group_bh,
  59. u32 bits_wanted, u32 min_bits,
  60. u16 *bit_off, u16 *bits_found);
  61. static int ocfs2_block_group_search(struct inode *inode,
  62. struct buffer_head *group_bh,
  63. u32 bits_wanted, u32 min_bits,
  64. u16 *bit_off, u16 *bits_found);
  65. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  66. u32 bits_wanted,
  67. u32 min_bits,
  68. u16 *bit_off,
  69. unsigned int *num_bits,
  70. u64 *bg_blkno);
  71. static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  72. struct ocfs2_alloc_context *ac,
  73. u32 bits_wanted,
  74. u32 min_bits,
  75. u16 *bit_off,
  76. unsigned int *num_bits,
  77. u64 *bg_blkno);
  78. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  79. int nr);
  80. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  81. struct buffer_head *bg_bh,
  82. unsigned int bits_wanted,
  83. u16 *bit_off,
  84. u16 *bits_found);
  85. static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *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 inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
  92. struct inode *alloc_inode,
  93. struct ocfs2_group_desc *bg,
  94. struct buffer_head *group_bh,
  95. unsigned int bit_off,
  96. unsigned int num_bits);
  97. static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
  98. struct inode *alloc_inode,
  99. struct buffer_head *fe_bh,
  100. struct buffer_head *bg_bh,
  101. struct buffer_head *prev_bg_bh,
  102. u16 chain);
  103. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  104. u32 wanted);
  105. static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
  106. struct inode *alloc_inode,
  107. struct buffer_head *alloc_bh,
  108. unsigned int start_bit,
  109. u64 bg_blkno,
  110. unsigned int count);
  111. static inline u64 ocfs2_which_suballoc_group(u64 block,
  112. unsigned int bit);
  113. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  114. u64 bg_blkno,
  115. u16 bg_bit_off);
  116. static inline u64 ocfs2_which_cluster_group(struct inode *inode,
  117. u32 cluster);
  118. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  119. u64 data_blkno,
  120. u64 *bg_blkno,
  121. u16 *bg_bit_off);
  122. void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
  123. {
  124. if (ac->ac_inode)
  125. iput(ac->ac_inode);
  126. if (ac->ac_bh)
  127. brelse(ac->ac_bh);
  128. kfree(ac);
  129. }
  130. static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
  131. {
  132. return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
  133. }
  134. static int ocfs2_block_group_fill(struct ocfs2_journal_handle *handle,
  135. struct inode *alloc_inode,
  136. struct buffer_head *bg_bh,
  137. u64 group_blkno,
  138. u16 my_chain,
  139. struct ocfs2_chain_list *cl)
  140. {
  141. int status = 0;
  142. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  143. struct super_block * sb = alloc_inode->i_sb;
  144. mlog_entry_void();
  145. if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
  146. ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
  147. "b_blocknr (%llu)",
  148. (unsigned long long)group_blkno,
  149. (unsigned long long) bg_bh->b_blocknr);
  150. status = -EIO;
  151. goto bail;
  152. }
  153. status = ocfs2_journal_access(handle,
  154. alloc_inode,
  155. bg_bh,
  156. OCFS2_JOURNAL_ACCESS_CREATE);
  157. if (status < 0) {
  158. mlog_errno(status);
  159. goto bail;
  160. }
  161. memset(bg, 0, sb->s_blocksize);
  162. strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
  163. bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  164. bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
  165. bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  166. bg->bg_chain = cpu_to_le16(my_chain);
  167. bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
  168. bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
  169. bg->bg_blkno = cpu_to_le64(group_blkno);
  170. /* set the 1st bit in the bitmap to account for the descriptor block */
  171. ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
  172. bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
  173. status = ocfs2_journal_dirty(handle, bg_bh);
  174. if (status < 0)
  175. mlog_errno(status);
  176. /* There is no need to zero out or otherwise initialize the
  177. * other blocks in a group - All valid FS metadata in a block
  178. * group stores the superblock fs_generation value at
  179. * allocation time. */
  180. bail:
  181. mlog_exit(status);
  182. return status;
  183. }
  184. static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
  185. {
  186. u16 curr, best;
  187. best = curr = 0;
  188. while (curr < le16_to_cpu(cl->cl_count)) {
  189. if (le32_to_cpu(cl->cl_recs[best].c_total) >
  190. le32_to_cpu(cl->cl_recs[curr].c_total))
  191. best = curr;
  192. curr++;
  193. }
  194. return best;
  195. }
  196. /*
  197. * We expect the block group allocator to already be locked.
  198. */
  199. static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  200. struct inode *alloc_inode,
  201. struct buffer_head *bh)
  202. {
  203. int status, credits;
  204. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  205. struct ocfs2_chain_list *cl;
  206. struct ocfs2_alloc_context *ac = NULL;
  207. struct ocfs2_journal_handle *handle = NULL;
  208. u32 bit_off, num_bits;
  209. u16 alloc_rec;
  210. u64 bg_blkno;
  211. struct buffer_head *bg_bh = NULL;
  212. struct ocfs2_group_desc *bg;
  213. BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
  214. mlog_entry_void();
  215. handle = ocfs2_alloc_handle(osb);
  216. if (!handle) {
  217. status = -ENOMEM;
  218. mlog_errno(status);
  219. goto bail;
  220. }
  221. cl = &fe->id2.i_chain;
  222. status = ocfs2_reserve_clusters(osb,
  223. handle,
  224. le16_to_cpu(cl->cl_cpg),
  225. &ac);
  226. if (status < 0) {
  227. if (status != -ENOSPC)
  228. mlog_errno(status);
  229. goto bail;
  230. }
  231. credits = ocfs2_calc_group_alloc_credits(osb->sb,
  232. le16_to_cpu(cl->cl_cpg));
  233. handle = ocfs2_start_trans(osb, handle, credits);
  234. if (IS_ERR(handle)) {
  235. status = PTR_ERR(handle);
  236. handle = NULL;
  237. mlog_errno(status);
  238. goto bail;
  239. }
  240. status = ocfs2_claim_clusters(osb,
  241. handle,
  242. ac,
  243. le16_to_cpu(cl->cl_cpg),
  244. &bit_off,
  245. &num_bits);
  246. if (status < 0) {
  247. if (status != -ENOSPC)
  248. mlog_errno(status);
  249. goto bail;
  250. }
  251. alloc_rec = ocfs2_find_smallest_chain(cl);
  252. /* setup the group */
  253. bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
  254. mlog(0, "new descriptor, record %u, at block %llu\n",
  255. alloc_rec, (unsigned long long)bg_blkno);
  256. bg_bh = sb_getblk(osb->sb, bg_blkno);
  257. if (!bg_bh) {
  258. status = -EIO;
  259. mlog_errno(status);
  260. goto bail;
  261. }
  262. ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
  263. status = ocfs2_block_group_fill(handle,
  264. alloc_inode,
  265. bg_bh,
  266. bg_blkno,
  267. alloc_rec,
  268. cl);
  269. if (status < 0) {
  270. mlog_errno(status);
  271. goto bail;
  272. }
  273. bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  274. status = ocfs2_journal_access(handle, alloc_inode,
  275. bh, OCFS2_JOURNAL_ACCESS_WRITE);
  276. if (status < 0) {
  277. mlog_errno(status);
  278. goto bail;
  279. }
  280. le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
  281. le16_to_cpu(bg->bg_free_bits_count));
  282. le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
  283. cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
  284. if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
  285. le16_add_cpu(&cl->cl_next_free_rec, 1);
  286. le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
  287. le16_to_cpu(bg->bg_free_bits_count));
  288. le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
  289. le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
  290. status = ocfs2_journal_dirty(handle, bh);
  291. if (status < 0) {
  292. mlog_errno(status);
  293. goto bail;
  294. }
  295. spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
  296. OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  297. fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
  298. le32_to_cpu(fe->i_clusters)));
  299. spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
  300. i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
  301. alloc_inode->i_blocks =
  302. ocfs2_align_bytes_to_sectors(i_size_read(alloc_inode));
  303. status = 0;
  304. bail:
  305. if (handle)
  306. ocfs2_commit_trans(handle);
  307. if (ac)
  308. ocfs2_free_alloc_context(ac);
  309. if (bg_bh)
  310. brelse(bg_bh);
  311. mlog_exit(status);
  312. return status;
  313. }
  314. static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
  315. struct ocfs2_alloc_context *ac)
  316. {
  317. int status;
  318. u32 bits_wanted = ac->ac_bits_wanted;
  319. struct inode *alloc_inode = ac->ac_inode;
  320. struct buffer_head *bh = NULL;
  321. struct ocfs2_journal_handle *handle = ac->ac_handle;
  322. struct ocfs2_dinode *fe;
  323. u32 free_bits;
  324. mlog_entry_void();
  325. BUG_ON(handle->flags & OCFS2_HANDLE_STARTED);
  326. ocfs2_handle_add_inode(handle, alloc_inode);
  327. status = ocfs2_meta_lock(alloc_inode, handle, &bh, 1);
  328. if (status < 0) {
  329. mlog_errno(status);
  330. goto bail;
  331. }
  332. fe = (struct ocfs2_dinode *) bh->b_data;
  333. if (!OCFS2_IS_VALID_DINODE(fe)) {
  334. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  335. status = -EIO;
  336. goto bail;
  337. }
  338. if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
  339. ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
  340. (unsigned long long)le64_to_cpu(fe->i_blkno));
  341. status = -EIO;
  342. goto bail;
  343. }
  344. free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
  345. le32_to_cpu(fe->id1.bitmap1.i_used);
  346. if (bits_wanted > free_bits) {
  347. /* cluster bitmap never grows */
  348. if (ocfs2_is_cluster_bitmap(alloc_inode)) {
  349. mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
  350. bits_wanted, free_bits);
  351. status = -ENOSPC;
  352. goto bail;
  353. }
  354. status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
  355. if (status < 0) {
  356. if (status != -ENOSPC)
  357. mlog_errno(status);
  358. goto bail;
  359. }
  360. atomic_inc(&osb->alloc_stats.bg_extends);
  361. /* You should never ask for this much metadata */
  362. BUG_ON(bits_wanted >
  363. (le32_to_cpu(fe->id1.bitmap1.i_total)
  364. - le32_to_cpu(fe->id1.bitmap1.i_used)));
  365. }
  366. get_bh(bh);
  367. ac->ac_bh = bh;
  368. bail:
  369. if (bh)
  370. brelse(bh);
  371. mlog_exit(status);
  372. return status;
  373. }
  374. int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
  375. struct ocfs2_journal_handle *handle,
  376. struct ocfs2_dinode *fe,
  377. struct ocfs2_alloc_context **ac)
  378. {
  379. int status;
  380. struct inode *alloc_inode = NULL;
  381. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  382. if (!(*ac)) {
  383. status = -ENOMEM;
  384. mlog_errno(status);
  385. goto bail;
  386. }
  387. (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
  388. (*ac)->ac_handle = handle;
  389. (*ac)->ac_which = OCFS2_AC_USE_META;
  390. #ifndef OCFS2_USE_ALL_METADATA_SUBALLOCATORS
  391. alloc_inode = ocfs2_get_system_file_inode(osb,
  392. EXTENT_ALLOC_SYSTEM_INODE,
  393. 0);
  394. #else
  395. alloc_inode = ocfs2_get_system_file_inode(osb,
  396. EXTENT_ALLOC_SYSTEM_INODE,
  397. osb->slot_num);
  398. #endif
  399. if (!alloc_inode) {
  400. status = -ENOMEM;
  401. mlog_errno(status);
  402. goto bail;
  403. }
  404. (*ac)->ac_inode = igrab(alloc_inode);
  405. (*ac)->ac_group_search = ocfs2_block_group_search;
  406. status = ocfs2_reserve_suballoc_bits(osb, (*ac));
  407. if (status < 0) {
  408. if (status != -ENOSPC)
  409. mlog_errno(status);
  410. goto bail;
  411. }
  412. status = 0;
  413. bail:
  414. if ((status < 0) && *ac) {
  415. ocfs2_free_alloc_context(*ac);
  416. *ac = NULL;
  417. }
  418. if (alloc_inode)
  419. iput(alloc_inode);
  420. mlog_exit(status);
  421. return status;
  422. }
  423. int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
  424. struct ocfs2_journal_handle *handle,
  425. struct ocfs2_alloc_context **ac)
  426. {
  427. int status;
  428. struct inode *alloc_inode = NULL;
  429. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  430. if (!(*ac)) {
  431. status = -ENOMEM;
  432. mlog_errno(status);
  433. goto bail;
  434. }
  435. (*ac)->ac_bits_wanted = 1;
  436. (*ac)->ac_handle = handle;
  437. (*ac)->ac_which = OCFS2_AC_USE_INODE;
  438. alloc_inode = ocfs2_get_system_file_inode(osb,
  439. INODE_ALLOC_SYSTEM_INODE,
  440. osb->slot_num);
  441. if (!alloc_inode) {
  442. status = -ENOMEM;
  443. mlog_errno(status);
  444. goto bail;
  445. }
  446. (*ac)->ac_inode = igrab(alloc_inode);
  447. (*ac)->ac_group_search = ocfs2_block_group_search;
  448. status = ocfs2_reserve_suballoc_bits(osb, *ac);
  449. if (status < 0) {
  450. if (status != -ENOSPC)
  451. mlog_errno(status);
  452. goto bail;
  453. }
  454. status = 0;
  455. bail:
  456. if ((status < 0) && *ac) {
  457. ocfs2_free_alloc_context(*ac);
  458. *ac = NULL;
  459. }
  460. if (alloc_inode)
  461. iput(alloc_inode);
  462. mlog_exit(status);
  463. return status;
  464. }
  465. /* local alloc code has to do the same thing, so rather than do this
  466. * twice.. */
  467. int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  468. struct ocfs2_alloc_context *ac)
  469. {
  470. int status;
  471. ac->ac_inode = ocfs2_get_system_file_inode(osb,
  472. GLOBAL_BITMAP_SYSTEM_INODE,
  473. OCFS2_INVALID_SLOT);
  474. if (!ac->ac_inode) {
  475. status = -EINVAL;
  476. mlog(ML_ERROR, "Could not get bitmap inode!\n");
  477. goto bail;
  478. }
  479. ac->ac_which = OCFS2_AC_USE_MAIN;
  480. ac->ac_group_search = ocfs2_cluster_group_search;
  481. status = ocfs2_reserve_suballoc_bits(osb, ac);
  482. if (status < 0 && status != -ENOSPC)
  483. mlog_errno(status);
  484. bail:
  485. return status;
  486. }
  487. /* Callers don't need to care which bitmap (local alloc or main) to
  488. * use so we figure it out for them, but unfortunately this clutters
  489. * things a bit. */
  490. int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  491. struct ocfs2_journal_handle *handle,
  492. u32 bits_wanted,
  493. struct ocfs2_alloc_context **ac)
  494. {
  495. int status;
  496. mlog_entry_void();
  497. BUG_ON(!handle);
  498. *ac = kcalloc(1, sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
  499. if (!(*ac)) {
  500. status = -ENOMEM;
  501. mlog_errno(status);
  502. goto bail;
  503. }
  504. (*ac)->ac_bits_wanted = bits_wanted;
  505. (*ac)->ac_handle = handle;
  506. status = -ENOSPC;
  507. if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
  508. status = ocfs2_reserve_local_alloc_bits(osb,
  509. handle,
  510. bits_wanted,
  511. *ac);
  512. if ((status < 0) && (status != -ENOSPC)) {
  513. mlog_errno(status);
  514. goto bail;
  515. } else if (status == -ENOSPC) {
  516. /* reserve_local_bits will return enospc with
  517. * the local alloc inode still locked, so we
  518. * can change this safely here. */
  519. mlog(0, "Disabling local alloc\n");
  520. /* We set to OCFS2_LA_DISABLED so that umount
  521. * can clean up what's left of the local
  522. * allocation */
  523. osb->local_alloc_state = OCFS2_LA_DISABLED;
  524. }
  525. }
  526. if (status == -ENOSPC) {
  527. status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  528. if (status < 0) {
  529. if (status != -ENOSPC)
  530. mlog_errno(status);
  531. goto bail;
  532. }
  533. }
  534. status = 0;
  535. bail:
  536. if ((status < 0) && *ac) {
  537. ocfs2_free_alloc_context(*ac);
  538. *ac = NULL;
  539. }
  540. mlog_exit(status);
  541. return status;
  542. }
  543. /*
  544. * More or less lifted from ext3. I'll leave their description below:
  545. *
  546. * "For ext3 allocations, we must not reuse any blocks which are
  547. * allocated in the bitmap buffer's "last committed data" copy. This
  548. * prevents deletes from freeing up the page for reuse until we have
  549. * committed the delete transaction.
  550. *
  551. * If we didn't do this, then deleting something and reallocating it as
  552. * data would allow the old block to be overwritten before the
  553. * transaction committed (because we force data to disk before commit).
  554. * This would lead to corruption if we crashed between overwriting the
  555. * data and committing the delete.
  556. *
  557. * @@@ We may want to make this allocation behaviour conditional on
  558. * data-writes at some point, and disable it for metadata allocations or
  559. * sync-data inodes."
  560. *
  561. * Note: OCFS2 already does this differently for metadata vs data
  562. * allocations, as those bitmaps are seperate and undo access is never
  563. * called on a metadata group descriptor.
  564. */
  565. static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  566. int nr)
  567. {
  568. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  569. if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  570. return 0;
  571. if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
  572. return 1;
  573. bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
  574. return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  575. }
  576. static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  577. struct buffer_head *bg_bh,
  578. unsigned int bits_wanted,
  579. u16 *bit_off,
  580. u16 *bits_found)
  581. {
  582. void *bitmap;
  583. u16 best_offset, best_size;
  584. int offset, start, found, status = 0;
  585. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  586. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  587. OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
  588. return -EIO;
  589. }
  590. found = start = best_offset = best_size = 0;
  591. bitmap = bg->bg_bitmap;
  592. while((offset = ocfs2_find_next_zero_bit(bitmap,
  593. le16_to_cpu(bg->bg_bits),
  594. start)) != -1) {
  595. if (offset == le16_to_cpu(bg->bg_bits))
  596. break;
  597. if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  598. /* We found a zero, but we can't use it as it
  599. * hasn't been put to disk yet! */
  600. found = 0;
  601. start = offset + 1;
  602. } else if (offset == start) {
  603. /* we found a zero */
  604. found++;
  605. /* move start to the next bit to test */
  606. start++;
  607. } else {
  608. /* got a zero after some ones */
  609. found = 1;
  610. start = offset + 1;
  611. }
  612. if (found > best_size) {
  613. best_size = found;
  614. best_offset = start - found;
  615. }
  616. /* we got everything we needed */
  617. if (found == bits_wanted) {
  618. /* mlog(0, "Found it all!\n"); */
  619. break;
  620. }
  621. }
  622. /* XXX: I think the first clause is equivalent to the second
  623. * - jlbec */
  624. if (found == bits_wanted) {
  625. *bit_off = start - found;
  626. *bits_found = found;
  627. } else if (best_size) {
  628. *bit_off = best_offset;
  629. *bits_found = best_size;
  630. } else {
  631. status = -ENOSPC;
  632. /* No error log here -- see the comment above
  633. * ocfs2_test_bg_bit_allocatable */
  634. }
  635. return status;
  636. }
  637. static inline int ocfs2_block_group_set_bits(struct ocfs2_journal_handle *handle,
  638. struct inode *alloc_inode,
  639. struct ocfs2_group_desc *bg,
  640. struct buffer_head *group_bh,
  641. unsigned int bit_off,
  642. unsigned int num_bits)
  643. {
  644. int status;
  645. void *bitmap = bg->bg_bitmap;
  646. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  647. mlog_entry_void();
  648. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  649. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  650. status = -EIO;
  651. goto bail;
  652. }
  653. BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  654. mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
  655. num_bits);
  656. if (ocfs2_is_cluster_bitmap(alloc_inode))
  657. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  658. status = ocfs2_journal_access(handle,
  659. alloc_inode,
  660. group_bh,
  661. journal_type);
  662. if (status < 0) {
  663. mlog_errno(status);
  664. goto bail;
  665. }
  666. le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  667. while(num_bits--)
  668. ocfs2_set_bit(bit_off++, bitmap);
  669. status = ocfs2_journal_dirty(handle,
  670. group_bh);
  671. if (status < 0) {
  672. mlog_errno(status);
  673. goto bail;
  674. }
  675. bail:
  676. mlog_exit(status);
  677. return status;
  678. }
  679. /* find the one with the most empty bits */
  680. static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  681. {
  682. u16 curr, best;
  683. BUG_ON(!cl->cl_next_free_rec);
  684. best = curr = 0;
  685. while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  686. if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  687. le32_to_cpu(cl->cl_recs[best].c_free))
  688. best = curr;
  689. curr++;
  690. }
  691. BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  692. return best;
  693. }
  694. static int ocfs2_relink_block_group(struct ocfs2_journal_handle *handle,
  695. struct inode *alloc_inode,
  696. struct buffer_head *fe_bh,
  697. struct buffer_head *bg_bh,
  698. struct buffer_head *prev_bg_bh,
  699. u16 chain)
  700. {
  701. int status;
  702. /* there is a really tiny chance the journal calls could fail,
  703. * but we wouldn't want inconsistent blocks in *any* case. */
  704. u64 fe_ptr, bg_ptr, prev_bg_ptr;
  705. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  706. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  707. struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
  708. if (!OCFS2_IS_VALID_DINODE(fe)) {
  709. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  710. status = -EIO;
  711. goto out;
  712. }
  713. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  714. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  715. status = -EIO;
  716. goto out;
  717. }
  718. if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
  719. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
  720. status = -EIO;
  721. goto out;
  722. }
  723. mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
  724. (unsigned long long)fe->i_blkno, chain,
  725. (unsigned long long)bg->bg_blkno,
  726. (unsigned long long)prev_bg->bg_blkno);
  727. fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  728. bg_ptr = le64_to_cpu(bg->bg_next_group);
  729. prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
  730. status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
  731. OCFS2_JOURNAL_ACCESS_WRITE);
  732. if (status < 0) {
  733. mlog_errno(status);
  734. goto out_rollback;
  735. }
  736. prev_bg->bg_next_group = bg->bg_next_group;
  737. status = ocfs2_journal_dirty(handle, prev_bg_bh);
  738. if (status < 0) {
  739. mlog_errno(status);
  740. goto out_rollback;
  741. }
  742. status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
  743. OCFS2_JOURNAL_ACCESS_WRITE);
  744. if (status < 0) {
  745. mlog_errno(status);
  746. goto out_rollback;
  747. }
  748. bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  749. status = ocfs2_journal_dirty(handle, bg_bh);
  750. if (status < 0) {
  751. mlog_errno(status);
  752. goto out_rollback;
  753. }
  754. status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
  755. OCFS2_JOURNAL_ACCESS_WRITE);
  756. if (status < 0) {
  757. mlog_errno(status);
  758. goto out_rollback;
  759. }
  760. fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  761. status = ocfs2_journal_dirty(handle, fe_bh);
  762. if (status < 0) {
  763. mlog_errno(status);
  764. goto out_rollback;
  765. }
  766. status = 0;
  767. out_rollback:
  768. if (status < 0) {
  769. fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  770. bg->bg_next_group = cpu_to_le64(bg_ptr);
  771. prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  772. }
  773. out:
  774. mlog_exit(status);
  775. return status;
  776. }
  777. static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  778. u32 wanted)
  779. {
  780. return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  781. }
  782. /* return 0 on success, -ENOSPC to keep searching and any other < 0
  783. * value on error. */
  784. static int ocfs2_cluster_group_search(struct inode *inode,
  785. struct buffer_head *group_bh,
  786. u32 bits_wanted, u32 min_bits,
  787. u16 *bit_off, u16 *bits_found)
  788. {
  789. int search = -ENOSPC;
  790. int ret;
  791. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  792. u16 tmp_off, tmp_found;
  793. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  794. if (bg->bg_free_bits_count) {
  795. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  796. group_bh, bits_wanted,
  797. &tmp_off, &tmp_found);
  798. if (ret)
  799. return ret;
  800. /* ocfs2_block_group_find_clear_bits() might
  801. * return success, but we still want to return
  802. * -ENOSPC unless it found the minimum number
  803. * of bits. */
  804. if (min_bits <= tmp_found) {
  805. *bit_off = tmp_off;
  806. *bits_found = tmp_found;
  807. search = 0; /* success */
  808. }
  809. }
  810. return search;
  811. }
  812. static int ocfs2_block_group_search(struct inode *inode,
  813. struct buffer_head *group_bh,
  814. u32 bits_wanted, u32 min_bits,
  815. u16 *bit_off, u16 *bits_found)
  816. {
  817. int ret = -ENOSPC;
  818. struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  819. BUG_ON(min_bits != 1);
  820. BUG_ON(ocfs2_is_cluster_bitmap(inode));
  821. if (bg->bg_free_bits_count)
  822. ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  823. group_bh, bits_wanted,
  824. bit_off, bits_found);
  825. return ret;
  826. }
  827. static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
  828. u32 bits_wanted,
  829. u32 min_bits,
  830. u16 *bit_off,
  831. unsigned int *num_bits,
  832. u64 *bg_blkno)
  833. {
  834. int status;
  835. u16 chain, tmp_bits;
  836. u32 tmp_used;
  837. u64 next_group;
  838. struct ocfs2_journal_handle *handle = ac->ac_handle;
  839. struct inode *alloc_inode = ac->ac_inode;
  840. struct buffer_head *group_bh = NULL;
  841. struct buffer_head *prev_group_bh = NULL;
  842. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  843. struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  844. struct ocfs2_group_desc *bg;
  845. chain = ac->ac_chain;
  846. mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
  847. bits_wanted, chain,
  848. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
  849. status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
  850. le64_to_cpu(cl->cl_recs[chain].c_blkno),
  851. &group_bh, OCFS2_BH_CACHED, alloc_inode);
  852. if (status < 0) {
  853. mlog_errno(status);
  854. goto bail;
  855. }
  856. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  857. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  858. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  859. status = -EIO;
  860. goto bail;
  861. }
  862. status = -ENOSPC;
  863. /* for now, the chain search is a bit simplistic. We just use
  864. * the 1st group with any empty bits. */
  865. while ((status = ac->ac_group_search(alloc_inode, group_bh,
  866. bits_wanted, min_bits, bit_off,
  867. &tmp_bits)) == -ENOSPC) {
  868. if (!bg->bg_next_group)
  869. break;
  870. if (prev_group_bh) {
  871. brelse(prev_group_bh);
  872. prev_group_bh = NULL;
  873. }
  874. next_group = le64_to_cpu(bg->bg_next_group);
  875. prev_group_bh = group_bh;
  876. group_bh = NULL;
  877. status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
  878. next_group, &group_bh,
  879. OCFS2_BH_CACHED, alloc_inode);
  880. if (status < 0) {
  881. mlog_errno(status);
  882. goto bail;
  883. }
  884. bg = (struct ocfs2_group_desc *) group_bh->b_data;
  885. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  886. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  887. status = -EIO;
  888. goto bail;
  889. }
  890. }
  891. if (status < 0) {
  892. if (status != -ENOSPC)
  893. mlog_errno(status);
  894. goto bail;
  895. }
  896. mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
  897. tmp_bits, (unsigned long long)bg->bg_blkno);
  898. *num_bits = tmp_bits;
  899. BUG_ON(*num_bits == 0);
  900. /*
  901. * Keep track of previous block descriptor read. When
  902. * we find a target, if we have read more than X
  903. * number of descriptors, and the target is reasonably
  904. * empty, relink him to top of his chain.
  905. *
  906. * We've read 0 extra blocks and only send one more to
  907. * the transaction, yet the next guy to search has a
  908. * much easier time.
  909. *
  910. * Do this *after* figuring out how many bits we're taking out
  911. * of our target group.
  912. */
  913. if (ac->ac_allow_chain_relink &&
  914. (prev_group_bh) &&
  915. (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
  916. status = ocfs2_relink_block_group(handle, alloc_inode,
  917. ac->ac_bh, group_bh,
  918. prev_group_bh, chain);
  919. if (status < 0) {
  920. mlog_errno(status);
  921. goto bail;
  922. }
  923. }
  924. /* Ok, claim our bits now: set the info on dinode, chainlist
  925. * and then the group */
  926. status = ocfs2_journal_access(handle,
  927. alloc_inode,
  928. ac->ac_bh,
  929. OCFS2_JOURNAL_ACCESS_WRITE);
  930. if (status < 0) {
  931. mlog_errno(status);
  932. goto bail;
  933. }
  934. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  935. fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
  936. le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
  937. status = ocfs2_journal_dirty(handle,
  938. ac->ac_bh);
  939. if (status < 0) {
  940. mlog_errno(status);
  941. goto bail;
  942. }
  943. status = ocfs2_block_group_set_bits(handle,
  944. alloc_inode,
  945. bg,
  946. group_bh,
  947. *bit_off,
  948. *num_bits);
  949. if (status < 0) {
  950. mlog_errno(status);
  951. goto bail;
  952. }
  953. mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
  954. (unsigned long long)fe->i_blkno);
  955. *bg_blkno = le64_to_cpu(bg->bg_blkno);
  956. bail:
  957. if (group_bh)
  958. brelse(group_bh);
  959. if (prev_group_bh)
  960. brelse(prev_group_bh);
  961. mlog_exit(status);
  962. return status;
  963. }
  964. /* will give out up to bits_wanted contiguous bits. */
  965. static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  966. struct ocfs2_alloc_context *ac,
  967. u32 bits_wanted,
  968. u32 min_bits,
  969. u16 *bit_off,
  970. unsigned int *num_bits,
  971. u64 *bg_blkno)
  972. {
  973. int status;
  974. u16 victim, i;
  975. struct ocfs2_chain_list *cl;
  976. struct ocfs2_dinode *fe;
  977. mlog_entry_void();
  978. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  979. BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  980. BUG_ON(!ac->ac_bh);
  981. fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  982. if (!OCFS2_IS_VALID_DINODE(fe)) {
  983. OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
  984. status = -EIO;
  985. goto bail;
  986. }
  987. if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  988. le32_to_cpu(fe->id1.bitmap1.i_total)) {
  989. ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
  990. "bits but only %u total.",
  991. (unsigned long long)le64_to_cpu(fe->i_blkno),
  992. le32_to_cpu(fe->id1.bitmap1.i_used),
  993. le32_to_cpu(fe->id1.bitmap1.i_total));
  994. status = -EIO;
  995. goto bail;
  996. }
  997. cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  998. victim = ocfs2_find_victim_chain(cl);
  999. ac->ac_chain = victim;
  1000. ac->ac_allow_chain_relink = 1;
  1001. status = ocfs2_search_chain(ac, bits_wanted, min_bits, bit_off,
  1002. num_bits, bg_blkno);
  1003. if (!status)
  1004. goto bail;
  1005. if (status < 0 && status != -ENOSPC) {
  1006. mlog_errno(status);
  1007. goto bail;
  1008. }
  1009. mlog(0, "Search of victim chain %u came up with nothing, "
  1010. "trying all chains now.\n", victim);
  1011. /* If we didn't pick a good victim, then just default to
  1012. * searching each chain in order. Don't allow chain relinking
  1013. * because we only calculate enough journal credits for one
  1014. * relink per alloc. */
  1015. ac->ac_allow_chain_relink = 0;
  1016. for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  1017. if (i == victim)
  1018. continue;
  1019. if (!cl->cl_recs[i].c_free)
  1020. continue;
  1021. ac->ac_chain = i;
  1022. status = ocfs2_search_chain(ac, bits_wanted, min_bits,
  1023. bit_off, num_bits,
  1024. bg_blkno);
  1025. if (!status)
  1026. break;
  1027. if (status < 0 && status != -ENOSPC) {
  1028. mlog_errno(status);
  1029. goto bail;
  1030. }
  1031. }
  1032. bail:
  1033. mlog_exit(status);
  1034. return status;
  1035. }
  1036. int ocfs2_claim_metadata(struct ocfs2_super *osb,
  1037. struct ocfs2_journal_handle *handle,
  1038. struct ocfs2_alloc_context *ac,
  1039. u32 bits_wanted,
  1040. u16 *suballoc_bit_start,
  1041. unsigned int *num_bits,
  1042. u64 *blkno_start)
  1043. {
  1044. int status;
  1045. u64 bg_blkno;
  1046. BUG_ON(!ac);
  1047. BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  1048. BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
  1049. BUG_ON(ac->ac_handle != handle);
  1050. status = ocfs2_claim_suballoc_bits(osb,
  1051. ac,
  1052. bits_wanted,
  1053. 1,
  1054. suballoc_bit_start,
  1055. num_bits,
  1056. &bg_blkno);
  1057. if (status < 0) {
  1058. mlog_errno(status);
  1059. goto bail;
  1060. }
  1061. atomic_inc(&osb->alloc_stats.bg_allocs);
  1062. *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
  1063. ac->ac_bits_given += (*num_bits);
  1064. status = 0;
  1065. bail:
  1066. mlog_exit(status);
  1067. return status;
  1068. }
  1069. int ocfs2_claim_new_inode(struct ocfs2_super *osb,
  1070. struct ocfs2_journal_handle *handle,
  1071. struct ocfs2_alloc_context *ac,
  1072. u16 *suballoc_bit,
  1073. u64 *fe_blkno)
  1074. {
  1075. int status;
  1076. unsigned int num_bits;
  1077. u64 bg_blkno;
  1078. mlog_entry_void();
  1079. BUG_ON(!ac);
  1080. BUG_ON(ac->ac_bits_given != 0);
  1081. BUG_ON(ac->ac_bits_wanted != 1);
  1082. BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  1083. BUG_ON(ac->ac_handle != handle);
  1084. status = ocfs2_claim_suballoc_bits(osb,
  1085. ac,
  1086. 1,
  1087. 1,
  1088. suballoc_bit,
  1089. &num_bits,
  1090. &bg_blkno);
  1091. if (status < 0) {
  1092. mlog_errno(status);
  1093. goto bail;
  1094. }
  1095. atomic_inc(&osb->alloc_stats.bg_allocs);
  1096. BUG_ON(num_bits != 1);
  1097. *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
  1098. ac->ac_bits_given++;
  1099. status = 0;
  1100. bail:
  1101. mlog_exit(status);
  1102. return status;
  1103. }
  1104. /* translate a group desc. blkno and it's bitmap offset into
  1105. * disk cluster offset. */
  1106. static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  1107. u64 bg_blkno,
  1108. u16 bg_bit_off)
  1109. {
  1110. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1111. u32 cluster = 0;
  1112. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1113. if (bg_blkno != osb->first_cluster_group_blkno)
  1114. cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  1115. cluster += (u32) bg_bit_off;
  1116. return cluster;
  1117. }
  1118. /* given a cluster offset, calculate which block group it belongs to
  1119. * and return that block offset. */
  1120. static inline u64 ocfs2_which_cluster_group(struct inode *inode,
  1121. u32 cluster)
  1122. {
  1123. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1124. u32 group_no;
  1125. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1126. group_no = cluster / osb->bitmap_cpg;
  1127. if (!group_no)
  1128. return osb->first_cluster_group_blkno;
  1129. return ocfs2_clusters_to_blocks(inode->i_sb,
  1130. group_no * osb->bitmap_cpg);
  1131. }
  1132. /* given the block number of a cluster start, calculate which cluster
  1133. * group and descriptor bitmap offset that corresponds to. */
  1134. static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  1135. u64 data_blkno,
  1136. u64 *bg_blkno,
  1137. u16 *bg_bit_off)
  1138. {
  1139. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1140. u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  1141. BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  1142. *bg_blkno = ocfs2_which_cluster_group(inode,
  1143. data_cluster);
  1144. if (*bg_blkno == osb->first_cluster_group_blkno)
  1145. *bg_bit_off = (u16) data_cluster;
  1146. else
  1147. *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  1148. data_blkno - *bg_blkno);
  1149. }
  1150. /*
  1151. * min_bits - minimum contiguous chunk from this total allocation we
  1152. * can handle. set to what we asked for originally for a full
  1153. * contig. allocation, set to '1' to indicate we can deal with extents
  1154. * of any size.
  1155. */
  1156. int ocfs2_claim_clusters(struct ocfs2_super *osb,
  1157. struct ocfs2_journal_handle *handle,
  1158. struct ocfs2_alloc_context *ac,
  1159. u32 min_clusters,
  1160. u32 *cluster_start,
  1161. u32 *num_clusters)
  1162. {
  1163. int status;
  1164. unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
  1165. u64 bg_blkno;
  1166. u16 bg_bit_off;
  1167. mlog_entry_void();
  1168. BUG_ON(!ac);
  1169. BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  1170. BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  1171. && ac->ac_which != OCFS2_AC_USE_MAIN);
  1172. BUG_ON(ac->ac_handle != handle);
  1173. if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  1174. status = ocfs2_claim_local_alloc_bits(osb,
  1175. handle,
  1176. ac,
  1177. bits_wanted,
  1178. cluster_start,
  1179. num_clusters);
  1180. if (!status)
  1181. atomic_inc(&osb->alloc_stats.local_data);
  1182. } else {
  1183. if (min_clusters > (osb->bitmap_cpg - 1)) {
  1184. /* The only paths asking for contiguousness
  1185. * should know about this already. */
  1186. mlog(ML_ERROR, "minimum allocation requested exceeds "
  1187. "group bitmap size!");
  1188. status = -ENOSPC;
  1189. goto bail;
  1190. }
  1191. /* clamp the current request down to a realistic size. */
  1192. if (bits_wanted > (osb->bitmap_cpg - 1))
  1193. bits_wanted = osb->bitmap_cpg - 1;
  1194. status = ocfs2_claim_suballoc_bits(osb,
  1195. ac,
  1196. bits_wanted,
  1197. min_clusters,
  1198. &bg_bit_off,
  1199. num_clusters,
  1200. &bg_blkno);
  1201. if (!status) {
  1202. *cluster_start =
  1203. ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  1204. bg_blkno,
  1205. bg_bit_off);
  1206. atomic_inc(&osb->alloc_stats.bitmap_data);
  1207. }
  1208. }
  1209. if (status < 0) {
  1210. if (status != -ENOSPC)
  1211. mlog_errno(status);
  1212. goto bail;
  1213. }
  1214. ac->ac_bits_given += *num_clusters;
  1215. bail:
  1216. mlog_exit(status);
  1217. return status;
  1218. }
  1219. static inline int ocfs2_block_group_clear_bits(struct ocfs2_journal_handle *handle,
  1220. struct inode *alloc_inode,
  1221. struct ocfs2_group_desc *bg,
  1222. struct buffer_head *group_bh,
  1223. unsigned int bit_off,
  1224. unsigned int num_bits)
  1225. {
  1226. int status;
  1227. unsigned int tmp;
  1228. int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  1229. struct ocfs2_group_desc *undo_bg = NULL;
  1230. mlog_entry_void();
  1231. if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
  1232. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
  1233. status = -EIO;
  1234. goto bail;
  1235. }
  1236. mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
  1237. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1238. journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
  1239. status = ocfs2_journal_access(handle, alloc_inode, group_bh,
  1240. journal_type);
  1241. if (status < 0) {
  1242. mlog_errno(status);
  1243. goto bail;
  1244. }
  1245. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1246. undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
  1247. tmp = num_bits;
  1248. while(tmp--) {
  1249. ocfs2_clear_bit((bit_off + tmp),
  1250. (unsigned long *) bg->bg_bitmap);
  1251. if (ocfs2_is_cluster_bitmap(alloc_inode))
  1252. ocfs2_set_bit(bit_off + tmp,
  1253. (unsigned long *) undo_bg->bg_bitmap);
  1254. }
  1255. le16_add_cpu(&bg->bg_free_bits_count, num_bits);
  1256. status = ocfs2_journal_dirty(handle, group_bh);
  1257. if (status < 0)
  1258. mlog_errno(status);
  1259. bail:
  1260. return status;
  1261. }
  1262. /*
  1263. * expects the suballoc inode to already be locked.
  1264. */
  1265. static int ocfs2_free_suballoc_bits(struct ocfs2_journal_handle *handle,
  1266. struct inode *alloc_inode,
  1267. struct buffer_head *alloc_bh,
  1268. unsigned int start_bit,
  1269. u64 bg_blkno,
  1270. unsigned int count)
  1271. {
  1272. int status = 0;
  1273. u32 tmp_used;
  1274. struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  1275. struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  1276. struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  1277. struct buffer_head *group_bh = NULL;
  1278. struct ocfs2_group_desc *group;
  1279. mlog_entry_void();
  1280. if (!OCFS2_IS_VALID_DINODE(fe)) {
  1281. OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
  1282. status = -EIO;
  1283. goto bail;
  1284. }
  1285. BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
  1286. mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
  1287. (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
  1288. (unsigned long long)bg_blkno, start_bit);
  1289. status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
  1290. alloc_inode);
  1291. if (status < 0) {
  1292. mlog_errno(status);
  1293. goto bail;
  1294. }
  1295. group = (struct ocfs2_group_desc *) group_bh->b_data;
  1296. if (!OCFS2_IS_VALID_GROUP_DESC(group)) {
  1297. OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, group);
  1298. status = -EIO;
  1299. goto bail;
  1300. }
  1301. BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  1302. status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  1303. group, group_bh,
  1304. start_bit, count);
  1305. if (status < 0) {
  1306. mlog_errno(status);
  1307. goto bail;
  1308. }
  1309. status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
  1310. OCFS2_JOURNAL_ACCESS_WRITE);
  1311. if (status < 0) {
  1312. mlog_errno(status);
  1313. goto bail;
  1314. }
  1315. le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  1316. count);
  1317. tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  1318. fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
  1319. status = ocfs2_journal_dirty(handle, alloc_bh);
  1320. if (status < 0) {
  1321. mlog_errno(status);
  1322. goto bail;
  1323. }
  1324. bail:
  1325. if (group_bh)
  1326. brelse(group_bh);
  1327. mlog_exit(status);
  1328. return status;
  1329. }
  1330. static inline u64 ocfs2_which_suballoc_group(u64 block, unsigned int bit)
  1331. {
  1332. u64 group = block - (u64) bit;
  1333. return group;
  1334. }
  1335. int ocfs2_free_dinode(struct ocfs2_journal_handle *handle,
  1336. struct inode *inode_alloc_inode,
  1337. struct buffer_head *inode_alloc_bh,
  1338. struct ocfs2_dinode *di)
  1339. {
  1340. u64 blk = le64_to_cpu(di->i_blkno);
  1341. u16 bit = le16_to_cpu(di->i_suballoc_bit);
  1342. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1343. return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  1344. inode_alloc_bh, bit, bg_blkno, 1);
  1345. }
  1346. int ocfs2_free_extent_block(struct ocfs2_journal_handle *handle,
  1347. struct inode *eb_alloc_inode,
  1348. struct buffer_head *eb_alloc_bh,
  1349. struct ocfs2_extent_block *eb)
  1350. {
  1351. u64 blk = le64_to_cpu(eb->h_blkno);
  1352. u16 bit = le16_to_cpu(eb->h_suballoc_bit);
  1353. u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
  1354. return ocfs2_free_suballoc_bits(handle, eb_alloc_inode, eb_alloc_bh,
  1355. bit, bg_blkno, 1);
  1356. }
  1357. int ocfs2_free_clusters(struct ocfs2_journal_handle *handle,
  1358. struct inode *bitmap_inode,
  1359. struct buffer_head *bitmap_bh,
  1360. u64 start_blk,
  1361. unsigned int num_clusters)
  1362. {
  1363. int status;
  1364. u16 bg_start_bit;
  1365. u64 bg_blkno;
  1366. struct ocfs2_dinode *fe;
  1367. /* You can't ever have a contiguous set of clusters
  1368. * bigger than a block group bitmap so we never have to worry
  1369. * about looping on them. */
  1370. mlog_entry_void();
  1371. /* This is expensive. We can safely remove once this stuff has
  1372. * gotten tested really well. */
  1373. BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  1374. fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  1375. ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  1376. &bg_start_bit);
  1377. mlog(0, "want to free %u clusters starting at block %llu\n",
  1378. num_clusters, (unsigned long long)start_blk);
  1379. mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
  1380. (unsigned long long)bg_blkno, bg_start_bit);
  1381. status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  1382. bg_start_bit, bg_blkno,
  1383. num_clusters);
  1384. if (status < 0)
  1385. mlog_errno(status);
  1386. mlog_exit(status);
  1387. return status;
  1388. }
  1389. static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  1390. {
  1391. printk("Block Group:\n");
  1392. printk("bg_signature: %s\n", bg->bg_signature);
  1393. printk("bg_size: %u\n", bg->bg_size);
  1394. printk("bg_bits: %u\n", bg->bg_bits);
  1395. printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
  1396. printk("bg_chain: %u\n", bg->bg_chain);
  1397. printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
  1398. printk("bg_next_group: %llu\n",
  1399. (unsigned long long)bg->bg_next_group);
  1400. printk("bg_parent_dinode: %llu\n",
  1401. (unsigned long long)bg->bg_parent_dinode);
  1402. printk("bg_blkno: %llu\n",
  1403. (unsigned long long)bg->bg_blkno);
  1404. }
  1405. static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  1406. {
  1407. int i;
  1408. printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
  1409. printk("i_signature: %s\n", fe->i_signature);
  1410. printk("i_size: %llu\n",
  1411. (unsigned long long)fe->i_size);
  1412. printk("i_clusters: %u\n", fe->i_clusters);
  1413. printk("i_generation: %u\n",
  1414. le32_to_cpu(fe->i_generation));
  1415. printk("id1.bitmap1.i_used: %u\n",
  1416. le32_to_cpu(fe->id1.bitmap1.i_used));
  1417. printk("id1.bitmap1.i_total: %u\n",
  1418. le32_to_cpu(fe->id1.bitmap1.i_total));
  1419. printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
  1420. printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
  1421. printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
  1422. printk("id2.i_chain.cl_next_free_rec: %u\n",
  1423. fe->id2.i_chain.cl_next_free_rec);
  1424. for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  1425. printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
  1426. fe->id2.i_chain.cl_recs[i].c_free);
  1427. printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
  1428. fe->id2.i_chain.cl_recs[i].c_total);
  1429. printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
  1430. (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
  1431. }
  1432. }