blob: e5403acdb3f5b7a1c8a76edbb3379f72b0316993 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * suballoc.c
5 *
6 * metadata alloc and free
7 * Inspired by ext3 block groups.
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public
22 * License along with this program; if not, write to the
23 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 * Boston, MA 021110-1307, USA.
25 */
26
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/highmem.h>
31
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070038#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "inode.h"
41#include "journal.h"
42#include "localalloc.h"
43#include "suballoc.h"
44#include "super.h"
45#include "sysfile.h"
46#include "uptodate.h"
47
48#include "buffer_head_io.h"
49
Tao Maffda89a2008-03-03 17:12:09 +080050#define NOT_ALLOC_NEW_GROUP 0
Tao Ma60ca81e2009-02-25 00:53:24 +080051#define ALLOC_NEW_GROUP 0x1
52#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080053
Tiger Yangb89c5422010-01-25 14:11:06 +080054#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080055
Joel Becker7d1fe092010-04-13 14:30:19 +080056struct ocfs2_suballoc_result {
Joel Becker2b6cb572010-03-26 10:09:15 +080057 u64 sr_bg_blkno; /* The bg we allocated from. Set
58 to 0 when a block group is
59 contiguous. */
Joel Beckerba206632010-03-26 10:08:59 +080060 u64 sr_blkno; /* The first allocated block */
Joel Becker7d1fe092010-04-13 14:30:19 +080061 unsigned int sr_bit_offset; /* The bit in the bg */
62 unsigned int sr_bits; /* How many bits we claimed */
63};
64
Mark Fashehccd979b2005-12-15 14:31:24 -080065static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
66static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
67static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070068static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080069 struct inode *alloc_inode,
70 struct buffer_head *bg_bh,
71 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080072 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080073 u16 my_chain,
74 struct ocfs2_chain_list *cl);
75static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
76 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070077 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080078 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080079 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080080 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080081
Mark Fashehccd979b2005-12-15 14:31:24 -080082static int ocfs2_cluster_group_search(struct inode *inode,
83 struct buffer_head *group_bh,
84 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070085 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080086 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080087static int ocfs2_block_group_search(struct inode *inode,
88 struct buffer_head *group_bh,
89 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -070090 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +080091 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +080092static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -070093 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080094 u32 bits_wanted,
95 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +080096 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -080097static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
98 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -070099static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800100 struct inode *alloc_inode,
101 struct ocfs2_group_desc *bg,
102 struct buffer_head *group_bh,
103 unsigned int bit_off,
104 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700105static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800106 struct inode *alloc_inode,
107 struct buffer_head *fe_bh,
108 struct buffer_head *bg_bh,
109 struct buffer_head *prev_bg_bh,
110 u16 chain);
111static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
112 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800113static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
114 u64 bg_blkno,
115 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800116static inline void ocfs2_block_to_cluster_group(struct inode *inode,
117 u64 data_blkno,
118 u64 *bg_blkno,
119 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700120static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
121 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800122 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700123 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800124
Mark Fasheh9c7af402008-07-28 18:02:53 -0700125void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800126{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700127 struct inode *inode = ac->ac_inode;
128
129 if (inode) {
130 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700131 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700132
133 mutex_unlock(&inode->i_mutex);
134
135 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800136 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700137 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700138 brelse(ac->ac_bh);
139 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800140 ac->ac_resv = NULL;
Tao Ma4d0ddb22008-03-05 16:11:46 +0800141}
142
143void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
144{
145 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800146 kfree(ac);
147}
148
149static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
150{
151 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
152}
153
Joel Becker57e3e792008-11-13 14:49:13 -0800154#define do_error(fmt, ...) \
155 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800156 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800157 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
158 else \
159 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
160 } while (0)
161
Joel Becker970e4932008-11-13 14:49:19 -0800162static int ocfs2_validate_gd_self(struct super_block *sb,
163 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800164 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800165{
166 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
167
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700168 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800169 do_error("Group descriptor #%llu has bad signature %.*s",
170 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800171 gd->bg_signature);
172 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700173 }
174
Joel Becker68f64d42008-11-13 14:49:14 -0800175 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
176 do_error("Group descriptor #%llu has an invalid bg_blkno "
177 "of %llu",
178 (unsigned long long)bh->b_blocknr,
179 (unsigned long long)le64_to_cpu(gd->bg_blkno));
180 return -EINVAL;
181 }
182
183 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
184 do_error("Group descriptor #%llu has an invalid "
185 "fs_generation of #%u",
186 (unsigned long long)bh->b_blocknr,
187 le32_to_cpu(gd->bg_generation));
188 return -EINVAL;
189 }
190
Joel Becker970e4932008-11-13 14:49:19 -0800191 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
192 do_error("Group descriptor #%llu has bit count %u but "
193 "claims that %u are free",
194 (unsigned long long)bh->b_blocknr,
195 le16_to_cpu(gd->bg_bits),
196 le16_to_cpu(gd->bg_free_bits_count));
197 return -EINVAL;
198 }
199
200 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
201 do_error("Group descriptor #%llu has bit count %u but "
202 "max bitmap bits of %u",
203 (unsigned long long)bh->b_blocknr,
204 le16_to_cpu(gd->bg_bits),
205 8 * le16_to_cpu(gd->bg_size));
206 return -EINVAL;
207 }
208
209 return 0;
210}
211
212static int ocfs2_validate_gd_parent(struct super_block *sb,
213 struct ocfs2_dinode *di,
214 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800215 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800216{
217 unsigned int max_bits;
218 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
219
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700220 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800221 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800222 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800223 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800224 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
225 (unsigned long long)le64_to_cpu(di->i_blkno));
226 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700227 }
228
229 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
230 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800231 do_error("Group descriptor #%llu has bit count of %u",
232 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800233 le16_to_cpu(gd->bg_bits));
234 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700235 }
236
Tao Ma78c37eb2010-03-03 11:26:27 +0800237 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
238 if ((le16_to_cpu(gd->bg_chain) >
239 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
240 ((le16_to_cpu(gd->bg_chain) ==
241 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800242 do_error("Group descriptor #%llu has bad chain %u",
243 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800244 le16_to_cpu(gd->bg_chain));
245 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700246 }
247
Joel Becker970e4932008-11-13 14:49:19 -0800248 return 0;
249}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700250
Joel Becker57e3e792008-11-13 14:49:13 -0800251#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700252
Joel Becker970e4932008-11-13 14:49:19 -0800253/*
254 * This version only prints errors. It does not fail the filesystem, and
255 * exists only for resize.
256 */
257int ocfs2_check_group_descriptor(struct super_block *sb,
258 struct ocfs2_dinode *di,
259 struct buffer_head *bh)
260{
261 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700262 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800263
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700264 BUG_ON(!buffer_uptodate(bh));
265
266 /*
267 * If the ecc fails, we return the error but otherwise
268 * leave the filesystem running. We know any error is
269 * local to this block.
270 */
271 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700272 if (rc) {
273 mlog(ML_ERROR,
274 "Checksum failed for group descriptor %llu\n",
275 (unsigned long long)bh->b_blocknr);
276 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700277 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800278 if (!rc)
279 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
280
281 return rc;
282}
283
284static int ocfs2_validate_group_descriptor(struct super_block *sb,
285 struct buffer_head *bh)
286{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700287 int rc;
288 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
289
Joel Becker970e4932008-11-13 14:49:19 -0800290 mlog(0, "Validating group descriptor %llu\n",
291 (unsigned long long)bh->b_blocknr);
292
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700293 BUG_ON(!buffer_uptodate(bh));
294
295 /*
296 * If the ecc fails, we return the error but otherwise
297 * leave the filesystem running. We know any error is
298 * local to this block.
299 */
300 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
301 if (rc)
302 return rc;
303
304 /*
305 * Errors after here are fatal.
306 */
307
Joel Becker970e4932008-11-13 14:49:19 -0800308 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700309}
310
Joel Becker68f64d42008-11-13 14:49:14 -0800311int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
312 u64 gd_blkno, struct buffer_head **bh)
313{
314 int rc;
315 struct buffer_head *tmp = *bh;
316
Joel Becker8cb471e2009-02-10 20:00:41 -0800317 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800318 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800319 if (rc)
320 goto out;
321
Joel Becker970e4932008-11-13 14:49:19 -0800322 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800323 if (rc) {
324 brelse(tmp);
325 goto out;
326 }
327
328 /* If ocfs2_read_block() got us a new bh, pass it up. */
329 if (!*bh)
330 *bh = tmp;
331
332out:
333 return rc;
334}
335
Joel Becker798db352010-04-13 14:26:32 +0800336static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
337 struct ocfs2_group_desc *bg,
338 struct ocfs2_chain_list *cl,
339 u64 p_blkno, u32 clusters)
340{
341 struct ocfs2_extent_list *el = &bg->bg_list;
342 struct ocfs2_extent_rec *rec;
343
344 BUG_ON(!ocfs2_supports_discontig_bh(osb));
345 if (!el->l_next_free_rec)
346 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
347 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
348 rec->e_blkno = p_blkno;
349 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
350 le16_to_cpu(cl->cl_bpc));
351 rec->e_leaf_clusters = cpu_to_le32(clusters);
352 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
353 le16_add_cpu(&el->l_next_free_rec, 1);
354}
355
Mark Fasheh1fabe142006-10-09 18:11:45 -0700356static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800357 struct inode *alloc_inode,
358 struct buffer_head *bg_bh,
359 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800360 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800361 u16 my_chain,
362 struct ocfs2_chain_list *cl)
363{
364 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800365 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800366 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
367 struct super_block * sb = alloc_inode->i_sb;
368
369 mlog_entry_void();
370
371 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800372 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
373 "b_blocknr (%llu)",
374 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800375 (unsigned long long) bg_bh->b_blocknr);
376 status = -EIO;
377 goto bail;
378 }
379
Joel Becker13723d02008-10-17 19:25:01 -0700380 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800381 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700382 bg_bh,
383 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800384 if (status < 0) {
385 mlog_errno(status);
386 goto bail;
387 }
388
389 memset(bg, 0, sb->s_blocksize);
390 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
391 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Joel Becker4cbe4242010-04-13 14:26:12 +0800392 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -0800393 bg->bg_chain = cpu_to_le16(my_chain);
394 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
395 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
396 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800397 if (group_clusters == le16_to_cpu(cl->cl_cpg))
398 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
399 else
400 ocfs2_bg_discontig_add_extent(osb, bg, cl, bg->bg_blkno,
401 group_clusters);
402
Mark Fashehccd979b2005-12-15 14:31:24 -0800403 /* set the 1st bit in the bitmap to account for the descriptor block */
404 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
405 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
406
Joel Beckerec20cec2010-03-19 14:13:52 -0700407 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800408
409 /* There is no need to zero out or otherwise initialize the
410 * other blocks in a group - All valid FS metadata in a block
411 * group stores the superblock fs_generation value at
412 * allocation time. */
413
414bail:
415 mlog_exit(status);
416 return status;
417}
418
419static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
420{
421 u16 curr, best;
422
423 best = curr = 0;
424 while (curr < le16_to_cpu(cl->cl_count)) {
425 if (le32_to_cpu(cl->cl_recs[best].c_total) >
426 le32_to_cpu(cl->cl_recs[curr].c_total))
427 best = curr;
428 curr++;
429 }
430 return best;
431}
432
Joel Becker798db352010-04-13 14:26:32 +0800433static struct buffer_head *
434ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
435 struct inode *alloc_inode,
436 struct ocfs2_alloc_context *ac,
437 struct ocfs2_chain_list *cl)
438{
439 int status;
440 u32 bit_off, num_bits;
441 u64 bg_blkno;
442 struct buffer_head *bg_bh;
443 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
444
Joel Becker1ed9b772010-05-06 13:59:06 +0800445 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800446 le16_to_cpu(cl->cl_cpg), &bit_off,
447 &num_bits);
448 if (status < 0) {
449 if (status != -ENOSPC)
450 mlog_errno(status);
451 goto bail;
452 }
453
454 /* setup the group */
455 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
456 mlog(0, "new descriptor, record %u, at block %llu\n",
457 alloc_rec, (unsigned long long)bg_blkno);
458
459 bg_bh = sb_getblk(osb->sb, bg_blkno);
460 if (!bg_bh) {
461 status = -EIO;
462 mlog_errno(status);
463 goto bail;
464 }
465 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
466
467 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
468 bg_blkno, num_bits, alloc_rec, cl);
469 if (status < 0) {
470 brelse(bg_bh);
471 mlog_errno(status);
472 }
473
474bail:
475 return status ? ERR_PTR(status) : bg_bh;
476}
477
478static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
479 handle_t *handle,
480 struct ocfs2_alloc_context *ac,
481 unsigned int min_bits,
482 u32 *bit_off, u32 *num_bits)
483{
484 int status;
485
486 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800487 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800488 bit_off, num_bits);
489 if (status != -ENOSPC)
490 break;
491
492 min_bits >>= 1;
493 }
494
495 return status;
496}
497
498static int ocfs2_block_group_grow_discontig(handle_t *handle,
499 struct inode *alloc_inode,
500 struct buffer_head *bg_bh,
501 struct ocfs2_alloc_context *ac,
502 struct ocfs2_chain_list *cl,
503 unsigned int min_bits)
504{
505 int status;
506 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
507 struct ocfs2_group_desc *bg =
508 (struct ocfs2_group_desc *)bg_bh->b_data;
509 unsigned int needed =
510 ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
511 u32 p_cpos, clusters;
512 u64 p_blkno;
513 struct ocfs2_extent_list *el = &bg->bg_list;
514
515 status = ocfs2_journal_access_gd(handle,
516 INODE_CACHE(alloc_inode),
517 bg_bh,
518 OCFS2_JOURNAL_ACCESS_CREATE);
519 if (status < 0) {
520 mlog_errno(status);
521 goto bail;
522 }
523
524 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
525 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800526 if (min_bits > needed)
527 min_bits = needed;
528 status = ocfs2_block_group_claim_bits(osb, handle, ac,
529 min_bits, &p_cpos,
530 &clusters);
531 if (status < 0) {
532 if (status != -ENOSPC)
533 mlog_errno(status);
534 goto bail;
535 }
536 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
537 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
538 clusters);
539
540 min_bits = clusters;
541 needed = ocfs2_bits_per_group(cl) - le16_to_cpu(bg->bg_bits);
542 }
543
544 if (needed > 0) {
545 }
546
547 ocfs2_journal_dirty(handle, bg_bh);
548
549bail:
550 return status;
551}
552
Joel Becker8b06bc52010-03-26 10:09:29 +0800553static void ocfs2_bg_alloc_cleanup(handle_t *handle,
554 struct ocfs2_alloc_context *cluster_ac,
555 struct inode *alloc_inode,
556 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800557{
Joel Becker8b06bc52010-03-26 10:09:29 +0800558 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800559 struct ocfs2_group_desc *bg;
560 struct ocfs2_extent_list *el;
561 struct ocfs2_extent_rec *rec;
562
563 if (!bg_bh)
564 return;
565
566 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
567 el = &bg->bg_list;
568 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
569 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800570 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
571 cluster_ac->ac_bh,
572 le64_to_cpu(rec->e_blkno),
573 le32_to_cpu(rec->e_leaf_clusters));
574 if (ret)
575 mlog_errno(ret);
576 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800577 }
578
579 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
580 brelse(bg_bh);
581}
582
583static struct buffer_head *
584ocfs2_block_group_alloc_discontig(handle_t *handle,
585 struct inode *alloc_inode,
586 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800587 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800588{
589 int status;
590 u32 bit_off, num_bits;
591 u64 bg_blkno;
592 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
593 struct buffer_head *bg_bh = NULL;
594 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
595 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
596
597 if (!ocfs2_supports_discontig_bh(osb)) {
598 status = -ENOSPC;
599 goto bail;
600 }
601
Joel Becker8b06bc52010-03-26 10:09:29 +0800602 status = ocfs2_extend_trans(handle,
603 ocfs2_calc_bg_discontig_credits(osb->sb));
604 if (status) {
605 mlog_errno(status);
606 goto bail;
607 }
608
Joel Becker798db352010-04-13 14:26:32 +0800609 /* Claim the first region */
610 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
611 &bit_off, &num_bits);
612 if (status < 0) {
613 if (status != -ENOSPC)
614 mlog_errno(status);
615 goto bail;
616 }
617 min_bits = num_bits;
618
619 /* setup the group */
620 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
621 mlog(0, "new descriptor, record %u, at block %llu\n",
622 alloc_rec, (unsigned long long)bg_blkno);
623
624 bg_bh = sb_getblk(osb->sb, bg_blkno);
625 if (!bg_bh) {
626 status = -EIO;
627 mlog_errno(status);
628 goto bail;
629 }
630 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
631
632 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
633 bg_blkno, num_bits, alloc_rec, cl);
634 if (status < 0) {
635 mlog_errno(status);
636 goto bail;
637 }
638
639 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
640 bg_bh, ac, cl, min_bits);
641 if (status)
642 mlog_errno(status);
643
644bail:
645 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800646 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800647 return status ? ERR_PTR(status) : bg_bh;
648}
649
Mark Fashehccd979b2005-12-15 14:31:24 -0800650/*
651 * We expect the block group allocator to already be locked.
652 */
653static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
654 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700655 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800656 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800657 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800658 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800659{
660 int status, credits;
661 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
662 struct ocfs2_chain_list *cl;
663 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700664 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800665 u64 bg_blkno;
666 struct buffer_head *bg_bh = NULL;
667 struct ocfs2_group_desc *bg;
668
669 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
670
671 mlog_entry_void();
672
Mark Fashehccd979b2005-12-15 14:31:24 -0800673 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700674 status = ocfs2_reserve_clusters_with_limit(osb,
675 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800676 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800677 if (status < 0) {
678 if (status != -ENOSPC)
679 mlog_errno(status);
680 goto bail;
681 }
682
683 credits = ocfs2_calc_group_alloc_credits(osb->sb,
684 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700685 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800686 if (IS_ERR(handle)) {
687 status = PTR_ERR(handle);
688 handle = NULL;
689 mlog_errno(status);
690 goto bail;
691 }
692
Tao Mafeb473a2009-02-25 00:53:25 +0800693 if (last_alloc_group && *last_alloc_group != 0) {
694 mlog(0, "use old allocation group %llu for block group alloc\n",
695 (unsigned long long)*last_alloc_group);
696 ac->ac_last_group = *last_alloc_group;
697 }
Joel Becker798db352010-04-13 14:26:32 +0800698
699 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
700 ac, cl);
701 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
702 bg_bh = ocfs2_block_group_alloc_discontig(handle,
703 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800704 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800705 if (IS_ERR(bg_bh)) {
706 status = PTR_ERR(bg_bh);
707 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800708 if (status != -ENOSPC)
709 mlog_errno(status);
710 goto bail;
711 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800712 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
713
Joel Becker0cf2f762009-02-12 16:41:25 -0800714 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700715 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800716 if (status < 0) {
717 mlog_errno(status);
718 goto bail;
719 }
720
Joel Becker798db352010-04-13 14:26:32 +0800721 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800722 le16_to_cpu(bg->bg_free_bits_count));
Joel Becker798db352010-04-13 14:26:32 +0800723 le32_add_cpu(&cl->cl_recs[bg->bg_chain].c_total,
724 le16_to_cpu(bg->bg_bits));
725 cl->cl_recs[bg->bg_chain].c_blkno = cpu_to_le64(bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800726 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
727 le16_add_cpu(&cl->cl_next_free_rec, 1);
728
729 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
730 le16_to_cpu(bg->bg_free_bits_count));
731 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
732 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
733
Joel Beckerec20cec2010-03-19 14:13:52 -0700734 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800735
736 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
737 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
738 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
739 le32_to_cpu(fe->i_clusters)));
740 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
741 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700742 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800743
744 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800745
746 /* save the new last alloc group so that the caller can cache it. */
747 if (last_alloc_group)
748 *last_alloc_group = ac->ac_last_group;
749
Mark Fashehccd979b2005-12-15 14:31:24 -0800750bail:
751 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700752 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800753
754 if (ac)
755 ocfs2_free_alloc_context(ac);
756
Mark Fasheha81cb882008-10-07 14:25:16 -0700757 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800758
759 mlog_exit(status);
760 return status;
761}
762
763static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700764 struct ocfs2_alloc_context *ac,
765 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800766 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800767 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800768 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800769{
770 int status;
771 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700772 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800774 struct ocfs2_dinode *fe;
775 u32 free_bits;
776
777 mlog_entry_void();
778
Mark Fashehda5cbf22006-10-06 18:34:35 -0700779 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
780 if (!alloc_inode) {
781 mlog_errno(-EINVAL);
782 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800783 }
784
Mark Fashehda5cbf22006-10-06 18:34:35 -0700785 mutex_lock(&alloc_inode->i_mutex);
786
Mark Fashehe63aecb62007-10-18 15:30:42 -0700787 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700788 if (status < 0) {
789 mutex_unlock(&alloc_inode->i_mutex);
790 iput(alloc_inode);
791
792 mlog_errno(status);
793 return status;
794 }
795
796 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800797 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700798
Mark Fashehccd979b2005-12-15 14:31:24 -0800799 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800800
801 /* The bh was validated by the inode read inside
802 * ocfs2_inode_lock(). Any corruption is a code bug. */
803 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
804
Mark Fashehccd979b2005-12-15 14:31:24 -0800805 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800806 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
807 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800808 status = -EIO;
809 goto bail;
810 }
811
812 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
813 le32_to_cpu(fe->id1.bitmap1.i_used);
814
815 if (bits_wanted > free_bits) {
816 /* cluster bitmap never grows */
817 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
818 mlog(0, "Disk Full: wanted=%u, free_bits=%u\n",
819 bits_wanted, free_bits);
820 status = -ENOSPC;
821 goto bail;
822 }
823
Tao Ma60ca81e2009-02-25 00:53:24 +0800824 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Maffda89a2008-03-03 17:12:09 +0800825 mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
826 "and we don't alloc a new group for it.\n",
827 slot, bits_wanted, free_bits);
828 status = -ENOSPC;
829 goto bail;
830 }
831
Joel Becker1187c962008-09-03 20:03:39 -0700832 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800833 ac->ac_max_block,
834 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800835 if (status < 0) {
836 if (status != -ENOSPC)
837 mlog_errno(status);
838 goto bail;
839 }
840 atomic_inc(&osb->alloc_stats.bg_extends);
841
842 /* You should never ask for this much metadata */
843 BUG_ON(bits_wanted >
844 (le32_to_cpu(fe->id1.bitmap1.i_total)
845 - le32_to_cpu(fe->id1.bitmap1.i_used)));
846 }
847
848 get_bh(bh);
849 ac->ac_bh = bh;
850bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700851 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800852
853 mlog_exit(status);
854 return status;
855}
856
Tiger Yangb89c5422010-01-25 14:11:06 +0800857static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
858{
859 spin_lock(&osb->osb_lock);
860 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
861 spin_unlock(&osb->osb_lock);
862 atomic_set(&osb->s_num_inodes_stolen, 0);
863}
864
865static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
866{
867 spin_lock(&osb->osb_lock);
868 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
869 spin_unlock(&osb->osb_lock);
870 atomic_set(&osb->s_num_meta_stolen, 0);
871}
872
873void ocfs2_init_steal_slots(struct ocfs2_super *osb)
874{
875 ocfs2_init_inode_steal_slot(osb);
876 ocfs2_init_meta_steal_slot(osb);
877}
878
879static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
880{
881 spin_lock(&osb->osb_lock);
882 if (type == INODE_ALLOC_SYSTEM_INODE)
883 osb->s_inode_steal_slot = slot;
884 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
885 osb->s_meta_steal_slot = slot;
886 spin_unlock(&osb->osb_lock);
887}
888
889static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
890{
891 int slot = OCFS2_INVALID_SLOT;
892
893 spin_lock(&osb->osb_lock);
894 if (type == INODE_ALLOC_SYSTEM_INODE)
895 slot = osb->s_inode_steal_slot;
896 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
897 slot = osb->s_meta_steal_slot;
898 spin_unlock(&osb->osb_lock);
899
900 return slot;
901}
902
903static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
904{
905 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
906}
907
908static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
909{
910 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
911}
912
913static int ocfs2_steal_resource(struct ocfs2_super *osb,
914 struct ocfs2_alloc_context *ac,
915 int type)
916{
917 int i, status = -ENOSPC;
918 int slot = __ocfs2_get_steal_slot(osb, type);
919
920 /* Start to steal resource from the first slot after ours. */
921 if (slot == OCFS2_INVALID_SLOT)
922 slot = osb->slot_num + 1;
923
924 for (i = 0; i < osb->max_slots; i++, slot++) {
925 if (slot == osb->max_slots)
926 slot = 0;
927
928 if (slot == osb->slot_num)
929 continue;
930
931 status = ocfs2_reserve_suballoc_bits(osb, ac,
932 type,
933 (u32)slot, NULL,
934 NOT_ALLOC_NEW_GROUP);
935 if (status >= 0) {
936 __ocfs2_set_steal_slot(osb, slot, type);
937 break;
938 }
939
940 ocfs2_free_ac_resource(ac);
941 }
942
943 return status;
944}
945
946static int ocfs2_steal_inode(struct ocfs2_super *osb,
947 struct ocfs2_alloc_context *ac)
948{
949 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
950}
951
952static int ocfs2_steal_meta(struct ocfs2_super *osb,
953 struct ocfs2_alloc_context *ac)
954{
955 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
956}
957
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800958int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
959 int blocks,
960 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800961{
962 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +0800963 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800964
Robert P. J. Daycd861282006-12-13 00:34:52 -0800965 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800966 if (!(*ac)) {
967 status = -ENOMEM;
968 mlog_errno(status);
969 goto bail;
970 }
971
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800972 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -0800973 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 (*ac)->ac_group_search = ocfs2_block_group_search;
975
Tiger Yangb89c5422010-01-25 14:11:06 +0800976 if (slot != OCFS2_INVALID_SLOT &&
977 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
978 goto extent_steal;
979
980 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700981 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +0800982 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +0800983 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -0800984 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +0800985
986
987 if (status >= 0) {
988 status = 0;
989 if (slot != OCFS2_INVALID_SLOT)
990 ocfs2_init_meta_steal_slot(osb);
991 goto bail;
992 } else if (status < 0 && status != -ENOSPC) {
993 mlog_errno(status);
994 goto bail;
995 }
996
997 ocfs2_free_ac_resource(*ac);
998
999extent_steal:
1000 status = ocfs2_steal_meta(osb, *ac);
1001 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 if (status < 0) {
1003 if (status != -ENOSPC)
1004 mlog_errno(status);
1005 goto bail;
1006 }
1007
1008 status = 0;
1009bail:
1010 if ((status < 0) && *ac) {
1011 ocfs2_free_alloc_context(*ac);
1012 *ac = NULL;
1013 }
1014
Mark Fashehccd979b2005-12-15 14:31:24 -08001015 mlog_exit(status);
1016 return status;
1017}
1018
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001019int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1020 struct ocfs2_extent_list *root_el,
1021 struct ocfs2_alloc_context **ac)
1022{
1023 return ocfs2_reserve_new_metadata_blocks(osb,
1024 ocfs2_extend_meta_needed(root_el),
1025 ac);
1026}
1027
Mark Fashehccd979b2005-12-15 14:31:24 -08001028int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001029 struct ocfs2_alloc_context **ac)
1030{
1031 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001032 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001033 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001034
Robert P. J. Daycd861282006-12-13 00:34:52 -08001035 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 if (!(*ac)) {
1037 status = -ENOMEM;
1038 mlog_errno(status);
1039 goto bail;
1040 }
1041
1042 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1044
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 (*ac)->ac_group_search = ocfs2_block_group_search;
1046
Tao Ma4d0ddb22008-03-05 16:11:46 +08001047 /*
Joel Becker1187c962008-09-03 20:03:39 -07001048 * stat(2) can't handle i_ino > 32bits, so we tell the
1049 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001050 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001051 */
Joel Becker12462f12008-09-03 20:03:40 -07001052 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1053 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001054
1055 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001056 * slot is set when we successfully steal inode from other nodes.
1057 * It is reset in 3 places:
1058 * 1. when we flush the truncate log
1059 * 2. when we complete local alloc recovery.
1060 * 3. when we successfully allocate from our own slot.
1061 * After it is set, we will go on stealing inodes until we find the
1062 * need to check our slots to see whether there is some space for us.
1063 */
1064 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001065 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001066 goto inode_steal;
1067
1068 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001069 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001070 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1071 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001072 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001073 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001074 ALLOC_NEW_GROUP |
1075 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001076 if (status >= 0) {
1077 status = 0;
1078
Tao Mafeb473a2009-02-25 00:53:25 +08001079 spin_lock(&osb->osb_lock);
1080 osb->osb_inode_alloc_group = alloc_group;
1081 spin_unlock(&osb->osb_lock);
1082 mlog(0, "after reservation, new allocation group is "
1083 "%llu\n", (unsigned long long)alloc_group);
1084
Tao Ma4d0ddb22008-03-05 16:11:46 +08001085 /*
1086 * Some inodes must be freed by us, so try to allocate
1087 * from our own next time.
1088 */
1089 if (slot != OCFS2_INVALID_SLOT)
1090 ocfs2_init_inode_steal_slot(osb);
1091 goto bail;
1092 } else if (status < 0 && status != -ENOSPC) {
1093 mlog_errno(status);
1094 goto bail;
1095 }
1096
1097 ocfs2_free_ac_resource(*ac);
1098
1099inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001100 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001101 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001102 if (status < 0) {
1103 if (status != -ENOSPC)
1104 mlog_errno(status);
1105 goto bail;
1106 }
1107
1108 status = 0;
1109bail:
1110 if ((status < 0) && *ac) {
1111 ocfs2_free_alloc_context(*ac);
1112 *ac = NULL;
1113 }
1114
Mark Fashehccd979b2005-12-15 14:31:24 -08001115 mlog_exit(status);
1116 return status;
1117}
1118
1119/* local alloc code has to do the same thing, so rather than do this
1120 * twice.. */
1121int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1122 struct ocfs2_alloc_context *ac)
1123{
1124 int status;
1125
Mark Fashehccd979b2005-12-15 14:31:24 -08001126 ac->ac_which = OCFS2_AC_USE_MAIN;
1127 ac->ac_group_search = ocfs2_cluster_group_search;
1128
Mark Fashehda5cbf22006-10-06 18:34:35 -07001129 status = ocfs2_reserve_suballoc_bits(osb, ac,
1130 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001131 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001132 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001133 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001134 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001135 goto bail;
1136 }
1137
Mark Fashehccd979b2005-12-15 14:31:24 -08001138bail:
1139 return status;
1140}
1141
1142/* Callers don't need to care which bitmap (local alloc or main) to
1143 * use so we figure it out for them, but unfortunately this clutters
1144 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001145static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1146 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001147 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001148 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001149{
1150 int status;
1151
1152 mlog_entry_void();
1153
Robert P. J. Daycd861282006-12-13 00:34:52 -08001154 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 if (!(*ac)) {
1156 status = -ENOMEM;
1157 mlog_errno(status);
1158 goto bail;
1159 }
1160
1161 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001162 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001163
1164 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001165 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1166 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 bits_wanted,
1169 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001170 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001171 mlog_errno(status);
1172 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001173 }
1174 }
1175
1176 if (status == -ENOSPC) {
1177 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1178 if (status < 0) {
1179 if (status != -ENOSPC)
1180 mlog_errno(status);
1181 goto bail;
1182 }
1183 }
1184
1185 status = 0;
1186bail:
1187 if ((status < 0) && *ac) {
1188 ocfs2_free_alloc_context(*ac);
1189 *ac = NULL;
1190 }
1191
1192 mlog_exit(status);
1193 return status;
1194}
1195
Joel Becker1187c962008-09-03 20:03:39 -07001196int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1197 u32 bits_wanted,
1198 struct ocfs2_alloc_context **ac)
1199{
Tao Ma60ca81e2009-02-25 00:53:24 +08001200 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1201 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001202}
1203
Mark Fashehccd979b2005-12-15 14:31:24 -08001204/*
1205 * More or less lifted from ext3. I'll leave their description below:
1206 *
1207 * "For ext3 allocations, we must not reuse any blocks which are
1208 * allocated in the bitmap buffer's "last committed data" copy. This
1209 * prevents deletes from freeing up the page for reuse until we have
1210 * committed the delete transaction.
1211 *
1212 * If we didn't do this, then deleting something and reallocating it as
1213 * data would allow the old block to be overwritten before the
1214 * transaction committed (because we force data to disk before commit).
1215 * This would lead to corruption if we crashed between overwriting the
1216 * data and committing the delete.
1217 *
1218 * @@@ We may want to make this allocation behaviour conditional on
1219 * data-writes at some point, and disable it for metadata allocations or
1220 * sync-data inodes."
1221 *
1222 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001223 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001224 * called on a metadata group descriptor.
1225 */
1226static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1227 int nr)
1228{
1229 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001230 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001231
1232 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1233 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001234
1235 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001236 return 1;
1237
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001238 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001240 if (bg)
1241 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1242 else
1243 ret = 1;
1244 jbd_unlock_bh_state(bg_bh);
1245
1246 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001247}
1248
1249static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1250 struct buffer_head *bg_bh,
1251 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001252 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001253 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001254{
1255 void *bitmap;
1256 u16 best_offset, best_size;
1257 int offset, start, found, status = 0;
1258 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1259
Joel Becker42035302008-11-13 14:49:15 -08001260 /* Callers got this descriptor from
1261 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1262 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001263
1264 found = start = best_offset = best_size = 0;
1265 bitmap = bg->bg_bitmap;
1266
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001267 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1268 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 break;
1270
1271 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1272 /* We found a zero, but we can't use it as it
1273 * hasn't been put to disk yet! */
1274 found = 0;
1275 start = offset + 1;
1276 } else if (offset == start) {
1277 /* we found a zero */
1278 found++;
1279 /* move start to the next bit to test */
1280 start++;
1281 } else {
1282 /* got a zero after some ones */
1283 found = 1;
1284 start = offset + 1;
1285 }
1286 if (found > best_size) {
1287 best_size = found;
1288 best_offset = start - found;
1289 }
1290 /* we got everything we needed */
1291 if (found == bits_wanted) {
1292 /* mlog(0, "Found it all!\n"); */
1293 break;
1294 }
1295 }
1296
Joel Becker7d1fe092010-04-13 14:30:19 +08001297 if (best_size) {
1298 res->sr_bit_offset = best_offset;
1299 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 } else {
1301 status = -ENOSPC;
1302 /* No error log here -- see the comment above
1303 * ocfs2_test_bg_bit_allocatable */
1304 }
1305
1306 return status;
1307}
1308
Mark Fasheh1fabe142006-10-09 18:11:45 -07001309static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 struct inode *alloc_inode,
1311 struct ocfs2_group_desc *bg,
1312 struct buffer_head *group_bh,
1313 unsigned int bit_off,
1314 unsigned int num_bits)
1315{
1316 int status;
1317 void *bitmap = bg->bg_bitmap;
1318 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1319
1320 mlog_entry_void();
1321
Joel Becker42035302008-11-13 14:49:15 -08001322 /* All callers get the descriptor via
1323 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1324 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1326
1327 mlog(0, "block_group_set_bits: off = %u, num = %u\n", bit_off,
1328 num_bits);
1329
1330 if (ocfs2_is_cluster_bitmap(alloc_inode))
1331 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1332
Joel Becker13723d02008-10-17 19:25:01 -07001333 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001334 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001335 group_bh,
1336 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001337 if (status < 0) {
1338 mlog_errno(status);
1339 goto bail;
1340 }
1341
1342 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 while(num_bits--)
1344 ocfs2_set_bit(bit_off++, bitmap);
1345
Joel Beckerec20cec2010-03-19 14:13:52 -07001346 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001347
1348bail:
1349 mlog_exit(status);
1350 return status;
1351}
1352
1353/* find the one with the most empty bits */
1354static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1355{
1356 u16 curr, best;
1357
1358 BUG_ON(!cl->cl_next_free_rec);
1359
1360 best = curr = 0;
1361 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1362 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1363 le32_to_cpu(cl->cl_recs[best].c_free))
1364 best = curr;
1365 curr++;
1366 }
1367
1368 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1369 return best;
1370}
1371
Mark Fasheh1fabe142006-10-09 18:11:45 -07001372static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001373 struct inode *alloc_inode,
1374 struct buffer_head *fe_bh,
1375 struct buffer_head *bg_bh,
1376 struct buffer_head *prev_bg_bh,
1377 u16 chain)
1378{
1379 int status;
1380 /* there is a really tiny chance the journal calls could fail,
1381 * but we wouldn't want inconsistent blocks in *any* case. */
1382 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1383 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1384 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1385 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1386
Joel Becker42035302008-11-13 14:49:15 -08001387 /* The caller got these descriptors from
1388 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1389 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1390 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001391
Mark Fashehb06970532006-03-03 10:24:33 -08001392 mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu\n",
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001393 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1394 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1395 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001396
1397 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1398 bg_ptr = le64_to_cpu(bg->bg_next_group);
1399 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1400
Joel Becker0cf2f762009-02-12 16:41:25 -08001401 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1402 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001403 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001404 if (status < 0) {
1405 mlog_errno(status);
1406 goto out_rollback;
1407 }
1408
1409 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001410 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001411
Joel Becker0cf2f762009-02-12 16:41:25 -08001412 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1413 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001414 if (status < 0) {
1415 mlog_errno(status);
1416 goto out_rollback;
1417 }
1418
1419 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001420 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001421
Joel Becker0cf2f762009-02-12 16:41:25 -08001422 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1423 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 if (status < 0) {
1425 mlog_errno(status);
1426 goto out_rollback;
1427 }
1428
1429 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001430 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001431
Mark Fashehccd979b2005-12-15 14:31:24 -08001432out_rollback:
1433 if (status < 0) {
1434 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1435 bg->bg_next_group = cpu_to_le64(bg_ptr);
1436 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1437 }
Joel Becker42035302008-11-13 14:49:15 -08001438
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 mlog_exit(status);
1440 return status;
1441}
1442
1443static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1444 u32 wanted)
1445{
1446 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1447}
1448
1449/* return 0 on success, -ENOSPC to keep searching and any other < 0
1450 * value on error. */
1451static int ocfs2_cluster_group_search(struct inode *inode,
1452 struct buffer_head *group_bh,
1453 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001454 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001455 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001456{
1457 int search = -ENOSPC;
1458 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001459 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001460 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001461 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001462 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001463
1464 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1465
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001466 if (gd->bg_free_bits_count) {
1467 max_bits = le16_to_cpu(gd->bg_bits);
1468
1469 /* Tail groups in cluster bitmaps which aren't cpg
1470 * aligned are prone to partial extention by a failed
1471 * fs resize. If the file system resize never got to
1472 * update the dinode cluster count, then we don't want
1473 * to trust any clusters past it, regardless of what
1474 * the group descriptor says. */
1475 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1476 le64_to_cpu(gd->bg_blkno));
1477 if ((gd_cluster_off + max_bits) >
1478 OCFS2_I(inode)->ip_clusters) {
1479 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
1480 mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u\n",
1481 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1482 le16_to_cpu(gd->bg_bits),
1483 OCFS2_I(inode)->ip_clusters, max_bits);
1484 }
1485
Mark Fashehccd979b2005-12-15 14:31:24 -08001486 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1487 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001488 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001489 if (ret)
1490 return ret;
1491
Joel Becker1187c962008-09-03 20:03:39 -07001492 if (max_block) {
1493 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1494 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001495 res->sr_bit_offset +
1496 res->sr_bits);
Joel Becker1187c962008-09-03 20:03:39 -07001497 mlog(0, "Checking %llu against %llu\n",
1498 (unsigned long long)blkoff,
1499 (unsigned long long)max_block);
1500 if (blkoff > max_block)
1501 return -ENOSPC;
1502 }
1503
Mark Fashehccd979b2005-12-15 14:31:24 -08001504 /* ocfs2_block_group_find_clear_bits() might
1505 * return success, but we still want to return
1506 * -ENOSPC unless it found the minimum number
1507 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001508 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001509 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001510 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001511 /*
1512 * Don't show bits which we'll be returning
1513 * for allocation to the local alloc bitmap.
1514 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001515 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 }
1517 }
1518
1519 return search;
1520}
1521
1522static int ocfs2_block_group_search(struct inode *inode,
1523 struct buffer_head *group_bh,
1524 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001525 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001526 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001527{
1528 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001529 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001530 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1531
1532 BUG_ON(min_bits != 1);
1533 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1534
Joel Becker1187c962008-09-03 20:03:39 -07001535 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001536 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1537 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001538 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001539 res);
Joel Becker1187c962008-09-03 20:03:39 -07001540 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001541 blkoff = le64_to_cpu(bg->bg_blkno) +
1542 res->sr_bit_offset + res->sr_bits;
Joel Becker1187c962008-09-03 20:03:39 -07001543 mlog(0, "Checking %llu against %llu\n",
1544 (unsigned long long)blkoff,
1545 (unsigned long long)max_block);
1546 if (blkoff > max_block)
1547 ret = -ENOSPC;
1548 }
1549 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001550
1551 return ret;
1552}
1553
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001554static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001555 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001556 struct buffer_head *di_bh,
1557 u32 num_bits,
1558 u16 chain)
1559{
1560 int ret;
1561 u32 tmp_used;
1562 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1563 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1564
Joel Becker0cf2f762009-02-12 16:41:25 -08001565 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001566 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001567 if (ret < 0) {
1568 mlog_errno(ret);
1569 goto out;
1570 }
1571
1572 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1573 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1574 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001575 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001576
1577out:
1578 return ret;
1579}
1580
Joel Beckerba206632010-03-26 10:08:59 +08001581static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1582 struct ocfs2_extent_rec *rec,
1583 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001584{
1585 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1586 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1587 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1588
1589 if (res->sr_bit_offset < bitoff)
1590 return 0;
1591 if (res->sr_bit_offset >= (bitoff + bitcount))
1592 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001593 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1594 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001595 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1596 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1597 return 1;
1598}
1599
Joel Beckerba206632010-03-26 10:08:59 +08001600static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1601 struct ocfs2_group_desc *bg,
1602 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001603{
1604 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001605 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001606 struct ocfs2_extent_rec *rec;
1607 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1608 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1609
Joel Beckerba206632010-03-26 10:08:59 +08001610 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1611 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001612 return;
Joel Beckerba206632010-03-26 10:08:59 +08001613 }
Joel Becker13e434c2010-03-26 10:08:27 +08001614
Joel Beckerba206632010-03-26 10:08:59 +08001615 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001616 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Joel Beckerba206632010-03-26 10:08:59 +08001617 if (!ocfs2_supports_discontig_bh(OCFS2_SB(ac->ac_inode->i_sb)) ||
1618 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001619 return;
1620
1621 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1622 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001623 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1624 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001625 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001626 }
Joel Becker13e434c2010-03-26 10:08:27 +08001627 }
1628}
1629
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001630static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001631 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001632 u32 bits_wanted,
1633 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001634 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001635 u16 *bits_left)
1636{
1637 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001638 struct buffer_head *group_bh = NULL;
1639 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001640 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001641 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001642
Joel Becker7d1fe092010-04-13 14:30:19 +08001643 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1644 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001645 if (ret < 0) {
1646 mlog_errno(ret);
1647 return ret;
1648 }
1649
1650 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001651 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001652 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001653 if (ret < 0) {
1654 if (ret != -ENOSPC)
1655 mlog_errno(ret);
1656 goto out;
1657 }
1658
Joel Becker13e434c2010-03-26 10:08:27 +08001659 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001660 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001661
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001662 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001663 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001664 le16_to_cpu(gd->bg_chain));
1665 if (ret < 0) {
1666 mlog_errno(ret);
1667 goto out;
1668 }
1669
1670 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001671 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001672 if (ret < 0)
1673 mlog_errno(ret);
1674
1675 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1676
1677out:
1678 brelse(group_bh);
1679
1680 return ret;
1681}
1682
Mark Fashehccd979b2005-12-15 14:31:24 -08001683static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001684 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001685 u32 bits_wanted,
1686 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001687 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001688 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001689{
1690 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001691 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001692 u32 tmp_used;
1693 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001694 struct inode *alloc_inode = ac->ac_inode;
1695 struct buffer_head *group_bh = NULL;
1696 struct buffer_head *prev_group_bh = NULL;
1697 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1698 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1699 struct ocfs2_group_desc *bg;
1700
1701 chain = ac->ac_chain;
Mark Fashehb06970532006-03-03 10:24:33 -08001702 mlog(0, "trying to alloc %u bits from chain %u, inode %llu\n",
1703 bits_wanted, chain,
1704 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001705
Joel Becker68f64d42008-11-13 14:49:14 -08001706 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1707 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1708 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001709 if (status < 0) {
1710 mlog_errno(status);
1711 goto bail;
1712 }
1713 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001714
1715 status = -ENOSPC;
1716 /* for now, the chain search is a bit simplistic. We just use
1717 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001718 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1719 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001720 ac->ac_max_block,
1721 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001722 if (!bg->bg_next_group)
1723 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001724
1725 brelse(prev_group_bh);
1726 prev_group_bh = NULL;
1727
Mark Fashehccd979b2005-12-15 14:31:24 -08001728 next_group = le64_to_cpu(bg->bg_next_group);
1729 prev_group_bh = group_bh;
1730 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001731 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1732 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001733 if (status < 0) {
1734 mlog_errno(status);
1735 goto bail;
1736 }
1737 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 }
1739 if (status < 0) {
1740 if (status != -ENOSPC)
1741 mlog_errno(status);
1742 goto bail;
1743 }
1744
Mark Fashehb06970532006-03-03 10:24:33 -08001745 mlog(0, "alloc succeeds: we give %u bits from block group %llu\n",
Joel Becker7d1fe092010-04-13 14:30:19 +08001746 res->sr_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001747
Joel Becker7d1fe092010-04-13 14:30:19 +08001748 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001749
Joel Becker7d1fe092010-04-13 14:30:19 +08001750 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001751 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001752 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001753
Mark Fashehccd979b2005-12-15 14:31:24 -08001754
1755 /*
1756 * Keep track of previous block descriptor read. When
1757 * we find a target, if we have read more than X
1758 * number of descriptors, and the target is reasonably
1759 * empty, relink him to top of his chain.
1760 *
1761 * We've read 0 extra blocks and only send one more to
1762 * the transaction, yet the next guy to search has a
1763 * much easier time.
1764 *
1765 * Do this *after* figuring out how many bits we're taking out
1766 * of our target group.
1767 */
1768 if (ac->ac_allow_chain_relink &&
1769 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001770 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001771 status = ocfs2_relink_block_group(handle, alloc_inode,
1772 ac->ac_bh, group_bh,
1773 prev_group_bh, chain);
1774 if (status < 0) {
1775 mlog_errno(status);
1776 goto bail;
1777 }
1778 }
1779
1780 /* Ok, claim our bits now: set the info on dinode, chainlist
1781 * and then the group */
Joel Becker13723d02008-10-17 19:25:01 -07001782 status = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001783 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001784 ac->ac_bh,
1785 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001786 if (status < 0) {
1787 mlog_errno(status);
1788 goto bail;
1789 }
1790
1791 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
Joel Becker7d1fe092010-04-13 14:30:19 +08001792 fe->id1.bitmap1.i_used = cpu_to_le32(res->sr_bits + tmp_used);
1793 le32_add_cpu(&cl->cl_recs[chain].c_free, -res->sr_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001794 ocfs2_journal_dirty(handle, ac->ac_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001795
1796 status = ocfs2_block_group_set_bits(handle,
1797 alloc_inode,
1798 bg,
1799 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001800 res->sr_bit_offset,
1801 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001802 if (status < 0) {
1803 mlog_errno(status);
1804 goto bail;
1805 }
1806
Joel Becker7d1fe092010-04-13 14:30:19 +08001807 mlog(0, "Allocated %u bits from suballocator %llu\n", res->sr_bits,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07001808 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001809
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001810 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001811bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001812 brelse(group_bh);
1813 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001814
1815 mlog_exit(status);
1816 return status;
1817}
1818
1819/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001820static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001821 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001822 u32 bits_wanted,
1823 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001824 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001825{
1826 int status;
1827 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001828 u16 bits_left = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001829 struct ocfs2_chain_list *cl;
1830 struct ocfs2_dinode *fe;
1831
1832 mlog_entry_void();
1833
1834 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1835 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1836 BUG_ON(!ac->ac_bh);
1837
1838 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001839
1840 /* The bh was validated by the inode read during
1841 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1842 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1843
Mark Fashehccd979b2005-12-15 14:31:24 -08001844 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1845 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001846 ocfs2_error(ac->ac_inode->i_sb,
1847 "Chain allocator dinode %llu has %u used "
Mark Fashehb06970532006-03-03 10:24:33 -08001848 "bits but only %u total.",
1849 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 le32_to_cpu(fe->id1.bitmap1.i_used),
1851 le32_to_cpu(fe->id1.bitmap1.i_total));
1852 status = -EIO;
1853 goto bail;
1854 }
1855
Joel Becker7d1fe092010-04-13 14:30:19 +08001856 res->sr_bg_blkno = ac->ac_last_group;
1857 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001858 /* Attempt to short-circuit the usual search mechanism
1859 * by jumping straight to the most recently used
1860 * allocation group. This helps us mantain some
1861 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001862 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001863 min_bits, res, &bits_left);
1864 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001865 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001866 if (status < 0 && status != -ENOSPC) {
1867 mlog_errno(status);
1868 goto bail;
1869 }
1870 }
1871
Mark Fashehccd979b2005-12-15 14:31:24 -08001872 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1873
1874 victim = ocfs2_find_victim_chain(cl);
1875 ac->ac_chain = victim;
1876 ac->ac_allow_chain_relink = 1;
1877
Joel Becker7d1fe092010-04-13 14:30:19 +08001878 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1879 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001880 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001881 goto set_hint;
Mark Fashehccd979b2005-12-15 14:31:24 -08001882 if (status < 0 && status != -ENOSPC) {
1883 mlog_errno(status);
1884 goto bail;
1885 }
1886
1887 mlog(0, "Search of victim chain %u came up with nothing, "
1888 "trying all chains now.\n", victim);
1889
1890 /* If we didn't pick a good victim, then just default to
1891 * searching each chain in order. Don't allow chain relinking
1892 * because we only calculate enough journal credits for one
1893 * relink per alloc. */
1894 ac->ac_allow_chain_relink = 0;
1895 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1896 if (i == victim)
1897 continue;
1898 if (!cl->cl_recs[i].c_free)
1899 continue;
1900
1901 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001902 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001903 res, &bits_left);
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 if (!status)
1905 break;
1906 if (status < 0 && status != -ENOSPC) {
1907 mlog_errno(status);
1908 goto bail;
1909 }
1910 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001911
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001912set_hint:
1913 if (status != -ENOSPC) {
1914 /* If the next search of this group is not likely to
1915 * yield a suitable extent, then we reset the last
1916 * group hint so as to not waste a disk read */
1917 if (bits_left < min_bits)
1918 ac->ac_last_group = 0;
1919 else
Joel Becker7d1fe092010-04-13 14:30:19 +08001920 ac->ac_last_group = res->sr_bg_blkno;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001921 }
1922
1923bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08001924 mlog_exit(status);
1925 return status;
1926}
1927
Joel Becker1ed9b772010-05-06 13:59:06 +08001928int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001929 struct ocfs2_alloc_context *ac,
1930 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001931 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001932 u16 *suballoc_bit_start,
1933 unsigned int *num_bits,
1934 u64 *blkno_start)
1935{
1936 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001937 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001938
1939 BUG_ON(!ac);
1940 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
1941 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08001942
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001943 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07001944 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001945 bits_wanted,
1946 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08001947 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 if (status < 0) {
1949 mlog_errno(status);
1950 goto bail;
1951 }
Joel Becker1ed9b772010-05-06 13:59:06 +08001952 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08001953
Joel Becker2b6cb572010-03-26 10:09:15 +08001954 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001955 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08001956 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08001957 ac->ac_bits_given += res.sr_bits;
1958 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08001959 status = 0;
1960bail:
1961 mlog_exit(status);
1962 return status;
1963}
1964
Tao Ma13821152009-02-25 00:53:23 +08001965static void ocfs2_init_inode_ac_group(struct inode *dir,
1966 struct buffer_head *parent_fe_bh,
1967 struct ocfs2_alloc_context *ac)
1968{
1969 struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1970 /*
1971 * Try to allocate inodes from some specific group.
1972 *
1973 * If the parent dir has recorded the last group used in allocation,
1974 * cool, use it. Otherwise if we try to allocate new inode from the
1975 * same slot the parent dir belongs to, use the same chunk.
1976 *
1977 * We are very careful here to avoid the mistake of setting
1978 * ac_last_group to a group descriptor from a different (unlocked) slot.
1979 */
1980 if (OCFS2_I(dir)->ip_last_used_group &&
1981 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
1982 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
1983 else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
1984 ac->ac_last_group = ocfs2_which_suballoc_group(
1985 le64_to_cpu(fe->i_blkno),
1986 le16_to_cpu(fe->i_suballoc_bit));
1987}
1988
1989static inline void ocfs2_save_inode_ac_group(struct inode *dir,
1990 struct ocfs2_alloc_context *ac)
1991{
1992 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
1993 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
1994}
1995
Joel Becker1ed9b772010-05-06 13:59:06 +08001996int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08001997 struct inode *dir,
1998 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001999 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002000 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002001 u16 *suballoc_bit,
2002 u64 *fe_blkno)
2003{
2004 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002005 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002006
2007 mlog_entry_void();
2008
2009 BUG_ON(!ac);
2010 BUG_ON(ac->ac_bits_given != 0);
2011 BUG_ON(ac->ac_bits_wanted != 1);
2012 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002013
Tao Ma13821152009-02-25 00:53:23 +08002014 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2015
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002016 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002017 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002018 1,
2019 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002020 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002021 if (status < 0) {
2022 mlog_errno(status);
2023 goto bail;
2024 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002025 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002026
Joel Becker7d1fe092010-04-13 14:30:19 +08002027 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002028
Joel Becker2b6cb572010-03-26 10:09:15 +08002029 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002030 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002031 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002032 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002033 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002034 status = 0;
2035bail:
2036 mlog_exit(status);
2037 return status;
2038}
2039
2040/* translate a group desc. blkno and it's bitmap offset into
2041 * disk cluster offset. */
2042static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2043 u64 bg_blkno,
2044 u16 bg_bit_off)
2045{
2046 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2047 u32 cluster = 0;
2048
2049 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2050
2051 if (bg_blkno != osb->first_cluster_group_blkno)
2052 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2053 cluster += (u32) bg_bit_off;
2054 return cluster;
2055}
2056
2057/* given a cluster offset, calculate which block group it belongs to
2058 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002059u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002060{
2061 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2062 u32 group_no;
2063
2064 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2065
2066 group_no = cluster / osb->bitmap_cpg;
2067 if (!group_no)
2068 return osb->first_cluster_group_blkno;
2069 return ocfs2_clusters_to_blocks(inode->i_sb,
2070 group_no * osb->bitmap_cpg);
2071}
2072
2073/* given the block number of a cluster start, calculate which cluster
2074 * group and descriptor bitmap offset that corresponds to. */
2075static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2076 u64 data_blkno,
2077 u64 *bg_blkno,
2078 u16 *bg_bit_off)
2079{
2080 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2081 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2082
2083 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2084
2085 *bg_blkno = ocfs2_which_cluster_group(inode,
2086 data_cluster);
2087
2088 if (*bg_blkno == osb->first_cluster_group_blkno)
2089 *bg_bit_off = (u16) data_cluster;
2090 else
2091 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2092 data_blkno - *bg_blkno);
2093}
2094
2095/*
2096 * min_bits - minimum contiguous chunk from this total allocation we
2097 * can handle. set to what we asked for originally for a full
2098 * contig. allocation, set to '1' to indicate we can deal with extents
2099 * of any size.
2100 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002101int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002102 struct ocfs2_alloc_context *ac,
2103 u32 min_clusters,
2104 u32 max_clusters,
2105 u32 *cluster_start,
2106 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002107{
2108 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002109 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002110 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002111 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002112
2113 mlog_entry_void();
2114
Mark Fashehccd979b2005-12-15 14:31:24 -08002115 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2116
2117 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2118 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002119
2120 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002121 WARN_ON(min_clusters > 1);
2122
Mark Fashehccd979b2005-12-15 14:31:24 -08002123 status = ocfs2_claim_local_alloc_bits(osb,
2124 handle,
2125 ac,
2126 bits_wanted,
2127 cluster_start,
2128 num_clusters);
2129 if (!status)
2130 atomic_inc(&osb->alloc_stats.local_data);
2131 } else {
2132 if (min_clusters > (osb->bitmap_cpg - 1)) {
2133 /* The only paths asking for contiguousness
2134 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002135 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2136 "group bitmap size %u!\n", min_clusters,
2137 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002138 status = -ENOSPC;
2139 goto bail;
2140 }
2141 /* clamp the current request down to a realistic size. */
2142 if (bits_wanted > (osb->bitmap_cpg - 1))
2143 bits_wanted = osb->bitmap_cpg - 1;
2144
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002145 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002146 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002147 bits_wanted,
2148 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002149 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002150 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002151 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002152 *cluster_start =
2153 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002154 res.sr_bg_blkno,
2155 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002156 atomic_inc(&osb->alloc_stats.bitmap_data);
2157 }
2158 }
2159 if (status < 0) {
2160 if (status != -ENOSPC)
2161 mlog_errno(status);
2162 goto bail;
2163 }
2164
Joel Becker7d1fe092010-04-13 14:30:19 +08002165 ac->ac_bits_given += res.sr_bits;
2166 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002167
2168bail:
2169 mlog_exit(status);
2170 return status;
2171}
2172
Joel Becker1ed9b772010-05-06 13:59:06 +08002173int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002174 struct ocfs2_alloc_context *ac,
2175 u32 min_clusters,
2176 u32 *cluster_start,
2177 u32 *num_clusters)
2178{
2179 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2180
Joel Becker1ed9b772010-05-06 13:59:06 +08002181 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002182 bits_wanted, cluster_start, num_clusters);
2183}
2184
Mark Fashehb4414ee2010-03-11 18:31:09 -08002185static int ocfs2_block_group_clear_bits(handle_t *handle,
2186 struct inode *alloc_inode,
2187 struct ocfs2_group_desc *bg,
2188 struct buffer_head *group_bh,
2189 unsigned int bit_off,
2190 unsigned int num_bits,
2191 void (*undo_fn)(unsigned int bit,
2192 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002193{
2194 int status;
2195 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002196 struct ocfs2_group_desc *undo_bg = NULL;
2197
2198 mlog_entry_void();
2199
Joel Becker42035302008-11-13 14:49:15 -08002200 /* The caller got this descriptor from
2201 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2202 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002203
2204 mlog(0, "off = %u, num = %u\n", bit_off, num_bits);
2205
Mark Fashehb4414ee2010-03-11 18:31:09 -08002206 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002207 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002208 group_bh,
2209 undo_fn ?
2210 OCFS2_JOURNAL_ACCESS_UNDO :
2211 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002212 if (status < 0) {
2213 mlog_errno(status);
2214 goto bail;
2215 }
2216
Mark Fashehb4414ee2010-03-11 18:31:09 -08002217 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002218 jbd_lock_bh_state(group_bh);
2219 undo_bg = (struct ocfs2_group_desc *)
2220 bh2jh(group_bh)->b_committed_data;
2221 BUG_ON(!undo_bg);
2222 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002223
2224 tmp = num_bits;
2225 while(tmp--) {
2226 ocfs2_clear_bit((bit_off + tmp),
2227 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002228 if (undo_fn)
2229 undo_fn(bit_off + tmp,
2230 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 }
2232 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
2233
Mark Fashehb4414ee2010-03-11 18:31:09 -08002234 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002235 jbd_unlock_bh_state(group_bh);
2236
Joel Beckerec20cec2010-03-19 14:13:52 -07002237 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002238bail:
2239 return status;
2240}
2241
2242/*
2243 * expects the suballoc inode to already be locked.
2244 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002245static int _ocfs2_free_suballoc_bits(handle_t *handle,
2246 struct inode *alloc_inode,
2247 struct buffer_head *alloc_bh,
2248 unsigned int start_bit,
2249 u64 bg_blkno,
2250 unsigned int count,
2251 void (*undo_fn)(unsigned int bit,
2252 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002253{
2254 int status = 0;
2255 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002256 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2257 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2258 struct buffer_head *group_bh = NULL;
2259 struct ocfs2_group_desc *group;
2260
2261 mlog_entry_void();
2262
Joel Becker10995aa2008-11-13 14:49:12 -08002263 /* The alloc_bh comes from ocfs2_free_dinode() or
2264 * ocfs2_free_clusters(). The callers have all locked the
2265 * allocator and gotten alloc_bh from the lock call. This
2266 * validates the dinode buffer. Any corruption that has happended
2267 * is a code bug. */
2268 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002269 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2270
Mark Fashehb06970532006-03-03 10:24:33 -08002271 mlog(0, "%llu: freeing %u bits from group %llu, starting at %u\n",
2272 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
2273 (unsigned long long)bg_blkno, start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002274
Joel Becker68f64d42008-11-13 14:49:14 -08002275 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2276 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002277 if (status < 0) {
2278 mlog_errno(status);
2279 goto bail;
2280 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002281 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002282
Mark Fashehccd979b2005-12-15 14:31:24 -08002283 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2284
2285 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2286 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002287 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002288 if (status < 0) {
2289 mlog_errno(status);
2290 goto bail;
2291 }
2292
Joel Becker0cf2f762009-02-12 16:41:25 -08002293 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2294 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002295 if (status < 0) {
2296 mlog_errno(status);
2297 goto bail;
2298 }
2299
2300 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2301 count);
2302 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2303 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002304 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002305
2306bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002307 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002308
2309 mlog_exit(status);
2310 return status;
2311}
2312
Mark Fashehb4414ee2010-03-11 18:31:09 -08002313int ocfs2_free_suballoc_bits(handle_t *handle,
2314 struct inode *alloc_inode,
2315 struct buffer_head *alloc_bh,
2316 unsigned int start_bit,
2317 u64 bg_blkno,
2318 unsigned int count)
2319{
2320 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2321 start_bit, bg_blkno, count, NULL);
2322}
2323
Mark Fasheh1fabe142006-10-09 18:11:45 -07002324int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002325 struct inode *inode_alloc_inode,
2326 struct buffer_head *inode_alloc_bh,
2327 struct ocfs2_dinode *di)
2328{
2329 u64 blk = le64_to_cpu(di->i_blkno);
2330 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2331 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2332
2333 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2334 inode_alloc_bh, bit, bg_blkno, 1);
2335}
2336
Mark Fashehb4414ee2010-03-11 18:31:09 -08002337static int _ocfs2_free_clusters(handle_t *handle,
2338 struct inode *bitmap_inode,
2339 struct buffer_head *bitmap_bh,
2340 u64 start_blk,
2341 unsigned int num_clusters,
2342 void (*undo_fn)(unsigned int bit,
2343 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002344{
2345 int status;
2346 u16 bg_start_bit;
2347 u64 bg_blkno;
2348 struct ocfs2_dinode *fe;
2349
2350 /* You can't ever have a contiguous set of clusters
2351 * bigger than a block group bitmap so we never have to worry
2352 * about looping on them. */
2353
2354 mlog_entry_void();
2355
2356 /* This is expensive. We can safely remove once this stuff has
2357 * gotten tested really well. */
2358 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2359
2360 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2361
2362 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2363 &bg_start_bit);
2364
Mark Fashehb06970532006-03-03 10:24:33 -08002365 mlog(0, "want to free %u clusters starting at block %llu\n",
2366 num_clusters, (unsigned long long)start_blk);
2367 mlog(0, "bg_blkno = %llu, bg_start_bit = %u\n",
2368 (unsigned long long)bg_blkno, bg_start_bit);
Mark Fashehccd979b2005-12-15 14:31:24 -08002369
Mark Fashehb4414ee2010-03-11 18:31:09 -08002370 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2371 bg_start_bit, bg_blkno,
2372 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002373 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002374 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002375 goto out;
2376 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002377
Mark Fasheh9c7af402008-07-28 18:02:53 -07002378 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2379 num_clusters);
2380
2381out:
Mark Fashehccd979b2005-12-15 14:31:24 -08002382 mlog_exit(status);
2383 return status;
2384}
2385
Mark Fashehb4414ee2010-03-11 18:31:09 -08002386int ocfs2_free_clusters(handle_t *handle,
2387 struct inode *bitmap_inode,
2388 struct buffer_head *bitmap_bh,
2389 u64 start_blk,
2390 unsigned int num_clusters)
2391{
2392 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2393 start_blk, num_clusters,
2394 _ocfs2_set_bit);
2395}
2396
2397/*
2398 * Give never-used clusters back to the global bitmap. We don't need
2399 * to protect these bits in the undo buffer.
2400 */
2401int ocfs2_release_clusters(handle_t *handle,
2402 struct inode *bitmap_inode,
2403 struct buffer_head *bitmap_bh,
2404 u64 start_blk,
2405 unsigned int num_clusters)
2406{
2407 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2408 start_blk, num_clusters,
2409 _ocfs2_clear_bit);
2410}
2411
Mark Fashehccd979b2005-12-15 14:31:24 -08002412static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2413{
2414 printk("Block Group:\n");
2415 printk("bg_signature: %s\n", bg->bg_signature);
2416 printk("bg_size: %u\n", bg->bg_size);
2417 printk("bg_bits: %u\n", bg->bg_bits);
2418 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2419 printk("bg_chain: %u\n", bg->bg_chain);
2420 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002421 printk("bg_next_group: %llu\n",
2422 (unsigned long long)bg->bg_next_group);
2423 printk("bg_parent_dinode: %llu\n",
2424 (unsigned long long)bg->bg_parent_dinode);
2425 printk("bg_blkno: %llu\n",
2426 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002427}
2428
2429static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2430{
2431 int i;
2432
Mark Fashehb06970532006-03-03 10:24:33 -08002433 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002434 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002435 printk("i_size: %llu\n",
2436 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002437 printk("i_clusters: %u\n", fe->i_clusters);
2438 printk("i_generation: %u\n",
2439 le32_to_cpu(fe->i_generation));
2440 printk("id1.bitmap1.i_used: %u\n",
2441 le32_to_cpu(fe->id1.bitmap1.i_used));
2442 printk("id1.bitmap1.i_total: %u\n",
2443 le32_to_cpu(fe->id1.bitmap1.i_total));
2444 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2445 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2446 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2447 printk("id2.i_chain.cl_next_free_rec: %u\n",
2448 fe->id2.i_chain.cl_next_free_rec);
2449 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2450 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2451 fe->id2.i_chain.cl_recs[i].c_free);
2452 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2453 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002454 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2455 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002456 }
2457}
Tao Mae7d4cb62008-08-18 17:38:44 +08002458
2459/*
2460 * For a given allocation, determine which allocators will need to be
2461 * accessed, and lock them, reserving the appropriate number of bits.
2462 *
2463 * Sparse file systems call this from ocfs2_write_begin_nolock()
2464 * and ocfs2_allocate_unwritten_extents().
2465 *
2466 * File systems which don't support holes call this from
2467 * ocfs2_extend_allocation().
2468 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002469int ocfs2_lock_allocators(struct inode *inode,
2470 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002471 u32 clusters_to_add, u32 extents_to_split,
2472 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002473 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002474{
2475 int ret = 0, num_free_extents;
2476 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2477 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2478
2479 *meta_ac = NULL;
2480 if (data_ac)
2481 *data_ac = NULL;
2482
2483 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2484
Joel Becker3d03a302009-02-12 17:49:26 -08002485 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002486 if (num_free_extents < 0) {
2487 ret = num_free_extents;
2488 mlog_errno(ret);
2489 goto out;
2490 }
2491
2492 /*
2493 * Sparse allocation file systems need to be more conservative
2494 * with reserving room for expansion - the actual allocation
2495 * happens while we've got a journal handle open so re-taking
2496 * a cluster lock (because we ran out of room for another
2497 * extent) will violate ordering rules.
2498 *
2499 * Most of the time we'll only be seeing this 1 cluster at a time
2500 * anyway.
2501 *
2502 * Always lock for any unwritten extents - we might want to
2503 * add blocks during a split.
2504 */
2505 if (!num_free_extents ||
2506 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002507 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002508 if (ret < 0) {
2509 if (ret != -ENOSPC)
2510 mlog_errno(ret);
2511 goto out;
2512 }
2513 }
2514
2515 if (clusters_to_add == 0)
2516 goto out;
2517
2518 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2519 if (ret < 0) {
2520 if (ret != -ENOSPC)
2521 mlog_errno(ret);
2522 goto out;
2523 }
2524
2525out:
2526 if (ret) {
2527 if (*meta_ac) {
2528 ocfs2_free_alloc_context(*meta_ac);
2529 *meta_ac = NULL;
2530 }
2531
2532 /*
2533 * We cannot have an error and a non null *data_ac.
2534 */
2535 }
2536
2537 return ret;
2538}
wengang wang6ca497a2009-03-06 21:29:10 +08002539
2540/*
2541 * Read the inode specified by blkno to get suballoc_slot and
2542 * suballoc_bit.
2543 */
2544static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
2545 u16 *suballoc_slot, u16 *suballoc_bit)
2546{
2547 int status;
2548 struct buffer_head *inode_bh = NULL;
2549 struct ocfs2_dinode *inode_fe;
2550
Joel Becker5b09b502009-04-21 16:31:20 -07002551 mlog_entry("blkno: %llu\n", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002552
2553 /* dirty read disk */
2554 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2555 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002556 mlog(ML_ERROR, "read block %llu failed %d\n",
2557 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002558 goto bail;
2559 }
2560
2561 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2562 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002563 mlog(ML_ERROR, "invalid inode %llu requested\n",
2564 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002565 status = -EINVAL;
2566 goto bail;
2567 }
2568
Tao Ma0fba8132009-03-19 05:08:43 +08002569 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002570 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2571 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002572 (unsigned long long)blkno,
2573 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002574 status = -EINVAL;
2575 goto bail;
2576 }
2577
2578 if (suballoc_slot)
2579 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2580 if (suballoc_bit)
2581 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
2582
2583bail:
2584 brelse(inode_bh);
2585
2586 mlog_exit(status);
2587 return status;
2588}
2589
2590/*
2591 * test whether bit is SET in allocator bitmap or not. on success, 0
2592 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2593 * is returned and *res is meaningless. Call this after you have
2594 * cluster locked against suballoc, or you may get a result based on
2595 * non-up2date contents
2596 */
2597static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2598 struct inode *suballoc,
2599 struct buffer_head *alloc_bh, u64 blkno,
2600 u16 bit, int *res)
2601{
2602 struct ocfs2_dinode *alloc_fe;
2603 struct ocfs2_group_desc *group;
2604 struct buffer_head *group_bh = NULL;
2605 u64 bg_blkno;
2606 int status;
2607
Joel Becker5b09b502009-04-21 16:31:20 -07002608 mlog_entry("blkno: %llu bit: %u\n", (unsigned long long)blkno,
2609 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002610
2611 alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
2612 if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
2613 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2614 (unsigned int)bit,
2615 ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
2616 status = -EINVAL;
2617 goto bail;
2618 }
2619
2620 bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
2621 status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
2622 &group_bh);
2623 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002624 mlog(ML_ERROR, "read group %llu failed %d\n",
2625 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002626 goto bail;
2627 }
2628
2629 group = (struct ocfs2_group_desc *) group_bh->b_data;
2630 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2631
2632bail:
2633 brelse(group_bh);
2634
2635 mlog_exit(status);
2636 return status;
2637}
2638
2639/*
2640 * Test if the bit representing this inode (blkno) is set in the
2641 * suballocator.
2642 *
2643 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2644 *
2645 * In the event of failure, a negative value is returned and *res is
2646 * meaningless.
2647 *
2648 * Callers must make sure to hold nfs_sync_lock to prevent
2649 * ocfs2_delete_inode() on another node from accessing the same
2650 * suballocator concurrently.
2651 */
2652int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2653{
2654 int status;
2655 u16 suballoc_bit = 0, suballoc_slot = 0;
2656 struct inode *inode_alloc_inode;
2657 struct buffer_head *alloc_bh = NULL;
2658
Joel Becker5b09b502009-04-21 16:31:20 -07002659 mlog_entry("blkno: %llu", (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002660
2661 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
2662 &suballoc_bit);
2663 if (status < 0) {
2664 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2665 goto bail;
2666 }
2667
2668 inode_alloc_inode =
2669 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2670 suballoc_slot);
2671 if (!inode_alloc_inode) {
2672 /* the error code could be inaccurate, but we are not able to
2673 * get the correct one. */
2674 status = -EINVAL;
2675 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2676 (u32)suballoc_slot);
2677 goto bail;
2678 }
2679
2680 mutex_lock(&inode_alloc_inode->i_mutex);
2681 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2682 if (status < 0) {
2683 mutex_unlock(&inode_alloc_inode->i_mutex);
2684 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2685 (u32)suballoc_slot, status);
2686 goto bail;
2687 }
2688
2689 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
2690 blkno, suballoc_bit, res);
2691 if (status < 0)
2692 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2693
2694 ocfs2_inode_unlock(inode_alloc_inode, 0);
2695 mutex_unlock(&inode_alloc_inode->i_mutex);
2696
2697 iput(inode_alloc_inode);
2698 brelse(alloc_bh);
2699bail:
2700 mlog_exit(status);
2701 return status;
2702}