blob: c9661c4778692f6c69391dfe628b0dd653739da0 [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 Fashehb0697052006-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 Fashehb0697052006-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 Fashehb0697052006-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
Joel Becker13e434c2010-03-26 10:08:27 +08001582static int ocfs2_bg_discontig_trim_by_rec(struct ocfs2_suballoc_result *res,
1583 struct ocfs2_extent_rec *rec,
1584 struct ocfs2_chain_list *cl)
1585{
1586 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1587 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1588 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1589
1590 if (res->sr_bit_offset < bitoff)
1591 return 0;
1592 if (res->sr_bit_offset >= (bitoff + bitcount))
1593 return 0;
1594 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1595 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1596 return 1;
1597}
1598
1599static void ocfs2_bg_discontig_trim_result(struct ocfs2_alloc_context *ac,
1600 struct ocfs2_group_desc *bg,
1601 struct ocfs2_suballoc_result *res)
1602{
1603 int i;
1604 struct ocfs2_extent_rec *rec;
1605 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1606 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1607
1608 if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb)))
1609 return;
1610
1611 if (ocfs2_is_cluster_bitmap(ac->ac_inode))
1612 return;
1613
1614 if (!bg->bg_list.l_next_free_rec)
1615 return;
1616
1617 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1618 rec = &bg->bg_list.l_recs[i];
1619 if (ocfs2_bg_discontig_trim_by_rec(res, rec, cl))
1620 break;
1621 }
1622}
1623
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001624static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001625 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001626 u32 bits_wanted,
1627 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001628 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001629 u16 *bits_left)
1630{
1631 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001632 struct buffer_head *group_bh = NULL;
1633 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001634 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001635 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001636
Joel Becker7d1fe092010-04-13 14:30:19 +08001637 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1638 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001639 if (ret < 0) {
1640 mlog_errno(ret);
1641 return ret;
1642 }
1643
1644 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001645 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001646 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001647 if (ret < 0) {
1648 if (ret != -ENOSPC)
1649 mlog_errno(ret);
1650 goto out;
1651 }
1652
Joel Becker13e434c2010-03-26 10:08:27 +08001653 if (!ret)
1654 ocfs2_bg_discontig_trim_result(ac, gd, res);
1655
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001656 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001657 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001658 le16_to_cpu(gd->bg_chain));
1659 if (ret < 0) {
1660 mlog_errno(ret);
1661 goto out;
1662 }
1663
1664 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001665 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001666 if (ret < 0)
1667 mlog_errno(ret);
1668
1669 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1670
1671out:
1672 brelse(group_bh);
1673
1674 return ret;
1675}
1676
Mark Fashehccd979b2005-12-15 14:31:24 -08001677static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001678 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001679 u32 bits_wanted,
1680 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001681 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001682 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001683{
1684 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001685 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001686 u32 tmp_used;
1687 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001688 struct inode *alloc_inode = ac->ac_inode;
1689 struct buffer_head *group_bh = NULL;
1690 struct buffer_head *prev_group_bh = NULL;
1691 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1692 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1693 struct ocfs2_group_desc *bg;
1694
1695 chain = ac->ac_chain;
Mark Fashehb0697052006-03-03 10:24:33 -08001696 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1697 bits_wanted, chain,
1698 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001699
Joel Becker68f64d42008-11-13 14:49:14 -08001700 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1701 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1702 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001703 if (status < 0) {
1704 mlog_errno(status);
1705 goto bail;
1706 }
1707 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001708
1709 status = -ENOSPC;
1710 /* for now, the chain search is a bit simplistic. We just use
1711 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001712 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1713 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001714 ac->ac_max_block,
1715 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001716 if (!bg->bg_next_group)
1717 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001718
1719 brelse(prev_group_bh);
1720 prev_group_bh = NULL;
1721
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 next_group = le64_to_cpu(bg->bg_next_group);
1723 prev_group_bh = group_bh;
1724 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001725 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1726 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001727 if (status < 0) {
1728 mlog_errno(status);
1729 goto bail;
1730 }
1731 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001732 }
1733 if (status < 0) {
1734 if (status != -ENOSPC)
1735 mlog_errno(status);
1736 goto bail;
1737 }
1738
Mark Fashehb0697052006-03-03 10:24:33 -08001739 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001740 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001741
Joel Becker7d1fe092010-04-13 14:30:19 +08001742 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001743
Joel Becker7d1fe092010-04-13 14:30:19 +08001744 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001745 if (!status)
1746 ocfs2_bg_discontig_trim_result(ac, bg, res);
1747
Mark Fashehccd979b2005-12-15 14:31:24 -08001748
1749 /*
1750 * Keep track of previous block descriptor read. When
1751 * we find a target, if we have read more than X
1752 * number of descriptors, and the target is reasonably
1753 * empty, relink him to top of his chain.
1754 *
1755 * We've read 0 extra blocks and only send one more to
1756 * the transaction, yet the next guy to search has a
1757 * much easier time.
1758 *
1759 * Do this *after* figuring out how many bits we're taking out
1760 * of our target group.
1761 */
1762 if (ac->ac_allow_chain_relink &&
1763 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001764 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001765 status = ocfs2_relink_block_group(handle, alloc_inode,
1766 ac->ac_bh, group_bh,
1767 prev_group_bh, chain);
1768 if (status < 0) {
1769 mlog_errno(status);
1770 goto bail;
1771 }
1772 }
1773
1774 /* Ok, claim our bits now: set the info on dinode, chainlist
1775 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001776 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001777 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001778 ac->ac_bh,
1779 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001780 if (status < 0) {
1781 mlog_errno(status);
1782 goto bail;
1783 }
1784
1785 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001786 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1787 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001788 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001789
1790 status = ocfs2_block_group_set_bits(handle,
1791 alloc_inode,
1792 bg,
1793 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001794 res->sr_bit_offset,
1795 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001796 if (status < 0) {
1797 mlog_errno(status);
1798 goto bail;
1799 }
1800
Joel Becker7d1fe092010-04-13 14:30:19 +08001801 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001802 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001803
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001804 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001805bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001806 brelse(group_bh);
1807 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001808
1809 mlog_exit(status);
1810 return status;
1811}
1812
1813/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001814static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001815 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001816 u32 bits_wanted,
1817 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001818 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001819{
1820 int status;
1821 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001822 u16 bits_left = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001823 struct ocfs2_chain_list *cl;
1824 struct ocfs2_dinode *fe;
1825
1826 mlog_entry_void();
1827
1828 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1829 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1830 BUG_ON(!ac->ac_bh);
1831
1832 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001833
1834 /* The bh was validated by the inode read during
1835 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1836 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1837
Mark Fashehccd979b2005-12-15 14:31:24 -08001838 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1839 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001840 ocfs2_error(ac->ac_inode->i_sb,
1841 "Chain allocator dinode %llu has %u used "
Mark Fashehb0697052006-03-03 10:24:33 -08001842 "bits but only %u total.",
1843 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001844 le32_to_cpu(fe->id1.bitmap1.i_used),
1845 le32_to_cpu(fe->id1.bitmap1.i_total));
1846 status = -EIO;
1847 goto bail;
1848 }
1849
Joel Becker7d1fe092010-04-13 14:30:19 +08001850 res->sr_bg_blkno = ac->ac_last_group;
1851 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001852 /* Attempt to short-circuit the usual search mechanism
1853 * by jumping straight to the most recently used
1854 * allocation group. This helps us mantain some
1855 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001856 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001857 min_bits, res, &bits_left);
1858 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001859 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001860 if (status < 0 && status != -ENOSPC) {
1861 mlog_errno(status);
1862 goto bail;
1863 }
1864 }
1865
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1867
1868 victim = ocfs2_find_victim_chain(cl);
1869 ac->ac_chain = victim;
1870 ac->ac_allow_chain_relink = 1;
1871
Joel Becker7d1fe092010-04-13 14:30:19 +08001872 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1873 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001874 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001875 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001876 if (status < 0 && status != -ENOSPC) {
1877 mlog_errno(status);
1878 goto bail;
1879 }
1880
1881 mlog(0, "Search of victim chain %u came up with nothing, "
1882 "trying all chains now.\n", victim);
1883
1884 /* If we didn't pick a good victim, then just default to
1885 * searching each chain in order. Don't allow chain relinking
1886 * because we only calculate enough journal credits for one
1887 * relink per alloc. */
1888 ac->ac_allow_chain_relink = 0;
1889 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1890 if (i == victim)
1891 continue;
1892 if (!cl->cl_recs[i].c_free)
1893 continue;
1894
1895 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001896 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001897 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001898 if (!status)
1899 break;
1900 if (status < 0 && status != -ENOSPC) {
1901 mlog_errno(status);
1902 goto bail;
1903 }
1904 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001905
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001906set_hint:
1907 if (status != -ENOSPC) {
1908 /* If the next search of this group is not likely to
1909 * yield a suitable extent, then we reset the last
1910 * group hint so as to not waste a disk read */
1911 if (bits_left < min_bits)
1912 ac->ac_last_group = 0;
1913 else
Joel Becker7d1fe092010-04-13 14:30:19 +08001914 ac->ac_last_group = res->sr_bg_blkno;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001915 }
1916
1917bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001918 mlog_exit(status);
1919 return status;
1920}
1921
1922int ocfs2_claim_metadata(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001923 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 struct ocfs2_alloc_context *ac,
1925 u32 bits_wanted,
1926 u16 *suballoc_bit_start,
1927 unsigned int *num_bits,
1928 u64 *blkno_start)
1929{
1930 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001931 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08001932
1933 BUG_ON(!ac);
1934 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1935 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001936
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001937 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001938 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001939 bits_wanted,
1940 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001941 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001942 if (status < 0) {
1943 mlog_errno(status);
1944 goto bail;
1945 }
1946 atomic_inc(&osb->alloc_stats.bg_allocs);
1947
Joel Becker7d1fe092010-04-13 14:30:19 +08001948 *suballoc_bit_start = res.sr_bit_offset;
1949 *blkno_start = res.sr_bg_blkno + (u64)(res.sr_bit_offset);
1950 ac->ac_bits_given += res.sr_bits;
1951 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001952 status = 0;
1953bail:
1954 mlog_exit(status);
1955 return status;
1956}
1957
Tao Ma13821152009-02-25 00:53:23 +08001958static void ocfs2_init_inode_ac_group(struct inode *dir,
1959 struct buffer_head *parent_fe_bh,
1960 struct ocfs2_alloc_context *ac)
1961{
1962 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1963 /*
1964 * Try to allocate inodes from some specific group.
1965 *
1966 * If the parent dir has recorded the last group used in allocation,
1967 * cool, use it. Otherwise if we try to allocate new inode from the
1968 * same slot the parent dir belongs to, use the same chunk.
1969 *
1970 * We are very careful here to avoid the mistake of setting
1971 * ac_last_group to a group descriptor from a different (unlocked) slot.
1972 */
1973 if (OCFS2_I(dir)->ip_last_used_group &&
1974 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1975 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1976 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1977 ac->ac_last_group = ocfs2_which_suballoc_group(
1978 le64_to_cpu(fe->i_blkno),
1979 le16_to_cpu(fe->i_suballoc_bit));
1980}
1981
1982static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1983 struct ocfs2_alloc_context *ac)
1984{
1985 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1986 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1987}
1988
Mark Fashehccd979b2005-12-15 14:31:24 -08001989int ocfs2_claim_new_inode(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001990 handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001991 struct inode *dir,
1992 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001993 struct ocfs2_alloc_context *ac,
1994 u16 *suballoc_bit,
1995 u64 *fe_blkno)
1996{
1997 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001998 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08001999
2000 mlog_entry_void();
2001
2002 BUG_ON(!ac);
2003 BUG_ON(ac->ac_bits_given != 0);
2004 BUG_ON(ac->ac_bits_wanted != 1);
2005 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002006
Tao Ma13821152009-02-25 00:53:23 +08002007 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2008
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002009 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002010 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002011 1,
2012 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002013 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002014 if (status < 0) {
2015 mlog_errno(status);
2016 goto bail;
2017 }
2018 atomic_inc(&osb->alloc_stats.bg_allocs);
2019
Joel Becker7d1fe092010-04-13 14:30:19 +08002020 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002021
Joel Becker7d1fe092010-04-13 14:30:19 +08002022 *suballoc_bit = res.sr_bit_offset;
2023 *fe_blkno = res.sr_bg_blkno + (u64)(res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002024 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002025 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002026 status = 0;
2027bail:
2028 mlog_exit(status);
2029 return status;
2030}
2031
2032/* translate a group desc. blkno and it's bitmap offset into
2033 * disk cluster offset. */
2034static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2035 u64 bg_blkno,
2036 u16 bg_bit_off)
2037{
2038 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2039 u32 cluster = 0;
2040
2041 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2042
2043 if (bg_blkno != osb->first_cluster_group_blkno)
2044 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2045 cluster += (u32) bg_bit_off;
2046 return cluster;
2047}
2048
2049/* given a cluster offset, calculate which block group it belongs to
2050 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002051u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002052{
2053 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2054 u32 group_no;
2055
2056 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2057
2058 group_no = cluster / osb->bitmap_cpg;
2059 if (!group_no)
2060 return osb->first_cluster_group_blkno;
2061 return ocfs2_clusters_to_blocks(inode->i_sb,
2062 group_no * osb->bitmap_cpg);
2063}
2064
2065/* given the block number of a cluster start, calculate which cluster
2066 * group and descriptor bitmap offset that corresponds to. */
2067static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2068 u64 data_blkno,
2069 u64 *bg_blkno,
2070 u16 *bg_bit_off)
2071{
2072 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2073 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2074
2075 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2076
2077 *bg_blkno = ocfs2_which_cluster_group(inode,
2078 data_cluster);
2079
2080 if (*bg_blkno == osb->first_cluster_group_blkno)
2081 *bg_bit_off = (u16) data_cluster;
2082 else
2083 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2084 data_blkno - *bg_blkno);
2085}
2086
2087/*
2088 * min_bits - minimum contiguous chunk from this total allocation we
2089 * can handle. set to what we asked for originally for a full
2090 * contig. allocation, set to '1' to indicate we can deal with extents
2091 * of any size.
2092 */
Mark Fasheh415cb802007-09-16 20:10:16 -07002093int __ocfs2_claim_clusters(struct ocfs2_super *osb,
2094 handle_t *handle,
2095 struct ocfs2_alloc_context *ac,
2096 u32 min_clusters,
2097 u32 max_clusters,
2098 u32 *cluster_start,
2099 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002100{
2101 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002102 unsigned int bits_wanted = max_clusters;
Joel Becker7d1fe092010-04-13 14:30:19 +08002103 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002104
2105 mlog_entry_void();
2106
Mark Fashehccd979b2005-12-15 14:31:24 -08002107 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2108
2109 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2110 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002111
2112 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002113 WARN_ON(min_clusters > 1);
2114
Mark Fashehccd979b2005-12-15 14:31:24 -08002115 status = ocfs2_claim_local_alloc_bits(osb,
2116 handle,
2117 ac,
2118 bits_wanted,
2119 cluster_start,
2120 num_clusters);
2121 if (!status)
2122 atomic_inc(&osb->alloc_stats.local_data);
2123 } else {
2124 if (min_clusters > (osb->bitmap_cpg - 1)) {
2125 /* The only paths asking for contiguousness
2126 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002127 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2128 "group bitmap size %u!\n", min_clusters,
2129 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002130 status = -ENOSPC;
2131 goto bail;
2132 }
2133 /* clamp the current request down to a realistic size. */
2134 if (bits_wanted > (osb->bitmap_cpg - 1))
2135 bits_wanted = osb->bitmap_cpg - 1;
2136
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002137 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002138 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002139 bits_wanted,
2140 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002141 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002142 if (!status) {
2143 *cluster_start =
2144 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002145 res.sr_bg_blkno,
2146 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002147 atomic_inc(&osb->alloc_stats.bitmap_data);
2148 }
2149 }
2150 if (status < 0) {
2151 if (status != -ENOSPC)
2152 mlog_errno(status);
2153 goto bail;
2154 }
2155
Joel Becker7d1fe092010-04-13 14:30:19 +08002156 ac->ac_bits_given += res.sr_bits;
2157 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002158
2159bail:
2160 mlog_exit(status);
2161 return status;
2162}
2163
Mark Fasheh415cb802007-09-16 20:10:16 -07002164int ocfs2_claim_clusters(struct ocfs2_super *osb,
2165 handle_t *handle,
2166 struct ocfs2_alloc_context *ac,
2167 u32 min_clusters,
2168 u32 *cluster_start,
2169 u32 *num_clusters)
2170{
2171 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2172
2173 return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
2174 bits_wanted, cluster_start, num_clusters);
2175}
2176
Mark Fashehb4414ee2010-03-11 18:31:09 -08002177static int ocfs2_block_group_clear_bits(handle_t *handle,
2178 struct inode *alloc_inode,
2179 struct ocfs2_group_desc *bg,
2180 struct buffer_head *group_bh,
2181 unsigned int bit_off,
2182 unsigned int num_bits,
2183 void (*undo_fn)(unsigned int bit,
2184 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002185{
2186 int status;
2187 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002188 struct ocfs2_group_desc *undo_bg = NULL;
2189
2190 mlog_entry_void();
2191
Joel Becker42035302008-11-13 14:49:15 -08002192 /* The caller got this descriptor from
2193 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2194 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002195
2196 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2197
Mark Fashehb4414ee2010-03-11 18:31:09 -08002198 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002199 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002200 group_bh,
2201 undo_fn ?
2202 OCFS2_JOURNAL_ACCESS_UNDO :
2203 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002204 if (status < 0) {
2205 mlog_errno(status);
2206 goto bail;
2207 }
2208
Mark Fashehb4414ee2010-03-11 18:31:09 -08002209 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002210 jbd_lock_bh_state(group_bh);
2211 undo_bg = (struct ocfs2_group_desc *)
2212 bh2jh(group_bh)->b_committed_data;
2213 BUG_ON(!undo_bg);
2214 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002215
2216 tmp = num_bits;
2217 while(tmp--) {
2218 ocfs2_clear_bit((bit_off + tmp),
2219 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002220 if (undo_fn)
2221 undo_fn(bit_off + tmp,
2222 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002223 }
2224 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2225
Mark Fashehb4414ee2010-03-11 18:31:09 -08002226 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002227 jbd_unlock_bh_state(group_bh);
2228
Joel Beckerec20cec2010-03-19 14:13:52 -07002229 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002230bail:
2231 return status;
2232}
2233
2234/*
2235 * expects the suballoc inode to already be locked.
2236 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002237static int _ocfs2_free_suballoc_bits(handle_t *handle,
2238 struct inode *alloc_inode,
2239 struct buffer_head *alloc_bh,
2240 unsigned int start_bit,
2241 u64 bg_blkno,
2242 unsigned int count,
2243 void (*undo_fn)(unsigned int bit,
2244 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002245{
2246 int status = 0;
2247 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002248 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2249 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2250 struct buffer_head *group_bh = NULL;
2251 struct ocfs2_group_desc *group;
2252
2253 mlog_entry_void();
2254
Joel Becker10995aa2008-11-13 14:49:12 -08002255 /* The alloc_bh comes from ocfs2_free_dinode() or
2256 * ocfs2_free_clusters(). The callers have all locked the
2257 * allocator and gotten alloc_bh from the lock call. This
2258 * validates the dinode buffer. Any corruption that has happended
2259 * is a code bug. */
2260 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002261 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2262
Mark Fashehb0697052006-03-03 10:24:33 -08002263 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2264 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2265 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002266
Joel Becker68f64d42008-11-13 14:49:14 -08002267 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2268 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002269 if (status < 0) {
2270 mlog_errno(status);
2271 goto bail;
2272 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002273 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002274
Mark Fashehccd979b2005-12-15 14:31:24 -08002275 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2276
2277 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2278 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002279 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002280 if (status < 0) {
2281 mlog_errno(status);
2282 goto bail;
2283 }
2284
Joel Becker0cf2f762009-02-12 16:41:25 -08002285 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2286 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002287 if (status < 0) {
2288 mlog_errno(status);
2289 goto bail;
2290 }
2291
2292 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2293 count);
2294 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2295 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002296 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002297
2298bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002299 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002300
2301 mlog_exit(status);
2302 return status;
2303}
2304
Mark Fashehb4414ee2010-03-11 18:31:09 -08002305int ocfs2_free_suballoc_bits(handle_t *handle,
2306 struct inode *alloc_inode,
2307 struct buffer_head *alloc_bh,
2308 unsigned int start_bit,
2309 u64 bg_blkno,
2310 unsigned int count)
2311{
2312 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2313 start_bit, bg_blkno, count, NULL);
2314}
2315
Mark Fasheh1fabe142006-10-09 18:11:45 -07002316int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002317 struct inode *inode_alloc_inode,
2318 struct buffer_head *inode_alloc_bh,
2319 struct ocfs2_dinode *di)
2320{
2321 u64 blk = le64_to_cpu(di->i_blkno);
2322 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2323 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2324
2325 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2326 inode_alloc_bh, bit, bg_blkno, 1);
2327}
2328
Mark Fashehb4414ee2010-03-11 18:31:09 -08002329static int _ocfs2_free_clusters(handle_t *handle,
2330 struct inode *bitmap_inode,
2331 struct buffer_head *bitmap_bh,
2332 u64 start_blk,
2333 unsigned int num_clusters,
2334 void (*undo_fn)(unsigned int bit,
2335 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002336{
2337 int status;
2338 u16 bg_start_bit;
2339 u64 bg_blkno;
2340 struct ocfs2_dinode *fe;
2341
2342 /* You can't ever have a contiguous set of clusters
2343 * bigger than a block group bitmap so we never have to worry
2344 * about looping on them. */
2345
2346 mlog_entry_void();
2347
2348 /* This is expensive. We can safely remove once this stuff has
2349 * gotten tested really well. */
2350 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2351
2352 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2353
2354 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2355 &bg_start_bit);
2356
Mark Fashehb0697052006-03-03 10:24:33 -08002357 mlog(0, "want to free %u clusters starting at block %llu\n",
2358 num_clusters, (unsigned long long)start_blk);
2359 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2360 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002361
Mark Fashehb4414ee2010-03-11 18:31:09 -08002362 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2363 bg_start_bit, bg_blkno,
2364 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002365 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002366 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002367 goto out;
2368 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002369
Mark Fasheh9c7af402008-07-28 18:02:53 -07002370 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2371 num_clusters);
2372
2373out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002374 mlog_exit(status);
2375 return status;
2376}
2377
Mark Fashehb4414ee2010-03-11 18:31:09 -08002378int ocfs2_free_clusters(handle_t *handle,
2379 struct inode *bitmap_inode,
2380 struct buffer_head *bitmap_bh,
2381 u64 start_blk,
2382 unsigned int num_clusters)
2383{
2384 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2385 start_blk, num_clusters,
2386 _ocfs2_set_bit);
2387}
2388
2389/*
2390 * Give never-used clusters back to the global bitmap. We don't need
2391 * to protect these bits in the undo buffer.
2392 */
2393int ocfs2_release_clusters(handle_t *handle,
2394 struct inode *bitmap_inode,
2395 struct buffer_head *bitmap_bh,
2396 u64 start_blk,
2397 unsigned int num_clusters)
2398{
2399 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2400 start_blk, num_clusters,
2401 _ocfs2_clear_bit);
2402}
2403
Mark Fashehccd979b2005-12-15 14:31:24 -08002404static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2405{
2406 printk("Block Group:\n");
2407 printk("bg_signature: %s\n", bg->bg_signature);
2408 printk("bg_size: %u\n", bg->bg_size);
2409 printk("bg_bits: %u\n", bg->bg_bits);
2410 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2411 printk("bg_chain: %u\n", bg->bg_chain);
2412 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb0697052006-03-03 10:24:33 -08002413 printk("bg_next_group: %llu\n",
2414 (unsigned long long)bg->bg_next_group);
2415 printk("bg_parent_dinode: %llu\n",
2416 (unsigned long long)bg->bg_parent_dinode);
2417 printk("bg_blkno: %llu\n",
2418 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002419}
2420
2421static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2422{
2423 int i;
2424
Mark Fashehb0697052006-03-03 10:24:33 -08002425 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002426 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb0697052006-03-03 10:24:33 -08002427 printk("i_size: %llu\n",
2428 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002429 printk("i_clusters: %u\n", fe->i_clusters);
2430 printk("i_generation: %u\n",
2431 le32_to_cpu(fe->i_generation));
2432 printk("id1.bitmap1.i_used: %u\n",
2433 le32_to_cpu(fe->id1.bitmap1.i_used));
2434 printk("id1.bitmap1.i_total: %u\n",
2435 le32_to_cpu(fe->id1.bitmap1.i_total));
2436 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2437 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2438 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2439 printk("id2.i_chain.cl_next_free_rec: %u\n",
2440 fe->id2.i_chain.cl_next_free_rec);
2441 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2442 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2443 fe->id2.i_chain.cl_recs[i].c_free);
2444 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2445 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb0697052006-03-03 10:24:33 -08002446 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2447 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002448 }
2449}
Tao Mae7d4cb62008-08-18 17:38:44 +08002450
2451/*
2452 * For a given allocation, determine which allocators will need to be
2453 * accessed, and lock them, reserving the appropriate number of bits.
2454 *
2455 * Sparse file systems call this from ocfs2_write_begin_nolock()
2456 * and ocfs2_allocate_unwritten_extents().
2457 *
2458 * File systems which don't support holes call this from
2459 * ocfs2_extend_allocation().
2460 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002461int ocfs2_lock_allocators(struct inode *inode,
2462 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002463 u32 clusters_to_add, u32 extents_to_split,
2464 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002465 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002466{
2467 int ret = 0, num_free_extents;
2468 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2469 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2470
2471 *meta_ac = NULL;
2472 if (data_ac)
2473 *data_ac = NULL;
2474
2475 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2476
Joel Becker3d03a302009-02-12 17:49:26 -08002477 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002478 if (num_free_extents < 0) {
2479 ret = num_free_extents;
2480 mlog_errno(ret);
2481 goto out;
2482 }
2483
2484 /*
2485 * Sparse allocation file systems need to be more conservative
2486 * with reserving room for expansion - the actual allocation
2487 * happens while we've got a journal handle open so re-taking
2488 * a cluster lock (because we ran out of room for another
2489 * extent) will violate ordering rules.
2490 *
2491 * Most of the time we'll only be seeing this 1 cluster at a time
2492 * anyway.
2493 *
2494 * Always lock for any unwritten extents - we might want to
2495 * add blocks during a split.
2496 */
2497 if (!num_free_extents ||
2498 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002499 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002500 if (ret < 0) {
2501 if (ret != -ENOSPC)
2502 mlog_errno(ret);
2503 goto out;
2504 }
2505 }
2506
2507 if (clusters_to_add == 0)
2508 goto out;
2509
2510 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2511 if (ret < 0) {
2512 if (ret != -ENOSPC)
2513 mlog_errno(ret);
2514 goto out;
2515 }
2516
2517out:
2518 if (ret) {
2519 if (*meta_ac) {
2520 ocfs2_free_alloc_context(*meta_ac);
2521 *meta_ac = NULL;
2522 }
2523
2524 /*
2525 * We cannot have an error and a non null *data_ac.
2526 */
2527 }
2528
2529 return ret;
2530}
wengang wang6ca497a2009-03-06 21:29:10 +08002531
2532/*
2533 * Read the inode specified by blkno to get suballoc_slot and
2534 * suballoc_bit.
2535 */
2536static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2537 u16 *suballoc_slot, u16 *suballoc_bit)
2538{
2539 int status;
2540 struct buffer_head *inode_bh = NULL;
2541 struct ocfs2_dinode *inode_fe;
2542
Joel Becker5b09b502009-04-21 16:31:20 -07002543 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002544
2545 /* dirty read disk */
2546 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2547 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002548 mlog(ML_ERROR, "read block %llu failed %d\n",
2549 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002550 goto bail;
2551 }
2552
2553 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2554 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002555 mlog(ML_ERROR, "invalid inode %llu requested\n",
2556 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002557 status = -EINVAL;
2558 goto bail;
2559 }
2560
Tao Ma0fba8132009-03-19 05:08:43 +08002561 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002562 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2563 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002564 (unsigned long long)blkno,
2565 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002566 status = -EINVAL;
2567 goto bail;
2568 }
2569
2570 if (suballoc_slot)
2571 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2572 if (suballoc_bit)
2573 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2574
2575bail:
2576 brelse(inode_bh);
2577
2578 mlog_exit(status);
2579 return status;
2580}
2581
2582/*
2583 * test whether bit is SET in allocator bitmap or not. on success, 0
2584 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2585 * is returned and *res is meaningless. Call this after you have
2586 * cluster locked against suballoc, or you may get a result based on
2587 * non-up2date contents
2588 */
2589static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2590 struct inode *suballoc,
2591 struct buffer_head *alloc_bh, u64 blkno,
2592 u16 bit, int *res)
2593{
2594 struct ocfs2_dinode *alloc_fe;
2595 struct ocfs2_group_desc *group;
2596 struct buffer_head *group_bh = NULL;
2597 u64 bg_blkno;
2598 int status;
2599
Joel Becker5b09b502009-04-21 16:31:20 -07002600 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2601 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002602
2603 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2604 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2605 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2606 (unsigned int)bit,
2607 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2608 status = -EINVAL;
2609 goto bail;
2610 }
2611
2612 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2613 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2614 &group_bh);
2615 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002616 mlog(ML_ERROR, "read group %llu failed %d\n",
2617 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002618 goto bail;
2619 }
2620
2621 group = (struct ocfs2_group_desc *) group_bh->b_data;
2622 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2623
2624bail:
2625 brelse(group_bh);
2626
2627 mlog_exit(status);
2628 return status;
2629}
2630
2631/*
2632 * Test if the bit representing this inode (blkno) is set in the
2633 * suballocator.
2634 *
2635 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2636 *
2637 * In the event of failure, a negative value is returned and *res is
2638 * meaningless.
2639 *
2640 * Callers must make sure to hold nfs_sync_lock to prevent
2641 * ocfs2_delete_inode() on another node from accessing the same
2642 * suballocator concurrently.
2643 */
2644int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2645{
2646 int status;
2647 u16 suballoc_bit = 0, suballoc_slot = 0;
2648 struct inode *inode_alloc_inode;
2649 struct buffer_head *alloc_bh = NULL;
2650
Joel Becker5b09b502009-04-21 16:31:20 -07002651 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002652
2653 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2654 &suballoc_bit);
2655 if (status < 0) {
2656 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2657 goto bail;
2658 }
2659
2660 inode_alloc_inode =
2661 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2662 suballoc_slot);
2663 if (!inode_alloc_inode) {
2664 /* the error code could be inaccurate, but we are not able to
2665 * get the correct one. */
2666 status = -EINVAL;
2667 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2668 (u32)suballoc_slot);
2669 goto bail;
2670 }
2671
2672 mutex_lock(&inode_alloc_inode->i_mutex);
2673 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2674 if (status < 0) {
2675 mutex_unlock(&inode_alloc_inode->i_mutex);
2676 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2677 (u32)suballoc_slot, status);
2678 goto bail;
2679 }
2680
2681 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2682 blkno, suballoc_bit, res);
2683 if (status < 0)
2684 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2685
2686 ocfs2_inode_unlock(inode_alloc_inode, 0);
2687 mutex_unlock(&inode_alloc_inode->i_mutex);
2688
2689 iput(inode_alloc_inode);
2690 brelse(alloc_bh);
2691bail:
2692 mlog_exit(status);
2693 return status;
2694}