suballoc.c 43 KB

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