blob: 36c82a39d03fca9c9ad06176de40e05387875b76 [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
Bobi Jam18aadd42012-02-20 17:53:02 -050024#include "ext4_jbd2.h"
Mingming Cao8f6e39a2008-04-29 22:01:31 -040025#include "mballoc.h"
Theodore Ts'o28623c22012-09-05 01:31:50 -040026#include <linux/log2.h>
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050027#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040029#include <trace/events/ext4.h>
30
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050031#ifdef CONFIG_EXT4_DEBUG
32ushort ext4_mballoc_debug __read_mostly;
33
34module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
35MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
36#endif
37
Alex Tomasc9de5602008-01-29 00:19:52 -050038/*
39 * MUSTDO:
40 * - test ext4_ext_search_left() and ext4_ext_search_right()
41 * - search for metadata in few groups
42 *
43 * TODO v4:
44 * - normalization should take into account whether file is still open
45 * - discard preallocations if no free space left (policy?)
46 * - don't normalize tails
47 * - quota
48 * - reservation for superuser
49 *
50 * TODO v3:
51 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
52 * - track min/max extents in each group for better group selection
53 * - mb_mark_used() may allocate chunk right after splitting buddy
54 * - tree of groups sorted by number of free blocks
55 * - error handling
56 */
57
58/*
59 * The allocation request involve request for multiple number of blocks
60 * near to the goal(block) value specified.
61 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040062 * During initialization phase of the allocator we decide to use the
63 * group preallocation or inode preallocation depending on the size of
64 * the file. The size of the file could be the resulting file size we
65 * would have after allocation, or the current file size, which ever
66 * is larger. If the size is less than sbi->s_mb_stream_request we
67 * select to use the group preallocation. The default value of
68 * s_mb_stream_request is 16 blocks. This can also be tuned via
69 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
70 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050071 *
72 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040073 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050074 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040075 * First stage the allocator looks at the inode prealloc list,
76 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
77 * spaces for this particular inode. The inode prealloc space is
78 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050079 *
80 * pa_lstart -> the logical start block for this prealloc space
81 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040082 * pa_len -> length for this prealloc space (in clusters)
83 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050084 *
85 * The inode preallocation space is used looking at the _logical_ start
86 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040087 * space we will consume the particular prealloc space. This makes sure that
88 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050089 *
90 * The important thing to be noted in case of inode prealloc space is that
91 * we don't modify the values associated to inode prealloc space except
92 * pa_free.
93 *
94 * If we are not able to find blocks in the inode prealloc space and if we
95 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040096 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050097 *
98 * ext4_sb_info.s_locality_groups[smp_processor_id()]
99 *
100 * The reason for having a per cpu locality group is to reduce the contention
101 * between CPUs. It is possible to get scheduled at this point.
102 *
103 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300104 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -0500105 *
106 * If we can't allocate blocks via inode prealloc or/and locality group
107 * prealloc then we look at the buddy cache. The buddy cache is represented
108 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
109 * mapped to the buddy and bitmap information regarding different
110 * groups. The buddy information is attached to buddy cache inode so that
111 * we can access them through the page cache. The information regarding
112 * each group is loaded via ext4_mb_load_buddy. The information involve
113 * block bitmap and buddy information. The information are stored in the
114 * inode as:
115 *
116 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500117 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500118 *
119 *
120 * one block each for bitmap and buddy information. So for each group we
121 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
122 * blocksize) blocks. So it can have information regarding groups_per_page
123 * which is blocks_per_page/2
124 *
125 * The buddy cache inode is not stored on disk. The inode is thrown
126 * away when the filesystem is unmounted.
127 *
128 * We look for count number of blocks in the buddy cache. If we were able
129 * to locate that many free blocks we return with additional information
130 * regarding rest of the contiguous physical block available
131 *
132 * Before allocating blocks via buddy cache we normalize the request
133 * blocks. This ensure we ask for more blocks that we needed. The extra
134 * blocks that we get after allocation is added to the respective prealloc
135 * list. In case of inode preallocation we follow a list of heuristics
136 * based on file size. This can be found in ext4_mb_normalize_request. If
137 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400138 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
139 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500140 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400141 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500142 * terms of number of blocks. If we have mounted the file system with -O
143 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400144 * the smallest multiple of the stripe value (sbi->s_stripe) which is
145 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500146 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400147 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500148 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400149 * /sys/fs/ext4/<partition>/mb_min_to_scan
150 * /sys/fs/ext4/<partition>/mb_max_to_scan
151 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500152 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400153 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500154 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
155 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400156 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200157 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400158 * stripe size. This should result in better allocation on RAID setups. If
159 * not, we search in the specific group using bitmap for best extents. The
160 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500161 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400162 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500163 * best extent in the found extents. Searching for the blocks starts with
164 * the group specified as the goal value in allocation context via
165 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400166 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500167 * checked.
168 *
169 * Both the prealloc space are getting populated as above. So for the first
170 * request we will hit the buddy cache which will result in this prealloc
171 * space getting filled. The prealloc space is then later used for the
172 * subsequent request.
173 */
174
175/*
176 * mballoc operates on the following data:
177 * - on-disk bitmap
178 * - in-core buddy (actually includes buddy and bitmap)
179 * - preallocation descriptors (PAs)
180 *
181 * there are two types of preallocations:
182 * - inode
183 * assiged to specific inode and can be used for this inode only.
184 * it describes part of inode's space preallocated to specific
185 * physical blocks. any block from that preallocated can be used
186 * independent. the descriptor just tracks number of blocks left
187 * unused. so, before taking some block from descriptor, one must
188 * make sure corresponded logical block isn't allocated yet. this
189 * also means that freeing any block within descriptor's range
190 * must discard all preallocated blocks.
191 * - locality group
192 * assigned to specific locality group which does not translate to
193 * permanent set of inodes: inode can join and leave group. space
194 * from this type of preallocation can be used for any inode. thus
195 * it's consumed from the beginning to the end.
196 *
197 * relation between them can be expressed as:
198 * in-core buddy = on-disk bitmap + preallocation descriptors
199 *
200 * this mean blocks mballoc considers used are:
201 * - allocated blocks (persistent)
202 * - preallocated blocks (non-persistent)
203 *
204 * consistency in mballoc world means that at any time a block is either
205 * free or used in ALL structures. notice: "any time" should not be read
206 * literally -- time is discrete and delimited by locks.
207 *
208 * to keep it simple, we don't use block numbers, instead we count number of
209 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
210 *
211 * all operations can be expressed as:
212 * - init buddy: buddy = on-disk + PAs
213 * - new PA: buddy += N; PA = N
214 * - use inode PA: on-disk += N; PA -= N
215 * - discard inode PA buddy -= on-disk - PA; PA = 0
216 * - use locality group PA on-disk += N; PA -= N
217 * - discard locality group PA buddy -= PA; PA = 0
218 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
219 * is used in real operation because we can't know actual used
220 * bits from PA, only from on-disk bitmap
221 *
222 * if we follow this strict logic, then all operations above should be atomic.
223 * given some of them can block, we'd have to use something like semaphores
224 * killing performance on high-end SMP hardware. let's try to relax it using
225 * the following knowledge:
226 * 1) if buddy is referenced, it's already initialized
227 * 2) while block is used in buddy and the buddy is referenced,
228 * nobody can re-allocate that block
229 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
230 * bit set and PA claims same block, it's OK. IOW, one can set bit in
231 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
232 * block
233 *
234 * so, now we're building a concurrency table:
235 * - init buddy vs.
236 * - new PA
237 * blocks for PA are allocated in the buddy, buddy must be referenced
238 * until PA is linked to allocation group to avoid concurrent buddy init
239 * - use inode PA
240 * we need to make sure that either on-disk bitmap or PA has uptodate data
241 * given (3) we care that PA-=N operation doesn't interfere with init
242 * - discard inode PA
243 * the simplest way would be to have buddy initialized by the discard
244 * - use locality group PA
245 * again PA-=N must be serialized with init
246 * - discard locality group PA
247 * the simplest way would be to have buddy initialized by the discard
248 * - new PA vs.
249 * - use inode PA
250 * i_data_sem serializes them
251 * - discard inode PA
252 * discard process must wait until PA isn't used by another process
253 * - use locality group PA
254 * some mutex should serialize them
255 * - discard locality group PA
256 * discard process must wait until PA isn't used by another process
257 * - use inode PA
258 * - use inode PA
259 * i_data_sem or another mutex should serializes them
260 * - discard inode PA
261 * discard process must wait until PA isn't used by another process
262 * - use locality group PA
263 * nothing wrong here -- they're different PAs covering different blocks
264 * - discard locality group PA
265 * discard process must wait until PA isn't used by another process
266 *
267 * now we're ready to make few consequences:
268 * - PA is referenced and while it is no discard is possible
269 * - PA is referenced until block isn't marked in on-disk bitmap
270 * - PA changes only after on-disk bitmap
271 * - discard must not compete with init. either init is done before
272 * any discard or they're serialized somehow
273 * - buddy init as sum of on-disk bitmap and PAs is done atomically
274 *
275 * a special case when we've used PA to emptiness. no need to modify buddy
276 * in this case, but we should care about concurrent init
277 *
278 */
279
280 /*
281 * Logic in few words:
282 *
283 * - allocation:
284 * load group
285 * find blocks
286 * mark bits in on-disk bitmap
287 * release group
288 *
289 * - use preallocation:
290 * find proper PA (per-inode or group)
291 * load group
292 * mark bits in on-disk bitmap
293 * release group
294 * release PA
295 *
296 * - free:
297 * load group
298 * mark bits in on-disk bitmap
299 * release group
300 *
301 * - discard preallocations in group:
302 * mark PAs deleted
303 * move them onto local list
304 * load on-disk bitmap
305 * load group
306 * remove PA from object (inode or locality group)
307 * mark free blocks in-core
308 *
309 * - discard inode's preallocations:
310 */
311
312/*
313 * Locking rules
314 *
315 * Locks:
316 * - bitlock on a group (group)
317 * - object (inode/locality) (object)
318 * - per-pa lock (pa)
319 *
320 * Paths:
321 * - new pa
322 * object
323 * group
324 *
325 * - find and use pa:
326 * pa
327 *
328 * - release consumed pa:
329 * pa
330 * group
331 * object
332 *
333 * - generate in-core bitmap:
334 * group
335 * pa
336 *
337 * - discard all for given object (inode, locality group):
338 * object
339 * pa
340 * group
341 *
342 * - discard all for given group:
343 * group
344 * pa
345 * group
346 * object
347 *
348 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500349static struct kmem_cache *ext4_pspace_cachep;
350static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500351static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400352
353/* We create slab caches for groupinfo data structures based on the
354 * superblock block size. There will be one per mounted filesystem for
355 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500356#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400357static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
358
Eric Sandeen2892c152011-02-12 08:12:18 -0500359static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
360 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
361 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
362 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
363};
364
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500365static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
366 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500367static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
368 ext4_group_t group);
Bobi Jam18aadd42012-02-20 17:53:02 -0500369static void ext4_free_data_callback(struct super_block *sb,
370 struct ext4_journal_cb_entry *jce, int rc);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500371
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500372static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
373{
Alex Tomasc9de5602008-01-29 00:19:52 -0500374#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500375 *bit += ((unsigned long) addr & 7UL) << 3;
376 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500377#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500378 *bit += ((unsigned long) addr & 3UL) << 3;
379 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500380#else
381#error "how many bits you are?!"
382#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500383 return addr;
384}
Alex Tomasc9de5602008-01-29 00:19:52 -0500385
386static inline int mb_test_bit(int bit, void *addr)
387{
388 /*
389 * ext4_test_bit on architecture like powerpc
390 * needs unsigned long aligned address
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 return ext4_test_bit(bit, addr);
394}
395
396static inline void mb_set_bit(int bit, void *addr)
397{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500398 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500399 ext4_set_bit(bit, addr);
400}
401
Alex Tomasc9de5602008-01-29 00:19:52 -0500402static inline void mb_clear_bit(int bit, void *addr)
403{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500404 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500405 ext4_clear_bit(bit, addr);
406}
407
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500408static inline int mb_find_next_zero_bit(void *addr, int max, int start)
409{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400410 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500411 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400412 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500413 start += fix;
414
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400415 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
416 if (ret > max)
417 return max;
418 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500419}
420
421static inline int mb_find_next_bit(void *addr, int max, int start)
422{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400423 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500424 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400425 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500426 start += fix;
427
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400428 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
429 if (ret > max)
430 return max;
431 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500432}
433
Alex Tomasc9de5602008-01-29 00:19:52 -0500434static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
435{
436 char *bb;
437
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500438 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500439 BUG_ON(max == NULL);
440
441 if (order > e4b->bd_blkbits + 1) {
442 *max = 0;
443 return NULL;
444 }
445
446 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500447 if (order == 0) {
448 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500449 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500450 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500451
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500452 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500453 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
454
455 return bb;
456}
457
458#ifdef DOUBLE_CHECK
459static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
460 int first, int count)
461{
462 int i;
463 struct super_block *sb = e4b->bd_sb;
464
465 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
466 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400467 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500468 for (i = 0; i < count; i++) {
469 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
470 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500471
472 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400473 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500474 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400475 inode ? inode->i_ino : 0,
476 blocknr,
477 "freeing block already freed "
478 "(bit %u)",
479 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500480 }
481 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
482 }
483}
484
485static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
486{
487 int i;
488
489 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
490 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400491 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500492 for (i = 0; i < count; i++) {
493 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
494 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
495 }
496}
497
498static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
499{
500 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
501 unsigned char *b1, *b2;
502 int i;
503 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
504 b2 = (unsigned char *) bitmap;
505 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
506 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400507 ext4_msg(e4b->bd_sb, KERN_ERR,
508 "corruption in group %u "
509 "at byte %u(%u): %x in copy != %x "
510 "on disk/prealloc",
511 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500512 BUG();
513 }
514 }
515 }
516}
517
518#else
519static inline void mb_free_blocks_double(struct inode *inode,
520 struct ext4_buddy *e4b, int first, int count)
521{
522 return;
523}
524static inline void mb_mark_used_double(struct ext4_buddy *e4b,
525 int first, int count)
526{
527 return;
528}
529static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
530{
531 return;
532}
533#endif
534
535#ifdef AGGRESSIVE_CHECK
536
537#define MB_CHECK_ASSERT(assert) \
538do { \
539 if (!(assert)) { \
540 printk(KERN_EMERG \
541 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
542 function, file, line, # assert); \
543 BUG(); \
544 } \
545} while (0)
546
547static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
548 const char *function, int line)
549{
550 struct super_block *sb = e4b->bd_sb;
551 int order = e4b->bd_blkbits + 1;
552 int max;
553 int max2;
554 int i;
555 int j;
556 int k;
557 int count;
558 struct ext4_group_info *grp;
559 int fragments = 0;
560 int fstart;
561 struct list_head *cur;
562 void *buddy;
563 void *buddy2;
564
Alex Tomasc9de5602008-01-29 00:19:52 -0500565 {
566 static int mb_check_counter;
567 if (mb_check_counter++ % 100 != 0)
568 return 0;
569 }
570
571 while (order > 1) {
572 buddy = mb_find_buddy(e4b, order, &max);
573 MB_CHECK_ASSERT(buddy);
574 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
575 MB_CHECK_ASSERT(buddy2);
576 MB_CHECK_ASSERT(buddy != buddy2);
577 MB_CHECK_ASSERT(max * 2 == max2);
578
579 count = 0;
580 for (i = 0; i < max; i++) {
581
582 if (mb_test_bit(i, buddy)) {
583 /* only single bit in buddy2 may be 1 */
584 if (!mb_test_bit(i << 1, buddy2)) {
585 MB_CHECK_ASSERT(
586 mb_test_bit((i<<1)+1, buddy2));
587 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
588 MB_CHECK_ASSERT(
589 mb_test_bit(i << 1, buddy2));
590 }
591 continue;
592 }
593
Robin Dong0a10da72011-10-26 08:48:54 -0400594 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500595 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
596 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
597
598 for (j = 0; j < (1 << order); j++) {
599 k = (i * (1 << order)) + j;
600 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500601 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500602 }
603 count++;
604 }
605 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
606 order--;
607 }
608
609 fstart = -1;
610 buddy = mb_find_buddy(e4b, 0, &max);
611 for (i = 0; i < max; i++) {
612 if (!mb_test_bit(i, buddy)) {
613 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
614 if (fstart == -1) {
615 fragments++;
616 fstart = i;
617 }
618 continue;
619 }
620 fstart = -1;
621 /* check used bits only */
622 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
623 buddy2 = mb_find_buddy(e4b, j, &max2);
624 k = i >> j;
625 MB_CHECK_ASSERT(k < max2);
626 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
627 }
628 }
629 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
630 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
631
632 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500633 list_for_each(cur, &grp->bb_prealloc_list) {
634 ext4_group_t groupnr;
635 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400636 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
637 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500638 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400639 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500640 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
641 }
642 return 0;
643}
644#undef MB_CHECK_ASSERT
645#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400646 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500647#else
648#define mb_check_buddy(e4b)
649#endif
650
Coly Li7c786052011-02-24 13:24:25 -0500651/*
652 * Divide blocks started from @first with length @len into
653 * smaller chunks with power of 2 blocks.
654 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
655 * then increase bb_counters[] for corresponded chunk size.
656 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500657static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400658 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500659 struct ext4_group_info *grp)
660{
661 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400662 ext4_grpblk_t min;
663 ext4_grpblk_t max;
664 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500665 unsigned short border;
666
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400667 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500668
669 border = 2 << sb->s_blocksize_bits;
670
671 while (len > 0) {
672 /* find how many blocks can be covered since this position */
673 max = ffs(first | border) - 1;
674
675 /* find how many blocks of power 2 we need to mark */
676 min = fls(len) - 1;
677
678 if (max < min)
679 min = max;
680 chunk = 1 << min;
681
682 /* mark multiblock chunks only */
683 grp->bb_counters[min]++;
684 if (min > 0)
685 mb_clear_bit(first >> min,
686 buddy + sbi->s_mb_offsets[min]);
687
688 len -= chunk;
689 first += chunk;
690 }
691}
692
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400693/*
694 * Cache the order of the largest free extent we have available in this block
695 * group.
696 */
697static void
698mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
699{
700 int i;
701 int bits;
702
703 grp->bb_largest_free_order = -1; /* uninit */
704
705 bits = sb->s_blocksize_bits + 1;
706 for (i = bits; i >= 0; i--) {
707 if (grp->bb_counters[i] > 0) {
708 grp->bb_largest_free_order = i;
709 break;
710 }
711 }
712}
713
Eric Sandeen089ceec2009-07-05 22:17:31 -0400714static noinline_for_stack
715void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500716 void *buddy, void *bitmap, ext4_group_t group)
717{
718 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400719 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400720 ext4_grpblk_t i = 0;
721 ext4_grpblk_t first;
722 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500723 unsigned free = 0;
724 unsigned fragments = 0;
725 unsigned long long period = get_cycles();
726
727 /* initialize buddy from bitmap which is aggregation
728 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500729 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 grp->bb_first_free = i;
731 while (i < max) {
732 fragments++;
733 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500734 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500735 len = i - first;
736 free += len;
737 if (len > 1)
738 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
739 else
740 grp->bb_counters[0]++;
741 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500742 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500743 }
744 grp->bb_fragments = fragments;
745
746 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400747 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400748 "%u clusters in bitmap, %u in gd",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400749 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500750 /*
751 * If we intent to continue, we consider group descritor
752 * corrupt and update bb_free using bitmap value
753 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500754 grp->bb_free = free;
755 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400756 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500757
758 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
759
760 period = get_cycles() - period;
761 spin_lock(&EXT4_SB(sb)->s_bal_lock);
762 EXT4_SB(sb)->s_mb_buddies_generated++;
763 EXT4_SB(sb)->s_mb_generation_time += period;
764 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
765}
766
767/* The buddy information is attached the buddy cache inode
768 * for convenience. The information regarding each group
769 * is loaded via ext4_mb_load_buddy. The information involve
770 * block bitmap and buddy information. The information are
771 * stored in the inode as
772 *
773 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500774 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500775 *
776 *
777 * one block each for bitmap and buddy information.
778 * So for each group we take up 2 blocks. A page can
779 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
780 * So it can have information regarding groups_per_page which
781 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400782 *
783 * Locking note: This routine takes the block group lock of all groups
784 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500785 */
786
787static int ext4_mb_init_cache(struct page *page, char *incore)
788{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400789 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500790 int blocksize;
791 int blocks_per_page;
792 int groups_per_page;
793 int err = 0;
794 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500795 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500796 int first_block;
797 struct super_block *sb;
798 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400799 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500800 struct inode *inode;
801 char *data;
802 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400803 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500804
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400805 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500806
807 inode = page->mapping->host;
808 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400809 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500810 blocksize = 1 << inode->i_blkbits;
811 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
812
813 groups_per_page = blocks_per_page >> 1;
814 if (groups_per_page == 0)
815 groups_per_page = 1;
816
817 /* allocate buffer_heads to read bitmaps */
818 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500819 i = sizeof(struct buffer_head *) * groups_per_page;
820 bh = kzalloc(i, GFP_NOFS);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500821 if (bh == NULL) {
822 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500823 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500824 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500825 } else
826 bh = &bhs;
827
828 first_group = page->index * blocks_per_page / 2;
829
830 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500831 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
832 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500833 break;
834
Theodore Ts'o813e5722012-02-20 17:52:46 -0500835 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400836 /*
837 * If page is uptodate then we came here after online resize
838 * which added some new uninitialized group info structs, so
839 * we must skip all initialized uptodate buddies on the page,
840 * which may be currently in use by an allocating task.
841 */
842 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
843 bh[i] = NULL;
844 continue;
845 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500846 if (!(bh[i] = ext4_read_block_bitmap_nowait(sb, group))) {
847 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500848 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500849 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500850 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500851 }
852
853 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500854 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
855 if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
856 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500857 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500858 }
859 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500860
861 first_block = page->index * blocks_per_page;
862 for (i = 0; i < blocks_per_page; i++) {
863 int group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500864
865 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400866 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500867 break;
868
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400869 if (!bh[group - first_group])
870 /* skip initialized uptodate buddy */
871 continue;
872
Alex Tomasc9de5602008-01-29 00:19:52 -0500873 /*
874 * data carry information regarding this
875 * particular group in the format specified
876 * above
877 *
878 */
879 data = page_address(page) + (i * blocksize);
880 bitmap = bh[group - first_group]->b_data;
881
882 /*
883 * We place the buddy block and bitmap block
884 * close together
885 */
886 if ((first_block + i) & 1) {
887 /* this is block of buddy */
888 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400889 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500890 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400891 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500892 grinfo = ext4_get_group_info(sb, group);
893 grinfo->bb_fragments = 0;
894 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400895 sizeof(*grinfo->bb_counters) *
896 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 /*
898 * incore got set to the group block bitmap below
899 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500900 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400901 /* init the buddy */
902 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500903 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500904 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500905 incore = NULL;
906 } else {
907 /* this is block of bitmap */
908 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400909 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500910 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400911 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500912
913 /* see comments in ext4_mb_put_pa() */
914 ext4_lock_group(sb, group);
915 memcpy(data, bitmap, blocksize);
916
917 /* mark all preallocated blks used in in-core bitmap */
918 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500919 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500920 ext4_unlock_group(sb, group);
921
922 /* set incore so that the buddy information can be
923 * generated using this
924 */
925 incore = data;
926 }
927 }
928 SetPageUptodate(page);
929
930out:
931 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400932 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500933 brelse(bh[i]);
934 if (bh != &bhs)
935 kfree(bh);
936 }
937 return err;
938}
939
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400940/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400941 * Lock the buddy and bitmap pages. This make sure other parallel init_group
942 * on the same buddy page doesn't happen whild holding the buddy page lock.
943 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
944 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400945 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400946static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
947 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400948{
Amir Goldstein2de88072011-05-09 21:48:13 -0400949 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
950 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400951 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400952 struct page *page;
953
954 e4b->bd_buddy_page = NULL;
955 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400956
957 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
958 /*
959 * the buddy cache inode stores the block bitmap
960 * and buddy information in consecutive blocks.
961 * So for each group we need two blocks.
962 */
963 block = group * 2;
964 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400965 poff = block % blocks_per_page;
966 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
967 if (!page)
968 return -EIO;
969 BUG_ON(page->mapping != inode->i_mapping);
970 e4b->bd_bitmap_page = page;
971 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400972
Amir Goldstein2de88072011-05-09 21:48:13 -0400973 if (blocks_per_page >= 2) {
974 /* buddy and bitmap are on the same page */
975 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400976 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400977
978 block++;
979 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400980 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
981 if (!page)
982 return -EIO;
983 BUG_ON(page->mapping != inode->i_mapping);
984 e4b->bd_buddy_page = page;
985 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400986}
987
Amir Goldstein2de88072011-05-09 21:48:13 -0400988static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400989{
Amir Goldstein2de88072011-05-09 21:48:13 -0400990 if (e4b->bd_bitmap_page) {
991 unlock_page(e4b->bd_bitmap_page);
992 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400993 }
Amir Goldstein2de88072011-05-09 21:48:13 -0400994 if (e4b->bd_buddy_page) {
995 unlock_page(e4b->bd_buddy_page);
996 page_cache_release(e4b->bd_buddy_page);
997 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400998}
999
1000/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001001 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1002 * block group lock of all groups for this page; do not hold the BG lock when
1003 * calling this routine!
1004 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001005static noinline_for_stack
1006int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1007{
1008
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001009 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001010 struct ext4_buddy e4b;
1011 struct page *page;
1012 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001013
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001014 might_sleep();
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001015 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001016 this_grp = ext4_get_group_info(sb, group);
1017 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001018 * This ensures that we don't reinit the buddy cache
1019 * page which map to the group from which we are already
1020 * allocating. If we are looking at the buddy cache we would
1021 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001022 * would have pinned buddy page to page cache.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001023 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001024 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1025 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001026 /*
1027 * somebody initialized the group
1028 * return without doing anything
1029 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001030 goto err;
1031 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001032
1033 page = e4b.bd_bitmap_page;
1034 ret = ext4_mb_init_cache(page, NULL);
1035 if (ret)
1036 goto err;
1037 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001038 ret = -EIO;
1039 goto err;
1040 }
1041 mark_page_accessed(page);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001042
Amir Goldstein2de88072011-05-09 21:48:13 -04001043 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001044 /*
1045 * If both the bitmap and buddy are in
1046 * the same page we don't need to force
1047 * init the buddy
1048 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001049 ret = 0;
1050 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001051 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001052 /* init buddy cache */
1053 page = e4b.bd_buddy_page;
1054 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1055 if (ret)
1056 goto err;
1057 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001058 ret = -EIO;
1059 goto err;
1060 }
1061 mark_page_accessed(page);
1062err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001063 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001064 return ret;
1065}
1066
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001067/*
1068 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1069 * block group lock of all groups for this page; do not hold the BG lock when
1070 * calling this routine!
1071 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001072static noinline_for_stack int
1073ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1074 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001075{
Alex Tomasc9de5602008-01-29 00:19:52 -05001076 int blocks_per_page;
1077 int block;
1078 int pnum;
1079 int poff;
1080 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001081 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001082 struct ext4_group_info *grp;
1083 struct ext4_sb_info *sbi = EXT4_SB(sb);
1084 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001085
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001086 might_sleep();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001087 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001088
1089 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001090 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001091
1092 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001093 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001094 e4b->bd_sb = sb;
1095 e4b->bd_group = group;
1096 e4b->bd_buddy_page = NULL;
1097 e4b->bd_bitmap_page = NULL;
1098
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001099 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001100 /*
1101 * we need full data about the group
1102 * to make a good selection
1103 */
1104 ret = ext4_mb_init_group(sb, group);
1105 if (ret)
1106 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001107 }
1108
Alex Tomasc9de5602008-01-29 00:19:52 -05001109 /*
1110 * the buddy cache inode stores the block bitmap
1111 * and buddy information in consecutive blocks.
1112 * So for each group we need two blocks.
1113 */
1114 block = group * 2;
1115 pnum = block / blocks_per_page;
1116 poff = block % blocks_per_page;
1117
1118 /* we could use find_or_create_page(), but it locks page
1119 * what we'd like to avoid in fast path ... */
1120 page = find_get_page(inode->i_mapping, pnum);
1121 if (page == NULL || !PageUptodate(page)) {
1122 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001123 /*
1124 * drop the page reference and try
1125 * to get the page with lock. If we
1126 * are not uptodate that implies
1127 * somebody just created the page but
1128 * is yet to initialize the same. So
1129 * wait for it to initialize.
1130 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001131 page_cache_release(page);
1132 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1133 if (page) {
1134 BUG_ON(page->mapping != inode->i_mapping);
1135 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001136 ret = ext4_mb_init_cache(page, NULL);
1137 if (ret) {
1138 unlock_page(page);
1139 goto err;
1140 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001141 mb_cmp_bitmaps(e4b, page_address(page) +
1142 (poff * sb->s_blocksize));
1143 }
1144 unlock_page(page);
1145 }
1146 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001147 if (page == NULL || !PageUptodate(page)) {
1148 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001149 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001150 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001151 e4b->bd_bitmap_page = page;
1152 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1153 mark_page_accessed(page);
1154
1155 block++;
1156 pnum = block / blocks_per_page;
1157 poff = block % blocks_per_page;
1158
1159 page = find_get_page(inode->i_mapping, pnum);
1160 if (page == NULL || !PageUptodate(page)) {
1161 if (page)
1162 page_cache_release(page);
1163 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1164 if (page) {
1165 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001166 if (!PageUptodate(page)) {
1167 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1168 if (ret) {
1169 unlock_page(page);
1170 goto err;
1171 }
1172 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001173 unlock_page(page);
1174 }
1175 }
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001176 if (page == NULL || !PageUptodate(page)) {
1177 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001178 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001179 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001180 e4b->bd_buddy_page = page;
1181 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1182 mark_page_accessed(page);
1183
1184 BUG_ON(e4b->bd_bitmap_page == NULL);
1185 BUG_ON(e4b->bd_buddy_page == NULL);
1186
1187 return 0;
1188
1189err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001190 if (page)
1191 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001192 if (e4b->bd_bitmap_page)
1193 page_cache_release(e4b->bd_bitmap_page);
1194 if (e4b->bd_buddy_page)
1195 page_cache_release(e4b->bd_buddy_page);
1196 e4b->bd_buddy = NULL;
1197 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001198 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001199}
1200
Jing Zhange39e07f2010-05-14 00:00:00 -04001201static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001202{
1203 if (e4b->bd_bitmap_page)
1204 page_cache_release(e4b->bd_bitmap_page);
1205 if (e4b->bd_buddy_page)
1206 page_cache_release(e4b->bd_buddy_page);
1207}
1208
1209
1210static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1211{
1212 int order = 1;
1213 void *bb;
1214
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001215 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001216 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1217
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001218 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001219 while (order <= e4b->bd_blkbits + 1) {
1220 block = block >> 1;
1221 if (!mb_test_bit(block, bb)) {
1222 /* this block is part of buddy of order 'order' */
1223 return order;
1224 }
1225 bb += 1 << (e4b->bd_blkbits - order);
1226 order++;
1227 }
1228 return 0;
1229}
1230
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001231static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001232{
1233 __u32 *addr;
1234
1235 len = cur + len;
1236 while (cur < len) {
1237 if ((cur & 31) == 0 && (len - cur) >= 32) {
1238 /* fast path: clear whole word at once */
1239 addr = bm + (cur >> 3);
1240 *addr = 0;
1241 cur += 32;
1242 continue;
1243 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001244 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001245 cur++;
1246 }
1247}
1248
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001249void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001250{
1251 __u32 *addr;
1252
1253 len = cur + len;
1254 while (cur < len) {
1255 if ((cur & 31) == 0 && (len - cur) >= 32) {
1256 /* fast path: set whole word at once */
1257 addr = bm + (cur >> 3);
1258 *addr = 0xffffffff;
1259 cur += 32;
1260 continue;
1261 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001262 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001263 cur++;
1264 }
1265}
1266
Shen Feng7e5a8cd2008-07-13 21:03:31 -04001267static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
Alex Tomasc9de5602008-01-29 00:19:52 -05001268 int first, int count)
1269{
1270 int block = 0;
1271 int max = 0;
1272 int order;
1273 void *buddy;
1274 void *buddy2;
1275 struct super_block *sb = e4b->bd_sb;
1276
1277 BUG_ON(first + count > (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001278 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001279 mb_check_buddy(e4b);
1280 mb_free_blocks_double(inode, e4b, first, count);
1281
1282 e4b->bd_info->bb_free += count;
1283 if (first < e4b->bd_info->bb_first_free)
1284 e4b->bd_info->bb_first_free = first;
1285
1286 /* let's maintain fragments counter */
1287 if (first != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001288 block = !mb_test_bit(first - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001289 if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001290 max = !mb_test_bit(first + count, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001291 if (block && max)
1292 e4b->bd_info->bb_fragments--;
1293 else if (!block && !max)
1294 e4b->bd_info->bb_fragments++;
1295
1296 /* let's maintain buddy itself */
1297 while (count-- > 0) {
1298 block = first++;
1299 order = 0;
1300
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001301 if (!mb_test_bit(block, e4b->bd_bitmap)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001302 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -05001303
1304 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001305 blocknr += EXT4_C2B(EXT4_SB(sb), block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001306 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001307 inode ? inode->i_ino : 0,
1308 blocknr,
1309 "freeing already freed block "
1310 "(bit %u)", block);
Alex Tomasc9de5602008-01-29 00:19:52 -05001311 }
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001312 mb_clear_bit(block, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001313 e4b->bd_info->bb_counters[order]++;
1314
1315 /* start of the buddy */
1316 buddy = mb_find_buddy(e4b, order, &max);
1317
1318 do {
1319 block &= ~1UL;
1320 if (mb_test_bit(block, buddy) ||
1321 mb_test_bit(block + 1, buddy))
1322 break;
1323
1324 /* both the buddies are free, try to coalesce them */
1325 buddy2 = mb_find_buddy(e4b, order + 1, &max);
1326
1327 if (!buddy2)
1328 break;
1329
1330 if (order > 0) {
1331 /* for special purposes, we don't set
1332 * free bits in bitmap */
1333 mb_set_bit(block, buddy);
1334 mb_set_bit(block + 1, buddy);
1335 }
1336 e4b->bd_info->bb_counters[order]--;
1337 e4b->bd_info->bb_counters[order]--;
1338
1339 block = block >> 1;
1340 order++;
1341 e4b->bd_info->bb_counters[order]++;
1342
1343 mb_clear_bit(block, buddy2);
1344 buddy = buddy2;
1345 } while (1);
1346 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001347 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001348 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001349}
1350
Robin Dong15c006a2012-08-17 10:02:17 -04001351static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001352 int needed, struct ext4_free_extent *ex)
1353{
1354 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001355 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001356 void *buddy;
1357
Vincent Minetbc8e6742009-05-15 08:33:18 -04001358 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001359 BUG_ON(ex == NULL);
1360
Robin Dong15c006a2012-08-17 10:02:17 -04001361 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001362 BUG_ON(buddy == NULL);
1363 BUG_ON(block >= max);
1364 if (mb_test_bit(block, buddy)) {
1365 ex->fe_len = 0;
1366 ex->fe_start = 0;
1367 ex->fe_group = 0;
1368 return 0;
1369 }
1370
Robin Dong15c006a2012-08-17 10:02:17 -04001371 /* find actual order */
1372 order = mb_find_order_for_block(e4b, block);
1373 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001374
1375 ex->fe_len = 1 << order;
1376 ex->fe_start = block << order;
1377 ex->fe_group = e4b->bd_group;
1378
1379 /* calc difference from given start */
1380 next = next - ex->fe_start;
1381 ex->fe_len -= next;
1382 ex->fe_start += next;
1383
1384 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001385 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001386
1387 if (block + 1 >= max)
1388 break;
1389
1390 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001391 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001392 break;
1393
Robin Dongb051d8d2011-10-26 05:30:30 -04001394 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001395
Alex Tomasc9de5602008-01-29 00:19:52 -05001396 block = next >> order;
1397 ex->fe_len += 1 << order;
1398 }
1399
1400 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1401 return ex->fe_len;
1402}
1403
1404static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1405{
1406 int ord;
1407 int mlen = 0;
1408 int max = 0;
1409 int cur;
1410 int start = ex->fe_start;
1411 int len = ex->fe_len;
1412 unsigned ret = 0;
1413 int len0 = len;
1414 void *buddy;
1415
1416 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1417 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001418 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001419 mb_check_buddy(e4b);
1420 mb_mark_used_double(e4b, start, len);
1421
1422 e4b->bd_info->bb_free -= len;
1423 if (e4b->bd_info->bb_first_free == start)
1424 e4b->bd_info->bb_first_free += len;
1425
1426 /* let's maintain fragments counter */
1427 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001428 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001429 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001430 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001431 if (mlen && max)
1432 e4b->bd_info->bb_fragments++;
1433 else if (!mlen && !max)
1434 e4b->bd_info->bb_fragments--;
1435
1436 /* let's maintain buddy itself */
1437 while (len) {
1438 ord = mb_find_order_for_block(e4b, start);
1439
1440 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1441 /* the whole chunk may be allocated at once! */
1442 mlen = 1 << ord;
1443 buddy = mb_find_buddy(e4b, ord, &max);
1444 BUG_ON((start >> ord) >= max);
1445 mb_set_bit(start >> ord, buddy);
1446 e4b->bd_info->bb_counters[ord]--;
1447 start += mlen;
1448 len -= mlen;
1449 BUG_ON(len < 0);
1450 continue;
1451 }
1452
1453 /* store for history */
1454 if (ret == 0)
1455 ret = len | (ord << 16);
1456
1457 /* we have to split large buddy */
1458 BUG_ON(ord <= 0);
1459 buddy = mb_find_buddy(e4b, ord, &max);
1460 mb_set_bit(start >> ord, buddy);
1461 e4b->bd_info->bb_counters[ord]--;
1462
1463 ord--;
1464 cur = (start >> ord) & ~1U;
1465 buddy = mb_find_buddy(e4b, ord, &max);
1466 mb_clear_bit(cur, buddy);
1467 mb_clear_bit(cur + 1, buddy);
1468 e4b->bd_info->bb_counters[ord]++;
1469 e4b->bd_info->bb_counters[ord]++;
1470 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001471 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001472
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001473 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001474 mb_check_buddy(e4b);
1475
1476 return ret;
1477}
1478
1479/*
1480 * Must be called under group lock!
1481 */
1482static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1483 struct ext4_buddy *e4b)
1484{
1485 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1486 int ret;
1487
1488 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1489 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1490
1491 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1492 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1493 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1494
1495 /* preallocation can change ac_b_ex, thus we store actually
1496 * allocated blocks for history */
1497 ac->ac_f_ex = ac->ac_b_ex;
1498
1499 ac->ac_status = AC_STATUS_FOUND;
1500 ac->ac_tail = ret & 0xffff;
1501 ac->ac_buddy = ret >> 16;
1502
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001503 /*
1504 * take the page reference. We want the page to be pinned
1505 * so that we don't get a ext4_mb_init_cache_call for this
1506 * group until we update the bitmap. That would mean we
1507 * double allocate blocks. The reference is dropped
1508 * in ext4_mb_release_context
1509 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001510 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1511 get_page(ac->ac_bitmap_page);
1512 ac->ac_buddy_page = e4b->bd_buddy_page;
1513 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001514 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001515 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001516 spin_lock(&sbi->s_md_lock);
1517 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1518 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1519 spin_unlock(&sbi->s_md_lock);
1520 }
1521}
1522
1523/*
1524 * regular allocator, for general purposes allocation
1525 */
1526
1527static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1528 struct ext4_buddy *e4b,
1529 int finish_group)
1530{
1531 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1532 struct ext4_free_extent *bex = &ac->ac_b_ex;
1533 struct ext4_free_extent *gex = &ac->ac_g_ex;
1534 struct ext4_free_extent ex;
1535 int max;
1536
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001537 if (ac->ac_status == AC_STATUS_FOUND)
1538 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001539 /*
1540 * We don't want to scan for a whole year
1541 */
1542 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1543 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1544 ac->ac_status = AC_STATUS_BREAK;
1545 return;
1546 }
1547
1548 /*
1549 * Haven't found good chunk so far, let's continue
1550 */
1551 if (bex->fe_len < gex->fe_len)
1552 return;
1553
1554 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1555 && bex->fe_group == e4b->bd_group) {
1556 /* recheck chunk's availability - we don't know
1557 * when it was found (within this lock-unlock
1558 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001559 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001560 if (max >= gex->fe_len) {
1561 ext4_mb_use_best_found(ac, e4b);
1562 return;
1563 }
1564 }
1565}
1566
1567/*
1568 * The routine checks whether found extent is good enough. If it is,
1569 * then the extent gets marked used and flag is set to the context
1570 * to stop scanning. Otherwise, the extent is compared with the
1571 * previous found extent and if new one is better, then it's stored
1572 * in the context. Later, the best found extent will be used, if
1573 * mballoc can't find good enough extent.
1574 *
1575 * FIXME: real allocation policy is to be designed yet!
1576 */
1577static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1578 struct ext4_free_extent *ex,
1579 struct ext4_buddy *e4b)
1580{
1581 struct ext4_free_extent *bex = &ac->ac_b_ex;
1582 struct ext4_free_extent *gex = &ac->ac_g_ex;
1583
1584 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001585 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1586 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001587 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1588
1589 ac->ac_found++;
1590
1591 /*
1592 * The special case - take what you catch first
1593 */
1594 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1595 *bex = *ex;
1596 ext4_mb_use_best_found(ac, e4b);
1597 return;
1598 }
1599
1600 /*
1601 * Let's check whether the chuck is good enough
1602 */
1603 if (ex->fe_len == gex->fe_len) {
1604 *bex = *ex;
1605 ext4_mb_use_best_found(ac, e4b);
1606 return;
1607 }
1608
1609 /*
1610 * If this is first found extent, just store it in the context
1611 */
1612 if (bex->fe_len == 0) {
1613 *bex = *ex;
1614 return;
1615 }
1616
1617 /*
1618 * If new found extent is better, store it in the context
1619 */
1620 if (bex->fe_len < gex->fe_len) {
1621 /* if the request isn't satisfied, any found extent
1622 * larger than previous best one is better */
1623 if (ex->fe_len > bex->fe_len)
1624 *bex = *ex;
1625 } else if (ex->fe_len > gex->fe_len) {
1626 /* if the request is satisfied, then we try to find
1627 * an extent that still satisfy the request, but is
1628 * smaller than previous one */
1629 if (ex->fe_len < bex->fe_len)
1630 *bex = *ex;
1631 }
1632
1633 ext4_mb_check_limits(ac, e4b, 0);
1634}
1635
Eric Sandeen089ceec2009-07-05 22:17:31 -04001636static noinline_for_stack
1637int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001638 struct ext4_buddy *e4b)
1639{
1640 struct ext4_free_extent ex = ac->ac_b_ex;
1641 ext4_group_t group = ex.fe_group;
1642 int max;
1643 int err;
1644
1645 BUG_ON(ex.fe_len <= 0);
1646 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1647 if (err)
1648 return err;
1649
1650 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001651 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001652
1653 if (max > 0) {
1654 ac->ac_b_ex = ex;
1655 ext4_mb_use_best_found(ac, e4b);
1656 }
1657
1658 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001659 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001660
1661 return 0;
1662}
1663
Eric Sandeen089ceec2009-07-05 22:17:31 -04001664static noinline_for_stack
1665int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001666 struct ext4_buddy *e4b)
1667{
1668 ext4_group_t group = ac->ac_g_ex.fe_group;
1669 int max;
1670 int err;
1671 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001672 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001673 struct ext4_free_extent ex;
1674
1675 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1676 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001677 if (grp->bb_free == 0)
1678 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001679
1680 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1681 if (err)
1682 return err;
1683
1684 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001685 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001686 ac->ac_g_ex.fe_len, &ex);
1687
1688 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1689 ext4_fsblk_t start;
1690
Akinobu Mita5661bd62010-03-03 23:53:39 -05001691 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1692 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001693 /* use do_div to get remainder (would be 64-bit modulo) */
1694 if (do_div(start, sbi->s_stripe) == 0) {
1695 ac->ac_found++;
1696 ac->ac_b_ex = ex;
1697 ext4_mb_use_best_found(ac, e4b);
1698 }
1699 } else if (max >= ac->ac_g_ex.fe_len) {
1700 BUG_ON(ex.fe_len <= 0);
1701 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1702 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1703 ac->ac_found++;
1704 ac->ac_b_ex = ex;
1705 ext4_mb_use_best_found(ac, e4b);
1706 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1707 /* Sometimes, caller may want to merge even small
1708 * number of blocks to an existing extent */
1709 BUG_ON(ex.fe_len <= 0);
1710 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1711 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1712 ac->ac_found++;
1713 ac->ac_b_ex = ex;
1714 ext4_mb_use_best_found(ac, e4b);
1715 }
1716 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001717 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001718
1719 return 0;
1720}
1721
1722/*
1723 * The routine scans buddy structures (not bitmap!) from given order
1724 * to max order and tries to find big enough chunk to satisfy the req
1725 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001726static noinline_for_stack
1727void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001728 struct ext4_buddy *e4b)
1729{
1730 struct super_block *sb = ac->ac_sb;
1731 struct ext4_group_info *grp = e4b->bd_info;
1732 void *buddy;
1733 int i;
1734 int k;
1735 int max;
1736
1737 BUG_ON(ac->ac_2order <= 0);
1738 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1739 if (grp->bb_counters[i] == 0)
1740 continue;
1741
1742 buddy = mb_find_buddy(e4b, i, &max);
1743 BUG_ON(buddy == NULL);
1744
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001745 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001746 BUG_ON(k >= max);
1747
1748 ac->ac_found++;
1749
1750 ac->ac_b_ex.fe_len = 1 << i;
1751 ac->ac_b_ex.fe_start = k << i;
1752 ac->ac_b_ex.fe_group = e4b->bd_group;
1753
1754 ext4_mb_use_best_found(ac, e4b);
1755
1756 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1757
1758 if (EXT4_SB(sb)->s_mb_stats)
1759 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1760
1761 break;
1762 }
1763}
1764
1765/*
1766 * The routine scans the group and measures all found extents.
1767 * In order to optimize scanning, caller must pass number of
1768 * free blocks in the group, so the routine can know upper limit.
1769 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001770static noinline_for_stack
1771void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001772 struct ext4_buddy *e4b)
1773{
1774 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001775 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001776 struct ext4_free_extent ex;
1777 int i;
1778 int free;
1779
1780 free = e4b->bd_info->bb_free;
1781 BUG_ON(free <= 0);
1782
1783 i = e4b->bd_info->bb_first_free;
1784
1785 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001786 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001787 EXT4_CLUSTERS_PER_GROUP(sb), i);
1788 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001789 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001790 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001791 * free blocks even though group info says we
1792 * we have free blocks
1793 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001794 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001795 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001796 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001797 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001798 break;
1799 }
1800
Robin Dong15c006a2012-08-17 10:02:17 -04001801 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001802 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001803 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001804 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001805 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001806 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001807 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001808 /*
1809 * The number of free blocks differs. This mostly
1810 * indicate that the bitmap is corrupt. So exit
1811 * without claiming the space.
1812 */
1813 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001814 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001815
1816 ext4_mb_measure_extent(ac, &ex, e4b);
1817
1818 i += ex.fe_len;
1819 free -= ex.fe_len;
1820 }
1821
1822 ext4_mb_check_limits(ac, e4b, 1);
1823}
1824
1825/*
1826 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001827 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001828 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001829static noinline_for_stack
1830void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001831 struct ext4_buddy *e4b)
1832{
1833 struct super_block *sb = ac->ac_sb;
1834 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001835 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001836 struct ext4_free_extent ex;
1837 ext4_fsblk_t first_group_block;
1838 ext4_fsblk_t a;
1839 ext4_grpblk_t i;
1840 int max;
1841
1842 BUG_ON(sbi->s_stripe == 0);
1843
1844 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001845 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1846
Alex Tomasc9de5602008-01-29 00:19:52 -05001847 a = first_group_block + sbi->s_stripe - 1;
1848 do_div(a, sbi->s_stripe);
1849 i = (a * sbi->s_stripe) - first_group_block;
1850
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001851 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001852 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04001853 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001854 if (max >= sbi->s_stripe) {
1855 ac->ac_found++;
1856 ac->ac_b_ex = ex;
1857 ext4_mb_use_best_found(ac, e4b);
1858 break;
1859 }
1860 }
1861 i += sbi->s_stripe;
1862 }
1863}
1864
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001865/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05001866static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1867 ext4_group_t group, int cr)
1868{
1869 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04001870 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001871 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1872
1873 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001874
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04001875 free = grp->bb_free;
1876 if (free == 0)
1877 return 0;
1878 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
1879 return 0;
1880
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001881 /* We only do this if the grp has never been initialized */
1882 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1883 int ret = ext4_mb_init_group(ac->ac_sb, group);
1884 if (ret)
1885 return 0;
1886 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001887
Alex Tomasc9de5602008-01-29 00:19:52 -05001888 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05001889 if (fragments == 0)
1890 return 0;
1891
1892 switch (cr) {
1893 case 0:
1894 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001895
Theodore Ts'oa4912122009-03-12 12:18:34 -04001896 /* Avoid using the first bg of a flexgroup for data files */
1897 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1898 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1899 ((group % flex_size) == 0))
1900 return 0;
1901
Theodore Ts'o40ae3482013-02-04 15:08:40 -05001902 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
1903 (free / fragments) >= ac->ac_g_ex.fe_len)
1904 return 1;
1905
1906 if (grp->bb_largest_free_order < ac->ac_2order)
1907 return 0;
1908
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001909 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001910 case 1:
1911 if ((free / fragments) >= ac->ac_g_ex.fe_len)
1912 return 1;
1913 break;
1914 case 2:
1915 if (free >= ac->ac_g_ex.fe_len)
1916 return 1;
1917 break;
1918 case 3:
1919 return 1;
1920 default:
1921 BUG();
1922 }
1923
1924 return 0;
1925}
1926
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001927static noinline_for_stack int
1928ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05001929{
Theodore Ts'o8df96752009-05-01 08:50:38 -04001930 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05001931 int cr;
1932 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001933 struct ext4_sb_info *sbi;
1934 struct super_block *sb;
1935 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05001936
1937 sb = ac->ac_sb;
1938 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04001939 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001940 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001941 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04001942 ngroups = sbi->s_blockfile_groups;
1943
Alex Tomasc9de5602008-01-29 00:19:52 -05001944 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1945
1946 /* first, try the goal */
1947 err = ext4_mb_find_by_goal(ac, &e4b);
1948 if (err || ac->ac_status == AC_STATUS_FOUND)
1949 goto out;
1950
1951 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1952 goto out;
1953
1954 /*
1955 * ac->ac2_order is set only if the fe_len is a power of 2
1956 * if ac2_order is set we also set criteria to 0 so that we
1957 * try exact allocation using buddy.
1958 */
1959 i = fls(ac->ac_g_ex.fe_len);
1960 ac->ac_2order = 0;
1961 /*
1962 * We search using buddy data only if the order of the request
1963 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04001964 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05001965 */
1966 if (i >= sbi->s_mb_order2_reqs) {
1967 /*
1968 * This should tell if fe_len is exactly power of 2
1969 */
1970 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1971 ac->ac_2order = i - 1;
1972 }
1973
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001974 /* if stream allocation is enabled, use global goal */
1975 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001976 /* TBD: may be hot point */
1977 spin_lock(&sbi->s_md_lock);
1978 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
1979 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
1980 spin_unlock(&sbi->s_md_lock);
1981 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001982
Alex Tomasc9de5602008-01-29 00:19:52 -05001983 /* Let's just scan groups to find more-less suitable blocks */
1984 cr = ac->ac_2order ? 0 : 1;
1985 /*
1986 * cr == 0 try to get exact allocation,
1987 * cr == 3 try to get anything
1988 */
1989repeat:
1990 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
1991 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04001992 /*
1993 * searching for the right group start
1994 * from the goal value specified
1995 */
1996 group = ac->ac_g_ex.fe_group;
1997
Theodore Ts'o8df96752009-05-01 08:50:38 -04001998 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04001999 if (group == ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002000 group = 0;
2001
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002002 /* This now checks without needing the buddy page */
2003 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002004 continue;
2005
Alex Tomasc9de5602008-01-29 00:19:52 -05002006 err = ext4_mb_load_buddy(sb, group, &e4b);
2007 if (err)
2008 goto out;
2009
2010 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002011
2012 /*
2013 * We need to check again after locking the
2014 * block group
2015 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002016 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002017 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002018 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002019 continue;
2020 }
2021
2022 ac->ac_groups_scanned++;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002023 if (cr == 0 && ac->ac_2order < sb->s_blocksize_bits+2)
Alex Tomasc9de5602008-01-29 00:19:52 -05002024 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002025 else if (cr == 1 && sbi->s_stripe &&
2026 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002027 ext4_mb_scan_aligned(ac, &e4b);
2028 else
2029 ext4_mb_complex_scan_group(ac, &e4b);
2030
2031 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002032 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002033
2034 if (ac->ac_status != AC_STATUS_CONTINUE)
2035 break;
2036 }
2037 }
2038
2039 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2040 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2041 /*
2042 * We've been searching too long. Let's try to allocate
2043 * the best chunk we've found so far
2044 */
2045
2046 ext4_mb_try_best_found(ac, &e4b);
2047 if (ac->ac_status != AC_STATUS_FOUND) {
2048 /*
2049 * Someone more lucky has already allocated it.
2050 * The only thing we can do is just take first
2051 * found block(s)
2052 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2053 */
2054 ac->ac_b_ex.fe_group = 0;
2055 ac->ac_b_ex.fe_start = 0;
2056 ac->ac_b_ex.fe_len = 0;
2057 ac->ac_status = AC_STATUS_CONTINUE;
2058 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2059 cr = 3;
2060 atomic_inc(&sbi->s_mb_lost_chunks);
2061 goto repeat;
2062 }
2063 }
2064out:
2065 return err;
2066}
2067
Alex Tomasc9de5602008-01-29 00:19:52 -05002068static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2069{
2070 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002071 ext4_group_t group;
2072
Theodore Ts'o8df96752009-05-01 08:50:38 -04002073 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002074 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002075 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002076 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002077}
2078
2079static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2080{
2081 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002082 ext4_group_t group;
2083
2084 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002085 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002086 return NULL;
2087 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002088 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002089}
2090
2091static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2092{
2093 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002094 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002095 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002096 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002097 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002098 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002099 struct sg {
2100 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002101 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002102 } sg;
2103
2104 group--;
2105 if (group == 0)
2106 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2107 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2108 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2109 "group", "free", "frags", "first",
2110 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2111 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2112
2113 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2114 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002115 grinfo = ext4_get_group_info(sb, group);
2116 /* Load the group info in memory only if not already loaded. */
2117 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2118 err = ext4_mb_load_buddy(sb, group, &e4b);
2119 if (err) {
2120 seq_printf(seq, "#%-5u: I/O error\n", group);
2121 return 0;
2122 }
2123 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002124 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002125
Alex Tomasc9de5602008-01-29 00:19:52 -05002126 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002127
2128 if (buddy_loaded)
2129 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002130
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002131 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002132 sg.info.bb_fragments, sg.info.bb_first_free);
2133 for (i = 0; i <= 13; i++)
2134 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2135 sg.info.bb_counters[i] : 0);
2136 seq_printf(seq, " ]\n");
2137
2138 return 0;
2139}
2140
2141static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2142{
2143}
2144
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002145static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002146 .start = ext4_mb_seq_groups_start,
2147 .next = ext4_mb_seq_groups_next,
2148 .stop = ext4_mb_seq_groups_stop,
2149 .show = ext4_mb_seq_groups_show,
2150};
2151
2152static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2153{
2154 struct super_block *sb = PDE(inode)->data;
2155 int rc;
2156
2157 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2158 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002159 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002160 m->private = sb;
2161 }
2162 return rc;
2163
2164}
2165
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002166static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002167 .owner = THIS_MODULE,
2168 .open = ext4_mb_seq_groups_open,
2169 .read = seq_read,
2170 .llseek = seq_lseek,
2171 .release = seq_release,
2172};
2173
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002174static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2175{
2176 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2177 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2178
2179 BUG_ON(!cachep);
2180 return cachep;
2181}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002182
Theodore Ts'o28623c22012-09-05 01:31:50 -04002183/*
2184 * Allocate the top-level s_group_info array for the specified number
2185 * of groups
2186 */
2187int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2188{
2189 struct ext4_sb_info *sbi = EXT4_SB(sb);
2190 unsigned size;
2191 struct ext4_group_info ***new_groupinfo;
2192
2193 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2194 EXT4_DESC_PER_BLOCK_BITS(sb);
2195 if (size <= sbi->s_group_info_size)
2196 return 0;
2197
2198 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2199 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2200 if (!new_groupinfo) {
2201 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2202 return -ENOMEM;
2203 }
2204 if (sbi->s_group_info) {
2205 memcpy(new_groupinfo, sbi->s_group_info,
2206 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
2207 ext4_kvfree(sbi->s_group_info);
2208 }
2209 sbi->s_group_info = new_groupinfo;
2210 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2211 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2212 sbi->s_group_info_size);
2213 return 0;
2214}
2215
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002216/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002217int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002218 struct ext4_group_desc *desc)
2219{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002220 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002221 int metalen = 0;
2222 struct ext4_sb_info *sbi = EXT4_SB(sb);
2223 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002224 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002225
2226 /*
2227 * First check if this group is the first of a reserved block.
2228 * If it's true, we have to allocate a new table of pointers
2229 * to ext4_group_info structures
2230 */
2231 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2232 metalen = sizeof(*meta_group_info) <<
2233 EXT4_DESC_PER_BLOCK_BITS(sb);
2234 meta_group_info = kmalloc(metalen, GFP_KERNEL);
2235 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002236 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002237 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002238 goto exit_meta_group_info;
2239 }
2240 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2241 meta_group_info;
2242 }
2243
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002244 meta_group_info =
2245 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2246 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2247
Wei Yongjun85556c92012-09-26 20:43:37 -04002248 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_KERNEL);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002249 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002250 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002251 goto exit_group_info;
2252 }
2253 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2254 &(meta_group_info[i]->bb_state));
2255
2256 /*
2257 * initialize bb_free to be able to skip
2258 * empty groups without initialization
2259 */
2260 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2261 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002262 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002263 } else {
2264 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002265 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002266 }
2267
2268 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002269 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002270 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002271 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002272
2273#ifdef DOUBLE_CHECK
2274 {
2275 struct buffer_head *bh;
2276 meta_group_info[i]->bb_bitmap =
2277 kmalloc(sb->s_blocksize, GFP_KERNEL);
2278 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2279 bh = ext4_read_block_bitmap(sb, group);
2280 BUG_ON(bh == NULL);
2281 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2282 sb->s_blocksize);
2283 put_bh(bh);
2284 }
2285#endif
2286
2287 return 0;
2288
2289exit_group_info:
2290 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002291 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002292 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002293 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2294 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002295exit_meta_group_info:
2296 return -ENOMEM;
2297} /* ext4_mb_add_groupinfo */
2298
Alex Tomasc9de5602008-01-29 00:19:52 -05002299static int ext4_mb_init_backend(struct super_block *sb)
2300{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002301 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002302 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002303 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002304 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002305 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002306 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002307
Theodore Ts'o28623c22012-09-05 01:31:50 -04002308 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2309 if (err)
2310 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002311
Alex Tomasc9de5602008-01-29 00:19:52 -05002312 sbi->s_buddy_cache = new_inode(sb);
2313 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002314 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002315 goto err_freesgi;
2316 }
Yu Jian48e60612011-08-01 17:41:39 -04002317 /* To avoid potentially colliding with an valid on-disk inode number,
2318 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2319 * not in the inode hash, so it should never be found by iget(), but
2320 * this will avoid confusion if it ever shows up during debugging. */
2321 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002322 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002323 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002324 desc = ext4_get_group_desc(sb, i, NULL);
2325 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002326 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002327 goto err_freebuddy;
2328 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002329 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2330 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002331 }
2332
2333 return 0;
2334
2335err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002336 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002337 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002338 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002339 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002340 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002341 kfree(sbi->s_group_info[i]);
2342 iput(sbi->s_buddy_cache);
2343err_freesgi:
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002344 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002345 return -ENOMEM;
2346}
2347
Eric Sandeen2892c152011-02-12 08:12:18 -05002348static void ext4_groupinfo_destroy_slabs(void)
2349{
2350 int i;
2351
2352 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2353 if (ext4_groupinfo_caches[i])
2354 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2355 ext4_groupinfo_caches[i] = NULL;
2356 }
2357}
2358
2359static int ext4_groupinfo_create_slab(size_t size)
2360{
2361 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2362 int slab_size;
2363 int blocksize_bits = order_base_2(size);
2364 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2365 struct kmem_cache *cachep;
2366
2367 if (cache_index >= NR_GRPINFO_CACHES)
2368 return -EINVAL;
2369
2370 if (unlikely(cache_index < 0))
2371 cache_index = 0;
2372
2373 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2374 if (ext4_groupinfo_caches[cache_index]) {
2375 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2376 return 0; /* Already created */
2377 }
2378
2379 slab_size = offsetof(struct ext4_group_info,
2380 bb_counters[blocksize_bits + 2]);
2381
2382 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2383 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2384 NULL);
2385
Tao Ma823ba012011-07-11 18:26:01 -04002386 ext4_groupinfo_caches[cache_index] = cachep;
2387
Eric Sandeen2892c152011-02-12 08:12:18 -05002388 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2389 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002390 printk(KERN_EMERG
2391 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002392 return -ENOMEM;
2393 }
2394
Eric Sandeen2892c152011-02-12 08:12:18 -05002395 return 0;
2396}
2397
Akira Fujita9d990122012-05-28 14:19:25 -04002398int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002399{
2400 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002401 unsigned i, j;
Alex Tomasc9de5602008-01-29 00:19:52 -05002402 unsigned offset;
2403 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002404 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002405
Eric Sandeen19278052009-08-25 22:36:25 -04002406 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002407
2408 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2409 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002410 ret = -ENOMEM;
2411 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002412 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002413
Eric Sandeen19278052009-08-25 22:36:25 -04002414 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002415 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2416 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002417 ret = -ENOMEM;
2418 goto out;
2419 }
2420
Eric Sandeen2892c152011-02-12 08:12:18 -05002421 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2422 if (ret < 0)
2423 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002424
2425 /* order 0 is regular bitmap */
2426 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2427 sbi->s_mb_offsets[0] = 0;
2428
2429 i = 1;
2430 offset = 0;
2431 max = sb->s_blocksize << 2;
2432 do {
2433 sbi->s_mb_offsets[i] = offset;
2434 sbi->s_mb_maxs[i] = max;
2435 offset += 1 << (sb->s_blocksize_bits - i);
2436 max = max >> 1;
2437 i++;
2438 } while (i <= sb->s_blocksize_bits + 1);
2439
Alex Tomasc9de5602008-01-29 00:19:52 -05002440 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002441 spin_lock_init(&sbi->s_bal_lock);
2442
2443 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2444 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2445 sbi->s_mb_stats = MB_DEFAULT_STATS;
2446 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2447 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002448 /*
2449 * The default group preallocation is 512, which for 4k block
2450 * sizes translates to 2 megabytes. However for bigalloc file
2451 * systems, this is probably too big (i.e, if the cluster size
2452 * is 1 megabyte, then group preallocation size becomes half a
2453 * gigabyte!). As a default, we will keep a two megabyte
2454 * group pralloc size for cluster sizes up to 64k, and after
2455 * that, we will force a minimum group preallocation size of
2456 * 32 clusters. This translates to 8 megs when the cluster
2457 * size is 256k, and 32 megs when the cluster size is 1 meg,
2458 * which seems reasonable as a default.
2459 */
2460 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2461 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002462 /*
2463 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2464 * to the lowest multiple of s_stripe which is bigger than
2465 * the s_mb_group_prealloc as determined above. We want
2466 * the preallocation size to be an exact multiple of the
2467 * RAID stripe size so that preallocations don't fragment
2468 * the stripes.
2469 */
2470 if (sbi->s_stripe > 1) {
2471 sbi->s_mb_group_prealloc = roundup(
2472 sbi->s_mb_group_prealloc, sbi->s_stripe);
2473 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002474
Eric Sandeen730c2132008-09-13 15:23:29 -04002475 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002476 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002477 ret = -ENOMEM;
Tao Ma7aa0bae2011-10-06 10:22:28 -04002478 goto out_free_groupinfo_slab;
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002480 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002481 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002482 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002483 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002484 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2485 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002486 spin_lock_init(&lg->lg_prealloc_lock);
2487 }
2488
Yu Jian79a77c52011-08-01 17:41:46 -04002489 /* init file for buddy data */
2490 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002491 if (ret != 0)
2492 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002493
Theodore Ts'o296c3552009-09-30 00:32:42 -04002494 if (sbi->s_proc)
2495 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2496 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002497
Tao Ma7aa0bae2011-10-06 10:22:28 -04002498 return 0;
2499
2500out_free_locality_groups:
2501 free_percpu(sbi->s_locality_groups);
2502 sbi->s_locality_groups = NULL;
2503out_free_groupinfo_slab:
2504 ext4_groupinfo_destroy_slabs();
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002505out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002506 kfree(sbi->s_mb_offsets);
2507 sbi->s_mb_offsets = NULL;
2508 kfree(sbi->s_mb_maxs);
2509 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002510 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002511}
2512
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002513/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002514static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2515{
2516 struct ext4_prealloc_space *pa;
2517 struct list_head *cur, *tmp;
2518 int count = 0;
2519
2520 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2521 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2522 list_del(&pa->pa_group_list);
2523 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002524 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002525 }
2526 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002527 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002528
2529}
2530
2531int ext4_mb_release(struct super_block *sb)
2532{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002533 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002534 ext4_group_t i;
2535 int num_meta_group_infos;
2536 struct ext4_group_info *grinfo;
2537 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002538 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002539
Salman Qazi95599962012-05-31 23:52:14 -04002540 if (sbi->s_proc)
2541 remove_proc_entry("mb_groups", sbi->s_proc);
2542
Alex Tomasc9de5602008-01-29 00:19:52 -05002543 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002544 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002545 grinfo = ext4_get_group_info(sb, i);
2546#ifdef DOUBLE_CHECK
2547 kfree(grinfo->bb_bitmap);
2548#endif
2549 ext4_lock_group(sb, i);
2550 ext4_mb_cleanup_pa(grinfo);
2551 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002552 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002553 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002554 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002555 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2556 EXT4_DESC_PER_BLOCK_BITS(sb);
2557 for (i = 0; i < num_meta_group_infos; i++)
2558 kfree(sbi->s_group_info[i]);
Theodore Ts'of18a5f22011-08-01 08:45:38 -04002559 ext4_kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002560 }
2561 kfree(sbi->s_mb_offsets);
2562 kfree(sbi->s_mb_maxs);
2563 if (sbi->s_buddy_cache)
2564 iput(sbi->s_buddy_cache);
2565 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002566 ext4_msg(sb, KERN_INFO,
2567 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002568 atomic_read(&sbi->s_bal_allocated),
2569 atomic_read(&sbi->s_bal_reqs),
2570 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002571 ext4_msg(sb, KERN_INFO,
2572 "mballoc: %u extents scanned, %u goal hits, "
2573 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002574 atomic_read(&sbi->s_bal_ex_scanned),
2575 atomic_read(&sbi->s_bal_goals),
2576 atomic_read(&sbi->s_bal_2orders),
2577 atomic_read(&sbi->s_bal_breaks),
2578 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002579 ext4_msg(sb, KERN_INFO,
2580 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002581 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002582 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002583 ext4_msg(sb, KERN_INFO,
2584 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002585 atomic_read(&sbi->s_mb_preallocated),
2586 atomic_read(&sbi->s_mb_discarded));
2587 }
2588
Eric Sandeen730c2132008-09-13 15:23:29 -04002589 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002590
2591 return 0;
2592}
2593
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002594static inline int ext4_issue_discard(struct super_block *sb,
Theodore Ts'o84130192011-09-09 18:50:51 -04002595 ext4_group_t block_group, ext4_grpblk_t cluster, int count)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002596{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002597 ext4_fsblk_t discard_block;
2598
Theodore Ts'o84130192011-09-09 18:50:51 -04002599 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2600 ext4_group_first_block_no(sb, block_group));
2601 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002602 trace_ext4_discard_blocks(sb,
2603 (unsigned long long) discard_block, count);
Lukas Czerner93259632011-01-10 12:09:59 -05002604 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002605}
2606
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002607/*
2608 * This function is called by the jbd2 layer once the commit has finished,
2609 * so we know we can free the blocks that were released with that commit.
2610 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002611static void ext4_free_data_callback(struct super_block *sb,
2612 struct ext4_journal_cb_entry *jce,
2613 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002614{
Bobi Jam18aadd42012-02-20 17:53:02 -05002615 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002616 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002617 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002618 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002619
Bobi Jam18aadd42012-02-20 17:53:02 -05002620 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2621 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002622
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002623 if (test_opt(sb, DISCARD)) {
2624 err = ext4_issue_discard(sb, entry->efd_group,
2625 entry->efd_start_cluster,
2626 entry->efd_count);
2627 if (err && err != -EOPNOTSUPP)
2628 ext4_msg(sb, KERN_WARNING, "discard request in"
2629 " group:%d block:%d count:%d failed"
2630 " with %d", entry->efd_group,
2631 entry->efd_start_cluster,
2632 entry->efd_count, err);
2633 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002634
Bobi Jam18aadd42012-02-20 17:53:02 -05002635 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2636 /* we expect to find existing buddy because it's pinned */
2637 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002638
Alex Tomasc9de5602008-01-29 00:19:52 -05002639
Bobi Jam18aadd42012-02-20 17:53:02 -05002640 db = e4b.bd_info;
2641 /* there are blocks to put in buddy to make them really free */
2642 count += entry->efd_count;
2643 count2++;
2644 ext4_lock_group(sb, entry->efd_group);
2645 /* Take it out of per group rb tree */
2646 rb_erase(&entry->efd_node, &(db->bb_free_root));
2647 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002648
Bobi Jam18aadd42012-02-20 17:53:02 -05002649 /*
2650 * Clear the trimmed flag for the group so that the next
2651 * ext4_trim_fs can trim it.
2652 * If the volume is mounted with -o discard, online discard
2653 * is supported and the free blocks will be trimmed online.
2654 */
2655 if (!test_opt(sb, DISCARD))
2656 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2657
2658 if (!db->bb_free_root.rb_node) {
2659 /* No more items in the per group rb tree
2660 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002661 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002662 page_cache_release(e4b.bd_buddy_page);
2663 page_cache_release(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002664 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002665 ext4_unlock_group(sb, entry->efd_group);
2666 kmem_cache_free(ext4_free_data_cachep, entry);
2667 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002668
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002669 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002670}
2671
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002672int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002673{
Theodore Ts'o16828082010-10-27 21:30:09 -04002674 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2675 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002676 if (ext4_pspace_cachep == NULL)
2677 return -ENOMEM;
2678
Theodore Ts'o16828082010-10-27 21:30:09 -04002679 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2680 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002681 if (ext4_ac_cachep == NULL) {
2682 kmem_cache_destroy(ext4_pspace_cachep);
2683 return -ENOMEM;
2684 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002685
Bobi Jam18aadd42012-02-20 17:53:02 -05002686 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2687 SLAB_RECLAIM_ACCOUNT);
2688 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002689 kmem_cache_destroy(ext4_pspace_cachep);
2690 kmem_cache_destroy(ext4_ac_cachep);
2691 return -ENOMEM;
2692 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002693 return 0;
2694}
2695
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002696void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002697{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002698 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002699 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2700 * before destroying the slab cache.
2701 */
2702 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002703 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002704 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002705 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002706 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002707}
2708
2709
2710/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002711 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002712 * Returns 0 if success or error code
2713 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002714static noinline_for_stack int
2715ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002716 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002717{
2718 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002719 struct ext4_group_desc *gdp;
2720 struct buffer_head *gdp_bh;
2721 struct ext4_sb_info *sbi;
2722 struct super_block *sb;
2723 ext4_fsblk_t block;
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002724 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002725
2726 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2727 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2728
2729 sb = ac->ac_sb;
2730 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002731
2732 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002733 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002734 if (!bitmap_bh)
2735 goto out_err;
2736
2737 err = ext4_journal_get_write_access(handle, bitmap_bh);
2738 if (err)
2739 goto out_err;
2740
2741 err = -EIO;
2742 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2743 if (!gdp)
2744 goto out_err;
2745
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002746 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002747 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002748
Alex Tomasc9de5602008-01-29 00:19:52 -05002749 err = ext4_journal_get_write_access(handle, gdp_bh);
2750 if (err)
2751 goto out_err;
2752
Akinobu Mitabda00de2010-03-03 23:53:25 -05002753 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002754
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002755 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002756 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002757 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002758 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002759 /* File system mounted not to panic on error
2760 * Fix the bitmap and repeat the block allocation
2761 * We leak some of the blocks here.
2762 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002763 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002764 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2765 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002766 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002767 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04002768 if (!err)
2769 err = -EAGAIN;
2770 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002771 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002772
2773 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002774#ifdef AGGRESSIVE_CHECK
2775 {
2776 int i;
2777 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2778 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2779 bitmap_bh->b_data));
2780 }
2781 }
2782#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002783 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2784 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002785 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2786 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002787 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002788 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002789 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002790 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002791 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2792 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04002793 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002794 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002795
2796 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002797 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002798 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002799 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002800 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002801 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2802 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002803 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2804 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002805
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002806 if (sbi->s_log_groups_per_flex) {
2807 ext4_group_t flex_group = ext4_flex_group(sbi,
2808 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04002809 atomic64_sub(ac->ac_b_ex.fe_len,
2810 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002811 }
2812
Frank Mayhar03901312009-01-07 00:06:22 -05002813 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002814 if (err)
2815 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002816 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002817
2818out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002819 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002820 return err;
2821}
2822
2823/*
2824 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002825 * Group request are normalized to s_mb_group_prealloc, which goes to
2826 * s_strip if we set the same via mount option.
2827 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002828 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002829 *
2830 * XXX: should we try to preallocate more than the group has now?
2831 */
2832static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2833{
2834 struct super_block *sb = ac->ac_sb;
2835 struct ext4_locality_group *lg = ac->ac_lg;
2836
2837 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002838 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002839 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002840 current->pid, ac->ac_g_ex.fe_len);
2841}
2842
2843/*
2844 * Normalization means making request better in terms of
2845 * size and alignment
2846 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002847static noinline_for_stack void
2848ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05002849 struct ext4_allocation_request *ar)
2850{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002851 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002852 int bsbits, max;
2853 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05002854 loff_t size, start_off;
2855 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04002856 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05002857 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002858 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05002859
2860 /* do normalize only data requests, metadata requests
2861 do not need preallocation */
2862 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
2863 return;
2864
2865 /* sometime caller may want exact blocks */
2866 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2867 return;
2868
2869 /* caller may indicate that preallocation isn't
2870 * required (it's a tail, for example) */
2871 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
2872 return;
2873
2874 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
2875 ext4_mb_normalize_group_request(ac);
2876 return ;
2877 }
2878
2879 bsbits = ac->ac_sb->s_blocksize_bits;
2880
2881 /* first, let's learn actual file size
2882 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002883 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002884 size = size << bsbits;
2885 if (size < i_size_read(ac->ac_inode))
2886 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04002887 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05002888
Valerie Clement19304792008-05-13 19:31:14 -04002889 /* max size of free chunks */
2890 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002891
Valerie Clement19304792008-05-13 19:31:14 -04002892#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
2893 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05002894
2895 /* first, try to predict filesize */
2896 /* XXX: should this table be tunable? */
2897 start_off = 0;
2898 if (size <= 16 * 1024) {
2899 size = 16 * 1024;
2900 } else if (size <= 32 * 1024) {
2901 size = 32 * 1024;
2902 } else if (size <= 64 * 1024) {
2903 size = 64 * 1024;
2904 } else if (size <= 128 * 1024) {
2905 size = 128 * 1024;
2906 } else if (size <= 256 * 1024) {
2907 size = 256 * 1024;
2908 } else if (size <= 512 * 1024) {
2909 size = 512 * 1024;
2910 } else if (size <= 1024 * 1024) {
2911 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04002912 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002913 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04002914 (21 - bsbits)) << 21;
2915 size = 2 * 1024 * 1024;
2916 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002917 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2918 (22 - bsbits)) << 22;
2919 size = 4 * 1024 * 1024;
2920 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04002921 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002922 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
2923 (23 - bsbits)) << 23;
2924 size = 8 * 1024 * 1024;
2925 } else {
2926 start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
2927 size = ac->ac_o_ex.fe_len << bsbits;
2928 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04002929 size = size >> bsbits;
2930 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05002931
2932 /* don't cover already allocated blocks in selected range */
2933 if (ar->pleft && start <= ar->lleft) {
2934 size -= ar->lleft + 1 - start;
2935 start = ar->lleft + 1;
2936 }
2937 if (ar->pright && start + size - 1 >= ar->lright)
2938 size -= start + size - ar->lright;
2939
2940 end = start + size;
2941
2942 /* check we don't cross already preallocated blocks */
2943 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002944 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002945 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05002946
Alex Tomasc9de5602008-01-29 00:19:52 -05002947 if (pa->pa_deleted)
2948 continue;
2949 spin_lock(&pa->pa_lock);
2950 if (pa->pa_deleted) {
2951 spin_unlock(&pa->pa_lock);
2952 continue;
2953 }
2954
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002955 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
2956 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002957
2958 /* PA must not overlap original request */
2959 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
2960 ac->ac_o_ex.fe_logical < pa->pa_lstart));
2961
Eric Sandeen38877f42009-08-17 23:55:24 -04002962 /* skip PAs this normalized request doesn't overlap with */
2963 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002964 spin_unlock(&pa->pa_lock);
2965 continue;
2966 }
2967 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
2968
Eric Sandeen38877f42009-08-17 23:55:24 -04002969 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05002970 if (pa_end <= ac->ac_o_ex.fe_logical) {
2971 BUG_ON(pa_end < start);
2972 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04002973 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002974 BUG_ON(pa->pa_lstart > end);
2975 end = pa->pa_lstart;
2976 }
2977 spin_unlock(&pa->pa_lock);
2978 }
2979 rcu_read_unlock();
2980 size = end - start;
2981
2982 /* XXX: extra loop to check we really don't overlap preallocations */
2983 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04002984 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002985 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002986
Alex Tomasc9de5602008-01-29 00:19:52 -05002987 spin_lock(&pa->pa_lock);
2988 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002989 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
2990 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002991 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
2992 }
2993 spin_unlock(&pa->pa_lock);
2994 }
2995 rcu_read_unlock();
2996
2997 if (start + size <= ac->ac_o_ex.fe_logical &&
2998 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002999 ext4_msg(ac->ac_sb, KERN_ERR,
3000 "start %lu, size %lu, fe_logical %lu",
3001 (unsigned long) start, (unsigned long) size,
3002 (unsigned long) ac->ac_o_ex.fe_logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05003003 }
3004 BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3005 start > ac->ac_o_ex.fe_logical);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003006 BUG_ON(size <= 0 || size > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003007
3008 /* now prepare goal request */
3009
3010 /* XXX: is it better to align blocks WRT to logical
3011 * placement or satisfy big request as is */
3012 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003013 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003014
3015 /* define goal start in order to merge */
3016 if (ar->pright && (ar->lright == (start + size))) {
3017 /* merge to the right */
3018 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3019 &ac->ac_f_ex.fe_group,
3020 &ac->ac_f_ex.fe_start);
3021 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3022 }
3023 if (ar->pleft && (ar->lleft + 1 == start)) {
3024 /* merge to the left */
3025 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3026 &ac->ac_f_ex.fe_group,
3027 &ac->ac_f_ex.fe_start);
3028 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3029 }
3030
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003031 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003032 (unsigned) orig_size, (unsigned) start);
3033}
3034
3035static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3036{
3037 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3038
3039 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3040 atomic_inc(&sbi->s_bal_reqs);
3041 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003042 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003043 atomic_inc(&sbi->s_bal_success);
3044 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3045 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3046 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3047 atomic_inc(&sbi->s_bal_goals);
3048 if (ac->ac_found > sbi->s_mb_max_to_scan)
3049 atomic_inc(&sbi->s_bal_breaks);
3050 }
3051
Theodore Ts'o296c3552009-09-30 00:32:42 -04003052 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3053 trace_ext4_mballoc_alloc(ac);
3054 else
3055 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003056}
3057
3058/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003059 * Called on failure; free up any blocks from the inode PA for this
3060 * context. We don't need this for MB_GROUP_PA because we only change
3061 * pa_free in ext4_mb_release_context(), but on failure, we've already
3062 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3063 */
3064static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3065{
3066 struct ext4_prealloc_space *pa = ac->ac_pa;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003067
Zheng Liu400db9d2012-05-28 17:53:53 -04003068 if (pa && pa->pa_type == MB_INODE_PA)
3069 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003070}
3071
3072/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003073 * use blocks preallocated to inode
3074 */
3075static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3076 struct ext4_prealloc_space *pa)
3077{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003078 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003079 ext4_fsblk_t start;
3080 ext4_fsblk_t end;
3081 int len;
3082
3083 /* found preallocated blocks, use them */
3084 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003085 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3086 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3087 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003088 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3089 &ac->ac_b_ex.fe_start);
3090 ac->ac_b_ex.fe_len = len;
3091 ac->ac_status = AC_STATUS_FOUND;
3092 ac->ac_pa = pa;
3093
3094 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003095 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003096 BUG_ON(pa->pa_free < len);
3097 pa->pa_free -= len;
3098
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003099 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003100}
3101
3102/*
3103 * use blocks preallocated to locality group
3104 */
3105static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3106 struct ext4_prealloc_space *pa)
3107{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003108 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003109
Alex Tomasc9de5602008-01-29 00:19:52 -05003110 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3111 &ac->ac_b_ex.fe_group,
3112 &ac->ac_b_ex.fe_start);
3113 ac->ac_b_ex.fe_len = len;
3114 ac->ac_status = AC_STATUS_FOUND;
3115 ac->ac_pa = pa;
3116
3117 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003118 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003119 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003120 * in on-disk bitmap -- see ext4_mb_release_context()
3121 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003122 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003123 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003124}
3125
3126/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003127 * Return the prealloc space that have minimal distance
3128 * from the goal block. @cpa is the prealloc
3129 * space that is having currently known minimal distance
3130 * from the goal block.
3131 */
3132static struct ext4_prealloc_space *
3133ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3134 struct ext4_prealloc_space *pa,
3135 struct ext4_prealloc_space *cpa)
3136{
3137 ext4_fsblk_t cur_distance, new_distance;
3138
3139 if (cpa == NULL) {
3140 atomic_inc(&pa->pa_count);
3141 return pa;
3142 }
3143 cur_distance = abs(goal_block - cpa->pa_pstart);
3144 new_distance = abs(goal_block - pa->pa_pstart);
3145
Coly Li5a54b2f2011-02-24 14:10:05 -05003146 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003147 return cpa;
3148
3149 /* drop the previous reference */
3150 atomic_dec(&cpa->pa_count);
3151 atomic_inc(&pa->pa_count);
3152 return pa;
3153}
3154
3155/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003156 * search goal blocks in preallocated space
3157 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003158static noinline_for_stack int
3159ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003160{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003161 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003162 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003163 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3164 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003165 struct ext4_prealloc_space *pa, *cpa = NULL;
3166 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003167
3168 /* only data can be preallocated */
3169 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3170 return 0;
3171
3172 /* first, try per-file preallocation */
3173 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003174 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003175
3176 /* all fields in this condition don't change,
3177 * so we can skip locking for them */
3178 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003179 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3180 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003181 continue;
3182
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003183 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003184 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003185 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3186 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003187 continue;
3188
Alex Tomasc9de5602008-01-29 00:19:52 -05003189 /* found preallocated blocks, use them */
3190 spin_lock(&pa->pa_lock);
3191 if (pa->pa_deleted == 0 && pa->pa_free) {
3192 atomic_inc(&pa->pa_count);
3193 ext4_mb_use_inode_pa(ac, pa);
3194 spin_unlock(&pa->pa_lock);
3195 ac->ac_criteria = 10;
3196 rcu_read_unlock();
3197 return 1;
3198 }
3199 spin_unlock(&pa->pa_lock);
3200 }
3201 rcu_read_unlock();
3202
3203 /* can we use group allocation? */
3204 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3205 return 0;
3206
3207 /* inode may have no locality group for some reason */
3208 lg = ac->ac_lg;
3209 if (lg == NULL)
3210 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003211 order = fls(ac->ac_o_ex.fe_len) - 1;
3212 if (order > PREALLOC_TB_SIZE - 1)
3213 /* The max size of hash table is PREALLOC_TB_SIZE */
3214 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003215
Akinobu Mitabda00de2010-03-03 23:53:25 -05003216 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003217 /*
3218 * search for the prealloc space that is having
3219 * minimal distance from the goal block.
3220 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003221 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3222 rcu_read_lock();
3223 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3224 pa_inode_list) {
3225 spin_lock(&pa->pa_lock);
3226 if (pa->pa_deleted == 0 &&
3227 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003228
3229 cpa = ext4_mb_check_group_pa(goal_block,
3230 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003231 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003232 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003233 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003234 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003235 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003236 if (cpa) {
3237 ext4_mb_use_group_pa(ac, cpa);
3238 ac->ac_criteria = 20;
3239 return 1;
3240 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003241 return 0;
3242}
3243
3244/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003245 * the function goes through all block freed in the group
3246 * but not yet committed and marks them used in in-core bitmap.
3247 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003248 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003249 */
3250static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3251 ext4_group_t group)
3252{
3253 struct rb_node *n;
3254 struct ext4_group_info *grp;
3255 struct ext4_free_data *entry;
3256
3257 grp = ext4_get_group_info(sb, group);
3258 n = rb_first(&(grp->bb_free_root));
3259
3260 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003261 entry = rb_entry(n, struct ext4_free_data, efd_node);
3262 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003263 n = rb_next(n);
3264 }
3265 return;
3266}
3267
3268/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003269 * the function goes through all preallocation in this group and marks them
3270 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003271 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003272 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003273static noinline_for_stack
3274void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003275 ext4_group_t group)
3276{
3277 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3278 struct ext4_prealloc_space *pa;
3279 struct list_head *cur;
3280 ext4_group_t groupnr;
3281 ext4_grpblk_t start;
3282 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003283 int len;
3284
3285 /* all form of preallocation discards first load group,
3286 * so the only competing code is preallocation use.
3287 * we don't need any locking here
3288 * notice we do NOT ignore preallocations with pa_deleted
3289 * otherwise we could leave used blocks available for
3290 * allocation in buddy when concurrent ext4_mb_put_pa()
3291 * is dropping preallocation
3292 */
3293 list_for_each(cur, &grp->bb_prealloc_list) {
3294 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3295 spin_lock(&pa->pa_lock);
3296 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3297 &groupnr, &start);
3298 len = pa->pa_len;
3299 spin_unlock(&pa->pa_lock);
3300 if (unlikely(len == 0))
3301 continue;
3302 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003303 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003304 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003305 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003306 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003307}
3308
3309static void ext4_mb_pa_callback(struct rcu_head *head)
3310{
3311 struct ext4_prealloc_space *pa;
3312 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3313 kmem_cache_free(ext4_pspace_cachep, pa);
3314}
3315
3316/*
3317 * drops a reference to preallocated space descriptor
3318 * if this was the last reference and the space is consumed
3319 */
3320static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3321 struct super_block *sb, struct ext4_prealloc_space *pa)
3322{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003323 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003324 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003325
3326 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3327 return;
3328
3329 /* in this short window concurrent discard can set pa_deleted */
3330 spin_lock(&pa->pa_lock);
3331 if (pa->pa_deleted == 1) {
3332 spin_unlock(&pa->pa_lock);
3333 return;
3334 }
3335
3336 pa->pa_deleted = 1;
3337 spin_unlock(&pa->pa_lock);
3338
Eric Sandeend33a1972009-03-16 23:25:40 -04003339 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003340 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003341 * If doing group-based preallocation, pa_pstart may be in the
3342 * next group when pa is used up
3343 */
3344 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003345 grp_blk--;
3346
3347 ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003348
3349 /*
3350 * possible race:
3351 *
3352 * P1 (buddy init) P2 (regular allocation)
3353 * find block B in PA
3354 * copy on-disk bitmap to buddy
3355 * mark B in on-disk bitmap
3356 * drop PA from group
3357 * mark all PAs in buddy
3358 *
3359 * thus, P1 initializes buddy with B available. to prevent this
3360 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3361 * against that pair
3362 */
3363 ext4_lock_group(sb, grp);
3364 list_del(&pa->pa_group_list);
3365 ext4_unlock_group(sb, grp);
3366
3367 spin_lock(pa->pa_obj_lock);
3368 list_del_rcu(&pa->pa_inode_list);
3369 spin_unlock(pa->pa_obj_lock);
3370
3371 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3372}
3373
3374/*
3375 * creates new preallocated space for given inode
3376 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003377static noinline_for_stack int
3378ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003379{
3380 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003381 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003382 struct ext4_prealloc_space *pa;
3383 struct ext4_group_info *grp;
3384 struct ext4_inode_info *ei;
3385
3386 /* preallocate only when found space is larger then requested */
3387 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3388 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3389 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3390
3391 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3392 if (pa == NULL)
3393 return -ENOMEM;
3394
3395 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3396 int winl;
3397 int wins;
3398 int win;
3399 int offs;
3400
3401 /* we can't allocate as much as normalizer wants.
3402 * so, found space must get proper lstart
3403 * to cover original request */
3404 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3405 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3406
3407 /* we're limited by original request in that
3408 * logical block must be covered any way
3409 * winl is window we can move our chunk within */
3410 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3411
3412 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003413 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003414
3415 /* the smallest one defines real window */
3416 win = min(winl, wins);
3417
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003418 offs = ac->ac_o_ex.fe_logical %
3419 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003420 if (offs && offs < win)
3421 win = offs;
3422
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003423 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003424 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003425 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3426 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3427 }
3428
3429 /* preallocation can change ac_b_ex, thus we store actually
3430 * allocated blocks for history */
3431 ac->ac_f_ex = ac->ac_b_ex;
3432
3433 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3434 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3435 pa->pa_len = ac->ac_b_ex.fe_len;
3436 pa->pa_free = pa->pa_len;
3437 atomic_set(&pa->pa_count, 1);
3438 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003439 INIT_LIST_HEAD(&pa->pa_inode_list);
3440 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003441 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003442 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003443
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003444 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003445 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003446 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003447
3448 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003449 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003450
3451 ei = EXT4_I(ac->ac_inode);
3452 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3453
3454 pa->pa_obj_lock = &ei->i_prealloc_lock;
3455 pa->pa_inode = ac->ac_inode;
3456
3457 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3458 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3459 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3460
3461 spin_lock(pa->pa_obj_lock);
3462 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3463 spin_unlock(pa->pa_obj_lock);
3464
3465 return 0;
3466}
3467
3468/*
3469 * creates new preallocated space for locality group inodes belongs to
3470 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003471static noinline_for_stack int
3472ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003473{
3474 struct super_block *sb = ac->ac_sb;
3475 struct ext4_locality_group *lg;
3476 struct ext4_prealloc_space *pa;
3477 struct ext4_group_info *grp;
3478
3479 /* preallocate only when found space is larger then requested */
3480 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3481 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3482 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3483
3484 BUG_ON(ext4_pspace_cachep == NULL);
3485 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3486 if (pa == NULL)
3487 return -ENOMEM;
3488
3489 /* preallocation can change ac_b_ex, thus we store actually
3490 * allocated blocks for history */
3491 ac->ac_f_ex = ac->ac_b_ex;
3492
3493 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3494 pa->pa_lstart = pa->pa_pstart;
3495 pa->pa_len = ac->ac_b_ex.fe_len;
3496 pa->pa_free = pa->pa_len;
3497 atomic_set(&pa->pa_count, 1);
3498 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003499 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003500 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003501 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003502 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003503
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003504 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003505 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3506 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003507
3508 ext4_mb_use_group_pa(ac, pa);
3509 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3510
3511 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3512 lg = ac->ac_lg;
3513 BUG_ON(lg == NULL);
3514
3515 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3516 pa->pa_inode = NULL;
3517
3518 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3519 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3520 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3521
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003522 /*
3523 * We will later add the new pa to the right bucket
3524 * after updating the pa_free in ext4_mb_release_context
3525 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003526 return 0;
3527}
3528
3529static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3530{
3531 int err;
3532
3533 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3534 err = ext4_mb_new_group_pa(ac);
3535 else
3536 err = ext4_mb_new_inode_pa(ac);
3537 return err;
3538}
3539
3540/*
3541 * finds all unused blocks in on-disk bitmap, frees them in
3542 * in-core bitmap and buddy.
3543 * @pa must be unlinked from inode and group lists, so that
3544 * nobody else can find/use it.
3545 * the caller MUST hold group/inode locks.
3546 * TODO: optimize the case when there are no in-core structures yet
3547 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003548static noinline_for_stack int
3549ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003550 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003551{
Alex Tomasc9de5602008-01-29 00:19:52 -05003552 struct super_block *sb = e4b->bd_sb;
3553 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003554 unsigned int end;
3555 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003556 ext4_group_t group;
3557 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003558 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003559 int err = 0;
3560 int free = 0;
3561
3562 BUG_ON(pa->pa_deleted == 0);
3563 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003564 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003565 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3566 end = bit + pa->pa_len;
3567
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003569 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003570 if (bit >= end)
3571 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003572 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003573 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003574 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3575 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003576 free += next - bit;
3577
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003578 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003579 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3580 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003581 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003582 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3583 bit = next + 1;
3584 }
3585 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003586 ext4_msg(e4b->bd_sb, KERN_CRIT,
3587 "pa %p: logic %lu, phys. %lu, len %lu",
3588 pa, (unsigned long) pa->pa_lstart,
3589 (unsigned long) pa->pa_pstart,
3590 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003591 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003592 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003593 /*
3594 * pa is already deleted so we use the value obtained
3595 * from the bitmap and continue.
3596 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003597 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003598 atomic_add(free, &sbi->s_mb_discarded);
3599
3600 return err;
3601}
3602
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003603static noinline_for_stack int
3604ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003605 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003606{
Alex Tomasc9de5602008-01-29 00:19:52 -05003607 struct super_block *sb = e4b->bd_sb;
3608 ext4_group_t group;
3609 ext4_grpblk_t bit;
3610
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003611 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003612 BUG_ON(pa->pa_deleted == 0);
3613 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3614 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3615 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3616 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003617 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003618
3619 return 0;
3620}
3621
3622/*
3623 * releases all preallocations in given group
3624 *
3625 * first, we need to decide discard policy:
3626 * - when do we discard
3627 * 1) ENOSPC
3628 * - how many do we discard
3629 * 1) how many requested
3630 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003631static noinline_for_stack int
3632ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003633 ext4_group_t group, int needed)
3634{
3635 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3636 struct buffer_head *bitmap_bh = NULL;
3637 struct ext4_prealloc_space *pa, *tmp;
3638 struct list_head list;
3639 struct ext4_buddy e4b;
3640 int err;
3641 int busy = 0;
3642 int free = 0;
3643
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003644 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003645
3646 if (list_empty(&grp->bb_prealloc_list))
3647 return 0;
3648
Theodore Ts'o574ca172008-07-11 19:27:31 -04003649 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003650 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003651 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003652 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003653 }
3654
3655 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003656 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003657 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003658 put_bh(bitmap_bh);
3659 return 0;
3660 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003661
3662 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003663 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003664
Alex Tomasc9de5602008-01-29 00:19:52 -05003665 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003666repeat:
3667 ext4_lock_group(sb, group);
3668 list_for_each_entry_safe(pa, tmp,
3669 &grp->bb_prealloc_list, pa_group_list) {
3670 spin_lock(&pa->pa_lock);
3671 if (atomic_read(&pa->pa_count)) {
3672 spin_unlock(&pa->pa_lock);
3673 busy = 1;
3674 continue;
3675 }
3676 if (pa->pa_deleted) {
3677 spin_unlock(&pa->pa_lock);
3678 continue;
3679 }
3680
3681 /* seems this one can be freed ... */
3682 pa->pa_deleted = 1;
3683
3684 /* we can trust pa_free ... */
3685 free += pa->pa_free;
3686
3687 spin_unlock(&pa->pa_lock);
3688
3689 list_del(&pa->pa_group_list);
3690 list_add(&pa->u.pa_tmp_list, &list);
3691 }
3692
3693 /* if we still need more blocks and some PAs were used, try again */
3694 if (free < needed && busy) {
3695 busy = 0;
3696 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003697 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003698 goto repeat;
3699 }
3700
3701 /* found anything to free? */
3702 if (list_empty(&list)) {
3703 BUG_ON(free != 0);
3704 goto out;
3705 }
3706
3707 /* now free all selected PAs */
3708 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3709
3710 /* remove from object (inode or locality group) */
3711 spin_lock(pa->pa_obj_lock);
3712 list_del_rcu(&pa->pa_inode_list);
3713 spin_unlock(pa->pa_obj_lock);
3714
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003715 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003716 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003717 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003718 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003719
3720 list_del(&pa->u.pa_tmp_list);
3721 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3722 }
3723
3724out:
3725 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003726 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003727 put_bh(bitmap_bh);
3728 return free;
3729}
3730
3731/*
3732 * releases all non-used preallocated blocks for given inode
3733 *
3734 * It's important to discard preallocations under i_data_sem
3735 * We don't want another block to be served from the prealloc
3736 * space when we are discarding the inode prealloc space.
3737 *
3738 * FIXME!! Make sure it is valid at all the call sites
3739 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003740void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003741{
3742 struct ext4_inode_info *ei = EXT4_I(inode);
3743 struct super_block *sb = inode->i_sb;
3744 struct buffer_head *bitmap_bh = NULL;
3745 struct ext4_prealloc_space *pa, *tmp;
3746 ext4_group_t group = 0;
3747 struct list_head list;
3748 struct ext4_buddy e4b;
3749 int err;
3750
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003751 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003752 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3753 return;
3754 }
3755
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003756 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003757 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003758
3759 INIT_LIST_HEAD(&list);
3760
3761repeat:
3762 /* first, collect all pa's in the inode */
3763 spin_lock(&ei->i_prealloc_lock);
3764 while (!list_empty(&ei->i_prealloc_list)) {
3765 pa = list_entry(ei->i_prealloc_list.next,
3766 struct ext4_prealloc_space, pa_inode_list);
3767 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3768 spin_lock(&pa->pa_lock);
3769 if (atomic_read(&pa->pa_count)) {
3770 /* this shouldn't happen often - nobody should
3771 * use preallocation while we're discarding it */
3772 spin_unlock(&pa->pa_lock);
3773 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003774 ext4_msg(sb, KERN_ERR,
3775 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05003776 WARN_ON(1);
3777 schedule_timeout_uninterruptible(HZ);
3778 goto repeat;
3779
3780 }
3781 if (pa->pa_deleted == 0) {
3782 pa->pa_deleted = 1;
3783 spin_unlock(&pa->pa_lock);
3784 list_del_rcu(&pa->pa_inode_list);
3785 list_add(&pa->u.pa_tmp_list, &list);
3786 continue;
3787 }
3788
3789 /* someone is deleting pa right now */
3790 spin_unlock(&pa->pa_lock);
3791 spin_unlock(&ei->i_prealloc_lock);
3792
3793 /* we have to wait here because pa_deleted
3794 * doesn't mean pa is already unlinked from
3795 * the list. as we might be called from
3796 * ->clear_inode() the inode will get freed
3797 * and concurrent thread which is unlinking
3798 * pa from inode's list may access already
3799 * freed memory, bad-bad-bad */
3800
3801 /* XXX: if this happens too often, we can
3802 * add a flag to force wait only in case
3803 * of ->clear_inode(), but not in case of
3804 * regular truncate */
3805 schedule_timeout_uninterruptible(HZ);
3806 goto repeat;
3807 }
3808 spin_unlock(&ei->i_prealloc_lock);
3809
3810 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003811 BUG_ON(pa->pa_type != MB_INODE_PA);
Alex Tomasc9de5602008-01-29 00:19:52 -05003812 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
3813
3814 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003815 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003816 ext4_error(sb, "Error loading buddy information for %u",
3817 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003818 continue;
3819 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003820
Theodore Ts'o574ca172008-07-11 19:27:31 -04003821 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003822 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003823 ext4_error(sb, "Error reading block bitmap for %u",
3824 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003825 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003826 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05003827 }
3828
3829 ext4_lock_group(sb, group);
3830 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003831 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 ext4_unlock_group(sb, group);
3833
Jing Zhange39e07f2010-05-14 00:00:00 -04003834 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003835 put_bh(bitmap_bh);
3836
3837 list_del(&pa->u.pa_tmp_list);
3838 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3839 }
3840}
3841
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003842#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05003843static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3844{
3845 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003846 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003847
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05003848 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05003849 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04003850 return;
3851
Joe Perches7f6a11e2012-03-19 23:09:43 -04003852 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003853 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04003854 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003855 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003856 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003857 "goal %lu/%lu/%lu@%lu, "
3858 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05003859 (unsigned long)ac->ac_o_ex.fe_group,
3860 (unsigned long)ac->ac_o_ex.fe_start,
3861 (unsigned long)ac->ac_o_ex.fe_len,
3862 (unsigned long)ac->ac_o_ex.fe_logical,
3863 (unsigned long)ac->ac_g_ex.fe_group,
3864 (unsigned long)ac->ac_g_ex.fe_start,
3865 (unsigned long)ac->ac_g_ex.fe_len,
3866 (unsigned long)ac->ac_g_ex.fe_logical,
3867 (unsigned long)ac->ac_b_ex.fe_group,
3868 (unsigned long)ac->ac_b_ex.fe_start,
3869 (unsigned long)ac->ac_b_ex.fe_len,
3870 (unsigned long)ac->ac_b_ex.fe_logical,
3871 (int)ac->ac_criteria);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003872 ext4_msg(ac->ac_sb, KERN_ERR, "%lu scanned, %d found",
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003873 ac->ac_ex_scanned, ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04003874 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04003875 ngroups = ext4_get_groups_count(sb);
3876 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003877 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
3878 struct ext4_prealloc_space *pa;
3879 ext4_grpblk_t start;
3880 struct list_head *cur;
3881 ext4_lock_group(sb, i);
3882 list_for_each(cur, &grp->bb_prealloc_list) {
3883 pa = list_entry(cur, struct ext4_prealloc_space,
3884 pa_group_list);
3885 spin_lock(&pa->pa_lock);
3886 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3887 NULL, &start);
3888 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04003889 printk(KERN_ERR "PA:%u:%d:%u \n", i,
3890 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003891 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04003892 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05003893
3894 if (grp->bb_free == 0)
3895 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04003896 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05003897 i, grp->bb_free, grp->bb_fragments);
3898 }
3899 printk(KERN_ERR "\n");
3900}
3901#else
3902static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
3903{
3904 return;
3905}
3906#endif
3907
3908/*
3909 * We use locality group preallocation for small size file. The size of the
3910 * file is determined by the current size or the resulting size after
3911 * allocation which ever is larger
3912 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003913 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05003914 */
3915static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
3916{
3917 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3918 int bsbits = ac->ac_sb->s_blocksize_bits;
3919 loff_t size, isize;
3920
3921 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3922 return;
3923
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003924 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3925 return;
3926
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003927 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04003928 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
3929 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003930
Theodore Ts'o50797482009-09-18 13:34:02 -04003931 if ((size == isize) &&
3932 !ext4_fs_is_busy(sbi) &&
3933 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
3934 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
3935 return;
3936 }
3937
Robin Dongebbe0272011-10-26 05:14:27 -04003938 if (sbi->s_mb_group_prealloc <= 0) {
3939 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
3940 return;
3941 }
3942
Alex Tomasc9de5602008-01-29 00:19:52 -05003943 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04003944 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05003945 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003946 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003947 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04003948 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003949
3950 BUG_ON(ac->ac_lg != NULL);
3951 /*
3952 * locality group prealloc space are per cpu. The reason for having
3953 * per cpu locality group is to reduce the contention between block
3954 * request from multiple CPUs.
3955 */
Christoph Lameterca0c9582009-10-03 19:48:22 +09003956 ac->ac_lg = __this_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05003957
3958 /* we're going to use group allocation */
3959 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
3960
3961 /* serialize all allocations in the group */
3962 mutex_lock(&ac->ac_lg->lg_mutex);
3963}
3964
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003965static noinline_for_stack int
3966ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003967 struct ext4_allocation_request *ar)
3968{
3969 struct super_block *sb = ar->inode->i_sb;
3970 struct ext4_sb_info *sbi = EXT4_SB(sb);
3971 struct ext4_super_block *es = sbi->s_es;
3972 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003973 unsigned int len;
3974 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05003975 ext4_grpblk_t block;
3976
3977 /* we can't allocate > group size */
3978 len = ar->len;
3979
3980 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05003981 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
3982 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003983
3984 /* start searching from the goal */
3985 goal = ar->goal;
3986 if (goal < le32_to_cpu(es->s_first_data_block) ||
3987 goal >= ext4_blocks_count(es))
3988 goal = le32_to_cpu(es->s_first_data_block);
3989 ext4_get_group_no_and_offset(sb, goal, &group, &block);
3990
3991 /* set up allocation goals */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003992 ac->ac_b_ex.fe_logical = ar->logical & ~(sbi->s_cluster_ratio - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05003993 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05003994 ac->ac_sb = sb;
3995 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003996 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05003997 ac->ac_o_ex.fe_group = group;
3998 ac->ac_o_ex.fe_start = block;
3999 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004000 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004001 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004002
4003 /* we have to define context: we'll we work with a file or
4004 * locality group. this is a policy, actually */
4005 ext4_mb_group_or_file(ac);
4006
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004007 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004008 "left: %u/%u, right %u/%u to %swritable\n",
4009 (unsigned) ar->len, (unsigned) ar->logical,
4010 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4011 (unsigned) ar->lleft, (unsigned) ar->pleft,
4012 (unsigned) ar->lright, (unsigned) ar->pright,
4013 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4014 return 0;
4015
4016}
4017
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004018static noinline_for_stack void
4019ext4_mb_discard_lg_preallocations(struct super_block *sb,
4020 struct ext4_locality_group *lg,
4021 int order, int total_entries)
4022{
4023 ext4_group_t group = 0;
4024 struct ext4_buddy e4b;
4025 struct list_head discard_list;
4026 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004027
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004028 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004029
4030 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004031
4032 spin_lock(&lg->lg_prealloc_lock);
4033 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4034 pa_inode_list) {
4035 spin_lock(&pa->pa_lock);
4036 if (atomic_read(&pa->pa_count)) {
4037 /*
4038 * This is the pa that we just used
4039 * for block allocation. So don't
4040 * free that
4041 */
4042 spin_unlock(&pa->pa_lock);
4043 continue;
4044 }
4045 if (pa->pa_deleted) {
4046 spin_unlock(&pa->pa_lock);
4047 continue;
4048 }
4049 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004050 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004051
4052 /* seems this one can be freed ... */
4053 pa->pa_deleted = 1;
4054 spin_unlock(&pa->pa_lock);
4055
4056 list_del_rcu(&pa->pa_inode_list);
4057 list_add(&pa->u.pa_tmp_list, &discard_list);
4058
4059 total_entries--;
4060 if (total_entries <= 5) {
4061 /*
4062 * we want to keep only 5 entries
4063 * allowing it to grow to 8. This
4064 * mak sure we don't call discard
4065 * soon for this list.
4066 */
4067 break;
4068 }
4069 }
4070 spin_unlock(&lg->lg_prealloc_lock);
4071
4072 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4073
4074 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4075 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004076 ext4_error(sb, "Error loading buddy information for %u",
4077 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004078 continue;
4079 }
4080 ext4_lock_group(sb, group);
4081 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004082 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004083 ext4_unlock_group(sb, group);
4084
Jing Zhange39e07f2010-05-14 00:00:00 -04004085 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004086 list_del(&pa->u.pa_tmp_list);
4087 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4088 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004089}
4090
4091/*
4092 * We have incremented pa_count. So it cannot be freed at this
4093 * point. Also we hold lg_mutex. So no parallel allocation is
4094 * possible from this lg. That means pa_free cannot be updated.
4095 *
4096 * A parallel ext4_mb_discard_group_preallocations is possible.
4097 * which can cause the lg_prealloc_list to be updated.
4098 */
4099
4100static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4101{
4102 int order, added = 0, lg_prealloc_count = 1;
4103 struct super_block *sb = ac->ac_sb;
4104 struct ext4_locality_group *lg = ac->ac_lg;
4105 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4106
4107 order = fls(pa->pa_free) - 1;
4108 if (order > PREALLOC_TB_SIZE - 1)
4109 /* The max size of hash table is PREALLOC_TB_SIZE */
4110 order = PREALLOC_TB_SIZE - 1;
4111 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004112 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004113 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4114 pa_inode_list) {
4115 spin_lock(&tmp_pa->pa_lock);
4116 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004117 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004118 continue;
4119 }
4120 if (!added && pa->pa_free < tmp_pa->pa_free) {
4121 /* Add to the tail of the previous entry */
4122 list_add_tail_rcu(&pa->pa_inode_list,
4123 &tmp_pa->pa_inode_list);
4124 added = 1;
4125 /*
4126 * we want to count the total
4127 * number of entries in the list
4128 */
4129 }
4130 spin_unlock(&tmp_pa->pa_lock);
4131 lg_prealloc_count++;
4132 }
4133 if (!added)
4134 list_add_tail_rcu(&pa->pa_inode_list,
4135 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004136 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004137
4138 /* Now trim the list to be not more than 8 elements */
4139 if (lg_prealloc_count > 8) {
4140 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004141 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004142 return;
4143 }
4144 return ;
4145}
4146
Alex Tomasc9de5602008-01-29 00:19:52 -05004147/*
4148 * release all resource we used in allocation
4149 */
4150static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4151{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004152 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004153 struct ext4_prealloc_space *pa = ac->ac_pa;
4154 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004155 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004156 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004157 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004158 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4159 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004160 pa->pa_free -= ac->ac_b_ex.fe_len;
4161 pa->pa_len -= ac->ac_b_ex.fe_len;
4162 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004163 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004164 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004165 if (pa) {
4166 /*
4167 * We want to add the pa to the right bucket.
4168 * Remove it from the list and while adding
4169 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004170 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004171 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004172 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004173 spin_lock(pa->pa_obj_lock);
4174 list_del_rcu(&pa->pa_inode_list);
4175 spin_unlock(pa->pa_obj_lock);
4176 ext4_mb_add_n_trim(ac);
4177 }
4178 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4179 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004180 if (ac->ac_bitmap_page)
4181 page_cache_release(ac->ac_bitmap_page);
4182 if (ac->ac_buddy_page)
4183 page_cache_release(ac->ac_buddy_page);
4184 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4185 mutex_unlock(&ac->ac_lg->lg_mutex);
4186 ext4_mb_collect_stats(ac);
4187 return 0;
4188}
4189
4190static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4191{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004192 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004193 int ret;
4194 int freed = 0;
4195
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004196 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004197 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004198 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4199 freed += ret;
4200 needed -= ret;
4201 }
4202
4203 return freed;
4204}
4205
4206/*
4207 * Main entry point into mballoc to allocate blocks
4208 * it tries to use preallocation first, then falls back
4209 * to usual allocation
4210 */
4211ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004212 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004213{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004214 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004215 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004216 struct ext4_sb_info *sbi;
4217 struct super_block *sb;
4218 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004219 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004220 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004221
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004222 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004223 sb = ar->inode->i_sb;
4224 sbi = EXT4_SB(sb);
4225
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004226 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004227
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004228 /* Allow to use superuser reservation for quota file */
4229 if (IS_NOQUOTA(ar->inode))
4230 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4231
Mingming Cao60e58e02009-01-22 18:13:05 +01004232 /*
4233 * For delayed allocation, we could skip the ENOSPC and
4234 * EDQUOT check, as blocks and quotas have been already
4235 * reserved when data being copied into pagecache.
4236 */
Theodore Ts'of2321092011-01-10 12:12:36 -05004237 if (ext4_test_inode_state(ar->inode, EXT4_STATE_DELALLOC_RESERVED))
Mingming Cao60e58e02009-01-22 18:13:05 +01004238 ar->flags |= EXT4_MB_DELALLOC_RESERVED;
4239 else {
4240 /* Without delayed allocation we need to verify
4241 * there is enough free blocks to do block allocation
4242 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004243 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004244 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004245 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004246
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004247 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004248 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004249 ar->len = ar->len >> 1;
4250 }
4251 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004252 *errp = -ENOSPC;
4253 return 0;
4254 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004255 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004256 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004257 dquot_alloc_block_nofail(ar->inode,
4258 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004259 } else {
4260 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004261 dquot_alloc_block(ar->inode,
4262 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004263
4264 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4265 ar->len--;
4266 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004267 }
4268 inquota = ar->len;
4269 if (ar->len == 0) {
4270 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004271 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004272 }
Mingming Caod2a17632008-07-14 17:52:37 -04004273 }
Mingming Caod2a17632008-07-14 17:52:37 -04004274
Wei Yongjun85556c92012-09-26 20:43:37 -04004275 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004276 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004277 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004278 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004279 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004280 }
4281
Eric Sandeen256bdb42008-02-10 01:13:33 -05004282 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004283 if (*errp) {
4284 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004285 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004286 }
4287
Eric Sandeen256bdb42008-02-10 01:13:33 -05004288 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4289 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004290 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4291 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004292repeat:
4293 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004294 *errp = ext4_mb_regular_allocator(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004295 if (*errp) {
4296 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004297 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004298 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004299
4300 /* as we've just preallocated more space than
4301 * user requested orinally, we store allocated
4302 * space in a special descriptor */
Eric Sandeen256bdb42008-02-10 01:13:33 -05004303 if (ac->ac_status == AC_STATUS_FOUND &&
4304 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4305 ext4_mb_new_preallocation(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004306 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004307 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004308 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004309 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004310 /*
4311 * drop the reference that we took
4312 * in ext4_mb_use_best_found
4313 */
4314 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004315 ac->ac_b_ex.fe_group = 0;
4316 ac->ac_b_ex.fe_start = 0;
4317 ac->ac_b_ex.fe_len = 0;
4318 ac->ac_status = AC_STATUS_CONTINUE;
4319 goto repeat;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004320 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004321 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004322 goto errout;
4323 } else {
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004324 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4325 ar->len = ac->ac_b_ex.fe_len;
4326 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004327 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004328 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004329 if (freed)
4330 goto repeat;
4331 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004332 }
4333
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004334errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004335 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004336 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004337 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004338 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004339 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004340 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004341out:
4342 if (ac)
4343 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004344 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004345 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004346 if (!ar->len) {
Theodore Ts'of2321092011-01-10 12:12:36 -05004347 if (!ext4_test_inode_state(ar->inode,
4348 EXT4_STATE_DELALLOC_RESERVED))
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004349 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004350 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004351 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004352 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004353
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004354 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004355
Alex Tomasc9de5602008-01-29 00:19:52 -05004356 return block;
4357}
Alex Tomasc9de5602008-01-29 00:19:52 -05004358
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004359/*
4360 * We can merge two free data extents only if the physical blocks
4361 * are contiguous, AND the extents were freed by the same transaction,
4362 * AND the blocks are associated with the same group.
4363 */
4364static int can_merge(struct ext4_free_data *entry1,
4365 struct ext4_free_data *entry2)
4366{
Bobi Jam18aadd42012-02-20 17:53:02 -05004367 if ((entry1->efd_tid == entry2->efd_tid) &&
4368 (entry1->efd_group == entry2->efd_group) &&
4369 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004370 return 1;
4371 return 0;
4372}
4373
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004374static noinline_for_stack int
4375ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004376 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004377{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004378 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004379 ext4_grpblk_t cluster;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004380 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004381 struct ext4_group_info *db = e4b->bd_info;
4382 struct super_block *sb = e4b->bd_sb;
4383 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004384 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4385 struct rb_node *parent = NULL, *new_node;
4386
Frank Mayhar03901312009-01-07 00:06:22 -05004387 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004388 BUG_ON(e4b->bd_bitmap_page == NULL);
4389 BUG_ON(e4b->bd_buddy_page == NULL);
4390
Bobi Jam18aadd42012-02-20 17:53:02 -05004391 new_node = &new_entry->efd_node;
4392 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004393
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004394 if (!*n) {
4395 /* first free block exent. We need to
4396 protect buddy cache from being freed,
4397 * otherwise we'll refresh it from
4398 * on-disk bitmap and lose not-yet-available
4399 * blocks */
4400 page_cache_get(e4b->bd_buddy_page);
4401 page_cache_get(e4b->bd_bitmap_page);
4402 }
4403 while (*n) {
4404 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004405 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4406 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004407 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004408 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004409 n = &(*n)->rb_right;
4410 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004411 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004412 ext4_group_first_block_no(sb, group) +
4413 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004414 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004415 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004416 }
4417 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004418
4419 rb_link_node(new_node, parent, n);
4420 rb_insert_color(new_node, &db->bb_free_root);
4421
4422 /* Now try to see the extent can be merged to left and right */
4423 node = rb_prev(new_node);
4424 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004425 entry = rb_entry(node, struct ext4_free_data, efd_node);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004426 if (can_merge(entry, new_entry)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004427 new_entry->efd_start_cluster = entry->efd_start_cluster;
4428 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004429 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004430 ext4_journal_callback_del(handle, &entry->efd_jce);
4431 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004432 }
4433 }
4434
4435 node = rb_next(new_node);
4436 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004437 entry = rb_entry(node, struct ext4_free_data, efd_node);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004438 if (can_merge(new_entry, entry)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004439 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004440 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004441 ext4_journal_callback_del(handle, &entry->efd_jce);
4442 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004443 }
4444 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004445 /* Add the extent to transaction's private list */
Bobi Jam18aadd42012-02-20 17:53:02 -05004446 ext4_journal_callback_add(handle, ext4_free_data_callback,
4447 &new_entry->efd_jce);
Alex Tomasc9de5602008-01-29 00:19:52 -05004448 return 0;
4449}
4450
Theodore Ts'o44338712009-11-22 07:44:56 -05004451/**
4452 * ext4_free_blocks() -- Free given blocks and update quota
4453 * @handle: handle for this transaction
4454 * @inode: inode
4455 * @block: start physical block to free
4456 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004457 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004458 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004459void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004460 struct buffer_head *bh, ext4_fsblk_t block,
4461 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004462{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004463 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004464 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004465 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004466 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004467 ext4_grpblk_t bit;
4468 struct buffer_head *gd_bh;
4469 ext4_group_t block_group;
4470 struct ext4_sb_info *sbi;
4471 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004472 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004473 int err = 0;
4474 int ret;
4475
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004476 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004477 if (bh) {
4478 if (block)
4479 BUG_ON(block != bh->b_blocknr);
4480 else
4481 block = bh->b_blocknr;
4482 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004483
Alex Tomasc9de5602008-01-29 00:19:52 -05004484 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004485 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4486 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004487 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004488 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004489 goto error_return;
4490 }
4491
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004492 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004493 trace_ext4_free_blocks(inode, block, count, flags);
4494
4495 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4496 struct buffer_head *tbh = bh;
4497 int i;
4498
4499 BUG_ON(bh && (count > 1));
4500
4501 for (i = 0; i < count; i++) {
4502 if (!bh)
4503 tbh = sb_find_get_block(inode->i_sb,
4504 block + i);
Namhyung Kim87783692010-10-27 21:30:11 -04004505 if (unlikely(!tbh))
4506 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004507 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004508 inode, tbh, block + i);
4509 }
4510 }
4511
Theodore Ts'o60e66792010-05-17 07:00:00 -04004512 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004513 * We need to make sure we don't reuse the freed block until
4514 * after the transaction is committed, which we can do by
4515 * treating the block as metadata, below. We make an
4516 * exception if the inode is to be written in writeback mode
4517 * since writeback mode has weak data consistency guarantees.
4518 */
4519 if (!ext4_should_writeback_data(inode))
4520 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004521
Theodore Ts'o84130192011-09-09 18:50:51 -04004522 /*
4523 * If the extent to be freed does not begin on a cluster
4524 * boundary, we need to deal with partial clusters at the
4525 * beginning and end of the extent. Normally we will free
4526 * blocks at the beginning or the end unless we are explicitly
4527 * requested to avoid doing so.
4528 */
4529 overflow = block & (sbi->s_cluster_ratio - 1);
4530 if (overflow) {
4531 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4532 overflow = sbi->s_cluster_ratio - overflow;
4533 block += overflow;
4534 if (count > overflow)
4535 count -= overflow;
4536 else
4537 return;
4538 } else {
4539 block -= overflow;
4540 count += overflow;
4541 }
4542 }
4543 overflow = count & (sbi->s_cluster_ratio - 1);
4544 if (overflow) {
4545 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4546 if (count > overflow)
4547 count -= overflow;
4548 else
4549 return;
4550 } else
4551 count += sbi->s_cluster_ratio - overflow;
4552 }
4553
Alex Tomasc9de5602008-01-29 00:19:52 -05004554do_more:
4555 overflow = 0;
4556 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4557
4558 /*
4559 * Check to see if we are freeing blocks across a group
4560 * boundary.
4561 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004562 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4563 overflow = EXT4_C2B(sbi, bit) + count -
4564 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004565 count -= overflow;
4566 }
Lukas Czerner810da242013-03-02 17:18:58 -05004567 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004568 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004569 if (!bitmap_bh) {
4570 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004571 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004572 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004573 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004574 if (!gdp) {
4575 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004576 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004577 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004578
4579 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4580 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4581 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004582 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004583 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004584 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004585
Eric Sandeen12062dd2010-02-15 14:19:27 -05004586 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004587 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca2008-05-15 14:43:20 -04004588 /* err = 0. ext4_std_error should be a no op */
4589 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004590 }
4591
4592 BUFFER_TRACE(bitmap_bh, "getting write access");
4593 err = ext4_journal_get_write_access(handle, bitmap_bh);
4594 if (err)
4595 goto error_return;
4596
4597 /*
4598 * We are about to modify some metadata. Call the journal APIs
4599 * to unshare ->b_data if a currently-committing transaction is
4600 * using it
4601 */
4602 BUFFER_TRACE(gd_bh, "get_write_access");
4603 err = ext4_journal_get_write_access(handle, gd_bh);
4604 if (err)
4605 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004606#ifdef AGGRESSIVE_CHECK
4607 {
4608 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004609 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004610 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4611 }
4612#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004613 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004614
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004615 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4616 if (err)
4617 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004618
4619 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004620 struct ext4_free_data *new_entry;
4621 /*
4622 * blocks being freed are metadata. these blocks shouldn't
4623 * be used until this transaction is committed
4624 */
Bobi Jam18aadd42012-02-20 17:53:02 -05004625 new_entry = kmem_cache_alloc(ext4_free_data_cachep, GFP_NOFS);
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004626 if (!new_entry) {
Salman Qazi02b78312012-05-31 23:51:27 -04004627 ext4_mb_unload_buddy(&e4b);
Theodore Ts'ob72143a2010-12-20 07:26:59 -05004628 err = -ENOMEM;
4629 goto error_return;
4630 }
Bobi Jam18aadd42012-02-20 17:53:02 -05004631 new_entry->efd_start_cluster = bit;
4632 new_entry->efd_group = block_group;
4633 new_entry->efd_count = count_clusters;
4634 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004635
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004636 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004637 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004638 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004639 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004640 /* need to update group_info->bb_free and bitmap
4641 * with group lock held. generate_buddy look at
4642 * them with group lock_held
4643 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004644 if (test_opt(sb, DISCARD)) {
4645 err = ext4_issue_discard(sb, block_group, bit, count);
4646 if (err && err != -EOPNOTSUPP)
4647 ext4_msg(sb, KERN_WARNING, "discard request in"
4648 " group:%d block:%d count:%lu failed"
4649 " with %d", block_group, bit, count,
4650 err);
4651 }
4652
4653
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004654 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004655 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4656 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004657 }
4658
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004659 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4660 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004661 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004662 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004663 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004664 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004665
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004666 if (sbi->s_log_groups_per_flex) {
4667 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004668 atomic64_add(count_clusters,
4669 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004670 }
4671
Jing Zhange39e07f2010-05-14 00:00:00 -04004672 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004673
Aditya Kali7b415bf2011-09-09 19:04:51 -04004674 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
4675 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
4676
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004677 /* We dirtied the bitmap block */
4678 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4679 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4680
Alex Tomasc9de5602008-01-29 00:19:52 -05004681 /* And the group descriptor block */
4682 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004683 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004684 if (!err)
4685 err = ret;
4686
4687 if (overflow && !err) {
4688 block += count;
4689 count = overflow;
4690 put_bh(bitmap_bh);
4691 goto do_more;
4692 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004693error_return:
4694 brelse(bitmap_bh);
4695 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004696 return;
4697}
Lukas Czerner7360d172010-10-27 21:30:12 -04004698
4699/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004700 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004701 * @handle: handle to this transaction
4702 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004703 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004704 * @count: number of blocks to free
4705 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004706 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004707 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004708int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004709 ext4_fsblk_t block, unsigned long count)
4710{
4711 struct buffer_head *bitmap_bh = NULL;
4712 struct buffer_head *gd_bh;
4713 ext4_group_t block_group;
4714 ext4_grpblk_t bit;
4715 unsigned int i;
4716 struct ext4_group_desc *desc;
4717 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004718 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004719 int err = 0, ret, blk_free_count;
4720 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004721
4722 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4723
Yongqiang Yang4740b832011-07-26 21:51:08 -04004724 if (count == 0)
4725 return 0;
4726
Amir Goldstein2846e822011-05-09 10:46:41 -04004727 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004728 /*
4729 * Check to see if we are freeing blocks across a group
4730 * boundary.
4731 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004732 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4733 ext4_warning(sb, "too much blocks added to group %u\n",
4734 block_group);
4735 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004736 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004737 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004738
Amir Goldstein2846e822011-05-09 10:46:41 -04004739 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004740 if (!bitmap_bh) {
4741 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004742 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004743 }
4744
Amir Goldstein2846e822011-05-09 10:46:41 -04004745 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004746 if (!desc) {
4747 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004748 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004749 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004750
4751 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4752 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4753 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4754 in_range(block + count - 1, ext4_inode_table(sb, desc),
4755 sbi->s_itb_per_group)) {
4756 ext4_error(sb, "Adding blocks in system zones - "
4757 "Block = %llu, count = %lu",
4758 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004759 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004760 goto error_return;
4761 }
4762
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004763 BUFFER_TRACE(bitmap_bh, "getting write access");
4764 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004765 if (err)
4766 goto error_return;
4767
4768 /*
4769 * We are about to modify some metadata. Call the journal APIs
4770 * to unshare ->b_data if a currently-committing transaction is
4771 * using it
4772 */
4773 BUFFER_TRACE(gd_bh, "get_write_access");
4774 err = ext4_journal_get_write_access(handle, gd_bh);
4775 if (err)
4776 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004777
Amir Goldstein2846e822011-05-09 10:46:41 -04004778 for (i = 0, blocks_freed = 0; i < count; i++) {
4779 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004780 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004781 ext4_error(sb, "bit already cleared for block %llu",
4782 (ext4_fsblk_t)(block + i));
4783 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4784 } else {
4785 blocks_freed++;
4786 }
4787 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004788
4789 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4790 if (err)
4791 goto error_return;
4792
4793 /*
4794 * need to update group_info->bb_free and bitmap
4795 * with group lock held. generate_buddy look at
4796 * them with group lock_held
4797 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004798 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004799 mb_clear_bits(bitmap_bh->b_data, bit, count);
4800 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004801 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
4802 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04004803 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004804 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04004805 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004806 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05004807 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04004808
4809 if (sbi->s_log_groups_per_flex) {
4810 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004811 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
4812 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04004813 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004814
4815 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04004816
4817 /* We dirtied the bitmap block */
4818 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4819 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4820
4821 /* And the group descriptor block */
4822 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
4823 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4824 if (!err)
4825 err = ret;
4826
4827error_return:
4828 brelse(bitmap_bh);
4829 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004830 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04004831}
4832
4833/**
Lukas Czerner7360d172010-10-27 21:30:12 -04004834 * ext4_trim_extent -- function to TRIM one single free extent in the group
4835 * @sb: super block for the file system
4836 * @start: starting block of the free extent in the alloc. group
4837 * @count: number of blocks to TRIM
4838 * @group: alloc. group we are working with
4839 * @e4b: ext4 buddy for the group
4840 *
4841 * Trim "count" blocks starting at "start" in the "group". To assure that no
4842 * one will allocate those blocks, mark it as used in buddy bitmap. This must
4843 * be called with under the group lock.
4844 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004845static int ext4_trim_extent(struct super_block *sb, int start, int count,
Theodore Ts'od9f34502011-04-30 13:47:24 -04004846 ext4_group_t group, struct ext4_buddy *e4b)
Lukas Czerner7360d172010-10-27 21:30:12 -04004847{
4848 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004849 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004850
Tao Mab3d4c2b2011-07-11 00:01:52 -04004851 trace_ext4_trim_extent(sb, group, start, count);
4852
Lukas Czerner7360d172010-10-27 21:30:12 -04004853 assert_spin_locked(ext4_group_lock_ptr(sb, group));
4854
4855 ex.fe_start = start;
4856 ex.fe_group = group;
4857 ex.fe_len = count;
4858
4859 /*
4860 * Mark blocks used, so no one can reuse them while
4861 * being trimmed.
4862 */
4863 mb_mark_used(e4b, &ex);
4864 ext4_unlock_group(sb, group);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004865 ret = ext4_issue_discard(sb, group, start, count);
Lukas Czerner7360d172010-10-27 21:30:12 -04004866 ext4_lock_group(sb, group);
4867 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004868 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004869}
4870
4871/**
4872 * ext4_trim_all_free -- function to trim all free space in alloc. group
4873 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04004874 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04004875 * @start: first group block to examine
4876 * @max: last group block to examine
4877 * @minblocks: minimum extent block count
4878 *
4879 * ext4_trim_all_free walks through group's buddy bitmap searching for free
4880 * extents. When the free block is found, ext4_trim_extent is called to TRIM
4881 * the extent.
4882 *
4883 *
4884 * ext4_trim_all_free walks through group's block bitmap searching for free
4885 * extents. When the free extent is found, mark it as used in group buddy
4886 * bitmap. Then issue a TRIM command on this extent and free the extent in
4887 * the group buddy bitmap. This is done until whole group is scanned.
4888 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05004889static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04004890ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
4891 ext4_grpblk_t start, ext4_grpblk_t max,
4892 ext4_grpblk_t minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004893{
4894 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04004895 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04004896 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004897 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004898
Tao Mab3d4c2b2011-07-11 00:01:52 -04004899 trace_ext4_trim_all_free(sb, group, start, max);
4900
Lukas Czerner78944082011-05-24 18:16:27 -04004901 ret = ext4_mb_load_buddy(sb, group, &e4b);
4902 if (ret) {
4903 ext4_error(sb, "Error in loading buddy "
4904 "information for %u", group);
4905 return ret;
4906 }
Lukas Czerner78944082011-05-24 18:16:27 -04004907 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04004908
4909 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04004910 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
4911 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
4912 goto out;
4913
Lukas Czerner78944082011-05-24 18:16:27 -04004914 start = (e4b.bd_info->bb_first_free > start) ?
4915 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004916
Lukas Czerner913eed832012-03-21 21:22:22 -04004917 while (start <= max) {
4918 start = mb_find_next_zero_bit(bitmap, max + 1, start);
4919 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04004920 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04004921 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04004922
4923 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004924 ret = ext4_trim_extent(sb, start,
4925 next - start, group, &e4b);
4926 if (ret && ret != -EOPNOTSUPP)
4927 break;
4928 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04004929 count += next - start;
4930 }
Tao Ma169ddc32011-07-11 00:00:07 -04004931 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04004932 start = next + 1;
4933
4934 if (fatal_signal_pending(current)) {
4935 count = -ERESTARTSYS;
4936 break;
4937 }
4938
4939 if (need_resched()) {
4940 ext4_unlock_group(sb, group);
4941 cond_resched();
4942 ext4_lock_group(sb, group);
4943 }
4944
Tao Ma169ddc32011-07-11 00:00:07 -04004945 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04004946 break;
4947 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004948
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004949 if (!ret) {
4950 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04004951 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004952 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04004953out:
Lukas Czerner7360d172010-10-27 21:30:12 -04004954 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04004955 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04004956
4957 ext4_debug("trimmed %d blocks in the group %d\n",
4958 count, group);
4959
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004960 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04004961}
4962
4963/**
4964 * ext4_trim_fs() -- trim ioctl handle function
4965 * @sb: superblock for filesystem
4966 * @range: fstrim_range structure
4967 *
4968 * start: First Byte to trim
4969 * len: number of Bytes to trim from start
4970 * minlen: minimum extent length in Bytes
4971 * ext4_trim_fs goes through all allocation groups containing Bytes from
4972 * start to start+len. For each such a group ext4_trim_all_free function
4973 * is invoked to trim all free space.
4974 */
4975int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
4976{
Lukas Czerner78944082011-05-24 18:16:27 -04004977 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04004978 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04004979 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04004980 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05004981 ext4_fsblk_t first_data_blk =
4982 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04004983 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04004984 int ret = 0;
4985
4986 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04004987 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04004988 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
4989 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04004990
Lukas Czerner5de35e82012-10-22 18:01:19 -04004991 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
4992 start >= max_blks ||
4993 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04004994 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04004995 if (end >= max_blks)
4996 end = max_blks - 1;
4997 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04004998 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04004999 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005000 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005001
Lukas Czerner913eed832012-03-21 21:22:22 -04005002 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005003 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005004 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005005 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005006 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005007
Lukas Czerner913eed832012-03-21 21:22:22 -04005008 /* end now represents the last cluster to discard in this group */
5009 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005010
5011 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005012 grp = ext4_get_group_info(sb, group);
5013 /* We only do this if the grp has never been initialized */
5014 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
5015 ret = ext4_mb_init_group(sb, group);
5016 if (ret)
5017 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005018 }
5019
Tao Ma0ba08512011-03-23 15:48:11 -04005020 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005021 * For all the groups except the last one, last cluster will
5022 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5023 * change it for the last group, note that last_cluster is
5024 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005025 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005026 if (group == last_group)
5027 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005028
Lukas Czerner78944082011-05-24 18:16:27 -04005029 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005030 cnt = ext4_trim_all_free(sb, group, first_cluster,
Lukas Czerner913eed832012-03-21 21:22:22 -04005031 end, minlen);
Lukas Czerner7360d172010-10-27 21:30:12 -04005032 if (cnt < 0) {
5033 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005034 break;
5035 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005036 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005037 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005038
5039 /*
5040 * For every group except the first one, we are sure
5041 * that the first cluster to discard will be cluster #0.
5042 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005043 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005044 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005045
Tao Ma3d56b8d2011-07-11 00:03:38 -04005046 if (!ret)
5047 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5048
Tao Ma22f10452011-07-10 23:52:37 -04005049out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005050 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005051 return ret;
5052}