blob: 4f2d3a9d4e21c493eb2d8b142733f6e3159acbc1 [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 */
18
19
20/*
21 * mballoc.c contains the multiblocks allocation routines
22 */
23
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040025#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040027#include <trace/events/ext4.h>
28
Alex Tomasc9de5602008-01-29 00:19:52 -050029/*
30 * MUSTDO:
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
33 *
34 * TODO v4:
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
38 * - quota
39 * - reservation for superuser
40 *
41 * TODO v3:
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
46 * - error handling
47 */
48
49/*
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
52 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040053 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050062 *
63 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050065 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040066 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
69 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050070 *
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
Daniel Mack1537a362010-01-29 15:57:49 +080073 * pa_len -> length for this prealloc space
Alex Tomasc9de5602008-01-29 00:19:52 -050074 * pa_free -> free space available in this prealloc space
75 *
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
80 *
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
83 * pa_free.
84 *
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
87 * prealloc space. These are per CPU prealloc list repreasented as
88 *
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 *
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
93 *
94 * The locality group prealloc space is used looking at whether we have
95 * enough free space (pa_free) withing the prealloc space.
96 *
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
105 * inode as:
106 *
107 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500109 *
110 *
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
115 *
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
118 *
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
122 *
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500130 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
135 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400136 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400142 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * checked.
157 *
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
162 */
163
164/*
165 * mballoc operates on the following data:
166 * - on-disk bitmap
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
169 *
170 * there are two types of preallocations:
171 * - inode
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
180 * - locality group
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
185 *
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
188 *
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
192 *
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
196 *
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 *
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
210 *
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * block
222 *
223 * so, now we're building a concurrency table:
224 * - init buddy vs.
225 * - new PA
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
228 * - use inode PA
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
231 * - discard inode PA
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
237 * - new PA vs.
238 * - use inode PA
239 * i_data_sem serializes them
240 * - discard inode PA
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
246 * - use inode PA
247 * - use inode PA
248 * i_data_sem or another mutex should serializes them
249 * - discard inode PA
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
255 *
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 *
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
266 *
267 */
268
269 /*
270 * Logic in few words:
271 *
272 * - allocation:
273 * load group
274 * find blocks
275 * mark bits in on-disk bitmap
276 * release group
277 *
278 * - use preallocation:
279 * find proper PA (per-inode or group)
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 * release PA
284 *
285 * - free:
286 * load group
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - discard preallocations in group:
291 * mark PAs deleted
292 * move them onto local list
293 * load on-disk bitmap
294 * load group
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
297 *
298 * - discard inode's preallocations:
299 */
300
301/*
302 * Locking rules
303 *
304 * Locks:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
307 * - per-pa lock (pa)
308 *
309 * Paths:
310 * - new pa
311 * object
312 * group
313 *
314 * - find and use pa:
315 * pa
316 *
317 * - release consumed pa:
318 * pa
319 * group
320 * object
321 *
322 * - generate in-core bitmap:
323 * group
324 * pa
325 *
326 * - discard all for given object (inode, locality group):
327 * object
328 * pa
329 * group
330 *
331 * - discard all for given group:
332 * group
333 * pa
334 * group
335 * object
336 *
337 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500338static struct kmem_cache *ext4_pspace_cachep;
339static struct kmem_cache *ext4_ac_cachep;
340static struct kmem_cache *ext4_free_ext_cachep;
341static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
342 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500343static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
344 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500345static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
346
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500347static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
348{
Alex Tomasc9de5602008-01-29 00:19:52 -0500349#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500350 *bit += ((unsigned long) addr & 7UL) << 3;
351 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500352#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500353 *bit += ((unsigned long) addr & 3UL) << 3;
354 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500355#else
356#error "how many bits you are?!"
357#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500358 return addr;
359}
Alex Tomasc9de5602008-01-29 00:19:52 -0500360
361static inline int mb_test_bit(int bit, void *addr)
362{
363 /*
364 * ext4_test_bit on architecture like powerpc
365 * needs unsigned long aligned address
366 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500367 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500368 return ext4_test_bit(bit, addr);
369}
370
371static inline void mb_set_bit(int bit, void *addr)
372{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500373 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500374 ext4_set_bit(bit, addr);
375}
376
Alex Tomasc9de5602008-01-29 00:19:52 -0500377static inline void mb_clear_bit(int bit, void *addr)
378{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500379 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500380 ext4_clear_bit(bit, addr);
381}
382
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500383static inline int mb_find_next_zero_bit(void *addr, int max, int start)
384{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400385 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400387 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500388 start += fix;
389
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400390 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
391 if (ret > max)
392 return max;
393 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500394}
395
396static inline int mb_find_next_bit(void *addr, int max, int start)
397{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400398 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400400 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500401 start += fix;
402
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400403 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
404 if (ret > max)
405 return max;
406 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500407}
408
Alex Tomasc9de5602008-01-29 00:19:52 -0500409static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
410{
411 char *bb;
412
Alex Tomasc9de5602008-01-29 00:19:52 -0500413 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
414 BUG_ON(max == NULL);
415
416 if (order > e4b->bd_blkbits + 1) {
417 *max = 0;
418 return NULL;
419 }
420
421 /* at order 0 we see each particular block */
422 *max = 1 << (e4b->bd_blkbits + 3);
423 if (order == 0)
424 return EXT4_MB_BITMAP(e4b);
425
426 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
427 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
428
429 return bb;
430}
431
432#ifdef DOUBLE_CHECK
433static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
434 int first, int count)
435{
436 int i;
437 struct super_block *sb = e4b->bd_sb;
438
439 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
440 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400441 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500442 for (i = 0; i < count; i++) {
443 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
444 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500445
446 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500447 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500448 ext4_grp_locked_error(sb, e4b->bd_group,
449 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500450 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -0500451 inode ? inode->i_ino : 0, blocknr,
452 first + i, e4b->bd_group);
453 }
454 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
455 }
456}
457
458static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
459{
460 int i;
461
462 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
463 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400464 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500465 for (i = 0; i < count; i++) {
466 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
467 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
468 }
469}
470
471static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
472{
473 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
474 unsigned char *b1, *b2;
475 int i;
476 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
477 b2 = (unsigned char *) bitmap;
478 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
479 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500480 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o47760042008-09-08 23:00:52 -0400481 "at byte %u(%u): %x in copy != %x "
482 "on disk/prealloc\n",
483 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500484 BUG();
485 }
486 }
487 }
488}
489
490#else
491static inline void mb_free_blocks_double(struct inode *inode,
492 struct ext4_buddy *e4b, int first, int count)
493{
494 return;
495}
496static inline void mb_mark_used_double(struct ext4_buddy *e4b,
497 int first, int count)
498{
499 return;
500}
501static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
502{
503 return;
504}
505#endif
506
507#ifdef AGGRESSIVE_CHECK
508
509#define MB_CHECK_ASSERT(assert) \
510do { \
511 if (!(assert)) { \
512 printk(KERN_EMERG \
513 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
514 function, file, line, # assert); \
515 BUG(); \
516 } \
517} while (0)
518
519static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
520 const char *function, int line)
521{
522 struct super_block *sb = e4b->bd_sb;
523 int order = e4b->bd_blkbits + 1;
524 int max;
525 int max2;
526 int i;
527 int j;
528 int k;
529 int count;
530 struct ext4_group_info *grp;
531 int fragments = 0;
532 int fstart;
533 struct list_head *cur;
534 void *buddy;
535 void *buddy2;
536
Alex Tomasc9de5602008-01-29 00:19:52 -0500537 {
538 static int mb_check_counter;
539 if (mb_check_counter++ % 100 != 0)
540 return 0;
541 }
542
543 while (order > 1) {
544 buddy = mb_find_buddy(e4b, order, &max);
545 MB_CHECK_ASSERT(buddy);
546 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
547 MB_CHECK_ASSERT(buddy2);
548 MB_CHECK_ASSERT(buddy != buddy2);
549 MB_CHECK_ASSERT(max * 2 == max2);
550
551 count = 0;
552 for (i = 0; i < max; i++) {
553
554 if (mb_test_bit(i, buddy)) {
555 /* only single bit in buddy2 may be 1 */
556 if (!mb_test_bit(i << 1, buddy2)) {
557 MB_CHECK_ASSERT(
558 mb_test_bit((i<<1)+1, buddy2));
559 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
560 MB_CHECK_ASSERT(
561 mb_test_bit(i << 1, buddy2));
562 }
563 continue;
564 }
565
566 /* both bits in buddy2 must be 0 */
567 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
568 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
569
570 for (j = 0; j < (1 << order); j++) {
571 k = (i * (1 << order)) + j;
572 MB_CHECK_ASSERT(
573 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
574 }
575 count++;
576 }
577 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
578 order--;
579 }
580
581 fstart = -1;
582 buddy = mb_find_buddy(e4b, 0, &max);
583 for (i = 0; i < max; i++) {
584 if (!mb_test_bit(i, buddy)) {
585 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
586 if (fstart == -1) {
587 fragments++;
588 fstart = i;
589 }
590 continue;
591 }
592 fstart = -1;
593 /* check used bits only */
594 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
595 buddy2 = mb_find_buddy(e4b, j, &max2);
596 k = i >> j;
597 MB_CHECK_ASSERT(k < max2);
598 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
599 }
600 }
601 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
602 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
603
604 grp = ext4_get_group_info(sb, e4b->bd_group);
605 buddy = mb_find_buddy(e4b, 0, &max);
606 list_for_each(cur, &grp->bb_prealloc_list) {
607 ext4_group_t groupnr;
608 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400609 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
610 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500611 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400612 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500613 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
614 }
615 return 0;
616}
617#undef MB_CHECK_ASSERT
618#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400619 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500620#else
621#define mb_check_buddy(e4b)
622#endif
623
624/* FIXME!! need more doc */
625static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400626 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500627 struct ext4_group_info *grp)
628{
629 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400630 ext4_grpblk_t min;
631 ext4_grpblk_t max;
632 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 unsigned short border;
634
Valerie Clementb73fce62008-02-15 13:48:51 -0500635 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500636
637 border = 2 << sb->s_blocksize_bits;
638
639 while (len > 0) {
640 /* find how many blocks can be covered since this position */
641 max = ffs(first | border) - 1;
642
643 /* find how many blocks of power 2 we need to mark */
644 min = fls(len) - 1;
645
646 if (max < min)
647 min = max;
648 chunk = 1 << min;
649
650 /* mark multiblock chunks only */
651 grp->bb_counters[min]++;
652 if (min > 0)
653 mb_clear_bit(first >> min,
654 buddy + sbi->s_mb_offsets[min]);
655
656 len -= chunk;
657 first += chunk;
658 }
659}
660
Eric Sandeen089ceec2009-07-05 22:17:31 -0400661static noinline_for_stack
662void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500663 void *buddy, void *bitmap, ext4_group_t group)
664{
665 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400666 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
667 ext4_grpblk_t i = 0;
668 ext4_grpblk_t first;
669 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500670 unsigned free = 0;
671 unsigned fragments = 0;
672 unsigned long long period = get_cycles();
673
674 /* initialize buddy from bitmap which is aggregation
675 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500676 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500677 grp->bb_first_free = i;
678 while (i < max) {
679 fragments++;
680 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500681 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500682 len = i - first;
683 free += len;
684 if (len > 1)
685 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
686 else
687 grp->bb_counters[0]++;
688 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500689 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500690 }
691 grp->bb_fragments = fragments;
692
693 if (free != grp->bb_free) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500694 ext4_grp_locked_error(sb, group, __func__,
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500695 "EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
Alex Tomasc9de5602008-01-29 00:19:52 -0500696 group, free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500697 /*
698 * If we intent to continue, we consider group descritor
699 * corrupt and update bb_free using bitmap value
700 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500701 grp->bb_free = free;
702 }
703
704 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
705
706 period = get_cycles() - period;
707 spin_lock(&EXT4_SB(sb)->s_bal_lock);
708 EXT4_SB(sb)->s_mb_buddies_generated++;
709 EXT4_SB(sb)->s_mb_generation_time += period;
710 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
711}
712
713/* The buddy information is attached the buddy cache inode
714 * for convenience. The information regarding each group
715 * is loaded via ext4_mb_load_buddy. The information involve
716 * block bitmap and buddy information. The information are
717 * stored in the inode as
718 *
719 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500720 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500721 *
722 *
723 * one block each for bitmap and buddy information.
724 * So for each group we take up 2 blocks. A page can
725 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
726 * So it can have information regarding groups_per_page which
727 * is blocks_per_page/2
728 */
729
730static int ext4_mb_init_cache(struct page *page, char *incore)
731{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400732 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500733 int blocksize;
734 int blocks_per_page;
735 int groups_per_page;
736 int err = 0;
737 int i;
738 ext4_group_t first_group;
739 int first_block;
740 struct super_block *sb;
741 struct buffer_head *bhs;
742 struct buffer_head **bh;
743 struct inode *inode;
744 char *data;
745 char *bitmap;
746
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400747 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500748
749 inode = page->mapping->host;
750 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400751 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500752 blocksize = 1 << inode->i_blkbits;
753 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
754
755 groups_per_page = blocks_per_page >> 1;
756 if (groups_per_page == 0)
757 groups_per_page = 1;
758
759 /* allocate buffer_heads to read bitmaps */
760 if (groups_per_page > 1) {
761 err = -ENOMEM;
762 i = sizeof(struct buffer_head *) * groups_per_page;
763 bh = kzalloc(i, GFP_NOFS);
764 if (bh == NULL)
765 goto out;
766 } else
767 bh = &bhs;
768
769 first_group = page->index * blocks_per_page / 2;
770
771 /* read all groups the page covers into the cache */
772 for (i = 0; i < groups_per_page; i++) {
773 struct ext4_group_desc *desc;
774
Theodore Ts'o8df96752009-05-01 08:50:38 -0400775 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500776 break;
777
778 err = -EIO;
779 desc = ext4_get_group_desc(sb, first_group + i, NULL);
780 if (desc == NULL)
781 goto out;
782
783 err = -ENOMEM;
784 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
785 if (bh[i] == NULL)
786 goto out;
787
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500788 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500789 continue;
790
Frederic Bohec806e682008-10-10 08:09:18 -0400791 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500792 if (bitmap_uptodate(bh[i])) {
793 unlock_buffer(bh[i]);
794 continue;
795 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400796 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500797 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
798 ext4_init_block_bitmap(sb, bh[i],
799 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500800 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500801 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400802 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500803 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500804 continue;
805 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400806 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500807 if (buffer_uptodate(bh[i])) {
808 /*
809 * if not uninit if bh is uptodate,
810 * bitmap is also uptodate
811 */
812 set_bitmap_uptodate(bh[i]);
813 unlock_buffer(bh[i]);
814 continue;
815 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500817 /*
818 * submit the buffer_head for read. We can
819 * safely mark the bitmap as uptodate now.
820 * We do it here so the bitmap uptodate bit
821 * get set with buffer lock held.
822 */
823 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500824 bh[i]->b_end_io = end_buffer_read_sync;
825 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400826 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500827 }
828
829 /* wait for I/O completion */
830 for (i = 0; i < groups_per_page && bh[i]; i++)
831 wait_on_buffer(bh[i]);
832
833 err = -EIO;
834 for (i = 0; i < groups_per_page && bh[i]; i++)
835 if (!buffer_uptodate(bh[i]))
836 goto out;
837
Mingming Cao31b481d2008-07-11 19:27:31 -0400838 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500839 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500840 /* init the page */
841 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500842 for (i = 0; i < blocks_per_page; i++) {
843 int group;
844 struct ext4_group_info *grinfo;
845
846 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400847 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 break;
849
850 /*
851 * data carry information regarding this
852 * particular group in the format specified
853 * above
854 *
855 */
856 data = page_address(page) + (i * blocksize);
857 bitmap = bh[group - first_group]->b_data;
858
859 /*
860 * We place the buddy block and bitmap block
861 * close together
862 */
863 if ((first_block + i) & 1) {
864 /* this is block of buddy */
865 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400866 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 group, page->index, i * blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500868 grinfo = ext4_get_group_info(sb, group);
869 grinfo->bb_fragments = 0;
870 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400871 sizeof(*grinfo->bb_counters) *
872 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 /*
874 * incore got set to the group block bitmap below
875 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500876 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500877 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500878 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 incore = NULL;
880 } else {
881 /* this is block of bitmap */
882 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400883 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500884 group, page->index, i * blocksize);
885
886 /* see comments in ext4_mb_put_pa() */
887 ext4_lock_group(sb, group);
888 memcpy(data, bitmap, blocksize);
889
890 /* mark all preallocated blks used in in-core bitmap */
891 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500892 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500893 ext4_unlock_group(sb, group);
894
895 /* set incore so that the buddy information can be
896 * generated using this
897 */
898 incore = data;
899 }
900 }
901 SetPageUptodate(page);
902
903out:
904 if (bh) {
905 for (i = 0; i < groups_per_page && bh[i]; i++)
906 brelse(bh[i]);
907 if (bh != &bhs)
908 kfree(bh);
909 }
910 return err;
911}
912
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400913static noinline_for_stack
914int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
915{
916
917 int ret = 0;
918 void *bitmap;
919 int blocks_per_page;
920 int block, pnum, poff;
921 int num_grp_locked = 0;
922 struct ext4_group_info *this_grp;
923 struct ext4_sb_info *sbi = EXT4_SB(sb);
924 struct inode *inode = sbi->s_buddy_cache;
925 struct page *page = NULL, *bitmap_page = NULL;
926
927 mb_debug(1, "init group %u\n", group);
928 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
929 this_grp = ext4_get_group_info(sb, group);
930 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -0400931 * This ensures that we don't reinit the buddy cache
932 * page which map to the group from which we are already
933 * allocating. If we are looking at the buddy cache we would
934 * have taken a reference using ext4_mb_load_buddy and that
935 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -0400936 */
937 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
938 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
939 /*
940 * somebody initialized the group
941 * return without doing anything
942 */
943 ret = 0;
944 goto err;
945 }
946 /*
947 * the buddy cache inode stores the block bitmap
948 * and buddy information in consecutive blocks.
949 * So for each group we need two blocks.
950 */
951 block = group * 2;
952 pnum = block / blocks_per_page;
953 poff = block % blocks_per_page;
954 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
955 if (page) {
956 BUG_ON(page->mapping != inode->i_mapping);
957 ret = ext4_mb_init_cache(page, NULL);
958 if (ret) {
959 unlock_page(page);
960 goto err;
961 }
962 unlock_page(page);
963 }
964 if (page == NULL || !PageUptodate(page)) {
965 ret = -EIO;
966 goto err;
967 }
968 mark_page_accessed(page);
969 bitmap_page = page;
970 bitmap = page_address(page) + (poff * sb->s_blocksize);
971
972 /* init buddy cache */
973 block++;
974 pnum = block / blocks_per_page;
975 poff = block % blocks_per_page;
976 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
977 if (page == bitmap_page) {
978 /*
979 * If both the bitmap and buddy are in
980 * the same page we don't need to force
981 * init the buddy
982 */
983 unlock_page(page);
984 } else if (page) {
985 BUG_ON(page->mapping != inode->i_mapping);
986 ret = ext4_mb_init_cache(page, bitmap);
987 if (ret) {
988 unlock_page(page);
989 goto err;
990 }
991 unlock_page(page);
992 }
993 if (page == NULL || !PageUptodate(page)) {
994 ret = -EIO;
995 goto err;
996 }
997 mark_page_accessed(page);
998err:
999 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1000 if (bitmap_page)
1001 page_cache_release(bitmap_page);
1002 if (page)
1003 page_cache_release(page);
1004 return ret;
1005}
1006
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001007static noinline_for_stack int
1008ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1009 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001010{
Alex Tomasc9de5602008-01-29 00:19:52 -05001011 int blocks_per_page;
1012 int block;
1013 int pnum;
1014 int poff;
1015 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001016 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001017 struct ext4_group_info *grp;
1018 struct ext4_sb_info *sbi = EXT4_SB(sb);
1019 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001020
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001021 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001022
1023 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001024 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001025
1026 e4b->bd_blkbits = sb->s_blocksize_bits;
1027 e4b->bd_info = ext4_get_group_info(sb, group);
1028 e4b->bd_sb = sb;
1029 e4b->bd_group = group;
1030 e4b->bd_buddy_page = NULL;
1031 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001032 e4b->alloc_semp = &grp->alloc_sem;
1033
1034 /* Take the read lock on the group alloc
1035 * sem. This would make sure a parallel
1036 * ext4_mb_init_group happening on other
1037 * groups mapped by the page is blocked
1038 * till we are done with allocation
1039 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001040repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001041 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001042
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001043 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1044 /* we need to check for group need init flag
1045 * with alloc_semp held so that we can be sure
1046 * that new blocks didn't get added to the group
1047 * when we are loading the buddy cache
1048 */
1049 up_read(e4b->alloc_semp);
1050 /*
1051 * we need full data about the group
1052 * to make a good selection
1053 */
1054 ret = ext4_mb_init_group(sb, group);
1055 if (ret)
1056 return ret;
1057 goto repeat_load_buddy;
1058 }
1059
Alex Tomasc9de5602008-01-29 00:19:52 -05001060 /*
1061 * the buddy cache inode stores the block bitmap
1062 * and buddy information in consecutive blocks.
1063 * So for each group we need two blocks.
1064 */
1065 block = group * 2;
1066 pnum = block / blocks_per_page;
1067 poff = block % blocks_per_page;
1068
1069 /* we could use find_or_create_page(), but it locks page
1070 * what we'd like to avoid in fast path ... */
1071 page = find_get_page(inode->i_mapping, pnum);
1072 if (page == NULL || !PageUptodate(page)) {
1073 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001074 /*
1075 * drop the page reference and try
1076 * to get the page with lock. If we
1077 * are not uptodate that implies
1078 * somebody just created the page but
1079 * is yet to initialize the same. So
1080 * wait for it to initialize.
1081 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001082 page_cache_release(page);
1083 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1084 if (page) {
1085 BUG_ON(page->mapping != inode->i_mapping);
1086 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001087 ret = ext4_mb_init_cache(page, NULL);
1088 if (ret) {
1089 unlock_page(page);
1090 goto err;
1091 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001092 mb_cmp_bitmaps(e4b, page_address(page) +
1093 (poff * sb->s_blocksize));
1094 }
1095 unlock_page(page);
1096 }
1097 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001098 if (page == NULL || !PageUptodate(page)) {
1099 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001100 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001101 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001102 e4b->bd_bitmap_page = page;
1103 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1104 mark_page_accessed(page);
1105
1106 block++;
1107 pnum = block / blocks_per_page;
1108 poff = block % blocks_per_page;
1109
1110 page = find_get_page(inode->i_mapping, pnum);
1111 if (page == NULL || !PageUptodate(page)) {
1112 if (page)
1113 page_cache_release(page);
1114 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1115 if (page) {
1116 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001117 if (!PageUptodate(page)) {
1118 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1119 if (ret) {
1120 unlock_page(page);
1121 goto err;
1122 }
1123 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001124 unlock_page(page);
1125 }
1126 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001127 if (page == NULL || !PageUptodate(page)) {
1128 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001129 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001130 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001131 e4b->bd_buddy_page = page;
1132 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1133 mark_page_accessed(page);
1134
1135 BUG_ON(e4b->bd_bitmap_page == NULL);
1136 BUG_ON(e4b->bd_buddy_page == NULL);
1137
1138 return 0;
1139
1140err:
1141 if (e4b->bd_bitmap_page)
1142 page_cache_release(e4b->bd_bitmap_page);
1143 if (e4b->bd_buddy_page)
1144 page_cache_release(e4b->bd_buddy_page);
1145 e4b->bd_buddy = NULL;
1146 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001147
1148 /* Done with the buddy cache */
1149 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001150 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001151}
1152
Jing Zhange39e07f2010-05-14 00:00:00 -04001153static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001154{
1155 if (e4b->bd_bitmap_page)
1156 page_cache_release(e4b->bd_bitmap_page);
1157 if (e4b->bd_buddy_page)
1158 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001159 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001160 if (e4b->alloc_semp)
1161 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001162}
1163
1164
1165static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1166{
1167 int order = 1;
1168 void *bb;
1169
1170 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1171 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1172
1173 bb = EXT4_MB_BUDDY(e4b);
1174 while (order <= e4b->bd_blkbits + 1) {
1175 block = block >> 1;
1176 if (!mb_test_bit(block, bb)) {
1177 /* this block is part of buddy of order 'order' */
1178 return order;
1179 }
1180 bb += 1 << (e4b->bd_blkbits - order);
1181 order++;
1182 }
1183 return 0;
1184}
1185
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001186static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001187{
1188 __u32 *addr;
1189
1190 len = cur + len;
1191 while (cur < len) {
1192 if ((cur & 31) == 0 && (len - cur) >= 32) {
1193 /* fast path: clear whole word at once */
1194 addr = bm + (cur >> 3);
1195 *addr = 0;
1196 cur += 32;
1197 continue;
1198 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001199 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001200 cur++;
1201 }
1202}
1203
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001204static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001205{
1206 __u32 *addr;
1207
1208 len = cur + len;
1209 while (cur < len) {
1210 if ((cur & 31) == 0 && (len - cur) >= 32) {
1211 /* fast path: set whole word at once */
1212 addr = bm + (cur >> 3);
1213 *addr = 0xffffffff;
1214 cur += 32;
1215 continue;
1216 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001217 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001218 cur++;
1219 }
1220}
1221
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001222static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001223 int first, int count)
1224{
1225 int block = 0;
1226 int max = 0;
1227 int order;
1228 void *buddy;
1229 void *buddy2;
1230 struct super_block *sb = e4b->bd_sb;
1231
1232 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001233 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001234 mb_check_buddy(e4b);
1235 mb_free_blocks_double(inode, e4b, first, count);
1236
1237 e4b->bd_info->bb_free += count;
1238 if (first < e4b->bd_info->bb_first_free)
1239 e4b->bd_info->bb_first_free = first;
1240
1241 /* let's maintain fragments counter */
1242 if (first != 0)
1243 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1244 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1245 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1246 if (block && max)
1247 e4b->bd_info->bb_fragments--;
1248 else if (!block && !max)
1249 e4b->bd_info->bb_fragments++;
1250
1251 /* let's maintain buddy itself */
1252 while (count-- > 0) {
1253 block = first++;
1254 order = 0;
1255
1256 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1257 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001258
1259 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001260 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001261 ext4_grp_locked_error(sb, e4b->bd_group,
1262 __func__, "double-free of inode"
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001263 " %lu's block %llu(bit %u in group %u)",
Alex Tomasc9de5602008-01-29 00:19:52 -05001264 inode ? inode->i_ino : 0, blocknr, block,
1265 e4b->bd_group);
1266 }
1267 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1268 e4b->bd_info->bb_counters[order]++;
1269
1270 /* start of the buddy */
1271 buddy = mb_find_buddy(e4b, order, &max);
1272
1273 do {
1274 block &= ~1UL;
1275 if (mb_test_bit(block, buddy) ||
1276 mb_test_bit(block + 1, buddy))
1277 break;
1278
1279 /* both the buddies are free, try to coalesce them */
1280 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1281
1282 if (!buddy2)
1283 break;
1284
1285 if (order > 0) {
1286 /* for special purposes, we don't set
1287 * free bits in bitmap */
1288 mb_set_bit(block, buddy);
1289 mb_set_bit(block + 1, buddy);
1290 }
1291 e4b->bd_info->bb_counters[order]--;
1292 e4b->bd_info->bb_counters[order]--;
1293
1294 block = block >> 1;
1295 order++;
1296 e4b->bd_info->bb_counters[order]++;
1297
1298 mb_clear_bit(block, buddy2);
1299 buddy = buddy2;
1300 } while (1);
1301 }
1302 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001303}
1304
1305static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1306 int needed, struct ext4_free_extent *ex)
1307{
1308 int next = block;
1309 int max;
1310 int ord;
1311 void *buddy;
1312
Vincent Minetbc8e6742009-05-15 08:33:18 -04001313 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001314 BUG_ON(ex == NULL);
1315
1316 buddy = mb_find_buddy(e4b, order, &max);
1317 BUG_ON(buddy == NULL);
1318 BUG_ON(block >= max);
1319 if (mb_test_bit(block, buddy)) {
1320 ex->fe_len = 0;
1321 ex->fe_start = 0;
1322 ex->fe_group = 0;
1323 return 0;
1324 }
1325
1326 /* FIXME dorp order completely ? */
1327 if (likely(order == 0)) {
1328 /* find actual order */
1329 order = mb_find_order_for_block(e4b, block);
1330 block = block >> order;
1331 }
1332
1333 ex->fe_len = 1 << order;
1334 ex->fe_start = block << order;
1335 ex->fe_group = e4b->bd_group;
1336
1337 /* calc difference from given start */
1338 next = next - ex->fe_start;
1339 ex->fe_len -= next;
1340 ex->fe_start += next;
1341
1342 while (needed > ex->fe_len &&
1343 (buddy = mb_find_buddy(e4b, order, &max))) {
1344
1345 if (block + 1 >= max)
1346 break;
1347
1348 next = (block + 1) * (1 << order);
1349 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1350 break;
1351
1352 ord = mb_find_order_for_block(e4b, next);
1353
1354 order = ord;
1355 block = next >> order;
1356 ex->fe_len += 1 << order;
1357 }
1358
1359 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1360 return ex->fe_len;
1361}
1362
1363static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1364{
1365 int ord;
1366 int mlen = 0;
1367 int max = 0;
1368 int cur;
1369 int start = ex->fe_start;
1370 int len = ex->fe_len;
1371 unsigned ret = 0;
1372 int len0 = len;
1373 void *buddy;
1374
1375 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1376 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001377 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001378 mb_check_buddy(e4b);
1379 mb_mark_used_double(e4b, start, len);
1380
1381 e4b->bd_info->bb_free -= len;
1382 if (e4b->bd_info->bb_first_free == start)
1383 e4b->bd_info->bb_first_free += len;
1384
1385 /* let's maintain fragments counter */
1386 if (start != 0)
1387 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1388 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1389 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1390 if (mlen && max)
1391 e4b->bd_info->bb_fragments++;
1392 else if (!mlen && !max)
1393 e4b->bd_info->bb_fragments--;
1394
1395 /* let's maintain buddy itself */
1396 while (len) {
1397 ord = mb_find_order_for_block(e4b, start);
1398
1399 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1400 /* the whole chunk may be allocated at once! */
1401 mlen = 1 << ord;
1402 buddy = mb_find_buddy(e4b, ord, &max);
1403 BUG_ON((start >> ord) >= max);
1404 mb_set_bit(start >> ord, buddy);
1405 e4b->bd_info->bb_counters[ord]--;
1406 start += mlen;
1407 len -= mlen;
1408 BUG_ON(len < 0);
1409 continue;
1410 }
1411
1412 /* store for history */
1413 if (ret == 0)
1414 ret = len | (ord << 16);
1415
1416 /* we have to split large buddy */
1417 BUG_ON(ord <= 0);
1418 buddy = mb_find_buddy(e4b, ord, &max);
1419 mb_set_bit(start >> ord, buddy);
1420 e4b->bd_info->bb_counters[ord]--;
1421
1422 ord--;
1423 cur = (start >> ord) & ~1U;
1424 buddy = mb_find_buddy(e4b, ord, &max);
1425 mb_clear_bit(cur, buddy);
1426 mb_clear_bit(cur + 1, buddy);
1427 e4b->bd_info->bb_counters[ord]++;
1428 e4b->bd_info->bb_counters[ord]++;
1429 }
1430
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001431 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001432 mb_check_buddy(e4b);
1433
1434 return ret;
1435}
1436
1437/*
1438 * Must be called under group lock!
1439 */
1440static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1441 struct ext4_buddy *e4b)
1442{
1443 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1444 int ret;
1445
1446 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1447 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1448
1449 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1450 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1451 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1452
1453 /* preallocation can change ac_b_ex, thus we store actually
1454 * allocated blocks for history */
1455 ac->ac_f_ex = ac->ac_b_ex;
1456
1457 ac->ac_status = AC_STATUS_FOUND;
1458 ac->ac_tail = ret & 0xffff;
1459 ac->ac_buddy = ret >> 16;
1460
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001461 /*
1462 * take the page reference. We want the page to be pinned
1463 * so that we don't get a ext4_mb_init_cache_call for this
1464 * group until we update the bitmap. That would mean we
1465 * double allocate blocks. The reference is dropped
1466 * in ext4_mb_release_context
1467 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001468 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1469 get_page(ac->ac_bitmap_page);
1470 ac->ac_buddy_page = e4b->bd_buddy_page;
1471 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001472 /* on allocation we use ac to track the held semaphore */
1473 ac->alloc_semp = e4b->alloc_semp;
1474 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001475 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001476 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001477 spin_lock(&sbi->s_md_lock);
1478 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1479 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1480 spin_unlock(&sbi->s_md_lock);
1481 }
1482}
1483
1484/*
1485 * regular allocator, for general purposes allocation
1486 */
1487
1488static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1489 struct ext4_buddy *e4b,
1490 int finish_group)
1491{
1492 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1493 struct ext4_free_extent *bex = &ac->ac_b_ex;
1494 struct ext4_free_extent *gex = &ac->ac_g_ex;
1495 struct ext4_free_extent ex;
1496 int max;
1497
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001498 if (ac->ac_status == AC_STATUS_FOUND)
1499 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001500 /*
1501 * We don't want to scan for a whole year
1502 */
1503 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1504 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1505 ac->ac_status = AC_STATUS_BREAK;
1506 return;
1507 }
1508
1509 /*
1510 * Haven't found good chunk so far, let's continue
1511 */
1512 if (bex->fe_len < gex->fe_len)
1513 return;
1514
1515 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1516 && bex->fe_group == e4b->bd_group) {
1517 /* recheck chunk's availability - we don't know
1518 * when it was found (within this lock-unlock
1519 * period or not) */
1520 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1521 if (max >= gex->fe_len) {
1522 ext4_mb_use_best_found(ac, e4b);
1523 return;
1524 }
1525 }
1526}
1527
1528/*
1529 * The routine checks whether found extent is good enough. If it is,
1530 * then the extent gets marked used and flag is set to the context
1531 * to stop scanning. Otherwise, the extent is compared with the
1532 * previous found extent and if new one is better, then it's stored
1533 * in the context. Later, the best found extent will be used, if
1534 * mballoc can't find good enough extent.
1535 *
1536 * FIXME: real allocation policy is to be designed yet!
1537 */
1538static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1539 struct ext4_free_extent *ex,
1540 struct ext4_buddy *e4b)
1541{
1542 struct ext4_free_extent *bex = &ac->ac_b_ex;
1543 struct ext4_free_extent *gex = &ac->ac_g_ex;
1544
1545 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001546 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001547 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1548 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1549
1550 ac->ac_found++;
1551
1552 /*
1553 * The special case - take what you catch first
1554 */
1555 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1556 *bex = *ex;
1557 ext4_mb_use_best_found(ac, e4b);
1558 return;
1559 }
1560
1561 /*
1562 * Let's check whether the chuck is good enough
1563 */
1564 if (ex->fe_len == gex->fe_len) {
1565 *bex = *ex;
1566 ext4_mb_use_best_found(ac, e4b);
1567 return;
1568 }
1569
1570 /*
1571 * If this is first found extent, just store it in the context
1572 */
1573 if (bex->fe_len == 0) {
1574 *bex = *ex;
1575 return;
1576 }
1577
1578 /*
1579 * If new found extent is better, store it in the context
1580 */
1581 if (bex->fe_len < gex->fe_len) {
1582 /* if the request isn't satisfied, any found extent
1583 * larger than previous best one is better */
1584 if (ex->fe_len > bex->fe_len)
1585 *bex = *ex;
1586 } else if (ex->fe_len > gex->fe_len) {
1587 /* if the request is satisfied, then we try to find
1588 * an extent that still satisfy the request, but is
1589 * smaller than previous one */
1590 if (ex->fe_len < bex->fe_len)
1591 *bex = *ex;
1592 }
1593
1594 ext4_mb_check_limits(ac, e4b, 0);
1595}
1596
Eric Sandeen089ceec2009-07-05 22:17:31 -04001597static noinline_for_stack
1598int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001599 struct ext4_buddy *e4b)
1600{
1601 struct ext4_free_extent ex = ac->ac_b_ex;
1602 ext4_group_t group = ex.fe_group;
1603 int max;
1604 int err;
1605
1606 BUG_ON(ex.fe_len <= 0);
1607 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1608 if (err)
1609 return err;
1610
1611 ext4_lock_group(ac->ac_sb, group);
1612 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1613
1614 if (max > 0) {
1615 ac->ac_b_ex = ex;
1616 ext4_mb_use_best_found(ac, e4b);
1617 }
1618
1619 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001620 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001621
1622 return 0;
1623}
1624
Eric Sandeen089ceec2009-07-05 22:17:31 -04001625static noinline_for_stack
1626int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001627 struct ext4_buddy *e4b)
1628{
1629 ext4_group_t group = ac->ac_g_ex.fe_group;
1630 int max;
1631 int err;
1632 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001633 struct ext4_free_extent ex;
1634
1635 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1636 return 0;
1637
1638 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1639 if (err)
1640 return err;
1641
1642 ext4_lock_group(ac->ac_sb, group);
1643 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1644 ac->ac_g_ex.fe_len, &ex);
1645
1646 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1647 ext4_fsblk_t start;
1648
Akinobu Mita5661bd62010-03-03 23:53:39 -05001649 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1650 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001651 /* use do_div to get remainder (would be 64-bit modulo) */
1652 if (do_div(start, sbi->s_stripe) == 0) {
1653 ac->ac_found++;
1654 ac->ac_b_ex = ex;
1655 ext4_mb_use_best_found(ac, e4b);
1656 }
1657 } else if (max >= ac->ac_g_ex.fe_len) {
1658 BUG_ON(ex.fe_len <= 0);
1659 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1660 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1661 ac->ac_found++;
1662 ac->ac_b_ex = ex;
1663 ext4_mb_use_best_found(ac, e4b);
1664 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1665 /* Sometimes, caller may want to merge even small
1666 * number of blocks to an existing extent */
1667 BUG_ON(ex.fe_len <= 0);
1668 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1669 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1670 ac->ac_found++;
1671 ac->ac_b_ex = ex;
1672 ext4_mb_use_best_found(ac, e4b);
1673 }
1674 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001675 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001676
1677 return 0;
1678}
1679
1680/*
1681 * The routine scans buddy structures (not bitmap!) from given order
1682 * to max order and tries to find big enough chunk to satisfy the req
1683 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001684static noinline_for_stack
1685void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001686 struct ext4_buddy *e4b)
1687{
1688 struct super_block *sb = ac->ac_sb;
1689 struct ext4_group_info *grp = e4b->bd_info;
1690 void *buddy;
1691 int i;
1692 int k;
1693 int max;
1694
1695 BUG_ON(ac->ac_2order <= 0);
1696 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1697 if (grp->bb_counters[i] == 0)
1698 continue;
1699
1700 buddy = mb_find_buddy(e4b, i, &max);
1701 BUG_ON(buddy == NULL);
1702
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001703 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001704 BUG_ON(k >= max);
1705
1706 ac->ac_found++;
1707
1708 ac->ac_b_ex.fe_len = 1 << i;
1709 ac->ac_b_ex.fe_start = k << i;
1710 ac->ac_b_ex.fe_group = e4b->bd_group;
1711
1712 ext4_mb_use_best_found(ac, e4b);
1713
1714 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1715
1716 if (EXT4_SB(sb)->s_mb_stats)
1717 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1718
1719 break;
1720 }
1721}
1722
1723/*
1724 * The routine scans the group and measures all found extents.
1725 * In order to optimize scanning, caller must pass number of
1726 * free blocks in the group, so the routine can know upper limit.
1727 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001728static noinline_for_stack
1729void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001730 struct ext4_buddy *e4b)
1731{
1732 struct super_block *sb = ac->ac_sb;
1733 void *bitmap = EXT4_MB_BITMAP(e4b);
1734 struct ext4_free_extent ex;
1735 int i;
1736 int free;
1737
1738 free = e4b->bd_info->bb_free;
1739 BUG_ON(free <= 0);
1740
1741 i = e4b->bd_info->bb_first_free;
1742
1743 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001744 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001745 EXT4_BLOCKS_PER_GROUP(sb), i);
1746 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001747 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001748 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001749 * free blocks even though group info says we
1750 * we have free blocks
1751 */
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001752 ext4_grp_locked_error(sb, e4b->bd_group,
1753 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001754 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001755 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001756 break;
1757 }
1758
1759 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1760 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001761 if (free < ex.fe_len) {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001762 ext4_grp_locked_error(sb, e4b->bd_group,
1763 __func__, "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001764 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001765 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001766 /*
1767 * The number of free blocks differs. This mostly
1768 * indicate that the bitmap is corrupt. So exit
1769 * without claiming the space.
1770 */
1771 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001772 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001773
1774 ext4_mb_measure_extent(ac, &ex, e4b);
1775
1776 i += ex.fe_len;
1777 free -= ex.fe_len;
1778 }
1779
1780 ext4_mb_check_limits(ac, e4b, 1);
1781}
1782
1783/*
1784 * This is a special case for storages like raid5
1785 * we try to find stripe-aligned chunks for stripe-size requests
1786 * XXX should do so at least for multiples of stripe size as well
1787 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001788static noinline_for_stack
1789void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001790 struct ext4_buddy *e4b)
1791{
1792 struct super_block *sb = ac->ac_sb;
1793 struct ext4_sb_info *sbi = EXT4_SB(sb);
1794 void *bitmap = EXT4_MB_BITMAP(e4b);
1795 struct ext4_free_extent ex;
1796 ext4_fsblk_t first_group_block;
1797 ext4_fsblk_t a;
1798 ext4_grpblk_t i;
1799 int max;
1800
1801 BUG_ON(sbi->s_stripe == 0);
1802
1803 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001804 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1805
Alex Tomasc9de5602008-01-29 00:19:52 -05001806 a = first_group_block + sbi->s_stripe - 1;
1807 do_div(a, sbi->s_stripe);
1808 i = (a * sbi->s_stripe) - first_group_block;
1809
1810 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1811 if (!mb_test_bit(i, bitmap)) {
1812 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1813 if (max >= sbi->s_stripe) {
1814 ac->ac_found++;
1815 ac->ac_b_ex = ex;
1816 ext4_mb_use_best_found(ac, e4b);
1817 break;
1818 }
1819 }
1820 i += sbi->s_stripe;
1821 }
1822}
1823
1824static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1825 ext4_group_t group, int cr)
1826{
1827 unsigned free, fragments;
1828 unsigned i, bits;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001829 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001830 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1831
1832 BUG_ON(cr < 0 || cr >= 4);
1833 BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1834
1835 free = grp->bb_free;
1836 fragments = grp->bb_fragments;
1837 if (free == 0)
1838 return 0;
1839 if (fragments == 0)
1840 return 0;
1841
1842 switch (cr) {
1843 case 0:
1844 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001845
Theodore Ts'oa4912122009-03-12 12:18:34 -04001846 /* Avoid using the first bg of a flexgroup for data files */
1847 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1848 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1849 ((group % flex_size) == 0))
1850 return 0;
1851
Alex Tomasc9de5602008-01-29 00:19:52 -05001852 bits = ac->ac_sb->s_blocksize_bits + 1;
1853 for (i = ac->ac_2order; i <= bits; i++)
1854 if (grp->bb_counters[i] > 0)
1855 return 1;
1856 break;
1857 case 1:
1858 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1859 return 1;
1860 break;
1861 case 2:
1862 if (free >= ac->ac_g_ex.fe_len)
1863 return 1;
1864 break;
1865 case 3:
1866 return 1;
1867 default:
1868 BUG();
1869 }
1870
1871 return 0;
1872}
1873
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001874/*
1875 * lock the group_info alloc_sem of all the groups
1876 * belonging to the same buddy cache page. This
1877 * make sure other parallel operation on the buddy
1878 * cache doesn't happen whild holding the buddy cache
1879 * lock
1880 */
1881int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1882{
1883 int i;
1884 int block, pnum;
1885 int blocks_per_page;
1886 int groups_per_page;
Theodore Ts'o8df96752009-05-01 08:50:38 -04001887 ext4_group_t ngroups = ext4_get_groups_count(sb);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001888 ext4_group_t first_group;
1889 struct ext4_group_info *grp;
1890
1891 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1892 /*
1893 * the buddy cache inode stores the block bitmap
1894 * and buddy information in consecutive blocks.
1895 * So for each group we need two blocks.
1896 */
1897 block = group * 2;
1898 pnum = block / blocks_per_page;
1899 first_group = pnum * blocks_per_page / 2;
1900
1901 groups_per_page = blocks_per_page >> 1;
1902 if (groups_per_page == 0)
1903 groups_per_page = 1;
1904 /* read all groups the page covers into the cache */
1905 for (i = 0; i < groups_per_page; i++) {
1906
Theodore Ts'o8df96752009-05-01 08:50:38 -04001907 if ((first_group + i) >= ngroups)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001908 break;
1909 grp = ext4_get_group_info(sb, first_group + i);
1910 /* take all groups write allocation
1911 * semaphore. This make sure there is
1912 * no block allocation going on in any
1913 * of that groups
1914 */
Aneesh Kumar K.Vb7be0192008-11-23 23:51:53 -05001915 down_write_nested(&grp->alloc_sem, i);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001916 }
1917 return i;
1918}
1919
1920void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1921 ext4_group_t group, int locked_group)
1922{
1923 int i;
1924 int block, pnum;
1925 int blocks_per_page;
1926 ext4_group_t first_group;
1927 struct ext4_group_info *grp;
1928
1929 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1930 /*
1931 * the buddy cache inode stores the block bitmap
1932 * and buddy information in consecutive blocks.
1933 * So for each group we need two blocks.
1934 */
1935 block = group * 2;
1936 pnum = block / blocks_per_page;
1937 first_group = pnum * blocks_per_page / 2;
1938 /* release locks on all the groups */
1939 for (i = 0; i < locked_group; i++) {
1940
1941 grp = ext4_get_group_info(sb, first_group + i);
1942 /* take all groups write allocation
1943 * semaphore. This make sure there is
1944 * no block allocation going on in any
1945 * of that groups
1946 */
1947 up_write(&grp->alloc_sem);
1948 }
1949
1950}
1951
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001952static noinline_for_stack int
1953ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001954{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001955 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001956 int cr;
1957 int err = 0;
1958 int bsbits;
1959 struct ext4_sb_info *sbi;
1960 struct super_block *sb;
1961 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001962
1963 sb = ac->ac_sb;
1964 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001965 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001966 /* non-extent files are limited to low blocks/groups */
1967 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL))
1968 ngroups = sbi->s_blockfile_groups;
1969
Alex Tomasc9de5602008-01-29 00:19:52 -05001970 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1971
1972 /* first, try the goal */
1973 err = ext4_mb_find_by_goal(ac, &e4b);
1974 if (err || ac->ac_status == AC_STATUS_FOUND)
1975 goto out;
1976
1977 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1978 goto out;
1979
1980 /*
1981 * ac->ac2_order is set only if the fe_len is a power of 2
1982 * if ac2_order is set we also set criteria to 0 so that we
1983 * try exact allocation using buddy.
1984 */
1985 i = fls(ac->ac_g_ex.fe_len);
1986 ac->ac_2order = 0;
1987 /*
1988 * We search using buddy data only if the order of the request
1989 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001990 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001991 */
1992 if (i >= sbi->s_mb_order2_reqs) {
1993 /*
1994 * This should tell if fe_len is exactly power of 2
1995 */
1996 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1997 ac->ac_2order = i - 1;
1998 }
1999
2000 bsbits = ac->ac_sb->s_blocksize_bits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002001
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002002 /* if stream allocation is enabled, use global goal */
2003 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 /* TBD: may be hot point */
2005 spin_lock(&sbi->s_md_lock);
2006 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2007 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2008 spin_unlock(&sbi->s_md_lock);
2009 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002010
Alex Tomasc9de5602008-01-29 00:19:52 -05002011 /* Let's just scan groups to find more-less suitable blocks */
2012 cr = ac->ac_2order ? 0 : 1;
2013 /*
2014 * cr == 0 try to get exact allocation,
2015 * cr == 3 try to get anything
2016 */
2017repeat:
2018 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2019 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002020 /*
2021 * searching for the right group start
2022 * from the goal value specified
2023 */
2024 group = ac->ac_g_ex.fe_group;
2025
Theodore Ts'o8df96752009-05-01 08:50:38 -04002026 for (i = 0; i < ngroups; group++, i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002027 struct ext4_group_info *grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002028
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);
Jing Zhange39e07f2010-05-14 00:00:00 -04002045 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002046 continue;
2047 }
2048
2049 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002050 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002051 ext4_mb_simple_scan_group(ac, &e4b);
2052 else if (cr == 1 &&
2053 ac->ac_g_ex.fe_len == sbi->s_stripe)
2054 ext4_mb_scan_aligned(ac, &e4b);
2055 else
2056 ext4_mb_complex_scan_group(ac, &e4b);
2057
2058 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002059 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002060
2061 if (ac->ac_status != AC_STATUS_CONTINUE)
2062 break;
2063 }
2064 }
2065
2066 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2067 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2068 /*
2069 * We've been searching too long. Let's try to allocate
2070 * the best chunk we've found so far
2071 */
2072
2073 ext4_mb_try_best_found(ac, &e4b);
2074 if (ac->ac_status != AC_STATUS_FOUND) {
2075 /*
2076 * Someone more lucky has already allocated it.
2077 * The only thing we can do is just take first
2078 * found block(s)
2079 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2080 */
2081 ac->ac_b_ex.fe_group = 0;
2082 ac->ac_b_ex.fe_start = 0;
2083 ac->ac_b_ex.fe_len = 0;
2084 ac->ac_status = AC_STATUS_CONTINUE;
2085 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2086 cr = 3;
2087 atomic_inc(&sbi->s_mb_lost_chunks);
2088 goto repeat;
2089 }
2090 }
2091out:
2092 return err;
2093}
2094
Alex Tomasc9de5602008-01-29 00:19:52 -05002095static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2096{
2097 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002098 ext4_group_t group;
2099
Theodore Ts'o8df96752009-05-01 08:50:38 -04002100 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002101 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002102 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002103 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002104}
2105
2106static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2107{
2108 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002109 ext4_group_t group;
2110
2111 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002112 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002113 return NULL;
2114 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002115 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002116}
2117
2118static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2119{
2120 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002121 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002122 int i;
2123 int err;
2124 struct ext4_buddy e4b;
2125 struct sg {
2126 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002127 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002128 } sg;
2129
2130 group--;
2131 if (group == 0)
2132 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2133 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2134 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2135 "group", "free", "frags", "first",
2136 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2137 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2138
2139 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2140 sizeof(struct ext4_group_info);
2141 err = ext4_mb_load_buddy(sb, group, &e4b);
2142 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002143 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002144 return 0;
2145 }
2146 ext4_lock_group(sb, group);
2147 memcpy(&sg, ext4_get_group_info(sb, group), i);
2148 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002149 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002150
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002151 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002152 sg.info.bb_fragments, sg.info.bb_first_free);
2153 for (i = 0; i <= 13; i++)
2154 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2155 sg.info.bb_counters[i] : 0);
2156 seq_printf(seq, " ]\n");
2157
2158 return 0;
2159}
2160
2161static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2162{
2163}
2164
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002165static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002166 .start = ext4_mb_seq_groups_start,
2167 .next = ext4_mb_seq_groups_next,
2168 .stop = ext4_mb_seq_groups_stop,
2169 .show = ext4_mb_seq_groups_show,
2170};
2171
2172static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2173{
2174 struct super_block *sb = PDE(inode)->data;
2175 int rc;
2176
2177 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2178 if (rc == 0) {
2179 struct seq_file *m = (struct seq_file *)file->private_data;
2180 m->private = sb;
2181 }
2182 return rc;
2183
2184}
2185
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002186static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002187 .owner = THIS_MODULE,
2188 .open = ext4_mb_seq_groups_open,
2189 .read = seq_read,
2190 .llseek = seq_lseek,
2191 .release = seq_release,
2192};
2193
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002194
2195/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002196int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002197 struct ext4_group_desc *desc)
2198{
2199 int i, len;
2200 int metalen = 0;
2201 struct ext4_sb_info *sbi = EXT4_SB(sb);
2202 struct ext4_group_info **meta_group_info;
2203
2204 /*
2205 * First check if this group is the first of a reserved block.
2206 * If it's true, we have to allocate a new table of pointers
2207 * to ext4_group_info structures
2208 */
2209 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2210 metalen = sizeof(*meta_group_info) <<
2211 EXT4_DESC_PER_BLOCK_BITS(sb);
2212 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2213 if (meta_group_info == NULL) {
2214 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2215 "buddy group\n");
2216 goto exit_meta_group_info;
2217 }
2218 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2219 meta_group_info;
2220 }
2221
2222 /*
2223 * calculate needed size. if change bb_counters size,
2224 * don't forget about ext4_mb_generate_buddy()
2225 */
2226 len = offsetof(typeof(**meta_group_info),
2227 bb_counters[sb->s_blocksize_bits + 2]);
2228
2229 meta_group_info =
2230 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2231 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2232
2233 meta_group_info[i] = kzalloc(len, GFP_KERNEL);
2234 if (meta_group_info[i] == NULL) {
2235 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2236 goto exit_group_info;
2237 }
2238 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2239 &(meta_group_info[i]->bb_state));
2240
2241 /*
2242 * initialize bb_free to be able to skip
2243 * empty groups without initialization
2244 */
2245 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2246 meta_group_info[i]->bb_free =
2247 ext4_free_blocks_after_init(sb, group, desc);
2248 } else {
2249 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002250 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002251 }
2252
2253 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002254 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002255 meta_group_info[i]->bb_free_root = RB_ROOT;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002256
2257#ifdef DOUBLE_CHECK
2258 {
2259 struct buffer_head *bh;
2260 meta_group_info[i]->bb_bitmap =
2261 kmalloc(sb->s_blocksize, GFP_KERNEL);
2262 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2263 bh = ext4_read_block_bitmap(sb, group);
2264 BUG_ON(bh == NULL);
2265 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2266 sb->s_blocksize);
2267 put_bh(bh);
2268 }
2269#endif
2270
2271 return 0;
2272
2273exit_group_info:
2274 /* If a meta_group_info table has been allocated, release it now */
2275 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2276 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2277exit_meta_group_info:
2278 return -ENOMEM;
2279} /* ext4_mb_add_groupinfo */
2280
Alex Tomasc9de5602008-01-29 00:19:52 -05002281static int ext4_mb_init_backend(struct super_block *sb)
2282{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002283 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002284 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002285 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002286 struct ext4_super_block *es = sbi->s_es;
2287 int num_meta_group_infos;
2288 int num_meta_group_infos_max;
2289 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002290 struct ext4_group_desc *desc;
Alex Tomasc9de5602008-01-29 00:19:52 -05002291
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002292 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002293 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002294 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2295
2296 /*
2297 * This is the total number of blocks used by GDT including
2298 * the number of reserved blocks for GDT.
2299 * The s_group_info array is allocated with this value
2300 * to allow a clean online resize without a complex
2301 * manipulation of pointer.
2302 * The drawback is the unused memory when no resize
2303 * occurs but it's very low in terms of pages
2304 * (see comments below)
2305 * Need to handle this properly when META_BG resizing is allowed
2306 */
2307 num_meta_group_infos_max = num_meta_group_infos +
2308 le16_to_cpu(es->s_reserved_gdt_blocks);
2309
2310 /*
2311 * array_size is the size of s_group_info array. We round it
2312 * to the next power of two because this approximation is done
2313 * internally by kmalloc so we can have some more memory
2314 * for free here (e.g. may be used for META_BG resize).
2315 */
2316 array_size = 1;
2317 while (array_size < sizeof(*sbi->s_group_info) *
2318 num_meta_group_infos_max)
2319 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002320 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2321 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2322 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002323 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002324 if (sbi->s_group_info == NULL) {
2325 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2326 return -ENOMEM;
2327 }
2328 sbi->s_buddy_cache = new_inode(sb);
2329 if (sbi->s_buddy_cache == NULL) {
2330 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2331 goto err_freesgi;
2332 }
2333 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002334 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002335 desc = ext4_get_group_desc(sb, i, NULL);
2336 if (desc == NULL) {
2337 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002338 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002339 goto err_freebuddy;
2340 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002341 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2342 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002343 }
2344
2345 return 0;
2346
2347err_freebuddy:
Roel Kluinf1fa3342008-04-29 22:01:15 -04002348 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002349 kfree(ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002350 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002351 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002352 kfree(sbi->s_group_info[i]);
2353 iput(sbi->s_buddy_cache);
2354err_freesgi:
2355 kfree(sbi->s_group_info);
2356 return -ENOMEM;
2357}
2358
2359int ext4_mb_init(struct super_block *sb, int needs_recovery)
2360{
2361 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002362 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002363 unsigned offset;
2364 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002365 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002366
Eric Sandeen19278052009-08-25 22:36:25 -04002367 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002368
2369 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2370 if (sbi->s_mb_offsets == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002371 return -ENOMEM;
2372 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002373
Eric Sandeen19278052009-08-25 22:36:25 -04002374 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002375 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2376 if (sbi->s_mb_maxs == NULL) {
Dan Carpentera7b19442009-03-27 19:42:54 -04002377 kfree(sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002378 return -ENOMEM;
2379 }
2380
2381 /* order 0 is regular bitmap */
2382 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2383 sbi->s_mb_offsets[0] = 0;
2384
2385 i = 1;
2386 offset = 0;
2387 max = sb->s_blocksize << 2;
2388 do {
2389 sbi->s_mb_offsets[i] = offset;
2390 sbi->s_mb_maxs[i] = max;
2391 offset += 1 << (sb->s_blocksize_bits - i);
2392 max = max >> 1;
2393 i++;
2394 } while (i <= sb->s_blocksize_bits + 1);
2395
2396 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002397 ret = ext4_mb_init_backend(sb);
2398 if (ret != 0) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002399 kfree(sbi->s_mb_offsets);
2400 kfree(sbi->s_mb_maxs);
Shen Feng74767c52008-07-11 19:27:31 -04002401 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002402 }
2403
2404 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002405 spin_lock_init(&sbi->s_bal_lock);
2406
2407 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2408 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2409 sbi->s_mb_stats = MB_DEFAULT_STATS;
2410 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2411 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002412 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2413
Eric Sandeen730c2132008-09-13 15:23:29 -04002414 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002415 if (sbi->s_locality_groups == NULL) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002416 kfree(sbi->s_mb_offsets);
2417 kfree(sbi->s_mb_maxs);
2418 return -ENOMEM;
2419 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002420 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002421 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002422 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002423 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002424 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2425 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002426 spin_lock_init(&lg->lg_prealloc_lock);
2427 }
2428
Theodore Ts'o296c3552009-09-30 00:32:42 -04002429 if (sbi->s_proc)
2430 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2431 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002432
Frank Mayhar03901312009-01-07 00:06:22 -05002433 if (sbi->s_journal)
2434 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Alex Tomasc9de5602008-01-29 00:19:52 -05002435 return 0;
2436}
2437
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002438/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002439static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2440{
2441 struct ext4_prealloc_space *pa;
2442 struct list_head *cur, *tmp;
2443 int count = 0;
2444
2445 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2446 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2447 list_del(&pa->pa_group_list);
2448 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002449 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002450 }
2451 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002452 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002453
2454}
2455
2456int ext4_mb_release(struct super_block *sb)
2457{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002458 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002459 ext4_group_t i;
2460 int num_meta_group_infos;
2461 struct ext4_group_info *grinfo;
2462 struct ext4_sb_info *sbi = EXT4_SB(sb);
2463
Alex Tomasc9de5602008-01-29 00:19:52 -05002464 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002465 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002466 grinfo = ext4_get_group_info(sb, i);
2467#ifdef DOUBLE_CHECK
2468 kfree(grinfo->bb_bitmap);
2469#endif
2470 ext4_lock_group(sb, i);
2471 ext4_mb_cleanup_pa(grinfo);
2472 ext4_unlock_group(sb, i);
2473 kfree(grinfo);
2474 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002475 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002476 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2477 EXT4_DESC_PER_BLOCK_BITS(sb);
2478 for (i = 0; i < num_meta_group_infos; i++)
2479 kfree(sbi->s_group_info[i]);
2480 kfree(sbi->s_group_info);
2481 }
2482 kfree(sbi->s_mb_offsets);
2483 kfree(sbi->s_mb_maxs);
2484 if (sbi->s_buddy_cache)
2485 iput(sbi->s_buddy_cache);
2486 if (sbi->s_mb_stats) {
2487 printk(KERN_INFO
2488 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2489 atomic_read(&sbi->s_bal_allocated),
2490 atomic_read(&sbi->s_bal_reqs),
2491 atomic_read(&sbi->s_bal_success));
2492 printk(KERN_INFO
2493 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2494 "%u 2^N hits, %u breaks, %u lost\n",
2495 atomic_read(&sbi->s_bal_ex_scanned),
2496 atomic_read(&sbi->s_bal_goals),
2497 atomic_read(&sbi->s_bal_2orders),
2498 atomic_read(&sbi->s_bal_breaks),
2499 atomic_read(&sbi->s_mb_lost_chunks));
2500 printk(KERN_INFO
2501 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2502 sbi->s_mb_buddies_generated++,
2503 sbi->s_mb_generation_time);
2504 printk(KERN_INFO
2505 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2506 atomic_read(&sbi->s_mb_preallocated),
2507 atomic_read(&sbi->s_mb_discarded));
2508 }
2509
Eric Sandeen730c2132008-09-13 15:23:29 -04002510 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002511 if (sbi->s_proc)
2512 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002513
2514 return 0;
2515}
2516
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002517/*
2518 * This function is called by the jbd2 layer once the commit has finished,
2519 * so we know we can free the blocks that were released with that commit.
2520 */
2521static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002522{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002523 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002524 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002525 struct ext4_group_info *db;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002526 int err, count = 0, count2 = 0;
2527 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002528 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002529
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002530 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2531 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002532
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002533 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002534 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002535
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002536 if (test_opt(sb, DISCARD)) {
Eric Sandeena30eec22010-05-16 03:00:00 -04002537 int ret;
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002538 ext4_fsblk_t discard_block;
2539
2540 discard_block = entry->start_blk +
2541 ext4_group_first_block_no(sb, entry->group);
2542 trace_ext4_discard_blocks(sb,
2543 (unsigned long long)discard_block,
2544 entry->count);
Eric Sandeena30eec22010-05-16 03:00:00 -04002545 ret = sb_issue_discard(sb, discard_block, entry->count);
2546 if (ret == EOPNOTSUPP) {
2547 ext4_warning(sb,
2548 "discard not supported, disabling");
2549 clear_opt(EXT4_SB(sb)->s_mount_opt, DISCARD);
2550 }
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002551 }
2552
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002553 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002554 /* we expect to find existing buddy because it's pinned */
2555 BUG_ON(err != 0);
2556
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002557 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002558 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002559 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002560 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002561 ext4_lock_group(sb, entry->group);
2562 /* Take it out of per group rb tree */
2563 rb_erase(&entry->node, &(db->bb_free_root));
2564 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2565
2566 if (!db->bb_free_root.rb_node) {
2567 /* No more items in the per group rb tree
2568 * balance refcounts from ext4_mb_free_metadata()
2569 */
2570 page_cache_release(e4b.bd_buddy_page);
2571 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002572 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002573 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002574 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002575 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002576 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002577
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002578 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002579}
2580
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002581#ifdef CONFIG_EXT4_DEBUG
2582u8 mb_enable_debug __read_mostly;
2583
2584static struct dentry *debugfs_dir;
2585static struct dentry *debugfs_debug;
2586
2587static void __init ext4_create_debugfs_entry(void)
2588{
2589 debugfs_dir = debugfs_create_dir("ext4", NULL);
2590 if (debugfs_dir)
2591 debugfs_debug = debugfs_create_u8("mballoc-debug",
2592 S_IRUGO | S_IWUSR,
2593 debugfs_dir,
2594 &mb_enable_debug);
2595}
2596
2597static void ext4_remove_debugfs_entry(void)
2598{
2599 debugfs_remove(debugfs_debug);
2600 debugfs_remove(debugfs_dir);
2601}
2602
2603#else
2604
2605static void __init ext4_create_debugfs_entry(void)
2606{
2607}
2608
2609static void ext4_remove_debugfs_entry(void)
2610{
2611}
2612
2613#endif
2614
Alex Tomasc9de5602008-01-29 00:19:52 -05002615int __init init_ext4_mballoc(void)
2616{
2617 ext4_pspace_cachep =
2618 kmem_cache_create("ext4_prealloc_space",
2619 sizeof(struct ext4_prealloc_space),
2620 0, SLAB_RECLAIM_ACCOUNT, NULL);
2621 if (ext4_pspace_cachep == NULL)
2622 return -ENOMEM;
2623
Eric Sandeen256bdb42008-02-10 01:13:33 -05002624 ext4_ac_cachep =
2625 kmem_cache_create("ext4_alloc_context",
2626 sizeof(struct ext4_allocation_context),
2627 0, SLAB_RECLAIM_ACCOUNT, NULL);
2628 if (ext4_ac_cachep == NULL) {
2629 kmem_cache_destroy(ext4_pspace_cachep);
2630 return -ENOMEM;
2631 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002632
2633 ext4_free_ext_cachep =
2634 kmem_cache_create("ext4_free_block_extents",
2635 sizeof(struct ext4_free_data),
2636 0, SLAB_RECLAIM_ACCOUNT, NULL);
2637 if (ext4_free_ext_cachep == NULL) {
2638 kmem_cache_destroy(ext4_pspace_cachep);
2639 kmem_cache_destroy(ext4_ac_cachep);
2640 return -ENOMEM;
2641 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002642 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002643 return 0;
2644}
2645
2646void exit_ext4_mballoc(void)
2647{
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002648 /*
2649 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2650 * before destroying the slab cache.
2651 */
2652 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002653 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002654 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002655 kmem_cache_destroy(ext4_free_ext_cachep);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002656 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002657}
2658
2659
2660/*
2661 * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2662 * Returns 0 if success or error code
2663 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002664static noinline_for_stack int
2665ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002666 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002667{
2668 struct buffer_head *bitmap_bh = NULL;
2669 struct ext4_super_block *es;
2670 struct ext4_group_desc *gdp;
2671 struct buffer_head *gdp_bh;
2672 struct ext4_sb_info *sbi;
2673 struct super_block *sb;
2674 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002675 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002676
2677 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2678 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2679
2680 sb = ac->ac_sb;
2681 sbi = EXT4_SB(sb);
2682 es = sbi->s_es;
2683
Alex Tomasc9de5602008-01-29 00:19:52 -05002684
2685 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002686 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 if (!bitmap_bh)
2688 goto out_err;
2689
2690 err = ext4_journal_get_write_access(handle, bitmap_bh);
2691 if (err)
2692 goto out_err;
2693
2694 err = -EIO;
2695 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2696 if (!gdp)
2697 goto out_err;
2698
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002699 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002700 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002701
Alex Tomasc9de5602008-01-29 00:19:52 -05002702 err = ext4_journal_get_write_access(handle, gdp_bh);
2703 if (err)
2704 goto out_err;
2705
Akinobu Mitabda00de2010-03-03 23:53:25 -05002706 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002707
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002708 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002709 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002710 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002711 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002712 /* File system mounted not to panic on error
2713 * Fix the bitmap and repeat the block allocation
2714 * We leak some of the blocks here.
2715 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002716 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2717 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2718 ac->ac_b_ex.fe_len);
2719 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002720 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002721 if (!err)
2722 err = -EAGAIN;
2723 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002724 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002725
2726 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002727#ifdef AGGRESSIVE_CHECK
2728 {
2729 int i;
2730 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2731 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2732 bitmap_bh->b_data));
2733 }
2734 }
2735#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002736 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 -05002737 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2738 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002739 ext4_free_blks_set(sb, gdp,
2740 ext4_free_blocks_after_init(sb,
2741 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002742 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002743 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2744 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002745 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002746
2747 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002748 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002749 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002750 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002751 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002752 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2753 /* release all the reserved blocks if non delalloc */
2754 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002755
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002756 if (sbi->s_log_groups_per_flex) {
2757 ext4_group_t flex_group = ext4_flex_group(sbi,
2758 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002759 atomic_sub(ac->ac_b_ex.fe_len,
2760 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002761 }
2762
Frank Mayhar03901312009-01-07 00:06:22 -05002763 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002764 if (err)
2765 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002766 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002767
2768out_err:
2769 sb->s_dirt = 1;
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002770 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002771 return err;
2772}
2773
2774/*
2775 * here we normalize request for locality group
2776 * Group request are normalized to s_strip size if we set the same via mount
2777 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002778 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002779 *
2780 * XXX: should we try to preallocate more than the group has now?
2781 */
2782static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2783{
2784 struct super_block *sb = ac->ac_sb;
2785 struct ext4_locality_group *lg = ac->ac_lg;
2786
2787 BUG_ON(lg == NULL);
2788 if (EXT4_SB(sb)->s_stripe)
2789 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2790 else
2791 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002792 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002793 current->pid, ac->ac_g_ex.fe_len);
2794}
2795
2796/*
2797 * Normalization means making request better in terms of
2798 * size and alignment
2799 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002800static noinline_for_stack void
2801ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002802 struct ext4_allocation_request *ar)
2803{
2804 int bsbits, max;
2805 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002806 loff_t size, orig_size, start_off;
2807 ext4_lblk_t start, orig_start;
2808 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002809 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002810
2811 /* do normalize only data requests, metadata requests
2812 do not need preallocation */
2813 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2814 return;
2815
2816 /* sometime caller may want exact blocks */
2817 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2818 return;
2819
2820 /* caller may indicate that preallocation isn't
2821 * required (it's a tail, for example) */
2822 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2823 return;
2824
2825 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2826 ext4_mb_normalize_group_request(ac);
2827 return ;
2828 }
2829
2830 bsbits = ac->ac_sb->s_blocksize_bits;
2831
2832 /* first, let's learn actual file size
2833 * given current request is allocated */
2834 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2835 size = size << bsbits;
2836 if (size < i_size_read(ac->ac_inode))
2837 size = i_size_read(ac->ac_inode);
2838
Valerie Clement19304792008-05-13 19:31:14 -04002839 /* max size of free chunks */
2840 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002841
Valerie Clement19304792008-05-13 19:31:14 -04002842#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2843 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002844
2845 /* first, try to predict filesize */
2846 /* XXX: should this table be tunable? */
2847 start_off = 0;
2848 if (size <= 16 * 1024) {
2849 size = 16 * 1024;
2850 } else if (size <= 32 * 1024) {
2851 size = 32 * 1024;
2852 } else if (size <= 64 * 1024) {
2853 size = 64 * 1024;
2854 } else if (size <= 128 * 1024) {
2855 size = 128 * 1024;
2856 } else if (size <= 256 * 1024) {
2857 size = 256 * 1024;
2858 } else if (size <= 512 * 1024) {
2859 size = 512 * 1024;
2860 } else if (size <= 1024 * 1024) {
2861 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002862 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002863 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002864 (21 - bsbits)) << 21;
2865 size = 2 * 1024 * 1024;
2866 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002867 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2868 (22 - bsbits)) << 22;
2869 size = 4 * 1024 * 1024;
2870 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002871 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002872 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2873 (23 - bsbits)) << 23;
2874 size = 8 * 1024 * 1024;
2875 } else {
2876 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2877 size = ac->ac_o_ex.fe_len << bsbits;
2878 }
2879 orig_size = size = size >> bsbits;
2880 orig_start = start = start_off >> bsbits;
2881
2882 /* don't cover already allocated blocks in selected range */
2883 if (ar->pleft && start <= ar->lleft) {
2884 size -= ar->lleft + 1 - start;
2885 start = ar->lleft + 1;
2886 }
2887 if (ar->pright && start + size - 1 >= ar->lright)
2888 size -= start + size - ar->lright;
2889
2890 end = start + size;
2891
2892 /* check we don't cross already preallocated blocks */
2893 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002894 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002895 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002896
Alex Tomasc9de5602008-01-29 00:19:52 -05002897 if (pa->pa_deleted)
2898 continue;
2899 spin_lock(&pa->pa_lock);
2900 if (pa->pa_deleted) {
2901 spin_unlock(&pa->pa_lock);
2902 continue;
2903 }
2904
2905 pa_end = pa->pa_lstart + pa->pa_len;
2906
2907 /* PA must not overlap original request */
2908 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2909 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2910
Eric Sandeen38877f42009-08-17 23:55:24 -04002911 /* skip PAs this normalized request doesn't overlap with */
2912 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002913 spin_unlock(&pa->pa_lock);
2914 continue;
2915 }
2916 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2917
Eric Sandeen38877f42009-08-17 23:55:24 -04002918 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002919 if (pa_end <= ac->ac_o_ex.fe_logical) {
2920 BUG_ON(pa_end < start);
2921 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002922 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 BUG_ON(pa->pa_lstart > end);
2924 end = pa->pa_lstart;
2925 }
2926 spin_unlock(&pa->pa_lock);
2927 }
2928 rcu_read_unlock();
2929 size = end - start;
2930
2931 /* XXX: extra loop to check we really don't overlap preallocations */
2932 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002933 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002934 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002935 spin_lock(&pa->pa_lock);
2936 if (pa->pa_deleted == 0) {
2937 pa_end = pa->pa_lstart + pa->pa_len;
2938 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2939 }
2940 spin_unlock(&pa->pa_lock);
2941 }
2942 rcu_read_unlock();
2943
2944 if (start + size <= ac->ac_o_ex.fe_logical &&
2945 start > ac->ac_o_ex.fe_logical) {
2946 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
2947 (unsigned long) start, (unsigned long) size,
2948 (unsigned long) ac->ac_o_ex.fe_logical);
2949 }
2950 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
2951 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04002952 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002953
2954 /* now prepare goal request */
2955
2956 /* XXX: is it better to align blocks WRT to logical
2957 * placement or satisfy big request as is */
2958 ac->ac_g_ex.fe_logical = start;
2959 ac->ac_g_ex.fe_len = size;
2960
2961 /* define goal start in order to merge */
2962 if (ar->pright && (ar->lright == (start + size))) {
2963 /* merge to the right */
2964 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
2965 &ac->ac_f_ex.fe_group,
2966 &ac->ac_f_ex.fe_start);
2967 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2968 }
2969 if (ar->pleft && (ar->lleft + 1 == start)) {
2970 /* merge to the left */
2971 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
2972 &ac->ac_f_ex.fe_group,
2973 &ac->ac_f_ex.fe_start);
2974 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
2975 }
2976
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002977 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05002978 (unsigned) orig_size, (unsigned) start);
2979}
2980
2981static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
2982{
2983 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2984
2985 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
2986 atomic_inc(&sbi->s_bal_reqs);
2987 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
2988 if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
2989 atomic_inc(&sbi->s_bal_success);
2990 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
2991 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2992 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2993 atomic_inc(&sbi->s_bal_goals);
2994 if (ac->ac_found > sbi->s_mb_max_to_scan)
2995 atomic_inc(&sbi->s_bal_breaks);
2996 }
2997
Theodore Ts'o296c3552009-09-30 00:32:42 -04002998 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
2999 trace_ext4_mballoc_alloc(ac);
3000 else
3001 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003002}
3003
3004/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003005 * Called on failure; free up any blocks from the inode PA for this
3006 * context. We don't need this for MB_GROUP_PA because we only change
3007 * pa_free in ext4_mb_release_context(), but on failure, we've already
3008 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3009 */
3010static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3011{
3012 struct ext4_prealloc_space *pa = ac->ac_pa;
3013 int len;
3014
3015 if (pa && pa->pa_type == MB_INODE_PA) {
3016 len = ac->ac_b_ex.fe_len;
3017 pa->pa_free += len;
3018 }
3019
3020}
3021
3022/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003023 * use blocks preallocated to inode
3024 */
3025static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3026 struct ext4_prealloc_space *pa)
3027{
3028 ext4_fsblk_t start;
3029 ext4_fsblk_t end;
3030 int len;
3031
3032 /* found preallocated blocks, use them */
3033 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3034 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3035 len = end - start;
3036 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3037 &ac->ac_b_ex.fe_start);
3038 ac->ac_b_ex.fe_len = len;
3039 ac->ac_status = AC_STATUS_FOUND;
3040 ac->ac_pa = pa;
3041
3042 BUG_ON(start < pa->pa_pstart);
3043 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3044 BUG_ON(pa->pa_free < len);
3045 pa->pa_free -= len;
3046
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003047 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003048}
3049
3050/*
3051 * use blocks preallocated to locality group
3052 */
3053static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3054 struct ext4_prealloc_space *pa)
3055{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003056 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003057
Alex Tomasc9de5602008-01-29 00:19:52 -05003058 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3059 &ac->ac_b_ex.fe_group,
3060 &ac->ac_b_ex.fe_start);
3061 ac->ac_b_ex.fe_len = len;
3062 ac->ac_status = AC_STATUS_FOUND;
3063 ac->ac_pa = pa;
3064
3065 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003066 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003067 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003068 * in on-disk bitmap -- see ext4_mb_release_context()
3069 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003070 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003071 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003072}
3073
3074/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003075 * Return the prealloc space that have minimal distance
3076 * from the goal block. @cpa is the prealloc
3077 * space that is having currently known minimal distance
3078 * from the goal block.
3079 */
3080static struct ext4_prealloc_space *
3081ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3082 struct ext4_prealloc_space *pa,
3083 struct ext4_prealloc_space *cpa)
3084{
3085 ext4_fsblk_t cur_distance, new_distance;
3086
3087 if (cpa == NULL) {
3088 atomic_inc(&pa->pa_count);
3089 return pa;
3090 }
3091 cur_distance = abs(goal_block - cpa->pa_pstart);
3092 new_distance = abs(goal_block - pa->pa_pstart);
3093
3094 if (cur_distance < new_distance)
3095 return cpa;
3096
3097 /* drop the previous reference */
3098 atomic_dec(&cpa->pa_count);
3099 atomic_inc(&pa->pa_count);
3100 return pa;
3101}
3102
3103/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003104 * search goal blocks in preallocated space
3105 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003106static noinline_for_stack int
3107ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003108{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003109 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003110 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3111 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003112 struct ext4_prealloc_space *pa, *cpa = NULL;
3113 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003114
3115 /* only data can be preallocated */
3116 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3117 return 0;
3118
3119 /* first, try per-file preallocation */
3120 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003121 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003122
3123 /* all fields in this condition don't change,
3124 * so we can skip locking for them */
3125 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3126 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3127 continue;
3128
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003129 /* non-extent files can't have physical blocks past 2^32 */
3130 if (!(EXT4_I(ac->ac_inode)->i_flags & EXT4_EXTENTS_FL) &&
3131 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3132 continue;
3133
Alex Tomasc9de5602008-01-29 00:19:52 -05003134 /* found preallocated blocks, use them */
3135 spin_lock(&pa->pa_lock);
3136 if (pa->pa_deleted == 0 && pa->pa_free) {
3137 atomic_inc(&pa->pa_count);
3138 ext4_mb_use_inode_pa(ac, pa);
3139 spin_unlock(&pa->pa_lock);
3140 ac->ac_criteria = 10;
3141 rcu_read_unlock();
3142 return 1;
3143 }
3144 spin_unlock(&pa->pa_lock);
3145 }
3146 rcu_read_unlock();
3147
3148 /* can we use group allocation? */
3149 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3150 return 0;
3151
3152 /* inode may have no locality group for some reason */
3153 lg = ac->ac_lg;
3154 if (lg == NULL)
3155 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003156 order = fls(ac->ac_o_ex.fe_len) - 1;
3157 if (order > PREALLOC_TB_SIZE - 1)
3158 /* The max size of hash table is PREALLOC_TB_SIZE */
3159 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003160
Akinobu Mitabda00de2010-03-03 23:53:25 -05003161 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003162 /*
3163 * search for the prealloc space that is having
3164 * minimal distance from the goal block.
3165 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003166 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3167 rcu_read_lock();
3168 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3169 pa_inode_list) {
3170 spin_lock(&pa->pa_lock);
3171 if (pa->pa_deleted == 0 &&
3172 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003173
3174 cpa = ext4_mb_check_group_pa(goal_block,
3175 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003176 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003177 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003178 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003179 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003180 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003181 if (cpa) {
3182 ext4_mb_use_group_pa(ac, cpa);
3183 ac->ac_criteria = 20;
3184 return 1;
3185 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003186 return 0;
3187}
3188
3189/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003190 * the function goes through all block freed in the group
3191 * but not yet committed and marks them used in in-core bitmap.
3192 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003193 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003194 */
3195static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3196 ext4_group_t group)
3197{
3198 struct rb_node *n;
3199 struct ext4_group_info *grp;
3200 struct ext4_free_data *entry;
3201
3202 grp = ext4_get_group_info(sb, group);
3203 n = rb_first(&(grp->bb_free_root));
3204
3205 while (n) {
3206 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003207 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003208 n = rb_next(n);
3209 }
3210 return;
3211}
3212
3213/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003214 * the function goes through all preallocation in this group and marks them
3215 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003216 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003217 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003218static noinline_for_stack
3219void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003220 ext4_group_t group)
3221{
3222 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3223 struct ext4_prealloc_space *pa;
3224 struct list_head *cur;
3225 ext4_group_t groupnr;
3226 ext4_grpblk_t start;
3227 int preallocated = 0;
3228 int count = 0;
3229 int len;
3230
3231 /* all form of preallocation discards first load group,
3232 * so the only competing code is preallocation use.
3233 * we don't need any locking here
3234 * notice we do NOT ignore preallocations with pa_deleted
3235 * otherwise we could leave used blocks available for
3236 * allocation in buddy when concurrent ext4_mb_put_pa()
3237 * is dropping preallocation
3238 */
3239 list_for_each(cur, &grp->bb_prealloc_list) {
3240 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3241 spin_lock(&pa->pa_lock);
3242 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3243 &groupnr, &start);
3244 len = pa->pa_len;
3245 spin_unlock(&pa->pa_lock);
3246 if (unlikely(len == 0))
3247 continue;
3248 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003249 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003250 preallocated += len;
3251 count++;
3252 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003253 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003254}
3255
3256static void ext4_mb_pa_callback(struct rcu_head *head)
3257{
3258 struct ext4_prealloc_space *pa;
3259 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3260 kmem_cache_free(ext4_pspace_cachep, pa);
3261}
3262
3263/*
3264 * drops a reference to preallocated space descriptor
3265 * if this was the last reference and the space is consumed
3266 */
3267static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3268 struct super_block *sb, struct ext4_prealloc_space *pa)
3269{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003270 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003271 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003272
3273 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3274 return;
3275
3276 /* in this short window concurrent discard can set pa_deleted */
3277 spin_lock(&pa->pa_lock);
3278 if (pa->pa_deleted == 1) {
3279 spin_unlock(&pa->pa_lock);
3280 return;
3281 }
3282
3283 pa->pa_deleted = 1;
3284 spin_unlock(&pa->pa_lock);
3285
Eric Sandeend33a1972009-03-16 23:25:40 -04003286 grp_blk = pa->pa_pstart;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003287 /*
3288 * If doing group-based preallocation, pa_pstart may be in the
3289 * next group when pa is used up
3290 */
3291 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003292 grp_blk--;
3293
3294 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003295
3296 /*
3297 * possible race:
3298 *
3299 * P1 (buddy init) P2 (regular allocation)
3300 * find block B in PA
3301 * copy on-disk bitmap to buddy
3302 * mark B in on-disk bitmap
3303 * drop PA from group
3304 * mark all PAs in buddy
3305 *
3306 * thus, P1 initializes buddy with B available. to prevent this
3307 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3308 * against that pair
3309 */
3310 ext4_lock_group(sb, grp);
3311 list_del(&pa->pa_group_list);
3312 ext4_unlock_group(sb, grp);
3313
3314 spin_lock(pa->pa_obj_lock);
3315 list_del_rcu(&pa->pa_inode_list);
3316 spin_unlock(pa->pa_obj_lock);
3317
3318 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3319}
3320
3321/*
3322 * creates new preallocated space for given inode
3323 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003324static noinline_for_stack int
3325ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003326{
3327 struct super_block *sb = ac->ac_sb;
3328 struct ext4_prealloc_space *pa;
3329 struct ext4_group_info *grp;
3330 struct ext4_inode_info *ei;
3331
3332 /* preallocate only when found space is larger then requested */
3333 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3334 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3335 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3336
3337 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3338 if (pa == NULL)
3339 return -ENOMEM;
3340
3341 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3342 int winl;
3343 int wins;
3344 int win;
3345 int offs;
3346
3347 /* we can't allocate as much as normalizer wants.
3348 * so, found space must get proper lstart
3349 * to cover original request */
3350 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3351 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3352
3353 /* we're limited by original request in that
3354 * logical block must be covered any way
3355 * winl is window we can move our chunk within */
3356 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3357
3358 /* also, we should cover whole original request */
3359 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3360
3361 /* the smallest one defines real window */
3362 win = min(winl, wins);
3363
3364 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3365 if (offs && offs < win)
3366 win = offs;
3367
3368 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3369 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3370 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3371 }
3372
3373 /* preallocation can change ac_b_ex, thus we store actually
3374 * allocated blocks for history */
3375 ac->ac_f_ex = ac->ac_b_ex;
3376
3377 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3378 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3379 pa->pa_len = ac->ac_b_ex.fe_len;
3380 pa->pa_free = pa->pa_len;
3381 atomic_set(&pa->pa_count, 1);
3382 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003383 INIT_LIST_HEAD(&pa->pa_inode_list);
3384 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003385 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003386 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003387
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003388 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003389 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003390 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003391
3392 ext4_mb_use_inode_pa(ac, pa);
3393 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3394
3395 ei = EXT4_I(ac->ac_inode);
3396 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3397
3398 pa->pa_obj_lock = &ei->i_prealloc_lock;
3399 pa->pa_inode = ac->ac_inode;
3400
3401 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3402 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3403 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3404
3405 spin_lock(pa->pa_obj_lock);
3406 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3407 spin_unlock(pa->pa_obj_lock);
3408
3409 return 0;
3410}
3411
3412/*
3413 * creates new preallocated space for locality group inodes belongs to
3414 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003415static noinline_for_stack int
3416ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003417{
3418 struct super_block *sb = ac->ac_sb;
3419 struct ext4_locality_group *lg;
3420 struct ext4_prealloc_space *pa;
3421 struct ext4_group_info *grp;
3422
3423 /* preallocate only when found space is larger then requested */
3424 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3425 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3426 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3427
3428 BUG_ON(ext4_pspace_cachep == NULL);
3429 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3430 if (pa == NULL)
3431 return -ENOMEM;
3432
3433 /* preallocation can change ac_b_ex, thus we store actually
3434 * allocated blocks for history */
3435 ac->ac_f_ex = ac->ac_b_ex;
3436
3437 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3438 pa->pa_lstart = pa->pa_pstart;
3439 pa->pa_len = ac->ac_b_ex.fe_len;
3440 pa->pa_free = pa->pa_len;
3441 atomic_set(&pa->pa_count, 1);
3442 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003443 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003444 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003445 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003446 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003447
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003448 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003449 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3450 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003451
3452 ext4_mb_use_group_pa(ac, pa);
3453 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3454
3455 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3456 lg = ac->ac_lg;
3457 BUG_ON(lg == NULL);
3458
3459 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3460 pa->pa_inode = NULL;
3461
3462 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3463 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3464 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3465
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003466 /*
3467 * We will later add the new pa to the right bucket
3468 * after updating the pa_free in ext4_mb_release_context
3469 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003470 return 0;
3471}
3472
3473static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3474{
3475 int err;
3476
3477 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3478 err = ext4_mb_new_group_pa(ac);
3479 else
3480 err = ext4_mb_new_inode_pa(ac);
3481 return err;
3482}
3483
3484/*
3485 * finds all unused blocks in on-disk bitmap, frees them in
3486 * in-core bitmap and buddy.
3487 * @pa must be unlinked from inode and group lists, so that
3488 * nobody else can find/use it.
3489 * the caller MUST hold group/inode locks.
3490 * TODO: optimize the case when there are no in-core structures yet
3491 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003492static noinline_for_stack int
3493ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003494 struct ext4_prealloc_space *pa,
3495 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003496{
Alex Tomasc9de5602008-01-29 00:19:52 -05003497 struct super_block *sb = e4b->bd_sb;
3498 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003499 unsigned int end;
3500 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003501 ext4_group_t group;
3502 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003503 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003504 sector_t start;
3505 int err = 0;
3506 int free = 0;
3507
3508 BUG_ON(pa->pa_deleted == 0);
3509 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003510 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003511 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3512 end = bit + pa->pa_len;
3513
Eric Sandeen256bdb42008-02-10 01:13:33 -05003514 if (ac) {
3515 ac->ac_sb = sb;
3516 ac->ac_inode = pa->pa_inode;
Eric Sandeen256bdb42008-02-10 01:13:33 -05003517 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003518
3519 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003520 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003521 if (bit >= end)
3522 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003523 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Akinobu Mita5661bd62010-03-03 23:53:39 -05003524 start = ext4_group_first_block_no(sb, group) + bit;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003525 mb_debug(1, " free preallocated %u/%u in group %u\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003526 (unsigned) start, (unsigned) next - bit,
3527 (unsigned) group);
3528 free += next - bit;
3529
Eric Sandeen256bdb42008-02-10 01:13:33 -05003530 if (ac) {
3531 ac->ac_b_ex.fe_group = group;
3532 ac->ac_b_ex.fe_start = bit;
3533 ac->ac_b_ex.fe_len = next - bit;
3534 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003535 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003536 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003537
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003538 trace_ext4_mb_release_inode_pa(ac, pa, grp_blk_start + bit,
3539 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003540 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3541 bit = next + 1;
3542 }
3543 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003544 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003545 pa, (unsigned long) pa->pa_lstart,
3546 (unsigned long) pa->pa_pstart,
3547 (unsigned long) pa->pa_len);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003548 ext4_grp_locked_error(sb, group,
3549 __func__, "free %u, pa_free %u",
3550 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003551 /*
3552 * pa is already deleted so we use the value obtained
3553 * from the bitmap and continue.
3554 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003555 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003556 atomic_add(free, &sbi->s_mb_discarded);
3557
3558 return err;
3559}
3560
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003561static noinline_for_stack int
3562ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003563 struct ext4_prealloc_space *pa,
3564 struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003565{
Alex Tomasc9de5602008-01-29 00:19:52 -05003566 struct super_block *sb = e4b->bd_sb;
3567 ext4_group_t group;
3568 ext4_grpblk_t bit;
3569
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003570 trace_ext4_mb_release_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003571 BUG_ON(pa->pa_deleted == 0);
3572 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3573 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3574 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3575 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3576
Eric Sandeen256bdb42008-02-10 01:13:33 -05003577 if (ac) {
3578 ac->ac_sb = sb;
3579 ac->ac_inode = NULL;
3580 ac->ac_b_ex.fe_group = group;
3581 ac->ac_b_ex.fe_start = bit;
3582 ac->ac_b_ex.fe_len = pa->pa_len;
3583 ac->ac_b_ex.fe_logical = 0;
Theodore Ts'o296c3552009-09-30 00:32:42 -04003584 trace_ext4_mballoc_discard(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05003585 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003586
3587 return 0;
3588}
3589
3590/*
3591 * releases all preallocations in given group
3592 *
3593 * first, we need to decide discard policy:
3594 * - when do we discard
3595 * 1) ENOSPC
3596 * - how many do we discard
3597 * 1) how many requested
3598 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003599static noinline_for_stack int
3600ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003601 ext4_group_t group, int needed)
3602{
3603 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3604 struct buffer_head *bitmap_bh = NULL;
3605 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003606 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003607 struct list_head list;
3608 struct ext4_buddy e4b;
3609 int err;
3610 int busy = 0;
3611 int free = 0;
3612
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003613 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003614
3615 if (list_empty(&grp->bb_prealloc_list))
3616 return 0;
3617
Theodore Ts'o574ca172008-07-11 19:27:31 -04003618 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003619 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003620 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003621 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003622 }
3623
3624 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003625 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003626 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003627 put_bh(bitmap_bh);
3628 return 0;
3629 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003630
3631 if (needed == 0)
3632 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3633
Alex Tomasc9de5602008-01-29 00:19:52 -05003634 INIT_LIST_HEAD(&list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003635 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003636 if (ac)
3637 ac->ac_sb = sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05003638repeat:
3639 ext4_lock_group(sb, group);
3640 list_for_each_entry_safe(pa, tmp,
3641 &grp->bb_prealloc_list, pa_group_list) {
3642 spin_lock(&pa->pa_lock);
3643 if (atomic_read(&pa->pa_count)) {
3644 spin_unlock(&pa->pa_lock);
3645 busy = 1;
3646 continue;
3647 }
3648 if (pa->pa_deleted) {
3649 spin_unlock(&pa->pa_lock);
3650 continue;
3651 }
3652
3653 /* seems this one can be freed ... */
3654 pa->pa_deleted = 1;
3655
3656 /* we can trust pa_free ... */
3657 free += pa->pa_free;
3658
3659 spin_unlock(&pa->pa_lock);
3660
3661 list_del(&pa->pa_group_list);
3662 list_add(&pa->u.pa_tmp_list, &list);
3663 }
3664
3665 /* if we still need more blocks and some PAs were used, try again */
3666 if (free < needed && busy) {
3667 busy = 0;
3668 ext4_unlock_group(sb, group);
3669 /*
3670 * Yield the CPU here so that we don't get soft lockup
3671 * in non preempt case.
3672 */
3673 yield();
3674 goto repeat;
3675 }
3676
3677 /* found anything to free? */
3678 if (list_empty(&list)) {
3679 BUG_ON(free != 0);
3680 goto out;
3681 }
3682
3683 /* now free all selected PAs */
3684 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3685
3686 /* remove from object (inode or locality group) */
3687 spin_lock(pa->pa_obj_lock);
3688 list_del_rcu(&pa->pa_inode_list);
3689 spin_unlock(pa->pa_obj_lock);
3690
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003691 if (pa->pa_type == MB_GROUP_PA)
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003692 ext4_mb_release_group_pa(&e4b, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003693 else
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003694 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003695
3696 list_del(&pa->u.pa_tmp_list);
3697 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3698 }
3699
3700out:
3701 ext4_unlock_group(sb, group);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003702 if (ac)
3703 kmem_cache_free(ext4_ac_cachep, ac);
Jing Zhange39e07f2010-05-14 00:00:00 -04003704 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003705 put_bh(bitmap_bh);
3706 return free;
3707}
3708
3709/*
3710 * releases all non-used preallocated blocks for given inode
3711 *
3712 * It's important to discard preallocations under i_data_sem
3713 * We don't want another block to be served from the prealloc
3714 * space when we are discarding the inode prealloc space.
3715 *
3716 * FIXME!! Make sure it is valid at all the call sites
3717 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003718void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003719{
3720 struct ext4_inode_info *ei = EXT4_I(inode);
3721 struct super_block *sb = inode->i_sb;
3722 struct buffer_head *bitmap_bh = NULL;
3723 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003724 struct ext4_allocation_context *ac;
Alex Tomasc9de5602008-01-29 00:19:52 -05003725 ext4_group_t group = 0;
3726 struct list_head list;
3727 struct ext4_buddy e4b;
3728 int err;
3729
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003730 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003731 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3732 return;
3733 }
3734
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003735 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003736 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003737
3738 INIT_LIST_HEAD(&list);
3739
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003740 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003741 if (ac) {
3742 ac->ac_sb = sb;
3743 ac->ac_inode = inode;
3744 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003745repeat:
3746 /* first, collect all pa's in the inode */
3747 spin_lock(&ei->i_prealloc_lock);
3748 while (!list_empty(&ei->i_prealloc_list)) {
3749 pa = list_entry(ei->i_prealloc_list.next,
3750 struct ext4_prealloc_space, pa_inode_list);
3751 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3752 spin_lock(&pa->pa_lock);
3753 if (atomic_read(&pa->pa_count)) {
3754 /* this shouldn't happen often - nobody should
3755 * use preallocation while we're discarding it */
3756 spin_unlock(&pa->pa_lock);
3757 spin_unlock(&ei->i_prealloc_lock);
3758 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3759 WARN_ON(1);
3760 schedule_timeout_uninterruptible(HZ);
3761 goto repeat;
3762
3763 }
3764 if (pa->pa_deleted == 0) {
3765 pa->pa_deleted = 1;
3766 spin_unlock(&pa->pa_lock);
3767 list_del_rcu(&pa->pa_inode_list);
3768 list_add(&pa->u.pa_tmp_list, &list);
3769 continue;
3770 }
3771
3772 /* someone is deleting pa right now */
3773 spin_unlock(&pa->pa_lock);
3774 spin_unlock(&ei->i_prealloc_lock);
3775
3776 /* we have to wait here because pa_deleted
3777 * doesn't mean pa is already unlinked from
3778 * the list. as we might be called from
3779 * ->clear_inode() the inode will get freed
3780 * and concurrent thread which is unlinking
3781 * pa from inode's list may access already
3782 * freed memory, bad-bad-bad */
3783
3784 /* XXX: if this happens too often, we can
3785 * add a flag to force wait only in case
3786 * of ->clear_inode(), but not in case of
3787 * regular truncate */
3788 schedule_timeout_uninterruptible(HZ);
3789 goto repeat;
3790 }
3791 spin_unlock(&ei->i_prealloc_lock);
3792
3793 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003794 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003795 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3796
3797 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003798 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003799 ext4_error(sb, "Error loading buddy information for %u",
3800 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003801 continue;
3802 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003803
Theodore Ts'o574ca172008-07-11 19:27:31 -04003804 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003805 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003806 ext4_error(sb, "Error reading block bitmap for %u",
3807 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003808 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003809 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003810 }
3811
3812 ext4_lock_group(sb, group);
3813 list_del(&pa->pa_group_list);
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003814 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003815 ext4_unlock_group(sb, group);
3816
Jing Zhange39e07f2010-05-14 00:00:00 -04003817 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003818 put_bh(bitmap_bh);
3819
3820 list_del(&pa->u.pa_tmp_list);
3821 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3822 }
Aneesh Kumar K.Vc83617d2008-04-29 22:00:47 -04003823 if (ac)
3824 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003825}
3826
3827/*
3828 * finds all preallocated spaces and return blocks being freed to them
3829 * if preallocated space becomes full (no block is used from the space)
3830 * then the function frees space in buddy
3831 * XXX: at the moment, truncate (which is the only way to free blocks)
3832 * discards all preallocations
3833 */
3834static void ext4_mb_return_to_preallocation(struct inode *inode,
3835 struct ext4_buddy *e4b,
3836 sector_t block, int count)
3837{
3838 BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
3839}
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003840#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003841static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3842{
3843 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003844 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003845
3846 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3847 " Allocation context details:\n");
3848 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3849 ac->ac_status, ac->ac_flags);
3850 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3851 "best %lu/%lu/%lu@%lu cr %d\n",
3852 (unsigned long)ac->ac_o_ex.fe_group,
3853 (unsigned long)ac->ac_o_ex.fe_start,
3854 (unsigned long)ac->ac_o_ex.fe_len,
3855 (unsigned long)ac->ac_o_ex.fe_logical,
3856 (unsigned long)ac->ac_g_ex.fe_group,
3857 (unsigned long)ac->ac_g_ex.fe_start,
3858 (unsigned long)ac->ac_g_ex.fe_len,
3859 (unsigned long)ac->ac_g_ex.fe_logical,
3860 (unsigned long)ac->ac_b_ex.fe_group,
3861 (unsigned long)ac->ac_b_ex.fe_start,
3862 (unsigned long)ac->ac_b_ex.fe_len,
3863 (unsigned long)ac->ac_b_ex.fe_logical,
3864 (int)ac->ac_criteria);
3865 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3866 ac->ac_found);
3867 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003868 ngroups = ext4_get_groups_count(sb);
3869 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003870 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3871 struct ext4_prealloc_space *pa;
3872 ext4_grpblk_t start;
3873 struct list_head *cur;
3874 ext4_lock_group(sb, i);
3875 list_for_each(cur, &grp->bb_prealloc_list) {
3876 pa = list_entry(cur, struct ext4_prealloc_space,
3877 pa_group_list);
3878 spin_lock(&pa->pa_lock);
3879 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3880 NULL, &start);
3881 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003882 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3883 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003884 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003885 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003886
3887 if (grp->bb_free == 0)
3888 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003889 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003890 i, grp->bb_free, grp->bb_fragments);
3891 }
3892 printk(KERN_ERR "\n");
3893}
3894#else
3895static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3896{
3897 return;
3898}
3899#endif
3900
3901/*
3902 * We use locality group preallocation for small size file. The size of the
3903 * file is determined by the current size or the resulting size after
3904 * allocation which ever is larger
3905 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003906 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003907 */
3908static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3909{
3910 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3911 int bsbits = ac->ac_sb->s_blocksize_bits;
3912 loff_t size, isize;
3913
3914 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3915 return;
3916
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003917 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3918 return;
3919
Alex Tomasc9de5602008-01-29 00:19:52 -05003920 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003921 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3922 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003923
Theodore Ts'o50797482009-09-18 13:34:02 -04003924 if ((size == isize) &&
3925 !ext4_fs_is_busy(sbi) &&
3926 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3927 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3928 return;
3929 }
3930
Alex Tomasc9de5602008-01-29 00:19:52 -05003931 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003932 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003933 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003934 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003935 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003936 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003937
3938 BUG_ON(ac->ac_lg != NULL);
3939 /*
3940 * locality group prealloc space are per cpu. The reason for having
3941 * per cpu locality group is to reduce the contention between block
3942 * request from multiple CPUs.
3943 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003944 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003945
3946 /* we're going to use group allocation */
3947 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3948
3949 /* serialize all allocations in the group */
3950 mutex_lock(&ac->ac_lg->lg_mutex);
3951}
3952
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003953static noinline_for_stack int
3954ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003955 struct ext4_allocation_request *ar)
3956{
3957 struct super_block *sb = ar->inode->i_sb;
3958 struct ext4_sb_info *sbi = EXT4_SB(sb);
3959 struct ext4_super_block *es = sbi->s_es;
3960 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003961 unsigned int len;
3962 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003963 ext4_grpblk_t block;
3964
3965 /* we can't allocate > group size */
3966 len = ar->len;
3967
3968 /* just a dirty hack to filter too big requests */
3969 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3970 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3971
3972 /* start searching from the goal */
3973 goal = ar->goal;
3974 if (goal < le32_to_cpu(es->s_first_data_block) ||
3975 goal >= ext4_blocks_count(es))
3976 goal = le32_to_cpu(es->s_first_data_block);
3977 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3978
3979 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04003980 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05003981 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05003982 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05003983 ac->ac_sb = sb;
3984 ac->ac_inode = ar->inode;
3985 ac->ac_o_ex.fe_logical = ar->logical;
3986 ac->ac_o_ex.fe_group = group;
3987 ac->ac_o_ex.fe_start = block;
3988 ac->ac_o_ex.fe_len = len;
3989 ac->ac_g_ex.fe_logical = ar->logical;
3990 ac->ac_g_ex.fe_group = group;
3991 ac->ac_g_ex.fe_start = block;
3992 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003993 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05003994
3995 /* we have to define context: we'll we work with a file or
3996 * locality group. this is a policy, actually */
3997 ext4_mb_group_or_file(ac);
3998
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003999 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004000 "left: %u/%u, right %u/%u to %swritable\n",
4001 (unsigned) ar->len, (unsigned) ar->logical,
4002 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4003 (unsigned) ar->lleft, (unsigned) ar->pleft,
4004 (unsigned) ar->lright, (unsigned) ar->pright,
4005 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4006 return 0;
4007
4008}
4009
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004010static noinline_for_stack void
4011ext4_mb_discard_lg_preallocations(struct super_block *sb,
4012 struct ext4_locality_group *lg,
4013 int order, int total_entries)
4014{
4015 ext4_group_t group = 0;
4016 struct ext4_buddy e4b;
4017 struct list_head discard_list;
4018 struct ext4_prealloc_space *pa, *tmp;
4019 struct ext4_allocation_context *ac;
4020
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004021 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004022
4023 INIT_LIST_HEAD(&discard_list);
4024 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004025 if (ac)
4026 ac->ac_sb = sb;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004027
4028 spin_lock(&lg->lg_prealloc_lock);
4029 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4030 pa_inode_list) {
4031 spin_lock(&pa->pa_lock);
4032 if (atomic_read(&pa->pa_count)) {
4033 /*
4034 * This is the pa that we just used
4035 * for block allocation. So don't
4036 * free that
4037 */
4038 spin_unlock(&pa->pa_lock);
4039 continue;
4040 }
4041 if (pa->pa_deleted) {
4042 spin_unlock(&pa->pa_lock);
4043 continue;
4044 }
4045 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004046 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004047
4048 /* seems this one can be freed ... */
4049 pa->pa_deleted = 1;
4050 spin_unlock(&pa->pa_lock);
4051
4052 list_del_rcu(&pa->pa_inode_list);
4053 list_add(&pa->u.pa_tmp_list, &discard_list);
4054
4055 total_entries--;
4056 if (total_entries <= 5) {
4057 /*
4058 * we want to keep only 5 entries
4059 * allowing it to grow to 8. This
4060 * mak sure we don't call discard
4061 * soon for this list.
4062 */
4063 break;
4064 }
4065 }
4066 spin_unlock(&lg->lg_prealloc_lock);
4067
4068 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4069
4070 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4071 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004072 ext4_error(sb, "Error loading buddy information for %u",
4073 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004074 continue;
4075 }
4076 ext4_lock_group(sb, group);
4077 list_del(&pa->pa_group_list);
4078 ext4_mb_release_group_pa(&e4b, pa, ac);
4079 ext4_unlock_group(sb, group);
4080
Jing Zhange39e07f2010-05-14 00:00:00 -04004081 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004082 list_del(&pa->u.pa_tmp_list);
4083 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4084 }
4085 if (ac)
4086 kmem_cache_free(ext4_ac_cachep, ac);
4087}
4088
4089/*
4090 * We have incremented pa_count. So it cannot be freed at this
4091 * point. Also we hold lg_mutex. So no parallel allocation is
4092 * possible from this lg. That means pa_free cannot be updated.
4093 *
4094 * A parallel ext4_mb_discard_group_preallocations is possible.
4095 * which can cause the lg_prealloc_list to be updated.
4096 */
4097
4098static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4099{
4100 int order, added = 0, lg_prealloc_count = 1;
4101 struct super_block *sb = ac->ac_sb;
4102 struct ext4_locality_group *lg = ac->ac_lg;
4103 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4104
4105 order = fls(pa->pa_free) - 1;
4106 if (order > PREALLOC_TB_SIZE - 1)
4107 /* The max size of hash table is PREALLOC_TB_SIZE */
4108 order = PREALLOC_TB_SIZE - 1;
4109 /* Add the prealloc space to lg */
4110 rcu_read_lock();
4111 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4112 pa_inode_list) {
4113 spin_lock(&tmp_pa->pa_lock);
4114 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004115 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004116 continue;
4117 }
4118 if (!added && pa->pa_free < tmp_pa->pa_free) {
4119 /* Add to the tail of the previous entry */
4120 list_add_tail_rcu(&pa->pa_inode_list,
4121 &tmp_pa->pa_inode_list);
4122 added = 1;
4123 /*
4124 * we want to count the total
4125 * number of entries in the list
4126 */
4127 }
4128 spin_unlock(&tmp_pa->pa_lock);
4129 lg_prealloc_count++;
4130 }
4131 if (!added)
4132 list_add_tail_rcu(&pa->pa_inode_list,
4133 &lg->lg_prealloc_list[order]);
4134 rcu_read_unlock();
4135
4136 /* Now trim the list to be not more than 8 elements */
4137 if (lg_prealloc_count > 8) {
4138 ext4_mb_discard_lg_preallocations(sb, lg,
4139 order, lg_prealloc_count);
4140 return;
4141 }
4142 return ;
4143}
4144
Alex Tomasc9de5602008-01-29 00:19:52 -05004145/*
4146 * release all resource we used in allocation
4147 */
4148static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4149{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004150 struct ext4_prealloc_space *pa = ac->ac_pa;
4151 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004152 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004153 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004154 spin_lock(&pa->pa_lock);
4155 pa->pa_pstart += ac->ac_b_ex.fe_len;
4156 pa->pa_lstart += ac->ac_b_ex.fe_len;
4157 pa->pa_free -= ac->ac_b_ex.fe_len;
4158 pa->pa_len -= ac->ac_b_ex.fe_len;
4159 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004160 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004161 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004162 if (ac->alloc_semp)
4163 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004164 if (pa) {
4165 /*
4166 * We want to add the pa to the right bucket.
4167 * Remove it from the list and while adding
4168 * make sure the list to which we are adding
4169 * doesn't grow big. We need to release
4170 * alloc_semp before calling ext4_mb_add_n_trim()
4171 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004172 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004173 spin_lock(pa->pa_obj_lock);
4174 list_del_rcu(&pa->pa_inode_list);
4175 spin_unlock(pa->pa_obj_lock);
4176 ext4_mb_add_n_trim(ac);
4177 }
4178 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4179 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004180 if (ac->ac_bitmap_page)
4181 page_cache_release(ac->ac_bitmap_page);
4182 if (ac->ac_buddy_page)
4183 page_cache_release(ac->ac_buddy_page);
4184 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4185 mutex_unlock(&ac->ac_lg->lg_mutex);
4186 ext4_mb_collect_stats(ac);
4187 return 0;
4188}
4189
4190static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4191{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004192 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004193 int ret;
4194 int freed = 0;
4195
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004196 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004197 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004198 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4199 freed += ret;
4200 needed -= ret;
4201 }
4202
4203 return freed;
4204}
4205
4206/*
4207 * Main entry point into mballoc to allocate blocks
4208 * it tries to use preallocation first, then falls back
4209 * to usual allocation
4210 */
4211ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4212 struct ext4_allocation_request *ar, int *errp)
4213{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004214 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004215 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004216 struct ext4_sb_info *sbi;
4217 struct super_block *sb;
4218 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004219 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004220 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004221
4222 sb = ar->inode->i_sb;
4223 sbi = EXT4_SB(sb);
4224
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004225 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004226
Mingming Cao60e58e02009-01-22 18:13:05 +01004227 /*
4228 * For delayed allocation, we could skip the ENOSPC and
4229 * EDQUOT check, as blocks and quotas have been already
4230 * reserved when data being copied into pagecache.
4231 */
4232 if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4233 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4234 else {
4235 /* Without delayed allocation we need to verify
4236 * there is enough free blocks to do block allocation
4237 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004238 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004239 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4240 /* let others to free the space */
4241 yield();
4242 ar->len = ar->len >> 1;
4243 }
4244 if (!ar->len) {
Aneesh Kumar K.Va30d5422008-10-09 10:56:23 -04004245 *errp = -ENOSPC;
4246 return 0;
4247 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004248 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004249 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004250 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4251 ar->len--;
4252 }
4253 inquota = ar->len;
4254 if (ar->len == 0) {
4255 *errp = -EDQUOT;
4256 goto out3;
4257 }
Mingming Caod2a17632008-07-14 17:52:37 -04004258 }
Mingming Caod2a17632008-07-14 17:52:37 -04004259
Eric Sandeen256bdb42008-02-10 01:13:33 -05004260 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004261 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004262 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004263 *errp = -ENOMEM;
Shen Feng363d4252008-07-11 19:27:31 -04004264 goto out1;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004265 }
4266
Eric Sandeen256bdb42008-02-10 01:13:33 -05004267 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004268 if (*errp) {
4269 ar->len = 0;
Shen Feng363d4252008-07-11 19:27:31 -04004270 goto out2;
Alex Tomasc9de5602008-01-29 00:19:52 -05004271 }
4272
Eric Sandeen256bdb42008-02-10 01:13:33 -05004273 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4274 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004275 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4276 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004277repeat:
4278 /* allocate space in core */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004279 ext4_mb_regular_allocator(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004280
4281 /* as we've just preallocated more space than
4282 * user requested orinally, we store allocated
4283 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004284 if (ac->ac_status == AC_STATUS_FOUND &&
4285 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4286 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004287 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004288 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004289 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004290 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004291 /*
4292 * drop the reference that we took
4293 * in ext4_mb_use_best_found
4294 */
4295 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004296 ac->ac_b_ex.fe_group = 0;
4297 ac->ac_b_ex.fe_start = 0;
4298 ac->ac_b_ex.fe_len = 0;
4299 ac->ac_status = AC_STATUS_CONTINUE;
4300 goto repeat;
4301 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004302 ext4_discard_allocated_blocks(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004303 ac->ac_b_ex.fe_len = 0;
4304 ar->len = 0;
4305 ext4_mb_show_ac(ac);
4306 } else {
4307 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4308 ar->len = ac->ac_b_ex.fe_len;
4309 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004310 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004311 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004312 if (freed)
4313 goto repeat;
4314 *errp = -ENOSPC;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004315 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004316 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004317 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004318 }
4319
Eric Sandeen256bdb42008-02-10 01:13:33 -05004320 ext4_mb_release_context(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004321
Shen Feng363d4252008-07-11 19:27:31 -04004322out2:
4323 kmem_cache_free(ext4_ac_cachep, ac);
4324out1:
Mingming Cao60e58e02009-01-22 18:13:05 +01004325 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004326 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004327out3:
4328 if (!ar->len) {
4329 if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
4330 /* release all the reserved blocks if non delalloc */
4331 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4332 reserv_blks);
4333 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004334
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004335 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004336
Alex Tomasc9de5602008-01-29 00:19:52 -05004337 return block;
4338}
Alex Tomasc9de5602008-01-29 00:19:52 -05004339
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004340/*
4341 * We can merge two free data extents only if the physical blocks
4342 * are contiguous, AND the extents were freed by the same transaction,
4343 * AND the blocks are associated with the same group.
4344 */
4345static int can_merge(struct ext4_free_data *entry1,
4346 struct ext4_free_data *entry2)
4347{
4348 if ((entry1->t_tid == entry2->t_tid) &&
4349 (entry1->group == entry2->group) &&
4350 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4351 return 1;
4352 return 0;
4353}
4354
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004355static noinline_for_stack int
4356ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004357 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004358{
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004359 ext4_grpblk_t block;
4360 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004361 struct ext4_group_info *db = e4b->bd_info;
4362 struct super_block *sb = e4b->bd_sb;
4363 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004364 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4365 struct rb_node *parent = NULL, *new_node;
4366
Frank Mayhar03901312009-01-07 00:06:22 -05004367 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004368 BUG_ON(e4b->bd_bitmap_page == NULL);
4369 BUG_ON(e4b->bd_buddy_page == NULL);
4370
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004371 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004372 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004373
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004374 if (!*n) {
4375 /* first free block exent. We need to
4376 protect buddy cache from being freed,
4377 * otherwise we'll refresh it from
4378 * on-disk bitmap and lose not-yet-available
4379 * blocks */
4380 page_cache_get(e4b->bd_buddy_page);
4381 page_cache_get(e4b->bd_bitmap_page);
4382 }
4383 while (*n) {
4384 parent = *n;
4385 entry = rb_entry(parent, struct ext4_free_data, node);
4386 if (block < entry->start_blk)
4387 n = &(*n)->rb_left;
4388 else if (block >= (entry->start_blk + entry->count))
4389 n = &(*n)->rb_right;
4390 else {
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05004391 ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4392 "Double free of blocks %d (%d %d)",
4393 block, entry->start_blk, entry->count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004394 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004395 }
4396 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004397
4398 rb_link_node(new_node, parent, n);
4399 rb_insert_color(new_node, &db->bb_free_root);
4400
4401 /* Now try to see the extent can be merged to left and right */
4402 node = rb_prev(new_node);
4403 if (node) {
4404 entry = rb_entry(node, struct ext4_free_data, node);
4405 if (can_merge(entry, new_entry)) {
4406 new_entry->start_blk = entry->start_blk;
4407 new_entry->count += entry->count;
4408 rb_erase(node, &(db->bb_free_root));
4409 spin_lock(&sbi->s_md_lock);
4410 list_del(&entry->list);
4411 spin_unlock(&sbi->s_md_lock);
4412 kmem_cache_free(ext4_free_ext_cachep, entry);
4413 }
4414 }
4415
4416 node = rb_next(new_node);
4417 if (node) {
4418 entry = rb_entry(node, struct ext4_free_data, node);
4419 if (can_merge(new_entry, entry)) {
4420 new_entry->count += entry->count;
4421 rb_erase(node, &(db->bb_free_root));
4422 spin_lock(&sbi->s_md_lock);
4423 list_del(&entry->list);
4424 spin_unlock(&sbi->s_md_lock);
4425 kmem_cache_free(ext4_free_ext_cachep, entry);
4426 }
4427 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004428 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004429 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004430 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004431 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004432 return 0;
4433}
4434
Theodore Ts'o44338712009-11-22 07:44:56 -05004435/**
4436 * ext4_free_blocks() -- Free given blocks and update quota
4437 * @handle: handle for this transaction
4438 * @inode: inode
4439 * @block: start physical block to free
4440 * @count: number of blocks to count
4441 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004442 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004443void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004444 struct buffer_head *bh, ext4_fsblk_t block,
4445 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004446{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004447 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004448 struct super_block *sb = inode->i_sb;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004449 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004450 struct ext4_group_desc *gdp;
4451 struct ext4_super_block *es;
Theodore Ts'o44338712009-11-22 07:44:56 -05004452 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004453 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004454 ext4_grpblk_t bit;
4455 struct buffer_head *gd_bh;
4456 ext4_group_t block_group;
4457 struct ext4_sb_info *sbi;
4458 struct ext4_buddy e4b;
4459 int err = 0;
4460 int ret;
4461
Theodore Ts'oe6362602009-11-23 07:17:05 -05004462 if (bh) {
4463 if (block)
4464 BUG_ON(block != bh->b_blocknr);
4465 else
4466 block = bh->b_blocknr;
4467 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004468
Alex Tomasc9de5602008-01-29 00:19:52 -05004469 sbi = EXT4_SB(sb);
4470 es = EXT4_SB(sb)->s_es;
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004471 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4472 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004473 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004474 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004475 goto error_return;
4476 }
4477
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004478 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004479 trace_ext4_free_blocks(inode, block, count, flags);
4480
4481 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4482 struct buffer_head *tbh = bh;
4483 int i;
4484
4485 BUG_ON(bh && (count > 1));
4486
4487 for (i = 0; i < count; i++) {
4488 if (!bh)
4489 tbh = sb_find_get_block(inode->i_sb,
4490 block + i);
4491 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4492 inode, tbh, block + i);
4493 }
4494 }
4495
4496 /*
4497 * We need to make sure we don't reuse the freed block until
4498 * after the transaction is committed, which we can do by
4499 * treating the block as metadata, below. We make an
4500 * exception if the inode is to be written in writeback mode
4501 * since writeback mode has weak data consistency guarantees.
4502 */
4503 if (!ext4_should_writeback_data(inode))
4504 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004505
Eric Sandeen256bdb42008-02-10 01:13:33 -05004506 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4507 if (ac) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004508 ac->ac_inode = inode;
4509 ac->ac_sb = sb;
4510 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004511
4512do_more:
4513 overflow = 0;
4514 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4515
4516 /*
4517 * Check to see if we are freeing blocks across a group
4518 * boundary.
4519 */
4520 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4521 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4522 count -= overflow;
4523 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004524 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004525 if (!bitmap_bh) {
4526 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004527 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004528 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004529 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004530 if (!gdp) {
4531 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004532 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004533 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004534
4535 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4536 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4537 in_range(block, ext4_inode_table(sb, gdp),
4538 EXT4_SB(sb)->s_itb_per_group) ||
4539 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4540 EXT4_SB(sb)->s_itb_per_group)) {
4541
Eric Sandeen12062dd2010-02-15 14:19:27 -05004542 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004543 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004544 /* err = 0. ext4_std_error should be a no op */
4545 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004546 }
4547
4548 BUFFER_TRACE(bitmap_bh, "getting write access");
4549 err = ext4_journal_get_write_access(handle, bitmap_bh);
4550 if (err)
4551 goto error_return;
4552
4553 /*
4554 * We are about to modify some metadata. Call the journal APIs
4555 * to unshare ->b_data if a currently-committing transaction is
4556 * using it
4557 */
4558 BUFFER_TRACE(gd_bh, "get_write_access");
4559 err = ext4_journal_get_write_access(handle, gd_bh);
4560 if (err)
4561 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004562#ifdef AGGRESSIVE_CHECK
4563 {
4564 int i;
4565 for (i = 0; i < count; i++)
4566 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4567 }
4568#endif
Eric Sandeen256bdb42008-02-10 01:13:33 -05004569 if (ac) {
4570 ac->ac_b_ex.fe_group = block_group;
4571 ac->ac_b_ex.fe_start = bit;
4572 ac->ac_b_ex.fe_len = count;
Theodore Ts'o296c3552009-09-30 00:32:42 -04004573 trace_ext4_mballoc_free(ac);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004574 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004575
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004576 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4577 if (err)
4578 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004579
4580 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004581 struct ext4_free_data *new_entry;
4582 /*
4583 * blocks being freed are metadata. these blocks shouldn't
4584 * be used until this transaction is committed
4585 */
4586 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4587 new_entry->start_blk = bit;
4588 new_entry->group = block_group;
4589 new_entry->count = count;
4590 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004591
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004592 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004593 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004594 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004595 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004596 /* need to update group_info->bb_free and bitmap
4597 * with group lock held. generate_buddy look at
4598 * them with group lock_held
4599 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004600 ext4_lock_group(sb, block_group);
4601 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004602 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004603 ext4_mb_return_to_preallocation(inode, &e4b, block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004604 }
4605
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004606 ret = ext4_free_blks_count(sb, gdp) + count;
4607 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004608 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004609 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004610 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4611
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004612 if (sbi->s_log_groups_per_flex) {
4613 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004614 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004615 }
4616
Jing Zhange39e07f2010-05-14 00:00:00 -04004617 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004618
Theodore Ts'o44338712009-11-22 07:44:56 -05004619 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004620
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004621 /* We dirtied the bitmap block */
4622 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4623 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4624
Alex Tomasc9de5602008-01-29 00:19:52 -05004625 /* And the group descriptor block */
4626 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004627 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004628 if (!err)
4629 err = ret;
4630
4631 if (overflow && !err) {
4632 block += count;
4633 count = overflow;
4634 put_bh(bitmap_bh);
4635 goto do_more;
4636 }
4637 sb->s_dirt = 1;
4638error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004639 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004640 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004641 brelse(bitmap_bh);
4642 ext4_std_error(sb, err);
Eric Sandeen256bdb42008-02-10 01:13:33 -05004643 if (ac)
4644 kmem_cache_free(ext4_ac_cachep, ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004645 return;
4646}