blob: e7edda8c6a113bceca5b50e8b9cd5439938b8eef [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 Fashehb2b6ebf2010-08-26 13:06:50 -070065static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
66{
67 if (res->sr_blkno == 0)
68 return 0;
69
70 if (res->sr_bg_blkno)
71 return res->sr_bg_blkno;
72
73 return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
74}
75
Mark Fashehccd979b2005-12-15 14:31:24 -080076static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
77static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
78static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070079static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080080 struct inode *alloc_inode,
81 struct buffer_head *bg_bh,
82 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080083 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080084 u16 my_chain,
85 struct ocfs2_chain_list *cl);
86static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
87 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070088 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080089 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080090 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080091 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080092
Mark Fashehccd979b2005-12-15 14:31:24 -080093static int ocfs2_cluster_group_search(struct inode *inode,
94 struct buffer_head *group_bh,
95 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070096 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080097 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080098static int ocfs2_block_group_search(struct inode *inode,
99 struct buffer_head *group_bh,
100 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -0700101 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +0800102 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +0800103static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700104 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800105 u32 bits_wanted,
106 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +0800107 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800108static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
109 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700110static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800111 struct inode *alloc_inode,
112 struct ocfs2_group_desc *bg,
113 struct buffer_head *group_bh,
114 unsigned int bit_off,
115 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700116static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800117 struct inode *alloc_inode,
118 struct buffer_head *fe_bh,
119 struct buffer_head *bg_bh,
120 struct buffer_head *prev_bg_bh,
121 u16 chain);
122static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
123 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
125 u64 bg_blkno,
126 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800127static inline void ocfs2_block_to_cluster_group(struct inode *inode,
128 u64 data_blkno,
129 u64 *bg_blkno,
130 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700131static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
132 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800133 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700134 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800135
Mark Fasheh9c7af402008-07-28 18:02:53 -0700136void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800137{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700138 struct inode *inode = ac->ac_inode;
139
140 if (inode) {
141 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700142 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700143
144 mutex_unlock(&inode->i_mutex);
145
146 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800147 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700148 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700149 brelse(ac->ac_bh);
150 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800151 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800152}
153
154void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
155{
156 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800157 kfree(ac);
158}
159
160static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
161{
162 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
163}
164
Joel Becker57e3e792008-11-13 14:49:13 -0800165#define do_error(fmt, ...) \
166 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800167 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800168 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
169 else \
170 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
171 } while (0)
172
Joel Becker970e4932008-11-13 14:49:19 -0800173static int ocfs2_validate_gd_self(struct super_block *sb,
174 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800175 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800176{
177 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
178
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700179 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800180 do_error("Group descriptor #%llu has bad signature %.*s",
181 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800182 gd->bg_signature);
183 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700184 }
185
Joel Becker68f64d42008-11-13 14:49:14 -0800186 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
187 do_error("Group descriptor #%llu has an invalid bg_blkno "
188 "of %llu",
189 (unsigned long long)bh->b_blocknr,
190 (unsigned long long)le64_to_cpu(gd->bg_blkno));
191 return -EINVAL;
192 }
193
194 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
195 do_error("Group descriptor #%llu has an invalid "
196 "fs_generation of #%u",
197 (unsigned long long)bh->b_blocknr,
198 le32_to_cpu(gd->bg_generation));
199 return -EINVAL;
200 }
201
Joel Becker970e4932008-11-13 14:49:19 -0800202 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
203 do_error("Group descriptor #%llu has bit count %u but "
204 "claims that %u are free",
205 (unsigned long long)bh->b_blocknr,
206 le16_to_cpu(gd->bg_bits),
207 le16_to_cpu(gd->bg_free_bits_count));
208 return -EINVAL;
209 }
210
211 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
212 do_error("Group descriptor #%llu has bit count %u but "
213 "max bitmap bits of %u",
214 (unsigned long long)bh->b_blocknr,
215 le16_to_cpu(gd->bg_bits),
216 8 * le16_to_cpu(gd->bg_size));
217 return -EINVAL;
218 }
219
220 return 0;
221}
222
223static int ocfs2_validate_gd_parent(struct super_block *sb,
224 struct ocfs2_dinode *di,
225 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800226 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800227{
228 unsigned int max_bits;
229 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
230
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700231 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800232 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800233 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800234 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800235 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
236 (unsigned long long)le64_to_cpu(di->i_blkno));
237 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700238 }
239
240 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
241 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800242 do_error("Group descriptor #%llu has bit count of %u",
243 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800244 le16_to_cpu(gd->bg_bits));
245 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246 }
247
Tao Ma78c37eb2010-03-03 11:26:27 +0800248 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
249 if ((le16_to_cpu(gd->bg_chain) >
250 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
251 ((le16_to_cpu(gd->bg_chain) ==
252 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800253 do_error("Group descriptor #%llu has bad chain %u",
254 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800255 le16_to_cpu(gd->bg_chain));
256 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700257 }
258
Joel Becker970e4932008-11-13 14:49:19 -0800259 return 0;
260}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700261
Joel Becker57e3e792008-11-13 14:49:13 -0800262#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700263
Joel Becker970e4932008-11-13 14:49:19 -0800264/*
265 * This version only prints errors. It does not fail the filesystem, and
266 * exists only for resize.
267 */
268int ocfs2_check_group_descriptor(struct super_block *sb,
269 struct ocfs2_dinode *di,
270 struct buffer_head *bh)
271{
272 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700273 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800274
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700275 BUG_ON(!buffer_uptodate(bh));
276
277 /*
278 * If the ecc fails, we return the error but otherwise
279 * leave the filesystem running. We know any error is
280 * local to this block.
281 */
282 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700283 if (rc) {
284 mlog(ML_ERROR,
285 "Checksum failed for group descriptor %llu\n",
286 (unsigned long long)bh->b_blocknr);
287 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700288 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800289 if (!rc)
290 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
291
292 return rc;
293}
294
295static int ocfs2_validate_group_descriptor(struct super_block *sb,
296 struct buffer_head *bh)
297{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700298 int rc;
299 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
300
Joel Becker970e4932008-11-13 14:49:19 -0800301 mlog(0, "Validating group descriptor %llu\n",
302 (unsigned long long)bh->b_blocknr);
303
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700304 BUG_ON(!buffer_uptodate(bh));
305
306 /*
307 * If the ecc fails, we return the error but otherwise
308 * leave the filesystem running. We know any error is
309 * local to this block.
310 */
311 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
312 if (rc)
313 return rc;
314
315 /*
316 * Errors after here are fatal.
317 */
318
Joel Becker970e4932008-11-13 14:49:19 -0800319 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700320}
321
Joel Becker68f64d42008-11-13 14:49:14 -0800322int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
323 u64 gd_blkno, struct buffer_head **bh)
324{
325 int rc;
326 struct buffer_head *tmp = *bh;
327
Joel Becker8cb471e2009-02-10 20:00:41 -0800328 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800329 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800330 if (rc)
331 goto out;
332
Joel Becker970e4932008-11-13 14:49:19 -0800333 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800334 if (rc) {
335 brelse(tmp);
336 goto out;
337 }
338
339 /* If ocfs2_read_block() got us a new bh, pass it up. */
340 if (!*bh)
341 *bh = tmp;
342
343out:
344 return rc;
345}
346
Joel Becker798db352010-04-13 14:26:32 +0800347static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
348 struct ocfs2_group_desc *bg,
349 struct ocfs2_chain_list *cl,
350 u64 p_blkno, u32 clusters)
351{
352 struct ocfs2_extent_list *el = &bg->bg_list;
353 struct ocfs2_extent_rec *rec;
354
Tao Ma47119542010-04-22 14:09:15 +0800355 BUG_ON(!ocfs2_supports_discontig_bg(osb));
Joel Becker798db352010-04-13 14:26:32 +0800356 if (!el->l_next_free_rec)
357 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
358 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
Tao Ma47119542010-04-22 14:09:15 +0800359 rec->e_blkno = cpu_to_le64(p_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800360 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
361 le16_to_cpu(cl->cl_bpc));
362 rec->e_leaf_clusters = cpu_to_le32(clusters);
363 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
Tao Ma47119542010-04-22 14:09:15 +0800364 le16_add_cpu(&bg->bg_free_bits_count,
365 clusters * le16_to_cpu(cl->cl_bpc));
Joel Becker798db352010-04-13 14:26:32 +0800366 le16_add_cpu(&el->l_next_free_rec, 1);
367}
368
Mark Fasheh1fabe142006-10-09 18:11:45 -0700369static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800370 struct inode *alloc_inode,
371 struct buffer_head *bg_bh,
372 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800373 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800374 u16 my_chain,
375 struct ocfs2_chain_list *cl)
376{
377 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800378 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800379 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
380 struct super_block * sb = alloc_inode->i_sb;
381
382 mlog_entry_void();
383
384 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb0697052006-03-03 10:24:33 -0800385 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
386 "b_blocknr (%llu)",
387 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800388 (unsigned long long) bg_bh->b_blocknr);
389 status = -EIO;
390 goto bail;
391 }
392
Joel Becker13723d02008-10-17 19:25:01 -0700393 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800394 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700395 bg_bh,
396 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800397 if (status < 0) {
398 mlog_errno(status);
399 goto bail;
400 }
401
402 memset(bg, 0, sb->s_blocksize);
403 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
404 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Tao Ma85718822010-04-13 14:38:06 +0800405 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
406 osb->s_feature_incompat));
Mark Fashehccd979b2005-12-15 14:31:24 -0800407 bg->bg_chain = cpu_to_le16(my_chain);
408 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
409 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
410 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800411 if (group_clusters == le16_to_cpu(cl->cl_cpg))
412 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
413 else
Tao Ma47119542010-04-22 14:09:15 +0800414 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800415 group_clusters);
416
Mark Fashehccd979b2005-12-15 14:31:24 -0800417 /* set the 1st bit in the bitmap to account for the descriptor block */
418 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
419 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
420
Joel Beckerec20cec2010-03-19 14:13:52 -0700421 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800422
423 /* There is no need to zero out or otherwise initialize the
424 * other blocks in a group - All valid FS metadata in a block
425 * group stores the superblock fs_generation value at
426 * allocation time. */
427
428bail:
429 mlog_exit(status);
430 return status;
431}
432
433static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
434{
435 u16 curr, best;
436
437 best = curr = 0;
438 while (curr < le16_to_cpu(cl->cl_count)) {
439 if (le32_to_cpu(cl->cl_recs[best].c_total) >
440 le32_to_cpu(cl->cl_recs[curr].c_total))
441 best = curr;
442 curr++;
443 }
444 return best;
445}
446
Joel Becker798db352010-04-13 14:26:32 +0800447static struct buffer_head *
448ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
449 struct inode *alloc_inode,
450 struct ocfs2_alloc_context *ac,
451 struct ocfs2_chain_list *cl)
452{
453 int status;
454 u32 bit_off, num_bits;
455 u64 bg_blkno;
456 struct buffer_head *bg_bh;
457 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
458
Joel Becker1ed9b772010-05-06 13:59:06 +0800459 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800460 le16_to_cpu(cl->cl_cpg), &bit_off,
461 &num_bits);
462 if (status < 0) {
463 if (status != -ENOSPC)
464 mlog_errno(status);
465 goto bail;
466 }
467
468 /* setup the group */
469 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
470 mlog(0, "new descriptor, record %u, at block %llu\n",
471 alloc_rec, (unsigned long long)bg_blkno);
472
473 bg_bh = sb_getblk(osb->sb, bg_blkno);
474 if (!bg_bh) {
475 status = -EIO;
476 mlog_errno(status);
477 goto bail;
478 }
479 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
480
481 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
482 bg_blkno, num_bits, alloc_rec, cl);
483 if (status < 0) {
484 brelse(bg_bh);
485 mlog_errno(status);
486 }
487
488bail:
489 return status ? ERR_PTR(status) : bg_bh;
490}
491
492static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
493 handle_t *handle,
494 struct ocfs2_alloc_context *ac,
495 unsigned int min_bits,
496 u32 *bit_off, u32 *num_bits)
497{
Joel Becker18d3a982010-05-18 16:47:55 -0700498 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800499
500 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800501 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800502 bit_off, num_bits);
503 if (status != -ENOSPC)
504 break;
505
506 min_bits >>= 1;
507 }
508
509 return status;
510}
511
512static int ocfs2_block_group_grow_discontig(handle_t *handle,
513 struct inode *alloc_inode,
514 struct buffer_head *bg_bh,
515 struct ocfs2_alloc_context *ac,
516 struct ocfs2_chain_list *cl,
517 unsigned int min_bits)
518{
519 int status;
520 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
521 struct ocfs2_group_desc *bg =
522 (struct ocfs2_group_desc *)bg_bh->b_data;
Tao Ma47119542010-04-22 14:09:15 +0800523 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
524 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800525 u32 p_cpos, clusters;
526 u64 p_blkno;
527 struct ocfs2_extent_list *el = &bg->bg_list;
528
529 status = ocfs2_journal_access_gd(handle,
530 INODE_CACHE(alloc_inode),
531 bg_bh,
532 OCFS2_JOURNAL_ACCESS_CREATE);
533 if (status < 0) {
534 mlog_errno(status);
535 goto bail;
536 }
537
538 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
539 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800540 if (min_bits > needed)
541 min_bits = needed;
542 status = ocfs2_block_group_claim_bits(osb, handle, ac,
543 min_bits, &p_cpos,
544 &clusters);
545 if (status < 0) {
546 if (status != -ENOSPC)
547 mlog_errno(status);
548 goto bail;
549 }
550 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
551 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
552 clusters);
553
554 min_bits = clusters;
Tao Ma47119542010-04-22 14:09:15 +0800555 needed = le16_to_cpu(cl->cl_cpg) -
556 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800557 }
558
559 if (needed > 0) {
Tao Ma47119542010-04-22 14:09:15 +0800560 /*
561 * We have used up all the extent rec but can't fill up
562 * the cpg. So bail out.
563 */
564 status = -ENOSPC;
565 goto bail;
Joel Becker798db352010-04-13 14:26:32 +0800566 }
567
568 ocfs2_journal_dirty(handle, bg_bh);
569
570bail:
571 return status;
572}
573
Joel Becker8b06bc52010-03-26 10:09:29 +0800574static void ocfs2_bg_alloc_cleanup(handle_t *handle,
575 struct ocfs2_alloc_context *cluster_ac,
576 struct inode *alloc_inode,
577 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800578{
Joel Becker8b06bc52010-03-26 10:09:29 +0800579 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800580 struct ocfs2_group_desc *bg;
581 struct ocfs2_extent_list *el;
582 struct ocfs2_extent_rec *rec;
583
584 if (!bg_bh)
585 return;
586
587 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
588 el = &bg->bg_list;
589 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
590 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800591 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
592 cluster_ac->ac_bh,
593 le64_to_cpu(rec->e_blkno),
594 le32_to_cpu(rec->e_leaf_clusters));
595 if (ret)
596 mlog_errno(ret);
597 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800598 }
599
600 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
601 brelse(bg_bh);
602}
603
604static struct buffer_head *
605ocfs2_block_group_alloc_discontig(handle_t *handle,
606 struct inode *alloc_inode,
607 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800608 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800609{
610 int status;
611 u32 bit_off, num_bits;
612 u64 bg_blkno;
613 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
614 struct buffer_head *bg_bh = NULL;
615 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
616 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
617
Tao Ma47119542010-04-22 14:09:15 +0800618 if (!ocfs2_supports_discontig_bg(osb)) {
Joel Becker798db352010-04-13 14:26:32 +0800619 status = -ENOSPC;
620 goto bail;
621 }
622
Joel Becker8b06bc52010-03-26 10:09:29 +0800623 status = ocfs2_extend_trans(handle,
624 ocfs2_calc_bg_discontig_credits(osb->sb));
625 if (status) {
626 mlog_errno(status);
627 goto bail;
628 }
629
Joel Becker95ec0ad2010-03-26 10:10:08 +0800630 /*
631 * We're going to be grabbing from multiple cluster groups.
632 * We don't have enough credits to relink them all, and the
633 * cluster groups will be staying in cache for the duration of
634 * this operation.
635 */
636 ac->ac_allow_chain_relink = 0;
637
Joel Becker798db352010-04-13 14:26:32 +0800638 /* Claim the first region */
639 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
640 &bit_off, &num_bits);
641 if (status < 0) {
642 if (status != -ENOSPC)
643 mlog_errno(status);
644 goto bail;
645 }
646 min_bits = num_bits;
647
648 /* setup the group */
649 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
650 mlog(0, "new descriptor, record %u, at block %llu\n",
651 alloc_rec, (unsigned long long)bg_blkno);
652
653 bg_bh = sb_getblk(osb->sb, bg_blkno);
654 if (!bg_bh) {
655 status = -EIO;
656 mlog_errno(status);
657 goto bail;
658 }
659 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
660
661 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
662 bg_blkno, num_bits, alloc_rec, cl);
663 if (status < 0) {
664 mlog_errno(status);
665 goto bail;
666 }
667
668 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
669 bg_bh, ac, cl, min_bits);
670 if (status)
671 mlog_errno(status);
672
673bail:
674 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800675 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800676 return status ? ERR_PTR(status) : bg_bh;
677}
678
Mark Fashehccd979b2005-12-15 14:31:24 -0800679/*
680 * We expect the block group allocator to already be locked.
681 */
682static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
683 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700684 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800685 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800686 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800687 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800688{
689 int status, credits;
690 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
691 struct ocfs2_chain_list *cl;
692 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700693 handle_t *handle = NULL;
Tao Ma47119542010-04-22 14:09:15 +0800694 u16 alloc_rec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800695 struct buffer_head *bg_bh = NULL;
696 struct ocfs2_group_desc *bg;
697
698 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
699
700 mlog_entry_void();
701
Mark Fashehccd979b2005-12-15 14:31:24 -0800702 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700703 status = ocfs2_reserve_clusters_with_limit(osb,
704 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800705 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706 if (status < 0) {
707 if (status != -ENOSPC)
708 mlog_errno(status);
709 goto bail;
710 }
711
712 credits = ocfs2_calc_group_alloc_credits(osb->sb,
713 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700714 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800715 if (IS_ERR(handle)) {
716 status = PTR_ERR(handle);
717 handle = NULL;
718 mlog_errno(status);
719 goto bail;
720 }
721
Tao Mafeb473a2009-02-25 00:53:25 +0800722 if (last_alloc_group && *last_alloc_group != 0) {
723 mlog(0, "use old allocation group %llu for block group alloc\n",
724 (unsigned long long)*last_alloc_group);
725 ac->ac_last_group = *last_alloc_group;
726 }
Joel Becker798db352010-04-13 14:26:32 +0800727
728 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
729 ac, cl);
730 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
731 bg_bh = ocfs2_block_group_alloc_discontig(handle,
732 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800733 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800734 if (IS_ERR(bg_bh)) {
735 status = PTR_ERR(bg_bh);
736 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800737 if (status != -ENOSPC)
738 mlog_errno(status);
739 goto bail;
740 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800741 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
742
Joel Becker0cf2f762009-02-12 16:41:25 -0800743 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700744 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 if (status < 0) {
746 mlog_errno(status);
747 goto bail;
748 }
749
Tao Ma47119542010-04-22 14:09:15 +0800750 alloc_rec = le16_to_cpu(bg->bg_chain);
751 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800752 le16_to_cpu(bg->bg_free_bits_count));
Tao Ma47119542010-04-22 14:09:15 +0800753 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
Joel Becker798db352010-04-13 14:26:32 +0800754 le16_to_cpu(bg->bg_bits));
Tao Ma0a463b72010-07-08 11:11:11 +0800755 cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800756 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
757 le16_add_cpu(&cl->cl_next_free_rec, 1);
758
759 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
760 le16_to_cpu(bg->bg_free_bits_count));
761 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
762 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
763
Joel Beckerec20cec2010-03-19 14:13:52 -0700764 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800765
766 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
767 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
768 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
769 le32_to_cpu(fe->i_clusters)));
770 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
771 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700772 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800773
774 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800775
776 /* save the new last alloc group so that the caller can cache it. */
777 if (last_alloc_group)
778 *last_alloc_group = ac->ac_last_group;
779
Mark Fashehccd979b2005-12-15 14:31:24 -0800780bail:
781 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700782 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800783
784 if (ac)
785 ocfs2_free_alloc_context(ac);
786
Mark Fasheha81cb882008-10-07 14:25:16 -0700787 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800788
789 mlog_exit(status);
790 return status;
791}
792
793static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700794 struct ocfs2_alloc_context *ac,
795 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800796 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800797 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800798 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800799{
800 int status;
801 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700802 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800803 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800804 struct ocfs2_dinode *fe;
805 u32 free_bits;
806
807 mlog_entry_void();
808
Mark Fashehda5cbf22006-10-06 18:34:35 -0700809 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
810 if (!alloc_inode) {
811 mlog_errno(-EINVAL);
812 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800813 }
814
Mark Fashehda5cbf22006-10-06 18:34:35 -0700815 mutex_lock(&alloc_inode->i_mutex);
816
Mark Fashehe63aecb62007-10-18 15:30:42 -0700817 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700818 if (status < 0) {
819 mutex_unlock(&alloc_inode->i_mutex);
820 iput(alloc_inode);
821
822 mlog_errno(status);
823 return status;
824 }
825
826 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800827 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700828
Mark Fashehccd979b2005-12-15 14:31:24 -0800829 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800830
831 /* The bh was validated by the inode read inside
832 * ocfs2_inode_lock(). Any corruption is a code bug. */
833 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
834
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb0697052006-03-03 10:24:33 -0800836 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
837 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800838 status = -EIO;
839 goto bail;
840 }
841
842 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
843 le32_to_cpu(fe->id1.bitmap1.i_used);
844
845 if (bits_wanted > free_bits) {
846 /* cluster bitmap never grows */
847 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
848 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
849 bits_wanted, free_bits);
850 status = -ENOSPC;
851 goto bail;
852 }
853
Tao Ma60ca81e2009-02-25 00:53:24 +0800854 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800855 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
856 "and we don't alloc a new group for it.\n",
857 slot, bits_wanted, free_bits);
858 status = -ENOSPC;
859 goto bail;
860 }
861
Joel Becker1187c962008-09-03 20:03:39 -0700862 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800863 ac->ac_max_block,
864 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800865 if (status < 0) {
866 if (status != -ENOSPC)
867 mlog_errno(status);
868 goto bail;
869 }
870 atomic_inc(&osb->alloc_stats.bg_extends);
871
872 /* You should never ask for this much metadata */
873 BUG_ON(bits_wanted >
874 (le32_to_cpu(fe->id1.bitmap1.i_total)
875 - le32_to_cpu(fe->id1.bitmap1.i_used)));
876 }
877
878 get_bh(bh);
879 ac->ac_bh = bh;
880bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700881 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800882
883 mlog_exit(status);
884 return status;
885}
886
Tiger Yangb89c5422010-01-25 14:11:06 +0800887static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
888{
889 spin_lock(&osb->osb_lock);
890 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
891 spin_unlock(&osb->osb_lock);
892 atomic_set(&osb->s_num_inodes_stolen, 0);
893}
894
895static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
896{
897 spin_lock(&osb->osb_lock);
898 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
899 spin_unlock(&osb->osb_lock);
900 atomic_set(&osb->s_num_meta_stolen, 0);
901}
902
903void ocfs2_init_steal_slots(struct ocfs2_super *osb)
904{
905 ocfs2_init_inode_steal_slot(osb);
906 ocfs2_init_meta_steal_slot(osb);
907}
908
909static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
910{
911 spin_lock(&osb->osb_lock);
912 if (type == INODE_ALLOC_SYSTEM_INODE)
913 osb->s_inode_steal_slot = slot;
914 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
915 osb->s_meta_steal_slot = slot;
916 spin_unlock(&osb->osb_lock);
917}
918
919static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
920{
921 int slot = OCFS2_INVALID_SLOT;
922
923 spin_lock(&osb->osb_lock);
924 if (type == INODE_ALLOC_SYSTEM_INODE)
925 slot = osb->s_inode_steal_slot;
926 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
927 slot = osb->s_meta_steal_slot;
928 spin_unlock(&osb->osb_lock);
929
930 return slot;
931}
932
933static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
934{
935 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
936}
937
938static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
939{
940 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
941}
942
943static int ocfs2_steal_resource(struct ocfs2_super *osb,
944 struct ocfs2_alloc_context *ac,
945 int type)
946{
947 int i, status = -ENOSPC;
948 int slot = __ocfs2_get_steal_slot(osb, type);
949
950 /* Start to steal resource from the first slot after ours. */
951 if (slot == OCFS2_INVALID_SLOT)
952 slot = osb->slot_num + 1;
953
954 for (i = 0; i < osb->max_slots; i++, slot++) {
955 if (slot == osb->max_slots)
956 slot = 0;
957
958 if (slot == osb->slot_num)
959 continue;
960
961 status = ocfs2_reserve_suballoc_bits(osb, ac,
962 type,
963 (u32)slot, NULL,
964 NOT_ALLOC_NEW_GROUP);
965 if (status >= 0) {
966 __ocfs2_set_steal_slot(osb, slot, type);
967 break;
968 }
969
970 ocfs2_free_ac_resource(ac);
971 }
972
973 return status;
974}
975
976static int ocfs2_steal_inode(struct ocfs2_super *osb,
977 struct ocfs2_alloc_context *ac)
978{
979 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
980}
981
982static int ocfs2_steal_meta(struct ocfs2_super *osb,
983 struct ocfs2_alloc_context *ac)
984{
985 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
986}
987
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800988int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
989 int blocks,
990 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800991{
992 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800993 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800994
Robert P. J. Daycd861282006-12-13 00:34:52 -0800995 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800996 if (!(*ac)) {
997 status = -ENOMEM;
998 mlog_errno(status);
999 goto bail;
1000 }
1001
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001002 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001003 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -08001004 (*ac)->ac_group_search = ocfs2_block_group_search;
1005
Tiger Yangb89c5422010-01-25 14:11:06 +08001006 if (slot != OCFS2_INVALID_SLOT &&
1007 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
1008 goto extent_steal;
1009
1010 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001011 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +08001012 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001013 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -08001014 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +08001015
1016
1017 if (status >= 0) {
1018 status = 0;
1019 if (slot != OCFS2_INVALID_SLOT)
1020 ocfs2_init_meta_steal_slot(osb);
1021 goto bail;
1022 } else if (status < 0 && status != -ENOSPC) {
1023 mlog_errno(status);
1024 goto bail;
1025 }
1026
1027 ocfs2_free_ac_resource(*ac);
1028
1029extent_steal:
1030 status = ocfs2_steal_meta(osb, *ac);
1031 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 if (status < 0) {
1033 if (status != -ENOSPC)
1034 mlog_errno(status);
1035 goto bail;
1036 }
1037
1038 status = 0;
1039bail:
1040 if ((status < 0) && *ac) {
1041 ocfs2_free_alloc_context(*ac);
1042 *ac = NULL;
1043 }
1044
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 mlog_exit(status);
1046 return status;
1047}
1048
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001049int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1050 struct ocfs2_extent_list *root_el,
1051 struct ocfs2_alloc_context **ac)
1052{
1053 return ocfs2_reserve_new_metadata_blocks(osb,
1054 ocfs2_extend_meta_needed(root_el),
1055 ac);
1056}
1057
Mark Fashehccd979b2005-12-15 14:31:24 -08001058int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 struct ocfs2_alloc_context **ac)
1060{
1061 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001062 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001063 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001064
Robert P. J. Daycd861282006-12-13 00:34:52 -08001065 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 if (!(*ac)) {
1067 status = -ENOMEM;
1068 mlog_errno(status);
1069 goto bail;
1070 }
1071
1072 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001073 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1074
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 (*ac)->ac_group_search = ocfs2_block_group_search;
1076
Tao Ma4d0ddb22008-03-05 16:11:46 +08001077 /*
Joel Becker1187c962008-09-03 20:03:39 -07001078 * stat(2) can't handle i_ino > 32bits, so we tell the
1079 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001080 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001081 */
Joel Becker12462f12008-09-03 20:03:40 -07001082 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1083 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001084
1085 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001086 * slot is set when we successfully steal inode from other nodes.
1087 * It is reset in 3 places:
1088 * 1. when we flush the truncate log
1089 * 2. when we complete local alloc recovery.
1090 * 3. when we successfully allocate from our own slot.
1091 * After it is set, we will go on stealing inodes until we find the
1092 * need to check our slots to see whether there is some space for us.
1093 */
1094 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001095 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001096 goto inode_steal;
1097
1098 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001099 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001100 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1101 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001102 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001103 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001104 ALLOC_NEW_GROUP |
1105 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001106 if (status >= 0) {
1107 status = 0;
1108
Tao Mafeb473a2009-02-25 00:53:25 +08001109 spin_lock(&osb->osb_lock);
1110 osb->osb_inode_alloc_group = alloc_group;
1111 spin_unlock(&osb->osb_lock);
1112 mlog(0, "after reservation, new allocation group is "
1113 "%llu\n", (unsigned long long)alloc_group);
1114
Tao Ma4d0ddb22008-03-05 16:11:46 +08001115 /*
1116 * Some inodes must be freed by us, so try to allocate
1117 * from our own next time.
1118 */
1119 if (slot != OCFS2_INVALID_SLOT)
1120 ocfs2_init_inode_steal_slot(osb);
1121 goto bail;
1122 } else if (status < 0 && status != -ENOSPC) {
1123 mlog_errno(status);
1124 goto bail;
1125 }
1126
1127 ocfs2_free_ac_resource(*ac);
1128
1129inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001130 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001131 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 if (status < 0) {
1133 if (status != -ENOSPC)
1134 mlog_errno(status);
1135 goto bail;
1136 }
1137
1138 status = 0;
1139bail:
1140 if ((status < 0) && *ac) {
1141 ocfs2_free_alloc_context(*ac);
1142 *ac = NULL;
1143 }
1144
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 mlog_exit(status);
1146 return status;
1147}
1148
1149/* local alloc code has to do the same thing, so rather than do this
1150 * twice.. */
1151int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1152 struct ocfs2_alloc_context *ac)
1153{
1154 int status;
1155
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 ac->ac_which = OCFS2_AC_USE_MAIN;
1157 ac->ac_group_search = ocfs2_cluster_group_search;
1158
Mark Fashehda5cbf22006-10-06 18:34:35 -07001159 status = ocfs2_reserve_suballoc_bits(osb, ac,
1160 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001161 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001162 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001163 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001165 goto bail;
1166 }
1167
Mark Fashehccd979b2005-12-15 14:31:24 -08001168bail:
1169 return status;
1170}
1171
1172/* Callers don't need to care which bitmap (local alloc or main) to
1173 * use so we figure it out for them, but unfortunately this clutters
1174 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001175static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1176 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001177 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001178 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001179{
1180 int status;
1181
1182 mlog_entry_void();
1183
Robert P. J. Daycd861282006-12-13 00:34:52 -08001184 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001185 if (!(*ac)) {
1186 status = -ENOMEM;
1187 mlog_errno(status);
1188 goto bail;
1189 }
1190
1191 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001192 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001193
1194 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001195 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1196 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 bits_wanted,
1199 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001200 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 mlog_errno(status);
1202 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001203 }
1204 }
1205
1206 if (status == -ENOSPC) {
1207 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1208 if (status < 0) {
1209 if (status != -ENOSPC)
1210 mlog_errno(status);
1211 goto bail;
1212 }
1213 }
1214
1215 status = 0;
1216bail:
1217 if ((status < 0) && *ac) {
1218 ocfs2_free_alloc_context(*ac);
1219 *ac = NULL;
1220 }
1221
1222 mlog_exit(status);
1223 return status;
1224}
1225
Joel Becker1187c962008-09-03 20:03:39 -07001226int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1227 u32 bits_wanted,
1228 struct ocfs2_alloc_context **ac)
1229{
Tao Ma60ca81e2009-02-25 00:53:24 +08001230 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1231 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001232}
1233
Mark Fashehccd979b2005-12-15 14:31:24 -08001234/*
1235 * More or less lifted from ext3. I'll leave their description below:
1236 *
1237 * "For ext3 allocations, we must not reuse any blocks which are
1238 * allocated in the bitmap buffer's "last committed data" copy. This
1239 * prevents deletes from freeing up the page for reuse until we have
1240 * committed the delete transaction.
1241 *
1242 * If we didn't do this, then deleting something and reallocating it as
1243 * data would allow the old block to be overwritten before the
1244 * transaction committed (because we force data to disk before commit).
1245 * This would lead to corruption if we crashed between overwriting the
1246 * data and committing the delete.
1247 *
1248 * @@@ We may want to make this allocation behaviour conditional on
1249 * data-writes at some point, and disable it for metadata allocations or
1250 * sync-data inodes."
1251 *
1252 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001253 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 * called on a metadata group descriptor.
1255 */
1256static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1257 int nr)
1258{
1259 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001260 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001261
1262 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1263 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001264
1265 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 return 1;
1267
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001268 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001270 if (bg)
1271 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1272 else
1273 ret = 1;
1274 jbd_unlock_bh_state(bg_bh);
1275
1276 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001277}
1278
1279static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1280 struct buffer_head *bg_bh,
1281 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001282 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001283 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001284{
1285 void *bitmap;
1286 u16 best_offset, best_size;
1287 int offset, start, found, status = 0;
1288 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1289
Joel Becker42035302008-11-13 14:49:15 -08001290 /* Callers got this descriptor from
1291 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1292 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001293
1294 found = start = best_offset = best_size = 0;
1295 bitmap = bg->bg_bitmap;
1296
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001297 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1298 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001299 break;
1300
1301 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1302 /* We found a zero, but we can't use it as it
1303 * hasn't been put to disk yet! */
1304 found = 0;
1305 start = offset + 1;
1306 } else if (offset == start) {
1307 /* we found a zero */
1308 found++;
1309 /* move start to the next bit to test */
1310 start++;
1311 } else {
1312 /* got a zero after some ones */
1313 found = 1;
1314 start = offset + 1;
1315 }
1316 if (found > best_size) {
1317 best_size = found;
1318 best_offset = start - found;
1319 }
1320 /* we got everything we needed */
1321 if (found == bits_wanted) {
1322 /* mlog(0, "Found it all!\n"); */
1323 break;
1324 }
1325 }
1326
Joel Becker7d1fe092010-04-13 14:30:19 +08001327 if (best_size) {
1328 res->sr_bit_offset = best_offset;
1329 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 } else {
1331 status = -ENOSPC;
1332 /* No error log here -- see the comment above
1333 * ocfs2_test_bg_bit_allocatable */
1334 }
1335
1336 return status;
1337}
1338
Mark Fasheh1fabe142006-10-09 18:11:45 -07001339static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 struct inode *alloc_inode,
1341 struct ocfs2_group_desc *bg,
1342 struct buffer_head *group_bh,
1343 unsigned int bit_off,
1344 unsigned int num_bits)
1345{
1346 int status;
1347 void *bitmap = bg->bg_bitmap;
1348 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1349
1350 mlog_entry_void();
1351
Joel Becker42035302008-11-13 14:49:15 -08001352 /* All callers get the descriptor via
1353 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1354 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001355 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1356
1357 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1358 num_bits);
1359
1360 if (ocfs2_is_cluster_bitmap(alloc_inode))
1361 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1362
Joel Becker13723d02008-10-17 19:25:01 -07001363 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001364 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001365 group_bh,
1366 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001367 if (status < 0) {
1368 mlog_errno(status);
1369 goto bail;
1370 }
1371
1372 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001373 while(num_bits--)
1374 ocfs2_set_bit(bit_off++, bitmap);
1375
Joel Beckerec20cec2010-03-19 14:13:52 -07001376 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001377
1378bail:
1379 mlog_exit(status);
1380 return status;
1381}
1382
1383/* find the one with the most empty bits */
1384static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1385{
1386 u16 curr, best;
1387
1388 BUG_ON(!cl->cl_next_free_rec);
1389
1390 best = curr = 0;
1391 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1392 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1393 le32_to_cpu(cl->cl_recs[best].c_free))
1394 best = curr;
1395 curr++;
1396 }
1397
1398 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1399 return best;
1400}
1401
Mark Fasheh1fabe142006-10-09 18:11:45 -07001402static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 struct inode *alloc_inode,
1404 struct buffer_head *fe_bh,
1405 struct buffer_head *bg_bh,
1406 struct buffer_head *prev_bg_bh,
1407 u16 chain)
1408{
1409 int status;
1410 /* there is a really tiny chance the journal calls could fail,
1411 * but we wouldn't want inconsistent blocks in *any* case. */
1412 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1413 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1414 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1415 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1416
Joel Becker42035302008-11-13 14:49:15 -08001417 /* The caller got these descriptors from
1418 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1419 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1420 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001421
Mark Fashehb0697052006-03-03 10:24:33 -08001422 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001423 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1424 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1425 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001426
1427 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1428 bg_ptr = le64_to_cpu(bg->bg_next_group);
1429 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1430
Joel Becker0cf2f762009-02-12 16:41:25 -08001431 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1432 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001433 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 if (status < 0) {
1435 mlog_errno(status);
1436 goto out_rollback;
1437 }
1438
1439 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001440 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001441
Joel Becker0cf2f762009-02-12 16:41:25 -08001442 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1443 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001444 if (status < 0) {
1445 mlog_errno(status);
1446 goto out_rollback;
1447 }
1448
1449 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001450 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001451
Joel Becker0cf2f762009-02-12 16:41:25 -08001452 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1453 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001454 if (status < 0) {
1455 mlog_errno(status);
1456 goto out_rollback;
1457 }
1458
1459 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001460 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001461
Mark Fashehccd979b2005-12-15 14:31:24 -08001462out_rollback:
1463 if (status < 0) {
1464 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1465 bg->bg_next_group = cpu_to_le64(bg_ptr);
1466 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1467 }
Joel Becker42035302008-11-13 14:49:15 -08001468
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 mlog_exit(status);
1470 return status;
1471}
1472
1473static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1474 u32 wanted)
1475{
1476 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1477}
1478
1479/* return 0 on success, -ENOSPC to keep searching and any other < 0
1480 * value on error. */
1481static int ocfs2_cluster_group_search(struct inode *inode,
1482 struct buffer_head *group_bh,
1483 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001484 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001485 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001486{
1487 int search = -ENOSPC;
1488 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001489 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001490 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001491 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001492 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001493
1494 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1495
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001496 if (gd->bg_free_bits_count) {
1497 max_bits = le16_to_cpu(gd->bg_bits);
1498
1499 /* Tail groups in cluster bitmaps which aren't cpg
1500 * aligned are prone to partial extention by a failed
1501 * fs resize. If the file system resize never got to
1502 * update the dinode cluster count, then we don't want
1503 * to trust any clusters past it, regardless of what
1504 * the group descriptor says. */
1505 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1506 le64_to_cpu(gd->bg_blkno));
1507 if ((gd_cluster_off + max_bits) >
1508 OCFS2_I(inode)->ip_clusters) {
1509 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1510 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1511 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1512 le16_to_cpu(gd->bg_bits),
1513 OCFS2_I(inode)->ip_clusters, max_bits);
1514 }
1515
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1517 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001518 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001519 if (ret)
1520 return ret;
1521
Joel Becker1187c962008-09-03 20:03:39 -07001522 if (max_block) {
1523 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1524 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001525 res->sr_bit_offset +
1526 res->sr_bits);
Joel Becker1187c962008-09-03 20:03:39 -07001527 mlog(0, "Checking %llu against %llu\n",
1528 (unsigned long long)blkoff,
1529 (unsigned long long)max_block);
1530 if (blkoff > max_block)
1531 return -ENOSPC;
1532 }
1533
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 /* ocfs2_block_group_find_clear_bits() might
1535 * return success, but we still want to return
1536 * -ENOSPC unless it found the minimum number
1537 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001538 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001540 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001541 /*
1542 * Don't show bits which we'll be returning
1543 * for allocation to the local alloc bitmap.
1544 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001545 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001546 }
1547 }
1548
1549 return search;
1550}
1551
1552static int ocfs2_block_group_search(struct inode *inode,
1553 struct buffer_head *group_bh,
1554 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001555 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001556 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001557{
1558 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001559 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001560 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1561
1562 BUG_ON(min_bits != 1);
1563 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1564
Joel Becker1187c962008-09-03 20:03:39 -07001565 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001566 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1567 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001568 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001569 res);
Joel Becker1187c962008-09-03 20:03:39 -07001570 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001571 blkoff = le64_to_cpu(bg->bg_blkno) +
1572 res->sr_bit_offset + res->sr_bits;
Joel Becker1187c962008-09-03 20:03:39 -07001573 mlog(0, "Checking %llu against %llu\n",
1574 (unsigned long long)blkoff,
1575 (unsigned long long)max_block);
1576 if (blkoff > max_block)
1577 ret = -ENOSPC;
1578 }
1579 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001580
1581 return ret;
1582}
1583
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001584static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001585 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001586 struct buffer_head *di_bh,
1587 u32 num_bits,
1588 u16 chain)
1589{
1590 int ret;
1591 u32 tmp_used;
1592 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1593 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1594
Joel Becker0cf2f762009-02-12 16:41:25 -08001595 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001596 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001597 if (ret < 0) {
1598 mlog_errno(ret);
1599 goto out;
1600 }
1601
1602 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1603 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1604 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001605 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001606
1607out:
1608 return ret;
1609}
1610
Joel Beckerba206632010-03-26 10:08:59 +08001611static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1612 struct ocfs2_extent_rec *rec,
1613 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001614{
1615 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1616 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1617 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1618
1619 if (res->sr_bit_offset < bitoff)
1620 return 0;
1621 if (res->sr_bit_offset >= (bitoff + bitcount))
1622 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001623 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1624 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001625 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1626 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1627 return 1;
1628}
1629
Joel Beckerba206632010-03-26 10:08:59 +08001630static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1631 struct ocfs2_group_desc *bg,
1632 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001633{
1634 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001635 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001636 struct ocfs2_extent_rec *rec;
1637 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1638 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1639
Joel Beckerba206632010-03-26 10:08:59 +08001640 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1641 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001642 return;
Joel Beckerba206632010-03-26 10:08:59 +08001643 }
Joel Becker13e434c2010-03-26 10:08:27 +08001644
Joel Beckerba206632010-03-26 10:08:59 +08001645 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001646 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Tao Ma47119542010-04-22 14:09:15 +08001647 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
Joel Beckerba206632010-03-26 10:08:59 +08001648 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001649 return;
1650
1651 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1652 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001653 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1654 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001655 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001656 }
Joel Becker13e434c2010-03-26 10:08:27 +08001657 }
1658}
1659
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001660static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001661 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001662 u32 bits_wanted,
1663 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001664 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001665 u16 *bits_left)
1666{
1667 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001668 struct buffer_head *group_bh = NULL;
1669 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001670 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001671 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001672
Joel Becker7d1fe092010-04-13 14:30:19 +08001673 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1674 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001675 if (ret < 0) {
1676 mlog_errno(ret);
1677 return ret;
1678 }
1679
1680 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001681 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001682 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001683 if (ret < 0) {
1684 if (ret != -ENOSPC)
1685 mlog_errno(ret);
1686 goto out;
1687 }
1688
Joel Becker13e434c2010-03-26 10:08:27 +08001689 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001690 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001691
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001692 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001693 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001694 le16_to_cpu(gd->bg_chain));
1695 if (ret < 0) {
1696 mlog_errno(ret);
1697 goto out;
1698 }
1699
1700 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001701 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001702 if (ret < 0)
1703 mlog_errno(ret);
1704
1705 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1706
1707out:
1708 brelse(group_bh);
1709
1710 return ret;
1711}
1712
Mark Fashehccd979b2005-12-15 14:31:24 -08001713static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001714 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001715 u32 bits_wanted,
1716 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001717 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001718 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001719{
1720 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001721 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001723 struct inode *alloc_inode = ac->ac_inode;
1724 struct buffer_head *group_bh = NULL;
1725 struct buffer_head *prev_group_bh = NULL;
1726 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1727 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1728 struct ocfs2_group_desc *bg;
1729
1730 chain = ac->ac_chain;
Mark Fashehb0697052006-03-03 10:24:33 -08001731 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1732 bits_wanted, chain,
1733 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001734
Joel Becker68f64d42008-11-13 14:49:14 -08001735 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1736 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1737 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 if (status < 0) {
1739 mlog_errno(status);
1740 goto bail;
1741 }
1742 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001743
1744 status = -ENOSPC;
1745 /* for now, the chain search is a bit simplistic. We just use
1746 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001747 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1748 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001749 ac->ac_max_block,
1750 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001751 if (!bg->bg_next_group)
1752 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001753
1754 brelse(prev_group_bh);
1755 prev_group_bh = NULL;
1756
Mark Fashehccd979b2005-12-15 14:31:24 -08001757 next_group = le64_to_cpu(bg->bg_next_group);
1758 prev_group_bh = group_bh;
1759 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001760 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1761 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001762 if (status < 0) {
1763 mlog_errno(status);
1764 goto bail;
1765 }
1766 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001767 }
1768 if (status < 0) {
1769 if (status != -ENOSPC)
1770 mlog_errno(status);
1771 goto bail;
1772 }
1773
Mark Fashehb0697052006-03-03 10:24:33 -08001774 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001775 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001776
Joel Becker7d1fe092010-04-13 14:30:19 +08001777 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001778
Joel Becker7d1fe092010-04-13 14:30:19 +08001779 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001780 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001781 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001782
Mark Fashehccd979b2005-12-15 14:31:24 -08001783
1784 /*
1785 * Keep track of previous block descriptor read. When
1786 * we find a target, if we have read more than X
1787 * number of descriptors, and the target is reasonably
1788 * empty, relink him to top of his chain.
1789 *
1790 * We've read 0 extra blocks and only send one more to
1791 * the transaction, yet the next guy to search has a
1792 * much easier time.
1793 *
1794 * Do this *after* figuring out how many bits we're taking out
1795 * of our target group.
1796 */
1797 if (ac->ac_allow_chain_relink &&
1798 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001799 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001800 status = ocfs2_relink_block_group(handle, alloc_inode,
1801 ac->ac_bh, group_bh,
1802 prev_group_bh, chain);
1803 if (status < 0) {
1804 mlog_errno(status);
1805 goto bail;
1806 }
1807 }
1808
Mark Fashehd5134982010-08-13 15:15:16 -07001809 status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
1810 ac->ac_bh, res->sr_bits,
1811 chain);
1812 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001813 mlog_errno(status);
1814 goto bail;
1815 }
1816
Mark Fashehccd979b2005-12-15 14:31:24 -08001817 status = ocfs2_block_group_set_bits(handle,
1818 alloc_inode,
1819 bg,
1820 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001821 res->sr_bit_offset,
1822 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001823 if (status < 0) {
1824 mlog_errno(status);
1825 goto bail;
1826 }
1827
Joel Becker7d1fe092010-04-13 14:30:19 +08001828 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001829 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001830
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001831 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001832bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001833 brelse(group_bh);
1834 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001835
1836 mlog_exit(status);
1837 return status;
1838}
1839
1840/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001841static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001842 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001843 u32 bits_wanted,
1844 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001845 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001846{
1847 int status;
1848 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001849 u16 bits_left = 0;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001850 u64 hint = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001851 struct ocfs2_chain_list *cl;
1852 struct ocfs2_dinode *fe;
1853
1854 mlog_entry_void();
1855
1856 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1857 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1858 BUG_ON(!ac->ac_bh);
1859
1860 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001861
1862 /* The bh was validated by the inode read during
1863 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1864 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1865
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1867 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001868 ocfs2_error(ac->ac_inode->i_sb,
1869 "Chain allocator dinode %llu has %u used "
Mark Fashehb0697052006-03-03 10:24:33 -08001870 "bits but only %u total.",
1871 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001872 le32_to_cpu(fe->id1.bitmap1.i_used),
1873 le32_to_cpu(fe->id1.bitmap1.i_total));
1874 status = -EIO;
1875 goto bail;
1876 }
1877
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001878 res->sr_bg_blkno = hint;
Joel Becker7d1fe092010-04-13 14:30:19 +08001879 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001880 /* Attempt to short-circuit the usual search mechanism
1881 * by jumping straight to the most recently used
1882 * allocation group. This helps us mantain some
1883 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001884 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001885 min_bits, res, &bits_left);
1886 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001887 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001888 if (status < 0 && status != -ENOSPC) {
1889 mlog_errno(status);
1890 goto bail;
1891 }
1892 }
1893
Mark Fashehccd979b2005-12-15 14:31:24 -08001894 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1895
1896 victim = ocfs2_find_victim_chain(cl);
1897 ac->ac_chain = victim;
1898 ac->ac_allow_chain_relink = 1;
1899
Joel Becker7d1fe092010-04-13 14:30:19 +08001900 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1901 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001902 if (!status) {
1903 hint = ocfs2_group_from_res(res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001904 goto set_hint;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001905 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001906 if (status < 0 && status != -ENOSPC) {
1907 mlog_errno(status);
1908 goto bail;
1909 }
1910
1911 mlog(0, "Search of victim chain %u came up with nothing, "
1912 "trying all chains now.\n", victim);
1913
1914 /* If we didn't pick a good victim, then just default to
1915 * searching each chain in order. Don't allow chain relinking
1916 * because we only calculate enough journal credits for one
1917 * relink per alloc. */
1918 ac->ac_allow_chain_relink = 0;
1919 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1920 if (i == victim)
1921 continue;
1922 if (!cl->cl_recs[i].c_free)
1923 continue;
1924
1925 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001926 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001927 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001928 if (!status) {
1929 hint = ocfs2_group_from_res(res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001930 break;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001931 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 if (status < 0 && status != -ENOSPC) {
1933 mlog_errno(status);
1934 goto bail;
1935 }
1936 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001937
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001938set_hint:
1939 if (status != -ENOSPC) {
1940 /* If the next search of this group is not likely to
1941 * yield a suitable extent, then we reset the last
1942 * group hint so as to not waste a disk read */
1943 if (bits_left < min_bits)
1944 ac->ac_last_group = 0;
1945 else
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001946 ac->ac_last_group = hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001947 }
1948
1949bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001950 mlog_exit(status);
1951 return status;
1952}
1953
Joel Becker1ed9b772010-05-06 13:59:06 +08001954int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001955 struct ocfs2_alloc_context *ac,
1956 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001957 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001958 u16 *suballoc_bit_start,
1959 unsigned int *num_bits,
1960 u64 *blkno_start)
1961{
1962 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001963 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001964
1965 BUG_ON(!ac);
1966 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1967 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001968
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001969 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001970 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001971 bits_wanted,
1972 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001973 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001974 if (status < 0) {
1975 mlog_errno(status);
1976 goto bail;
1977 }
Joel Becker1ed9b772010-05-06 13:59:06 +08001978 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001979
Joel Becker2b6cb572010-03-26 10:09:15 +08001980 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001981 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08001982 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001983 ac->ac_bits_given += res.sr_bits;
1984 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001985 status = 0;
1986bail:
1987 mlog_exit(status);
1988 return status;
1989}
1990
Tao Ma13821152009-02-25 00:53:23 +08001991static void ocfs2_init_inode_ac_group(struct inode *dir,
Tao Maabf1b3c2010-04-27 08:30:36 +08001992 struct buffer_head *parent_di_bh,
Tao Ma13821152009-02-25 00:53:23 +08001993 struct ocfs2_alloc_context *ac)
1994{
Tao Maabf1b3c2010-04-27 08:30:36 +08001995 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
Tao Ma13821152009-02-25 00:53:23 +08001996 /*
1997 * Try to allocate inodes from some specific group.
1998 *
1999 * If the parent dir has recorded the last group used in allocation,
2000 * cool, use it. Otherwise if we try to allocate new inode from the
2001 * same slot the parent dir belongs to, use the same chunk.
2002 *
2003 * We are very careful here to avoid the mistake of setting
2004 * ac_last_group to a group descriptor from a different (unlocked) slot.
2005 */
2006 if (OCFS2_I(dir)->ip_last_used_group &&
2007 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2008 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
Tao Maabf1b3c2010-04-27 08:30:36 +08002009 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2010 if (di->i_suballoc_loc)
2011 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2012 else
2013 ac->ac_last_group = ocfs2_which_suballoc_group(
2014 le64_to_cpu(di->i_blkno),
2015 le16_to_cpu(di->i_suballoc_bit));
2016 }
Tao Ma13821152009-02-25 00:53:23 +08002017}
2018
2019static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2020 struct ocfs2_alloc_context *ac)
2021{
2022 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2023 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2024}
2025
Joel Becker1ed9b772010-05-06 13:59:06 +08002026int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002027 struct inode *dir,
2028 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002029 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002030 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002031 u16 *suballoc_bit,
2032 u64 *fe_blkno)
2033{
2034 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002035 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002036
2037 mlog_entry_void();
2038
2039 BUG_ON(!ac);
2040 BUG_ON(ac->ac_bits_given != 0);
2041 BUG_ON(ac->ac_bits_wanted != 1);
2042 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002043
Tao Ma13821152009-02-25 00:53:23 +08002044 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2045
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002046 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002047 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002048 1,
2049 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002050 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002051 if (status < 0) {
2052 mlog_errno(status);
2053 goto bail;
2054 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002055 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002056
Joel Becker7d1fe092010-04-13 14:30:19 +08002057 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002058
Joel Becker2b6cb572010-03-26 10:09:15 +08002059 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002060 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002061 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002062 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002063 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002064 status = 0;
2065bail:
2066 mlog_exit(status);
2067 return status;
2068}
2069
2070/* translate a group desc. blkno and it's bitmap offset into
2071 * disk cluster offset. */
2072static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2073 u64 bg_blkno,
2074 u16 bg_bit_off)
2075{
2076 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2077 u32 cluster = 0;
2078
2079 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2080
2081 if (bg_blkno != osb->first_cluster_group_blkno)
2082 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2083 cluster += (u32) bg_bit_off;
2084 return cluster;
2085}
2086
2087/* given a cluster offset, calculate which block group it belongs to
2088 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002089u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002090{
2091 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2092 u32 group_no;
2093
2094 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2095
2096 group_no = cluster / osb->bitmap_cpg;
2097 if (!group_no)
2098 return osb->first_cluster_group_blkno;
2099 return ocfs2_clusters_to_blocks(inode->i_sb,
2100 group_no * osb->bitmap_cpg);
2101}
2102
2103/* given the block number of a cluster start, calculate which cluster
2104 * group and descriptor bitmap offset that corresponds to. */
2105static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2106 u64 data_blkno,
2107 u64 *bg_blkno,
2108 u16 *bg_bit_off)
2109{
2110 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2111 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2112
2113 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2114
2115 *bg_blkno = ocfs2_which_cluster_group(inode,
2116 data_cluster);
2117
2118 if (*bg_blkno == osb->first_cluster_group_blkno)
2119 *bg_bit_off = (u16) data_cluster;
2120 else
2121 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2122 data_blkno - *bg_blkno);
2123}
2124
2125/*
2126 * min_bits - minimum contiguous chunk from this total allocation we
2127 * can handle. set to what we asked for originally for a full
2128 * contig. allocation, set to '1' to indicate we can deal with extents
2129 * of any size.
2130 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002131int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002132 struct ocfs2_alloc_context *ac,
2133 u32 min_clusters,
2134 u32 max_clusters,
2135 u32 *cluster_start,
2136 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002137{
2138 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002139 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002140 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002141 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002142
2143 mlog_entry_void();
2144
Mark Fashehccd979b2005-12-15 14:31:24 -08002145 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2146
2147 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2148 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002149
2150 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002151 WARN_ON(min_clusters > 1);
2152
Mark Fashehccd979b2005-12-15 14:31:24 -08002153 status = ocfs2_claim_local_alloc_bits(osb,
2154 handle,
2155 ac,
2156 bits_wanted,
2157 cluster_start,
2158 num_clusters);
2159 if (!status)
2160 atomic_inc(&osb->alloc_stats.local_data);
2161 } else {
2162 if (min_clusters > (osb->bitmap_cpg - 1)) {
2163 /* The only paths asking for contiguousness
2164 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002165 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2166 "group bitmap size %u!\n", min_clusters,
2167 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002168 status = -ENOSPC;
2169 goto bail;
2170 }
2171 /* clamp the current request down to a realistic size. */
2172 if (bits_wanted > (osb->bitmap_cpg - 1))
2173 bits_wanted = osb->bitmap_cpg - 1;
2174
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002175 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002176 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002177 bits_wanted,
2178 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002179 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002180 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002181 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002182 *cluster_start =
2183 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002184 res.sr_bg_blkno,
2185 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002186 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002187 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002188 }
2189 }
2190 if (status < 0) {
2191 if (status != -ENOSPC)
2192 mlog_errno(status);
2193 goto bail;
2194 }
2195
Tao Ma47119542010-04-22 14:09:15 +08002196 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002197
2198bail:
2199 mlog_exit(status);
2200 return status;
2201}
2202
Joel Becker1ed9b772010-05-06 13:59:06 +08002203int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002204 struct ocfs2_alloc_context *ac,
2205 u32 min_clusters,
2206 u32 *cluster_start,
2207 u32 *num_clusters)
2208{
2209 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2210
Joel Becker1ed9b772010-05-06 13:59:06 +08002211 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002212 bits_wanted, cluster_start, num_clusters);
2213}
2214
Mark Fashehb4414ee2010-03-11 18:31:09 -08002215static int ocfs2_block_group_clear_bits(handle_t *handle,
2216 struct inode *alloc_inode,
2217 struct ocfs2_group_desc *bg,
2218 struct buffer_head *group_bh,
2219 unsigned int bit_off,
2220 unsigned int num_bits,
2221 void (*undo_fn)(unsigned int bit,
2222 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002223{
2224 int status;
2225 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002226 struct ocfs2_group_desc *undo_bg = NULL;
2227
2228 mlog_entry_void();
2229
Joel Becker42035302008-11-13 14:49:15 -08002230 /* The caller got this descriptor from
2231 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2232 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002233
2234 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2235
Mark Fashehb4414ee2010-03-11 18:31:09 -08002236 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002237 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002238 group_bh,
2239 undo_fn ?
2240 OCFS2_JOURNAL_ACCESS_UNDO :
2241 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002242 if (status < 0) {
2243 mlog_errno(status);
2244 goto bail;
2245 }
2246
Mark Fashehb4414ee2010-03-11 18:31:09 -08002247 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002248 jbd_lock_bh_state(group_bh);
2249 undo_bg = (struct ocfs2_group_desc *)
2250 bh2jh(group_bh)->b_committed_data;
2251 BUG_ON(!undo_bg);
2252 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002253
2254 tmp = num_bits;
2255 while(tmp--) {
2256 ocfs2_clear_bit((bit_off + tmp),
2257 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002258 if (undo_fn)
2259 undo_fn(bit_off + tmp,
2260 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 }
2262 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2263
Mark Fashehb4414ee2010-03-11 18:31:09 -08002264 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002265 jbd_unlock_bh_state(group_bh);
2266
Joel Beckerec20cec2010-03-19 14:13:52 -07002267 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002268bail:
2269 return status;
2270}
2271
2272/*
2273 * expects the suballoc inode to already be locked.
2274 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002275static int _ocfs2_free_suballoc_bits(handle_t *handle,
2276 struct inode *alloc_inode,
2277 struct buffer_head *alloc_bh,
2278 unsigned int start_bit,
2279 u64 bg_blkno,
2280 unsigned int count,
2281 void (*undo_fn)(unsigned int bit,
2282 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002283{
2284 int status = 0;
2285 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002286 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2287 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2288 struct buffer_head *group_bh = NULL;
2289 struct ocfs2_group_desc *group;
2290
2291 mlog_entry_void();
2292
Joel Becker10995aa2008-11-13 14:49:12 -08002293 /* The alloc_bh comes from ocfs2_free_dinode() or
2294 * ocfs2_free_clusters(). The callers have all locked the
2295 * allocator and gotten alloc_bh from the lock call. This
2296 * validates the dinode buffer. Any corruption that has happended
2297 * is a code bug. */
2298 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002299 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2300
Mark Fashehb0697052006-03-03 10:24:33 -08002301 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2302 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2303 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002304
Joel Becker68f64d42008-11-13 14:49:14 -08002305 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2306 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 if (status < 0) {
2308 mlog_errno(status);
2309 goto bail;
2310 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002311 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002312
Mark Fashehccd979b2005-12-15 14:31:24 -08002313 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2314
2315 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2316 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002317 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002318 if (status < 0) {
2319 mlog_errno(status);
2320 goto bail;
2321 }
2322
Joel Becker0cf2f762009-02-12 16:41:25 -08002323 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2324 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002325 if (status < 0) {
2326 mlog_errno(status);
2327 goto bail;
2328 }
2329
2330 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2331 count);
2332 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2333 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002334 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002335
2336bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002337 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002338
2339 mlog_exit(status);
2340 return status;
2341}
2342
Mark Fashehb4414ee2010-03-11 18:31:09 -08002343int ocfs2_free_suballoc_bits(handle_t *handle,
2344 struct inode *alloc_inode,
2345 struct buffer_head *alloc_bh,
2346 unsigned int start_bit,
2347 u64 bg_blkno,
2348 unsigned int count)
2349{
2350 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2351 start_bit, bg_blkno, count, NULL);
2352}
2353
Mark Fasheh1fabe142006-10-09 18:11:45 -07002354int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002355 struct inode *inode_alloc_inode,
2356 struct buffer_head *inode_alloc_bh,
2357 struct ocfs2_dinode *di)
2358{
2359 u64 blk = le64_to_cpu(di->i_blkno);
2360 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2361 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2362
Tao Ma74380c42010-03-22 14:20:18 +08002363 if (di->i_suballoc_loc)
2364 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002365 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2366 inode_alloc_bh, bit, bg_blkno, 1);
2367}
2368
Mark Fashehb4414ee2010-03-11 18:31:09 -08002369static int _ocfs2_free_clusters(handle_t *handle,
2370 struct inode *bitmap_inode,
2371 struct buffer_head *bitmap_bh,
2372 u64 start_blk,
2373 unsigned int num_clusters,
2374 void (*undo_fn)(unsigned int bit,
2375 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002376{
2377 int status;
2378 u16 bg_start_bit;
2379 u64 bg_blkno;
2380 struct ocfs2_dinode *fe;
2381
2382 /* You can't ever have a contiguous set of clusters
2383 * bigger than a block group bitmap so we never have to worry
2384 * about looping on them. */
2385
2386 mlog_entry_void();
2387
2388 /* This is expensive. We can safely remove once this stuff has
2389 * gotten tested really well. */
2390 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2391
2392 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2393
2394 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2395 &bg_start_bit);
2396
Mark Fashehb0697052006-03-03 10:24:33 -08002397 mlog(0, "want to free %u clusters starting at block %llu\n",
2398 num_clusters, (unsigned long long)start_blk);
2399 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2400 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002401
Mark Fashehb4414ee2010-03-11 18:31:09 -08002402 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2403 bg_start_bit, bg_blkno,
2404 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002405 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002406 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002407 goto out;
2408 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002409
Mark Fasheh9c7af402008-07-28 18:02:53 -07002410 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2411 num_clusters);
2412
2413out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002414 mlog_exit(status);
2415 return status;
2416}
2417
Mark Fashehb4414ee2010-03-11 18:31:09 -08002418int ocfs2_free_clusters(handle_t *handle,
2419 struct inode *bitmap_inode,
2420 struct buffer_head *bitmap_bh,
2421 u64 start_blk,
2422 unsigned int num_clusters)
2423{
2424 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2425 start_blk, num_clusters,
2426 _ocfs2_set_bit);
2427}
2428
2429/*
2430 * Give never-used clusters back to the global bitmap. We don't need
2431 * to protect these bits in the undo buffer.
2432 */
2433int ocfs2_release_clusters(handle_t *handle,
2434 struct inode *bitmap_inode,
2435 struct buffer_head *bitmap_bh,
2436 u64 start_blk,
2437 unsigned int num_clusters)
2438{
2439 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2440 start_blk, num_clusters,
2441 _ocfs2_clear_bit);
2442}
2443
Mark Fashehccd979b2005-12-15 14:31:24 -08002444static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2445{
2446 printk("Block Group:\n");
2447 printk("bg_signature: %s\n", bg->bg_signature);
2448 printk("bg_size: %u\n", bg->bg_size);
2449 printk("bg_bits: %u\n", bg->bg_bits);
2450 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2451 printk("bg_chain: %u\n", bg->bg_chain);
2452 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb0697052006-03-03 10:24:33 -08002453 printk("bg_next_group: %llu\n",
2454 (unsigned long long)bg->bg_next_group);
2455 printk("bg_parent_dinode: %llu\n",
2456 (unsigned long long)bg->bg_parent_dinode);
2457 printk("bg_blkno: %llu\n",
2458 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002459}
2460
2461static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2462{
2463 int i;
2464
Mark Fashehb0697052006-03-03 10:24:33 -08002465 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002466 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb0697052006-03-03 10:24:33 -08002467 printk("i_size: %llu\n",
2468 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002469 printk("i_clusters: %u\n", fe->i_clusters);
2470 printk("i_generation: %u\n",
2471 le32_to_cpu(fe->i_generation));
2472 printk("id1.bitmap1.i_used: %u\n",
2473 le32_to_cpu(fe->id1.bitmap1.i_used));
2474 printk("id1.bitmap1.i_total: %u\n",
2475 le32_to_cpu(fe->id1.bitmap1.i_total));
2476 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2477 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2478 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2479 printk("id2.i_chain.cl_next_free_rec: %u\n",
2480 fe->id2.i_chain.cl_next_free_rec);
2481 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2482 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2483 fe->id2.i_chain.cl_recs[i].c_free);
2484 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2485 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb0697052006-03-03 10:24:33 -08002486 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2487 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002488 }
2489}
Tao Mae7d4cb62008-08-18 17:38:44 +08002490
2491/*
2492 * For a given allocation, determine which allocators will need to be
2493 * accessed, and lock them, reserving the appropriate number of bits.
2494 *
2495 * Sparse file systems call this from ocfs2_write_begin_nolock()
2496 * and ocfs2_allocate_unwritten_extents().
2497 *
2498 * File systems which don't support holes call this from
2499 * ocfs2_extend_allocation().
2500 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002501int ocfs2_lock_allocators(struct inode *inode,
2502 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002503 u32 clusters_to_add, u32 extents_to_split,
2504 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002505 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002506{
2507 int ret = 0, num_free_extents;
2508 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2509 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2510
2511 *meta_ac = NULL;
2512 if (data_ac)
2513 *data_ac = NULL;
2514
2515 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2516
Joel Becker3d03a302009-02-12 17:49:26 -08002517 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002518 if (num_free_extents < 0) {
2519 ret = num_free_extents;
2520 mlog_errno(ret);
2521 goto out;
2522 }
2523
2524 /*
2525 * Sparse allocation file systems need to be more conservative
2526 * with reserving room for expansion - the actual allocation
2527 * happens while we've got a journal handle open so re-taking
2528 * a cluster lock (because we ran out of room for another
2529 * extent) will violate ordering rules.
2530 *
2531 * Most of the time we'll only be seeing this 1 cluster at a time
2532 * anyway.
2533 *
2534 * Always lock for any unwritten extents - we might want to
2535 * add blocks during a split.
2536 */
2537 if (!num_free_extents ||
2538 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002539 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002540 if (ret < 0) {
2541 if (ret != -ENOSPC)
2542 mlog_errno(ret);
2543 goto out;
2544 }
2545 }
2546
2547 if (clusters_to_add == 0)
2548 goto out;
2549
2550 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2551 if (ret < 0) {
2552 if (ret != -ENOSPC)
2553 mlog_errno(ret);
2554 goto out;
2555 }
2556
2557out:
2558 if (ret) {
2559 if (*meta_ac) {
2560 ocfs2_free_alloc_context(*meta_ac);
2561 *meta_ac = NULL;
2562 }
2563
2564 /*
2565 * We cannot have an error and a non null *data_ac.
2566 */
2567 }
2568
2569 return ret;
2570}
wengang wang6ca497a2009-03-06 21:29:10 +08002571
2572/*
2573 * Read the inode specified by blkno to get suballoc_slot and
2574 * suballoc_bit.
2575 */
2576static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
Tao Ma889f0042010-09-02 13:10:10 +08002577 u16 *suballoc_slot, u64 *group_blkno,
2578 u16 *suballoc_bit)
wengang wang6ca497a2009-03-06 21:29:10 +08002579{
2580 int status;
2581 struct buffer_head *inode_bh = NULL;
2582 struct ocfs2_dinode *inode_fe;
2583
Joel Becker5b09b502009-04-21 16:31:20 -07002584 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002585
2586 /* dirty read disk */
2587 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2588 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002589 mlog(ML_ERROR, "read block %llu failed %d\n",
2590 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002591 goto bail;
2592 }
2593
2594 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2595 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002596 mlog(ML_ERROR, "invalid inode %llu requested\n",
2597 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002598 status = -EINVAL;
2599 goto bail;
2600 }
2601
Tao Ma0fba8132009-03-19 05:08:43 +08002602 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002603 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2604 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002605 (unsigned long long)blkno,
2606 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002607 status = -EINVAL;
2608 goto bail;
2609 }
2610
2611 if (suballoc_slot)
2612 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2613 if (suballoc_bit)
2614 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
Tao Ma889f0042010-09-02 13:10:10 +08002615 if (group_blkno)
2616 *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
wengang wang6ca497a2009-03-06 21:29:10 +08002617
2618bail:
2619 brelse(inode_bh);
2620
2621 mlog_exit(status);
2622 return status;
2623}
2624
2625/*
2626 * test whether bit is SET in allocator bitmap or not. on success, 0
2627 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2628 * is returned and *res is meaningless. Call this after you have
2629 * cluster locked against suballoc, or you may get a result based on
2630 * non-up2date contents
2631 */
2632static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2633 struct inode *suballoc,
Tao Ma889f0042010-09-02 13:10:10 +08002634 struct buffer_head *alloc_bh,
2635 u64 group_blkno, u64 blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002636 u16 bit, int *res)
2637{
Tao Maabf1b3c2010-04-27 08:30:36 +08002638 struct ocfs2_dinode *alloc_di;
wengang wang6ca497a2009-03-06 21:29:10 +08002639 struct ocfs2_group_desc *group;
2640 struct buffer_head *group_bh = NULL;
2641 u64 bg_blkno;
2642 int status;
2643
Joel Becker5b09b502009-04-21 16:31:20 -07002644 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2645 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002646
Tao Maabf1b3c2010-04-27 08:30:36 +08002647 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2648 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
wengang wang6ca497a2009-03-06 21:29:10 +08002649 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2650 (unsigned int)bit,
Tao Maabf1b3c2010-04-27 08:30:36 +08002651 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
wengang wang6ca497a2009-03-06 21:29:10 +08002652 status = -EINVAL;
2653 goto bail;
2654 }
2655
Tao Ma889f0042010-09-02 13:10:10 +08002656 bg_blkno = group_blkno ? group_blkno :
2657 ocfs2_which_suballoc_group(blkno, bit);
Tao Maabf1b3c2010-04-27 08:30:36 +08002658 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002659 &group_bh);
2660 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002661 mlog(ML_ERROR, "read group %llu failed %d\n",
2662 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002663 goto bail;
2664 }
2665
2666 group = (struct ocfs2_group_desc *) group_bh->b_data;
2667 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2668
2669bail:
2670 brelse(group_bh);
2671
2672 mlog_exit(status);
2673 return status;
2674}
2675
2676/*
2677 * Test if the bit representing this inode (blkno) is set in the
2678 * suballocator.
2679 *
2680 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2681 *
2682 * In the event of failure, a negative value is returned and *res is
2683 * meaningless.
2684 *
2685 * Callers must make sure to hold nfs_sync_lock to prevent
2686 * ocfs2_delete_inode() on another node from accessing the same
2687 * suballocator concurrently.
2688 */
2689int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2690{
2691 int status;
Tao Ma889f0042010-09-02 13:10:10 +08002692 u64 group_blkno = 0;
wengang wang6ca497a2009-03-06 21:29:10 +08002693 u16 suballoc_bit = 0, suballoc_slot = 0;
2694 struct inode *inode_alloc_inode;
2695 struct buffer_head *alloc_bh = NULL;
2696
Joel Becker5b09b502009-04-21 16:31:20 -07002697 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002698
2699 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
Tao Ma889f0042010-09-02 13:10:10 +08002700 &group_blkno, &suballoc_bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002701 if (status < 0) {
2702 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2703 goto bail;
2704 }
2705
2706 inode_alloc_inode =
2707 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2708 suballoc_slot);
2709 if (!inode_alloc_inode) {
2710 /* the error code could be inaccurate, but we are not able to
2711 * get the correct one. */
2712 status = -EINVAL;
2713 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2714 (u32)suballoc_slot);
2715 goto bail;
2716 }
2717
2718 mutex_lock(&inode_alloc_inode->i_mutex);
2719 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2720 if (status < 0) {
2721 mutex_unlock(&inode_alloc_inode->i_mutex);
2722 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2723 (u32)suballoc_slot, status);
2724 goto bail;
2725 }
2726
2727 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
Tao Ma889f0042010-09-02 13:10:10 +08002728 group_blkno, blkno, suballoc_bit, res);
wengang wang6ca497a2009-03-06 21:29:10 +08002729 if (status < 0)
2730 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2731
2732 ocfs2_inode_unlock(inode_alloc_inode, 0);
2733 mutex_unlock(&inode_alloc_inode->i_mutex);
2734
2735 iput(inode_alloc_inode);
2736 brelse(alloc_bh);
2737bail:
2738 mlog_exit(status);
2739 return status;
2740}