blob: 752d6770def10a07b2ea6903de8c570a444e079d [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>
Tejun Heo9d6e9852015-05-22 17:13:32 -040029#include <linux/backing-dev.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040030#include <trace/events/ext4.h>
31
Theodore Ts'oa0b30c12013-02-09 16:28:20 -050032#ifdef CONFIG_EXT4_DEBUG
33ushort ext4_mballoc_debug __read_mostly;
34
35module_param_named(mballoc_debug, ext4_mballoc_debug, ushort, 0644);
36MODULE_PARM_DESC(mballoc_debug, "Debugging level for ext4's mballoc");
37#endif
38
Alex Tomasc9de5602008-01-29 00:19:52 -050039/*
40 * MUSTDO:
41 * - test ext4_ext_search_left() and ext4_ext_search_right()
42 * - search for metadata in few groups
43 *
44 * TODO v4:
45 * - normalization should take into account whether file is still open
46 * - discard preallocations if no free space left (policy?)
47 * - don't normalize tails
48 * - quota
49 * - reservation for superuser
50 *
51 * TODO v3:
52 * - bitmap read-ahead (proposed by Oleg Drokin aka green)
53 * - track min/max extents in each group for better group selection
54 * - mb_mark_used() may allocate chunk right after splitting buddy
55 * - tree of groups sorted by number of free blocks
56 * - error handling
57 */
58
59/*
60 * The allocation request involve request for multiple number of blocks
61 * near to the goal(block) value specified.
62 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040063 * During initialization phase of the allocator we decide to use the
64 * group preallocation or inode preallocation depending on the size of
65 * the file. The size of the file could be the resulting file size we
66 * would have after allocation, or the current file size, which ever
67 * is larger. If the size is less than sbi->s_mb_stream_request we
68 * select to use the group preallocation. The default value of
69 * s_mb_stream_request is 16 blocks. This can also be tuned via
70 * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
71 * terms of number of blocks.
Alex Tomasc9de5602008-01-29 00:19:52 -050072 *
73 * The main motivation for having small file use group preallocation is to
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040074 * ensure that we have small files closer together on the disk.
Alex Tomasc9de5602008-01-29 00:19:52 -050075 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -040076 * First stage the allocator looks at the inode prealloc list,
77 * ext4_inode_info->i_prealloc_list, which contains list of prealloc
78 * spaces for this particular inode. The inode prealloc space is
79 * represented as:
Alex Tomasc9de5602008-01-29 00:19:52 -050080 *
81 * pa_lstart -> the logical start block for this prealloc space
82 * pa_pstart -> the physical start block for this prealloc space
Theodore Ts'o53accfa2011-09-09 18:48:51 -040083 * pa_len -> length for this prealloc space (in clusters)
84 * pa_free -> free space available in this prealloc space (in clusters)
Alex Tomasc9de5602008-01-29 00:19:52 -050085 *
86 * The inode preallocation space is used looking at the _logical_ start
87 * block. If only the logical file block falls within the range of prealloc
Tao Macaaf7a22011-07-11 18:42:42 -040088 * space we will consume the particular prealloc space. This makes sure that
89 * we have contiguous physical blocks representing the file blocks
Alex Tomasc9de5602008-01-29 00:19:52 -050090 *
91 * The important thing to be noted in case of inode prealloc space is that
92 * we don't modify the values associated to inode prealloc space except
93 * pa_free.
94 *
95 * If we are not able to find blocks in the inode prealloc space and if we
96 * have the group allocation flag set then we look at the locality group
Tao Macaaf7a22011-07-11 18:42:42 -040097 * prealloc space. These are per CPU prealloc list represented as
Alex Tomasc9de5602008-01-29 00:19:52 -050098 *
99 * ext4_sb_info.s_locality_groups[smp_processor_id()]
100 *
101 * The reason for having a per cpu locality group is to reduce the contention
102 * between CPUs. It is possible to get scheduled at this point.
103 *
104 * The locality group prealloc space is used looking at whether we have
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300105 * enough free space (pa_free) within the prealloc space.
Alex Tomasc9de5602008-01-29 00:19:52 -0500106 *
107 * If we can't allocate blocks via inode prealloc or/and locality group
108 * prealloc then we look at the buddy cache. The buddy cache is represented
109 * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
110 * mapped to the buddy and bitmap information regarding different
111 * groups. The buddy information is attached to buddy cache inode so that
112 * we can access them through the page cache. The information regarding
113 * each group is loaded via ext4_mb_load_buddy. The information involve
114 * block bitmap and buddy information. The information are stored in the
115 * inode as:
116 *
117 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500118 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500119 *
120 *
121 * one block each for bitmap and buddy information. So for each group we
122 * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
123 * blocksize) blocks. So it can have information regarding groups_per_page
124 * which is blocks_per_page/2
125 *
126 * The buddy cache inode is not stored on disk. The inode is thrown
127 * away when the filesystem is unmounted.
128 *
129 * We look for count number of blocks in the buddy cache. If we were able
130 * to locate that many free blocks we return with additional information
131 * regarding rest of the contiguous physical block available
132 *
133 * Before allocating blocks via buddy cache we normalize the request
134 * blocks. This ensure we ask for more blocks that we needed. The extra
135 * blocks that we get after allocation is added to the respective prealloc
136 * list. In case of inode preallocation we follow a list of heuristics
137 * based on file size. This can be found in ext4_mb_normalize_request. If
138 * we are doing a group prealloc we try to normalize the request to
Theodore Ts'o27baebb2011-09-09 19:02:51 -0400139 * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
140 * dependent on the cluster size; for non-bigalloc file systems, it is
Alex Tomasc9de5602008-01-29 00:19:52 -0500141 * 512 blocks. This can be tuned via
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400142 * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
Alex Tomasc9de5602008-01-29 00:19:52 -0500143 * terms of number of blocks. If we have mounted the file system with -O
144 * stripe=<value> option the group prealloc request is normalized to the
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400145 * the smallest multiple of the stripe value (sbi->s_stripe) which is
146 * greater than the default mb_group_prealloc.
Alex Tomasc9de5602008-01-29 00:19:52 -0500147 *
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -0400148 * The regular allocator (using the buddy cache) supports a few tunables.
Alex Tomasc9de5602008-01-29 00:19:52 -0500149 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400150 * /sys/fs/ext4/<partition>/mb_min_to_scan
151 * /sys/fs/ext4/<partition>/mb_max_to_scan
152 * /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -0500153 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400154 * The regular allocator uses buddy scan only if the request len is power of
Alex Tomasc9de5602008-01-29 00:19:52 -0500155 * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
156 * value of s_mb_order2_reqs can be tuned via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400157 * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200158 * stripe size (sbi->s_stripe), we try to search for contiguous block in
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400159 * stripe size. This should result in better allocation on RAID setups. If
160 * not, we search in the specific group using bitmap for best extents. The
161 * tunable min_to_scan and max_to_scan control the behaviour here.
Alex Tomasc9de5602008-01-29 00:19:52 -0500162 * min_to_scan indicate how long the mballoc __must__ look for a best
Theodore Ts'ob713a5e2009-03-31 09:11:14 -0400163 * extent and max_to_scan indicates how long the mballoc __can__ look for a
Alex Tomasc9de5602008-01-29 00:19:52 -0500164 * best extent in the found extents. Searching for the blocks starts with
165 * the group specified as the goal value in allocation context via
166 * ac_g_ex. Each group is first checked based on the criteria whether it
Tao Macaaf7a22011-07-11 18:42:42 -0400167 * can be used for allocation. ext4_mb_good_group explains how the groups are
Alex Tomasc9de5602008-01-29 00:19:52 -0500168 * checked.
169 *
170 * Both the prealloc space are getting populated as above. So for the first
171 * request we will hit the buddy cache which will result in this prealloc
172 * space getting filled. The prealloc space is then later used for the
173 * subsequent request.
174 */
175
176/*
177 * mballoc operates on the following data:
178 * - on-disk bitmap
179 * - in-core buddy (actually includes buddy and bitmap)
180 * - preallocation descriptors (PAs)
181 *
182 * there are two types of preallocations:
183 * - inode
184 * assiged to specific inode and can be used for this inode only.
185 * it describes part of inode's space preallocated to specific
186 * physical blocks. any block from that preallocated can be used
187 * independent. the descriptor just tracks number of blocks left
188 * unused. so, before taking some block from descriptor, one must
189 * make sure corresponded logical block isn't allocated yet. this
190 * also means that freeing any block within descriptor's range
191 * must discard all preallocated blocks.
192 * - locality group
193 * assigned to specific locality group which does not translate to
194 * permanent set of inodes: inode can join and leave group. space
195 * from this type of preallocation can be used for any inode. thus
196 * it's consumed from the beginning to the end.
197 *
198 * relation between them can be expressed as:
199 * in-core buddy = on-disk bitmap + preallocation descriptors
200 *
201 * this mean blocks mballoc considers used are:
202 * - allocated blocks (persistent)
203 * - preallocated blocks (non-persistent)
204 *
205 * consistency in mballoc world means that at any time a block is either
206 * free or used in ALL structures. notice: "any time" should not be read
207 * literally -- time is discrete and delimited by locks.
208 *
209 * to keep it simple, we don't use block numbers, instead we count number of
210 * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
211 *
212 * all operations can be expressed as:
213 * - init buddy: buddy = on-disk + PAs
214 * - new PA: buddy += N; PA = N
215 * - use inode PA: on-disk += N; PA -= N
216 * - discard inode PA buddy -= on-disk - PA; PA = 0
217 * - use locality group PA on-disk += N; PA -= N
218 * - discard locality group PA buddy -= PA; PA = 0
219 * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
220 * is used in real operation because we can't know actual used
221 * bits from PA, only from on-disk bitmap
222 *
223 * if we follow this strict logic, then all operations above should be atomic.
224 * given some of them can block, we'd have to use something like semaphores
225 * killing performance on high-end SMP hardware. let's try to relax it using
226 * the following knowledge:
227 * 1) if buddy is referenced, it's already initialized
228 * 2) while block is used in buddy and the buddy is referenced,
229 * nobody can re-allocate that block
230 * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
231 * bit set and PA claims same block, it's OK. IOW, one can set bit in
232 * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
233 * block
234 *
235 * so, now we're building a concurrency table:
236 * - init buddy vs.
237 * - new PA
238 * blocks for PA are allocated in the buddy, buddy must be referenced
239 * until PA is linked to allocation group to avoid concurrent buddy init
240 * - use inode PA
241 * we need to make sure that either on-disk bitmap or PA has uptodate data
242 * given (3) we care that PA-=N operation doesn't interfere with init
243 * - discard inode PA
244 * the simplest way would be to have buddy initialized by the discard
245 * - use locality group PA
246 * again PA-=N must be serialized with init
247 * - discard locality group PA
248 * the simplest way would be to have buddy initialized by the discard
249 * - new PA vs.
250 * - use inode PA
251 * i_data_sem serializes them
252 * - discard inode PA
253 * discard process must wait until PA isn't used by another process
254 * - use locality group PA
255 * some mutex should serialize them
256 * - discard locality group PA
257 * discard process must wait until PA isn't used by another process
258 * - use inode PA
259 * - use inode PA
260 * i_data_sem or another mutex should serializes them
261 * - discard inode PA
262 * discard process must wait until PA isn't used by another process
263 * - use locality group PA
264 * nothing wrong here -- they're different PAs covering different blocks
265 * - discard locality group PA
266 * discard process must wait until PA isn't used by another process
267 *
268 * now we're ready to make few consequences:
269 * - PA is referenced and while it is no discard is possible
270 * - PA is referenced until block isn't marked in on-disk bitmap
271 * - PA changes only after on-disk bitmap
272 * - discard must not compete with init. either init is done before
273 * any discard or they're serialized somehow
274 * - buddy init as sum of on-disk bitmap and PAs is done atomically
275 *
276 * a special case when we've used PA to emptiness. no need to modify buddy
277 * in this case, but we should care about concurrent init
278 *
279 */
280
281 /*
282 * Logic in few words:
283 *
284 * - allocation:
285 * load group
286 * find blocks
287 * mark bits in on-disk bitmap
288 * release group
289 *
290 * - use preallocation:
291 * find proper PA (per-inode or group)
292 * load group
293 * mark bits in on-disk bitmap
294 * release group
295 * release PA
296 *
297 * - free:
298 * load group
299 * mark bits in on-disk bitmap
300 * release group
301 *
302 * - discard preallocations in group:
303 * mark PAs deleted
304 * move them onto local list
305 * load on-disk bitmap
306 * load group
307 * remove PA from object (inode or locality group)
308 * mark free blocks in-core
309 *
310 * - discard inode's preallocations:
311 */
312
313/*
314 * Locking rules
315 *
316 * Locks:
317 * - bitlock on a group (group)
318 * - object (inode/locality) (object)
319 * - per-pa lock (pa)
320 *
321 * Paths:
322 * - new pa
323 * object
324 * group
325 *
326 * - find and use pa:
327 * pa
328 *
329 * - release consumed pa:
330 * pa
331 * group
332 * object
333 *
334 * - generate in-core bitmap:
335 * group
336 * pa
337 *
338 * - discard all for given object (inode, locality group):
339 * object
340 * pa
341 * group
342 *
343 * - discard all for given group:
344 * group
345 * pa
346 * group
347 * object
348 *
349 */
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500350static struct kmem_cache *ext4_pspace_cachep;
351static struct kmem_cache *ext4_ac_cachep;
Bobi Jam18aadd42012-02-20 17:53:02 -0500352static struct kmem_cache *ext4_free_data_cachep;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400353
354/* We create slab caches for groupinfo data structures based on the
355 * superblock block size. There will be one per mounted filesystem for
356 * each unique s_blocksize_bits */
Eric Sandeen2892c152011-02-12 08:12:18 -0500357#define NR_GRPINFO_CACHES 8
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -0400358static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
359
Eric Sandeen2892c152011-02-12 08:12:18 -0500360static const char *ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
361 "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
362 "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
363 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
364};
365
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500366static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
367 ext4_group_t group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500368static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
369 ext4_group_t group);
Bobi Jam18aadd42012-02-20 17:53:02 -0500370static void ext4_free_data_callback(struct super_block *sb,
371 struct ext4_journal_cb_entry *jce, int rc);
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500372
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500373static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
374{
Alex Tomasc9de5602008-01-29 00:19:52 -0500375#if BITS_PER_LONG == 64
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500376 *bit += ((unsigned long) addr & 7UL) << 3;
377 addr = (void *) ((unsigned long) addr & ~7UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500378#elif BITS_PER_LONG == 32
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500379 *bit += ((unsigned long) addr & 3UL) << 3;
380 addr = (void *) ((unsigned long) addr & ~3UL);
Alex Tomasc9de5602008-01-29 00:19:52 -0500381#else
382#error "how many bits you are?!"
383#endif
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500384 return addr;
385}
Alex Tomasc9de5602008-01-29 00:19:52 -0500386
387static inline int mb_test_bit(int bit, void *addr)
388{
389 /*
390 * ext4_test_bit on architecture like powerpc
391 * needs unsigned long aligned address
392 */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500393 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500394 return ext4_test_bit(bit, addr);
395}
396
397static inline void mb_set_bit(int bit, void *addr)
398{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500399 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500400 ext4_set_bit(bit, addr);
401}
402
Alex Tomasc9de5602008-01-29 00:19:52 -0500403static inline void mb_clear_bit(int bit, void *addr)
404{
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500405 addr = mb_correct_addr_and_bit(&bit, addr);
Alex Tomasc9de5602008-01-29 00:19:52 -0500406 ext4_clear_bit(bit, addr);
407}
408
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400409static inline int mb_test_and_clear_bit(int bit, void *addr)
410{
411 addr = mb_correct_addr_and_bit(&bit, addr);
412 return ext4_test_and_clear_bit(bit, addr);
413}
414
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500415static inline int mb_find_next_zero_bit(void *addr, int max, int start)
416{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400417 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500418 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400419 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500420 start += fix;
421
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400422 ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
423 if (ret > max)
424 return max;
425 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500426}
427
428static inline int mb_find_next_bit(void *addr, int max, int start)
429{
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400430 int fix = 0, ret, tmpmax;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500431 addr = mb_correct_addr_and_bit(&fix, addr);
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400432 tmpmax = max + fix;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500433 start += fix;
434
Aneesh Kumar K.Ve7dfb242008-07-11 19:27:31 -0400435 ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
436 if (ret > max)
437 return max;
438 return ret;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500439}
440
Alex Tomasc9de5602008-01-29 00:19:52 -0500441static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
442{
443 char *bb;
444
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500445 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -0500446 BUG_ON(max == NULL);
447
448 if (order > e4b->bd_blkbits + 1) {
449 *max = 0;
450 return NULL;
451 }
452
453 /* at order 0 we see each particular block */
Coly Li84b775a2011-02-24 12:51:59 -0500454 if (order == 0) {
455 *max = 1 << (e4b->bd_blkbits + 3);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500456 return e4b->bd_bitmap;
Coly Li84b775a2011-02-24 12:51:59 -0500457 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500458
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500459 bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
Alex Tomasc9de5602008-01-29 00:19:52 -0500460 *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
461
462 return bb;
463}
464
465#ifdef DOUBLE_CHECK
466static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
467 int first, int count)
468{
469 int i;
470 struct super_block *sb = e4b->bd_sb;
471
472 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
473 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400474 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500475 for (i = 0; i < count; i++) {
476 if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
477 ext4_fsblk_t blocknr;
Akinobu Mita5661bd62010-03-03 23:53:39 -0500478
479 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
Theodore Ts'o53accfa2011-09-09 18:48:51 -0400480 blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500481 ext4_grp_locked_error(sb, e4b->bd_group,
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400482 inode ? inode->i_ino : 0,
483 blocknr,
484 "freeing block already freed "
485 "(bit %u)",
486 first + i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500487 }
488 mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
489 }
490}
491
492static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
493{
494 int i;
495
496 if (unlikely(e4b->bd_info->bb_bitmap == NULL))
497 return;
Vincent Minetbc8e6742009-05-15 08:33:18 -0400498 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -0500499 for (i = 0; i < count; i++) {
500 BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
501 mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
502 }
503}
504
505static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
506{
507 if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
508 unsigned char *b1, *b2;
509 int i;
510 b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
511 b2 = (unsigned char *) bitmap;
512 for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
513 if (b1[i] != b2[i]) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -0400514 ext4_msg(e4b->bd_sb, KERN_ERR,
515 "corruption in group %u "
516 "at byte %u(%u): %x in copy != %x "
517 "on disk/prealloc",
518 e4b->bd_group, i, i * 8, b1[i], b2[i]);
Alex Tomasc9de5602008-01-29 00:19:52 -0500519 BUG();
520 }
521 }
522 }
523}
524
525#else
526static inline void mb_free_blocks_double(struct inode *inode,
527 struct ext4_buddy *e4b, int first, int count)
528{
529 return;
530}
531static inline void mb_mark_used_double(struct ext4_buddy *e4b,
532 int first, int count)
533{
534 return;
535}
536static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
537{
538 return;
539}
540#endif
541
542#ifdef AGGRESSIVE_CHECK
543
544#define MB_CHECK_ASSERT(assert) \
545do { \
546 if (!(assert)) { \
547 printk(KERN_EMERG \
548 "Assertion failure in %s() at %s:%d: \"%s\"\n", \
549 function, file, line, # assert); \
550 BUG(); \
551 } \
552} while (0)
553
554static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
555 const char *function, int line)
556{
557 struct super_block *sb = e4b->bd_sb;
558 int order = e4b->bd_blkbits + 1;
559 int max;
560 int max2;
561 int i;
562 int j;
563 int k;
564 int count;
565 struct ext4_group_info *grp;
566 int fragments = 0;
567 int fstart;
568 struct list_head *cur;
569 void *buddy;
570 void *buddy2;
571
Alex Tomasc9de5602008-01-29 00:19:52 -0500572 {
573 static int mb_check_counter;
574 if (mb_check_counter++ % 100 != 0)
575 return 0;
576 }
577
578 while (order > 1) {
579 buddy = mb_find_buddy(e4b, order, &max);
580 MB_CHECK_ASSERT(buddy);
581 buddy2 = mb_find_buddy(e4b, order - 1, &max2);
582 MB_CHECK_ASSERT(buddy2);
583 MB_CHECK_ASSERT(buddy != buddy2);
584 MB_CHECK_ASSERT(max * 2 == max2);
585
586 count = 0;
587 for (i = 0; i < max; i++) {
588
589 if (mb_test_bit(i, buddy)) {
590 /* only single bit in buddy2 may be 1 */
591 if (!mb_test_bit(i << 1, buddy2)) {
592 MB_CHECK_ASSERT(
593 mb_test_bit((i<<1)+1, buddy2));
594 } else if (!mb_test_bit((i << 1) + 1, buddy2)) {
595 MB_CHECK_ASSERT(
596 mb_test_bit(i << 1, buddy2));
597 }
598 continue;
599 }
600
Robin Dong0a10da72011-10-26 08:48:54 -0400601 /* both bits in buddy2 must be 1 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500602 MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
603 MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
604
605 for (j = 0; j < (1 << order); j++) {
606 k = (i * (1 << order)) + j;
607 MB_CHECK_ASSERT(
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -0500608 !mb_test_bit(k, e4b->bd_bitmap));
Alex Tomasc9de5602008-01-29 00:19:52 -0500609 }
610 count++;
611 }
612 MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
613 order--;
614 }
615
616 fstart = -1;
617 buddy = mb_find_buddy(e4b, 0, &max);
618 for (i = 0; i < max; i++) {
619 if (!mb_test_bit(i, buddy)) {
620 MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
621 if (fstart == -1) {
622 fragments++;
623 fstart = i;
624 }
625 continue;
626 }
627 fstart = -1;
628 /* check used bits only */
629 for (j = 0; j < e4b->bd_blkbits + 1; j++) {
630 buddy2 = mb_find_buddy(e4b, j, &max2);
631 k = i >> j;
632 MB_CHECK_ASSERT(k < max2);
633 MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
634 }
635 }
636 MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
637 MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
638
639 grp = ext4_get_group_info(sb, e4b->bd_group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500640 list_for_each(cur, &grp->bb_prealloc_list) {
641 ext4_group_t groupnr;
642 struct ext4_prealloc_space *pa;
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400643 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
644 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
Alex Tomasc9de5602008-01-29 00:19:52 -0500645 MB_CHECK_ASSERT(groupnr == e4b->bd_group);
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -0400646 for (i = 0; i < pa->pa_len; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500647 MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
648 }
649 return 0;
650}
651#undef MB_CHECK_ASSERT
652#define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400653 __FILE__, __func__, __LINE__)
Alex Tomasc9de5602008-01-29 00:19:52 -0500654#else
655#define mb_check_buddy(e4b)
656#endif
657
Coly Li7c786052011-02-24 13:24:25 -0500658/*
659 * Divide blocks started from @first with length @len into
660 * smaller chunks with power of 2 blocks.
661 * Clear the bits in bitmap which the blocks of the chunk(s) covered,
662 * then increase bb_counters[] for corresponded chunk size.
663 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500664static void ext4_mb_mark_free_simple(struct super_block *sb,
Eric Sandeena36b4492009-08-25 22:36:45 -0400665 void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
Alex Tomasc9de5602008-01-29 00:19:52 -0500666 struct ext4_group_info *grp)
667{
668 struct ext4_sb_info *sbi = EXT4_SB(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400669 ext4_grpblk_t min;
670 ext4_grpblk_t max;
671 ext4_grpblk_t chunk;
Alex Tomasc9de5602008-01-29 00:19:52 -0500672 unsigned short border;
673
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400674 BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
Alex Tomasc9de5602008-01-29 00:19:52 -0500675
676 border = 2 << sb->s_blocksize_bits;
677
678 while (len > 0) {
679 /* find how many blocks can be covered since this position */
680 max = ffs(first | border) - 1;
681
682 /* find how many blocks of power 2 we need to mark */
683 min = fls(len) - 1;
684
685 if (max < min)
686 min = max;
687 chunk = 1 << min;
688
689 /* mark multiblock chunks only */
690 grp->bb_counters[min]++;
691 if (min > 0)
692 mb_clear_bit(first >> min,
693 buddy + sbi->s_mb_offsets[min]);
694
695 len -= chunk;
696 first += chunk;
697 }
698}
699
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400700/*
701 * Cache the order of the largest free extent we have available in this block
702 * group.
703 */
704static void
705mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
706{
707 int i;
708 int bits;
709
710 grp->bb_largest_free_order = -1; /* uninit */
711
712 bits = sb->s_blocksize_bits + 1;
713 for (i = bits; i >= 0; i--) {
714 if (grp->bb_counters[i] > 0) {
715 grp->bb_largest_free_order = i;
716 break;
717 }
718 }
719}
720
Eric Sandeen089ceec2009-07-05 22:17:31 -0400721static noinline_for_stack
722void ext4_mb_generate_buddy(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -0500723 void *buddy, void *bitmap, ext4_group_t group)
724{
725 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400726 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -0400727 ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
Eric Sandeena36b4492009-08-25 22:36:45 -0400728 ext4_grpblk_t i = 0;
729 ext4_grpblk_t first;
730 ext4_grpblk_t len;
Alex Tomasc9de5602008-01-29 00:19:52 -0500731 unsigned free = 0;
732 unsigned fragments = 0;
733 unsigned long long period = get_cycles();
734
735 /* initialize buddy from bitmap which is aggregation
736 * of on-disk bitmap and preallocations */
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500737 i = mb_find_next_zero_bit(bitmap, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -0500738 grp->bb_first_free = i;
739 while (i < max) {
740 fragments++;
741 first = i;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500742 i = mb_find_next_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500743 len = i - first;
744 free += len;
745 if (len > 1)
746 ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
747 else
748 grp->bb_counters[0]++;
749 if (i < max)
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -0500750 i = mb_find_next_zero_bit(bitmap, max, i);
Alex Tomasc9de5602008-01-29 00:19:52 -0500751 }
752 grp->bb_fragments = fragments;
753
754 if (free != grp->bb_free) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400755 ext4_grp_locked_error(sb, group, 0, 0,
Theodore Ts'o94d4c062014-07-05 19:15:50 -0400756 "block bitmap and bg descriptor "
757 "inconsistent: %u vs %u free clusters",
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400758 free, grp->bb_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500759 /*
Darrick J. Wong163a2032013-08-28 17:35:51 -0400760 * If we intend to continue, we consider group descriptor
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -0500761 * corrupt and update bb_free using bitmap value
762 */
Alex Tomasc9de5602008-01-29 00:19:52 -0500763 grp->bb_free = free;
Namjae Jeone43bb4e2014-06-26 10:11:53 -0400764 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
765 percpu_counter_sub(&sbi->s_freeclusters_counter,
766 grp->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -0400767 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
Alex Tomasc9de5602008-01-29 00:19:52 -0500768 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400769 mb_set_largest_free_order(sb, grp);
Alex Tomasc9de5602008-01-29 00:19:52 -0500770
771 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
772
773 period = get_cycles() - period;
774 spin_lock(&EXT4_SB(sb)->s_bal_lock);
775 EXT4_SB(sb)->s_mb_buddies_generated++;
776 EXT4_SB(sb)->s_mb_generation_time += period;
777 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
778}
779
Andrey Sidoroveabe0442013-04-09 12:22:29 -0400780static void mb_regenerate_buddy(struct ext4_buddy *e4b)
781{
782 int count;
783 int order = 1;
784 void *buddy;
785
786 while ((buddy = mb_find_buddy(e4b, order++, &count))) {
787 ext4_set_bits(buddy, 0, count);
788 }
789 e4b->bd_info->bb_fragments = 0;
790 memset(e4b->bd_info->bb_counters, 0,
791 sizeof(*e4b->bd_info->bb_counters) *
792 (e4b->bd_sb->s_blocksize_bits + 2));
793
794 ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
795 e4b->bd_bitmap, e4b->bd_group);
796}
797
Alex Tomasc9de5602008-01-29 00:19:52 -0500798/* The buddy information is attached the buddy cache inode
799 * for convenience. The information regarding each group
800 * is loaded via ext4_mb_load_buddy. The information involve
801 * block bitmap and buddy information. The information are
802 * stored in the inode as
803 *
804 * { page }
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -0500805 * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
Alex Tomasc9de5602008-01-29 00:19:52 -0500806 *
807 *
808 * one block each for bitmap and buddy information.
809 * So for each group we take up 2 blocks. A page can
810 * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize) blocks.
811 * So it can have information regarding groups_per_page which
812 * is blocks_per_page/2
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400813 *
814 * Locking note: This routine takes the block group lock of all groups
815 * for this page; do not hold this lock when calling this routine!
Alex Tomasc9de5602008-01-29 00:19:52 -0500816 */
817
818static int ext4_mb_init_cache(struct page *page, char *incore)
819{
Theodore Ts'o8df96752009-05-01 08:50:38 -0400820 ext4_group_t ngroups;
Alex Tomasc9de5602008-01-29 00:19:52 -0500821 int blocksize;
822 int blocks_per_page;
823 int groups_per_page;
824 int err = 0;
825 int i;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500826 ext4_group_t first_group, group;
Alex Tomasc9de5602008-01-29 00:19:52 -0500827 int first_block;
828 struct super_block *sb;
829 struct buffer_head *bhs;
Darrick J. Wongfa77dcf2012-04-29 18:35:10 -0400830 struct buffer_head **bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -0500831 struct inode *inode;
832 char *data;
833 char *bitmap;
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400834 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -0500835
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400836 mb_debug(1, "init page %lu\n", page->index);
Alex Tomasc9de5602008-01-29 00:19:52 -0500837
838 inode = page->mapping->host;
839 sb = inode->i_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400840 ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -0500841 blocksize = 1 << inode->i_blkbits;
842 blocks_per_page = PAGE_CACHE_SIZE / blocksize;
843
844 groups_per_page = blocks_per_page >> 1;
845 if (groups_per_page == 0)
846 groups_per_page = 1;
847
848 /* allocate buffer_heads to read bitmaps */
849 if (groups_per_page > 1) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500850 i = sizeof(struct buffer_head *) * groups_per_page;
851 bh = kzalloc(i, GFP_NOFS);
Theodore Ts'o813e5722012-02-20 17:52:46 -0500852 if (bh == NULL) {
853 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500854 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500855 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500856 } else
857 bh = &bhs;
858
859 first_group = page->index * blocks_per_page / 2;
860
861 /* read all groups the page covers into the cache */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500862 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
863 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500864 break;
865
Theodore Ts'o813e5722012-02-20 17:52:46 -0500866 grinfo = ext4_get_group_info(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400867 /*
868 * If page is uptodate then we came here after online resize
869 * which added some new uninitialized group info structs, so
870 * we must skip all initialized uptodate buddies on the page,
871 * which may be currently in use by an allocating task.
872 */
873 if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
874 bh[i] = NULL;
875 continue;
876 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500877 if (!(bh[i] = ext4_read_block_bitmap_nowait(sb, group))) {
878 err = -ENOMEM;
Alex Tomasc9de5602008-01-29 00:19:52 -0500879 goto out;
Aneesh Kumar K.V2ccb5fb2009-01-05 21:49:55 -0500880 }
Theodore Ts'o813e5722012-02-20 17:52:46 -0500881 mb_debug(1, "read bitmap for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500882 }
883
884 /* wait for I/O completion */
Theodore Ts'o813e5722012-02-20 17:52:46 -0500885 for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
886 if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
887 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -0500888 goto out;
Theodore Ts'o813e5722012-02-20 17:52:46 -0500889 }
890 }
Alex Tomasc9de5602008-01-29 00:19:52 -0500891
892 first_block = page->index * blocks_per_page;
893 for (i = 0; i < blocks_per_page; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -0500894 group = (first_block + i) >> 1;
Theodore Ts'o8df96752009-05-01 08:50:38 -0400895 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -0500896 break;
897
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400898 if (!bh[group - first_group])
899 /* skip initialized uptodate buddy */
900 continue;
901
Alex Tomasc9de5602008-01-29 00:19:52 -0500902 /*
903 * data carry information regarding this
904 * particular group in the format specified
905 * above
906 *
907 */
908 data = page_address(page) + (i * blocksize);
909 bitmap = bh[group - first_group]->b_data;
910
911 /*
912 * We place the buddy block and bitmap block
913 * close together
914 */
915 if ((first_block + i) & 1) {
916 /* this is block of buddy */
917 BUG_ON(incore == NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400918 mb_debug(1, "put buddy for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500919 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400920 trace_ext4_mb_buddy_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500921 grinfo = ext4_get_group_info(sb, group);
922 grinfo->bb_fragments = 0;
923 memset(grinfo->bb_counters, 0,
Eric Sandeen19278052009-08-25 22:36:25 -0400924 sizeof(*grinfo->bb_counters) *
925 (sb->s_blocksize_bits+2));
Alex Tomasc9de5602008-01-29 00:19:52 -0500926 /*
927 * incore got set to the group block bitmap below
928 */
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500929 ext4_lock_group(sb, group);
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400930 /* init the buddy */
931 memset(data, 0xff, blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -0500932 ext4_mb_generate_buddy(sb, data, incore, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500933 ext4_unlock_group(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500934 incore = NULL;
935 } else {
936 /* this is block of bitmap */
937 BUG_ON(incore != NULL);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -0400938 mb_debug(1, "put bitmap for group %u in page %lu/%x\n",
Alex Tomasc9de5602008-01-29 00:19:52 -0500939 group, page->index, i * blocksize);
Theodore Ts'of3073332010-05-17 03:00:00 -0400940 trace_ext4_mb_bitmap_load(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500941
942 /* see comments in ext4_mb_put_pa() */
943 ext4_lock_group(sb, group);
944 memcpy(data, bitmap, blocksize);
945
946 /* mark all preallocated blks used in in-core bitmap */
947 ext4_mb_generate_from_pa(sb, data, group);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -0500948 ext4_mb_generate_from_freelist(sb, data, group);
Alex Tomasc9de5602008-01-29 00:19:52 -0500949 ext4_unlock_group(sb, group);
950
951 /* set incore so that the buddy information can be
952 * generated using this
953 */
954 incore = data;
955 }
956 }
957 SetPageUptodate(page);
958
959out:
960 if (bh) {
Amir Goldstein9b8b7d32011-05-09 21:49:42 -0400961 for (i = 0; i < groups_per_page; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -0500962 brelse(bh[i]);
963 if (bh != &bhs)
964 kfree(bh);
965 }
966 return err;
967}
968
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -0400969/*
Amir Goldstein2de88072011-05-09 21:48:13 -0400970 * Lock the buddy and bitmap pages. This make sure other parallel init_group
971 * on the same buddy page doesn't happen whild holding the buddy page lock.
972 * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
973 * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400974 */
Amir Goldstein2de88072011-05-09 21:48:13 -0400975static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
976 ext4_group_t group, struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400977{
Amir Goldstein2de88072011-05-09 21:48:13 -0400978 struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
979 int block, pnum, poff;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400980 int blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400981 struct page *page;
982
983 e4b->bd_buddy_page = NULL;
984 e4b->bd_bitmap_page = NULL;
Eric Sandeeneee4adc2010-10-27 21:30:15 -0400985
986 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
987 /*
988 * the buddy cache inode stores the block bitmap
989 * and buddy information in consecutive blocks.
990 * So for each group we need two blocks.
991 */
992 block = group * 2;
993 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -0400994 poff = block % blocks_per_page;
995 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
996 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -0400997 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -0400998 BUG_ON(page->mapping != inode->i_mapping);
999 e4b->bd_bitmap_page = page;
1000 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001001
Amir Goldstein2de88072011-05-09 21:48:13 -04001002 if (blocks_per_page >= 2) {
1003 /* buddy and bitmap are on the same page */
1004 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001005 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001006
1007 block++;
1008 pnum = block / blocks_per_page;
Amir Goldstein2de88072011-05-09 21:48:13 -04001009 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1010 if (!page)
Younger Liuc57ab392014-04-10 23:03:43 -04001011 return -ENOMEM;
Amir Goldstein2de88072011-05-09 21:48:13 -04001012 BUG_ON(page->mapping != inode->i_mapping);
1013 e4b->bd_buddy_page = page;
1014 return 0;
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001015}
1016
Amir Goldstein2de88072011-05-09 21:48:13 -04001017static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001018{
Amir Goldstein2de88072011-05-09 21:48:13 -04001019 if (e4b->bd_bitmap_page) {
1020 unlock_page(e4b->bd_bitmap_page);
1021 page_cache_release(e4b->bd_bitmap_page);
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001022 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001023 if (e4b->bd_buddy_page) {
1024 unlock_page(e4b->bd_buddy_page);
1025 page_cache_release(e4b->bd_buddy_page);
1026 }
Eric Sandeeneee4adc2010-10-27 21:30:15 -04001027}
1028
1029/*
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001030 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1031 * block group lock of all groups for this page; do not hold the BG lock when
1032 * calling this routine!
1033 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001034static noinline_for_stack
1035int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1036{
1037
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001038 struct ext4_group_info *this_grp;
Amir Goldstein2de88072011-05-09 21:48:13 -04001039 struct ext4_buddy e4b;
1040 struct page *page;
1041 int ret = 0;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001042
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001043 might_sleep();
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001044 mb_debug(1, "init group %u\n", group);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001045 this_grp = ext4_get_group_info(sb, group);
1046 /*
Aneesh Kumar K.V08c3a812009-09-09 23:50:17 -04001047 * This ensures that we don't reinit the buddy cache
1048 * page which map to the group from which we are already
1049 * allocating. If we are looking at the buddy cache we would
1050 * have taken a reference using ext4_mb_load_buddy and that
Amir Goldstein2de88072011-05-09 21:48:13 -04001051 * would have pinned buddy page to page cache.
Mel Gorman2457aec2014-06-04 16:10:31 -07001052 * The call to ext4_mb_get_buddy_page_lock will mark the
1053 * page accessed.
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001054 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001055 ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b);
1056 if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001057 /*
1058 * somebody initialized the group
1059 * return without doing anything
1060 */
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001061 goto err;
1062 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001063
1064 page = e4b.bd_bitmap_page;
1065 ret = ext4_mb_init_cache(page, NULL);
1066 if (ret)
1067 goto err;
1068 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001069 ret = -EIO;
1070 goto err;
1071 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001072
Amir Goldstein2de88072011-05-09 21:48:13 -04001073 if (e4b.bd_buddy_page == NULL) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001074 /*
1075 * If both the bitmap and buddy are in
1076 * the same page we don't need to force
1077 * init the buddy
1078 */
Amir Goldstein2de88072011-05-09 21:48:13 -04001079 ret = 0;
1080 goto err;
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001081 }
Amir Goldstein2de88072011-05-09 21:48:13 -04001082 /* init buddy cache */
1083 page = e4b.bd_buddy_page;
1084 ret = ext4_mb_init_cache(page, e4b.bd_bitmap);
1085 if (ret)
1086 goto err;
1087 if (!PageUptodate(page)) {
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001088 ret = -EIO;
1089 goto err;
1090 }
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001091err:
Amir Goldstein2de88072011-05-09 21:48:13 -04001092 ext4_mb_put_buddy_page_lock(&e4b);
Aneesh Kumar K.Vb6a758e2009-09-09 23:47:46 -04001093 return ret;
1094}
1095
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001096/*
1097 * Locking note: This routine calls ext4_mb_init_cache(), which takes the
1098 * block group lock of all groups for this page; do not hold the BG lock when
1099 * calling this routine!
1100 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04001101static noinline_for_stack int
1102ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1103 struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001104{
Alex Tomasc9de5602008-01-29 00:19:52 -05001105 int blocks_per_page;
1106 int block;
1107 int pnum;
1108 int poff;
1109 struct page *page;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001110 int ret;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001111 struct ext4_group_info *grp;
1112 struct ext4_sb_info *sbi = EXT4_SB(sb);
1113 struct inode *inode = sbi->s_buddy_cache;
Alex Tomasc9de5602008-01-29 00:19:52 -05001114
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04001115 might_sleep();
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04001116 mb_debug(1, "load group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001117
1118 blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001119 grp = ext4_get_group_info(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001120
1121 e4b->bd_blkbits = sb->s_blocksize_bits;
Tao Ma529da702011-07-23 16:07:26 -04001122 e4b->bd_info = grp;
Alex Tomasc9de5602008-01-29 00:19:52 -05001123 e4b->bd_sb = sb;
1124 e4b->bd_group = group;
1125 e4b->bd_buddy_page = NULL;
1126 e4b->bd_bitmap_page = NULL;
1127
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001128 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001129 /*
1130 * we need full data about the group
1131 * to make a good selection
1132 */
1133 ret = ext4_mb_init_group(sb, group);
1134 if (ret)
1135 return ret;
Aneesh Kumar K.Vf41c0752009-09-09 23:34:50 -04001136 }
1137
Alex Tomasc9de5602008-01-29 00:19:52 -05001138 /*
1139 * the buddy cache inode stores the block bitmap
1140 * and buddy information in consecutive blocks.
1141 * So for each group we need two blocks.
1142 */
1143 block = group * 2;
1144 pnum = block / blocks_per_page;
1145 poff = block % blocks_per_page;
1146
1147 /* we could use find_or_create_page(), but it locks page
1148 * what we'd like to avoid in fast path ... */
Mel Gorman2457aec2014-06-04 16:10:31 -07001149 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001150 if (page == NULL || !PageUptodate(page)) {
1151 if (page)
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05001152 /*
1153 * drop the page reference and try
1154 * to get the page with lock. If we
1155 * are not uptodate that implies
1156 * somebody just created the page but
1157 * is yet to initialize the same. So
1158 * wait for it to initialize.
1159 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001160 page_cache_release(page);
1161 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1162 if (page) {
1163 BUG_ON(page->mapping != inode->i_mapping);
1164 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001165 ret = ext4_mb_init_cache(page, NULL);
1166 if (ret) {
1167 unlock_page(page);
1168 goto err;
1169 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001170 mb_cmp_bitmaps(e4b, page_address(page) +
1171 (poff * sb->s_blocksize));
1172 }
1173 unlock_page(page);
1174 }
1175 }
Younger Liuc57ab392014-04-10 23:03:43 -04001176 if (page == NULL) {
1177 ret = -ENOMEM;
1178 goto err;
1179 }
1180 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001181 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001182 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001183 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001184
1185 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001186 e4b->bd_bitmap_page = page;
1187 e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001188
1189 block++;
1190 pnum = block / blocks_per_page;
1191 poff = block % blocks_per_page;
1192
Mel Gorman2457aec2014-06-04 16:10:31 -07001193 page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
Alex Tomasc9de5602008-01-29 00:19:52 -05001194 if (page == NULL || !PageUptodate(page)) {
1195 if (page)
1196 page_cache_release(page);
1197 page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1198 if (page) {
1199 BUG_ON(page->mapping != inode->i_mapping);
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001200 if (!PageUptodate(page)) {
1201 ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1202 if (ret) {
1203 unlock_page(page);
1204 goto err;
1205 }
1206 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001207 unlock_page(page);
1208 }
1209 }
Younger Liuc57ab392014-04-10 23:03:43 -04001210 if (page == NULL) {
1211 ret = -ENOMEM;
1212 goto err;
1213 }
1214 if (!PageUptodate(page)) {
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001215 ret = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05001216 goto err;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001217 }
Mel Gorman2457aec2014-06-04 16:10:31 -07001218
1219 /* Pages marked accessed already */
Alex Tomasc9de5602008-01-29 00:19:52 -05001220 e4b->bd_buddy_page = page;
1221 e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
Alex Tomasc9de5602008-01-29 00:19:52 -05001222
1223 BUG_ON(e4b->bd_bitmap_page == NULL);
1224 BUG_ON(e4b->bd_buddy_page == NULL);
1225
1226 return 0;
1227
1228err:
Yang Ruirui26626f112011-04-16 19:17:48 -04001229 if (page)
1230 page_cache_release(page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001231 if (e4b->bd_bitmap_page)
1232 page_cache_release(e4b->bd_bitmap_page);
1233 if (e4b->bd_buddy_page)
1234 page_cache_release(e4b->bd_buddy_page);
1235 e4b->bd_buddy = NULL;
1236 e4b->bd_bitmap = NULL;
Shen Fengfdf6c7a2008-07-11 19:27:31 -04001237 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05001238}
1239
Jing Zhange39e07f2010-05-14 00:00:00 -04001240static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
Alex Tomasc9de5602008-01-29 00:19:52 -05001241{
1242 if (e4b->bd_bitmap_page)
1243 page_cache_release(e4b->bd_bitmap_page);
1244 if (e4b->bd_buddy_page)
1245 page_cache_release(e4b->bd_buddy_page);
1246}
1247
1248
1249static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1250{
1251 int order = 1;
Nicolai Stange76caa712016-05-05 17:38:03 -04001252 int bb_incr = 1 << (e4b->bd_blkbits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05001253 void *bb;
1254
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001255 BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
Alex Tomasc9de5602008-01-29 00:19:52 -05001256 BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1257
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001258 bb = e4b->bd_buddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05001259 while (order <= e4b->bd_blkbits + 1) {
1260 block = block >> 1;
1261 if (!mb_test_bit(block, bb)) {
1262 /* this block is part of buddy of order 'order' */
1263 return order;
1264 }
Nicolai Stange76caa712016-05-05 17:38:03 -04001265 bb += bb_incr;
1266 bb_incr >>= 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001267 order++;
1268 }
1269 return 0;
1270}
1271
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001272static void mb_clear_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001273{
1274 __u32 *addr;
1275
1276 len = cur + len;
1277 while (cur < len) {
1278 if ((cur & 31) == 0 && (len - cur) >= 32) {
1279 /* fast path: clear whole word at once */
1280 addr = bm + (cur >> 3);
1281 *addr = 0;
1282 cur += 32;
1283 continue;
1284 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001285 mb_clear_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001286 cur++;
1287 }
1288}
1289
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001290/* clear bits in given range
1291 * will return first found zero bit if any, -1 otherwise
1292 */
1293static int mb_test_and_clear_bits(void *bm, int cur, int len)
1294{
1295 __u32 *addr;
1296 int zero_bit = -1;
1297
1298 len = cur + len;
1299 while (cur < len) {
1300 if ((cur & 31) == 0 && (len - cur) >= 32) {
1301 /* fast path: clear whole word at once */
1302 addr = bm + (cur >> 3);
1303 if (*addr != (__u32)(-1) && zero_bit == -1)
1304 zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1305 *addr = 0;
1306 cur += 32;
1307 continue;
1308 }
1309 if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1310 zero_bit = cur;
1311 cur++;
1312 }
1313
1314 return zero_bit;
1315}
1316
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04001317void ext4_set_bits(void *bm, int cur, int len)
Alex Tomasc9de5602008-01-29 00:19:52 -05001318{
1319 __u32 *addr;
1320
1321 len = cur + len;
1322 while (cur < len) {
1323 if ((cur & 31) == 0 && (len - cur) >= 32) {
1324 /* fast path: set whole word at once */
1325 addr = bm + (cur >> 3);
1326 *addr = 0xffffffff;
1327 cur += 32;
1328 continue;
1329 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001330 mb_set_bit(cur, bm);
Alex Tomasc9de5602008-01-29 00:19:52 -05001331 cur++;
1332 }
1333}
1334
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001335/*
1336 * _________________________________________________________________ */
1337
1338static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
Alex Tomasc9de5602008-01-29 00:19:52 -05001339{
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001340 if (mb_test_bit(*bit + side, bitmap)) {
1341 mb_clear_bit(*bit, bitmap);
1342 (*bit) -= side;
1343 return 1;
1344 }
1345 else {
1346 (*bit) += side;
1347 mb_set_bit(*bit, bitmap);
1348 return -1;
1349 }
1350}
1351
1352static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1353{
1354 int max;
1355 int order = 1;
1356 void *buddy = mb_find_buddy(e4b, order, &max);
1357
1358 while (buddy) {
1359 void *buddy2;
1360
1361 /* Bits in range [first; last] are known to be set since
1362 * corresponding blocks were allocated. Bits in range
1363 * (first; last) will stay set because they form buddies on
1364 * upper layer. We just deal with borders if they don't
1365 * align with upper layer and then go up.
1366 * Releasing entire group is all about clearing
1367 * single bit of highest order buddy.
1368 */
1369
1370 /* Example:
1371 * ---------------------------------
1372 * | 1 | 1 | 1 | 1 |
1373 * ---------------------------------
1374 * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1375 * ---------------------------------
1376 * 0 1 2 3 4 5 6 7
1377 * \_____________________/
1378 *
1379 * Neither [1] nor [6] is aligned to above layer.
1380 * Left neighbour [0] is free, so mark it busy,
1381 * decrease bb_counters and extend range to
1382 * [0; 6]
1383 * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1384 * mark [6] free, increase bb_counters and shrink range to
1385 * [0; 5].
1386 * Then shift range to [0; 2], go up and do the same.
1387 */
1388
1389
1390 if (first & 1)
1391 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1392 if (!(last & 1))
1393 e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1394 if (first > last)
1395 break;
1396 order++;
1397
1398 if (first == last || !(buddy2 = mb_find_buddy(e4b, order, &max))) {
1399 mb_clear_bits(buddy, first, last - first + 1);
1400 e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1401 break;
1402 }
1403 first >>= 1;
1404 last >>= 1;
1405 buddy = buddy2;
1406 }
1407}
1408
1409static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1410 int first, int count)
1411{
1412 int left_is_free = 0;
1413 int right_is_free = 0;
1414 int block;
1415 int last = first + count - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001416 struct super_block *sb = e4b->bd_sb;
1417
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04001418 if (WARN_ON(count == 0))
1419 return;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001420 BUG_ON(last >= (sb->s_blocksize << 3));
Vincent Minetbc8e6742009-05-15 08:33:18 -04001421 assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
Darrick J. Wong163a2032013-08-28 17:35:51 -04001422 /* Don't bother if the block group is corrupt. */
1423 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1424 return;
1425
Alex Tomasc9de5602008-01-29 00:19:52 -05001426 mb_check_buddy(e4b);
1427 mb_free_blocks_double(inode, e4b, first, count);
1428
1429 e4b->bd_info->bb_free += count;
1430 if (first < e4b->bd_info->bb_first_free)
1431 e4b->bd_info->bb_first_free = first;
1432
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001433 /* access memory sequentially: check left neighbour,
1434 * clear range and then check right neighbour
1435 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001436 if (first != 0)
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001437 left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1438 block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1439 if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1440 right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1441
1442 if (unlikely(block != -1)) {
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001443 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001444 ext4_fsblk_t blocknr;
1445
1446 blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
1447 blocknr += EXT4_C2B(EXT4_SB(sb), block);
1448 ext4_grp_locked_error(sb, e4b->bd_group,
1449 inode ? inode->i_ino : 0,
1450 blocknr,
1451 "freeing already freed block "
Darrick J. Wong163a2032013-08-28 17:35:51 -04001452 "(bit %u); block bitmap corrupt.",
1453 block);
Namjae Jeone43bb4e2014-06-26 10:11:53 -04001454 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))
1455 percpu_counter_sub(&sbi->s_freeclusters_counter,
1456 e4b->bd_info->bb_free);
Darrick J. Wong163a2032013-08-28 17:35:51 -04001457 /* Mark the block group as corrupt. */
1458 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1459 &e4b->bd_info->bb_state);
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001460 mb_regenerate_buddy(e4b);
1461 goto done;
1462 }
1463
1464 /* let's maintain fragments counter */
1465 if (left_is_free && right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001466 e4b->bd_info->bb_fragments--;
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001467 else if (!left_is_free && !right_is_free)
Alex Tomasc9de5602008-01-29 00:19:52 -05001468 e4b->bd_info->bb_fragments++;
1469
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001470 /* buddy[0] == bd_bitmap is a special case, so handle
1471 * it right away and let mb_buddy_mark_free stay free of
1472 * zero order checks.
1473 * Check if neighbours are to be coaleasced,
1474 * adjust bitmap bb_counters and borders appropriately.
1475 */
1476 if (first & 1) {
1477 first += !left_is_free;
1478 e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05001479 }
Andrey Sidoroveabe0442013-04-09 12:22:29 -04001480 if (!(last & 1)) {
1481 last -= !right_is_free;
1482 e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1483 }
1484
1485 if (first <= last)
1486 mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1487
1488done:
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001489 mb_set_largest_free_order(sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001490 mb_check_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001491}
1492
Robin Dong15c006a2012-08-17 10:02:17 -04001493static int mb_find_extent(struct ext4_buddy *e4b, int block,
Alex Tomasc9de5602008-01-29 00:19:52 -05001494 int needed, struct ext4_free_extent *ex)
1495{
1496 int next = block;
Robin Dong15c006a2012-08-17 10:02:17 -04001497 int max, order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001498 void *buddy;
1499
Vincent Minetbc8e6742009-05-15 08:33:18 -04001500 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001501 BUG_ON(ex == NULL);
1502
Robin Dong15c006a2012-08-17 10:02:17 -04001503 buddy = mb_find_buddy(e4b, 0, &max);
Alex Tomasc9de5602008-01-29 00:19:52 -05001504 BUG_ON(buddy == NULL);
1505 BUG_ON(block >= max);
1506 if (mb_test_bit(block, buddy)) {
1507 ex->fe_len = 0;
1508 ex->fe_start = 0;
1509 ex->fe_group = 0;
1510 return 0;
1511 }
1512
Robin Dong15c006a2012-08-17 10:02:17 -04001513 /* find actual order */
1514 order = mb_find_order_for_block(e4b, block);
1515 block = block >> order;
Alex Tomasc9de5602008-01-29 00:19:52 -05001516
1517 ex->fe_len = 1 << order;
1518 ex->fe_start = block << order;
1519 ex->fe_group = e4b->bd_group;
1520
1521 /* calc difference from given start */
1522 next = next - ex->fe_start;
1523 ex->fe_len -= next;
1524 ex->fe_start += next;
1525
1526 while (needed > ex->fe_len &&
Alan Coxd8ec0c32012-11-08 12:19:58 -05001527 mb_find_buddy(e4b, order, &max)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001528
1529 if (block + 1 >= max)
1530 break;
1531
1532 next = (block + 1) * (1 << order);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001533 if (mb_test_bit(next, e4b->bd_bitmap))
Alex Tomasc9de5602008-01-29 00:19:52 -05001534 break;
1535
Robin Dongb051d8d2011-10-26 05:30:30 -04001536 order = mb_find_order_for_block(e4b, next);
Alex Tomasc9de5602008-01-29 00:19:52 -05001537
Alex Tomasc9de5602008-01-29 00:19:52 -05001538 block = next >> order;
1539 ex->fe_len += 1 << order;
1540 }
1541
1542 BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1543 return ex->fe_len;
1544}
1545
1546static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1547{
1548 int ord;
1549 int mlen = 0;
1550 int max = 0;
1551 int cur;
1552 int start = ex->fe_start;
1553 int len = ex->fe_len;
1554 unsigned ret = 0;
1555 int len0 = len;
1556 void *buddy;
1557
1558 BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1559 BUG_ON(e4b->bd_group != ex->fe_group);
Vincent Minetbc8e6742009-05-15 08:33:18 -04001560 assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
Alex Tomasc9de5602008-01-29 00:19:52 -05001561 mb_check_buddy(e4b);
1562 mb_mark_used_double(e4b, start, len);
1563
1564 e4b->bd_info->bb_free -= len;
1565 if (e4b->bd_info->bb_first_free == start)
1566 e4b->bd_info->bb_first_free += len;
1567
1568 /* let's maintain fragments counter */
1569 if (start != 0)
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001570 mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001571 if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001572 max = !mb_test_bit(start + len, e4b->bd_bitmap);
Alex Tomasc9de5602008-01-29 00:19:52 -05001573 if (mlen && max)
1574 e4b->bd_info->bb_fragments++;
1575 else if (!mlen && !max)
1576 e4b->bd_info->bb_fragments--;
1577
1578 /* let's maintain buddy itself */
1579 while (len) {
1580 ord = mb_find_order_for_block(e4b, start);
1581
1582 if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1583 /* the whole chunk may be allocated at once! */
1584 mlen = 1 << ord;
1585 buddy = mb_find_buddy(e4b, ord, &max);
1586 BUG_ON((start >> ord) >= max);
1587 mb_set_bit(start >> ord, buddy);
1588 e4b->bd_info->bb_counters[ord]--;
1589 start += mlen;
1590 len -= mlen;
1591 BUG_ON(len < 0);
1592 continue;
1593 }
1594
1595 /* store for history */
1596 if (ret == 0)
1597 ret = len | (ord << 16);
1598
1599 /* we have to split large buddy */
1600 BUG_ON(ord <= 0);
1601 buddy = mb_find_buddy(e4b, ord, &max);
1602 mb_set_bit(start >> ord, buddy);
1603 e4b->bd_info->bb_counters[ord]--;
1604
1605 ord--;
1606 cur = (start >> ord) & ~1U;
1607 buddy = mb_find_buddy(e4b, ord, &max);
1608 mb_clear_bit(cur, buddy);
1609 mb_clear_bit(cur + 1, buddy);
1610 e4b->bd_info->bb_counters[ord]++;
1611 e4b->bd_info->bb_counters[ord]++;
1612 }
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04001613 mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05001614
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001615 ext4_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001616 mb_check_buddy(e4b);
1617
1618 return ret;
1619}
1620
1621/*
1622 * Must be called under group lock!
1623 */
1624static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1625 struct ext4_buddy *e4b)
1626{
1627 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1628 int ret;
1629
1630 BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1631 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1632
1633 ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1634 ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1635 ret = mb_mark_used(e4b, &ac->ac_b_ex);
1636
1637 /* preallocation can change ac_b_ex, thus we store actually
1638 * allocated blocks for history */
1639 ac->ac_f_ex = ac->ac_b_ex;
1640
1641 ac->ac_status = AC_STATUS_FOUND;
1642 ac->ac_tail = ret & 0xffff;
1643 ac->ac_buddy = ret >> 16;
1644
Aneesh Kumar K.Vc3a326a2008-11-25 15:11:52 -05001645 /*
1646 * take the page reference. We want the page to be pinned
1647 * so that we don't get a ext4_mb_init_cache_call for this
1648 * group until we update the bitmap. That would mean we
1649 * double allocate blocks. The reference is dropped
1650 * in ext4_mb_release_context
1651 */
Alex Tomasc9de5602008-01-29 00:19:52 -05001652 ac->ac_bitmap_page = e4b->bd_bitmap_page;
1653 get_page(ac->ac_bitmap_page);
1654 ac->ac_buddy_page = e4b->bd_buddy_page;
1655 get_page(ac->ac_buddy_page);
Alex Tomasc9de5602008-01-29 00:19:52 -05001656 /* store last allocated for subsequent stream allocation */
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04001657 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05001658 spin_lock(&sbi->s_md_lock);
1659 sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1660 sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1661 spin_unlock(&sbi->s_md_lock);
1662 }
1663}
1664
1665/*
1666 * regular allocator, for general purposes allocation
1667 */
1668
1669static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1670 struct ext4_buddy *e4b,
1671 int finish_group)
1672{
1673 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1674 struct ext4_free_extent *bex = &ac->ac_b_ex;
1675 struct ext4_free_extent *gex = &ac->ac_g_ex;
1676 struct ext4_free_extent ex;
1677 int max;
1678
Aneesh Kumar K.V032115f2009-01-05 21:34:30 -05001679 if (ac->ac_status == AC_STATUS_FOUND)
1680 return;
Alex Tomasc9de5602008-01-29 00:19:52 -05001681 /*
1682 * We don't want to scan for a whole year
1683 */
1684 if (ac->ac_found > sbi->s_mb_max_to_scan &&
1685 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1686 ac->ac_status = AC_STATUS_BREAK;
1687 return;
1688 }
1689
1690 /*
1691 * Haven't found good chunk so far, let's continue
1692 */
1693 if (bex->fe_len < gex->fe_len)
1694 return;
1695
1696 if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1697 && bex->fe_group == e4b->bd_group) {
1698 /* recheck chunk's availability - we don't know
1699 * when it was found (within this lock-unlock
1700 * period or not) */
Robin Dong15c006a2012-08-17 10:02:17 -04001701 max = mb_find_extent(e4b, bex->fe_start, gex->fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001702 if (max >= gex->fe_len) {
1703 ext4_mb_use_best_found(ac, e4b);
1704 return;
1705 }
1706 }
1707}
1708
1709/*
1710 * The routine checks whether found extent is good enough. If it is,
1711 * then the extent gets marked used and flag is set to the context
1712 * to stop scanning. Otherwise, the extent is compared with the
1713 * previous found extent and if new one is better, then it's stored
1714 * in the context. Later, the best found extent will be used, if
1715 * mballoc can't find good enough extent.
1716 *
1717 * FIXME: real allocation policy is to be designed yet!
1718 */
1719static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1720 struct ext4_free_extent *ex,
1721 struct ext4_buddy *e4b)
1722{
1723 struct ext4_free_extent *bex = &ac->ac_b_ex;
1724 struct ext4_free_extent *gex = &ac->ac_g_ex;
1725
1726 BUG_ON(ex->fe_len <= 0);
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001727 BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
1728 BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05001729 BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1730
1731 ac->ac_found++;
1732
1733 /*
1734 * The special case - take what you catch first
1735 */
1736 if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1737 *bex = *ex;
1738 ext4_mb_use_best_found(ac, e4b);
1739 return;
1740 }
1741
1742 /*
1743 * Let's check whether the chuck is good enough
1744 */
1745 if (ex->fe_len == gex->fe_len) {
1746 *bex = *ex;
1747 ext4_mb_use_best_found(ac, e4b);
1748 return;
1749 }
1750
1751 /*
1752 * If this is first found extent, just store it in the context
1753 */
1754 if (bex->fe_len == 0) {
1755 *bex = *ex;
1756 return;
1757 }
1758
1759 /*
1760 * If new found extent is better, store it in the context
1761 */
1762 if (bex->fe_len < gex->fe_len) {
1763 /* if the request isn't satisfied, any found extent
1764 * larger than previous best one is better */
1765 if (ex->fe_len > bex->fe_len)
1766 *bex = *ex;
1767 } else if (ex->fe_len > gex->fe_len) {
1768 /* if the request is satisfied, then we try to find
1769 * an extent that still satisfy the request, but is
1770 * smaller than previous one */
1771 if (ex->fe_len < bex->fe_len)
1772 *bex = *ex;
1773 }
1774
1775 ext4_mb_check_limits(ac, e4b, 0);
1776}
1777
Eric Sandeen089ceec2009-07-05 22:17:31 -04001778static noinline_for_stack
1779int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001780 struct ext4_buddy *e4b)
1781{
1782 struct ext4_free_extent ex = ac->ac_b_ex;
1783 ext4_group_t group = ex.fe_group;
1784 int max;
1785 int err;
1786
1787 BUG_ON(ex.fe_len <= 0);
1788 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1789 if (err)
1790 return err;
1791
1792 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001793 max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001794
1795 if (max > 0) {
1796 ac->ac_b_ex = ex;
1797 ext4_mb_use_best_found(ac, e4b);
1798 }
1799
1800 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001801 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001802
1803 return 0;
1804}
1805
Eric Sandeen089ceec2009-07-05 22:17:31 -04001806static noinline_for_stack
1807int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001808 struct ext4_buddy *e4b)
1809{
1810 ext4_group_t group = ac->ac_g_ex.fe_group;
1811 int max;
1812 int err;
1813 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001814 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05001815 struct ext4_free_extent ex;
1816
1817 if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1818 return 0;
Yongqiang Yang838cd0c2012-09-23 23:10:51 -04001819 if (grp->bb_free == 0)
1820 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05001821
1822 err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1823 if (err)
1824 return err;
1825
Darrick J. Wong163a2032013-08-28 17:35:51 -04001826 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info))) {
1827 ext4_mb_unload_buddy(e4b);
1828 return 0;
1829 }
1830
Alex Tomasc9de5602008-01-29 00:19:52 -05001831 ext4_lock_group(ac->ac_sb, group);
Robin Dong15c006a2012-08-17 10:02:17 -04001832 max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
Alex Tomasc9de5602008-01-29 00:19:52 -05001833 ac->ac_g_ex.fe_len, &ex);
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001834 ex.fe_logical = 0xDEADFA11; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001835
1836 if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1837 ext4_fsblk_t start;
1838
Akinobu Mita5661bd62010-03-03 23:53:39 -05001839 start = ext4_group_first_block_no(ac->ac_sb, e4b->bd_group) +
1840 ex.fe_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05001841 /* use do_div to get remainder (would be 64-bit modulo) */
1842 if (do_div(start, sbi->s_stripe) == 0) {
1843 ac->ac_found++;
1844 ac->ac_b_ex = ex;
1845 ext4_mb_use_best_found(ac, e4b);
1846 }
1847 } else if (max >= ac->ac_g_ex.fe_len) {
1848 BUG_ON(ex.fe_len <= 0);
1849 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1850 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1851 ac->ac_found++;
1852 ac->ac_b_ex = ex;
1853 ext4_mb_use_best_found(ac, e4b);
1854 } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1855 /* Sometimes, caller may want to merge even small
1856 * number of blocks to an existing extent */
1857 BUG_ON(ex.fe_len <= 0);
1858 BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1859 BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1860 ac->ac_found++;
1861 ac->ac_b_ex = ex;
1862 ext4_mb_use_best_found(ac, e4b);
1863 }
1864 ext4_unlock_group(ac->ac_sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04001865 ext4_mb_unload_buddy(e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05001866
1867 return 0;
1868}
1869
1870/*
1871 * The routine scans buddy structures (not bitmap!) from given order
1872 * to max order and tries to find big enough chunk to satisfy the req
1873 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001874static noinline_for_stack
1875void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001876 struct ext4_buddy *e4b)
1877{
1878 struct super_block *sb = ac->ac_sb;
1879 struct ext4_group_info *grp = e4b->bd_info;
1880 void *buddy;
1881 int i;
1882 int k;
1883 int max;
1884
1885 BUG_ON(ac->ac_2order <= 0);
1886 for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1887 if (grp->bb_counters[i] == 0)
1888 continue;
1889
1890 buddy = mb_find_buddy(e4b, i, &max);
1891 BUG_ON(buddy == NULL);
1892
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001893 k = mb_find_next_zero_bit(buddy, max, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001894 BUG_ON(k >= max);
1895
1896 ac->ac_found++;
1897
1898 ac->ac_b_ex.fe_len = 1 << i;
1899 ac->ac_b_ex.fe_start = k << i;
1900 ac->ac_b_ex.fe_group = e4b->bd_group;
1901
1902 ext4_mb_use_best_found(ac, e4b);
1903
1904 BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1905
1906 if (EXT4_SB(sb)->s_mb_stats)
1907 atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1908
1909 break;
1910 }
1911}
1912
1913/*
1914 * The routine scans the group and measures all found extents.
1915 * In order to optimize scanning, caller must pass number of
1916 * free blocks in the group, so the routine can know upper limit.
1917 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001918static noinline_for_stack
1919void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001920 struct ext4_buddy *e4b)
1921{
1922 struct super_block *sb = ac->ac_sb;
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001923 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001924 struct ext4_free_extent ex;
1925 int i;
1926 int free;
1927
1928 free = e4b->bd_info->bb_free;
1929 BUG_ON(free <= 0);
1930
1931 i = e4b->bd_info->bb_first_free;
1932
1933 while (free && ac->ac_status == AC_STATUS_CONTINUE) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05001934 i = mb_find_next_zero_bit(bitmap,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001935 EXT4_CLUSTERS_PER_GROUP(sb), i);
1936 if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001937 /*
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001938 * IF we have corrupt bitmap, we won't find any
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001939 * free blocks even though group info says we
1940 * we have free blocks
1941 */
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001942 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001943 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001944 "group info. But bitmap says 0",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001945 free);
Alex Tomasc9de5602008-01-29 00:19:52 -05001946 break;
1947 }
1948
Robin Dong15c006a2012-08-17 10:02:17 -04001949 mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001950 BUG_ON(ex.fe_len <= 0);
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001951 if (free < ex.fe_len) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04001952 ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04001953 "%d free clusters as per "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001954 "group info. But got %d blocks",
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001955 free, ex.fe_len);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05001956 /*
1957 * The number of free blocks differs. This mostly
1958 * indicate that the bitmap is corrupt. So exit
1959 * without claiming the space.
1960 */
1961 break;
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05001962 }
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05001963 ex.fe_logical = 0xDEADC0DE; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05001964 ext4_mb_measure_extent(ac, &ex, e4b);
1965
1966 i += ex.fe_len;
1967 free -= ex.fe_len;
1968 }
1969
1970 ext4_mb_check_limits(ac, e4b, 1);
1971}
1972
1973/*
1974 * This is a special case for storages like raid5
Eric Sandeen506bf2d2010-07-27 11:56:06 -04001975 * we try to find stripe-aligned chunks for stripe-size-multiple requests
Alex Tomasc9de5602008-01-29 00:19:52 -05001976 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04001977static noinline_for_stack
1978void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05001979 struct ext4_buddy *e4b)
1980{
1981 struct super_block *sb = ac->ac_sb;
1982 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'oc5e8f3f2012-02-20 17:54:06 -05001983 void *bitmap = e4b->bd_bitmap;
Alex Tomasc9de5602008-01-29 00:19:52 -05001984 struct ext4_free_extent ex;
1985 ext4_fsblk_t first_group_block;
1986 ext4_fsblk_t a;
1987 ext4_grpblk_t i;
1988 int max;
1989
1990 BUG_ON(sbi->s_stripe == 0);
1991
1992 /* find first stripe-aligned block in group */
Akinobu Mita5661bd62010-03-03 23:53:39 -05001993 first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
1994
Alex Tomasc9de5602008-01-29 00:19:52 -05001995 a = first_group_block + sbi->s_stripe - 1;
1996 do_div(a, sbi->s_stripe);
1997 i = (a * sbi->s_stripe) - first_group_block;
1998
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04001999 while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002000 if (!mb_test_bit(i, bitmap)) {
Robin Dong15c006a2012-08-17 10:02:17 -04002001 max = mb_find_extent(e4b, i, sbi->s_stripe, &ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002002 if (max >= sbi->s_stripe) {
2003 ac->ac_found++;
Theodore Ts'oab0c00f2014-02-20 00:36:41 -05002004 ex.fe_logical = 0xDEADF00D; /* debug value */
Alex Tomasc9de5602008-01-29 00:19:52 -05002005 ac->ac_b_ex = ex;
2006 ext4_mb_use_best_found(ac, e4b);
2007 break;
2008 }
2009 }
2010 i += sbi->s_stripe;
2011 }
2012}
2013
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002014/* This is now called BEFORE we load the buddy bitmap. */
Alex Tomasc9de5602008-01-29 00:19:52 -05002015static int ext4_mb_good_group(struct ext4_allocation_context *ac,
2016 ext4_group_t group, int cr)
2017{
2018 unsigned free, fragments;
Theodore Ts'oa4912122009-03-12 12:18:34 -04002019 int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05002020 struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2021
2022 BUG_ON(cr < 0 || cr >= 4);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002023
Theodore Ts'o01fc48e2012-08-17 09:46:17 -04002024 free = grp->bb_free;
2025 if (free == 0)
2026 return 0;
2027 if (cr <= 2 && free < ac->ac_g_ex.fe_len)
2028 return 0;
2029
Darrick J. Wong163a2032013-08-28 17:35:51 -04002030 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
2031 return 0;
2032
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002033 /* We only do this if the grp has never been initialized */
2034 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
2035 int ret = ext4_mb_init_group(ac->ac_sb, group);
2036 if (ret)
2037 return 0;
2038 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002039
Alex Tomasc9de5602008-01-29 00:19:52 -05002040 fragments = grp->bb_fragments;
Alex Tomasc9de5602008-01-29 00:19:52 -05002041 if (fragments == 0)
2042 return 0;
2043
2044 switch (cr) {
2045 case 0:
2046 BUG_ON(ac->ac_2order == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05002047
Theodore Ts'oa4912122009-03-12 12:18:34 -04002048 /* Avoid using the first bg of a flexgroup for data files */
2049 if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2050 (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2051 ((group % flex_size) == 0))
2052 return 0;
2053
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002054 if ((ac->ac_2order > ac->ac_sb->s_blocksize_bits+1) ||
2055 (free / fragments) >= ac->ac_g_ex.fe_len)
2056 return 1;
2057
2058 if (grp->bb_largest_free_order < ac->ac_2order)
2059 return 0;
2060
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002061 return 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002062 case 1:
2063 if ((free / fragments) >= ac->ac_g_ex.fe_len)
2064 return 1;
2065 break;
2066 case 2:
2067 if (free >= ac->ac_g_ex.fe_len)
2068 return 1;
2069 break;
2070 case 3:
2071 return 1;
2072 default:
2073 BUG();
2074 }
2075
2076 return 0;
2077}
2078
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002079static noinline_for_stack int
2080ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05002081{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002082 ext4_group_t ngroups, group, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002083 int cr;
2084 int err = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002085 struct ext4_sb_info *sbi;
2086 struct super_block *sb;
2087 struct ext4_buddy e4b;
Alex Tomasc9de5602008-01-29 00:19:52 -05002088
2089 sb = ac->ac_sb;
2090 sbi = EXT4_SB(sb);
Theodore Ts'o8df96752009-05-01 08:50:38 -04002091 ngroups = ext4_get_groups_count(sb);
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002092 /* non-extent files are limited to low blocks/groups */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002093 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002094 ngroups = sbi->s_blockfile_groups;
2095
Alex Tomasc9de5602008-01-29 00:19:52 -05002096 BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2097
2098 /* first, try the goal */
2099 err = ext4_mb_find_by_goal(ac, &e4b);
2100 if (err || ac->ac_status == AC_STATUS_FOUND)
2101 goto out;
2102
2103 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2104 goto out;
2105
2106 /*
2107 * ac->ac2_order is set only if the fe_len is a power of 2
2108 * if ac2_order is set we also set criteria to 0 so that we
2109 * try exact allocation using buddy.
2110 */
2111 i = fls(ac->ac_g_ex.fe_len);
2112 ac->ac_2order = 0;
2113 /*
2114 * We search using buddy data only if the order of the request
2115 * is greater than equal to the sbi_s_mb_order2_reqs
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002116 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
Alex Tomasc9de5602008-01-29 00:19:52 -05002117 */
2118 if (i >= sbi->s_mb_order2_reqs) {
2119 /*
2120 * This should tell if fe_len is exactly power of 2
2121 */
2122 if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
2123 ac->ac_2order = i - 1;
2124 }
2125
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002126 /* if stream allocation is enabled, use global goal */
2127 if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002128 /* TBD: may be hot point */
2129 spin_lock(&sbi->s_md_lock);
2130 ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2131 ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2132 spin_unlock(&sbi->s_md_lock);
2133 }
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04002134
Alex Tomasc9de5602008-01-29 00:19:52 -05002135 /* Let's just scan groups to find more-less suitable blocks */
2136 cr = ac->ac_2order ? 0 : 1;
2137 /*
2138 * cr == 0 try to get exact allocation,
2139 * cr == 3 try to get anything
2140 */
2141repeat:
2142 for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2143 ac->ac_criteria = cr;
Aneesh Kumar K.Ved8f9c72008-07-11 19:27:31 -04002144 /*
2145 * searching for the right group start
2146 * from the goal value specified
2147 */
2148 group = ac->ac_g_ex.fe_group;
2149
Theodore Ts'o8df96752009-05-01 08:50:38 -04002150 for (i = 0; i < ngroups; group++, i++) {
Theodore Ts'o2ed57242013-06-12 11:43:02 -04002151 cond_resched();
Lachlan McIlroye6155732013-05-05 23:10:00 -04002152 /*
2153 * Artificially restricted ngroups for non-extent
2154 * files makes group > ngroups possible on first loop.
2155 */
2156 if (group >= ngroups)
Alex Tomasc9de5602008-01-29 00:19:52 -05002157 group = 0;
2158
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002159 /* This now checks without needing the buddy page */
2160 if (!ext4_mb_good_group(ac, group, cr))
Alex Tomasc9de5602008-01-29 00:19:52 -05002161 continue;
2162
Alex Tomasc9de5602008-01-29 00:19:52 -05002163 err = ext4_mb_load_buddy(sb, group, &e4b);
2164 if (err)
2165 goto out;
2166
2167 ext4_lock_group(sb, group);
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002168
2169 /*
2170 * We need to check again after locking the
2171 * block group
2172 */
Alex Tomasc9de5602008-01-29 00:19:52 -05002173 if (!ext4_mb_good_group(ac, group, cr)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002174 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002175 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002176 continue;
2177 }
2178
2179 ac->ac_groups_scanned++;
Theodore Ts'o40ae3482013-02-04 15:08:40 -05002180 if (cr == 0 && ac->ac_2order < sb->s_blocksize_bits+2)
Alex Tomasc9de5602008-01-29 00:19:52 -05002181 ext4_mb_simple_scan_group(ac, &e4b);
Eric Sandeen506bf2d2010-07-27 11:56:06 -04002182 else if (cr == 1 && sbi->s_stripe &&
2183 !(ac->ac_g_ex.fe_len % sbi->s_stripe))
Alex Tomasc9de5602008-01-29 00:19:52 -05002184 ext4_mb_scan_aligned(ac, &e4b);
2185 else
2186 ext4_mb_complex_scan_group(ac, &e4b);
2187
2188 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04002189 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002190
2191 if (ac->ac_status != AC_STATUS_CONTINUE)
2192 break;
2193 }
2194 }
2195
2196 if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2197 !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2198 /*
2199 * We've been searching too long. Let's try to allocate
2200 * the best chunk we've found so far
2201 */
2202
2203 ext4_mb_try_best_found(ac, &e4b);
2204 if (ac->ac_status != AC_STATUS_FOUND) {
2205 /*
2206 * Someone more lucky has already allocated it.
2207 * The only thing we can do is just take first
2208 * found block(s)
2209 printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2210 */
2211 ac->ac_b_ex.fe_group = 0;
2212 ac->ac_b_ex.fe_start = 0;
2213 ac->ac_b_ex.fe_len = 0;
2214 ac->ac_status = AC_STATUS_CONTINUE;
2215 ac->ac_flags |= EXT4_MB_HINT_FIRST;
2216 cr = 3;
2217 atomic_inc(&sbi->s_mb_lost_chunks);
2218 goto repeat;
2219 }
2220 }
2221out:
2222 return err;
2223}
2224
Alex Tomasc9de5602008-01-29 00:19:52 -05002225static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2226{
2227 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002228 ext4_group_t group;
2229
Theodore Ts'o8df96752009-05-01 08:50:38 -04002230 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002231 return NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002232 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002233 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002234}
2235
2236static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2237{
2238 struct super_block *sb = seq->private;
Alex Tomasc9de5602008-01-29 00:19:52 -05002239 ext4_group_t group;
2240
2241 ++*pos;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002242 if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
Alex Tomasc9de5602008-01-29 00:19:52 -05002243 return NULL;
2244 group = *pos + 1;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002245 return (void *) ((unsigned long) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002246}
2247
2248static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2249{
2250 struct super_block *sb = seq->private;
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002251 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
Alex Tomasc9de5602008-01-29 00:19:52 -05002252 int i;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002253 int err, buddy_loaded = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002254 struct ext4_buddy e4b;
Aditya Kali1c8457c2012-06-30 19:10:57 -04002255 struct ext4_group_info *grinfo;
Alex Tomasc9de5602008-01-29 00:19:52 -05002256 struct sg {
2257 struct ext4_group_info info;
Eric Sandeena36b4492009-08-25 22:36:45 -04002258 ext4_grpblk_t counters[16];
Alex Tomasc9de5602008-01-29 00:19:52 -05002259 } sg;
2260
2261 group--;
2262 if (group == 0)
2263 seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2264 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2265 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2266 "group", "free", "frags", "first",
2267 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2268 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2269
2270 i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2271 sizeof(struct ext4_group_info);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002272 grinfo = ext4_get_group_info(sb, group);
2273 /* Load the group info in memory only if not already loaded. */
2274 if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
2275 err = ext4_mb_load_buddy(sb, group, &e4b);
2276 if (err) {
2277 seq_printf(seq, "#%-5u: I/O error\n", group);
2278 return 0;
2279 }
2280 buddy_loaded = 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002281 }
Aditya Kali1c8457c2012-06-30 19:10:57 -04002282
Alex Tomasc9de5602008-01-29 00:19:52 -05002283 memcpy(&sg, ext4_get_group_info(sb, group), i);
Aditya Kali1c8457c2012-06-30 19:10:57 -04002284
2285 if (buddy_loaded)
2286 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002287
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002288 seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
Alex Tomasc9de5602008-01-29 00:19:52 -05002289 sg.info.bb_fragments, sg.info.bb_first_free);
2290 for (i = 0; i <= 13; i++)
2291 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2292 sg.info.bb_counters[i] : 0);
2293 seq_printf(seq, " ]\n");
2294
2295 return 0;
2296}
2297
2298static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2299{
2300}
2301
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002302static const struct seq_operations ext4_mb_seq_groups_ops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002303 .start = ext4_mb_seq_groups_start,
2304 .next = ext4_mb_seq_groups_next,
2305 .stop = ext4_mb_seq_groups_stop,
2306 .show = ext4_mb_seq_groups_show,
2307};
2308
2309static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2310{
Al Virod9dda782013-03-31 18:16:14 -04002311 struct super_block *sb = PDE_DATA(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002312 int rc;
2313
2314 rc = seq_open(file, &ext4_mb_seq_groups_ops);
2315 if (rc == 0) {
Joe Perchesa271fe82010-07-27 11:56:04 -04002316 struct seq_file *m = file->private_data;
Alex Tomasc9de5602008-01-29 00:19:52 -05002317 m->private = sb;
2318 }
2319 return rc;
2320
2321}
2322
Tobias Klauser7f1346a2009-09-05 09:28:54 -04002323static const struct file_operations ext4_mb_seq_groups_fops = {
Alex Tomasc9de5602008-01-29 00:19:52 -05002324 .owner = THIS_MODULE,
2325 .open = ext4_mb_seq_groups_open,
2326 .read = seq_read,
2327 .llseek = seq_lseek,
2328 .release = seq_release,
2329};
2330
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002331static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
2332{
2333 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2334 struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
2335
2336 BUG_ON(!cachep);
2337 return cachep;
2338}
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002339
Theodore Ts'o28623c22012-09-05 01:31:50 -04002340/*
2341 * Allocate the top-level s_group_info array for the specified number
2342 * of groups
2343 */
2344int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
2345{
2346 struct ext4_sb_info *sbi = EXT4_SB(sb);
2347 unsigned size;
2348 struct ext4_group_info ***new_groupinfo;
2349
2350 size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
2351 EXT4_DESC_PER_BLOCK_BITS(sb);
2352 if (size <= sbi->s_group_info_size)
2353 return 0;
2354
2355 size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
2356 new_groupinfo = ext4_kvzalloc(size, GFP_KERNEL);
2357 if (!new_groupinfo) {
2358 ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
2359 return -ENOMEM;
2360 }
2361 if (sbi->s_group_info) {
2362 memcpy(new_groupinfo, sbi->s_group_info,
2363 sbi->s_group_info_size * sizeof(*sbi->s_group_info));
Al Virob93b41d2014-11-20 12:19:11 -05002364 kvfree(sbi->s_group_info);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002365 }
2366 sbi->s_group_info = new_groupinfo;
2367 sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
2368 ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
2369 sbi->s_group_info_size);
2370 return 0;
2371}
2372
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002373/* Create and initialize ext4_group_info data for the given group. */
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002374int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002375 struct ext4_group_desc *desc)
2376{
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002377 int i;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002378 int metalen = 0;
2379 struct ext4_sb_info *sbi = EXT4_SB(sb);
2380 struct ext4_group_info **meta_group_info;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002381 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002382
2383 /*
2384 * First check if this group is the first of a reserved block.
2385 * If it's true, we have to allocate a new table of pointers
2386 * to ext4_group_info structures
2387 */
2388 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
2389 metalen = sizeof(*meta_group_info) <<
2390 EXT4_DESC_PER_BLOCK_BITS(sb);
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002391 meta_group_info = kmalloc(metalen, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002392 if (meta_group_info == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002393 ext4_msg(sb, KERN_ERR, "can't allocate mem "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002394 "for a buddy group");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002395 goto exit_meta_group_info;
2396 }
2397 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
2398 meta_group_info;
2399 }
2400
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002401 meta_group_info =
2402 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
2403 i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
2404
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002405 meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002406 if (meta_group_info[i] == NULL) {
Joe Perches7f6a11e2012-03-19 23:09:43 -04002407 ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002408 goto exit_group_info;
2409 }
2410 set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
2411 &(meta_group_info[i]->bb_state));
2412
2413 /*
2414 * initialize bb_free to be able to skip
2415 * empty groups without initialization
2416 */
2417 if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2418 meta_group_info[i]->bb_free =
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002419 ext4_free_clusters_after_init(sb, group, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002420 } else {
2421 meta_group_info[i]->bb_free =
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002422 ext4_free_group_clusters(sb, desc);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002423 }
2424
2425 INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05002426 init_rwsem(&meta_group_info[i]->alloc_sem);
Venkatesh Pallipadi64e290e2010-03-04 22:25:21 -05002427 meta_group_info[i]->bb_free_root = RB_ROOT;
Curt Wohlgemuth8a57d9d2010-05-16 15:00:00 -04002428 meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002429
2430#ifdef DOUBLE_CHECK
2431 {
2432 struct buffer_head *bh;
2433 meta_group_info[i]->bb_bitmap =
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05002434 kmalloc(sb->s_blocksize, GFP_NOFS);
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002435 BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
2436 bh = ext4_read_block_bitmap(sb, group);
2437 BUG_ON(bh == NULL);
2438 memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
2439 sb->s_blocksize);
2440 put_bh(bh);
2441 }
2442#endif
2443
2444 return 0;
2445
2446exit_group_info:
2447 /* If a meta_group_info table has been allocated, release it now */
Tao Macaaf7a22011-07-11 18:42:42 -04002448 if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002449 kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
Tao Macaaf7a22011-07-11 18:42:42 -04002450 sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] = NULL;
2451 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002452exit_meta_group_info:
2453 return -ENOMEM;
2454} /* ext4_mb_add_groupinfo */
2455
Alex Tomasc9de5602008-01-29 00:19:52 -05002456static int ext4_mb_init_backend(struct super_block *sb)
2457{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002458 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002459 ext4_group_t i;
Alex Tomasc9de5602008-01-29 00:19:52 -05002460 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o28623c22012-09-05 01:31:50 -04002461 int err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002462 struct ext4_group_desc *desc;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002463 struct kmem_cache *cachep;
Alex Tomasc9de5602008-01-29 00:19:52 -05002464
Theodore Ts'o28623c22012-09-05 01:31:50 -04002465 err = ext4_mb_alloc_groupinfo(sb, ngroups);
2466 if (err)
2467 return err;
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002468
Alex Tomasc9de5602008-01-29 00:19:52 -05002469 sbi->s_buddy_cache = new_inode(sb);
2470 if (sbi->s_buddy_cache == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002471 ext4_msg(sb, KERN_ERR, "can't get new inode");
Alex Tomasc9de5602008-01-29 00:19:52 -05002472 goto err_freesgi;
2473 }
Yu Jian48e60612011-08-01 17:41:39 -04002474 /* To avoid potentially colliding with an valid on-disk inode number,
2475 * use EXT4_BAD_INO for the buddy cache inode number. This inode is
2476 * not in the inode hash, so it should never be found by iget(), but
2477 * this will avoid confusion if it ever shows up during debugging. */
2478 sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
Alex Tomasc9de5602008-01-29 00:19:52 -05002479 EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
Theodore Ts'o8df96752009-05-01 08:50:38 -04002480 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002481 desc = ext4_get_group_desc(sb, i, NULL);
2482 if (desc == NULL) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002483 ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002484 goto err_freebuddy;
2485 }
Frederic Bohe5f21b0e2008-07-11 19:27:31 -04002486 if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
2487 goto err_freebuddy;
Alex Tomasc9de5602008-01-29 00:19:52 -05002488 }
2489
2490 return 0;
2491
2492err_freebuddy:
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002493 cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Roel Kluinf1fa3342008-04-29 22:01:15 -04002494 while (i-- > 0)
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002495 kmem_cache_free(cachep, ext4_get_group_info(sb, i));
Theodore Ts'o28623c22012-09-05 01:31:50 -04002496 i = sbi->s_group_info_size;
Roel Kluinf1fa3342008-04-29 22:01:15 -04002497 while (i-- > 0)
Alex Tomasc9de5602008-01-29 00:19:52 -05002498 kfree(sbi->s_group_info[i]);
2499 iput(sbi->s_buddy_cache);
2500err_freesgi:
Al Virob93b41d2014-11-20 12:19:11 -05002501 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002502 return -ENOMEM;
2503}
2504
Eric Sandeen2892c152011-02-12 08:12:18 -05002505static void ext4_groupinfo_destroy_slabs(void)
2506{
2507 int i;
2508
2509 for (i = 0; i < NR_GRPINFO_CACHES; i++) {
2510 if (ext4_groupinfo_caches[i])
2511 kmem_cache_destroy(ext4_groupinfo_caches[i]);
2512 ext4_groupinfo_caches[i] = NULL;
2513 }
2514}
2515
2516static int ext4_groupinfo_create_slab(size_t size)
2517{
2518 static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
2519 int slab_size;
2520 int blocksize_bits = order_base_2(size);
2521 int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
2522 struct kmem_cache *cachep;
2523
2524 if (cache_index >= NR_GRPINFO_CACHES)
2525 return -EINVAL;
2526
2527 if (unlikely(cache_index < 0))
2528 cache_index = 0;
2529
2530 mutex_lock(&ext4_grpinfo_slab_create_mutex);
2531 if (ext4_groupinfo_caches[cache_index]) {
2532 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2533 return 0; /* Already created */
2534 }
2535
2536 slab_size = offsetof(struct ext4_group_info,
2537 bb_counters[blocksize_bits + 2]);
2538
2539 cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
2540 slab_size, 0, SLAB_RECLAIM_ACCOUNT,
2541 NULL);
2542
Tao Ma823ba012011-07-11 18:26:01 -04002543 ext4_groupinfo_caches[cache_index] = cachep;
2544
Eric Sandeen2892c152011-02-12 08:12:18 -05002545 mutex_unlock(&ext4_grpinfo_slab_create_mutex);
2546 if (!cachep) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002547 printk(KERN_EMERG
2548 "EXT4-fs: no memory for groupinfo slab cache\n");
Eric Sandeen2892c152011-02-12 08:12:18 -05002549 return -ENOMEM;
2550 }
2551
Eric Sandeen2892c152011-02-12 08:12:18 -05002552 return 0;
2553}
2554
Akira Fujita9d990122012-05-28 14:19:25 -04002555int ext4_mb_init(struct super_block *sb)
Alex Tomasc9de5602008-01-29 00:19:52 -05002556{
2557 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002558 unsigned i, j;
Nicolai Stange7346b872016-05-05 19:46:19 -04002559 unsigned offset, offset_incr;
Alex Tomasc9de5602008-01-29 00:19:52 -05002560 unsigned max;
Shen Feng74767c52008-07-11 19:27:31 -04002561 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002562
Eric Sandeen19278052009-08-25 22:36:25 -04002563 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_offsets);
Alex Tomasc9de5602008-01-29 00:19:52 -05002564
2565 sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2566 if (sbi->s_mb_offsets == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002567 ret = -ENOMEM;
2568 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002569 }
Yasunori Gotoff7ef322008-12-17 00:48:39 -05002570
Eric Sandeen19278052009-08-25 22:36:25 -04002571 i = (sb->s_blocksize_bits + 2) * sizeof(*sbi->s_mb_maxs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002572 sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2573 if (sbi->s_mb_maxs == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002574 ret = -ENOMEM;
2575 goto out;
2576 }
2577
Eric Sandeen2892c152011-02-12 08:12:18 -05002578 ret = ext4_groupinfo_create_slab(sb->s_blocksize);
2579 if (ret < 0)
2580 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002581
2582 /* order 0 is regular bitmap */
2583 sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2584 sbi->s_mb_offsets[0] = 0;
2585
2586 i = 1;
2587 offset = 0;
Nicolai Stange7346b872016-05-05 19:46:19 -04002588 offset_incr = 1 << (sb->s_blocksize_bits - 1);
Alex Tomasc9de5602008-01-29 00:19:52 -05002589 max = sb->s_blocksize << 2;
2590 do {
2591 sbi->s_mb_offsets[i] = offset;
2592 sbi->s_mb_maxs[i] = max;
Nicolai Stange7346b872016-05-05 19:46:19 -04002593 offset += offset_incr;
2594 offset_incr = offset_incr >> 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05002595 max = max >> 1;
2596 i++;
2597 } while (i <= sb->s_blocksize_bits + 1);
2598
Alex Tomasc9de5602008-01-29 00:19:52 -05002599 spin_lock_init(&sbi->s_md_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002600 spin_lock_init(&sbi->s_bal_lock);
2601
2602 sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2603 sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2604 sbi->s_mb_stats = MB_DEFAULT_STATS;
2605 sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2606 sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
Theodore Ts'o27baebb2011-09-09 19:02:51 -04002607 /*
2608 * The default group preallocation is 512, which for 4k block
2609 * sizes translates to 2 megabytes. However for bigalloc file
2610 * systems, this is probably too big (i.e, if the cluster size
2611 * is 1 megabyte, then group preallocation size becomes half a
2612 * gigabyte!). As a default, we will keep a two megabyte
2613 * group pralloc size for cluster sizes up to 64k, and after
2614 * that, we will force a minimum group preallocation size of
2615 * 32 clusters. This translates to 8 megs when the cluster
2616 * size is 256k, and 32 megs when the cluster size is 1 meg,
2617 * which seems reasonable as a default.
2618 */
2619 sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
2620 sbi->s_cluster_bits, 32);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002621 /*
2622 * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
2623 * to the lowest multiple of s_stripe which is bigger than
2624 * the s_mb_group_prealloc as determined above. We want
2625 * the preallocation size to be an exact multiple of the
2626 * RAID stripe size so that preallocations don't fragment
2627 * the stripes.
2628 */
2629 if (sbi->s_stripe > 1) {
2630 sbi->s_mb_group_prealloc = roundup(
2631 sbi->s_mb_group_prealloc, sbi->s_stripe);
2632 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002633
Eric Sandeen730c2132008-09-13 15:23:29 -04002634 sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002635 if (sbi->s_locality_groups == NULL) {
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002636 ret = -ENOMEM;
Andrey Tsyvarev029b10c2014-05-12 12:34:21 -04002637 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05002638 }
Eric Sandeen730c2132008-09-13 15:23:29 -04002639 for_each_possible_cpu(i) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002640 struct ext4_locality_group *lg;
Eric Sandeen730c2132008-09-13 15:23:29 -04002641 lg = per_cpu_ptr(sbi->s_locality_groups, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05002642 mutex_init(&lg->lg_mutex);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04002643 for (j = 0; j < PREALLOC_TB_SIZE; j++)
2644 INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
Alex Tomasc9de5602008-01-29 00:19:52 -05002645 spin_lock_init(&lg->lg_prealloc_lock);
2646 }
2647
Yu Jian79a77c52011-08-01 17:41:46 -04002648 /* init file for buddy data */
2649 ret = ext4_mb_init_backend(sb);
Tao Ma7aa0bae2011-10-06 10:22:28 -04002650 if (ret != 0)
2651 goto out_free_locality_groups;
Yu Jian79a77c52011-08-01 17:41:46 -04002652
Theodore Ts'o296c3552009-09-30 00:32:42 -04002653 if (sbi->s_proc)
2654 proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
2655 &ext4_mb_seq_groups_fops, sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002656
Tao Ma7aa0bae2011-10-06 10:22:28 -04002657 return 0;
2658
2659out_free_locality_groups:
2660 free_percpu(sbi->s_locality_groups);
2661 sbi->s_locality_groups = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002662out:
Tao Ma7aa0bae2011-10-06 10:22:28 -04002663 kfree(sbi->s_mb_offsets);
2664 sbi->s_mb_offsets = NULL;
2665 kfree(sbi->s_mb_maxs);
2666 sbi->s_mb_maxs = NULL;
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002667 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05002668}
2669
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002670/* need to called with the ext4 group lock held */
Alex Tomasc9de5602008-01-29 00:19:52 -05002671static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2672{
2673 struct ext4_prealloc_space *pa;
2674 struct list_head *cur, *tmp;
2675 int count = 0;
2676
2677 list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2678 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2679 list_del(&pa->pa_group_list);
2680 count++;
Aneesh Kumar K.V688f05a2008-10-13 12:14:14 -04002681 kmem_cache_free(ext4_pspace_cachep, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05002682 }
2683 if (count)
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002684 mb_debug(1, "mballoc: %u PAs left\n", count);
Alex Tomasc9de5602008-01-29 00:19:52 -05002685
2686}
2687
2688int ext4_mb_release(struct super_block *sb)
2689{
Theodore Ts'o8df96752009-05-01 08:50:38 -04002690 ext4_group_t ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002691 ext4_group_t i;
2692 int num_meta_group_infos;
2693 struct ext4_group_info *grinfo;
2694 struct ext4_sb_info *sbi = EXT4_SB(sb);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002695 struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
Alex Tomasc9de5602008-01-29 00:19:52 -05002696
Salman Qazi95599962012-05-31 23:52:14 -04002697 if (sbi->s_proc)
2698 remove_proc_entry("mb_groups", sbi->s_proc);
2699
Alex Tomasc9de5602008-01-29 00:19:52 -05002700 if (sbi->s_group_info) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04002701 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05002702 grinfo = ext4_get_group_info(sb, i);
2703#ifdef DOUBLE_CHECK
2704 kfree(grinfo->bb_bitmap);
2705#endif
2706 ext4_lock_group(sb, i);
2707 ext4_mb_cleanup_pa(grinfo);
2708 ext4_unlock_group(sb, i);
Curt Wohlgemuthfb1813f2010-10-27 21:29:12 -04002709 kmem_cache_free(cachep, grinfo);
Alex Tomasc9de5602008-01-29 00:19:52 -05002710 }
Theodore Ts'o8df96752009-05-01 08:50:38 -04002711 num_meta_group_infos = (ngroups +
Alex Tomasc9de5602008-01-29 00:19:52 -05002712 EXT4_DESC_PER_BLOCK(sb) - 1) >>
2713 EXT4_DESC_PER_BLOCK_BITS(sb);
2714 for (i = 0; i < num_meta_group_infos; i++)
2715 kfree(sbi->s_group_info[i]);
Al Virob93b41d2014-11-20 12:19:11 -05002716 kvfree(sbi->s_group_info);
Alex Tomasc9de5602008-01-29 00:19:52 -05002717 }
2718 kfree(sbi->s_mb_offsets);
2719 kfree(sbi->s_mb_maxs);
Markus Elfringbfcba2d2014-11-25 20:01:37 -05002720 iput(sbi->s_buddy_cache);
Alex Tomasc9de5602008-01-29 00:19:52 -05002721 if (sbi->s_mb_stats) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002722 ext4_msg(sb, KERN_INFO,
2723 "mballoc: %u blocks %u reqs (%u success)",
Alex Tomasc9de5602008-01-29 00:19:52 -05002724 atomic_read(&sbi->s_bal_allocated),
2725 atomic_read(&sbi->s_bal_reqs),
2726 atomic_read(&sbi->s_bal_success));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002727 ext4_msg(sb, KERN_INFO,
2728 "mballoc: %u extents scanned, %u goal hits, "
2729 "%u 2^N hits, %u breaks, %u lost",
Alex Tomasc9de5602008-01-29 00:19:52 -05002730 atomic_read(&sbi->s_bal_ex_scanned),
2731 atomic_read(&sbi->s_bal_goals),
2732 atomic_read(&sbi->s_bal_2orders),
2733 atomic_read(&sbi->s_bal_breaks),
2734 atomic_read(&sbi->s_mb_lost_chunks));
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002735 ext4_msg(sb, KERN_INFO,
2736 "mballoc: %lu generated and it took %Lu",
Tao Maced156e2011-07-23 16:18:05 -04002737 sbi->s_mb_buddies_generated,
Alex Tomasc9de5602008-01-29 00:19:52 -05002738 sbi->s_mb_generation_time);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04002739 ext4_msg(sb, KERN_INFO,
2740 "mballoc: %u preallocated, %u discarded",
Alex Tomasc9de5602008-01-29 00:19:52 -05002741 atomic_read(&sbi->s_mb_preallocated),
2742 atomic_read(&sbi->s_mb_discarded));
2743 }
2744
Eric Sandeen730c2132008-09-13 15:23:29 -04002745 free_percpu(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05002746
2747 return 0;
2748}
2749
Lukas Czerner77ca6cd2010-10-27 21:30:11 -04002750static inline int ext4_issue_discard(struct super_block *sb,
JP Abgrallb7b01892014-07-23 16:55:07 -07002751 ext4_group_t block_group, ext4_grpblk_t cluster, int count,
2752 unsigned long flags)
Jiaying Zhang5c521832010-07-27 11:56:05 -04002753{
Jiaying Zhang5c521832010-07-27 11:56:05 -04002754 ext4_fsblk_t discard_block;
2755
Theodore Ts'o84130192011-09-09 18:50:51 -04002756 discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
2757 ext4_group_first_block_no(sb, block_group));
2758 count = EXT4_C2B(EXT4_SB(sb), count);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002759 trace_ext4_discard_blocks(sb,
2760 (unsigned long long) discard_block, count);
JP Abgrallb7b01892014-07-23 16:55:07 -07002761 return sb_issue_discard(sb, discard_block, count, GFP_NOFS, flags);
Jiaying Zhang5c521832010-07-27 11:56:05 -04002762}
2763
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002764/*
2765 * This function is called by the jbd2 layer once the commit has finished,
2766 * so we know we can free the blocks that were released with that commit.
2767 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002768static void ext4_free_data_callback(struct super_block *sb,
2769 struct ext4_journal_cb_entry *jce,
2770 int rc)
Alex Tomasc9de5602008-01-29 00:19:52 -05002771{
Bobi Jam18aadd42012-02-20 17:53:02 -05002772 struct ext4_free_data *entry = (struct ext4_free_data *)jce;
Alex Tomasc9de5602008-01-29 00:19:52 -05002773 struct ext4_buddy e4b;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002774 struct ext4_group_info *db;
Theodore Ts'od9f34502011-04-30 13:47:24 -04002775 int err, count = 0, count2 = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002776
Bobi Jam18aadd42012-02-20 17:53:02 -05002777 mb_debug(1, "gonna free %u blocks in group %u (0x%p):",
2778 entry->efd_count, entry->efd_group, entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05002779
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002780 if (test_opt(sb, DISCARD)) {
2781 err = ext4_issue_discard(sb, entry->efd_group,
2782 entry->efd_start_cluster,
JP Abgrallb7b01892014-07-23 16:55:07 -07002783 entry->efd_count, 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05002784 if (err && err != -EOPNOTSUPP)
2785 ext4_msg(sb, KERN_WARNING, "discard request in"
2786 " group:%d block:%d count:%d failed"
2787 " with %d", entry->efd_group,
2788 entry->efd_start_cluster,
2789 entry->efd_count, err);
2790 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002791
Bobi Jam18aadd42012-02-20 17:53:02 -05002792 err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
2793 /* we expect to find existing buddy because it's pinned */
2794 BUG_ON(err != 0);
Theodore Ts'ob90f6872010-04-20 16:51:59 -04002795
Alex Tomasc9de5602008-01-29 00:19:52 -05002796
Bobi Jam18aadd42012-02-20 17:53:02 -05002797 db = e4b.bd_info;
2798 /* there are blocks to put in buddy to make them really free */
2799 count += entry->efd_count;
2800 count2++;
2801 ext4_lock_group(sb, entry->efd_group);
2802 /* Take it out of per group rb tree */
2803 rb_erase(&entry->efd_node, &(db->bb_free_root));
2804 mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002805
Bobi Jam18aadd42012-02-20 17:53:02 -05002806 /*
2807 * Clear the trimmed flag for the group so that the next
2808 * ext4_trim_fs can trim it.
2809 * If the volume is mounted with -o discard, online discard
2810 * is supported and the free blocks will be trimmed online.
2811 */
2812 if (!test_opt(sb, DISCARD))
2813 EXT4_MB_GRP_CLEAR_TRIMMED(db);
2814
2815 if (!db->bb_free_root.rb_node) {
2816 /* No more items in the per group rb tree
2817 * balance refcounts from ext4_mb_free_metadata()
Tao Ma3d56b8d2011-07-11 00:03:38 -04002818 */
Bobi Jam18aadd42012-02-20 17:53:02 -05002819 page_cache_release(e4b.bd_buddy_page);
2820 page_cache_release(e4b.bd_bitmap_page);
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04002821 }
Bobi Jam18aadd42012-02-20 17:53:02 -05002822 ext4_unlock_group(sb, entry->efd_group);
2823 kmem_cache_free(ext4_free_data_cachep, entry);
2824 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05002825
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002826 mb_debug(1, "freed %u blocks in %u structures\n", count, count2);
Alex Tomasc9de5602008-01-29 00:19:52 -05002827}
2828
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002829int __init ext4_init_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002830{
Theodore Ts'o16828082010-10-27 21:30:09 -04002831 ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
2832 SLAB_RECLAIM_ACCOUNT);
Alex Tomasc9de5602008-01-29 00:19:52 -05002833 if (ext4_pspace_cachep == NULL)
2834 return -ENOMEM;
2835
Theodore Ts'o16828082010-10-27 21:30:09 -04002836 ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
2837 SLAB_RECLAIM_ACCOUNT);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002838 if (ext4_ac_cachep == NULL) {
2839 kmem_cache_destroy(ext4_pspace_cachep);
2840 return -ENOMEM;
2841 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002842
Bobi Jam18aadd42012-02-20 17:53:02 -05002843 ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
2844 SLAB_RECLAIM_ACCOUNT);
2845 if (ext4_free_data_cachep == NULL) {
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04002846 kmem_cache_destroy(ext4_pspace_cachep);
2847 kmem_cache_destroy(ext4_ac_cachep);
2848 return -ENOMEM;
2849 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002850 return 0;
2851}
2852
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04002853void ext4_exit_mballoc(void)
Alex Tomasc9de5602008-01-29 00:19:52 -05002854{
Theodore Ts'o60e66792010-05-17 07:00:00 -04002855 /*
Jesper Dangaard Brouer3e03f9c2009-07-05 22:29:27 -04002856 * Wait for completion of call_rcu()'s on ext4_pspace_cachep
2857 * before destroying the slab cache.
2858 */
2859 rcu_barrier();
Alex Tomasc9de5602008-01-29 00:19:52 -05002860 kmem_cache_destroy(ext4_pspace_cachep);
Eric Sandeen256bdb42008-02-10 01:13:33 -05002861 kmem_cache_destroy(ext4_ac_cachep);
Bobi Jam18aadd42012-02-20 17:53:02 -05002862 kmem_cache_destroy(ext4_free_data_cachep);
Eric Sandeen2892c152011-02-12 08:12:18 -05002863 ext4_groupinfo_destroy_slabs();
Alex Tomasc9de5602008-01-29 00:19:52 -05002864}
2865
2866
2867/*
Uwe Kleine-König73b2c712010-07-30 21:02:47 +02002868 * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
Alex Tomasc9de5602008-01-29 00:19:52 -05002869 * Returns 0 if success or error code
2870 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04002871static noinline_for_stack int
2872ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002873 handle_t *handle, unsigned int reserv_clstrs)
Alex Tomasc9de5602008-01-29 00:19:52 -05002874{
2875 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05002876 struct ext4_group_desc *gdp;
2877 struct buffer_head *gdp_bh;
2878 struct ext4_sb_info *sbi;
2879 struct super_block *sb;
2880 ext4_fsblk_t block;
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002881 int err, len;
Alex Tomasc9de5602008-01-29 00:19:52 -05002882
2883 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2884 BUG_ON(ac->ac_b_ex.fe_len <= 0);
2885
2886 sb = ac->ac_sb;
2887 sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05002888
2889 err = -EIO;
Theodore Ts'o574ca172008-07-11 19:27:31 -04002890 bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002891 if (!bitmap_bh)
2892 goto out_err;
2893
liang xie5d601252014-05-12 22:06:43 -04002894 BUFFER_TRACE(bitmap_bh, "getting write access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002895 err = ext4_journal_get_write_access(handle, bitmap_bh);
2896 if (err)
2897 goto out_err;
2898
2899 err = -EIO;
2900 gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2901 if (!gdp)
2902 goto out_err;
2903
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002904 ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002905 ext4_free_group_clusters(sb, gdp));
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04002906
liang xie5d601252014-05-12 22:06:43 -04002907 BUFFER_TRACE(gdp_bh, "get_write_access");
Alex Tomasc9de5602008-01-29 00:19:52 -05002908 err = ext4_journal_get_write_access(handle, gdp_bh);
2909 if (err)
2910 goto out_err;
2911
Akinobu Mitabda00de2010-03-03 23:53:25 -05002912 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002913
Theodore Ts'o53accfa2011-09-09 18:48:51 -04002914 len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002915 if (!ext4_data_block_valid(sbi, block, len)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05002916 ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
Theodore Ts'o1084f252012-03-19 23:13:43 -04002917 "fs metadata", block, block+len);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002918 /* File system mounted not to panic on error
2919 * Fix the bitmap and repeat the block allocation
2920 * We leak some of the blocks here.
2921 */
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002922 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002923 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2924 ac->ac_b_ex.fe_len);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002925 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Frank Mayhar03901312009-01-07 00:06:22 -05002926 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04002927 if (!err)
2928 err = -EAGAIN;
2929 goto out_err;
Alex Tomasc9de5602008-01-29 00:19:52 -05002930 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002931
2932 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05002933#ifdef AGGRESSIVE_CHECK
2934 {
2935 int i;
2936 for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
2937 BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
2938 bitmap_bh->b_data));
2939 }
2940 }
2941#endif
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04002942 ext4_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
2943 ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05002944 if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
2945 gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002946 ext4_free_group_clusters_set(sb, gdp,
Theodore Ts'ocff1dfd72011-09-09 19:12:51 -04002947 ext4_free_clusters_after_init(sb,
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002948 ac->ac_b_ex.fe_group, gdp));
Alex Tomasc9de5602008-01-29 00:19:52 -05002949 }
Theodore Ts'o021b65b2011-09-09 19:08:51 -04002950 len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
2951 ext4_free_group_clusters_set(sb, gdp, len);
Tao Ma79f1ba42012-10-22 00:34:32 -04002952 ext4_block_bitmap_csum_set(sb, ac->ac_b_ex.fe_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002953 ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002954
2955 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04002956 percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
Mingming Caod2a17632008-07-14 17:52:37 -04002957 /*
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002958 * Now reduce the dirty block count also. Should not go negative
Mingming Caod2a17632008-07-14 17:52:37 -04002959 */
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002960 if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
2961 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04002962 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
2963 reserv_clstrs);
Alex Tomasc9de5602008-01-29 00:19:52 -05002964
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002965 if (sbi->s_log_groups_per_flex) {
2966 ext4_group_t flex_group = ext4_flex_group(sbi,
2967 ac->ac_b_ex.fe_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04002968 atomic64_sub(ac->ac_b_ex.fe_len,
2969 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002970 }
2971
Frank Mayhar03901312009-01-07 00:06:22 -05002972 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002973 if (err)
2974 goto out_err;
Frank Mayhar03901312009-01-07 00:06:22 -05002975 err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002976
2977out_err:
Aneesh Kumar K.V42a10ad2008-02-10 01:07:28 -05002978 brelse(bitmap_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05002979 return err;
2980}
2981
2982/*
2983 * here we normalize request for locality group
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002984 * Group request are normalized to s_mb_group_prealloc, which goes to
2985 * s_strip if we set the same via mount option.
2986 * s_mb_group_prealloc can be configured via
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04002987 * /sys/fs/ext4/<partition>/mb_group_prealloc
Alex Tomasc9de5602008-01-29 00:19:52 -05002988 *
2989 * XXX: should we try to preallocate more than the group has now?
2990 */
2991static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
2992{
2993 struct super_block *sb = ac->ac_sb;
2994 struct ext4_locality_group *lg = ac->ac_lg;
2995
2996 BUG_ON(lg == NULL);
Dan Ehrenbergd7a1fee2011-07-17 21:11:30 -04002997 ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04002998 mb_debug(1, "#%u: goal %u blocks for locality group\n",
Alex Tomasc9de5602008-01-29 00:19:52 -05002999 current->pid, ac->ac_g_ex.fe_len);
3000}
3001
3002/*
3003 * Normalization means making request better in terms of
3004 * size and alignment
3005 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003006static noinline_for_stack void
3007ext4_mb_normalize_request(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05003008 struct ext4_allocation_request *ar)
3009{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003010 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003011 int bsbits, max;
3012 ext4_lblk_t end;
Curt Wohlgemuth1592d2c2012-02-20 17:53:03 -05003013 loff_t size, start_off;
3014 loff_t orig_size __maybe_unused;
Andi Kleen5a0790c2010-06-14 13:28:03 -04003015 ext4_lblk_t start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003016 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003017 struct ext4_prealloc_space *pa;
Alex Tomasc9de5602008-01-29 00:19:52 -05003018
3019 /* do normalize only data requests, metadata requests
3020 do not need preallocation */
3021 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3022 return;
3023
3024 /* sometime caller may want exact blocks */
3025 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3026 return;
3027
3028 /* caller may indicate that preallocation isn't
3029 * required (it's a tail, for example) */
3030 if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3031 return;
3032
3033 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3034 ext4_mb_normalize_group_request(ac);
3035 return ;
3036 }
3037
3038 bsbits = ac->ac_sb->s_blocksize_bits;
3039
3040 /* first, let's learn actual file size
3041 * given current request is allocated */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003042 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003043 size = size << bsbits;
3044 if (size < i_size_read(ac->ac_inode))
3045 size = i_size_read(ac->ac_inode);
Andi Kleen5a0790c2010-06-14 13:28:03 -04003046 orig_size = size;
Alex Tomasc9de5602008-01-29 00:19:52 -05003047
Valerie Clement19304792008-05-13 19:31:14 -04003048 /* max size of free chunks */
3049 max = 2 << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003050
Valerie Clement19304792008-05-13 19:31:14 -04003051#define NRL_CHECK_SIZE(req, size, max, chunk_size) \
3052 (req <= (size) || max <= (chunk_size))
Alex Tomasc9de5602008-01-29 00:19:52 -05003053
3054 /* first, try to predict filesize */
3055 /* XXX: should this table be tunable? */
3056 start_off = 0;
3057 if (size <= 16 * 1024) {
3058 size = 16 * 1024;
3059 } else if (size <= 32 * 1024) {
3060 size = 32 * 1024;
3061 } else if (size <= 64 * 1024) {
3062 size = 64 * 1024;
3063 } else if (size <= 128 * 1024) {
3064 size = 128 * 1024;
3065 } else if (size <= 256 * 1024) {
3066 size = 256 * 1024;
3067 } else if (size <= 512 * 1024) {
3068 size = 512 * 1024;
3069 } else if (size <= 1024 * 1024) {
3070 size = 1024 * 1024;
Valerie Clement19304792008-05-13 19:31:14 -04003071 } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003072 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
Valerie Clement19304792008-05-13 19:31:14 -04003073 (21 - bsbits)) << 21;
3074 size = 2 * 1024 * 1024;
3075 } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003076 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3077 (22 - bsbits)) << 22;
3078 size = 4 * 1024 * 1024;
3079 } else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
Valerie Clement19304792008-05-13 19:31:14 -04003080 (8<<20)>>bsbits, max, 8 * 1024)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003081 start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3082 (23 - bsbits)) << 23;
3083 size = 8 * 1024 * 1024;
3084 } else {
Xiaoguang Wangb27b1532014-07-27 22:26:36 -04003085 start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
3086 size = (loff_t) EXT4_C2B(EXT4_SB(ac->ac_sb),
3087 ac->ac_o_ex.fe_len) << bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003088 }
Andi Kleen5a0790c2010-06-14 13:28:03 -04003089 size = size >> bsbits;
3090 start = start_off >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05003091
3092 /* don't cover already allocated blocks in selected range */
3093 if (ar->pleft && start <= ar->lleft) {
3094 size -= ar->lleft + 1 - start;
3095 start = ar->lleft + 1;
3096 }
3097 if (ar->pright && start + size - 1 >= ar->lright)
3098 size -= start + size - ar->lright;
3099
3100 end = start + size;
3101
3102 /* check we don't cross already preallocated blocks */
3103 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003104 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003105 ext4_lblk_t pa_end;
Alex Tomasc9de5602008-01-29 00:19:52 -05003106
Alex Tomasc9de5602008-01-29 00:19:52 -05003107 if (pa->pa_deleted)
3108 continue;
3109 spin_lock(&pa->pa_lock);
3110 if (pa->pa_deleted) {
3111 spin_unlock(&pa->pa_lock);
3112 continue;
3113 }
3114
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003115 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3116 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003117
3118 /* PA must not overlap original request */
3119 BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3120 ac->ac_o_ex.fe_logical < pa->pa_lstart));
3121
Eric Sandeen38877f42009-08-17 23:55:24 -04003122 /* skip PAs this normalized request doesn't overlap with */
3123 if (pa->pa_lstart >= end || pa_end <= start) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003124 spin_unlock(&pa->pa_lock);
3125 continue;
3126 }
3127 BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3128
Eric Sandeen38877f42009-08-17 23:55:24 -04003129 /* adjust start or end to be adjacent to this pa */
Alex Tomasc9de5602008-01-29 00:19:52 -05003130 if (pa_end <= ac->ac_o_ex.fe_logical) {
3131 BUG_ON(pa_end < start);
3132 start = pa_end;
Eric Sandeen38877f42009-08-17 23:55:24 -04003133 } else if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003134 BUG_ON(pa->pa_lstart > end);
3135 end = pa->pa_lstart;
3136 }
3137 spin_unlock(&pa->pa_lock);
3138 }
3139 rcu_read_unlock();
3140 size = end - start;
3141
3142 /* XXX: extra loop to check we really don't overlap preallocations */
3143 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003144 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003145 ext4_lblk_t pa_end;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003146
Alex Tomasc9de5602008-01-29 00:19:52 -05003147 spin_lock(&pa->pa_lock);
3148 if (pa->pa_deleted == 0) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003149 pa_end = pa->pa_lstart + EXT4_C2B(EXT4_SB(ac->ac_sb),
3150 pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003151 BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3152 }
3153 spin_unlock(&pa->pa_lock);
3154 }
3155 rcu_read_unlock();
3156
3157 if (start + size <= ac->ac_o_ex.fe_logical &&
3158 start > ac->ac_o_ex.fe_logical) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003159 ext4_msg(ac->ac_sb, KERN_ERR,
3160 "start %lu, size %lu, fe_logical %lu",
3161 (unsigned long) start, (unsigned long) size,
3162 (unsigned long) ac->ac_o_ex.fe_logical);
Dmitry Monakhovdfe076c2014-10-01 22:26:17 -04003163 BUG();
Alex Tomasc9de5602008-01-29 00:19:52 -05003164 }
Maurizio Lombardib5b60772014-05-27 12:48:56 -04003165 BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
Alex Tomasc9de5602008-01-29 00:19:52 -05003166
3167 /* now prepare goal request */
3168
3169 /* XXX: is it better to align blocks WRT to logical
3170 * placement or satisfy big request as is */
3171 ac->ac_g_ex.fe_logical = start;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003172 ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
Alex Tomasc9de5602008-01-29 00:19:52 -05003173
3174 /* define goal start in order to merge */
3175 if (ar->pright && (ar->lright == (start + size))) {
3176 /* merge to the right */
3177 ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3178 &ac->ac_f_ex.fe_group,
3179 &ac->ac_f_ex.fe_start);
3180 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3181 }
3182 if (ar->pleft && (ar->lleft + 1 == start)) {
3183 /* merge to the left */
3184 ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3185 &ac->ac_f_ex.fe_group,
3186 &ac->ac_f_ex.fe_start);
3187 ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3188 }
3189
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003190 mb_debug(1, "goal: %u(was %u) blocks at %u\n", (unsigned) size,
Alex Tomasc9de5602008-01-29 00:19:52 -05003191 (unsigned) orig_size, (unsigned) start);
3192}
3193
3194static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3195{
3196 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3197
3198 if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3199 atomic_inc(&sbi->s_bal_reqs);
3200 atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
Curt Wohlgemuth291dae42010-05-16 16:00:00 -04003201 if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
Alex Tomasc9de5602008-01-29 00:19:52 -05003202 atomic_inc(&sbi->s_bal_success);
3203 atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3204 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3205 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3206 atomic_inc(&sbi->s_bal_goals);
3207 if (ac->ac_found > sbi->s_mb_max_to_scan)
3208 atomic_inc(&sbi->s_bal_breaks);
3209 }
3210
Theodore Ts'o296c3552009-09-30 00:32:42 -04003211 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
3212 trace_ext4_mballoc_alloc(ac);
3213 else
3214 trace_ext4_mballoc_prealloc(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05003215}
3216
3217/*
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003218 * Called on failure; free up any blocks from the inode PA for this
3219 * context. We don't need this for MB_GROUP_PA because we only change
3220 * pa_free in ext4_mb_release_context(), but on failure, we've already
3221 * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
3222 */
3223static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
3224{
3225 struct ext4_prealloc_space *pa = ac->ac_pa;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003226 struct ext4_buddy e4b;
3227 int err;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003228
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003229 if (pa == NULL) {
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003230 if (ac->ac_f_ex.fe_len == 0)
3231 return;
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003232 err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
3233 if (err) {
3234 /*
3235 * This should never happen since we pin the
3236 * pages in the ext4_allocation_context so
3237 * ext4_mb_load_buddy() should never fail.
3238 */
3239 WARN(1, "mb_load_buddy failed (%d)", err);
3240 return;
3241 }
3242 ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
3243 mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
3244 ac->ac_f_ex.fe_len);
3245 ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
Theodore Ts'oc99d1e62014-08-23 17:47:28 -04003246 ext4_mb_unload_buddy(&e4b);
Theodore Ts'o86f0afd2014-07-30 22:17:17 -04003247 return;
3248 }
3249 if (pa->pa_type == MB_INODE_PA)
Zheng Liu400db9d2012-05-28 17:53:53 -04003250 pa->pa_free += ac->ac_b_ex.fe_len;
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05003251}
3252
3253/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003254 * use blocks preallocated to inode
3255 */
3256static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3257 struct ext4_prealloc_space *pa)
3258{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003259 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003260 ext4_fsblk_t start;
3261 ext4_fsblk_t end;
3262 int len;
3263
3264 /* found preallocated blocks, use them */
3265 start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003266 end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
3267 start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
3268 len = EXT4_NUM_B2C(sbi, end - start);
Alex Tomasc9de5602008-01-29 00:19:52 -05003269 ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3270 &ac->ac_b_ex.fe_start);
3271 ac->ac_b_ex.fe_len = len;
3272 ac->ac_status = AC_STATUS_FOUND;
3273 ac->ac_pa = pa;
3274
3275 BUG_ON(start < pa->pa_pstart);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003276 BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
Alex Tomasc9de5602008-01-29 00:19:52 -05003277 BUG_ON(pa->pa_free < len);
3278 pa->pa_free -= len;
3279
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003280 mb_debug(1, "use %llu/%u from inode pa %p\n", start, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003281}
3282
3283/*
3284 * use blocks preallocated to locality group
3285 */
3286static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3287 struct ext4_prealloc_space *pa)
3288{
Aneesh Kumar K.V03cddb82008-06-05 20:59:29 -04003289 unsigned int len = ac->ac_o_ex.fe_len;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003290
Alex Tomasc9de5602008-01-29 00:19:52 -05003291 ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3292 &ac->ac_b_ex.fe_group,
3293 &ac->ac_b_ex.fe_start);
3294 ac->ac_b_ex.fe_len = len;
3295 ac->ac_status = AC_STATUS_FOUND;
3296 ac->ac_pa = pa;
3297
3298 /* we don't correct pa_pstart or pa_plen here to avoid
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003299 * possible race when the group is being loaded concurrently
Alex Tomasc9de5602008-01-29 00:19:52 -05003300 * instead we correct pa later, after blocks are marked
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05003301 * in on-disk bitmap -- see ext4_mb_release_context()
3302 * Other CPUs are prevented from allocating from this pa by lg_mutex
Alex Tomasc9de5602008-01-29 00:19:52 -05003303 */
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003304 mb_debug(1, "use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003305}
3306
3307/*
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003308 * Return the prealloc space that have minimal distance
3309 * from the goal block. @cpa is the prealloc
3310 * space that is having currently known minimal distance
3311 * from the goal block.
3312 */
3313static struct ext4_prealloc_space *
3314ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
3315 struct ext4_prealloc_space *pa,
3316 struct ext4_prealloc_space *cpa)
3317{
3318 ext4_fsblk_t cur_distance, new_distance;
3319
3320 if (cpa == NULL) {
3321 atomic_inc(&pa->pa_count);
3322 return pa;
3323 }
3324 cur_distance = abs(goal_block - cpa->pa_pstart);
3325 new_distance = abs(goal_block - pa->pa_pstart);
3326
Coly Li5a54b2f2011-02-24 14:10:05 -05003327 if (cur_distance <= new_distance)
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003328 return cpa;
3329
3330 /* drop the previous reference */
3331 atomic_dec(&cpa->pa_count);
3332 atomic_inc(&pa->pa_count);
3333 return pa;
3334}
3335
3336/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003337 * search goal blocks in preallocated space
3338 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003339static noinline_for_stack int
3340ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003341{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003342 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003343 int order, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05003344 struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3345 struct ext4_locality_group *lg;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003346 struct ext4_prealloc_space *pa, *cpa = NULL;
3347 ext4_fsblk_t goal_block;
Alex Tomasc9de5602008-01-29 00:19:52 -05003348
3349 /* only data can be preallocated */
3350 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3351 return 0;
3352
3353 /* first, try per-file preallocation */
3354 rcu_read_lock();
Aneesh Kumar K.V9a0762c2008-04-17 10:38:59 -04003355 list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003356
3357 /* all fields in this condition don't change,
3358 * so we can skip locking for them */
3359 if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003360 ac->ac_o_ex.fe_logical >= (pa->pa_lstart +
3361 EXT4_C2B(sbi, pa->pa_len)))
Alex Tomasc9de5602008-01-29 00:19:52 -05003362 continue;
3363
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003364 /* non-extent files can't have physical blocks past 2^32 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003365 if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003366 (pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len) >
3367 EXT4_MAX_BLOCK_FILE_PHYS))
Eric Sandeenfb0a3872009-09-16 14:45:10 -04003368 continue;
3369
Alex Tomasc9de5602008-01-29 00:19:52 -05003370 /* found preallocated blocks, use them */
3371 spin_lock(&pa->pa_lock);
3372 if (pa->pa_deleted == 0 && pa->pa_free) {
3373 atomic_inc(&pa->pa_count);
3374 ext4_mb_use_inode_pa(ac, pa);
3375 spin_unlock(&pa->pa_lock);
3376 ac->ac_criteria = 10;
3377 rcu_read_unlock();
3378 return 1;
3379 }
3380 spin_unlock(&pa->pa_lock);
3381 }
3382 rcu_read_unlock();
3383
3384 /* can we use group allocation? */
3385 if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3386 return 0;
3387
3388 /* inode may have no locality group for some reason */
3389 lg = ac->ac_lg;
3390 if (lg == NULL)
3391 return 0;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003392 order = fls(ac->ac_o_ex.fe_len) - 1;
3393 if (order > PREALLOC_TB_SIZE - 1)
3394 /* The max size of hash table is PREALLOC_TB_SIZE */
3395 order = PREALLOC_TB_SIZE - 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003396
Akinobu Mitabda00de2010-03-03 23:53:25 -05003397 goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003398 /*
3399 * search for the prealloc space that is having
3400 * minimal distance from the goal block.
3401 */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003402 for (i = order; i < PREALLOC_TB_SIZE; i++) {
3403 rcu_read_lock();
3404 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
3405 pa_inode_list) {
3406 spin_lock(&pa->pa_lock);
3407 if (pa->pa_deleted == 0 &&
3408 pa->pa_free >= ac->ac_o_ex.fe_len) {
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003409
3410 cpa = ext4_mb_check_group_pa(goal_block,
3411 pa, cpa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003412 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003413 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003414 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003415 rcu_read_unlock();
Alex Tomasc9de5602008-01-29 00:19:52 -05003416 }
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04003417 if (cpa) {
3418 ext4_mb_use_group_pa(ac, cpa);
3419 ac->ac_criteria = 20;
3420 return 1;
3421 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003422 return 0;
3423}
3424
3425/*
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003426 * the function goes through all block freed in the group
3427 * but not yet committed and marks them used in in-core bitmap.
3428 * buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003429 * Need to be called with the ext4 group lock held
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003430 */
3431static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3432 ext4_group_t group)
3433{
3434 struct rb_node *n;
3435 struct ext4_group_info *grp;
3436 struct ext4_free_data *entry;
3437
3438 grp = ext4_get_group_info(sb, group);
3439 n = rb_first(&(grp->bb_free_root));
3440
3441 while (n) {
Bobi Jam18aadd42012-02-20 17:53:02 -05003442 entry = rb_entry(n, struct ext4_free_data, efd_node);
3443 ext4_set_bits(bitmap, entry->efd_start_cluster, entry->efd_count);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05003444 n = rb_next(n);
3445 }
3446 return;
3447}
3448
3449/*
Alex Tomasc9de5602008-01-29 00:19:52 -05003450 * the function goes through all preallocation in this group and marks them
3451 * used in in-core bitmap. buddy must be generated from this bitmap
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04003452 * Need to be called with ext4 group lock held
Alex Tomasc9de5602008-01-29 00:19:52 -05003453 */
Eric Sandeen089ceec2009-07-05 22:17:31 -04003454static noinline_for_stack
3455void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
Alex Tomasc9de5602008-01-29 00:19:52 -05003456 ext4_group_t group)
3457{
3458 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3459 struct ext4_prealloc_space *pa;
3460 struct list_head *cur;
3461 ext4_group_t groupnr;
3462 ext4_grpblk_t start;
3463 int preallocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003464 int len;
3465
3466 /* all form of preallocation discards first load group,
3467 * so the only competing code is preallocation use.
3468 * we don't need any locking here
3469 * notice we do NOT ignore preallocations with pa_deleted
3470 * otherwise we could leave used blocks available for
3471 * allocation in buddy when concurrent ext4_mb_put_pa()
3472 * is dropping preallocation
3473 */
3474 list_for_each(cur, &grp->bb_prealloc_list) {
3475 pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3476 spin_lock(&pa->pa_lock);
3477 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3478 &groupnr, &start);
3479 len = pa->pa_len;
3480 spin_unlock(&pa->pa_lock);
3481 if (unlikely(len == 0))
3482 continue;
3483 BUG_ON(groupnr != group);
Yongqiang Yangc3e94d12011-07-26 22:05:53 -04003484 ext4_set_bits(bitmap, start, len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003485 preallocated += len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003486 }
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003487 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003488}
3489
3490static void ext4_mb_pa_callback(struct rcu_head *head)
3491{
3492 struct ext4_prealloc_space *pa;
3493 pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003494
3495 BUG_ON(atomic_read(&pa->pa_count));
3496 BUG_ON(pa->pa_deleted == 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05003497 kmem_cache_free(ext4_pspace_cachep, pa);
3498}
3499
3500/*
3501 * drops a reference to preallocated space descriptor
3502 * if this was the last reference and the space is consumed
3503 */
3504static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3505 struct super_block *sb, struct ext4_prealloc_space *pa)
3506{
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003507 ext4_group_t grp;
Eric Sandeend33a1972009-03-16 23:25:40 -04003508 ext4_fsblk_t grp_blk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003509
Alex Tomasc9de5602008-01-29 00:19:52 -05003510 /* in this short window concurrent discard can set pa_deleted */
3511 spin_lock(&pa->pa_lock);
Junho Ryu4e8d2132013-12-03 18:10:28 -05003512 if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
3513 spin_unlock(&pa->pa_lock);
3514 return;
3515 }
3516
Alex Tomasc9de5602008-01-29 00:19:52 -05003517 if (pa->pa_deleted == 1) {
3518 spin_unlock(&pa->pa_lock);
3519 return;
3520 }
3521
3522 pa->pa_deleted = 1;
3523 spin_unlock(&pa->pa_lock);
3524
Eric Sandeend33a1972009-03-16 23:25:40 -04003525 grp_blk = pa->pa_pstart;
Theodore Ts'o60e66792010-05-17 07:00:00 -04003526 /*
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003527 * If doing group-based preallocation, pa_pstart may be in the
3528 * next group when pa is used up
3529 */
3530 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeend33a1972009-03-16 23:25:40 -04003531 grp_blk--;
3532
Lukas Czernerbd862982013-04-03 23:32:34 -04003533 grp = ext4_get_group_number(sb, grp_blk);
Alex Tomasc9de5602008-01-29 00:19:52 -05003534
3535 /*
3536 * possible race:
3537 *
3538 * P1 (buddy init) P2 (regular allocation)
3539 * find block B in PA
3540 * copy on-disk bitmap to buddy
3541 * mark B in on-disk bitmap
3542 * drop PA from group
3543 * mark all PAs in buddy
3544 *
3545 * thus, P1 initializes buddy with B available. to prevent this
3546 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3547 * against that pair
3548 */
3549 ext4_lock_group(sb, grp);
3550 list_del(&pa->pa_group_list);
3551 ext4_unlock_group(sb, grp);
3552
3553 spin_lock(pa->pa_obj_lock);
3554 list_del_rcu(&pa->pa_inode_list);
3555 spin_unlock(pa->pa_obj_lock);
3556
3557 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3558}
3559
3560/*
3561 * creates new preallocated space for given inode
3562 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003563static noinline_for_stack int
3564ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003565{
3566 struct super_block *sb = ac->ac_sb;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003567 struct ext4_sb_info *sbi = EXT4_SB(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 struct ext4_prealloc_space *pa;
3569 struct ext4_group_info *grp;
3570 struct ext4_inode_info *ei;
3571
3572 /* preallocate only when found space is larger then requested */
3573 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3574 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3575 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3576
3577 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3578 if (pa == NULL)
3579 return -ENOMEM;
3580
3581 if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3582 int winl;
3583 int wins;
3584 int win;
3585 int offs;
3586
3587 /* we can't allocate as much as normalizer wants.
3588 * so, found space must get proper lstart
3589 * to cover original request */
3590 BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3591 BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3592
3593 /* we're limited by original request in that
3594 * logical block must be covered any way
3595 * winl is window we can move our chunk within */
3596 winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3597
3598 /* also, we should cover whole original request */
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003599 wins = EXT4_C2B(sbi, ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003600
3601 /* the smallest one defines real window */
3602 win = min(winl, wins);
3603
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003604 offs = ac->ac_o_ex.fe_logical %
3605 EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003606 if (offs && offs < win)
3607 win = offs;
3608
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003609 ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical -
Lukas Czerner810da242013-03-02 17:18:58 -05003610 EXT4_NUM_B2C(sbi, win);
Alex Tomasc9de5602008-01-29 00:19:52 -05003611 BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3612 BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3613 }
3614
3615 /* preallocation can change ac_b_ex, thus we store actually
3616 * allocated blocks for history */
3617 ac->ac_f_ex = ac->ac_b_ex;
3618
3619 pa->pa_lstart = ac->ac_b_ex.fe_logical;
3620 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3621 pa->pa_len = ac->ac_b_ex.fe_len;
3622 pa->pa_free = pa->pa_len;
3623 atomic_set(&pa->pa_count, 1);
3624 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003625 INIT_LIST_HEAD(&pa->pa_inode_list);
3626 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003627 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003628 pa->pa_type = MB_INODE_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003629
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003630 mb_debug(1, "new inode pa %p: %llu/%u for %u\n", pa,
Alex Tomasc9de5602008-01-29 00:19:52 -05003631 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003632 trace_ext4_mb_new_inode_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003633
3634 ext4_mb_use_inode_pa(ac, pa);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003635 atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
Alex Tomasc9de5602008-01-29 00:19:52 -05003636
3637 ei = EXT4_I(ac->ac_inode);
3638 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3639
3640 pa->pa_obj_lock = &ei->i_prealloc_lock;
3641 pa->pa_inode = ac->ac_inode;
3642
3643 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3644 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3645 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3646
3647 spin_lock(pa->pa_obj_lock);
3648 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3649 spin_unlock(pa->pa_obj_lock);
3650
3651 return 0;
3652}
3653
3654/*
3655 * creates new preallocated space for locality group inodes belongs to
3656 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003657static noinline_for_stack int
3658ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
Alex Tomasc9de5602008-01-29 00:19:52 -05003659{
3660 struct super_block *sb = ac->ac_sb;
3661 struct ext4_locality_group *lg;
3662 struct ext4_prealloc_space *pa;
3663 struct ext4_group_info *grp;
3664
3665 /* preallocate only when found space is larger then requested */
3666 BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3667 BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3668 BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3669
3670 BUG_ON(ext4_pspace_cachep == NULL);
3671 pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3672 if (pa == NULL)
3673 return -ENOMEM;
3674
3675 /* preallocation can change ac_b_ex, thus we store actually
3676 * allocated blocks for history */
3677 ac->ac_f_ex = ac->ac_b_ex;
3678
3679 pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3680 pa->pa_lstart = pa->pa_pstart;
3681 pa->pa_len = ac->ac_b_ex.fe_len;
3682 pa->pa_free = pa->pa_len;
3683 atomic_set(&pa->pa_count, 1);
3684 spin_lock_init(&pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003685 INIT_LIST_HEAD(&pa->pa_inode_list);
Aneesh Kumar K.Vd794bf82009-02-14 10:31:16 -05003686 INIT_LIST_HEAD(&pa->pa_group_list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003687 pa->pa_deleted = 0;
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003688 pa->pa_type = MB_GROUP_PA;
Alex Tomasc9de5602008-01-29 00:19:52 -05003689
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003690 mb_debug(1, "new group pa %p: %llu/%u for %u\n", pa,
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003691 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3692 trace_ext4_mb_new_group_pa(ac, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003693
3694 ext4_mb_use_group_pa(ac, pa);
3695 atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3696
3697 grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3698 lg = ac->ac_lg;
3699 BUG_ON(lg == NULL);
3700
3701 pa->pa_obj_lock = &lg->lg_prealloc_lock;
3702 pa->pa_inode = NULL;
3703
3704 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3705 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3706 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3707
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04003708 /*
3709 * We will later add the new pa to the right bucket
3710 * after updating the pa_free in ext4_mb_release_context
3711 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003712 return 0;
3713}
3714
3715static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3716{
3717 int err;
3718
3719 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3720 err = ext4_mb_new_group_pa(ac);
3721 else
3722 err = ext4_mb_new_inode_pa(ac);
3723 return err;
3724}
3725
3726/*
3727 * finds all unused blocks in on-disk bitmap, frees them in
3728 * in-core bitmap and buddy.
3729 * @pa must be unlinked from inode and group lists, so that
3730 * nobody else can find/use it.
3731 * the caller MUST hold group/inode locks.
3732 * TODO: optimize the case when there are no in-core structures yet
3733 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003734static noinline_for_stack int
3735ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003736 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003737{
Alex Tomasc9de5602008-01-29 00:19:52 -05003738 struct super_block *sb = e4b->bd_sb;
3739 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003740 unsigned int end;
3741 unsigned int next;
Alex Tomasc9de5602008-01-29 00:19:52 -05003742 ext4_group_t group;
3743 ext4_grpblk_t bit;
Theodore Ts'oba80b102009-01-03 20:03:21 -05003744 unsigned long long grp_blk_start;
Alex Tomasc9de5602008-01-29 00:19:52 -05003745 int err = 0;
3746 int free = 0;
3747
3748 BUG_ON(pa->pa_deleted == 0);
3749 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003750 grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003751 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3752 end = bit + pa->pa_len;
3753
Alex Tomasc9de5602008-01-29 00:19:52 -05003754 while (bit < end) {
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003755 bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003756 if (bit >= end)
3757 break;
Aneesh Kumar K.Vffad0a42008-02-23 01:38:34 -05003758 next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003759 mb_debug(1, " free preallocated %u/%u in group %u\n",
Andi Kleen5a0790c2010-06-14 13:28:03 -04003760 (unsigned) ext4_group_first_block_no(sb, group) + bit,
3761 (unsigned) next - bit, (unsigned) group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003762 free += next - bit;
3763
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003764 trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04003765 trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
3766 EXT4_C2B(sbi, bit)),
Lukas Czernera9c667f2011-06-06 09:51:52 -04003767 next - bit);
Alex Tomasc9de5602008-01-29 00:19:52 -05003768 mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3769 bit = next + 1;
3770 }
3771 if (free != pa->pa_free) {
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003772 ext4_msg(e4b->bd_sb, KERN_CRIT,
3773 "pa %p: logic %lu, phys. %lu, len %lu",
3774 pa, (unsigned long) pa->pa_lstart,
3775 (unsigned long) pa->pa_pstart,
3776 (unsigned long) pa->pa_len);
Theodore Ts'oe29136f2010-06-29 12:54:28 -04003777 ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003778 free, pa->pa_free);
Aneesh Kumar K.Ve56eb652008-02-15 13:48:21 -05003779 /*
3780 * pa is already deleted so we use the value obtained
3781 * from the bitmap and continue.
3782 */
Alex Tomasc9de5602008-01-29 00:19:52 -05003783 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003784 atomic_add(free, &sbi->s_mb_discarded);
3785
3786 return err;
3787}
3788
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003789static noinline_for_stack int
3790ext4_mb_release_group_pa(struct ext4_buddy *e4b,
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003791 struct ext4_prealloc_space *pa)
Alex Tomasc9de5602008-01-29 00:19:52 -05003792{
Alex Tomasc9de5602008-01-29 00:19:52 -05003793 struct super_block *sb = e4b->bd_sb;
3794 ext4_group_t group;
3795 ext4_grpblk_t bit;
3796
Yongqiang Yang60e07cf2011-12-18 15:49:54 -05003797 trace_ext4_mb_release_group_pa(sb, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003798 BUG_ON(pa->pa_deleted == 0);
3799 ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3800 BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3801 mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3802 atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003803 trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05003804
3805 return 0;
3806}
3807
3808/*
3809 * releases all preallocations in given group
3810 *
3811 * first, we need to decide discard policy:
3812 * - when do we discard
3813 * 1) ENOSPC
3814 * - how many do we discard
3815 * 1) how many requested
3816 */
Eric Sandeen4ddfef72008-04-29 08:11:12 -04003817static noinline_for_stack int
3818ext4_mb_discard_group_preallocations(struct super_block *sb,
Alex Tomasc9de5602008-01-29 00:19:52 -05003819 ext4_group_t group, int needed)
3820{
3821 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3822 struct buffer_head *bitmap_bh = NULL;
3823 struct ext4_prealloc_space *pa, *tmp;
3824 struct list_head list;
3825 struct ext4_buddy e4b;
3826 int err;
3827 int busy = 0;
3828 int free = 0;
3829
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003830 mb_debug(1, "discard preallocation for group %u\n", group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003831
3832 if (list_empty(&grp->bb_prealloc_list))
3833 return 0;
3834
Theodore Ts'o574ca172008-07-11 19:27:31 -04003835 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05003836 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003837 ext4_error(sb, "Error reading block bitmap for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003838 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003839 }
3840
3841 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003842 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003843 ext4_error(sb, "Error loading buddy information for %u", group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04003844 put_bh(bitmap_bh);
3845 return 0;
3846 }
Alex Tomasc9de5602008-01-29 00:19:52 -05003847
3848 if (needed == 0)
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04003849 needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
Alex Tomasc9de5602008-01-29 00:19:52 -05003850
Alex Tomasc9de5602008-01-29 00:19:52 -05003851 INIT_LIST_HEAD(&list);
Alex Tomasc9de5602008-01-29 00:19:52 -05003852repeat:
3853 ext4_lock_group(sb, group);
3854 list_for_each_entry_safe(pa, tmp,
3855 &grp->bb_prealloc_list, pa_group_list) {
3856 spin_lock(&pa->pa_lock);
3857 if (atomic_read(&pa->pa_count)) {
3858 spin_unlock(&pa->pa_lock);
3859 busy = 1;
3860 continue;
3861 }
3862 if (pa->pa_deleted) {
3863 spin_unlock(&pa->pa_lock);
3864 continue;
3865 }
3866
3867 /* seems this one can be freed ... */
3868 pa->pa_deleted = 1;
3869
3870 /* we can trust pa_free ... */
3871 free += pa->pa_free;
3872
3873 spin_unlock(&pa->pa_lock);
3874
3875 list_del(&pa->pa_group_list);
3876 list_add(&pa->u.pa_tmp_list, &list);
3877 }
3878
3879 /* if we still need more blocks and some PAs were used, try again */
3880 if (free < needed && busy) {
3881 busy = 0;
3882 ext4_unlock_group(sb, group);
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04003883 cond_resched();
Alex Tomasc9de5602008-01-29 00:19:52 -05003884 goto repeat;
3885 }
3886
3887 /* found anything to free? */
3888 if (list_empty(&list)) {
3889 BUG_ON(free != 0);
3890 goto out;
3891 }
3892
3893 /* now free all selected PAs */
3894 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3895
3896 /* remove from object (inode or locality group) */
3897 spin_lock(pa->pa_obj_lock);
3898 list_del_rcu(&pa->pa_inode_list);
3899 spin_unlock(pa->pa_obj_lock);
3900
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003901 if (pa->pa_type == MB_GROUP_PA)
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003902 ext4_mb_release_group_pa(&e4b, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003903 else
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04003904 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05003905
3906 list_del(&pa->u.pa_tmp_list);
3907 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3908 }
3909
3910out:
3911 ext4_unlock_group(sb, group);
Jing Zhange39e07f2010-05-14 00:00:00 -04003912 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05003913 put_bh(bitmap_bh);
3914 return free;
3915}
3916
3917/*
3918 * releases all non-used preallocated blocks for given inode
3919 *
3920 * It's important to discard preallocations under i_data_sem
3921 * We don't want another block to be served from the prealloc
3922 * space when we are discarding the inode prealloc space.
3923 *
3924 * FIXME!! Make sure it is valid at all the call sites
3925 */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003926void ext4_discard_preallocations(struct inode *inode)
Alex Tomasc9de5602008-01-29 00:19:52 -05003927{
3928 struct ext4_inode_info *ei = EXT4_I(inode);
3929 struct super_block *sb = inode->i_sb;
3930 struct buffer_head *bitmap_bh = NULL;
3931 struct ext4_prealloc_space *pa, *tmp;
3932 ext4_group_t group = 0;
3933 struct list_head list;
3934 struct ext4_buddy e4b;
3935 int err;
3936
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003937 if (!S_ISREG(inode->i_mode)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05003938 /*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
3939 return;
3940 }
3941
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04003942 mb_debug(1, "discard preallocation for inode %lu\n", inode->i_ino);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003943 trace_ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003944
3945 INIT_LIST_HEAD(&list);
3946
3947repeat:
3948 /* first, collect all pa's in the inode */
3949 spin_lock(&ei->i_prealloc_lock);
3950 while (!list_empty(&ei->i_prealloc_list)) {
3951 pa = list_entry(ei->i_prealloc_list.next,
3952 struct ext4_prealloc_space, pa_inode_list);
3953 BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
3954 spin_lock(&pa->pa_lock);
3955 if (atomic_read(&pa->pa_count)) {
3956 /* this shouldn't happen often - nobody should
3957 * use preallocation while we're discarding it */
3958 spin_unlock(&pa->pa_lock);
3959 spin_unlock(&ei->i_prealloc_lock);
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04003960 ext4_msg(sb, KERN_ERR,
3961 "uh-oh! used pa while discarding");
Alex Tomasc9de5602008-01-29 00:19:52 -05003962 WARN_ON(1);
3963 schedule_timeout_uninterruptible(HZ);
3964 goto repeat;
3965
3966 }
3967 if (pa->pa_deleted == 0) {
3968 pa->pa_deleted = 1;
3969 spin_unlock(&pa->pa_lock);
3970 list_del_rcu(&pa->pa_inode_list);
3971 list_add(&pa->u.pa_tmp_list, &list);
3972 continue;
3973 }
3974
3975 /* someone is deleting pa right now */
3976 spin_unlock(&pa->pa_lock);
3977 spin_unlock(&ei->i_prealloc_lock);
3978
3979 /* we have to wait here because pa_deleted
3980 * doesn't mean pa is already unlinked from
3981 * the list. as we might be called from
3982 * ->clear_inode() the inode will get freed
3983 * and concurrent thread which is unlinking
3984 * pa from inode's list may access already
3985 * freed memory, bad-bad-bad */
3986
3987 /* XXX: if this happens too often, we can
3988 * add a flag to force wait only in case
3989 * of ->clear_inode(), but not in case of
3990 * regular truncate */
3991 schedule_timeout_uninterruptible(HZ);
3992 goto repeat;
3993 }
3994 spin_unlock(&ei->i_prealloc_lock);
3995
3996 list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04003997 BUG_ON(pa->pa_type != MB_INODE_PA);
Lukas Czernerbd862982013-04-03 23:32:34 -04003998 group = ext4_get_group_number(sb, pa->pa_pstart);
Alex Tomasc9de5602008-01-29 00:19:52 -05003999
4000 err = ext4_mb_load_buddy(sb, group, &e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004001 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004002 ext4_error(sb, "Error loading buddy information for %u",
4003 group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004004 continue;
4005 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004006
Theodore Ts'o574ca172008-07-11 19:27:31 -04004007 bitmap_bh = ext4_read_block_bitmap(sb, group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004008 if (bitmap_bh == NULL) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004009 ext4_error(sb, "Error reading block bitmap for %u",
4010 group);
Jing Zhange39e07f2010-05-14 00:00:00 -04004011 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004012 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -05004013 }
4014
4015 ext4_lock_group(sb, group);
4016 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004017 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
Alex Tomasc9de5602008-01-29 00:19:52 -05004018 ext4_unlock_group(sb, group);
4019
Jing Zhange39e07f2010-05-14 00:00:00 -04004020 ext4_mb_unload_buddy(&e4b);
Alex Tomasc9de5602008-01-29 00:19:52 -05004021 put_bh(bitmap_bh);
4022
4023 list_del(&pa->u.pa_tmp_list);
4024 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4025 }
4026}
4027
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004028#ifdef CONFIG_EXT4_DEBUG
Alex Tomasc9de5602008-01-29 00:19:52 -05004029static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4030{
4031 struct super_block *sb = ac->ac_sb;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004032 ext4_group_t ngroups, i;
Alex Tomasc9de5602008-01-29 00:19:52 -05004033
Theodore Ts'oa0b30c12013-02-09 16:28:20 -05004034 if (!ext4_mballoc_debug ||
Theodore Ts'o4dd89fc2011-02-27 17:23:47 -05004035 (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED))
Eric Sandeene3570632010-07-27 11:56:08 -04004036 return;
4037
Joe Perches7f6a11e2012-03-19 23:09:43 -04004038 ext4_msg(ac->ac_sb, KERN_ERR, "Can't allocate:"
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004039 " Allocation context details:");
Joe Perches7f6a11e2012-03-19 23:09:43 -04004040 ext4_msg(ac->ac_sb, KERN_ERR, "status %d flags %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004041 ac->ac_status, ac->ac_flags);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004042 ext4_msg(ac->ac_sb, KERN_ERR, "orig %lu/%lu/%lu@%lu, "
Theodore Ts'o9d8b9ec2011-08-01 17:41:35 -04004043 "goal %lu/%lu/%lu@%lu, "
4044 "best %lu/%lu/%lu@%lu cr %d",
Alex Tomasc9de5602008-01-29 00:19:52 -05004045 (unsigned long)ac->ac_o_ex.fe_group,
4046 (unsigned long)ac->ac_o_ex.fe_start,
4047 (unsigned long)ac->ac_o_ex.fe_len,
4048 (unsigned long)ac->ac_o_ex.fe_logical,
4049 (unsigned long)ac->ac_g_ex.fe_group,
4050 (unsigned long)ac->ac_g_ex.fe_start,
4051 (unsigned long)ac->ac_g_ex.fe_len,
4052 (unsigned long)ac->ac_g_ex.fe_logical,
4053 (unsigned long)ac->ac_b_ex.fe_group,
4054 (unsigned long)ac->ac_b_ex.fe_start,
4055 (unsigned long)ac->ac_b_ex.fe_len,
4056 (unsigned long)ac->ac_b_ex.fe_logical,
4057 (int)ac->ac_criteria);
Eric Sandeendc9ddd92014-02-20 13:32:10 -05004058 ext4_msg(ac->ac_sb, KERN_ERR, "%d found", ac->ac_found);
Joe Perches7f6a11e2012-03-19 23:09:43 -04004059 ext4_msg(ac->ac_sb, KERN_ERR, "groups: ");
Theodore Ts'o8df96752009-05-01 08:50:38 -04004060 ngroups = ext4_get_groups_count(sb);
4061 for (i = 0; i < ngroups; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004062 struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4063 struct ext4_prealloc_space *pa;
4064 ext4_grpblk_t start;
4065 struct list_head *cur;
4066 ext4_lock_group(sb, i);
4067 list_for_each(cur, &grp->bb_prealloc_list) {
4068 pa = list_entry(cur, struct ext4_prealloc_space,
4069 pa_group_list);
4070 spin_lock(&pa->pa_lock);
4071 ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4072 NULL, &start);
4073 spin_unlock(&pa->pa_lock);
Akira Fujita1c718502009-07-05 23:04:36 -04004074 printk(KERN_ERR "PA:%u:%d:%u \n", i,
4075 start, pa->pa_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004076 }
Solofo Ramangalahy60bd63d2008-04-29 21:59:59 -04004077 ext4_unlock_group(sb, i);
Alex Tomasc9de5602008-01-29 00:19:52 -05004078
4079 if (grp->bb_free == 0)
4080 continue;
Akira Fujita1c718502009-07-05 23:04:36 -04004081 printk(KERN_ERR "%u: %d/%d \n",
Alex Tomasc9de5602008-01-29 00:19:52 -05004082 i, grp->bb_free, grp->bb_fragments);
4083 }
4084 printk(KERN_ERR "\n");
4085}
4086#else
4087static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4088{
4089 return;
4090}
4091#endif
4092
4093/*
4094 * We use locality group preallocation for small size file. The size of the
4095 * file is determined by the current size or the resulting size after
4096 * allocation which ever is larger
4097 *
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004098 * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
Alex Tomasc9de5602008-01-29 00:19:52 -05004099 */
4100static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4101{
4102 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4103 int bsbits = ac->ac_sb->s_blocksize_bits;
4104 loff_t size, isize;
4105
4106 if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4107 return;
4108
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004109 if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4110 return;
4111
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004112 size = ac->ac_o_ex.fe_logical + EXT4_C2B(sbi, ac->ac_o_ex.fe_len);
Theodore Ts'o50797482009-09-18 13:34:02 -04004113 isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
4114 >> bsbits;
Alex Tomasc9de5602008-01-29 00:19:52 -05004115
Theodore Ts'o50797482009-09-18 13:34:02 -04004116 if ((size == isize) &&
4117 !ext4_fs_is_busy(sbi) &&
4118 (atomic_read(&ac->ac_inode->i_writecount) == 0)) {
4119 ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
4120 return;
4121 }
4122
Robin Dongebbe0272011-10-26 05:14:27 -04004123 if (sbi->s_mb_group_prealloc <= 0) {
4124 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
4125 return;
4126 }
4127
Alex Tomasc9de5602008-01-29 00:19:52 -05004128 /* don't use group allocation for large files */
Theodore Ts'o71780572009-09-28 00:06:20 -04004129 size = max(size, isize);
Tao Macc483f12010-03-01 19:06:35 -05004130 if (size > sbi->s_mb_stream_request) {
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004131 ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004132 return;
Theodore Ts'o4ba74d02009-08-09 22:01:13 -04004133 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004134
4135 BUG_ON(ac->ac_lg != NULL);
4136 /*
4137 * locality group prealloc space are per cpu. The reason for having
4138 * per cpu locality group is to reduce the contention between block
4139 * request from multiple CPUs.
4140 */
Christoph Lametera0b6bc62014-08-17 12:30:28 -05004141 ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
Alex Tomasc9de5602008-01-29 00:19:52 -05004142
4143 /* we're going to use group allocation */
4144 ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4145
4146 /* serialize all allocations in the group */
4147 mutex_lock(&ac->ac_lg->lg_mutex);
4148}
4149
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004150static noinline_for_stack int
4151ext4_mb_initialize_context(struct ext4_allocation_context *ac,
Alex Tomasc9de5602008-01-29 00:19:52 -05004152 struct ext4_allocation_request *ar)
4153{
4154 struct super_block *sb = ar->inode->i_sb;
4155 struct ext4_sb_info *sbi = EXT4_SB(sb);
4156 struct ext4_super_block *es = sbi->s_es;
4157 ext4_group_t group;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004158 unsigned int len;
4159 ext4_fsblk_t goal;
Alex Tomasc9de5602008-01-29 00:19:52 -05004160 ext4_grpblk_t block;
4161
4162 /* we can't allocate > group size */
4163 len = ar->len;
4164
4165 /* just a dirty hack to filter too big requests */
Theodore Ts'o40ae3482013-02-04 15:08:40 -05004166 if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
4167 len = EXT4_CLUSTERS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004168
4169 /* start searching from the goal */
4170 goal = ar->goal;
4171 if (goal < le32_to_cpu(es->s_first_data_block) ||
4172 goal >= ext4_blocks_count(es))
4173 goal = le32_to_cpu(es->s_first_data_block);
4174 ext4_get_group_no_and_offset(sb, goal, &group, &block);
4175
4176 /* set up allocation goals */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004177 ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
Alex Tomasc9de5602008-01-29 00:19:52 -05004178 ac->ac_status = AC_STATUS_CONTINUE;
Alex Tomasc9de5602008-01-29 00:19:52 -05004179 ac->ac_sb = sb;
4180 ac->ac_inode = ar->inode;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004181 ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
Alex Tomasc9de5602008-01-29 00:19:52 -05004182 ac->ac_o_ex.fe_group = group;
4183 ac->ac_o_ex.fe_start = block;
4184 ac->ac_o_ex.fe_len = len;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004185 ac->ac_g_ex = ac->ac_o_ex;
Alex Tomasc9de5602008-01-29 00:19:52 -05004186 ac->ac_flags = ar->flags;
Alex Tomasc9de5602008-01-29 00:19:52 -05004187
4188 /* we have to define context: we'll we work with a file or
4189 * locality group. this is a policy, actually */
4190 ext4_mb_group_or_file(ac);
4191
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004192 mb_debug(1, "init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
Alex Tomasc9de5602008-01-29 00:19:52 -05004193 "left: %u/%u, right %u/%u to %swritable\n",
4194 (unsigned) ar->len, (unsigned) ar->logical,
4195 (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4196 (unsigned) ar->lleft, (unsigned) ar->pleft,
4197 (unsigned) ar->lright, (unsigned) ar->pright,
4198 atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4199 return 0;
4200
4201}
4202
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004203static noinline_for_stack void
4204ext4_mb_discard_lg_preallocations(struct super_block *sb,
4205 struct ext4_locality_group *lg,
4206 int order, int total_entries)
4207{
4208 ext4_group_t group = 0;
4209 struct ext4_buddy e4b;
4210 struct list_head discard_list;
4211 struct ext4_prealloc_space *pa, *tmp;
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004212
Theodore Ts'o6ba495e2009-09-18 13:38:55 -04004213 mb_debug(1, "discard locality group preallocation\n");
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004214
4215 INIT_LIST_HEAD(&discard_list);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004216
4217 spin_lock(&lg->lg_prealloc_lock);
4218 list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
4219 pa_inode_list) {
4220 spin_lock(&pa->pa_lock);
4221 if (atomic_read(&pa->pa_count)) {
4222 /*
4223 * This is the pa that we just used
4224 * for block allocation. So don't
4225 * free that
4226 */
4227 spin_unlock(&pa->pa_lock);
4228 continue;
4229 }
4230 if (pa->pa_deleted) {
4231 spin_unlock(&pa->pa_lock);
4232 continue;
4233 }
4234 /* only lg prealloc space */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004235 BUG_ON(pa->pa_type != MB_GROUP_PA);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004236
4237 /* seems this one can be freed ... */
4238 pa->pa_deleted = 1;
4239 spin_unlock(&pa->pa_lock);
4240
4241 list_del_rcu(&pa->pa_inode_list);
4242 list_add(&pa->u.pa_tmp_list, &discard_list);
4243
4244 total_entries--;
4245 if (total_entries <= 5) {
4246 /*
4247 * we want to keep only 5 entries
4248 * allowing it to grow to 8. This
4249 * mak sure we don't call discard
4250 * soon for this list.
4251 */
4252 break;
4253 }
4254 }
4255 spin_unlock(&lg->lg_prealloc_lock);
4256
4257 list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
4258
Lukas Czernerbd862982013-04-03 23:32:34 -04004259 group = ext4_get_group_number(sb, pa->pa_pstart);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004260 if (ext4_mb_load_buddy(sb, group, &e4b)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004261 ext4_error(sb, "Error loading buddy information for %u",
4262 group);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004263 continue;
4264 }
4265 ext4_lock_group(sb, group);
4266 list_del(&pa->pa_group_list);
Eric Sandeen3e1e5f52010-10-27 21:30:07 -04004267 ext4_mb_release_group_pa(&e4b, pa);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004268 ext4_unlock_group(sb, group);
4269
Jing Zhange39e07f2010-05-14 00:00:00 -04004270 ext4_mb_unload_buddy(&e4b);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004271 list_del(&pa->u.pa_tmp_list);
4272 call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4273 }
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004274}
4275
4276/*
4277 * We have incremented pa_count. So it cannot be freed at this
4278 * point. Also we hold lg_mutex. So no parallel allocation is
4279 * possible from this lg. That means pa_free cannot be updated.
4280 *
4281 * A parallel ext4_mb_discard_group_preallocations is possible.
4282 * which can cause the lg_prealloc_list to be updated.
4283 */
4284
4285static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
4286{
4287 int order, added = 0, lg_prealloc_count = 1;
4288 struct super_block *sb = ac->ac_sb;
4289 struct ext4_locality_group *lg = ac->ac_lg;
4290 struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
4291
4292 order = fls(pa->pa_free) - 1;
4293 if (order > PREALLOC_TB_SIZE - 1)
4294 /* The max size of hash table is PREALLOC_TB_SIZE */
4295 order = PREALLOC_TB_SIZE - 1;
4296 /* Add the prealloc space to lg */
Niu Yaweif1167002013-02-01 21:31:27 -05004297 spin_lock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004298 list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
4299 pa_inode_list) {
4300 spin_lock(&tmp_pa->pa_lock);
4301 if (tmp_pa->pa_deleted) {
Theodore Ts'oe7c9e3e2009-03-27 19:43:21 -04004302 spin_unlock(&tmp_pa->pa_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004303 continue;
4304 }
4305 if (!added && pa->pa_free < tmp_pa->pa_free) {
4306 /* Add to the tail of the previous entry */
4307 list_add_tail_rcu(&pa->pa_inode_list,
4308 &tmp_pa->pa_inode_list);
4309 added = 1;
4310 /*
4311 * we want to count the total
4312 * number of entries in the list
4313 */
4314 }
4315 spin_unlock(&tmp_pa->pa_lock);
4316 lg_prealloc_count++;
4317 }
4318 if (!added)
4319 list_add_tail_rcu(&pa->pa_inode_list,
4320 &lg->lg_prealloc_list[order]);
Niu Yaweif1167002013-02-01 21:31:27 -05004321 spin_unlock(&lg->lg_prealloc_lock);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004322
4323 /* Now trim the list to be not more than 8 elements */
4324 if (lg_prealloc_count > 8) {
4325 ext4_mb_discard_lg_preallocations(sb, lg,
Niu Yaweif1167002013-02-01 21:31:27 -05004326 order, lg_prealloc_count);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004327 return;
4328 }
4329 return ;
4330}
4331
Alex Tomasc9de5602008-01-29 00:19:52 -05004332/*
4333 * release all resource we used in allocation
4334 */
4335static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4336{
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004337 struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004338 struct ext4_prealloc_space *pa = ac->ac_pa;
4339 if (pa) {
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004340 if (pa->pa_type == MB_GROUP_PA) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004341 /* see comment in ext4_mb_use_group_pa() */
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004342 spin_lock(&pa->pa_lock);
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004343 pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4344 pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
Aneesh Kumar K.V6be2ded2008-07-23 14:14:05 -04004345 pa->pa_free -= ac->ac_b_ex.fe_len;
4346 pa->pa_len -= ac->ac_b_ex.fe_len;
4347 spin_unlock(&pa->pa_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05004348 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004349 }
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004350 if (pa) {
4351 /*
4352 * We want to add the pa to the right bucket.
4353 * Remove it from the list and while adding
4354 * make sure the list to which we are adding
Amir Goldstein44183d42011-05-09 21:52:36 -04004355 * doesn't grow big.
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004356 */
Aneesh Kumar K.Vcc0fb9a2009-03-27 17:16:58 -04004357 if ((pa->pa_type == MB_GROUP_PA) && likely(pa->pa_free)) {
Aneesh Kumar K.Vba443912009-02-10 11:14:34 -05004358 spin_lock(pa->pa_obj_lock);
4359 list_del_rcu(&pa->pa_inode_list);
4360 spin_unlock(pa->pa_obj_lock);
4361 ext4_mb_add_n_trim(ac);
4362 }
4363 ext4_mb_put_pa(ac, ac->ac_sb, pa);
4364 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004365 if (ac->ac_bitmap_page)
4366 page_cache_release(ac->ac_bitmap_page);
4367 if (ac->ac_buddy_page)
4368 page_cache_release(ac->ac_buddy_page);
4369 if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4370 mutex_unlock(&ac->ac_lg->lg_mutex);
4371 ext4_mb_collect_stats(ac);
4372 return 0;
4373}
4374
4375static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4376{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004377 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004378 int ret;
4379 int freed = 0;
4380
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004381 trace_ext4_mb_discard_preallocations(sb, needed);
Theodore Ts'o8df96752009-05-01 08:50:38 -04004382 for (i = 0; i < ngroups && needed > 0; i++) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004383 ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4384 freed += ret;
4385 needed -= ret;
4386 }
4387
4388 return freed;
4389}
4390
4391/*
4392 * Main entry point into mballoc to allocate blocks
4393 * it tries to use preallocation first, then falls back
4394 * to usual allocation
4395 */
4396ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
Aditya Kali6c7a1202010-08-05 16:22:24 -04004397 struct ext4_allocation_request *ar, int *errp)
Alex Tomasc9de5602008-01-29 00:19:52 -05004398{
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04004399 int freed;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004400 struct ext4_allocation_context *ac = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004401 struct ext4_sb_info *sbi;
4402 struct super_block *sb;
4403 ext4_fsblk_t block = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01004404 unsigned int inquota = 0;
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004405 unsigned int reserv_clstrs = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004406
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004407 might_sleep();
Alex Tomasc9de5602008-01-29 00:19:52 -05004408 sb = ar->inode->i_sb;
4409 sbi = EXT4_SB(sb);
4410
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004411 trace_ext4_request_blocks(ar);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004412
Dmitry Monakhov45dc63e2011-10-20 20:07:23 -04004413 /* Allow to use superuser reservation for quota file */
4414 if (IS_NOQUOTA(ar->inode))
4415 ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
4416
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004417 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
Mingming Cao60e58e02009-01-22 18:13:05 +01004418 /* Without delayed allocation we need to verify
4419 * there is enough free blocks to do block allocation
4420 * and verify allocation doesn't exceed the quota limits.
Mingming Caod2a17632008-07-14 17:52:37 -04004421 */
Allison Henderson55f020d2011-05-25 07:41:26 -04004422 while (ar->len &&
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04004423 ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004424
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004425 /* let others to free the space */
Lukas Czernerbb8b20e2013-03-10 22:28:09 -04004426 cond_resched();
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04004427 ar->len = ar->len >> 1;
4428 }
4429 if (!ar->len) {
Aneesh Kumar K.Va30d542a2008-10-09 10:56:23 -04004430 *errp = -ENOSPC;
4431 return 0;
4432 }
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004433 reserv_clstrs = ar->len;
Allison Henderson55f020d2011-05-25 07:41:26 -04004434 if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004435 dquot_alloc_block_nofail(ar->inode,
4436 EXT4_C2B(sbi, ar->len));
Allison Henderson55f020d2011-05-25 07:41:26 -04004437 } else {
4438 while (ar->len &&
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004439 dquot_alloc_block(ar->inode,
4440 EXT4_C2B(sbi, ar->len))) {
Allison Henderson55f020d2011-05-25 07:41:26 -04004441
4442 ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4443 ar->len--;
4444 }
Mingming Cao60e58e02009-01-22 18:13:05 +01004445 }
4446 inquota = ar->len;
4447 if (ar->len == 0) {
4448 *errp = -EDQUOT;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004449 goto out;
Mingming Cao60e58e02009-01-22 18:13:05 +01004450 }
Mingming Caod2a17632008-07-14 17:52:37 -04004451 }
Mingming Caod2a17632008-07-14 17:52:37 -04004452
Wei Yongjun85556c92012-09-26 20:43:37 -04004453 ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
Theodore Ts'o833576b2009-07-13 09:45:52 -04004454 if (!ac) {
Shen Feng363d4252008-07-11 19:27:31 -04004455 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004456 *errp = -ENOMEM;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004457 goto out;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004458 }
4459
Eric Sandeen256bdb42008-02-10 01:13:33 -05004460 *errp = ext4_mb_initialize_context(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004461 if (*errp) {
4462 ar->len = 0;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004463 goto out;
Alex Tomasc9de5602008-01-29 00:19:52 -05004464 }
4465
Eric Sandeen256bdb42008-02-10 01:13:33 -05004466 ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4467 if (!ext4_mb_use_preallocated(ac)) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004468 ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4469 ext4_mb_normalize_request(ac, ar);
Alex Tomasc9de5602008-01-29 00:19:52 -05004470repeat:
4471 /* allocate space in core */
Aditya Kali6c7a1202010-08-05 16:22:24 -04004472 *errp = ext4_mb_regular_allocator(ac);
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004473 if (*errp)
4474 goto discard_and_exit;
4475
4476 /* as we've just preallocated more space than
4477 * user requested originally, we store allocated
4478 * space in a special descriptor */
4479 if (ac->ac_status == AC_STATUS_FOUND &&
4480 ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4481 *errp = ext4_mb_new_preallocation(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004482 if (*errp) {
Alexey Khoroshilov2c00ef32013-07-01 08:12:36 -04004483 discard_and_exit:
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004484 ext4_discard_allocated_blocks(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004485 goto errout;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004486 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004487 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004488 if (likely(ac->ac_status == AC_STATUS_FOUND)) {
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004489 *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004490 if (*errp == -EAGAIN) {
Aneesh Kumar K.V8556e8f2009-01-05 21:46:55 -05004491 /*
4492 * drop the reference that we took
4493 * in ext4_mb_use_best_found
4494 */
4495 ext4_mb_release_context(ac);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004496 ac->ac_b_ex.fe_group = 0;
4497 ac->ac_b_ex.fe_start = 0;
4498 ac->ac_b_ex.fe_len = 0;
4499 ac->ac_status = AC_STATUS_CONTINUE;
4500 goto repeat;
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004501 } else if (*errp) {
Curt Wohlgemuthb8441672009-12-08 22:18:25 -05004502 ext4_discard_allocated_blocks(ac);
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004503 goto errout;
4504 } else {
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004505 block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4506 ar->len = ac->ac_b_ex.fe_len;
4507 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004508 } else {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004509 freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
Alex Tomasc9de5602008-01-29 00:19:52 -05004510 if (freed)
4511 goto repeat;
4512 *errp = -ENOSPC;
Aditya Kali6c7a1202010-08-05 16:22:24 -04004513 }
4514
Eric Sandeen6d138ce2012-11-08 11:11:59 -05004515errout:
Aditya Kali6c7a1202010-08-05 16:22:24 -04004516 if (*errp) {
Eric Sandeen256bdb42008-02-10 01:13:33 -05004517 ac->ac_b_ex.fe_len = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004518 ar->len = 0;
Eric Sandeen256bdb42008-02-10 01:13:33 -05004519 ext4_mb_show_ac(ac);
Alex Tomasc9de5602008-01-29 00:19:52 -05004520 }
Eric Sandeen256bdb42008-02-10 01:13:33 -05004521 ext4_mb_release_context(ac);
Aditya Kali6c7a1202010-08-05 16:22:24 -04004522out:
4523 if (ac)
4524 kmem_cache_free(ext4_ac_cachep, ac);
Mingming Cao60e58e02009-01-22 18:13:05 +01004525 if (inquota && ar->len < inquota)
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004526 dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004527 if (!ar->len) {
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004528 if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004529 /* release all the reserved blocks if non delalloc */
Theodore Ts'o57042652011-09-09 18:56:51 -04004530 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Theodore Ts'o53accfa2011-09-09 18:48:51 -04004531 reserv_clstrs);
Aneesh Kumar K.V0087d9f2009-01-05 21:49:12 -05004532 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004533
Theodore Ts'o9bffad12009-06-17 11:48:11 -04004534 trace_ext4_allocate_blocks(ar, (unsigned long long)block);
Theodore Ts'oba80b102009-01-03 20:03:21 -05004535
Alex Tomasc9de5602008-01-29 00:19:52 -05004536 return block;
4537}
Alex Tomasc9de5602008-01-29 00:19:52 -05004538
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004539/*
4540 * We can merge two free data extents only if the physical blocks
4541 * are contiguous, AND the extents were freed by the same transaction,
4542 * AND the blocks are associated with the same group.
4543 */
4544static int can_merge(struct ext4_free_data *entry1,
4545 struct ext4_free_data *entry2)
4546{
Bobi Jam18aadd42012-02-20 17:53:02 -05004547 if ((entry1->efd_tid == entry2->efd_tid) &&
4548 (entry1->efd_group == entry2->efd_group) &&
4549 ((entry1->efd_start_cluster + entry1->efd_count) == entry2->efd_start_cluster))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004550 return 1;
4551 return 0;
4552}
4553
Eric Sandeen4ddfef72008-04-29 08:11:12 -04004554static noinline_for_stack int
4555ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004556 struct ext4_free_data *new_entry)
Alex Tomasc9de5602008-01-29 00:19:52 -05004557{
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004558 ext4_group_t group = e4b->bd_group;
Theodore Ts'o84130192011-09-09 18:50:51 -04004559 ext4_grpblk_t cluster;
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004560 struct ext4_free_data *entry;
Alex Tomasc9de5602008-01-29 00:19:52 -05004561 struct ext4_group_info *db = e4b->bd_info;
4562 struct super_block *sb = e4b->bd_sb;
4563 struct ext4_sb_info *sbi = EXT4_SB(sb);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004564 struct rb_node **n = &db->bb_free_root.rb_node, *node;
4565 struct rb_node *parent = NULL, *new_node;
4566
Frank Mayhar03901312009-01-07 00:06:22 -05004567 BUG_ON(!ext4_handle_valid(handle));
Alex Tomasc9de5602008-01-29 00:19:52 -05004568 BUG_ON(e4b->bd_bitmap_page == NULL);
4569 BUG_ON(e4b->bd_buddy_page == NULL);
4570
Bobi Jam18aadd42012-02-20 17:53:02 -05004571 new_node = &new_entry->efd_node;
4572 cluster = new_entry->efd_start_cluster;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004573
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004574 if (!*n) {
4575 /* first free block exent. We need to
4576 protect buddy cache from being freed,
4577 * otherwise we'll refresh it from
4578 * on-disk bitmap and lose not-yet-available
4579 * blocks */
4580 page_cache_get(e4b->bd_buddy_page);
4581 page_cache_get(e4b->bd_bitmap_page);
4582 }
4583 while (*n) {
4584 parent = *n;
Bobi Jam18aadd42012-02-20 17:53:02 -05004585 entry = rb_entry(parent, struct ext4_free_data, efd_node);
4586 if (cluster < entry->efd_start_cluster)
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004587 n = &(*n)->rb_left;
Bobi Jam18aadd42012-02-20 17:53:02 -05004588 else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004589 n = &(*n)->rb_right;
4590 else {
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004591 ext4_grp_locked_error(sb, group, 0,
Theodore Ts'o84130192011-09-09 18:50:51 -04004592 ext4_group_first_block_no(sb, group) +
4593 EXT4_C2B(sbi, cluster),
Theodore Ts'oe29136f2010-06-29 12:54:28 -04004594 "Block already on to-be-freed list");
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004595 return 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004596 }
4597 }
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004598
4599 rb_link_node(new_node, parent, n);
4600 rb_insert_color(new_node, &db->bb_free_root);
4601
4602 /* Now try to see the extent can be merged to left and right */
4603 node = rb_prev(new_node);
4604 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004605 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004606 if (can_merge(entry, new_entry) &&
4607 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004608 new_entry->efd_start_cluster = entry->efd_start_cluster;
4609 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004610 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004611 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004612 }
4613 }
4614
4615 node = rb_next(new_node);
4616 if (node) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004617 entry = rb_entry(node, struct ext4_free_data, efd_node);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -04004618 if (can_merge(new_entry, entry) &&
4619 ext4_journal_callback_try_del(handle, &entry->efd_jce)) {
Bobi Jam18aadd42012-02-20 17:53:02 -05004620 new_entry->efd_count += entry->efd_count;
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004621 rb_erase(node, &(db->bb_free_root));
Bobi Jam18aadd42012-02-20 17:53:02 -05004622 kmem_cache_free(ext4_free_data_cachep, entry);
Aneesh Kumar K.Vc8940582008-10-16 10:14:27 -04004623 }
4624 }
Theodore Ts'o3e624fc2008-10-16 20:00:24 -04004625 /* Add the extent to transaction's private list */
Bobi Jam18aadd42012-02-20 17:53:02 -05004626 ext4_journal_callback_add(handle, ext4_free_data_callback,
4627 &new_entry->efd_jce);
Alex Tomasc9de5602008-01-29 00:19:52 -05004628 return 0;
4629}
4630
Theodore Ts'o44338712009-11-22 07:44:56 -05004631/**
4632 * ext4_free_blocks() -- Free given blocks and update quota
4633 * @handle: handle for this transaction
4634 * @inode: inode
4635 * @block: start physical block to free
4636 * @count: number of blocks to count
Yongqiang Yang5def1362011-06-05 23:26:40 -04004637 * @flags: flags used by ext4_free_blocks
Alex Tomasc9de5602008-01-29 00:19:52 -05004638 */
Theodore Ts'o44338712009-11-22 07:44:56 -05004639void ext4_free_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004640 struct buffer_head *bh, ext4_fsblk_t block,
4641 unsigned long count, int flags)
Alex Tomasc9de5602008-01-29 00:19:52 -05004642{
Aneesh Kumar K.V26346ff2008-02-10 01:10:04 -05004643 struct buffer_head *bitmap_bh = NULL;
Alex Tomasc9de5602008-01-29 00:19:52 -05004644 struct super_block *sb = inode->i_sb;
Alex Tomasc9de5602008-01-29 00:19:52 -05004645 struct ext4_group_desc *gdp;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004646 unsigned int overflow;
Alex Tomasc9de5602008-01-29 00:19:52 -05004647 ext4_grpblk_t bit;
4648 struct buffer_head *gd_bh;
4649 ext4_group_t block_group;
4650 struct ext4_sb_info *sbi;
4651 struct ext4_buddy e4b;
Theodore Ts'o84130192011-09-09 18:50:51 -04004652 unsigned int count_clusters;
Alex Tomasc9de5602008-01-29 00:19:52 -05004653 int err = 0;
4654 int ret;
4655
Theodore Ts'ob10a44c2013-04-03 22:00:52 -04004656 might_sleep();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004657 if (bh) {
4658 if (block)
4659 BUG_ON(block != bh->b_blocknr);
4660 else
4661 block = bh->b_blocknr;
4662 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004663
Alex Tomasc9de5602008-01-29 00:19:52 -05004664 sbi = EXT4_SB(sb);
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004665 if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
4666 !ext4_data_block_valid(sbi, block, count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004667 ext4_error(sb, "Freeing blocks not in datazone - "
Theodore Ts'o1f2acb62010-01-22 17:40:42 -05004668 "block = %llu, count = %lu", block, count);
Alex Tomasc9de5602008-01-29 00:19:52 -05004669 goto error_return;
4670 }
4671
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004672 ext4_debug("freeing block %llu\n", block);
Theodore Ts'oe6362602009-11-23 07:17:05 -05004673 trace_ext4_free_blocks(inode, block, count, flags);
4674
4675 if (flags & EXT4_FREE_BLOCKS_FORGET) {
4676 struct buffer_head *tbh = bh;
4677 int i;
4678
4679 BUG_ON(bh && (count > 1));
4680
4681 for (i = 0; i < count; i++) {
Theodore Ts'o2ed57242013-06-12 11:43:02 -04004682 cond_resched();
Theodore Ts'oe6362602009-11-23 07:17:05 -05004683 if (!bh)
4684 tbh = sb_find_get_block(inode->i_sb,
4685 block + i);
Theodore Ts'o2ed57242013-06-12 11:43:02 -04004686 if (!tbh)
Namhyung Kim87783692010-10-27 21:30:11 -04004687 continue;
Theodore Ts'o60e66792010-05-17 07:00:00 -04004688 ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
Theodore Ts'oe6362602009-11-23 07:17:05 -05004689 inode, tbh, block + i);
4690 }
4691 }
4692
Theodore Ts'o60e66792010-05-17 07:00:00 -04004693 /*
Theodore Ts'oe6362602009-11-23 07:17:05 -05004694 * We need to make sure we don't reuse the freed block until
4695 * after the transaction is committed, which we can do by
4696 * treating the block as metadata, below. We make an
4697 * exception if the inode is to be written in writeback mode
4698 * since writeback mode has weak data consistency guarantees.
4699 */
4700 if (!ext4_should_writeback_data(inode))
4701 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasc9de5602008-01-29 00:19:52 -05004702
Theodore Ts'o84130192011-09-09 18:50:51 -04004703 /*
4704 * If the extent to be freed does not begin on a cluster
4705 * boundary, we need to deal with partial clusters at the
4706 * beginning and end of the extent. Normally we will free
4707 * blocks at the beginning or the end unless we are explicitly
4708 * requested to avoid doing so.
4709 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004710 overflow = EXT4_PBLK_COFF(sbi, block);
Theodore Ts'o84130192011-09-09 18:50:51 -04004711 if (overflow) {
4712 if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
4713 overflow = sbi->s_cluster_ratio - overflow;
4714 block += overflow;
4715 if (count > overflow)
4716 count -= overflow;
4717 else
4718 return;
4719 } else {
4720 block -= overflow;
4721 count += overflow;
4722 }
4723 }
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004724 overflow = EXT4_LBLK_COFF(sbi, count);
Theodore Ts'o84130192011-09-09 18:50:51 -04004725 if (overflow) {
4726 if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
4727 if (count > overflow)
4728 count -= overflow;
4729 else
4730 return;
4731 } else
4732 count += sbi->s_cluster_ratio - overflow;
4733 }
4734
Alex Tomasc9de5602008-01-29 00:19:52 -05004735do_more:
4736 overflow = 0;
4737 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4738
Darrick J. Wong163a2032013-08-28 17:35:51 -04004739 if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(
4740 ext4_get_group_info(sb, block_group))))
4741 return;
4742
Alex Tomasc9de5602008-01-29 00:19:52 -05004743 /*
4744 * Check to see if we are freeing blocks across a group
4745 * boundary.
4746 */
Theodore Ts'o84130192011-09-09 18:50:51 -04004747 if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4748 overflow = EXT4_C2B(sbi, bit) + count -
4749 EXT4_BLOCKS_PER_GROUP(sb);
Alex Tomasc9de5602008-01-29 00:19:52 -05004750 count -= overflow;
4751 }
Lukas Czerner810da242013-03-02 17:18:58 -05004752 count_clusters = EXT4_NUM_B2C(sbi, count);
Theodore Ts'o574ca172008-07-11 19:27:31 -04004753 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004754 if (!bitmap_bh) {
4755 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004756 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004757 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004758 gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004759 if (!gdp) {
4760 err = -EIO;
Alex Tomasc9de5602008-01-29 00:19:52 -05004761 goto error_return;
Aneesh Kumar K.Vce89f462008-07-23 14:09:29 -04004762 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004763
4764 if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4765 in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4766 in_range(block, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004767 EXT4_SB(sb)->s_itb_per_group) ||
Alex Tomasc9de5602008-01-29 00:19:52 -05004768 in_range(block + count - 1, ext4_inode_table(sb, gdp),
Theodore Ts'o84130192011-09-09 18:50:51 -04004769 EXT4_SB(sb)->s_itb_per_group)) {
Alex Tomasc9de5602008-01-29 00:19:52 -05004770
Eric Sandeen12062dd2010-02-15 14:19:27 -05004771 ext4_error(sb, "Freeing blocks in system zone - "
Theodore Ts'o0610b6e2009-06-15 03:45:05 -04004772 "Block = %llu, count = %lu", block, count);
Aneesh Kumar K.V519deca02008-05-15 14:43:20 -04004773 /* err = 0. ext4_std_error should be a no op */
4774 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004775 }
4776
4777 BUFFER_TRACE(bitmap_bh, "getting write access");
4778 err = ext4_journal_get_write_access(handle, bitmap_bh);
4779 if (err)
4780 goto error_return;
4781
4782 /*
4783 * We are about to modify some metadata. Call the journal APIs
4784 * to unshare ->b_data if a currently-committing transaction is
4785 * using it
4786 */
4787 BUFFER_TRACE(gd_bh, "get_write_access");
4788 err = ext4_journal_get_write_access(handle, gd_bh);
4789 if (err)
4790 goto error_return;
Alex Tomasc9de5602008-01-29 00:19:52 -05004791#ifdef AGGRESSIVE_CHECK
4792 {
4793 int i;
Theodore Ts'o84130192011-09-09 18:50:51 -04004794 for (i = 0; i < count_clusters; i++)
Alex Tomasc9de5602008-01-29 00:19:52 -05004795 BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4796 }
4797#endif
Theodore Ts'o84130192011-09-09 18:50:51 -04004798 trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004799
Aneesh Kumar K.V920313a2009-01-05 21:36:19 -05004800 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4801 if (err)
4802 goto error_return;
Theodore Ts'oe6362602009-11-23 07:17:05 -05004803
4804 if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004805 struct ext4_free_data *new_entry;
4806 /*
4807 * blocks being freed are metadata. these blocks shouldn't
4808 * be used until this transaction is committed
Michal Hockoa345e342015-07-05 12:33:44 -04004809 *
4810 * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
4811 * to fail.
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004812 */
Michal Hockoa345e342015-07-05 12:33:44 -04004813 new_entry = kmem_cache_alloc(ext4_free_data_cachep,
4814 GFP_NOFS|__GFP_NOFAIL);
Bobi Jam18aadd42012-02-20 17:53:02 -05004815 new_entry->efd_start_cluster = bit;
4816 new_entry->efd_group = block_group;
4817 new_entry->efd_count = count_clusters;
4818 new_entry->efd_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004819
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004820 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004821 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004822 ext4_mb_free_metadata(handle, &e4b, new_entry);
Alex Tomasc9de5602008-01-29 00:19:52 -05004823 } else {
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004824 /* need to update group_info->bb_free and bitmap
4825 * with group lock held. generate_buddy look at
4826 * them with group lock_held
4827 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004828 if (test_opt(sb, DISCARD)) {
JP Abgrallb7b01892014-07-23 16:55:07 -07004829 err = ext4_issue_discard(sb, block_group, bit, count,
4830 0);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004831 if (err && err != -EOPNOTSUPP)
4832 ext4_msg(sb, KERN_WARNING, "discard request in"
4833 " group:%d block:%d count:%lu failed"
4834 " with %d", block_group, bit, count,
4835 err);
Lukas Czerner8f9ff182013-10-30 11:10:52 -04004836 } else
4837 EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05004838
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004839 ext4_lock_group(sb, block_group);
Theodore Ts'o84130192011-09-09 18:50:51 -04004840 mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
4841 mb_free_blocks(inode, &e4b, bit, count_clusters);
Alex Tomasc9de5602008-01-29 00:19:52 -05004842 }
4843
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004844 ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
4845 ext4_free_group_clusters_set(sb, gdp, ret);
Tao Ma79f1ba42012-10-22 00:34:32 -04004846 ext4_block_bitmap_csum_set(sb, block_group, gdp, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004847 ext4_group_desc_csum_set(sb, block_group, gdp);
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04004848 ext4_unlock_group(sb, block_group);
Alex Tomasc9de5602008-01-29 00:19:52 -05004849
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004850 if (sbi->s_log_groups_per_flex) {
4851 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004852 atomic64_add(count_clusters,
4853 &sbi->s_flex_groups[flex_group].free_clusters);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004854 }
4855
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04004856 if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004857 dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
Jan Kara7d734532013-08-17 09:36:54 -04004858 percpu_counter_add(&sbi->s_freeclusters_counter, count_clusters);
4859
4860 ext4_mb_unload_buddy(&e4b);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004861
Aneesh Kumar K.V7a2fcbf2009-01-05 21:36:55 -05004862 /* We dirtied the bitmap block */
4863 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
4864 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4865
Alex Tomasc9de5602008-01-29 00:19:52 -05004866 /* And the group descriptor block */
4867 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
Frank Mayhar03901312009-01-07 00:06:22 -05004868 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
Alex Tomasc9de5602008-01-29 00:19:52 -05004869 if (!err)
4870 err = ret;
4871
4872 if (overflow && !err) {
4873 block += count;
4874 count = overflow;
4875 put_bh(bitmap_bh);
4876 goto do_more;
4877 }
Alex Tomasc9de5602008-01-29 00:19:52 -05004878error_return:
4879 brelse(bitmap_bh);
4880 ext4_std_error(sb, err);
Alex Tomasc9de5602008-01-29 00:19:52 -05004881 return;
4882}
Lukas Czerner7360d172010-10-27 21:30:12 -04004883
4884/**
Yongqiang Yang05291552011-07-26 21:43:56 -04004885 * ext4_group_add_blocks() -- Add given blocks to an existing group
Amir Goldstein2846e822011-05-09 10:46:41 -04004886 * @handle: handle to this transaction
4887 * @sb: super block
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004888 * @block: start physical block to add to the block group
Amir Goldstein2846e822011-05-09 10:46:41 -04004889 * @count: number of blocks to free
4890 *
Amir Goldsteine73a3472011-05-09 21:40:01 -04004891 * This marks the blocks as free in the bitmap and buddy.
Amir Goldstein2846e822011-05-09 10:46:41 -04004892 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004893int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
Amir Goldstein2846e822011-05-09 10:46:41 -04004894 ext4_fsblk_t block, unsigned long count)
4895{
4896 struct buffer_head *bitmap_bh = NULL;
4897 struct buffer_head *gd_bh;
4898 ext4_group_t block_group;
4899 ext4_grpblk_t bit;
4900 unsigned int i;
4901 struct ext4_group_desc *desc;
4902 struct ext4_sb_info *sbi = EXT4_SB(sb);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004903 struct ext4_buddy e4b;
Amir Goldstein2846e822011-05-09 10:46:41 -04004904 int err = 0, ret, blk_free_count;
4905 ext4_grpblk_t blocks_freed;
Amir Goldstein2846e822011-05-09 10:46:41 -04004906
4907 ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
4908
Yongqiang Yang4740b832011-07-26 21:51:08 -04004909 if (count == 0)
4910 return 0;
4911
Amir Goldstein2846e822011-05-09 10:46:41 -04004912 ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
Amir Goldstein2846e822011-05-09 10:46:41 -04004913 /*
4914 * Check to see if we are freeing blocks across a group
4915 * boundary.
4916 */
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004917 if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4918 ext4_warning(sb, "too much blocks added to group %u\n",
4919 block_group);
4920 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004921 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004922 }
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004923
Amir Goldstein2846e822011-05-09 10:46:41 -04004924 bitmap_bh = ext4_read_block_bitmap(sb, block_group);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004925 if (!bitmap_bh) {
4926 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004927 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004928 }
4929
Amir Goldstein2846e822011-05-09 10:46:41 -04004930 desc = ext4_get_group_desc(sb, block_group, &gd_bh);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004931 if (!desc) {
4932 err = -EIO;
Amir Goldstein2846e822011-05-09 10:46:41 -04004933 goto error_return;
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004934 }
Amir Goldstein2846e822011-05-09 10:46:41 -04004935
4936 if (in_range(ext4_block_bitmap(sb, desc), block, count) ||
4937 in_range(ext4_inode_bitmap(sb, desc), block, count) ||
4938 in_range(block, ext4_inode_table(sb, desc), sbi->s_itb_per_group) ||
4939 in_range(block + count - 1, ext4_inode_table(sb, desc),
4940 sbi->s_itb_per_group)) {
4941 ext4_error(sb, "Adding blocks in system zones - "
4942 "Block = %llu, count = %lu",
4943 block, count);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04004944 err = -EINVAL;
Amir Goldstein2846e822011-05-09 10:46:41 -04004945 goto error_return;
4946 }
4947
Theodore Ts'o2cd05cc2011-05-09 10:58:45 -04004948 BUFFER_TRACE(bitmap_bh, "getting write access");
4949 err = ext4_journal_get_write_access(handle, bitmap_bh);
Amir Goldstein2846e822011-05-09 10:46:41 -04004950 if (err)
4951 goto error_return;
4952
4953 /*
4954 * We are about to modify some metadata. Call the journal APIs
4955 * to unshare ->b_data if a currently-committing transaction is
4956 * using it
4957 */
4958 BUFFER_TRACE(gd_bh, "get_write_access");
4959 err = ext4_journal_get_write_access(handle, gd_bh);
4960 if (err)
4961 goto error_return;
Amir Goldsteine73a3472011-05-09 21:40:01 -04004962
Amir Goldstein2846e822011-05-09 10:46:41 -04004963 for (i = 0, blocks_freed = 0; i < count; i++) {
4964 BUFFER_TRACE(bitmap_bh, "clear bit");
Amir Goldsteine73a3472011-05-09 21:40:01 -04004965 if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
Amir Goldstein2846e822011-05-09 10:46:41 -04004966 ext4_error(sb, "bit already cleared for block %llu",
4967 (ext4_fsblk_t)(block + i));
4968 BUFFER_TRACE(bitmap_bh, "bit already cleared");
4969 } else {
4970 blocks_freed++;
4971 }
4972 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004973
4974 err = ext4_mb_load_buddy(sb, block_group, &e4b);
4975 if (err)
4976 goto error_return;
4977
4978 /*
4979 * need to update group_info->bb_free and bitmap
4980 * with group lock held. generate_buddy look at
4981 * them with group lock_held
4982 */
Amir Goldstein2846e822011-05-09 10:46:41 -04004983 ext4_lock_group(sb, block_group);
Amir Goldsteine73a3472011-05-09 21:40:01 -04004984 mb_clear_bits(bitmap_bh->b_data, bit, count);
4985 mb_free_blocks(NULL, &e4b, bit, count);
Theodore Ts'o021b65b2011-09-09 19:08:51 -04004986 blk_free_count = blocks_freed + ext4_free_group_clusters(sb, desc);
4987 ext4_free_group_clusters_set(sb, desc, blk_free_count);
Tao Ma79f1ba42012-10-22 00:34:32 -04004988 ext4_block_bitmap_csum_set(sb, block_group, desc, bitmap_bh);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004989 ext4_group_desc_csum_set(sb, block_group, desc);
Amir Goldstein2846e822011-05-09 10:46:41 -04004990 ext4_unlock_group(sb, block_group);
Theodore Ts'o57042652011-09-09 18:56:51 -04004991 percpu_counter_add(&sbi->s_freeclusters_counter,
Lukas Czerner810da242013-03-02 17:18:58 -05004992 EXT4_NUM_B2C(sbi, blocks_freed));
Amir Goldstein2846e822011-05-09 10:46:41 -04004993
4994 if (sbi->s_log_groups_per_flex) {
4995 ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04004996 atomic64_add(EXT4_NUM_B2C(sbi, blocks_freed),
4997 &sbi->s_flex_groups[flex_group].free_clusters);
Amir Goldstein2846e822011-05-09 10:46:41 -04004998 }
Amir Goldsteine73a3472011-05-09 21:40:01 -04004999
5000 ext4_mb_unload_buddy(&e4b);
Amir Goldstein2846e822011-05-09 10:46:41 -04005001
5002 /* We dirtied the bitmap block */
5003 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
5004 err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
5005
5006 /* And the group descriptor block */
5007 BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
5008 ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
5009 if (!err)
5010 err = ret;
5011
5012error_return:
5013 brelse(bitmap_bh);
5014 ext4_std_error(sb, err);
Yongqiang Yangcc7365d2011-07-26 21:46:07 -04005015 return err;
Amir Goldstein2846e822011-05-09 10:46:41 -04005016}
5017
5018/**
Lukas Czerner7360d172010-10-27 21:30:12 -04005019 * ext4_trim_extent -- function to TRIM one single free extent in the group
5020 * @sb: super block for the file system
5021 * @start: starting block of the free extent in the alloc. group
5022 * @count: number of blocks to TRIM
5023 * @group: alloc. group we are working with
5024 * @e4b: ext4 buddy for the group
JP Abgrallb7b01892014-07-23 16:55:07 -07005025 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005026 *
5027 * Trim "count" blocks starting at "start" in the "group". To assure that no
5028 * one will allocate those blocks, mark it as used in buddy bitmap. This must
5029 * be called with under the group lock.
5030 */
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005031static int ext4_trim_extent(struct super_block *sb, int start, int count,
JP Abgrallb7b01892014-07-23 16:55:07 -07005032 ext4_group_t group, struct ext4_buddy *e4b,
5033 unsigned long blkdev_flags)
jon ernste2cbd582014-04-12 23:01:28 -04005034__releases(bitlock)
5035__acquires(bitlock)
Lukas Czerner7360d172010-10-27 21:30:12 -04005036{
5037 struct ext4_free_extent ex;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005038 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005039
Tao Mab3d4c2b2011-07-11 00:01:52 -04005040 trace_ext4_trim_extent(sb, group, start, count);
5041
Lukas Czerner7360d172010-10-27 21:30:12 -04005042 assert_spin_locked(ext4_group_lock_ptr(sb, group));
5043
5044 ex.fe_start = start;
5045 ex.fe_group = group;
5046 ex.fe_len = count;
5047
5048 /*
5049 * Mark blocks used, so no one can reuse them while
5050 * being trimmed.
5051 */
5052 mb_mark_used(e4b, &ex);
5053 ext4_unlock_group(sb, group);
JP Abgrallb7b01892014-07-23 16:55:07 -07005054 ret = ext4_issue_discard(sb, group, start, count, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005055 ext4_lock_group(sb, group);
5056 mb_free_blocks(NULL, e4b, start, ex.fe_len);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005057 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005058}
5059
5060/**
5061 * ext4_trim_all_free -- function to trim all free space in alloc. group
5062 * @sb: super block for file system
Tao Ma22612282011-07-11 00:04:34 -04005063 * @group: group to be trimmed
Lukas Czerner7360d172010-10-27 21:30:12 -04005064 * @start: first group block to examine
5065 * @max: last group block to examine
5066 * @minblocks: minimum extent block count
JP Abgrallb7b01892014-07-23 16:55:07 -07005067 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005068 *
5069 * ext4_trim_all_free walks through group's buddy bitmap searching for free
5070 * extents. When the free block is found, ext4_trim_extent is called to TRIM
5071 * the extent.
5072 *
5073 *
5074 * ext4_trim_all_free walks through group's block bitmap searching for free
5075 * extents. When the free extent is found, mark it as used in group buddy
5076 * bitmap. Then issue a TRIM command on this extent and free the extent in
5077 * the group buddy bitmap. This is done until whole group is scanned.
5078 */
Lukas Czerner0b75a842011-02-23 12:22:49 -05005079static ext4_grpblk_t
Lukas Czerner78944082011-05-24 18:16:27 -04005080ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
5081 ext4_grpblk_t start, ext4_grpblk_t max,
JP Abgrallb7b01892014-07-23 16:55:07 -07005082 ext4_grpblk_t minblocks, unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005083{
5084 void *bitmap;
Tao Ma169ddc32011-07-11 00:00:07 -04005085 ext4_grpblk_t next, count = 0, free_count = 0;
Lukas Czerner78944082011-05-24 18:16:27 -04005086 struct ext4_buddy e4b;
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005087 int ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005088
Tao Mab3d4c2b2011-07-11 00:01:52 -04005089 trace_ext4_trim_all_free(sb, group, start, max);
5090
Lukas Czerner78944082011-05-24 18:16:27 -04005091 ret = ext4_mb_load_buddy(sb, group, &e4b);
5092 if (ret) {
5093 ext4_error(sb, "Error in loading buddy "
5094 "information for %u", group);
5095 return ret;
5096 }
Lukas Czerner78944082011-05-24 18:16:27 -04005097 bitmap = e4b.bd_bitmap;
Lukas Czerner28739ee2011-05-24 18:28:07 -04005098
5099 ext4_lock_group(sb, group);
Tao Ma3d56b8d2011-07-11 00:03:38 -04005100 if (EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) &&
5101 minblocks >= atomic_read(&EXT4_SB(sb)->s_last_trim_minblks))
5102 goto out;
5103
Lukas Czerner78944082011-05-24 18:16:27 -04005104 start = (e4b.bd_info->bb_first_free > start) ?
5105 e4b.bd_info->bb_first_free : start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005106
Lukas Czerner913eed832012-03-21 21:22:22 -04005107 while (start <= max) {
5108 start = mb_find_next_zero_bit(bitmap, max + 1, start);
5109 if (start > max)
Lukas Czerner7360d172010-10-27 21:30:12 -04005110 break;
Lukas Czerner913eed832012-03-21 21:22:22 -04005111 next = mb_find_next_bit(bitmap, max + 1, start);
Lukas Czerner7360d172010-10-27 21:30:12 -04005112
5113 if ((next - start) >= minblocks) {
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005114 ret = ext4_trim_extent(sb, start,
JP Abgrallb7b01892014-07-23 16:55:07 -07005115 next - start, group, &e4b,
5116 blkdev_flags);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005117 if (ret && ret != -EOPNOTSUPP)
5118 break;
5119 ret = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005120 count += next - start;
5121 }
Tao Ma169ddc32011-07-11 00:00:07 -04005122 free_count += next - start;
Lukas Czerner7360d172010-10-27 21:30:12 -04005123 start = next + 1;
5124
5125 if (fatal_signal_pending(current)) {
5126 count = -ERESTARTSYS;
5127 break;
5128 }
5129
5130 if (need_resched()) {
5131 ext4_unlock_group(sb, group);
5132 cond_resched();
5133 ext4_lock_group(sb, group);
5134 }
5135
Tao Ma169ddc32011-07-11 00:00:07 -04005136 if ((e4b.bd_info->bb_free - free_count) < minblocks)
Lukas Czerner7360d172010-10-27 21:30:12 -04005137 break;
5138 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005139
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005140 if (!ret) {
5141 ret = count;
Tao Ma3d56b8d2011-07-11 00:03:38 -04005142 EXT4_MB_GRP_SET_TRIMMED(e4b.bd_info);
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005143 }
Tao Ma3d56b8d2011-07-11 00:03:38 -04005144out:
Lukas Czerner7360d172010-10-27 21:30:12 -04005145 ext4_unlock_group(sb, group);
Lukas Czerner78944082011-05-24 18:16:27 -04005146 ext4_mb_unload_buddy(&e4b);
Lukas Czerner7360d172010-10-27 21:30:12 -04005147
5148 ext4_debug("trimmed %d blocks in the group %d\n",
5149 count, group);
5150
Lukas Czernerd71c1ae2012-11-08 14:04:52 -05005151 return ret;
Lukas Czerner7360d172010-10-27 21:30:12 -04005152}
5153
5154/**
5155 * ext4_trim_fs() -- trim ioctl handle function
5156 * @sb: superblock for filesystem
5157 * @range: fstrim_range structure
JP Abgrallb7b01892014-07-23 16:55:07 -07005158 * @blkdev_flags: flags for the block device
Lukas Czerner7360d172010-10-27 21:30:12 -04005159 *
5160 * start: First Byte to trim
5161 * len: number of Bytes to trim from start
5162 * minlen: minimum extent length in Bytes
5163 * ext4_trim_fs goes through all allocation groups containing Bytes from
5164 * start to start+len. For each such a group ext4_trim_all_free function
5165 * is invoked to trim all free space.
5166 */
JP Abgrallb7b01892014-07-23 16:55:07 -07005167int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range,
5168 unsigned long blkdev_flags)
Lukas Czerner7360d172010-10-27 21:30:12 -04005169{
Lukas Czerner78944082011-05-24 18:16:27 -04005170 struct ext4_group_info *grp;
Lukas Czerner913eed832012-03-21 21:22:22 -04005171 ext4_group_t group, first_group, last_group;
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005172 ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
Lukas Czerner913eed832012-03-21 21:22:22 -04005173 uint64_t start, end, minlen, trimmed = 0;
Jan Kara0f0a25b2011-01-11 15:16:31 -05005174 ext4_fsblk_t first_data_blk =
5175 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
Lukas Czerner913eed832012-03-21 21:22:22 -04005176 ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
Lukas Czerner7360d172010-10-27 21:30:12 -04005177 int ret = 0;
5178
5179 start = range->start >> sb->s_blocksize_bits;
Lukas Czerner913eed832012-03-21 21:22:22 -04005180 end = start + (range->len >> sb->s_blocksize_bits) - 1;
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005181 minlen = EXT4_NUM_B2C(EXT4_SB(sb),
5182 range->minlen >> sb->s_blocksize_bits);
Lukas Czerner7360d172010-10-27 21:30:12 -04005183
Lukas Czerner5de35e82012-10-22 18:01:19 -04005184 if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
5185 start >= max_blks ||
5186 range->len < sb->s_blocksize)
Lukas Czerner7360d172010-10-27 21:30:12 -04005187 return -EINVAL;
Lukas Czerner913eed832012-03-21 21:22:22 -04005188 if (end >= max_blks)
5189 end = max_blks - 1;
5190 if (end <= first_data_blk)
Tao Ma22f10452011-07-10 23:52:37 -04005191 goto out;
Lukas Czerner913eed832012-03-21 21:22:22 -04005192 if (start < first_data_blk)
Jan Kara0f0a25b2011-01-11 15:16:31 -05005193 start = first_data_blk;
Lukas Czerner7360d172010-10-27 21:30:12 -04005194
Lukas Czerner913eed832012-03-21 21:22:22 -04005195 /* Determine first and last group to examine based on start and end */
Lukas Czerner7360d172010-10-27 21:30:12 -04005196 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005197 &first_group, &first_cluster);
Lukas Czerner913eed832012-03-21 21:22:22 -04005198 ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005199 &last_group, &last_cluster);
Lukas Czerner7360d172010-10-27 21:30:12 -04005200
Lukas Czerner913eed832012-03-21 21:22:22 -04005201 /* end now represents the last cluster to discard in this group */
5202 end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
Lukas Czerner7360d172010-10-27 21:30:12 -04005203
5204 for (group = first_group; group <= last_group; group++) {
Lukas Czerner78944082011-05-24 18:16:27 -04005205 grp = ext4_get_group_info(sb, group);
5206 /* We only do this if the grp has never been initialized */
5207 if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
5208 ret = ext4_mb_init_group(sb, group);
5209 if (ret)
5210 break;
Lukas Czerner7360d172010-10-27 21:30:12 -04005211 }
5212
Tao Ma0ba08512011-03-23 15:48:11 -04005213 /*
Lukas Czerner913eed832012-03-21 21:22:22 -04005214 * For all the groups except the last one, last cluster will
5215 * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
5216 * change it for the last group, note that last_cluster is
5217 * already computed earlier by ext4_get_group_no_and_offset()
Tao Ma0ba08512011-03-23 15:48:11 -04005218 */
Lukas Czerner913eed832012-03-21 21:22:22 -04005219 if (group == last_group)
5220 end = last_cluster;
Lukas Czerner7360d172010-10-27 21:30:12 -04005221
Lukas Czerner78944082011-05-24 18:16:27 -04005222 if (grp->bb_free >= minlen) {
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005223 cnt = ext4_trim_all_free(sb, group, first_cluster,
JP Abgrallb7b01892014-07-23 16:55:07 -07005224 end, minlen, blkdev_flags);
Lukas Czerner7360d172010-10-27 21:30:12 -04005225 if (cnt < 0) {
5226 ret = cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005227 break;
5228 }
Lukas Czerner21e7fd22012-03-21 21:24:22 -04005229 trimmed += cnt;
Lukas Czerner7360d172010-10-27 21:30:12 -04005230 }
Lukas Czerner913eed832012-03-21 21:22:22 -04005231
5232 /*
5233 * For every group except the first one, we are sure
5234 * that the first cluster to discard will be cluster #0.
5235 */
Theodore Ts'o7137d7a2011-09-09 18:38:51 -04005236 first_cluster = 0;
Lukas Czerner7360d172010-10-27 21:30:12 -04005237 }
Lukas Czerner7360d172010-10-27 21:30:12 -04005238
Tao Ma3d56b8d2011-07-11 00:03:38 -04005239 if (!ret)
5240 atomic_set(&EXT4_SB(sb)->s_last_trim_minblks, minlen);
5241
Tao Ma22f10452011-07-10 23:52:37 -04005242out:
Lukas Czerneraaf7d732012-09-26 22:21:21 -04005243 range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
Lukas Czerner7360d172010-10-27 21:30:12 -04005244 return ret;
5245}