blob: d2d278fb9819ee3da92577a367b79f0bdc18681f [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
Tao Maffda89a2008-03-03 17:12:09 +080049#define NOT_ALLOC_NEW_GROUP 0
50#define ALLOC_NEW_GROUP 1
51
Tao Ma4d0ddb22008-03-05 16:11:46 +080052#define OCFS2_MAX_INODES_TO_STEAL 1024
53
Mark Fashehccd979b2005-12-15 14:31:24 -080054static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
55static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
56static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070057static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080058 struct inode *alloc_inode,
59 struct buffer_head *bg_bh,
60 u64 group_blkno,
61 u16 my_chain,
62 struct ocfs2_chain_list *cl);
63static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
64 struct inode *alloc_inode,
65 struct buffer_head *bh);
66
Mark Fashehccd979b2005-12-15 14:31:24 -080067static int ocfs2_cluster_group_search(struct inode *inode,
68 struct buffer_head *group_bh,
69 u32 bits_wanted, u32 min_bits,
70 u16 *bit_off, u16 *bits_found);
71static int ocfs2_block_group_search(struct inode *inode,
72 struct buffer_head *group_bh,
73 u32 bits_wanted, u32 min_bits,
74 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080075static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
76 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070077 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080078 u32 bits_wanted,
79 u32 min_bits,
80 u16 *bit_off,
81 unsigned int *num_bits,
82 u64 *bg_blkno);
83static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
84 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070085static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080086 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);
Mark Fasheh1fabe142006-10-09 18:11:45 -070091static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080092 struct inode *alloc_inode,
93 struct ocfs2_group_desc *bg,
94 struct buffer_head *group_bh,
95 unsigned int bit_off,
96 unsigned int num_bits);
97
Mark Fasheh1fabe142006-10-09 18:11:45 -070098static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080099 struct inode *alloc_inode,
100 struct buffer_head *fe_bh,
101 struct buffer_head *bg_bh,
102 struct buffer_head *prev_bg_bh,
103 u16 chain);
104static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
105 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800106static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
107 u64 bg_blkno,
108 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109static inline void ocfs2_block_to_cluster_group(struct inode *inode,
110 u64 data_blkno,
111 u64 *bg_blkno,
112 u16 *bg_bit_off);
113
Tao Ma4d0ddb22008-03-05 16:11:46 +0800114static void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800115{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700116 struct inode *inode = ac->ac_inode;
117
118 if (inode) {
119 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700120 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700121
122 mutex_unlock(&inode->i_mutex);
123
124 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800125 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700126 }
Tao Ma4d0ddb22008-03-05 16:11:46 +0800127 if (ac->ac_bh) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800128 brelse(ac->ac_bh);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800129 ac->ac_bh = NULL;
130 }
131}
132
133void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
134{
135 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800136 kfree(ac);
137}
138
139static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
140{
141 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
142}
143
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700144/* somewhat more expensive than our other checks, so use sparingly. */
Tao Mad6590722007-12-18 15:47:03 +0800145int ocfs2_check_group_descriptor(struct super_block *sb,
146 struct ocfs2_dinode *di,
147 struct ocfs2_group_desc *gd)
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700148{
149 unsigned int max_bits;
150
151 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
152 OCFS2_RO_ON_INVALID_GROUP_DESC(sb, gd);
153 return -EIO;
154 }
155
156 if (di->i_blkno != gd->bg_parent_dinode) {
157 ocfs2_error(sb, "Group descriptor # %llu has bad parent "
158 "pointer (%llu, expected %llu)",
159 (unsigned long long)le64_to_cpu(gd->bg_blkno),
160 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
161 (unsigned long long)le64_to_cpu(di->i_blkno));
162 return -EIO;
163 }
164
165 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
166 if (le16_to_cpu(gd->bg_bits) > max_bits) {
167 ocfs2_error(sb, "Group descriptor # %llu has bit count of %u",
168 (unsigned long long)le64_to_cpu(gd->bg_blkno),
169 le16_to_cpu(gd->bg_bits));
170 return -EIO;
171 }
172
173 if (le16_to_cpu(gd->bg_chain) >=
174 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
175 ocfs2_error(sb, "Group descriptor # %llu has bad chain %u",
176 (unsigned long long)le64_to_cpu(gd->bg_blkno),
177 le16_to_cpu(gd->bg_chain));
178 return -EIO;
179 }
180
181 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
182 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
183 "claims that %u are free",
184 (unsigned long long)le64_to_cpu(gd->bg_blkno),
185 le16_to_cpu(gd->bg_bits),
186 le16_to_cpu(gd->bg_free_bits_count));
187 return -EIO;
188 }
189
190 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
191 ocfs2_error(sb, "Group descriptor # %llu has bit count %u but "
192 "max bitmap bits of %u",
193 (unsigned long long)le64_to_cpu(gd->bg_blkno),
194 le16_to_cpu(gd->bg_bits),
195 8 * le16_to_cpu(gd->bg_size));
196 return -EIO;
197 }
198
199 return 0;
200}
201
Mark Fasheh1fabe142006-10-09 18:11:45 -0700202static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800203 struct inode *alloc_inode,
204 struct buffer_head *bg_bh,
205 u64 group_blkno,
206 u16 my_chain,
207 struct ocfs2_chain_list *cl)
208{
209 int status = 0;
210 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
211 struct super_block * sb = alloc_inode->i_sb;
212
213 mlog_entry_void();
214
215 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb0697052006-03-03 10:24:33 -0800216 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
217 "b_blocknr (%llu)",
218 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800219 (unsigned long long) bg_bh->b_blocknr);
220 status = -EIO;
221 goto bail;
222 }
223
224 status = ocfs2_journal_access(handle,
225 alloc_inode,
226 bg_bh,
227 OCFS2_JOURNAL_ACCESS_CREATE);
228 if (status < 0) {
229 mlog_errno(status);
230 goto bail;
231 }
232
233 memset(bg, 0, sb->s_blocksize);
234 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
235 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
236 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
237 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
238 bg->bg_chain = cpu_to_le16(my_chain);
239 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
240 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
241 bg->bg_blkno = cpu_to_le64(group_blkno);
242 /* set the 1st bit in the bitmap to account for the descriptor block */
243 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
244 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
245
246 status = ocfs2_journal_dirty(handle, bg_bh);
247 if (status < 0)
248 mlog_errno(status);
249
250 /* There is no need to zero out or otherwise initialize the
251 * other blocks in a group - All valid FS metadata in a block
252 * group stores the superblock fs_generation value at
253 * allocation time. */
254
255bail:
256 mlog_exit(status);
257 return status;
258}
259
260static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
261{
262 u16 curr, best;
263
264 best = curr = 0;
265 while (curr < le16_to_cpu(cl->cl_count)) {
266 if (le32_to_cpu(cl->cl_recs[best].c_total) >
267 le32_to_cpu(cl->cl_recs[curr].c_total))
268 best = curr;
269 curr++;
270 }
271 return best;
272}
273
274/*
275 * We expect the block group allocator to already be locked.
276 */
277static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
278 struct inode *alloc_inode,
279 struct buffer_head *bh)
280{
281 int status, credits;
282 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
283 struct ocfs2_chain_list *cl;
284 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700285 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800286 u32 bit_off, num_bits;
287 u16 alloc_rec;
288 u64 bg_blkno;
289 struct buffer_head *bg_bh = NULL;
290 struct ocfs2_group_desc *bg;
291
292 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
293
294 mlog_entry_void();
295
Mark Fashehccd979b2005-12-15 14:31:24 -0800296 cl = &fe->id2.i_chain;
297 status = ocfs2_reserve_clusters(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800298 le16_to_cpu(cl->cl_cpg),
299 &ac);
300 if (status < 0) {
301 if (status != -ENOSPC)
302 mlog_errno(status);
303 goto bail;
304 }
305
306 credits = ocfs2_calc_group_alloc_credits(osb->sb,
307 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700308 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800309 if (IS_ERR(handle)) {
310 status = PTR_ERR(handle);
311 handle = NULL;
312 mlog_errno(status);
313 goto bail;
314 }
315
316 status = ocfs2_claim_clusters(osb,
317 handle,
318 ac,
319 le16_to_cpu(cl->cl_cpg),
320 &bit_off,
321 &num_bits);
322 if (status < 0) {
323 if (status != -ENOSPC)
324 mlog_errno(status);
325 goto bail;
326 }
327
328 alloc_rec = ocfs2_find_smallest_chain(cl);
329
330 /* setup the group */
331 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb0697052006-03-03 10:24:33 -0800332 mlog(0, "new descriptor, record %u, at block %llu\n",
333 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800334
335 bg_bh = sb_getblk(osb->sb, bg_blkno);
336 if (!bg_bh) {
337 status = -EIO;
338 mlog_errno(status);
339 goto bail;
340 }
341 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
342
343 status = ocfs2_block_group_fill(handle,
344 alloc_inode,
345 bg_bh,
346 bg_blkno,
347 alloc_rec,
348 cl);
349 if (status < 0) {
350 mlog_errno(status);
351 goto bail;
352 }
353
354 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
355
356 status = ocfs2_journal_access(handle, alloc_inode,
357 bh, OCFS2_JOURNAL_ACCESS_WRITE);
358 if (status < 0) {
359 mlog_errno(status);
360 goto bail;
361 }
362
363 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
364 le16_to_cpu(bg->bg_free_bits_count));
365 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
366 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
367 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
368 le16_add_cpu(&cl->cl_next_free_rec, 1);
369
370 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
371 le16_to_cpu(bg->bg_free_bits_count));
372 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
373 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
374
375 status = ocfs2_journal_dirty(handle, bh);
376 if (status < 0) {
377 mlog_errno(status);
378 goto bail;
379 }
380
381 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
382 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
383 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
384 le32_to_cpu(fe->i_clusters)));
385 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
386 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700387 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800388
389 status = 0;
390bail:
391 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700392 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800393
394 if (ac)
395 ocfs2_free_alloc_context(ac);
396
397 if (bg_bh)
398 brelse(bg_bh);
399
400 mlog_exit(status);
401 return status;
402}
403
404static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700405 struct ocfs2_alloc_context *ac,
406 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800407 u32 slot,
408 int alloc_new_group)
Mark Fashehccd979b2005-12-15 14:31:24 -0800409{
410 int status;
411 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700412 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800414 struct ocfs2_dinode *fe;
415 u32 free_bits;
416
417 mlog_entry_void();
418
Mark Fashehda5cbf22006-10-06 18:34:35 -0700419 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
420 if (!alloc_inode) {
421 mlog_errno(-EINVAL);
422 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 }
424
Mark Fashehda5cbf22006-10-06 18:34:35 -0700425 mutex_lock(&alloc_inode->i_mutex);
426
Mark Fashehe63aecb62007-10-18 15:30:42 -0700427 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700428 if (status < 0) {
429 mutex_unlock(&alloc_inode->i_mutex);
430 iput(alloc_inode);
431
432 mlog_errno(status);
433 return status;
434 }
435
436 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800437 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700438
Mark Fashehccd979b2005-12-15 14:31:24 -0800439 fe = (struct ocfs2_dinode *) bh->b_data;
440 if (!OCFS2_IS_VALID_DINODE(fe)) {
441 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
442 status = -EIO;
443 goto bail;
444 }
445 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800446 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
447 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800448 status = -EIO;
449 goto bail;
450 }
451
452 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
453 le32_to_cpu(fe->id1.bitmap1.i_used);
454
455 if (bits_wanted > free_bits) {
456 /* cluster bitmap never grows */
457 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
458 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
459 bits_wanted, free_bits);
460 status = -ENOSPC;
461 goto bail;
462 }
463
Tao Maffda89a2008-03-03 17:12:09 +0800464 if (alloc_new_group != ALLOC_NEW_GROUP) {
465 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
466 "and we don't alloc a new group for it.\n",
467 slot, bits_wanted, free_bits);
468 status = -ENOSPC;
469 goto bail;
470 }
471
Mark Fashehccd979b2005-12-15 14:31:24 -0800472 status = ocfs2_block_group_alloc(osb, alloc_inode, bh);
473 if (status < 0) {
474 if (status != -ENOSPC)
475 mlog_errno(status);
476 goto bail;
477 }
478 atomic_inc(&osb->alloc_stats.bg_extends);
479
480 /* You should never ask for this much metadata */
481 BUG_ON(bits_wanted >
482 (le32_to_cpu(fe->id1.bitmap1.i_total)
483 - le32_to_cpu(fe->id1.bitmap1.i_used)));
484 }
485
486 get_bh(bh);
487 ac->ac_bh = bh;
488bail:
489 if (bh)
490 brelse(bh);
491
492 mlog_exit(status);
493 return status;
494}
495
496int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800497 struct ocfs2_dinode *fe,
498 struct ocfs2_alloc_context **ac)
499{
500 int status;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700501 u32 slot;
Mark Fashehccd979b2005-12-15 14:31:24 -0800502
Robert P. J. Daycd861282006-12-13 00:34:52 -0800503 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800504 if (!(*ac)) {
505 status = -ENOMEM;
506 mlog_errno(status);
507 goto bail;
508 }
509
510 (*ac)->ac_bits_wanted = ocfs2_extend_meta_needed(fe);
Mark Fashehccd979b2005-12-15 14:31:24 -0800511 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700512 slot = osb->slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -0800513 (*ac)->ac_group_search = ocfs2_block_group_search;
514
Mark Fashehda5cbf22006-10-06 18:34:35 -0700515 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800516 EXTENT_ALLOC_SYSTEM_INODE,
517 slot, ALLOC_NEW_GROUP);
Mark Fashehccd979b2005-12-15 14:31:24 -0800518 if (status < 0) {
519 if (status != -ENOSPC)
520 mlog_errno(status);
521 goto bail;
522 }
523
524 status = 0;
525bail:
526 if ((status < 0) && *ac) {
527 ocfs2_free_alloc_context(*ac);
528 *ac = NULL;
529 }
530
Mark Fashehccd979b2005-12-15 14:31:24 -0800531 mlog_exit(status);
532 return status;
533}
534
Tao Ma4d0ddb22008-03-05 16:11:46 +0800535static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
536 struct ocfs2_alloc_context *ac)
537{
538 int i, status = -ENOSPC;
539 s16 slot = ocfs2_get_inode_steal_slot(osb);
540
541 /* Start to steal inodes from the first slot after ours. */
542 if (slot == OCFS2_INVALID_SLOT)
543 slot = osb->slot_num + 1;
544
545 for (i = 0; i < osb->max_slots; i++, slot++) {
546 if (slot == osb->max_slots)
547 slot = 0;
548
549 if (slot == osb->slot_num)
550 continue;
551
552 status = ocfs2_reserve_suballoc_bits(osb, ac,
553 INODE_ALLOC_SYSTEM_INODE,
554 slot, NOT_ALLOC_NEW_GROUP);
555 if (status >= 0) {
556 ocfs2_set_inode_steal_slot(osb, slot);
557 break;
558 }
559
560 ocfs2_free_ac_resource(ac);
561 }
562
563 return status;
564}
565
Mark Fashehccd979b2005-12-15 14:31:24 -0800566int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800567 struct ocfs2_alloc_context **ac)
568{
569 int status;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800570 s16 slot = ocfs2_get_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800571
Robert P. J. Daycd861282006-12-13 00:34:52 -0800572 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800573 if (!(*ac)) {
574 status = -ENOMEM;
575 mlog_errno(status);
576 goto bail;
577 }
578
579 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800580 (*ac)->ac_which = OCFS2_AC_USE_INODE;
581
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 (*ac)->ac_group_search = ocfs2_block_group_search;
583
Tao Ma4d0ddb22008-03-05 16:11:46 +0800584 /*
585 * slot is set when we successfully steal inode from other nodes.
586 * It is reset in 3 places:
587 * 1. when we flush the truncate log
588 * 2. when we complete local alloc recovery.
589 * 3. when we successfully allocate from our own slot.
590 * After it is set, we will go on stealing inodes until we find the
591 * need to check our slots to see whether there is some space for us.
592 */
593 if (slot != OCFS2_INVALID_SLOT &&
594 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
595 goto inode_steal;
596
597 atomic_set(&osb->s_num_inodes_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700598 status = ocfs2_reserve_suballoc_bits(osb, *ac,
599 INODE_ALLOC_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800600 osb->slot_num, ALLOC_NEW_GROUP);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800601 if (status >= 0) {
602 status = 0;
603
604 /*
605 * Some inodes must be freed by us, so try to allocate
606 * from our own next time.
607 */
608 if (slot != OCFS2_INVALID_SLOT)
609 ocfs2_init_inode_steal_slot(osb);
610 goto bail;
611 } else if (status < 0 && status != -ENOSPC) {
612 mlog_errno(status);
613 goto bail;
614 }
615
616 ocfs2_free_ac_resource(*ac);
617
618inode_steal:
619 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
620 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800621 if (status < 0) {
622 if (status != -ENOSPC)
623 mlog_errno(status);
624 goto bail;
625 }
626
627 status = 0;
628bail:
629 if ((status < 0) && *ac) {
630 ocfs2_free_alloc_context(*ac);
631 *ac = NULL;
632 }
633
Mark Fashehccd979b2005-12-15 14:31:24 -0800634 mlog_exit(status);
635 return status;
636}
637
638/* local alloc code has to do the same thing, so rather than do this
639 * twice.. */
640int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
641 struct ocfs2_alloc_context *ac)
642{
643 int status;
644
Mark Fashehccd979b2005-12-15 14:31:24 -0800645 ac->ac_which = OCFS2_AC_USE_MAIN;
646 ac->ac_group_search = ocfs2_cluster_group_search;
647
Mark Fashehda5cbf22006-10-06 18:34:35 -0700648 status = ocfs2_reserve_suballoc_bits(osb, ac,
649 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800650 OCFS2_INVALID_SLOT,
651 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700652 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800653 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700654 goto bail;
655 }
656
Mark Fashehccd979b2005-12-15 14:31:24 -0800657bail:
658 return status;
659}
660
661/* Callers don't need to care which bitmap (local alloc or main) to
662 * use so we figure it out for them, but unfortunately this clutters
663 * things a bit. */
664int ocfs2_reserve_clusters(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 u32 bits_wanted,
666 struct ocfs2_alloc_context **ac)
667{
668 int status;
669
670 mlog_entry_void();
671
Robert P. J. Daycd861282006-12-13 00:34:52 -0800672 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800673 if (!(*ac)) {
674 status = -ENOMEM;
675 mlog_errno(status);
676 goto bail;
677 }
678
679 (*ac)->ac_bits_wanted = bits_wanted;
Mark Fashehccd979b2005-12-15 14:31:24 -0800680
681 status = -ENOSPC;
682 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
683 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800684 bits_wanted,
685 *ac);
686 if ((status < 0) && (status != -ENOSPC)) {
687 mlog_errno(status);
688 goto bail;
689 } else if (status == -ENOSPC) {
690 /* reserve_local_bits will return enospc with
691 * the local alloc inode still locked, so we
692 * can change this safely here. */
693 mlog(0, "Disabling local alloc\n");
694 /* We set to OCFS2_LA_DISABLED so that umount
695 * can clean up what's left of the local
696 * allocation */
697 osb->local_alloc_state = OCFS2_LA_DISABLED;
698 }
699 }
700
701 if (status == -ENOSPC) {
702 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
703 if (status < 0) {
704 if (status != -ENOSPC)
705 mlog_errno(status);
706 goto bail;
707 }
708 }
709
710 status = 0;
711bail:
712 if ((status < 0) && *ac) {
713 ocfs2_free_alloc_context(*ac);
714 *ac = NULL;
715 }
716
717 mlog_exit(status);
718 return status;
719}
720
721/*
722 * More or less lifted from ext3. I'll leave their description below:
723 *
724 * "For ext3 allocations, we must not reuse any blocks which are
725 * allocated in the bitmap buffer's "last committed data" copy. This
726 * prevents deletes from freeing up the page for reuse until we have
727 * committed the delete transaction.
728 *
729 * If we didn't do this, then deleting something and reallocating it as
730 * data would allow the old block to be overwritten before the
731 * transaction committed (because we force data to disk before commit).
732 * This would lead to corruption if we crashed between overwriting the
733 * data and committing the delete.
734 *
735 * @@@ We may want to make this allocation behaviour conditional on
736 * data-writes at some point, and disable it for metadata allocations or
737 * sync-data inodes."
738 *
739 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +0200740 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 * called on a metadata group descriptor.
742 */
743static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
744 int nr)
745{
746 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
747
748 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
749 return 0;
750 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
751 return 1;
752
753 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
754 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
755}
756
757static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
758 struct buffer_head *bg_bh,
759 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700760 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800761 u16 *bit_off,
762 u16 *bits_found)
763{
764 void *bitmap;
765 u16 best_offset, best_size;
766 int offset, start, found, status = 0;
767 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
768
769 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
770 OCFS2_RO_ON_INVALID_GROUP_DESC(osb->sb, bg);
771 return -EIO;
772 }
773
774 found = start = best_offset = best_size = 0;
775 bitmap = bg->bg_bitmap;
776
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700777 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
778 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800779 break;
780
781 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
782 /* We found a zero, but we can't use it as it
783 * hasn't been put to disk yet! */
784 found = 0;
785 start = offset + 1;
786 } else if (offset == start) {
787 /* we found a zero */
788 found++;
789 /* move start to the next bit to test */
790 start++;
791 } else {
792 /* got a zero after some ones */
793 found = 1;
794 start = offset + 1;
795 }
796 if (found > best_size) {
797 best_size = found;
798 best_offset = start - found;
799 }
800 /* we got everything we needed */
801 if (found == bits_wanted) {
802 /* mlog(0, "Found it all!\n"); */
803 break;
804 }
805 }
806
807 /* XXX: I think the first clause is equivalent to the second
808 * - jlbec */
809 if (found == bits_wanted) {
810 *bit_off = start - found;
811 *bits_found = found;
812 } else if (best_size) {
813 *bit_off = best_offset;
814 *bits_found = best_size;
815 } else {
816 status = -ENOSPC;
817 /* No error log here -- see the comment above
818 * ocfs2_test_bg_bit_allocatable */
819 }
820
821 return status;
822}
823
Mark Fasheh1fabe142006-10-09 18:11:45 -0700824static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800825 struct inode *alloc_inode,
826 struct ocfs2_group_desc *bg,
827 struct buffer_head *group_bh,
828 unsigned int bit_off,
829 unsigned int num_bits)
830{
831 int status;
832 void *bitmap = bg->bg_bitmap;
833 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
834
835 mlog_entry_void();
836
837 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
838 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
839 status = -EIO;
840 goto bail;
841 }
842 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
843
844 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
845 num_bits);
846
847 if (ocfs2_is_cluster_bitmap(alloc_inode))
848 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
849
850 status = ocfs2_journal_access(handle,
851 alloc_inode,
852 group_bh,
853 journal_type);
854 if (status < 0) {
855 mlog_errno(status);
856 goto bail;
857 }
858
859 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
860
861 while(num_bits--)
862 ocfs2_set_bit(bit_off++, bitmap);
863
864 status = ocfs2_journal_dirty(handle,
865 group_bh);
866 if (status < 0) {
867 mlog_errno(status);
868 goto bail;
869 }
870
871bail:
872 mlog_exit(status);
873 return status;
874}
875
876/* find the one with the most empty bits */
877static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
878{
879 u16 curr, best;
880
881 BUG_ON(!cl->cl_next_free_rec);
882
883 best = curr = 0;
884 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
885 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
886 le32_to_cpu(cl->cl_recs[best].c_free))
887 best = curr;
888 curr++;
889 }
890
891 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
892 return best;
893}
894
Mark Fasheh1fabe142006-10-09 18:11:45 -0700895static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800896 struct inode *alloc_inode,
897 struct buffer_head *fe_bh,
898 struct buffer_head *bg_bh,
899 struct buffer_head *prev_bg_bh,
900 u16 chain)
901{
902 int status;
903 /* there is a really tiny chance the journal calls could fail,
904 * but we wouldn't want inconsistent blocks in *any* case. */
905 u64 fe_ptr, bg_ptr, prev_bg_ptr;
906 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
907 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
908 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
909
910 if (!OCFS2_IS_VALID_DINODE(fe)) {
911 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
912 status = -EIO;
913 goto out;
914 }
915 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
916 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
917 status = -EIO;
918 goto out;
919 }
920 if (!OCFS2_IS_VALID_GROUP_DESC(prev_bg)) {
921 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, prev_bg);
922 status = -EIO;
923 goto out;
924 }
925
Mark Fashehb0697052006-03-03 10:24:33 -0800926 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -0700927 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
928 (unsigned long long)le64_to_cpu(bg->bg_blkno),
929 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800930
931 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
932 bg_ptr = le64_to_cpu(bg->bg_next_group);
933 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
934
935 status = ocfs2_journal_access(handle, alloc_inode, prev_bg_bh,
936 OCFS2_JOURNAL_ACCESS_WRITE);
937 if (status < 0) {
938 mlog_errno(status);
939 goto out_rollback;
940 }
941
942 prev_bg->bg_next_group = bg->bg_next_group;
943
944 status = ocfs2_journal_dirty(handle, prev_bg_bh);
945 if (status < 0) {
946 mlog_errno(status);
947 goto out_rollback;
948 }
949
950 status = ocfs2_journal_access(handle, alloc_inode, bg_bh,
951 OCFS2_JOURNAL_ACCESS_WRITE);
952 if (status < 0) {
953 mlog_errno(status);
954 goto out_rollback;
955 }
956
957 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
958
959 status = ocfs2_journal_dirty(handle, bg_bh);
960 if (status < 0) {
961 mlog_errno(status);
962 goto out_rollback;
963 }
964
965 status = ocfs2_journal_access(handle, alloc_inode, fe_bh,
966 OCFS2_JOURNAL_ACCESS_WRITE);
967 if (status < 0) {
968 mlog_errno(status);
969 goto out_rollback;
970 }
971
972 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
973
974 status = ocfs2_journal_dirty(handle, fe_bh);
975 if (status < 0) {
976 mlog_errno(status);
977 goto out_rollback;
978 }
979
980 status = 0;
981out_rollback:
982 if (status < 0) {
983 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
984 bg->bg_next_group = cpu_to_le64(bg_ptr);
985 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
986 }
987out:
988 mlog_exit(status);
989 return status;
990}
991
992static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
993 u32 wanted)
994{
995 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
996}
997
998/* return 0 on success, -ENOSPC to keep searching and any other < 0
999 * value on error. */
1000static int ocfs2_cluster_group_search(struct inode *inode,
1001 struct buffer_head *group_bh,
1002 u32 bits_wanted, u32 min_bits,
1003 u16 *bit_off, u16 *bits_found)
1004{
1005 int search = -ENOSPC;
1006 int ret;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001007 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001008 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001009 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001010
1011 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1012
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001013 if (gd->bg_free_bits_count) {
1014 max_bits = le16_to_cpu(gd->bg_bits);
1015
1016 /* Tail groups in cluster bitmaps which aren't cpg
1017 * aligned are prone to partial extention by a failed
1018 * fs resize. If the file system resize never got to
1019 * update the dinode cluster count, then we don't want
1020 * to trust any clusters past it, regardless of what
1021 * the group descriptor says. */
1022 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1023 le64_to_cpu(gd->bg_blkno));
1024 if ((gd_cluster_off + max_bits) >
1025 OCFS2_I(inode)->ip_clusters) {
1026 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1027 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1028 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1029 le16_to_cpu(gd->bg_bits),
1030 OCFS2_I(inode)->ip_clusters, max_bits);
1031 }
1032
Mark Fashehccd979b2005-12-15 14:31:24 -08001033 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1034 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001035 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 &tmp_off, &tmp_found);
1037 if (ret)
1038 return ret;
1039
1040 /* ocfs2_block_group_find_clear_bits() might
1041 * return success, but we still want to return
1042 * -ENOSPC unless it found the minimum number
1043 * of bits. */
1044 if (min_bits <= tmp_found) {
1045 *bit_off = tmp_off;
1046 *bits_found = tmp_found;
1047 search = 0; /* success */
1048 }
1049 }
1050
1051 return search;
1052}
1053
1054static int ocfs2_block_group_search(struct inode *inode,
1055 struct buffer_head *group_bh,
1056 u32 bits_wanted, u32 min_bits,
1057 u16 *bit_off, u16 *bits_found)
1058{
1059 int ret = -ENOSPC;
1060 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1061
1062 BUG_ON(min_bits != 1);
1063 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1064
1065 if (bg->bg_free_bits_count)
1066 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1067 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001068 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 bit_off, bits_found);
1070
1071 return ret;
1072}
1073
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001074static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001075 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001076 struct buffer_head *di_bh,
1077 u32 num_bits,
1078 u16 chain)
1079{
1080 int ret;
1081 u32 tmp_used;
1082 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1083 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1084
1085 ret = ocfs2_journal_access(handle, inode, di_bh,
1086 OCFS2_JOURNAL_ACCESS_WRITE);
1087 if (ret < 0) {
1088 mlog_errno(ret);
1089 goto out;
1090 }
1091
1092 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1093 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1094 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1095
1096 ret = ocfs2_journal_dirty(handle, di_bh);
1097 if (ret < 0)
1098 mlog_errno(ret);
1099
1100out:
1101 return ret;
1102}
1103
1104static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001105 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001106 u32 bits_wanted,
1107 u32 min_bits,
1108 u16 *bit_off,
1109 unsigned int *num_bits,
1110 u64 gd_blkno,
1111 u16 *bits_left)
1112{
1113 int ret;
1114 u16 found;
1115 struct buffer_head *group_bh = NULL;
1116 struct ocfs2_group_desc *gd;
1117 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001118
1119 ret = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb), gd_blkno,
1120 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1121 if (ret < 0) {
1122 mlog_errno(ret);
1123 return ret;
1124 }
1125
1126 gd = (struct ocfs2_group_desc *) group_bh->b_data;
1127 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
1128 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, gd);
1129 ret = -EIO;
1130 goto out;
1131 }
1132
1133 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1134 bit_off, &found);
1135 if (ret < 0) {
1136 if (ret != -ENOSPC)
1137 mlog_errno(ret);
1138 goto out;
1139 }
1140
1141 *num_bits = found;
1142
1143 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1144 *num_bits,
1145 le16_to_cpu(gd->bg_chain));
1146 if (ret < 0) {
1147 mlog_errno(ret);
1148 goto out;
1149 }
1150
1151 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1152 *bit_off, *num_bits);
1153 if (ret < 0)
1154 mlog_errno(ret);
1155
1156 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1157
1158out:
1159 brelse(group_bh);
1160
1161 return ret;
1162}
1163
Mark Fashehccd979b2005-12-15 14:31:24 -08001164static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001165 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 u32 bits_wanted,
1167 u32 min_bits,
1168 u16 *bit_off,
1169 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001170 u64 *bg_blkno,
1171 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001172{
1173 int status;
1174 u16 chain, tmp_bits;
1175 u32 tmp_used;
1176 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001177 struct inode *alloc_inode = ac->ac_inode;
1178 struct buffer_head *group_bh = NULL;
1179 struct buffer_head *prev_group_bh = NULL;
1180 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1181 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1182 struct ocfs2_group_desc *bg;
1183
1184 chain = ac->ac_chain;
Mark Fashehb0697052006-03-03 10:24:33 -08001185 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1186 bits_wanted, chain,
1187 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188
1189 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1190 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1191 &group_bh, OCFS2_BH_CACHED, alloc_inode);
1192 if (status < 0) {
1193 mlog_errno(status);
1194 goto bail;
1195 }
1196 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001197 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1198 if (status) {
1199 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001200 goto bail;
1201 }
1202
1203 status = -ENOSPC;
1204 /* for now, the chain search is a bit simplistic. We just use
1205 * the 1st group with any empty bits. */
1206 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1207 bits_wanted, min_bits, bit_off,
1208 &tmp_bits)) == -ENOSPC) {
1209 if (!bg->bg_next_group)
1210 break;
1211
1212 if (prev_group_bh) {
1213 brelse(prev_group_bh);
1214 prev_group_bh = NULL;
1215 }
1216 next_group = le64_to_cpu(bg->bg_next_group);
1217 prev_group_bh = group_bh;
1218 group_bh = NULL;
1219 status = ocfs2_read_block(OCFS2_SB(alloc_inode->i_sb),
1220 next_group, &group_bh,
1221 OCFS2_BH_CACHED, alloc_inode);
1222 if (status < 0) {
1223 mlog_errno(status);
1224 goto bail;
1225 }
1226 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001227 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, bg);
1228 if (status) {
1229 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001230 goto bail;
1231 }
1232 }
1233 if (status < 0) {
1234 if (status != -ENOSPC)
1235 mlog_errno(status);
1236 goto bail;
1237 }
1238
Mark Fashehb0697052006-03-03 10:24:33 -08001239 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001240 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001241
1242 *num_bits = tmp_bits;
1243
1244 BUG_ON(*num_bits == 0);
1245
1246 /*
1247 * Keep track of previous block descriptor read. When
1248 * we find a target, if we have read more than X
1249 * number of descriptors, and the target is reasonably
1250 * empty, relink him to top of his chain.
1251 *
1252 * We've read 0 extra blocks and only send one more to
1253 * the transaction, yet the next guy to search has a
1254 * much easier time.
1255 *
1256 * Do this *after* figuring out how many bits we're taking out
1257 * of our target group.
1258 */
1259 if (ac->ac_allow_chain_relink &&
1260 (prev_group_bh) &&
1261 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1262 status = ocfs2_relink_block_group(handle, alloc_inode,
1263 ac->ac_bh, group_bh,
1264 prev_group_bh, chain);
1265 if (status < 0) {
1266 mlog_errno(status);
1267 goto bail;
1268 }
1269 }
1270
1271 /* Ok, claim our bits now: set the info on dinode, chainlist
1272 * and then the group */
1273 status = ocfs2_journal_access(handle,
1274 alloc_inode,
1275 ac->ac_bh,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1277 if (status < 0) {
1278 mlog_errno(status);
1279 goto bail;
1280 }
1281
1282 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1283 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1284 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1285
1286 status = ocfs2_journal_dirty(handle,
1287 ac->ac_bh);
1288 if (status < 0) {
1289 mlog_errno(status);
1290 goto bail;
1291 }
1292
1293 status = ocfs2_block_group_set_bits(handle,
1294 alloc_inode,
1295 bg,
1296 group_bh,
1297 *bit_off,
1298 *num_bits);
1299 if (status < 0) {
1300 mlog_errno(status);
1301 goto bail;
1302 }
1303
Mark Fashehb0697052006-03-03 10:24:33 -08001304 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001305 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001306
1307 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001308 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001309bail:
1310 if (group_bh)
1311 brelse(group_bh);
1312 if (prev_group_bh)
1313 brelse(prev_group_bh);
1314
1315 mlog_exit(status);
1316 return status;
1317}
1318
1319/* will give out up to bits_wanted contiguous bits. */
1320static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1321 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001322 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001323 u32 bits_wanted,
1324 u32 min_bits,
1325 u16 *bit_off,
1326 unsigned int *num_bits,
1327 u64 *bg_blkno)
1328{
1329 int status;
1330 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001331 u16 bits_left = 0;
1332 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001333 struct ocfs2_chain_list *cl;
1334 struct ocfs2_dinode *fe;
1335
1336 mlog_entry_void();
1337
1338 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1339 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1340 BUG_ON(!ac->ac_bh);
1341
1342 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1343 if (!OCFS2_IS_VALID_DINODE(fe)) {
1344 OCFS2_RO_ON_INVALID_DINODE(osb->sb, fe);
1345 status = -EIO;
1346 goto bail;
1347 }
1348 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1349 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb0697052006-03-03 10:24:33 -08001350 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1351 "bits but only %u total.",
1352 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 le32_to_cpu(fe->id1.bitmap1.i_used),
1354 le32_to_cpu(fe->id1.bitmap1.i_total));
1355 status = -EIO;
1356 goto bail;
1357 }
1358
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001359 if (hint_blkno) {
1360 /* Attempt to short-circuit the usual search mechanism
1361 * by jumping straight to the most recently used
1362 * allocation group. This helps us mantain some
1363 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001364 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1365 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001366 hint_blkno, &bits_left);
1367 if (!status) {
1368 /* Be careful to update *bg_blkno here as the
1369 * caller is expecting it to be filled in, and
1370 * ocfs2_search_one_group() won't do that for
1371 * us. */
1372 *bg_blkno = hint_blkno;
1373 goto set_hint;
1374 }
1375 if (status < 0 && status != -ENOSPC) {
1376 mlog_errno(status);
1377 goto bail;
1378 }
1379 }
1380
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1382
1383 victim = ocfs2_find_victim_chain(cl);
1384 ac->ac_chain = victim;
1385 ac->ac_allow_chain_relink = 1;
1386
Mark Fashehda5cbf22006-10-06 18:34:35 -07001387 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001388 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001389 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001390 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 if (status < 0 && status != -ENOSPC) {
1392 mlog_errno(status);
1393 goto bail;
1394 }
1395
1396 mlog(0, "Search of victim chain %u came up with nothing, "
1397 "trying all chains now.\n", victim);
1398
1399 /* If we didn't pick a good victim, then just default to
1400 * searching each chain in order. Don't allow chain relinking
1401 * because we only calculate enough journal credits for one
1402 * relink per alloc. */
1403 ac->ac_allow_chain_relink = 0;
1404 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1405 if (i == victim)
1406 continue;
1407 if (!cl->cl_recs[i].c_free)
1408 continue;
1409
1410 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001411 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001412 bit_off, num_bits, bg_blkno,
1413 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001414 if (!status)
1415 break;
1416 if (status < 0 && status != -ENOSPC) {
1417 mlog_errno(status);
1418 goto bail;
1419 }
1420 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001421
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001422set_hint:
1423 if (status != -ENOSPC) {
1424 /* If the next search of this group is not likely to
1425 * yield a suitable extent, then we reset the last
1426 * group hint so as to not waste a disk read */
1427 if (bits_left < min_bits)
1428 ac->ac_last_group = 0;
1429 else
1430 ac->ac_last_group = *bg_blkno;
1431 }
1432
1433bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 mlog_exit(status);
1435 return status;
1436}
1437
1438int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001439 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001440 struct ocfs2_alloc_context *ac,
1441 u32 bits_wanted,
1442 u16 *suballoc_bit_start,
1443 unsigned int *num_bits,
1444 u64 *blkno_start)
1445{
1446 int status;
1447 u64 bg_blkno;
1448
1449 BUG_ON(!ac);
1450 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1451 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452
1453 status = ocfs2_claim_suballoc_bits(osb,
1454 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001455 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001456 bits_wanted,
1457 1,
1458 suballoc_bit_start,
1459 num_bits,
1460 &bg_blkno);
1461 if (status < 0) {
1462 mlog_errno(status);
1463 goto bail;
1464 }
1465 atomic_inc(&osb->alloc_stats.bg_allocs);
1466
1467 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1468 ac->ac_bits_given += (*num_bits);
1469 status = 0;
1470bail:
1471 mlog_exit(status);
1472 return status;
1473}
1474
1475int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001476 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001477 struct ocfs2_alloc_context *ac,
1478 u16 *suballoc_bit,
1479 u64 *fe_blkno)
1480{
1481 int status;
1482 unsigned int num_bits;
1483 u64 bg_blkno;
1484
1485 mlog_entry_void();
1486
1487 BUG_ON(!ac);
1488 BUG_ON(ac->ac_bits_given != 0);
1489 BUG_ON(ac->ac_bits_wanted != 1);
1490 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001491
1492 status = ocfs2_claim_suballoc_bits(osb,
1493 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001494 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001495 1,
1496 1,
1497 suballoc_bit,
1498 &num_bits,
1499 &bg_blkno);
1500 if (status < 0) {
1501 mlog_errno(status);
1502 goto bail;
1503 }
1504 atomic_inc(&osb->alloc_stats.bg_allocs);
1505
1506 BUG_ON(num_bits != 1);
1507
1508 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1509 ac->ac_bits_given++;
1510 status = 0;
1511bail:
1512 mlog_exit(status);
1513 return status;
1514}
1515
1516/* translate a group desc. blkno and it's bitmap offset into
1517 * disk cluster offset. */
1518static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1519 u64 bg_blkno,
1520 u16 bg_bit_off)
1521{
1522 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1523 u32 cluster = 0;
1524
1525 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1526
1527 if (bg_blkno != osb->first_cluster_group_blkno)
1528 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1529 cluster += (u32) bg_bit_off;
1530 return cluster;
1531}
1532
1533/* given a cluster offset, calculate which block group it belongs to
1534 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08001535u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08001536{
1537 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1538 u32 group_no;
1539
1540 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1541
1542 group_no = cluster / osb->bitmap_cpg;
1543 if (!group_no)
1544 return osb->first_cluster_group_blkno;
1545 return ocfs2_clusters_to_blocks(inode->i_sb,
1546 group_no * osb->bitmap_cpg);
1547}
1548
1549/* given the block number of a cluster start, calculate which cluster
1550 * group and descriptor bitmap offset that corresponds to. */
1551static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1552 u64 data_blkno,
1553 u64 *bg_blkno,
1554 u16 *bg_bit_off)
1555{
1556 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1557 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1558
1559 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1560
1561 *bg_blkno = ocfs2_which_cluster_group(inode,
1562 data_cluster);
1563
1564 if (*bg_blkno == osb->first_cluster_group_blkno)
1565 *bg_bit_off = (u16) data_cluster;
1566 else
1567 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1568 data_blkno - *bg_blkno);
1569}
1570
1571/*
1572 * min_bits - minimum contiguous chunk from this total allocation we
1573 * can handle. set to what we asked for originally for a full
1574 * contig. allocation, set to '1' to indicate we can deal with extents
1575 * of any size.
1576 */
Mark Fasheh415cb802007-09-16 20:10:16 -07001577int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1578 handle_t *handle,
1579 struct ocfs2_alloc_context *ac,
1580 u32 min_clusters,
1581 u32 max_clusters,
1582 u32 *cluster_start,
1583 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08001584{
1585 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07001586 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001587 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001588 u16 bg_bit_off;
1589
1590 mlog_entry_void();
1591
Mark Fashehccd979b2005-12-15 14:31:24 -08001592 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1593
1594 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1595 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001596
1597 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1598 status = ocfs2_claim_local_alloc_bits(osb,
1599 handle,
1600 ac,
1601 bits_wanted,
1602 cluster_start,
1603 num_clusters);
1604 if (!status)
1605 atomic_inc(&osb->alloc_stats.local_data);
1606 } else {
1607 if (min_clusters > (osb->bitmap_cpg - 1)) {
1608 /* The only paths asking for contiguousness
1609 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001610 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1611 "group bitmap size %u!\n", min_clusters,
1612 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 status = -ENOSPC;
1614 goto bail;
1615 }
1616 /* clamp the current request down to a realistic size. */
1617 if (bits_wanted > (osb->bitmap_cpg - 1))
1618 bits_wanted = osb->bitmap_cpg - 1;
1619
1620 status = ocfs2_claim_suballoc_bits(osb,
1621 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001622 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001623 bits_wanted,
1624 min_clusters,
1625 &bg_bit_off,
1626 num_clusters,
1627 &bg_blkno);
1628 if (!status) {
1629 *cluster_start =
1630 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1631 bg_blkno,
1632 bg_bit_off);
1633 atomic_inc(&osb->alloc_stats.bitmap_data);
1634 }
1635 }
1636 if (status < 0) {
1637 if (status != -ENOSPC)
1638 mlog_errno(status);
1639 goto bail;
1640 }
1641
1642 ac->ac_bits_given += *num_clusters;
1643
1644bail:
1645 mlog_exit(status);
1646 return status;
1647}
1648
Mark Fasheh415cb802007-09-16 20:10:16 -07001649int ocfs2_claim_clusters(struct ocfs2_super *osb,
1650 handle_t *handle,
1651 struct ocfs2_alloc_context *ac,
1652 u32 min_clusters,
1653 u32 *cluster_start,
1654 u32 *num_clusters)
1655{
1656 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1657
1658 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1659 bits_wanted, cluster_start, num_clusters);
1660}
1661
Mark Fasheh1fabe142006-10-09 18:11:45 -07001662static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001663 struct inode *alloc_inode,
1664 struct ocfs2_group_desc *bg,
1665 struct buffer_head *group_bh,
1666 unsigned int bit_off,
1667 unsigned int num_bits)
1668{
1669 int status;
1670 unsigned int tmp;
1671 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1672 struct ocfs2_group_desc *undo_bg = NULL;
1673
1674 mlog_entry_void();
1675
1676 if (!OCFS2_IS_VALID_GROUP_DESC(bg)) {
1677 OCFS2_RO_ON_INVALID_GROUP_DESC(alloc_inode->i_sb, bg);
1678 status = -EIO;
1679 goto bail;
1680 }
1681
1682 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1683
1684 if (ocfs2_is_cluster_bitmap(alloc_inode))
1685 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1686
1687 status = ocfs2_journal_access(handle, alloc_inode, group_bh,
1688 journal_type);
1689 if (status < 0) {
1690 mlog_errno(status);
1691 goto bail;
1692 }
1693
1694 if (ocfs2_is_cluster_bitmap(alloc_inode))
1695 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1696
1697 tmp = num_bits;
1698 while(tmp--) {
1699 ocfs2_clear_bit((bit_off + tmp),
1700 (unsigned long *) bg->bg_bitmap);
1701 if (ocfs2_is_cluster_bitmap(alloc_inode))
1702 ocfs2_set_bit(bit_off + tmp,
1703 (unsigned long *) undo_bg->bg_bitmap);
1704 }
1705 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1706
1707 status = ocfs2_journal_dirty(handle, group_bh);
1708 if (status < 0)
1709 mlog_errno(status);
1710bail:
1711 return status;
1712}
1713
1714/*
1715 * expects the suballoc inode to already be locked.
1716 */
Mark Fasheh2b604352007-06-22 15:45:27 -07001717int ocfs2_free_suballoc_bits(handle_t *handle,
1718 struct inode *alloc_inode,
1719 struct buffer_head *alloc_bh,
1720 unsigned int start_bit,
1721 u64 bg_blkno,
1722 unsigned int count)
Mark Fashehccd979b2005-12-15 14:31:24 -08001723{
1724 int status = 0;
1725 u32 tmp_used;
1726 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
1727 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1728 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1729 struct buffer_head *group_bh = NULL;
1730 struct ocfs2_group_desc *group;
1731
1732 mlog_entry_void();
1733
1734 if (!OCFS2_IS_VALID_DINODE(fe)) {
1735 OCFS2_RO_ON_INVALID_DINODE(alloc_inode->i_sb, fe);
1736 status = -EIO;
1737 goto bail;
1738 }
1739 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1740
Mark Fashehb0697052006-03-03 10:24:33 -08001741 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1742 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1743 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001744
1745 status = ocfs2_read_block(osb, bg_blkno, &group_bh, OCFS2_BH_CACHED,
1746 alloc_inode);
1747 if (status < 0) {
1748 mlog_errno(status);
1749 goto bail;
1750 }
1751
1752 group = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001753 status = ocfs2_check_group_descriptor(alloc_inode->i_sb, fe, group);
1754 if (status) {
1755 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001756 goto bail;
1757 }
1758 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1759
1760 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1761 group, group_bh,
1762 start_bit, count);
1763 if (status < 0) {
1764 mlog_errno(status);
1765 goto bail;
1766 }
1767
1768 status = ocfs2_journal_access(handle, alloc_inode, alloc_bh,
1769 OCFS2_JOURNAL_ACCESS_WRITE);
1770 if (status < 0) {
1771 mlog_errno(status);
1772 goto bail;
1773 }
1774
1775 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1776 count);
1777 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1778 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1779
1780 status = ocfs2_journal_dirty(handle, alloc_bh);
1781 if (status < 0) {
1782 mlog_errno(status);
1783 goto bail;
1784 }
1785
1786bail:
1787 if (group_bh)
1788 brelse(group_bh);
1789
1790 mlog_exit(status);
1791 return status;
1792}
1793
Mark Fasheh1fabe142006-10-09 18:11:45 -07001794int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 struct inode *inode_alloc_inode,
1796 struct buffer_head *inode_alloc_bh,
1797 struct ocfs2_dinode *di)
1798{
1799 u64 blk = le64_to_cpu(di->i_blkno);
1800 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1801 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1802
1803 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1804 inode_alloc_bh, bit, bg_blkno, 1);
1805}
1806
Mark Fasheh1fabe142006-10-09 18:11:45 -07001807int ocfs2_free_clusters(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001808 struct inode *bitmap_inode,
1809 struct buffer_head *bitmap_bh,
1810 u64 start_blk,
1811 unsigned int num_clusters)
1812{
1813 int status;
1814 u16 bg_start_bit;
1815 u64 bg_blkno;
1816 struct ocfs2_dinode *fe;
1817
1818 /* You can't ever have a contiguous set of clusters
1819 * bigger than a block group bitmap so we never have to worry
1820 * about looping on them. */
1821
1822 mlog_entry_void();
1823
1824 /* This is expensive. We can safely remove once this stuff has
1825 * gotten tested really well. */
1826 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
1827
1828 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
1829
1830 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
1831 &bg_start_bit);
1832
Mark Fashehb0697052006-03-03 10:24:33 -08001833 mlog(0, "want to free %u clusters starting at block %llu\n",
1834 num_clusters, (unsigned long long)start_blk);
1835 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
1836 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001837
1838 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
1839 bg_start_bit, bg_blkno,
1840 num_clusters);
1841 if (status < 0)
1842 mlog_errno(status);
1843
1844 mlog_exit(status);
1845 return status;
1846}
1847
1848static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
1849{
1850 printk("Block Group:\n");
1851 printk("bg_signature: %s\n", bg->bg_signature);
1852 printk("bg_size: %u\n", bg->bg_size);
1853 printk("bg_bits: %u\n", bg->bg_bits);
1854 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
1855 printk("bg_chain: %u\n", bg->bg_chain);
1856 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb0697052006-03-03 10:24:33 -08001857 printk("bg_next_group: %llu\n",
1858 (unsigned long long)bg->bg_next_group);
1859 printk("bg_parent_dinode: %llu\n",
1860 (unsigned long long)bg->bg_parent_dinode);
1861 printk("bg_blkno: %llu\n",
1862 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001863}
1864
1865static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
1866{
1867 int i;
1868
Mark Fashehb0697052006-03-03 10:24:33 -08001869 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001870 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb0697052006-03-03 10:24:33 -08001871 printk("i_size: %llu\n",
1872 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08001873 printk("i_clusters: %u\n", fe->i_clusters);
1874 printk("i_generation: %u\n",
1875 le32_to_cpu(fe->i_generation));
1876 printk("id1.bitmap1.i_used: %u\n",
1877 le32_to_cpu(fe->id1.bitmap1.i_used));
1878 printk("id1.bitmap1.i_total: %u\n",
1879 le32_to_cpu(fe->id1.bitmap1.i_total));
1880 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
1881 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
1882 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
1883 printk("id2.i_chain.cl_next_free_rec: %u\n",
1884 fe->id2.i_chain.cl_next_free_rec);
1885 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
1886 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
1887 fe->id2.i_chain.cl_recs[i].c_free);
1888 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
1889 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb0697052006-03-03 10:24:33 -08001890 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
1891 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001892 }
1893}