Blame view

fs/ocfs2/suballoc.c 74.8 KB
328970de0   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2
3
4
5
6
7
8
9
10
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * suballoc.c
   *
   * metadata alloc and free
   * Inspired by ext3 block groups.
   *
   * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
11
12
13
14
15
16
   */
  
  #include <linux/fs.h>
  #include <linux/types.h>
  #include <linux/slab.h>
  #include <linux/highmem.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
17
18
19
20
21
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
  
  #include "alloc.h"
d6b32bbb3   Joel Becker   ocfs2: block read...
22
  #include "blockcheck.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
23
24
25
26
27
28
29
30
  #include "dlmglue.h"
  #include "inode.h"
  #include "journal.h"
  #include "localalloc.h"
  #include "suballoc.h"
  #include "super.h"
  #include "sysfile.h"
  #include "uptodate.h"
2f73e135b   Tao Ma   ocfs2: Remove mlo...
31
  #include "ocfs2_trace.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
32
33
  
  #include "buffer_head_io.h"
ffda89a3b   Tao Ma   ocfs2: Add a new ...
34
  #define NOT_ALLOC_NEW_GROUP		0
60ca81e82   Tao Ma   ocfs2: Allocate i...
35
36
  #define ALLOC_NEW_GROUP			0x1
  #define ALLOC_GROUPS_FROM_GLOBAL	0x2
ffda89a3b   Tao Ma   ocfs2: Add a new ...
37

b89c54282   Tiger Yang   ocfs2: add extent...
38
  #define OCFS2_MAX_TO_STEAL		1024
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
39

7d1fe093b   Joel Becker   ocfs2: Pass subal...
40
  struct ocfs2_suballoc_result {
2b6cb576a   Joel Becker   ocfs2: Set suball...
41
42
43
  	u64		sr_bg_blkno;	/* The bg we allocated from.  Set
  					   to 0 when a block group is
  					   contiguous. */
e49e27674   Mark Fasheh   ocfs2: allow retu...
44
45
46
47
48
49
  	u64		sr_bg_stable_blkno; /*
  					     * Doesn't change, always
  					     * set to target block
  					     * group descriptor
  					     * block.
  					     */
ba2066351   Joel Becker   ocfs2: Return all...
50
  	u64		sr_blkno;	/* The first allocated block */
7d1fe093b   Joel Becker   ocfs2: Pass subal...
51
52
53
  	unsigned int	sr_bit_offset;	/* The bit in the bg */
  	unsigned int	sr_bits;	/* How many bits we claimed */
  };
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
54
55
56
57
58
59
60
61
62
63
  static u64 ocfs2_group_from_res(struct ocfs2_suballoc_result *res)
  {
  	if (res->sr_blkno == 0)
  		return 0;
  
  	if (res->sr_bg_blkno)
  		return res->sr_bg_blkno;
  
  	return ocfs2_which_suballoc_group(res->sr_blkno, res->sr_bit_offset);
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
64
  static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
65
  static int ocfs2_block_group_fill(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
66
67
68
  				  struct inode *alloc_inode,
  				  struct buffer_head *bg_bh,
  				  u64 group_blkno,
798db35f4   Joel Becker   ocfs2: Allocate d...
69
  				  unsigned int group_clusters,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
70
71
72
73
  				  u16 my_chain,
  				  struct ocfs2_chain_list *cl);
  static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  				   struct inode *alloc_inode,
1187c9688   Joel Becker   ocfs2: Limit inod...
74
  				   struct buffer_head *bh,
60ca81e82   Tao Ma   ocfs2: Allocate i...
75
  				   u64 max_block,
feb473a6e   Tao Ma   ocfs2: Optimize i...
76
  				   u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
77
  				   int flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
78

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
79
80
81
  static int ocfs2_cluster_group_search(struct inode *inode,
  				      struct buffer_head *group_bh,
  				      u32 bits_wanted, u32 min_bits,
1187c9688   Joel Becker   ocfs2: Limit inod...
82
  				      u64 max_block,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
83
  				      struct ocfs2_suballoc_result *res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
84
85
86
  static int ocfs2_block_group_search(struct inode *inode,
  				    struct buffer_head *group_bh,
  				    u32 bits_wanted, u32 min_bits,
1187c9688   Joel Becker   ocfs2: Limit inod...
87
  				    u64 max_block,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
88
  				    struct ocfs2_suballoc_result *res);
aa8f8e93c   Joel Becker   ocfs2: ocfs2_clai...
89
  static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
90
  				     handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
91
92
  				     u32 bits_wanted,
  				     u32 min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
93
  				     struct ocfs2_suballoc_result *res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
94
95
  static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  					 int nr);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
96
  static int ocfs2_relink_block_group(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
97
98
99
100
101
102
103
  				    struct inode *alloc_inode,
  				    struct buffer_head *fe_bh,
  				    struct buffer_head *bg_bh,
  				    struct buffer_head *prev_bg_bh,
  				    u16 chain);
  static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  						     u32 wanted);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
104
105
106
  static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  						   u64 bg_blkno,
  						   u16 bg_bit_off);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
107
108
109
110
  static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  						u64 data_blkno,
  						u64 *bg_blkno,
  						u16 *bg_bit_off);
1187c9688   Joel Becker   ocfs2: Limit inod...
111
112
  static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  					     u32 bits_wanted, u64 max_block,
60ca81e82   Tao Ma   ocfs2: Allocate i...
113
  					     int flags,
1187c9688   Joel Becker   ocfs2: Limit inod...
114
  					     struct ocfs2_alloc_context **ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
115

9c7af40b2   Mark Fasheh   ocfs2: throttle b...
116
  void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
117
  {
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
118
119
120
121
  	struct inode *inode = ac->ac_inode;
  
  	if (inode) {
  		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
122
  			ocfs2_inode_unlock(inode, 1);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
123

5955102c9   Al Viro   wrappers for ->i_...
124
  		inode_unlock(inode);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
125
126
  
  		iput(inode);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
127
  		ac->ac_inode = NULL;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
128
  	}
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
129
130
  	brelse(ac->ac_bh);
  	ac->ac_bh = NULL;
e3b4a97db   Mark Fasheh   ocfs2: use alloca...
131
  	ac->ac_resv = NULL;
46359295a   Joseph Qi   ocfs2: clean up r...
132
133
  	kfree(ac->ac_find_loc_priv);
  	ac->ac_find_loc_priv = NULL;
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
134
135
136
137
138
  }
  
  void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
  {
  	ocfs2_free_ac_resource(ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
139
140
141
142
143
144
145
  	kfree(ac);
  }
  
  static u32 ocfs2_bits_per_group(struct ocfs2_chain_list *cl)
  {
  	return (u32)le16_to_cpu(cl->cl_cpg) * (u32)le16_to_cpu(cl->cl_bpc);
  }
57e3e7971   Joel Becker   ocfs2: Consolidat...
146
  #define do_error(fmt, ...)						\
7ecef14ab   Joe Perches   ocfs2: neaten do_...
147
148
149
150
151
152
  do {									\
  	if (resize)							\
  		mlog(ML_ERROR, fmt, ##__VA_ARGS__);			\
  	else								\
  		return ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
  } while (0)
57e3e7971   Joel Becker   ocfs2: Consolidat...
153

970e4936d   Joel Becker   ocfs2: Validate m...
154
155
  static int ocfs2_validate_gd_self(struct super_block *sb,
  				  struct buffer_head *bh,
78c37eb0d   Tao Ma   ocfs2: Change bg_...
156
  				  int resize)
970e4936d   Joel Becker   ocfs2: Validate m...
157
158
  {
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
7bf72edee   Mark Fasheh   ocfs2: better gro...
159
  	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
160
161
  		do_error("Group descriptor #%llu has bad signature %.*s
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
162
  			 (unsigned long long)bh->b_blocknr, 7,
57e3e7971   Joel Becker   ocfs2: Consolidat...
163
  			 gd->bg_signature);
7bf72edee   Mark Fasheh   ocfs2: better gro...
164
  	}
68f64d471   Joel Becker   ocfs2: Wrap group...
165
  	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
166
167
  		do_error("Group descriptor #%llu has an invalid bg_blkno of %llu
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
168
169
  			 (unsigned long long)bh->b_blocknr,
  			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
68f64d471   Joel Becker   ocfs2: Wrap group...
170
171
172
  	}
  
  	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
173
174
  		do_error("Group descriptor #%llu has an invalid fs_generation of #%u
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
175
176
  			 (unsigned long long)bh->b_blocknr,
  			 le32_to_cpu(gd->bg_generation));
68f64d471   Joel Becker   ocfs2: Wrap group...
177
  	}
970e4936d   Joel Becker   ocfs2: Validate m...
178
  	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
179
180
  		do_error("Group descriptor #%llu has bit count %u but claims that %u are free
  ",
970e4936d   Joel Becker   ocfs2: Validate m...
181
182
183
  			 (unsigned long long)bh->b_blocknr,
  			 le16_to_cpu(gd->bg_bits),
  			 le16_to_cpu(gd->bg_free_bits_count));
970e4936d   Joel Becker   ocfs2: Validate m...
184
185
186
  	}
  
  	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
187
188
  		do_error("Group descriptor #%llu has bit count %u but max bitmap bits of %u
  ",
970e4936d   Joel Becker   ocfs2: Validate m...
189
190
191
  			 (unsigned long long)bh->b_blocknr,
  			 le16_to_cpu(gd->bg_bits),
  			 8 * le16_to_cpu(gd->bg_size));
970e4936d   Joel Becker   ocfs2: Validate m...
192
193
194
195
196
197
198
199
  	}
  
  	return 0;
  }
  
  static int ocfs2_validate_gd_parent(struct super_block *sb,
  				    struct ocfs2_dinode *di,
  				    struct buffer_head *bh,
78c37eb0d   Tao Ma   ocfs2: Change bg_...
200
  				    int resize)
970e4936d   Joel Becker   ocfs2: Validate m...
201
202
203
  {
  	unsigned int max_bits;
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
7bf72edee   Mark Fasheh   ocfs2: better gro...
204
  	if (di->i_blkno != gd->bg_parent_dinode) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
205
206
  		do_error("Group descriptor #%llu has bad parent pointer (%llu, expected %llu)
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
207
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
208
209
  			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
  			 (unsigned long long)le64_to_cpu(di->i_blkno));
7bf72edee   Mark Fasheh   ocfs2: better gro...
210
211
212
213
  	}
  
  	max_bits = le16_to_cpu(di->id2.i_chain.cl_cpg) * le16_to_cpu(di->id2.i_chain.cl_bpc);
  	if (le16_to_cpu(gd->bg_bits) > max_bits) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
214
215
  		do_error("Group descriptor #%llu has bit count of %u
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
216
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
217
  			 le16_to_cpu(gd->bg_bits));
7bf72edee   Mark Fasheh   ocfs2: better gro...
218
  	}
78c37eb0d   Tao Ma   ocfs2: Change bg_...
219
220
221
222
223
  	/* In resize, we may meet the case bg_chain == cl_next_free_rec. */
  	if ((le16_to_cpu(gd->bg_chain) >
  	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) ||
  	    ((le16_to_cpu(gd->bg_chain) ==
  	     le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) && !resize)) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
224
225
  		do_error("Group descriptor #%llu has bad chain %u
  ",
68f64d471   Joel Becker   ocfs2: Wrap group...
226
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
227
  			 le16_to_cpu(gd->bg_chain));
7bf72edee   Mark Fasheh   ocfs2: better gro...
228
  	}
970e4936d   Joel Becker   ocfs2: Validate m...
229
230
  	return 0;
  }
7bf72edee   Mark Fasheh   ocfs2: better gro...
231

57e3e7971   Joel Becker   ocfs2: Consolidat...
232
  #undef do_error
7bf72edee   Mark Fasheh   ocfs2: better gro...
233

970e4936d   Joel Becker   ocfs2: Validate m...
234
235
236
237
238
239
240
241
242
  /*
   * This version only prints errors.  It does not fail the filesystem, and
   * exists only for resize.
   */
  int ocfs2_check_group_descriptor(struct super_block *sb,
  				 struct ocfs2_dinode *di,
  				 struct buffer_head *bh)
  {
  	int rc;
d6b32bbb3   Joel Becker   ocfs2: block read...
243
244
245
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  
  	BUG_ON(!buffer_uptodate(bh));
970e4936d   Joel Becker   ocfs2: Validate m...
246

d6b32bbb3   Joel Becker   ocfs2: block read...
247
248
249
250
251
252
  	/*
  	 * If the ecc fails, we return the error but otherwise
  	 * leave the filesystem running.  We know any error is
  	 * local to this block.
  	 */
  	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
13723d00e   Joel Becker   ocfs2: Use metada...
253
254
255
256
257
258
  	if (rc) {
  		mlog(ML_ERROR,
  		     "Checksum failed for group descriptor %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
  	} else
d6b32bbb3   Joel Becker   ocfs2: block read...
259
  		rc = ocfs2_validate_gd_self(sb, bh, 1);
970e4936d   Joel Becker   ocfs2: Validate m...
260
261
262
263
264
265
266
267
268
  	if (!rc)
  		rc = ocfs2_validate_gd_parent(sb, di, bh, 1);
  
  	return rc;
  }
  
  static int ocfs2_validate_group_descriptor(struct super_block *sb,
  					   struct buffer_head *bh)
  {
d6b32bbb3   Joel Becker   ocfs2: block read...
269
270
  	int rc;
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
271
272
  	trace_ocfs2_validate_group_descriptor(
  					(unsigned long long)bh->b_blocknr);
970e4936d   Joel Becker   ocfs2: Validate m...
273

d6b32bbb3   Joel Becker   ocfs2: block read...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  	BUG_ON(!buffer_uptodate(bh));
  
  	/*
  	 * If the ecc fails, we return the error but otherwise
  	 * leave the filesystem running.  We know any error is
  	 * local to this block.
  	 */
  	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &gd->bg_check);
  	if (rc)
  		return rc;
  
  	/*
  	 * Errors after here are fatal.
  	 */
970e4936d   Joel Becker   ocfs2: Validate m...
288
  	return ocfs2_validate_gd_self(sb, bh, 0);
7bf72edee   Mark Fasheh   ocfs2: better gro...
289
  }
68f64d471   Joel Becker   ocfs2: Wrap group...
290
291
292
293
294
  int ocfs2_read_group_descriptor(struct inode *inode, struct ocfs2_dinode *di,
  				u64 gd_blkno, struct buffer_head **bh)
  {
  	int rc;
  	struct buffer_head *tmp = *bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
295
  	rc = ocfs2_read_block(INODE_CACHE(inode), gd_blkno, &tmp,
970e4936d   Joel Becker   ocfs2: Validate m...
296
  			      ocfs2_validate_group_descriptor);
68f64d471   Joel Becker   ocfs2: Wrap group...
297
298
  	if (rc)
  		goto out;
970e4936d   Joel Becker   ocfs2: Validate m...
299
  	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
68f64d471   Joel Becker   ocfs2: Wrap group...
300
301
302
303
304
305
306
307
308
309
310
311
  	if (rc) {
  		brelse(tmp);
  		goto out;
  	}
  
  	/* If ocfs2_read_block() got us a new bh, pass it up. */
  	if (!*bh)
  		*bh = tmp;
  
  out:
  	return rc;
  }
798db35f4   Joel Becker   ocfs2: Allocate d...
312
313
314
  static void ocfs2_bg_discontig_add_extent(struct ocfs2_super *osb,
  					  struct ocfs2_group_desc *bg,
  					  struct ocfs2_chain_list *cl,
47dea4237   Tao Ma   ocfs2: Use cpu_to...
315
  					  u64 p_blkno, unsigned int clusters)
798db35f4   Joel Becker   ocfs2: Allocate d...
316
317
318
  {
  	struct ocfs2_extent_list *el = &bg->bg_list;
  	struct ocfs2_extent_rec *rec;
4711954ea   Tao Ma   ocfs2: Some tiny ...
319
  	BUG_ON(!ocfs2_supports_discontig_bg(osb));
798db35f4   Joel Becker   ocfs2: Allocate d...
320
321
322
  	if (!el->l_next_free_rec)
  		el->l_count = cpu_to_le16(ocfs2_extent_recs_per_gd(osb->sb));
  	rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec)];
4711954ea   Tao Ma   ocfs2: Some tiny ...
323
  	rec->e_blkno = cpu_to_le64(p_blkno);
798db35f4   Joel Becker   ocfs2: Allocate d...
324
325
  	rec->e_cpos = cpu_to_le32(le16_to_cpu(bg->bg_bits) /
  				  le16_to_cpu(cl->cl_bpc));
47dea4237   Tao Ma   ocfs2: Use cpu_to...
326
  	rec->e_leaf_clusters = cpu_to_le16(clusters);
798db35f4   Joel Becker   ocfs2: Allocate d...
327
  	le16_add_cpu(&bg->bg_bits, clusters * le16_to_cpu(cl->cl_bpc));
4711954ea   Tao Ma   ocfs2: Some tiny ...
328
329
  	le16_add_cpu(&bg->bg_free_bits_count,
  		     clusters * le16_to_cpu(cl->cl_bpc));
798db35f4   Joel Becker   ocfs2: Allocate d...
330
331
  	le16_add_cpu(&el->l_next_free_rec, 1);
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
332
  static int ocfs2_block_group_fill(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
333
334
335
  				  struct inode *alloc_inode,
  				  struct buffer_head *bg_bh,
  				  u64 group_blkno,
798db35f4   Joel Becker   ocfs2: Allocate d...
336
  				  unsigned int group_clusters,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
337
338
339
340
  				  u16 my_chain,
  				  struct ocfs2_chain_list *cl)
  {
  	int status = 0;
798db35f4   Joel Becker   ocfs2: Allocate d...
341
  	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
342
343
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  	struct super_block * sb = alloc_inode->i_sb;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
344
  	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
345
346
347
348
349
  		status = ocfs2_error(alloc_inode->i_sb,
  				     "group block (%llu) != b_blocknr (%llu)
  ",
  				     (unsigned long long)group_blkno,
  				     (unsigned long long) bg_bh->b_blocknr);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
350
351
  		goto bail;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
352
  	status = ocfs2_journal_access_gd(handle,
0cf2f7632   Joel Becker   ocfs2: Pass struc...
353
  					 INODE_CACHE(alloc_inode),
13723d00e   Joel Becker   ocfs2: Use metada...
354
355
  					 bg_bh,
  					 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
356
357
358
359
360
361
362
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	memset(bg, 0, sb->s_blocksize);
  	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
1119d3c06   piaojun   ocfs2: use 'osb' ...
363
  	bg->bg_generation = cpu_to_le32(osb->fs_generation);
8571882c2   Tao Ma   ocfs2: ocfs2_grou...
364
365
  	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb, 1,
  						osb->s_feature_incompat));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
366
367
368
369
  	bg->bg_chain = cpu_to_le16(my_chain);
  	bg->bg_next_group = cl->cl_recs[my_chain].c_blkno;
  	bg->bg_parent_dinode = cpu_to_le64(OCFS2_I(alloc_inode)->ip_blkno);
  	bg->bg_blkno = cpu_to_le64(group_blkno);
798db35f4   Joel Becker   ocfs2: Allocate d...
370
371
372
  	if (group_clusters == le16_to_cpu(cl->cl_cpg))
  		bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  	else
4711954ea   Tao Ma   ocfs2: Some tiny ...
373
  		ocfs2_bg_discontig_add_extent(osb, bg, cl, group_blkno,
798db35f4   Joel Becker   ocfs2: Allocate d...
374
  					      group_clusters);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
375
376
377
  	/* set the 1st bit in the bitmap to account for the descriptor block */
  	ocfs2_set_bit(0, (unsigned long *)bg->bg_bitmap);
  	bg->bg_free_bits_count = cpu_to_le16(le16_to_cpu(bg->bg_bits) - 1);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
378
  	ocfs2_journal_dirty(handle, bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
379
380
381
382
383
384
385
  
  	/* There is no need to zero out or otherwise initialize the
  	 * other blocks in a group - All valid FS metadata in a block
  	 * group stores the superblock fs_generation value at
  	 * allocation time. */
  
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
386
387
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  	return status;
  }
  
  static inline u16 ocfs2_find_smallest_chain(struct ocfs2_chain_list *cl)
  {
  	u16 curr, best;
  
  	best = curr = 0;
  	while (curr < le16_to_cpu(cl->cl_count)) {
  		if (le32_to_cpu(cl->cl_recs[best].c_total) >
  		    le32_to_cpu(cl->cl_recs[curr].c_total))
  			best = curr;
  		curr++;
  	}
  	return best;
  }
798db35f4   Joel Becker   ocfs2: Allocate d...
404
405
406
407
408
409
410
411
412
413
414
  static struct buffer_head *
  ocfs2_block_group_alloc_contig(struct ocfs2_super *osb, handle_t *handle,
  			       struct inode *alloc_inode,
  			       struct ocfs2_alloc_context *ac,
  			       struct ocfs2_chain_list *cl)
  {
  	int status;
  	u32 bit_off, num_bits;
  	u64 bg_blkno;
  	struct buffer_head *bg_bh;
  	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
415
  	status = ocfs2_claim_clusters(handle, ac,
798db35f4   Joel Becker   ocfs2: Allocate d...
416
417
418
419
420
421
422
423
424
425
  				      le16_to_cpu(cl->cl_cpg), &bit_off,
  				      &num_bits);
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	/* setup the group */
  	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
426
427
  	trace_ocfs2_block_group_alloc_contig(
  	     (unsigned long long)bg_blkno, alloc_rec);
798db35f4   Joel Becker   ocfs2: Allocate d...
428
429
430
  
  	bg_bh = sb_getblk(osb->sb, bg_blkno);
  	if (!bg_bh) {
7391a294b   Rui Xiang   ocfs2: return ENO...
431
  		status = -ENOMEM;
798db35f4   Joel Becker   ocfs2: Allocate d...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  		mlog_errno(status);
  		goto bail;
  	}
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
  
  	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
  					bg_blkno, num_bits, alloc_rec, cl);
  	if (status < 0) {
  		brelse(bg_bh);
  		mlog_errno(status);
  	}
  
  bail:
  	return status ? ERR_PTR(status) : bg_bh;
  }
  
  static int ocfs2_block_group_claim_bits(struct ocfs2_super *osb,
  					handle_t *handle,
  					struct ocfs2_alloc_context *ac,
  					unsigned int min_bits,
  					u32 *bit_off, u32 *num_bits)
  {
18d3a98f3   Joel Becker   ocfs2: Silence a ...
454
  	int status = 0;
798db35f4   Joel Becker   ocfs2: Allocate d...
455
456
  
  	while (min_bits) {
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
457
  		status = ocfs2_claim_clusters(handle, ac, min_bits,
798db35f4   Joel Becker   ocfs2: Allocate d...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  					      bit_off, num_bits);
  		if (status != -ENOSPC)
  			break;
  
  		min_bits >>= 1;
  	}
  
  	return status;
  }
  
  static int ocfs2_block_group_grow_discontig(handle_t *handle,
  					    struct inode *alloc_inode,
  					    struct buffer_head *bg_bh,
  					    struct ocfs2_alloc_context *ac,
  					    struct ocfs2_chain_list *cl,
  					    unsigned int min_bits)
  {
  	int status;
  	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
  	struct ocfs2_group_desc *bg =
  		(struct ocfs2_group_desc *)bg_bh->b_data;
4711954ea   Tao Ma   ocfs2: Some tiny ...
479
480
  	unsigned int needed = le16_to_cpu(cl->cl_cpg) -
  			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
798db35f4   Joel Becker   ocfs2: Allocate d...
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  	u32 p_cpos, clusters;
  	u64 p_blkno;
  	struct ocfs2_extent_list *el = &bg->bg_list;
  
  	status = ocfs2_journal_access_gd(handle,
  					 INODE_CACHE(alloc_inode),
  					 bg_bh,
  					 OCFS2_JOURNAL_ACCESS_CREATE);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	while ((needed > 0) && (le16_to_cpu(el->l_next_free_rec) <
  				le16_to_cpu(el->l_count))) {
798db35f4   Joel Becker   ocfs2: Allocate d...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  		if (min_bits > needed)
  			min_bits = needed;
  		status = ocfs2_block_group_claim_bits(osb, handle, ac,
  						      min_bits, &p_cpos,
  						      &clusters);
  		if (status < 0) {
  			if (status != -ENOSPC)
  				mlog_errno(status);
  			goto bail;
  		}
  		p_blkno = ocfs2_clusters_to_blocks(osb->sb, p_cpos);
  		ocfs2_bg_discontig_add_extent(osb, bg, cl, p_blkno,
  					      clusters);
  
  		min_bits = clusters;
4711954ea   Tao Ma   ocfs2: Some tiny ...
511
512
  		needed = le16_to_cpu(cl->cl_cpg) -
  			 le16_to_cpu(bg->bg_bits) / le16_to_cpu(cl->cl_bpc);
798db35f4   Joel Becker   ocfs2: Allocate d...
513
514
515
  	}
  
  	if (needed > 0) {
4711954ea   Tao Ma   ocfs2: Some tiny ...
516
517
518
519
520
521
  		/*
  		 * We have used up all the extent rec but can't fill up
  		 * the cpg. So bail out.
  		 */
  		status = -ENOSPC;
  		goto bail;
798db35f4   Joel Becker   ocfs2: Allocate d...
522
523
524
525
526
527
528
  	}
  
  	ocfs2_journal_dirty(handle, bg_bh);
  
  bail:
  	return status;
  }
8b06bc592   Joel Becker   ocfs2: Grow disco...
529
530
531
532
  static void ocfs2_bg_alloc_cleanup(handle_t *handle,
  				   struct ocfs2_alloc_context *cluster_ac,
  				   struct inode *alloc_inode,
  				   struct buffer_head *bg_bh)
798db35f4   Joel Becker   ocfs2: Allocate d...
533
  {
8b06bc592   Joel Becker   ocfs2: Grow disco...
534
  	int i, ret;
798db35f4   Joel Becker   ocfs2: Allocate d...
535
536
537
538
539
540
541
542
543
544
545
  	struct ocfs2_group_desc *bg;
  	struct ocfs2_extent_list *el;
  	struct ocfs2_extent_rec *rec;
  
  	if (!bg_bh)
  		return;
  
  	bg = (struct ocfs2_group_desc *)bg_bh->b_data;
  	el = &bg->bg_list;
  	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
  		rec = &el->l_recs[i];
8b06bc592   Joel Becker   ocfs2: Grow disco...
546
547
548
  		ret = ocfs2_free_clusters(handle, cluster_ac->ac_inode,
  					  cluster_ac->ac_bh,
  					  le64_to_cpu(rec->e_blkno),
72094e43e   Al Viro   ocfs2: ->e_leaf_c...
549
  					  le16_to_cpu(rec->e_leaf_clusters));
8b06bc592   Joel Becker   ocfs2: Grow disco...
550
551
552
  		if (ret)
  			mlog_errno(ret);
  		/* Try all the clusters to free */
798db35f4   Joel Becker   ocfs2: Allocate d...
553
554
555
556
557
558
559
560
561
562
  	}
  
  	ocfs2_remove_from_cache(INODE_CACHE(alloc_inode), bg_bh);
  	brelse(bg_bh);
  }
  
  static struct buffer_head *
  ocfs2_block_group_alloc_discontig(handle_t *handle,
  				  struct inode *alloc_inode,
  				  struct ocfs2_alloc_context *ac,
8b06bc592   Joel Becker   ocfs2: Grow disco...
563
  				  struct ocfs2_chain_list *cl)
798db35f4   Joel Becker   ocfs2: Allocate d...
564
565
566
567
568
569
570
571
  {
  	int status;
  	u32 bit_off, num_bits;
  	u64 bg_blkno;
  	unsigned int min_bits = le16_to_cpu(cl->cl_cpg) >> 1;
  	struct buffer_head *bg_bh = NULL;
  	unsigned int alloc_rec = ocfs2_find_smallest_chain(cl);
  	struct ocfs2_super *osb = OCFS2_SB(alloc_inode->i_sb);
4711954ea   Tao Ma   ocfs2: Some tiny ...
572
  	if (!ocfs2_supports_discontig_bg(osb)) {
798db35f4   Joel Becker   ocfs2: Allocate d...
573
574
575
  		status = -ENOSPC;
  		goto bail;
  	}
8b06bc592   Joel Becker   ocfs2: Grow disco...
576
577
578
579
580
581
  	status = ocfs2_extend_trans(handle,
  				    ocfs2_calc_bg_discontig_credits(osb->sb));
  	if (status) {
  		mlog_errno(status);
  		goto bail;
  	}
95ec0adf0   Joel Becker   ocfs2: Don't reli...
582
583
584
585
586
587
  	/*
  	 * We're going to be grabbing from multiple cluster groups.
  	 * We don't have enough credits to relink them all, and the
  	 * cluster groups will be staying in cache for the duration of
  	 * this operation.
  	 */
309a85b68   Xiaowei.Hu   ocfs2: ac->ac_all...
588
  	ac->ac_disable_chain_relink = 1;
95ec0adf0   Joel Becker   ocfs2: Don't reli...
589

798db35f4   Joel Becker   ocfs2: Allocate d...
590
591
592
593
594
595
596
597
598
599
600
601
  	/* Claim the first region */
  	status = ocfs2_block_group_claim_bits(osb, handle, ac, min_bits,
  					      &bit_off, &num_bits);
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  	min_bits = num_bits;
  
  	/* setup the group */
  	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
602
603
  	trace_ocfs2_block_group_alloc_discontig(
  				(unsigned long long)bg_blkno, alloc_rec);
798db35f4   Joel Becker   ocfs2: Allocate d...
604
605
606
  
  	bg_bh = sb_getblk(osb->sb, bg_blkno);
  	if (!bg_bh) {
7391a294b   Rui Xiang   ocfs2: return ENO...
607
  		status = -ENOMEM;
798db35f4   Joel Becker   ocfs2: Allocate d...
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  		mlog_errno(status);
  		goto bail;
  	}
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(alloc_inode), bg_bh);
  
  	status = ocfs2_block_group_fill(handle, alloc_inode, bg_bh,
  					bg_blkno, num_bits, alloc_rec, cl);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	status = ocfs2_block_group_grow_discontig(handle, alloc_inode,
  						  bg_bh, ac, cl, min_bits);
  	if (status)
  		mlog_errno(status);
  
  bail:
  	if (status)
8b06bc592   Joel Becker   ocfs2: Grow disco...
627
  		ocfs2_bg_alloc_cleanup(handle, ac, alloc_inode, bg_bh);
798db35f4   Joel Becker   ocfs2: Allocate d...
628
629
  	return status ? ERR_PTR(status) : bg_bh;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
630
631
632
633
634
  /*
   * We expect the block group allocator to already be locked.
   */
  static int ocfs2_block_group_alloc(struct ocfs2_super *osb,
  				   struct inode *alloc_inode,
1187c9688   Joel Becker   ocfs2: Limit inod...
635
  				   struct buffer_head *bh,
60ca81e82   Tao Ma   ocfs2: Allocate i...
636
  				   u64 max_block,
feb473a6e   Tao Ma   ocfs2: Optimize i...
637
  				   u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
638
  				   int flags)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
639
640
641
642
643
  {
  	int status, credits;
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data;
  	struct ocfs2_chain_list *cl;
  	struct ocfs2_alloc_context *ac = NULL;
1fabe1481   Mark Fasheh   ocfs2: Remove str...
644
  	handle_t *handle = NULL;
4711954ea   Tao Ma   ocfs2: Some tiny ...
645
  	u16 alloc_rec;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
646
647
648
649
  	struct buffer_head *bg_bh = NULL;
  	struct ocfs2_group_desc *bg;
  
  	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
650
  	cl = &fe->id2.i_chain;
1187c9688   Joel Becker   ocfs2: Limit inod...
651
652
  	status = ocfs2_reserve_clusters_with_limit(osb,
  						   le16_to_cpu(cl->cl_cpg),
60ca81e82   Tao Ma   ocfs2: Allocate i...
653
  						   max_block, flags, &ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
654
655
656
657
658
659
660
661
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	credits = ocfs2_calc_group_alloc_credits(osb->sb,
  						 le16_to_cpu(cl->cl_cpg));
65eff9ccf   Mark Fasheh   ocfs2: remove han...
662
  	handle = ocfs2_start_trans(osb, credits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
663
664
665
666
667
668
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		handle = NULL;
  		mlog_errno(status);
  		goto bail;
  	}
feb473a6e   Tao Ma   ocfs2: Optimize i...
669
  	if (last_alloc_group && *last_alloc_group != 0) {
2f73e135b   Tao Ma   ocfs2: Remove mlo...
670
671
  		trace_ocfs2_block_group_alloc(
  				(unsigned long long)*last_alloc_group);
feb473a6e   Tao Ma   ocfs2: Optimize i...
672
673
  		ac->ac_last_group = *last_alloc_group;
  	}
798db35f4   Joel Becker   ocfs2: Allocate d...
674
675
676
  
  	bg_bh = ocfs2_block_group_alloc_contig(osb, handle, alloc_inode,
  					       ac, cl);
45586c707   Masahiro Yamada   treewide: remove ...
677
  	if (PTR_ERR(bg_bh) == -ENOSPC)
798db35f4   Joel Becker   ocfs2: Allocate d...
678
679
  		bg_bh = ocfs2_block_group_alloc_discontig(handle,
  							  alloc_inode,
8b06bc592   Joel Becker   ocfs2: Grow disco...
680
  							  ac, cl);
798db35f4   Joel Becker   ocfs2: Allocate d...
681
682
683
  	if (IS_ERR(bg_bh)) {
  		status = PTR_ERR(bg_bh);
  		bg_bh = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
684
685
686
687
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
688
  	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
689
  	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
13723d00e   Joel Becker   ocfs2: Use metada...
690
  					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
691
692
693
694
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
4711954ea   Tao Ma   ocfs2: Some tiny ...
695
696
  	alloc_rec = le16_to_cpu(bg->bg_chain);
  	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
697
  		     le16_to_cpu(bg->bg_free_bits_count));
4711954ea   Tao Ma   ocfs2: Some tiny ...
698
  	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total,
798db35f4   Joel Becker   ocfs2: Allocate d...
699
  		     le16_to_cpu(bg->bg_bits));
0a463b74e   Tao Ma   ocfs2: Remove the...
700
  	cl->cl_recs[alloc_rec].c_blkno = bg->bg_blkno;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
701
702
703
704
705
706
707
  	if (le16_to_cpu(cl->cl_next_free_rec) < le16_to_cpu(cl->cl_count))
  		le16_add_cpu(&cl->cl_next_free_rec, 1);
  
  	le32_add_cpu(&fe->id1.bitmap1.i_used, le16_to_cpu(bg->bg_bits) -
  					le16_to_cpu(bg->bg_free_bits_count));
  	le32_add_cpu(&fe->id1.bitmap1.i_total, le16_to_cpu(bg->bg_bits));
  	le32_add_cpu(&fe->i_clusters, le16_to_cpu(cl->cl_cpg));
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
708
  	ocfs2_journal_dirty(handle, bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
709
710
711
712
713
714
715
  
  	spin_lock(&OCFS2_I(alloc_inode)->ip_lock);
  	OCFS2_I(alloc_inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  	fe->i_size = cpu_to_le64(ocfs2_clusters_to_bytes(alloc_inode->i_sb,
  					     le32_to_cpu(fe->i_clusters)));
  	spin_unlock(&OCFS2_I(alloc_inode)->ip_lock);
  	i_size_write(alloc_inode, le64_to_cpu(fe->i_size));
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
716
  	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
6fdb702d6   Darrick J. Wong   ocfs2: call ocfs2...
717
  	ocfs2_update_inode_fsync_trans(handle, alloc_inode, 0);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
718
719
  
  	status = 0;
feb473a6e   Tao Ma   ocfs2: Optimize i...
720
721
722
723
  
  	/* save the new last alloc group so that the caller can cache it. */
  	if (last_alloc_group)
  		*last_alloc_group = ac->ac_last_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
724
725
  bail:
  	if (handle)
02dc1af44   Mark Fasheh   ocfs2: pass ocfs2...
726
  		ocfs2_commit_trans(osb, handle);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
727
728
729
  
  	if (ac)
  		ocfs2_free_alloc_context(ac);
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
730
  	brelse(bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
731

c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
732
733
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
734
735
736
737
  	return status;
  }
  
  static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
738
739
  				       struct ocfs2_alloc_context *ac,
  				       int type,
ffda89a3b   Tao Ma   ocfs2: Add a new ...
740
  				       u32 slot,
feb473a6e   Tao Ma   ocfs2: Optimize i...
741
  				       u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
742
  				       int flags)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
743
744
745
  {
  	int status;
  	u32 bits_wanted = ac->ac_bits_wanted;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
746
  	struct inode *alloc_inode;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
747
  	struct buffer_head *bh = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
748
749
  	struct ocfs2_dinode *fe;
  	u32 free_bits;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
750
751
752
753
754
  	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
  	if (!alloc_inode) {
  		mlog_errno(-EINVAL);
  		return -EINVAL;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
755

5955102c9   Al Viro   wrappers for ->i_...
756
  	inode_lock(alloc_inode);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
757

e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
758
  	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
759
  	if (status < 0) {
5955102c9   Al Viro   wrappers for ->i_...
760
  		inode_unlock(alloc_inode);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
761
  		iput(alloc_inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
762
  		mlog_errno(status);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
763
  		return status;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
764
  	}
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
765
  	ac->ac_inode = alloc_inode;
a4a489116   Tao Ma   ocfs2: Add ac_all...
766
  	ac->ac_alloc_slot = slot;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
767

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
768
  	fe = (struct ocfs2_dinode *) bh->b_data;
10995aa24   Joel Becker   ocfs2: Morph the ...
769
770
771
772
  
  	/* The bh was validated by the inode read inside
  	 * ocfs2_inode_lock().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
773
  	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
774
775
776
777
  		status = ocfs2_error(alloc_inode->i_sb,
  				     "Invalid chain allocator %llu
  ",
  				     (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
778
779
780
781
782
783
784
785
786
  		goto bail;
  	}
  
  	free_bits = le32_to_cpu(fe->id1.bitmap1.i_total) -
  		le32_to_cpu(fe->id1.bitmap1.i_used);
  
  	if (bits_wanted > free_bits) {
  		/* cluster bitmap never grows */
  		if (ocfs2_is_cluster_bitmap(alloc_inode)) {
2f73e135b   Tao Ma   ocfs2: Remove mlo...
787
788
  			trace_ocfs2_reserve_suballoc_bits_nospc(bits_wanted,
  								free_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
789
790
791
  			status = -ENOSPC;
  			goto bail;
  		}
60ca81e82   Tao Ma   ocfs2: Allocate i...
792
  		if (!(flags & ALLOC_NEW_GROUP)) {
2f73e135b   Tao Ma   ocfs2: Remove mlo...
793
794
  			trace_ocfs2_reserve_suballoc_bits_no_new_group(
  						slot, bits_wanted, free_bits);
ffda89a3b   Tao Ma   ocfs2: Add a new ...
795
796
797
  			status = -ENOSPC;
  			goto bail;
  		}
1187c9688   Joel Becker   ocfs2: Limit inod...
798
  		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
feb473a6e   Tao Ma   ocfs2: Optimize i...
799
800
  						 ac->ac_max_block,
  						 last_alloc_group, flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
  		if (status < 0) {
  			if (status != -ENOSPC)
  				mlog_errno(status);
  			goto bail;
  		}
  		atomic_inc(&osb->alloc_stats.bg_extends);
  
  		/* You should never ask for this much metadata */
  		BUG_ON(bits_wanted >
  		       (le32_to_cpu(fe->id1.bitmap1.i_total)
  			- le32_to_cpu(fe->id1.bitmap1.i_used)));
  	}
  
  	get_bh(bh);
  	ac->ac_bh = bh;
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
817
  	brelse(bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
818

c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
819
820
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
821
822
  	return status;
  }
b89c54282   Tiger Yang   ocfs2: add extent...
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
  static void ocfs2_init_inode_steal_slot(struct ocfs2_super *osb)
  {
  	spin_lock(&osb->osb_lock);
  	osb->s_inode_steal_slot = OCFS2_INVALID_SLOT;
  	spin_unlock(&osb->osb_lock);
  	atomic_set(&osb->s_num_inodes_stolen, 0);
  }
  
  static void ocfs2_init_meta_steal_slot(struct ocfs2_super *osb)
  {
  	spin_lock(&osb->osb_lock);
  	osb->s_meta_steal_slot = OCFS2_INVALID_SLOT;
  	spin_unlock(&osb->osb_lock);
  	atomic_set(&osb->s_num_meta_stolen, 0);
  }
  
  void ocfs2_init_steal_slots(struct ocfs2_super *osb)
  {
  	ocfs2_init_inode_steal_slot(osb);
  	ocfs2_init_meta_steal_slot(osb);
  }
  
  static void __ocfs2_set_steal_slot(struct ocfs2_super *osb, int slot, int type)
  {
  	spin_lock(&osb->osb_lock);
  	if (type == INODE_ALLOC_SYSTEM_INODE)
38d51b2dd   Junxiao Bi   ocfs2: change slo...
849
  		osb->s_inode_steal_slot = (u16)slot;
b89c54282   Tiger Yang   ocfs2: add extent...
850
  	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
38d51b2dd   Junxiao Bi   ocfs2: change slo...
851
  		osb->s_meta_steal_slot = (u16)slot;
b89c54282   Tiger Yang   ocfs2: add extent...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  	spin_unlock(&osb->osb_lock);
  }
  
  static int __ocfs2_get_steal_slot(struct ocfs2_super *osb, int type)
  {
  	int slot = OCFS2_INVALID_SLOT;
  
  	spin_lock(&osb->osb_lock);
  	if (type == INODE_ALLOC_SYSTEM_INODE)
  		slot = osb->s_inode_steal_slot;
  	else if (type == EXTENT_ALLOC_SYSTEM_INODE)
  		slot = osb->s_meta_steal_slot;
  	spin_unlock(&osb->osb_lock);
  
  	return slot;
  }
  
  static int ocfs2_get_inode_steal_slot(struct ocfs2_super *osb)
  {
  	return __ocfs2_get_steal_slot(osb, INODE_ALLOC_SYSTEM_INODE);
  }
  
  static int ocfs2_get_meta_steal_slot(struct ocfs2_super *osb)
  {
  	return __ocfs2_get_steal_slot(osb, EXTENT_ALLOC_SYSTEM_INODE);
  }
  
  static int ocfs2_steal_resource(struct ocfs2_super *osb,
  				struct ocfs2_alloc_context *ac,
  				int type)
  {
  	int i, status = -ENOSPC;
  	int slot = __ocfs2_get_steal_slot(osb, type);
  
  	/* Start to steal resource from the first slot after ours. */
  	if (slot == OCFS2_INVALID_SLOT)
  		slot = osb->slot_num + 1;
  
  	for (i = 0; i < osb->max_slots; i++, slot++) {
  		if (slot == osb->max_slots)
  			slot = 0;
  
  		if (slot == osb->slot_num)
  			continue;
  
  		status = ocfs2_reserve_suballoc_bits(osb, ac,
  						     type,
  						     (u32)slot, NULL,
  						     NOT_ALLOC_NEW_GROUP);
  		if (status >= 0) {
  			__ocfs2_set_steal_slot(osb, slot, type);
  			break;
  		}
  
  		ocfs2_free_ac_resource(ac);
  	}
  
  	return status;
  }
  
  static int ocfs2_steal_inode(struct ocfs2_super *osb,
  			     struct ocfs2_alloc_context *ac)
  {
  	return ocfs2_steal_resource(osb, ac, INODE_ALLOC_SYSTEM_INODE);
  }
  
  static int ocfs2_steal_meta(struct ocfs2_super *osb,
  			    struct ocfs2_alloc_context *ac)
  {
  	return ocfs2_steal_resource(osb, ac, EXTENT_ALLOC_SYSTEM_INODE);
  }
cf1d6c763   Tiger Yang   ocfs2: Add extend...
923
924
925
  int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
  				      int blocks,
  				      struct ocfs2_alloc_context **ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
926
927
  {
  	int status;
b89c54282   Tiger Yang   ocfs2: add extent...
928
  	int slot = ocfs2_get_meta_steal_slot(osb);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
929

cd8612808   Robert P. J. Day   [PATCH] Fix numer...
930
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
931
932
933
934
935
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
cf1d6c763   Tiger Yang   ocfs2: Add extend...
936
  	(*ac)->ac_bits_wanted = blocks;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
937
  	(*ac)->ac_which = OCFS2_AC_USE_META;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
938
  	(*ac)->ac_group_search = ocfs2_block_group_search;
b89c54282   Tiger Yang   ocfs2: add extent...
939
940
941
942
943
  	if (slot != OCFS2_INVALID_SLOT &&
  		atomic_read(&osb->s_num_meta_stolen) < OCFS2_MAX_TO_STEAL)
  		goto extent_steal;
  
  	atomic_set(&osb->s_num_meta_stolen, 0);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
944
  	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
ffda89a3b   Tao Ma   ocfs2: Add a new ...
945
  					     EXTENT_ALLOC_SYSTEM_INODE,
b89c54282   Tiger Yang   ocfs2: add extent...
946
  					     (u32)osb->slot_num, NULL,
33d5d380d   Mark Fasheh   ocfs2: allocate b...
947
  					     ALLOC_GROUPS_FROM_GLOBAL|ALLOC_NEW_GROUP);
b89c54282   Tiger Yang   ocfs2: add extent...
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  
  
  	if (status >= 0) {
  		status = 0;
  		if (slot != OCFS2_INVALID_SLOT)
  			ocfs2_init_meta_steal_slot(osb);
  		goto bail;
  	} else if (status < 0 && status != -ENOSPC) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	ocfs2_free_ac_resource(*ac);
  
  extent_steal:
  	status = ocfs2_steal_meta(osb, *ac);
  	atomic_inc(&osb->s_num_meta_stolen);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
965
966
967
968
969
970
971
972
973
974
975
976
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	status = 0;
  bail:
  	if ((status < 0) && *ac) {
  		ocfs2_free_alloc_context(*ac);
  		*ac = NULL;
  	}
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
977
978
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
979
980
  	return status;
  }
cf1d6c763   Tiger Yang   ocfs2: Add extend...
981
982
983
984
985
986
987
988
  int ocfs2_reserve_new_metadata(struct ocfs2_super *osb,
  			       struct ocfs2_extent_list *root_el,
  			       struct ocfs2_alloc_context **ac)
  {
  	return ocfs2_reserve_new_metadata_blocks(osb,
  					ocfs2_extend_meta_needed(root_el),
  					ac);
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
989
  int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
990
991
992
  			    struct ocfs2_alloc_context **ac)
  {
  	int status;
b89c54282   Tiger Yang   ocfs2: add extent...
993
  	int slot = ocfs2_get_inode_steal_slot(osb);
feb473a6e   Tao Ma   ocfs2: Optimize i...
994
  	u64 alloc_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
995

cd8612808   Robert P. J. Day   [PATCH] Fix numer...
996
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
997
998
999
1000
1001
1002
1003
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
  
  	(*ac)->ac_bits_wanted = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1004
  	(*ac)->ac_which = OCFS2_AC_USE_INODE;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1005
  	(*ac)->ac_group_search = ocfs2_block_group_search;
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1006
  	/*
1187c9688   Joel Becker   ocfs2: Limit inod...
1007
1008
  	 * stat(2) can't handle i_ino > 32bits, so we tell the
  	 * lower levels not to allocate us a block group past that
12462f1d9   Joel Becker   ocfs2: Add the 'i...
1009
  	 * limit.  The 'inode64' mount option avoids this behavior.
1187c9688   Joel Becker   ocfs2: Limit inod...
1010
  	 */
12462f1d9   Joel Becker   ocfs2: Add the 'i...
1011
1012
  	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
  		(*ac)->ac_max_block = (u32)~0U;
1187c9688   Joel Becker   ocfs2: Limit inod...
1013
1014
  
  	/*
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1015
1016
1017
1018
1019
1020
1021
1022
1023
  	 * slot is set when we successfully steal inode from other nodes.
  	 * It is reset in 3 places:
  	 * 1. when we flush the truncate log
  	 * 2. when we complete local alloc recovery.
  	 * 3. when we successfully allocate from our own slot.
  	 * After it is set, we will go on stealing inodes until we find the
  	 * need to check our slots to see whether there is some space for us.
  	 */
  	if (slot != OCFS2_INVALID_SLOT &&
b89c54282   Tiger Yang   ocfs2: add extent...
1024
  	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_TO_STEAL)
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1025
1026
1027
  		goto inode_steal;
  
  	atomic_set(&osb->s_num_inodes_stolen, 0);
feb473a6e   Tao Ma   ocfs2: Optimize i...
1028
  	alloc_group = osb->osb_inode_alloc_group;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1029
1030
  	status = ocfs2_reserve_suballoc_bits(osb, *ac,
  					     INODE_ALLOC_SYSTEM_INODE,
b89c54282   Tiger Yang   ocfs2: add extent...
1031
  					     (u32)osb->slot_num,
feb473a6e   Tao Ma   ocfs2: Optimize i...
1032
  					     &alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
1033
1034
  					     ALLOC_NEW_GROUP |
  					     ALLOC_GROUPS_FROM_GLOBAL);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1035
1036
  	if (status >= 0) {
  		status = 0;
feb473a6e   Tao Ma   ocfs2: Optimize i...
1037
1038
1039
  		spin_lock(&osb->osb_lock);
  		osb->osb_inode_alloc_group = alloc_group;
  		spin_unlock(&osb->osb_lock);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1040
1041
  		trace_ocfs2_reserve_new_inode_new_group(
  			(unsigned long long)alloc_group);
feb473a6e   Tao Ma   ocfs2: Optimize i...
1042

4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
  		/*
  		 * Some inodes must be freed by us, so try to allocate
  		 * from our own next time.
  		 */
  		if (slot != OCFS2_INVALID_SLOT)
  			ocfs2_init_inode_steal_slot(osb);
  		goto bail;
  	} else if (status < 0 && status != -ENOSPC) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	ocfs2_free_ac_resource(*ac);
  
  inode_steal:
b89c54282   Tiger Yang   ocfs2: add extent...
1058
  	status = ocfs2_steal_inode(osb, *ac);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
1059
  	atomic_inc(&osb->s_num_inodes_stolen);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	status = 0;
  bail:
  	if ((status < 0) && *ac) {
  		ocfs2_free_alloc_context(*ac);
  		*ac = NULL;
  	}
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1072
1073
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1074
1075
1076
1077
1078
1079
1080
1081
1082
  	return status;
  }
  
  /* local alloc code has to do the same thing, so rather than do this
   * twice.. */
  int ocfs2_reserve_cluster_bitmap_bits(struct ocfs2_super *osb,
  				      struct ocfs2_alloc_context *ac)
  {
  	int status;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1083
1084
  	ac->ac_which = OCFS2_AC_USE_MAIN;
  	ac->ac_group_search = ocfs2_cluster_group_search;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1085
1086
  	status = ocfs2_reserve_suballoc_bits(osb, ac,
  					     GLOBAL_BITMAP_SYSTEM_INODE,
feb473a6e   Tao Ma   ocfs2: Optimize i...
1087
  					     OCFS2_INVALID_SLOT, NULL,
ffda89a3b   Tao Ma   ocfs2: Add a new ...
1088
  					     ALLOC_NEW_GROUP);
47ee9d89f   Guozhonghua   ocfs2: remove unn...
1089
  	if (status < 0 && status != -ENOSPC)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1090
  		mlog_errno(status);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1091

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1092
1093
1094
1095
1096
1097
  	return status;
  }
  
  /* Callers don't need to care which bitmap (local alloc or main) to
   * use so we figure it out for them, but unfortunately this clutters
   * things a bit. */
1187c9688   Joel Becker   ocfs2: Limit inod...
1098
1099
  static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  					     u32 bits_wanted, u64 max_block,
60ca81e82   Tao Ma   ocfs2: Allocate i...
1100
  					     int flags,
1187c9688   Joel Becker   ocfs2: Limit inod...
1101
  					     struct ocfs2_alloc_context **ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1102
  {
2070ad1ae   Eric Ren   ocfs2: retry on E...
1103
1104
  	int status, ret = 0;
  	int retried = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1105

cd8612808   Robert P. J. Day   [PATCH] Fix numer...
1106
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1107
1108
1109
1110
1111
1112
1113
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
  
  	(*ac)->ac_bits_wanted = bits_wanted;
1187c9688   Joel Becker   ocfs2: Limit inod...
1114
  	(*ac)->ac_max_block = max_block;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1115
1116
  
  	status = -ENOSPC;
60ca81e82   Tao Ma   ocfs2: Allocate i...
1117
1118
  	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
  	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1119
  		status = ocfs2_reserve_local_alloc_bits(osb,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1120
1121
  							bits_wanted,
  							*ac);
a57c8fd2a   Mark Fasheh   ocfs2: remove ocf...
1122
  		if ((status < 0) && (status != -ENOSPC)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1123
1124
  			mlog_errno(status);
  			goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1125
1126
1127
1128
  		}
  	}
  
  	if (status == -ENOSPC) {
2070ad1ae   Eric Ren   ocfs2: retry on E...
1129
  retry:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1130
  		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
2070ad1ae   Eric Ren   ocfs2: retry on E...
1131
1132
1133
1134
1135
1136
1137
  		/* Retry if there is sufficient space cached in truncate log */
  		if (status == -ENOSPC && !retried) {
  			retried = 1;
  			ocfs2_inode_unlock((*ac)->ac_inode, 1);
  			inode_unlock((*ac)->ac_inode);
  
  			ret = ocfs2_try_to_free_truncate_log(osb, bits_wanted);
3bb8b653c   Joseph Qi   ocfs2: fix double...
1138
1139
1140
  			if (ret == 1) {
  				iput((*ac)->ac_inode);
  				(*ac)->ac_inode = NULL;
2070ad1ae   Eric Ren   ocfs2: retry on E...
1141
  				goto retry;
3bb8b653c   Joseph Qi   ocfs2: fix double...
1142
  			}
2070ad1ae   Eric Ren   ocfs2: retry on E...
1143
1144
1145
1146
1147
  
  			if (ret < 0)
  				mlog_errno(ret);
  
  			inode_lock((*ac)->ac_inode);
3bb8b653c   Joseph Qi   ocfs2: fix double...
1148
1149
1150
1151
1152
1153
1154
1155
  			ret = ocfs2_inode_lock((*ac)->ac_inode, NULL, 1);
  			if (ret < 0) {
  				mlog_errno(ret);
  				inode_unlock((*ac)->ac_inode);
  				iput((*ac)->ac_inode);
  				(*ac)->ac_inode = NULL;
  				goto bail;
  			}
2070ad1ae   Eric Ren   ocfs2: retry on E...
1156
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  		if (status < 0) {
  			if (status != -ENOSPC)
  				mlog_errno(status);
  			goto bail;
  		}
  	}
  
  	status = 0;
  bail:
  	if ((status < 0) && *ac) {
  		ocfs2_free_alloc_context(*ac);
  		*ac = NULL;
  	}
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1170
1171
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1172
1173
  	return status;
  }
1187c9688   Joel Becker   ocfs2: Limit inod...
1174
1175
1176
1177
  int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  			   u32 bits_wanted,
  			   struct ocfs2_alloc_context **ac)
  {
60ca81e82   Tao Ma   ocfs2: Allocate i...
1178
1179
  	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
  						 ALLOC_NEW_GROUP, ac);
1187c9688   Joel Becker   ocfs2: Limit inod...
1180
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
  /*
   * More or less lifted from ext3. I'll leave their description below:
   *
   * "For ext3 allocations, we must not reuse any blocks which are
   * allocated in the bitmap buffer's "last committed data" copy.  This
   * prevents deletes from freeing up the page for reuse until we have
   * committed the delete transaction.
   *
   * If we didn't do this, then deleting something and reallocating it as
   * data would allow the old block to be overwritten before the
   * transaction committed (because we force data to disk before commit).
   * This would lead to corruption if we crashed between overwriting the
   * data and committing the delete.
   *
   * @@@ We may want to make this allocation behaviour conditional on
   * data-writes at some point, and disable it for metadata allocations or
   * sync-data inodes."
   *
   * Note: OCFS2 already does this differently for metadata vs data
c78bad11f   Joe Perches   fs/: Spelling fixes
1200
   * allocations, as those bitmaps are separate and undo access is never
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1201
1202
1203
1204
1205
1206
   * called on a metadata group descriptor.
   */
  static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  					 int nr)
  {
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
464170647   Thomas Gleixner   jbd2: Make state ...
1207
  	struct journal_head *jh;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1208
  	int ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1209
1210
1211
  
  	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  		return 0;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1212
1213
  
  	if (!buffer_jbd(bg_bh))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1214
  		return 1;
464170647   Thomas Gleixner   jbd2: Make state ...
1215
1216
1217
  	jh = bh2jh(bg_bh);
  	spin_lock(&jh->b_state_lock);
  	bg = (struct ocfs2_group_desc *) jh->b_committed_data;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1218
1219
1220
1221
  	if (bg)
  		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  	else
  		ret = 1;
464170647   Thomas Gleixner   jbd2: Make state ...
1222
  	spin_unlock(&jh->b_state_lock);
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1223
1224
  
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1225
1226
1227
1228
1229
  }
  
  static int ocfs2_block_group_find_clear_bits(struct ocfs2_super *osb,
  					     struct buffer_head *bg_bh,
  					     unsigned int bits_wanted,
7bf72edee   Mark Fasheh   ocfs2: better gro...
1230
  					     unsigned int total_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1231
  					     struct ocfs2_suballoc_result *res)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1232
1233
1234
1235
1236
  {
  	void *bitmap;
  	u16 best_offset, best_size;
  	int offset, start, found, status = 0;
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
420353061   Joel Becker   ocfs2: Morph the ...
1237
1238
1239
  	/* Callers got this descriptor from
  	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1240
1241
1242
  
  	found = start = best_offset = best_size = 0;
  	bitmap = bg->bg_bitmap;
7bf72edee   Mark Fasheh   ocfs2: better gro...
1243
1244
  	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  		if (offset == total_bits)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
  			break;
  
  		if (!ocfs2_test_bg_bit_allocatable(bg_bh, offset)) {
  			/* We found a zero, but we can't use it as it
  			 * hasn't been put to disk yet! */
  			found = 0;
  			start = offset + 1;
  		} else if (offset == start) {
  			/* we found a zero */
  			found++;
  			/* move start to the next bit to test */
  			start++;
  		} else {
  			/* got a zero after some ones */
  			found = 1;
  			start = offset + 1;
  		}
  		if (found > best_size) {
  			best_size = found;
  			best_offset = start - found;
  		}
  		/* we got everything we needed */
  		if (found == bits_wanted) {
  			/* mlog(0, "Found it all!
  "); */
  			break;
  		}
  	}
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1273
1274
1275
  	if (best_size) {
  		res->sr_bit_offset = best_offset;
  		res->sr_bits = best_size;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1276
1277
1278
1279
1280
1281
1282
1283
  	} else {
  		status = -ENOSPC;
  		/* No error log here -- see the comment above
  		 * ocfs2_test_bg_bit_allocatable */
  	}
  
  	return status;
  }
0a2fcd898   Younger Liu   ocfs2: remove red...
1284
  int ocfs2_block_group_set_bits(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1285
1286
1287
1288
1289
1290
1291
1292
1293
  					     struct inode *alloc_inode,
  					     struct ocfs2_group_desc *bg,
  					     struct buffer_head *group_bh,
  					     unsigned int bit_off,
  					     unsigned int num_bits)
  {
  	int status;
  	void *bitmap = bg->bg_bitmap;
  	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
420353061   Joel Becker   ocfs2: Morph the ...
1294
1295
1296
  	/* All callers get the descriptor via
  	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1297
  	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1298
  	trace_ocfs2_block_group_set_bits(bit_off, num_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1299
1300
1301
  
  	if (ocfs2_is_cluster_bitmap(alloc_inode))
  		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
13723d00e   Joel Becker   ocfs2: Use metada...
1302
  	status = ocfs2_journal_access_gd(handle,
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1303
  					 INODE_CACHE(alloc_inode),
13723d00e   Joel Becker   ocfs2: Use metada...
1304
1305
  					 group_bh,
  					 journal_type);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1306
1307
1308
1309
1310
1311
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
9b5cd10e4   Srinivas Eeda   ocfs2: validate b...
1312
  	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
1313
1314
1315
1316
1317
1318
  		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d
  ",
  				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
  				   le16_to_cpu(bg->bg_bits),
  				   le16_to_cpu(bg->bg_free_bits_count),
  				   num_bits);
9b5cd10e4   Srinivas Eeda   ocfs2: validate b...
1319
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1320
1321
  	while(num_bits--)
  		ocfs2_set_bit(bit_off++, bitmap);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1322
  	ocfs2_journal_dirty(handle, group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1323
1324
  
  bail:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
  	return status;
  }
  
  /* find the one with the most empty bits */
  static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl)
  {
  	u16 curr, best;
  
  	BUG_ON(!cl->cl_next_free_rec);
  
  	best = curr = 0;
  	while (curr < le16_to_cpu(cl->cl_next_free_rec)) {
  		if (le32_to_cpu(cl->cl_recs[curr].c_free) >
  		    le32_to_cpu(cl->cl_recs[best].c_free))
  			best = curr;
  		curr++;
  	}
  
  	BUG_ON(best >= le16_to_cpu(cl->cl_next_free_rec));
  	return best;
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1346
  static int ocfs2_relink_block_group(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1347
1348
1349
1350
1351
1352
1353
1354
1355
  				    struct inode *alloc_inode,
  				    struct buffer_head *fe_bh,
  				    struct buffer_head *bg_bh,
  				    struct buffer_head *prev_bg_bh,
  				    u16 chain)
  {
  	int status;
  	/* there is a really tiny chance the journal calls could fail,
  	 * but we wouldn't want inconsistent blocks in *any* case. */
493098413   Jie Liu   ocfs2: rework tra...
1356
  	u64 bg_ptr, prev_bg_ptr;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1357
1358
1359
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data;
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  	struct ocfs2_group_desc *prev_bg = (struct ocfs2_group_desc *) prev_bg_bh->b_data;
420353061   Joel Becker   ocfs2: Morph the ...
1360
1361
1362
1363
  	/* The caller got these descriptors from
  	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
  	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(prev_bg));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1364

2f73e135b   Tao Ma   ocfs2: Remove mlo...
1365
1366
1367
1368
  	trace_ocfs2_relink_block_group(
  		(unsigned long long)le64_to_cpu(fe->i_blkno), chain,
  		(unsigned long long)le64_to_cpu(bg->bg_blkno),
  		(unsigned long long)le64_to_cpu(prev_bg->bg_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1369

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1370
1371
  	bg_ptr = le64_to_cpu(bg->bg_next_group);
  	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1372
1373
  	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  					 prev_bg_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
1374
  					 OCFS2_JOURNAL_ACCESS_WRITE);
25e289210   Andrew Morton   ocfs2: remove dup...
1375
  	if (status < 0)
493098413   Jie Liu   ocfs2: rework tra...
1376
  		goto out;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1377
1378
  
  	prev_bg->bg_next_group = bg->bg_next_group;
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1379
  	ocfs2_journal_dirty(handle, prev_bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1380

0cf2f7632   Joel Becker   ocfs2: Pass struc...
1381
1382
  	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
  					 bg_bh, OCFS2_JOURNAL_ACCESS_WRITE);
25e289210   Andrew Morton   ocfs2: remove dup...
1383
  	if (status < 0)
493098413   Jie Liu   ocfs2: rework tra...
1384
  		goto out_rollback_prev_bg;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1385
1386
  
  	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1387
  	ocfs2_journal_dirty(handle, bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1388

0cf2f7632   Joel Becker   ocfs2: Pass struc...
1389
1390
  	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  					 fe_bh, OCFS2_JOURNAL_ACCESS_WRITE);
25e289210   Andrew Morton   ocfs2: remove dup...
1391
  	if (status < 0)
493098413   Jie Liu   ocfs2: rework tra...
1392
  		goto out_rollback_bg;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1393
1394
  
  	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1395
  	ocfs2_journal_dirty(handle, fe_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1396

493098413   Jie Liu   ocfs2: rework tra...
1397
  out:
25e289210   Andrew Morton   ocfs2: remove dup...
1398
1399
  	if (status < 0)
  		mlog_errno(status);
493098413   Jie Liu   ocfs2: rework tra...
1400
  	return status;
420353061   Joel Becker   ocfs2: Morph the ...
1401

493098413   Jie Liu   ocfs2: rework tra...
1402
1403
1404
1405
  out_rollback_bg:
  	bg->bg_next_group = cpu_to_le64(bg_ptr);
  out_rollback_prev_bg:
  	prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
25e289210   Andrew Morton   ocfs2: remove dup...
1406
  	goto out;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
  }
  
  static inline int ocfs2_block_group_reasonably_empty(struct ocfs2_group_desc *bg,
  						     u32 wanted)
  {
  	return le16_to_cpu(bg->bg_free_bits_count) > wanted;
  }
  
  /* return 0 on success, -ENOSPC to keep searching and any other < 0
   * value on error. */
  static int ocfs2_cluster_group_search(struct inode *inode,
  				      struct buffer_head *group_bh,
  				      u32 bits_wanted, u32 min_bits,
1187c9688   Joel Becker   ocfs2: Limit inod...
1420
  				      u64 max_block,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1421
  				      struct ocfs2_suballoc_result *res)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1422
1423
1424
  {
  	int search = -ENOSPC;
  	int ret;
1187c9688   Joel Becker   ocfs2: Limit inod...
1425
  	u64 blkoff;
7bf72edee   Mark Fasheh   ocfs2: better gro...
1426
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
1427
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7bf72edee   Mark Fasheh   ocfs2: better gro...
1428
  	unsigned int max_bits, gd_cluster_off;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1429
1430
  
  	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
7bf72edee   Mark Fasheh   ocfs2: better gro...
1431
1432
1433
1434
  	if (gd->bg_free_bits_count) {
  		max_bits = le16_to_cpu(gd->bg_bits);
  
  		/* Tail groups in cluster bitmaps which aren't cpg
25985edce   Lucas De Marchi   Fix common misspe...
1435
  		 * aligned are prone to partial extension by a failed
7bf72edee   Mark Fasheh   ocfs2: better gro...
1436
1437
1438
1439
1440
1441
1442
1443
1444
  		 * fs resize. If the file system resize never got to
  		 * update the dinode cluster count, then we don't want
  		 * to trust any clusters past it, regardless of what
  		 * the group descriptor says. */
  		gd_cluster_off = ocfs2_blocks_to_clusters(inode->i_sb,
  							  le64_to_cpu(gd->bg_blkno));
  		if ((gd_cluster_off + max_bits) >
  		    OCFS2_I(inode)->ip_clusters) {
  			max_bits = OCFS2_I(inode)->ip_clusters - gd_cluster_off;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1445
1446
1447
1448
  			trace_ocfs2_cluster_group_search_wrong_max_bits(
  				(unsigned long long)le64_to_cpu(gd->bg_blkno),
  				le16_to_cpu(gd->bg_bits),
  				OCFS2_I(inode)->ip_clusters, max_bits);
7bf72edee   Mark Fasheh   ocfs2: better gro...
1449
  		}
1119d3c06   piaojun   ocfs2: use 'osb' ...
1450
  		ret = ocfs2_block_group_find_clear_bits(osb,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1451
  							group_bh, bits_wanted,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1452
  							max_bits, res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1453
1454
  		if (ret)
  			return ret;
1187c9688   Joel Becker   ocfs2: Limit inod...
1455
1456
1457
  		if (max_block) {
  			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
  							  gd_cluster_off +
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1458
1459
  							  res->sr_bit_offset +
  							  res->sr_bits);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1460
1461
1462
  			trace_ocfs2_cluster_group_search_max_block(
  				(unsigned long long)blkoff,
  				(unsigned long long)max_block);
1187c9688   Joel Becker   ocfs2: Limit inod...
1463
1464
1465
  			if (blkoff > max_block)
  				return -ENOSPC;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1466
1467
1468
1469
  		/* ocfs2_block_group_find_clear_bits() might
  		 * return success, but we still want to return
  		 * -ENOSPC unless it found the minimum number
  		 * of bits. */
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1470
  		if (min_bits <= res->sr_bits)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1471
  			search = 0; /* success */
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1472
  		else if (res->sr_bits) {
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
1473
1474
1475
1476
  			/*
  			 * Don't show bits which we'll be returning
  			 * for allocation to the local alloc bitmap.
  			 */
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1477
  			ocfs2_local_alloc_seen_free_bits(osb, res->sr_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1478
1479
1480
1481
1482
1483
1484
1485
1486
  		}
  	}
  
  	return search;
  }
  
  static int ocfs2_block_group_search(struct inode *inode,
  				    struct buffer_head *group_bh,
  				    u32 bits_wanted, u32 min_bits,
1187c9688   Joel Becker   ocfs2: Limit inod...
1487
  				    u64 max_block,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1488
  				    struct ocfs2_suballoc_result *res)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1489
1490
  {
  	int ret = -ENOSPC;
1187c9688   Joel Becker   ocfs2: Limit inod...
1491
  	u64 blkoff;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1492
1493
1494
1495
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) group_bh->b_data;
  
  	BUG_ON(min_bits != 1);
  	BUG_ON(ocfs2_is_cluster_bitmap(inode));
1187c9688   Joel Becker   ocfs2: Limit inod...
1496
  	if (bg->bg_free_bits_count) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1497
1498
  		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  							group_bh, bits_wanted,
7bf72edee   Mark Fasheh   ocfs2: better gro...
1499
  							le16_to_cpu(bg->bg_bits),
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1500
  							res);
1187c9688   Joel Becker   ocfs2: Limit inod...
1501
  		if (!ret && max_block) {
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1502
1503
  			blkoff = le64_to_cpu(bg->bg_blkno) +
  				res->sr_bit_offset + res->sr_bits;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1504
1505
1506
  			trace_ocfs2_block_group_search_max_block(
  				(unsigned long long)blkoff,
  				(unsigned long long)max_block);
1187c9688   Joel Becker   ocfs2: Limit inod...
1507
1508
1509
1510
  			if (blkoff > max_block)
  				ret = -ENOSPC;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1511
1512
1513
  
  	return ret;
  }
0a2fcd898   Younger Liu   ocfs2: remove red...
1514
  int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1515
  				       handle_t *handle,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1516
1517
1518
1519
1520
1521
1522
1523
  				       struct buffer_head *di_bh,
  				       u32 num_bits,
  				       u16 chain)
  {
  	int ret;
  	u32 tmp_used;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &di->id2.i_chain;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1524
  	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
1525
  				      OCFS2_JOURNAL_ACCESS_WRITE);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1526
1527
1528
1529
1530
1531
1532
1533
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  	di->id1.bitmap1.i_used = cpu_to_le32(num_bits + tmp_used);
  	le32_add_cpu(&cl->cl_recs[chain].c_free, -num_bits);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1534
  	ocfs2_journal_dirty(handle, di_bh);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1535
1536
1537
1538
  
  out:
  	return ret;
  }
db66c7157   Younger Liu   ocfs2: rollback a...
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
  void ocfs2_rollback_alloc_dinode_counts(struct inode *inode,
  				       struct buffer_head *di_bh,
  				       u32 num_bits,
  				       u16 chain)
  {
  	u32 tmp_used;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  	struct ocfs2_chain_list *cl;
  
  	cl = (struct ocfs2_chain_list *)&di->id2.i_chain;
  	tmp_used = le32_to_cpu(di->id1.bitmap1.i_used);
  	di->id1.bitmap1.i_used = cpu_to_le32(tmp_used - num_bits);
  	le32_add_cpu(&cl->cl_recs[chain].c_free, num_bits);
  }
ba2066351   Joel Becker   ocfs2: Return all...
1553
1554
1555
  static int ocfs2_bg_discontig_fix_by_rec(struct ocfs2_suballoc_result *res,
  					 struct ocfs2_extent_rec *rec,
  					 struct ocfs2_chain_list *cl)
13e434cf0   Joel Becker   ocfs2: Trim subal...
1556
1557
1558
  {
  	unsigned int bpc = le16_to_cpu(cl->cl_bpc);
  	unsigned int bitoff = le32_to_cpu(rec->e_cpos) * bpc;
72094e43e   Al Viro   ocfs2: ->e_leaf_c...
1559
  	unsigned int bitcount = le16_to_cpu(rec->e_leaf_clusters) * bpc;
13e434cf0   Joel Becker   ocfs2: Trim subal...
1560
1561
1562
1563
1564
  
  	if (res->sr_bit_offset < bitoff)
  		return 0;
  	if (res->sr_bit_offset >= (bitoff + bitcount))
  		return 0;
ba2066351   Joel Becker   ocfs2: Return all...
1565
1566
  	res->sr_blkno = le64_to_cpu(rec->e_blkno) +
  		(res->sr_bit_offset - bitoff);
13e434cf0   Joel Becker   ocfs2: Trim subal...
1567
1568
1569
1570
  	if ((res->sr_bit_offset + res->sr_bits) > (bitoff + bitcount))
  		res->sr_bits = (bitoff + bitcount) - res->sr_bit_offset;
  	return 1;
  }
ba2066351   Joel Becker   ocfs2: Return all...
1571
1572
1573
  static void ocfs2_bg_discontig_fix_result(struct ocfs2_alloc_context *ac,
  					  struct ocfs2_group_desc *bg,
  					  struct ocfs2_suballoc_result *res)
13e434cf0   Joel Becker   ocfs2: Trim subal...
1574
1575
  {
  	int i;
2b6cb576a   Joel Becker   ocfs2: Set suball...
1576
  	u64 bg_blkno = res->sr_bg_blkno;  /* Save off */
13e434cf0   Joel Becker   ocfs2: Trim subal...
1577
1578
1579
  	struct ocfs2_extent_rec *rec;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
  	struct ocfs2_chain_list *cl = &di->id2.i_chain;
ba2066351   Joel Becker   ocfs2: Return all...
1580
1581
  	if (ocfs2_is_cluster_bitmap(ac->ac_inode)) {
  		res->sr_blkno = 0;
13e434cf0   Joel Becker   ocfs2: Trim subal...
1582
  		return;
ba2066351   Joel Becker   ocfs2: Return all...
1583
  	}
13e434cf0   Joel Becker   ocfs2: Trim subal...
1584

ba2066351   Joel Becker   ocfs2: Return all...
1585
  	res->sr_blkno = res->sr_bg_blkno + res->sr_bit_offset;
2b6cb576a   Joel Becker   ocfs2: Set suball...
1586
  	res->sr_bg_blkno = 0;  /* Clear it for contig block groups */
4711954ea   Tao Ma   ocfs2: Some tiny ...
1587
  	if (!ocfs2_supports_discontig_bg(OCFS2_SB(ac->ac_inode->i_sb)) ||
ba2066351   Joel Becker   ocfs2: Return all...
1588
  	    !bg->bg_list.l_next_free_rec)
13e434cf0   Joel Becker   ocfs2: Trim subal...
1589
1590
1591
1592
  		return;
  
  	for (i = 0; i < le16_to_cpu(bg->bg_list.l_next_free_rec); i++) {
  		rec = &bg->bg_list.l_recs[i];
2b6cb576a   Joel Becker   ocfs2: Set suball...
1593
1594
  		if (ocfs2_bg_discontig_fix_by_rec(res, rec, cl)) {
  			res->sr_bg_blkno = bg_blkno;  /* Restore */
13e434cf0   Joel Becker   ocfs2: Trim subal...
1595
  			break;
2b6cb576a   Joel Becker   ocfs2: Set suball...
1596
  		}
13e434cf0   Joel Becker   ocfs2: Trim subal...
1597
1598
  	}
  }
883d4cae4   Mark Fasheh   ocfs2: allocation...
1599
  static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1600
  				  handle_t *handle,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1601
1602
  				  u32 bits_wanted,
  				  u32 min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1603
  				  struct ocfs2_suballoc_result *res,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1604
1605
1606
  				  u16 *bits_left)
  {
  	int ret;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1607
1608
  	struct buffer_head *group_bh = NULL;
  	struct ocfs2_group_desc *gd;
68f64d471   Joel Becker   ocfs2: Wrap group...
1609
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1610
  	struct inode *alloc_inode = ac->ac_inode;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1611

7d1fe093b   Joel Becker   ocfs2: Pass subal...
1612
1613
  	ret = ocfs2_read_group_descriptor(alloc_inode, di,
  					  res->sr_bg_blkno, &group_bh);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1614
1615
1616
1617
1618
1619
  	if (ret < 0) {
  		mlog_errno(ret);
  		return ret;
  	}
  
  	gd = (struct ocfs2_group_desc *) group_bh->b_data;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1620
  	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1621
  				  ac->ac_max_block, res);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1622
1623
1624
1625
1626
  	if (ret < 0) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
  		goto out;
  	}
13e434cf0   Joel Becker   ocfs2: Trim subal...
1627
  	if (!ret)
ba2066351   Joel Becker   ocfs2: Return all...
1628
  		ocfs2_bg_discontig_fix_result(ac, gd, res);
13e434cf0   Joel Becker   ocfs2: Trim subal...
1629

e49e27674   Mark Fasheh   ocfs2: allow retu...
1630
1631
1632
1633
1634
1635
1636
1637
  	/*
  	 * sr_bg_blkno might have been changed by
  	 * ocfs2_bg_discontig_fix_result
  	 */
  	res->sr_bg_stable_blkno = group_bh->b_blocknr;
  
  	if (ac->ac_find_loc_only)
  		goto out_loc_only;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1638
  	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1639
  					       res->sr_bits,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1640
1641
1642
1643
1644
1645
1646
  					       le16_to_cpu(gd->bg_chain));
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ret = ocfs2_block_group_set_bits(handle, alloc_inode, gd, group_bh,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1647
  					 res->sr_bit_offset, res->sr_bits);
db66c7157   Younger Liu   ocfs2: rollback a...
1648
1649
1650
1651
  	if (ret < 0) {
  		ocfs2_rollback_alloc_dinode_counts(alloc_inode, ac->ac_bh,
  					       res->sr_bits,
  					       le16_to_cpu(gd->bg_chain));
883d4cae4   Mark Fasheh   ocfs2: allocation...
1652
  		mlog_errno(ret);
db66c7157   Younger Liu   ocfs2: rollback a...
1653
  	}
883d4cae4   Mark Fasheh   ocfs2: allocation...
1654

e49e27674   Mark Fasheh   ocfs2: allow retu...
1655
  out_loc_only:
883d4cae4   Mark Fasheh   ocfs2: allocation...
1656
1657
1658
1659
1660
1661
1662
  	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
  
  out:
  	brelse(group_bh);
  
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1663
  static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1664
  			      handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1665
1666
  			      u32 bits_wanted,
  			      u32 min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1667
  			      struct ocfs2_suballoc_result *res,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1668
  			      u16 *bits_left)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1669
1670
  {
  	int status;
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1671
  	u16 chain;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1672
  	u64 next_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1673
1674
1675
1676
1677
1678
1679
1680
  	struct inode *alloc_inode = ac->ac_inode;
  	struct buffer_head *group_bh = NULL;
  	struct buffer_head *prev_group_bh = NULL;
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  	struct ocfs2_chain_list *cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  	struct ocfs2_group_desc *bg;
  
  	chain = ac->ac_chain;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1681
1682
1683
  	trace_ocfs2_search_chain_begin(
  		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
  		bits_wanted, chain);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1684

68f64d471   Joel Becker   ocfs2: Wrap group...
1685
1686
1687
  	status = ocfs2_read_group_descriptor(alloc_inode, fe,
  					     le64_to_cpu(cl->cl_recs[chain].c_blkno),
  					     &group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1688
1689
1690
1691
1692
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1693
1694
1695
1696
  
  	status = -ENOSPC;
  	/* for now, the chain search is a bit simplistic. We just use
  	 * the 1st group with any empty bits. */
1187c9688   Joel Becker   ocfs2: Limit inod...
1697
1698
  	while ((status = ac->ac_group_search(alloc_inode, group_bh,
  					     bits_wanted, min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1699
1700
  					     ac->ac_max_block,
  					     res)) == -ENOSPC) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1701
1702
  		if (!bg->bg_next_group)
  			break;
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
1703
1704
1705
  
  		brelse(prev_group_bh);
  		prev_group_bh = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1706
1707
1708
  		next_group = le64_to_cpu(bg->bg_next_group);
  		prev_group_bh = group_bh;
  		group_bh = NULL;
68f64d471   Joel Becker   ocfs2: Wrap group...
1709
1710
  		status = ocfs2_read_group_descriptor(alloc_inode, fe,
  						     next_group, &group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1711
1712
1713
1714
1715
  		if (status < 0) {
  			mlog_errno(status);
  			goto bail;
  		}
  		bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1716
1717
1718
1719
1720
1721
  	}
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1722
1723
  	trace_ocfs2_search_chain_succ(
  		(unsigned long long)le64_to_cpu(bg->bg_blkno), res->sr_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1724

7d1fe093b   Joel Becker   ocfs2: Pass subal...
1725
  	res->sr_bg_blkno = le64_to_cpu(bg->bg_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1726

7d1fe093b   Joel Becker   ocfs2: Pass subal...
1727
  	BUG_ON(res->sr_bits == 0);
13e434cf0   Joel Becker   ocfs2: Trim subal...
1728
  	if (!status)
ba2066351   Joel Becker   ocfs2: Return all...
1729
  		ocfs2_bg_discontig_fix_result(ac, bg, res);
13e434cf0   Joel Becker   ocfs2: Trim subal...
1730

e49e27674   Mark Fasheh   ocfs2: allow retu...
1731
1732
1733
1734
1735
  	/*
  	 * sr_bg_blkno might have been changed by
  	 * ocfs2_bg_discontig_fix_result
  	 */
  	res->sr_bg_stable_blkno = group_bh->b_blocknr;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
  
  	/*
  	 * Keep track of previous block descriptor read. When
  	 * we find a target, if we have read more than X
  	 * number of descriptors, and the target is reasonably
  	 * empty, relink him to top of his chain.
  	 *
  	 * We've read 0 extra blocks and only send one more to
  	 * the transaction, yet the next guy to search has a
  	 * much easier time.
  	 *
  	 * Do this *after* figuring out how many bits we're taking out
  	 * of our target group.
  	 */
309a85b68   Xiaowei.Hu   ocfs2: ac->ac_all...
1750
  	if (!ac->ac_disable_chain_relink &&
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1751
  	    (prev_group_bh) &&
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1752
  	    (ocfs2_block_group_reasonably_empty(bg, res->sr_bits))) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1753
1754
1755
1756
1757
1758
1759
1760
  		status = ocfs2_relink_block_group(handle, alloc_inode,
  						  ac->ac_bh, group_bh,
  						  prev_group_bh, chain);
  		if (status < 0) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
e49e27674   Mark Fasheh   ocfs2: allow retu...
1761
1762
  	if (ac->ac_find_loc_only)
  		goto out_loc_only;
d51349829   Mark Fasheh   ocfs2: use ocfs2_...
1763
1764
1765
1766
  	status = ocfs2_alloc_dinode_update_counts(alloc_inode, handle,
  						  ac->ac_bh, res->sr_bits,
  						  chain);
  	if (status) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1767
1768
1769
  		mlog_errno(status);
  		goto bail;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1770
1771
1772
1773
  	status = ocfs2_block_group_set_bits(handle,
  					    alloc_inode,
  					    bg,
  					    group_bh,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1774
1775
  					    res->sr_bit_offset,
  					    res->sr_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1776
  	if (status < 0) {
db66c7157   Younger Liu   ocfs2: rollback a...
1777
1778
  		ocfs2_rollback_alloc_dinode_counts(alloc_inode,
  					ac->ac_bh, res->sr_bits, chain);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1779
1780
1781
  		mlog_errno(status);
  		goto bail;
  	}
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1782
1783
1784
  	trace_ocfs2_search_chain_end(
  			(unsigned long long)le64_to_cpu(fe->i_blkno),
  			res->sr_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1785

e49e27674   Mark Fasheh   ocfs2: allow retu...
1786
  out_loc_only:
883d4cae4   Mark Fasheh   ocfs2: allocation...
1787
  	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1788
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
1789
1790
  	brelse(group_bh);
  	brelse(prev_group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1791

c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1792
1793
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1794
1795
1796
1797
  	return status;
  }
  
  /* will give out up to bits_wanted contiguous bits. */
aa8f8e93c   Joel Becker   ocfs2: ocfs2_clai...
1798
  static int ocfs2_claim_suballoc_bits(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1799
  				     handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1800
1801
  				     u32 bits_wanted,
  				     u32 min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1802
  				     struct ocfs2_suballoc_result *res)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1803
1804
1805
  {
  	int status;
  	u16 victim, i;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1806
  	u16 bits_left = 0;
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1807
  	u64 hint = ac->ac_last_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1808
1809
  	struct ocfs2_chain_list *cl;
  	struct ocfs2_dinode *fe;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1810
1811
1812
1813
1814
  	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  	BUG_ON(bits_wanted > (ac->ac_bits_wanted - ac->ac_bits_given));
  	BUG_ON(!ac->ac_bh);
  
  	fe = (struct ocfs2_dinode *) ac->ac_bh->b_data;
10995aa24   Joel Becker   ocfs2: Morph the ...
1815
1816
1817
1818
  
  	/* The bh was validated by the inode read during
  	 * ocfs2_reserve_suballoc_bits().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1819
1820
  	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
1821
  		status = ocfs2_error(ac->ac_inode->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
1822
1823
1824
1825
1826
  				     "Chain allocator dinode %llu has %u used bits but only %u total
  ",
  				     (unsigned long long)le64_to_cpu(fe->i_blkno),
  				     le32_to_cpu(fe->id1.bitmap1.i_used),
  				     le32_to_cpu(fe->id1.bitmap1.i_total));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1827
1828
  		goto bail;
  	}
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1829
  	res->sr_bg_blkno = hint;
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1830
  	if (res->sr_bg_blkno) {
883d4cae4   Mark Fasheh   ocfs2: allocation...
1831
1832
  		/* Attempt to short-circuit the usual search mechanism
  		 * by jumping straight to the most recently used
b595076a1   Uwe Kleine-König   tree-wide: fix co...
1833
  		 * allocation group. This helps us maintain some
883d4cae4   Mark Fasheh   ocfs2: allocation...
1834
  		 * contiguousness across allocations. */
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1835
  		status = ocfs2_search_one_group(ac, handle, bits_wanted,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1836
1837
  						min_bits, res, &bits_left);
  		if (!status)
883d4cae4   Mark Fasheh   ocfs2: allocation...
1838
  			goto set_hint;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1839
1840
1841
1842
1843
  		if (status < 0 && status != -ENOSPC) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1844
1845
1846
1847
  	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  
  	victim = ocfs2_find_victim_chain(cl);
  	ac->ac_chain = victim;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1848

7d1fe093b   Joel Becker   ocfs2: Pass subal...
1849
1850
  	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
  				    res, &bits_left);
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1851
  	if (!status) {
1d1aff8cf   Joseph Qi   ocfs2: improve pe...
1852
1853
1854
1855
  		if (ocfs2_is_cluster_bitmap(ac->ac_inode))
  			hint = res->sr_bg_blkno;
  		else
  			hint = ocfs2_group_from_res(res);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1856
  		goto set_hint;
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1857
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1858
1859
1860
1861
  	if (status < 0 && status != -ENOSPC) {
  		mlog_errno(status);
  		goto bail;
  	}
2f73e135b   Tao Ma   ocfs2: Remove mlo...
1862
  	trace_ocfs2_claim_suballoc_bits(victim);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1863
1864
1865
1866
1867
  
  	/* If we didn't pick a good victim, then just default to
  	 * searching each chain in order. Don't allow chain relinking
  	 * because we only calculate enough journal credits for one
  	 * relink per alloc. */
309a85b68   Xiaowei.Hu   ocfs2: ac->ac_all...
1868
  	ac->ac_disable_chain_relink = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1869
1870
1871
1872
1873
1874
1875
  	for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i ++) {
  		if (i == victim)
  			continue;
  		if (!cl->cl_recs[i].c_free)
  			continue;
  
  		ac->ac_chain = i;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1876
  		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1877
  					    res, &bits_left);
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1878
1879
  		if (!status) {
  			hint = ocfs2_group_from_res(res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1880
  			break;
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1881
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1882
1883
1884
1885
1886
  		if (status < 0 && status != -ENOSPC) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1887

883d4cae4   Mark Fasheh   ocfs2: allocation...
1888
1889
1890
1891
1892
1893
1894
1895
  set_hint:
  	if (status != -ENOSPC) {
  		/* If the next search of this group is not likely to
  		 * yield a suitable extent, then we reset the last
  		 * group hint so as to not waste a disk read */
  		if (bits_left < min_bits)
  			ac->ac_last_group = 0;
  		else
b2b6ebf5f   Mark Fasheh   ocfs2: properly s...
1896
  			ac->ac_last_group = hint;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1897
1898
1899
  	}
  
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1900
1901
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1902
1903
  	return status;
  }
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
1904
  int ocfs2_claim_metadata(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1905
1906
  			 struct ocfs2_alloc_context *ac,
  			 u32 bits_wanted,
2b6cb576a   Joel Becker   ocfs2: Set suball...
1907
  			 u64 *suballoc_loc,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1908
1909
1910
1911
1912
  			 u16 *suballoc_bit_start,
  			 unsigned int *num_bits,
  			 u64 *blkno_start)
  {
  	int status;
ba2066351   Joel Becker   ocfs2: Return all...
1913
  	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1914
1915
1916
1917
  
  	BUG_ON(!ac);
  	BUG_ON(ac->ac_bits_wanted < (ac->ac_bits_given + bits_wanted));
  	BUG_ON(ac->ac_which != OCFS2_AC_USE_META);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1918

aa8f8e93c   Joel Becker   ocfs2: ocfs2_clai...
1919
  	status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1920
  					   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1921
1922
  					   bits_wanted,
  					   1,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1923
  					   &res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1924
1925
1926
1927
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
1928
  	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1929

2b6cb576a   Joel Becker   ocfs2: Set suball...
1930
  	*suballoc_loc = res.sr_bg_blkno;
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1931
  	*suballoc_bit_start = res.sr_bit_offset;
ba2066351   Joel Becker   ocfs2: Return all...
1932
  	*blkno_start = res.sr_blkno;
7d1fe093b   Joel Becker   ocfs2: Pass subal...
1933
1934
  	ac->ac_bits_given += res.sr_bits;
  	*num_bits = res.sr_bits;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1935
1936
  	status = 0;
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1937
1938
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1939
1940
  	return status;
  }
138211515   Tao Ma   ocfs2: Optimize i...
1941
  static void ocfs2_init_inode_ac_group(struct inode *dir,
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
1942
  				      struct buffer_head *parent_di_bh,
138211515   Tao Ma   ocfs2: Optimize i...
1943
1944
  				      struct ocfs2_alloc_context *ac)
  {
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
1945
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_di_bh->b_data;
138211515   Tao Ma   ocfs2: Optimize i...
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
  	/*
  	 * Try to allocate inodes from some specific group.
  	 *
  	 * If the parent dir has recorded the last group used in allocation,
  	 * cool, use it. Otherwise if we try to allocate new inode from the
  	 * same slot the parent dir belongs to, use the same chunk.
  	 *
  	 * We are very careful here to avoid the mistake of setting
  	 * ac_last_group to a group descriptor from a different (unlocked) slot.
  	 */
  	if (OCFS2_I(dir)->ip_last_used_group &&
  	    OCFS2_I(dir)->ip_last_used_slot == ac->ac_alloc_slot)
  		ac->ac_last_group = OCFS2_I(dir)->ip_last_used_group;
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
1959
1960
1961
1962
1963
1964
1965
1966
  	else if (le16_to_cpu(di->i_suballoc_slot) == ac->ac_alloc_slot) {
  		if (di->i_suballoc_loc)
  			ac->ac_last_group = le64_to_cpu(di->i_suballoc_loc);
  		else
  			ac->ac_last_group = ocfs2_which_suballoc_group(
  					le64_to_cpu(di->i_blkno),
  					le16_to_cpu(di->i_suballoc_bit));
  	}
138211515   Tao Ma   ocfs2: Optimize i...
1967
1968
1969
1970
1971
1972
1973
1974
  }
  
  static inline void ocfs2_save_inode_ac_group(struct inode *dir,
  					     struct ocfs2_alloc_context *ac)
  {
  	OCFS2_I(dir)->ip_last_used_group = ac->ac_last_group;
  	OCFS2_I(dir)->ip_last_used_slot = ac->ac_alloc_slot;
  }
e49e27674   Mark Fasheh   ocfs2: allow retu...
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
  int ocfs2_find_new_inode_loc(struct inode *dir,
  			     struct buffer_head *parent_fe_bh,
  			     struct ocfs2_alloc_context *ac,
  			     u64 *fe_blkno)
  {
  	int ret;
  	handle_t *handle = NULL;
  	struct ocfs2_suballoc_result *res;
  
  	BUG_ON(!ac);
  	BUG_ON(ac->ac_bits_given != 0);
  	BUG_ON(ac->ac_bits_wanted != 1);
  	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
  
  	res = kzalloc(sizeof(*res), GFP_NOFS);
  	if (res == NULL) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
  
  	/*
  	 * The handle started here is for chain relink. Alternatively,
  	 * we could just disable relink for these calls.
  	 */
  	handle = ocfs2_start_trans(OCFS2_SB(dir->i_sb), OCFS2_SUBALLOC_ALLOC);
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		handle = NULL;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	/*
  	 * This will instruct ocfs2_claim_suballoc_bits and
  	 * ocfs2_search_one_group to search but save actual allocation
  	 * for later.
  	 */
  	ac->ac_find_loc_only = 1;
  
  	ret = ocfs2_claim_suballoc_bits(ac, handle, 1, 1, res);
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ac->ac_find_loc_priv = res;
  	*fe_blkno = res->sr_blkno;
6fdb702d6   Darrick J. Wong   ocfs2: call ocfs2...
2025
  	ocfs2_update_inode_fsync_trans(handle, dir, 0);
e49e27674   Mark Fasheh   ocfs2: allow retu...
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
  out:
  	if (handle)
  		ocfs2_commit_trans(OCFS2_SB(dir->i_sb), handle);
  
  	if (ret)
  		kfree(res);
  
  	return ret;
  }
  
  int ocfs2_claim_new_inode_at_loc(handle_t *handle,
  				 struct inode *dir,
  				 struct ocfs2_alloc_context *ac,
  				 u64 *suballoc_loc,
  				 u16 *suballoc_bit,
  				 u64 di_blkno)
  {
  	int ret;
  	u16 chain;
  	struct ocfs2_suballoc_result *res = ac->ac_find_loc_priv;
  	struct buffer_head *bg_bh = NULL;
  	struct ocfs2_group_desc *bg;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) ac->ac_bh->b_data;
  
  	/*
  	 * Since di_blkno is being passed back in, we check for any
  	 * inconsistencies which may have happened between
  	 * calls. These are code bugs as di_blkno is not expected to
  	 * change once returned from ocfs2_find_new_inode_loc()
  	 */
  	BUG_ON(res->sr_blkno != di_blkno);
  
  	ret = ocfs2_read_group_descriptor(ac->ac_inode, di,
  					  res->sr_bg_stable_blkno, &bg_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  	chain = le16_to_cpu(bg->bg_chain);
  
  	ret = ocfs2_alloc_dinode_update_counts(ac->ac_inode, handle,
  					       ac->ac_bh, res->sr_bits,
  					       chain);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ret = ocfs2_block_group_set_bits(handle,
  					 ac->ac_inode,
  					 bg,
  					 bg_bh,
  					 res->sr_bit_offset,
  					 res->sr_bits);
  	if (ret < 0) {
db66c7157   Younger Liu   ocfs2: rollback a...
2083
2084
  		ocfs2_rollback_alloc_dinode_counts(ac->ac_inode,
  					       ac->ac_bh, res->sr_bits, chain);
e49e27674   Mark Fasheh   ocfs2: allow retu...
2085
2086
2087
  		mlog_errno(ret);
  		goto out;
  	}
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2088
2089
  	trace_ocfs2_claim_new_inode_at_loc((unsigned long long)di_blkno,
  					   res->sr_bits);
e49e27674   Mark Fasheh   ocfs2: allow retu...
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
  
  	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
  
  	BUG_ON(res->sr_bits != 1);
  
  	*suballoc_loc = res->sr_bg_blkno;
  	*suballoc_bit = res->sr_bit_offset;
  	ac->ac_bits_given++;
  	ocfs2_save_inode_ac_group(dir, ac);
  
  out:
  	brelse(bg_bh);
  
  	return ret;
  }
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2105
  int ocfs2_claim_new_inode(handle_t *handle,
138211515   Tao Ma   ocfs2: Optimize i...
2106
2107
  			  struct inode *dir,
  			  struct buffer_head *parent_fe_bh,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2108
  			  struct ocfs2_alloc_context *ac,
2b6cb576a   Joel Becker   ocfs2: Set suball...
2109
  			  u64 *suballoc_loc,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2110
2111
2112
2113
  			  u16 *suballoc_bit,
  			  u64 *fe_blkno)
  {
  	int status;
2b6cb576a   Joel Becker   ocfs2: Set suball...
2114
  	struct ocfs2_suballoc_result res;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2115

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2116
2117
2118
2119
  	BUG_ON(!ac);
  	BUG_ON(ac->ac_bits_given != 0);
  	BUG_ON(ac->ac_bits_wanted != 1);
  	BUG_ON(ac->ac_which != OCFS2_AC_USE_INODE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2120

138211515   Tao Ma   ocfs2: Optimize i...
2121
  	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
aa8f8e93c   Joel Becker   ocfs2: ocfs2_clai...
2122
  	status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
2123
  					   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2124
2125
  					   1,
  					   1,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
2126
  					   &res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2127
2128
2129
2130
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2131
  	atomic_inc(&OCFS2_SB(ac->ac_inode->i_sb)->alloc_stats.bg_allocs);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2132

7d1fe093b   Joel Becker   ocfs2: Pass subal...
2133
  	BUG_ON(res.sr_bits != 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2134

2b6cb576a   Joel Becker   ocfs2: Set suball...
2135
  	*suballoc_loc = res.sr_bg_blkno;
7d1fe093b   Joel Becker   ocfs2: Pass subal...
2136
  	*suballoc_bit = res.sr_bit_offset;
ba2066351   Joel Becker   ocfs2: Return all...
2137
  	*fe_blkno = res.sr_blkno;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2138
  	ac->ac_bits_given++;
138211515   Tao Ma   ocfs2: Optimize i...
2139
  	ocfs2_save_inode_ac_group(dir, ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2140
2141
  	status = 0;
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
2142
2143
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
  	return status;
  }
  
  /* translate a group desc. blkno and it's bitmap offset into
   * disk cluster offset. */
  static inline u32 ocfs2_desc_bitmap_to_cluster_off(struct inode *inode,
  						   u64 bg_blkno,
  						   u16 bg_bit_off)
  {
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	u32 cluster = 0;
  
  	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  
  	if (bg_blkno != osb->first_cluster_group_blkno)
  		cluster = ocfs2_blocks_to_clusters(inode->i_sb, bg_blkno);
  	cluster += (u32) bg_bit_off;
  	return cluster;
  }
  
  /* given a cluster offset, calculate which block group it belongs to
   * and return that block offset. */
d659072f7   Tao Ma   [PATCH 1/2] ocfs2...
2166
  u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
  {
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	u32 group_no;
  
  	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  
  	group_no = cluster / osb->bitmap_cpg;
  	if (!group_no)
  		return osb->first_cluster_group_blkno;
  	return ocfs2_clusters_to_blocks(inode->i_sb,
  					group_no * osb->bitmap_cpg);
  }
  
  /* given the block number of a cluster start, calculate which cluster
   * group and descriptor bitmap offset that corresponds to. */
  static inline void ocfs2_block_to_cluster_group(struct inode *inode,
  						u64 data_blkno,
  						u64 *bg_blkno,
  						u16 *bg_bit_off)
  {
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  	u32 data_cluster = ocfs2_blocks_to_clusters(osb->sb, data_blkno);
  
  	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
  
  	*bg_blkno = ocfs2_which_cluster_group(inode,
  					      data_cluster);
  
  	if (*bg_blkno == osb->first_cluster_group_blkno)
  		*bg_bit_off = (u16) data_cluster;
  	else
  		*bg_bit_off = (u16) ocfs2_blocks_to_clusters(osb->sb,
  							     data_blkno - *bg_blkno);
  }
  
  /*
   * min_bits - minimum contiguous chunk from this total allocation we
   * can handle. set to what we asked for originally for a full
   * contig. allocation, set to '1' to indicate we can deal with extents
   * of any size.
   */
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2208
  int __ocfs2_claim_clusters(handle_t *handle,
415cb8003   Mark Fasheh   ocfs2: Allow smal...
2209
2210
2211
2212
2213
  			   struct ocfs2_alloc_context *ac,
  			   u32 min_clusters,
  			   u32 max_clusters,
  			   u32 *cluster_start,
  			   u32 *num_clusters)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2214
2215
  {
  	int status;
415cb8003   Mark Fasheh   ocfs2: Allow smal...
2216
  	unsigned int bits_wanted = max_clusters;
ba2066351   Joel Becker   ocfs2: Return all...
2217
  	struct ocfs2_suballoc_result res = { .sr_blkno = 0, };
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2218
  	struct ocfs2_super *osb = OCFS2_SB(ac->ac_inode->i_sb);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2219

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2220
2221
2222
2223
  	BUG_ON(ac->ac_bits_given >= ac->ac_bits_wanted);
  
  	BUG_ON(ac->ac_which != OCFS2_AC_USE_LOCAL
  	       && ac->ac_which != OCFS2_AC_USE_MAIN);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2224
2225
  
  	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
33d5d380d   Mark Fasheh   ocfs2: allocate b...
2226
  		WARN_ON(min_clusters > 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
  		status = ocfs2_claim_local_alloc_bits(osb,
  						      handle,
  						      ac,
  						      bits_wanted,
  						      cluster_start,
  						      num_clusters);
  		if (!status)
  			atomic_inc(&osb->alloc_stats.local_data);
  	} else {
  		if (min_clusters > (osb->bitmap_cpg - 1)) {
  			/* The only paths asking for contiguousness
  			 * should know about this already. */
2fbe8d1eb   Sunil Mushran   ocfs2: Local allo...
2239
2240
2241
2242
  			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
  			     "group bitmap size %u!
  ", min_clusters,
  			     osb->bitmap_cpg);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2243
2244
2245
2246
2247
2248
  			status = -ENOSPC;
  			goto bail;
  		}
  		/* clamp the current request down to a realistic size. */
  		if (bits_wanted > (osb->bitmap_cpg - 1))
  			bits_wanted = osb->bitmap_cpg - 1;
aa8f8e93c   Joel Becker   ocfs2: ocfs2_clai...
2249
  		status = ocfs2_claim_suballoc_bits(ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
2250
  						   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2251
2252
  						   bits_wanted,
  						   min_clusters,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
2253
  						   &res);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2254
  		if (!status) {
ba2066351   Joel Becker   ocfs2: Return all...
2255
  			BUG_ON(res.sr_blkno); /* cluster alloc can't set */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2256
2257
  			*cluster_start =
  				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
7d1fe093b   Joel Becker   ocfs2: Pass subal...
2258
2259
  								 res.sr_bg_blkno,
  								 res.sr_bit_offset);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2260
  			atomic_inc(&osb->alloc_stats.bitmap_data);
4711954ea   Tao Ma   ocfs2: Some tiny ...
2261
  			*num_clusters = res.sr_bits;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2262
2263
2264
2265
2266
2267
2268
  		}
  	}
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
4711954ea   Tao Ma   ocfs2: Some tiny ...
2269
  	ac->ac_bits_given += *num_clusters;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2270
2271
  
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
2272
2273
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2274
2275
  	return status;
  }
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2276
  int ocfs2_claim_clusters(handle_t *handle,
415cb8003   Mark Fasheh   ocfs2: Allow smal...
2277
2278
2279
2280
2281
2282
  			 struct ocfs2_alloc_context *ac,
  			 u32 min_clusters,
  			 u32 *cluster_start,
  			 u32 *num_clusters)
  {
  	unsigned int bits_wanted = ac->ac_bits_wanted - ac->ac_bits_given;
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2283
  	return __ocfs2_claim_clusters(handle, ac, min_clusters,
415cb8003   Mark Fasheh   ocfs2: Allow smal...
2284
2285
  				      bits_wanted, cluster_start, num_clusters);
  }
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2286
2287
2288
2289
2290
2291
2292
2293
  static int ocfs2_block_group_clear_bits(handle_t *handle,
  					struct inode *alloc_inode,
  					struct ocfs2_group_desc *bg,
  					struct buffer_head *group_bh,
  					unsigned int bit_off,
  					unsigned int num_bits,
  					void (*undo_fn)(unsigned int bit,
  							unsigned long *bmap))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2294
2295
2296
  {
  	int status;
  	unsigned int tmp;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2297
  	struct ocfs2_group_desc *undo_bg = NULL;
464170647   Thomas Gleixner   jbd2: Make state ...
2298
  	struct journal_head *jh;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2299

420353061   Joel Becker   ocfs2: Morph the ...
2300
2301
2302
  	/* The caller got this descriptor from
  	 * ocfs2_read_group_descriptor().  Any corruption is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_GROUP_DESC(bg));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2303

2f73e135b   Tao Ma   ocfs2: Remove mlo...
2304
  	trace_ocfs2_block_group_clear_bits(bit_off, num_bits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2305

b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2306
  	BUG_ON(undo_fn && !ocfs2_is_cluster_bitmap(alloc_inode));
0cf2f7632   Joel Becker   ocfs2: Pass struc...
2307
  	status = ocfs2_journal_access_gd(handle, INODE_CACHE(alloc_inode),
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2308
2309
2310
2311
  					 group_bh,
  					 undo_fn ?
  					 OCFS2_JOURNAL_ACCESS_UNDO :
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2312
2313
2314
2315
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
464170647   Thomas Gleixner   jbd2: Make state ...
2316
  	jh = bh2jh(group_bh);
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2317
  	if (undo_fn) {
464170647   Thomas Gleixner   jbd2: Make state ...
2318
2319
  		spin_lock(&jh->b_state_lock);
  		undo_bg = (struct ocfs2_group_desc *) jh->b_committed_data;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
2320
2321
  		BUG_ON(!undo_bg);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2322
2323
2324
2325
2326
  
  	tmp = num_bits;
  	while(tmp--) {
  		ocfs2_clear_bit((bit_off + tmp),
  				(unsigned long *) bg->bg_bitmap);
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2327
2328
2329
  		if (undo_fn)
  			undo_fn(bit_off + tmp,
  				(unsigned long *) undo_bg->bg_bitmap);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2330
2331
  	}
  	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
9b5cd10e4   Srinivas Eeda   ocfs2: validate b...
2332
  	if (le16_to_cpu(bg->bg_free_bits_count) > le16_to_cpu(bg->bg_bits)) {
e75ed71be   Changwei Ge   ocfs2: unlock bh_...
2333
  		if (undo_fn)
464170647   Thomas Gleixner   jbd2: Make state ...
2334
  			spin_unlock(&jh->b_state_lock);
7ecef14ab   Joe Perches   ocfs2: neaten do_...
2335
2336
2337
2338
2339
2340
  		return ocfs2_error(alloc_inode->i_sb, "Group descriptor # %llu has bit count %u but claims %u are freed. num_bits %d
  ",
  				   (unsigned long long)le64_to_cpu(bg->bg_blkno),
  				   le16_to_cpu(bg->bg_bits),
  				   le16_to_cpu(bg->bg_free_bits_count),
  				   num_bits);
9b5cd10e4   Srinivas Eeda   ocfs2: validate b...
2341
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2342

b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2343
  	if (undo_fn)
464170647   Thomas Gleixner   jbd2: Make state ...
2344
  		spin_unlock(&jh->b_state_lock);
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
2345

ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2346
  	ocfs2_journal_dirty(handle, group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2347
2348
2349
2350
2351
2352
2353
  bail:
  	return status;
  }
  
  /*
   * expects the suballoc inode to already be locked.
   */
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2354
2355
2356
2357
2358
2359
2360
2361
  static int _ocfs2_free_suballoc_bits(handle_t *handle,
  				     struct inode *alloc_inode,
  				     struct buffer_head *alloc_bh,
  				     unsigned int start_bit,
  				     u64 bg_blkno,
  				     unsigned int count,
  				     void (*undo_fn)(unsigned int bit,
  						     unsigned long *bitmap))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2362
2363
2364
  {
  	int status = 0;
  	u32 tmp_used;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2365
2366
2367
2368
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) alloc_bh->b_data;
  	struct ocfs2_chain_list *cl = &fe->id2.i_chain;
  	struct buffer_head *group_bh = NULL;
  	struct ocfs2_group_desc *group;
10995aa24   Joel Becker   ocfs2: Morph the ...
2369
2370
2371
  	/* The alloc_bh comes from ocfs2_free_dinode() or
  	 * ocfs2_free_clusters().  The callers have all locked the
  	 * allocator and gotten alloc_bh from the lock call.  This
25985edce   Lucas De Marchi   Fix common misspe...
2372
  	 * validates the dinode buffer.  Any corruption that has happened
10995aa24   Joel Becker   ocfs2: Morph the ...
2373
2374
  	 * is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2375
  	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2376
2377
2378
2379
  	trace_ocfs2_free_suballoc_bits(
  		(unsigned long long)OCFS2_I(alloc_inode)->ip_blkno,
  		(unsigned long long)bg_blkno,
  		start_bit, count);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2380

68f64d471   Joel Becker   ocfs2: Wrap group...
2381
2382
  	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
  					     &group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2383
2384
2385
2386
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2387
  	group = (struct ocfs2_group_desc *) group_bh->b_data;
68f64d471   Joel Becker   ocfs2: Wrap group...
2388

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2389
2390
2391
2392
  	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  
  	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  					      group, group_bh,
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2393
  					      start_bit, count, undo_fn);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2394
2395
2396
2397
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
0cf2f7632   Joel Becker   ocfs2: Pass struc...
2398
2399
  	status = ocfs2_journal_access_di(handle, INODE_CACHE(alloc_inode),
  					 alloc_bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2400
2401
  	if (status < 0) {
  		mlog_errno(status);
a47726bcf   Joseph Qi   ocfs2: rollback t...
2402
2403
  		ocfs2_block_group_set_bits(handle, alloc_inode, group, group_bh,
  				start_bit, count);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2404
2405
2406
2407
2408
2409
2410
  		goto bail;
  	}
  
  	le32_add_cpu(&cl->cl_recs[le16_to_cpu(group->bg_chain)].c_free,
  		     count);
  	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  	fe->id1.bitmap1.i_used = cpu_to_le32(tmp_used - count);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2411
  	ocfs2_journal_dirty(handle, alloc_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2412
2413
  
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
2414
  	brelse(group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2415
2416
  	return status;
  }
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
  int ocfs2_free_suballoc_bits(handle_t *handle,
  			     struct inode *alloc_inode,
  			     struct buffer_head *alloc_bh,
  			     unsigned int start_bit,
  			     u64 bg_blkno,
  			     unsigned int count)
  {
  	return _ocfs2_free_suballoc_bits(handle, alloc_inode, alloc_bh,
  					 start_bit, bg_blkno, count, NULL);
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
2427
  int ocfs2_free_dinode(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2428
2429
2430
2431
2432
2433
2434
  		      struct inode *inode_alloc_inode,
  		      struct buffer_head *inode_alloc_bh,
  		      struct ocfs2_dinode *di)
  {
  	u64 blk = le64_to_cpu(di->i_blkno);
  	u16 bit = le16_to_cpu(di->i_suballoc_bit);
  	u64 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
74380c479   Tao Ma   ocfs2: Free block...
2435
2436
  	if (di->i_suballoc_loc)
  		bg_blkno = le64_to_cpu(di->i_suballoc_loc);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2437
2438
2439
  	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  					inode_alloc_bh, bit, bg_blkno, 1);
  }
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2440
2441
2442
2443
2444
2445
2446
  static int _ocfs2_free_clusters(handle_t *handle,
  				struct inode *bitmap_inode,
  				struct buffer_head *bitmap_bh,
  				u64 start_blk,
  				unsigned int num_clusters,
  				void (*undo_fn)(unsigned int bit,
  						unsigned long *bitmap))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2447
2448
2449
2450
  {
  	int status;
  	u16 bg_start_bit;
  	u64 bg_blkno;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2451
2452
2453
  
  	/* You can't ever have a contiguous set of clusters
  	 * bigger than a block group bitmap so we never have to worry
ef6b689b6   Tao Ma   ocfs2: Remove ENT...
2454
2455
  	 * about looping on them.
  	 * This is expensive. We can safely remove once this stuff has
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2456
  	 * gotten tested really well. */
dd7b5f9d0   Changwei Ge   ocfs2: clean dead...
2457
2458
2459
  	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb,
  				ocfs2_blocks_to_clusters(bitmap_inode->i_sb,
  							 start_blk)));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2460

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2461
2462
2463
  
  	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  				     &bg_start_bit);
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2464
2465
2466
  	trace_ocfs2_free_clusters((unsigned long long)bg_blkno,
  			(unsigned long long)start_blk,
  			bg_start_bit, num_clusters);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2467

b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2468
2469
2470
  	status = _ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  					   bg_start_bit, bg_blkno,
  					   num_clusters, undo_fn);
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2471
  	if (status < 0) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2472
  		mlog_errno(status);
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2473
2474
2475
2476
2477
  		goto out;
  	}
  
  	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
  					 num_clusters);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2478

9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2479
  out:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2480
2481
  	return status;
  }
b4414eea0   Mark Fasheh   ocfs2: Clear undo...
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
  int ocfs2_free_clusters(handle_t *handle,
  			struct inode *bitmap_inode,
  			struct buffer_head *bitmap_bh,
  			u64 start_blk,
  			unsigned int num_clusters)
  {
  	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  				    start_blk, num_clusters,
  				    _ocfs2_set_bit);
  }
  
  /*
   * Give never-used clusters back to the global bitmap.  We don't need
   * to protect these bits in the undo buffer.
   */
  int ocfs2_release_clusters(handle_t *handle,
  			   struct inode *bitmap_inode,
  			   struct buffer_head *bitmap_bh,
  			   u64 start_blk,
  			   unsigned int num_clusters)
  {
  	return _ocfs2_free_clusters(handle, bitmap_inode, bitmap_bh,
  				    start_blk, num_clusters,
  				    _ocfs2_clear_bit);
  }
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
  /*
   * For a given allocation, determine which allocators will need to be
   * accessed, and lock them, reserving the appropriate number of bits.
   *
   * Sparse file systems call this from ocfs2_write_begin_nolock()
   * and ocfs2_allocate_unwritten_extents().
   *
   * File systems which don't support holes call this from
   * ocfs2_extend_allocation().
   */
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2517
2518
  int ocfs2_lock_allocators(struct inode *inode,
  			  struct ocfs2_extent_tree *et,
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2519
2520
  			  u32 clusters_to_add, u32 extents_to_split,
  			  struct ocfs2_alloc_context **data_ac,
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2521
  			  struct ocfs2_alloc_context **meta_ac)
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
  {
  	int ret = 0, num_free_extents;
  	unsigned int max_recs_needed = clusters_to_add + 2 * extents_to_split;
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  
  	*meta_ac = NULL;
  	if (data_ac)
  		*data_ac = NULL;
  
  	BUG_ON(clusters_to_add != 0 && data_ac == NULL);
964f14a0d   Jun Piao   ocfs2: clean up s...
2532
  	num_free_extents = ocfs2_num_free_extents(et);
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
  	if (num_free_extents < 0) {
  		ret = num_free_extents;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	/*
  	 * Sparse allocation file systems need to be more conservative
  	 * with reserving room for expansion - the actual allocation
  	 * happens while we've got a journal handle open so re-taking
  	 * a cluster lock (because we ran out of room for another
  	 * extent) will violate ordering rules.
  	 *
  	 * Most of the time we'll only be seeing this 1 cluster at a time
  	 * anyway.
  	 *
  	 * Always lock for any unwritten extents - we might want to
  	 * add blocks during a split.
  	 */
  	if (!num_free_extents ||
  	    (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) {
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2554
  		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
  		if (ret < 0) {
  			if (ret != -ENOSPC)
  				mlog_errno(ret);
  			goto out;
  		}
  	}
  
  	if (clusters_to_add == 0)
  		goto out;
  
  	ret = ocfs2_reserve_clusters(osb, clusters_to_add, data_ac);
  	if (ret < 0) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
  		goto out;
  	}
  
  out:
  	if (ret) {
  		if (*meta_ac) {
  			ocfs2_free_alloc_context(*meta_ac);
  			*meta_ac = NULL;
  		}
  
  		/*
  		 * We cannot have an error and a non null *data_ac.
  		 */
  	}
  
  	return ret;
  }
6ca497a83   wengang wang   ocfs2: fix rare s...
2586
2587
2588
2589
2590
2591
  
  /*
   * Read the inode specified by blkno to get suballoc_slot and
   * suballoc_bit.
   */
  static int ocfs2_get_suballoc_slot_bit(struct ocfs2_super *osb, u64 blkno,
889f004a8   Tao Ma   ocfs2: Use the ri...
2592
2593
  				       u16 *suballoc_slot, u64 *group_blkno,
  				       u16 *suballoc_bit)
6ca497a83   wengang wang   ocfs2: fix rare s...
2594
2595
2596
2597
  {
  	int status;
  	struct buffer_head *inode_bh = NULL;
  	struct ocfs2_dinode *inode_fe;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2598
  	trace_ocfs2_get_suballoc_slot_bit((unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2599
2600
2601
2602
  
  	/* dirty read disk */
  	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
  	if (status < 0) {
5b09b507d   Joel Becker   ocfs2: Fix some p...
2603
2604
2605
  		mlog(ML_ERROR, "read block %llu failed %d
  ",
  		     (unsigned long long)blkno, status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2606
2607
2608
2609
2610
  		goto bail;
  	}
  
  	inode_fe = (struct ocfs2_dinode *) inode_bh->b_data;
  	if (!OCFS2_IS_VALID_DINODE(inode_fe)) {
5b09b507d   Joel Becker   ocfs2: Fix some p...
2611
2612
2613
  		mlog(ML_ERROR, "invalid inode %llu requested
  ",
  		     (unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2614
2615
2616
  		status = -EINVAL;
  		goto bail;
  	}
0fba81374   Tao Ma   ocfs2: Fix 2 warn...
2617
  	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
6ca497a83   wengang wang   ocfs2: fix rare s...
2618
2619
2620
  	    (u32)le16_to_cpu(inode_fe->i_suballoc_slot) > osb->max_slots - 1) {
  		mlog(ML_ERROR, "inode %llu has invalid suballoc slot %u
  ",
5b09b507d   Joel Becker   ocfs2: Fix some p...
2621
2622
  		     (unsigned long long)blkno,
  		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
6ca497a83   wengang wang   ocfs2: fix rare s...
2623
2624
2625
2626
2627
2628
2629
2630
  		status = -EINVAL;
  		goto bail;
  	}
  
  	if (suballoc_slot)
  		*suballoc_slot = le16_to_cpu(inode_fe->i_suballoc_slot);
  	if (suballoc_bit)
  		*suballoc_bit = le16_to_cpu(inode_fe->i_suballoc_bit);
889f004a8   Tao Ma   ocfs2: Use the ri...
2631
2632
  	if (group_blkno)
  		*group_blkno = le64_to_cpu(inode_fe->i_suballoc_loc);
6ca497a83   wengang wang   ocfs2: fix rare s...
2633
2634
2635
  
  bail:
  	brelse(inode_bh);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
2636
2637
  	if (status)
  		mlog_errno(status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
  	return status;
  }
  
  /*
   * test whether bit is SET in allocator bitmap or not.  on success, 0
   * is returned and *res is 1 for SET; 0 otherwise.  when fails, errno
   * is returned and *res is meaningless.  Call this after you have
   * cluster locked against suballoc, or you may get a result based on
   * non-up2date contents
   */
  static int ocfs2_test_suballoc_bit(struct ocfs2_super *osb,
  				   struct inode *suballoc,
889f004a8   Tao Ma   ocfs2: Use the ri...
2650
2651
  				   struct buffer_head *alloc_bh,
  				   u64 group_blkno, u64 blkno,
6ca497a83   wengang wang   ocfs2: fix rare s...
2652
2653
  				   u16 bit, int *res)
  {
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
2654
  	struct ocfs2_dinode *alloc_di;
6ca497a83   wengang wang   ocfs2: fix rare s...
2655
2656
2657
2658
  	struct ocfs2_group_desc *group;
  	struct buffer_head *group_bh = NULL;
  	u64 bg_blkno;
  	int status;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2659
2660
  	trace_ocfs2_test_suballoc_bit((unsigned long long)blkno,
  				      (unsigned int)bit);
6ca497a83   wengang wang   ocfs2: fix rare s...
2661

abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
2662
2663
  	alloc_di = (struct ocfs2_dinode *)alloc_bh->b_data;
  	if ((bit + 1) > ocfs2_bits_per_group(&alloc_di->id2.i_chain)) {
6ca497a83   wengang wang   ocfs2: fix rare s...
2664
2665
2666
  		mlog(ML_ERROR, "suballoc bit %u out of range of %u
  ",
  		     (unsigned int)bit,
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
2667
  		     ocfs2_bits_per_group(&alloc_di->id2.i_chain));
6ca497a83   wengang wang   ocfs2: fix rare s...
2668
2669
2670
  		status = -EINVAL;
  		goto bail;
  	}
889f004a8   Tao Ma   ocfs2: Use the ri...
2671
2672
  	bg_blkno = group_blkno ? group_blkno :
  		   ocfs2_which_suballoc_group(blkno, bit);
abf1b3cb5   Tao Ma   ocfs2: Set ac_las...
2673
  	status = ocfs2_read_group_descriptor(suballoc, alloc_di, bg_blkno,
6ca497a83   wengang wang   ocfs2: fix rare s...
2674
2675
  					     &group_bh);
  	if (status < 0) {
5b09b507d   Joel Becker   ocfs2: Fix some p...
2676
2677
2678
  		mlog(ML_ERROR, "read group %llu failed %d
  ",
  		     (unsigned long long)bg_blkno, status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2679
2680
2681
2682
2683
2684
2685
2686
  		goto bail;
  	}
  
  	group = (struct ocfs2_group_desc *) group_bh->b_data;
  	*res = ocfs2_test_bit(bit, (unsigned long *)group->bg_bitmap);
  
  bail:
  	brelse(group_bh);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
2687
2688
  	if (status)
  		mlog_errno(status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
  	return status;
  }
  
  /*
   * Test if the bit representing this inode (blkno) is set in the
   * suballocator.
   *
   * On success, 0 is returned and *res is 1 for SET; 0 otherwise.
   *
   * In the event of failure, a negative value is returned and *res is
   * meaningless.
   *
   * Callers must make sure to hold nfs_sync_lock to prevent
   * ocfs2_delete_inode() on another node from accessing the same
   * suballocator concurrently.
   */
  int ocfs2_test_inode_bit(struct ocfs2_super *osb, u64 blkno, int *res)
  {
  	int status;
889f004a8   Tao Ma   ocfs2: Use the ri...
2708
  	u64 group_blkno = 0;
6ca497a83   wengang wang   ocfs2: fix rare s...
2709
2710
2711
  	u16 suballoc_bit = 0, suballoc_slot = 0;
  	struct inode *inode_alloc_inode;
  	struct buffer_head *alloc_bh = NULL;
2f73e135b   Tao Ma   ocfs2: Remove mlo...
2712
  	trace_ocfs2_test_inode_bit((unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2713
2714
  
  	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
889f004a8   Tao Ma   ocfs2: Use the ri...
2715
  					     &group_blkno, &suballoc_bit);
6ca497a83   wengang wang   ocfs2: fix rare s...
2716
2717
2718
2719
2720
  	if (status < 0) {
  		mlog(ML_ERROR, "get alloc slot and bit failed %d
  ", status);
  		goto bail;
  	}
e5a15e17a   Junxiao Bi   ocfs2: fix panic ...
2721
2722
2723
2724
2725
2726
  	if (suballoc_slot == (u16)OCFS2_INVALID_SLOT)
  		inode_alloc_inode = ocfs2_get_system_file_inode(osb,
  			GLOBAL_INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
  	else
  		inode_alloc_inode = ocfs2_get_system_file_inode(osb,
  			INODE_ALLOC_SYSTEM_INODE, suballoc_slot);
6ca497a83   wengang wang   ocfs2: fix rare s...
2727
2728
2729
2730
2731
2732
2733
2734
2735
  	if (!inode_alloc_inode) {
  		/* the error code could be inaccurate, but we are not able to
  		 * get the correct one. */
  		status = -EINVAL;
  		mlog(ML_ERROR, "unable to get alloc inode in slot %u
  ",
  		     (u32)suballoc_slot);
  		goto bail;
  	}
5955102c9   Al Viro   wrappers for ->i_...
2736
  	inode_lock(inode_alloc_inode);
6ca497a83   wengang wang   ocfs2: fix rare s...
2737
2738
  	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
  	if (status < 0) {
5955102c9   Al Viro   wrappers for ->i_...
2739
  		inode_unlock(inode_alloc_inode);
7dc3e8390   jiangyiwen   ocfs2: iput inode...
2740
  		iput(inode_alloc_inode);
6ca497a83   wengang wang   ocfs2: fix rare s...
2741
2742
2743
2744
2745
2746
2747
  		mlog(ML_ERROR, "lock on alloc inode on slot %u failed %d
  ",
  		     (u32)suballoc_slot, status);
  		goto bail;
  	}
  
  	status = ocfs2_test_suballoc_bit(osb, inode_alloc_inode, alloc_bh,
889f004a8   Tao Ma   ocfs2: Use the ri...
2748
  					 group_blkno, blkno, suballoc_bit, res);
6ca497a83   wengang wang   ocfs2: fix rare s...
2749
2750
2751
2752
2753
  	if (status < 0)
  		mlog(ML_ERROR, "test suballoc bit failed %d
  ", status);
  
  	ocfs2_inode_unlock(inode_alloc_inode, 0);
5955102c9   Al Viro   wrappers for ->i_...
2754
  	inode_unlock(inode_alloc_inode);
6ca497a83   wengang wang   ocfs2: fix rare s...
2755
2756
2757
2758
  
  	iput(inode_alloc_inode);
  	brelse(alloc_bh);
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
2759
2760
  	if (status)
  		mlog_errno(status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2761
2762
  	return status;
  }