suballoc.c 53 KB

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