blob: 11568697b1926a53ddb9562661f91607b1bd974c [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 */
18
19
20/*
21 * mballoc.c contains the multiblocks allocation routines
22 */
23
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040025#include <linux/debugfs.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040026#include <trace/events/ext4.h>
27
Alex Tomasc9de5602008-01-29 00:19:52 -050028/*
29 * MUSTDO:
30 * - test ext4_ext_search_left() and ext4_ext_search_right()
31 * - search for metadata in few groups
32 *
33 * TODO v4:
34 * - normalization should take into account whether file is still open
35 * - discard preallocations if no free space left (policy?)
36 * - don't normalize tails
37 * - quota
38 * - reservation for superuser
39 *
40 * TODO v3:
41 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
42 * - track min/max extents in each group for better group selection
43 * - mb_mark_used() may allocate chunk right after splitting buddy
44 * - tree of groups sorted by number of free blocks
45 * - error handling
46 */
47
48/*
49 * The allocation request involve request for multiple number of blocks
50 * near to the goal(block) value specified.
51 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040052 * During initialization phase of the allocator we decide to use the
53 * group preallocation or inode preallocation depending on the size of
54 * the file. The size of the file could be the resulting file size we
55 * would have after allocation, or the current file size, which ever
56 * is larger. If the size is less than sbi->s_mb_stream_request we
57 * select to use the group preallocation. The default value of
58 * s_mb_stream_request is 16 blocks. This can also be tuned via
59 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
60 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050061 *
62 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040063 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050064 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040065 * First stage the allocator looks at the inode prealloc list,
66 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
67 * spaces for this particular inode. The inode prealloc space is
68 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050069 *
70 * pa_lstart -> the logical start block for this prealloc space
71 * pa_pstart -> the physical start block for this prealloc space
72 * pa_len -> lenght for this prealloc space
73 * pa_free -> free space available in this prealloc space
74 *
75 * The inode preallocation space is used looking at the _logical_ start
76 * block. If only the logical file block falls within the range of prealloc
77 * space we will consume the particular prealloc space. This make sure that
78 * that the we have contiguous physical blocks representing the file blocks
79 *
80 * The important thing to be noted in case of inode prealloc space is that
81 * we don't modify the values associated to inode prealloc space except
82 * pa_free.
83 *
84 * If we are not able to find blocks in the inode prealloc space and if we
85 * have the group allocation flag set then we look at the locality group
86 * prealloc space. These are per CPU prealloc list repreasented as
87 *
88 * ext4_sb_info.s_locality_groups[smp_processor_id()]
89 *
90 * The reason for having a per cpu locality group is to reduce the contention
91 * between CPUs. It is possible to get scheduled at this point.
92 *
93 * The locality group prealloc space is used looking at whether we have
94 * enough free space (pa_free) withing the prealloc space.
95 *
96 * If we can't allocate blocks via inode prealloc or/and locality group
97 * prealloc then we look at the buddy cache. The buddy cache is represented
98 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
99 * mapped to the buddy and bitmap information regarding different
100 * groups. The buddy information is attached to buddy cache inode so that
101 * we can access them through the page cache. The information regarding
102 * each group is loaded via ext4_mb_load_buddy. The information involve
103 * block bitmap and buddy information. The information are stored in the
104 * inode as:
105 *
106 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500107 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500108 *
109 *
110 * one block each for bitmap and buddy information. So for each group we
111 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
112 * blocksize) blocks. So it can have information regarding groups_per_page
113 * which is blocks_per_page/2
114 *
115 * The buddy cache inode is not stored on disk. The inode is thrown
116 * away when the filesystem is unmounted.
117 *
118 * We look for count number of blocks in the buddy cache. If we were able
119 * to locate that many free blocks we return with additional information
120 * regarding rest of the contiguous physical block available
121 *
122 * Before allocating blocks via buddy cache we normalize the request
123 * blocks. This ensure we ask for more blocks that we needed. The extra
124 * blocks that we get after allocation is added to the respective prealloc
125 * list. In case of inode preallocation we follow a list of heuristics
126 * based on file size. This can be found in ext4_mb_normalize_request. If
127 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400128 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500129 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400130 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500131 * terms of number of blocks. If we have mounted the file system with -O
132 * stripe=<value> option the group prealloc request is normalized to the
133 * stripe value (sbi->s_stripe)
134 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400135 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500136 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400137 * /sys/fs/ext4/<partition>/mb_min_to_scan
138 * /sys/fs/ext4/<partition>/mb_max_to_scan
139 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500140 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400141 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500142 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
143 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400144 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200145 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400146 * stripe size. This should result in better allocation on RAID setups. If
147 * not, we search in the specific group using bitmap for best extents. The
148 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500149 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400150 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500151 * best extent in the found extents. Searching for the blocks starts with
152 * the group specified as the goal value in allocation context via
153 * ac_g_ex. Each group is first checked based on the criteria whether it
154 * can used for allocation. ext4_mb_good_group explains how the groups are
155 * checked.
156 *
157 * Both the prealloc space are getting populated as above. So for the first
158 * request we will hit the buddy cache which will result in this prealloc
159 * space getting filled. The prealloc space is then later used for the
160 * subsequent request.
161 */
162
163/*
164 * mballoc operates on the following data:
165 * - on-disk bitmap
166 * - in-core buddy (actually includes buddy and bitmap)
167 * - preallocation descriptors (PAs)
168 *
169 * there are two types of preallocations:
170 * - inode
171 * assiged to specific inode and can be used for this inode only.
172 * it describes part of inode's space preallocated to specific
173 * physical blocks. any block from that preallocated can be used
174 * independent. the descriptor just tracks number of blocks left
175 * unused. so, before taking some block from descriptor, one must
176 * make sure corresponded logical block isn't allocated yet. this
177 * also means that freeing any block within descriptor's range
178 * must discard all preallocated blocks.
179 * - locality group
180 * assigned to specific locality group which does not translate to
181 * permanent set of inodes: inode can join and leave group. space
182 * from this type of preallocation can be used for any inode. thus
183 * it's consumed from the beginning to the end.
184 *
185 * relation between them can be expressed as:
186 * in-core buddy = on-disk bitmap + preallocation descriptors
187 *
188 * this mean blocks mballoc considers used are:
189 * - allocated blocks (persistent)
190 * - preallocated blocks (non-persistent)
191 *
192 * consistency in mballoc world means that at any time a block is either
193 * free or used in ALL structures. notice: "any time" should not be read
194 * literally -- time is discrete and delimited by locks.
195 *
196 * to keep it simple, we don't use block numbers, instead we count number of
197 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
198 *
199 * all operations can be expressed as:
200 * - init buddy: buddy = on-disk + PAs
201 * - new PA: buddy += N; PA = N
202 * - use inode PA: on-disk += N; PA -= N
203 * - discard inode PA buddy -= on-disk - PA; PA = 0
204 * - use locality group PA on-disk += N; PA -= N
205 * - discard locality group PA buddy -= PA; PA = 0
206 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
207 * is used in real operation because we can't know actual used
208 * bits from PA, only from on-disk bitmap
209 *
210 * if we follow this strict logic, then all operations above should be atomic.
211 * given some of them can block, we'd have to use something like semaphores
212 * killing performance on high-end SMP hardware. let's try to relax it using
213 * the following knowledge:
214 * 1) if buddy is referenced, it's already initialized
215 * 2) while block is used in buddy and the buddy is referenced,
216 * nobody can re-allocate that block
217 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
218 * bit set and PA claims same block, it's OK. IOW, one can set bit in
219 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
220 * block
221 *
222 * so, now we're building a concurrency table:
223 * - init buddy vs.
224 * - new PA
225 * blocks for PA are allocated in the buddy, buddy must be referenced
226 * until PA is linked to allocation group to avoid concurrent buddy init
227 * - use inode PA
228 * we need to make sure that either on-disk bitmap or PA has uptodate data
229 * given (3) we care that PA-=N operation doesn't interfere with init
230 * - discard inode PA
231 * the simplest way would be to have buddy initialized by the discard
232 * - use locality group PA
233 * again PA-=N must be serialized with init
234 * - discard locality group PA
235 * the simplest way would be to have buddy initialized by the discard
236 * - new PA vs.
237 * - use inode PA
238 * i_data_sem serializes them
239 * - discard inode PA
240 * discard process must wait until PA isn't used by another process
241 * - use locality group PA
242 * some mutex should serialize them
243 * - discard locality group PA
244 * discard process must wait until PA isn't used by another process
245 * - use inode PA
246 * - use inode PA
247 * i_data_sem or another mutex should serializes them
248 * - discard inode PA
249 * discard process must wait until PA isn't used by another process
250 * - use locality group PA
251 * nothing wrong here -- they're different PAs covering different blocks
252 * - discard locality group PA
253 * discard process must wait until PA isn't used by another process
254 *
255 * now we're ready to make few consequences:
256 * - PA is referenced and while it is no discard is possible
257 * - PA is referenced until block isn't marked in on-disk bitmap
258 * - PA changes only after on-disk bitmap
259 * - discard must not compete with init. either init is done before
260 * any discard or they're serialized somehow
261 * - buddy init as sum of on-disk bitmap and PAs is done atomically
262 *
263 * a special case when we've used PA to emptiness. no need to modify buddy
264 * in this case, but we should care about concurrent init
265 *
266 */
267
268 /*
269 * Logic in few words:
270 *
271 * - allocation:
272 * load group
273 * find blocks
274 * mark bits in on-disk bitmap
275 * release group
276 *
277 * - use preallocation:
278 * find proper PA (per-inode or group)
279 * load group
280 * mark bits in on-disk bitmap
281 * release group
282 * release PA
283 *
284 * - free:
285 * load group
286 * mark bits in on-disk bitmap
287 * release group
288 *
289 * - discard preallocations in group:
290 * mark PAs deleted
291 * move them onto local list
292 * load on-disk bitmap
293 * load group
294 * remove PA from object (inode or locality group)
295 * mark free blocks in-core
296 *
297 * - discard inode's preallocations:
298 */
299
300/*
301 * Locking rules
302 *
303 * Locks:
304 * - bitlock on a group (group)
305 * - object (inode/locality) (object)
306 * - per-pa lock (pa)
307 *
308 * Paths:
309 * - new pa
310 * object
311 * group
312 *
313 * - find and use pa:
314 * pa
315 *
316 * - release consumed pa:
317 * pa
318 * group
319 * object
320 *
321 * - generate in-core bitmap:
322 * group
323 * pa
324 *
325 * - discard all for given object (inode, locality group):
326 * object
327 * pa
328 * group
329 *
330 * - discard all for given group:
331 * group
332 * pa
333 * group
334 * object
335 *
336 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500337static struct kmem_cache *ext4_pspace_cachep;
338static struct kmem_cache *ext4_ac_cachep;
339static struct kmem_cache *ext4_free_ext_cachep;
340static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
341 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500342static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
343 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500344static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
345
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500346static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
347{
Alex Tomasc9de5602008-01-29 00:19:52 -0500348#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500349 *bit += ((unsigned long) addr & 7UL) << 3;
350 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500351#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500352 *bit += ((unsigned long) addr & 3UL) << 3;
353 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500354#else
355#error "how many bits you are?!"
356#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500357 return addr;
358}
Alex Tomasc9de5602008-01-29 00:19:52 -0500359
360static inline int mb_test_bit(int bit, void *addr)
361{
362 /*
363 * ext4_test_bit on architecture like powerpc
364 * needs unsigned long aligned address
365 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500367 return ext4_test_bit(bit, addr);
368}
369
370static inline void mb_set_bit(int bit, void *addr)
371{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500372 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500373 ext4_set_bit(bit, addr);
374}
375
Alex Tomasc9de5602008-01-29 00:19:52 -0500376static inline void mb_clear_bit(int bit, void *addr)
377{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500378 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500379 ext4_clear_bit(bit, addr);
380}
381
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500382static inline int mb_find_next_zero_bit(void *addr, int max, int start)
383{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400384 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500385 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400386 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500387 start += fix;
388
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400389 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
390 if (ret > max)
391 return max;
392 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500393}
394
395static inline int mb_find_next_bit(void *addr, int max, int start)
396{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400397 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400399 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500400 start += fix;
401
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400402 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
403 if (ret > max)
404 return max;
405 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500406}
407
Alex Tomasc9de5602008-01-29 00:19:52 -0500408static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
409{
410 char *bb;
411
Alex Tomasc9de5602008-01-29 00:19:52 -0500412 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
413 BUG_ON(max == NULL);
414
415 if (order > e4b->bd_blkbits + 1) {
416 *max = 0;
417 return NULL;
418 }
419
420 /* at order 0 we see each particular block */
421 *max = 1 << (e4b->bd_blkbits + 3);
422 if (order == 0)
423 return EXT4_MB_BITMAP(e4b);
424
425 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
426 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
427
428 return bb;
429}
430
431#ifdef DOUBLE_CHECK
432static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
433 int first, int count)
434{
435 int i;
436 struct super_block *sb = e4b->bd_sb;
437
438 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
439 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400440 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500441 for (i = 0; i < count; i++) {
442 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
443 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500444
445 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500446 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500447 ext4_grp_locked_error(sb, e4b->bd_group,
448 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500449 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500450 inode ? inode->i_ino : 0, blocknr,
451 first + i, e4b->bd_group);
452 }
453 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
454 }
455}
456
457static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
458{
459 int i;
460
461 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
462 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400463 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500464 for (i = 0; i < count; i++) {
465 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
466 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
467 }
468}
469
470static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
471{
472 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
473 unsigned char *b1, *b2;
474 int i;
475 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
476 b2 = (unsigned char *) bitmap;
477 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
478 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500479 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400480 "at byte %u(%u): %x in copy != %x "
481 "on disk/prealloc\n",
482 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500483 BUG();
484 }
485 }
486 }
487}
488
489#else
490static inline void mb_free_blocks_double(struct inode *inode,
491 struct ext4_buddy *e4b, int first, int count)
492{
493 return;
494}
495static inline void mb_mark_used_double(struct ext4_buddy *e4b,
496 int first, int count)
497{
498 return;
499}
500static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
501{
502 return;
503}
504#endif
505
506#ifdef AGGRESSIVE_CHECK
507
508#define MB_CHECK_ASSERT(assert) \
509do { \
510 if (!(assert)) { \
511 printk(KERN_EMERG \
512 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
513 function, file, line, # assert); \
514 BUG(); \
515 } \
516} while (0)
517
518static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
519 const char *function, int line)
520{
521 struct super_block *sb = e4b->bd_sb;
522 int order = e4b->bd_blkbits + 1;
523 int max;
524 int max2;
525 int i;
526 int j;
527 int k;
528 int count;
529 struct ext4_group_info *grp;
530 int fragments = 0;
531 int fstart;
532 struct list_head *cur;
533 void *buddy;
534 void *buddy2;
535
Alex Tomasc9de5602008-01-29 00:19:52 -0500536 {
537 static int mb_check_counter;
538 if (mb_check_counter++ % 100 != 0)
539 return 0;
540 }
541
542 while (order > 1) {
543 buddy = mb_find_buddy(e4b, order, &max);
544 MB_CHECK_ASSERT(buddy);
545 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
546 MB_CHECK_ASSERT(buddy2);
547 MB_CHECK_ASSERT(buddy != buddy2);
548 MB_CHECK_ASSERT(max * 2 == max2);
549
550 count = 0;
551 for (i = 0; i < max; i++) {
552
553 if (mb_test_bit(i, buddy)) {
554 /* only single bit in buddy2 may be 1 */
555 if (!mb_test_bit(i << 1, buddy2)) {
556 MB_CHECK_ASSERT(
557 mb_test_bit((i<<1)+1, buddy2));
558 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
559 MB_CHECK_ASSERT(
560 mb_test_bit(i << 1, buddy2));
561 }
562 continue;
563 }
564
565 /* both bits in buddy2 must be 0 */
566 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
567 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
568
569 for (j = 0; j < (1 << order); j++) {
570 k = (i * (1 << order)) + j;
571 MB_CHECK_ASSERT(
572 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
573 }
574 count++;
575 }
576 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
577 order--;
578 }
579
580 fstart = -1;
581 buddy = mb_find_buddy(e4b, 0, &max);
582 for (i = 0; i < max; i++) {
583 if (!mb_test_bit(i, buddy)) {
584 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
585 if (fstart == -1) {
586 fragments++;
587 fstart = i;
588 }
589 continue;
590 }
591 fstart = -1;
592 /* check used bits only */
593 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
594 buddy2 = mb_find_buddy(e4b, j, &max2);
595 k = i >> j;
596 MB_CHECK_ASSERT(k < max2);
597 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
598 }
599 }
600 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
601 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
602
603 grp = ext4_get_group_info(sb, e4b->bd_group);
604 buddy = mb_find_buddy(e4b, 0, &max);
605 list_for_each(cur, &grp->bb_prealloc_list) {
606 ext4_group_t groupnr;
607 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400608 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
609 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500610 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400611 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500612 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
613 }
614 return 0;
615}
616#undef MB_CHECK_ASSERT
617#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400618 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500619#else
620#define mb_check_buddy(e4b)
621#endif
622
623/* FIXME!! need more doc */
624static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400625 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500626 struct ext4_group_info *grp)
627{
628 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400629 ext4_grpblk_t min;
630 ext4_grpblk_t max;
631 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500632 unsigned short border;
633
Valerie Clementb73fce62008-02-15 13:48:51 -0500634 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500635
636 border = 2 << sb->s_blocksize_bits;
637
638 while (len > 0) {
639 /* find how many blocks can be covered since this position */
640 max = ffs(first | border) - 1;
641
642 /* find how many blocks of power 2 we need to mark */
643 min = fls(len) - 1;
644
645 if (max < min)
646 min = max;
647 chunk = 1 << min;
648
649 /* mark multiblock chunks only */
650 grp->bb_counters[min]++;
651 if (min > 0)
652 mb_clear_bit(first >> min,
653 buddy + sbi->s_mb_offsets[min]);
654
655 len -= chunk;
656 first += chunk;
657 }
658}
659
Eric Sandeen089ceec2009-07-05 22:17:31 -0400660static noinline_for_stack
661void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500662 void *buddy, void *bitmap, ext4_group_t group)
663{
664 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400665 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
666 ext4_grpblk_t i = 0;
667 ext4_grpblk_t first;
668 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500669 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) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500693 ext4_grp_locked_error(sb, group, __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 }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500719 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500720 *
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{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400731 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500732 int blocksize;
733 int blocks_per_page;
734 int groups_per_page;
735 int err = 0;
736 int i;
737 ext4_group_t first_group;
738 int first_block;
739 struct super_block *sb;
740 struct buffer_head *bhs;
741 struct buffer_head **bh;
742 struct inode *inode;
743 char *data;
744 char *bitmap;
745
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400746 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500747
748 inode = page->mapping->host;
749 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400750 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500751 blocksize = 1 << inode->i_blkbits;
752 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
753
754 groups_per_page = blocks_per_page >> 1;
755 if (groups_per_page == 0)
756 groups_per_page = 1;
757
758 /* allocate buffer_heads to read bitmaps */
759 if (groups_per_page > 1) {
760 err = -ENOMEM;
761 i = sizeof(struct buffer_head *) * groups_per_page;
762 bh = kzalloc(i, GFP_NOFS);
763 if (bh == NULL)
764 goto out;
765 } else
766 bh = &bhs;
767
768 first_group = page->index * blocks_per_page / 2;
769
770 /* read all groups the page covers into the cache */
771 for (i = 0; i < groups_per_page; i++) {
772 struct ext4_group_desc *desc;
773
Theodore Ts'o8df96752009-05-01 08:50:38 -0400774 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500775 break;
776
777 err = -EIO;
778 desc = ext4_get_group_desc(sb, first_group + i, NULL);
779 if (desc == NULL)
780 goto out;
781
782 err = -ENOMEM;
783 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
784 if (bh[i] == NULL)
785 goto out;
786
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500787 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500788 continue;
789
Frederic Bohec806e682008-10-10 08:09:18 -0400790 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500791 if (bitmap_uptodate(bh[i])) {
792 unlock_buffer(bh[i]);
793 continue;
794 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400795 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500796 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
797 ext4_init_block_bitmap(sb, bh[i],
798 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500799 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500800 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400801 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500802 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500803 continue;
804 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400805 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500806 if (buffer_uptodate(bh[i])) {
807 /*
808 * if not uninit if bh is uptodate,
809 * bitmap is also uptodate
810 */
811 set_bitmap_uptodate(bh[i]);
812 unlock_buffer(bh[i]);
813 continue;
814 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500815 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500816 /*
817 * submit the buffer_head for read. We can
818 * safely mark the bitmap as uptodate now.
819 * We do it here so the bitmap uptodate bit
820 * get set with buffer lock held.
821 */
822 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 bh[i]->b_end_io = end_buffer_read_sync;
824 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400825 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500826 }
827
828 /* wait for I/O completion */
829 for (i = 0; i < groups_per_page && bh[i]; i++)
830 wait_on_buffer(bh[i]);
831
832 err = -EIO;
833 for (i = 0; i < groups_per_page && bh[i]; i++)
834 if (!buffer_uptodate(bh[i]))
835 goto out;
836
Mingming Cao31b481d2008-07-11 19:27:31 -0400837 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500838 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500839 /* init the page */
840 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 for (i = 0; i < blocks_per_page; i++) {
842 int group;
843 struct ext4_group_info *grinfo;
844
845 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400846 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500847 break;
848
849 /*
850 * data carry information regarding this
851 * particular group in the format specified
852 * above
853 *
854 */
855 data = page_address(page) + (i * blocksize);
856 bitmap = bh[group - first_group]->b_data;
857
858 /*
859 * We place the buddy block and bitmap block
860 * close together
861 */
862 if ((first_block + i) & 1) {
863 /* this is block of buddy */
864 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400865 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500866 group, page->index, i * blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 grinfo = ext4_get_group_info(sb, group);
868 grinfo->bb_fragments = 0;
869 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400870 sizeof(*grinfo->bb_counters) *
871 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500872 /*
873 * incore got set to the group block bitmap below
874 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500875 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500876 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500877 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500878 incore = NULL;
879 } else {
880 /* this is block of bitmap */
881 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400882 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500883 group, page->index, i * blocksize);
884
885 /* see comments in ext4_mb_put_pa() */
886 ext4_lock_group(sb, group);
887 memcpy(data, bitmap, blocksize);
888
889 /* mark all preallocated blks used in in-core bitmap */
890 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500891 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500892 ext4_unlock_group(sb, group);
893
894 /* set incore so that the buddy information can be
895 * generated using this
896 */
897 incore = data;
898 }
899 }
900 SetPageUptodate(page);
901
902out:
903 if (bh) {
904 for (i = 0; i < groups_per_page && bh[i]; i++)
905 brelse(bh[i]);
906 if (bh != &bhs)
907 kfree(bh);
908 }
909 return err;
910}
911
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400912static noinline_for_stack
913int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
914{
915
916 int ret = 0;
917 void *bitmap;
918 int blocks_per_page;
919 int block, pnum, poff;
920 int num_grp_locked = 0;
921 struct ext4_group_info *this_grp;
922 struct ext4_sb_info *sbi = EXT4_SB(sb);
923 struct inode *inode = sbi->s_buddy_cache;
924 struct page *page = NULL, *bitmap_page = NULL;
925
926 mb_debug(1, "init group %u\n", group);
927 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
928 this_grp = ext4_get_group_info(sb, group);
929 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400930 * This ensures that we don't reinit the buddy cache
931 * page which map to the group from which we are already
932 * allocating. If we are looking at the buddy cache we would
933 * have taken a reference using ext4_mb_load_buddy and that
934 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400935 */
936 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
937 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
938 /*
939 * somebody initialized the group
940 * return without doing anything
941 */
942 ret = 0;
943 goto err;
944 }
945 /*
946 * the buddy cache inode stores the block bitmap
947 * and buddy information in consecutive blocks.
948 * So for each group we need two blocks.
949 */
950 block = group * 2;
951 pnum = block / blocks_per_page;
952 poff = block % blocks_per_page;
953 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
954 if (page) {
955 BUG_ON(page->mapping != inode->i_mapping);
956 ret = ext4_mb_init_cache(page, NULL);
957 if (ret) {
958 unlock_page(page);
959 goto err;
960 }
961 unlock_page(page);
962 }
963 if (page == NULL || !PageUptodate(page)) {
964 ret = -EIO;
965 goto err;
966 }
967 mark_page_accessed(page);
968 bitmap_page = page;
969 bitmap = page_address(page) + (poff * sb->s_blocksize);
970
971 /* init buddy cache */
972 block++;
973 pnum = block / blocks_per_page;
974 poff = block % blocks_per_page;
975 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
976 if (page == bitmap_page) {
977 /*
978 * If both the bitmap and buddy are in
979 * the same page we don't need to force
980 * init the buddy
981 */
982 unlock_page(page);
983 } else if (page) {
984 BUG_ON(page->mapping != inode->i_mapping);
985 ret = ext4_mb_init_cache(page, bitmap);
986 if (ret) {
987 unlock_page(page);
988 goto err;
989 }
990 unlock_page(page);
991 }
992 if (page == NULL || !PageUptodate(page)) {
993 ret = -EIO;
994 goto err;
995 }
996 mark_page_accessed(page);
997err:
998 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
999 if (bitmap_page)
1000 page_cache_release(bitmap_page);
1001 if (page)
1002 page_cache_release(page);
1003 return ret;
1004}
1005
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001006static noinline_for_stack int
1007ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1008 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001009{
Alex Tomasc9de5602008-01-29 00:19:52 -05001010 int blocks_per_page;
1011 int block;
1012 int pnum;
1013 int poff;
1014 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001015 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001016 struct ext4_group_info *grp;
1017 struct ext4_sb_info *sbi = EXT4_SB(sb);
1018 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001019
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001020 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001021
1022 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001023 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001024
1025 e4b->bd_blkbits = sb->s_blocksize_bits;
1026 e4b->bd_info = ext4_get_group_info(sb, group);
1027 e4b->bd_sb = sb;
1028 e4b->bd_group = group;
1029 e4b->bd_buddy_page = NULL;
1030 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001031 e4b->alloc_semp = &grp->alloc_sem;
1032
1033 /* Take the read lock on the group alloc
1034 * sem. This would make sure a parallel
1035 * ext4_mb_init_group happening on other
1036 * groups mapped by the page is blocked
1037 * till we are done with allocation
1038 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001039repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001040 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001041
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001042 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1043 /* we need to check for group need init flag
1044 * with alloc_semp held so that we can be sure
1045 * that new blocks didn't get added to the group
1046 * when we are loading the buddy cache
1047 */
1048 up_read(e4b->alloc_semp);
1049 /*
1050 * we need full data about the group
1051 * to make a good selection
1052 */
1053 ret = ext4_mb_init_group(sb, group);
1054 if (ret)
1055 return ret;
1056 goto repeat_load_buddy;
1057 }
1058
Alex Tomasc9de5602008-01-29 00:19:52 -05001059 /*
1060 * the buddy cache inode stores the block bitmap
1061 * and buddy information in consecutive blocks.
1062 * So for each group we need two blocks.
1063 */
1064 block = group * 2;
1065 pnum = block / blocks_per_page;
1066 poff = block % blocks_per_page;
1067
1068 /* we could use find_or_create_page(), but it locks page
1069 * what we'd like to avoid in fast path ... */
1070 page = find_get_page(inode->i_mapping, pnum);
1071 if (page == NULL || !PageUptodate(page)) {
1072 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001073 /*
1074 * drop the page reference and try
1075 * to get the page with lock. If we
1076 * are not uptodate that implies
1077 * somebody just created the page but
1078 * is yet to initialize the same. So
1079 * wait for it to initialize.
1080 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001081 page_cache_release(page);
1082 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1083 if (page) {
1084 BUG_ON(page->mapping != inode->i_mapping);
1085 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001086 ret = ext4_mb_init_cache(page, NULL);
1087 if (ret) {
1088 unlock_page(page);
1089 goto err;
1090 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001091 mb_cmp_bitmaps(e4b, page_address(page) +
1092 (poff * sb->s_blocksize));
1093 }
1094 unlock_page(page);
1095 }
1096 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001097 if (page == NULL || !PageUptodate(page)) {
1098 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001099 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001100 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001101 e4b->bd_bitmap_page = page;
1102 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1103 mark_page_accessed(page);
1104
1105 block++;
1106 pnum = block / blocks_per_page;
1107 poff = block % blocks_per_page;
1108
1109 page = find_get_page(inode->i_mapping, pnum);
1110 if (page == NULL || !PageUptodate(page)) {
1111 if (page)
1112 page_cache_release(page);
1113 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1114 if (page) {
1115 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001116 if (!PageUptodate(page)) {
1117 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1118 if (ret) {
1119 unlock_page(page);
1120 goto err;
1121 }
1122 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001123 unlock_page(page);
1124 }
1125 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001126 if (page == NULL || !PageUptodate(page)) {
1127 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001128 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001129 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001130 e4b->bd_buddy_page = page;
1131 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1132 mark_page_accessed(page);
1133
1134 BUG_ON(e4b->bd_bitmap_page == NULL);
1135 BUG_ON(e4b->bd_buddy_page == NULL);
1136
1137 return 0;
1138
1139err:
1140 if (e4b->bd_bitmap_page)
1141 page_cache_release(e4b->bd_bitmap_page);
1142 if (e4b->bd_buddy_page)
1143 page_cache_release(e4b->bd_buddy_page);
1144 e4b->bd_buddy = NULL;
1145 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001146
1147 /* Done with the buddy cache */
1148 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001149 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001150}
1151
1152static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1153{
1154 if (e4b->bd_bitmap_page)
1155 page_cache_release(e4b->bd_bitmap_page);
1156 if (e4b->bd_buddy_page)
1157 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001158 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001159 if (e4b->alloc_semp)
1160 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001161}
1162
1163
1164static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1165{
1166 int order = 1;
1167 void *bb;
1168
1169 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1170 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1171
1172 bb = EXT4_MB_BUDDY(e4b);
1173 while (order <= e4b->bd_blkbits + 1) {
1174 block = block >> 1;
1175 if (!mb_test_bit(block, bb)) {
1176 /* this block is part of buddy of order 'order' */
1177 return order;
1178 }
1179 bb += 1 << (e4b->bd_blkbits - order);
1180 order++;
1181 }
1182 return 0;
1183}
1184
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001185static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001186{
1187 __u32 *addr;
1188
1189 len = cur + len;
1190 while (cur < len) {
1191 if ((cur & 31) == 0 && (len - cur) >= 32) {
1192 /* fast path: clear whole word at once */
1193 addr = bm + (cur >> 3);
1194 *addr = 0;
1195 cur += 32;
1196 continue;
1197 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001198 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001199 cur++;
1200 }
1201}
1202
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001203static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001204{
1205 __u32 *addr;
1206
1207 len = cur + len;
1208 while (cur < len) {
1209 if ((cur & 31) == 0 && (len - cur) >= 32) {
1210 /* fast path: set whole word at once */
1211 addr = bm + (cur >> 3);
1212 *addr = 0xffffffff;
1213 cur += 32;
1214 continue;
1215 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001216 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001217 cur++;
1218 }
1219}
1220
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001221static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001222 int first, int count)
1223{
1224 int block = 0;
1225 int max = 0;
1226 int order;
1227 void *buddy;
1228 void *buddy2;
1229 struct super_block *sb = e4b->bd_sb;
1230
1231 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001232 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001233 mb_check_buddy(e4b);
1234 mb_free_blocks_double(inode, e4b, first, count);
1235
1236 e4b->bd_info->bb_free += count;
1237 if (first < e4b->bd_info->bb_first_free)
1238 e4b->bd_info->bb_first_free = first;
1239
1240 /* let's maintain fragments counter */
1241 if (first != 0)
1242 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1243 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1244 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1245 if (block && max)
1246 e4b->bd_info->bb_fragments--;
1247 else if (!block && !max)
1248 e4b->bd_info->bb_fragments++;
1249
1250 /* let's maintain buddy itself */
1251 while (count-- > 0) {
1252 block = first++;
1253 order = 0;
1254
1255 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1256 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001257
1258 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001259 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001260 ext4_grp_locked_error(sb, e4b->bd_group,
1261 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001262 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001263 inode ? inode->i_ino : 0, blocknr, block,
1264 e4b->bd_group);
1265 }
1266 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1267 e4b->bd_info->bb_counters[order]++;
1268
1269 /* start of the buddy */
1270 buddy = mb_find_buddy(e4b, order, &max);
1271
1272 do {
1273 block &= ~1UL;
1274 if (mb_test_bit(block, buddy) ||
1275 mb_test_bit(block + 1, buddy))
1276 break;
1277
1278 /* both the buddies are free, try to coalesce them */
1279 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1280
1281 if (!buddy2)
1282 break;
1283
1284 if (order > 0) {
1285 /* for special purposes, we don't set
1286 * free bits in bitmap */
1287 mb_set_bit(block, buddy);
1288 mb_set_bit(block + 1, buddy);
1289 }
1290 e4b->bd_info->bb_counters[order]--;
1291 e4b->bd_info->bb_counters[order]--;
1292
1293 block = block >> 1;
1294 order++;
1295 e4b->bd_info->bb_counters[order]++;
1296
1297 mb_clear_bit(block, buddy2);
1298 buddy = buddy2;
1299 } while (1);
1300 }
1301 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001302}
1303
1304static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1305 int needed, struct ext4_free_extent *ex)
1306{
1307 int next = block;
1308 int max;
1309 int ord;
1310 void *buddy;
1311
Vincent Minetbc8e6742009-05-15 08:33:18 -04001312 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001313 BUG_ON(ex == NULL);
1314
1315 buddy = mb_find_buddy(e4b, order, &max);
1316 BUG_ON(buddy == NULL);
1317 BUG_ON(block >= max);
1318 if (mb_test_bit(block, buddy)) {
1319 ex->fe_len = 0;
1320 ex->fe_start = 0;
1321 ex->fe_group = 0;
1322 return 0;
1323 }
1324
1325 /* FIXME dorp order completely ? */
1326 if (likely(order == 0)) {
1327 /* find actual order */
1328 order = mb_find_order_for_block(e4b, block);
1329 block = block >> order;
1330 }
1331
1332 ex->fe_len = 1 << order;
1333 ex->fe_start = block << order;
1334 ex->fe_group = e4b->bd_group;
1335
1336 /* calc difference from given start */
1337 next = next - ex->fe_start;
1338 ex->fe_len -= next;
1339 ex->fe_start += next;
1340
1341 while (needed > ex->fe_len &&
1342 (buddy = mb_find_buddy(e4b, order, &max))) {
1343
1344 if (block + 1 >= max)
1345 break;
1346
1347 next = (block + 1) * (1 << order);
1348 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1349 break;
1350
1351 ord = mb_find_order_for_block(e4b, next);
1352
1353 order = ord;
1354 block = next >> order;
1355 ex->fe_len += 1 << order;
1356 }
1357
1358 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1359 return ex->fe_len;
1360}
1361
1362static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1363{
1364 int ord;
1365 int mlen = 0;
1366 int max = 0;
1367 int cur;
1368 int start = ex->fe_start;
1369 int len = ex->fe_len;
1370 unsigned ret = 0;
1371 int len0 = len;
1372 void *buddy;
1373
1374 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1375 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001376 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001377 mb_check_buddy(e4b);
1378 mb_mark_used_double(e4b, start, len);
1379
1380 e4b->bd_info->bb_free -= len;
1381 if (e4b->bd_info->bb_first_free == start)
1382 e4b->bd_info->bb_first_free += len;
1383
1384 /* let's maintain fragments counter */
1385 if (start != 0)
1386 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1387 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1388 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1389 if (mlen && max)
1390 e4b->bd_info->bb_fragments++;
1391 else if (!mlen && !max)
1392 e4b->bd_info->bb_fragments--;
1393
1394 /* let's maintain buddy itself */
1395 while (len) {
1396 ord = mb_find_order_for_block(e4b, start);
1397
1398 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1399 /* the whole chunk may be allocated at once! */
1400 mlen = 1 << ord;
1401 buddy = mb_find_buddy(e4b, ord, &max);
1402 BUG_ON((start >> ord) >= max);
1403 mb_set_bit(start >> ord, buddy);
1404 e4b->bd_info->bb_counters[ord]--;
1405 start += mlen;
1406 len -= mlen;
1407 BUG_ON(len < 0);
1408 continue;
1409 }
1410
1411 /* store for history */
1412 if (ret == 0)
1413 ret = len | (ord << 16);
1414
1415 /* we have to split large buddy */
1416 BUG_ON(ord <= 0);
1417 buddy = mb_find_buddy(e4b, ord, &max);
1418 mb_set_bit(start >> ord, buddy);
1419 e4b->bd_info->bb_counters[ord]--;
1420
1421 ord--;
1422 cur = (start >> ord) & ~1U;
1423 buddy = mb_find_buddy(e4b, ord, &max);
1424 mb_clear_bit(cur, buddy);
1425 mb_clear_bit(cur + 1, buddy);
1426 e4b->bd_info->bb_counters[ord]++;
1427 e4b->bd_info->bb_counters[ord]++;
1428 }
1429
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001430 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001431 mb_check_buddy(e4b);
1432
1433 return ret;
1434}
1435
1436/*
1437 * Must be called under group lock!
1438 */
1439static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1440 struct ext4_buddy *e4b)
1441{
1442 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1443 int ret;
1444
1445 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1446 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1447
1448 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1449 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1450 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1451
1452 /* preallocation can change ac_b_ex, thus we store actually
1453 * allocated blocks for history */
1454 ac->ac_f_ex = ac->ac_b_ex;
1455
1456 ac->ac_status = AC_STATUS_FOUND;
1457 ac->ac_tail = ret & 0xffff;
1458 ac->ac_buddy = ret >> 16;
1459
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001460 /*
1461 * take the page reference. We want the page to be pinned
1462 * so that we don't get a ext4_mb_init_cache_call for this
1463 * group until we update the bitmap. That would mean we
1464 * double allocate blocks. The reference is dropped
1465 * in ext4_mb_release_context
1466 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001467 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1468 get_page(ac->ac_bitmap_page);
1469 ac->ac_buddy_page = e4b->bd_buddy_page;
1470 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001471 /* on allocation we use ac to track the held semaphore */
1472 ac->alloc_semp = e4b->alloc_semp;
1473 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001474 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001475 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001476 spin_lock(&sbi->s_md_lock);
1477 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1478 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1479 spin_unlock(&sbi->s_md_lock);
1480 }
1481}
1482
1483/*
1484 * regular allocator, for general purposes allocation
1485 */
1486
1487static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1488 struct ext4_buddy *e4b,
1489 int finish_group)
1490{
1491 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1492 struct ext4_free_extent *bex = &ac->ac_b_ex;
1493 struct ext4_free_extent *gex = &ac->ac_g_ex;
1494 struct ext4_free_extent ex;
1495 int max;
1496
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001497 if (ac->ac_status == AC_STATUS_FOUND)
1498 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001499 /*
1500 * We don't want to scan for a whole year
1501 */
1502 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1503 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1504 ac->ac_status = AC_STATUS_BREAK;
1505 return;
1506 }
1507
1508 /*
1509 * Haven't found good chunk so far, let's continue
1510 */
1511 if (bex->fe_len < gex->fe_len)
1512 return;
1513
1514 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1515 && bex->fe_group == e4b->bd_group) {
1516 /* recheck chunk's availability - we don't know
1517 * when it was found (within this lock-unlock
1518 * period or not) */
1519 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1520 if (max >= gex->fe_len) {
1521 ext4_mb_use_best_found(ac, e4b);
1522 return;
1523 }
1524 }
1525}
1526
1527/*
1528 * The routine checks whether found extent is good enough. If it is,
1529 * then the extent gets marked used and flag is set to the context
1530 * to stop scanning. Otherwise, the extent is compared with the
1531 * previous found extent and if new one is better, then it's stored
1532 * in the context. Later, the best found extent will be used, if
1533 * mballoc can't find good enough extent.
1534 *
1535 * FIXME: real allocation policy is to be designed yet!
1536 */
1537static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1538 struct ext4_free_extent *ex,
1539 struct ext4_buddy *e4b)
1540{
1541 struct ext4_free_extent *bex = &ac->ac_b_ex;
1542 struct ext4_free_extent *gex = &ac->ac_g_ex;
1543
1544 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001545 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001546 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1547 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1548
1549 ac->ac_found++;
1550
1551 /*
1552 * The special case - take what you catch first
1553 */
1554 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1555 *bex = *ex;
1556 ext4_mb_use_best_found(ac, e4b);
1557 return;
1558 }
1559
1560 /*
1561 * Let's check whether the chuck is good enough
1562 */
1563 if (ex->fe_len == gex->fe_len) {
1564 *bex = *ex;
1565 ext4_mb_use_best_found(ac, e4b);
1566 return;
1567 }
1568
1569 /*
1570 * If this is first found extent, just store it in the context
1571 */
1572 if (bex->fe_len == 0) {
1573 *bex = *ex;
1574 return;
1575 }
1576
1577 /*
1578 * If new found extent is better, store it in the context
1579 */
1580 if (bex->fe_len < gex->fe_len) {
1581 /* if the request isn't satisfied, any found extent
1582 * larger than previous best one is better */
1583 if (ex->fe_len > bex->fe_len)
1584 *bex = *ex;
1585 } else if (ex->fe_len > gex->fe_len) {
1586 /* if the request is satisfied, then we try to find
1587 * an extent that still satisfy the request, but is
1588 * smaller than previous one */
1589 if (ex->fe_len < bex->fe_len)
1590 *bex = *ex;
1591 }
1592
1593 ext4_mb_check_limits(ac, e4b, 0);
1594}
1595
Eric Sandeen089ceec2009-07-05 22:17:31 -04001596static noinline_for_stack
1597int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001598 struct ext4_buddy *e4b)
1599{
1600 struct ext4_free_extent ex = ac->ac_b_ex;
1601 ext4_group_t group = ex.fe_group;
1602 int max;
1603 int err;
1604
1605 BUG_ON(ex.fe_len <= 0);
1606 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1607 if (err)
1608 return err;
1609
1610 ext4_lock_group(ac->ac_sb, group);
1611 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1612
1613 if (max > 0) {
1614 ac->ac_b_ex = ex;
1615 ext4_mb_use_best_found(ac, e4b);
1616 }
1617
1618 ext4_unlock_group(ac->ac_sb, group);
1619 ext4_mb_release_desc(e4b);
1620
1621 return 0;
1622}
1623
Eric Sandeen089ceec2009-07-05 22:17:31 -04001624static noinline_for_stack
1625int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001626 struct ext4_buddy *e4b)
1627{
1628 ext4_group_t group = ac->ac_g_ex.fe_group;
1629 int max;
1630 int err;
1631 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001632 struct ext4_free_extent ex;
1633
1634 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1635 return 0;
1636
1637 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1638 if (err)
1639 return err;
1640
1641 ext4_lock_group(ac->ac_sb, group);
1642 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1643 ac->ac_g_ex.fe_len, &ex);
1644
1645 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1646 ext4_fsblk_t start;
1647
Akinobu Mita5661bd62010-03-03 23:53:39 -05001648 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1649 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001650 /* use do_div to get remainder (would be 64-bit modulo) */
1651 if (do_div(start, sbi->s_stripe) == 0) {
1652 ac->ac_found++;
1653 ac->ac_b_ex = ex;
1654 ext4_mb_use_best_found(ac, e4b);
1655 }
1656 } else if (max >= ac->ac_g_ex.fe_len) {
1657 BUG_ON(ex.fe_len <= 0);
1658 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1659 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1660 ac->ac_found++;
1661 ac->ac_b_ex = ex;
1662 ext4_mb_use_best_found(ac, e4b);
1663 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1664 /* Sometimes, caller may want to merge even small
1665 * number of blocks to an existing extent */
1666 BUG_ON(ex.fe_len <= 0);
1667 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1668 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1669 ac->ac_found++;
1670 ac->ac_b_ex = ex;
1671 ext4_mb_use_best_found(ac, e4b);
1672 }
1673 ext4_unlock_group(ac->ac_sb, group);
1674 ext4_mb_release_desc(e4b);
1675
1676 return 0;
1677}
1678
1679/*
1680 * The routine scans buddy structures (not bitmap!) from given order
1681 * to max order and tries to find big enough chunk to satisfy the req
1682 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001683static noinline_for_stack
1684void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001685 struct ext4_buddy *e4b)
1686{
1687 struct super_block *sb = ac->ac_sb;
1688 struct ext4_group_info *grp = e4b->bd_info;
1689 void *buddy;
1690 int i;
1691 int k;
1692 int max;
1693
1694 BUG_ON(ac->ac_2order <= 0);
1695 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1696 if (grp->bb_counters[i] == 0)
1697 continue;
1698
1699 buddy = mb_find_buddy(e4b, i, &max);
1700 BUG_ON(buddy == NULL);
1701
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001702 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001703 BUG_ON(k >= max);
1704
1705 ac->ac_found++;
1706
1707 ac->ac_b_ex.fe_len = 1 << i;
1708 ac->ac_b_ex.fe_start = k << i;
1709 ac->ac_b_ex.fe_group = e4b->bd_group;
1710
1711 ext4_mb_use_best_found(ac, e4b);
1712
1713 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1714
1715 if (EXT4_SB(sb)->s_mb_stats)
1716 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1717
1718 break;
1719 }
1720}
1721
1722/*
1723 * The routine scans the group and measures all found extents.
1724 * In order to optimize scanning, caller must pass number of
1725 * free blocks in the group, so the routine can know upper limit.
1726 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001727static noinline_for_stack
1728void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001729 struct ext4_buddy *e4b)
1730{
1731 struct super_block *sb = ac->ac_sb;
1732 void *bitmap = EXT4_MB_BITMAP(e4b);
1733 struct ext4_free_extent ex;
1734 int i;
1735 int free;
1736
1737 free = e4b->bd_info->bb_free;
1738 BUG_ON(free <= 0);
1739
1740 i = e4b->bd_info->bb_first_free;
1741
1742 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001743 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001744 EXT4_BLOCKS_PER_GROUP(sb), i);
1745 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001746 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001747 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001748 * free blocks even though group info says we
1749 * we have free blocks
1750 */
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001751 ext4_grp_locked_error(sb, e4b->bd_group,
1752 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001753 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001754 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001755 break;
1756 }
1757
1758 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1759 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001760 if (free < ex.fe_len) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001761 ext4_grp_locked_error(sb, e4b->bd_group,
1762 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001763 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001764 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001765 /*
1766 * The number of free blocks differs. This mostly
1767 * indicate that the bitmap is corrupt. So exit
1768 * without claiming the space.
1769 */
1770 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001771 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001772
1773 ext4_mb_measure_extent(ac, &ex, e4b);
1774
1775 i += ex.fe_len;
1776 free -= ex.fe_len;
1777 }
1778
1779 ext4_mb_check_limits(ac, e4b, 1);
1780}
1781
1782/*
1783 * This is a special case for storages like raid5
1784 * we try to find stripe-aligned chunks for stripe-size requests
1785 * XXX should do so at least for multiples of stripe size as well
1786 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001787static noinline_for_stack
1788void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001789 struct ext4_buddy *e4b)
1790{
1791 struct super_block *sb = ac->ac_sb;
1792 struct ext4_sb_info *sbi = EXT4_SB(sb);
1793 void *bitmap = EXT4_MB_BITMAP(e4b);
1794 struct ext4_free_extent ex;
1795 ext4_fsblk_t first_group_block;
1796 ext4_fsblk_t a;
1797 ext4_grpblk_t i;
1798 int max;
1799
1800 BUG_ON(sbi->s_stripe == 0);
1801
1802 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001803 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1804
Alex Tomasc9de5602008-01-29 00:19:52 -05001805 a = first_group_block + sbi->s_stripe - 1;
1806 do_div(a, sbi->s_stripe);
1807 i = (a * sbi->s_stripe) - first_group_block;
1808
1809 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1810 if (!mb_test_bit(i, bitmap)) {
1811 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1812 if (max >= sbi->s_stripe) {
1813 ac->ac_found++;
1814 ac->ac_b_ex = ex;
1815 ext4_mb_use_best_found(ac, e4b);
1816 break;
1817 }
1818 }
1819 i += sbi->s_stripe;
1820 }
1821}
1822
1823static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1824 ext4_group_t group, int cr)
1825{
1826 unsigned free, fragments;
1827 unsigned i, bits;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001828 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001829 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1830
1831 BUG_ON(cr < 0 || cr >= 4);
1832 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1833
1834 free = grp->bb_free;
1835 fragments = grp->bb_fragments;
1836 if (free == 0)
1837 return 0;
1838 if (fragments == 0)
1839 return 0;
1840
1841 switch (cr) {
1842 case 0:
1843 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001844
Theodore Ts'oa4912122009-03-12 12:18:34 -04001845 /* Avoid using the first bg of a flexgroup for data files */
1846 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1847 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1848 ((group % flex_size) == 0))
1849 return 0;
1850
Alex Tomasc9de5602008-01-29 00:19:52 -05001851 bits = ac->ac_sb->s_blocksize_bits + 1;
1852 for (i = ac->ac_2order; i <= bits; i++)
1853 if (grp->bb_counters[i] > 0)
1854 return 1;
1855 break;
1856 case 1:
1857 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1858 return 1;
1859 break;
1860 case 2:
1861 if (free >= ac->ac_g_ex.fe_len)
1862 return 1;
1863 break;
1864 case 3:
1865 return 1;
1866 default:
1867 BUG();
1868 }
1869
1870 return 0;
1871}
1872
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001873/*
1874 * lock the group_info alloc_sem of all the groups
1875 * belonging to the same buddy cache page. This
1876 * make sure other parallel operation on the buddy
1877 * cache doesn't happen whild holding the buddy cache
1878 * lock
1879 */
1880int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1881{
1882 int i;
1883 int block, pnum;
1884 int blocks_per_page;
1885 int groups_per_page;
Theodore Ts'o8df96752009-05-01 08:50:38 -04001886 ext4_group_t ngroups = ext4_get_groups_count(sb);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001887 ext4_group_t first_group;
1888 struct ext4_group_info *grp;
1889
1890 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1891 /*
1892 * the buddy cache inode stores the block bitmap
1893 * and buddy information in consecutive blocks.
1894 * So for each group we need two blocks.
1895 */
1896 block = group * 2;
1897 pnum = block / blocks_per_page;
1898 first_group = pnum * blocks_per_page / 2;
1899
1900 groups_per_page = blocks_per_page >> 1;
1901 if (groups_per_page == 0)
1902 groups_per_page = 1;
1903 /* read all groups the page covers into the cache */
1904 for (i = 0; i < groups_per_page; i++) {
1905
Theodore Ts'o8df96752009-05-01 08:50:38 -04001906 if ((first_group + i) >= ngroups)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001907 break;
1908 grp = ext4_get_group_info(sb, first_group + i);
1909 /* take all groups write allocation
1910 * semaphore. This make sure there is
1911 * no block allocation going on in any
1912 * of that groups
1913 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001914 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001915 }
1916 return i;
1917}
1918
1919void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1920 ext4_group_t group, int locked_group)
1921{
1922 int i;
1923 int block, pnum;
1924 int blocks_per_page;
1925 ext4_group_t first_group;
1926 struct ext4_group_info *grp;
1927
1928 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1929 /*
1930 * the buddy cache inode stores the block bitmap
1931 * and buddy information in consecutive blocks.
1932 * So for each group we need two blocks.
1933 */
1934 block = group * 2;
1935 pnum = block / blocks_per_page;
1936 first_group = pnum * blocks_per_page / 2;
1937 /* release locks on all the groups */
1938 for (i = 0; i < locked_group; i++) {
1939
1940 grp = ext4_get_group_info(sb, first_group + i);
1941 /* take all groups write allocation
1942 * semaphore. This make sure there is
1943 * no block allocation going on in any
1944 * of that groups
1945 */
1946 up_write(&grp->alloc_sem);
1947 }
1948
1949}
1950
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001951static noinline_for_stack int
1952ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001953{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001954 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001955 int cr;
1956 int err = 0;
1957 int bsbits;
1958 struct ext4_sb_info *sbi;
1959 struct super_block *sb;
1960 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001961
1962 sb = ac->ac_sb;
1963 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001964 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001965 /* non-extent files are limited to low blocks/groups */
1966 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
1967 ngroups = sbi->s_blockfile_groups;
1968
Alex Tomasc9de5602008-01-29 00:19:52 -05001969 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1970
1971 /* first, try the goal */
1972 err = ext4_mb_find_by_goal(ac, &e4b);
1973 if (err || ac->ac_status == AC_STATUS_FOUND)
1974 goto out;
1975
1976 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1977 goto out;
1978
1979 /*
1980 * ac->ac2_order is set only if the fe_len is a power of 2
1981 * if ac2_order is set we also set criteria to 0 so that we
1982 * try exact allocation using buddy.
1983 */
1984 i = fls(ac->ac_g_ex.fe_len);
1985 ac->ac_2order = 0;
1986 /*
1987 * We search using buddy data only if the order of the request
1988 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001989 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001990 */
1991 if (i >= sbi->s_mb_order2_reqs) {
1992 /*
1993 * This should tell if fe_len is exactly power of 2
1994 */
1995 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1996 ac->ac_2order = i - 1;
1997 }
1998
1999 bsbits = ac->ac_sb->s_blocksize_bits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002000
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002001 /* if stream allocation is enabled, use global goal */
2002 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002003 /* TBD: may be hot point */
2004 spin_lock(&sbi->s_md_lock);
2005 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2006 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2007 spin_unlock(&sbi->s_md_lock);
2008 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002009
Alex Tomasc9de5602008-01-29 00:19:52 -05002010 /* Let's just scan groups to find more-less suitable blocks */
2011 cr = ac->ac_2order ? 0 : 1;
2012 /*
2013 * cr == 0 try to get exact allocation,
2014 * cr == 3 try to get anything
2015 */
2016repeat:
2017 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2018 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002019 /*
2020 * searching for the right group start
2021 * from the goal value specified
2022 */
2023 group = ac->ac_g_ex.fe_group;
2024
Theodore Ts'o8df96752009-05-01 08:50:38 -04002025 for (i = 0; i < ngroups; group++, i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002026 struct ext4_group_info *grp;
2027 struct ext4_group_desc *desc;
2028
Theodore Ts'o8df96752009-05-01 08:50:38 -04002029 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002030 group = 0;
2031
2032 /* quick check to skip empty groups */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002033 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002034 if (grp->bb_free == 0)
2035 continue;
2036
Alex Tomasc9de5602008-01-29 00:19:52 -05002037 err = ext4_mb_load_buddy(sb, group, &e4b);
2038 if (err)
2039 goto out;
2040
2041 ext4_lock_group(sb, group);
2042 if (!ext4_mb_good_group(ac, group, cr)) {
2043 /* someone did allocation from this group */
2044 ext4_unlock_group(sb, group);
2045 ext4_mb_release_desc(&e4b);
2046 continue;
2047 }
2048
2049 ac->ac_groups_scanned++;
2050 desc = ext4_get_group_desc(sb, group, NULL);
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002051 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002052 ext4_mb_simple_scan_group(ac, &e4b);
2053 else if (cr == 1 &&
2054 ac->ac_g_ex.fe_len == sbi->s_stripe)
2055 ext4_mb_scan_aligned(ac, &e4b);
2056 else
2057 ext4_mb_complex_scan_group(ac, &e4b);
2058
2059 ext4_unlock_group(sb, group);
2060 ext4_mb_release_desc(&e4b);
2061
2062 if (ac->ac_status != AC_STATUS_CONTINUE)
2063 break;
2064 }
2065 }
2066
2067 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2068 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2069 /*
2070 * We've been searching too long. Let's try to allocate
2071 * the best chunk we've found so far
2072 */
2073
2074 ext4_mb_try_best_found(ac, &e4b);
2075 if (ac->ac_status != AC_STATUS_FOUND) {
2076 /*
2077 * Someone more lucky has already allocated it.
2078 * The only thing we can do is just take first
2079 * found block(s)
2080 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2081 */
2082 ac->ac_b_ex.fe_group = 0;
2083 ac->ac_b_ex.fe_start = 0;
2084 ac->ac_b_ex.fe_len = 0;
2085 ac->ac_status = AC_STATUS_CONTINUE;
2086 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2087 cr = 3;
2088 atomic_inc(&sbi->s_mb_lost_chunks);
2089 goto repeat;
2090 }
2091 }
2092out:
2093 return err;
2094}
2095
Alex Tomasc9de5602008-01-29 00:19:52 -05002096static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2097{
2098 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002099 ext4_group_t group;
2100
Theodore Ts'o8df96752009-05-01 08:50:38 -04002101 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002102 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002103 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002104 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002105}
2106
2107static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2108{
2109 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002110 ext4_group_t group;
2111
2112 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002113 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002114 return NULL;
2115 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002116 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002117}
2118
2119static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2120{
2121 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002122 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002123 int i;
2124 int err;
2125 struct ext4_buddy e4b;
2126 struct sg {
2127 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002128 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002129 } sg;
2130
2131 group--;
2132 if (group == 0)
2133 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2134 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2135 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2136 "group", "free", "frags", "first",
2137 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2138 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2139
2140 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2141 sizeof(struct ext4_group_info);
2142 err = ext4_mb_load_buddy(sb, group, &e4b);
2143 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002144 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002145 return 0;
2146 }
2147 ext4_lock_group(sb, group);
2148 memcpy(&sg, ext4_get_group_info(sb, group), i);
2149 ext4_unlock_group(sb, group);
2150 ext4_mb_release_desc(&e4b);
2151
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002152 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002153 sg.info.bb_fragments, sg.info.bb_first_free);
2154 for (i = 0; i <= 13; i++)
2155 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2156 sg.info.bb_counters[i] : 0);
2157 seq_printf(seq, " ]\n");
2158
2159 return 0;
2160}
2161
2162static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2163{
2164}
2165
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002166static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002167 .start = ext4_mb_seq_groups_start,
2168 .next = ext4_mb_seq_groups_next,
2169 .stop = ext4_mb_seq_groups_stop,
2170 .show = ext4_mb_seq_groups_show,
2171};
2172
2173static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2174{
2175 struct super_block *sb = PDE(inode)->data;
2176 int rc;
2177
2178 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2179 if (rc == 0) {
2180 struct seq_file *m = (struct seq_file *)file->private_data;
2181 m->private = sb;
2182 }
2183 return rc;
2184
2185}
2186
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002187static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002188 .owner = THIS_MODULE,
2189 .open = ext4_mb_seq_groups_open,
2190 .read = seq_read,
2191 .llseek = seq_lseek,
2192 .release = seq_release,
2193};
2194
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002195
2196/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002197int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002198 struct ext4_group_desc *desc)
2199{
2200 int i, len;
2201 int metalen = 0;
2202 struct ext4_sb_info *sbi = EXT4_SB(sb);
2203 struct ext4_group_info **meta_group_info;
2204
2205 /*
2206 * First check if this group is the first of a reserved block.
2207 * If it's true, we have to allocate a new table of pointers
2208 * to ext4_group_info structures
2209 */
2210 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2211 metalen = sizeof(*meta_group_info) <<
2212 EXT4_DESC_PER_BLOCK_BITS(sb);
2213 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2214 if (meta_group_info == NULL) {
2215 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2216 "buddy group\n");
2217 goto exit_meta_group_info;
2218 }
2219 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2220 meta_group_info;
2221 }
2222
2223 /*
2224 * calculate needed size. if change bb_counters size,
2225 * don't forget about ext4_mb_generate_buddy()
2226 */
2227 len = offsetof(typeof(**meta_group_info),
2228 bb_counters[sb->s_blocksize_bits + 2]);
2229
2230 meta_group_info =
2231 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2232 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2233
2234 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2235 if (meta_group_info[i] == NULL) {
2236 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2237 goto exit_group_info;
2238 }
2239 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2240 &(meta_group_info[i]->bb_state));
2241
2242 /*
2243 * initialize bb_free to be able to skip
2244 * empty groups without initialization
2245 */
2246 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2247 meta_group_info[i]->bb_free =
2248 ext4_free_blocks_after_init(sb, group, desc);
2249 } else {
2250 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002251 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002252 }
2253
2254 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002255 init_rwsem(&meta_group_info[i]->alloc_sem);
Joe Perches5a4a7982009-07-05 22:33:08 -04002256 meta_group_info[i]->bb_free_root.rb_node = NULL;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002257
2258#ifdef DOUBLE_CHECK
2259 {
2260 struct buffer_head *bh;
2261 meta_group_info[i]->bb_bitmap =
2262 kmalloc(sb->s_blocksize, GFP_KERNEL);
2263 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2264 bh = ext4_read_block_bitmap(sb, group);
2265 BUG_ON(bh == NULL);
2266 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2267 sb->s_blocksize);
2268 put_bh(bh);
2269 }
2270#endif
2271
2272 return 0;
2273
2274exit_group_info:
2275 /* If a meta_group_info table has been allocated, release it now */
2276 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2277 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2278exit_meta_group_info:
2279 return -ENOMEM;
2280} /* ext4_mb_add_groupinfo */
2281
Alex Tomasc9de5602008-01-29 00:19:52 -05002282static int ext4_mb_init_backend(struct super_block *sb)
2283{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002284 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002285 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002286 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002287 struct ext4_super_block *es = sbi->s_es;
2288 int num_meta_group_infos;
2289 int num_meta_group_infos_max;
2290 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002291 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002292
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002293 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002294 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002295 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2296
2297 /*
2298 * This is the total number of blocks used by GDT including
2299 * the number of reserved blocks for GDT.
2300 * The s_group_info array is allocated with this value
2301 * to allow a clean online resize without a complex
2302 * manipulation of pointer.
2303 * The drawback is the unused memory when no resize
2304 * occurs but it's very low in terms of pages
2305 * (see comments below)
2306 * Need to handle this properly when META_BG resizing is allowed
2307 */
2308 num_meta_group_infos_max = num_meta_group_infos +
2309 le16_to_cpu(es->s_reserved_gdt_blocks);
2310
2311 /*
2312 * array_size is the size of s_group_info array. We round it
2313 * to the next power of two because this approximation is done
2314 * internally by kmalloc so we can have some more memory
2315 * for free here (e.g. may be used for META_BG resize).
2316 */
2317 array_size = 1;
2318 while (array_size < sizeof(*sbi->s_group_info) *
2319 num_meta_group_infos_max)
2320 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002321 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2322 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2323 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002324 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002325 if (sbi->s_group_info == NULL) {
2326 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2327 return -ENOMEM;
2328 }
2329 sbi->s_buddy_cache = new_inode(sb);
2330 if (sbi->s_buddy_cache == NULL) {
2331 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2332 goto err_freesgi;
2333 }
2334 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002335 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002336 desc = ext4_get_group_desc(sb, i, NULL);
2337 if (desc == NULL) {
2338 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002339 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002340 goto err_freebuddy;
2341 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002342 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2343 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002344 }
2345
2346 return 0;
2347
2348err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002349 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002350 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002351 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002352 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002353 kfree(sbi->s_group_info[i]);
2354 iput(sbi->s_buddy_cache);
2355err_freesgi:
2356 kfree(sbi->s_group_info);
2357 return -ENOMEM;
2358}
2359
2360int ext4_mb_init(struct super_block *sb, int needs_recovery)
2361{
2362 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002363 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002364 unsigned offset;
2365 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002366 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002367
Eric Sandeen19278052009-08-25 22:36:25 -04002368 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002369
2370 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2371 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002372 return -ENOMEM;
2373 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002374
Eric Sandeen19278052009-08-25 22:36:25 -04002375 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002376 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2377 if (sbi->s_mb_maxs == NULL) {
Dan Carpentera7b19442009-03-27 19:42:54 -04002378 kfree(sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002379 return -ENOMEM;
2380 }
2381
2382 /* order 0 is regular bitmap */
2383 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2384 sbi->s_mb_offsets[0] = 0;
2385
2386 i = 1;
2387 offset = 0;
2388 max = sb->s_blocksize << 2;
2389 do {
2390 sbi->s_mb_offsets[i] = offset;
2391 sbi->s_mb_maxs[i] = max;
2392 offset += 1 << (sb->s_blocksize_bits - i);
2393 max = max >> 1;
2394 i++;
2395 } while (i <= sb->s_blocksize_bits + 1);
2396
2397 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002398 ret = ext4_mb_init_backend(sb);
2399 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002400 kfree(sbi->s_mb_offsets);
2401 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002402 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002403 }
2404
2405 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002406 spin_lock_init(&sbi->s_bal_lock);
2407
2408 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2409 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2410 sbi->s_mb_stats = MB_DEFAULT_STATS;
2411 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2412 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002413 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2414
Eric Sandeen730c2132008-09-13 15:23:29 -04002415 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002416 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002417 kfree(sbi->s_mb_offsets);
2418 kfree(sbi->s_mb_maxs);
2419 return -ENOMEM;
2420 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002421 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002422 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002423 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002424 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002425 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2426 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002427 spin_lock_init(&lg->lg_prealloc_lock);
2428 }
2429
Theodore Ts'o296c3552009-09-30 00:32:42 -04002430 if (sbi->s_proc)
2431 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2432 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002433
Frank Mayhar03901312009-01-07 00:06:22 -05002434 if (sbi->s_journal)
2435 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Alex Tomasc9de5602008-01-29 00:19:52 -05002436 return 0;
2437}
2438
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002439/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002440static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2441{
2442 struct ext4_prealloc_space *pa;
2443 struct list_head *cur, *tmp;
2444 int count = 0;
2445
2446 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2447 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2448 list_del(&pa->pa_group_list);
2449 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002450 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002451 }
2452 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002453 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002454
2455}
2456
2457int ext4_mb_release(struct super_block *sb)
2458{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002459 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002460 ext4_group_t i;
2461 int num_meta_group_infos;
2462 struct ext4_group_info *grinfo;
2463 struct ext4_sb_info *sbi = EXT4_SB(sb);
2464
Alex Tomasc9de5602008-01-29 00:19:52 -05002465 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002466 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002467 grinfo = ext4_get_group_info(sb, i);
2468#ifdef DOUBLE_CHECK
2469 kfree(grinfo->bb_bitmap);
2470#endif
2471 ext4_lock_group(sb, i);
2472 ext4_mb_cleanup_pa(grinfo);
2473 ext4_unlock_group(sb, i);
2474 kfree(grinfo);
2475 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002476 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002477 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2478 EXT4_DESC_PER_BLOCK_BITS(sb);
2479 for (i = 0; i < num_meta_group_infos; i++)
2480 kfree(sbi->s_group_info[i]);
2481 kfree(sbi->s_group_info);
2482 }
2483 kfree(sbi->s_mb_offsets);
2484 kfree(sbi->s_mb_maxs);
2485 if (sbi->s_buddy_cache)
2486 iput(sbi->s_buddy_cache);
2487 if (sbi->s_mb_stats) {
2488 printk(KERN_INFO
2489 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2490 atomic_read(&sbi->s_bal_allocated),
2491 atomic_read(&sbi->s_bal_reqs),
2492 atomic_read(&sbi->s_bal_success));
2493 printk(KERN_INFO
2494 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2495 "%u 2^N hits, %u breaks, %u lost\n",
2496 atomic_read(&sbi->s_bal_ex_scanned),
2497 atomic_read(&sbi->s_bal_goals),
2498 atomic_read(&sbi->s_bal_2orders),
2499 atomic_read(&sbi->s_bal_breaks),
2500 atomic_read(&sbi->s_mb_lost_chunks));
2501 printk(KERN_INFO
2502 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2503 sbi->s_mb_buddies_generated++,
2504 sbi->s_mb_generation_time);
2505 printk(KERN_INFO
2506 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2507 atomic_read(&sbi->s_mb_preallocated),
2508 atomic_read(&sbi->s_mb_discarded));
2509 }
2510
Eric Sandeen730c2132008-09-13 15:23:29 -04002511 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002512 if (sbi->s_proc)
2513 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002514
2515 return 0;
2516}
2517
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002518/*
2519 * This function is called by the jbd2 layer once the commit has finished,
2520 * so we know we can free the blocks that were released with that commit.
2521 */
2522static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002523{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002524 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002525 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002526 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002527 int err, count = 0, count2 = 0;
2528 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002529 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002530
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002531 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2532 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002533
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002534 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002535 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002536
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002537 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002538 /* we expect to find existing buddy because it's pinned */
2539 BUG_ON(err != 0);
2540
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002541 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002542 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002543 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002544 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002545 ext4_lock_group(sb, entry->group);
2546 /* Take it out of per group rb tree */
2547 rb_erase(&entry->node, &(db->bb_free_root));
2548 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2549
2550 if (!db->bb_free_root.rb_node) {
2551 /* No more items in the per group rb tree
2552 * balance refcounts from ext4_mb_free_metadata()
2553 */
2554 page_cache_release(e4b.bd_buddy_page);
2555 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002556 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002557 ext4_unlock_group(sb, entry->group);
Eric Sandeen5328e632009-11-19 14:25:42 -05002558 if (test_opt(sb, DISCARD)) {
2559 ext4_fsblk_t discard_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05002560
Akinobu Mita5661bd62010-03-03 23:53:39 -05002561 discard_block = entry->start_blk +
2562 ext4_group_first_block_no(sb, entry->group);
Eric Sandeen5328e632009-11-19 14:25:42 -05002563 trace_ext4_discard_blocks(sb,
2564 (unsigned long long)discard_block,
2565 entry->count);
2566 sb_issue_discard(sb, discard_block, entry->count);
2567 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002568 kmem_cache_free(ext4_free_ext_cachep, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002569 ext4_mb_release_desc(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002570 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002571
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002572 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002573}
2574
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002575#ifdef CONFIG_EXT4_DEBUG
2576u8 mb_enable_debug __read_mostly;
2577
2578static struct dentry *debugfs_dir;
2579static struct dentry *debugfs_debug;
2580
2581static void __init ext4_create_debugfs_entry(void)
2582{
2583 debugfs_dir = debugfs_create_dir("ext4", NULL);
2584 if (debugfs_dir)
2585 debugfs_debug = debugfs_create_u8("mballoc-debug",
2586 S_IRUGO | S_IWUSR,
2587 debugfs_dir,
2588 &mb_enable_debug);
2589}
2590
2591static void ext4_remove_debugfs_entry(void)
2592{
2593 debugfs_remove(debugfs_debug);
2594 debugfs_remove(debugfs_dir);
2595}
2596
2597#else
2598
2599static void __init ext4_create_debugfs_entry(void)
2600{
2601}
2602
2603static void ext4_remove_debugfs_entry(void)
2604{
2605}
2606
2607#endif
2608
Alex Tomasc9de5602008-01-29 00:19:52 -05002609int __init init_ext4_mballoc(void)
2610{
2611 ext4_pspace_cachep =
2612 kmem_cache_create("ext4_prealloc_space",
2613 sizeof(struct ext4_prealloc_space),
2614 0, SLAB_RECLAIM_ACCOUNT, NULL);
2615 if (ext4_pspace_cachep == NULL)
2616 return -ENOMEM;
2617
Eric Sandeen256bdb42008-02-10 01:13:33 -05002618 ext4_ac_cachep =
2619 kmem_cache_create("ext4_alloc_context",
2620 sizeof(struct ext4_allocation_context),
2621 0, SLAB_RECLAIM_ACCOUNT, NULL);
2622 if (ext4_ac_cachep == NULL) {
2623 kmem_cache_destroy(ext4_pspace_cachep);
2624 return -ENOMEM;
2625 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002626
2627 ext4_free_ext_cachep =
2628 kmem_cache_create("ext4_free_block_extents",
2629 sizeof(struct ext4_free_data),
2630 0, SLAB_RECLAIM_ACCOUNT, NULL);
2631 if (ext4_free_ext_cachep == NULL) {
2632 kmem_cache_destroy(ext4_pspace_cachep);
2633 kmem_cache_destroy(ext4_ac_cachep);
2634 return -ENOMEM;
2635 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002636 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002637 return 0;
2638}
2639
2640void exit_ext4_mballoc(void)
2641{
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002642 /*
2643 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2644 * before destroying the slab cache.
2645 */
2646 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002647 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002648 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002649 kmem_cache_destroy(ext4_free_ext_cachep);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002650 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002651}
2652
2653
2654/*
2655 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2656 * Returns 0 if success or error code
2657 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002658static noinline_for_stack int
2659ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002660 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002661{
2662 struct buffer_head *bitmap_bh = NULL;
2663 struct ext4_super_block *es;
2664 struct ext4_group_desc *gdp;
2665 struct buffer_head *gdp_bh;
2666 struct ext4_sb_info *sbi;
2667 struct super_block *sb;
2668 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002669 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002670
2671 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2672 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2673
2674 sb = ac->ac_sb;
2675 sbi = EXT4_SB(sb);
2676 es = sbi->s_es;
2677
Alex Tomasc9de5602008-01-29 00:19:52 -05002678
2679 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002680 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002681 if (!bitmap_bh)
2682 goto out_err;
2683
2684 err = ext4_journal_get_write_access(handle, bitmap_bh);
2685 if (err)
2686 goto out_err;
2687
2688 err = -EIO;
2689 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2690 if (!gdp)
2691 goto out_err;
2692
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002693 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002694 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002695
Alex Tomasc9de5602008-01-29 00:19:52 -05002696 err = ext4_journal_get_write_access(handle, gdp_bh);
2697 if (err)
2698 goto out_err;
2699
2700 block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
2701 + ac->ac_b_ex.fe_start
2702 + le32_to_cpu(es->s_first_data_block);
2703
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002704 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002705 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002706 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002707 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002708 /* File system mounted not to panic on error
2709 * Fix the bitmap and repeat the block allocation
2710 * We leak some of the blocks here.
2711 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002712 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2713 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2714 ac->ac_b_ex.fe_len);
2715 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002716 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002717 if (!err)
2718 err = -EAGAIN;
2719 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002720 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002721
2722 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002723#ifdef AGGRESSIVE_CHECK
2724 {
2725 int i;
2726 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2727 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2728 bitmap_bh->b_data));
2729 }
2730 }
2731#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002732 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002733 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2734 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002735 ext4_free_blks_set(sb, gdp,
2736 ext4_free_blocks_after_init(sb,
2737 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002738 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002739 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2740 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002741 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002742
2743 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002744 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002745 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002746 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002747 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002748 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2749 /* release all the reserved blocks if non delalloc */
2750 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002751
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002752 if (sbi->s_log_groups_per_flex) {
2753 ext4_group_t flex_group = ext4_flex_group(sbi,
2754 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002755 atomic_sub(ac->ac_b_ex.fe_len,
2756 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002757 }
2758
Frank Mayhar03901312009-01-07 00:06:22 -05002759 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002760 if (err)
2761 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002762 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002763
2764out_err:
2765 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002766 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002767 return err;
2768}
2769
2770/*
2771 * here we normalize request for locality group
2772 * Group request are normalized to s_strip size if we set the same via mount
2773 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002774 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002775 *
2776 * XXX: should we try to preallocate more than the group has now?
2777 */
2778static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2779{
2780 struct super_block *sb = ac->ac_sb;
2781 struct ext4_locality_group *lg = ac->ac_lg;
2782
2783 BUG_ON(lg == NULL);
2784 if (EXT4_SB(sb)->s_stripe)
2785 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2786 else
2787 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002788 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002789 current->pid, ac->ac_g_ex.fe_len);
2790}
2791
2792/*
2793 * Normalization means making request better in terms of
2794 * size and alignment
2795 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002796static noinline_for_stack void
2797ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002798 struct ext4_allocation_request *ar)
2799{
2800 int bsbits, max;
2801 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002802 loff_t size, orig_size, start_off;
2803 ext4_lblk_t start, orig_start;
2804 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002805 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002806
2807 /* do normalize only data requests, metadata requests
2808 do not need preallocation */
2809 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2810 return;
2811
2812 /* sometime caller may want exact blocks */
2813 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2814 return;
2815
2816 /* caller may indicate that preallocation isn't
2817 * required (it's a tail, for example) */
2818 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2819 return;
2820
2821 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2822 ext4_mb_normalize_group_request(ac);
2823 return ;
2824 }
2825
2826 bsbits = ac->ac_sb->s_blocksize_bits;
2827
2828 /* first, let's learn actual file size
2829 * given current request is allocated */
2830 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2831 size = size << bsbits;
2832 if (size < i_size_read(ac->ac_inode))
2833 size = i_size_read(ac->ac_inode);
2834
Valerie Clement19304792008-05-13 19:31:14 -04002835 /* max size of free chunks */
2836 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002837
Valerie Clement19304792008-05-13 19:31:14 -04002838#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2839 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002840
2841 /* first, try to predict filesize */
2842 /* XXX: should this table be tunable? */
2843 start_off = 0;
2844 if (size <= 16 * 1024) {
2845 size = 16 * 1024;
2846 } else if (size <= 32 * 1024) {
2847 size = 32 * 1024;
2848 } else if (size <= 64 * 1024) {
2849 size = 64 * 1024;
2850 } else if (size <= 128 * 1024) {
2851 size = 128 * 1024;
2852 } else if (size <= 256 * 1024) {
2853 size = 256 * 1024;
2854 } else if (size <= 512 * 1024) {
2855 size = 512 * 1024;
2856 } else if (size <= 1024 * 1024) {
2857 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002858 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002859 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002860 (21 - bsbits)) << 21;
2861 size = 2 * 1024 * 1024;
2862 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002863 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2864 (22 - bsbits)) << 22;
2865 size = 4 * 1024 * 1024;
2866 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002867 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002868 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2869 (23 - bsbits)) << 23;
2870 size = 8 * 1024 * 1024;
2871 } else {
2872 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2873 size = ac->ac_o_ex.fe_len << bsbits;
2874 }
2875 orig_size = size = size >> bsbits;
2876 orig_start = start = start_off >> bsbits;
2877
2878 /* don't cover already allocated blocks in selected range */
2879 if (ar->pleft && start <= ar->lleft) {
2880 size -= ar->lleft + 1 - start;
2881 start = ar->lleft + 1;
2882 }
2883 if (ar->pright && start + size - 1 >= ar->lright)
2884 size -= start + size - ar->lright;
2885
2886 end = start + size;
2887
2888 /* check we don't cross already preallocated blocks */
2889 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002890 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002891 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002892
Alex Tomasc9de5602008-01-29 00:19:52 -05002893 if (pa->pa_deleted)
2894 continue;
2895 spin_lock(&pa->pa_lock);
2896 if (pa->pa_deleted) {
2897 spin_unlock(&pa->pa_lock);
2898 continue;
2899 }
2900
2901 pa_end = pa->pa_lstart + pa->pa_len;
2902
2903 /* PA must not overlap original request */
2904 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2905 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2906
Eric Sandeen38877f42009-08-17 23:55:24 -04002907 /* skip PAs this normalized request doesn't overlap with */
2908 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002909 spin_unlock(&pa->pa_lock);
2910 continue;
2911 }
2912 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2913
Eric Sandeen38877f42009-08-17 23:55:24 -04002914 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002915 if (pa_end <= ac->ac_o_ex.fe_logical) {
2916 BUG_ON(pa_end < start);
2917 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002918 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002919 BUG_ON(pa->pa_lstart > end);
2920 end = pa->pa_lstart;
2921 }
2922 spin_unlock(&pa->pa_lock);
2923 }
2924 rcu_read_unlock();
2925 size = end - start;
2926
2927 /* XXX: extra loop to check we really don't overlap preallocations */
2928 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002929 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002930 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002931 spin_lock(&pa->pa_lock);
2932 if (pa->pa_deleted == 0) {
2933 pa_end = pa->pa_lstart + pa->pa_len;
2934 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2935 }
2936 spin_unlock(&pa->pa_lock);
2937 }
2938 rcu_read_unlock();
2939
2940 if (start + size <= ac->ac_o_ex.fe_logical &&
2941 start > ac->ac_o_ex.fe_logical) {
2942 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
2943 (unsigned long) start, (unsigned long) size,
2944 (unsigned long) ac->ac_o_ex.fe_logical);
2945 }
2946 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
2947 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04002948 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002949
2950 /* now prepare goal request */
2951
2952 /* XXX: is it better to align blocks WRT to logical
2953 * placement or satisfy big request as is */
2954 ac->ac_g_ex.fe_logical = start;
2955 ac->ac_g_ex.fe_len = size;
2956
2957 /* define goal start in order to merge */
2958 if (ar->pright && (ar->lright == (start + size))) {
2959 /* merge to the right */
2960 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
2961 &ac->ac_f_ex.fe_group,
2962 &ac->ac_f_ex.fe_start);
2963 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2964 }
2965 if (ar->pleft && (ar->lleft + 1 == start)) {
2966 /* merge to the left */
2967 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
2968 &ac->ac_f_ex.fe_group,
2969 &ac->ac_f_ex.fe_start);
2970 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2971 }
2972
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002973 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05002974 (unsigned) orig_size, (unsigned) start);
2975}
2976
2977static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
2978{
2979 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2980
2981 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
2982 atomic_inc(&sbi->s_bal_reqs);
2983 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
2984 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
2985 atomic_inc(&sbi->s_bal_success);
2986 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
2987 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2988 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2989 atomic_inc(&sbi->s_bal_goals);
2990 if (ac->ac_found > sbi->s_mb_max_to_scan)
2991 atomic_inc(&sbi->s_bal_breaks);
2992 }
2993
Theodore Ts'o296c3552009-09-30 00:32:42 -04002994 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
2995 trace_ext4_mballoc_alloc(ac);
2996 else
2997 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05002998}
2999
3000/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003001 * Called on failure; free up any blocks from the inode PA for this
3002 * context. We don't need this for MB_GROUP_PA because we only change
3003 * pa_free in ext4_mb_release_context(), but on failure, we've already
3004 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3005 */
3006static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3007{
3008 struct ext4_prealloc_space *pa = ac->ac_pa;
3009 int len;
3010
3011 if (pa && pa->pa_type == MB_INODE_PA) {
3012 len = ac->ac_b_ex.fe_len;
3013 pa->pa_free += len;
3014 }
3015
3016}
3017
3018/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 * use blocks preallocated to inode
3020 */
3021static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3022 struct ext4_prealloc_space *pa)
3023{
3024 ext4_fsblk_t start;
3025 ext4_fsblk_t end;
3026 int len;
3027
3028 /* found preallocated blocks, use them */
3029 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3030 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3031 len = end - start;
3032 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3033 &ac->ac_b_ex.fe_start);
3034 ac->ac_b_ex.fe_len = len;
3035 ac->ac_status = AC_STATUS_FOUND;
3036 ac->ac_pa = pa;
3037
3038 BUG_ON(start < pa->pa_pstart);
3039 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3040 BUG_ON(pa->pa_free < len);
3041 pa->pa_free -= len;
3042
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003043 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003044}
3045
3046/*
3047 * use blocks preallocated to locality group
3048 */
3049static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3050 struct ext4_prealloc_space *pa)
3051{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003052 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003053
Alex Tomasc9de5602008-01-29 00:19:52 -05003054 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3055 &ac->ac_b_ex.fe_group,
3056 &ac->ac_b_ex.fe_start);
3057 ac->ac_b_ex.fe_len = len;
3058 ac->ac_status = AC_STATUS_FOUND;
3059 ac->ac_pa = pa;
3060
3061 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003062 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003063 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003064 * in on-disk bitmap -- see ext4_mb_release_context()
3065 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003066 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003067 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003068}
3069
3070/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003071 * Return the prealloc space that have minimal distance
3072 * from the goal block. @cpa is the prealloc
3073 * space that is having currently known minimal distance
3074 * from the goal block.
3075 */
3076static struct ext4_prealloc_space *
3077ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3078 struct ext4_prealloc_space *pa,
3079 struct ext4_prealloc_space *cpa)
3080{
3081 ext4_fsblk_t cur_distance, new_distance;
3082
3083 if (cpa == NULL) {
3084 atomic_inc(&pa->pa_count);
3085 return pa;
3086 }
3087 cur_distance = abs(goal_block - cpa->pa_pstart);
3088 new_distance = abs(goal_block - pa->pa_pstart);
3089
3090 if (cur_distance < new_distance)
3091 return cpa;
3092
3093 /* drop the previous reference */
3094 atomic_dec(&cpa->pa_count);
3095 atomic_inc(&pa->pa_count);
3096 return pa;
3097}
3098
3099/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003100 * search goal blocks in preallocated space
3101 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003102static noinline_for_stack int
3103ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003104{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003105 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003106 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3107 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003108 struct ext4_prealloc_space *pa, *cpa = NULL;
3109 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003110
3111 /* only data can be preallocated */
3112 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3113 return 0;
3114
3115 /* first, try per-file preallocation */
3116 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003117 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003118
3119 /* all fields in this condition don't change,
3120 * so we can skip locking for them */
3121 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3122 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3123 continue;
3124
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003125 /* non-extent files can't have physical blocks past 2^32 */
3126 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
3127 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3128 continue;
3129
Alex Tomasc9de5602008-01-29 00:19:52 -05003130 /* found preallocated blocks, use them */
3131 spin_lock(&pa->pa_lock);
3132 if (pa->pa_deleted == 0 && pa->pa_free) {
3133 atomic_inc(&pa->pa_count);
3134 ext4_mb_use_inode_pa(ac, pa);
3135 spin_unlock(&pa->pa_lock);
3136 ac->ac_criteria = 10;
3137 rcu_read_unlock();
3138 return 1;
3139 }
3140 spin_unlock(&pa->pa_lock);
3141 }
3142 rcu_read_unlock();
3143
3144 /* can we use group allocation? */
3145 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3146 return 0;
3147
3148 /* inode may have no locality group for some reason */
3149 lg = ac->ac_lg;
3150 if (lg == NULL)
3151 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003152 order = fls(ac->ac_o_ex.fe_len) - 1;
3153 if (order > PREALLOC_TB_SIZE - 1)
3154 /* The max size of hash table is PREALLOC_TB_SIZE */
3155 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003156
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003157 goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
3158 ac->ac_g_ex.fe_start +
3159 le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
3160 /*
3161 * search for the prealloc space that is having
3162 * minimal distance from the goal block.
3163 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003164 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3165 rcu_read_lock();
3166 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3167 pa_inode_list) {
3168 spin_lock(&pa->pa_lock);
3169 if (pa->pa_deleted == 0 &&
3170 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003171
3172 cpa = ext4_mb_check_group_pa(goal_block,
3173 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003174 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003175 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003176 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003177 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003178 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003179 if (cpa) {
3180 ext4_mb_use_group_pa(ac, cpa);
3181 ac->ac_criteria = 20;
3182 return 1;
3183 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003184 return 0;
3185}
3186
3187/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003188 * the function goes through all block freed in the group
3189 * but not yet committed and marks them used in in-core bitmap.
3190 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003191 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003192 */
3193static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3194 ext4_group_t group)
3195{
3196 struct rb_node *n;
3197 struct ext4_group_info *grp;
3198 struct ext4_free_data *entry;
3199
3200 grp = ext4_get_group_info(sb, group);
3201 n = rb_first(&(grp->bb_free_root));
3202
3203 while (n) {
3204 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003205 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003206 n = rb_next(n);
3207 }
3208 return;
3209}
3210
3211/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003212 * the function goes through all preallocation in this group and marks them
3213 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003214 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003215 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003216static noinline_for_stack
3217void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003218 ext4_group_t group)
3219{
3220 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3221 struct ext4_prealloc_space *pa;
3222 struct list_head *cur;
3223 ext4_group_t groupnr;
3224 ext4_grpblk_t start;
3225 int preallocated = 0;
3226 int count = 0;
3227 int len;
3228
3229 /* all form of preallocation discards first load group,
3230 * so the only competing code is preallocation use.
3231 * we don't need any locking here
3232 * notice we do NOT ignore preallocations with pa_deleted
3233 * otherwise we could leave used blocks available for
3234 * allocation in buddy when concurrent ext4_mb_put_pa()
3235 * is dropping preallocation
3236 */
3237 list_for_each(cur, &grp->bb_prealloc_list) {
3238 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3239 spin_lock(&pa->pa_lock);
3240 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3241 &groupnr, &start);
3242 len = pa->pa_len;
3243 spin_unlock(&pa->pa_lock);
3244 if (unlikely(len == 0))
3245 continue;
3246 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003247 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003248 preallocated += len;
3249 count++;
3250 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003251 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003252}
3253
3254static void ext4_mb_pa_callback(struct rcu_head *head)
3255{
3256 struct ext4_prealloc_space *pa;
3257 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3258 kmem_cache_free(ext4_pspace_cachep, pa);
3259}
3260
3261/*
3262 * drops a reference to preallocated space descriptor
3263 * if this was the last reference and the space is consumed
3264 */
3265static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3266 struct super_block *sb, struct ext4_prealloc_space *pa)
3267{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003268 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003269 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003270
3271 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3272 return;
3273
3274 /* in this short window concurrent discard can set pa_deleted */
3275 spin_lock(&pa->pa_lock);
3276 if (pa->pa_deleted == 1) {
3277 spin_unlock(&pa->pa_lock);
3278 return;
3279 }
3280
3281 pa->pa_deleted = 1;
3282 spin_unlock(&pa->pa_lock);
3283
Eric Sandeend33a1972009-03-16 23:25:40 -04003284 grp_blk = pa->pa_pstart;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003285 /*
3286 * If doing group-based preallocation, pa_pstart may be in the
3287 * next group when pa is used up
3288 */
3289 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003290 grp_blk--;
3291
3292 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003293
3294 /*
3295 * possible race:
3296 *
3297 * P1 (buddy init) P2 (regular allocation)
3298 * find block B in PA
3299 * copy on-disk bitmap to buddy
3300 * mark B in on-disk bitmap
3301 * drop PA from group
3302 * mark all PAs in buddy
3303 *
3304 * thus, P1 initializes buddy with B available. to prevent this
3305 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3306 * against that pair
3307 */
3308 ext4_lock_group(sb, grp);
3309 list_del(&pa->pa_group_list);
3310 ext4_unlock_group(sb, grp);
3311
3312 spin_lock(pa->pa_obj_lock);
3313 list_del_rcu(&pa->pa_inode_list);
3314 spin_unlock(pa->pa_obj_lock);
3315
3316 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3317}
3318
3319/*
3320 * creates new preallocated space for given inode
3321 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003322static noinline_for_stack int
3323ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003324{
3325 struct super_block *sb = ac->ac_sb;
3326 struct ext4_prealloc_space *pa;
3327 struct ext4_group_info *grp;
3328 struct ext4_inode_info *ei;
3329
3330 /* preallocate only when found space is larger then requested */
3331 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3332 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3333 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3334
3335 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3336 if (pa == NULL)
3337 return -ENOMEM;
3338
3339 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3340 int winl;
3341 int wins;
3342 int win;
3343 int offs;
3344
3345 /* we can't allocate as much as normalizer wants.
3346 * so, found space must get proper lstart
3347 * to cover original request */
3348 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3349 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3350
3351 /* we're limited by original request in that
3352 * logical block must be covered any way
3353 * winl is window we can move our chunk within */
3354 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3355
3356 /* also, we should cover whole original request */
3357 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3358
3359 /* the smallest one defines real window */
3360 win = min(winl, wins);
3361
3362 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3363 if (offs && offs < win)
3364 win = offs;
3365
3366 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3367 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3368 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3369 }
3370
3371 /* preallocation can change ac_b_ex, thus we store actually
3372 * allocated blocks for history */
3373 ac->ac_f_ex = ac->ac_b_ex;
3374
3375 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3376 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3377 pa->pa_len = ac->ac_b_ex.fe_len;
3378 pa->pa_free = pa->pa_len;
3379 atomic_set(&pa->pa_count, 1);
3380 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003381 INIT_LIST_HEAD(&pa->pa_inode_list);
3382 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003383 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003384 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003385
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003386 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003387 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003388 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003389
3390 ext4_mb_use_inode_pa(ac, pa);
3391 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3392
3393 ei = EXT4_I(ac->ac_inode);
3394 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3395
3396 pa->pa_obj_lock = &ei->i_prealloc_lock;
3397 pa->pa_inode = ac->ac_inode;
3398
3399 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3400 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3401 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3402
3403 spin_lock(pa->pa_obj_lock);
3404 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3405 spin_unlock(pa->pa_obj_lock);
3406
3407 return 0;
3408}
3409
3410/*
3411 * creates new preallocated space for locality group inodes belongs to
3412 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003413static noinline_for_stack int
3414ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003415{
3416 struct super_block *sb = ac->ac_sb;
3417 struct ext4_locality_group *lg;
3418 struct ext4_prealloc_space *pa;
3419 struct ext4_group_info *grp;
3420
3421 /* preallocate only when found space is larger then requested */
3422 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3423 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3424 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3425
3426 BUG_ON(ext4_pspace_cachep == NULL);
3427 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3428 if (pa == NULL)
3429 return -ENOMEM;
3430
3431 /* preallocation can change ac_b_ex, thus we store actually
3432 * allocated blocks for history */
3433 ac->ac_f_ex = ac->ac_b_ex;
3434
3435 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3436 pa->pa_lstart = pa->pa_pstart;
3437 pa->pa_len = ac->ac_b_ex.fe_len;
3438 pa->pa_free = pa->pa_len;
3439 atomic_set(&pa->pa_count, 1);
3440 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003441 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003442 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003443 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003444 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003445
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003446 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003447 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3448 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003449
3450 ext4_mb_use_group_pa(ac, pa);
3451 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3452
3453 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3454 lg = ac->ac_lg;
3455 BUG_ON(lg == NULL);
3456
3457 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3458 pa->pa_inode = NULL;
3459
3460 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3461 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3462 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3463
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003464 /*
3465 * We will later add the new pa to the right bucket
3466 * after updating the pa_free in ext4_mb_release_context
3467 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003468 return 0;
3469}
3470
3471static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3472{
3473 int err;
3474
3475 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3476 err = ext4_mb_new_group_pa(ac);
3477 else
3478 err = ext4_mb_new_inode_pa(ac);
3479 return err;
3480}
3481
3482/*
3483 * finds all unused blocks in on-disk bitmap, frees them in
3484 * in-core bitmap and buddy.
3485 * @pa must be unlinked from inode and group lists, so that
3486 * nobody else can find/use it.
3487 * the caller MUST hold group/inode locks.
3488 * TODO: optimize the case when there are no in-core structures yet
3489 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003490static noinline_for_stack int
3491ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003492 struct ext4_prealloc_space *pa,
3493 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003494{
Alex Tomasc9de5602008-01-29 00:19:52 -05003495 struct super_block *sb = e4b->bd_sb;
3496 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003497 unsigned int end;
3498 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003499 ext4_group_t group;
3500 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003501 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003502 sector_t start;
3503 int err = 0;
3504 int free = 0;
3505
3506 BUG_ON(pa->pa_deleted == 0);
3507 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003508 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003509 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3510 end = bit + pa->pa_len;
3511
Eric Sandeen256bdb42008-02-10 01:13:33 -05003512 if (ac) {
3513 ac->ac_sb = sb;
3514 ac->ac_inode = pa->pa_inode;
Eric Sandeen256bdb42008-02-10 01:13:33 -05003515 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003516
3517 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003518 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003519 if (bit >= end)
3520 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003521 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Akinobu Mita5661bd62010-03-03 23:53:39 -05003522 start = ext4_group_first_block_no(sb, group) + bit;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003523 mb_debug(1, " free preallocated %u/%u in group %u\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003524 (unsigned) start, (unsigned) next - bit,
3525 (unsigned) group);
3526 free += next - bit;
3527
Eric Sandeen256bdb42008-02-10 01:13:33 -05003528 if (ac) {
3529 ac->ac_b_ex.fe_group = group;
3530 ac->ac_b_ex.fe_start = bit;
3531 ac->ac_b_ex.fe_len = next - bit;
3532 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003533 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003534 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003535
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003536 trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3537 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003538 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3539 bit = next + 1;
3540 }
3541 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003542 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003543 pa, (unsigned long) pa->pa_lstart,
3544 (unsigned long) pa->pa_pstart,
3545 (unsigned long) pa->pa_len);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003546 ext4_grp_locked_error(sb, group,
3547 __func__, "free %u, pa_free %u",
3548 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003549 /*
3550 * pa is already deleted so we use the value obtained
3551 * from the bitmap and continue.
3552 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003553 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003554 atomic_add(free, &sbi->s_mb_discarded);
3555
3556 return err;
3557}
3558
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003559static noinline_for_stack int
3560ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003561 struct ext4_prealloc_space *pa,
3562 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003563{
Alex Tomasc9de5602008-01-29 00:19:52 -05003564 struct super_block *sb = e4b->bd_sb;
3565 ext4_group_t group;
3566 ext4_grpblk_t bit;
3567
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003568 trace_ext4_mb_release_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003569 BUG_ON(pa->pa_deleted == 0);
3570 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3571 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3572 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3573 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3574
Eric Sandeen256bdb42008-02-10 01:13:33 -05003575 if (ac) {
3576 ac->ac_sb = sb;
3577 ac->ac_inode = NULL;
3578 ac->ac_b_ex.fe_group = group;
3579 ac->ac_b_ex.fe_start = bit;
3580 ac->ac_b_ex.fe_len = pa->pa_len;
3581 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003582 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003583 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003584
3585 return 0;
3586}
3587
3588/*
3589 * releases all preallocations in given group
3590 *
3591 * first, we need to decide discard policy:
3592 * - when do we discard
3593 * 1) ENOSPC
3594 * - how many do we discard
3595 * 1) how many requested
3596 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003597static noinline_for_stack int
3598ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003599 ext4_group_t group, int needed)
3600{
3601 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3602 struct buffer_head *bitmap_bh = NULL;
3603 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003604 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003605 struct list_head list;
3606 struct ext4_buddy e4b;
3607 int err;
3608 int busy = 0;
3609 int free = 0;
3610
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003611 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003612
3613 if (list_empty(&grp->bb_prealloc_list))
3614 return 0;
3615
Theodore Ts'o574ca172008-07-11 19:27:31 -04003616 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003617 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003618 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003619 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003620 }
3621
3622 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003623 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003624 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003625 put_bh(bitmap_bh);
3626 return 0;
3627 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003628
3629 if (needed == 0)
3630 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3631
Alex Tomasc9de5602008-01-29 00:19:52 -05003632 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003633 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003634 if (ac)
3635 ac->ac_sb = sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05003636repeat:
3637 ext4_lock_group(sb, group);
3638 list_for_each_entry_safe(pa, tmp,
3639 &grp->bb_prealloc_list, pa_group_list) {
3640 spin_lock(&pa->pa_lock);
3641 if (atomic_read(&pa->pa_count)) {
3642 spin_unlock(&pa->pa_lock);
3643 busy = 1;
3644 continue;
3645 }
3646 if (pa->pa_deleted) {
3647 spin_unlock(&pa->pa_lock);
3648 continue;
3649 }
3650
3651 /* seems this one can be freed ... */
3652 pa->pa_deleted = 1;
3653
3654 /* we can trust pa_free ... */
3655 free += pa->pa_free;
3656
3657 spin_unlock(&pa->pa_lock);
3658
3659 list_del(&pa->pa_group_list);
3660 list_add(&pa->u.pa_tmp_list, &list);
3661 }
3662
3663 /* if we still need more blocks and some PAs were used, try again */
3664 if (free < needed && busy) {
3665 busy = 0;
3666 ext4_unlock_group(sb, group);
3667 /*
3668 * Yield the CPU here so that we don't get soft lockup
3669 * in non preempt case.
3670 */
3671 yield();
3672 goto repeat;
3673 }
3674
3675 /* found anything to free? */
3676 if (list_empty(&list)) {
3677 BUG_ON(free != 0);
3678 goto out;
3679 }
3680
3681 /* now free all selected PAs */
3682 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3683
3684 /* remove from object (inode or locality group) */
3685 spin_lock(pa->pa_obj_lock);
3686 list_del_rcu(&pa->pa_inode_list);
3687 spin_unlock(pa->pa_obj_lock);
3688
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003689 if (pa->pa_type == MB_GROUP_PA)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003690 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003691 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003692 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003693
3694 list_del(&pa->u.pa_tmp_list);
3695 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3696 }
3697
3698out:
3699 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003700 if (ac)
3701 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003702 ext4_mb_release_desc(&e4b);
3703 put_bh(bitmap_bh);
3704 return free;
3705}
3706
3707/*
3708 * releases all non-used preallocated blocks for given inode
3709 *
3710 * It's important to discard preallocations under i_data_sem
3711 * We don't want another block to be served from the prealloc
3712 * space when we are discarding the inode prealloc space.
3713 *
3714 * FIXME!! Make sure it is valid at all the call sites
3715 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003716void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003717{
3718 struct ext4_inode_info *ei = EXT4_I(inode);
3719 struct super_block *sb = inode->i_sb;
3720 struct buffer_head *bitmap_bh = NULL;
3721 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003722 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003723 ext4_group_t group = 0;
3724 struct list_head list;
3725 struct ext4_buddy e4b;
3726 int err;
3727
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003728 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003729 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3730 return;
3731 }
3732
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003733 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003734 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003735
3736 INIT_LIST_HEAD(&list);
3737
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003738 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003739 if (ac) {
3740 ac->ac_sb = sb;
3741 ac->ac_inode = inode;
3742 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003743repeat:
3744 /* first, collect all pa's in the inode */
3745 spin_lock(&ei->i_prealloc_lock);
3746 while (!list_empty(&ei->i_prealloc_list)) {
3747 pa = list_entry(ei->i_prealloc_list.next,
3748 struct ext4_prealloc_space, pa_inode_list);
3749 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3750 spin_lock(&pa->pa_lock);
3751 if (atomic_read(&pa->pa_count)) {
3752 /* this shouldn't happen often - nobody should
3753 * use preallocation while we're discarding it */
3754 spin_unlock(&pa->pa_lock);
3755 spin_unlock(&ei->i_prealloc_lock);
3756 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3757 WARN_ON(1);
3758 schedule_timeout_uninterruptible(HZ);
3759 goto repeat;
3760
3761 }
3762 if (pa->pa_deleted == 0) {
3763 pa->pa_deleted = 1;
3764 spin_unlock(&pa->pa_lock);
3765 list_del_rcu(&pa->pa_inode_list);
3766 list_add(&pa->u.pa_tmp_list, &list);
3767 continue;
3768 }
3769
3770 /* someone is deleting pa right now */
3771 spin_unlock(&pa->pa_lock);
3772 spin_unlock(&ei->i_prealloc_lock);
3773
3774 /* we have to wait here because pa_deleted
3775 * doesn't mean pa is already unlinked from
3776 * the list. as we might be called from
3777 * ->clear_inode() the inode will get freed
3778 * and concurrent thread which is unlinking
3779 * pa from inode's list may access already
3780 * freed memory, bad-bad-bad */
3781
3782 /* XXX: if this happens too often, we can
3783 * add a flag to force wait only in case
3784 * of ->clear_inode(), but not in case of
3785 * regular truncate */
3786 schedule_timeout_uninterruptible(HZ);
3787 goto repeat;
3788 }
3789 spin_unlock(&ei->i_prealloc_lock);
3790
3791 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003792 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003793 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3794
3795 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003796 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003797 ext4_error(sb, "Error loading buddy information for %u",
3798 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003799 continue;
3800 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003801
Theodore Ts'o574ca172008-07-11 19:27:31 -04003802 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003803 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003804 ext4_error(sb, "Error reading block bitmap for %u",
3805 group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003806 ext4_mb_release_desc(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003807 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003808 }
3809
3810 ext4_lock_group(sb, group);
3811 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003812 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003813 ext4_unlock_group(sb, group);
3814
3815 ext4_mb_release_desc(&e4b);
3816 put_bh(bitmap_bh);
3817
3818 list_del(&pa->u.pa_tmp_list);
3819 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3820 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003821 if (ac)
3822 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003823}
3824
3825/*
3826 * finds all preallocated spaces and return blocks being freed to them
3827 * if preallocated space becomes full (no block is used from the space)
3828 * then the function frees space in buddy
3829 * XXX: at the moment, truncate (which is the only way to free blocks)
3830 * discards all preallocations
3831 */
3832static void ext4_mb_return_to_preallocation(struct inode *inode,
3833 struct ext4_buddy *e4b,
3834 sector_t block, int count)
3835{
3836 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3837}
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003838#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003839static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3840{
3841 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003842 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003843
3844 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3845 " Allocation context details:\n");
3846 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3847 ac->ac_status, ac->ac_flags);
3848 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3849 "best %lu/%lu/%lu@%lu cr %d\n",
3850 (unsigned long)ac->ac_o_ex.fe_group,
3851 (unsigned long)ac->ac_o_ex.fe_start,
3852 (unsigned long)ac->ac_o_ex.fe_len,
3853 (unsigned long)ac->ac_o_ex.fe_logical,
3854 (unsigned long)ac->ac_g_ex.fe_group,
3855 (unsigned long)ac->ac_g_ex.fe_start,
3856 (unsigned long)ac->ac_g_ex.fe_len,
3857 (unsigned long)ac->ac_g_ex.fe_logical,
3858 (unsigned long)ac->ac_b_ex.fe_group,
3859 (unsigned long)ac->ac_b_ex.fe_start,
3860 (unsigned long)ac->ac_b_ex.fe_len,
3861 (unsigned long)ac->ac_b_ex.fe_logical,
3862 (int)ac->ac_criteria);
3863 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3864 ac->ac_found);
3865 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003866 ngroups = ext4_get_groups_count(sb);
3867 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003868 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3869 struct ext4_prealloc_space *pa;
3870 ext4_grpblk_t start;
3871 struct list_head *cur;
3872 ext4_lock_group(sb, i);
3873 list_for_each(cur, &grp->bb_prealloc_list) {
3874 pa = list_entry(cur, struct ext4_prealloc_space,
3875 pa_group_list);
3876 spin_lock(&pa->pa_lock);
3877 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3878 NULL, &start);
3879 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003880 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3881 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003882 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003883 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003884
3885 if (grp->bb_free == 0)
3886 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003887 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003888 i, grp->bb_free, grp->bb_fragments);
3889 }
3890 printk(KERN_ERR "\n");
3891}
3892#else
3893static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3894{
3895 return;
3896}
3897#endif
3898
3899/*
3900 * We use locality group preallocation for small size file. The size of the
3901 * file is determined by the current size or the resulting size after
3902 * allocation which ever is larger
3903 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003904 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003905 */
3906static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3907{
3908 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3909 int bsbits = ac->ac_sb->s_blocksize_bits;
3910 loff_t size, isize;
3911
3912 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3913 return;
3914
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003915 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3916 return;
3917
Alex Tomasc9de5602008-01-29 00:19:52 -05003918 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003919 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3920 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003921
Theodore Ts'o50797482009-09-18 13:34:02 -04003922 if ((size == isize) &&
3923 !ext4_fs_is_busy(sbi) &&
3924 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3925 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3926 return;
3927 }
3928
Alex Tomasc9de5602008-01-29 00:19:52 -05003929 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003930 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003931 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003932 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003933 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003934 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003935
3936 BUG_ON(ac->ac_lg != NULL);
3937 /*
3938 * locality group prealloc space are per cpu. The reason for having
3939 * per cpu locality group is to reduce the contention between block
3940 * request from multiple CPUs.
3941 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003942 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003943
3944 /* we're going to use group allocation */
3945 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3946
3947 /* serialize all allocations in the group */
3948 mutex_lock(&ac->ac_lg->lg_mutex);
3949}
3950
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003951static noinline_for_stack int
3952ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003953 struct ext4_allocation_request *ar)
3954{
3955 struct super_block *sb = ar->inode->i_sb;
3956 struct ext4_sb_info *sbi = EXT4_SB(sb);
3957 struct ext4_super_block *es = sbi->s_es;
3958 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003959 unsigned int len;
3960 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003961 ext4_grpblk_t block;
3962
3963 /* we can't allocate > group size */
3964 len = ar->len;
3965
3966 /* just a dirty hack to filter too big requests */
3967 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3968 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3969
3970 /* start searching from the goal */
3971 goal = ar->goal;
3972 if (goal < le32_to_cpu(es->s_first_data_block) ||
3973 goal >= ext4_blocks_count(es))
3974 goal = le32_to_cpu(es->s_first_data_block);
3975 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3976
3977 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04003978 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05003979 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05003980 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05003981 ac->ac_sb = sb;
3982 ac->ac_inode = ar->inode;
3983 ac->ac_o_ex.fe_logical = ar->logical;
3984 ac->ac_o_ex.fe_group = group;
3985 ac->ac_o_ex.fe_start = block;
3986 ac->ac_o_ex.fe_len = len;
3987 ac->ac_g_ex.fe_logical = ar->logical;
3988 ac->ac_g_ex.fe_group = group;
3989 ac->ac_g_ex.fe_start = block;
3990 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003991 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05003992
3993 /* we have to define context: we'll we work with a file or
3994 * locality group. this is a policy, actually */
3995 ext4_mb_group_or_file(ac);
3996
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003997 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05003998 "left: %u/%u, right %u/%u to %swritable\n",
3999 (unsigned) ar->len, (unsigned) ar->logical,
4000 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4001 (unsigned) ar->lleft, (unsigned) ar->pleft,
4002 (unsigned) ar->lright, (unsigned) ar->pright,
4003 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4004 return 0;
4005
4006}
4007
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004008static noinline_for_stack void
4009ext4_mb_discard_lg_preallocations(struct super_block *sb,
4010 struct ext4_locality_group *lg,
4011 int order, int total_entries)
4012{
4013 ext4_group_t group = 0;
4014 struct ext4_buddy e4b;
4015 struct list_head discard_list;
4016 struct ext4_prealloc_space *pa, *tmp;
4017 struct ext4_allocation_context *ac;
4018
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004019 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004020
4021 INIT_LIST_HEAD(&discard_list);
4022 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004023 if (ac)
4024 ac->ac_sb = sb;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004025
4026 spin_lock(&lg->lg_prealloc_lock);
4027 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4028 pa_inode_list) {
4029 spin_lock(&pa->pa_lock);
4030 if (atomic_read(&pa->pa_count)) {
4031 /*
4032 * This is the pa that we just used
4033 * for block allocation. So don't
4034 * free that
4035 */
4036 spin_unlock(&pa->pa_lock);
4037 continue;
4038 }
4039 if (pa->pa_deleted) {
4040 spin_unlock(&pa->pa_lock);
4041 continue;
4042 }
4043 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004044 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004045
4046 /* seems this one can be freed ... */
4047 pa->pa_deleted = 1;
4048 spin_unlock(&pa->pa_lock);
4049
4050 list_del_rcu(&pa->pa_inode_list);
4051 list_add(&pa->u.pa_tmp_list, &discard_list);
4052
4053 total_entries--;
4054 if (total_entries <= 5) {
4055 /*
4056 * we want to keep only 5 entries
4057 * allowing it to grow to 8. This
4058 * mak sure we don't call discard
4059 * soon for this list.
4060 */
4061 break;
4062 }
4063 }
4064 spin_unlock(&lg->lg_prealloc_lock);
4065
4066 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4067
4068 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4069 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004070 ext4_error(sb, "Error loading buddy information for %u",
4071 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004072 continue;
4073 }
4074 ext4_lock_group(sb, group);
4075 list_del(&pa->pa_group_list);
4076 ext4_mb_release_group_pa(&e4b, pa, ac);
4077 ext4_unlock_group(sb, group);
4078
4079 ext4_mb_release_desc(&e4b);
4080 list_del(&pa->u.pa_tmp_list);
4081 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4082 }
4083 if (ac)
4084 kmem_cache_free(ext4_ac_cachep, ac);
4085}
4086
4087/*
4088 * We have incremented pa_count. So it cannot be freed at this
4089 * point. Also we hold lg_mutex. So no parallel allocation is
4090 * possible from this lg. That means pa_free cannot be updated.
4091 *
4092 * A parallel ext4_mb_discard_group_preallocations is possible.
4093 * which can cause the lg_prealloc_list to be updated.
4094 */
4095
4096static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4097{
4098 int order, added = 0, lg_prealloc_count = 1;
4099 struct super_block *sb = ac->ac_sb;
4100 struct ext4_locality_group *lg = ac->ac_lg;
4101 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4102
4103 order = fls(pa->pa_free) - 1;
4104 if (order > PREALLOC_TB_SIZE - 1)
4105 /* The max size of hash table is PREALLOC_TB_SIZE */
4106 order = PREALLOC_TB_SIZE - 1;
4107 /* Add the prealloc space to lg */
4108 rcu_read_lock();
4109 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4110 pa_inode_list) {
4111 spin_lock(&tmp_pa->pa_lock);
4112 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004113 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004114 continue;
4115 }
4116 if (!added && pa->pa_free < tmp_pa->pa_free) {
4117 /* Add to the tail of the previous entry */
4118 list_add_tail_rcu(&pa->pa_inode_list,
4119 &tmp_pa->pa_inode_list);
4120 added = 1;
4121 /*
4122 * we want to count the total
4123 * number of entries in the list
4124 */
4125 }
4126 spin_unlock(&tmp_pa->pa_lock);
4127 lg_prealloc_count++;
4128 }
4129 if (!added)
4130 list_add_tail_rcu(&pa->pa_inode_list,
4131 &lg->lg_prealloc_list[order]);
4132 rcu_read_unlock();
4133
4134 /* Now trim the list to be not more than 8 elements */
4135 if (lg_prealloc_count > 8) {
4136 ext4_mb_discard_lg_preallocations(sb, lg,
4137 order, lg_prealloc_count);
4138 return;
4139 }
4140 return ;
4141}
4142
Alex Tomasc9de5602008-01-29 00:19:52 -05004143/*
4144 * release all resource we used in allocation
4145 */
4146static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4147{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004148 struct ext4_prealloc_space *pa = ac->ac_pa;
4149 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004150 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004151 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004152 spin_lock(&pa->pa_lock);
4153 pa->pa_pstart += ac->ac_b_ex.fe_len;
4154 pa->pa_lstart += ac->ac_b_ex.fe_len;
4155 pa->pa_free -= ac->ac_b_ex.fe_len;
4156 pa->pa_len -= ac->ac_b_ex.fe_len;
4157 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004158 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004159 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004160 if (ac->alloc_semp)
4161 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004162 if (pa) {
4163 /*
4164 * We want to add the pa to the right bucket.
4165 * Remove it from the list and while adding
4166 * make sure the list to which we are adding
4167 * doesn't grow big. We need to release
4168 * alloc_semp before calling ext4_mb_add_n_trim()
4169 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004170 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004171 spin_lock(pa->pa_obj_lock);
4172 list_del_rcu(&pa->pa_inode_list);
4173 spin_unlock(pa->pa_obj_lock);
4174 ext4_mb_add_n_trim(ac);
4175 }
4176 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4177 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004178 if (ac->ac_bitmap_page)
4179 page_cache_release(ac->ac_bitmap_page);
4180 if (ac->ac_buddy_page)
4181 page_cache_release(ac->ac_buddy_page);
4182 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4183 mutex_unlock(&ac->ac_lg->lg_mutex);
4184 ext4_mb_collect_stats(ac);
4185 return 0;
4186}
4187
4188static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4189{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004190 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004191 int ret;
4192 int freed = 0;
4193
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004194 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004195 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004196 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4197 freed += ret;
4198 needed -= ret;
4199 }
4200
4201 return freed;
4202}
4203
4204/*
4205 * Main entry point into mballoc to allocate blocks
4206 * it tries to use preallocation first, then falls back
4207 * to usual allocation
4208 */
4209ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4210 struct ext4_allocation_request *ar, int *errp)
4211{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004212 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004213 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004214 struct ext4_sb_info *sbi;
4215 struct super_block *sb;
4216 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004217 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004218 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004219
4220 sb = ar->inode->i_sb;
4221 sbi = EXT4_SB(sb);
4222
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004223 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004224
Mingming Cao60e58e02009-01-22 18:13:05 +01004225 /*
4226 * For delayed allocation, we could skip the ENOSPC and
4227 * EDQUOT check, as blocks and quotas have been already
4228 * reserved when data being copied into pagecache.
4229 */
4230 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4231 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4232 else {
4233 /* Without delayed allocation we need to verify
4234 * there is enough free blocks to do block allocation
4235 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004236 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004237 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4238 /* let others to free the space */
4239 yield();
4240 ar->len = ar->len >> 1;
4241 }
4242 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004243 *errp = -ENOSPC;
4244 return 0;
4245 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004246 reserv_blks = ar->len;
Jan Karaa269eb12009-01-26 17:04:39 +01004247 while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004248 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4249 ar->len--;
4250 }
4251 inquota = ar->len;
4252 if (ar->len == 0) {
4253 *errp = -EDQUOT;
4254 goto out3;
4255 }
Mingming Caod2a17632008-07-14 17:52:37 -04004256 }
Mingming Caod2a17632008-07-14 17:52:37 -04004257
Eric Sandeen256bdb42008-02-10 01:13:33 -05004258 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004259 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004260 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004261 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004262 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004263 }
4264
Eric Sandeen256bdb42008-02-10 01:13:33 -05004265 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004266 if (*errp) {
4267 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004268 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004269 }
4270
Eric Sandeen256bdb42008-02-10 01:13:33 -05004271 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4272 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004273 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4274 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004275repeat:
4276 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004277 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004278
4279 /* as we've just preallocated more space than
4280 * user requested orinally, we store allocated
4281 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004282 if (ac->ac_status == AC_STATUS_FOUND &&
4283 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4284 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004285 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004286 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004287 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004288 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004289 /*
4290 * drop the reference that we took
4291 * in ext4_mb_use_best_found
4292 */
4293 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004294 ac->ac_b_ex.fe_group = 0;
4295 ac->ac_b_ex.fe_start = 0;
4296 ac->ac_b_ex.fe_len = 0;
4297 ac->ac_status = AC_STATUS_CONTINUE;
4298 goto repeat;
4299 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004300 ext4_discard_allocated_blocks(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004301 ac->ac_b_ex.fe_len = 0;
4302 ar->len = 0;
4303 ext4_mb_show_ac(ac);
4304 } else {
4305 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4306 ar->len = ac->ac_b_ex.fe_len;
4307 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004308 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004309 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004310 if (freed)
4311 goto repeat;
4312 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004313 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004314 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004315 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004316 }
4317
Eric Sandeen256bdb42008-02-10 01:13:33 -05004318 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004319
Shen Feng363d4252008-07-11 19:27:31 -04004320out2:
4321 kmem_cache_free(ext4_ac_cachep, ac);
4322out1:
Mingming Cao60e58e02009-01-22 18:13:05 +01004323 if (inquota && ar->len < inquota)
Jan Karaa269eb12009-01-26 17:04:39 +01004324 vfs_dq_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004325out3:
4326 if (!ar->len) {
4327 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4328 /* release all the reserved blocks if non delalloc */
4329 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4330 reserv_blks);
4331 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004332
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004333 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004334
Alex Tomasc9de5602008-01-29 00:19:52 -05004335 return block;
4336}
Alex Tomasc9de5602008-01-29 00:19:52 -05004337
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004338/*
4339 * We can merge two free data extents only if the physical blocks
4340 * are contiguous, AND the extents were freed by the same transaction,
4341 * AND the blocks are associated with the same group.
4342 */
4343static int can_merge(struct ext4_free_data *entry1,
4344 struct ext4_free_data *entry2)
4345{
4346 if ((entry1->t_tid == entry2->t_tid) &&
4347 (entry1->group == entry2->group) &&
4348 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4349 return 1;
4350 return 0;
4351}
4352
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004353static noinline_for_stack int
4354ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004355 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004356{
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004357 ext4_grpblk_t block;
4358 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004359 struct ext4_group_info *db = e4b->bd_info;
4360 struct super_block *sb = e4b->bd_sb;
4361 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004362 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4363 struct rb_node *parent = NULL, *new_node;
4364
Frank Mayhar03901312009-01-07 00:06:22 -05004365 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004366 BUG_ON(e4b->bd_bitmap_page == NULL);
4367 BUG_ON(e4b->bd_buddy_page == NULL);
4368
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004369 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004370 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004371
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004372 if (!*n) {
4373 /* first free block exent. We need to
4374 protect buddy cache from being freed,
4375 * otherwise we'll refresh it from
4376 * on-disk bitmap and lose not-yet-available
4377 * blocks */
4378 page_cache_get(e4b->bd_buddy_page);
4379 page_cache_get(e4b->bd_bitmap_page);
4380 }
4381 while (*n) {
4382 parent = *n;
4383 entry = rb_entry(parent, struct ext4_free_data, node);
4384 if (block < entry->start_blk)
4385 n = &(*n)->rb_left;
4386 else if (block >= (entry->start_blk + entry->count))
4387 n = &(*n)->rb_right;
4388 else {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05004389 ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4390 "Double free of blocks %d (%d %d)",
4391 block, entry->start_blk, entry->count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004392 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004393 }
4394 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004395
4396 rb_link_node(new_node, parent, n);
4397 rb_insert_color(new_node, &db->bb_free_root);
4398
4399 /* Now try to see the extent can be merged to left and right */
4400 node = rb_prev(new_node);
4401 if (node) {
4402 entry = rb_entry(node, struct ext4_free_data, node);
4403 if (can_merge(entry, new_entry)) {
4404 new_entry->start_blk = entry->start_blk;
4405 new_entry->count += entry->count;
4406 rb_erase(node, &(db->bb_free_root));
4407 spin_lock(&sbi->s_md_lock);
4408 list_del(&entry->list);
4409 spin_unlock(&sbi->s_md_lock);
4410 kmem_cache_free(ext4_free_ext_cachep, entry);
4411 }
4412 }
4413
4414 node = rb_next(new_node);
4415 if (node) {
4416 entry = rb_entry(node, struct ext4_free_data, node);
4417 if (can_merge(new_entry, entry)) {
4418 new_entry->count += entry->count;
4419 rb_erase(node, &(db->bb_free_root));
4420 spin_lock(&sbi->s_md_lock);
4421 list_del(&entry->list);
4422 spin_unlock(&sbi->s_md_lock);
4423 kmem_cache_free(ext4_free_ext_cachep, entry);
4424 }
4425 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004426 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004427 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004428 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004429 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004430 return 0;
4431}
4432
Theodore Ts'o44338712009-11-22 07:44:56 -05004433/**
4434 * ext4_free_blocks() -- Free given blocks and update quota
4435 * @handle: handle for this transaction
4436 * @inode: inode
4437 * @block: start physical block to free
4438 * @count: number of blocks to count
4439 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004440 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004441void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004442 struct buffer_head *bh, ext4_fsblk_t block,
4443 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004444{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004445 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004446 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004447 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004448 struct ext4_group_desc *gdp;
4449 struct ext4_super_block *es;
Theodore Ts'o44338712009-11-22 07:44:56 -05004450 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004451 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004452 ext4_grpblk_t bit;
4453 struct buffer_head *gd_bh;
4454 ext4_group_t block_group;
4455 struct ext4_sb_info *sbi;
4456 struct ext4_buddy e4b;
4457 int err = 0;
4458 int ret;
4459
Theodore Ts'oe6362602009-11-23 07:17:05 -05004460 if (bh) {
4461 if (block)
4462 BUG_ON(block != bh->b_blocknr);
4463 else
4464 block = bh->b_blocknr;
4465 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004466
Alex Tomasc9de5602008-01-29 00:19:52 -05004467 sbi = EXT4_SB(sb);
4468 es = EXT4_SB(sb)->s_es;
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004469 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4470 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004471 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004472 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004473 goto error_return;
4474 }
4475
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004476 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004477 trace_ext4_free_blocks(inode, block, count, flags);
4478
4479 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4480 struct buffer_head *tbh = bh;
4481 int i;
4482
4483 BUG_ON(bh && (count > 1));
4484
4485 for (i = 0; i < count; i++) {
4486 if (!bh)
4487 tbh = sb_find_get_block(inode->i_sb,
4488 block + i);
4489 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4490 inode, tbh, block + i);
4491 }
4492 }
4493
4494 /*
4495 * We need to make sure we don't reuse the freed block until
4496 * after the transaction is committed, which we can do by
4497 * treating the block as metadata, below. We make an
4498 * exception if the inode is to be written in writeback mode
4499 * since writeback mode has weak data consistency guarantees.
4500 */
4501 if (!ext4_should_writeback_data(inode))
4502 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004503
Eric Sandeen256bdb42008-02-10 01:13:33 -05004504 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4505 if (ac) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004506 ac->ac_inode = inode;
4507 ac->ac_sb = sb;
4508 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004509
4510do_more:
4511 overflow = 0;
4512 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4513
4514 /*
4515 * Check to see if we are freeing blocks across a group
4516 * boundary.
4517 */
4518 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4519 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4520 count -= overflow;
4521 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004522 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004523 if (!bitmap_bh) {
4524 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004525 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004526 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004527 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004528 if (!gdp) {
4529 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004530 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004531 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004532
4533 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4534 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4535 in_range(block, ext4_inode_table(sb, gdp),
4536 EXT4_SB(sb)->s_itb_per_group) ||
4537 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4538 EXT4_SB(sb)->s_itb_per_group)) {
4539
Eric Sandeen12062dd2010-02-15 14:19:27 -05004540 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004541 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004542 /* err = 0. ext4_std_error should be a no op */
4543 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004544 }
4545
4546 BUFFER_TRACE(bitmap_bh, "getting write access");
4547 err = ext4_journal_get_write_access(handle, bitmap_bh);
4548 if (err)
4549 goto error_return;
4550
4551 /*
4552 * We are about to modify some metadata. Call the journal APIs
4553 * to unshare ->b_data if a currently-committing transaction is
4554 * using it
4555 */
4556 BUFFER_TRACE(gd_bh, "get_write_access");
4557 err = ext4_journal_get_write_access(handle, gd_bh);
4558 if (err)
4559 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004560#ifdef AGGRESSIVE_CHECK
4561 {
4562 int i;
4563 for (i = 0; i < count; i++)
4564 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4565 }
4566#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004567 if (ac) {
4568 ac->ac_b_ex.fe_group = block_group;
4569 ac->ac_b_ex.fe_start = bit;
4570 ac->ac_b_ex.fe_len = count;
Theodore Ts'o296c3552009-09-30 00:32:42 -04004571 trace_ext4_mballoc_free(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004572 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004573
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004574 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4575 if (err)
4576 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004577
4578 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004579 struct ext4_free_data *new_entry;
4580 /*
4581 * blocks being freed are metadata. these blocks shouldn't
4582 * be used until this transaction is committed
4583 */
4584 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4585 new_entry->start_blk = bit;
4586 new_entry->group = block_group;
4587 new_entry->count = count;
4588 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004589
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004590 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004591 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004592 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004593 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004594 /* need to update group_info->bb_free and bitmap
4595 * with group lock held. generate_buddy look at
4596 * them with group lock_held
4597 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004598 ext4_lock_group(sb, block_group);
4599 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004600 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004601 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004602 }
4603
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004604 ret = ext4_free_blks_count(sb, gdp) + count;
4605 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004606 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004607 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004608 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4609
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004610 if (sbi->s_log_groups_per_flex) {
4611 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004612 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004613 }
4614
Alex Tomasc9de5602008-01-29 00:19:52 -05004615 ext4_mb_release_desc(&e4b);
4616
Theodore Ts'o44338712009-11-22 07:44:56 -05004617 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004618
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004619 /* We dirtied the bitmap block */
4620 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4621 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4622
Alex Tomasc9de5602008-01-29 00:19:52 -05004623 /* And the group descriptor block */
4624 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004625 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004626 if (!err)
4627 err = ret;
4628
4629 if (overflow && !err) {
4630 block += count;
4631 count = overflow;
4632 put_bh(bitmap_bh);
4633 goto do_more;
4634 }
4635 sb->s_dirt = 1;
4636error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004637 if (freed)
4638 vfs_dq_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004639 brelse(bitmap_bh);
4640 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004641 if (ac)
4642 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004643 return;
4644}