Blame view

fs/ocfs2/suballoc.c 62.4 KB
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  /* -*- 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.
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public
   * License as published by the Free Software Foundation; either
   * version 2 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public
   * License along with this program; if not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   * Boston, MA 021110-1307, USA.
   */
  
  #include <linux/fs.h>
  #include <linux/types.h>
  #include <linux/slab.h>
  #include <linux/highmem.h>
  
  #define MLOG_MASK_PREFIX ML_DISK_ALLOC
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
  
  #include "alloc.h"
d6b32bbb3   Joel Becker   ocfs2: block read...
38
  #include "blockcheck.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
39
40
41
42
43
44
45
46
47
48
  #include "dlmglue.h"
  #include "inode.h"
  #include "journal.h"
  #include "localalloc.h"
  #include "suballoc.h"
  #include "super.h"
  #include "sysfile.h"
  #include "uptodate.h"
  
  #include "buffer_head_io.h"
ffda89a3b   Tao Ma   ocfs2: Add a new ...
49
  #define NOT_ALLOC_NEW_GROUP		0
60ca81e82   Tao Ma   ocfs2: Allocate i...
50
51
  #define ALLOC_NEW_GROUP			0x1
  #define ALLOC_GROUPS_FROM_GLOBAL	0x2
ffda89a3b   Tao Ma   ocfs2: Add a new ...
52

4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
53
  #define OCFS2_MAX_INODES_TO_STEAL	1024
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
54
55
56
  static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg);
  static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe);
  static inline u16 ocfs2_find_victim_chain(struct ocfs2_chain_list *cl);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
57
  static int ocfs2_block_group_fill(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
58
59
60
61
62
63
64
  				  struct inode *alloc_inode,
  				  struct buffer_head *bg_bh,
  				  u64 group_blkno,
  				  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...
65
  				   struct buffer_head *bh,
60ca81e82   Tao Ma   ocfs2: Allocate i...
66
  				   u64 max_block,
feb473a6e   Tao Ma   ocfs2: Optimize i...
67
  				   u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
68
  				   int flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
69

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
70
71
72
  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...
73
  				      u64 max_block,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
74
75
76
77
  				      u16 *bit_off, u16 *bits_found);
  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...
78
  				    u64 max_block,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
79
  				    u16 *bit_off, u16 *bits_found);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
80
81
  static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  				     struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
82
  				     handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
83
84
85
86
87
88
89
  				     u32 bits_wanted,
  				     u32 min_bits,
  				     u16 *bit_off,
  				     unsigned int *num_bits,
  				     u64 *bg_blkno);
  static int ocfs2_test_bg_bit_allocatable(struct buffer_head *bg_bh,
  					 int nr);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
90
  static inline int ocfs2_block_group_set_bits(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
91
92
93
94
95
  					     struct inode *alloc_inode,
  					     struct ocfs2_group_desc *bg,
  					     struct buffer_head *group_bh,
  					     unsigned int bit_off,
  					     unsigned int num_bits);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
96
  static inline int ocfs2_block_group_clear_bits(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
97
98
99
100
101
  					       struct inode *alloc_inode,
  					       struct ocfs2_group_desc *bg,
  					       struct buffer_head *group_bh,
  					       unsigned int bit_off,
  					       unsigned int num_bits);
1fabe1481   Mark Fasheh   ocfs2: Remove str...
102
  static int ocfs2_relink_block_group(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
103
104
105
106
107
108
109
  				    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...
110
111
112
  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...
113
114
115
116
  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...
117
118
  static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  					     u32 bits_wanted, u64 max_block,
60ca81e82   Tao Ma   ocfs2: Allocate i...
119
  					     int flags,
1187c9688   Joel Becker   ocfs2: Limit inod...
120
  					     struct ocfs2_alloc_context **ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
121

9c7af40b2   Mark Fasheh   ocfs2: throttle b...
122
  void ocfs2_free_ac_resource(struct ocfs2_alloc_context *ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
123
  {
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
124
125
126
127
  	struct inode *inode = ac->ac_inode;
  
  	if (inode) {
  		if (ac->ac_which != OCFS2_AC_USE_LOCAL)
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
128
  			ocfs2_inode_unlock(inode, 1);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
129
130
131
132
  
  		mutex_unlock(&inode->i_mutex);
  
  		iput(inode);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
133
  		ac->ac_inode = NULL;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
134
  	}
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
135
136
  	brelse(ac->ac_bh);
  	ac->ac_bh = NULL;
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
137
138
139
140
141
  }
  
  void ocfs2_free_alloc_context(struct ocfs2_alloc_context *ac)
  {
  	ocfs2_free_ac_resource(ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
142
143
144
145
146
147
148
  	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...
149
150
151
152
153
154
155
156
  #define do_error(fmt, ...)						\
  	do{								\
  		if (clean_error)					\
  			mlog(ML_ERROR, fmt "
  ", ##__VA_ARGS__);	\
  		else							\
  			ocfs2_error(sb, fmt, ##__VA_ARGS__);		\
  	} while (0)
970e4936d   Joel Becker   ocfs2: Validate m...
157
158
159
160
161
  static int ocfs2_validate_gd_self(struct super_block *sb,
  				  struct buffer_head *bh,
  				  int clean_error)
  {
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
7bf72edee   Mark Fasheh   ocfs2: better gro...
162
  	if (!OCFS2_IS_VALID_GROUP_DESC(gd)) {
68f64d471   Joel Becker   ocfs2: Wrap group...
163
164
  		do_error("Group descriptor #%llu has bad signature %.*s",
  			 (unsigned long long)bh->b_blocknr, 7,
57e3e7971   Joel Becker   ocfs2: Consolidat...
165
166
  			 gd->bg_signature);
  		return -EINVAL;
7bf72edee   Mark Fasheh   ocfs2: better gro...
167
  	}
68f64d471   Joel Becker   ocfs2: Wrap group...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  	if (le64_to_cpu(gd->bg_blkno) != bh->b_blocknr) {
  		do_error("Group descriptor #%llu has an invalid bg_blkno "
  			 "of %llu",
  			 (unsigned long long)bh->b_blocknr,
  			 (unsigned long long)le64_to_cpu(gd->bg_blkno));
  		return -EINVAL;
  	}
  
  	if (le32_to_cpu(gd->bg_generation) != OCFS2_SB(sb)->fs_generation) {
  		do_error("Group descriptor #%llu has an invalid "
  			 "fs_generation of #%u",
  			 (unsigned long long)bh->b_blocknr,
  			 le32_to_cpu(gd->bg_generation));
  		return -EINVAL;
  	}
970e4936d   Joel Becker   ocfs2: Validate m...
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  	if (le16_to_cpu(gd->bg_free_bits_count) > le16_to_cpu(gd->bg_bits)) {
  		do_error("Group descriptor #%llu has bit count %u but "
  			 "claims that %u are free",
  			 (unsigned long long)bh->b_blocknr,
  			 le16_to_cpu(gd->bg_bits),
  			 le16_to_cpu(gd->bg_free_bits_count));
  		return -EINVAL;
  	}
  
  	if (le16_to_cpu(gd->bg_bits) > (8 * le16_to_cpu(gd->bg_size))) {
  		do_error("Group descriptor #%llu has bit count %u but "
  			 "max bitmap bits of %u",
  			 (unsigned long long)bh->b_blocknr,
  			 le16_to_cpu(gd->bg_bits),
  			 8 * le16_to_cpu(gd->bg_size));
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int ocfs2_validate_gd_parent(struct super_block *sb,
  				    struct ocfs2_dinode *di,
  				    struct buffer_head *bh,
  				    int clean_error)
  {
  	unsigned int max_bits;
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
7bf72edee   Mark Fasheh   ocfs2: better gro...
211
  	if (di->i_blkno != gd->bg_parent_dinode) {
68f64d471   Joel Becker   ocfs2: Wrap group...
212
  		do_error("Group descriptor #%llu has bad parent "
57e3e7971   Joel Becker   ocfs2: Consolidat...
213
  			 "pointer (%llu, expected %llu)",
68f64d471   Joel Becker   ocfs2: Wrap group...
214
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
215
216
217
  			 (unsigned long long)le64_to_cpu(gd->bg_parent_dinode),
  			 (unsigned long long)le64_to_cpu(di->i_blkno));
  		return -EINVAL;
7bf72edee   Mark Fasheh   ocfs2: better gro...
218
219
220
221
  	}
  
  	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) {
68f64d471   Joel Becker   ocfs2: Wrap group...
222
223
  		do_error("Group descriptor #%llu has bit count of %u",
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
224
225
  			 le16_to_cpu(gd->bg_bits));
  		return -EINVAL;
7bf72edee   Mark Fasheh   ocfs2: better gro...
226
227
228
229
  	}
  
  	if (le16_to_cpu(gd->bg_chain) >=
  	    le16_to_cpu(di->id2.i_chain.cl_next_free_rec)) {
68f64d471   Joel Becker   ocfs2: Wrap group...
230
231
  		do_error("Group descriptor #%llu has bad chain %u",
  			 (unsigned long long)bh->b_blocknr,
57e3e7971   Joel Becker   ocfs2: Consolidat...
232
233
  			 le16_to_cpu(gd->bg_chain));
  		return -EINVAL;
7bf72edee   Mark Fasheh   ocfs2: better gro...
234
  	}
970e4936d   Joel Becker   ocfs2: Validate m...
235
236
  	return 0;
  }
7bf72edee   Mark Fasheh   ocfs2: better gro...
237

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

970e4936d   Joel Becker   ocfs2: Validate m...
240
241
242
243
244
245
246
247
248
  /*
   * 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...
249
250
251
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
  
  	BUG_ON(!buffer_uptodate(bh));
970e4936d   Joel Becker   ocfs2: Validate m...
252

d6b32bbb3   Joel Becker   ocfs2: block read...
253
254
255
256
257
258
  	/*
  	 * 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...
259
260
261
262
263
264
  	if (rc) {
  		mlog(ML_ERROR,
  		     "Checksum failed for group descriptor %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
  	} else
d6b32bbb3   Joel Becker   ocfs2: block read...
265
  		rc = ocfs2_validate_gd_self(sb, bh, 1);
970e4936d   Joel Becker   ocfs2: Validate m...
266
267
268
269
270
271
272
273
274
  	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...
275
276
  	int rc;
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data;
970e4936d   Joel Becker   ocfs2: Validate m...
277
278
279
  	mlog(0, "Validating group descriptor %llu
  ",
  	     (unsigned long long)bh->b_blocknr);
d6b32bbb3   Joel Becker   ocfs2: block read...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
  	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...
294
  	return ocfs2_validate_gd_self(sb, bh, 0);
7bf72edee   Mark Fasheh   ocfs2: better gro...
295
  }
68f64d471   Joel Becker   ocfs2: Wrap group...
296
297
298
299
300
  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;
970e4936d   Joel Becker   ocfs2: Validate m...
301
302
  	rc = ocfs2_read_block(inode, gd_blkno, &tmp,
  			      ocfs2_validate_group_descriptor);
68f64d471   Joel Becker   ocfs2: Wrap group...
303
304
  	if (rc)
  		goto out;
970e4936d   Joel Becker   ocfs2: Validate m...
305
  	rc = ocfs2_validate_gd_parent(inode->i_sb, di, tmp, 0);
68f64d471   Joel Becker   ocfs2: Wrap group...
306
307
308
309
310
311
312
313
314
315
316
317
  	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;
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
318
  static int ocfs2_block_group_fill(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
319
320
321
322
323
324
325
326
327
328
329
330
331
  				  struct inode *alloc_inode,
  				  struct buffer_head *bg_bh,
  				  u64 group_blkno,
  				  u16 my_chain,
  				  struct ocfs2_chain_list *cl)
  {
  	int status = 0;
  	struct ocfs2_group_desc *bg = (struct ocfs2_group_desc *) bg_bh->b_data;
  	struct super_block * sb = alloc_inode->i_sb;
  
  	mlog_entry_void();
  
  	if (((unsigned long long) bg_bh->b_blocknr) != group_blkno) {
b0697053f   Mark Fasheh   ocfs2: don't use ...
332
333
334
  		ocfs2_error(alloc_inode->i_sb, "group block (%llu) != "
  			    "b_blocknr (%llu)",
  			    (unsigned long long)group_blkno,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
335
336
337
338
  			    (unsigned long long) bg_bh->b_blocknr);
  		status = -EIO;
  		goto bail;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
339
340
341
342
  	status = ocfs2_journal_access_gd(handle,
  					 alloc_inode,
  					 bg_bh,
  					 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	memset(bg, 0, sb->s_blocksize);
  	strcpy(bg->bg_signature, OCFS2_GROUP_DESC_SIGNATURE);
  	bg->bg_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
  	bg->bg_size = cpu_to_le16(ocfs2_group_bitmap_size(sb));
  	bg->bg_bits = cpu_to_le16(ocfs2_bits_per_group(cl));
  	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);
  	/* 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);
  
  	status = ocfs2_journal_dirty(handle, bg_bh);
  	if (status < 0)
  		mlog_errno(status);
  
  	/* 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:
  	mlog_exit(status);
  	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;
  }
  
  /*
   * 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...
394
  				   struct buffer_head *bh,
60ca81e82   Tao Ma   ocfs2: Allocate i...
395
  				   u64 max_block,
feb473a6e   Tao Ma   ocfs2: Optimize i...
396
  				   u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
397
  				   int flags)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
398
399
400
401
402
  {
  	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...
403
  	handle_t *handle = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
404
405
406
407
408
409
410
411
412
  	u32 bit_off, num_bits;
  	u16 alloc_rec;
  	u64 bg_blkno;
  	struct buffer_head *bg_bh = NULL;
  	struct ocfs2_group_desc *bg;
  
  	BUG_ON(ocfs2_is_cluster_bitmap(alloc_inode));
  
  	mlog_entry_void();
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
413
  	cl = &fe->id2.i_chain;
1187c9688   Joel Becker   ocfs2: Limit inod...
414
415
  	status = ocfs2_reserve_clusters_with_limit(osb,
  						   le16_to_cpu(cl->cl_cpg),
60ca81e82   Tao Ma   ocfs2: Allocate i...
416
  						   max_block, flags, &ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
417
418
419
420
421
422
423
424
  	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...
425
  	handle = ocfs2_start_trans(osb, credits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
426
427
428
429
430
431
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		handle = NULL;
  		mlog_errno(status);
  		goto bail;
  	}
feb473a6e   Tao Ma   ocfs2: Optimize i...
432
433
434
435
436
437
  	if (last_alloc_group && *last_alloc_group != 0) {
  		mlog(0, "use old allocation group %llu for block group alloc
  ",
  		     (unsigned long long)*last_alloc_group);
  		ac->ac_last_group = *last_alloc_group;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  	status = ocfs2_claim_clusters(osb,
  				      handle,
  				      ac,
  				      le16_to_cpu(cl->cl_cpg),
  				      &bit_off,
  				      &num_bits);
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	alloc_rec = ocfs2_find_smallest_chain(cl);
  
  	/* setup the group */
  	bg_blkno = ocfs2_clusters_to_blocks(osb->sb, bit_off);
b0697053f   Mark Fasheh   ocfs2: don't use ...
454
455
456
  	mlog(0, "new descriptor, record %u, at block %llu
  ",
  	     alloc_rec, (unsigned long long)bg_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  
  	bg_bh = sb_getblk(osb->sb, bg_blkno);
  	if (!bg_bh) {
  		status = -EIO;
  		mlog_errno(status);
  		goto bail;
  	}
  	ocfs2_set_new_buffer_uptodate(alloc_inode, bg_bh);
  
  	status = ocfs2_block_group_fill(handle,
  					alloc_inode,
  					bg_bh,
  					bg_blkno,
  					alloc_rec,
  					cl);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	bg = (struct ocfs2_group_desc *) bg_bh->b_data;
13723d00e   Joel Becker   ocfs2: Use metada...
478
479
  	status = ocfs2_journal_access_di(handle, alloc_inode,
  					 bh, OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	le32_add_cpu(&cl->cl_recs[alloc_rec].c_free,
  		     le16_to_cpu(bg->bg_free_bits_count));
  	le32_add_cpu(&cl->cl_recs[alloc_rec].c_total, le16_to_cpu(bg->bg_bits));
  	cl->cl_recs[alloc_rec].c_blkno  = cpu_to_le64(bg_blkno);
  	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));
  
  	status = ocfs2_journal_dirty(handle, bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	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...
509
  	alloc_inode->i_blocks = ocfs2_inode_sector_count(alloc_inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
510
511
  
  	status = 0;
feb473a6e   Tao Ma   ocfs2: Optimize i...
512
513
514
515
  
  	/* 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...
516
517
  bail:
  	if (handle)
02dc1af44   Mark Fasheh   ocfs2: pass ocfs2...
518
  		ocfs2_commit_trans(osb, handle);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
519
520
521
  
  	if (ac)
  		ocfs2_free_alloc_context(ac);
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
522
  	brelse(bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
523
524
525
526
527
528
  
  	mlog_exit(status);
  	return status;
  }
  
  static int ocfs2_reserve_suballoc_bits(struct ocfs2_super *osb,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
529
530
  				       struct ocfs2_alloc_context *ac,
  				       int type,
ffda89a3b   Tao Ma   ocfs2: Add a new ...
531
  				       u32 slot,
feb473a6e   Tao Ma   ocfs2: Optimize i...
532
  				       u64 *last_alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
533
  				       int flags)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
534
535
536
  {
  	int status;
  	u32 bits_wanted = ac->ac_bits_wanted;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
537
  	struct inode *alloc_inode;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
538
  	struct buffer_head *bh = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
539
540
541
542
  	struct ocfs2_dinode *fe;
  	u32 free_bits;
  
  	mlog_entry_void();
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
543
544
545
546
547
  	alloc_inode = ocfs2_get_system_file_inode(osb, type, slot);
  	if (!alloc_inode) {
  		mlog_errno(-EINVAL);
  		return -EINVAL;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
548

da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
549
  	mutex_lock(&alloc_inode->i_mutex);
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
550
  	status = ocfs2_inode_lock(alloc_inode, &bh, 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
551
  	if (status < 0) {
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
552
553
  		mutex_unlock(&alloc_inode->i_mutex);
  		iput(alloc_inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
554
  		mlog_errno(status);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
555
  		return status;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
556
  	}
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
557
  	ac->ac_inode = alloc_inode;
a4a489116   Tao Ma   ocfs2: Add ac_all...
558
  	ac->ac_alloc_slot = slot;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
559

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
560
  	fe = (struct ocfs2_dinode *) bh->b_data;
10995aa24   Joel Becker   ocfs2: Morph the ...
561
562
563
564
  
  	/* 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...
565
  	if (!(fe->i_flags & cpu_to_le32(OCFS2_CHAIN_FL))) {
b0697053f   Mark Fasheh   ocfs2: don't use ...
566
567
  		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...
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  		status = -EIO;
  		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)) {
  			mlog(0, "Disk Full: wanted=%u, free_bits=%u
  ",
  			     bits_wanted, free_bits);
  			status = -ENOSPC;
  			goto bail;
  		}
60ca81e82   Tao Ma   ocfs2: Allocate i...
584
  		if (!(flags & ALLOC_NEW_GROUP)) {
ffda89a3b   Tao Ma   ocfs2: Add a new ...
585
586
587
588
589
590
591
  			mlog(0, "Alloc File %u Full: wanted=%u, free_bits=%u, "
  			     "and we don't alloc a new group for it.
  ",
  			     slot, bits_wanted, free_bits);
  			status = -ENOSPC;
  			goto bail;
  		}
1187c9688   Joel Becker   ocfs2: Limit inod...
592
  		status = ocfs2_block_group_alloc(osb, alloc_inode, bh,
feb473a6e   Tao Ma   ocfs2: Optimize i...
593
594
  						 ac->ac_max_block,
  						 last_alloc_group, flags);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  		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...
611
  	brelse(bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
612
613
614
615
  
  	mlog_exit(status);
  	return status;
  }
cf1d6c763   Tiger Yang   ocfs2: Add extend...
616
617
618
  int ocfs2_reserve_new_metadata_blocks(struct ocfs2_super *osb,
  				      int blocks,
  				      struct ocfs2_alloc_context **ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
619
620
  {
  	int status;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
621
  	u32 slot;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
622

cd8612808   Robert P. J. Day   [PATCH] Fix numer...
623
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
624
625
626
627
628
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
cf1d6c763   Tiger Yang   ocfs2: Add extend...
629
  	(*ac)->ac_bits_wanted = blocks;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
630
  	(*ac)->ac_which = OCFS2_AC_USE_META;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
631
  	slot = osb->slot_num;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
632
  	(*ac)->ac_group_search = ocfs2_block_group_search;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
633
  	status = ocfs2_reserve_suballoc_bits(osb, (*ac),
ffda89a3b   Tao Ma   ocfs2: Add a new ...
634
  					     EXTENT_ALLOC_SYSTEM_INODE,
feb473a6e   Tao Ma   ocfs2: Optimize i...
635
  					     slot, NULL, ALLOC_NEW_GROUP);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
636
637
638
639
640
641
642
643
644
645
646
647
  	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;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
648
649
650
  	mlog_exit(status);
  	return status;
  }
cf1d6c763   Tiger Yang   ocfs2: Add extend...
651
652
653
654
655
656
657
658
  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);
  }
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
  static int ocfs2_steal_inode_from_other_nodes(struct ocfs2_super *osb,
  					      struct ocfs2_alloc_context *ac)
  {
  	int i, status = -ENOSPC;
  	s16 slot = ocfs2_get_inode_steal_slot(osb);
  
  	/* Start to steal inodes 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,
  						     INODE_ALLOC_SYSTEM_INODE,
feb473a6e   Tao Ma   ocfs2: Optimize i...
678
679
  						     slot, NULL,
  						     NOT_ALLOC_NEW_GROUP);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
680
681
682
683
684
685
686
687
688
689
  		if (status >= 0) {
  			ocfs2_set_inode_steal_slot(osb, slot);
  			break;
  		}
  
  		ocfs2_free_ac_resource(ac);
  	}
  
  	return status;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
690
  int ocfs2_reserve_new_inode(struct ocfs2_super *osb,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
691
692
693
  			    struct ocfs2_alloc_context **ac)
  {
  	int status;
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
694
  	s16 slot = ocfs2_get_inode_steal_slot(osb);
feb473a6e   Tao Ma   ocfs2: Optimize i...
695
  	u64 alloc_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
696

cd8612808   Robert P. J. Day   [PATCH] Fix numer...
697
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
698
699
700
701
702
703
704
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
  
  	(*ac)->ac_bits_wanted = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
705
  	(*ac)->ac_which = OCFS2_AC_USE_INODE;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
706
  	(*ac)->ac_group_search = ocfs2_block_group_search;
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
707
  	/*
1187c9688   Joel Becker   ocfs2: Limit inod...
708
709
  	 * 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...
710
  	 * limit.  The 'inode64' mount option avoids this behavior.
1187c9688   Joel Becker   ocfs2: Limit inod...
711
  	 */
12462f1d9   Joel Becker   ocfs2: Add the 'i...
712
713
  	if (!(osb->s_mount_opt & OCFS2_MOUNT_INODE64))
  		(*ac)->ac_max_block = (u32)~0U;
1187c9688   Joel Becker   ocfs2: Limit inod...
714
715
  
  	/*
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
716
717
718
719
720
721
722
723
724
725
726
727
728
  	 * 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 &&
  	    atomic_read(&osb->s_num_inodes_stolen) < OCFS2_MAX_INODES_TO_STEAL)
  		goto inode_steal;
  
  	atomic_set(&osb->s_num_inodes_stolen, 0);
feb473a6e   Tao Ma   ocfs2: Optimize i...
729
  	alloc_group = osb->osb_inode_alloc_group;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
730
731
  	status = ocfs2_reserve_suballoc_bits(osb, *ac,
  					     INODE_ALLOC_SYSTEM_INODE,
60ca81e82   Tao Ma   ocfs2: Allocate i...
732
  					     osb->slot_num,
feb473a6e   Tao Ma   ocfs2: Optimize i...
733
  					     &alloc_group,
60ca81e82   Tao Ma   ocfs2: Allocate i...
734
735
  					     ALLOC_NEW_GROUP |
  					     ALLOC_GROUPS_FROM_GLOBAL);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
736
737
  	if (status >= 0) {
  		status = 0;
feb473a6e   Tao Ma   ocfs2: Optimize i...
738
739
740
741
742
743
  		spin_lock(&osb->osb_lock);
  		osb->osb_inode_alloc_group = alloc_group;
  		spin_unlock(&osb->osb_lock);
  		mlog(0, "after reservation, new allocation group is "
  		     "%llu
  ", (unsigned long long)alloc_group);
4d0ddb2ce   Tao Ma   ocfs2: Add inode ...
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
  		/*
  		 * 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:
  	status = ocfs2_steal_inode_from_other_nodes(osb, *ac);
  	atomic_inc(&osb->s_num_inodes_stolen);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
761
762
763
764
765
766
767
768
769
770
771
772
  	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;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
773
774
775
776
777
778
779
780
781
782
  	mlog_exit(status);
  	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...
783
784
  	ac->ac_which = OCFS2_AC_USE_MAIN;
  	ac->ac_group_search = ocfs2_cluster_group_search;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
785
786
  	status = ocfs2_reserve_suballoc_bits(osb, ac,
  					     GLOBAL_BITMAP_SYSTEM_INODE,
feb473a6e   Tao Ma   ocfs2: Optimize i...
787
  					     OCFS2_INVALID_SLOT, NULL,
ffda89a3b   Tao Ma   ocfs2: Add a new ...
788
  					     ALLOC_NEW_GROUP);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
789
  	if (status < 0 && status != -ENOSPC) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
790
  		mlog_errno(status);
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
791
792
  		goto bail;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
793
794
795
796
797
798
799
  bail:
  	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...
800
801
  static int ocfs2_reserve_clusters_with_limit(struct ocfs2_super *osb,
  					     u32 bits_wanted, u64 max_block,
60ca81e82   Tao Ma   ocfs2: Allocate i...
802
  					     int flags,
1187c9688   Joel Becker   ocfs2: Limit inod...
803
  					     struct ocfs2_alloc_context **ac)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
804
805
806
807
  {
  	int status;
  
  	mlog_entry_void();
cd8612808   Robert P. J. Day   [PATCH] Fix numer...
808
  	*ac = kzalloc(sizeof(struct ocfs2_alloc_context), GFP_KERNEL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
809
810
811
812
813
814
815
  	if (!(*ac)) {
  		status = -ENOMEM;
  		mlog_errno(status);
  		goto bail;
  	}
  
  	(*ac)->ac_bits_wanted = bits_wanted;
1187c9688   Joel Becker   ocfs2: Limit inod...
816
  	(*ac)->ac_max_block = max_block;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
817
818
  
  	status = -ENOSPC;
60ca81e82   Tao Ma   ocfs2: Allocate i...
819
820
  	if (!(flags & ALLOC_GROUPS_FROM_GLOBAL) &&
  	    ocfs2_alloc_should_use_local(osb, bits_wanted)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
821
  		status = ocfs2_reserve_local_alloc_bits(osb,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
822
823
  							bits_wanted,
  							*ac);
1187c9688   Joel Becker   ocfs2: Limit inod...
824
825
826
827
828
  		if (status == -EFBIG) {
  			/* The local alloc window is outside ac_max_block.
  			 * use the main bitmap. */
  			status = -ENOSPC;
  		} else if ((status < 0) && (status != -ENOSPC)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
829
830
  			mlog_errno(status);
  			goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
  		}
  	}
  
  	if (status == -ENOSPC) {
  		status = ocfs2_reserve_cluster_bitmap_bits(osb, *ac);
  		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;
  	}
  
  	mlog_exit(status);
  	return status;
  }
1187c9688   Joel Becker   ocfs2: Limit inod...
853
854
855
856
  int ocfs2_reserve_clusters(struct ocfs2_super *osb,
  			   u32 bits_wanted,
  			   struct ocfs2_alloc_context **ac)
  {
60ca81e82   Tao Ma   ocfs2: Allocate i...
857
858
  	return ocfs2_reserve_clusters_with_limit(osb, bits_wanted, 0,
  						 ALLOC_NEW_GROUP, ac);
1187c9688   Joel Becker   ocfs2: Limit inod...
859
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
  /*
   * 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
879
   * allocations, as those bitmaps are separate and undo access is never
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
880
881
882
883
884
885
   * 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;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
886
  	int ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
887
888
889
  
  	if (ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap))
  		return 0;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
890
891
  
  	if (!buffer_jbd(bg_bh))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
892
  		return 1;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
893
  	jbd_lock_bh_state(bg_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
894
  	bg = (struct ocfs2_group_desc *) bh2jh(bg_bh)->b_committed_data;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
895
896
897
898
899
900
901
  	if (bg)
  		ret = !ocfs2_test_bit(nr, (unsigned long *)bg->bg_bitmap);
  	else
  		ret = 1;
  	jbd_unlock_bh_state(bg_bh);
  
  	return ret;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
902
903
904
905
906
  }
  
  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...
907
  					     unsigned int total_bits,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
908
909
910
911
912
913
914
  					     u16 *bit_off,
  					     u16 *bits_found)
  {
  	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 ...
915
916
917
  	/* 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...
918
919
920
  
  	found = start = best_offset = best_size = 0;
  	bitmap = bg->bg_bitmap;
7bf72edee   Mark Fasheh   ocfs2: better gro...
921
922
  	while((offset = ocfs2_find_next_zero_bit(bitmap, total_bits, start)) != -1) {
  		if (offset == total_bits)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
  			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;
  		}
  	}
  
  	/* XXX: I think the first clause is equivalent to the second
  	 * 	- jlbec */
  	if (found == bits_wanted) {
  		*bit_off = start - found;
  		*bits_found = found;
  	} else if (best_size) {
  		*bit_off = best_offset;
  		*bits_found = best_size;
  	} else {
  		status = -ENOSPC;
  		/* No error log here -- see the comment above
  		 * ocfs2_test_bg_bit_allocatable */
  	}
  
  	return status;
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
968
  static inline int ocfs2_block_group_set_bits(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
969
970
971
972
973
974
975
976
977
978
979
  					     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;
  
  	mlog_entry_void();
420353061   Joel Becker   ocfs2: Morph the ...
980
981
982
  	/* 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...
983
984
985
986
987
988
989
990
  	BUG_ON(le16_to_cpu(bg->bg_free_bits_count) < num_bits);
  
  	mlog(0, "block_group_set_bits: off = %u, num = %u
  ", bit_off,
  	     num_bits);
  
  	if (ocfs2_is_cluster_bitmap(alloc_inode))
  		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
13723d00e   Joel Becker   ocfs2: Use metada...
991
992
993
994
  	status = ocfs2_journal_access_gd(handle,
  					 alloc_inode,
  					 group_bh,
  					 journal_type);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	le16_add_cpu(&bg->bg_free_bits_count, -num_bits);
  
  	while(num_bits--)
  		ocfs2_set_bit(bit_off++, bitmap);
  
  	status = ocfs2_journal_dirty(handle,
  				     group_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  bail:
  	mlog_exit(status);
  	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...
1035
  static int ocfs2_relink_block_group(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  				    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. */
  	u64 fe_ptr, bg_ptr, prev_bg_ptr;
  	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 ...
1049
1050
1051
1052
  	/* 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...
1053

b0697053f   Mark Fasheh   ocfs2: don't use ...
1054
1055
  	mlog(0, "Suballoc %llu, chain %u, move group %llu to top, prev = %llu
  ",
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
1056
1057
1058
  	     (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...
1059
1060
1061
1062
  
  	fe_ptr = le64_to_cpu(fe->id2.i_chain.cl_recs[chain].c_blkno);
  	bg_ptr = le64_to_cpu(bg->bg_next_group);
  	prev_bg_ptr = le64_to_cpu(prev_bg->bg_next_group);
13723d00e   Joel Becker   ocfs2: Use metada...
1063
1064
  	status = ocfs2_journal_access_gd(handle, alloc_inode, prev_bg_bh,
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
  
  	prev_bg->bg_next_group = bg->bg_next_group;
  
  	status = ocfs2_journal_dirty(handle, prev_bg_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
1077
1078
  	status = ocfs2_journal_access_gd(handle, alloc_inode, bg_bh,
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
  
  	bg->bg_next_group = fe->id2.i_chain.cl_recs[chain].c_blkno;
  
  	status = ocfs2_journal_dirty(handle, bg_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
1091
1092
  	status = ocfs2_journal_access_di(handle, alloc_inode, fe_bh,
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
  
  	fe->id2.i_chain.cl_recs[chain].c_blkno = bg->bg_blkno;
  
  	status = ocfs2_journal_dirty(handle, fe_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto out_rollback;
  	}
  
  	status = 0;
  out_rollback:
  	if (status < 0) {
  		fe->id2.i_chain.cl_recs[chain].c_blkno = cpu_to_le64(fe_ptr);
  		bg->bg_next_group = cpu_to_le64(bg_ptr);
  		prev_bg->bg_next_group = cpu_to_le64(prev_bg_ptr);
  	}
420353061   Joel Becker   ocfs2: Morph the ...
1113

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
  	mlog_exit(status);
  	return status;
  }
  
  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...
1129
  				      u64 max_block,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1130
1131
1132
1133
  				      u16 *bit_off, u16 *bits_found)
  {
  	int search = -ENOSPC;
  	int ret;
1187c9688   Joel Becker   ocfs2: Limit inod...
1134
  	u64 blkoff;
7bf72edee   Mark Fasheh   ocfs2: better gro...
1135
  	struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *) group_bh->b_data;
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
1136
  	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1137
  	u16 tmp_off, tmp_found;
7bf72edee   Mark Fasheh   ocfs2: better gro...
1138
  	unsigned int max_bits, gd_cluster_off;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1139
1140
  
  	BUG_ON(!ocfs2_is_cluster_bitmap(inode));
7bf72edee   Mark Fasheh   ocfs2: better gro...
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
  	if (gd->bg_free_bits_count) {
  		max_bits = le16_to_cpu(gd->bg_bits);
  
  		/* Tail groups in cluster bitmaps which aren't cpg
  		 * aligned are prone to partial extention by a failed
  		 * 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;
  			mlog(0, "Desc %llu, bg_bits %u, clusters %u, use %u
  ",
  			     (unsigned long long)le64_to_cpu(gd->bg_blkno),
  			     le16_to_cpu(gd->bg_bits),
  			     OCFS2_I(inode)->ip_clusters, max_bits);
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1161
1162
  		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  							group_bh, bits_wanted,
7bf72edee   Mark Fasheh   ocfs2: better gro...
1163
  							max_bits,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1164
1165
1166
  							&tmp_off, &tmp_found);
  		if (ret)
  			return ret;
1187c9688   Joel Becker   ocfs2: Limit inod...
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
  		if (max_block) {
  			blkoff = ocfs2_clusters_to_blocks(inode->i_sb,
  							  gd_cluster_off +
  							  tmp_off + tmp_found);
  			mlog(0, "Checking %llu against %llu
  ",
  			     (unsigned long long)blkoff,
  			     (unsigned long long)max_block);
  			if (blkoff > max_block)
  				return -ENOSPC;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1178
1179
1180
1181
1182
1183
1184
1185
  		/* ocfs2_block_group_find_clear_bits() might
  		 * return success, but we still want to return
  		 * -ENOSPC unless it found the minimum number
  		 * of bits. */
  		if (min_bits <= tmp_found) {
  			*bit_off = tmp_off;
  			*bits_found = tmp_found;
  			search = 0; /* success */
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
1186
1187
1188
1189
1190
1191
  		} else if (tmp_found) {
  			/*
  			 * Don't show bits which we'll be returning
  			 * for allocation to the local alloc bitmap.
  			 */
  			ocfs2_local_alloc_seen_free_bits(osb, tmp_found);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1192
1193
1194
1195
1196
1197
1198
1199
1200
  		}
  	}
  
  	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...
1201
  				    u64 max_block,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1202
1203
1204
  				    u16 *bit_off, u16 *bits_found)
  {
  	int ret = -ENOSPC;
1187c9688   Joel Becker   ocfs2: Limit inod...
1205
  	u64 blkoff;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1206
1207
1208
1209
  	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...
1210
  	if (bg->bg_free_bits_count) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1211
1212
  		ret = ocfs2_block_group_find_clear_bits(OCFS2_SB(inode->i_sb),
  							group_bh, bits_wanted,
7bf72edee   Mark Fasheh   ocfs2: better gro...
1213
  							le16_to_cpu(bg->bg_bits),
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1214
  							bit_off, bits_found);
1187c9688   Joel Becker   ocfs2: Limit inod...
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
  		if (!ret && max_block) {
  			blkoff = le64_to_cpu(bg->bg_blkno) + *bit_off +
  				*bits_found;
  			mlog(0, "Checking %llu against %llu
  ",
  			     (unsigned long long)blkoff,
  			     (unsigned long long)max_block);
  			if (blkoff > max_block)
  				ret = -ENOSPC;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1226
1227
1228
  
  	return ret;
  }
883d4cae4   Mark Fasheh   ocfs2: allocation...
1229
  static int ocfs2_alloc_dinode_update_counts(struct inode *inode,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1230
  				       handle_t *handle,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1231
1232
1233
1234
1235
1236
1237
1238
  				       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;
13723d00e   Joel Becker   ocfs2: Use metada...
1239
1240
  	ret = ocfs2_journal_access_di(handle, inode, di_bh,
  				      OCFS2_JOURNAL_ACCESS_WRITE);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
  	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);
  
  	ret = ocfs2_journal_dirty(handle, di_bh);
  	if (ret < 0)
  		mlog_errno(ret);
  
  out:
  	return ret;
  }
  
  static int ocfs2_search_one_group(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1259
  				  handle_t *handle,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
  				  u32 bits_wanted,
  				  u32 min_bits,
  				  u16 *bit_off,
  				  unsigned int *num_bits,
  				  u64 gd_blkno,
  				  u16 *bits_left)
  {
  	int ret;
  	u16 found;
  	struct buffer_head *group_bh = NULL;
  	struct ocfs2_group_desc *gd;
68f64d471   Joel Becker   ocfs2: Wrap group...
1271
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)ac->ac_bh->b_data;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1272
  	struct inode *alloc_inode = ac->ac_inode;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1273

68f64d471   Joel Becker   ocfs2: Wrap group...
1274
1275
  	ret = ocfs2_read_group_descriptor(alloc_inode, di, gd_blkno,
  					  &group_bh);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1276
1277
1278
1279
1280
1281
  	if (ret < 0) {
  		mlog_errno(ret);
  		return ret;
  	}
  
  	gd = (struct ocfs2_group_desc *) group_bh->b_data;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1282
  	ret = ac->ac_group_search(alloc_inode, group_bh, bits_wanted, min_bits,
1187c9688   Joel Becker   ocfs2: Limit inod...
1283
  				  ac->ac_max_block, bit_off, &found);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
  	if (ret < 0) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
  		goto out;
  	}
  
  	*num_bits = found;
  
  	ret = ocfs2_alloc_dinode_update_counts(alloc_inode, handle, ac->ac_bh,
  					       *num_bits,
  					       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,
  					 *bit_off, *num_bits);
  	if (ret < 0)
  		mlog_errno(ret);
  
  	*bits_left = le16_to_cpu(gd->bg_free_bits_count);
  
  out:
  	brelse(group_bh);
  
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1312
  static int ocfs2_search_chain(struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1313
  			      handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1314
1315
1316
1317
  			      u32 bits_wanted,
  			      u32 min_bits,
  			      u16 *bit_off,
  			      unsigned int *num_bits,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1318
1319
  			      u64 *bg_blkno,
  			      u16 *bits_left)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1320
1321
1322
1323
1324
  {
  	int status;
  	u16 chain, tmp_bits;
  	u32 tmp_used;
  	u64 next_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1325
1326
1327
1328
1329
1330
1331
1332
  	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;
b0697053f   Mark Fasheh   ocfs2: don't use ...
1333
1334
1335
1336
  	mlog(0, "trying to alloc %u bits from chain %u, inode %llu
  ",
  	     bits_wanted, chain,
  	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1337

68f64d471   Joel Becker   ocfs2: Wrap group...
1338
1339
1340
  	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...
1341
1342
1343
1344
1345
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1346
1347
1348
1349
  
  	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...
1350
1351
1352
1353
  	while ((status = ac->ac_group_search(alloc_inode, group_bh,
  					     bits_wanted, min_bits,
  					     ac->ac_max_block, bit_off,
  					     &tmp_bits)) == -ENOSPC) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1354
1355
  		if (!bg->bg_next_group)
  			break;
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
1356
1357
1358
  
  		brelse(prev_group_bh);
  		prev_group_bh = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1359
1360
1361
  		next_group = le64_to_cpu(bg->bg_next_group);
  		prev_group_bh = group_bh;
  		group_bh = NULL;
68f64d471   Joel Becker   ocfs2: Wrap group...
1362
1363
  		status = ocfs2_read_group_descriptor(alloc_inode, fe,
  						     next_group, &group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1364
1365
1366
1367
1368
  		if (status < 0) {
  			mlog_errno(status);
  			goto bail;
  		}
  		bg = (struct ocfs2_group_desc *) group_bh->b_data;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1369
1370
1371
1372
1373
1374
  	}
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
b0697053f   Mark Fasheh   ocfs2: don't use ...
1375
1376
  	mlog(0, "alloc succeeds: we give %u bits from block group %llu
  ",
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
1377
  	     tmp_bits, (unsigned long long)le64_to_cpu(bg->bg_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
  
  	*num_bits = tmp_bits;
  
  	BUG_ON(*num_bits == 0);
  
  	/*
  	 * 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.
  	 */
  	if (ac->ac_allow_chain_relink &&
  	    (prev_group_bh) &&
  	    (ocfs2_block_group_reasonably_empty(bg, *num_bits))) {
  		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;
  		}
  	}
  
  	/* Ok, claim our bits now: set the info on dinode, chainlist
  	 * and then the group */
13723d00e   Joel Becker   ocfs2: Use metada...
1410
1411
1412
1413
  	status = ocfs2_journal_access_di(handle,
  					 alloc_inode,
  					 ac->ac_bh,
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	tmp_used = le32_to_cpu(fe->id1.bitmap1.i_used);
  	fe->id1.bitmap1.i_used = cpu_to_le32(*num_bits + tmp_used);
  	le32_add_cpu(&cl->cl_recs[chain].c_free, -(*num_bits));
  
  	status = ocfs2_journal_dirty(handle,
  				     ac->ac_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	status = ocfs2_block_group_set_bits(handle,
  					    alloc_inode,
  					    bg,
  					    group_bh,
  					    *bit_off,
  					    *num_bits);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
b0697053f   Mark Fasheh   ocfs2: don't use ...
1440
1441
  	mlog(0, "Allocated %u bits from suballocator %llu
  ", *num_bits,
1ca1a111b   Mark Fasheh   ocfs2: fix sparse...
1442
  	     (unsigned long long)le64_to_cpu(fe->i_blkno));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1443
1444
  
  	*bg_blkno = le64_to_cpu(bg->bg_blkno);
883d4cae4   Mark Fasheh   ocfs2: allocation...
1445
  	*bits_left = le16_to_cpu(bg->bg_free_bits_count);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1446
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
1447
1448
  	brelse(group_bh);
  	brelse(prev_group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1449
1450
1451
1452
1453
1454
1455
1456
  
  	mlog_exit(status);
  	return status;
  }
  
  /* will give out up to bits_wanted contiguous bits. */
  static int ocfs2_claim_suballoc_bits(struct ocfs2_super *osb,
  				     struct ocfs2_alloc_context *ac,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1457
  				     handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1458
1459
1460
1461
1462
1463
1464
1465
  				     u32 bits_wanted,
  				     u32 min_bits,
  				     u16 *bit_off,
  				     unsigned int *num_bits,
  				     u64 *bg_blkno)
  {
  	int status;
  	u16 victim, i;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1466
1467
  	u16 bits_left = 0;
  	u64 hint_blkno = ac->ac_last_group;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
  	struct ocfs2_chain_list *cl;
  	struct ocfs2_dinode *fe;
  
  	mlog_entry_void();
  
  	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 ...
1478
1479
1480
1481
  
  	/* 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...
1482
1483
  	if (le32_to_cpu(fe->id1.bitmap1.i_used) >=
  	    le32_to_cpu(fe->id1.bitmap1.i_total)) {
b0697053f   Mark Fasheh   ocfs2: don't use ...
1484
1485
1486
  		ocfs2_error(osb->sb, "Chain allocator dinode %llu has %u used "
  			    "bits but only %u total.",
  			    (unsigned long long)le64_to_cpu(fe->i_blkno),
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1487
1488
1489
1490
1491
  			    le32_to_cpu(fe->id1.bitmap1.i_used),
  			    le32_to_cpu(fe->id1.bitmap1.i_total));
  		status = -EIO;
  		goto bail;
  	}
883d4cae4   Mark Fasheh   ocfs2: allocation...
1492
1493
1494
1495
1496
  	if (hint_blkno) {
  		/* Attempt to short-circuit the usual search mechanism
  		 * by jumping straight to the most recently used
  		 * allocation group. This helps us mantain some
  		 * contiguousness across allocations. */
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1497
1498
  		status = ocfs2_search_one_group(ac, handle, bits_wanted,
  						min_bits, bit_off, num_bits,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
  						hint_blkno, &bits_left);
  		if (!status) {
  			/* Be careful to update *bg_blkno here as the
  			 * caller is expecting it to be filled in, and
  			 * ocfs2_search_one_group() won't do that for
  			 * us. */
  			*bg_blkno = hint_blkno;
  			goto set_hint;
  		}
  		if (status < 0 && status != -ENOSPC) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1513
1514
1515
1516
1517
  	cl = (struct ocfs2_chain_list *) &fe->id2.i_chain;
  
  	victim = ocfs2_find_victim_chain(cl);
  	ac->ac_chain = victim;
  	ac->ac_allow_chain_relink = 1;
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1518
  	status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits, bit_off,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1519
  				    num_bits, bg_blkno, &bits_left);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1520
  	if (!status)
883d4cae4   Mark Fasheh   ocfs2: allocation...
1521
  		goto set_hint;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
  	if (status < 0 && status != -ENOSPC) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	mlog(0, "Search of victim chain %u came up with nothing, "
  	     "trying all chains now.
  ", victim);
  
  	/* 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. */
  	ac->ac_allow_chain_relink = 0;
  	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 ...
1543
  		status = ocfs2_search_chain(ac, handle, bits_wanted, min_bits,
883d4cae4   Mark Fasheh   ocfs2: allocation...
1544
1545
  					    bit_off, num_bits, bg_blkno,
  					    &bits_left);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1546
1547
1548
1549
1550
1551
1552
  		if (!status)
  			break;
  		if (status < 0 && status != -ENOSPC) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1553

883d4cae4   Mark Fasheh   ocfs2: allocation...
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
  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
  			ac->ac_last_group = *bg_blkno;
  	}
  
  bail:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1566
1567
1568
1569
1570
  	mlog_exit(status);
  	return status;
  }
  
  int ocfs2_claim_metadata(struct ocfs2_super *osb,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1571
  			 handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
  			 struct ocfs2_alloc_context *ac,
  			 u32 bits_wanted,
  			 u16 *suballoc_bit_start,
  			 unsigned int *num_bits,
  			 u64 *blkno_start)
  {
  	int status;
  	u64 bg_blkno;
  
  	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...
1584
1585
1586
  
  	status = ocfs2_claim_suballoc_bits(osb,
  					   ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1587
  					   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
  					   bits_wanted,
  					   1,
  					   suballoc_bit_start,
  					   num_bits,
  					   &bg_blkno);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	atomic_inc(&osb->alloc_stats.bg_allocs);
  
  	*blkno_start = bg_blkno + (u64) *suballoc_bit_start;
  	ac->ac_bits_given += (*num_bits);
  	status = 0;
  bail:
  	mlog_exit(status);
  	return status;
  }
138211515   Tao Ma   ocfs2: Optimize i...
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
  static void ocfs2_init_inode_ac_group(struct inode *dir,
  				      struct buffer_head *parent_fe_bh,
  				      struct ocfs2_alloc_context *ac)
  {
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  	/*
  	 * 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;
  	else if (le16_to_cpu(fe->i_suballoc_slot) == ac->ac_alloc_slot)
  		ac->ac_last_group = ocfs2_which_suballoc_group(
  					le64_to_cpu(fe->i_blkno),
  					le16_to_cpu(fe->i_suballoc_bit));
  }
  
  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;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1636
  int ocfs2_claim_new_inode(struct ocfs2_super *osb,
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1637
  			  handle_t *handle,
138211515   Tao Ma   ocfs2: Optimize i...
1638
1639
  			  struct inode *dir,
  			  struct buffer_head *parent_fe_bh,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
  			  struct ocfs2_alloc_context *ac,
  			  u16 *suballoc_bit,
  			  u64 *fe_blkno)
  {
  	int status;
  	unsigned int num_bits;
  	u64 bg_blkno;
  
  	mlog_entry_void();
  
  	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...
1654

138211515   Tao Ma   ocfs2: Optimize i...
1655
  	ocfs2_init_inode_ac_group(dir, parent_fe_bh, ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1656
1657
  	status = ocfs2_claim_suballoc_bits(osb,
  					   ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1658
  					   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
  					   1,
  					   1,
  					   suballoc_bit,
  					   &num_bits,
  					   &bg_blkno);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	atomic_inc(&osb->alloc_stats.bg_allocs);
  
  	BUG_ON(num_bits != 1);
  
  	*fe_blkno = bg_blkno + (u64) (*suballoc_bit);
  	ac->ac_bits_given++;
138211515   Tao Ma   ocfs2: Optimize i...
1674
  	ocfs2_save_inode_ac_group(dir, ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
  	status = 0;
  bail:
  	mlog_exit(status);
  	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...
1700
  u64 ocfs2_which_cluster_group(struct inode *inode, u32 cluster)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
  {
  	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.
   */
415cb8003   Mark Fasheh   ocfs2: Allow smal...
1742
1743
1744
1745
1746
1747
1748
  int __ocfs2_claim_clusters(struct ocfs2_super *osb,
  			   handle_t *handle,
  			   struct ocfs2_alloc_context *ac,
  			   u32 min_clusters,
  			   u32 max_clusters,
  			   u32 *cluster_start,
  			   u32 *num_clusters)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1749
1750
  {
  	int status;
415cb8003   Mark Fasheh   ocfs2: Allow smal...
1751
  	unsigned int bits_wanted = max_clusters;
883d4cae4   Mark Fasheh   ocfs2: allocation...
1752
  	u64 bg_blkno = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1753
1754
1755
  	u16 bg_bit_off;
  
  	mlog_entry_void();
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1756
1757
1758
1759
  	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...
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
  
  	if (ac->ac_which == OCFS2_AC_USE_LOCAL) {
  		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...
1774
1775
1776
1777
  			mlog(ML_ERROR, "minimum allocation requested %u exceeds "
  			     "group bitmap size %u!
  ", min_clusters,
  			     osb->bitmap_cpg);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1778
1779
1780
1781
1782
1783
1784
1785
1786
  			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;
  
  		status = ocfs2_claim_suballoc_bits(osb,
  						   ac,
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
1787
  						   handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
  						   bits_wanted,
  						   min_clusters,
  						   &bg_bit_off,
  						   num_clusters,
  						   &bg_blkno);
  		if (!status) {
  			*cluster_start =
  				ocfs2_desc_bitmap_to_cluster_off(ac->ac_inode,
  								 bg_blkno,
  								 bg_bit_off);
  			atomic_inc(&osb->alloc_stats.bitmap_data);
  		}
  	}
  	if (status < 0) {
  		if (status != -ENOSPC)
  			mlog_errno(status);
  		goto bail;
  	}
  
  	ac->ac_bits_given += *num_clusters;
  
  bail:
  	mlog_exit(status);
  	return status;
  }
415cb8003   Mark Fasheh   ocfs2: Allow smal...
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
  int ocfs2_claim_clusters(struct ocfs2_super *osb,
  			 handle_t *handle,
  			 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;
  
  	return __ocfs2_claim_clusters(osb, handle, ac, min_clusters,
  				      bits_wanted, cluster_start, num_clusters);
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1825
  static inline int ocfs2_block_group_clear_bits(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
  					       struct inode *alloc_inode,
  					       struct ocfs2_group_desc *bg,
  					       struct buffer_head *group_bh,
  					       unsigned int bit_off,
  					       unsigned int num_bits)
  {
  	int status;
  	unsigned int tmp;
  	int journal_type = OCFS2_JOURNAL_ACCESS_WRITE;
  	struct ocfs2_group_desc *undo_bg = NULL;
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1836
  	int cluster_bitmap = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1837
1838
  
  	mlog_entry_void();
420353061   Joel Becker   ocfs2: Morph the ...
1839
1840
1841
  	/* 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...
1842
1843
1844
1845
1846
1847
  
  	mlog(0, "off = %u, num = %u
  ", bit_off, num_bits);
  
  	if (ocfs2_is_cluster_bitmap(alloc_inode))
  		journal_type = OCFS2_JOURNAL_ACCESS_UNDO;
13723d00e   Joel Becker   ocfs2: Use metada...
1848
1849
  	status = ocfs2_journal_access_gd(handle, alloc_inode, group_bh,
  					 journal_type);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1850
1851
1852
1853
1854
1855
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	if (ocfs2_is_cluster_bitmap(alloc_inode))
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1856
1857
1858
1859
1860
1861
1862
1863
  		cluster_bitmap = 1;
  
  	if (cluster_bitmap) {
  		jbd_lock_bh_state(group_bh);
  		undo_bg = (struct ocfs2_group_desc *)
  					bh2jh(group_bh)->b_committed_data;
  		BUG_ON(!undo_bg);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1864
1865
1866
1867
1868
  
  	tmp = num_bits;
  	while(tmp--) {
  		ocfs2_clear_bit((bit_off + tmp),
  				(unsigned long *) bg->bg_bitmap);
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1869
  		if (cluster_bitmap)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1870
1871
1872
1873
  			ocfs2_set_bit(bit_off + tmp,
  				      (unsigned long *) undo_bg->bg_bitmap);
  	}
  	le16_add_cpu(&bg->bg_free_bits_count, num_bits);
94e41ecfe   Sunil Mushran   ocfs2: Pin journa...
1874
1875
  	if (cluster_bitmap)
  		jbd_unlock_bh_state(group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
  	status = ocfs2_journal_dirty(handle, group_bh);
  	if (status < 0)
  		mlog_errno(status);
  bail:
  	return status;
  }
  
  /*
   * expects the suballoc inode to already be locked.
   */
2b604351b   Mark Fasheh   ocfs2: simplify d...
1886
1887
1888
1889
1890
1891
  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)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1892
1893
1894
  {
  	int status = 0;
  	u32 tmp_used;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1895
1896
1897
1898
1899
1900
  	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;
  
  	mlog_entry_void();
10995aa24   Joel Becker   ocfs2: Morph the ...
1901
1902
1903
1904
1905
1906
  	/* 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
  	 * validates the dinode buffer.  Any corruption that has happended
  	 * is a code bug. */
  	BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1907
  	BUG_ON((count + start_bit) > ocfs2_bits_per_group(cl));
b0697053f   Mark Fasheh   ocfs2: don't use ...
1908
1909
1910
1911
  	mlog(0, "%llu: freeing %u bits from group %llu, starting at %u
  ",
  	     (unsigned long long)OCFS2_I(alloc_inode)->ip_blkno, count,
  	     (unsigned long long)bg_blkno, start_bit);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1912

68f64d471   Joel Becker   ocfs2: Wrap group...
1913
1914
  	status = ocfs2_read_group_descriptor(alloc_inode, fe, bg_blkno,
  					     &group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1915
1916
1917
1918
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1919
  	group = (struct ocfs2_group_desc *) group_bh->b_data;
68f64d471   Joel Becker   ocfs2: Wrap group...
1920

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1921
1922
1923
1924
1925
1926
1927
1928
1929
  	BUG_ON((count + start_bit) > le16_to_cpu(group->bg_bits));
  
  	status = ocfs2_block_group_clear_bits(handle, alloc_inode,
  					      group, group_bh,
  					      start_bit, count);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
1930
1931
  	status = ocfs2_journal_access_di(handle, alloc_inode, alloc_bh,
  					 OCFS2_JOURNAL_ACCESS_WRITE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
  	if (status < 0) {
  		mlog_errno(status);
  		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);
  
  	status = ocfs2_journal_dirty(handle, alloc_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
1949
  	brelse(group_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1950
1951
1952
1953
  
  	mlog_exit(status);
  	return status;
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1954
  int ocfs2_free_dinode(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
  		      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);
  
  	return ocfs2_free_suballoc_bits(handle, inode_alloc_inode,
  					inode_alloc_bh, bit, bg_blkno, 1);
  }
1fabe1481   Mark Fasheh   ocfs2: Remove str...
1966
  int ocfs2_free_clusters(handle_t *handle,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
  		       struct inode *bitmap_inode,
  		       struct buffer_head *bitmap_bh,
  		       u64 start_blk,
  		       unsigned int num_clusters)
  {
  	int status;
  	u16 bg_start_bit;
  	u64 bg_blkno;
  	struct ocfs2_dinode *fe;
  
  	/* You can't ever have a contiguous set of clusters
  	 * bigger than a block group bitmap so we never have to worry
  	 * about looping on them. */
  
  	mlog_entry_void();
  
  	/* This is expensive. We can safely remove once this stuff has
  	 * gotten tested really well. */
  	BUG_ON(start_blk != ocfs2_clusters_to_blocks(bitmap_inode->i_sb, ocfs2_blocks_to_clusters(bitmap_inode->i_sb, start_blk)));
  
  	fe = (struct ocfs2_dinode *) bitmap_bh->b_data;
  
  	ocfs2_block_to_cluster_group(bitmap_inode, start_blk, &bg_blkno,
  				     &bg_start_bit);
b0697053f   Mark Fasheh   ocfs2: don't use ...
1991
1992
1993
1994
1995
1996
  	mlog(0, "want to free %u clusters starting at block %llu
  ",
  	     num_clusters, (unsigned long long)start_blk);
  	mlog(0, "bg_blkno = %llu, bg_start_bit = %u
  ",
  	     (unsigned long long)bg_blkno, bg_start_bit);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1997
1998
1999
2000
  
  	status = ocfs2_free_suballoc_bits(handle, bitmap_inode, bitmap_bh,
  					  bg_start_bit, bg_blkno,
  					  num_clusters);
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2001
  	if (status < 0) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2002
  		mlog_errno(status);
9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2003
2004
2005
2006
2007
  		goto out;
  	}
  
  	ocfs2_local_alloc_seen_free_bits(OCFS2_SB(bitmap_inode->i_sb),
  					 num_clusters);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2008

9c7af40b2   Mark Fasheh   ocfs2: throttle b...
2009
  out:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
  	mlog_exit(status);
  	return status;
  }
  
  static inline void ocfs2_debug_bg(struct ocfs2_group_desc *bg)
  {
  	printk("Block Group:
  ");
  	printk("bg_signature:       %s
  ", bg->bg_signature);
  	printk("bg_size:            %u
  ", bg->bg_size);
  	printk("bg_bits:            %u
  ", bg->bg_bits);
  	printk("bg_free_bits_count: %u
  ", bg->bg_free_bits_count);
  	printk("bg_chain:           %u
  ", bg->bg_chain);
  	printk("bg_generation:      %u
  ", le32_to_cpu(bg->bg_generation));
b0697053f   Mark Fasheh   ocfs2: don't use ...
2030
2031
2032
2033
2034
2035
2036
2037
2038
  	printk("bg_next_group:      %llu
  ",
  	       (unsigned long long)bg->bg_next_group);
  	printk("bg_parent_dinode:   %llu
  ",
  	       (unsigned long long)bg->bg_parent_dinode);
  	printk("bg_blkno:           %llu
  ",
  	       (unsigned long long)bg->bg_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2039
2040
2041
2042
2043
  }
  
  static inline void ocfs2_debug_suballoc_inode(struct ocfs2_dinode *fe)
  {
  	int i;
b0697053f   Mark Fasheh   ocfs2: don't use ...
2044
2045
  	printk("Suballoc Inode %llu:
  ", (unsigned long long)fe->i_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2046
2047
  	printk("i_signature:                  %s
  ", fe->i_signature);
b0697053f   Mark Fasheh   ocfs2: don't use ...
2048
2049
2050
  	printk("i_size:                       %llu
  ",
  	       (unsigned long long)fe->i_size);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
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
  	printk("i_clusters:                   %u
  ", fe->i_clusters);
  	printk("i_generation:                 %u
  ",
  	       le32_to_cpu(fe->i_generation));
  	printk("id1.bitmap1.i_used:           %u
  ",
  	       le32_to_cpu(fe->id1.bitmap1.i_used));
  	printk("id1.bitmap1.i_total:          %u
  ",
  	       le32_to_cpu(fe->id1.bitmap1.i_total));
  	printk("id2.i_chain.cl_cpg:           %u
  ", fe->id2.i_chain.cl_cpg);
  	printk("id2.i_chain.cl_bpc:           %u
  ", fe->id2.i_chain.cl_bpc);
  	printk("id2.i_chain.cl_count:         %u
  ", fe->id2.i_chain.cl_count);
  	printk("id2.i_chain.cl_next_free_rec: %u
  ",
  	       fe->id2.i_chain.cl_next_free_rec);
  	for(i = 0; i < fe->id2.i_chain.cl_next_free_rec; i++) {
  		printk("fe->id2.i_chain.cl_recs[%d].c_free:  %u
  ", i,
  		       fe->id2.i_chain.cl_recs[i].c_free);
  		printk("fe->id2.i_chain.cl_recs[%d].c_total: %u
  ", i,
  		       fe->id2.i_chain.cl_recs[i].c_total);
b0697053f   Mark Fasheh   ocfs2: don't use ...
2078
2079
2080
  		printk("fe->id2.i_chain.cl_recs[%d].c_blkno: %llu
  ", i,
  		       (unsigned long long)fe->id2.i_chain.cl_recs[i].c_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2081
2082
  	}
  }
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
  
  /*
   * 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...
2094
2095
  int ocfs2_lock_allocators(struct inode *inode,
  			  struct ocfs2_extent_tree *et,
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2096
2097
  			  u32 clusters_to_add, u32 extents_to_split,
  			  struct ocfs2_alloc_context **data_ac,
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2098
  			  struct ocfs2_alloc_context **meta_ac)
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
  {
  	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);
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2109
  	num_free_extents = ocfs2_num_free_extents(osb, inode, et);
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
  	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...
2131
  		ret = ocfs2_reserve_new_metadata(osb, et->et_root_el, meta_ac);
e7d4cb6bc   Tao Ma   ocfs2: Abstract o...
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
  		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...
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
  
  /*
   * 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,
  				       u16 *suballoc_slot, u16 *suballoc_bit)
  {
  	int status;
  	struct buffer_head *inode_bh = NULL;
  	struct ocfs2_dinode *inode_fe;
5b09b507d   Joel Becker   ocfs2: Fix some p...
2174
2175
  	mlog_entry("blkno: %llu
  ", (unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2176
2177
2178
2179
  
  	/* dirty read disk */
  	status = ocfs2_read_blocks_sync(osb, blkno, 1, &inode_bh);
  	if (status < 0) {
5b09b507d   Joel Becker   ocfs2: Fix some p...
2180
2181
2182
  		mlog(ML_ERROR, "read block %llu failed %d
  ",
  		     (unsigned long long)blkno, status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2183
2184
2185
2186
2187
  		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...
2188
2189
2190
  		mlog(ML_ERROR, "invalid inode %llu requested
  ",
  		     (unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2191
2192
2193
  		status = -EINVAL;
  		goto bail;
  	}
0fba81374   Tao Ma   ocfs2: Fix 2 warn...
2194
  	if (le16_to_cpu(inode_fe->i_suballoc_slot) != (u16)OCFS2_INVALID_SLOT &&
6ca497a83   wengang wang   ocfs2: fix rare s...
2195
2196
2197
  	    (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...
2198
2199
  		     (unsigned long long)blkno,
  		     (u32)le16_to_cpu(inode_fe->i_suballoc_slot));
6ca497a83   wengang wang   ocfs2: fix rare s...
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
  		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);
  
  bail:
  	brelse(inode_bh);
  
  	mlog_exit(status);
  	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,
  				   struct buffer_head *alloc_bh, u64 blkno,
  				   u16 bit, int *res)
  {
  	struct ocfs2_dinode *alloc_fe;
  	struct ocfs2_group_desc *group;
  	struct buffer_head *group_bh = NULL;
  	u64 bg_blkno;
  	int status;
5b09b507d   Joel Becker   ocfs2: Fix some p...
2233
2234
2235
  	mlog_entry("blkno: %llu bit: %u
  ", (unsigned long long)blkno,
  		   (unsigned int)bit);
6ca497a83   wengang wang   ocfs2: fix rare s...
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
  
  	alloc_fe = (struct ocfs2_dinode *)alloc_bh->b_data;
  	if ((bit + 1) > ocfs2_bits_per_group(&alloc_fe->id2.i_chain)) {
  		mlog(ML_ERROR, "suballoc bit %u out of range of %u
  ",
  		     (unsigned int)bit,
  		     ocfs2_bits_per_group(&alloc_fe->id2.i_chain));
  		status = -EINVAL;
  		goto bail;
  	}
  
  	bg_blkno = ocfs2_which_suballoc_group(blkno, bit);
  	status = ocfs2_read_group_descriptor(suballoc, alloc_fe, bg_blkno,
  					     &group_bh);
  	if (status < 0) {
5b09b507d   Joel Becker   ocfs2: Fix some p...
2251
2252
2253
  		mlog(ML_ERROR, "read group %llu failed %d
  ",
  		     (unsigned long long)bg_blkno, status);
6ca497a83   wengang wang   ocfs2: fix rare s...
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
  		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);
  
  	mlog_exit(status);
  	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;
  	u16 suballoc_bit = 0, suballoc_slot = 0;
  	struct inode *inode_alloc_inode;
  	struct buffer_head *alloc_bh = NULL;
5b09b507d   Joel Becker   ocfs2: Fix some p...
2286
  	mlog_entry("blkno: %llu", (unsigned long long)blkno);
6ca497a83   wengang wang   ocfs2: fix rare s...
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
  
  	status = ocfs2_get_suballoc_slot_bit(osb, blkno, &suballoc_slot,
  					     &suballoc_bit);
  	if (status < 0) {
  		mlog(ML_ERROR, "get alloc slot and bit failed %d
  ", status);
  		goto bail;
  	}
  
  	inode_alloc_inode =
  		ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE,
  					    suballoc_slot);
  	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;
  	}
  
  	mutex_lock(&inode_alloc_inode->i_mutex);
  	status = ocfs2_inode_lock(inode_alloc_inode, &alloc_bh, 0);
  	if (status < 0) {
  		mutex_unlock(&inode_alloc_inode->i_mutex);
  		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,
  					 blkno, suballoc_bit, res);
  	if (status < 0)
  		mlog(ML_ERROR, "test suballoc bit failed %d
  ", status);
  
  	ocfs2_inode_unlock(inode_alloc_inode, 0);
  	mutex_unlock(&inode_alloc_inode->i_mutex);
  
  	iput(inode_alloc_inode);
  	brelse(alloc_bh);
  bail:
  	mlog_exit(status);
  	return status;
  }