blob: 602c05eae7c53c906427203ca4aebb74fee185af [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 {
57 u64 sr_bg_blkno; /* The bg we allocated from */
58 unsigned int sr_bit_offset; /* The bit in the bg */
59 unsigned int sr_bits; /* How many bits we claimed */
60};
61
Mark Fashehccd979b2005-12-15 14:31:24 -080062static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
63static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
64static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070065static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080066 struct inode *alloc_inode,
67 struct buffer_head *bg_bh,
68 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080069 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080070 u16 my_chain,
71 struct ocfs2_chain_list *cl);
72static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
73 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070074 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080075 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080076 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080077 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080078
Mark Fashehccd979b2005-12-15 14:31:24 -080079static int ocfs2_cluster_group_search(struct inode *inode,
80 struct buffer_head *group_bh,
81 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070082 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080083 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080084static int ocfs2_block_group_search(struct inode *inode,
85 struct buffer_head *group_bh,
86 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070087 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080088 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +080089static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070090 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080091 u32 bits_wanted,
92 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +080093 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080094static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
95 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070096static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080097 struct inode *alloc_inode,
98 struct ocfs2_group_desc *bg,
99 struct buffer_head *group_bh,
100 unsigned int bit_off,
101 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700102static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800103 struct inode *alloc_inode,
104 struct buffer_head *fe_bh,
105 struct buffer_head *bg_bh,
106 struct buffer_head *prev_bg_bh,
107 u16 chain);
108static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
109 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800110static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
111 u64 bg_blkno,
112 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static inline void ocfs2_block_to_cluster_group(struct inode *inode,
114 u64 data_blkno,
115 u64 *bg_blkno,
116 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700117static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
118 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800119 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700120 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800121
Mark Fasheh9c7af402008-07-28 18:02:53 -0700122void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800123{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700124 struct inode *inode = ac->ac_inode;
125
126 if (inode) {
127 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700128 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700129
130 mutex_unlock(&inode->i_mutex);
131
132 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800133 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700134 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700135 brelse(ac->ac_bh);
136 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800137 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800138}
139
140void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
141{
142 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800143 kfree(ac);
144}
145
146static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
147{
148 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
149}
150
Joel Becker57e3e792008-11-13 14:49:13 -0800151#define do_error(fmt, ...) \
152 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800153 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800154 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
155 else \
156 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
157 } while (0)
158
Joel Becker970e4932008-11-13 14:49:19 -0800159static int ocfs2_validate_gd_self(struct super_block *sb,
160 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800161 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800162{
163 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
164
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700165 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800166 do_error("Group descriptor #%llu has bad signature %.*s",
167 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800168 gd->bg_signature);
169 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700170 }
171
Joel Becker68f64d42008-11-13 14:49:14 -0800172 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
173 do_error("Group descriptor #%llu has an invalid bg_blkno "
174 "of %llu",
175 (unsigned long long)bh->b_blocknr,
176 (unsigned long long)le64_to_cpu(gd->bg_blkno));
177 return -EINVAL;
178 }
179
180 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
181 do_error("Group descriptor #%llu has an invalid "
182 "fs_generation of #%u",
183 (unsigned long long)bh->b_blocknr,
184 le32_to_cpu(gd->bg_generation));
185 return -EINVAL;
186 }
187
Joel Becker970e4932008-11-13 14:49:19 -0800188 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
189 do_error("Group descriptor #%llu has bit count %u but "
190 "claims that %u are free",
191 (unsigned long long)bh->b_blocknr,
192 le16_to_cpu(gd->bg_bits),
193 le16_to_cpu(gd->bg_free_bits_count));
194 return -EINVAL;
195 }
196
197 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
198 do_error("Group descriptor #%llu has bit count %u but "
199 "max bitmap bits of %u",
200 (unsigned long long)bh->b_blocknr,
201 le16_to_cpu(gd->bg_bits),
202 8 * le16_to_cpu(gd->bg_size));
203 return -EINVAL;
204 }
205
206 return 0;
207}
208
209static int ocfs2_validate_gd_parent(struct super_block *sb,
210 struct ocfs2_dinode *di,
211 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800212 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800213{
214 unsigned int max_bits;
215 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
216
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700217 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800218 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800219 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800220 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800221 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
222 (unsigned long long)le64_to_cpu(di->i_blkno));
223 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700224 }
225
226 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
227 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800228 do_error("Group descriptor #%llu has bit count of %u",
229 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800230 le16_to_cpu(gd->bg_bits));
231 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700232 }
233
Tao Ma78c37eb2010-03-03 11:26:27 +0800234 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
235 if ((le16_to_cpu(gd->bg_chain) >
236 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
237 ((le16_to_cpu(gd->bg_chain) ==
238 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800239 do_error("Group descriptor #%llu has bad chain %u",
240 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800241 le16_to_cpu(gd->bg_chain));
242 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700243 }
244
Joel Becker970e4932008-11-13 14:49:19 -0800245 return 0;
246}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700247
Joel Becker57e3e792008-11-13 14:49:13 -0800248#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700249
Joel Becker970e4932008-11-13 14:49:19 -0800250/*
251 * This version only prints errors. It does not fail the filesystem, and
252 * exists only for resize.
253 */
254int ocfs2_check_group_descriptor(struct super_block *sb,
255 struct ocfs2_dinode *di,
256 struct buffer_head *bh)
257{
258 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700259 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800260
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700261 BUG_ON(!buffer_uptodate(bh));
262
263 /*
264 * If the ecc fails, we return the error but otherwise
265 * leave the filesystem running. We know any error is
266 * local to this block.
267 */
268 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700269 if (rc) {
270 mlog(ML_ERROR,
271 "Checksum failed for group descriptor %llu\n",
272 (unsigned long long)bh->b_blocknr);
273 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700274 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800275 if (!rc)
276 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
277
278 return rc;
279}
280
281static int ocfs2_validate_group_descriptor(struct super_block *sb,
282 struct buffer_head *bh)
283{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700284 int rc;
285 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
286
Joel Becker970e4932008-11-13 14:49:19 -0800287 mlog(0, "Validating group descriptor %llu\n",
288 (unsigned long long)bh->b_blocknr);
289
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700290 BUG_ON(!buffer_uptodate(bh));
291
292 /*
293 * If the ecc fails, we return the error but otherwise
294 * leave the filesystem running. We know any error is
295 * local to this block.
296 */
297 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
298 if (rc)
299 return rc;
300
301 /*
302 * Errors after here are fatal.
303 */
304
Joel Becker970e4932008-11-13 14:49:19 -0800305 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700306}
307
Joel Becker68f64d42008-11-13 14:49:14 -0800308int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
309 u64 gd_blkno, struct buffer_head **bh)
310{
311 int rc;
312 struct buffer_head *tmp = *bh;
313
Joel Becker8cb471e2009-02-10 20:00:41 -0800314 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800315 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800316 if (rc)
317 goto out;
318
Joel Becker970e4932008-11-13 14:49:19 -0800319 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800320 if (rc) {
321 brelse(tmp);
322 goto out;
323 }
324
325 /* If ocfs2_read_block() got us a new bh, pass it up. */
326 if (!*bh)
327 *bh = tmp;
328
329out:
330 return rc;
331}
332
Joel Becker798db352010-04-13 14:26:32 +0800333static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
334 struct ocfs2_group_desc *bg,
335 struct ocfs2_chain_list *cl,
336 u64 p_blkno, u32 clusters)
337{
338 struct ocfs2_extent_list *el = &bg->bg_list;
339 struct ocfs2_extent_rec *rec;
340
341 BUG_ON(!ocfs2_supports_discontig_bh(osb));
342 if (!el->l_next_free_rec)
343 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
344 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
345 rec->e_blkno = p_blkno;
346 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
347 le16_to_cpu(cl->cl_bpc));
348 rec->e_leaf_clusters = cpu_to_le32(clusters);
349 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
350 le16_add_cpu(&el->l_next_free_rec, 1);
351}
352
Mark Fasheh1fabe142006-10-09 18:11:45 -0700353static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800354 struct inode *alloc_inode,
355 struct buffer_head *bg_bh,
356 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800357 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800358 u16 my_chain,
359 struct ocfs2_chain_list *cl)
360{
361 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800362 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800363 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
364 struct super_block * sb = alloc_inode->i_sb;
365
366 mlog_entry_void();
367
368 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800369 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
370 "b_blocknr (%llu)",
371 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800372 (unsigned long long) bg_bh->b_blocknr);
373 status = -EIO;
374 goto bail;
375 }
376
Joel Becker13723d02008-10-17 19:25:01 -0700377 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800378 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700379 bg_bh,
380 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 if (status < 0) {
382 mlog_errno(status);
383 goto bail;
384 }
385
386 memset(bg, 0, sb->s_blocksize);
387 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
388 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Joel Becker4cbe4242010-04-13 14:26:12 +0800389 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 bg->bg_chain = cpu_to_le16(my_chain);
391 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
392 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
393 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800394 if (group_clusters == le16_to_cpu(cl->cl_cpg))
395 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
396 else
397 ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno,
398 group_clusters);
399
Mark Fashehccd979b2005-12-15 14:31:24 -0800400 /* set the 1st bit in the bitmap to account for the descriptor block */
401 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
402 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
403
Joel Beckerec20cec2010-03-19 14:13:52 -0700404 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800405
406 /* There is no need to zero out or otherwise initialize the
407 * other blocks in a group - All valid FS metadata in a block
408 * group stores the superblock fs_generation value at
409 * allocation time. */
410
411bail:
412 mlog_exit(status);
413 return status;
414}
415
416static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
417{
418 u16 curr, best;
419
420 best = curr = 0;
421 while (curr < le16_to_cpu(cl->cl_count)) {
422 if (le32_to_cpu(cl->cl_recs[best].c_total) >
423 le32_to_cpu(cl->cl_recs[curr].c_total))
424 best = curr;
425 curr++;
426 }
427 return best;
428}
429
Joel Becker798db352010-04-13 14:26:32 +0800430static struct buffer_head *
431ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
432 struct inode *alloc_inode,
433 struct ocfs2_alloc_context *ac,
434 struct ocfs2_chain_list *cl)
435{
436 int status;
437 u32 bit_off, num_bits;
438 u64 bg_blkno;
439 struct buffer_head *bg_bh;
440 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
441
442 status = ocfs2_claim_clusters(osb, handle, ac,
443 le16_to_cpu(cl->cl_cpg), &bit_off,
444 &num_bits);
445 if (status < 0) {
446 if (status != -ENOSPC)
447 mlog_errno(status);
448 goto bail;
449 }
450
451 /* setup the group */
452 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
453 mlog(0, "new descriptor, record %u, at block %llu\n",
454 alloc_rec, (unsigned long long)bg_blkno);
455
456 bg_bh = sb_getblk(osb->sb, bg_blkno);
457 if (!bg_bh) {
458 status = -EIO;
459 mlog_errno(status);
460 goto bail;
461 }
462 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
463
464 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
465 bg_blkno, num_bits, alloc_rec, cl);
466 if (status < 0) {
467 brelse(bg_bh);
468 mlog_errno(status);
469 }
470
471bail:
472 return status ? ERR_PTR(status) : bg_bh;
473}
474
475static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
476 handle_t *handle,
477 struct ocfs2_alloc_context *ac,
478 unsigned int min_bits,
479 u32 *bit_off, u32 *num_bits)
480{
481 int status;
482
483 while (min_bits) {
484 status = ocfs2_claim_clusters(osb, handle, ac, min_bits,
485 bit_off, num_bits);
486 if (status != -ENOSPC)
487 break;
488
489 min_bits >>= 1;
490 }
491
492 return status;
493}
494
495static int ocfs2_block_group_grow_discontig(handle_t *handle,
496 struct inode *alloc_inode,
497 struct buffer_head *bg_bh,
498 struct ocfs2_alloc_context *ac,
499 struct ocfs2_chain_list *cl,
500 unsigned int min_bits)
501{
502 int status;
503 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
504 struct ocfs2_group_desc *bg =
505 (struct ocfs2_group_desc *)bg_bh->b_data;
506 unsigned int needed =
507 ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
508 u32 p_cpos, clusters;
509 u64 p_blkno;
510 struct ocfs2_extent_list *el = &bg->bg_list;
511
512 status = ocfs2_journal_access_gd(handle,
513 INODE_CACHE(alloc_inode),
514 bg_bh,
515 OCFS2_JOURNAL_ACCESS_CREATE);
516 if (status < 0) {
517 mlog_errno(status);
518 goto bail;
519 }
520
521 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
522 le16_to_cpu(el->l_count))) {
523 status = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_ALLOC);
524 if (status) {
525 mlog_errno(status);
526 goto bail;
527 }
528
529 if (min_bits > needed)
530 min_bits = needed;
531 status = ocfs2_block_group_claim_bits(osb, handle, ac,
532 min_bits, &p_cpos,
533 &clusters);
534 if (status < 0) {
535 if (status != -ENOSPC)
536 mlog_errno(status);
537 goto bail;
538 }
539 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
540 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
541 clusters);
542
543 min_bits = clusters;
544 needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
545 }
546
547 if (needed > 0) {
548 }
549
550 ocfs2_journal_dirty(handle, bg_bh);
551
552bail:
553 return status;
554}
555
556static void ocfs2_bg_alloc_cleanup(struct inode *alloc_inode,
557 struct buffer_head *bg_bh,
558 struct ocfs2_cached_dealloc_ctxt *dealloc)
559{
560 int i;
561 struct ocfs2_group_desc *bg;
562 struct ocfs2_extent_list *el;
563 struct ocfs2_extent_rec *rec;
564
565 if (!bg_bh)
566 return;
567
568 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
569 el = &bg->bg_list;
570 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
571 rec = &el->l_recs[i];
572 ocfs2_cache_cluster_dealloc(dealloc,
573 le64_to_cpu(rec->e_blkno),
574 le32_to_cpu(rec->e_leaf_clusters));
575 }
576
577 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
578 brelse(bg_bh);
579}
580
581static struct buffer_head *
582ocfs2_block_group_alloc_discontig(handle_t *handle,
583 struct inode *alloc_inode,
584 struct ocfs2_alloc_context *ac,
585 struct ocfs2_chain_list *cl,
586 struct ocfs2_cached_dealloc_ctxt *dealloc)
587{
588 int status;
589 u32 bit_off, num_bits;
590 u64 bg_blkno;
591 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
592 struct buffer_head *bg_bh = NULL;
593 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
594 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
595
596 if (!ocfs2_supports_discontig_bh(osb)) {
597 status = -ENOSPC;
598 goto bail;
599 }
600
601 /* Claim the first region */
602 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
603 &bit_off, &num_bits);
604 if (status < 0) {
605 if (status != -ENOSPC)
606 mlog_errno(status);
607 goto bail;
608 }
609 min_bits = num_bits;
610
611 /* setup the group */
612 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
613 mlog(0, "new descriptor, record %u, at block %llu\n",
614 alloc_rec, (unsigned long long)bg_blkno);
615
616 bg_bh = sb_getblk(osb->sb, bg_blkno);
617 if (!bg_bh) {
618 status = -EIO;
619 mlog_errno(status);
620 goto bail;
621 }
622 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
623
624 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
625 bg_blkno, num_bits, alloc_rec, cl);
626 if (status < 0) {
627 mlog_errno(status);
628 goto bail;
629 }
630
631 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
632 bg_bh, ac, cl, min_bits);
633 if (status)
634 mlog_errno(status);
635
636bail:
637 if (status)
638 ocfs2_bg_alloc_cleanup(alloc_inode, bg_bh, dealloc);
639 return status ? ERR_PTR(status) : bg_bh;
640}
641
Mark Fashehccd979b2005-12-15 14:31:24 -0800642/*
643 * We expect the block group allocator to already be locked.
644 */
645static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
646 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700647 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800648 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800649 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800650 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800651{
652 int status, credits;
653 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
654 struct ocfs2_chain_list *cl;
655 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700656 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800657 u64 bg_blkno;
658 struct buffer_head *bg_bh = NULL;
659 struct ocfs2_group_desc *bg;
Joel Becker798db352010-04-13 14:26:32 +0800660 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -0800661
662 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
663
664 mlog_entry_void();
665
Joel Becker798db352010-04-13 14:26:32 +0800666 ocfs2_init_dealloc_ctxt(&dealloc);
667
Mark Fashehccd979b2005-12-15 14:31:24 -0800668 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700669 status = ocfs2_reserve_clusters_with_limit(osb,
670 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800671 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800672 if (status < 0) {
673 if (status != -ENOSPC)
674 mlog_errno(status);
675 goto bail;
676 }
677
678 credits = ocfs2_calc_group_alloc_credits(osb->sb,
679 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700680 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800681 if (IS_ERR(handle)) {
682 status = PTR_ERR(handle);
683 handle = NULL;
684 mlog_errno(status);
685 goto bail;
686 }
687
Tao Mafeb473a2009-02-25 00:53:25 +0800688 if (last_alloc_group && *last_alloc_group != 0) {
689 mlog(0, "use old allocation group %llu for block group alloc\n",
690 (unsigned long long)*last_alloc_group);
691 ac->ac_last_group = *last_alloc_group;
692 }
Joel Becker798db352010-04-13 14:26:32 +0800693
694 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
695 ac, cl);
696 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
697 bg_bh = ocfs2_block_group_alloc_discontig(handle,
698 alloc_inode,
699 ac, cl,
700 &dealloc);
701 if (IS_ERR(bg_bh)) {
702 status = PTR_ERR(bg_bh);
703 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 if (status != -ENOSPC)
705 mlog_errno(status);
706 goto bail;
707 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800708 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
709
Joel Becker0cf2f762009-02-12 16:41:25 -0800710 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700711 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800712 if (status < 0) {
713 mlog_errno(status);
714 goto bail;
715 }
716
Joel Becker798db352010-04-13 14:26:32 +0800717 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800718 le16_to_cpu(bg->bg_free_bits_count));
Joel Becker798db352010-04-13 14:26:32 +0800719 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total,
720 le16_to_cpu(bg->bg_bits));
721 cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
723 le16_add_cpu(&cl->cl_next_free_rec, 1);
724
725 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
726 le16_to_cpu(bg->bg_free_bits_count));
727 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
728 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
729
Joel Beckerec20cec2010-03-19 14:13:52 -0700730 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800731
732 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
733 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
734 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
735 le32_to_cpu(fe->i_clusters)));
736 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
737 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700738 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800739
740 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800741
742 /* save the new last alloc group so that the caller can cache it. */
743 if (last_alloc_group)
744 *last_alloc_group = ac->ac_last_group;
745
Mark Fashehccd979b2005-12-15 14:31:24 -0800746bail:
747 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700748 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800749
Joel Becker798db352010-04-13 14:26:32 +0800750 if (ocfs2_dealloc_has_cluster(&dealloc)) {
751 ocfs2_schedule_truncate_log_flush(osb, 1);
752 ocfs2_run_deallocs(osb, &dealloc);
753 }
754
Mark Fashehccd979b2005-12-15 14:31:24 -0800755 if (ac)
756 ocfs2_free_alloc_context(ac);
757
Mark Fasheha81cb882008-10-07 14:25:16 -0700758 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800759
760 mlog_exit(status);
761 return status;
762}
763
764static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700765 struct ocfs2_alloc_context *ac,
766 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800767 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800768 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800769 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800770{
771 int status;
772 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700773 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800774 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800775 struct ocfs2_dinode *fe;
776 u32 free_bits;
777
778 mlog_entry_void();
779
Mark Fashehda5cbf22006-10-06 18:34:35 -0700780 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
781 if (!alloc_inode) {
782 mlog_errno(-EINVAL);
783 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800784 }
785
Mark Fashehda5cbf22006-10-06 18:34:35 -0700786 mutex_lock(&alloc_inode->i_mutex);
787
Mark Fashehe63aecb62007-10-18 15:30:42 -0700788 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700789 if (status < 0) {
790 mutex_unlock(&alloc_inode->i_mutex);
791 iput(alloc_inode);
792
793 mlog_errno(status);
794 return status;
795 }
796
797 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800798 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700799
Mark Fashehccd979b2005-12-15 14:31:24 -0800800 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800801
802 /* The bh was validated by the inode read inside
803 * ocfs2_inode_lock(). Any corruption is a code bug. */
804 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
805
Mark Fashehccd979b2005-12-15 14:31:24 -0800806 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800807 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
808 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 status = -EIO;
810 goto bail;
811 }
812
813 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
814 le32_to_cpu(fe->id1.bitmap1.i_used);
815
816 if (bits_wanted > free_bits) {
817 /* cluster bitmap never grows */
818 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
819 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
820 bits_wanted, free_bits);
821 status = -ENOSPC;
822 goto bail;
823 }
824
Tao Ma60ca81e2009-02-25 00:53:24 +0800825 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800826 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
827 "and we don't alloc a new group for it.\n",
828 slot, bits_wanted, free_bits);
829 status = -ENOSPC;
830 goto bail;
831 }
832
Joel Becker1187c962008-09-03 20:03:39 -0700833 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800834 ac->ac_max_block,
835 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 if (status < 0) {
837 if (status != -ENOSPC)
838 mlog_errno(status);
839 goto bail;
840 }
841 atomic_inc(&osb->alloc_stats.bg_extends);
842
843 /* You should never ask for this much metadata */
844 BUG_ON(bits_wanted >
845 (le32_to_cpu(fe->id1.bitmap1.i_total)
846 - le32_to_cpu(fe->id1.bitmap1.i_used)));
847 }
848
849 get_bh(bh);
850 ac->ac_bh = bh;
851bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700852 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800853
854 mlog_exit(status);
855 return status;
856}
857
Tiger Yangb89c5422010-01-25 14:11:06 +0800858static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
859{
860 spin_lock(&osb->osb_lock);
861 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
862 spin_unlock(&osb->osb_lock);
863 atomic_set(&osb->s_num_inodes_stolen, 0);
864}
865
866static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
867{
868 spin_lock(&osb->osb_lock);
869 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
870 spin_unlock(&osb->osb_lock);
871 atomic_set(&osb->s_num_meta_stolen, 0);
872}
873
874void ocfs2_init_steal_slots(struct ocfs2_super *osb)
875{
876 ocfs2_init_inode_steal_slot(osb);
877 ocfs2_init_meta_steal_slot(osb);
878}
879
880static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
881{
882 spin_lock(&osb->osb_lock);
883 if (type == INODE_ALLOC_SYSTEM_INODE)
884 osb->s_inode_steal_slot = slot;
885 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
886 osb->s_meta_steal_slot = slot;
887 spin_unlock(&osb->osb_lock);
888}
889
890static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
891{
892 int slot = OCFS2_INVALID_SLOT;
893
894 spin_lock(&osb->osb_lock);
895 if (type == INODE_ALLOC_SYSTEM_INODE)
896 slot = osb->s_inode_steal_slot;
897 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
898 slot = osb->s_meta_steal_slot;
899 spin_unlock(&osb->osb_lock);
900
901 return slot;
902}
903
904static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
905{
906 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
907}
908
909static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
910{
911 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
912}
913
914static int ocfs2_steal_resource(struct ocfs2_super *osb,
915 struct ocfs2_alloc_context *ac,
916 int type)
917{
918 int i, status = -ENOSPC;
919 int slot = __ocfs2_get_steal_slot(osb, type);
920
921 /* Start to steal resource from the first slot after ours. */
922 if (slot == OCFS2_INVALID_SLOT)
923 slot = osb->slot_num + 1;
924
925 for (i = 0; i < osb->max_slots; i++, slot++) {
926 if (slot == osb->max_slots)
927 slot = 0;
928
929 if (slot == osb->slot_num)
930 continue;
931
932 status = ocfs2_reserve_suballoc_bits(osb, ac,
933 type,
934 (u32)slot, NULL,
935 NOT_ALLOC_NEW_GROUP);
936 if (status >= 0) {
937 __ocfs2_set_steal_slot(osb, slot, type);
938 break;
939 }
940
941 ocfs2_free_ac_resource(ac);
942 }
943
944 return status;
945}
946
947static int ocfs2_steal_inode(struct ocfs2_super *osb,
948 struct ocfs2_alloc_context *ac)
949{
950 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
951}
952
953static int ocfs2_steal_meta(struct ocfs2_super *osb,
954 struct ocfs2_alloc_context *ac)
955{
956 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
957}
958
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800959int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
960 int blocks,
961 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800962{
963 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800964 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800965
Robert P. J. Daycd861282006-12-13 00:34:52 -0800966 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 if (!(*ac)) {
968 status = -ENOMEM;
969 mlog_errno(status);
970 goto bail;
971 }
972
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800973 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800975 (*ac)->ac_group_search = ocfs2_block_group_search;
976
Tiger Yangb89c5422010-01-25 14:11:06 +0800977 if (slot != OCFS2_INVALID_SLOT &&
978 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
979 goto extent_steal;
980
981 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700982 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800983 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800984 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -0800985 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +0800986
987
988 if (status >= 0) {
989 status = 0;
990 if (slot != OCFS2_INVALID_SLOT)
991 ocfs2_init_meta_steal_slot(osb);
992 goto bail;
993 } else if (status < 0 && status != -ENOSPC) {
994 mlog_errno(status);
995 goto bail;
996 }
997
998 ocfs2_free_ac_resource(*ac);
999
1000extent_steal:
1001 status = ocfs2_steal_meta(osb, *ac);
1002 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001003 if (status < 0) {
1004 if (status != -ENOSPC)
1005 mlog_errno(status);
1006 goto bail;
1007 }
1008
1009 status = 0;
1010bail:
1011 if ((status < 0) && *ac) {
1012 ocfs2_free_alloc_context(*ac);
1013 *ac = NULL;
1014 }
1015
Mark Fashehccd979b2005-12-15 14:31:24 -08001016 mlog_exit(status);
1017 return status;
1018}
1019
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001020int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1021 struct ocfs2_extent_list *root_el,
1022 struct ocfs2_alloc_context **ac)
1023{
1024 return ocfs2_reserve_new_metadata_blocks(osb,
1025 ocfs2_extend_meta_needed(root_el),
1026 ac);
1027}
1028
Mark Fashehccd979b2005-12-15 14:31:24 -08001029int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 struct ocfs2_alloc_context **ac)
1031{
1032 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001033 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001034 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001035
Robert P. J. Daycd861282006-12-13 00:34:52 -08001036 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 if (!(*ac)) {
1038 status = -ENOMEM;
1039 mlog_errno(status);
1040 goto bail;
1041 }
1042
1043 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001044 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1045
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 (*ac)->ac_group_search = ocfs2_block_group_search;
1047
Tao Ma4d0ddb22008-03-05 16:11:46 +08001048 /*
Joel Becker1187c962008-09-03 20:03:39 -07001049 * stat(2) can't handle i_ino > 32bits, so we tell the
1050 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001051 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001052 */
Joel Becker12462f12008-09-03 20:03:40 -07001053 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1054 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001055
1056 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001057 * slot is set when we successfully steal inode from other nodes.
1058 * It is reset in 3 places:
1059 * 1. when we flush the truncate log
1060 * 2. when we complete local alloc recovery.
1061 * 3. when we successfully allocate from our own slot.
1062 * After it is set, we will go on stealing inodes until we find the
1063 * need to check our slots to see whether there is some space for us.
1064 */
1065 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001066 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001067 goto inode_steal;
1068
1069 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001070 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001071 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1072 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001073 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001074 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001075 ALLOC_NEW_GROUP |
1076 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001077 if (status >= 0) {
1078 status = 0;
1079
Tao Mafeb473a2009-02-25 00:53:25 +08001080 spin_lock(&osb->osb_lock);
1081 osb->osb_inode_alloc_group = alloc_group;
1082 spin_unlock(&osb->osb_lock);
1083 mlog(0, "after reservation, new allocation group is "
1084 "%llu\n", (unsigned long long)alloc_group);
1085
Tao Ma4d0ddb22008-03-05 16:11:46 +08001086 /*
1087 * Some inodes must be freed by us, so try to allocate
1088 * from our own next time.
1089 */
1090 if (slot != OCFS2_INVALID_SLOT)
1091 ocfs2_init_inode_steal_slot(osb);
1092 goto bail;
1093 } else if (status < 0 && status != -ENOSPC) {
1094 mlog_errno(status);
1095 goto bail;
1096 }
1097
1098 ocfs2_free_ac_resource(*ac);
1099
1100inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001101 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001102 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 if (status < 0) {
1104 if (status != -ENOSPC)
1105 mlog_errno(status);
1106 goto bail;
1107 }
1108
1109 status = 0;
1110bail:
1111 if ((status < 0) && *ac) {
1112 ocfs2_free_alloc_context(*ac);
1113 *ac = NULL;
1114 }
1115
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 mlog_exit(status);
1117 return status;
1118}
1119
1120/* local alloc code has to do the same thing, so rather than do this
1121 * twice.. */
1122int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1123 struct ocfs2_alloc_context *ac)
1124{
1125 int status;
1126
Mark Fashehccd979b2005-12-15 14:31:24 -08001127 ac->ac_which = OCFS2_AC_USE_MAIN;
1128 ac->ac_group_search = ocfs2_cluster_group_search;
1129
Mark Fashehda5cbf22006-10-06 18:34:35 -07001130 status = ocfs2_reserve_suballoc_bits(osb, ac,
1131 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001132 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001133 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001134 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001136 goto bail;
1137 }
1138
Mark Fashehccd979b2005-12-15 14:31:24 -08001139bail:
1140 return status;
1141}
1142
1143/* Callers don't need to care which bitmap (local alloc or main) to
1144 * use so we figure it out for them, but unfortunately this clutters
1145 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001146static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1147 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001148 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001149 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001150{
1151 int status;
1152
1153 mlog_entry_void();
1154
Robert P. J. Daycd861282006-12-13 00:34:52 -08001155 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 if (!(*ac)) {
1157 status = -ENOMEM;
1158 mlog_errno(status);
1159 goto bail;
1160 }
1161
1162 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001163 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001164
1165 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001166 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1167 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 bits_wanted,
1170 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001171 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 mlog_errno(status);
1173 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 }
1175 }
1176
1177 if (status == -ENOSPC) {
1178 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1179 if (status < 0) {
1180 if (status != -ENOSPC)
1181 mlog_errno(status);
1182 goto bail;
1183 }
1184 }
1185
1186 status = 0;
1187bail:
1188 if ((status < 0) && *ac) {
1189 ocfs2_free_alloc_context(*ac);
1190 *ac = NULL;
1191 }
1192
1193 mlog_exit(status);
1194 return status;
1195}
1196
Joel Becker1187c962008-09-03 20:03:39 -07001197int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1198 u32 bits_wanted,
1199 struct ocfs2_alloc_context **ac)
1200{
Tao Ma60ca81e2009-02-25 00:53:24 +08001201 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1202 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001203}
1204
Mark Fashehccd979b2005-12-15 14:31:24 -08001205/*
1206 * More or less lifted from ext3. I'll leave their description below:
1207 *
1208 * "For ext3 allocations, we must not reuse any blocks which are
1209 * allocated in the bitmap buffer's "last committed data" copy. This
1210 * prevents deletes from freeing up the page for reuse until we have
1211 * committed the delete transaction.
1212 *
1213 * If we didn't do this, then deleting something and reallocating it as
1214 * data would allow the old block to be overwritten before the
1215 * transaction committed (because we force data to disk before commit).
1216 * This would lead to corruption if we crashed between overwriting the
1217 * data and committing the delete.
1218 *
1219 * @@@ We may want to make this allocation behaviour conditional on
1220 * data-writes at some point, and disable it for metadata allocations or
1221 * sync-data inodes."
1222 *
1223 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001224 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001225 * called on a metadata group descriptor.
1226 */
1227static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1228 int nr)
1229{
1230 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001231 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001232
1233 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1234 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001235
1236 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 return 1;
1238
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001239 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001241 if (bg)
1242 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1243 else
1244 ret = 1;
1245 jbd_unlock_bh_state(bg_bh);
1246
1247 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001248}
1249
1250static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1251 struct buffer_head *bg_bh,
1252 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001253 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001254 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001255{
1256 void *bitmap;
1257 u16 best_offset, best_size;
1258 int offset, start, found, status = 0;
1259 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1260
Joel Becker42035302008-11-13 14:49:15 -08001261 /* Callers got this descriptor from
1262 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1263 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001264
1265 found = start = best_offset = best_size = 0;
1266 bitmap = bg->bg_bitmap;
1267
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001268 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1269 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 break;
1271
1272 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1273 /* We found a zero, but we can't use it as it
1274 * hasn't been put to disk yet! */
1275 found = 0;
1276 start = offset + 1;
1277 } else if (offset == start) {
1278 /* we found a zero */
1279 found++;
1280 /* move start to the next bit to test */
1281 start++;
1282 } else {
1283 /* got a zero after some ones */
1284 found = 1;
1285 start = offset + 1;
1286 }
1287 if (found > best_size) {
1288 best_size = found;
1289 best_offset = start - found;
1290 }
1291 /* we got everything we needed */
1292 if (found == bits_wanted) {
1293 /* mlog(0, "Found it all!\n"); */
1294 break;
1295 }
1296 }
1297
Joel Becker7d1fe092010-04-13 14:30:19 +08001298 if (best_size) {
1299 res->sr_bit_offset = best_offset;
1300 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001301 } else {
1302 status = -ENOSPC;
1303 /* No error log here -- see the comment above
1304 * ocfs2_test_bg_bit_allocatable */
1305 }
1306
1307 return status;
1308}
1309
Mark Fasheh1fabe142006-10-09 18:11:45 -07001310static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 struct inode *alloc_inode,
1312 struct ocfs2_group_desc *bg,
1313 struct buffer_head *group_bh,
1314 unsigned int bit_off,
1315 unsigned int num_bits)
1316{
1317 int status;
1318 void *bitmap = bg->bg_bitmap;
1319 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1320
1321 mlog_entry_void();
1322
Joel Becker42035302008-11-13 14:49:15 -08001323 /* All callers get the descriptor via
1324 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1325 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1327
1328 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1329 num_bits);
1330
1331 if (ocfs2_is_cluster_bitmap(alloc_inode))
1332 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1333
Joel Becker13723d02008-10-17 19:25:01 -07001334 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001335 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001336 group_bh,
1337 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001338 if (status < 0) {
1339 mlog_errno(status);
1340 goto bail;
1341 }
1342
1343 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 while(num_bits--)
1345 ocfs2_set_bit(bit_off++, bitmap);
1346
Joel Beckerec20cec2010-03-19 14:13:52 -07001347 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001348
1349bail:
1350 mlog_exit(status);
1351 return status;
1352}
1353
1354/* find the one with the most empty bits */
1355static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1356{
1357 u16 curr, best;
1358
1359 BUG_ON(!cl->cl_next_free_rec);
1360
1361 best = curr = 0;
1362 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1363 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1364 le32_to_cpu(cl->cl_recs[best].c_free))
1365 best = curr;
1366 curr++;
1367 }
1368
1369 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1370 return best;
1371}
1372
Mark Fasheh1fabe142006-10-09 18:11:45 -07001373static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001374 struct inode *alloc_inode,
1375 struct buffer_head *fe_bh,
1376 struct buffer_head *bg_bh,
1377 struct buffer_head *prev_bg_bh,
1378 u16 chain)
1379{
1380 int status;
1381 /* there is a really tiny chance the journal calls could fail,
1382 * but we wouldn't want inconsistent blocks in *any* case. */
1383 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1384 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1385 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1386 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1387
Joel Becker42035302008-11-13 14:49:15 -08001388 /* The caller got these descriptors from
1389 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1390 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1391 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001392
Mark Fashehb06970532006-03-03 10:24:33 -08001393 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001394 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1395 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1396 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001397
1398 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1399 bg_ptr = le64_to_cpu(bg->bg_next_group);
1400 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1401
Joel Becker0cf2f762009-02-12 16:41:25 -08001402 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1403 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001404 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001405 if (status < 0) {
1406 mlog_errno(status);
1407 goto out_rollback;
1408 }
1409
1410 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001411 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001412
Joel Becker0cf2f762009-02-12 16:41:25 -08001413 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1414 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 if (status < 0) {
1416 mlog_errno(status);
1417 goto out_rollback;
1418 }
1419
1420 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001421 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001422
Joel Becker0cf2f762009-02-12 16:41:25 -08001423 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1424 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001425 if (status < 0) {
1426 mlog_errno(status);
1427 goto out_rollback;
1428 }
1429
1430 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001431 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001432
Mark Fashehccd979b2005-12-15 14:31:24 -08001433out_rollback:
1434 if (status < 0) {
1435 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1436 bg->bg_next_group = cpu_to_le64(bg_ptr);
1437 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1438 }
Joel Becker42035302008-11-13 14:49:15 -08001439
Mark Fashehccd979b2005-12-15 14:31:24 -08001440 mlog_exit(status);
1441 return status;
1442}
1443
1444static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1445 u32 wanted)
1446{
1447 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1448}
1449
1450/* return 0 on success, -ENOSPC to keep searching and any other < 0
1451 * value on error. */
1452static int ocfs2_cluster_group_search(struct inode *inode,
1453 struct buffer_head *group_bh,
1454 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001455 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001456 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001457{
1458 int search = -ENOSPC;
1459 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001460 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001461 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001462 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001463 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001464
1465 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1466
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001467 if (gd->bg_free_bits_count) {
1468 max_bits = le16_to_cpu(gd->bg_bits);
1469
1470 /* Tail groups in cluster bitmaps which aren't cpg
1471 * aligned are prone to partial extention by a failed
1472 * fs resize. If the file system resize never got to
1473 * update the dinode cluster count, then we don't want
1474 * to trust any clusters past it, regardless of what
1475 * the group descriptor says. */
1476 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1477 le64_to_cpu(gd->bg_blkno));
1478 if ((gd_cluster_off + max_bits) >
1479 OCFS2_I(inode)->ip_clusters) {
1480 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1481 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1482 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1483 le16_to_cpu(gd->bg_bits),
1484 OCFS2_I(inode)->ip_clusters, max_bits);
1485 }
1486
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1488 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001489 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 if (ret)
1491 return ret;
1492
Joel Becker1187c962008-09-03 20:03:39 -07001493 if (max_block) {
1494 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1495 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001496 res->sr_bit_offset +
1497 res->sr_bits);
Joel Becker1187c962008-09-03 20:03:39 -07001498 mlog(0, "Checking %llu against %llu\n",
1499 (unsigned long long)blkoff,
1500 (unsigned long long)max_block);
1501 if (blkoff > max_block)
1502 return -ENOSPC;
1503 }
1504
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 /* ocfs2_block_group_find_clear_bits() might
1506 * return success, but we still want to return
1507 * -ENOSPC unless it found the minimum number
1508 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001509 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001510 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001511 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001512 /*
1513 * Don't show bits which we'll be returning
1514 * for allocation to the local alloc bitmap.
1515 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001516 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001517 }
1518 }
1519
1520 return search;
1521}
1522
1523static int ocfs2_block_group_search(struct inode *inode,
1524 struct buffer_head *group_bh,
1525 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001526 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001527 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001528{
1529 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001530 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001531 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1532
1533 BUG_ON(min_bits != 1);
1534 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1535
Joel Becker1187c962008-09-03 20:03:39 -07001536 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001537 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1538 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001539 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001540 res);
Joel Becker1187c962008-09-03 20:03:39 -07001541 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001542 blkoff = le64_to_cpu(bg->bg_blkno) +
1543 res->sr_bit_offset + res->sr_bits;
Joel Becker1187c962008-09-03 20:03:39 -07001544 mlog(0, "Checking %llu against %llu\n",
1545 (unsigned long long)blkoff,
1546 (unsigned long long)max_block);
1547 if (blkoff > max_block)
1548 ret = -ENOSPC;
1549 }
1550 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001551
1552 return ret;
1553}
1554
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001555static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001556 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001557 struct buffer_head *di_bh,
1558 u32 num_bits,
1559 u16 chain)
1560{
1561 int ret;
1562 u32 tmp_used;
1563 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1564 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1565
Joel Becker0cf2f762009-02-12 16:41:25 -08001566 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001567 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001568 if (ret < 0) {
1569 mlog_errno(ret);
1570 goto out;
1571 }
1572
1573 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1574 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1575 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001576 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001577
1578out:
1579 return ret;
1580}
1581
1582static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001583 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001584 u32 bits_wanted,
1585 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001586 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001587 u16 *bits_left)
1588{
1589 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001590 struct buffer_head *group_bh = NULL;
1591 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001592 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001593 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001594
Joel Becker7d1fe092010-04-13 14:30:19 +08001595 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1596 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001597 if (ret < 0) {
1598 mlog_errno(ret);
1599 return ret;
1600 }
1601
1602 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001603 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001604 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001605 if (ret < 0) {
1606 if (ret != -ENOSPC)
1607 mlog_errno(ret);
1608 goto out;
1609 }
1610
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001611 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001612 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001613 le16_to_cpu(gd->bg_chain));
1614 if (ret < 0) {
1615 mlog_errno(ret);
1616 goto out;
1617 }
1618
1619 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001620 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001621 if (ret < 0)
1622 mlog_errno(ret);
1623
1624 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1625
1626out:
1627 brelse(group_bh);
1628
1629 return ret;
1630}
1631
Mark Fashehccd979b2005-12-15 14:31:24 -08001632static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001633 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001634 u32 bits_wanted,
1635 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001636 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001637 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001638{
1639 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001640 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001641 u32 tmp_used;
1642 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001643 struct inode *alloc_inode = ac->ac_inode;
1644 struct buffer_head *group_bh = NULL;
1645 struct buffer_head *prev_group_bh = NULL;
1646 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1647 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1648 struct ocfs2_group_desc *bg;
1649
1650 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001651 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1652 bits_wanted, chain,
1653 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001654
Joel Becker68f64d42008-11-13 14:49:14 -08001655 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1656 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1657 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001658 if (status < 0) {
1659 mlog_errno(status);
1660 goto bail;
1661 }
1662 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001663
1664 status = -ENOSPC;
1665 /* for now, the chain search is a bit simplistic. We just use
1666 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001667 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1668 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001669 ac->ac_max_block,
1670 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001671 if (!bg->bg_next_group)
1672 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001673
1674 brelse(prev_group_bh);
1675 prev_group_bh = NULL;
1676
Mark Fashehccd979b2005-12-15 14:31:24 -08001677 next_group = le64_to_cpu(bg->bg_next_group);
1678 prev_group_bh = group_bh;
1679 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001680 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1681 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001682 if (status < 0) {
1683 mlog_errno(status);
1684 goto bail;
1685 }
1686 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001687 }
1688 if (status < 0) {
1689 if (status != -ENOSPC)
1690 mlog_errno(status);
1691 goto bail;
1692 }
1693
Mark Fashehb06970532006-03-03 10:24:33 -08001694 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001695 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001696
Joel Becker7d1fe092010-04-13 14:30:19 +08001697 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001698
Joel Becker7d1fe092010-04-13 14:30:19 +08001699 BUG_ON(res->sr_bits == 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001700
1701 /*
1702 * Keep track of previous block descriptor read. When
1703 * we find a target, if we have read more than X
1704 * number of descriptors, and the target is reasonably
1705 * empty, relink him to top of his chain.
1706 *
1707 * We've read 0 extra blocks and only send one more to
1708 * the transaction, yet the next guy to search has a
1709 * much easier time.
1710 *
1711 * Do this *after* figuring out how many bits we're taking out
1712 * of our target group.
1713 */
1714 if (ac->ac_allow_chain_relink &&
1715 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001716 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001717 status = ocfs2_relink_block_group(handle, alloc_inode,
1718 ac->ac_bh, group_bh,
1719 prev_group_bh, chain);
1720 if (status < 0) {
1721 mlog_errno(status);
1722 goto bail;
1723 }
1724 }
1725
1726 /* Ok, claim our bits now: set the info on dinode, chainlist
1727 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001728 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001729 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001730 ac->ac_bh,
1731 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001732 if (status < 0) {
1733 mlog_errno(status);
1734 goto bail;
1735 }
1736
1737 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001738 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1739 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001740 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001741
1742 status = ocfs2_block_group_set_bits(handle,
1743 alloc_inode,
1744 bg,
1745 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001746 res->sr_bit_offset,
1747 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001748 if (status < 0) {
1749 mlog_errno(status);
1750 goto bail;
1751 }
1752
Joel Becker7d1fe092010-04-13 14:30:19 +08001753 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001754 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001755
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001756 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001757bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001758 brelse(group_bh);
1759 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001760
1761 mlog_exit(status);
1762 return status;
1763}
1764
1765/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001766static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001767 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001768 u32 bits_wanted,
1769 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001770 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001771{
1772 int status;
1773 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001774 u16 bits_left = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001775 struct ocfs2_chain_list *cl;
1776 struct ocfs2_dinode *fe;
1777
1778 mlog_entry_void();
1779
1780 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1781 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1782 BUG_ON(!ac->ac_bh);
1783
1784 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001785
1786 /* The bh was validated by the inode read during
1787 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1788 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1789
Mark Fashehccd979b2005-12-15 14:31:24 -08001790 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1791 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001792 ocfs2_error(ac->ac_inode->i_sb,
1793 "Chain allocator dinode %llu has %u used "
Mark Fashehb06970532006-03-03 10:24:33 -08001794 "bits but only %u total.",
1795 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 le32_to_cpu(fe->id1.bitmap1.i_used),
1797 le32_to_cpu(fe->id1.bitmap1.i_total));
1798 status = -EIO;
1799 goto bail;
1800 }
1801
Joel Becker7d1fe092010-04-13 14:30:19 +08001802 res->sr_bg_blkno = ac->ac_last_group;
1803 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001804 /* Attempt to short-circuit the usual search mechanism
1805 * by jumping straight to the most recently used
1806 * allocation group. This helps us mantain some
1807 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001808 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001809 min_bits, res, &bits_left);
1810 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001811 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001812 if (status < 0 && status != -ENOSPC) {
1813 mlog_errno(status);
1814 goto bail;
1815 }
1816 }
1817
Mark Fashehccd979b2005-12-15 14:31:24 -08001818 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1819
1820 victim = ocfs2_find_victim_chain(cl);
1821 ac->ac_chain = victim;
1822 ac->ac_allow_chain_relink = 1;
1823
Joel Becker7d1fe092010-04-13 14:30:19 +08001824 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1825 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001826 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001827 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001828 if (status < 0 && status != -ENOSPC) {
1829 mlog_errno(status);
1830 goto bail;
1831 }
1832
1833 mlog(0, "Search of victim chain %u came up with nothing, "
1834 "trying all chains now.\n", victim);
1835
1836 /* If we didn't pick a good victim, then just default to
1837 * searching each chain in order. Don't allow chain relinking
1838 * because we only calculate enough journal credits for one
1839 * relink per alloc. */
1840 ac->ac_allow_chain_relink = 0;
1841 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1842 if (i == victim)
1843 continue;
1844 if (!cl->cl_recs[i].c_free)
1845 continue;
1846
1847 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001848 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001849 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 if (!status)
1851 break;
1852 if (status < 0 && status != -ENOSPC) {
1853 mlog_errno(status);
1854 goto bail;
1855 }
1856 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001857
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001858set_hint:
1859 if (status != -ENOSPC) {
1860 /* If the next search of this group is not likely to
1861 * yield a suitable extent, then we reset the last
1862 * group hint so as to not waste a disk read */
1863 if (bits_left < min_bits)
1864 ac->ac_last_group = 0;
1865 else
Joel Becker7d1fe092010-04-13 14:30:19 +08001866 ac->ac_last_group = res->sr_bg_blkno;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001867 }
1868
1869bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001870 mlog_exit(status);
1871 return status;
1872}
1873
1874int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001875 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001876 struct ocfs2_alloc_context *ac,
1877 u32 bits_wanted,
1878 u16 *suballoc_bit_start,
1879 unsigned int *num_bits,
1880 u64 *blkno_start)
1881{
1882 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001883 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08001884
1885 BUG_ON(!ac);
1886 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1887 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001888
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001889 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001890 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 bits_wanted,
1892 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001893 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001894 if (status < 0) {
1895 mlog_errno(status);
1896 goto bail;
1897 }
1898 atomic_inc(&osb->alloc_stats.bg_allocs);
1899
Joel Becker7d1fe092010-04-13 14:30:19 +08001900 *suballoc_bit_start = res.sr_bit_offset;
1901 *blkno_start = res.sr_bg_blkno + (u64)(res.sr_bit_offset);
1902 ac->ac_bits_given += res.sr_bits;
1903 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 status = 0;
1905bail:
1906 mlog_exit(status);
1907 return status;
1908}
1909
Tao Ma13821152009-02-25 00:53:23 +08001910static void ocfs2_init_inode_ac_group(struct inode *dir,
1911 struct buffer_head *parent_fe_bh,
1912 struct ocfs2_alloc_context *ac)
1913{
1914 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1915 /*
1916 * Try to allocate inodes from some specific group.
1917 *
1918 * If the parent dir has recorded the last group used in allocation,
1919 * cool, use it. Otherwise if we try to allocate new inode from the
1920 * same slot the parent dir belongs to, use the same chunk.
1921 *
1922 * We are very careful here to avoid the mistake of setting
1923 * ac_last_group to a group descriptor from a different (unlocked) slot.
1924 */
1925 if (OCFS2_I(dir)->ip_last_used_group &&
1926 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1927 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1928 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1929 ac->ac_last_group = ocfs2_which_suballoc_group(
1930 le64_to_cpu(fe->i_blkno),
1931 le16_to_cpu(fe->i_suballoc_bit));
1932}
1933
1934static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1935 struct ocfs2_alloc_context *ac)
1936{
1937 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1938 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1939}
1940
Mark Fashehccd979b2005-12-15 14:31:24 -08001941int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001942 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001943 struct inode *dir,
1944 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001945 struct ocfs2_alloc_context *ac,
1946 u16 *suballoc_bit,
1947 u64 *fe_blkno)
1948{
1949 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001950 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08001951
1952 mlog_entry_void();
1953
1954 BUG_ON(!ac);
1955 BUG_ON(ac->ac_bits_given != 0);
1956 BUG_ON(ac->ac_bits_wanted != 1);
1957 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001958
Tao Ma13821152009-02-25 00:53:23 +08001959 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
1960
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001961 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001962 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001963 1,
1964 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001965 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001966 if (status < 0) {
1967 mlog_errno(status);
1968 goto bail;
1969 }
1970 atomic_inc(&osb->alloc_stats.bg_allocs);
1971
Joel Becker7d1fe092010-04-13 14:30:19 +08001972 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001973
Joel Becker7d1fe092010-04-13 14:30:19 +08001974 *suballoc_bit = res.sr_bit_offset;
1975 *fe_blkno = res.sr_bg_blkno + (u64)(res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08001976 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08001977 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08001978 status = 0;
1979bail:
1980 mlog_exit(status);
1981 return status;
1982}
1983
1984/* translate a group desc. blkno and it's bitmap offset into
1985 * disk cluster offset. */
1986static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
1987 u64 bg_blkno,
1988 u16 bg_bit_off)
1989{
1990 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1991 u32 cluster = 0;
1992
1993 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1994
1995 if (bg_blkno != osb->first_cluster_group_blkno)
1996 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
1997 cluster += (u32) bg_bit_off;
1998 return cluster;
1999}
2000
2001/* given a cluster offset, calculate which block group it belongs to
2002 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002003u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002004{
2005 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2006 u32 group_no;
2007
2008 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2009
2010 group_no = cluster / osb->bitmap_cpg;
2011 if (!group_no)
2012 return osb->first_cluster_group_blkno;
2013 return ocfs2_clusters_to_blocks(inode->i_sb,
2014 group_no * osb->bitmap_cpg);
2015}
2016
2017/* given the block number of a cluster start, calculate which cluster
2018 * group and descriptor bitmap offset that corresponds to. */
2019static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2020 u64 data_blkno,
2021 u64 *bg_blkno,
2022 u16 *bg_bit_off)
2023{
2024 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2025 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2026
2027 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2028
2029 *bg_blkno = ocfs2_which_cluster_group(inode,
2030 data_cluster);
2031
2032 if (*bg_blkno == osb->first_cluster_group_blkno)
2033 *bg_bit_off = (u16) data_cluster;
2034 else
2035 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2036 data_blkno - *bg_blkno);
2037}
2038
2039/*
2040 * min_bits - minimum contiguous chunk from this total allocation we
2041 * can handle. set to what we asked for originally for a full
2042 * contig. allocation, set to '1' to indicate we can deal with extents
2043 * of any size.
2044 */
Mark Fasheh415cb802007-09-16 20:10:16 -07002045int __ocfs2_claim_clusters(struct ocfs2_super *osb,
2046 handle_t *handle,
2047 struct ocfs2_alloc_context *ac,
2048 u32 min_clusters,
2049 u32 max_clusters,
2050 u32 *cluster_start,
2051 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002052{
2053 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002054 unsigned int bits_wanted = max_clusters;
Joel Becker7d1fe092010-04-13 14:30:19 +08002055 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002056
2057 mlog_entry_void();
2058
Mark Fashehccd979b2005-12-15 14:31:24 -08002059 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2060
2061 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2062 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002063
2064 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002065 WARN_ON(min_clusters > 1);
2066
Mark Fashehccd979b2005-12-15 14:31:24 -08002067 status = ocfs2_claim_local_alloc_bits(osb,
2068 handle,
2069 ac,
2070 bits_wanted,
2071 cluster_start,
2072 num_clusters);
2073 if (!status)
2074 atomic_inc(&osb->alloc_stats.local_data);
2075 } else {
2076 if (min_clusters > (osb->bitmap_cpg - 1)) {
2077 /* The only paths asking for contiguousness
2078 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002079 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2080 "group bitmap size %u!\n", min_clusters,
2081 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002082 status = -ENOSPC;
2083 goto bail;
2084 }
2085 /* clamp the current request down to a realistic size. */
2086 if (bits_wanted > (osb->bitmap_cpg - 1))
2087 bits_wanted = osb->bitmap_cpg - 1;
2088
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002089 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002090 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002091 bits_wanted,
2092 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002093 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002094 if (!status) {
2095 *cluster_start =
2096 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002097 res.sr_bg_blkno,
2098 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002099 atomic_inc(&osb->alloc_stats.bitmap_data);
2100 }
2101 }
2102 if (status < 0) {
2103 if (status != -ENOSPC)
2104 mlog_errno(status);
2105 goto bail;
2106 }
2107
Joel Becker7d1fe092010-04-13 14:30:19 +08002108 ac->ac_bits_given += res.sr_bits;
2109 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002110
2111bail:
2112 mlog_exit(status);
2113 return status;
2114}
2115
Mark Fasheh415cb802007-09-16 20:10:16 -07002116int ocfs2_claim_clusters(struct ocfs2_super *osb,
2117 handle_t *handle,
2118 struct ocfs2_alloc_context *ac,
2119 u32 min_clusters,
2120 u32 *cluster_start,
2121 u32 *num_clusters)
2122{
2123 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2124
2125 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
2126 bits_wanted, cluster_start, num_clusters);
2127}
2128
Mark Fashehb4414ee2010-03-11 18:31:09 -08002129static int ocfs2_block_group_clear_bits(handle_t *handle,
2130 struct inode *alloc_inode,
2131 struct ocfs2_group_desc *bg,
2132 struct buffer_head *group_bh,
2133 unsigned int bit_off,
2134 unsigned int num_bits,
2135 void (*undo_fn)(unsigned int bit,
2136 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002137{
2138 int status;
2139 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002140 struct ocfs2_group_desc *undo_bg = NULL;
2141
2142 mlog_entry_void();
2143
Joel Becker42035302008-11-13 14:49:15 -08002144 /* The caller got this descriptor from
2145 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2146 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002147
2148 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2149
Mark Fashehb4414ee2010-03-11 18:31:09 -08002150 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002151 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002152 group_bh,
2153 undo_fn ?
2154 OCFS2_JOURNAL_ACCESS_UNDO :
2155 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 if (status < 0) {
2157 mlog_errno(status);
2158 goto bail;
2159 }
2160
Mark Fashehb4414ee2010-03-11 18:31:09 -08002161 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002162 jbd_lock_bh_state(group_bh);
2163 undo_bg = (struct ocfs2_group_desc *)
2164 bh2jh(group_bh)->b_committed_data;
2165 BUG_ON(!undo_bg);
2166 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002167
2168 tmp = num_bits;
2169 while(tmp--) {
2170 ocfs2_clear_bit((bit_off + tmp),
2171 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002172 if (undo_fn)
2173 undo_fn(bit_off + tmp,
2174 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002175 }
2176 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2177
Mark Fashehb4414ee2010-03-11 18:31:09 -08002178 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002179 jbd_unlock_bh_state(group_bh);
2180
Joel Beckerec20cec2010-03-19 14:13:52 -07002181 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002182bail:
2183 return status;
2184}
2185
2186/*
2187 * expects the suballoc inode to already be locked.
2188 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002189static int _ocfs2_free_suballoc_bits(handle_t *handle,
2190 struct inode *alloc_inode,
2191 struct buffer_head *alloc_bh,
2192 unsigned int start_bit,
2193 u64 bg_blkno,
2194 unsigned int count,
2195 void (*undo_fn)(unsigned int bit,
2196 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002197{
2198 int status = 0;
2199 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002200 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2201 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2202 struct buffer_head *group_bh = NULL;
2203 struct ocfs2_group_desc *group;
2204
2205 mlog_entry_void();
2206
Joel Becker10995aa2008-11-13 14:49:12 -08002207 /* The alloc_bh comes from ocfs2_free_dinode() or
2208 * ocfs2_free_clusters(). The callers have all locked the
2209 * allocator and gotten alloc_bh from the lock call. This
2210 * validates the dinode buffer. Any corruption that has happended
2211 * is a code bug. */
2212 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002213 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2214
Mark Fashehb06970532006-03-03 10:24:33 -08002215 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2216 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2217 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002218
Joel Becker68f64d42008-11-13 14:49:14 -08002219 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2220 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002221 if (status < 0) {
2222 mlog_errno(status);
2223 goto bail;
2224 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002225 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002226
Mark Fashehccd979b2005-12-15 14:31:24 -08002227 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2228
2229 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2230 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002231 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002232 if (status < 0) {
2233 mlog_errno(status);
2234 goto bail;
2235 }
2236
Joel Becker0cf2f762009-02-12 16:41:25 -08002237 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2238 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002239 if (status < 0) {
2240 mlog_errno(status);
2241 goto bail;
2242 }
2243
2244 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2245 count);
2246 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2247 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002248 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002249
2250bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002251 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002252
2253 mlog_exit(status);
2254 return status;
2255}
2256
Mark Fashehb4414ee2010-03-11 18:31:09 -08002257int ocfs2_free_suballoc_bits(handle_t *handle,
2258 struct inode *alloc_inode,
2259 struct buffer_head *alloc_bh,
2260 unsigned int start_bit,
2261 u64 bg_blkno,
2262 unsigned int count)
2263{
2264 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2265 start_bit, bg_blkno, count, NULL);
2266}
2267
Mark Fasheh1fabe142006-10-09 18:11:45 -07002268int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002269 struct inode *inode_alloc_inode,
2270 struct buffer_head *inode_alloc_bh,
2271 struct ocfs2_dinode *di)
2272{
2273 u64 blk = le64_to_cpu(di->i_blkno);
2274 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2275 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2276
2277 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2278 inode_alloc_bh, bit, bg_blkno, 1);
2279}
2280
Mark Fashehb4414ee2010-03-11 18:31:09 -08002281static int _ocfs2_free_clusters(handle_t *handle,
2282 struct inode *bitmap_inode,
2283 struct buffer_head *bitmap_bh,
2284 u64 start_blk,
2285 unsigned int num_clusters,
2286 void (*undo_fn)(unsigned int bit,
2287 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002288{
2289 int status;
2290 u16 bg_start_bit;
2291 u64 bg_blkno;
2292 struct ocfs2_dinode *fe;
2293
2294 /* You can't ever have a contiguous set of clusters
2295 * bigger than a block group bitmap so we never have to worry
2296 * about looping on them. */
2297
2298 mlog_entry_void();
2299
2300 /* This is expensive. We can safely remove once this stuff has
2301 * gotten tested really well. */
2302 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2303
2304 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2305
2306 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2307 &bg_start_bit);
2308
Mark Fashehb06970532006-03-03 10:24:33 -08002309 mlog(0, "want to free %u clusters starting at block %llu\n",
2310 num_clusters, (unsigned long long)start_blk);
2311 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2312 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002313
Mark Fashehb4414ee2010-03-11 18:31:09 -08002314 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2315 bg_start_bit, bg_blkno,
2316 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002317 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002318 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002319 goto out;
2320 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002321
Mark Fasheh9c7af402008-07-28 18:02:53 -07002322 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2323 num_clusters);
2324
2325out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002326 mlog_exit(status);
2327 return status;
2328}
2329
Mark Fashehb4414ee2010-03-11 18:31:09 -08002330int ocfs2_free_clusters(handle_t *handle,
2331 struct inode *bitmap_inode,
2332 struct buffer_head *bitmap_bh,
2333 u64 start_blk,
2334 unsigned int num_clusters)
2335{
2336 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2337 start_blk, num_clusters,
2338 _ocfs2_set_bit);
2339}
2340
2341/*
2342 * Give never-used clusters back to the global bitmap. We don't need
2343 * to protect these bits in the undo buffer.
2344 */
2345int ocfs2_release_clusters(handle_t *handle,
2346 struct inode *bitmap_inode,
2347 struct buffer_head *bitmap_bh,
2348 u64 start_blk,
2349 unsigned int num_clusters)
2350{
2351 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2352 start_blk, num_clusters,
2353 _ocfs2_clear_bit);
2354}
2355
Mark Fashehccd979b2005-12-15 14:31:24 -08002356static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2357{
2358 printk("Block Group:\n");
2359 printk("bg_signature: %s\n", bg->bg_signature);
2360 printk("bg_size: %u\n", bg->bg_size);
2361 printk("bg_bits: %u\n", bg->bg_bits);
2362 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2363 printk("bg_chain: %u\n", bg->bg_chain);
2364 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002365 printk("bg_next_group: %llu\n",
2366 (unsigned long long)bg->bg_next_group);
2367 printk("bg_parent_dinode: %llu\n",
2368 (unsigned long long)bg->bg_parent_dinode);
2369 printk("bg_blkno: %llu\n",
2370 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002371}
2372
2373static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2374{
2375 int i;
2376
Mark Fashehb06970532006-03-03 10:24:33 -08002377 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002378 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002379 printk("i_size: %llu\n",
2380 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002381 printk("i_clusters: %u\n", fe->i_clusters);
2382 printk("i_generation: %u\n",
2383 le32_to_cpu(fe->i_generation));
2384 printk("id1.bitmap1.i_used: %u\n",
2385 le32_to_cpu(fe->id1.bitmap1.i_used));
2386 printk("id1.bitmap1.i_total: %u\n",
2387 le32_to_cpu(fe->id1.bitmap1.i_total));
2388 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2389 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2390 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2391 printk("id2.i_chain.cl_next_free_rec: %u\n",
2392 fe->id2.i_chain.cl_next_free_rec);
2393 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2394 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2395 fe->id2.i_chain.cl_recs[i].c_free);
2396 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2397 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002398 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2399 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002400 }
2401}
Tao Mae7d4cb62008-08-18 17:38:44 +08002402
2403/*
2404 * For a given allocation, determine which allocators will need to be
2405 * accessed, and lock them, reserving the appropriate number of bits.
2406 *
2407 * Sparse file systems call this from ocfs2_write_begin_nolock()
2408 * and ocfs2_allocate_unwritten_extents().
2409 *
2410 * File systems which don't support holes call this from
2411 * ocfs2_extend_allocation().
2412 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002413int ocfs2_lock_allocators(struct inode *inode,
2414 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002415 u32 clusters_to_add, u32 extents_to_split,
2416 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002417 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002418{
2419 int ret = 0, num_free_extents;
2420 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2421 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2422
2423 *meta_ac = NULL;
2424 if (data_ac)
2425 *data_ac = NULL;
2426
2427 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2428
Joel Becker3d03a302009-02-12 17:49:26 -08002429 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002430 if (num_free_extents < 0) {
2431 ret = num_free_extents;
2432 mlog_errno(ret);
2433 goto out;
2434 }
2435
2436 /*
2437 * Sparse allocation file systems need to be more conservative
2438 * with reserving room for expansion - the actual allocation
2439 * happens while we've got a journal handle open so re-taking
2440 * a cluster lock (because we ran out of room for another
2441 * extent) will violate ordering rules.
2442 *
2443 * Most of the time we'll only be seeing this 1 cluster at a time
2444 * anyway.
2445 *
2446 * Always lock for any unwritten extents - we might want to
2447 * add blocks during a split.
2448 */
2449 if (!num_free_extents ||
2450 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002451 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002452 if (ret < 0) {
2453 if (ret != -ENOSPC)
2454 mlog_errno(ret);
2455 goto out;
2456 }
2457 }
2458
2459 if (clusters_to_add == 0)
2460 goto out;
2461
2462 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2463 if (ret < 0) {
2464 if (ret != -ENOSPC)
2465 mlog_errno(ret);
2466 goto out;
2467 }
2468
2469out:
2470 if (ret) {
2471 if (*meta_ac) {
2472 ocfs2_free_alloc_context(*meta_ac);
2473 *meta_ac = NULL;
2474 }
2475
2476 /*
2477 * We cannot have an error and a non null *data_ac.
2478 */
2479 }
2480
2481 return ret;
2482}
wengang wang6ca497a2009-03-06 21:29:10 +08002483
2484/*
2485 * Read the inode specified by blkno to get suballoc_slot and
2486 * suballoc_bit.
2487 */
2488static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2489 u16 *suballoc_slot, u16 *suballoc_bit)
2490{
2491 int status;
2492 struct buffer_head *inode_bh = NULL;
2493 struct ocfs2_dinode *inode_fe;
2494
Joel Becker5b09b502009-04-21 16:31:20 -07002495 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002496
2497 /* dirty read disk */
2498 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2499 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002500 mlog(ML_ERROR, "read block %llu failed %d\n",
2501 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002502 goto bail;
2503 }
2504
2505 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2506 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002507 mlog(ML_ERROR, "invalid inode %llu requested\n",
2508 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002509 status = -EINVAL;
2510 goto bail;
2511 }
2512
Tao Ma0fba8132009-03-19 05:08:43 +08002513 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002514 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2515 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002516 (unsigned long long)blkno,
2517 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002518 status = -EINVAL;
2519 goto bail;
2520 }
2521
2522 if (suballoc_slot)
2523 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2524 if (suballoc_bit)
2525 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2526
2527bail:
2528 brelse(inode_bh);
2529
2530 mlog_exit(status);
2531 return status;
2532}
2533
2534/*
2535 * test whether bit is SET in allocator bitmap or not. on success, 0
2536 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2537 * is returned and *res is meaningless. Call this after you have
2538 * cluster locked against suballoc, or you may get a result based on
2539 * non-up2date contents
2540 */
2541static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2542 struct inode *suballoc,
2543 struct buffer_head *alloc_bh, u64 blkno,
2544 u16 bit, int *res)
2545{
2546 struct ocfs2_dinode *alloc_fe;
2547 struct ocfs2_group_desc *group;
2548 struct buffer_head *group_bh = NULL;
2549 u64 bg_blkno;
2550 int status;
2551
Joel Becker5b09b502009-04-21 16:31:20 -07002552 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2553 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002554
2555 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2556 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2557 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2558 (unsigned int)bit,
2559 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2560 status = -EINVAL;
2561 goto bail;
2562 }
2563
2564 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2565 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2566 &group_bh);
2567 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002568 mlog(ML_ERROR, "read group %llu failed %d\n",
2569 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002570 goto bail;
2571 }
2572
2573 group = (struct ocfs2_group_desc *) group_bh->b_data;
2574 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2575
2576bail:
2577 brelse(group_bh);
2578
2579 mlog_exit(status);
2580 return status;
2581}
2582
2583/*
2584 * Test if the bit representing this inode (blkno) is set in the
2585 * suballocator.
2586 *
2587 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2588 *
2589 * In the event of failure, a negative value is returned and *res is
2590 * meaningless.
2591 *
2592 * Callers must make sure to hold nfs_sync_lock to prevent
2593 * ocfs2_delete_inode() on another node from accessing the same
2594 * suballocator concurrently.
2595 */
2596int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2597{
2598 int status;
2599 u16 suballoc_bit = 0, suballoc_slot = 0;
2600 struct inode *inode_alloc_inode;
2601 struct buffer_head *alloc_bh = NULL;
2602
Joel Becker5b09b502009-04-21 16:31:20 -07002603 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002604
2605 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2606 &suballoc_bit);
2607 if (status < 0) {
2608 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2609 goto bail;
2610 }
2611
2612 inode_alloc_inode =
2613 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2614 suballoc_slot);
2615 if (!inode_alloc_inode) {
2616 /* the error code could be inaccurate, but we are not able to
2617 * get the correct one. */
2618 status = -EINVAL;
2619 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2620 (u32)suballoc_slot);
2621 goto bail;
2622 }
2623
2624 mutex_lock(&inode_alloc_inode->i_mutex);
2625 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2626 if (status < 0) {
2627 mutex_unlock(&inode_alloc_inode->i_mutex);
2628 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2629 (u32)suballoc_slot, status);
2630 goto bail;
2631 }
2632
2633 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2634 blkno, suballoc_bit, res);
2635 if (status < 0)
2636 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2637
2638 ocfs2_inode_unlock(inode_alloc_inode, 0);
2639 mutex_unlock(&inode_alloc_inode->i_mutex);
2640
2641 iput(inode_alloc_inode);
2642 brelse(alloc_bh);
2643bail:
2644 mlog_exit(status);
2645 return status;
2646}