xref: /openbmc/linux/fs/ext4/mballoc.c (revision 9f24e420)
1c9de560dSAlex Tomas /*
2c9de560dSAlex Tomas  * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3c9de560dSAlex Tomas  * Written by Alex Tomas <alex@clusterfs.com>
4c9de560dSAlex Tomas  *
5c9de560dSAlex Tomas  * This program is free software; you can redistribute it and/or modify
6c9de560dSAlex Tomas  * it under the terms of the GNU General Public License version 2 as
7c9de560dSAlex Tomas  * published by the Free Software Foundation.
8c9de560dSAlex Tomas  *
9c9de560dSAlex Tomas  * This program is distributed in the hope that it will be useful,
10c9de560dSAlex Tomas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11c9de560dSAlex Tomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12c9de560dSAlex Tomas  * GNU General Public License for more details.
13c9de560dSAlex Tomas  *
14c9de560dSAlex Tomas  * You should have received a copy of the GNU General Public Licens
15c9de560dSAlex Tomas  * along with this program; if not, write to the Free Software
16c9de560dSAlex Tomas  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
17c9de560dSAlex Tomas  */
18c9de560dSAlex Tomas 
19c9de560dSAlex Tomas 
20c9de560dSAlex Tomas /*
21c9de560dSAlex Tomas  * mballoc.c contains the multiblocks allocation routines
22c9de560dSAlex Tomas  */
23c9de560dSAlex Tomas 
248f6e39a7SMingming Cao #include "mballoc.h"
25c9de560dSAlex Tomas /*
26c9de560dSAlex Tomas  * MUSTDO:
27c9de560dSAlex Tomas  *   - test ext4_ext_search_left() and ext4_ext_search_right()
28c9de560dSAlex Tomas  *   - search for metadata in few groups
29c9de560dSAlex Tomas  *
30c9de560dSAlex Tomas  * TODO v4:
31c9de560dSAlex Tomas  *   - normalization should take into account whether file is still open
32c9de560dSAlex Tomas  *   - discard preallocations if no free space left (policy?)
33c9de560dSAlex Tomas  *   - don't normalize tails
34c9de560dSAlex Tomas  *   - quota
35c9de560dSAlex Tomas  *   - reservation for superuser
36c9de560dSAlex Tomas  *
37c9de560dSAlex Tomas  * TODO v3:
38c9de560dSAlex Tomas  *   - bitmap read-ahead (proposed by Oleg Drokin aka green)
39c9de560dSAlex Tomas  *   - track min/max extents in each group for better group selection
40c9de560dSAlex Tomas  *   - mb_mark_used() may allocate chunk right after splitting buddy
41c9de560dSAlex Tomas  *   - tree of groups sorted by number of free blocks
42c9de560dSAlex Tomas  *   - error handling
43c9de560dSAlex Tomas  */
44c9de560dSAlex Tomas 
45c9de560dSAlex Tomas /*
46c9de560dSAlex Tomas  * The allocation request involve request for multiple number of blocks
47c9de560dSAlex Tomas  * near to the goal(block) value specified.
48c9de560dSAlex Tomas  *
49b713a5ecSTheodore Ts'o  * During initialization phase of the allocator we decide to use the
50b713a5ecSTheodore Ts'o  * group preallocation or inode preallocation depending on the size of
51b713a5ecSTheodore Ts'o  * the file. The size of the file could be the resulting file size we
52b713a5ecSTheodore Ts'o  * would have after allocation, or the current file size, which ever
53b713a5ecSTheodore Ts'o  * is larger. If the size is less than sbi->s_mb_stream_request we
54b713a5ecSTheodore Ts'o  * select to use the group preallocation. The default value of
55b713a5ecSTheodore Ts'o  * s_mb_stream_request is 16 blocks. This can also be tuned via
56b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
57b713a5ecSTheodore Ts'o  * terms of number of blocks.
58c9de560dSAlex Tomas  *
59c9de560dSAlex Tomas  * The main motivation for having small file use group preallocation is to
60b713a5ecSTheodore Ts'o  * ensure that we have small files closer together on the disk.
61c9de560dSAlex Tomas  *
62b713a5ecSTheodore Ts'o  * First stage the allocator looks at the inode prealloc list,
63b713a5ecSTheodore Ts'o  * ext4_inode_info->i_prealloc_list, which contains list of prealloc
64b713a5ecSTheodore Ts'o  * spaces for this particular inode. The inode prealloc space is
65b713a5ecSTheodore Ts'o  * represented as:
66c9de560dSAlex Tomas  *
67c9de560dSAlex Tomas  * pa_lstart -> the logical start block for this prealloc space
68c9de560dSAlex Tomas  * pa_pstart -> the physical start block for this prealloc space
69c9de560dSAlex Tomas  * pa_len    -> lenght for this prealloc space
70c9de560dSAlex Tomas  * pa_free   ->  free space available in this prealloc space
71c9de560dSAlex Tomas  *
72c9de560dSAlex Tomas  * The inode preallocation space is used looking at the _logical_ start
73c9de560dSAlex Tomas  * block. If only the logical file block falls within the range of prealloc
74c9de560dSAlex Tomas  * space we will consume the particular prealloc space. This make sure that
75c9de560dSAlex Tomas  * that the we have contiguous physical blocks representing the file blocks
76c9de560dSAlex Tomas  *
77c9de560dSAlex Tomas  * The important thing to be noted in case of inode prealloc space is that
78c9de560dSAlex Tomas  * we don't modify the values associated to inode prealloc space except
79c9de560dSAlex Tomas  * pa_free.
80c9de560dSAlex Tomas  *
81c9de560dSAlex Tomas  * If we are not able to find blocks in the inode prealloc space and if we
82c9de560dSAlex Tomas  * have the group allocation flag set then we look at the locality group
83c9de560dSAlex Tomas  * prealloc space. These are per CPU prealloc list repreasented as
84c9de560dSAlex Tomas  *
85c9de560dSAlex Tomas  * ext4_sb_info.s_locality_groups[smp_processor_id()]
86c9de560dSAlex Tomas  *
87c9de560dSAlex Tomas  * The reason for having a per cpu locality group is to reduce the contention
88c9de560dSAlex Tomas  * between CPUs. It is possible to get scheduled at this point.
89c9de560dSAlex Tomas  *
90c9de560dSAlex Tomas  * The locality group prealloc space is used looking at whether we have
91c9de560dSAlex Tomas  * enough free space (pa_free) withing the prealloc space.
92c9de560dSAlex Tomas  *
93c9de560dSAlex Tomas  * If we can't allocate blocks via inode prealloc or/and locality group
94c9de560dSAlex Tomas  * prealloc then we look at the buddy cache. The buddy cache is represented
95c9de560dSAlex Tomas  * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
96c9de560dSAlex Tomas  * mapped to the buddy and bitmap information regarding different
97c9de560dSAlex Tomas  * groups. The buddy information is attached to buddy cache inode so that
98c9de560dSAlex Tomas  * we can access them through the page cache. The information regarding
99c9de560dSAlex Tomas  * each group is loaded via ext4_mb_load_buddy.  The information involve
100c9de560dSAlex Tomas  * block bitmap and buddy information. The information are stored in the
101c9de560dSAlex Tomas  * inode as:
102c9de560dSAlex Tomas  *
103c9de560dSAlex Tomas  *  {                        page                        }
104c3a326a6SAneesh Kumar K.V  *  [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
105c9de560dSAlex Tomas  *
106c9de560dSAlex Tomas  *
107c9de560dSAlex Tomas  * one block each for bitmap and buddy information.  So for each group we
108c9de560dSAlex Tomas  * take up 2 blocks. A page can contain blocks_per_page (PAGE_CACHE_SIZE /
109c9de560dSAlex Tomas  * blocksize) blocks.  So it can have information regarding groups_per_page
110c9de560dSAlex Tomas  * which is blocks_per_page/2
111c9de560dSAlex Tomas  *
112c9de560dSAlex Tomas  * The buddy cache inode is not stored on disk. The inode is thrown
113c9de560dSAlex Tomas  * away when the filesystem is unmounted.
114c9de560dSAlex Tomas  *
115c9de560dSAlex Tomas  * We look for count number of blocks in the buddy cache. If we were able
116c9de560dSAlex Tomas  * to locate that many free blocks we return with additional information
117c9de560dSAlex Tomas  * regarding rest of the contiguous physical block available
118c9de560dSAlex Tomas  *
119c9de560dSAlex Tomas  * Before allocating blocks via buddy cache we normalize the request
120c9de560dSAlex Tomas  * blocks. This ensure we ask for more blocks that we needed. The extra
121c9de560dSAlex Tomas  * blocks that we get after allocation is added to the respective prealloc
122c9de560dSAlex Tomas  * list. In case of inode preallocation we follow a list of heuristics
123c9de560dSAlex Tomas  * based on file size. This can be found in ext4_mb_normalize_request. If
124c9de560dSAlex Tomas  * we are doing a group prealloc we try to normalize the request to
125b713a5ecSTheodore Ts'o  * sbi->s_mb_group_prealloc. Default value of s_mb_group_prealloc is
126c9de560dSAlex Tomas  * 512 blocks. This can be tuned via
127b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition/mb_group_prealloc. The value is represented in
128c9de560dSAlex Tomas  * terms of number of blocks. If we have mounted the file system with -O
129c9de560dSAlex Tomas  * stripe=<value> option the group prealloc request is normalized to the
130c9de560dSAlex Tomas  * stripe value (sbi->s_stripe)
131c9de560dSAlex Tomas  *
132b713a5ecSTheodore Ts'o  * The regular allocator(using the buddy cache) supports few tunables.
133c9de560dSAlex Tomas  *
134b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_min_to_scan
135b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_max_to_scan
136b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_order2_req
137c9de560dSAlex Tomas  *
138b713a5ecSTheodore Ts'o  * The regular allocator uses buddy scan only if the request len is power of
139c9de560dSAlex Tomas  * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
140c9de560dSAlex Tomas  * value of s_mb_order2_reqs can be tuned via
141b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_order2_req.  If the request len is equal to
142c9de560dSAlex Tomas  * stripe size (sbi->s_stripe), we try to search for contigous block in
143b713a5ecSTheodore Ts'o  * stripe size. This should result in better allocation on RAID setups. If
144b713a5ecSTheodore Ts'o  * not, we search in the specific group using bitmap for best extents. The
145b713a5ecSTheodore Ts'o  * tunable min_to_scan and max_to_scan control the behaviour here.
146c9de560dSAlex Tomas  * min_to_scan indicate how long the mballoc __must__ look for a best
147b713a5ecSTheodore Ts'o  * extent and max_to_scan indicates how long the mballoc __can__ look for a
148c9de560dSAlex Tomas  * best extent in the found extents. Searching for the blocks starts with
149c9de560dSAlex Tomas  * the group specified as the goal value in allocation context via
150c9de560dSAlex Tomas  * ac_g_ex. Each group is first checked based on the criteria whether it
151c9de560dSAlex Tomas  * can used for allocation. ext4_mb_good_group explains how the groups are
152c9de560dSAlex Tomas  * checked.
153c9de560dSAlex Tomas  *
154c9de560dSAlex Tomas  * Both the prealloc space are getting populated as above. So for the first
155c9de560dSAlex Tomas  * request we will hit the buddy cache which will result in this prealloc
156c9de560dSAlex Tomas  * space getting filled. The prealloc space is then later used for the
157c9de560dSAlex Tomas  * subsequent request.
158c9de560dSAlex Tomas  */
159c9de560dSAlex Tomas 
160c9de560dSAlex Tomas /*
161c9de560dSAlex Tomas  * mballoc operates on the following data:
162c9de560dSAlex Tomas  *  - on-disk bitmap
163c9de560dSAlex Tomas  *  - in-core buddy (actually includes buddy and bitmap)
164c9de560dSAlex Tomas  *  - preallocation descriptors (PAs)
165c9de560dSAlex Tomas  *
166c9de560dSAlex Tomas  * there are two types of preallocations:
167c9de560dSAlex Tomas  *  - inode
168c9de560dSAlex Tomas  *    assiged to specific inode and can be used for this inode only.
169c9de560dSAlex Tomas  *    it describes part of inode's space preallocated to specific
170c9de560dSAlex Tomas  *    physical blocks. any block from that preallocated can be used
171c9de560dSAlex Tomas  *    independent. the descriptor just tracks number of blocks left
172c9de560dSAlex Tomas  *    unused. so, before taking some block from descriptor, one must
173c9de560dSAlex Tomas  *    make sure corresponded logical block isn't allocated yet. this
174c9de560dSAlex Tomas  *    also means that freeing any block within descriptor's range
175c9de560dSAlex Tomas  *    must discard all preallocated blocks.
176c9de560dSAlex Tomas  *  - locality group
177c9de560dSAlex Tomas  *    assigned to specific locality group which does not translate to
178c9de560dSAlex Tomas  *    permanent set of inodes: inode can join and leave group. space
179c9de560dSAlex Tomas  *    from this type of preallocation can be used for any inode. thus
180c9de560dSAlex Tomas  *    it's consumed from the beginning to the end.
181c9de560dSAlex Tomas  *
182c9de560dSAlex Tomas  * relation between them can be expressed as:
183c9de560dSAlex Tomas  *    in-core buddy = on-disk bitmap + preallocation descriptors
184c9de560dSAlex Tomas  *
185c9de560dSAlex Tomas  * this mean blocks mballoc considers used are:
186c9de560dSAlex Tomas  *  - allocated blocks (persistent)
187c9de560dSAlex Tomas  *  - preallocated blocks (non-persistent)
188c9de560dSAlex Tomas  *
189c9de560dSAlex Tomas  * consistency in mballoc world means that at any time a block is either
190c9de560dSAlex Tomas  * free or used in ALL structures. notice: "any time" should not be read
191c9de560dSAlex Tomas  * literally -- time is discrete and delimited by locks.
192c9de560dSAlex Tomas  *
193c9de560dSAlex Tomas  *  to keep it simple, we don't use block numbers, instead we count number of
194c9de560dSAlex Tomas  *  blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
195c9de560dSAlex Tomas  *
196c9de560dSAlex Tomas  * all operations can be expressed as:
197c9de560dSAlex Tomas  *  - init buddy:			buddy = on-disk + PAs
198c9de560dSAlex Tomas  *  - new PA:				buddy += N; PA = N
199c9de560dSAlex Tomas  *  - use inode PA:			on-disk += N; PA -= N
200c9de560dSAlex Tomas  *  - discard inode PA			buddy -= on-disk - PA; PA = 0
201c9de560dSAlex Tomas  *  - use locality group PA		on-disk += N; PA -= N
202c9de560dSAlex Tomas  *  - discard locality group PA		buddy -= PA; PA = 0
203c9de560dSAlex Tomas  *  note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
204c9de560dSAlex Tomas  *        is used in real operation because we can't know actual used
205c9de560dSAlex Tomas  *        bits from PA, only from on-disk bitmap
206c9de560dSAlex Tomas  *
207c9de560dSAlex Tomas  * if we follow this strict logic, then all operations above should be atomic.
208c9de560dSAlex Tomas  * given some of them can block, we'd have to use something like semaphores
209c9de560dSAlex Tomas  * killing performance on high-end SMP hardware. let's try to relax it using
210c9de560dSAlex Tomas  * the following knowledge:
211c9de560dSAlex Tomas  *  1) if buddy is referenced, it's already initialized
212c9de560dSAlex Tomas  *  2) while block is used in buddy and the buddy is referenced,
213c9de560dSAlex Tomas  *     nobody can re-allocate that block
214c9de560dSAlex Tomas  *  3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
215c9de560dSAlex Tomas  *     bit set and PA claims same block, it's OK. IOW, one can set bit in
216c9de560dSAlex Tomas  *     on-disk bitmap if buddy has same bit set or/and PA covers corresponded
217c9de560dSAlex Tomas  *     block
218c9de560dSAlex Tomas  *
219c9de560dSAlex Tomas  * so, now we're building a concurrency table:
220c9de560dSAlex Tomas  *  - init buddy vs.
221c9de560dSAlex Tomas  *    - new PA
222c9de560dSAlex Tomas  *      blocks for PA are allocated in the buddy, buddy must be referenced
223c9de560dSAlex Tomas  *      until PA is linked to allocation group to avoid concurrent buddy init
224c9de560dSAlex Tomas  *    - use inode PA
225c9de560dSAlex Tomas  *      we need to make sure that either on-disk bitmap or PA has uptodate data
226c9de560dSAlex Tomas  *      given (3) we care that PA-=N operation doesn't interfere with init
227c9de560dSAlex Tomas  *    - discard inode PA
228c9de560dSAlex Tomas  *      the simplest way would be to have buddy initialized by the discard
229c9de560dSAlex Tomas  *    - use locality group PA
230c9de560dSAlex Tomas  *      again PA-=N must be serialized with init
231c9de560dSAlex Tomas  *    - discard locality group PA
232c9de560dSAlex Tomas  *      the simplest way would be to have buddy initialized by the discard
233c9de560dSAlex Tomas  *  - new PA vs.
234c9de560dSAlex Tomas  *    - use inode PA
235c9de560dSAlex Tomas  *      i_data_sem serializes them
236c9de560dSAlex Tomas  *    - discard inode PA
237c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
238c9de560dSAlex Tomas  *    - use locality group PA
239c9de560dSAlex Tomas  *      some mutex should serialize them
240c9de560dSAlex Tomas  *    - discard locality group PA
241c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
242c9de560dSAlex Tomas  *  - use inode PA
243c9de560dSAlex Tomas  *    - use inode PA
244c9de560dSAlex Tomas  *      i_data_sem or another mutex should serializes them
245c9de560dSAlex Tomas  *    - discard inode PA
246c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
247c9de560dSAlex Tomas  *    - use locality group PA
248c9de560dSAlex Tomas  *      nothing wrong here -- they're different PAs covering different blocks
249c9de560dSAlex Tomas  *    - discard locality group PA
250c9de560dSAlex Tomas  *      discard process must wait until PA isn't used by another process
251c9de560dSAlex Tomas  *
252c9de560dSAlex Tomas  * now we're ready to make few consequences:
253c9de560dSAlex Tomas  *  - PA is referenced and while it is no discard is possible
254c9de560dSAlex Tomas  *  - PA is referenced until block isn't marked in on-disk bitmap
255c9de560dSAlex Tomas  *  - PA changes only after on-disk bitmap
256c9de560dSAlex Tomas  *  - discard must not compete with init. either init is done before
257c9de560dSAlex Tomas  *    any discard or they're serialized somehow
258c9de560dSAlex Tomas  *  - buddy init as sum of on-disk bitmap and PAs is done atomically
259c9de560dSAlex Tomas  *
260c9de560dSAlex Tomas  * a special case when we've used PA to emptiness. no need to modify buddy
261c9de560dSAlex Tomas  * in this case, but we should care about concurrent init
262c9de560dSAlex Tomas  *
263c9de560dSAlex Tomas  */
264c9de560dSAlex Tomas 
265c9de560dSAlex Tomas  /*
266c9de560dSAlex Tomas  * Logic in few words:
267c9de560dSAlex Tomas  *
268c9de560dSAlex Tomas  *  - allocation:
269c9de560dSAlex Tomas  *    load group
270c9de560dSAlex Tomas  *    find blocks
271c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
272c9de560dSAlex Tomas  *    release group
273c9de560dSAlex Tomas  *
274c9de560dSAlex Tomas  *  - use preallocation:
275c9de560dSAlex Tomas  *    find proper PA (per-inode or group)
276c9de560dSAlex Tomas  *    load group
277c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
278c9de560dSAlex Tomas  *    release group
279c9de560dSAlex Tomas  *    release PA
280c9de560dSAlex Tomas  *
281c9de560dSAlex Tomas  *  - free:
282c9de560dSAlex Tomas  *    load group
283c9de560dSAlex Tomas  *    mark bits in on-disk bitmap
284c9de560dSAlex Tomas  *    release group
285c9de560dSAlex Tomas  *
286c9de560dSAlex Tomas  *  - discard preallocations in group:
287c9de560dSAlex Tomas  *    mark PAs deleted
288c9de560dSAlex Tomas  *    move them onto local list
289c9de560dSAlex Tomas  *    load on-disk bitmap
290c9de560dSAlex Tomas  *    load group
291c9de560dSAlex Tomas  *    remove PA from object (inode or locality group)
292c9de560dSAlex Tomas  *    mark free blocks in-core
293c9de560dSAlex Tomas  *
294c9de560dSAlex Tomas  *  - discard inode's preallocations:
295c9de560dSAlex Tomas  */
296c9de560dSAlex Tomas 
297c9de560dSAlex Tomas /*
298c9de560dSAlex Tomas  * Locking rules
299c9de560dSAlex Tomas  *
300c9de560dSAlex Tomas  * Locks:
301c9de560dSAlex Tomas  *  - bitlock on a group	(group)
302c9de560dSAlex Tomas  *  - object (inode/locality)	(object)
303c9de560dSAlex Tomas  *  - per-pa lock		(pa)
304c9de560dSAlex Tomas  *
305c9de560dSAlex Tomas  * Paths:
306c9de560dSAlex Tomas  *  - new pa
307c9de560dSAlex Tomas  *    object
308c9de560dSAlex Tomas  *    group
309c9de560dSAlex Tomas  *
310c9de560dSAlex Tomas  *  - find and use pa:
311c9de560dSAlex Tomas  *    pa
312c9de560dSAlex Tomas  *
313c9de560dSAlex Tomas  *  - release consumed pa:
314c9de560dSAlex Tomas  *    pa
315c9de560dSAlex Tomas  *    group
316c9de560dSAlex Tomas  *    object
317c9de560dSAlex Tomas  *
318c9de560dSAlex Tomas  *  - generate in-core bitmap:
319c9de560dSAlex Tomas  *    group
320c9de560dSAlex Tomas  *        pa
321c9de560dSAlex Tomas  *
322c9de560dSAlex Tomas  *  - discard all for given object (inode, locality group):
323c9de560dSAlex Tomas  *    object
324c9de560dSAlex Tomas  *        pa
325c9de560dSAlex Tomas  *    group
326c9de560dSAlex Tomas  *
327c9de560dSAlex Tomas  *  - discard all for given group:
328c9de560dSAlex Tomas  *    group
329c9de560dSAlex Tomas  *        pa
330c9de560dSAlex Tomas  *    group
331c9de560dSAlex Tomas  *        object
332c9de560dSAlex Tomas  *
333c9de560dSAlex Tomas  */
334c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_pspace_cachep;
335c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_ac_cachep;
336c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_free_ext_cachep;
337c3a326a6SAneesh Kumar K.V static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
338c3a326a6SAneesh Kumar K.V 					ext4_group_t group);
3397a2fcbf7SAneesh Kumar K.V static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
3407a2fcbf7SAneesh Kumar K.V 						ext4_group_t group);
341c3a326a6SAneesh Kumar K.V static void release_blocks_on_commit(journal_t *journal, transaction_t *txn);
342c3a326a6SAneesh Kumar K.V 
343c3a326a6SAneesh Kumar K.V 
344c9de560dSAlex Tomas 
345ffad0a44SAneesh Kumar K.V static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
346ffad0a44SAneesh Kumar K.V {
347c9de560dSAlex Tomas #if BITS_PER_LONG == 64
348ffad0a44SAneesh Kumar K.V 	*bit += ((unsigned long) addr & 7UL) << 3;
349ffad0a44SAneesh Kumar K.V 	addr = (void *) ((unsigned long) addr & ~7UL);
350c9de560dSAlex Tomas #elif BITS_PER_LONG == 32
351ffad0a44SAneesh Kumar K.V 	*bit += ((unsigned long) addr & 3UL) << 3;
352ffad0a44SAneesh Kumar K.V 	addr = (void *) ((unsigned long) addr & ~3UL);
353c9de560dSAlex Tomas #else
354c9de560dSAlex Tomas #error "how many bits you are?!"
355c9de560dSAlex Tomas #endif
356ffad0a44SAneesh Kumar K.V 	return addr;
357ffad0a44SAneesh Kumar K.V }
358c9de560dSAlex Tomas 
359c9de560dSAlex Tomas static inline int mb_test_bit(int bit, void *addr)
360c9de560dSAlex Tomas {
361c9de560dSAlex Tomas 	/*
362c9de560dSAlex Tomas 	 * ext4_test_bit on architecture like powerpc
363c9de560dSAlex Tomas 	 * needs unsigned long aligned address
364c9de560dSAlex Tomas 	 */
365ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
366c9de560dSAlex Tomas 	return ext4_test_bit(bit, addr);
367c9de560dSAlex Tomas }
368c9de560dSAlex Tomas 
369c9de560dSAlex Tomas static inline void mb_set_bit(int bit, void *addr)
370c9de560dSAlex Tomas {
371ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
372c9de560dSAlex Tomas 	ext4_set_bit(bit, addr);
373c9de560dSAlex Tomas }
374c9de560dSAlex Tomas 
375c9de560dSAlex Tomas static inline void mb_set_bit_atomic(spinlock_t *lock, int bit, void *addr)
376c9de560dSAlex Tomas {
377ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
378c9de560dSAlex Tomas 	ext4_set_bit_atomic(lock, bit, addr);
379c9de560dSAlex Tomas }
380c9de560dSAlex Tomas 
381c9de560dSAlex Tomas static inline void mb_clear_bit(int bit, void *addr)
382c9de560dSAlex Tomas {
383ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
384c9de560dSAlex Tomas 	ext4_clear_bit(bit, addr);
385c9de560dSAlex Tomas }
386c9de560dSAlex Tomas 
387c9de560dSAlex Tomas static inline void mb_clear_bit_atomic(spinlock_t *lock, int bit, void *addr)
388c9de560dSAlex Tomas {
389ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&bit, addr);
390c9de560dSAlex Tomas 	ext4_clear_bit_atomic(lock, bit, addr);
391c9de560dSAlex Tomas }
392c9de560dSAlex Tomas 
393ffad0a44SAneesh Kumar K.V static inline int mb_find_next_zero_bit(void *addr, int max, int start)
394ffad0a44SAneesh Kumar K.V {
395e7dfb246SAneesh Kumar K.V 	int fix = 0, ret, tmpmax;
396ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&fix, addr);
397e7dfb246SAneesh Kumar K.V 	tmpmax = max + fix;
398ffad0a44SAneesh Kumar K.V 	start += fix;
399ffad0a44SAneesh Kumar K.V 
400e7dfb246SAneesh Kumar K.V 	ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
401e7dfb246SAneesh Kumar K.V 	if (ret > max)
402e7dfb246SAneesh Kumar K.V 		return max;
403e7dfb246SAneesh Kumar K.V 	return ret;
404ffad0a44SAneesh Kumar K.V }
405ffad0a44SAneesh Kumar K.V 
406ffad0a44SAneesh Kumar K.V static inline int mb_find_next_bit(void *addr, int max, int start)
407ffad0a44SAneesh Kumar K.V {
408e7dfb246SAneesh Kumar K.V 	int fix = 0, ret, tmpmax;
409ffad0a44SAneesh Kumar K.V 	addr = mb_correct_addr_and_bit(&fix, addr);
410e7dfb246SAneesh Kumar K.V 	tmpmax = max + fix;
411ffad0a44SAneesh Kumar K.V 	start += fix;
412ffad0a44SAneesh Kumar K.V 
413e7dfb246SAneesh Kumar K.V 	ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
414e7dfb246SAneesh Kumar K.V 	if (ret > max)
415e7dfb246SAneesh Kumar K.V 		return max;
416e7dfb246SAneesh Kumar K.V 	return ret;
417ffad0a44SAneesh Kumar K.V }
418ffad0a44SAneesh Kumar K.V 
419c9de560dSAlex Tomas static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
420c9de560dSAlex Tomas {
421c9de560dSAlex Tomas 	char *bb;
422c9de560dSAlex Tomas 
423c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
424c9de560dSAlex Tomas 	BUG_ON(max == NULL);
425c9de560dSAlex Tomas 
426c9de560dSAlex Tomas 	if (order > e4b->bd_blkbits + 1) {
427c9de560dSAlex Tomas 		*max = 0;
428c9de560dSAlex Tomas 		return NULL;
429c9de560dSAlex Tomas 	}
430c9de560dSAlex Tomas 
431c9de560dSAlex Tomas 	/* at order 0 we see each particular block */
432c9de560dSAlex Tomas 	*max = 1 << (e4b->bd_blkbits + 3);
433c9de560dSAlex Tomas 	if (order == 0)
434c9de560dSAlex Tomas 		return EXT4_MB_BITMAP(e4b);
435c9de560dSAlex Tomas 
436c9de560dSAlex Tomas 	bb = EXT4_MB_BUDDY(e4b) + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
437c9de560dSAlex Tomas 	*max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
438c9de560dSAlex Tomas 
439c9de560dSAlex Tomas 	return bb;
440c9de560dSAlex Tomas }
441c9de560dSAlex Tomas 
442c9de560dSAlex Tomas #ifdef DOUBLE_CHECK
443c9de560dSAlex Tomas static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
444c9de560dSAlex Tomas 			   int first, int count)
445c9de560dSAlex Tomas {
446c9de560dSAlex Tomas 	int i;
447c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
448c9de560dSAlex Tomas 
449c9de560dSAlex Tomas 	if (unlikely(e4b->bd_info->bb_bitmap == NULL))
450c9de560dSAlex Tomas 		return;
451c9de560dSAlex Tomas 	BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
452c9de560dSAlex Tomas 	for (i = 0; i < count; i++) {
453c9de560dSAlex Tomas 		if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
454c9de560dSAlex Tomas 			ext4_fsblk_t blocknr;
455c9de560dSAlex Tomas 			blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
456c9de560dSAlex Tomas 			blocknr += first + i;
457c9de560dSAlex Tomas 			blocknr +=
458c9de560dSAlex Tomas 			    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
4595d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
4605d1b1b3fSAneesh Kumar K.V 				   __func__, "double-free of inode"
461a9df9a49STheodore Ts'o 				   " %lu's block %llu(bit %u in group %u)",
462c9de560dSAlex Tomas 				   inode ? inode->i_ino : 0, blocknr,
463c9de560dSAlex Tomas 				   first + i, e4b->bd_group);
464c9de560dSAlex Tomas 		}
465c9de560dSAlex Tomas 		mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
466c9de560dSAlex Tomas 	}
467c9de560dSAlex Tomas }
468c9de560dSAlex Tomas 
469c9de560dSAlex Tomas static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
470c9de560dSAlex Tomas {
471c9de560dSAlex Tomas 	int i;
472c9de560dSAlex Tomas 
473c9de560dSAlex Tomas 	if (unlikely(e4b->bd_info->bb_bitmap == NULL))
474c9de560dSAlex Tomas 		return;
475c9de560dSAlex Tomas 	BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
476c9de560dSAlex Tomas 	for (i = 0; i < count; i++) {
477c9de560dSAlex Tomas 		BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
478c9de560dSAlex Tomas 		mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
479c9de560dSAlex Tomas 	}
480c9de560dSAlex Tomas }
481c9de560dSAlex Tomas 
482c9de560dSAlex Tomas static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
483c9de560dSAlex Tomas {
484c9de560dSAlex Tomas 	if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
485c9de560dSAlex Tomas 		unsigned char *b1, *b2;
486c9de560dSAlex Tomas 		int i;
487c9de560dSAlex Tomas 		b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
488c9de560dSAlex Tomas 		b2 = (unsigned char *) bitmap;
489c9de560dSAlex Tomas 		for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
490c9de560dSAlex Tomas 			if (b1[i] != b2[i]) {
491a9df9a49STheodore Ts'o 				printk(KERN_ERR "corruption in group %u "
4924776004fSTheodore Ts'o 				       "at byte %u(%u): %x in copy != %x "
4934776004fSTheodore Ts'o 				       "on disk/prealloc\n",
494c9de560dSAlex Tomas 				       e4b->bd_group, i, i * 8, b1[i], b2[i]);
495c9de560dSAlex Tomas 				BUG();
496c9de560dSAlex Tomas 			}
497c9de560dSAlex Tomas 		}
498c9de560dSAlex Tomas 	}
499c9de560dSAlex Tomas }
500c9de560dSAlex Tomas 
501c9de560dSAlex Tomas #else
502c9de560dSAlex Tomas static inline void mb_free_blocks_double(struct inode *inode,
503c9de560dSAlex Tomas 				struct ext4_buddy *e4b, int first, int count)
504c9de560dSAlex Tomas {
505c9de560dSAlex Tomas 	return;
506c9de560dSAlex Tomas }
507c9de560dSAlex Tomas static inline void mb_mark_used_double(struct ext4_buddy *e4b,
508c9de560dSAlex Tomas 						int first, int count)
509c9de560dSAlex Tomas {
510c9de560dSAlex Tomas 	return;
511c9de560dSAlex Tomas }
512c9de560dSAlex Tomas static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
513c9de560dSAlex Tomas {
514c9de560dSAlex Tomas 	return;
515c9de560dSAlex Tomas }
516c9de560dSAlex Tomas #endif
517c9de560dSAlex Tomas 
518c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
519c9de560dSAlex Tomas 
520c9de560dSAlex Tomas #define MB_CHECK_ASSERT(assert)						\
521c9de560dSAlex Tomas do {									\
522c9de560dSAlex Tomas 	if (!(assert)) {						\
523c9de560dSAlex Tomas 		printk(KERN_EMERG					\
524c9de560dSAlex Tomas 			"Assertion failure in %s() at %s:%d: \"%s\"\n",	\
525c9de560dSAlex Tomas 			function, file, line, # assert);		\
526c9de560dSAlex Tomas 		BUG();							\
527c9de560dSAlex Tomas 	}								\
528c9de560dSAlex Tomas } while (0)
529c9de560dSAlex Tomas 
530c9de560dSAlex Tomas static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
531c9de560dSAlex Tomas 				const char *function, int line)
532c9de560dSAlex Tomas {
533c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
534c9de560dSAlex Tomas 	int order = e4b->bd_blkbits + 1;
535c9de560dSAlex Tomas 	int max;
536c9de560dSAlex Tomas 	int max2;
537c9de560dSAlex Tomas 	int i;
538c9de560dSAlex Tomas 	int j;
539c9de560dSAlex Tomas 	int k;
540c9de560dSAlex Tomas 	int count;
541c9de560dSAlex Tomas 	struct ext4_group_info *grp;
542c9de560dSAlex Tomas 	int fragments = 0;
543c9de560dSAlex Tomas 	int fstart;
544c9de560dSAlex Tomas 	struct list_head *cur;
545c9de560dSAlex Tomas 	void *buddy;
546c9de560dSAlex Tomas 	void *buddy2;
547c9de560dSAlex Tomas 
548c9de560dSAlex Tomas 	{
549c9de560dSAlex Tomas 		static int mb_check_counter;
550c9de560dSAlex Tomas 		if (mb_check_counter++ % 100 != 0)
551c9de560dSAlex Tomas 			return 0;
552c9de560dSAlex Tomas 	}
553c9de560dSAlex Tomas 
554c9de560dSAlex Tomas 	while (order > 1) {
555c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, order, &max);
556c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy);
557c9de560dSAlex Tomas 		buddy2 = mb_find_buddy(e4b, order - 1, &max2);
558c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy2);
559c9de560dSAlex Tomas 		MB_CHECK_ASSERT(buddy != buddy2);
560c9de560dSAlex Tomas 		MB_CHECK_ASSERT(max * 2 == max2);
561c9de560dSAlex Tomas 
562c9de560dSAlex Tomas 		count = 0;
563c9de560dSAlex Tomas 		for (i = 0; i < max; i++) {
564c9de560dSAlex Tomas 
565c9de560dSAlex Tomas 			if (mb_test_bit(i, buddy)) {
566c9de560dSAlex Tomas 				/* only single bit in buddy2 may be 1 */
567c9de560dSAlex Tomas 				if (!mb_test_bit(i << 1, buddy2)) {
568c9de560dSAlex Tomas 					MB_CHECK_ASSERT(
569c9de560dSAlex Tomas 						mb_test_bit((i<<1)+1, buddy2));
570c9de560dSAlex Tomas 				} else if (!mb_test_bit((i << 1) + 1, buddy2)) {
571c9de560dSAlex Tomas 					MB_CHECK_ASSERT(
572c9de560dSAlex Tomas 						mb_test_bit(i << 1, buddy2));
573c9de560dSAlex Tomas 				}
574c9de560dSAlex Tomas 				continue;
575c9de560dSAlex Tomas 			}
576c9de560dSAlex Tomas 
577c9de560dSAlex Tomas 			/* both bits in buddy2 must be 0 */
578c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
579c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
580c9de560dSAlex Tomas 
581c9de560dSAlex Tomas 			for (j = 0; j < (1 << order); j++) {
582c9de560dSAlex Tomas 				k = (i * (1 << order)) + j;
583c9de560dSAlex Tomas 				MB_CHECK_ASSERT(
584c9de560dSAlex Tomas 					!mb_test_bit(k, EXT4_MB_BITMAP(e4b)));
585c9de560dSAlex Tomas 			}
586c9de560dSAlex Tomas 			count++;
587c9de560dSAlex Tomas 		}
588c9de560dSAlex Tomas 		MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
589c9de560dSAlex Tomas 		order--;
590c9de560dSAlex Tomas 	}
591c9de560dSAlex Tomas 
592c9de560dSAlex Tomas 	fstart = -1;
593c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, 0, &max);
594c9de560dSAlex Tomas 	for (i = 0; i < max; i++) {
595c9de560dSAlex Tomas 		if (!mb_test_bit(i, buddy)) {
596c9de560dSAlex Tomas 			MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
597c9de560dSAlex Tomas 			if (fstart == -1) {
598c9de560dSAlex Tomas 				fragments++;
599c9de560dSAlex Tomas 				fstart = i;
600c9de560dSAlex Tomas 			}
601c9de560dSAlex Tomas 			continue;
602c9de560dSAlex Tomas 		}
603c9de560dSAlex Tomas 		fstart = -1;
604c9de560dSAlex Tomas 		/* check used bits only */
605c9de560dSAlex Tomas 		for (j = 0; j < e4b->bd_blkbits + 1; j++) {
606c9de560dSAlex Tomas 			buddy2 = mb_find_buddy(e4b, j, &max2);
607c9de560dSAlex Tomas 			k = i >> j;
608c9de560dSAlex Tomas 			MB_CHECK_ASSERT(k < max2);
609c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
610c9de560dSAlex Tomas 		}
611c9de560dSAlex Tomas 	}
612c9de560dSAlex Tomas 	MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
613c9de560dSAlex Tomas 	MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
614c9de560dSAlex Tomas 
615c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, e4b->bd_group);
616c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, 0, &max);
617c9de560dSAlex Tomas 	list_for_each(cur, &grp->bb_prealloc_list) {
618c9de560dSAlex Tomas 		ext4_group_t groupnr;
619c9de560dSAlex Tomas 		struct ext4_prealloc_space *pa;
62060bd63d1SSolofo Ramangalahy 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
62160bd63d1SSolofo Ramangalahy 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
622c9de560dSAlex Tomas 		MB_CHECK_ASSERT(groupnr == e4b->bd_group);
62360bd63d1SSolofo Ramangalahy 		for (i = 0; i < pa->pa_len; i++)
624c9de560dSAlex Tomas 			MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
625c9de560dSAlex Tomas 	}
626c9de560dSAlex Tomas 	return 0;
627c9de560dSAlex Tomas }
628c9de560dSAlex Tomas #undef MB_CHECK_ASSERT
629c9de560dSAlex Tomas #define mb_check_buddy(e4b) __mb_check_buddy(e4b,	\
63046e665e9SHarvey Harrison 					__FILE__, __func__, __LINE__)
631c9de560dSAlex Tomas #else
632c9de560dSAlex Tomas #define mb_check_buddy(e4b)
633c9de560dSAlex Tomas #endif
634c9de560dSAlex Tomas 
635c9de560dSAlex Tomas /* FIXME!! need more doc */
636c9de560dSAlex Tomas static void ext4_mb_mark_free_simple(struct super_block *sb,
637c9de560dSAlex Tomas 				void *buddy, unsigned first, int len,
638c9de560dSAlex Tomas 					struct ext4_group_info *grp)
639c9de560dSAlex Tomas {
640c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
641c9de560dSAlex Tomas 	unsigned short min;
642c9de560dSAlex Tomas 	unsigned short max;
643c9de560dSAlex Tomas 	unsigned short chunk;
644c9de560dSAlex Tomas 	unsigned short border;
645c9de560dSAlex Tomas 
646b73fce69SValerie Clement 	BUG_ON(len > EXT4_BLOCKS_PER_GROUP(sb));
647c9de560dSAlex Tomas 
648c9de560dSAlex Tomas 	border = 2 << sb->s_blocksize_bits;
649c9de560dSAlex Tomas 
650c9de560dSAlex Tomas 	while (len > 0) {
651c9de560dSAlex Tomas 		/* find how many blocks can be covered since this position */
652c9de560dSAlex Tomas 		max = ffs(first | border) - 1;
653c9de560dSAlex Tomas 
654c9de560dSAlex Tomas 		/* find how many blocks of power 2 we need to mark */
655c9de560dSAlex Tomas 		min = fls(len) - 1;
656c9de560dSAlex Tomas 
657c9de560dSAlex Tomas 		if (max < min)
658c9de560dSAlex Tomas 			min = max;
659c9de560dSAlex Tomas 		chunk = 1 << min;
660c9de560dSAlex Tomas 
661c9de560dSAlex Tomas 		/* mark multiblock chunks only */
662c9de560dSAlex Tomas 		grp->bb_counters[min]++;
663c9de560dSAlex Tomas 		if (min > 0)
664c9de560dSAlex Tomas 			mb_clear_bit(first >> min,
665c9de560dSAlex Tomas 				     buddy + sbi->s_mb_offsets[min]);
666c9de560dSAlex Tomas 
667c9de560dSAlex Tomas 		len -= chunk;
668c9de560dSAlex Tomas 		first += chunk;
669c9de560dSAlex Tomas 	}
670c9de560dSAlex Tomas }
671c9de560dSAlex Tomas 
672c9de560dSAlex Tomas static void ext4_mb_generate_buddy(struct super_block *sb,
673c9de560dSAlex Tomas 				void *buddy, void *bitmap, ext4_group_t group)
674c9de560dSAlex Tomas {
675c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
676c9de560dSAlex Tomas 	unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
677c9de560dSAlex Tomas 	unsigned short i = 0;
678c9de560dSAlex Tomas 	unsigned short first;
679c9de560dSAlex Tomas 	unsigned short len;
680c9de560dSAlex Tomas 	unsigned free = 0;
681c9de560dSAlex Tomas 	unsigned fragments = 0;
682c9de560dSAlex Tomas 	unsigned long long period = get_cycles();
683c9de560dSAlex Tomas 
684c9de560dSAlex Tomas 	/* initialize buddy from bitmap which is aggregation
685c9de560dSAlex Tomas 	 * of on-disk bitmap and preallocations */
686ffad0a44SAneesh Kumar K.V 	i = mb_find_next_zero_bit(bitmap, max, 0);
687c9de560dSAlex Tomas 	grp->bb_first_free = i;
688c9de560dSAlex Tomas 	while (i < max) {
689c9de560dSAlex Tomas 		fragments++;
690c9de560dSAlex Tomas 		first = i;
691ffad0a44SAneesh Kumar K.V 		i = mb_find_next_bit(bitmap, max, i);
692c9de560dSAlex Tomas 		len = i - first;
693c9de560dSAlex Tomas 		free += len;
694c9de560dSAlex Tomas 		if (len > 1)
695c9de560dSAlex Tomas 			ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
696c9de560dSAlex Tomas 		else
697c9de560dSAlex Tomas 			grp->bb_counters[0]++;
698c9de560dSAlex Tomas 		if (i < max)
699ffad0a44SAneesh Kumar K.V 			i = mb_find_next_zero_bit(bitmap, max, i);
700c9de560dSAlex Tomas 	}
701c9de560dSAlex Tomas 	grp->bb_fragments = fragments;
702c9de560dSAlex Tomas 
703c9de560dSAlex Tomas 	if (free != grp->bb_free) {
7045d1b1b3fSAneesh Kumar K.V 		ext4_grp_locked_error(sb, group,  __func__,
705a9df9a49STheodore Ts'o 			"EXT4-fs: group %u: %u blocks in bitmap, %u in gd",
706c9de560dSAlex Tomas 			group, free, grp->bb_free);
707e56eb659SAneesh Kumar K.V 		/*
708e56eb659SAneesh Kumar K.V 		 * If we intent to continue, we consider group descritor
709e56eb659SAneesh Kumar K.V 		 * corrupt and update bb_free using bitmap value
710e56eb659SAneesh Kumar K.V 		 */
711c9de560dSAlex Tomas 		grp->bb_free = free;
712c9de560dSAlex Tomas 	}
713c9de560dSAlex Tomas 
714c9de560dSAlex Tomas 	clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
715c9de560dSAlex Tomas 
716c9de560dSAlex Tomas 	period = get_cycles() - period;
717c9de560dSAlex Tomas 	spin_lock(&EXT4_SB(sb)->s_bal_lock);
718c9de560dSAlex Tomas 	EXT4_SB(sb)->s_mb_buddies_generated++;
719c9de560dSAlex Tomas 	EXT4_SB(sb)->s_mb_generation_time += period;
720c9de560dSAlex Tomas 	spin_unlock(&EXT4_SB(sb)->s_bal_lock);
721c9de560dSAlex Tomas }
722c9de560dSAlex Tomas 
723c9de560dSAlex Tomas /* The buddy information is attached the buddy cache inode
724c9de560dSAlex Tomas  * for convenience. The information regarding each group
725c9de560dSAlex Tomas  * is loaded via ext4_mb_load_buddy. The information involve
726c9de560dSAlex Tomas  * block bitmap and buddy information. The information are
727c9de560dSAlex Tomas  * stored in the inode as
728c9de560dSAlex Tomas  *
729c9de560dSAlex Tomas  * {                        page                        }
730c3a326a6SAneesh Kumar K.V  * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
731c9de560dSAlex Tomas  *
732c9de560dSAlex Tomas  *
733c9de560dSAlex Tomas  * one block each for bitmap and buddy information.
734c9de560dSAlex Tomas  * So for each group we take up 2 blocks. A page can
735c9de560dSAlex Tomas  * contain blocks_per_page (PAGE_CACHE_SIZE / blocksize)  blocks.
736c9de560dSAlex Tomas  * So it can have information regarding groups_per_page which
737c9de560dSAlex Tomas  * is blocks_per_page/2
738c9de560dSAlex Tomas  */
739c9de560dSAlex Tomas 
740c9de560dSAlex Tomas static int ext4_mb_init_cache(struct page *page, char *incore)
741c9de560dSAlex Tomas {
742c9de560dSAlex Tomas 	int blocksize;
743c9de560dSAlex Tomas 	int blocks_per_page;
744c9de560dSAlex Tomas 	int groups_per_page;
745c9de560dSAlex Tomas 	int err = 0;
746c9de560dSAlex Tomas 	int i;
747c9de560dSAlex Tomas 	ext4_group_t first_group;
748c9de560dSAlex Tomas 	int first_block;
749c9de560dSAlex Tomas 	struct super_block *sb;
750c9de560dSAlex Tomas 	struct buffer_head *bhs;
751c9de560dSAlex Tomas 	struct buffer_head **bh;
752c9de560dSAlex Tomas 	struct inode *inode;
753c9de560dSAlex Tomas 	char *data;
754c9de560dSAlex Tomas 	char *bitmap;
755c9de560dSAlex Tomas 
756c9de560dSAlex Tomas 	mb_debug("init page %lu\n", page->index);
757c9de560dSAlex Tomas 
758c9de560dSAlex Tomas 	inode = page->mapping->host;
759c9de560dSAlex Tomas 	sb = inode->i_sb;
760c9de560dSAlex Tomas 	blocksize = 1 << inode->i_blkbits;
761c9de560dSAlex Tomas 	blocks_per_page = PAGE_CACHE_SIZE / blocksize;
762c9de560dSAlex Tomas 
763c9de560dSAlex Tomas 	groups_per_page = blocks_per_page >> 1;
764c9de560dSAlex Tomas 	if (groups_per_page == 0)
765c9de560dSAlex Tomas 		groups_per_page = 1;
766c9de560dSAlex Tomas 
767c9de560dSAlex Tomas 	/* allocate buffer_heads to read bitmaps */
768c9de560dSAlex Tomas 	if (groups_per_page > 1) {
769c9de560dSAlex Tomas 		err = -ENOMEM;
770c9de560dSAlex Tomas 		i = sizeof(struct buffer_head *) * groups_per_page;
771c9de560dSAlex Tomas 		bh = kzalloc(i, GFP_NOFS);
772c9de560dSAlex Tomas 		if (bh == NULL)
773c9de560dSAlex Tomas 			goto out;
774c9de560dSAlex Tomas 	} else
775c9de560dSAlex Tomas 		bh = &bhs;
776c9de560dSAlex Tomas 
777c9de560dSAlex Tomas 	first_group = page->index * blocks_per_page / 2;
778c9de560dSAlex Tomas 
779c9de560dSAlex Tomas 	/* read all groups the page covers into the cache */
780c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page; i++) {
781c9de560dSAlex Tomas 		struct ext4_group_desc *desc;
782c9de560dSAlex Tomas 
783c9de560dSAlex Tomas 		if (first_group + i >= EXT4_SB(sb)->s_groups_count)
784c9de560dSAlex Tomas 			break;
785c9de560dSAlex Tomas 
786c9de560dSAlex Tomas 		err = -EIO;
787c9de560dSAlex Tomas 		desc = ext4_get_group_desc(sb, first_group + i, NULL);
788c9de560dSAlex Tomas 		if (desc == NULL)
789c9de560dSAlex Tomas 			goto out;
790c9de560dSAlex Tomas 
791c9de560dSAlex Tomas 		err = -ENOMEM;
792c9de560dSAlex Tomas 		bh[i] = sb_getblk(sb, ext4_block_bitmap(sb, desc));
793c9de560dSAlex Tomas 		if (bh[i] == NULL)
794c9de560dSAlex Tomas 			goto out;
795c9de560dSAlex Tomas 
7962ccb5fb9SAneesh Kumar K.V 		if (bitmap_uptodate(bh[i]))
797c9de560dSAlex Tomas 			continue;
798c9de560dSAlex Tomas 
799c806e68fSFrederic Bohe 		lock_buffer(bh[i]);
8002ccb5fb9SAneesh Kumar K.V 		if (bitmap_uptodate(bh[i])) {
8012ccb5fb9SAneesh Kumar K.V 			unlock_buffer(bh[i]);
8022ccb5fb9SAneesh Kumar K.V 			continue;
8032ccb5fb9SAneesh Kumar K.V 		}
804b5f10eedSEric Sandeen 		spin_lock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
805c9de560dSAlex Tomas 		if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
806c9de560dSAlex Tomas 			ext4_init_block_bitmap(sb, bh[i],
807c9de560dSAlex Tomas 						first_group + i, desc);
8082ccb5fb9SAneesh Kumar K.V 			set_bitmap_uptodate(bh[i]);
809c9de560dSAlex Tomas 			set_buffer_uptodate(bh[i]);
810b5f10eedSEric Sandeen 			spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
8113300bedaSAneesh Kumar K.V 			unlock_buffer(bh[i]);
812c9de560dSAlex Tomas 			continue;
813c9de560dSAlex Tomas 		}
814b5f10eedSEric Sandeen 		spin_unlock(sb_bgl_lock(EXT4_SB(sb), first_group + i));
8152ccb5fb9SAneesh Kumar K.V 		if (buffer_uptodate(bh[i])) {
8162ccb5fb9SAneesh Kumar K.V 			/*
8172ccb5fb9SAneesh Kumar K.V 			 * if not uninit if bh is uptodate,
8182ccb5fb9SAneesh Kumar K.V 			 * bitmap is also uptodate
8192ccb5fb9SAneesh Kumar K.V 			 */
8202ccb5fb9SAneesh Kumar K.V 			set_bitmap_uptodate(bh[i]);
8212ccb5fb9SAneesh Kumar K.V 			unlock_buffer(bh[i]);
8222ccb5fb9SAneesh Kumar K.V 			continue;
8232ccb5fb9SAneesh Kumar K.V 		}
824c9de560dSAlex Tomas 		get_bh(bh[i]);
8252ccb5fb9SAneesh Kumar K.V 		/*
8262ccb5fb9SAneesh Kumar K.V 		 * submit the buffer_head for read. We can
8272ccb5fb9SAneesh Kumar K.V 		 * safely mark the bitmap as uptodate now.
8282ccb5fb9SAneesh Kumar K.V 		 * We do it here so the bitmap uptodate bit
8292ccb5fb9SAneesh Kumar K.V 		 * get set with buffer lock held.
8302ccb5fb9SAneesh Kumar K.V 		 */
8312ccb5fb9SAneesh Kumar K.V 		set_bitmap_uptodate(bh[i]);
832c9de560dSAlex Tomas 		bh[i]->b_end_io = end_buffer_read_sync;
833c9de560dSAlex Tomas 		submit_bh(READ, bh[i]);
834a9df9a49STheodore Ts'o 		mb_debug("read bitmap for group %u\n", first_group + i);
835c9de560dSAlex Tomas 	}
836c9de560dSAlex Tomas 
837c9de560dSAlex Tomas 	/* wait for I/O completion */
838c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page && bh[i]; i++)
839c9de560dSAlex Tomas 		wait_on_buffer(bh[i]);
840c9de560dSAlex Tomas 
841c9de560dSAlex Tomas 	err = -EIO;
842c9de560dSAlex Tomas 	for (i = 0; i < groups_per_page && bh[i]; i++)
843c9de560dSAlex Tomas 		if (!buffer_uptodate(bh[i]))
844c9de560dSAlex Tomas 			goto out;
845c9de560dSAlex Tomas 
84631b481dcSMingming Cao 	err = 0;
847c9de560dSAlex Tomas 	first_block = page->index * blocks_per_page;
84829eaf024SAneesh Kumar K.V 	/* init the page  */
84929eaf024SAneesh Kumar K.V 	memset(page_address(page), 0xff, PAGE_CACHE_SIZE);
850c9de560dSAlex Tomas 	for (i = 0; i < blocks_per_page; i++) {
851c9de560dSAlex Tomas 		int group;
852c9de560dSAlex Tomas 		struct ext4_group_info *grinfo;
853c9de560dSAlex Tomas 
854c9de560dSAlex Tomas 		group = (first_block + i) >> 1;
855c9de560dSAlex Tomas 		if (group >= EXT4_SB(sb)->s_groups_count)
856c9de560dSAlex Tomas 			break;
857c9de560dSAlex Tomas 
858c9de560dSAlex Tomas 		/*
859c9de560dSAlex Tomas 		 * data carry information regarding this
860c9de560dSAlex Tomas 		 * particular group in the format specified
861c9de560dSAlex Tomas 		 * above
862c9de560dSAlex Tomas 		 *
863c9de560dSAlex Tomas 		 */
864c9de560dSAlex Tomas 		data = page_address(page) + (i * blocksize);
865c9de560dSAlex Tomas 		bitmap = bh[group - first_group]->b_data;
866c9de560dSAlex Tomas 
867c9de560dSAlex Tomas 		/*
868c9de560dSAlex Tomas 		 * We place the buddy block and bitmap block
869c9de560dSAlex Tomas 		 * close together
870c9de560dSAlex Tomas 		 */
871c9de560dSAlex Tomas 		if ((first_block + i) & 1) {
872c9de560dSAlex Tomas 			/* this is block of buddy */
873c9de560dSAlex Tomas 			BUG_ON(incore == NULL);
874c9de560dSAlex Tomas 			mb_debug("put buddy for group %u in page %lu/%x\n",
875c9de560dSAlex Tomas 				group, page->index, i * blocksize);
876c9de560dSAlex Tomas 			grinfo = ext4_get_group_info(sb, group);
877c9de560dSAlex Tomas 			grinfo->bb_fragments = 0;
878c9de560dSAlex Tomas 			memset(grinfo->bb_counters, 0,
879c9de560dSAlex Tomas 			       sizeof(unsigned short)*(sb->s_blocksize_bits+2));
880c9de560dSAlex Tomas 			/*
881c9de560dSAlex Tomas 			 * incore got set to the group block bitmap below
882c9de560dSAlex Tomas 			 */
8837a2fcbf7SAneesh Kumar K.V 			ext4_lock_group(sb, group);
884c9de560dSAlex Tomas 			ext4_mb_generate_buddy(sb, data, incore, group);
8857a2fcbf7SAneesh Kumar K.V 			ext4_unlock_group(sb, group);
886c9de560dSAlex Tomas 			incore = NULL;
887c9de560dSAlex Tomas 		} else {
888c9de560dSAlex Tomas 			/* this is block of bitmap */
889c9de560dSAlex Tomas 			BUG_ON(incore != NULL);
890c9de560dSAlex Tomas 			mb_debug("put bitmap for group %u in page %lu/%x\n",
891c9de560dSAlex Tomas 				group, page->index, i * blocksize);
892c9de560dSAlex Tomas 
893c9de560dSAlex Tomas 			/* see comments in ext4_mb_put_pa() */
894c9de560dSAlex Tomas 			ext4_lock_group(sb, group);
895c9de560dSAlex Tomas 			memcpy(data, bitmap, blocksize);
896c9de560dSAlex Tomas 
897c9de560dSAlex Tomas 			/* mark all preallocated blks used in in-core bitmap */
898c9de560dSAlex Tomas 			ext4_mb_generate_from_pa(sb, data, group);
8997a2fcbf7SAneesh Kumar K.V 			ext4_mb_generate_from_freelist(sb, data, group);
900c9de560dSAlex Tomas 			ext4_unlock_group(sb, group);
901c9de560dSAlex Tomas 
902c9de560dSAlex Tomas 			/* set incore so that the buddy information can be
903c9de560dSAlex Tomas 			 * generated using this
904c9de560dSAlex Tomas 			 */
905c9de560dSAlex Tomas 			incore = data;
906c9de560dSAlex Tomas 		}
907c9de560dSAlex Tomas 	}
908c9de560dSAlex Tomas 	SetPageUptodate(page);
909c9de560dSAlex Tomas 
910c9de560dSAlex Tomas out:
911c9de560dSAlex Tomas 	if (bh) {
912c9de560dSAlex Tomas 		for (i = 0; i < groups_per_page && bh[i]; i++)
913c9de560dSAlex Tomas 			brelse(bh[i]);
914c9de560dSAlex Tomas 		if (bh != &bhs)
915c9de560dSAlex Tomas 			kfree(bh);
916c9de560dSAlex Tomas 	}
917c9de560dSAlex Tomas 	return err;
918c9de560dSAlex Tomas }
919c9de560dSAlex Tomas 
9204ddfef7bSEric Sandeen static noinline_for_stack int
9214ddfef7bSEric Sandeen ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
922c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
923c9de560dSAlex Tomas {
924c9de560dSAlex Tomas 	int blocks_per_page;
925c9de560dSAlex Tomas 	int block;
926c9de560dSAlex Tomas 	int pnum;
927c9de560dSAlex Tomas 	int poff;
928c9de560dSAlex Tomas 	struct page *page;
929fdf6c7a7SShen Feng 	int ret;
930920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
931920313a7SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
932920313a7SAneesh Kumar K.V 	struct inode *inode = sbi->s_buddy_cache;
933c9de560dSAlex Tomas 
934a9df9a49STheodore Ts'o 	mb_debug("load group %u\n", group);
935c9de560dSAlex Tomas 
936c9de560dSAlex Tomas 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
937920313a7SAneesh Kumar K.V 	grp = ext4_get_group_info(sb, group);
938c9de560dSAlex Tomas 
939c9de560dSAlex Tomas 	e4b->bd_blkbits = sb->s_blocksize_bits;
940c9de560dSAlex Tomas 	e4b->bd_info = ext4_get_group_info(sb, group);
941c9de560dSAlex Tomas 	e4b->bd_sb = sb;
942c9de560dSAlex Tomas 	e4b->bd_group = group;
943c9de560dSAlex Tomas 	e4b->bd_buddy_page = NULL;
944c9de560dSAlex Tomas 	e4b->bd_bitmap_page = NULL;
945920313a7SAneesh Kumar K.V 	e4b->alloc_semp = &grp->alloc_sem;
946920313a7SAneesh Kumar K.V 
947920313a7SAneesh Kumar K.V 	/* Take the read lock on the group alloc
948920313a7SAneesh Kumar K.V 	 * sem. This would make sure a parallel
949920313a7SAneesh Kumar K.V 	 * ext4_mb_init_group happening on other
950920313a7SAneesh Kumar K.V 	 * groups mapped by the page is blocked
951920313a7SAneesh Kumar K.V 	 * till we are done with allocation
952920313a7SAneesh Kumar K.V 	 */
953920313a7SAneesh Kumar K.V 	down_read(e4b->alloc_semp);
954c9de560dSAlex Tomas 
955c9de560dSAlex Tomas 	/*
956c9de560dSAlex Tomas 	 * the buddy cache inode stores the block bitmap
957c9de560dSAlex Tomas 	 * and buddy information in consecutive blocks.
958c9de560dSAlex Tomas 	 * So for each group we need two blocks.
959c9de560dSAlex Tomas 	 */
960c9de560dSAlex Tomas 	block = group * 2;
961c9de560dSAlex Tomas 	pnum = block / blocks_per_page;
962c9de560dSAlex Tomas 	poff = block % blocks_per_page;
963c9de560dSAlex Tomas 
964c9de560dSAlex Tomas 	/* we could use find_or_create_page(), but it locks page
965c9de560dSAlex Tomas 	 * what we'd like to avoid in fast path ... */
966c9de560dSAlex Tomas 	page = find_get_page(inode->i_mapping, pnum);
967c9de560dSAlex Tomas 	if (page == NULL || !PageUptodate(page)) {
968c9de560dSAlex Tomas 		if (page)
969920313a7SAneesh Kumar K.V 			/*
970920313a7SAneesh Kumar K.V 			 * drop the page reference and try
971920313a7SAneesh Kumar K.V 			 * to get the page with lock. If we
972920313a7SAneesh Kumar K.V 			 * are not uptodate that implies
973920313a7SAneesh Kumar K.V 			 * somebody just created the page but
974920313a7SAneesh Kumar K.V 			 * is yet to initialize the same. So
975920313a7SAneesh Kumar K.V 			 * wait for it to initialize.
976920313a7SAneesh Kumar K.V 			 */
977c9de560dSAlex Tomas 			page_cache_release(page);
978c9de560dSAlex Tomas 		page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
979c9de560dSAlex Tomas 		if (page) {
980c9de560dSAlex Tomas 			BUG_ON(page->mapping != inode->i_mapping);
981c9de560dSAlex Tomas 			if (!PageUptodate(page)) {
982fdf6c7a7SShen Feng 				ret = ext4_mb_init_cache(page, NULL);
983fdf6c7a7SShen Feng 				if (ret) {
984fdf6c7a7SShen Feng 					unlock_page(page);
985fdf6c7a7SShen Feng 					goto err;
986fdf6c7a7SShen Feng 				}
987c9de560dSAlex Tomas 				mb_cmp_bitmaps(e4b, page_address(page) +
988c9de560dSAlex Tomas 					       (poff * sb->s_blocksize));
989c9de560dSAlex Tomas 			}
990c9de560dSAlex Tomas 			unlock_page(page);
991c9de560dSAlex Tomas 		}
992c9de560dSAlex Tomas 	}
993fdf6c7a7SShen Feng 	if (page == NULL || !PageUptodate(page)) {
994fdf6c7a7SShen Feng 		ret = -EIO;
995c9de560dSAlex Tomas 		goto err;
996fdf6c7a7SShen Feng 	}
997c9de560dSAlex Tomas 	e4b->bd_bitmap_page = page;
998c9de560dSAlex Tomas 	e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
999c9de560dSAlex Tomas 	mark_page_accessed(page);
1000c9de560dSAlex Tomas 
1001c9de560dSAlex Tomas 	block++;
1002c9de560dSAlex Tomas 	pnum = block / blocks_per_page;
1003c9de560dSAlex Tomas 	poff = block % blocks_per_page;
1004c9de560dSAlex Tomas 
1005c9de560dSAlex Tomas 	page = find_get_page(inode->i_mapping, pnum);
1006c9de560dSAlex Tomas 	if (page == NULL || !PageUptodate(page)) {
1007c9de560dSAlex Tomas 		if (page)
1008c9de560dSAlex Tomas 			page_cache_release(page);
1009c9de560dSAlex Tomas 		page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1010c9de560dSAlex Tomas 		if (page) {
1011c9de560dSAlex Tomas 			BUG_ON(page->mapping != inode->i_mapping);
1012fdf6c7a7SShen Feng 			if (!PageUptodate(page)) {
1013fdf6c7a7SShen Feng 				ret = ext4_mb_init_cache(page, e4b->bd_bitmap);
1014fdf6c7a7SShen Feng 				if (ret) {
1015fdf6c7a7SShen Feng 					unlock_page(page);
1016fdf6c7a7SShen Feng 					goto err;
1017fdf6c7a7SShen Feng 				}
1018fdf6c7a7SShen Feng 			}
1019c9de560dSAlex Tomas 			unlock_page(page);
1020c9de560dSAlex Tomas 		}
1021c9de560dSAlex Tomas 	}
1022fdf6c7a7SShen Feng 	if (page == NULL || !PageUptodate(page)) {
1023fdf6c7a7SShen Feng 		ret = -EIO;
1024c9de560dSAlex Tomas 		goto err;
1025fdf6c7a7SShen Feng 	}
1026c9de560dSAlex Tomas 	e4b->bd_buddy_page = page;
1027c9de560dSAlex Tomas 	e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1028c9de560dSAlex Tomas 	mark_page_accessed(page);
1029c9de560dSAlex Tomas 
1030c9de560dSAlex Tomas 	BUG_ON(e4b->bd_bitmap_page == NULL);
1031c9de560dSAlex Tomas 	BUG_ON(e4b->bd_buddy_page == NULL);
1032c9de560dSAlex Tomas 
1033c9de560dSAlex Tomas 	return 0;
1034c9de560dSAlex Tomas 
1035c9de560dSAlex Tomas err:
1036c9de560dSAlex Tomas 	if (e4b->bd_bitmap_page)
1037c9de560dSAlex Tomas 		page_cache_release(e4b->bd_bitmap_page);
1038c9de560dSAlex Tomas 	if (e4b->bd_buddy_page)
1039c9de560dSAlex Tomas 		page_cache_release(e4b->bd_buddy_page);
1040c9de560dSAlex Tomas 	e4b->bd_buddy = NULL;
1041c9de560dSAlex Tomas 	e4b->bd_bitmap = NULL;
1042920313a7SAneesh Kumar K.V 
1043920313a7SAneesh Kumar K.V 	/* Done with the buddy cache */
1044920313a7SAneesh Kumar K.V 	up_read(e4b->alloc_semp);
1045fdf6c7a7SShen Feng 	return ret;
1046c9de560dSAlex Tomas }
1047c9de560dSAlex Tomas 
1048c9de560dSAlex Tomas static void ext4_mb_release_desc(struct ext4_buddy *e4b)
1049c9de560dSAlex Tomas {
1050c9de560dSAlex Tomas 	if (e4b->bd_bitmap_page)
1051c9de560dSAlex Tomas 		page_cache_release(e4b->bd_bitmap_page);
1052c9de560dSAlex Tomas 	if (e4b->bd_buddy_page)
1053c9de560dSAlex Tomas 		page_cache_release(e4b->bd_buddy_page);
1054920313a7SAneesh Kumar K.V 	/* Done with the buddy cache */
10558556e8f3SAneesh Kumar K.V 	if (e4b->alloc_semp)
1056920313a7SAneesh Kumar K.V 		up_read(e4b->alloc_semp);
1057c9de560dSAlex Tomas }
1058c9de560dSAlex Tomas 
1059c9de560dSAlex Tomas 
1060c9de560dSAlex Tomas static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1061c9de560dSAlex Tomas {
1062c9de560dSAlex Tomas 	int order = 1;
1063c9de560dSAlex Tomas 	void *bb;
1064c9de560dSAlex Tomas 
1065c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_BITMAP(e4b) == EXT4_MB_BUDDY(e4b));
1066c9de560dSAlex Tomas 	BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1067c9de560dSAlex Tomas 
1068c9de560dSAlex Tomas 	bb = EXT4_MB_BUDDY(e4b);
1069c9de560dSAlex Tomas 	while (order <= e4b->bd_blkbits + 1) {
1070c9de560dSAlex Tomas 		block = block >> 1;
1071c9de560dSAlex Tomas 		if (!mb_test_bit(block, bb)) {
1072c9de560dSAlex Tomas 			/* this block is part of buddy of order 'order' */
1073c9de560dSAlex Tomas 			return order;
1074c9de560dSAlex Tomas 		}
1075c9de560dSAlex Tomas 		bb += 1 << (e4b->bd_blkbits - order);
1076c9de560dSAlex Tomas 		order++;
1077c9de560dSAlex Tomas 	}
1078c9de560dSAlex Tomas 	return 0;
1079c9de560dSAlex Tomas }
1080c9de560dSAlex Tomas 
1081c9de560dSAlex Tomas static void mb_clear_bits(spinlock_t *lock, void *bm, int cur, int len)
1082c9de560dSAlex Tomas {
1083c9de560dSAlex Tomas 	__u32 *addr;
1084c9de560dSAlex Tomas 
1085c9de560dSAlex Tomas 	len = cur + len;
1086c9de560dSAlex Tomas 	while (cur < len) {
1087c9de560dSAlex Tomas 		if ((cur & 31) == 0 && (len - cur) >= 32) {
1088c9de560dSAlex Tomas 			/* fast path: clear whole word at once */
1089c9de560dSAlex Tomas 			addr = bm + (cur >> 3);
1090c9de560dSAlex Tomas 			*addr = 0;
1091c9de560dSAlex Tomas 			cur += 32;
1092c9de560dSAlex Tomas 			continue;
1093c9de560dSAlex Tomas 		}
1094e8134b27SAneesh Kumar K.V 		if (lock)
1095c9de560dSAlex Tomas 			mb_clear_bit_atomic(lock, cur, bm);
1096e8134b27SAneesh Kumar K.V 		else
1097e8134b27SAneesh Kumar K.V 			mb_clear_bit(cur, bm);
1098c9de560dSAlex Tomas 		cur++;
1099c9de560dSAlex Tomas 	}
1100c9de560dSAlex Tomas }
1101c9de560dSAlex Tomas 
1102c9de560dSAlex Tomas static void mb_set_bits(spinlock_t *lock, void *bm, int cur, int len)
1103c9de560dSAlex Tomas {
1104c9de560dSAlex Tomas 	__u32 *addr;
1105c9de560dSAlex Tomas 
1106c9de560dSAlex Tomas 	len = cur + len;
1107c9de560dSAlex Tomas 	while (cur < len) {
1108c9de560dSAlex Tomas 		if ((cur & 31) == 0 && (len - cur) >= 32) {
1109c9de560dSAlex Tomas 			/* fast path: set whole word at once */
1110c9de560dSAlex Tomas 			addr = bm + (cur >> 3);
1111c9de560dSAlex Tomas 			*addr = 0xffffffff;
1112c9de560dSAlex Tomas 			cur += 32;
1113c9de560dSAlex Tomas 			continue;
1114c9de560dSAlex Tomas 		}
1115e8134b27SAneesh Kumar K.V 		if (lock)
1116c9de560dSAlex Tomas 			mb_set_bit_atomic(lock, cur, bm);
1117e8134b27SAneesh Kumar K.V 		else
1118e8134b27SAneesh Kumar K.V 			mb_set_bit(cur, bm);
1119c9de560dSAlex Tomas 		cur++;
1120c9de560dSAlex Tomas 	}
1121c9de560dSAlex Tomas }
1122c9de560dSAlex Tomas 
11237e5a8cddSShen Feng static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1124c9de560dSAlex Tomas 			  int first, int count)
1125c9de560dSAlex Tomas {
1126c9de560dSAlex Tomas 	int block = 0;
1127c9de560dSAlex Tomas 	int max = 0;
1128c9de560dSAlex Tomas 	int order;
1129c9de560dSAlex Tomas 	void *buddy;
1130c9de560dSAlex Tomas 	void *buddy2;
1131c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
1132c9de560dSAlex Tomas 
1133c9de560dSAlex Tomas 	BUG_ON(first + count > (sb->s_blocksize << 3));
1134c9de560dSAlex Tomas 	BUG_ON(!ext4_is_group_locked(sb, e4b->bd_group));
1135c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1136c9de560dSAlex Tomas 	mb_free_blocks_double(inode, e4b, first, count);
1137c9de560dSAlex Tomas 
1138c9de560dSAlex Tomas 	e4b->bd_info->bb_free += count;
1139c9de560dSAlex Tomas 	if (first < e4b->bd_info->bb_first_free)
1140c9de560dSAlex Tomas 		e4b->bd_info->bb_first_free = first;
1141c9de560dSAlex Tomas 
1142c9de560dSAlex Tomas 	/* let's maintain fragments counter */
1143c9de560dSAlex Tomas 	if (first != 0)
1144c9de560dSAlex Tomas 		block = !mb_test_bit(first - 1, EXT4_MB_BITMAP(e4b));
1145c9de560dSAlex Tomas 	if (first + count < EXT4_SB(sb)->s_mb_maxs[0])
1146c9de560dSAlex Tomas 		max = !mb_test_bit(first + count, EXT4_MB_BITMAP(e4b));
1147c9de560dSAlex Tomas 	if (block && max)
1148c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments--;
1149c9de560dSAlex Tomas 	else if (!block && !max)
1150c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments++;
1151c9de560dSAlex Tomas 
1152c9de560dSAlex Tomas 	/* let's maintain buddy itself */
1153c9de560dSAlex Tomas 	while (count-- > 0) {
1154c9de560dSAlex Tomas 		block = first++;
1155c9de560dSAlex Tomas 		order = 0;
1156c9de560dSAlex Tomas 
1157c9de560dSAlex Tomas 		if (!mb_test_bit(block, EXT4_MB_BITMAP(e4b))) {
1158c9de560dSAlex Tomas 			ext4_fsblk_t blocknr;
1159c9de560dSAlex Tomas 			blocknr = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb);
1160c9de560dSAlex Tomas 			blocknr += block;
1161c9de560dSAlex Tomas 			blocknr +=
1162c9de560dSAlex Tomas 			    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
11635d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
11645d1b1b3fSAneesh Kumar K.V 				   __func__, "double-free of inode"
1165a9df9a49STheodore Ts'o 				   " %lu's block %llu(bit %u in group %u)",
1166c9de560dSAlex Tomas 				   inode ? inode->i_ino : 0, blocknr, block,
1167c9de560dSAlex Tomas 				   e4b->bd_group);
1168c9de560dSAlex Tomas 		}
1169c9de560dSAlex Tomas 		mb_clear_bit(block, EXT4_MB_BITMAP(e4b));
1170c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[order]++;
1171c9de560dSAlex Tomas 
1172c9de560dSAlex Tomas 		/* start of the buddy */
1173c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, order, &max);
1174c9de560dSAlex Tomas 
1175c9de560dSAlex Tomas 		do {
1176c9de560dSAlex Tomas 			block &= ~1UL;
1177c9de560dSAlex Tomas 			if (mb_test_bit(block, buddy) ||
1178c9de560dSAlex Tomas 					mb_test_bit(block + 1, buddy))
1179c9de560dSAlex Tomas 				break;
1180c9de560dSAlex Tomas 
1181c9de560dSAlex Tomas 			/* both the buddies are free, try to coalesce them */
1182c9de560dSAlex Tomas 			buddy2 = mb_find_buddy(e4b, order + 1, &max);
1183c9de560dSAlex Tomas 
1184c9de560dSAlex Tomas 			if (!buddy2)
1185c9de560dSAlex Tomas 				break;
1186c9de560dSAlex Tomas 
1187c9de560dSAlex Tomas 			if (order > 0) {
1188c9de560dSAlex Tomas 				/* for special purposes, we don't set
1189c9de560dSAlex Tomas 				 * free bits in bitmap */
1190c9de560dSAlex Tomas 				mb_set_bit(block, buddy);
1191c9de560dSAlex Tomas 				mb_set_bit(block + 1, buddy);
1192c9de560dSAlex Tomas 			}
1193c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]--;
1194c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]--;
1195c9de560dSAlex Tomas 
1196c9de560dSAlex Tomas 			block = block >> 1;
1197c9de560dSAlex Tomas 			order++;
1198c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[order]++;
1199c9de560dSAlex Tomas 
1200c9de560dSAlex Tomas 			mb_clear_bit(block, buddy2);
1201c9de560dSAlex Tomas 			buddy = buddy2;
1202c9de560dSAlex Tomas 		} while (1);
1203c9de560dSAlex Tomas 	}
1204c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1205c9de560dSAlex Tomas }
1206c9de560dSAlex Tomas 
1207c9de560dSAlex Tomas static int mb_find_extent(struct ext4_buddy *e4b, int order, int block,
1208c9de560dSAlex Tomas 				int needed, struct ext4_free_extent *ex)
1209c9de560dSAlex Tomas {
1210c9de560dSAlex Tomas 	int next = block;
1211c9de560dSAlex Tomas 	int max;
1212c9de560dSAlex Tomas 	int ord;
1213c9de560dSAlex Tomas 	void *buddy;
1214c9de560dSAlex Tomas 
1215c9de560dSAlex Tomas 	BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1216c9de560dSAlex Tomas 	BUG_ON(ex == NULL);
1217c9de560dSAlex Tomas 
1218c9de560dSAlex Tomas 	buddy = mb_find_buddy(e4b, order, &max);
1219c9de560dSAlex Tomas 	BUG_ON(buddy == NULL);
1220c9de560dSAlex Tomas 	BUG_ON(block >= max);
1221c9de560dSAlex Tomas 	if (mb_test_bit(block, buddy)) {
1222c9de560dSAlex Tomas 		ex->fe_len = 0;
1223c9de560dSAlex Tomas 		ex->fe_start = 0;
1224c9de560dSAlex Tomas 		ex->fe_group = 0;
1225c9de560dSAlex Tomas 		return 0;
1226c9de560dSAlex Tomas 	}
1227c9de560dSAlex Tomas 
1228c9de560dSAlex Tomas 	/* FIXME dorp order completely ? */
1229c9de560dSAlex Tomas 	if (likely(order == 0)) {
1230c9de560dSAlex Tomas 		/* find actual order */
1231c9de560dSAlex Tomas 		order = mb_find_order_for_block(e4b, block);
1232c9de560dSAlex Tomas 		block = block >> order;
1233c9de560dSAlex Tomas 	}
1234c9de560dSAlex Tomas 
1235c9de560dSAlex Tomas 	ex->fe_len = 1 << order;
1236c9de560dSAlex Tomas 	ex->fe_start = block << order;
1237c9de560dSAlex Tomas 	ex->fe_group = e4b->bd_group;
1238c9de560dSAlex Tomas 
1239c9de560dSAlex Tomas 	/* calc difference from given start */
1240c9de560dSAlex Tomas 	next = next - ex->fe_start;
1241c9de560dSAlex Tomas 	ex->fe_len -= next;
1242c9de560dSAlex Tomas 	ex->fe_start += next;
1243c9de560dSAlex Tomas 
1244c9de560dSAlex Tomas 	while (needed > ex->fe_len &&
1245c9de560dSAlex Tomas 	       (buddy = mb_find_buddy(e4b, order, &max))) {
1246c9de560dSAlex Tomas 
1247c9de560dSAlex Tomas 		if (block + 1 >= max)
1248c9de560dSAlex Tomas 			break;
1249c9de560dSAlex Tomas 
1250c9de560dSAlex Tomas 		next = (block + 1) * (1 << order);
1251c9de560dSAlex Tomas 		if (mb_test_bit(next, EXT4_MB_BITMAP(e4b)))
1252c9de560dSAlex Tomas 			break;
1253c9de560dSAlex Tomas 
1254c9de560dSAlex Tomas 		ord = mb_find_order_for_block(e4b, next);
1255c9de560dSAlex Tomas 
1256c9de560dSAlex Tomas 		order = ord;
1257c9de560dSAlex Tomas 		block = next >> order;
1258c9de560dSAlex Tomas 		ex->fe_len += 1 << order;
1259c9de560dSAlex Tomas 	}
1260c9de560dSAlex Tomas 
1261c9de560dSAlex Tomas 	BUG_ON(ex->fe_start + ex->fe_len > (1 << (e4b->bd_blkbits + 3)));
1262c9de560dSAlex Tomas 	return ex->fe_len;
1263c9de560dSAlex Tomas }
1264c9de560dSAlex Tomas 
1265c9de560dSAlex Tomas static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
1266c9de560dSAlex Tomas {
1267c9de560dSAlex Tomas 	int ord;
1268c9de560dSAlex Tomas 	int mlen = 0;
1269c9de560dSAlex Tomas 	int max = 0;
1270c9de560dSAlex Tomas 	int cur;
1271c9de560dSAlex Tomas 	int start = ex->fe_start;
1272c9de560dSAlex Tomas 	int len = ex->fe_len;
1273c9de560dSAlex Tomas 	unsigned ret = 0;
1274c9de560dSAlex Tomas 	int len0 = len;
1275c9de560dSAlex Tomas 	void *buddy;
1276c9de560dSAlex Tomas 
1277c9de560dSAlex Tomas 	BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
1278c9de560dSAlex Tomas 	BUG_ON(e4b->bd_group != ex->fe_group);
1279c9de560dSAlex Tomas 	BUG_ON(!ext4_is_group_locked(e4b->bd_sb, e4b->bd_group));
1280c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1281c9de560dSAlex Tomas 	mb_mark_used_double(e4b, start, len);
1282c9de560dSAlex Tomas 
1283c9de560dSAlex Tomas 	e4b->bd_info->bb_free -= len;
1284c9de560dSAlex Tomas 	if (e4b->bd_info->bb_first_free == start)
1285c9de560dSAlex Tomas 		e4b->bd_info->bb_first_free += len;
1286c9de560dSAlex Tomas 
1287c9de560dSAlex Tomas 	/* let's maintain fragments counter */
1288c9de560dSAlex Tomas 	if (start != 0)
1289c9de560dSAlex Tomas 		mlen = !mb_test_bit(start - 1, EXT4_MB_BITMAP(e4b));
1290c9de560dSAlex Tomas 	if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
1291c9de560dSAlex Tomas 		max = !mb_test_bit(start + len, EXT4_MB_BITMAP(e4b));
1292c9de560dSAlex Tomas 	if (mlen && max)
1293c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments++;
1294c9de560dSAlex Tomas 	else if (!mlen && !max)
1295c9de560dSAlex Tomas 		e4b->bd_info->bb_fragments--;
1296c9de560dSAlex Tomas 
1297c9de560dSAlex Tomas 	/* let's maintain buddy itself */
1298c9de560dSAlex Tomas 	while (len) {
1299c9de560dSAlex Tomas 		ord = mb_find_order_for_block(e4b, start);
1300c9de560dSAlex Tomas 
1301c9de560dSAlex Tomas 		if (((start >> ord) << ord) == start && len >= (1 << ord)) {
1302c9de560dSAlex Tomas 			/* the whole chunk may be allocated at once! */
1303c9de560dSAlex Tomas 			mlen = 1 << ord;
1304c9de560dSAlex Tomas 			buddy = mb_find_buddy(e4b, ord, &max);
1305c9de560dSAlex Tomas 			BUG_ON((start >> ord) >= max);
1306c9de560dSAlex Tomas 			mb_set_bit(start >> ord, buddy);
1307c9de560dSAlex Tomas 			e4b->bd_info->bb_counters[ord]--;
1308c9de560dSAlex Tomas 			start += mlen;
1309c9de560dSAlex Tomas 			len -= mlen;
1310c9de560dSAlex Tomas 			BUG_ON(len < 0);
1311c9de560dSAlex Tomas 			continue;
1312c9de560dSAlex Tomas 		}
1313c9de560dSAlex Tomas 
1314c9de560dSAlex Tomas 		/* store for history */
1315c9de560dSAlex Tomas 		if (ret == 0)
1316c9de560dSAlex Tomas 			ret = len | (ord << 16);
1317c9de560dSAlex Tomas 
1318c9de560dSAlex Tomas 		/* we have to split large buddy */
1319c9de560dSAlex Tomas 		BUG_ON(ord <= 0);
1320c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, ord, &max);
1321c9de560dSAlex Tomas 		mb_set_bit(start >> ord, buddy);
1322c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]--;
1323c9de560dSAlex Tomas 
1324c9de560dSAlex Tomas 		ord--;
1325c9de560dSAlex Tomas 		cur = (start >> ord) & ~1U;
1326c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, ord, &max);
1327c9de560dSAlex Tomas 		mb_clear_bit(cur, buddy);
1328c9de560dSAlex Tomas 		mb_clear_bit(cur + 1, buddy);
1329c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]++;
1330c9de560dSAlex Tomas 		e4b->bd_info->bb_counters[ord]++;
1331c9de560dSAlex Tomas 	}
1332c9de560dSAlex Tomas 
1333c9de560dSAlex Tomas 	mb_set_bits(sb_bgl_lock(EXT4_SB(e4b->bd_sb), ex->fe_group),
1334c9de560dSAlex Tomas 			EXT4_MB_BITMAP(e4b), ex->fe_start, len0);
1335c9de560dSAlex Tomas 	mb_check_buddy(e4b);
1336c9de560dSAlex Tomas 
1337c9de560dSAlex Tomas 	return ret;
1338c9de560dSAlex Tomas }
1339c9de560dSAlex Tomas 
1340c9de560dSAlex Tomas /*
1341c9de560dSAlex Tomas  * Must be called under group lock!
1342c9de560dSAlex Tomas  */
1343c9de560dSAlex Tomas static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
1344c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1345c9de560dSAlex Tomas {
1346c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1347c9de560dSAlex Tomas 	int ret;
1348c9de560dSAlex Tomas 
1349c9de560dSAlex Tomas 	BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
1350c9de560dSAlex Tomas 	BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1351c9de560dSAlex Tomas 
1352c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
1353c9de560dSAlex Tomas 	ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
1354c9de560dSAlex Tomas 	ret = mb_mark_used(e4b, &ac->ac_b_ex);
1355c9de560dSAlex Tomas 
1356c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
1357c9de560dSAlex Tomas 	 * allocated blocks for history */
1358c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
1359c9de560dSAlex Tomas 
1360c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
1361c9de560dSAlex Tomas 	ac->ac_tail = ret & 0xffff;
1362c9de560dSAlex Tomas 	ac->ac_buddy = ret >> 16;
1363c9de560dSAlex Tomas 
1364c3a326a6SAneesh Kumar K.V 	/*
1365c3a326a6SAneesh Kumar K.V 	 * take the page reference. We want the page to be pinned
1366c3a326a6SAneesh Kumar K.V 	 * so that we don't get a ext4_mb_init_cache_call for this
1367c3a326a6SAneesh Kumar K.V 	 * group until we update the bitmap. That would mean we
1368c3a326a6SAneesh Kumar K.V 	 * double allocate blocks. The reference is dropped
1369c3a326a6SAneesh Kumar K.V 	 * in ext4_mb_release_context
1370c3a326a6SAneesh Kumar K.V 	 */
1371c9de560dSAlex Tomas 	ac->ac_bitmap_page = e4b->bd_bitmap_page;
1372c9de560dSAlex Tomas 	get_page(ac->ac_bitmap_page);
1373c9de560dSAlex Tomas 	ac->ac_buddy_page = e4b->bd_buddy_page;
1374c9de560dSAlex Tomas 	get_page(ac->ac_buddy_page);
13758556e8f3SAneesh Kumar K.V 	/* on allocation we use ac to track the held semaphore */
13768556e8f3SAneesh Kumar K.V 	ac->alloc_semp =  e4b->alloc_semp;
13778556e8f3SAneesh Kumar K.V 	e4b->alloc_semp = NULL;
1378c9de560dSAlex Tomas 	/* store last allocated for subsequent stream allocation */
1379c9de560dSAlex Tomas 	if ((ac->ac_flags & EXT4_MB_HINT_DATA)) {
1380c9de560dSAlex Tomas 		spin_lock(&sbi->s_md_lock);
1381c9de560dSAlex Tomas 		sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
1382c9de560dSAlex Tomas 		sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
1383c9de560dSAlex Tomas 		spin_unlock(&sbi->s_md_lock);
1384c9de560dSAlex Tomas 	}
1385c9de560dSAlex Tomas }
1386c9de560dSAlex Tomas 
1387c9de560dSAlex Tomas /*
1388c9de560dSAlex Tomas  * regular allocator, for general purposes allocation
1389c9de560dSAlex Tomas  */
1390c9de560dSAlex Tomas 
1391c9de560dSAlex Tomas static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
1392c9de560dSAlex Tomas 					struct ext4_buddy *e4b,
1393c9de560dSAlex Tomas 					int finish_group)
1394c9de560dSAlex Tomas {
1395c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1396c9de560dSAlex Tomas 	struct ext4_free_extent *bex = &ac->ac_b_ex;
1397c9de560dSAlex Tomas 	struct ext4_free_extent *gex = &ac->ac_g_ex;
1398c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1399c9de560dSAlex Tomas 	int max;
1400c9de560dSAlex Tomas 
1401032115fcSAneesh Kumar K.V 	if (ac->ac_status == AC_STATUS_FOUND)
1402032115fcSAneesh Kumar K.V 		return;
1403c9de560dSAlex Tomas 	/*
1404c9de560dSAlex Tomas 	 * We don't want to scan for a whole year
1405c9de560dSAlex Tomas 	 */
1406c9de560dSAlex Tomas 	if (ac->ac_found > sbi->s_mb_max_to_scan &&
1407c9de560dSAlex Tomas 			!(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1408c9de560dSAlex Tomas 		ac->ac_status = AC_STATUS_BREAK;
1409c9de560dSAlex Tomas 		return;
1410c9de560dSAlex Tomas 	}
1411c9de560dSAlex Tomas 
1412c9de560dSAlex Tomas 	/*
1413c9de560dSAlex Tomas 	 * Haven't found good chunk so far, let's continue
1414c9de560dSAlex Tomas 	 */
1415c9de560dSAlex Tomas 	if (bex->fe_len < gex->fe_len)
1416c9de560dSAlex Tomas 		return;
1417c9de560dSAlex Tomas 
1418c9de560dSAlex Tomas 	if ((finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
1419c9de560dSAlex Tomas 			&& bex->fe_group == e4b->bd_group) {
1420c9de560dSAlex Tomas 		/* recheck chunk's availability - we don't know
1421c9de560dSAlex Tomas 		 * when it was found (within this lock-unlock
1422c9de560dSAlex Tomas 		 * period or not) */
1423c9de560dSAlex Tomas 		max = mb_find_extent(e4b, 0, bex->fe_start, gex->fe_len, &ex);
1424c9de560dSAlex Tomas 		if (max >= gex->fe_len) {
1425c9de560dSAlex Tomas 			ext4_mb_use_best_found(ac, e4b);
1426c9de560dSAlex Tomas 			return;
1427c9de560dSAlex Tomas 		}
1428c9de560dSAlex Tomas 	}
1429c9de560dSAlex Tomas }
1430c9de560dSAlex Tomas 
1431c9de560dSAlex Tomas /*
1432c9de560dSAlex Tomas  * The routine checks whether found extent is good enough. If it is,
1433c9de560dSAlex Tomas  * then the extent gets marked used and flag is set to the context
1434c9de560dSAlex Tomas  * to stop scanning. Otherwise, the extent is compared with the
1435c9de560dSAlex Tomas  * previous found extent and if new one is better, then it's stored
1436c9de560dSAlex Tomas  * in the context. Later, the best found extent will be used, if
1437c9de560dSAlex Tomas  * mballoc can't find good enough extent.
1438c9de560dSAlex Tomas  *
1439c9de560dSAlex Tomas  * FIXME: real allocation policy is to be designed yet!
1440c9de560dSAlex Tomas  */
1441c9de560dSAlex Tomas static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
1442c9de560dSAlex Tomas 					struct ext4_free_extent *ex,
1443c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1444c9de560dSAlex Tomas {
1445c9de560dSAlex Tomas 	struct ext4_free_extent *bex = &ac->ac_b_ex;
1446c9de560dSAlex Tomas 	struct ext4_free_extent *gex = &ac->ac_g_ex;
1447c9de560dSAlex Tomas 
1448c9de560dSAlex Tomas 	BUG_ON(ex->fe_len <= 0);
14498d03c7a0SEric Sandeen 	BUG_ON(ex->fe_len > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1450c9de560dSAlex Tomas 	BUG_ON(ex->fe_start >= EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
1451c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
1452c9de560dSAlex Tomas 
1453c9de560dSAlex Tomas 	ac->ac_found++;
1454c9de560dSAlex Tomas 
1455c9de560dSAlex Tomas 	/*
1456c9de560dSAlex Tomas 	 * The special case - take what you catch first
1457c9de560dSAlex Tomas 	 */
1458c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
1459c9de560dSAlex Tomas 		*bex = *ex;
1460c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1461c9de560dSAlex Tomas 		return;
1462c9de560dSAlex Tomas 	}
1463c9de560dSAlex Tomas 
1464c9de560dSAlex Tomas 	/*
1465c9de560dSAlex Tomas 	 * Let's check whether the chuck is good enough
1466c9de560dSAlex Tomas 	 */
1467c9de560dSAlex Tomas 	if (ex->fe_len == gex->fe_len) {
1468c9de560dSAlex Tomas 		*bex = *ex;
1469c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1470c9de560dSAlex Tomas 		return;
1471c9de560dSAlex Tomas 	}
1472c9de560dSAlex Tomas 
1473c9de560dSAlex Tomas 	/*
1474c9de560dSAlex Tomas 	 * If this is first found extent, just store it in the context
1475c9de560dSAlex Tomas 	 */
1476c9de560dSAlex Tomas 	if (bex->fe_len == 0) {
1477c9de560dSAlex Tomas 		*bex = *ex;
1478c9de560dSAlex Tomas 		return;
1479c9de560dSAlex Tomas 	}
1480c9de560dSAlex Tomas 
1481c9de560dSAlex Tomas 	/*
1482c9de560dSAlex Tomas 	 * If new found extent is better, store it in the context
1483c9de560dSAlex Tomas 	 */
1484c9de560dSAlex Tomas 	if (bex->fe_len < gex->fe_len) {
1485c9de560dSAlex Tomas 		/* if the request isn't satisfied, any found extent
1486c9de560dSAlex Tomas 		 * larger than previous best one is better */
1487c9de560dSAlex Tomas 		if (ex->fe_len > bex->fe_len)
1488c9de560dSAlex Tomas 			*bex = *ex;
1489c9de560dSAlex Tomas 	} else if (ex->fe_len > gex->fe_len) {
1490c9de560dSAlex Tomas 		/* if the request is satisfied, then we try to find
1491c9de560dSAlex Tomas 		 * an extent that still satisfy the request, but is
1492c9de560dSAlex Tomas 		 * smaller than previous one */
1493c9de560dSAlex Tomas 		if (ex->fe_len < bex->fe_len)
1494c9de560dSAlex Tomas 			*bex = *ex;
1495c9de560dSAlex Tomas 	}
1496c9de560dSAlex Tomas 
1497c9de560dSAlex Tomas 	ext4_mb_check_limits(ac, e4b, 0);
1498c9de560dSAlex Tomas }
1499c9de560dSAlex Tomas 
1500c9de560dSAlex Tomas static int ext4_mb_try_best_found(struct ext4_allocation_context *ac,
1501c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1502c9de560dSAlex Tomas {
1503c9de560dSAlex Tomas 	struct ext4_free_extent ex = ac->ac_b_ex;
1504c9de560dSAlex Tomas 	ext4_group_t group = ex.fe_group;
1505c9de560dSAlex Tomas 	int max;
1506c9de560dSAlex Tomas 	int err;
1507c9de560dSAlex Tomas 
1508c9de560dSAlex Tomas 	BUG_ON(ex.fe_len <= 0);
1509c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1510c9de560dSAlex Tomas 	if (err)
1511c9de560dSAlex Tomas 		return err;
1512c9de560dSAlex Tomas 
1513c9de560dSAlex Tomas 	ext4_lock_group(ac->ac_sb, group);
1514c9de560dSAlex Tomas 	max = mb_find_extent(e4b, 0, ex.fe_start, ex.fe_len, &ex);
1515c9de560dSAlex Tomas 
1516c9de560dSAlex Tomas 	if (max > 0) {
1517c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1518c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1519c9de560dSAlex Tomas 	}
1520c9de560dSAlex Tomas 
1521c9de560dSAlex Tomas 	ext4_unlock_group(ac->ac_sb, group);
1522c9de560dSAlex Tomas 	ext4_mb_release_desc(e4b);
1523c9de560dSAlex Tomas 
1524c9de560dSAlex Tomas 	return 0;
1525c9de560dSAlex Tomas }
1526c9de560dSAlex Tomas 
1527c9de560dSAlex Tomas static int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
1528c9de560dSAlex Tomas 				struct ext4_buddy *e4b)
1529c9de560dSAlex Tomas {
1530c9de560dSAlex Tomas 	ext4_group_t group = ac->ac_g_ex.fe_group;
1531c9de560dSAlex Tomas 	int max;
1532c9de560dSAlex Tomas 	int err;
1533c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
1534c9de560dSAlex Tomas 	struct ext4_super_block *es = sbi->s_es;
1535c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1536c9de560dSAlex Tomas 
1537c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_TRY_GOAL))
1538c9de560dSAlex Tomas 		return 0;
1539c9de560dSAlex Tomas 
1540c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
1541c9de560dSAlex Tomas 	if (err)
1542c9de560dSAlex Tomas 		return err;
1543c9de560dSAlex Tomas 
1544c9de560dSAlex Tomas 	ext4_lock_group(ac->ac_sb, group);
1545c9de560dSAlex Tomas 	max = mb_find_extent(e4b, 0, ac->ac_g_ex.fe_start,
1546c9de560dSAlex Tomas 			     ac->ac_g_ex.fe_len, &ex);
1547c9de560dSAlex Tomas 
1548c9de560dSAlex Tomas 	if (max >= ac->ac_g_ex.fe_len && ac->ac_g_ex.fe_len == sbi->s_stripe) {
1549c9de560dSAlex Tomas 		ext4_fsblk_t start;
1550c9de560dSAlex Tomas 
1551c9de560dSAlex Tomas 		start = (e4b->bd_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb)) +
1552c9de560dSAlex Tomas 			ex.fe_start + le32_to_cpu(es->s_first_data_block);
1553c9de560dSAlex Tomas 		/* use do_div to get remainder (would be 64-bit modulo) */
1554c9de560dSAlex Tomas 		if (do_div(start, sbi->s_stripe) == 0) {
1555c9de560dSAlex Tomas 			ac->ac_found++;
1556c9de560dSAlex Tomas 			ac->ac_b_ex = ex;
1557c9de560dSAlex Tomas 			ext4_mb_use_best_found(ac, e4b);
1558c9de560dSAlex Tomas 		}
1559c9de560dSAlex Tomas 	} else if (max >= ac->ac_g_ex.fe_len) {
1560c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
1561c9de560dSAlex Tomas 		BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1562c9de560dSAlex Tomas 		BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1563c9de560dSAlex Tomas 		ac->ac_found++;
1564c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1565c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1566c9de560dSAlex Tomas 	} else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
1567c9de560dSAlex Tomas 		/* Sometimes, caller may want to merge even small
1568c9de560dSAlex Tomas 		 * number of blocks to an existing extent */
1569c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
1570c9de560dSAlex Tomas 		BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
1571c9de560dSAlex Tomas 		BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
1572c9de560dSAlex Tomas 		ac->ac_found++;
1573c9de560dSAlex Tomas 		ac->ac_b_ex = ex;
1574c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1575c9de560dSAlex Tomas 	}
1576c9de560dSAlex Tomas 	ext4_unlock_group(ac->ac_sb, group);
1577c9de560dSAlex Tomas 	ext4_mb_release_desc(e4b);
1578c9de560dSAlex Tomas 
1579c9de560dSAlex Tomas 	return 0;
1580c9de560dSAlex Tomas }
1581c9de560dSAlex Tomas 
1582c9de560dSAlex Tomas /*
1583c9de560dSAlex Tomas  * The routine scans buddy structures (not bitmap!) from given order
1584c9de560dSAlex Tomas  * to max order and tries to find big enough chunk to satisfy the req
1585c9de560dSAlex Tomas  */
1586c9de560dSAlex Tomas static void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
1587c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1588c9de560dSAlex Tomas {
1589c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1590c9de560dSAlex Tomas 	struct ext4_group_info *grp = e4b->bd_info;
1591c9de560dSAlex Tomas 	void *buddy;
1592c9de560dSAlex Tomas 	int i;
1593c9de560dSAlex Tomas 	int k;
1594c9de560dSAlex Tomas 	int max;
1595c9de560dSAlex Tomas 
1596c9de560dSAlex Tomas 	BUG_ON(ac->ac_2order <= 0);
1597c9de560dSAlex Tomas 	for (i = ac->ac_2order; i <= sb->s_blocksize_bits + 1; i++) {
1598c9de560dSAlex Tomas 		if (grp->bb_counters[i] == 0)
1599c9de560dSAlex Tomas 			continue;
1600c9de560dSAlex Tomas 
1601c9de560dSAlex Tomas 		buddy = mb_find_buddy(e4b, i, &max);
1602c9de560dSAlex Tomas 		BUG_ON(buddy == NULL);
1603c9de560dSAlex Tomas 
1604ffad0a44SAneesh Kumar K.V 		k = mb_find_next_zero_bit(buddy, max, 0);
1605c9de560dSAlex Tomas 		BUG_ON(k >= max);
1606c9de560dSAlex Tomas 
1607c9de560dSAlex Tomas 		ac->ac_found++;
1608c9de560dSAlex Tomas 
1609c9de560dSAlex Tomas 		ac->ac_b_ex.fe_len = 1 << i;
1610c9de560dSAlex Tomas 		ac->ac_b_ex.fe_start = k << i;
1611c9de560dSAlex Tomas 		ac->ac_b_ex.fe_group = e4b->bd_group;
1612c9de560dSAlex Tomas 
1613c9de560dSAlex Tomas 		ext4_mb_use_best_found(ac, e4b);
1614c9de560dSAlex Tomas 
1615c9de560dSAlex Tomas 		BUG_ON(ac->ac_b_ex.fe_len != ac->ac_g_ex.fe_len);
1616c9de560dSAlex Tomas 
1617c9de560dSAlex Tomas 		if (EXT4_SB(sb)->s_mb_stats)
1618c9de560dSAlex Tomas 			atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
1619c9de560dSAlex Tomas 
1620c9de560dSAlex Tomas 		break;
1621c9de560dSAlex Tomas 	}
1622c9de560dSAlex Tomas }
1623c9de560dSAlex Tomas 
1624c9de560dSAlex Tomas /*
1625c9de560dSAlex Tomas  * The routine scans the group and measures all found extents.
1626c9de560dSAlex Tomas  * In order to optimize scanning, caller must pass number of
1627c9de560dSAlex Tomas  * free blocks in the group, so the routine can know upper limit.
1628c9de560dSAlex Tomas  */
1629c9de560dSAlex Tomas static void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
1630c9de560dSAlex Tomas 					struct ext4_buddy *e4b)
1631c9de560dSAlex Tomas {
1632c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1633c9de560dSAlex Tomas 	void *bitmap = EXT4_MB_BITMAP(e4b);
1634c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1635c9de560dSAlex Tomas 	int i;
1636c9de560dSAlex Tomas 	int free;
1637c9de560dSAlex Tomas 
1638c9de560dSAlex Tomas 	free = e4b->bd_info->bb_free;
1639c9de560dSAlex Tomas 	BUG_ON(free <= 0);
1640c9de560dSAlex Tomas 
1641c9de560dSAlex Tomas 	i = e4b->bd_info->bb_first_free;
1642c9de560dSAlex Tomas 
1643c9de560dSAlex Tomas 	while (free && ac->ac_status == AC_STATUS_CONTINUE) {
1644ffad0a44SAneesh Kumar K.V 		i = mb_find_next_zero_bit(bitmap,
1645c9de560dSAlex Tomas 						EXT4_BLOCKS_PER_GROUP(sb), i);
1646c9de560dSAlex Tomas 		if (i >= EXT4_BLOCKS_PER_GROUP(sb)) {
164726346ff6SAneesh Kumar K.V 			/*
1648e56eb659SAneesh Kumar K.V 			 * IF we have corrupt bitmap, we won't find any
164926346ff6SAneesh Kumar K.V 			 * free blocks even though group info says we
165026346ff6SAneesh Kumar K.V 			 * we have free blocks
165126346ff6SAneesh Kumar K.V 			 */
16525d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
16535d1b1b3fSAneesh Kumar K.V 					__func__, "%d free blocks as per "
1654fde4d95aSTheodore Ts'o 					"group info. But bitmap says 0",
165526346ff6SAneesh Kumar K.V 					free);
1656c9de560dSAlex Tomas 			break;
1657c9de560dSAlex Tomas 		}
1658c9de560dSAlex Tomas 
1659c9de560dSAlex Tomas 		mb_find_extent(e4b, 0, i, ac->ac_g_ex.fe_len, &ex);
1660c9de560dSAlex Tomas 		BUG_ON(ex.fe_len <= 0);
166126346ff6SAneesh Kumar K.V 		if (free < ex.fe_len) {
16625d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group,
16635d1b1b3fSAneesh Kumar K.V 					__func__, "%d free blocks as per "
1664fde4d95aSTheodore Ts'o 					"group info. But got %d blocks",
166526346ff6SAneesh Kumar K.V 					free, ex.fe_len);
1666e56eb659SAneesh Kumar K.V 			/*
1667e56eb659SAneesh Kumar K.V 			 * The number of free blocks differs. This mostly
1668e56eb659SAneesh Kumar K.V 			 * indicate that the bitmap is corrupt. So exit
1669e56eb659SAneesh Kumar K.V 			 * without claiming the space.
1670e56eb659SAneesh Kumar K.V 			 */
1671e56eb659SAneesh Kumar K.V 			break;
167226346ff6SAneesh Kumar K.V 		}
1673c9de560dSAlex Tomas 
1674c9de560dSAlex Tomas 		ext4_mb_measure_extent(ac, &ex, e4b);
1675c9de560dSAlex Tomas 
1676c9de560dSAlex Tomas 		i += ex.fe_len;
1677c9de560dSAlex Tomas 		free -= ex.fe_len;
1678c9de560dSAlex Tomas 	}
1679c9de560dSAlex Tomas 
1680c9de560dSAlex Tomas 	ext4_mb_check_limits(ac, e4b, 1);
1681c9de560dSAlex Tomas }
1682c9de560dSAlex Tomas 
1683c9de560dSAlex Tomas /*
1684c9de560dSAlex Tomas  * This is a special case for storages like raid5
1685c9de560dSAlex Tomas  * we try to find stripe-aligned chunks for stripe-size requests
1686c9de560dSAlex Tomas  * XXX should do so at least for multiples of stripe size as well
1687c9de560dSAlex Tomas  */
1688c9de560dSAlex Tomas static void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
1689c9de560dSAlex Tomas 				 struct ext4_buddy *e4b)
1690c9de560dSAlex Tomas {
1691c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
1692c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1693c9de560dSAlex Tomas 	void *bitmap = EXT4_MB_BITMAP(e4b);
1694c9de560dSAlex Tomas 	struct ext4_free_extent ex;
1695c9de560dSAlex Tomas 	ext4_fsblk_t first_group_block;
1696c9de560dSAlex Tomas 	ext4_fsblk_t a;
1697c9de560dSAlex Tomas 	ext4_grpblk_t i;
1698c9de560dSAlex Tomas 	int max;
1699c9de560dSAlex Tomas 
1700c9de560dSAlex Tomas 	BUG_ON(sbi->s_stripe == 0);
1701c9de560dSAlex Tomas 
1702c9de560dSAlex Tomas 	/* find first stripe-aligned block in group */
1703c9de560dSAlex Tomas 	first_group_block = e4b->bd_group * EXT4_BLOCKS_PER_GROUP(sb)
1704c9de560dSAlex Tomas 		+ le32_to_cpu(sbi->s_es->s_first_data_block);
1705c9de560dSAlex Tomas 	a = first_group_block + sbi->s_stripe - 1;
1706c9de560dSAlex Tomas 	do_div(a, sbi->s_stripe);
1707c9de560dSAlex Tomas 	i = (a * sbi->s_stripe) - first_group_block;
1708c9de560dSAlex Tomas 
1709c9de560dSAlex Tomas 	while (i < EXT4_BLOCKS_PER_GROUP(sb)) {
1710c9de560dSAlex Tomas 		if (!mb_test_bit(i, bitmap)) {
1711c9de560dSAlex Tomas 			max = mb_find_extent(e4b, 0, i, sbi->s_stripe, &ex);
1712c9de560dSAlex Tomas 			if (max >= sbi->s_stripe) {
1713c9de560dSAlex Tomas 				ac->ac_found++;
1714c9de560dSAlex Tomas 				ac->ac_b_ex = ex;
1715c9de560dSAlex Tomas 				ext4_mb_use_best_found(ac, e4b);
1716c9de560dSAlex Tomas 				break;
1717c9de560dSAlex Tomas 			}
1718c9de560dSAlex Tomas 		}
1719c9de560dSAlex Tomas 		i += sbi->s_stripe;
1720c9de560dSAlex Tomas 	}
1721c9de560dSAlex Tomas }
1722c9de560dSAlex Tomas 
1723c9de560dSAlex Tomas static int ext4_mb_good_group(struct ext4_allocation_context *ac,
1724c9de560dSAlex Tomas 				ext4_group_t group, int cr)
1725c9de560dSAlex Tomas {
1726c9de560dSAlex Tomas 	unsigned free, fragments;
1727c9de560dSAlex Tomas 	unsigned i, bits;
1728a4912123STheodore Ts'o 	int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
1729c9de560dSAlex Tomas 	struct ext4_group_desc *desc;
1730c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
1731c9de560dSAlex Tomas 
1732c9de560dSAlex Tomas 	BUG_ON(cr < 0 || cr >= 4);
1733c9de560dSAlex Tomas 	BUG_ON(EXT4_MB_GRP_NEED_INIT(grp));
1734c9de560dSAlex Tomas 
1735c9de560dSAlex Tomas 	free = grp->bb_free;
1736c9de560dSAlex Tomas 	fragments = grp->bb_fragments;
1737c9de560dSAlex Tomas 	if (free == 0)
1738c9de560dSAlex Tomas 		return 0;
1739c9de560dSAlex Tomas 	if (fragments == 0)
1740c9de560dSAlex Tomas 		return 0;
1741c9de560dSAlex Tomas 
1742c9de560dSAlex Tomas 	switch (cr) {
1743c9de560dSAlex Tomas 	case 0:
1744c9de560dSAlex Tomas 		BUG_ON(ac->ac_2order == 0);
1745c9de560dSAlex Tomas 		/* If this group is uninitialized, skip it initially */
1746c9de560dSAlex Tomas 		desc = ext4_get_group_desc(ac->ac_sb, group, NULL);
1747c9de560dSAlex Tomas 		if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
1748c9de560dSAlex Tomas 			return 0;
1749c9de560dSAlex Tomas 
1750a4912123STheodore Ts'o 		/* Avoid using the first bg of a flexgroup for data files */
1751a4912123STheodore Ts'o 		if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
1752a4912123STheodore Ts'o 		    (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
1753a4912123STheodore Ts'o 		    ((group % flex_size) == 0))
1754a4912123STheodore Ts'o 			return 0;
1755a4912123STheodore Ts'o 
1756c9de560dSAlex Tomas 		bits = ac->ac_sb->s_blocksize_bits + 1;
1757c9de560dSAlex Tomas 		for (i = ac->ac_2order; i <= bits; i++)
1758c9de560dSAlex Tomas 			if (grp->bb_counters[i] > 0)
1759c9de560dSAlex Tomas 				return 1;
1760c9de560dSAlex Tomas 		break;
1761c9de560dSAlex Tomas 	case 1:
1762c9de560dSAlex Tomas 		if ((free / fragments) >= ac->ac_g_ex.fe_len)
1763c9de560dSAlex Tomas 			return 1;
1764c9de560dSAlex Tomas 		break;
1765c9de560dSAlex Tomas 	case 2:
1766c9de560dSAlex Tomas 		if (free >= ac->ac_g_ex.fe_len)
1767c9de560dSAlex Tomas 			return 1;
1768c9de560dSAlex Tomas 		break;
1769c9de560dSAlex Tomas 	case 3:
1770c9de560dSAlex Tomas 		return 1;
1771c9de560dSAlex Tomas 	default:
1772c9de560dSAlex Tomas 		BUG();
1773c9de560dSAlex Tomas 	}
1774c9de560dSAlex Tomas 
1775c9de560dSAlex Tomas 	return 0;
1776c9de560dSAlex Tomas }
1777c9de560dSAlex Tomas 
1778920313a7SAneesh Kumar K.V /*
1779920313a7SAneesh Kumar K.V  * lock the group_info alloc_sem of all the groups
1780920313a7SAneesh Kumar K.V  * belonging to the same buddy cache page. This
1781920313a7SAneesh Kumar K.V  * make sure other parallel operation on the buddy
1782920313a7SAneesh Kumar K.V  * cache doesn't happen  whild holding the buddy cache
1783920313a7SAneesh Kumar K.V  * lock
1784920313a7SAneesh Kumar K.V  */
1785920313a7SAneesh Kumar K.V int ext4_mb_get_buddy_cache_lock(struct super_block *sb, ext4_group_t group)
1786920313a7SAneesh Kumar K.V {
1787920313a7SAneesh Kumar K.V 	int i;
1788920313a7SAneesh Kumar K.V 	int block, pnum;
1789920313a7SAneesh Kumar K.V 	int blocks_per_page;
1790920313a7SAneesh Kumar K.V 	int groups_per_page;
1791920313a7SAneesh Kumar K.V 	ext4_group_t first_group;
1792920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
1793920313a7SAneesh Kumar K.V 
1794920313a7SAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1795920313a7SAneesh Kumar K.V 	/*
1796920313a7SAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
1797920313a7SAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
1798920313a7SAneesh Kumar K.V 	 * So for each group we need two blocks.
1799920313a7SAneesh Kumar K.V 	 */
1800920313a7SAneesh Kumar K.V 	block = group * 2;
1801920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1802920313a7SAneesh Kumar K.V 	first_group = pnum * blocks_per_page / 2;
1803920313a7SAneesh Kumar K.V 
1804920313a7SAneesh Kumar K.V 	groups_per_page = blocks_per_page >> 1;
1805920313a7SAneesh Kumar K.V 	if (groups_per_page == 0)
1806920313a7SAneesh Kumar K.V 		groups_per_page = 1;
1807920313a7SAneesh Kumar K.V 	/* read all groups the page covers into the cache */
1808920313a7SAneesh Kumar K.V 	for (i = 0; i < groups_per_page; i++) {
1809920313a7SAneesh Kumar K.V 
1810920313a7SAneesh Kumar K.V 		if ((first_group + i) >= EXT4_SB(sb)->s_groups_count)
1811920313a7SAneesh Kumar K.V 			break;
1812920313a7SAneesh Kumar K.V 		grp = ext4_get_group_info(sb, first_group + i);
1813920313a7SAneesh Kumar K.V 		/* take all groups write allocation
1814920313a7SAneesh Kumar K.V 		 * semaphore. This make sure there is
1815920313a7SAneesh Kumar K.V 		 * no block allocation going on in any
1816920313a7SAneesh Kumar K.V 		 * of that groups
1817920313a7SAneesh Kumar K.V 		 */
1818b7be019eSAneesh Kumar K.V 		down_write_nested(&grp->alloc_sem, i);
1819920313a7SAneesh Kumar K.V 	}
1820920313a7SAneesh Kumar K.V 	return i;
1821920313a7SAneesh Kumar K.V }
1822920313a7SAneesh Kumar K.V 
1823920313a7SAneesh Kumar K.V void ext4_mb_put_buddy_cache_lock(struct super_block *sb,
1824920313a7SAneesh Kumar K.V 					ext4_group_t group, int locked_group)
1825920313a7SAneesh Kumar K.V {
1826920313a7SAneesh Kumar K.V 	int i;
1827920313a7SAneesh Kumar K.V 	int block, pnum;
1828920313a7SAneesh Kumar K.V 	int blocks_per_page;
1829920313a7SAneesh Kumar K.V 	ext4_group_t first_group;
1830920313a7SAneesh Kumar K.V 	struct ext4_group_info *grp;
1831920313a7SAneesh Kumar K.V 
1832920313a7SAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1833920313a7SAneesh Kumar K.V 	/*
1834920313a7SAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
1835920313a7SAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
1836920313a7SAneesh Kumar K.V 	 * So for each group we need two blocks.
1837920313a7SAneesh Kumar K.V 	 */
1838920313a7SAneesh Kumar K.V 	block = group * 2;
1839920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1840920313a7SAneesh Kumar K.V 	first_group = pnum * blocks_per_page / 2;
1841920313a7SAneesh Kumar K.V 	/* release locks on all the groups */
1842920313a7SAneesh Kumar K.V 	for (i = 0; i < locked_group; i++) {
1843920313a7SAneesh Kumar K.V 
1844920313a7SAneesh Kumar K.V 		grp = ext4_get_group_info(sb, first_group + i);
1845920313a7SAneesh Kumar K.V 		/* take all groups write allocation
1846920313a7SAneesh Kumar K.V 		 * semaphore. This make sure there is
1847920313a7SAneesh Kumar K.V 		 * no block allocation going on in any
1848920313a7SAneesh Kumar K.V 		 * of that groups
1849920313a7SAneesh Kumar K.V 		 */
1850920313a7SAneesh Kumar K.V 		up_write(&grp->alloc_sem);
1851920313a7SAneesh Kumar K.V 	}
1852920313a7SAneesh Kumar K.V 
1853920313a7SAneesh Kumar K.V }
1854920313a7SAneesh Kumar K.V 
1855920313a7SAneesh Kumar K.V static int ext4_mb_init_group(struct super_block *sb, ext4_group_t group)
1856920313a7SAneesh Kumar K.V {
1857920313a7SAneesh Kumar K.V 
1858920313a7SAneesh Kumar K.V 	int ret;
1859920313a7SAneesh Kumar K.V 	void *bitmap;
1860920313a7SAneesh Kumar K.V 	int blocks_per_page;
1861920313a7SAneesh Kumar K.V 	int block, pnum, poff;
1862920313a7SAneesh Kumar K.V 	int num_grp_locked = 0;
1863920313a7SAneesh Kumar K.V 	struct ext4_group_info *this_grp;
1864920313a7SAneesh Kumar K.V 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1865920313a7SAneesh Kumar K.V 	struct inode *inode = sbi->s_buddy_cache;
1866920313a7SAneesh Kumar K.V 	struct page *page = NULL, *bitmap_page = NULL;
1867920313a7SAneesh Kumar K.V 
1868920313a7SAneesh Kumar K.V 	mb_debug("init group %lu\n", group);
1869920313a7SAneesh Kumar K.V 	blocks_per_page = PAGE_CACHE_SIZE / sb->s_blocksize;
1870920313a7SAneesh Kumar K.V 	this_grp = ext4_get_group_info(sb, group);
1871920313a7SAneesh Kumar K.V 	/*
1872920313a7SAneesh Kumar K.V 	 * This ensures we don't add group
1873920313a7SAneesh Kumar K.V 	 * to this buddy cache via resize
1874920313a7SAneesh Kumar K.V 	 */
1875920313a7SAneesh Kumar K.V 	num_grp_locked =  ext4_mb_get_buddy_cache_lock(sb, group);
1876920313a7SAneesh Kumar K.V 	if (!EXT4_MB_GRP_NEED_INIT(this_grp)) {
1877920313a7SAneesh Kumar K.V 		/*
1878920313a7SAneesh Kumar K.V 		 * somebody initialized the group
1879920313a7SAneesh Kumar K.V 		 * return without doing anything
1880920313a7SAneesh Kumar K.V 		 */
1881920313a7SAneesh Kumar K.V 		ret = 0;
1882920313a7SAneesh Kumar K.V 		goto err;
1883920313a7SAneesh Kumar K.V 	}
1884920313a7SAneesh Kumar K.V 	/*
1885920313a7SAneesh Kumar K.V 	 * the buddy cache inode stores the block bitmap
1886920313a7SAneesh Kumar K.V 	 * and buddy information in consecutive blocks.
1887920313a7SAneesh Kumar K.V 	 * So for each group we need two blocks.
1888920313a7SAneesh Kumar K.V 	 */
1889920313a7SAneesh Kumar K.V 	block = group * 2;
1890920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1891920313a7SAneesh Kumar K.V 	poff = block % blocks_per_page;
1892920313a7SAneesh Kumar K.V 	page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1893920313a7SAneesh Kumar K.V 	if (page) {
1894920313a7SAneesh Kumar K.V 		BUG_ON(page->mapping != inode->i_mapping);
1895920313a7SAneesh Kumar K.V 		ret = ext4_mb_init_cache(page, NULL);
1896920313a7SAneesh Kumar K.V 		if (ret) {
1897920313a7SAneesh Kumar K.V 			unlock_page(page);
1898920313a7SAneesh Kumar K.V 			goto err;
1899920313a7SAneesh Kumar K.V 		}
1900920313a7SAneesh Kumar K.V 		unlock_page(page);
1901920313a7SAneesh Kumar K.V 	}
1902920313a7SAneesh Kumar K.V 	if (page == NULL || !PageUptodate(page)) {
1903920313a7SAneesh Kumar K.V 		ret = -EIO;
1904920313a7SAneesh Kumar K.V 		goto err;
1905920313a7SAneesh Kumar K.V 	}
1906920313a7SAneesh Kumar K.V 	mark_page_accessed(page);
1907920313a7SAneesh Kumar K.V 	bitmap_page = page;
1908920313a7SAneesh Kumar K.V 	bitmap = page_address(page) + (poff * sb->s_blocksize);
1909920313a7SAneesh Kumar K.V 
1910920313a7SAneesh Kumar K.V 	/* init buddy cache */
1911920313a7SAneesh Kumar K.V 	block++;
1912920313a7SAneesh Kumar K.V 	pnum = block / blocks_per_page;
1913920313a7SAneesh Kumar K.V 	poff = block % blocks_per_page;
1914920313a7SAneesh Kumar K.V 	page = find_or_create_page(inode->i_mapping, pnum, GFP_NOFS);
1915920313a7SAneesh Kumar K.V 	if (page == bitmap_page) {
1916920313a7SAneesh Kumar K.V 		/*
1917920313a7SAneesh Kumar K.V 		 * If both the bitmap and buddy are in
1918920313a7SAneesh Kumar K.V 		 * the same page we don't need to force
1919920313a7SAneesh Kumar K.V 		 * init the buddy
1920920313a7SAneesh Kumar K.V 		 */
1921920313a7SAneesh Kumar K.V 		unlock_page(page);
1922920313a7SAneesh Kumar K.V 	} else if (page) {
1923920313a7SAneesh Kumar K.V 		BUG_ON(page->mapping != inode->i_mapping);
1924920313a7SAneesh Kumar K.V 		ret = ext4_mb_init_cache(page, bitmap);
1925920313a7SAneesh Kumar K.V 		if (ret) {
1926920313a7SAneesh Kumar K.V 			unlock_page(page);
1927920313a7SAneesh Kumar K.V 			goto err;
1928920313a7SAneesh Kumar K.V 		}
1929920313a7SAneesh Kumar K.V 		unlock_page(page);
1930920313a7SAneesh Kumar K.V 	}
1931920313a7SAneesh Kumar K.V 	if (page == NULL || !PageUptodate(page)) {
1932920313a7SAneesh Kumar K.V 		ret = -EIO;
1933920313a7SAneesh Kumar K.V 		goto err;
1934920313a7SAneesh Kumar K.V 	}
1935920313a7SAneesh Kumar K.V 	mark_page_accessed(page);
1936920313a7SAneesh Kumar K.V err:
1937920313a7SAneesh Kumar K.V 	ext4_mb_put_buddy_cache_lock(sb, group, num_grp_locked);
1938920313a7SAneesh Kumar K.V 	if (bitmap_page)
1939920313a7SAneesh Kumar K.V 		page_cache_release(bitmap_page);
1940920313a7SAneesh Kumar K.V 	if (page)
1941920313a7SAneesh Kumar K.V 		page_cache_release(page);
1942920313a7SAneesh Kumar K.V 	return ret;
1943920313a7SAneesh Kumar K.V }
1944920313a7SAneesh Kumar K.V 
19454ddfef7bSEric Sandeen static noinline_for_stack int
19464ddfef7bSEric Sandeen ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
1947c9de560dSAlex Tomas {
1948c9de560dSAlex Tomas 	ext4_group_t group;
1949c9de560dSAlex Tomas 	ext4_group_t i;
1950c9de560dSAlex Tomas 	int cr;
1951c9de560dSAlex Tomas 	int err = 0;
1952c9de560dSAlex Tomas 	int bsbits;
1953c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
1954c9de560dSAlex Tomas 	struct super_block *sb;
1955c9de560dSAlex Tomas 	struct ext4_buddy e4b;
1956c9de560dSAlex Tomas 	loff_t size, isize;
1957c9de560dSAlex Tomas 
1958c9de560dSAlex Tomas 	sb = ac->ac_sb;
1959c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
1960c9de560dSAlex Tomas 	BUG_ON(ac->ac_status == AC_STATUS_FOUND);
1961c9de560dSAlex Tomas 
1962c9de560dSAlex Tomas 	/* first, try the goal */
1963c9de560dSAlex Tomas 	err = ext4_mb_find_by_goal(ac, &e4b);
1964c9de560dSAlex Tomas 	if (err || ac->ac_status == AC_STATUS_FOUND)
1965c9de560dSAlex Tomas 		goto out;
1966c9de560dSAlex Tomas 
1967c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
1968c9de560dSAlex Tomas 		goto out;
1969c9de560dSAlex Tomas 
1970c9de560dSAlex Tomas 	/*
1971c9de560dSAlex Tomas 	 * ac->ac2_order is set only if the fe_len is a power of 2
1972c9de560dSAlex Tomas 	 * if ac2_order is set we also set criteria to 0 so that we
1973c9de560dSAlex Tomas 	 * try exact allocation using buddy.
1974c9de560dSAlex Tomas 	 */
1975c9de560dSAlex Tomas 	i = fls(ac->ac_g_ex.fe_len);
1976c9de560dSAlex Tomas 	ac->ac_2order = 0;
1977c9de560dSAlex Tomas 	/*
1978c9de560dSAlex Tomas 	 * We search using buddy data only if the order of the request
1979c9de560dSAlex Tomas 	 * is greater than equal to the sbi_s_mb_order2_reqs
1980b713a5ecSTheodore Ts'o 	 * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
1981c9de560dSAlex Tomas 	 */
1982c9de560dSAlex Tomas 	if (i >= sbi->s_mb_order2_reqs) {
1983c9de560dSAlex Tomas 		/*
1984c9de560dSAlex Tomas 		 * This should tell if fe_len is exactly power of 2
1985c9de560dSAlex Tomas 		 */
1986c9de560dSAlex Tomas 		if ((ac->ac_g_ex.fe_len & (~(1 << (i - 1)))) == 0)
1987c9de560dSAlex Tomas 			ac->ac_2order = i - 1;
1988c9de560dSAlex Tomas 	}
1989c9de560dSAlex Tomas 
1990c9de560dSAlex Tomas 	bsbits = ac->ac_sb->s_blocksize_bits;
1991c9de560dSAlex Tomas 	/* if stream allocation is enabled, use global goal */
1992c9de560dSAlex Tomas 	size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
1993c9de560dSAlex Tomas 	isize = i_size_read(ac->ac_inode) >> bsbits;
1994c9de560dSAlex Tomas 	if (size < isize)
1995c9de560dSAlex Tomas 		size = isize;
1996c9de560dSAlex Tomas 
1997c9de560dSAlex Tomas 	if (size < sbi->s_mb_stream_request &&
1998c9de560dSAlex Tomas 			(ac->ac_flags & EXT4_MB_HINT_DATA)) {
1999c9de560dSAlex Tomas 		/* TBD: may be hot point */
2000c9de560dSAlex Tomas 		spin_lock(&sbi->s_md_lock);
2001c9de560dSAlex Tomas 		ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2002c9de560dSAlex Tomas 		ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2003c9de560dSAlex Tomas 		spin_unlock(&sbi->s_md_lock);
2004c9de560dSAlex Tomas 	}
2005c9de560dSAlex Tomas 	/* Let's just scan groups to find more-less suitable blocks */
2006c9de560dSAlex Tomas 	cr = ac->ac_2order ? 0 : 1;
2007c9de560dSAlex Tomas 	/*
2008c9de560dSAlex Tomas 	 * cr == 0 try to get exact allocation,
2009c9de560dSAlex Tomas 	 * cr == 3  try to get anything
2010c9de560dSAlex Tomas 	 */
2011c9de560dSAlex Tomas repeat:
2012c9de560dSAlex Tomas 	for (; cr < 4 && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2013c9de560dSAlex Tomas 		ac->ac_criteria = cr;
2014ed8f9c75SAneesh Kumar K.V 		/*
2015ed8f9c75SAneesh Kumar K.V 		 * searching for the right group start
2016ed8f9c75SAneesh Kumar K.V 		 * from the goal value specified
2017ed8f9c75SAneesh Kumar K.V 		 */
2018ed8f9c75SAneesh Kumar K.V 		group = ac->ac_g_ex.fe_group;
2019ed8f9c75SAneesh Kumar K.V 
2020c9de560dSAlex Tomas 		for (i = 0; i < EXT4_SB(sb)->s_groups_count; group++, i++) {
2021c9de560dSAlex Tomas 			struct ext4_group_info *grp;
2022c9de560dSAlex Tomas 			struct ext4_group_desc *desc;
2023c9de560dSAlex Tomas 
2024c9de560dSAlex Tomas 			if (group == EXT4_SB(sb)->s_groups_count)
2025c9de560dSAlex Tomas 				group = 0;
2026c9de560dSAlex Tomas 
2027c9de560dSAlex Tomas 			/* quick check to skip empty groups */
2028920313a7SAneesh Kumar K.V 			grp = ext4_get_group_info(sb, group);
2029c9de560dSAlex Tomas 			if (grp->bb_free == 0)
2030c9de560dSAlex Tomas 				continue;
2031c9de560dSAlex Tomas 
2032c9de560dSAlex Tomas 			/*
2033c9de560dSAlex Tomas 			 * if the group is already init we check whether it is
2034c9de560dSAlex Tomas 			 * a good group and if not we don't load the buddy
2035c9de560dSAlex Tomas 			 */
2036c9de560dSAlex Tomas 			if (EXT4_MB_GRP_NEED_INIT(grp)) {
2037c9de560dSAlex Tomas 				/*
2038c9de560dSAlex Tomas 				 * we need full data about the group
2039c9de560dSAlex Tomas 				 * to make a good selection
2040c9de560dSAlex Tomas 				 */
2041920313a7SAneesh Kumar K.V 				err = ext4_mb_init_group(sb, group);
2042c9de560dSAlex Tomas 				if (err)
2043c9de560dSAlex Tomas 					goto out;
2044c9de560dSAlex Tomas 			}
2045c9de560dSAlex Tomas 
2046c9de560dSAlex Tomas 			/*
2047c9de560dSAlex Tomas 			 * If the particular group doesn't satisfy our
2048c9de560dSAlex Tomas 			 * criteria we continue with the next group
2049c9de560dSAlex Tomas 			 */
2050c9de560dSAlex Tomas 			if (!ext4_mb_good_group(ac, group, cr))
2051c9de560dSAlex Tomas 				continue;
2052c9de560dSAlex Tomas 
2053c9de560dSAlex Tomas 			err = ext4_mb_load_buddy(sb, group, &e4b);
2054c9de560dSAlex Tomas 			if (err)
2055c9de560dSAlex Tomas 				goto out;
2056c9de560dSAlex Tomas 
2057c9de560dSAlex Tomas 			ext4_lock_group(sb, group);
2058c9de560dSAlex Tomas 			if (!ext4_mb_good_group(ac, group, cr)) {
2059c9de560dSAlex Tomas 				/* someone did allocation from this group */
2060c9de560dSAlex Tomas 				ext4_unlock_group(sb, group);
2061c9de560dSAlex Tomas 				ext4_mb_release_desc(&e4b);
2062c9de560dSAlex Tomas 				continue;
2063c9de560dSAlex Tomas 			}
2064c9de560dSAlex Tomas 
2065c9de560dSAlex Tomas 			ac->ac_groups_scanned++;
2066c9de560dSAlex Tomas 			desc = ext4_get_group_desc(sb, group, NULL);
2067c9de560dSAlex Tomas 			if (cr == 0 || (desc->bg_flags &
2068c9de560dSAlex Tomas 					cpu_to_le16(EXT4_BG_BLOCK_UNINIT) &&
2069c9de560dSAlex Tomas 					ac->ac_2order != 0))
2070c9de560dSAlex Tomas 				ext4_mb_simple_scan_group(ac, &e4b);
2071c9de560dSAlex Tomas 			else if (cr == 1 &&
2072c9de560dSAlex Tomas 					ac->ac_g_ex.fe_len == sbi->s_stripe)
2073c9de560dSAlex Tomas 				ext4_mb_scan_aligned(ac, &e4b);
2074c9de560dSAlex Tomas 			else
2075c9de560dSAlex Tomas 				ext4_mb_complex_scan_group(ac, &e4b);
2076c9de560dSAlex Tomas 
2077c9de560dSAlex Tomas 			ext4_unlock_group(sb, group);
2078c9de560dSAlex Tomas 			ext4_mb_release_desc(&e4b);
2079c9de560dSAlex Tomas 
2080c9de560dSAlex Tomas 			if (ac->ac_status != AC_STATUS_CONTINUE)
2081c9de560dSAlex Tomas 				break;
2082c9de560dSAlex Tomas 		}
2083c9de560dSAlex Tomas 	}
2084c9de560dSAlex Tomas 
2085c9de560dSAlex Tomas 	if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2086c9de560dSAlex Tomas 	    !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2087c9de560dSAlex Tomas 		/*
2088c9de560dSAlex Tomas 		 * We've been searching too long. Let's try to allocate
2089c9de560dSAlex Tomas 		 * the best chunk we've found so far
2090c9de560dSAlex Tomas 		 */
2091c9de560dSAlex Tomas 
2092c9de560dSAlex Tomas 		ext4_mb_try_best_found(ac, &e4b);
2093c9de560dSAlex Tomas 		if (ac->ac_status != AC_STATUS_FOUND) {
2094c9de560dSAlex Tomas 			/*
2095c9de560dSAlex Tomas 			 * Someone more lucky has already allocated it.
2096c9de560dSAlex Tomas 			 * The only thing we can do is just take first
2097c9de560dSAlex Tomas 			 * found block(s)
2098c9de560dSAlex Tomas 			printk(KERN_DEBUG "EXT4-fs: someone won our chunk\n");
2099c9de560dSAlex Tomas 			 */
2100c9de560dSAlex Tomas 			ac->ac_b_ex.fe_group = 0;
2101c9de560dSAlex Tomas 			ac->ac_b_ex.fe_start = 0;
2102c9de560dSAlex Tomas 			ac->ac_b_ex.fe_len = 0;
2103c9de560dSAlex Tomas 			ac->ac_status = AC_STATUS_CONTINUE;
2104c9de560dSAlex Tomas 			ac->ac_flags |= EXT4_MB_HINT_FIRST;
2105c9de560dSAlex Tomas 			cr = 3;
2106c9de560dSAlex Tomas 			atomic_inc(&sbi->s_mb_lost_chunks);
2107c9de560dSAlex Tomas 			goto repeat;
2108c9de560dSAlex Tomas 		}
2109c9de560dSAlex Tomas 	}
2110c9de560dSAlex Tomas out:
2111c9de560dSAlex Tomas 	return err;
2112c9de560dSAlex Tomas }
2113c9de560dSAlex Tomas 
2114c9de560dSAlex Tomas #ifdef EXT4_MB_HISTORY
2115c9de560dSAlex Tomas struct ext4_mb_proc_session {
2116c9de560dSAlex Tomas 	struct ext4_mb_history *history;
2117c9de560dSAlex Tomas 	struct super_block *sb;
2118c9de560dSAlex Tomas 	int start;
2119c9de560dSAlex Tomas 	int max;
2120c9de560dSAlex Tomas };
2121c9de560dSAlex Tomas 
2122c9de560dSAlex Tomas static void *ext4_mb_history_skip_empty(struct ext4_mb_proc_session *s,
2123c9de560dSAlex Tomas 					struct ext4_mb_history *hs,
2124c9de560dSAlex Tomas 					int first)
2125c9de560dSAlex Tomas {
2126c9de560dSAlex Tomas 	if (hs == s->history + s->max)
2127c9de560dSAlex Tomas 		hs = s->history;
2128c9de560dSAlex Tomas 	if (!first && hs == s->history + s->start)
2129c9de560dSAlex Tomas 		return NULL;
2130c9de560dSAlex Tomas 	while (hs->orig.fe_len == 0) {
2131c9de560dSAlex Tomas 		hs++;
2132c9de560dSAlex Tomas 		if (hs == s->history + s->max)
2133c9de560dSAlex Tomas 			hs = s->history;
2134c9de560dSAlex Tomas 		if (hs == s->history + s->start)
2135c9de560dSAlex Tomas 			return NULL;
2136c9de560dSAlex Tomas 	}
2137c9de560dSAlex Tomas 	return hs;
2138c9de560dSAlex Tomas }
2139c9de560dSAlex Tomas 
2140c9de560dSAlex Tomas static void *ext4_mb_seq_history_start(struct seq_file *seq, loff_t *pos)
2141c9de560dSAlex Tomas {
2142c9de560dSAlex Tomas 	struct ext4_mb_proc_session *s = seq->private;
2143c9de560dSAlex Tomas 	struct ext4_mb_history *hs;
2144c9de560dSAlex Tomas 	int l = *pos;
2145c9de560dSAlex Tomas 
2146c9de560dSAlex Tomas 	if (l == 0)
2147c9de560dSAlex Tomas 		return SEQ_START_TOKEN;
2148c9de560dSAlex Tomas 	hs = ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2149c9de560dSAlex Tomas 	if (!hs)
2150c9de560dSAlex Tomas 		return NULL;
2151c9de560dSAlex Tomas 	while (--l && (hs = ext4_mb_history_skip_empty(s, ++hs, 0)) != NULL);
2152c9de560dSAlex Tomas 	return hs;
2153c9de560dSAlex Tomas }
2154c9de560dSAlex Tomas 
2155c9de560dSAlex Tomas static void *ext4_mb_seq_history_next(struct seq_file *seq, void *v,
2156c9de560dSAlex Tomas 				      loff_t *pos)
2157c9de560dSAlex Tomas {
2158c9de560dSAlex Tomas 	struct ext4_mb_proc_session *s = seq->private;
2159c9de560dSAlex Tomas 	struct ext4_mb_history *hs = v;
2160c9de560dSAlex Tomas 
2161c9de560dSAlex Tomas 	++*pos;
2162c9de560dSAlex Tomas 	if (v == SEQ_START_TOKEN)
2163c9de560dSAlex Tomas 		return ext4_mb_history_skip_empty(s, s->history + s->start, 1);
2164c9de560dSAlex Tomas 	else
2165c9de560dSAlex Tomas 		return ext4_mb_history_skip_empty(s, ++hs, 0);
2166c9de560dSAlex Tomas }
2167c9de560dSAlex Tomas 
2168c9de560dSAlex Tomas static int ext4_mb_seq_history_show(struct seq_file *seq, void *v)
2169c9de560dSAlex Tomas {
2170c9de560dSAlex Tomas 	char buf[25], buf2[25], buf3[25], *fmt;
2171c9de560dSAlex Tomas 	struct ext4_mb_history *hs = v;
2172c9de560dSAlex Tomas 
2173c9de560dSAlex Tomas 	if (v == SEQ_START_TOKEN) {
2174c9de560dSAlex Tomas 		seq_printf(seq, "%-5s %-8s %-23s %-23s %-23s %-5s "
2175c9de560dSAlex Tomas 				"%-5s %-2s %-5s %-5s %-5s %-6s\n",
2176c9de560dSAlex Tomas 			  "pid", "inode", "original", "goal", "result", "found",
2177c9de560dSAlex Tomas 			   "grps", "cr", "flags", "merge", "tail", "broken");
2178c9de560dSAlex Tomas 		return 0;
2179c9de560dSAlex Tomas 	}
2180c9de560dSAlex Tomas 
2181c9de560dSAlex Tomas 	if (hs->op == EXT4_MB_HISTORY_ALLOC) {
2182c9de560dSAlex Tomas 		fmt = "%-5u %-8u %-23s %-23s %-23s %-5u %-5u %-2u "
2183c9de560dSAlex Tomas 			"%-5u %-5s %-5u %-6u\n";
2184a9df9a49STheodore Ts'o 		sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
2185c9de560dSAlex Tomas 			hs->result.fe_start, hs->result.fe_len,
2186c9de560dSAlex Tomas 			hs->result.fe_logical);
2187a9df9a49STheodore Ts'o 		sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
2188c9de560dSAlex Tomas 			hs->orig.fe_start, hs->orig.fe_len,
2189c9de560dSAlex Tomas 			hs->orig.fe_logical);
2190a9df9a49STheodore Ts'o 		sprintf(buf3, "%u/%d/%u@%u", hs->goal.fe_group,
2191c9de560dSAlex Tomas 			hs->goal.fe_start, hs->goal.fe_len,
2192c9de560dSAlex Tomas 			hs->goal.fe_logical);
2193c9de560dSAlex Tomas 		seq_printf(seq, fmt, hs->pid, hs->ino, buf, buf3, buf2,
2194c9de560dSAlex Tomas 				hs->found, hs->groups, hs->cr, hs->flags,
2195c9de560dSAlex Tomas 				hs->merged ? "M" : "", hs->tail,
2196c9de560dSAlex Tomas 				hs->buddy ? 1 << hs->buddy : 0);
2197c9de560dSAlex Tomas 	} else if (hs->op == EXT4_MB_HISTORY_PREALLOC) {
2198c9de560dSAlex Tomas 		fmt = "%-5u %-8u %-23s %-23s %-23s\n";
2199a9df9a49STheodore Ts'o 		sprintf(buf2, "%u/%d/%u@%u", hs->result.fe_group,
2200c9de560dSAlex Tomas 			hs->result.fe_start, hs->result.fe_len,
2201c9de560dSAlex Tomas 			hs->result.fe_logical);
2202a9df9a49STheodore Ts'o 		sprintf(buf, "%u/%d/%u@%u", hs->orig.fe_group,
2203c9de560dSAlex Tomas 			hs->orig.fe_start, hs->orig.fe_len,
2204c9de560dSAlex Tomas 			hs->orig.fe_logical);
2205c9de560dSAlex Tomas 		seq_printf(seq, fmt, hs->pid, hs->ino, buf, "", buf2);
2206c9de560dSAlex Tomas 	} else if (hs->op == EXT4_MB_HISTORY_DISCARD) {
2207a9df9a49STheodore Ts'o 		sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
2208c9de560dSAlex Tomas 			hs->result.fe_start, hs->result.fe_len);
2209c9de560dSAlex Tomas 		seq_printf(seq, "%-5u %-8u %-23s discard\n",
2210c9de560dSAlex Tomas 				hs->pid, hs->ino, buf2);
2211c9de560dSAlex Tomas 	} else if (hs->op == EXT4_MB_HISTORY_FREE) {
2212a9df9a49STheodore Ts'o 		sprintf(buf2, "%u/%d/%u", hs->result.fe_group,
2213c9de560dSAlex Tomas 			hs->result.fe_start, hs->result.fe_len);
2214c9de560dSAlex Tomas 		seq_printf(seq, "%-5u %-8u %-23s free\n",
2215c9de560dSAlex Tomas 				hs->pid, hs->ino, buf2);
2216c9de560dSAlex Tomas 	}
2217c9de560dSAlex Tomas 	return 0;
2218c9de560dSAlex Tomas }
2219c9de560dSAlex Tomas 
2220c9de560dSAlex Tomas static void ext4_mb_seq_history_stop(struct seq_file *seq, void *v)
2221c9de560dSAlex Tomas {
2222c9de560dSAlex Tomas }
2223c9de560dSAlex Tomas 
2224c9de560dSAlex Tomas static struct seq_operations ext4_mb_seq_history_ops = {
2225c9de560dSAlex Tomas 	.start  = ext4_mb_seq_history_start,
2226c9de560dSAlex Tomas 	.next   = ext4_mb_seq_history_next,
2227c9de560dSAlex Tomas 	.stop   = ext4_mb_seq_history_stop,
2228c9de560dSAlex Tomas 	.show   = ext4_mb_seq_history_show,
2229c9de560dSAlex Tomas };
2230c9de560dSAlex Tomas 
2231c9de560dSAlex Tomas static int ext4_mb_seq_history_open(struct inode *inode, struct file *file)
2232c9de560dSAlex Tomas {
2233c9de560dSAlex Tomas 	struct super_block *sb = PDE(inode)->data;
2234c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2235c9de560dSAlex Tomas 	struct ext4_mb_proc_session *s;
2236c9de560dSAlex Tomas 	int rc;
2237c9de560dSAlex Tomas 	int size;
2238c9de560dSAlex Tomas 
223974767c5aSShen Feng 	if (unlikely(sbi->s_mb_history == NULL))
224074767c5aSShen Feng 		return -ENOMEM;
2241c9de560dSAlex Tomas 	s = kmalloc(sizeof(*s), GFP_KERNEL);
2242c9de560dSAlex Tomas 	if (s == NULL)
2243c9de560dSAlex Tomas 		return -ENOMEM;
2244c9de560dSAlex Tomas 	s->sb = sb;
2245c9de560dSAlex Tomas 	size = sizeof(struct ext4_mb_history) * sbi->s_mb_history_max;
2246c9de560dSAlex Tomas 	s->history = kmalloc(size, GFP_KERNEL);
2247c9de560dSAlex Tomas 	if (s->history == NULL) {
2248c9de560dSAlex Tomas 		kfree(s);
2249c9de560dSAlex Tomas 		return -ENOMEM;
2250c9de560dSAlex Tomas 	}
2251c9de560dSAlex Tomas 
2252c9de560dSAlex Tomas 	spin_lock(&sbi->s_mb_history_lock);
2253c9de560dSAlex Tomas 	memcpy(s->history, sbi->s_mb_history, size);
2254c9de560dSAlex Tomas 	s->max = sbi->s_mb_history_max;
2255c9de560dSAlex Tomas 	s->start = sbi->s_mb_history_cur % s->max;
2256c9de560dSAlex Tomas 	spin_unlock(&sbi->s_mb_history_lock);
2257c9de560dSAlex Tomas 
2258c9de560dSAlex Tomas 	rc = seq_open(file, &ext4_mb_seq_history_ops);
2259c9de560dSAlex Tomas 	if (rc == 0) {
2260c9de560dSAlex Tomas 		struct seq_file *m = (struct seq_file *)file->private_data;
2261c9de560dSAlex Tomas 		m->private = s;
2262c9de560dSAlex Tomas 	} else {
2263c9de560dSAlex Tomas 		kfree(s->history);
2264c9de560dSAlex Tomas 		kfree(s);
2265c9de560dSAlex Tomas 	}
2266c9de560dSAlex Tomas 	return rc;
2267c9de560dSAlex Tomas 
2268c9de560dSAlex Tomas }
2269c9de560dSAlex Tomas 
2270c9de560dSAlex Tomas static int ext4_mb_seq_history_release(struct inode *inode, struct file *file)
2271c9de560dSAlex Tomas {
2272c9de560dSAlex Tomas 	struct seq_file *seq = (struct seq_file *)file->private_data;
2273c9de560dSAlex Tomas 	struct ext4_mb_proc_session *s = seq->private;
2274c9de560dSAlex Tomas 	kfree(s->history);
2275c9de560dSAlex Tomas 	kfree(s);
2276c9de560dSAlex Tomas 	return seq_release(inode, file);
2277c9de560dSAlex Tomas }
2278c9de560dSAlex Tomas 
2279c9de560dSAlex Tomas static ssize_t ext4_mb_seq_history_write(struct file *file,
2280c9de560dSAlex Tomas 				const char __user *buffer,
2281c9de560dSAlex Tomas 				size_t count, loff_t *ppos)
2282c9de560dSAlex Tomas {
2283c9de560dSAlex Tomas 	struct seq_file *seq = (struct seq_file *)file->private_data;
2284c9de560dSAlex Tomas 	struct ext4_mb_proc_session *s = seq->private;
2285c9de560dSAlex Tomas 	struct super_block *sb = s->sb;
2286c9de560dSAlex Tomas 	char str[32];
2287c9de560dSAlex Tomas 	int value;
2288c9de560dSAlex Tomas 
2289c9de560dSAlex Tomas 	if (count >= sizeof(str)) {
2290c9de560dSAlex Tomas 		printk(KERN_ERR "EXT4-fs: %s string too long, max %u bytes\n",
2291c9de560dSAlex Tomas 				"mb_history", (int)sizeof(str));
2292c9de560dSAlex Tomas 		return -EOVERFLOW;
2293c9de560dSAlex Tomas 	}
2294c9de560dSAlex Tomas 
2295c9de560dSAlex Tomas 	if (copy_from_user(str, buffer, count))
2296c9de560dSAlex Tomas 		return -EFAULT;
2297c9de560dSAlex Tomas 
2298c9de560dSAlex Tomas 	value = simple_strtol(str, NULL, 0);
2299c9de560dSAlex Tomas 	if (value < 0)
2300c9de560dSAlex Tomas 		return -ERANGE;
2301c9de560dSAlex Tomas 	EXT4_SB(sb)->s_mb_history_filter = value;
2302c9de560dSAlex Tomas 
2303c9de560dSAlex Tomas 	return count;
2304c9de560dSAlex Tomas }
2305c9de560dSAlex Tomas 
2306c9de560dSAlex Tomas static struct file_operations ext4_mb_seq_history_fops = {
2307c9de560dSAlex Tomas 	.owner		= THIS_MODULE,
2308c9de560dSAlex Tomas 	.open		= ext4_mb_seq_history_open,
2309c9de560dSAlex Tomas 	.read		= seq_read,
2310c9de560dSAlex Tomas 	.write		= ext4_mb_seq_history_write,
2311c9de560dSAlex Tomas 	.llseek		= seq_lseek,
2312c9de560dSAlex Tomas 	.release	= ext4_mb_seq_history_release,
2313c9de560dSAlex Tomas };
2314c9de560dSAlex Tomas 
2315c9de560dSAlex Tomas static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2316c9de560dSAlex Tomas {
2317c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2318c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2319c9de560dSAlex Tomas 	ext4_group_t group;
2320c9de560dSAlex Tomas 
2321c9de560dSAlex Tomas 	if (*pos < 0 || *pos >= sbi->s_groups_count)
2322c9de560dSAlex Tomas 		return NULL;
2323c9de560dSAlex Tomas 
2324c9de560dSAlex Tomas 	group = *pos + 1;
2325a9df9a49STheodore Ts'o 	return (void *) ((unsigned long) group);
2326c9de560dSAlex Tomas }
2327c9de560dSAlex Tomas 
2328c9de560dSAlex Tomas static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
2329c9de560dSAlex Tomas {
2330c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2331c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2332c9de560dSAlex Tomas 	ext4_group_t group;
2333c9de560dSAlex Tomas 
2334c9de560dSAlex Tomas 	++*pos;
2335c9de560dSAlex Tomas 	if (*pos < 0 || *pos >= sbi->s_groups_count)
2336c9de560dSAlex Tomas 		return NULL;
2337c9de560dSAlex Tomas 	group = *pos + 1;
2338a9df9a49STheodore Ts'o 	return (void *) ((unsigned long) group);
2339c9de560dSAlex Tomas }
2340c9de560dSAlex Tomas 
2341c9de560dSAlex Tomas static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
2342c9de560dSAlex Tomas {
2343c9de560dSAlex Tomas 	struct super_block *sb = seq->private;
2344a9df9a49STheodore Ts'o 	ext4_group_t group = (ext4_group_t) ((unsigned long) v);
2345c9de560dSAlex Tomas 	int i;
2346c9de560dSAlex Tomas 	int err;
2347c9de560dSAlex Tomas 	struct ext4_buddy e4b;
2348c9de560dSAlex Tomas 	struct sg {
2349c9de560dSAlex Tomas 		struct ext4_group_info info;
2350c9de560dSAlex Tomas 		unsigned short counters[16];
2351c9de560dSAlex Tomas 	} sg;
2352c9de560dSAlex Tomas 
2353c9de560dSAlex Tomas 	group--;
2354c9de560dSAlex Tomas 	if (group == 0)
2355c9de560dSAlex Tomas 		seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
2356c9de560dSAlex Tomas 				"[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
2357c9de560dSAlex Tomas 				  "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
2358c9de560dSAlex Tomas 			   "group", "free", "frags", "first",
2359c9de560dSAlex Tomas 			   "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
2360c9de560dSAlex Tomas 			   "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
2361c9de560dSAlex Tomas 
2362c9de560dSAlex Tomas 	i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
2363c9de560dSAlex Tomas 		sizeof(struct ext4_group_info);
2364c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(sb, group, &e4b);
2365c9de560dSAlex Tomas 	if (err) {
2366a9df9a49STheodore Ts'o 		seq_printf(seq, "#%-5u: I/O error\n", group);
2367c9de560dSAlex Tomas 		return 0;
2368c9de560dSAlex Tomas 	}
2369c9de560dSAlex Tomas 	ext4_lock_group(sb, group);
2370c9de560dSAlex Tomas 	memcpy(&sg, ext4_get_group_info(sb, group), i);
2371c9de560dSAlex Tomas 	ext4_unlock_group(sb, group);
2372c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
2373c9de560dSAlex Tomas 
2374a9df9a49STheodore Ts'o 	seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
2375c9de560dSAlex Tomas 			sg.info.bb_fragments, sg.info.bb_first_free);
2376c9de560dSAlex Tomas 	for (i = 0; i <= 13; i++)
2377c9de560dSAlex Tomas 		seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
2378c9de560dSAlex Tomas 				sg.info.bb_counters[i] : 0);
2379c9de560dSAlex Tomas 	seq_printf(seq, " ]\n");
2380c9de560dSAlex Tomas 
2381c9de560dSAlex Tomas 	return 0;
2382c9de560dSAlex Tomas }
2383c9de560dSAlex Tomas 
2384c9de560dSAlex Tomas static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
2385c9de560dSAlex Tomas {
2386c9de560dSAlex Tomas }
2387c9de560dSAlex Tomas 
2388c9de560dSAlex Tomas static struct seq_operations ext4_mb_seq_groups_ops = {
2389c9de560dSAlex Tomas 	.start  = ext4_mb_seq_groups_start,
2390c9de560dSAlex Tomas 	.next   = ext4_mb_seq_groups_next,
2391c9de560dSAlex Tomas 	.stop   = ext4_mb_seq_groups_stop,
2392c9de560dSAlex Tomas 	.show   = ext4_mb_seq_groups_show,
2393c9de560dSAlex Tomas };
2394c9de560dSAlex Tomas 
2395c9de560dSAlex Tomas static int ext4_mb_seq_groups_open(struct inode *inode, struct file *file)
2396c9de560dSAlex Tomas {
2397c9de560dSAlex Tomas 	struct super_block *sb = PDE(inode)->data;
2398c9de560dSAlex Tomas 	int rc;
2399c9de560dSAlex Tomas 
2400c9de560dSAlex Tomas 	rc = seq_open(file, &ext4_mb_seq_groups_ops);
2401c9de560dSAlex Tomas 	if (rc == 0) {
2402c9de560dSAlex Tomas 		struct seq_file *m = (struct seq_file *)file->private_data;
2403c9de560dSAlex Tomas 		m->private = sb;
2404c9de560dSAlex Tomas 	}
2405c9de560dSAlex Tomas 	return rc;
2406c9de560dSAlex Tomas 
2407c9de560dSAlex Tomas }
2408c9de560dSAlex Tomas 
2409c9de560dSAlex Tomas static struct file_operations ext4_mb_seq_groups_fops = {
2410c9de560dSAlex Tomas 	.owner		= THIS_MODULE,
2411c9de560dSAlex Tomas 	.open		= ext4_mb_seq_groups_open,
2412c9de560dSAlex Tomas 	.read		= seq_read,
2413c9de560dSAlex Tomas 	.llseek		= seq_lseek,
2414c9de560dSAlex Tomas 	.release	= seq_release,
2415c9de560dSAlex Tomas };
2416c9de560dSAlex Tomas 
2417c9de560dSAlex Tomas static void ext4_mb_history_release(struct super_block *sb)
2418c9de560dSAlex Tomas {
2419c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2420c9de560dSAlex Tomas 
24219f6200bbSTheodore Ts'o 	if (sbi->s_proc != NULL) {
24229f6200bbSTheodore Ts'o 		remove_proc_entry("mb_groups", sbi->s_proc);
24239f6200bbSTheodore Ts'o 		remove_proc_entry("mb_history", sbi->s_proc);
24249f6200bbSTheodore Ts'o 	}
2425c9de560dSAlex Tomas 	kfree(sbi->s_mb_history);
2426c9de560dSAlex Tomas }
2427c9de560dSAlex Tomas 
2428c9de560dSAlex Tomas static void ext4_mb_history_init(struct super_block *sb)
2429c9de560dSAlex Tomas {
2430c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2431c9de560dSAlex Tomas 	int i;
2432c9de560dSAlex Tomas 
24339f6200bbSTheodore Ts'o 	if (sbi->s_proc != NULL) {
24349f6200bbSTheodore Ts'o 		proc_create_data("mb_history", S_IRUGO, sbi->s_proc,
243546fe74f2SDenis V. Lunev 				 &ext4_mb_seq_history_fops, sb);
24369f6200bbSTheodore Ts'o 		proc_create_data("mb_groups", S_IRUGO, sbi->s_proc,
243746fe74f2SDenis V. Lunev 				 &ext4_mb_seq_groups_fops, sb);
2438c9de560dSAlex Tomas 	}
2439c9de560dSAlex Tomas 
2440c9de560dSAlex Tomas 	sbi->s_mb_history_max = 1000;
2441c9de560dSAlex Tomas 	sbi->s_mb_history_cur = 0;
2442c9de560dSAlex Tomas 	spin_lock_init(&sbi->s_mb_history_lock);
2443c9de560dSAlex Tomas 	i = sbi->s_mb_history_max * sizeof(struct ext4_mb_history);
244474767c5aSShen Feng 	sbi->s_mb_history = kzalloc(i, GFP_KERNEL);
2445c9de560dSAlex Tomas 	/* if we can't allocate history, then we simple won't use it */
2446c9de560dSAlex Tomas }
2447c9de560dSAlex Tomas 
24484ddfef7bSEric Sandeen static noinline_for_stack void
24494ddfef7bSEric Sandeen ext4_mb_store_history(struct ext4_allocation_context *ac)
2450c9de560dSAlex Tomas {
2451c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2452c9de560dSAlex Tomas 	struct ext4_mb_history h;
2453c9de560dSAlex Tomas 
2454c9de560dSAlex Tomas 	if (unlikely(sbi->s_mb_history == NULL))
2455c9de560dSAlex Tomas 		return;
2456c9de560dSAlex Tomas 
2457c9de560dSAlex Tomas 	if (!(ac->ac_op & sbi->s_mb_history_filter))
2458c9de560dSAlex Tomas 		return;
2459c9de560dSAlex Tomas 
2460c9de560dSAlex Tomas 	h.op = ac->ac_op;
2461c9de560dSAlex Tomas 	h.pid = current->pid;
2462c9de560dSAlex Tomas 	h.ino = ac->ac_inode ? ac->ac_inode->i_ino : 0;
2463c9de560dSAlex Tomas 	h.orig = ac->ac_o_ex;
2464c9de560dSAlex Tomas 	h.result = ac->ac_b_ex;
2465c9de560dSAlex Tomas 	h.flags = ac->ac_flags;
2466c9de560dSAlex Tomas 	h.found = ac->ac_found;
2467c9de560dSAlex Tomas 	h.groups = ac->ac_groups_scanned;
2468c9de560dSAlex Tomas 	h.cr = ac->ac_criteria;
2469c9de560dSAlex Tomas 	h.tail = ac->ac_tail;
2470c9de560dSAlex Tomas 	h.buddy = ac->ac_buddy;
2471c9de560dSAlex Tomas 	h.merged = 0;
2472c9de560dSAlex Tomas 	if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
2473c9de560dSAlex Tomas 		if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
2474c9de560dSAlex Tomas 				ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
2475c9de560dSAlex Tomas 			h.merged = 1;
2476c9de560dSAlex Tomas 		h.goal = ac->ac_g_ex;
2477c9de560dSAlex Tomas 		h.result = ac->ac_f_ex;
2478c9de560dSAlex Tomas 	}
2479c9de560dSAlex Tomas 
2480c9de560dSAlex Tomas 	spin_lock(&sbi->s_mb_history_lock);
2481c9de560dSAlex Tomas 	memcpy(sbi->s_mb_history + sbi->s_mb_history_cur, &h, sizeof(h));
2482c9de560dSAlex Tomas 	if (++sbi->s_mb_history_cur >= sbi->s_mb_history_max)
2483c9de560dSAlex Tomas 		sbi->s_mb_history_cur = 0;
2484c9de560dSAlex Tomas 	spin_unlock(&sbi->s_mb_history_lock);
2485c9de560dSAlex Tomas }
2486c9de560dSAlex Tomas 
2487c9de560dSAlex Tomas #else
2488c9de560dSAlex Tomas #define ext4_mb_history_release(sb)
2489c9de560dSAlex Tomas #define ext4_mb_history_init(sb)
2490c9de560dSAlex Tomas #endif
2491c9de560dSAlex Tomas 
24925f21b0e6SFrederic Bohe 
24935f21b0e6SFrederic Bohe /* Create and initialize ext4_group_info data for the given group. */
2494920313a7SAneesh Kumar K.V int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
24955f21b0e6SFrederic Bohe 			  struct ext4_group_desc *desc)
24965f21b0e6SFrederic Bohe {
24975f21b0e6SFrederic Bohe 	int i, len;
24985f21b0e6SFrederic Bohe 	int metalen = 0;
24995f21b0e6SFrederic Bohe 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25005f21b0e6SFrederic Bohe 	struct ext4_group_info **meta_group_info;
25015f21b0e6SFrederic Bohe 
25025f21b0e6SFrederic Bohe 	/*
25035f21b0e6SFrederic Bohe 	 * First check if this group is the first of a reserved block.
25045f21b0e6SFrederic Bohe 	 * If it's true, we have to allocate a new table of pointers
25055f21b0e6SFrederic Bohe 	 * to ext4_group_info structures
25065f21b0e6SFrederic Bohe 	 */
25075f21b0e6SFrederic Bohe 	if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
25085f21b0e6SFrederic Bohe 		metalen = sizeof(*meta_group_info) <<
25095f21b0e6SFrederic Bohe 			EXT4_DESC_PER_BLOCK_BITS(sb);
25105f21b0e6SFrederic Bohe 		meta_group_info = kmalloc(metalen, GFP_KERNEL);
25115f21b0e6SFrederic Bohe 		if (meta_group_info == NULL) {
25125f21b0e6SFrederic Bohe 			printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
25135f21b0e6SFrederic Bohe 			       "buddy group\n");
25145f21b0e6SFrederic Bohe 			goto exit_meta_group_info;
25155f21b0e6SFrederic Bohe 		}
25165f21b0e6SFrederic Bohe 		sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)] =
25175f21b0e6SFrederic Bohe 			meta_group_info;
25185f21b0e6SFrederic Bohe 	}
25195f21b0e6SFrederic Bohe 
25205f21b0e6SFrederic Bohe 	/*
25215f21b0e6SFrederic Bohe 	 * calculate needed size. if change bb_counters size,
25225f21b0e6SFrederic Bohe 	 * don't forget about ext4_mb_generate_buddy()
25235f21b0e6SFrederic Bohe 	 */
25245f21b0e6SFrederic Bohe 	len = offsetof(typeof(**meta_group_info),
25255f21b0e6SFrederic Bohe 		       bb_counters[sb->s_blocksize_bits + 2]);
25265f21b0e6SFrederic Bohe 
25275f21b0e6SFrederic Bohe 	meta_group_info =
25285f21b0e6SFrederic Bohe 		sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)];
25295f21b0e6SFrederic Bohe 	i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
25305f21b0e6SFrederic Bohe 
25315f21b0e6SFrederic Bohe 	meta_group_info[i] = kzalloc(len, GFP_KERNEL);
25325f21b0e6SFrederic Bohe 	if (meta_group_info[i] == NULL) {
25335f21b0e6SFrederic Bohe 		printk(KERN_ERR "EXT4-fs: can't allocate buddy mem\n");
25345f21b0e6SFrederic Bohe 		goto exit_group_info;
25355f21b0e6SFrederic Bohe 	}
25365f21b0e6SFrederic Bohe 	set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
25375f21b0e6SFrederic Bohe 		&(meta_group_info[i]->bb_state));
25385f21b0e6SFrederic Bohe 
25395f21b0e6SFrederic Bohe 	/*
25405f21b0e6SFrederic Bohe 	 * initialize bb_free to be able to skip
25415f21b0e6SFrederic Bohe 	 * empty groups without initialization
25425f21b0e6SFrederic Bohe 	 */
25435f21b0e6SFrederic Bohe 	if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
25445f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_free =
25455f21b0e6SFrederic Bohe 			ext4_free_blocks_after_init(sb, group, desc);
25465f21b0e6SFrederic Bohe 	} else {
25475f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_free =
2548560671a0SAneesh Kumar K.V 			ext4_free_blks_count(sb, desc);
25495f21b0e6SFrederic Bohe 	}
25505f21b0e6SFrederic Bohe 
25515f21b0e6SFrederic Bohe 	INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
2552920313a7SAneesh Kumar K.V 	init_rwsem(&meta_group_info[i]->alloc_sem);
2553c894058dSAneesh Kumar K.V 	meta_group_info[i]->bb_free_root.rb_node = NULL;;
25545f21b0e6SFrederic Bohe 
25555f21b0e6SFrederic Bohe #ifdef DOUBLE_CHECK
25565f21b0e6SFrederic Bohe 	{
25575f21b0e6SFrederic Bohe 		struct buffer_head *bh;
25585f21b0e6SFrederic Bohe 		meta_group_info[i]->bb_bitmap =
25595f21b0e6SFrederic Bohe 			kmalloc(sb->s_blocksize, GFP_KERNEL);
25605f21b0e6SFrederic Bohe 		BUG_ON(meta_group_info[i]->bb_bitmap == NULL);
25615f21b0e6SFrederic Bohe 		bh = ext4_read_block_bitmap(sb, group);
25625f21b0e6SFrederic Bohe 		BUG_ON(bh == NULL);
25635f21b0e6SFrederic Bohe 		memcpy(meta_group_info[i]->bb_bitmap, bh->b_data,
25645f21b0e6SFrederic Bohe 			sb->s_blocksize);
25655f21b0e6SFrederic Bohe 		put_bh(bh);
25665f21b0e6SFrederic Bohe 	}
25675f21b0e6SFrederic Bohe #endif
25685f21b0e6SFrederic Bohe 
25695f21b0e6SFrederic Bohe 	return 0;
25705f21b0e6SFrederic Bohe 
25715f21b0e6SFrederic Bohe exit_group_info:
25725f21b0e6SFrederic Bohe 	/* If a meta_group_info table has been allocated, release it now */
25735f21b0e6SFrederic Bohe 	if (group % EXT4_DESC_PER_BLOCK(sb) == 0)
25745f21b0e6SFrederic Bohe 		kfree(sbi->s_group_info[group >> EXT4_DESC_PER_BLOCK_BITS(sb)]);
25755f21b0e6SFrederic Bohe exit_meta_group_info:
25765f21b0e6SFrederic Bohe 	return -ENOMEM;
25775f21b0e6SFrederic Bohe } /* ext4_mb_add_groupinfo */
25785f21b0e6SFrederic Bohe 
25795f21b0e6SFrederic Bohe /*
25805f21b0e6SFrederic Bohe  * Update an existing group.
25815f21b0e6SFrederic Bohe  * This function is used for online resize
25825f21b0e6SFrederic Bohe  */
25835f21b0e6SFrederic Bohe void ext4_mb_update_group_info(struct ext4_group_info *grp, ext4_grpblk_t add)
25845f21b0e6SFrederic Bohe {
25855f21b0e6SFrederic Bohe 	grp->bb_free += add;
25865f21b0e6SFrederic Bohe }
25875f21b0e6SFrederic Bohe 
2588c9de560dSAlex Tomas static int ext4_mb_init_backend(struct super_block *sb)
2589c9de560dSAlex Tomas {
2590c9de560dSAlex Tomas 	ext4_group_t i;
25915f21b0e6SFrederic Bohe 	int metalen;
2592c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
25935f21b0e6SFrederic Bohe 	struct ext4_super_block *es = sbi->s_es;
25945f21b0e6SFrederic Bohe 	int num_meta_group_infos;
25955f21b0e6SFrederic Bohe 	int num_meta_group_infos_max;
25965f21b0e6SFrederic Bohe 	int array_size;
2597c9de560dSAlex Tomas 	struct ext4_group_info **meta_group_info;
25985f21b0e6SFrederic Bohe 	struct ext4_group_desc *desc;
2599c9de560dSAlex Tomas 
26005f21b0e6SFrederic Bohe 	/* This is the number of blocks used by GDT */
26015f21b0e6SFrederic Bohe 	num_meta_group_infos = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) -
26025f21b0e6SFrederic Bohe 				1) >> EXT4_DESC_PER_BLOCK_BITS(sb);
26035f21b0e6SFrederic Bohe 
26045f21b0e6SFrederic Bohe 	/*
26055f21b0e6SFrederic Bohe 	 * This is the total number of blocks used by GDT including
26065f21b0e6SFrederic Bohe 	 * the number of reserved blocks for GDT.
26075f21b0e6SFrederic Bohe 	 * The s_group_info array is allocated with this value
26085f21b0e6SFrederic Bohe 	 * to allow a clean online resize without a complex
26095f21b0e6SFrederic Bohe 	 * manipulation of pointer.
26105f21b0e6SFrederic Bohe 	 * The drawback is the unused memory when no resize
26115f21b0e6SFrederic Bohe 	 * occurs but it's very low in terms of pages
26125f21b0e6SFrederic Bohe 	 * (see comments below)
26135f21b0e6SFrederic Bohe 	 * Need to handle this properly when META_BG resizing is allowed
26145f21b0e6SFrederic Bohe 	 */
26155f21b0e6SFrederic Bohe 	num_meta_group_infos_max = num_meta_group_infos +
26165f21b0e6SFrederic Bohe 				le16_to_cpu(es->s_reserved_gdt_blocks);
26175f21b0e6SFrederic Bohe 
26185f21b0e6SFrederic Bohe 	/*
26195f21b0e6SFrederic Bohe 	 * array_size is the size of s_group_info array. We round it
26205f21b0e6SFrederic Bohe 	 * to the next power of two because this approximation is done
26215f21b0e6SFrederic Bohe 	 * internally by kmalloc so we can have some more memory
26225f21b0e6SFrederic Bohe 	 * for free here (e.g. may be used for META_BG resize).
26235f21b0e6SFrederic Bohe 	 */
26245f21b0e6SFrederic Bohe 	array_size = 1;
26255f21b0e6SFrederic Bohe 	while (array_size < sizeof(*sbi->s_group_info) *
26265f21b0e6SFrederic Bohe 	       num_meta_group_infos_max)
26275f21b0e6SFrederic Bohe 		array_size = array_size << 1;
2628c9de560dSAlex Tomas 	/* An 8TB filesystem with 64-bit pointers requires a 4096 byte
2629c9de560dSAlex Tomas 	 * kmalloc. A 128kb malloc should suffice for a 256TB filesystem.
2630c9de560dSAlex Tomas 	 * So a two level scheme suffices for now. */
26315f21b0e6SFrederic Bohe 	sbi->s_group_info = kmalloc(array_size, GFP_KERNEL);
2632c9de560dSAlex Tomas 	if (sbi->s_group_info == NULL) {
2633c9de560dSAlex Tomas 		printk(KERN_ERR "EXT4-fs: can't allocate buddy meta group\n");
2634c9de560dSAlex Tomas 		return -ENOMEM;
2635c9de560dSAlex Tomas 	}
2636c9de560dSAlex Tomas 	sbi->s_buddy_cache = new_inode(sb);
2637c9de560dSAlex Tomas 	if (sbi->s_buddy_cache == NULL) {
2638c9de560dSAlex Tomas 		printk(KERN_ERR "EXT4-fs: can't get new inode\n");
2639c9de560dSAlex Tomas 		goto err_freesgi;
2640c9de560dSAlex Tomas 	}
2641c9de560dSAlex Tomas 	EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
2642c9de560dSAlex Tomas 
2643c9de560dSAlex Tomas 	metalen = sizeof(*meta_group_info) << EXT4_DESC_PER_BLOCK_BITS(sb);
2644c9de560dSAlex Tomas 	for (i = 0; i < num_meta_group_infos; i++) {
2645c9de560dSAlex Tomas 		if ((i + 1) == num_meta_group_infos)
2646c9de560dSAlex Tomas 			metalen = sizeof(*meta_group_info) *
2647c9de560dSAlex Tomas 				(sbi->s_groups_count -
2648c9de560dSAlex Tomas 					(i << EXT4_DESC_PER_BLOCK_BITS(sb)));
2649c9de560dSAlex Tomas 		meta_group_info = kmalloc(metalen, GFP_KERNEL);
2650c9de560dSAlex Tomas 		if (meta_group_info == NULL) {
2651c9de560dSAlex Tomas 			printk(KERN_ERR "EXT4-fs: can't allocate mem for a "
2652c9de560dSAlex Tomas 			       "buddy group\n");
2653c9de560dSAlex Tomas 			goto err_freemeta;
2654c9de560dSAlex Tomas 		}
2655c9de560dSAlex Tomas 		sbi->s_group_info[i] = meta_group_info;
2656c9de560dSAlex Tomas 	}
2657c9de560dSAlex Tomas 
2658c9de560dSAlex Tomas 	for (i = 0; i < sbi->s_groups_count; i++) {
2659c9de560dSAlex Tomas 		desc = ext4_get_group_desc(sb, i, NULL);
2660c9de560dSAlex Tomas 		if (desc == NULL) {
2661c9de560dSAlex Tomas 			printk(KERN_ERR
2662a9df9a49STheodore Ts'o 				"EXT4-fs: can't read descriptor %u\n", i);
2663c9de560dSAlex Tomas 			goto err_freebuddy;
2664c9de560dSAlex Tomas 		}
26655f21b0e6SFrederic Bohe 		if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
26665f21b0e6SFrederic Bohe 			goto err_freebuddy;
2667c9de560dSAlex Tomas 	}
2668c9de560dSAlex Tomas 
2669c9de560dSAlex Tomas 	return 0;
2670c9de560dSAlex Tomas 
2671c9de560dSAlex Tomas err_freebuddy:
2672f1fa3342SRoel Kluin 	while (i-- > 0)
2673c9de560dSAlex Tomas 		kfree(ext4_get_group_info(sb, i));
2674c9de560dSAlex Tomas 	i = num_meta_group_infos;
2675c9de560dSAlex Tomas err_freemeta:
2676f1fa3342SRoel Kluin 	while (i-- > 0)
2677c9de560dSAlex Tomas 		kfree(sbi->s_group_info[i]);
2678c9de560dSAlex Tomas 	iput(sbi->s_buddy_cache);
2679c9de560dSAlex Tomas err_freesgi:
2680c9de560dSAlex Tomas 	kfree(sbi->s_group_info);
2681c9de560dSAlex Tomas 	return -ENOMEM;
2682c9de560dSAlex Tomas }
2683c9de560dSAlex Tomas 
2684c9de560dSAlex Tomas int ext4_mb_init(struct super_block *sb, int needs_recovery)
2685c9de560dSAlex Tomas {
2686c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
26876be2ded1SAneesh Kumar K.V 	unsigned i, j;
2688c9de560dSAlex Tomas 	unsigned offset;
2689c9de560dSAlex Tomas 	unsigned max;
269074767c5aSShen Feng 	int ret;
2691c9de560dSAlex Tomas 
2692c9de560dSAlex Tomas 	i = (sb->s_blocksize_bits + 2) * sizeof(unsigned short);
2693c9de560dSAlex Tomas 
2694c9de560dSAlex Tomas 	sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
2695c9de560dSAlex Tomas 	if (sbi->s_mb_offsets == NULL) {
2696c9de560dSAlex Tomas 		return -ENOMEM;
2697c9de560dSAlex Tomas 	}
2698ff7ef329SYasunori Goto 
2699ff7ef329SYasunori Goto 	i = (sb->s_blocksize_bits + 2) * sizeof(unsigned int);
2700c9de560dSAlex Tomas 	sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
2701c9de560dSAlex Tomas 	if (sbi->s_mb_maxs == NULL) {
2702c9de560dSAlex Tomas 		kfree(sbi->s_mb_maxs);
2703c9de560dSAlex Tomas 		return -ENOMEM;
2704c9de560dSAlex Tomas 	}
2705c9de560dSAlex Tomas 
2706c9de560dSAlex Tomas 	/* order 0 is regular bitmap */
2707c9de560dSAlex Tomas 	sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
2708c9de560dSAlex Tomas 	sbi->s_mb_offsets[0] = 0;
2709c9de560dSAlex Tomas 
2710c9de560dSAlex Tomas 	i = 1;
2711c9de560dSAlex Tomas 	offset = 0;
2712c9de560dSAlex Tomas 	max = sb->s_blocksize << 2;
2713c9de560dSAlex Tomas 	do {
2714c9de560dSAlex Tomas 		sbi->s_mb_offsets[i] = offset;
2715c9de560dSAlex Tomas 		sbi->s_mb_maxs[i] = max;
2716c9de560dSAlex Tomas 		offset += 1 << (sb->s_blocksize_bits - i);
2717c9de560dSAlex Tomas 		max = max >> 1;
2718c9de560dSAlex Tomas 		i++;
2719c9de560dSAlex Tomas 	} while (i <= sb->s_blocksize_bits + 1);
2720c9de560dSAlex Tomas 
2721c9de560dSAlex Tomas 	/* init file for buddy data */
272274767c5aSShen Feng 	ret = ext4_mb_init_backend(sb);
272374767c5aSShen Feng 	if (ret != 0) {
2724c9de560dSAlex Tomas 		kfree(sbi->s_mb_offsets);
2725c9de560dSAlex Tomas 		kfree(sbi->s_mb_maxs);
272674767c5aSShen Feng 		return ret;
2727c9de560dSAlex Tomas 	}
2728c9de560dSAlex Tomas 
2729c9de560dSAlex Tomas 	spin_lock_init(&sbi->s_md_lock);
2730c9de560dSAlex Tomas 	spin_lock_init(&sbi->s_bal_lock);
2731c9de560dSAlex Tomas 
2732c9de560dSAlex Tomas 	sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
2733c9de560dSAlex Tomas 	sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
2734c9de560dSAlex Tomas 	sbi->s_mb_stats = MB_DEFAULT_STATS;
2735c9de560dSAlex Tomas 	sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
2736c9de560dSAlex Tomas 	sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
2737c9de560dSAlex Tomas 	sbi->s_mb_history_filter = EXT4_MB_HISTORY_DEFAULT;
2738c9de560dSAlex Tomas 	sbi->s_mb_group_prealloc = MB_DEFAULT_GROUP_PREALLOC;
2739c9de560dSAlex Tomas 
2740730c213cSEric Sandeen 	sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
2741c9de560dSAlex Tomas 	if (sbi->s_locality_groups == NULL) {
2742c9de560dSAlex Tomas 		kfree(sbi->s_mb_offsets);
2743c9de560dSAlex Tomas 		kfree(sbi->s_mb_maxs);
2744c9de560dSAlex Tomas 		return -ENOMEM;
2745c9de560dSAlex Tomas 	}
2746730c213cSEric Sandeen 	for_each_possible_cpu(i) {
2747c9de560dSAlex Tomas 		struct ext4_locality_group *lg;
2748730c213cSEric Sandeen 		lg = per_cpu_ptr(sbi->s_locality_groups, i);
2749c9de560dSAlex Tomas 		mutex_init(&lg->lg_mutex);
27506be2ded1SAneesh Kumar K.V 		for (j = 0; j < PREALLOC_TB_SIZE; j++)
27516be2ded1SAneesh Kumar K.V 			INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
2752c9de560dSAlex Tomas 		spin_lock_init(&lg->lg_prealloc_lock);
2753c9de560dSAlex Tomas 	}
2754c9de560dSAlex Tomas 
2755c9de560dSAlex Tomas 	ext4_mb_history_init(sb);
2756c9de560dSAlex Tomas 
27570390131bSFrank Mayhar 	if (sbi->s_journal)
27583e624fc7STheodore Ts'o 		sbi->s_journal->j_commit_callback = release_blocks_on_commit;
27593e624fc7STheodore Ts'o 
27604776004fSTheodore Ts'o 	printk(KERN_INFO "EXT4-fs: mballoc enabled\n");
2761c9de560dSAlex Tomas 	return 0;
2762c9de560dSAlex Tomas }
2763c9de560dSAlex Tomas 
2764c9de560dSAlex Tomas /* need to called with ext4 group lock (ext4_lock_group) */
2765c9de560dSAlex Tomas static void ext4_mb_cleanup_pa(struct ext4_group_info *grp)
2766c9de560dSAlex Tomas {
2767c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
2768c9de560dSAlex Tomas 	struct list_head *cur, *tmp;
2769c9de560dSAlex Tomas 	int count = 0;
2770c9de560dSAlex Tomas 
2771c9de560dSAlex Tomas 	list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
2772c9de560dSAlex Tomas 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
2773c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
2774c9de560dSAlex Tomas 		count++;
2775688f05a0SAneesh Kumar K.V 		kmem_cache_free(ext4_pspace_cachep, pa);
2776c9de560dSAlex Tomas 	}
2777c9de560dSAlex Tomas 	if (count)
2778c9de560dSAlex Tomas 		mb_debug("mballoc: %u PAs left\n", count);
2779c9de560dSAlex Tomas 
2780c9de560dSAlex Tomas }
2781c9de560dSAlex Tomas 
2782c9de560dSAlex Tomas int ext4_mb_release(struct super_block *sb)
2783c9de560dSAlex Tomas {
2784c9de560dSAlex Tomas 	ext4_group_t i;
2785c9de560dSAlex Tomas 	int num_meta_group_infos;
2786c9de560dSAlex Tomas 	struct ext4_group_info *grinfo;
2787c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2788c9de560dSAlex Tomas 
2789c9de560dSAlex Tomas 	if (sbi->s_group_info) {
2790c9de560dSAlex Tomas 		for (i = 0; i < sbi->s_groups_count; i++) {
2791c9de560dSAlex Tomas 			grinfo = ext4_get_group_info(sb, i);
2792c9de560dSAlex Tomas #ifdef DOUBLE_CHECK
2793c9de560dSAlex Tomas 			kfree(grinfo->bb_bitmap);
2794c9de560dSAlex Tomas #endif
2795c9de560dSAlex Tomas 			ext4_lock_group(sb, i);
2796c9de560dSAlex Tomas 			ext4_mb_cleanup_pa(grinfo);
2797c9de560dSAlex Tomas 			ext4_unlock_group(sb, i);
2798c9de560dSAlex Tomas 			kfree(grinfo);
2799c9de560dSAlex Tomas 		}
2800c9de560dSAlex Tomas 		num_meta_group_infos = (sbi->s_groups_count +
2801c9de560dSAlex Tomas 				EXT4_DESC_PER_BLOCK(sb) - 1) >>
2802c9de560dSAlex Tomas 			EXT4_DESC_PER_BLOCK_BITS(sb);
2803c9de560dSAlex Tomas 		for (i = 0; i < num_meta_group_infos; i++)
2804c9de560dSAlex Tomas 			kfree(sbi->s_group_info[i]);
2805c9de560dSAlex Tomas 		kfree(sbi->s_group_info);
2806c9de560dSAlex Tomas 	}
2807c9de560dSAlex Tomas 	kfree(sbi->s_mb_offsets);
2808c9de560dSAlex Tomas 	kfree(sbi->s_mb_maxs);
2809c9de560dSAlex Tomas 	if (sbi->s_buddy_cache)
2810c9de560dSAlex Tomas 		iput(sbi->s_buddy_cache);
2811c9de560dSAlex Tomas 	if (sbi->s_mb_stats) {
2812c9de560dSAlex Tomas 		printk(KERN_INFO
2813c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %u blocks %u reqs (%u success)\n",
2814c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_allocated),
2815c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_reqs),
2816c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_success));
2817c9de560dSAlex Tomas 		printk(KERN_INFO
2818c9de560dSAlex Tomas 		      "EXT4-fs: mballoc: %u extents scanned, %u goal hits, "
2819c9de560dSAlex Tomas 				"%u 2^N hits, %u breaks, %u lost\n",
2820c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_ex_scanned),
2821c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_goals),
2822c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_2orders),
2823c9de560dSAlex Tomas 				atomic_read(&sbi->s_bal_breaks),
2824c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_lost_chunks));
2825c9de560dSAlex Tomas 		printk(KERN_INFO
2826c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %lu generated and it took %Lu\n",
2827c9de560dSAlex Tomas 				sbi->s_mb_buddies_generated++,
2828c9de560dSAlex Tomas 				sbi->s_mb_generation_time);
2829c9de560dSAlex Tomas 		printk(KERN_INFO
2830c9de560dSAlex Tomas 		       "EXT4-fs: mballoc: %u preallocated, %u discarded\n",
2831c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_preallocated),
2832c9de560dSAlex Tomas 				atomic_read(&sbi->s_mb_discarded));
2833c9de560dSAlex Tomas 	}
2834c9de560dSAlex Tomas 
2835730c213cSEric Sandeen 	free_percpu(sbi->s_locality_groups);
2836c9de560dSAlex Tomas 	ext4_mb_history_release(sb);
2837c9de560dSAlex Tomas 
2838c9de560dSAlex Tomas 	return 0;
2839c9de560dSAlex Tomas }
2840c9de560dSAlex Tomas 
28413e624fc7STheodore Ts'o /*
28423e624fc7STheodore Ts'o  * This function is called by the jbd2 layer once the commit has finished,
28433e624fc7STheodore Ts'o  * so we know we can free the blocks that were released with that commit.
28443e624fc7STheodore Ts'o  */
28453e624fc7STheodore Ts'o static void release_blocks_on_commit(journal_t *journal, transaction_t *txn)
2846c9de560dSAlex Tomas {
28473e624fc7STheodore Ts'o 	struct super_block *sb = journal->j_private;
2848c9de560dSAlex Tomas 	struct ext4_buddy e4b;
2849c894058dSAneesh Kumar K.V 	struct ext4_group_info *db;
2850c894058dSAneesh Kumar K.V 	int err, count = 0, count2 = 0;
2851c894058dSAneesh Kumar K.V 	struct ext4_free_data *entry;
28528a0aba73STheodore Ts'o 	ext4_fsblk_t discard_block;
28533e624fc7STheodore Ts'o 	struct list_head *l, *ltmp;
2854c9de560dSAlex Tomas 
28553e624fc7STheodore Ts'o 	list_for_each_safe(l, ltmp, &txn->t_private_list) {
28563e624fc7STheodore Ts'o 		entry = list_entry(l, struct ext4_free_data, list);
2857c9de560dSAlex Tomas 
2858a9df9a49STheodore Ts'o 		mb_debug("gonna free %u blocks in group %u (0x%p):",
2859c894058dSAneesh Kumar K.V 			 entry->count, entry->group, entry);
2860c9de560dSAlex Tomas 
2861c894058dSAneesh Kumar K.V 		err = ext4_mb_load_buddy(sb, entry->group, &e4b);
2862c9de560dSAlex Tomas 		/* we expect to find existing buddy because it's pinned */
2863c9de560dSAlex Tomas 		BUG_ON(err != 0);
2864c9de560dSAlex Tomas 
2865c894058dSAneesh Kumar K.V 		db = e4b.bd_info;
2866c9de560dSAlex Tomas 		/* there are blocks to put in buddy to make them really free */
2867c894058dSAneesh Kumar K.V 		count += entry->count;
2868c9de560dSAlex Tomas 		count2++;
2869c894058dSAneesh Kumar K.V 		ext4_lock_group(sb, entry->group);
2870c894058dSAneesh Kumar K.V 		/* Take it out of per group rb tree */
2871c894058dSAneesh Kumar K.V 		rb_erase(&entry->node, &(db->bb_free_root));
2872c894058dSAneesh Kumar K.V 		mb_free_blocks(NULL, &e4b, entry->start_blk, entry->count);
2873c9de560dSAlex Tomas 
2874c894058dSAneesh Kumar K.V 		if (!db->bb_free_root.rb_node) {
2875c894058dSAneesh Kumar K.V 			/* No more items in the per group rb tree
2876c894058dSAneesh Kumar K.V 			 * balance refcounts from ext4_mb_free_metadata()
2877c894058dSAneesh Kumar K.V 			 */
2878c9de560dSAlex Tomas 			page_cache_release(e4b.bd_buddy_page);
2879c9de560dSAlex Tomas 			page_cache_release(e4b.bd_bitmap_page);
2880c894058dSAneesh Kumar K.V 		}
2881c894058dSAneesh Kumar K.V 		ext4_unlock_group(sb, entry->group);
28828a0aba73STheodore Ts'o 		discard_block = (ext4_fsblk_t) entry->group * EXT4_BLOCKS_PER_GROUP(sb)
28838a0aba73STheodore Ts'o 			+ entry->start_blk
28848a0aba73STheodore Ts'o 			+ le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
2885ba80b101STheodore Ts'o 		trace_mark(ext4_discard_blocks, "dev %s blk %llu count %u",
2886ba80b101STheodore Ts'o 			   sb->s_id, (unsigned long long) discard_block,
2887ba80b101STheodore Ts'o 			   entry->count);
28888a0aba73STheodore Ts'o 		sb_issue_discard(sb, discard_block, entry->count);
2889c9de560dSAlex Tomas 
2890c894058dSAneesh Kumar K.V 		kmem_cache_free(ext4_free_ext_cachep, entry);
2891c9de560dSAlex Tomas 		ext4_mb_release_desc(&e4b);
28923e624fc7STheodore Ts'o 	}
2893c9de560dSAlex Tomas 
2894c9de560dSAlex Tomas 	mb_debug("freed %u blocks in %u structures\n", count, count2);
2895c9de560dSAlex Tomas }
2896c9de560dSAlex Tomas 
2897c9de560dSAlex Tomas int __init init_ext4_mballoc(void)
2898c9de560dSAlex Tomas {
2899c9de560dSAlex Tomas 	ext4_pspace_cachep =
2900c9de560dSAlex Tomas 		kmem_cache_create("ext4_prealloc_space",
2901c9de560dSAlex Tomas 				     sizeof(struct ext4_prealloc_space),
2902c9de560dSAlex Tomas 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2903c9de560dSAlex Tomas 	if (ext4_pspace_cachep == NULL)
2904c9de560dSAlex Tomas 		return -ENOMEM;
2905c9de560dSAlex Tomas 
2906256bdb49SEric Sandeen 	ext4_ac_cachep =
2907256bdb49SEric Sandeen 		kmem_cache_create("ext4_alloc_context",
2908256bdb49SEric Sandeen 				     sizeof(struct ext4_allocation_context),
2909256bdb49SEric Sandeen 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2910256bdb49SEric Sandeen 	if (ext4_ac_cachep == NULL) {
2911256bdb49SEric Sandeen 		kmem_cache_destroy(ext4_pspace_cachep);
2912256bdb49SEric Sandeen 		return -ENOMEM;
2913256bdb49SEric Sandeen 	}
2914c894058dSAneesh Kumar K.V 
2915c894058dSAneesh Kumar K.V 	ext4_free_ext_cachep =
2916c894058dSAneesh Kumar K.V 		kmem_cache_create("ext4_free_block_extents",
2917c894058dSAneesh Kumar K.V 				     sizeof(struct ext4_free_data),
2918c894058dSAneesh Kumar K.V 				     0, SLAB_RECLAIM_ACCOUNT, NULL);
2919c894058dSAneesh Kumar K.V 	if (ext4_free_ext_cachep == NULL) {
2920c894058dSAneesh Kumar K.V 		kmem_cache_destroy(ext4_pspace_cachep);
2921c894058dSAneesh Kumar K.V 		kmem_cache_destroy(ext4_ac_cachep);
2922c894058dSAneesh Kumar K.V 		return -ENOMEM;
2923c894058dSAneesh Kumar K.V 	}
2924c9de560dSAlex Tomas 	return 0;
2925c9de560dSAlex Tomas }
2926c9de560dSAlex Tomas 
2927c9de560dSAlex Tomas void exit_ext4_mballoc(void)
2928c9de560dSAlex Tomas {
2929c9de560dSAlex Tomas 	/* XXX: synchronize_rcu(); */
2930c9de560dSAlex Tomas 	kmem_cache_destroy(ext4_pspace_cachep);
2931256bdb49SEric Sandeen 	kmem_cache_destroy(ext4_ac_cachep);
2932c894058dSAneesh Kumar K.V 	kmem_cache_destroy(ext4_free_ext_cachep);
2933c9de560dSAlex Tomas }
2934c9de560dSAlex Tomas 
2935c9de560dSAlex Tomas 
2936c9de560dSAlex Tomas /*
2937c9de560dSAlex Tomas  * Check quota and mark choosed space (ac->ac_b_ex) non-free in bitmaps
2938c9de560dSAlex Tomas  * Returns 0 if success or error code
2939c9de560dSAlex Tomas  */
29404ddfef7bSEric Sandeen static noinline_for_stack int
29414ddfef7bSEric Sandeen ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
2942498e5f24STheodore Ts'o 				handle_t *handle, unsigned int reserv_blks)
2943c9de560dSAlex Tomas {
2944c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
2945c9de560dSAlex Tomas 	struct ext4_super_block *es;
2946c9de560dSAlex Tomas 	struct ext4_group_desc *gdp;
2947c9de560dSAlex Tomas 	struct buffer_head *gdp_bh;
2948c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
2949c9de560dSAlex Tomas 	struct super_block *sb;
2950c9de560dSAlex Tomas 	ext4_fsblk_t block;
2951519deca0SAneesh Kumar K.V 	int err, len;
2952c9de560dSAlex Tomas 
2953c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
2954c9de560dSAlex Tomas 	BUG_ON(ac->ac_b_ex.fe_len <= 0);
2955c9de560dSAlex Tomas 
2956c9de560dSAlex Tomas 	sb = ac->ac_sb;
2957c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
2958c9de560dSAlex Tomas 	es = sbi->s_es;
2959c9de560dSAlex Tomas 
2960c9de560dSAlex Tomas 
2961c9de560dSAlex Tomas 	err = -EIO;
2962574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
2963c9de560dSAlex Tomas 	if (!bitmap_bh)
2964c9de560dSAlex Tomas 		goto out_err;
2965c9de560dSAlex Tomas 
2966c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, bitmap_bh);
2967c9de560dSAlex Tomas 	if (err)
2968c9de560dSAlex Tomas 		goto out_err;
2969c9de560dSAlex Tomas 
2970c9de560dSAlex Tomas 	err = -EIO;
2971c9de560dSAlex Tomas 	gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
2972c9de560dSAlex Tomas 	if (!gdp)
2973c9de560dSAlex Tomas 		goto out_err;
2974c9de560dSAlex Tomas 
2975a9df9a49STheodore Ts'o 	ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
29769fd9784cSThadeu Lima de Souza Cascardo 			ext4_free_blks_count(sb, gdp));
297703cddb80SAneesh Kumar K.V 
2978c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, gdp_bh);
2979c9de560dSAlex Tomas 	if (err)
2980c9de560dSAlex Tomas 		goto out_err;
2981c9de560dSAlex Tomas 
2982c9de560dSAlex Tomas 	block = ac->ac_b_ex.fe_group * EXT4_BLOCKS_PER_GROUP(sb)
2983c9de560dSAlex Tomas 		+ ac->ac_b_ex.fe_start
2984c9de560dSAlex Tomas 		+ le32_to_cpu(es->s_first_data_block);
2985c9de560dSAlex Tomas 
2986519deca0SAneesh Kumar K.V 	len = ac->ac_b_ex.fe_len;
2987519deca0SAneesh Kumar K.V 	if (in_range(ext4_block_bitmap(sb, gdp), block, len) ||
2988519deca0SAneesh Kumar K.V 	    in_range(ext4_inode_bitmap(sb, gdp), block, len) ||
2989c9de560dSAlex Tomas 	    in_range(block, ext4_inode_table(sb, gdp),
2990519deca0SAneesh Kumar K.V 		     EXT4_SB(sb)->s_itb_per_group) ||
2991519deca0SAneesh Kumar K.V 	    in_range(block + len - 1, ext4_inode_table(sb, gdp),
2992c9de560dSAlex Tomas 		     EXT4_SB(sb)->s_itb_per_group)) {
299346e665e9SHarvey Harrison 		ext4_error(sb, __func__,
2994648f5879SAneesh Kumar K.V 			   "Allocating block %llu in system zone of %d group\n",
2995648f5879SAneesh Kumar K.V 			   block, ac->ac_b_ex.fe_group);
2996519deca0SAneesh Kumar K.V 		/* File system mounted not to panic on error
2997519deca0SAneesh Kumar K.V 		 * Fix the bitmap and repeat the block allocation
2998519deca0SAneesh Kumar K.V 		 * We leak some of the blocks here.
2999519deca0SAneesh Kumar K.V 		 */
3000519deca0SAneesh Kumar K.V 		mb_set_bits(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group),
3001519deca0SAneesh Kumar K.V 				bitmap_bh->b_data, ac->ac_b_ex.fe_start,
3002519deca0SAneesh Kumar K.V 				ac->ac_b_ex.fe_len);
30030390131bSFrank Mayhar 		err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
3004519deca0SAneesh Kumar K.V 		if (!err)
3005519deca0SAneesh Kumar K.V 			err = -EAGAIN;
3006519deca0SAneesh Kumar K.V 		goto out_err;
3007c9de560dSAlex Tomas 	}
3008c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
3009c9de560dSAlex Tomas 	{
3010c9de560dSAlex Tomas 		int i;
3011c9de560dSAlex Tomas 		for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
3012c9de560dSAlex Tomas 			BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
3013c9de560dSAlex Tomas 						bitmap_bh->b_data));
3014c9de560dSAlex Tomas 		}
3015c9de560dSAlex Tomas 	}
3016c9de560dSAlex Tomas #endif
3017c9de560dSAlex Tomas 	spin_lock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
3018e8134b27SAneesh Kumar K.V 	mb_set_bits(NULL, bitmap_bh->b_data,
3019e8134b27SAneesh Kumar K.V 				ac->ac_b_ex.fe_start, ac->ac_b_ex.fe_len);
3020c9de560dSAlex Tomas 	if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
3021c9de560dSAlex Tomas 		gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
3022560671a0SAneesh Kumar K.V 		ext4_free_blks_set(sb, gdp,
3023560671a0SAneesh Kumar K.V 					ext4_free_blocks_after_init(sb,
3024560671a0SAneesh Kumar K.V 					ac->ac_b_ex.fe_group, gdp));
3025c9de560dSAlex Tomas 	}
3026560671a0SAneesh Kumar K.V 	len = ext4_free_blks_count(sb, gdp) - ac->ac_b_ex.fe_len;
3027560671a0SAneesh Kumar K.V 	ext4_free_blks_set(sb, gdp, len);
3028c9de560dSAlex Tomas 	gdp->bg_checksum = ext4_group_desc_csum(sbi, ac->ac_b_ex.fe_group, gdp);
3029c9de560dSAlex Tomas 	spin_unlock(sb_bgl_lock(sbi, ac->ac_b_ex.fe_group));
30306bc6e63fSAneesh Kumar K.V 	percpu_counter_sub(&sbi->s_freeblocks_counter, ac->ac_b_ex.fe_len);
3031d2a17637SMingming Cao 	/*
30326bc6e63fSAneesh Kumar K.V 	 * Now reduce the dirty block count also. Should not go negative
3033d2a17637SMingming Cao 	 */
30346bc6e63fSAneesh Kumar K.V 	if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
30356bc6e63fSAneesh Kumar K.V 		/* release all the reserved blocks if non delalloc */
30366bc6e63fSAneesh Kumar K.V 		percpu_counter_sub(&sbi->s_dirtyblocks_counter, reserv_blks);
303760e58e0fSMingming Cao 	else {
30386bc6e63fSAneesh Kumar K.V 		percpu_counter_sub(&sbi->s_dirtyblocks_counter,
30396bc6e63fSAneesh Kumar K.V 						ac->ac_b_ex.fe_len);
304060e58e0fSMingming Cao 		/* convert reserved quota blocks to real quota blocks */
304160e58e0fSMingming Cao 		vfs_dq_claim_block(ac->ac_inode, ac->ac_b_ex.fe_len);
304260e58e0fSMingming Cao 	}
3043c9de560dSAlex Tomas 
3044772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
3045772cb7c8SJose R. Santos 		ext4_group_t flex_group = ext4_flex_group(sbi,
3046772cb7c8SJose R. Santos 							  ac->ac_b_ex.fe_group);
30479f24e420STheodore Ts'o 		atomic_sub(ac->ac_b_ex.fe_len,
30489f24e420STheodore Ts'o 			   &sbi->s_flex_groups[flex_group].free_blocks);
3049772cb7c8SJose R. Santos 	}
3050772cb7c8SJose R. Santos 
30510390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
3052c9de560dSAlex Tomas 	if (err)
3053c9de560dSAlex Tomas 		goto out_err;
30540390131bSFrank Mayhar 	err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
3055c9de560dSAlex Tomas 
3056c9de560dSAlex Tomas out_err:
3057c9de560dSAlex Tomas 	sb->s_dirt = 1;
305842a10addSAneesh Kumar K.V 	brelse(bitmap_bh);
3059c9de560dSAlex Tomas 	return err;
3060c9de560dSAlex Tomas }
3061c9de560dSAlex Tomas 
3062c9de560dSAlex Tomas /*
3063c9de560dSAlex Tomas  * here we normalize request for locality group
3064c9de560dSAlex Tomas  * Group request are normalized to s_strip size if we set the same via mount
3065c9de560dSAlex Tomas  * option. If not we set it to s_mb_group_prealloc which can be configured via
3066b713a5ecSTheodore Ts'o  * /sys/fs/ext4/<partition>/mb_group_prealloc
3067c9de560dSAlex Tomas  *
3068c9de560dSAlex Tomas  * XXX: should we try to preallocate more than the group has now?
3069c9de560dSAlex Tomas  */
3070c9de560dSAlex Tomas static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
3071c9de560dSAlex Tomas {
3072c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
3073c9de560dSAlex Tomas 	struct ext4_locality_group *lg = ac->ac_lg;
3074c9de560dSAlex Tomas 
3075c9de560dSAlex Tomas 	BUG_ON(lg == NULL);
3076c9de560dSAlex Tomas 	if (EXT4_SB(sb)->s_stripe)
3077c9de560dSAlex Tomas 		ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_stripe;
3078c9de560dSAlex Tomas 	else
3079c9de560dSAlex Tomas 		ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
308060bd63d1SSolofo Ramangalahy 	mb_debug("#%u: goal %u blocks for locality group\n",
3081c9de560dSAlex Tomas 		current->pid, ac->ac_g_ex.fe_len);
3082c9de560dSAlex Tomas }
3083c9de560dSAlex Tomas 
3084c9de560dSAlex Tomas /*
3085c9de560dSAlex Tomas  * Normalization means making request better in terms of
3086c9de560dSAlex Tomas  * size and alignment
3087c9de560dSAlex Tomas  */
30884ddfef7bSEric Sandeen static noinline_for_stack void
30894ddfef7bSEric Sandeen ext4_mb_normalize_request(struct ext4_allocation_context *ac,
3090c9de560dSAlex Tomas 				struct ext4_allocation_request *ar)
3091c9de560dSAlex Tomas {
3092c9de560dSAlex Tomas 	int bsbits, max;
3093c9de560dSAlex Tomas 	ext4_lblk_t end;
3094c9de560dSAlex Tomas 	loff_t size, orig_size, start_off;
3095c9de560dSAlex Tomas 	ext4_lblk_t start, orig_start;
3096c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
30979a0762c5SAneesh Kumar K.V 	struct ext4_prealloc_space *pa;
3098c9de560dSAlex Tomas 
3099c9de560dSAlex Tomas 	/* do normalize only data requests, metadata requests
3100c9de560dSAlex Tomas 	   do not need preallocation */
3101c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3102c9de560dSAlex Tomas 		return;
3103c9de560dSAlex Tomas 
3104c9de560dSAlex Tomas 	/* sometime caller may want exact blocks */
3105c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
3106c9de560dSAlex Tomas 		return;
3107c9de560dSAlex Tomas 
3108c9de560dSAlex Tomas 	/* caller may indicate that preallocation isn't
3109c9de560dSAlex Tomas 	 * required (it's a tail, for example) */
3110c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
3111c9de560dSAlex Tomas 		return;
3112c9de560dSAlex Tomas 
3113c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
3114c9de560dSAlex Tomas 		ext4_mb_normalize_group_request(ac);
3115c9de560dSAlex Tomas 		return ;
3116c9de560dSAlex Tomas 	}
3117c9de560dSAlex Tomas 
3118c9de560dSAlex Tomas 	bsbits = ac->ac_sb->s_blocksize_bits;
3119c9de560dSAlex Tomas 
3120c9de560dSAlex Tomas 	/* first, let's learn actual file size
3121c9de560dSAlex Tomas 	 * given current request is allocated */
3122c9de560dSAlex Tomas 	size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
3123c9de560dSAlex Tomas 	size = size << bsbits;
3124c9de560dSAlex Tomas 	if (size < i_size_read(ac->ac_inode))
3125c9de560dSAlex Tomas 		size = i_size_read(ac->ac_inode);
3126c9de560dSAlex Tomas 
31271930479cSValerie Clement 	/* max size of free chunks */
31281930479cSValerie Clement 	max = 2 << bsbits;
3129c9de560dSAlex Tomas 
31301930479cSValerie Clement #define NRL_CHECK_SIZE(req, size, max, chunk_size)	\
31311930479cSValerie Clement 		(req <= (size) || max <= (chunk_size))
3132c9de560dSAlex Tomas 
3133c9de560dSAlex Tomas 	/* first, try to predict filesize */
3134c9de560dSAlex Tomas 	/* XXX: should this table be tunable? */
3135c9de560dSAlex Tomas 	start_off = 0;
3136c9de560dSAlex Tomas 	if (size <= 16 * 1024) {
3137c9de560dSAlex Tomas 		size = 16 * 1024;
3138c9de560dSAlex Tomas 	} else if (size <= 32 * 1024) {
3139c9de560dSAlex Tomas 		size = 32 * 1024;
3140c9de560dSAlex Tomas 	} else if (size <= 64 * 1024) {
3141c9de560dSAlex Tomas 		size = 64 * 1024;
3142c9de560dSAlex Tomas 	} else if (size <= 128 * 1024) {
3143c9de560dSAlex Tomas 		size = 128 * 1024;
3144c9de560dSAlex Tomas 	} else if (size <= 256 * 1024) {
3145c9de560dSAlex Tomas 		size = 256 * 1024;
3146c9de560dSAlex Tomas 	} else if (size <= 512 * 1024) {
3147c9de560dSAlex Tomas 		size = 512 * 1024;
3148c9de560dSAlex Tomas 	} else if (size <= 1024 * 1024) {
3149c9de560dSAlex Tomas 		size = 1024 * 1024;
31501930479cSValerie Clement 	} else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
3151c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
31521930479cSValerie Clement 						(21 - bsbits)) << 21;
31531930479cSValerie Clement 		size = 2 * 1024 * 1024;
31541930479cSValerie Clement 	} else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
3155c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3156c9de560dSAlex Tomas 							(22 - bsbits)) << 22;
3157c9de560dSAlex Tomas 		size = 4 * 1024 * 1024;
3158c9de560dSAlex Tomas 	} else if (NRL_CHECK_SIZE(ac->ac_o_ex.fe_len,
31591930479cSValerie Clement 					(8<<20)>>bsbits, max, 8 * 1024)) {
3160c9de560dSAlex Tomas 		start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
3161c9de560dSAlex Tomas 							(23 - bsbits)) << 23;
3162c9de560dSAlex Tomas 		size = 8 * 1024 * 1024;
3163c9de560dSAlex Tomas 	} else {
3164c9de560dSAlex Tomas 		start_off = (loff_t)ac->ac_o_ex.fe_logical << bsbits;
3165c9de560dSAlex Tomas 		size	  = ac->ac_o_ex.fe_len << bsbits;
3166c9de560dSAlex Tomas 	}
3167c9de560dSAlex Tomas 	orig_size = size = size >> bsbits;
3168c9de560dSAlex Tomas 	orig_start = start = start_off >> bsbits;
3169c9de560dSAlex Tomas 
3170c9de560dSAlex Tomas 	/* don't cover already allocated blocks in selected range */
3171c9de560dSAlex Tomas 	if (ar->pleft && start <= ar->lleft) {
3172c9de560dSAlex Tomas 		size -= ar->lleft + 1 - start;
3173c9de560dSAlex Tomas 		start = ar->lleft + 1;
3174c9de560dSAlex Tomas 	}
3175c9de560dSAlex Tomas 	if (ar->pright && start + size - 1 >= ar->lright)
3176c9de560dSAlex Tomas 		size -= start + size - ar->lright;
3177c9de560dSAlex Tomas 
3178c9de560dSAlex Tomas 	end = start + size;
3179c9de560dSAlex Tomas 
3180c9de560dSAlex Tomas 	/* check we don't cross already preallocated blocks */
3181c9de560dSAlex Tomas 	rcu_read_lock();
31829a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
3183498e5f24STheodore Ts'o 		ext4_lblk_t pa_end;
3184c9de560dSAlex Tomas 
3185c9de560dSAlex Tomas 		if (pa->pa_deleted)
3186c9de560dSAlex Tomas 			continue;
3187c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3188c9de560dSAlex Tomas 		if (pa->pa_deleted) {
3189c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3190c9de560dSAlex Tomas 			continue;
3191c9de560dSAlex Tomas 		}
3192c9de560dSAlex Tomas 
3193c9de560dSAlex Tomas 		pa_end = pa->pa_lstart + pa->pa_len;
3194c9de560dSAlex Tomas 
3195c9de560dSAlex Tomas 		/* PA must not overlap original request */
3196c9de560dSAlex Tomas 		BUG_ON(!(ac->ac_o_ex.fe_logical >= pa_end ||
3197c9de560dSAlex Tomas 			ac->ac_o_ex.fe_logical < pa->pa_lstart));
3198c9de560dSAlex Tomas 
3199c9de560dSAlex Tomas 		/* skip PA normalized request doesn't overlap with */
3200c9de560dSAlex Tomas 		if (pa->pa_lstart >= end) {
3201c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3202c9de560dSAlex Tomas 			continue;
3203c9de560dSAlex Tomas 		}
3204c9de560dSAlex Tomas 		if (pa_end <= start) {
3205c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3206c9de560dSAlex Tomas 			continue;
3207c9de560dSAlex Tomas 		}
3208c9de560dSAlex Tomas 		BUG_ON(pa->pa_lstart <= start && pa_end >= end);
3209c9de560dSAlex Tomas 
3210c9de560dSAlex Tomas 		if (pa_end <= ac->ac_o_ex.fe_logical) {
3211c9de560dSAlex Tomas 			BUG_ON(pa_end < start);
3212c9de560dSAlex Tomas 			start = pa_end;
3213c9de560dSAlex Tomas 		}
3214c9de560dSAlex Tomas 
3215c9de560dSAlex Tomas 		if (pa->pa_lstart > ac->ac_o_ex.fe_logical) {
3216c9de560dSAlex Tomas 			BUG_ON(pa->pa_lstart > end);
3217c9de560dSAlex Tomas 			end = pa->pa_lstart;
3218c9de560dSAlex Tomas 		}
3219c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3220c9de560dSAlex Tomas 	}
3221c9de560dSAlex Tomas 	rcu_read_unlock();
3222c9de560dSAlex Tomas 	size = end - start;
3223c9de560dSAlex Tomas 
3224c9de560dSAlex Tomas 	/* XXX: extra loop to check we really don't overlap preallocations */
3225c9de560dSAlex Tomas 	rcu_read_lock();
32269a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
3227498e5f24STheodore Ts'o 		ext4_lblk_t pa_end;
3228c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3229c9de560dSAlex Tomas 		if (pa->pa_deleted == 0) {
3230c9de560dSAlex Tomas 			pa_end = pa->pa_lstart + pa->pa_len;
3231c9de560dSAlex Tomas 			BUG_ON(!(start >= pa_end || end <= pa->pa_lstart));
3232c9de560dSAlex Tomas 		}
3233c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3234c9de560dSAlex Tomas 	}
3235c9de560dSAlex Tomas 	rcu_read_unlock();
3236c9de560dSAlex Tomas 
3237c9de560dSAlex Tomas 	if (start + size <= ac->ac_o_ex.fe_logical &&
3238c9de560dSAlex Tomas 			start > ac->ac_o_ex.fe_logical) {
3239c9de560dSAlex Tomas 		printk(KERN_ERR "start %lu, size %lu, fe_logical %lu\n",
3240c9de560dSAlex Tomas 			(unsigned long) start, (unsigned long) size,
3241c9de560dSAlex Tomas 			(unsigned long) ac->ac_o_ex.fe_logical);
3242c9de560dSAlex Tomas 	}
3243c9de560dSAlex Tomas 	BUG_ON(start + size <= ac->ac_o_ex.fe_logical &&
3244c9de560dSAlex Tomas 			start > ac->ac_o_ex.fe_logical);
32458d03c7a0SEric Sandeen 	BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
3246c9de560dSAlex Tomas 
3247c9de560dSAlex Tomas 	/* now prepare goal request */
3248c9de560dSAlex Tomas 
3249c9de560dSAlex Tomas 	/* XXX: is it better to align blocks WRT to logical
3250c9de560dSAlex Tomas 	 * placement or satisfy big request as is */
3251c9de560dSAlex Tomas 	ac->ac_g_ex.fe_logical = start;
3252c9de560dSAlex Tomas 	ac->ac_g_ex.fe_len = size;
3253c9de560dSAlex Tomas 
3254c9de560dSAlex Tomas 	/* define goal start in order to merge */
3255c9de560dSAlex Tomas 	if (ar->pright && (ar->lright == (start + size))) {
3256c9de560dSAlex Tomas 		/* merge to the right */
3257c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
3258c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_group,
3259c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_start);
3260c9de560dSAlex Tomas 		ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3261c9de560dSAlex Tomas 	}
3262c9de560dSAlex Tomas 	if (ar->pleft && (ar->lleft + 1 == start)) {
3263c9de560dSAlex Tomas 		/* merge to the left */
3264c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
3265c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_group,
3266c9de560dSAlex Tomas 						&ac->ac_f_ex.fe_start);
3267c9de560dSAlex Tomas 		ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
3268c9de560dSAlex Tomas 	}
3269c9de560dSAlex Tomas 
3270c9de560dSAlex Tomas 	mb_debug("goal: %u(was %u) blocks at %u\n", (unsigned) size,
3271c9de560dSAlex Tomas 		(unsigned) orig_size, (unsigned) start);
3272c9de560dSAlex Tomas }
3273c9de560dSAlex Tomas 
3274c9de560dSAlex Tomas static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
3275c9de560dSAlex Tomas {
3276c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
3277c9de560dSAlex Tomas 
3278c9de560dSAlex Tomas 	if (sbi->s_mb_stats && ac->ac_g_ex.fe_len > 1) {
3279c9de560dSAlex Tomas 		atomic_inc(&sbi->s_bal_reqs);
3280c9de560dSAlex Tomas 		atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
3281c9de560dSAlex Tomas 		if (ac->ac_o_ex.fe_len >= ac->ac_g_ex.fe_len)
3282c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_success);
3283c9de560dSAlex Tomas 		atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
3284c9de560dSAlex Tomas 		if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
3285c9de560dSAlex Tomas 				ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
3286c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_goals);
3287c9de560dSAlex Tomas 		if (ac->ac_found > sbi->s_mb_max_to_scan)
3288c9de560dSAlex Tomas 			atomic_inc(&sbi->s_bal_breaks);
3289c9de560dSAlex Tomas 	}
3290c9de560dSAlex Tomas 
3291c9de560dSAlex Tomas 	ext4_mb_store_history(ac);
3292c9de560dSAlex Tomas }
3293c9de560dSAlex Tomas 
3294c9de560dSAlex Tomas /*
3295c9de560dSAlex Tomas  * use blocks preallocated to inode
3296c9de560dSAlex Tomas  */
3297c9de560dSAlex Tomas static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
3298c9de560dSAlex Tomas 				struct ext4_prealloc_space *pa)
3299c9de560dSAlex Tomas {
3300c9de560dSAlex Tomas 	ext4_fsblk_t start;
3301c9de560dSAlex Tomas 	ext4_fsblk_t end;
3302c9de560dSAlex Tomas 	int len;
3303c9de560dSAlex Tomas 
3304c9de560dSAlex Tomas 	/* found preallocated blocks, use them */
3305c9de560dSAlex Tomas 	start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
3306c9de560dSAlex Tomas 	end = min(pa->pa_pstart + pa->pa_len, start + ac->ac_o_ex.fe_len);
3307c9de560dSAlex Tomas 	len = end - start;
3308c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
3309c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_start);
3310c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = len;
3311c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
3312c9de560dSAlex Tomas 	ac->ac_pa = pa;
3313c9de560dSAlex Tomas 
3314c9de560dSAlex Tomas 	BUG_ON(start < pa->pa_pstart);
3315c9de560dSAlex Tomas 	BUG_ON(start + len > pa->pa_pstart + pa->pa_len);
3316c9de560dSAlex Tomas 	BUG_ON(pa->pa_free < len);
3317c9de560dSAlex Tomas 	pa->pa_free -= len;
3318c9de560dSAlex Tomas 
331960bd63d1SSolofo Ramangalahy 	mb_debug("use %llu/%u from inode pa %p\n", start, len, pa);
3320c9de560dSAlex Tomas }
3321c9de560dSAlex Tomas 
3322c9de560dSAlex Tomas /*
3323c9de560dSAlex Tomas  * use blocks preallocated to locality group
3324c9de560dSAlex Tomas  */
3325c9de560dSAlex Tomas static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
3326c9de560dSAlex Tomas 				struct ext4_prealloc_space *pa)
3327c9de560dSAlex Tomas {
332803cddb80SAneesh Kumar K.V 	unsigned int len = ac->ac_o_ex.fe_len;
33296be2ded1SAneesh Kumar K.V 
3330c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
3331c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_group,
3332c9de560dSAlex Tomas 					&ac->ac_b_ex.fe_start);
3333c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = len;
3334c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_FOUND;
3335c9de560dSAlex Tomas 	ac->ac_pa = pa;
3336c9de560dSAlex Tomas 
3337c9de560dSAlex Tomas 	/* we don't correct pa_pstart or pa_plen here to avoid
333826346ff6SAneesh Kumar K.V 	 * possible race when the group is being loaded concurrently
3339c9de560dSAlex Tomas 	 * instead we correct pa later, after blocks are marked
334026346ff6SAneesh Kumar K.V 	 * in on-disk bitmap -- see ext4_mb_release_context()
334126346ff6SAneesh Kumar K.V 	 * Other CPUs are prevented from allocating from this pa by lg_mutex
3342c9de560dSAlex Tomas 	 */
3343c9de560dSAlex Tomas 	mb_debug("use %u/%u from group pa %p\n", pa->pa_lstart-len, len, pa);
3344c9de560dSAlex Tomas }
3345c9de560dSAlex Tomas 
3346c9de560dSAlex Tomas /*
33475e745b04SAneesh Kumar K.V  * Return the prealloc space that have minimal distance
33485e745b04SAneesh Kumar K.V  * from the goal block. @cpa is the prealloc
33495e745b04SAneesh Kumar K.V  * space that is having currently known minimal distance
33505e745b04SAneesh Kumar K.V  * from the goal block.
33515e745b04SAneesh Kumar K.V  */
33525e745b04SAneesh Kumar K.V static struct ext4_prealloc_space *
33535e745b04SAneesh Kumar K.V ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
33545e745b04SAneesh Kumar K.V 			struct ext4_prealloc_space *pa,
33555e745b04SAneesh Kumar K.V 			struct ext4_prealloc_space *cpa)
33565e745b04SAneesh Kumar K.V {
33575e745b04SAneesh Kumar K.V 	ext4_fsblk_t cur_distance, new_distance;
33585e745b04SAneesh Kumar K.V 
33595e745b04SAneesh Kumar K.V 	if (cpa == NULL) {
33605e745b04SAneesh Kumar K.V 		atomic_inc(&pa->pa_count);
33615e745b04SAneesh Kumar K.V 		return pa;
33625e745b04SAneesh Kumar K.V 	}
33635e745b04SAneesh Kumar K.V 	cur_distance = abs(goal_block - cpa->pa_pstart);
33645e745b04SAneesh Kumar K.V 	new_distance = abs(goal_block - pa->pa_pstart);
33655e745b04SAneesh Kumar K.V 
33665e745b04SAneesh Kumar K.V 	if (cur_distance < new_distance)
33675e745b04SAneesh Kumar K.V 		return cpa;
33685e745b04SAneesh Kumar K.V 
33695e745b04SAneesh Kumar K.V 	/* drop the previous reference */
33705e745b04SAneesh Kumar K.V 	atomic_dec(&cpa->pa_count);
33715e745b04SAneesh Kumar K.V 	atomic_inc(&pa->pa_count);
33725e745b04SAneesh Kumar K.V 	return pa;
33735e745b04SAneesh Kumar K.V }
33745e745b04SAneesh Kumar K.V 
33755e745b04SAneesh Kumar K.V /*
3376c9de560dSAlex Tomas  * search goal blocks in preallocated space
3377c9de560dSAlex Tomas  */
33784ddfef7bSEric Sandeen static noinline_for_stack int
33794ddfef7bSEric Sandeen ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
3380c9de560dSAlex Tomas {
33816be2ded1SAneesh Kumar K.V 	int order, i;
3382c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
3383c9de560dSAlex Tomas 	struct ext4_locality_group *lg;
33845e745b04SAneesh Kumar K.V 	struct ext4_prealloc_space *pa, *cpa = NULL;
33855e745b04SAneesh Kumar K.V 	ext4_fsblk_t goal_block;
3386c9de560dSAlex Tomas 
3387c9de560dSAlex Tomas 	/* only data can be preallocated */
3388c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
3389c9de560dSAlex Tomas 		return 0;
3390c9de560dSAlex Tomas 
3391c9de560dSAlex Tomas 	/* first, try per-file preallocation */
3392c9de560dSAlex Tomas 	rcu_read_lock();
33939a0762c5SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &ei->i_prealloc_list, pa_inode_list) {
3394c9de560dSAlex Tomas 
3395c9de560dSAlex Tomas 		/* all fields in this condition don't change,
3396c9de560dSAlex Tomas 		 * so we can skip locking for them */
3397c9de560dSAlex Tomas 		if (ac->ac_o_ex.fe_logical < pa->pa_lstart ||
3398c9de560dSAlex Tomas 			ac->ac_o_ex.fe_logical >= pa->pa_lstart + pa->pa_len)
3399c9de560dSAlex Tomas 			continue;
3400c9de560dSAlex Tomas 
3401c9de560dSAlex Tomas 		/* found preallocated blocks, use them */
3402c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3403c9de560dSAlex Tomas 		if (pa->pa_deleted == 0 && pa->pa_free) {
3404c9de560dSAlex Tomas 			atomic_inc(&pa->pa_count);
3405c9de560dSAlex Tomas 			ext4_mb_use_inode_pa(ac, pa);
3406c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3407c9de560dSAlex Tomas 			ac->ac_criteria = 10;
3408c9de560dSAlex Tomas 			rcu_read_unlock();
3409c9de560dSAlex Tomas 			return 1;
3410c9de560dSAlex Tomas 		}
3411c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3412c9de560dSAlex Tomas 	}
3413c9de560dSAlex Tomas 	rcu_read_unlock();
3414c9de560dSAlex Tomas 
3415c9de560dSAlex Tomas 	/* can we use group allocation? */
3416c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
3417c9de560dSAlex Tomas 		return 0;
3418c9de560dSAlex Tomas 
3419c9de560dSAlex Tomas 	/* inode may have no locality group for some reason */
3420c9de560dSAlex Tomas 	lg = ac->ac_lg;
3421c9de560dSAlex Tomas 	if (lg == NULL)
3422c9de560dSAlex Tomas 		return 0;
34236be2ded1SAneesh Kumar K.V 	order  = fls(ac->ac_o_ex.fe_len) - 1;
34246be2ded1SAneesh Kumar K.V 	if (order > PREALLOC_TB_SIZE - 1)
34256be2ded1SAneesh Kumar K.V 		/* The max size of hash table is PREALLOC_TB_SIZE */
34266be2ded1SAneesh Kumar K.V 		order = PREALLOC_TB_SIZE - 1;
3427c9de560dSAlex Tomas 
34285e745b04SAneesh Kumar K.V 	goal_block = ac->ac_g_ex.fe_group * EXT4_BLOCKS_PER_GROUP(ac->ac_sb) +
34295e745b04SAneesh Kumar K.V 		     ac->ac_g_ex.fe_start +
34305e745b04SAneesh Kumar K.V 		     le32_to_cpu(EXT4_SB(ac->ac_sb)->s_es->s_first_data_block);
34315e745b04SAneesh Kumar K.V 	/*
34325e745b04SAneesh Kumar K.V 	 * search for the prealloc space that is having
34335e745b04SAneesh Kumar K.V 	 * minimal distance from the goal block.
34345e745b04SAneesh Kumar K.V 	 */
34356be2ded1SAneesh Kumar K.V 	for (i = order; i < PREALLOC_TB_SIZE; i++) {
3436c9de560dSAlex Tomas 		rcu_read_lock();
34376be2ded1SAneesh Kumar K.V 		list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[i],
34386be2ded1SAneesh Kumar K.V 					pa_inode_list) {
3439c9de560dSAlex Tomas 			spin_lock(&pa->pa_lock);
34406be2ded1SAneesh Kumar K.V 			if (pa->pa_deleted == 0 &&
34416be2ded1SAneesh Kumar K.V 					pa->pa_free >= ac->ac_o_ex.fe_len) {
34425e745b04SAneesh Kumar K.V 
34435e745b04SAneesh Kumar K.V 				cpa = ext4_mb_check_group_pa(goal_block,
34445e745b04SAneesh Kumar K.V 								pa, cpa);
34455e745b04SAneesh Kumar K.V 			}
3446c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
34475e745b04SAneesh Kumar K.V 		}
34485e745b04SAneesh Kumar K.V 		rcu_read_unlock();
34495e745b04SAneesh Kumar K.V 	}
34505e745b04SAneesh Kumar K.V 	if (cpa) {
34515e745b04SAneesh Kumar K.V 		ext4_mb_use_group_pa(ac, cpa);
3452c9de560dSAlex Tomas 		ac->ac_criteria = 20;
3453c9de560dSAlex Tomas 		return 1;
3454c9de560dSAlex Tomas 	}
3455c9de560dSAlex Tomas 	return 0;
3456c9de560dSAlex Tomas }
3457c9de560dSAlex Tomas 
3458c9de560dSAlex Tomas /*
34597a2fcbf7SAneesh Kumar K.V  * the function goes through all block freed in the group
34607a2fcbf7SAneesh Kumar K.V  * but not yet committed and marks them used in in-core bitmap.
34617a2fcbf7SAneesh Kumar K.V  * buddy must be generated from this bitmap
34627a2fcbf7SAneesh Kumar K.V  * Need to be called with ext4 group lock (ext4_lock_group)
34637a2fcbf7SAneesh Kumar K.V  */
34647a2fcbf7SAneesh Kumar K.V static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
34657a2fcbf7SAneesh Kumar K.V 						ext4_group_t group)
34667a2fcbf7SAneesh Kumar K.V {
34677a2fcbf7SAneesh Kumar K.V 	struct rb_node *n;
34687a2fcbf7SAneesh Kumar K.V 	struct ext4_group_info *grp;
34697a2fcbf7SAneesh Kumar K.V 	struct ext4_free_data *entry;
34707a2fcbf7SAneesh Kumar K.V 
34717a2fcbf7SAneesh Kumar K.V 	grp = ext4_get_group_info(sb, group);
34727a2fcbf7SAneesh Kumar K.V 	n = rb_first(&(grp->bb_free_root));
34737a2fcbf7SAneesh Kumar K.V 
34747a2fcbf7SAneesh Kumar K.V 	while (n) {
34757a2fcbf7SAneesh Kumar K.V 		entry = rb_entry(n, struct ext4_free_data, node);
34767a2fcbf7SAneesh Kumar K.V 		mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
34777a2fcbf7SAneesh Kumar K.V 				bitmap, entry->start_blk,
34787a2fcbf7SAneesh Kumar K.V 				entry->count);
34797a2fcbf7SAneesh Kumar K.V 		n = rb_next(n);
34807a2fcbf7SAneesh Kumar K.V 	}
34817a2fcbf7SAneesh Kumar K.V 	return;
34827a2fcbf7SAneesh Kumar K.V }
34837a2fcbf7SAneesh Kumar K.V 
34847a2fcbf7SAneesh Kumar K.V /*
3485c9de560dSAlex Tomas  * the function goes through all preallocation in this group and marks them
3486c9de560dSAlex Tomas  * used in in-core bitmap. buddy must be generated from this bitmap
3487c9de560dSAlex Tomas  * Need to be called with ext4 group lock (ext4_lock_group)
3488c9de560dSAlex Tomas  */
3489c9de560dSAlex Tomas static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
3490c9de560dSAlex Tomas 					ext4_group_t group)
3491c9de560dSAlex Tomas {
3492c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3493c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3494c9de560dSAlex Tomas 	struct list_head *cur;
3495c9de560dSAlex Tomas 	ext4_group_t groupnr;
3496c9de560dSAlex Tomas 	ext4_grpblk_t start;
3497c9de560dSAlex Tomas 	int preallocated = 0;
3498c9de560dSAlex Tomas 	int count = 0;
3499c9de560dSAlex Tomas 	int len;
3500c9de560dSAlex Tomas 
3501c9de560dSAlex Tomas 	/* all form of preallocation discards first load group,
3502c9de560dSAlex Tomas 	 * so the only competing code is preallocation use.
3503c9de560dSAlex Tomas 	 * we don't need any locking here
3504c9de560dSAlex Tomas 	 * notice we do NOT ignore preallocations with pa_deleted
3505c9de560dSAlex Tomas 	 * otherwise we could leave used blocks available for
3506c9de560dSAlex Tomas 	 * allocation in buddy when concurrent ext4_mb_put_pa()
3507c9de560dSAlex Tomas 	 * is dropping preallocation
3508c9de560dSAlex Tomas 	 */
3509c9de560dSAlex Tomas 	list_for_each(cur, &grp->bb_prealloc_list) {
3510c9de560dSAlex Tomas 		pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3511c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3512c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(sb, pa->pa_pstart,
3513c9de560dSAlex Tomas 					     &groupnr, &start);
3514c9de560dSAlex Tomas 		len = pa->pa_len;
3515c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3516c9de560dSAlex Tomas 		if (unlikely(len == 0))
3517c9de560dSAlex Tomas 			continue;
3518c9de560dSAlex Tomas 		BUG_ON(groupnr != group);
3519c9de560dSAlex Tomas 		mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
3520c9de560dSAlex Tomas 						bitmap, start, len);
3521c9de560dSAlex Tomas 		preallocated += len;
3522c9de560dSAlex Tomas 		count++;
3523c9de560dSAlex Tomas 	}
3524a9df9a49STheodore Ts'o 	mb_debug("prellocated %u for group %u\n", preallocated, group);
3525c9de560dSAlex Tomas }
3526c9de560dSAlex Tomas 
3527c9de560dSAlex Tomas static void ext4_mb_pa_callback(struct rcu_head *head)
3528c9de560dSAlex Tomas {
3529c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3530c9de560dSAlex Tomas 	pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
3531c9de560dSAlex Tomas 	kmem_cache_free(ext4_pspace_cachep, pa);
3532c9de560dSAlex Tomas }
3533c9de560dSAlex Tomas 
3534c9de560dSAlex Tomas /*
3535c9de560dSAlex Tomas  * drops a reference to preallocated space descriptor
3536c9de560dSAlex Tomas  * if this was the last reference and the space is consumed
3537c9de560dSAlex Tomas  */
3538c9de560dSAlex Tomas static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
3539c9de560dSAlex Tomas 			struct super_block *sb, struct ext4_prealloc_space *pa)
3540c9de560dSAlex Tomas {
3541a9df9a49STheodore Ts'o 	ext4_group_t grp;
3542d33a1976SEric Sandeen 	ext4_fsblk_t grp_blk;
3543c9de560dSAlex Tomas 
3544c9de560dSAlex Tomas 	if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0)
3545c9de560dSAlex Tomas 		return;
3546c9de560dSAlex Tomas 
3547c9de560dSAlex Tomas 	/* in this short window concurrent discard can set pa_deleted */
3548c9de560dSAlex Tomas 	spin_lock(&pa->pa_lock);
3549c9de560dSAlex Tomas 	if (pa->pa_deleted == 1) {
3550c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3551c9de560dSAlex Tomas 		return;
3552c9de560dSAlex Tomas 	}
3553c9de560dSAlex Tomas 
3554c9de560dSAlex Tomas 	pa->pa_deleted = 1;
3555c9de560dSAlex Tomas 	spin_unlock(&pa->pa_lock);
3556c9de560dSAlex Tomas 
3557d33a1976SEric Sandeen 	grp_blk = pa->pa_pstart;
3558d33a1976SEric Sandeen 	/* If linear, pa_pstart may be in the next group when pa is used up */
3559d33a1976SEric Sandeen 	if (pa->pa_linear)
3560d33a1976SEric Sandeen 		grp_blk--;
3561d33a1976SEric Sandeen 
3562d33a1976SEric Sandeen 	ext4_get_group_no_and_offset(sb, grp_blk, &grp, NULL);
3563c9de560dSAlex Tomas 
3564c9de560dSAlex Tomas 	/*
3565c9de560dSAlex Tomas 	 * possible race:
3566c9de560dSAlex Tomas 	 *
3567c9de560dSAlex Tomas 	 *  P1 (buddy init)			P2 (regular allocation)
3568c9de560dSAlex Tomas 	 *					find block B in PA
3569c9de560dSAlex Tomas 	 *  copy on-disk bitmap to buddy
3570c9de560dSAlex Tomas 	 *  					mark B in on-disk bitmap
3571c9de560dSAlex Tomas 	 *					drop PA from group
3572c9de560dSAlex Tomas 	 *  mark all PAs in buddy
3573c9de560dSAlex Tomas 	 *
3574c9de560dSAlex Tomas 	 * thus, P1 initializes buddy with B available. to prevent this
3575c9de560dSAlex Tomas 	 * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
3576c9de560dSAlex Tomas 	 * against that pair
3577c9de560dSAlex Tomas 	 */
3578c9de560dSAlex Tomas 	ext4_lock_group(sb, grp);
3579c9de560dSAlex Tomas 	list_del(&pa->pa_group_list);
3580c9de560dSAlex Tomas 	ext4_unlock_group(sb, grp);
3581c9de560dSAlex Tomas 
3582c9de560dSAlex Tomas 	spin_lock(pa->pa_obj_lock);
3583c9de560dSAlex Tomas 	list_del_rcu(&pa->pa_inode_list);
3584c9de560dSAlex Tomas 	spin_unlock(pa->pa_obj_lock);
3585c9de560dSAlex Tomas 
3586c9de560dSAlex Tomas 	call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3587c9de560dSAlex Tomas }
3588c9de560dSAlex Tomas 
3589c9de560dSAlex Tomas /*
3590c9de560dSAlex Tomas  * creates new preallocated space for given inode
3591c9de560dSAlex Tomas  */
35924ddfef7bSEric Sandeen static noinline_for_stack int
35934ddfef7bSEric Sandeen ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
3594c9de560dSAlex Tomas {
3595c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
3596c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3597c9de560dSAlex Tomas 	struct ext4_group_info *grp;
3598c9de560dSAlex Tomas 	struct ext4_inode_info *ei;
3599c9de560dSAlex Tomas 
3600c9de560dSAlex Tomas 	/* preallocate only when found space is larger then requested */
3601c9de560dSAlex Tomas 	BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3602c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3603c9de560dSAlex Tomas 	BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3604c9de560dSAlex Tomas 
3605c9de560dSAlex Tomas 	pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3606c9de560dSAlex Tomas 	if (pa == NULL)
3607c9de560dSAlex Tomas 		return -ENOMEM;
3608c9de560dSAlex Tomas 
3609c9de560dSAlex Tomas 	if (ac->ac_b_ex.fe_len < ac->ac_g_ex.fe_len) {
3610c9de560dSAlex Tomas 		int winl;
3611c9de560dSAlex Tomas 		int wins;
3612c9de560dSAlex Tomas 		int win;
3613c9de560dSAlex Tomas 		int offs;
3614c9de560dSAlex Tomas 
3615c9de560dSAlex Tomas 		/* we can't allocate as much as normalizer wants.
3616c9de560dSAlex Tomas 		 * so, found space must get proper lstart
3617c9de560dSAlex Tomas 		 * to cover original request */
3618c9de560dSAlex Tomas 		BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
3619c9de560dSAlex Tomas 		BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
3620c9de560dSAlex Tomas 
3621c9de560dSAlex Tomas 		/* we're limited by original request in that
3622c9de560dSAlex Tomas 		 * logical block must be covered any way
3623c9de560dSAlex Tomas 		 * winl is window we can move our chunk within */
3624c9de560dSAlex Tomas 		winl = ac->ac_o_ex.fe_logical - ac->ac_g_ex.fe_logical;
3625c9de560dSAlex Tomas 
3626c9de560dSAlex Tomas 		/* also, we should cover whole original request */
3627c9de560dSAlex Tomas 		wins = ac->ac_b_ex.fe_len - ac->ac_o_ex.fe_len;
3628c9de560dSAlex Tomas 
3629c9de560dSAlex Tomas 		/* the smallest one defines real window */
3630c9de560dSAlex Tomas 		win = min(winl, wins);
3631c9de560dSAlex Tomas 
3632c9de560dSAlex Tomas 		offs = ac->ac_o_ex.fe_logical % ac->ac_b_ex.fe_len;
3633c9de560dSAlex Tomas 		if (offs && offs < win)
3634c9de560dSAlex Tomas 			win = offs;
3635c9de560dSAlex Tomas 
3636c9de560dSAlex Tomas 		ac->ac_b_ex.fe_logical = ac->ac_o_ex.fe_logical - win;
3637c9de560dSAlex Tomas 		BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
3638c9de560dSAlex Tomas 		BUG_ON(ac->ac_o_ex.fe_len > ac->ac_b_ex.fe_len);
3639c9de560dSAlex Tomas 	}
3640c9de560dSAlex Tomas 
3641c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
3642c9de560dSAlex Tomas 	 * allocated blocks for history */
3643c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
3644c9de560dSAlex Tomas 
3645c9de560dSAlex Tomas 	pa->pa_lstart = ac->ac_b_ex.fe_logical;
3646c9de560dSAlex Tomas 	pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3647c9de560dSAlex Tomas 	pa->pa_len = ac->ac_b_ex.fe_len;
3648c9de560dSAlex Tomas 	pa->pa_free = pa->pa_len;
3649c9de560dSAlex Tomas 	atomic_set(&pa->pa_count, 1);
3650c9de560dSAlex Tomas 	spin_lock_init(&pa->pa_lock);
3651d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_inode_list);
3652d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_group_list);
3653c9de560dSAlex Tomas 	pa->pa_deleted = 0;
3654c9de560dSAlex Tomas 	pa->pa_linear = 0;
3655c9de560dSAlex Tomas 
3656c9de560dSAlex Tomas 	mb_debug("new inode pa %p: %llu/%u for %u\n", pa,
3657c9de560dSAlex Tomas 			pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3658ba80b101STheodore Ts'o 	trace_mark(ext4_mb_new_inode_pa,
3659ba80b101STheodore Ts'o 		   "dev %s ino %lu pstart %llu len %u lstart %u",
3660ba80b101STheodore Ts'o 		   sb->s_id, ac->ac_inode->i_ino,
3661ba80b101STheodore Ts'o 		   pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3662c9de560dSAlex Tomas 
3663c9de560dSAlex Tomas 	ext4_mb_use_inode_pa(ac, pa);
3664c9de560dSAlex Tomas 	atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3665c9de560dSAlex Tomas 
3666c9de560dSAlex Tomas 	ei = EXT4_I(ac->ac_inode);
3667c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3668c9de560dSAlex Tomas 
3669c9de560dSAlex Tomas 	pa->pa_obj_lock = &ei->i_prealloc_lock;
3670c9de560dSAlex Tomas 	pa->pa_inode = ac->ac_inode;
3671c9de560dSAlex Tomas 
3672c9de560dSAlex Tomas 	ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3673c9de560dSAlex Tomas 	list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3674c9de560dSAlex Tomas 	ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3675c9de560dSAlex Tomas 
3676c9de560dSAlex Tomas 	spin_lock(pa->pa_obj_lock);
3677c9de560dSAlex Tomas 	list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
3678c9de560dSAlex Tomas 	spin_unlock(pa->pa_obj_lock);
3679c9de560dSAlex Tomas 
3680c9de560dSAlex Tomas 	return 0;
3681c9de560dSAlex Tomas }
3682c9de560dSAlex Tomas 
3683c9de560dSAlex Tomas /*
3684c9de560dSAlex Tomas  * creates new preallocated space for locality group inodes belongs to
3685c9de560dSAlex Tomas  */
36864ddfef7bSEric Sandeen static noinline_for_stack int
36874ddfef7bSEric Sandeen ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
3688c9de560dSAlex Tomas {
3689c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
3690c9de560dSAlex Tomas 	struct ext4_locality_group *lg;
3691c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa;
3692c9de560dSAlex Tomas 	struct ext4_group_info *grp;
3693c9de560dSAlex Tomas 
3694c9de560dSAlex Tomas 	/* preallocate only when found space is larger then requested */
3695c9de560dSAlex Tomas 	BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
3696c9de560dSAlex Tomas 	BUG_ON(ac->ac_status != AC_STATUS_FOUND);
3697c9de560dSAlex Tomas 	BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
3698c9de560dSAlex Tomas 
3699c9de560dSAlex Tomas 	BUG_ON(ext4_pspace_cachep == NULL);
3700c9de560dSAlex Tomas 	pa = kmem_cache_alloc(ext4_pspace_cachep, GFP_NOFS);
3701c9de560dSAlex Tomas 	if (pa == NULL)
3702c9de560dSAlex Tomas 		return -ENOMEM;
3703c9de560dSAlex Tomas 
3704c9de560dSAlex Tomas 	/* preallocation can change ac_b_ex, thus we store actually
3705c9de560dSAlex Tomas 	 * allocated blocks for history */
3706c9de560dSAlex Tomas 	ac->ac_f_ex = ac->ac_b_ex;
3707c9de560dSAlex Tomas 
3708c9de560dSAlex Tomas 	pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
3709c9de560dSAlex Tomas 	pa->pa_lstart = pa->pa_pstart;
3710c9de560dSAlex Tomas 	pa->pa_len = ac->ac_b_ex.fe_len;
3711c9de560dSAlex Tomas 	pa->pa_free = pa->pa_len;
3712c9de560dSAlex Tomas 	atomic_set(&pa->pa_count, 1);
3713c9de560dSAlex Tomas 	spin_lock_init(&pa->pa_lock);
37146be2ded1SAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_inode_list);
3715d794bf8eSAneesh Kumar K.V 	INIT_LIST_HEAD(&pa->pa_group_list);
3716c9de560dSAlex Tomas 	pa->pa_deleted = 0;
3717c9de560dSAlex Tomas 	pa->pa_linear = 1;
3718c9de560dSAlex Tomas 
3719c9de560dSAlex Tomas 	mb_debug("new group pa %p: %llu/%u for %u\n", pa,
3720c9de560dSAlex Tomas 		 pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3721ba80b101STheodore Ts'o 	trace_mark(ext4_mb_new_group_pa, "dev %s pstart %llu len %u lstart %u",
3722ba80b101STheodore Ts'o 		   sb->s_id, pa->pa_pstart, pa->pa_len, pa->pa_lstart);
3723c9de560dSAlex Tomas 
3724c9de560dSAlex Tomas 	ext4_mb_use_group_pa(ac, pa);
3725c9de560dSAlex Tomas 	atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
3726c9de560dSAlex Tomas 
3727c9de560dSAlex Tomas 	grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
3728c9de560dSAlex Tomas 	lg = ac->ac_lg;
3729c9de560dSAlex Tomas 	BUG_ON(lg == NULL);
3730c9de560dSAlex Tomas 
3731c9de560dSAlex Tomas 	pa->pa_obj_lock = &lg->lg_prealloc_lock;
3732c9de560dSAlex Tomas 	pa->pa_inode = NULL;
3733c9de560dSAlex Tomas 
3734c9de560dSAlex Tomas 	ext4_lock_group(sb, ac->ac_b_ex.fe_group);
3735c9de560dSAlex Tomas 	list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
3736c9de560dSAlex Tomas 	ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
3737c9de560dSAlex Tomas 
37386be2ded1SAneesh Kumar K.V 	/*
37396be2ded1SAneesh Kumar K.V 	 * We will later add the new pa to the right bucket
37406be2ded1SAneesh Kumar K.V 	 * after updating the pa_free in ext4_mb_release_context
37416be2ded1SAneesh Kumar K.V 	 */
3742c9de560dSAlex Tomas 	return 0;
3743c9de560dSAlex Tomas }
3744c9de560dSAlex Tomas 
3745c9de560dSAlex Tomas static int ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
3746c9de560dSAlex Tomas {
3747c9de560dSAlex Tomas 	int err;
3748c9de560dSAlex Tomas 
3749c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
3750c9de560dSAlex Tomas 		err = ext4_mb_new_group_pa(ac);
3751c9de560dSAlex Tomas 	else
3752c9de560dSAlex Tomas 		err = ext4_mb_new_inode_pa(ac);
3753c9de560dSAlex Tomas 	return err;
3754c9de560dSAlex Tomas }
3755c9de560dSAlex Tomas 
3756c9de560dSAlex Tomas /*
3757c9de560dSAlex Tomas  * finds all unused blocks in on-disk bitmap, frees them in
3758c9de560dSAlex Tomas  * in-core bitmap and buddy.
3759c9de560dSAlex Tomas  * @pa must be unlinked from inode and group lists, so that
3760c9de560dSAlex Tomas  * nobody else can find/use it.
3761c9de560dSAlex Tomas  * the caller MUST hold group/inode locks.
3762c9de560dSAlex Tomas  * TODO: optimize the case when there are no in-core structures yet
3763c9de560dSAlex Tomas  */
37644ddfef7bSEric Sandeen static noinline_for_stack int
37654ddfef7bSEric Sandeen ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
3766c83617dbSAneesh Kumar K.V 			struct ext4_prealloc_space *pa,
3767c83617dbSAneesh Kumar K.V 			struct ext4_allocation_context *ac)
3768c9de560dSAlex Tomas {
3769c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
3770c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3771498e5f24STheodore Ts'o 	unsigned int end;
3772498e5f24STheodore Ts'o 	unsigned int next;
3773c9de560dSAlex Tomas 	ext4_group_t group;
3774c9de560dSAlex Tomas 	ext4_grpblk_t bit;
3775ba80b101STheodore Ts'o 	unsigned long long grp_blk_start;
3776c9de560dSAlex Tomas 	sector_t start;
3777c9de560dSAlex Tomas 	int err = 0;
3778c9de560dSAlex Tomas 	int free = 0;
3779c9de560dSAlex Tomas 
3780c9de560dSAlex Tomas 	BUG_ON(pa->pa_deleted == 0);
3781c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3782ba80b101STheodore Ts'o 	grp_blk_start = pa->pa_pstart - bit;
3783c9de560dSAlex Tomas 	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3784c9de560dSAlex Tomas 	end = bit + pa->pa_len;
3785c9de560dSAlex Tomas 
3786256bdb49SEric Sandeen 	if (ac) {
3787256bdb49SEric Sandeen 		ac->ac_sb = sb;
3788256bdb49SEric Sandeen 		ac->ac_inode = pa->pa_inode;
3789256bdb49SEric Sandeen 		ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3790256bdb49SEric Sandeen 	}
3791c9de560dSAlex Tomas 
3792c9de560dSAlex Tomas 	while (bit < end) {
3793ffad0a44SAneesh Kumar K.V 		bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
3794c9de560dSAlex Tomas 		if (bit >= end)
3795c9de560dSAlex Tomas 			break;
3796ffad0a44SAneesh Kumar K.V 		next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
3797c9de560dSAlex Tomas 		start = group * EXT4_BLOCKS_PER_GROUP(sb) + bit +
3798c9de560dSAlex Tomas 				le32_to_cpu(sbi->s_es->s_first_data_block);
3799c9de560dSAlex Tomas 		mb_debug("    free preallocated %u/%u in group %u\n",
3800c9de560dSAlex Tomas 				(unsigned) start, (unsigned) next - bit,
3801c9de560dSAlex Tomas 				(unsigned) group);
3802c9de560dSAlex Tomas 		free += next - bit;
3803c9de560dSAlex Tomas 
3804256bdb49SEric Sandeen 		if (ac) {
3805256bdb49SEric Sandeen 			ac->ac_b_ex.fe_group = group;
3806256bdb49SEric Sandeen 			ac->ac_b_ex.fe_start = bit;
3807256bdb49SEric Sandeen 			ac->ac_b_ex.fe_len = next - bit;
3808256bdb49SEric Sandeen 			ac->ac_b_ex.fe_logical = 0;
3809256bdb49SEric Sandeen 			ext4_mb_store_history(ac);
3810256bdb49SEric Sandeen 		}
3811c9de560dSAlex Tomas 
3812ba80b101STheodore Ts'o 		trace_mark(ext4_mb_release_inode_pa,
3813ba80b101STheodore Ts'o 			   "dev %s ino %lu block %llu count %u",
3814ba80b101STheodore Ts'o 			   sb->s_id, pa->pa_inode->i_ino, grp_blk_start + bit,
3815ba80b101STheodore Ts'o 			   next - bit);
3816c9de560dSAlex Tomas 		mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
3817c9de560dSAlex Tomas 		bit = next + 1;
3818c9de560dSAlex Tomas 	}
3819c9de560dSAlex Tomas 	if (free != pa->pa_free) {
382026346ff6SAneesh Kumar K.V 		printk(KERN_CRIT "pa %p: logic %lu, phys. %lu, len %lu\n",
3821c9de560dSAlex Tomas 			pa, (unsigned long) pa->pa_lstart,
3822c9de560dSAlex Tomas 			(unsigned long) pa->pa_pstart,
3823c9de560dSAlex Tomas 			(unsigned long) pa->pa_len);
38245d1b1b3fSAneesh Kumar K.V 		ext4_grp_locked_error(sb, group,
38255d1b1b3fSAneesh Kumar K.V 					__func__, "free %u, pa_free %u",
382626346ff6SAneesh Kumar K.V 					free, pa->pa_free);
3827e56eb659SAneesh Kumar K.V 		/*
3828e56eb659SAneesh Kumar K.V 		 * pa is already deleted so we use the value obtained
3829e56eb659SAneesh Kumar K.V 		 * from the bitmap and continue.
3830e56eb659SAneesh Kumar K.V 		 */
3831c9de560dSAlex Tomas 	}
3832c9de560dSAlex Tomas 	atomic_add(free, &sbi->s_mb_discarded);
3833c9de560dSAlex Tomas 
3834c9de560dSAlex Tomas 	return err;
3835c9de560dSAlex Tomas }
3836c9de560dSAlex Tomas 
38374ddfef7bSEric Sandeen static noinline_for_stack int
38384ddfef7bSEric Sandeen ext4_mb_release_group_pa(struct ext4_buddy *e4b,
3839c83617dbSAneesh Kumar K.V 				struct ext4_prealloc_space *pa,
3840c83617dbSAneesh Kumar K.V 				struct ext4_allocation_context *ac)
3841c9de560dSAlex Tomas {
3842c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
3843c9de560dSAlex Tomas 	ext4_group_t group;
3844c9de560dSAlex Tomas 	ext4_grpblk_t bit;
3845c9de560dSAlex Tomas 
3846256bdb49SEric Sandeen 	if (ac)
3847256bdb49SEric Sandeen 		ac->ac_op = EXT4_MB_HISTORY_DISCARD;
3848c9de560dSAlex Tomas 
3849ba80b101STheodore Ts'o 	trace_mark(ext4_mb_release_group_pa, "dev %s pstart %llu len %d",
3850ba80b101STheodore Ts'o 		   sb->s_id, pa->pa_pstart, pa->pa_len);
3851c9de560dSAlex Tomas 	BUG_ON(pa->pa_deleted == 0);
3852c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
3853c9de560dSAlex Tomas 	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
3854c9de560dSAlex Tomas 	mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
3855c9de560dSAlex Tomas 	atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
3856c9de560dSAlex Tomas 
3857256bdb49SEric Sandeen 	if (ac) {
3858256bdb49SEric Sandeen 		ac->ac_sb = sb;
3859256bdb49SEric Sandeen 		ac->ac_inode = NULL;
3860256bdb49SEric Sandeen 		ac->ac_b_ex.fe_group = group;
3861256bdb49SEric Sandeen 		ac->ac_b_ex.fe_start = bit;
3862256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = pa->pa_len;
3863256bdb49SEric Sandeen 		ac->ac_b_ex.fe_logical = 0;
3864256bdb49SEric Sandeen 		ext4_mb_store_history(ac);
3865256bdb49SEric Sandeen 	}
3866c9de560dSAlex Tomas 
3867c9de560dSAlex Tomas 	return 0;
3868c9de560dSAlex Tomas }
3869c9de560dSAlex Tomas 
3870c9de560dSAlex Tomas /*
3871c9de560dSAlex Tomas  * releases all preallocations in given group
3872c9de560dSAlex Tomas  *
3873c9de560dSAlex Tomas  * first, we need to decide discard policy:
3874c9de560dSAlex Tomas  * - when do we discard
3875c9de560dSAlex Tomas  *   1) ENOSPC
3876c9de560dSAlex Tomas  * - how many do we discard
3877c9de560dSAlex Tomas  *   1) how many requested
3878c9de560dSAlex Tomas  */
38794ddfef7bSEric Sandeen static noinline_for_stack int
38804ddfef7bSEric Sandeen ext4_mb_discard_group_preallocations(struct super_block *sb,
3881c9de560dSAlex Tomas 					ext4_group_t group, int needed)
3882c9de560dSAlex Tomas {
3883c9de560dSAlex Tomas 	struct ext4_group_info *grp = ext4_get_group_info(sb, group);
3884c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
3885c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa, *tmp;
3886c83617dbSAneesh Kumar K.V 	struct ext4_allocation_context *ac;
3887c9de560dSAlex Tomas 	struct list_head list;
3888c9de560dSAlex Tomas 	struct ext4_buddy e4b;
3889c9de560dSAlex Tomas 	int err;
3890c9de560dSAlex Tomas 	int busy = 0;
3891c9de560dSAlex Tomas 	int free = 0;
3892c9de560dSAlex Tomas 
3893a9df9a49STheodore Ts'o 	mb_debug("discard preallocation for group %u\n", group);
3894c9de560dSAlex Tomas 
3895c9de560dSAlex Tomas 	if (list_empty(&grp->bb_prealloc_list))
3896c9de560dSAlex Tomas 		return 0;
3897c9de560dSAlex Tomas 
3898574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, group);
3899c9de560dSAlex Tomas 	if (bitmap_bh == NULL) {
3900ce89f46cSAneesh Kumar K.V 		ext4_error(sb, __func__, "Error in reading block "
3901a9df9a49STheodore Ts'o 				"bitmap for %u", group);
3902ce89f46cSAneesh Kumar K.V 		return 0;
3903c9de560dSAlex Tomas 	}
3904c9de560dSAlex Tomas 
3905c9de560dSAlex Tomas 	err = ext4_mb_load_buddy(sb, group, &e4b);
3906ce89f46cSAneesh Kumar K.V 	if (err) {
3907ce89f46cSAneesh Kumar K.V 		ext4_error(sb, __func__, "Error in loading buddy "
3908a9df9a49STheodore Ts'o 				"information for %u", group);
3909ce89f46cSAneesh Kumar K.V 		put_bh(bitmap_bh);
3910ce89f46cSAneesh Kumar K.V 		return 0;
3911ce89f46cSAneesh Kumar K.V 	}
3912c9de560dSAlex Tomas 
3913c9de560dSAlex Tomas 	if (needed == 0)
3914c9de560dSAlex Tomas 		needed = EXT4_BLOCKS_PER_GROUP(sb) + 1;
3915c9de560dSAlex Tomas 
3916c9de560dSAlex Tomas 	INIT_LIST_HEAD(&list);
3917c83617dbSAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
3918c9de560dSAlex Tomas repeat:
3919c9de560dSAlex Tomas 	ext4_lock_group(sb, group);
3920c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp,
3921c9de560dSAlex Tomas 				&grp->bb_prealloc_list, pa_group_list) {
3922c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
3923c9de560dSAlex Tomas 		if (atomic_read(&pa->pa_count)) {
3924c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3925c9de560dSAlex Tomas 			busy = 1;
3926c9de560dSAlex Tomas 			continue;
3927c9de560dSAlex Tomas 		}
3928c9de560dSAlex Tomas 		if (pa->pa_deleted) {
3929c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
3930c9de560dSAlex Tomas 			continue;
3931c9de560dSAlex Tomas 		}
3932c9de560dSAlex Tomas 
3933c9de560dSAlex Tomas 		/* seems this one can be freed ... */
3934c9de560dSAlex Tomas 		pa->pa_deleted = 1;
3935c9de560dSAlex Tomas 
3936c9de560dSAlex Tomas 		/* we can trust pa_free ... */
3937c9de560dSAlex Tomas 		free += pa->pa_free;
3938c9de560dSAlex Tomas 
3939c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
3940c9de560dSAlex Tomas 
3941c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
3942c9de560dSAlex Tomas 		list_add(&pa->u.pa_tmp_list, &list);
3943c9de560dSAlex Tomas 	}
3944c9de560dSAlex Tomas 
3945c9de560dSAlex Tomas 	/* if we still need more blocks and some PAs were used, try again */
3946c9de560dSAlex Tomas 	if (free < needed && busy) {
3947c9de560dSAlex Tomas 		busy = 0;
3948c9de560dSAlex Tomas 		ext4_unlock_group(sb, group);
3949c9de560dSAlex Tomas 		/*
3950c9de560dSAlex Tomas 		 * Yield the CPU here so that we don't get soft lockup
3951c9de560dSAlex Tomas 		 * in non preempt case.
3952c9de560dSAlex Tomas 		 */
3953c9de560dSAlex Tomas 		yield();
3954c9de560dSAlex Tomas 		goto repeat;
3955c9de560dSAlex Tomas 	}
3956c9de560dSAlex Tomas 
3957c9de560dSAlex Tomas 	/* found anything to free? */
3958c9de560dSAlex Tomas 	if (list_empty(&list)) {
3959c9de560dSAlex Tomas 		BUG_ON(free != 0);
3960c9de560dSAlex Tomas 		goto out;
3961c9de560dSAlex Tomas 	}
3962c9de560dSAlex Tomas 
3963c9de560dSAlex Tomas 	/* now free all selected PAs */
3964c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
3965c9de560dSAlex Tomas 
3966c9de560dSAlex Tomas 		/* remove from object (inode or locality group) */
3967c9de560dSAlex Tomas 		spin_lock(pa->pa_obj_lock);
3968c9de560dSAlex Tomas 		list_del_rcu(&pa->pa_inode_list);
3969c9de560dSAlex Tomas 		spin_unlock(pa->pa_obj_lock);
3970c9de560dSAlex Tomas 
3971c9de560dSAlex Tomas 		if (pa->pa_linear)
3972c83617dbSAneesh Kumar K.V 			ext4_mb_release_group_pa(&e4b, pa, ac);
3973c9de560dSAlex Tomas 		else
3974c83617dbSAneesh Kumar K.V 			ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
3975c9de560dSAlex Tomas 
3976c9de560dSAlex Tomas 		list_del(&pa->u.pa_tmp_list);
3977c9de560dSAlex Tomas 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
3978c9de560dSAlex Tomas 	}
3979c9de560dSAlex Tomas 
3980c9de560dSAlex Tomas out:
3981c9de560dSAlex Tomas 	ext4_unlock_group(sb, group);
3982c83617dbSAneesh Kumar K.V 	if (ac)
3983c83617dbSAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
3984c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
3985c9de560dSAlex Tomas 	put_bh(bitmap_bh);
3986c9de560dSAlex Tomas 	return free;
3987c9de560dSAlex Tomas }
3988c9de560dSAlex Tomas 
3989c9de560dSAlex Tomas /*
3990c9de560dSAlex Tomas  * releases all non-used preallocated blocks for given inode
3991c9de560dSAlex Tomas  *
3992c9de560dSAlex Tomas  * It's important to discard preallocations under i_data_sem
3993c9de560dSAlex Tomas  * We don't want another block to be served from the prealloc
3994c9de560dSAlex Tomas  * space when we are discarding the inode prealloc space.
3995c9de560dSAlex Tomas  *
3996c9de560dSAlex Tomas  * FIXME!! Make sure it is valid at all the call sites
3997c9de560dSAlex Tomas  */
3998c2ea3fdeSTheodore Ts'o void ext4_discard_preallocations(struct inode *inode)
3999c9de560dSAlex Tomas {
4000c9de560dSAlex Tomas 	struct ext4_inode_info *ei = EXT4_I(inode);
4001c9de560dSAlex Tomas 	struct super_block *sb = inode->i_sb;
4002c9de560dSAlex Tomas 	struct buffer_head *bitmap_bh = NULL;
4003c9de560dSAlex Tomas 	struct ext4_prealloc_space *pa, *tmp;
4004c83617dbSAneesh Kumar K.V 	struct ext4_allocation_context *ac;
4005c9de560dSAlex Tomas 	ext4_group_t group = 0;
4006c9de560dSAlex Tomas 	struct list_head list;
4007c9de560dSAlex Tomas 	struct ext4_buddy e4b;
4008c9de560dSAlex Tomas 	int err;
4009c9de560dSAlex Tomas 
4010c2ea3fdeSTheodore Ts'o 	if (!S_ISREG(inode->i_mode)) {
4011c9de560dSAlex Tomas 		/*BUG_ON(!list_empty(&ei->i_prealloc_list));*/
4012c9de560dSAlex Tomas 		return;
4013c9de560dSAlex Tomas 	}
4014c9de560dSAlex Tomas 
4015c9de560dSAlex Tomas 	mb_debug("discard preallocation for inode %lu\n", inode->i_ino);
4016ba80b101STheodore Ts'o 	trace_mark(ext4_discard_preallocations, "dev %s ino %lu", sb->s_id,
4017ba80b101STheodore Ts'o 		   inode->i_ino);
4018c9de560dSAlex Tomas 
4019c9de560dSAlex Tomas 	INIT_LIST_HEAD(&list);
4020c9de560dSAlex Tomas 
4021c83617dbSAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4022c9de560dSAlex Tomas repeat:
4023c9de560dSAlex Tomas 	/* first, collect all pa's in the inode */
4024c9de560dSAlex Tomas 	spin_lock(&ei->i_prealloc_lock);
4025c9de560dSAlex Tomas 	while (!list_empty(&ei->i_prealloc_list)) {
4026c9de560dSAlex Tomas 		pa = list_entry(ei->i_prealloc_list.next,
4027c9de560dSAlex Tomas 				struct ext4_prealloc_space, pa_inode_list);
4028c9de560dSAlex Tomas 		BUG_ON(pa->pa_obj_lock != &ei->i_prealloc_lock);
4029c9de560dSAlex Tomas 		spin_lock(&pa->pa_lock);
4030c9de560dSAlex Tomas 		if (atomic_read(&pa->pa_count)) {
4031c9de560dSAlex Tomas 			/* this shouldn't happen often - nobody should
4032c9de560dSAlex Tomas 			 * use preallocation while we're discarding it */
4033c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
4034c9de560dSAlex Tomas 			spin_unlock(&ei->i_prealloc_lock);
4035c9de560dSAlex Tomas 			printk(KERN_ERR "uh-oh! used pa while discarding\n");
4036c9de560dSAlex Tomas 			WARN_ON(1);
4037c9de560dSAlex Tomas 			schedule_timeout_uninterruptible(HZ);
4038c9de560dSAlex Tomas 			goto repeat;
4039c9de560dSAlex Tomas 
4040c9de560dSAlex Tomas 		}
4041c9de560dSAlex Tomas 		if (pa->pa_deleted == 0) {
4042c9de560dSAlex Tomas 			pa->pa_deleted = 1;
4043c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
4044c9de560dSAlex Tomas 			list_del_rcu(&pa->pa_inode_list);
4045c9de560dSAlex Tomas 			list_add(&pa->u.pa_tmp_list, &list);
4046c9de560dSAlex Tomas 			continue;
4047c9de560dSAlex Tomas 		}
4048c9de560dSAlex Tomas 
4049c9de560dSAlex Tomas 		/* someone is deleting pa right now */
4050c9de560dSAlex Tomas 		spin_unlock(&pa->pa_lock);
4051c9de560dSAlex Tomas 		spin_unlock(&ei->i_prealloc_lock);
4052c9de560dSAlex Tomas 
4053c9de560dSAlex Tomas 		/* we have to wait here because pa_deleted
4054c9de560dSAlex Tomas 		 * doesn't mean pa is already unlinked from
4055c9de560dSAlex Tomas 		 * the list. as we might be called from
4056c9de560dSAlex Tomas 		 * ->clear_inode() the inode will get freed
4057c9de560dSAlex Tomas 		 * and concurrent thread which is unlinking
4058c9de560dSAlex Tomas 		 * pa from inode's list may access already
4059c9de560dSAlex Tomas 		 * freed memory, bad-bad-bad */
4060c9de560dSAlex Tomas 
4061c9de560dSAlex Tomas 		/* XXX: if this happens too often, we can
4062c9de560dSAlex Tomas 		 * add a flag to force wait only in case
4063c9de560dSAlex Tomas 		 * of ->clear_inode(), but not in case of
4064c9de560dSAlex Tomas 		 * regular truncate */
4065c9de560dSAlex Tomas 		schedule_timeout_uninterruptible(HZ);
4066c9de560dSAlex Tomas 		goto repeat;
4067c9de560dSAlex Tomas 	}
4068c9de560dSAlex Tomas 	spin_unlock(&ei->i_prealloc_lock);
4069c9de560dSAlex Tomas 
4070c9de560dSAlex Tomas 	list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
4071c9de560dSAlex Tomas 		BUG_ON(pa->pa_linear != 0);
4072c9de560dSAlex Tomas 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
4073c9de560dSAlex Tomas 
4074c9de560dSAlex Tomas 		err = ext4_mb_load_buddy(sb, group, &e4b);
4075ce89f46cSAneesh Kumar K.V 		if (err) {
4076ce89f46cSAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in loading buddy "
4077a9df9a49STheodore Ts'o 					"information for %u", group);
4078ce89f46cSAneesh Kumar K.V 			continue;
4079ce89f46cSAneesh Kumar K.V 		}
4080c9de560dSAlex Tomas 
4081574ca174STheodore Ts'o 		bitmap_bh = ext4_read_block_bitmap(sb, group);
4082c9de560dSAlex Tomas 		if (bitmap_bh == NULL) {
4083ce89f46cSAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in reading block "
4084a9df9a49STheodore Ts'o 					"bitmap for %u", group);
4085c9de560dSAlex Tomas 			ext4_mb_release_desc(&e4b);
4086ce89f46cSAneesh Kumar K.V 			continue;
4087c9de560dSAlex Tomas 		}
4088c9de560dSAlex Tomas 
4089c9de560dSAlex Tomas 		ext4_lock_group(sb, group);
4090c9de560dSAlex Tomas 		list_del(&pa->pa_group_list);
4091c83617dbSAneesh Kumar K.V 		ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
4092c9de560dSAlex Tomas 		ext4_unlock_group(sb, group);
4093c9de560dSAlex Tomas 
4094c9de560dSAlex Tomas 		ext4_mb_release_desc(&e4b);
4095c9de560dSAlex Tomas 		put_bh(bitmap_bh);
4096c9de560dSAlex Tomas 
4097c9de560dSAlex Tomas 		list_del(&pa->u.pa_tmp_list);
4098c9de560dSAlex Tomas 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
4099c9de560dSAlex Tomas 	}
4100c83617dbSAneesh Kumar K.V 	if (ac)
4101c83617dbSAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
4102c9de560dSAlex Tomas }
4103c9de560dSAlex Tomas 
4104c9de560dSAlex Tomas /*
4105c9de560dSAlex Tomas  * finds all preallocated spaces and return blocks being freed to them
4106c9de560dSAlex Tomas  * if preallocated space becomes full (no block is used from the space)
4107c9de560dSAlex Tomas  * then the function frees space in buddy
4108c9de560dSAlex Tomas  * XXX: at the moment, truncate (which is the only way to free blocks)
4109c9de560dSAlex Tomas  * discards all preallocations
4110c9de560dSAlex Tomas  */
4111c9de560dSAlex Tomas static void ext4_mb_return_to_preallocation(struct inode *inode,
4112c9de560dSAlex Tomas 					struct ext4_buddy *e4b,
4113c9de560dSAlex Tomas 					sector_t block, int count)
4114c9de560dSAlex Tomas {
4115c9de560dSAlex Tomas 	BUG_ON(!list_empty(&EXT4_I(inode)->i_prealloc_list));
4116c9de560dSAlex Tomas }
4117c9de560dSAlex Tomas #ifdef MB_DEBUG
4118c9de560dSAlex Tomas static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4119c9de560dSAlex Tomas {
4120c9de560dSAlex Tomas 	struct super_block *sb = ac->ac_sb;
4121c9de560dSAlex Tomas 	ext4_group_t i;
4122c9de560dSAlex Tomas 
4123c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: Can't allocate:"
4124c9de560dSAlex Tomas 			" Allocation context details:\n");
4125c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: status %d flags %d\n",
4126c9de560dSAlex Tomas 			ac->ac_status, ac->ac_flags);
4127c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: orig %lu/%lu/%lu@%lu, goal %lu/%lu/%lu@%lu, "
4128c9de560dSAlex Tomas 			"best %lu/%lu/%lu@%lu cr %d\n",
4129c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_group,
4130c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_start,
4131c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_len,
4132c9de560dSAlex Tomas 			(unsigned long)ac->ac_o_ex.fe_logical,
4133c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_group,
4134c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_start,
4135c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_len,
4136c9de560dSAlex Tomas 			(unsigned long)ac->ac_g_ex.fe_logical,
4137c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_group,
4138c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_start,
4139c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_len,
4140c9de560dSAlex Tomas 			(unsigned long)ac->ac_b_ex.fe_logical,
4141c9de560dSAlex Tomas 			(int)ac->ac_criteria);
4142c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: %lu scanned, %d found\n", ac->ac_ex_scanned,
4143c9de560dSAlex Tomas 		ac->ac_found);
4144c9de560dSAlex Tomas 	printk(KERN_ERR "EXT4-fs: groups: \n");
4145c9de560dSAlex Tomas 	for (i = 0; i < EXT4_SB(sb)->s_groups_count; i++) {
4146c9de560dSAlex Tomas 		struct ext4_group_info *grp = ext4_get_group_info(sb, i);
4147c9de560dSAlex Tomas 		struct ext4_prealloc_space *pa;
4148c9de560dSAlex Tomas 		ext4_grpblk_t start;
4149c9de560dSAlex Tomas 		struct list_head *cur;
4150c9de560dSAlex Tomas 		ext4_lock_group(sb, i);
4151c9de560dSAlex Tomas 		list_for_each(cur, &grp->bb_prealloc_list) {
4152c9de560dSAlex Tomas 			pa = list_entry(cur, struct ext4_prealloc_space,
4153c9de560dSAlex Tomas 					pa_group_list);
4154c9de560dSAlex Tomas 			spin_lock(&pa->pa_lock);
4155c9de560dSAlex Tomas 			ext4_get_group_no_and_offset(sb, pa->pa_pstart,
4156c9de560dSAlex Tomas 						     NULL, &start);
4157c9de560dSAlex Tomas 			spin_unlock(&pa->pa_lock);
4158c9de560dSAlex Tomas 			printk(KERN_ERR "PA:%lu:%d:%u \n", i,
4159c9de560dSAlex Tomas 							start, pa->pa_len);
4160c9de560dSAlex Tomas 		}
416160bd63d1SSolofo Ramangalahy 		ext4_unlock_group(sb, i);
4162c9de560dSAlex Tomas 
4163c9de560dSAlex Tomas 		if (grp->bb_free == 0)
4164c9de560dSAlex Tomas 			continue;
4165c9de560dSAlex Tomas 		printk(KERN_ERR "%lu: %d/%d \n",
4166c9de560dSAlex Tomas 		       i, grp->bb_free, grp->bb_fragments);
4167c9de560dSAlex Tomas 	}
4168c9de560dSAlex Tomas 	printk(KERN_ERR "\n");
4169c9de560dSAlex Tomas }
4170c9de560dSAlex Tomas #else
4171c9de560dSAlex Tomas static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
4172c9de560dSAlex Tomas {
4173c9de560dSAlex Tomas 	return;
4174c9de560dSAlex Tomas }
4175c9de560dSAlex Tomas #endif
4176c9de560dSAlex Tomas 
4177c9de560dSAlex Tomas /*
4178c9de560dSAlex Tomas  * We use locality group preallocation for small size file. The size of the
4179c9de560dSAlex Tomas  * file is determined by the current size or the resulting size after
4180c9de560dSAlex Tomas  * allocation which ever is larger
4181c9de560dSAlex Tomas  *
4182b713a5ecSTheodore Ts'o  * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
4183c9de560dSAlex Tomas  */
4184c9de560dSAlex Tomas static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
4185c9de560dSAlex Tomas {
4186c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4187c9de560dSAlex Tomas 	int bsbits = ac->ac_sb->s_blocksize_bits;
4188c9de560dSAlex Tomas 	loff_t size, isize;
4189c9de560dSAlex Tomas 
4190c9de560dSAlex Tomas 	if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4191c9de560dSAlex Tomas 		return;
4192c9de560dSAlex Tomas 
4193c9de560dSAlex Tomas 	size = ac->ac_o_ex.fe_logical + ac->ac_o_ex.fe_len;
4194c9de560dSAlex Tomas 	isize = i_size_read(ac->ac_inode) >> bsbits;
4195c9de560dSAlex Tomas 	size = max(size, isize);
4196c9de560dSAlex Tomas 
4197c9de560dSAlex Tomas 	/* don't use group allocation for large files */
4198c9de560dSAlex Tomas 	if (size >= sbi->s_mb_stream_request)
4199c9de560dSAlex Tomas 		return;
4200c9de560dSAlex Tomas 
4201c9de560dSAlex Tomas 	if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4202c9de560dSAlex Tomas 		return;
4203c9de560dSAlex Tomas 
4204c9de560dSAlex Tomas 	BUG_ON(ac->ac_lg != NULL);
4205c9de560dSAlex Tomas 	/*
4206c9de560dSAlex Tomas 	 * locality group prealloc space are per cpu. The reason for having
4207c9de560dSAlex Tomas 	 * per cpu locality group is to reduce the contention between block
4208c9de560dSAlex Tomas 	 * request from multiple CPUs.
4209c9de560dSAlex Tomas 	 */
4210730c213cSEric Sandeen 	ac->ac_lg = per_cpu_ptr(sbi->s_locality_groups, raw_smp_processor_id());
4211c9de560dSAlex Tomas 
4212c9de560dSAlex Tomas 	/* we're going to use group allocation */
4213c9de560dSAlex Tomas 	ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
4214c9de560dSAlex Tomas 
4215c9de560dSAlex Tomas 	/* serialize all allocations in the group */
4216c9de560dSAlex Tomas 	mutex_lock(&ac->ac_lg->lg_mutex);
4217c9de560dSAlex Tomas }
4218c9de560dSAlex Tomas 
42194ddfef7bSEric Sandeen static noinline_for_stack int
42204ddfef7bSEric Sandeen ext4_mb_initialize_context(struct ext4_allocation_context *ac,
4221c9de560dSAlex Tomas 				struct ext4_allocation_request *ar)
4222c9de560dSAlex Tomas {
4223c9de560dSAlex Tomas 	struct super_block *sb = ar->inode->i_sb;
4224c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4225c9de560dSAlex Tomas 	struct ext4_super_block *es = sbi->s_es;
4226c9de560dSAlex Tomas 	ext4_group_t group;
4227498e5f24STheodore Ts'o 	unsigned int len;
4228498e5f24STheodore Ts'o 	ext4_fsblk_t goal;
4229c9de560dSAlex Tomas 	ext4_grpblk_t block;
4230c9de560dSAlex Tomas 
4231c9de560dSAlex Tomas 	/* we can't allocate > group size */
4232c9de560dSAlex Tomas 	len = ar->len;
4233c9de560dSAlex Tomas 
4234c9de560dSAlex Tomas 	/* just a dirty hack to filter too big requests  */
4235c9de560dSAlex Tomas 	if (len >= EXT4_BLOCKS_PER_GROUP(sb) - 10)
4236c9de560dSAlex Tomas 		len = EXT4_BLOCKS_PER_GROUP(sb) - 10;
4237c9de560dSAlex Tomas 
4238c9de560dSAlex Tomas 	/* start searching from the goal */
4239c9de560dSAlex Tomas 	goal = ar->goal;
4240c9de560dSAlex Tomas 	if (goal < le32_to_cpu(es->s_first_data_block) ||
4241c9de560dSAlex Tomas 			goal >= ext4_blocks_count(es))
4242c9de560dSAlex Tomas 		goal = le32_to_cpu(es->s_first_data_block);
4243c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, goal, &group, &block);
4244c9de560dSAlex Tomas 
4245c9de560dSAlex Tomas 	/* set up allocation goals */
4246c9de560dSAlex Tomas 	ac->ac_b_ex.fe_logical = ar->logical;
4247c9de560dSAlex Tomas 	ac->ac_b_ex.fe_group = 0;
4248c9de560dSAlex Tomas 	ac->ac_b_ex.fe_start = 0;
4249c9de560dSAlex Tomas 	ac->ac_b_ex.fe_len = 0;
4250c9de560dSAlex Tomas 	ac->ac_status = AC_STATUS_CONTINUE;
4251c9de560dSAlex Tomas 	ac->ac_groups_scanned = 0;
4252c9de560dSAlex Tomas 	ac->ac_ex_scanned = 0;
4253c9de560dSAlex Tomas 	ac->ac_found = 0;
4254c9de560dSAlex Tomas 	ac->ac_sb = sb;
4255c9de560dSAlex Tomas 	ac->ac_inode = ar->inode;
4256c9de560dSAlex Tomas 	ac->ac_o_ex.fe_logical = ar->logical;
4257c9de560dSAlex Tomas 	ac->ac_o_ex.fe_group = group;
4258c9de560dSAlex Tomas 	ac->ac_o_ex.fe_start = block;
4259c9de560dSAlex Tomas 	ac->ac_o_ex.fe_len = len;
4260c9de560dSAlex Tomas 	ac->ac_g_ex.fe_logical = ar->logical;
4261c9de560dSAlex Tomas 	ac->ac_g_ex.fe_group = group;
4262c9de560dSAlex Tomas 	ac->ac_g_ex.fe_start = block;
4263c9de560dSAlex Tomas 	ac->ac_g_ex.fe_len = len;
4264c9de560dSAlex Tomas 	ac->ac_f_ex.fe_len = 0;
4265c9de560dSAlex Tomas 	ac->ac_flags = ar->flags;
4266c9de560dSAlex Tomas 	ac->ac_2order = 0;
4267c9de560dSAlex Tomas 	ac->ac_criteria = 0;
4268c9de560dSAlex Tomas 	ac->ac_pa = NULL;
4269c9de560dSAlex Tomas 	ac->ac_bitmap_page = NULL;
4270c9de560dSAlex Tomas 	ac->ac_buddy_page = NULL;
42718556e8f3SAneesh Kumar K.V 	ac->alloc_semp = NULL;
4272c9de560dSAlex Tomas 	ac->ac_lg = NULL;
4273c9de560dSAlex Tomas 
4274c9de560dSAlex Tomas 	/* we have to define context: we'll we work with a file or
4275c9de560dSAlex Tomas 	 * locality group. this is a policy, actually */
4276c9de560dSAlex Tomas 	ext4_mb_group_or_file(ac);
4277c9de560dSAlex Tomas 
4278c9de560dSAlex Tomas 	mb_debug("init ac: %u blocks @ %u, goal %u, flags %x, 2^%d, "
4279c9de560dSAlex Tomas 			"left: %u/%u, right %u/%u to %swritable\n",
4280c9de560dSAlex Tomas 			(unsigned) ar->len, (unsigned) ar->logical,
4281c9de560dSAlex Tomas 			(unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
4282c9de560dSAlex Tomas 			(unsigned) ar->lleft, (unsigned) ar->pleft,
4283c9de560dSAlex Tomas 			(unsigned) ar->lright, (unsigned) ar->pright,
4284c9de560dSAlex Tomas 			atomic_read(&ar->inode->i_writecount) ? "" : "non-");
4285c9de560dSAlex Tomas 	return 0;
4286c9de560dSAlex Tomas 
4287c9de560dSAlex Tomas }
4288c9de560dSAlex Tomas 
42896be2ded1SAneesh Kumar K.V static noinline_for_stack void
42906be2ded1SAneesh Kumar K.V ext4_mb_discard_lg_preallocations(struct super_block *sb,
42916be2ded1SAneesh Kumar K.V 					struct ext4_locality_group *lg,
42926be2ded1SAneesh Kumar K.V 					int order, int total_entries)
42936be2ded1SAneesh Kumar K.V {
42946be2ded1SAneesh Kumar K.V 	ext4_group_t group = 0;
42956be2ded1SAneesh Kumar K.V 	struct ext4_buddy e4b;
42966be2ded1SAneesh Kumar K.V 	struct list_head discard_list;
42976be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *pa, *tmp;
42986be2ded1SAneesh Kumar K.V 	struct ext4_allocation_context *ac;
42996be2ded1SAneesh Kumar K.V 
43006be2ded1SAneesh Kumar K.V 	mb_debug("discard locality group preallocation\n");
43016be2ded1SAneesh Kumar K.V 
43026be2ded1SAneesh Kumar K.V 	INIT_LIST_HEAD(&discard_list);
43036be2ded1SAneesh Kumar K.V 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
43046be2ded1SAneesh Kumar K.V 
43056be2ded1SAneesh Kumar K.V 	spin_lock(&lg->lg_prealloc_lock);
43066be2ded1SAneesh Kumar K.V 	list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
43076be2ded1SAneesh Kumar K.V 						pa_inode_list) {
43086be2ded1SAneesh Kumar K.V 		spin_lock(&pa->pa_lock);
43096be2ded1SAneesh Kumar K.V 		if (atomic_read(&pa->pa_count)) {
43106be2ded1SAneesh Kumar K.V 			/*
43116be2ded1SAneesh Kumar K.V 			 * This is the pa that we just used
43126be2ded1SAneesh Kumar K.V 			 * for block allocation. So don't
43136be2ded1SAneesh Kumar K.V 			 * free that
43146be2ded1SAneesh Kumar K.V 			 */
43156be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
43166be2ded1SAneesh Kumar K.V 			continue;
43176be2ded1SAneesh Kumar K.V 		}
43186be2ded1SAneesh Kumar K.V 		if (pa->pa_deleted) {
43196be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
43206be2ded1SAneesh Kumar K.V 			continue;
43216be2ded1SAneesh Kumar K.V 		}
43226be2ded1SAneesh Kumar K.V 		/* only lg prealloc space */
43236be2ded1SAneesh Kumar K.V 		BUG_ON(!pa->pa_linear);
43246be2ded1SAneesh Kumar K.V 
43256be2ded1SAneesh Kumar K.V 		/* seems this one can be freed ... */
43266be2ded1SAneesh Kumar K.V 		pa->pa_deleted = 1;
43276be2ded1SAneesh Kumar K.V 		spin_unlock(&pa->pa_lock);
43286be2ded1SAneesh Kumar K.V 
43296be2ded1SAneesh Kumar K.V 		list_del_rcu(&pa->pa_inode_list);
43306be2ded1SAneesh Kumar K.V 		list_add(&pa->u.pa_tmp_list, &discard_list);
43316be2ded1SAneesh Kumar K.V 
43326be2ded1SAneesh Kumar K.V 		total_entries--;
43336be2ded1SAneesh Kumar K.V 		if (total_entries <= 5) {
43346be2ded1SAneesh Kumar K.V 			/*
43356be2ded1SAneesh Kumar K.V 			 * we want to keep only 5 entries
43366be2ded1SAneesh Kumar K.V 			 * allowing it to grow to 8. This
43376be2ded1SAneesh Kumar K.V 			 * mak sure we don't call discard
43386be2ded1SAneesh Kumar K.V 			 * soon for this list.
43396be2ded1SAneesh Kumar K.V 			 */
43406be2ded1SAneesh Kumar K.V 			break;
43416be2ded1SAneesh Kumar K.V 		}
43426be2ded1SAneesh Kumar K.V 	}
43436be2ded1SAneesh Kumar K.V 	spin_unlock(&lg->lg_prealloc_lock);
43446be2ded1SAneesh Kumar K.V 
43456be2ded1SAneesh Kumar K.V 	list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
43466be2ded1SAneesh Kumar K.V 
43476be2ded1SAneesh Kumar K.V 		ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, NULL);
43486be2ded1SAneesh Kumar K.V 		if (ext4_mb_load_buddy(sb, group, &e4b)) {
43496be2ded1SAneesh Kumar K.V 			ext4_error(sb, __func__, "Error in loading buddy "
4350a9df9a49STheodore Ts'o 					"information for %u", group);
43516be2ded1SAneesh Kumar K.V 			continue;
43526be2ded1SAneesh Kumar K.V 		}
43536be2ded1SAneesh Kumar K.V 		ext4_lock_group(sb, group);
43546be2ded1SAneesh Kumar K.V 		list_del(&pa->pa_group_list);
43556be2ded1SAneesh Kumar K.V 		ext4_mb_release_group_pa(&e4b, pa, ac);
43566be2ded1SAneesh Kumar K.V 		ext4_unlock_group(sb, group);
43576be2ded1SAneesh Kumar K.V 
43586be2ded1SAneesh Kumar K.V 		ext4_mb_release_desc(&e4b);
43596be2ded1SAneesh Kumar K.V 		list_del(&pa->u.pa_tmp_list);
43606be2ded1SAneesh Kumar K.V 		call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
43616be2ded1SAneesh Kumar K.V 	}
43626be2ded1SAneesh Kumar K.V 	if (ac)
43636be2ded1SAneesh Kumar K.V 		kmem_cache_free(ext4_ac_cachep, ac);
43646be2ded1SAneesh Kumar K.V }
43656be2ded1SAneesh Kumar K.V 
43666be2ded1SAneesh Kumar K.V /*
43676be2ded1SAneesh Kumar K.V  * We have incremented pa_count. So it cannot be freed at this
43686be2ded1SAneesh Kumar K.V  * point. Also we hold lg_mutex. So no parallel allocation is
43696be2ded1SAneesh Kumar K.V  * possible from this lg. That means pa_free cannot be updated.
43706be2ded1SAneesh Kumar K.V  *
43716be2ded1SAneesh Kumar K.V  * A parallel ext4_mb_discard_group_preallocations is possible.
43726be2ded1SAneesh Kumar K.V  * which can cause the lg_prealloc_list to be updated.
43736be2ded1SAneesh Kumar K.V  */
43746be2ded1SAneesh Kumar K.V 
43756be2ded1SAneesh Kumar K.V static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
43766be2ded1SAneesh Kumar K.V {
43776be2ded1SAneesh Kumar K.V 	int order, added = 0, lg_prealloc_count = 1;
43786be2ded1SAneesh Kumar K.V 	struct super_block *sb = ac->ac_sb;
43796be2ded1SAneesh Kumar K.V 	struct ext4_locality_group *lg = ac->ac_lg;
43806be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
43816be2ded1SAneesh Kumar K.V 
43826be2ded1SAneesh Kumar K.V 	order = fls(pa->pa_free) - 1;
43836be2ded1SAneesh Kumar K.V 	if (order > PREALLOC_TB_SIZE - 1)
43846be2ded1SAneesh Kumar K.V 		/* The max size of hash table is PREALLOC_TB_SIZE */
43856be2ded1SAneesh Kumar K.V 		order = PREALLOC_TB_SIZE - 1;
43866be2ded1SAneesh Kumar K.V 	/* Add the prealloc space to lg */
43876be2ded1SAneesh Kumar K.V 	rcu_read_lock();
43886be2ded1SAneesh Kumar K.V 	list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
43896be2ded1SAneesh Kumar K.V 						pa_inode_list) {
43906be2ded1SAneesh Kumar K.V 		spin_lock(&tmp_pa->pa_lock);
43916be2ded1SAneesh Kumar K.V 		if (tmp_pa->pa_deleted) {
43926be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
43936be2ded1SAneesh Kumar K.V 			continue;
43946be2ded1SAneesh Kumar K.V 		}
43956be2ded1SAneesh Kumar K.V 		if (!added && pa->pa_free < tmp_pa->pa_free) {
43966be2ded1SAneesh Kumar K.V 			/* Add to the tail of the previous entry */
43976be2ded1SAneesh Kumar K.V 			list_add_tail_rcu(&pa->pa_inode_list,
43986be2ded1SAneesh Kumar K.V 						&tmp_pa->pa_inode_list);
43996be2ded1SAneesh Kumar K.V 			added = 1;
44006be2ded1SAneesh Kumar K.V 			/*
44016be2ded1SAneesh Kumar K.V 			 * we want to count the total
44026be2ded1SAneesh Kumar K.V 			 * number of entries in the list
44036be2ded1SAneesh Kumar K.V 			 */
44046be2ded1SAneesh Kumar K.V 		}
44056be2ded1SAneesh Kumar K.V 		spin_unlock(&tmp_pa->pa_lock);
44066be2ded1SAneesh Kumar K.V 		lg_prealloc_count++;
44076be2ded1SAneesh Kumar K.V 	}
44086be2ded1SAneesh Kumar K.V 	if (!added)
44096be2ded1SAneesh Kumar K.V 		list_add_tail_rcu(&pa->pa_inode_list,
44106be2ded1SAneesh Kumar K.V 					&lg->lg_prealloc_list[order]);
44116be2ded1SAneesh Kumar K.V 	rcu_read_unlock();
44126be2ded1SAneesh Kumar K.V 
44136be2ded1SAneesh Kumar K.V 	/* Now trim the list to be not more than 8 elements */
44146be2ded1SAneesh Kumar K.V 	if (lg_prealloc_count > 8) {
44156be2ded1SAneesh Kumar K.V 		ext4_mb_discard_lg_preallocations(sb, lg,
44166be2ded1SAneesh Kumar K.V 						order, lg_prealloc_count);
44176be2ded1SAneesh Kumar K.V 		return;
44186be2ded1SAneesh Kumar K.V 	}
44196be2ded1SAneesh Kumar K.V 	return ;
44206be2ded1SAneesh Kumar K.V }
44216be2ded1SAneesh Kumar K.V 
4422c9de560dSAlex Tomas /*
4423c9de560dSAlex Tomas  * release all resource we used in allocation
4424c9de560dSAlex Tomas  */
4425c9de560dSAlex Tomas static int ext4_mb_release_context(struct ext4_allocation_context *ac)
4426c9de560dSAlex Tomas {
44276be2ded1SAneesh Kumar K.V 	struct ext4_prealloc_space *pa = ac->ac_pa;
44286be2ded1SAneesh Kumar K.V 	if (pa) {
44296be2ded1SAneesh Kumar K.V 		if (pa->pa_linear) {
4430c9de560dSAlex Tomas 			/* see comment in ext4_mb_use_group_pa() */
44316be2ded1SAneesh Kumar K.V 			spin_lock(&pa->pa_lock);
44326be2ded1SAneesh Kumar K.V 			pa->pa_pstart += ac->ac_b_ex.fe_len;
44336be2ded1SAneesh Kumar K.V 			pa->pa_lstart += ac->ac_b_ex.fe_len;
44346be2ded1SAneesh Kumar K.V 			pa->pa_free -= ac->ac_b_ex.fe_len;
44356be2ded1SAneesh Kumar K.V 			pa->pa_len -= ac->ac_b_ex.fe_len;
44366be2ded1SAneesh Kumar K.V 			spin_unlock(&pa->pa_lock);
4437ba443916SAneesh Kumar K.V 		}
4438ba443916SAneesh Kumar K.V 	}
4439ba443916SAneesh Kumar K.V 	if (ac->alloc_semp)
4440ba443916SAneesh Kumar K.V 		up_read(ac->alloc_semp);
4441ba443916SAneesh Kumar K.V 	if (pa) {
44426be2ded1SAneesh Kumar K.V 		/*
44436be2ded1SAneesh Kumar K.V 		 * We want to add the pa to the right bucket.
44446be2ded1SAneesh Kumar K.V 		 * Remove it from the list and while adding
44456be2ded1SAneesh Kumar K.V 		 * make sure the list to which we are adding
4446ba443916SAneesh Kumar K.V 		 * doesn't grow big.  We need to release
4447ba443916SAneesh Kumar K.V 		 * alloc_semp before calling ext4_mb_add_n_trim()
44486be2ded1SAneesh Kumar K.V 		 */
4449ba443916SAneesh Kumar K.V 		if (pa->pa_linear && likely(pa->pa_free)) {
44506be2ded1SAneesh Kumar K.V 			spin_lock(pa->pa_obj_lock);
44516be2ded1SAneesh Kumar K.V 			list_del_rcu(&pa->pa_inode_list);
44526be2ded1SAneesh Kumar K.V 			spin_unlock(pa->pa_obj_lock);
44536be2ded1SAneesh Kumar K.V 			ext4_mb_add_n_trim(ac);
4454c9de560dSAlex Tomas 		}
44556be2ded1SAneesh Kumar K.V 		ext4_mb_put_pa(ac, ac->ac_sb, pa);
4456c9de560dSAlex Tomas 	}
4457c9de560dSAlex Tomas 	if (ac->ac_bitmap_page)
4458c9de560dSAlex Tomas 		page_cache_release(ac->ac_bitmap_page);
4459c9de560dSAlex Tomas 	if (ac->ac_buddy_page)
4460c9de560dSAlex Tomas 		page_cache_release(ac->ac_buddy_page);
4461c9de560dSAlex Tomas 	if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
4462c9de560dSAlex Tomas 		mutex_unlock(&ac->ac_lg->lg_mutex);
4463c9de560dSAlex Tomas 	ext4_mb_collect_stats(ac);
4464c9de560dSAlex Tomas 	return 0;
4465c9de560dSAlex Tomas }
4466c9de560dSAlex Tomas 
4467c9de560dSAlex Tomas static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
4468c9de560dSAlex Tomas {
4469c9de560dSAlex Tomas 	ext4_group_t i;
4470c9de560dSAlex Tomas 	int ret;
4471c9de560dSAlex Tomas 	int freed = 0;
4472c9de560dSAlex Tomas 
4473ba80b101STheodore Ts'o 	trace_mark(ext4_mb_discard_preallocations, "dev %s needed %d",
4474ba80b101STheodore Ts'o 		   sb->s_id, needed);
4475c9de560dSAlex Tomas 	for (i = 0; i < EXT4_SB(sb)->s_groups_count && needed > 0; i++) {
4476c9de560dSAlex Tomas 		ret = ext4_mb_discard_group_preallocations(sb, i, needed);
4477c9de560dSAlex Tomas 		freed += ret;
4478c9de560dSAlex Tomas 		needed -= ret;
4479c9de560dSAlex Tomas 	}
4480c9de560dSAlex Tomas 
4481c9de560dSAlex Tomas 	return freed;
4482c9de560dSAlex Tomas }
4483c9de560dSAlex Tomas 
4484c9de560dSAlex Tomas /*
4485c9de560dSAlex Tomas  * Main entry point into mballoc to allocate blocks
4486c9de560dSAlex Tomas  * it tries to use preallocation first, then falls back
4487c9de560dSAlex Tomas  * to usual allocation
4488c9de560dSAlex Tomas  */
4489c9de560dSAlex Tomas ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
4490c9de560dSAlex Tomas 				 struct ext4_allocation_request *ar, int *errp)
4491c9de560dSAlex Tomas {
44926bc6e63fSAneesh Kumar K.V 	int freed;
4493256bdb49SEric Sandeen 	struct ext4_allocation_context *ac = NULL;
4494c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
4495c9de560dSAlex Tomas 	struct super_block *sb;
4496c9de560dSAlex Tomas 	ext4_fsblk_t block = 0;
449760e58e0fSMingming Cao 	unsigned int inquota = 0;
4498498e5f24STheodore Ts'o 	unsigned int reserv_blks = 0;
4499c9de560dSAlex Tomas 
4500c9de560dSAlex Tomas 	sb = ar->inode->i_sb;
4501c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
4502c9de560dSAlex Tomas 
4503ba80b101STheodore Ts'o 	trace_mark(ext4_request_blocks, "dev %s flags %u len %u ino %lu "
4504ba80b101STheodore Ts'o 		   "lblk %llu goal %llu lleft %llu lright %llu "
4505ba80b101STheodore Ts'o 		   "pleft %llu pright %llu ",
4506ba80b101STheodore Ts'o 		   sb->s_id, ar->flags, ar->len,
4507ba80b101STheodore Ts'o 		   ar->inode ? ar->inode->i_ino : 0,
4508ba80b101STheodore Ts'o 		   (unsigned long long) ar->logical,
4509ba80b101STheodore Ts'o 		   (unsigned long long) ar->goal,
4510ba80b101STheodore Ts'o 		   (unsigned long long) ar->lleft,
4511ba80b101STheodore Ts'o 		   (unsigned long long) ar->lright,
4512ba80b101STheodore Ts'o 		   (unsigned long long) ar->pleft,
4513ba80b101STheodore Ts'o 		   (unsigned long long) ar->pright);
4514ba80b101STheodore Ts'o 
4515d2a17637SMingming Cao 	/*
451660e58e0fSMingming Cao 	 * For delayed allocation, we could skip the ENOSPC and
451760e58e0fSMingming Cao 	 * EDQUOT check, as blocks and quotas have been already
451860e58e0fSMingming Cao 	 * reserved when data being copied into pagecache.
451960e58e0fSMingming Cao 	 */
452060e58e0fSMingming Cao 	if (EXT4_I(ar->inode)->i_delalloc_reserved_flag)
452160e58e0fSMingming Cao 		ar->flags |= EXT4_MB_DELALLOC_RESERVED;
452260e58e0fSMingming Cao 	else {
452360e58e0fSMingming Cao 		/* Without delayed allocation we need to verify
452460e58e0fSMingming Cao 		 * there is enough free blocks to do block allocation
452560e58e0fSMingming Cao 		 * and verify allocation doesn't exceed the quota limits.
4526d2a17637SMingming Cao 		 */
4527030ba6bcSAneesh Kumar K.V 		while (ar->len && ext4_claim_free_blocks(sbi, ar->len)) {
4528030ba6bcSAneesh Kumar K.V 			/* let others to free the space */
4529030ba6bcSAneesh Kumar K.V 			yield();
4530030ba6bcSAneesh Kumar K.V 			ar->len = ar->len >> 1;
4531030ba6bcSAneesh Kumar K.V 		}
4532030ba6bcSAneesh Kumar K.V 		if (!ar->len) {
453307031431SMingming Cao 			*errp = -ENOSPC;
453407031431SMingming Cao 			return 0;
453507031431SMingming Cao 		}
45366bc6e63fSAneesh Kumar K.V 		reserv_blks = ar->len;
4537a269eb18SJan Kara 		while (ar->len && vfs_dq_alloc_block(ar->inode, ar->len)) {
4538c9de560dSAlex Tomas 			ar->flags |= EXT4_MB_HINT_NOPREALLOC;
4539c9de560dSAlex Tomas 			ar->len--;
4540c9de560dSAlex Tomas 		}
454160e58e0fSMingming Cao 		inquota = ar->len;
4542c9de560dSAlex Tomas 		if (ar->len == 0) {
4543c9de560dSAlex Tomas 			*errp = -EDQUOT;
45440087d9fbSAneesh Kumar K.V 			goto out3;
4545c9de560dSAlex Tomas 		}
454660e58e0fSMingming Cao 	}
4547d2a17637SMingming Cao 
4548256bdb49SEric Sandeen 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4549256bdb49SEric Sandeen 	if (!ac) {
4550363d4251SShen Feng 		ar->len = 0;
4551256bdb49SEric Sandeen 		*errp = -ENOMEM;
4552363d4251SShen Feng 		goto out1;
4553256bdb49SEric Sandeen 	}
4554256bdb49SEric Sandeen 
4555256bdb49SEric Sandeen 	*errp = ext4_mb_initialize_context(ac, ar);
4556c9de560dSAlex Tomas 	if (*errp) {
4557c9de560dSAlex Tomas 		ar->len = 0;
4558363d4251SShen Feng 		goto out2;
4559c9de560dSAlex Tomas 	}
4560c9de560dSAlex Tomas 
4561256bdb49SEric Sandeen 	ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
4562256bdb49SEric Sandeen 	if (!ext4_mb_use_preallocated(ac)) {
4563256bdb49SEric Sandeen 		ac->ac_op = EXT4_MB_HISTORY_ALLOC;
4564256bdb49SEric Sandeen 		ext4_mb_normalize_request(ac, ar);
4565c9de560dSAlex Tomas repeat:
4566c9de560dSAlex Tomas 		/* allocate space in core */
4567256bdb49SEric Sandeen 		ext4_mb_regular_allocator(ac);
4568c9de560dSAlex Tomas 
4569c9de560dSAlex Tomas 		/* as we've just preallocated more space than
4570c9de560dSAlex Tomas 		 * user requested orinally, we store allocated
4571c9de560dSAlex Tomas 		 * space in a special descriptor */
4572256bdb49SEric Sandeen 		if (ac->ac_status == AC_STATUS_FOUND &&
4573256bdb49SEric Sandeen 				ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
4574256bdb49SEric Sandeen 			ext4_mb_new_preallocation(ac);
4575c9de560dSAlex Tomas 	}
4576256bdb49SEric Sandeen 	if (likely(ac->ac_status == AC_STATUS_FOUND)) {
45776bc6e63fSAneesh Kumar K.V 		*errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_blks);
4578519deca0SAneesh Kumar K.V 		if (*errp ==  -EAGAIN) {
45798556e8f3SAneesh Kumar K.V 			/*
45808556e8f3SAneesh Kumar K.V 			 * drop the reference that we took
45818556e8f3SAneesh Kumar K.V 			 * in ext4_mb_use_best_found
45828556e8f3SAneesh Kumar K.V 			 */
45838556e8f3SAneesh Kumar K.V 			ext4_mb_release_context(ac);
4584519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_group = 0;
4585519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_start = 0;
4586519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_len = 0;
4587519deca0SAneesh Kumar K.V 			ac->ac_status = AC_STATUS_CONTINUE;
4588519deca0SAneesh Kumar K.V 			goto repeat;
4589519deca0SAneesh Kumar K.V 		} else if (*errp) {
4590519deca0SAneesh Kumar K.V 			ac->ac_b_ex.fe_len = 0;
4591519deca0SAneesh Kumar K.V 			ar->len = 0;
4592519deca0SAneesh Kumar K.V 			ext4_mb_show_ac(ac);
4593519deca0SAneesh Kumar K.V 		} else {
4594256bdb49SEric Sandeen 			block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4595256bdb49SEric Sandeen 			ar->len = ac->ac_b_ex.fe_len;
4596519deca0SAneesh Kumar K.V 		}
4597c9de560dSAlex Tomas 	} else {
4598256bdb49SEric Sandeen 		freed  = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
4599c9de560dSAlex Tomas 		if (freed)
4600c9de560dSAlex Tomas 			goto repeat;
4601c9de560dSAlex Tomas 		*errp = -ENOSPC;
4602256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = 0;
4603c9de560dSAlex Tomas 		ar->len = 0;
4604256bdb49SEric Sandeen 		ext4_mb_show_ac(ac);
4605c9de560dSAlex Tomas 	}
4606c9de560dSAlex Tomas 
4607256bdb49SEric Sandeen 	ext4_mb_release_context(ac);
4608c9de560dSAlex Tomas 
4609363d4251SShen Feng out2:
4610363d4251SShen Feng 	kmem_cache_free(ext4_ac_cachep, ac);
4611363d4251SShen Feng out1:
461260e58e0fSMingming Cao 	if (inquota && ar->len < inquota)
4613a269eb18SJan Kara 		vfs_dq_free_block(ar->inode, inquota - ar->len);
46140087d9fbSAneesh Kumar K.V out3:
46150087d9fbSAneesh Kumar K.V 	if (!ar->len) {
46160087d9fbSAneesh Kumar K.V 		if (!EXT4_I(ar->inode)->i_delalloc_reserved_flag)
46170087d9fbSAneesh Kumar K.V 			/* release all the reserved blocks if non delalloc */
46180087d9fbSAneesh Kumar K.V 			percpu_counter_sub(&sbi->s_dirtyblocks_counter,
46190087d9fbSAneesh Kumar K.V 						reserv_blks);
46200087d9fbSAneesh Kumar K.V 	}
4621c9de560dSAlex Tomas 
4622ba80b101STheodore Ts'o 	trace_mark(ext4_allocate_blocks,
4623ba80b101STheodore Ts'o 		   "dev %s block %llu flags %u len %u ino %lu "
4624ba80b101STheodore Ts'o 		   "logical %llu goal %llu lleft %llu lright %llu "
4625ba80b101STheodore Ts'o 		   "pleft %llu pright %llu ",
4626ba80b101STheodore Ts'o 		   sb->s_id, (unsigned long long) block,
4627ba80b101STheodore Ts'o 		   ar->flags, ar->len, ar->inode ? ar->inode->i_ino : 0,
4628ba80b101STheodore Ts'o 		   (unsigned long long) ar->logical,
4629ba80b101STheodore Ts'o 		   (unsigned long long) ar->goal,
4630ba80b101STheodore Ts'o 		   (unsigned long long) ar->lleft,
4631ba80b101STheodore Ts'o 		   (unsigned long long) ar->lright,
4632ba80b101STheodore Ts'o 		   (unsigned long long) ar->pleft,
4633ba80b101STheodore Ts'o 		   (unsigned long long) ar->pright);
4634ba80b101STheodore Ts'o 
4635c9de560dSAlex Tomas 	return block;
4636c9de560dSAlex Tomas }
4637c9de560dSAlex Tomas 
4638c894058dSAneesh Kumar K.V /*
4639c894058dSAneesh Kumar K.V  * We can merge two free data extents only if the physical blocks
4640c894058dSAneesh Kumar K.V  * are contiguous, AND the extents were freed by the same transaction,
4641c894058dSAneesh Kumar K.V  * AND the blocks are associated with the same group.
4642c894058dSAneesh Kumar K.V  */
4643c894058dSAneesh Kumar K.V static int can_merge(struct ext4_free_data *entry1,
4644c894058dSAneesh Kumar K.V 			struct ext4_free_data *entry2)
4645c894058dSAneesh Kumar K.V {
4646c894058dSAneesh Kumar K.V 	if ((entry1->t_tid == entry2->t_tid) &&
4647c894058dSAneesh Kumar K.V 	    (entry1->group == entry2->group) &&
4648c894058dSAneesh Kumar K.V 	    ((entry1->start_blk + entry1->count) == entry2->start_blk))
4649c894058dSAneesh Kumar K.V 		return 1;
4650c894058dSAneesh Kumar K.V 	return 0;
4651c894058dSAneesh Kumar K.V }
4652c894058dSAneesh Kumar K.V 
46534ddfef7bSEric Sandeen static noinline_for_stack int
46544ddfef7bSEric Sandeen ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
46557a2fcbf7SAneesh Kumar K.V 		      struct ext4_free_data *new_entry)
4656c9de560dSAlex Tomas {
46577a2fcbf7SAneesh Kumar K.V 	ext4_grpblk_t block;
46587a2fcbf7SAneesh Kumar K.V 	struct ext4_free_data *entry;
4659c9de560dSAlex Tomas 	struct ext4_group_info *db = e4b->bd_info;
4660c9de560dSAlex Tomas 	struct super_block *sb = e4b->bd_sb;
4661c9de560dSAlex Tomas 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4662c894058dSAneesh Kumar K.V 	struct rb_node **n = &db->bb_free_root.rb_node, *node;
4663c894058dSAneesh Kumar K.V 	struct rb_node *parent = NULL, *new_node;
4664c894058dSAneesh Kumar K.V 
46650390131bSFrank Mayhar 	BUG_ON(!ext4_handle_valid(handle));
4666c9de560dSAlex Tomas 	BUG_ON(e4b->bd_bitmap_page == NULL);
4667c9de560dSAlex Tomas 	BUG_ON(e4b->bd_buddy_page == NULL);
4668c9de560dSAlex Tomas 
4669c894058dSAneesh Kumar K.V 	new_node = &new_entry->node;
46707a2fcbf7SAneesh Kumar K.V 	block = new_entry->start_blk;
4671c9de560dSAlex Tomas 
4672c894058dSAneesh Kumar K.V 	if (!*n) {
4673c894058dSAneesh Kumar K.V 		/* first free block exent. We need to
4674c894058dSAneesh Kumar K.V 		   protect buddy cache from being freed,
4675c9de560dSAlex Tomas 		 * otherwise we'll refresh it from
4676c9de560dSAlex Tomas 		 * on-disk bitmap and lose not-yet-available
4677c9de560dSAlex Tomas 		 * blocks */
4678c9de560dSAlex Tomas 		page_cache_get(e4b->bd_buddy_page);
4679c9de560dSAlex Tomas 		page_cache_get(e4b->bd_bitmap_page);
4680c894058dSAneesh Kumar K.V 	}
4681c894058dSAneesh Kumar K.V 	while (*n) {
4682c894058dSAneesh Kumar K.V 		parent = *n;
4683c894058dSAneesh Kumar K.V 		entry = rb_entry(parent, struct ext4_free_data, node);
4684c894058dSAneesh Kumar K.V 		if (block < entry->start_blk)
4685c894058dSAneesh Kumar K.V 			n = &(*n)->rb_left;
4686c894058dSAneesh Kumar K.V 		else if (block >= (entry->start_blk + entry->count))
4687c894058dSAneesh Kumar K.V 			n = &(*n)->rb_right;
4688c894058dSAneesh Kumar K.V 		else {
46895d1b1b3fSAneesh Kumar K.V 			ext4_grp_locked_error(sb, e4b->bd_group, __func__,
4690fde4d95aSTheodore Ts'o 					"Double free of blocks %d (%d %d)",
4691c894058dSAneesh Kumar K.V 					block, entry->start_blk, entry->count);
4692c894058dSAneesh Kumar K.V 			return 0;
4693c9de560dSAlex Tomas 		}
4694c9de560dSAlex Tomas 	}
4695c9de560dSAlex Tomas 
4696c894058dSAneesh Kumar K.V 	rb_link_node(new_node, parent, n);
4697c894058dSAneesh Kumar K.V 	rb_insert_color(new_node, &db->bb_free_root);
4698c894058dSAneesh Kumar K.V 
4699c894058dSAneesh Kumar K.V 	/* Now try to see the extent can be merged to left and right */
4700c894058dSAneesh Kumar K.V 	node = rb_prev(new_node);
4701c894058dSAneesh Kumar K.V 	if (node) {
4702c894058dSAneesh Kumar K.V 		entry = rb_entry(node, struct ext4_free_data, node);
4703c894058dSAneesh Kumar K.V 		if (can_merge(entry, new_entry)) {
4704c894058dSAneesh Kumar K.V 			new_entry->start_blk = entry->start_blk;
4705c894058dSAneesh Kumar K.V 			new_entry->count += entry->count;
4706c894058dSAneesh Kumar K.V 			rb_erase(node, &(db->bb_free_root));
4707c894058dSAneesh Kumar K.V 			spin_lock(&sbi->s_md_lock);
4708c894058dSAneesh Kumar K.V 			list_del(&entry->list);
4709c894058dSAneesh Kumar K.V 			spin_unlock(&sbi->s_md_lock);
4710c894058dSAneesh Kumar K.V 			kmem_cache_free(ext4_free_ext_cachep, entry);
4711c9de560dSAlex Tomas 		}
4712c9de560dSAlex Tomas 	}
4713c894058dSAneesh Kumar K.V 
4714c894058dSAneesh Kumar K.V 	node = rb_next(new_node);
4715c894058dSAneesh Kumar K.V 	if (node) {
4716c894058dSAneesh Kumar K.V 		entry = rb_entry(node, struct ext4_free_data, node);
4717c894058dSAneesh Kumar K.V 		if (can_merge(new_entry, entry)) {
4718c894058dSAneesh Kumar K.V 			new_entry->count += entry->count;
4719c894058dSAneesh Kumar K.V 			rb_erase(node, &(db->bb_free_root));
4720c894058dSAneesh Kumar K.V 			spin_lock(&sbi->s_md_lock);
4721c894058dSAneesh Kumar K.V 			list_del(&entry->list);
4722c894058dSAneesh Kumar K.V 			spin_unlock(&sbi->s_md_lock);
4723c894058dSAneesh Kumar K.V 			kmem_cache_free(ext4_free_ext_cachep, entry);
4724c894058dSAneesh Kumar K.V 		}
4725c894058dSAneesh Kumar K.V 	}
47263e624fc7STheodore Ts'o 	/* Add the extent to transaction's private list */
4727c894058dSAneesh Kumar K.V 	spin_lock(&sbi->s_md_lock);
47283e624fc7STheodore Ts'o 	list_add(&new_entry->list, &handle->h_transaction->t_private_list);
4729c894058dSAneesh Kumar K.V 	spin_unlock(&sbi->s_md_lock);
4730c9de560dSAlex Tomas 	return 0;
4731c9de560dSAlex Tomas }
4732c9de560dSAlex Tomas 
4733c9de560dSAlex Tomas /*
4734c9de560dSAlex Tomas  * Main entry point into mballoc to free blocks
4735c9de560dSAlex Tomas  */
4736c9de560dSAlex Tomas void ext4_mb_free_blocks(handle_t *handle, struct inode *inode,
4737c9de560dSAlex Tomas 			unsigned long block, unsigned long count,
4738c9de560dSAlex Tomas 			int metadata, unsigned long *freed)
4739c9de560dSAlex Tomas {
474026346ff6SAneesh Kumar K.V 	struct buffer_head *bitmap_bh = NULL;
4741c9de560dSAlex Tomas 	struct super_block *sb = inode->i_sb;
4742256bdb49SEric Sandeen 	struct ext4_allocation_context *ac = NULL;
4743c9de560dSAlex Tomas 	struct ext4_group_desc *gdp;
4744c9de560dSAlex Tomas 	struct ext4_super_block *es;
4745498e5f24STheodore Ts'o 	unsigned int overflow;
4746c9de560dSAlex Tomas 	ext4_grpblk_t bit;
4747c9de560dSAlex Tomas 	struct buffer_head *gd_bh;
4748c9de560dSAlex Tomas 	ext4_group_t block_group;
4749c9de560dSAlex Tomas 	struct ext4_sb_info *sbi;
4750c9de560dSAlex Tomas 	struct ext4_buddy e4b;
4751c9de560dSAlex Tomas 	int err = 0;
4752c9de560dSAlex Tomas 	int ret;
4753c9de560dSAlex Tomas 
4754c9de560dSAlex Tomas 	*freed = 0;
4755c9de560dSAlex Tomas 
4756c9de560dSAlex Tomas 	sbi = EXT4_SB(sb);
4757c9de560dSAlex Tomas 	es = EXT4_SB(sb)->s_es;
4758c9de560dSAlex Tomas 	if (block < le32_to_cpu(es->s_first_data_block) ||
4759c9de560dSAlex Tomas 	    block + count < block ||
4760c9de560dSAlex Tomas 	    block + count > ext4_blocks_count(es)) {
476146e665e9SHarvey Harrison 		ext4_error(sb, __func__,
4762c9de560dSAlex Tomas 			    "Freeing blocks not in datazone - "
4763c9de560dSAlex Tomas 			    "block = %lu, count = %lu", block, count);
4764c9de560dSAlex Tomas 		goto error_return;
4765c9de560dSAlex Tomas 	}
4766c9de560dSAlex Tomas 
4767c9de560dSAlex Tomas 	ext4_debug("freeing block %lu\n", block);
4768ba80b101STheodore Ts'o 	trace_mark(ext4_free_blocks,
4769ba80b101STheodore Ts'o 		   "dev %s block %llu count %lu metadata %d ino %lu",
4770ba80b101STheodore Ts'o 		   sb->s_id, (unsigned long long) block, count, metadata,
4771ba80b101STheodore Ts'o 		   inode ? inode->i_ino : 0);
4772c9de560dSAlex Tomas 
4773256bdb49SEric Sandeen 	ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
4774256bdb49SEric Sandeen 	if (ac) {
4775256bdb49SEric Sandeen 		ac->ac_op = EXT4_MB_HISTORY_FREE;
4776256bdb49SEric Sandeen 		ac->ac_inode = inode;
4777256bdb49SEric Sandeen 		ac->ac_sb = sb;
4778256bdb49SEric Sandeen 	}
4779c9de560dSAlex Tomas 
4780c9de560dSAlex Tomas do_more:
4781c9de560dSAlex Tomas 	overflow = 0;
4782c9de560dSAlex Tomas 	ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
4783c9de560dSAlex Tomas 
4784c9de560dSAlex Tomas 	/*
4785c9de560dSAlex Tomas 	 * Check to see if we are freeing blocks across a group
4786c9de560dSAlex Tomas 	 * boundary.
4787c9de560dSAlex Tomas 	 */
4788c9de560dSAlex Tomas 	if (bit + count > EXT4_BLOCKS_PER_GROUP(sb)) {
4789c9de560dSAlex Tomas 		overflow = bit + count - EXT4_BLOCKS_PER_GROUP(sb);
4790c9de560dSAlex Tomas 		count -= overflow;
4791c9de560dSAlex Tomas 	}
4792574ca174STheodore Ts'o 	bitmap_bh = ext4_read_block_bitmap(sb, block_group);
4793ce89f46cSAneesh Kumar K.V 	if (!bitmap_bh) {
4794ce89f46cSAneesh Kumar K.V 		err = -EIO;
4795c9de560dSAlex Tomas 		goto error_return;
4796ce89f46cSAneesh Kumar K.V 	}
4797c9de560dSAlex Tomas 	gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
4798ce89f46cSAneesh Kumar K.V 	if (!gdp) {
4799ce89f46cSAneesh Kumar K.V 		err = -EIO;
4800c9de560dSAlex Tomas 		goto error_return;
4801ce89f46cSAneesh Kumar K.V 	}
4802c9de560dSAlex Tomas 
4803c9de560dSAlex Tomas 	if (in_range(ext4_block_bitmap(sb, gdp), block, count) ||
4804c9de560dSAlex Tomas 	    in_range(ext4_inode_bitmap(sb, gdp), block, count) ||
4805c9de560dSAlex Tomas 	    in_range(block, ext4_inode_table(sb, gdp),
4806c9de560dSAlex Tomas 		      EXT4_SB(sb)->s_itb_per_group) ||
4807c9de560dSAlex Tomas 	    in_range(block + count - 1, ext4_inode_table(sb, gdp),
4808c9de560dSAlex Tomas 		      EXT4_SB(sb)->s_itb_per_group)) {
4809c9de560dSAlex Tomas 
481046e665e9SHarvey Harrison 		ext4_error(sb, __func__,
4811c9de560dSAlex Tomas 			   "Freeing blocks in system zone - "
4812c9de560dSAlex Tomas 			   "Block = %lu, count = %lu", block, count);
4813519deca0SAneesh Kumar K.V 		/* err = 0. ext4_std_error should be a no op */
4814519deca0SAneesh Kumar K.V 		goto error_return;
4815c9de560dSAlex Tomas 	}
4816c9de560dSAlex Tomas 
4817c9de560dSAlex Tomas 	BUFFER_TRACE(bitmap_bh, "getting write access");
4818c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, bitmap_bh);
4819c9de560dSAlex Tomas 	if (err)
4820c9de560dSAlex Tomas 		goto error_return;
4821c9de560dSAlex Tomas 
4822c9de560dSAlex Tomas 	/*
4823c9de560dSAlex Tomas 	 * We are about to modify some metadata.  Call the journal APIs
4824c9de560dSAlex Tomas 	 * to unshare ->b_data if a currently-committing transaction is
4825c9de560dSAlex Tomas 	 * using it
4826c9de560dSAlex Tomas 	 */
4827c9de560dSAlex Tomas 	BUFFER_TRACE(gd_bh, "get_write_access");
4828c9de560dSAlex Tomas 	err = ext4_journal_get_write_access(handle, gd_bh);
4829c9de560dSAlex Tomas 	if (err)
4830c9de560dSAlex Tomas 		goto error_return;
4831c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
4832c9de560dSAlex Tomas 	{
4833c9de560dSAlex Tomas 		int i;
4834c9de560dSAlex Tomas 		for (i = 0; i < count; i++)
4835c9de560dSAlex Tomas 			BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
4836c9de560dSAlex Tomas 	}
4837c9de560dSAlex Tomas #endif
4838256bdb49SEric Sandeen 	if (ac) {
4839256bdb49SEric Sandeen 		ac->ac_b_ex.fe_group = block_group;
4840256bdb49SEric Sandeen 		ac->ac_b_ex.fe_start = bit;
4841256bdb49SEric Sandeen 		ac->ac_b_ex.fe_len = count;
4842256bdb49SEric Sandeen 		ext4_mb_store_history(ac);
4843256bdb49SEric Sandeen 	}
4844c9de560dSAlex Tomas 
4845920313a7SAneesh Kumar K.V 	err = ext4_mb_load_buddy(sb, block_group, &e4b);
4846920313a7SAneesh Kumar K.V 	if (err)
4847920313a7SAneesh Kumar K.V 		goto error_return;
48480390131bSFrank Mayhar 	if (metadata && ext4_handle_valid(handle)) {
48497a2fcbf7SAneesh Kumar K.V 		struct ext4_free_data *new_entry;
48507a2fcbf7SAneesh Kumar K.V 		/*
48517a2fcbf7SAneesh Kumar K.V 		 * blocks being freed are metadata. these blocks shouldn't
48527a2fcbf7SAneesh Kumar K.V 		 * be used until this transaction is committed
48537a2fcbf7SAneesh Kumar K.V 		 */
48547a2fcbf7SAneesh Kumar K.V 		new_entry  = kmem_cache_alloc(ext4_free_ext_cachep, GFP_NOFS);
48557a2fcbf7SAneesh Kumar K.V 		new_entry->start_blk = bit;
48567a2fcbf7SAneesh Kumar K.V 		new_entry->group  = block_group;
48577a2fcbf7SAneesh Kumar K.V 		new_entry->count = count;
48587a2fcbf7SAneesh Kumar K.V 		new_entry->t_tid = handle->h_transaction->t_tid;
48597a2fcbf7SAneesh Kumar K.V 		ext4_lock_group(sb, block_group);
48607a2fcbf7SAneesh Kumar K.V 		mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
48617a2fcbf7SAneesh Kumar K.V 				bit, count);
48627a2fcbf7SAneesh Kumar K.V 		ext4_mb_free_metadata(handle, &e4b, new_entry);
48637a2fcbf7SAneesh Kumar K.V 		ext4_unlock_group(sb, block_group);
4864c9de560dSAlex Tomas 	} else {
4865c9de560dSAlex Tomas 		ext4_lock_group(sb, block_group);
48667a2fcbf7SAneesh Kumar K.V 		/* need to update group_info->bb_free and bitmap
48677a2fcbf7SAneesh Kumar K.V 		 * with group lock held. generate_buddy look at
48687a2fcbf7SAneesh Kumar K.V 		 * them with group lock_held
48697a2fcbf7SAneesh Kumar K.V 		 */
48707a2fcbf7SAneesh Kumar K.V 		mb_clear_bits(sb_bgl_lock(sbi, block_group), bitmap_bh->b_data,
48717a2fcbf7SAneesh Kumar K.V 				bit, count);
48727e5a8cddSShen Feng 		mb_free_blocks(inode, &e4b, bit, count);
4873c9de560dSAlex Tomas 		ext4_mb_return_to_preallocation(inode, &e4b, block, count);
4874c9de560dSAlex Tomas 		ext4_unlock_group(sb, block_group);
4875c9de560dSAlex Tomas 	}
4876c9de560dSAlex Tomas 
4877c9de560dSAlex Tomas 	spin_lock(sb_bgl_lock(sbi, block_group));
4878560671a0SAneesh Kumar K.V 	ret = ext4_free_blks_count(sb, gdp) + count;
4879560671a0SAneesh Kumar K.V 	ext4_free_blks_set(sb, gdp, ret);
4880c9de560dSAlex Tomas 	gdp->bg_checksum = ext4_group_desc_csum(sbi, block_group, gdp);
4881c9de560dSAlex Tomas 	spin_unlock(sb_bgl_lock(sbi, block_group));
4882c9de560dSAlex Tomas 	percpu_counter_add(&sbi->s_freeblocks_counter, count);
4883c9de560dSAlex Tomas 
4884772cb7c8SJose R. Santos 	if (sbi->s_log_groups_per_flex) {
4885772cb7c8SJose R. Santos 		ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
48869f24e420STheodore Ts'o 		atomic_add(count, &sbi->s_flex_groups[flex_group].free_blocks);
4887772cb7c8SJose R. Santos 	}
4888772cb7c8SJose R. Santos 
4889c9de560dSAlex Tomas 	ext4_mb_release_desc(&e4b);
4890c9de560dSAlex Tomas 
4891c9de560dSAlex Tomas 	*freed += count;
4892c9de560dSAlex Tomas 
48937a2fcbf7SAneesh Kumar K.V 	/* We dirtied the bitmap block */
48947a2fcbf7SAneesh Kumar K.V 	BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
48957a2fcbf7SAneesh Kumar K.V 	err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
48967a2fcbf7SAneesh Kumar K.V 
4897c9de560dSAlex Tomas 	/* And the group descriptor block */
4898c9de560dSAlex Tomas 	BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
48990390131bSFrank Mayhar 	ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
4900c9de560dSAlex Tomas 	if (!err)
4901c9de560dSAlex Tomas 		err = ret;
4902c9de560dSAlex Tomas 
4903c9de560dSAlex Tomas 	if (overflow && !err) {
4904c9de560dSAlex Tomas 		block += count;
4905c9de560dSAlex Tomas 		count = overflow;
4906c9de560dSAlex Tomas 		put_bh(bitmap_bh);
4907c9de560dSAlex Tomas 		goto do_more;
4908c9de560dSAlex Tomas 	}
4909c9de560dSAlex Tomas 	sb->s_dirt = 1;
4910c9de560dSAlex Tomas error_return:
4911c9de560dSAlex Tomas 	brelse(bitmap_bh);
4912c9de560dSAlex Tomas 	ext4_std_error(sb, err);
4913256bdb49SEric Sandeen 	if (ac)
4914256bdb49SEric Sandeen 		kmem_cache_free(ext4_ac_cachep, ac);
4915c9de560dSAlex Tomas 	return;
4916c9de560dSAlex Tomas }
4917