blob: d2b1bcaf88ecb994ef8be9c1e91b1ca204fbbd94 [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 }
103 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
104 *
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 */
333
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500334static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
335{
Alex Tomasc9de5602008-01-29 00:19:52 -0500336#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500337 *bit += ((unsigned long) addr & 7UL) << 3;
338 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500339#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500340 *bit += ((unsigned long) addr & 3UL) << 3;
341 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500342#else
343#error "how many bits you are?!"
344#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500345 return addr;
346}
Alex Tomasc9de5602008-01-29 00:19:52 -0500347
348static inline int mb_test_bit(int bit, void *addr)
349{
350 /*
351 * ext4_test_bit on architecture like powerpc
352 * needs unsigned long aligned address
353 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500354 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500355 return ext4_test_bit(bit, addr);
356}
357
358static inline void mb_set_bit(int bit, void *addr)
359{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500360 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500361 ext4_set_bit(bit, addr);
362}
363
364static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
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 ext4_set_bit_atomic(lock, bit, addr);
368}
369
370static inline void mb_clear_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_clear_bit(bit, addr);
374}
375
376static inline void mb_clear_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_clear_bit_atomic(lock, bit, addr);
380}
381
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500382static inline int mb_find_next_zero_bit(void *addr, int max, int start)
383{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400384 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500385 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400386 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500387 start += fix;
388
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400389 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
390 if (ret > max)
391 return max;
392 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500393}
394
395static inline int mb_find_next_bit(void *addr, int max, int start)
396{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400397 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400399 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500400 start += fix;
401
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400402 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
403 if (ret > max)
404 return max;
405 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500406}
407
Alex Tomasc9de5602008-01-29 00:19:52 -0500408static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
409{
410 char *bb;
411
Alex Tomasc9de5602008-01-29 00:19:52 -0500412 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
413 BUG_ON(max == NULL);
414
415 if (order > e4b->bd_blkbits + 1) {
416 *max = 0;
417 return NULL;
418 }
419
420 /* at order 0 we see each particular block */
421 *max = 1 << (e4b->bd_blkbits + 3);
422 if (order == 0)
423 return EXT4_MB_BITMAP(e4b);
424
425 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
426 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
427
428 return bb;
429}
430
431#ifdef DOUBLE_CHECK
432static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
433 int first, int count)
434{
435 int i;
436 struct super_block *sb = e4b->bd_sb;
437
438 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
439 return;
440 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
441 for (i = 0; i < count; i++) {
442 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
443 ext4_fsblk_t blocknr;
444 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
445 blocknr += first + i;
446 blocknr +=
447 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
448
Harvey Harrison46e665e2008-04-17 10:38:59 -0400449 ext4_error(sb, __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500450 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500451 inode ? inode->i_ino : 0, blocknr,
452 first + i, e4b->bd_group);
453 }
454 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
455 }
456}
457
458static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
459{
460 int i;
461
462 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
463 return;
464 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
465 for (i = 0; i < count; i++) {
466 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
467 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
468 }
469}
470
471static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
472{
473 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
474 unsigned char *b1, *b2;
475 int i;
476 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
477 b2 = (unsigned char *) bitmap;
478 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
479 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500480 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400481 "at byte %u(%u): %x in copy != %x "
482 "on disk/prealloc\n",
483 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500484 BUG();
485 }
486 }
487 }
488}
489
490#else
491static inline void mb_free_blocks_double(struct inode *inode,
492 struct ext4_buddy *e4b, int first, int count)
493{
494 return;
495}
496static inline void mb_mark_used_double(struct ext4_buddy *e4b,
497 int first, int count)
498{
499 return;
500}
501static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
502{
503 return;
504}
505#endif
506
507#ifdef AGGRESSIVE_CHECK
508
509#define MB_CHECK_ASSERT(assert) \
510do { \
511 if (!(assert)) { \
512 printk(KERN_EMERG \
513 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
514 function, file, line, # assert); \
515 BUG(); \
516 } \
517} while (0)
518
519static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
520 const char *function, int line)
521{
522 struct super_block *sb = e4b->bd_sb;
523 int order = e4b->bd_blkbits + 1;
524 int max;
525 int max2;
526 int i;
527 int j;
528 int k;
529 int count;
530 struct ext4_group_info *grp;
531 int fragments = 0;
532 int fstart;
533 struct list_head *cur;
534 void *buddy;
535 void *buddy2;
536
Alex Tomasc9de5602008-01-29 00:19:52 -0500537 {
538 static int mb_check_counter;
539 if (mb_check_counter++ % 100 != 0)
540 return 0;
541 }
542
543 while (order > 1) {
544 buddy = mb_find_buddy(e4b, order, &max);
545 MB_CHECK_ASSERT(buddy);
546 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
547 MB_CHECK_ASSERT(buddy2);
548 MB_CHECK_ASSERT(buddy != buddy2);
549 MB_CHECK_ASSERT(max * 2 == max2);
550
551 count = 0;
552 for (i = 0; i < max; i++) {
553
554 if (mb_test_bit(i, buddy)) {
555 /* only single bit in buddy2 may be 1 */
556 if (!mb_test_bit(i << 1, buddy2)) {
557 MB_CHECK_ASSERT(
558 mb_test_bit((i<<1)+1, buddy2));
559 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
560 MB_CHECK_ASSERT(
561 mb_test_bit(i << 1, buddy2));
562 }
563 continue;
564 }
565
566 /* both bits in buddy2 must be 0 */
567 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
568 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
569
570 for (j = 0; j < (1 << order); j++) {
571 k = (i * (1 << order)) + j;
572 MB_CHECK_ASSERT(
573 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
574 }
575 count++;
576 }
577 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
578 order--;
579 }
580
581 fstart = -1;
582 buddy = mb_find_buddy(e4b, 0, &max);
583 for (i = 0; i < max; i++) {
584 if (!mb_test_bit(i, buddy)) {
585 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
586 if (fstart == -1) {
587 fragments++;
588 fstart = i;
589 }
590 continue;
591 }
592 fstart = -1;
593 /* check used bits only */
594 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
595 buddy2 = mb_find_buddy(e4b, j, &max2);
596 k = i >> j;
597 MB_CHECK_ASSERT(k < max2);
598 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
599 }
600 }
601 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
602 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
603
604 grp = ext4_get_group_info(sb, e4b->bd_group);
605 buddy = mb_find_buddy(e4b, 0, &max);
606 list_for_each(cur, &grp->bb_prealloc_list) {
607 ext4_group_t groupnr;
608 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400609 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
610 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500611 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400612 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500613 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
614 }
615 return 0;
616}
617#undef MB_CHECK_ASSERT
618#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400619 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500620#else
621#define mb_check_buddy(e4b)
622#endif
623
624/* FIXME!! need more doc */
625static void ext4_mb_mark_free_simple(struct super_block *sb,
626 void *buddy, unsigned first, int len,
627 struct ext4_group_info *grp)
628{
629 struct ext4_sb_info *sbi = EXT4_SB(sb);
630 unsigned short min;
631 unsigned short max;
632 unsigned short chunk;
633 unsigned short border;
634
Valerie Clementb73fce62008-02-15 13:48:51 -0500635 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500636
637 border = 2 << sb->s_blocksize_bits;
638
639 while (len > 0) {
640 /* find how many blocks can be covered since this position */
641 max = ffs(first | border) - 1;
642
643 /* find how many blocks of power 2 we need to mark */
644 min = fls(len) - 1;
645
646 if (max < min)
647 min = max;
648 chunk = 1 << min;
649
650 /* mark multiblock chunks only */
651 grp->bb_counters[min]++;
652 if (min > 0)
653 mb_clear_bit(first >> min,
654 buddy + sbi->s_mb_offsets[min]);
655
656 len -= chunk;
657 first += chunk;
658 }
659}
660
661static void ext4_mb_generate_buddy(struct super_block *sb,
662 void *buddy, void *bitmap, ext4_group_t group)
663{
664 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
665 unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
666 unsigned short i = 0;
667 unsigned short first;
668 unsigned short len;
669 unsigned free = 0;
670 unsigned fragments = 0;
671 unsigned long long period = get_cycles();
672
673 /* initialize buddy from bitmap which is aggregation
674 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500675 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500676 grp->bb_first_free = i;
677 while (i < max) {
678 fragments++;
679 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500680 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500681 len = i - first;
682 free += len;
683 if (len > 1)
684 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
685 else
686 grp->bb_counters[0]++;
687 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500688 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500689 }
690 grp->bb_fragments = fragments;
691
692 if (free != grp->bb_free) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400693 ext4_error(sb, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500694 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
Alex Tomasc9de5602008-01-29 00:19:52 -0500695 group, free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500696 /*
697 * If we intent to continue, we consider group descritor
698 * corrupt and update bb_free using bitmap value
699 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500700 grp->bb_free = free;
701 }
702
703 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
704
705 period = get_cycles() - period;
706 spin_lock(&EXT4_SB(sb)->s_bal_lock);
707 EXT4_SB(sb)->s_mb_buddies_generated++;
708 EXT4_SB(sb)->s_mb_generation_time += period;
709 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
710}
711
712/* The buddy information is attached the buddy cache inode
713 * for convenience. The information regarding each group
714 * is loaded via ext4_mb_load_buddy. The information involve
715 * block bitmap and buddy information. The information are
716 * stored in the inode as
717 *
718 * { page }
719 * [ group 0 buddy][ group 0 bitmap] [group 1][ group 1]...
720 *
721 *
722 * one block each for bitmap and buddy information.
723 * So for each group we take up 2 blocks. A page can
724 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
725 * So it can have information regarding groups_per_page which
726 * is blocks_per_page/2
727 */
728
729static int ext4_mb_init_cache(struct page *page, char *incore)
730{
731 int blocksize;
732 int blocks_per_page;
733 int groups_per_page;
734 int err = 0;
735 int i;
736 ext4_group_t first_group;
737 int first_block;
738 struct super_block *sb;
739 struct buffer_head *bhs;
740 struct buffer_head **bh;
741 struct inode *inode;
742 char *data;
743 char *bitmap;
744
745 mb_debug("init page %lu\n", page->index);
746
747 inode = page->mapping->host;
748 sb = inode->i_sb;
749 blocksize = 1 << inode->i_blkbits;
750 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
751
752 groups_per_page = blocks_per_page >> 1;
753 if (groups_per_page == 0)
754 groups_per_page = 1;
755
756 /* allocate buffer_heads to read bitmaps */
757 if (groups_per_page > 1) {
758 err = -ENOMEM;
759 i = sizeof(struct buffer_head *) * groups_per_page;
760 bh = kzalloc(i, GFP_NOFS);
761 if (bh == NULL)
762 goto out;
763 } else
764 bh = &bhs;
765
766 first_group = page->index * blocks_per_page / 2;
767
768 /* read all groups the page covers into the cache */
769 for (i = 0; i < groups_per_page; i++) {
770 struct ext4_group_desc *desc;
771
772 if (first_group + i >= EXT4_SB(sb)->s_groups_count)
773 break;
774
775 err = -EIO;
776 desc = ext4_get_group_desc(sb, first_group + i, NULL);
777 if (desc == NULL)
778 goto out;
779
780 err = -ENOMEM;
781 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
782 if (bh[i] == NULL)
783 goto out;
784
Frederic Bohec806e682008-10-10 08:09:18 -0400785 if (buffer_uptodate(bh[i]) &&
786 !(desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)))
Alex Tomasc9de5602008-01-29 00:19:52 -0500787 continue;
788
Frederic Bohec806e682008-10-10 08:09:18 -0400789 lock_buffer(bh[i]);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400790 spin_lock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500791 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
792 ext4_init_block_bitmap(sb, bh[i],
793 first_group + i, desc);
794 set_buffer_uptodate(bh[i]);
795 unlock_buffer(bh[i]);
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400796 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 continue;
798 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -0400799 spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
Alex Tomasc9de5602008-01-29 00:19:52 -0500800 get_bh(bh[i]);
801 bh[i]->b_end_io = end_buffer_read_sync;
802 submit_bh(READ, bh[i]);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500803 mb_debug("read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500804 }
805
806 /* wait for I/O completion */
807 for (i = 0; i < groups_per_page && bh[i]; i++)
808 wait_on_buffer(bh[i]);
809
810 err = -EIO;
811 for (i = 0; i < groups_per_page && bh[i]; i++)
812 if (!buffer_uptodate(bh[i]))
813 goto out;
814
Mingming Cao31b481d2008-07-11 19:27:31 -0400815 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 first_block = page->index * blocks_per_page;
817 for (i = 0; i < blocks_per_page; i++) {
818 int group;
819 struct ext4_group_info *grinfo;
820
821 group = (first_block + i) >> 1;
822 if (group >= EXT4_SB(sb)->s_groups_count)
823 break;
824
825 /*
826 * data carry information regarding this
827 * particular group in the format specified
828 * above
829 *
830 */
831 data = page_address(page) + (i * blocksize);
832 bitmap = bh[group - first_group]->b_data;
833
834 /*
835 * We place the buddy block and bitmap block
836 * close together
837 */
838 if ((first_block + i) & 1) {
839 /* this is block of buddy */
840 BUG_ON(incore == NULL);
841 mb_debug("put buddy for group %u in page %lu/%x\n",
842 group, page->index, i * blocksize);
843 memset(data, 0xff, blocksize);
844 grinfo = ext4_get_group_info(sb, group);
845 grinfo->bb_fragments = 0;
846 memset(grinfo->bb_counters, 0,
847 sizeof(unsigned short)*(sb->s_blocksize_bits+2));
848 /*
849 * incore got set to the group block bitmap below
850 */
851 ext4_mb_generate_buddy(sb, data, incore, group);
852 incore = NULL;
853 } else {
854 /* this is block of bitmap */
855 BUG_ON(incore != NULL);
856 mb_debug("put bitmap for group %u in page %lu/%x\n",
857 group, page->index, i * blocksize);
858
859 /* see comments in ext4_mb_put_pa() */
860 ext4_lock_group(sb, group);
861 memcpy(data, bitmap, blocksize);
862
863 /* mark all preallocated blks used in in-core bitmap */
864 ext4_mb_generate_from_pa(sb, data, group);
865 ext4_unlock_group(sb, group);
866
867 /* set incore so that the buddy information can be
868 * generated using this
869 */
870 incore = data;
871 }
872 }
873 SetPageUptodate(page);
874
875out:
876 if (bh) {
877 for (i = 0; i < groups_per_page && bh[i]; i++)
878 brelse(bh[i]);
879 if (bh != &bhs)
880 kfree(bh);
881 }
882 return err;
883}
884
Eric Sandeen4ddfef72008-04-29 08:11:12 -0400885static noinline_for_stack int
886ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
887 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -0500888{
Alex Tomasc9de5602008-01-29 00:19:52 -0500889 int blocks_per_page;
890 int block;
891 int pnum;
892 int poff;
893 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400894 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500895 struct ext4_group_info *grp;
896 struct ext4_sb_info *sbi = EXT4_SB(sb);
897 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -0500898
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500899 mb_debug("load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500900
901 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500902 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500903
904 e4b->bd_blkbits = sb->s_blocksize_bits;
905 e4b->bd_info = ext4_get_group_info(sb, group);
906 e4b->bd_sb = sb;
907 e4b->bd_group = group;
908 e4b->bd_buddy_page = NULL;
909 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500910 e4b->alloc_semp = &grp->alloc_sem;
911
912 /* Take the read lock on the group alloc
913 * sem. This would make sure a parallel
914 * ext4_mb_init_group happening on other
915 * groups mapped by the page is blocked
916 * till we are done with allocation
917 */
918 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500919
920 /*
921 * the buddy cache inode stores the block bitmap
922 * and buddy information in consecutive blocks.
923 * So for each group we need two blocks.
924 */
925 block = group * 2;
926 pnum = block / blocks_per_page;
927 poff = block % blocks_per_page;
928
929 /* we could use find_or_create_page(), but it locks page
930 * what we'd like to avoid in fast path ... */
931 page = find_get_page(inode->i_mapping, pnum);
932 if (page == NULL || !PageUptodate(page)) {
933 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -0500934 /*
935 * drop the page reference and try
936 * to get the page with lock. If we
937 * are not uptodate that implies
938 * somebody just created the page but
939 * is yet to initialize the same. So
940 * wait for it to initialize.
941 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500942 page_cache_release(page);
943 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
944 if (page) {
945 BUG_ON(page->mapping != inode->i_mapping);
946 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400947 ret = ext4_mb_init_cache(page, NULL);
948 if (ret) {
949 unlock_page(page);
950 goto err;
951 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500952 mb_cmp_bitmaps(e4b, page_address(page) +
953 (poff * sb->s_blocksize));
954 }
955 unlock_page(page);
956 }
957 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400958 if (page == NULL || !PageUptodate(page)) {
959 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500960 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400961 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500962 e4b->bd_bitmap_page = page;
963 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
964 mark_page_accessed(page);
965
966 block++;
967 pnum = block / blocks_per_page;
968 poff = block % blocks_per_page;
969
970 page = find_get_page(inode->i_mapping, pnum);
971 if (page == NULL || !PageUptodate(page)) {
972 if (page)
973 page_cache_release(page);
974 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
975 if (page) {
976 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400977 if (!PageUptodate(page)) {
978 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
979 if (ret) {
980 unlock_page(page);
981 goto err;
982 }
983 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500984 unlock_page(page);
985 }
986 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400987 if (page == NULL || !PageUptodate(page)) {
988 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500989 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -0400990 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500991 e4b->bd_buddy_page = page;
992 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
993 mark_page_accessed(page);
994
995 BUG_ON(e4b->bd_bitmap_page == NULL);
996 BUG_ON(e4b->bd_buddy_page == NULL);
997
998 return 0;
999
1000err:
1001 if (e4b->bd_bitmap_page)
1002 page_cache_release(e4b->bd_bitmap_page);
1003 if (e4b->bd_buddy_page)
1004 page_cache_release(e4b->bd_buddy_page);
1005 e4b->bd_buddy = NULL;
1006 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001007
1008 /* Done with the buddy cache */
1009 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001010 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001011}
1012
1013static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1014{
1015 if (e4b->bd_bitmap_page)
1016 page_cache_release(e4b->bd_bitmap_page);
1017 if (e4b->bd_buddy_page)
1018 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001019 /* Done with the buddy cache */
1020 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001021}
1022
1023
1024static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1025{
1026 int order = 1;
1027 void *bb;
1028
1029 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1030 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1031
1032 bb = EXT4_MB_BUDDY(e4b);
1033 while (order <= e4b->bd_blkbits + 1) {
1034 block = block >> 1;
1035 if (!mb_test_bit(block, bb)) {
1036 /* this block is part of buddy of order 'order' */
1037 return order;
1038 }
1039 bb += 1 << (e4b->bd_blkbits - order);
1040 order++;
1041 }
1042 return 0;
1043}
1044
1045static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1046{
1047 __u32 *addr;
1048
1049 len = cur + len;
1050 while (cur < len) {
1051 if ((cur & 31) == 0 && (len - cur) >= 32) {
1052 /* fast path: clear whole word at once */
1053 addr = bm + (cur >> 3);
1054 *addr = 0;
1055 cur += 32;
1056 continue;
1057 }
1058 mb_clear_bit_atomic(lock, cur, bm);
1059 cur++;
1060 }
1061}
1062
1063static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1064{
1065 __u32 *addr;
1066
1067 len = cur + len;
1068 while (cur < len) {
1069 if ((cur & 31) == 0 && (len - cur) >= 32) {
1070 /* fast path: set whole word at once */
1071 addr = bm + (cur >> 3);
1072 *addr = 0xffffffff;
1073 cur += 32;
1074 continue;
1075 }
1076 mb_set_bit_atomic(lock, cur, bm);
1077 cur++;
1078 }
1079}
1080
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001081static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001082 int first, int count)
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05001083__releases(bitlock)
1084__acquires(bitlock)
Alex Tomasc9de5602008-01-29 00:19:52 -05001085{
1086 int block = 0;
1087 int max = 0;
1088 int order;
1089 void *buddy;
1090 void *buddy2;
1091 struct super_block *sb = e4b->bd_sb;
1092
1093 BUG_ON(first + count > (sb->s_blocksize << 3));
1094 BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
1095 mb_check_buddy(e4b);
1096 mb_free_blocks_double(inode, e4b, first, count);
1097
1098 e4b->bd_info->bb_free += count;
1099 if (first < e4b->bd_info->bb_first_free)
1100 e4b->bd_info->bb_first_free = first;
1101
1102 /* let's maintain fragments counter */
1103 if (first != 0)
1104 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1105 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1106 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1107 if (block && max)
1108 e4b->bd_info->bb_fragments--;
1109 else if (!block && !max)
1110 e4b->bd_info->bb_fragments++;
1111
1112 /* let's maintain buddy itself */
1113 while (count-- > 0) {
1114 block = first++;
1115 order = 0;
1116
1117 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1118 ext4_fsblk_t blocknr;
1119 blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1120 blocknr += block;
1121 blocknr +=
1122 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001123 ext4_unlock_group(sb, e4b->bd_group);
Harvey Harrison46e665e2008-04-17 10:38:59 -04001124 ext4_error(sb, __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001125 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001126 inode ? inode->i_ino : 0, blocknr, block,
1127 e4b->bd_group);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001128 ext4_lock_group(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001129 }
1130 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1131 e4b->bd_info->bb_counters[order]++;
1132
1133 /* start of the buddy */
1134 buddy = mb_find_buddy(e4b, order, &max);
1135
1136 do {
1137 block &= ~1UL;
1138 if (mb_test_bit(block, buddy) ||
1139 mb_test_bit(block + 1, buddy))
1140 break;
1141
1142 /* both the buddies are free, try to coalesce them */
1143 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1144
1145 if (!buddy2)
1146 break;
1147
1148 if (order > 0) {
1149 /* for special purposes, we don't set
1150 * free bits in bitmap */
1151 mb_set_bit(block, buddy);
1152 mb_set_bit(block + 1, buddy);
1153 }
1154 e4b->bd_info->bb_counters[order]--;
1155 e4b->bd_info->bb_counters[order]--;
1156
1157 block = block >> 1;
1158 order++;
1159 e4b->bd_info->bb_counters[order]++;
1160
1161 mb_clear_bit(block, buddy2);
1162 buddy = buddy2;
1163 } while (1);
1164 }
1165 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001166}
1167
1168static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1169 int needed, struct ext4_free_extent *ex)
1170{
1171 int next = block;
1172 int max;
1173 int ord;
1174 void *buddy;
1175
1176 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1177 BUG_ON(ex == NULL);
1178
1179 buddy = mb_find_buddy(e4b, order, &max);
1180 BUG_ON(buddy == NULL);
1181 BUG_ON(block >= max);
1182 if (mb_test_bit(block, buddy)) {
1183 ex->fe_len = 0;
1184 ex->fe_start = 0;
1185 ex->fe_group = 0;
1186 return 0;
1187 }
1188
1189 /* FIXME dorp order completely ? */
1190 if (likely(order == 0)) {
1191 /* find actual order */
1192 order = mb_find_order_for_block(e4b, block);
1193 block = block >> order;
1194 }
1195
1196 ex->fe_len = 1 << order;
1197 ex->fe_start = block << order;
1198 ex->fe_group = e4b->bd_group;
1199
1200 /* calc difference from given start */
1201 next = next - ex->fe_start;
1202 ex->fe_len -= next;
1203 ex->fe_start += next;
1204
1205 while (needed > ex->fe_len &&
1206 (buddy = mb_find_buddy(e4b, order, &max))) {
1207
1208 if (block + 1 >= max)
1209 break;
1210
1211 next = (block + 1) * (1 << order);
1212 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1213 break;
1214
1215 ord = mb_find_order_for_block(e4b, next);
1216
1217 order = ord;
1218 block = next >> order;
1219 ex->fe_len += 1 << order;
1220 }
1221
1222 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1223 return ex->fe_len;
1224}
1225
1226static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1227{
1228 int ord;
1229 int mlen = 0;
1230 int max = 0;
1231 int cur;
1232 int start = ex->fe_start;
1233 int len = ex->fe_len;
1234 unsigned ret = 0;
1235 int len0 = len;
1236 void *buddy;
1237
1238 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1239 BUG_ON(e4b->bd_group != ex->fe_group);
1240 BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1241 mb_check_buddy(e4b);
1242 mb_mark_used_double(e4b, start, len);
1243
1244 e4b->bd_info->bb_free -= len;
1245 if (e4b->bd_info->bb_first_free == start)
1246 e4b->bd_info->bb_first_free += len;
1247
1248 /* let's maintain fragments counter */
1249 if (start != 0)
1250 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1251 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1252 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1253 if (mlen && max)
1254 e4b->bd_info->bb_fragments++;
1255 else if (!mlen && !max)
1256 e4b->bd_info->bb_fragments--;
1257
1258 /* let's maintain buddy itself */
1259 while (len) {
1260 ord = mb_find_order_for_block(e4b, start);
1261
1262 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1263 /* the whole chunk may be allocated at once! */
1264 mlen = 1 << ord;
1265 buddy = mb_find_buddy(e4b, ord, &max);
1266 BUG_ON((start >> ord) >= max);
1267 mb_set_bit(start >> ord, buddy);
1268 e4b->bd_info->bb_counters[ord]--;
1269 start += mlen;
1270 len -= mlen;
1271 BUG_ON(len < 0);
1272 continue;
1273 }
1274
1275 /* store for history */
1276 if (ret == 0)
1277 ret = len | (ord << 16);
1278
1279 /* we have to split large buddy */
1280 BUG_ON(ord <= 0);
1281 buddy = mb_find_buddy(e4b, ord, &max);
1282 mb_set_bit(start >> ord, buddy);
1283 e4b->bd_info->bb_counters[ord]--;
1284
1285 ord--;
1286 cur = (start >> ord) & ~1U;
1287 buddy = mb_find_buddy(e4b, ord, &max);
1288 mb_clear_bit(cur, buddy);
1289 mb_clear_bit(cur + 1, buddy);
1290 e4b->bd_info->bb_counters[ord]++;
1291 e4b->bd_info->bb_counters[ord]++;
1292 }
1293
1294 mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
1295 EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1296 mb_check_buddy(e4b);
1297
1298 return ret;
1299}
1300
1301/*
1302 * Must be called under group lock!
1303 */
1304static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1305 struct ext4_buddy *e4b)
1306{
1307 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1308 int ret;
1309
1310 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1311 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1312
1313 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1314 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1315 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1316
1317 /* preallocation can change ac_b_ex, thus we store actually
1318 * allocated blocks for history */
1319 ac->ac_f_ex = ac->ac_b_ex;
1320
1321 ac->ac_status = AC_STATUS_FOUND;
1322 ac->ac_tail = ret & 0xffff;
1323 ac->ac_buddy = ret >> 16;
1324
1325 /* XXXXXXX: SUCH A HORRIBLE **CK */
1326 /*FIXME!! Why ? */
1327 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1328 get_page(ac->ac_bitmap_page);
1329 ac->ac_buddy_page = e4b->bd_buddy_page;
1330 get_page(ac->ac_buddy_page);
1331
1332 /* store last allocated for subsequent stream allocation */
1333 if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
1334 spin_lock(&sbi->s_md_lock);
1335 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1336 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1337 spin_unlock(&sbi->s_md_lock);
1338 }
1339}
1340
1341/*
1342 * regular allocator, for general purposes allocation
1343 */
1344
1345static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1346 struct ext4_buddy *e4b,
1347 int finish_group)
1348{
1349 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1350 struct ext4_free_extent *bex = &ac->ac_b_ex;
1351 struct ext4_free_extent *gex = &ac->ac_g_ex;
1352 struct ext4_free_extent ex;
1353 int max;
1354
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001355 if (ac->ac_status == AC_STATUS_FOUND)
1356 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001357 /*
1358 * We don't want to scan for a whole year
1359 */
1360 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1361 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1362 ac->ac_status = AC_STATUS_BREAK;
1363 return;
1364 }
1365
1366 /*
1367 * Haven't found good chunk so far, let's continue
1368 */
1369 if (bex->fe_len < gex->fe_len)
1370 return;
1371
1372 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1373 && bex->fe_group == e4b->bd_group) {
1374 /* recheck chunk's availability - we don't know
1375 * when it was found (within this lock-unlock
1376 * period or not) */
1377 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1378 if (max >= gex->fe_len) {
1379 ext4_mb_use_best_found(ac, e4b);
1380 return;
1381 }
1382 }
1383}
1384
1385/*
1386 * The routine checks whether found extent is good enough. If it is,
1387 * then the extent gets marked used and flag is set to the context
1388 * to stop scanning. Otherwise, the extent is compared with the
1389 * previous found extent and if new one is better, then it's stored
1390 * in the context. Later, the best found extent will be used, if
1391 * mballoc can't find good enough extent.
1392 *
1393 * FIXME: real allocation policy is to be designed yet!
1394 */
1395static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1396 struct ext4_free_extent *ex,
1397 struct ext4_buddy *e4b)
1398{
1399 struct ext4_free_extent *bex = &ac->ac_b_ex;
1400 struct ext4_free_extent *gex = &ac->ac_g_ex;
1401
1402 BUG_ON(ex->fe_len <= 0);
1403 BUG_ON(ex->fe_len >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1404 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1405 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1406
1407 ac->ac_found++;
1408
1409 /*
1410 * The special case - take what you catch first
1411 */
1412 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1413 *bex = *ex;
1414 ext4_mb_use_best_found(ac, e4b);
1415 return;
1416 }
1417
1418 /*
1419 * Let's check whether the chuck is good enough
1420 */
1421 if (ex->fe_len == gex->fe_len) {
1422 *bex = *ex;
1423 ext4_mb_use_best_found(ac, e4b);
1424 return;
1425 }
1426
1427 /*
1428 * If this is first found extent, just store it in the context
1429 */
1430 if (bex->fe_len == 0) {
1431 *bex = *ex;
1432 return;
1433 }
1434
1435 /*
1436 * If new found extent is better, store it in the context
1437 */
1438 if (bex->fe_len < gex->fe_len) {
1439 /* if the request isn't satisfied, any found extent
1440 * larger than previous best one is better */
1441 if (ex->fe_len > bex->fe_len)
1442 *bex = *ex;
1443 } else if (ex->fe_len > gex->fe_len) {
1444 /* if the request is satisfied, then we try to find
1445 * an extent that still satisfy the request, but is
1446 * smaller than previous one */
1447 if (ex->fe_len < bex->fe_len)
1448 *bex = *ex;
1449 }
1450
1451 ext4_mb_check_limits(ac, e4b, 0);
1452}
1453
1454static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1455 struct ext4_buddy *e4b)
1456{
1457 struct ext4_free_extent ex = ac->ac_b_ex;
1458 ext4_group_t group = ex.fe_group;
1459 int max;
1460 int err;
1461
1462 BUG_ON(ex.fe_len <= 0);
1463 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1464 if (err)
1465 return err;
1466
1467 ext4_lock_group(ac->ac_sb, group);
1468 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1469
1470 if (max > 0) {
1471 ac->ac_b_ex = ex;
1472 ext4_mb_use_best_found(ac, e4b);
1473 }
1474
1475 ext4_unlock_group(ac->ac_sb, group);
1476 ext4_mb_release_desc(e4b);
1477
1478 return 0;
1479}
1480
1481static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1482 struct ext4_buddy *e4b)
1483{
1484 ext4_group_t group = ac->ac_g_ex.fe_group;
1485 int max;
1486 int err;
1487 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1488 struct ext4_super_block *es = sbi->s_es;
1489 struct ext4_free_extent ex;
1490
1491 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1492 return 0;
1493
1494 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1495 if (err)
1496 return err;
1497
1498 ext4_lock_group(ac->ac_sb, group);
1499 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1500 ac->ac_g_ex.fe_len, &ex);
1501
1502 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1503 ext4_fsblk_t start;
1504
1505 start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1506 ex.fe_start + le32_to_cpu(es->s_first_data_block);
1507 /* use do_div to get remainder (would be 64-bit modulo) */
1508 if (do_div(start, sbi->s_stripe) == 0) {
1509 ac->ac_found++;
1510 ac->ac_b_ex = ex;
1511 ext4_mb_use_best_found(ac, e4b);
1512 }
1513 } else if (max >= ac->ac_g_ex.fe_len) {
1514 BUG_ON(ex.fe_len <= 0);
1515 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1516 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1517 ac->ac_found++;
1518 ac->ac_b_ex = ex;
1519 ext4_mb_use_best_found(ac, e4b);
1520 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1521 /* Sometimes, caller may want to merge even small
1522 * number of blocks to an existing extent */
1523 BUG_ON(ex.fe_len <= 0);
1524 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1525 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1526 ac->ac_found++;
1527 ac->ac_b_ex = ex;
1528 ext4_mb_use_best_found(ac, e4b);
1529 }
1530 ext4_unlock_group(ac->ac_sb, group);
1531 ext4_mb_release_desc(e4b);
1532
1533 return 0;
1534}
1535
1536/*
1537 * The routine scans buddy structures (not bitmap!) from given order
1538 * to max order and tries to find big enough chunk to satisfy the req
1539 */
1540static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1541 struct ext4_buddy *e4b)
1542{
1543 struct super_block *sb = ac->ac_sb;
1544 struct ext4_group_info *grp = e4b->bd_info;
1545 void *buddy;
1546 int i;
1547 int k;
1548 int max;
1549
1550 BUG_ON(ac->ac_2order <= 0);
1551 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1552 if (grp->bb_counters[i] == 0)
1553 continue;
1554
1555 buddy = mb_find_buddy(e4b, i, &max);
1556 BUG_ON(buddy == NULL);
1557
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001558 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001559 BUG_ON(k >= max);
1560
1561 ac->ac_found++;
1562
1563 ac->ac_b_ex.fe_len = 1 << i;
1564 ac->ac_b_ex.fe_start = k << i;
1565 ac->ac_b_ex.fe_group = e4b->bd_group;
1566
1567 ext4_mb_use_best_found(ac, e4b);
1568
1569 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1570
1571 if (EXT4_SB(sb)->s_mb_stats)
1572 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1573
1574 break;
1575 }
1576}
1577
1578/*
1579 * The routine scans the group and measures all found extents.
1580 * In order to optimize scanning, caller must pass number of
1581 * free blocks in the group, so the routine can know upper limit.
1582 */
1583static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1584 struct ext4_buddy *e4b)
1585{
1586 struct super_block *sb = ac->ac_sb;
1587 void *bitmap = EXT4_MB_BITMAP(e4b);
1588 struct ext4_free_extent ex;
1589 int i;
1590 int free;
1591
1592 free = e4b->bd_info->bb_free;
1593 BUG_ON(free <= 0);
1594
1595 i = e4b->bd_info->bb_first_free;
1596
1597 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001598 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001599 EXT4_BLOCKS_PER_GROUP(sb), i);
1600 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001601 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001602 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001603 * free blocks even though group info says we
1604 * we have free blocks
1605 */
Harvey Harrison46e665e2008-04-17 10:38:59 -04001606 ext4_error(sb, __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001607 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001608 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001609 break;
1610 }
1611
1612 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1613 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001614 if (free < ex.fe_len) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04001615 ext4_error(sb, __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001616 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001617 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001618 /*
1619 * The number of free blocks differs. This mostly
1620 * indicate that the bitmap is corrupt. So exit
1621 * without claiming the space.
1622 */
1623 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001624 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001625
1626 ext4_mb_measure_extent(ac, &ex, e4b);
1627
1628 i += ex.fe_len;
1629 free -= ex.fe_len;
1630 }
1631
1632 ext4_mb_check_limits(ac, e4b, 1);
1633}
1634
1635/*
1636 * This is a special case for storages like raid5
1637 * we try to find stripe-aligned chunks for stripe-size requests
1638 * XXX should do so at least for multiples of stripe size as well
1639 */
1640static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1641 struct ext4_buddy *e4b)
1642{
1643 struct super_block *sb = ac->ac_sb;
1644 struct ext4_sb_info *sbi = EXT4_SB(sb);
1645 void *bitmap = EXT4_MB_BITMAP(e4b);
1646 struct ext4_free_extent ex;
1647 ext4_fsblk_t first_group_block;
1648 ext4_fsblk_t a;
1649 ext4_grpblk_t i;
1650 int max;
1651
1652 BUG_ON(sbi->s_stripe == 0);
1653
1654 /* find first stripe-aligned block in group */
1655 first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1656 + le32_to_cpu(sbi->s_es->s_first_data_block);
1657 a = first_group_block + sbi->s_stripe - 1;
1658 do_div(a, sbi->s_stripe);
1659 i = (a * sbi->s_stripe) - first_group_block;
1660
1661 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1662 if (!mb_test_bit(i, bitmap)) {
1663 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1664 if (max >= sbi->s_stripe) {
1665 ac->ac_found++;
1666 ac->ac_b_ex = ex;
1667 ext4_mb_use_best_found(ac, e4b);
1668 break;
1669 }
1670 }
1671 i += sbi->s_stripe;
1672 }
1673}
1674
1675static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1676 ext4_group_t group, int cr)
1677{
1678 unsigned free, fragments;
1679 unsigned i, bits;
1680 struct ext4_group_desc *desc;
1681 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1682
1683 BUG_ON(cr < 0 || cr >= 4);
1684 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1685
1686 free = grp->bb_free;
1687 fragments = grp->bb_fragments;
1688 if (free == 0)
1689 return 0;
1690 if (fragments == 0)
1691 return 0;
1692
1693 switch (cr) {
1694 case 0:
1695 BUG_ON(ac->ac_2order == 0);
1696 /* If this group is uninitialized, skip it initially */
1697 desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
1698 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
1699 return 0;
1700
1701 bits = ac->ac_sb->s_blocksize_bits + 1;
1702 for (i = ac->ac_2order; i <= bits; i++)
1703 if (grp->bb_counters[i] > 0)
1704 return 1;
1705 break;
1706 case 1:
1707 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1708 return 1;
1709 break;
1710 case 2:
1711 if (free >= ac->ac_g_ex.fe_len)
1712 return 1;
1713 break;
1714 case 3:
1715 return 1;
1716 default:
1717 BUG();
1718 }
1719
1720 return 0;
1721}
1722
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001723/*
1724 * lock the group_info alloc_sem of all the groups
1725 * belonging to the same buddy cache page. This
1726 * make sure other parallel operation on the buddy
1727 * cache doesn't happen whild holding the buddy cache
1728 * lock
1729 */
1730int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1731{
1732 int i;
1733 int block, pnum;
1734 int blocks_per_page;
1735 int groups_per_page;
1736 ext4_group_t first_group;
1737 struct ext4_group_info *grp;
1738
1739 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1740 /*
1741 * the buddy cache inode stores the block bitmap
1742 * and buddy information in consecutive blocks.
1743 * So for each group we need two blocks.
1744 */
1745 block = group * 2;
1746 pnum = block / blocks_per_page;
1747 first_group = pnum * blocks_per_page / 2;
1748
1749 groups_per_page = blocks_per_page >> 1;
1750 if (groups_per_page == 0)
1751 groups_per_page = 1;
1752 /* read all groups the page covers into the cache */
1753 for (i = 0; i < groups_per_page; i++) {
1754
1755 if ((first_group + i) >= EXT4_SB(sb)->s_groups_count)
1756 break;
1757 grp = ext4_get_group_info(sb, first_group + i);
1758 /* take all groups write allocation
1759 * semaphore. This make sure there is
1760 * no block allocation going on in any
1761 * of that groups
1762 */
1763 down_write(&grp->alloc_sem);
1764 }
1765 return i;
1766}
1767
1768void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1769 ext4_group_t group, int locked_group)
1770{
1771 int i;
1772 int block, pnum;
1773 int blocks_per_page;
1774 ext4_group_t first_group;
1775 struct ext4_group_info *grp;
1776
1777 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1778 /*
1779 * the buddy cache inode stores the block bitmap
1780 * and buddy information in consecutive blocks.
1781 * So for each group we need two blocks.
1782 */
1783 block = group * 2;
1784 pnum = block / blocks_per_page;
1785 first_group = pnum * blocks_per_page / 2;
1786 /* release locks on all the groups */
1787 for (i = 0; i < locked_group; i++) {
1788
1789 grp = ext4_get_group_info(sb, first_group + i);
1790 /* take all groups write allocation
1791 * semaphore. This make sure there is
1792 * no block allocation going on in any
1793 * of that groups
1794 */
1795 up_write(&grp->alloc_sem);
1796 }
1797
1798}
1799
1800static int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1801{
1802
1803 int ret;
1804 void *bitmap;
1805 int blocks_per_page;
1806 int block, pnum, poff;
1807 int num_grp_locked = 0;
1808 struct ext4_group_info *this_grp;
1809 struct ext4_sb_info *sbi = EXT4_SB(sb);
1810 struct inode *inode = sbi->s_buddy_cache;
1811 struct page *page = NULL, *bitmap_page = NULL;
1812
1813 mb_debug("init group %lu\n", group);
1814 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1815 this_grp = ext4_get_group_info(sb, group);
1816 /*
1817 * This ensures we don't add group
1818 * to this buddy cache via resize
1819 */
1820 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
1821 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1822 /*
1823 * somebody initialized the group
1824 * return without doing anything
1825 */
1826 ret = 0;
1827 goto err;
1828 }
1829 /*
1830 * the buddy cache inode stores the block bitmap
1831 * and buddy information in consecutive blocks.
1832 * So for each group we need two blocks.
1833 */
1834 block = group * 2;
1835 pnum = block / blocks_per_page;
1836 poff = block % blocks_per_page;
1837 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1838 if (page) {
1839 BUG_ON(page->mapping != inode->i_mapping);
1840 ret = ext4_mb_init_cache(page, NULL);
1841 if (ret) {
1842 unlock_page(page);
1843 goto err;
1844 }
1845 unlock_page(page);
1846 }
1847 if (page == NULL || !PageUptodate(page)) {
1848 ret = -EIO;
1849 goto err;
1850 }
1851 mark_page_accessed(page);
1852 bitmap_page = page;
1853 bitmap = page_address(page) + (poff * sb->s_blocksize);
1854
1855 /* init buddy cache */
1856 block++;
1857 pnum = block / blocks_per_page;
1858 poff = block % blocks_per_page;
1859 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1860 if (page == bitmap_page) {
1861 /*
1862 * If both the bitmap and buddy are in
1863 * the same page we don't need to force
1864 * init the buddy
1865 */
1866 unlock_page(page);
1867 } else if (page) {
1868 BUG_ON(page->mapping != inode->i_mapping);
1869 ret = ext4_mb_init_cache(page, bitmap);
1870 if (ret) {
1871 unlock_page(page);
1872 goto err;
1873 }
1874 unlock_page(page);
1875 }
1876 if (page == NULL || !PageUptodate(page)) {
1877 ret = -EIO;
1878 goto err;
1879 }
1880 mark_page_accessed(page);
1881err:
1882 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1883 if (bitmap_page)
1884 page_cache_release(bitmap_page);
1885 if (page)
1886 page_cache_release(page);
1887 return ret;
1888}
1889
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001890static noinline_for_stack int
1891ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001892{
1893 ext4_group_t group;
1894 ext4_group_t i;
1895 int cr;
1896 int err = 0;
1897 int bsbits;
1898 struct ext4_sb_info *sbi;
1899 struct super_block *sb;
1900 struct ext4_buddy e4b;
1901 loff_t size, isize;
1902
1903 sb = ac->ac_sb;
1904 sbi = EXT4_SB(sb);
1905 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1906
1907 /* first, try the goal */
1908 err = ext4_mb_find_by_goal(ac, &e4b);
1909 if (err || ac->ac_status == AC_STATUS_FOUND)
1910 goto out;
1911
1912 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1913 goto out;
1914
1915 /*
1916 * ac->ac2_order is set only if the fe_len is a power of 2
1917 * if ac2_order is set we also set criteria to 0 so that we
1918 * try exact allocation using buddy.
1919 */
1920 i = fls(ac->ac_g_ex.fe_len);
1921 ac->ac_2order = 0;
1922 /*
1923 * We search using buddy data only if the order of the request
1924 * is greater than equal to the sbi_s_mb_order2_reqs
1925 * You can tune it via /proc/fs/ext4/<partition>/order2_req
1926 */
1927 if (i >= sbi->s_mb_order2_reqs) {
1928 /*
1929 * This should tell if fe_len is exactly power of 2
1930 */
1931 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1932 ac->ac_2order = i - 1;
1933 }
1934
1935 bsbits = ac->ac_sb->s_blocksize_bits;
1936 /* if stream allocation is enabled, use global goal */
1937 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
1938 isize = i_size_read(ac->ac_inode) >> bsbits;
1939 if (size < isize)
1940 size = isize;
1941
1942 if (size < sbi->s_mb_stream_request &&
1943 (ac->ac_flags & EXT4_MB_HINT_DATA)) {
1944 /* TBD: may be hot point */
1945 spin_lock(&sbi->s_md_lock);
1946 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
1947 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
1948 spin_unlock(&sbi->s_md_lock);
1949 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001950 /* Let's just scan groups to find more-less suitable blocks */
1951 cr = ac->ac_2order ? 0 : 1;
1952 /*
1953 * cr == 0 try to get exact allocation,
1954 * cr == 3 try to get anything
1955 */
1956repeat:
1957 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1958 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04001959 /*
1960 * searching for the right group start
1961 * from the goal value specified
1962 */
1963 group = ac->ac_g_ex.fe_group;
1964
Alex Tomasc9de5602008-01-29 00:19:52 -05001965 for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
1966 struct ext4_group_info *grp;
1967 struct ext4_group_desc *desc;
1968
1969 if (group == EXT4_SB(sb)->s_groups_count)
1970 group = 0;
1971
1972 /* quick check to skip empty groups */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001973 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001974 if (grp->bb_free == 0)
1975 continue;
1976
1977 /*
1978 * if the group is already init we check whether it is
1979 * a good group and if not we don't load the buddy
1980 */
1981 if (EXT4_MB_GRP_NEED_INIT(grp)) {
1982 /*
1983 * we need full data about the group
1984 * to make a good selection
1985 */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001986 err = ext4_mb_init_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001987 if (err)
1988 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05001989 }
1990
1991 /*
1992 * If the particular group doesn't satisfy our
1993 * criteria we continue with the next group
1994 */
1995 if (!ext4_mb_good_group(ac, group, cr))
1996 continue;
1997
1998 err = ext4_mb_load_buddy(sb, group, &e4b);
1999 if (err)
2000 goto out;
2001
2002 ext4_lock_group(sb, group);
2003 if (!ext4_mb_good_group(ac, group, cr)) {
2004 /* someone did allocation from this group */
2005 ext4_unlock_group(sb, group);
2006 ext4_mb_release_desc(&e4b);
2007 continue;
2008 }
2009
2010 ac->ac_groups_scanned++;
2011 desc = ext4_get_group_desc(sb, group, NULL);
2012 if (cr == 0 || (desc->bg_flags &
2013 cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
2014 ac->ac_2order != 0))
2015 ext4_mb_simple_scan_group(ac, &e4b);
2016 else if (cr == 1 &&
2017 ac->ac_g_ex.fe_len == sbi->s_stripe)
2018 ext4_mb_scan_aligned(ac, &e4b);
2019 else
2020 ext4_mb_complex_scan_group(ac, &e4b);
2021
2022 ext4_unlock_group(sb, group);
2023 ext4_mb_release_desc(&e4b);
2024
2025 if (ac->ac_status != AC_STATUS_CONTINUE)
2026 break;
2027 }
2028 }
2029
2030 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2031 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2032 /*
2033 * We've been searching too long. Let's try to allocate
2034 * the best chunk we've found so far
2035 */
2036
2037 ext4_mb_try_best_found(ac, &e4b);
2038 if (ac->ac_status != AC_STATUS_FOUND) {
2039 /*
2040 * Someone more lucky has already allocated it.
2041 * The only thing we can do is just take first
2042 * found block(s)
2043 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2044 */
2045 ac->ac_b_ex.fe_group = 0;
2046 ac->ac_b_ex.fe_start = 0;
2047 ac->ac_b_ex.fe_len = 0;
2048 ac->ac_status = AC_STATUS_CONTINUE;
2049 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2050 cr = 3;
2051 atomic_inc(&sbi->s_mb_lost_chunks);
2052 goto repeat;
2053 }
2054 }
2055out:
2056 return err;
2057}
2058
2059#ifdef EXT4_MB_HISTORY
2060struct ext4_mb_proc_session {
2061 struct ext4_mb_history *history;
2062 struct super_block *sb;
2063 int start;
2064 int max;
2065};
2066
2067static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2068 struct ext4_mb_history *hs,
2069 int first)
2070{
2071 if (hs == s->history + s->max)
2072 hs = s->history;
2073 if (!first && hs == s->history + s->start)
2074 return NULL;
2075 while (hs->orig.fe_len == 0) {
2076 hs++;
2077 if (hs == s->history + s->max)
2078 hs = s->history;
2079 if (hs == s->history + s->start)
2080 return NULL;
2081 }
2082 return hs;
2083}
2084
2085static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2086{
2087 struct ext4_mb_proc_session *s = seq->private;
2088 struct ext4_mb_history *hs;
2089 int l = *pos;
2090
2091 if (l == 0)
2092 return SEQ_START_TOKEN;
2093 hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2094 if (!hs)
2095 return NULL;
2096 while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2097 return hs;
2098}
2099
2100static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2101 loff_t *pos)
2102{
2103 struct ext4_mb_proc_session *s = seq->private;
2104 struct ext4_mb_history *hs = v;
2105
2106 ++*pos;
2107 if (v == SEQ_START_TOKEN)
2108 return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2109 else
2110 return ext4_mb_history_skip_empty(s, ++hs, 0);
2111}
2112
2113static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2114{
2115 char buf[25], buf2[25], buf3[25], *fmt;
2116 struct ext4_mb_history *hs = v;
2117
2118 if (v == SEQ_START_TOKEN) {
2119 seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2120 "%-5s %-2s %-5s %-5s %-5s %-6s\n",
2121 "pid", "inode", "original", "goal", "result", "found",
2122 "grps", "cr", "flags", "merge", "tail", "broken");
2123 return 0;
2124 }
2125
2126 if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2127 fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2128 "%-5u %-5s %-5u %-6u\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002129 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002130 hs->result.fe_start, hs->result.fe_len,
2131 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002132 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002133 hs->orig.fe_start, hs->orig.fe_len,
2134 hs->orig.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002135 sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002136 hs->goal.fe_start, hs->goal.fe_len,
2137 hs->goal.fe_logical);
2138 seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2139 hs->found, hs->groups, hs->cr, hs->flags,
2140 hs->merged ? "M" : "", hs->tail,
2141 hs->buddy ? 1 << hs->buddy : 0);
2142 } else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2143 fmt = "%-5u %-8u %-23s %-23s %-23s\n";
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002144 sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002145 hs->result.fe_start, hs->result.fe_len,
2146 hs->result.fe_logical);
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002147 sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002148 hs->orig.fe_start, hs->orig.fe_len,
2149 hs->orig.fe_logical);
2150 seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2151 } else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002152 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002153 hs->result.fe_start, hs->result.fe_len);
2154 seq_printf(seq, "%-5u %-8u %-23s discard\n",
2155 hs->pid, hs->ino, buf2);
2156 } else if (hs->op == EXT4_MB_HISTORY_FREE) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002157 sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
Alex Tomasc9de5602008-01-29 00:19:52 -05002158 hs->result.fe_start, hs->result.fe_len);
2159 seq_printf(seq, "%-5u %-8u %-23s free\n",
2160 hs->pid, hs->ino, buf2);
2161 }
2162 return 0;
2163}
2164
2165static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2166{
2167}
2168
2169static struct seq_operations ext4_mb_seq_history_ops = {
2170 .start = ext4_mb_seq_history_start,
2171 .next = ext4_mb_seq_history_next,
2172 .stop = ext4_mb_seq_history_stop,
2173 .show = ext4_mb_seq_history_show,
2174};
2175
2176static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2177{
2178 struct super_block *sb = PDE(inode)->data;
2179 struct ext4_sb_info *sbi = EXT4_SB(sb);
2180 struct ext4_mb_proc_session *s;
2181 int rc;
2182 int size;
2183
Shen Feng74767c52008-07-11 19:27:31 -04002184 if (unlikely(sbi->s_mb_history == NULL))
2185 return -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -05002186 s = kmalloc(sizeof(*s), GFP_KERNEL);
2187 if (s == NULL)
2188 return -ENOMEM;
2189 s->sb = sb;
2190 size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2191 s->history = kmalloc(size, GFP_KERNEL);
2192 if (s->history == NULL) {
2193 kfree(s);
2194 return -ENOMEM;
2195 }
2196
2197 spin_lock(&sbi->s_mb_history_lock);
2198 memcpy(s->history, sbi->s_mb_history, size);
2199 s->max = sbi->s_mb_history_max;
2200 s->start = sbi->s_mb_history_cur % s->max;
2201 spin_unlock(&sbi->s_mb_history_lock);
2202
2203 rc = seq_open(file, &ext4_mb_seq_history_ops);
2204 if (rc == 0) {
2205 struct seq_file *m = (struct seq_file *)file->private_data;
2206 m->private = s;
2207 } else {
2208 kfree(s->history);
2209 kfree(s);
2210 }
2211 return rc;
2212
2213}
2214
2215static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2216{
2217 struct seq_file *seq = (struct seq_file *)file->private_data;
2218 struct ext4_mb_proc_session *s = seq->private;
2219 kfree(s->history);
2220 kfree(s);
2221 return seq_release(inode, file);
2222}
2223
2224static ssize_t ext4_mb_seq_history_write(struct file *file,
2225 const char __user *buffer,
2226 size_t count, loff_t *ppos)
2227{
2228 struct seq_file *seq = (struct seq_file *)file->private_data;
2229 struct ext4_mb_proc_session *s = seq->private;
2230 struct super_block *sb = s->sb;
2231 char str[32];
2232 int value;
2233
2234 if (count >= sizeof(str)) {
2235 printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2236 "mb_history", (int)sizeof(str));
2237 return -EOVERFLOW;
2238 }
2239
2240 if (copy_from_user(str, buffer, count))
2241 return -EFAULT;
2242
2243 value = simple_strtol(str, NULL, 0);
2244 if (value < 0)
2245 return -ERANGE;
2246 EXT4_SB(sb)->s_mb_history_filter = value;
2247
2248 return count;
2249}
2250
2251static struct file_operations ext4_mb_seq_history_fops = {
2252 .owner = THIS_MODULE,
2253 .open = ext4_mb_seq_history_open,
2254 .read = seq_read,
2255 .write = ext4_mb_seq_history_write,
2256 .llseek = seq_lseek,
2257 .release = ext4_mb_seq_history_release,
2258};
2259
2260static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2261{
2262 struct super_block *sb = seq->private;
2263 struct ext4_sb_info *sbi = EXT4_SB(sb);
2264 ext4_group_t group;
2265
2266 if (*pos < 0 || *pos >= sbi->s_groups_count)
2267 return NULL;
2268
2269 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002270 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002271}
2272
2273static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2274{
2275 struct super_block *sb = seq->private;
2276 struct ext4_sb_info *sbi = EXT4_SB(sb);
2277 ext4_group_t group;
2278
2279 ++*pos;
2280 if (*pos < 0 || *pos >= sbi->s_groups_count)
2281 return NULL;
2282 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002283 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002284}
2285
2286static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2287{
2288 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002289 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002290 int i;
2291 int err;
2292 struct ext4_buddy e4b;
2293 struct sg {
2294 struct ext4_group_info info;
2295 unsigned short counters[16];
2296 } sg;
2297
2298 group--;
2299 if (group == 0)
2300 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2301 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2302 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2303 "group", "free", "frags", "first",
2304 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2305 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2306
2307 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2308 sizeof(struct ext4_group_info);
2309 err = ext4_mb_load_buddy(sb, group, &e4b);
2310 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002311 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002312 return 0;
2313 }
2314 ext4_lock_group(sb, group);
2315 memcpy(&sg, ext4_get_group_info(sb, group), i);
2316 ext4_unlock_group(sb, group);
2317 ext4_mb_release_desc(&e4b);
2318
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002319 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002320 sg.info.bb_fragments, sg.info.bb_first_free);
2321 for (i = 0; i <= 13; i++)
2322 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2323 sg.info.bb_counters[i] : 0);
2324 seq_printf(seq, " ]\n");
2325
2326 return 0;
2327}
2328
2329static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2330{
2331}
2332
2333static struct seq_operations ext4_mb_seq_groups_ops = {
2334 .start = ext4_mb_seq_groups_start,
2335 .next = ext4_mb_seq_groups_next,
2336 .stop = ext4_mb_seq_groups_stop,
2337 .show = ext4_mb_seq_groups_show,
2338};
2339
2340static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2341{
2342 struct super_block *sb = PDE(inode)->data;
2343 int rc;
2344
2345 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2346 if (rc == 0) {
2347 struct seq_file *m = (struct seq_file *)file->private_data;
2348 m->private = sb;
2349 }
2350 return rc;
2351
2352}
2353
2354static struct file_operations ext4_mb_seq_groups_fops = {
2355 .owner = THIS_MODULE,
2356 .open = ext4_mb_seq_groups_open,
2357 .read = seq_read,
2358 .llseek = seq_lseek,
2359 .release = seq_release,
2360};
2361
2362static void ext4_mb_history_release(struct super_block *sb)
2363{
2364 struct ext4_sb_info *sbi = EXT4_SB(sb);
2365
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002366 if (sbi->s_proc != NULL) {
2367 remove_proc_entry("mb_groups", sbi->s_proc);
2368 remove_proc_entry("mb_history", sbi->s_proc);
2369 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002370 kfree(sbi->s_mb_history);
2371}
2372
2373static void ext4_mb_history_init(struct super_block *sb)
2374{
2375 struct ext4_sb_info *sbi = EXT4_SB(sb);
2376 int i;
2377
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002378 if (sbi->s_proc != NULL) {
2379 proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002380 &ext4_mb_seq_history_fops, sb);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002381 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
Denis V. Lunev46fe74f2008-04-29 01:02:08 -07002382 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002383 }
2384
2385 sbi->s_mb_history_max = 1000;
2386 sbi->s_mb_history_cur = 0;
2387 spin_lock_init(&sbi->s_mb_history_lock);
2388 i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
Shen Feng74767c52008-07-11 19:27:31 -04002389 sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002390 /* if we can't allocate history, then we simple won't use it */
2391}
2392
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002393static noinline_for_stack void
2394ext4_mb_store_history(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002395{
2396 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2397 struct ext4_mb_history h;
2398
2399 if (unlikely(sbi->s_mb_history == NULL))
2400 return;
2401
2402 if (!(ac->ac_op & sbi->s_mb_history_filter))
2403 return;
2404
2405 h.op = ac->ac_op;
2406 h.pid = current->pid;
2407 h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2408 h.orig = ac->ac_o_ex;
2409 h.result = ac->ac_b_ex;
2410 h.flags = ac->ac_flags;
2411 h.found = ac->ac_found;
2412 h.groups = ac->ac_groups_scanned;
2413 h.cr = ac->ac_criteria;
2414 h.tail = ac->ac_tail;
2415 h.buddy = ac->ac_buddy;
2416 h.merged = 0;
2417 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2418 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2419 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2420 h.merged = 1;
2421 h.goal = ac->ac_g_ex;
2422 h.result = ac->ac_f_ex;
2423 }
2424
2425 spin_lock(&sbi->s_mb_history_lock);
2426 memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2427 if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2428 sbi->s_mb_history_cur = 0;
2429 spin_unlock(&sbi->s_mb_history_lock);
2430}
2431
2432#else
2433#define ext4_mb_history_release(sb)
2434#define ext4_mb_history_init(sb)
2435#endif
2436
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002437
2438/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002439int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002440 struct ext4_group_desc *desc)
2441{
2442 int i, len;
2443 int metalen = 0;
2444 struct ext4_sb_info *sbi = EXT4_SB(sb);
2445 struct ext4_group_info **meta_group_info;
2446
2447 /*
2448 * First check if this group is the first of a reserved block.
2449 * If it's true, we have to allocate a new table of pointers
2450 * to ext4_group_info structures
2451 */
2452 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2453 metalen = sizeof(*meta_group_info) <<
2454 EXT4_DESC_PER_BLOCK_BITS(sb);
2455 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2456 if (meta_group_info == NULL) {
2457 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2458 "buddy group\n");
2459 goto exit_meta_group_info;
2460 }
2461 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2462 meta_group_info;
2463 }
2464
2465 /*
2466 * calculate needed size. if change bb_counters size,
2467 * don't forget about ext4_mb_generate_buddy()
2468 */
2469 len = offsetof(typeof(**meta_group_info),
2470 bb_counters[sb->s_blocksize_bits + 2]);
2471
2472 meta_group_info =
2473 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2474 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2475
2476 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2477 if (meta_group_info[i] == NULL) {
2478 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2479 goto exit_group_info;
2480 }
2481 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2482 &(meta_group_info[i]->bb_state));
2483
2484 /*
2485 * initialize bb_free to be able to skip
2486 * empty groups without initialization
2487 */
2488 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2489 meta_group_info[i]->bb_free =
2490 ext4_free_blocks_after_init(sb, group, desc);
2491 } else {
2492 meta_group_info[i]->bb_free =
2493 le16_to_cpu(desc->bg_free_blocks_count);
2494 }
2495
2496 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002497 init_rwsem(&meta_group_info[i]->alloc_sem);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002498 meta_group_info[i]->bb_free_root.rb_node = NULL;;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002499
2500#ifdef DOUBLE_CHECK
2501 {
2502 struct buffer_head *bh;
2503 meta_group_info[i]->bb_bitmap =
2504 kmalloc(sb->s_blocksize, GFP_KERNEL);
2505 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2506 bh = ext4_read_block_bitmap(sb, group);
2507 BUG_ON(bh == NULL);
2508 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2509 sb->s_blocksize);
2510 put_bh(bh);
2511 }
2512#endif
2513
2514 return 0;
2515
2516exit_group_info:
2517 /* If a meta_group_info table has been allocated, release it now */
2518 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2519 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2520exit_meta_group_info:
2521 return -ENOMEM;
2522} /* ext4_mb_add_groupinfo */
2523
2524/*
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002525 * Update an existing group.
2526 * This function is used for online resize
2527 */
2528void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
2529{
2530 grp->bb_free += add;
2531}
2532
Alex Tomasc9de5602008-01-29 00:19:52 -05002533static int ext4_mb_init_backend(struct super_block *sb)
2534{
2535 ext4_group_t i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002536 int metalen;
Alex Tomasc9de5602008-01-29 00:19:52 -05002537 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002538 struct ext4_super_block *es = sbi->s_es;
2539 int num_meta_group_infos;
2540 int num_meta_group_infos_max;
2541 int array_size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002542 struct ext4_group_info **meta_group_info;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002543 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002544
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002545 /* This is the number of blocks used by GDT */
2546 num_meta_group_infos = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) -
2547 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2548
2549 /*
2550 * This is the total number of blocks used by GDT including
2551 * the number of reserved blocks for GDT.
2552 * The s_group_info array is allocated with this value
2553 * to allow a clean online resize without a complex
2554 * manipulation of pointer.
2555 * The drawback is the unused memory when no resize
2556 * occurs but it's very low in terms of pages
2557 * (see comments below)
2558 * Need to handle this properly when META_BG resizing is allowed
2559 */
2560 num_meta_group_infos_max = num_meta_group_infos +
2561 le16_to_cpu(es->s_reserved_gdt_blocks);
2562
2563 /*
2564 * array_size is the size of s_group_info array. We round it
2565 * to the next power of two because this approximation is done
2566 * internally by kmalloc so we can have some more memory
2567 * for free here (e.g. may be used for META_BG resize).
2568 */
2569 array_size = 1;
2570 while (array_size < sizeof(*sbi->s_group_info) *
2571 num_meta_group_infos_max)
2572 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002573 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2574 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2575 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002576 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002577 if (sbi->s_group_info == NULL) {
2578 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2579 return -ENOMEM;
2580 }
2581 sbi->s_buddy_cache = new_inode(sb);
2582 if (sbi->s_buddy_cache == NULL) {
2583 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2584 goto err_freesgi;
2585 }
2586 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2587
2588 metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
2589 for (i = 0; i < num_meta_group_infos; i++) {
2590 if ((i + 1) == num_meta_group_infos)
2591 metalen = sizeof(*meta_group_info) *
2592 (sbi->s_groups_count -
2593 (i << EXT4_DESC_PER_BLOCK_BITS(sb)));
2594 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2595 if (meta_group_info == NULL) {
2596 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2597 "buddy group\n");
2598 goto err_freemeta;
2599 }
2600 sbi->s_group_info[i] = meta_group_info;
2601 }
2602
Alex Tomasc9de5602008-01-29 00:19:52 -05002603 for (i = 0; i < sbi->s_groups_count; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002604 desc = ext4_get_group_desc(sb, i, NULL);
2605 if (desc == NULL) {
2606 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002607 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002608 goto err_freebuddy;
2609 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002610 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2611 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002612 }
2613
2614 return 0;
2615
2616err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002617 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002618 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002619 i = num_meta_group_infos;
2620err_freemeta:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002621 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002622 kfree(sbi->s_group_info[i]);
2623 iput(sbi->s_buddy_cache);
2624err_freesgi:
2625 kfree(sbi->s_group_info);
2626 return -ENOMEM;
2627}
2628
2629int ext4_mb_init(struct super_block *sb, int needs_recovery)
2630{
2631 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002632 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002633 unsigned offset;
2634 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002635 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002636
Alex Tomasc9de5602008-01-29 00:19:52 -05002637 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2638
2639 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2640 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002641 return -ENOMEM;
2642 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002643
2644 i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
Alex Tomasc9de5602008-01-29 00:19:52 -05002645 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2646 if (sbi->s_mb_maxs == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002647 kfree(sbi->s_mb_maxs);
2648 return -ENOMEM;
2649 }
2650
2651 /* order 0 is regular bitmap */
2652 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2653 sbi->s_mb_offsets[0] = 0;
2654
2655 i = 1;
2656 offset = 0;
2657 max = sb->s_blocksize << 2;
2658 do {
2659 sbi->s_mb_offsets[i] = offset;
2660 sbi->s_mb_maxs[i] = max;
2661 offset += 1 << (sb->s_blocksize_bits - i);
2662 max = max >> 1;
2663 i++;
2664 } while (i <= sb->s_blocksize_bits + 1);
2665
2666 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002667 ret = ext4_mb_init_backend(sb);
2668 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002669 kfree(sbi->s_mb_offsets);
2670 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002671 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002672 }
2673
2674 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002675 spin_lock_init(&sbi->s_bal_lock);
2676
2677 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2678 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2679 sbi->s_mb_stats = MB_DEFAULT_STATS;
2680 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2681 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2682 sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2683 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2684
Eric Sandeen730c2132008-09-13 15:23:29 -04002685 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002686 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 kfree(sbi->s_mb_offsets);
2688 kfree(sbi->s_mb_maxs);
2689 return -ENOMEM;
2690 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002691 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002692 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002693 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002694 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002695 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2696 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002697 spin_lock_init(&lg->lg_prealloc_lock);
2698 }
2699
2700 ext4_mb_init_per_dev_proc(sb);
2701 ext4_mb_history_init(sb);
2702
Frank Mayhar03901312009-01-07 00:06:22 -05002703 if (sbi->s_journal)
2704 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002705
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002706 printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
Alex Tomasc9de5602008-01-29 00:19:52 -05002707 return 0;
2708}
2709
2710/* need to called with ext4 group lock (ext4_lock_group) */
2711static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2712{
2713 struct ext4_prealloc_space *pa;
2714 struct list_head *cur, *tmp;
2715 int count = 0;
2716
2717 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2718 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2719 list_del(&pa->pa_group_list);
2720 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002721 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002722 }
2723 if (count)
2724 mb_debug("mballoc: %u PAs left\n", count);
2725
2726}
2727
2728int ext4_mb_release(struct super_block *sb)
2729{
2730 ext4_group_t i;
2731 int num_meta_group_infos;
2732 struct ext4_group_info *grinfo;
2733 struct ext4_sb_info *sbi = EXT4_SB(sb);
2734
Alex Tomasc9de5602008-01-29 00:19:52 -05002735 if (sbi->s_group_info) {
2736 for (i = 0; i < sbi->s_groups_count; i++) {
2737 grinfo = ext4_get_group_info(sb, i);
2738#ifdef DOUBLE_CHECK
2739 kfree(grinfo->bb_bitmap);
2740#endif
2741 ext4_lock_group(sb, i);
2742 ext4_mb_cleanup_pa(grinfo);
2743 ext4_unlock_group(sb, i);
2744 kfree(grinfo);
2745 }
2746 num_meta_group_infos = (sbi->s_groups_count +
2747 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2748 EXT4_DESC_PER_BLOCK_BITS(sb);
2749 for (i = 0; i < num_meta_group_infos; i++)
2750 kfree(sbi->s_group_info[i]);
2751 kfree(sbi->s_group_info);
2752 }
2753 kfree(sbi->s_mb_offsets);
2754 kfree(sbi->s_mb_maxs);
2755 if (sbi->s_buddy_cache)
2756 iput(sbi->s_buddy_cache);
2757 if (sbi->s_mb_stats) {
2758 printk(KERN_INFO
2759 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2760 atomic_read(&sbi->s_bal_allocated),
2761 atomic_read(&sbi->s_bal_reqs),
2762 atomic_read(&sbi->s_bal_success));
2763 printk(KERN_INFO
2764 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2765 "%u 2^N hits, %u breaks, %u lost\n",
2766 atomic_read(&sbi->s_bal_ex_scanned),
2767 atomic_read(&sbi->s_bal_goals),
2768 atomic_read(&sbi->s_bal_2orders),
2769 atomic_read(&sbi->s_bal_breaks),
2770 atomic_read(&sbi->s_mb_lost_chunks));
2771 printk(KERN_INFO
2772 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2773 sbi->s_mb_buddies_generated++,
2774 sbi->s_mb_generation_time);
2775 printk(KERN_INFO
2776 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2777 atomic_read(&sbi->s_mb_preallocated),
2778 atomic_read(&sbi->s_mb_discarded));
2779 }
2780
Eric Sandeen730c2132008-09-13 15:23:29 -04002781 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002782 ext4_mb_history_release(sb);
2783 ext4_mb_destroy_per_dev_proc(sb);
2784
2785 return 0;
2786}
2787
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002788/*
2789 * This function is called by the jbd2 layer once the commit has finished,
2790 * so we know we can free the blocks that were released with that commit.
2791 */
2792static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002793{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002794 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002795 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002796 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002797 int err, count = 0, count2 = 0;
2798 struct ext4_free_data *entry;
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002799 ext4_fsblk_t discard_block;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002800 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002801
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002802 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2803 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002804
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002805 mb_debug("gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002806 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002807
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002808 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002809 /* we expect to find existing buddy because it's pinned */
2810 BUG_ON(err != 0);
2811
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002812 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002813 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002814 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002815 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002816 ext4_lock_group(sb, entry->group);
2817 /* Take it out of per group rb tree */
2818 rb_erase(&entry->node, &(db->bb_free_root));
2819 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2820
2821 if (!db->bb_free_root.rb_node) {
2822 /* No more items in the per group rb tree
2823 * balance refcounts from ext4_mb_free_metadata()
2824 */
2825 page_cache_release(e4b.bd_buddy_page);
2826 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002827 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002828 ext4_unlock_group(sb, entry->group);
Theodore Ts'o8a0aba72008-10-16 10:06:27 -04002829 discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
2830 + entry->start_blk
2831 + le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
2832 trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u", sb->s_id,
2833 (unsigned long long) discard_block, entry->count);
2834 sb_issue_discard(sb, discard_block, entry->count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002835
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002836 kmem_cache_free(ext4_free_ext_cachep, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002837 ext4_mb_release_desc(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002838 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002839
2840 mb_debug("freed %u blocks in %u structures\n", count, count2);
2841}
2842
Alex Tomasc9de5602008-01-29 00:19:52 -05002843#define EXT4_MB_STATS_NAME "stats"
2844#define EXT4_MB_MAX_TO_SCAN_NAME "max_to_scan"
2845#define EXT4_MB_MIN_TO_SCAN_NAME "min_to_scan"
2846#define EXT4_MB_ORDER2_REQ "order2_req"
2847#define EXT4_MB_STREAM_REQ "stream_req"
2848#define EXT4_MB_GROUP_PREALLOC "group_prealloc"
2849
Alex Tomasc9de5602008-01-29 00:19:52 -05002850static int ext4_mb_init_per_dev_proc(struct super_block *sb)
2851{
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002852#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002853 mode_t mode = S_IFREG | S_IRUGO | S_IWUSR;
2854 struct ext4_sb_info *sbi = EXT4_SB(sb);
2855 struct proc_dir_entry *proc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002856
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002857 if (sbi->s_proc == NULL)
Shen Fengcfbe7e42008-07-13 21:03:31 -04002858 return -EINVAL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002859
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04002860 EXT4_PROC_HANDLER(EXT4_MB_STATS_NAME, mb_stats);
2861 EXT4_PROC_HANDLER(EXT4_MB_MAX_TO_SCAN_NAME, mb_max_to_scan);
2862 EXT4_PROC_HANDLER(EXT4_MB_MIN_TO_SCAN_NAME, mb_min_to_scan);
2863 EXT4_PROC_HANDLER(EXT4_MB_ORDER2_REQ, mb_order2_reqs);
2864 EXT4_PROC_HANDLER(EXT4_MB_STREAM_REQ, mb_stream_request);
2865 EXT4_PROC_HANDLER(EXT4_MB_GROUP_PREALLOC, mb_group_prealloc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002866 return 0;
2867
2868err_out:
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002869 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2870 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2871 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2872 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2873 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2874 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002875 return -ENOMEM;
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002876#else
2877 return 0;
2878#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002879}
2880
2881static int ext4_mb_destroy_per_dev_proc(struct super_block *sb)
2882{
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002883#ifdef CONFIG_PROC_FS
Alex Tomasc9de5602008-01-29 00:19:52 -05002884 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002885
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002886 if (sbi->s_proc == NULL)
Alex Tomasc9de5602008-01-29 00:19:52 -05002887 return -EINVAL;
2888
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002889 remove_proc_entry(EXT4_MB_GROUP_PREALLOC, sbi->s_proc);
2890 remove_proc_entry(EXT4_MB_STREAM_REQ, sbi->s_proc);
2891 remove_proc_entry(EXT4_MB_ORDER2_REQ, sbi->s_proc);
2892 remove_proc_entry(EXT4_MB_MIN_TO_SCAN_NAME, sbi->s_proc);
2893 remove_proc_entry(EXT4_MB_MAX_TO_SCAN_NAME, sbi->s_proc);
2894 remove_proc_entry(EXT4_MB_STATS_NAME, sbi->s_proc);
Manish Katiyar0b09923e2008-10-17 14:58:45 -04002895#endif
Alex Tomasc9de5602008-01-29 00:19:52 -05002896 return 0;
2897}
2898
2899int __init init_ext4_mballoc(void)
2900{
2901 ext4_pspace_cachep =
2902 kmem_cache_create("ext4_prealloc_space",
2903 sizeof(struct ext4_prealloc_space),
2904 0, SLAB_RECLAIM_ACCOUNT, NULL);
2905 if (ext4_pspace_cachep == NULL)
2906 return -ENOMEM;
2907
Eric Sandeen256bdb42008-02-10 01:13:33 -05002908 ext4_ac_cachep =
2909 kmem_cache_create("ext4_alloc_context",
2910 sizeof(struct ext4_allocation_context),
2911 0, SLAB_RECLAIM_ACCOUNT, NULL);
2912 if (ext4_ac_cachep == NULL) {
2913 kmem_cache_destroy(ext4_pspace_cachep);
2914 return -ENOMEM;
2915 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002916
2917 ext4_free_ext_cachep =
2918 kmem_cache_create("ext4_free_block_extents",
2919 sizeof(struct ext4_free_data),
2920 0, SLAB_RECLAIM_ACCOUNT, NULL);
2921 if (ext4_free_ext_cachep == NULL) {
2922 kmem_cache_destroy(ext4_pspace_cachep);
2923 kmem_cache_destroy(ext4_ac_cachep);
2924 return -ENOMEM;
2925 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002926 return 0;
2927}
2928
2929void exit_ext4_mballoc(void)
2930{
2931 /* XXX: synchronize_rcu(); */
2932 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002933 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002934 kmem_cache_destroy(ext4_free_ext_cachep);
Alex Tomasc9de5602008-01-29 00:19:52 -05002935}
2936
2937
2938/*
2939 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2940 * Returns 0 if success or error code
2941 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002942static noinline_for_stack int
2943ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002944 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002945{
2946 struct buffer_head *bitmap_bh = NULL;
2947 struct ext4_super_block *es;
2948 struct ext4_group_desc *gdp;
2949 struct buffer_head *gdp_bh;
2950 struct ext4_sb_info *sbi;
2951 struct super_block *sb;
2952 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002953 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002954
2955 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2956 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2957
2958 sb = ac->ac_sb;
2959 sbi = EXT4_SB(sb);
2960 es = sbi->s_es;
2961
Alex Tomasc9de5602008-01-29 00:19:52 -05002962
2963 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002964 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002965 if (!bitmap_bh)
2966 goto out_err;
2967
2968 err = ext4_journal_get_write_access(handle, bitmap_bh);
2969 if (err)
2970 goto out_err;
2971
2972 err = -EIO;
2973 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2974 if (!gdp)
2975 goto out_err;
2976
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002977 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002978 gdp->bg_free_blocks_count);
2979
Alex Tomasc9de5602008-01-29 00:19:52 -05002980 err = ext4_journal_get_write_access(handle, gdp_bh);
2981 if (err)
2982 goto out_err;
2983
2984 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
2985 + ac->ac_b_ex.fe_start
2986 + le32_to_cpu(es->s_first_data_block);
2987
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002988 len = ac->ac_b_ex.fe_len;
2989 if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
2990 in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
2991 in_range(block, ext4_inode_table(sb, gdp),
2992 EXT4_SB(sb)->s_itb_per_group) ||
2993 in_range(block + len - 1, ext4_inode_table(sb, gdp),
2994 EXT4_SB(sb)->s_itb_per_group)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04002995 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05002996 "Allocating block in system zone - block = %llu",
2997 block);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002998 /* File system mounted not to panic on error
2999 * Fix the bitmap and repeat the block allocation
3000 * We leak some of the blocks here.
3001 */
3002 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
3003 bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3004 ac->ac_b_ex.fe_len);
Frank Mayhar03901312009-01-07 00:06:22 -05003005 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04003006 if (!err)
3007 err = -EAGAIN;
3008 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003009 }
3010#ifdef AGGRESSIVE_CHECK
3011 {
3012 int i;
3013 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3014 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3015 bitmap_bh->b_data));
3016 }
3017 }
3018#endif
3019 mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group), bitmap_bh->b_data,
3020 ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3021
3022 spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3023 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3024 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
3025 gdp->bg_free_blocks_count =
3026 cpu_to_le16(ext4_free_blocks_after_init(sb,
3027 ac->ac_b_ex.fe_group,
3028 gdp));
3029 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04003030 le16_add_cpu(&gdp->bg_free_blocks_count, -ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003031 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
3032 spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003033 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04003034 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003035 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04003036 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003037 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
3038 /* release all the reserved blocks if non delalloc */
3039 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
3040 else
3041 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
3042 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003043
Jose R. Santos772cb7c2008-07-11 19:27:31 -04003044 if (sbi->s_log_groups_per_flex) {
3045 ext4_group_t flex_group = ext4_flex_group(sbi,
3046 ac->ac_b_ex.fe_group);
3047 spin_lock(sb_bgl_lock(sbi, flex_group));
3048 sbi->s_flex_groups[flex_group].free_blocks -= ac->ac_b_ex.fe_len;
3049 spin_unlock(sb_bgl_lock(sbi, flex_group));
3050 }
3051
Frank Mayhar03901312009-01-07 00:06:22 -05003052 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003053 if (err)
3054 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05003055 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003056
3057out_err:
3058 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05003059 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05003060 return err;
3061}
3062
3063/*
3064 * here we normalize request for locality group
3065 * Group request are normalized to s_strip size if we set the same via mount
3066 * option. If not we set it to s_mb_group_prealloc which can be configured via
3067 * /proc/fs/ext4/<partition>/group_prealloc
3068 *
3069 * XXX: should we try to preallocate more than the group has now?
3070 */
3071static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3072{
3073 struct super_block *sb = ac->ac_sb;
3074 struct ext4_locality_group *lg = ac->ac_lg;
3075
3076 BUG_ON(lg == NULL);
3077 if (EXT4_SB(sb)->s_stripe)
3078 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3079 else
3080 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003081 mb_debug("#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003082 current->pid, ac->ac_g_ex.fe_len);
3083}
3084
3085/*
3086 * Normalization means making request better in terms of
3087 * size and alignment
3088 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003089static noinline_for_stack void
3090ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003091 struct ext4_allocation_request *ar)
3092{
3093 int bsbits, max;
3094 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003095 loff_t size, orig_size, start_off;
3096 ext4_lblk_t start, orig_start;
3097 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003098 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003099
3100 /* do normalize only data requests, metadata requests
3101 do not need preallocation */
3102 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3103 return;
3104
3105 /* sometime caller may want exact blocks */
3106 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3107 return;
3108
3109 /* caller may indicate that preallocation isn't
3110 * required (it's a tail, for example) */
3111 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3112 return;
3113
3114 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3115 ext4_mb_normalize_group_request(ac);
3116 return ;
3117 }
3118
3119 bsbits = ac->ac_sb->s_blocksize_bits;
3120
3121 /* first, let's learn actual file size
3122 * given current request is allocated */
3123 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3124 size = size << bsbits;
3125 if (size < i_size_read(ac->ac_inode))
3126 size = i_size_read(ac->ac_inode);
3127
Valerie Clement19304792008-05-13 19:31:14 -04003128 /* max size of free chunks */
3129 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003130
Valerie Clement19304792008-05-13 19:31:14 -04003131#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3132 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003133
3134 /* first, try to predict filesize */
3135 /* XXX: should this table be tunable? */
3136 start_off = 0;
3137 if (size <= 16 * 1024) {
3138 size = 16 * 1024;
3139 } else if (size <= 32 * 1024) {
3140 size = 32 * 1024;
3141 } else if (size <= 64 * 1024) {
3142 size = 64 * 1024;
3143 } else if (size <= 128 * 1024) {
3144 size = 128 * 1024;
3145 } else if (size <= 256 * 1024) {
3146 size = 256 * 1024;
3147 } else if (size <= 512 * 1024) {
3148 size = 512 * 1024;
3149 } else if (size <= 1024 * 1024) {
3150 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003151 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003152 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003153 (21 - bsbits)) << 21;
3154 size = 2 * 1024 * 1024;
3155 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003156 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3157 (22 - bsbits)) << 22;
3158 size = 4 * 1024 * 1024;
3159 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003160 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003161 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3162 (23 - bsbits)) << 23;
3163 size = 8 * 1024 * 1024;
3164 } else {
3165 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3166 size = ac->ac_o_ex.fe_len << bsbits;
3167 }
3168 orig_size = size = size >> bsbits;
3169 orig_start = start = start_off >> bsbits;
3170
3171 /* don't cover already allocated blocks in selected range */
3172 if (ar->pleft && start <= ar->lleft) {
3173 size -= ar->lleft + 1 - start;
3174 start = ar->lleft + 1;
3175 }
3176 if (ar->pright && start + size - 1 >= ar->lright)
3177 size -= start + size - ar->lright;
3178
3179 end = start + size;
3180
3181 /* check we don't cross already preallocated blocks */
3182 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003183 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003184 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003185
Alex Tomasc9de5602008-01-29 00:19:52 -05003186 if (pa->pa_deleted)
3187 continue;
3188 spin_lock(&pa->pa_lock);
3189 if (pa->pa_deleted) {
3190 spin_unlock(&pa->pa_lock);
3191 continue;
3192 }
3193
3194 pa_end = pa->pa_lstart + pa->pa_len;
3195
3196 /* PA must not overlap original request */
3197 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3198 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3199
3200 /* skip PA normalized request doesn't overlap with */
3201 if (pa->pa_lstart >= end) {
3202 spin_unlock(&pa->pa_lock);
3203 continue;
3204 }
3205 if (pa_end <= start) {
3206 spin_unlock(&pa->pa_lock);
3207 continue;
3208 }
3209 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3210
3211 if (pa_end <= ac->ac_o_ex.fe_logical) {
3212 BUG_ON(pa_end < start);
3213 start = pa_end;
3214 }
3215
3216 if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3217 BUG_ON(pa->pa_lstart > end);
3218 end = pa->pa_lstart;
3219 }
3220 spin_unlock(&pa->pa_lock);
3221 }
3222 rcu_read_unlock();
3223 size = end - start;
3224
3225 /* XXX: extra loop to check we really don't overlap preallocations */
3226 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003227 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003228 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003229 spin_lock(&pa->pa_lock);
3230 if (pa->pa_deleted == 0) {
3231 pa_end = pa->pa_lstart + pa->pa_len;
3232 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3233 }
3234 spin_unlock(&pa->pa_lock);
3235 }
3236 rcu_read_unlock();
3237
3238 if (start + size <= ac->ac_o_ex.fe_logical &&
3239 start > ac->ac_o_ex.fe_logical) {
3240 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3241 (unsigned long) start, (unsigned long) size,
3242 (unsigned long) ac->ac_o_ex.fe_logical);
3243 }
3244 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3245 start > ac->ac_o_ex.fe_logical);
3246 BUG_ON(size <= 0 || size >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
3247
3248 /* now prepare goal request */
3249
3250 /* XXX: is it better to align blocks WRT to logical
3251 * placement or satisfy big request as is */
3252 ac->ac_g_ex.fe_logical = start;
3253 ac->ac_g_ex.fe_len = size;
3254
3255 /* define goal start in order to merge */
3256 if (ar->pright && (ar->lright == (start + size))) {
3257 /* merge to the right */
3258 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3259 &ac->ac_f_ex.fe_group,
3260 &ac->ac_f_ex.fe_start);
3261 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3262 }
3263 if (ar->pleft && (ar->lleft + 1 == start)) {
3264 /* merge to the left */
3265 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3266 &ac->ac_f_ex.fe_group,
3267 &ac->ac_f_ex.fe_start);
3268 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3269 }
3270
3271 mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3272 (unsigned) orig_size, (unsigned) start);
3273}
3274
3275static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3276{
3277 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3278
3279 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3280 atomic_inc(&sbi->s_bal_reqs);
3281 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3282 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3283 atomic_inc(&sbi->s_bal_success);
3284 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3285 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3286 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3287 atomic_inc(&sbi->s_bal_goals);
3288 if (ac->ac_found > sbi->s_mb_max_to_scan)
3289 atomic_inc(&sbi->s_bal_breaks);
3290 }
3291
3292 ext4_mb_store_history(ac);
3293}
3294
3295/*
3296 * use blocks preallocated to inode
3297 */
3298static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3299 struct ext4_prealloc_space *pa)
3300{
3301 ext4_fsblk_t start;
3302 ext4_fsblk_t end;
3303 int len;
3304
3305 /* found preallocated blocks, use them */
3306 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3307 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3308 len = end - start;
3309 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3310 &ac->ac_b_ex.fe_start);
3311 ac->ac_b_ex.fe_len = len;
3312 ac->ac_status = AC_STATUS_FOUND;
3313 ac->ac_pa = pa;
3314
3315 BUG_ON(start < pa->pa_pstart);
3316 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3317 BUG_ON(pa->pa_free < len);
3318 pa->pa_free -= len;
3319
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003320 mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003321}
3322
3323/*
3324 * use blocks preallocated to locality group
3325 */
3326static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3327 struct ext4_prealloc_space *pa)
3328{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003329 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003330
Alex Tomasc9de5602008-01-29 00:19:52 -05003331 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3332 &ac->ac_b_ex.fe_group,
3333 &ac->ac_b_ex.fe_start);
3334 ac->ac_b_ex.fe_len = len;
3335 ac->ac_status = AC_STATUS_FOUND;
3336 ac->ac_pa = pa;
3337
3338 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003339 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003340 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003341 * in on-disk bitmap -- see ext4_mb_release_context()
3342 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003343 */
3344 mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3345}
3346
3347/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003348 * Return the prealloc space that have minimal distance
3349 * from the goal block. @cpa is the prealloc
3350 * space that is having currently known minimal distance
3351 * from the goal block.
3352 */
3353static struct ext4_prealloc_space *
3354ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3355 struct ext4_prealloc_space *pa,
3356 struct ext4_prealloc_space *cpa)
3357{
3358 ext4_fsblk_t cur_distance, new_distance;
3359
3360 if (cpa == NULL) {
3361 atomic_inc(&pa->pa_count);
3362 return pa;
3363 }
3364 cur_distance = abs(goal_block - cpa->pa_pstart);
3365 new_distance = abs(goal_block - pa->pa_pstart);
3366
3367 if (cur_distance < new_distance)
3368 return cpa;
3369
3370 /* drop the previous reference */
3371 atomic_dec(&cpa->pa_count);
3372 atomic_inc(&pa->pa_count);
3373 return pa;
3374}
3375
3376/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003377 * search goal blocks in preallocated space
3378 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003379static noinline_for_stack int
3380ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003381{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003382 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003383 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3384 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003385 struct ext4_prealloc_space *pa, *cpa = NULL;
3386 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003387
3388 /* only data can be preallocated */
3389 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3390 return 0;
3391
3392 /* first, try per-file preallocation */
3393 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003394 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003395
3396 /* all fields in this condition don't change,
3397 * so we can skip locking for them */
3398 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3399 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3400 continue;
3401
3402 /* found preallocated blocks, use them */
3403 spin_lock(&pa->pa_lock);
3404 if (pa->pa_deleted == 0 && pa->pa_free) {
3405 atomic_inc(&pa->pa_count);
3406 ext4_mb_use_inode_pa(ac, pa);
3407 spin_unlock(&pa->pa_lock);
3408 ac->ac_criteria = 10;
3409 rcu_read_unlock();
3410 return 1;
3411 }
3412 spin_unlock(&pa->pa_lock);
3413 }
3414 rcu_read_unlock();
3415
3416 /* can we use group allocation? */
3417 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3418 return 0;
3419
3420 /* inode may have no locality group for some reason */
3421 lg = ac->ac_lg;
3422 if (lg == NULL)
3423 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003424 order = fls(ac->ac_o_ex.fe_len) - 1;
3425 if (order > PREALLOC_TB_SIZE - 1)
3426 /* The max size of hash table is PREALLOC_TB_SIZE */
3427 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003428
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003429 goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
3430 ac->ac_g_ex.fe_start +
3431 le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
3432 /*
3433 * search for the prealloc space that is having
3434 * minimal distance from the goal block.
3435 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003436 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3437 rcu_read_lock();
3438 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3439 pa_inode_list) {
3440 spin_lock(&pa->pa_lock);
3441 if (pa->pa_deleted == 0 &&
3442 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003443
3444 cpa = ext4_mb_check_group_pa(goal_block,
3445 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003446 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003447 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003448 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003449 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003450 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003451 if (cpa) {
3452 ext4_mb_use_group_pa(ac, cpa);
3453 ac->ac_criteria = 20;
3454 return 1;
3455 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003456 return 0;
3457}
3458
3459/*
3460 * the function goes through all preallocation in this group and marks them
3461 * used in in-core bitmap. buddy must be generated from this bitmap
3462 * Need to be called with ext4 group lock (ext4_lock_group)
3463 */
3464static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3465 ext4_group_t group)
3466{
3467 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3468 struct ext4_prealloc_space *pa;
3469 struct list_head *cur;
3470 ext4_group_t groupnr;
3471 ext4_grpblk_t start;
3472 int preallocated = 0;
3473 int count = 0;
3474 int len;
3475
3476 /* all form of preallocation discards first load group,
3477 * so the only competing code is preallocation use.
3478 * we don't need any locking here
3479 * notice we do NOT ignore preallocations with pa_deleted
3480 * otherwise we could leave used blocks available for
3481 * allocation in buddy when concurrent ext4_mb_put_pa()
3482 * is dropping preallocation
3483 */
3484 list_for_each(cur, &grp->bb_prealloc_list) {
3485 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3486 spin_lock(&pa->pa_lock);
3487 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3488 &groupnr, &start);
3489 len = pa->pa_len;
3490 spin_unlock(&pa->pa_lock);
3491 if (unlikely(len == 0))
3492 continue;
3493 BUG_ON(groupnr != group);
3494 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3495 bitmap, start, len);
3496 preallocated += len;
3497 count++;
3498 }
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003499 mb_debug("prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003500}
3501
3502static void ext4_mb_pa_callback(struct rcu_head *head)
3503{
3504 struct ext4_prealloc_space *pa;
3505 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3506 kmem_cache_free(ext4_pspace_cachep, pa);
3507}
3508
3509/*
3510 * drops a reference to preallocated space descriptor
3511 * if this was the last reference and the space is consumed
3512 */
3513static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3514 struct super_block *sb, struct ext4_prealloc_space *pa)
3515{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003516 ext4_group_t grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05003517
3518 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3519 return;
3520
3521 /* in this short window concurrent discard can set pa_deleted */
3522 spin_lock(&pa->pa_lock);
3523 if (pa->pa_deleted == 1) {
3524 spin_unlock(&pa->pa_lock);
3525 return;
3526 }
3527
3528 pa->pa_deleted = 1;
3529 spin_unlock(&pa->pa_lock);
3530
3531 /* -1 is to protect from crossing allocation group */
3532 ext4_get_group_no_and_offset(sb, pa->pa_pstart - 1, &grp, NULL);
3533
3534 /*
3535 * possible race:
3536 *
3537 * P1 (buddy init) P2 (regular allocation)
3538 * find block B in PA
3539 * copy on-disk bitmap to buddy
3540 * mark B in on-disk bitmap
3541 * drop PA from group
3542 * mark all PAs in buddy
3543 *
3544 * thus, P1 initializes buddy with B available. to prevent this
3545 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3546 * against that pair
3547 */
3548 ext4_lock_group(sb, grp);
3549 list_del(&pa->pa_group_list);
3550 ext4_unlock_group(sb, grp);
3551
3552 spin_lock(pa->pa_obj_lock);
3553 list_del_rcu(&pa->pa_inode_list);
3554 spin_unlock(pa->pa_obj_lock);
3555
3556 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3557}
3558
3559/*
3560 * creates new preallocated space for given inode
3561 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003562static noinline_for_stack int
3563ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003564{
3565 struct super_block *sb = ac->ac_sb;
3566 struct ext4_prealloc_space *pa;
3567 struct ext4_group_info *grp;
3568 struct ext4_inode_info *ei;
3569
3570 /* preallocate only when found space is larger then requested */
3571 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3572 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3573 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3574
3575 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3576 if (pa == NULL)
3577 return -ENOMEM;
3578
3579 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3580 int winl;
3581 int wins;
3582 int win;
3583 int offs;
3584
3585 /* we can't allocate as much as normalizer wants.
3586 * so, found space must get proper lstart
3587 * to cover original request */
3588 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3589 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3590
3591 /* we're limited by original request in that
3592 * logical block must be covered any way
3593 * winl is window we can move our chunk within */
3594 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3595
3596 /* also, we should cover whole original request */
3597 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3598
3599 /* the smallest one defines real window */
3600 win = min(winl, wins);
3601
3602 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3603 if (offs && offs < win)
3604 win = offs;
3605
3606 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3607 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3608 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3609 }
3610
3611 /* preallocation can change ac_b_ex, thus we store actually
3612 * allocated blocks for history */
3613 ac->ac_f_ex = ac->ac_b_ex;
3614
3615 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3616 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3617 pa->pa_len = ac->ac_b_ex.fe_len;
3618 pa->pa_free = pa->pa_len;
3619 atomic_set(&pa->pa_count, 1);
3620 spin_lock_init(&pa->pa_lock);
3621 pa->pa_deleted = 0;
3622 pa->pa_linear = 0;
3623
3624 mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
3625 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3626
3627 ext4_mb_use_inode_pa(ac, pa);
3628 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3629
3630 ei = EXT4_I(ac->ac_inode);
3631 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3632
3633 pa->pa_obj_lock = &ei->i_prealloc_lock;
3634 pa->pa_inode = ac->ac_inode;
3635
3636 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3637 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3638 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3639
3640 spin_lock(pa->pa_obj_lock);
3641 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3642 spin_unlock(pa->pa_obj_lock);
3643
3644 return 0;
3645}
3646
3647/*
3648 * creates new preallocated space for locality group inodes belongs to
3649 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003650static noinline_for_stack int
3651ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003652{
3653 struct super_block *sb = ac->ac_sb;
3654 struct ext4_locality_group *lg;
3655 struct ext4_prealloc_space *pa;
3656 struct ext4_group_info *grp;
3657
3658 /* preallocate only when found space is larger then requested */
3659 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3660 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3661 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3662
3663 BUG_ON(ext4_pspace_cachep == NULL);
3664 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3665 if (pa == NULL)
3666 return -ENOMEM;
3667
3668 /* preallocation can change ac_b_ex, thus we store actually
3669 * allocated blocks for history */
3670 ac->ac_f_ex = ac->ac_b_ex;
3671
3672 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3673 pa->pa_lstart = pa->pa_pstart;
3674 pa->pa_len = ac->ac_b_ex.fe_len;
3675 pa->pa_free = pa->pa_len;
3676 atomic_set(&pa->pa_count, 1);
3677 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003678 INIT_LIST_HEAD(&pa->pa_inode_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003679 pa->pa_deleted = 0;
3680 pa->pa_linear = 1;
3681
3682 mb_debug("new group pa %p: %llu/%u for %u\n", pa,
3683 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3684
3685 ext4_mb_use_group_pa(ac, pa);
3686 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3687
3688 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3689 lg = ac->ac_lg;
3690 BUG_ON(lg == NULL);
3691
3692 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3693 pa->pa_inode = NULL;
3694
3695 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3696 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3697 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3698
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003699 /*
3700 * We will later add the new pa to the right bucket
3701 * after updating the pa_free in ext4_mb_release_context
3702 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003703 return 0;
3704}
3705
3706static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3707{
3708 int err;
3709
3710 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3711 err = ext4_mb_new_group_pa(ac);
3712 else
3713 err = ext4_mb_new_inode_pa(ac);
3714 return err;
3715}
3716
3717/*
3718 * finds all unused blocks in on-disk bitmap, frees them in
3719 * in-core bitmap and buddy.
3720 * @pa must be unlinked from inode and group lists, so that
3721 * nobody else can find/use it.
3722 * the caller MUST hold group/inode locks.
3723 * TODO: optimize the case when there are no in-core structures yet
3724 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003725static noinline_for_stack int
3726ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003727 struct ext4_prealloc_space *pa,
3728 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003729{
Alex Tomasc9de5602008-01-29 00:19:52 -05003730 struct super_block *sb = e4b->bd_sb;
3731 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003732 unsigned int end;
3733 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003734 ext4_group_t group;
3735 ext4_grpblk_t bit;
3736 sector_t start;
3737 int err = 0;
3738 int free = 0;
3739
3740 BUG_ON(pa->pa_deleted == 0);
3741 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3742 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3743 end = bit + pa->pa_len;
3744
Eric Sandeen256bdb42008-02-10 01:13:33 -05003745 if (ac) {
3746 ac->ac_sb = sb;
3747 ac->ac_inode = pa->pa_inode;
3748 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3749 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003750
3751 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003752 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003753 if (bit >= end)
3754 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003755 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003756 start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3757 le32_to_cpu(sbi->s_es->s_first_data_block);
3758 mb_debug(" free preallocated %u/%u in group %u\n",
3759 (unsigned) start, (unsigned) next - bit,
3760 (unsigned) group);
3761 free += next - bit;
3762
Eric Sandeen256bdb42008-02-10 01:13:33 -05003763 if (ac) {
3764 ac->ac_b_ex.fe_group = group;
3765 ac->ac_b_ex.fe_start = bit;
3766 ac->ac_b_ex.fe_len = next - bit;
3767 ac->ac_b_ex.fe_logical = 0;
3768 ext4_mb_store_history(ac);
3769 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003770
3771 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3772 bit = next + 1;
3773 }
3774 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003775 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003776 pa, (unsigned long) pa->pa_lstart,
3777 (unsigned long) pa->pa_pstart,
3778 (unsigned long) pa->pa_len);
Theodore Ts'ofde4d952009-01-05 22:17:35 -05003779 ext4_error(sb, __func__, "free %u, pa_free %u",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003780 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003781 /*
3782 * pa is already deleted so we use the value obtained
3783 * from the bitmap and continue.
3784 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003785 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003786 atomic_add(free, &sbi->s_mb_discarded);
3787
3788 return err;
3789}
3790
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003791static noinline_for_stack int
3792ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003793 struct ext4_prealloc_space *pa,
3794 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003795{
Alex Tomasc9de5602008-01-29 00:19:52 -05003796 struct super_block *sb = e4b->bd_sb;
3797 ext4_group_t group;
3798 ext4_grpblk_t bit;
3799
Eric Sandeen256bdb42008-02-10 01:13:33 -05003800 if (ac)
3801 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
Alex Tomasc9de5602008-01-29 00:19:52 -05003802
3803 BUG_ON(pa->pa_deleted == 0);
3804 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3805 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3806 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3807 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3808
Eric Sandeen256bdb42008-02-10 01:13:33 -05003809 if (ac) {
3810 ac->ac_sb = sb;
3811 ac->ac_inode = NULL;
3812 ac->ac_b_ex.fe_group = group;
3813 ac->ac_b_ex.fe_start = bit;
3814 ac->ac_b_ex.fe_len = pa->pa_len;
3815 ac->ac_b_ex.fe_logical = 0;
3816 ext4_mb_store_history(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003817 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003818
3819 return 0;
3820}
3821
3822/*
3823 * releases all preallocations in given group
3824 *
3825 * first, we need to decide discard policy:
3826 * - when do we discard
3827 * 1) ENOSPC
3828 * - how many do we discard
3829 * 1) how many requested
3830 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003831static noinline_for_stack int
3832ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003833 ext4_group_t group, int needed)
3834{
3835 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3836 struct buffer_head *bitmap_bh = NULL;
3837 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003838 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003839 struct list_head list;
3840 struct ext4_buddy e4b;
3841 int err;
3842 int busy = 0;
3843 int free = 0;
3844
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003845 mb_debug("discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003846
3847 if (list_empty(&grp->bb_prealloc_list))
3848 return 0;
3849
Theodore Ts'o574ca172008-07-11 19:27:31 -04003850 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003851 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003852 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003853 "bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003854 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003855 }
3856
3857 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003858 if (err) {
3859 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003860 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003861 put_bh(bitmap_bh);
3862 return 0;
3863 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003864
3865 if (needed == 0)
3866 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3867
Alex Tomasc9de5602008-01-29 00:19:52 -05003868 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003869 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05003870repeat:
3871 ext4_lock_group(sb, group);
3872 list_for_each_entry_safe(pa, tmp,
3873 &grp->bb_prealloc_list, pa_group_list) {
3874 spin_lock(&pa->pa_lock);
3875 if (atomic_read(&pa->pa_count)) {
3876 spin_unlock(&pa->pa_lock);
3877 busy = 1;
3878 continue;
3879 }
3880 if (pa->pa_deleted) {
3881 spin_unlock(&pa->pa_lock);
3882 continue;
3883 }
3884
3885 /* seems this one can be freed ... */
3886 pa->pa_deleted = 1;
3887
3888 /* we can trust pa_free ... */
3889 free += pa->pa_free;
3890
3891 spin_unlock(&pa->pa_lock);
3892
3893 list_del(&pa->pa_group_list);
3894 list_add(&pa->u.pa_tmp_list, &list);
3895 }
3896
3897 /* if we still need more blocks and some PAs were used, try again */
3898 if (free < needed && busy) {
3899 busy = 0;
3900 ext4_unlock_group(sb, group);
3901 /*
3902 * Yield the CPU here so that we don't get soft lockup
3903 * in non preempt case.
3904 */
3905 yield();
3906 goto repeat;
3907 }
3908
3909 /* found anything to free? */
3910 if (list_empty(&list)) {
3911 BUG_ON(free != 0);
3912 goto out;
3913 }
3914
3915 /* now free all selected PAs */
3916 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3917
3918 /* remove from object (inode or locality group) */
3919 spin_lock(pa->pa_obj_lock);
3920 list_del_rcu(&pa->pa_inode_list);
3921 spin_unlock(pa->pa_obj_lock);
3922
3923 if (pa->pa_linear)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003924 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003925 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003926 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003927
3928 list_del(&pa->u.pa_tmp_list);
3929 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3930 }
3931
3932out:
3933 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003934 if (ac)
3935 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003936 ext4_mb_release_desc(&e4b);
3937 put_bh(bitmap_bh);
3938 return free;
3939}
3940
3941/*
3942 * releases all non-used preallocated blocks for given inode
3943 *
3944 * It's important to discard preallocations under i_data_sem
3945 * We don't want another block to be served from the prealloc
3946 * space when we are discarding the inode prealloc space.
3947 *
3948 * FIXME!! Make sure it is valid at all the call sites
3949 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003950void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003951{
3952 struct ext4_inode_info *ei = EXT4_I(inode);
3953 struct super_block *sb = inode->i_sb;
3954 struct buffer_head *bitmap_bh = NULL;
3955 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003956 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003957 ext4_group_t group = 0;
3958 struct list_head list;
3959 struct ext4_buddy e4b;
3960 int err;
3961
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003962 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003963 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3964 return;
3965 }
3966
3967 mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
3968
3969 INIT_LIST_HEAD(&list);
3970
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003971 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Alex Tomasc9de5602008-01-29 00:19:52 -05003972repeat:
3973 /* first, collect all pa's in the inode */
3974 spin_lock(&ei->i_prealloc_lock);
3975 while (!list_empty(&ei->i_prealloc_list)) {
3976 pa = list_entry(ei->i_prealloc_list.next,
3977 struct ext4_prealloc_space, pa_inode_list);
3978 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3979 spin_lock(&pa->pa_lock);
3980 if (atomic_read(&pa->pa_count)) {
3981 /* this shouldn't happen often - nobody should
3982 * use preallocation while we're discarding it */
3983 spin_unlock(&pa->pa_lock);
3984 spin_unlock(&ei->i_prealloc_lock);
3985 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3986 WARN_ON(1);
3987 schedule_timeout_uninterruptible(HZ);
3988 goto repeat;
3989
3990 }
3991 if (pa->pa_deleted == 0) {
3992 pa->pa_deleted = 1;
3993 spin_unlock(&pa->pa_lock);
3994 list_del_rcu(&pa->pa_inode_list);
3995 list_add(&pa->u.pa_tmp_list, &list);
3996 continue;
3997 }
3998
3999 /* someone is deleting pa right now */
4000 spin_unlock(&pa->pa_lock);
4001 spin_unlock(&ei->i_prealloc_lock);
4002
4003 /* we have to wait here because pa_deleted
4004 * doesn't mean pa is already unlinked from
4005 * the list. as we might be called from
4006 * ->clear_inode() the inode will get freed
4007 * and concurrent thread which is unlinking
4008 * pa from inode's list may access already
4009 * freed memory, bad-bad-bad */
4010
4011 /* XXX: if this happens too often, we can
4012 * add a flag to force wait only in case
4013 * of ->clear_inode(), but not in case of
4014 * regular truncate */
4015 schedule_timeout_uninterruptible(HZ);
4016 goto repeat;
4017 }
4018 spin_unlock(&ei->i_prealloc_lock);
4019
4020 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4021 BUG_ON(pa->pa_linear != 0);
4022 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4023
4024 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004025 if (err) {
4026 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004027 "information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004028 continue;
4029 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004030
Theodore Ts'o574ca172008-07-11 19:27:31 -04004031 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004032 if (bitmap_bh == NULL) {
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004033 ext4_error(sb, __func__, "Error in reading block "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004034 "bitmap for %u", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004035 ext4_mb_release_desc(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004036 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004037 }
4038
4039 ext4_lock_group(sb, group);
4040 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004041 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004042 ext4_unlock_group(sb, group);
4043
4044 ext4_mb_release_desc(&e4b);
4045 put_bh(bitmap_bh);
4046
4047 list_del(&pa->u.pa_tmp_list);
4048 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4049 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04004050 if (ac)
4051 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004052}
4053
4054/*
4055 * finds all preallocated spaces and return blocks being freed to them
4056 * if preallocated space becomes full (no block is used from the space)
4057 * then the function frees space in buddy
4058 * XXX: at the moment, truncate (which is the only way to free blocks)
4059 * discards all preallocations
4060 */
4061static void ext4_mb_return_to_preallocation(struct inode *inode,
4062 struct ext4_buddy *e4b,
4063 sector_t block, int count)
4064{
4065 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4066}
4067#ifdef MB_DEBUG
4068static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4069{
4070 struct super_block *sb = ac->ac_sb;
4071 ext4_group_t i;
4072
4073 printk(KERN_ERR "EXT4-fs: Can't allocate:"
4074 " Allocation context details:\n");
4075 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4076 ac->ac_status, ac->ac_flags);
4077 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4078 "best %lu/%lu/%lu@%lu cr %d\n",
4079 (unsigned long)ac->ac_o_ex.fe_group,
4080 (unsigned long)ac->ac_o_ex.fe_start,
4081 (unsigned long)ac->ac_o_ex.fe_len,
4082 (unsigned long)ac->ac_o_ex.fe_logical,
4083 (unsigned long)ac->ac_g_ex.fe_group,
4084 (unsigned long)ac->ac_g_ex.fe_start,
4085 (unsigned long)ac->ac_g_ex.fe_len,
4086 (unsigned long)ac->ac_g_ex.fe_logical,
4087 (unsigned long)ac->ac_b_ex.fe_group,
4088 (unsigned long)ac->ac_b_ex.fe_start,
4089 (unsigned long)ac->ac_b_ex.fe_len,
4090 (unsigned long)ac->ac_b_ex.fe_logical,
4091 (int)ac->ac_criteria);
4092 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4093 ac->ac_found);
4094 printk(KERN_ERR "EXT4-fs: groups: \n");
4095 for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
4096 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4097 struct ext4_prealloc_space *pa;
4098 ext4_grpblk_t start;
4099 struct list_head *cur;
4100 ext4_lock_group(sb, i);
4101 list_for_each(cur, &grp->bb_prealloc_list) {
4102 pa = list_entry(cur, struct ext4_prealloc_space,
4103 pa_group_list);
4104 spin_lock(&pa->pa_lock);
4105 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4106 NULL, &start);
4107 spin_unlock(&pa->pa_lock);
4108 printk(KERN_ERR "PA:%lu:%d:%u \n", i,
4109 start, pa->pa_len);
4110 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004111 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004112
4113 if (grp->bb_free == 0)
4114 continue;
4115 printk(KERN_ERR "%lu: %d/%d \n",
4116 i, grp->bb_free, grp->bb_fragments);
4117 }
4118 printk(KERN_ERR "\n");
4119}
4120#else
4121static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4122{
4123 return;
4124}
4125#endif
4126
4127/*
4128 * We use locality group preallocation for small size file. The size of the
4129 * file is determined by the current size or the resulting size after
4130 * allocation which ever is larger
4131 *
4132 * One can tune this size via /proc/fs/ext4/<partition>/stream_req
4133 */
4134static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4135{
4136 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4137 int bsbits = ac->ac_sb->s_blocksize_bits;
4138 loff_t size, isize;
4139
4140 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4141 return;
4142
4143 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4144 isize = i_size_read(ac->ac_inode) >> bsbits;
4145 size = max(size, isize);
4146
4147 /* don't use group allocation for large files */
4148 if (size >= sbi->s_mb_stream_request)
4149 return;
4150
4151 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4152 return;
4153
4154 BUG_ON(ac->ac_lg != NULL);
4155 /*
4156 * locality group prealloc space are per cpu. The reason for having
4157 * per cpu locality group is to reduce the contention between block
4158 * request from multiple CPUs.
4159 */
Eric Sandeen730c2132008-09-13 15:23:29 -04004160 ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
Alex Tomasc9de5602008-01-29 00:19:52 -05004161
4162 /* we're going to use group allocation */
4163 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4164
4165 /* serialize all allocations in the group */
4166 mutex_lock(&ac->ac_lg->lg_mutex);
4167}
4168
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004169static noinline_for_stack int
4170ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004171 struct ext4_allocation_request *ar)
4172{
4173 struct super_block *sb = ar->inode->i_sb;
4174 struct ext4_sb_info *sbi = EXT4_SB(sb);
4175 struct ext4_super_block *es = sbi->s_es;
4176 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004177 unsigned int len;
4178 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004179 ext4_grpblk_t block;
4180
4181 /* we can't allocate > group size */
4182 len = ar->len;
4183
4184 /* just a dirty hack to filter too big requests */
4185 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4186 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4187
4188 /* start searching from the goal */
4189 goal = ar->goal;
4190 if (goal < le32_to_cpu(es->s_first_data_block) ||
4191 goal >= ext4_blocks_count(es))
4192 goal = le32_to_cpu(es->s_first_data_block);
4193 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4194
4195 /* set up allocation goals */
4196 ac->ac_b_ex.fe_logical = ar->logical;
4197 ac->ac_b_ex.fe_group = 0;
4198 ac->ac_b_ex.fe_start = 0;
4199 ac->ac_b_ex.fe_len = 0;
4200 ac->ac_status = AC_STATUS_CONTINUE;
4201 ac->ac_groups_scanned = 0;
4202 ac->ac_ex_scanned = 0;
4203 ac->ac_found = 0;
4204 ac->ac_sb = sb;
4205 ac->ac_inode = ar->inode;
4206 ac->ac_o_ex.fe_logical = ar->logical;
4207 ac->ac_o_ex.fe_group = group;
4208 ac->ac_o_ex.fe_start = block;
4209 ac->ac_o_ex.fe_len = len;
4210 ac->ac_g_ex.fe_logical = ar->logical;
4211 ac->ac_g_ex.fe_group = group;
4212 ac->ac_g_ex.fe_start = block;
4213 ac->ac_g_ex.fe_len = len;
4214 ac->ac_f_ex.fe_len = 0;
4215 ac->ac_flags = ar->flags;
4216 ac->ac_2order = 0;
4217 ac->ac_criteria = 0;
4218 ac->ac_pa = NULL;
4219 ac->ac_bitmap_page = NULL;
4220 ac->ac_buddy_page = NULL;
4221 ac->ac_lg = NULL;
4222
4223 /* we have to define context: we'll we work with a file or
4224 * locality group. this is a policy, actually */
4225 ext4_mb_group_or_file(ac);
4226
4227 mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4228 "left: %u/%u, right %u/%u to %swritable\n",
4229 (unsigned) ar->len, (unsigned) ar->logical,
4230 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4231 (unsigned) ar->lleft, (unsigned) ar->pleft,
4232 (unsigned) ar->lright, (unsigned) ar->pright,
4233 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4234 return 0;
4235
4236}
4237
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004238static noinline_for_stack void
4239ext4_mb_discard_lg_preallocations(struct super_block *sb,
4240 struct ext4_locality_group *lg,
4241 int order, int total_entries)
4242{
4243 ext4_group_t group = 0;
4244 struct ext4_buddy e4b;
4245 struct list_head discard_list;
4246 struct ext4_prealloc_space *pa, *tmp;
4247 struct ext4_allocation_context *ac;
4248
4249 mb_debug("discard locality group preallocation\n");
4250
4251 INIT_LIST_HEAD(&discard_list);
4252 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4253
4254 spin_lock(&lg->lg_prealloc_lock);
4255 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4256 pa_inode_list) {
4257 spin_lock(&pa->pa_lock);
4258 if (atomic_read(&pa->pa_count)) {
4259 /*
4260 * This is the pa that we just used
4261 * for block allocation. So don't
4262 * free that
4263 */
4264 spin_unlock(&pa->pa_lock);
4265 continue;
4266 }
4267 if (pa->pa_deleted) {
4268 spin_unlock(&pa->pa_lock);
4269 continue;
4270 }
4271 /* only lg prealloc space */
4272 BUG_ON(!pa->pa_linear);
4273
4274 /* seems this one can be freed ... */
4275 pa->pa_deleted = 1;
4276 spin_unlock(&pa->pa_lock);
4277
4278 list_del_rcu(&pa->pa_inode_list);
4279 list_add(&pa->u.pa_tmp_list, &discard_list);
4280
4281 total_entries--;
4282 if (total_entries <= 5) {
4283 /*
4284 * we want to keep only 5 entries
4285 * allowing it to grow to 8. This
4286 * mak sure we don't call discard
4287 * soon for this list.
4288 */
4289 break;
4290 }
4291 }
4292 spin_unlock(&lg->lg_prealloc_lock);
4293
4294 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4295
4296 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4297 if (ext4_mb_load_buddy(sb, group, &e4b)) {
4298 ext4_error(sb, __func__, "Error in loading buddy "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05004299 "information for %u", group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004300 continue;
4301 }
4302 ext4_lock_group(sb, group);
4303 list_del(&pa->pa_group_list);
4304 ext4_mb_release_group_pa(&e4b, pa, ac);
4305 ext4_unlock_group(sb, group);
4306
4307 ext4_mb_release_desc(&e4b);
4308 list_del(&pa->u.pa_tmp_list);
4309 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4310 }
4311 if (ac)
4312 kmem_cache_free(ext4_ac_cachep, ac);
4313}
4314
4315/*
4316 * We have incremented pa_count. So it cannot be freed at this
4317 * point. Also we hold lg_mutex. So no parallel allocation is
4318 * possible from this lg. That means pa_free cannot be updated.
4319 *
4320 * A parallel ext4_mb_discard_group_preallocations is possible.
4321 * which can cause the lg_prealloc_list to be updated.
4322 */
4323
4324static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4325{
4326 int order, added = 0, lg_prealloc_count = 1;
4327 struct super_block *sb = ac->ac_sb;
4328 struct ext4_locality_group *lg = ac->ac_lg;
4329 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4330
4331 order = fls(pa->pa_free) - 1;
4332 if (order > PREALLOC_TB_SIZE - 1)
4333 /* The max size of hash table is PREALLOC_TB_SIZE */
4334 order = PREALLOC_TB_SIZE - 1;
4335 /* Add the prealloc space to lg */
4336 rcu_read_lock();
4337 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4338 pa_inode_list) {
4339 spin_lock(&tmp_pa->pa_lock);
4340 if (tmp_pa->pa_deleted) {
4341 spin_unlock(&pa->pa_lock);
4342 continue;
4343 }
4344 if (!added && pa->pa_free < tmp_pa->pa_free) {
4345 /* Add to the tail of the previous entry */
4346 list_add_tail_rcu(&pa->pa_inode_list,
4347 &tmp_pa->pa_inode_list);
4348 added = 1;
4349 /*
4350 * we want to count the total
4351 * number of entries in the list
4352 */
4353 }
4354 spin_unlock(&tmp_pa->pa_lock);
4355 lg_prealloc_count++;
4356 }
4357 if (!added)
4358 list_add_tail_rcu(&pa->pa_inode_list,
4359 &lg->lg_prealloc_list[order]);
4360 rcu_read_unlock();
4361
4362 /* Now trim the list to be not more than 8 elements */
4363 if (lg_prealloc_count > 8) {
4364 ext4_mb_discard_lg_preallocations(sb, lg,
4365 order, lg_prealloc_count);
4366 return;
4367 }
4368 return ;
4369}
4370
Alex Tomasc9de5602008-01-29 00:19:52 -05004371/*
4372 * release all resource we used in allocation
4373 */
4374static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4375{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004376 struct ext4_prealloc_space *pa = ac->ac_pa;
4377 if (pa) {
4378 if (pa->pa_linear) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004379 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004380 spin_lock(&pa->pa_lock);
4381 pa->pa_pstart += ac->ac_b_ex.fe_len;
4382 pa->pa_lstart += ac->ac_b_ex.fe_len;
4383 pa->pa_free -= ac->ac_b_ex.fe_len;
4384 pa->pa_len -= ac->ac_b_ex.fe_len;
4385 spin_unlock(&pa->pa_lock);
4386 /*
4387 * We want to add the pa to the right bucket.
4388 * Remove it from the list and while adding
4389 * make sure the list to which we are adding
4390 * doesn't grow big.
4391 */
4392 if (likely(pa->pa_free)) {
4393 spin_lock(pa->pa_obj_lock);
4394 list_del_rcu(&pa->pa_inode_list);
4395 spin_unlock(pa->pa_obj_lock);
4396 ext4_mb_add_n_trim(ac);
4397 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004398 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004399 ext4_mb_put_pa(ac, ac->ac_sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004400 }
4401 if (ac->ac_bitmap_page)
4402 page_cache_release(ac->ac_bitmap_page);
4403 if (ac->ac_buddy_page)
4404 page_cache_release(ac->ac_buddy_page);
4405 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4406 mutex_unlock(&ac->ac_lg->lg_mutex);
4407 ext4_mb_collect_stats(ac);
4408 return 0;
4409}
4410
4411static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4412{
4413 ext4_group_t i;
4414 int ret;
4415 int freed = 0;
4416
4417 for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
4418 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4419 freed += ret;
4420 needed -= ret;
4421 }
4422
4423 return freed;
4424}
4425
4426/*
4427 * Main entry point into mballoc to allocate blocks
4428 * it tries to use preallocation first, then falls back
4429 * to usual allocation
4430 */
4431ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4432 struct ext4_allocation_request *ar, int *errp)
4433{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004434 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004435 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004436 struct ext4_sb_info *sbi;
4437 struct super_block *sb;
4438 ext4_fsblk_t block = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004439 unsigned int inquota;
4440 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004441
4442 sb = ar->inode->i_sb;
4443 sbi = EXT4_SB(sb);
4444
Mingming Caod2a17632008-07-14 17:52:37 -04004445 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag) {
4446 /*
4447 * With delalloc we already reserved the blocks
4448 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004449 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4450 /* let others to free the space */
4451 yield();
4452 ar->len = ar->len >> 1;
4453 }
4454 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004455 *errp = -ENOSPC;
4456 return 0;
4457 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004458 reserv_blks = ar->len;
Mingming Caod2a17632008-07-14 17:52:37 -04004459 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004460 while (ar->len && DQUOT_ALLOC_BLOCK(ar->inode, ar->len)) {
4461 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4462 ar->len--;
4463 }
4464 if (ar->len == 0) {
4465 *errp = -EDQUOT;
4466 return 0;
4467 }
4468 inquota = ar->len;
4469
Mingming Caod2a17632008-07-14 17:52:37 -04004470 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4471 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4472
Eric Sandeen256bdb42008-02-10 01:13:33 -05004473 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4474 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004475 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004476 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004477 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004478 }
4479
Eric Sandeen256bdb42008-02-10 01:13:33 -05004480 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004481 if (*errp) {
4482 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004483 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004484 }
4485
Eric Sandeen256bdb42008-02-10 01:13:33 -05004486 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4487 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004488 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4489 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004490repeat:
4491 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004492 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004493
4494 /* as we've just preallocated more space than
4495 * user requested orinally, we store allocated
4496 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004497 if (ac->ac_status == AC_STATUS_FOUND &&
4498 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4499 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004500 }
4501
Eric Sandeen256bdb42008-02-10 01:13:33 -05004502 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004503 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004504 if (*errp == -EAGAIN) {
4505 ac->ac_b_ex.fe_group = 0;
4506 ac->ac_b_ex.fe_start = 0;
4507 ac->ac_b_ex.fe_len = 0;
4508 ac->ac_status = AC_STATUS_CONTINUE;
4509 goto repeat;
4510 } else if (*errp) {
4511 ac->ac_b_ex.fe_len = 0;
4512 ar->len = 0;
4513 ext4_mb_show_ac(ac);
4514 } else {
4515 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4516 ar->len = ac->ac_b_ex.fe_len;
4517 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004518 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004519 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004520 if (freed)
4521 goto repeat;
4522 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004523 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004524 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004525 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004526 }
4527
Eric Sandeen256bdb42008-02-10 01:13:33 -05004528 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004529
Shen Feng363d4252008-07-11 19:27:31 -04004530out2:
4531 kmem_cache_free(ext4_ac_cachep, ac);
4532out1:
Alex Tomasc9de5602008-01-29 00:19:52 -05004533 if (ar->len < inquota)
4534 DQUOT_FREE_BLOCK(ar->inode, inquota - ar->len);
4535
4536 return block;
4537}
Alex Tomasc9de5602008-01-29 00:19:52 -05004538
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004539/*
4540 * We can merge two free data extents only if the physical blocks
4541 * are contiguous, AND the extents were freed by the same transaction,
4542 * AND the blocks are associated with the same group.
4543 */
4544static int can_merge(struct ext4_free_data *entry1,
4545 struct ext4_free_data *entry2)
4546{
4547 if ((entry1->t_tid == entry2->t_tid) &&
4548 (entry1->group == entry2->group) &&
4549 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4550 return 1;
4551 return 0;
4552}
4553
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004554static noinline_for_stack int
4555ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05004556 ext4_group_t group, ext4_grpblk_t block, int count)
4557{
4558 struct ext4_group_info *db = e4b->bd_info;
4559 struct super_block *sb = e4b->bd_sb;
4560 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004561 struct ext4_free_data *entry, *new_entry;
4562 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4563 struct rb_node *parent = NULL, *new_node;
4564
Frank Mayhar03901312009-01-07 00:06:22 -05004565 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004566 BUG_ON(e4b->bd_bitmap_page == NULL);
4567 BUG_ON(e4b->bd_buddy_page == NULL);
4568
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004569 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4570 new_entry->start_blk = block;
4571 new_entry->group = group;
4572 new_entry->count = count;
4573 new_entry->t_tid = handle->h_transaction->t_tid;
4574 new_node = &new_entry->node;
4575
Alex Tomasc9de5602008-01-29 00:19:52 -05004576 ext4_lock_group(sb, group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004577 if (!*n) {
4578 /* first free block exent. We need to
4579 protect buddy cache from being freed,
4580 * otherwise we'll refresh it from
4581 * on-disk bitmap and lose not-yet-available
4582 * blocks */
4583 page_cache_get(e4b->bd_buddy_page);
4584 page_cache_get(e4b->bd_bitmap_page);
4585 }
4586 while (*n) {
4587 parent = *n;
4588 entry = rb_entry(parent, struct ext4_free_data, node);
4589 if (block < entry->start_blk)
4590 n = &(*n)->rb_left;
4591 else if (block >= (entry->start_blk + entry->count))
4592 n = &(*n)->rb_right;
4593 else {
Aneesh Kumar K.Vae2d9fb2008-11-04 09:10:50 -05004594 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004595 ext4_error(sb, __func__,
Theodore Ts'ofde4d952009-01-05 22:17:35 -05004596 "Double free of blocks %d (%d %d)",
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004597 block, entry->start_blk, entry->count);
4598 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004599 }
4600 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004601
4602 rb_link_node(new_node, parent, n);
4603 rb_insert_color(new_node, &db->bb_free_root);
4604
4605 /* Now try to see the extent can be merged to left and right */
4606 node = rb_prev(new_node);
4607 if (node) {
4608 entry = rb_entry(node, struct ext4_free_data, node);
4609 if (can_merge(entry, new_entry)) {
4610 new_entry->start_blk = entry->start_blk;
4611 new_entry->count += entry->count;
4612 rb_erase(node, &(db->bb_free_root));
4613 spin_lock(&sbi->s_md_lock);
4614 list_del(&entry->list);
4615 spin_unlock(&sbi->s_md_lock);
4616 kmem_cache_free(ext4_free_ext_cachep, entry);
4617 }
4618 }
4619
4620 node = rb_next(new_node);
4621 if (node) {
4622 entry = rb_entry(node, struct ext4_free_data, node);
4623 if (can_merge(new_entry, entry)) {
4624 new_entry->count += entry->count;
4625 rb_erase(node, &(db->bb_free_root));
4626 spin_lock(&sbi->s_md_lock);
4627 list_del(&entry->list);
4628 spin_unlock(&sbi->s_md_lock);
4629 kmem_cache_free(ext4_free_ext_cachep, entry);
4630 }
4631 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004632 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004633 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004634 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004635 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004636 ext4_unlock_group(sb, group);
4637 return 0;
4638}
4639
4640/*
4641 * Main entry point into mballoc to free blocks
4642 */
4643void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
4644 unsigned long block, unsigned long count,
4645 int metadata, unsigned long *freed)
4646{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004647 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004648 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004649 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004650 struct ext4_group_desc *gdp;
4651 struct ext4_super_block *es;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004652 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004653 ext4_grpblk_t bit;
4654 struct buffer_head *gd_bh;
4655 ext4_group_t block_group;
4656 struct ext4_sb_info *sbi;
4657 struct ext4_buddy e4b;
4658 int err = 0;
4659 int ret;
4660
4661 *freed = 0;
4662
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 sbi = EXT4_SB(sb);
4664 es = EXT4_SB(sb)->s_es;
4665 if (block < le32_to_cpu(es->s_first_data_block) ||
4666 block + count < block ||
4667 block + count > ext4_blocks_count(es)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -04004668 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004669 "Freeing blocks not in datazone - "
4670 "block = %lu, count = %lu", block, count);
4671 goto error_return;
4672 }
4673
4674 ext4_debug("freeing block %lu\n", block);
4675
Eric Sandeen256bdb42008-02-10 01:13:33 -05004676 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4677 if (ac) {
4678 ac->ac_op = EXT4_MB_HISTORY_FREE;
4679 ac->ac_inode = inode;
4680 ac->ac_sb = sb;
4681 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004682
4683do_more:
4684 overflow = 0;
4685 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4686
4687 /*
4688 * Check to see if we are freeing blocks across a group
4689 * boundary.
4690 */
4691 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4692 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4693 count -= overflow;
4694 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004695 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004696 if (!bitmap_bh) {
4697 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004698 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004699 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004700 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004701 if (!gdp) {
4702 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004703 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004704 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004705
4706 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4707 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4708 in_range(block, ext4_inode_table(sb, gdp),
4709 EXT4_SB(sb)->s_itb_per_group) ||
4710 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4711 EXT4_SB(sb)->s_itb_per_group)) {
4712
Harvey Harrison46e665e2008-04-17 10:38:59 -04004713 ext4_error(sb, __func__,
Alex Tomasc9de5602008-01-29 00:19:52 -05004714 "Freeing blocks in system zone - "
4715 "Block = %lu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004716 /* err = 0. ext4_std_error should be a no op */
4717 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004718 }
4719
4720 BUFFER_TRACE(bitmap_bh, "getting write access");
4721 err = ext4_journal_get_write_access(handle, bitmap_bh);
4722 if (err)
4723 goto error_return;
4724
4725 /*
4726 * We are about to modify some metadata. Call the journal APIs
4727 * to unshare ->b_data if a currently-committing transaction is
4728 * using it
4729 */
4730 BUFFER_TRACE(gd_bh, "get_write_access");
4731 err = ext4_journal_get_write_access(handle, gd_bh);
4732 if (err)
4733 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004734#ifdef AGGRESSIVE_CHECK
4735 {
4736 int i;
4737 for (i = 0; i < count; i++)
4738 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4739 }
4740#endif
4741 mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
4742 bit, count);
4743
4744 /* We dirtied the bitmap block */
4745 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
Frank Mayhar03901312009-01-07 00:06:22 -05004746 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004747 if (err)
4748 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004749
Eric Sandeen256bdb42008-02-10 01:13:33 -05004750 if (ac) {
4751 ac->ac_b_ex.fe_group = block_group;
4752 ac->ac_b_ex.fe_start = bit;
4753 ac->ac_b_ex.fe_len = count;
4754 ext4_mb_store_history(ac);
4755 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004756
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004757 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4758 if (err)
4759 goto error_return;
Frank Mayhar03901312009-01-07 00:06:22 -05004760 if (metadata && ext4_handle_valid(handle)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004761 /* blocks being freed are metadata. these blocks shouldn't
4762 * be used until this transaction is committed */
4763 ext4_mb_free_metadata(handle, &e4b, block_group, bit, count);
4764 } else {
4765 ext4_lock_group(sb, block_group);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004766 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004767 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4768 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004769 }
4770
4771 spin_lock(sb_bgl_lock(sbi, block_group));
Marcin Slusarze8546d02008-04-17 10:38:59 -04004772 le16_add_cpu(&gdp->bg_free_blocks_count, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004773 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4774 spin_unlock(sb_bgl_lock(sbi, block_group));
4775 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4776
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004777 if (sbi->s_log_groups_per_flex) {
4778 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4779 spin_lock(sb_bgl_lock(sbi, flex_group));
4780 sbi->s_flex_groups[flex_group].free_blocks += count;
4781 spin_unlock(sb_bgl_lock(sbi, flex_group));
4782 }
4783
Alex Tomasc9de5602008-01-29 00:19:52 -05004784 ext4_mb_release_desc(&e4b);
4785
4786 *freed += count;
4787
4788 /* And the group descriptor block */
4789 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004790 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004791 if (!err)
4792 err = ret;
4793
4794 if (overflow && !err) {
4795 block += count;
4796 count = overflow;
4797 put_bh(bitmap_bh);
4798 goto do_more;
4799 }
4800 sb->s_dirt = 1;
4801error_return:
4802 brelse(bitmap_bh);
4803 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004804 if (ac)
4805 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004806 return;
4807}