blob: 0c08353fddac89a32cc4f29f146aa1c02234c36e [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 Fashehb0697052006-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);
Tao Ma85718822010-04-13 14:38:06 +0800394 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
395 osb->s_feature_incompat));
Mark Fashehccd979b2005-12-15 14:31:24 -0800396 bg->bg_chain = cpu_to_le16(my_chain);
397 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
398 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
399 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800400 if (group_clusters == le16_to_cpu(cl->cl_cpg))
401 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
402 else
Tao Ma47119542010-04-22 14:09:15 +0800403 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800404 group_clusters);
405
Mark Fashehccd979b2005-12-15 14:31:24 -0800406 /* set the 1st bit in the bitmap to account for the descriptor block */
407 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
408 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
409
Joel Beckerec20cec2010-03-19 14:13:52 -0700410 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800411
412 /* There is no need to zero out or otherwise initialize the
413 * other blocks in a group - All valid FS metadata in a block
414 * group stores the superblock fs_generation value at
415 * allocation time. */
416
417bail:
418 mlog_exit(status);
419 return status;
420}
421
422static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
423{
424 u16 curr, best;
425
426 best = curr = 0;
427 while (curr < le16_to_cpu(cl->cl_count)) {
428 if (le32_to_cpu(cl->cl_recs[best].c_total) >
429 le32_to_cpu(cl->cl_recs[curr].c_total))
430 best = curr;
431 curr++;
432 }
433 return best;
434}
435
Joel Becker798db352010-04-13 14:26:32 +0800436static struct buffer_head *
437ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
438 struct inode *alloc_inode,
439 struct ocfs2_alloc_context *ac,
440 struct ocfs2_chain_list *cl)
441{
442 int status;
443 u32 bit_off, num_bits;
444 u64 bg_blkno;
445 struct buffer_head *bg_bh;
446 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
447
Joel Becker1ed9b772010-05-06 13:59:06 +0800448 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800449 le16_to_cpu(cl->cl_cpg), &bit_off,
450 &num_bits);
451 if (status < 0) {
452 if (status != -ENOSPC)
453 mlog_errno(status);
454 goto bail;
455 }
456
457 /* setup the group */
458 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
459 mlog(0, "new descriptor, record %u, at block %llu\n",
460 alloc_rec, (unsigned long long)bg_blkno);
461
462 bg_bh = sb_getblk(osb->sb, bg_blkno);
463 if (!bg_bh) {
464 status = -EIO;
465 mlog_errno(status);
466 goto bail;
467 }
468 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
469
470 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
471 bg_blkno, num_bits, alloc_rec, cl);
472 if (status < 0) {
473 brelse(bg_bh);
474 mlog_errno(status);
475 }
476
477bail:
478 return status ? ERR_PTR(status) : bg_bh;
479}
480
481static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
482 handle_t *handle,
483 struct ocfs2_alloc_context *ac,
484 unsigned int min_bits,
485 u32 *bit_off, u32 *num_bits)
486{
487 int status;
488
489 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800490 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800491 bit_off, num_bits);
492 if (status != -ENOSPC)
493 break;
494
495 min_bits >>= 1;
496 }
497
498 return status;
499}
500
501static int ocfs2_block_group_grow_discontig(handle_t *handle,
502 struct inode *alloc_inode,
503 struct buffer_head *bg_bh,
504 struct ocfs2_alloc_context *ac,
505 struct ocfs2_chain_list *cl,
506 unsigned int min_bits)
507{
508 int status;
509 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
510 struct ocfs2_group_desc *bg =
511 (struct ocfs2_group_desc *)bg_bh->b_data;
Tao Ma47119542010-04-22 14:09:15 +0800512 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
513 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800514 u32 p_cpos, clusters;
515 u64 p_blkno;
516 struct ocfs2_extent_list *el = &bg->bg_list;
517
518 status = ocfs2_journal_access_gd(handle,
519 INODE_CACHE(alloc_inode),
520 bg_bh,
521 OCFS2_JOURNAL_ACCESS_CREATE);
522 if (status < 0) {
523 mlog_errno(status);
524 goto bail;
525 }
526
527 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
528 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800529 if (min_bits > needed)
530 min_bits = needed;
531 status = ocfs2_block_group_claim_bits(osb, handle, ac,
532 min_bits, &p_cpos,
533 &clusters);
534 if (status < 0) {
535 if (status != -ENOSPC)
536 mlog_errno(status);
537 goto bail;
538 }
539 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
540 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
541 clusters);
542
543 min_bits = clusters;
Tao Ma47119542010-04-22 14:09:15 +0800544 needed = le16_to_cpu(cl->cl_cpg) -
545 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800546 }
547
548 if (needed > 0) {
Tao Ma47119542010-04-22 14:09:15 +0800549 /*
550 * We have used up all the extent rec but can't fill up
551 * the cpg. So bail out.
552 */
553 status = -ENOSPC;
554 goto bail;
Joel Becker798db352010-04-13 14:26:32 +0800555 }
556
557 ocfs2_journal_dirty(handle, bg_bh);
558
559bail:
560 return status;
561}
562
Joel Becker8b06bc52010-03-26 10:09:29 +0800563static void ocfs2_bg_alloc_cleanup(handle_t *handle,
564 struct ocfs2_alloc_context *cluster_ac,
565 struct inode *alloc_inode,
566 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800567{
Joel Becker8b06bc52010-03-26 10:09:29 +0800568 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800569 struct ocfs2_group_desc *bg;
570 struct ocfs2_extent_list *el;
571 struct ocfs2_extent_rec *rec;
572
573 if (!bg_bh)
574 return;
575
576 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
577 el = &bg->bg_list;
578 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
579 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800580 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
581 cluster_ac->ac_bh,
582 le64_to_cpu(rec->e_blkno),
583 le32_to_cpu(rec->e_leaf_clusters));
584 if (ret)
585 mlog_errno(ret);
586 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800587 }
588
589 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
590 brelse(bg_bh);
591}
592
593static struct buffer_head *
594ocfs2_block_group_alloc_discontig(handle_t *handle,
595 struct inode *alloc_inode,
596 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800597 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800598{
599 int status;
600 u32 bit_off, num_bits;
601 u64 bg_blkno;
602 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
603 struct buffer_head *bg_bh = NULL;
604 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
605 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
606
Tao Ma47119542010-04-22 14:09:15 +0800607 if (!ocfs2_supports_discontig_bg(osb)) {
Joel Becker798db352010-04-13 14:26:32 +0800608 status = -ENOSPC;
609 goto bail;
610 }
611
Joel Becker8b06bc52010-03-26 10:09:29 +0800612 status = ocfs2_extend_trans(handle,
613 ocfs2_calc_bg_discontig_credits(osb->sb));
614 if (status) {
615 mlog_errno(status);
616 goto bail;
617 }
618
Joel Becker95ec0ad2010-03-26 10:10:08 +0800619 /*
620 * We're going to be grabbing from multiple cluster groups.
621 * We don't have enough credits to relink them all, and the
622 * cluster groups will be staying in cache for the duration of
623 * this operation.
624 */
625 ac->ac_allow_chain_relink = 0;
626
Joel Becker798db352010-04-13 14:26:32 +0800627 /* Claim the first region */
628 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
629 &bit_off, &num_bits);
630 if (status < 0) {
631 if (status != -ENOSPC)
632 mlog_errno(status);
633 goto bail;
634 }
635 min_bits = num_bits;
636
637 /* setup the group */
638 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
639 mlog(0, "new descriptor, record %u, at block %llu\n",
640 alloc_rec, (unsigned long long)bg_blkno);
641
642 bg_bh = sb_getblk(osb->sb, bg_blkno);
643 if (!bg_bh) {
644 status = -EIO;
645 mlog_errno(status);
646 goto bail;
647 }
648 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
649
650 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
651 bg_blkno, num_bits, alloc_rec, cl);
652 if (status < 0) {
653 mlog_errno(status);
654 goto bail;
655 }
656
657 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
658 bg_bh, ac, cl, min_bits);
659 if (status)
660 mlog_errno(status);
661
662bail:
663 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800664 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800665 return status ? ERR_PTR(status) : bg_bh;
666}
667
Mark Fashehccd979b2005-12-15 14:31:24 -0800668/*
669 * We expect the block group allocator to already be locked.
670 */
671static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
672 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700673 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800674 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800675 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800676 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800677{
678 int status, credits;
679 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
680 struct ocfs2_chain_list *cl;
681 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700682 handle_t *handle = NULL;
Tao Ma47119542010-04-22 14:09:15 +0800683 u16 alloc_rec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800684 struct buffer_head *bg_bh = NULL;
685 struct ocfs2_group_desc *bg;
686
687 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
688
689 mlog_entry_void();
690
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700692 status = ocfs2_reserve_clusters_with_limit(osb,
693 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800694 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800695 if (status < 0) {
696 if (status != -ENOSPC)
697 mlog_errno(status);
698 goto bail;
699 }
700
701 credits = ocfs2_calc_group_alloc_credits(osb->sb,
702 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700703 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 if (IS_ERR(handle)) {
705 status = PTR_ERR(handle);
706 handle = NULL;
707 mlog_errno(status);
708 goto bail;
709 }
710
Tao Mafeb473a2009-02-25 00:53:25 +0800711 if (last_alloc_group && *last_alloc_group != 0) {
712 mlog(0, "use old allocation group %llu for block group alloc\n",
713 (unsigned long long)*last_alloc_group);
714 ac->ac_last_group = *last_alloc_group;
715 }
Joel Becker798db352010-04-13 14:26:32 +0800716
717 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
718 ac, cl);
719 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
720 bg_bh = ocfs2_block_group_alloc_discontig(handle,
721 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800722 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800723 if (IS_ERR(bg_bh)) {
724 status = PTR_ERR(bg_bh);
725 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 if (status != -ENOSPC)
727 mlog_errno(status);
728 goto bail;
729 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800730 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
731
Joel Becker0cf2f762009-02-12 16:41:25 -0800732 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700733 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800734 if (status < 0) {
735 mlog_errno(status);
736 goto bail;
737 }
738
Tao Ma47119542010-04-22 14:09:15 +0800739 alloc_rec = le16_to_cpu(bg->bg_chain);
740 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 le16_to_cpu(bg->bg_free_bits_count));
Tao Ma47119542010-04-22 14:09:15 +0800742 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
Joel Becker798db352010-04-13 14:26:32 +0800743 le16_to_cpu(bg->bg_bits));
Tao Ma47119542010-04-22 14:09:15 +0800744 cl->cl_recs[alloc_rec].c_blkno = cpu_to_le64(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
746 le16_add_cpu(&cl->cl_next_free_rec, 1);
747
748 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
749 le16_to_cpu(bg->bg_free_bits_count));
750 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
751 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
752
Joel Beckerec20cec2010-03-19 14:13:52 -0700753 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800754
755 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
756 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
757 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
758 le32_to_cpu(fe->i_clusters)));
759 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
760 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700761 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800762
763 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800764
765 /* save the new last alloc group so that the caller can cache it. */
766 if (last_alloc_group)
767 *last_alloc_group = ac->ac_last_group;
768
Mark Fashehccd979b2005-12-15 14:31:24 -0800769bail:
770 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700771 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800772
773 if (ac)
774 ocfs2_free_alloc_context(ac);
775
Mark Fasheha81cb882008-10-07 14:25:16 -0700776 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800777
778 mlog_exit(status);
779 return status;
780}
781
782static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700783 struct ocfs2_alloc_context *ac,
784 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800785 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800786 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800787 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800788{
789 int status;
790 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700791 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800792 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800793 struct ocfs2_dinode *fe;
794 u32 free_bits;
795
796 mlog_entry_void();
797
Mark Fashehda5cbf22006-10-06 18:34:35 -0700798 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
799 if (!alloc_inode) {
800 mlog_errno(-EINVAL);
801 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800802 }
803
Mark Fashehda5cbf22006-10-06 18:34:35 -0700804 mutex_lock(&alloc_inode->i_mutex);
805
Mark Fashehe63aecb62007-10-18 15:30:42 -0700806 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700807 if (status < 0) {
808 mutex_unlock(&alloc_inode->i_mutex);
809 iput(alloc_inode);
810
811 mlog_errno(status);
812 return status;
813 }
814
815 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800816 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700817
Mark Fashehccd979b2005-12-15 14:31:24 -0800818 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800819
820 /* The bh was validated by the inode read inside
821 * ocfs2_inode_lock(). Any corruption is a code bug. */
822 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
823
Mark Fashehccd979b2005-12-15 14:31:24 -0800824 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800825 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
826 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800827 status = -EIO;
828 goto bail;
829 }
830
831 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
832 le32_to_cpu(fe->id1.bitmap1.i_used);
833
834 if (bits_wanted > free_bits) {
835 /* cluster bitmap never grows */
836 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
837 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
838 bits_wanted, free_bits);
839 status = -ENOSPC;
840 goto bail;
841 }
842
Tao Ma60ca81e2009-02-25 00:53:24 +0800843 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800844 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
845 "and we don't alloc a new group for it.\n",
846 slot, bits_wanted, free_bits);
847 status = -ENOSPC;
848 goto bail;
849 }
850
Joel Becker1187c962008-09-03 20:03:39 -0700851 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800852 ac->ac_max_block,
853 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800854 if (status < 0) {
855 if (status != -ENOSPC)
856 mlog_errno(status);
857 goto bail;
858 }
859 atomic_inc(&osb->alloc_stats.bg_extends);
860
861 /* You should never ask for this much metadata */
862 BUG_ON(bits_wanted >
863 (le32_to_cpu(fe->id1.bitmap1.i_total)
864 - le32_to_cpu(fe->id1.bitmap1.i_used)));
865 }
866
867 get_bh(bh);
868 ac->ac_bh = bh;
869bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700870 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800871
872 mlog_exit(status);
873 return status;
874}
875
Tiger Yangb89c5422010-01-25 14:11:06 +0800876static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
877{
878 spin_lock(&osb->osb_lock);
879 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
880 spin_unlock(&osb->osb_lock);
881 atomic_set(&osb->s_num_inodes_stolen, 0);
882}
883
884static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
885{
886 spin_lock(&osb->osb_lock);
887 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
888 spin_unlock(&osb->osb_lock);
889 atomic_set(&osb->s_num_meta_stolen, 0);
890}
891
892void ocfs2_init_steal_slots(struct ocfs2_super *osb)
893{
894 ocfs2_init_inode_steal_slot(osb);
895 ocfs2_init_meta_steal_slot(osb);
896}
897
898static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
899{
900 spin_lock(&osb->osb_lock);
901 if (type == INODE_ALLOC_SYSTEM_INODE)
902 osb->s_inode_steal_slot = slot;
903 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
904 osb->s_meta_steal_slot = slot;
905 spin_unlock(&osb->osb_lock);
906}
907
908static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
909{
910 int slot = OCFS2_INVALID_SLOT;
911
912 spin_lock(&osb->osb_lock);
913 if (type == INODE_ALLOC_SYSTEM_INODE)
914 slot = osb->s_inode_steal_slot;
915 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
916 slot = osb->s_meta_steal_slot;
917 spin_unlock(&osb->osb_lock);
918
919 return slot;
920}
921
922static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
923{
924 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
925}
926
927static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
928{
929 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
930}
931
932static int ocfs2_steal_resource(struct ocfs2_super *osb,
933 struct ocfs2_alloc_context *ac,
934 int type)
935{
936 int i, status = -ENOSPC;
937 int slot = __ocfs2_get_steal_slot(osb, type);
938
939 /* Start to steal resource from the first slot after ours. */
940 if (slot == OCFS2_INVALID_SLOT)
941 slot = osb->slot_num + 1;
942
943 for (i = 0; i < osb->max_slots; i++, slot++) {
944 if (slot == osb->max_slots)
945 slot = 0;
946
947 if (slot == osb->slot_num)
948 continue;
949
950 status = ocfs2_reserve_suballoc_bits(osb, ac,
951 type,
952 (u32)slot, NULL,
953 NOT_ALLOC_NEW_GROUP);
954 if (status >= 0) {
955 __ocfs2_set_steal_slot(osb, slot, type);
956 break;
957 }
958
959 ocfs2_free_ac_resource(ac);
960 }
961
962 return status;
963}
964
965static int ocfs2_steal_inode(struct ocfs2_super *osb,
966 struct ocfs2_alloc_context *ac)
967{
968 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
969}
970
971static int ocfs2_steal_meta(struct ocfs2_super *osb,
972 struct ocfs2_alloc_context *ac)
973{
974 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
975}
976
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800977int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
978 int blocks,
979 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800980{
981 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800982 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800983
Robert P. J. Daycd861282006-12-13 00:34:52 -0800984 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 if (!(*ac)) {
986 status = -ENOMEM;
987 mlog_errno(status);
988 goto bail;
989 }
990
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800991 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 (*ac)->ac_group_search = ocfs2_block_group_search;
994
Tiger Yangb89c5422010-01-25 14:11:06 +0800995 if (slot != OCFS2_INVALID_SLOT &&
996 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
997 goto extent_steal;
998
999 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001000 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +08001001 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001002 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -08001003 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +08001004
1005
1006 if (status >= 0) {
1007 status = 0;
1008 if (slot != OCFS2_INVALID_SLOT)
1009 ocfs2_init_meta_steal_slot(osb);
1010 goto bail;
1011 } else if (status < 0 && status != -ENOSPC) {
1012 mlog_errno(status);
1013 goto bail;
1014 }
1015
1016 ocfs2_free_ac_resource(*ac);
1017
1018extent_steal:
1019 status = ocfs2_steal_meta(osb, *ac);
1020 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001021 if (status < 0) {
1022 if (status != -ENOSPC)
1023 mlog_errno(status);
1024 goto bail;
1025 }
1026
1027 status = 0;
1028bail:
1029 if ((status < 0) && *ac) {
1030 ocfs2_free_alloc_context(*ac);
1031 *ac = NULL;
1032 }
1033
Mark Fashehccd979b2005-12-15 14:31:24 -08001034 mlog_exit(status);
1035 return status;
1036}
1037
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001038int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1039 struct ocfs2_extent_list *root_el,
1040 struct ocfs2_alloc_context **ac)
1041{
1042 return ocfs2_reserve_new_metadata_blocks(osb,
1043 ocfs2_extend_meta_needed(root_el),
1044 ac);
1045}
1046
Mark Fashehccd979b2005-12-15 14:31:24 -08001047int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001048 struct ocfs2_alloc_context **ac)
1049{
1050 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001051 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001052 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001053
Robert P. J. Daycd861282006-12-13 00:34:52 -08001054 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001055 if (!(*ac)) {
1056 status = -ENOMEM;
1057 mlog_errno(status);
1058 goto bail;
1059 }
1060
1061 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1063
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 (*ac)->ac_group_search = ocfs2_block_group_search;
1065
Tao Ma4d0ddb22008-03-05 16:11:46 +08001066 /*
Joel Becker1187c962008-09-03 20:03:39 -07001067 * stat(2) can't handle i_ino > 32bits, so we tell the
1068 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001069 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001070 */
Joel Becker12462f12008-09-03 20:03:40 -07001071 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1072 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001073
1074 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001075 * slot is set when we successfully steal inode from other nodes.
1076 * It is reset in 3 places:
1077 * 1. when we flush the truncate log
1078 * 2. when we complete local alloc recovery.
1079 * 3. when we successfully allocate from our own slot.
1080 * After it is set, we will go on stealing inodes until we find the
1081 * need to check our slots to see whether there is some space for us.
1082 */
1083 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001084 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001085 goto inode_steal;
1086
1087 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001088 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001089 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1090 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001091 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001092 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001093 ALLOC_NEW_GROUP |
1094 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001095 if (status >= 0) {
1096 status = 0;
1097
Tao Mafeb473a2009-02-25 00:53:25 +08001098 spin_lock(&osb->osb_lock);
1099 osb->osb_inode_alloc_group = alloc_group;
1100 spin_unlock(&osb->osb_lock);
1101 mlog(0, "after reservation, new allocation group is "
1102 "%llu\n", (unsigned long long)alloc_group);
1103
Tao Ma4d0ddb22008-03-05 16:11:46 +08001104 /*
1105 * Some inodes must be freed by us, so try to allocate
1106 * from our own next time.
1107 */
1108 if (slot != OCFS2_INVALID_SLOT)
1109 ocfs2_init_inode_steal_slot(osb);
1110 goto bail;
1111 } else if (status < 0 && status != -ENOSPC) {
1112 mlog_errno(status);
1113 goto bail;
1114 }
1115
1116 ocfs2_free_ac_resource(*ac);
1117
1118inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001119 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001120 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001121 if (status < 0) {
1122 if (status != -ENOSPC)
1123 mlog_errno(status);
1124 goto bail;
1125 }
1126
1127 status = 0;
1128bail:
1129 if ((status < 0) && *ac) {
1130 ocfs2_free_alloc_context(*ac);
1131 *ac = NULL;
1132 }
1133
Mark Fashehccd979b2005-12-15 14:31:24 -08001134 mlog_exit(status);
1135 return status;
1136}
1137
1138/* local alloc code has to do the same thing, so rather than do this
1139 * twice.. */
1140int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1141 struct ocfs2_alloc_context *ac)
1142{
1143 int status;
1144
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 ac->ac_which = OCFS2_AC_USE_MAIN;
1146 ac->ac_group_search = ocfs2_cluster_group_search;
1147
Mark Fashehda5cbf22006-10-06 18:34:35 -07001148 status = ocfs2_reserve_suballoc_bits(osb, ac,
1149 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001150 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001151 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001152 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001153 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001154 goto bail;
1155 }
1156
Mark Fashehccd979b2005-12-15 14:31:24 -08001157bail:
1158 return status;
1159}
1160
1161/* Callers don't need to care which bitmap (local alloc or main) to
1162 * use so we figure it out for them, but unfortunately this clutters
1163 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001164static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1165 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001166 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001167 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001168{
1169 int status;
1170
1171 mlog_entry_void();
1172
Robert P. J. Daycd861282006-12-13 00:34:52 -08001173 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 if (!(*ac)) {
1175 status = -ENOMEM;
1176 mlog_errno(status);
1177 goto bail;
1178 }
1179
1180 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001181 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001182
1183 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001184 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1185 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 bits_wanted,
1188 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001189 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001190 mlog_errno(status);
1191 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 }
1193 }
1194
1195 if (status == -ENOSPC) {
1196 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1197 if (status < 0) {
1198 if (status != -ENOSPC)
1199 mlog_errno(status);
1200 goto bail;
1201 }
1202 }
1203
1204 status = 0;
1205bail:
1206 if ((status < 0) && *ac) {
1207 ocfs2_free_alloc_context(*ac);
1208 *ac = NULL;
1209 }
1210
1211 mlog_exit(status);
1212 return status;
1213}
1214
Joel Becker1187c962008-09-03 20:03:39 -07001215int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1216 u32 bits_wanted,
1217 struct ocfs2_alloc_context **ac)
1218{
Tao Ma60ca81e2009-02-25 00:53:24 +08001219 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1220 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001221}
1222
Mark Fashehccd979b2005-12-15 14:31:24 -08001223/*
1224 * More or less lifted from ext3. I'll leave their description below:
1225 *
1226 * "For ext3 allocations, we must not reuse any blocks which are
1227 * allocated in the bitmap buffer's "last committed data" copy. This
1228 * prevents deletes from freeing up the page for reuse until we have
1229 * committed the delete transaction.
1230 *
1231 * If we didn't do this, then deleting something and reallocating it as
1232 * data would allow the old block to be overwritten before the
1233 * transaction committed (because we force data to disk before commit).
1234 * This would lead to corruption if we crashed between overwriting the
1235 * data and committing the delete.
1236 *
1237 * @@@ We may want to make this allocation behaviour conditional on
1238 * data-writes at some point, and disable it for metadata allocations or
1239 * sync-data inodes."
1240 *
1241 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001242 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 * called on a metadata group descriptor.
1244 */
1245static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1246 int nr)
1247{
1248 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001249 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001250
1251 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1252 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001253
1254 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001255 return 1;
1256
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001257 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001258 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001259 if (bg)
1260 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1261 else
1262 ret = 1;
1263 jbd_unlock_bh_state(bg_bh);
1264
1265 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001266}
1267
1268static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1269 struct buffer_head *bg_bh,
1270 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001271 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001272 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001273{
1274 void *bitmap;
1275 u16 best_offset, best_size;
1276 int offset, start, found, status = 0;
1277 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1278
Joel Becker42035302008-11-13 14:49:15 -08001279 /* Callers got this descriptor from
1280 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1281 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001282
1283 found = start = best_offset = best_size = 0;
1284 bitmap = bg->bg_bitmap;
1285
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001286 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1287 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 break;
1289
1290 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1291 /* We found a zero, but we can't use it as it
1292 * hasn't been put to disk yet! */
1293 found = 0;
1294 start = offset + 1;
1295 } else if (offset == start) {
1296 /* we found a zero */
1297 found++;
1298 /* move start to the next bit to test */
1299 start++;
1300 } else {
1301 /* got a zero after some ones */
1302 found = 1;
1303 start = offset + 1;
1304 }
1305 if (found > best_size) {
1306 best_size = found;
1307 best_offset = start - found;
1308 }
1309 /* we got everything we needed */
1310 if (found == bits_wanted) {
1311 /* mlog(0, "Found it all!\n"); */
1312 break;
1313 }
1314 }
1315
Joel Becker7d1fe092010-04-13 14:30:19 +08001316 if (best_size) {
1317 res->sr_bit_offset = best_offset;
1318 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001319 } else {
1320 status = -ENOSPC;
1321 /* No error log here -- see the comment above
1322 * ocfs2_test_bg_bit_allocatable */
1323 }
1324
1325 return status;
1326}
1327
Mark Fasheh1fabe142006-10-09 18:11:45 -07001328static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001329 struct inode *alloc_inode,
1330 struct ocfs2_group_desc *bg,
1331 struct buffer_head *group_bh,
1332 unsigned int bit_off,
1333 unsigned int num_bits)
1334{
1335 int status;
1336 void *bitmap = bg->bg_bitmap;
1337 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1338
1339 mlog_entry_void();
1340
Joel Becker42035302008-11-13 14:49:15 -08001341 /* All callers get the descriptor via
1342 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1343 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1345
1346 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1347 num_bits);
1348
1349 if (ocfs2_is_cluster_bitmap(alloc_inode))
1350 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1351
Joel Becker13723d02008-10-17 19:25:01 -07001352 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001353 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001354 group_bh,
1355 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 if (status < 0) {
1357 mlog_errno(status);
1358 goto bail;
1359 }
1360
1361 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001362 while(num_bits--)
1363 ocfs2_set_bit(bit_off++, bitmap);
1364
Joel Beckerec20cec2010-03-19 14:13:52 -07001365 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001366
1367bail:
1368 mlog_exit(status);
1369 return status;
1370}
1371
1372/* find the one with the most empty bits */
1373static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1374{
1375 u16 curr, best;
1376
1377 BUG_ON(!cl->cl_next_free_rec);
1378
1379 best = curr = 0;
1380 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1381 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1382 le32_to_cpu(cl->cl_recs[best].c_free))
1383 best = curr;
1384 curr++;
1385 }
1386
1387 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1388 return best;
1389}
1390
Mark Fasheh1fabe142006-10-09 18:11:45 -07001391static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001392 struct inode *alloc_inode,
1393 struct buffer_head *fe_bh,
1394 struct buffer_head *bg_bh,
1395 struct buffer_head *prev_bg_bh,
1396 u16 chain)
1397{
1398 int status;
1399 /* there is a really tiny chance the journal calls could fail,
1400 * but we wouldn't want inconsistent blocks in *any* case. */
1401 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1402 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1403 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1404 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1405
Joel Becker42035302008-11-13 14:49:15 -08001406 /* The caller got these descriptors from
1407 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1408 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1409 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001410
Mark Fashehb0697052006-03-03 10:24:33 -08001411 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001412 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1413 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1414 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001415
1416 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1417 bg_ptr = le64_to_cpu(bg->bg_next_group);
1418 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1419
Joel Becker0cf2f762009-02-12 16:41:25 -08001420 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1421 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001422 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001423 if (status < 0) {
1424 mlog_errno(status);
1425 goto out_rollback;
1426 }
1427
1428 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001429 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001430
Joel Becker0cf2f762009-02-12 16:41:25 -08001431 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1432 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 if (status < 0) {
1434 mlog_errno(status);
1435 goto out_rollback;
1436 }
1437
1438 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001439 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001440
Joel Becker0cf2f762009-02-12 16:41:25 -08001441 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1442 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 if (status < 0) {
1444 mlog_errno(status);
1445 goto out_rollback;
1446 }
1447
1448 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001449 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001450
Mark Fashehccd979b2005-12-15 14:31:24 -08001451out_rollback:
1452 if (status < 0) {
1453 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1454 bg->bg_next_group = cpu_to_le64(bg_ptr);
1455 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1456 }
Joel Becker42035302008-11-13 14:49:15 -08001457
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 mlog_exit(status);
1459 return status;
1460}
1461
1462static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1463 u32 wanted)
1464{
1465 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1466}
1467
1468/* return 0 on success, -ENOSPC to keep searching and any other < 0
1469 * value on error. */
1470static int ocfs2_cluster_group_search(struct inode *inode,
1471 struct buffer_head *group_bh,
1472 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001473 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001474 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001475{
1476 int search = -ENOSPC;
1477 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001478 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001479 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001480 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001481 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001482
1483 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1484
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001485 if (gd->bg_free_bits_count) {
1486 max_bits = le16_to_cpu(gd->bg_bits);
1487
1488 /* Tail groups in cluster bitmaps which aren't cpg
1489 * aligned are prone to partial extention by a failed
1490 * fs resize. If the file system resize never got to
1491 * update the dinode cluster count, then we don't want
1492 * to trust any clusters past it, regardless of what
1493 * the group descriptor says. */
1494 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1495 le64_to_cpu(gd->bg_blkno));
1496 if ((gd_cluster_off + max_bits) >
1497 OCFS2_I(inode)->ip_clusters) {
1498 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1499 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1500 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1501 le16_to_cpu(gd->bg_bits),
1502 OCFS2_I(inode)->ip_clusters, max_bits);
1503 }
1504
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1506 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001507 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001508 if (ret)
1509 return ret;
1510
Joel Becker1187c962008-09-03 20:03:39 -07001511 if (max_block) {
1512 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1513 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001514 res->sr_bit_offset +
1515 res->sr_bits);
Joel Becker1187c962008-09-03 20:03:39 -07001516 mlog(0, "Checking %llu against %llu\n",
1517 (unsigned long long)blkoff,
1518 (unsigned long long)max_block);
1519 if (blkoff > max_block)
1520 return -ENOSPC;
1521 }
1522
Mark Fashehccd979b2005-12-15 14:31:24 -08001523 /* ocfs2_block_group_find_clear_bits() might
1524 * return success, but we still want to return
1525 * -ENOSPC unless it found the minimum number
1526 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001527 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001528 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001529 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001530 /*
1531 * Don't show bits which we'll be returning
1532 * for allocation to the local alloc bitmap.
1533 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001534 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001535 }
1536 }
1537
1538 return search;
1539}
1540
1541static int ocfs2_block_group_search(struct inode *inode,
1542 struct buffer_head *group_bh,
1543 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001544 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001545 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001546{
1547 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001548 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001549 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1550
1551 BUG_ON(min_bits != 1);
1552 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1553
Joel Becker1187c962008-09-03 20:03:39 -07001554 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001555 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1556 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001557 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001558 res);
Joel Becker1187c962008-09-03 20:03:39 -07001559 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001560 blkoff = le64_to_cpu(bg->bg_blkno) +
1561 res->sr_bit_offset + res->sr_bits;
Joel Becker1187c962008-09-03 20:03:39 -07001562 mlog(0, "Checking %llu against %llu\n",
1563 (unsigned long long)blkoff,
1564 (unsigned long long)max_block);
1565 if (blkoff > max_block)
1566 ret = -ENOSPC;
1567 }
1568 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001569
1570 return ret;
1571}
1572
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001573static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001574 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001575 struct buffer_head *di_bh,
1576 u32 num_bits,
1577 u16 chain)
1578{
1579 int ret;
1580 u32 tmp_used;
1581 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1582 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1583
Joel Becker0cf2f762009-02-12 16:41:25 -08001584 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001585 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001586 if (ret < 0) {
1587 mlog_errno(ret);
1588 goto out;
1589 }
1590
1591 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1592 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1593 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001594 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001595
1596out:
1597 return ret;
1598}
1599
Joel Beckerba206632010-03-26 10:08:59 +08001600static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1601 struct ocfs2_extent_rec *rec,
1602 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001603{
1604 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1605 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1606 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1607
1608 if (res->sr_bit_offset < bitoff)
1609 return 0;
1610 if (res->sr_bit_offset >= (bitoff + bitcount))
1611 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001612 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1613 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001614 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1615 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1616 return 1;
1617}
1618
Joel Beckerba206632010-03-26 10:08:59 +08001619static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1620 struct ocfs2_group_desc *bg,
1621 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001622{
1623 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001624 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001625 struct ocfs2_extent_rec *rec;
1626 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1627 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1628
Joel Beckerba206632010-03-26 10:08:59 +08001629 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1630 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001631 return;
Joel Beckerba206632010-03-26 10:08:59 +08001632 }
Joel Becker13e434c2010-03-26 10:08:27 +08001633
Joel Beckerba206632010-03-26 10:08:59 +08001634 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001635 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Tao Ma47119542010-04-22 14:09:15 +08001636 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
Joel Beckerba206632010-03-26 10:08:59 +08001637 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001638 return;
1639
1640 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1641 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001642 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1643 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001644 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001645 }
Joel Becker13e434c2010-03-26 10:08:27 +08001646 }
1647}
1648
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001649static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001650 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001651 u32 bits_wanted,
1652 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001653 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001654 u16 *bits_left)
1655{
1656 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001657 struct buffer_head *group_bh = NULL;
1658 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001659 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001660 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001661
Joel Becker7d1fe092010-04-13 14:30:19 +08001662 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1663 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001664 if (ret < 0) {
1665 mlog_errno(ret);
1666 return ret;
1667 }
1668
1669 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001670 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001671 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001672 if (ret < 0) {
1673 if (ret != -ENOSPC)
1674 mlog_errno(ret);
1675 goto out;
1676 }
1677
Joel Becker13e434c2010-03-26 10:08:27 +08001678 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001679 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001680
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001681 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001682 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001683 le16_to_cpu(gd->bg_chain));
1684 if (ret < 0) {
1685 mlog_errno(ret);
1686 goto out;
1687 }
1688
1689 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001690 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001691 if (ret < 0)
1692 mlog_errno(ret);
1693
1694 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1695
1696out:
1697 brelse(group_bh);
1698
1699 return ret;
1700}
1701
Mark Fashehccd979b2005-12-15 14:31:24 -08001702static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001703 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001704 u32 bits_wanted,
1705 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001706 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001707 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001708{
1709 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001710 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001711 u32 tmp_used;
1712 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001713 struct inode *alloc_inode = ac->ac_inode;
1714 struct buffer_head *group_bh = NULL;
1715 struct buffer_head *prev_group_bh = NULL;
1716 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1717 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1718 struct ocfs2_group_desc *bg;
1719
1720 chain = ac->ac_chain;
Mark Fashehb0697052006-03-03 10:24:33 -08001721 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1722 bits_wanted, chain,
1723 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001724
Joel Becker68f64d42008-11-13 14:49:14 -08001725 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1726 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1727 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 if (status < 0) {
1729 mlog_errno(status);
1730 goto bail;
1731 }
1732 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001733
1734 status = -ENOSPC;
1735 /* for now, the chain search is a bit simplistic. We just use
1736 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001737 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1738 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001739 ac->ac_max_block,
1740 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001741 if (!bg->bg_next_group)
1742 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001743
1744 brelse(prev_group_bh);
1745 prev_group_bh = NULL;
1746
Mark Fashehccd979b2005-12-15 14:31:24 -08001747 next_group = le64_to_cpu(bg->bg_next_group);
1748 prev_group_bh = group_bh;
1749 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001750 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1751 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001752 if (status < 0) {
1753 mlog_errno(status);
1754 goto bail;
1755 }
1756 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001757 }
1758 if (status < 0) {
1759 if (status != -ENOSPC)
1760 mlog_errno(status);
1761 goto bail;
1762 }
1763
Mark Fashehb0697052006-03-03 10:24:33 -08001764 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001765 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001766
Joel Becker7d1fe092010-04-13 14:30:19 +08001767 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001768
Joel Becker7d1fe092010-04-13 14:30:19 +08001769 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001770 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001771 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001772
Mark Fashehccd979b2005-12-15 14:31:24 -08001773
1774 /*
1775 * Keep track of previous block descriptor read. When
1776 * we find a target, if we have read more than X
1777 * number of descriptors, and the target is reasonably
1778 * empty, relink him to top of his chain.
1779 *
1780 * We've read 0 extra blocks and only send one more to
1781 * the transaction, yet the next guy to search has a
1782 * much easier time.
1783 *
1784 * Do this *after* figuring out how many bits we're taking out
1785 * of our target group.
1786 */
1787 if (ac->ac_allow_chain_relink &&
1788 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001789 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 status = ocfs2_relink_block_group(handle, alloc_inode,
1791 ac->ac_bh, group_bh,
1792 prev_group_bh, chain);
1793 if (status < 0) {
1794 mlog_errno(status);
1795 goto bail;
1796 }
1797 }
1798
1799 /* Ok, claim our bits now: set the info on dinode, chainlist
1800 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001801 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001802 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001803 ac->ac_bh,
1804 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001805 if (status < 0) {
1806 mlog_errno(status);
1807 goto bail;
1808 }
1809
1810 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001811 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1812 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001813 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001814
1815 status = ocfs2_block_group_set_bits(handle,
1816 alloc_inode,
1817 bg,
1818 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001819 res->sr_bit_offset,
1820 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001821 if (status < 0) {
1822 mlog_errno(status);
1823 goto bail;
1824 }
1825
Joel Becker7d1fe092010-04-13 14:30:19 +08001826 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001827 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001828
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001829 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001830bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001831 brelse(group_bh);
1832 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001833
1834 mlog_exit(status);
1835 return status;
1836}
1837
1838/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001839static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001840 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001841 u32 bits_wanted,
1842 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001843 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001844{
1845 int status;
1846 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001847 u16 bits_left = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001848 struct ocfs2_chain_list *cl;
1849 struct ocfs2_dinode *fe;
1850
1851 mlog_entry_void();
1852
1853 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1854 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1855 BUG_ON(!ac->ac_bh);
1856
1857 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001858
1859 /* The bh was validated by the inode read during
1860 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1861 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1862
Mark Fashehccd979b2005-12-15 14:31:24 -08001863 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1864 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001865 ocfs2_error(ac->ac_inode->i_sb,
1866 "Chain allocator dinode %llu has %u used "
Mark Fashehb0697052006-03-03 10:24:33 -08001867 "bits but only %u total.",
1868 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 le32_to_cpu(fe->id1.bitmap1.i_used),
1870 le32_to_cpu(fe->id1.bitmap1.i_total));
1871 status = -EIO;
1872 goto bail;
1873 }
1874
Joel Becker7d1fe092010-04-13 14:30:19 +08001875 res->sr_bg_blkno = ac->ac_last_group;
1876 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001877 /* Attempt to short-circuit the usual search mechanism
1878 * by jumping straight to the most recently used
1879 * allocation group. This helps us mantain some
1880 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001881 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001882 min_bits, res, &bits_left);
1883 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001884 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001885 if (status < 0 && status != -ENOSPC) {
1886 mlog_errno(status);
1887 goto bail;
1888 }
1889 }
1890
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1892
1893 victim = ocfs2_find_victim_chain(cl);
1894 ac->ac_chain = victim;
1895 ac->ac_allow_chain_relink = 1;
1896
Joel Becker7d1fe092010-04-13 14:30:19 +08001897 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1898 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001899 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001900 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001901 if (status < 0 && status != -ENOSPC) {
1902 mlog_errno(status);
1903 goto bail;
1904 }
1905
1906 mlog(0, "Search of victim chain %u came up with nothing, "
1907 "trying all chains now.\n", victim);
1908
1909 /* If we didn't pick a good victim, then just default to
1910 * searching each chain in order. Don't allow chain relinking
1911 * because we only calculate enough journal credits for one
1912 * relink per alloc. */
1913 ac->ac_allow_chain_relink = 0;
1914 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1915 if (i == victim)
1916 continue;
1917 if (!cl->cl_recs[i].c_free)
1918 continue;
1919
1920 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001921 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001922 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001923 if (!status)
1924 break;
1925 if (status < 0 && status != -ENOSPC) {
1926 mlog_errno(status);
1927 goto bail;
1928 }
1929 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001930
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001931set_hint:
1932 if (status != -ENOSPC) {
1933 /* If the next search of this group is not likely to
1934 * yield a suitable extent, then we reset the last
1935 * group hint so as to not waste a disk read */
1936 if (bits_left < min_bits)
1937 ac->ac_last_group = 0;
1938 else
Joel Becker7d1fe092010-04-13 14:30:19 +08001939 ac->ac_last_group = res->sr_bg_blkno;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001940 }
1941
1942bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001943 mlog_exit(status);
1944 return status;
1945}
1946
Joel Becker1ed9b772010-05-06 13:59:06 +08001947int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 struct ocfs2_alloc_context *ac,
1949 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001950 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001951 u16 *suballoc_bit_start,
1952 unsigned int *num_bits,
1953 u64 *blkno_start)
1954{
1955 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001956 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001957
1958 BUG_ON(!ac);
1959 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1960 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001961
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001962 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001963 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001964 bits_wanted,
1965 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001966 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001967 if (status < 0) {
1968 mlog_errno(status);
1969 goto bail;
1970 }
Joel Becker1ed9b772010-05-06 13:59:06 +08001971 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001972
Joel Becker2b6cb572010-03-26 10:09:15 +08001973 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001974 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08001975 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001976 ac->ac_bits_given += res.sr_bits;
1977 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001978 status = 0;
1979bail:
1980 mlog_exit(status);
1981 return status;
1982}
1983
Tao Ma13821152009-02-25 00:53:23 +08001984static void ocfs2_init_inode_ac_group(struct inode *dir,
1985 struct buffer_head *parent_fe_bh,
1986 struct ocfs2_alloc_context *ac)
1987{
1988 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1989 /*
1990 * Try to allocate inodes from some specific group.
1991 *
1992 * If the parent dir has recorded the last group used in allocation,
1993 * cool, use it. Otherwise if we try to allocate new inode from the
1994 * same slot the parent dir belongs to, use the same chunk.
1995 *
1996 * We are very careful here to avoid the mistake of setting
1997 * ac_last_group to a group descriptor from a different (unlocked) slot.
1998 */
1999 if (OCFS2_I(dir)->ip_last_used_group &&
2000 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2001 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
2002 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
2003 ac->ac_last_group = ocfs2_which_suballoc_group(
2004 le64_to_cpu(fe->i_blkno),
2005 le16_to_cpu(fe->i_suballoc_bit));
2006}
2007
2008static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2009 struct ocfs2_alloc_context *ac)
2010{
2011 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2012 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2013}
2014
Joel Becker1ed9b772010-05-06 13:59:06 +08002015int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002016 struct inode *dir,
2017 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002018 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002019 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002020 u16 *suballoc_bit,
2021 u64 *fe_blkno)
2022{
2023 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002024 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002025
2026 mlog_entry_void();
2027
2028 BUG_ON(!ac);
2029 BUG_ON(ac->ac_bits_given != 0);
2030 BUG_ON(ac->ac_bits_wanted != 1);
2031 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002032
Tao Ma13821152009-02-25 00:53:23 +08002033 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2034
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002035 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002036 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002037 1,
2038 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002039 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002040 if (status < 0) {
2041 mlog_errno(status);
2042 goto bail;
2043 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002044 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002045
Joel Becker7d1fe092010-04-13 14:30:19 +08002046 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002047
Joel Becker2b6cb572010-03-26 10:09:15 +08002048 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002049 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002050 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002051 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002052 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002053 status = 0;
2054bail:
2055 mlog_exit(status);
2056 return status;
2057}
2058
2059/* translate a group desc. blkno and it's bitmap offset into
2060 * disk cluster offset. */
2061static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2062 u64 bg_blkno,
2063 u16 bg_bit_off)
2064{
2065 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2066 u32 cluster = 0;
2067
2068 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2069
2070 if (bg_blkno != osb->first_cluster_group_blkno)
2071 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2072 cluster += (u32) bg_bit_off;
2073 return cluster;
2074}
2075
2076/* given a cluster offset, calculate which block group it belongs to
2077 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002078u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002079{
2080 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2081 u32 group_no;
2082
2083 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2084
2085 group_no = cluster / osb->bitmap_cpg;
2086 if (!group_no)
2087 return osb->first_cluster_group_blkno;
2088 return ocfs2_clusters_to_blocks(inode->i_sb,
2089 group_no * osb->bitmap_cpg);
2090}
2091
2092/* given the block number of a cluster start, calculate which cluster
2093 * group and descriptor bitmap offset that corresponds to. */
2094static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2095 u64 data_blkno,
2096 u64 *bg_blkno,
2097 u16 *bg_bit_off)
2098{
2099 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2100 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2101
2102 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2103
2104 *bg_blkno = ocfs2_which_cluster_group(inode,
2105 data_cluster);
2106
2107 if (*bg_blkno == osb->first_cluster_group_blkno)
2108 *bg_bit_off = (u16) data_cluster;
2109 else
2110 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2111 data_blkno - *bg_blkno);
2112}
2113
2114/*
2115 * min_bits - minimum contiguous chunk from this total allocation we
2116 * can handle. set to what we asked for originally for a full
2117 * contig. allocation, set to '1' to indicate we can deal with extents
2118 * of any size.
2119 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002120int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002121 struct ocfs2_alloc_context *ac,
2122 u32 min_clusters,
2123 u32 max_clusters,
2124 u32 *cluster_start,
2125 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002126{
2127 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002128 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002129 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002130 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002131
2132 mlog_entry_void();
2133
Mark Fashehccd979b2005-12-15 14:31:24 -08002134 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2135
2136 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2137 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002138
2139 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002140 WARN_ON(min_clusters > 1);
2141
Mark Fashehccd979b2005-12-15 14:31:24 -08002142 status = ocfs2_claim_local_alloc_bits(osb,
2143 handle,
2144 ac,
2145 bits_wanted,
2146 cluster_start,
2147 num_clusters);
2148 if (!status)
2149 atomic_inc(&osb->alloc_stats.local_data);
2150 } else {
2151 if (min_clusters > (osb->bitmap_cpg - 1)) {
2152 /* The only paths asking for contiguousness
2153 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002154 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2155 "group bitmap size %u!\n", min_clusters,
2156 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002157 status = -ENOSPC;
2158 goto bail;
2159 }
2160 /* clamp the current request down to a realistic size. */
2161 if (bits_wanted > (osb->bitmap_cpg - 1))
2162 bits_wanted = osb->bitmap_cpg - 1;
2163
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002164 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002165 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002166 bits_wanted,
2167 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002168 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002169 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002170 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 *cluster_start =
2172 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002173 res.sr_bg_blkno,
2174 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002175 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002176 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002177 }
2178 }
2179 if (status < 0) {
2180 if (status != -ENOSPC)
2181 mlog_errno(status);
2182 goto bail;
2183 }
2184
Tao Ma47119542010-04-22 14:09:15 +08002185 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002186
2187bail:
2188 mlog_exit(status);
2189 return status;
2190}
2191
Joel Becker1ed9b772010-05-06 13:59:06 +08002192int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002193 struct ocfs2_alloc_context *ac,
2194 u32 min_clusters,
2195 u32 *cluster_start,
2196 u32 *num_clusters)
2197{
2198 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2199
Joel Becker1ed9b772010-05-06 13:59:06 +08002200 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002201 bits_wanted, cluster_start, num_clusters);
2202}
2203
Mark Fashehb4414ee2010-03-11 18:31:09 -08002204static int ocfs2_block_group_clear_bits(handle_t *handle,
2205 struct inode *alloc_inode,
2206 struct ocfs2_group_desc *bg,
2207 struct buffer_head *group_bh,
2208 unsigned int bit_off,
2209 unsigned int num_bits,
2210 void (*undo_fn)(unsigned int bit,
2211 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002212{
2213 int status;
2214 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002215 struct ocfs2_group_desc *undo_bg = NULL;
2216
2217 mlog_entry_void();
2218
Joel Becker42035302008-11-13 14:49:15 -08002219 /* The caller got this descriptor from
2220 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2221 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002222
2223 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2224
Mark Fashehb4414ee2010-03-11 18:31:09 -08002225 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002226 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002227 group_bh,
2228 undo_fn ?
2229 OCFS2_JOURNAL_ACCESS_UNDO :
2230 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 if (status < 0) {
2232 mlog_errno(status);
2233 goto bail;
2234 }
2235
Mark Fashehb4414ee2010-03-11 18:31:09 -08002236 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002237 jbd_lock_bh_state(group_bh);
2238 undo_bg = (struct ocfs2_group_desc *)
2239 bh2jh(group_bh)->b_committed_data;
2240 BUG_ON(!undo_bg);
2241 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002242
2243 tmp = num_bits;
2244 while(tmp--) {
2245 ocfs2_clear_bit((bit_off + tmp),
2246 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002247 if (undo_fn)
2248 undo_fn(bit_off + tmp,
2249 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002250 }
2251 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2252
Mark Fashehb4414ee2010-03-11 18:31:09 -08002253 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002254 jbd_unlock_bh_state(group_bh);
2255
Joel Beckerec20cec2010-03-19 14:13:52 -07002256 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002257bail:
2258 return status;
2259}
2260
2261/*
2262 * expects the suballoc inode to already be locked.
2263 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002264static int _ocfs2_free_suballoc_bits(handle_t *handle,
2265 struct inode *alloc_inode,
2266 struct buffer_head *alloc_bh,
2267 unsigned int start_bit,
2268 u64 bg_blkno,
2269 unsigned int count,
2270 void (*undo_fn)(unsigned int bit,
2271 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002272{
2273 int status = 0;
2274 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002275 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2276 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2277 struct buffer_head *group_bh = NULL;
2278 struct ocfs2_group_desc *group;
2279
2280 mlog_entry_void();
2281
Joel Becker10995aa2008-11-13 14:49:12 -08002282 /* The alloc_bh comes from ocfs2_free_dinode() or
2283 * ocfs2_free_clusters(). The callers have all locked the
2284 * allocator and gotten alloc_bh from the lock call. This
2285 * validates the dinode buffer. Any corruption that has happended
2286 * is a code bug. */
2287 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002288 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2289
Mark Fashehb0697052006-03-03 10:24:33 -08002290 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2291 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2292 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002293
Joel Becker68f64d42008-11-13 14:49:14 -08002294 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2295 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002296 if (status < 0) {
2297 mlog_errno(status);
2298 goto bail;
2299 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002300 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002301
Mark Fashehccd979b2005-12-15 14:31:24 -08002302 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2303
2304 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2305 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002306 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 if (status < 0) {
2308 mlog_errno(status);
2309 goto bail;
2310 }
2311
Joel Becker0cf2f762009-02-12 16:41:25 -08002312 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2313 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002314 if (status < 0) {
2315 mlog_errno(status);
2316 goto bail;
2317 }
2318
2319 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2320 count);
2321 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2322 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002323 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002324
2325bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002326 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002327
2328 mlog_exit(status);
2329 return status;
2330}
2331
Mark Fashehb4414ee2010-03-11 18:31:09 -08002332int ocfs2_free_suballoc_bits(handle_t *handle,
2333 struct inode *alloc_inode,
2334 struct buffer_head *alloc_bh,
2335 unsigned int start_bit,
2336 u64 bg_blkno,
2337 unsigned int count)
2338{
2339 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2340 start_bit, bg_blkno, count, NULL);
2341}
2342
Mark Fasheh1fabe142006-10-09 18:11:45 -07002343int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002344 struct inode *inode_alloc_inode,
2345 struct buffer_head *inode_alloc_bh,
2346 struct ocfs2_dinode *di)
2347{
2348 u64 blk = le64_to_cpu(di->i_blkno);
2349 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2350 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2351
Tao Ma74380c42010-03-22 14:20:18 +08002352 if (di->i_suballoc_loc)
2353 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002354 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2355 inode_alloc_bh, bit, bg_blkno, 1);
2356}
2357
Mark Fashehb4414ee2010-03-11 18:31:09 -08002358static int _ocfs2_free_clusters(handle_t *handle,
2359 struct inode *bitmap_inode,
2360 struct buffer_head *bitmap_bh,
2361 u64 start_blk,
2362 unsigned int num_clusters,
2363 void (*undo_fn)(unsigned int bit,
2364 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002365{
2366 int status;
2367 u16 bg_start_bit;
2368 u64 bg_blkno;
2369 struct ocfs2_dinode *fe;
2370
2371 /* You can't ever have a contiguous set of clusters
2372 * bigger than a block group bitmap so we never have to worry
2373 * about looping on them. */
2374
2375 mlog_entry_void();
2376
2377 /* This is expensive. We can safely remove once this stuff has
2378 * gotten tested really well. */
2379 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2380
2381 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2382
2383 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2384 &bg_start_bit);
2385
Mark Fashehb0697052006-03-03 10:24:33 -08002386 mlog(0, "want to free %u clusters starting at block %llu\n",
2387 num_clusters, (unsigned long long)start_blk);
2388 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2389 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002390
Mark Fashehb4414ee2010-03-11 18:31:09 -08002391 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2392 bg_start_bit, bg_blkno,
2393 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002394 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002395 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002396 goto out;
2397 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002398
Mark Fasheh9c7af402008-07-28 18:02:53 -07002399 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2400 num_clusters);
2401
2402out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 mlog_exit(status);
2404 return status;
2405}
2406
Mark Fashehb4414ee2010-03-11 18:31:09 -08002407int ocfs2_free_clusters(handle_t *handle,
2408 struct inode *bitmap_inode,
2409 struct buffer_head *bitmap_bh,
2410 u64 start_blk,
2411 unsigned int num_clusters)
2412{
2413 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2414 start_blk, num_clusters,
2415 _ocfs2_set_bit);
2416}
2417
2418/*
2419 * Give never-used clusters back to the global bitmap. We don't need
2420 * to protect these bits in the undo buffer.
2421 */
2422int ocfs2_release_clusters(handle_t *handle,
2423 struct inode *bitmap_inode,
2424 struct buffer_head *bitmap_bh,
2425 u64 start_blk,
2426 unsigned int num_clusters)
2427{
2428 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2429 start_blk, num_clusters,
2430 _ocfs2_clear_bit);
2431}
2432
Mark Fashehccd979b2005-12-15 14:31:24 -08002433static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2434{
2435 printk("Block Group:\n");
2436 printk("bg_signature: %s\n", bg->bg_signature);
2437 printk("bg_size: %u\n", bg->bg_size);
2438 printk("bg_bits: %u\n", bg->bg_bits);
2439 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2440 printk("bg_chain: %u\n", bg->bg_chain);
2441 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb0697052006-03-03 10:24:33 -08002442 printk("bg_next_group: %llu\n",
2443 (unsigned long long)bg->bg_next_group);
2444 printk("bg_parent_dinode: %llu\n",
2445 (unsigned long long)bg->bg_parent_dinode);
2446 printk("bg_blkno: %llu\n",
2447 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002448}
2449
2450static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2451{
2452 int i;
2453
Mark Fashehb0697052006-03-03 10:24:33 -08002454 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002455 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb0697052006-03-03 10:24:33 -08002456 printk("i_size: %llu\n",
2457 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002458 printk("i_clusters: %u\n", fe->i_clusters);
2459 printk("i_generation: %u\n",
2460 le32_to_cpu(fe->i_generation));
2461 printk("id1.bitmap1.i_used: %u\n",
2462 le32_to_cpu(fe->id1.bitmap1.i_used));
2463 printk("id1.bitmap1.i_total: %u\n",
2464 le32_to_cpu(fe->id1.bitmap1.i_total));
2465 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2466 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2467 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2468 printk("id2.i_chain.cl_next_free_rec: %u\n",
2469 fe->id2.i_chain.cl_next_free_rec);
2470 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2471 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2472 fe->id2.i_chain.cl_recs[i].c_free);
2473 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2474 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb0697052006-03-03 10:24:33 -08002475 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2476 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002477 }
2478}
Tao Mae7d4cb62008-08-18 17:38:44 +08002479
2480/*
2481 * For a given allocation, determine which allocators will need to be
2482 * accessed, and lock them, reserving the appropriate number of bits.
2483 *
2484 * Sparse file systems call this from ocfs2_write_begin_nolock()
2485 * and ocfs2_allocate_unwritten_extents().
2486 *
2487 * File systems which don't support holes call this from
2488 * ocfs2_extend_allocation().
2489 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002490int ocfs2_lock_allocators(struct inode *inode,
2491 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002492 u32 clusters_to_add, u32 extents_to_split,
2493 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002494 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002495{
2496 int ret = 0, num_free_extents;
2497 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2498 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2499
2500 *meta_ac = NULL;
2501 if (data_ac)
2502 *data_ac = NULL;
2503
2504 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2505
Joel Becker3d03a302009-02-12 17:49:26 -08002506 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002507 if (num_free_extents < 0) {
2508 ret = num_free_extents;
2509 mlog_errno(ret);
2510 goto out;
2511 }
2512
2513 /*
2514 * Sparse allocation file systems need to be more conservative
2515 * with reserving room for expansion - the actual allocation
2516 * happens while we've got a journal handle open so re-taking
2517 * a cluster lock (because we ran out of room for another
2518 * extent) will violate ordering rules.
2519 *
2520 * Most of the time we'll only be seeing this 1 cluster at a time
2521 * anyway.
2522 *
2523 * Always lock for any unwritten extents - we might want to
2524 * add blocks during a split.
2525 */
2526 if (!num_free_extents ||
2527 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002528 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002529 if (ret < 0) {
2530 if (ret != -ENOSPC)
2531 mlog_errno(ret);
2532 goto out;
2533 }
2534 }
2535
2536 if (clusters_to_add == 0)
2537 goto out;
2538
2539 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2540 if (ret < 0) {
2541 if (ret != -ENOSPC)
2542 mlog_errno(ret);
2543 goto out;
2544 }
2545
2546out:
2547 if (ret) {
2548 if (*meta_ac) {
2549 ocfs2_free_alloc_context(*meta_ac);
2550 *meta_ac = NULL;
2551 }
2552
2553 /*
2554 * We cannot have an error and a non null *data_ac.
2555 */
2556 }
2557
2558 return ret;
2559}
wengang wang6ca497a2009-03-06 21:29:10 +08002560
2561/*
2562 * Read the inode specified by blkno to get suballoc_slot and
2563 * suballoc_bit.
2564 */
2565static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2566 u16 *suballoc_slot, u16 *suballoc_bit)
2567{
2568 int status;
2569 struct buffer_head *inode_bh = NULL;
2570 struct ocfs2_dinode *inode_fe;
2571
Joel Becker5b09b502009-04-21 16:31:20 -07002572 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002573
2574 /* dirty read disk */
2575 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2576 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002577 mlog(ML_ERROR, "read block %llu failed %d\n",
2578 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002579 goto bail;
2580 }
2581
2582 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2583 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002584 mlog(ML_ERROR, "invalid inode %llu requested\n",
2585 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002586 status = -EINVAL;
2587 goto bail;
2588 }
2589
Tao Ma0fba8132009-03-19 05:08:43 +08002590 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002591 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2592 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002593 (unsigned long long)blkno,
2594 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002595 status = -EINVAL;
2596 goto bail;
2597 }
2598
2599 if (suballoc_slot)
2600 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2601 if (suballoc_bit)
2602 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2603
2604bail:
2605 brelse(inode_bh);
2606
2607 mlog_exit(status);
2608 return status;
2609}
2610
2611/*
2612 * test whether bit is SET in allocator bitmap or not. on success, 0
2613 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2614 * is returned and *res is meaningless. Call this after you have
2615 * cluster locked against suballoc, or you may get a result based on
2616 * non-up2date contents
2617 */
2618static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2619 struct inode *suballoc,
2620 struct buffer_head *alloc_bh, u64 blkno,
2621 u16 bit, int *res)
2622{
2623 struct ocfs2_dinode *alloc_fe;
2624 struct ocfs2_group_desc *group;
2625 struct buffer_head *group_bh = NULL;
2626 u64 bg_blkno;
2627 int status;
2628
Joel Becker5b09b502009-04-21 16:31:20 -07002629 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2630 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002631
2632 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2633 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2634 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2635 (unsigned int)bit,
2636 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2637 status = -EINVAL;
2638 goto bail;
2639 }
2640
2641 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2642 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2643 &group_bh);
2644 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002645 mlog(ML_ERROR, "read group %llu failed %d\n",
2646 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002647 goto bail;
2648 }
2649
2650 group = (struct ocfs2_group_desc *) group_bh->b_data;
2651 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2652
2653bail:
2654 brelse(group_bh);
2655
2656 mlog_exit(status);
2657 return status;
2658}
2659
2660/*
2661 * Test if the bit representing this inode (blkno) is set in the
2662 * suballocator.
2663 *
2664 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2665 *
2666 * In the event of failure, a negative value is returned and *res is
2667 * meaningless.
2668 *
2669 * Callers must make sure to hold nfs_sync_lock to prevent
2670 * ocfs2_delete_inode() on another node from accessing the same
2671 * suballocator concurrently.
2672 */
2673int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2674{
2675 int status;
2676 u16 suballoc_bit = 0, suballoc_slot = 0;
2677 struct inode *inode_alloc_inode;
2678 struct buffer_head *alloc_bh = NULL;
2679
Joel Becker5b09b502009-04-21 16:31:20 -07002680 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002681
2682 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2683 &suballoc_bit);
2684 if (status < 0) {
2685 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2686 goto bail;
2687 }
2688
2689 inode_alloc_inode =
2690 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2691 suballoc_slot);
2692 if (!inode_alloc_inode) {
2693 /* the error code could be inaccurate, but we are not able to
2694 * get the correct one. */
2695 status = -EINVAL;
2696 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2697 (u32)suballoc_slot);
2698 goto bail;
2699 }
2700
2701 mutex_lock(&inode_alloc_inode->i_mutex);
2702 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2703 if (status < 0) {
2704 mutex_unlock(&inode_alloc_inode->i_mutex);
2705 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2706 (u32)suballoc_slot, status);
2707 goto bail;
2708 }
2709
2710 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2711 blkno, suballoc_bit, res);
2712 if (status < 0)
2713 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2714
2715 ocfs2_inode_unlock(inode_alloc_inode, 0);
2716 mutex_unlock(&inode_alloc_inode->i_mutex);
2717
2718 iput(inode_alloc_inode);
2719 brelse(alloc_bh);
2720bail:
2721 mlog_exit(status);
2722 return status;
2723}