blob: 66bee7274d6a8e8b5a1d2bcb4edfeff7d7df3014 [file] [log] [blame]
Alex Tomasc9de5602008-01-29 00:19:52 -05001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public Licens
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
17 */
18
19
20/*
21 * mballoc.c contains the multiblocks allocation routines
22 */
23
Mingming Cao8f6e39a2008-04-29 22:01:31 -040024#include "mballoc.h"
Theodore Ts'o6ba495e2009-09-18 13:38:55 -040025#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040027#include <trace/events/ext4.h>
28
Alex Tomasc9de5602008-01-29 00:19:52 -050029/*
30 * MUSTDO:
31 * - test ext4_ext_search_left() and ext4_ext_search_right()
32 * - search for metadata in few groups
33 *
34 * TODO v4:
35 * - normalization should take into account whether file is still open
36 * - discard preallocations if no free space left (policy?)
37 * - don't normalize tails
38 * - quota
39 * - reservation for superuser
40 *
41 * TODO v3:
42 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
43 * - track min/max extents in each group for better group selection
44 * - mb_mark_used() may allocate chunk right after splitting buddy
45 * - tree of groups sorted by number of free blocks
46 * - error handling
47 */
48
49/*
50 * The allocation request involve request for multiple number of blocks
51 * near to the goal(block) value specified.
52 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040053 * During initialization phase of the allocator we decide to use the
54 * group preallocation or inode preallocation depending on the size of
55 * the file. The size of the file could be the resulting file size we
56 * would have after allocation, or the current file size, which ever
57 * is larger. If the size is less than sbi->s_mb_stream_request we
58 * select to use the group preallocation. The default value of
59 * s_mb_stream_request is 16 blocks. This can also be tuned via
60 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
61 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050062 *
63 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040064 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050065 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040066 * First stage the allocator looks at the inode prealloc list,
67 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
68 * spaces for this particular inode. The inode prealloc space is
69 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050070 *
71 * pa_lstart -> the logical start block for this prealloc space
72 * pa_pstart -> the physical start block for this prealloc space
Daniel Mack1537a362010-01-29 15:57:49 +080073 * pa_len -> length for this prealloc space
Alex Tomasc9de5602008-01-29 00:19:52 -050074 * pa_free -> free space available in this prealloc space
75 *
76 * The inode preallocation space is used looking at the _logical_ start
77 * block. If only the logical file block falls within the range of prealloc
78 * space we will consume the particular prealloc space. This make sure that
79 * that the we have contiguous physical blocks representing the file blocks
80 *
81 * The important thing to be noted in case of inode prealloc space is that
82 * we don't modify the values associated to inode prealloc space except
83 * pa_free.
84 *
85 * If we are not able to find blocks in the inode prealloc space and if we
86 * have the group allocation flag set then we look at the locality group
87 * prealloc space. These are per CPU prealloc list repreasented as
88 *
89 * ext4_sb_info.s_locality_groups[smp_processor_id()]
90 *
91 * The reason for having a per cpu locality group is to reduce the contention
92 * between CPUs. It is possible to get scheduled at this point.
93 *
94 * The locality group prealloc space is used looking at whether we have
95 * enough free space (pa_free) withing the prealloc space.
96 *
97 * If we can't allocate blocks via inode prealloc or/and locality group
98 * prealloc then we look at the buddy cache. The buddy cache is represented
99 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
100 * mapped to the buddy and bitmap information regarding different
101 * groups. The buddy information is attached to buddy cache inode so that
102 * we can access them through the page cache. The information regarding
103 * each group is loaded via ext4_mb_load_buddy. The information involve
104 * block bitmap and buddy information. The information are stored in the
105 * inode as:
106 *
107 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500108 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500109 *
110 *
111 * one block each for bitmap and buddy information. So for each group we
112 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
113 * blocksize) blocks. So it can have information regarding groups_per_page
114 * which is blocks_per_page/2
115 *
116 * The buddy cache inode is not stored on disk. The inode is thrown
117 * away when the filesystem is unmounted.
118 *
119 * We look for count number of blocks in the buddy cache. If we were able
120 * to locate that many free blocks we return with additional information
121 * regarding rest of the contiguous physical block available
122 *
123 * Before allocating blocks via buddy cache we normalize the request
124 * blocks. This ensure we ask for more blocks that we needed. The extra
125 * blocks that we get after allocation is added to the respective prealloc
126 * list. In case of inode preallocation we follow a list of heuristics
127 * based on file size. This can be found in ext4_mb_normalize_request. If
128 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400129 * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
Alex Tomasc9de5602008-01-29 00:19:52 -0500130 * 512 blocks. This can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400131 * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500132 * terms of number of blocks. If we have mounted the file system with -O
133 * stripe=<value> option the group prealloc request is normalized to the
134 * stripe value (sbi->s_stripe)
135 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400136 * The regular allocator(using the buddy cache) supports few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500137 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400138 * /sys/fs/ext4/<partition>/mb_min_to_scan
139 * /sys/fs/ext4/<partition>/mb_max_to_scan
140 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400142 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
144 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400145 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200146 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400147 * stripe size. This should result in better allocation on RAID setups. If
148 * not, we search in the specific group using bitmap for best extents. The
149 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500150 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400151 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 * best extent in the found extents. Searching for the blocks starts with
153 * the group specified as the goal value in allocation context via
154 * ac_g_ex. Each group is first checked based on the criteria whether it
155 * can used for allocation. ext4_mb_good_group explains how the groups are
156 * checked.
157 *
158 * Both the prealloc space are getting populated as above. So for the first
159 * request we will hit the buddy cache which will result in this prealloc
160 * space getting filled. The prealloc space is then later used for the
161 * subsequent request.
162 */
163
164/*
165 * mballoc operates on the following data:
166 * - on-disk bitmap
167 * - in-core buddy (actually includes buddy and bitmap)
168 * - preallocation descriptors (PAs)
169 *
170 * there are two types of preallocations:
171 * - inode
172 * assiged to specific inode and can be used for this inode only.
173 * it describes part of inode's space preallocated to specific
174 * physical blocks. any block from that preallocated can be used
175 * independent. the descriptor just tracks number of blocks left
176 * unused. so, before taking some block from descriptor, one must
177 * make sure corresponded logical block isn't allocated yet. this
178 * also means that freeing any block within descriptor's range
179 * must discard all preallocated blocks.
180 * - locality group
181 * assigned to specific locality group which does not translate to
182 * permanent set of inodes: inode can join and leave group. space
183 * from this type of preallocation can be used for any inode. thus
184 * it's consumed from the beginning to the end.
185 *
186 * relation between them can be expressed as:
187 * in-core buddy = on-disk bitmap + preallocation descriptors
188 *
189 * this mean blocks mballoc considers used are:
190 * - allocated blocks (persistent)
191 * - preallocated blocks (non-persistent)
192 *
193 * consistency in mballoc world means that at any time a block is either
194 * free or used in ALL structures. notice: "any time" should not be read
195 * literally -- time is discrete and delimited by locks.
196 *
197 * to keep it simple, we don't use block numbers, instead we count number of
198 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
199 *
200 * all operations can be expressed as:
201 * - init buddy: buddy = on-disk + PAs
202 * - new PA: buddy += N; PA = N
203 * - use inode PA: on-disk += N; PA -= N
204 * - discard inode PA buddy -= on-disk - PA; PA = 0
205 * - use locality group PA on-disk += N; PA -= N
206 * - discard locality group PA buddy -= PA; PA = 0
207 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
208 * is used in real operation because we can't know actual used
209 * bits from PA, only from on-disk bitmap
210 *
211 * if we follow this strict logic, then all operations above should be atomic.
212 * given some of them can block, we'd have to use something like semaphores
213 * killing performance on high-end SMP hardware. let's try to relax it using
214 * the following knowledge:
215 * 1) if buddy is referenced, it's already initialized
216 * 2) while block is used in buddy and the buddy is referenced,
217 * nobody can re-allocate that block
218 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
219 * bit set and PA claims same block, it's OK. IOW, one can set bit in
220 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
221 * block
222 *
223 * so, now we're building a concurrency table:
224 * - init buddy vs.
225 * - new PA
226 * blocks for PA are allocated in the buddy, buddy must be referenced
227 * until PA is linked to allocation group to avoid concurrent buddy init
228 * - use inode PA
229 * we need to make sure that either on-disk bitmap or PA has uptodate data
230 * given (3) we care that PA-=N operation doesn't interfere with init
231 * - discard inode PA
232 * the simplest way would be to have buddy initialized by the discard
233 * - use locality group PA
234 * again PA-=N must be serialized with init
235 * - discard locality group PA
236 * the simplest way would be to have buddy initialized by the discard
237 * - new PA vs.
238 * - use inode PA
239 * i_data_sem serializes them
240 * - discard inode PA
241 * discard process must wait until PA isn't used by another process
242 * - use locality group PA
243 * some mutex should serialize them
244 * - discard locality group PA
245 * discard process must wait until PA isn't used by another process
246 * - use inode PA
247 * - use inode PA
248 * i_data_sem or another mutex should serializes them
249 * - discard inode PA
250 * discard process must wait until PA isn't used by another process
251 * - use locality group PA
252 * nothing wrong here -- they're different PAs covering different blocks
253 * - discard locality group PA
254 * discard process must wait until PA isn't used by another process
255 *
256 * now we're ready to make few consequences:
257 * - PA is referenced and while it is no discard is possible
258 * - PA is referenced until block isn't marked in on-disk bitmap
259 * - PA changes only after on-disk bitmap
260 * - discard must not compete with init. either init is done before
261 * any discard or they're serialized somehow
262 * - buddy init as sum of on-disk bitmap and PAs is done atomically
263 *
264 * a special case when we've used PA to emptiness. no need to modify buddy
265 * in this case, but we should care about concurrent init
266 *
267 */
268
269 /*
270 * Logic in few words:
271 *
272 * - allocation:
273 * load group
274 * find blocks
275 * mark bits in on-disk bitmap
276 * release group
277 *
278 * - use preallocation:
279 * find proper PA (per-inode or group)
280 * load group
281 * mark bits in on-disk bitmap
282 * release group
283 * release PA
284 *
285 * - free:
286 * load group
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - discard preallocations in group:
291 * mark PAs deleted
292 * move them onto local list
293 * load on-disk bitmap
294 * load group
295 * remove PA from object (inode or locality group)
296 * mark free blocks in-core
297 *
298 * - discard inode's preallocations:
299 */
300
301/*
302 * Locking rules
303 *
304 * Locks:
305 * - bitlock on a group (group)
306 * - object (inode/locality) (object)
307 * - per-pa lock (pa)
308 *
309 * Paths:
310 * - new pa
311 * object
312 * group
313 *
314 * - find and use pa:
315 * pa
316 *
317 * - release consumed pa:
318 * pa
319 * group
320 * object
321 *
322 * - generate in-core bitmap:
323 * group
324 * pa
325 *
326 * - discard all for given object (inode, locality group):
327 * object
328 * pa
329 * group
330 *
331 * - discard all for given group:
332 * group
333 * pa
334 * group
335 * object
336 *
337 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500338static struct kmem_cache *ext4_pspace_cachep;
339static struct kmem_cache *ext4_ac_cachep;
340static struct kmem_cache *ext4_free_ext_cachep;
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/*
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400960 * lock the group_info alloc_sem of all the groups
961 * belonging to the same buddy cache page. This
962 * make sure other parallel operation on the buddy
963 * cache doesn't happen whild holding the buddy cache
964 * lock
965 */
966static int ext4_mb_get_buddy_cache_lock(struct super_block *sb,
967 ext4_group_t group)
968{
969 int i;
970 int block, pnum;
971 int blocks_per_page;
972 int groups_per_page;
973 ext4_group_t ngroups = ext4_get_groups_count(sb);
974 ext4_group_t first_group;
975 struct ext4_group_info *grp;
976
977 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
978 /*
979 * the buddy cache inode stores the block bitmap
980 * and buddy information in consecutive blocks.
981 * So for each group we need two blocks.
982 */
983 block = group * 2;
984 pnum = block / blocks_per_page;
985 first_group = pnum * blocks_per_page / 2;
986
987 groups_per_page = blocks_per_page >> 1;
988 if (groups_per_page == 0)
989 groups_per_page = 1;
990 /* read all groups the page covers into the cache */
991 for (i = 0; i < groups_per_page; i++) {
992
993 if ((first_group + i) >= ngroups)
994 break;
995 grp = ext4_get_group_info(sb, first_group + i);
996 /* take all groups write allocation
997 * semaphore. This make sure there is
998 * no block allocation going on in any
999 * of that groups
1000 */
1001 down_write_nested(&grp->alloc_sem, i);
1002 }
1003 return i;
1004}
1005
1006static void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1007 ext4_group_t group, int locked_group)
1008{
1009 int i;
1010 int block, pnum;
1011 int blocks_per_page;
1012 ext4_group_t first_group;
1013 struct ext4_group_info *grp;
1014
1015 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1016 /*
1017 * the buddy cache inode stores the block bitmap
1018 * and buddy information in consecutive blocks.
1019 * So for each group we need two blocks.
1020 */
1021 block = group * 2;
1022 pnum = block / blocks_per_page;
1023 first_group = pnum * blocks_per_page / 2;
1024 /* release locks on all the groups */
1025 for (i = 0; i < locked_group; i++) {
1026
1027 grp = ext4_get_group_info(sb, first_group + i);
1028 /* take all groups write allocation
1029 * semaphore. This make sure there is
1030 * no block allocation going on in any
1031 * of that groups
1032 */
1033 up_write(&grp->alloc_sem);
1034 }
1035
1036}
1037
1038/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001039 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1040 * block group lock of all groups for this page; do not hold the BG lock when
1041 * calling this routine!
1042 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001043static noinline_for_stack
1044int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1045{
1046
1047 int ret = 0;
1048 void *bitmap;
1049 int blocks_per_page;
1050 int block, pnum, poff;
1051 int num_grp_locked = 0;
1052 struct ext4_group_info *this_grp;
1053 struct ext4_sb_info *sbi = EXT4_SB(sb);
1054 struct inode *inode = sbi->s_buddy_cache;
1055 struct page *page = NULL, *bitmap_page = NULL;
1056
1057 mb_debug(1, "init group %u\n", group);
1058 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1059 this_grp = ext4_get_group_info(sb, group);
1060 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001061 * This ensures that we don't reinit the buddy cache
1062 * page which map to the group from which we are already
1063 * allocating. If we are looking at the buddy cache we would
1064 * have taken a reference using ext4_mb_load_buddy and that
1065 * would have taken the alloc_sem lock.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001066 */
1067 num_grp_locked = ext4_mb_get_buddy_cache_lock(sb, group);
1068 if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1069 /*
1070 * somebody initialized the group
1071 * return without doing anything
1072 */
1073 ret = 0;
1074 goto err;
1075 }
1076 /*
1077 * the buddy cache inode stores the block bitmap
1078 * and buddy information in consecutive blocks.
1079 * So for each group we need two blocks.
1080 */
1081 block = group * 2;
1082 pnum = block / blocks_per_page;
1083 poff = block % blocks_per_page;
1084 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1085 if (page) {
1086 BUG_ON(page->mapping != inode->i_mapping);
1087 ret = ext4_mb_init_cache(page, NULL);
1088 if (ret) {
1089 unlock_page(page);
1090 goto err;
1091 }
1092 unlock_page(page);
1093 }
1094 if (page == NULL || !PageUptodate(page)) {
1095 ret = -EIO;
1096 goto err;
1097 }
1098 mark_page_accessed(page);
1099 bitmap_page = page;
1100 bitmap = page_address(page) + (poff * sb->s_blocksize);
1101
1102 /* init buddy cache */
1103 block++;
1104 pnum = block / blocks_per_page;
1105 poff = block % blocks_per_page;
1106 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1107 if (page == bitmap_page) {
1108 /*
1109 * If both the bitmap and buddy are in
1110 * the same page we don't need to force
1111 * init the buddy
1112 */
1113 unlock_page(page);
1114 } else if (page) {
1115 BUG_ON(page->mapping != inode->i_mapping);
1116 ret = ext4_mb_init_cache(page, bitmap);
1117 if (ret) {
1118 unlock_page(page);
1119 goto err;
1120 }
1121 unlock_page(page);
1122 }
1123 if (page == NULL || !PageUptodate(page)) {
1124 ret = -EIO;
1125 goto err;
1126 }
1127 mark_page_accessed(page);
1128err:
1129 ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1130 if (bitmap_page)
1131 page_cache_release(bitmap_page);
1132 if (page)
1133 page_cache_release(page);
1134 return ret;
1135}
1136
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001137/*
1138 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1139 * block group lock of all groups for this page; do not hold the BG lock when
1140 * calling this routine!
1141 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001142static noinline_for_stack int
1143ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1144 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001145{
Alex Tomasc9de5602008-01-29 00:19:52 -05001146 int blocks_per_page;
1147 int block;
1148 int pnum;
1149 int poff;
1150 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001151 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001152 struct ext4_group_info *grp;
1153 struct ext4_sb_info *sbi = EXT4_SB(sb);
1154 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001155
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001156 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001157
1158 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001159 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001160
1161 e4b->bd_blkbits = sb->s_blocksize_bits;
1162 e4b->bd_info = ext4_get_group_info(sb, group);
1163 e4b->bd_sb = sb;
1164 e4b->bd_group = group;
1165 e4b->bd_buddy_page = NULL;
1166 e4b->bd_bitmap_page = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001167 e4b->alloc_semp = &grp->alloc_sem;
1168
1169 /* Take the read lock on the group alloc
1170 * sem. This would make sure a parallel
1171 * ext4_mb_init_group happening on other
1172 * groups mapped by the page is blocked
1173 * till we are done with allocation
1174 */
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001175repeat_load_buddy:
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001176 down_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001177
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001178 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1179 /* we need to check for group need init flag
1180 * with alloc_semp held so that we can be sure
1181 * that new blocks didn't get added to the group
1182 * when we are loading the buddy cache
1183 */
1184 up_read(e4b->alloc_semp);
1185 /*
1186 * we need full data about the group
1187 * to make a good selection
1188 */
1189 ret = ext4_mb_init_group(sb, group);
1190 if (ret)
1191 return ret;
1192 goto repeat_load_buddy;
1193 }
1194
Alex Tomasc9de5602008-01-29 00:19:52 -05001195 /*
1196 * the buddy cache inode stores the block bitmap
1197 * and buddy information in consecutive blocks.
1198 * So for each group we need two blocks.
1199 */
1200 block = group * 2;
1201 pnum = block / blocks_per_page;
1202 poff = block % blocks_per_page;
1203
1204 /* we could use find_or_create_page(), but it locks page
1205 * what we'd like to avoid in fast path ... */
1206 page = find_get_page(inode->i_mapping, pnum);
1207 if (page == NULL || !PageUptodate(page)) {
1208 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001209 /*
1210 * drop the page reference and try
1211 * to get the page with lock. If we
1212 * are not uptodate that implies
1213 * somebody just created the page but
1214 * is yet to initialize the same. So
1215 * wait for it to initialize.
1216 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001217 page_cache_release(page);
1218 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1219 if (page) {
1220 BUG_ON(page->mapping != inode->i_mapping);
1221 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001222 ret = ext4_mb_init_cache(page, NULL);
1223 if (ret) {
1224 unlock_page(page);
1225 goto err;
1226 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001227 mb_cmp_bitmaps(e4b, page_address(page) +
1228 (poff * sb->s_blocksize));
1229 }
1230 unlock_page(page);
1231 }
1232 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001233 if (page == NULL || !PageUptodate(page)) {
1234 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001235 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001236 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001237 e4b->bd_bitmap_page = page;
1238 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1239 mark_page_accessed(page);
1240
1241 block++;
1242 pnum = block / blocks_per_page;
1243 poff = block % blocks_per_page;
1244
1245 page = find_get_page(inode->i_mapping, pnum);
1246 if (page == NULL || !PageUptodate(page)) {
1247 if (page)
1248 page_cache_release(page);
1249 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1250 if (page) {
1251 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001252 if (!PageUptodate(page)) {
1253 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1254 if (ret) {
1255 unlock_page(page);
1256 goto err;
1257 }
1258 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001259 unlock_page(page);
1260 }
1261 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001262 if (page == NULL || !PageUptodate(page)) {
1263 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001264 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001265 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001266 e4b->bd_buddy_page = page;
1267 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1268 mark_page_accessed(page);
1269
1270 BUG_ON(e4b->bd_bitmap_page == NULL);
1271 BUG_ON(e4b->bd_buddy_page == NULL);
1272
1273 return 0;
1274
1275err:
1276 if (e4b->bd_bitmap_page)
1277 page_cache_release(e4b->bd_bitmap_page);
1278 if (e4b->bd_buddy_page)
1279 page_cache_release(e4b->bd_buddy_page);
1280 e4b->bd_buddy = NULL;
1281 e4b->bd_bitmap = NULL;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001282
1283 /* Done with the buddy cache */
1284 up_read(e4b->alloc_semp);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001285 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001286}
1287
Jing Zhange39e07f2010-05-14 00:00:00 -04001288static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001289{
1290 if (e4b->bd_bitmap_page)
1291 page_cache_release(e4b->bd_bitmap_page);
1292 if (e4b->bd_buddy_page)
1293 page_cache_release(e4b->bd_buddy_page);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001294 /* Done with the buddy cache */
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001295 if (e4b->alloc_semp)
1296 up_read(e4b->alloc_semp);
Alex Tomasc9de5602008-01-29 00:19:52 -05001297}
1298
1299
1300static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1301{
1302 int order = 1;
1303 void *bb;
1304
1305 BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1306 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1307
1308 bb = EXT4_MB_BUDDY(e4b);
1309 while (order <= e4b->bd_blkbits + 1) {
1310 block = block >> 1;
1311 if (!mb_test_bit(block, bb)) {
1312 /* this block is part of buddy of order 'order' */
1313 return order;
1314 }
1315 bb += 1 << (e4b->bd_blkbits - order);
1316 order++;
1317 }
1318 return 0;
1319}
1320
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001321static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001322{
1323 __u32 *addr;
1324
1325 len = cur + len;
1326 while (cur < len) {
1327 if ((cur & 31) == 0 && (len - cur) >= 32) {
1328 /* fast path: clear whole word at once */
1329 addr = bm + (cur >> 3);
1330 *addr = 0;
1331 cur += 32;
1332 continue;
1333 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001334 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001335 cur++;
1336 }
1337}
1338
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001339static void mb_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001340{
1341 __u32 *addr;
1342
1343 len = cur + len;
1344 while (cur < len) {
1345 if ((cur & 31) == 0 && (len - cur) >= 32) {
1346 /* fast path: set whole word at once */
1347 addr = bm + (cur >> 3);
1348 *addr = 0xffffffff;
1349 cur += 32;
1350 continue;
1351 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001352 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001353 cur++;
1354 }
1355}
1356
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001357static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001358 int first, int count)
1359{
1360 int block = 0;
1361 int max = 0;
1362 int order;
1363 void *buddy;
1364 void *buddy2;
1365 struct super_block *sb = e4b->bd_sb;
1366
1367 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001368 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001369 mb_check_buddy(e4b);
1370 mb_free_blocks_double(inode, e4b, first, count);
1371
1372 e4b->bd_info->bb_free += count;
1373 if (first < e4b->bd_info->bb_first_free)
1374 e4b->bd_info->bb_first_free = first;
1375
1376 /* let's maintain fragments counter */
1377 if (first != 0)
1378 block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1379 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1380 max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1381 if (block && max)
1382 e4b->bd_info->bb_fragments--;
1383 else if (!block && !max)
1384 e4b->bd_info->bb_fragments++;
1385
1386 /* let's maintain buddy itself */
1387 while (count-- > 0) {
1388 block = first++;
1389 order = 0;
1390
1391 if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1392 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001393
1394 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001395 blocknr += block;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001396 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001397 inode ? inode->i_ino : 0,
1398 blocknr,
1399 "freeing already freed block "
1400 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001401 }
1402 mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1403 e4b->bd_info->bb_counters[order]++;
1404
1405 /* start of the buddy */
1406 buddy = mb_find_buddy(e4b, order, &max);
1407
1408 do {
1409 block &= ~1UL;
1410 if (mb_test_bit(block, buddy) ||
1411 mb_test_bit(block + 1, buddy))
1412 break;
1413
1414 /* both the buddies are free, try to coalesce them */
1415 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1416
1417 if (!buddy2)
1418 break;
1419
1420 if (order > 0) {
1421 /* for special purposes, we don't set
1422 * free bits in bitmap */
1423 mb_set_bit(block, buddy);
1424 mb_set_bit(block + 1, buddy);
1425 }
1426 e4b->bd_info->bb_counters[order]--;
1427 e4b->bd_info->bb_counters[order]--;
1428
1429 block = block >> 1;
1430 order++;
1431 e4b->bd_info->bb_counters[order]++;
1432
1433 mb_clear_bit(block, buddy2);
1434 buddy = buddy2;
1435 } while (1);
1436 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001437 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001438 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001439}
1440
1441static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1442 int needed, struct ext4_free_extent *ex)
1443{
1444 int next = block;
1445 int max;
1446 int ord;
1447 void *buddy;
1448
Vincent Minetbc8e6742009-05-15 08:33:18 -04001449 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001450 BUG_ON(ex == NULL);
1451
1452 buddy = mb_find_buddy(e4b, order, &max);
1453 BUG_ON(buddy == NULL);
1454 BUG_ON(block >= max);
1455 if (mb_test_bit(block, buddy)) {
1456 ex->fe_len = 0;
1457 ex->fe_start = 0;
1458 ex->fe_group = 0;
1459 return 0;
1460 }
1461
1462 /* FIXME dorp order completely ? */
1463 if (likely(order == 0)) {
1464 /* find actual order */
1465 order = mb_find_order_for_block(e4b, block);
1466 block = block >> order;
1467 }
1468
1469 ex->fe_len = 1 << order;
1470 ex->fe_start = block << order;
1471 ex->fe_group = e4b->bd_group;
1472
1473 /* calc difference from given start */
1474 next = next - ex->fe_start;
1475 ex->fe_len -= next;
1476 ex->fe_start += next;
1477
1478 while (needed > ex->fe_len &&
1479 (buddy = mb_find_buddy(e4b, order, &max))) {
1480
1481 if (block + 1 >= max)
1482 break;
1483
1484 next = (block + 1) * (1 << order);
1485 if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1486 break;
1487
1488 ord = mb_find_order_for_block(e4b, next);
1489
1490 order = ord;
1491 block = next >> order;
1492 ex->fe_len += 1 << order;
1493 }
1494
1495 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1496 return ex->fe_len;
1497}
1498
1499static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1500{
1501 int ord;
1502 int mlen = 0;
1503 int max = 0;
1504 int cur;
1505 int start = ex->fe_start;
1506 int len = ex->fe_len;
1507 unsigned ret = 0;
1508 int len0 = len;
1509 void *buddy;
1510
1511 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1512 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001513 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001514 mb_check_buddy(e4b);
1515 mb_mark_used_double(e4b, start, len);
1516
1517 e4b->bd_info->bb_free -= len;
1518 if (e4b->bd_info->bb_first_free == start)
1519 e4b->bd_info->bb_first_free += len;
1520
1521 /* let's maintain fragments counter */
1522 if (start != 0)
1523 mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1524 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1525 max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1526 if (mlen && max)
1527 e4b->bd_info->bb_fragments++;
1528 else if (!mlen && !max)
1529 e4b->bd_info->bb_fragments--;
1530
1531 /* let's maintain buddy itself */
1532 while (len) {
1533 ord = mb_find_order_for_block(e4b, start);
1534
1535 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1536 /* the whole chunk may be allocated at once! */
1537 mlen = 1 << ord;
1538 buddy = mb_find_buddy(e4b, ord, &max);
1539 BUG_ON((start >> ord) >= max);
1540 mb_set_bit(start >> ord, buddy);
1541 e4b->bd_info->bb_counters[ord]--;
1542 start += mlen;
1543 len -= mlen;
1544 BUG_ON(len < 0);
1545 continue;
1546 }
1547
1548 /* store for history */
1549 if (ret == 0)
1550 ret = len | (ord << 16);
1551
1552 /* we have to split large buddy */
1553 BUG_ON(ord <= 0);
1554 buddy = mb_find_buddy(e4b, ord, &max);
1555 mb_set_bit(start >> ord, buddy);
1556 e4b->bd_info->bb_counters[ord]--;
1557
1558 ord--;
1559 cur = (start >> ord) & ~1U;
1560 buddy = mb_find_buddy(e4b, ord, &max);
1561 mb_clear_bit(cur, buddy);
1562 mb_clear_bit(cur + 1, buddy);
1563 e4b->bd_info->bb_counters[ord]++;
1564 e4b->bd_info->bb_counters[ord]++;
1565 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001566 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001567
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001568 mb_set_bits(EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001569 mb_check_buddy(e4b);
1570
1571 return ret;
1572}
1573
1574/*
1575 * Must be called under group lock!
1576 */
1577static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1578 struct ext4_buddy *e4b)
1579{
1580 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1581 int ret;
1582
1583 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1584 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1585
1586 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1587 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1588 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1589
1590 /* preallocation can change ac_b_ex, thus we store actually
1591 * allocated blocks for history */
1592 ac->ac_f_ex = ac->ac_b_ex;
1593
1594 ac->ac_status = AC_STATUS_FOUND;
1595 ac->ac_tail = ret & 0xffff;
1596 ac->ac_buddy = ret >> 16;
1597
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001598 /*
1599 * take the page reference. We want the page to be pinned
1600 * so that we don't get a ext4_mb_init_cache_call for this
1601 * group until we update the bitmap. That would mean we
1602 * double allocate blocks. The reference is dropped
1603 * in ext4_mb_release_context
1604 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001605 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1606 get_page(ac->ac_bitmap_page);
1607 ac->ac_buddy_page = e4b->bd_buddy_page;
1608 get_page(ac->ac_buddy_page);
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05001609 /* on allocation we use ac to track the held semaphore */
1610 ac->alloc_semp = e4b->alloc_semp;
1611 e4b->alloc_semp = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05001612 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001613 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001614 spin_lock(&sbi->s_md_lock);
1615 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1616 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1617 spin_unlock(&sbi->s_md_lock);
1618 }
1619}
1620
1621/*
1622 * regular allocator, for general purposes allocation
1623 */
1624
1625static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1626 struct ext4_buddy *e4b,
1627 int finish_group)
1628{
1629 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1630 struct ext4_free_extent *bex = &ac->ac_b_ex;
1631 struct ext4_free_extent *gex = &ac->ac_g_ex;
1632 struct ext4_free_extent ex;
1633 int max;
1634
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001635 if (ac->ac_status == AC_STATUS_FOUND)
1636 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001637 /*
1638 * We don't want to scan for a whole year
1639 */
1640 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1641 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1642 ac->ac_status = AC_STATUS_BREAK;
1643 return;
1644 }
1645
1646 /*
1647 * Haven't found good chunk so far, let's continue
1648 */
1649 if (bex->fe_len < gex->fe_len)
1650 return;
1651
1652 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1653 && bex->fe_group == e4b->bd_group) {
1654 /* recheck chunk's availability - we don't know
1655 * when it was found (within this lock-unlock
1656 * period or not) */
1657 max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1658 if (max >= gex->fe_len) {
1659 ext4_mb_use_best_found(ac, e4b);
1660 return;
1661 }
1662 }
1663}
1664
1665/*
1666 * The routine checks whether found extent is good enough. If it is,
1667 * then the extent gets marked used and flag is set to the context
1668 * to stop scanning. Otherwise, the extent is compared with the
1669 * previous found extent and if new one is better, then it's stored
1670 * in the context. Later, the best found extent will be used, if
1671 * mballoc can't find good enough extent.
1672 *
1673 * FIXME: real allocation policy is to be designed yet!
1674 */
1675static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1676 struct ext4_free_extent *ex,
1677 struct ext4_buddy *e4b)
1678{
1679 struct ext4_free_extent *bex = &ac->ac_b_ex;
1680 struct ext4_free_extent *gex = &ac->ac_g_ex;
1681
1682 BUG_ON(ex->fe_len <= 0);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04001683 BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001684 BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1685 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1686
1687 ac->ac_found++;
1688
1689 /*
1690 * The special case - take what you catch first
1691 */
1692 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1693 *bex = *ex;
1694 ext4_mb_use_best_found(ac, e4b);
1695 return;
1696 }
1697
1698 /*
1699 * Let's check whether the chuck is good enough
1700 */
1701 if (ex->fe_len == gex->fe_len) {
1702 *bex = *ex;
1703 ext4_mb_use_best_found(ac, e4b);
1704 return;
1705 }
1706
1707 /*
1708 * If this is first found extent, just store it in the context
1709 */
1710 if (bex->fe_len == 0) {
1711 *bex = *ex;
1712 return;
1713 }
1714
1715 /*
1716 * If new found extent is better, store it in the context
1717 */
1718 if (bex->fe_len < gex->fe_len) {
1719 /* if the request isn't satisfied, any found extent
1720 * larger than previous best one is better */
1721 if (ex->fe_len > bex->fe_len)
1722 *bex = *ex;
1723 } else if (ex->fe_len > gex->fe_len) {
1724 /* if the request is satisfied, then we try to find
1725 * an extent that still satisfy the request, but is
1726 * smaller than previous one */
1727 if (ex->fe_len < bex->fe_len)
1728 *bex = *ex;
1729 }
1730
1731 ext4_mb_check_limits(ac, e4b, 0);
1732}
1733
Eric Sandeen089ceec2009-07-05 22:17:31 -04001734static noinline_for_stack
1735int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001736 struct ext4_buddy *e4b)
1737{
1738 struct ext4_free_extent ex = ac->ac_b_ex;
1739 ext4_group_t group = ex.fe_group;
1740 int max;
1741 int err;
1742
1743 BUG_ON(ex.fe_len <= 0);
1744 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1745 if (err)
1746 return err;
1747
1748 ext4_lock_group(ac->ac_sb, group);
1749 max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1750
1751 if (max > 0) {
1752 ac->ac_b_ex = ex;
1753 ext4_mb_use_best_found(ac, e4b);
1754 }
1755
1756 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001757 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001758
1759 return 0;
1760}
1761
Eric Sandeen089ceec2009-07-05 22:17:31 -04001762static noinline_for_stack
1763int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001764 struct ext4_buddy *e4b)
1765{
1766 ext4_group_t group = ac->ac_g_ex.fe_group;
1767 int max;
1768 int err;
1769 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05001770 struct ext4_free_extent ex;
1771
1772 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1773 return 0;
1774
1775 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1776 if (err)
1777 return err;
1778
1779 ext4_lock_group(ac->ac_sb, group);
1780 max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1781 ac->ac_g_ex.fe_len, &ex);
1782
1783 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1784 ext4_fsblk_t start;
1785
Akinobu Mita5661bd62010-03-03 23:53:39 -05001786 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1787 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001788 /* use do_div to get remainder (would be 64-bit modulo) */
1789 if (do_div(start, sbi->s_stripe) == 0) {
1790 ac->ac_found++;
1791 ac->ac_b_ex = ex;
1792 ext4_mb_use_best_found(ac, e4b);
1793 }
1794 } else if (max >= ac->ac_g_ex.fe_len) {
1795 BUG_ON(ex.fe_len <= 0);
1796 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1797 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1798 ac->ac_found++;
1799 ac->ac_b_ex = ex;
1800 ext4_mb_use_best_found(ac, e4b);
1801 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1802 /* Sometimes, caller may want to merge even small
1803 * number of blocks to an existing extent */
1804 BUG_ON(ex.fe_len <= 0);
1805 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1806 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1807 ac->ac_found++;
1808 ac->ac_b_ex = ex;
1809 ext4_mb_use_best_found(ac, e4b);
1810 }
1811 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001812 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001813
1814 return 0;
1815}
1816
1817/*
1818 * The routine scans buddy structures (not bitmap!) from given order
1819 * to max order and tries to find big enough chunk to satisfy the req
1820 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001821static noinline_for_stack
1822void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001823 struct ext4_buddy *e4b)
1824{
1825 struct super_block *sb = ac->ac_sb;
1826 struct ext4_group_info *grp = e4b->bd_info;
1827 void *buddy;
1828 int i;
1829 int k;
1830 int max;
1831
1832 BUG_ON(ac->ac_2order <= 0);
1833 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1834 if (grp->bb_counters[i] == 0)
1835 continue;
1836
1837 buddy = mb_find_buddy(e4b, i, &max);
1838 BUG_ON(buddy == NULL);
1839
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001840 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001841 BUG_ON(k >= max);
1842
1843 ac->ac_found++;
1844
1845 ac->ac_b_ex.fe_len = 1 << i;
1846 ac->ac_b_ex.fe_start = k << i;
1847 ac->ac_b_ex.fe_group = e4b->bd_group;
1848
1849 ext4_mb_use_best_found(ac, e4b);
1850
1851 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1852
1853 if (EXT4_SB(sb)->s_mb_stats)
1854 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1855
1856 break;
1857 }
1858}
1859
1860/*
1861 * The routine scans the group and measures all found extents.
1862 * In order to optimize scanning, caller must pass number of
1863 * free blocks in the group, so the routine can know upper limit.
1864 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001865static noinline_for_stack
1866void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001867 struct ext4_buddy *e4b)
1868{
1869 struct super_block *sb = ac->ac_sb;
1870 void *bitmap = EXT4_MB_BITMAP(e4b);
1871 struct ext4_free_extent ex;
1872 int i;
1873 int free;
1874
1875 free = e4b->bd_info->bb_free;
1876 BUG_ON(free <= 0);
1877
1878 i = e4b->bd_info->bb_first_free;
1879
1880 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001881 i = mb_find_next_zero_bit(bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05001882 EXT4_BLOCKS_PER_GROUP(sb), i);
1883 if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001884 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001885 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001886 * free blocks even though group info says we
1887 * we have free blocks
1888 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001889 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1890 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001891 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001892 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001893 break;
1894 }
1895
1896 mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1897 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001898 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001899 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
1900 "%d free blocks as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001901 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001902 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001903 /*
1904 * The number of free blocks differs. This mostly
1905 * indicate that the bitmap is corrupt. So exit
1906 * without claiming the space.
1907 */
1908 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001909 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001910
1911 ext4_mb_measure_extent(ac, &ex, e4b);
1912
1913 i += ex.fe_len;
1914 free -= ex.fe_len;
1915 }
1916
1917 ext4_mb_check_limits(ac, e4b, 1);
1918}
1919
1920/*
1921 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001922 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001923 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001924static noinline_for_stack
1925void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001926 struct ext4_buddy *e4b)
1927{
1928 struct super_block *sb = ac->ac_sb;
1929 struct ext4_sb_info *sbi = EXT4_SB(sb);
1930 void *bitmap = EXT4_MB_BITMAP(e4b);
1931 struct ext4_free_extent ex;
1932 ext4_fsblk_t first_group_block;
1933 ext4_fsblk_t a;
1934 ext4_grpblk_t i;
1935 int max;
1936
1937 BUG_ON(sbi->s_stripe == 0);
1938
1939 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001940 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1941
Alex Tomasc9de5602008-01-29 00:19:52 -05001942 a = first_group_block + sbi->s_stripe - 1;
1943 do_div(a, sbi->s_stripe);
1944 i = (a * sbi->s_stripe) - first_group_block;
1945
1946 while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1947 if (!mb_test_bit(i, bitmap)) {
1948 max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1949 if (max >= sbi->s_stripe) {
1950 ac->ac_found++;
1951 ac->ac_b_ex = ex;
1952 ext4_mb_use_best_found(ac, e4b);
1953 break;
1954 }
1955 }
1956 i += sbi->s_stripe;
1957 }
1958}
1959
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001960/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001961static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1962 ext4_group_t group, int cr)
1963{
1964 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001965 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001966 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1967
1968 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001969
1970 /* We only do this if the grp has never been initialized */
1971 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1972 int ret = ext4_mb_init_group(ac->ac_sb, group);
1973 if (ret)
1974 return 0;
1975 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001976
1977 free = grp->bb_free;
1978 fragments = grp->bb_fragments;
1979 if (free == 0)
1980 return 0;
1981 if (fragments == 0)
1982 return 0;
1983
1984 switch (cr) {
1985 case 0:
1986 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001987
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001988 if (grp->bb_largest_free_order < ac->ac_2order)
1989 return 0;
1990
Theodore Ts'oa4912122009-03-12 12:18:34 -04001991 /* Avoid using the first bg of a flexgroup for data files */
1992 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1993 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1994 ((group % flex_size) == 0))
1995 return 0;
1996
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001997 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001998 case 1:
1999 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2000 return 1;
2001 break;
2002 case 2:
2003 if (free >= ac->ac_g_ex.fe_len)
2004 return 1;
2005 break;
2006 case 3:
2007 return 1;
2008 default:
2009 BUG();
2010 }
2011
2012 return 0;
2013}
2014
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002015static noinline_for_stack int
2016ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002017{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002018 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002019 int cr;
2020 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002021 struct ext4_sb_info *sbi;
2022 struct super_block *sb;
2023 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002024
2025 sb = ac->ac_sb;
2026 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002027 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002028 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002029 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002030 ngroups = sbi->s_blockfile_groups;
2031
Alex Tomasc9de5602008-01-29 00:19:52 -05002032 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2033
2034 /* first, try the goal */
2035 err = ext4_mb_find_by_goal(ac, &e4b);
2036 if (err || ac->ac_status == AC_STATUS_FOUND)
2037 goto out;
2038
2039 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2040 goto out;
2041
2042 /*
2043 * ac->ac2_order is set only if the fe_len is a power of 2
2044 * if ac2_order is set we also set criteria to 0 so that we
2045 * try exact allocation using buddy.
2046 */
2047 i = fls(ac->ac_g_ex.fe_len);
2048 ac->ac_2order = 0;
2049 /*
2050 * We search using buddy data only if the order of the request
2051 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002052 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002053 */
2054 if (i >= sbi->s_mb_order2_reqs) {
2055 /*
2056 * This should tell if fe_len is exactly power of 2
2057 */
2058 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2059 ac->ac_2order = i - 1;
2060 }
2061
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002062 /* if stream allocation is enabled, use global goal */
2063 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002064 /* TBD: may be hot point */
2065 spin_lock(&sbi->s_md_lock);
2066 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2067 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2068 spin_unlock(&sbi->s_md_lock);
2069 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002070
Alex Tomasc9de5602008-01-29 00:19:52 -05002071 /* Let's just scan groups to find more-less suitable blocks */
2072 cr = ac->ac_2order ? 0 : 1;
2073 /*
2074 * cr == 0 try to get exact allocation,
2075 * cr == 3 try to get anything
2076 */
2077repeat:
2078 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2079 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002080 /*
2081 * searching for the right group start
2082 * from the goal value specified
2083 */
2084 group = ac->ac_g_ex.fe_group;
2085
Theodore Ts'o8df96752009-05-01 08:50:38 -04002086 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002087 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002088 group = 0;
2089
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002090 /* This now checks without needing the buddy page */
2091 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002092 continue;
2093
Alex Tomasc9de5602008-01-29 00:19:52 -05002094 err = ext4_mb_load_buddy(sb, group, &e4b);
2095 if (err)
2096 goto out;
2097
2098 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002099
2100 /*
2101 * We need to check again after locking the
2102 * block group
2103 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002104 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002105 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002106 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002107 continue;
2108 }
2109
2110 ac->ac_groups_scanned++;
Theodore Ts'o75507ef2009-05-01 12:58:36 -04002111 if (cr == 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002112 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002113 else if (cr == 1 && sbi->s_stripe &&
2114 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002115 ext4_mb_scan_aligned(ac, &e4b);
2116 else
2117 ext4_mb_complex_scan_group(ac, &e4b);
2118
2119 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002120 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002121
2122 if (ac->ac_status != AC_STATUS_CONTINUE)
2123 break;
2124 }
2125 }
2126
2127 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2128 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2129 /*
2130 * We've been searching too long. Let's try to allocate
2131 * the best chunk we've found so far
2132 */
2133
2134 ext4_mb_try_best_found(ac, &e4b);
2135 if (ac->ac_status != AC_STATUS_FOUND) {
2136 /*
2137 * Someone more lucky has already allocated it.
2138 * The only thing we can do is just take first
2139 * found block(s)
2140 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2141 */
2142 ac->ac_b_ex.fe_group = 0;
2143 ac->ac_b_ex.fe_start = 0;
2144 ac->ac_b_ex.fe_len = 0;
2145 ac->ac_status = AC_STATUS_CONTINUE;
2146 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2147 cr = 3;
2148 atomic_inc(&sbi->s_mb_lost_chunks);
2149 goto repeat;
2150 }
2151 }
2152out:
2153 return err;
2154}
2155
Alex Tomasc9de5602008-01-29 00:19:52 -05002156static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2157{
2158 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002159 ext4_group_t group;
2160
Theodore Ts'o8df96752009-05-01 08:50:38 -04002161 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002162 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002164 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002165}
2166
2167static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2168{
2169 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002170 ext4_group_t group;
2171
2172 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002173 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002174 return NULL;
2175 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002176 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002177}
2178
2179static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2180{
2181 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002182 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002183 int i;
2184 int err;
2185 struct ext4_buddy e4b;
2186 struct sg {
2187 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002188 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002189 } sg;
2190
2191 group--;
2192 if (group == 0)
2193 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2194 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2195 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2196 "group", "free", "frags", "first",
2197 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2198 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2199
2200 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2201 sizeof(struct ext4_group_info);
2202 err = ext4_mb_load_buddy(sb, group, &e4b);
2203 if (err) {
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002204 seq_printf(seq, "#%-5u: I/O error\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002205 return 0;
2206 }
2207 ext4_lock_group(sb, group);
2208 memcpy(&sg, ext4_get_group_info(sb, group), i);
2209 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002210 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002211
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002212 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002213 sg.info.bb_fragments, sg.info.bb_first_free);
2214 for (i = 0; i <= 13; i++)
2215 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2216 sg.info.bb_counters[i] : 0);
2217 seq_printf(seq, " ]\n");
2218
2219 return 0;
2220}
2221
2222static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2223{
2224}
2225
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002226static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002227 .start = ext4_mb_seq_groups_start,
2228 .next = ext4_mb_seq_groups_next,
2229 .stop = ext4_mb_seq_groups_stop,
2230 .show = ext4_mb_seq_groups_show,
2231};
2232
2233static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2234{
2235 struct super_block *sb = PDE(inode)->data;
2236 int rc;
2237
2238 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2239 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002240 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002241 m->private = sb;
2242 }
2243 return rc;
2244
2245}
2246
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002247static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002248 .owner = THIS_MODULE,
2249 .open = ext4_mb_seq_groups_open,
2250 .read = seq_read,
2251 .llseek = seq_lseek,
2252 .release = seq_release,
2253};
2254
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002255static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2256{
2257 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2258 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2259
2260 BUG_ON(!cachep);
2261 return cachep;
2262}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002263
2264/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002265int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002266 struct ext4_group_desc *desc)
2267{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002268 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002269 int metalen = 0;
2270 struct ext4_sb_info *sbi = EXT4_SB(sb);
2271 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002272 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002273
2274 /*
2275 * First check if this group is the first of a reserved block.
2276 * If it's true, we have to allocate a new table of pointers
2277 * to ext4_group_info structures
2278 */
2279 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2280 metalen = sizeof(*meta_group_info) <<
2281 EXT4_DESC_PER_BLOCK_BITS(sb);
2282 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2283 if (meta_group_info == NULL) {
2284 printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2285 "buddy group\n");
2286 goto exit_meta_group_info;
2287 }
2288 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2289 meta_group_info;
2290 }
2291
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002292 meta_group_info =
2293 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2294 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2295
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002296 meta_group_info[i] = kmem_cache_alloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002297 if (meta_group_info[i] == NULL) {
2298 printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
2299 goto exit_group_info;
2300 }
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002301 memset(meta_group_info[i], 0, kmem_cache_size(cachep));
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002302 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2303 &(meta_group_info[i]->bb_state));
2304
2305 /*
2306 * initialize bb_free to be able to skip
2307 * empty groups without initialization
2308 */
2309 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2310 meta_group_info[i]->bb_free =
2311 ext4_free_blocks_after_init(sb, group, desc);
2312 } else {
2313 meta_group_info[i]->bb_free =
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002314 ext4_free_blks_count(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002315 }
2316
2317 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002318 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002319 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002320 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002321
2322#ifdef DOUBLE_CHECK
2323 {
2324 struct buffer_head *bh;
2325 meta_group_info[i]->bb_bitmap =
2326 kmalloc(sb->s_blocksize, GFP_KERNEL);
2327 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2328 bh = ext4_read_block_bitmap(sb, group);
2329 BUG_ON(bh == NULL);
2330 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2331 sb->s_blocksize);
2332 put_bh(bh);
2333 }
2334#endif
2335
2336 return 0;
2337
2338exit_group_info:
2339 /* If a meta_group_info table has been allocated, release it now */
2340 if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
2341 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
2342exit_meta_group_info:
2343 return -ENOMEM;
2344} /* ext4_mb_add_groupinfo */
2345
Alex Tomasc9de5602008-01-29 00:19:52 -05002346static int ext4_mb_init_backend(struct super_block *sb)
2347{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002348 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002349 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002350 struct ext4_sb_info *sbi = EXT4_SB(sb);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002351 struct ext4_super_block *es = sbi->s_es;
2352 int num_meta_group_infos;
2353 int num_meta_group_infos_max;
2354 int array_size;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002355 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002356 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002357
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002358 /* This is the number of blocks used by GDT */
Theodore Ts'o8df96752009-05-01 08:50:38 -04002359 num_meta_group_infos = (ngroups + EXT4_DESC_PER_BLOCK(sb) -
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002360 1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
2361
2362 /*
2363 * This is the total number of blocks used by GDT including
2364 * the number of reserved blocks for GDT.
2365 * The s_group_info array is allocated with this value
2366 * to allow a clean online resize without a complex
2367 * manipulation of pointer.
2368 * The drawback is the unused memory when no resize
2369 * occurs but it's very low in terms of pages
2370 * (see comments below)
2371 * Need to handle this properly when META_BG resizing is allowed
2372 */
2373 num_meta_group_infos_max = num_meta_group_infos +
2374 le16_to_cpu(es->s_reserved_gdt_blocks);
2375
2376 /*
2377 * array_size is the size of s_group_info array. We round it
2378 * to the next power of two because this approximation is done
2379 * internally by kmalloc so we can have some more memory
2380 * for free here (e.g. may be used for META_BG resize).
2381 */
2382 array_size = 1;
2383 while (array_size < sizeof(*sbi->s_group_info) *
2384 num_meta_group_infos_max)
2385 array_size = array_size << 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002386 /* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2387 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2388 * So a two level scheme suffices for now. */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002389 sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
Alex Tomasc9de5602008-01-29 00:19:52 -05002390 if (sbi->s_group_info == NULL) {
2391 printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2392 return -ENOMEM;
2393 }
2394 sbi->s_buddy_cache = new_inode(sb);
2395 if (sbi->s_buddy_cache == NULL) {
2396 printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2397 goto err_freesgi;
2398 }
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002399 sbi->s_buddy_cache->i_ino = get_next_ino();
Alex Tomasc9de5602008-01-29 00:19:52 -05002400 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002401 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002402 desc = ext4_get_group_desc(sb, i, NULL);
2403 if (desc == NULL) {
2404 printk(KERN_ERR
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002405 "EXT4-fs: can't read descriptor %u\n", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002406 goto err_freebuddy;
2407 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002408 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2409 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002410 }
2411
2412 return 0;
2413
2414err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002415 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002416 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002417 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Alex Tomasc9de5602008-01-29 00:19:52 -05002418 i = num_meta_group_infos;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002419 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002420 kfree(sbi->s_group_info[i]);
2421 iput(sbi->s_buddy_cache);
2422err_freesgi:
2423 kfree(sbi->s_group_info);
2424 return -ENOMEM;
2425}
2426
Eric Sandeen2892c152011-02-12 08:12:18 -05002427static void ext4_groupinfo_destroy_slabs(void)
2428{
2429 int i;
2430
2431 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2432 if (ext4_groupinfo_caches[i])
2433 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2434 ext4_groupinfo_caches[i] = NULL;
2435 }
2436}
2437
2438static int ext4_groupinfo_create_slab(size_t size)
2439{
2440 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2441 int slab_size;
2442 int blocksize_bits = order_base_2(size);
2443 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2444 struct kmem_cache *cachep;
2445
2446 if (cache_index >= NR_GRPINFO_CACHES)
2447 return -EINVAL;
2448
2449 if (unlikely(cache_index < 0))
2450 cache_index = 0;
2451
2452 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2453 if (ext4_groupinfo_caches[cache_index]) {
2454 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2455 return 0; /* Already created */
2456 }
2457
2458 slab_size = offsetof(struct ext4_group_info,
2459 bb_counters[blocksize_bits + 2]);
2460
2461 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2462 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2463 NULL);
2464
2465 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2466 if (!cachep) {
2467 printk(KERN_EMERG "EXT4: no memory for groupinfo slab cache\n");
2468 return -ENOMEM;
2469 }
2470
2471 ext4_groupinfo_caches[cache_index] = cachep;
2472
2473 return 0;
2474}
2475
Alex Tomasc9de5602008-01-29 00:19:52 -05002476int ext4_mb_init(struct super_block *sb, int needs_recovery)
2477{
2478 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002479 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002480 unsigned offset;
2481 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002482 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002483
Eric Sandeen19278052009-08-25 22:36:25 -04002484 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002485
2486 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2487 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002488 ret = -ENOMEM;
2489 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002490 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002491
Eric Sandeen19278052009-08-25 22:36:25 -04002492 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002493 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2494 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002495 ret = -ENOMEM;
2496 goto out;
2497 }
2498
Eric Sandeen2892c152011-02-12 08:12:18 -05002499 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2500 if (ret < 0)
2501 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002502
2503 /* order 0 is regular bitmap */
2504 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2505 sbi->s_mb_offsets[0] = 0;
2506
2507 i = 1;
2508 offset = 0;
2509 max = sb->s_blocksize << 2;
2510 do {
2511 sbi->s_mb_offsets[i] = offset;
2512 sbi->s_mb_maxs[i] = max;
2513 offset += 1 << (sb->s_blocksize_bits - i);
2514 max = max >> 1;
2515 i++;
2516 } while (i <= sb->s_blocksize_bits + 1);
2517
2518 /* init file for buddy data */
Shen Feng74767c52008-07-11 19:27:31 -04002519 ret = ext4_mb_init_backend(sb);
2520 if (ret != 0) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002521 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002522 }
2523
2524 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002525 spin_lock_init(&sbi->s_bal_lock);
2526
2527 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2528 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2529 sbi->s_mb_stats = MB_DEFAULT_STATS;
2530 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2531 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Alex Tomasc9de5602008-01-29 00:19:52 -05002532 sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2533
Eric Sandeen730c2132008-09-13 15:23:29 -04002534 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002535 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002536 ret = -ENOMEM;
2537 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002538 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002539 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002540 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002541 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002542 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002543 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2544 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002545 spin_lock_init(&lg->lg_prealloc_lock);
2546 }
2547
Theodore Ts'o296c3552009-09-30 00:32:42 -04002548 if (sbi->s_proc)
2549 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2550 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002551
Frank Mayhar03901312009-01-07 00:06:22 -05002552 if (sbi->s_journal)
2553 sbi->s_journal->j_commit_callback = release_blocks_on_commit;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002554out:
2555 if (ret) {
2556 kfree(sbi->s_mb_offsets);
2557 kfree(sbi->s_mb_maxs);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002558 }
2559 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002560}
2561
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002562/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002563static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2564{
2565 struct ext4_prealloc_space *pa;
2566 struct list_head *cur, *tmp;
2567 int count = 0;
2568
2569 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2570 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2571 list_del(&pa->pa_group_list);
2572 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002573 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002574 }
2575 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002576 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002577
2578}
2579
2580int ext4_mb_release(struct super_block *sb)
2581{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002582 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002583 ext4_group_t i;
2584 int num_meta_group_infos;
2585 struct ext4_group_info *grinfo;
2586 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002587 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002588
Alex Tomasc9de5602008-01-29 00:19:52 -05002589 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002590 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002591 grinfo = ext4_get_group_info(sb, i);
2592#ifdef DOUBLE_CHECK
2593 kfree(grinfo->bb_bitmap);
2594#endif
2595 ext4_lock_group(sb, i);
2596 ext4_mb_cleanup_pa(grinfo);
2597 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002598 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002599 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002600 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002601 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2602 EXT4_DESC_PER_BLOCK_BITS(sb);
2603 for (i = 0; i < num_meta_group_infos; i++)
2604 kfree(sbi->s_group_info[i]);
2605 kfree(sbi->s_group_info);
2606 }
2607 kfree(sbi->s_mb_offsets);
2608 kfree(sbi->s_mb_maxs);
2609 if (sbi->s_buddy_cache)
2610 iput(sbi->s_buddy_cache);
2611 if (sbi->s_mb_stats) {
2612 printk(KERN_INFO
2613 "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2614 atomic_read(&sbi->s_bal_allocated),
2615 atomic_read(&sbi->s_bal_reqs),
2616 atomic_read(&sbi->s_bal_success));
2617 printk(KERN_INFO
2618 "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2619 "%u 2^N hits, %u breaks, %u lost\n",
2620 atomic_read(&sbi->s_bal_ex_scanned),
2621 atomic_read(&sbi->s_bal_goals),
2622 atomic_read(&sbi->s_bal_2orders),
2623 atomic_read(&sbi->s_bal_breaks),
2624 atomic_read(&sbi->s_mb_lost_chunks));
2625 printk(KERN_INFO
2626 "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2627 sbi->s_mb_buddies_generated++,
2628 sbi->s_mb_generation_time);
2629 printk(KERN_INFO
2630 "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2631 atomic_read(&sbi->s_mb_preallocated),
2632 atomic_read(&sbi->s_mb_discarded));
2633 }
2634
Eric Sandeen730c2132008-09-13 15:23:29 -04002635 free_percpu(sbi->s_locality_groups);
Theodore Ts'o296c3552009-09-30 00:32:42 -04002636 if (sbi->s_proc)
2637 remove_proc_entry("mb_groups", sbi->s_proc);
Alex Tomasc9de5602008-01-29 00:19:52 -05002638
2639 return 0;
2640}
2641
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002642static inline int ext4_issue_discard(struct super_block *sb,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002643 ext4_group_t block_group, ext4_grpblk_t block, int count)
2644{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002645 ext4_fsblk_t discard_block;
2646
2647 discard_block = block + ext4_group_first_block_no(sb, block_group);
2648 trace_ext4_discard_blocks(sb,
2649 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002650 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002651}
2652
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002653/*
2654 * This function is called by the jbd2 layer once the commit has finished,
2655 * so we know we can free the blocks that were released with that commit.
2656 */
2657static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
Alex Tomasc9de5602008-01-29 00:19:52 -05002658{
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002659 struct super_block *sb = journal->j_private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002660 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002661 struct ext4_group_info *db;
Lukas Czerner93259632011-01-10 12:09:59 -05002662 int err, ret, count = 0, count2 = 0;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002663 struct ext4_free_data *entry;
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002664 struct list_head *l, *ltmp;
Alex Tomasc9de5602008-01-29 00:19:52 -05002665
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002666 list_for_each_safe(l, ltmp, &txn->t_private_list) {
2667 entry = list_entry(l, struct ext4_free_data, list);
Alex Tomasc9de5602008-01-29 00:19:52 -05002668
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002669 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002670 entry->count, entry->group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002671
Lukas Czerner93259632011-01-10 12:09:59 -05002672 if (test_opt(sb, DISCARD)) {
2673 ret = ext4_issue_discard(sb, entry->group,
Jiaying Zhang5c521832010-07-27 11:56:05 -04002674 entry->start_blk, entry->count);
Lukas Czerner93259632011-01-10 12:09:59 -05002675 if (unlikely(ret == -EOPNOTSUPP)) {
2676 ext4_warning(sb, "discard not supported, "
2677 "disabling");
2678 clear_opt(sb, DISCARD);
2679 }
2680 }
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002681
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002682 err = ext4_mb_load_buddy(sb, entry->group, &e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002683 /* we expect to find existing buddy because it's pinned */
2684 BUG_ON(err != 0);
2685
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002686 db = e4b.bd_info;
Alex Tomasc9de5602008-01-29 00:19:52 -05002687 /* there are blocks to put in buddy to make them really free */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002688 count += entry->count;
Alex Tomasc9de5602008-01-29 00:19:52 -05002689 count2++;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002690 ext4_lock_group(sb, entry->group);
2691 /* Take it out of per group rb tree */
2692 rb_erase(&entry->node, &(db->bb_free_root));
2693 mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2694
2695 if (!db->bb_free_root.rb_node) {
2696 /* No more items in the per group rb tree
2697 * balance refcounts from ext4_mb_free_metadata()
2698 */
2699 page_cache_release(e4b.bd_buddy_page);
2700 page_cache_release(e4b.bd_bitmap_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05002701 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002702 ext4_unlock_group(sb, entry->group);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002703 kmem_cache_free(ext4_free_ext_cachep, entry);
Jing Zhange39e07f2010-05-14 00:00:00 -04002704 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002705 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002706
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002707 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002708}
2709
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002710#ifdef CONFIG_EXT4_DEBUG
2711u8 mb_enable_debug __read_mostly;
2712
2713static struct dentry *debugfs_dir;
2714static struct dentry *debugfs_debug;
2715
2716static void __init ext4_create_debugfs_entry(void)
2717{
2718 debugfs_dir = debugfs_create_dir("ext4", NULL);
2719 if (debugfs_dir)
2720 debugfs_debug = debugfs_create_u8("mballoc-debug",
2721 S_IRUGO | S_IWUSR,
2722 debugfs_dir,
2723 &mb_enable_debug);
2724}
2725
2726static void ext4_remove_debugfs_entry(void)
2727{
2728 debugfs_remove(debugfs_debug);
2729 debugfs_remove(debugfs_dir);
2730}
2731
2732#else
2733
2734static void __init ext4_create_debugfs_entry(void)
2735{
2736}
2737
2738static void ext4_remove_debugfs_entry(void)
2739{
2740}
2741
2742#endif
2743
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002744int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002745{
Theodore Ts'o16828082010-10-27 21:30:09 -04002746 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2747 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002748 if (ext4_pspace_cachep == NULL)
2749 return -ENOMEM;
2750
Theodore Ts'o16828082010-10-27 21:30:09 -04002751 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2752 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002753 if (ext4_ac_cachep == NULL) {
2754 kmem_cache_destroy(ext4_pspace_cachep);
2755 return -ENOMEM;
2756 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002757
Theodore Ts'o16828082010-10-27 21:30:09 -04002758 ext4_free_ext_cachep = KMEM_CACHE(ext4_free_data,
2759 SLAB_RECLAIM_ACCOUNT);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002760 if (ext4_free_ext_cachep == NULL) {
2761 kmem_cache_destroy(ext4_pspace_cachep);
2762 kmem_cache_destroy(ext4_ac_cachep);
2763 return -ENOMEM;
2764 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002765 ext4_create_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002766 return 0;
2767}
2768
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002769void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002770{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002771 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002772 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2773 * before destroying the slab cache.
2774 */
2775 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002776 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002777 kmem_cache_destroy(ext4_ac_cachep);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002778 kmem_cache_destroy(ext4_free_ext_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002779 ext4_groupinfo_destroy_slabs();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002780 ext4_remove_debugfs_entry();
Alex Tomasc9de5602008-01-29 00:19:52 -05002781}
2782
2783
2784/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002785 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002786 * Returns 0 if success or error code
2787 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002788static noinline_for_stack int
2789ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002790 handle_t *handle, unsigned int reserv_blks)
Alex Tomasc9de5602008-01-29 00:19:52 -05002791{
2792 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002793 struct ext4_group_desc *gdp;
2794 struct buffer_head *gdp_bh;
2795 struct ext4_sb_info *sbi;
2796 struct super_block *sb;
2797 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002798 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002799
2800 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2801 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2802
2803 sb = ac->ac_sb;
2804 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002805
2806 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002807 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002808 if (!bitmap_bh)
2809 goto out_err;
2810
2811 err = ext4_journal_get_write_access(handle, bitmap_bh);
2812 if (err)
2813 goto out_err;
2814
2815 err = -EIO;
2816 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2817 if (!gdp)
2818 goto out_err;
2819
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002820 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05002821 ext4_free_blks_count(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002822
Alex Tomasc9de5602008-01-29 00:19:52 -05002823 err = ext4_journal_get_write_access(handle, gdp_bh);
2824 if (err)
2825 goto out_err;
2826
Akinobu Mitabda00de2010-03-03 23:53:25 -05002827 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002828
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002829 len = ac->ac_b_ex.fe_len;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002830 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002831 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002832 "fs metadata\n", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002833 /* File system mounted not to panic on error
2834 * Fix the bitmap and repeat the block allocation
2835 * We leak some of the blocks here.
2836 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002837 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
2838 mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2839 ac->ac_b_ex.fe_len);
2840 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002841 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002842 if (!err)
2843 err = -EAGAIN;
2844 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002845 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002846
2847 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002848#ifdef AGGRESSIVE_CHECK
2849 {
2850 int i;
2851 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2852 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2853 bitmap_bh->b_data));
2854 }
2855 }
2856#endif
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002857 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 -05002858 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2859 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002860 ext4_free_blks_set(sb, gdp,
2861 ext4_free_blocks_after_init(sb,
2862 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002863 }
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05002864 len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
2865 ext4_free_blks_set(sb, gdp, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002866 gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002867
2868 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002869 percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002870 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002871 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002872 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002873 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2874 /* release all the reserved blocks if non delalloc */
2875 percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
Alex Tomasc9de5602008-01-29 00:19:52 -05002876
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002877 if (sbi->s_log_groups_per_flex) {
2878 ext4_group_t flex_group = ext4_flex_group(sbi,
2879 ac->ac_b_ex.fe_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05002880 atomic_sub(ac->ac_b_ex.fe_len,
2881 &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002882 }
2883
Frank Mayhar03901312009-01-07 00:06:22 -05002884 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002885 if (err)
2886 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002887 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002888
2889out_err:
Theodore Ts'oa0375152010-06-11 23:14:04 -04002890 ext4_mark_super_dirty(sb);
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002891 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002892 return err;
2893}
2894
2895/*
2896 * here we normalize request for locality group
2897 * Group request are normalized to s_strip size if we set the same via mount
2898 * option. If not we set it to s_mb_group_prealloc which can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002899 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002900 *
2901 * XXX: should we try to preallocate more than the group has now?
2902 */
2903static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2904{
2905 struct super_block *sb = ac->ac_sb;
2906 struct ext4_locality_group *lg = ac->ac_lg;
2907
2908 BUG_ON(lg == NULL);
2909 if (EXT4_SB(sb)->s_stripe)
2910 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
2911 else
2912 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002913 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002914 current->pid, ac->ac_g_ex.fe_len);
2915}
2916
2917/*
2918 * Normalization means making request better in terms of
2919 * size and alignment
2920 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002921static noinline_for_stack void
2922ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002923 struct ext4_allocation_request *ar)
2924{
2925 int bsbits, max;
2926 ext4_lblk_t end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002927 loff_t size, orig_size, start_off;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002928 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002929 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002930 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002931
2932 /* do normalize only data requests, metadata requests
2933 do not need preallocation */
2934 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2935 return;
2936
2937 /* sometime caller may want exact blocks */
2938 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2939 return;
2940
2941 /* caller may indicate that preallocation isn't
2942 * required (it's a tail, for example) */
2943 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2944 return;
2945
2946 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2947 ext4_mb_normalize_group_request(ac);
2948 return ;
2949 }
2950
2951 bsbits = ac->ac_sb->s_blocksize_bits;
2952
2953 /* first, let's learn actual file size
2954 * given current request is allocated */
2955 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
2956 size = size << bsbits;
2957 if (size < i_size_read(ac->ac_inode))
2958 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002959 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002960
Valerie Clement19304792008-05-13 19:31:14 -04002961 /* max size of free chunks */
2962 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002963
Valerie Clement19304792008-05-13 19:31:14 -04002964#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2965 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002966
2967 /* first, try to predict filesize */
2968 /* XXX: should this table be tunable? */
2969 start_off = 0;
2970 if (size <= 16 * 1024) {
2971 size = 16 * 1024;
2972 } else if (size <= 32 * 1024) {
2973 size = 32 * 1024;
2974 } else if (size <= 64 * 1024) {
2975 size = 64 * 1024;
2976 } else if (size <= 128 * 1024) {
2977 size = 128 * 1024;
2978 } else if (size <= 256 * 1024) {
2979 size = 256 * 1024;
2980 } else if (size <= 512 * 1024) {
2981 size = 512 * 1024;
2982 } else if (size <= 1024 * 1024) {
2983 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002984 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002985 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002986 (21 - bsbits)) << 21;
2987 size = 2 * 1024 * 1024;
2988 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002989 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2990 (22 - bsbits)) << 22;
2991 size = 4 * 1024 * 1024;
2992 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002993 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002994 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2995 (23 - bsbits)) << 23;
2996 size = 8 * 1024 * 1024;
2997 } else {
2998 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2999 size = ac->ac_o_ex.fe_len << bsbits;
3000 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003001 size = size >> bsbits;
3002 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003003
3004 /* don't cover already allocated blocks in selected range */
3005 if (ar->pleft && start <= ar->lleft) {
3006 size -= ar->lleft + 1 - start;
3007 start = ar->lleft + 1;
3008 }
3009 if (ar->pright && start + size - 1 >= ar->lright)
3010 size -= start + size - ar->lright;
3011
3012 end = start + size;
3013
3014 /* check we don't cross already preallocated blocks */
3015 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003016 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003017 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003018
Alex Tomasc9de5602008-01-29 00:19:52 -05003019 if (pa->pa_deleted)
3020 continue;
3021 spin_lock(&pa->pa_lock);
3022 if (pa->pa_deleted) {
3023 spin_unlock(&pa->pa_lock);
3024 continue;
3025 }
3026
3027 pa_end = pa->pa_lstart + pa->pa_len;
3028
3029 /* PA must not overlap original request */
3030 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3031 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3032
Eric Sandeen38877f42009-08-17 23:55:24 -04003033 /* skip PAs this normalized request doesn't overlap with */
3034 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003035 spin_unlock(&pa->pa_lock);
3036 continue;
3037 }
3038 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3039
Eric Sandeen38877f42009-08-17 23:55:24 -04003040 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003041 if (pa_end <= ac->ac_o_ex.fe_logical) {
3042 BUG_ON(pa_end < start);
3043 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003044 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003045 BUG_ON(pa->pa_lstart > end);
3046 end = pa->pa_lstart;
3047 }
3048 spin_unlock(&pa->pa_lock);
3049 }
3050 rcu_read_unlock();
3051 size = end - start;
3052
3053 /* XXX: extra loop to check we really don't overlap preallocations */
3054 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003055 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003056 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003057 spin_lock(&pa->pa_lock);
3058 if (pa->pa_deleted == 0) {
3059 pa_end = pa->pa_lstart + pa->pa_len;
3060 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3061 }
3062 spin_unlock(&pa->pa_lock);
3063 }
3064 rcu_read_unlock();
3065
3066 if (start + size <= ac->ac_o_ex.fe_logical &&
3067 start > ac->ac_o_ex.fe_logical) {
3068 printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3069 (unsigned long) start, (unsigned long) size,
3070 (unsigned long) ac->ac_o_ex.fe_logical);
3071 }
3072 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3073 start > ac->ac_o_ex.fe_logical);
Eric Sandeen8d03c7a2009-03-14 11:51:46 -04003074 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003075
3076 /* now prepare goal request */
3077
3078 /* XXX: is it better to align blocks WRT to logical
3079 * placement or satisfy big request as is */
3080 ac->ac_g_ex.fe_logical = start;
3081 ac->ac_g_ex.fe_len = size;
3082
3083 /* define goal start in order to merge */
3084 if (ar->pright && (ar->lright == (start + size))) {
3085 /* merge to the right */
3086 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3087 &ac->ac_f_ex.fe_group,
3088 &ac->ac_f_ex.fe_start);
3089 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3090 }
3091 if (ar->pleft && (ar->lleft + 1 == start)) {
3092 /* merge to the left */
3093 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3094 &ac->ac_f_ex.fe_group,
3095 &ac->ac_f_ex.fe_start);
3096 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3097 }
3098
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003099 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003100 (unsigned) orig_size, (unsigned) start);
3101}
3102
3103static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3104{
3105 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3106
3107 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3108 atomic_inc(&sbi->s_bal_reqs);
3109 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003110 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003111 atomic_inc(&sbi->s_bal_success);
3112 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3113 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3114 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3115 atomic_inc(&sbi->s_bal_goals);
3116 if (ac->ac_found > sbi->s_mb_max_to_scan)
3117 atomic_inc(&sbi->s_bal_breaks);
3118 }
3119
Theodore Ts'o296c3552009-09-30 00:32:42 -04003120 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3121 trace_ext4_mballoc_alloc(ac);
3122 else
3123 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003124}
3125
3126/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003127 * Called on failure; free up any blocks from the inode PA for this
3128 * context. We don't need this for MB_GROUP_PA because we only change
3129 * pa_free in ext4_mb_release_context(), but on failure, we've already
3130 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3131 */
3132static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3133{
3134 struct ext4_prealloc_space *pa = ac->ac_pa;
3135 int len;
3136
3137 if (pa && pa->pa_type == MB_INODE_PA) {
3138 len = ac->ac_b_ex.fe_len;
3139 pa->pa_free += len;
3140 }
3141
3142}
3143
3144/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003145 * use blocks preallocated to inode
3146 */
3147static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3148 struct ext4_prealloc_space *pa)
3149{
3150 ext4_fsblk_t start;
3151 ext4_fsblk_t end;
3152 int len;
3153
3154 /* found preallocated blocks, use them */
3155 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3156 end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3157 len = end - start;
3158 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3159 &ac->ac_b_ex.fe_start);
3160 ac->ac_b_ex.fe_len = len;
3161 ac->ac_status = AC_STATUS_FOUND;
3162 ac->ac_pa = pa;
3163
3164 BUG_ON(start < pa->pa_pstart);
3165 BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3166 BUG_ON(pa->pa_free < len);
3167 pa->pa_free -= len;
3168
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003169 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003170}
3171
3172/*
3173 * use blocks preallocated to locality group
3174 */
3175static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3176 struct ext4_prealloc_space *pa)
3177{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003178 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003179
Alex Tomasc9de5602008-01-29 00:19:52 -05003180 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3181 &ac->ac_b_ex.fe_group,
3182 &ac->ac_b_ex.fe_start);
3183 ac->ac_b_ex.fe_len = len;
3184 ac->ac_status = AC_STATUS_FOUND;
3185 ac->ac_pa = pa;
3186
3187 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003188 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003189 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003190 * in on-disk bitmap -- see ext4_mb_release_context()
3191 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003192 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003193 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003194}
3195
3196/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003197 * Return the prealloc space that have minimal distance
3198 * from the goal block. @cpa is the prealloc
3199 * space that is having currently known minimal distance
3200 * from the goal block.
3201 */
3202static struct ext4_prealloc_space *
3203ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3204 struct ext4_prealloc_space *pa,
3205 struct ext4_prealloc_space *cpa)
3206{
3207 ext4_fsblk_t cur_distance, new_distance;
3208
3209 if (cpa == NULL) {
3210 atomic_inc(&pa->pa_count);
3211 return pa;
3212 }
3213 cur_distance = abs(goal_block - cpa->pa_pstart);
3214 new_distance = abs(goal_block - pa->pa_pstart);
3215
Coly Li5a54b2f2011-02-24 14:10:05 -05003216 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003217 return cpa;
3218
3219 /* drop the previous reference */
3220 atomic_dec(&cpa->pa_count);
3221 atomic_inc(&pa->pa_count);
3222 return pa;
3223}
3224
3225/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003226 * search goal blocks in preallocated space
3227 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003228static noinline_for_stack int
3229ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003230{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003231 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003232 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3233 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003234 struct ext4_prealloc_space *pa, *cpa = NULL;
3235 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003236
3237 /* only data can be preallocated */
3238 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3239 return 0;
3240
3241 /* first, try per-file preallocation */
3242 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003243 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003244
3245 /* all fields in this condition don't change,
3246 * so we can skip locking for them */
3247 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3248 ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3249 continue;
3250
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003251 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003252 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003253 pa->pa_pstart + pa->pa_len > EXT4_MAX_BLOCK_FILE_PHYS)
3254 continue;
3255
Alex Tomasc9de5602008-01-29 00:19:52 -05003256 /* found preallocated blocks, use them */
3257 spin_lock(&pa->pa_lock);
3258 if (pa->pa_deleted == 0 && pa->pa_free) {
3259 atomic_inc(&pa->pa_count);
3260 ext4_mb_use_inode_pa(ac, pa);
3261 spin_unlock(&pa->pa_lock);
3262 ac->ac_criteria = 10;
3263 rcu_read_unlock();
3264 return 1;
3265 }
3266 spin_unlock(&pa->pa_lock);
3267 }
3268 rcu_read_unlock();
3269
3270 /* can we use group allocation? */
3271 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3272 return 0;
3273
3274 /* inode may have no locality group for some reason */
3275 lg = ac->ac_lg;
3276 if (lg == NULL)
3277 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003278 order = fls(ac->ac_o_ex.fe_len) - 1;
3279 if (order > PREALLOC_TB_SIZE - 1)
3280 /* The max size of hash table is PREALLOC_TB_SIZE */
3281 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003282
Akinobu Mitabda00de2010-03-03 23:53:25 -05003283 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003284 /*
3285 * search for the prealloc space that is having
3286 * minimal distance from the goal block.
3287 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003288 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3289 rcu_read_lock();
3290 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3291 pa_inode_list) {
3292 spin_lock(&pa->pa_lock);
3293 if (pa->pa_deleted == 0 &&
3294 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003295
3296 cpa = ext4_mb_check_group_pa(goal_block,
3297 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003298 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003299 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003300 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003301 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003302 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003303 if (cpa) {
3304 ext4_mb_use_group_pa(ac, cpa);
3305 ac->ac_criteria = 20;
3306 return 1;
3307 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003308 return 0;
3309}
3310
3311/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003312 * the function goes through all block freed in the group
3313 * but not yet committed and marks them used in in-core bitmap.
3314 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003315 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003316 */
3317static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3318 ext4_group_t group)
3319{
3320 struct rb_node *n;
3321 struct ext4_group_info *grp;
3322 struct ext4_free_data *entry;
3323
3324 grp = ext4_get_group_info(sb, group);
3325 n = rb_first(&(grp->bb_free_root));
3326
3327 while (n) {
3328 entry = rb_entry(n, struct ext4_free_data, node);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003329 mb_set_bits(bitmap, entry->start_blk, entry->count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003330 n = rb_next(n);
3331 }
3332 return;
3333}
3334
3335/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003336 * the function goes through all preallocation in this group and marks them
3337 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003338 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003339 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003340static noinline_for_stack
3341void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003342 ext4_group_t group)
3343{
3344 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3345 struct ext4_prealloc_space *pa;
3346 struct list_head *cur;
3347 ext4_group_t groupnr;
3348 ext4_grpblk_t start;
3349 int preallocated = 0;
3350 int count = 0;
3351 int len;
3352
3353 /* all form of preallocation discards first load group,
3354 * so the only competing code is preallocation use.
3355 * we don't need any locking here
3356 * notice we do NOT ignore preallocations with pa_deleted
3357 * otherwise we could leave used blocks available for
3358 * allocation in buddy when concurrent ext4_mb_put_pa()
3359 * is dropping preallocation
3360 */
3361 list_for_each(cur, &grp->bb_prealloc_list) {
3362 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3363 spin_lock(&pa->pa_lock);
3364 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3365 &groupnr, &start);
3366 len = pa->pa_len;
3367 spin_unlock(&pa->pa_lock);
3368 if (unlikely(len == 0))
3369 continue;
3370 BUG_ON(groupnr != group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003371 mb_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003372 preallocated += len;
3373 count++;
3374 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003375 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003376}
3377
3378static void ext4_mb_pa_callback(struct rcu_head *head)
3379{
3380 struct ext4_prealloc_space *pa;
3381 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3382 kmem_cache_free(ext4_pspace_cachep, pa);
3383}
3384
3385/*
3386 * drops a reference to preallocated space descriptor
3387 * if this was the last reference and the space is consumed
3388 */
3389static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3390 struct super_block *sb, struct ext4_prealloc_space *pa)
3391{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003392 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003393 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003394
3395 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3396 return;
3397
3398 /* in this short window concurrent discard can set pa_deleted */
3399 spin_lock(&pa->pa_lock);
3400 if (pa->pa_deleted == 1) {
3401 spin_unlock(&pa->pa_lock);
3402 return;
3403 }
3404
3405 pa->pa_deleted = 1;
3406 spin_unlock(&pa->pa_lock);
3407
Eric Sandeend33a1972009-03-16 23:25:40 -04003408 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003409 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003410 * If doing group-based preallocation, pa_pstart may be in the
3411 * next group when pa is used up
3412 */
3413 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003414 grp_blk--;
3415
3416 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003417
3418 /*
3419 * possible race:
3420 *
3421 * P1 (buddy init) P2 (regular allocation)
3422 * find block B in PA
3423 * copy on-disk bitmap to buddy
3424 * mark B in on-disk bitmap
3425 * drop PA from group
3426 * mark all PAs in buddy
3427 *
3428 * thus, P1 initializes buddy with B available. to prevent this
3429 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3430 * against that pair
3431 */
3432 ext4_lock_group(sb, grp);
3433 list_del(&pa->pa_group_list);
3434 ext4_unlock_group(sb, grp);
3435
3436 spin_lock(pa->pa_obj_lock);
3437 list_del_rcu(&pa->pa_inode_list);
3438 spin_unlock(pa->pa_obj_lock);
3439
3440 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3441}
3442
3443/*
3444 * creates new preallocated space for given inode
3445 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003446static noinline_for_stack int
3447ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003448{
3449 struct super_block *sb = ac->ac_sb;
3450 struct ext4_prealloc_space *pa;
3451 struct ext4_group_info *grp;
3452 struct ext4_inode_info *ei;
3453
3454 /* preallocate only when found space is larger then requested */
3455 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3456 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3457 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3458
3459 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3460 if (pa == NULL)
3461 return -ENOMEM;
3462
3463 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3464 int winl;
3465 int wins;
3466 int win;
3467 int offs;
3468
3469 /* we can't allocate as much as normalizer wants.
3470 * so, found space must get proper lstart
3471 * to cover original request */
3472 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3473 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3474
3475 /* we're limited by original request in that
3476 * logical block must be covered any way
3477 * winl is window we can move our chunk within */
3478 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3479
3480 /* also, we should cover whole original request */
3481 wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3482
3483 /* the smallest one defines real window */
3484 win = min(winl, wins);
3485
3486 offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3487 if (offs && offs < win)
3488 win = offs;
3489
3490 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3491 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3492 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3493 }
3494
3495 /* preallocation can change ac_b_ex, thus we store actually
3496 * allocated blocks for history */
3497 ac->ac_f_ex = ac->ac_b_ex;
3498
3499 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3500 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3501 pa->pa_len = ac->ac_b_ex.fe_len;
3502 pa->pa_free = pa->pa_len;
3503 atomic_set(&pa->pa_count, 1);
3504 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003505 INIT_LIST_HEAD(&pa->pa_inode_list);
3506 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003507 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003508 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003509
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003510 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003511 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003512 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003513
3514 ext4_mb_use_inode_pa(ac, pa);
3515 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3516
3517 ei = EXT4_I(ac->ac_inode);
3518 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3519
3520 pa->pa_obj_lock = &ei->i_prealloc_lock;
3521 pa->pa_inode = ac->ac_inode;
3522
3523 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3524 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3525 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3526
3527 spin_lock(pa->pa_obj_lock);
3528 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3529 spin_unlock(pa->pa_obj_lock);
3530
3531 return 0;
3532}
3533
3534/*
3535 * creates new preallocated space for locality group inodes belongs to
3536 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003537static noinline_for_stack int
3538ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003539{
3540 struct super_block *sb = ac->ac_sb;
3541 struct ext4_locality_group *lg;
3542 struct ext4_prealloc_space *pa;
3543 struct ext4_group_info *grp;
3544
3545 /* preallocate only when found space is larger then requested */
3546 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3547 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3548 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3549
3550 BUG_ON(ext4_pspace_cachep == NULL);
3551 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3552 if (pa == NULL)
3553 return -ENOMEM;
3554
3555 /* preallocation can change ac_b_ex, thus we store actually
3556 * allocated blocks for history */
3557 ac->ac_f_ex = ac->ac_b_ex;
3558
3559 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3560 pa->pa_lstart = pa->pa_pstart;
3561 pa->pa_len = ac->ac_b_ex.fe_len;
3562 pa->pa_free = pa->pa_len;
3563 atomic_set(&pa->pa_count, 1);
3564 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003565 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003566 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003567 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003568 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003569
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003570 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003571 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3572 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003573
3574 ext4_mb_use_group_pa(ac, pa);
3575 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3576
3577 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3578 lg = ac->ac_lg;
3579 BUG_ON(lg == NULL);
3580
3581 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3582 pa->pa_inode = NULL;
3583
3584 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3585 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3586 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3587
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003588 /*
3589 * We will later add the new pa to the right bucket
3590 * after updating the pa_free in ext4_mb_release_context
3591 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003592 return 0;
3593}
3594
3595static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3596{
3597 int err;
3598
3599 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3600 err = ext4_mb_new_group_pa(ac);
3601 else
3602 err = ext4_mb_new_inode_pa(ac);
3603 return err;
3604}
3605
3606/*
3607 * finds all unused blocks in on-disk bitmap, frees them in
3608 * in-core bitmap and buddy.
3609 * @pa must be unlinked from inode and group lists, so that
3610 * nobody else can find/use it.
3611 * the caller MUST hold group/inode locks.
3612 * TODO: optimize the case when there are no in-core structures yet
3613 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003614static noinline_for_stack int
3615ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003616 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003617{
Alex Tomasc9de5602008-01-29 00:19:52 -05003618 struct super_block *sb = e4b->bd_sb;
3619 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003620 unsigned int end;
3621 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003622 ext4_group_t group;
3623 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003624 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003625 int err = 0;
3626 int free = 0;
3627
3628 BUG_ON(pa->pa_deleted == 0);
3629 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'oba80b102009-01-03 20:03:21 -05003630 grp_blk_start = pa->pa_pstart - bit;
Alex Tomasc9de5602008-01-29 00:19:52 -05003631 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3632 end = bit + pa->pa_len;
3633
Alex Tomasc9de5602008-01-29 00:19:52 -05003634 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003635 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003636 if (bit >= end)
3637 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003638 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003639 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003640 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3641 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003642 free += next - bit;
3643
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003644 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
3645 trace_ext4_mb_release_inode_pa(sb, pa->pa_inode, pa,
3646 grp_blk_start + bit, next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003647 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3648 bit = next + 1;
3649 }
3650 if (free != pa->pa_free) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003651 printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003652 pa, (unsigned long) pa->pa_lstart,
3653 (unsigned long) pa->pa_pstart,
3654 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003655 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003656 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003657 /*
3658 * pa is already deleted so we use the value obtained
3659 * from the bitmap and continue.
3660 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003661 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003662 atomic_add(free, &sbi->s_mb_discarded);
3663
3664 return err;
3665}
3666
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003667static noinline_for_stack int
3668ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003669 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003670{
Alex Tomasc9de5602008-01-29 00:19:52 -05003671 struct super_block *sb = e4b->bd_sb;
3672 ext4_group_t group;
3673 ext4_grpblk_t bit;
3674
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003675 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003676 BUG_ON(pa->pa_deleted == 0);
3677 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3678 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3679 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3680 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003681 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003682
3683 return 0;
3684}
3685
3686/*
3687 * releases all preallocations in given group
3688 *
3689 * first, we need to decide discard policy:
3690 * - when do we discard
3691 * 1) ENOSPC
3692 * - how many do we discard
3693 * 1) how many requested
3694 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003695static noinline_for_stack int
3696ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003697 ext4_group_t group, int needed)
3698{
3699 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3700 struct buffer_head *bitmap_bh = NULL;
3701 struct ext4_prealloc_space *pa, *tmp;
3702 struct list_head list;
3703 struct ext4_buddy e4b;
3704 int err;
3705 int busy = 0;
3706 int free = 0;
3707
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003708 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003709
3710 if (list_empty(&grp->bb_prealloc_list))
3711 return 0;
3712
Theodore Ts'o574ca172008-07-11 19:27:31 -04003713 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003714 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003715 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003716 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003717 }
3718
3719 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003720 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003721 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003722 put_bh(bitmap_bh);
3723 return 0;
3724 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003725
3726 if (needed == 0)
3727 needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3728
Alex Tomasc9de5602008-01-29 00:19:52 -05003729 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003730repeat:
3731 ext4_lock_group(sb, group);
3732 list_for_each_entry_safe(pa, tmp,
3733 &grp->bb_prealloc_list, pa_group_list) {
3734 spin_lock(&pa->pa_lock);
3735 if (atomic_read(&pa->pa_count)) {
3736 spin_unlock(&pa->pa_lock);
3737 busy = 1;
3738 continue;
3739 }
3740 if (pa->pa_deleted) {
3741 spin_unlock(&pa->pa_lock);
3742 continue;
3743 }
3744
3745 /* seems this one can be freed ... */
3746 pa->pa_deleted = 1;
3747
3748 /* we can trust pa_free ... */
3749 free += pa->pa_free;
3750
3751 spin_unlock(&pa->pa_lock);
3752
3753 list_del(&pa->pa_group_list);
3754 list_add(&pa->u.pa_tmp_list, &list);
3755 }
3756
3757 /* if we still need more blocks and some PAs were used, try again */
3758 if (free < needed && busy) {
3759 busy = 0;
3760 ext4_unlock_group(sb, group);
3761 /*
3762 * Yield the CPU here so that we don't get soft lockup
3763 * in non preempt case.
3764 */
3765 yield();
3766 goto repeat;
3767 }
3768
3769 /* found anything to free? */
3770 if (list_empty(&list)) {
3771 BUG_ON(free != 0);
3772 goto out;
3773 }
3774
3775 /* now free all selected PAs */
3776 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3777
3778 /* remove from object (inode or locality group) */
3779 spin_lock(pa->pa_obj_lock);
3780 list_del_rcu(&pa->pa_inode_list);
3781 spin_unlock(pa->pa_obj_lock);
3782
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003783 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003784 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003785 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003786 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003787
3788 list_del(&pa->u.pa_tmp_list);
3789 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3790 }
3791
3792out:
3793 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003794 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003795 put_bh(bitmap_bh);
3796 return free;
3797}
3798
3799/*
3800 * releases all non-used preallocated blocks for given inode
3801 *
3802 * It's important to discard preallocations under i_data_sem
3803 * We don't want another block to be served from the prealloc
3804 * space when we are discarding the inode prealloc space.
3805 *
3806 * FIXME!! Make sure it is valid at all the call sites
3807 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003808void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003809{
3810 struct ext4_inode_info *ei = EXT4_I(inode);
3811 struct super_block *sb = inode->i_sb;
3812 struct buffer_head *bitmap_bh = NULL;
3813 struct ext4_prealloc_space *pa, *tmp;
3814 ext4_group_t group = 0;
3815 struct list_head list;
3816 struct ext4_buddy e4b;
3817 int err;
3818
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003819 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003820 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3821 return;
3822 }
3823
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003824 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003825 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003826
3827 INIT_LIST_HEAD(&list);
3828
3829repeat:
3830 /* first, collect all pa's in the inode */
3831 spin_lock(&ei->i_prealloc_lock);
3832 while (!list_empty(&ei->i_prealloc_list)) {
3833 pa = list_entry(ei->i_prealloc_list.next,
3834 struct ext4_prealloc_space, pa_inode_list);
3835 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3836 spin_lock(&pa->pa_lock);
3837 if (atomic_read(&pa->pa_count)) {
3838 /* this shouldn't happen often - nobody should
3839 * use preallocation while we're discarding it */
3840 spin_unlock(&pa->pa_lock);
3841 spin_unlock(&ei->i_prealloc_lock);
3842 printk(KERN_ERR "uh-oh! used pa while discarding\n");
3843 WARN_ON(1);
3844 schedule_timeout_uninterruptible(HZ);
3845 goto repeat;
3846
3847 }
3848 if (pa->pa_deleted == 0) {
3849 pa->pa_deleted = 1;
3850 spin_unlock(&pa->pa_lock);
3851 list_del_rcu(&pa->pa_inode_list);
3852 list_add(&pa->u.pa_tmp_list, &list);
3853 continue;
3854 }
3855
3856 /* someone is deleting pa right now */
3857 spin_unlock(&pa->pa_lock);
3858 spin_unlock(&ei->i_prealloc_lock);
3859
3860 /* we have to wait here because pa_deleted
3861 * doesn't mean pa is already unlinked from
3862 * the list. as we might be called from
3863 * ->clear_inode() the inode will get freed
3864 * and concurrent thread which is unlinking
3865 * pa from inode's list may access already
3866 * freed memory, bad-bad-bad */
3867
3868 /* XXX: if this happens too often, we can
3869 * add a flag to force wait only in case
3870 * of ->clear_inode(), but not in case of
3871 * regular truncate */
3872 schedule_timeout_uninterruptible(HZ);
3873 goto repeat;
3874 }
3875 spin_unlock(&ei->i_prealloc_lock);
3876
3877 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003878 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003879 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3880
3881 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003882 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003883 ext4_error(sb, "Error loading buddy information for %u",
3884 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003885 continue;
3886 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003887
Theodore Ts'o574ca172008-07-11 19:27:31 -04003888 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003889 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003890 ext4_error(sb, "Error reading block bitmap for %u",
3891 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003892 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003893 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003894 }
3895
3896 ext4_lock_group(sb, group);
3897 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003898 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003899 ext4_unlock_group(sb, group);
3900
Jing Zhange39e07f2010-05-14 00:00:00 -04003901 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003902 put_bh(bitmap_bh);
3903
3904 list_del(&pa->u.pa_tmp_list);
3905 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3906 }
3907}
3908
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003909#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003910static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3911{
3912 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003913 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003914
Eric Sandeene3570632010-07-27 11:56:08 -04003915 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
3916 return;
3917
Alex Tomasc9de5602008-01-29 00:19:52 -05003918 printk(KERN_ERR "EXT4-fs: Can't allocate:"
3919 " Allocation context details:\n");
3920 printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
3921 ac->ac_status, ac->ac_flags);
3922 printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
3923 "best %lu/%lu/%lu@%lu cr %d\n",
3924 (unsigned long)ac->ac_o_ex.fe_group,
3925 (unsigned long)ac->ac_o_ex.fe_start,
3926 (unsigned long)ac->ac_o_ex.fe_len,
3927 (unsigned long)ac->ac_o_ex.fe_logical,
3928 (unsigned long)ac->ac_g_ex.fe_group,
3929 (unsigned long)ac->ac_g_ex.fe_start,
3930 (unsigned long)ac->ac_g_ex.fe_len,
3931 (unsigned long)ac->ac_g_ex.fe_logical,
3932 (unsigned long)ac->ac_b_ex.fe_group,
3933 (unsigned long)ac->ac_b_ex.fe_start,
3934 (unsigned long)ac->ac_b_ex.fe_len,
3935 (unsigned long)ac->ac_b_ex.fe_logical,
3936 (int)ac->ac_criteria);
3937 printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
3938 ac->ac_found);
3939 printk(KERN_ERR "EXT4-fs: groups: \n");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003940 ngroups = ext4_get_groups_count(sb);
3941 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003942 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3943 struct ext4_prealloc_space *pa;
3944 ext4_grpblk_t start;
3945 struct list_head *cur;
3946 ext4_lock_group(sb, i);
3947 list_for_each(cur, &grp->bb_prealloc_list) {
3948 pa = list_entry(cur, struct ext4_prealloc_space,
3949 pa_group_list);
3950 spin_lock(&pa->pa_lock);
3951 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3952 NULL, &start);
3953 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003954 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3955 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003956 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003957 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003958
3959 if (grp->bb_free == 0)
3960 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003961 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003962 i, grp->bb_free, grp->bb_fragments);
3963 }
3964 printk(KERN_ERR "\n");
3965}
3966#else
3967static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3968{
3969 return;
3970}
3971#endif
3972
3973/*
3974 * We use locality group preallocation for small size file. The size of the
3975 * file is determined by the current size or the resulting size after
3976 * allocation which ever is larger
3977 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003978 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003979 */
3980static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3981{
3982 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3983 int bsbits = ac->ac_sb->s_blocksize_bits;
3984 loff_t size, isize;
3985
3986 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3987 return;
3988
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003989 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3990 return;
3991
Alex Tomasc9de5602008-01-29 00:19:52 -05003992 size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
Theodore Ts'o50797482009-09-18 13:34:02 -04003993 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3994 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003995
Theodore Ts'o50797482009-09-18 13:34:02 -04003996 if ((size == isize) &&
3997 !ext4_fs_is_busy(sbi) &&
3998 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3999 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4000 return;
4001 }
4002
Alex Tomasc9de5602008-01-29 00:19:52 -05004003 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004004 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004005 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004006 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004007 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004008 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004009
4010 BUG_ON(ac->ac_lg != NULL);
4011 /*
4012 * locality group prealloc space are per cpu. The reason for having
4013 * per cpu locality group is to reduce the contention between block
4014 * request from multiple CPUs.
4015 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09004016 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004017
4018 /* we're going to use group allocation */
4019 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4020
4021 /* serialize all allocations in the group */
4022 mutex_lock(&ac->ac_lg->lg_mutex);
4023}
4024
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004025static noinline_for_stack int
4026ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004027 struct ext4_allocation_request *ar)
4028{
4029 struct super_block *sb = ar->inode->i_sb;
4030 struct ext4_sb_info *sbi = EXT4_SB(sb);
4031 struct ext4_super_block *es = sbi->s_es;
4032 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004033 unsigned int len;
4034 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004035 ext4_grpblk_t block;
4036
4037 /* we can't allocate > group size */
4038 len = ar->len;
4039
4040 /* just a dirty hack to filter too big requests */
4041 if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4042 len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4043
4044 /* start searching from the goal */
4045 goal = ar->goal;
4046 if (goal < le32_to_cpu(es->s_first_data_block) ||
4047 goal >= ext4_blocks_count(es))
4048 goal = le32_to_cpu(es->s_first_data_block);
4049 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4050
4051 /* set up allocation goals */
Theodore Ts'o833576b2009-07-13 09:45:52 -04004052 memset(ac, 0, sizeof(struct ext4_allocation_context));
Alex Tomasc9de5602008-01-29 00:19:52 -05004053 ac->ac_b_ex.fe_logical = ar->logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004054 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004055 ac->ac_sb = sb;
4056 ac->ac_inode = ar->inode;
4057 ac->ac_o_ex.fe_logical = ar->logical;
4058 ac->ac_o_ex.fe_group = group;
4059 ac->ac_o_ex.fe_start = block;
4060 ac->ac_o_ex.fe_len = len;
4061 ac->ac_g_ex.fe_logical = ar->logical;
4062 ac->ac_g_ex.fe_group = group;
4063 ac->ac_g_ex.fe_start = block;
4064 ac->ac_g_ex.fe_len = len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004065 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004066
4067 /* we have to define context: we'll we work with a file or
4068 * locality group. this is a policy, actually */
4069 ext4_mb_group_or_file(ac);
4070
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004071 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004072 "left: %u/%u, right %u/%u to %swritable\n",
4073 (unsigned) ar->len, (unsigned) ar->logical,
4074 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4075 (unsigned) ar->lleft, (unsigned) ar->pleft,
4076 (unsigned) ar->lright, (unsigned) ar->pright,
4077 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4078 return 0;
4079
4080}
4081
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004082static noinline_for_stack void
4083ext4_mb_discard_lg_preallocations(struct super_block *sb,
4084 struct ext4_locality_group *lg,
4085 int order, int total_entries)
4086{
4087 ext4_group_t group = 0;
4088 struct ext4_buddy e4b;
4089 struct list_head discard_list;
4090 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004091
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004092 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004093
4094 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004095
4096 spin_lock(&lg->lg_prealloc_lock);
4097 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4098 pa_inode_list) {
4099 spin_lock(&pa->pa_lock);
4100 if (atomic_read(&pa->pa_count)) {
4101 /*
4102 * This is the pa that we just used
4103 * for block allocation. So don't
4104 * free that
4105 */
4106 spin_unlock(&pa->pa_lock);
4107 continue;
4108 }
4109 if (pa->pa_deleted) {
4110 spin_unlock(&pa->pa_lock);
4111 continue;
4112 }
4113 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004114 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004115
4116 /* seems this one can be freed ... */
4117 pa->pa_deleted = 1;
4118 spin_unlock(&pa->pa_lock);
4119
4120 list_del_rcu(&pa->pa_inode_list);
4121 list_add(&pa->u.pa_tmp_list, &discard_list);
4122
4123 total_entries--;
4124 if (total_entries <= 5) {
4125 /*
4126 * we want to keep only 5 entries
4127 * allowing it to grow to 8. This
4128 * mak sure we don't call discard
4129 * soon for this list.
4130 */
4131 break;
4132 }
4133 }
4134 spin_unlock(&lg->lg_prealloc_lock);
4135
4136 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4137
4138 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4139 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004140 ext4_error(sb, "Error loading buddy information for %u",
4141 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004142 continue;
4143 }
4144 ext4_lock_group(sb, group);
4145 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004146 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004147 ext4_unlock_group(sb, group);
4148
Jing Zhange39e07f2010-05-14 00:00:00 -04004149 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004150 list_del(&pa->u.pa_tmp_list);
4151 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4152 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004153}
4154
4155/*
4156 * We have incremented pa_count. So it cannot be freed at this
4157 * point. Also we hold lg_mutex. So no parallel allocation is
4158 * possible from this lg. That means pa_free cannot be updated.
4159 *
4160 * A parallel ext4_mb_discard_group_preallocations is possible.
4161 * which can cause the lg_prealloc_list to be updated.
4162 */
4163
4164static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4165{
4166 int order, added = 0, lg_prealloc_count = 1;
4167 struct super_block *sb = ac->ac_sb;
4168 struct ext4_locality_group *lg = ac->ac_lg;
4169 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4170
4171 order = fls(pa->pa_free) - 1;
4172 if (order > PREALLOC_TB_SIZE - 1)
4173 /* The max size of hash table is PREALLOC_TB_SIZE */
4174 order = PREALLOC_TB_SIZE - 1;
4175 /* Add the prealloc space to lg */
4176 rcu_read_lock();
4177 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4178 pa_inode_list) {
4179 spin_lock(&tmp_pa->pa_lock);
4180 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004181 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004182 continue;
4183 }
4184 if (!added && pa->pa_free < tmp_pa->pa_free) {
4185 /* Add to the tail of the previous entry */
4186 list_add_tail_rcu(&pa->pa_inode_list,
4187 &tmp_pa->pa_inode_list);
4188 added = 1;
4189 /*
4190 * we want to count the total
4191 * number of entries in the list
4192 */
4193 }
4194 spin_unlock(&tmp_pa->pa_lock);
4195 lg_prealloc_count++;
4196 }
4197 if (!added)
4198 list_add_tail_rcu(&pa->pa_inode_list,
4199 &lg->lg_prealloc_list[order]);
4200 rcu_read_unlock();
4201
4202 /* Now trim the list to be not more than 8 elements */
4203 if (lg_prealloc_count > 8) {
4204 ext4_mb_discard_lg_preallocations(sb, lg,
4205 order, lg_prealloc_count);
4206 return;
4207 }
4208 return ;
4209}
4210
Alex Tomasc9de5602008-01-29 00:19:52 -05004211/*
4212 * release all resource we used in allocation
4213 */
4214static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4215{
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004216 struct ext4_prealloc_space *pa = ac->ac_pa;
4217 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004218 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004219 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004220 spin_lock(&pa->pa_lock);
4221 pa->pa_pstart += ac->ac_b_ex.fe_len;
4222 pa->pa_lstart += ac->ac_b_ex.fe_len;
4223 pa->pa_free -= ac->ac_b_ex.fe_len;
4224 pa->pa_len -= ac->ac_b_ex.fe_len;
4225 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004226 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004227 }
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004228 if (ac->alloc_semp)
4229 up_read(ac->alloc_semp);
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004230 if (pa) {
4231 /*
4232 * We want to add the pa to the right bucket.
4233 * Remove it from the list and while adding
4234 * make sure the list to which we are adding
4235 * doesn't grow big. We need to release
4236 * alloc_semp before calling ext4_mb_add_n_trim()
4237 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004238 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004239 spin_lock(pa->pa_obj_lock);
4240 list_del_rcu(&pa->pa_inode_list);
4241 spin_unlock(pa->pa_obj_lock);
4242 ext4_mb_add_n_trim(ac);
4243 }
4244 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4245 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004246 if (ac->ac_bitmap_page)
4247 page_cache_release(ac->ac_bitmap_page);
4248 if (ac->ac_buddy_page)
4249 page_cache_release(ac->ac_buddy_page);
4250 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4251 mutex_unlock(&ac->ac_lg->lg_mutex);
4252 ext4_mb_collect_stats(ac);
4253 return 0;
4254}
4255
4256static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4257{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004258 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004259 int ret;
4260 int freed = 0;
4261
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004262 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004263 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004264 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4265 freed += ret;
4266 needed -= ret;
4267 }
4268
4269 return freed;
4270}
4271
4272/*
4273 * Main entry point into mballoc to allocate blocks
4274 * it tries to use preallocation first, then falls back
4275 * to usual allocation
4276 */
4277ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004278 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004279{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004280 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004281 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004282 struct ext4_sb_info *sbi;
4283 struct super_block *sb;
4284 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004285 unsigned int inquota = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004286 unsigned int reserv_blks = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004287
4288 sb = ar->inode->i_sb;
4289 sbi = EXT4_SB(sb);
4290
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004291 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004292
Mingming Cao60e58e02009-01-22 18:13:05 +01004293 /*
4294 * For delayed allocation, we could skip the ENOSPC and
4295 * EDQUOT check, as blocks and quotas have been already
4296 * reserved when data being copied into pagecache.
4297 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004298 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004299 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4300 else {
4301 /* Without delayed allocation we need to verify
4302 * there is enough free blocks to do block allocation
4303 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004304 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004305 while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4306 /* let others to free the space */
4307 yield();
4308 ar->len = ar->len >> 1;
4309 }
4310 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004311 *errp = -ENOSPC;
4312 return 0;
4313 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004314 reserv_blks = ar->len;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004315 while (ar->len && dquot_alloc_block(ar->inode, ar->len)) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004316 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4317 ar->len--;
4318 }
4319 inquota = ar->len;
4320 if (ar->len == 0) {
4321 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004322 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004323 }
Mingming Caod2a17632008-07-14 17:52:37 -04004324 }
Mingming Caod2a17632008-07-14 17:52:37 -04004325
Eric Sandeen256bdb42008-02-10 01:13:33 -05004326 ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004327 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004328 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004329 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004330 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004331 }
4332
Eric Sandeen256bdb42008-02-10 01:13:33 -05004333 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004334 if (*errp) {
4335 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004336 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004337 }
4338
Eric Sandeen256bdb42008-02-10 01:13:33 -05004339 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4340 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004341 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4342 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004343repeat:
4344 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004345 *errp = ext4_mb_regular_allocator(ac);
4346 if (*errp)
4347 goto errout;
Alex Tomasc9de5602008-01-29 00:19:52 -05004348
4349 /* as we've just preallocated more space than
4350 * user requested orinally, we store allocated
4351 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004352 if (ac->ac_status == AC_STATUS_FOUND &&
4353 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4354 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004355 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004356 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004357 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004358 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004359 /*
4360 * drop the reference that we took
4361 * in ext4_mb_use_best_found
4362 */
4363 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004364 ac->ac_b_ex.fe_group = 0;
4365 ac->ac_b_ex.fe_start = 0;
4366 ac->ac_b_ex.fe_len = 0;
4367 ac->ac_status = AC_STATUS_CONTINUE;
4368 goto repeat;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004369 } else if (*errp)
4370 errout:
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004371 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004372 else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004373 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4374 ar->len = ac->ac_b_ex.fe_len;
4375 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004376 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004377 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004378 if (freed)
4379 goto repeat;
4380 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004381 }
4382
4383 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004384 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004385 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004386 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004387 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004388 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004389out:
4390 if (ac)
4391 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004392 if (inquota && ar->len < inquota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004393 dquot_free_block(ar->inode, inquota - ar->len);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004394 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004395 if (!ext4_test_inode_state(ar->inode,
4396 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004397 /* release all the reserved blocks if non delalloc */
4398 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
4399 reserv_blks);
4400 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004401
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004402 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004403
Alex Tomasc9de5602008-01-29 00:19:52 -05004404 return block;
4405}
Alex Tomasc9de5602008-01-29 00:19:52 -05004406
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004407/*
4408 * We can merge two free data extents only if the physical blocks
4409 * are contiguous, AND the extents were freed by the same transaction,
4410 * AND the blocks are associated with the same group.
4411 */
4412static int can_merge(struct ext4_free_data *entry1,
4413 struct ext4_free_data *entry2)
4414{
4415 if ((entry1->t_tid == entry2->t_tid) &&
4416 (entry1->group == entry2->group) &&
4417 ((entry1->start_blk + entry1->count) == entry2->start_blk))
4418 return 1;
4419 return 0;
4420}
4421
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004422static noinline_for_stack int
4423ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004424 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004425{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004426 ext4_group_t group = e4b->bd_group;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004427 ext4_grpblk_t block;
4428 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004429 struct ext4_group_info *db = e4b->bd_info;
4430 struct super_block *sb = e4b->bd_sb;
4431 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004432 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4433 struct rb_node *parent = NULL, *new_node;
4434
Frank Mayhar03901312009-01-07 00:06:22 -05004435 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004436 BUG_ON(e4b->bd_bitmap_page == NULL);
4437 BUG_ON(e4b->bd_buddy_page == NULL);
4438
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004439 new_node = &new_entry->node;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004440 block = new_entry->start_blk;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004441
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004442 if (!*n) {
4443 /* first free block exent. We need to
4444 protect buddy cache from being freed,
4445 * otherwise we'll refresh it from
4446 * on-disk bitmap and lose not-yet-available
4447 * blocks */
4448 page_cache_get(e4b->bd_buddy_page);
4449 page_cache_get(e4b->bd_bitmap_page);
4450 }
4451 while (*n) {
4452 parent = *n;
4453 entry = rb_entry(parent, struct ext4_free_data, node);
4454 if (block < entry->start_blk)
4455 n = &(*n)->rb_left;
4456 else if (block >= (entry->start_blk + entry->count))
4457 n = &(*n)->rb_right;
4458 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004459 ext4_grp_locked_error(sb, group, 0,
4460 ext4_group_first_block_no(sb, group) + block,
4461 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004462 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004463 }
4464 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004465
4466 rb_link_node(new_node, parent, n);
4467 rb_insert_color(new_node, &db->bb_free_root);
4468
4469 /* Now try to see the extent can be merged to left and right */
4470 node = rb_prev(new_node);
4471 if (node) {
4472 entry = rb_entry(node, struct ext4_free_data, node);
4473 if (can_merge(entry, new_entry)) {
4474 new_entry->start_blk = entry->start_blk;
4475 new_entry->count += entry->count;
4476 rb_erase(node, &(db->bb_free_root));
4477 spin_lock(&sbi->s_md_lock);
4478 list_del(&entry->list);
4479 spin_unlock(&sbi->s_md_lock);
4480 kmem_cache_free(ext4_free_ext_cachep, entry);
4481 }
4482 }
4483
4484 node = rb_next(new_node);
4485 if (node) {
4486 entry = rb_entry(node, struct ext4_free_data, node);
4487 if (can_merge(new_entry, entry)) {
4488 new_entry->count += entry->count;
4489 rb_erase(node, &(db->bb_free_root));
4490 spin_lock(&sbi->s_md_lock);
4491 list_del(&entry->list);
4492 spin_unlock(&sbi->s_md_lock);
4493 kmem_cache_free(ext4_free_ext_cachep, entry);
4494 }
4495 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004496 /* Add the extent to transaction's private list */
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004497 spin_lock(&sbi->s_md_lock);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004498 list_add(&new_entry->list, &handle->h_transaction->t_private_list);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004499 spin_unlock(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004500 return 0;
4501}
4502
Theodore Ts'o44338712009-11-22 07:44:56 -05004503/**
4504 * ext4_free_blocks() -- Free given blocks and update quota
4505 * @handle: handle for this transaction
4506 * @inode: inode
4507 * @block: start physical block to free
4508 * @count: number of blocks to count
4509 * @metadata: Are these metadata blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004510 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004511void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004512 struct buffer_head *bh, ext4_fsblk_t block,
4513 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004514{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004515 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004516 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004517 struct ext4_group_desc *gdp;
Theodore Ts'o44338712009-11-22 07:44:56 -05004518 unsigned long freed = 0;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004519 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004520 ext4_grpblk_t bit;
4521 struct buffer_head *gd_bh;
4522 ext4_group_t block_group;
4523 struct ext4_sb_info *sbi;
4524 struct ext4_buddy e4b;
4525 int err = 0;
4526 int ret;
4527
Theodore Ts'oe6362602009-11-23 07:17:05 -05004528 if (bh) {
4529 if (block)
4530 BUG_ON(block != bh->b_blocknr);
4531 else
4532 block = bh->b_blocknr;
4533 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004534
Alex Tomasc9de5602008-01-29 00:19:52 -05004535 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004536 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4537 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004538 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004539 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004540 goto error_return;
4541 }
4542
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004543 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004544 trace_ext4_free_blocks(inode, block, count, flags);
4545
4546 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4547 struct buffer_head *tbh = bh;
4548 int i;
4549
4550 BUG_ON(bh && (count > 1));
4551
4552 for (i = 0; i < count; i++) {
4553 if (!bh)
4554 tbh = sb_find_get_block(inode->i_sb,
4555 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004556 if (unlikely(!tbh))
4557 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004558 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004559 inode, tbh, block + i);
4560 }
4561 }
4562
Theodore Ts'o60e66792010-05-17 07:00:00 -04004563 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004564 * We need to make sure we don't reuse the freed block until
4565 * after the transaction is committed, which we can do by
4566 * treating the block as metadata, below. We make an
4567 * exception if the inode is to be written in writeback mode
4568 * since writeback mode has weak data consistency guarantees.
4569 */
4570 if (!ext4_should_writeback_data(inode))
4571 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004572
Alex Tomasc9de5602008-01-29 00:19:52 -05004573do_more:
4574 overflow = 0;
4575 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4576
4577 /*
4578 * Check to see if we are freeing blocks across a group
4579 * boundary.
4580 */
4581 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4582 overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4583 count -= overflow;
4584 }
Theodore Ts'o574ca172008-07-11 19:27:31 -04004585 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004586 if (!bitmap_bh) {
4587 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004588 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004589 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004590 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004591 if (!gdp) {
4592 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004593 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004594 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004595
4596 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4597 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4598 in_range(block, ext4_inode_table(sb, gdp),
4599 EXT4_SB(sb)->s_itb_per_group) ||
4600 in_range(block + count - 1, ext4_inode_table(sb, gdp),
4601 EXT4_SB(sb)->s_itb_per_group)) {
4602
Eric Sandeen12062dd2010-02-15 14:19:27 -05004603 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004604 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004605 /* err = 0. ext4_std_error should be a no op */
4606 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004607 }
4608
4609 BUFFER_TRACE(bitmap_bh, "getting write access");
4610 err = ext4_journal_get_write_access(handle, bitmap_bh);
4611 if (err)
4612 goto error_return;
4613
4614 /*
4615 * We are about to modify some metadata. Call the journal APIs
4616 * to unshare ->b_data if a currently-committing transaction is
4617 * using it
4618 */
4619 BUFFER_TRACE(gd_bh, "get_write_access");
4620 err = ext4_journal_get_write_access(handle, gd_bh);
4621 if (err)
4622 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004623#ifdef AGGRESSIVE_CHECK
4624 {
4625 int i;
4626 for (i = 0; i < count; i++)
4627 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4628 }
4629#endif
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004630 trace_ext4_mballoc_free(sb, inode, block_group, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004631
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004632 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4633 if (err)
4634 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004635
4636 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004637 struct ext4_free_data *new_entry;
4638 /*
4639 * blocks being freed are metadata. these blocks shouldn't
4640 * be used until this transaction is committed
4641 */
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004642 new_entry = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
4643 if (!new_entry) {
4644 err = -ENOMEM;
4645 goto error_return;
4646 }
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004647 new_entry->start_blk = bit;
4648 new_entry->group = block_group;
4649 new_entry->count = count;
4650 new_entry->t_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004651
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004652 ext4_lock_group(sb, block_group);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004653 mb_clear_bits(bitmap_bh->b_data, bit, count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004654 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004655 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004656 /* need to update group_info->bb_free and bitmap
4657 * with group lock held. generate_buddy look at
4658 * them with group lock_held
4659 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004660 ext4_lock_group(sb, block_group);
4661 mb_clear_bits(bitmap_bh->b_data, bit, count);
Shen Feng7e5a8cd2008-07-13 21:03:31 -04004662 mb_free_blocks(inode, &e4b, bit, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004663 }
4664
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004665 ret = ext4_free_blks_count(sb, gdp) + count;
4666 ext4_free_blks_set(sb, gdp, ret);
Alex Tomasc9de5602008-01-29 00:19:52 -05004667 gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004668 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004669 percpu_counter_add(&sbi->s_freeblocks_counter, count);
4670
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004671 if (sbi->s_log_groups_per_flex) {
4672 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05004673 atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004674 }
4675
Jing Zhange39e07f2010-05-14 00:00:00 -04004676 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004677
Theodore Ts'o44338712009-11-22 07:44:56 -05004678 freed += count;
Alex Tomasc9de5602008-01-29 00:19:52 -05004679
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004680 /* We dirtied the bitmap block */
4681 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4682 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4683
Alex Tomasc9de5602008-01-29 00:19:52 -05004684 /* And the group descriptor block */
4685 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004686 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004687 if (!err)
4688 err = ret;
4689
4690 if (overflow && !err) {
4691 block += count;
4692 count = overflow;
4693 put_bh(bitmap_bh);
4694 goto do_more;
4695 }
Theodore Ts'oa0375152010-06-11 23:14:04 -04004696 ext4_mark_super_dirty(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004697error_return:
Theodore Ts'o44338712009-11-22 07:44:56 -05004698 if (freed)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004699 dquot_free_block(inode, freed);
Alex Tomasc9de5602008-01-29 00:19:52 -05004700 brelse(bitmap_bh);
4701 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004702 return;
4703}
Lukas Czerner7360d172010-10-27 21:30:12 -04004704
4705/**
4706 * ext4_trim_extent -- function to TRIM one single free extent in the group
4707 * @sb: super block for the file system
4708 * @start: starting block of the free extent in the alloc. group
4709 * @count: number of blocks to TRIM
4710 * @group: alloc. group we are working with
4711 * @e4b: ext4 buddy for the group
4712 *
4713 * Trim "count" blocks starting at "start" in the "group". To assure that no
4714 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4715 * be called with under the group lock.
4716 */
4717static int ext4_trim_extent(struct super_block *sb, int start, int count,
4718 ext4_group_t group, struct ext4_buddy *e4b)
4719{
4720 struct ext4_free_extent ex;
4721 int ret = 0;
4722
4723 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4724
4725 ex.fe_start = start;
4726 ex.fe_group = group;
4727 ex.fe_len = count;
4728
4729 /*
4730 * Mark blocks used, so no one can reuse them while
4731 * being trimmed.
4732 */
4733 mb_mark_used(e4b, &ex);
4734 ext4_unlock_group(sb, group);
4735
4736 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004737
4738 ext4_lock_group(sb, group);
4739 mb_free_blocks(NULL, e4b, start, ex.fe_len);
4740 return ret;
4741}
4742
4743/**
4744 * ext4_trim_all_free -- function to trim all free space in alloc. group
4745 * @sb: super block for file system
4746 * @e4b: ext4 buddy
4747 * @start: first group block to examine
4748 * @max: last group block to examine
4749 * @minblocks: minimum extent block count
4750 *
4751 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4752 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4753 * the extent.
4754 *
4755 *
4756 * ext4_trim_all_free walks through group's block bitmap searching for free
4757 * extents. When the free extent is found, mark it as used in group buddy
4758 * bitmap. Then issue a TRIM command on this extent and free the extent in
4759 * the group buddy bitmap. This is done until whole group is scanned.
4760 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004761static ext4_grpblk_t
4762ext4_trim_all_free(struct super_block *sb, struct ext4_buddy *e4b,
Lukas Czerner7360d172010-10-27 21:30:12 -04004763 ext4_grpblk_t start, ext4_grpblk_t max, ext4_grpblk_t minblocks)
4764{
4765 void *bitmap;
4766 ext4_grpblk_t next, count = 0;
4767 ext4_group_t group;
4768 int ret = 0;
4769
4770 BUG_ON(e4b == NULL);
4771
4772 bitmap = e4b->bd_bitmap;
4773 group = e4b->bd_group;
4774 start = (e4b->bd_info->bb_first_free > start) ?
4775 e4b->bd_info->bb_first_free : start;
4776 ext4_lock_group(sb, group);
4777
4778 while (start < max) {
4779 start = mb_find_next_zero_bit(bitmap, max, start);
4780 if (start >= max)
4781 break;
4782 next = mb_find_next_bit(bitmap, max, start);
4783
4784 if ((next - start) >= minblocks) {
4785 ret = ext4_trim_extent(sb, start,
4786 next - start, group, e4b);
4787 if (ret < 0)
4788 break;
4789 count += next - start;
4790 }
4791 start = next + 1;
4792
4793 if (fatal_signal_pending(current)) {
4794 count = -ERESTARTSYS;
4795 break;
4796 }
4797
4798 if (need_resched()) {
4799 ext4_unlock_group(sb, group);
4800 cond_resched();
4801 ext4_lock_group(sb, group);
4802 }
4803
4804 if ((e4b->bd_info->bb_free - count) < minblocks)
4805 break;
4806 }
4807 ext4_unlock_group(sb, group);
4808
4809 ext4_debug("trimmed %d blocks in the group %d\n",
4810 count, group);
4811
4812 if (ret < 0)
4813 count = ret;
4814
4815 return count;
4816}
4817
4818/**
4819 * ext4_trim_fs() -- trim ioctl handle function
4820 * @sb: superblock for filesystem
4821 * @range: fstrim_range structure
4822 *
4823 * start: First Byte to trim
4824 * len: number of Bytes to trim from start
4825 * minlen: minimum extent length in Bytes
4826 * ext4_trim_fs goes through all allocation groups containing Bytes from
4827 * start to start+len. For each such a group ext4_trim_all_free function
4828 * is invoked to trim all free space.
4829 */
4830int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4831{
4832 struct ext4_buddy e4b;
4833 ext4_group_t first_group, last_group;
4834 ext4_group_t group, ngroups = ext4_get_groups_count(sb);
4835 ext4_grpblk_t cnt = 0, first_block, last_block;
4836 uint64_t start, len, minlen, trimmed;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004837 ext4_fsblk_t first_data_blk =
4838 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner7360d172010-10-27 21:30:12 -04004839 int ret = 0;
4840
4841 start = range->start >> sb->s_blocksize_bits;
4842 len = range->len >> sb->s_blocksize_bits;
4843 minlen = range->minlen >> sb->s_blocksize_bits;
4844 trimmed = 0;
4845
4846 if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
4847 return -EINVAL;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004848 if (start < first_data_blk) {
4849 len -= first_data_blk - start;
4850 start = first_data_blk;
4851 }
Lukas Czerner7360d172010-10-27 21:30:12 -04004852
4853 /* Determine first and last group to examine based on start and len */
4854 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
4855 &first_group, &first_block);
4856 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) (start + len),
4857 &last_group, &last_block);
4858 last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
4859 last_block = EXT4_BLOCKS_PER_GROUP(sb);
4860
4861 if (first_group > last_group)
4862 return -EINVAL;
4863
4864 for (group = first_group; group <= last_group; group++) {
4865 ret = ext4_mb_load_buddy(sb, group, &e4b);
4866 if (ret) {
4867 ext4_error(sb, "Error in loading buddy "
4868 "information for %u", group);
4869 break;
4870 }
4871
4872 if (len >= EXT4_BLOCKS_PER_GROUP(sb))
4873 len -= (EXT4_BLOCKS_PER_GROUP(sb) - first_block);
4874 else
Jan Karaca6e9092011-01-10 12:30:39 -05004875 last_block = first_block + len;
Lukas Czerner7360d172010-10-27 21:30:12 -04004876
4877 if (e4b.bd_info->bb_free >= minlen) {
4878 cnt = ext4_trim_all_free(sb, &e4b, first_block,
4879 last_block, minlen);
4880 if (cnt < 0) {
4881 ret = cnt;
4882 ext4_mb_unload_buddy(&e4b);
4883 break;
4884 }
4885 }
4886 ext4_mb_unload_buddy(&e4b);
4887 trimmed += cnt;
4888 first_block = 0;
4889 }
4890 range->len = trimmed * sb->s_blocksize;
4891
4892 return ret;
4893}