blob: c73d43995b13768e7fd5fc0849fd64936cd636c0 [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>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040026#include <trace/events/ext4.h>
27
Alex Tomasc9de5602008-01-29 00:19:52 -050028/*
29 * MUSTDO:
30 * - test ext4_ext_search_left() and ext4_ext_search_right()
31 * - search for metadata in few groups
32 *
33 * TODO v4:
34 * - normalization should take into account whether file is still open
35 * - discard preallocations if no free space left (policy?)
36 * - don't normalize tails
37 * - quota
38 * - reservation for superuser
39 *
40 * TODO v3:
41 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
42 * - track min/max extents in each group for better group selection
43 * - mb_mark_used() may allocate chunk right after splitting buddy
44 * - tree of groups sorted by number of free blocks
45 * - error handling
46 */
47
48/*
49 * The allocation request involve request for multiple number of blocks
50 * near to the goal(block) value specified.
51 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040052 * During initialization phase of the allocator we decide to use the
53 * group preallocation or inode preallocation depending on the size of
54 * the file. The size of the file could be the resulting file size we
55 * would have after allocation, or the current file size, which ever
56 * is larger. If the size is less than sbi->s_mb_stream_request we
57 * select to use the group preallocation. The default value of
58 * s_mb_stream_request is 16 blocks. This can also be tuned via
59 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
60 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050061 *
62 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040063 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050064 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040065 * First stage the allocator looks at the inode prealloc list,
66 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
67 * spaces for this particular inode. The inode prealloc space is
68 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050069 *
70 * pa_lstart -> the logical start block for this prealloc space
71 * pa_pstart -> the physical start block for this prealloc space
72 * pa_len -> lenght for this prealloc space
73 * pa_free -> free space available in this prealloc space
74 *
75 * The inode preallocation space is used looking at the _logical_ start
76 * block. If only the logical file block falls within the range of prealloc
77 * space we will consume the particular prealloc space. This make sure that
78 * that the we have contiguous physical blocks representing the file blocks
79 *
80 * The important thing to be noted in case of inode prealloc space is that
81 * we don't modify the values associated to inode prealloc space except
82 * pa_free.
83 *
84 * If we are not able to find blocks in the inode prealloc space and if we
85 * have the group allocation flag set then we look at the locality group
86 * prealloc space. These are per CPU prealloc list repreasented as
87 *
88 * ext4_sb_info.s_locality_groups[smp_processor_id()]
89 *
90 * The reason for having a per cpu locality group is to reduce the contention
91 * between CPUs. It is possible to get scheduled at this point.
92 *
93 * The locality group prealloc space is used looking at whether we have
94 * enough free space (pa_free) withing the prealloc space.
95 *
96 * If we can't allocate blocks via inode prealloc or/and locality group
97 * prealloc then we look at the buddy cache. The buddy cache is represented
98 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
99 * mapped to the buddy and bitmap information regarding different
100 * groups. The buddy information is attached to buddy cache inode so that
101 * we can access them through the page cache. The information regarding
102 * each group is loaded via ext4_mb_load_buddy. The information involve
103 * block bitmap and buddy information. The information are stored in the
104 * inode as:
105 *
106 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500107 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500108 *
109 *
110 * one block each for bitmap and buddy information. So for each group we
111 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
112 * blocksize) blocks. So it can have information regarding groups_per_page
113 * which is blocks_per_page/2
114 *
115 * The buddy cache inode is not stored on disk. The inode is thrown
116 * away when the filesystem is unmounted.
117 *
118 * We look for count number of blocks in the buddy cache. If we were able
119 * to locate that many free blocks we return with additional information
120 * regarding rest of the contiguous physical block available
121 *
122 * Before allocating blocks via buddy cache we normalize the request
123 * blocks. This ensure we ask for more blocks that we needed. The extra
124 * blocks that we get after allocation is added to the respective prealloc
125 * list. In case of inode preallocation we follow a list of heuristics
126 * based on file size. This can be found in ext4_mb_normalize_request. If
127 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400128 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500129 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400130 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
133 * stripe value (sbi->s_stripe)
134 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400135 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500136 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400137 * /sys/fs/ext4/<partition>/mb_min_to_scan
138 * /sys/fs/ext4/<partition>/mb_max_to_scan
139 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500140 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400141 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500142 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
143 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400144 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
Alex Tomasc9de5602008-01-29 00:19:52 -0500145 * stripe size (sbi->s_stripe), we try to search for contigous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400146 * stripe size. This should result in better allocation on RAID setups. If
147 * not, we search in the specific group using bitmap for best extents. The
148 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500149 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400150 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500151 * best extent in the found extents. Searching for the blocks starts with
152 * the group specified as the goal value in allocation context via
153 * ac_g_ex. Each group is first checked based on the criteria whether it
154 * can used for allocation. ext4_mb_good_group explains how the groups are
155 * checked.
156 *
157 * Both the prealloc space are getting populated as above. So for the first
158 * request we will hit the buddy cache which will result in this prealloc
159 * space getting filled. The prealloc space is then later used for the
160 * subsequent request.
161 */
162
163/*
164 * mballoc operates on the following data:
165 * - on-disk bitmap
166 * - in-core buddy (actually includes buddy and bitmap)
167 * - preallocation descriptors (PAs)
168 *
169 * there are two types of preallocations:
170 * - inode
171 * assiged to specific inode and can be used for this inode only.
172 * it describes part of inode's space preallocated to specific
173 * physical blocks. any block from that preallocated can be used
174 * independent. the descriptor just tracks number of blocks left
175 * unused. so, before taking some block from descriptor, one must
176 * make sure corresponded logical block isn't allocated yet. this
177 * also means that freeing any block within descriptor's range
178 * must discard all preallocated blocks.
179 * - locality group
180 * assigned to specific locality group which does not translate to
181 * permanent set of inodes: inode can join and leave group. space
182 * from this type of preallocation can be used for any inode. thus
183 * it's consumed from the beginning to the end.
184 *
185 * relation between them can be expressed as:
186 * in-core buddy = on-disk bitmap + preallocation descriptors
187 *
188 * this mean blocks mballoc considers used are:
189 * - allocated blocks (persistent)
190 * - preallocated blocks (non-persistent)
191 *
192 * consistency in mballoc world means that at any time a block is either
193 * free or used in ALL structures. notice: "any time" should not be read
194 * literally -- time is discrete and delimited by locks.
195 *
196 * to keep it simple, we don't use block numbers, instead we count number of
197 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
198 *
199 * all operations can be expressed as:
200 * - init buddy: buddy = on-disk + PAs
201 * - new PA: buddy += N; PA = N
202 * - use inode PA: on-disk += N; PA -= N
203 * - discard inode PA buddy -= on-disk - PA; PA = 0
204 * - use locality group PA on-disk += N; PA -= N
205 * - discard locality group PA buddy -= PA; PA = 0
206 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
207 * is used in real operation because we can't know actual used
208 * bits from PA, only from on-disk bitmap
209 *
210 * if we follow this strict logic, then all operations above should be atomic.
211 * given some of them can block, we'd have to use something like semaphores
212 * killing performance on high-end SMP hardware. let's try to relax it using
213 * the following knowledge:
214 * 1) if buddy is referenced, it's already initialized
215 * 2) while block is used in buddy and the buddy is referenced,
216 * nobody can re-allocate that block
217 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
218 * bit set and PA claims same block, it's OK. IOW, one can set bit in
219 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
220 * block
221 *
222 * so, now we're building a concurrency table:
223 * - init buddy vs.
224 * - new PA
225 * blocks for PA are allocated in the buddy, buddy must be referenced
226 * until PA is linked to allocation group to avoid concurrent buddy init
227 * - use inode PA
228 * we need to make sure that either on-disk bitmap or PA has uptodate data
229 * given (3) we care that PA-=N operation doesn't interfere with init
230 * - discard inode PA
231 * the simplest way would be to have buddy initialized by the discard
232 * - use locality group PA
233 * again PA-=N must be serialized with init
234 * - discard locality group PA
235 * the simplest way would be to have buddy initialized by the discard
236 * - new PA vs.
237 * - use inode PA
238 * i_data_sem serializes them
239 * - discard inode PA
240 * discard process must wait until PA isn't used by another process
241 * - use locality group PA
242 * some mutex should serialize them
243 * - discard locality group PA
244 * discard process must wait until PA isn't used by another process
245 * - use inode PA
246 * - use inode PA
247 * i_data_sem or another mutex should serializes them
248 * - discard inode PA
249 * discard process must wait until PA isn't used by another process
250 * - use locality group PA
251 * nothing wrong here -- they're different PAs covering different blocks
252 * - discard locality group PA
253 * discard process must wait until PA isn't used by another process
254 *
255 * now we're ready to make few consequences:
256 * - PA is referenced and while it is no discard is possible
257 * - PA is referenced until block isn't marked in on-disk bitmap
258 * - PA changes only after on-disk bitmap
259 * - discard must not compete with init. either init is done before
260 * any discard or they're serialized somehow
261 * - buddy init as sum of on-disk bitmap and PAs is done atomically
262 *
263 * a special case when we've used PA to emptiness. no need to modify buddy
264 * in this case, but we should care about concurrent init
265 *
266 */
267
268 /*
269 * Logic in few words:
270 *
271 * - allocation:
272 * load group
273 * find blocks
274 * mark bits in on-disk bitmap
275 * release group
276 *
277 * - use preallocation:
278 * find proper PA (per-inode or group)
279 * load group
280 * mark bits in on-disk bitmap
281 * release group
282 * release PA
283 *
284 * - free:
285 * load group
286 * mark bits in on-disk bitmap
287 * release group
288 *
289 * - discard preallocations in group:
290 * mark PAs deleted
291 * move them onto local list
292 * load on-disk bitmap
293 * load group
294 * remove PA from object (inode or locality group)
295 * mark free blocks in-core
296 *
297 * - discard inode's preallocations:
298 */
299
300/*
301 * Locking rules
302 *
303 * Locks:
304 * - bitlock on a group (group)
305 * - object (inode/locality) (object)
306 * - per-pa lock (pa)
307 *
308 * Paths:
309 * - new pa
310 * object
311 * group
312 *
313 * - find and use pa:
314 * pa
315 *
316 * - release consumed pa:
317 * pa
318 * group
319 * object
320 *
321 * - generate in-core bitmap:
322 * group
323 * pa
324 *
325 * - discard all for given object (inode, locality group):
326 * object
327 * pa
328 * group
329 *
330 * - discard all for given group:
331 * group
332 * pa
333 * group
334 * object
335 *
336 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500337static struct kmem_cache *ext4_pspace_cachep;
338static struct kmem_cache *ext4_ac_cachep;
339static struct kmem_cache *ext4_free_ext_cachep;
340static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
341 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500342static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
343 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500344static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
345
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500346static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
347{
Alex Tomasc9de5602008-01-29 00:19:52 -0500348#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500349 *bit += ((unsigned long) addr & 7UL) << 3;
350 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500351#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500352 *bit += ((unsigned long) addr & 3UL) << 3;
353 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500354#else
355#error "how many bits you are?!"
356#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500357 return addr;
358}
Alex Tomasc9de5602008-01-29 00:19:52 -0500359
360static inline int mb_test_bit(int bit, void *addr)
361{
362 /*
363 * ext4_test_bit on architecture like powerpc
364 * needs unsigned long aligned address
365 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500367 return ext4_test_bit(bit, addr);
368}
369
370static inline void mb_set_bit(int bit, void *addr)
371{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500372 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500373 ext4_set_bit(bit, addr);
374}
375
Alex Tomasc9de5602008-01-29 00:19:52 -0500376static inline void mb_clear_bit(int bit, void *addr)
377{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500378 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500379 ext4_clear_bit(bit, addr);
380}
381
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500382static inline int mb_find_next_zero_bit(void *addr, int max, int start)
383{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400384 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500385 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400386 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500387 start += fix;
388
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400389 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
390 if (ret > max)
391 return max;
392 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500393}
394
395static inline int mb_find_next_bit(void *addr, int max, int start)
396{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400397 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400399 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500400 start += fix;
401
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400402 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
403 if (ret > max)
404 return max;
405 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500406}
407
Alex Tomasc9de5602008-01-29 00:19:52 -0500408static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
409{
410 char *bb;
411
Alex Tomasc9de5602008-01-29 00:19:52 -0500412 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
413 BUG_ON(max == NULL);
414
415 if (order > e4b->bd_blkbits + 1) {
416 *max = 0;
417 return NULL;
418 }
419
420 /* at order 0 we see each particular block */
421 *max = 1 << (e4b->bd_blkbits + 3);
422 if (order == 0)
423 return EXT4_MB_BITMAP(e4b);
424
425 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
426 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
427
428 return bb;
429}
430
431#ifdef DOUBLE_CHECK
432static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
433 int first, int count)
434{
435 int i;
436 struct super_block *sb = e4b->bd_sb;
437
438 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
439 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400440 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500441 for (i = 0; i < count; i++) {
442 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
443 ext4_fsblk_t blocknr;
444 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
445 blocknr += first + i;
446 blocknr +=
447 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500448 ext4_grp_locked_error(sb, e4b->bd_group,
449 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500450 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500451 inode ? inode->i_ino : 0, blocknr,
452 first + i, e4b->bd_group);
453 }
454 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
455 }
456}
457
458static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
459{
460 int i;
461
462 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
463 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400464 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500465 for (i = 0; i < count; i++) {
466 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
467 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
468 }
469}
470
471static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
472{
473 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
474 unsigned char *b1, *b2;
475 int i;
476 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
477 b2 = (unsigned char *) bitmap;
478 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
479 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500480 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400481 "at byte %u(%u): %x in copy != %x "
482 "on disk/prealloc\n",
483 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500484 BUG();
485 }
486 }
487 }
488}
489
490#else
491static inline void mb_free_blocks_double(struct inode *inode,
492 struct ext4_buddy *e4b, int first, int count)
493{
494 return;
495}
496static inline void mb_mark_used_double(struct ext4_buddy *e4b,
497 int first, int count)
498{
499 return;
500}
501static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
502{
503 return;
504}
505#endif
506
507#ifdef AGGRESSIVE_CHECK
508
509#define MB_CHECK_ASSERT(assert) \
510do { \
511 if (!(assert)) { \
512 printk(KERN_EMERG \
513 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
514 function, file, line, # assert); \
515 BUG(); \
516 } \
517} while (0)
518
519static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
520 const char *function, int line)
521{
522 struct super_block *sb = e4b->bd_sb;
523 int order = e4b->bd_blkbits + 1;
524 int max;
525 int max2;
526 int i;
527 int j;
528 int k;
529 int count;
530 struct ext4_group_info *grp;
531 int fragments = 0;
532 int fstart;
533 struct list_head *cur;
534 void *buddy;
535 void *buddy2;
536
Alex Tomasc9de5602008-01-29 00:19:52 -0500537 {
538 static int mb_check_counter;
539 if (mb_check_counter++ % 100 != 0)
540 return 0;
541 }
542
543 while (order > 1) {
544 buddy = mb_find_buddy(e4b, order, &max);
545 MB_CHECK_ASSERT(buddy);
546 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
547 MB_CHECK_ASSERT(buddy2);
548 MB_CHECK_ASSERT(buddy != buddy2);
549 MB_CHECK_ASSERT(max * 2 == max2);
550
551 count = 0;
552 for (i = 0; i < max; i++) {
553
554 if (mb_test_bit(i, buddy)) {
555 /* only single bit in buddy2 may be 1 */
556 if (!mb_test_bit(i << 1, buddy2)) {
557 MB_CHECK_ASSERT(
558 mb_test_bit((i<<1)+1, buddy2));
559 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
560 MB_CHECK_ASSERT(
561 mb_test_bit(i << 1, buddy2));
562 }
563 continue;
564 }
565
566 /* both bits in buddy2 must be 0 */
567 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
568 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
569
570 for (j = 0; j < (1 << order); j++) {
571 k = (i * (1 << order)) + j;
572 MB_CHECK_ASSERT(
573 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
574 }
575 count++;
576 }
577 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
578 order--;
579 }
580
581 fstart = -1;
582 buddy = mb_find_buddy(e4b, 0, &max);
583 for (i = 0; i < max; i++) {
584 if (!mb_test_bit(i, buddy)) {
585 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
586 if (fstart == -1) {
587 fragments++;
588 fstart = i;
589 }
590 continue;
591 }
592 fstart = -1;
593 /* check used bits only */
594 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
595 buddy2 = mb_find_buddy(e4b, j, &max2);
596 k = i >> j;
597 MB_CHECK_ASSERT(k < max2);
598 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
599 }
600 }
601 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
602 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
603
604 grp = ext4_get_group_info(sb, e4b->bd_group);
605 buddy = mb_find_buddy(e4b, 0, &max);
606 list_for_each(cur, &grp->bb_prealloc_list) {
607 ext4_group_t groupnr;
608 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400609 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
610 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500611 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400612 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500613 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
614 }
615 return 0;
616}
617#undef MB_CHECK_ASSERT
618#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400619 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500620#else
621#define mb_check_buddy(e4b)
622#endif
623
624/* FIXME!! need more doc */
625static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400626 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500627 struct ext4_group_info *grp)
628{
629 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400630 ext4_grpblk_t min;
631 ext4_grpblk_t max;
632 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 unsigned short border;
634
Valerie Clementb73fce62008-02-15 13:48:51 -0500635 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500636
637 border = 2 << sb->s_blocksize_bits;
638
639 while (len > 0) {
640 /* find how many blocks can be covered since this position */
641 max = ffs(first | border) - 1;
642
643 /* find how many blocks of power 2 we need to mark */
644 min = fls(len) - 1;
645
646 if (max < min)
647 min = max;
648 chunk = 1 << min;
649
650 /* mark multiblock chunks only */
651 grp->bb_counters[min]++;
652 if (min > 0)
653 mb_clear_bit(first >> min,
654 buddy + sbi->s_mb_offsets[min]);
655
656 len -= chunk;
657 first += chunk;
658 }
659}
660
Eric Sandeen089ceec2009-07-05 22:17:31 -0400661static noinline_for_stack
662void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500663 void *buddy, void *bitmap, ext4_group_t group)
664{
665 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400666 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
667 ext4_grpblk_t i = 0;
668 ext4_grpblk_t first;
669 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500670 unsigned free = 0;
671 unsigned fragments = 0;
672 unsigned long long period = get_cycles();
673
674 /* initialize buddy from bitmap which is aggregation
675 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500676 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500677 grp->bb_first_free = i;
678 while (i < max) {
679 fragments++;
680 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500681 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500682 len = i - first;
683 free += len;
684 if (len > 1)
685 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
686 else
687 grp->bb_counters[0]++;
688 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500689 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500690 }
691 grp->bb_fragments = fragments;
692
693 if (free != grp->bb_free) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500694 ext4_grp_locked_error(sb, group, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500695 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
Alex Tomasc9de5602008-01-29 00:19:52 -0500696 group, free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500697 /*
698 * If we intent to continue, we consider group descritor
699 * corrupt and update bb_free using bitmap value
700 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500701 grp->bb_free = free;
702 }
703
704 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
705
706 period = get_cycles() - period;
707 spin_lock(&EXT4_SB(sb)->s_bal_lock);
708 EXT4_SB(sb)->s_mb_buddies_generated++;
709 EXT4_SB(sb)->s_mb_generation_time += period;
710 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
711}
712
713/* The buddy information is attached the buddy cache inode
714 * for convenience. The information regarding each group
715 * is loaded via ext4_mb_load_buddy. The information involve
716 * block bitmap and buddy information. The information are
717 * stored in the inode as
718 *
719 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500720 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500721 *
722 *
723 * one block each for bitmap and buddy information.
724 * So for each group we take up 2 blocks. A page can
725 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
726 * So it can have information regarding groups_per_page which
727 * is blocks_per_page/2
728 */
729
730static int ext4_mb_init_cache(struct page *page, char *incore)
731{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400732 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500733 int blocksize;
734 int blocks_per_page;
735 int groups_per_page;
736 int err = 0;
737 int i;
738 ext4_group_t first_group;
739 int first_block;
740 struct super_block *sb;
741 struct buffer_head *bhs;
742 struct buffer_head **bh;
743 struct inode *inode;
744 char *data;
745 char *bitmap;
746
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400747 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500748
749 inode = page->mapping->host;
750 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400751 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500752 blocksize = 1 << inode->i_blkbits;
753 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
754
755 groups_per_page = blocks_per_page >> 1;
756 if (groups_per_page == 0)
757 groups_per_page = 1;
758
759 /* allocate buffer_heads to read bitmaps */
760 if (groups_per_page > 1) {
761 err = -ENOMEM;
762 i = sizeof(struct buffer_head *) * groups_per_page;
763 bh = kzalloc(i, GFP_NOFS);
764 if (bh == NULL)
765 goto out;
766 } else
767 bh = &bhs;
768
769 first_group = page->index * blocks_per_page / 2;
770
771 /* read all groups the page covers into the cache */
772 for (i = 0; i < groups_per_page; i++) {
773 struct ext4_group_desc *desc;
774
Theodore Ts'o8df96752009-05-01 08:50:38 -0400775 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500776 break;
777
778 err = -EIO;
779 desc = ext4_get_group_desc(sb, first_group + i, NULL);
780 if (desc == NULL)
781 goto out;
782
783 err = -ENOMEM;
784 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
785 if (bh[i] == NULL)
786 goto out;
787
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500788 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500789 continue;
790
Frederic Bohec806e682008-10-10 08:09:18 -0400791 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500792 if (bitmap_uptodate(bh[i])) {
793 unlock_buffer(bh[i]);
794 continue;
795 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400796 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
798 ext4_init_block_bitmap(sb, bh[i],
799 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500800 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500801 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400802 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500803 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500804 continue;
805 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400806 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500807 if (buffer_uptodate(bh[i])) {
808 /*
809 * if not uninit if bh is uptodate,
810 * bitmap is also uptodate
811 */
812 set_bitmap_uptodate(bh[i]);
813 unlock_buffer(bh[i]);
814 continue;
815 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500817 /*
818 * submit the buffer_head for read. We can
819 * safely mark the bitmap as uptodate now.
820 * We do it here so the bitmap uptodate bit
821 * get set with buffer lock held.
822 */
823 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500824 bh[i]->b_end_io = end_buffer_read_sync;
825 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400826 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500827 }
828
829 /* wait for I/O completion */
830 for (i = 0; i < groups_per_page && bh[i]; i++)
831 wait_on_buffer(bh[i]);
832
833 err = -EIO;
834 for (i = 0; i < groups_per_page && bh[i]; i++)
835 if (!buffer_uptodate(bh[i]))
836 goto out;
837
Mingming Cao31b481d2008-07-11 19:27:31 -0400838 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500839 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500840 /* init the page */
841 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500842 for (i = 0; i < blocks_per_page; i++) {
843 int group;
844 struct ext4_group_info *grinfo;
845
846 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400847 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 break;
849
850 /*
851 * data carry information regarding this
852 * particular group in the format specified
853 * above
854 *
855 */
856 data = page_address(page) + (i * blocksize);
857 bitmap = bh[group - first_group]->b_data;
858
859 /*
860 * We place the buddy block and bitmap block
861 * close together
862 */
863 if ((first_block + i) & 1) {
864 /* this is block of buddy */
865 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400866 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 group, page->index, i * blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500868 grinfo = ext4_get_group_info(sb, group);
869 grinfo->bb_fragments = 0;
870 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400871 sizeof(*grinfo->bb_counters) *
872 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 /*
874 * incore got set to the group block bitmap below
875 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500876 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500877 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500878 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 incore = NULL;
880 } else {
881 /* this is block of bitmap */
882 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400883 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500884 group, page->index, i * blocksize);
885
886 /* see comments in ext4_mb_put_pa() */
887 ext4_lock_group(sb, group);
888 memcpy(data, bitmap, blocksize);
889
890 /* mark all preallocated blks used in in-core bitmap */
891 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500892 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500893 ext4_unlock_group(sb, group);
894
895 /* set incore so that the buddy information can be
896 * generated using this
897 */
898 incore = data;
899 }
900 }
901 SetPageUptodate(page);
902
903out:
904 if (bh) {
905 for (i = 0; i < groups_per_page && bh[i]; i++)
906 brelse(bh[i]);
907 if (bh != &bhs)
908 kfree(bh);
909 }
910 return err;
911}
912
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400913static noinline_for_stack
914int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
915{
916
917 int ret = 0;
918 void *bitmap;
919 int blocks_per_page;
920 int block, pnum, poff;
921 int num_grp_locked = 0;
922 struct ext4_group_info *this_grp;
923 struct ext4_sb_info *sbi = EXT4_SB(sb);
924 struct inode *inode = sbi->s_buddy_cache;
925 struct page *page = NULL, *bitmap_page = NULL;
926
927 mb_debug(1, "init group %u\n", group);
928 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
929 this_grp = ext4_get_group_info(sb, group);
930 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400931 * This ensures that we don't reinit the buddy cache
932 * page which map to the group from which we are already
933 * allocating. If we are looking at the buddy cache we would
934 * have taken a reference using ext4_mb_load_buddy and that
935 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400936 */
937 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
938 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
939 /*
940 * somebody initialized the group
941 * return without doing anything
942 */
943 ret = 0;
944 goto err;
945 }
946 /*
947 * the buddy cache inode stores the block bitmap
948 * and buddy information in consecutive blocks.
949 * So for each group we need two blocks.
950 */
951 block = group * 2;
952 pnum = block / blocks_per_page;
953 poff = block % blocks_per_page;
954 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
955 if (page) {
956 BUG_ON(page->mapping != inode->i_mapping);
957 ret = ext4_mb_init_cache(page, NULL);
958 if (ret) {
959 unlock_page(page);
960 goto err;
961 }
962 unlock_page(page);
963 }
964 if (page == NULL || !PageUptodate(page)) {
965 ret = -EIO;
966 goto err;
967 }
968 mark_page_accessed(page);
969 bitmap_page = page;
970 bitmap = page_address(page) + (poff * sb->s_blocksize);
971
972 /* init buddy cache */
973 block++;
974 pnum = block / blocks_per_page;
975 poff = block % blocks_per_page;
976 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
977 if (page == bitmap_page) {
978 /*
979 * If both the bitmap and buddy are in
980 * the same page we don't need to force
981 * init the buddy
982 */
983 unlock_page(page);
984 } else if (page) {
985 BUG_ON(page->mapping != inode->i_mapping);
986 ret = ext4_mb_init_cache(page, bitmap);
987 if (ret) {
988 unlock_page(page);
989 goto err;
990 }
991 unlock_page(page);
992 }
993 if (page == NULL || !PageUptodate(page)) {
994 ret = -EIO;
995 goto err;
996 }
997 mark_page_accessed(page);
998err:
999 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1000 if (bitmap_page)
1001 page_cache_release(bitmap_page);
1002 if (page)
1003 page_cache_release(page);
1004 return ret;
1005}
1006
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001007static noinline_for_stack int
1008ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1009 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001010{
Alex Tomasc9de5602008-01-29 00:19:52 -05001011 int blocks_per_page;
1012 int block;
1013 int pnum;
1014 int poff;
1015 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001016 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001017 struct ext4_group_info *grp;
1018 struct ext4_sb_info *sbi = EXT4_SB(sb);
1019 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001020
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001021 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001022
1023 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001024 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001025
1026 e4b->bd_blkbits = sb->s_blocksize_bits;
1027 e4b->bd_info = ext4_get_group_info(sb, group);
1028 e4b->bd_sb = sb;
1029 e4b->bd_group = group;
1030 e4b->bd_buddy_page = NULL;
1031 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001032 e4b->alloc_semp = &grp->alloc_sem;
1033
1034 /* Take the read lock on the group alloc
1035 * sem. This would make sure a parallel
1036 * ext4_mb_init_group happening on other
1037 * groups mapped by the page is blocked
1038 * till we are done with allocation
1039 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001040repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001041 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001042
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001043 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1044 /* we need to check for group need init flag
1045 * with alloc_semp held so that we can be sure
1046 * that new blocks didn't get added to the group
1047 * when we are loading the buddy cache
1048 */
1049 up_read(e4b->alloc_semp);
1050 /*
1051 * we need full data about the group
1052 * to make a good selection
1053 */
1054 ret = ext4_mb_init_group(sb, group);
1055 if (ret)
1056 return ret;
1057 goto repeat_load_buddy;
1058 }
1059
Alex Tomasc9de5602008-01-29 00:19:52 -05001060 /*
1061 * the buddy cache inode stores the block bitmap
1062 * and buddy information in consecutive blocks.
1063 * So for each group we need two blocks.
1064 */
1065 block = group * 2;
1066 pnum = block / blocks_per_page;
1067 poff = block % blocks_per_page;
1068
1069 /* we could use find_or_create_page(), but it locks page
1070 * what we'd like to avoid in fast path ... */
1071 page = find_get_page(inode->i_mapping, pnum);
1072 if (page == NULL || !PageUptodate(page)) {
1073 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001074 /*
1075 * drop the page reference and try
1076 * to get the page with lock. If we
1077 * are not uptodate that implies
1078 * somebody just created the page but
1079 * is yet to initialize the same. So
1080 * wait for it to initialize.
1081 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001082 page_cache_release(page);
1083 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1084 if (page) {
1085 BUG_ON(page->mapping != inode->i_mapping);
1086 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001087 ret = ext4_mb_init_cache(page, NULL);
1088 if (ret) {
1089 unlock_page(page);
1090 goto err;
1091 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001092 mb_cmp_bitmaps(e4b, page_address(page) +
1093 (poff * sb->s_blocksize));
1094 }
1095 unlock_page(page);
1096 }
1097 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001098 if (page == NULL || !PageUptodate(page)) {
1099 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001100 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001101 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001102 e4b->bd_bitmap_page = page;
1103 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1104 mark_page_accessed(page);
1105
1106 block++;
1107 pnum = block / blocks_per_page;
1108 poff = block % blocks_per_page;
1109
1110 page = find_get_page(inode->i_mapping, pnum);
1111 if (page == NULL || !PageUptodate(page)) {
1112 if (page)
1113 page_cache_release(page);
1114 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1115 if (page) {
1116 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001117 if (!PageUptodate(page)) {
1118 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1119 if (ret) {
1120 unlock_page(page);
1121 goto err;
1122 }
1123 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001124 unlock_page(page);
1125 }
1126 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001127 if (page == NULL || !PageUptodate(page)) {
1128 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001129 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001130 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001131 e4b->bd_buddy_page = page;
1132 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1133 mark_page_accessed(page);
1134
1135 BUG_ON(e4b->bd_bitmap_page == NULL);
1136 BUG_ON(e4b->bd_buddy_page == NULL);
1137
1138 return 0;
1139
1140err:
1141 if (e4b->bd_bitmap_page)
1142 page_cache_release(e4b->bd_bitmap_page);
1143 if (e4b->bd_buddy_page)
1144 page_cache_release(e4b->bd_buddy_page);
1145 e4b->bd_buddy = NULL;
1146 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001147
1148 /* Done with the buddy cache */
1149 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001150 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001151}
1152
1153static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1154{
1155 if (e4b->bd_bitmap_page)
1156 page_cache_release(e4b->bd_bitmap_page);
1157 if (e4b->bd_buddy_page)
1158 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001159 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001160 if (e4b->alloc_semp)
1161 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001162}
1163
1164
1165static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1166{
1167 int order = 1;
1168 void *bb;
1169
1170 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1171 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1172
1173 bb = EXT4_MB_BUDDY(e4b);
1174 while (order <= e4b->bd_blkbits + 1) {
1175 block = block >> 1;
1176 if (!mb_test_bit(block, bb)) {
1177 /* this block is part of buddy of order 'order' */
1178 return order;
1179 }
1180 bb += 1 << (e4b->bd_blkbits - order);
1181 order++;
1182 }
1183 return 0;
1184}
1185
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001186static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001187{
1188 __u32 *addr;
1189
1190 len = cur + len;
1191 while (cur < len) {
1192 if ((cur & 31) == 0 && (len - cur) >= 32) {
1193 /* fast path: clear whole word at once */
1194 addr = bm + (cur >> 3);
1195 *addr = 0;
1196 cur += 32;
1197 continue;
1198 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001199 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001200 cur++;
1201 }
1202}
1203
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001204static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001205{
1206 __u32 *addr;
1207
1208 len = cur + len;
1209 while (cur < len) {
1210 if ((cur & 31) == 0 && (len - cur) >= 32) {
1211 /* fast path: set whole word at once */
1212 addr = bm + (cur >> 3);
1213 *addr = 0xffffffff;
1214 cur += 32;
1215 continue;
1216 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001217 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001218 cur++;
1219 }
1220}
1221
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001222static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001223 int first, int count)
1224{
1225 int block = 0;
1226 int max = 0;
1227 int order;
1228 void *buddy;
1229 void *buddy2;
1230 struct super_block *sb = e4b->bd_sb;
1231
1232 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001233 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001234 mb_check_buddy(e4b);
1235 mb_free_blocks_double(inode, e4b, first, count);
1236
1237 e4b->bd_info->bb_free += count;
1238 if (first < e4b->bd_info->bb_first_free)
1239 e4b->bd_info->bb_first_free = first;
1240
1241 /* let's maintain fragments counter */
1242 if (first != 0)
1243 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1244 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1245 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1246 if (block && max)
1247 e4b->bd_info->bb_fragments--;
1248 else if (!block && !max)
1249 e4b->bd_info->bb_fragments++;
1250
1251 /* let's maintain buddy itself */
1252 while (count-- > 0) {
1253 block = first++;
1254 order = 0;
1255
1256 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1257 ext4_fsblk_t blocknr;
1258 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1259 blocknr += block;
1260 blocknr +=
1261 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001262 ext4_grp_locked_error(sb, e4b->bd_group,
1263 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001264 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001265 inode ? inode->i_ino : 0, blocknr, block,
1266 e4b->bd_group);
1267 }
1268 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1269 e4b->bd_info->bb_counters[order]++;
1270
1271 /* start of the buddy */
1272 buddy = mb_find_buddy(e4b, order, &max);
1273
1274 do {
1275 block &= ~1UL;
1276 if (mb_test_bit(block, buddy) ||
1277 mb_test_bit(block + 1, buddy))
1278 break;
1279
1280 /* both the buddies are free, try to coalesce them */
1281 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1282
1283 if (!buddy2)
1284 break;
1285
1286 if (order > 0) {
1287 /* for special purposes, we don't set
1288 * free bits in bitmap */
1289 mb_set_bit(block, buddy);
1290 mb_set_bit(block + 1, buddy);
1291 }
1292 e4b->bd_info->bb_counters[order]--;
1293 e4b->bd_info->bb_counters[order]--;
1294
1295 block = block >> 1;
1296 order++;
1297 e4b->bd_info->bb_counters[order]++;
1298
1299 mb_clear_bit(block, buddy2);
1300 buddy = buddy2;
1301 } while (1);
1302 }
1303 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001304}
1305
1306static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1307 int needed, struct ext4_free_extent *ex)
1308{
1309 int next = block;
1310 int max;
1311 int ord;
1312 void *buddy;
1313
Vincent Minetbc8e6742009-05-15 08:33:18 -04001314 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001315 BUG_ON(ex == NULL);
1316
1317 buddy = mb_find_buddy(e4b, order, &max);
1318 BUG_ON(buddy == NULL);
1319 BUG_ON(block >= max);
1320 if (mb_test_bit(block, buddy)) {
1321 ex->fe_len = 0;
1322 ex->fe_start = 0;
1323 ex->fe_group = 0;
1324 return 0;
1325 }
1326
1327 /* FIXME dorp order completely ? */
1328 if (likely(order == 0)) {
1329 /* find actual order */
1330 order = mb_find_order_for_block(e4b, block);
1331 block = block >> order;
1332 }
1333
1334 ex->fe_len = 1 << order;
1335 ex->fe_start = block << order;
1336 ex->fe_group = e4b->bd_group;
1337
1338 /* calc difference from given start */
1339 next = next - ex->fe_start;
1340 ex->fe_len -= next;
1341 ex->fe_start += next;
1342
1343 while (needed > ex->fe_len &&
1344 (buddy = mb_find_buddy(e4b, order, &max))) {
1345
1346 if (block + 1 >= max)
1347 break;
1348
1349 next = (block + 1) * (1 << order);
1350 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1351 break;
1352
1353 ord = mb_find_order_for_block(e4b, next);
1354
1355 order = ord;
1356 block = next >> order;
1357 ex->fe_len += 1 << order;
1358 }
1359
1360 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1361 return ex->fe_len;
1362}
1363
1364static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1365{
1366 int ord;
1367 int mlen = 0;
1368 int max = 0;
1369 int cur;
1370 int start = ex->fe_start;
1371 int len = ex->fe_len;
1372 unsigned ret = 0;
1373 int len0 = len;
1374 void *buddy;
1375
1376 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1377 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001378 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001379 mb_check_buddy(e4b);
1380 mb_mark_used_double(e4b, start, len);
1381
1382 e4b->bd_info->bb_free -= len;
1383 if (e4b->bd_info->bb_first_free == start)
1384 e4b->bd_info->bb_first_free += len;
1385
1386 /* let's maintain fragments counter */
1387 if (start != 0)
1388 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1389 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1390 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1391 if (mlen && max)
1392 e4b->bd_info->bb_fragments++;
1393 else if (!mlen && !max)
1394 e4b->bd_info->bb_fragments--;
1395
1396 /* let's maintain buddy itself */
1397 while (len) {
1398 ord = mb_find_order_for_block(e4b, start);
1399
1400 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1401 /* the whole chunk may be allocated at once! */
1402 mlen = 1 << ord;
1403 buddy = mb_find_buddy(e4b, ord, &max);
1404 BUG_ON((start >> ord) >= max);
1405 mb_set_bit(start >> ord, buddy);
1406 e4b->bd_info->bb_counters[ord]--;
1407 start += mlen;
1408 len -= mlen;
1409 BUG_ON(len < 0);
1410 continue;
1411 }
1412
1413 /* store for history */
1414 if (ret == 0)
1415 ret = len | (ord << 16);
1416
1417 /* we have to split large buddy */
1418 BUG_ON(ord <= 0);
1419 buddy = mb_find_buddy(e4b, ord, &max);
1420 mb_set_bit(start >> ord, buddy);
1421 e4b->bd_info->bb_counters[ord]--;
1422
1423 ord--;
1424 cur = (start >> ord) & ~1U;
1425 buddy = mb_find_buddy(e4b, ord, &max);
1426 mb_clear_bit(cur, buddy);
1427 mb_clear_bit(cur + 1, buddy);
1428 e4b->bd_info->bb_counters[ord]++;
1429 e4b->bd_info->bb_counters[ord]++;
1430 }
1431
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001432 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001433 mb_check_buddy(e4b);
1434
1435 return ret;
1436}
1437
1438/*
1439 * Must be called under group lock!
1440 */
1441static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1442 struct ext4_buddy *e4b)
1443{
1444 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1445 int ret;
1446
1447 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1448 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1449
1450 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1451 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1452 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1453
1454 /* preallocation can change ac_b_ex, thus we store actually
1455 * allocated blocks for history */
1456 ac->ac_f_ex = ac->ac_b_ex;
1457
1458 ac->ac_status = AC_STATUS_FOUND;
1459 ac->ac_tail = ret & 0xffff;
1460 ac->ac_buddy = ret >> 16;
1461
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001462 /*
1463 * take the page reference. We want the page to be pinned
1464 * so that we don't get a ext4_mb_init_cache_call for this
1465 * group until we update the bitmap. That would mean we
1466 * double allocate blocks. The reference is dropped
1467 * in ext4_mb_release_context
1468 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001469 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1470 get_page(ac->ac_bitmap_page);
1471 ac->ac_buddy_page = e4b->bd_buddy_page;
1472 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001473 /* on allocation we use ac to track the held semaphore */
1474 ac->alloc_semp = e4b->alloc_semp;
1475 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001476 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001477 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001478 spin_lock(&sbi->s_md_lock);
1479 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1480 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1481 spin_unlock(&sbi->s_md_lock);
1482 }
1483}
1484
1485/*
1486 * regular allocator, for general purposes allocation
1487 */
1488
1489static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1490 struct ext4_buddy *e4b,
1491 int finish_group)
1492{
1493 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1494 struct ext4_free_extent *bex = &ac->ac_b_ex;
1495 struct ext4_free_extent *gex = &ac->ac_g_ex;
1496 struct ext4_free_extent ex;
1497 int max;
1498
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001499 if (ac->ac_status == AC_STATUS_FOUND)
1500 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001501 /*
1502 * We don't want to scan for a whole year
1503 */
1504 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1505 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1506 ac->ac_status = AC_STATUS_BREAK;
1507 return;
1508 }
1509
1510 /*
1511 * Haven't found good chunk so far, let's continue
1512 */
1513 if (bex->fe_len < gex->fe_len)
1514 return;
1515
1516 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1517 && bex->fe_group == e4b->bd_group) {
1518 /* recheck chunk's availability - we don't know
1519 * when it was found (within this lock-unlock
1520 * period or not) */
1521 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1522 if (max >= gex->fe_len) {
1523 ext4_mb_use_best_found(ac, e4b);
1524 return;
1525 }
1526 }
1527}
1528
1529/*
1530 * The routine checks whether found extent is good enough. If it is,
1531 * then the extent gets marked used and flag is set to the context
1532 * to stop scanning. Otherwise, the extent is compared with the
1533 * previous found extent and if new one is better, then it's stored
1534 * in the context. Later, the best found extent will be used, if
1535 * mballoc can't find good enough extent.
1536 *
1537 * FIXME: real allocation policy is to be designed yet!
1538 */
1539static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1540 struct ext4_free_extent *ex,
1541 struct ext4_buddy *e4b)
1542{
1543 struct ext4_free_extent *bex = &ac->ac_b_ex;
1544 struct ext4_free_extent *gex = &ac->ac_g_ex;
1545
1546 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001547 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001548 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1549 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1550
1551 ac->ac_found++;
1552
1553 /*
1554 * The special case - take what you catch first
1555 */
1556 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1557 *bex = *ex;
1558 ext4_mb_use_best_found(ac, e4b);
1559 return;
1560 }
1561
1562 /*
1563 * Let's check whether the chuck is good enough
1564 */
1565 if (ex->fe_len == gex->fe_len) {
1566 *bex = *ex;
1567 ext4_mb_use_best_found(ac, e4b);
1568 return;
1569 }
1570
1571 /*
1572 * If this is first found extent, just store it in the context
1573 */
1574 if (bex->fe_len == 0) {
1575 *bex = *ex;
1576 return;
1577 }
1578
1579 /*
1580 * If new found extent is better, store it in the context
1581 */
1582 if (bex->fe_len < gex->fe_len) {
1583 /* if the request isn't satisfied, any found extent
1584 * larger than previous best one is better */
1585 if (ex->fe_len > bex->fe_len)
1586 *bex = *ex;
1587 } else if (ex->fe_len > gex->fe_len) {
1588 /* if the request is satisfied, then we try to find
1589 * an extent that still satisfy the request, but is
1590 * smaller than previous one */
1591 if (ex->fe_len < bex->fe_len)
1592 *bex = *ex;
1593 }
1594
1595 ext4_mb_check_limits(ac, e4b, 0);
1596}
1597
Eric Sandeen089ceec2009-07-05 22:17:31 -04001598static noinline_for_stack
1599int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001600 struct ext4_buddy *e4b)
1601{
1602 struct ext4_free_extent ex = ac->ac_b_ex;
1603 ext4_group_t group = ex.fe_group;
1604 int max;
1605 int err;
1606
1607 BUG_ON(ex.fe_len <= 0);
1608 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1609 if (err)
1610 return err;
1611
1612 ext4_lock_group(ac->ac_sb, group);
1613 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1614
1615 if (max > 0) {
1616 ac->ac_b_ex = ex;
1617 ext4_mb_use_best_found(ac, e4b);
1618 }
1619
1620 ext4_unlock_group(ac->ac_sb, group);
1621 ext4_mb_release_desc(e4b);
1622
1623 return 0;
1624}
1625
Eric Sandeen089ceec2009-07-05 22:17:31 -04001626static noinline_for_stack
1627int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001628 struct ext4_buddy *e4b)
1629{
1630 ext4_group_t group = ac->ac_g_ex.fe_group;
1631 int max;
1632 int err;
1633 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1634 struct ext4_super_block *es = sbi->s_es;
1635 struct ext4_free_extent ex;
1636
1637 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1638 return 0;
1639
1640 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1641 if (err)
1642 return err;
1643
1644 ext4_lock_group(ac->ac_sb, group);
1645 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1646 ac->ac_g_ex.fe_len, &ex);
1647
1648 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1649 ext4_fsblk_t start;
1650
1651 start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1652 ex.fe_start + le32_to_cpu(es->s_first_data_block);
1653 /* use do_div to get remainder (would be 64-bit modulo) */
1654 if (do_div(start, sbi->s_stripe) == 0) {
1655 ac->ac_found++;
1656 ac->ac_b_ex = ex;
1657 ext4_mb_use_best_found(ac, e4b);
1658 }
1659 } else if (max >= ac->ac_g_ex.fe_len) {
1660 BUG_ON(ex.fe_len <= 0);
1661 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1662 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1663 ac->ac_found++;
1664 ac->ac_b_ex = ex;
1665 ext4_mb_use_best_found(ac, e4b);
1666 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1667 /* Sometimes, caller may want to merge even small
1668 * number of blocks to an existing extent */
1669 BUG_ON(ex.fe_len <= 0);
1670 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1671 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1672 ac->ac_found++;
1673 ac->ac_b_ex = ex;
1674 ext4_mb_use_best_found(ac, e4b);
1675 }
1676 ext4_unlock_group(ac->ac_sb, group);
1677 ext4_mb_release_desc(e4b);
1678
1679 return 0;
1680}
1681
1682/*
1683 * The routine scans buddy structures (not bitmap!) from given order
1684 * to max order and tries to find big enough chunk to satisfy the req
1685 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001686static noinline_for_stack
1687void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001688 struct ext4_buddy *e4b)
1689{
1690 struct super_block *sb = ac->ac_sb;
1691 struct ext4_group_info *grp = e4b->bd_info;
1692 void *buddy;
1693 int i;
1694 int k;
1695 int max;
1696
1697 BUG_ON(ac->ac_2order <= 0);
1698 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1699 if (grp->bb_counters[i] == 0)
1700 continue;
1701
1702 buddy = mb_find_buddy(e4b, i, &max);
1703 BUG_ON(buddy == NULL);
1704
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001705 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001706 BUG_ON(k >= max);
1707
1708 ac->ac_found++;
1709
1710 ac->ac_b_ex.fe_len = 1 << i;
1711 ac->ac_b_ex.fe_start = k << i;
1712 ac->ac_b_ex.fe_group = e4b->bd_group;
1713
1714 ext4_mb_use_best_found(ac, e4b);
1715
1716 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1717
1718 if (EXT4_SB(sb)->s_mb_stats)
1719 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1720
1721 break;
1722 }
1723}
1724
1725/*
1726 * The routine scans the group and measures all found extents.
1727 * In order to optimize scanning, caller must pass number of
1728 * free blocks in the group, so the routine can know upper limit.
1729 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001730static noinline_for_stack
1731void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001732 struct ext4_buddy *e4b)
1733{
1734 struct super_block *sb = ac->ac_sb;
1735 void *bitmap = EXT4_MB_BITMAP(e4b);
1736 struct ext4_free_extent ex;
1737 int i;
1738 int free;
1739
1740 free = e4b->bd_info->bb_free;
1741 BUG_ON(free <= 0);
1742
1743 i = e4b->bd_info->bb_first_free;
1744
1745 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001746 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001747 EXT4_BLOCKS_PER_GROUP(sb), i);
1748 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001749 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001750 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001751 * free blocks even though group info says we
1752 * we have free blocks
1753 */
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001754 ext4_grp_locked_error(sb, e4b->bd_group,
1755 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001756 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001757 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001758 break;
1759 }
1760
1761 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1762 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001763 if (free < ex.fe_len) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001764 ext4_grp_locked_error(sb, e4b->bd_group,
1765 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001766 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001767 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001768 /*
1769 * The number of free blocks differs. This mostly
1770 * indicate that the bitmap is corrupt. So exit
1771 * without claiming the space.
1772 */
1773 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001774 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001775
1776 ext4_mb_measure_extent(ac, &ex, e4b);
1777
1778 i += ex.fe_len;
1779 free -= ex.fe_len;
1780 }
1781
1782 ext4_mb_check_limits(ac, e4b, 1);
1783}
1784
1785/*
1786 * This is a special case for storages like raid5
1787 * we try to find stripe-aligned chunks for stripe-size requests
1788 * XXX should do so at least for multiples of stripe size as well
1789 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001790static noinline_for_stack
1791void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001792 struct ext4_buddy *e4b)
1793{
1794 struct super_block *sb = ac->ac_sb;
1795 struct ext4_sb_info *sbi = EXT4_SB(sb);
1796 void *bitmap = EXT4_MB_BITMAP(e4b);
1797 struct ext4_free_extent ex;
1798 ext4_fsblk_t first_group_block;
1799 ext4_fsblk_t a;
1800 ext4_grpblk_t i;
1801 int max;
1802
1803 BUG_ON(sbi->s_stripe == 0);
1804
1805 /* find first stripe-aligned block in group */
1806 first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1807 + le32_to_cpu(sbi->s_es->s_first_data_block);
1808 a = first_group_block + sbi->s_stripe - 1;
1809 do_div(a, sbi->s_stripe);
1810 i = (a * sbi->s_stripe) - first_group_block;
1811
1812 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1813 if (!mb_test_bit(i, bitmap)) {
1814 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1815 if (max >= sbi->s_stripe) {
1816 ac->ac_found++;
1817 ac->ac_b_ex = ex;
1818 ext4_mb_use_best_found(ac, e4b);
1819 break;
1820 }
1821 }
1822 i += sbi->s_stripe;
1823 }
1824}
1825
1826static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1827 ext4_group_t group, int cr)
1828{
1829 unsigned free, fragments;
1830 unsigned i, bits;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001831 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001832 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1833
1834 BUG_ON(cr < 0 || cr >= 4);
1835 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1836
1837 free = grp->bb_free;
1838 fragments = grp->bb_fragments;
1839 if (free == 0)
1840 return 0;
1841 if (fragments == 0)
1842 return 0;
1843
1844 switch (cr) {
1845 case 0:
1846 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001847
Theodore Ts'oa4912122009-03-12 12:18:34 -04001848 /* Avoid using the first bg of a flexgroup for data files */
1849 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1850 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1851 ((group % flex_size) == 0))
1852 return 0;
1853
Alex Tomasc9de5602008-01-29 00:19:52 -05001854 bits = ac->ac_sb->s_blocksize_bits + 1;
1855 for (i = ac->ac_2order; i <= bits; i++)
1856 if (grp->bb_counters[i] > 0)
1857 return 1;
1858 break;
1859 case 1:
1860 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1861 return 1;
1862 break;
1863 case 2:
1864 if (free >= ac->ac_g_ex.fe_len)
1865 return 1;
1866 break;
1867 case 3:
1868 return 1;
1869 default:
1870 BUG();
1871 }
1872
1873 return 0;
1874}
1875
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001876/*
1877 * lock the group_info alloc_sem of all the groups
1878 * belonging to the same buddy cache page. This
1879 * make sure other parallel operation on the buddy
1880 * cache doesn't happen whild holding the buddy cache
1881 * lock
1882 */
1883int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1884{
1885 int i;
1886 int block, pnum;
1887 int blocks_per_page;
1888 int groups_per_page;
Theodore Ts'o8df96752009-05-01 08:50:38 -04001889 ext4_group_t ngroups = ext4_get_groups_count(sb);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001890 ext4_group_t first_group;
1891 struct ext4_group_info *grp;
1892
1893 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1894 /*
1895 * the buddy cache inode stores the block bitmap
1896 * and buddy information in consecutive blocks.
1897 * So for each group we need two blocks.
1898 */
1899 block = group * 2;
1900 pnum = block / blocks_per_page;
1901 first_group = pnum * blocks_per_page / 2;
1902
1903 groups_per_page = blocks_per_page >> 1;
1904 if (groups_per_page == 0)
1905 groups_per_page = 1;
1906 /* read all groups the page covers into the cache */
1907 for (i = 0; i < groups_per_page; i++) {
1908
Theodore Ts'o8df96752009-05-01 08:50:38 -04001909 if ((first_group + i) >= ngroups)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001910 break;
1911 grp = ext4_get_group_info(sb, first_group + i);
1912 /* take all groups write allocation
1913 * semaphore. This make sure there is
1914 * no block allocation going on in any
1915 * of that groups
1916 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001917 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001918 }
1919 return i;
1920}
1921
1922void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1923 ext4_group_t group, int locked_group)
1924{
1925 int i;
1926 int block, pnum;
1927 int blocks_per_page;
1928 ext4_group_t first_group;
1929 struct ext4_group_info *grp;
1930
1931 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1932 /*
1933 * the buddy cache inode stores the block bitmap
1934 * and buddy information in consecutive blocks.
1935 * So for each group we need two blocks.
1936 */
1937 block = group * 2;
1938 pnum = block / blocks_per_page;
1939 first_group = pnum * blocks_per_page / 2;
1940 /* release locks on all the groups */
1941 for (i = 0; i < locked_group; i++) {
1942
1943 grp = ext4_get_group_info(sb, first_group + i);
1944 /* take all groups write allocation
1945 * semaphore. This make sure there is
1946 * no block allocation going on in any
1947 * of that groups
1948 */
1949 up_write(&grp->alloc_sem);
1950 }
1951
1952}
1953
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001954static noinline_for_stack int
1955ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001956{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001957 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001958 int cr;
1959 int err = 0;
1960 int bsbits;
1961 struct ext4_sb_info *sbi;
1962 struct super_block *sb;
1963 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001964
1965 sb = ac->ac_sb;
1966 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001967 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001968 /* non-extent files are limited to low blocks/groups */
1969 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
1970 ngroups = sbi->s_blockfile_groups;
1971
Alex Tomasc9de5602008-01-29 00:19:52 -05001972 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1973
1974 /* first, try the goal */
1975 err = ext4_mb_find_by_goal(ac, &e4b);
1976 if (err || ac->ac_status == AC_STATUS_FOUND)
1977 goto out;
1978
1979 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1980 goto out;
1981
1982 /*
1983 * ac->ac2_order is set only if the fe_len is a power of 2
1984 * if ac2_order is set we also set criteria to 0 so that we
1985 * try exact allocation using buddy.
1986 */
1987 i = fls(ac->ac_g_ex.fe_len);
1988 ac->ac_2order = 0;
1989 /*
1990 * We search using buddy data only if the order of the request
1991 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001992 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001993 */
1994 if (i >= sbi->s_mb_order2_reqs) {
1995 /*
1996 * This should tell if fe_len is exactly power of 2
1997 */
1998 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1999 ac->ac_2order = i - 1;
2000 }
2001
2002 bsbits = ac->ac_sb->s_blocksize_bits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002003
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002004 /* if stream allocation is enabled, use global goal */
2005 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002006 /* TBD: may be hot point */
2007 spin_lock(&sbi->s_md_lock);
2008 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2009 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2010 spin_unlock(&sbi->s_md_lock);
2011 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002012
Alex Tomasc9de5602008-01-29 00:19:52 -05002013 /* Let's just scan groups to find more-less suitable blocks */
2014 cr = ac->ac_2order ? 0 : 1;
2015 /*
2016 * cr == 0 try to get exact allocation,
2017 * cr == 3 try to get anything
2018 */
2019repeat:
2020 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2021 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002022 /*
2023 * searching for the right group start
2024 * from the goal value specified
2025 */
2026 group = ac->ac_g_ex.fe_group;
2027
Theodore Ts'o8df96752009-05-01 08:50:38 -04002028 for (i = 0; i < ngroups; group++, i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002029 struct ext4_group_info *grp;
2030 struct ext4_group_desc *desc;
2031
Theodore Ts'o8df96752009-05-01 08:50:38 -04002032 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002033 group = 0;
2034
2035 /* quick check to skip empty groups */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002036 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002037 if (grp->bb_free == 0)
2038 continue;
2039
Alex Tomasc9de5602008-01-29 00:19:52 -05002040 err = ext4_mb_load_buddy(sb, group, &e4b);
2041 if (err)
2042 goto out;
2043
2044 ext4_lock_group(sb, group);
2045 if (!ext4_mb_good_group(ac, group, cr)) {
2046 /* someone did allocation from this group */
2047 ext4_unlock_group(sb, group);
2048 ext4_mb_release_desc(&e4b);
2049 continue;
2050 }
2051
2052 ac->ac_groups_scanned++;
2053 desc = ext4_get_group_desc(sb, group, NULL);
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002054 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002055 ext4_mb_simple_scan_group(ac, &e4b);
2056 else if (cr == 1 &&
2057 ac->ac_g_ex.fe_len == sbi->s_stripe)
2058 ext4_mb_scan_aligned(ac, &e4b);
2059 else
2060 ext4_mb_complex_scan_group(ac, &e4b);
2061
2062 ext4_unlock_group(sb, group);
2063 ext4_mb_release_desc(&e4b);
2064
2065 if (ac->ac_status != AC_STATUS_CONTINUE)
2066 break;
2067 }
2068 }
2069
2070 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2071 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2072 /*
2073 * We've been searching too long. Let's try to allocate
2074 * the best chunk we've found so far
2075 */
2076
2077 ext4_mb_try_best_found(ac, &e4b);
2078 if (ac->ac_status != AC_STATUS_FOUND) {
2079 /*
2080 * Someone more lucky has already allocated it.
2081 * The only thing we can do is just take first
2082 * found block(s)
2083 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2084 */
2085 ac->ac_b_ex.fe_group = 0;
2086 ac->ac_b_ex.fe_start = 0;
2087 ac->ac_b_ex.fe_len = 0;
2088 ac->ac_status = AC_STATUS_CONTINUE;
2089 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2090 cr = 3;
2091 atomic_inc(&sbi->s_mb_lost_chunks);
2092 goto repeat;
2093 }
2094 }
2095out:
2096 return err;
2097}
2098
2099#ifdef EXT4_MB_HISTORY
2100struct ext4_mb_proc_session {
2101 struct ext4_mb_history *history;
2102 struct super_block *sb;
2103 int start;
2104 int max;
2105};
2106
2107static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2108 struct ext4_mb_history *hs,
2109 int first)
2110{
2111 if (hs == s->history + s->max)
2112 hs = s->history;
2113 if (!first && hs == s->history + s->start)
2114 return NULL;
2115 while (hs->orig.fe_len == 0) {
2116 hs++;
2117 if (hs == s->history + s->max)
2118 hs = s->history;
2119 if (hs == s->history + s->start)
2120 return NULL;
2121 }
2122 return hs;
2123}
2124
2125static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2126{
2127 struct ext4_mb_proc_session *s = seq->private;
2128 struct ext4_mb_history *hs;
2129 int l = *pos;
2130
2131 if (l == 0)
2132 return SEQ_START_TOKEN;
2133 hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2134 if (!hs)
2135 return NULL;
2136 while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2137 return hs;
2138}
2139
2140static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2141 loff_t *pos)
2142{
2143 struct ext4_mb_proc_session *s = seq->private;
2144 struct ext4_mb_history *hs = v;
2145
2146 ++*pos;
2147 if (v == SEQ_START_TOKEN)
2148 return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2149 else
2150 return ext4_mb_history_skip_empty(s, ++hs, 0);
2151}
2152
2153static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2154{
2155 char buf[25], buf2[25], buf3[25], *fmt;
2156 struct ext4_mb_history *hs = v;
2157
2158 if (v == SEQ_START_TOKEN) {
2159 seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
Theodore Ts'o0ef90db2009-08-09 16:46:13 -04002160 "%-5s %-2s %-6s %-5s %-5s %-6s\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002161 "pid", "inode", "original", "goal", "result", "found",
2162 "grps", "cr", "flags", "merge", "tail", "broken");
2163 return 0;
2164 }
2165
2166 if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2167 fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
Theodore Ts'o0ef90db2009-08-09 16:46:13 -04002168 "0x%04x %-5s %-5u %-6u\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002169 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002170 hs->result.fe_start, hs->result.fe_len,
2171 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002172 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002173 hs->orig.fe_start, hs->orig.fe_len,
2174 hs->orig.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002175 sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002176 hs->goal.fe_start, hs->goal.fe_len,
2177 hs->goal.fe_logical);
2178 seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2179 hs->found, hs->groups, hs->cr, hs->flags,
2180 hs->merged ? "M" : "", hs->tail,
2181 hs->buddy ? 1 << hs->buddy : 0);
2182 } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2183 fmt = "%-5u %-8u %-23s %-23s %-23s\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002184 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002185 hs->result.fe_start, hs->result.fe_len,
2186 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002187 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002188 hs->orig.fe_start, hs->orig.fe_len,
2189 hs->orig.fe_logical);
2190 seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2191 } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002192 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002193 hs->result.fe_start, hs->result.fe_len);
2194 seq_printf(seq, "%-5u %-8u %-23s discard\n",
2195 hs->pid, hs->ino, buf2);
2196 } else if (hs->op == EXT4_MB_HISTORY_FREE) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002197 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002198 hs->result.fe_start, hs->result.fe_len);
2199 seq_printf(seq, "%-5u %-8u %-23s free\n",
2200 hs->pid, hs->ino, buf2);
2201 }
2202 return 0;
2203}
2204
2205static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2206{
2207}
2208
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002209static const struct seq_operations ext4_mb_seq_history_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002210 .start = ext4_mb_seq_history_start,
2211 .next = ext4_mb_seq_history_next,
2212 .stop = ext4_mb_seq_history_stop,
2213 .show = ext4_mb_seq_history_show,
2214};
2215
2216static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2217{
2218 struct super_block *sb = PDE(inode)->data;
2219 struct ext4_sb_info *sbi = EXT4_SB(sb);
2220 struct ext4_mb_proc_session *s;
2221 int rc;
2222 int size;
2223
Shen Feng74767c52008-07-11 19:27:31 -04002224 if (unlikely(sbi->s_mb_history == NULL))
2225 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05002226 s = kmalloc(sizeof(*s), GFP_KERNEL);
2227 if (s == NULL)
2228 return -ENOMEM;
2229 s->sb = sb;
2230 size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2231 s->history = kmalloc(size, GFP_KERNEL);
2232 if (s->history == NULL) {
2233 kfree(s);
2234 return -ENOMEM;
2235 }
2236
2237 spin_lock(&sbi->s_mb_history_lock);
2238 memcpy(s->history, sbi->s_mb_history, size);
2239 s->max = sbi->s_mb_history_max;
2240 s->start = sbi->s_mb_history_cur % s->max;
2241 spin_unlock(&sbi->s_mb_history_lock);
2242
2243 rc = seq_open(file, &ext4_mb_seq_history_ops);
2244 if (rc == 0) {
2245 struct seq_file *m = (struct seq_file *)file->private_data;
2246 m->private = s;
2247 } else {
2248 kfree(s->history);
2249 kfree(s);
2250 }
2251 return rc;
2252
2253}
2254
2255static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2256{
2257 struct seq_file *seq = (struct seq_file *)file->private_data;
2258 struct ext4_mb_proc_session *s = seq->private;
2259 kfree(s->history);
2260 kfree(s);
2261 return seq_release(inode, file);
2262}
2263
2264static ssize_t ext4_mb_seq_history_write(struct file *file,
2265 const char __user *buffer,
2266 size_t count, loff_t *ppos)
2267{
2268 struct seq_file *seq = (struct seq_file *)file->private_data;
2269 struct ext4_mb_proc_session *s = seq->private;
2270 struct super_block *sb = s->sb;
2271 char str[32];
2272 int value;
2273
2274 if (count >= sizeof(str)) {
2275 printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2276 "mb_history", (int)sizeof(str));
2277 return -EOVERFLOW;
2278 }
2279
2280 if (copy_from_user(str, buffer, count))
2281 return -EFAULT;
2282
2283 value = simple_strtol(str, NULL, 0);
2284 if (value < 0)
2285 return -ERANGE;
2286 EXT4_SB(sb)->s_mb_history_filter = value;
2287
2288 return count;
2289}
2290
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002291static const struct file_operations ext4_mb_seq_history_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002292 .owner = THIS_MODULE,
2293 .open = ext4_mb_seq_history_open,
2294 .read = seq_read,
2295 .write = ext4_mb_seq_history_write,
2296 .llseek = seq_lseek,
2297 .release = ext4_mb_seq_history_release,
2298};
2299
2300static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2301{
2302 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002303 ext4_group_t group;
2304
Theodore Ts'o8df96752009-05-01 08:50:38 -04002305 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002306 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002307 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002308 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002309}
2310
2311static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2312{
2313 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002314 ext4_group_t group;
2315
2316 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002317 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002318 return NULL;
2319 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002320 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002321}
2322
2323static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2324{
2325 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002326 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002327 int i;
2328 int err;
2329 struct ext4_buddy e4b;
2330 struct sg {
2331 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002332 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002333 } sg;
2334
2335 group--;
2336 if (group == 0)
2337 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2338 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2339 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2340 "group", "free", "frags", "first",
2341 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2342 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2343
2344 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2345 sizeof(struct ext4_group_info);
2346 err = ext4_mb_load_buddy(sb, group, &e4b);
2347 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002348 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002349 return 0;
2350 }
2351 ext4_lock_group(sb, group);
2352 memcpy(&sg, ext4_get_group_info(sb, group), i);
2353 ext4_unlock_group(sb, group);
2354 ext4_mb_release_desc(&e4b);
2355
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002356 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002357 sg.info.bb_fragments, sg.info.bb_first_free);
2358 for (i = 0; i <= 13; i++)
2359 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2360 sg.info.bb_counters[i] : 0);
2361 seq_printf(seq, " ]\n");
2362
2363 return 0;
2364}
2365
2366static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2367{
2368}
2369
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002370static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002371 .start = ext4_mb_seq_groups_start,
2372 .next = ext4_mb_seq_groups_next,
2373 .stop = ext4_mb_seq_groups_stop,
2374 .show = ext4_mb_seq_groups_show,
2375};
2376
2377static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2378{
2379 struct super_block *sb = PDE(inode)->data;
2380 int rc;
2381
2382 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2383 if (rc == 0) {
2384 struct seq_file *m = (struct seq_file *)file->private_data;
2385 m->private = sb;
2386 }
2387 return rc;
2388
2389}
2390
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002391static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002392 .owner = THIS_MODULE,
2393 .open = ext4_mb_seq_groups_open,
2394 .read = seq_read,
2395 .llseek = seq_lseek,
2396 .release = seq_release,
2397};
2398
2399static void ext4_mb_history_release(struct super_block *sb)
2400{
2401 struct ext4_sb_info *sbi = EXT4_SB(sb);
2402
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002403 if (sbi->s_proc != NULL) {
2404 remove_proc_entry("mb_groups", sbi->s_proc);
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04002405 if (sbi->s_mb_history_max)
2406 remove_proc_entry("mb_history", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002407 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002408 kfree(sbi->s_mb_history);
2409}
2410
2411static void ext4_mb_history_init(struct super_block *sb)
2412{
2413 struct ext4_sb_info *sbi = EXT4_SB(sb);
2414 int i;
2415
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002416 if (sbi->s_proc != NULL) {
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04002417 if (sbi->s_mb_history_max)
2418 proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
2419 &ext4_mb_seq_history_fops, sb);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002420 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002421 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002422 }
2423
Alex Tomasc9de5602008-01-29 00:19:52 -05002424 sbi->s_mb_history_cur = 0;
2425 spin_lock_init(&sbi->s_mb_history_lock);
2426 i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04002427 sbi->s_mb_history = i ? kzalloc(i, GFP_KERNEL) : NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002428 /* if we can't allocate history, then we simple won't use it */
2429}
2430
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002431static noinline_for_stack void
2432ext4_mb_store_history(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002433{
2434 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2435 struct ext4_mb_history h;
2436
Curt Wohlgemuthf4033902009-05-01 20:27:20 -04002437 if (sbi->s_mb_history == NULL)
Alex Tomasc9de5602008-01-29 00:19:52 -05002438 return;
2439
2440 if (!(ac->ac_op & sbi->s_mb_history_filter))
2441 return;
2442
2443 h.op = ac->ac_op;
2444 h.pid = current->pid;
2445 h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2446 h.orig = ac->ac_o_ex;
2447 h.result = ac->ac_b_ex;
2448 h.flags = ac->ac_flags;
2449 h.found = ac->ac_found;
2450 h.groups = ac->ac_groups_scanned;
2451 h.cr = ac->ac_criteria;
2452 h.tail = ac->ac_tail;
2453 h.buddy = ac->ac_buddy;
2454 h.merged = 0;
2455 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2456 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2457 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2458 h.merged = 1;
2459 h.goal = ac->ac_g_ex;
2460 h.result = ac->ac_f_ex;
2461 }
2462
2463 spin_lock(&sbi->s_mb_history_lock);
2464 memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2465 if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2466 sbi->s_mb_history_cur = 0;
2467 spin_unlock(&sbi->s_mb_history_lock);
2468}
2469
2470#else
2471#define ext4_mb_history_release(sb)
2472#define ext4_mb_history_init(sb)
2473#endif
2474
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002475
2476/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002477int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002478 struct ext4_group_desc *desc)
2479{
2480 int i, len;
2481 int metalen = 0;
2482 struct ext4_sb_info *sbi = EXT4_SB(sb);
2483 struct ext4_group_info **meta_group_info;
2484
2485 /*
2486 * First check if this group is the first of a reserved block.
2487 * If it's true, we have to allocate a new table of pointers
2488 * to ext4_group_info structures
2489 */
2490 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2491 metalen = sizeof(*meta_group_info) <<
2492 EXT4_DESC_PER_BLOCK_BITS(sb);
2493 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2494 if (meta_group_info == NULL) {
2495 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2496 "buddy group\n");
2497 goto exit_meta_group_info;
2498 }
2499 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2500 meta_group_info;
2501 }
2502
2503 /*
2504 * calculate needed size. if change bb_counters size,
2505 * don't forget about ext4_mb_generate_buddy()
2506 */
2507 len = offsetof(typeof(**meta_group_info),
2508 bb_counters[sb->s_blocksize_bits + 2]);
2509
2510 meta_group_info =
2511 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2512 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2513
2514 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2515 if (meta_group_info[i] == NULL) {
2516 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2517 goto exit_group_info;
2518 }
2519 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2520 &(meta_group_info[i]->bb_state));
2521
2522 /*
2523 * initialize bb_free to be able to skip
2524 * empty groups without initialization
2525 */
2526 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2527 meta_group_info[i]->bb_free =
2528 ext4_free_blocks_after_init(sb, group, desc);
2529 } else {
2530 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002531 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002532 }
2533
2534 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002535 init_rwsem(&meta_group_info[i]->alloc_sem);
Joe Perches5a4a7982009-07-05 22:33:08 -04002536 meta_group_info[i]->bb_free_root.rb_node = NULL;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002537
2538#ifdef DOUBLE_CHECK
2539 {
2540 struct buffer_head *bh;
2541 meta_group_info[i]->bb_bitmap =
2542 kmalloc(sb->s_blocksize, GFP_KERNEL);
2543 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2544 bh = ext4_read_block_bitmap(sb, group);
2545 BUG_ON(bh == NULL);
2546 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2547 sb->s_blocksize);
2548 put_bh(bh);
2549 }
2550#endif
2551
2552 return 0;
2553
2554exit_group_info:
2555 /* If a meta_group_info table has been allocated, release it now */
2556 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2557 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2558exit_meta_group_info:
2559 return -ENOMEM;
2560} /* ext4_mb_add_groupinfo */
2561
Alex Tomasc9de5602008-01-29 00:19:52 -05002562static int ext4_mb_init_backend(struct super_block *sb)
2563{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002564 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002565 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002566 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002567 struct ext4_super_block *es = sbi->s_es;
2568 int num_meta_group_infos;
2569 int num_meta_group_infos_max;
2570 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002571 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002572
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002573 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002574 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002575 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2576
2577 /*
2578 * This is the total number of blocks used by GDT including
2579 * the number of reserved blocks for GDT.
2580 * The s_group_info array is allocated with this value
2581 * to allow a clean online resize without a complex
2582 * manipulation of pointer.
2583 * The drawback is the unused memory when no resize
2584 * occurs but it's very low in terms of pages
2585 * (see comments below)
2586 * Need to handle this properly when META_BG resizing is allowed
2587 */
2588 num_meta_group_infos_max = num_meta_group_infos +
2589 le16_to_cpu(es->s_reserved_gdt_blocks);
2590
2591 /*
2592 * array_size is the size of s_group_info array. We round it
2593 * to the next power of two because this approximation is done
2594 * internally by kmalloc so we can have some more memory
2595 * for free here (e.g. may be used for META_BG resize).
2596 */
2597 array_size = 1;
2598 while (array_size < sizeof(*sbi->s_group_info) *
2599 num_meta_group_infos_max)
2600 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002601 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2602 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2603 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002604 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002605 if (sbi->s_group_info == NULL) {
2606 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2607 return -ENOMEM;
2608 }
2609 sbi->s_buddy_cache = new_inode(sb);
2610 if (sbi->s_buddy_cache == NULL) {
2611 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2612 goto err_freesgi;
2613 }
2614 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002615 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002616 desc = ext4_get_group_desc(sb, i, NULL);
2617 if (desc == NULL) {
2618 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002619 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002620 goto err_freebuddy;
2621 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002622 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2623 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002624 }
2625
2626 return 0;
2627
2628err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002629 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002630 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002631 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002632 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 kfree(sbi->s_group_info[i]);
2634 iput(sbi->s_buddy_cache);
2635err_freesgi:
2636 kfree(sbi->s_group_info);
2637 return -ENOMEM;
2638}
2639
2640int ext4_mb_init(struct super_block *sb, int needs_recovery)
2641{
2642 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002643 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002644 unsigned offset;
2645 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002646 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002647
Eric Sandeen19278052009-08-25 22:36:25 -04002648 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002649
2650 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2651 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002652 return -ENOMEM;
2653 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002654
Eric Sandeen19278052009-08-25 22:36:25 -04002655 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002656 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2657 if (sbi->s_mb_maxs == NULL) {
Dan Carpentera7b19442009-03-27 19:42:54 -04002658 kfree(sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002659 return -ENOMEM;
2660 }
2661
2662 /* order 0 is regular bitmap */
2663 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2664 sbi->s_mb_offsets[0] = 0;
2665
2666 i = 1;
2667 offset = 0;
2668 max = sb->s_blocksize << 2;
2669 do {
2670 sbi->s_mb_offsets[i] = offset;
2671 sbi->s_mb_maxs[i] = max;
2672 offset += 1 << (sb->s_blocksize_bits - i);
2673 max = max >> 1;
2674 i++;
2675 } while (i <= sb->s_blocksize_bits + 1);
2676
2677 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002678 ret = ext4_mb_init_backend(sb);
2679 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002680 kfree(sbi->s_mb_offsets);
2681 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002682 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002683 }
2684
2685 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002686 spin_lock_init(&sbi->s_bal_lock);
2687
2688 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2689 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2690 sbi->s_mb_stats = MB_DEFAULT_STATS;
2691 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2692 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2693 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2694 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2695
Eric Sandeen730c2132008-09-13 15:23:29 -04002696 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002697 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002698 kfree(sbi->s_mb_offsets);
2699 kfree(sbi->s_mb_maxs);
2700 return -ENOMEM;
2701 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002702 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002703 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002704 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002705 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002706 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2707 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002708 spin_lock_init(&lg->lg_prealloc_lock);
2709 }
2710
Alex Tomasc9de5602008-01-29 00:19:52 -05002711 ext4_mb_history_init(sb);
2712
Frank Mayhar03901312009-01-07 00:06:22 -05002713 if (sbi->s_journal)
2714 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002715
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002716 printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002717 return 0;
2718}
2719
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002720/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002721static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2722{
2723 struct ext4_prealloc_space *pa;
2724 struct list_head *cur, *tmp;
2725 int count = 0;
2726
2727 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2728 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2729 list_del(&pa->pa_group_list);
2730 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002731 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002732 }
2733 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002734 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002735
2736}
2737
2738int ext4_mb_release(struct super_block *sb)
2739{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002740 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002741 ext4_group_t i;
2742 int num_meta_group_infos;
2743 struct ext4_group_info *grinfo;
2744 struct ext4_sb_info *sbi = EXT4_SB(sb);
2745
Alex Tomasc9de5602008-01-29 00:19:52 -05002746 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002747 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002748 grinfo = ext4_get_group_info(sb, i);
2749#ifdef DOUBLE_CHECK
2750 kfree(grinfo->bb_bitmap);
2751#endif
2752 ext4_lock_group(sb, i);
2753 ext4_mb_cleanup_pa(grinfo);
2754 ext4_unlock_group(sb, i);
2755 kfree(grinfo);
2756 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002757 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002758 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2759 EXT4_DESC_PER_BLOCK_BITS(sb);
2760 for (i = 0; i < num_meta_group_infos; i++)
2761 kfree(sbi->s_group_info[i]);
2762 kfree(sbi->s_group_info);
2763 }
2764 kfree(sbi->s_mb_offsets);
2765 kfree(sbi->s_mb_maxs);
2766 if (sbi->s_buddy_cache)
2767 iput(sbi->s_buddy_cache);
2768 if (sbi->s_mb_stats) {
2769 printk(KERN_INFO
2770 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2771 atomic_read(&sbi->s_bal_allocated),
2772 atomic_read(&sbi->s_bal_reqs),
2773 atomic_read(&sbi->s_bal_success));
2774 printk(KERN_INFO
2775 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2776 "%u 2^N hits, %u breaks, %u lost\n",
2777 atomic_read(&sbi->s_bal_ex_scanned),
2778 atomic_read(&sbi->s_bal_goals),
2779 atomic_read(&sbi->s_bal_2orders),
2780 atomic_read(&sbi->s_bal_breaks),
2781 atomic_read(&sbi->s_mb_lost_chunks));
2782 printk(KERN_INFO
2783 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2784 sbi->s_mb_buddies_generated++,
2785 sbi->s_mb_generation_time);
2786 printk(KERN_INFO
2787 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2788 atomic_read(&sbi->s_mb_preallocated),
2789 atomic_read(&sbi->s_mb_discarded));
2790 }
2791
Eric Sandeen730c2132008-09-13 15:23:29 -04002792 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002793 ext4_mb_history_release(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002794
2795 return 0;
2796}
2797
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002798/*
2799 * This function is called by the jbd2 layer once the commit has finished,
2800 * so we know we can free the blocks that were released with that commit.
2801 */
2802static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002803{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002804 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002805 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002806 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002807 int err, count = 0, count2 = 0;
2808 struct ext4_free_data *entry;
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002809 ext4_fsblk_t discard_block;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002810 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002811
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002812 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2813 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002814
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002815 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002816 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002817
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002818 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002819 /* we expect to find existing buddy because it's pinned */
2820 BUG_ON(err != 0);
2821
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002822 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002823 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002824 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002825 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002826 ext4_lock_group(sb, entry->group);
2827 /* Take it out of per group rb tree */
2828 rb_erase(&entry->node, &(db->bb_free_root));
2829 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2830
2831 if (!db->bb_free_root.rb_node) {
2832 /* No more items in the per group rb tree
2833 * balance refcounts from ext4_mb_free_metadata()
2834 */
2835 page_cache_release(e4b.bd_buddy_page);
2836 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002837 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002838 ext4_unlock_group(sb, entry->group);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002839 discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
2840 + entry->start_blk
2841 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002842 trace_ext4_discard_blocks(sb, (unsigned long long)discard_block,
2843 entry->count);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002844 sb_issue_discard(sb, discard_block, entry->count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002845
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002846 kmem_cache_free(ext4_free_ext_cachep, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002847 ext4_mb_release_desc(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002848 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002849
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002850 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002851}
2852
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002853#ifdef CONFIG_EXT4_DEBUG
2854u8 mb_enable_debug __read_mostly;
2855
2856static struct dentry *debugfs_dir;
2857static struct dentry *debugfs_debug;
2858
2859static void __init ext4_create_debugfs_entry(void)
2860{
2861 debugfs_dir = debugfs_create_dir("ext4", NULL);
2862 if (debugfs_dir)
2863 debugfs_debug = debugfs_create_u8("mballoc-debug",
2864 S_IRUGO | S_IWUSR,
2865 debugfs_dir,
2866 &mb_enable_debug);
2867}
2868
2869static void ext4_remove_debugfs_entry(void)
2870{
2871 debugfs_remove(debugfs_debug);
2872 debugfs_remove(debugfs_dir);
2873}
2874
2875#else
2876
2877static void __init ext4_create_debugfs_entry(void)
2878{
2879}
2880
2881static void ext4_remove_debugfs_entry(void)
2882{
2883}
2884
2885#endif
2886
Alex Tomasc9de5602008-01-29 00:19:52 -05002887int __init init_ext4_mballoc(void)
2888{
2889 ext4_pspace_cachep =
2890 kmem_cache_create("ext4_prealloc_space",
2891 sizeof(struct ext4_prealloc_space),
2892 0, SLAB_RECLAIM_ACCOUNT, NULL);
2893 if (ext4_pspace_cachep == NULL)
2894 return -ENOMEM;
2895
Eric Sandeen256bdb42008-02-10 01:13:33 -05002896 ext4_ac_cachep =
2897 kmem_cache_create("ext4_alloc_context",
2898 sizeof(struct ext4_allocation_context),
2899 0, SLAB_RECLAIM_ACCOUNT, NULL);
2900 if (ext4_ac_cachep == NULL) {
2901 kmem_cache_destroy(ext4_pspace_cachep);
2902 return -ENOMEM;
2903 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002904
2905 ext4_free_ext_cachep =
2906 kmem_cache_create("ext4_free_block_extents",
2907 sizeof(struct ext4_free_data),
2908 0, SLAB_RECLAIM_ACCOUNT, NULL);
2909 if (ext4_free_ext_cachep == NULL) {
2910 kmem_cache_destroy(ext4_pspace_cachep);
2911 kmem_cache_destroy(ext4_ac_cachep);
2912 return -ENOMEM;
2913 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002914 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002915 return 0;
2916}
2917
2918void exit_ext4_mballoc(void)
2919{
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002920 /*
2921 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2922 * before destroying the slab cache.
2923 */
2924 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002925 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002926 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002927 kmem_cache_destroy(ext4_free_ext_cachep);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002928 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002929}
2930
2931
2932/*
2933 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2934 * Returns 0 if success or error code
2935 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002936static noinline_for_stack int
2937ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002938 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002939{
2940 struct buffer_head *bitmap_bh = NULL;
2941 struct ext4_super_block *es;
2942 struct ext4_group_desc *gdp;
2943 struct buffer_head *gdp_bh;
2944 struct ext4_sb_info *sbi;
2945 struct super_block *sb;
2946 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002947 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002948
2949 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2950 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2951
2952 sb = ac->ac_sb;
2953 sbi = EXT4_SB(sb);
2954 es = sbi->s_es;
2955
Alex Tomasc9de5602008-01-29 00:19:52 -05002956
2957 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002958 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002959 if (!bitmap_bh)
2960 goto out_err;
2961
2962 err = ext4_journal_get_write_access(handle, bitmap_bh);
2963 if (err)
2964 goto out_err;
2965
2966 err = -EIO;
2967 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2968 if (!gdp)
2969 goto out_err;
2970
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002971 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002972 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002973
Alex Tomasc9de5602008-01-29 00:19:52 -05002974 err = ext4_journal_get_write_access(handle, gdp_bh);
2975 if (err)
2976 goto out_err;
2977
2978 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
2979 + ac->ac_b_ex.fe_start
2980 + le32_to_cpu(es->s_first_data_block);
2981
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002982 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002983 if (!ext4_data_block_valid(sbi, block, len)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04002984 ext4_error(sb, __func__,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002985 "Allocating blocks %llu-%llu which overlap "
2986 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002987 /* File system mounted not to panic on error
2988 * Fix the bitmap and repeat the block allocation
2989 * We leak some of the blocks here.
2990 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002991 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2992 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2993 ac->ac_b_ex.fe_len);
2994 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002995 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002996 if (!err)
2997 err = -EAGAIN;
2998 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002999 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003000
3001 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003002#ifdef AGGRESSIVE_CHECK
3003 {
3004 int i;
3005 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3006 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3007 bitmap_bh->b_data));
3008 }
3009 }
3010#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003011 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 -05003012 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3013 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003014 ext4_free_blks_set(sb, gdp,
3015 ext4_free_blocks_after_init(sb,
3016 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003017 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003018 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
3019 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003020 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003021
3022 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003023 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003024 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003025 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003026 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003027 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3028 /* release all the reserved blocks if non delalloc */
3029 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Mingming Cao60e58e02009-01-22 18:13:05 +01003030 else {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003031 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
3032 ac->ac_b_ex.fe_len);
Mingming Cao60e58e02009-01-22 18:13:05 +01003033 /* convert reserved quota blocks to real quota blocks */
3034 vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
3035 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003036
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003037 if (sbi->s_log_groups_per_flex) {
3038 ext4_group_t flex_group = ext4_flex_group(sbi,
3039 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05003040 atomic_sub(ac->ac_b_ex.fe_len,
3041 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003042 }
3043
Frank Mayhar03901312009-01-07 00:06:22 -05003044 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003045 if (err)
3046 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003047 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003048
3049out_err:
3050 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003051 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003052 return err;
3053}
3054
3055/*
3056 * here we normalize request for locality group
3057 * Group request are normalized to s_strip size if we set the same via mount
3058 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003059 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05003060 *
3061 * XXX: should we try to preallocate more than the group has now?
3062 */
3063static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3064{
3065 struct super_block *sb = ac->ac_sb;
3066 struct ext4_locality_group *lg = ac->ac_lg;
3067
3068 BUG_ON(lg == NULL);
3069 if (EXT4_SB(sb)->s_stripe)
3070 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3071 else
3072 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003073 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003074 current->pid, ac->ac_g_ex.fe_len);
3075}
3076
3077/*
3078 * Normalization means making request better in terms of
3079 * size and alignment
3080 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003081static noinline_for_stack void
3082ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003083 struct ext4_allocation_request *ar)
3084{
3085 int bsbits, max;
3086 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003087 loff_t size, orig_size, start_off;
3088 ext4_lblk_t start, orig_start;
3089 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003090 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003091
3092 /* do normalize only data requests, metadata requests
3093 do not need preallocation */
3094 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3095 return;
3096
3097 /* sometime caller may want exact blocks */
3098 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3099 return;
3100
3101 /* caller may indicate that preallocation isn't
3102 * required (it's a tail, for example) */
3103 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3104 return;
3105
3106 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3107 ext4_mb_normalize_group_request(ac);
3108 return ;
3109 }
3110
3111 bsbits = ac->ac_sb->s_blocksize_bits;
3112
3113 /* first, let's learn actual file size
3114 * given current request is allocated */
3115 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3116 size = size << bsbits;
3117 if (size < i_size_read(ac->ac_inode))
3118 size = i_size_read(ac->ac_inode);
3119
Valerie Clement19304792008-05-13 19:31:14 -04003120 /* max size of free chunks */
3121 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003122
Valerie Clement19304792008-05-13 19:31:14 -04003123#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3124 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003125
3126 /* first, try to predict filesize */
3127 /* XXX: should this table be tunable? */
3128 start_off = 0;
3129 if (size <= 16 * 1024) {
3130 size = 16 * 1024;
3131 } else if (size <= 32 * 1024) {
3132 size = 32 * 1024;
3133 } else if (size <= 64 * 1024) {
3134 size = 64 * 1024;
3135 } else if (size <= 128 * 1024) {
3136 size = 128 * 1024;
3137 } else if (size <= 256 * 1024) {
3138 size = 256 * 1024;
3139 } else if (size <= 512 * 1024) {
3140 size = 512 * 1024;
3141 } else if (size <= 1024 * 1024) {
3142 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003143 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003144 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003145 (21 - bsbits)) << 21;
3146 size = 2 * 1024 * 1024;
3147 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003148 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3149 (22 - bsbits)) << 22;
3150 size = 4 * 1024 * 1024;
3151 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003152 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003153 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3154 (23 - bsbits)) << 23;
3155 size = 8 * 1024 * 1024;
3156 } else {
3157 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3158 size = ac->ac_o_ex.fe_len << bsbits;
3159 }
3160 orig_size = size = size >> bsbits;
3161 orig_start = start = start_off >> bsbits;
3162
3163 /* don't cover already allocated blocks in selected range */
3164 if (ar->pleft && start <= ar->lleft) {
3165 size -= ar->lleft + 1 - start;
3166 start = ar->lleft + 1;
3167 }
3168 if (ar->pright && start + size - 1 >= ar->lright)
3169 size -= start + size - ar->lright;
3170
3171 end = start + size;
3172
3173 /* check we don't cross already preallocated blocks */
3174 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003175 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003176 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003177
Alex Tomasc9de5602008-01-29 00:19:52 -05003178 if (pa->pa_deleted)
3179 continue;
3180 spin_lock(&pa->pa_lock);
3181 if (pa->pa_deleted) {
3182 spin_unlock(&pa->pa_lock);
3183 continue;
3184 }
3185
3186 pa_end = pa->pa_lstart + pa->pa_len;
3187
3188 /* PA must not overlap original request */
3189 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3190 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3191
Eric Sandeen38877f42009-08-17 23:55:24 -04003192 /* skip PAs this normalized request doesn't overlap with */
3193 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003194 spin_unlock(&pa->pa_lock);
3195 continue;
3196 }
3197 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3198
Eric Sandeen38877f42009-08-17 23:55:24 -04003199 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003200 if (pa_end <= ac->ac_o_ex.fe_logical) {
3201 BUG_ON(pa_end < start);
3202 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003203 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003204 BUG_ON(pa->pa_lstart > end);
3205 end = pa->pa_lstart;
3206 }
3207 spin_unlock(&pa->pa_lock);
3208 }
3209 rcu_read_unlock();
3210 size = end - start;
3211
3212 /* XXX: extra loop to check we really don't overlap preallocations */
3213 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003214 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003215 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003216 spin_lock(&pa->pa_lock);
3217 if (pa->pa_deleted == 0) {
3218 pa_end = pa->pa_lstart + pa->pa_len;
3219 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3220 }
3221 spin_unlock(&pa->pa_lock);
3222 }
3223 rcu_read_unlock();
3224
3225 if (start + size <= ac->ac_o_ex.fe_logical &&
3226 start > ac->ac_o_ex.fe_logical) {
3227 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3228 (unsigned long) start, (unsigned long) size,
3229 (unsigned long) ac->ac_o_ex.fe_logical);
3230 }
3231 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3232 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003233 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003234
3235 /* now prepare goal request */
3236
3237 /* XXX: is it better to align blocks WRT to logical
3238 * placement or satisfy big request as is */
3239 ac->ac_g_ex.fe_logical = start;
3240 ac->ac_g_ex.fe_len = size;
3241
3242 /* define goal start in order to merge */
3243 if (ar->pright && (ar->lright == (start + size))) {
3244 /* merge to the right */
3245 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3246 &ac->ac_f_ex.fe_group,
3247 &ac->ac_f_ex.fe_start);
3248 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3249 }
3250 if (ar->pleft && (ar->lleft + 1 == start)) {
3251 /* merge to the left */
3252 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3253 &ac->ac_f_ex.fe_group,
3254 &ac->ac_f_ex.fe_start);
3255 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3256 }
3257
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003258 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003259 (unsigned) orig_size, (unsigned) start);
3260}
3261
3262static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3263{
3264 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3265
3266 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3267 atomic_inc(&sbi->s_bal_reqs);
3268 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3269 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3270 atomic_inc(&sbi->s_bal_success);
3271 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3272 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3273 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3274 atomic_inc(&sbi->s_bal_goals);
3275 if (ac->ac_found > sbi->s_mb_max_to_scan)
3276 atomic_inc(&sbi->s_bal_breaks);
3277 }
3278
3279 ext4_mb_store_history(ac);
3280}
3281
3282/*
3283 * use blocks preallocated to inode
3284 */
3285static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3286 struct ext4_prealloc_space *pa)
3287{
3288 ext4_fsblk_t start;
3289 ext4_fsblk_t end;
3290 int len;
3291
3292 /* found preallocated blocks, use them */
3293 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3294 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3295 len = end - start;
3296 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3297 &ac->ac_b_ex.fe_start);
3298 ac->ac_b_ex.fe_len = len;
3299 ac->ac_status = AC_STATUS_FOUND;
3300 ac->ac_pa = pa;
3301
3302 BUG_ON(start < pa->pa_pstart);
3303 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3304 BUG_ON(pa->pa_free < len);
3305 pa->pa_free -= len;
3306
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003307 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003308}
3309
3310/*
3311 * use blocks preallocated to locality group
3312 */
3313static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3314 struct ext4_prealloc_space *pa)
3315{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003316 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003317
Alex Tomasc9de5602008-01-29 00:19:52 -05003318 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3319 &ac->ac_b_ex.fe_group,
3320 &ac->ac_b_ex.fe_start);
3321 ac->ac_b_ex.fe_len = len;
3322 ac->ac_status = AC_STATUS_FOUND;
3323 ac->ac_pa = pa;
3324
3325 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003326 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003327 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003328 * in on-disk bitmap -- see ext4_mb_release_context()
3329 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003330 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003331 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003332}
3333
3334/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003335 * Return the prealloc space that have minimal distance
3336 * from the goal block. @cpa is the prealloc
3337 * space that is having currently known minimal distance
3338 * from the goal block.
3339 */
3340static struct ext4_prealloc_space *
3341ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3342 struct ext4_prealloc_space *pa,
3343 struct ext4_prealloc_space *cpa)
3344{
3345 ext4_fsblk_t cur_distance, new_distance;
3346
3347 if (cpa == NULL) {
3348 atomic_inc(&pa->pa_count);
3349 return pa;
3350 }
3351 cur_distance = abs(goal_block - cpa->pa_pstart);
3352 new_distance = abs(goal_block - pa->pa_pstart);
3353
3354 if (cur_distance < new_distance)
3355 return cpa;
3356
3357 /* drop the previous reference */
3358 atomic_dec(&cpa->pa_count);
3359 atomic_inc(&pa->pa_count);
3360 return pa;
3361}
3362
3363/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003364 * search goal blocks in preallocated space
3365 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003366static noinline_for_stack int
3367ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003368{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003369 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003370 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3371 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003372 struct ext4_prealloc_space *pa, *cpa = NULL;
3373 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003374
3375 /* only data can be preallocated */
3376 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3377 return 0;
3378
3379 /* first, try per-file preallocation */
3380 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003381 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003382
3383 /* all fields in this condition don't change,
3384 * so we can skip locking for them */
3385 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3386 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3387 continue;
3388
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003389 /* non-extent files can't have physical blocks past 2^32 */
3390 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
3391 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3392 continue;
3393
Alex Tomasc9de5602008-01-29 00:19:52 -05003394 /* found preallocated blocks, use them */
3395 spin_lock(&pa->pa_lock);
3396 if (pa->pa_deleted == 0 && pa->pa_free) {
3397 atomic_inc(&pa->pa_count);
3398 ext4_mb_use_inode_pa(ac, pa);
3399 spin_unlock(&pa->pa_lock);
3400 ac->ac_criteria = 10;
3401 rcu_read_unlock();
3402 return 1;
3403 }
3404 spin_unlock(&pa->pa_lock);
3405 }
3406 rcu_read_unlock();
3407
3408 /* can we use group allocation? */
3409 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3410 return 0;
3411
3412 /* inode may have no locality group for some reason */
3413 lg = ac->ac_lg;
3414 if (lg == NULL)
3415 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003416 order = fls(ac->ac_o_ex.fe_len) - 1;
3417 if (order > PREALLOC_TB_SIZE - 1)
3418 /* The max size of hash table is PREALLOC_TB_SIZE */
3419 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003420
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003421 goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
3422 ac->ac_g_ex.fe_start +
3423 le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
3424 /*
3425 * search for the prealloc space that is having
3426 * minimal distance from the goal block.
3427 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003428 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3429 rcu_read_lock();
3430 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3431 pa_inode_list) {
3432 spin_lock(&pa->pa_lock);
3433 if (pa->pa_deleted == 0 &&
3434 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003435
3436 cpa = ext4_mb_check_group_pa(goal_block,
3437 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003438 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003439 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003440 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003441 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003442 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003443 if (cpa) {
3444 ext4_mb_use_group_pa(ac, cpa);
3445 ac->ac_criteria = 20;
3446 return 1;
3447 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003448 return 0;
3449}
3450
3451/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003452 * the function goes through all block freed in the group
3453 * but not yet committed and marks them used in in-core bitmap.
3454 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003455 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003456 */
3457static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3458 ext4_group_t group)
3459{
3460 struct rb_node *n;
3461 struct ext4_group_info *grp;
3462 struct ext4_free_data *entry;
3463
3464 grp = ext4_get_group_info(sb, group);
3465 n = rb_first(&(grp->bb_free_root));
3466
3467 while (n) {
3468 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003469 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003470 n = rb_next(n);
3471 }
3472 return;
3473}
3474
3475/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003476 * the function goes through all preallocation in this group and marks them
3477 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003478 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003479 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003480static noinline_for_stack
3481void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003482 ext4_group_t group)
3483{
3484 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3485 struct ext4_prealloc_space *pa;
3486 struct list_head *cur;
3487 ext4_group_t groupnr;
3488 ext4_grpblk_t start;
3489 int preallocated = 0;
3490 int count = 0;
3491 int len;
3492
3493 /* all form of preallocation discards first load group,
3494 * so the only competing code is preallocation use.
3495 * we don't need any locking here
3496 * notice we do NOT ignore preallocations with pa_deleted
3497 * otherwise we could leave used blocks available for
3498 * allocation in buddy when concurrent ext4_mb_put_pa()
3499 * is dropping preallocation
3500 */
3501 list_for_each(cur, &grp->bb_prealloc_list) {
3502 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3503 spin_lock(&pa->pa_lock);
3504 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3505 &groupnr, &start);
3506 len = pa->pa_len;
3507 spin_unlock(&pa->pa_lock);
3508 if (unlikely(len == 0))
3509 continue;
3510 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003511 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003512 preallocated += len;
3513 count++;
3514 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003515 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003516}
3517
3518static void ext4_mb_pa_callback(struct rcu_head *head)
3519{
3520 struct ext4_prealloc_space *pa;
3521 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3522 kmem_cache_free(ext4_pspace_cachep, pa);
3523}
3524
3525/*
3526 * drops a reference to preallocated space descriptor
3527 * if this was the last reference and the space is consumed
3528 */
3529static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3530 struct super_block *sb, struct ext4_prealloc_space *pa)
3531{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003532 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003533 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003534
3535 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3536 return;
3537
3538 /* in this short window concurrent discard can set pa_deleted */
3539 spin_lock(&pa->pa_lock);
3540 if (pa->pa_deleted == 1) {
3541 spin_unlock(&pa->pa_lock);
3542 return;
3543 }
3544
3545 pa->pa_deleted = 1;
3546 spin_unlock(&pa->pa_lock);
3547
Eric Sandeend33a1972009-03-16 23:25:40 -04003548 grp_blk = pa->pa_pstart;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003549 /*
3550 * If doing group-based preallocation, pa_pstart may be in the
3551 * next group when pa is used up
3552 */
3553 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003554 grp_blk--;
3555
3556 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003557
3558 /*
3559 * possible race:
3560 *
3561 * P1 (buddy init) P2 (regular allocation)
3562 * find block B in PA
3563 * copy on-disk bitmap to buddy
3564 * mark B in on-disk bitmap
3565 * drop PA from group
3566 * mark all PAs in buddy
3567 *
3568 * thus, P1 initializes buddy with B available. to prevent this
3569 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3570 * against that pair
3571 */
3572 ext4_lock_group(sb, grp);
3573 list_del(&pa->pa_group_list);
3574 ext4_unlock_group(sb, grp);
3575
3576 spin_lock(pa->pa_obj_lock);
3577 list_del_rcu(&pa->pa_inode_list);
3578 spin_unlock(pa->pa_obj_lock);
3579
3580 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3581}
3582
3583/*
3584 * creates new preallocated space for given inode
3585 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003586static noinline_for_stack int
3587ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003588{
3589 struct super_block *sb = ac->ac_sb;
3590 struct ext4_prealloc_space *pa;
3591 struct ext4_group_info *grp;
3592 struct ext4_inode_info *ei;
3593
3594 /* preallocate only when found space is larger then requested */
3595 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3596 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3597 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3598
3599 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3600 if (pa == NULL)
3601 return -ENOMEM;
3602
3603 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3604 int winl;
3605 int wins;
3606 int win;
3607 int offs;
3608
3609 /* we can't allocate as much as normalizer wants.
3610 * so, found space must get proper lstart
3611 * to cover original request */
3612 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3613 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3614
3615 /* we're limited by original request in that
3616 * logical block must be covered any way
3617 * winl is window we can move our chunk within */
3618 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3619
3620 /* also, we should cover whole original request */
3621 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3622
3623 /* the smallest one defines real window */
3624 win = min(winl, wins);
3625
3626 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3627 if (offs && offs < win)
3628 win = offs;
3629
3630 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3631 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3632 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3633 }
3634
3635 /* preallocation can change ac_b_ex, thus we store actually
3636 * allocated blocks for history */
3637 ac->ac_f_ex = ac->ac_b_ex;
3638
3639 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3640 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3641 pa->pa_len = ac->ac_b_ex.fe_len;
3642 pa->pa_free = pa->pa_len;
3643 atomic_set(&pa->pa_count, 1);
3644 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003645 INIT_LIST_HEAD(&pa->pa_inode_list);
3646 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003647 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003648 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003649
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003650 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003651 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003652 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003653
3654 ext4_mb_use_inode_pa(ac, pa);
3655 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3656
3657 ei = EXT4_I(ac->ac_inode);
3658 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3659
3660 pa->pa_obj_lock = &ei->i_prealloc_lock;
3661 pa->pa_inode = ac->ac_inode;
3662
3663 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3664 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3665 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3666
3667 spin_lock(pa->pa_obj_lock);
3668 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3669 spin_unlock(pa->pa_obj_lock);
3670
3671 return 0;
3672}
3673
3674/*
3675 * creates new preallocated space for locality group inodes belongs to
3676 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003677static noinline_for_stack int
3678ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003679{
3680 struct super_block *sb = ac->ac_sb;
3681 struct ext4_locality_group *lg;
3682 struct ext4_prealloc_space *pa;
3683 struct ext4_group_info *grp;
3684
3685 /* preallocate only when found space is larger then requested */
3686 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3687 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3688 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3689
3690 BUG_ON(ext4_pspace_cachep == NULL);
3691 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3692 if (pa == NULL)
3693 return -ENOMEM;
3694
3695 /* preallocation can change ac_b_ex, thus we store actually
3696 * allocated blocks for history */
3697 ac->ac_f_ex = ac->ac_b_ex;
3698
3699 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3700 pa->pa_lstart = pa->pa_pstart;
3701 pa->pa_len = ac->ac_b_ex.fe_len;
3702 pa->pa_free = pa->pa_len;
3703 atomic_set(&pa->pa_count, 1);
3704 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003705 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003706 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003707 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003708 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003709
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003710 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003711 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3712 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003713
3714 ext4_mb_use_group_pa(ac, pa);
3715 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3716
3717 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3718 lg = ac->ac_lg;
3719 BUG_ON(lg == NULL);
3720
3721 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3722 pa->pa_inode = NULL;
3723
3724 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3725 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3726 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3727
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003728 /*
3729 * We will later add the new pa to the right bucket
3730 * after updating the pa_free in ext4_mb_release_context
3731 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003732 return 0;
3733}
3734
3735static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3736{
3737 int err;
3738
3739 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3740 err = ext4_mb_new_group_pa(ac);
3741 else
3742 err = ext4_mb_new_inode_pa(ac);
3743 return err;
3744}
3745
3746/*
3747 * finds all unused blocks in on-disk bitmap, frees them in
3748 * in-core bitmap and buddy.
3749 * @pa must be unlinked from inode and group lists, so that
3750 * nobody else can find/use it.
3751 * the caller MUST hold group/inode locks.
3752 * TODO: optimize the case when there are no in-core structures yet
3753 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003754static noinline_for_stack int
3755ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003756 struct ext4_prealloc_space *pa,
3757 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003758{
Alex Tomasc9de5602008-01-29 00:19:52 -05003759 struct super_block *sb = e4b->bd_sb;
3760 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003761 unsigned int end;
3762 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003763 ext4_group_t group;
3764 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003765 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003766 sector_t start;
3767 int err = 0;
3768 int free = 0;
3769
3770 BUG_ON(pa->pa_deleted == 0);
3771 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003772 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003773 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3774 end = bit + pa->pa_len;
3775
Eric Sandeen256bdb42008-02-10 01:13:33 -05003776 if (ac) {
3777 ac->ac_sb = sb;
3778 ac->ac_inode = pa->pa_inode;
3779 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3780 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003781
3782 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003783 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003784 if (bit >= end)
3785 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003786 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003787 start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3788 le32_to_cpu(sbi->s_es->s_first_data_block);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003789 mb_debug(1, " free preallocated %u/%u in group %u\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003790 (unsigned) start, (unsigned) next - bit,
3791 (unsigned) group);
3792 free += next - bit;
3793
Eric Sandeen256bdb42008-02-10 01:13:33 -05003794 if (ac) {
3795 ac->ac_b_ex.fe_group = group;
3796 ac->ac_b_ex.fe_start = bit;
3797 ac->ac_b_ex.fe_len = next - bit;
3798 ac->ac_b_ex.fe_logical = 0;
3799 ext4_mb_store_history(ac);
3800 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003801
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003802 trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3803 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003804 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3805 bit = next + 1;
3806 }
3807 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003808 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003809 pa, (unsigned long) pa->pa_lstart,
3810 (unsigned long) pa->pa_pstart,
3811 (unsigned long) pa->pa_len);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003812 ext4_grp_locked_error(sb, group,
3813 __func__, "free %u, pa_free %u",
3814 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003815 /*
3816 * pa is already deleted so we use the value obtained
3817 * from the bitmap and continue.
3818 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003819 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003820 atomic_add(free, &sbi->s_mb_discarded);
3821
3822 return err;
3823}
3824
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003825static noinline_for_stack int
3826ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003827 struct ext4_prealloc_space *pa,
3828 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003829{
Alex Tomasc9de5602008-01-29 00:19:52 -05003830 struct super_block *sb = e4b->bd_sb;
3831 ext4_group_t group;
3832 ext4_grpblk_t bit;
3833
Eric Sandeen256bdb42008-02-10 01:13:33 -05003834 if (ac)
3835 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
Alex Tomasc9de5602008-01-29 00:19:52 -05003836
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003837 trace_ext4_mb_release_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003838 BUG_ON(pa->pa_deleted == 0);
3839 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3840 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3841 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3842 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3843
Eric Sandeen256bdb42008-02-10 01:13:33 -05003844 if (ac) {
3845 ac->ac_sb = sb;
3846 ac->ac_inode = NULL;
3847 ac->ac_b_ex.fe_group = group;
3848 ac->ac_b_ex.fe_start = bit;
3849 ac->ac_b_ex.fe_len = pa->pa_len;
3850 ac->ac_b_ex.fe_logical = 0;
3851 ext4_mb_store_history(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003852 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003853
3854 return 0;
3855}
3856
3857/*
3858 * releases all preallocations in given group
3859 *
3860 * first, we need to decide discard policy:
3861 * - when do we discard
3862 * 1) ENOSPC
3863 * - how many do we discard
3864 * 1) how many requested
3865 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003866static noinline_for_stack int
3867ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003868 ext4_group_t group, int needed)
3869{
3870 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3871 struct buffer_head *bitmap_bh = NULL;
3872 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003873 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003874 struct list_head list;
3875 struct ext4_buddy e4b;
3876 int err;
3877 int busy = 0;
3878 int free = 0;
3879
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003880 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003881
3882 if (list_empty(&grp->bb_prealloc_list))
3883 return 0;
3884
Theodore Ts'o574ca172008-07-11 19:27:31 -04003885 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003886 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003887 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003888 "bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003889 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003890 }
3891
3892 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003893 if (err) {
3894 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003895 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003896 put_bh(bitmap_bh);
3897 return 0;
3898 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003899
3900 if (needed == 0)
3901 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3902
Alex Tomasc9de5602008-01-29 00:19:52 -05003903 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003904 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003905 if (ac)
3906 ac->ac_sb = sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05003907repeat:
3908 ext4_lock_group(sb, group);
3909 list_for_each_entry_safe(pa, tmp,
3910 &grp->bb_prealloc_list, pa_group_list) {
3911 spin_lock(&pa->pa_lock);
3912 if (atomic_read(&pa->pa_count)) {
3913 spin_unlock(&pa->pa_lock);
3914 busy = 1;
3915 continue;
3916 }
3917 if (pa->pa_deleted) {
3918 spin_unlock(&pa->pa_lock);
3919 continue;
3920 }
3921
3922 /* seems this one can be freed ... */
3923 pa->pa_deleted = 1;
3924
3925 /* we can trust pa_free ... */
3926 free += pa->pa_free;
3927
3928 spin_unlock(&pa->pa_lock);
3929
3930 list_del(&pa->pa_group_list);
3931 list_add(&pa->u.pa_tmp_list, &list);
3932 }
3933
3934 /* if we still need more blocks and some PAs were used, try again */
3935 if (free < needed && busy) {
3936 busy = 0;
3937 ext4_unlock_group(sb, group);
3938 /*
3939 * Yield the CPU here so that we don't get soft lockup
3940 * in non preempt case.
3941 */
3942 yield();
3943 goto repeat;
3944 }
3945
3946 /* found anything to free? */
3947 if (list_empty(&list)) {
3948 BUG_ON(free != 0);
3949 goto out;
3950 }
3951
3952 /* now free all selected PAs */
3953 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3954
3955 /* remove from object (inode or locality group) */
3956 spin_lock(pa->pa_obj_lock);
3957 list_del_rcu(&pa->pa_inode_list);
3958 spin_unlock(pa->pa_obj_lock);
3959
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003960 if (pa->pa_type == MB_GROUP_PA)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003961 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003962 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003963 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003964
3965 list_del(&pa->u.pa_tmp_list);
3966 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3967 }
3968
3969out:
3970 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003971 if (ac)
3972 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003973 ext4_mb_release_desc(&e4b);
3974 put_bh(bitmap_bh);
3975 return free;
3976}
3977
3978/*
3979 * releases all non-used preallocated blocks for given inode
3980 *
3981 * It's important to discard preallocations under i_data_sem
3982 * We don't want another block to be served from the prealloc
3983 * space when we are discarding the inode prealloc space.
3984 *
3985 * FIXME!! Make sure it is valid at all the call sites
3986 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003987void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003988{
3989 struct ext4_inode_info *ei = EXT4_I(inode);
3990 struct super_block *sb = inode->i_sb;
3991 struct buffer_head *bitmap_bh = NULL;
3992 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003993 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003994 ext4_group_t group = 0;
3995 struct list_head list;
3996 struct ext4_buddy e4b;
3997 int err;
3998
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003999 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004000 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4001 return;
4002 }
4003
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004004 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004005 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004006
4007 INIT_LIST_HEAD(&list);
4008
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004009 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004010 if (ac) {
4011 ac->ac_sb = sb;
4012 ac->ac_inode = inode;
4013 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004014repeat:
4015 /* first, collect all pa's in the inode */
4016 spin_lock(&ei->i_prealloc_lock);
4017 while (!list_empty(&ei->i_prealloc_list)) {
4018 pa = list_entry(ei->i_prealloc_list.next,
4019 struct ext4_prealloc_space, pa_inode_list);
4020 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4021 spin_lock(&pa->pa_lock);
4022 if (atomic_read(&pa->pa_count)) {
4023 /* this shouldn't happen often - nobody should
4024 * use preallocation while we're discarding it */
4025 spin_unlock(&pa->pa_lock);
4026 spin_unlock(&ei->i_prealloc_lock);
4027 printk(KERN_ERR "uh-oh! used pa while discarding\n");
4028 WARN_ON(1);
4029 schedule_timeout_uninterruptible(HZ);
4030 goto repeat;
4031
4032 }
4033 if (pa->pa_deleted == 0) {
4034 pa->pa_deleted = 1;
4035 spin_unlock(&pa->pa_lock);
4036 list_del_rcu(&pa->pa_inode_list);
4037 list_add(&pa->u.pa_tmp_list, &list);
4038 continue;
4039 }
4040
4041 /* someone is deleting pa right now */
4042 spin_unlock(&pa->pa_lock);
4043 spin_unlock(&ei->i_prealloc_lock);
4044
4045 /* we have to wait here because pa_deleted
4046 * doesn't mean pa is already unlinked from
4047 * the list. as we might be called from
4048 * ->clear_inode() the inode will get freed
4049 * and concurrent thread which is unlinking
4050 * pa from inode's list may access already
4051 * freed memory, bad-bad-bad */
4052
4053 /* XXX: if this happens too often, we can
4054 * add a flag to force wait only in case
4055 * of ->clear_inode(), but not in case of
4056 * regular truncate */
4057 schedule_timeout_uninterruptible(HZ);
4058 goto repeat;
4059 }
4060 spin_unlock(&ei->i_prealloc_lock);
4061
4062 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004063 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05004064 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4065
4066 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004067 if (err) {
4068 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004069 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004070 continue;
4071 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004072
Theodore Ts'o574ca172008-07-11 19:27:31 -04004073 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004074 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004075 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004076 "bitmap for %u", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004077 ext4_mb_release_desc(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004078 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004079 }
4080
4081 ext4_lock_group(sb, group);
4082 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004083 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004084 ext4_unlock_group(sb, group);
4085
4086 ext4_mb_release_desc(&e4b);
4087 put_bh(bitmap_bh);
4088
4089 list_del(&pa->u.pa_tmp_list);
4090 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4091 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004092 if (ac)
4093 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004094}
4095
4096/*
4097 * finds all preallocated spaces and return blocks being freed to them
4098 * if preallocated space becomes full (no block is used from the space)
4099 * then the function frees space in buddy
4100 * XXX: at the moment, truncate (which is the only way to free blocks)
4101 * discards all preallocations
4102 */
4103static void ext4_mb_return_to_preallocation(struct inode *inode,
4104 struct ext4_buddy *e4b,
4105 sector_t block, int count)
4106{
4107 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4108}
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004109#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004110static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4111{
4112 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004113 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004114
4115 printk(KERN_ERR "EXT4-fs: Can't allocate:"
4116 " Allocation context details:\n");
4117 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4118 ac->ac_status, ac->ac_flags);
4119 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4120 "best %lu/%lu/%lu@%lu cr %d\n",
4121 (unsigned long)ac->ac_o_ex.fe_group,
4122 (unsigned long)ac->ac_o_ex.fe_start,
4123 (unsigned long)ac->ac_o_ex.fe_len,
4124 (unsigned long)ac->ac_o_ex.fe_logical,
4125 (unsigned long)ac->ac_g_ex.fe_group,
4126 (unsigned long)ac->ac_g_ex.fe_start,
4127 (unsigned long)ac->ac_g_ex.fe_len,
4128 (unsigned long)ac->ac_g_ex.fe_logical,
4129 (unsigned long)ac->ac_b_ex.fe_group,
4130 (unsigned long)ac->ac_b_ex.fe_start,
4131 (unsigned long)ac->ac_b_ex.fe_len,
4132 (unsigned long)ac->ac_b_ex.fe_logical,
4133 (int)ac->ac_criteria);
4134 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4135 ac->ac_found);
4136 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004137 ngroups = ext4_get_groups_count(sb);
4138 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004139 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4140 struct ext4_prealloc_space *pa;
4141 ext4_grpblk_t start;
4142 struct list_head *cur;
4143 ext4_lock_group(sb, i);
4144 list_for_each(cur, &grp->bb_prealloc_list) {
4145 pa = list_entry(cur, struct ext4_prealloc_space,
4146 pa_group_list);
4147 spin_lock(&pa->pa_lock);
4148 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4149 NULL, &start);
4150 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004151 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4152 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004153 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004154 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004155
4156 if (grp->bb_free == 0)
4157 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004158 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004159 i, grp->bb_free, grp->bb_fragments);
4160 }
4161 printk(KERN_ERR "\n");
4162}
4163#else
4164static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4165{
4166 return;
4167}
4168#endif
4169
4170/*
4171 * We use locality group preallocation for small size file. The size of the
4172 * file is determined by the current size or the resulting size after
4173 * allocation which ever is larger
4174 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004175 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004176 */
4177static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4178{
4179 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4180 int bsbits = ac->ac_sb->s_blocksize_bits;
4181 loff_t size, isize;
4182
4183 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4184 return;
4185
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004186 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4187 return;
4188
Alex Tomasc9de5602008-01-29 00:19:52 -05004189 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04004190 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4191 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004192
Theodore Ts'o50797482009-09-18 13:34:02 -04004193 if ((size == isize) &&
4194 !ext4_fs_is_busy(sbi) &&
4195 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4196 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4197 return;
4198 }
4199
Alex Tomasc9de5602008-01-29 00:19:52 -05004200 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004201 size = max(size, isize);
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004202 if (size >= sbi->s_mb_stream_request) {
4203 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004204 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004205 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004206
4207 BUG_ON(ac->ac_lg != NULL);
4208 /*
4209 * locality group prealloc space are per cpu. The reason for having
4210 * per cpu locality group is to reduce the contention between block
4211 * request from multiple CPUs.
4212 */
Eric Sandeen730c2132008-09-13 15:23:29 -04004213 ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
Alex Tomasc9de5602008-01-29 00:19:52 -05004214
4215 /* we're going to use group allocation */
4216 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4217
4218 /* serialize all allocations in the group */
4219 mutex_lock(&ac->ac_lg->lg_mutex);
4220}
4221
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004222static noinline_for_stack int
4223ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004224 struct ext4_allocation_request *ar)
4225{
4226 struct super_block *sb = ar->inode->i_sb;
4227 struct ext4_sb_info *sbi = EXT4_SB(sb);
4228 struct ext4_super_block *es = sbi->s_es;
4229 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004230 unsigned int len;
4231 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004232 ext4_grpblk_t block;
4233
4234 /* we can't allocate > group size */
4235 len = ar->len;
4236
4237 /* just a dirty hack to filter too big requests */
4238 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4239 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4240
4241 /* start searching from the goal */
4242 goal = ar->goal;
4243 if (goal < le32_to_cpu(es->s_first_data_block) ||
4244 goal >= ext4_blocks_count(es))
4245 goal = le32_to_cpu(es->s_first_data_block);
4246 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4247
4248 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004249 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004250 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004251 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004252 ac->ac_sb = sb;
4253 ac->ac_inode = ar->inode;
4254 ac->ac_o_ex.fe_logical = ar->logical;
4255 ac->ac_o_ex.fe_group = group;
4256 ac->ac_o_ex.fe_start = block;
4257 ac->ac_o_ex.fe_len = len;
4258 ac->ac_g_ex.fe_logical = ar->logical;
4259 ac->ac_g_ex.fe_group = group;
4260 ac->ac_g_ex.fe_start = block;
4261 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004262 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004263
4264 /* we have to define context: we'll we work with a file or
4265 * locality group. this is a policy, actually */
4266 ext4_mb_group_or_file(ac);
4267
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004268 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004269 "left: %u/%u, right %u/%u to %swritable\n",
4270 (unsigned) ar->len, (unsigned) ar->logical,
4271 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4272 (unsigned) ar->lleft, (unsigned) ar->pleft,
4273 (unsigned) ar->lright, (unsigned) ar->pright,
4274 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4275 return 0;
4276
4277}
4278
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004279static noinline_for_stack void
4280ext4_mb_discard_lg_preallocations(struct super_block *sb,
4281 struct ext4_locality_group *lg,
4282 int order, int total_entries)
4283{
4284 ext4_group_t group = 0;
4285 struct ext4_buddy e4b;
4286 struct list_head discard_list;
4287 struct ext4_prealloc_space *pa, *tmp;
4288 struct ext4_allocation_context *ac;
4289
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004290 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004291
4292 INIT_LIST_HEAD(&discard_list);
4293 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004294 if (ac)
4295 ac->ac_sb = sb;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004296
4297 spin_lock(&lg->lg_prealloc_lock);
4298 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4299 pa_inode_list) {
4300 spin_lock(&pa->pa_lock);
4301 if (atomic_read(&pa->pa_count)) {
4302 /*
4303 * This is the pa that we just used
4304 * for block allocation. So don't
4305 * free that
4306 */
4307 spin_unlock(&pa->pa_lock);
4308 continue;
4309 }
4310 if (pa->pa_deleted) {
4311 spin_unlock(&pa->pa_lock);
4312 continue;
4313 }
4314 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004315 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004316
4317 /* seems this one can be freed ... */
4318 pa->pa_deleted = 1;
4319 spin_unlock(&pa->pa_lock);
4320
4321 list_del_rcu(&pa->pa_inode_list);
4322 list_add(&pa->u.pa_tmp_list, &discard_list);
4323
4324 total_entries--;
4325 if (total_entries <= 5) {
4326 /*
4327 * we want to keep only 5 entries
4328 * allowing it to grow to 8. This
4329 * mak sure we don't call discard
4330 * soon for this list.
4331 */
4332 break;
4333 }
4334 }
4335 spin_unlock(&lg->lg_prealloc_lock);
4336
4337 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4338
4339 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4340 if (ext4_mb_load_buddy(sb, group, &e4b)) {
4341 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004342 "information for %u", group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004343 continue;
4344 }
4345 ext4_lock_group(sb, group);
4346 list_del(&pa->pa_group_list);
4347 ext4_mb_release_group_pa(&e4b, pa, ac);
4348 ext4_unlock_group(sb, group);
4349
4350 ext4_mb_release_desc(&e4b);
4351 list_del(&pa->u.pa_tmp_list);
4352 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4353 }
4354 if (ac)
4355 kmem_cache_free(ext4_ac_cachep, ac);
4356}
4357
4358/*
4359 * We have incremented pa_count. So it cannot be freed at this
4360 * point. Also we hold lg_mutex. So no parallel allocation is
4361 * possible from this lg. That means pa_free cannot be updated.
4362 *
4363 * A parallel ext4_mb_discard_group_preallocations is possible.
4364 * which can cause the lg_prealloc_list to be updated.
4365 */
4366
4367static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4368{
4369 int order, added = 0, lg_prealloc_count = 1;
4370 struct super_block *sb = ac->ac_sb;
4371 struct ext4_locality_group *lg = ac->ac_lg;
4372 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4373
4374 order = fls(pa->pa_free) - 1;
4375 if (order > PREALLOC_TB_SIZE - 1)
4376 /* The max size of hash table is PREALLOC_TB_SIZE */
4377 order = PREALLOC_TB_SIZE - 1;
4378 /* Add the prealloc space to lg */
4379 rcu_read_lock();
4380 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4381 pa_inode_list) {
4382 spin_lock(&tmp_pa->pa_lock);
4383 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004384 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004385 continue;
4386 }
4387 if (!added && pa->pa_free < tmp_pa->pa_free) {
4388 /* Add to the tail of the previous entry */
4389 list_add_tail_rcu(&pa->pa_inode_list,
4390 &tmp_pa->pa_inode_list);
4391 added = 1;
4392 /*
4393 * we want to count the total
4394 * number of entries in the list
4395 */
4396 }
4397 spin_unlock(&tmp_pa->pa_lock);
4398 lg_prealloc_count++;
4399 }
4400 if (!added)
4401 list_add_tail_rcu(&pa->pa_inode_list,
4402 &lg->lg_prealloc_list[order]);
4403 rcu_read_unlock();
4404
4405 /* Now trim the list to be not more than 8 elements */
4406 if (lg_prealloc_count > 8) {
4407 ext4_mb_discard_lg_preallocations(sb, lg,
4408 order, lg_prealloc_count);
4409 return;
4410 }
4411 return ;
4412}
4413
Alex Tomasc9de5602008-01-29 00:19:52 -05004414/*
4415 * release all resource we used in allocation
4416 */
4417static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4418{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004419 struct ext4_prealloc_space *pa = ac->ac_pa;
4420 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004421 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004422 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004423 spin_lock(&pa->pa_lock);
4424 pa->pa_pstart += ac->ac_b_ex.fe_len;
4425 pa->pa_lstart += ac->ac_b_ex.fe_len;
4426 pa->pa_free -= ac->ac_b_ex.fe_len;
4427 pa->pa_len -= ac->ac_b_ex.fe_len;
4428 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004429 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004430 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004431 if (ac->alloc_semp)
4432 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004433 if (pa) {
4434 /*
4435 * We want to add the pa to the right bucket.
4436 * Remove it from the list and while adding
4437 * make sure the list to which we are adding
4438 * doesn't grow big. We need to release
4439 * alloc_semp before calling ext4_mb_add_n_trim()
4440 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004441 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004442 spin_lock(pa->pa_obj_lock);
4443 list_del_rcu(&pa->pa_inode_list);
4444 spin_unlock(pa->pa_obj_lock);
4445 ext4_mb_add_n_trim(ac);
4446 }
4447 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4448 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004449 if (ac->ac_bitmap_page)
4450 page_cache_release(ac->ac_bitmap_page);
4451 if (ac->ac_buddy_page)
4452 page_cache_release(ac->ac_buddy_page);
4453 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4454 mutex_unlock(&ac->ac_lg->lg_mutex);
4455 ext4_mb_collect_stats(ac);
4456 return 0;
4457}
4458
4459static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4460{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004461 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004462 int ret;
4463 int freed = 0;
4464
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004465 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004466 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004467 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4468 freed += ret;
4469 needed -= ret;
4470 }
4471
4472 return freed;
4473}
4474
4475/*
4476 * Main entry point into mballoc to allocate blocks
4477 * it tries to use preallocation first, then falls back
4478 * to usual allocation
4479 */
4480ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4481 struct ext4_allocation_request *ar, int *errp)
4482{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004483 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004484 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004485 struct ext4_sb_info *sbi;
4486 struct super_block *sb;
4487 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004488 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004489 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004490
4491 sb = ar->inode->i_sb;
4492 sbi = EXT4_SB(sb);
4493
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004494 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004495
Mingming Cao60e58e02009-01-22 18:13:05 +01004496 /*
4497 * For delayed allocation, we could skip the ENOSPC and
4498 * EDQUOT check, as blocks and quotas have been already
4499 * reserved when data being copied into pagecache.
4500 */
4501 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4502 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4503 else {
4504 /* Without delayed allocation we need to verify
4505 * there is enough free blocks to do block allocation
4506 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004507 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004508 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4509 /* let others to free the space */
4510 yield();
4511 ar->len = ar->len >> 1;
4512 }
4513 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004514 *errp = -ENOSPC;
4515 return 0;
4516 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004517 reserv_blks = ar->len;
Jan Karaa269eb12009-01-26 17:04:39 +01004518 while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004519 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4520 ar->len--;
4521 }
4522 inquota = ar->len;
4523 if (ar->len == 0) {
4524 *errp = -EDQUOT;
4525 goto out3;
4526 }
Mingming Caod2a17632008-07-14 17:52:37 -04004527 }
Mingming Caod2a17632008-07-14 17:52:37 -04004528
Eric Sandeen256bdb42008-02-10 01:13:33 -05004529 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004530 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004531 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004532 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004533 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004534 }
4535
Eric Sandeen256bdb42008-02-10 01:13:33 -05004536 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004537 if (*errp) {
4538 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004539 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004540 }
4541
Eric Sandeen256bdb42008-02-10 01:13:33 -05004542 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4543 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004544 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4545 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004546repeat:
4547 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004548 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004549
4550 /* as we've just preallocated more space than
4551 * user requested orinally, we store allocated
4552 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004553 if (ac->ac_status == AC_STATUS_FOUND &&
4554 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4555 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004556 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004557 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004558 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004559 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004560 /*
4561 * drop the reference that we took
4562 * in ext4_mb_use_best_found
4563 */
4564 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004565 ac->ac_b_ex.fe_group = 0;
4566 ac->ac_b_ex.fe_start = 0;
4567 ac->ac_b_ex.fe_len = 0;
4568 ac->ac_status = AC_STATUS_CONTINUE;
4569 goto repeat;
4570 } else if (*errp) {
4571 ac->ac_b_ex.fe_len = 0;
4572 ar->len = 0;
4573 ext4_mb_show_ac(ac);
4574 } else {
4575 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4576 ar->len = ac->ac_b_ex.fe_len;
4577 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004578 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004579 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004580 if (freed)
4581 goto repeat;
4582 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004583 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004584 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004585 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004586 }
4587
Eric Sandeen256bdb42008-02-10 01:13:33 -05004588 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004589
Shen Feng363d4252008-07-11 19:27:31 -04004590out2:
4591 kmem_cache_free(ext4_ac_cachep, ac);
4592out1:
Mingming Cao60e58e02009-01-22 18:13:05 +01004593 if (inquota && ar->len < inquota)
Jan Karaa269eb12009-01-26 17:04:39 +01004594 vfs_dq_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004595out3:
4596 if (!ar->len) {
4597 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4598 /* release all the reserved blocks if non delalloc */
4599 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4600 reserv_blks);
4601 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004602
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004603 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004604
Alex Tomasc9de5602008-01-29 00:19:52 -05004605 return block;
4606}
Alex Tomasc9de5602008-01-29 00:19:52 -05004607
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004608/*
4609 * We can merge two free data extents only if the physical blocks
4610 * are contiguous, AND the extents were freed by the same transaction,
4611 * AND the blocks are associated with the same group.
4612 */
4613static int can_merge(struct ext4_free_data *entry1,
4614 struct ext4_free_data *entry2)
4615{
4616 if ((entry1->t_tid == entry2->t_tid) &&
4617 (entry1->group == entry2->group) &&
4618 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4619 return 1;
4620 return 0;
4621}
4622
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004623static noinline_for_stack int
4624ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004625 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004626{
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004627 ext4_grpblk_t block;
4628 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004629 struct ext4_group_info *db = e4b->bd_info;
4630 struct super_block *sb = e4b->bd_sb;
4631 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004632 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4633 struct rb_node *parent = NULL, *new_node;
4634
Frank Mayhar03901312009-01-07 00:06:22 -05004635 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004636 BUG_ON(e4b->bd_bitmap_page == NULL);
4637 BUG_ON(e4b->bd_buddy_page == NULL);
4638
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004639 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004640 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004641
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004642 if (!*n) {
4643 /* first free block exent. We need to
4644 protect buddy cache from being freed,
4645 * otherwise we'll refresh it from
4646 * on-disk bitmap and lose not-yet-available
4647 * blocks */
4648 page_cache_get(e4b->bd_buddy_page);
4649 page_cache_get(e4b->bd_bitmap_page);
4650 }
4651 while (*n) {
4652 parent = *n;
4653 entry = rb_entry(parent, struct ext4_free_data, node);
4654 if (block < entry->start_blk)
4655 n = &(*n)->rb_left;
4656 else if (block >= (entry->start_blk + entry->count))
4657 n = &(*n)->rb_right;
4658 else {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05004659 ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4660 "Double free of blocks %d (%d %d)",
4661 block, entry->start_blk, entry->count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004662 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 }
4664 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004665
4666 rb_link_node(new_node, parent, n);
4667 rb_insert_color(new_node, &db->bb_free_root);
4668
4669 /* Now try to see the extent can be merged to left and right */
4670 node = rb_prev(new_node);
4671 if (node) {
4672 entry = rb_entry(node, struct ext4_free_data, node);
4673 if (can_merge(entry, new_entry)) {
4674 new_entry->start_blk = entry->start_blk;
4675 new_entry->count += entry->count;
4676 rb_erase(node, &(db->bb_free_root));
4677 spin_lock(&sbi->s_md_lock);
4678 list_del(&entry->list);
4679 spin_unlock(&sbi->s_md_lock);
4680 kmem_cache_free(ext4_free_ext_cachep, entry);
4681 }
4682 }
4683
4684 node = rb_next(new_node);
4685 if (node) {
4686 entry = rb_entry(node, struct ext4_free_data, node);
4687 if (can_merge(new_entry, entry)) {
4688 new_entry->count += entry->count;
4689 rb_erase(node, &(db->bb_free_root));
4690 spin_lock(&sbi->s_md_lock);
4691 list_del(&entry->list);
4692 spin_unlock(&sbi->s_md_lock);
4693 kmem_cache_free(ext4_free_ext_cachep, entry);
4694 }
4695 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004696 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004697 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004698 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004699 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004700 return 0;
4701}
4702
4703/*
4704 * Main entry point into mballoc to free blocks
4705 */
4706void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004707 ext4_fsblk_t block, unsigned long count,
Alex Tomasc9de5602008-01-29 00:19:52 -05004708 int metadata, unsigned long *freed)
4709{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004710 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004711 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004712 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004713 struct ext4_group_desc *gdp;
4714 struct ext4_super_block *es;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004715 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004716 ext4_grpblk_t bit;
4717 struct buffer_head *gd_bh;
4718 ext4_group_t block_group;
4719 struct ext4_sb_info *sbi;
4720 struct ext4_buddy e4b;
4721 int err = 0;
4722 int ret;
4723
4724 *freed = 0;
4725
Alex Tomasc9de5602008-01-29 00:19:52 -05004726 sbi = EXT4_SB(sb);
4727 es = EXT4_SB(sb)->s_es;
4728 if (block < le32_to_cpu(es->s_first_data_block) ||
4729 block + count < block ||
4730 block + count > ext4_blocks_count(es)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004731 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004732 "Freeing blocks not in datazone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004733 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004734 goto error_return;
4735 }
4736
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004737 ext4_debug("freeing block %llu\n", block);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004738 trace_ext4_free_blocks(inode, block, count, metadata);
Alex Tomasc9de5602008-01-29 00:19:52 -05004739
Eric Sandeen256bdb42008-02-10 01:13:33 -05004740 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4741 if (ac) {
4742 ac->ac_op = EXT4_MB_HISTORY_FREE;
4743 ac->ac_inode = inode;
4744 ac->ac_sb = sb;
4745 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004746
4747do_more:
4748 overflow = 0;
4749 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4750
4751 /*
4752 * Check to see if we are freeing blocks across a group
4753 * boundary.
4754 */
4755 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4756 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4757 count -= overflow;
4758 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004759 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004760 if (!bitmap_bh) {
4761 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004762 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004763 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004764 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004765 if (!gdp) {
4766 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004767 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004768 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004769
4770 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4771 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4772 in_range(block, ext4_inode_table(sb, gdp),
4773 EXT4_SB(sb)->s_itb_per_group) ||
4774 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4775 EXT4_SB(sb)->s_itb_per_group)) {
4776
Harvey Harrison46e665e2008-04-17 10:38:59 -04004777 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004778 "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004779 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004780 /* err = 0. ext4_std_error should be a no op */
4781 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004782 }
4783
4784 BUFFER_TRACE(bitmap_bh, "getting write access");
4785 err = ext4_journal_get_write_access(handle, bitmap_bh);
4786 if (err)
4787 goto error_return;
4788
4789 /*
4790 * We are about to modify some metadata. Call the journal APIs
4791 * to unshare ->b_data if a currently-committing transaction is
4792 * using it
4793 */
4794 BUFFER_TRACE(gd_bh, "get_write_access");
4795 err = ext4_journal_get_write_access(handle, gd_bh);
4796 if (err)
4797 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004798#ifdef AGGRESSIVE_CHECK
4799 {
4800 int i;
4801 for (i = 0; i < count; i++)
4802 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4803 }
4804#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004805 if (ac) {
4806 ac->ac_b_ex.fe_group = block_group;
4807 ac->ac_b_ex.fe_start = bit;
4808 ac->ac_b_ex.fe_len = count;
4809 ext4_mb_store_history(ac);
4810 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004811
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004812 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4813 if (err)
4814 goto error_return;
Frank Mayhar03901312009-01-07 00:06:22 -05004815 if (metadata && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004816 struct ext4_free_data *new_entry;
4817 /*
4818 * blocks being freed are metadata. these blocks shouldn't
4819 * be used until this transaction is committed
4820 */
4821 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4822 new_entry->start_blk = bit;
4823 new_entry->group = block_group;
4824 new_entry->count = count;
4825 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004826
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004827 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004828 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004829 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004830 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004831 /* need to update group_info->bb_free and bitmap
4832 * with group lock held. generate_buddy look at
4833 * them with group lock_held
4834 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004835 ext4_lock_group(sb, block_group);
4836 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004837 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004838 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004839 }
4840
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004841 ret = ext4_free_blks_count(sb, gdp) + count;
4842 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004843 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004844 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004845 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4846
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004847 if (sbi->s_log_groups_per_flex) {
4848 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004849 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004850 }
4851
Alex Tomasc9de5602008-01-29 00:19:52 -05004852 ext4_mb_release_desc(&e4b);
4853
4854 *freed += count;
4855
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004856 /* We dirtied the bitmap block */
4857 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4858 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4859
Alex Tomasc9de5602008-01-29 00:19:52 -05004860 /* And the group descriptor block */
4861 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004862 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004863 if (!err)
4864 err = ret;
4865
4866 if (overflow && !err) {
4867 block += count;
4868 count = overflow;
4869 put_bh(bitmap_bh);
4870 goto do_more;
4871 }
4872 sb->s_dirt = 1;
4873error_return:
4874 brelse(bitmap_bh);
4875 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004876 if (ac)
4877 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004878 return;
4879}