blob: b7491e2481ca59fe339c086bd1e21c664ddcaa26 [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
Joel Becker7d1fe092010-04-13 14:30:19 +080056struct ocfs2_suballoc_result {
Joel Becker2b6cb572010-03-26 10:09:15 +080057 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
Joel Beckerba206632010-03-26 10:08:59 +080060 u64 sr_blkno; /* The first allocated block */
Joel Becker7d1fe092010-04-13 14:30:19 +080061 unsigned int sr_bit_offset; /* The bit in the bg */
62 unsigned int sr_bits; /* How many bits we claimed */
63};
64
Mark Fashehccd979b2005-12-15 14:31:24 -080065static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
66static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
67static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070068static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080069 struct inode *alloc_inode,
70 struct buffer_head *bg_bh,
71 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080072 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 u16 my_chain,
74 struct ocfs2_chain_list *cl);
75static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
76 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070077 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080078 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080079 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080080 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080081
Mark Fashehccd979b2005-12-15 14:31:24 -080082static int ocfs2_cluster_group_search(struct inode *inode,
83 struct buffer_head *group_bh,
84 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070085 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080086 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080087static int ocfs2_block_group_search(struct inode *inode,
88 struct buffer_head *group_bh,
89 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070090 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080091 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +080092static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070093 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 u32 bits_wanted,
95 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +080096 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080097static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
98 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070099static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800100 struct inode *alloc_inode,
101 struct ocfs2_group_desc *bg,
102 struct buffer_head *group_bh,
103 unsigned int bit_off,
104 unsigned int num_bits);
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;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800140 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800141}
142
143void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
144{
145 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800146 kfree(ac);
147}
148
149static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
150{
151 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
152}
153
Joel Becker57e3e792008-11-13 14:49:13 -0800154#define do_error(fmt, ...) \
155 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800156 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800157 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
158 else \
159 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
160 } while (0)
161
Joel Becker970e4932008-11-13 14:49:19 -0800162static int ocfs2_validate_gd_self(struct super_block *sb,
163 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800164 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800165{
166 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
167
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700168 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800169 do_error("Group descriptor #%llu has bad signature %.*s",
170 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800171 gd->bg_signature);
172 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700173 }
174
Joel Becker68f64d42008-11-13 14:49:14 -0800175 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
176 do_error("Group descriptor #%llu has an invalid bg_blkno "
177 "of %llu",
178 (unsigned long long)bh->b_blocknr,
179 (unsigned long long)le64_to_cpu(gd->bg_blkno));
180 return -EINVAL;
181 }
182
183 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
184 do_error("Group descriptor #%llu has an invalid "
185 "fs_generation of #%u",
186 (unsigned long long)bh->b_blocknr,
187 le32_to_cpu(gd->bg_generation));
188 return -EINVAL;
189 }
190
Joel Becker970e4932008-11-13 14:49:19 -0800191 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
192 do_error("Group descriptor #%llu has bit count %u but "
193 "claims that %u are free",
194 (unsigned long long)bh->b_blocknr,
195 le16_to_cpu(gd->bg_bits),
196 le16_to_cpu(gd->bg_free_bits_count));
197 return -EINVAL;
198 }
199
200 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
201 do_error("Group descriptor #%llu has bit count %u but "
202 "max bitmap bits of %u",
203 (unsigned long long)bh->b_blocknr,
204 le16_to_cpu(gd->bg_bits),
205 8 * le16_to_cpu(gd->bg_size));
206 return -EINVAL;
207 }
208
209 return 0;
210}
211
212static int ocfs2_validate_gd_parent(struct super_block *sb,
213 struct ocfs2_dinode *di,
214 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800215 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800216{
217 unsigned int max_bits;
218 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
219
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700220 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800221 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800222 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800223 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800224 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
225 (unsigned long long)le64_to_cpu(di->i_blkno));
226 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700227 }
228
229 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
230 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800231 do_error("Group descriptor #%llu has bit count of %u",
232 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800233 le16_to_cpu(gd->bg_bits));
234 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700235 }
236
Tao Ma78c37eb2010-03-03 11:26:27 +0800237 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
238 if ((le16_to_cpu(gd->bg_chain) >
239 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
240 ((le16_to_cpu(gd->bg_chain) ==
241 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800242 do_error("Group descriptor #%llu has bad chain %u",
243 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800244 le16_to_cpu(gd->bg_chain));
245 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246 }
247
Joel Becker970e4932008-11-13 14:49:19 -0800248 return 0;
249}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700250
Joel Becker57e3e792008-11-13 14:49:13 -0800251#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700252
Joel Becker970e4932008-11-13 14:49:19 -0800253/*
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
256 */
257int ocfs2_check_group_descriptor(struct super_block *sb,
258 struct ocfs2_dinode *di,
259 struct buffer_head *bh)
260{
261 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700262 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800263
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700264 BUG_ON(!buffer_uptodate(bh));
265
266 /*
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
270 */
271 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700272 if (rc) {
273 mlog(ML_ERROR,
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh->b_blocknr);
276 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700277 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800278 if (!rc)
279 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280
281 return rc;
282}
283
284static int ocfs2_validate_group_descriptor(struct super_block *sb,
285 struct buffer_head *bh)
286{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700287 int rc;
288 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289
Joel Becker970e4932008-11-13 14:49:19 -0800290 mlog(0, "Validating group descriptor %llu\n",
291 (unsigned long long)bh->b_blocknr);
292
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700293 BUG_ON(!buffer_uptodate(bh));
294
295 /*
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
299 */
300 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301 if (rc)
302 return rc;
303
304 /*
305 * Errors after here are fatal.
306 */
307
Joel Becker970e4932008-11-13 14:49:19 -0800308 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700309}
310
Joel Becker68f64d42008-11-13 14:49:14 -0800311int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
312 u64 gd_blkno, struct buffer_head **bh)
313{
314 int rc;
315 struct buffer_head *tmp = *bh;
316
Joel Becker8cb471e2009-02-10 20:00:41 -0800317 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800318 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800319 if (rc)
320 goto out;
321
Joel Becker970e4932008-11-13 14:49:19 -0800322 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800323 if (rc) {
324 brelse(tmp);
325 goto out;
326 }
327
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
329 if (!*bh)
330 *bh = tmp;
331
332out:
333 return rc;
334}
335
Joel Becker798db352010-04-13 14:26:32 +0800336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
339 u64 p_blkno, u32 clusters)
340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
Tao Ma47119542010-04-22 14:09:15 +0800344 BUG_ON(!ocfs2_supports_discontig_bg(osb));
Joel Becker798db352010-04-13 14:26:32 +0800345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
Tao Ma47119542010-04-22 14:09:15 +0800348 rec->e_blkno = cpu_to_le64(p_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
351 rec->e_leaf_clusters = cpu_to_le32(clusters);
352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
Tao Ma47119542010-04-22 14:09:15 +0800353 le16_add_cpu(&bg->bg_free_bits_count,
354 clusters * le16_to_cpu(cl->cl_bpc));
Joel Becker798db352010-04-13 14:26:32 +0800355 le16_add_cpu(&el->l_next_free_rec, 1);
356}
357
Mark Fasheh1fabe142006-10-09 18:11:45 -0700358static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 struct inode *alloc_inode,
360 struct buffer_head *bg_bh,
361 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800362 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 u16 my_chain,
364 struct ocfs2_chain_list *cl)
365{
366 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800367 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
369 struct super_block * sb = alloc_inode->i_sb;
370
371 mlog_entry_void();
372
373 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800374 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
375 "b_blocknr (%llu)",
376 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 (unsigned long long) bg_bh->b_blocknr);
378 status = -EIO;
379 goto bail;
380 }
381
Joel Becker13723d02008-10-17 19:25:01 -0700382 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800383 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700384 bg_bh,
385 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800386 if (status < 0) {
387 mlog_errno(status);
388 goto bail;
389 }
390
391 memset(bg, 0, sb->s_blocksize);
392 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
393 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Joel Becker4cbe4242010-04-13 14:26:12 +0800394 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800395 bg->bg_chain = cpu_to_le16(my_chain);
396 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
397 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
398 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800399 if (group_clusters == le16_to_cpu(cl->cl_cpg))
400 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
401 else
Tao Ma47119542010-04-22 14:09:15 +0800402 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800403 group_clusters);
404
Mark Fashehccd979b2005-12-15 14:31:24 -0800405 /* set the 1st bit in the bitmap to account for the descriptor block */
406 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
407 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
408
Joel Beckerec20cec2010-03-19 14:13:52 -0700409 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800410
411 /* There is no need to zero out or otherwise initialize the
412 * other blocks in a group - All valid FS metadata in a block
413 * group stores the superblock fs_generation value at
414 * allocation time. */
415
416bail:
417 mlog_exit(status);
418 return status;
419}
420
421static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
422{
423 u16 curr, best;
424
425 best = curr = 0;
426 while (curr < le16_to_cpu(cl->cl_count)) {
427 if (le32_to_cpu(cl->cl_recs[best].c_total) >
428 le32_to_cpu(cl->cl_recs[curr].c_total))
429 best = curr;
430 curr++;
431 }
432 return best;
433}
434
Joel Becker798db352010-04-13 14:26:32 +0800435static struct buffer_head *
436ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
437 struct inode *alloc_inode,
438 struct ocfs2_alloc_context *ac,
439 struct ocfs2_chain_list *cl)
440{
441 int status;
442 u32 bit_off, num_bits;
443 u64 bg_blkno;
444 struct buffer_head *bg_bh;
445 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
446
Joel Becker1ed9b772010-05-06 13:59:06 +0800447 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800448 le16_to_cpu(cl->cl_cpg), &bit_off,
449 &num_bits);
450 if (status < 0) {
451 if (status != -ENOSPC)
452 mlog_errno(status);
453 goto bail;
454 }
455
456 /* setup the group */
457 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
458 mlog(0, "new descriptor, record %u, at block %llu\n",
459 alloc_rec, (unsigned long long)bg_blkno);
460
461 bg_bh = sb_getblk(osb->sb, bg_blkno);
462 if (!bg_bh) {
463 status = -EIO;
464 mlog_errno(status);
465 goto bail;
466 }
467 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
468
469 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
470 bg_blkno, num_bits, alloc_rec, cl);
471 if (status < 0) {
472 brelse(bg_bh);
473 mlog_errno(status);
474 }
475
476bail:
477 return status ? ERR_PTR(status) : bg_bh;
478}
479
480static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
481 handle_t *handle,
482 struct ocfs2_alloc_context *ac,
483 unsigned int min_bits,
484 u32 *bit_off, u32 *num_bits)
485{
486 int status;
487
488 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800489 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800490 bit_off, num_bits);
491 if (status != -ENOSPC)
492 break;
493
494 min_bits >>= 1;
495 }
496
497 return status;
498}
499
500static int ocfs2_block_group_grow_discontig(handle_t *handle,
501 struct inode *alloc_inode,
502 struct buffer_head *bg_bh,
503 struct ocfs2_alloc_context *ac,
504 struct ocfs2_chain_list *cl,
505 unsigned int min_bits)
506{
507 int status;
508 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
509 struct ocfs2_group_desc *bg =
510 (struct ocfs2_group_desc *)bg_bh->b_data;
Tao Ma47119542010-04-22 14:09:15 +0800511 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
512 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800513 u32 p_cpos, clusters;
514 u64 p_blkno;
515 struct ocfs2_extent_list *el = &bg->bg_list;
516
517 status = ocfs2_journal_access_gd(handle,
518 INODE_CACHE(alloc_inode),
519 bg_bh,
520 OCFS2_JOURNAL_ACCESS_CREATE);
521 if (status < 0) {
522 mlog_errno(status);
523 goto bail;
524 }
525
526 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
527 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800528 if (min_bits > needed)
529 min_bits = needed;
530 status = ocfs2_block_group_claim_bits(osb, handle, ac,
531 min_bits, &p_cpos,
532 &clusters);
533 if (status < 0) {
534 if (status != -ENOSPC)
535 mlog_errno(status);
536 goto bail;
537 }
538 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
539 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
540 clusters);
541
542 min_bits = clusters;
Tao Ma47119542010-04-22 14:09:15 +0800543 needed = le16_to_cpu(cl->cl_cpg) -
544 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800545 }
546
547 if (needed > 0) {
Tao Ma47119542010-04-22 14:09:15 +0800548 /*
549 * We have used up all the extent rec but can't fill up
550 * the cpg. So bail out.
551 */
552 status = -ENOSPC;
553 goto bail;
Joel Becker798db352010-04-13 14:26:32 +0800554 }
555
556 ocfs2_journal_dirty(handle, bg_bh);
557
558bail:
559 return status;
560}
561
Joel Becker8b06bc52010-03-26 10:09:29 +0800562static void ocfs2_bg_alloc_cleanup(handle_t *handle,
563 struct ocfs2_alloc_context *cluster_ac,
564 struct inode *alloc_inode,
565 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800566{
Joel Becker8b06bc52010-03-26 10:09:29 +0800567 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800568 struct ocfs2_group_desc *bg;
569 struct ocfs2_extent_list *el;
570 struct ocfs2_extent_rec *rec;
571
572 if (!bg_bh)
573 return;
574
575 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
576 el = &bg->bg_list;
577 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
578 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800579 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
580 cluster_ac->ac_bh,
581 le64_to_cpu(rec->e_blkno),
582 le32_to_cpu(rec->e_leaf_clusters));
583 if (ret)
584 mlog_errno(ret);
585 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800586 }
587
588 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
589 brelse(bg_bh);
590}
591
592static struct buffer_head *
593ocfs2_block_group_alloc_discontig(handle_t *handle,
594 struct inode *alloc_inode,
595 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800596 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800597{
598 int status;
599 u32 bit_off, num_bits;
600 u64 bg_blkno;
601 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
602 struct buffer_head *bg_bh = NULL;
603 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
604 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
605
Tao Ma47119542010-04-22 14:09:15 +0800606 if (!ocfs2_supports_discontig_bg(osb)) {
Joel Becker798db352010-04-13 14:26:32 +0800607 status = -ENOSPC;
608 goto bail;
609 }
610
Joel Becker8b06bc52010-03-26 10:09:29 +0800611 status = ocfs2_extend_trans(handle,
612 ocfs2_calc_bg_discontig_credits(osb->sb));
613 if (status) {
614 mlog_errno(status);
615 goto bail;
616 }
617
Joel Becker95ec0ad2010-03-26 10:10:08 +0800618 /*
619 * We're going to be grabbing from multiple cluster groups.
620 * We don't have enough credits to relink them all, and the
621 * cluster groups will be staying in cache for the duration of
622 * this operation.
623 */
624 ac->ac_allow_chain_relink = 0;
625
Joel Becker798db352010-04-13 14:26:32 +0800626 /* Claim the first region */
627 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
628 &bit_off, &num_bits);
629 if (status < 0) {
630 if (status != -ENOSPC)
631 mlog_errno(status);
632 goto bail;
633 }
634 min_bits = num_bits;
635
636 /* setup the group */
637 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
638 mlog(0, "new descriptor, record %u, at block %llu\n",
639 alloc_rec, (unsigned long long)bg_blkno);
640
641 bg_bh = sb_getblk(osb->sb, bg_blkno);
642 if (!bg_bh) {
643 status = -EIO;
644 mlog_errno(status);
645 goto bail;
646 }
647 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
648
649 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
650 bg_blkno, num_bits, alloc_rec, cl);
651 if (status < 0) {
652 mlog_errno(status);
653 goto bail;
654 }
655
656 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
657 bg_bh, ac, cl, min_bits);
658 if (status)
659 mlog_errno(status);
660
661bail:
662 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800663 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800664 return status ? ERR_PTR(status) : bg_bh;
665}
666
Mark Fashehccd979b2005-12-15 14:31:24 -0800667/*
668 * We expect the block group allocator to already be locked.
669 */
670static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
671 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700672 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800673 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800674 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800675 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800676{
677 int status, credits;
678 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
679 struct ocfs2_chain_list *cl;
680 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700681 handle_t *handle = NULL;
Tao Ma47119542010-04-22 14:09:15 +0800682 u16 alloc_rec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800683 struct buffer_head *bg_bh = NULL;
684 struct ocfs2_group_desc *bg;
685
686 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
687
688 mlog_entry_void();
689
Mark Fashehccd979b2005-12-15 14:31:24 -0800690 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700691 status = ocfs2_reserve_clusters_with_limit(osb,
692 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800693 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800694 if (status < 0) {
695 if (status != -ENOSPC)
696 mlog_errno(status);
697 goto bail;
698 }
699
700 credits = ocfs2_calc_group_alloc_credits(osb->sb,
701 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700702 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800703 if (IS_ERR(handle)) {
704 status = PTR_ERR(handle);
705 handle = NULL;
706 mlog_errno(status);
707 goto bail;
708 }
709
Tao Mafeb473a2009-02-25 00:53:25 +0800710 if (last_alloc_group && *last_alloc_group != 0) {
711 mlog(0, "use old allocation group %llu for block group alloc\n",
712 (unsigned long long)*last_alloc_group);
713 ac->ac_last_group = *last_alloc_group;
714 }
Joel Becker798db352010-04-13 14:26:32 +0800715
716 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
717 ac, cl);
718 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
719 bg_bh = ocfs2_block_group_alloc_discontig(handle,
720 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800721 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800722 if (IS_ERR(bg_bh)) {
723 status = PTR_ERR(bg_bh);
724 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800725 if (status != -ENOSPC)
726 mlog_errno(status);
727 goto bail;
728 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800729 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
730
Joel Becker0cf2f762009-02-12 16:41:25 -0800731 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700732 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800733 if (status < 0) {
734 mlog_errno(status);
735 goto bail;
736 }
737
Tao Ma47119542010-04-22 14:09:15 +0800738 alloc_rec = le16_to_cpu(bg->bg_chain);
739 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800740 le16_to_cpu(bg->bg_free_bits_count));
Tao Ma47119542010-04-22 14:09:15 +0800741 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
Joel Becker798db352010-04-13 14:26:32 +0800742 le16_to_cpu(bg->bg_bits));
Tao Ma47119542010-04-22 14:09:15 +0800743 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800744 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
745 le16_add_cpu(&cl->cl_next_free_rec, 1);
746
747 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
748 le16_to_cpu(bg->bg_free_bits_count));
749 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
750 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
751
Joel Beckerec20cec2010-03-19 14:13:52 -0700752 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800753
754 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
755 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
756 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
757 le32_to_cpu(fe->i_clusters)));
758 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
759 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700760 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800761
762 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800763
764 /* save the new last alloc group so that the caller can cache it. */
765 if (last_alloc_group)
766 *last_alloc_group = ac->ac_last_group;
767
Mark Fashehccd979b2005-12-15 14:31:24 -0800768bail:
769 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700770 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800771
772 if (ac)
773 ocfs2_free_alloc_context(ac);
774
Mark Fasheha81cb882008-10-07 14:25:16 -0700775 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800776
777 mlog_exit(status);
778 return status;
779}
780
781static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700782 struct ocfs2_alloc_context *ac,
783 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800784 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800785 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800786 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800787{
788 int status;
789 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700790 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800791 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800792 struct ocfs2_dinode *fe;
793 u32 free_bits;
794
795 mlog_entry_void();
796
Mark Fashehda5cbf22006-10-06 18:34:35 -0700797 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
798 if (!alloc_inode) {
799 mlog_errno(-EINVAL);
800 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800801 }
802
Mark Fashehda5cbf22006-10-06 18:34:35 -0700803 mutex_lock(&alloc_inode->i_mutex);
804
Mark Fashehe63aecb62007-10-18 15:30:42 -0700805 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700806 if (status < 0) {
807 mutex_unlock(&alloc_inode->i_mutex);
808 iput(alloc_inode);
809
810 mlog_errno(status);
811 return status;
812 }
813
814 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800815 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700816
Mark Fashehccd979b2005-12-15 14:31:24 -0800817 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800818
819 /* The bh was validated by the inode read inside
820 * ocfs2_inode_lock(). Any corruption is a code bug. */
821 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
822
Mark Fashehccd979b2005-12-15 14:31:24 -0800823 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800824 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
825 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800826 status = -EIO;
827 goto bail;
828 }
829
830 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
831 le32_to_cpu(fe->id1.bitmap1.i_used);
832
833 if (bits_wanted > free_bits) {
834 /* cluster bitmap never grows */
835 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
836 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
837 bits_wanted, free_bits);
838 status = -ENOSPC;
839 goto bail;
840 }
841
Tao Ma60ca81e2009-02-25 00:53:24 +0800842 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800843 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
844 "and we don't alloc a new group for it.\n",
845 slot, bits_wanted, free_bits);
846 status = -ENOSPC;
847 goto bail;
848 }
849
Joel Becker1187c962008-09-03 20:03:39 -0700850 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800851 ac->ac_max_block,
852 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 if (status < 0) {
854 if (status != -ENOSPC)
855 mlog_errno(status);
856 goto bail;
857 }
858 atomic_inc(&osb->alloc_stats.bg_extends);
859
860 /* You should never ask for this much metadata */
861 BUG_ON(bits_wanted >
862 (le32_to_cpu(fe->id1.bitmap1.i_total)
863 - le32_to_cpu(fe->id1.bitmap1.i_used)));
864 }
865
866 get_bh(bh);
867 ac->ac_bh = bh;
868bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700869 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800870
871 mlog_exit(status);
872 return status;
873}
874
Tiger Yangb89c5422010-01-25 14:11:06 +0800875static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
876{
877 spin_lock(&osb->osb_lock);
878 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
879 spin_unlock(&osb->osb_lock);
880 atomic_set(&osb->s_num_inodes_stolen, 0);
881}
882
883static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
884{
885 spin_lock(&osb->osb_lock);
886 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
887 spin_unlock(&osb->osb_lock);
888 atomic_set(&osb->s_num_meta_stolen, 0);
889}
890
891void ocfs2_init_steal_slots(struct ocfs2_super *osb)
892{
893 ocfs2_init_inode_steal_slot(osb);
894 ocfs2_init_meta_steal_slot(osb);
895}
896
897static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
898{
899 spin_lock(&osb->osb_lock);
900 if (type == INODE_ALLOC_SYSTEM_INODE)
901 osb->s_inode_steal_slot = slot;
902 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
903 osb->s_meta_steal_slot = slot;
904 spin_unlock(&osb->osb_lock);
905}
906
907static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
908{
909 int slot = OCFS2_INVALID_SLOT;
910
911 spin_lock(&osb->osb_lock);
912 if (type == INODE_ALLOC_SYSTEM_INODE)
913 slot = osb->s_inode_steal_slot;
914 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
915 slot = osb->s_meta_steal_slot;
916 spin_unlock(&osb->osb_lock);
917
918 return slot;
919}
920
921static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
922{
923 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
924}
925
926static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
927{
928 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
929}
930
931static int ocfs2_steal_resource(struct ocfs2_super *osb,
932 struct ocfs2_alloc_context *ac,
933 int type)
934{
935 int i, status = -ENOSPC;
936 int slot = __ocfs2_get_steal_slot(osb, type);
937
938 /* Start to steal resource from the first slot after ours. */
939 if (slot == OCFS2_INVALID_SLOT)
940 slot = osb->slot_num + 1;
941
942 for (i = 0; i < osb->max_slots; i++, slot++) {
943 if (slot == osb->max_slots)
944 slot = 0;
945
946 if (slot == osb->slot_num)
947 continue;
948
949 status = ocfs2_reserve_suballoc_bits(osb, ac,
950 type,
951 (u32)slot, NULL,
952 NOT_ALLOC_NEW_GROUP);
953 if (status >= 0) {
954 __ocfs2_set_steal_slot(osb, slot, type);
955 break;
956 }
957
958 ocfs2_free_ac_resource(ac);
959 }
960
961 return status;
962}
963
964static int ocfs2_steal_inode(struct ocfs2_super *osb,
965 struct ocfs2_alloc_context *ac)
966{
967 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
968}
969
970static int ocfs2_steal_meta(struct ocfs2_super *osb,
971 struct ocfs2_alloc_context *ac)
972{
973 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
974}
975
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800976int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
977 int blocks,
978 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800979{
980 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800981 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800982
Robert P. J. Daycd861282006-12-13 00:34:52 -0800983 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800984 if (!(*ac)) {
985 status = -ENOMEM;
986 mlog_errno(status);
987 goto bail;
988 }
989
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800990 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800991 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 (*ac)->ac_group_search = ocfs2_block_group_search;
993
Tiger Yangb89c5422010-01-25 14:11:06 +0800994 if (slot != OCFS2_INVALID_SLOT &&
995 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
996 goto extent_steal;
997
998 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700999 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +08001000 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001001 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -08001002 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +08001003
1004
1005 if (status >= 0) {
1006 status = 0;
1007 if (slot != OCFS2_INVALID_SLOT)
1008 ocfs2_init_meta_steal_slot(osb);
1009 goto bail;
1010 } else if (status < 0 && status != -ENOSPC) {
1011 mlog_errno(status);
1012 goto bail;
1013 }
1014
1015 ocfs2_free_ac_resource(*ac);
1016
1017extent_steal:
1018 status = ocfs2_steal_meta(osb, *ac);
1019 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001020 if (status < 0) {
1021 if (status != -ENOSPC)
1022 mlog_errno(status);
1023 goto bail;
1024 }
1025
1026 status = 0;
1027bail:
1028 if ((status < 0) && *ac) {
1029 ocfs2_free_alloc_context(*ac);
1030 *ac = NULL;
1031 }
1032
Mark Fashehccd979b2005-12-15 14:31:24 -08001033 mlog_exit(status);
1034 return status;
1035}
1036
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001037int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1038 struct ocfs2_extent_list *root_el,
1039 struct ocfs2_alloc_context **ac)
1040{
1041 return ocfs2_reserve_new_metadata_blocks(osb,
1042 ocfs2_extend_meta_needed(root_el),
1043 ac);
1044}
1045
Mark Fashehccd979b2005-12-15 14:31:24 -08001046int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001047 struct ocfs2_alloc_context **ac)
1048{
1049 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001050 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001051 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001052
Robert P. J. Daycd861282006-12-13 00:34:52 -08001053 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 if (!(*ac)) {
1055 status = -ENOMEM;
1056 mlog_errno(status);
1057 goto bail;
1058 }
1059
1060 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1062
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 (*ac)->ac_group_search = ocfs2_block_group_search;
1064
Tao Ma4d0ddb22008-03-05 16:11:46 +08001065 /*
Joel Becker1187c962008-09-03 20:03:39 -07001066 * stat(2) can't handle i_ino > 32bits, so we tell the
1067 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001068 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001069 */
Joel Becker12462f12008-09-03 20:03:40 -07001070 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1071 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001072
1073 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001074 * slot is set when we successfully steal inode from other nodes.
1075 * It is reset in 3 places:
1076 * 1. when we flush the truncate log
1077 * 2. when we complete local alloc recovery.
1078 * 3. when we successfully allocate from our own slot.
1079 * After it is set, we will go on stealing inodes until we find the
1080 * need to check our slots to see whether there is some space for us.
1081 */
1082 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001083 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001084 goto inode_steal;
1085
1086 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001087 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001088 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1089 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001090 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001091 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001092 ALLOC_NEW_GROUP |
1093 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001094 if (status >= 0) {
1095 status = 0;
1096
Tao Mafeb473a2009-02-25 00:53:25 +08001097 spin_lock(&osb->osb_lock);
1098 osb->osb_inode_alloc_group = alloc_group;
1099 spin_unlock(&osb->osb_lock);
1100 mlog(0, "after reservation, new allocation group is "
1101 "%llu\n", (unsigned long long)alloc_group);
1102
Tao Ma4d0ddb22008-03-05 16:11:46 +08001103 /*
1104 * Some inodes must be freed by us, so try to allocate
1105 * from our own next time.
1106 */
1107 if (slot != OCFS2_INVALID_SLOT)
1108 ocfs2_init_inode_steal_slot(osb);
1109 goto bail;
1110 } else if (status < 0 && status != -ENOSPC) {
1111 mlog_errno(status);
1112 goto bail;
1113 }
1114
1115 ocfs2_free_ac_resource(*ac);
1116
1117inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001118 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001119 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001120 if (status < 0) {
1121 if (status != -ENOSPC)
1122 mlog_errno(status);
1123 goto bail;
1124 }
1125
1126 status = 0;
1127bail:
1128 if ((status < 0) && *ac) {
1129 ocfs2_free_alloc_context(*ac);
1130 *ac = NULL;
1131 }
1132
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 mlog_exit(status);
1134 return status;
1135}
1136
1137/* local alloc code has to do the same thing, so rather than do this
1138 * twice.. */
1139int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1140 struct ocfs2_alloc_context *ac)
1141{
1142 int status;
1143
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 ac->ac_which = OCFS2_AC_USE_MAIN;
1145 ac->ac_group_search = ocfs2_cluster_group_search;
1146
Mark Fashehda5cbf22006-10-06 18:34:35 -07001147 status = ocfs2_reserve_suballoc_bits(osb, ac,
1148 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001149 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001150 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001151 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001153 goto bail;
1154 }
1155
Mark Fashehccd979b2005-12-15 14:31:24 -08001156bail:
1157 return status;
1158}
1159
1160/* Callers don't need to care which bitmap (local alloc or main) to
1161 * use so we figure it out for them, but unfortunately this clutters
1162 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001163static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1164 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001165 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001166 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001167{
1168 int status;
1169
1170 mlog_entry_void();
1171
Robert P. J. Daycd861282006-12-13 00:34:52 -08001172 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001173 if (!(*ac)) {
1174 status = -ENOMEM;
1175 mlog_errno(status);
1176 goto bail;
1177 }
1178
1179 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001180 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001181
1182 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001183 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1184 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001185 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 bits_wanted,
1187 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001188 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001189 mlog_errno(status);
1190 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 }
1192 }
1193
1194 if (status == -ENOSPC) {
1195 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1196 if (status < 0) {
1197 if (status != -ENOSPC)
1198 mlog_errno(status);
1199 goto bail;
1200 }
1201 }
1202
1203 status = 0;
1204bail:
1205 if ((status < 0) && *ac) {
1206 ocfs2_free_alloc_context(*ac);
1207 *ac = NULL;
1208 }
1209
1210 mlog_exit(status);
1211 return status;
1212}
1213
Joel Becker1187c962008-09-03 20:03:39 -07001214int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1215 u32 bits_wanted,
1216 struct ocfs2_alloc_context **ac)
1217{
Tao Ma60ca81e2009-02-25 00:53:24 +08001218 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1219 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001220}
1221
Mark Fashehccd979b2005-12-15 14:31:24 -08001222/*
1223 * More or less lifted from ext3. I'll leave their description below:
1224 *
1225 * "For ext3 allocations, we must not reuse any blocks which are
1226 * allocated in the bitmap buffer's "last committed data" copy. This
1227 * prevents deletes from freeing up the page for reuse until we have
1228 * committed the delete transaction.
1229 *
1230 * If we didn't do this, then deleting something and reallocating it as
1231 * data would allow the old block to be overwritten before the
1232 * transaction committed (because we force data to disk before commit).
1233 * This would lead to corruption if we crashed between overwriting the
1234 * data and committing the delete.
1235 *
1236 * @@@ We may want to make this allocation behaviour conditional on
1237 * data-writes at some point, and disable it for metadata allocations or
1238 * sync-data inodes."
1239 *
1240 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001241 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 * called on a metadata group descriptor.
1243 */
1244static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1245 int nr)
1246{
1247 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001248 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001249
1250 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1251 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001252
1253 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 return 1;
1255
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001256 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001258 if (bg)
1259 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1260 else
1261 ret = 1;
1262 jbd_unlock_bh_state(bg_bh);
1263
1264 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001265}
1266
1267static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1268 struct buffer_head *bg_bh,
1269 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001270 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001271 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001272{
1273 void *bitmap;
1274 u16 best_offset, best_size;
1275 int offset, start, found, status = 0;
1276 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1277
Joel Becker42035302008-11-13 14:49:15 -08001278 /* Callers got this descriptor from
1279 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1280 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001281
1282 found = start = best_offset = best_size = 0;
1283 bitmap = bg->bg_bitmap;
1284
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001285 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1286 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 break;
1288
1289 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1290 /* We found a zero, but we can't use it as it
1291 * hasn't been put to disk yet! */
1292 found = 0;
1293 start = offset + 1;
1294 } else if (offset == start) {
1295 /* we found a zero */
1296 found++;
1297 /* move start to the next bit to test */
1298 start++;
1299 } else {
1300 /* got a zero after some ones */
1301 found = 1;
1302 start = offset + 1;
1303 }
1304 if (found > best_size) {
1305 best_size = found;
1306 best_offset = start - found;
1307 }
1308 /* we got everything we needed */
1309 if (found == bits_wanted) {
1310 /* mlog(0, "Found it all!\n"); */
1311 break;
1312 }
1313 }
1314
Joel Becker7d1fe092010-04-13 14:30:19 +08001315 if (best_size) {
1316 res->sr_bit_offset = best_offset;
1317 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001318 } else {
1319 status = -ENOSPC;
1320 /* No error log here -- see the comment above
1321 * ocfs2_test_bg_bit_allocatable */
1322 }
1323
1324 return status;
1325}
1326
Mark Fasheh1fabe142006-10-09 18:11:45 -07001327static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 struct inode *alloc_inode,
1329 struct ocfs2_group_desc *bg,
1330 struct buffer_head *group_bh,
1331 unsigned int bit_off,
1332 unsigned int num_bits)
1333{
1334 int status;
1335 void *bitmap = bg->bg_bitmap;
1336 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1337
1338 mlog_entry_void();
1339
Joel Becker42035302008-11-13 14:49:15 -08001340 /* All callers get the descriptor via
1341 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1342 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1344
1345 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1346 num_bits);
1347
1348 if (ocfs2_is_cluster_bitmap(alloc_inode))
1349 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1350
Joel Becker13723d02008-10-17 19:25:01 -07001351 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001352 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001353 group_bh,
1354 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001355 if (status < 0) {
1356 mlog_errno(status);
1357 goto bail;
1358 }
1359
1360 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001361 while(num_bits--)
1362 ocfs2_set_bit(bit_off++, bitmap);
1363
Joel Beckerec20cec2010-03-19 14:13:52 -07001364 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001365
1366bail:
1367 mlog_exit(status);
1368 return status;
1369}
1370
1371/* find the one with the most empty bits */
1372static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1373{
1374 u16 curr, best;
1375
1376 BUG_ON(!cl->cl_next_free_rec);
1377
1378 best = curr = 0;
1379 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1380 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1381 le32_to_cpu(cl->cl_recs[best].c_free))
1382 best = curr;
1383 curr++;
1384 }
1385
1386 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1387 return best;
1388}
1389
Mark Fasheh1fabe142006-10-09 18:11:45 -07001390static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 struct inode *alloc_inode,
1392 struct buffer_head *fe_bh,
1393 struct buffer_head *bg_bh,
1394 struct buffer_head *prev_bg_bh,
1395 u16 chain)
1396{
1397 int status;
1398 /* there is a really tiny chance the journal calls could fail,
1399 * but we wouldn't want inconsistent blocks in *any* case. */
1400 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1401 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1402 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1403 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1404
Joel Becker42035302008-11-13 14:49:15 -08001405 /* The caller got these descriptors from
1406 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1407 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1408 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001409
Mark Fashehb06970532006-03-03 10:24:33 -08001410 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001411 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1412 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1413 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001414
1415 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1416 bg_ptr = le64_to_cpu(bg->bg_next_group);
1417 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1418
Joel Becker0cf2f762009-02-12 16:41:25 -08001419 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1420 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001421 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 if (status < 0) {
1423 mlog_errno(status);
1424 goto out_rollback;
1425 }
1426
1427 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001428 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001429
Joel Becker0cf2f762009-02-12 16:41:25 -08001430 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1431 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001432 if (status < 0) {
1433 mlog_errno(status);
1434 goto out_rollback;
1435 }
1436
1437 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001438 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001439
Joel Becker0cf2f762009-02-12 16:41:25 -08001440 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1441 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442 if (status < 0) {
1443 mlog_errno(status);
1444 goto out_rollback;
1445 }
1446
1447 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001448 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001449
Mark Fashehccd979b2005-12-15 14:31:24 -08001450out_rollback:
1451 if (status < 0) {
1452 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1453 bg->bg_next_group = cpu_to_le64(bg_ptr);
1454 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1455 }
Joel Becker42035302008-11-13 14:49:15 -08001456
Mark Fashehccd979b2005-12-15 14:31:24 -08001457 mlog_exit(status);
1458 return status;
1459}
1460
1461static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1462 u32 wanted)
1463{
1464 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1465}
1466
1467/* return 0 on success, -ENOSPC to keep searching and any other < 0
1468 * value on error. */
1469static int ocfs2_cluster_group_search(struct inode *inode,
1470 struct buffer_head *group_bh,
1471 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001472 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001473 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001474{
1475 int search = -ENOSPC;
1476 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001477 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001478 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001479 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001480 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001481
1482 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1483
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001484 if (gd->bg_free_bits_count) {
1485 max_bits = le16_to_cpu(gd->bg_bits);
1486
1487 /* Tail groups in cluster bitmaps which aren't cpg
1488 * aligned are prone to partial extention by a failed
1489 * fs resize. If the file system resize never got to
1490 * update the dinode cluster count, then we don't want
1491 * to trust any clusters past it, regardless of what
1492 * the group descriptor says. */
1493 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1494 le64_to_cpu(gd->bg_blkno));
1495 if ((gd_cluster_off + max_bits) >
1496 OCFS2_I(inode)->ip_clusters) {
1497 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1498 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1499 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1500 le16_to_cpu(gd->bg_bits),
1501 OCFS2_I(inode)->ip_clusters, max_bits);
1502 }
1503
Mark Fashehccd979b2005-12-15 14:31:24 -08001504 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1505 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001506 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001507 if (ret)
1508 return ret;
1509
Joel Becker1187c962008-09-03 20:03:39 -07001510 if (max_block) {
1511 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1512 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001513 res->sr_bit_offset +
1514 res->sr_bits);
Joel Becker1187c962008-09-03 20:03:39 -07001515 mlog(0, "Checking %llu against %llu\n",
1516 (unsigned long long)blkoff,
1517 (unsigned long long)max_block);
1518 if (blkoff > max_block)
1519 return -ENOSPC;
1520 }
1521
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 /* ocfs2_block_group_find_clear_bits() might
1523 * return success, but we still want to return
1524 * -ENOSPC unless it found the minimum number
1525 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001526 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001527 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001528 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001529 /*
1530 * Don't show bits which we'll be returning
1531 * for allocation to the local alloc bitmap.
1532 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001533 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 }
1535 }
1536
1537 return search;
1538}
1539
1540static int ocfs2_block_group_search(struct inode *inode,
1541 struct buffer_head *group_bh,
1542 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001543 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001544 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001545{
1546 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001547 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001548 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1549
1550 BUG_ON(min_bits != 1);
1551 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1552
Joel Becker1187c962008-09-03 20:03:39 -07001553 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001554 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1555 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001556 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001557 res);
Joel Becker1187c962008-09-03 20:03:39 -07001558 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001559 blkoff = le64_to_cpu(bg->bg_blkno) +
1560 res->sr_bit_offset + res->sr_bits;
Joel Becker1187c962008-09-03 20:03:39 -07001561 mlog(0, "Checking %llu against %llu\n",
1562 (unsigned long long)blkoff,
1563 (unsigned long long)max_block);
1564 if (blkoff > max_block)
1565 ret = -ENOSPC;
1566 }
1567 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001568
1569 return ret;
1570}
1571
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001572static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001573 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001574 struct buffer_head *di_bh,
1575 u32 num_bits,
1576 u16 chain)
1577{
1578 int ret;
1579 u32 tmp_used;
1580 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1581 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1582
Joel Becker0cf2f762009-02-12 16:41:25 -08001583 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001584 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001585 if (ret < 0) {
1586 mlog_errno(ret);
1587 goto out;
1588 }
1589
1590 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1591 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1592 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001593 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001594
1595out:
1596 return ret;
1597}
1598
Joel Beckerba206632010-03-26 10:08:59 +08001599static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1600 struct ocfs2_extent_rec *rec,
1601 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001602{
1603 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1604 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1605 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1606
1607 if (res->sr_bit_offset < bitoff)
1608 return 0;
1609 if (res->sr_bit_offset >= (bitoff + bitcount))
1610 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001611 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1612 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001613 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1614 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1615 return 1;
1616}
1617
Joel Beckerba206632010-03-26 10:08:59 +08001618static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1619 struct ocfs2_group_desc *bg,
1620 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001621{
1622 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001623 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001624 struct ocfs2_extent_rec *rec;
1625 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1626 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1627
Joel Beckerba206632010-03-26 10:08:59 +08001628 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1629 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001630 return;
Joel Beckerba206632010-03-26 10:08:59 +08001631 }
Joel Becker13e434c2010-03-26 10:08:27 +08001632
Joel Beckerba206632010-03-26 10:08:59 +08001633 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001634 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Tao Ma47119542010-04-22 14:09:15 +08001635 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
Joel Beckerba206632010-03-26 10:08:59 +08001636 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001637 return;
1638
1639 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1640 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001641 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1642 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001643 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001644 }
Joel Becker13e434c2010-03-26 10:08:27 +08001645 }
1646}
1647
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001648static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001649 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001650 u32 bits_wanted,
1651 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001652 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001653 u16 *bits_left)
1654{
1655 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001656 struct buffer_head *group_bh = NULL;
1657 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001658 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001659 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001660
Joel Becker7d1fe092010-04-13 14:30:19 +08001661 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1662 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001663 if (ret < 0) {
1664 mlog_errno(ret);
1665 return ret;
1666 }
1667
1668 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001669 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001670 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001671 if (ret < 0) {
1672 if (ret != -ENOSPC)
1673 mlog_errno(ret);
1674 goto out;
1675 }
1676
Joel Becker13e434c2010-03-26 10:08:27 +08001677 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001678 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001679
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001680 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001681 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001682 le16_to_cpu(gd->bg_chain));
1683 if (ret < 0) {
1684 mlog_errno(ret);
1685 goto out;
1686 }
1687
1688 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001689 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001690 if (ret < 0)
1691 mlog_errno(ret);
1692
1693 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1694
1695out:
1696 brelse(group_bh);
1697
1698 return ret;
1699}
1700
Mark Fashehccd979b2005-12-15 14:31:24 -08001701static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001702 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001703 u32 bits_wanted,
1704 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001705 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001706 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001707{
1708 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001709 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001710 u32 tmp_used;
1711 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001712 struct inode *alloc_inode = ac->ac_inode;
1713 struct buffer_head *group_bh = NULL;
1714 struct buffer_head *prev_group_bh = NULL;
1715 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1716 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1717 struct ocfs2_group_desc *bg;
1718
1719 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001720 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1721 bits_wanted, chain,
1722 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001723
Joel Becker68f64d42008-11-13 14:49:14 -08001724 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1725 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1726 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001727 if (status < 0) {
1728 mlog_errno(status);
1729 goto bail;
1730 }
1731 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001732
1733 status = -ENOSPC;
1734 /* for now, the chain search is a bit simplistic. We just use
1735 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001736 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1737 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001738 ac->ac_max_block,
1739 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001740 if (!bg->bg_next_group)
1741 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001742
1743 brelse(prev_group_bh);
1744 prev_group_bh = NULL;
1745
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 next_group = le64_to_cpu(bg->bg_next_group);
1747 prev_group_bh = group_bh;
1748 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001749 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1750 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001751 if (status < 0) {
1752 mlog_errno(status);
1753 goto bail;
1754 }
1755 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001756 }
1757 if (status < 0) {
1758 if (status != -ENOSPC)
1759 mlog_errno(status);
1760 goto bail;
1761 }
1762
Mark Fashehb06970532006-03-03 10:24:33 -08001763 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001764 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001765
Joel Becker7d1fe092010-04-13 14:30:19 +08001766 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001767
Joel Becker7d1fe092010-04-13 14:30:19 +08001768 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001769 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001770 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001771
Mark Fashehccd979b2005-12-15 14:31:24 -08001772
1773 /*
1774 * Keep track of previous block descriptor read. When
1775 * we find a target, if we have read more than X
1776 * number of descriptors, and the target is reasonably
1777 * empty, relink him to top of his chain.
1778 *
1779 * We've read 0 extra blocks and only send one more to
1780 * the transaction, yet the next guy to search has a
1781 * much easier time.
1782 *
1783 * Do this *after* figuring out how many bits we're taking out
1784 * of our target group.
1785 */
1786 if (ac->ac_allow_chain_relink &&
1787 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001788 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001789 status = ocfs2_relink_block_group(handle, alloc_inode,
1790 ac->ac_bh, group_bh,
1791 prev_group_bh, chain);
1792 if (status < 0) {
1793 mlog_errno(status);
1794 goto bail;
1795 }
1796 }
1797
1798 /* Ok, claim our bits now: set the info on dinode, chainlist
1799 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001800 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001801 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001802 ac->ac_bh,
1803 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001804 if (status < 0) {
1805 mlog_errno(status);
1806 goto bail;
1807 }
1808
1809 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001810 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1811 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001812 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001813
1814 status = ocfs2_block_group_set_bits(handle,
1815 alloc_inode,
1816 bg,
1817 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001818 res->sr_bit_offset,
1819 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001820 if (status < 0) {
1821 mlog_errno(status);
1822 goto bail;
1823 }
1824
Joel Becker7d1fe092010-04-13 14:30:19 +08001825 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001826 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001827
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001828 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001829bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001830 brelse(group_bh);
1831 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001832
1833 mlog_exit(status);
1834 return status;
1835}
1836
1837/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001838static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001839 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001840 u32 bits_wanted,
1841 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001842 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001843{
1844 int status;
1845 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001846 u16 bits_left = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001847 struct ocfs2_chain_list *cl;
1848 struct ocfs2_dinode *fe;
1849
1850 mlog_entry_void();
1851
1852 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1853 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1854 BUG_ON(!ac->ac_bh);
1855
1856 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001857
1858 /* The bh was validated by the inode read during
1859 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1860 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1861
Mark Fashehccd979b2005-12-15 14:31:24 -08001862 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1863 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001864 ocfs2_error(ac->ac_inode->i_sb,
1865 "Chain allocator dinode %llu has %u used "
Mark Fashehb06970532006-03-03 10:24:33 -08001866 "bits but only %u total.",
1867 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001868 le32_to_cpu(fe->id1.bitmap1.i_used),
1869 le32_to_cpu(fe->id1.bitmap1.i_total));
1870 status = -EIO;
1871 goto bail;
1872 }
1873
Joel Becker7d1fe092010-04-13 14:30:19 +08001874 res->sr_bg_blkno = ac->ac_last_group;
1875 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001876 /* Attempt to short-circuit the usual search mechanism
1877 * by jumping straight to the most recently used
1878 * allocation group. This helps us mantain some
1879 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001880 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001881 min_bits, res, &bits_left);
1882 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001883 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001884 if (status < 0 && status != -ENOSPC) {
1885 mlog_errno(status);
1886 goto bail;
1887 }
1888 }
1889
Mark Fashehccd979b2005-12-15 14:31:24 -08001890 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1891
1892 victim = ocfs2_find_victim_chain(cl);
1893 ac->ac_chain = victim;
1894 ac->ac_allow_chain_relink = 1;
1895
Joel Becker7d1fe092010-04-13 14:30:19 +08001896 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1897 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001898 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001899 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 if (status < 0 && status != -ENOSPC) {
1901 mlog_errno(status);
1902 goto bail;
1903 }
1904
1905 mlog(0, "Search of victim chain %u came up with nothing, "
1906 "trying all chains now.\n", victim);
1907
1908 /* If we didn't pick a good victim, then just default to
1909 * searching each chain in order. Don't allow chain relinking
1910 * because we only calculate enough journal credits for one
1911 * relink per alloc. */
1912 ac->ac_allow_chain_relink = 0;
1913 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1914 if (i == victim)
1915 continue;
1916 if (!cl->cl_recs[i].c_free)
1917 continue;
1918
1919 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001920 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001921 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001922 if (!status)
1923 break;
1924 if (status < 0 && status != -ENOSPC) {
1925 mlog_errno(status);
1926 goto bail;
1927 }
1928 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001929
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001930set_hint:
1931 if (status != -ENOSPC) {
1932 /* If the next search of this group is not likely to
1933 * yield a suitable extent, then we reset the last
1934 * group hint so as to not waste a disk read */
1935 if (bits_left < min_bits)
1936 ac->ac_last_group = 0;
1937 else
Joel Becker7d1fe092010-04-13 14:30:19 +08001938 ac->ac_last_group = res->sr_bg_blkno;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001939 }
1940
1941bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001942 mlog_exit(status);
1943 return status;
1944}
1945
Joel Becker1ed9b772010-05-06 13:59:06 +08001946int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001947 struct ocfs2_alloc_context *ac,
1948 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001949 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001950 u16 *suballoc_bit_start,
1951 unsigned int *num_bits,
1952 u64 *blkno_start)
1953{
1954 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001955 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001956
1957 BUG_ON(!ac);
1958 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1959 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001960
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001961 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001962 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001963 bits_wanted,
1964 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001965 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 if (status < 0) {
1967 mlog_errno(status);
1968 goto bail;
1969 }
Joel Becker1ed9b772010-05-06 13:59:06 +08001970 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001971
Joel Becker2b6cb572010-03-26 10:09:15 +08001972 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001973 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08001974 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001975 ac->ac_bits_given += res.sr_bits;
1976 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001977 status = 0;
1978bail:
1979 mlog_exit(status);
1980 return status;
1981}
1982
Tao Ma13821152009-02-25 00:53:23 +08001983static void ocfs2_init_inode_ac_group(struct inode *dir,
1984 struct buffer_head *parent_fe_bh,
1985 struct ocfs2_alloc_context *ac)
1986{
1987 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1988 /*
1989 * Try to allocate inodes from some specific group.
1990 *
1991 * If the parent dir has recorded the last group used in allocation,
1992 * cool, use it. Otherwise if we try to allocate new inode from the
1993 * same slot the parent dir belongs to, use the same chunk.
1994 *
1995 * We are very careful here to avoid the mistake of setting
1996 * ac_last_group to a group descriptor from a different (unlocked) slot.
1997 */
1998 if (OCFS2_I(dir)->ip_last_used_group &&
1999 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2000 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
2001 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
2002 ac->ac_last_group = ocfs2_which_suballoc_group(
2003 le64_to_cpu(fe->i_blkno),
2004 le16_to_cpu(fe->i_suballoc_bit));
2005}
2006
2007static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2008 struct ocfs2_alloc_context *ac)
2009{
2010 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2011 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2012}
2013
Joel Becker1ed9b772010-05-06 13:59:06 +08002014int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002015 struct inode *dir,
2016 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002017 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002018 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002019 u16 *suballoc_bit,
2020 u64 *fe_blkno)
2021{
2022 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002023 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002024
2025 mlog_entry_void();
2026
2027 BUG_ON(!ac);
2028 BUG_ON(ac->ac_bits_given != 0);
2029 BUG_ON(ac->ac_bits_wanted != 1);
2030 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002031
Tao Ma13821152009-02-25 00:53:23 +08002032 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2033
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002034 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002035 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002036 1,
2037 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002038 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002039 if (status < 0) {
2040 mlog_errno(status);
2041 goto bail;
2042 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002043 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002044
Joel Becker7d1fe092010-04-13 14:30:19 +08002045 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002046
Joel Becker2b6cb572010-03-26 10:09:15 +08002047 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002048 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002049 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002050 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002051 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002052 status = 0;
2053bail:
2054 mlog_exit(status);
2055 return status;
2056}
2057
2058/* translate a group desc. blkno and it's bitmap offset into
2059 * disk cluster offset. */
2060static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2061 u64 bg_blkno,
2062 u16 bg_bit_off)
2063{
2064 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2065 u32 cluster = 0;
2066
2067 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2068
2069 if (bg_blkno != osb->first_cluster_group_blkno)
2070 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2071 cluster += (u32) bg_bit_off;
2072 return cluster;
2073}
2074
2075/* given a cluster offset, calculate which block group it belongs to
2076 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002077u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002078{
2079 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2080 u32 group_no;
2081
2082 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2083
2084 group_no = cluster / osb->bitmap_cpg;
2085 if (!group_no)
2086 return osb->first_cluster_group_blkno;
2087 return ocfs2_clusters_to_blocks(inode->i_sb,
2088 group_no * osb->bitmap_cpg);
2089}
2090
2091/* given the block number of a cluster start, calculate which cluster
2092 * group and descriptor bitmap offset that corresponds to. */
2093static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2094 u64 data_blkno,
2095 u64 *bg_blkno,
2096 u16 *bg_bit_off)
2097{
2098 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2099 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2100
2101 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2102
2103 *bg_blkno = ocfs2_which_cluster_group(inode,
2104 data_cluster);
2105
2106 if (*bg_blkno == osb->first_cluster_group_blkno)
2107 *bg_bit_off = (u16) data_cluster;
2108 else
2109 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2110 data_blkno - *bg_blkno);
2111}
2112
2113/*
2114 * min_bits - minimum contiguous chunk from this total allocation we
2115 * can handle. set to what we asked for originally for a full
2116 * contig. allocation, set to '1' to indicate we can deal with extents
2117 * of any size.
2118 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002119int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002120 struct ocfs2_alloc_context *ac,
2121 u32 min_clusters,
2122 u32 max_clusters,
2123 u32 *cluster_start,
2124 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002125{
2126 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002127 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002128 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002129 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002130
2131 mlog_entry_void();
2132
Mark Fashehccd979b2005-12-15 14:31:24 -08002133 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2134
2135 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2136 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002137
2138 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002139 WARN_ON(min_clusters > 1);
2140
Mark Fashehccd979b2005-12-15 14:31:24 -08002141 status = ocfs2_claim_local_alloc_bits(osb,
2142 handle,
2143 ac,
2144 bits_wanted,
2145 cluster_start,
2146 num_clusters);
2147 if (!status)
2148 atomic_inc(&osb->alloc_stats.local_data);
2149 } else {
2150 if (min_clusters > (osb->bitmap_cpg - 1)) {
2151 /* The only paths asking for contiguousness
2152 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002153 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2154 "group bitmap size %u!\n", min_clusters,
2155 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 status = -ENOSPC;
2157 goto bail;
2158 }
2159 /* clamp the current request down to a realistic size. */
2160 if (bits_wanted > (osb->bitmap_cpg - 1))
2161 bits_wanted = osb->bitmap_cpg - 1;
2162
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002163 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002164 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002165 bits_wanted,
2166 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002167 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002168 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002169 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002170 *cluster_start =
2171 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002172 res.sr_bg_blkno,
2173 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002174 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002175 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002176 }
2177 }
2178 if (status < 0) {
2179 if (status != -ENOSPC)
2180 mlog_errno(status);
2181 goto bail;
2182 }
2183
Tao Ma47119542010-04-22 14:09:15 +08002184 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002185
2186bail:
2187 mlog_exit(status);
2188 return status;
2189}
2190
Joel Becker1ed9b772010-05-06 13:59:06 +08002191int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002192 struct ocfs2_alloc_context *ac,
2193 u32 min_clusters,
2194 u32 *cluster_start,
2195 u32 *num_clusters)
2196{
2197 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2198
Joel Becker1ed9b772010-05-06 13:59:06 +08002199 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002200 bits_wanted, cluster_start, num_clusters);
2201}
2202
Mark Fashehb4414ee2010-03-11 18:31:09 -08002203static int ocfs2_block_group_clear_bits(handle_t *handle,
2204 struct inode *alloc_inode,
2205 struct ocfs2_group_desc *bg,
2206 struct buffer_head *group_bh,
2207 unsigned int bit_off,
2208 unsigned int num_bits,
2209 void (*undo_fn)(unsigned int bit,
2210 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002211{
2212 int status;
2213 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002214 struct ocfs2_group_desc *undo_bg = NULL;
2215
2216 mlog_entry_void();
2217
Joel Becker42035302008-11-13 14:49:15 -08002218 /* The caller got this descriptor from
2219 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2220 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002221
2222 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2223
Mark Fashehb4414ee2010-03-11 18:31:09 -08002224 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002225 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002226 group_bh,
2227 undo_fn ?
2228 OCFS2_JOURNAL_ACCESS_UNDO :
2229 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002230 if (status < 0) {
2231 mlog_errno(status);
2232 goto bail;
2233 }
2234
Mark Fashehb4414ee2010-03-11 18:31:09 -08002235 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002236 jbd_lock_bh_state(group_bh);
2237 undo_bg = (struct ocfs2_group_desc *)
2238 bh2jh(group_bh)->b_committed_data;
2239 BUG_ON(!undo_bg);
2240 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002241
2242 tmp = num_bits;
2243 while(tmp--) {
2244 ocfs2_clear_bit((bit_off + tmp),
2245 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002246 if (undo_fn)
2247 undo_fn(bit_off + tmp,
2248 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002249 }
2250 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2251
Mark Fashehb4414ee2010-03-11 18:31:09 -08002252 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002253 jbd_unlock_bh_state(group_bh);
2254
Joel Beckerec20cec2010-03-19 14:13:52 -07002255 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002256bail:
2257 return status;
2258}
2259
2260/*
2261 * expects the suballoc inode to already be locked.
2262 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002263static int _ocfs2_free_suballoc_bits(handle_t *handle,
2264 struct inode *alloc_inode,
2265 struct buffer_head *alloc_bh,
2266 unsigned int start_bit,
2267 u64 bg_blkno,
2268 unsigned int count,
2269 void (*undo_fn)(unsigned int bit,
2270 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002271{
2272 int status = 0;
2273 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002274 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2275 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2276 struct buffer_head *group_bh = NULL;
2277 struct ocfs2_group_desc *group;
2278
2279 mlog_entry_void();
2280
Joel Becker10995aa2008-11-13 14:49:12 -08002281 /* The alloc_bh comes from ocfs2_free_dinode() or
2282 * ocfs2_free_clusters(). The callers have all locked the
2283 * allocator and gotten alloc_bh from the lock call. This
2284 * validates the dinode buffer. Any corruption that has happended
2285 * is a code bug. */
2286 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002287 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2288
Mark Fashehb06970532006-03-03 10:24:33 -08002289 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2290 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2291 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002292
Joel Becker68f64d42008-11-13 14:49:14 -08002293 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2294 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002295 if (status < 0) {
2296 mlog_errno(status);
2297 goto bail;
2298 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002299 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002300
Mark Fashehccd979b2005-12-15 14:31:24 -08002301 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2302
2303 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2304 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002305 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002306 if (status < 0) {
2307 mlog_errno(status);
2308 goto bail;
2309 }
2310
Joel Becker0cf2f762009-02-12 16:41:25 -08002311 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2312 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002313 if (status < 0) {
2314 mlog_errno(status);
2315 goto bail;
2316 }
2317
2318 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2319 count);
2320 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2321 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002322 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002323
2324bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002325 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002326
2327 mlog_exit(status);
2328 return status;
2329}
2330
Mark Fashehb4414ee2010-03-11 18:31:09 -08002331int ocfs2_free_suballoc_bits(handle_t *handle,
2332 struct inode *alloc_inode,
2333 struct buffer_head *alloc_bh,
2334 unsigned int start_bit,
2335 u64 bg_blkno,
2336 unsigned int count)
2337{
2338 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2339 start_bit, bg_blkno, count, NULL);
2340}
2341
Mark Fasheh1fabe142006-10-09 18:11:45 -07002342int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002343 struct inode *inode_alloc_inode,
2344 struct buffer_head *inode_alloc_bh,
2345 struct ocfs2_dinode *di)
2346{
2347 u64 blk = le64_to_cpu(di->i_blkno);
2348 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2349 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2350
2351 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2352 inode_alloc_bh, bit, bg_blkno, 1);
2353}
2354
Mark Fashehb4414ee2010-03-11 18:31:09 -08002355static int _ocfs2_free_clusters(handle_t *handle,
2356 struct inode *bitmap_inode,
2357 struct buffer_head *bitmap_bh,
2358 u64 start_blk,
2359 unsigned int num_clusters,
2360 void (*undo_fn)(unsigned int bit,
2361 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002362{
2363 int status;
2364 u16 bg_start_bit;
2365 u64 bg_blkno;
2366 struct ocfs2_dinode *fe;
2367
2368 /* You can't ever have a contiguous set of clusters
2369 * bigger than a block group bitmap so we never have to worry
2370 * about looping on them. */
2371
2372 mlog_entry_void();
2373
2374 /* This is expensive. We can safely remove once this stuff has
2375 * gotten tested really well. */
2376 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2377
2378 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2379
2380 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2381 &bg_start_bit);
2382
Mark Fashehb06970532006-03-03 10:24:33 -08002383 mlog(0, "want to free %u clusters starting at block %llu\n",
2384 num_clusters, (unsigned long long)start_blk);
2385 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2386 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002387
Mark Fashehb4414ee2010-03-11 18:31:09 -08002388 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2389 bg_start_bit, bg_blkno,
2390 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002391 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002392 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002393 goto out;
2394 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002395
Mark Fasheh9c7af402008-07-28 18:02:53 -07002396 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2397 num_clusters);
2398
2399out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002400 mlog_exit(status);
2401 return status;
2402}
2403
Mark Fashehb4414ee2010-03-11 18:31:09 -08002404int ocfs2_free_clusters(handle_t *handle,
2405 struct inode *bitmap_inode,
2406 struct buffer_head *bitmap_bh,
2407 u64 start_blk,
2408 unsigned int num_clusters)
2409{
2410 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2411 start_blk, num_clusters,
2412 _ocfs2_set_bit);
2413}
2414
2415/*
2416 * Give never-used clusters back to the global bitmap. We don't need
2417 * to protect these bits in the undo buffer.
2418 */
2419int ocfs2_release_clusters(handle_t *handle,
2420 struct inode *bitmap_inode,
2421 struct buffer_head *bitmap_bh,
2422 u64 start_blk,
2423 unsigned int num_clusters)
2424{
2425 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2426 start_blk, num_clusters,
2427 _ocfs2_clear_bit);
2428}
2429
Mark Fashehccd979b2005-12-15 14:31:24 -08002430static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2431{
2432 printk("Block Group:\n");
2433 printk("bg_signature: %s\n", bg->bg_signature);
2434 printk("bg_size: %u\n", bg->bg_size);
2435 printk("bg_bits: %u\n", bg->bg_bits);
2436 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2437 printk("bg_chain: %u\n", bg->bg_chain);
2438 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002439 printk("bg_next_group: %llu\n",
2440 (unsigned long long)bg->bg_next_group);
2441 printk("bg_parent_dinode: %llu\n",
2442 (unsigned long long)bg->bg_parent_dinode);
2443 printk("bg_blkno: %llu\n",
2444 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002445}
2446
2447static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2448{
2449 int i;
2450
Mark Fashehb06970532006-03-03 10:24:33 -08002451 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002452 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002453 printk("i_size: %llu\n",
2454 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002455 printk("i_clusters: %u\n", fe->i_clusters);
2456 printk("i_generation: %u\n",
2457 le32_to_cpu(fe->i_generation));
2458 printk("id1.bitmap1.i_used: %u\n",
2459 le32_to_cpu(fe->id1.bitmap1.i_used));
2460 printk("id1.bitmap1.i_total: %u\n",
2461 le32_to_cpu(fe->id1.bitmap1.i_total));
2462 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2463 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2464 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2465 printk("id2.i_chain.cl_next_free_rec: %u\n",
2466 fe->id2.i_chain.cl_next_free_rec);
2467 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2468 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2469 fe->id2.i_chain.cl_recs[i].c_free);
2470 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2471 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002472 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2473 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002474 }
2475}
Tao Mae7d4cb62008-08-18 17:38:44 +08002476
2477/*
2478 * For a given allocation, determine which allocators will need to be
2479 * accessed, and lock them, reserving the appropriate number of bits.
2480 *
2481 * Sparse file systems call this from ocfs2_write_begin_nolock()
2482 * and ocfs2_allocate_unwritten_extents().
2483 *
2484 * File systems which don't support holes call this from
2485 * ocfs2_extend_allocation().
2486 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002487int ocfs2_lock_allocators(struct inode *inode,
2488 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002489 u32 clusters_to_add, u32 extents_to_split,
2490 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002491 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002492{
2493 int ret = 0, num_free_extents;
2494 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2495 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2496
2497 *meta_ac = NULL;
2498 if (data_ac)
2499 *data_ac = NULL;
2500
2501 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2502
Joel Becker3d03a302009-02-12 17:49:26 -08002503 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002504 if (num_free_extents < 0) {
2505 ret = num_free_extents;
2506 mlog_errno(ret);
2507 goto out;
2508 }
2509
2510 /*
2511 * Sparse allocation file systems need to be more conservative
2512 * with reserving room for expansion - the actual allocation
2513 * happens while we've got a journal handle open so re-taking
2514 * a cluster lock (because we ran out of room for another
2515 * extent) will violate ordering rules.
2516 *
2517 * Most of the time we'll only be seeing this 1 cluster at a time
2518 * anyway.
2519 *
2520 * Always lock for any unwritten extents - we might want to
2521 * add blocks during a split.
2522 */
2523 if (!num_free_extents ||
2524 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002525 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002526 if (ret < 0) {
2527 if (ret != -ENOSPC)
2528 mlog_errno(ret);
2529 goto out;
2530 }
2531 }
2532
2533 if (clusters_to_add == 0)
2534 goto out;
2535
2536 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2537 if (ret < 0) {
2538 if (ret != -ENOSPC)
2539 mlog_errno(ret);
2540 goto out;
2541 }
2542
2543out:
2544 if (ret) {
2545 if (*meta_ac) {
2546 ocfs2_free_alloc_context(*meta_ac);
2547 *meta_ac = NULL;
2548 }
2549
2550 /*
2551 * We cannot have an error and a non null *data_ac.
2552 */
2553 }
2554
2555 return ret;
2556}
wengang wang6ca497a2009-03-06 21:29:10 +08002557
2558/*
2559 * Read the inode specified by blkno to get suballoc_slot and
2560 * suballoc_bit.
2561 */
2562static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2563 u16 *suballoc_slot, u16 *suballoc_bit)
2564{
2565 int status;
2566 struct buffer_head *inode_bh = NULL;
2567 struct ocfs2_dinode *inode_fe;
2568
Joel Becker5b09b502009-04-21 16:31:20 -07002569 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002570
2571 /* dirty read disk */
2572 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2573 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002574 mlog(ML_ERROR, "read block %llu failed %d\n",
2575 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002576 goto bail;
2577 }
2578
2579 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2580 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002581 mlog(ML_ERROR, "invalid inode %llu requested\n",
2582 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002583 status = -EINVAL;
2584 goto bail;
2585 }
2586
Tao Ma0fba8132009-03-19 05:08:43 +08002587 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002588 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2589 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002590 (unsigned long long)blkno,
2591 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002592 status = -EINVAL;
2593 goto bail;
2594 }
2595
2596 if (suballoc_slot)
2597 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2598 if (suballoc_bit)
2599 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2600
2601bail:
2602 brelse(inode_bh);
2603
2604 mlog_exit(status);
2605 return status;
2606}
2607
2608/*
2609 * test whether bit is SET in allocator bitmap or not. on success, 0
2610 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2611 * is returned and *res is meaningless. Call this after you have
2612 * cluster locked against suballoc, or you may get a result based on
2613 * non-up2date contents
2614 */
2615static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2616 struct inode *suballoc,
2617 struct buffer_head *alloc_bh, u64 blkno,
2618 u16 bit, int *res)
2619{
2620 struct ocfs2_dinode *alloc_fe;
2621 struct ocfs2_group_desc *group;
2622 struct buffer_head *group_bh = NULL;
2623 u64 bg_blkno;
2624 int status;
2625
Joel Becker5b09b502009-04-21 16:31:20 -07002626 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2627 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002628
2629 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2630 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2631 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2632 (unsigned int)bit,
2633 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2634 status = -EINVAL;
2635 goto bail;
2636 }
2637
2638 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2639 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2640 &group_bh);
2641 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002642 mlog(ML_ERROR, "read group %llu failed %d\n",
2643 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002644 goto bail;
2645 }
2646
2647 group = (struct ocfs2_group_desc *) group_bh->b_data;
2648 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2649
2650bail:
2651 brelse(group_bh);
2652
2653 mlog_exit(status);
2654 return status;
2655}
2656
2657/*
2658 * Test if the bit representing this inode (blkno) is set in the
2659 * suballocator.
2660 *
2661 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2662 *
2663 * In the event of failure, a negative value is returned and *res is
2664 * meaningless.
2665 *
2666 * Callers must make sure to hold nfs_sync_lock to prevent
2667 * ocfs2_delete_inode() on another node from accessing the same
2668 * suballocator concurrently.
2669 */
2670int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2671{
2672 int status;
2673 u16 suballoc_bit = 0, suballoc_slot = 0;
2674 struct inode *inode_alloc_inode;
2675 struct buffer_head *alloc_bh = NULL;
2676
Joel Becker5b09b502009-04-21 16:31:20 -07002677 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002678
2679 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2680 &suballoc_bit);
2681 if (status < 0) {
2682 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2683 goto bail;
2684 }
2685
2686 inode_alloc_inode =
2687 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2688 suballoc_slot);
2689 if (!inode_alloc_inode) {
2690 /* the error code could be inaccurate, but we are not able to
2691 * get the correct one. */
2692 status = -EINVAL;
2693 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2694 (u32)suballoc_slot);
2695 goto bail;
2696 }
2697
2698 mutex_lock(&inode_alloc_inode->i_mutex);
2699 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2700 if (status < 0) {
2701 mutex_unlock(&inode_alloc_inode->i_mutex);
2702 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2703 (u32)suballoc_slot, status);
2704 goto bail;
2705 }
2706
2707 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2708 blkno, suballoc_bit, res);
2709 if (status < 0)
2710 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2711
2712 ocfs2_inode_unlock(inode_alloc_inode, 0);
2713 mutex_unlock(&inode_alloc_inode->i_mutex);
2714
2715 iput(inode_alloc_inode);
2716 brelse(alloc_bh);
2717bail:
2718 mlog_exit(status);
2719 return status;
2720}