blob: 94342bd9800dba14930cd419f32a67a8da0a6edf [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"
Tao Ma2f73e132011-02-22 08:22:33 +080047#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080048
49#include "buffer_head_io.h"
50
Tao Maffda89a2008-03-03 17:12:09 +080051#define NOT_ALLOC_NEW_GROUP 0
Tao Ma60ca81e2009-02-25 00:53:24 +080052#define ALLOC_NEW_GROUP 0x1
53#define ALLOC_GROUPS_FROM_GLOBAL 0x2
Tao Maffda89a2008-03-03 17:12:09 +080054
Tiger Yangb89c5422010-01-25 14:11:06 +080055#define OCFS2_MAX_TO_STEAL 1024
Tao Ma4d0ddb22008-03-05 16:11:46 +080056
Joel Becker7d1fe092010-04-13 14:30:19 +080057struct ocfs2_suballoc_result {
Joel Becker2b6cb572010-03-26 10:09:15 +080058 u64 sr_bg_blkno; /* The bg we allocated from. Set
59 to 0 when a block group is
60 contiguous. */
Mark Fashehe49e2762010-08-13 15:15:17 -070061 u64 sr_bg_stable_blkno; /*
62 * Doesn't change, always
63 * set to target block
64 * group descriptor
65 * block.
66 */
Joel Beckerba206632010-03-26 10:08:59 +080067 u64 sr_blkno; /* The first allocated block */
Joel Becker7d1fe092010-04-13 14:30:19 +080068 unsigned int sr_bit_offset; /* The bit in the bg */
69 unsigned int sr_bits; /* How many bits we claimed */
70};
71
Mark Fashehb2b6ebf2010-08-26 13:06:50 -070072static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
73{
74 if (res->sr_blkno == 0)
75 return 0;
76
77 if (res->sr_bg_blkno)
78 return res->sr_bg_blkno;
79
80 return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
81}
82
Mark Fashehccd979b2005-12-15 14:31:24 -080083static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
84static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
85static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
Mark Fasheh1fabe142006-10-09 18:11:45 -070086static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -080087 struct inode *alloc_inode,
88 struct buffer_head *bg_bh,
89 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +080090 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -080091 u16 my_chain,
92 struct ocfs2_chain_list *cl);
93static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
94 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -070095 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +080096 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +080097 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +080098 int flags);
Mark Fashehccd979b2005-12-15 14:31:24 -080099
Mark Fashehccd979b2005-12-15 14:31:24 -0800100static int ocfs2_cluster_group_search(struct inode *inode,
101 struct buffer_head *group_bh,
102 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -0700103 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +0800104 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800105static int ocfs2_block_group_search(struct inode *inode,
106 struct buffer_head *group_bh,
107 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -0700108 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +0800109 struct ocfs2_suballoc_result *res);
Joel Beckeraa8f8e92010-03-26 10:08:07 +0800110static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700111 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800112 u32 bits_wanted,
113 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +0800114 struct ocfs2_suballoc_result *res);
Mark Fashehccd979b2005-12-15 14:31:24 -0800115static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
116 int nr);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700117static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800118 struct inode *alloc_inode,
119 struct ocfs2_group_desc *bg,
120 struct buffer_head *group_bh,
121 unsigned int bit_off,
122 unsigned int num_bits);
Mark Fasheh1fabe142006-10-09 18:11:45 -0700123static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800124 struct inode *alloc_inode,
125 struct buffer_head *fe_bh,
126 struct buffer_head *bg_bh,
127 struct buffer_head *prev_bg_bh,
128 u16 chain);
129static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
130 u32 wanted);
Mark Fashehccd979b2005-12-15 14:31:24 -0800131static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
132 u64 bg_blkno,
133 u16 bg_bit_off);
Mark Fashehccd979b2005-12-15 14:31:24 -0800134static inline void ocfs2_block_to_cluster_group(struct inode *inode,
135 u64 data_blkno,
136 u64 *bg_blkno,
137 u16 *bg_bit_off);
Joel Becker1187c962008-09-03 20:03:39 -0700138static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
139 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +0800140 int flags,
Joel Becker1187c962008-09-03 20:03:39 -0700141 struct ocfs2_alloc_context **ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800142
Mark Fasheh9c7af402008-07-28 18:02:53 -0700143void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800144{
Mark Fashehda5cbf22006-10-06 18:34:35 -0700145 struct inode *inode = ac->ac_inode;
146
147 if (inode) {
148 if (ac->ac_which != OCFS2_AC_USE_LOCAL)
Mark Fashehe63aecb62007-10-18 15:30:42 -0700149 ocfs2_inode_unlock(inode, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700150
151 mutex_unlock(&inode->i_mutex);
152
153 iput(inode);
Tao Ma4d0ddb22008-03-05 16:11:46 +0800154 ac->ac_inode = NULL;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700155 }
Mark Fasheha81cb882008-10-07 14:25:16 -0700156 brelse(ac->ac_bh);
157 ac->ac_bh = NULL;
Mark Fashehe3b4a972009-12-07 13:16:07 -0800158 ac->ac_resv = NULL;
Mark Fashehe49e2762010-08-13 15:15:17 -0700159 if (ac->ac_find_loc_priv) {
160 kfree(ac->ac_find_loc_priv);
161 ac->ac_find_loc_priv = NULL;
162 }
Tao Ma4d0ddb22008-03-05 16:11:46 +0800163}
164
165void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
166{
167 ocfs2_free_ac_resource(ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800168 kfree(ac);
169}
170
171static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
172{
173 return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
174}
175
Joel Becker57e3e792008-11-13 14:49:13 -0800176#define do_error(fmt, ...) \
177 do{ \
Tao Ma78c37eb2010-03-03 11:26:27 +0800178 if (resize) \
Joel Becker57e3e792008-11-13 14:49:13 -0800179 mlog(ML_ERROR, fmt "\n", ##__VA_ARGS__); \
180 else \
181 ocfs2_error(sb, fmt, ##__VA_ARGS__); \
182 } while (0)
183
Joel Becker970e4932008-11-13 14:49:19 -0800184static int ocfs2_validate_gd_self(struct super_block *sb,
185 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800186 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800187{
188 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
189
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700190 if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800191 do_error("Group descriptor #%llu has bad signature %.*s",
192 (unsigned long long)bh->b_blocknr, 7,
Joel Becker57e3e792008-11-13 14:49:13 -0800193 gd->bg_signature);
194 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700195 }
196
Joel Becker68f64d42008-11-13 14:49:14 -0800197 if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
198 do_error("Group descriptor #%llu has an invalid bg_blkno "
199 "of %llu",
200 (unsigned long long)bh->b_blocknr,
201 (unsigned long long)le64_to_cpu(gd->bg_blkno));
202 return -EINVAL;
203 }
204
205 if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
206 do_error("Group descriptor #%llu has an invalid "
207 "fs_generation of #%u",
208 (unsigned long long)bh->b_blocknr,
209 le32_to_cpu(gd->bg_generation));
210 return -EINVAL;
211 }
212
Joel Becker970e4932008-11-13 14:49:19 -0800213 if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
214 do_error("Group descriptor #%llu has bit count %u but "
215 "claims that %u are free",
216 (unsigned long long)bh->b_blocknr,
217 le16_to_cpu(gd->bg_bits),
218 le16_to_cpu(gd->bg_free_bits_count));
219 return -EINVAL;
220 }
221
222 if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
223 do_error("Group descriptor #%llu has bit count %u but "
224 "max bitmap bits of %u",
225 (unsigned long long)bh->b_blocknr,
226 le16_to_cpu(gd->bg_bits),
227 8 * le16_to_cpu(gd->bg_size));
228 return -EINVAL;
229 }
230
231 return 0;
232}
233
234static int ocfs2_validate_gd_parent(struct super_block *sb,
235 struct ocfs2_dinode *di,
236 struct buffer_head *bh,
Tao Ma78c37eb2010-03-03 11:26:27 +0800237 int resize)
Joel Becker970e4932008-11-13 14:49:19 -0800238{
239 unsigned int max_bits;
240 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
241
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700242 if (di->i_blkno != gd->bg_parent_dinode) {
Joel Becker68f64d42008-11-13 14:49:14 -0800243 do_error("Group descriptor #%llu has bad parent "
Joel Becker57e3e792008-11-13 14:49:13 -0800244 "pointer (%llu, expected %llu)",
Joel Becker68f64d42008-11-13 14:49:14 -0800245 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800246 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
247 (unsigned long long)le64_to_cpu(di->i_blkno));
248 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700249 }
250
251 max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
252 if (le16_to_cpu(gd->bg_bits) > max_bits) {
Joel Becker68f64d42008-11-13 14:49:14 -0800253 do_error("Group descriptor #%llu has bit count of %u",
254 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800255 le16_to_cpu(gd->bg_bits));
256 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700257 }
258
Tao Ma78c37eb2010-03-03 11:26:27 +0800259 /* In resize, we may meet the case bg_chain == cl_next_free_rec. */
260 if ((le16_to_cpu(gd->bg_chain) >
261 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
262 ((le16_to_cpu(gd->bg_chain) ==
263 le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
Joel Becker68f64d42008-11-13 14:49:14 -0800264 do_error("Group descriptor #%llu has bad chain %u",
265 (unsigned long long)bh->b_blocknr,
Joel Becker57e3e792008-11-13 14:49:13 -0800266 le16_to_cpu(gd->bg_chain));
267 return -EINVAL;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700268 }
269
Joel Becker970e4932008-11-13 14:49:19 -0800270 return 0;
271}
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700272
Joel Becker57e3e792008-11-13 14:49:13 -0800273#undef do_error
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700274
Joel Becker970e4932008-11-13 14:49:19 -0800275/*
276 * This version only prints errors. It does not fail the filesystem, and
277 * exists only for resize.
278 */
279int ocfs2_check_group_descriptor(struct super_block *sb,
280 struct ocfs2_dinode *di,
281 struct buffer_head *bh)
282{
283 int rc;
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700284 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
Joel Becker970e4932008-11-13 14:49:19 -0800285
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700286 BUG_ON(!buffer_uptodate(bh));
287
288 /*
289 * If the ecc fails, we return the error but otherwise
290 * leave the filesystem running. We know any error is
291 * local to this block.
292 */
293 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
Joel Becker13723d02008-10-17 19:25:01 -0700294 if (rc) {
295 mlog(ML_ERROR,
296 "Checksum failed for group descriptor %llu\n",
297 (unsigned long long)bh->b_blocknr);
298 } else
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700299 rc = ocfs2_validate_gd_self(sb, bh, 1);
Joel Becker970e4932008-11-13 14:49:19 -0800300 if (!rc)
301 rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
302
303 return rc;
304}
305
306static int ocfs2_validate_group_descriptor(struct super_block *sb,
307 struct buffer_head *bh)
308{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700309 int rc;
310 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
311
Tao Ma2f73e132011-02-22 08:22:33 +0800312 trace_ocfs2_validate_group_descriptor(
313 (unsigned long long)bh->b_blocknr);
Joel Becker970e4932008-11-13 14:49:19 -0800314
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700315 BUG_ON(!buffer_uptodate(bh));
316
317 /*
318 * If the ecc fails, we return the error but otherwise
319 * leave the filesystem running. We know any error is
320 * local to this block.
321 */
322 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
323 if (rc)
324 return rc;
325
326 /*
327 * Errors after here are fatal.
328 */
329
Joel Becker970e4932008-11-13 14:49:19 -0800330 return ocfs2_validate_gd_self(sb, bh, 0);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -0700331}
332
Joel Becker68f64d42008-11-13 14:49:14 -0800333int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
334 u64 gd_blkno, struct buffer_head **bh)
335{
336 int rc;
337 struct buffer_head *tmp = *bh;
338
Joel Becker8cb471e2009-02-10 20:00:41 -0800339 rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800340 ocfs2_validate_group_descriptor);
Joel Becker68f64d42008-11-13 14:49:14 -0800341 if (rc)
342 goto out;
343
Joel Becker970e4932008-11-13 14:49:19 -0800344 rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
Joel Becker68f64d42008-11-13 14:49:14 -0800345 if (rc) {
346 brelse(tmp);
347 goto out;
348 }
349
350 /* If ocfs2_read_block() got us a new bh, pass it up. */
351 if (!*bh)
352 *bh = tmp;
353
354out:
355 return rc;
356}
357
Joel Becker798db352010-04-13 14:26:32 +0800358static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
359 struct ocfs2_group_desc *bg,
360 struct ocfs2_chain_list *cl,
Tao Ma47dea422010-09-13 15:13:50 +0800361 u64 p_blkno, unsigned int clusters)
Joel Becker798db352010-04-13 14:26:32 +0800362{
363 struct ocfs2_extent_list *el = &bg->bg_list;
364 struct ocfs2_extent_rec *rec;
365
Tao Ma47119542010-04-22 14:09:15 +0800366 BUG_ON(!ocfs2_supports_discontig_bg(osb));
Joel Becker798db352010-04-13 14:26:32 +0800367 if (!el->l_next_free_rec)
368 el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
369 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
Tao Ma47119542010-04-22 14:09:15 +0800370 rec->e_blkno = cpu_to_le64(p_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800371 rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
372 le16_to_cpu(cl->cl_bpc));
Tao Ma47dea422010-09-13 15:13:50 +0800373 rec->e_leaf_clusters = cpu_to_le16(clusters);
Joel Becker798db352010-04-13 14:26:32 +0800374 le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
Tao Ma47119542010-04-22 14:09:15 +0800375 le16_add_cpu(&bg->bg_free_bits_count,
376 clusters * le16_to_cpu(cl->cl_bpc));
Joel Becker798db352010-04-13 14:26:32 +0800377 le16_add_cpu(&el->l_next_free_rec, 1);
378}
379
Mark Fasheh1fabe142006-10-09 18:11:45 -0700380static int ocfs2_block_group_fill(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800381 struct inode *alloc_inode,
382 struct buffer_head *bg_bh,
383 u64 group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800384 unsigned int group_clusters,
Mark Fashehccd979b2005-12-15 14:31:24 -0800385 u16 my_chain,
386 struct ocfs2_chain_list *cl)
387{
388 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800389 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800390 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
391 struct super_block * sb = alloc_inode->i_sb;
392
Mark Fashehccd979b2005-12-15 14:31:24 -0800393 if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -0800394 ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
395 "b_blocknr (%llu)",
396 (unsigned long long)group_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -0800397 (unsigned long long) bg_bh->b_blocknr);
398 status = -EIO;
399 goto bail;
400 }
401
Joel Becker13723d02008-10-17 19:25:01 -0700402 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800403 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700404 bg_bh,
405 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800406 if (status < 0) {
407 mlog_errno(status);
408 goto bail;
409 }
410
411 memset(bg, 0, sb->s_blocksize);
412 strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
413 bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
Tao Ma85718822010-04-13 14:38:06 +0800414 bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
415 osb->s_feature_incompat));
Mark Fashehccd979b2005-12-15 14:31:24 -0800416 bg->bg_chain = cpu_to_le16(my_chain);
417 bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
418 bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
419 bg->bg_blkno = cpu_to_le64(group_blkno);
Joel Becker798db352010-04-13 14:26:32 +0800420 if (group_clusters == le16_to_cpu(cl->cl_cpg))
421 bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
422 else
Tao Ma47119542010-04-22 14:09:15 +0800423 ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
Joel Becker798db352010-04-13 14:26:32 +0800424 group_clusters);
425
Mark Fashehccd979b2005-12-15 14:31:24 -0800426 /* set the 1st bit in the bitmap to account for the descriptor block */
427 ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
428 bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
429
Joel Beckerec20cec2010-03-19 14:13:52 -0700430 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800431
432 /* There is no need to zero out or otherwise initialize the
433 * other blocks in a group - All valid FS metadata in a block
434 * group stores the superblock fs_generation value at
435 * allocation time. */
436
437bail:
Tao Mac1e8d352011-03-07 16:43:21 +0800438 if (status)
439 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800440 return status;
441}
442
443static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
444{
445 u16 curr, best;
446
447 best = curr = 0;
448 while (curr < le16_to_cpu(cl->cl_count)) {
449 if (le32_to_cpu(cl->cl_recs[best].c_total) >
450 le32_to_cpu(cl->cl_recs[curr].c_total))
451 best = curr;
452 curr++;
453 }
454 return best;
455}
456
Joel Becker798db352010-04-13 14:26:32 +0800457static struct buffer_head *
458ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
459 struct inode *alloc_inode,
460 struct ocfs2_alloc_context *ac,
461 struct ocfs2_chain_list *cl)
462{
463 int status;
464 u32 bit_off, num_bits;
465 u64 bg_blkno;
466 struct buffer_head *bg_bh;
467 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
468
Joel Becker1ed9b772010-05-06 13:59:06 +0800469 status = ocfs2_claim_clusters(handle, ac,
Joel Becker798db352010-04-13 14:26:32 +0800470 le16_to_cpu(cl->cl_cpg), &bit_off,
471 &num_bits);
472 if (status < 0) {
473 if (status != -ENOSPC)
474 mlog_errno(status);
475 goto bail;
476 }
477
478 /* setup the group */
479 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Ma2f73e132011-02-22 08:22:33 +0800480 trace_ocfs2_block_group_alloc_contig(
481 (unsigned long long)bg_blkno, alloc_rec);
Joel Becker798db352010-04-13 14:26:32 +0800482
483 bg_bh = sb_getblk(osb->sb, bg_blkno);
484 if (!bg_bh) {
485 status = -EIO;
486 mlog_errno(status);
487 goto bail;
488 }
489 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
490
491 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
492 bg_blkno, num_bits, alloc_rec, cl);
493 if (status < 0) {
494 brelse(bg_bh);
495 mlog_errno(status);
496 }
497
498bail:
499 return status ? ERR_PTR(status) : bg_bh;
500}
501
502static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
503 handle_t *handle,
504 struct ocfs2_alloc_context *ac,
505 unsigned int min_bits,
506 u32 *bit_off, u32 *num_bits)
507{
Joel Becker18d3a982010-05-18 16:47:55 -0700508 int status = 0;
Joel Becker798db352010-04-13 14:26:32 +0800509
510 while (min_bits) {
Joel Becker1ed9b772010-05-06 13:59:06 +0800511 status = ocfs2_claim_clusters(handle, ac, min_bits,
Joel Becker798db352010-04-13 14:26:32 +0800512 bit_off, num_bits);
513 if (status != -ENOSPC)
514 break;
515
516 min_bits >>= 1;
517 }
518
519 return status;
520}
521
522static int ocfs2_block_group_grow_discontig(handle_t *handle,
523 struct inode *alloc_inode,
524 struct buffer_head *bg_bh,
525 struct ocfs2_alloc_context *ac,
526 struct ocfs2_chain_list *cl,
527 unsigned int min_bits)
528{
529 int status;
530 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
531 struct ocfs2_group_desc *bg =
532 (struct ocfs2_group_desc *)bg_bh->b_data;
Tao Ma47119542010-04-22 14:09:15 +0800533 unsigned int needed = le16_to_cpu(cl->cl_cpg) -
534 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800535 u32 p_cpos, clusters;
536 u64 p_blkno;
537 struct ocfs2_extent_list *el = &bg->bg_list;
538
539 status = ocfs2_journal_access_gd(handle,
540 INODE_CACHE(alloc_inode),
541 bg_bh,
542 OCFS2_JOURNAL_ACCESS_CREATE);
543 if (status < 0) {
544 mlog_errno(status);
545 goto bail;
546 }
547
548 while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
549 le16_to_cpu(el->l_count))) {
Joel Becker798db352010-04-13 14:26:32 +0800550 if (min_bits > needed)
551 min_bits = needed;
552 status = ocfs2_block_group_claim_bits(osb, handle, ac,
553 min_bits, &p_cpos,
554 &clusters);
555 if (status < 0) {
556 if (status != -ENOSPC)
557 mlog_errno(status);
558 goto bail;
559 }
560 p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
561 ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
562 clusters);
563
564 min_bits = clusters;
Tao Ma47119542010-04-22 14:09:15 +0800565 needed = le16_to_cpu(cl->cl_cpg) -
566 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
Joel Becker798db352010-04-13 14:26:32 +0800567 }
568
569 if (needed > 0) {
Tao Ma47119542010-04-22 14:09:15 +0800570 /*
571 * We have used up all the extent rec but can't fill up
572 * the cpg. So bail out.
573 */
574 status = -ENOSPC;
575 goto bail;
Joel Becker798db352010-04-13 14:26:32 +0800576 }
577
578 ocfs2_journal_dirty(handle, bg_bh);
579
580bail:
581 return status;
582}
583
Joel Becker8b06bc52010-03-26 10:09:29 +0800584static void ocfs2_bg_alloc_cleanup(handle_t *handle,
585 struct ocfs2_alloc_context *cluster_ac,
586 struct inode *alloc_inode,
587 struct buffer_head *bg_bh)
Joel Becker798db352010-04-13 14:26:32 +0800588{
Joel Becker8b06bc52010-03-26 10:09:29 +0800589 int i, ret;
Joel Becker798db352010-04-13 14:26:32 +0800590 struct ocfs2_group_desc *bg;
591 struct ocfs2_extent_list *el;
592 struct ocfs2_extent_rec *rec;
593
594 if (!bg_bh)
595 return;
596
597 bg = (struct ocfs2_group_desc *)bg_bh->b_data;
598 el = &bg->bg_list;
599 for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
600 rec = &el->l_recs[i];
Joel Becker8b06bc52010-03-26 10:09:29 +0800601 ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
602 cluster_ac->ac_bh,
603 le64_to_cpu(rec->e_blkno),
604 le32_to_cpu(rec->e_leaf_clusters));
605 if (ret)
606 mlog_errno(ret);
607 /* Try all the clusters to free */
Joel Becker798db352010-04-13 14:26:32 +0800608 }
609
610 ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
611 brelse(bg_bh);
612}
613
614static struct buffer_head *
615ocfs2_block_group_alloc_discontig(handle_t *handle,
616 struct inode *alloc_inode,
617 struct ocfs2_alloc_context *ac,
Joel Becker8b06bc52010-03-26 10:09:29 +0800618 struct ocfs2_chain_list *cl)
Joel Becker798db352010-04-13 14:26:32 +0800619{
620 int status;
621 u32 bit_off, num_bits;
622 u64 bg_blkno;
623 unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
624 struct buffer_head *bg_bh = NULL;
625 unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
626 struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
627
Tao Ma47119542010-04-22 14:09:15 +0800628 if (!ocfs2_supports_discontig_bg(osb)) {
Joel Becker798db352010-04-13 14:26:32 +0800629 status = -ENOSPC;
630 goto bail;
631 }
632
Joel Becker8b06bc52010-03-26 10:09:29 +0800633 status = ocfs2_extend_trans(handle,
634 ocfs2_calc_bg_discontig_credits(osb->sb));
635 if (status) {
636 mlog_errno(status);
637 goto bail;
638 }
639
Joel Becker95ec0ad2010-03-26 10:10:08 +0800640 /*
641 * We're going to be grabbing from multiple cluster groups.
642 * We don't have enough credits to relink them all, and the
643 * cluster groups will be staying in cache for the duration of
644 * this operation.
645 */
646 ac->ac_allow_chain_relink = 0;
647
Joel Becker798db352010-04-13 14:26:32 +0800648 /* Claim the first region */
649 status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
650 &bit_off, &num_bits);
651 if (status < 0) {
652 if (status != -ENOSPC)
653 mlog_errno(status);
654 goto bail;
655 }
656 min_bits = num_bits;
657
658 /* setup the group */
659 bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Ma2f73e132011-02-22 08:22:33 +0800660 trace_ocfs2_block_group_alloc_discontig(
661 (unsigned long long)bg_blkno, alloc_rec);
Joel Becker798db352010-04-13 14:26:32 +0800662
663 bg_bh = sb_getblk(osb->sb, bg_blkno);
664 if (!bg_bh) {
665 status = -EIO;
666 mlog_errno(status);
667 goto bail;
668 }
669 ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
670
671 status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
672 bg_blkno, num_bits, alloc_rec, cl);
673 if (status < 0) {
674 mlog_errno(status);
675 goto bail;
676 }
677
678 status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
679 bg_bh, ac, cl, min_bits);
680 if (status)
681 mlog_errno(status);
682
683bail:
684 if (status)
Joel Becker8b06bc52010-03-26 10:09:29 +0800685 ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
Joel Becker798db352010-04-13 14:26:32 +0800686 return status ? ERR_PTR(status) : bg_bh;
687}
688
Mark Fashehccd979b2005-12-15 14:31:24 -0800689/*
690 * We expect the block group allocator to already be locked.
691 */
692static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
693 struct inode *alloc_inode,
Joel Becker1187c962008-09-03 20:03:39 -0700694 struct buffer_head *bh,
Tao Ma60ca81e2009-02-25 00:53:24 +0800695 u64 max_block,
Tao Mafeb473a2009-02-25 00:53:25 +0800696 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800697 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800698{
699 int status, credits;
700 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
701 struct ocfs2_chain_list *cl;
702 struct ocfs2_alloc_context *ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -0700703 handle_t *handle = NULL;
Tao Ma47119542010-04-22 14:09:15 +0800704 u16 alloc_rec;
Mark Fashehccd979b2005-12-15 14:31:24 -0800705 struct buffer_head *bg_bh = NULL;
706 struct ocfs2_group_desc *bg;
707
708 BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
709
Mark Fashehccd979b2005-12-15 14:31:24 -0800710 cl = &fe->id2.i_chain;
Joel Becker1187c962008-09-03 20:03:39 -0700711 status = ocfs2_reserve_clusters_with_limit(osb,
712 le16_to_cpu(cl->cl_cpg),
Tao Ma60ca81e2009-02-25 00:53:24 +0800713 max_block, flags, &ac);
Mark Fashehccd979b2005-12-15 14:31:24 -0800714 if (status < 0) {
715 if (status != -ENOSPC)
716 mlog_errno(status);
717 goto bail;
718 }
719
720 credits = ocfs2_calc_group_alloc_credits(osb->sb,
721 le16_to_cpu(cl->cl_cpg));
Mark Fasheh65eff9c2006-10-09 17:26:22 -0700722 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800723 if (IS_ERR(handle)) {
724 status = PTR_ERR(handle);
725 handle = NULL;
726 mlog_errno(status);
727 goto bail;
728 }
729
Tao Mafeb473a2009-02-25 00:53:25 +0800730 if (last_alloc_group && *last_alloc_group != 0) {
Tao Ma2f73e132011-02-22 08:22:33 +0800731 trace_ocfs2_block_group_alloc(
732 (unsigned long long)*last_alloc_group);
Tao Mafeb473a2009-02-25 00:53:25 +0800733 ac->ac_last_group = *last_alloc_group;
734 }
Joel Becker798db352010-04-13 14:26:32 +0800735
736 bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
737 ac, cl);
738 if (IS_ERR(bg_bh) && (PTR_ERR(bg_bh) == -ENOSPC))
739 bg_bh = ocfs2_block_group_alloc_discontig(handle,
740 alloc_inode,
Joel Becker8b06bc52010-03-26 10:09:29 +0800741 ac, cl);
Joel Becker798db352010-04-13 14:26:32 +0800742 if (IS_ERR(bg_bh)) {
743 status = PTR_ERR(bg_bh);
744 bg_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800745 if (status != -ENOSPC)
746 mlog_errno(status);
747 goto bail;
748 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800749 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
750
Joel Becker0cf2f762009-02-12 16:41:25 -0800751 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -0700752 bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800753 if (status < 0) {
754 mlog_errno(status);
755 goto bail;
756 }
757
Tao Ma47119542010-04-22 14:09:15 +0800758 alloc_rec = le16_to_cpu(bg->bg_chain);
759 le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
Mark Fashehccd979b2005-12-15 14:31:24 -0800760 le16_to_cpu(bg->bg_free_bits_count));
Tao Ma47119542010-04-22 14:09:15 +0800761 le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
Joel Becker798db352010-04-13 14:26:32 +0800762 le16_to_cpu(bg->bg_bits));
Tao Ma0a463b72010-07-08 11:11:11 +0800763 cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -0800764 if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
765 le16_add_cpu(&cl->cl_next_free_rec, 1);
766
767 le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
768 le16_to_cpu(bg->bg_free_bits_count));
769 le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
770 le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
771
Joel Beckerec20cec2010-03-19 14:13:52 -0700772 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800773
774 spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
775 OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
776 fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
777 le32_to_cpu(fe->i_clusters)));
778 spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
779 i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
Mark Fasheh8110b072007-03-22 16:53:23 -0700780 alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -0800781
782 status = 0;
Tao Mafeb473a2009-02-25 00:53:25 +0800783
784 /* save the new last alloc group so that the caller can cache it. */
785 if (last_alloc_group)
786 *last_alloc_group = ac->ac_last_group;
787
Mark Fashehccd979b2005-12-15 14:31:24 -0800788bail:
789 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -0700790 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -0800791
792 if (ac)
793 ocfs2_free_alloc_context(ac);
794
Mark Fasheha81cb882008-10-07 14:25:16 -0700795 brelse(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800796
Tao Mac1e8d352011-03-07 16:43:21 +0800797 if (status)
798 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800799 return status;
800}
801
802static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
Mark Fashehda5cbf22006-10-06 18:34:35 -0700803 struct ocfs2_alloc_context *ac,
804 int type,
Tao Maffda89a2008-03-03 17:12:09 +0800805 u32 slot,
Tao Mafeb473a2009-02-25 00:53:25 +0800806 u64 *last_alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +0800807 int flags)
Mark Fashehccd979b2005-12-15 14:31:24 -0800808{
809 int status;
810 u32 bits_wanted = ac->ac_bits_wanted;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700811 struct inode *alloc_inode;
Mark Fashehccd979b2005-12-15 14:31:24 -0800812 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800813 struct ocfs2_dinode *fe;
814 u32 free_bits;
815
Mark Fashehda5cbf22006-10-06 18:34:35 -0700816 alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
817 if (!alloc_inode) {
818 mlog_errno(-EINVAL);
819 return -EINVAL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800820 }
821
Mark Fashehda5cbf22006-10-06 18:34:35 -0700822 mutex_lock(&alloc_inode->i_mutex);
823
Mark Fashehe63aecb62007-10-18 15:30:42 -0700824 status = ocfs2_inode_lock(alloc_inode, &bh, 1);
Mark Fashehda5cbf22006-10-06 18:34:35 -0700825 if (status < 0) {
826 mutex_unlock(&alloc_inode->i_mutex);
827 iput(alloc_inode);
828
829 mlog_errno(status);
830 return status;
831 }
832
833 ac->ac_inode = alloc_inode;
Tao Maa4a48912008-03-03 17:12:30 +0800834 ac->ac_alloc_slot = slot;
Mark Fashehda5cbf22006-10-06 18:34:35 -0700835
Mark Fashehccd979b2005-12-15 14:31:24 -0800836 fe = (struct ocfs2_dinode *) bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -0800837
838 /* The bh was validated by the inode read inside
839 * ocfs2_inode_lock(). Any corruption is a code bug. */
840 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
841
Mark Fashehccd979b2005-12-15 14:31:24 -0800842 if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
Mark Fashehb06970532006-03-03 10:24:33 -0800843 ocfs2_error(alloc_inode->i_sb, "Invalid chain allocator %llu",
844 (unsigned long long)le64_to_cpu(fe->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -0800845 status = -EIO;
846 goto bail;
847 }
848
849 free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
850 le32_to_cpu(fe->id1.bitmap1.i_used);
851
852 if (bits_wanted > free_bits) {
853 /* cluster bitmap never grows */
854 if (ocfs2_is_cluster_bitmap(alloc_inode)) {
Tao Ma2f73e132011-02-22 08:22:33 +0800855 trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
856 free_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 status = -ENOSPC;
858 goto bail;
859 }
860
Tao Ma60ca81e2009-02-25 00:53:24 +0800861 if (!(flags & ALLOC_NEW_GROUP)) {
Tao Ma2f73e132011-02-22 08:22:33 +0800862 trace_ocfs2_reserve_suballoc_bits_no_new_group(
863 slot, bits_wanted, free_bits);
Tao Maffda89a2008-03-03 17:12:09 +0800864 status = -ENOSPC;
865 goto bail;
866 }
867
Joel Becker1187c962008-09-03 20:03:39 -0700868 status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
Tao Mafeb473a2009-02-25 00:53:25 +0800869 ac->ac_max_block,
870 last_alloc_group, flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800871 if (status < 0) {
872 if (status != -ENOSPC)
873 mlog_errno(status);
874 goto bail;
875 }
876 atomic_inc(&osb->alloc_stats.bg_extends);
877
878 /* You should never ask for this much metadata */
879 BUG_ON(bits_wanted >
880 (le32_to_cpu(fe->id1.bitmap1.i_total)
881 - le32_to_cpu(fe->id1.bitmap1.i_used)));
882 }
883
884 get_bh(bh);
885 ac->ac_bh = bh;
886bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700887 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800888
Tao Mac1e8d352011-03-07 16:43:21 +0800889 if (status)
890 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -0800891 return status;
892}
893
Tiger Yangb89c5422010-01-25 14:11:06 +0800894static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
895{
896 spin_lock(&osb->osb_lock);
897 osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
898 spin_unlock(&osb->osb_lock);
899 atomic_set(&osb->s_num_inodes_stolen, 0);
900}
901
902static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
903{
904 spin_lock(&osb->osb_lock);
905 osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
906 spin_unlock(&osb->osb_lock);
907 atomic_set(&osb->s_num_meta_stolen, 0);
908}
909
910void ocfs2_init_steal_slots(struct ocfs2_super *osb)
911{
912 ocfs2_init_inode_steal_slot(osb);
913 ocfs2_init_meta_steal_slot(osb);
914}
915
916static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
917{
918 spin_lock(&osb->osb_lock);
919 if (type == INODE_ALLOC_SYSTEM_INODE)
920 osb->s_inode_steal_slot = slot;
921 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
922 osb->s_meta_steal_slot = slot;
923 spin_unlock(&osb->osb_lock);
924}
925
926static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
927{
928 int slot = OCFS2_INVALID_SLOT;
929
930 spin_lock(&osb->osb_lock);
931 if (type == INODE_ALLOC_SYSTEM_INODE)
932 slot = osb->s_inode_steal_slot;
933 else if (type == EXTENT_ALLOC_SYSTEM_INODE)
934 slot = osb->s_meta_steal_slot;
935 spin_unlock(&osb->osb_lock);
936
937 return slot;
938}
939
940static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
941{
942 return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
943}
944
945static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
946{
947 return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
948}
949
950static int ocfs2_steal_resource(struct ocfs2_super *osb,
951 struct ocfs2_alloc_context *ac,
952 int type)
953{
954 int i, status = -ENOSPC;
955 int slot = __ocfs2_get_steal_slot(osb, type);
956
957 /* Start to steal resource from the first slot after ours. */
958 if (slot == OCFS2_INVALID_SLOT)
959 slot = osb->slot_num + 1;
960
961 for (i = 0; i < osb->max_slots; i++, slot++) {
962 if (slot == osb->max_slots)
963 slot = 0;
964
965 if (slot == osb->slot_num)
966 continue;
967
968 status = ocfs2_reserve_suballoc_bits(osb, ac,
969 type,
970 (u32)slot, NULL,
971 NOT_ALLOC_NEW_GROUP);
972 if (status >= 0) {
973 __ocfs2_set_steal_slot(osb, slot, type);
974 break;
975 }
976
977 ocfs2_free_ac_resource(ac);
978 }
979
980 return status;
981}
982
983static int ocfs2_steal_inode(struct ocfs2_super *osb,
984 struct ocfs2_alloc_context *ac)
985{
986 return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
987}
988
989static int ocfs2_steal_meta(struct ocfs2_super *osb,
990 struct ocfs2_alloc_context *ac)
991{
992 return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
993}
994
Tiger Yangcf1d6c72008-08-18 17:11:00 +0800995int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
996 int blocks,
997 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -0800998{
999 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001000 int slot = ocfs2_get_meta_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08001001
Robert P. J. Daycd861282006-12-13 00:34:52 -08001002 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001003 if (!(*ac)) {
1004 status = -ENOMEM;
1005 mlog_errno(status);
1006 goto bail;
1007 }
1008
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001009 (*ac)->ac_bits_wanted = blocks;
Mark Fashehccd979b2005-12-15 14:31:24 -08001010 (*ac)->ac_which = OCFS2_AC_USE_META;
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 (*ac)->ac_group_search = ocfs2_block_group_search;
1012
Tiger Yangb89c5422010-01-25 14:11:06 +08001013 if (slot != OCFS2_INVALID_SLOT &&
1014 atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
1015 goto extent_steal;
1016
1017 atomic_set(&osb->s_num_meta_stolen, 0);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001018 status = ocfs2_reserve_suballoc_bits(osb, (*ac),
Tao Maffda89a2008-03-03 17:12:09 +08001019 EXTENT_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001020 (u32)osb->slot_num, NULL,
Mark Fasheh33d5d382010-02-24 13:34:09 -08001021 ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
Tiger Yangb89c5422010-01-25 14:11:06 +08001022
1023
1024 if (status >= 0) {
1025 status = 0;
1026 if (slot != OCFS2_INVALID_SLOT)
1027 ocfs2_init_meta_steal_slot(osb);
1028 goto bail;
1029 } else if (status < 0 && status != -ENOSPC) {
1030 mlog_errno(status);
1031 goto bail;
1032 }
1033
1034 ocfs2_free_ac_resource(*ac);
1035
1036extent_steal:
1037 status = ocfs2_steal_meta(osb, *ac);
1038 atomic_inc(&osb->s_num_meta_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 if (status < 0) {
1040 if (status != -ENOSPC)
1041 mlog_errno(status);
1042 goto bail;
1043 }
1044
1045 status = 0;
1046bail:
1047 if ((status < 0) && *ac) {
1048 ocfs2_free_alloc_context(*ac);
1049 *ac = NULL;
1050 }
1051
Tao Mac1e8d352011-03-07 16:43:21 +08001052 if (status)
1053 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 return status;
1055}
1056
Tiger Yangcf1d6c72008-08-18 17:11:00 +08001057int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
1058 struct ocfs2_extent_list *root_el,
1059 struct ocfs2_alloc_context **ac)
1060{
1061 return ocfs2_reserve_new_metadata_blocks(osb,
1062 ocfs2_extend_meta_needed(root_el),
1063 ac);
1064}
1065
Mark Fashehccd979b2005-12-15 14:31:24 -08001066int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 struct ocfs2_alloc_context **ac)
1068{
1069 int status;
Tiger Yangb89c5422010-01-25 14:11:06 +08001070 int slot = ocfs2_get_inode_steal_slot(osb);
Tao Mafeb473a2009-02-25 00:53:25 +08001071 u64 alloc_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001072
Robert P. J. Daycd861282006-12-13 00:34:52 -08001073 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 if (!(*ac)) {
1075 status = -ENOMEM;
1076 mlog_errno(status);
1077 goto bail;
1078 }
1079
1080 (*ac)->ac_bits_wanted = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 (*ac)->ac_which = OCFS2_AC_USE_INODE;
1082
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 (*ac)->ac_group_search = ocfs2_block_group_search;
1084
Tao Ma4d0ddb22008-03-05 16:11:46 +08001085 /*
Joel Becker1187c962008-09-03 20:03:39 -07001086 * stat(2) can't handle i_ino > 32bits, so we tell the
1087 * lower levels not to allocate us a block group past that
Joel Becker12462f12008-09-03 20:03:40 -07001088 * limit. The 'inode64' mount option avoids this behavior.
Joel Becker1187c962008-09-03 20:03:39 -07001089 */
Joel Becker12462f12008-09-03 20:03:40 -07001090 if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
1091 (*ac)->ac_max_block = (u32)~0U;
Joel Becker1187c962008-09-03 20:03:39 -07001092
1093 /*
Tao Ma4d0ddb22008-03-05 16:11:46 +08001094 * slot is set when we successfully steal inode from other nodes.
1095 * It is reset in 3 places:
1096 * 1. when we flush the truncate log
1097 * 2. when we complete local alloc recovery.
1098 * 3. when we successfully allocate from our own slot.
1099 * After it is set, we will go on stealing inodes until we find the
1100 * need to check our slots to see whether there is some space for us.
1101 */
1102 if (slot != OCFS2_INVALID_SLOT &&
Tiger Yangb89c5422010-01-25 14:11:06 +08001103 atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
Tao Ma4d0ddb22008-03-05 16:11:46 +08001104 goto inode_steal;
1105
1106 atomic_set(&osb->s_num_inodes_stolen, 0);
Tao Mafeb473a2009-02-25 00:53:25 +08001107 alloc_group = osb->osb_inode_alloc_group;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001108 status = ocfs2_reserve_suballoc_bits(osb, *ac,
1109 INODE_ALLOC_SYSTEM_INODE,
Tiger Yangb89c5422010-01-25 14:11:06 +08001110 (u32)osb->slot_num,
Tao Mafeb473a2009-02-25 00:53:25 +08001111 &alloc_group,
Tao Ma60ca81e2009-02-25 00:53:24 +08001112 ALLOC_NEW_GROUP |
1113 ALLOC_GROUPS_FROM_GLOBAL);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001114 if (status >= 0) {
1115 status = 0;
1116
Tao Mafeb473a2009-02-25 00:53:25 +08001117 spin_lock(&osb->osb_lock);
1118 osb->osb_inode_alloc_group = alloc_group;
1119 spin_unlock(&osb->osb_lock);
Tao Ma2f73e132011-02-22 08:22:33 +08001120 trace_ocfs2_reserve_new_inode_new_group(
1121 (unsigned long long)alloc_group);
Tao Mafeb473a2009-02-25 00:53:25 +08001122
Tao Ma4d0ddb22008-03-05 16:11:46 +08001123 /*
1124 * Some inodes must be freed by us, so try to allocate
1125 * from our own next time.
1126 */
1127 if (slot != OCFS2_INVALID_SLOT)
1128 ocfs2_init_inode_steal_slot(osb);
1129 goto bail;
1130 } else if (status < 0 && status != -ENOSPC) {
1131 mlog_errno(status);
1132 goto bail;
1133 }
1134
1135 ocfs2_free_ac_resource(*ac);
1136
1137inode_steal:
Tiger Yangb89c5422010-01-25 14:11:06 +08001138 status = ocfs2_steal_inode(osb, *ac);
Tao Ma4d0ddb22008-03-05 16:11:46 +08001139 atomic_inc(&osb->s_num_inodes_stolen);
Mark Fashehccd979b2005-12-15 14:31:24 -08001140 if (status < 0) {
1141 if (status != -ENOSPC)
1142 mlog_errno(status);
1143 goto bail;
1144 }
1145
1146 status = 0;
1147bail:
1148 if ((status < 0) && *ac) {
1149 ocfs2_free_alloc_context(*ac);
1150 *ac = NULL;
1151 }
1152
Tao Mac1e8d352011-03-07 16:43:21 +08001153 if (status)
1154 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 return status;
1156}
1157
1158/* local alloc code has to do the same thing, so rather than do this
1159 * twice.. */
1160int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
1161 struct ocfs2_alloc_context *ac)
1162{
1163 int status;
1164
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 ac->ac_which = OCFS2_AC_USE_MAIN;
1166 ac->ac_group_search = ocfs2_cluster_group_search;
1167
Mark Fashehda5cbf22006-10-06 18:34:35 -07001168 status = ocfs2_reserve_suballoc_bits(osb, ac,
1169 GLOBAL_BITMAP_SYSTEM_INODE,
Tao Mafeb473a2009-02-25 00:53:25 +08001170 OCFS2_INVALID_SLOT, NULL,
Tao Maffda89a2008-03-03 17:12:09 +08001171 ALLOC_NEW_GROUP);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001172 if (status < 0 && status != -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001173 mlog_errno(status);
Mark Fashehda5cbf22006-10-06 18:34:35 -07001174 goto bail;
1175 }
1176
Mark Fashehccd979b2005-12-15 14:31:24 -08001177bail:
1178 return status;
1179}
1180
1181/* Callers don't need to care which bitmap (local alloc or main) to
1182 * use so we figure it out for them, but unfortunately this clutters
1183 * things a bit. */
Joel Becker1187c962008-09-03 20:03:39 -07001184static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
1185 u32 bits_wanted, u64 max_block,
Tao Ma60ca81e2009-02-25 00:53:24 +08001186 int flags,
Joel Becker1187c962008-09-03 20:03:39 -07001187 struct ocfs2_alloc_context **ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08001188{
1189 int status;
1190
Robert P. J. Daycd861282006-12-13 00:34:52 -08001191 *ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 if (!(*ac)) {
1193 status = -ENOMEM;
1194 mlog_errno(status);
1195 goto bail;
1196 }
1197
1198 (*ac)->ac_bits_wanted = bits_wanted;
Joel Becker1187c962008-09-03 20:03:39 -07001199 (*ac)->ac_max_block = max_block;
Mark Fashehccd979b2005-12-15 14:31:24 -08001200
1201 status = -ENOSPC;
Tao Ma60ca81e2009-02-25 00:53:24 +08001202 if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
1203 ocfs2_alloc_should_use_local(osb, bits_wanted)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 status = ocfs2_reserve_local_alloc_bits(osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08001205 bits_wanted,
1206 *ac);
Mark Fasheha57c8fd2010-03-16 21:01:00 -07001207 if ((status < 0) && (status != -ENOSPC)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001208 mlog_errno(status);
1209 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08001210 }
1211 }
1212
1213 if (status == -ENOSPC) {
1214 status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
1215 if (status < 0) {
1216 if (status != -ENOSPC)
1217 mlog_errno(status);
1218 goto bail;
1219 }
1220 }
1221
1222 status = 0;
1223bail:
1224 if ((status < 0) && *ac) {
1225 ocfs2_free_alloc_context(*ac);
1226 *ac = NULL;
1227 }
1228
Tao Mac1e8d352011-03-07 16:43:21 +08001229 if (status)
1230 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001231 return status;
1232}
1233
Joel Becker1187c962008-09-03 20:03:39 -07001234int ocfs2_reserve_clusters(struct ocfs2_super *osb,
1235 u32 bits_wanted,
1236 struct ocfs2_alloc_context **ac)
1237{
Tao Ma60ca81e2009-02-25 00:53:24 +08001238 return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
1239 ALLOC_NEW_GROUP, ac);
Joel Becker1187c962008-09-03 20:03:39 -07001240}
1241
Mark Fashehccd979b2005-12-15 14:31:24 -08001242/*
1243 * More or less lifted from ext3. I'll leave their description below:
1244 *
1245 * "For ext3 allocations, we must not reuse any blocks which are
1246 * allocated in the bitmap buffer's "last committed data" copy. This
1247 * prevents deletes from freeing up the page for reuse until we have
1248 * committed the delete transaction.
1249 *
1250 * If we didn't do this, then deleting something and reallocating it as
1251 * data would allow the old block to be overwritten before the
1252 * transaction committed (because we force data to disk before commit).
1253 * This would lead to corruption if we crashed between overwriting the
1254 * data and committing the delete.
1255 *
1256 * @@@ We may want to make this allocation behaviour conditional on
1257 * data-writes at some point, and disable it for metadata allocations or
1258 * sync-data inodes."
1259 *
1260 * Note: OCFS2 already does this differently for metadata vs data
Joe Perchesc78bad12008-02-03 17:33:42 +02001261 * allocations, as those bitmaps are separate and undo access is never
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 * called on a metadata group descriptor.
1263 */
1264static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
1265 int nr)
1266{
1267 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001268 int ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001269
1270 if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
1271 return 0;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001272
1273 if (!buffer_jbd(bg_bh))
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 return 1;
1275
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001276 jbd_lock_bh_state(bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
Sunil Mushran94e41ec2009-06-19 14:45:54 -07001278 if (bg)
1279 ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
1280 else
1281 ret = 1;
1282 jbd_unlock_bh_state(bg_bh);
1283
1284 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08001285}
1286
1287static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
1288 struct buffer_head *bg_bh,
1289 unsigned int bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001290 unsigned int total_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001291 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001292{
1293 void *bitmap;
1294 u16 best_offset, best_size;
1295 int offset, start, found, status = 0;
1296 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1297
Joel Becker42035302008-11-13 14:49:15 -08001298 /* Callers got this descriptor from
1299 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1300 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001301
1302 found = start = best_offset = best_size = 0;
1303 bitmap = bg->bg_bitmap;
1304
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001305 while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
1306 if (offset == total_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 break;
1308
1309 if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
1310 /* We found a zero, but we can't use it as it
1311 * hasn't been put to disk yet! */
1312 found = 0;
1313 start = offset + 1;
1314 } else if (offset == start) {
1315 /* we found a zero */
1316 found++;
1317 /* move start to the next bit to test */
1318 start++;
1319 } else {
1320 /* got a zero after some ones */
1321 found = 1;
1322 start = offset + 1;
1323 }
1324 if (found > best_size) {
1325 best_size = found;
1326 best_offset = start - found;
1327 }
1328 /* we got everything we needed */
1329 if (found == bits_wanted) {
1330 /* mlog(0, "Found it all!\n"); */
1331 break;
1332 }
1333 }
1334
Joel Becker7d1fe092010-04-13 14:30:19 +08001335 if (best_size) {
1336 res->sr_bit_offset = best_offset;
1337 res->sr_bits = best_size;
Mark Fashehccd979b2005-12-15 14:31:24 -08001338 } else {
1339 status = -ENOSPC;
1340 /* No error log here -- see the comment above
1341 * ocfs2_test_bg_bit_allocatable */
1342 }
1343
1344 return status;
1345}
1346
Mark Fasheh1fabe142006-10-09 18:11:45 -07001347static inline int ocfs2_block_group_set_bits(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 struct inode *alloc_inode,
1349 struct ocfs2_group_desc *bg,
1350 struct buffer_head *group_bh,
1351 unsigned int bit_off,
1352 unsigned int num_bits)
1353{
1354 int status;
1355 void *bitmap = bg->bg_bitmap;
1356 int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
1357
Joel Becker42035302008-11-13 14:49:15 -08001358 /* All callers get the descriptor via
1359 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1360 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001361 BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
1362
Tao Ma2f73e132011-02-22 08:22:33 +08001363 trace_ocfs2_block_group_set_bits(bit_off, num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001364
1365 if (ocfs2_is_cluster_bitmap(alloc_inode))
1366 journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
1367
Joel Becker13723d02008-10-17 19:25:01 -07001368 status = ocfs2_journal_access_gd(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001369 INODE_CACHE(alloc_inode),
Joel Becker13723d02008-10-17 19:25:01 -07001370 group_bh,
1371 journal_type);
Mark Fashehccd979b2005-12-15 14:31:24 -08001372 if (status < 0) {
1373 mlog_errno(status);
1374 goto bail;
1375 }
1376
1377 le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07001378 if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
1379 ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit"
1380 " count %u but claims %u are freed. num_bits %d",
1381 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1382 le16_to_cpu(bg->bg_bits),
1383 le16_to_cpu(bg->bg_free_bits_count), num_bits);
1384 return -EROFS;
1385 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001386 while(num_bits--)
1387 ocfs2_set_bit(bit_off++, bitmap);
1388
Joel Beckerec20cec2010-03-19 14:13:52 -07001389 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001390
1391bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001392 if (status)
1393 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001394 return status;
1395}
1396
1397/* find the one with the most empty bits */
1398static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
1399{
1400 u16 curr, best;
1401
1402 BUG_ON(!cl->cl_next_free_rec);
1403
1404 best = curr = 0;
1405 while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
1406 if (le32_to_cpu(cl->cl_recs[curr].c_free) >
1407 le32_to_cpu(cl->cl_recs[best].c_free))
1408 best = curr;
1409 curr++;
1410 }
1411
1412 BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
1413 return best;
1414}
1415
Mark Fasheh1fabe142006-10-09 18:11:45 -07001416static int ocfs2_relink_block_group(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001417 struct inode *alloc_inode,
1418 struct buffer_head *fe_bh,
1419 struct buffer_head *bg_bh,
1420 struct buffer_head *prev_bg_bh,
1421 u16 chain)
1422{
1423 int status;
1424 /* there is a really tiny chance the journal calls could fail,
1425 * but we wouldn't want inconsistent blocks in *any* case. */
1426 u64 fe_ptr, bg_ptr, prev_bg_ptr;
1427 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
1428 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
1429 struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
1430
Joel Becker42035302008-11-13 14:49:15 -08001431 /* The caller got these descriptors from
1432 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
1433 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
1434 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08001435
Tao Ma2f73e132011-02-22 08:22:33 +08001436 trace_ocfs2_relink_block_group(
1437 (unsigned long long)le64_to_cpu(fe->i_blkno), chain,
1438 (unsigned long long)le64_to_cpu(bg->bg_blkno),
1439 (unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001440
1441 fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
1442 bg_ptr = le64_to_cpu(bg->bg_next_group);
1443 prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
1444
Joel Becker0cf2f762009-02-12 16:41:25 -08001445 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1446 prev_bg_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001447 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 if (status < 0) {
1449 mlog_errno(status);
1450 goto out_rollback;
1451 }
1452
1453 prev_bg->bg_next_group = bg->bg_next_group;
Joel Beckerec20cec2010-03-19 14:13:52 -07001454 ocfs2_journal_dirty(handle, prev_bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001455
Joel Becker0cf2f762009-02-12 16:41:25 -08001456 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
1457 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 if (status < 0) {
1459 mlog_errno(status);
1460 goto out_rollback;
1461 }
1462
1463 bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001464 ocfs2_journal_dirty(handle, bg_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001465
Joel Becker0cf2f762009-02-12 16:41:25 -08001466 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
1467 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 if (status < 0) {
1469 mlog_errno(status);
1470 goto out_rollback;
1471 }
1472
1473 fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
Joel Beckerec20cec2010-03-19 14:13:52 -07001474 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001475
Mark Fashehccd979b2005-12-15 14:31:24 -08001476out_rollback:
1477 if (status < 0) {
1478 fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
1479 bg->bg_next_group = cpu_to_le64(bg_ptr);
1480 prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
1481 }
Joel Becker42035302008-11-13 14:49:15 -08001482
Tao Mac1e8d352011-03-07 16:43:21 +08001483 if (status)
1484 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 return status;
1486}
1487
1488static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
1489 u32 wanted)
1490{
1491 return le16_to_cpu(bg->bg_free_bits_count) > wanted;
1492}
1493
1494/* return 0 on success, -ENOSPC to keep searching and any other < 0
1495 * value on error. */
1496static int ocfs2_cluster_group_search(struct inode *inode,
1497 struct buffer_head *group_bh,
1498 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001499 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001500 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001501{
1502 int search = -ENOSPC;
1503 int ret;
Joel Becker1187c962008-09-03 20:03:39 -07001504 u64 blkoff;
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001505 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh9c7af402008-07-28 18:02:53 -07001506 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001507 unsigned int max_bits, gd_cluster_off;
Mark Fashehccd979b2005-12-15 14:31:24 -08001508
1509 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
1510
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001511 if (gd->bg_free_bits_count) {
1512 max_bits = le16_to_cpu(gd->bg_bits);
1513
1514 /* Tail groups in cluster bitmaps which aren't cpg
1515 * aligned are prone to partial extention by a failed
1516 * fs resize. If the file system resize never got to
1517 * update the dinode cluster count, then we don't want
1518 * to trust any clusters past it, regardless of what
1519 * the group descriptor says. */
1520 gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
1521 le64_to_cpu(gd->bg_blkno));
1522 if ((gd_cluster_off + max_bits) >
1523 OCFS2_I(inode)->ip_clusters) {
1524 max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
Tao Ma2f73e132011-02-22 08:22:33 +08001525 trace_ocfs2_cluster_group_search_wrong_max_bits(
1526 (unsigned long long)le64_to_cpu(gd->bg_blkno),
1527 le16_to_cpu(gd->bg_bits),
1528 OCFS2_I(inode)->ip_clusters, max_bits);
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001529 }
1530
Mark Fashehccd979b2005-12-15 14:31:24 -08001531 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1532 group_bh, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001533 max_bits, res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001534 if (ret)
1535 return ret;
1536
Joel Becker1187c962008-09-03 20:03:39 -07001537 if (max_block) {
1538 blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
1539 gd_cluster_off +
Joel Becker7d1fe092010-04-13 14:30:19 +08001540 res->sr_bit_offset +
1541 res->sr_bits);
Tao Ma2f73e132011-02-22 08:22:33 +08001542 trace_ocfs2_cluster_group_search_max_block(
1543 (unsigned long long)blkoff,
1544 (unsigned long long)max_block);
Joel Becker1187c962008-09-03 20:03:39 -07001545 if (blkoff > max_block)
1546 return -ENOSPC;
1547 }
1548
Mark Fashehccd979b2005-12-15 14:31:24 -08001549 /* ocfs2_block_group_find_clear_bits() might
1550 * return success, but we still want to return
1551 * -ENOSPC unless it found the minimum number
1552 * of bits. */
Joel Becker7d1fe092010-04-13 14:30:19 +08001553 if (min_bits <= res->sr_bits)
Mark Fashehccd979b2005-12-15 14:31:24 -08001554 search = 0; /* success */
Joel Becker7d1fe092010-04-13 14:30:19 +08001555 else if (res->sr_bits) {
Mark Fasheh9c7af402008-07-28 18:02:53 -07001556 /*
1557 * Don't show bits which we'll be returning
1558 * for allocation to the local alloc bitmap.
1559 */
Joel Becker7d1fe092010-04-13 14:30:19 +08001560 ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001561 }
1562 }
1563
1564 return search;
1565}
1566
1567static int ocfs2_block_group_search(struct inode *inode,
1568 struct buffer_head *group_bh,
1569 u32 bits_wanted, u32 min_bits,
Joel Becker1187c962008-09-03 20:03:39 -07001570 u64 max_block,
Joel Becker7d1fe092010-04-13 14:30:19 +08001571 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001572{
1573 int ret = -ENOSPC;
Joel Becker1187c962008-09-03 20:03:39 -07001574 u64 blkoff;
Mark Fashehccd979b2005-12-15 14:31:24 -08001575 struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
1576
1577 BUG_ON(min_bits != 1);
1578 BUG_ON(ocfs2_is_cluster_bitmap(inode));
1579
Joel Becker1187c962008-09-03 20:03:39 -07001580 if (bg->bg_free_bits_count) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001581 ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
1582 group_bh, bits_wanted,
Mark Fasheh7bf72ed2006-05-03 17:46:50 -07001583 le16_to_cpu(bg->bg_bits),
Joel Becker7d1fe092010-04-13 14:30:19 +08001584 res);
Joel Becker1187c962008-09-03 20:03:39 -07001585 if (!ret && max_block) {
Joel Becker7d1fe092010-04-13 14:30:19 +08001586 blkoff = le64_to_cpu(bg->bg_blkno) +
1587 res->sr_bit_offset + res->sr_bits;
Tao Ma2f73e132011-02-22 08:22:33 +08001588 trace_ocfs2_block_group_search_max_block(
1589 (unsigned long long)blkoff,
1590 (unsigned long long)max_block);
Joel Becker1187c962008-09-03 20:03:39 -07001591 if (blkoff > max_block)
1592 ret = -ENOSPC;
1593 }
1594 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001595
1596 return ret;
1597}
1598
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001599static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001600 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001601 struct buffer_head *di_bh,
1602 u32 num_bits,
1603 u16 chain)
1604{
1605 int ret;
1606 u32 tmp_used;
1607 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
1608 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
1609
Joel Becker0cf2f762009-02-12 16:41:25 -08001610 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001611 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001612 if (ret < 0) {
1613 mlog_errno(ret);
1614 goto out;
1615 }
1616
1617 tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
1618 di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
1619 le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
Joel Beckerec20cec2010-03-19 14:13:52 -07001620 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001621
1622out:
1623 return ret;
1624}
1625
Joel Beckerba206632010-03-26 10:08:59 +08001626static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
1627 struct ocfs2_extent_rec *rec,
1628 struct ocfs2_chain_list *cl)
Joel Becker13e434c2010-03-26 10:08:27 +08001629{
1630 unsigned int bpc = le16_to_cpu(cl->cl_bpc);
1631 unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
1632 unsigned int bitcount = le32_to_cpu(rec->e_leaf_clusters) * bpc;
1633
1634 if (res->sr_bit_offset < bitoff)
1635 return 0;
1636 if (res->sr_bit_offset >= (bitoff + bitcount))
1637 return 0;
Joel Beckerba206632010-03-26 10:08:59 +08001638 res->sr_blkno = le64_to_cpu(rec->e_blkno) +
1639 (res->sr_bit_offset - bitoff);
Joel Becker13e434c2010-03-26 10:08:27 +08001640 if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
1641 res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
1642 return 1;
1643}
1644
Joel Beckerba206632010-03-26 10:08:59 +08001645static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
1646 struct ocfs2_group_desc *bg,
1647 struct ocfs2_suballoc_result *res)
Joel Becker13e434c2010-03-26 10:08:27 +08001648{
1649 int i;
Joel Becker2b6cb572010-03-26 10:09:15 +08001650 u64 bg_blkno = res->sr_bg_blkno; /* Save off */
Joel Becker13e434c2010-03-26 10:08:27 +08001651 struct ocfs2_extent_rec *rec;
1652 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
1653 struct ocfs2_chain_list *cl = &di->id2.i_chain;
1654
Joel Beckerba206632010-03-26 10:08:59 +08001655 if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
1656 res->sr_blkno = 0;
Joel Becker13e434c2010-03-26 10:08:27 +08001657 return;
Joel Beckerba206632010-03-26 10:08:59 +08001658 }
Joel Becker13e434c2010-03-26 10:08:27 +08001659
Joel Beckerba206632010-03-26 10:08:59 +08001660 res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
Joel Becker2b6cb572010-03-26 10:09:15 +08001661 res->sr_bg_blkno = 0; /* Clear it for contig block groups */
Tao Ma47119542010-04-22 14:09:15 +08001662 if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
Joel Beckerba206632010-03-26 10:08:59 +08001663 !bg->bg_list.l_next_free_rec)
Joel Becker13e434c2010-03-26 10:08:27 +08001664 return;
1665
1666 for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
1667 rec = &bg->bg_list.l_recs[i];
Joel Becker2b6cb572010-03-26 10:09:15 +08001668 if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
1669 res->sr_bg_blkno = bg_blkno; /* Restore */
Joel Becker13e434c2010-03-26 10:08:27 +08001670 break;
Joel Becker2b6cb572010-03-26 10:09:15 +08001671 }
Joel Becker13e434c2010-03-26 10:08:27 +08001672 }
1673}
1674
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001675static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001676 handle_t *handle,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001677 u32 bits_wanted,
1678 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001679 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001680 u16 *bits_left)
1681{
1682 int ret;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001683 struct buffer_head *group_bh = NULL;
1684 struct ocfs2_group_desc *gd;
Joel Becker68f64d42008-11-13 14:49:14 -08001685 struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001686 struct inode *alloc_inode = ac->ac_inode;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001687
Joel Becker7d1fe092010-04-13 14:30:19 +08001688 ret = ocfs2_read_group_descriptor(alloc_inode, di,
1689 res->sr_bg_blkno, &group_bh);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001690 if (ret < 0) {
1691 mlog_errno(ret);
1692 return ret;
1693 }
1694
1695 gd = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001696 ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001697 ac->ac_max_block, res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001698 if (ret < 0) {
1699 if (ret != -ENOSPC)
1700 mlog_errno(ret);
1701 goto out;
1702 }
1703
Joel Becker13e434c2010-03-26 10:08:27 +08001704 if (!ret)
Joel Beckerba206632010-03-26 10:08:59 +08001705 ocfs2_bg_discontig_fix_result(ac, gd, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001706
Mark Fashehe49e2762010-08-13 15:15:17 -07001707 /*
1708 * sr_bg_blkno might have been changed by
1709 * ocfs2_bg_discontig_fix_result
1710 */
1711 res->sr_bg_stable_blkno = group_bh->b_blocknr;
1712
1713 if (ac->ac_find_loc_only)
1714 goto out_loc_only;
1715
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001716 ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001717 res->sr_bits,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001718 le16_to_cpu(gd->bg_chain));
1719 if (ret < 0) {
1720 mlog_errno(ret);
1721 goto out;
1722 }
1723
1724 ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001725 res->sr_bit_offset, res->sr_bits);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001726 if (ret < 0)
1727 mlog_errno(ret);
1728
Mark Fashehe49e2762010-08-13 15:15:17 -07001729out_loc_only:
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001730 *bits_left = le16_to_cpu(gd->bg_free_bits_count);
1731
1732out:
1733 brelse(group_bh);
1734
1735 return ret;
1736}
1737
Mark Fashehccd979b2005-12-15 14:31:24 -08001738static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001739 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001740 u32 bits_wanted,
1741 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001742 struct ocfs2_suballoc_result *res,
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001743 u16 *bits_left)
Mark Fashehccd979b2005-12-15 14:31:24 -08001744{
1745 int status;
Joel Becker7d1fe092010-04-13 14:30:19 +08001746 u16 chain;
Mark Fashehccd979b2005-12-15 14:31:24 -08001747 u64 next_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001748 struct inode *alloc_inode = ac->ac_inode;
1749 struct buffer_head *group_bh = NULL;
1750 struct buffer_head *prev_group_bh = NULL;
1751 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
1752 struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1753 struct ocfs2_group_desc *bg;
1754
1755 chain = ac->ac_chain;
Tao Ma2f73e132011-02-22 08:22:33 +08001756 trace_ocfs2_search_chain_begin(
1757 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
1758 bits_wanted, chain);
Mark Fashehccd979b2005-12-15 14:31:24 -08001759
Joel Becker68f64d42008-11-13 14:49:14 -08001760 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1761 le64_to_cpu(cl->cl_recs[chain].c_blkno),
1762 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001763 if (status < 0) {
1764 mlog_errno(status);
1765 goto bail;
1766 }
1767 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001768
1769 status = -ENOSPC;
1770 /* for now, the chain search is a bit simplistic. We just use
1771 * the 1st group with any empty bits. */
Joel Becker1187c962008-09-03 20:03:39 -07001772 while ((status = ac->ac_group_search(alloc_inode, group_bh,
1773 bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001774 ac->ac_max_block,
1775 res)) == -ENOSPC) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001776 if (!bg->bg_next_group)
1777 break;
Mark Fasheha81cb882008-10-07 14:25:16 -07001778
1779 brelse(prev_group_bh);
1780 prev_group_bh = NULL;
1781
Mark Fashehccd979b2005-12-15 14:31:24 -08001782 next_group = le64_to_cpu(bg->bg_next_group);
1783 prev_group_bh = group_bh;
1784 group_bh = NULL;
Joel Becker68f64d42008-11-13 14:49:14 -08001785 status = ocfs2_read_group_descriptor(alloc_inode, fe,
1786 next_group, &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001787 if (status < 0) {
1788 mlog_errno(status);
1789 goto bail;
1790 }
1791 bg = (struct ocfs2_group_desc *) group_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001792 }
1793 if (status < 0) {
1794 if (status != -ENOSPC)
1795 mlog_errno(status);
1796 goto bail;
1797 }
1798
Tao Ma2f73e132011-02-22 08:22:33 +08001799 trace_ocfs2_search_chain_succ(
1800 (unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001801
Joel Becker7d1fe092010-04-13 14:30:19 +08001802 res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001803
Joel Becker7d1fe092010-04-13 14:30:19 +08001804 BUG_ON(res->sr_bits == 0);
Joel Becker13e434c2010-03-26 10:08:27 +08001805 if (!status)
Joel Beckerba206632010-03-26 10:08:59 +08001806 ocfs2_bg_discontig_fix_result(ac, bg, res);
Joel Becker13e434c2010-03-26 10:08:27 +08001807
Mark Fashehe49e2762010-08-13 15:15:17 -07001808 /*
1809 * sr_bg_blkno might have been changed by
1810 * ocfs2_bg_discontig_fix_result
1811 */
1812 res->sr_bg_stable_blkno = group_bh->b_blocknr;
Mark Fashehccd979b2005-12-15 14:31:24 -08001813
1814 /*
1815 * Keep track of previous block descriptor read. When
1816 * we find a target, if we have read more than X
1817 * number of descriptors, and the target is reasonably
1818 * empty, relink him to top of his chain.
1819 *
1820 * We've read 0 extra blocks and only send one more to
1821 * the transaction, yet the next guy to search has a
1822 * much easier time.
1823 *
1824 * Do this *after* figuring out how many bits we're taking out
1825 * of our target group.
1826 */
1827 if (ac->ac_allow_chain_relink &&
1828 (prev_group_bh) &&
Joel Becker7d1fe092010-04-13 14:30:19 +08001829 (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001830 status = ocfs2_relink_block_group(handle, alloc_inode,
1831 ac->ac_bh, group_bh,
1832 prev_group_bh, chain);
1833 if (status < 0) {
1834 mlog_errno(status);
1835 goto bail;
1836 }
1837 }
1838
Mark Fashehe49e2762010-08-13 15:15:17 -07001839 if (ac->ac_find_loc_only)
1840 goto out_loc_only;
1841
Mark Fashehd5134982010-08-13 15:15:16 -07001842 status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
1843 ac->ac_bh, res->sr_bits,
1844 chain);
1845 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001846 mlog_errno(status);
1847 goto bail;
1848 }
1849
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 status = ocfs2_block_group_set_bits(handle,
1851 alloc_inode,
1852 bg,
1853 group_bh,
Joel Becker7d1fe092010-04-13 14:30:19 +08001854 res->sr_bit_offset,
1855 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001856 if (status < 0) {
1857 mlog_errno(status);
1858 goto bail;
1859 }
1860
Tao Ma2f73e132011-02-22 08:22:33 +08001861 trace_ocfs2_search_chain_end(
1862 (unsigned long long)le64_to_cpu(fe->i_blkno),
1863 res->sr_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08001864
Mark Fashehe49e2762010-08-13 15:15:17 -07001865out_loc_only:
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001866 *bits_left = le16_to_cpu(bg->bg_free_bits_count);
Mark Fashehccd979b2005-12-15 14:31:24 -08001867bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001868 brelse(group_bh);
1869 brelse(prev_group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001870
Tao Mac1e8d352011-03-07 16:43:21 +08001871 if (status)
1872 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001873 return status;
1874}
1875
1876/* will give out up to bits_wanted contiguous bits. */
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001877static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001878 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001879 u32 bits_wanted,
1880 u32 min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001881 struct ocfs2_suballoc_result *res)
Mark Fashehccd979b2005-12-15 14:31:24 -08001882{
1883 int status;
1884 u16 victim, i;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001885 u16 bits_left = 0;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001886 u64 hint = ac->ac_last_group;
Mark Fashehccd979b2005-12-15 14:31:24 -08001887 struct ocfs2_chain_list *cl;
1888 struct ocfs2_dinode *fe;
1889
Mark Fashehccd979b2005-12-15 14:31:24 -08001890 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
1891 BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
1892 BUG_ON(!ac->ac_bh);
1893
1894 fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
Joel Becker10995aa2008-11-13 14:49:12 -08001895
1896 /* The bh was validated by the inode read during
1897 * ocfs2_reserve_suballoc_bits(). Any corruption is a code bug. */
1898 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
1899
Mark Fashehccd979b2005-12-15 14:31:24 -08001900 if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
1901 le32_to_cpu(fe->id1.bitmap1.i_total)) {
Joel Beckeraa8f8e92010-03-26 10:08:07 +08001902 ocfs2_error(ac->ac_inode->i_sb,
1903 "Chain allocator dinode %llu has %u used "
Mark Fashehb06970532006-03-03 10:24:33 -08001904 "bits but only %u total.",
1905 (unsigned long long)le64_to_cpu(fe->i_blkno),
Mark Fashehccd979b2005-12-15 14:31:24 -08001906 le32_to_cpu(fe->id1.bitmap1.i_used),
1907 le32_to_cpu(fe->id1.bitmap1.i_total));
1908 status = -EIO;
1909 goto bail;
1910 }
1911
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001912 res->sr_bg_blkno = hint;
Joel Becker7d1fe092010-04-13 14:30:19 +08001913 if (res->sr_bg_blkno) {
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001914 /* Attempt to short-circuit the usual search mechanism
1915 * by jumping straight to the most recently used
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001916 * allocation group. This helps us maintain some
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001917 * contiguousness across allocations. */
Mark Fashehda5cbf22006-10-06 18:34:35 -07001918 status = ocfs2_search_one_group(ac, handle, bits_wanted,
Joel Becker7d1fe092010-04-13 14:30:19 +08001919 min_bits, res, &bits_left);
1920 if (!status)
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001921 goto set_hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001922 if (status < 0 && status != -ENOSPC) {
1923 mlog_errno(status);
1924 goto bail;
1925 }
1926 }
1927
Mark Fashehccd979b2005-12-15 14:31:24 -08001928 cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
1929
1930 victim = ocfs2_find_victim_chain(cl);
1931 ac->ac_chain = victim;
1932 ac->ac_allow_chain_relink = 1;
1933
Joel Becker7d1fe092010-04-13 14:30:19 +08001934 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
1935 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001936 if (!status) {
1937 hint = ocfs2_group_from_res(res);
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001938 goto set_hint;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001939 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001940 if (status < 0 && status != -ENOSPC) {
1941 mlog_errno(status);
1942 goto bail;
1943 }
1944
Tao Ma2f73e132011-02-22 08:22:33 +08001945 trace_ocfs2_claim_suballoc_bits(victim);
Mark Fashehccd979b2005-12-15 14:31:24 -08001946
1947 /* If we didn't pick a good victim, then just default to
1948 * searching each chain in order. Don't allow chain relinking
1949 * because we only calculate enough journal credits for one
1950 * relink per alloc. */
1951 ac->ac_allow_chain_relink = 0;
1952 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
1953 if (i == victim)
1954 continue;
1955 if (!cl->cl_recs[i].c_free)
1956 continue;
1957
1958 ac->ac_chain = i;
Mark Fashehda5cbf22006-10-06 18:34:35 -07001959 status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
Joel Becker7d1fe092010-04-13 14:30:19 +08001960 res, &bits_left);
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001961 if (!status) {
1962 hint = ocfs2_group_from_res(res);
Mark Fashehccd979b2005-12-15 14:31:24 -08001963 break;
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001964 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001965 if (status < 0 && status != -ENOSPC) {
1966 mlog_errno(status);
1967 goto bail;
1968 }
1969 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001970
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001971set_hint:
1972 if (status != -ENOSPC) {
1973 /* If the next search of this group is not likely to
1974 * yield a suitable extent, then we reset the last
1975 * group hint so as to not waste a disk read */
1976 if (bits_left < min_bits)
1977 ac->ac_last_group = 0;
1978 else
Mark Fashehb2b6ebf2010-08-26 13:06:50 -07001979 ac->ac_last_group = hint;
Mark Fasheh883d4ca2006-06-05 16:41:00 -04001980 }
1981
1982bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001983 if (status)
1984 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001985 return status;
1986}
1987
Joel Becker1ed9b772010-05-06 13:59:06 +08001988int ocfs2_claim_metadata(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001989 struct ocfs2_alloc_context *ac,
1990 u32 bits_wanted,
Joel Becker2b6cb572010-03-26 10:09:15 +08001991 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001992 u16 *suballoc_bit_start,
1993 unsigned int *num_bits,
1994 u64 *blkno_start)
1995{
1996 int status;
Joel Beckerba206632010-03-26 10:08:59 +08001997 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001998
1999 BUG_ON(!ac);
2000 BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
2001 BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
Mark Fashehccd979b2005-12-15 14:31:24 -08002002
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002003 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002004 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002005 bits_wanted,
2006 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002007 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002008 if (status < 0) {
2009 mlog_errno(status);
2010 goto bail;
2011 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002012 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002013
Joel Becker2b6cb572010-03-26 10:09:15 +08002014 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002015 *suballoc_bit_start = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002016 *blkno_start = res.sr_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002017 ac->ac_bits_given += res.sr_bits;
2018 *num_bits = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002019 status = 0;
2020bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002021 if (status)
2022 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002023 return status;
2024}
2025
Tao Ma13821152009-02-25 00:53:23 +08002026static void ocfs2_init_inode_ac_group(struct inode *dir,
Tao Maabf1b3c2010-04-27 08:30:36 +08002027 struct buffer_head *parent_di_bh,
Tao Ma13821152009-02-25 00:53:23 +08002028 struct ocfs2_alloc_context *ac)
2029{
Tao Maabf1b3c2010-04-27 08:30:36 +08002030 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
Tao Ma13821152009-02-25 00:53:23 +08002031 /*
2032 * Try to allocate inodes from some specific group.
2033 *
2034 * If the parent dir has recorded the last group used in allocation,
2035 * cool, use it. Otherwise if we try to allocate new inode from the
2036 * same slot the parent dir belongs to, use the same chunk.
2037 *
2038 * We are very careful here to avoid the mistake of setting
2039 * ac_last_group to a group descriptor from a different (unlocked) slot.
2040 */
2041 if (OCFS2_I(dir)->ip_last_used_group &&
2042 OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
2043 ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
Tao Maabf1b3c2010-04-27 08:30:36 +08002044 else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
2045 if (di->i_suballoc_loc)
2046 ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
2047 else
2048 ac->ac_last_group = ocfs2_which_suballoc_group(
2049 le64_to_cpu(di->i_blkno),
2050 le16_to_cpu(di->i_suballoc_bit));
2051 }
Tao Ma13821152009-02-25 00:53:23 +08002052}
2053
2054static inline void ocfs2_save_inode_ac_group(struct inode *dir,
2055 struct ocfs2_alloc_context *ac)
2056{
2057 OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
2058 OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
2059}
2060
Mark Fashehe49e2762010-08-13 15:15:17 -07002061int ocfs2_find_new_inode_loc(struct inode *dir,
2062 struct buffer_head *parent_fe_bh,
2063 struct ocfs2_alloc_context *ac,
2064 u64 *fe_blkno)
2065{
2066 int ret;
2067 handle_t *handle = NULL;
2068 struct ocfs2_suballoc_result *res;
2069
2070 BUG_ON(!ac);
2071 BUG_ON(ac->ac_bits_given != 0);
2072 BUG_ON(ac->ac_bits_wanted != 1);
2073 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
2074
2075 res = kzalloc(sizeof(*res), GFP_NOFS);
2076 if (res == NULL) {
2077 ret = -ENOMEM;
2078 mlog_errno(ret);
2079 goto out;
2080 }
2081
2082 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2083
2084 /*
2085 * The handle started here is for chain relink. Alternatively,
2086 * we could just disable relink for these calls.
2087 */
2088 handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
2089 if (IS_ERR(handle)) {
2090 ret = PTR_ERR(handle);
2091 handle = NULL;
2092 mlog_errno(ret);
2093 goto out;
2094 }
2095
2096 /*
2097 * This will instruct ocfs2_claim_suballoc_bits and
2098 * ocfs2_search_one_group to search but save actual allocation
2099 * for later.
2100 */
2101 ac->ac_find_loc_only = 1;
2102
2103 ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
2104 if (ret < 0) {
2105 mlog_errno(ret);
2106 goto out;
2107 }
2108
2109 ac->ac_find_loc_priv = res;
2110 *fe_blkno = res->sr_blkno;
2111
2112out:
2113 if (handle)
2114 ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
2115
2116 if (ret)
2117 kfree(res);
2118
2119 return ret;
2120}
2121
2122int ocfs2_claim_new_inode_at_loc(handle_t *handle,
2123 struct inode *dir,
2124 struct ocfs2_alloc_context *ac,
2125 u64 *suballoc_loc,
2126 u16 *suballoc_bit,
2127 u64 di_blkno)
2128{
2129 int ret;
2130 u16 chain;
2131 struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
2132 struct buffer_head *bg_bh = NULL;
2133 struct ocfs2_group_desc *bg;
2134 struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
2135
2136 /*
2137 * Since di_blkno is being passed back in, we check for any
2138 * inconsistencies which may have happened between
2139 * calls. These are code bugs as di_blkno is not expected to
2140 * change once returned from ocfs2_find_new_inode_loc()
2141 */
2142 BUG_ON(res->sr_blkno != di_blkno);
2143
2144 ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
2145 res->sr_bg_stable_blkno, &bg_bh);
2146 if (ret) {
2147 mlog_errno(ret);
2148 goto out;
2149 }
2150
2151 bg = (struct ocfs2_group_desc *) bg_bh->b_data;
2152 chain = le16_to_cpu(bg->bg_chain);
2153
2154 ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
2155 ac->ac_bh, res->sr_bits,
2156 chain);
2157 if (ret) {
2158 mlog_errno(ret);
2159 goto out;
2160 }
2161
2162 ret = ocfs2_block_group_set_bits(handle,
2163 ac->ac_inode,
2164 bg,
2165 bg_bh,
2166 res->sr_bit_offset,
2167 res->sr_bits);
2168 if (ret < 0) {
2169 mlog_errno(ret);
2170 goto out;
2171 }
2172
Tao Ma2f73e132011-02-22 08:22:33 +08002173 trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
2174 res->sr_bits);
Mark Fashehe49e2762010-08-13 15:15:17 -07002175
2176 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
2177
2178 BUG_ON(res->sr_bits != 1);
2179
2180 *suballoc_loc = res->sr_bg_blkno;
2181 *suballoc_bit = res->sr_bit_offset;
2182 ac->ac_bits_given++;
2183 ocfs2_save_inode_ac_group(dir, ac);
2184
2185out:
2186 brelse(bg_bh);
2187
2188 return ret;
2189}
2190
Joel Becker1ed9b772010-05-06 13:59:06 +08002191int ocfs2_claim_new_inode(handle_t *handle,
Tao Ma13821152009-02-25 00:53:23 +08002192 struct inode *dir,
2193 struct buffer_head *parent_fe_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08002194 struct ocfs2_alloc_context *ac,
Joel Becker2b6cb572010-03-26 10:09:15 +08002195 u64 *suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08002196 u16 *suballoc_bit,
2197 u64 *fe_blkno)
2198{
2199 int status;
Joel Becker2b6cb572010-03-26 10:09:15 +08002200 struct ocfs2_suballoc_result res;
Mark Fashehccd979b2005-12-15 14:31:24 -08002201
Mark Fashehccd979b2005-12-15 14:31:24 -08002202 BUG_ON(!ac);
2203 BUG_ON(ac->ac_bits_given != 0);
2204 BUG_ON(ac->ac_bits_wanted != 1);
2205 BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002206
Tao Ma13821152009-02-25 00:53:23 +08002207 ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
2208
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002209 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002210 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002211 1,
2212 1,
Joel Becker7d1fe092010-04-13 14:30:19 +08002213 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002214 if (status < 0) {
2215 mlog_errno(status);
2216 goto bail;
2217 }
Joel Becker1ed9b772010-05-06 13:59:06 +08002218 atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
Mark Fashehccd979b2005-12-15 14:31:24 -08002219
Joel Becker7d1fe092010-04-13 14:30:19 +08002220 BUG_ON(res.sr_bits != 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08002221
Joel Becker2b6cb572010-03-26 10:09:15 +08002222 *suballoc_loc = res.sr_bg_blkno;
Joel Becker7d1fe092010-04-13 14:30:19 +08002223 *suballoc_bit = res.sr_bit_offset;
Joel Beckerba206632010-03-26 10:08:59 +08002224 *fe_blkno = res.sr_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002225 ac->ac_bits_given++;
Tao Ma13821152009-02-25 00:53:23 +08002226 ocfs2_save_inode_ac_group(dir, ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08002227 status = 0;
2228bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002229 if (status)
2230 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002231 return status;
2232}
2233
2234/* translate a group desc. blkno and it's bitmap offset into
2235 * disk cluster offset. */
2236static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
2237 u64 bg_blkno,
2238 u16 bg_bit_off)
2239{
2240 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2241 u32 cluster = 0;
2242
2243 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2244
2245 if (bg_blkno != osb->first_cluster_group_blkno)
2246 cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
2247 cluster += (u32) bg_bit_off;
2248 return cluster;
2249}
2250
2251/* given a cluster offset, calculate which block group it belongs to
2252 * and return that block offset. */
Tao Mad6590722007-12-18 15:47:03 +08002253u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
Mark Fashehccd979b2005-12-15 14:31:24 -08002254{
2255 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2256 u32 group_no;
2257
2258 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2259
2260 group_no = cluster / osb->bitmap_cpg;
2261 if (!group_no)
2262 return osb->first_cluster_group_blkno;
2263 return ocfs2_clusters_to_blocks(inode->i_sb,
2264 group_no * osb->bitmap_cpg);
2265}
2266
2267/* given the block number of a cluster start, calculate which cluster
2268 * group and descriptor bitmap offset that corresponds to. */
2269static inline void ocfs2_block_to_cluster_group(struct inode *inode,
2270 u64 data_blkno,
2271 u64 *bg_blkno,
2272 u16 *bg_bit_off)
2273{
2274 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2275 u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
2276
2277 BUG_ON(!ocfs2_is_cluster_bitmap(inode));
2278
2279 *bg_blkno = ocfs2_which_cluster_group(inode,
2280 data_cluster);
2281
2282 if (*bg_blkno == osb->first_cluster_group_blkno)
2283 *bg_bit_off = (u16) data_cluster;
2284 else
2285 *bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
2286 data_blkno - *bg_blkno);
2287}
2288
2289/*
2290 * min_bits - minimum contiguous chunk from this total allocation we
2291 * can handle. set to what we asked for originally for a full
2292 * contig. allocation, set to '1' to indicate we can deal with extents
2293 * of any size.
2294 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002295int __ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002296 struct ocfs2_alloc_context *ac,
2297 u32 min_clusters,
2298 u32 max_clusters,
2299 u32 *cluster_start,
2300 u32 *num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08002301{
2302 int status;
Mark Fasheh415cb802007-09-16 20:10:16 -07002303 unsigned int bits_wanted = max_clusters;
Joel Beckerba206632010-03-26 10:08:59 +08002304 struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
Joel Becker1ed9b772010-05-06 13:59:06 +08002305 struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
Mark Fashehccd979b2005-12-15 14:31:24 -08002306
Mark Fashehccd979b2005-12-15 14:31:24 -08002307 BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
2308
2309 BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
2310 && ac->ac_which != OCFS2_AC_USE_MAIN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002311
2312 if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
Mark Fasheh33d5d382010-02-24 13:34:09 -08002313 WARN_ON(min_clusters > 1);
2314
Mark Fashehccd979b2005-12-15 14:31:24 -08002315 status = ocfs2_claim_local_alloc_bits(osb,
2316 handle,
2317 ac,
2318 bits_wanted,
2319 cluster_start,
2320 num_clusters);
2321 if (!status)
2322 atomic_inc(&osb->alloc_stats.local_data);
2323 } else {
2324 if (min_clusters > (osb->bitmap_cpg - 1)) {
2325 /* The only paths asking for contiguousness
2326 * should know about this already. */
Sunil Mushran2fbe8d12007-12-20 14:58:11 -08002327 mlog(ML_ERROR, "minimum allocation requested %u exceeds "
2328 "group bitmap size %u!\n", min_clusters,
2329 osb->bitmap_cpg);
Mark Fashehccd979b2005-12-15 14:31:24 -08002330 status = -ENOSPC;
2331 goto bail;
2332 }
2333 /* clamp the current request down to a realistic size. */
2334 if (bits_wanted > (osb->bitmap_cpg - 1))
2335 bits_wanted = osb->bitmap_cpg - 1;
2336
Joel Beckeraa8f8e92010-03-26 10:08:07 +08002337 status = ocfs2_claim_suballoc_bits(ac,
Mark Fashehda5cbf22006-10-06 18:34:35 -07002338 handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002339 bits_wanted,
2340 min_clusters,
Joel Becker7d1fe092010-04-13 14:30:19 +08002341 &res);
Mark Fashehccd979b2005-12-15 14:31:24 -08002342 if (!status) {
Joel Beckerba206632010-03-26 10:08:59 +08002343 BUG_ON(res.sr_blkno); /* cluster alloc can't set */
Mark Fashehccd979b2005-12-15 14:31:24 -08002344 *cluster_start =
2345 ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
Joel Becker7d1fe092010-04-13 14:30:19 +08002346 res.sr_bg_blkno,
2347 res.sr_bit_offset);
Mark Fashehccd979b2005-12-15 14:31:24 -08002348 atomic_inc(&osb->alloc_stats.bitmap_data);
Tao Ma47119542010-04-22 14:09:15 +08002349 *num_clusters = res.sr_bits;
Mark Fashehccd979b2005-12-15 14:31:24 -08002350 }
2351 }
2352 if (status < 0) {
2353 if (status != -ENOSPC)
2354 mlog_errno(status);
2355 goto bail;
2356 }
2357
Tao Ma47119542010-04-22 14:09:15 +08002358 ac->ac_bits_given += *num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08002359
2360bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002361 if (status)
2362 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002363 return status;
2364}
2365
Joel Becker1ed9b772010-05-06 13:59:06 +08002366int ocfs2_claim_clusters(handle_t *handle,
Mark Fasheh415cb802007-09-16 20:10:16 -07002367 struct ocfs2_alloc_context *ac,
2368 u32 min_clusters,
2369 u32 *cluster_start,
2370 u32 *num_clusters)
2371{
2372 unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
2373
Joel Becker1ed9b772010-05-06 13:59:06 +08002374 return __ocfs2_claim_clusters(handle, ac, min_clusters,
Mark Fasheh415cb802007-09-16 20:10:16 -07002375 bits_wanted, cluster_start, num_clusters);
2376}
2377
Mark Fashehb4414ee2010-03-11 18:31:09 -08002378static int ocfs2_block_group_clear_bits(handle_t *handle,
2379 struct inode *alloc_inode,
2380 struct ocfs2_group_desc *bg,
2381 struct buffer_head *group_bh,
2382 unsigned int bit_off,
2383 unsigned int num_bits,
2384 void (*undo_fn)(unsigned int bit,
2385 unsigned long *bmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002386{
2387 int status;
2388 unsigned int tmp;
Mark Fashehccd979b2005-12-15 14:31:24 -08002389 struct ocfs2_group_desc *undo_bg = NULL;
2390
Joel Becker42035302008-11-13 14:49:15 -08002391 /* The caller got this descriptor from
2392 * ocfs2_read_group_descriptor(). Any corruption is a code bug. */
2393 BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
Mark Fashehccd979b2005-12-15 14:31:24 -08002394
Tao Ma2f73e132011-02-22 08:22:33 +08002395 trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
Mark Fashehccd979b2005-12-15 14:31:24 -08002396
Mark Fashehb4414ee2010-03-11 18:31:09 -08002397 BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
Joel Becker0cf2f762009-02-12 16:41:25 -08002398 status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
Mark Fashehb4414ee2010-03-11 18:31:09 -08002399 group_bh,
2400 undo_fn ?
2401 OCFS2_JOURNAL_ACCESS_UNDO :
2402 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002403 if (status < 0) {
2404 mlog_errno(status);
2405 goto bail;
2406 }
2407
Mark Fashehb4414ee2010-03-11 18:31:09 -08002408 if (undo_fn) {
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002409 jbd_lock_bh_state(group_bh);
2410 undo_bg = (struct ocfs2_group_desc *)
2411 bh2jh(group_bh)->b_committed_data;
2412 BUG_ON(!undo_bg);
2413 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002414
2415 tmp = num_bits;
2416 while(tmp--) {
2417 ocfs2_clear_bit((bit_off + tmp),
2418 (unsigned long *) bg->bg_bitmap);
Mark Fashehb4414ee2010-03-11 18:31:09 -08002419 if (undo_fn)
2420 undo_fn(bit_off + tmp,
2421 (unsigned long *) undo_bg->bg_bitmap);
Mark Fashehccd979b2005-12-15 14:31:24 -08002422 }
2423 le16_add_cpu(&bg->bg_free_bits_count, num_bits);
Srinivas Eeda9b5cd102010-10-05 15:53:06 -07002424 if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
2425 ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit"
2426 " count %u but claims %u are freed. num_bits %d",
2427 (unsigned long long)le64_to_cpu(bg->bg_blkno),
2428 le16_to_cpu(bg->bg_bits),
2429 le16_to_cpu(bg->bg_free_bits_count), num_bits);
2430 return -EROFS;
2431 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002432
Mark Fashehb4414ee2010-03-11 18:31:09 -08002433 if (undo_fn)
Sunil Mushran94e41ec2009-06-19 14:45:54 -07002434 jbd_unlock_bh_state(group_bh);
2435
Joel Beckerec20cec2010-03-19 14:13:52 -07002436 ocfs2_journal_dirty(handle, group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002437bail:
2438 return status;
2439}
2440
2441/*
2442 * expects the suballoc inode to already be locked.
2443 */
Mark Fashehb4414ee2010-03-11 18:31:09 -08002444static int _ocfs2_free_suballoc_bits(handle_t *handle,
2445 struct inode *alloc_inode,
2446 struct buffer_head *alloc_bh,
2447 unsigned int start_bit,
2448 u64 bg_blkno,
2449 unsigned int count,
2450 void (*undo_fn)(unsigned int bit,
2451 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002452{
2453 int status = 0;
2454 u32 tmp_used;
Mark Fashehccd979b2005-12-15 14:31:24 -08002455 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
2456 struct ocfs2_chain_list *cl = &fe->id2.i_chain;
2457 struct buffer_head *group_bh = NULL;
2458 struct ocfs2_group_desc *group;
2459
Joel Becker10995aa2008-11-13 14:49:12 -08002460 /* The alloc_bh comes from ocfs2_free_dinode() or
2461 * ocfs2_free_clusters(). The callers have all locked the
2462 * allocator and gotten alloc_bh from the lock call. This
2463 * validates the dinode buffer. Any corruption that has happended
2464 * is a code bug. */
2465 BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
Mark Fashehccd979b2005-12-15 14:31:24 -08002466 BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2467
Tao Ma2f73e132011-02-22 08:22:33 +08002468 trace_ocfs2_free_suballoc_bits(
2469 (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
2470 (unsigned long long)bg_blkno,
2471 start_bit, count);
Mark Fashehccd979b2005-12-15 14:31:24 -08002472
Joel Becker68f64d42008-11-13 14:49:14 -08002473 status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
2474 &group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002475 if (status < 0) {
2476 mlog_errno(status);
2477 goto bail;
2478 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002479 group = (struct ocfs2_group_desc *) group_bh->b_data;
Joel Becker68f64d42008-11-13 14:49:14 -08002480
Mark Fashehccd979b2005-12-15 14:31:24 -08002481 BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
2482
2483 status = ocfs2_block_group_clear_bits(handle, alloc_inode,
2484 group, group_bh,
Mark Fashehb4414ee2010-03-11 18:31:09 -08002485 start_bit, count, undo_fn);
Mark Fashehccd979b2005-12-15 14:31:24 -08002486 if (status < 0) {
2487 mlog_errno(status);
2488 goto bail;
2489 }
2490
Joel Becker0cf2f762009-02-12 16:41:25 -08002491 status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
2492 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08002493 if (status < 0) {
2494 mlog_errno(status);
2495 goto bail;
2496 }
2497
2498 le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
2499 count);
2500 tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
2501 fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
Joel Beckerec20cec2010-03-19 14:13:52 -07002502 ocfs2_journal_dirty(handle, alloc_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002503
2504bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002505 brelse(group_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08002506
Tao Mac1e8d352011-03-07 16:43:21 +08002507 if (status)
2508 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002509 return status;
2510}
2511
Mark Fashehb4414ee2010-03-11 18:31:09 -08002512int ocfs2_free_suballoc_bits(handle_t *handle,
2513 struct inode *alloc_inode,
2514 struct buffer_head *alloc_bh,
2515 unsigned int start_bit,
2516 u64 bg_blkno,
2517 unsigned int count)
2518{
2519 return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
2520 start_bit, bg_blkno, count, NULL);
2521}
2522
Mark Fasheh1fabe142006-10-09 18:11:45 -07002523int ocfs2_free_dinode(handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08002524 struct inode *inode_alloc_inode,
2525 struct buffer_head *inode_alloc_bh,
2526 struct ocfs2_dinode *di)
2527{
2528 u64 blk = le64_to_cpu(di->i_blkno);
2529 u16 bit = le16_to_cpu(di->i_suballoc_bit);
2530 u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
2531
Tao Ma74380c42010-03-22 14:20:18 +08002532 if (di->i_suballoc_loc)
2533 bg_blkno = le64_to_cpu(di->i_suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08002534 return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
2535 inode_alloc_bh, bit, bg_blkno, 1);
2536}
2537
Mark Fashehb4414ee2010-03-11 18:31:09 -08002538static int _ocfs2_free_clusters(handle_t *handle,
2539 struct inode *bitmap_inode,
2540 struct buffer_head *bitmap_bh,
2541 u64 start_blk,
2542 unsigned int num_clusters,
2543 void (*undo_fn)(unsigned int bit,
2544 unsigned long *bitmap))
Mark Fashehccd979b2005-12-15 14:31:24 -08002545{
2546 int status;
2547 u16 bg_start_bit;
2548 u64 bg_blkno;
2549 struct ocfs2_dinode *fe;
2550
2551 /* You can't ever have a contiguous set of clusters
2552 * bigger than a block group bitmap so we never have to worry
Tao Maef6b6892011-02-21 11:10:44 +08002553 * about looping on them.
2554 * This is expensive. We can safely remove once this stuff has
Mark Fashehccd979b2005-12-15 14:31:24 -08002555 * gotten tested really well. */
2556 BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
2557
2558 fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
2559
2560 ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
2561 &bg_start_bit);
2562
Tao Ma2f73e132011-02-22 08:22:33 +08002563 trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
2564 (unsigned long long)start_blk,
2565 bg_start_bit, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08002566
Mark Fashehb4414ee2010-03-11 18:31:09 -08002567 status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
2568 bg_start_bit, bg_blkno,
2569 num_clusters, undo_fn);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002570 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08002571 mlog_errno(status);
Mark Fasheh9c7af402008-07-28 18:02:53 -07002572 goto out;
2573 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002574
Mark Fasheh9c7af402008-07-28 18:02:53 -07002575 ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
2576 num_clusters);
2577
2578out:
Tao Mac1e8d352011-03-07 16:43:21 +08002579 if (status)
2580 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08002581 return status;
2582}
2583
Mark Fashehb4414ee2010-03-11 18:31:09 -08002584int ocfs2_free_clusters(handle_t *handle,
2585 struct inode *bitmap_inode,
2586 struct buffer_head *bitmap_bh,
2587 u64 start_blk,
2588 unsigned int num_clusters)
2589{
2590 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2591 start_blk, num_clusters,
2592 _ocfs2_set_bit);
2593}
2594
2595/*
2596 * Give never-used clusters back to the global bitmap. We don't need
2597 * to protect these bits in the undo buffer.
2598 */
2599int ocfs2_release_clusters(handle_t *handle,
2600 struct inode *bitmap_inode,
2601 struct buffer_head *bitmap_bh,
2602 u64 start_blk,
2603 unsigned int num_clusters)
2604{
2605 return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
2606 start_blk, num_clusters,
2607 _ocfs2_clear_bit);
2608}
2609
Mark Fashehccd979b2005-12-15 14:31:24 -08002610static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
2611{
2612 printk("Block Group:\n");
2613 printk("bg_signature: %s\n", bg->bg_signature);
2614 printk("bg_size: %u\n", bg->bg_size);
2615 printk("bg_bits: %u\n", bg->bg_bits);
2616 printk("bg_free_bits_count: %u\n", bg->bg_free_bits_count);
2617 printk("bg_chain: %u\n", bg->bg_chain);
2618 printk("bg_generation: %u\n", le32_to_cpu(bg->bg_generation));
Mark Fashehb06970532006-03-03 10:24:33 -08002619 printk("bg_next_group: %llu\n",
2620 (unsigned long long)bg->bg_next_group);
2621 printk("bg_parent_dinode: %llu\n",
2622 (unsigned long long)bg->bg_parent_dinode);
2623 printk("bg_blkno: %llu\n",
2624 (unsigned long long)bg->bg_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002625}
2626
2627static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
2628{
2629 int i;
2630
Mark Fashehb06970532006-03-03 10:24:33 -08002631 printk("Suballoc Inode %llu:\n", (unsigned long long)fe->i_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002632 printk("i_signature: %s\n", fe->i_signature);
Mark Fashehb06970532006-03-03 10:24:33 -08002633 printk("i_size: %llu\n",
2634 (unsigned long long)fe->i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002635 printk("i_clusters: %u\n", fe->i_clusters);
2636 printk("i_generation: %u\n",
2637 le32_to_cpu(fe->i_generation));
2638 printk("id1.bitmap1.i_used: %u\n",
2639 le32_to_cpu(fe->id1.bitmap1.i_used));
2640 printk("id1.bitmap1.i_total: %u\n",
2641 le32_to_cpu(fe->id1.bitmap1.i_total));
2642 printk("id2.i_chain.cl_cpg: %u\n", fe->id2.i_chain.cl_cpg);
2643 printk("id2.i_chain.cl_bpc: %u\n", fe->id2.i_chain.cl_bpc);
2644 printk("id2.i_chain.cl_count: %u\n", fe->id2.i_chain.cl_count);
2645 printk("id2.i_chain.cl_next_free_rec: %u\n",
2646 fe->id2.i_chain.cl_next_free_rec);
2647 for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
2648 printk("fe->id2.i_chain.cl_recs[%d].c_free: %u\n", i,
2649 fe->id2.i_chain.cl_recs[i].c_free);
2650 printk("fe->id2.i_chain.cl_recs[%d].c_total: %u\n", i,
2651 fe->id2.i_chain.cl_recs[i].c_total);
Mark Fashehb06970532006-03-03 10:24:33 -08002652 printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu\n", i,
2653 (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002654 }
2655}
Tao Mae7d4cb62008-08-18 17:38:44 +08002656
2657/*
2658 * For a given allocation, determine which allocators will need to be
2659 * accessed, and lock them, reserving the appropriate number of bits.
2660 *
2661 * Sparse file systems call this from ocfs2_write_begin_nolock()
2662 * and ocfs2_allocate_unwritten_extents().
2663 *
2664 * File systems which don't support holes call this from
2665 * ocfs2_extend_allocation().
2666 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002667int ocfs2_lock_allocators(struct inode *inode,
2668 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002669 u32 clusters_to_add, u32 extents_to_split,
2670 struct ocfs2_alloc_context **data_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07002671 struct ocfs2_alloc_context **meta_ac)
Tao Mae7d4cb62008-08-18 17:38:44 +08002672{
2673 int ret = 0, num_free_extents;
2674 unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
2675 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2676
2677 *meta_ac = NULL;
2678 if (data_ac)
2679 *data_ac = NULL;
2680
2681 BUG_ON(clusters_to_add != 0 && data_ac == NULL);
2682
Joel Becker3d03a302009-02-12 17:49:26 -08002683 num_free_extents = ocfs2_num_free_extents(osb, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002684 if (num_free_extents < 0) {
2685 ret = num_free_extents;
2686 mlog_errno(ret);
2687 goto out;
2688 }
2689
2690 /*
2691 * Sparse allocation file systems need to be more conservative
2692 * with reserving room for expansion - the actual allocation
2693 * happens while we've got a journal handle open so re-taking
2694 * a cluster lock (because we ran out of room for another
2695 * extent) will violate ordering rules.
2696 *
2697 * Most of the time we'll only be seeing this 1 cluster at a time
2698 * anyway.
2699 *
2700 * Always lock for any unwritten extents - we might want to
2701 * add blocks during a split.
2702 */
2703 if (!num_free_extents ||
2704 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
Joel Beckerf99b9b72008-08-20 19:36:33 -07002705 ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
Tao Mae7d4cb62008-08-18 17:38:44 +08002706 if (ret < 0) {
2707 if (ret != -ENOSPC)
2708 mlog_errno(ret);
2709 goto out;
2710 }
2711 }
2712
2713 if (clusters_to_add == 0)
2714 goto out;
2715
2716 ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
2717 if (ret < 0) {
2718 if (ret != -ENOSPC)
2719 mlog_errno(ret);
2720 goto out;
2721 }
2722
2723out:
2724 if (ret) {
2725 if (*meta_ac) {
2726 ocfs2_free_alloc_context(*meta_ac);
2727 *meta_ac = NULL;
2728 }
2729
2730 /*
2731 * We cannot have an error and a non null *data_ac.
2732 */
2733 }
2734
2735 return ret;
2736}
wengang wang6ca497a2009-03-06 21:29:10 +08002737
2738/*
2739 * Read the inode specified by blkno to get suballoc_slot and
2740 * suballoc_bit.
2741 */
2742static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
Tao Ma889f0042010-09-02 13:10:10 +08002743 u16 *suballoc_slot, u64 *group_blkno,
2744 u16 *suballoc_bit)
wengang wang6ca497a2009-03-06 21:29:10 +08002745{
2746 int status;
2747 struct buffer_head *inode_bh = NULL;
2748 struct ocfs2_dinode *inode_fe;
2749
Tao Ma2f73e132011-02-22 08:22:33 +08002750 trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002751
2752 /* dirty read disk */
2753 status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
2754 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002755 mlog(ML_ERROR, "read block %llu failed %d\n",
2756 (unsigned long long)blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002757 goto bail;
2758 }
2759
2760 inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
2761 if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
Joel Becker5b09b502009-04-21 16:31:20 -07002762 mlog(ML_ERROR, "invalid inode %llu requested\n",
2763 (unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002764 status = -EINVAL;
2765 goto bail;
2766 }
2767
Tao Ma0fba8132009-03-19 05:08:43 +08002768 if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
wengang wang6ca497a2009-03-06 21:29:10 +08002769 (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
2770 mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u\n",
Joel Becker5b09b502009-04-21 16:31:20 -07002771 (unsigned long long)blkno,
2772 (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
wengang wang6ca497a2009-03-06 21:29:10 +08002773 status = -EINVAL;
2774 goto bail;
2775 }
2776
2777 if (suballoc_slot)
2778 *suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
2779 if (suballoc_bit)
2780 *suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
Tao Ma889f0042010-09-02 13:10:10 +08002781 if (group_blkno)
2782 *group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
wengang wang6ca497a2009-03-06 21:29:10 +08002783
2784bail:
2785 brelse(inode_bh);
2786
Tao Mac1e8d352011-03-07 16:43:21 +08002787 if (status)
2788 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002789 return status;
2790}
2791
2792/*
2793 * test whether bit is SET in allocator bitmap or not. on success, 0
2794 * is returned and *res is 1 for SET; 0 otherwise. when fails, errno
2795 * is returned and *res is meaningless. Call this after you have
2796 * cluster locked against suballoc, or you may get a result based on
2797 * non-up2date contents
2798 */
2799static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
2800 struct inode *suballoc,
Tao Ma889f0042010-09-02 13:10:10 +08002801 struct buffer_head *alloc_bh,
2802 u64 group_blkno, u64 blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002803 u16 bit, int *res)
2804{
Tao Maabf1b3c2010-04-27 08:30:36 +08002805 struct ocfs2_dinode *alloc_di;
wengang wang6ca497a2009-03-06 21:29:10 +08002806 struct ocfs2_group_desc *group;
2807 struct buffer_head *group_bh = NULL;
2808 u64 bg_blkno;
2809 int status;
2810
Tao Ma2f73e132011-02-22 08:22:33 +08002811 trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
2812 (unsigned int)bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002813
Tao Maabf1b3c2010-04-27 08:30:36 +08002814 alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
2815 if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
wengang wang6ca497a2009-03-06 21:29:10 +08002816 mlog(ML_ERROR, "suballoc bit %u out of range of %u\n",
2817 (unsigned int)bit,
Tao Maabf1b3c2010-04-27 08:30:36 +08002818 ocfs2_bits_per_group(&alloc_di->id2.i_chain));
wengang wang6ca497a2009-03-06 21:29:10 +08002819 status = -EINVAL;
2820 goto bail;
2821 }
2822
Tao Ma889f0042010-09-02 13:10:10 +08002823 bg_blkno = group_blkno ? group_blkno :
2824 ocfs2_which_suballoc_group(blkno, bit);
Tao Maabf1b3c2010-04-27 08:30:36 +08002825 status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
wengang wang6ca497a2009-03-06 21:29:10 +08002826 &group_bh);
2827 if (status < 0) {
Joel Becker5b09b502009-04-21 16:31:20 -07002828 mlog(ML_ERROR, "read group %llu failed %d\n",
2829 (unsigned long long)bg_blkno, status);
wengang wang6ca497a2009-03-06 21:29:10 +08002830 goto bail;
2831 }
2832
2833 group = (struct ocfs2_group_desc *) group_bh->b_data;
2834 *res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
2835
2836bail:
2837 brelse(group_bh);
2838
Tao Mac1e8d352011-03-07 16:43:21 +08002839 if (status)
2840 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002841 return status;
2842}
2843
2844/*
2845 * Test if the bit representing this inode (blkno) is set in the
2846 * suballocator.
2847 *
2848 * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
2849 *
2850 * In the event of failure, a negative value is returned and *res is
2851 * meaningless.
2852 *
2853 * Callers must make sure to hold nfs_sync_lock to prevent
2854 * ocfs2_delete_inode() on another node from accessing the same
2855 * suballocator concurrently.
2856 */
2857int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
2858{
2859 int status;
Tao Ma889f0042010-09-02 13:10:10 +08002860 u64 group_blkno = 0;
wengang wang6ca497a2009-03-06 21:29:10 +08002861 u16 suballoc_bit = 0, suballoc_slot = 0;
2862 struct inode *inode_alloc_inode;
2863 struct buffer_head *alloc_bh = NULL;
2864
Tao Ma2f73e132011-02-22 08:22:33 +08002865 trace_ocfs2_test_inode_bit((unsigned long long)blkno);
wengang wang6ca497a2009-03-06 21:29:10 +08002866
2867 status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
Tao Ma889f0042010-09-02 13:10:10 +08002868 &group_blkno, &suballoc_bit);
wengang wang6ca497a2009-03-06 21:29:10 +08002869 if (status < 0) {
2870 mlog(ML_ERROR, "get alloc slot and bit failed %d\n", status);
2871 goto bail;
2872 }
2873
2874 inode_alloc_inode =
2875 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
2876 suballoc_slot);
2877 if (!inode_alloc_inode) {
2878 /* the error code could be inaccurate, but we are not able to
2879 * get the correct one. */
2880 status = -EINVAL;
2881 mlog(ML_ERROR, "unable to get alloc inode in slot %u\n",
2882 (u32)suballoc_slot);
2883 goto bail;
2884 }
2885
2886 mutex_lock(&inode_alloc_inode->i_mutex);
2887 status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
2888 if (status < 0) {
2889 mutex_unlock(&inode_alloc_inode->i_mutex);
2890 mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d\n",
2891 (u32)suballoc_slot, status);
2892 goto bail;
2893 }
2894
2895 status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
Tao Ma889f0042010-09-02 13:10:10 +08002896 group_blkno, blkno, suballoc_bit, res);
wengang wang6ca497a2009-03-06 21:29:10 +08002897 if (status < 0)
2898 mlog(ML_ERROR, "test suballoc bit failed %d\n", status);
2899
2900 ocfs2_inode_unlock(inode_alloc_inode, 0);
2901 mutex_unlock(&inode_alloc_inode->i_mutex);
2902
2903 iput(inode_alloc_inode);
2904 brelse(alloc_bh);
2905bail:
Tao Mac1e8d352011-03-07 16:43:21 +08002906 if (status)
2907 mlog_errno(status);
wengang wang6ca497a2009-03-06 21:29:10 +08002908 return status;
2909}