blob: a8e6a95a353f03dcb8a34cf928ded84ff7e6d127 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * suballoc.c
5 *
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070038#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "inode.h"
41#include "journal.h"
42#include "localalloc.h"
43#include "suballoc.h"
44#include "super.h"
45#include "sysfile.h"
46#include "uptodate.h"
47
48#include "buffer_head_io.h"
49
Tao Maffda89a2008-03-03 17:12:09 +080050#define NOT_ALLOC_NEW_GROUP 0
Tao Ma60ca81e2009-02-25 00:53:24 +080051#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080053
Tiger Yangb89c5422010-01-25 14:11:06 +080054#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080055
Joel Becker7d1fe092010-04-13 14:30:19 +080056struct ocfs2_suballoc_result {
Joel Becker2b6cb572010-03-26 10:09:15 +080057 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
Joel Beckerba206632010-03-26 10:08:59 +080060 u64 sr_blkno; /* The first allocated block */
Joel Becker7d1fe092010-04-13 14:30:19 +080061 unsigned int sr_bit_offset; /* The bit in the bg */
62 unsigned int sr_bits; /* How many bits we claimed */
63};
64
Mark Fashehccd979b2005-12-15 14:31:24 -080065static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
66static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
67static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070068static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080069 struct inode *alloc_inode,
70 struct buffer_head *bg_bh,
71 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080072 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 u16 my_chain,
74 struct ocfs2_chain_list *cl);
75static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
76 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070077 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080078 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080079 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080080 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080081
Mark Fashehccd979b2005-12-15 14:31:24 -080082static int ocfs2_cluster_group_search(struct inode *inode,
83 struct buffer_head *group_bh,
84 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070085 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080086 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080087static int ocfs2_block_group_search(struct inode *inode,
88 struct buffer_head *group_bh,
89 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070090 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080091 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +080092static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070093 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 u32 bits_wanted,
95 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +080096 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080097static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
98 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070099static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800100 struct inode *alloc_inode,
101 struct ocfs2_group_desc *bg,
102 struct buffer_head *group_bh,
103 unsigned int bit_off,
104 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700105static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800106 struct inode *alloc_inode,
107 struct buffer_head *fe_bh,
108 struct buffer_head *bg_bh,
109 struct buffer_head *prev_bg_bh,
110 u16 chain);
111static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
112 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
114 u64 bg_blkno,
115 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800116static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 u64 data_blkno,
118 u64 *bg_blkno,
119 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700120static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
121 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800122 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700123 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124
Mark Fasheh9c7af402008-07-28 18:02:53 -0700125void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800126{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700127 struct inode *inode = ac->ac_inode;
128
129 if (inode) {
130 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700131 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700132
133 mutex_unlock(&inode->i_mutex);
134
135 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800136 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700137 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700138 brelse(ac->ac_bh);
139 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800140 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800141}
142
143void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
144{
145 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800146 kfree(ac);
147}
148
149static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
150{
151 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
152}
153
Joel Becker57e3e792008-11-13 14:49:13 -0800154#define do_error(fmt, ...) \
155 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800156 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800157 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
158 else \
159 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
160 } while (0)
161
Joel Becker970e4932008-11-13 14:49:19 -0800162static int ocfs2_validate_gd_self(struct super_block *sb,
163 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800164 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800165{
166 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
167
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700168 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800169 do_error("Group descriptor #%llu has bad signature %.*s",
170 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800171 gd->bg_signature);
172 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700173 }
174
Joel Becker68f64d42008-11-13 14:49:14 -0800175 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
176 do_error("Group descriptor #%llu has an invalid bg_blkno "
177 "of %llu",
178 (unsigned long long)bh->b_blocknr,
179 (unsigned long long)le64_to_cpu(gd->bg_blkno));
180 return -EINVAL;
181 }
182
183 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
184 do_error("Group descriptor #%llu has an invalid "
185 "fs_generation of #%u",
186 (unsigned long long)bh->b_blocknr,
187 le32_to_cpu(gd->bg_generation));
188 return -EINVAL;
189 }
190
Joel Becker970e4932008-11-13 14:49:19 -0800191 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
192 do_error("Group descriptor #%llu has bit count %u but "
193 "claims that %u are free",
194 (unsigned long long)bh->b_blocknr,
195 le16_to_cpu(gd->bg_bits),
196 le16_to_cpu(gd->bg_free_bits_count));
197 return -EINVAL;
198 }
199
200 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
201 do_error("Group descriptor #%llu has bit count %u but "
202 "max bitmap bits of %u",
203 (unsigned long long)bh->b_blocknr,
204 le16_to_cpu(gd->bg_bits),
205 8 * le16_to_cpu(gd->bg_size));
206 return -EINVAL;
207 }
208
209 return 0;
210}
211
212static int ocfs2_validate_gd_parent(struct super_block *sb,
213 struct ocfs2_dinode *di,
214 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800215 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800216{
217 unsigned int max_bits;
218 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
219
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700220 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800221 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800222 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800223 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800224 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
225 (unsigned long long)le64_to_cpu(di->i_blkno));
226 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700227 }
228
229 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
230 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800231 do_error("Group descriptor #%llu has bit count of %u",
232 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800233 le16_to_cpu(gd->bg_bits));
234 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700235 }
236
Tao Ma78c37eb2010-03-03 11:26:27 +0800237 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
238 if ((le16_to_cpu(gd->bg_chain) >
239 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
240 ((le16_to_cpu(gd->bg_chain) ==
241 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800242 do_error("Group descriptor #%llu has bad chain %u",
243 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800244 le16_to_cpu(gd->bg_chain));
245 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246 }
247
Joel Becker970e4932008-11-13 14:49:19 -0800248 return 0;
249}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700250
Joel Becker57e3e792008-11-13 14:49:13 -0800251#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700252
Joel Becker970e4932008-11-13 14:49:19 -0800253/*
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
256 */
257int ocfs2_check_group_descriptor(struct super_block *sb,
258 struct ocfs2_dinode *di,
259 struct buffer_head *bh)
260{
261 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700262 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800263
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700264 BUG_ON(!buffer_uptodate(bh));
265
266 /*
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
270 */
271 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700272 if (rc) {
273 mlog(ML_ERROR,
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh->b_blocknr);
276 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700277 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800278 if (!rc)
279 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280
281 return rc;
282}
283
284static int ocfs2_validate_group_descriptor(struct super_block *sb,
285 struct buffer_head *bh)
286{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700287 int rc;
288 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289
Joel Becker970e4932008-11-13 14:49:19 -0800290 mlog(0, "Validating group descriptor %llu\n",
291 (unsigned long long)bh->b_blocknr);
292
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700293 BUG_ON(!buffer_uptodate(bh));
294
295 /*
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
299 */
300 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301 if (rc)
302 return rc;
303
304 /*
305 * Errors after here are fatal.
306 */
307
Joel Becker970e4932008-11-13 14:49:19 -0800308 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700309}
310
Joel Becker68f64d42008-11-13 14:49:14 -0800311int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
312 u64 gd_blkno, struct buffer_head **bh)
313{
314 int rc;
315 struct buffer_head *tmp = *bh;
316
Joel Becker8cb471e2009-02-10 20:00:41 -0800317 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800318 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800319 if (rc)
320 goto out;
321
Joel Becker970e4932008-11-13 14:49:19 -0800322 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800323 if (rc) {
324 brelse(tmp);
325 goto out;
326 }
327
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
329 if (!*bh)
330 *bh = tmp;
331
332out:
333 return rc;
334}
335
Joel Becker798db352010-04-13 14:26:32 +0800336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
339 u64 p_blkno, u32 clusters)
340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
Tao Ma47119542010-04-22 14:09:15 +0800344 BUG_ON(!ocfs2_supports_discontig_bg(osb));
Joel Becker798db352010-04-13 14:26:32 +0800345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
Tao Ma47119542010-04-22 14:09:15 +0800348 rec->e_blkno = cpu_to_le64(p_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
351 rec->e_leaf_clusters = cpu_to_le32(clusters);
352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
Tao Ma47119542010-04-22 14:09:15 +0800353 le16_add_cpu(&bg->bg_free_bits_count,
354 clusters * le16_to_cpu(cl->cl_bpc));
Joel Becker798db352010-04-13 14:26:32 +0800355 le16_add_cpu(&el->l_next_free_rec, 1);
356}
357
Mark Fasheh1fabe142006-10-09 18:11:45 -0700358static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800359 struct inode *alloc_inode,
360 struct buffer_head *bg_bh,
361 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800362 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 u16 my_chain,
364 struct ocfs2_chain_list *cl)
365{
366 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800367 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800368 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
369 struct super_block * sb = alloc_inode->i_sb;
370
371 mlog_entry_void();
372
373 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800374 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
375 "b_blocknr (%llu)",
376 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800377 (unsigned long long) bg_bh->b_blocknr);
378 status = -EIO;
379 goto bail;
380 }
381
Joel Becker13723d02008-10-17 19:25:01 -0700382 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800383 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700384 bg_bh,
385 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800386 if (status < 0) {
387 mlog_errno(status);
388 goto bail;
389 }
390
391 memset(bg, 0, sb->s_blocksize);
392 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
393 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
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{
Joel Becker18d3a982010-05-18 16:47:55 -0700487 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800488
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 Ma0a463b72010-07-08 11:11:11 +0800744 cl->cl_recs[alloc_rec].c_blkno = 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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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,
Tao Maabf1b3c2010-04-27 08:30:36 +08001985 struct buffer_head *parent_di_bh,
Tao Ma13821152009-02-25 00:53:23 +08001986 struct ocfs2_alloc_context *ac)
1987{
Tao Maabf1b3c2010-04-27 08:30:36 +08001988 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
Tao Ma13821152009-02-25 00:53:23 +08001989 /*
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;
Tao Maabf1b3c2010-04-27 08:30:36 +08002002 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2003 if (di->i_suballoc_loc)
2004 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2005 else
2006 ac->ac_last_group = ocfs2_which_suballoc_group(
2007 le64_to_cpu(di->i_blkno),
2008 le16_to_cpu(di->i_suballoc_bit));
2009 }
Tao Ma13821152009-02-25 00:53:23 +08002010}
2011
2012static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2013 struct ocfs2_alloc_context *ac)
2014{
2015 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2016 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2017}
2018
Joel Becker1ed9b772010-05-06 13:59:06 +08002019int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002020 struct inode *dir,
2021 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002022 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002023 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 u16 *suballoc_bit,
2025 u64 *fe_blkno)
2026{
2027 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002028 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002029
2030 mlog_entry_void();
2031
2032 BUG_ON(!ac);
2033 BUG_ON(ac->ac_bits_given != 0);
2034 BUG_ON(ac->ac_bits_wanted != 1);
2035 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002036
Tao Ma13821152009-02-25 00:53:23 +08002037 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2038
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002039 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002040 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002041 1,
2042 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002043 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002044 if (status < 0) {
2045 mlog_errno(status);
2046 goto bail;
2047 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002048 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002049
Joel Becker7d1fe092010-04-13 14:30:19 +08002050 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002051
Joel Becker2b6cb572010-03-26 10:09:15 +08002052 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002053 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002054 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002055 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002056 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 status = 0;
2058bail:
2059 mlog_exit(status);
2060 return status;
2061}
2062
2063/* translate a group desc. blkno and it's bitmap offset into
2064 * disk cluster offset. */
2065static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2066 u64 bg_blkno,
2067 u16 bg_bit_off)
2068{
2069 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2070 u32 cluster = 0;
2071
2072 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2073
2074 if (bg_blkno != osb->first_cluster_group_blkno)
2075 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2076 cluster += (u32) bg_bit_off;
2077 return cluster;
2078}
2079
2080/* given a cluster offset, calculate which block group it belongs to
2081 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002082u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002083{
2084 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2085 u32 group_no;
2086
2087 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2088
2089 group_no = cluster / osb->bitmap_cpg;
2090 if (!group_no)
2091 return osb->first_cluster_group_blkno;
2092 return ocfs2_clusters_to_blocks(inode->i_sb,
2093 group_no * osb->bitmap_cpg);
2094}
2095
2096/* given the block number of a cluster start, calculate which cluster
2097 * group and descriptor bitmap offset that corresponds to. */
2098static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2099 u64 data_blkno,
2100 u64 *bg_blkno,
2101 u16 *bg_bit_off)
2102{
2103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2104 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2105
2106 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2107
2108 *bg_blkno = ocfs2_which_cluster_group(inode,
2109 data_cluster);
2110
2111 if (*bg_blkno == osb->first_cluster_group_blkno)
2112 *bg_bit_off = (u16) data_cluster;
2113 else
2114 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2115 data_blkno - *bg_blkno);
2116}
2117
2118/*
2119 * min_bits - minimum contiguous chunk from this total allocation we
2120 * can handle. set to what we asked for originally for a full
2121 * contig. allocation, set to '1' to indicate we can deal with extents
2122 * of any size.
2123 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002124int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002125 struct ocfs2_alloc_context *ac,
2126 u32 min_clusters,
2127 u32 max_clusters,
2128 u32 *cluster_start,
2129 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002130{
2131 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002132 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002133 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002134 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002135
2136 mlog_entry_void();
2137
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2139
2140 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2141 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002142
2143 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002144 WARN_ON(min_clusters > 1);
2145
Mark Fashehccd979b2005-12-15 14:31:24 -08002146 status = ocfs2_claim_local_alloc_bits(osb,
2147 handle,
2148 ac,
2149 bits_wanted,
2150 cluster_start,
2151 num_clusters);
2152 if (!status)
2153 atomic_inc(&osb->alloc_stats.local_data);
2154 } else {
2155 if (min_clusters > (osb->bitmap_cpg - 1)) {
2156 /* The only paths asking for contiguousness
2157 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002158 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2159 "group bitmap size %u!\n", min_clusters,
2160 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002161 status = -ENOSPC;
2162 goto bail;
2163 }
2164 /* clamp the current request down to a realistic size. */
2165 if (bits_wanted > (osb->bitmap_cpg - 1))
2166 bits_wanted = osb->bitmap_cpg - 1;
2167
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002168 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002169 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002170 bits_wanted,
2171 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002172 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002173 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002174 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002175 *cluster_start =
2176 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002177 res.sr_bg_blkno,
2178 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002179 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002180 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002181 }
2182 }
2183 if (status < 0) {
2184 if (status != -ENOSPC)
2185 mlog_errno(status);
2186 goto bail;
2187 }
2188
Tao Ma47119542010-04-22 14:09:15 +08002189 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002190
2191bail:
2192 mlog_exit(status);
2193 return status;
2194}
2195
Joel Becker1ed9b772010-05-06 13:59:06 +08002196int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002197 struct ocfs2_alloc_context *ac,
2198 u32 min_clusters,
2199 u32 *cluster_start,
2200 u32 *num_clusters)
2201{
2202 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2203
Joel Becker1ed9b772010-05-06 13:59:06 +08002204 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002205 bits_wanted, cluster_start, num_clusters);
2206}
2207
Mark Fashehb4414ee2010-03-11 18:31:09 -08002208static int ocfs2_block_group_clear_bits(handle_t *handle,
2209 struct inode *alloc_inode,
2210 struct ocfs2_group_desc *bg,
2211 struct buffer_head *group_bh,
2212 unsigned int bit_off,
2213 unsigned int num_bits,
2214 void (*undo_fn)(unsigned int bit,
2215 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002216{
2217 int status;
2218 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002219 struct ocfs2_group_desc *undo_bg = NULL;
2220
2221 mlog_entry_void();
2222
Joel Becker42035302008-11-13 14:49:15 -08002223 /* The caller got this descriptor from
2224 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2225 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002226
2227 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2228
Mark Fashehb4414ee2010-03-11 18:31:09 -08002229 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002230 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002231 group_bh,
2232 undo_fn ?
2233 OCFS2_JOURNAL_ACCESS_UNDO :
2234 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002235 if (status < 0) {
2236 mlog_errno(status);
2237 goto bail;
2238 }
2239
Mark Fashehb4414ee2010-03-11 18:31:09 -08002240 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002241 jbd_lock_bh_state(group_bh);
2242 undo_bg = (struct ocfs2_group_desc *)
2243 bh2jh(group_bh)->b_committed_data;
2244 BUG_ON(!undo_bg);
2245 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002246
2247 tmp = num_bits;
2248 while(tmp--) {
2249 ocfs2_clear_bit((bit_off + tmp),
2250 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002251 if (undo_fn)
2252 undo_fn(bit_off + tmp,
2253 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002254 }
2255 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2256
Mark Fashehb4414ee2010-03-11 18:31:09 -08002257 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002258 jbd_unlock_bh_state(group_bh);
2259
Joel Beckerec20cec2010-03-19 14:13:52 -07002260 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002261bail:
2262 return status;
2263}
2264
2265/*
2266 * expects the suballoc inode to already be locked.
2267 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002268static int _ocfs2_free_suballoc_bits(handle_t *handle,
2269 struct inode *alloc_inode,
2270 struct buffer_head *alloc_bh,
2271 unsigned int start_bit,
2272 u64 bg_blkno,
2273 unsigned int count,
2274 void (*undo_fn)(unsigned int bit,
2275 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002276{
2277 int status = 0;
2278 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002279 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2280 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2281 struct buffer_head *group_bh = NULL;
2282 struct ocfs2_group_desc *group;
2283
2284 mlog_entry_void();
2285
Joel Becker10995aa2008-11-13 14:49:12 -08002286 /* The alloc_bh comes from ocfs2_free_dinode() or
2287 * ocfs2_free_clusters(). The callers have all locked the
2288 * allocator and gotten alloc_bh from the lock call. This
2289 * validates the dinode buffer. Any corruption that has happended
2290 * is a code bug. */
2291 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002292 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2293
Mark Fashehb06970532006-03-03 10:24:33 -08002294 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2295 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2296 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002297
Joel Becker68f64d42008-11-13 14:49:14 -08002298 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2299 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002300 if (status < 0) {
2301 mlog_errno(status);
2302 goto bail;
2303 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002304 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002305
Mark Fashehccd979b2005-12-15 14:31:24 -08002306 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2307
2308 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2309 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002310 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002311 if (status < 0) {
2312 mlog_errno(status);
2313 goto bail;
2314 }
2315
Joel Becker0cf2f762009-02-12 16:41:25 -08002316 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2317 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002318 if (status < 0) {
2319 mlog_errno(status);
2320 goto bail;
2321 }
2322
2323 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2324 count);
2325 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2326 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002327 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002328
2329bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002330 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002331
2332 mlog_exit(status);
2333 return status;
2334}
2335
Mark Fashehb4414ee2010-03-11 18:31:09 -08002336int ocfs2_free_suballoc_bits(handle_t *handle,
2337 struct inode *alloc_inode,
2338 struct buffer_head *alloc_bh,
2339 unsigned int start_bit,
2340 u64 bg_blkno,
2341 unsigned int count)
2342{
2343 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2344 start_bit, bg_blkno, count, NULL);
2345}
2346
Mark Fasheh1fabe142006-10-09 18:11:45 -07002347int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002348 struct inode *inode_alloc_inode,
2349 struct buffer_head *inode_alloc_bh,
2350 struct ocfs2_dinode *di)
2351{
2352 u64 blk = le64_to_cpu(di->i_blkno);
2353 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2354 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2355
Tao Ma74380c42010-03-22 14:20:18 +08002356 if (di->i_suballoc_loc)
2357 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002358 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2359 inode_alloc_bh, bit, bg_blkno, 1);
2360}
2361
Mark Fashehb4414ee2010-03-11 18:31:09 -08002362static int _ocfs2_free_clusters(handle_t *handle,
2363 struct inode *bitmap_inode,
2364 struct buffer_head *bitmap_bh,
2365 u64 start_blk,
2366 unsigned int num_clusters,
2367 void (*undo_fn)(unsigned int bit,
2368 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002369{
2370 int status;
2371 u16 bg_start_bit;
2372 u64 bg_blkno;
2373 struct ocfs2_dinode *fe;
2374
2375 /* You can't ever have a contiguous set of clusters
2376 * bigger than a block group bitmap so we never have to worry
2377 * about looping on them. */
2378
2379 mlog_entry_void();
2380
2381 /* This is expensive. We can safely remove once this stuff has
2382 * gotten tested really well. */
2383 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2384
2385 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2386
2387 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2388 &bg_start_bit);
2389
Mark Fashehb06970532006-03-03 10:24:33 -08002390 mlog(0, "want to free %u clusters starting at block %llu\n",
2391 num_clusters, (unsigned long long)start_blk);
2392 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2393 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002394
Mark Fashehb4414ee2010-03-11 18:31:09 -08002395 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2396 bg_start_bit, bg_blkno,
2397 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002398 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002399 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002400 goto out;
2401 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002402
Mark Fasheh9c7af402008-07-28 18:02:53 -07002403 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2404 num_clusters);
2405
2406out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002407 mlog_exit(status);
2408 return status;
2409}
2410
Mark Fashehb4414ee2010-03-11 18:31:09 -08002411int ocfs2_free_clusters(handle_t *handle,
2412 struct inode *bitmap_inode,
2413 struct buffer_head *bitmap_bh,
2414 u64 start_blk,
2415 unsigned int num_clusters)
2416{
2417 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2418 start_blk, num_clusters,
2419 _ocfs2_set_bit);
2420}
2421
2422/*
2423 * Give never-used clusters back to the global bitmap. We don't need
2424 * to protect these bits in the undo buffer.
2425 */
2426int ocfs2_release_clusters(handle_t *handle,
2427 struct inode *bitmap_inode,
2428 struct buffer_head *bitmap_bh,
2429 u64 start_blk,
2430 unsigned int num_clusters)
2431{
2432 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2433 start_blk, num_clusters,
2434 _ocfs2_clear_bit);
2435}
2436
Mark Fashehccd979b2005-12-15 14:31:24 -08002437static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2438{
2439 printk("Block Group:\n");
2440 printk("bg_signature: %s\n", bg->bg_signature);
2441 printk("bg_size: %u\n", bg->bg_size);
2442 printk("bg_bits: %u\n", bg->bg_bits);
2443 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2444 printk("bg_chain: %u\n", bg->bg_chain);
2445 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002446 printk("bg_next_group: %llu\n",
2447 (unsigned long long)bg->bg_next_group);
2448 printk("bg_parent_dinode: %llu\n",
2449 (unsigned long long)bg->bg_parent_dinode);
2450 printk("bg_blkno: %llu\n",
2451 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002452}
2453
2454static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2455{
2456 int i;
2457
Mark Fashehb06970532006-03-03 10:24:33 -08002458 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002459 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002460 printk("i_size: %llu\n",
2461 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002462 printk("i_clusters: %u\n", fe->i_clusters);
2463 printk("i_generation: %u\n",
2464 le32_to_cpu(fe->i_generation));
2465 printk("id1.bitmap1.i_used: %u\n",
2466 le32_to_cpu(fe->id1.bitmap1.i_used));
2467 printk("id1.bitmap1.i_total: %u\n",
2468 le32_to_cpu(fe->id1.bitmap1.i_total));
2469 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2470 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2471 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2472 printk("id2.i_chain.cl_next_free_rec: %u\n",
2473 fe->id2.i_chain.cl_next_free_rec);
2474 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2475 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2476 fe->id2.i_chain.cl_recs[i].c_free);
2477 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2478 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002479 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2480 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002481 }
2482}
Tao Mae7d4cb62008-08-18 17:38:44 +08002483
2484/*
2485 * For a given allocation, determine which allocators will need to be
2486 * accessed, and lock them, reserving the appropriate number of bits.
2487 *
2488 * Sparse file systems call this from ocfs2_write_begin_nolock()
2489 * and ocfs2_allocate_unwritten_extents().
2490 *
2491 * File systems which don't support holes call this from
2492 * ocfs2_extend_allocation().
2493 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002494int ocfs2_lock_allocators(struct inode *inode,
2495 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002496 u32 clusters_to_add, u32 extents_to_split,
2497 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002498 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002499{
2500 int ret = 0, num_free_extents;
2501 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2502 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2503
2504 *meta_ac = NULL;
2505 if (data_ac)
2506 *data_ac = NULL;
2507
2508 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2509
Joel Becker3d03a302009-02-12 17:49:26 -08002510 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002511 if (num_free_extents < 0) {
2512 ret = num_free_extents;
2513 mlog_errno(ret);
2514 goto out;
2515 }
2516
2517 /*
2518 * Sparse allocation file systems need to be more conservative
2519 * with reserving room for expansion - the actual allocation
2520 * happens while we've got a journal handle open so re-taking
2521 * a cluster lock (because we ran out of room for another
2522 * extent) will violate ordering rules.
2523 *
2524 * Most of the time we'll only be seeing this 1 cluster at a time
2525 * anyway.
2526 *
2527 * Always lock for any unwritten extents - we might want to
2528 * add blocks during a split.
2529 */
2530 if (!num_free_extents ||
2531 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002532 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002533 if (ret < 0) {
2534 if (ret != -ENOSPC)
2535 mlog_errno(ret);
2536 goto out;
2537 }
2538 }
2539
2540 if (clusters_to_add == 0)
2541 goto out;
2542
2543 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2544 if (ret < 0) {
2545 if (ret != -ENOSPC)
2546 mlog_errno(ret);
2547 goto out;
2548 }
2549
2550out:
2551 if (ret) {
2552 if (*meta_ac) {
2553 ocfs2_free_alloc_context(*meta_ac);
2554 *meta_ac = NULL;
2555 }
2556
2557 /*
2558 * We cannot have an error and a non null *data_ac.
2559 */
2560 }
2561
2562 return ret;
2563}
wengang wang6ca497a2009-03-06 21:29:10 +08002564
2565/*
2566 * Read the inode specified by blkno to get suballoc_slot and
2567 * suballoc_bit.
2568 */
2569static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2570 u16 *suballoc_slot, u16 *suballoc_bit)
2571{
2572 int status;
2573 struct buffer_head *inode_bh = NULL;
2574 struct ocfs2_dinode *inode_fe;
2575
Joel Becker5b09b502009-04-21 16:31:20 -07002576 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002577
2578 /* dirty read disk */
2579 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2580 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002581 mlog(ML_ERROR, "read block %llu failed %d\n",
2582 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002583 goto bail;
2584 }
2585
2586 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2587 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002588 mlog(ML_ERROR, "invalid inode %llu requested\n",
2589 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002590 status = -EINVAL;
2591 goto bail;
2592 }
2593
Tao Ma0fba8132009-03-19 05:08:43 +08002594 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002595 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2596 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002597 (unsigned long long)blkno,
2598 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002599 status = -EINVAL;
2600 goto bail;
2601 }
2602
2603 if (suballoc_slot)
2604 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2605 if (suballoc_bit)
2606 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2607
2608bail:
2609 brelse(inode_bh);
2610
2611 mlog_exit(status);
2612 return status;
2613}
2614
2615/*
2616 * test whether bit is SET in allocator bitmap or not. on success, 0
2617 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2618 * is returned and *res is meaningless. Call this after you have
2619 * cluster locked against suballoc, or you may get a result based on
2620 * non-up2date contents
2621 */
2622static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2623 struct inode *suballoc,
2624 struct buffer_head *alloc_bh, u64 blkno,
2625 u16 bit, int *res)
2626{
Tao Maabf1b3c2010-04-27 08:30:36 +08002627 struct ocfs2_dinode *alloc_di;
wengang wang6ca497a2009-03-06 21:29:10 +08002628 struct ocfs2_group_desc *group;
2629 struct buffer_head *group_bh = NULL;
2630 u64 bg_blkno;
2631 int status;
2632
Joel Becker5b09b502009-04-21 16:31:20 -07002633 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2634 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002635
Tao Maabf1b3c2010-04-27 08:30:36 +08002636 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2637 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
wengang wang6ca497a2009-03-06 21:29:10 +08002638 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2639 (unsigned int)bit,
Tao Maabf1b3c2010-04-27 08:30:36 +08002640 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
wengang wang6ca497a2009-03-06 21:29:10 +08002641 status = -EINVAL;
2642 goto bail;
2643 }
2644
Tao Maabf1b3c2010-04-27 08:30:36 +08002645 if (alloc_di->i_suballoc_loc)
2646 bg_blkno = le64_to_cpu(alloc_di->i_suballoc_loc);
2647 else
2648 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2649 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002650 &group_bh);
2651 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002652 mlog(ML_ERROR, "read group %llu failed %d\n",
2653 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002654 goto bail;
2655 }
2656
2657 group = (struct ocfs2_group_desc *) group_bh->b_data;
2658 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2659
2660bail:
2661 brelse(group_bh);
2662
2663 mlog_exit(status);
2664 return status;
2665}
2666
2667/*
2668 * Test if the bit representing this inode (blkno) is set in the
2669 * suballocator.
2670 *
2671 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2672 *
2673 * In the event of failure, a negative value is returned and *res is
2674 * meaningless.
2675 *
2676 * Callers must make sure to hold nfs_sync_lock to prevent
2677 * ocfs2_delete_inode() on another node from accessing the same
2678 * suballocator concurrently.
2679 */
2680int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2681{
2682 int status;
2683 u16 suballoc_bit = 0, suballoc_slot = 0;
2684 struct inode *inode_alloc_inode;
2685 struct buffer_head *alloc_bh = NULL;
2686
Joel Becker5b09b502009-04-21 16:31:20 -07002687 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002688
2689 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2690 &suballoc_bit);
2691 if (status < 0) {
2692 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2693 goto bail;
2694 }
2695
2696 inode_alloc_inode =
2697 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2698 suballoc_slot);
2699 if (!inode_alloc_inode) {
2700 /* the error code could be inaccurate, but we are not able to
2701 * get the correct one. */
2702 status = -EINVAL;
2703 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2704 (u32)suballoc_slot);
2705 goto bail;
2706 }
2707
2708 mutex_lock(&inode_alloc_inode->i_mutex);
2709 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2710 if (status < 0) {
2711 mutex_unlock(&inode_alloc_inode->i_mutex);
2712 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2713 (u32)suballoc_slot, status);
2714 goto bail;
2715 }
2716
2717 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2718 blkno, suballoc_bit, res);
2719 if (status < 0)
2720 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2721
2722 ocfs2_inode_unlock(inode_alloc_inode, 0);
2723 mutex_unlock(&inode_alloc_inode->i_mutex);
2724
2725 iput(inode_alloc_inode);
2726 brelse(alloc_bh);
2727bail:
2728 mlog_exit(status);
2729 return status;
2730}