blob: c3c60bc3e0729a5fb55f99254dc5babd150ae6ff [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
Tao Ma60ca81e2009-02-25 00:53:24 +080051#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080053
Tiger Yangb89c5422010-01-25 14:11:06 +080054#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080055
Mark Fashehccd979b2005-12-15 14:31:24 -080056static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
57static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
58static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070059static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080060 struct inode *alloc_inode,
61 struct buffer_head *bg_bh,
62 u64 group_blkno,
63 u16 my_chain,
64 struct ocfs2_chain_list *cl);
65static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
66 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070067 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080068 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080069 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080070 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080071
Mark Fashehccd979b2005-12-15 14:31:24 -080072static int ocfs2_cluster_group_search(struct inode *inode,
73 struct buffer_head *group_bh,
74 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070075 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080076 u16 *bit_off, u16 *bits_found);
77static int ocfs2_block_group_search(struct inode *inode,
78 struct buffer_head *group_bh,
79 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070080 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -080081 u16 *bit_off, u16 *bits_found);
Mark Fashehccd979b2005-12-15 14:31:24 -080082static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
83 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070084 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080085 u32 bits_wanted,
86 u32 min_bits,
87 u16 *bit_off,
88 unsigned int *num_bits,
89 u64 *bg_blkno);
90static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
91 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070092static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080093 struct inode *alloc_inode,
94 struct ocfs2_group_desc *bg,
95 struct buffer_head *group_bh,
96 unsigned int bit_off,
97 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -070098static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080099 struct inode *alloc_inode,
100 struct ocfs2_group_desc *bg,
101 struct buffer_head *group_bh,
102 unsigned int bit_off,
103 unsigned int num_bits);
104
Mark Fasheh1fabe142006-10-09 18:11:45 -0700105static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800106 struct inode *alloc_inode,
107 struct buffer_head *fe_bh,
108 struct buffer_head *bg_bh,
109 struct buffer_head *prev_bg_bh,
110 u16 chain);
111static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
112 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
114 u64 bg_blkno,
115 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800116static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 u64 data_blkno,
118 u64 *bg_blkno,
119 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700120static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
121 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800122 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700123 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124
Mark Fasheh9c7af402008-07-28 18:02:53 -0700125void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800126{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700127 struct inode *inode = ac->ac_inode;
128
129 if (inode) {
130 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700131 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700132
133 mutex_unlock(&inode->i_mutex);
134
135 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800136 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700137 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700138 brelse(ac->ac_bh);
139 ac->ac_bh = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800140}
141
142void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
143{
144 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800145 kfree(ac);
146}
147
148static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
149{
150 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
151}
152
Joel Becker57e3e792008-11-13 14:49:13 -0800153#define do_error(fmt, ...) \
154 do{ \
155 if (clean_error) \
156 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
157 else \
158 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
159 } while (0)
160
Joel Becker970e4932008-11-13 14:49:19 -0800161static int ocfs2_validate_gd_self(struct super_block *sb,
162 struct buffer_head *bh,
163 int clean_error)
164{
165 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
166
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700167 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800168 do_error("Group descriptor #%llu has bad signature %.*s",
169 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800170 gd->bg_signature);
171 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700172 }
173
Joel Becker68f64d42008-11-13 14:49:14 -0800174 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
175 do_error("Group descriptor #%llu has an invalid bg_blkno "
176 "of %llu",
177 (unsigned long long)bh->b_blocknr,
178 (unsigned long long)le64_to_cpu(gd->bg_blkno));
179 return -EINVAL;
180 }
181
182 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
183 do_error("Group descriptor #%llu has an invalid "
184 "fs_generation of #%u",
185 (unsigned long long)bh->b_blocknr,
186 le32_to_cpu(gd->bg_generation));
187 return -EINVAL;
188 }
189
Joel Becker970e4932008-11-13 14:49:19 -0800190 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
191 do_error("Group descriptor #%llu has bit count %u but "
192 "claims that %u are free",
193 (unsigned long long)bh->b_blocknr,
194 le16_to_cpu(gd->bg_bits),
195 le16_to_cpu(gd->bg_free_bits_count));
196 return -EINVAL;
197 }
198
199 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
200 do_error("Group descriptor #%llu has bit count %u but "
201 "max bitmap bits of %u",
202 (unsigned long long)bh->b_blocknr,
203 le16_to_cpu(gd->bg_bits),
204 8 * le16_to_cpu(gd->bg_size));
205 return -EINVAL;
206 }
207
208 return 0;
209}
210
211static int ocfs2_validate_gd_parent(struct super_block *sb,
212 struct ocfs2_dinode *di,
213 struct buffer_head *bh,
214 int clean_error)
215{
216 unsigned int max_bits;
217 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
218
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700219 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800220 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800221 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800222 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800223 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
224 (unsigned long long)le64_to_cpu(di->i_blkno));
225 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700226 }
227
228 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
229 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800230 do_error("Group descriptor #%llu has bit count of %u",
231 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800232 le16_to_cpu(gd->bg_bits));
233 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700234 }
235
236 if (le16_to_cpu(gd->bg_chain) >=
237 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800238 do_error("Group descriptor #%llu has bad chain %u",
239 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800240 le16_to_cpu(gd->bg_chain));
241 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700242 }
243
Joel Becker970e4932008-11-13 14:49:19 -0800244 return 0;
245}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246
Joel Becker57e3e792008-11-13 14:49:13 -0800247#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700248
Joel Becker970e4932008-11-13 14:49:19 -0800249/*
250 * This version only prints errors. It does not fail the filesystem, and
251 * exists only for resize.
252 */
253int ocfs2_check_group_descriptor(struct super_block *sb,
254 struct ocfs2_dinode *di,
255 struct buffer_head *bh)
256{
257 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700258 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800259
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700260 BUG_ON(!buffer_uptodate(bh));
261
262 /*
263 * If the ecc fails, we return the error but otherwise
264 * leave the filesystem running. We know any error is
265 * local to this block.
266 */
267 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700268 if (rc) {
269 mlog(ML_ERROR,
270 "Checksum failed for group descriptor %llu\n",
271 (unsigned long long)bh->b_blocknr);
272 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700273 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800274 if (!rc)
275 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
276
277 return rc;
278}
279
280static int ocfs2_validate_group_descriptor(struct super_block *sb,
281 struct buffer_head *bh)
282{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700283 int rc;
284 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
285
Joel Becker970e4932008-11-13 14:49:19 -0800286 mlog(0, "Validating group descriptor %llu\n",
287 (unsigned long long)bh->b_blocknr);
288
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700289 BUG_ON(!buffer_uptodate(bh));
290
291 /*
292 * If the ecc fails, we return the error but otherwise
293 * leave the filesystem running. We know any error is
294 * local to this block.
295 */
296 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
297 if (rc)
298 return rc;
299
300 /*
301 * Errors after here are fatal.
302 */
303
Joel Becker970e4932008-11-13 14:49:19 -0800304 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700305}
306
Joel Becker68f64d42008-11-13 14:49:14 -0800307int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
308 u64 gd_blkno, struct buffer_head **bh)
309{
310 int rc;
311 struct buffer_head *tmp = *bh;
312
Joel Becker8cb471e2009-02-10 20:00:41 -0800313 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800314 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800315 if (rc)
316 goto out;
317
Joel Becker970e4932008-11-13 14:49:19 -0800318 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800319 if (rc) {
320 brelse(tmp);
321 goto out;
322 }
323
324 /* If ocfs2_read_block() got us a new bh, pass it up. */
325 if (!*bh)
326 *bh = tmp;
327
328out:
329 return rc;
330}
331
Mark Fasheh1fabe142006-10-09 18:11:45 -0700332static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800333 struct inode *alloc_inode,
334 struct buffer_head *bg_bh,
335 u64 group_blkno,
336 u16 my_chain,
337 struct ocfs2_chain_list *cl)
338{
339 int status = 0;
340 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
341 struct super_block * sb = alloc_inode->i_sb;
342
343 mlog_entry_void();
344
345 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800346 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
347 "b_blocknr (%llu)",
348 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800349 (unsigned long long) bg_bh->b_blocknr);
350 status = -EIO;
351 goto bail;
352 }
353
Joel Becker13723d02008-10-17 19:25:01 -0700354 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800355 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700356 bg_bh,
357 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800358 if (status < 0) {
359 mlog_errno(status);
360 goto bail;
361 }
362
363 memset(bg, 0, sb->s_blocksize);
364 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
365 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
366 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
367 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
368 bg->bg_chain = cpu_to_le16(my_chain);
369 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
370 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
371 bg->bg_blkno = cpu_to_le64(group_blkno);
372 /* set the 1st bit in the bitmap to account for the descriptor block */
373 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
374 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
375
376 status = ocfs2_journal_dirty(handle, bg_bh);
377 if (status < 0)
378 mlog_errno(status);
379
380 /* There is no need to zero out or otherwise initialize the
381 * other blocks in a group - All valid FS metadata in a block
382 * group stores the superblock fs_generation value at
383 * allocation time. */
384
385bail:
386 mlog_exit(status);
387 return status;
388}
389
390static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
391{
392 u16 curr, best;
393
394 best = curr = 0;
395 while (curr < le16_to_cpu(cl->cl_count)) {
396 if (le32_to_cpu(cl->cl_recs[best].c_total) >
397 le32_to_cpu(cl->cl_recs[curr].c_total))
398 best = curr;
399 curr++;
400 }
401 return best;
402}
403
404/*
405 * We expect the block group allocator to already be locked.
406 */
407static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
408 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700409 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800410 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800411 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800412 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800413{
414 int status, credits;
415 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
416 struct ocfs2_chain_list *cl;
417 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700418 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800419 u32 bit_off, num_bits;
420 u16 alloc_rec;
421 u64 bg_blkno;
422 struct buffer_head *bg_bh = NULL;
423 struct ocfs2_group_desc *bg;
424
425 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
426
427 mlog_entry_void();
428
Mark Fashehccd979b2005-12-15 14:31:24 -0800429 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700430 status = ocfs2_reserve_clusters_with_limit(osb,
431 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800432 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800433 if (status < 0) {
434 if (status != -ENOSPC)
435 mlog_errno(status);
436 goto bail;
437 }
438
439 credits = ocfs2_calc_group_alloc_credits(osb->sb,
440 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700441 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800442 if (IS_ERR(handle)) {
443 status = PTR_ERR(handle);
444 handle = NULL;
445 mlog_errno(status);
446 goto bail;
447 }
448
Tao Mafeb473a2009-02-25 00:53:25 +0800449 if (last_alloc_group && *last_alloc_group != 0) {
450 mlog(0, "use old allocation group %llu for block group alloc\n",
451 (unsigned long long)*last_alloc_group);
452 ac->ac_last_group = *last_alloc_group;
453 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800454 status = ocfs2_claim_clusters(osb,
455 handle,
456 ac,
457 le16_to_cpu(cl->cl_cpg),
458 &bit_off,
459 &num_bits);
460 if (status < 0) {
461 if (status != -ENOSPC)
462 mlog_errno(status);
463 goto bail;
464 }
465
466 alloc_rec = ocfs2_find_smallest_chain(cl);
467
468 /* setup the group */
469 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Mark Fashehb06970532006-03-03 10:24:33 -0800470 mlog(0, "new descriptor, record %u, at block %llu\n",
471 alloc_rec, (unsigned long long)bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800472
473 bg_bh = sb_getblk(osb->sb, bg_blkno);
474 if (!bg_bh) {
475 status = -EIO;
476 mlog_errno(status);
477 goto bail;
478 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800479 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800480
481 status = ocfs2_block_group_fill(handle,
482 alloc_inode,
483 bg_bh,
484 bg_blkno,
485 alloc_rec,
486 cl);
487 if (status < 0) {
488 mlog_errno(status);
489 goto bail;
490 }
491
492 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
493
Joel Becker0cf2f762009-02-12 16:41:25 -0800494 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700495 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800496 if (status < 0) {
497 mlog_errno(status);
498 goto bail;
499 }
500
501 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
502 le16_to_cpu(bg->bg_free_bits_count));
503 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
504 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg_blkno);
505 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
506 le16_add_cpu(&cl->cl_next_free_rec, 1);
507
508 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
509 le16_to_cpu(bg->bg_free_bits_count));
510 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
511 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
512
513 status = ocfs2_journal_dirty(handle, bh);
514 if (status < 0) {
515 mlog_errno(status);
516 goto bail;
517 }
518
519 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
520 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
521 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
522 le32_to_cpu(fe->i_clusters)));
523 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
524 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700525 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800526
527 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800528
529 /* save the new last alloc group so that the caller can cache it. */
530 if (last_alloc_group)
531 *last_alloc_group = ac->ac_last_group;
532
Mark Fashehccd979b2005-12-15 14:31:24 -0800533bail:
534 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700535 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800536
537 if (ac)
538 ocfs2_free_alloc_context(ac);
539
Mark Fasheha81cb882008-10-07 14:25:16 -0700540 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800541
542 mlog_exit(status);
543 return status;
544}
545
546static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700547 struct ocfs2_alloc_context *ac,
548 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800549 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800550 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800551 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800552{
553 int status;
554 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700555 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800556 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800557 struct ocfs2_dinode *fe;
558 u32 free_bits;
559
560 mlog_entry_void();
561
Mark Fashehda5cbf22006-10-06 18:34:35 -0700562 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
563 if (!alloc_inode) {
564 mlog_errno(-EINVAL);
565 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800566 }
567
Mark Fashehda5cbf22006-10-06 18:34:35 -0700568 mutex_lock(&alloc_inode->i_mutex);
569
Mark Fashehe63aecb62007-10-18 15:30:42 -0700570 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700571 if (status < 0) {
572 mutex_unlock(&alloc_inode->i_mutex);
573 iput(alloc_inode);
574
575 mlog_errno(status);
576 return status;
577 }
578
579 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800580 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700581
Mark Fashehccd979b2005-12-15 14:31:24 -0800582 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800583
584 /* The bh was validated by the inode read inside
585 * ocfs2_inode_lock(). Any corruption is a code bug. */
586 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
587
Mark Fashehccd979b2005-12-15 14:31:24 -0800588 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800589 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
590 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800591 status = -EIO;
592 goto bail;
593 }
594
595 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
596 le32_to_cpu(fe->id1.bitmap1.i_used);
597
598 if (bits_wanted > free_bits) {
599 /* cluster bitmap never grows */
600 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
601 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
602 bits_wanted, free_bits);
603 status = -ENOSPC;
604 goto bail;
605 }
606
Tao Ma60ca81e2009-02-25 00:53:24 +0800607 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800608 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
609 "and we don't alloc a new group for it.\n",
610 slot, bits_wanted, free_bits);
611 status = -ENOSPC;
612 goto bail;
613 }
614
Joel Becker1187c962008-09-03 20:03:39 -0700615 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800616 ac->ac_max_block,
617 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800618 if (status < 0) {
619 if (status != -ENOSPC)
620 mlog_errno(status);
621 goto bail;
622 }
623 atomic_inc(&osb->alloc_stats.bg_extends);
624
625 /* You should never ask for this much metadata */
626 BUG_ON(bits_wanted >
627 (le32_to_cpu(fe->id1.bitmap1.i_total)
628 - le32_to_cpu(fe->id1.bitmap1.i_used)));
629 }
630
631 get_bh(bh);
632 ac->ac_bh = bh;
633bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700634 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800635
636 mlog_exit(status);
637 return status;
638}
639
Tiger Yangb89c5422010-01-25 14:11:06 +0800640static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
641{
642 spin_lock(&osb->osb_lock);
643 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
644 spin_unlock(&osb->osb_lock);
645 atomic_set(&osb->s_num_inodes_stolen, 0);
646}
647
648static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
649{
650 spin_lock(&osb->osb_lock);
651 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
652 spin_unlock(&osb->osb_lock);
653 atomic_set(&osb->s_num_meta_stolen, 0);
654}
655
656void ocfs2_init_steal_slots(struct ocfs2_super *osb)
657{
658 ocfs2_init_inode_steal_slot(osb);
659 ocfs2_init_meta_steal_slot(osb);
660}
661
662static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
663{
664 spin_lock(&osb->osb_lock);
665 if (type == INODE_ALLOC_SYSTEM_INODE)
666 osb->s_inode_steal_slot = slot;
667 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
668 osb->s_meta_steal_slot = slot;
669 spin_unlock(&osb->osb_lock);
670}
671
672static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
673{
674 int slot = OCFS2_INVALID_SLOT;
675
676 spin_lock(&osb->osb_lock);
677 if (type == INODE_ALLOC_SYSTEM_INODE)
678 slot = osb->s_inode_steal_slot;
679 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
680 slot = osb->s_meta_steal_slot;
681 spin_unlock(&osb->osb_lock);
682
683 return slot;
684}
685
686static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
687{
688 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
689}
690
691static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
692{
693 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
694}
695
696static int ocfs2_steal_resource(struct ocfs2_super *osb,
697 struct ocfs2_alloc_context *ac,
698 int type)
699{
700 int i, status = -ENOSPC;
701 int slot = __ocfs2_get_steal_slot(osb, type);
702
703 /* Start to steal resource from the first slot after ours. */
704 if (slot == OCFS2_INVALID_SLOT)
705 slot = osb->slot_num + 1;
706
707 for (i = 0; i < osb->max_slots; i++, slot++) {
708 if (slot == osb->max_slots)
709 slot = 0;
710
711 if (slot == osb->slot_num)
712 continue;
713
714 status = ocfs2_reserve_suballoc_bits(osb, ac,
715 type,
716 (u32)slot, NULL,
717 NOT_ALLOC_NEW_GROUP);
718 if (status >= 0) {
719 __ocfs2_set_steal_slot(osb, slot, type);
720 break;
721 }
722
723 ocfs2_free_ac_resource(ac);
724 }
725
726 return status;
727}
728
729static int ocfs2_steal_inode(struct ocfs2_super *osb,
730 struct ocfs2_alloc_context *ac)
731{
732 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
733}
734
735static int ocfs2_steal_meta(struct ocfs2_super *osb,
736 struct ocfs2_alloc_context *ac)
737{
738 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
739}
740
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800741int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
742 int blocks,
743 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800744{
745 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800746 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800747
Robert P. J. Daycd861282006-12-13 00:34:52 -0800748 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 if (!(*ac)) {
750 status = -ENOMEM;
751 mlog_errno(status);
752 goto bail;
753 }
754
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800755 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800756 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800757 (*ac)->ac_group_search = ocfs2_block_group_search;
758
Tiger Yangb89c5422010-01-25 14:11:06 +0800759 if (slot != OCFS2_INVALID_SLOT &&
760 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
761 goto extent_steal;
762
763 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700764 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800765 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800766 (u32)osb->slot_num, NULL,
767 ALLOC_NEW_GROUP);
768
769
770 if (status >= 0) {
771 status = 0;
772 if (slot != OCFS2_INVALID_SLOT)
773 ocfs2_init_meta_steal_slot(osb);
774 goto bail;
775 } else if (status < 0 && status != -ENOSPC) {
776 mlog_errno(status);
777 goto bail;
778 }
779
780 ocfs2_free_ac_resource(*ac);
781
782extent_steal:
783 status = ocfs2_steal_meta(osb, *ac);
784 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 if (status < 0) {
786 if (status != -ENOSPC)
787 mlog_errno(status);
788 goto bail;
789 }
790
791 status = 0;
792bail:
793 if ((status < 0) && *ac) {
794 ocfs2_free_alloc_context(*ac);
795 *ac = NULL;
796 }
797
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 mlog_exit(status);
799 return status;
800}
801
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800802int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
803 struct ocfs2_extent_list *root_el,
804 struct ocfs2_alloc_context **ac)
805{
806 return ocfs2_reserve_new_metadata_blocks(osb,
807 ocfs2_extend_meta_needed(root_el),
808 ac);
809}
810
Mark Fashehccd979b2005-12-15 14:31:24 -0800811int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800812 struct ocfs2_alloc_context **ac)
813{
814 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800815 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +0800816 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -0800817
Robert P. J. Daycd861282006-12-13 00:34:52 -0800818 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800819 if (!(*ac)) {
820 status = -ENOMEM;
821 mlog_errno(status);
822 goto bail;
823 }
824
825 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 (*ac)->ac_which = OCFS2_AC_USE_INODE;
827
Mark Fashehccd979b2005-12-15 14:31:24 -0800828 (*ac)->ac_group_search = ocfs2_block_group_search;
829
Tao Ma4d0ddb22008-03-05 16:11:46 +0800830 /*
Joel Becker1187c962008-09-03 20:03:39 -0700831 * stat(2) can't handle i_ino > 32bits, so we tell the
832 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -0700833 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -0700834 */
Joel Becker12462f12008-09-03 20:03:40 -0700835 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
836 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -0700837
838 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +0800839 * slot is set when we successfully steal inode from other nodes.
840 * It is reset in 3 places:
841 * 1. when we flush the truncate log
842 * 2. when we complete local alloc recovery.
843 * 3. when we successfully allocate from our own slot.
844 * After it is set, we will go on stealing inodes until we find the
845 * need to check our slots to see whether there is some space for us.
846 */
847 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +0800848 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +0800849 goto inode_steal;
850
851 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +0800852 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700853 status = ocfs2_reserve_suballoc_bits(osb, *ac,
854 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800855 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +0800856 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800857 ALLOC_NEW_GROUP |
858 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800859 if (status >= 0) {
860 status = 0;
861
Tao Mafeb473a2009-02-25 00:53:25 +0800862 spin_lock(&osb->osb_lock);
863 osb->osb_inode_alloc_group = alloc_group;
864 spin_unlock(&osb->osb_lock);
865 mlog(0, "after reservation, new allocation group is "
866 "%llu\n", (unsigned long long)alloc_group);
867
Tao Ma4d0ddb22008-03-05 16:11:46 +0800868 /*
869 * Some inodes must be freed by us, so try to allocate
870 * from our own next time.
871 */
872 if (slot != OCFS2_INVALID_SLOT)
873 ocfs2_init_inode_steal_slot(osb);
874 goto bail;
875 } else if (status < 0 && status != -ENOSPC) {
876 mlog_errno(status);
877 goto bail;
878 }
879
880 ocfs2_free_ac_resource(*ac);
881
882inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +0800883 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800884 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -0800885 if (status < 0) {
886 if (status != -ENOSPC)
887 mlog_errno(status);
888 goto bail;
889 }
890
891 status = 0;
892bail:
893 if ((status < 0) && *ac) {
894 ocfs2_free_alloc_context(*ac);
895 *ac = NULL;
896 }
897
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 mlog_exit(status);
899 return status;
900}
901
902/* local alloc code has to do the same thing, so rather than do this
903 * twice.. */
904int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
905 struct ocfs2_alloc_context *ac)
906{
907 int status;
908
Mark Fashehccd979b2005-12-15 14:31:24 -0800909 ac->ac_which = OCFS2_AC_USE_MAIN;
910 ac->ac_group_search = ocfs2_cluster_group_search;
911
Mark Fashehda5cbf22006-10-06 18:34:35 -0700912 status = ocfs2_reserve_suballoc_bits(osb, ac,
913 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +0800914 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +0800915 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700916 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800917 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700918 goto bail;
919 }
920
Mark Fashehccd979b2005-12-15 14:31:24 -0800921bail:
922 return status;
923}
924
925/* Callers don't need to care which bitmap (local alloc or main) to
926 * use so we figure it out for them, but unfortunately this clutters
927 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -0700928static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
929 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800930 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700931 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800932{
933 int status;
934
935 mlog_entry_void();
936
Robert P. J. Daycd861282006-12-13 00:34:52 -0800937 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 if (!(*ac)) {
939 status = -ENOMEM;
940 mlog_errno(status);
941 goto bail;
942 }
943
944 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -0700945 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -0800946
947 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +0800948 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
949 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800950 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 bits_wanted,
952 *ac);
Joel Becker1187c962008-09-03 20:03:39 -0700953 if (status == -EFBIG) {
954 /* The local alloc window is outside ac_max_block.
955 * use the main bitmap. */
956 status = -ENOSPC;
957 } else if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -0800958 mlog_errno(status);
959 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -0800960 }
961 }
962
963 if (status == -ENOSPC) {
964 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
965 if (status < 0) {
966 if (status != -ENOSPC)
967 mlog_errno(status);
968 goto bail;
969 }
970 }
971
972 status = 0;
973bail:
974 if ((status < 0) && *ac) {
975 ocfs2_free_alloc_context(*ac);
976 *ac = NULL;
977 }
978
979 mlog_exit(status);
980 return status;
981}
982
Joel Becker1187c962008-09-03 20:03:39 -0700983int ocfs2_reserve_clusters(struct ocfs2_super *osb,
984 u32 bits_wanted,
985 struct ocfs2_alloc_context **ac)
986{
Tao Ma60ca81e2009-02-25 00:53:24 +0800987 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
988 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -0700989}
990
Mark Fashehccd979b2005-12-15 14:31:24 -0800991/*
992 * More or less lifted from ext3. I'll leave their description below:
993 *
994 * "For ext3 allocations, we must not reuse any blocks which are
995 * allocated in the bitmap buffer's "last committed data" copy. This
996 * prevents deletes from freeing up the page for reuse until we have
997 * committed the delete transaction.
998 *
999 * If we didn't do this, then deleting something and reallocating it as
1000 * data would allow the old block to be overwritten before the
1001 * transaction committed (because we force data to disk before commit).
1002 * This would lead to corruption if we crashed between overwriting the
1003 * data and committing the delete.
1004 *
1005 * @@@ We may want to make this allocation behaviour conditional on
1006 * data-writes at some point, and disable it for metadata allocations or
1007 * sync-data inodes."
1008 *
1009 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001010 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 * called on a metadata group descriptor.
1012 */
1013static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1014 int nr)
1015{
1016 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001017 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001018
1019 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1020 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001021
1022 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001023 return 1;
1024
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001025 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001027 if (bg)
1028 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1029 else
1030 ret = 1;
1031 jbd_unlock_bh_state(bg_bh);
1032
1033 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001034}
1035
1036static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1037 struct buffer_head *bg_bh,
1038 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001039 unsigned int total_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 u16 *bit_off,
1041 u16 *bits_found)
1042{
1043 void *bitmap;
1044 u16 best_offset, best_size;
1045 int offset, start, found, status = 0;
1046 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1047
Joel Becker42035302008-11-13 14:49:15 -08001048 /* Callers got this descriptor from
1049 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1050 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001051
1052 found = start = best_offset = best_size = 0;
1053 bitmap = bg->bg_bitmap;
1054
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001055 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1056 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001057 break;
1058
1059 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1060 /* We found a zero, but we can't use it as it
1061 * hasn't been put to disk yet! */
1062 found = 0;
1063 start = offset + 1;
1064 } else if (offset == start) {
1065 /* we found a zero */
1066 found++;
1067 /* move start to the next bit to test */
1068 start++;
1069 } else {
1070 /* got a zero after some ones */
1071 found = 1;
1072 start = offset + 1;
1073 }
1074 if (found > best_size) {
1075 best_size = found;
1076 best_offset = start - found;
1077 }
1078 /* we got everything we needed */
1079 if (found == bits_wanted) {
1080 /* mlog(0, "Found it all!\n"); */
1081 break;
1082 }
1083 }
1084
1085 /* XXX: I think the first clause is equivalent to the second
1086 * - jlbec */
1087 if (found == bits_wanted) {
1088 *bit_off = start - found;
1089 *bits_found = found;
1090 } else if (best_size) {
1091 *bit_off = best_offset;
1092 *bits_found = best_size;
1093 } else {
1094 status = -ENOSPC;
1095 /* No error log here -- see the comment above
1096 * ocfs2_test_bg_bit_allocatable */
1097 }
1098
1099 return status;
1100}
1101
Mark Fasheh1fabe142006-10-09 18:11:45 -07001102static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 struct inode *alloc_inode,
1104 struct ocfs2_group_desc *bg,
1105 struct buffer_head *group_bh,
1106 unsigned int bit_off,
1107 unsigned int num_bits)
1108{
1109 int status;
1110 void *bitmap = bg->bg_bitmap;
1111 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1112
1113 mlog_entry_void();
1114
Joel Becker42035302008-11-13 14:49:15 -08001115 /* All callers get the descriptor via
1116 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1117 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001118 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1119
1120 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1121 num_bits);
1122
1123 if (ocfs2_is_cluster_bitmap(alloc_inode))
1124 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1125
Joel Becker13723d02008-10-17 19:25:01 -07001126 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001127 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001128 group_bh,
1129 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001130 if (status < 0) {
1131 mlog_errno(status);
1132 goto bail;
1133 }
1134
1135 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
1136
1137 while(num_bits--)
1138 ocfs2_set_bit(bit_off++, bitmap);
1139
1140 status = ocfs2_journal_dirty(handle,
1141 group_bh);
1142 if (status < 0) {
1143 mlog_errno(status);
1144 goto bail;
1145 }
1146
1147bail:
1148 mlog_exit(status);
1149 return status;
1150}
1151
1152/* find the one with the most empty bits */
1153static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1154{
1155 u16 curr, best;
1156
1157 BUG_ON(!cl->cl_next_free_rec);
1158
1159 best = curr = 0;
1160 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1161 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1162 le32_to_cpu(cl->cl_recs[best].c_free))
1163 best = curr;
1164 curr++;
1165 }
1166
1167 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1168 return best;
1169}
1170
Mark Fasheh1fabe142006-10-09 18:11:45 -07001171static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 struct inode *alloc_inode,
1173 struct buffer_head *fe_bh,
1174 struct buffer_head *bg_bh,
1175 struct buffer_head *prev_bg_bh,
1176 u16 chain)
1177{
1178 int status;
1179 /* there is a really tiny chance the journal calls could fail,
1180 * but we wouldn't want inconsistent blocks in *any* case. */
1181 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1182 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1183 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1184 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1185
Joel Becker42035302008-11-13 14:49:15 -08001186 /* The caller got these descriptors from
1187 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1188 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1189 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001190
Mark Fashehb06970532006-03-03 10:24:33 -08001191 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001192 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1193 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1194 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001195
1196 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1197 bg_ptr = le64_to_cpu(bg->bg_next_group);
1198 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1199
Joel Becker0cf2f762009-02-12 16:41:25 -08001200 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1201 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001202 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001203 if (status < 0) {
1204 mlog_errno(status);
1205 goto out_rollback;
1206 }
1207
1208 prev_bg->bg_next_group = bg->bg_next_group;
1209
1210 status = ocfs2_journal_dirty(handle, prev_bg_bh);
1211 if (status < 0) {
1212 mlog_errno(status);
1213 goto out_rollback;
1214 }
1215
Joel Becker0cf2f762009-02-12 16:41:25 -08001216 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1217 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001218 if (status < 0) {
1219 mlog_errno(status);
1220 goto out_rollback;
1221 }
1222
1223 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
1224
1225 status = ocfs2_journal_dirty(handle, bg_bh);
1226 if (status < 0) {
1227 mlog_errno(status);
1228 goto out_rollback;
1229 }
1230
Joel Becker0cf2f762009-02-12 16:41:25 -08001231 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1232 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001233 if (status < 0) {
1234 mlog_errno(status);
1235 goto out_rollback;
1236 }
1237
1238 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
1239
1240 status = ocfs2_journal_dirty(handle, fe_bh);
1241 if (status < 0) {
1242 mlog_errno(status);
1243 goto out_rollback;
1244 }
1245
1246 status = 0;
1247out_rollback:
1248 if (status < 0) {
1249 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1250 bg->bg_next_group = cpu_to_le64(bg_ptr);
1251 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1252 }
Joel Becker42035302008-11-13 14:49:15 -08001253
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 mlog_exit(status);
1255 return status;
1256}
1257
1258static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1259 u32 wanted)
1260{
1261 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1262}
1263
1264/* return 0 on success, -ENOSPC to keep searching and any other < 0
1265 * value on error. */
1266static int ocfs2_cluster_group_search(struct inode *inode,
1267 struct buffer_head *group_bh,
1268 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001269 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 u16 *bit_off, u16 *bits_found)
1271{
1272 int search = -ENOSPC;
1273 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001274 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001275 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001276 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 u16 tmp_off, tmp_found;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001278 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001279
1280 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1281
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001282 if (gd->bg_free_bits_count) {
1283 max_bits = le16_to_cpu(gd->bg_bits);
1284
1285 /* Tail groups in cluster bitmaps which aren't cpg
1286 * aligned are prone to partial extention by a failed
1287 * fs resize. If the file system resize never got to
1288 * update the dinode cluster count, then we don't want
1289 * to trust any clusters past it, regardless of what
1290 * the group descriptor says. */
1291 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1292 le64_to_cpu(gd->bg_blkno));
1293 if ((gd_cluster_off + max_bits) >
1294 OCFS2_I(inode)->ip_clusters) {
1295 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1296 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1297 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1298 le16_to_cpu(gd->bg_bits),
1299 OCFS2_I(inode)->ip_clusters, max_bits);
1300 }
1301
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1303 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001304 max_bits,
Mark Fashehccd979b2005-12-15 14:31:24 -08001305 &tmp_off, &tmp_found);
1306 if (ret)
1307 return ret;
1308
Joel Becker1187c962008-09-03 20:03:39 -07001309 if (max_block) {
1310 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1311 gd_cluster_off +
1312 tmp_off + tmp_found);
1313 mlog(0, "Checking %llu against %llu\n",
1314 (unsigned long long)blkoff,
1315 (unsigned long long)max_block);
1316 if (blkoff > max_block)
1317 return -ENOSPC;
1318 }
1319
Mark Fashehccd979b2005-12-15 14:31:24 -08001320 /* ocfs2_block_group_find_clear_bits() might
1321 * return success, but we still want to return
1322 * -ENOSPC unless it found the minimum number
1323 * of bits. */
1324 if (min_bits <= tmp_found) {
1325 *bit_off = tmp_off;
1326 *bits_found = tmp_found;
1327 search = 0; /* success */
Mark Fasheh9c7af402008-07-28 18:02:53 -07001328 } else if (tmp_found) {
1329 /*
1330 * Don't show bits which we'll be returning
1331 * for allocation to the local alloc bitmap.
1332 */
1333 ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 }
1335 }
1336
1337 return search;
1338}
1339
1340static int ocfs2_block_group_search(struct inode *inode,
1341 struct buffer_head *group_bh,
1342 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001343 u64 max_block,
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 u16 *bit_off, u16 *bits_found)
1345{
1346 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001347 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1349
1350 BUG_ON(min_bits != 1);
1351 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1352
Joel Becker1187c962008-09-03 20:03:39 -07001353 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001354 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1355 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001356 le16_to_cpu(bg->bg_bits),
Mark Fashehccd979b2005-12-15 14:31:24 -08001357 bit_off, bits_found);
Joel Becker1187c962008-09-03 20:03:39 -07001358 if (!ret && max_block) {
1359 blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
1360 *bits_found;
1361 mlog(0, "Checking %llu against %llu\n",
1362 (unsigned long long)blkoff,
1363 (unsigned long long)max_block);
1364 if (blkoff > max_block)
1365 ret = -ENOSPC;
1366 }
1367 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001368
1369 return ret;
1370}
1371
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001372static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001373 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001374 struct buffer_head *di_bh,
1375 u32 num_bits,
1376 u16 chain)
1377{
1378 int ret;
1379 u32 tmp_used;
1380 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1381 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1382
Joel Becker0cf2f762009-02-12 16:41:25 -08001383 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001384 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001385 if (ret < 0) {
1386 mlog_errno(ret);
1387 goto out;
1388 }
1389
1390 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1391 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1392 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
1393
1394 ret = ocfs2_journal_dirty(handle, di_bh);
1395 if (ret < 0)
1396 mlog_errno(ret);
1397
1398out:
1399 return ret;
1400}
1401
1402static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001403 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001404 u32 bits_wanted,
1405 u32 min_bits,
1406 u16 *bit_off,
1407 unsigned int *num_bits,
1408 u64 gd_blkno,
1409 u16 *bits_left)
1410{
1411 int ret;
1412 u16 found;
1413 struct buffer_head *group_bh = NULL;
1414 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001415 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001416 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001417
Joel Becker68f64d42008-11-13 14:49:14 -08001418 ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
1419 &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001420 if (ret < 0) {
1421 mlog_errno(ret);
1422 return ret;
1423 }
1424
1425 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001426 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001427 ac->ac_max_block, bit_off, &found);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001428 if (ret < 0) {
1429 if (ret != -ENOSPC)
1430 mlog_errno(ret);
1431 goto out;
1432 }
1433
1434 *num_bits = found;
1435
1436 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
1437 *num_bits,
1438 le16_to_cpu(gd->bg_chain));
1439 if (ret < 0) {
1440 mlog_errno(ret);
1441 goto out;
1442 }
1443
1444 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
1445 *bit_off, *num_bits);
1446 if (ret < 0)
1447 mlog_errno(ret);
1448
1449 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1450
1451out:
1452 brelse(group_bh);
1453
1454 return ret;
1455}
1456
Mark Fashehccd979b2005-12-15 14:31:24 -08001457static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001458 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 u32 bits_wanted,
1460 u32 min_bits,
1461 u16 *bit_off,
1462 unsigned int *num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001463 u64 *bg_blkno,
1464 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001465{
1466 int status;
1467 u16 chain, tmp_bits;
1468 u32 tmp_used;
1469 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001470 struct inode *alloc_inode = ac->ac_inode;
1471 struct buffer_head *group_bh = NULL;
1472 struct buffer_head *prev_group_bh = NULL;
1473 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1474 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1475 struct ocfs2_group_desc *bg;
1476
1477 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001478 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1479 bits_wanted, chain,
1480 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001481
Joel Becker68f64d42008-11-13 14:49:14 -08001482 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1483 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1484 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 if (status < 0) {
1486 mlog_errno(status);
1487 goto bail;
1488 }
1489 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001490
1491 status = -ENOSPC;
1492 /* for now, the chain search is a bit simplistic. We just use
1493 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001494 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1495 bits_wanted, min_bits,
1496 ac->ac_max_block, bit_off,
1497 &tmp_bits)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001498 if (!bg->bg_next_group)
1499 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001500
1501 brelse(prev_group_bh);
1502 prev_group_bh = NULL;
1503
Mark Fashehccd979b2005-12-15 14:31:24 -08001504 next_group = le64_to_cpu(bg->bg_next_group);
1505 prev_group_bh = group_bh;
1506 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001507 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1508 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001509 if (status < 0) {
1510 mlog_errno(status);
1511 goto bail;
1512 }
1513 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001514 }
1515 if (status < 0) {
1516 if (status != -ENOSPC)
1517 mlog_errno(status);
1518 goto bail;
1519 }
1520
Mark Fashehb06970532006-03-03 10:24:33 -08001521 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001522 tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001523
1524 *num_bits = tmp_bits;
1525
1526 BUG_ON(*num_bits == 0);
1527
1528 /*
1529 * Keep track of previous block descriptor read. When
1530 * we find a target, if we have read more than X
1531 * number of descriptors, and the target is reasonably
1532 * empty, relink him to top of his chain.
1533 *
1534 * We've read 0 extra blocks and only send one more to
1535 * the transaction, yet the next guy to search has a
1536 * much easier time.
1537 *
1538 * Do this *after* figuring out how many bits we're taking out
1539 * of our target group.
1540 */
1541 if (ac->ac_allow_chain_relink &&
1542 (prev_group_bh) &&
1543 (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
1544 status = ocfs2_relink_block_group(handle, alloc_inode,
1545 ac->ac_bh, group_bh,
1546 prev_group_bh, chain);
1547 if (status < 0) {
1548 mlog_errno(status);
1549 goto bail;
1550 }
1551 }
1552
1553 /* Ok, claim our bits now: set the info on dinode, chainlist
1554 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001555 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001556 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001557 ac->ac_bh,
1558 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001559 if (status < 0) {
1560 mlog_errno(status);
1561 goto bail;
1562 }
1563
1564 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
1565 fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
1566 le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
1567
1568 status = ocfs2_journal_dirty(handle,
1569 ac->ac_bh);
1570 if (status < 0) {
1571 mlog_errno(status);
1572 goto bail;
1573 }
1574
1575 status = ocfs2_block_group_set_bits(handle,
1576 alloc_inode,
1577 bg,
1578 group_bh,
1579 *bit_off,
1580 *num_bits);
1581 if (status < 0) {
1582 mlog_errno(status);
1583 goto bail;
1584 }
1585
Mark Fashehb06970532006-03-03 10:24:33 -08001586 mlog(0, "Allocated %u bits from suballocator %llu\n", *num_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001587 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001588
1589 *bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001590 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001591bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001592 brelse(group_bh);
1593 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001594
1595 mlog_exit(status);
1596 return status;
1597}
1598
1599/* will give out up to bits_wanted contiguous bits. */
1600static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
1601 struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001602 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001603 u32 bits_wanted,
1604 u32 min_bits,
1605 u16 *bit_off,
1606 unsigned int *num_bits,
1607 u64 *bg_blkno)
1608{
1609 int status;
1610 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001611 u16 bits_left = 0;
1612 u64 hint_blkno = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001613 struct ocfs2_chain_list *cl;
1614 struct ocfs2_dinode *fe;
1615
1616 mlog_entry_void();
1617
1618 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1619 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1620 BUG_ON(!ac->ac_bh);
1621
1622 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001623
1624 /* The bh was validated by the inode read during
1625 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1626 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1627
Mark Fashehccd979b2005-12-15 14:31:24 -08001628 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1629 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Mark Fashehb06970532006-03-03 10:24:33 -08001630 ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
1631 "bits but only %u total.",
1632 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001633 le32_to_cpu(fe->id1.bitmap1.i_used),
1634 le32_to_cpu(fe->id1.bitmap1.i_total));
1635 status = -EIO;
1636 goto bail;
1637 }
1638
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001639 if (hint_blkno) {
1640 /* Attempt to short-circuit the usual search mechanism
1641 * by jumping straight to the most recently used
1642 * allocation group. This helps us mantain some
1643 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001644 status = ocfs2_search_one_group(ac, handle, bits_wanted,
1645 min_bits, bit_off, num_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001646 hint_blkno, &bits_left);
1647 if (!status) {
1648 /* Be careful to update *bg_blkno here as the
1649 * caller is expecting it to be filled in, and
1650 * ocfs2_search_one_group() won't do that for
1651 * us. */
1652 *bg_blkno = hint_blkno;
1653 goto set_hint;
1654 }
1655 if (status < 0 && status != -ENOSPC) {
1656 mlog_errno(status);
1657 goto bail;
1658 }
1659 }
1660
Mark Fashehccd979b2005-12-15 14:31:24 -08001661 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1662
1663 victim = ocfs2_find_victim_chain(cl);
1664 ac->ac_chain = victim;
1665 ac->ac_allow_chain_relink = 1;
1666
Mark Fashehda5cbf22006-10-06 18:34:35 -07001667 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001668 num_bits, bg_blkno, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001669 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001670 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 if (status < 0 && status != -ENOSPC) {
1672 mlog_errno(status);
1673 goto bail;
1674 }
1675
1676 mlog(0, "Search of victim chain %u came up with nothing, "
1677 "trying all chains now.\n", victim);
1678
1679 /* If we didn't pick a good victim, then just default to
1680 * searching each chain in order. Don't allow chain relinking
1681 * because we only calculate enough journal credits for one
1682 * relink per alloc. */
1683 ac->ac_allow_chain_relink = 0;
1684 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1685 if (i == victim)
1686 continue;
1687 if (!cl->cl_recs[i].c_free)
1688 continue;
1689
1690 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001691 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001692 bit_off, num_bits, bg_blkno,
1693 &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001694 if (!status)
1695 break;
1696 if (status < 0 && status != -ENOSPC) {
1697 mlog_errno(status);
1698 goto bail;
1699 }
1700 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001701
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001702set_hint:
1703 if (status != -ENOSPC) {
1704 /* If the next search of this group is not likely to
1705 * yield a suitable extent, then we reset the last
1706 * group hint so as to not waste a disk read */
1707 if (bits_left < min_bits)
1708 ac->ac_last_group = 0;
1709 else
1710 ac->ac_last_group = *bg_blkno;
1711 }
1712
1713bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 mlog_exit(status);
1715 return status;
1716}
1717
1718int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001719 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001720 struct ocfs2_alloc_context *ac,
1721 u32 bits_wanted,
1722 u16 *suballoc_bit_start,
1723 unsigned int *num_bits,
1724 u64 *blkno_start)
1725{
1726 int status;
1727 u64 bg_blkno;
1728
1729 BUG_ON(!ac);
1730 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1731 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001732
1733 status = ocfs2_claim_suballoc_bits(osb,
1734 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001735 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001736 bits_wanted,
1737 1,
1738 suballoc_bit_start,
1739 num_bits,
1740 &bg_blkno);
1741 if (status < 0) {
1742 mlog_errno(status);
1743 goto bail;
1744 }
1745 atomic_inc(&osb->alloc_stats.bg_allocs);
1746
1747 *blkno_start = bg_blkno + (u64) *suballoc_bit_start;
1748 ac->ac_bits_given += (*num_bits);
1749 status = 0;
1750bail:
1751 mlog_exit(status);
1752 return status;
1753}
1754
Tao Ma13821152009-02-25 00:53:23 +08001755static void ocfs2_init_inode_ac_group(struct inode *dir,
1756 struct buffer_head *parent_fe_bh,
1757 struct ocfs2_alloc_context *ac)
1758{
1759 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1760 /*
1761 * Try to allocate inodes from some specific group.
1762 *
1763 * If the parent dir has recorded the last group used in allocation,
1764 * cool, use it. Otherwise if we try to allocate new inode from the
1765 * same slot the parent dir belongs to, use the same chunk.
1766 *
1767 * We are very careful here to avoid the mistake of setting
1768 * ac_last_group to a group descriptor from a different (unlocked) slot.
1769 */
1770 if (OCFS2_I(dir)->ip_last_used_group &&
1771 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1772 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1773 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1774 ac->ac_last_group = ocfs2_which_suballoc_group(
1775 le64_to_cpu(fe->i_blkno),
1776 le16_to_cpu(fe->i_suballoc_bit));
1777}
1778
1779static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1780 struct ocfs2_alloc_context *ac)
1781{
1782 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1783 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1784}
1785
Mark Fashehccd979b2005-12-15 14:31:24 -08001786int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001787 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001788 struct inode *dir,
1789 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 struct ocfs2_alloc_context *ac,
1791 u16 *suballoc_bit,
1792 u64 *fe_blkno)
1793{
1794 int status;
1795 unsigned int num_bits;
1796 u64 bg_blkno;
1797
1798 mlog_entry_void();
1799
1800 BUG_ON(!ac);
1801 BUG_ON(ac->ac_bits_given != 0);
1802 BUG_ON(ac->ac_bits_wanted != 1);
1803 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001804
Tao Ma13821152009-02-25 00:53:23 +08001805 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1806
Mark Fashehccd979b2005-12-15 14:31:24 -08001807 status = ocfs2_claim_suballoc_bits(osb,
1808 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001809 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001810 1,
1811 1,
1812 suballoc_bit,
1813 &num_bits,
1814 &bg_blkno);
1815 if (status < 0) {
1816 mlog_errno(status);
1817 goto bail;
1818 }
1819 atomic_inc(&osb->alloc_stats.bg_allocs);
1820
1821 BUG_ON(num_bits != 1);
1822
1823 *fe_blkno = bg_blkno + (u64) (*suballoc_bit);
1824 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08001825 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001826 status = 0;
1827bail:
1828 mlog_exit(status);
1829 return status;
1830}
1831
1832/* translate a group desc. blkno and it's bitmap offset into
1833 * disk cluster offset. */
1834static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1835 u64 bg_blkno,
1836 u16 bg_bit_off)
1837{
1838 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1839 u32 cluster = 0;
1840
1841 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1842
1843 if (bg_blkno != osb->first_cluster_group_blkno)
1844 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1845 cluster += (u32) bg_bit_off;
1846 return cluster;
1847}
1848
1849/* given a cluster offset, calculate which block group it belongs to
1850 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08001851u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08001852{
1853 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1854 u32 group_no;
1855
1856 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1857
1858 group_no = cluster / osb->bitmap_cpg;
1859 if (!group_no)
1860 return osb->first_cluster_group_blkno;
1861 return ocfs2_clusters_to_blocks(inode->i_sb,
1862 group_no * osb->bitmap_cpg);
1863}
1864
1865/* given the block number of a cluster start, calculate which cluster
1866 * group and descriptor bitmap offset that corresponds to. */
1867static inline void ocfs2_block_to_cluster_group(struct inode *inode,
1868 u64 data_blkno,
1869 u64 *bg_blkno,
1870 u16 *bg_bit_off)
1871{
1872 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1873 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
1874
1875 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1876
1877 *bg_blkno = ocfs2_which_cluster_group(inode,
1878 data_cluster);
1879
1880 if (*bg_blkno == osb->first_cluster_group_blkno)
1881 *bg_bit_off = (u16) data_cluster;
1882 else
1883 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
1884 data_blkno - *bg_blkno);
1885}
1886
1887/*
1888 * min_bits - minimum contiguous chunk from this total allocation we
1889 * can handle. set to what we asked for originally for a full
1890 * contig. allocation, set to '1' to indicate we can deal with extents
1891 * of any size.
1892 */
Mark Fasheh415cb802007-09-16 20:10:16 -07001893int __ocfs2_claim_clusters(struct ocfs2_super *osb,
1894 handle_t *handle,
1895 struct ocfs2_alloc_context *ac,
1896 u32 min_clusters,
1897 u32 max_clusters,
1898 u32 *cluster_start,
1899 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08001900{
1901 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07001902 unsigned int bits_wanted = max_clusters;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001903 u64 bg_blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 u16 bg_bit_off;
1905
1906 mlog_entry_void();
1907
Mark Fashehccd979b2005-12-15 14:31:24 -08001908 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1909
1910 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
1911 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08001912
1913 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
1914 status = ocfs2_claim_local_alloc_bits(osb,
1915 handle,
1916 ac,
1917 bits_wanted,
1918 cluster_start,
1919 num_clusters);
1920 if (!status)
1921 atomic_inc(&osb->alloc_stats.local_data);
1922 } else {
1923 if (min_clusters > (osb->bitmap_cpg - 1)) {
1924 /* The only paths asking for contiguousness
1925 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08001926 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
1927 "group bitmap size %u!\n", min_clusters,
1928 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08001929 status = -ENOSPC;
1930 goto bail;
1931 }
1932 /* clamp the current request down to a realistic size. */
1933 if (bits_wanted > (osb->bitmap_cpg - 1))
1934 bits_wanted = osb->bitmap_cpg - 1;
1935
1936 status = ocfs2_claim_suballoc_bits(osb,
1937 ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001938 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 bits_wanted,
1940 min_clusters,
1941 &bg_bit_off,
1942 num_clusters,
1943 &bg_blkno);
1944 if (!status) {
1945 *cluster_start =
1946 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
1947 bg_blkno,
1948 bg_bit_off);
1949 atomic_inc(&osb->alloc_stats.bitmap_data);
1950 }
1951 }
1952 if (status < 0) {
1953 if (status != -ENOSPC)
1954 mlog_errno(status);
1955 goto bail;
1956 }
1957
1958 ac->ac_bits_given += *num_clusters;
1959
1960bail:
1961 mlog_exit(status);
1962 return status;
1963}
1964
Mark Fasheh415cb802007-09-16 20:10:16 -07001965int ocfs2_claim_clusters(struct ocfs2_super *osb,
1966 handle_t *handle,
1967 struct ocfs2_alloc_context *ac,
1968 u32 min_clusters,
1969 u32 *cluster_start,
1970 u32 *num_clusters)
1971{
1972 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1973
1974 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
1975 bits_wanted, cluster_start, num_clusters);
1976}
1977
Mark Fasheh1fabe142006-10-09 18:11:45 -07001978static inline int ocfs2_block_group_clear_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001979 struct inode *alloc_inode,
1980 struct ocfs2_group_desc *bg,
1981 struct buffer_head *group_bh,
1982 unsigned int bit_off,
1983 unsigned int num_bits)
1984{
1985 int status;
1986 unsigned int tmp;
1987 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1988 struct ocfs2_group_desc *undo_bg = NULL;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001989 int cluster_bitmap = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001990
1991 mlog_entry_void();
1992
Joel Becker42035302008-11-13 14:49:15 -08001993 /* The caller got this descriptor from
1994 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1995 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001996
1997 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
1998
1999 if (ocfs2_is_cluster_bitmap(alloc_inode))
2000 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
2001
Joel Becker0cf2f762009-02-12 16:41:25 -08002002 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
2003 group_bh, journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08002004 if (status < 0) {
2005 mlog_errno(status);
2006 goto bail;
2007 }
2008
2009 if (ocfs2_is_cluster_bitmap(alloc_inode))
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002010 cluster_bitmap = 1;
2011
2012 if (cluster_bitmap) {
2013 jbd_lock_bh_state(group_bh);
2014 undo_bg = (struct ocfs2_group_desc *)
2015 bh2jh(group_bh)->b_committed_data;
2016 BUG_ON(!undo_bg);
2017 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002018
2019 tmp = num_bits;
2020 while(tmp--) {
2021 ocfs2_clear_bit((bit_off + tmp),
2022 (unsigned long *) bg->bg_bitmap);
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002023 if (cluster_bitmap)
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 ocfs2_set_bit(bit_off + tmp,
2025 (unsigned long *) undo_bg->bg_bitmap);
2026 }
2027 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2028
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002029 if (cluster_bitmap)
2030 jbd_unlock_bh_state(group_bh);
2031
Mark Fashehccd979b2005-12-15 14:31:24 -08002032 status = ocfs2_journal_dirty(handle, group_bh);
2033 if (status < 0)
2034 mlog_errno(status);
2035bail:
2036 return status;
2037}
2038
2039/*
2040 * expects the suballoc inode to already be locked.
2041 */
Mark Fasheh2b604352007-06-22 15:45:27 -07002042int ocfs2_free_suballoc_bits(handle_t *handle,
2043 struct inode *alloc_inode,
2044 struct buffer_head *alloc_bh,
2045 unsigned int start_bit,
2046 u64 bg_blkno,
2047 unsigned int count)
Mark Fashehccd979b2005-12-15 14:31:24 -08002048{
2049 int status = 0;
2050 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002051 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2052 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2053 struct buffer_head *group_bh = NULL;
2054 struct ocfs2_group_desc *group;
2055
2056 mlog_entry_void();
2057
Joel Becker10995aa2008-11-13 14:49:12 -08002058 /* The alloc_bh comes from ocfs2_free_dinode() or
2059 * ocfs2_free_clusters(). The callers have all locked the
2060 * allocator and gotten alloc_bh from the lock call. This
2061 * validates the dinode buffer. Any corruption that has happended
2062 * is a code bug. */
2063 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002064 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2065
Mark Fashehb06970532006-03-03 10:24:33 -08002066 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2067 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2068 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002069
Joel Becker68f64d42008-11-13 14:49:14 -08002070 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2071 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002072 if (status < 0) {
2073 mlog_errno(status);
2074 goto bail;
2075 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002076 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002077
Mark Fashehccd979b2005-12-15 14:31:24 -08002078 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2079
2080 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2081 group, group_bh,
2082 start_bit, count);
2083 if (status < 0) {
2084 mlog_errno(status);
2085 goto bail;
2086 }
2087
Joel Becker0cf2f762009-02-12 16:41:25 -08002088 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2089 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002090 if (status < 0) {
2091 mlog_errno(status);
2092 goto bail;
2093 }
2094
2095 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2096 count);
2097 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2098 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
2099
2100 status = ocfs2_journal_dirty(handle, alloc_bh);
2101 if (status < 0) {
2102 mlog_errno(status);
2103 goto bail;
2104 }
2105
2106bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002107 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002108
2109 mlog_exit(status);
2110 return status;
2111}
2112
Mark Fasheh1fabe142006-10-09 18:11:45 -07002113int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002114 struct inode *inode_alloc_inode,
2115 struct buffer_head *inode_alloc_bh,
2116 struct ocfs2_dinode *di)
2117{
2118 u64 blk = le64_to_cpu(di->i_blkno);
2119 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2120 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2121
2122 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2123 inode_alloc_bh, bit, bg_blkno, 1);
2124}
2125
Mark Fasheh1fabe142006-10-09 18:11:45 -07002126int ocfs2_free_clusters(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002127 struct inode *bitmap_inode,
2128 struct buffer_head *bitmap_bh,
2129 u64 start_blk,
2130 unsigned int num_clusters)
2131{
2132 int status;
2133 u16 bg_start_bit;
2134 u64 bg_blkno;
2135 struct ocfs2_dinode *fe;
2136
2137 /* You can't ever have a contiguous set of clusters
2138 * bigger than a block group bitmap so we never have to worry
2139 * about looping on them. */
2140
2141 mlog_entry_void();
2142
2143 /* This is expensive. We can safely remove once this stuff has
2144 * gotten tested really well. */
2145 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2146
2147 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2148
2149 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2150 &bg_start_bit);
2151
Mark Fashehb06970532006-03-03 10:24:33 -08002152 mlog(0, "want to free %u clusters starting at block %llu\n",
2153 num_clusters, (unsigned long long)start_blk);
2154 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2155 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156
2157 status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2158 bg_start_bit, bg_blkno,
2159 num_clusters);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002160 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002161 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002162 goto out;
2163 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002164
Mark Fasheh9c7af402008-07-28 18:02:53 -07002165 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2166 num_clusters);
2167
2168out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002169 mlog_exit(status);
2170 return status;
2171}
2172
2173static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2174{
2175 printk("Block Group:\n");
2176 printk("bg_signature: %s\n", bg->bg_signature);
2177 printk("bg_size: %u\n", bg->bg_size);
2178 printk("bg_bits: %u\n", bg->bg_bits);
2179 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2180 printk("bg_chain: %u\n", bg->bg_chain);
2181 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002182 printk("bg_next_group: %llu\n",
2183 (unsigned long long)bg->bg_next_group);
2184 printk("bg_parent_dinode: %llu\n",
2185 (unsigned long long)bg->bg_parent_dinode);
2186 printk("bg_blkno: %llu\n",
2187 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002188}
2189
2190static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2191{
2192 int i;
2193
Mark Fashehb06970532006-03-03 10:24:33 -08002194 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002196 printk("i_size: %llu\n",
2197 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002198 printk("i_clusters: %u\n", fe->i_clusters);
2199 printk("i_generation: %u\n",
2200 le32_to_cpu(fe->i_generation));
2201 printk("id1.bitmap1.i_used: %u\n",
2202 le32_to_cpu(fe->id1.bitmap1.i_used));
2203 printk("id1.bitmap1.i_total: %u\n",
2204 le32_to_cpu(fe->id1.bitmap1.i_total));
2205 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2206 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2207 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2208 printk("id2.i_chain.cl_next_free_rec: %u\n",
2209 fe->id2.i_chain.cl_next_free_rec);
2210 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2211 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2212 fe->id2.i_chain.cl_recs[i].c_free);
2213 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2214 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002215 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2216 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002217 }
2218}
Tao Mae7d4cb62008-08-18 17:38:44 +08002219
2220/*
2221 * For a given allocation, determine which allocators will need to be
2222 * accessed, and lock them, reserving the appropriate number of bits.
2223 *
2224 * Sparse file systems call this from ocfs2_write_begin_nolock()
2225 * and ocfs2_allocate_unwritten_extents().
2226 *
2227 * File systems which don't support holes call this from
2228 * ocfs2_extend_allocation().
2229 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002230int ocfs2_lock_allocators(struct inode *inode,
2231 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002232 u32 clusters_to_add, u32 extents_to_split,
2233 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002234 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002235{
2236 int ret = 0, num_free_extents;
2237 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2239
2240 *meta_ac = NULL;
2241 if (data_ac)
2242 *data_ac = NULL;
2243
2244 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2245
Joel Becker3d03a302009-02-12 17:49:26 -08002246 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002247 if (num_free_extents < 0) {
2248 ret = num_free_extents;
2249 mlog_errno(ret);
2250 goto out;
2251 }
2252
2253 /*
2254 * Sparse allocation file systems need to be more conservative
2255 * with reserving room for expansion - the actual allocation
2256 * happens while we've got a journal handle open so re-taking
2257 * a cluster lock (because we ran out of room for another
2258 * extent) will violate ordering rules.
2259 *
2260 * Most of the time we'll only be seeing this 1 cluster at a time
2261 * anyway.
2262 *
2263 * Always lock for any unwritten extents - we might want to
2264 * add blocks during a split.
2265 */
2266 if (!num_free_extents ||
2267 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002268 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002269 if (ret < 0) {
2270 if (ret != -ENOSPC)
2271 mlog_errno(ret);
2272 goto out;
2273 }
2274 }
2275
2276 if (clusters_to_add == 0)
2277 goto out;
2278
2279 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2280 if (ret < 0) {
2281 if (ret != -ENOSPC)
2282 mlog_errno(ret);
2283 goto out;
2284 }
2285
2286out:
2287 if (ret) {
2288 if (*meta_ac) {
2289 ocfs2_free_alloc_context(*meta_ac);
2290 *meta_ac = NULL;
2291 }
2292
2293 /*
2294 * We cannot have an error and a non null *data_ac.
2295 */
2296 }
2297
2298 return ret;
2299}
wengang wang6ca497a2009-03-06 21:29:10 +08002300
2301/*
2302 * Read the inode specified by blkno to get suballoc_slot and
2303 * suballoc_bit.
2304 */
2305static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2306 u16 *suballoc_slot, u16 *suballoc_bit)
2307{
2308 int status;
2309 struct buffer_head *inode_bh = NULL;
2310 struct ocfs2_dinode *inode_fe;
2311
Joel Becker5b09b502009-04-21 16:31:20 -07002312 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002313
2314 /* dirty read disk */
2315 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2316 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002317 mlog(ML_ERROR, "read block %llu failed %d\n",
2318 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002319 goto bail;
2320 }
2321
2322 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2323 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002324 mlog(ML_ERROR, "invalid inode %llu requested\n",
2325 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002326 status = -EINVAL;
2327 goto bail;
2328 }
2329
Tao Ma0fba8132009-03-19 05:08:43 +08002330 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002331 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2332 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002333 (unsigned long long)blkno,
2334 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002335 status = -EINVAL;
2336 goto bail;
2337 }
2338
2339 if (suballoc_slot)
2340 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2341 if (suballoc_bit)
2342 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2343
2344bail:
2345 brelse(inode_bh);
2346
2347 mlog_exit(status);
2348 return status;
2349}
2350
2351/*
2352 * test whether bit is SET in allocator bitmap or not. on success, 0
2353 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2354 * is returned and *res is meaningless. Call this after you have
2355 * cluster locked against suballoc, or you may get a result based on
2356 * non-up2date contents
2357 */
2358static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2359 struct inode *suballoc,
2360 struct buffer_head *alloc_bh, u64 blkno,
2361 u16 bit, int *res)
2362{
2363 struct ocfs2_dinode *alloc_fe;
2364 struct ocfs2_group_desc *group;
2365 struct buffer_head *group_bh = NULL;
2366 u64 bg_blkno;
2367 int status;
2368
Joel Becker5b09b502009-04-21 16:31:20 -07002369 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2370 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002371
2372 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2373 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2374 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2375 (unsigned int)bit,
2376 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2377 status = -EINVAL;
2378 goto bail;
2379 }
2380
2381 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2382 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2383 &group_bh);
2384 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002385 mlog(ML_ERROR, "read group %llu failed %d\n",
2386 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002387 goto bail;
2388 }
2389
2390 group = (struct ocfs2_group_desc *) group_bh->b_data;
2391 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2392
2393bail:
2394 brelse(group_bh);
2395
2396 mlog_exit(status);
2397 return status;
2398}
2399
2400/*
2401 * Test if the bit representing this inode (blkno) is set in the
2402 * suballocator.
2403 *
2404 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2405 *
2406 * In the event of failure, a negative value is returned and *res is
2407 * meaningless.
2408 *
2409 * Callers must make sure to hold nfs_sync_lock to prevent
2410 * ocfs2_delete_inode() on another node from accessing the same
2411 * suballocator concurrently.
2412 */
2413int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2414{
2415 int status;
2416 u16 suballoc_bit = 0, suballoc_slot = 0;
2417 struct inode *inode_alloc_inode;
2418 struct buffer_head *alloc_bh = NULL;
2419
Joel Becker5b09b502009-04-21 16:31:20 -07002420 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002421
2422 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2423 &suballoc_bit);
2424 if (status < 0) {
2425 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2426 goto bail;
2427 }
2428
2429 inode_alloc_inode =
2430 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2431 suballoc_slot);
2432 if (!inode_alloc_inode) {
2433 /* the error code could be inaccurate, but we are not able to
2434 * get the correct one. */
2435 status = -EINVAL;
2436 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2437 (u32)suballoc_slot);
2438 goto bail;
2439 }
2440
2441 mutex_lock(&inode_alloc_inode->i_mutex);
2442 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2443 if (status < 0) {
2444 mutex_unlock(&inode_alloc_inode->i_mutex);
2445 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2446 (u32)suballoc_slot, status);
2447 goto bail;
2448 }
2449
2450 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2451 blkno, suballoc_bit, res);
2452 if (status < 0)
2453 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2454
2455 ocfs2_inode_unlock(inode_alloc_inode, 0);
2456 mutex_unlock(&inode_alloc_inode->i_mutex);
2457
2458 iput(inode_alloc_inode);
2459 brelse(alloc_bh);
2460bail:
2461 mlog_exit(status);
2462 return status;
2463}