blob: d9c5c9fcb30fc300bb9132f54f0526156f5847b8 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
38#include "dlmglue.h"
39#include "inode.h"
40#include "journal.h"
41#include "localalloc.h"
42#include "suballoc.h"
43#include "super.h"
44#include "sysfile.h"
45#include "uptodate.h"
46
47#include "buffer_head_io.h"
48
49static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
50static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
51static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070052static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080053 struct inode *alloc_inode,
54 struct buffer_head *bg_bh,
55 u64 group_blkno,
56 u16 my_chain,
57 struct ocfs2_chain_list *cl);
58static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
59 struct inode *alloc_inode,
60 struct buffer_head *bh);
61
Mark Fashehccd979b2005-12-15 14:31:24 -080062static int ocfs2_cluster_group_search(struct inode *inode,
63 struct buffer_head *group_bh,
64 u32 bits_wanted, u32 min_bits,
65 u16 *bit_off, u16 *bits_found);
66static int ocfs2_block_group_search(struct inode *inode,
67 struct buffer_head *group_bh,
68 u32 bits_wanted, u32 min_bits,
69 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080070static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
71 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070072 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 u32 bits_wanted,
74 u32 min_bits,
75 u16 *bit_off,
76 unsigned int *num_bits,
77 u64 *bg_blkno);
78static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
79 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070080static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080081 struct inode *alloc_inode,
82 struct ocfs2_group_desc *bg,
83 struct buffer_head *group_bh,
84 unsigned int bit_off,
85 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070086static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080087 struct inode *alloc_inode,
88 struct ocfs2_group_desc *bg,
89 struct buffer_head *group_bh,
90 unsigned int bit_off,
91 unsigned int num_bits);
92
Mark Fasheh1fabe142006-10-09 18:11:45 -070093static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 struct inode *alloc_inode,
95 struct buffer_head *fe_bh,
96 struct buffer_head *bg_bh,
97 struct buffer_head *prev_bg_bh,
98 u16 chain);
99static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
100 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800101static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
102 u64 bg_blkno,
103 u16 bg_bit_off);
104static inline u64 ocfs2_which_cluster_group(struct inode *inode,
105 u32 cluster);
106static inline void ocfs2_block_to_cluster_group(struct inode *inode,
107 u64 data_blkno,
108 u64 *bg_blkno,
109 u16 *bg_bit_off);
110
111void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
112{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700113 struct inode *inode = ac->ac_inode;
114
115 if (inode) {
116 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
117 ocfs2_meta_unlock(inode, 1);
118
119 mutex_unlock(&inode->i_mutex);
120
121 iput(inode);
122 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800123 if (ac->ac_bh)
124 brelse(ac->ac_bh);
125 kfree(ac);
126}
127
128static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
129{
130 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
131}
132
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700133/* somewhat more expensive than our other checks, so use sparingly. */
134static int ocfs2_check_group_descriptor(struct super_block *sb,
135 struct ocfs2_dinode *di,
136 struct ocfs2_group_desc *gd)
137{
138 unsigned int max_bits;
139
140 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
141 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
142 return -EIO;
143 }
144
145 if (di->i_blkno != gd->bg_parent_dinode) {
146 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
147 "pointer (%llu, expected %llu)",
148 (unsigned long long)le64_to_cpu(gd->bg_blkno),
149 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
150 (unsigned long long)le64_to_cpu(di->i_blkno));
151 return -EIO;
152 }
153
154 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
155 if (le16_to_cpu(gd->bg_bits) > max_bits) {
156 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
157 (unsigned long long)le64_to_cpu(gd->bg_blkno),
158 le16_to_cpu(gd->bg_bits));
159 return -EIO;
160 }
161
162 if (le16_to_cpu(gd->bg_chain) >=
163 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
164 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
165 (unsigned long long)le64_to_cpu(gd->bg_blkno),
166 le16_to_cpu(gd->bg_chain));
167 return -EIO;
168 }
169
170 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
171 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
172 "claims that %u are free",
173 (unsigned long long)le64_to_cpu(gd->bg_blkno),
174 le16_to_cpu(gd->bg_bits),
175 le16_to_cpu(gd->bg_free_bits_count));
176 return -EIO;
177 }
178
179 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
180 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
181 "max bitmap bits of %u",
182 (unsigned long long)le64_to_cpu(gd->bg_blkno),
183 le16_to_cpu(gd->bg_bits),
184 8 * le16_to_cpu(gd->bg_size));
185 return -EIO;
186 }
187
188 return 0;
189}
190
Mark Fasheh1fabe142006-10-09 18:11:45 -0700191static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800192 struct inode *alloc_inode,
193 struct buffer_head *bg_bh,
194 u64 group_blkno,
195 u16 my_chain,
196 struct ocfs2_chain_list *cl)
197{
198 int status = 0;
199 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
200 struct super_block * sb = alloc_inode->i_sb;
201
202 mlog_entry_void();
203
204 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800205 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
206 "b_blocknr (%llu)",
207 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800208 (unsigned long long) bg_bh->b_blocknr);
209 status = -EIO;
210 goto bail;
211 }
212
213 status = ocfs2_journal_access(handle,
214 alloc_inode,
215 bg_bh,
216 OCFS2_JOURNAL_ACCESS_CREATE);
217 if (status < 0) {
218 mlog_errno(status);
219 goto bail;
220 }
221
222 memset(bg, 0, sb->s_blocksize);
223 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
224 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
225 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
226 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
227 bg->bg_chain = cpu_to_le16(my_chain);
228 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
229 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
230 bg->bg_blkno = cpu_to_le64(group_blkno);
231 /* set the 1st bit in the bitmap to account for the descriptor block */
232 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
233 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
234
235 status = ocfs2_journal_dirty(handle, bg_bh);
236 if (status < 0)
237 mlog_errno(status);
238
239 /* There is no need to zero out or otherwise initialize the
240 * other blocks in a group - All valid FS metadata in a block
241 * group stores the superblock fs_generation value at
242 * allocation time. */
243
244bail:
245 mlog_exit(status);
246 return status;
247}
248
249static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
250{
251 u16 curr, best;
252
253 best = curr = 0;
254 while (curr < le16_to_cpu(cl->cl_count)) {
255 if (le32_to_cpu(cl->cl_recs[best].c_total) >
256 le32_to_cpu(cl->cl_recs[curr].c_total))
257 best = curr;
258 curr++;
259 }
260 return best;
261}
262
263/*
264 * We expect the block group allocator to already be locked.
265 */
266static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
267 struct inode *alloc_inode,
268 struct buffer_head *bh)
269{
270 int status, credits;
271 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
272 struct ocfs2_chain_list *cl;
273 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700274 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800275 u32 bit_off, num_bits;
276 u16 alloc_rec;
277 u64 bg_blkno;
278 struct buffer_head *bg_bh = NULL;
279 struct ocfs2_group_desc *bg;
280
281 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
282
283 mlog_entry_void();
284
Mark Fashehccd979b2005-12-15 14:31:24 -0800285 cl = &fe->id2.i_chain;
286 status = ocfs2_reserve_clusters(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800287 le16_to_cpu(cl->cl_cpg),
288 &ac);
289 if (status < 0) {
290 if (status != -ENOSPC)
291 mlog_errno(status);
292 goto bail;
293 }
294
295 credits = ocfs2_calc_group_alloc_credits(osb->sb,
296 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700297 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800298 if (IS_ERR(handle)) {
299 status = PTR_ERR(handle);
300 handle = NULL;
301 mlog_errno(status);
302 goto bail;
303 }
304
305 status = ocfs2_claim_clusters(osb,
306 handle,
307 ac,
308 le16_to_cpu(cl->cl_cpg),
309 &bit_off,
310 &num_bits);
311 if (status < 0) {
312 if (status != -ENOSPC)
313 mlog_errno(status);
314 goto bail;
315 }
316
317 alloc_rec = ocfs2_find_smallest_chain(cl);
318
319 /* setup the group */
320 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800321 mlog(0, "new descriptor, record %u, at block %llu\n",
322 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800323
324 bg_bh = sb_getblk(osb->sb, bg_blkno);
325 if (!bg_bh) {
326 status = -EIO;
327 mlog_errno(status);
328 goto bail;
329 }
330 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
331
332 status = ocfs2_block_group_fill(handle,
333 alloc_inode,
334 bg_bh,
335 bg_blkno,
336 alloc_rec,
337 cl);
338 if (status < 0) {
339 mlog_errno(status);
340 goto bail;
341 }
342
343 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
344
345 status = ocfs2_journal_access(handle, alloc_inode,
346 bh, OCFS2_JOURNAL_ACCESS_WRITE);
347 if (status < 0) {
348 mlog_errno(status);
349 goto bail;
350 }
351
352 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
353 le16_to_cpu(bg->bg_free_bits_count));
354 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
355 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
356 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
357 le16_add_cpu(&cl->cl_next_free_rec, 1);
358
359 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
360 le16_to_cpu(bg->bg_free_bits_count));
361 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
362 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
363
364 status = ocfs2_journal_dirty(handle, bh);
365 if (status < 0) {
366 mlog_errno(status);
367 goto bail;
368 }
369
370 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
371 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
372 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
373 le32_to_cpu(fe->i_clusters)));
374 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
375 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700376 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800377
378 status = 0;
379bail:
380 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700381 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800382
383 if (ac)
384 ocfs2_free_alloc_context(ac);
385
386 if (bg_bh)
387 brelse(bg_bh);
388
389 mlog_exit(status);
390 return status;
391}
392
393static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700394 struct ocfs2_alloc_context *ac,
395 int type,
396 u32 slot)
Mark Fashehccd979b2005-12-15 14:31:24 -0800397{
398 int status;
399 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700400 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800401 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800402 struct ocfs2_dinode *fe;
403 u32 free_bits;
404
405 mlog_entry_void();
406
Mark Fashehda5cbf22006-10-06 18:34:35 -0700407 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
408 if (!alloc_inode) {
409 mlog_errno(-EINVAL);
410 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800411 }
412
Mark Fashehda5cbf22006-10-06 18:34:35 -0700413 mutex_lock(&alloc_inode->i_mutex);
414
Mark Fasheh4bcec182006-10-09 16:02:40 -0700415 status = ocfs2_meta_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700416 if (status < 0) {
417 mutex_unlock(&alloc_inode->i_mutex);
418 iput(alloc_inode);
419
420 mlog_errno(status);
421 return status;
422 }
423
424 ac->ac_inode = alloc_inode;
425
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 fe = (struct ocfs2_dinode *) bh->b_data;
427 if (!OCFS2_IS_VALID_DINODE(fe)) {
428 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
429 status = -EIO;
430 goto bail;
431 }
432 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800433 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
434 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800435 status = -EIO;
436 goto bail;
437 }
438
439 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
440 le32_to_cpu(fe->id1.bitmap1.i_used);
441
442 if (bits_wanted > free_bits) {
443 /* cluster bitmap never grows */
444 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
445 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
446 bits_wanted, free_bits);
447 status = -ENOSPC;
448 goto bail;
449 }
450
451 status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
452 if (status < 0) {
453 if (status != -ENOSPC)
454 mlog_errno(status);
455 goto bail;
456 }
457 atomic_inc(&osb->alloc_stats.bg_extends);
458
459 /* You should never ask for this much metadata */
460 BUG_ON(bits_wanted >
461 (le32_to_cpu(fe->id1.bitmap1.i_total)
462 - le32_to_cpu(fe->id1.bitmap1.i_used)));
463 }
464
465 get_bh(bh);
466 ac->ac_bh = bh;
467bail:
468 if (bh)
469 brelse(bh);
470
471 mlog_exit(status);
472 return status;
473}
474
475int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800476 struct ocfs2_dinode *fe,
477 struct ocfs2_alloc_context **ac)
478{
479 int status;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700480 u32 slot;
Mark Fashehccd979b2005-12-15 14:31:24 -0800481
Robert P. J. Daycd861282006-12-13 00:34:52 -0800482 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800483 if (!(*ac)) {
484 status = -ENOMEM;
485 mlog_errno(status);
486 goto bail;
487 }
488
489 (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -0800490 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700491 slot = osb->slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -0800492 (*ac)->ac_group_search = ocfs2_block_group_search;
493
Mark Fashehda5cbf22006-10-06 18:34:35 -0700494 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
495 EXTENT_ALLOC_SYSTEM_INODE, slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 if (status < 0) {
497 if (status != -ENOSPC)
498 mlog_errno(status);
499 goto bail;
500 }
501
502 status = 0;
503bail:
504 if ((status < 0) && *ac) {
505 ocfs2_free_alloc_context(*ac);
506 *ac = NULL;
507 }
508
Mark Fashehccd979b2005-12-15 14:31:24 -0800509 mlog_exit(status);
510 return status;
511}
512
513int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800514 struct ocfs2_alloc_context **ac)
515{
516 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -0800517
Robert P. J. Daycd861282006-12-13 00:34:52 -0800518 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800519 if (!(*ac)) {
520 status = -ENOMEM;
521 mlog_errno(status);
522 goto bail;
523 }
524
525 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800526 (*ac)->ac_which = OCFS2_AC_USE_INODE;
527
Mark Fashehccd979b2005-12-15 14:31:24 -0800528 (*ac)->ac_group_search = ocfs2_block_group_search;
529
Mark Fashehda5cbf22006-10-06 18:34:35 -0700530 status = ocfs2_reserve_suballoc_bits(osb, *ac,
531 INODE_ALLOC_SYSTEM_INODE,
532 osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800533 if (status < 0) {
534 if (status != -ENOSPC)
535 mlog_errno(status);
536 goto bail;
537 }
538
539 status = 0;
540bail:
541 if ((status < 0) && *ac) {
542 ocfs2_free_alloc_context(*ac);
543 *ac = NULL;
544 }
545
Mark Fashehccd979b2005-12-15 14:31:24 -0800546 mlog_exit(status);
547 return status;
548}
549
550/* local alloc code has to do the same thing, so rather than do this
551 * twice.. */
552int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
553 struct ocfs2_alloc_context *ac)
554{
555 int status;
556
Mark Fashehccd979b2005-12-15 14:31:24 -0800557 ac->ac_which = OCFS2_AC_USE_MAIN;
558 ac->ac_group_search = ocfs2_cluster_group_search;
559
Mark Fashehda5cbf22006-10-06 18:34:35 -0700560 status = ocfs2_reserve_suballoc_bits(osb, ac,
561 GLOBAL_BITMAP_SYSTEM_INODE,
562 OCFS2_INVALID_SLOT);
563 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800564 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700565 goto bail;
566 }
567
Mark Fashehccd979b2005-12-15 14:31:24 -0800568bail:
569 return status;
570}
571
572/* Callers don't need to care which bitmap (local alloc or main) to
573 * use so we figure it out for them, but unfortunately this clutters
574 * things a bit. */
575int ocfs2_reserve_clusters(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800576 u32 bits_wanted,
577 struct ocfs2_alloc_context **ac)
578{
579 int status;
580
581 mlog_entry_void();
582
Robert P. J. Daycd861282006-12-13 00:34:52 -0800583 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800584 if (!(*ac)) {
585 status = -ENOMEM;
586 mlog_errno(status);
587 goto bail;
588 }
589
590 (*ac)->ac_bits_wanted = bits_wanted;
Mark Fashehccd979b2005-12-15 14:31:24 -0800591
592 status = -ENOSPC;
593 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
594 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800595 bits_wanted,
596 *ac);
597 if ((status < 0) && (status != -ENOSPC)) {
598 mlog_errno(status);
599 goto bail;
600 } else if (status == -ENOSPC) {
601 /* reserve_local_bits will return enospc with
602 * the local alloc inode still locked, so we
603 * can change this safely here. */
604 mlog(0, "Disabling local alloc\n");
605 /* We set to OCFS2_LA_DISABLED so that umount
606 * can clean up what's left of the local
607 * allocation */
608 osb->local_alloc_state = OCFS2_LA_DISABLED;
609 }
610 }
611
612 if (status == -ENOSPC) {
613 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
614 if (status < 0) {
615 if (status != -ENOSPC)
616 mlog_errno(status);
617 goto bail;
618 }
619 }
620
621 status = 0;
622bail:
623 if ((status < 0) && *ac) {
624 ocfs2_free_alloc_context(*ac);
625 *ac = NULL;
626 }
627
628 mlog_exit(status);
629 return status;
630}
631
632/*
633 * More or less lifted from ext3. I'll leave their description below:
634 *
635 * "For ext3 allocations, we must not reuse any blocks which are
636 * allocated in the bitmap buffer's "last committed data" copy. This
637 * prevents deletes from freeing up the page for reuse until we have
638 * committed the delete transaction.
639 *
640 * If we didn't do this, then deleting something and reallocating it as
641 * data would allow the old block to be overwritten before the
642 * transaction committed (because we force data to disk before commit).
643 * This would lead to corruption if we crashed between overwriting the
644 * data and committing the delete.
645 *
646 * @@@ We may want to make this allocation behaviour conditional on
647 * data-writes at some point, and disable it for metadata allocations or
648 * sync-data inodes."
649 *
650 * Note: OCFS2 already does this differently for metadata vs data
651 * allocations, as those bitmaps are seperate and undo access is never
652 * called on a metadata group descriptor.
653 */
654static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
655 int nr)
656{
657 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
658
659 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
660 return 0;
661 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
662 return 1;
663
664 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
665 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
666}
667
668static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
669 struct buffer_head *bg_bh,
670 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700671 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800672 u16 *bit_off,
673 u16 *bits_found)
674{
675 void *bitmap;
676 u16 best_offset, best_size;
677 int offset, start, found, status = 0;
678 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
679
680 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
681 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
682 return -EIO;
683 }
684
685 found = start = best_offset = best_size = 0;
686 bitmap = bg->bg_bitmap;
687
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700688 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
689 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800690 break;
691
692 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
693 /* We found a zero, but we can't use it as it
694 * hasn't been put to disk yet! */
695 found = 0;
696 start = offset + 1;
697 } else if (offset == start) {
698 /* we found a zero */
699 found++;
700 /* move start to the next bit to test */
701 start++;
702 } else {
703 /* got a zero after some ones */
704 found = 1;
705 start = offset + 1;
706 }
707 if (found > best_size) {
708 best_size = found;
709 best_offset = start - found;
710 }
711 /* we got everything we needed */
712 if (found == bits_wanted) {
713 /* mlog(0, "Found it all!\n"); */
714 break;
715 }
716 }
717
718 /* XXX: I think the first clause is equivalent to the second
719 * - jlbec */
720 if (found == bits_wanted) {
721 *bit_off = start - found;
722 *bits_found = found;
723 } else if (best_size) {
724 *bit_off = best_offset;
725 *bits_found = best_size;
726 } else {
727 status = -ENOSPC;
728 /* No error log here -- see the comment above
729 * ocfs2_test_bg_bit_allocatable */
730 }
731
732 return status;
733}
734
Mark Fasheh1fabe142006-10-09 18:11:45 -0700735static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 struct inode *alloc_inode,
737 struct ocfs2_group_desc *bg,
738 struct buffer_head *group_bh,
739 unsigned int bit_off,
740 unsigned int num_bits)
741{
742 int status;
743 void *bitmap = bg->bg_bitmap;
744 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
745
746 mlog_entry_void();
747
748 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
749 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
750 status = -EIO;
751 goto bail;
752 }
753 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
754
755 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
756 num_bits);
757
758 if (ocfs2_is_cluster_bitmap(alloc_inode))
759 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
760
761 status = ocfs2_journal_access(handle,
762 alloc_inode,
763 group_bh,
764 journal_type);
765 if (status < 0) {
766 mlog_errno(status);
767 goto bail;
768 }
769
770 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
771
772 while(num_bits--)
773 ocfs2_set_bit(bit_off++, bitmap);
774
775 status = ocfs2_journal_dirty(handle,
776 group_bh);
777 if (status < 0) {
778 mlog_errno(status);
779 goto bail;
780 }
781
782bail:
783 mlog_exit(status);
784 return status;
785}
786
787/* find the one with the most empty bits */
788static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
789{
790 u16 curr, best;
791
792 BUG_ON(!cl->cl_next_free_rec);
793
794 best = curr = 0;
795 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
796 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
797 le32_to_cpu(cl->cl_recs[best].c_free))
798 best = curr;
799 curr++;
800 }
801
802 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
803 return best;
804}
805
Mark Fasheh1fabe142006-10-09 18:11:45 -0700806static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800807 struct inode *alloc_inode,
808 struct buffer_head *fe_bh,
809 struct buffer_head *bg_bh,
810 struct buffer_head *prev_bg_bh,
811 u16 chain)
812{
813 int status;
814 /* there is a really tiny chance the journal calls could fail,
815 * but we wouldn't want inconsistent blocks in *any* case. */
816 u64 fe_ptr, bg_ptr, prev_bg_ptr;
817 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
818 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
819 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
820
821 if (!OCFS2_IS_VALID_DINODE(fe)) {
822 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
823 status = -EIO;
824 goto out;
825 }
826 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
827 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
828 status = -EIO;
829 goto out;
830 }
831 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
832 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
833 status = -EIO;
834 goto out;
835 }
836
Mark Fashehb06970532006-03-03 10:24:33 -0800837 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700838 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
839 (unsigned long long)le64_to_cpu(bg->bg_blkno),
840 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800841
842 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
843 bg_ptr = le64_to_cpu(bg->bg_next_group);
844 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
845
846 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
847 OCFS2_JOURNAL_ACCESS_WRITE);
848 if (status < 0) {
849 mlog_errno(status);
850 goto out_rollback;
851 }
852
853 prev_bg->bg_next_group = bg->bg_next_group;
854
855 status = ocfs2_journal_dirty(handle, prev_bg_bh);
856 if (status < 0) {
857 mlog_errno(status);
858 goto out_rollback;
859 }
860
861 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
862 OCFS2_JOURNAL_ACCESS_WRITE);
863 if (status < 0) {
864 mlog_errno(status);
865 goto out_rollback;
866 }
867
868 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
869
870 status = ocfs2_journal_dirty(handle, bg_bh);
871 if (status < 0) {
872 mlog_errno(status);
873 goto out_rollback;
874 }
875
876 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
877 OCFS2_JOURNAL_ACCESS_WRITE);
878 if (status < 0) {
879 mlog_errno(status);
880 goto out_rollback;
881 }
882
883 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
884
885 status = ocfs2_journal_dirty(handle, fe_bh);
886 if (status < 0) {
887 mlog_errno(status);
888 goto out_rollback;
889 }
890
891 status = 0;
892out_rollback:
893 if (status < 0) {
894 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
895 bg->bg_next_group = cpu_to_le64(bg_ptr);
896 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
897 }
898out:
899 mlog_exit(status);
900 return status;
901}
902
903static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
904 u32 wanted)
905{
906 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
907}
908
909/* return 0 on success, -ENOSPC to keep searching and any other < 0
910 * value on error. */
911static int ocfs2_cluster_group_search(struct inode *inode,
912 struct buffer_head *group_bh,
913 u32 bits_wanted, u32 min_bits,
914 u16 *bit_off, u16 *bits_found)
915{
916 int search = -ENOSPC;
917 int ret;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700918 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700920 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -0800921
922 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
923
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700924 if (gd->bg_free_bits_count) {
925 max_bits = le16_to_cpu(gd->bg_bits);
926
927 /* Tail groups in cluster bitmaps which aren't cpg
928 * aligned are prone to partial extention by a failed
929 * fs resize. If the file system resize never got to
930 * update the dinode cluster count, then we don't want
931 * to trust any clusters past it, regardless of what
932 * the group descriptor says. */
933 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
934 le64_to_cpu(gd->bg_blkno));
935 if ((gd_cluster_off + max_bits) >
936 OCFS2_I(inode)->ip_clusters) {
937 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
938 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
939 (unsigned long long)le64_to_cpu(gd->bg_blkno),
940 le16_to_cpu(gd->bg_bits),
941 OCFS2_I(inode)->ip_clusters, max_bits);
942 }
943
Mark Fashehccd979b2005-12-15 14:31:24 -0800944 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
945 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700946 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800947 &tmp_off, &tmp_found);
948 if (ret)
949 return ret;
950
951 /* ocfs2_block_group_find_clear_bits() might
952 * return success, but we still want to return
953 * -ENOSPC unless it found the minimum number
954 * of bits. */
955 if (min_bits <= tmp_found) {
956 *bit_off = tmp_off;
957 *bits_found = tmp_found;
958 search = 0; /* success */
959 }
960 }
961
962 return search;
963}
964
965static int ocfs2_block_group_search(struct inode *inode,
966 struct buffer_head *group_bh,
967 u32 bits_wanted, u32 min_bits,
968 u16 *bit_off, u16 *bits_found)
969{
970 int ret = -ENOSPC;
971 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
972
973 BUG_ON(min_bits != 1);
974 BUG_ON(ocfs2_is_cluster_bitmap(inode));
975
976 if (bg->bg_free_bits_count)
977 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
978 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700979 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -0800980 bit_off, bits_found);
981
982 return ret;
983}
984
Mark Fasheh883d4ca2006-06-05 16:41:00 -0400985static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700986 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -0400987 struct buffer_head *di_bh,
988 u32 num_bits,
989 u16 chain)
990{
991 int ret;
992 u32 tmp_used;
993 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
994 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
995
996 ret = ocfs2_journal_access(handle, inode, di_bh,
997 OCFS2_JOURNAL_ACCESS_WRITE);
998 if (ret < 0) {
999 mlog_errno(ret);
1000 goto out;
1001 }
1002
1003 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1004 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1005 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1006
1007 ret = ocfs2_journal_dirty(handle, di_bh);
1008 if (ret < 0)
1009 mlog_errno(ret);
1010
1011out:
1012 return ret;
1013}
1014
1015static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001016 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001017 u32 bits_wanted,
1018 u32 min_bits,
1019 u16 *bit_off,
1020 unsigned int *num_bits,
1021 u64 gd_blkno,
1022 u16 *bits_left)
1023{
1024 int ret;
1025 u16 found;
1026 struct buffer_head *group_bh = NULL;
1027 struct ocfs2_group_desc *gd;
1028 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001029
1030 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1031 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1032 if (ret < 0) {
1033 mlog_errno(ret);
1034 return ret;
1035 }
1036
1037 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1038 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1039 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1040 ret = -EIO;
1041 goto out;
1042 }
1043
1044 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1045 bit_off, &found);
1046 if (ret < 0) {
1047 if (ret != -ENOSPC)
1048 mlog_errno(ret);
1049 goto out;
1050 }
1051
1052 *num_bits = found;
1053
1054 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1055 *num_bits,
1056 le16_to_cpu(gd->bg_chain));
1057 if (ret < 0) {
1058 mlog_errno(ret);
1059 goto out;
1060 }
1061
1062 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1063 *bit_off, *num_bits);
1064 if (ret < 0)
1065 mlog_errno(ret);
1066
1067 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1068
1069out:
1070 brelse(group_bh);
1071
1072 return ret;
1073}
1074
Mark Fashehccd979b2005-12-15 14:31:24 -08001075static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001076 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001077 u32 bits_wanted,
1078 u32 min_bits,
1079 u16 *bit_off,
1080 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001081 u64 *bg_blkno,
1082 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001083{
1084 int status;
1085 u16 chain, tmp_bits;
1086 u32 tmp_used;
1087 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 struct inode *alloc_inode = ac->ac_inode;
1089 struct buffer_head *group_bh = NULL;
1090 struct buffer_head *prev_group_bh = NULL;
1091 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1092 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1093 struct ocfs2_group_desc *bg;
1094
1095 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001096 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1097 bits_wanted, chain,
1098 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099
1100 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1101 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1102 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1103 if (status < 0) {
1104 mlog_errno(status);
1105 goto bail;
1106 }
1107 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001108 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1109 if (status) {
1110 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 goto bail;
1112 }
1113
1114 status = -ENOSPC;
1115 /* for now, the chain search is a bit simplistic. We just use
1116 * the 1st group with any empty bits. */
1117 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1118 bits_wanted, min_bits, bit_off,
1119 &tmp_bits)) == -ENOSPC) {
1120 if (!bg->bg_next_group)
1121 break;
1122
1123 if (prev_group_bh) {
1124 brelse(prev_group_bh);
1125 prev_group_bh = NULL;
1126 }
1127 next_group = le64_to_cpu(bg->bg_next_group);
1128 prev_group_bh = group_bh;
1129 group_bh = NULL;
1130 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1131 next_group, &group_bh,
1132 OCFS2_BH_CACHED, alloc_inode);
1133 if (status < 0) {
1134 mlog_errno(status);
1135 goto bail;
1136 }
1137 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001138 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1139 if (status) {
1140 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001141 goto bail;
1142 }
1143 }
1144 if (status < 0) {
1145 if (status != -ENOSPC)
1146 mlog_errno(status);
1147 goto bail;
1148 }
1149
Mark Fashehb06970532006-03-03 10:24:33 -08001150 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001151 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001152
1153 *num_bits = tmp_bits;
1154
1155 BUG_ON(*num_bits == 0);
1156
1157 /*
1158 * Keep track of previous block descriptor read. When
1159 * we find a target, if we have read more than X
1160 * number of descriptors, and the target is reasonably
1161 * empty, relink him to top of his chain.
1162 *
1163 * We've read 0 extra blocks and only send one more to
1164 * the transaction, yet the next guy to search has a
1165 * much easier time.
1166 *
1167 * Do this *after* figuring out how many bits we're taking out
1168 * of our target group.
1169 */
1170 if (ac->ac_allow_chain_relink &&
1171 (prev_group_bh) &&
1172 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1173 status = ocfs2_relink_block_group(handle, alloc_inode,
1174 ac->ac_bh, group_bh,
1175 prev_group_bh, chain);
1176 if (status < 0) {
1177 mlog_errno(status);
1178 goto bail;
1179 }
1180 }
1181
1182 /* Ok, claim our bits now: set the info on dinode, chainlist
1183 * and then the group */
1184 status = ocfs2_journal_access(handle,
1185 alloc_inode,
1186 ac->ac_bh,
1187 OCFS2_JOURNAL_ACCESS_WRITE);
1188 if (status < 0) {
1189 mlog_errno(status);
1190 goto bail;
1191 }
1192
1193 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1194 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1195 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1196
1197 status = ocfs2_journal_dirty(handle,
1198 ac->ac_bh);
1199 if (status < 0) {
1200 mlog_errno(status);
1201 goto bail;
1202 }
1203
1204 status = ocfs2_block_group_set_bits(handle,
1205 alloc_inode,
1206 bg,
1207 group_bh,
1208 *bit_off,
1209 *num_bits);
1210 if (status < 0) {
1211 mlog_errno(status);
1212 goto bail;
1213 }
1214
Mark Fashehb06970532006-03-03 10:24:33 -08001215 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001216 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001217
1218 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001219 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001220bail:
1221 if (group_bh)
1222 brelse(group_bh);
1223 if (prev_group_bh)
1224 brelse(prev_group_bh);
1225
1226 mlog_exit(status);
1227 return status;
1228}
1229
1230/* will give out up to bits_wanted contiguous bits. */
1231static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1232 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001233 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001234 u32 bits_wanted,
1235 u32 min_bits,
1236 u16 *bit_off,
1237 unsigned int *num_bits,
1238 u64 *bg_blkno)
1239{
1240 int status;
1241 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001242 u16 bits_left = 0;
1243 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 struct ocfs2_chain_list *cl;
1245 struct ocfs2_dinode *fe;
1246
1247 mlog_entry_void();
1248
1249 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1250 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1251 BUG_ON(!ac->ac_bh);
1252
1253 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1254 if (!OCFS2_IS_VALID_DINODE(fe)) {
1255 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1256 status = -EIO;
1257 goto bail;
1258 }
1259 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1260 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001261 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1262 "bits but only %u total.",
1263 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001264 le32_to_cpu(fe->id1.bitmap1.i_used),
1265 le32_to_cpu(fe->id1.bitmap1.i_total));
1266 status = -EIO;
1267 goto bail;
1268 }
1269
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001270 if (hint_blkno) {
1271 /* Attempt to short-circuit the usual search mechanism
1272 * by jumping straight to the most recently used
1273 * allocation group. This helps us mantain some
1274 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001275 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1276 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001277 hint_blkno, &bits_left);
1278 if (!status) {
1279 /* Be careful to update *bg_blkno here as the
1280 * caller is expecting it to be filled in, and
1281 * ocfs2_search_one_group() won't do that for
1282 * us. */
1283 *bg_blkno = hint_blkno;
1284 goto set_hint;
1285 }
1286 if (status < 0 && status != -ENOSPC) {
1287 mlog_errno(status);
1288 goto bail;
1289 }
1290 }
1291
Mark Fashehccd979b2005-12-15 14:31:24 -08001292 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1293
1294 victim = ocfs2_find_victim_chain(cl);
1295 ac->ac_chain = victim;
1296 ac->ac_allow_chain_relink = 1;
1297
Mark Fashehda5cbf22006-10-06 18:34:35 -07001298 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001299 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001301 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 if (status < 0 && status != -ENOSPC) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306
1307 mlog(0, "Search of victim chain %u came up with nothing, "
1308 "trying all chains now.\n", victim);
1309
1310 /* If we didn't pick a good victim, then just default to
1311 * searching each chain in order. Don't allow chain relinking
1312 * because we only calculate enough journal credits for one
1313 * relink per alloc. */
1314 ac->ac_allow_chain_relink = 0;
1315 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1316 if (i == victim)
1317 continue;
1318 if (!cl->cl_recs[i].c_free)
1319 continue;
1320
1321 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001322 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001323 bit_off, num_bits, bg_blkno,
1324 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 if (!status)
1326 break;
1327 if (status < 0 && status != -ENOSPC) {
1328 mlog_errno(status);
1329 goto bail;
1330 }
1331 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001332
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001333set_hint:
1334 if (status != -ENOSPC) {
1335 /* If the next search of this group is not likely to
1336 * yield a suitable extent, then we reset the last
1337 * group hint so as to not waste a disk read */
1338 if (bits_left < min_bits)
1339 ac->ac_last_group = 0;
1340 else
1341 ac->ac_last_group = *bg_blkno;
1342 }
1343
1344bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 mlog_exit(status);
1346 return status;
1347}
1348
1349int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001350 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 struct ocfs2_alloc_context *ac,
1352 u32 bits_wanted,
1353 u16 *suballoc_bit_start,
1354 unsigned int *num_bits,
1355 u64 *blkno_start)
1356{
1357 int status;
1358 u64 bg_blkno;
1359
1360 BUG_ON(!ac);
1361 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1362 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001363
1364 status = ocfs2_claim_suballoc_bits(osb,
1365 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001366 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001367 bits_wanted,
1368 1,
1369 suballoc_bit_start,
1370 num_bits,
1371 &bg_blkno);
1372 if (status < 0) {
1373 mlog_errno(status);
1374 goto bail;
1375 }
1376 atomic_inc(&osb->alloc_stats.bg_allocs);
1377
1378 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1379 ac->ac_bits_given += (*num_bits);
1380 status = 0;
1381bail:
1382 mlog_exit(status);
1383 return status;
1384}
1385
1386int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001387 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001388 struct ocfs2_alloc_context *ac,
1389 u16 *suballoc_bit,
1390 u64 *fe_blkno)
1391{
1392 int status;
1393 unsigned int num_bits;
1394 u64 bg_blkno;
1395
1396 mlog_entry_void();
1397
1398 BUG_ON(!ac);
1399 BUG_ON(ac->ac_bits_given != 0);
1400 BUG_ON(ac->ac_bits_wanted != 1);
1401 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001402
1403 status = ocfs2_claim_suballoc_bits(osb,
1404 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001405 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 1,
1407 1,
1408 suballoc_bit,
1409 &num_bits,
1410 &bg_blkno);
1411 if (status < 0) {
1412 mlog_errno(status);
1413 goto bail;
1414 }
1415 atomic_inc(&osb->alloc_stats.bg_allocs);
1416
1417 BUG_ON(num_bits != 1);
1418
1419 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1420 ac->ac_bits_given++;
1421 status = 0;
1422bail:
1423 mlog_exit(status);
1424 return status;
1425}
1426
1427/* translate a group desc. blkno and it's bitmap offset into
1428 * disk cluster offset. */
1429static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1430 u64 bg_blkno,
1431 u16 bg_bit_off)
1432{
1433 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1434 u32 cluster = 0;
1435
1436 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1437
1438 if (bg_blkno != osb->first_cluster_group_blkno)
1439 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1440 cluster += (u32) bg_bit_off;
1441 return cluster;
1442}
1443
1444/* given a cluster offset, calculate which block group it belongs to
1445 * and return that block offset. */
1446static inline u64 ocfs2_which_cluster_group(struct inode *inode,
1447 u32 cluster)
1448{
1449 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1450 u32 group_no;
1451
1452 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1453
1454 group_no = cluster / osb->bitmap_cpg;
1455 if (!group_no)
1456 return osb->first_cluster_group_blkno;
1457 return ocfs2_clusters_to_blocks(inode->i_sb,
1458 group_no * osb->bitmap_cpg);
1459}
1460
1461/* given the block number of a cluster start, calculate which cluster
1462 * group and descriptor bitmap offset that corresponds to. */
1463static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1464 u64 data_blkno,
1465 u64 *bg_blkno,
1466 u16 *bg_bit_off)
1467{
1468 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1469 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1470
1471 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1472
1473 *bg_blkno = ocfs2_which_cluster_group(inode,
1474 data_cluster);
1475
1476 if (*bg_blkno == osb->first_cluster_group_blkno)
1477 *bg_bit_off = (u16) data_cluster;
1478 else
1479 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1480 data_blkno - *bg_blkno);
1481}
1482
1483/*
1484 * min_bits - minimum contiguous chunk from this total allocation we
1485 * can handle. set to what we asked for originally for a full
1486 * contig. allocation, set to '1' to indicate we can deal with extents
1487 * of any size.
1488 */
1489int ocfs2_claim_clusters(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001490 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001491 struct ocfs2_alloc_context *ac,
1492 u32 min_clusters,
1493 u32 *cluster_start,
1494 u32 *num_clusters)
1495{
1496 int status;
1497 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001498 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 u16 bg_bit_off;
1500
1501 mlog_entry_void();
1502
1503 BUG_ON(!ac);
1504 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1505
1506 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1507 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001508
1509 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1510 status = ocfs2_claim_local_alloc_bits(osb,
1511 handle,
1512 ac,
1513 bits_wanted,
1514 cluster_start,
1515 num_clusters);
1516 if (!status)
1517 atomic_inc(&osb->alloc_stats.local_data);
1518 } else {
1519 if (min_clusters > (osb->bitmap_cpg - 1)) {
1520 /* The only paths asking for contiguousness
1521 * should know about this already. */
1522 mlog(ML_ERROR, "minimum allocation requested exceeds "
1523 "group bitmap size!");
1524 status = -ENOSPC;
1525 goto bail;
1526 }
1527 /* clamp the current request down to a realistic size. */
1528 if (bits_wanted > (osb->bitmap_cpg - 1))
1529 bits_wanted = osb->bitmap_cpg - 1;
1530
1531 status = ocfs2_claim_suballoc_bits(osb,
1532 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001533 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 bits_wanted,
1535 min_clusters,
1536 &bg_bit_off,
1537 num_clusters,
1538 &bg_blkno);
1539 if (!status) {
1540 *cluster_start =
1541 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1542 bg_blkno,
1543 bg_bit_off);
1544 atomic_inc(&osb->alloc_stats.bitmap_data);
1545 }
1546 }
1547 if (status < 0) {
1548 if (status != -ENOSPC)
1549 mlog_errno(status);
1550 goto bail;
1551 }
1552
1553 ac->ac_bits_given += *num_clusters;
1554
1555bail:
1556 mlog_exit(status);
1557 return status;
1558}
1559
Mark Fasheh1fabe142006-10-09 18:11:45 -07001560static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001561 struct inode *alloc_inode,
1562 struct ocfs2_group_desc *bg,
1563 struct buffer_head *group_bh,
1564 unsigned int bit_off,
1565 unsigned int num_bits)
1566{
1567 int status;
1568 unsigned int tmp;
1569 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1570 struct ocfs2_group_desc *undo_bg = NULL;
1571
1572 mlog_entry_void();
1573
1574 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1575 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1576 status = -EIO;
1577 goto bail;
1578 }
1579
1580 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1581
1582 if (ocfs2_is_cluster_bitmap(alloc_inode))
1583 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1584
1585 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1586 journal_type);
1587 if (status < 0) {
1588 mlog_errno(status);
1589 goto bail;
1590 }
1591
1592 if (ocfs2_is_cluster_bitmap(alloc_inode))
1593 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1594
1595 tmp = num_bits;
1596 while(tmp--) {
1597 ocfs2_clear_bit((bit_off + tmp),
1598 (unsigned long *) bg->bg_bitmap);
1599 if (ocfs2_is_cluster_bitmap(alloc_inode))
1600 ocfs2_set_bit(bit_off + tmp,
1601 (unsigned long *) undo_bg->bg_bitmap);
1602 }
1603 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1604
1605 status = ocfs2_journal_dirty(handle, group_bh);
1606 if (status < 0)
1607 mlog_errno(status);
1608bail:
1609 return status;
1610}
1611
1612/*
1613 * expects the suballoc inode to already be locked.
1614 */
Mark Fasheh2b604352007-06-22 15:45:27 -07001615int ocfs2_free_suballoc_bits(handle_t *handle,
1616 struct inode *alloc_inode,
1617 struct buffer_head *alloc_bh,
1618 unsigned int start_bit,
1619 u64 bg_blkno,
1620 unsigned int count)
Mark Fashehccd979b2005-12-15 14:31:24 -08001621{
1622 int status = 0;
1623 u32 tmp_used;
1624 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1625 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1626 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1627 struct buffer_head *group_bh = NULL;
1628 struct ocfs2_group_desc *group;
1629
1630 mlog_entry_void();
1631
1632 if (!OCFS2_IS_VALID_DINODE(fe)) {
1633 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1634 status = -EIO;
1635 goto bail;
1636 }
1637 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1638
Mark Fashehb06970532006-03-03 10:24:33 -08001639 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1640 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1641 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001642
1643 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1644 alloc_inode);
1645 if (status < 0) {
1646 mlog_errno(status);
1647 goto bail;
1648 }
1649
1650 group = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001651 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1652 if (status) {
1653 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001654 goto bail;
1655 }
1656 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1657
1658 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1659 group, group_bh,
1660 start_bit, count);
1661 if (status < 0) {
1662 mlog_errno(status);
1663 goto bail;
1664 }
1665
1666 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1667 OCFS2_JOURNAL_ACCESS_WRITE);
1668 if (status < 0) {
1669 mlog_errno(status);
1670 goto bail;
1671 }
1672
1673 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1674 count);
1675 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1676 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1677
1678 status = ocfs2_journal_dirty(handle, alloc_bh);
1679 if (status < 0) {
1680 mlog_errno(status);
1681 goto bail;
1682 }
1683
1684bail:
1685 if (group_bh)
1686 brelse(group_bh);
1687
1688 mlog_exit(status);
1689 return status;
1690}
1691
Mark Fasheh1fabe142006-10-09 18:11:45 -07001692int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001693 struct inode *inode_alloc_inode,
1694 struct buffer_head *inode_alloc_bh,
1695 struct ocfs2_dinode *di)
1696{
1697 u64 blk = le64_to_cpu(di->i_blkno);
1698 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1699 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1700
1701 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1702 inode_alloc_bh, bit, bg_blkno, 1);
1703}
1704
Mark Fasheh1fabe142006-10-09 18:11:45 -07001705int ocfs2_free_clusters(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001706 struct inode *bitmap_inode,
1707 struct buffer_head *bitmap_bh,
1708 u64 start_blk,
1709 unsigned int num_clusters)
1710{
1711 int status;
1712 u16 bg_start_bit;
1713 u64 bg_blkno;
1714 struct ocfs2_dinode *fe;
1715
1716 /* You can't ever have a contiguous set of clusters
1717 * bigger than a block group bitmap so we never have to worry
1718 * about looping on them. */
1719
1720 mlog_entry_void();
1721
1722 /* This is expensive. We can safely remove once this stuff has
1723 * gotten tested really well. */
1724 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1725
1726 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1727
1728 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1729 &bg_start_bit);
1730
Mark Fashehb06970532006-03-03 10:24:33 -08001731 mlog(0, "want to free %u clusters starting at block %llu\n",
1732 num_clusters, (unsigned long long)start_blk);
1733 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1734 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001735
1736 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1737 bg_start_bit, bg_blkno,
1738 num_clusters);
1739 if (status < 0)
1740 mlog_errno(status);
1741
1742 mlog_exit(status);
1743 return status;
1744}
1745
1746static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1747{
1748 printk("Block Group:\n");
1749 printk("bg_signature: %s\n", bg->bg_signature);
1750 printk("bg_size: %u\n", bg->bg_size);
1751 printk("bg_bits: %u\n", bg->bg_bits);
1752 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1753 printk("bg_chain: %u\n", bg->bg_chain);
1754 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08001755 printk("bg_next_group: %llu\n",
1756 (unsigned long long)bg->bg_next_group);
1757 printk("bg_parent_dinode: %llu\n",
1758 (unsigned long long)bg->bg_parent_dinode);
1759 printk("bg_blkno: %llu\n",
1760 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001761}
1762
1763static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1764{
1765 int i;
1766
Mark Fashehb06970532006-03-03 10:24:33 -08001767 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001768 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08001769 printk("i_size: %llu\n",
1770 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001771 printk("i_clusters: %u\n", fe->i_clusters);
1772 printk("i_generation: %u\n",
1773 le32_to_cpu(fe->i_generation));
1774 printk("id1.bitmap1.i_used: %u\n",
1775 le32_to_cpu(fe->id1.bitmap1.i_used));
1776 printk("id1.bitmap1.i_total: %u\n",
1777 le32_to_cpu(fe->id1.bitmap1.i_total));
1778 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1779 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1780 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1781 printk("id2.i_chain.cl_next_free_rec: %u\n",
1782 fe->id2.i_chain.cl_next_free_rec);
1783 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1784 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1785 fe->id2.i_chain.cl_recs[i].c_free);
1786 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1787 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08001788 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1789 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 }
1791}