blob: 3dfad95f0f9889fde6d9cdfcd0bdd0ad6e5af15b [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 */
18
19
20/*
21 * mballoc.c contains the multiblocks allocation routines
22 */
23
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040025#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040027#include <trace/events/ext4.h>
28
Alex Tomasc9de5602008-01-29 00:19:52 -050029/*
30 * MUSTDO:
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
33 *
34 * TODO v4:
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
38 * - quota
39 * - reservation for superuser
40 *
41 * TODO v3:
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
46 * - error handling
47 */
48
49/*
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
52 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040053 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050062 *
63 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050065 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040066 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
69 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050070 *
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
Daniel Mack1537a362010-01-29 15:57:49 +080073 * pa_len -> length for this prealloc space
Alex Tomasc9de5602008-01-29 00:19:52 -050074 * pa_free -> free space available in this prealloc space
75 *
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
80 *
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
83 * pa_free.
84 *
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
87 * prealloc space. These are per CPU prealloc list repreasented as
88 *
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 *
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
93 *
94 * The locality group prealloc space is used looking at whether we have
95 * enough free space (pa_free) withing the prealloc space.
96 *
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
105 * inode as:
106 *
107 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500109 *
110 *
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
115 *
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
118 *
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
122 *
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500130 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
135 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400136 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400142 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * checked.
157 *
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
162 */
163
164/*
165 * mballoc operates on the following data:
166 * - on-disk bitmap
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
169 *
170 * there are two types of preallocations:
171 * - inode
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
180 * - locality group
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
185 *
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
188 *
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
192 *
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
196 *
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 *
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
210 *
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * block
222 *
223 * so, now we're building a concurrency table:
224 * - init buddy vs.
225 * - new PA
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
228 * - use inode PA
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
231 * - discard inode PA
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
237 * - new PA vs.
238 * - use inode PA
239 * i_data_sem serializes them
240 * - discard inode PA
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
246 * - use inode PA
247 * - use inode PA
248 * i_data_sem or another mutex should serializes them
249 * - discard inode PA
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
255 *
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 *
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
266 *
267 */
268
269 /*
270 * Logic in few words:
271 *
272 * - allocation:
273 * load group
274 * find blocks
275 * mark bits in on-disk bitmap
276 * release group
277 *
278 * - use preallocation:
279 * find proper PA (per-inode or group)
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 * release PA
284 *
285 * - free:
286 * load group
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - discard preallocations in group:
291 * mark PAs deleted
292 * move them onto local list
293 * load on-disk bitmap
294 * load group
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
297 *
298 * - discard inode's preallocations:
299 */
300
301/*
302 * Locking rules
303 *
304 * Locks:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
307 * - per-pa lock (pa)
308 *
309 * Paths:
310 * - new pa
311 * object
312 * group
313 *
314 * - find and use pa:
315 * pa
316 *
317 * - release consumed pa:
318 * pa
319 * group
320 * object
321 *
322 * - generate in-core bitmap:
323 * group
324 * pa
325 *
326 * - discard all for given object (inode, locality group):
327 * object
328 * pa
329 * group
330 *
331 * - discard all for given group:
332 * group
333 * pa
334 * group
335 * object
336 *
337 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500338static struct kmem_cache *ext4_pspace_cachep;
339static struct kmem_cache *ext4_ac_cachep;
340static struct kmem_cache *ext4_free_ext_cachep;
341static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
342 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500343static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
344 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500345static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
346
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500347static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
348{
Alex Tomasc9de5602008-01-29 00:19:52 -0500349#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500350 *bit += ((unsigned long) addr & 7UL) << 3;
351 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500352#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500353 *bit += ((unsigned long) addr & 3UL) << 3;
354 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500355#else
356#error "how many bits you are?!"
357#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500358 return addr;
359}
Alex Tomasc9de5602008-01-29 00:19:52 -0500360
361static inline int mb_test_bit(int bit, void *addr)
362{
363 /*
364 * ext4_test_bit on architecture like powerpc
365 * needs unsigned long aligned address
366 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500367 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500368 return ext4_test_bit(bit, addr);
369}
370
371static inline void mb_set_bit(int bit, void *addr)
372{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500373 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500374 ext4_set_bit(bit, addr);
375}
376
Alex Tomasc9de5602008-01-29 00:19:52 -0500377static inline void mb_clear_bit(int bit, void *addr)
378{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500379 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500380 ext4_clear_bit(bit, addr);
381}
382
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500383static inline int mb_find_next_zero_bit(void *addr, int max, int start)
384{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400385 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400387 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500388 start += fix;
389
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400390 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
391 if (ret > max)
392 return max;
393 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394}
395
396static inline int mb_find_next_bit(void *addr, int max, int start)
397{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400400 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401 start += fix;
402
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400403 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
404 if (ret > max)
405 return max;
406 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500407}
408
Alex Tomasc9de5602008-01-29 00:19:52 -0500409static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
410{
411 char *bb;
412
Alex Tomasc9de5602008-01-29 00:19:52 -0500413 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
414 BUG_ON(max == NULL);
415
416 if (order > e4b->bd_blkbits + 1) {
417 *max = 0;
418 return NULL;
419 }
420
421 /* at order 0 we see each particular block */
422 *max = 1 << (e4b->bd_blkbits + 3);
423 if (order == 0)
424 return EXT4_MB_BITMAP(e4b);
425
426 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
427 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
428
429 return bb;
430}
431
432#ifdef DOUBLE_CHECK
433static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
434 int first, int count)
435{
436 int i;
437 struct super_block *sb = e4b->bd_sb;
438
439 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
440 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400441 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500442 for (i = 0; i < count; i++) {
443 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
444 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500445
446 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500447 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500448 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400449 inode ? inode->i_ino : 0,
450 blocknr,
451 "freeing block already freed "
452 "(bit %u)",
453 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500454 }
455 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
456 }
457}
458
459static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
460{
461 int i;
462
463 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
464 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400465 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500466 for (i = 0; i < count; i++) {
467 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
468 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
469 }
470}
471
472static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
473{
474 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
475 unsigned char *b1, *b2;
476 int i;
477 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
478 b2 = (unsigned char *) bitmap;
479 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
480 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500481 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400482 "at byte %u(%u): %x in copy != %x "
483 "on disk/prealloc\n",
484 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500485 BUG();
486 }
487 }
488 }
489}
490
491#else
492static inline void mb_free_blocks_double(struct inode *inode,
493 struct ext4_buddy *e4b, int first, int count)
494{
495 return;
496}
497static inline void mb_mark_used_double(struct ext4_buddy *e4b,
498 int first, int count)
499{
500 return;
501}
502static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
503{
504 return;
505}
506#endif
507
508#ifdef AGGRESSIVE_CHECK
509
510#define MB_CHECK_ASSERT(assert) \
511do { \
512 if (!(assert)) { \
513 printk(KERN_EMERG \
514 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
515 function, file, line, # assert); \
516 BUG(); \
517 } \
518} while (0)
519
520static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
521 const char *function, int line)
522{
523 struct super_block *sb = e4b->bd_sb;
524 int order = e4b->bd_blkbits + 1;
525 int max;
526 int max2;
527 int i;
528 int j;
529 int k;
530 int count;
531 struct ext4_group_info *grp;
532 int fragments = 0;
533 int fstart;
534 struct list_head *cur;
535 void *buddy;
536 void *buddy2;
537
Alex Tomasc9de5602008-01-29 00:19:52 -0500538 {
539 static int mb_check_counter;
540 if (mb_check_counter++ % 100 != 0)
541 return 0;
542 }
543
544 while (order > 1) {
545 buddy = mb_find_buddy(e4b, order, &max);
546 MB_CHECK_ASSERT(buddy);
547 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
548 MB_CHECK_ASSERT(buddy2);
549 MB_CHECK_ASSERT(buddy != buddy2);
550 MB_CHECK_ASSERT(max * 2 == max2);
551
552 count = 0;
553 for (i = 0; i < max; i++) {
554
555 if (mb_test_bit(i, buddy)) {
556 /* only single bit in buddy2 may be 1 */
557 if (!mb_test_bit(i << 1, buddy2)) {
558 MB_CHECK_ASSERT(
559 mb_test_bit((i<<1)+1, buddy2));
560 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
561 MB_CHECK_ASSERT(
562 mb_test_bit(i << 1, buddy2));
563 }
564 continue;
565 }
566
567 /* both bits in buddy2 must be 0 */
568 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
569 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
570
571 for (j = 0; j < (1 << order); j++) {
572 k = (i * (1 << order)) + j;
573 MB_CHECK_ASSERT(
574 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
575 }
576 count++;
577 }
578 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
579 order--;
580 }
581
582 fstart = -1;
583 buddy = mb_find_buddy(e4b, 0, &max);
584 for (i = 0; i < max; i++) {
585 if (!mb_test_bit(i, buddy)) {
586 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
587 if (fstart == -1) {
588 fragments++;
589 fstart = i;
590 }
591 continue;
592 }
593 fstart = -1;
594 /* check used bits only */
595 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
596 buddy2 = mb_find_buddy(e4b, j, &max2);
597 k = i >> j;
598 MB_CHECK_ASSERT(k < max2);
599 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
600 }
601 }
602 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
603 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
604
605 grp = ext4_get_group_info(sb, e4b->bd_group);
606 buddy = mb_find_buddy(e4b, 0, &max);
607 list_for_each(cur, &grp->bb_prealloc_list) {
608 ext4_group_t groupnr;
609 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400610 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
611 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500612 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400613 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500614 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
615 }
616 return 0;
617}
618#undef MB_CHECK_ASSERT
619#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400620 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500621#else
622#define mb_check_buddy(e4b)
623#endif
624
625/* FIXME!! need more doc */
626static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400627 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500628 struct ext4_group_info *grp)
629{
630 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400631 ext4_grpblk_t min;
632 ext4_grpblk_t max;
633 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500634 unsigned short border;
635
Valerie Clementb73fce62008-02-15 13:48:51 -0500636 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500637
638 border = 2 << sb->s_blocksize_bits;
639
640 while (len > 0) {
641 /* find how many blocks can be covered since this position */
642 max = ffs(first | border) - 1;
643
644 /* find how many blocks of power 2 we need to mark */
645 min = fls(len) - 1;
646
647 if (max < min)
648 min = max;
649 chunk = 1 << min;
650
651 /* mark multiblock chunks only */
652 grp->bb_counters[min]++;
653 if (min > 0)
654 mb_clear_bit(first >> min,
655 buddy + sbi->s_mb_offsets[min]);
656
657 len -= chunk;
658 first += chunk;
659 }
660}
661
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400662/*
663 * Cache the order of the largest free extent we have available in this block
664 * group.
665 */
666static void
667mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
668{
669 int i;
670 int bits;
671
672 grp->bb_largest_free_order = -1; /* uninit */
673
674 bits = sb->s_blocksize_bits + 1;
675 for (i = bits; i >= 0; i--) {
676 if (grp->bb_counters[i] > 0) {
677 grp->bb_largest_free_order = i;
678 break;
679 }
680 }
681}
682
Eric Sandeen089ceec2009-07-05 22:17:31 -0400683static noinline_for_stack
684void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500685 void *buddy, void *bitmap, ext4_group_t group)
686{
687 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400688 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
689 ext4_grpblk_t i = 0;
690 ext4_grpblk_t first;
691 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500692 unsigned free = 0;
693 unsigned fragments = 0;
694 unsigned long long period = get_cycles();
695
696 /* initialize buddy from bitmap which is aggregation
697 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500698 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500699 grp->bb_first_free = i;
700 while (i < max) {
701 fragments++;
702 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500703 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500704 len = i - first;
705 free += len;
706 if (len > 1)
707 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
708 else
709 grp->bb_counters[0]++;
710 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500711 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500712 }
713 grp->bb_fragments = fragments;
714
715 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400716 ext4_grp_locked_error(sb, group, 0, 0,
717 "%u blocks in bitmap, %u in gd",
718 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500719 /*
720 * If we intent to continue, we consider group descritor
721 * corrupt and update bb_free using bitmap value
722 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500723 grp->bb_free = free;
724 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400725 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500726
727 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
728
729 period = get_cycles() - period;
730 spin_lock(&EXT4_SB(sb)->s_bal_lock);
731 EXT4_SB(sb)->s_mb_buddies_generated++;
732 EXT4_SB(sb)->s_mb_generation_time += period;
733 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
734}
735
736/* The buddy information is attached the buddy cache inode
737 * for convenience. The information regarding each group
738 * is loaded via ext4_mb_load_buddy. The information involve
739 * block bitmap and buddy information. The information are
740 * stored in the inode as
741 *
742 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500743 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500744 *
745 *
746 * one block each for bitmap and buddy information.
747 * So for each group we take up 2 blocks. A page can
748 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
749 * So it can have information regarding groups_per_page which
750 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400751 *
752 * Locking note: This routine takes the block group lock of all groups
753 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500754 */
755
756static int ext4_mb_init_cache(struct page *page, char *incore)
757{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400758 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500759 int blocksize;
760 int blocks_per_page;
761 int groups_per_page;
762 int err = 0;
763 int i;
764 ext4_group_t first_group;
765 int first_block;
766 struct super_block *sb;
767 struct buffer_head *bhs;
768 struct buffer_head **bh;
769 struct inode *inode;
770 char *data;
771 char *bitmap;
772
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400773 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500774
775 inode = page->mapping->host;
776 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400777 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500778 blocksize = 1 << inode->i_blkbits;
779 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
780
781 groups_per_page = blocks_per_page >> 1;
782 if (groups_per_page == 0)
783 groups_per_page = 1;
784
785 /* allocate buffer_heads to read bitmaps */
786 if (groups_per_page > 1) {
787 err = -ENOMEM;
788 i = sizeof(struct buffer_head *) * groups_per_page;
789 bh = kzalloc(i, GFP_NOFS);
790 if (bh == NULL)
791 goto out;
792 } else
793 bh = &bhs;
794
795 first_group = page->index * blocks_per_page / 2;
796
797 /* read all groups the page covers into the cache */
798 for (i = 0; i < groups_per_page; i++) {
799 struct ext4_group_desc *desc;
800
Theodore Ts'o8df96752009-05-01 08:50:38 -0400801 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500802 break;
803
804 err = -EIO;
805 desc = ext4_get_group_desc(sb, first_group + i, NULL);
806 if (desc == NULL)
807 goto out;
808
809 err = -ENOMEM;
810 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
811 if (bh[i] == NULL)
812 goto out;
813
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500814 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500815 continue;
816
Frederic Bohec806e682008-10-10 08:09:18 -0400817 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500818 if (bitmap_uptodate(bh[i])) {
819 unlock_buffer(bh[i]);
820 continue;
821 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400822 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
824 ext4_init_block_bitmap(sb, bh[i],
825 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500826 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500827 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400828 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500829 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500830 continue;
831 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400832 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500833 if (buffer_uptodate(bh[i])) {
834 /*
835 * if not uninit if bh is uptodate,
836 * bitmap is also uptodate
837 */
838 set_bitmap_uptodate(bh[i]);
839 unlock_buffer(bh[i]);
840 continue;
841 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500842 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500843 /*
844 * submit the buffer_head for read. We can
845 * safely mark the bitmap as uptodate now.
846 * We do it here so the bitmap uptodate bit
847 * get set with buffer lock held.
848 */
849 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500850 bh[i]->b_end_io = end_buffer_read_sync;
851 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400852 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500853 }
854
855 /* wait for I/O completion */
856 for (i = 0; i < groups_per_page && bh[i]; i++)
857 wait_on_buffer(bh[i]);
858
859 err = -EIO;
860 for (i = 0; i < groups_per_page && bh[i]; i++)
861 if (!buffer_uptodate(bh[i]))
862 goto out;
863
Mingming Cao31b481d2008-07-11 19:27:31 -0400864 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500865 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500866 /* init the page */
867 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500868 for (i = 0; i < blocks_per_page; i++) {
869 int group;
870 struct ext4_group_info *grinfo;
871
872 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400873 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500874 break;
875
876 /*
877 * data carry information regarding this
878 * particular group in the format specified
879 * above
880 *
881 */
882 data = page_address(page) + (i * blocksize);
883 bitmap = bh[group - first_group]->b_data;
884
885 /*
886 * We place the buddy block and bitmap block
887 * close together
888 */
889 if ((first_block + i) & 1) {
890 /* this is block of buddy */
891 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400892 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500893 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400894 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500895 grinfo = ext4_get_group_info(sb, group);
896 grinfo->bb_fragments = 0;
897 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400898 sizeof(*grinfo->bb_counters) *
899 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500900 /*
901 * incore got set to the group block bitmap below
902 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500903 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500904 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500905 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500906 incore = NULL;
907 } else {
908 /* this is block of bitmap */
909 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400910 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500911 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400912 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500913
914 /* see comments in ext4_mb_put_pa() */
915 ext4_lock_group(sb, group);
916 memcpy(data, bitmap, blocksize);
917
918 /* mark all preallocated blks used in in-core bitmap */
919 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500920 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500921 ext4_unlock_group(sb, group);
922
923 /* set incore so that the buddy information can be
924 * generated using this
925 */
926 incore = data;
927 }
928 }
929 SetPageUptodate(page);
930
931out:
932 if (bh) {
933 for (i = 0; i < groups_per_page && bh[i]; i++)
934 brelse(bh[i]);
935 if (bh != &bhs)
936 kfree(bh);
937 }
938 return err;
939}
940
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400941/*
942 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
943 * block group lock of all groups for this page; do not hold the BG lock when
944 * calling this routine!
945 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400946static noinline_for_stack
947int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
948{
949
950 int ret = 0;
951 void *bitmap;
952 int blocks_per_page;
953 int block, pnum, poff;
954 int num_grp_locked = 0;
955 struct ext4_group_info *this_grp;
956 struct ext4_sb_info *sbi = EXT4_SB(sb);
957 struct inode *inode = sbi->s_buddy_cache;
958 struct page *page = NULL, *bitmap_page = NULL;
959
960 mb_debug(1, "init group %u\n", group);
961 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
962 this_grp = ext4_get_group_info(sb, group);
963 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400964 * This ensures that we don't reinit the buddy cache
965 * page which map to the group from which we are already
966 * allocating. If we are looking at the buddy cache we would
967 * have taken a reference using ext4_mb_load_buddy and that
968 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400969 */
970 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
971 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
972 /*
973 * somebody initialized the group
974 * return without doing anything
975 */
976 ret = 0;
977 goto err;
978 }
979 /*
980 * the buddy cache inode stores the block bitmap
981 * and buddy information in consecutive blocks.
982 * So for each group we need two blocks.
983 */
984 block = group * 2;
985 pnum = block / blocks_per_page;
986 poff = block % blocks_per_page;
987 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
988 if (page) {
989 BUG_ON(page->mapping != inode->i_mapping);
990 ret = ext4_mb_init_cache(page, NULL);
991 if (ret) {
992 unlock_page(page);
993 goto err;
994 }
995 unlock_page(page);
996 }
997 if (page == NULL || !PageUptodate(page)) {
998 ret = -EIO;
999 goto err;
1000 }
1001 mark_page_accessed(page);
1002 bitmap_page = page;
1003 bitmap = page_address(page) + (poff * sb->s_blocksize);
1004
1005 /* init buddy cache */
1006 block++;
1007 pnum = block / blocks_per_page;
1008 poff = block % blocks_per_page;
1009 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1010 if (page == bitmap_page) {
1011 /*
1012 * If both the bitmap and buddy are in
1013 * the same page we don't need to force
1014 * init the buddy
1015 */
1016 unlock_page(page);
1017 } else if (page) {
1018 BUG_ON(page->mapping != inode->i_mapping);
1019 ret = ext4_mb_init_cache(page, bitmap);
1020 if (ret) {
1021 unlock_page(page);
1022 goto err;
1023 }
1024 unlock_page(page);
1025 }
1026 if (page == NULL || !PageUptodate(page)) {
1027 ret = -EIO;
1028 goto err;
1029 }
1030 mark_page_accessed(page);
1031err:
1032 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1033 if (bitmap_page)
1034 page_cache_release(bitmap_page);
1035 if (page)
1036 page_cache_release(page);
1037 return ret;
1038}
1039
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001040/*
1041 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1042 * block group lock of all groups for this page; do not hold the BG lock when
1043 * calling this routine!
1044 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001045static noinline_for_stack int
1046ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1047 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001048{
Alex Tomasc9de5602008-01-29 00:19:52 -05001049 int blocks_per_page;
1050 int block;
1051 int pnum;
1052 int poff;
1053 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001054 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001055 struct ext4_group_info *grp;
1056 struct ext4_sb_info *sbi = EXT4_SB(sb);
1057 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001058
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001059 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001060
1061 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001062 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001063
1064 e4b->bd_blkbits = sb->s_blocksize_bits;
1065 e4b->bd_info = ext4_get_group_info(sb, group);
1066 e4b->bd_sb = sb;
1067 e4b->bd_group = group;
1068 e4b->bd_buddy_page = NULL;
1069 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001070 e4b->alloc_semp = &grp->alloc_sem;
1071
1072 /* Take the read lock on the group alloc
1073 * sem. This would make sure a parallel
1074 * ext4_mb_init_group happening on other
1075 * groups mapped by the page is blocked
1076 * till we are done with allocation
1077 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001078repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001079 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001080
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001081 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1082 /* we need to check for group need init flag
1083 * with alloc_semp held so that we can be sure
1084 * that new blocks didn't get added to the group
1085 * when we are loading the buddy cache
1086 */
1087 up_read(e4b->alloc_semp);
1088 /*
1089 * we need full data about the group
1090 * to make a good selection
1091 */
1092 ret = ext4_mb_init_group(sb, group);
1093 if (ret)
1094 return ret;
1095 goto repeat_load_buddy;
1096 }
1097
Alex Tomasc9de5602008-01-29 00:19:52 -05001098 /*
1099 * the buddy cache inode stores the block bitmap
1100 * and buddy information in consecutive blocks.
1101 * So for each group we need two blocks.
1102 */
1103 block = group * 2;
1104 pnum = block / blocks_per_page;
1105 poff = block % blocks_per_page;
1106
1107 /* we could use find_or_create_page(), but it locks page
1108 * what we'd like to avoid in fast path ... */
1109 page = find_get_page(inode->i_mapping, pnum);
1110 if (page == NULL || !PageUptodate(page)) {
1111 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001112 /*
1113 * drop the page reference and try
1114 * to get the page with lock. If we
1115 * are not uptodate that implies
1116 * somebody just created the page but
1117 * is yet to initialize the same. So
1118 * wait for it to initialize.
1119 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001120 page_cache_release(page);
1121 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1122 if (page) {
1123 BUG_ON(page->mapping != inode->i_mapping);
1124 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001125 ret = ext4_mb_init_cache(page, NULL);
1126 if (ret) {
1127 unlock_page(page);
1128 goto err;
1129 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001130 mb_cmp_bitmaps(e4b, page_address(page) +
1131 (poff * sb->s_blocksize));
1132 }
1133 unlock_page(page);
1134 }
1135 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001136 if (page == NULL || !PageUptodate(page)) {
1137 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001138 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001139 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001140 e4b->bd_bitmap_page = page;
1141 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1142 mark_page_accessed(page);
1143
1144 block++;
1145 pnum = block / blocks_per_page;
1146 poff = block % blocks_per_page;
1147
1148 page = find_get_page(inode->i_mapping, pnum);
1149 if (page == NULL || !PageUptodate(page)) {
1150 if (page)
1151 page_cache_release(page);
1152 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1153 if (page) {
1154 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001155 if (!PageUptodate(page)) {
1156 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1157 if (ret) {
1158 unlock_page(page);
1159 goto err;
1160 }
1161 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001162 unlock_page(page);
1163 }
1164 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001165 if (page == NULL || !PageUptodate(page)) {
1166 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001167 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001168 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001169 e4b->bd_buddy_page = page;
1170 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1171 mark_page_accessed(page);
1172
1173 BUG_ON(e4b->bd_bitmap_page == NULL);
1174 BUG_ON(e4b->bd_buddy_page == NULL);
1175
1176 return 0;
1177
1178err:
1179 if (e4b->bd_bitmap_page)
1180 page_cache_release(e4b->bd_bitmap_page);
1181 if (e4b->bd_buddy_page)
1182 page_cache_release(e4b->bd_buddy_page);
1183 e4b->bd_buddy = NULL;
1184 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001185
1186 /* Done with the buddy cache */
1187 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001188 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001189}
1190
Jing Zhange39e07f2010-05-14 00:00:00 -04001191static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001192{
1193 if (e4b->bd_bitmap_page)
1194 page_cache_release(e4b->bd_bitmap_page);
1195 if (e4b->bd_buddy_page)
1196 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001197 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001198 if (e4b->alloc_semp)
1199 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001200}
1201
1202
1203static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1204{
1205 int order = 1;
1206 void *bb;
1207
1208 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1209 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1210
1211 bb = EXT4_MB_BUDDY(e4b);
1212 while (order <= e4b->bd_blkbits + 1) {
1213 block = block >> 1;
1214 if (!mb_test_bit(block, bb)) {
1215 /* this block is part of buddy of order 'order' */
1216 return order;
1217 }
1218 bb += 1 << (e4b->bd_blkbits - order);
1219 order++;
1220 }
1221 return 0;
1222}
1223
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001224static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001225{
1226 __u32 *addr;
1227
1228 len = cur + len;
1229 while (cur < len) {
1230 if ((cur & 31) == 0 && (len - cur) >= 32) {
1231 /* fast path: clear whole word at once */
1232 addr = bm + (cur >> 3);
1233 *addr = 0;
1234 cur += 32;
1235 continue;
1236 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001237 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001238 cur++;
1239 }
1240}
1241
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001242static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001243{
1244 __u32 *addr;
1245
1246 len = cur + len;
1247 while (cur < len) {
1248 if ((cur & 31) == 0 && (len - cur) >= 32) {
1249 /* fast path: set whole word at once */
1250 addr = bm + (cur >> 3);
1251 *addr = 0xffffffff;
1252 cur += 32;
1253 continue;
1254 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001255 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001256 cur++;
1257 }
1258}
1259
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001260static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001261 int first, int count)
1262{
1263 int block = 0;
1264 int max = 0;
1265 int order;
1266 void *buddy;
1267 void *buddy2;
1268 struct super_block *sb = e4b->bd_sb;
1269
1270 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001271 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001272 mb_check_buddy(e4b);
1273 mb_free_blocks_double(inode, e4b, first, count);
1274
1275 e4b->bd_info->bb_free += count;
1276 if (first < e4b->bd_info->bb_first_free)
1277 e4b->bd_info->bb_first_free = first;
1278
1279 /* let's maintain fragments counter */
1280 if (first != 0)
1281 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1282 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1283 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1284 if (block && max)
1285 e4b->bd_info->bb_fragments--;
1286 else if (!block && !max)
1287 e4b->bd_info->bb_fragments++;
1288
1289 /* let's maintain buddy itself */
1290 while (count-- > 0) {
1291 block = first++;
1292 order = 0;
1293
1294 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1295 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001296
1297 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001298 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001299 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001300 inode ? inode->i_ino : 0,
1301 blocknr,
1302 "freeing already freed block "
1303 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001304 }
1305 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1306 e4b->bd_info->bb_counters[order]++;
1307
1308 /* start of the buddy */
1309 buddy = mb_find_buddy(e4b, order, &max);
1310
1311 do {
1312 block &= ~1UL;
1313 if (mb_test_bit(block, buddy) ||
1314 mb_test_bit(block + 1, buddy))
1315 break;
1316
1317 /* both the buddies are free, try to coalesce them */
1318 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1319
1320 if (!buddy2)
1321 break;
1322
1323 if (order > 0) {
1324 /* for special purposes, we don't set
1325 * free bits in bitmap */
1326 mb_set_bit(block, buddy);
1327 mb_set_bit(block + 1, buddy);
1328 }
1329 e4b->bd_info->bb_counters[order]--;
1330 e4b->bd_info->bb_counters[order]--;
1331
1332 block = block >> 1;
1333 order++;
1334 e4b->bd_info->bb_counters[order]++;
1335
1336 mb_clear_bit(block, buddy2);
1337 buddy = buddy2;
1338 } while (1);
1339 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001340 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001341 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001342}
1343
1344static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1345 int needed, struct ext4_free_extent *ex)
1346{
1347 int next = block;
1348 int max;
1349 int ord;
1350 void *buddy;
1351
Vincent Minetbc8e6742009-05-15 08:33:18 -04001352 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001353 BUG_ON(ex == NULL);
1354
1355 buddy = mb_find_buddy(e4b, order, &max);
1356 BUG_ON(buddy == NULL);
1357 BUG_ON(block >= max);
1358 if (mb_test_bit(block, buddy)) {
1359 ex->fe_len = 0;
1360 ex->fe_start = 0;
1361 ex->fe_group = 0;
1362 return 0;
1363 }
1364
1365 /* FIXME dorp order completely ? */
1366 if (likely(order == 0)) {
1367 /* find actual order */
1368 order = mb_find_order_for_block(e4b, block);
1369 block = block >> order;
1370 }
1371
1372 ex->fe_len = 1 << order;
1373 ex->fe_start = block << order;
1374 ex->fe_group = e4b->bd_group;
1375
1376 /* calc difference from given start */
1377 next = next - ex->fe_start;
1378 ex->fe_len -= next;
1379 ex->fe_start += next;
1380
1381 while (needed > ex->fe_len &&
1382 (buddy = mb_find_buddy(e4b, order, &max))) {
1383
1384 if (block + 1 >= max)
1385 break;
1386
1387 next = (block + 1) * (1 << order);
1388 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1389 break;
1390
1391 ord = mb_find_order_for_block(e4b, next);
1392
1393 order = ord;
1394 block = next >> order;
1395 ex->fe_len += 1 << order;
1396 }
1397
1398 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1399 return ex->fe_len;
1400}
1401
1402static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1403{
1404 int ord;
1405 int mlen = 0;
1406 int max = 0;
1407 int cur;
1408 int start = ex->fe_start;
1409 int len = ex->fe_len;
1410 unsigned ret = 0;
1411 int len0 = len;
1412 void *buddy;
1413
1414 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1415 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001416 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001417 mb_check_buddy(e4b);
1418 mb_mark_used_double(e4b, start, len);
1419
1420 e4b->bd_info->bb_free -= len;
1421 if (e4b->bd_info->bb_first_free == start)
1422 e4b->bd_info->bb_first_free += len;
1423
1424 /* let's maintain fragments counter */
1425 if (start != 0)
1426 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1427 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1428 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1429 if (mlen && max)
1430 e4b->bd_info->bb_fragments++;
1431 else if (!mlen && !max)
1432 e4b->bd_info->bb_fragments--;
1433
1434 /* let's maintain buddy itself */
1435 while (len) {
1436 ord = mb_find_order_for_block(e4b, start);
1437
1438 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1439 /* the whole chunk may be allocated at once! */
1440 mlen = 1 << ord;
1441 buddy = mb_find_buddy(e4b, ord, &max);
1442 BUG_ON((start >> ord) >= max);
1443 mb_set_bit(start >> ord, buddy);
1444 e4b->bd_info->bb_counters[ord]--;
1445 start += mlen;
1446 len -= mlen;
1447 BUG_ON(len < 0);
1448 continue;
1449 }
1450
1451 /* store for history */
1452 if (ret == 0)
1453 ret = len | (ord << 16);
1454
1455 /* we have to split large buddy */
1456 BUG_ON(ord <= 0);
1457 buddy = mb_find_buddy(e4b, ord, &max);
1458 mb_set_bit(start >> ord, buddy);
1459 e4b->bd_info->bb_counters[ord]--;
1460
1461 ord--;
1462 cur = (start >> ord) & ~1U;
1463 buddy = mb_find_buddy(e4b, ord, &max);
1464 mb_clear_bit(cur, buddy);
1465 mb_clear_bit(cur + 1, buddy);
1466 e4b->bd_info->bb_counters[ord]++;
1467 e4b->bd_info->bb_counters[ord]++;
1468 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001469 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001470
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001471 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001472 mb_check_buddy(e4b);
1473
1474 return ret;
1475}
1476
1477/*
1478 * Must be called under group lock!
1479 */
1480static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1481 struct ext4_buddy *e4b)
1482{
1483 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1484 int ret;
1485
1486 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1487 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1488
1489 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1490 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1491 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1492
1493 /* preallocation can change ac_b_ex, thus we store actually
1494 * allocated blocks for history */
1495 ac->ac_f_ex = ac->ac_b_ex;
1496
1497 ac->ac_status = AC_STATUS_FOUND;
1498 ac->ac_tail = ret & 0xffff;
1499 ac->ac_buddy = ret >> 16;
1500
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001501 /*
1502 * take the page reference. We want the page to be pinned
1503 * so that we don't get a ext4_mb_init_cache_call for this
1504 * group until we update the bitmap. That would mean we
1505 * double allocate blocks. The reference is dropped
1506 * in ext4_mb_release_context
1507 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001508 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1509 get_page(ac->ac_bitmap_page);
1510 ac->ac_buddy_page = e4b->bd_buddy_page;
1511 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001512 /* on allocation we use ac to track the held semaphore */
1513 ac->alloc_semp = e4b->alloc_semp;
1514 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001515 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001516 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001517 spin_lock(&sbi->s_md_lock);
1518 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1519 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1520 spin_unlock(&sbi->s_md_lock);
1521 }
1522}
1523
1524/*
1525 * regular allocator, for general purposes allocation
1526 */
1527
1528static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1529 struct ext4_buddy *e4b,
1530 int finish_group)
1531{
1532 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1533 struct ext4_free_extent *bex = &ac->ac_b_ex;
1534 struct ext4_free_extent *gex = &ac->ac_g_ex;
1535 struct ext4_free_extent ex;
1536 int max;
1537
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001538 if (ac->ac_status == AC_STATUS_FOUND)
1539 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001540 /*
1541 * We don't want to scan for a whole year
1542 */
1543 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1544 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1545 ac->ac_status = AC_STATUS_BREAK;
1546 return;
1547 }
1548
1549 /*
1550 * Haven't found good chunk so far, let's continue
1551 */
1552 if (bex->fe_len < gex->fe_len)
1553 return;
1554
1555 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1556 && bex->fe_group == e4b->bd_group) {
1557 /* recheck chunk's availability - we don't know
1558 * when it was found (within this lock-unlock
1559 * period or not) */
1560 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1561 if (max >= gex->fe_len) {
1562 ext4_mb_use_best_found(ac, e4b);
1563 return;
1564 }
1565 }
1566}
1567
1568/*
1569 * The routine checks whether found extent is good enough. If it is,
1570 * then the extent gets marked used and flag is set to the context
1571 * to stop scanning. Otherwise, the extent is compared with the
1572 * previous found extent and if new one is better, then it's stored
1573 * in the context. Later, the best found extent will be used, if
1574 * mballoc can't find good enough extent.
1575 *
1576 * FIXME: real allocation policy is to be designed yet!
1577 */
1578static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1579 struct ext4_free_extent *ex,
1580 struct ext4_buddy *e4b)
1581{
1582 struct ext4_free_extent *bex = &ac->ac_b_ex;
1583 struct ext4_free_extent *gex = &ac->ac_g_ex;
1584
1585 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001586 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001587 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1588 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1589
1590 ac->ac_found++;
1591
1592 /*
1593 * The special case - take what you catch first
1594 */
1595 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1596 *bex = *ex;
1597 ext4_mb_use_best_found(ac, e4b);
1598 return;
1599 }
1600
1601 /*
1602 * Let's check whether the chuck is good enough
1603 */
1604 if (ex->fe_len == gex->fe_len) {
1605 *bex = *ex;
1606 ext4_mb_use_best_found(ac, e4b);
1607 return;
1608 }
1609
1610 /*
1611 * If this is first found extent, just store it in the context
1612 */
1613 if (bex->fe_len == 0) {
1614 *bex = *ex;
1615 return;
1616 }
1617
1618 /*
1619 * If new found extent is better, store it in the context
1620 */
1621 if (bex->fe_len < gex->fe_len) {
1622 /* if the request isn't satisfied, any found extent
1623 * larger than previous best one is better */
1624 if (ex->fe_len > bex->fe_len)
1625 *bex = *ex;
1626 } else if (ex->fe_len > gex->fe_len) {
1627 /* if the request is satisfied, then we try to find
1628 * an extent that still satisfy the request, but is
1629 * smaller than previous one */
1630 if (ex->fe_len < bex->fe_len)
1631 *bex = *ex;
1632 }
1633
1634 ext4_mb_check_limits(ac, e4b, 0);
1635}
1636
Eric Sandeen089ceec2009-07-05 22:17:31 -04001637static noinline_for_stack
1638int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001639 struct ext4_buddy *e4b)
1640{
1641 struct ext4_free_extent ex = ac->ac_b_ex;
1642 ext4_group_t group = ex.fe_group;
1643 int max;
1644 int err;
1645
1646 BUG_ON(ex.fe_len <= 0);
1647 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1648 if (err)
1649 return err;
1650
1651 ext4_lock_group(ac->ac_sb, group);
1652 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1653
1654 if (max > 0) {
1655 ac->ac_b_ex = ex;
1656 ext4_mb_use_best_found(ac, e4b);
1657 }
1658
1659 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001660 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001661
1662 return 0;
1663}
1664
Eric Sandeen089ceec2009-07-05 22:17:31 -04001665static noinline_for_stack
1666int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001667 struct ext4_buddy *e4b)
1668{
1669 ext4_group_t group = ac->ac_g_ex.fe_group;
1670 int max;
1671 int err;
1672 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001673 struct ext4_free_extent ex;
1674
1675 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1676 return 0;
1677
1678 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1679 if (err)
1680 return err;
1681
1682 ext4_lock_group(ac->ac_sb, group);
1683 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1684 ac->ac_g_ex.fe_len, &ex);
1685
1686 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1687 ext4_fsblk_t start;
1688
Akinobu Mita5661bd62010-03-03 23:53:39 -05001689 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1690 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001691 /* use do_div to get remainder (would be 64-bit modulo) */
1692 if (do_div(start, sbi->s_stripe) == 0) {
1693 ac->ac_found++;
1694 ac->ac_b_ex = ex;
1695 ext4_mb_use_best_found(ac, e4b);
1696 }
1697 } else if (max >= ac->ac_g_ex.fe_len) {
1698 BUG_ON(ex.fe_len <= 0);
1699 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1700 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1701 ac->ac_found++;
1702 ac->ac_b_ex = ex;
1703 ext4_mb_use_best_found(ac, e4b);
1704 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1705 /* Sometimes, caller may want to merge even small
1706 * number of blocks to an existing extent */
1707 BUG_ON(ex.fe_len <= 0);
1708 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1709 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1710 ac->ac_found++;
1711 ac->ac_b_ex = ex;
1712 ext4_mb_use_best_found(ac, e4b);
1713 }
1714 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001715 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001716
1717 return 0;
1718}
1719
1720/*
1721 * The routine scans buddy structures (not bitmap!) from given order
1722 * to max order and tries to find big enough chunk to satisfy the req
1723 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001724static noinline_for_stack
1725void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001726 struct ext4_buddy *e4b)
1727{
1728 struct super_block *sb = ac->ac_sb;
1729 struct ext4_group_info *grp = e4b->bd_info;
1730 void *buddy;
1731 int i;
1732 int k;
1733 int max;
1734
1735 BUG_ON(ac->ac_2order <= 0);
1736 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1737 if (grp->bb_counters[i] == 0)
1738 continue;
1739
1740 buddy = mb_find_buddy(e4b, i, &max);
1741 BUG_ON(buddy == NULL);
1742
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001743 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001744 BUG_ON(k >= max);
1745
1746 ac->ac_found++;
1747
1748 ac->ac_b_ex.fe_len = 1 << i;
1749 ac->ac_b_ex.fe_start = k << i;
1750 ac->ac_b_ex.fe_group = e4b->bd_group;
1751
1752 ext4_mb_use_best_found(ac, e4b);
1753
1754 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1755
1756 if (EXT4_SB(sb)->s_mb_stats)
1757 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1758
1759 break;
1760 }
1761}
1762
1763/*
1764 * The routine scans the group and measures all found extents.
1765 * In order to optimize scanning, caller must pass number of
1766 * free blocks in the group, so the routine can know upper limit.
1767 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001768static noinline_for_stack
1769void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001770 struct ext4_buddy *e4b)
1771{
1772 struct super_block *sb = ac->ac_sb;
1773 void *bitmap = EXT4_MB_BITMAP(e4b);
1774 struct ext4_free_extent ex;
1775 int i;
1776 int free;
1777
1778 free = e4b->bd_info->bb_free;
1779 BUG_ON(free <= 0);
1780
1781 i = e4b->bd_info->bb_first_free;
1782
1783 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001784 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001785 EXT4_BLOCKS_PER_GROUP(sb), i);
1786 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001787 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001788 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001789 * free blocks even though group info says we
1790 * we have free blocks
1791 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001792 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1793 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001794 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001795 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001796 break;
1797 }
1798
1799 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1800 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001801 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001802 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1803 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001804 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001805 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001806 /*
1807 * The number of free blocks differs. This mostly
1808 * indicate that the bitmap is corrupt. So exit
1809 * without claiming the space.
1810 */
1811 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001812 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001813
1814 ext4_mb_measure_extent(ac, &ex, e4b);
1815
1816 i += ex.fe_len;
1817 free -= ex.fe_len;
1818 }
1819
1820 ext4_mb_check_limits(ac, e4b, 1);
1821}
1822
1823/*
1824 * This is a special case for storages like raid5
1825 * we try to find stripe-aligned chunks for stripe-size requests
1826 * XXX should do so at least for multiples of stripe size as well
1827 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001828static noinline_for_stack
1829void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001830 struct ext4_buddy *e4b)
1831{
1832 struct super_block *sb = ac->ac_sb;
1833 struct ext4_sb_info *sbi = EXT4_SB(sb);
1834 void *bitmap = EXT4_MB_BITMAP(e4b);
1835 struct ext4_free_extent ex;
1836 ext4_fsblk_t first_group_block;
1837 ext4_fsblk_t a;
1838 ext4_grpblk_t i;
1839 int max;
1840
1841 BUG_ON(sbi->s_stripe == 0);
1842
1843 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001844 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1845
Alex Tomasc9de5602008-01-29 00:19:52 -05001846 a = first_group_block + sbi->s_stripe - 1;
1847 do_div(a, sbi->s_stripe);
1848 i = (a * sbi->s_stripe) - first_group_block;
1849
1850 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1851 if (!mb_test_bit(i, bitmap)) {
1852 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1853 if (max >= sbi->s_stripe) {
1854 ac->ac_found++;
1855 ac->ac_b_ex = ex;
1856 ext4_mb_use_best_found(ac, e4b);
1857 break;
1858 }
1859 }
1860 i += sbi->s_stripe;
1861 }
1862}
1863
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001864/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001865static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1866 ext4_group_t group, int cr)
1867{
1868 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001869 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001870 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1871
1872 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001873
1874 /* We only do this if the grp has never been initialized */
1875 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1876 int ret = ext4_mb_init_group(ac->ac_sb, group);
1877 if (ret)
1878 return 0;
1879 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001880
1881 free = grp->bb_free;
1882 fragments = grp->bb_fragments;
1883 if (free == 0)
1884 return 0;
1885 if (fragments == 0)
1886 return 0;
1887
1888 switch (cr) {
1889 case 0:
1890 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001891
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001892 if (grp->bb_largest_free_order < ac->ac_2order)
1893 return 0;
1894
Theodore Ts'oa4912122009-03-12 12:18:34 -04001895 /* Avoid using the first bg of a flexgroup for data files */
1896 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1897 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1898 ((group % flex_size) == 0))
1899 return 0;
1900
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001901 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001902 case 1:
1903 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1904 return 1;
1905 break;
1906 case 2:
1907 if (free >= ac->ac_g_ex.fe_len)
1908 return 1;
1909 break;
1910 case 3:
1911 return 1;
1912 default:
1913 BUG();
1914 }
1915
1916 return 0;
1917}
1918
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001919/*
1920 * lock the group_info alloc_sem of all the groups
1921 * belonging to the same buddy cache page. This
1922 * make sure other parallel operation on the buddy
1923 * cache doesn't happen whild holding the buddy cache
1924 * lock
1925 */
1926int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1927{
1928 int i;
1929 int block, pnum;
1930 int blocks_per_page;
1931 int groups_per_page;
Theodore Ts'o8df96752009-05-01 08:50:38 -04001932 ext4_group_t ngroups = ext4_get_groups_count(sb);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001933 ext4_group_t first_group;
1934 struct ext4_group_info *grp;
1935
1936 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1937 /*
1938 * the buddy cache inode stores the block bitmap
1939 * and buddy information in consecutive blocks.
1940 * So for each group we need two blocks.
1941 */
1942 block = group * 2;
1943 pnum = block / blocks_per_page;
1944 first_group = pnum * blocks_per_page / 2;
1945
1946 groups_per_page = blocks_per_page >> 1;
1947 if (groups_per_page == 0)
1948 groups_per_page = 1;
1949 /* read all groups the page covers into the cache */
1950 for (i = 0; i < groups_per_page; i++) {
1951
Theodore Ts'o8df96752009-05-01 08:50:38 -04001952 if ((first_group + i) >= ngroups)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001953 break;
1954 grp = ext4_get_group_info(sb, first_group + i);
1955 /* take all groups write allocation
1956 * semaphore. This make sure there is
1957 * no block allocation going on in any
1958 * of that groups
1959 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001960 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001961 }
1962 return i;
1963}
1964
1965void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1966 ext4_group_t group, int locked_group)
1967{
1968 int i;
1969 int block, pnum;
1970 int blocks_per_page;
1971 ext4_group_t first_group;
1972 struct ext4_group_info *grp;
1973
1974 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1975 /*
1976 * the buddy cache inode stores the block bitmap
1977 * and buddy information in consecutive blocks.
1978 * So for each group we need two blocks.
1979 */
1980 block = group * 2;
1981 pnum = block / blocks_per_page;
1982 first_group = pnum * blocks_per_page / 2;
1983 /* release locks on all the groups */
1984 for (i = 0; i < locked_group; i++) {
1985
1986 grp = ext4_get_group_info(sb, first_group + i);
1987 /* take all groups write allocation
1988 * semaphore. This make sure there is
1989 * no block allocation going on in any
1990 * of that groups
1991 */
1992 up_write(&grp->alloc_sem);
1993 }
1994
1995}
1996
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001997static noinline_for_stack int
1998ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001999{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002000 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002001 int cr;
2002 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002003 struct ext4_sb_info *sbi;
2004 struct super_block *sb;
2005 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002006
2007 sb = ac->ac_sb;
2008 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002009 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002010 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002011 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002012 ngroups = sbi->s_blockfile_groups;
2013
Alex Tomasc9de5602008-01-29 00:19:52 -05002014 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2015
2016 /* first, try the goal */
2017 err = ext4_mb_find_by_goal(ac, &e4b);
2018 if (err || ac->ac_status == AC_STATUS_FOUND)
2019 goto out;
2020
2021 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2022 goto out;
2023
2024 /*
2025 * ac->ac2_order is set only if the fe_len is a power of 2
2026 * if ac2_order is set we also set criteria to 0 so that we
2027 * try exact allocation using buddy.
2028 */
2029 i = fls(ac->ac_g_ex.fe_len);
2030 ac->ac_2order = 0;
2031 /*
2032 * We search using buddy data only if the order of the request
2033 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002034 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002035 */
2036 if (i >= sbi->s_mb_order2_reqs) {
2037 /*
2038 * This should tell if fe_len is exactly power of 2
2039 */
2040 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2041 ac->ac_2order = i - 1;
2042 }
2043
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002044 /* if stream allocation is enabled, use global goal */
2045 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002046 /* TBD: may be hot point */
2047 spin_lock(&sbi->s_md_lock);
2048 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2049 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2050 spin_unlock(&sbi->s_md_lock);
2051 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002052
Alex Tomasc9de5602008-01-29 00:19:52 -05002053 /* Let's just scan groups to find more-less suitable blocks */
2054 cr = ac->ac_2order ? 0 : 1;
2055 /*
2056 * cr == 0 try to get exact allocation,
2057 * cr == 3 try to get anything
2058 */
2059repeat:
2060 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2061 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002062 /*
2063 * searching for the right group start
2064 * from the goal value specified
2065 */
2066 group = ac->ac_g_ex.fe_group;
2067
Theodore Ts'o8df96752009-05-01 08:50:38 -04002068 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002069 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002070 group = 0;
2071
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002072 /* This now checks without needing the buddy page */
2073 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002074 continue;
2075
Alex Tomasc9de5602008-01-29 00:19:52 -05002076 err = ext4_mb_load_buddy(sb, group, &e4b);
2077 if (err)
2078 goto out;
2079
2080 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002081
2082 /*
2083 * We need to check again after locking the
2084 * block group
2085 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002086 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002087 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002088 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002089 continue;
2090 }
2091
2092 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002093 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002094 ext4_mb_simple_scan_group(ac, &e4b);
2095 else if (cr == 1 &&
2096 ac->ac_g_ex.fe_len == sbi->s_stripe)
2097 ext4_mb_scan_aligned(ac, &e4b);
2098 else
2099 ext4_mb_complex_scan_group(ac, &e4b);
2100
2101 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002102 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002103
2104 if (ac->ac_status != AC_STATUS_CONTINUE)
2105 break;
2106 }
2107 }
2108
2109 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2110 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2111 /*
2112 * We've been searching too long. Let's try to allocate
2113 * the best chunk we've found so far
2114 */
2115
2116 ext4_mb_try_best_found(ac, &e4b);
2117 if (ac->ac_status != AC_STATUS_FOUND) {
2118 /*
2119 * Someone more lucky has already allocated it.
2120 * The only thing we can do is just take first
2121 * found block(s)
2122 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2123 */
2124 ac->ac_b_ex.fe_group = 0;
2125 ac->ac_b_ex.fe_start = 0;
2126 ac->ac_b_ex.fe_len = 0;
2127 ac->ac_status = AC_STATUS_CONTINUE;
2128 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2129 cr = 3;
2130 atomic_inc(&sbi->s_mb_lost_chunks);
2131 goto repeat;
2132 }
2133 }
2134out:
2135 return err;
2136}
2137
Alex Tomasc9de5602008-01-29 00:19:52 -05002138static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2139{
2140 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002141 ext4_group_t group;
2142
Theodore Ts'o8df96752009-05-01 08:50:38 -04002143 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002144 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002145 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002146 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002147}
2148
2149static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2150{
2151 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002152 ext4_group_t group;
2153
2154 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002155 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002156 return NULL;
2157 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002158 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002159}
2160
2161static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2162{
2163 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002164 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002165 int i;
2166 int err;
2167 struct ext4_buddy e4b;
2168 struct sg {
2169 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002170 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002171 } sg;
2172
2173 group--;
2174 if (group == 0)
2175 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2176 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2177 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2178 "group", "free", "frags", "first",
2179 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2180 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2181
2182 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2183 sizeof(struct ext4_group_info);
2184 err = ext4_mb_load_buddy(sb, group, &e4b);
2185 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002186 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002187 return 0;
2188 }
2189 ext4_lock_group(sb, group);
2190 memcpy(&sg, ext4_get_group_info(sb, group), i);
2191 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002192 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002193
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002194 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002195 sg.info.bb_fragments, sg.info.bb_first_free);
2196 for (i = 0; i <= 13; i++)
2197 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2198 sg.info.bb_counters[i] : 0);
2199 seq_printf(seq, " ]\n");
2200
2201 return 0;
2202}
2203
2204static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2205{
2206}
2207
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002208static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002209 .start = ext4_mb_seq_groups_start,
2210 .next = ext4_mb_seq_groups_next,
2211 .stop = ext4_mb_seq_groups_stop,
2212 .show = ext4_mb_seq_groups_show,
2213};
2214
2215static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2216{
2217 struct super_block *sb = PDE(inode)->data;
2218 int rc;
2219
2220 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2221 if (rc == 0) {
2222 struct seq_file *m = (struct seq_file *)file->private_data;
2223 m->private = sb;
2224 }
2225 return rc;
2226
2227}
2228
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002229static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002230 .owner = THIS_MODULE,
2231 .open = ext4_mb_seq_groups_open,
2232 .read = seq_read,
2233 .llseek = seq_lseek,
2234 .release = seq_release,
2235};
2236
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002237
2238/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002239int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002240 struct ext4_group_desc *desc)
2241{
2242 int i, len;
2243 int metalen = 0;
2244 struct ext4_sb_info *sbi = EXT4_SB(sb);
2245 struct ext4_group_info **meta_group_info;
2246
2247 /*
2248 * First check if this group is the first of a reserved block.
2249 * If it's true, we have to allocate a new table of pointers
2250 * to ext4_group_info structures
2251 */
2252 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2253 metalen = sizeof(*meta_group_info) <<
2254 EXT4_DESC_PER_BLOCK_BITS(sb);
2255 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2256 if (meta_group_info == NULL) {
2257 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2258 "buddy group\n");
2259 goto exit_meta_group_info;
2260 }
2261 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2262 meta_group_info;
2263 }
2264
2265 /*
2266 * calculate needed size. if change bb_counters size,
2267 * don't forget about ext4_mb_generate_buddy()
2268 */
2269 len = offsetof(typeof(**meta_group_info),
2270 bb_counters[sb->s_blocksize_bits + 2]);
2271
2272 meta_group_info =
2273 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2274 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2275
2276 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2277 if (meta_group_info[i] == NULL) {
2278 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2279 goto exit_group_info;
2280 }
2281 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2282 &(meta_group_info[i]->bb_state));
2283
2284 /*
2285 * initialize bb_free to be able to skip
2286 * empty groups without initialization
2287 */
2288 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2289 meta_group_info[i]->bb_free =
2290 ext4_free_blocks_after_init(sb, group, desc);
2291 } else {
2292 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002293 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002294 }
2295
2296 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002297 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002298 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002299 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002300
2301#ifdef DOUBLE_CHECK
2302 {
2303 struct buffer_head *bh;
2304 meta_group_info[i]->bb_bitmap =
2305 kmalloc(sb->s_blocksize, GFP_KERNEL);
2306 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2307 bh = ext4_read_block_bitmap(sb, group);
2308 BUG_ON(bh == NULL);
2309 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2310 sb->s_blocksize);
2311 put_bh(bh);
2312 }
2313#endif
2314
2315 return 0;
2316
2317exit_group_info:
2318 /* If a meta_group_info table has been allocated, release it now */
2319 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2320 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2321exit_meta_group_info:
2322 return -ENOMEM;
2323} /* ext4_mb_add_groupinfo */
2324
Alex Tomasc9de5602008-01-29 00:19:52 -05002325static int ext4_mb_init_backend(struct super_block *sb)
2326{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002327 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002328 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002329 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002330 struct ext4_super_block *es = sbi->s_es;
2331 int num_meta_group_infos;
2332 int num_meta_group_infos_max;
2333 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002334 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002335
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002336 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002337 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002338 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2339
2340 /*
2341 * This is the total number of blocks used by GDT including
2342 * the number of reserved blocks for GDT.
2343 * The s_group_info array is allocated with this value
2344 * to allow a clean online resize without a complex
2345 * manipulation of pointer.
2346 * The drawback is the unused memory when no resize
2347 * occurs but it's very low in terms of pages
2348 * (see comments below)
2349 * Need to handle this properly when META_BG resizing is allowed
2350 */
2351 num_meta_group_infos_max = num_meta_group_infos +
2352 le16_to_cpu(es->s_reserved_gdt_blocks);
2353
2354 /*
2355 * array_size is the size of s_group_info array. We round it
2356 * to the next power of two because this approximation is done
2357 * internally by kmalloc so we can have some more memory
2358 * for free here (e.g. may be used for META_BG resize).
2359 */
2360 array_size = 1;
2361 while (array_size < sizeof(*sbi->s_group_info) *
2362 num_meta_group_infos_max)
2363 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002364 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2365 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2366 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002367 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002368 if (sbi->s_group_info == NULL) {
2369 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2370 return -ENOMEM;
2371 }
2372 sbi->s_buddy_cache = new_inode(sb);
2373 if (sbi->s_buddy_cache == NULL) {
2374 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2375 goto err_freesgi;
2376 }
2377 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002378 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002379 desc = ext4_get_group_desc(sb, i, NULL);
2380 if (desc == NULL) {
2381 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002382 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002383 goto err_freebuddy;
2384 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002385 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2386 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002387 }
2388
2389 return 0;
2390
2391err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002392 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002393 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002394 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002395 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002396 kfree(sbi->s_group_info[i]);
2397 iput(sbi->s_buddy_cache);
2398err_freesgi:
2399 kfree(sbi->s_group_info);
2400 return -ENOMEM;
2401}
2402
2403int ext4_mb_init(struct super_block *sb, int needs_recovery)
2404{
2405 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002406 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002407 unsigned offset;
2408 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002409 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002410
Eric Sandeen19278052009-08-25 22:36:25 -04002411 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002412
2413 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2414 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002415 return -ENOMEM;
2416 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002417
Eric Sandeen19278052009-08-25 22:36:25 -04002418 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002419 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2420 if (sbi->s_mb_maxs == NULL) {
Dan Carpentera7b19442009-03-27 19:42:54 -04002421 kfree(sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002422 return -ENOMEM;
2423 }
2424
2425 /* order 0 is regular bitmap */
2426 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2427 sbi->s_mb_offsets[0] = 0;
2428
2429 i = 1;
2430 offset = 0;
2431 max = sb->s_blocksize << 2;
2432 do {
2433 sbi->s_mb_offsets[i] = offset;
2434 sbi->s_mb_maxs[i] = max;
2435 offset += 1 << (sb->s_blocksize_bits - i);
2436 max = max >> 1;
2437 i++;
2438 } while (i <= sb->s_blocksize_bits + 1);
2439
2440 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002441 ret = ext4_mb_init_backend(sb);
2442 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002443 kfree(sbi->s_mb_offsets);
2444 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002445 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002446 }
2447
2448 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002449 spin_lock_init(&sbi->s_bal_lock);
2450
2451 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2452 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2453 sbi->s_mb_stats = MB_DEFAULT_STATS;
2454 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2455 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002456 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2457
Eric Sandeen730c2132008-09-13 15:23:29 -04002458 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002459 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002460 kfree(sbi->s_mb_offsets);
2461 kfree(sbi->s_mb_maxs);
2462 return -ENOMEM;
2463 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002464 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002465 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002466 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002467 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002468 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2469 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002470 spin_lock_init(&lg->lg_prealloc_lock);
2471 }
2472
Theodore Ts'o296c3552009-09-30 00:32:42 -04002473 if (sbi->s_proc)
2474 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2475 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002476
Frank Mayhar03901312009-01-07 00:06:22 -05002477 if (sbi->s_journal)
2478 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 return 0;
2480}
2481
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002482/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002483static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2484{
2485 struct ext4_prealloc_space *pa;
2486 struct list_head *cur, *tmp;
2487 int count = 0;
2488
2489 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2490 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2491 list_del(&pa->pa_group_list);
2492 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002493 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002494 }
2495 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002496 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002497
2498}
2499
2500int ext4_mb_release(struct super_block *sb)
2501{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002502 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002503 ext4_group_t i;
2504 int num_meta_group_infos;
2505 struct ext4_group_info *grinfo;
2506 struct ext4_sb_info *sbi = EXT4_SB(sb);
2507
Alex Tomasc9de5602008-01-29 00:19:52 -05002508 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002509 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002510 grinfo = ext4_get_group_info(sb, i);
2511#ifdef DOUBLE_CHECK
2512 kfree(grinfo->bb_bitmap);
2513#endif
2514 ext4_lock_group(sb, i);
2515 ext4_mb_cleanup_pa(grinfo);
2516 ext4_unlock_group(sb, i);
2517 kfree(grinfo);
2518 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002519 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002520 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2521 EXT4_DESC_PER_BLOCK_BITS(sb);
2522 for (i = 0; i < num_meta_group_infos; i++)
2523 kfree(sbi->s_group_info[i]);
2524 kfree(sbi->s_group_info);
2525 }
2526 kfree(sbi->s_mb_offsets);
2527 kfree(sbi->s_mb_maxs);
2528 if (sbi->s_buddy_cache)
2529 iput(sbi->s_buddy_cache);
2530 if (sbi->s_mb_stats) {
2531 printk(KERN_INFO
2532 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2533 atomic_read(&sbi->s_bal_allocated),
2534 atomic_read(&sbi->s_bal_reqs),
2535 atomic_read(&sbi->s_bal_success));
2536 printk(KERN_INFO
2537 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2538 "%u 2^N hits, %u breaks, %u lost\n",
2539 atomic_read(&sbi->s_bal_ex_scanned),
2540 atomic_read(&sbi->s_bal_goals),
2541 atomic_read(&sbi->s_bal_2orders),
2542 atomic_read(&sbi->s_bal_breaks),
2543 atomic_read(&sbi->s_mb_lost_chunks));
2544 printk(KERN_INFO
2545 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2546 sbi->s_mb_buddies_generated++,
2547 sbi->s_mb_generation_time);
2548 printk(KERN_INFO
2549 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2550 atomic_read(&sbi->s_mb_preallocated),
2551 atomic_read(&sbi->s_mb_discarded));
2552 }
2553
Eric Sandeen730c2132008-09-13 15:23:29 -04002554 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002555 if (sbi->s_proc)
2556 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002557
2558 return 0;
2559}
2560
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002561/*
2562 * This function is called by the jbd2 layer once the commit has finished,
2563 * so we know we can free the blocks that were released with that commit.
2564 */
2565static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002566{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002567 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002568 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002569 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002570 int err, count = 0, count2 = 0;
2571 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002572 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002573
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002574 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2575 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002576
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002577 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002578 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002579
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002580 if (test_opt(sb, DISCARD)) {
Eric Sandeena30eec22010-05-16 03:00:00 -04002581 int ret;
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002582 ext4_fsblk_t discard_block;
2583
2584 discard_block = entry->start_blk +
2585 ext4_group_first_block_no(sb, entry->group);
2586 trace_ext4_discard_blocks(sb,
2587 (unsigned long long)discard_block,
2588 entry->count);
Eric Sandeena30eec22010-05-16 03:00:00 -04002589 ret = sb_issue_discard(sb, discard_block, entry->count);
2590 if (ret == EOPNOTSUPP) {
2591 ext4_warning(sb,
2592 "discard not supported, disabling");
2593 clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
2594 }
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002595 }
2596
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002597 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002598 /* we expect to find existing buddy because it's pinned */
2599 BUG_ON(err != 0);
2600
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002601 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002602 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002603 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002604 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002605 ext4_lock_group(sb, entry->group);
2606 /* Take it out of per group rb tree */
2607 rb_erase(&entry->node, &(db->bb_free_root));
2608 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2609
2610 if (!db->bb_free_root.rb_node) {
2611 /* No more items in the per group rb tree
2612 * balance refcounts from ext4_mb_free_metadata()
2613 */
2614 page_cache_release(e4b.bd_buddy_page);
2615 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002616 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002617 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002618 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002619 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002620 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002621
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002622 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002623}
2624
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002625#ifdef CONFIG_EXT4_DEBUG
2626u8 mb_enable_debug __read_mostly;
2627
2628static struct dentry *debugfs_dir;
2629static struct dentry *debugfs_debug;
2630
2631static void __init ext4_create_debugfs_entry(void)
2632{
2633 debugfs_dir = debugfs_create_dir("ext4", NULL);
2634 if (debugfs_dir)
2635 debugfs_debug = debugfs_create_u8("mballoc-debug",
2636 S_IRUGO | S_IWUSR,
2637 debugfs_dir,
2638 &mb_enable_debug);
2639}
2640
2641static void ext4_remove_debugfs_entry(void)
2642{
2643 debugfs_remove(debugfs_debug);
2644 debugfs_remove(debugfs_dir);
2645}
2646
2647#else
2648
2649static void __init ext4_create_debugfs_entry(void)
2650{
2651}
2652
2653static void ext4_remove_debugfs_entry(void)
2654{
2655}
2656
2657#endif
2658
Alex Tomasc9de5602008-01-29 00:19:52 -05002659int __init init_ext4_mballoc(void)
2660{
2661 ext4_pspace_cachep =
2662 kmem_cache_create("ext4_prealloc_space",
2663 sizeof(struct ext4_prealloc_space),
2664 0, SLAB_RECLAIM_ACCOUNT, NULL);
2665 if (ext4_pspace_cachep == NULL)
2666 return -ENOMEM;
2667
Eric Sandeen256bdb42008-02-10 01:13:33 -05002668 ext4_ac_cachep =
2669 kmem_cache_create("ext4_alloc_context",
2670 sizeof(struct ext4_allocation_context),
2671 0, SLAB_RECLAIM_ACCOUNT, NULL);
2672 if (ext4_ac_cachep == NULL) {
2673 kmem_cache_destroy(ext4_pspace_cachep);
2674 return -ENOMEM;
2675 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002676
2677 ext4_free_ext_cachep =
2678 kmem_cache_create("ext4_free_block_extents",
2679 sizeof(struct ext4_free_data),
2680 0, SLAB_RECLAIM_ACCOUNT, NULL);
2681 if (ext4_free_ext_cachep == NULL) {
2682 kmem_cache_destroy(ext4_pspace_cachep);
2683 kmem_cache_destroy(ext4_ac_cachep);
2684 return -ENOMEM;
2685 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002686 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 return 0;
2688}
2689
2690void exit_ext4_mballoc(void)
2691{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002692 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002693 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2694 * before destroying the slab cache.
2695 */
2696 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002697 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002698 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002699 kmem_cache_destroy(ext4_free_ext_cachep);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002700 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002701}
2702
2703
2704/*
2705 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2706 * Returns 0 if success or error code
2707 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002708static noinline_for_stack int
2709ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002710 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002711{
2712 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002713 struct ext4_group_desc *gdp;
2714 struct buffer_head *gdp_bh;
2715 struct ext4_sb_info *sbi;
2716 struct super_block *sb;
2717 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002718 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002719
2720 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2721 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2722
2723 sb = ac->ac_sb;
2724 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002725
2726 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002727 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002728 if (!bitmap_bh)
2729 goto out_err;
2730
2731 err = ext4_journal_get_write_access(handle, bitmap_bh);
2732 if (err)
2733 goto out_err;
2734
2735 err = -EIO;
2736 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2737 if (!gdp)
2738 goto out_err;
2739
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002740 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002741 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002742
Alex Tomasc9de5602008-01-29 00:19:52 -05002743 err = ext4_journal_get_write_access(handle, gdp_bh);
2744 if (err)
2745 goto out_err;
2746
Akinobu Mitabda00de2010-03-03 23:53:25 -05002747 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002748
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002749 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002750 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002751 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002752 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002753 /* File system mounted not to panic on error
2754 * Fix the bitmap and repeat the block allocation
2755 * We leak some of the blocks here.
2756 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002757 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2758 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2759 ac->ac_b_ex.fe_len);
2760 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002761 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002762 if (!err)
2763 err = -EAGAIN;
2764 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002765 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002766
2767 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002768#ifdef AGGRESSIVE_CHECK
2769 {
2770 int i;
2771 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2772 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2773 bitmap_bh->b_data));
2774 }
2775 }
2776#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002777 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002778 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2779 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002780 ext4_free_blks_set(sb, gdp,
2781 ext4_free_blocks_after_init(sb,
2782 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002783 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002784 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2785 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002786 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002787
2788 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002789 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002790 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002791 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002792 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002793 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2794 /* release all the reserved blocks if non delalloc */
2795 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002796
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002797 if (sbi->s_log_groups_per_flex) {
2798 ext4_group_t flex_group = ext4_flex_group(sbi,
2799 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002800 atomic_sub(ac->ac_b_ex.fe_len,
2801 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002802 }
2803
Frank Mayhar03901312009-01-07 00:06:22 -05002804 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002805 if (err)
2806 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002807 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002808
2809out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002810 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002811 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002812 return err;
2813}
2814
2815/*
2816 * here we normalize request for locality group
2817 * Group request are normalized to s_strip size if we set the same via mount
2818 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002819 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002820 *
2821 * XXX: should we try to preallocate more than the group has now?
2822 */
2823static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2824{
2825 struct super_block *sb = ac->ac_sb;
2826 struct ext4_locality_group *lg = ac->ac_lg;
2827
2828 BUG_ON(lg == NULL);
2829 if (EXT4_SB(sb)->s_stripe)
2830 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2831 else
2832 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002833 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002834 current->pid, ac->ac_g_ex.fe_len);
2835}
2836
2837/*
2838 * Normalization means making request better in terms of
2839 * size and alignment
2840 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002841static noinline_for_stack void
2842ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002843 struct ext4_allocation_request *ar)
2844{
2845 int bsbits, max;
2846 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002847 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002848 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002849 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002850 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002851
2852 /* do normalize only data requests, metadata requests
2853 do not need preallocation */
2854 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2855 return;
2856
2857 /* sometime caller may want exact blocks */
2858 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2859 return;
2860
2861 /* caller may indicate that preallocation isn't
2862 * required (it's a tail, for example) */
2863 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2864 return;
2865
2866 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2867 ext4_mb_normalize_group_request(ac);
2868 return ;
2869 }
2870
2871 bsbits = ac->ac_sb->s_blocksize_bits;
2872
2873 /* first, let's learn actual file size
2874 * given current request is allocated */
2875 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2876 size = size << bsbits;
2877 if (size < i_size_read(ac->ac_inode))
2878 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002879 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002880
Valerie Clement19304792008-05-13 19:31:14 -04002881 /* max size of free chunks */
2882 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002883
Valerie Clement19304792008-05-13 19:31:14 -04002884#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2885 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002886
2887 /* first, try to predict filesize */
2888 /* XXX: should this table be tunable? */
2889 start_off = 0;
2890 if (size <= 16 * 1024) {
2891 size = 16 * 1024;
2892 } else if (size <= 32 * 1024) {
2893 size = 32 * 1024;
2894 } else if (size <= 64 * 1024) {
2895 size = 64 * 1024;
2896 } else if (size <= 128 * 1024) {
2897 size = 128 * 1024;
2898 } else if (size <= 256 * 1024) {
2899 size = 256 * 1024;
2900 } else if (size <= 512 * 1024) {
2901 size = 512 * 1024;
2902 } else if (size <= 1024 * 1024) {
2903 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002904 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002905 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002906 (21 - bsbits)) << 21;
2907 size = 2 * 1024 * 1024;
2908 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002909 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2910 (22 - bsbits)) << 22;
2911 size = 4 * 1024 * 1024;
2912 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002913 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002914 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2915 (23 - bsbits)) << 23;
2916 size = 8 * 1024 * 1024;
2917 } else {
2918 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2919 size = ac->ac_o_ex.fe_len << bsbits;
2920 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002921 size = size >> bsbits;
2922 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002923
2924 /* don't cover already allocated blocks in selected range */
2925 if (ar->pleft && start <= ar->lleft) {
2926 size -= ar->lleft + 1 - start;
2927 start = ar->lleft + 1;
2928 }
2929 if (ar->pright && start + size - 1 >= ar->lright)
2930 size -= start + size - ar->lright;
2931
2932 end = start + size;
2933
2934 /* check we don't cross already preallocated blocks */
2935 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002936 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002937 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002938
Alex Tomasc9de5602008-01-29 00:19:52 -05002939 if (pa->pa_deleted)
2940 continue;
2941 spin_lock(&pa->pa_lock);
2942 if (pa->pa_deleted) {
2943 spin_unlock(&pa->pa_lock);
2944 continue;
2945 }
2946
2947 pa_end = pa->pa_lstart + pa->pa_len;
2948
2949 /* PA must not overlap original request */
2950 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2951 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2952
Eric Sandeen38877f42009-08-17 23:55:24 -04002953 /* skip PAs this normalized request doesn't overlap with */
2954 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002955 spin_unlock(&pa->pa_lock);
2956 continue;
2957 }
2958 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2959
Eric Sandeen38877f42009-08-17 23:55:24 -04002960 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002961 if (pa_end <= ac->ac_o_ex.fe_logical) {
2962 BUG_ON(pa_end < start);
2963 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002964 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002965 BUG_ON(pa->pa_lstart > end);
2966 end = pa->pa_lstart;
2967 }
2968 spin_unlock(&pa->pa_lock);
2969 }
2970 rcu_read_unlock();
2971 size = end - start;
2972
2973 /* XXX: extra loop to check we really don't overlap preallocations */
2974 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002975 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002976 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002977 spin_lock(&pa->pa_lock);
2978 if (pa->pa_deleted == 0) {
2979 pa_end = pa->pa_lstart + pa->pa_len;
2980 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2981 }
2982 spin_unlock(&pa->pa_lock);
2983 }
2984 rcu_read_unlock();
2985
2986 if (start + size <= ac->ac_o_ex.fe_logical &&
2987 start > ac->ac_o_ex.fe_logical) {
2988 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
2989 (unsigned long) start, (unsigned long) size,
2990 (unsigned long) ac->ac_o_ex.fe_logical);
2991 }
2992 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
2993 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04002994 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002995
2996 /* now prepare goal request */
2997
2998 /* XXX: is it better to align blocks WRT to logical
2999 * placement or satisfy big request as is */
3000 ac->ac_g_ex.fe_logical = start;
3001 ac->ac_g_ex.fe_len = size;
3002
3003 /* define goal start in order to merge */
3004 if (ar->pright && (ar->lright == (start + size))) {
3005 /* merge to the right */
3006 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3007 &ac->ac_f_ex.fe_group,
3008 &ac->ac_f_ex.fe_start);
3009 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3010 }
3011 if (ar->pleft && (ar->lleft + 1 == start)) {
3012 /* merge to the left */
3013 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3014 &ac->ac_f_ex.fe_group,
3015 &ac->ac_f_ex.fe_start);
3016 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3017 }
3018
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003019 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003020 (unsigned) orig_size, (unsigned) start);
3021}
3022
3023static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3024{
3025 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3026
3027 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3028 atomic_inc(&sbi->s_bal_reqs);
3029 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003030 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003031 atomic_inc(&sbi->s_bal_success);
3032 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3033 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3034 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3035 atomic_inc(&sbi->s_bal_goals);
3036 if (ac->ac_found > sbi->s_mb_max_to_scan)
3037 atomic_inc(&sbi->s_bal_breaks);
3038 }
3039
Theodore Ts'o296c3552009-09-30 00:32:42 -04003040 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3041 trace_ext4_mballoc_alloc(ac);
3042 else
3043 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003044}
3045
3046/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003047 * Called on failure; free up any blocks from the inode PA for this
3048 * context. We don't need this for MB_GROUP_PA because we only change
3049 * pa_free in ext4_mb_release_context(), but on failure, we've already
3050 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3051 */
3052static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3053{
3054 struct ext4_prealloc_space *pa = ac->ac_pa;
3055 int len;
3056
3057 if (pa && pa->pa_type == MB_INODE_PA) {
3058 len = ac->ac_b_ex.fe_len;
3059 pa->pa_free += len;
3060 }
3061
3062}
3063
3064/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003065 * use blocks preallocated to inode
3066 */
3067static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3068 struct ext4_prealloc_space *pa)
3069{
3070 ext4_fsblk_t start;
3071 ext4_fsblk_t end;
3072 int len;
3073
3074 /* found preallocated blocks, use them */
3075 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3076 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3077 len = end - start;
3078 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3079 &ac->ac_b_ex.fe_start);
3080 ac->ac_b_ex.fe_len = len;
3081 ac->ac_status = AC_STATUS_FOUND;
3082 ac->ac_pa = pa;
3083
3084 BUG_ON(start < pa->pa_pstart);
3085 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3086 BUG_ON(pa->pa_free < len);
3087 pa->pa_free -= len;
3088
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003089 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003090}
3091
3092/*
3093 * use blocks preallocated to locality group
3094 */
3095static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3096 struct ext4_prealloc_space *pa)
3097{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003098 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003099
Alex Tomasc9de5602008-01-29 00:19:52 -05003100 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3101 &ac->ac_b_ex.fe_group,
3102 &ac->ac_b_ex.fe_start);
3103 ac->ac_b_ex.fe_len = len;
3104 ac->ac_status = AC_STATUS_FOUND;
3105 ac->ac_pa = pa;
3106
3107 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003108 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003109 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003110 * in on-disk bitmap -- see ext4_mb_release_context()
3111 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003112 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003113 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003114}
3115
3116/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003117 * Return the prealloc space that have minimal distance
3118 * from the goal block. @cpa is the prealloc
3119 * space that is having currently known minimal distance
3120 * from the goal block.
3121 */
3122static struct ext4_prealloc_space *
3123ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3124 struct ext4_prealloc_space *pa,
3125 struct ext4_prealloc_space *cpa)
3126{
3127 ext4_fsblk_t cur_distance, new_distance;
3128
3129 if (cpa == NULL) {
3130 atomic_inc(&pa->pa_count);
3131 return pa;
3132 }
3133 cur_distance = abs(goal_block - cpa->pa_pstart);
3134 new_distance = abs(goal_block - pa->pa_pstart);
3135
3136 if (cur_distance < new_distance)
3137 return cpa;
3138
3139 /* drop the previous reference */
3140 atomic_dec(&cpa->pa_count);
3141 atomic_inc(&pa->pa_count);
3142 return pa;
3143}
3144
3145/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003146 * search goal blocks in preallocated space
3147 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003148static noinline_for_stack int
3149ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003150{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003151 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003152 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3153 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003154 struct ext4_prealloc_space *pa, *cpa = NULL;
3155 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003156
3157 /* only data can be preallocated */
3158 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3159 return 0;
3160
3161 /* first, try per-file preallocation */
3162 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003163 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003164
3165 /* all fields in this condition don't change,
3166 * so we can skip locking for them */
3167 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3168 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3169 continue;
3170
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003171 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003172 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003173 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3174 continue;
3175
Alex Tomasc9de5602008-01-29 00:19:52 -05003176 /* found preallocated blocks, use them */
3177 spin_lock(&pa->pa_lock);
3178 if (pa->pa_deleted == 0 && pa->pa_free) {
3179 atomic_inc(&pa->pa_count);
3180 ext4_mb_use_inode_pa(ac, pa);
3181 spin_unlock(&pa->pa_lock);
3182 ac->ac_criteria = 10;
3183 rcu_read_unlock();
3184 return 1;
3185 }
3186 spin_unlock(&pa->pa_lock);
3187 }
3188 rcu_read_unlock();
3189
3190 /* can we use group allocation? */
3191 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3192 return 0;
3193
3194 /* inode may have no locality group for some reason */
3195 lg = ac->ac_lg;
3196 if (lg == NULL)
3197 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003198 order = fls(ac->ac_o_ex.fe_len) - 1;
3199 if (order > PREALLOC_TB_SIZE - 1)
3200 /* The max size of hash table is PREALLOC_TB_SIZE */
3201 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003202
Akinobu Mitabda00de2010-03-03 23:53:25 -05003203 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003204 /*
3205 * search for the prealloc space that is having
3206 * minimal distance from the goal block.
3207 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003208 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3209 rcu_read_lock();
3210 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3211 pa_inode_list) {
3212 spin_lock(&pa->pa_lock);
3213 if (pa->pa_deleted == 0 &&
3214 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003215
3216 cpa = ext4_mb_check_group_pa(goal_block,
3217 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003218 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003219 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003220 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003221 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003222 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003223 if (cpa) {
3224 ext4_mb_use_group_pa(ac, cpa);
3225 ac->ac_criteria = 20;
3226 return 1;
3227 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003228 return 0;
3229}
3230
3231/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003232 * the function goes through all block freed in the group
3233 * but not yet committed and marks them used in in-core bitmap.
3234 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003235 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003236 */
3237static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3238 ext4_group_t group)
3239{
3240 struct rb_node *n;
3241 struct ext4_group_info *grp;
3242 struct ext4_free_data *entry;
3243
3244 grp = ext4_get_group_info(sb, group);
3245 n = rb_first(&(grp->bb_free_root));
3246
3247 while (n) {
3248 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003249 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003250 n = rb_next(n);
3251 }
3252 return;
3253}
3254
3255/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003256 * the function goes through all preallocation in this group and marks them
3257 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003258 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003259 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003260static noinline_for_stack
3261void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003262 ext4_group_t group)
3263{
3264 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3265 struct ext4_prealloc_space *pa;
3266 struct list_head *cur;
3267 ext4_group_t groupnr;
3268 ext4_grpblk_t start;
3269 int preallocated = 0;
3270 int count = 0;
3271 int len;
3272
3273 /* all form of preallocation discards first load group,
3274 * so the only competing code is preallocation use.
3275 * we don't need any locking here
3276 * notice we do NOT ignore preallocations with pa_deleted
3277 * otherwise we could leave used blocks available for
3278 * allocation in buddy when concurrent ext4_mb_put_pa()
3279 * is dropping preallocation
3280 */
3281 list_for_each(cur, &grp->bb_prealloc_list) {
3282 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3283 spin_lock(&pa->pa_lock);
3284 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3285 &groupnr, &start);
3286 len = pa->pa_len;
3287 spin_unlock(&pa->pa_lock);
3288 if (unlikely(len == 0))
3289 continue;
3290 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003291 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003292 preallocated += len;
3293 count++;
3294 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003295 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003296}
3297
3298static void ext4_mb_pa_callback(struct rcu_head *head)
3299{
3300 struct ext4_prealloc_space *pa;
3301 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3302 kmem_cache_free(ext4_pspace_cachep, pa);
3303}
3304
3305/*
3306 * drops a reference to preallocated space descriptor
3307 * if this was the last reference and the space is consumed
3308 */
3309static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3310 struct super_block *sb, struct ext4_prealloc_space *pa)
3311{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003312 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003313 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003314
3315 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3316 return;
3317
3318 /* in this short window concurrent discard can set pa_deleted */
3319 spin_lock(&pa->pa_lock);
3320 if (pa->pa_deleted == 1) {
3321 spin_unlock(&pa->pa_lock);
3322 return;
3323 }
3324
3325 pa->pa_deleted = 1;
3326 spin_unlock(&pa->pa_lock);
3327
Eric Sandeend33a1972009-03-16 23:25:40 -04003328 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003329 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003330 * If doing group-based preallocation, pa_pstart may be in the
3331 * next group when pa is used up
3332 */
3333 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003334 grp_blk--;
3335
3336 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003337
3338 /*
3339 * possible race:
3340 *
3341 * P1 (buddy init) P2 (regular allocation)
3342 * find block B in PA
3343 * copy on-disk bitmap to buddy
3344 * mark B in on-disk bitmap
3345 * drop PA from group
3346 * mark all PAs in buddy
3347 *
3348 * thus, P1 initializes buddy with B available. to prevent this
3349 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3350 * against that pair
3351 */
3352 ext4_lock_group(sb, grp);
3353 list_del(&pa->pa_group_list);
3354 ext4_unlock_group(sb, grp);
3355
3356 spin_lock(pa->pa_obj_lock);
3357 list_del_rcu(&pa->pa_inode_list);
3358 spin_unlock(pa->pa_obj_lock);
3359
3360 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3361}
3362
3363/*
3364 * creates new preallocated space for given inode
3365 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003366static noinline_for_stack int
3367ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003368{
3369 struct super_block *sb = ac->ac_sb;
3370 struct ext4_prealloc_space *pa;
3371 struct ext4_group_info *grp;
3372 struct ext4_inode_info *ei;
3373
3374 /* preallocate only when found space is larger then requested */
3375 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3376 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3377 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3378
3379 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3380 if (pa == NULL)
3381 return -ENOMEM;
3382
3383 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3384 int winl;
3385 int wins;
3386 int win;
3387 int offs;
3388
3389 /* we can't allocate as much as normalizer wants.
3390 * so, found space must get proper lstart
3391 * to cover original request */
3392 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3393 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3394
3395 /* we're limited by original request in that
3396 * logical block must be covered any way
3397 * winl is window we can move our chunk within */
3398 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3399
3400 /* also, we should cover whole original request */
3401 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3402
3403 /* the smallest one defines real window */
3404 win = min(winl, wins);
3405
3406 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3407 if (offs && offs < win)
3408 win = offs;
3409
3410 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3411 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3412 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3413 }
3414
3415 /* preallocation can change ac_b_ex, thus we store actually
3416 * allocated blocks for history */
3417 ac->ac_f_ex = ac->ac_b_ex;
3418
3419 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3420 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3421 pa->pa_len = ac->ac_b_ex.fe_len;
3422 pa->pa_free = pa->pa_len;
3423 atomic_set(&pa->pa_count, 1);
3424 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003425 INIT_LIST_HEAD(&pa->pa_inode_list);
3426 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003427 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003428 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003429
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003430 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003431 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003432 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003433
3434 ext4_mb_use_inode_pa(ac, pa);
3435 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3436
3437 ei = EXT4_I(ac->ac_inode);
3438 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3439
3440 pa->pa_obj_lock = &ei->i_prealloc_lock;
3441 pa->pa_inode = ac->ac_inode;
3442
3443 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3444 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3445 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3446
3447 spin_lock(pa->pa_obj_lock);
3448 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3449 spin_unlock(pa->pa_obj_lock);
3450
3451 return 0;
3452}
3453
3454/*
3455 * creates new preallocated space for locality group inodes belongs to
3456 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003457static noinline_for_stack int
3458ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003459{
3460 struct super_block *sb = ac->ac_sb;
3461 struct ext4_locality_group *lg;
3462 struct ext4_prealloc_space *pa;
3463 struct ext4_group_info *grp;
3464
3465 /* preallocate only when found space is larger then requested */
3466 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3467 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3468 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3469
3470 BUG_ON(ext4_pspace_cachep == NULL);
3471 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3472 if (pa == NULL)
3473 return -ENOMEM;
3474
3475 /* preallocation can change ac_b_ex, thus we store actually
3476 * allocated blocks for history */
3477 ac->ac_f_ex = ac->ac_b_ex;
3478
3479 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3480 pa->pa_lstart = pa->pa_pstart;
3481 pa->pa_len = ac->ac_b_ex.fe_len;
3482 pa->pa_free = pa->pa_len;
3483 atomic_set(&pa->pa_count, 1);
3484 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003485 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003486 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003487 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003488 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003489
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003490 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003491 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3492 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003493
3494 ext4_mb_use_group_pa(ac, pa);
3495 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3496
3497 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3498 lg = ac->ac_lg;
3499 BUG_ON(lg == NULL);
3500
3501 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3502 pa->pa_inode = NULL;
3503
3504 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3505 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3506 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3507
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003508 /*
3509 * We will later add the new pa to the right bucket
3510 * after updating the pa_free in ext4_mb_release_context
3511 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003512 return 0;
3513}
3514
3515static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3516{
3517 int err;
3518
3519 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3520 err = ext4_mb_new_group_pa(ac);
3521 else
3522 err = ext4_mb_new_inode_pa(ac);
3523 return err;
3524}
3525
3526/*
3527 * finds all unused blocks in on-disk bitmap, frees them in
3528 * in-core bitmap and buddy.
3529 * @pa must be unlinked from inode and group lists, so that
3530 * nobody else can find/use it.
3531 * the caller MUST hold group/inode locks.
3532 * TODO: optimize the case when there are no in-core structures yet
3533 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003534static noinline_for_stack int
3535ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003536 struct ext4_prealloc_space *pa,
3537 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003538{
Alex Tomasc9de5602008-01-29 00:19:52 -05003539 struct super_block *sb = e4b->bd_sb;
3540 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003541 unsigned int end;
3542 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003543 ext4_group_t group;
3544 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003545 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003546 int err = 0;
3547 int free = 0;
3548
3549 BUG_ON(pa->pa_deleted == 0);
3550 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003551 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003552 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3553 end = bit + pa->pa_len;
3554
Eric Sandeen256bdb42008-02-10 01:13:33 -05003555 if (ac) {
3556 ac->ac_sb = sb;
3557 ac->ac_inode = pa->pa_inode;
Eric Sandeen256bdb42008-02-10 01:13:33 -05003558 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003559
3560 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003561 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003562 if (bit >= end)
3563 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003564 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003565 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003566 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3567 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 free += next - bit;
3569
Eric Sandeen256bdb42008-02-10 01:13:33 -05003570 if (ac) {
3571 ac->ac_b_ex.fe_group = group;
3572 ac->ac_b_ex.fe_start = bit;
3573 ac->ac_b_ex.fe_len = next - bit;
3574 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003575 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003576 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003577
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003578 trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3579 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003580 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3581 bit = next + 1;
3582 }
3583 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003584 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003585 pa, (unsigned long) pa->pa_lstart,
3586 (unsigned long) pa->pa_pstart,
3587 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003588 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003589 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003590 /*
3591 * pa is already deleted so we use the value obtained
3592 * from the bitmap and continue.
3593 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003594 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003595 atomic_add(free, &sbi->s_mb_discarded);
3596
3597 return err;
3598}
3599
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003600static noinline_for_stack int
3601ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003602 struct ext4_prealloc_space *pa,
3603 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003604{
Alex Tomasc9de5602008-01-29 00:19:52 -05003605 struct super_block *sb = e4b->bd_sb;
3606 ext4_group_t group;
3607 ext4_grpblk_t bit;
3608
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003609 trace_ext4_mb_release_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003610 BUG_ON(pa->pa_deleted == 0);
3611 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3612 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3613 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3614 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3615
Eric Sandeen256bdb42008-02-10 01:13:33 -05003616 if (ac) {
3617 ac->ac_sb = sb;
3618 ac->ac_inode = NULL;
3619 ac->ac_b_ex.fe_group = group;
3620 ac->ac_b_ex.fe_start = bit;
3621 ac->ac_b_ex.fe_len = pa->pa_len;
3622 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003623 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003624 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003625
3626 return 0;
3627}
3628
3629/*
3630 * releases all preallocations in given group
3631 *
3632 * first, we need to decide discard policy:
3633 * - when do we discard
3634 * 1) ENOSPC
3635 * - how many do we discard
3636 * 1) how many requested
3637 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003638static noinline_for_stack int
3639ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003640 ext4_group_t group, int needed)
3641{
3642 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3643 struct buffer_head *bitmap_bh = NULL;
3644 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003645 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003646 struct list_head list;
3647 struct ext4_buddy e4b;
3648 int err;
3649 int busy = 0;
3650 int free = 0;
3651
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003652 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003653
3654 if (list_empty(&grp->bb_prealloc_list))
3655 return 0;
3656
Theodore Ts'o574ca172008-07-11 19:27:31 -04003657 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003658 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003659 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003660 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003661 }
3662
3663 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003664 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003665 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003666 put_bh(bitmap_bh);
3667 return 0;
3668 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003669
3670 if (needed == 0)
3671 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3672
Alex Tomasc9de5602008-01-29 00:19:52 -05003673 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003674 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003675 if (ac)
3676 ac->ac_sb = sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05003677repeat:
3678 ext4_lock_group(sb, group);
3679 list_for_each_entry_safe(pa, tmp,
3680 &grp->bb_prealloc_list, pa_group_list) {
3681 spin_lock(&pa->pa_lock);
3682 if (atomic_read(&pa->pa_count)) {
3683 spin_unlock(&pa->pa_lock);
3684 busy = 1;
3685 continue;
3686 }
3687 if (pa->pa_deleted) {
3688 spin_unlock(&pa->pa_lock);
3689 continue;
3690 }
3691
3692 /* seems this one can be freed ... */
3693 pa->pa_deleted = 1;
3694
3695 /* we can trust pa_free ... */
3696 free += pa->pa_free;
3697
3698 spin_unlock(&pa->pa_lock);
3699
3700 list_del(&pa->pa_group_list);
3701 list_add(&pa->u.pa_tmp_list, &list);
3702 }
3703
3704 /* if we still need more blocks and some PAs were used, try again */
3705 if (free < needed && busy) {
3706 busy = 0;
3707 ext4_unlock_group(sb, group);
3708 /*
3709 * Yield the CPU here so that we don't get soft lockup
3710 * in non preempt case.
3711 */
3712 yield();
3713 goto repeat;
3714 }
3715
3716 /* found anything to free? */
3717 if (list_empty(&list)) {
3718 BUG_ON(free != 0);
3719 goto out;
3720 }
3721
3722 /* now free all selected PAs */
3723 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3724
3725 /* remove from object (inode or locality group) */
3726 spin_lock(pa->pa_obj_lock);
3727 list_del_rcu(&pa->pa_inode_list);
3728 spin_unlock(pa->pa_obj_lock);
3729
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003730 if (pa->pa_type == MB_GROUP_PA)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003731 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003732 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003733 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003734
3735 list_del(&pa->u.pa_tmp_list);
3736 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3737 }
3738
3739out:
3740 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003741 if (ac)
3742 kmem_cache_free(ext4_ac_cachep, ac);
Jing Zhange39e07f2010-05-14 00:00:00 -04003743 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003744 put_bh(bitmap_bh);
3745 return free;
3746}
3747
3748/*
3749 * releases all non-used preallocated blocks for given inode
3750 *
3751 * It's important to discard preallocations under i_data_sem
3752 * We don't want another block to be served from the prealloc
3753 * space when we are discarding the inode prealloc space.
3754 *
3755 * FIXME!! Make sure it is valid at all the call sites
3756 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003757void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003758{
3759 struct ext4_inode_info *ei = EXT4_I(inode);
3760 struct super_block *sb = inode->i_sb;
3761 struct buffer_head *bitmap_bh = NULL;
3762 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003763 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003764 ext4_group_t group = 0;
3765 struct list_head list;
3766 struct ext4_buddy e4b;
3767 int err;
3768
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003769 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003770 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3771 return;
3772 }
3773
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003774 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003775 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003776
3777 INIT_LIST_HEAD(&list);
3778
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003779 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003780 if (ac) {
3781 ac->ac_sb = sb;
3782 ac->ac_inode = inode;
3783 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003784repeat:
3785 /* first, collect all pa's in the inode */
3786 spin_lock(&ei->i_prealloc_lock);
3787 while (!list_empty(&ei->i_prealloc_list)) {
3788 pa = list_entry(ei->i_prealloc_list.next,
3789 struct ext4_prealloc_space, pa_inode_list);
3790 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3791 spin_lock(&pa->pa_lock);
3792 if (atomic_read(&pa->pa_count)) {
3793 /* this shouldn't happen often - nobody should
3794 * use preallocation while we're discarding it */
3795 spin_unlock(&pa->pa_lock);
3796 spin_unlock(&ei->i_prealloc_lock);
3797 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3798 WARN_ON(1);
3799 schedule_timeout_uninterruptible(HZ);
3800 goto repeat;
3801
3802 }
3803 if (pa->pa_deleted == 0) {
3804 pa->pa_deleted = 1;
3805 spin_unlock(&pa->pa_lock);
3806 list_del_rcu(&pa->pa_inode_list);
3807 list_add(&pa->u.pa_tmp_list, &list);
3808 continue;
3809 }
3810
3811 /* someone is deleting pa right now */
3812 spin_unlock(&pa->pa_lock);
3813 spin_unlock(&ei->i_prealloc_lock);
3814
3815 /* we have to wait here because pa_deleted
3816 * doesn't mean pa is already unlinked from
3817 * the list. as we might be called from
3818 * ->clear_inode() the inode will get freed
3819 * and concurrent thread which is unlinking
3820 * pa from inode's list may access already
3821 * freed memory, bad-bad-bad */
3822
3823 /* XXX: if this happens too often, we can
3824 * add a flag to force wait only in case
3825 * of ->clear_inode(), but not in case of
3826 * regular truncate */
3827 schedule_timeout_uninterruptible(HZ);
3828 goto repeat;
3829 }
3830 spin_unlock(&ei->i_prealloc_lock);
3831
3832 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003833 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003834 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3835
3836 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003837 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003838 ext4_error(sb, "Error loading buddy information for %u",
3839 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003840 continue;
3841 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003842
Theodore Ts'o574ca172008-07-11 19:27:31 -04003843 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003844 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003845 ext4_error(sb, "Error reading block bitmap for %u",
3846 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003847 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003848 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003849 }
3850
3851 ext4_lock_group(sb, group);
3852 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003853 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003854 ext4_unlock_group(sb, group);
3855
Jing Zhange39e07f2010-05-14 00:00:00 -04003856 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003857 put_bh(bitmap_bh);
3858
3859 list_del(&pa->u.pa_tmp_list);
3860 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3861 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003862 if (ac)
3863 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003864}
3865
3866/*
3867 * finds all preallocated spaces and return blocks being freed to them
3868 * if preallocated space becomes full (no block is used from the space)
3869 * then the function frees space in buddy
3870 * XXX: at the moment, truncate (which is the only way to free blocks)
3871 * discards all preallocations
3872 */
3873static void ext4_mb_return_to_preallocation(struct inode *inode,
3874 struct ext4_buddy *e4b,
3875 sector_t block, int count)
3876{
3877 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3878}
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003879#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003880static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3881{
3882 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003883 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003884
3885 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3886 " Allocation context details:\n");
3887 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3888 ac->ac_status, ac->ac_flags);
3889 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3890 "best %lu/%lu/%lu@%lu cr %d\n",
3891 (unsigned long)ac->ac_o_ex.fe_group,
3892 (unsigned long)ac->ac_o_ex.fe_start,
3893 (unsigned long)ac->ac_o_ex.fe_len,
3894 (unsigned long)ac->ac_o_ex.fe_logical,
3895 (unsigned long)ac->ac_g_ex.fe_group,
3896 (unsigned long)ac->ac_g_ex.fe_start,
3897 (unsigned long)ac->ac_g_ex.fe_len,
3898 (unsigned long)ac->ac_g_ex.fe_logical,
3899 (unsigned long)ac->ac_b_ex.fe_group,
3900 (unsigned long)ac->ac_b_ex.fe_start,
3901 (unsigned long)ac->ac_b_ex.fe_len,
3902 (unsigned long)ac->ac_b_ex.fe_logical,
3903 (int)ac->ac_criteria);
3904 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3905 ac->ac_found);
3906 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003907 ngroups = ext4_get_groups_count(sb);
3908 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003909 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3910 struct ext4_prealloc_space *pa;
3911 ext4_grpblk_t start;
3912 struct list_head *cur;
3913 ext4_lock_group(sb, i);
3914 list_for_each(cur, &grp->bb_prealloc_list) {
3915 pa = list_entry(cur, struct ext4_prealloc_space,
3916 pa_group_list);
3917 spin_lock(&pa->pa_lock);
3918 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3919 NULL, &start);
3920 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003921 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3922 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003923 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003924 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003925
3926 if (grp->bb_free == 0)
3927 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003928 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003929 i, grp->bb_free, grp->bb_fragments);
3930 }
3931 printk(KERN_ERR "\n");
3932}
3933#else
3934static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3935{
3936 return;
3937}
3938#endif
3939
3940/*
3941 * We use locality group preallocation for small size file. The size of the
3942 * file is determined by the current size or the resulting size after
3943 * allocation which ever is larger
3944 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003945 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003946 */
3947static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3948{
3949 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3950 int bsbits = ac->ac_sb->s_blocksize_bits;
3951 loff_t size, isize;
3952
3953 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3954 return;
3955
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003956 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3957 return;
3958
Alex Tomasc9de5602008-01-29 00:19:52 -05003959 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003960 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3961 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003962
Theodore Ts'o50797482009-09-18 13:34:02 -04003963 if ((size == isize) &&
3964 !ext4_fs_is_busy(sbi) &&
3965 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3966 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3967 return;
3968 }
3969
Alex Tomasc9de5602008-01-29 00:19:52 -05003970 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003971 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003972 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003973 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003974 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003975 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003976
3977 BUG_ON(ac->ac_lg != NULL);
3978 /*
3979 * locality group prealloc space are per cpu. The reason for having
3980 * per cpu locality group is to reduce the contention between block
3981 * request from multiple CPUs.
3982 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003983 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003984
3985 /* we're going to use group allocation */
3986 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3987
3988 /* serialize all allocations in the group */
3989 mutex_lock(&ac->ac_lg->lg_mutex);
3990}
3991
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003992static noinline_for_stack int
3993ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003994 struct ext4_allocation_request *ar)
3995{
3996 struct super_block *sb = ar->inode->i_sb;
3997 struct ext4_sb_info *sbi = EXT4_SB(sb);
3998 struct ext4_super_block *es = sbi->s_es;
3999 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004000 unsigned int len;
4001 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004002 ext4_grpblk_t block;
4003
4004 /* we can't allocate > group size */
4005 len = ar->len;
4006
4007 /* just a dirty hack to filter too big requests */
4008 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4009 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4010
4011 /* start searching from the goal */
4012 goal = ar->goal;
4013 if (goal < le32_to_cpu(es->s_first_data_block) ||
4014 goal >= ext4_blocks_count(es))
4015 goal = le32_to_cpu(es->s_first_data_block);
4016 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4017
4018 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004019 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004020 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004021 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004022 ac->ac_sb = sb;
4023 ac->ac_inode = ar->inode;
4024 ac->ac_o_ex.fe_logical = ar->logical;
4025 ac->ac_o_ex.fe_group = group;
4026 ac->ac_o_ex.fe_start = block;
4027 ac->ac_o_ex.fe_len = len;
4028 ac->ac_g_ex.fe_logical = ar->logical;
4029 ac->ac_g_ex.fe_group = group;
4030 ac->ac_g_ex.fe_start = block;
4031 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004032 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004033
4034 /* we have to define context: we'll we work with a file or
4035 * locality group. this is a policy, actually */
4036 ext4_mb_group_or_file(ac);
4037
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004038 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004039 "left: %u/%u, right %u/%u to %swritable\n",
4040 (unsigned) ar->len, (unsigned) ar->logical,
4041 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4042 (unsigned) ar->lleft, (unsigned) ar->pleft,
4043 (unsigned) ar->lright, (unsigned) ar->pright,
4044 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4045 return 0;
4046
4047}
4048
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004049static noinline_for_stack void
4050ext4_mb_discard_lg_preallocations(struct super_block *sb,
4051 struct ext4_locality_group *lg,
4052 int order, int total_entries)
4053{
4054 ext4_group_t group = 0;
4055 struct ext4_buddy e4b;
4056 struct list_head discard_list;
4057 struct ext4_prealloc_space *pa, *tmp;
4058 struct ext4_allocation_context *ac;
4059
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004060 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004061
4062 INIT_LIST_HEAD(&discard_list);
4063 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004064 if (ac)
4065 ac->ac_sb = sb;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004066
4067 spin_lock(&lg->lg_prealloc_lock);
4068 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4069 pa_inode_list) {
4070 spin_lock(&pa->pa_lock);
4071 if (atomic_read(&pa->pa_count)) {
4072 /*
4073 * This is the pa that we just used
4074 * for block allocation. So don't
4075 * free that
4076 */
4077 spin_unlock(&pa->pa_lock);
4078 continue;
4079 }
4080 if (pa->pa_deleted) {
4081 spin_unlock(&pa->pa_lock);
4082 continue;
4083 }
4084 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004085 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004086
4087 /* seems this one can be freed ... */
4088 pa->pa_deleted = 1;
4089 spin_unlock(&pa->pa_lock);
4090
4091 list_del_rcu(&pa->pa_inode_list);
4092 list_add(&pa->u.pa_tmp_list, &discard_list);
4093
4094 total_entries--;
4095 if (total_entries <= 5) {
4096 /*
4097 * we want to keep only 5 entries
4098 * allowing it to grow to 8. This
4099 * mak sure we don't call discard
4100 * soon for this list.
4101 */
4102 break;
4103 }
4104 }
4105 spin_unlock(&lg->lg_prealloc_lock);
4106
4107 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4108
4109 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4110 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004111 ext4_error(sb, "Error loading buddy information for %u",
4112 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004113 continue;
4114 }
4115 ext4_lock_group(sb, group);
4116 list_del(&pa->pa_group_list);
4117 ext4_mb_release_group_pa(&e4b, pa, ac);
4118 ext4_unlock_group(sb, group);
4119
Jing Zhange39e07f2010-05-14 00:00:00 -04004120 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004121 list_del(&pa->u.pa_tmp_list);
4122 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4123 }
4124 if (ac)
4125 kmem_cache_free(ext4_ac_cachep, ac);
4126}
4127
4128/*
4129 * We have incremented pa_count. So it cannot be freed at this
4130 * point. Also we hold lg_mutex. So no parallel allocation is
4131 * possible from this lg. That means pa_free cannot be updated.
4132 *
4133 * A parallel ext4_mb_discard_group_preallocations is possible.
4134 * which can cause the lg_prealloc_list to be updated.
4135 */
4136
4137static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4138{
4139 int order, added = 0, lg_prealloc_count = 1;
4140 struct super_block *sb = ac->ac_sb;
4141 struct ext4_locality_group *lg = ac->ac_lg;
4142 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4143
4144 order = fls(pa->pa_free) - 1;
4145 if (order > PREALLOC_TB_SIZE - 1)
4146 /* The max size of hash table is PREALLOC_TB_SIZE */
4147 order = PREALLOC_TB_SIZE - 1;
4148 /* Add the prealloc space to lg */
4149 rcu_read_lock();
4150 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4151 pa_inode_list) {
4152 spin_lock(&tmp_pa->pa_lock);
4153 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004154 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004155 continue;
4156 }
4157 if (!added && pa->pa_free < tmp_pa->pa_free) {
4158 /* Add to the tail of the previous entry */
4159 list_add_tail_rcu(&pa->pa_inode_list,
4160 &tmp_pa->pa_inode_list);
4161 added = 1;
4162 /*
4163 * we want to count the total
4164 * number of entries in the list
4165 */
4166 }
4167 spin_unlock(&tmp_pa->pa_lock);
4168 lg_prealloc_count++;
4169 }
4170 if (!added)
4171 list_add_tail_rcu(&pa->pa_inode_list,
4172 &lg->lg_prealloc_list[order]);
4173 rcu_read_unlock();
4174
4175 /* Now trim the list to be not more than 8 elements */
4176 if (lg_prealloc_count > 8) {
4177 ext4_mb_discard_lg_preallocations(sb, lg,
4178 order, lg_prealloc_count);
4179 return;
4180 }
4181 return ;
4182}
4183
Alex Tomasc9de5602008-01-29 00:19:52 -05004184/*
4185 * release all resource we used in allocation
4186 */
4187static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4188{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004189 struct ext4_prealloc_space *pa = ac->ac_pa;
4190 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004191 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004192 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004193 spin_lock(&pa->pa_lock);
4194 pa->pa_pstart += ac->ac_b_ex.fe_len;
4195 pa->pa_lstart += ac->ac_b_ex.fe_len;
4196 pa->pa_free -= ac->ac_b_ex.fe_len;
4197 pa->pa_len -= ac->ac_b_ex.fe_len;
4198 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004199 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004200 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004201 if (ac->alloc_semp)
4202 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004203 if (pa) {
4204 /*
4205 * We want to add the pa to the right bucket.
4206 * Remove it from the list and while adding
4207 * make sure the list to which we are adding
4208 * doesn't grow big. We need to release
4209 * alloc_semp before calling ext4_mb_add_n_trim()
4210 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004211 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004212 spin_lock(pa->pa_obj_lock);
4213 list_del_rcu(&pa->pa_inode_list);
4214 spin_unlock(pa->pa_obj_lock);
4215 ext4_mb_add_n_trim(ac);
4216 }
4217 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4218 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004219 if (ac->ac_bitmap_page)
4220 page_cache_release(ac->ac_bitmap_page);
4221 if (ac->ac_buddy_page)
4222 page_cache_release(ac->ac_buddy_page);
4223 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4224 mutex_unlock(&ac->ac_lg->lg_mutex);
4225 ext4_mb_collect_stats(ac);
4226 return 0;
4227}
4228
4229static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4230{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004231 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004232 int ret;
4233 int freed = 0;
4234
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004235 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004236 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004237 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4238 freed += ret;
4239 needed -= ret;
4240 }
4241
4242 return freed;
4243}
4244
4245/*
4246 * Main entry point into mballoc to allocate blocks
4247 * it tries to use preallocation first, then falls back
4248 * to usual allocation
4249 */
4250ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4251 struct ext4_allocation_request *ar, int *errp)
4252{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004253 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004254 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004255 struct ext4_sb_info *sbi;
4256 struct super_block *sb;
4257 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004258 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004259 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004260
4261 sb = ar->inode->i_sb;
4262 sbi = EXT4_SB(sb);
4263
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004264 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004265
Mingming Cao60e58e02009-01-22 18:13:05 +01004266 /*
4267 * For delayed allocation, we could skip the ENOSPC and
4268 * EDQUOT check, as blocks and quotas have been already
4269 * reserved when data being copied into pagecache.
4270 */
4271 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4272 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4273 else {
4274 /* Without delayed allocation we need to verify
4275 * there is enough free blocks to do block allocation
4276 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004277 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004278 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4279 /* let others to free the space */
4280 yield();
4281 ar->len = ar->len >> 1;
4282 }
4283 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004284 *errp = -ENOSPC;
4285 return 0;
4286 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004287 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004288 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004289 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4290 ar->len--;
4291 }
4292 inquota = ar->len;
4293 if (ar->len == 0) {
4294 *errp = -EDQUOT;
4295 goto out3;
4296 }
Mingming Caod2a17632008-07-14 17:52:37 -04004297 }
Mingming Caod2a17632008-07-14 17:52:37 -04004298
Eric Sandeen256bdb42008-02-10 01:13:33 -05004299 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004300 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004301 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004302 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004303 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004304 }
4305
Eric Sandeen256bdb42008-02-10 01:13:33 -05004306 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004307 if (*errp) {
4308 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004309 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004310 }
4311
Eric Sandeen256bdb42008-02-10 01:13:33 -05004312 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4313 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004314 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4315 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004316repeat:
4317 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004318 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004319
4320 /* as we've just preallocated more space than
4321 * user requested orinally, we store allocated
4322 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004323 if (ac->ac_status == AC_STATUS_FOUND &&
4324 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4325 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004326 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004327 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004328 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004329 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004330 /*
4331 * drop the reference that we took
4332 * in ext4_mb_use_best_found
4333 */
4334 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004335 ac->ac_b_ex.fe_group = 0;
4336 ac->ac_b_ex.fe_start = 0;
4337 ac->ac_b_ex.fe_len = 0;
4338 ac->ac_status = AC_STATUS_CONTINUE;
4339 goto repeat;
4340 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004341 ext4_discard_allocated_blocks(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004342 ac->ac_b_ex.fe_len = 0;
4343 ar->len = 0;
4344 ext4_mb_show_ac(ac);
4345 } else {
4346 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4347 ar->len = ac->ac_b_ex.fe_len;
4348 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004349 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004350 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004351 if (freed)
4352 goto repeat;
4353 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004354 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004355 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004356 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004357 }
4358
Eric Sandeen256bdb42008-02-10 01:13:33 -05004359 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004360
Shen Feng363d4252008-07-11 19:27:31 -04004361out2:
4362 kmem_cache_free(ext4_ac_cachep, ac);
4363out1:
Mingming Cao60e58e02009-01-22 18:13:05 +01004364 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004365 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004366out3:
4367 if (!ar->len) {
4368 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4369 /* release all the reserved blocks if non delalloc */
4370 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4371 reserv_blks);
4372 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004373
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004374 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004375
Alex Tomasc9de5602008-01-29 00:19:52 -05004376 return block;
4377}
Alex Tomasc9de5602008-01-29 00:19:52 -05004378
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004379/*
4380 * We can merge two free data extents only if the physical blocks
4381 * are contiguous, AND the extents were freed by the same transaction,
4382 * AND the blocks are associated with the same group.
4383 */
4384static int can_merge(struct ext4_free_data *entry1,
4385 struct ext4_free_data *entry2)
4386{
4387 if ((entry1->t_tid == entry2->t_tid) &&
4388 (entry1->group == entry2->group) &&
4389 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4390 return 1;
4391 return 0;
4392}
4393
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004394static noinline_for_stack int
4395ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004396 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004397{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004398 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004399 ext4_grpblk_t block;
4400 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004401 struct ext4_group_info *db = e4b->bd_info;
4402 struct super_block *sb = e4b->bd_sb;
4403 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004404 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4405 struct rb_node *parent = NULL, *new_node;
4406
Frank Mayhar03901312009-01-07 00:06:22 -05004407 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004408 BUG_ON(e4b->bd_bitmap_page == NULL);
4409 BUG_ON(e4b->bd_buddy_page == NULL);
4410
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004411 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004412 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004413
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004414 if (!*n) {
4415 /* first free block exent. We need to
4416 protect buddy cache from being freed,
4417 * otherwise we'll refresh it from
4418 * on-disk bitmap and lose not-yet-available
4419 * blocks */
4420 page_cache_get(e4b->bd_buddy_page);
4421 page_cache_get(e4b->bd_bitmap_page);
4422 }
4423 while (*n) {
4424 parent = *n;
4425 entry = rb_entry(parent, struct ext4_free_data, node);
4426 if (block < entry->start_blk)
4427 n = &(*n)->rb_left;
4428 else if (block >= (entry->start_blk + entry->count))
4429 n = &(*n)->rb_right;
4430 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004431 ext4_grp_locked_error(sb, group, 0,
4432 ext4_group_first_block_no(sb, group) + block,
4433 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004434 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004435 }
4436 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004437
4438 rb_link_node(new_node, parent, n);
4439 rb_insert_color(new_node, &db->bb_free_root);
4440
4441 /* Now try to see the extent can be merged to left and right */
4442 node = rb_prev(new_node);
4443 if (node) {
4444 entry = rb_entry(node, struct ext4_free_data, node);
4445 if (can_merge(entry, new_entry)) {
4446 new_entry->start_blk = entry->start_blk;
4447 new_entry->count += entry->count;
4448 rb_erase(node, &(db->bb_free_root));
4449 spin_lock(&sbi->s_md_lock);
4450 list_del(&entry->list);
4451 spin_unlock(&sbi->s_md_lock);
4452 kmem_cache_free(ext4_free_ext_cachep, entry);
4453 }
4454 }
4455
4456 node = rb_next(new_node);
4457 if (node) {
4458 entry = rb_entry(node, struct ext4_free_data, node);
4459 if (can_merge(new_entry, entry)) {
4460 new_entry->count += entry->count;
4461 rb_erase(node, &(db->bb_free_root));
4462 spin_lock(&sbi->s_md_lock);
4463 list_del(&entry->list);
4464 spin_unlock(&sbi->s_md_lock);
4465 kmem_cache_free(ext4_free_ext_cachep, entry);
4466 }
4467 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004468 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004469 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004470 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004471 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004472 return 0;
4473}
4474
Theodore Ts'o44338712009-11-22 07:44:56 -05004475/**
4476 * ext4_free_blocks() -- Free given blocks and update quota
4477 * @handle: handle for this transaction
4478 * @inode: inode
4479 * @block: start physical block to free
4480 * @count: number of blocks to count
4481 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004482 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004483void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004484 struct buffer_head *bh, ext4_fsblk_t block,
4485 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004486{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004487 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004488 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004489 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004490 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004491 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004492 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004493 ext4_grpblk_t bit;
4494 struct buffer_head *gd_bh;
4495 ext4_group_t block_group;
4496 struct ext4_sb_info *sbi;
4497 struct ext4_buddy e4b;
4498 int err = 0;
4499 int ret;
4500
Theodore Ts'oe6362602009-11-23 07:17:05 -05004501 if (bh) {
4502 if (block)
4503 BUG_ON(block != bh->b_blocknr);
4504 else
4505 block = bh->b_blocknr;
4506 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004507
Alex Tomasc9de5602008-01-29 00:19:52 -05004508 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004509 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4510 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004511 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004512 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004513 goto error_return;
4514 }
4515
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004516 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004517 trace_ext4_free_blocks(inode, block, count, flags);
4518
4519 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4520 struct buffer_head *tbh = bh;
4521 int i;
4522
4523 BUG_ON(bh && (count > 1));
4524
4525 for (i = 0; i < count; i++) {
4526 if (!bh)
4527 tbh = sb_find_get_block(inode->i_sb,
4528 block + i);
Theodore Ts'o60e66792010-05-17 07:00:00 -04004529 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004530 inode, tbh, block + i);
4531 }
4532 }
4533
Theodore Ts'o60e66792010-05-17 07:00:00 -04004534 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004535 * We need to make sure we don't reuse the freed block until
4536 * after the transaction is committed, which we can do by
4537 * treating the block as metadata, below. We make an
4538 * exception if the inode is to be written in writeback mode
4539 * since writeback mode has weak data consistency guarantees.
4540 */
4541 if (!ext4_should_writeback_data(inode))
4542 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004543
Eric Sandeen256bdb42008-02-10 01:13:33 -05004544 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4545 if (ac) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004546 ac->ac_inode = inode;
4547 ac->ac_sb = sb;
4548 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004549
4550do_more:
4551 overflow = 0;
4552 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4553
4554 /*
4555 * Check to see if we are freeing blocks across a group
4556 * boundary.
4557 */
4558 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4559 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4560 count -= overflow;
4561 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004562 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004563 if (!bitmap_bh) {
4564 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004565 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004566 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004567 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004568 if (!gdp) {
4569 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004570 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004571 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004572
4573 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4574 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4575 in_range(block, ext4_inode_table(sb, gdp),
4576 EXT4_SB(sb)->s_itb_per_group) ||
4577 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4578 EXT4_SB(sb)->s_itb_per_group)) {
4579
Eric Sandeen12062dd2010-02-15 14:19:27 -05004580 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004581 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004582 /* err = 0. ext4_std_error should be a no op */
4583 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 }
4585
4586 BUFFER_TRACE(bitmap_bh, "getting write access");
4587 err = ext4_journal_get_write_access(handle, bitmap_bh);
4588 if (err)
4589 goto error_return;
4590
4591 /*
4592 * We are about to modify some metadata. Call the journal APIs
4593 * to unshare ->b_data if a currently-committing transaction is
4594 * using it
4595 */
4596 BUFFER_TRACE(gd_bh, "get_write_access");
4597 err = ext4_journal_get_write_access(handle, gd_bh);
4598 if (err)
4599 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004600#ifdef AGGRESSIVE_CHECK
4601 {
4602 int i;
4603 for (i = 0; i < count; i++)
4604 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4605 }
4606#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004607 if (ac) {
4608 ac->ac_b_ex.fe_group = block_group;
4609 ac->ac_b_ex.fe_start = bit;
4610 ac->ac_b_ex.fe_len = count;
Theodore Ts'o296c3552009-09-30 00:32:42 -04004611 trace_ext4_mballoc_free(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004612 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004613
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004614 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4615 if (err)
4616 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004617
4618 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004619 struct ext4_free_data *new_entry;
4620 /*
4621 * blocks being freed are metadata. these blocks shouldn't
4622 * be used until this transaction is committed
4623 */
4624 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4625 new_entry->start_blk = bit;
4626 new_entry->group = block_group;
4627 new_entry->count = count;
4628 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004629
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004630 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004631 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004632 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004633 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004634 /* need to update group_info->bb_free and bitmap
4635 * with group lock held. generate_buddy look at
4636 * them with group lock_held
4637 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004638 ext4_lock_group(sb, block_group);
4639 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004640 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004641 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004642 }
4643
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004644 ret = ext4_free_blks_count(sb, gdp) + count;
4645 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004646 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004647 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004648 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4649
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004650 if (sbi->s_log_groups_per_flex) {
4651 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004652 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004653 }
4654
Jing Zhange39e07f2010-05-14 00:00:00 -04004655 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004656
Theodore Ts'o44338712009-11-22 07:44:56 -05004657 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004658
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004659 /* We dirtied the bitmap block */
4660 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4661 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4662
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 /* And the group descriptor block */
4664 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004665 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004666 if (!err)
4667 err = ret;
4668
4669 if (overflow && !err) {
4670 block += count;
4671 count = overflow;
4672 put_bh(bitmap_bh);
4673 goto do_more;
4674 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004675 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004676error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004677 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004678 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004679 brelse(bitmap_bh);
4680 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004681 if (ac)
4682 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004683 return;
4684}