blob: 7311f25a88ea811f55003ab7ff652f0ccf0ab4bd [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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -050096 *
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;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400341
342/* We create slab caches for groupinfo data structures based on the
343 * superblock block size. There will be one per mounted filesystem for
344 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500345#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400346static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
347
Eric Sandeen2892c152011-02-12 08:12:18 -0500348static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
349 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
350 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
351 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
352};
353
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500354static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
355 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500356static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
357 ext4_group_t group);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500358static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
359
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500360static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
361{
Alex Tomasc9de5602008-01-29 00:19:52 -0500362#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500363 *bit += ((unsigned long) addr & 7UL) << 3;
364 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500365#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500366 *bit += ((unsigned long) addr & 3UL) << 3;
367 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500368#else
369#error "how many bits you are?!"
370#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500371 return addr;
372}
Alex Tomasc9de5602008-01-29 00:19:52 -0500373
374static inline int mb_test_bit(int bit, void *addr)
375{
376 /*
377 * ext4_test_bit on architecture like powerpc
378 * needs unsigned long aligned address
379 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500380 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500381 return ext4_test_bit(bit, addr);
382}
383
384static inline void mb_set_bit(int bit, void *addr)
385{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500386 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500387 ext4_set_bit(bit, addr);
388}
389
Alex Tomasc9de5602008-01-29 00:19:52 -0500390static inline void mb_clear_bit(int bit, void *addr)
391{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500392 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500393 ext4_clear_bit(bit, addr);
394}
395
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500396static inline int mb_find_next_zero_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_zero_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
409static inline int mb_find_next_bit(void *addr, int max, int start)
410{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400411 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500412 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400413 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500414 start += fix;
415
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400416 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
417 if (ret > max)
418 return max;
419 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500420}
421
Alex Tomasc9de5602008-01-29 00:19:52 -0500422static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
423{
424 char *bb;
425
Alex Tomasc9de5602008-01-29 00:19:52 -0500426 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
427 BUG_ON(max == NULL);
428
429 if (order > e4b->bd_blkbits + 1) {
430 *max = 0;
431 return NULL;
432 }
433
434 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500435 if (order == 0) {
436 *max = 1 << (e4b->bd_blkbits + 3);
Alex Tomasc9de5602008-01-29 00:19:52 -0500437 return EXT4_MB_BITMAP(e4b);
Coly Li84b775a2011-02-24 12:51:59 -0500438 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500439
440 bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
441 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
442
443 return bb;
444}
445
446#ifdef DOUBLE_CHECK
447static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
448 int first, int count)
449{
450 int i;
451 struct super_block *sb = e4b->bd_sb;
452
453 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
454 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400455 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500456 for (i = 0; i < count; i++) {
457 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
458 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500459
460 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500461 blocknr += first + i;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500462 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400463 inode ? inode->i_ino : 0,
464 blocknr,
465 "freeing block already freed "
466 "(bit %u)",
467 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500468 }
469 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
470 }
471}
472
473static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
474{
475 int i;
476
477 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
478 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400479 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500480 for (i = 0; i < count; i++) {
481 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
482 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
483 }
484}
485
486static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
487{
488 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
489 unsigned char *b1, *b2;
490 int i;
491 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
492 b2 = (unsigned char *) bitmap;
493 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
494 if (b1[i] != b2[i]) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -0500495 printk(KERN_ERR "corruption in group %u "
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400496 "at byte %u(%u): %x in copy != %x "
497 "on disk/prealloc\n",
498 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 BUG();
500 }
501 }
502 }
503}
504
505#else
506static inline void mb_free_blocks_double(struct inode *inode,
507 struct ext4_buddy *e4b, int first, int count)
508{
509 return;
510}
511static inline void mb_mark_used_double(struct ext4_buddy *e4b,
512 int first, int count)
513{
514 return;
515}
516static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
517{
518 return;
519}
520#endif
521
522#ifdef AGGRESSIVE_CHECK
523
524#define MB_CHECK_ASSERT(assert) \
525do { \
526 if (!(assert)) { \
527 printk(KERN_EMERG \
528 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
529 function, file, line, # assert); \
530 BUG(); \
531 } \
532} while (0)
533
534static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
535 const char *function, int line)
536{
537 struct super_block *sb = e4b->bd_sb;
538 int order = e4b->bd_blkbits + 1;
539 int max;
540 int max2;
541 int i;
542 int j;
543 int k;
544 int count;
545 struct ext4_group_info *grp;
546 int fragments = 0;
547 int fstart;
548 struct list_head *cur;
549 void *buddy;
550 void *buddy2;
551
Alex Tomasc9de5602008-01-29 00:19:52 -0500552 {
553 static int mb_check_counter;
554 if (mb_check_counter++ % 100 != 0)
555 return 0;
556 }
557
558 while (order > 1) {
559 buddy = mb_find_buddy(e4b, order, &max);
560 MB_CHECK_ASSERT(buddy);
561 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
562 MB_CHECK_ASSERT(buddy2);
563 MB_CHECK_ASSERT(buddy != buddy2);
564 MB_CHECK_ASSERT(max * 2 == max2);
565
566 count = 0;
567 for (i = 0; i < max; i++) {
568
569 if (mb_test_bit(i, buddy)) {
570 /* only single bit in buddy2 may be 1 */
571 if (!mb_test_bit(i << 1, buddy2)) {
572 MB_CHECK_ASSERT(
573 mb_test_bit((i<<1)+1, buddy2));
574 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
575 MB_CHECK_ASSERT(
576 mb_test_bit(i << 1, buddy2));
577 }
578 continue;
579 }
580
581 /* both bits in buddy2 must be 0 */
582 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
583 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
584
585 for (j = 0; j < (1 << order); j++) {
586 k = (i * (1 << order)) + j;
587 MB_CHECK_ASSERT(
588 !mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
589 }
590 count++;
591 }
592 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
593 order--;
594 }
595
596 fstart = -1;
597 buddy = mb_find_buddy(e4b, 0, &max);
598 for (i = 0; i < max; i++) {
599 if (!mb_test_bit(i, buddy)) {
600 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
601 if (fstart == -1) {
602 fragments++;
603 fstart = i;
604 }
605 continue;
606 }
607 fstart = -1;
608 /* check used bits only */
609 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
610 buddy2 = mb_find_buddy(e4b, j, &max2);
611 k = i >> j;
612 MB_CHECK_ASSERT(k < max2);
613 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
614 }
615 }
616 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
617 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
618
619 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500620 list_for_each(cur, &grp->bb_prealloc_list) {
621 ext4_group_t groupnr;
622 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400623 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
624 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500625 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400626 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500627 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
628 }
629 return 0;
630}
631#undef MB_CHECK_ASSERT
632#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400633 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500634#else
635#define mb_check_buddy(e4b)
636#endif
637
Coly Li7c786052011-02-24 13:24:25 -0500638/*
639 * Divide blocks started from @first with length @len into
640 * smaller chunks with power of 2 blocks.
641 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
642 * then increase bb_counters[] for corresponded chunk size.
643 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500644static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400645 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500646 struct ext4_group_info *grp)
647{
648 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400649 ext4_grpblk_t min;
650 ext4_grpblk_t max;
651 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500652 unsigned short border;
653
Valerie Clementb73fce62008-02-15 13:48:51 -0500654 BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500655
656 border = 2 << sb->s_blocksize_bits;
657
658 while (len > 0) {
659 /* find how many blocks can be covered since this position */
660 max = ffs(first | border) - 1;
661
662 /* find how many blocks of power 2 we need to mark */
663 min = fls(len) - 1;
664
665 if (max < min)
666 min = max;
667 chunk = 1 << min;
668
669 /* mark multiblock chunks only */
670 grp->bb_counters[min]++;
671 if (min > 0)
672 mb_clear_bit(first >> min,
673 buddy + sbi->s_mb_offsets[min]);
674
675 len -= chunk;
676 first += chunk;
677 }
678}
679
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400680/*
681 * Cache the order of the largest free extent we have available in this block
682 * group.
683 */
684static void
685mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
686{
687 int i;
688 int bits;
689
690 grp->bb_largest_free_order = -1; /* uninit */
691
692 bits = sb->s_blocksize_bits + 1;
693 for (i = bits; i >= 0; i--) {
694 if (grp->bb_counters[i] > 0) {
695 grp->bb_largest_free_order = i;
696 break;
697 }
698 }
699}
700
Eric Sandeen089ceec2009-07-05 22:17:31 -0400701static noinline_for_stack
702void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500703 void *buddy, void *bitmap, ext4_group_t group)
704{
705 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Eric Sandeena36b4492009-08-25 22:36:45 -0400706 ext4_grpblk_t max = EXT4_BLOCKS_PER_GROUP(sb);
707 ext4_grpblk_t i = 0;
708 ext4_grpblk_t first;
709 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500710 unsigned free = 0;
711 unsigned fragments = 0;
712 unsigned long long period = get_cycles();
713
714 /* initialize buddy from bitmap which is aggregation
715 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500716 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500717 grp->bb_first_free = i;
718 while (i < max) {
719 fragments++;
720 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500721 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500722 len = i - first;
723 free += len;
724 if (len > 1)
725 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
726 else
727 grp->bb_counters[0]++;
728 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500729 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 }
731 grp->bb_fragments = fragments;
732
733 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400734 ext4_grp_locked_error(sb, group, 0, 0,
735 "%u blocks in bitmap, %u in gd",
736 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500737 /*
738 * If we intent to continue, we consider group descritor
739 * corrupt and update bb_free using bitmap value
740 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500741 grp->bb_free = free;
742 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400743 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500744
745 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
746
747 period = get_cycles() - period;
748 spin_lock(&EXT4_SB(sb)->s_bal_lock);
749 EXT4_SB(sb)->s_mb_buddies_generated++;
750 EXT4_SB(sb)->s_mb_generation_time += period;
751 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
752}
753
754/* The buddy information is attached the buddy cache inode
755 * for convenience. The information regarding each group
756 * is loaded via ext4_mb_load_buddy. The information involve
757 * block bitmap and buddy information. The information are
758 * stored in the inode as
759 *
760 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500761 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500762 *
763 *
764 * one block each for bitmap and buddy information.
765 * So for each group we take up 2 blocks. A page can
766 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
767 * So it can have information regarding groups_per_page which
768 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400769 *
770 * Locking note: This routine takes the block group lock of all groups
771 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500772 */
773
774static int ext4_mb_init_cache(struct page *page, char *incore)
775{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400776 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500777 int blocksize;
778 int blocks_per_page;
779 int groups_per_page;
780 int err = 0;
781 int i;
782 ext4_group_t first_group;
783 int first_block;
784 struct super_block *sb;
785 struct buffer_head *bhs;
786 struct buffer_head **bh;
787 struct inode *inode;
788 char *data;
789 char *bitmap;
790
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400791 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500792
793 inode = page->mapping->host;
794 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400795 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500796 blocksize = 1 << inode->i_blkbits;
797 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
798
799 groups_per_page = blocks_per_page >> 1;
800 if (groups_per_page == 0)
801 groups_per_page = 1;
802
803 /* allocate buffer_heads to read bitmaps */
804 if (groups_per_page > 1) {
805 err = -ENOMEM;
806 i = sizeof(struct buffer_head *) * groups_per_page;
807 bh = kzalloc(i, GFP_NOFS);
808 if (bh == NULL)
809 goto out;
810 } else
811 bh = &bhs;
812
813 first_group = page->index * blocks_per_page / 2;
814
815 /* read all groups the page covers into the cache */
816 for (i = 0; i < groups_per_page; i++) {
817 struct ext4_group_desc *desc;
818
Theodore Ts'o8df96752009-05-01 08:50:38 -0400819 if (first_group + i >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500820 break;
821
822 err = -EIO;
823 desc = ext4_get_group_desc(sb, first_group + i, NULL);
824 if (desc == NULL)
825 goto out;
826
827 err = -ENOMEM;
828 bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
829 if (bh[i] == NULL)
830 goto out;
831
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500832 if (bitmap_uptodate(bh[i]))
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 continue;
834
Frederic Bohec806e682008-10-10 08:09:18 -0400835 lock_buffer(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500836 if (bitmap_uptodate(bh[i])) {
837 unlock_buffer(bh[i]);
838 continue;
839 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400840 ext4_lock_group(sb, first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
842 ext4_init_block_bitmap(sb, bh[i],
843 first_group + i, desc);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500844 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500845 set_buffer_uptodate(bh[i]);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400846 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V3300bed2009-01-03 22:33:39 -0500847 unlock_buffer(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 continue;
849 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -0400850 ext4_unlock_group(sb, first_group + i);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500851 if (buffer_uptodate(bh[i])) {
852 /*
853 * if not uninit if bh is uptodate,
854 * bitmap is also uptodate
855 */
856 set_bitmap_uptodate(bh[i]);
857 unlock_buffer(bh[i]);
858 continue;
859 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500860 get_bh(bh[i]);
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500861 /*
862 * submit the buffer_head for read. We can
863 * safely mark the bitmap as uptodate now.
864 * We do it here so the bitmap uptodate bit
865 * get set with buffer lock held.
866 */
867 set_bitmap_uptodate(bh[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500868 bh[i]->b_end_io = end_buffer_read_sync;
869 submit_bh(READ, bh[i]);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400870 mb_debug(1, "read bitmap for group %u\n", first_group + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500871 }
872
873 /* wait for I/O completion */
874 for (i = 0; i < groups_per_page && bh[i]; i++)
875 wait_on_buffer(bh[i]);
876
877 err = -EIO;
878 for (i = 0; i < groups_per_page && bh[i]; i++)
879 if (!buffer_uptodate(bh[i]))
880 goto out;
881
Mingming Cao31b481d2008-07-11 19:27:31 -0400882 err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -0500883 first_block = page->index * blocks_per_page;
Aneesh Kumar K.V29eaf022009-01-05 21:48:56 -0500884 /* init the page */
885 memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
Alex Tomasc9de5602008-01-29 00:19:52 -0500886 for (i = 0; i < blocks_per_page; i++) {
887 int group;
888 struct ext4_group_info *grinfo;
889
890 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400891 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500892 break;
893
894 /*
895 * data carry information regarding this
896 * particular group in the format specified
897 * above
898 *
899 */
900 data = page_address(page) + (i * blocksize);
901 bitmap = bh[group - first_group]->b_data;
902
903 /*
904 * We place the buddy block and bitmap block
905 * close together
906 */
907 if ((first_block + i) & 1) {
908 /* this is block of buddy */
909 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400910 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500911 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400912 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500913 grinfo = ext4_get_group_info(sb, group);
914 grinfo->bb_fragments = 0;
915 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400916 sizeof(*grinfo->bb_counters) *
917 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500918 /*
919 * incore got set to the group block bitmap below
920 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500921 ext4_lock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500922 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500923 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500924 incore = NULL;
925 } else {
926 /* this is block of bitmap */
927 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400928 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500929 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400930 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500931
932 /* see comments in ext4_mb_put_pa() */
933 ext4_lock_group(sb, group);
934 memcpy(data, bitmap, blocksize);
935
936 /* mark all preallocated blks used in in-core bitmap */
937 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500938 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500939 ext4_unlock_group(sb, group);
940
941 /* set incore so that the buddy information can be
942 * generated using this
943 */
944 incore = data;
945 }
946 }
947 SetPageUptodate(page);
948
949out:
950 if (bh) {
951 for (i = 0; i < groups_per_page && bh[i]; i++)
952 brelse(bh[i]);
953 if (bh != &bhs)
954 kfree(bh);
955 }
956 return err;
957}
958
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400959/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400960 * Lock the buddy and bitmap pages. This make sure other parallel init_group
961 * on the same buddy page doesn't happen whild holding the buddy page lock.
962 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
963 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400964 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400965static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
966 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400967{
Amir Goldstein2de88072011-05-09 21:48:13 -0400968 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
969 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400970 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400971 struct page *page;
972
973 e4b->bd_buddy_page = NULL;
974 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400975
976 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
977 /*
978 * the buddy cache inode stores the block bitmap
979 * and buddy information in consecutive blocks.
980 * So for each group we need two blocks.
981 */
982 block = group * 2;
983 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400984 poff = block % blocks_per_page;
985 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
986 if (!page)
987 return -EIO;
988 BUG_ON(page->mapping != inode->i_mapping);
989 e4b->bd_bitmap_page = page;
990 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400991
Amir Goldstein2de88072011-05-09 21:48:13 -0400992 if (blocks_per_page >= 2) {
993 /* buddy and bitmap are on the same page */
994 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400995 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400996
997 block++;
998 pnum = block / blocks_per_page;
999 poff = block % blocks_per_page;
1000 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1001 if (!page)
1002 return -EIO;
1003 BUG_ON(page->mapping != inode->i_mapping);
1004 e4b->bd_buddy_page = page;
1005 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001006}
1007
Amir Goldstein2de88072011-05-09 21:48:13 -04001008static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001009{
Amir Goldstein2de88072011-05-09 21:48:13 -04001010 if (e4b->bd_bitmap_page) {
1011 unlock_page(e4b->bd_bitmap_page);
1012 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001013 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001014 if (e4b->bd_buddy_page) {
1015 unlock_page(e4b->bd_buddy_page);
1016 page_cache_release(e4b->bd_buddy_page);
1017 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001018}
1019
1020/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001021 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1022 * block group lock of all groups for this page; do not hold the BG lock when
1023 * calling this routine!
1024 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001025static noinline_for_stack
1026int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1027{
1028
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001029 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001030 struct ext4_buddy e4b;
1031 struct page *page;
1032 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001033
1034 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001035 this_grp = ext4_get_group_info(sb, group);
1036 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001037 * This ensures that we don't reinit the buddy cache
1038 * page which map to the group from which we are already
1039 * allocating. If we are looking at the buddy cache we would
1040 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001041 * would have pinned buddy page to page cache.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001042 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001043 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1044 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001045 /*
1046 * somebody initialized the group
1047 * return without doing anything
1048 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001049 goto err;
1050 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001051
1052 page = e4b.bd_bitmap_page;
1053 ret = ext4_mb_init_cache(page, NULL);
1054 if (ret)
1055 goto err;
1056 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001057 ret = -EIO;
1058 goto err;
1059 }
1060 mark_page_accessed(page);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001061
Amir Goldstein2de88072011-05-09 21:48:13 -04001062 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001063 /*
1064 * If both the bitmap and buddy are in
1065 * the same page we don't need to force
1066 * init the buddy
1067 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001068 ret = 0;
1069 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001070 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001071 /* init buddy cache */
1072 page = e4b.bd_buddy_page;
1073 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1074 if (ret)
1075 goto err;
1076 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001077 ret = -EIO;
1078 goto err;
1079 }
1080 mark_page_accessed(page);
1081err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001082 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001083 return ret;
1084}
1085
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001086/*
1087 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1088 * block group lock of all groups for this page; do not hold the BG lock when
1089 * calling this routine!
1090 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001091static noinline_for_stack int
1092ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1093 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001094{
Alex Tomasc9de5602008-01-29 00:19:52 -05001095 int blocks_per_page;
1096 int block;
1097 int pnum;
1098 int poff;
1099 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001100 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001101 struct ext4_group_info *grp;
1102 struct ext4_sb_info *sbi = EXT4_SB(sb);
1103 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001104
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001105 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001106
1107 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001108 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001109
1110 e4b->bd_blkbits = sb->s_blocksize_bits;
1111 e4b->bd_info = ext4_get_group_info(sb, group);
1112 e4b->bd_sb = sb;
1113 e4b->bd_group = group;
1114 e4b->bd_buddy_page = NULL;
1115 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001116 e4b->alloc_semp = &grp->alloc_sem;
1117
1118 /* Take the read lock on the group alloc
1119 * sem. This would make sure a parallel
1120 * ext4_mb_init_group happening on other
1121 * groups mapped by the page is blocked
1122 * till we are done with allocation
1123 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001124repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001125 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001126
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001127 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1128 /* we need to check for group need init flag
1129 * with alloc_semp held so that we can be sure
1130 * that new blocks didn't get added to the group
1131 * when we are loading the buddy cache
1132 */
1133 up_read(e4b->alloc_semp);
1134 /*
1135 * we need full data about the group
1136 * to make a good selection
1137 */
1138 ret = ext4_mb_init_group(sb, group);
1139 if (ret)
1140 return ret;
1141 goto repeat_load_buddy;
1142 }
1143
Alex Tomasc9de5602008-01-29 00:19:52 -05001144 /*
1145 * the buddy cache inode stores the block bitmap
1146 * and buddy information in consecutive blocks.
1147 * So for each group we need two blocks.
1148 */
1149 block = group * 2;
1150 pnum = block / blocks_per_page;
1151 poff = block % blocks_per_page;
1152
1153 /* we could use find_or_create_page(), but it locks page
1154 * what we'd like to avoid in fast path ... */
1155 page = find_get_page(inode->i_mapping, pnum);
1156 if (page == NULL || !PageUptodate(page)) {
1157 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001158 /*
1159 * drop the page reference and try
1160 * to get the page with lock. If we
1161 * are not uptodate that implies
1162 * somebody just created the page but
1163 * is yet to initialize the same. So
1164 * wait for it to initialize.
1165 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001166 page_cache_release(page);
1167 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1168 if (page) {
1169 BUG_ON(page->mapping != inode->i_mapping);
1170 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001171 ret = ext4_mb_init_cache(page, NULL);
1172 if (ret) {
1173 unlock_page(page);
1174 goto err;
1175 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001176 mb_cmp_bitmaps(e4b, page_address(page) +
1177 (poff * sb->s_blocksize));
1178 }
1179 unlock_page(page);
1180 }
1181 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001182 if (page == NULL || !PageUptodate(page)) {
1183 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001184 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001185 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001186 e4b->bd_bitmap_page = page;
1187 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1188 mark_page_accessed(page);
1189
1190 block++;
1191 pnum = block / blocks_per_page;
1192 poff = block % blocks_per_page;
1193
1194 page = find_get_page(inode->i_mapping, pnum);
1195 if (page == NULL || !PageUptodate(page)) {
1196 if (page)
1197 page_cache_release(page);
1198 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1199 if (page) {
1200 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001201 if (!PageUptodate(page)) {
1202 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1203 if (ret) {
1204 unlock_page(page);
1205 goto err;
1206 }
1207 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001208 unlock_page(page);
1209 }
1210 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001211 if (page == NULL || !PageUptodate(page)) {
1212 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001213 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001214 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001215 e4b->bd_buddy_page = page;
1216 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1217 mark_page_accessed(page);
1218
1219 BUG_ON(e4b->bd_bitmap_page == NULL);
1220 BUG_ON(e4b->bd_buddy_page == NULL);
1221
1222 return 0;
1223
1224err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001225 if (page)
1226 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001227 if (e4b->bd_bitmap_page)
1228 page_cache_release(e4b->bd_bitmap_page);
1229 if (e4b->bd_buddy_page)
1230 page_cache_release(e4b->bd_buddy_page);
1231 e4b->bd_buddy = NULL;
1232 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001233
1234 /* Done with the buddy cache */
1235 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001236 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001237}
1238
Jing Zhange39e07f2010-05-14 00:00:00 -04001239static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001240{
1241 if (e4b->bd_bitmap_page)
1242 page_cache_release(e4b->bd_bitmap_page);
1243 if (e4b->bd_buddy_page)
1244 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001245 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001246 if (e4b->alloc_semp)
1247 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001248}
1249
1250
1251static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1252{
1253 int order = 1;
1254 void *bb;
1255
1256 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1257 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1258
1259 bb = EXT4_MB_BUDDY(e4b);
1260 while (order <= e4b->bd_blkbits + 1) {
1261 block = block >> 1;
1262 if (!mb_test_bit(block, bb)) {
1263 /* this block is part of buddy of order 'order' */
1264 return order;
1265 }
1266 bb += 1 << (e4b->bd_blkbits - order);
1267 order++;
1268 }
1269 return 0;
1270}
1271
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001272static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001273{
1274 __u32 *addr;
1275
1276 len = cur + len;
1277 while (cur < len) {
1278 if ((cur & 31) == 0 && (len - cur) >= 32) {
1279 /* fast path: clear whole word at once */
1280 addr = bm + (cur >> 3);
1281 *addr = 0;
1282 cur += 32;
1283 continue;
1284 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001285 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001286 cur++;
1287 }
1288}
1289
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001290static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001291{
1292 __u32 *addr;
1293
1294 len = cur + len;
1295 while (cur < len) {
1296 if ((cur & 31) == 0 && (len - cur) >= 32) {
1297 /* fast path: set whole word at once */
1298 addr = bm + (cur >> 3);
1299 *addr = 0xffffffff;
1300 cur += 32;
1301 continue;
1302 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001303 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001304 cur++;
1305 }
1306}
1307
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001308static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001309 int first, int count)
1310{
1311 int block = 0;
1312 int max = 0;
1313 int order;
1314 void *buddy;
1315 void *buddy2;
1316 struct super_block *sb = e4b->bd_sb;
1317
1318 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001319 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001320 mb_check_buddy(e4b);
1321 mb_free_blocks_double(inode, e4b, first, count);
1322
1323 e4b->bd_info->bb_free += count;
1324 if (first < e4b->bd_info->bb_first_free)
1325 e4b->bd_info->bb_first_free = first;
1326
1327 /* let's maintain fragments counter */
1328 if (first != 0)
1329 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1330 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1331 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1332 if (block && max)
1333 e4b->bd_info->bb_fragments--;
1334 else if (!block && !max)
1335 e4b->bd_info->bb_fragments++;
1336
1337 /* let's maintain buddy itself */
1338 while (count-- > 0) {
1339 block = first++;
1340 order = 0;
1341
1342 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1343 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001344
1345 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001346 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001347 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001348 inode ? inode->i_ino : 0,
1349 blocknr,
1350 "freeing already freed block "
1351 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001352 }
1353 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1354 e4b->bd_info->bb_counters[order]++;
1355
1356 /* start of the buddy */
1357 buddy = mb_find_buddy(e4b, order, &max);
1358
1359 do {
1360 block &= ~1UL;
1361 if (mb_test_bit(block, buddy) ||
1362 mb_test_bit(block + 1, buddy))
1363 break;
1364
1365 /* both the buddies are free, try to coalesce them */
1366 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1367
1368 if (!buddy2)
1369 break;
1370
1371 if (order > 0) {
1372 /* for special purposes, we don't set
1373 * free bits in bitmap */
1374 mb_set_bit(block, buddy);
1375 mb_set_bit(block + 1, buddy);
1376 }
1377 e4b->bd_info->bb_counters[order]--;
1378 e4b->bd_info->bb_counters[order]--;
1379
1380 block = block >> 1;
1381 order++;
1382 e4b->bd_info->bb_counters[order]++;
1383
1384 mb_clear_bit(block, buddy2);
1385 buddy = buddy2;
1386 } while (1);
1387 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001388 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001389 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001390}
1391
1392static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1393 int needed, struct ext4_free_extent *ex)
1394{
1395 int next = block;
1396 int max;
1397 int ord;
1398 void *buddy;
1399
Vincent Minetbc8e6742009-05-15 08:33:18 -04001400 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001401 BUG_ON(ex == NULL);
1402
1403 buddy = mb_find_buddy(e4b, order, &max);
1404 BUG_ON(buddy == NULL);
1405 BUG_ON(block >= max);
1406 if (mb_test_bit(block, buddy)) {
1407 ex->fe_len = 0;
1408 ex->fe_start = 0;
1409 ex->fe_group = 0;
1410 return 0;
1411 }
1412
1413 /* FIXME dorp order completely ? */
1414 if (likely(order == 0)) {
1415 /* find actual order */
1416 order = mb_find_order_for_block(e4b, block);
1417 block = block >> order;
1418 }
1419
1420 ex->fe_len = 1 << order;
1421 ex->fe_start = block << order;
1422 ex->fe_group = e4b->bd_group;
1423
1424 /* calc difference from given start */
1425 next = next - ex->fe_start;
1426 ex->fe_len -= next;
1427 ex->fe_start += next;
1428
1429 while (needed > ex->fe_len &&
1430 (buddy = mb_find_buddy(e4b, order, &max))) {
1431
1432 if (block + 1 >= max)
1433 break;
1434
1435 next = (block + 1) * (1 << order);
1436 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1437 break;
1438
1439 ord = mb_find_order_for_block(e4b, next);
1440
1441 order = ord;
1442 block = next >> order;
1443 ex->fe_len += 1 << order;
1444 }
1445
1446 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1447 return ex->fe_len;
1448}
1449
1450static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1451{
1452 int ord;
1453 int mlen = 0;
1454 int max = 0;
1455 int cur;
1456 int start = ex->fe_start;
1457 int len = ex->fe_len;
1458 unsigned ret = 0;
1459 int len0 = len;
1460 void *buddy;
1461
1462 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1463 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001464 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001465 mb_check_buddy(e4b);
1466 mb_mark_used_double(e4b, start, len);
1467
1468 e4b->bd_info->bb_free -= len;
1469 if (e4b->bd_info->bb_first_free == start)
1470 e4b->bd_info->bb_first_free += len;
1471
1472 /* let's maintain fragments counter */
1473 if (start != 0)
1474 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1475 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1476 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1477 if (mlen && max)
1478 e4b->bd_info->bb_fragments++;
1479 else if (!mlen && !max)
1480 e4b->bd_info->bb_fragments--;
1481
1482 /* let's maintain buddy itself */
1483 while (len) {
1484 ord = mb_find_order_for_block(e4b, start);
1485
1486 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1487 /* the whole chunk may be allocated at once! */
1488 mlen = 1 << ord;
1489 buddy = mb_find_buddy(e4b, ord, &max);
1490 BUG_ON((start >> ord) >= max);
1491 mb_set_bit(start >> ord, buddy);
1492 e4b->bd_info->bb_counters[ord]--;
1493 start += mlen;
1494 len -= mlen;
1495 BUG_ON(len < 0);
1496 continue;
1497 }
1498
1499 /* store for history */
1500 if (ret == 0)
1501 ret = len | (ord << 16);
1502
1503 /* we have to split large buddy */
1504 BUG_ON(ord <= 0);
1505 buddy = mb_find_buddy(e4b, ord, &max);
1506 mb_set_bit(start >> ord, buddy);
1507 e4b->bd_info->bb_counters[ord]--;
1508
1509 ord--;
1510 cur = (start >> ord) & ~1U;
1511 buddy = mb_find_buddy(e4b, ord, &max);
1512 mb_clear_bit(cur, buddy);
1513 mb_clear_bit(cur + 1, buddy);
1514 e4b->bd_info->bb_counters[ord]++;
1515 e4b->bd_info->bb_counters[ord]++;
1516 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001517 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001518
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001519 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001520 mb_check_buddy(e4b);
1521
1522 return ret;
1523}
1524
1525/*
1526 * Must be called under group lock!
1527 */
1528static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1529 struct ext4_buddy *e4b)
1530{
1531 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1532 int ret;
1533
1534 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1535 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1536
1537 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1538 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1539 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1540
1541 /* preallocation can change ac_b_ex, thus we store actually
1542 * allocated blocks for history */
1543 ac->ac_f_ex = ac->ac_b_ex;
1544
1545 ac->ac_status = AC_STATUS_FOUND;
1546 ac->ac_tail = ret & 0xffff;
1547 ac->ac_buddy = ret >> 16;
1548
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001549 /*
1550 * take the page reference. We want the page to be pinned
1551 * so that we don't get a ext4_mb_init_cache_call for this
1552 * group until we update the bitmap. That would mean we
1553 * double allocate blocks. The reference is dropped
1554 * in ext4_mb_release_context
1555 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001556 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1557 get_page(ac->ac_bitmap_page);
1558 ac->ac_buddy_page = e4b->bd_buddy_page;
1559 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001560 /* on allocation we use ac to track the held semaphore */
1561 ac->alloc_semp = e4b->alloc_semp;
1562 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001563 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001564 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001565 spin_lock(&sbi->s_md_lock);
1566 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1567 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1568 spin_unlock(&sbi->s_md_lock);
1569 }
1570}
1571
1572/*
1573 * regular allocator, for general purposes allocation
1574 */
1575
1576static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1577 struct ext4_buddy *e4b,
1578 int finish_group)
1579{
1580 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1581 struct ext4_free_extent *bex = &ac->ac_b_ex;
1582 struct ext4_free_extent *gex = &ac->ac_g_ex;
1583 struct ext4_free_extent ex;
1584 int max;
1585
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001586 if (ac->ac_status == AC_STATUS_FOUND)
1587 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001588 /*
1589 * We don't want to scan for a whole year
1590 */
1591 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1592 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1593 ac->ac_status = AC_STATUS_BREAK;
1594 return;
1595 }
1596
1597 /*
1598 * Haven't found good chunk so far, let's continue
1599 */
1600 if (bex->fe_len < gex->fe_len)
1601 return;
1602
1603 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1604 && bex->fe_group == e4b->bd_group) {
1605 /* recheck chunk's availability - we don't know
1606 * when it was found (within this lock-unlock
1607 * period or not) */
1608 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1609 if (max >= gex->fe_len) {
1610 ext4_mb_use_best_found(ac, e4b);
1611 return;
1612 }
1613 }
1614}
1615
1616/*
1617 * The routine checks whether found extent is good enough. If it is,
1618 * then the extent gets marked used and flag is set to the context
1619 * to stop scanning. Otherwise, the extent is compared with the
1620 * previous found extent and if new one is better, then it's stored
1621 * in the context. Later, the best found extent will be used, if
1622 * mballoc can't find good enough extent.
1623 *
1624 * FIXME: real allocation policy is to be designed yet!
1625 */
1626static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1627 struct ext4_free_extent *ex,
1628 struct ext4_buddy *e4b)
1629{
1630 struct ext4_free_extent *bex = &ac->ac_b_ex;
1631 struct ext4_free_extent *gex = &ac->ac_g_ex;
1632
1633 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001634 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001635 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1636 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1637
1638 ac->ac_found++;
1639
1640 /*
1641 * The special case - take what you catch first
1642 */
1643 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1644 *bex = *ex;
1645 ext4_mb_use_best_found(ac, e4b);
1646 return;
1647 }
1648
1649 /*
1650 * Let's check whether the chuck is good enough
1651 */
1652 if (ex->fe_len == gex->fe_len) {
1653 *bex = *ex;
1654 ext4_mb_use_best_found(ac, e4b);
1655 return;
1656 }
1657
1658 /*
1659 * If this is first found extent, just store it in the context
1660 */
1661 if (bex->fe_len == 0) {
1662 *bex = *ex;
1663 return;
1664 }
1665
1666 /*
1667 * If new found extent is better, store it in the context
1668 */
1669 if (bex->fe_len < gex->fe_len) {
1670 /* if the request isn't satisfied, any found extent
1671 * larger than previous best one is better */
1672 if (ex->fe_len > bex->fe_len)
1673 *bex = *ex;
1674 } else if (ex->fe_len > gex->fe_len) {
1675 /* if the request is satisfied, then we try to find
1676 * an extent that still satisfy the request, but is
1677 * smaller than previous one */
1678 if (ex->fe_len < bex->fe_len)
1679 *bex = *ex;
1680 }
1681
1682 ext4_mb_check_limits(ac, e4b, 0);
1683}
1684
Eric Sandeen089ceec2009-07-05 22:17:31 -04001685static noinline_for_stack
1686int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001687 struct ext4_buddy *e4b)
1688{
1689 struct ext4_free_extent ex = ac->ac_b_ex;
1690 ext4_group_t group = ex.fe_group;
1691 int max;
1692 int err;
1693
1694 BUG_ON(ex.fe_len <= 0);
1695 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1696 if (err)
1697 return err;
1698
1699 ext4_lock_group(ac->ac_sb, group);
1700 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1701
1702 if (max > 0) {
1703 ac->ac_b_ex = ex;
1704 ext4_mb_use_best_found(ac, e4b);
1705 }
1706
1707 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001708 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001709
1710 return 0;
1711}
1712
Eric Sandeen089ceec2009-07-05 22:17:31 -04001713static noinline_for_stack
1714int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001715 struct ext4_buddy *e4b)
1716{
1717 ext4_group_t group = ac->ac_g_ex.fe_group;
1718 int max;
1719 int err;
1720 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001721 struct ext4_free_extent ex;
1722
1723 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1724 return 0;
1725
1726 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1727 if (err)
1728 return err;
1729
1730 ext4_lock_group(ac->ac_sb, group);
1731 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1732 ac->ac_g_ex.fe_len, &ex);
1733
1734 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1735 ext4_fsblk_t start;
1736
Akinobu Mita5661bd62010-03-03 23:53:39 -05001737 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1738 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001739 /* use do_div to get remainder (would be 64-bit modulo) */
1740 if (do_div(start, sbi->s_stripe) == 0) {
1741 ac->ac_found++;
1742 ac->ac_b_ex = ex;
1743 ext4_mb_use_best_found(ac, e4b);
1744 }
1745 } else if (max >= ac->ac_g_ex.fe_len) {
1746 BUG_ON(ex.fe_len <= 0);
1747 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1748 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1749 ac->ac_found++;
1750 ac->ac_b_ex = ex;
1751 ext4_mb_use_best_found(ac, e4b);
1752 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1753 /* Sometimes, caller may want to merge even small
1754 * number of blocks to an existing extent */
1755 BUG_ON(ex.fe_len <= 0);
1756 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1757 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1758 ac->ac_found++;
1759 ac->ac_b_ex = ex;
1760 ext4_mb_use_best_found(ac, e4b);
1761 }
1762 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001763 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001764
1765 return 0;
1766}
1767
1768/*
1769 * The routine scans buddy structures (not bitmap!) from given order
1770 * to max order and tries to find big enough chunk to satisfy the req
1771 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001772static noinline_for_stack
1773void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001774 struct ext4_buddy *e4b)
1775{
1776 struct super_block *sb = ac->ac_sb;
1777 struct ext4_group_info *grp = e4b->bd_info;
1778 void *buddy;
1779 int i;
1780 int k;
1781 int max;
1782
1783 BUG_ON(ac->ac_2order <= 0);
1784 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1785 if (grp->bb_counters[i] == 0)
1786 continue;
1787
1788 buddy = mb_find_buddy(e4b, i, &max);
1789 BUG_ON(buddy == NULL);
1790
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001791 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001792 BUG_ON(k >= max);
1793
1794 ac->ac_found++;
1795
1796 ac->ac_b_ex.fe_len = 1 << i;
1797 ac->ac_b_ex.fe_start = k << i;
1798 ac->ac_b_ex.fe_group = e4b->bd_group;
1799
1800 ext4_mb_use_best_found(ac, e4b);
1801
1802 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1803
1804 if (EXT4_SB(sb)->s_mb_stats)
1805 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1806
1807 break;
1808 }
1809}
1810
1811/*
1812 * The routine scans the group and measures all found extents.
1813 * In order to optimize scanning, caller must pass number of
1814 * free blocks in the group, so the routine can know upper limit.
1815 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001816static noinline_for_stack
1817void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001818 struct ext4_buddy *e4b)
1819{
1820 struct super_block *sb = ac->ac_sb;
1821 void *bitmap = EXT4_MB_BITMAP(e4b);
1822 struct ext4_free_extent ex;
1823 int i;
1824 int free;
1825
1826 free = e4b->bd_info->bb_free;
1827 BUG_ON(free <= 0);
1828
1829 i = e4b->bd_info->bb_first_free;
1830
1831 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001832 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001833 EXT4_BLOCKS_PER_GROUP(sb), i);
1834 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001835 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001836 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001837 * free blocks even though group info says we
1838 * we have free blocks
1839 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001840 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1841 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001842 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001843 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001844 break;
1845 }
1846
1847 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1848 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001849 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001850 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1851 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001852 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001853 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001854 /*
1855 * The number of free blocks differs. This mostly
1856 * indicate that the bitmap is corrupt. So exit
1857 * without claiming the space.
1858 */
1859 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001860 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001861
1862 ext4_mb_measure_extent(ac, &ex, e4b);
1863
1864 i += ex.fe_len;
1865 free -= ex.fe_len;
1866 }
1867
1868 ext4_mb_check_limits(ac, e4b, 1);
1869}
1870
1871/*
1872 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001873 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001874 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001875static noinline_for_stack
1876void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001877 struct ext4_buddy *e4b)
1878{
1879 struct super_block *sb = ac->ac_sb;
1880 struct ext4_sb_info *sbi = EXT4_SB(sb);
1881 void *bitmap = EXT4_MB_BITMAP(e4b);
1882 struct ext4_free_extent ex;
1883 ext4_fsblk_t first_group_block;
1884 ext4_fsblk_t a;
1885 ext4_grpblk_t i;
1886 int max;
1887
1888 BUG_ON(sbi->s_stripe == 0);
1889
1890 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001891 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1892
Alex Tomasc9de5602008-01-29 00:19:52 -05001893 a = first_group_block + sbi->s_stripe - 1;
1894 do_div(a, sbi->s_stripe);
1895 i = (a * sbi->s_stripe) - first_group_block;
1896
1897 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1898 if (!mb_test_bit(i, bitmap)) {
1899 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1900 if (max >= sbi->s_stripe) {
1901 ac->ac_found++;
1902 ac->ac_b_ex = ex;
1903 ext4_mb_use_best_found(ac, e4b);
1904 break;
1905 }
1906 }
1907 i += sbi->s_stripe;
1908 }
1909}
1910
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001911/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001912static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1913 ext4_group_t group, int cr)
1914{
1915 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001916 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001917 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1918
1919 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001920
1921 /* We only do this if the grp has never been initialized */
1922 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1923 int ret = ext4_mb_init_group(ac->ac_sb, group);
1924 if (ret)
1925 return 0;
1926 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001927
1928 free = grp->bb_free;
1929 fragments = grp->bb_fragments;
1930 if (free == 0)
1931 return 0;
1932 if (fragments == 0)
1933 return 0;
1934
1935 switch (cr) {
1936 case 0:
1937 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001938
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001939 if (grp->bb_largest_free_order < ac->ac_2order)
1940 return 0;
1941
Theodore Ts'oa4912122009-03-12 12:18:34 -04001942 /* Avoid using the first bg of a flexgroup for data files */
1943 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1944 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1945 ((group % flex_size) == 0))
1946 return 0;
1947
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001948 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001949 case 1:
1950 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1951 return 1;
1952 break;
1953 case 2:
1954 if (free >= ac->ac_g_ex.fe_len)
1955 return 1;
1956 break;
1957 case 3:
1958 return 1;
1959 default:
1960 BUG();
1961 }
1962
1963 return 0;
1964}
1965
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001966static noinline_for_stack int
1967ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001968{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001969 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001970 int cr;
1971 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001972 struct ext4_sb_info *sbi;
1973 struct super_block *sb;
1974 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001975
1976 sb = ac->ac_sb;
1977 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001978 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001979 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001980 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001981 ngroups = sbi->s_blockfile_groups;
1982
Alex Tomasc9de5602008-01-29 00:19:52 -05001983 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1984
1985 /* first, try the goal */
1986 err = ext4_mb_find_by_goal(ac, &e4b);
1987 if (err || ac->ac_status == AC_STATUS_FOUND)
1988 goto out;
1989
1990 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1991 goto out;
1992
1993 /*
1994 * ac->ac2_order is set only if the fe_len is a power of 2
1995 * if ac2_order is set we also set criteria to 0 so that we
1996 * try exact allocation using buddy.
1997 */
1998 i = fls(ac->ac_g_ex.fe_len);
1999 ac->ac_2order = 0;
2000 /*
2001 * We search using buddy data only if the order of the request
2002 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002003 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 */
2005 if (i >= sbi->s_mb_order2_reqs) {
2006 /*
2007 * This should tell if fe_len is exactly power of 2
2008 */
2009 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2010 ac->ac_2order = i - 1;
2011 }
2012
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002013 /* if stream allocation is enabled, use global goal */
2014 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002015 /* TBD: may be hot point */
2016 spin_lock(&sbi->s_md_lock);
2017 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2018 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2019 spin_unlock(&sbi->s_md_lock);
2020 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002021
Alex Tomasc9de5602008-01-29 00:19:52 -05002022 /* Let's just scan groups to find more-less suitable blocks */
2023 cr = ac->ac_2order ? 0 : 1;
2024 /*
2025 * cr == 0 try to get exact allocation,
2026 * cr == 3 try to get anything
2027 */
2028repeat:
2029 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2030 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002031 /*
2032 * searching for the right group start
2033 * from the goal value specified
2034 */
2035 group = ac->ac_g_ex.fe_group;
2036
Theodore Ts'o8df96752009-05-01 08:50:38 -04002037 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002038 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002039 group = 0;
2040
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002041 /* This now checks without needing the buddy page */
2042 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002043 continue;
2044
Alex Tomasc9de5602008-01-29 00:19:52 -05002045 err = ext4_mb_load_buddy(sb, group, &e4b);
2046 if (err)
2047 goto out;
2048
2049 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002050
2051 /*
2052 * We need to check again after locking the
2053 * block group
2054 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002055 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002056 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002057 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002058 continue;
2059 }
2060
2061 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002062 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002063 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002064 else if (cr == 1 && sbi->s_stripe &&
2065 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002066 ext4_mb_scan_aligned(ac, &e4b);
2067 else
2068 ext4_mb_complex_scan_group(ac, &e4b);
2069
2070 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002071 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002072
2073 if (ac->ac_status != AC_STATUS_CONTINUE)
2074 break;
2075 }
2076 }
2077
2078 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2079 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2080 /*
2081 * We've been searching too long. Let's try to allocate
2082 * the best chunk we've found so far
2083 */
2084
2085 ext4_mb_try_best_found(ac, &e4b);
2086 if (ac->ac_status != AC_STATUS_FOUND) {
2087 /*
2088 * Someone more lucky has already allocated it.
2089 * The only thing we can do is just take first
2090 * found block(s)
2091 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2092 */
2093 ac->ac_b_ex.fe_group = 0;
2094 ac->ac_b_ex.fe_start = 0;
2095 ac->ac_b_ex.fe_len = 0;
2096 ac->ac_status = AC_STATUS_CONTINUE;
2097 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2098 cr = 3;
2099 atomic_inc(&sbi->s_mb_lost_chunks);
2100 goto repeat;
2101 }
2102 }
2103out:
2104 return err;
2105}
2106
Alex Tomasc9de5602008-01-29 00:19:52 -05002107static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2108{
2109 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002110 ext4_group_t group;
2111
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;
Alex Tomasc9de5602008-01-29 00:19:52 -05002114 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 void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2119{
2120 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002121 ext4_group_t group;
2122
2123 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002124 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002125 return NULL;
2126 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002127 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002128}
2129
2130static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2131{
2132 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002133 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002134 int i;
2135 int err;
2136 struct ext4_buddy e4b;
2137 struct sg {
2138 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002139 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002140 } sg;
2141
2142 group--;
2143 if (group == 0)
2144 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2145 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2146 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2147 "group", "free", "frags", "first",
2148 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2149 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2150
2151 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2152 sizeof(struct ext4_group_info);
2153 err = ext4_mb_load_buddy(sb, group, &e4b);
2154 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002155 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002156 return 0;
2157 }
2158 ext4_lock_group(sb, group);
2159 memcpy(&sg, ext4_get_group_info(sb, group), i);
2160 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002161 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002162
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002163 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002164 sg.info.bb_fragments, sg.info.bb_first_free);
2165 for (i = 0; i <= 13; i++)
2166 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2167 sg.info.bb_counters[i] : 0);
2168 seq_printf(seq, " ]\n");
2169
2170 return 0;
2171}
2172
2173static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2174{
2175}
2176
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002177static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002178 .start = ext4_mb_seq_groups_start,
2179 .next = ext4_mb_seq_groups_next,
2180 .stop = ext4_mb_seq_groups_stop,
2181 .show = ext4_mb_seq_groups_show,
2182};
2183
2184static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2185{
2186 struct super_block *sb = PDE(inode)->data;
2187 int rc;
2188
2189 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2190 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002191 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002192 m->private = sb;
2193 }
2194 return rc;
2195
2196}
2197
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002198static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002199 .owner = THIS_MODULE,
2200 .open = ext4_mb_seq_groups_open,
2201 .read = seq_read,
2202 .llseek = seq_lseek,
2203 .release = seq_release,
2204};
2205
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002206static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2207{
2208 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2209 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2210
2211 BUG_ON(!cachep);
2212 return cachep;
2213}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002214
2215/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002216int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002217 struct ext4_group_desc *desc)
2218{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002219 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002220 int metalen = 0;
2221 struct ext4_sb_info *sbi = EXT4_SB(sb);
2222 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002223 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002224
2225 /*
2226 * First check if this group is the first of a reserved block.
2227 * If it's true, we have to allocate a new table of pointers
2228 * to ext4_group_info structures
2229 */
2230 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2231 metalen = sizeof(*meta_group_info) <<
2232 EXT4_DESC_PER_BLOCK_BITS(sb);
2233 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2234 if (meta_group_info == NULL) {
2235 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2236 "buddy group\n");
2237 goto exit_meta_group_info;
2238 }
2239 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2240 meta_group_info;
2241 }
2242
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002243 meta_group_info =
2244 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2245 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2246
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002247 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002248 if (meta_group_info[i] == NULL) {
2249 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2250 goto exit_group_info;
2251 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002252 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002253 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2254 &(meta_group_info[i]->bb_state));
2255
2256 /*
2257 * initialize bb_free to be able to skip
2258 * empty groups without initialization
2259 */
2260 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2261 meta_group_info[i]->bb_free =
2262 ext4_free_blocks_after_init(sb, group, desc);
2263 } else {
2264 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002265 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002266 }
2267
2268 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002269 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002270 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002271 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002272
2273#ifdef DOUBLE_CHECK
2274 {
2275 struct buffer_head *bh;
2276 meta_group_info[i]->bb_bitmap =
2277 kmalloc(sb->s_blocksize, GFP_KERNEL);
2278 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2279 bh = ext4_read_block_bitmap(sb, group);
2280 BUG_ON(bh == NULL);
2281 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2282 sb->s_blocksize);
2283 put_bh(bh);
2284 }
2285#endif
2286
2287 return 0;
2288
2289exit_group_info:
2290 /* If a meta_group_info table has been allocated, release it now */
2291 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2292 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2293exit_meta_group_info:
2294 return -ENOMEM;
2295} /* ext4_mb_add_groupinfo */
2296
Alex Tomasc9de5602008-01-29 00:19:52 -05002297static int ext4_mb_init_backend(struct super_block *sb)
2298{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002299 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002300 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002301 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002302 struct ext4_super_block *es = sbi->s_es;
2303 int num_meta_group_infos;
2304 int num_meta_group_infos_max;
2305 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002306 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002307 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002308
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002309 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002310 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002311 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2312
2313 /*
2314 * This is the total number of blocks used by GDT including
2315 * the number of reserved blocks for GDT.
2316 * The s_group_info array is allocated with this value
2317 * to allow a clean online resize without a complex
2318 * manipulation of pointer.
2319 * The drawback is the unused memory when no resize
2320 * occurs but it's very low in terms of pages
2321 * (see comments below)
2322 * Need to handle this properly when META_BG resizing is allowed
2323 */
2324 num_meta_group_infos_max = num_meta_group_infos +
2325 le16_to_cpu(es->s_reserved_gdt_blocks);
2326
2327 /*
2328 * array_size is the size of s_group_info array. We round it
2329 * to the next power of two because this approximation is done
2330 * internally by kmalloc so we can have some more memory
2331 * for free here (e.g. may be used for META_BG resize).
2332 */
2333 array_size = 1;
2334 while (array_size < sizeof(*sbi->s_group_info) *
2335 num_meta_group_infos_max)
2336 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002337 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2338 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2339 * So a two level scheme suffices for now. */
Eric Sandeen4596fe02011-03-21 21:25:13 -04002340 sbi->s_group_info = kzalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002341 if (sbi->s_group_info == NULL) {
2342 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2343 return -ENOMEM;
2344 }
2345 sbi->s_buddy_cache = new_inode(sb);
2346 if (sbi->s_buddy_cache == NULL) {
2347 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2348 goto err_freesgi;
2349 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002350 sbi->s_buddy_cache->i_ino = get_next_ino();
Alex Tomasc9de5602008-01-29 00:19:52 -05002351 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002352 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002353 desc = ext4_get_group_desc(sb, i, NULL);
2354 if (desc == NULL) {
2355 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002356 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002357 goto err_freebuddy;
2358 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002359 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2360 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002361 }
2362
2363 return 0;
2364
2365err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002366 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002367 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002368 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002369 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002370 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002371 kfree(sbi->s_group_info[i]);
2372 iput(sbi->s_buddy_cache);
2373err_freesgi:
2374 kfree(sbi->s_group_info);
2375 return -ENOMEM;
2376}
2377
Eric Sandeen2892c152011-02-12 08:12:18 -05002378static void ext4_groupinfo_destroy_slabs(void)
2379{
2380 int i;
2381
2382 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2383 if (ext4_groupinfo_caches[i])
2384 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2385 ext4_groupinfo_caches[i] = NULL;
2386 }
2387}
2388
2389static int ext4_groupinfo_create_slab(size_t size)
2390{
2391 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2392 int slab_size;
2393 int blocksize_bits = order_base_2(size);
2394 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2395 struct kmem_cache *cachep;
2396
2397 if (cache_index >= NR_GRPINFO_CACHES)
2398 return -EINVAL;
2399
2400 if (unlikely(cache_index < 0))
2401 cache_index = 0;
2402
2403 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2404 if (ext4_groupinfo_caches[cache_index]) {
2405 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2406 return 0; /* Already created */
2407 }
2408
2409 slab_size = offsetof(struct ext4_group_info,
2410 bb_counters[blocksize_bits + 2]);
2411
2412 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2413 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2414 NULL);
2415
2416 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2417 if (!cachep) {
2418 printk(KERN_EMERG "EXT4: no memory for groupinfo slab cache\n");
2419 return -ENOMEM;
2420 }
2421
2422 ext4_groupinfo_caches[cache_index] = cachep;
2423
2424 return 0;
2425}
2426
Alex Tomasc9de5602008-01-29 00:19:52 -05002427int ext4_mb_init(struct super_block *sb, int needs_recovery)
2428{
2429 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002430 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002431 unsigned offset;
2432 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002433 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002434
Eric Sandeen19278052009-08-25 22:36:25 -04002435 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002436
2437 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2438 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002439 ret = -ENOMEM;
2440 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002441 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002442
Eric Sandeen19278052009-08-25 22:36:25 -04002443 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002444 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2445 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002446 ret = -ENOMEM;
2447 goto out;
2448 }
2449
Eric Sandeen2892c152011-02-12 08:12:18 -05002450 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2451 if (ret < 0)
2452 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002453
2454 /* order 0 is regular bitmap */
2455 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2456 sbi->s_mb_offsets[0] = 0;
2457
2458 i = 1;
2459 offset = 0;
2460 max = sb->s_blocksize << 2;
2461 do {
2462 sbi->s_mb_offsets[i] = offset;
2463 sbi->s_mb_maxs[i] = max;
2464 offset += 1 << (sb->s_blocksize_bits - i);
2465 max = max >> 1;
2466 i++;
2467 } while (i <= sb->s_blocksize_bits + 1);
2468
2469 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002470 ret = ext4_mb_init_backend(sb);
2471 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002472 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002473 }
2474
2475 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002476 spin_lock_init(&sbi->s_bal_lock);
2477
2478 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2479 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2480 sbi->s_mb_stats = MB_DEFAULT_STATS;
2481 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2482 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002483 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2484
Eric Sandeen730c2132008-09-13 15:23:29 -04002485 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002487 ret = -ENOMEM;
2488 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002489 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002490 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002491 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002492 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002493 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002494 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2495 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002496 spin_lock_init(&lg->lg_prealloc_lock);
2497 }
2498
Theodore Ts'o296c3552009-09-30 00:32:42 -04002499 if (sbi->s_proc)
2500 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2501 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002502
Frank Mayhar03901312009-01-07 00:06:22 -05002503 if (sbi->s_journal)
2504 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002505out:
2506 if (ret) {
2507 kfree(sbi->s_mb_offsets);
2508 kfree(sbi->s_mb_maxs);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002509 }
2510 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002511}
2512
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002513/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002514static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2515{
2516 struct ext4_prealloc_space *pa;
2517 struct list_head *cur, *tmp;
2518 int count = 0;
2519
2520 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2521 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2522 list_del(&pa->pa_group_list);
2523 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002524 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002525 }
2526 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002527 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002528
2529}
2530
2531int ext4_mb_release(struct super_block *sb)
2532{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002533 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002534 ext4_group_t i;
2535 int num_meta_group_infos;
2536 struct ext4_group_info *grinfo;
2537 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002538 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002539
Alex Tomasc9de5602008-01-29 00:19:52 -05002540 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002541 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002542 grinfo = ext4_get_group_info(sb, i);
2543#ifdef DOUBLE_CHECK
2544 kfree(grinfo->bb_bitmap);
2545#endif
2546 ext4_lock_group(sb, i);
2547 ext4_mb_cleanup_pa(grinfo);
2548 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002549 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002550 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002551 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002552 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2553 EXT4_DESC_PER_BLOCK_BITS(sb);
2554 for (i = 0; i < num_meta_group_infos; i++)
2555 kfree(sbi->s_group_info[i]);
2556 kfree(sbi->s_group_info);
2557 }
2558 kfree(sbi->s_mb_offsets);
2559 kfree(sbi->s_mb_maxs);
2560 if (sbi->s_buddy_cache)
2561 iput(sbi->s_buddy_cache);
2562 if (sbi->s_mb_stats) {
2563 printk(KERN_INFO
2564 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2565 atomic_read(&sbi->s_bal_allocated),
2566 atomic_read(&sbi->s_bal_reqs),
2567 atomic_read(&sbi->s_bal_success));
2568 printk(KERN_INFO
2569 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2570 "%u 2^N hits, %u breaks, %u lost\n",
2571 atomic_read(&sbi->s_bal_ex_scanned),
2572 atomic_read(&sbi->s_bal_goals),
2573 atomic_read(&sbi->s_bal_2orders),
2574 atomic_read(&sbi->s_bal_breaks),
2575 atomic_read(&sbi->s_mb_lost_chunks));
2576 printk(KERN_INFO
2577 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2578 sbi->s_mb_buddies_generated++,
2579 sbi->s_mb_generation_time);
2580 printk(KERN_INFO
2581 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2582 atomic_read(&sbi->s_mb_preallocated),
2583 atomic_read(&sbi->s_mb_discarded));
2584 }
2585
Eric Sandeen730c2132008-09-13 15:23:29 -04002586 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002587 if (sbi->s_proc)
2588 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002589
2590 return 0;
2591}
2592
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002593static inline int ext4_issue_discard(struct super_block *sb,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002594 ext4_group_t block_group, ext4_grpblk_t block, int count)
2595{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002596 ext4_fsblk_t discard_block;
2597
2598 discard_block = block + ext4_group_first_block_no(sb, block_group);
2599 trace_ext4_discard_blocks(sb,
2600 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002601 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002602}
2603
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002604/*
2605 * This function is called by the jbd2 layer once the commit has finished,
2606 * so we know we can free the blocks that were released with that commit.
2607 */
2608static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002609{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002610 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002611 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002612 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002613 int err, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002614 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002615 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002616
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002617 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2618 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002619
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002620 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002621 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002622
Theodore Ts'od9f34502011-04-30 13:47:24 -04002623 if (test_opt(sb, DISCARD))
2624 ext4_issue_discard(sb, entry->group,
2625 entry->start_blk, entry->count);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002626
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002627 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002628 /* we expect to find existing buddy because it's pinned */
2629 BUG_ON(err != 0);
2630
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002631 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002632 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002633 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002634 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002635 ext4_lock_group(sb, entry->group);
2636 /* Take it out of per group rb tree */
2637 rb_erase(&entry->node, &(db->bb_free_root));
2638 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2639
2640 if (!db->bb_free_root.rb_node) {
2641 /* No more items in the per group rb tree
2642 * balance refcounts from ext4_mb_free_metadata()
2643 */
2644 page_cache_release(e4b.bd_buddy_page);
2645 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002646 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002647 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002648 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002649 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002650 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002651
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002652 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002653}
2654
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002655#ifdef CONFIG_EXT4_DEBUG
2656u8 mb_enable_debug __read_mostly;
2657
2658static struct dentry *debugfs_dir;
2659static struct dentry *debugfs_debug;
2660
2661static void __init ext4_create_debugfs_entry(void)
2662{
2663 debugfs_dir = debugfs_create_dir("ext4", NULL);
2664 if (debugfs_dir)
2665 debugfs_debug = debugfs_create_u8("mballoc-debug",
2666 S_IRUGO | S_IWUSR,
2667 debugfs_dir,
2668 &mb_enable_debug);
2669}
2670
2671static void ext4_remove_debugfs_entry(void)
2672{
2673 debugfs_remove(debugfs_debug);
2674 debugfs_remove(debugfs_dir);
2675}
2676
2677#else
2678
2679static void __init ext4_create_debugfs_entry(void)
2680{
2681}
2682
2683static void ext4_remove_debugfs_entry(void)
2684{
2685}
2686
2687#endif
2688
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002689int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002690{
Theodore Ts'o16828082010-10-27 21:30:09 -04002691 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2692 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002693 if (ext4_pspace_cachep == NULL)
2694 return -ENOMEM;
2695
Theodore Ts'o16828082010-10-27 21:30:09 -04002696 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2697 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002698 if (ext4_ac_cachep == NULL) {
2699 kmem_cache_destroy(ext4_pspace_cachep);
2700 return -ENOMEM;
2701 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002702
Theodore Ts'o16828082010-10-27 21:30:09 -04002703 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2704 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002705 if (ext4_free_ext_cachep == NULL) {
2706 kmem_cache_destroy(ext4_pspace_cachep);
2707 kmem_cache_destroy(ext4_ac_cachep);
2708 return -ENOMEM;
2709 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002710 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002711 return 0;
2712}
2713
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002714void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002715{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002716 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002717 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2718 * before destroying the slab cache.
2719 */
2720 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002721 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002722 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002723 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002724 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002725 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002726}
2727
2728
2729/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002730 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002731 * Returns 0 if success or error code
2732 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002733static noinline_for_stack int
2734ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002735 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002736{
2737 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002738 struct ext4_group_desc *gdp;
2739 struct buffer_head *gdp_bh;
2740 struct ext4_sb_info *sbi;
2741 struct super_block *sb;
2742 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002743 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002744
2745 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2746 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2747
2748 sb = ac->ac_sb;
2749 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002750
2751 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002752 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002753 if (!bitmap_bh)
2754 goto out_err;
2755
2756 err = ext4_journal_get_write_access(handle, bitmap_bh);
2757 if (err)
2758 goto out_err;
2759
2760 err = -EIO;
2761 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2762 if (!gdp)
2763 goto out_err;
2764
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002765 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002766 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002767
Alex Tomasc9de5602008-01-29 00:19:52 -05002768 err = ext4_journal_get_write_access(handle, gdp_bh);
2769 if (err)
2770 goto out_err;
2771
Akinobu Mitabda00de2010-03-03 23:53:25 -05002772 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002773
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002774 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002775 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002776 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002777 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002778 /* File system mounted not to panic on error
2779 * Fix the bitmap and repeat the block allocation
2780 * We leak some of the blocks here.
2781 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002782 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2783 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2784 ac->ac_b_ex.fe_len);
2785 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002786 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002787 if (!err)
2788 err = -EAGAIN;
2789 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002790 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002791
2792 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002793#ifdef AGGRESSIVE_CHECK
2794 {
2795 int i;
2796 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2797 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2798 bitmap_bh->b_data));
2799 }
2800 }
2801#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002802 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 -05002803 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2804 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002805 ext4_free_blks_set(sb, gdp,
2806 ext4_free_blocks_after_init(sb,
2807 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002808 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002809 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2810 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002811 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002812
2813 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002814 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002815 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002816 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002817 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002818 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2819 /* release all the reserved blocks if non delalloc */
2820 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002821
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002822 if (sbi->s_log_groups_per_flex) {
2823 ext4_group_t flex_group = ext4_flex_group(sbi,
2824 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002825 atomic_sub(ac->ac_b_ex.fe_len,
2826 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002827 }
2828
Frank Mayhar03901312009-01-07 00:06:22 -05002829 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002830 if (err)
2831 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002832 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002833
2834out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002835 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002836 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002837 return err;
2838}
2839
2840/*
2841 * here we normalize request for locality group
2842 * Group request are normalized to s_strip size if we set the same via mount
2843 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002844 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002845 *
2846 * XXX: should we try to preallocate more than the group has now?
2847 */
2848static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2849{
2850 struct super_block *sb = ac->ac_sb;
2851 struct ext4_locality_group *lg = ac->ac_lg;
2852
2853 BUG_ON(lg == NULL);
2854 if (EXT4_SB(sb)->s_stripe)
2855 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2856 else
2857 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002858 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002859 current->pid, ac->ac_g_ex.fe_len);
2860}
2861
2862/*
2863 * Normalization means making request better in terms of
2864 * size and alignment
2865 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002866static noinline_for_stack void
2867ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002868 struct ext4_allocation_request *ar)
2869{
2870 int bsbits, max;
2871 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002872 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002873 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002874 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002875 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002876
2877 /* do normalize only data requests, metadata requests
2878 do not need preallocation */
2879 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2880 return;
2881
2882 /* sometime caller may want exact blocks */
2883 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2884 return;
2885
2886 /* caller may indicate that preallocation isn't
2887 * required (it's a tail, for example) */
2888 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2889 return;
2890
2891 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2892 ext4_mb_normalize_group_request(ac);
2893 return ;
2894 }
2895
2896 bsbits = ac->ac_sb->s_blocksize_bits;
2897
2898 /* first, let's learn actual file size
2899 * given current request is allocated */
2900 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2901 size = size << bsbits;
2902 if (size < i_size_read(ac->ac_inode))
2903 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002904 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002905
Valerie Clement19304792008-05-13 19:31:14 -04002906 /* max size of free chunks */
2907 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002908
Valerie Clement19304792008-05-13 19:31:14 -04002909#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2910 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002911
2912 /* first, try to predict filesize */
2913 /* XXX: should this table be tunable? */
2914 start_off = 0;
2915 if (size <= 16 * 1024) {
2916 size = 16 * 1024;
2917 } else if (size <= 32 * 1024) {
2918 size = 32 * 1024;
2919 } else if (size <= 64 * 1024) {
2920 size = 64 * 1024;
2921 } else if (size <= 128 * 1024) {
2922 size = 128 * 1024;
2923 } else if (size <= 256 * 1024) {
2924 size = 256 * 1024;
2925 } else if (size <= 512 * 1024) {
2926 size = 512 * 1024;
2927 } else if (size <= 1024 * 1024) {
2928 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002929 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002930 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002931 (21 - bsbits)) << 21;
2932 size = 2 * 1024 * 1024;
2933 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002934 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2935 (22 - bsbits)) << 22;
2936 size = 4 * 1024 * 1024;
2937 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002938 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002939 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2940 (23 - bsbits)) << 23;
2941 size = 8 * 1024 * 1024;
2942 } else {
2943 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2944 size = ac->ac_o_ex.fe_len << bsbits;
2945 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002946 size = size >> bsbits;
2947 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002948
2949 /* don't cover already allocated blocks in selected range */
2950 if (ar->pleft && start <= ar->lleft) {
2951 size -= ar->lleft + 1 - start;
2952 start = ar->lleft + 1;
2953 }
2954 if (ar->pright && start + size - 1 >= ar->lright)
2955 size -= start + size - ar->lright;
2956
2957 end = start + size;
2958
2959 /* check we don't cross already preallocated blocks */
2960 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002961 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002962 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002963
Alex Tomasc9de5602008-01-29 00:19:52 -05002964 if (pa->pa_deleted)
2965 continue;
2966 spin_lock(&pa->pa_lock);
2967 if (pa->pa_deleted) {
2968 spin_unlock(&pa->pa_lock);
2969 continue;
2970 }
2971
2972 pa_end = pa->pa_lstart + pa->pa_len;
2973
2974 /* PA must not overlap original request */
2975 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2976 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2977
Eric Sandeen38877f42009-08-17 23:55:24 -04002978 /* skip PAs this normalized request doesn't overlap with */
2979 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002980 spin_unlock(&pa->pa_lock);
2981 continue;
2982 }
2983 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2984
Eric Sandeen38877f42009-08-17 23:55:24 -04002985 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002986 if (pa_end <= ac->ac_o_ex.fe_logical) {
2987 BUG_ON(pa_end < start);
2988 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002989 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002990 BUG_ON(pa->pa_lstart > end);
2991 end = pa->pa_lstart;
2992 }
2993 spin_unlock(&pa->pa_lock);
2994 }
2995 rcu_read_unlock();
2996 size = end - start;
2997
2998 /* XXX: extra loop to check we really don't overlap preallocations */
2999 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003000 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003001 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003002 spin_lock(&pa->pa_lock);
3003 if (pa->pa_deleted == 0) {
3004 pa_end = pa->pa_lstart + pa->pa_len;
3005 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3006 }
3007 spin_unlock(&pa->pa_lock);
3008 }
3009 rcu_read_unlock();
3010
3011 if (start + size <= ac->ac_o_ex.fe_logical &&
3012 start > ac->ac_o_ex.fe_logical) {
3013 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3014 (unsigned long) start, (unsigned long) size,
3015 (unsigned long) ac->ac_o_ex.fe_logical);
3016 }
3017 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3018 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003019 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003020
3021 /* now prepare goal request */
3022
3023 /* XXX: is it better to align blocks WRT to logical
3024 * placement or satisfy big request as is */
3025 ac->ac_g_ex.fe_logical = start;
3026 ac->ac_g_ex.fe_len = size;
3027
3028 /* define goal start in order to merge */
3029 if (ar->pright && (ar->lright == (start + size))) {
3030 /* merge to the right */
3031 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3032 &ac->ac_f_ex.fe_group,
3033 &ac->ac_f_ex.fe_start);
3034 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3035 }
3036 if (ar->pleft && (ar->lleft + 1 == start)) {
3037 /* merge to the left */
3038 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3039 &ac->ac_f_ex.fe_group,
3040 &ac->ac_f_ex.fe_start);
3041 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3042 }
3043
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003044 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003045 (unsigned) orig_size, (unsigned) start);
3046}
3047
3048static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3049{
3050 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3051
3052 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3053 atomic_inc(&sbi->s_bal_reqs);
3054 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003055 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003056 atomic_inc(&sbi->s_bal_success);
3057 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3058 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3059 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3060 atomic_inc(&sbi->s_bal_goals);
3061 if (ac->ac_found > sbi->s_mb_max_to_scan)
3062 atomic_inc(&sbi->s_bal_breaks);
3063 }
3064
Theodore Ts'o296c3552009-09-30 00:32:42 -04003065 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3066 trace_ext4_mballoc_alloc(ac);
3067 else
3068 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003069}
3070
3071/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003072 * Called on failure; free up any blocks from the inode PA for this
3073 * context. We don't need this for MB_GROUP_PA because we only change
3074 * pa_free in ext4_mb_release_context(), but on failure, we've already
3075 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3076 */
3077static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3078{
3079 struct ext4_prealloc_space *pa = ac->ac_pa;
3080 int len;
3081
3082 if (pa && pa->pa_type == MB_INODE_PA) {
3083 len = ac->ac_b_ex.fe_len;
3084 pa->pa_free += len;
3085 }
3086
3087}
3088
3089/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003090 * use blocks preallocated to inode
3091 */
3092static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3093 struct ext4_prealloc_space *pa)
3094{
3095 ext4_fsblk_t start;
3096 ext4_fsblk_t end;
3097 int len;
3098
3099 /* found preallocated blocks, use them */
3100 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3101 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3102 len = end - start;
3103 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3104 &ac->ac_b_ex.fe_start);
3105 ac->ac_b_ex.fe_len = len;
3106 ac->ac_status = AC_STATUS_FOUND;
3107 ac->ac_pa = pa;
3108
3109 BUG_ON(start < pa->pa_pstart);
3110 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3111 BUG_ON(pa->pa_free < len);
3112 pa->pa_free -= len;
3113
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003114 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003115}
3116
3117/*
3118 * use blocks preallocated to locality group
3119 */
3120static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3121 struct ext4_prealloc_space *pa)
3122{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003123 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003124
Alex Tomasc9de5602008-01-29 00:19:52 -05003125 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3126 &ac->ac_b_ex.fe_group,
3127 &ac->ac_b_ex.fe_start);
3128 ac->ac_b_ex.fe_len = len;
3129 ac->ac_status = AC_STATUS_FOUND;
3130 ac->ac_pa = pa;
3131
3132 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003133 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003134 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003135 * in on-disk bitmap -- see ext4_mb_release_context()
3136 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003137 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003138 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003139}
3140
3141/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003142 * Return the prealloc space that have minimal distance
3143 * from the goal block. @cpa is the prealloc
3144 * space that is having currently known minimal distance
3145 * from the goal block.
3146 */
3147static struct ext4_prealloc_space *
3148ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3149 struct ext4_prealloc_space *pa,
3150 struct ext4_prealloc_space *cpa)
3151{
3152 ext4_fsblk_t cur_distance, new_distance;
3153
3154 if (cpa == NULL) {
3155 atomic_inc(&pa->pa_count);
3156 return pa;
3157 }
3158 cur_distance = abs(goal_block - cpa->pa_pstart);
3159 new_distance = abs(goal_block - pa->pa_pstart);
3160
Coly Li5a54b2f2011-02-24 14:10:05 -05003161 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003162 return cpa;
3163
3164 /* drop the previous reference */
3165 atomic_dec(&cpa->pa_count);
3166 atomic_inc(&pa->pa_count);
3167 return pa;
3168}
3169
3170/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003171 * search goal blocks in preallocated space
3172 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003173static noinline_for_stack int
3174ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003175{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003176 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003177 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3178 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003179 struct ext4_prealloc_space *pa, *cpa = NULL;
3180 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003181
3182 /* only data can be preallocated */
3183 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3184 return 0;
3185
3186 /* first, try per-file preallocation */
3187 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003188 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003189
3190 /* all fields in this condition don't change,
3191 * so we can skip locking for them */
3192 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3193 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3194 continue;
3195
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003196 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003197 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003198 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3199 continue;
3200
Alex Tomasc9de5602008-01-29 00:19:52 -05003201 /* found preallocated blocks, use them */
3202 spin_lock(&pa->pa_lock);
3203 if (pa->pa_deleted == 0 && pa->pa_free) {
3204 atomic_inc(&pa->pa_count);
3205 ext4_mb_use_inode_pa(ac, pa);
3206 spin_unlock(&pa->pa_lock);
3207 ac->ac_criteria = 10;
3208 rcu_read_unlock();
3209 return 1;
3210 }
3211 spin_unlock(&pa->pa_lock);
3212 }
3213 rcu_read_unlock();
3214
3215 /* can we use group allocation? */
3216 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3217 return 0;
3218
3219 /* inode may have no locality group for some reason */
3220 lg = ac->ac_lg;
3221 if (lg == NULL)
3222 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003223 order = fls(ac->ac_o_ex.fe_len) - 1;
3224 if (order > PREALLOC_TB_SIZE - 1)
3225 /* The max size of hash table is PREALLOC_TB_SIZE */
3226 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003227
Akinobu Mitabda00de2010-03-03 23:53:25 -05003228 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003229 /*
3230 * search for the prealloc space that is having
3231 * minimal distance from the goal block.
3232 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003233 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3234 rcu_read_lock();
3235 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3236 pa_inode_list) {
3237 spin_lock(&pa->pa_lock);
3238 if (pa->pa_deleted == 0 &&
3239 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003240
3241 cpa = ext4_mb_check_group_pa(goal_block,
3242 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003243 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003244 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003245 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003246 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003247 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003248 if (cpa) {
3249 ext4_mb_use_group_pa(ac, cpa);
3250 ac->ac_criteria = 20;
3251 return 1;
3252 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003253 return 0;
3254}
3255
3256/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003257 * the function goes through all block freed in the group
3258 * but not yet committed and marks them used in in-core bitmap.
3259 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003260 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003261 */
3262static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3263 ext4_group_t group)
3264{
3265 struct rb_node *n;
3266 struct ext4_group_info *grp;
3267 struct ext4_free_data *entry;
3268
3269 grp = ext4_get_group_info(sb, group);
3270 n = rb_first(&(grp->bb_free_root));
3271
3272 while (n) {
3273 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003274 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003275 n = rb_next(n);
3276 }
3277 return;
3278}
3279
3280/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003281 * the function goes through all preallocation in this group and marks them
3282 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003283 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003284 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003285static noinline_for_stack
3286void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003287 ext4_group_t group)
3288{
3289 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3290 struct ext4_prealloc_space *pa;
3291 struct list_head *cur;
3292 ext4_group_t groupnr;
3293 ext4_grpblk_t start;
3294 int preallocated = 0;
3295 int count = 0;
3296 int len;
3297
3298 /* all form of preallocation discards first load group,
3299 * so the only competing code is preallocation use.
3300 * we don't need any locking here
3301 * notice we do NOT ignore preallocations with pa_deleted
3302 * otherwise we could leave used blocks available for
3303 * allocation in buddy when concurrent ext4_mb_put_pa()
3304 * is dropping preallocation
3305 */
3306 list_for_each(cur, &grp->bb_prealloc_list) {
3307 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3308 spin_lock(&pa->pa_lock);
3309 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3310 &groupnr, &start);
3311 len = pa->pa_len;
3312 spin_unlock(&pa->pa_lock);
3313 if (unlikely(len == 0))
3314 continue;
3315 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003316 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003317 preallocated += len;
3318 count++;
3319 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003320 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003321}
3322
3323static void ext4_mb_pa_callback(struct rcu_head *head)
3324{
3325 struct ext4_prealloc_space *pa;
3326 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3327 kmem_cache_free(ext4_pspace_cachep, pa);
3328}
3329
3330/*
3331 * drops a reference to preallocated space descriptor
3332 * if this was the last reference and the space is consumed
3333 */
3334static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3335 struct super_block *sb, struct ext4_prealloc_space *pa)
3336{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003337 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003338 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003339
3340 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3341 return;
3342
3343 /* in this short window concurrent discard can set pa_deleted */
3344 spin_lock(&pa->pa_lock);
3345 if (pa->pa_deleted == 1) {
3346 spin_unlock(&pa->pa_lock);
3347 return;
3348 }
3349
3350 pa->pa_deleted = 1;
3351 spin_unlock(&pa->pa_lock);
3352
Eric Sandeend33a1972009-03-16 23:25:40 -04003353 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003354 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003355 * If doing group-based preallocation, pa_pstart may be in the
3356 * next group when pa is used up
3357 */
3358 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003359 grp_blk--;
3360
3361 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003362
3363 /*
3364 * possible race:
3365 *
3366 * P1 (buddy init) P2 (regular allocation)
3367 * find block B in PA
3368 * copy on-disk bitmap to buddy
3369 * mark B in on-disk bitmap
3370 * drop PA from group
3371 * mark all PAs in buddy
3372 *
3373 * thus, P1 initializes buddy with B available. to prevent this
3374 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3375 * against that pair
3376 */
3377 ext4_lock_group(sb, grp);
3378 list_del(&pa->pa_group_list);
3379 ext4_unlock_group(sb, grp);
3380
3381 spin_lock(pa->pa_obj_lock);
3382 list_del_rcu(&pa->pa_inode_list);
3383 spin_unlock(pa->pa_obj_lock);
3384
3385 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3386}
3387
3388/*
3389 * creates new preallocated space for given inode
3390 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003391static noinline_for_stack int
3392ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003393{
3394 struct super_block *sb = ac->ac_sb;
3395 struct ext4_prealloc_space *pa;
3396 struct ext4_group_info *grp;
3397 struct ext4_inode_info *ei;
3398
3399 /* preallocate only when found space is larger then requested */
3400 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3401 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3402 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3403
3404 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3405 if (pa == NULL)
3406 return -ENOMEM;
3407
3408 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3409 int winl;
3410 int wins;
3411 int win;
3412 int offs;
3413
3414 /* we can't allocate as much as normalizer wants.
3415 * so, found space must get proper lstart
3416 * to cover original request */
3417 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3418 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3419
3420 /* we're limited by original request in that
3421 * logical block must be covered any way
3422 * winl is window we can move our chunk within */
3423 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3424
3425 /* also, we should cover whole original request */
3426 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3427
3428 /* the smallest one defines real window */
3429 win = min(winl, wins);
3430
3431 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3432 if (offs && offs < win)
3433 win = offs;
3434
3435 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3436 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3437 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3438 }
3439
3440 /* preallocation can change ac_b_ex, thus we store actually
3441 * allocated blocks for history */
3442 ac->ac_f_ex = ac->ac_b_ex;
3443
3444 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3445 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3446 pa->pa_len = ac->ac_b_ex.fe_len;
3447 pa->pa_free = pa->pa_len;
3448 atomic_set(&pa->pa_count, 1);
3449 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003450 INIT_LIST_HEAD(&pa->pa_inode_list);
3451 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003452 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003453 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003454
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003455 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003456 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003457 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003458
3459 ext4_mb_use_inode_pa(ac, pa);
3460 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3461
3462 ei = EXT4_I(ac->ac_inode);
3463 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3464
3465 pa->pa_obj_lock = &ei->i_prealloc_lock;
3466 pa->pa_inode = ac->ac_inode;
3467
3468 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3469 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3470 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3471
3472 spin_lock(pa->pa_obj_lock);
3473 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3474 spin_unlock(pa->pa_obj_lock);
3475
3476 return 0;
3477}
3478
3479/*
3480 * creates new preallocated space for locality group inodes belongs to
3481 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003482static noinline_for_stack int
3483ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003484{
3485 struct super_block *sb = ac->ac_sb;
3486 struct ext4_locality_group *lg;
3487 struct ext4_prealloc_space *pa;
3488 struct ext4_group_info *grp;
3489
3490 /* preallocate only when found space is larger then requested */
3491 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3492 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3493 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3494
3495 BUG_ON(ext4_pspace_cachep == NULL);
3496 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3497 if (pa == NULL)
3498 return -ENOMEM;
3499
3500 /* preallocation can change ac_b_ex, thus we store actually
3501 * allocated blocks for history */
3502 ac->ac_f_ex = ac->ac_b_ex;
3503
3504 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3505 pa->pa_lstart = pa->pa_pstart;
3506 pa->pa_len = ac->ac_b_ex.fe_len;
3507 pa->pa_free = pa->pa_len;
3508 atomic_set(&pa->pa_count, 1);
3509 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003510 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003511 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003512 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003513 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003514
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003515 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003516 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3517 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003518
3519 ext4_mb_use_group_pa(ac, pa);
3520 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3521
3522 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3523 lg = ac->ac_lg;
3524 BUG_ON(lg == NULL);
3525
3526 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3527 pa->pa_inode = NULL;
3528
3529 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3530 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3531 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3532
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003533 /*
3534 * We will later add the new pa to the right bucket
3535 * after updating the pa_free in ext4_mb_release_context
3536 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003537 return 0;
3538}
3539
3540static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3541{
3542 int err;
3543
3544 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3545 err = ext4_mb_new_group_pa(ac);
3546 else
3547 err = ext4_mb_new_inode_pa(ac);
3548 return err;
3549}
3550
3551/*
3552 * finds all unused blocks in on-disk bitmap, frees them in
3553 * in-core bitmap and buddy.
3554 * @pa must be unlinked from inode and group lists, so that
3555 * nobody else can find/use it.
3556 * the caller MUST hold group/inode locks.
3557 * TODO: optimize the case when there are no in-core structures yet
3558 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003559static noinline_for_stack int
3560ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003561 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003562{
Alex Tomasc9de5602008-01-29 00:19:52 -05003563 struct super_block *sb = e4b->bd_sb;
3564 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003565 unsigned int end;
3566 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003567 ext4_group_t group;
3568 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003569 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003570 int err = 0;
3571 int free = 0;
3572
3573 BUG_ON(pa->pa_deleted == 0);
3574 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003575 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003576 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3577 end = bit + pa->pa_len;
3578
Alex Tomasc9de5602008-01-29 00:19:52 -05003579 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003580 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003581 if (bit >= end)
3582 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003583 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003584 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003585 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3586 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003587 free += next - bit;
3588
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003589 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
3590 trace_ext4_mb_release_inode_pa(sb, pa->pa_inode, pa,
3591 grp_blk_start + bit, next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003592 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3593 bit = next + 1;
3594 }
3595 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003596 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003597 pa, (unsigned long) pa->pa_lstart,
3598 (unsigned long) pa->pa_pstart,
3599 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003600 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003601 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003602 /*
3603 * pa is already deleted so we use the value obtained
3604 * from the bitmap and continue.
3605 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003606 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003607 atomic_add(free, &sbi->s_mb_discarded);
3608
3609 return err;
3610}
3611
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003612static noinline_for_stack int
3613ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003614 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003615{
Alex Tomasc9de5602008-01-29 00:19:52 -05003616 struct super_block *sb = e4b->bd_sb;
3617 ext4_group_t group;
3618 ext4_grpblk_t bit;
3619
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003620 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003621 BUG_ON(pa->pa_deleted == 0);
3622 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3623 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3624 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3625 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003626 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003627
3628 return 0;
3629}
3630
3631/*
3632 * releases all preallocations in given group
3633 *
3634 * first, we need to decide discard policy:
3635 * - when do we discard
3636 * 1) ENOSPC
3637 * - how many do we discard
3638 * 1) how many requested
3639 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003640static noinline_for_stack int
3641ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003642 ext4_group_t group, int needed)
3643{
3644 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3645 struct buffer_head *bitmap_bh = NULL;
3646 struct ext4_prealloc_space *pa, *tmp;
3647 struct list_head list;
3648 struct ext4_buddy e4b;
3649 int err;
3650 int busy = 0;
3651 int free = 0;
3652
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003653 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003654
3655 if (list_empty(&grp->bb_prealloc_list))
3656 return 0;
3657
Theodore Ts'o574ca172008-07-11 19:27:31 -04003658 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003659 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003660 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003661 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003662 }
3663
3664 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003665 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003666 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003667 put_bh(bitmap_bh);
3668 return 0;
3669 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003670
3671 if (needed == 0)
3672 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3673
Alex Tomasc9de5602008-01-29 00:19:52 -05003674 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003675repeat:
3676 ext4_lock_group(sb, group);
3677 list_for_each_entry_safe(pa, tmp,
3678 &grp->bb_prealloc_list, pa_group_list) {
3679 spin_lock(&pa->pa_lock);
3680 if (atomic_read(&pa->pa_count)) {
3681 spin_unlock(&pa->pa_lock);
3682 busy = 1;
3683 continue;
3684 }
3685 if (pa->pa_deleted) {
3686 spin_unlock(&pa->pa_lock);
3687 continue;
3688 }
3689
3690 /* seems this one can be freed ... */
3691 pa->pa_deleted = 1;
3692
3693 /* we can trust pa_free ... */
3694 free += pa->pa_free;
3695
3696 spin_unlock(&pa->pa_lock);
3697
3698 list_del(&pa->pa_group_list);
3699 list_add(&pa->u.pa_tmp_list, &list);
3700 }
3701
3702 /* if we still need more blocks and some PAs were used, try again */
3703 if (free < needed && busy) {
3704 busy = 0;
3705 ext4_unlock_group(sb, group);
3706 /*
3707 * Yield the CPU here so that we don't get soft lockup
3708 * in non preempt case.
3709 */
3710 yield();
3711 goto repeat;
3712 }
3713
3714 /* found anything to free? */
3715 if (list_empty(&list)) {
3716 BUG_ON(free != 0);
3717 goto out;
3718 }
3719
3720 /* now free all selected PAs */
3721 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3722
3723 /* remove from object (inode or locality group) */
3724 spin_lock(pa->pa_obj_lock);
3725 list_del_rcu(&pa->pa_inode_list);
3726 spin_unlock(pa->pa_obj_lock);
3727
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003728 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003729 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003730 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003731 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003732
3733 list_del(&pa->u.pa_tmp_list);
3734 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3735 }
3736
3737out:
3738 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003739 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003740 put_bh(bitmap_bh);
3741 return free;
3742}
3743
3744/*
3745 * releases all non-used preallocated blocks for given inode
3746 *
3747 * It's important to discard preallocations under i_data_sem
3748 * We don't want another block to be served from the prealloc
3749 * space when we are discarding the inode prealloc space.
3750 *
3751 * FIXME!! Make sure it is valid at all the call sites
3752 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003753void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003754{
3755 struct ext4_inode_info *ei = EXT4_I(inode);
3756 struct super_block *sb = inode->i_sb;
3757 struct buffer_head *bitmap_bh = NULL;
3758 struct ext4_prealloc_space *pa, *tmp;
3759 ext4_group_t group = 0;
3760 struct list_head list;
3761 struct ext4_buddy e4b;
3762 int err;
3763
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003764 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003765 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3766 return;
3767 }
3768
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003769 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003770 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003771
3772 INIT_LIST_HEAD(&list);
3773
3774repeat:
3775 /* first, collect all pa's in the inode */
3776 spin_lock(&ei->i_prealloc_lock);
3777 while (!list_empty(&ei->i_prealloc_list)) {
3778 pa = list_entry(ei->i_prealloc_list.next,
3779 struct ext4_prealloc_space, pa_inode_list);
3780 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3781 spin_lock(&pa->pa_lock);
3782 if (atomic_read(&pa->pa_count)) {
3783 /* this shouldn't happen often - nobody should
3784 * use preallocation while we're discarding it */
3785 spin_unlock(&pa->pa_lock);
3786 spin_unlock(&ei->i_prealloc_lock);
3787 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3788 WARN_ON(1);
3789 schedule_timeout_uninterruptible(HZ);
3790 goto repeat;
3791
3792 }
3793 if (pa->pa_deleted == 0) {
3794 pa->pa_deleted = 1;
3795 spin_unlock(&pa->pa_lock);
3796 list_del_rcu(&pa->pa_inode_list);
3797 list_add(&pa->u.pa_tmp_list, &list);
3798 continue;
3799 }
3800
3801 /* someone is deleting pa right now */
3802 spin_unlock(&pa->pa_lock);
3803 spin_unlock(&ei->i_prealloc_lock);
3804
3805 /* we have to wait here because pa_deleted
3806 * doesn't mean pa is already unlinked from
3807 * the list. as we might be called from
3808 * ->clear_inode() the inode will get freed
3809 * and concurrent thread which is unlinking
3810 * pa from inode's list may access already
3811 * freed memory, bad-bad-bad */
3812
3813 /* XXX: if this happens too often, we can
3814 * add a flag to force wait only in case
3815 * of ->clear_inode(), but not in case of
3816 * regular truncate */
3817 schedule_timeout_uninterruptible(HZ);
3818 goto repeat;
3819 }
3820 spin_unlock(&ei->i_prealloc_lock);
3821
3822 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003823 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003824 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3825
3826 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003827 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003828 ext4_error(sb, "Error loading buddy information for %u",
3829 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003830 continue;
3831 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003832
Theodore Ts'o574ca172008-07-11 19:27:31 -04003833 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003834 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003835 ext4_error(sb, "Error reading block bitmap for %u",
3836 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003837 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003838 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003839 }
3840
3841 ext4_lock_group(sb, group);
3842 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003843 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003844 ext4_unlock_group(sb, group);
3845
Jing Zhange39e07f2010-05-14 00:00:00 -04003846 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003847 put_bh(bitmap_bh);
3848
3849 list_del(&pa->u.pa_tmp_list);
3850 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3851 }
3852}
3853
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003854#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003855static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3856{
3857 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003858 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003859
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003860 if (!mb_enable_debug ||
3861 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003862 return;
3863
Alex Tomasc9de5602008-01-29 00:19:52 -05003864 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3865 " Allocation context details:\n");
3866 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3867 ac->ac_status, ac->ac_flags);
3868 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3869 "best %lu/%lu/%lu@%lu cr %d\n",
3870 (unsigned long)ac->ac_o_ex.fe_group,
3871 (unsigned long)ac->ac_o_ex.fe_start,
3872 (unsigned long)ac->ac_o_ex.fe_len,
3873 (unsigned long)ac->ac_o_ex.fe_logical,
3874 (unsigned long)ac->ac_g_ex.fe_group,
3875 (unsigned long)ac->ac_g_ex.fe_start,
3876 (unsigned long)ac->ac_g_ex.fe_len,
3877 (unsigned long)ac->ac_g_ex.fe_logical,
3878 (unsigned long)ac->ac_b_ex.fe_group,
3879 (unsigned long)ac->ac_b_ex.fe_start,
3880 (unsigned long)ac->ac_b_ex.fe_len,
3881 (unsigned long)ac->ac_b_ex.fe_logical,
3882 (int)ac->ac_criteria);
3883 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3884 ac->ac_found);
3885 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003886 ngroups = ext4_get_groups_count(sb);
3887 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003888 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3889 struct ext4_prealloc_space *pa;
3890 ext4_grpblk_t start;
3891 struct list_head *cur;
3892 ext4_lock_group(sb, i);
3893 list_for_each(cur, &grp->bb_prealloc_list) {
3894 pa = list_entry(cur, struct ext4_prealloc_space,
3895 pa_group_list);
3896 spin_lock(&pa->pa_lock);
3897 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3898 NULL, &start);
3899 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003900 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3901 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003902 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003903 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003904
3905 if (grp->bb_free == 0)
3906 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003907 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003908 i, grp->bb_free, grp->bb_fragments);
3909 }
3910 printk(KERN_ERR "\n");
3911}
3912#else
3913static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3914{
3915 return;
3916}
3917#endif
3918
3919/*
3920 * We use locality group preallocation for small size file. The size of the
3921 * file is determined by the current size or the resulting size after
3922 * allocation which ever is larger
3923 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003924 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003925 */
3926static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3927{
3928 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3929 int bsbits = ac->ac_sb->s_blocksize_bits;
3930 loff_t size, isize;
3931
3932 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3933 return;
3934
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003935 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3936 return;
3937
Alex Tomasc9de5602008-01-29 00:19:52 -05003938 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003939 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3940 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003941
Theodore Ts'o50797482009-09-18 13:34:02 -04003942 if ((size == isize) &&
3943 !ext4_fs_is_busy(sbi) &&
3944 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3945 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3946 return;
3947 }
3948
Alex Tomasc9de5602008-01-29 00:19:52 -05003949 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003950 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003951 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003952 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003953 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003954 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003955
3956 BUG_ON(ac->ac_lg != NULL);
3957 /*
3958 * locality group prealloc space are per cpu. The reason for having
3959 * per cpu locality group is to reduce the contention between block
3960 * request from multiple CPUs.
3961 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003962 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003963
3964 /* we're going to use group allocation */
3965 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3966
3967 /* serialize all allocations in the group */
3968 mutex_lock(&ac->ac_lg->lg_mutex);
3969}
3970
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003971static noinline_for_stack int
3972ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003973 struct ext4_allocation_request *ar)
3974{
3975 struct super_block *sb = ar->inode->i_sb;
3976 struct ext4_sb_info *sbi = EXT4_SB(sb);
3977 struct ext4_super_block *es = sbi->s_es;
3978 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003979 unsigned int len;
3980 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003981 ext4_grpblk_t block;
3982
3983 /* we can't allocate > group size */
3984 len = ar->len;
3985
3986 /* just a dirty hack to filter too big requests */
3987 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
3988 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
3989
3990 /* start searching from the goal */
3991 goal = ar->goal;
3992 if (goal < le32_to_cpu(es->s_first_data_block) ||
3993 goal >= ext4_blocks_count(es))
3994 goal = le32_to_cpu(es->s_first_data_block);
3995 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3996
3997 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04003998 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004000 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004001 ac->ac_sb = sb;
4002 ac->ac_inode = ar->inode;
4003 ac->ac_o_ex.fe_logical = ar->logical;
4004 ac->ac_o_ex.fe_group = group;
4005 ac->ac_o_ex.fe_start = block;
4006 ac->ac_o_ex.fe_len = len;
4007 ac->ac_g_ex.fe_logical = ar->logical;
4008 ac->ac_g_ex.fe_group = group;
4009 ac->ac_g_ex.fe_start = block;
4010 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004011 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004012
4013 /* we have to define context: we'll we work with a file or
4014 * locality group. this is a policy, actually */
4015 ext4_mb_group_or_file(ac);
4016
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004017 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004018 "left: %u/%u, right %u/%u to %swritable\n",
4019 (unsigned) ar->len, (unsigned) ar->logical,
4020 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4021 (unsigned) ar->lleft, (unsigned) ar->pleft,
4022 (unsigned) ar->lright, (unsigned) ar->pright,
4023 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4024 return 0;
4025
4026}
4027
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004028static noinline_for_stack void
4029ext4_mb_discard_lg_preallocations(struct super_block *sb,
4030 struct ext4_locality_group *lg,
4031 int order, int total_entries)
4032{
4033 ext4_group_t group = 0;
4034 struct ext4_buddy e4b;
4035 struct list_head discard_list;
4036 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004037
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004038 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004039
4040 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004041
4042 spin_lock(&lg->lg_prealloc_lock);
4043 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4044 pa_inode_list) {
4045 spin_lock(&pa->pa_lock);
4046 if (atomic_read(&pa->pa_count)) {
4047 /*
4048 * This is the pa that we just used
4049 * for block allocation. So don't
4050 * free that
4051 */
4052 spin_unlock(&pa->pa_lock);
4053 continue;
4054 }
4055 if (pa->pa_deleted) {
4056 spin_unlock(&pa->pa_lock);
4057 continue;
4058 }
4059 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004060 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004061
4062 /* seems this one can be freed ... */
4063 pa->pa_deleted = 1;
4064 spin_unlock(&pa->pa_lock);
4065
4066 list_del_rcu(&pa->pa_inode_list);
4067 list_add(&pa->u.pa_tmp_list, &discard_list);
4068
4069 total_entries--;
4070 if (total_entries <= 5) {
4071 /*
4072 * we want to keep only 5 entries
4073 * allowing it to grow to 8. This
4074 * mak sure we don't call discard
4075 * soon for this list.
4076 */
4077 break;
4078 }
4079 }
4080 spin_unlock(&lg->lg_prealloc_lock);
4081
4082 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4083
4084 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4085 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004086 ext4_error(sb, "Error loading buddy information for %u",
4087 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004088 continue;
4089 }
4090 ext4_lock_group(sb, group);
4091 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004092 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004093 ext4_unlock_group(sb, group);
4094
Jing Zhange39e07f2010-05-14 00:00:00 -04004095 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004096 list_del(&pa->u.pa_tmp_list);
4097 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4098 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004099}
4100
4101/*
4102 * We have incremented pa_count. So it cannot be freed at this
4103 * point. Also we hold lg_mutex. So no parallel allocation is
4104 * possible from this lg. That means pa_free cannot be updated.
4105 *
4106 * A parallel ext4_mb_discard_group_preallocations is possible.
4107 * which can cause the lg_prealloc_list to be updated.
4108 */
4109
4110static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4111{
4112 int order, added = 0, lg_prealloc_count = 1;
4113 struct super_block *sb = ac->ac_sb;
4114 struct ext4_locality_group *lg = ac->ac_lg;
4115 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4116
4117 order = fls(pa->pa_free) - 1;
4118 if (order > PREALLOC_TB_SIZE - 1)
4119 /* The max size of hash table is PREALLOC_TB_SIZE */
4120 order = PREALLOC_TB_SIZE - 1;
4121 /* Add the prealloc space to lg */
4122 rcu_read_lock();
4123 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4124 pa_inode_list) {
4125 spin_lock(&tmp_pa->pa_lock);
4126 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004127 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004128 continue;
4129 }
4130 if (!added && pa->pa_free < tmp_pa->pa_free) {
4131 /* Add to the tail of the previous entry */
4132 list_add_tail_rcu(&pa->pa_inode_list,
4133 &tmp_pa->pa_inode_list);
4134 added = 1;
4135 /*
4136 * we want to count the total
4137 * number of entries in the list
4138 */
4139 }
4140 spin_unlock(&tmp_pa->pa_lock);
4141 lg_prealloc_count++;
4142 }
4143 if (!added)
4144 list_add_tail_rcu(&pa->pa_inode_list,
4145 &lg->lg_prealloc_list[order]);
4146 rcu_read_unlock();
4147
4148 /* Now trim the list to be not more than 8 elements */
4149 if (lg_prealloc_count > 8) {
4150 ext4_mb_discard_lg_preallocations(sb, lg,
4151 order, lg_prealloc_count);
4152 return;
4153 }
4154 return ;
4155}
4156
Alex Tomasc9de5602008-01-29 00:19:52 -05004157/*
4158 * release all resource we used in allocation
4159 */
4160static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4161{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004162 struct ext4_prealloc_space *pa = ac->ac_pa;
4163 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004164 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004165 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004166 spin_lock(&pa->pa_lock);
4167 pa->pa_pstart += ac->ac_b_ex.fe_len;
4168 pa->pa_lstart += ac->ac_b_ex.fe_len;
4169 pa->pa_free -= ac->ac_b_ex.fe_len;
4170 pa->pa_len -= ac->ac_b_ex.fe_len;
4171 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004172 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004173 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004174 if (ac->alloc_semp)
4175 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004176 if (pa) {
4177 /*
4178 * We want to add the pa to the right bucket.
4179 * Remove it from the list and while adding
4180 * make sure the list to which we are adding
4181 * doesn't grow big. We need to release
4182 * alloc_semp before calling ext4_mb_add_n_trim()
4183 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004184 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004185 spin_lock(pa->pa_obj_lock);
4186 list_del_rcu(&pa->pa_inode_list);
4187 spin_unlock(pa->pa_obj_lock);
4188 ext4_mb_add_n_trim(ac);
4189 }
4190 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4191 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004192 if (ac->ac_bitmap_page)
4193 page_cache_release(ac->ac_bitmap_page);
4194 if (ac->ac_buddy_page)
4195 page_cache_release(ac->ac_buddy_page);
4196 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4197 mutex_unlock(&ac->ac_lg->lg_mutex);
4198 ext4_mb_collect_stats(ac);
4199 return 0;
4200}
4201
4202static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4203{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004204 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004205 int ret;
4206 int freed = 0;
4207
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004208 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004209 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004210 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4211 freed += ret;
4212 needed -= ret;
4213 }
4214
4215 return freed;
4216}
4217
4218/*
4219 * Main entry point into mballoc to allocate blocks
4220 * it tries to use preallocation first, then falls back
4221 * to usual allocation
4222 */
4223ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004224 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004225{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004226 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004227 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004228 struct ext4_sb_info *sbi;
4229 struct super_block *sb;
4230 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004231 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004232 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004233
4234 sb = ar->inode->i_sb;
4235 sbi = EXT4_SB(sb);
4236
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004237 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004238
Mingming Cao60e58e02009-01-22 18:13:05 +01004239 /*
4240 * For delayed allocation, we could skip the ENOSPC and
4241 * EDQUOT check, as blocks and quotas have been already
4242 * reserved when data being copied into pagecache.
4243 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004244 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004245 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4246 else {
4247 /* Without delayed allocation we need to verify
4248 * there is enough free blocks to do block allocation
4249 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004250 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004251 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4252 /* let others to free the space */
4253 yield();
4254 ar->len = ar->len >> 1;
4255 }
4256 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004257 *errp = -ENOSPC;
4258 return 0;
4259 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004260 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004261 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004262 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4263 ar->len--;
4264 }
4265 inquota = ar->len;
4266 if (ar->len == 0) {
4267 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004268 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004269 }
Mingming Caod2a17632008-07-14 17:52:37 -04004270 }
Mingming Caod2a17632008-07-14 17:52:37 -04004271
Eric Sandeen256bdb42008-02-10 01:13:33 -05004272 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004273 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004274 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004275 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004276 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004277 }
4278
Eric Sandeen256bdb42008-02-10 01:13:33 -05004279 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004280 if (*errp) {
4281 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004282 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004283 }
4284
Eric Sandeen256bdb42008-02-10 01:13:33 -05004285 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4286 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004287 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4288 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004289repeat:
4290 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004291 *errp = ext4_mb_regular_allocator(ac);
4292 if (*errp)
4293 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004294
4295 /* as we've just preallocated more space than
4296 * user requested orinally, we store allocated
4297 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004298 if (ac->ac_status == AC_STATUS_FOUND &&
4299 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4300 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004301 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004302 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004303 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004304 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004305 /*
4306 * drop the reference that we took
4307 * in ext4_mb_use_best_found
4308 */
4309 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004310 ac->ac_b_ex.fe_group = 0;
4311 ac->ac_b_ex.fe_start = 0;
4312 ac->ac_b_ex.fe_len = 0;
4313 ac->ac_status = AC_STATUS_CONTINUE;
4314 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004315 } else if (*errp)
4316 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004317 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004318 else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004319 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4320 ar->len = ac->ac_b_ex.fe_len;
4321 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004322 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004323 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004324 if (freed)
4325 goto repeat;
4326 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004327 }
4328
4329 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004330 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004331 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004332 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004333 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004334 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004335out:
4336 if (ac)
4337 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004338 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004339 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004340 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004341 if (!ext4_test_inode_state(ar->inode,
4342 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004343 /* release all the reserved blocks if non delalloc */
4344 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4345 reserv_blks);
4346 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004347
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004348 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004349
Alex Tomasc9de5602008-01-29 00:19:52 -05004350 return block;
4351}
Alex Tomasc9de5602008-01-29 00:19:52 -05004352
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004353/*
4354 * We can merge two free data extents only if the physical blocks
4355 * are contiguous, AND the extents were freed by the same transaction,
4356 * AND the blocks are associated with the same group.
4357 */
4358static int can_merge(struct ext4_free_data *entry1,
4359 struct ext4_free_data *entry2)
4360{
4361 if ((entry1->t_tid == entry2->t_tid) &&
4362 (entry1->group == entry2->group) &&
4363 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4364 return 1;
4365 return 0;
4366}
4367
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004368static noinline_for_stack int
4369ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004370 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004371{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004372 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004373 ext4_grpblk_t block;
4374 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004375 struct ext4_group_info *db = e4b->bd_info;
4376 struct super_block *sb = e4b->bd_sb;
4377 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004378 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4379 struct rb_node *parent = NULL, *new_node;
4380
Frank Mayhar03901312009-01-07 00:06:22 -05004381 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004382 BUG_ON(e4b->bd_bitmap_page == NULL);
4383 BUG_ON(e4b->bd_buddy_page == NULL);
4384
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004385 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004386 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004387
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004388 if (!*n) {
4389 /* first free block exent. We need to
4390 protect buddy cache from being freed,
4391 * otherwise we'll refresh it from
4392 * on-disk bitmap and lose not-yet-available
4393 * blocks */
4394 page_cache_get(e4b->bd_buddy_page);
4395 page_cache_get(e4b->bd_bitmap_page);
4396 }
4397 while (*n) {
4398 parent = *n;
4399 entry = rb_entry(parent, struct ext4_free_data, node);
4400 if (block < entry->start_blk)
4401 n = &(*n)->rb_left;
4402 else if (block >= (entry->start_blk + entry->count))
4403 n = &(*n)->rb_right;
4404 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004405 ext4_grp_locked_error(sb, group, 0,
4406 ext4_group_first_block_no(sb, group) + block,
4407 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004408 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004409 }
4410 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004411
4412 rb_link_node(new_node, parent, n);
4413 rb_insert_color(new_node, &db->bb_free_root);
4414
4415 /* Now try to see the extent can be merged to left and right */
4416 node = rb_prev(new_node);
4417 if (node) {
4418 entry = rb_entry(node, struct ext4_free_data, node);
4419 if (can_merge(entry, new_entry)) {
4420 new_entry->start_blk = entry->start_blk;
4421 new_entry->count += entry->count;
4422 rb_erase(node, &(db->bb_free_root));
4423 spin_lock(&sbi->s_md_lock);
4424 list_del(&entry->list);
4425 spin_unlock(&sbi->s_md_lock);
4426 kmem_cache_free(ext4_free_ext_cachep, entry);
4427 }
4428 }
4429
4430 node = rb_next(new_node);
4431 if (node) {
4432 entry = rb_entry(node, struct ext4_free_data, node);
4433 if (can_merge(new_entry, entry)) {
4434 new_entry->count += entry->count;
4435 rb_erase(node, &(db->bb_free_root));
4436 spin_lock(&sbi->s_md_lock);
4437 list_del(&entry->list);
4438 spin_unlock(&sbi->s_md_lock);
4439 kmem_cache_free(ext4_free_ext_cachep, entry);
4440 }
4441 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004442 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004443 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004444 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004445 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004446 return 0;
4447}
4448
Theodore Ts'o44338712009-11-22 07:44:56 -05004449/**
4450 * ext4_free_blocks() -- Free given blocks and update quota
4451 * @handle: handle for this transaction
4452 * @inode: inode
4453 * @block: start physical block to free
4454 * @count: number of blocks to count
4455 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004456 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004457void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004458 struct buffer_head *bh, ext4_fsblk_t block,
4459 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004460{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004461 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004462 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004463 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004464 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004465 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004466 ext4_grpblk_t bit;
4467 struct buffer_head *gd_bh;
4468 ext4_group_t block_group;
4469 struct ext4_sb_info *sbi;
4470 struct ext4_buddy e4b;
4471 int err = 0;
4472 int ret;
4473
Theodore Ts'oe6362602009-11-23 07:17:05 -05004474 if (bh) {
4475 if (block)
4476 BUG_ON(block != bh->b_blocknr);
4477 else
4478 block = bh->b_blocknr;
4479 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004480
Alex Tomasc9de5602008-01-29 00:19:52 -05004481 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004482 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4483 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004484 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004485 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004486 goto error_return;
4487 }
4488
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004489 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004490 trace_ext4_free_blocks(inode, block, count, flags);
4491
4492 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4493 struct buffer_head *tbh = bh;
4494 int i;
4495
4496 BUG_ON(bh && (count > 1));
4497
4498 for (i = 0; i < count; i++) {
4499 if (!bh)
4500 tbh = sb_find_get_block(inode->i_sb,
4501 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004502 if (unlikely(!tbh))
4503 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004504 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004505 inode, tbh, block + i);
4506 }
4507 }
4508
Theodore Ts'o60e66792010-05-17 07:00:00 -04004509 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004510 * We need to make sure we don't reuse the freed block until
4511 * after the transaction is committed, which we can do by
4512 * treating the block as metadata, below. We make an
4513 * exception if the inode is to be written in writeback mode
4514 * since writeback mode has weak data consistency guarantees.
4515 */
4516 if (!ext4_should_writeback_data(inode))
4517 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004518
Alex Tomasc9de5602008-01-29 00:19:52 -05004519do_more:
4520 overflow = 0;
4521 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4522
4523 /*
4524 * Check to see if we are freeing blocks across a group
4525 * boundary.
4526 */
4527 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4528 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4529 count -= overflow;
4530 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004531 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004532 if (!bitmap_bh) {
4533 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004534 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004535 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004536 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004537 if (!gdp) {
4538 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004539 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004540 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004541
4542 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4543 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4544 in_range(block, ext4_inode_table(sb, gdp),
4545 EXT4_SB(sb)->s_itb_per_group) ||
4546 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4547 EXT4_SB(sb)->s_itb_per_group)) {
4548
Eric Sandeen12062dd2010-02-15 14:19:27 -05004549 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004550 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004551 /* err = 0. ext4_std_error should be a no op */
4552 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004553 }
4554
4555 BUFFER_TRACE(bitmap_bh, "getting write access");
4556 err = ext4_journal_get_write_access(handle, bitmap_bh);
4557 if (err)
4558 goto error_return;
4559
4560 /*
4561 * We are about to modify some metadata. Call the journal APIs
4562 * to unshare ->b_data if a currently-committing transaction is
4563 * using it
4564 */
4565 BUFFER_TRACE(gd_bh, "get_write_access");
4566 err = ext4_journal_get_write_access(handle, gd_bh);
4567 if (err)
4568 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004569#ifdef AGGRESSIVE_CHECK
4570 {
4571 int i;
4572 for (i = 0; i < count; i++)
4573 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4574 }
4575#endif
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004576 trace_ext4_mballoc_free(sb, inode, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004577
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004578 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4579 if (err)
4580 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004581
4582 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004583 struct ext4_free_data *new_entry;
4584 /*
4585 * blocks being freed are metadata. these blocks shouldn't
4586 * be used until this transaction is committed
4587 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004588 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4589 if (!new_entry) {
4590 err = -ENOMEM;
4591 goto error_return;
4592 }
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004593 new_entry->start_blk = bit;
4594 new_entry->group = block_group;
4595 new_entry->count = count;
4596 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004597
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004598 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004599 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004600 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004601 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004602 /* need to update group_info->bb_free and bitmap
4603 * with group lock held. generate_buddy look at
4604 * them with group lock_held
4605 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004606 ext4_lock_group(sb, block_group);
4607 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004608 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004609 }
4610
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004611 ret = ext4_free_blks_count(sb, gdp) + count;
4612 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004613 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004614 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004615 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4616
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004617 if (sbi->s_log_groups_per_flex) {
4618 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004619 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004620 }
4621
Jing Zhange39e07f2010-05-14 00:00:00 -04004622 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004623
Theodore Ts'o44338712009-11-22 07:44:56 -05004624 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004625
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004626 /* We dirtied the bitmap block */
4627 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4628 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4629
Alex Tomasc9de5602008-01-29 00:19:52 -05004630 /* And the group descriptor block */
4631 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004632 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004633 if (!err)
4634 err = ret;
4635
4636 if (overflow && !err) {
4637 block += count;
4638 count = overflow;
4639 put_bh(bitmap_bh);
4640 goto do_more;
4641 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004642 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004643error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004644 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004645 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004646 brelse(bitmap_bh);
4647 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004648 return;
4649}
Lukas Czerner7360d172010-10-27 21:30:12 -04004650
4651/**
Amir Goldstein2846e822011-05-09 10:46:41 -04004652 * ext4_add_groupblocks() -- Add given blocks to an existing group
4653 * @handle: handle to this transaction
4654 * @sb: super block
4655 * @block: start physcial block to add to the block group
4656 * @count: number of blocks to free
4657 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004658 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004659 */
4660void ext4_add_groupblocks(handle_t *handle, struct super_block *sb,
4661 ext4_fsblk_t block, unsigned long count)
4662{
4663 struct buffer_head *bitmap_bh = NULL;
4664 struct buffer_head *gd_bh;
4665 ext4_group_t block_group;
4666 ext4_grpblk_t bit;
4667 unsigned int i;
4668 struct ext4_group_desc *desc;
4669 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004670 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004671 int err = 0, ret, blk_free_count;
4672 ext4_grpblk_t blocks_freed;
4673 struct ext4_group_info *grp;
4674
4675 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4676
4677 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4678 grp = ext4_get_group_info(sb, block_group);
4679 /*
4680 * Check to see if we are freeing blocks across a group
4681 * boundary.
4682 */
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004683 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb))
Amir Goldstein2846e822011-05-09 10:46:41 -04004684 goto error_return;
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004685
Amir Goldstein2846e822011-05-09 10:46:41 -04004686 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4687 if (!bitmap_bh)
4688 goto error_return;
4689 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
4690 if (!desc)
4691 goto error_return;
4692
4693 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4694 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4695 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4696 in_range(block + count - 1, ext4_inode_table(sb, desc),
4697 sbi->s_itb_per_group)) {
4698 ext4_error(sb, "Adding blocks in system zones - "
4699 "Block = %llu, count = %lu",
4700 block, count);
4701 goto error_return;
4702 }
4703
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004704 BUFFER_TRACE(bitmap_bh, "getting write access");
4705 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004706 if (err)
4707 goto error_return;
4708
4709 /*
4710 * We are about to modify some metadata. Call the journal APIs
4711 * to unshare ->b_data if a currently-committing transaction is
4712 * using it
4713 */
4714 BUFFER_TRACE(gd_bh, "get_write_access");
4715 err = ext4_journal_get_write_access(handle, gd_bh);
4716 if (err)
4717 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004718
Amir Goldstein2846e822011-05-09 10:46:41 -04004719 for (i = 0, blocks_freed = 0; i < count; i++) {
4720 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004721 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004722 ext4_error(sb, "bit already cleared for block %llu",
4723 (ext4_fsblk_t)(block + i));
4724 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4725 } else {
4726 blocks_freed++;
4727 }
4728 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004729
4730 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4731 if (err)
4732 goto error_return;
4733
4734 /*
4735 * need to update group_info->bb_free and bitmap
4736 * with group lock held. generate_buddy look at
4737 * them with group lock_held
4738 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004739 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004740 mb_clear_bits(bitmap_bh->b_data, bit, count);
4741 mb_free_blocks(NULL, &e4b, bit, count);
Amir Goldstein2846e822011-05-09 10:46:41 -04004742 blk_free_count = blocks_freed + ext4_free_blks_count(sb, desc);
4743 ext4_free_blks_set(sb, desc, blk_free_count);
4744 desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
4745 ext4_unlock_group(sb, block_group);
4746 percpu_counter_add(&sbi->s_freeblocks_counter, blocks_freed);
4747
4748 if (sbi->s_log_groups_per_flex) {
4749 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
4750 atomic_add(blocks_freed,
4751 &sbi->s_flex_groups[flex_group].free_blocks);
4752 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004753
4754 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004755
4756 /* We dirtied the bitmap block */
4757 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4758 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4759
4760 /* And the group descriptor block */
4761 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4762 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4763 if (!err)
4764 err = ret;
4765
4766error_return:
4767 brelse(bitmap_bh);
4768 ext4_std_error(sb, err);
4769 return;
4770}
4771
4772/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004773 * ext4_trim_extent -- function to TRIM one single free extent in the group
4774 * @sb: super block for the file system
4775 * @start: starting block of the free extent in the alloc. group
4776 * @count: number of blocks to TRIM
4777 * @group: alloc. group we are working with
4778 * @e4b: ext4 buddy for the group
4779 *
4780 * Trim "count" blocks starting at "start" in the "group". To assure that no
4781 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4782 * be called with under the group lock.
4783 */
Theodore Ts'od9f34502011-04-30 13:47:24 -04004784static void ext4_trim_extent(struct super_block *sb, int start, int count,
4785 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004786{
4787 struct ext4_free_extent ex;
Lukas Czerner7360d172010-10-27 21:30:12 -04004788
4789 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4790
4791 ex.fe_start = start;
4792 ex.fe_group = group;
4793 ex.fe_len = count;
4794
4795 /*
4796 * Mark blocks used, so no one can reuse them while
4797 * being trimmed.
4798 */
4799 mb_mark_used(e4b, &ex);
4800 ext4_unlock_group(sb, group);
Theodore Ts'od9f34502011-04-30 13:47:24 -04004801 ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004802 ext4_lock_group(sb, group);
4803 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czerner7360d172010-10-27 21:30:12 -04004804}
4805
4806/**
4807 * ext4_trim_all_free -- function to trim all free space in alloc. group
4808 * @sb: super block for file system
4809 * @e4b: ext4 buddy
4810 * @start: first group block to examine
4811 * @max: last group block to examine
4812 * @minblocks: minimum extent block count
4813 *
4814 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4815 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4816 * the extent.
4817 *
4818 *
4819 * ext4_trim_all_free walks through group's block bitmap searching for free
4820 * extents. When the free extent is found, mark it as used in group buddy
4821 * bitmap. Then issue a TRIM command on this extent and free the extent in
4822 * the group buddy bitmap. This is done until whole group is scanned.
4823 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004824static ext4_grpblk_t
4825ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
Lukas Czerner7360d172010-10-27 21:30:12 -04004826 ext4_grpblk_t start, ext4_grpblk_t max, ext4_grpblk_t minblocks)
4827{
4828 void *bitmap;
4829 ext4_grpblk_t next, count = 0;
4830 ext4_group_t group;
Lukas Czerner7360d172010-10-27 21:30:12 -04004831
4832 BUG_ON(e4b == NULL);
4833
4834 bitmap = e4b->bd_bitmap;
4835 group = e4b->bd_group;
4836 start = (e4b->bd_info->bb_first_free > start) ?
4837 e4b->bd_info->bb_first_free : start;
4838 ext4_lock_group(sb, group);
4839
4840 while (start < max) {
4841 start = mb_find_next_zero_bit(bitmap, max, start);
4842 if (start >= max)
4843 break;
4844 next = mb_find_next_bit(bitmap, max, start);
4845
4846 if ((next - start) >= minblocks) {
Theodore Ts'od9f34502011-04-30 13:47:24 -04004847 ext4_trim_extent(sb, start,
4848 next - start, group, e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004849 count += next - start;
4850 }
4851 start = next + 1;
4852
4853 if (fatal_signal_pending(current)) {
4854 count = -ERESTARTSYS;
4855 break;
4856 }
4857
4858 if (need_resched()) {
4859 ext4_unlock_group(sb, group);
4860 cond_resched();
4861 ext4_lock_group(sb, group);
4862 }
4863
4864 if ((e4b->bd_info->bb_free - count) < minblocks)
4865 break;
4866 }
4867 ext4_unlock_group(sb, group);
4868
4869 ext4_debug("trimmed %d blocks in the group %d\n",
4870 count, group);
4871
Lukas Czerner7360d172010-10-27 21:30:12 -04004872 return count;
4873}
4874
4875/**
4876 * ext4_trim_fs() -- trim ioctl handle function
4877 * @sb: superblock for filesystem
4878 * @range: fstrim_range structure
4879 *
4880 * start: First Byte to trim
4881 * len: number of Bytes to trim from start
4882 * minlen: minimum extent length in Bytes
4883 * ext4_trim_fs goes through all allocation groups containing Bytes from
4884 * start to start+len. For each such a group ext4_trim_all_free function
4885 * is invoked to trim all free space.
4886 */
4887int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4888{
4889 struct ext4_buddy e4b;
4890 ext4_group_t first_group, last_group;
4891 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
4892 ext4_grpblk_t cnt = 0, first_block, last_block;
4893 uint64_t start, len, minlen, trimmed;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004894 ext4_fsblk_t first_data_blk =
4895 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004896 int ret = 0;
4897
4898 start = range->start >> sb->s_blocksize_bits;
4899 len = range->len >> sb->s_blocksize_bits;
4900 minlen = range->minlen >> sb->s_blocksize_bits;
4901 trimmed = 0;
4902
4903 if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
4904 return -EINVAL;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004905 if (start < first_data_blk) {
4906 len -= first_data_blk - start;
4907 start = first_data_blk;
4908 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004909
4910 /* Determine first and last group to examine based on start and len */
4911 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
4912 &first_group, &first_block);
4913 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
4914 &last_group, &last_block);
4915 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
4916 last_block = EXT4_BLOCKS_PER_GROUP(sb);
4917
4918 if (first_group > last_group)
4919 return -EINVAL;
4920
4921 for (group = first_group; group <= last_group; group++) {
4922 ret = ext4_mb_load_buddy(sb, group, &e4b);
4923 if (ret) {
4924 ext4_error(sb, "Error in loading buddy "
4925 "information for %u", group);
4926 break;
4927 }
4928
Tao Ma0ba08512011-03-23 15:48:11 -04004929 /*
4930 * For all the groups except the last one, last block will
4931 * always be EXT4_BLOCKS_PER_GROUP(sb), so we only need to
4932 * change it for the last group in which case start +
4933 * len < EXT4_BLOCKS_PER_GROUP(sb).
4934 */
4935 if (first_block + len < EXT4_BLOCKS_PER_GROUP(sb))
Jan Karaca6e9092011-01-10 12:30:39 -05004936 last_block = first_block + len;
Tao Ma0ba08512011-03-23 15:48:11 -04004937 len -= last_block - first_block;
Lukas Czerner7360d172010-10-27 21:30:12 -04004938
4939 if (e4b.bd_info->bb_free >= minlen) {
4940 cnt = ext4_trim_all_free(sb, &e4b, first_block,
4941 last_block, minlen);
4942 if (cnt < 0) {
4943 ret = cnt;
4944 ext4_mb_unload_buddy(&e4b);
4945 break;
4946 }
4947 }
4948 ext4_mb_unload_buddy(&e4b);
4949 trimmed += cnt;
4950 first_block = 0;
4951 }
4952 range->len = trimmed * sb->s_blocksize;
4953
4954 return ret;
4955}