blob: b93d7e72175a563fe781baa7c024ef54f1d82b28 [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 u32 tmp_used;
1723 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001724 struct inode *alloc_inode = ac->ac_inode;
1725 struct buffer_head *group_bh = NULL;
1726 struct buffer_head *prev_group_bh = NULL;
1727 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1728 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1729 struct ocfs2_group_desc *bg;
1730
1731 chain = ac->ac_chain;
Mark Fashehb0697052006-03-03 10:24:33 -08001732 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1733 bits_wanted, chain,
1734 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001735
Joel Becker68f64d42008-11-13 14:49:14 -08001736 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1737 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1738 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001739 if (status < 0) {
1740 mlog_errno(status);
1741 goto bail;
1742 }
1743 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001744
1745 status = -ENOSPC;
1746 /* for now, the chain search is a bit simplistic. We just use
1747 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001748 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1749 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001750 ac->ac_max_block,
1751 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001752 if (!bg->bg_next_group)
1753 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001754
1755 brelse(prev_group_bh);
1756 prev_group_bh = NULL;
1757
Mark Fashehccd979b2005-12-15 14:31:24 -08001758 next_group = le64_to_cpu(bg->bg_next_group);
1759 prev_group_bh = group_bh;
1760 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001761 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1762 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001763 if (status < 0) {
1764 mlog_errno(status);
1765 goto bail;
1766 }
1767 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001768 }
1769 if (status < 0) {
1770 if (status != -ENOSPC)
1771 mlog_errno(status);
1772 goto bail;
1773 }
1774
Mark Fashehb0697052006-03-03 10:24:33 -08001775 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001776 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001777
Joel Becker7d1fe092010-04-13 14:30:19 +08001778 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001779
Joel Becker7d1fe092010-04-13 14:30:19 +08001780 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001781 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001782 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001783
Mark Fashehccd979b2005-12-15 14:31:24 -08001784
1785 /*
1786 * Keep track of previous block descriptor read. When
1787 * we find a target, if we have read more than X
1788 * number of descriptors, and the target is reasonably
1789 * empty, relink him to top of his chain.
1790 *
1791 * We've read 0 extra blocks and only send one more to
1792 * the transaction, yet the next guy to search has a
1793 * much easier time.
1794 *
1795 * Do this *after* figuring out how many bits we're taking out
1796 * of our target group.
1797 */
1798 if (ac->ac_allow_chain_relink &&
1799 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001800 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001801 status = ocfs2_relink_block_group(handle, alloc_inode,
1802 ac->ac_bh, group_bh,
1803 prev_group_bh, chain);
1804 if (status < 0) {
1805 mlog_errno(status);
1806 goto bail;
1807 }
1808 }
1809
1810 /* Ok, claim our bits now: set the info on dinode, chainlist
1811 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001812 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001813 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001814 ac->ac_bh,
1815 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001816 if (status < 0) {
1817 mlog_errno(status);
1818 goto bail;
1819 }
1820
1821 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001822 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1823 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001824 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001825
1826 status = ocfs2_block_group_set_bits(handle,
1827 alloc_inode,
1828 bg,
1829 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001830 res->sr_bit_offset,
1831 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001832 if (status < 0) {
1833 mlog_errno(status);
1834 goto bail;
1835 }
1836
Joel Becker7d1fe092010-04-13 14:30:19 +08001837 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001838 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001839
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001840 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001841bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001842 brelse(group_bh);
1843 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001844
1845 mlog_exit(status);
1846 return status;
1847}
1848
1849/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001850static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001851 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001852 u32 bits_wanted,
1853 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001854 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001855{
1856 int status;
1857 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001858 u16 bits_left = 0;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001859 u64 hint = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001860 struct ocfs2_chain_list *cl;
1861 struct ocfs2_dinode *fe;
1862
1863 mlog_entry_void();
1864
1865 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1866 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1867 BUG_ON(!ac->ac_bh);
1868
1869 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001870
1871 /* The bh was validated by the inode read during
1872 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1873 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1874
Mark Fashehccd979b2005-12-15 14:31:24 -08001875 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1876 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001877 ocfs2_error(ac->ac_inode->i_sb,
1878 "Chain allocator dinode %llu has %u used "
Mark Fashehb0697052006-03-03 10:24:33 -08001879 "bits but only %u total.",
1880 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001881 le32_to_cpu(fe->id1.bitmap1.i_used),
1882 le32_to_cpu(fe->id1.bitmap1.i_total));
1883 status = -EIO;
1884 goto bail;
1885 }
1886
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001887 res->sr_bg_blkno = hint;
Joel Becker7d1fe092010-04-13 14:30:19 +08001888 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001889 /* Attempt to short-circuit the usual search mechanism
1890 * by jumping straight to the most recently used
1891 * allocation group. This helps us mantain some
1892 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001893 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001894 min_bits, res, &bits_left);
1895 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001896 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001897 if (status < 0 && status != -ENOSPC) {
1898 mlog_errno(status);
1899 goto bail;
1900 }
1901 }
1902
Mark Fashehccd979b2005-12-15 14:31:24 -08001903 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1904
1905 victim = ocfs2_find_victim_chain(cl);
1906 ac->ac_chain = victim;
1907 ac->ac_allow_chain_relink = 1;
1908
Joel Becker7d1fe092010-04-13 14:30:19 +08001909 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1910 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001911 if (!status) {
1912 hint = ocfs2_group_from_res(res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001913 goto set_hint;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001914 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001915 if (status < 0 && status != -ENOSPC) {
1916 mlog_errno(status);
1917 goto bail;
1918 }
1919
1920 mlog(0, "Search of victim chain %u came up with nothing, "
1921 "trying all chains now.\n", victim);
1922
1923 /* If we didn't pick a good victim, then just default to
1924 * searching each chain in order. Don't allow chain relinking
1925 * because we only calculate enough journal credits for one
1926 * relink per alloc. */
1927 ac->ac_allow_chain_relink = 0;
1928 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1929 if (i == victim)
1930 continue;
1931 if (!cl->cl_recs[i].c_free)
1932 continue;
1933
1934 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001935 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001936 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001937 if (!status) {
1938 hint = ocfs2_group_from_res(res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 break;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001940 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001941 if (status < 0 && status != -ENOSPC) {
1942 mlog_errno(status);
1943 goto bail;
1944 }
1945 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001946
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001947set_hint:
1948 if (status != -ENOSPC) {
1949 /* If the next search of this group is not likely to
1950 * yield a suitable extent, then we reset the last
1951 * group hint so as to not waste a disk read */
1952 if (bits_left < min_bits)
1953 ac->ac_last_group = 0;
1954 else
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001955 ac->ac_last_group = hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001956 }
1957
1958bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001959 mlog_exit(status);
1960 return status;
1961}
1962
Joel Becker1ed9b772010-05-06 13:59:06 +08001963int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001964 struct ocfs2_alloc_context *ac,
1965 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001966 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001967 u16 *suballoc_bit_start,
1968 unsigned int *num_bits,
1969 u64 *blkno_start)
1970{
1971 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001972 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001973
1974 BUG_ON(!ac);
1975 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1976 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001977
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001978 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001979 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001980 bits_wanted,
1981 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001982 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001983 if (status < 0) {
1984 mlog_errno(status);
1985 goto bail;
1986 }
Joel Becker1ed9b772010-05-06 13:59:06 +08001987 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001988
Joel Becker2b6cb572010-03-26 10:09:15 +08001989 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001990 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08001991 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001992 ac->ac_bits_given += res.sr_bits;
1993 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001994 status = 0;
1995bail:
1996 mlog_exit(status);
1997 return status;
1998}
1999
Tao Ma13821152009-02-25 00:53:23 +08002000static void ocfs2_init_inode_ac_group(struct inode *dir,
Tao Maabf1b3c2010-04-27 08:30:36 +08002001 struct buffer_head *parent_di_bh,
Tao Ma13821152009-02-25 00:53:23 +08002002 struct ocfs2_alloc_context *ac)
2003{
Tao Maabf1b3c2010-04-27 08:30:36 +08002004 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
Tao Ma13821152009-02-25 00:53:23 +08002005 /*
2006 * Try to allocate inodes from some specific group.
2007 *
2008 * If the parent dir has recorded the last group used in allocation,
2009 * cool, use it. Otherwise if we try to allocate new inode from the
2010 * same slot the parent dir belongs to, use the same chunk.
2011 *
2012 * We are very careful here to avoid the mistake of setting
2013 * ac_last_group to a group descriptor from a different (unlocked) slot.
2014 */
2015 if (OCFS2_I(dir)->ip_last_used_group &&
2016 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2017 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
Tao Maabf1b3c2010-04-27 08:30:36 +08002018 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2019 if (di->i_suballoc_loc)
2020 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2021 else
2022 ac->ac_last_group = ocfs2_which_suballoc_group(
2023 le64_to_cpu(di->i_blkno),
2024 le16_to_cpu(di->i_suballoc_bit));
2025 }
Tao Ma13821152009-02-25 00:53:23 +08002026}
2027
2028static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2029 struct ocfs2_alloc_context *ac)
2030{
2031 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2032 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2033}
2034
Joel Becker1ed9b772010-05-06 13:59:06 +08002035int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002036 struct inode *dir,
2037 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002038 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002039 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002040 u16 *suballoc_bit,
2041 u64 *fe_blkno)
2042{
2043 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002044 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002045
2046 mlog_entry_void();
2047
2048 BUG_ON(!ac);
2049 BUG_ON(ac->ac_bits_given != 0);
2050 BUG_ON(ac->ac_bits_wanted != 1);
2051 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002052
Tao Ma13821152009-02-25 00:53:23 +08002053 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2054
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002055 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002056 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002057 1,
2058 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002059 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002060 if (status < 0) {
2061 mlog_errno(status);
2062 goto bail;
2063 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002064 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002065
Joel Becker7d1fe092010-04-13 14:30:19 +08002066 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002067
Joel Becker2b6cb572010-03-26 10:09:15 +08002068 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002069 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002070 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002071 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002072 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002073 status = 0;
2074bail:
2075 mlog_exit(status);
2076 return status;
2077}
2078
2079/* translate a group desc. blkno and it's bitmap offset into
2080 * disk cluster offset. */
2081static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2082 u64 bg_blkno,
2083 u16 bg_bit_off)
2084{
2085 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2086 u32 cluster = 0;
2087
2088 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2089
2090 if (bg_blkno != osb->first_cluster_group_blkno)
2091 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2092 cluster += (u32) bg_bit_off;
2093 return cluster;
2094}
2095
2096/* given a cluster offset, calculate which block group it belongs to
2097 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002098u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002099{
2100 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2101 u32 group_no;
2102
2103 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2104
2105 group_no = cluster / osb->bitmap_cpg;
2106 if (!group_no)
2107 return osb->first_cluster_group_blkno;
2108 return ocfs2_clusters_to_blocks(inode->i_sb,
2109 group_no * osb->bitmap_cpg);
2110}
2111
2112/* given the block number of a cluster start, calculate which cluster
2113 * group and descriptor bitmap offset that corresponds to. */
2114static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2115 u64 data_blkno,
2116 u64 *bg_blkno,
2117 u16 *bg_bit_off)
2118{
2119 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2120 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2121
2122 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2123
2124 *bg_blkno = ocfs2_which_cluster_group(inode,
2125 data_cluster);
2126
2127 if (*bg_blkno == osb->first_cluster_group_blkno)
2128 *bg_bit_off = (u16) data_cluster;
2129 else
2130 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2131 data_blkno - *bg_blkno);
2132}
2133
2134/*
2135 * min_bits - minimum contiguous chunk from this total allocation we
2136 * can handle. set to what we asked for originally for a full
2137 * contig. allocation, set to '1' to indicate we can deal with extents
2138 * of any size.
2139 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002140int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002141 struct ocfs2_alloc_context *ac,
2142 u32 min_clusters,
2143 u32 max_clusters,
2144 u32 *cluster_start,
2145 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002146{
2147 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002148 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002149 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002150 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002151
2152 mlog_entry_void();
2153
Mark Fashehccd979b2005-12-15 14:31:24 -08002154 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2155
2156 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2157 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002158
2159 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002160 WARN_ON(min_clusters > 1);
2161
Mark Fashehccd979b2005-12-15 14:31:24 -08002162 status = ocfs2_claim_local_alloc_bits(osb,
2163 handle,
2164 ac,
2165 bits_wanted,
2166 cluster_start,
2167 num_clusters);
2168 if (!status)
2169 atomic_inc(&osb->alloc_stats.local_data);
2170 } else {
2171 if (min_clusters > (osb->bitmap_cpg - 1)) {
2172 /* The only paths asking for contiguousness
2173 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002174 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2175 "group bitmap size %u!\n", min_clusters,
2176 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002177 status = -ENOSPC;
2178 goto bail;
2179 }
2180 /* clamp the current request down to a realistic size. */
2181 if (bits_wanted > (osb->bitmap_cpg - 1))
2182 bits_wanted = osb->bitmap_cpg - 1;
2183
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002184 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002185 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002186 bits_wanted,
2187 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002188 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002189 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002190 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002191 *cluster_start =
2192 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002193 res.sr_bg_blkno,
2194 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002195 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002196 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002197 }
2198 }
2199 if (status < 0) {
2200 if (status != -ENOSPC)
2201 mlog_errno(status);
2202 goto bail;
2203 }
2204
Tao Ma47119542010-04-22 14:09:15 +08002205 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002206
2207bail:
2208 mlog_exit(status);
2209 return status;
2210}
2211
Joel Becker1ed9b772010-05-06 13:59:06 +08002212int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002213 struct ocfs2_alloc_context *ac,
2214 u32 min_clusters,
2215 u32 *cluster_start,
2216 u32 *num_clusters)
2217{
2218 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2219
Joel Becker1ed9b772010-05-06 13:59:06 +08002220 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002221 bits_wanted, cluster_start, num_clusters);
2222}
2223
Mark Fashehb4414ee2010-03-11 18:31:09 -08002224static int ocfs2_block_group_clear_bits(handle_t *handle,
2225 struct inode *alloc_inode,
2226 struct ocfs2_group_desc *bg,
2227 struct buffer_head *group_bh,
2228 unsigned int bit_off,
2229 unsigned int num_bits,
2230 void (*undo_fn)(unsigned int bit,
2231 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002232{
2233 int status;
2234 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002235 struct ocfs2_group_desc *undo_bg = NULL;
2236
2237 mlog_entry_void();
2238
Joel Becker42035302008-11-13 14:49:15 -08002239 /* The caller got this descriptor from
2240 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2241 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002242
2243 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2244
Mark Fashehb4414ee2010-03-11 18:31:09 -08002245 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002246 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002247 group_bh,
2248 undo_fn ?
2249 OCFS2_JOURNAL_ACCESS_UNDO :
2250 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002251 if (status < 0) {
2252 mlog_errno(status);
2253 goto bail;
2254 }
2255
Mark Fashehb4414ee2010-03-11 18:31:09 -08002256 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002257 jbd_lock_bh_state(group_bh);
2258 undo_bg = (struct ocfs2_group_desc *)
2259 bh2jh(group_bh)->b_committed_data;
2260 BUG_ON(!undo_bg);
2261 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002262
2263 tmp = num_bits;
2264 while(tmp--) {
2265 ocfs2_clear_bit((bit_off + tmp),
2266 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002267 if (undo_fn)
2268 undo_fn(bit_off + tmp,
2269 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002270 }
2271 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2272
Mark Fashehb4414ee2010-03-11 18:31:09 -08002273 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002274 jbd_unlock_bh_state(group_bh);
2275
Joel Beckerec20cec2010-03-19 14:13:52 -07002276 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002277bail:
2278 return status;
2279}
2280
2281/*
2282 * expects the suballoc inode to already be locked.
2283 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002284static int _ocfs2_free_suballoc_bits(handle_t *handle,
2285 struct inode *alloc_inode,
2286 struct buffer_head *alloc_bh,
2287 unsigned int start_bit,
2288 u64 bg_blkno,
2289 unsigned int count,
2290 void (*undo_fn)(unsigned int bit,
2291 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002292{
2293 int status = 0;
2294 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002295 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2296 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2297 struct buffer_head *group_bh = NULL;
2298 struct ocfs2_group_desc *group;
2299
2300 mlog_entry_void();
2301
Joel Becker10995aa2008-11-13 14:49:12 -08002302 /* The alloc_bh comes from ocfs2_free_dinode() or
2303 * ocfs2_free_clusters(). The callers have all locked the
2304 * allocator and gotten alloc_bh from the lock call. This
2305 * validates the dinode buffer. Any corruption that has happended
2306 * is a code bug. */
2307 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002308 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2309
Mark Fashehb0697052006-03-03 10:24:33 -08002310 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2311 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2312 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002313
Joel Becker68f64d42008-11-13 14:49:14 -08002314 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2315 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002316 if (status < 0) {
2317 mlog_errno(status);
2318 goto bail;
2319 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002320 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002321
Mark Fashehccd979b2005-12-15 14:31:24 -08002322 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2323
2324 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2325 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002326 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002327 if (status < 0) {
2328 mlog_errno(status);
2329 goto bail;
2330 }
2331
Joel Becker0cf2f762009-02-12 16:41:25 -08002332 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2333 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002334 if (status < 0) {
2335 mlog_errno(status);
2336 goto bail;
2337 }
2338
2339 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2340 count);
2341 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2342 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002343 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002344
2345bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002346 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002347
2348 mlog_exit(status);
2349 return status;
2350}
2351
Mark Fashehb4414ee2010-03-11 18:31:09 -08002352int ocfs2_free_suballoc_bits(handle_t *handle,
2353 struct inode *alloc_inode,
2354 struct buffer_head *alloc_bh,
2355 unsigned int start_bit,
2356 u64 bg_blkno,
2357 unsigned int count)
2358{
2359 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2360 start_bit, bg_blkno, count, NULL);
2361}
2362
Mark Fasheh1fabe142006-10-09 18:11:45 -07002363int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002364 struct inode *inode_alloc_inode,
2365 struct buffer_head *inode_alloc_bh,
2366 struct ocfs2_dinode *di)
2367{
2368 u64 blk = le64_to_cpu(di->i_blkno);
2369 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2370 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2371
Tao Ma74380c42010-03-22 14:20:18 +08002372 if (di->i_suballoc_loc)
2373 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002374 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2375 inode_alloc_bh, bit, bg_blkno, 1);
2376}
2377
Mark Fashehb4414ee2010-03-11 18:31:09 -08002378static int _ocfs2_free_clusters(handle_t *handle,
2379 struct inode *bitmap_inode,
2380 struct buffer_head *bitmap_bh,
2381 u64 start_blk,
2382 unsigned int num_clusters,
2383 void (*undo_fn)(unsigned int bit,
2384 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002385{
2386 int status;
2387 u16 bg_start_bit;
2388 u64 bg_blkno;
2389 struct ocfs2_dinode *fe;
2390
2391 /* You can't ever have a contiguous set of clusters
2392 * bigger than a block group bitmap so we never have to worry
2393 * about looping on them. */
2394
2395 mlog_entry_void();
2396
2397 /* This is expensive. We can safely remove once this stuff has
2398 * gotten tested really well. */
2399 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2400
2401 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2402
2403 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2404 &bg_start_bit);
2405
Mark Fashehb0697052006-03-03 10:24:33 -08002406 mlog(0, "want to free %u clusters starting at block %llu\n",
2407 num_clusters, (unsigned long long)start_blk);
2408 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2409 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002410
Mark Fashehb4414ee2010-03-11 18:31:09 -08002411 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2412 bg_start_bit, bg_blkno,
2413 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002414 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002415 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002416 goto out;
2417 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002418
Mark Fasheh9c7af402008-07-28 18:02:53 -07002419 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2420 num_clusters);
2421
2422out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002423 mlog_exit(status);
2424 return status;
2425}
2426
Mark Fashehb4414ee2010-03-11 18:31:09 -08002427int ocfs2_free_clusters(handle_t *handle,
2428 struct inode *bitmap_inode,
2429 struct buffer_head *bitmap_bh,
2430 u64 start_blk,
2431 unsigned int num_clusters)
2432{
2433 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2434 start_blk, num_clusters,
2435 _ocfs2_set_bit);
2436}
2437
2438/*
2439 * Give never-used clusters back to the global bitmap. We don't need
2440 * to protect these bits in the undo buffer.
2441 */
2442int ocfs2_release_clusters(handle_t *handle,
2443 struct inode *bitmap_inode,
2444 struct buffer_head *bitmap_bh,
2445 u64 start_blk,
2446 unsigned int num_clusters)
2447{
2448 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2449 start_blk, num_clusters,
2450 _ocfs2_clear_bit);
2451}
2452
Mark Fashehccd979b2005-12-15 14:31:24 -08002453static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2454{
2455 printk("Block Group:\n");
2456 printk("bg_signature: %s\n", bg->bg_signature);
2457 printk("bg_size: %u\n", bg->bg_size);
2458 printk("bg_bits: %u\n", bg->bg_bits);
2459 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2460 printk("bg_chain: %u\n", bg->bg_chain);
2461 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb0697052006-03-03 10:24:33 -08002462 printk("bg_next_group: %llu\n",
2463 (unsigned long long)bg->bg_next_group);
2464 printk("bg_parent_dinode: %llu\n",
2465 (unsigned long long)bg->bg_parent_dinode);
2466 printk("bg_blkno: %llu\n",
2467 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002468}
2469
2470static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2471{
2472 int i;
2473
Mark Fashehb0697052006-03-03 10:24:33 -08002474 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002475 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb0697052006-03-03 10:24:33 -08002476 printk("i_size: %llu\n",
2477 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002478 printk("i_clusters: %u\n", fe->i_clusters);
2479 printk("i_generation: %u\n",
2480 le32_to_cpu(fe->i_generation));
2481 printk("id1.bitmap1.i_used: %u\n",
2482 le32_to_cpu(fe->id1.bitmap1.i_used));
2483 printk("id1.bitmap1.i_total: %u\n",
2484 le32_to_cpu(fe->id1.bitmap1.i_total));
2485 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2486 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2487 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2488 printk("id2.i_chain.cl_next_free_rec: %u\n",
2489 fe->id2.i_chain.cl_next_free_rec);
2490 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2491 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2492 fe->id2.i_chain.cl_recs[i].c_free);
2493 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2494 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb0697052006-03-03 10:24:33 -08002495 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2496 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002497 }
2498}
Tao Mae7d4cb62008-08-18 17:38:44 +08002499
2500/*
2501 * For a given allocation, determine which allocators will need to be
2502 * accessed, and lock them, reserving the appropriate number of bits.
2503 *
2504 * Sparse file systems call this from ocfs2_write_begin_nolock()
2505 * and ocfs2_allocate_unwritten_extents().
2506 *
2507 * File systems which don't support holes call this from
2508 * ocfs2_extend_allocation().
2509 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002510int ocfs2_lock_allocators(struct inode *inode,
2511 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002512 u32 clusters_to_add, u32 extents_to_split,
2513 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002514 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002515{
2516 int ret = 0, num_free_extents;
2517 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2518 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2519
2520 *meta_ac = NULL;
2521 if (data_ac)
2522 *data_ac = NULL;
2523
2524 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2525
Joel Becker3d03a302009-02-12 17:49:26 -08002526 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002527 if (num_free_extents < 0) {
2528 ret = num_free_extents;
2529 mlog_errno(ret);
2530 goto out;
2531 }
2532
2533 /*
2534 * Sparse allocation file systems need to be more conservative
2535 * with reserving room for expansion - the actual allocation
2536 * happens while we've got a journal handle open so re-taking
2537 * a cluster lock (because we ran out of room for another
2538 * extent) will violate ordering rules.
2539 *
2540 * Most of the time we'll only be seeing this 1 cluster at a time
2541 * anyway.
2542 *
2543 * Always lock for any unwritten extents - we might want to
2544 * add blocks during a split.
2545 */
2546 if (!num_free_extents ||
2547 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002548 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002549 if (ret < 0) {
2550 if (ret != -ENOSPC)
2551 mlog_errno(ret);
2552 goto out;
2553 }
2554 }
2555
2556 if (clusters_to_add == 0)
2557 goto out;
2558
2559 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2560 if (ret < 0) {
2561 if (ret != -ENOSPC)
2562 mlog_errno(ret);
2563 goto out;
2564 }
2565
2566out:
2567 if (ret) {
2568 if (*meta_ac) {
2569 ocfs2_free_alloc_context(*meta_ac);
2570 *meta_ac = NULL;
2571 }
2572
2573 /*
2574 * We cannot have an error and a non null *data_ac.
2575 */
2576 }
2577
2578 return ret;
2579}
wengang wang6ca497a2009-03-06 21:29:10 +08002580
2581/*
2582 * Read the inode specified by blkno to get suballoc_slot and
2583 * suballoc_bit.
2584 */
2585static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
Tao Ma889f0042010-09-02 13:10:10 +08002586 u16 *suballoc_slot, u64 *group_blkno,
2587 u16 *suballoc_bit)
wengang wang6ca497a2009-03-06 21:29:10 +08002588{
2589 int status;
2590 struct buffer_head *inode_bh = NULL;
2591 struct ocfs2_dinode *inode_fe;
2592
Joel Becker5b09b502009-04-21 16:31:20 -07002593 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002594
2595 /* dirty read disk */
2596 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2597 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002598 mlog(ML_ERROR, "read block %llu failed %d\n",
2599 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002600 goto bail;
2601 }
2602
2603 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2604 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002605 mlog(ML_ERROR, "invalid inode %llu requested\n",
2606 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002607 status = -EINVAL;
2608 goto bail;
2609 }
2610
Tao Ma0fba8132009-03-19 05:08:43 +08002611 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002612 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2613 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002614 (unsigned long long)blkno,
2615 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002616 status = -EINVAL;
2617 goto bail;
2618 }
2619
2620 if (suballoc_slot)
2621 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2622 if (suballoc_bit)
2623 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
Tao Ma889f0042010-09-02 13:10:10 +08002624 if (group_blkno)
2625 *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
wengang wang6ca497a2009-03-06 21:29:10 +08002626
2627bail:
2628 brelse(inode_bh);
2629
2630 mlog_exit(status);
2631 return status;
2632}
2633
2634/*
2635 * test whether bit is SET in allocator bitmap or not. on success, 0
2636 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2637 * is returned and *res is meaningless. Call this after you have
2638 * cluster locked against suballoc, or you may get a result based on
2639 * non-up2date contents
2640 */
2641static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2642 struct inode *suballoc,
Tao Ma889f0042010-09-02 13:10:10 +08002643 struct buffer_head *alloc_bh,
2644 u64 group_blkno, u64 blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002645 u16 bit, int *res)
2646{
Tao Maabf1b3c2010-04-27 08:30:36 +08002647 struct ocfs2_dinode *alloc_di;
wengang wang6ca497a2009-03-06 21:29:10 +08002648 struct ocfs2_group_desc *group;
2649 struct buffer_head *group_bh = NULL;
2650 u64 bg_blkno;
2651 int status;
2652
Joel Becker5b09b502009-04-21 16:31:20 -07002653 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2654 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002655
Tao Maabf1b3c2010-04-27 08:30:36 +08002656 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2657 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
wengang wang6ca497a2009-03-06 21:29:10 +08002658 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2659 (unsigned int)bit,
Tao Maabf1b3c2010-04-27 08:30:36 +08002660 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
wengang wang6ca497a2009-03-06 21:29:10 +08002661 status = -EINVAL;
2662 goto bail;
2663 }
2664
Tao Ma889f0042010-09-02 13:10:10 +08002665 bg_blkno = group_blkno ? group_blkno :
2666 ocfs2_which_suballoc_group(blkno, bit);
Tao Maabf1b3c2010-04-27 08:30:36 +08002667 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002668 &group_bh);
2669 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002670 mlog(ML_ERROR, "read group %llu failed %d\n",
2671 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002672 goto bail;
2673 }
2674
2675 group = (struct ocfs2_group_desc *) group_bh->b_data;
2676 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2677
2678bail:
2679 brelse(group_bh);
2680
2681 mlog_exit(status);
2682 return status;
2683}
2684
2685/*
2686 * Test if the bit representing this inode (blkno) is set in the
2687 * suballocator.
2688 *
2689 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2690 *
2691 * In the event of failure, a negative value is returned and *res is
2692 * meaningless.
2693 *
2694 * Callers must make sure to hold nfs_sync_lock to prevent
2695 * ocfs2_delete_inode() on another node from accessing the same
2696 * suballocator concurrently.
2697 */
2698int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2699{
2700 int status;
Tao Ma889f0042010-09-02 13:10:10 +08002701 u64 group_blkno = 0;
wengang wang6ca497a2009-03-06 21:29:10 +08002702 u16 suballoc_bit = 0, suballoc_slot = 0;
2703 struct inode *inode_alloc_inode;
2704 struct buffer_head *alloc_bh = NULL;
2705
Joel Becker5b09b502009-04-21 16:31:20 -07002706 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002707
2708 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
Tao Ma889f0042010-09-02 13:10:10 +08002709 &group_blkno, &suballoc_bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002710 if (status < 0) {
2711 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2712 goto bail;
2713 }
2714
2715 inode_alloc_inode =
2716 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2717 suballoc_slot);
2718 if (!inode_alloc_inode) {
2719 /* the error code could be inaccurate, but we are not able to
2720 * get the correct one. */
2721 status = -EINVAL;
2722 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2723 (u32)suballoc_slot);
2724 goto bail;
2725 }
2726
2727 mutex_lock(&inode_alloc_inode->i_mutex);
2728 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2729 if (status < 0) {
2730 mutex_unlock(&inode_alloc_inode->i_mutex);
2731 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2732 (u32)suballoc_slot, status);
2733 goto bail;
2734 }
2735
2736 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
Tao Ma889f0042010-09-02 13:10:10 +08002737 group_blkno, blkno, suballoc_bit, res);
wengang wang6ca497a2009-03-06 21:29:10 +08002738 if (status < 0)
2739 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2740
2741 ocfs2_inode_unlock(inode_alloc_inode, 0);
2742 mutex_unlock(&inode_alloc_inode->i_mutex);
2743
2744 iput(inode_alloc_inode);
2745 brelse(alloc_bh);
2746bail:
2747 mlog_exit(status);
2748 return status;
2749}