blob: b0d6022eaa67ff32aadf81f91916a1617d730da0 [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"
Alex Tomasc9de5602008-01-29 00:19:52 -050025/*
26 * MUSTDO:
27 * - test ext4_ext_search_left() and ext4_ext_search_right()
28 * - search for metadata in few groups
29 *
30 * TODO v4:
31 * - normalization should take into account whether file is still open
32 * - discard preallocations if no free space left (policy?)
33 * - don't normalize tails
34 * - quota
35 * - reservation for superuser
36 *
37 * TODO v3:
38 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
39 * - track min/max extents in each group for better group selection
40 * - mb_mark_used() may allocate chunk right after splitting buddy
41 * - tree of groups sorted by number of free blocks
42 * - error handling
43 */
44
45/*
46 * The allocation request involve request for multiple number of blocks
47 * near to the goal(block) value specified.
48 *
49 * During initialization phase of the allocator we decide to use the group
50 * preallocation or inode preallocation depending on the size file. The
51 * size of the file could be the resulting file size we would have after
52 * allocation or the current file size which ever is larger. If the size is
53 * less that sbi->s_mb_stream_request we select the group
54 * preallocation. The default value of s_mb_stream_request is 16
55 * blocks. This can also be tuned via
56 * /proc/fs/ext4/<partition>/stream_req. The value is represented in terms
57 * of number of blocks.
58 *
59 * The main motivation for having small file use group preallocation is to
60 * ensure that we have small file closer in the disk.
61 *
62 * First stage the allocator looks at the inode prealloc list
63 * ext4_inode_info->i_prealloc_list contain list of prealloc spaces for
64 * this particular inode. The inode prealloc space is represented as:
65 *
66 * pa_lstart -> the logical start block for this prealloc space
67 * pa_pstart -> the physical start block for this prealloc space
68 * pa_len -> lenght for this prealloc space
69 * pa_free -> free space available in this prealloc space
70 *
71 * The inode preallocation space is used looking at the _logical_ start
72 * block. If only the logical file block falls within the range of prealloc
73 * space we will consume the particular prealloc space. This make sure that
74 * that the we have contiguous physical blocks representing the file blocks
75 *
76 * The important thing to be noted in case of inode prealloc space is that
77 * we don't modify the values associated to inode prealloc space except
78 * pa_free.
79 *
80 * If we are not able to find blocks in the inode prealloc space and if we
81 * have the group allocation flag set then we look at the locality group
82 * prealloc space. These are per CPU prealloc list repreasented as
83 *
84 * ext4_sb_info.s_locality_groups[smp_processor_id()]
85 *
86 * The reason for having a per cpu locality group is to reduce the contention
87 * between CPUs. It is possible to get scheduled at this point.
88 *
89 * The locality group prealloc space is used looking at whether we have
90 * enough free space (pa_free) withing the prealloc space.
91 *
92 * If we can't allocate blocks via inode prealloc or/and locality group
93 * prealloc then we look at the buddy cache. The buddy cache is represented
94 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
95 * mapped to the buddy and bitmap information regarding different
96 * groups. The buddy information is attached to buddy cache inode so that
97 * we can access them through the page cache. The information regarding
98 * each group is loaded via ext4_mb_load_buddy. The information involve
99 * block bitmap and buddy information. The information are stored in the
100 * inode as:
101 *
102 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500103 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500104 *
105 *
106 * one block each for bitmap and buddy information. So for each group we
107 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
108 * blocksize) blocks. So it can have information regarding groups_per_page
109 * which is blocks_per_page/2
110 *
111 * The buddy cache inode is not stored on disk. The inode is thrown
112 * away when the filesystem is unmounted.
113 *
114 * We look for count number of blocks in the buddy cache. If we were able
115 * to locate that many free blocks we return with additional information
116 * regarding rest of the contiguous physical block available
117 *
118 * Before allocating blocks via buddy cache we normalize the request
119 * blocks. This ensure we ask for more blocks that we needed. The extra
120 * blocks that we get after allocation is added to the respective prealloc
121 * list. In case of inode preallocation we follow a list of heuristics
122 * based on file size. This can be found in ext4_mb_normalize_request. If
123 * we are doing a group prealloc we try to normalize the request to
124 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is set to
125 * 512 blocks. This can be tuned via
126 * /proc/fs/ext4/<partition/group_prealloc. The value is represented in
127 * terms of number of blocks. If we have mounted the file system with -O
128 * stripe=<value> option the group prealloc request is normalized to the
129 * stripe value (sbi->s_stripe)
130 *
131 * The regular allocator(using the buddy cache) support few tunables.
132 *
133 * /proc/fs/ext4/<partition>/min_to_scan
134 * /proc/fs/ext4/<partition>/max_to_scan
135 * /proc/fs/ext4/<partition>/order2_req
136 *
137 * The regular allocator use buddy scan only if the request len is power of
138 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
139 * value of s_mb_order2_reqs can be tuned via
140 * /proc/fs/ext4/<partition>/order2_req. If the request len is equal to
141 * stripe size (sbi->s_stripe), we try to search for contigous block in
142 * stripe size. This should result in better allocation on RAID setup. If
143 * not we search in the specific group using bitmap for best extents. The
144 * tunable min_to_scan and max_to_scan controll the behaviour here.
145 * min_to_scan indicate how long the mballoc __must__ look for a best
146 * extent and max_to_scanindicate how long the mballoc __can__ look for a
147 * best extent in the found extents. Searching for the blocks starts with
148 * the group specified as the goal value in allocation context via
149 * ac_g_ex. Each group is first checked based on the criteria whether it
150 * can used for allocation. ext4_mb_good_group explains how the groups are
151 * checked.
152 *
153 * Both the prealloc space are getting populated as above. So for the first
154 * request we will hit the buddy cache which will result in this prealloc
155 * space getting filled. The prealloc space is then later used for the
156 * subsequent request.
157 */
158
159/*
160 * mballoc operates on the following data:
161 * - on-disk bitmap
162 * - in-core buddy (actually includes buddy and bitmap)
163 * - preallocation descriptors (PAs)
164 *
165 * there are two types of preallocations:
166 * - inode
167 * assiged to specific inode and can be used for this inode only.
168 * it describes part of inode's space preallocated to specific
169 * physical blocks. any block from that preallocated can be used
170 * independent. the descriptor just tracks number of blocks left
171 * unused. so, before taking some block from descriptor, one must
172 * make sure corresponded logical block isn't allocated yet. this
173 * also means that freeing any block within descriptor's range
174 * must discard all preallocated blocks.
175 * - locality group
176 * assigned to specific locality group which does not translate to
177 * permanent set of inodes: inode can join and leave group. space
178 * from this type of preallocation can be used for any inode. thus
179 * it's consumed from the beginning to the end.
180 *
181 * relation between them can be expressed as:
182 * in-core buddy = on-disk bitmap + preallocation descriptors
183 *
184 * this mean blocks mballoc considers used are:
185 * - allocated blocks (persistent)
186 * - preallocated blocks (non-persistent)
187 *
188 * consistency in mballoc world means that at any time a block is either
189 * free or used in ALL structures. notice: "any time" should not be read
190 * literally -- time is discrete and delimited by locks.
191 *
192 * to keep it simple, we don't use block numbers, instead we count number of
193 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
194 *
195 * all operations can be expressed as:
196 * - init buddy: buddy = on-disk + PAs
197 * - new PA: buddy += N; PA = N
198 * - use inode PA: on-disk += N; PA -= N
199 * - discard inode PA buddy -= on-disk - PA; PA = 0
200 * - use locality group PA on-disk += N; PA -= N
201 * - discard locality group PA buddy -= PA; PA = 0
202 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
203 * is used in real operation because we can't know actual used
204 * bits from PA, only from on-disk bitmap
205 *
206 * if we follow this strict logic, then all operations above should be atomic.
207 * given some of them can block, we'd have to use something like semaphores
208 * killing performance on high-end SMP hardware. let's try to relax it using
209 * the following knowledge:
210 * 1) if buddy is referenced, it's already initialized
211 * 2) while block is used in buddy and the buddy is referenced,
212 * nobody can re-allocate that block
213 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
214 * bit set and PA claims same block, it's OK. IOW, one can set bit in
215 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
216 * block
217 *
218 * so, now we're building a concurrency table:
219 * - init buddy vs.
220 * - new PA
221 * blocks for PA are allocated in the buddy, buddy must be referenced
222 * until PA is linked to allocation group to avoid concurrent buddy init
223 * - use inode PA
224 * we need to make sure that either on-disk bitmap or PA has uptodate data
225 * given (3) we care that PA-=N operation doesn't interfere with init
226 * - discard inode PA
227 * the simplest way would be to have buddy initialized by the discard
228 * - use locality group PA
229 * again PA-=N must be serialized with init
230 * - discard locality group PA
231 * the simplest way would be to have buddy initialized by the discard
232 * - new PA vs.
233 * - use inode PA
234 * i_data_sem serializes them
235 * - discard inode PA
236 * discard process must wait until PA isn't used by another process
237 * - use locality group PA
238 * some mutex should serialize them
239 * - discard locality group PA
240 * discard process must wait until PA isn't used by another process
241 * - use inode PA
242 * - use inode PA
243 * i_data_sem or another mutex should serializes them
244 * - discard inode PA
245 * discard process must wait until PA isn't used by another process
246 * - use locality group PA
247 * nothing wrong here -- they're different PAs covering different blocks
248 * - discard locality group PA
249 * discard process must wait until PA isn't used by another process
250 *
251 * now we're ready to make few consequences:
252 * - PA is referenced and while it is no discard is possible
253 * - PA is referenced until block isn't marked in on-disk bitmap
254 * - PA changes only after on-disk bitmap
255 * - discard must not compete with init. either init is done before
256 * any discard or they're serialized somehow
257 * - buddy init as sum of on-disk bitmap and PAs is done atomically
258 *
259 * a special case when we've used PA to emptiness. no need to modify buddy
260 * in this case, but we should care about concurrent init
261 *
262 */
263
264 /*
265 * Logic in few words:
266 *
267 * - allocation:
268 * load group
269 * find blocks
270 * mark bits in on-disk bitmap
271 * release group
272 *
273 * - use preallocation:
274 * find proper PA (per-inode or group)
275 * load group
276 * mark bits in on-disk bitmap
277 * release group
278 * release PA
279 *
280 * - free:
281 * load group
282 * mark bits in on-disk bitmap
283 * release group
284 *
285 * - discard preallocations in group:
286 * mark PAs deleted
287 * move them onto local list
288 * load on-disk bitmap
289 * load group
290 * remove PA from object (inode or locality group)
291 * mark free blocks in-core
292 *
293 * - discard inode's preallocations:
294 */
295
296/*
297 * Locking rules
298 *
299 * Locks:
300 * - bitlock on a group (group)
301 * - object (inode/locality) (object)
302 * - per-pa lock (pa)
303 *
304 * Paths:
305 * - new pa
306 * object
307 * group
308 *
309 * - find and use pa:
310 * pa
311 *
312 * - release consumed pa:
313 * pa
314 * group
315 * object
316 *
317 * - generate in-core bitmap:
318 * group
319 * pa
320 *
321 * - discard all for given object (inode, locality group):
322 * object
323 * pa
324 * group
325 *
326 * - discard all for given group:
327 * group
328 * pa
329 * group
330 * object
331 *
332 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500333static struct kmem_cache *ext4_pspace_cachep;
334static struct kmem_cache *ext4_ac_cachep;
335static struct kmem_cache *ext4_free_ext_cachep;
336static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
337 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500338static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
339 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500340static int ext4_mb_init_per_dev_proc(struct super_block *sb);
341static int ext4_mb_destroy_per_dev_proc(struct super_block *sb);
342static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
343
344
Alex Tomasc9de5602008-01-29 00:19:52 -0500345
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
376static inline void mb_set_bit_atomic(spinlock_t *lock, 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_set_bit_atomic(lock, bit, addr);
380}
381
382static inline void mb_clear_bit(int bit, void *addr)
383{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500384 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500385 ext4_clear_bit(bit, addr);
386}
387
388static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
389{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500390 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500391 ext4_clear_bit_atomic(lock, bit, addr);
392}
393
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394static inline int mb_find_next_zero_bit(void *addr, int max, int start)
395{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400396 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500397 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 start += fix;
400
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400401 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
402 if (ret > max)
403 return max;
404 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500405}
406
407static inline int mb_find_next_bit(void *addr, int max, int start)
408{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400409 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500410 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400411 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500412 start += fix;
413
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400414 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
415 if (ret > max)
416 return max;
417 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500418}
419
Alex Tomasc9de5602008-01-29 00:19:52 -0500420static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
421{
422 char *bb;
423
Alex Tomasc9de5602008-01-29 00:19:52 -0500424 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
425 BUG_ON(max == NULL);
426
427 if (order > e4b->bd_blkbits + 1) {
428 *max = 0;
429 return NULL;
430 }
431
432 /* at order 0 we see each particular block */
433 *max = 1 << (e4b->bd_blkbits + 3);
434 if (order == 0)
435 return EXT4_MB_BITMAP(e4b);
436
437 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
438 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
439
440 return bb;
441}
442
443#ifdef DOUBLE_CHECK
444static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
445 int first, int count)
446{
447 int i;
448 struct super_block *sb = e4b->bd_sb;
449
450 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
451 return;
452 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
453 for (i = 0; i < count; i++) {
454 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
455 ext4_fsblk_t blocknr;
456 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
457 blocknr += first + i;
458 blocknr +=
459 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500460 ext4_grp_locked_error(sb, e4b->bd_group,
461 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500462 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500463 inode ? inode->i_ino : 0, blocknr,
464 first + i, e4b->bd_group);
465 }
466 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
467 }
468}
469
470static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
471{
472 int i;
473
474 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
475 return;
476 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
477 for (i = 0; i < count; i++) {
478 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
479 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
480 }
481}
482
483static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
484{
485 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
486 unsigned char *b1, *b2;
487 int i;
488 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
489 b2 = (unsigned char *) bitmap;
490 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
491 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500492 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o47760042008-09-08 23:00:52 -0400493 "at byte %u(%u): %x in copy != %x "
494 "on disk/prealloc\n",
495 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500496 BUG();
497 }
498 }
499 }
500}
501
502#else
503static inline void mb_free_blocks_double(struct inode *inode,
504 struct ext4_buddy *e4b, int first, int count)
505{
506 return;
507}
508static inline void mb_mark_used_double(struct ext4_buddy *e4b,
509 int first, int count)
510{
511 return;
512}
513static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
514{
515 return;
516}
517#endif
518
519#ifdef AGGRESSIVE_CHECK
520
521#define MB_CHECK_ASSERT(assert) \
522do { \
523 if (!(assert)) { \
524 printk(KERN_EMERG \
525 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
526 function, file, line, # assert); \
527 BUG(); \
528 } \
529} while (0)
530
531static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
532 const char *function, int line)
533{
534 struct super_block *sb = e4b->bd_sb;
535 int order = e4b->bd_blkbits + 1;
536 int max;
537 int max2;
538 int i;
539 int j;
540 int k;
541 int count;
542 struct ext4_group_info *grp;
543 int fragments = 0;
544 int fstart;
545 struct list_head *cur;
546 void *buddy;
547 void *buddy2;
548
Alex Tomasc9de5602008-01-29 00:19:52 -0500549 {
550 static int mb_check_counter;
551 if (mb_check_counter++ % 100 != 0)
552 return 0;
553 }
554
555 while (order > 1) {
556 buddy = mb_find_buddy(e4b, order, &max);
557 MB_CHECK_ASSERT(buddy);
558 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
559 MB_CHECK_ASSERT(buddy2);
560 MB_CHECK_ASSERT(buddy != buddy2);
561 MB_CHECK_ASSERT(max * 2 == max2);
562
563 count = 0;
564 for (i = 0; i < max; i++) {
565
566 if (mb_test_bit(i, buddy)) {
567 /* only single bit in buddy2 may be 1 */
568 if (!mb_test_bit(i << 1, buddy2)) {
569 MB_CHECK_ASSERT(
570 mb_test_bit((i<<1)+1, buddy2));
571 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
572 MB_CHECK_ASSERT(
573 mb_test_bit(i << 1, buddy2));
574 }
575 continue;
576 }
577
578 /* both bits in buddy2 must be 0 */
579 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
580 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
581
582 for (j = 0; j < (1 << order); j++) {
583 k = (i * (1 << order)) + j;
584 MB_CHECK_ASSERT(
585 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
586 }
587 count++;
588 }
589 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
590 order--;
591 }
592
593 fstart = -1;
594 buddy = mb_find_buddy(e4b, 0, &max);
595 for (i = 0; i < max; i++) {
596 if (!mb_test_bit(i, buddy)) {
597 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
598 if (fstart == -1) {
599 fragments++;
600 fstart = i;
601 }
602 continue;
603 }
604 fstart = -1;
605 /* check used bits only */
606 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
607 buddy2 = mb_find_buddy(e4b, j, &max2);
608 k = i >> j;
609 MB_CHECK_ASSERT(k < max2);
610 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
611 }
612 }
613 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
614 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
615
616 grp = ext4_get_group_info(sb, e4b->bd_group);
617 buddy = mb_find_buddy(e4b, 0, &max);
618 list_for_each(cur, &grp->bb_prealloc_list) {
619 ext4_group_t groupnr;
620 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400621 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
622 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500623 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400624 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500625 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
626 }
627 return 0;
628}
629#undef MB_CHECK_ASSERT
630#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400631 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500632#else
633#define mb_check_buddy(e4b)
634#endif
635
636/* FIXME!! need more doc */
637static void ext4_mb_mark_free_simple(struct super_block *sb,
638 void *buddy, unsigned first, int len,
639 struct ext4_group_info *grp)
640{
641 struct ext4_sb_info *sbi = EXT4_SB(sb);
642 unsigned short min;
643 unsigned short max;
644 unsigned short chunk;
645 unsigned short border;
646
Valerie Clementb73fce62008-02-15 13:48:51 -0500647 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500648
649 border = 2 << sb->s_blocksize_bits;
650
651 while (len > 0) {
652 /* find how many blocks can be covered since this position */
653 max = ffs(first | border) - 1;
654
655 /* find how many blocks of power 2 we need to mark */
656 min = fls(len) - 1;
657
658 if (max < min)
659 min = max;
660 chunk = 1 << min;
661
662 /* mark multiblock chunks only */
663 grp->bb_counters[min]++;
664 if (min > 0)
665 mb_clear_bit(first >> min,
666 buddy + sbi->s_mb_offsets[min]);
667
668 len -= chunk;
669 first += chunk;
670 }
671}
672
673static void ext4_mb_generate_buddy(struct super_block *sb,
674 void *buddy, void *bitmap, ext4_group_t group)
675{
676 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
677 unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
678 unsigned short i = 0;
679 unsigned short first;
680 unsigned short len;
681 unsigned free = 0;
682 unsigned fragments = 0;
683 unsigned long long period = get_cycles();
684
685 /* initialize buddy from bitmap which is aggregation
686 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500687 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500688 grp->bb_first_free = i;
689 while (i < max) {
690 fragments++;
691 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500692 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500693 len = i - first;
694 free += len;
695 if (len > 1)
696 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
697 else
698 grp->bb_counters[0]++;
699 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500700 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500701 }
702 grp->bb_fragments = fragments;
703
704 if (free != grp->bb_free) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500705 ext4_grp_locked_error(sb, group, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500706 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
Alex Tomasc9de5602008-01-29 00:19:52 -0500707 group, free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500708 /*
709 * If we intent to continue, we consider group descritor
710 * corrupt and update bb_free using bitmap value
711 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500712 grp->bb_free = free;
713 }
714
715 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
716
717 period = get_cycles() - period;
718 spin_lock(&EXT4_SB(sb)->s_bal_lock);
719 EXT4_SB(sb)->s_mb_buddies_generated++;
720 EXT4_SB(sb)->s_mb_generation_time += period;
721 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
722}
723
724/* The buddy information is attached the buddy cache inode
725 * for convenience. The information regarding each group
726 * is loaded via ext4_mb_load_buddy. The information involve
727 * block bitmap and buddy information. The information are
728 * stored in the inode as
729 *
730 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500731 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500732 *
733 *
734 * one block each for bitmap and buddy information.
735 * So for each group we take up 2 blocks. A page can
736 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
737 * So it can have information regarding groups_per_page which
738 * is blocks_per_page/2
739 */
740
741static int ext4_mb_init_cache(struct page *page, char *incore)
742{
743 int blocksize;
744 int blocks_per_page;
745 int groups_per_page;
746 int err = 0;
747 int i;
748 ext4_group_t first_group;
749 int first_block;
750 struct super_block *sb;
751 struct buffer_head *bhs;
752 struct buffer_head **bh;
753 struct inode *inode;
754 char *data;
755 char *bitmap;
756
757 mb_debug("init page %lu\n", page->index);
758
759 inode = page->mapping->host;
760 sb = inode->i_sb;
761 blocksize = 1 << inode->i_blkbits;
762 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
763
764 groups_per_page = blocks_per_page >> 1;
765 if (groups_per_page == 0)
766 groups_per_page = 1;
767
768 /* allocate buffer_heads to read bitmaps */
769 if (groups_per_page > 1) {
770 err = -ENOMEM;
771 i = sizeof(struct buffer_head *) * groups_per_page;
772 bh = kzalloc(i, GFP_NOFS);
773 if (bh == NULL)
774 goto out;
775 } else
776 bh = &bhs;
777
778 first_group = page->index * blocks_per_page / 2;
779
780 /* read all groups the page covers into the cache */
781 for (i = 0; i < groups_per_page; i++) {
782 struct ext4_group_desc *desc;
783
784 if (first_group + i >= EXT4_SB(sb)->s_groups_count)
785 break;
786
787 err = -EIO;
788 desc = ext4_get_group_desc(sb, first_group + i, NULL);
789 if (desc == NULL)
790 goto out;
791
792 err = -ENOMEM;
793 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
794 if (bh[i] == NULL)
795 goto out;
796
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500797 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500798 continue;
799
Frederic Bohec806e682008-10-10 08:09:18 -0400800 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500801 if (bitmap_uptodate(bh[i])) {
802 unlock_buffer(bh[i]);
803 continue;
804 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400805 spin_lock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500806 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
807 ext4_init_block_bitmap(sb, bh[i],
808 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500809 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 set_buffer_uptodate(bh[i]);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400811 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500812 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500813 continue;
814 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400815 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500816 if (buffer_uptodate(bh[i])) {
817 /*
818 * if not uninit if bh is uptodate,
819 * bitmap is also uptodate
820 */
821 set_bitmap_uptodate(bh[i]);
822 unlock_buffer(bh[i]);
823 continue;
824 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500825 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500826 /*
827 * submit the buffer_head for read. We can
828 * safely mark the bitmap as uptodate now.
829 * We do it here so the bitmap uptodate bit
830 * get set with buffer lock held.
831 */
832 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 bh[i]->b_end_io = end_buffer_read_sync;
834 submit_bh(READ, bh[i]);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500835 mb_debug("read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500836 }
837
838 /* wait for I/O completion */
839 for (i = 0; i < groups_per_page && bh[i]; i++)
840 wait_on_buffer(bh[i]);
841
842 err = -EIO;
843 for (i = 0; i < groups_per_page && bh[i]; i++)
844 if (!buffer_uptodate(bh[i]))
845 goto out;
846
Mingming Cao31b481d2008-07-11 19:27:31 -0400847 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500849 /* init the page */
850 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500851 for (i = 0; i < blocks_per_page; i++) {
852 int group;
853 struct ext4_group_info *grinfo;
854
855 group = (first_block + i) >> 1;
856 if (group >= EXT4_SB(sb)->s_groups_count)
857 break;
858
859 /*
860 * data carry information regarding this
861 * particular group in the format specified
862 * above
863 *
864 */
865 data = page_address(page) + (i * blocksize);
866 bitmap = bh[group - first_group]->b_data;
867
868 /*
869 * We place the buddy block and bitmap block
870 * close together
871 */
872 if ((first_block + i) & 1) {
873 /* this is block of buddy */
874 BUG_ON(incore == NULL);
875 mb_debug("put buddy for group %u in page %lu/%x\n",
876 group, page->index, i * blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500877 grinfo = ext4_get_group_info(sb, group);
878 grinfo->bb_fragments = 0;
879 memset(grinfo->bb_counters, 0,
880 sizeof(unsigned short)*(sb->s_blocksize_bits+2));
881 /*
882 * incore got set to the group block bitmap below
883 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500884 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500885 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500886 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500887 incore = NULL;
888 } else {
889 /* this is block of bitmap */
890 BUG_ON(incore != NULL);
891 mb_debug("put bitmap for group %u in page %lu/%x\n",
892 group, page->index, i * blocksize);
893
894 /* see comments in ext4_mb_put_pa() */
895 ext4_lock_group(sb, group);
896 memcpy(data, bitmap, blocksize);
897
898 /* mark all preallocated blks used in in-core bitmap */
899 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500900 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500901 ext4_unlock_group(sb, group);
902
903 /* set incore so that the buddy information can be
904 * generated using this
905 */
906 incore = data;
907 }
908 }
909 SetPageUptodate(page);
910
911out:
912 if (bh) {
913 for (i = 0; i < groups_per_page && bh[i]; i++)
914 brelse(bh[i]);
915 if (bh != &bhs)
916 kfree(bh);
917 }
918 return err;
919}
920
Eric Sandeen4ddfef72008-04-29 08:11:12 -0400921static noinline_for_stack int
922ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
923 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -0500924{
Alex Tomasc9de5602008-01-29 00:19:52 -0500925 int blocks_per_page;
926 int block;
927 int pnum;
928 int poff;
929 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400930 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500931 struct ext4_group_info *grp;
932 struct ext4_sb_info *sbi = EXT4_SB(sb);
933 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -0500934
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500935 mb_debug("load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500936
937 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500938 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500939
940 e4b->bd_blkbits = sb->s_blocksize_bits;
941 e4b->bd_info = ext4_get_group_info(sb, group);
942 e4b->bd_sb = sb;
943 e4b->bd_group = group;
944 e4b->bd_buddy_page = NULL;
945 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500946 e4b->alloc_semp = &grp->alloc_sem;
947
948 /* Take the read lock on the group alloc
949 * sem. This would make sure a parallel
950 * ext4_mb_init_group happening on other
951 * groups mapped by the page is blocked
952 * till we are done with allocation
953 */
954 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500955
956 /*
957 * the buddy cache inode stores the block bitmap
958 * and buddy information in consecutive blocks.
959 * So for each group we need two blocks.
960 */
961 block = group * 2;
962 pnum = block / blocks_per_page;
963 poff = block % blocks_per_page;
964
965 /* we could use find_or_create_page(), but it locks page
966 * what we'd like to avoid in fast path ... */
967 page = find_get_page(inode->i_mapping, pnum);
968 if (page == NULL || !PageUptodate(page)) {
969 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500970 /*
971 * drop the page reference and try
972 * to get the page with lock. If we
973 * are not uptodate that implies
974 * somebody just created the page but
975 * is yet to initialize the same. So
976 * wait for it to initialize.
977 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500978 page_cache_release(page);
979 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
980 if (page) {
981 BUG_ON(page->mapping != inode->i_mapping);
982 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400983 ret = ext4_mb_init_cache(page, NULL);
984 if (ret) {
985 unlock_page(page);
986 goto err;
987 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500988 mb_cmp_bitmaps(e4b, page_address(page) +
989 (poff * sb->s_blocksize));
990 }
991 unlock_page(page);
992 }
993 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400994 if (page == NULL || !PageUptodate(page)) {
995 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500996 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400997 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500998 e4b->bd_bitmap_page = page;
999 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1000 mark_page_accessed(page);
1001
1002 block++;
1003 pnum = block / blocks_per_page;
1004 poff = block % blocks_per_page;
1005
1006 page = find_get_page(inode->i_mapping, pnum);
1007 if (page == NULL || !PageUptodate(page)) {
1008 if (page)
1009 page_cache_release(page);
1010 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1011 if (page) {
1012 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001013 if (!PageUptodate(page)) {
1014 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1015 if (ret) {
1016 unlock_page(page);
1017 goto err;
1018 }
1019 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001020 unlock_page(page);
1021 }
1022 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001023 if (page == NULL || !PageUptodate(page)) {
1024 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001025 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001026 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001027 e4b->bd_buddy_page = page;
1028 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1029 mark_page_accessed(page);
1030
1031 BUG_ON(e4b->bd_bitmap_page == NULL);
1032 BUG_ON(e4b->bd_buddy_page == NULL);
1033
1034 return 0;
1035
1036err:
1037 if (e4b->bd_bitmap_page)
1038 page_cache_release(e4b->bd_bitmap_page);
1039 if (e4b->bd_buddy_page)
1040 page_cache_release(e4b->bd_buddy_page);
1041 e4b->bd_buddy = NULL;
1042 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001043
1044 /* Done with the buddy cache */
1045 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001046 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001047}
1048
1049static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1050{
1051 if (e4b->bd_bitmap_page)
1052 page_cache_release(e4b->bd_bitmap_page);
1053 if (e4b->bd_buddy_page)
1054 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001055 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001056 if (e4b->alloc_semp)
1057 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001058}
1059
1060
1061static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1062{
1063 int order = 1;
1064 void *bb;
1065
1066 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1067 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1068
1069 bb = EXT4_MB_BUDDY(e4b);
1070 while (order <= e4b->bd_blkbits + 1) {
1071 block = block >> 1;
1072 if (!mb_test_bit(block, bb)) {
1073 /* this block is part of buddy of order 'order' */
1074 return order;
1075 }
1076 bb += 1 << (e4b->bd_blkbits - order);
1077 order++;
1078 }
1079 return 0;
1080}
1081
1082static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1083{
1084 __u32 *addr;
1085
1086 len = cur + len;
1087 while (cur < len) {
1088 if ((cur & 31) == 0 && (len - cur) >= 32) {
1089 /* fast path: clear whole word at once */
1090 addr = bm + (cur >> 3);
1091 *addr = 0;
1092 cur += 32;
1093 continue;
1094 }
Aneesh Kumar K.Ve8134b22009-01-05 21:38:26 -05001095 if (lock)
1096 mb_clear_bit_atomic(lock, cur, bm);
1097 else
1098 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001099 cur++;
1100 }
1101}
1102
1103static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1104{
1105 __u32 *addr;
1106
1107 len = cur + len;
1108 while (cur < len) {
1109 if ((cur & 31) == 0 && (len - cur) >= 32) {
1110 /* fast path: set whole word at once */
1111 addr = bm + (cur >> 3);
1112 *addr = 0xffffffff;
1113 cur += 32;
1114 continue;
1115 }
Aneesh Kumar K.Ve8134b22009-01-05 21:38:26 -05001116 if (lock)
1117 mb_set_bit_atomic(lock, cur, bm);
1118 else
1119 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001120 cur++;
1121 }
1122}
1123
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001124static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001125 int first, int count)
1126{
1127 int block = 0;
1128 int max = 0;
1129 int order;
1130 void *buddy;
1131 void *buddy2;
1132 struct super_block *sb = e4b->bd_sb;
1133
1134 BUG_ON(first + count > (sb->s_blocksize << 3));
1135 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
1136 mb_check_buddy(e4b);
1137 mb_free_blocks_double(inode, e4b, first, count);
1138
1139 e4b->bd_info->bb_free += count;
1140 if (first < e4b->bd_info->bb_first_free)
1141 e4b->bd_info->bb_first_free = first;
1142
1143 /* let's maintain fragments counter */
1144 if (first != 0)
1145 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1146 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1147 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1148 if (block && max)
1149 e4b->bd_info->bb_fragments--;
1150 else if (!block && !max)
1151 e4b->bd_info->bb_fragments++;
1152
1153 /* let's maintain buddy itself */
1154 while (count-- > 0) {
1155 block = first++;
1156 order = 0;
1157
1158 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1159 ext4_fsblk_t blocknr;
1160 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1161 blocknr += block;
1162 blocknr +=
1163 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001164 ext4_grp_locked_error(sb, e4b->bd_group,
1165 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001166 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001167 inode ? inode->i_ino : 0, blocknr, block,
1168 e4b->bd_group);
1169 }
1170 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1171 e4b->bd_info->bb_counters[order]++;
1172
1173 /* start of the buddy */
1174 buddy = mb_find_buddy(e4b, order, &max);
1175
1176 do {
1177 block &= ~1UL;
1178 if (mb_test_bit(block, buddy) ||
1179 mb_test_bit(block + 1, buddy))
1180 break;
1181
1182 /* both the buddies are free, try to coalesce them */
1183 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1184
1185 if (!buddy2)
1186 break;
1187
1188 if (order > 0) {
1189 /* for special purposes, we don't set
1190 * free bits in bitmap */
1191 mb_set_bit(block, buddy);
1192 mb_set_bit(block + 1, buddy);
1193 }
1194 e4b->bd_info->bb_counters[order]--;
1195 e4b->bd_info->bb_counters[order]--;
1196
1197 block = block >> 1;
1198 order++;
1199 e4b->bd_info->bb_counters[order]++;
1200
1201 mb_clear_bit(block, buddy2);
1202 buddy = buddy2;
1203 } while (1);
1204 }
1205 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001206}
1207
1208static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1209 int needed, struct ext4_free_extent *ex)
1210{
1211 int next = block;
1212 int max;
1213 int ord;
1214 void *buddy;
1215
1216 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1217 BUG_ON(ex == NULL);
1218
1219 buddy = mb_find_buddy(e4b, order, &max);
1220 BUG_ON(buddy == NULL);
1221 BUG_ON(block >= max);
1222 if (mb_test_bit(block, buddy)) {
1223 ex->fe_len = 0;
1224 ex->fe_start = 0;
1225 ex->fe_group = 0;
1226 return 0;
1227 }
1228
1229 /* FIXME dorp order completely ? */
1230 if (likely(order == 0)) {
1231 /* find actual order */
1232 order = mb_find_order_for_block(e4b, block);
1233 block = block >> order;
1234 }
1235
1236 ex->fe_len = 1 << order;
1237 ex->fe_start = block << order;
1238 ex->fe_group = e4b->bd_group;
1239
1240 /* calc difference from given start */
1241 next = next - ex->fe_start;
1242 ex->fe_len -= next;
1243 ex->fe_start += next;
1244
1245 while (needed > ex->fe_len &&
1246 (buddy = mb_find_buddy(e4b, order, &max))) {
1247
1248 if (block + 1 >= max)
1249 break;
1250
1251 next = (block + 1) * (1 << order);
1252 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1253 break;
1254
1255 ord = mb_find_order_for_block(e4b, next);
1256
1257 order = ord;
1258 block = next >> order;
1259 ex->fe_len += 1 << order;
1260 }
1261
1262 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1263 return ex->fe_len;
1264}
1265
1266static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1267{
1268 int ord;
1269 int mlen = 0;
1270 int max = 0;
1271 int cur;
1272 int start = ex->fe_start;
1273 int len = ex->fe_len;
1274 unsigned ret = 0;
1275 int len0 = len;
1276 void *buddy;
1277
1278 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1279 BUG_ON(e4b->bd_group != ex->fe_group);
1280 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1281 mb_check_buddy(e4b);
1282 mb_mark_used_double(e4b, start, len);
1283
1284 e4b->bd_info->bb_free -= len;
1285 if (e4b->bd_info->bb_first_free == start)
1286 e4b->bd_info->bb_first_free += len;
1287
1288 /* let's maintain fragments counter */
1289 if (start != 0)
1290 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1291 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1292 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1293 if (mlen && max)
1294 e4b->bd_info->bb_fragments++;
1295 else if (!mlen && !max)
1296 e4b->bd_info->bb_fragments--;
1297
1298 /* let's maintain buddy itself */
1299 while (len) {
1300 ord = mb_find_order_for_block(e4b, start);
1301
1302 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1303 /* the whole chunk may be allocated at once! */
1304 mlen = 1 << ord;
1305 buddy = mb_find_buddy(e4b, ord, &max);
1306 BUG_ON((start >> ord) >= max);
1307 mb_set_bit(start >> ord, buddy);
1308 e4b->bd_info->bb_counters[ord]--;
1309 start += mlen;
1310 len -= mlen;
1311 BUG_ON(len < 0);
1312 continue;
1313 }
1314
1315 /* store for history */
1316 if (ret == 0)
1317 ret = len | (ord << 16);
1318
1319 /* we have to split large buddy */
1320 BUG_ON(ord <= 0);
1321 buddy = mb_find_buddy(e4b, ord, &max);
1322 mb_set_bit(start >> ord, buddy);
1323 e4b->bd_info->bb_counters[ord]--;
1324
1325 ord--;
1326 cur = (start >> ord) & ~1U;
1327 buddy = mb_find_buddy(e4b, ord, &max);
1328 mb_clear_bit(cur, buddy);
1329 mb_clear_bit(cur + 1, buddy);
1330 e4b->bd_info->bb_counters[ord]++;
1331 e4b->bd_info->bb_counters[ord]++;
1332 }
1333
1334 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
1335 EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1336 mb_check_buddy(e4b);
1337
1338 return ret;
1339}
1340
1341/*
1342 * Must be called under group lock!
1343 */
1344static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1345 struct ext4_buddy *e4b)
1346{
1347 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1348 int ret;
1349
1350 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1351 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1352
1353 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1354 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1355 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1356
1357 /* preallocation can change ac_b_ex, thus we store actually
1358 * allocated blocks for history */
1359 ac->ac_f_ex = ac->ac_b_ex;
1360
1361 ac->ac_status = AC_STATUS_FOUND;
1362 ac->ac_tail = ret & 0xffff;
1363 ac->ac_buddy = ret >> 16;
1364
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001365 /*
1366 * take the page reference. We want the page to be pinned
1367 * so that we don't get a ext4_mb_init_cache_call for this
1368 * group until we update the bitmap. That would mean we
1369 * double allocate blocks. The reference is dropped
1370 * in ext4_mb_release_context
1371 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001372 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1373 get_page(ac->ac_bitmap_page);
1374 ac->ac_buddy_page = e4b->bd_buddy_page;
1375 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001376 /* on allocation we use ac to track the held semaphore */
1377 ac->alloc_semp = e4b->alloc_semp;
1378 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001379 /* store last allocated for subsequent stream allocation */
1380 if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
1381 spin_lock(&sbi->s_md_lock);
1382 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1383 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1384 spin_unlock(&sbi->s_md_lock);
1385 }
1386}
1387
1388/*
1389 * regular allocator, for general purposes allocation
1390 */
1391
1392static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1393 struct ext4_buddy *e4b,
1394 int finish_group)
1395{
1396 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1397 struct ext4_free_extent *bex = &ac->ac_b_ex;
1398 struct ext4_free_extent *gex = &ac->ac_g_ex;
1399 struct ext4_free_extent ex;
1400 int max;
1401
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001402 if (ac->ac_status == AC_STATUS_FOUND)
1403 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001404 /*
1405 * We don't want to scan for a whole year
1406 */
1407 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1408 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1409 ac->ac_status = AC_STATUS_BREAK;
1410 return;
1411 }
1412
1413 /*
1414 * Haven't found good chunk so far, let's continue
1415 */
1416 if (bex->fe_len < gex->fe_len)
1417 return;
1418
1419 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1420 && bex->fe_group == e4b->bd_group) {
1421 /* recheck chunk's availability - we don't know
1422 * when it was found (within this lock-unlock
1423 * period or not) */
1424 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1425 if (max >= gex->fe_len) {
1426 ext4_mb_use_best_found(ac, e4b);
1427 return;
1428 }
1429 }
1430}
1431
1432/*
1433 * The routine checks whether found extent is good enough. If it is,
1434 * then the extent gets marked used and flag is set to the context
1435 * to stop scanning. Otherwise, the extent is compared with the
1436 * previous found extent and if new one is better, then it's stored
1437 * in the context. Later, the best found extent will be used, if
1438 * mballoc can't find good enough extent.
1439 *
1440 * FIXME: real allocation policy is to be designed yet!
1441 */
1442static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1443 struct ext4_free_extent *ex,
1444 struct ext4_buddy *e4b)
1445{
1446 struct ext4_free_extent *bex = &ac->ac_b_ex;
1447 struct ext4_free_extent *gex = &ac->ac_g_ex;
1448
1449 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001450 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001451 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1452 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1453
1454 ac->ac_found++;
1455
1456 /*
1457 * The special case - take what you catch first
1458 */
1459 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1460 *bex = *ex;
1461 ext4_mb_use_best_found(ac, e4b);
1462 return;
1463 }
1464
1465 /*
1466 * Let's check whether the chuck is good enough
1467 */
1468 if (ex->fe_len == gex->fe_len) {
1469 *bex = *ex;
1470 ext4_mb_use_best_found(ac, e4b);
1471 return;
1472 }
1473
1474 /*
1475 * If this is first found extent, just store it in the context
1476 */
1477 if (bex->fe_len == 0) {
1478 *bex = *ex;
1479 return;
1480 }
1481
1482 /*
1483 * If new found extent is better, store it in the context
1484 */
1485 if (bex->fe_len < gex->fe_len) {
1486 /* if the request isn't satisfied, any found extent
1487 * larger than previous best one is better */
1488 if (ex->fe_len > bex->fe_len)
1489 *bex = *ex;
1490 } else if (ex->fe_len > gex->fe_len) {
1491 /* if the request is satisfied, then we try to find
1492 * an extent that still satisfy the request, but is
1493 * smaller than previous one */
1494 if (ex->fe_len < bex->fe_len)
1495 *bex = *ex;
1496 }
1497
1498 ext4_mb_check_limits(ac, e4b, 0);
1499}
1500
1501static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1502 struct ext4_buddy *e4b)
1503{
1504 struct ext4_free_extent ex = ac->ac_b_ex;
1505 ext4_group_t group = ex.fe_group;
1506 int max;
1507 int err;
1508
1509 BUG_ON(ex.fe_len <= 0);
1510 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1511 if (err)
1512 return err;
1513
1514 ext4_lock_group(ac->ac_sb, group);
1515 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1516
1517 if (max > 0) {
1518 ac->ac_b_ex = ex;
1519 ext4_mb_use_best_found(ac, e4b);
1520 }
1521
1522 ext4_unlock_group(ac->ac_sb, group);
1523 ext4_mb_release_desc(e4b);
1524
1525 return 0;
1526}
1527
1528static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1529 struct ext4_buddy *e4b)
1530{
1531 ext4_group_t group = ac->ac_g_ex.fe_group;
1532 int max;
1533 int err;
1534 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1535 struct ext4_super_block *es = sbi->s_es;
1536 struct ext4_free_extent ex;
1537
1538 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1539 return 0;
1540
1541 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1542 if (err)
1543 return err;
1544
1545 ext4_lock_group(ac->ac_sb, group);
1546 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1547 ac->ac_g_ex.fe_len, &ex);
1548
1549 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1550 ext4_fsblk_t start;
1551
1552 start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1553 ex.fe_start + le32_to_cpu(es->s_first_data_block);
1554 /* use do_div to get remainder (would be 64-bit modulo) */
1555 if (do_div(start, sbi->s_stripe) == 0) {
1556 ac->ac_found++;
1557 ac->ac_b_ex = ex;
1558 ext4_mb_use_best_found(ac, e4b);
1559 }
1560 } else if (max >= ac->ac_g_ex.fe_len) {
1561 BUG_ON(ex.fe_len <= 0);
1562 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1563 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1564 ac->ac_found++;
1565 ac->ac_b_ex = ex;
1566 ext4_mb_use_best_found(ac, e4b);
1567 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1568 /* Sometimes, caller may want to merge even small
1569 * number of blocks to an existing extent */
1570 BUG_ON(ex.fe_len <= 0);
1571 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1572 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1573 ac->ac_found++;
1574 ac->ac_b_ex = ex;
1575 ext4_mb_use_best_found(ac, e4b);
1576 }
1577 ext4_unlock_group(ac->ac_sb, group);
1578 ext4_mb_release_desc(e4b);
1579
1580 return 0;
1581}
1582
1583/*
1584 * The routine scans buddy structures (not bitmap!) from given order
1585 * to max order and tries to find big enough chunk to satisfy the req
1586 */
1587static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1588 struct ext4_buddy *e4b)
1589{
1590 struct super_block *sb = ac->ac_sb;
1591 struct ext4_group_info *grp = e4b->bd_info;
1592 void *buddy;
1593 int i;
1594 int k;
1595 int max;
1596
1597 BUG_ON(ac->ac_2order <= 0);
1598 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1599 if (grp->bb_counters[i] == 0)
1600 continue;
1601
1602 buddy = mb_find_buddy(e4b, i, &max);
1603 BUG_ON(buddy == NULL);
1604
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001605 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001606 BUG_ON(k >= max);
1607
1608 ac->ac_found++;
1609
1610 ac->ac_b_ex.fe_len = 1 << i;
1611 ac->ac_b_ex.fe_start = k << i;
1612 ac->ac_b_ex.fe_group = e4b->bd_group;
1613
1614 ext4_mb_use_best_found(ac, e4b);
1615
1616 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1617
1618 if (EXT4_SB(sb)->s_mb_stats)
1619 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1620
1621 break;
1622 }
1623}
1624
1625/*
1626 * The routine scans the group and measures all found extents.
1627 * In order to optimize scanning, caller must pass number of
1628 * free blocks in the group, so the routine can know upper limit.
1629 */
1630static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1631 struct ext4_buddy *e4b)
1632{
1633 struct super_block *sb = ac->ac_sb;
1634 void *bitmap = EXT4_MB_BITMAP(e4b);
1635 struct ext4_free_extent ex;
1636 int i;
1637 int free;
1638
1639 free = e4b->bd_info->bb_free;
1640 BUG_ON(free <= 0);
1641
1642 i = e4b->bd_info->bb_first_free;
1643
1644 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001645 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001646 EXT4_BLOCKS_PER_GROUP(sb), i);
1647 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001648 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001649 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001650 * free blocks even though group info says we
1651 * we have free blocks
1652 */
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001653 ext4_grp_locked_error(sb, e4b->bd_group,
1654 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001655 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001656 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001657 break;
1658 }
1659
1660 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1661 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001662 if (free < ex.fe_len) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001663 ext4_grp_locked_error(sb, e4b->bd_group,
1664 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001665 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001666 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001667 /*
1668 * The number of free blocks differs. This mostly
1669 * indicate that the bitmap is corrupt. So exit
1670 * without claiming the space.
1671 */
1672 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001673 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001674
1675 ext4_mb_measure_extent(ac, &ex, e4b);
1676
1677 i += ex.fe_len;
1678 free -= ex.fe_len;
1679 }
1680
1681 ext4_mb_check_limits(ac, e4b, 1);
1682}
1683
1684/*
1685 * This is a special case for storages like raid5
1686 * we try to find stripe-aligned chunks for stripe-size requests
1687 * XXX should do so at least for multiples of stripe size as well
1688 */
1689static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1690 struct ext4_buddy *e4b)
1691{
1692 struct super_block *sb = ac->ac_sb;
1693 struct ext4_sb_info *sbi = EXT4_SB(sb);
1694 void *bitmap = EXT4_MB_BITMAP(e4b);
1695 struct ext4_free_extent ex;
1696 ext4_fsblk_t first_group_block;
1697 ext4_fsblk_t a;
1698 ext4_grpblk_t i;
1699 int max;
1700
1701 BUG_ON(sbi->s_stripe == 0);
1702
1703 /* find first stripe-aligned block in group */
1704 first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1705 + le32_to_cpu(sbi->s_es->s_first_data_block);
1706 a = first_group_block + sbi->s_stripe - 1;
1707 do_div(a, sbi->s_stripe);
1708 i = (a * sbi->s_stripe) - first_group_block;
1709
1710 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1711 if (!mb_test_bit(i, bitmap)) {
1712 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1713 if (max >= sbi->s_stripe) {
1714 ac->ac_found++;
1715 ac->ac_b_ex = ex;
1716 ext4_mb_use_best_found(ac, e4b);
1717 break;
1718 }
1719 }
1720 i += sbi->s_stripe;
1721 }
1722}
1723
1724static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1725 ext4_group_t group, int cr)
1726{
1727 unsigned free, fragments;
1728 unsigned i, bits;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001729 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001730 struct ext4_group_desc *desc;
1731 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1732
1733 BUG_ON(cr < 0 || cr >= 4);
1734 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1735
1736 free = grp->bb_free;
1737 fragments = grp->bb_fragments;
1738 if (free == 0)
1739 return 0;
1740 if (fragments == 0)
1741 return 0;
1742
1743 switch (cr) {
1744 case 0:
1745 BUG_ON(ac->ac_2order == 0);
1746 /* If this group is uninitialized, skip it initially */
1747 desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
1748 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
1749 return 0;
1750
Theodore Ts'oa4912122009-03-12 12:18:34 -04001751 /* Avoid using the first bg of a flexgroup for data files */
1752 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1753 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1754 ((group % flex_size) == 0))
1755 return 0;
1756
Alex Tomasc9de5602008-01-29 00:19:52 -05001757 bits = ac->ac_sb->s_blocksize_bits + 1;
1758 for (i = ac->ac_2order; i <= bits; i++)
1759 if (grp->bb_counters[i] > 0)
1760 return 1;
1761 break;
1762 case 1:
1763 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1764 return 1;
1765 break;
1766 case 2:
1767 if (free >= ac->ac_g_ex.fe_len)
1768 return 1;
1769 break;
1770 case 3:
1771 return 1;
1772 default:
1773 BUG();
1774 }
1775
1776 return 0;
1777}
1778
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001779/*
1780 * lock the group_info alloc_sem of all the groups
1781 * belonging to the same buddy cache page. This
1782 * make sure other parallel operation on the buddy
1783 * cache doesn't happen whild holding the buddy cache
1784 * lock
1785 */
1786int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1787{
1788 int i;
1789 int block, pnum;
1790 int blocks_per_page;
1791 int groups_per_page;
1792 ext4_group_t first_group;
1793 struct ext4_group_info *grp;
1794
1795 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1796 /*
1797 * the buddy cache inode stores the block bitmap
1798 * and buddy information in consecutive blocks.
1799 * So for each group we need two blocks.
1800 */
1801 block = group * 2;
1802 pnum = block / blocks_per_page;
1803 first_group = pnum * blocks_per_page / 2;
1804
1805 groups_per_page = blocks_per_page >> 1;
1806 if (groups_per_page == 0)
1807 groups_per_page = 1;
1808 /* read all groups the page covers into the cache */
1809 for (i = 0; i < groups_per_page; i++) {
1810
1811 if ((first_group + i) >= EXT4_SB(sb)->s_groups_count)
1812 break;
1813 grp = ext4_get_group_info(sb, first_group + i);
1814 /* take all groups write allocation
1815 * semaphore. This make sure there is
1816 * no block allocation going on in any
1817 * of that groups
1818 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001819 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001820 }
1821 return i;
1822}
1823
1824void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1825 ext4_group_t group, int locked_group)
1826{
1827 int i;
1828 int block, pnum;
1829 int blocks_per_page;
1830 ext4_group_t first_group;
1831 struct ext4_group_info *grp;
1832
1833 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1834 /*
1835 * the buddy cache inode stores the block bitmap
1836 * and buddy information in consecutive blocks.
1837 * So for each group we need two blocks.
1838 */
1839 block = group * 2;
1840 pnum = block / blocks_per_page;
1841 first_group = pnum * blocks_per_page / 2;
1842 /* release locks on all the groups */
1843 for (i = 0; i < locked_group; i++) {
1844
1845 grp = ext4_get_group_info(sb, first_group + i);
1846 /* take all groups write allocation
1847 * semaphore. This make sure there is
1848 * no block allocation going on in any
1849 * of that groups
1850 */
1851 up_write(&grp->alloc_sem);
1852 }
1853
1854}
1855
1856static int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1857{
1858
1859 int ret;
1860 void *bitmap;
1861 int blocks_per_page;
1862 int block, pnum, poff;
1863 int num_grp_locked = 0;
1864 struct ext4_group_info *this_grp;
1865 struct ext4_sb_info *sbi = EXT4_SB(sb);
1866 struct inode *inode = sbi->s_buddy_cache;
1867 struct page *page = NULL, *bitmap_page = NULL;
1868
1869 mb_debug("init group %lu\n", group);
1870 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1871 this_grp = ext4_get_group_info(sb, group);
1872 /*
1873 * This ensures we don't add group
1874 * to this buddy cache via resize
1875 */
1876 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
1877 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1878 /*
1879 * somebody initialized the group
1880 * return without doing anything
1881 */
1882 ret = 0;
1883 goto err;
1884 }
1885 /*
1886 * the buddy cache inode stores the block bitmap
1887 * and buddy information in consecutive blocks.
1888 * So for each group we need two blocks.
1889 */
1890 block = group * 2;
1891 pnum = block / blocks_per_page;
1892 poff = block % blocks_per_page;
1893 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1894 if (page) {
1895 BUG_ON(page->mapping != inode->i_mapping);
1896 ret = ext4_mb_init_cache(page, NULL);
1897 if (ret) {
1898 unlock_page(page);
1899 goto err;
1900 }
1901 unlock_page(page);
1902 }
1903 if (page == NULL || !PageUptodate(page)) {
1904 ret = -EIO;
1905 goto err;
1906 }
1907 mark_page_accessed(page);
1908 bitmap_page = page;
1909 bitmap = page_address(page) + (poff * sb->s_blocksize);
1910
1911 /* init buddy cache */
1912 block++;
1913 pnum = block / blocks_per_page;
1914 poff = block % blocks_per_page;
1915 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1916 if (page == bitmap_page) {
1917 /*
1918 * If both the bitmap and buddy are in
1919 * the same page we don't need to force
1920 * init the buddy
1921 */
1922 unlock_page(page);
1923 } else if (page) {
1924 BUG_ON(page->mapping != inode->i_mapping);
1925 ret = ext4_mb_init_cache(page, bitmap);
1926 if (ret) {
1927 unlock_page(page);
1928 goto err;
1929 }
1930 unlock_page(page);
1931 }
1932 if (page == NULL || !PageUptodate(page)) {
1933 ret = -EIO;
1934 goto err;
1935 }
1936 mark_page_accessed(page);
1937err:
1938 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1939 if (bitmap_page)
1940 page_cache_release(bitmap_page);
1941 if (page)
1942 page_cache_release(page);
1943 return ret;
1944}
1945
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001946static noinline_for_stack int
1947ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001948{
1949 ext4_group_t group;
1950 ext4_group_t i;
1951 int cr;
1952 int err = 0;
1953 int bsbits;
1954 struct ext4_sb_info *sbi;
1955 struct super_block *sb;
1956 struct ext4_buddy e4b;
1957 loff_t size, isize;
1958
1959 sb = ac->ac_sb;
1960 sbi = EXT4_SB(sb);
1961 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1962
1963 /* first, try the goal */
1964 err = ext4_mb_find_by_goal(ac, &e4b);
1965 if (err || ac->ac_status == AC_STATUS_FOUND)
1966 goto out;
1967
1968 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1969 goto out;
1970
1971 /*
1972 * ac->ac2_order is set only if the fe_len is a power of 2
1973 * if ac2_order is set we also set criteria to 0 so that we
1974 * try exact allocation using buddy.
1975 */
1976 i = fls(ac->ac_g_ex.fe_len);
1977 ac->ac_2order = 0;
1978 /*
1979 * We search using buddy data only if the order of the request
1980 * is greater than equal to the sbi_s_mb_order2_reqs
1981 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1982 */
1983 if (i >= sbi->s_mb_order2_reqs) {
1984 /*
1985 * This should tell if fe_len is exactly power of 2
1986 */
1987 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1988 ac->ac_2order = i - 1;
1989 }
1990
1991 bsbits = ac->ac_sb->s_blocksize_bits;
1992 /* if stream allocation is enabled, use global goal */
1993 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
1994 isize = i_size_read(ac->ac_inode) >> bsbits;
1995 if (size < isize)
1996 size = isize;
1997
1998 if (size < sbi->s_mb_stream_request &&
1999 (ac->ac_flags & EXT4_MB_HINT_DATA)) {
2000 /* TBD: may be hot point */
2001 spin_lock(&sbi->s_md_lock);
2002 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2003 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2004 spin_unlock(&sbi->s_md_lock);
2005 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002006 /* Let's just scan groups to find more-less suitable blocks */
2007 cr = ac->ac_2order ? 0 : 1;
2008 /*
2009 * cr == 0 try to get exact allocation,
2010 * cr == 3 try to get anything
2011 */
2012repeat:
2013 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2014 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002015 /*
2016 * searching for the right group start
2017 * from the goal value specified
2018 */
2019 group = ac->ac_g_ex.fe_group;
2020
Alex Tomasc9de5602008-01-29 00:19:52 -05002021 for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
2022 struct ext4_group_info *grp;
2023 struct ext4_group_desc *desc;
2024
2025 if (group == EXT4_SB(sb)->s_groups_count)
2026 group = 0;
2027
2028 /* quick check to skip empty groups */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002029 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002030 if (grp->bb_free == 0)
2031 continue;
2032
2033 /*
2034 * if the group is already init we check whether it is
2035 * a good group and if not we don't load the buddy
2036 */
2037 if (EXT4_MB_GRP_NEED_INIT(grp)) {
2038 /*
2039 * we need full data about the group
2040 * to make a good selection
2041 */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002042 err = ext4_mb_init_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002043 if (err)
2044 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002045 }
2046
2047 /*
2048 * If the particular group doesn't satisfy our
2049 * criteria we continue with the next group
2050 */
2051 if (!ext4_mb_good_group(ac, group, cr))
2052 continue;
2053
2054 err = ext4_mb_load_buddy(sb, group, &e4b);
2055 if (err)
2056 goto out;
2057
2058 ext4_lock_group(sb, group);
2059 if (!ext4_mb_good_group(ac, group, cr)) {
2060 /* someone did allocation from this group */
2061 ext4_unlock_group(sb, group);
2062 ext4_mb_release_desc(&e4b);
2063 continue;
2064 }
2065
2066 ac->ac_groups_scanned++;
2067 desc = ext4_get_group_desc(sb, group, NULL);
2068 if (cr == 0 || (desc->bg_flags &
2069 cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
2070 ac->ac_2order != 0))
2071 ext4_mb_simple_scan_group(ac, &e4b);
2072 else if (cr == 1 &&
2073 ac->ac_g_ex.fe_len == sbi->s_stripe)
2074 ext4_mb_scan_aligned(ac, &e4b);
2075 else
2076 ext4_mb_complex_scan_group(ac, &e4b);
2077
2078 ext4_unlock_group(sb, group);
2079 ext4_mb_release_desc(&e4b);
2080
2081 if (ac->ac_status != AC_STATUS_CONTINUE)
2082 break;
2083 }
2084 }
2085
2086 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2087 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2088 /*
2089 * We've been searching too long. Let's try to allocate
2090 * the best chunk we've found so far
2091 */
2092
2093 ext4_mb_try_best_found(ac, &e4b);
2094 if (ac->ac_status != AC_STATUS_FOUND) {
2095 /*
2096 * Someone more lucky has already allocated it.
2097 * The only thing we can do is just take first
2098 * found block(s)
2099 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2100 */
2101 ac->ac_b_ex.fe_group = 0;
2102 ac->ac_b_ex.fe_start = 0;
2103 ac->ac_b_ex.fe_len = 0;
2104 ac->ac_status = AC_STATUS_CONTINUE;
2105 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2106 cr = 3;
2107 atomic_inc(&sbi->s_mb_lost_chunks);
2108 goto repeat;
2109 }
2110 }
2111out:
2112 return err;
2113}
2114
2115#ifdef EXT4_MB_HISTORY
2116struct ext4_mb_proc_session {
2117 struct ext4_mb_history *history;
2118 struct super_block *sb;
2119 int start;
2120 int max;
2121};
2122
2123static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2124 struct ext4_mb_history *hs,
2125 int first)
2126{
2127 if (hs == s->history + s->max)
2128 hs = s->history;
2129 if (!first && hs == s->history + s->start)
2130 return NULL;
2131 while (hs->orig.fe_len == 0) {
2132 hs++;
2133 if (hs == s->history + s->max)
2134 hs = s->history;
2135 if (hs == s->history + s->start)
2136 return NULL;
2137 }
2138 return hs;
2139}
2140
2141static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2142{
2143 struct ext4_mb_proc_session *s = seq->private;
2144 struct ext4_mb_history *hs;
2145 int l = *pos;
2146
2147 if (l == 0)
2148 return SEQ_START_TOKEN;
2149 hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2150 if (!hs)
2151 return NULL;
2152 while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2153 return hs;
2154}
2155
2156static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2157 loff_t *pos)
2158{
2159 struct ext4_mb_proc_session *s = seq->private;
2160 struct ext4_mb_history *hs = v;
2161
2162 ++*pos;
2163 if (v == SEQ_START_TOKEN)
2164 return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2165 else
2166 return ext4_mb_history_skip_empty(s, ++hs, 0);
2167}
2168
2169static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2170{
2171 char buf[25], buf2[25], buf3[25], *fmt;
2172 struct ext4_mb_history *hs = v;
2173
2174 if (v == SEQ_START_TOKEN) {
2175 seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2176 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2177 "pid", "inode", "original", "goal", "result", "found",
2178 "grps", "cr", "flags", "merge", "tail", "broken");
2179 return 0;
2180 }
2181
2182 if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2183 fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2184 "%-5u %-5s %-5u %-6u\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002185 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002186 hs->result.fe_start, hs->result.fe_len,
2187 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002188 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002189 hs->orig.fe_start, hs->orig.fe_len,
2190 hs->orig.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002191 sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002192 hs->goal.fe_start, hs->goal.fe_len,
2193 hs->goal.fe_logical);
2194 seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2195 hs->found, hs->groups, hs->cr, hs->flags,
2196 hs->merged ? "M" : "", hs->tail,
2197 hs->buddy ? 1 << hs->buddy : 0);
2198 } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2199 fmt = "%-5u %-8u %-23s %-23s %-23s\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002200 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002201 hs->result.fe_start, hs->result.fe_len,
2202 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002203 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002204 hs->orig.fe_start, hs->orig.fe_len,
2205 hs->orig.fe_logical);
2206 seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2207 } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002208 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002209 hs->result.fe_start, hs->result.fe_len);
2210 seq_printf(seq, "%-5u %-8u %-23s discard\n",
2211 hs->pid, hs->ino, buf2);
2212 } else if (hs->op == EXT4_MB_HISTORY_FREE) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002213 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002214 hs->result.fe_start, hs->result.fe_len);
2215 seq_printf(seq, "%-5u %-8u %-23s free\n",
2216 hs->pid, hs->ino, buf2);
2217 }
2218 return 0;
2219}
2220
2221static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2222{
2223}
2224
2225static struct seq_operations ext4_mb_seq_history_ops = {
2226 .start = ext4_mb_seq_history_start,
2227 .next = ext4_mb_seq_history_next,
2228 .stop = ext4_mb_seq_history_stop,
2229 .show = ext4_mb_seq_history_show,
2230};
2231
2232static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2233{
2234 struct super_block *sb = PDE(inode)->data;
2235 struct ext4_sb_info *sbi = EXT4_SB(sb);
2236 struct ext4_mb_proc_session *s;
2237 int rc;
2238 int size;
2239
Shen Feng74767c52008-07-11 19:27:31 -04002240 if (unlikely(sbi->s_mb_history == NULL))
2241 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05002242 s = kmalloc(sizeof(*s), GFP_KERNEL);
2243 if (s == NULL)
2244 return -ENOMEM;
2245 s->sb = sb;
2246 size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2247 s->history = kmalloc(size, GFP_KERNEL);
2248 if (s->history == NULL) {
2249 kfree(s);
2250 return -ENOMEM;
2251 }
2252
2253 spin_lock(&sbi->s_mb_history_lock);
2254 memcpy(s->history, sbi->s_mb_history, size);
2255 s->max = sbi->s_mb_history_max;
2256 s->start = sbi->s_mb_history_cur % s->max;
2257 spin_unlock(&sbi->s_mb_history_lock);
2258
2259 rc = seq_open(file, &ext4_mb_seq_history_ops);
2260 if (rc == 0) {
2261 struct seq_file *m = (struct seq_file *)file->private_data;
2262 m->private = s;
2263 } else {
2264 kfree(s->history);
2265 kfree(s);
2266 }
2267 return rc;
2268
2269}
2270
2271static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2272{
2273 struct seq_file *seq = (struct seq_file *)file->private_data;
2274 struct ext4_mb_proc_session *s = seq->private;
2275 kfree(s->history);
2276 kfree(s);
2277 return seq_release(inode, file);
2278}
2279
2280static ssize_t ext4_mb_seq_history_write(struct file *file,
2281 const char __user *buffer,
2282 size_t count, loff_t *ppos)
2283{
2284 struct seq_file *seq = (struct seq_file *)file->private_data;
2285 struct ext4_mb_proc_session *s = seq->private;
2286 struct super_block *sb = s->sb;
2287 char str[32];
2288 int value;
2289
2290 if (count >= sizeof(str)) {
2291 printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2292 "mb_history", (int)sizeof(str));
2293 return -EOVERFLOW;
2294 }
2295
2296 if (copy_from_user(str, buffer, count))
2297 return -EFAULT;
2298
2299 value = simple_strtol(str, NULL, 0);
2300 if (value < 0)
2301 return -ERANGE;
2302 EXT4_SB(sb)->s_mb_history_filter = value;
2303
2304 return count;
2305}
2306
2307static struct file_operations ext4_mb_seq_history_fops = {
2308 .owner = THIS_MODULE,
2309 .open = ext4_mb_seq_history_open,
2310 .read = seq_read,
2311 .write = ext4_mb_seq_history_write,
2312 .llseek = seq_lseek,
2313 .release = ext4_mb_seq_history_release,
2314};
2315
2316static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2317{
2318 struct super_block *sb = seq->private;
2319 struct ext4_sb_info *sbi = EXT4_SB(sb);
2320 ext4_group_t group;
2321
2322 if (*pos < 0 || *pos >= sbi->s_groups_count)
2323 return NULL;
2324
2325 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002326 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002327}
2328
2329static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2330{
2331 struct super_block *sb = seq->private;
2332 struct ext4_sb_info *sbi = EXT4_SB(sb);
2333 ext4_group_t group;
2334
2335 ++*pos;
2336 if (*pos < 0 || *pos >= sbi->s_groups_count)
2337 return NULL;
2338 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002339 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002340}
2341
2342static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2343{
2344 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002345 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002346 int i;
2347 int err;
2348 struct ext4_buddy e4b;
2349 struct sg {
2350 struct ext4_group_info info;
2351 unsigned short counters[16];
2352 } sg;
2353
2354 group--;
2355 if (group == 0)
2356 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2357 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2358 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2359 "group", "free", "frags", "first",
2360 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2361 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2362
2363 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2364 sizeof(struct ext4_group_info);
2365 err = ext4_mb_load_buddy(sb, group, &e4b);
2366 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002367 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002368 return 0;
2369 }
2370 ext4_lock_group(sb, group);
2371 memcpy(&sg, ext4_get_group_info(sb, group), i);
2372 ext4_unlock_group(sb, group);
2373 ext4_mb_release_desc(&e4b);
2374
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002375 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002376 sg.info.bb_fragments, sg.info.bb_first_free);
2377 for (i = 0; i <= 13; i++)
2378 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2379 sg.info.bb_counters[i] : 0);
2380 seq_printf(seq, " ]\n");
2381
2382 return 0;
2383}
2384
2385static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2386{
2387}
2388
2389static struct seq_operations ext4_mb_seq_groups_ops = {
2390 .start = ext4_mb_seq_groups_start,
2391 .next = ext4_mb_seq_groups_next,
2392 .stop = ext4_mb_seq_groups_stop,
2393 .show = ext4_mb_seq_groups_show,
2394};
2395
2396static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2397{
2398 struct super_block *sb = PDE(inode)->data;
2399 int rc;
2400
2401 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2402 if (rc == 0) {
2403 struct seq_file *m = (struct seq_file *)file->private_data;
2404 m->private = sb;
2405 }
2406 return rc;
2407
2408}
2409
2410static struct file_operations ext4_mb_seq_groups_fops = {
2411 .owner = THIS_MODULE,
2412 .open = ext4_mb_seq_groups_open,
2413 .read = seq_read,
2414 .llseek = seq_lseek,
2415 .release = seq_release,
2416};
2417
2418static void ext4_mb_history_release(struct super_block *sb)
2419{
2420 struct ext4_sb_info *sbi = EXT4_SB(sb);
2421
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002422 if (sbi->s_proc != NULL) {
2423 remove_proc_entry("mb_groups", sbi->s_proc);
2424 remove_proc_entry("mb_history", sbi->s_proc);
2425 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002426 kfree(sbi->s_mb_history);
2427}
2428
2429static void ext4_mb_history_init(struct super_block *sb)
2430{
2431 struct ext4_sb_info *sbi = EXT4_SB(sb);
2432 int i;
2433
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002434 if (sbi->s_proc != NULL) {
2435 proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002436 &ext4_mb_seq_history_fops, sb);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002437 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002438 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002439 }
2440
2441 sbi->s_mb_history_max = 1000;
2442 sbi->s_mb_history_cur = 0;
2443 spin_lock_init(&sbi->s_mb_history_lock);
2444 i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
Shen Feng74767c52008-07-11 19:27:31 -04002445 sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002446 /* if we can't allocate history, then we simple won't use it */
2447}
2448
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002449static noinline_for_stack void
2450ext4_mb_store_history(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002451{
2452 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2453 struct ext4_mb_history h;
2454
2455 if (unlikely(sbi->s_mb_history == NULL))
2456 return;
2457
2458 if (!(ac->ac_op & sbi->s_mb_history_filter))
2459 return;
2460
2461 h.op = ac->ac_op;
2462 h.pid = current->pid;
2463 h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2464 h.orig = ac->ac_o_ex;
2465 h.result = ac->ac_b_ex;
2466 h.flags = ac->ac_flags;
2467 h.found = ac->ac_found;
2468 h.groups = ac->ac_groups_scanned;
2469 h.cr = ac->ac_criteria;
2470 h.tail = ac->ac_tail;
2471 h.buddy = ac->ac_buddy;
2472 h.merged = 0;
2473 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2474 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2475 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2476 h.merged = 1;
2477 h.goal = ac->ac_g_ex;
2478 h.result = ac->ac_f_ex;
2479 }
2480
2481 spin_lock(&sbi->s_mb_history_lock);
2482 memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2483 if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2484 sbi->s_mb_history_cur = 0;
2485 spin_unlock(&sbi->s_mb_history_lock);
2486}
2487
2488#else
2489#define ext4_mb_history_release(sb)
2490#define ext4_mb_history_init(sb)
2491#endif
2492
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002493
2494/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002495int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002496 struct ext4_group_desc *desc)
2497{
2498 int i, len;
2499 int metalen = 0;
2500 struct ext4_sb_info *sbi = EXT4_SB(sb);
2501 struct ext4_group_info **meta_group_info;
2502
2503 /*
2504 * First check if this group is the first of a reserved block.
2505 * If it's true, we have to allocate a new table of pointers
2506 * to ext4_group_info structures
2507 */
2508 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2509 metalen = sizeof(*meta_group_info) <<
2510 EXT4_DESC_PER_BLOCK_BITS(sb);
2511 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2512 if (meta_group_info == NULL) {
2513 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2514 "buddy group\n");
2515 goto exit_meta_group_info;
2516 }
2517 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2518 meta_group_info;
2519 }
2520
2521 /*
2522 * calculate needed size. if change bb_counters size,
2523 * don't forget about ext4_mb_generate_buddy()
2524 */
2525 len = offsetof(typeof(**meta_group_info),
2526 bb_counters[sb->s_blocksize_bits + 2]);
2527
2528 meta_group_info =
2529 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2530 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2531
2532 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2533 if (meta_group_info[i] == NULL) {
2534 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2535 goto exit_group_info;
2536 }
2537 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2538 &(meta_group_info[i]->bb_state));
2539
2540 /*
2541 * initialize bb_free to be able to skip
2542 * empty groups without initialization
2543 */
2544 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2545 meta_group_info[i]->bb_free =
2546 ext4_free_blocks_after_init(sb, group, desc);
2547 } else {
2548 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002549 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002550 }
2551
2552 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002553 init_rwsem(&meta_group_info[i]->alloc_sem);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002554 meta_group_info[i]->bb_free_root.rb_node = NULL;;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002555
2556#ifdef DOUBLE_CHECK
2557 {
2558 struct buffer_head *bh;
2559 meta_group_info[i]->bb_bitmap =
2560 kmalloc(sb->s_blocksize, GFP_KERNEL);
2561 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2562 bh = ext4_read_block_bitmap(sb, group);
2563 BUG_ON(bh == NULL);
2564 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2565 sb->s_blocksize);
2566 put_bh(bh);
2567 }
2568#endif
2569
2570 return 0;
2571
2572exit_group_info:
2573 /* If a meta_group_info table has been allocated, release it now */
2574 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2575 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2576exit_meta_group_info:
2577 return -ENOMEM;
2578} /* ext4_mb_add_groupinfo */
2579
2580/*
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002581 * Update an existing group.
2582 * This function is used for online resize
2583 */
2584void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
2585{
2586 grp->bb_free += add;
2587}
2588
Alex Tomasc9de5602008-01-29 00:19:52 -05002589static int ext4_mb_init_backend(struct super_block *sb)
2590{
2591 ext4_group_t i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002592 int metalen;
Alex Tomasc9de5602008-01-29 00:19:52 -05002593 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002594 struct ext4_super_block *es = sbi->s_es;
2595 int num_meta_group_infos;
2596 int num_meta_group_infos_max;
2597 int array_size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002598 struct ext4_group_info **meta_group_info;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002599 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002600
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002601 /* This is the number of blocks used by GDT */
2602 num_meta_group_infos = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) -
2603 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2604
2605 /*
2606 * This is the total number of blocks used by GDT including
2607 * the number of reserved blocks for GDT.
2608 * The s_group_info array is allocated with this value
2609 * to allow a clean online resize without a complex
2610 * manipulation of pointer.
2611 * The drawback is the unused memory when no resize
2612 * occurs but it's very low in terms of pages
2613 * (see comments below)
2614 * Need to handle this properly when META_BG resizing is allowed
2615 */
2616 num_meta_group_infos_max = num_meta_group_infos +
2617 le16_to_cpu(es->s_reserved_gdt_blocks);
2618
2619 /*
2620 * array_size is the size of s_group_info array. We round it
2621 * to the next power of two because this approximation is done
2622 * internally by kmalloc so we can have some more memory
2623 * for free here (e.g. may be used for META_BG resize).
2624 */
2625 array_size = 1;
2626 while (array_size < sizeof(*sbi->s_group_info) *
2627 num_meta_group_infos_max)
2628 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002629 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2630 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2631 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002632 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 if (sbi->s_group_info == NULL) {
2634 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2635 return -ENOMEM;
2636 }
2637 sbi->s_buddy_cache = new_inode(sb);
2638 if (sbi->s_buddy_cache == NULL) {
2639 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2640 goto err_freesgi;
2641 }
2642 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2643
2644 metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
2645 for (i = 0; i < num_meta_group_infos; i++) {
2646 if ((i + 1) == num_meta_group_infos)
2647 metalen = sizeof(*meta_group_info) *
2648 (sbi->s_groups_count -
2649 (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
2650 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2651 if (meta_group_info == NULL) {
2652 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2653 "buddy group\n");
2654 goto err_freemeta;
2655 }
2656 sbi->s_group_info[i] = meta_group_info;
2657 }
2658
Alex Tomasc9de5602008-01-29 00:19:52 -05002659 for (i = 0; i < sbi->s_groups_count; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002660 desc = ext4_get_group_desc(sb, i, NULL);
2661 if (desc == NULL) {
2662 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002663 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002664 goto err_freebuddy;
2665 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002666 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2667 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002668 }
2669
2670 return 0;
2671
2672err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002673 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002674 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002675 i = num_meta_group_infos;
2676err_freemeta:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002677 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002678 kfree(sbi->s_group_info[i]);
2679 iput(sbi->s_buddy_cache);
2680err_freesgi:
2681 kfree(sbi->s_group_info);
2682 return -ENOMEM;
2683}
2684
2685int ext4_mb_init(struct super_block *sb, int needs_recovery)
2686{
2687 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002688 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002689 unsigned offset;
2690 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002691 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002692
Alex Tomasc9de5602008-01-29 00:19:52 -05002693 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2694
2695 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2696 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002697 return -ENOMEM;
2698 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002699
2700 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
Alex Tomasc9de5602008-01-29 00:19:52 -05002701 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2702 if (sbi->s_mb_maxs == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002703 kfree(sbi->s_mb_maxs);
2704 return -ENOMEM;
2705 }
2706
2707 /* order 0 is regular bitmap */
2708 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2709 sbi->s_mb_offsets[0] = 0;
2710
2711 i = 1;
2712 offset = 0;
2713 max = sb->s_blocksize << 2;
2714 do {
2715 sbi->s_mb_offsets[i] = offset;
2716 sbi->s_mb_maxs[i] = max;
2717 offset += 1 << (sb->s_blocksize_bits - i);
2718 max = max >> 1;
2719 i++;
2720 } while (i <= sb->s_blocksize_bits + 1);
2721
2722 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002723 ret = ext4_mb_init_backend(sb);
2724 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002725 kfree(sbi->s_mb_offsets);
2726 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002727 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002728 }
2729
2730 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002731 spin_lock_init(&sbi->s_bal_lock);
2732
2733 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2734 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2735 sbi->s_mb_stats = MB_DEFAULT_STATS;
2736 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2737 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2738 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2739 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2740
Eric Sandeen730c2132008-09-13 15:23:29 -04002741 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002742 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002743 kfree(sbi->s_mb_offsets);
2744 kfree(sbi->s_mb_maxs);
2745 return -ENOMEM;
2746 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002747 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002748 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002749 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002750 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002751 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2752 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002753 spin_lock_init(&lg->lg_prealloc_lock);
2754 }
2755
2756 ext4_mb_init_per_dev_proc(sb);
2757 ext4_mb_history_init(sb);
2758
Frank Mayhar03901312009-01-07 00:06:22 -05002759 if (sbi->s_journal)
2760 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002761
Theodore Ts'o47760042008-09-08 23:00:52 -04002762 printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002763 return 0;
2764}
2765
2766/* need to called with ext4 group lock (ext4_lock_group) */
2767static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2768{
2769 struct ext4_prealloc_space *pa;
2770 struct list_head *cur, *tmp;
2771 int count = 0;
2772
2773 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2774 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2775 list_del(&pa->pa_group_list);
2776 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002777 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002778 }
2779 if (count)
2780 mb_debug("mballoc: %u PAs left\n", count);
2781
2782}
2783
2784int ext4_mb_release(struct super_block *sb)
2785{
2786 ext4_group_t i;
2787 int num_meta_group_infos;
2788 struct ext4_group_info *grinfo;
2789 struct ext4_sb_info *sbi = EXT4_SB(sb);
2790
Alex Tomasc9de5602008-01-29 00:19:52 -05002791 if (sbi->s_group_info) {
2792 for (i = 0; i < sbi->s_groups_count; i++) {
2793 grinfo = ext4_get_group_info(sb, i);
2794#ifdef DOUBLE_CHECK
2795 kfree(grinfo->bb_bitmap);
2796#endif
2797 ext4_lock_group(sb, i);
2798 ext4_mb_cleanup_pa(grinfo);
2799 ext4_unlock_group(sb, i);
2800 kfree(grinfo);
2801 }
2802 num_meta_group_infos = (sbi->s_groups_count +
2803 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2804 EXT4_DESC_PER_BLOCK_BITS(sb);
2805 for (i = 0; i < num_meta_group_infos; i++)
2806 kfree(sbi->s_group_info[i]);
2807 kfree(sbi->s_group_info);
2808 }
2809 kfree(sbi->s_mb_offsets);
2810 kfree(sbi->s_mb_maxs);
2811 if (sbi->s_buddy_cache)
2812 iput(sbi->s_buddy_cache);
2813 if (sbi->s_mb_stats) {
2814 printk(KERN_INFO
2815 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2816 atomic_read(&sbi->s_bal_allocated),
2817 atomic_read(&sbi->s_bal_reqs),
2818 atomic_read(&sbi->s_bal_success));
2819 printk(KERN_INFO
2820 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2821 "%u 2^N hits, %u breaks, %u lost\n",
2822 atomic_read(&sbi->s_bal_ex_scanned),
2823 atomic_read(&sbi->s_bal_goals),
2824 atomic_read(&sbi->s_bal_2orders),
2825 atomic_read(&sbi->s_bal_breaks),
2826 atomic_read(&sbi->s_mb_lost_chunks));
2827 printk(KERN_INFO
2828 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2829 sbi->s_mb_buddies_generated++,
2830 sbi->s_mb_generation_time);
2831 printk(KERN_INFO
2832 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2833 atomic_read(&sbi->s_mb_preallocated),
2834 atomic_read(&sbi->s_mb_discarded));
2835 }
2836
Eric Sandeen730c2132008-09-13 15:23:29 -04002837 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002838 ext4_mb_history_release(sb);
2839 ext4_mb_destroy_per_dev_proc(sb);
2840
2841 return 0;
2842}
2843
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002844/*
2845 * This function is called by the jbd2 layer once the commit has finished,
2846 * so we know we can free the blocks that were released with that commit.
2847 */
2848static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002849{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002850 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002851 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002852 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002853 int err, count = 0, count2 = 0;
2854 struct ext4_free_data *entry;
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002855 ext4_fsblk_t discard_block;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002856 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002857
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002858 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2859 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002860
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002861 mb_debug("gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002862 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002863
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002864 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002865 /* we expect to find existing buddy because it's pinned */
2866 BUG_ON(err != 0);
2867
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002868 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002869 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002870 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002871 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002872 ext4_lock_group(sb, entry->group);
2873 /* Take it out of per group rb tree */
2874 rb_erase(&entry->node, &(db->bb_free_root));
2875 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2876
2877 if (!db->bb_free_root.rb_node) {
2878 /* No more items in the per group rb tree
2879 * balance refcounts from ext4_mb_free_metadata()
2880 */
2881 page_cache_release(e4b.bd_buddy_page);
2882 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002883 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002884 ext4_unlock_group(sb, entry->group);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002885 discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
2886 + entry->start_blk
2887 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002888 trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u",
2889 sb->s_id, (unsigned long long) discard_block,
2890 entry->count);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002891 sb_issue_discard(sb, discard_block, entry->count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002892
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002893 kmem_cache_free(ext4_free_ext_cachep, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002894 ext4_mb_release_desc(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002895 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002896
2897 mb_debug("freed %u blocks in %u structures\n", count, count2);
2898}
2899
Alex Tomasc9de5602008-01-29 00:19:52 -05002900#define EXT4_MB_STATS_NAME "stats"
2901#define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2902#define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2903#define EXT4_MB_ORDER2_REQ "order2_req"
2904#define EXT4_MB_STREAM_REQ "stream_req"
2905#define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2906
Alex Tomasc9de5602008-01-29 00:19:52 -05002907static int ext4_mb_init_per_dev_proc(struct super_block *sb)
2908{
Manish Katiyar0b099232008-10-17 14:58:45 -04002909#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002910 mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
2911 struct ext4_sb_info *sbi = EXT4_SB(sb);
2912 struct proc_dir_entry *proc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002913
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002914 if (sbi->s_proc == NULL)
Shen Fengcfbe7e42008-07-13 21:03:31 -04002915 return -EINVAL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002916
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04002917 EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME, mb_stats);
2918 EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
2919 EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
2920 EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
2921 EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
2922 EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 return 0;
2924
2925err_out:
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002926 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2927 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2928 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2929 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2930 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2931 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002932 return -ENOMEM;
Manish Katiyar0b099232008-10-17 14:58:45 -04002933#else
2934 return 0;
2935#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002936}
2937
2938static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
2939{
Manish Katiyar0b099232008-10-17 14:58:45 -04002940#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002941 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002942
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002943 if (sbi->s_proc == NULL)
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 return -EINVAL;
2945
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002946 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2947 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2948 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2949 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2950 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2951 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Manish Katiyar0b099232008-10-17 14:58:45 -04002952#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002953 return 0;
2954}
2955
2956int __init init_ext4_mballoc(void)
2957{
2958 ext4_pspace_cachep =
2959 kmem_cache_create("ext4_prealloc_space",
2960 sizeof(struct ext4_prealloc_space),
2961 0, SLAB_RECLAIM_ACCOUNT, NULL);
2962 if (ext4_pspace_cachep == NULL)
2963 return -ENOMEM;
2964
Eric Sandeen256bdb42008-02-10 01:13:33 -05002965 ext4_ac_cachep =
2966 kmem_cache_create("ext4_alloc_context",
2967 sizeof(struct ext4_allocation_context),
2968 0, SLAB_RECLAIM_ACCOUNT, NULL);
2969 if (ext4_ac_cachep == NULL) {
2970 kmem_cache_destroy(ext4_pspace_cachep);
2971 return -ENOMEM;
2972 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002973
2974 ext4_free_ext_cachep =
2975 kmem_cache_create("ext4_free_block_extents",
2976 sizeof(struct ext4_free_data),
2977 0, SLAB_RECLAIM_ACCOUNT, NULL);
2978 if (ext4_free_ext_cachep == NULL) {
2979 kmem_cache_destroy(ext4_pspace_cachep);
2980 kmem_cache_destroy(ext4_ac_cachep);
2981 return -ENOMEM;
2982 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002983 return 0;
2984}
2985
2986void exit_ext4_mballoc(void)
2987{
2988 /* XXX: synchronize_rcu(); */
2989 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002990 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002991 kmem_cache_destroy(ext4_free_ext_cachep);
Alex Tomasc9de5602008-01-29 00:19:52 -05002992}
2993
2994
2995/*
2996 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2997 * Returns 0 if success or error code
2998 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002999static noinline_for_stack int
3000ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003001 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05003002{
3003 struct buffer_head *bitmap_bh = NULL;
3004 struct ext4_super_block *es;
3005 struct ext4_group_desc *gdp;
3006 struct buffer_head *gdp_bh;
3007 struct ext4_sb_info *sbi;
3008 struct super_block *sb;
3009 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003010 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003011
3012 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3013 BUG_ON(ac->ac_b_ex.fe_len <= 0);
3014
3015 sb = ac->ac_sb;
3016 sbi = EXT4_SB(sb);
3017 es = sbi->s_es;
3018
Alex Tomasc9de5602008-01-29 00:19:52 -05003019
3020 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04003021 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003022 if (!bitmap_bh)
3023 goto out_err;
3024
3025 err = ext4_journal_get_write_access(handle, bitmap_bh);
3026 if (err)
3027 goto out_err;
3028
3029 err = -EIO;
3030 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
3031 if (!gdp)
3032 goto out_err;
3033
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003034 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003035 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003036
Alex Tomasc9de5602008-01-29 00:19:52 -05003037 err = ext4_journal_get_write_access(handle, gdp_bh);
3038 if (err)
3039 goto out_err;
3040
3041 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
3042 + ac->ac_b_ex.fe_start
3043 + le32_to_cpu(es->s_first_data_block);
3044
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003045 len = ac->ac_b_ex.fe_len;
3046 if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
3047 in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
3048 in_range(block, ext4_inode_table(sb, gdp),
3049 EXT4_SB(sb)->s_itb_per_group) ||
3050 in_range(block + len - 1, ext4_inode_table(sb, gdp),
3051 EXT4_SB(sb)->s_itb_per_group)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04003052 ext4_error(sb, __func__,
Aneesh Kumar K.V648f5872009-01-05 21:46:04 -05003053 "Allocating block %llu in system zone of %d group\n",
3054 block, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003055 /* File system mounted not to panic on error
3056 * Fix the bitmap and repeat the block allocation
3057 * We leak some of the blocks here.
3058 */
3059 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
3060 bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3061 ac->ac_b_ex.fe_len);
Frank Mayhar03901312009-01-07 00:06:22 -05003062 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003063 if (!err)
3064 err = -EAGAIN;
3065 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003066 }
3067#ifdef AGGRESSIVE_CHECK
3068 {
3069 int i;
3070 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3071 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3072 bitmap_bh->b_data));
3073 }
3074 }
3075#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05003076 spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
Aneesh Kumar K.Ve8134b22009-01-05 21:38:26 -05003077 mb_set_bits(NULL, bitmap_bh->b_data,
3078 ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003079 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3080 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003081 ext4_free_blks_set(sb, gdp,
3082 ext4_free_blocks_after_init(sb,
3083 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05003084 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003085 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
3086 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003087 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
3088 spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003089 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003090 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003091 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003092 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003093 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3094 /* release all the reserved blocks if non delalloc */
3095 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Mingming Cao60e58e02009-01-22 18:13:05 +01003096 else {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003097 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
3098 ac->ac_b_ex.fe_len);
Mingming Cao60e58e02009-01-22 18:13:05 +01003099 /* convert reserved quota blocks to real quota blocks */
3100 vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
3101 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003102
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003103 if (sbi->s_log_groups_per_flex) {
3104 ext4_group_t flex_group = ext4_flex_group(sbi,
3105 ac->ac_b_ex.fe_group);
3106 spin_lock(sb_bgl_lock(sbi, flex_group));
3107 sbi->s_flex_groups[flex_group].free_blocks -= ac->ac_b_ex.fe_len;
3108 spin_unlock(sb_bgl_lock(sbi, flex_group));
3109 }
3110
Frank Mayhar03901312009-01-07 00:06:22 -05003111 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003112 if (err)
3113 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003114 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003115
3116out_err:
3117 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003118 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003119 return err;
3120}
3121
3122/*
3123 * here we normalize request for locality group
3124 * Group request are normalized to s_strip size if we set the same via mount
3125 * option. If not we set it to s_mb_group_prealloc which can be configured via
3126 * /proc/fs/ext4/<partition>/group_prealloc
3127 *
3128 * XXX: should we try to preallocate more than the group has now?
3129 */
3130static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3131{
3132 struct super_block *sb = ac->ac_sb;
3133 struct ext4_locality_group *lg = ac->ac_lg;
3134
3135 BUG_ON(lg == NULL);
3136 if (EXT4_SB(sb)->s_stripe)
3137 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3138 else
3139 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003140 mb_debug("#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003141 current->pid, ac->ac_g_ex.fe_len);
3142}
3143
3144/*
3145 * Normalization means making request better in terms of
3146 * size and alignment
3147 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003148static noinline_for_stack void
3149ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003150 struct ext4_allocation_request *ar)
3151{
3152 int bsbits, max;
3153 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003154 loff_t size, orig_size, start_off;
3155 ext4_lblk_t start, orig_start;
3156 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003157 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003158
3159 /* do normalize only data requests, metadata requests
3160 do not need preallocation */
3161 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3162 return;
3163
3164 /* sometime caller may want exact blocks */
3165 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3166 return;
3167
3168 /* caller may indicate that preallocation isn't
3169 * required (it's a tail, for example) */
3170 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3171 return;
3172
3173 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3174 ext4_mb_normalize_group_request(ac);
3175 return ;
3176 }
3177
3178 bsbits = ac->ac_sb->s_blocksize_bits;
3179
3180 /* first, let's learn actual file size
3181 * given current request is allocated */
3182 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3183 size = size << bsbits;
3184 if (size < i_size_read(ac->ac_inode))
3185 size = i_size_read(ac->ac_inode);
3186
Valerie Clement19304792008-05-13 19:31:14 -04003187 /* max size of free chunks */
3188 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003189
Valerie Clement19304792008-05-13 19:31:14 -04003190#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3191 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003192
3193 /* first, try to predict filesize */
3194 /* XXX: should this table be tunable? */
3195 start_off = 0;
3196 if (size <= 16 * 1024) {
3197 size = 16 * 1024;
3198 } else if (size <= 32 * 1024) {
3199 size = 32 * 1024;
3200 } else if (size <= 64 * 1024) {
3201 size = 64 * 1024;
3202 } else if (size <= 128 * 1024) {
3203 size = 128 * 1024;
3204 } else if (size <= 256 * 1024) {
3205 size = 256 * 1024;
3206 } else if (size <= 512 * 1024) {
3207 size = 512 * 1024;
3208 } else if (size <= 1024 * 1024) {
3209 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003210 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003211 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003212 (21 - bsbits)) << 21;
3213 size = 2 * 1024 * 1024;
3214 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003215 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3216 (22 - bsbits)) << 22;
3217 size = 4 * 1024 * 1024;
3218 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003219 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003220 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3221 (23 - bsbits)) << 23;
3222 size = 8 * 1024 * 1024;
3223 } else {
3224 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3225 size = ac->ac_o_ex.fe_len << bsbits;
3226 }
3227 orig_size = size = size >> bsbits;
3228 orig_start = start = start_off >> bsbits;
3229
3230 /* don't cover already allocated blocks in selected range */
3231 if (ar->pleft && start <= ar->lleft) {
3232 size -= ar->lleft + 1 - start;
3233 start = ar->lleft + 1;
3234 }
3235 if (ar->pright && start + size - 1 >= ar->lright)
3236 size -= start + size - ar->lright;
3237
3238 end = start + size;
3239
3240 /* check we don't cross already preallocated blocks */
3241 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003242 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003243 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003244
Alex Tomasc9de5602008-01-29 00:19:52 -05003245 if (pa->pa_deleted)
3246 continue;
3247 spin_lock(&pa->pa_lock);
3248 if (pa->pa_deleted) {
3249 spin_unlock(&pa->pa_lock);
3250 continue;
3251 }
3252
3253 pa_end = pa->pa_lstart + pa->pa_len;
3254
3255 /* PA must not overlap original request */
3256 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3257 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3258
3259 /* skip PA normalized request doesn't overlap with */
3260 if (pa->pa_lstart >= end) {
3261 spin_unlock(&pa->pa_lock);
3262 continue;
3263 }
3264 if (pa_end <= start) {
3265 spin_unlock(&pa->pa_lock);
3266 continue;
3267 }
3268 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3269
3270 if (pa_end <= ac->ac_o_ex.fe_logical) {
3271 BUG_ON(pa_end < start);
3272 start = pa_end;
3273 }
3274
3275 if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3276 BUG_ON(pa->pa_lstart > end);
3277 end = pa->pa_lstart;
3278 }
3279 spin_unlock(&pa->pa_lock);
3280 }
3281 rcu_read_unlock();
3282 size = end - start;
3283
3284 /* XXX: extra loop to check we really don't overlap preallocations */
3285 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003286 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003287 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003288 spin_lock(&pa->pa_lock);
3289 if (pa->pa_deleted == 0) {
3290 pa_end = pa->pa_lstart + pa->pa_len;
3291 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3292 }
3293 spin_unlock(&pa->pa_lock);
3294 }
3295 rcu_read_unlock();
3296
3297 if (start + size <= ac->ac_o_ex.fe_logical &&
3298 start > ac->ac_o_ex.fe_logical) {
3299 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3300 (unsigned long) start, (unsigned long) size,
3301 (unsigned long) ac->ac_o_ex.fe_logical);
3302 }
3303 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3304 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003305 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003306
3307 /* now prepare goal request */
3308
3309 /* XXX: is it better to align blocks WRT to logical
3310 * placement or satisfy big request as is */
3311 ac->ac_g_ex.fe_logical = start;
3312 ac->ac_g_ex.fe_len = size;
3313
3314 /* define goal start in order to merge */
3315 if (ar->pright && (ar->lright == (start + size))) {
3316 /* merge to the right */
3317 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3318 &ac->ac_f_ex.fe_group,
3319 &ac->ac_f_ex.fe_start);
3320 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3321 }
3322 if (ar->pleft && (ar->lleft + 1 == start)) {
3323 /* merge to the left */
3324 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3325 &ac->ac_f_ex.fe_group,
3326 &ac->ac_f_ex.fe_start);
3327 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3328 }
3329
3330 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3331 (unsigned) orig_size, (unsigned) start);
3332}
3333
3334static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3335{
3336 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3337
3338 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3339 atomic_inc(&sbi->s_bal_reqs);
3340 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3341 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3342 atomic_inc(&sbi->s_bal_success);
3343 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3344 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3345 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3346 atomic_inc(&sbi->s_bal_goals);
3347 if (ac->ac_found > sbi->s_mb_max_to_scan)
3348 atomic_inc(&sbi->s_bal_breaks);
3349 }
3350
3351 ext4_mb_store_history(ac);
3352}
3353
3354/*
3355 * use blocks preallocated to inode
3356 */
3357static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3358 struct ext4_prealloc_space *pa)
3359{
3360 ext4_fsblk_t start;
3361 ext4_fsblk_t end;
3362 int len;
3363
3364 /* found preallocated blocks, use them */
3365 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3366 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3367 len = end - start;
3368 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3369 &ac->ac_b_ex.fe_start);
3370 ac->ac_b_ex.fe_len = len;
3371 ac->ac_status = AC_STATUS_FOUND;
3372 ac->ac_pa = pa;
3373
3374 BUG_ON(start < pa->pa_pstart);
3375 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3376 BUG_ON(pa->pa_free < len);
3377 pa->pa_free -= len;
3378
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003379 mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003380}
3381
3382/*
3383 * use blocks preallocated to locality group
3384 */
3385static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3386 struct ext4_prealloc_space *pa)
3387{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003388 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003389
Alex Tomasc9de5602008-01-29 00:19:52 -05003390 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3391 &ac->ac_b_ex.fe_group,
3392 &ac->ac_b_ex.fe_start);
3393 ac->ac_b_ex.fe_len = len;
3394 ac->ac_status = AC_STATUS_FOUND;
3395 ac->ac_pa = pa;
3396
3397 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003398 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003399 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003400 * in on-disk bitmap -- see ext4_mb_release_context()
3401 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003402 */
3403 mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3404}
3405
3406/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003407 * Return the prealloc space that have minimal distance
3408 * from the goal block. @cpa is the prealloc
3409 * space that is having currently known minimal distance
3410 * from the goal block.
3411 */
3412static struct ext4_prealloc_space *
3413ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3414 struct ext4_prealloc_space *pa,
3415 struct ext4_prealloc_space *cpa)
3416{
3417 ext4_fsblk_t cur_distance, new_distance;
3418
3419 if (cpa == NULL) {
3420 atomic_inc(&pa->pa_count);
3421 return pa;
3422 }
3423 cur_distance = abs(goal_block - cpa->pa_pstart);
3424 new_distance = abs(goal_block - pa->pa_pstart);
3425
3426 if (cur_distance < new_distance)
3427 return cpa;
3428
3429 /* drop the previous reference */
3430 atomic_dec(&cpa->pa_count);
3431 atomic_inc(&pa->pa_count);
3432 return pa;
3433}
3434
3435/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003436 * search goal blocks in preallocated space
3437 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003438static noinline_for_stack int
3439ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003440{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003441 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003442 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3443 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003444 struct ext4_prealloc_space *pa, *cpa = NULL;
3445 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003446
3447 /* only data can be preallocated */
3448 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3449 return 0;
3450
3451 /* first, try per-file preallocation */
3452 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003453 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003454
3455 /* all fields in this condition don't change,
3456 * so we can skip locking for them */
3457 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3458 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3459 continue;
3460
3461 /* found preallocated blocks, use them */
3462 spin_lock(&pa->pa_lock);
3463 if (pa->pa_deleted == 0 && pa->pa_free) {
3464 atomic_inc(&pa->pa_count);
3465 ext4_mb_use_inode_pa(ac, pa);
3466 spin_unlock(&pa->pa_lock);
3467 ac->ac_criteria = 10;
3468 rcu_read_unlock();
3469 return 1;
3470 }
3471 spin_unlock(&pa->pa_lock);
3472 }
3473 rcu_read_unlock();
3474
3475 /* can we use group allocation? */
3476 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3477 return 0;
3478
3479 /* inode may have no locality group for some reason */
3480 lg = ac->ac_lg;
3481 if (lg == NULL)
3482 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003483 order = fls(ac->ac_o_ex.fe_len) - 1;
3484 if (order > PREALLOC_TB_SIZE - 1)
3485 /* The max size of hash table is PREALLOC_TB_SIZE */
3486 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003487
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003488 goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
3489 ac->ac_g_ex.fe_start +
3490 le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
3491 /*
3492 * search for the prealloc space that is having
3493 * minimal distance from the goal block.
3494 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003495 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3496 rcu_read_lock();
3497 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3498 pa_inode_list) {
3499 spin_lock(&pa->pa_lock);
3500 if (pa->pa_deleted == 0 &&
3501 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003502
3503 cpa = ext4_mb_check_group_pa(goal_block,
3504 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003505 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003506 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003507 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003508 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003509 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003510 if (cpa) {
3511 ext4_mb_use_group_pa(ac, cpa);
3512 ac->ac_criteria = 20;
3513 return 1;
3514 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003515 return 0;
3516}
3517
3518/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003519 * the function goes through all block freed in the group
3520 * but not yet committed and marks them used in in-core bitmap.
3521 * buddy must be generated from this bitmap
3522 * Need to be called with ext4 group lock (ext4_lock_group)
3523 */
3524static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3525 ext4_group_t group)
3526{
3527 struct rb_node *n;
3528 struct ext4_group_info *grp;
3529 struct ext4_free_data *entry;
3530
3531 grp = ext4_get_group_info(sb, group);
3532 n = rb_first(&(grp->bb_free_root));
3533
3534 while (n) {
3535 entry = rb_entry(n, struct ext4_free_data, node);
3536 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3537 bitmap, entry->start_blk,
3538 entry->count);
3539 n = rb_next(n);
3540 }
3541 return;
3542}
3543
3544/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 * the function goes through all preallocation in this group and marks them
3546 * used in in-core bitmap. buddy must be generated from this bitmap
3547 * Need to be called with ext4 group lock (ext4_lock_group)
3548 */
3549static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3550 ext4_group_t group)
3551{
3552 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3553 struct ext4_prealloc_space *pa;
3554 struct list_head *cur;
3555 ext4_group_t groupnr;
3556 ext4_grpblk_t start;
3557 int preallocated = 0;
3558 int count = 0;
3559 int len;
3560
3561 /* all form of preallocation discards first load group,
3562 * so the only competing code is preallocation use.
3563 * we don't need any locking here
3564 * notice we do NOT ignore preallocations with pa_deleted
3565 * otherwise we could leave used blocks available for
3566 * allocation in buddy when concurrent ext4_mb_put_pa()
3567 * is dropping preallocation
3568 */
3569 list_for_each(cur, &grp->bb_prealloc_list) {
3570 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3571 spin_lock(&pa->pa_lock);
3572 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3573 &groupnr, &start);
3574 len = pa->pa_len;
3575 spin_unlock(&pa->pa_lock);
3576 if (unlikely(len == 0))
3577 continue;
3578 BUG_ON(groupnr != group);
3579 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3580 bitmap, start, len);
3581 preallocated += len;
3582 count++;
3583 }
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003584 mb_debug("prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003585}
3586
3587static void ext4_mb_pa_callback(struct rcu_head *head)
3588{
3589 struct ext4_prealloc_space *pa;
3590 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3591 kmem_cache_free(ext4_pspace_cachep, pa);
3592}
3593
3594/*
3595 * drops a reference to preallocated space descriptor
3596 * if this was the last reference and the space is consumed
3597 */
3598static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3599 struct super_block *sb, struct ext4_prealloc_space *pa)
3600{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003601 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003602 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003603
3604 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3605 return;
3606
3607 /* in this short window concurrent discard can set pa_deleted */
3608 spin_lock(&pa->pa_lock);
3609 if (pa->pa_deleted == 1) {
3610 spin_unlock(&pa->pa_lock);
3611 return;
3612 }
3613
3614 pa->pa_deleted = 1;
3615 spin_unlock(&pa->pa_lock);
3616
Eric Sandeend33a1972009-03-16 23:25:40 -04003617 grp_blk = pa->pa_pstart;
3618 /* If linear, pa_pstart may be in the next group when pa is used up */
3619 if (pa->pa_linear)
3620 grp_blk--;
3621
3622 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003623
3624 /*
3625 * possible race:
3626 *
3627 * P1 (buddy init) P2 (regular allocation)
3628 * find block B in PA
3629 * copy on-disk bitmap to buddy
3630 * mark B in on-disk bitmap
3631 * drop PA from group
3632 * mark all PAs in buddy
3633 *
3634 * thus, P1 initializes buddy with B available. to prevent this
3635 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3636 * against that pair
3637 */
3638 ext4_lock_group(sb, grp);
3639 list_del(&pa->pa_group_list);
3640 ext4_unlock_group(sb, grp);
3641
3642 spin_lock(pa->pa_obj_lock);
3643 list_del_rcu(&pa->pa_inode_list);
3644 spin_unlock(pa->pa_obj_lock);
3645
3646 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3647}
3648
3649/*
3650 * creates new preallocated space for given inode
3651 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003652static noinline_for_stack int
3653ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003654{
3655 struct super_block *sb = ac->ac_sb;
3656 struct ext4_prealloc_space *pa;
3657 struct ext4_group_info *grp;
3658 struct ext4_inode_info *ei;
3659
3660 /* preallocate only when found space is larger then requested */
3661 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3662 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3663 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3664
3665 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3666 if (pa == NULL)
3667 return -ENOMEM;
3668
3669 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3670 int winl;
3671 int wins;
3672 int win;
3673 int offs;
3674
3675 /* we can't allocate as much as normalizer wants.
3676 * so, found space must get proper lstart
3677 * to cover original request */
3678 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3679 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3680
3681 /* we're limited by original request in that
3682 * logical block must be covered any way
3683 * winl is window we can move our chunk within */
3684 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3685
3686 /* also, we should cover whole original request */
3687 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3688
3689 /* the smallest one defines real window */
3690 win = min(winl, wins);
3691
3692 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3693 if (offs && offs < win)
3694 win = offs;
3695
3696 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3697 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3698 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3699 }
3700
3701 /* preallocation can change ac_b_ex, thus we store actually
3702 * allocated blocks for history */
3703 ac->ac_f_ex = ac->ac_b_ex;
3704
3705 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3706 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3707 pa->pa_len = ac->ac_b_ex.fe_len;
3708 pa->pa_free = pa->pa_len;
3709 atomic_set(&pa->pa_count, 1);
3710 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003711 INIT_LIST_HEAD(&pa->pa_inode_list);
3712 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003713 pa->pa_deleted = 0;
3714 pa->pa_linear = 0;
3715
3716 mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
3717 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003718 trace_mark(ext4_mb_new_inode_pa,
3719 "dev %s ino %lu pstart %llu len %u lstart %u",
3720 sb->s_id, ac->ac_inode->i_ino,
3721 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05003722
3723 ext4_mb_use_inode_pa(ac, pa);
3724 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3725
3726 ei = EXT4_I(ac->ac_inode);
3727 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3728
3729 pa->pa_obj_lock = &ei->i_prealloc_lock;
3730 pa->pa_inode = ac->ac_inode;
3731
3732 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3733 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3734 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3735
3736 spin_lock(pa->pa_obj_lock);
3737 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3738 spin_unlock(pa->pa_obj_lock);
3739
3740 return 0;
3741}
3742
3743/*
3744 * creates new preallocated space for locality group inodes belongs to
3745 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003746static noinline_for_stack int
3747ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003748{
3749 struct super_block *sb = ac->ac_sb;
3750 struct ext4_locality_group *lg;
3751 struct ext4_prealloc_space *pa;
3752 struct ext4_group_info *grp;
3753
3754 /* preallocate only when found space is larger then requested */
3755 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3756 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3757 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3758
3759 BUG_ON(ext4_pspace_cachep == NULL);
3760 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3761 if (pa == NULL)
3762 return -ENOMEM;
3763
3764 /* preallocation can change ac_b_ex, thus we store actually
3765 * allocated blocks for history */
3766 ac->ac_f_ex = ac->ac_b_ex;
3767
3768 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3769 pa->pa_lstart = pa->pa_pstart;
3770 pa->pa_len = ac->ac_b_ex.fe_len;
3771 pa->pa_free = pa->pa_len;
3772 atomic_set(&pa->pa_count, 1);
3773 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003774 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003775 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003776 pa->pa_deleted = 0;
3777 pa->pa_linear = 1;
3778
3779 mb_debug("new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'oba80b102009-01-03 20:03:21 -05003780 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3781 trace_mark(ext4_mb_new_group_pa, "dev %s pstart %llu len %u lstart %u",
3782 sb->s_id, pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05003783
3784 ext4_mb_use_group_pa(ac, pa);
3785 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3786
3787 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3788 lg = ac->ac_lg;
3789 BUG_ON(lg == NULL);
3790
3791 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3792 pa->pa_inode = NULL;
3793
3794 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3795 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3796 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3797
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003798 /*
3799 * We will later add the new pa to the right bucket
3800 * after updating the pa_free in ext4_mb_release_context
3801 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003802 return 0;
3803}
3804
3805static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3806{
3807 int err;
3808
3809 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3810 err = ext4_mb_new_group_pa(ac);
3811 else
3812 err = ext4_mb_new_inode_pa(ac);
3813 return err;
3814}
3815
3816/*
3817 * finds all unused blocks in on-disk bitmap, frees them in
3818 * in-core bitmap and buddy.
3819 * @pa must be unlinked from inode and group lists, so that
3820 * nobody else can find/use it.
3821 * the caller MUST hold group/inode locks.
3822 * TODO: optimize the case when there are no in-core structures yet
3823 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003824static noinline_for_stack int
3825ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003826 struct ext4_prealloc_space *pa,
3827 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003828{
Alex Tomasc9de5602008-01-29 00:19:52 -05003829 struct super_block *sb = e4b->bd_sb;
3830 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003831 unsigned int end;
3832 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003833 ext4_group_t group;
3834 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003835 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003836 sector_t start;
3837 int err = 0;
3838 int free = 0;
3839
3840 BUG_ON(pa->pa_deleted == 0);
3841 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003842 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003843 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3844 end = bit + pa->pa_len;
3845
Eric Sandeen256bdb42008-02-10 01:13:33 -05003846 if (ac) {
3847 ac->ac_sb = sb;
3848 ac->ac_inode = pa->pa_inode;
3849 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3850 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003851
3852 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003853 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003854 if (bit >= end)
3855 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003856 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003857 start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3858 le32_to_cpu(sbi->s_es->s_first_data_block);
3859 mb_debug(" free preallocated %u/%u in group %u\n",
3860 (unsigned) start, (unsigned) next - bit,
3861 (unsigned) group);
3862 free += next - bit;
3863
Eric Sandeen256bdb42008-02-10 01:13:33 -05003864 if (ac) {
3865 ac->ac_b_ex.fe_group = group;
3866 ac->ac_b_ex.fe_start = bit;
3867 ac->ac_b_ex.fe_len = next - bit;
3868 ac->ac_b_ex.fe_logical = 0;
3869 ext4_mb_store_history(ac);
3870 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003871
Theodore Ts'oba80b102009-01-03 20:03:21 -05003872 trace_mark(ext4_mb_release_inode_pa,
3873 "dev %s ino %lu block %llu count %u",
3874 sb->s_id, pa->pa_inode->i_ino, grp_blk_start + bit,
3875 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003876 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3877 bit = next + 1;
3878 }
3879 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003880 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003881 pa, (unsigned long) pa->pa_lstart,
3882 (unsigned long) pa->pa_pstart,
3883 (unsigned long) pa->pa_len);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003884 ext4_grp_locked_error(sb, group,
3885 __func__, "free %u, pa_free %u",
3886 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003887 /*
3888 * pa is already deleted so we use the value obtained
3889 * from the bitmap and continue.
3890 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003891 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003892 atomic_add(free, &sbi->s_mb_discarded);
3893
3894 return err;
3895}
3896
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003897static noinline_for_stack int
3898ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003899 struct ext4_prealloc_space *pa,
3900 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003901{
Alex Tomasc9de5602008-01-29 00:19:52 -05003902 struct super_block *sb = e4b->bd_sb;
3903 ext4_group_t group;
3904 ext4_grpblk_t bit;
3905
Eric Sandeen256bdb42008-02-10 01:13:33 -05003906 if (ac)
3907 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
Alex Tomasc9de5602008-01-29 00:19:52 -05003908
Theodore Ts'oba80b102009-01-03 20:03:21 -05003909 trace_mark(ext4_mb_release_group_pa, "dev %s pstart %llu len %d",
3910 sb->s_id, pa->pa_pstart, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003911 BUG_ON(pa->pa_deleted == 0);
3912 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3913 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3914 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3915 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3916
Eric Sandeen256bdb42008-02-10 01:13:33 -05003917 if (ac) {
3918 ac->ac_sb = sb;
3919 ac->ac_inode = NULL;
3920 ac->ac_b_ex.fe_group = group;
3921 ac->ac_b_ex.fe_start = bit;
3922 ac->ac_b_ex.fe_len = pa->pa_len;
3923 ac->ac_b_ex.fe_logical = 0;
3924 ext4_mb_store_history(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003925 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003926
3927 return 0;
3928}
3929
3930/*
3931 * releases all preallocations in given group
3932 *
3933 * first, we need to decide discard policy:
3934 * - when do we discard
3935 * 1) ENOSPC
3936 * - how many do we discard
3937 * 1) how many requested
3938 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003939static noinline_for_stack int
3940ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003941 ext4_group_t group, int needed)
3942{
3943 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3944 struct buffer_head *bitmap_bh = NULL;
3945 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003946 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003947 struct list_head list;
3948 struct ext4_buddy e4b;
3949 int err;
3950 int busy = 0;
3951 int free = 0;
3952
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003953 mb_debug("discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003954
3955 if (list_empty(&grp->bb_prealloc_list))
3956 return 0;
3957
Theodore Ts'o574ca172008-07-11 19:27:31 -04003958 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003959 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003960 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003961 "bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003962 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003963 }
3964
3965 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003966 if (err) {
3967 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003968 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003969 put_bh(bitmap_bh);
3970 return 0;
3971 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003972
3973 if (needed == 0)
3974 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3975
Alex Tomasc9de5602008-01-29 00:19:52 -05003976 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003977 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05003978repeat:
3979 ext4_lock_group(sb, group);
3980 list_for_each_entry_safe(pa, tmp,
3981 &grp->bb_prealloc_list, pa_group_list) {
3982 spin_lock(&pa->pa_lock);
3983 if (atomic_read(&pa->pa_count)) {
3984 spin_unlock(&pa->pa_lock);
3985 busy = 1;
3986 continue;
3987 }
3988 if (pa->pa_deleted) {
3989 spin_unlock(&pa->pa_lock);
3990 continue;
3991 }
3992
3993 /* seems this one can be freed ... */
3994 pa->pa_deleted = 1;
3995
3996 /* we can trust pa_free ... */
3997 free += pa->pa_free;
3998
3999 spin_unlock(&pa->pa_lock);
4000
4001 list_del(&pa->pa_group_list);
4002 list_add(&pa->u.pa_tmp_list, &list);
4003 }
4004
4005 /* if we still need more blocks and some PAs were used, try again */
4006 if (free < needed && busy) {
4007 busy = 0;
4008 ext4_unlock_group(sb, group);
4009 /*
4010 * Yield the CPU here so that we don't get soft lockup
4011 * in non preempt case.
4012 */
4013 yield();
4014 goto repeat;
4015 }
4016
4017 /* found anything to free? */
4018 if (list_empty(&list)) {
4019 BUG_ON(free != 0);
4020 goto out;
4021 }
4022
4023 /* now free all selected PAs */
4024 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4025
4026 /* remove from object (inode or locality group) */
4027 spin_lock(pa->pa_obj_lock);
4028 list_del_rcu(&pa->pa_inode_list);
4029 spin_unlock(pa->pa_obj_lock);
4030
4031 if (pa->pa_linear)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004032 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004033 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004034 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004035
4036 list_del(&pa->u.pa_tmp_list);
4037 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4038 }
4039
4040out:
4041 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004042 if (ac)
4043 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004044 ext4_mb_release_desc(&e4b);
4045 put_bh(bitmap_bh);
4046 return free;
4047}
4048
4049/*
4050 * releases all non-used preallocated blocks for given inode
4051 *
4052 * It's important to discard preallocations under i_data_sem
4053 * We don't want another block to be served from the prealloc
4054 * space when we are discarding the inode prealloc space.
4055 *
4056 * FIXME!! Make sure it is valid at all the call sites
4057 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004058void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05004059{
4060 struct ext4_inode_info *ei = EXT4_I(inode);
4061 struct super_block *sb = inode->i_sb;
4062 struct buffer_head *bitmap_bh = NULL;
4063 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004064 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05004065 ext4_group_t group = 0;
4066 struct list_head list;
4067 struct ext4_buddy e4b;
4068 int err;
4069
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004070 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004071 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4072 return;
4073 }
4074
4075 mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004076 trace_mark(ext4_discard_preallocations, "dev %s ino %lu", sb->s_id,
4077 inode->i_ino);
Alex Tomasc9de5602008-01-29 00:19:52 -05004078
4079 INIT_LIST_HEAD(&list);
4080
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004081 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05004082repeat:
4083 /* first, collect all pa's in the inode */
4084 spin_lock(&ei->i_prealloc_lock);
4085 while (!list_empty(&ei->i_prealloc_list)) {
4086 pa = list_entry(ei->i_prealloc_list.next,
4087 struct ext4_prealloc_space, pa_inode_list);
4088 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4089 spin_lock(&pa->pa_lock);
4090 if (atomic_read(&pa->pa_count)) {
4091 /* this shouldn't happen often - nobody should
4092 * use preallocation while we're discarding it */
4093 spin_unlock(&pa->pa_lock);
4094 spin_unlock(&ei->i_prealloc_lock);
4095 printk(KERN_ERR "uh-oh! used pa while discarding\n");
4096 WARN_ON(1);
4097 schedule_timeout_uninterruptible(HZ);
4098 goto repeat;
4099
4100 }
4101 if (pa->pa_deleted == 0) {
4102 pa->pa_deleted = 1;
4103 spin_unlock(&pa->pa_lock);
4104 list_del_rcu(&pa->pa_inode_list);
4105 list_add(&pa->u.pa_tmp_list, &list);
4106 continue;
4107 }
4108
4109 /* someone is deleting pa right now */
4110 spin_unlock(&pa->pa_lock);
4111 spin_unlock(&ei->i_prealloc_lock);
4112
4113 /* we have to wait here because pa_deleted
4114 * doesn't mean pa is already unlinked from
4115 * the list. as we might be called from
4116 * ->clear_inode() the inode will get freed
4117 * and concurrent thread which is unlinking
4118 * pa from inode's list may access already
4119 * freed memory, bad-bad-bad */
4120
4121 /* XXX: if this happens too often, we can
4122 * add a flag to force wait only in case
4123 * of ->clear_inode(), but not in case of
4124 * regular truncate */
4125 schedule_timeout_uninterruptible(HZ);
4126 goto repeat;
4127 }
4128 spin_unlock(&ei->i_prealloc_lock);
4129
4130 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4131 BUG_ON(pa->pa_linear != 0);
4132 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4133
4134 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004135 if (err) {
4136 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004137 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004138 continue;
4139 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004140
Theodore Ts'o574ca172008-07-11 19:27:31 -04004141 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004142 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004143 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004144 "bitmap for %u", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004145 ext4_mb_release_desc(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004146 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004147 }
4148
4149 ext4_lock_group(sb, group);
4150 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004151 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004152 ext4_unlock_group(sb, group);
4153
4154 ext4_mb_release_desc(&e4b);
4155 put_bh(bitmap_bh);
4156
4157 list_del(&pa->u.pa_tmp_list);
4158 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4159 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004160 if (ac)
4161 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004162}
4163
4164/*
4165 * finds all preallocated spaces and return blocks being freed to them
4166 * if preallocated space becomes full (no block is used from the space)
4167 * then the function frees space in buddy
4168 * XXX: at the moment, truncate (which is the only way to free blocks)
4169 * discards all preallocations
4170 */
4171static void ext4_mb_return_to_preallocation(struct inode *inode,
4172 struct ext4_buddy *e4b,
4173 sector_t block, int count)
4174{
4175 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4176}
4177#ifdef MB_DEBUG
4178static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4179{
4180 struct super_block *sb = ac->ac_sb;
4181 ext4_group_t i;
4182
4183 printk(KERN_ERR "EXT4-fs: Can't allocate:"
4184 " Allocation context details:\n");
4185 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4186 ac->ac_status, ac->ac_flags);
4187 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4188 "best %lu/%lu/%lu@%lu cr %d\n",
4189 (unsigned long)ac->ac_o_ex.fe_group,
4190 (unsigned long)ac->ac_o_ex.fe_start,
4191 (unsigned long)ac->ac_o_ex.fe_len,
4192 (unsigned long)ac->ac_o_ex.fe_logical,
4193 (unsigned long)ac->ac_g_ex.fe_group,
4194 (unsigned long)ac->ac_g_ex.fe_start,
4195 (unsigned long)ac->ac_g_ex.fe_len,
4196 (unsigned long)ac->ac_g_ex.fe_logical,
4197 (unsigned long)ac->ac_b_ex.fe_group,
4198 (unsigned long)ac->ac_b_ex.fe_start,
4199 (unsigned long)ac->ac_b_ex.fe_len,
4200 (unsigned long)ac->ac_b_ex.fe_logical,
4201 (int)ac->ac_criteria);
4202 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4203 ac->ac_found);
4204 printk(KERN_ERR "EXT4-fs: groups: \n");
4205 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
4206 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4207 struct ext4_prealloc_space *pa;
4208 ext4_grpblk_t start;
4209 struct list_head *cur;
4210 ext4_lock_group(sb, i);
4211 list_for_each(cur, &grp->bb_prealloc_list) {
4212 pa = list_entry(cur, struct ext4_prealloc_space,
4213 pa_group_list);
4214 spin_lock(&pa->pa_lock);
4215 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4216 NULL, &start);
4217 spin_unlock(&pa->pa_lock);
4218 printk(KERN_ERR "PA:%lu:%d:%u \n", i,
4219 start, pa->pa_len);
4220 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004221 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004222
4223 if (grp->bb_free == 0)
4224 continue;
4225 printk(KERN_ERR "%lu: %d/%d \n",
4226 i, grp->bb_free, grp->bb_fragments);
4227 }
4228 printk(KERN_ERR "\n");
4229}
4230#else
4231static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4232{
4233 return;
4234}
4235#endif
4236
4237/*
4238 * We use locality group preallocation for small size file. The size of the
4239 * file is determined by the current size or the resulting size after
4240 * allocation which ever is larger
4241 *
4242 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4243 */
4244static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4245{
4246 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4247 int bsbits = ac->ac_sb->s_blocksize_bits;
4248 loff_t size, isize;
4249
4250 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4251 return;
4252
4253 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4254 isize = i_size_read(ac->ac_inode) >> bsbits;
4255 size = max(size, isize);
4256
4257 /* don't use group allocation for large files */
4258 if (size >= sbi->s_mb_stream_request)
4259 return;
4260
4261 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4262 return;
4263
4264 BUG_ON(ac->ac_lg != NULL);
4265 /*
4266 * locality group prealloc space are per cpu. The reason for having
4267 * per cpu locality group is to reduce the contention between block
4268 * request from multiple CPUs.
4269 */
Eric Sandeen730c2132008-09-13 15:23:29 -04004270 ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
Alex Tomasc9de5602008-01-29 00:19:52 -05004271
4272 /* we're going to use group allocation */
4273 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4274
4275 /* serialize all allocations in the group */
4276 mutex_lock(&ac->ac_lg->lg_mutex);
4277}
4278
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004279static noinline_for_stack int
4280ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004281 struct ext4_allocation_request *ar)
4282{
4283 struct super_block *sb = ar->inode->i_sb;
4284 struct ext4_sb_info *sbi = EXT4_SB(sb);
4285 struct ext4_super_block *es = sbi->s_es;
4286 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004287 unsigned int len;
4288 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004289 ext4_grpblk_t block;
4290
4291 /* we can't allocate > group size */
4292 len = ar->len;
4293
4294 /* just a dirty hack to filter too big requests */
4295 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4296 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4297
4298 /* start searching from the goal */
4299 goal = ar->goal;
4300 if (goal < le32_to_cpu(es->s_first_data_block) ||
4301 goal >= ext4_blocks_count(es))
4302 goal = le32_to_cpu(es->s_first_data_block);
4303 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4304
4305 /* set up allocation goals */
4306 ac->ac_b_ex.fe_logical = ar->logical;
4307 ac->ac_b_ex.fe_group = 0;
4308 ac->ac_b_ex.fe_start = 0;
4309 ac->ac_b_ex.fe_len = 0;
4310 ac->ac_status = AC_STATUS_CONTINUE;
4311 ac->ac_groups_scanned = 0;
4312 ac->ac_ex_scanned = 0;
4313 ac->ac_found = 0;
4314 ac->ac_sb = sb;
4315 ac->ac_inode = ar->inode;
4316 ac->ac_o_ex.fe_logical = ar->logical;
4317 ac->ac_o_ex.fe_group = group;
4318 ac->ac_o_ex.fe_start = block;
4319 ac->ac_o_ex.fe_len = len;
4320 ac->ac_g_ex.fe_logical = ar->logical;
4321 ac->ac_g_ex.fe_group = group;
4322 ac->ac_g_ex.fe_start = block;
4323 ac->ac_g_ex.fe_len = len;
4324 ac->ac_f_ex.fe_len = 0;
4325 ac->ac_flags = ar->flags;
4326 ac->ac_2order = 0;
4327 ac->ac_criteria = 0;
4328 ac->ac_pa = NULL;
4329 ac->ac_bitmap_page = NULL;
4330 ac->ac_buddy_page = NULL;
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004331 ac->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004332 ac->ac_lg = NULL;
4333
4334 /* we have to define context: we'll we work with a file or
4335 * locality group. this is a policy, actually */
4336 ext4_mb_group_or_file(ac);
4337
4338 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4339 "left: %u/%u, right %u/%u to %swritable\n",
4340 (unsigned) ar->len, (unsigned) ar->logical,
4341 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4342 (unsigned) ar->lleft, (unsigned) ar->pleft,
4343 (unsigned) ar->lright, (unsigned) ar->pright,
4344 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4345 return 0;
4346
4347}
4348
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004349static noinline_for_stack void
4350ext4_mb_discard_lg_preallocations(struct super_block *sb,
4351 struct ext4_locality_group *lg,
4352 int order, int total_entries)
4353{
4354 ext4_group_t group = 0;
4355 struct ext4_buddy e4b;
4356 struct list_head discard_list;
4357 struct ext4_prealloc_space *pa, *tmp;
4358 struct ext4_allocation_context *ac;
4359
4360 mb_debug("discard locality group preallocation\n");
4361
4362 INIT_LIST_HEAD(&discard_list);
4363 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4364
4365 spin_lock(&lg->lg_prealloc_lock);
4366 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4367 pa_inode_list) {
4368 spin_lock(&pa->pa_lock);
4369 if (atomic_read(&pa->pa_count)) {
4370 /*
4371 * This is the pa that we just used
4372 * for block allocation. So don't
4373 * free that
4374 */
4375 spin_unlock(&pa->pa_lock);
4376 continue;
4377 }
4378 if (pa->pa_deleted) {
4379 spin_unlock(&pa->pa_lock);
4380 continue;
4381 }
4382 /* only lg prealloc space */
4383 BUG_ON(!pa->pa_linear);
4384
4385 /* seems this one can be freed ... */
4386 pa->pa_deleted = 1;
4387 spin_unlock(&pa->pa_lock);
4388
4389 list_del_rcu(&pa->pa_inode_list);
4390 list_add(&pa->u.pa_tmp_list, &discard_list);
4391
4392 total_entries--;
4393 if (total_entries <= 5) {
4394 /*
4395 * we want to keep only 5 entries
4396 * allowing it to grow to 8. This
4397 * mak sure we don't call discard
4398 * soon for this list.
4399 */
4400 break;
4401 }
4402 }
4403 spin_unlock(&lg->lg_prealloc_lock);
4404
4405 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4406
4407 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4408 if (ext4_mb_load_buddy(sb, group, &e4b)) {
4409 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004410 "information for %u", group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004411 continue;
4412 }
4413 ext4_lock_group(sb, group);
4414 list_del(&pa->pa_group_list);
4415 ext4_mb_release_group_pa(&e4b, pa, ac);
4416 ext4_unlock_group(sb, group);
4417
4418 ext4_mb_release_desc(&e4b);
4419 list_del(&pa->u.pa_tmp_list);
4420 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4421 }
4422 if (ac)
4423 kmem_cache_free(ext4_ac_cachep, ac);
4424}
4425
4426/*
4427 * We have incremented pa_count. So it cannot be freed at this
4428 * point. Also we hold lg_mutex. So no parallel allocation is
4429 * possible from this lg. That means pa_free cannot be updated.
4430 *
4431 * A parallel ext4_mb_discard_group_preallocations is possible.
4432 * which can cause the lg_prealloc_list to be updated.
4433 */
4434
4435static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4436{
4437 int order, added = 0, lg_prealloc_count = 1;
4438 struct super_block *sb = ac->ac_sb;
4439 struct ext4_locality_group *lg = ac->ac_lg;
4440 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4441
4442 order = fls(pa->pa_free) - 1;
4443 if (order > PREALLOC_TB_SIZE - 1)
4444 /* The max size of hash table is PREALLOC_TB_SIZE */
4445 order = PREALLOC_TB_SIZE - 1;
4446 /* Add the prealloc space to lg */
4447 rcu_read_lock();
4448 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4449 pa_inode_list) {
4450 spin_lock(&tmp_pa->pa_lock);
4451 if (tmp_pa->pa_deleted) {
4452 spin_unlock(&pa->pa_lock);
4453 continue;
4454 }
4455 if (!added && pa->pa_free < tmp_pa->pa_free) {
4456 /* Add to the tail of the previous entry */
4457 list_add_tail_rcu(&pa->pa_inode_list,
4458 &tmp_pa->pa_inode_list);
4459 added = 1;
4460 /*
4461 * we want to count the total
4462 * number of entries in the list
4463 */
4464 }
4465 spin_unlock(&tmp_pa->pa_lock);
4466 lg_prealloc_count++;
4467 }
4468 if (!added)
4469 list_add_tail_rcu(&pa->pa_inode_list,
4470 &lg->lg_prealloc_list[order]);
4471 rcu_read_unlock();
4472
4473 /* Now trim the list to be not more than 8 elements */
4474 if (lg_prealloc_count > 8) {
4475 ext4_mb_discard_lg_preallocations(sb, lg,
4476 order, lg_prealloc_count);
4477 return;
4478 }
4479 return ;
4480}
4481
Alex Tomasc9de5602008-01-29 00:19:52 -05004482/*
4483 * release all resource we used in allocation
4484 */
4485static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4486{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004487 struct ext4_prealloc_space *pa = ac->ac_pa;
4488 if (pa) {
4489 if (pa->pa_linear) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004490 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004491 spin_lock(&pa->pa_lock);
4492 pa->pa_pstart += ac->ac_b_ex.fe_len;
4493 pa->pa_lstart += ac->ac_b_ex.fe_len;
4494 pa->pa_free -= ac->ac_b_ex.fe_len;
4495 pa->pa_len -= ac->ac_b_ex.fe_len;
4496 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004497 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004498 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004499 if (ac->alloc_semp)
4500 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004501 if (pa) {
4502 /*
4503 * We want to add the pa to the right bucket.
4504 * Remove it from the list and while adding
4505 * make sure the list to which we are adding
4506 * doesn't grow big. We need to release
4507 * alloc_semp before calling ext4_mb_add_n_trim()
4508 */
4509 if (pa->pa_linear && likely(pa->pa_free)) {
4510 spin_lock(pa->pa_obj_lock);
4511 list_del_rcu(&pa->pa_inode_list);
4512 spin_unlock(pa->pa_obj_lock);
4513 ext4_mb_add_n_trim(ac);
4514 }
4515 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4516 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004517 if (ac->ac_bitmap_page)
4518 page_cache_release(ac->ac_bitmap_page);
4519 if (ac->ac_buddy_page)
4520 page_cache_release(ac->ac_buddy_page);
4521 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4522 mutex_unlock(&ac->ac_lg->lg_mutex);
4523 ext4_mb_collect_stats(ac);
4524 return 0;
4525}
4526
4527static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4528{
4529 ext4_group_t i;
4530 int ret;
4531 int freed = 0;
4532
Theodore Ts'oba80b102009-01-03 20:03:21 -05004533 trace_mark(ext4_mb_discard_preallocations, "dev %s needed %d",
4534 sb->s_id, needed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004535 for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
4536 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4537 freed += ret;
4538 needed -= ret;
4539 }
4540
4541 return freed;
4542}
4543
4544/*
4545 * Main entry point into mballoc to allocate blocks
4546 * it tries to use preallocation first, then falls back
4547 * to usual allocation
4548 */
4549ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4550 struct ext4_allocation_request *ar, int *errp)
4551{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004552 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004553 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004554 struct ext4_sb_info *sbi;
4555 struct super_block *sb;
4556 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004557 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004558 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004559
4560 sb = ar->inode->i_sb;
4561 sbi = EXT4_SB(sb);
4562
Theodore Ts'oba80b102009-01-03 20:03:21 -05004563 trace_mark(ext4_request_blocks, "dev %s flags %u len %u ino %lu "
4564 "lblk %llu goal %llu lleft %llu lright %llu "
4565 "pleft %llu pright %llu ",
4566 sb->s_id, ar->flags, ar->len,
4567 ar->inode ? ar->inode->i_ino : 0,
4568 (unsigned long long) ar->logical,
4569 (unsigned long long) ar->goal,
4570 (unsigned long long) ar->lleft,
4571 (unsigned long long) ar->lright,
4572 (unsigned long long) ar->pleft,
4573 (unsigned long long) ar->pright);
4574
Mingming Cao60e58e02009-01-22 18:13:05 +01004575 /*
4576 * For delayed allocation, we could skip the ENOSPC and
4577 * EDQUOT check, as blocks and quotas have been already
4578 * reserved when data being copied into pagecache.
4579 */
4580 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4581 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4582 else {
4583 /* Without delayed allocation we need to verify
4584 * there is enough free blocks to do block allocation
4585 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004586 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004587 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4588 /* let others to free the space */
4589 yield();
4590 ar->len = ar->len >> 1;
4591 }
4592 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004593 *errp = -ENOSPC;
4594 return 0;
4595 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004596 reserv_blks = ar->len;
Jan Karaa269eb12009-01-26 17:04:39 +01004597 while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004598 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4599 ar->len--;
4600 }
4601 inquota = ar->len;
4602 if (ar->len == 0) {
4603 *errp = -EDQUOT;
4604 goto out3;
4605 }
Mingming Caod2a17632008-07-14 17:52:37 -04004606 }
Mingming Caod2a17632008-07-14 17:52:37 -04004607
Eric Sandeen256bdb42008-02-10 01:13:33 -05004608 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4609 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004610 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004611 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004612 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004613 }
4614
Eric Sandeen256bdb42008-02-10 01:13:33 -05004615 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004616 if (*errp) {
4617 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004618 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004619 }
4620
Eric Sandeen256bdb42008-02-10 01:13:33 -05004621 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4622 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004623 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4624 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004625repeat:
4626 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004627 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004628
4629 /* as we've just preallocated more space than
4630 * user requested orinally, we store allocated
4631 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004632 if (ac->ac_status == AC_STATUS_FOUND &&
4633 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4634 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004635 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004636 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004637 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004638 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004639 /*
4640 * drop the reference that we took
4641 * in ext4_mb_use_best_found
4642 */
4643 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004644 ac->ac_b_ex.fe_group = 0;
4645 ac->ac_b_ex.fe_start = 0;
4646 ac->ac_b_ex.fe_len = 0;
4647 ac->ac_status = AC_STATUS_CONTINUE;
4648 goto repeat;
4649 } else if (*errp) {
4650 ac->ac_b_ex.fe_len = 0;
4651 ar->len = 0;
4652 ext4_mb_show_ac(ac);
4653 } else {
4654 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4655 ar->len = ac->ac_b_ex.fe_len;
4656 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004657 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004658 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004659 if (freed)
4660 goto repeat;
4661 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004662 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004664 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004665 }
4666
Eric Sandeen256bdb42008-02-10 01:13:33 -05004667 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004668
Shen Feng363d4252008-07-11 19:27:31 -04004669out2:
4670 kmem_cache_free(ext4_ac_cachep, ac);
4671out1:
Mingming Cao60e58e02009-01-22 18:13:05 +01004672 if (inquota && ar->len < inquota)
Jan Karaa269eb12009-01-26 17:04:39 +01004673 vfs_dq_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004674out3:
4675 if (!ar->len) {
4676 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4677 /* release all the reserved blocks if non delalloc */
4678 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4679 reserv_blks);
4680 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004681
Theodore Ts'oba80b102009-01-03 20:03:21 -05004682 trace_mark(ext4_allocate_blocks,
4683 "dev %s block %llu flags %u len %u ino %lu "
4684 "logical %llu goal %llu lleft %llu lright %llu "
4685 "pleft %llu pright %llu ",
4686 sb->s_id, (unsigned long long) block,
4687 ar->flags, ar->len, ar->inode ? ar->inode->i_ino : 0,
4688 (unsigned long long) ar->logical,
4689 (unsigned long long) ar->goal,
4690 (unsigned long long) ar->lleft,
4691 (unsigned long long) ar->lright,
4692 (unsigned long long) ar->pleft,
4693 (unsigned long long) ar->pright);
4694
Alex Tomasc9de5602008-01-29 00:19:52 -05004695 return block;
4696}
Alex Tomasc9de5602008-01-29 00:19:52 -05004697
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004698/*
4699 * We can merge two free data extents only if the physical blocks
4700 * are contiguous, AND the extents were freed by the same transaction,
4701 * AND the blocks are associated with the same group.
4702 */
4703static int can_merge(struct ext4_free_data *entry1,
4704 struct ext4_free_data *entry2)
4705{
4706 if ((entry1->t_tid == entry2->t_tid) &&
4707 (entry1->group == entry2->group) &&
4708 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4709 return 1;
4710 return 0;
4711}
4712
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004713static noinline_for_stack int
4714ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004715 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004716{
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004717 ext4_grpblk_t block;
4718 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004719 struct ext4_group_info *db = e4b->bd_info;
4720 struct super_block *sb = e4b->bd_sb;
4721 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004722 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4723 struct rb_node *parent = NULL, *new_node;
4724
Frank Mayhar03901312009-01-07 00:06:22 -05004725 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004726 BUG_ON(e4b->bd_bitmap_page == NULL);
4727 BUG_ON(e4b->bd_buddy_page == NULL);
4728
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004729 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004730 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004731
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004732 if (!*n) {
4733 /* first free block exent. We need to
4734 protect buddy cache from being freed,
4735 * otherwise we'll refresh it from
4736 * on-disk bitmap and lose not-yet-available
4737 * blocks */
4738 page_cache_get(e4b->bd_buddy_page);
4739 page_cache_get(e4b->bd_bitmap_page);
4740 }
4741 while (*n) {
4742 parent = *n;
4743 entry = rb_entry(parent, struct ext4_free_data, node);
4744 if (block < entry->start_blk)
4745 n = &(*n)->rb_left;
4746 else if (block >= (entry->start_blk + entry->count))
4747 n = &(*n)->rb_right;
4748 else {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05004749 ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4750 "Double free of blocks %d (%d %d)",
4751 block, entry->start_blk, entry->count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004752 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004753 }
4754 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004755
4756 rb_link_node(new_node, parent, n);
4757 rb_insert_color(new_node, &db->bb_free_root);
4758
4759 /* Now try to see the extent can be merged to left and right */
4760 node = rb_prev(new_node);
4761 if (node) {
4762 entry = rb_entry(node, struct ext4_free_data, node);
4763 if (can_merge(entry, new_entry)) {
4764 new_entry->start_blk = entry->start_blk;
4765 new_entry->count += entry->count;
4766 rb_erase(node, &(db->bb_free_root));
4767 spin_lock(&sbi->s_md_lock);
4768 list_del(&entry->list);
4769 spin_unlock(&sbi->s_md_lock);
4770 kmem_cache_free(ext4_free_ext_cachep, entry);
4771 }
4772 }
4773
4774 node = rb_next(new_node);
4775 if (node) {
4776 entry = rb_entry(node, struct ext4_free_data, node);
4777 if (can_merge(new_entry, entry)) {
4778 new_entry->count += entry->count;
4779 rb_erase(node, &(db->bb_free_root));
4780 spin_lock(&sbi->s_md_lock);
4781 list_del(&entry->list);
4782 spin_unlock(&sbi->s_md_lock);
4783 kmem_cache_free(ext4_free_ext_cachep, entry);
4784 }
4785 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004786 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004787 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004788 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004789 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004790 return 0;
4791}
4792
4793/*
4794 * Main entry point into mballoc to free blocks
4795 */
4796void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
4797 unsigned long block, unsigned long count,
4798 int metadata, unsigned long *freed)
4799{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004800 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004801 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004802 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004803 struct ext4_group_desc *gdp;
4804 struct ext4_super_block *es;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004805 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004806 ext4_grpblk_t bit;
4807 struct buffer_head *gd_bh;
4808 ext4_group_t block_group;
4809 struct ext4_sb_info *sbi;
4810 struct ext4_buddy e4b;
4811 int err = 0;
4812 int ret;
4813
4814 *freed = 0;
4815
Alex Tomasc9de5602008-01-29 00:19:52 -05004816 sbi = EXT4_SB(sb);
4817 es = EXT4_SB(sb)->s_es;
4818 if (block < le32_to_cpu(es->s_first_data_block) ||
4819 block + count < block ||
4820 block + count > ext4_blocks_count(es)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004821 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004822 "Freeing blocks not in datazone - "
4823 "block = %lu, count = %lu", block, count);
4824 goto error_return;
4825 }
4826
4827 ext4_debug("freeing block %lu\n", block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004828 trace_mark(ext4_free_blocks,
4829 "dev %s block %llu count %lu metadata %d ino %lu",
4830 sb->s_id, (unsigned long long) block, count, metadata,
4831 inode ? inode->i_ino : 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05004832
Eric Sandeen256bdb42008-02-10 01:13:33 -05004833 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4834 if (ac) {
4835 ac->ac_op = EXT4_MB_HISTORY_FREE;
4836 ac->ac_inode = inode;
4837 ac->ac_sb = sb;
4838 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004839
4840do_more:
4841 overflow = 0;
4842 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4843
4844 /*
4845 * Check to see if we are freeing blocks across a group
4846 * boundary.
4847 */
4848 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4849 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4850 count -= overflow;
4851 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004852 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004853 if (!bitmap_bh) {
4854 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004855 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004856 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004857 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004858 if (!gdp) {
4859 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004860 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004861 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004862
4863 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4864 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4865 in_range(block, ext4_inode_table(sb, gdp),
4866 EXT4_SB(sb)->s_itb_per_group) ||
4867 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4868 EXT4_SB(sb)->s_itb_per_group)) {
4869
Harvey Harrison46e665e2008-04-17 10:38:59 -04004870 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004871 "Freeing blocks in system zone - "
4872 "Block = %lu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004873 /* err = 0. ext4_std_error should be a no op */
4874 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004875 }
4876
4877 BUFFER_TRACE(bitmap_bh, "getting write access");
4878 err = ext4_journal_get_write_access(handle, bitmap_bh);
4879 if (err)
4880 goto error_return;
4881
4882 /*
4883 * We are about to modify some metadata. Call the journal APIs
4884 * to unshare ->b_data if a currently-committing transaction is
4885 * using it
4886 */
4887 BUFFER_TRACE(gd_bh, "get_write_access");
4888 err = ext4_journal_get_write_access(handle, gd_bh);
4889 if (err)
4890 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004891#ifdef AGGRESSIVE_CHECK
4892 {
4893 int i;
4894 for (i = 0; i < count; i++)
4895 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4896 }
4897#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004898 if (ac) {
4899 ac->ac_b_ex.fe_group = block_group;
4900 ac->ac_b_ex.fe_start = bit;
4901 ac->ac_b_ex.fe_len = count;
4902 ext4_mb_store_history(ac);
4903 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004904
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004905 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4906 if (err)
4907 goto error_return;
Frank Mayhar03901312009-01-07 00:06:22 -05004908 if (metadata && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004909 struct ext4_free_data *new_entry;
4910 /*
4911 * blocks being freed are metadata. these blocks shouldn't
4912 * be used until this transaction is committed
4913 */
4914 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4915 new_entry->start_blk = bit;
4916 new_entry->group = block_group;
4917 new_entry->count = count;
4918 new_entry->t_tid = handle->h_transaction->t_tid;
4919 ext4_lock_group(sb, block_group);
4920 mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
4921 bit, count);
4922 ext4_mb_free_metadata(handle, &e4b, new_entry);
4923 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004924 } else {
4925 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004926 /* need to update group_info->bb_free and bitmap
4927 * with group lock held. generate_buddy look at
4928 * them with group lock_held
4929 */
4930 mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
4931 bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004932 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004933 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4934 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004935 }
4936
4937 spin_lock(sb_bgl_lock(sbi, block_group));
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004938 ret = ext4_free_blks_count(sb, gdp) + count;
4939 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004940 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4941 spin_unlock(sb_bgl_lock(sbi, block_group));
4942 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4943
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004944 if (sbi->s_log_groups_per_flex) {
4945 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4946 spin_lock(sb_bgl_lock(sbi, flex_group));
4947 sbi->s_flex_groups[flex_group].free_blocks += count;
4948 spin_unlock(sb_bgl_lock(sbi, flex_group));
4949 }
4950
Alex Tomasc9de5602008-01-29 00:19:52 -05004951 ext4_mb_release_desc(&e4b);
4952
4953 *freed += count;
4954
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004955 /* We dirtied the bitmap block */
4956 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4957 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4958
Alex Tomasc9de5602008-01-29 00:19:52 -05004959 /* And the group descriptor block */
4960 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004961 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004962 if (!err)
4963 err = ret;
4964
4965 if (overflow && !err) {
4966 block += count;
4967 count = overflow;
4968 put_bh(bitmap_bh);
4969 goto do_more;
4970 }
4971 sb->s_dirt = 1;
4972error_return:
4973 brelse(bitmap_bh);
4974 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004975 if (ac)
4976 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004977 return;
4978}