blob: 487f00c45f8407c3295c1acdacbab52dea94508b [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"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070038#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "inode.h"
41#include "journal.h"
42#include "localalloc.h"
43#include "suballoc.h"
44#include "super.h"
45#include "sysfile.h"
46#include "uptodate.h"
47
48#include "buffer_head_io.h"
49
Tao Maffda89a2008-03-03 17:12:09 +080050#define NOT_ALLOC_NEW_GROUP 0
51#define ALLOC_NEW_GROUP 1
52
Tao Ma4d0ddb22008-03-05 16:11:46 +080053#define OCFS2_MAX_INODES_TO_STEAL 1024
54
Mark Fashehccd979b2005-12-15 14:31:24 -080055static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
56static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
57static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070058static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080059 struct inode *alloc_inode,
60 struct buffer_head *bg_bh,
61 u64 group_blkno,
62 u16 my_chain,
63 struct ocfs2_chain_list *cl);
64static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
65 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070066 struct buffer_head *bh,
67 u64 max_block);
Mark Fashehccd979b2005-12-15 14:31:24 -080068
Mark Fashehccd979b2005-12-15 14:31:24 -080069static int ocfs2_cluster_group_search(struct inode *inode,
70 struct buffer_head *group_bh,
71 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070072 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 u16 *bit_off, u16 *bits_found);
74static int ocfs2_block_group_search(struct inode *inode,
75 struct buffer_head *group_bh,
76 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070077 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080078 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080079static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
80 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070081 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080082 u32 bits_wanted,
83 u32 min_bits,
84 u16 *bit_off,
85 unsigned int *num_bits,
86 u64 *bg_blkno);
87static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
88 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070089static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080090 struct inode *alloc_inode,
91 struct ocfs2_group_desc *bg,
92 struct buffer_head *group_bh,
93 unsigned int bit_off,
94 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070095static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080096 struct inode *alloc_inode,
97 struct ocfs2_group_desc *bg,
98 struct buffer_head *group_bh,
99 unsigned int bit_off,
100 unsigned int num_bits);
101
Mark Fasheh1fabe142006-10-09 18:11:45 -0700102static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800103 struct inode *alloc_inode,
104 struct buffer_head *fe_bh,
105 struct buffer_head *bg_bh,
106 struct buffer_head *prev_bg_bh,
107 u16 chain);
108static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
109 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800110static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
111 u64 bg_blkno,
112 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static inline void ocfs2_block_to_cluster_group(struct inode *inode,
114 u64 data_blkno,
115 u64 *bg_blkno,
116 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700117static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
118 u32 bits_wanted, u64 max_block,
119 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800120
Mark Fasheh9c7af402008-07-28 18:02:53 -0700121void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800122{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700123 struct inode *inode = ac->ac_inode;
124
125 if (inode) {
126 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700127 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700128
129 mutex_unlock(&inode->i_mutex);
130
131 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800132 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700133 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700134 brelse(ac->ac_bh);
135 ac->ac_bh = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800136}
137
138void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
139{
140 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800141 kfree(ac);
142}
143
144static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
145{
146 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
147}
148
Joel Becker57e3e792008-11-13 14:49:13 -0800149#define do_error(fmt, ...) \
150 do{ \
151 if (clean_error) \
152 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
153 else \
154 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
155 } while (0)
156
Joel Becker970e4932008-11-13 14:49:19 -0800157static int ocfs2_validate_gd_self(struct super_block *sb,
158 struct buffer_head *bh,
159 int clean_error)
160{
161 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
162
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700163 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800164 do_error("Group descriptor #%llu has bad signature %.*s",
165 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800166 gd->bg_signature);
167 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700168 }
169
Joel Becker68f64d42008-11-13 14:49:14 -0800170 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
171 do_error("Group descriptor #%llu has an invalid bg_blkno "
172 "of %llu",
173 (unsigned long long)bh->b_blocknr,
174 (unsigned long long)le64_to_cpu(gd->bg_blkno));
175 return -EINVAL;
176 }
177
178 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
179 do_error("Group descriptor #%llu has an invalid "
180 "fs_generation of #%u",
181 (unsigned long long)bh->b_blocknr,
182 le32_to_cpu(gd->bg_generation));
183 return -EINVAL;
184 }
185
Joel Becker970e4932008-11-13 14:49:19 -0800186 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
187 do_error("Group descriptor #%llu has bit count %u but "
188 "claims that %u are free",
189 (unsigned long long)bh->b_blocknr,
190 le16_to_cpu(gd->bg_bits),
191 le16_to_cpu(gd->bg_free_bits_count));
192 return -EINVAL;
193 }
194
195 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
196 do_error("Group descriptor #%llu has bit count %u but "
197 "max bitmap bits of %u",
198 (unsigned long long)bh->b_blocknr,
199 le16_to_cpu(gd->bg_bits),
200 8 * le16_to_cpu(gd->bg_size));
201 return -EINVAL;
202 }
203
204 return 0;
205}
206
207static int ocfs2_validate_gd_parent(struct super_block *sb,
208 struct ocfs2_dinode *di,
209 struct buffer_head *bh,
210 int clean_error)
211{
212 unsigned int max_bits;
213 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
214
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700215 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800216 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800217 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800218 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800219 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
220 (unsigned long long)le64_to_cpu(di->i_blkno));
221 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700222 }
223
224 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
225 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800226 do_error("Group descriptor #%llu has bit count of %u",
227 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800228 le16_to_cpu(gd->bg_bits));
229 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700230 }
231
232 if (le16_to_cpu(gd->bg_chain) >=
233 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800234 do_error("Group descriptor #%llu has bad chain %u",
235 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800236 le16_to_cpu(gd->bg_chain));
237 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700238 }
239
Joel Becker970e4932008-11-13 14:49:19 -0800240 return 0;
241}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700242
Joel Becker57e3e792008-11-13 14:49:13 -0800243#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700244
Joel Becker970e4932008-11-13 14:49:19 -0800245/*
246 * This version only prints errors. It does not fail the filesystem, and
247 * exists only for resize.
248 */
249int ocfs2_check_group_descriptor(struct super_block *sb,
250 struct ocfs2_dinode *di,
251 struct buffer_head *bh)
252{
253 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700254 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800255
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700256 BUG_ON(!buffer_uptodate(bh));
257
258 /*
259 * If the ecc fails, we return the error but otherwise
260 * leave the filesystem running. We know any error is
261 * local to this block.
262 */
263 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700264 if (rc) {
265 mlog(ML_ERROR,
266 "Checksum failed for group descriptor %llu\n",
267 (unsigned long long)bh->b_blocknr);
268 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700269 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800270 if (!rc)
271 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
272
273 return rc;
274}
275
276static int ocfs2_validate_group_descriptor(struct super_block *sb,
277 struct buffer_head *bh)
278{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700279 int rc;
280 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
281
Joel Becker970e4932008-11-13 14:49:19 -0800282 mlog(0, "Validating group descriptor %llu\n",
283 (unsigned long long)bh->b_blocknr);
284
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700285 BUG_ON(!buffer_uptodate(bh));
286
287 /*
288 * If the ecc fails, we return the error but otherwise
289 * leave the filesystem running. We know any error is
290 * local to this block.
291 */
292 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
293 if (rc)
294 return rc;
295
296 /*
297 * Errors after here are fatal.
298 */
299
Joel Becker970e4932008-11-13 14:49:19 -0800300 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700301}
302
Joel Becker68f64d42008-11-13 14:49:14 -0800303int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
304 u64 gd_blkno, struct buffer_head **bh)
305{
306 int rc;
307 struct buffer_head *tmp = *bh;
308
Joel Becker970e4932008-11-13 14:49:19 -0800309 rc = ocfs2_read_block(inode, gd_blkno, &tmp,
310 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800311 if (rc)
312 goto out;
313
Joel Becker970e4932008-11-13 14:49:19 -0800314 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800315 if (rc) {
316 brelse(tmp);
317 goto out;
318 }
319
320 /* If ocfs2_read_block() got us a new bh, pass it up. */
321 if (!*bh)
322 *bh = tmp;
323
324out:
325 return rc;
326}
327
Mark Fasheh1fabe142006-10-09 18:11:45 -0700328static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800329 struct inode *alloc_inode,
330 struct buffer_head *bg_bh,
331 u64 group_blkno,
332 u16 my_chain,
333 struct ocfs2_chain_list *cl)
334{
335 int status = 0;
336 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
337 struct super_block * sb = alloc_inode->i_sb;
338
339 mlog_entry_void();
340
341 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800342 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
343 "b_blocknr (%llu)",
344 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800345 (unsigned long long) bg_bh->b_blocknr);
346 status = -EIO;
347 goto bail;
348 }
349
Joel Becker13723d02008-10-17 19:25:01 -0700350 status = ocfs2_journal_access_gd(handle,
351 alloc_inode,
352 bg_bh,
353 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800354 if (status < 0) {
355 mlog_errno(status);
356 goto bail;
357 }
358
359 memset(bg, 0, sb->s_blocksize);
360 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
361 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
362 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
363 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
364 bg->bg_chain = cpu_to_le16(my_chain);
365 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
366 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
367 bg->bg_blkno = cpu_to_le64(group_blkno);
368 /* set the 1st bit in the bitmap to account for the descriptor block */
369 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
370 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
371
372 status = ocfs2_journal_dirty(handle, bg_bh);
373 if (status < 0)
374 mlog_errno(status);
375
376 /* There is no need to zero out or otherwise initialize the
377 * other blocks in a group - All valid FS metadata in a block
378 * group stores the superblock fs_generation value at
379 * allocation time. */
380
381bail:
382 mlog_exit(status);
383 return status;
384}
385
386static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
387{
388 u16 curr, best;
389
390 best = curr = 0;
391 while (curr < le16_to_cpu(cl->cl_count)) {
392 if (le32_to_cpu(cl->cl_recs[best].c_total) >
393 le32_to_cpu(cl->cl_recs[curr].c_total))
394 best = curr;
395 curr++;
396 }
397 return best;
398}
399
400/*
401 * We expect the block group allocator to already be locked.
402 */
403static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
404 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700405 struct buffer_head *bh,
406 u64 max_block)
Mark Fashehccd979b2005-12-15 14:31:24 -0800407{
408 int status, credits;
409 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
410 struct ocfs2_chain_list *cl;
411 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700412 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800413 u32 bit_off, num_bits;
414 u16 alloc_rec;
415 u64 bg_blkno;
416 struct buffer_head *bg_bh = NULL;
417 struct ocfs2_group_desc *bg;
418
419 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
420
421 mlog_entry_void();
422
Mark Fashehccd979b2005-12-15 14:31:24 -0800423 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700424 status = ocfs2_reserve_clusters_with_limit(osb,
425 le16_to_cpu(cl->cl_cpg),
426 max_block, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800427 if (status < 0) {
428 if (status != -ENOSPC)
429 mlog_errno(status);
430 goto bail;
431 }
432
433 credits = ocfs2_calc_group_alloc_credits(osb->sb,
434 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700435 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800436 if (IS_ERR(handle)) {
437 status = PTR_ERR(handle);
438 handle = NULL;
439 mlog_errno(status);
440 goto bail;
441 }
442
443 status = ocfs2_claim_clusters(osb,
444 handle,
445 ac,
446 le16_to_cpu(cl->cl_cpg),
447 &bit_off,
448 &num_bits);
449 if (status < 0) {
450 if (status != -ENOSPC)
451 mlog_errno(status);
452 goto bail;
453 }
454
455 alloc_rec = ocfs2_find_smallest_chain(cl);
456
457 /* setup the group */
458 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800459 mlog(0, "new descriptor, record %u, at block %llu\n",
460 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800461
462 bg_bh = sb_getblk(osb->sb, bg_blkno);
463 if (!bg_bh) {
464 status = -EIO;
465 mlog_errno(status);
466 goto bail;
467 }
468 ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
469
470 status = ocfs2_block_group_fill(handle,
471 alloc_inode,
472 bg_bh,
473 bg_blkno,
474 alloc_rec,
475 cl);
476 if (status < 0) {
477 mlog_errno(status);
478 goto bail;
479 }
480
481 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
482
Joel Becker13723d02008-10-17 19:25:01 -0700483 status = ocfs2_journal_access_di(handle, alloc_inode,
484 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800485 if (status < 0) {
486 mlog_errno(status);
487 goto bail;
488 }
489
490 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
491 le16_to_cpu(bg->bg_free_bits_count));
492 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
493 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
494 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
495 le16_add_cpu(&cl->cl_next_free_rec, 1);
496
497 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
498 le16_to_cpu(bg->bg_free_bits_count));
499 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
500 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
501
502 status = ocfs2_journal_dirty(handle, bh);
503 if (status < 0) {
504 mlog_errno(status);
505 goto bail;
506 }
507
508 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
509 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
510 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
511 le32_to_cpu(fe->i_clusters)));
512 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
513 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700514 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800515
516 status = 0;
517bail:
518 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700519 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800520
521 if (ac)
522 ocfs2_free_alloc_context(ac);
523
Mark Fasheha81cb882008-10-07 14:25:16 -0700524 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800525
526 mlog_exit(status);
527 return status;
528}
529
530static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700531 struct ocfs2_alloc_context *ac,
532 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800533 u32 slot,
534 int alloc_new_group)
Mark Fashehccd979b2005-12-15 14:31:24 -0800535{
536 int status;
537 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700538 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800539 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800540 struct ocfs2_dinode *fe;
541 u32 free_bits;
542
543 mlog_entry_void();
544
Mark Fashehda5cbf22006-10-06 18:34:35 -0700545 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
546 if (!alloc_inode) {
547 mlog_errno(-EINVAL);
548 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800549 }
550
Mark Fashehda5cbf22006-10-06 18:34:35 -0700551 mutex_lock(&alloc_inode->i_mutex);
552
Mark Fashehe63aecb62007-10-18 15:30:42 -0700553 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700554 if (status < 0) {
555 mutex_unlock(&alloc_inode->i_mutex);
556 iput(alloc_inode);
557
558 mlog_errno(status);
559 return status;
560 }
561
562 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800563 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700564
Mark Fashehccd979b2005-12-15 14:31:24 -0800565 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800566
567 /* The bh was validated by the inode read inside
568 * ocfs2_inode_lock(). Any corruption is a code bug. */
569 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
570
Mark Fashehccd979b2005-12-15 14:31:24 -0800571 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800572 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
573 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800574 status = -EIO;
575 goto bail;
576 }
577
578 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
579 le32_to_cpu(fe->id1.bitmap1.i_used);
580
581 if (bits_wanted > free_bits) {
582 /* cluster bitmap never grows */
583 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
584 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
585 bits_wanted, free_bits);
586 status = -ENOSPC;
587 goto bail;
588 }
589
Tao Maffda89a2008-03-03 17:12:09 +0800590 if (alloc_new_group != ALLOC_NEW_GROUP) {
591 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
592 "and we don't alloc a new group for it.\n",
593 slot, bits_wanted, free_bits);
594 status = -ENOSPC;
595 goto bail;
596 }
597
Joel Becker1187c962008-09-03 20:03:39 -0700598 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
599 ac->ac_max_block);
Mark Fashehccd979b2005-12-15 14:31:24 -0800600 if (status < 0) {
601 if (status != -ENOSPC)
602 mlog_errno(status);
603 goto bail;
604 }
605 atomic_inc(&osb->alloc_stats.bg_extends);
606
607 /* You should never ask for this much metadata */
608 BUG_ON(bits_wanted >
609 (le32_to_cpu(fe->id1.bitmap1.i_total)
610 - le32_to_cpu(fe->id1.bitmap1.i_used)));
611 }
612
613 get_bh(bh);
614 ac->ac_bh = bh;
615bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700616 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800617
618 mlog_exit(status);
619 return status;
620}
621
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800622int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
623 int blocks,
624 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800625{
626 int status;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700627 u32 slot;
Mark Fashehccd979b2005-12-15 14:31:24 -0800628
Robert P. J. Daycd861282006-12-13 00:34:52 -0800629 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800630 if (!(*ac)) {
631 status = -ENOMEM;
632 mlog_errno(status);
633 goto bail;
634 }
635
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800636 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800637 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700638 slot = osb->slot_num;
Mark Fashehccd979b2005-12-15 14:31:24 -0800639 (*ac)->ac_group_search = ocfs2_block_group_search;
640
Mark Fashehda5cbf22006-10-06 18:34:35 -0700641 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800642 EXTENT_ALLOC_SYSTEM_INODE,
643 slot, ALLOC_NEW_GROUP);
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 if (status < 0) {
645 if (status != -ENOSPC)
646 mlog_errno(status);
647 goto bail;
648 }
649
650 status = 0;
651bail:
652 if ((status < 0) && *ac) {
653 ocfs2_free_alloc_context(*ac);
654 *ac = NULL;
655 }
656
Mark Fashehccd979b2005-12-15 14:31:24 -0800657 mlog_exit(status);
658 return status;
659}
660
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800661int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
662 struct ocfs2_extent_list *root_el,
663 struct ocfs2_alloc_context **ac)
664{
665 return ocfs2_reserve_new_metadata_blocks(osb,
666 ocfs2_extend_meta_needed(root_el),
667 ac);
668}
669
Tao Ma4d0ddb22008-03-05 16:11:46 +0800670static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
671 struct ocfs2_alloc_context *ac)
672{
673 int i, status = -ENOSPC;
674 s16 slot = ocfs2_get_inode_steal_slot(osb);
675
676 /* Start to steal inodes from the first slot after ours. */
677 if (slot == OCFS2_INVALID_SLOT)
678 slot = osb->slot_num + 1;
679
680 for (i = 0; i < osb->max_slots; i++, slot++) {
681 if (slot == osb->max_slots)
682 slot = 0;
683
684 if (slot == osb->slot_num)
685 continue;
686
687 status = ocfs2_reserve_suballoc_bits(osb, ac,
688 INODE_ALLOC_SYSTEM_INODE,
689 slot, NOT_ALLOC_NEW_GROUP);
690 if (status >= 0) {
691 ocfs2_set_inode_steal_slot(osb, slot);
692 break;
693 }
694
695 ocfs2_free_ac_resource(ac);
696 }
697
698 return status;
699}
700
Mark Fashehccd979b2005-12-15 14:31:24 -0800701int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800702 struct ocfs2_alloc_context **ac)
703{
704 int status;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800705 s16 slot = ocfs2_get_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706
Robert P. J. Daycd861282006-12-13 00:34:52 -0800707 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800708 if (!(*ac)) {
709 status = -ENOMEM;
710 mlog_errno(status);
711 goto bail;
712 }
713
714 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800715 (*ac)->ac_which = OCFS2_AC_USE_INODE;
716
Mark Fashehccd979b2005-12-15 14:31:24 -0800717 (*ac)->ac_group_search = ocfs2_block_group_search;
718
Tao Ma4d0ddb22008-03-05 16:11:46 +0800719 /*
Joel Becker1187c962008-09-03 20:03:39 -0700720 * stat(2) can't handle i_ino > 32bits, so we tell the
721 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -0700722 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -0700723 */
Joel Becker12462f12008-09-03 20:03:40 -0700724 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
725 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -0700726
727 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +0800728 * slot is set when we successfully steal inode from other nodes.
729 * It is reset in 3 places:
730 * 1. when we flush the truncate log
731 * 2. when we complete local alloc recovery.
732 * 3. when we successfully allocate from our own slot.
733 * After it is set, we will go on stealing inodes until we find the
734 * need to check our slots to see whether there is some space for us.
735 */
736 if (slot != OCFS2_INVALID_SLOT &&
737 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
738 goto inode_steal;
739
740 atomic_set(&osb->s_num_inodes_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700741 status = ocfs2_reserve_suballoc_bits(osb, *ac,
742 INODE_ALLOC_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800743 osb->slot_num, ALLOC_NEW_GROUP);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800744 if (status >= 0) {
745 status = 0;
746
747 /*
748 * Some inodes must be freed by us, so try to allocate
749 * from our own next time.
750 */
751 if (slot != OCFS2_INVALID_SLOT)
752 ocfs2_init_inode_steal_slot(osb);
753 goto bail;
754 } else if (status < 0 && status != -ENOSPC) {
755 mlog_errno(status);
756 goto bail;
757 }
758
759 ocfs2_free_ac_resource(*ac);
760
761inode_steal:
762 status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
763 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800764 if (status < 0) {
765 if (status != -ENOSPC)
766 mlog_errno(status);
767 goto bail;
768 }
769
770 status = 0;
771bail:
772 if ((status < 0) && *ac) {
773 ocfs2_free_alloc_context(*ac);
774 *ac = NULL;
775 }
776
Mark Fashehccd979b2005-12-15 14:31:24 -0800777 mlog_exit(status);
778 return status;
779}
780
781/* local alloc code has to do the same thing, so rather than do this
782 * twice.. */
783int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
784 struct ocfs2_alloc_context *ac)
785{
786 int status;
787
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 ac->ac_which = OCFS2_AC_USE_MAIN;
789 ac->ac_group_search = ocfs2_cluster_group_search;
790
Mark Fashehda5cbf22006-10-06 18:34:35 -0700791 status = ocfs2_reserve_suballoc_bits(osb, ac,
792 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Maffda89a2008-03-03 17:12:09 +0800793 OCFS2_INVALID_SLOT,
794 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700795 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800796 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700797 goto bail;
798 }
799
Mark Fashehccd979b2005-12-15 14:31:24 -0800800bail:
801 return status;
802}
803
804/* Callers don't need to care which bitmap (local alloc or main) to
805 * use so we figure it out for them, but unfortunately this clutters
806 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -0700807static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
808 u32 bits_wanted, u64 max_block,
809 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800810{
811 int status;
812
813 mlog_entry_void();
814
Robert P. J. Daycd861282006-12-13 00:34:52 -0800815 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800816 if (!(*ac)) {
817 status = -ENOMEM;
818 mlog_errno(status);
819 goto bail;
820 }
821
822 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -0700823 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -0800824
825 status = -ENOSPC;
826 if (ocfs2_alloc_should_use_local(osb, bits_wanted)) {
827 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800828 bits_wanted,
829 *ac);
Joel Becker1187c962008-09-03 20:03:39 -0700830 if (status == -EFBIG) {
831 /* The local alloc window is outside ac_max_block.
832 * use the main bitmap. */
833 status = -ENOSPC;
834 } else if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 mlog_errno(status);
836 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 }
838 }
839
840 if (status == -ENOSPC) {
841 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
842 if (status < 0) {
843 if (status != -ENOSPC)
844 mlog_errno(status);
845 goto bail;
846 }
847 }
848
849 status = 0;
850bail:
851 if ((status < 0) && *ac) {
852 ocfs2_free_alloc_context(*ac);
853 *ac = NULL;
854 }
855
856 mlog_exit(status);
857 return status;
858}
859
Joel Becker1187c962008-09-03 20:03:39 -0700860int ocfs2_reserve_clusters(struct ocfs2_super *osb,
861 u32 bits_wanted,
862 struct ocfs2_alloc_context **ac)
863{
864 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0, ac);
865}
866
Mark Fashehccd979b2005-12-15 14:31:24 -0800867/*
868 * More or less lifted from ext3. I'll leave their description below:
869 *
870 * "For ext3 allocations, we must not reuse any blocks which are
871 * allocated in the bitmap buffer's "last committed data" copy. This
872 * prevents deletes from freeing up the page for reuse until we have
873 * committed the delete transaction.
874 *
875 * If we didn't do this, then deleting something and reallocating it as
876 * data would allow the old block to be overwritten before the
877 * transaction committed (because we force data to disk before commit).
878 * This would lead to corruption if we crashed between overwriting the
879 * data and committing the delete.
880 *
881 * @@@ We may want to make this allocation behaviour conditional on
882 * data-writes at some point, and disable it for metadata allocations or
883 * sync-data inodes."
884 *
885 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +0200886 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -0800887 * called on a metadata group descriptor.
888 */
889static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
890 int nr)
891{
892 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
893
894 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
895 return 0;
896 if (!buffer_jbd(bg_bh) || !bh2jh(bg_bh)->b_committed_data)
897 return 1;
898
899 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
900 return !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
901}
902
903static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
904 struct buffer_head *bg_bh,
905 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700906 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 u16 *bit_off,
908 u16 *bits_found)
909{
910 void *bitmap;
911 u16 best_offset, best_size;
912 int offset, start, found, status = 0;
913 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
914
Joel Becker42035302008-11-13 14:49:15 -0800915 /* Callers got this descriptor from
916 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
917 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -0800918
919 found = start = best_offset = best_size = 0;
920 bitmap = bg->bg_bitmap;
921
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700922 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
923 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -0800924 break;
925
926 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
927 /* We found a zero, but we can't use it as it
928 * hasn't been put to disk yet! */
929 found = 0;
930 start = offset + 1;
931 } else if (offset == start) {
932 /* we found a zero */
933 found++;
934 /* move start to the next bit to test */
935 start++;
936 } else {
937 /* got a zero after some ones */
938 found = 1;
939 start = offset + 1;
940 }
941 if (found > best_size) {
942 best_size = found;
943 best_offset = start - found;
944 }
945 /* we got everything we needed */
946 if (found == bits_wanted) {
947 /* mlog(0, "Found it all!\n"); */
948 break;
949 }
950 }
951
952 /* XXX: I think the first clause is equivalent to the second
953 * - jlbec */
954 if (found == bits_wanted) {
955 *bit_off = start - found;
956 *bits_found = found;
957 } else if (best_size) {
958 *bit_off = best_offset;
959 *bits_found = best_size;
960 } else {
961 status = -ENOSPC;
962 /* No error log here -- see the comment above
963 * ocfs2_test_bg_bit_allocatable */
964 }
965
966 return status;
967}
968
Mark Fasheh1fabe142006-10-09 18:11:45 -0700969static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 struct inode *alloc_inode,
971 struct ocfs2_group_desc *bg,
972 struct buffer_head *group_bh,
973 unsigned int bit_off,
974 unsigned int num_bits)
975{
976 int status;
977 void *bitmap = bg->bg_bitmap;
978 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
979
980 mlog_entry_void();
981
Joel Becker42035302008-11-13 14:49:15 -0800982 /* All callers get the descriptor via
983 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
984 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
986
987 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
988 num_bits);
989
990 if (ocfs2_is_cluster_bitmap(alloc_inode))
991 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
992
Joel Becker13723d02008-10-17 19:25:01 -0700993 status = ocfs2_journal_access_gd(handle,
994 alloc_inode,
995 group_bh,
996 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 if (status < 0) {
998 mlog_errno(status);
999 goto bail;
1000 }
1001
1002 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1003
1004 while(num_bits--)
1005 ocfs2_set_bit(bit_off++, bitmap);
1006
1007 status = ocfs2_journal_dirty(handle,
1008 group_bh);
1009 if (status < 0) {
1010 mlog_errno(status);
1011 goto bail;
1012 }
1013
1014bail:
1015 mlog_exit(status);
1016 return status;
1017}
1018
1019/* find the one with the most empty bits */
1020static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1021{
1022 u16 curr, best;
1023
1024 BUG_ON(!cl->cl_next_free_rec);
1025
1026 best = curr = 0;
1027 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1028 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1029 le32_to_cpu(cl->cl_recs[best].c_free))
1030 best = curr;
1031 curr++;
1032 }
1033
1034 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1035 return best;
1036}
1037
Mark Fasheh1fabe142006-10-09 18:11:45 -07001038static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 struct inode *alloc_inode,
1040 struct buffer_head *fe_bh,
1041 struct buffer_head *bg_bh,
1042 struct buffer_head *prev_bg_bh,
1043 u16 chain)
1044{
1045 int status;
1046 /* there is a really tiny chance the journal calls could fail,
1047 * but we wouldn't want inconsistent blocks in *any* case. */
1048 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1049 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1050 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1051 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1052
Joel Becker42035302008-11-13 14:49:15 -08001053 /* The caller got these descriptors from
1054 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1055 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1056 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001057
Mark Fashehb06970532006-03-03 10:24:33 -08001058 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001059 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1060 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1061 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001062
1063 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1064 bg_ptr = le64_to_cpu(bg->bg_next_group);
1065 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1066
Joel Becker13723d02008-10-17 19:25:01 -07001067 status = ocfs2_journal_access_gd(handle, alloc_inode, prev_bg_bh,
1068 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 if (status < 0) {
1070 mlog_errno(status);
1071 goto out_rollback;
1072 }
1073
1074 prev_bg->bg_next_group = bg->bg_next_group;
1075
1076 status = ocfs2_journal_dirty(handle, prev_bg_bh);
1077 if (status < 0) {
1078 mlog_errno(status);
1079 goto out_rollback;
1080 }
1081
Joel Becker13723d02008-10-17 19:25:01 -07001082 status = ocfs2_journal_access_gd(handle, alloc_inode, bg_bh,
1083 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 if (status < 0) {
1085 mlog_errno(status);
1086 goto out_rollback;
1087 }
1088
1089 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1090
1091 status = ocfs2_journal_dirty(handle, bg_bh);
1092 if (status < 0) {
1093 mlog_errno(status);
1094 goto out_rollback;
1095 }
1096
Joel Becker13723d02008-10-17 19:25:01 -07001097 status = ocfs2_journal_access_di(handle, alloc_inode, fe_bh,
1098 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 if (status < 0) {
1100 mlog_errno(status);
1101 goto out_rollback;
1102 }
1103
1104 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1105
1106 status = ocfs2_journal_dirty(handle, fe_bh);
1107 if (status < 0) {
1108 mlog_errno(status);
1109 goto out_rollback;
1110 }
1111
1112 status = 0;
1113out_rollback:
1114 if (status < 0) {
1115 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1116 bg->bg_next_group = cpu_to_le64(bg_ptr);
1117 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1118 }
Joel Becker42035302008-11-13 14:49:15 -08001119
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 mlog_exit(status);
1121 return status;
1122}
1123
1124static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1125 u32 wanted)
1126{
1127 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1128}
1129
1130/* return 0 on success, -ENOSPC to keep searching and any other < 0
1131 * value on error. */
1132static int ocfs2_cluster_group_search(struct inode *inode,
1133 struct buffer_head *group_bh,
1134 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001135 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 u16 *bit_off, u16 *bits_found)
1137{
1138 int search = -ENOSPC;
1139 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001140 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001141 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001142 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001143 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001144 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001145
1146 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1147
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001148 if (gd->bg_free_bits_count) {
1149 max_bits = le16_to_cpu(gd->bg_bits);
1150
1151 /* Tail groups in cluster bitmaps which aren't cpg
1152 * aligned are prone to partial extention by a failed
1153 * fs resize. If the file system resize never got to
1154 * update the dinode cluster count, then we don't want
1155 * to trust any clusters past it, regardless of what
1156 * the group descriptor says. */
1157 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1158 le64_to_cpu(gd->bg_blkno));
1159 if ((gd_cluster_off + max_bits) >
1160 OCFS2_I(inode)->ip_clusters) {
1161 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1162 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1163 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1164 le16_to_cpu(gd->bg_bits),
1165 OCFS2_I(inode)->ip_clusters, max_bits);
1166 }
1167
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1169 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001170 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 &tmp_off, &tmp_found);
1172 if (ret)
1173 return ret;
1174
Joel Becker1187c962008-09-03 20:03:39 -07001175 if (max_block) {
1176 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1177 gd_cluster_off +
1178 tmp_off + tmp_found);
1179 mlog(0, "Checking %llu against %llu\n",
1180 (unsigned long long)blkoff,
1181 (unsigned long long)max_block);
1182 if (blkoff > max_block)
1183 return -ENOSPC;
1184 }
1185
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 /* ocfs2_block_group_find_clear_bits() might
1187 * return success, but we still want to return
1188 * -ENOSPC unless it found the minimum number
1189 * of bits. */
1190 if (min_bits <= tmp_found) {
1191 *bit_off = tmp_off;
1192 *bits_found = tmp_found;
1193 search = 0; /* success */
Mark Fasheh9c7af402008-07-28 18:02:53 -07001194 } else if (tmp_found) {
1195 /*
1196 * Don't show bits which we'll be returning
1197 * for allocation to the local alloc bitmap.
1198 */
1199 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
Mark Fashehccd979b2005-12-15 14:31:24 -08001200 }
1201 }
1202
1203 return search;
1204}
1205
1206static int ocfs2_block_group_search(struct inode *inode,
1207 struct buffer_head *group_bh,
1208 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001209 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001210 u16 *bit_off, u16 *bits_found)
1211{
1212 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001213 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1215
1216 BUG_ON(min_bits != 1);
1217 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1218
Joel Becker1187c962008-09-03 20:03:39 -07001219 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1221 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001222 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001223 bit_off, bits_found);
Joel Becker1187c962008-09-03 20:03:39 -07001224 if (!ret && max_block) {
1225 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1226 *bits_found;
1227 mlog(0, "Checking %llu against %llu\n",
1228 (unsigned long long)blkoff,
1229 (unsigned long long)max_block);
1230 if (blkoff > max_block)
1231 ret = -ENOSPC;
1232 }
1233 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001234
1235 return ret;
1236}
1237
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001238static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001239 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001240 struct buffer_head *di_bh,
1241 u32 num_bits,
1242 u16 chain)
1243{
1244 int ret;
1245 u32 tmp_used;
1246 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1247 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1248
Joel Becker13723d02008-10-17 19:25:01 -07001249 ret = ocfs2_journal_access_di(handle, inode, di_bh,
1250 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001251 if (ret < 0) {
1252 mlog_errno(ret);
1253 goto out;
1254 }
1255
1256 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1257 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1258 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1259
1260 ret = ocfs2_journal_dirty(handle, di_bh);
1261 if (ret < 0)
1262 mlog_errno(ret);
1263
1264out:
1265 return ret;
1266}
1267
1268static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001269 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001270 u32 bits_wanted,
1271 u32 min_bits,
1272 u16 *bit_off,
1273 unsigned int *num_bits,
1274 u64 gd_blkno,
1275 u16 *bits_left)
1276{
1277 int ret;
1278 u16 found;
1279 struct buffer_head *group_bh = NULL;
1280 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001281 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001282 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001283
Joel Becker68f64d42008-11-13 14:49:14 -08001284 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1285 &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001286 if (ret < 0) {
1287 mlog_errno(ret);
1288 return ret;
1289 }
1290
1291 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001292 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001293 ac->ac_max_block, bit_off, &found);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001294 if (ret < 0) {
1295 if (ret != -ENOSPC)
1296 mlog_errno(ret);
1297 goto out;
1298 }
1299
1300 *num_bits = found;
1301
1302 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1303 *num_bits,
1304 le16_to_cpu(gd->bg_chain));
1305 if (ret < 0) {
1306 mlog_errno(ret);
1307 goto out;
1308 }
1309
1310 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1311 *bit_off, *num_bits);
1312 if (ret < 0)
1313 mlog_errno(ret);
1314
1315 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1316
1317out:
1318 brelse(group_bh);
1319
1320 return ret;
1321}
1322
Mark Fashehccd979b2005-12-15 14:31:24 -08001323static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001324 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 u32 bits_wanted,
1326 u32 min_bits,
1327 u16 *bit_off,
1328 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001329 u64 *bg_blkno,
1330 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001331{
1332 int status;
1333 u16 chain, tmp_bits;
1334 u32 tmp_used;
1335 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001336 struct inode *alloc_inode = ac->ac_inode;
1337 struct buffer_head *group_bh = NULL;
1338 struct buffer_head *prev_group_bh = NULL;
1339 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1340 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1341 struct ocfs2_group_desc *bg;
1342
1343 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001344 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1345 bits_wanted, chain,
1346 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001347
Joel Becker68f64d42008-11-13 14:49:14 -08001348 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1349 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1350 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 if (status < 0) {
1352 mlog_errno(status);
1353 goto bail;
1354 }
1355 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001356
1357 status = -ENOSPC;
1358 /* for now, the chain search is a bit simplistic. We just use
1359 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001360 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1361 bits_wanted, min_bits,
1362 ac->ac_max_block, bit_off,
1363 &tmp_bits)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001364 if (!bg->bg_next_group)
1365 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001366
1367 brelse(prev_group_bh);
1368 prev_group_bh = NULL;
1369
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 next_group = le64_to_cpu(bg->bg_next_group);
1371 prev_group_bh = group_bh;
1372 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001373 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1374 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 if (status < 0) {
1376 mlog_errno(status);
1377 goto bail;
1378 }
1379 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001380 }
1381 if (status < 0) {
1382 if (status != -ENOSPC)
1383 mlog_errno(status);
1384 goto bail;
1385 }
1386
Mark Fashehb06970532006-03-03 10:24:33 -08001387 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001388 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001389
1390 *num_bits = tmp_bits;
1391
1392 BUG_ON(*num_bits == 0);
1393
1394 /*
1395 * Keep track of previous block descriptor read. When
1396 * we find a target, if we have read more than X
1397 * number of descriptors, and the target is reasonably
1398 * empty, relink him to top of his chain.
1399 *
1400 * We've read 0 extra blocks and only send one more to
1401 * the transaction, yet the next guy to search has a
1402 * much easier time.
1403 *
1404 * Do this *after* figuring out how many bits we're taking out
1405 * of our target group.
1406 */
1407 if (ac->ac_allow_chain_relink &&
1408 (prev_group_bh) &&
1409 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1410 status = ocfs2_relink_block_group(handle, alloc_inode,
1411 ac->ac_bh, group_bh,
1412 prev_group_bh, chain);
1413 if (status < 0) {
1414 mlog_errno(status);
1415 goto bail;
1416 }
1417 }
1418
1419 /* Ok, claim our bits now: set the info on dinode, chainlist
1420 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001421 status = ocfs2_journal_access_di(handle,
1422 alloc_inode,
1423 ac->ac_bh,
1424 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001425 if (status < 0) {
1426 mlog_errno(status);
1427 goto bail;
1428 }
1429
1430 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1431 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1432 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1433
1434 status = ocfs2_journal_dirty(handle,
1435 ac->ac_bh);
1436 if (status < 0) {
1437 mlog_errno(status);
1438 goto bail;
1439 }
1440
1441 status = ocfs2_block_group_set_bits(handle,
1442 alloc_inode,
1443 bg,
1444 group_bh,
1445 *bit_off,
1446 *num_bits);
1447 if (status < 0) {
1448 mlog_errno(status);
1449 goto bail;
1450 }
1451
Mark Fashehb06970532006-03-03 10:24:33 -08001452 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001453 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001454
1455 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001456 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001457bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001458 brelse(group_bh);
1459 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460
1461 mlog_exit(status);
1462 return status;
1463}
1464
1465/* will give out up to bits_wanted contiguous bits. */
1466static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1467 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001468 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 u32 bits_wanted,
1470 u32 min_bits,
1471 u16 *bit_off,
1472 unsigned int *num_bits,
1473 u64 *bg_blkno)
1474{
1475 int status;
1476 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001477 u16 bits_left = 0;
1478 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 struct ocfs2_chain_list *cl;
1480 struct ocfs2_dinode *fe;
1481
1482 mlog_entry_void();
1483
1484 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1485 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1486 BUG_ON(!ac->ac_bh);
1487
1488 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001489
1490 /* The bh was validated by the inode read during
1491 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1492 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1493
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1495 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001496 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1497 "bits but only %u total.",
1498 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 le32_to_cpu(fe->id1.bitmap1.i_used),
1500 le32_to_cpu(fe->id1.bitmap1.i_total));
1501 status = -EIO;
1502 goto bail;
1503 }
1504
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001505 if (hint_blkno) {
1506 /* Attempt to short-circuit the usual search mechanism
1507 * by jumping straight to the most recently used
1508 * allocation group. This helps us mantain some
1509 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001510 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1511 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001512 hint_blkno, &bits_left);
1513 if (!status) {
1514 /* Be careful to update *bg_blkno here as the
1515 * caller is expecting it to be filled in, and
1516 * ocfs2_search_one_group() won't do that for
1517 * us. */
1518 *bg_blkno = hint_blkno;
1519 goto set_hint;
1520 }
1521 if (status < 0 && status != -ENOSPC) {
1522 mlog_errno(status);
1523 goto bail;
1524 }
1525 }
1526
Mark Fashehccd979b2005-12-15 14:31:24 -08001527 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1528
1529 victim = ocfs2_find_victim_chain(cl);
1530 ac->ac_chain = victim;
1531 ac->ac_allow_chain_relink = 1;
1532
Mark Fashehda5cbf22006-10-06 18:34:35 -07001533 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001534 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001535 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001536 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 if (status < 0 && status != -ENOSPC) {
1538 mlog_errno(status);
1539 goto bail;
1540 }
1541
1542 mlog(0, "Search of victim chain %u came up with nothing, "
1543 "trying all chains now.\n", victim);
1544
1545 /* If we didn't pick a good victim, then just default to
1546 * searching each chain in order. Don't allow chain relinking
1547 * because we only calculate enough journal credits for one
1548 * relink per alloc. */
1549 ac->ac_allow_chain_relink = 0;
1550 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1551 if (i == victim)
1552 continue;
1553 if (!cl->cl_recs[i].c_free)
1554 continue;
1555
1556 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001557 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001558 bit_off, num_bits, bg_blkno,
1559 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001560 if (!status)
1561 break;
1562 if (status < 0 && status != -ENOSPC) {
1563 mlog_errno(status);
1564 goto bail;
1565 }
1566 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001567
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001568set_hint:
1569 if (status != -ENOSPC) {
1570 /* If the next search of this group is not likely to
1571 * yield a suitable extent, then we reset the last
1572 * group hint so as to not waste a disk read */
1573 if (bits_left < min_bits)
1574 ac->ac_last_group = 0;
1575 else
1576 ac->ac_last_group = *bg_blkno;
1577 }
1578
1579bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001580 mlog_exit(status);
1581 return status;
1582}
1583
1584int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001585 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001586 struct ocfs2_alloc_context *ac,
1587 u32 bits_wanted,
1588 u16 *suballoc_bit_start,
1589 unsigned int *num_bits,
1590 u64 *blkno_start)
1591{
1592 int status;
1593 u64 bg_blkno;
1594
1595 BUG_ON(!ac);
1596 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1597 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001598
1599 status = ocfs2_claim_suballoc_bits(osb,
1600 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001601 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001602 bits_wanted,
1603 1,
1604 suballoc_bit_start,
1605 num_bits,
1606 &bg_blkno);
1607 if (status < 0) {
1608 mlog_errno(status);
1609 goto bail;
1610 }
1611 atomic_inc(&osb->alloc_stats.bg_allocs);
1612
1613 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1614 ac->ac_bits_given += (*num_bits);
1615 status = 0;
1616bail:
1617 mlog_exit(status);
1618 return status;
1619}
1620
Tao Ma13821152009-02-25 00:53:23 +08001621static void ocfs2_init_inode_ac_group(struct inode *dir,
1622 struct buffer_head *parent_fe_bh,
1623 struct ocfs2_alloc_context *ac)
1624{
1625 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1626 /*
1627 * Try to allocate inodes from some specific group.
1628 *
1629 * If the parent dir has recorded the last group used in allocation,
1630 * cool, use it. Otherwise if we try to allocate new inode from the
1631 * same slot the parent dir belongs to, use the same chunk.
1632 *
1633 * We are very careful here to avoid the mistake of setting
1634 * ac_last_group to a group descriptor from a different (unlocked) slot.
1635 */
1636 if (OCFS2_I(dir)->ip_last_used_group &&
1637 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1638 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1639 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1640 ac->ac_last_group = ocfs2_which_suballoc_group(
1641 le64_to_cpu(fe->i_blkno),
1642 le16_to_cpu(fe->i_suballoc_bit));
1643}
1644
1645static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1646 struct ocfs2_alloc_context *ac)
1647{
1648 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1649 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1650}
1651
Mark Fashehccd979b2005-12-15 14:31:24 -08001652int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001653 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001654 struct inode *dir,
1655 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001656 struct ocfs2_alloc_context *ac,
1657 u16 *suballoc_bit,
1658 u64 *fe_blkno)
1659{
1660 int status;
1661 unsigned int num_bits;
1662 u64 bg_blkno;
1663
1664 mlog_entry_void();
1665
1666 BUG_ON(!ac);
1667 BUG_ON(ac->ac_bits_given != 0);
1668 BUG_ON(ac->ac_bits_wanted != 1);
1669 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001670
Tao Ma13821152009-02-25 00:53:23 +08001671 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1672
Mark Fashehccd979b2005-12-15 14:31:24 -08001673 status = ocfs2_claim_suballoc_bits(osb,
1674 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001675 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001676 1,
1677 1,
1678 suballoc_bit,
1679 &num_bits,
1680 &bg_blkno);
1681 if (status < 0) {
1682 mlog_errno(status);
1683 goto bail;
1684 }
1685 atomic_inc(&osb->alloc_stats.bg_allocs);
1686
1687 BUG_ON(num_bits != 1);
1688
1689 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1690 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08001691 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001692 status = 0;
1693bail:
1694 mlog_exit(status);
1695 return status;
1696}
1697
1698/* translate a group desc. blkno and it's bitmap offset into
1699 * disk cluster offset. */
1700static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1701 u64 bg_blkno,
1702 u16 bg_bit_off)
1703{
1704 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1705 u32 cluster = 0;
1706
1707 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1708
1709 if (bg_blkno != osb->first_cluster_group_blkno)
1710 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1711 cluster += (u32) bg_bit_off;
1712 return cluster;
1713}
1714
1715/* given a cluster offset, calculate which block group it belongs to
1716 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08001717u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08001718{
1719 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1720 u32 group_no;
1721
1722 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1723
1724 group_no = cluster / osb->bitmap_cpg;
1725 if (!group_no)
1726 return osb->first_cluster_group_blkno;
1727 return ocfs2_clusters_to_blocks(inode->i_sb,
1728 group_no * osb->bitmap_cpg);
1729}
1730
1731/* given the block number of a cluster start, calculate which cluster
1732 * group and descriptor bitmap offset that corresponds to. */
1733static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1734 u64 data_blkno,
1735 u64 *bg_blkno,
1736 u16 *bg_bit_off)
1737{
1738 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1739 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1740
1741 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1742
1743 *bg_blkno = ocfs2_which_cluster_group(inode,
1744 data_cluster);
1745
1746 if (*bg_blkno == osb->first_cluster_group_blkno)
1747 *bg_bit_off = (u16) data_cluster;
1748 else
1749 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1750 data_blkno - *bg_blkno);
1751}
1752
1753/*
1754 * min_bits - minimum contiguous chunk from this total allocation we
1755 * can handle. set to what we asked for originally for a full
1756 * contig. allocation, set to '1' to indicate we can deal with extents
1757 * of any size.
1758 */
Mark Fasheh415cb802007-09-16 20:10:16 -07001759int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1760 handle_t *handle,
1761 struct ocfs2_alloc_context *ac,
1762 u32 min_clusters,
1763 u32 max_clusters,
1764 u32 *cluster_start,
1765 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08001766{
1767 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07001768 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001769 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001770 u16 bg_bit_off;
1771
1772 mlog_entry_void();
1773
Mark Fashehccd979b2005-12-15 14:31:24 -08001774 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1775
1776 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1777 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001778
1779 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1780 status = ocfs2_claim_local_alloc_bits(osb,
1781 handle,
1782 ac,
1783 bits_wanted,
1784 cluster_start,
1785 num_clusters);
1786 if (!status)
1787 atomic_inc(&osb->alloc_stats.local_data);
1788 } else {
1789 if (min_clusters > (osb->bitmap_cpg - 1)) {
1790 /* The only paths asking for contiguousness
1791 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001792 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1793 "group bitmap size %u!\n", min_clusters,
1794 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08001795 status = -ENOSPC;
1796 goto bail;
1797 }
1798 /* clamp the current request down to a realistic size. */
1799 if (bits_wanted > (osb->bitmap_cpg - 1))
1800 bits_wanted = osb->bitmap_cpg - 1;
1801
1802 status = ocfs2_claim_suballoc_bits(osb,
1803 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001804 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001805 bits_wanted,
1806 min_clusters,
1807 &bg_bit_off,
1808 num_clusters,
1809 &bg_blkno);
1810 if (!status) {
1811 *cluster_start =
1812 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1813 bg_blkno,
1814 bg_bit_off);
1815 atomic_inc(&osb->alloc_stats.bitmap_data);
1816 }
1817 }
1818 if (status < 0) {
1819 if (status != -ENOSPC)
1820 mlog_errno(status);
1821 goto bail;
1822 }
1823
1824 ac->ac_bits_given += *num_clusters;
1825
1826bail:
1827 mlog_exit(status);
1828 return status;
1829}
1830
Mark Fasheh415cb802007-09-16 20:10:16 -07001831int ocfs2_claim_clusters(struct ocfs2_super *osb,
1832 handle_t *handle,
1833 struct ocfs2_alloc_context *ac,
1834 u32 min_clusters,
1835 u32 *cluster_start,
1836 u32 *num_clusters)
1837{
1838 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1839
1840 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1841 bits_wanted, cluster_start, num_clusters);
1842}
1843
Mark Fasheh1fabe142006-10-09 18:11:45 -07001844static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001845 struct inode *alloc_inode,
1846 struct ocfs2_group_desc *bg,
1847 struct buffer_head *group_bh,
1848 unsigned int bit_off,
1849 unsigned int num_bits)
1850{
1851 int status;
1852 unsigned int tmp;
1853 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1854 struct ocfs2_group_desc *undo_bg = NULL;
1855
1856 mlog_entry_void();
1857
Joel Becker42035302008-11-13 14:49:15 -08001858 /* The caller got this descriptor from
1859 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1860 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001861
1862 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1863
1864 if (ocfs2_is_cluster_bitmap(alloc_inode))
1865 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1866
Joel Becker13723d02008-10-17 19:25:01 -07001867 status = ocfs2_journal_access_gd(handle, alloc_inode, group_bh,
1868 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 if (status < 0) {
1870 mlog_errno(status);
1871 goto bail;
1872 }
1873
1874 if (ocfs2_is_cluster_bitmap(alloc_inode))
1875 undo_bg = (struct ocfs2_group_desc *) bh2jh(group_bh)->b_committed_data;
1876
1877 tmp = num_bits;
1878 while(tmp--) {
1879 ocfs2_clear_bit((bit_off + tmp),
1880 (unsigned long *) bg->bg_bitmap);
1881 if (ocfs2_is_cluster_bitmap(alloc_inode))
1882 ocfs2_set_bit(bit_off + tmp,
1883 (unsigned long *) undo_bg->bg_bitmap);
1884 }
1885 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
1886
1887 status = ocfs2_journal_dirty(handle, group_bh);
1888 if (status < 0)
1889 mlog_errno(status);
1890bail:
1891 return status;
1892}
1893
1894/*
1895 * expects the suballoc inode to already be locked.
1896 */
Mark Fasheh2b604352007-06-22 15:45:27 -07001897int ocfs2_free_suballoc_bits(handle_t *handle,
1898 struct inode *alloc_inode,
1899 struct buffer_head *alloc_bh,
1900 unsigned int start_bit,
1901 u64 bg_blkno,
1902 unsigned int count)
Mark Fashehccd979b2005-12-15 14:31:24 -08001903{
1904 int status = 0;
1905 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08001906 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
1907 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
1908 struct buffer_head *group_bh = NULL;
1909 struct ocfs2_group_desc *group;
1910
1911 mlog_entry_void();
1912
Joel Becker10995aa2008-11-13 14:49:12 -08001913 /* The alloc_bh comes from ocfs2_free_dinode() or
1914 * ocfs2_free_clusters(). The callers have all locked the
1915 * allocator and gotten alloc_bh from the lock call. This
1916 * validates the dinode buffer. Any corruption that has happended
1917 * is a code bug. */
1918 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08001919 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
1920
Mark Fashehb06970532006-03-03 10:24:33 -08001921 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
1922 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
1923 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08001924
Joel Becker68f64d42008-11-13 14:49:14 -08001925 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
1926 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001927 if (status < 0) {
1928 mlog_errno(status);
1929 goto bail;
1930 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001931 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08001932
Mark Fashehccd979b2005-12-15 14:31:24 -08001933 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
1934
1935 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
1936 group, group_bh,
1937 start_bit, count);
1938 if (status < 0) {
1939 mlog_errno(status);
1940 goto bail;
1941 }
1942
Joel Becker13723d02008-10-17 19:25:01 -07001943 status = ocfs2_journal_access_di(handle, alloc_inode, alloc_bh,
1944 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001945 if (status < 0) {
1946 mlog_errno(status);
1947 goto bail;
1948 }
1949
1950 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
1951 count);
1952 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1953 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
1954
1955 status = ocfs2_journal_dirty(handle, alloc_bh);
1956 if (status < 0) {
1957 mlog_errno(status);
1958 goto bail;
1959 }
1960
1961bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001962 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001963
1964 mlog_exit(status);
1965 return status;
1966}
1967
Mark Fasheh1fabe142006-10-09 18:11:45 -07001968int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001969 struct inode *inode_alloc_inode,
1970 struct buffer_head *inode_alloc_bh,
1971 struct ocfs2_dinode *di)
1972{
1973 u64 blk = le64_to_cpu(di->i_blkno);
1974 u16 bit = le16_to_cpu(di->i_suballoc_bit);
1975 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
1976
1977 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
1978 inode_alloc_bh, bit, bg_blkno, 1);
1979}
1980
Mark Fasheh1fabe142006-10-09 18:11:45 -07001981int ocfs2_free_clusters(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001982 struct inode *bitmap_inode,
1983 struct buffer_head *bitmap_bh,
1984 u64 start_blk,
1985 unsigned int num_clusters)
1986{
1987 int status;
1988 u16 bg_start_bit;
1989 u64 bg_blkno;
1990 struct ocfs2_dinode *fe;
1991
1992 /* You can't ever have a contiguous set of clusters
1993 * bigger than a block group bitmap so we never have to worry
1994 * about looping on them. */
1995
1996 mlog_entry_void();
1997
1998 /* This is expensive. We can safely remove once this stuff has
1999 * gotten tested really well. */
2000 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2001
2002 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2003
2004 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2005 &bg_start_bit);
2006
Mark Fashehb06970532006-03-03 10:24:33 -08002007 mlog(0, "want to free %u clusters starting at block %llu\n",
2008 num_clusters, (unsigned long long)start_blk);
2009 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2010 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002011
2012 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2013 bg_start_bit, bg_blkno,
2014 num_clusters);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002015 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002016 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002017 goto out;
2018 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002019
Mark Fasheh9c7af402008-07-28 18:02:53 -07002020 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2021 num_clusters);
2022
2023out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 mlog_exit(status);
2025 return status;
2026}
2027
2028static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2029{
2030 printk("Block Group:\n");
2031 printk("bg_signature: %s\n", bg->bg_signature);
2032 printk("bg_size: %u\n", bg->bg_size);
2033 printk("bg_bits: %u\n", bg->bg_bits);
2034 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2035 printk("bg_chain: %u\n", bg->bg_chain);
2036 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002037 printk("bg_next_group: %llu\n",
2038 (unsigned long long)bg->bg_next_group);
2039 printk("bg_parent_dinode: %llu\n",
2040 (unsigned long long)bg->bg_parent_dinode);
2041 printk("bg_blkno: %llu\n",
2042 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002043}
2044
2045static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2046{
2047 int i;
2048
Mark Fashehb06970532006-03-03 10:24:33 -08002049 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002050 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002051 printk("i_size: %llu\n",
2052 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002053 printk("i_clusters: %u\n", fe->i_clusters);
2054 printk("i_generation: %u\n",
2055 le32_to_cpu(fe->i_generation));
2056 printk("id1.bitmap1.i_used: %u\n",
2057 le32_to_cpu(fe->id1.bitmap1.i_used));
2058 printk("id1.bitmap1.i_total: %u\n",
2059 le32_to_cpu(fe->id1.bitmap1.i_total));
2060 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2061 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2062 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2063 printk("id2.i_chain.cl_next_free_rec: %u\n",
2064 fe->id2.i_chain.cl_next_free_rec);
2065 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2066 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2067 fe->id2.i_chain.cl_recs[i].c_free);
2068 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2069 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002070 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2071 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002072 }
2073}
Tao Mae7d4cb62008-08-18 17:38:44 +08002074
2075/*
2076 * For a given allocation, determine which allocators will need to be
2077 * accessed, and lock them, reserving the appropriate number of bits.
2078 *
2079 * Sparse file systems call this from ocfs2_write_begin_nolock()
2080 * and ocfs2_allocate_unwritten_extents().
2081 *
2082 * File systems which don't support holes call this from
2083 * ocfs2_extend_allocation().
2084 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002085int ocfs2_lock_allocators(struct inode *inode,
2086 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002087 u32 clusters_to_add, u32 extents_to_split,
2088 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002089 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002090{
2091 int ret = 0, num_free_extents;
2092 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2093 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2094
2095 *meta_ac = NULL;
2096 if (data_ac)
2097 *data_ac = NULL;
2098
2099 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2100
Joel Beckerf99b9b72008-08-20 19:36:33 -07002101 num_free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002102 if (num_free_extents < 0) {
2103 ret = num_free_extents;
2104 mlog_errno(ret);
2105 goto out;
2106 }
2107
2108 /*
2109 * Sparse allocation file systems need to be more conservative
2110 * with reserving room for expansion - the actual allocation
2111 * happens while we've got a journal handle open so re-taking
2112 * a cluster lock (because we ran out of room for another
2113 * extent) will violate ordering rules.
2114 *
2115 * Most of the time we'll only be seeing this 1 cluster at a time
2116 * anyway.
2117 *
2118 * Always lock for any unwritten extents - we might want to
2119 * add blocks during a split.
2120 */
2121 if (!num_free_extents ||
2122 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002123 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002124 if (ret < 0) {
2125 if (ret != -ENOSPC)
2126 mlog_errno(ret);
2127 goto out;
2128 }
2129 }
2130
2131 if (clusters_to_add == 0)
2132 goto out;
2133
2134 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2135 if (ret < 0) {
2136 if (ret != -ENOSPC)
2137 mlog_errno(ret);
2138 goto out;
2139 }
2140
2141out:
2142 if (ret) {
2143 if (*meta_ac) {
2144 ocfs2_free_alloc_context(*meta_ac);
2145 *meta_ac = NULL;
2146 }
2147
2148 /*
2149 * We cannot have an error and a non null *data_ac.
2150 */
2151 }
2152
2153 return ret;
2154}