1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
2c9de560dSAlex Tomas /*
3c9de560dSAlex Tomas * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
4c9de560dSAlex Tomas * Written by Alex Tomas <alex@clusterfs.com>
5c9de560dSAlex Tomas */
6c9de560dSAlex Tomas
7c9de560dSAlex Tomas
8c9de560dSAlex Tomas /*
9c9de560dSAlex Tomas * mballoc.c contains the multiblocks allocation routines
10c9de560dSAlex Tomas */
11c9de560dSAlex Tomas
1218aadd47SBobi Jam #include "ext4_jbd2.h"
138f6e39a7SMingming Cao #include "mballoc.h"
1428623c2fSTheodore Ts'o #include <linux/log2.h>
15a0b30c12STheodore Ts'o #include <linux/module.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
171a5d5e5dSJeremy Cline #include <linux/nospec.h>
1866114cadSTejun Heo #include <linux/backing-dev.h>
195229a658SJan Kara #include <linux/freezer.h>
209bffad1eSTheodore Ts'o #include <trace/events/ext4.h>
219bffad1eSTheodore Ts'o
22c9de560dSAlex Tomas /*
23c9de560dSAlex Tomas * MUSTDO:
24c9de560dSAlex Tomas * - test ext4_ext_search_left() and ext4_ext_search_right()
25c9de560dSAlex Tomas * - search for metadata in few groups
26c9de560dSAlex Tomas *
27c9de560dSAlex Tomas * TODO v4:
28c9de560dSAlex Tomas * - normalization should take into account whether file is still open
29c9de560dSAlex Tomas * - discard preallocations if no free space left (policy?)
30c9de560dSAlex Tomas * - don't normalize tails
31c9de560dSAlex Tomas * - quota
32c9de560dSAlex Tomas * - reservation for superuser
33c9de560dSAlex Tomas *
34c9de560dSAlex Tomas * TODO v3:
35c9de560dSAlex Tomas * - bitmap read-ahead (proposed by Oleg Drokin aka green)
36c9de560dSAlex Tomas * - track min/max extents in each group for better group selection
37c9de560dSAlex Tomas * - mb_mark_used() may allocate chunk right after splitting buddy
38c9de560dSAlex Tomas * - tree of groups sorted by number of free blocks
39c9de560dSAlex Tomas * - error handling
40c9de560dSAlex Tomas */
41c9de560dSAlex Tomas
42c9de560dSAlex Tomas /*
43c9de560dSAlex Tomas * The allocation request involve request for multiple number of blocks
44c9de560dSAlex Tomas * near to the goal(block) value specified.
45c9de560dSAlex Tomas *
46b713a5ecSTheodore Ts'o * During initialization phase of the allocator we decide to use the
47b713a5ecSTheodore Ts'o * group preallocation or inode preallocation depending on the size of
48b713a5ecSTheodore Ts'o * the file. The size of the file could be the resulting file size we
49b713a5ecSTheodore Ts'o * would have after allocation, or the current file size, which ever
50b713a5ecSTheodore Ts'o * is larger. If the size is less than sbi->s_mb_stream_request we
51b713a5ecSTheodore Ts'o * select to use the group preallocation. The default value of
52b713a5ecSTheodore Ts'o * s_mb_stream_request is 16 blocks. This can also be tuned via
53b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_stream_req. The value is represented in
54b713a5ecSTheodore Ts'o * terms of number of blocks.
55c9de560dSAlex Tomas *
56c9de560dSAlex Tomas * The main motivation for having small file use group preallocation is to
57b713a5ecSTheodore Ts'o * ensure that we have small files closer together on the disk.
58c9de560dSAlex Tomas *
59b713a5ecSTheodore Ts'o * First stage the allocator looks at the inode prealloc list,
60b713a5ecSTheodore Ts'o * ext4_inode_info->i_prealloc_list, which contains list of prealloc
61b713a5ecSTheodore Ts'o * spaces for this particular inode. The inode prealloc space is
62b713a5ecSTheodore Ts'o * represented as:
63c9de560dSAlex Tomas *
64c9de560dSAlex Tomas * pa_lstart -> the logical start block for this prealloc space
65c9de560dSAlex Tomas * pa_pstart -> the physical start block for this prealloc space
6653accfa9STheodore Ts'o * pa_len -> length for this prealloc space (in clusters)
6753accfa9STheodore Ts'o * pa_free -> free space available in this prealloc space (in clusters)
68c9de560dSAlex Tomas *
69c9de560dSAlex Tomas * The inode preallocation space is used looking at the _logical_ start
70c9de560dSAlex Tomas * block. If only the logical file block falls within the range of prealloc
71caaf7a29STao Ma * space we will consume the particular prealloc space. This makes sure that
72caaf7a29STao Ma * we have contiguous physical blocks representing the file blocks
73c9de560dSAlex Tomas *
74c9de560dSAlex Tomas * The important thing to be noted in case of inode prealloc space is that
75c9de560dSAlex Tomas * we don't modify the values associated to inode prealloc space except
76c9de560dSAlex Tomas * pa_free.
77c9de560dSAlex Tomas *
78c9de560dSAlex Tomas * If we are not able to find blocks in the inode prealloc space and if we
79c9de560dSAlex Tomas * have the group allocation flag set then we look at the locality group
80caaf7a29STao Ma * prealloc space. These are per CPU prealloc list represented as
81c9de560dSAlex Tomas *
82c9de560dSAlex Tomas * ext4_sb_info.s_locality_groups[smp_processor_id()]
83c9de560dSAlex Tomas *
84c9de560dSAlex Tomas * The reason for having a per cpu locality group is to reduce the contention
85c9de560dSAlex Tomas * between CPUs. It is possible to get scheduled at this point.
86c9de560dSAlex Tomas *
87c9de560dSAlex Tomas * The locality group prealloc space is used looking at whether we have
8825985edcSLucas De Marchi * enough free space (pa_free) within the prealloc space.
89c9de560dSAlex Tomas *
90c9de560dSAlex Tomas * If we can't allocate blocks via inode prealloc or/and locality group
91c9de560dSAlex Tomas * prealloc then we look at the buddy cache. The buddy cache is represented
92c9de560dSAlex Tomas * by ext4_sb_info.s_buddy_cache (struct inode) whose file offset gets
93c9de560dSAlex Tomas * mapped to the buddy and bitmap information regarding different
94c9de560dSAlex Tomas * groups. The buddy information is attached to buddy cache inode so that
95c9de560dSAlex Tomas * we can access them through the page cache. The information regarding
96c9de560dSAlex Tomas * each group is loaded via ext4_mb_load_buddy. The information involve
97c9de560dSAlex Tomas * block bitmap and buddy information. The information are stored in the
98c9de560dSAlex Tomas * inode as:
99c9de560dSAlex Tomas *
100c9de560dSAlex Tomas * { page }
101c3a326a6SAneesh Kumar K.V * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
102c9de560dSAlex Tomas *
103c9de560dSAlex Tomas *
104c9de560dSAlex Tomas * one block each for bitmap and buddy information. So for each group we
105ea1754a0SKirill A. Shutemov * take up 2 blocks. A page can contain blocks_per_page (PAGE_SIZE /
106c9de560dSAlex Tomas * blocksize) blocks. So it can have information regarding groups_per_page
107c9de560dSAlex Tomas * which is blocks_per_page/2
108c9de560dSAlex Tomas *
109c9de560dSAlex Tomas * The buddy cache inode is not stored on disk. The inode is thrown
110c9de560dSAlex Tomas * away when the filesystem is unmounted.
111c9de560dSAlex Tomas *
112c9de560dSAlex Tomas * We look for count number of blocks in the buddy cache. If we were able
113c9de560dSAlex Tomas * to locate that many free blocks we return with additional information
114c9de560dSAlex Tomas * regarding rest of the contiguous physical block available
115c9de560dSAlex Tomas *
116c9de560dSAlex Tomas * Before allocating blocks via buddy cache we normalize the request
117c9de560dSAlex Tomas * blocks. This ensure we ask for more blocks that we needed. The extra
118c9de560dSAlex Tomas * blocks that we get after allocation is added to the respective prealloc
119c9de560dSAlex Tomas * list. In case of inode preallocation we follow a list of heuristics
120c9de560dSAlex Tomas * based on file size. This can be found in ext4_mb_normalize_request. If
121c9de560dSAlex Tomas * we are doing a group prealloc we try to normalize the request to
12227baebb8STheodore Ts'o * sbi->s_mb_group_prealloc. The default value of s_mb_group_prealloc is
12327baebb8STheodore Ts'o * dependent on the cluster size; for non-bigalloc file systems, it is
124c9de560dSAlex Tomas * 512 blocks. This can be tuned via
125d7a1fee1SDan Ehrenberg * /sys/fs/ext4/<partition>/mb_group_prealloc. The value is represented in
126c9de560dSAlex Tomas * terms of number of blocks. If we have mounted the file system with -O
127c9de560dSAlex Tomas * stripe=<value> option the group prealloc request is normalized to the
128b483bb77SRandy Dunlap * smallest multiple of the stripe value (sbi->s_stripe) which is
129d7a1fee1SDan Ehrenberg * greater than the default mb_group_prealloc.
130c9de560dSAlex Tomas *
131196e402aSHarshad Shirwadkar * If "mb_optimize_scan" mount option is set, we maintain in memory group info
132196e402aSHarshad Shirwadkar * structures in two data structures:
133196e402aSHarshad Shirwadkar *
134196e402aSHarshad Shirwadkar * 1) Array of largest free order lists (sbi->s_mb_largest_free_orders)
135196e402aSHarshad Shirwadkar *
136196e402aSHarshad Shirwadkar * Locking: sbi->s_mb_largest_free_orders_locks(array of rw locks)
137196e402aSHarshad Shirwadkar *
138196e402aSHarshad Shirwadkar * This is an array of lists where the index in the array represents the
139196e402aSHarshad Shirwadkar * largest free order in the buddy bitmap of the participating group infos of
140196e402aSHarshad Shirwadkar * that list. So, there are exactly MB_NUM_ORDERS(sb) (which means total
141196e402aSHarshad Shirwadkar * number of buddy bitmap orders possible) number of lists. Group-infos are
142196e402aSHarshad Shirwadkar * placed in appropriate lists.
143196e402aSHarshad Shirwadkar *
14483e80a6eSJan Kara * 2) Average fragment size lists (sbi->s_mb_avg_fragment_size)
145196e402aSHarshad Shirwadkar *
14683e80a6eSJan Kara * Locking: sbi->s_mb_avg_fragment_size_locks(array of rw locks)
147196e402aSHarshad Shirwadkar *
14883e80a6eSJan Kara * This is an array of lists where in the i-th list there are groups with
14983e80a6eSJan Kara * average fragment size >= 2^i and < 2^(i+1). The average fragment size
15083e80a6eSJan Kara * is computed as ext4_group_info->bb_free / ext4_group_info->bb_fragments.
15183e80a6eSJan Kara * Note that we don't bother with a special list for completely empty groups
15283e80a6eSJan Kara * so we only have MB_NUM_ORDERS(sb) lists.
153196e402aSHarshad Shirwadkar *
154196e402aSHarshad Shirwadkar * When "mb_optimize_scan" mount option is set, mballoc consults the above data
155196e402aSHarshad Shirwadkar * structures to decide the order in which groups are to be traversed for
156196e402aSHarshad Shirwadkar * fulfilling an allocation request.
157196e402aSHarshad Shirwadkar *
158f52f3d2bSOjaswin Mujoo * At CR_POWER2_ALIGNED , we look for groups which have the largest_free_order
159f52f3d2bSOjaswin Mujoo * >= the order of the request. We directly look at the largest free order list
160f52f3d2bSOjaswin Mujoo * in the data structure (1) above where largest_free_order = order of the
161f52f3d2bSOjaswin Mujoo * request. If that list is empty, we look at remaining list in the increasing
162f52f3d2bSOjaswin Mujoo * order of largest_free_order. This allows us to perform CR_POWER2_ALIGNED
163f52f3d2bSOjaswin Mujoo * lookup in O(1) time.
164196e402aSHarshad Shirwadkar *
165f52f3d2bSOjaswin Mujoo * At CR_GOAL_LEN_FAST, we only consider groups where
166f52f3d2bSOjaswin Mujoo * average fragment size > request size. So, we lookup a group which has average
167f52f3d2bSOjaswin Mujoo * fragment size just above or equal to request size using our average fragment
168f52f3d2bSOjaswin Mujoo * size group lists (data structure 2) in O(1) time.
169196e402aSHarshad Shirwadkar *
170f52f3d2bSOjaswin Mujoo * At CR_BEST_AVAIL_LEN, we aim to optimize allocations which can't be satisfied
171f52f3d2bSOjaswin Mujoo * in CR_GOAL_LEN_FAST. The fact that we couldn't find a group in
172f52f3d2bSOjaswin Mujoo * CR_GOAL_LEN_FAST suggests that there is no BG that has avg
173f52f3d2bSOjaswin Mujoo * fragment size > goal length. So before falling to the slower
174f52f3d2bSOjaswin Mujoo * CR_GOAL_LEN_SLOW, in CR_BEST_AVAIL_LEN we proactively trim goal length and
175f52f3d2bSOjaswin Mujoo * then use the same fragment lists as CR_GOAL_LEN_FAST to find a BG with a big
176f52f3d2bSOjaswin Mujoo * enough average fragment size. This increases the chances of finding a
177f52f3d2bSOjaswin Mujoo * suitable block group in O(1) time and results in faster allocation at the
178f52f3d2bSOjaswin Mujoo * cost of reduced size of allocation.
1797e170922SOjaswin Mujoo *
180196e402aSHarshad Shirwadkar * If "mb_optimize_scan" mount option is not set, mballoc traverses groups in
181f52f3d2bSOjaswin Mujoo * linear order which requires O(N) search time for each CR_POWER2_ALIGNED and
182f52f3d2bSOjaswin Mujoo * CR_GOAL_LEN_FAST phase.
183196e402aSHarshad Shirwadkar *
184d7a1fee1SDan Ehrenberg * The regular allocator (using the buddy cache) supports a few tunables.
185c9de560dSAlex Tomas *
186b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_min_to_scan
187b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_max_to_scan
188b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_order2_req
189196e402aSHarshad Shirwadkar * /sys/fs/ext4/<partition>/mb_linear_limit
190c9de560dSAlex Tomas *
191b713a5ecSTheodore Ts'o * The regular allocator uses buddy scan only if the request len is power of
192c9de560dSAlex Tomas * 2 blocks and the order of allocation is >= sbi->s_mb_order2_reqs. The
193c9de560dSAlex Tomas * value of s_mb_order2_reqs can be tuned via
194b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_order2_req. If the request len is equal to
195af901ca1SAndré Goddard Rosa * stripe size (sbi->s_stripe), we try to search for contiguous block in
196b713a5ecSTheodore Ts'o * stripe size. This should result in better allocation on RAID setups. If
197b713a5ecSTheodore Ts'o * not, we search in the specific group using bitmap for best extents. The
198b713a5ecSTheodore Ts'o * tunable min_to_scan and max_to_scan control the behaviour here.
199c9de560dSAlex Tomas * min_to_scan indicate how long the mballoc __must__ look for a best
200b713a5ecSTheodore Ts'o * extent and max_to_scan indicates how long the mballoc __can__ look for a
201c9de560dSAlex Tomas * best extent in the found extents. Searching for the blocks starts with
202c9de560dSAlex Tomas * the group specified as the goal value in allocation context via
203c9de560dSAlex Tomas * ac_g_ex. Each group is first checked based on the criteria whether it
204caaf7a29STao Ma * can be used for allocation. ext4_mb_good_group explains how the groups are
205c9de560dSAlex Tomas * checked.
206c9de560dSAlex Tomas *
207196e402aSHarshad Shirwadkar * When "mb_optimize_scan" is turned on, as mentioned above, the groups may not
208196e402aSHarshad Shirwadkar * get traversed linearly. That may result in subsequent allocations being not
209196e402aSHarshad Shirwadkar * close to each other. And so, the underlying device may get filled up in a
210196e402aSHarshad Shirwadkar * non-linear fashion. While that may not matter on non-rotational devices, for
211196e402aSHarshad Shirwadkar * rotational devices that may result in higher seek times. "mb_linear_limit"
212196e402aSHarshad Shirwadkar * tells mballoc how many groups mballoc should search linearly before
213196e402aSHarshad Shirwadkar * performing consulting above data structures for more efficient lookups. For
214196e402aSHarshad Shirwadkar * non rotational devices, this value defaults to 0 and for rotational devices
215196e402aSHarshad Shirwadkar * this is set to MB_DEFAULT_LINEAR_LIMIT.
216196e402aSHarshad Shirwadkar *
217c9de560dSAlex Tomas * Both the prealloc space are getting populated as above. So for the first
218c9de560dSAlex Tomas * request we will hit the buddy cache which will result in this prealloc
219c9de560dSAlex Tomas * space getting filled. The prealloc space is then later used for the
220c9de560dSAlex Tomas * subsequent request.
221c9de560dSAlex Tomas */
222c9de560dSAlex Tomas
223c9de560dSAlex Tomas /*
224c9de560dSAlex Tomas * mballoc operates on the following data:
225c9de560dSAlex Tomas * - on-disk bitmap
226c9de560dSAlex Tomas * - in-core buddy (actually includes buddy and bitmap)
227c9de560dSAlex Tomas * - preallocation descriptors (PAs)
228c9de560dSAlex Tomas *
229c9de560dSAlex Tomas * there are two types of preallocations:
230c9de560dSAlex Tomas * - inode
231c9de560dSAlex Tomas * assiged to specific inode and can be used for this inode only.
232c9de560dSAlex Tomas * it describes part of inode's space preallocated to specific
233c9de560dSAlex Tomas * physical blocks. any block from that preallocated can be used
234c9de560dSAlex Tomas * independent. the descriptor just tracks number of blocks left
235c9de560dSAlex Tomas * unused. so, before taking some block from descriptor, one must
236c9de560dSAlex Tomas * make sure corresponded logical block isn't allocated yet. this
237c9de560dSAlex Tomas * also means that freeing any block within descriptor's range
238c9de560dSAlex Tomas * must discard all preallocated blocks.
239c9de560dSAlex Tomas * - locality group
240c9de560dSAlex Tomas * assigned to specific locality group which does not translate to
241c9de560dSAlex Tomas * permanent set of inodes: inode can join and leave group. space
242c9de560dSAlex Tomas * from this type of preallocation can be used for any inode. thus
243c9de560dSAlex Tomas * it's consumed from the beginning to the end.
244c9de560dSAlex Tomas *
245c9de560dSAlex Tomas * relation between them can be expressed as:
246c9de560dSAlex Tomas * in-core buddy = on-disk bitmap + preallocation descriptors
247c9de560dSAlex Tomas *
248c9de560dSAlex Tomas * this mean blocks mballoc considers used are:
249c9de560dSAlex Tomas * - allocated blocks (persistent)
250c9de560dSAlex Tomas * - preallocated blocks (non-persistent)
251c9de560dSAlex Tomas *
252c9de560dSAlex Tomas * consistency in mballoc world means that at any time a block is either
253c9de560dSAlex Tomas * free or used in ALL structures. notice: "any time" should not be read
254c9de560dSAlex Tomas * literally -- time is discrete and delimited by locks.
255c9de560dSAlex Tomas *
256c9de560dSAlex Tomas * to keep it simple, we don't use block numbers, instead we count number of
257c9de560dSAlex Tomas * blocks: how many blocks marked used/free in on-disk bitmap, buddy and PA.
258c9de560dSAlex Tomas *
259c9de560dSAlex Tomas * all operations can be expressed as:
260c9de560dSAlex Tomas * - init buddy: buddy = on-disk + PAs
261c9de560dSAlex Tomas * - new PA: buddy += N; PA = N
262c9de560dSAlex Tomas * - use inode PA: on-disk += N; PA -= N
263c9de560dSAlex Tomas * - discard inode PA buddy -= on-disk - PA; PA = 0
264c9de560dSAlex Tomas * - use locality group PA on-disk += N; PA -= N
265c9de560dSAlex Tomas * - discard locality group PA buddy -= PA; PA = 0
266c9de560dSAlex Tomas * note: 'buddy -= on-disk - PA' is used to show that on-disk bitmap
267c9de560dSAlex Tomas * is used in real operation because we can't know actual used
268c9de560dSAlex Tomas * bits from PA, only from on-disk bitmap
269c9de560dSAlex Tomas *
270c9de560dSAlex Tomas * if we follow this strict logic, then all operations above should be atomic.
271c9de560dSAlex Tomas * given some of them can block, we'd have to use something like semaphores
272c9de560dSAlex Tomas * killing performance on high-end SMP hardware. let's try to relax it using
273c9de560dSAlex Tomas * the following knowledge:
274c9de560dSAlex Tomas * 1) if buddy is referenced, it's already initialized
275c9de560dSAlex Tomas * 2) while block is used in buddy and the buddy is referenced,
276c9de560dSAlex Tomas * nobody can re-allocate that block
277c9de560dSAlex Tomas * 3) we work on bitmaps and '+' actually means 'set bits'. if on-disk has
278c9de560dSAlex Tomas * bit set and PA claims same block, it's OK. IOW, one can set bit in
279c9de560dSAlex Tomas * on-disk bitmap if buddy has same bit set or/and PA covers corresponded
280c9de560dSAlex Tomas * block
281c9de560dSAlex Tomas *
282c9de560dSAlex Tomas * so, now we're building a concurrency table:
283c9de560dSAlex Tomas * - init buddy vs.
284c9de560dSAlex Tomas * - new PA
285c9de560dSAlex Tomas * blocks for PA are allocated in the buddy, buddy must be referenced
286c9de560dSAlex Tomas * until PA is linked to allocation group to avoid concurrent buddy init
287c9de560dSAlex Tomas * - use inode PA
288c9de560dSAlex Tomas * we need to make sure that either on-disk bitmap or PA has uptodate data
289c9de560dSAlex Tomas * given (3) we care that PA-=N operation doesn't interfere with init
290c9de560dSAlex Tomas * - discard inode PA
291c9de560dSAlex Tomas * the simplest way would be to have buddy initialized by the discard
292c9de560dSAlex Tomas * - use locality group PA
293c9de560dSAlex Tomas * again PA-=N must be serialized with init
294c9de560dSAlex Tomas * - discard locality group PA
295c9de560dSAlex Tomas * the simplest way would be to have buddy initialized by the discard
296c9de560dSAlex Tomas * - new PA vs.
297c9de560dSAlex Tomas * - use inode PA
298c9de560dSAlex Tomas * i_data_sem serializes them
299c9de560dSAlex Tomas * - discard inode PA
300c9de560dSAlex Tomas * discard process must wait until PA isn't used by another process
301c9de560dSAlex Tomas * - use locality group PA
302c9de560dSAlex Tomas * some mutex should serialize them
303c9de560dSAlex Tomas * - discard locality group PA
304c9de560dSAlex Tomas * discard process must wait until PA isn't used by another process
305c9de560dSAlex Tomas * - use inode PA
306c9de560dSAlex Tomas * - use inode PA
307c9de560dSAlex Tomas * i_data_sem or another mutex should serializes them
308c9de560dSAlex Tomas * - discard inode PA
309c9de560dSAlex Tomas * discard process must wait until PA isn't used by another process
310c9de560dSAlex Tomas * - use locality group PA
311c9de560dSAlex Tomas * nothing wrong here -- they're different PAs covering different blocks
312c9de560dSAlex Tomas * - discard locality group PA
313c9de560dSAlex Tomas * discard process must wait until PA isn't used by another process
314c9de560dSAlex Tomas *
315c9de560dSAlex Tomas * now we're ready to make few consequences:
316c9de560dSAlex Tomas * - PA is referenced and while it is no discard is possible
317c9de560dSAlex Tomas * - PA is referenced until block isn't marked in on-disk bitmap
318c9de560dSAlex Tomas * - PA changes only after on-disk bitmap
319c9de560dSAlex Tomas * - discard must not compete with init. either init is done before
320c9de560dSAlex Tomas * any discard or they're serialized somehow
321c9de560dSAlex Tomas * - buddy init as sum of on-disk bitmap and PAs is done atomically
322c9de560dSAlex Tomas *
323c9de560dSAlex Tomas * a special case when we've used PA to emptiness. no need to modify buddy
324c9de560dSAlex Tomas * in this case, but we should care about concurrent init
325c9de560dSAlex Tomas *
326c9de560dSAlex Tomas */
327c9de560dSAlex Tomas
328c9de560dSAlex Tomas /*
329c9de560dSAlex Tomas * Logic in few words:
330c9de560dSAlex Tomas *
331c9de560dSAlex Tomas * - allocation:
332c9de560dSAlex Tomas * load group
333c9de560dSAlex Tomas * find blocks
334c9de560dSAlex Tomas * mark bits in on-disk bitmap
335c9de560dSAlex Tomas * release group
336c9de560dSAlex Tomas *
337c9de560dSAlex Tomas * - use preallocation:
338c9de560dSAlex Tomas * find proper PA (per-inode or group)
339c9de560dSAlex Tomas * load group
340c9de560dSAlex Tomas * mark bits in on-disk bitmap
341c9de560dSAlex Tomas * release group
342c9de560dSAlex Tomas * release PA
343c9de560dSAlex Tomas *
344c9de560dSAlex Tomas * - free:
345c9de560dSAlex Tomas * load group
346c9de560dSAlex Tomas * mark bits in on-disk bitmap
347c9de560dSAlex Tomas * release group
348c9de560dSAlex Tomas *
349c9de560dSAlex Tomas * - discard preallocations in group:
350c9de560dSAlex Tomas * mark PAs deleted
351c9de560dSAlex Tomas * move them onto local list
352c9de560dSAlex Tomas * load on-disk bitmap
353c9de560dSAlex Tomas * load group
354c9de560dSAlex Tomas * remove PA from object (inode or locality group)
355c9de560dSAlex Tomas * mark free blocks in-core
356c9de560dSAlex Tomas *
357c9de560dSAlex Tomas * - discard inode's preallocations:
358c9de560dSAlex Tomas */
359c9de560dSAlex Tomas
360c9de560dSAlex Tomas /*
361c9de560dSAlex Tomas * Locking rules
362c9de560dSAlex Tomas *
363c9de560dSAlex Tomas * Locks:
364c9de560dSAlex Tomas * - bitlock on a group (group)
365c9de560dSAlex Tomas * - object (inode/locality) (object)
366c9de560dSAlex Tomas * - per-pa lock (pa)
367f52f3d2bSOjaswin Mujoo * - cr_power2_aligned lists lock (cr_power2_aligned)
368f52f3d2bSOjaswin Mujoo * - cr_goal_len_fast lists lock (cr_goal_len_fast)
369c9de560dSAlex Tomas *
370c9de560dSAlex Tomas * Paths:
371c9de560dSAlex Tomas * - new pa
372c9de560dSAlex Tomas * object
373c9de560dSAlex Tomas * group
374c9de560dSAlex Tomas *
375c9de560dSAlex Tomas * - find and use pa:
376c9de560dSAlex Tomas * pa
377c9de560dSAlex Tomas *
378c9de560dSAlex Tomas * - release consumed pa:
379c9de560dSAlex Tomas * pa
380c9de560dSAlex Tomas * group
381c9de560dSAlex Tomas * object
382c9de560dSAlex Tomas *
383c9de560dSAlex Tomas * - generate in-core bitmap:
384c9de560dSAlex Tomas * group
385c9de560dSAlex Tomas * pa
386c9de560dSAlex Tomas *
387c9de560dSAlex Tomas * - discard all for given object (inode, locality group):
388c9de560dSAlex Tomas * object
389c9de560dSAlex Tomas * pa
390c9de560dSAlex Tomas * group
391c9de560dSAlex Tomas *
392c9de560dSAlex Tomas * - discard all for given group:
393c9de560dSAlex Tomas * group
394c9de560dSAlex Tomas * pa
395c9de560dSAlex Tomas * group
396c9de560dSAlex Tomas * object
397c9de560dSAlex Tomas *
398196e402aSHarshad Shirwadkar * - allocation path (ext4_mb_regular_allocator)
399196e402aSHarshad Shirwadkar * group
400f52f3d2bSOjaswin Mujoo * cr_power2_aligned/cr_goal_len_fast
401c9de560dSAlex Tomas */
402c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_pspace_cachep;
403c3a326a6SAneesh Kumar K.V static struct kmem_cache *ext4_ac_cachep;
40418aadd47SBobi Jam static struct kmem_cache *ext4_free_data_cachep;
405fb1813f4SCurt Wohlgemuth
406fb1813f4SCurt Wohlgemuth /* We create slab caches for groupinfo data structures based on the
407fb1813f4SCurt Wohlgemuth * superblock block size. There will be one per mounted filesystem for
408fb1813f4SCurt Wohlgemuth * each unique s_blocksize_bits */
4092892c15dSEric Sandeen #define NR_GRPINFO_CACHES 8
410fb1813f4SCurt Wohlgemuth static struct kmem_cache *ext4_groupinfo_caches[NR_GRPINFO_CACHES];
411fb1813f4SCurt Wohlgemuth
412d6006186SEric Biggers static const char * const ext4_groupinfo_slab_names[NR_GRPINFO_CACHES] = {
4132892c15dSEric Sandeen "ext4_groupinfo_1k", "ext4_groupinfo_2k", "ext4_groupinfo_4k",
4142892c15dSEric Sandeen "ext4_groupinfo_8k", "ext4_groupinfo_16k", "ext4_groupinfo_32k",
4152892c15dSEric Sandeen "ext4_groupinfo_64k", "ext4_groupinfo_128k"
4162892c15dSEric Sandeen };
4172892c15dSEric Sandeen
418c3a326a6SAneesh Kumar K.V static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
419c3a326a6SAneesh Kumar K.V ext4_group_t group);
42053f86b17SRitesh Harjani static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac);
421c3a326a6SAneesh Kumar K.V
422196e402aSHarshad Shirwadkar static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
4234eb7a4a1SOjaswin Mujoo ext4_group_t group, enum criteria cr);
424196e402aSHarshad Shirwadkar
42555cdd0afSWang Jianchao static int ext4_try_to_trim_range(struct super_block *sb,
42655cdd0afSWang Jianchao struct ext4_buddy *e4b, ext4_grpblk_t start,
42755cdd0afSWang Jianchao ext4_grpblk_t max, ext4_grpblk_t minblocks);
42855cdd0afSWang Jianchao
42907b5b8e1SRitesh Harjani /*
43007b5b8e1SRitesh Harjani * The algorithm using this percpu seq counter goes below:
43107b5b8e1SRitesh Harjani * 1. We sample the percpu discard_pa_seq counter before trying for block
43207b5b8e1SRitesh Harjani * allocation in ext4_mb_new_blocks().
43307b5b8e1SRitesh Harjani * 2. We increment this percpu discard_pa_seq counter when we either allocate
43407b5b8e1SRitesh Harjani * or free these blocks i.e. while marking those blocks as used/free in
43507b5b8e1SRitesh Harjani * mb_mark_used()/mb_free_blocks().
43607b5b8e1SRitesh Harjani * 3. We also increment this percpu seq counter when we successfully identify
43707b5b8e1SRitesh Harjani * that the bb_prealloc_list is not empty and hence proceed for discarding
43807b5b8e1SRitesh Harjani * of those PAs inside ext4_mb_discard_group_preallocations().
43907b5b8e1SRitesh Harjani *
44007b5b8e1SRitesh Harjani * Now to make sure that the regular fast path of block allocation is not
44107b5b8e1SRitesh Harjani * affected, as a small optimization we only sample the percpu seq counter
44207b5b8e1SRitesh Harjani * on that cpu. Only when the block allocation fails and when freed blocks
44307b5b8e1SRitesh Harjani * found were 0, that is when we sample percpu seq counter for all cpus using
44407b5b8e1SRitesh Harjani * below function ext4_get_discard_pa_seq_sum(). This happens after making
44507b5b8e1SRitesh Harjani * sure that all the PAs on grp->bb_prealloc_list got freed or if it's empty.
44607b5b8e1SRitesh Harjani */
44707b5b8e1SRitesh Harjani static DEFINE_PER_CPU(u64, discard_pa_seq);
ext4_get_discard_pa_seq_sum(void)44807b5b8e1SRitesh Harjani static inline u64 ext4_get_discard_pa_seq_sum(void)
44907b5b8e1SRitesh Harjani {
45007b5b8e1SRitesh Harjani int __cpu;
45107b5b8e1SRitesh Harjani u64 __seq = 0;
45207b5b8e1SRitesh Harjani
45307b5b8e1SRitesh Harjani for_each_possible_cpu(__cpu)
45407b5b8e1SRitesh Harjani __seq += per_cpu(discard_pa_seq, __cpu);
45507b5b8e1SRitesh Harjani return __seq;
45607b5b8e1SRitesh Harjani }
45707b5b8e1SRitesh Harjani
mb_correct_addr_and_bit(int * bit,void * addr)458ffad0a44SAneesh Kumar K.V static inline void *mb_correct_addr_and_bit(int *bit, void *addr)
459ffad0a44SAneesh Kumar K.V {
460c9de560dSAlex Tomas #if BITS_PER_LONG == 64
461ffad0a44SAneesh Kumar K.V *bit += ((unsigned long) addr & 7UL) << 3;
462ffad0a44SAneesh Kumar K.V addr = (void *) ((unsigned long) addr & ~7UL);
463c9de560dSAlex Tomas #elif BITS_PER_LONG == 32
464ffad0a44SAneesh Kumar K.V *bit += ((unsigned long) addr & 3UL) << 3;
465ffad0a44SAneesh Kumar K.V addr = (void *) ((unsigned long) addr & ~3UL);
466c9de560dSAlex Tomas #else
467c9de560dSAlex Tomas #error "how many bits you are?!"
468c9de560dSAlex Tomas #endif
469ffad0a44SAneesh Kumar K.V return addr;
470ffad0a44SAneesh Kumar K.V }
471c9de560dSAlex Tomas
mb_test_bit(int bit,void * addr)472c9de560dSAlex Tomas static inline int mb_test_bit(int bit, void *addr)
473c9de560dSAlex Tomas {
474c9de560dSAlex Tomas /*
475c9de560dSAlex Tomas * ext4_test_bit on architecture like powerpc
476c9de560dSAlex Tomas * needs unsigned long aligned address
477c9de560dSAlex Tomas */
478ffad0a44SAneesh Kumar K.V addr = mb_correct_addr_and_bit(&bit, addr);
479c9de560dSAlex Tomas return ext4_test_bit(bit, addr);
480c9de560dSAlex Tomas }
481c9de560dSAlex Tomas
mb_set_bit(int bit,void * addr)482c9de560dSAlex Tomas static inline void mb_set_bit(int bit, void *addr)
483c9de560dSAlex Tomas {
484ffad0a44SAneesh Kumar K.V addr = mb_correct_addr_and_bit(&bit, addr);
485c9de560dSAlex Tomas ext4_set_bit(bit, addr);
486c9de560dSAlex Tomas }
487c9de560dSAlex Tomas
mb_clear_bit(int bit,void * addr)488c9de560dSAlex Tomas static inline void mb_clear_bit(int bit, void *addr)
489c9de560dSAlex Tomas {
490ffad0a44SAneesh Kumar K.V addr = mb_correct_addr_and_bit(&bit, addr);
491c9de560dSAlex Tomas ext4_clear_bit(bit, addr);
492c9de560dSAlex Tomas }
493c9de560dSAlex Tomas
mb_test_and_clear_bit(int bit,void * addr)494eabe0444SAndrey Sidorov static inline int mb_test_and_clear_bit(int bit, void *addr)
495eabe0444SAndrey Sidorov {
496eabe0444SAndrey Sidorov addr = mb_correct_addr_and_bit(&bit, addr);
497eabe0444SAndrey Sidorov return ext4_test_and_clear_bit(bit, addr);
498eabe0444SAndrey Sidorov }
499eabe0444SAndrey Sidorov
mb_find_next_zero_bit(void * addr,int max,int start)500ffad0a44SAneesh Kumar K.V static inline int mb_find_next_zero_bit(void *addr, int max, int start)
501ffad0a44SAneesh Kumar K.V {
502e7dfb246SAneesh Kumar K.V int fix = 0, ret, tmpmax;
503ffad0a44SAneesh Kumar K.V addr = mb_correct_addr_and_bit(&fix, addr);
504e7dfb246SAneesh Kumar K.V tmpmax = max + fix;
505ffad0a44SAneesh Kumar K.V start += fix;
506ffad0a44SAneesh Kumar K.V
507e7dfb246SAneesh Kumar K.V ret = ext4_find_next_zero_bit(addr, tmpmax, start) - fix;
508e7dfb246SAneesh Kumar K.V if (ret > max)
509e7dfb246SAneesh Kumar K.V return max;
510e7dfb246SAneesh Kumar K.V return ret;
511ffad0a44SAneesh Kumar K.V }
512ffad0a44SAneesh Kumar K.V
mb_find_next_bit(void * addr,int max,int start)513ffad0a44SAneesh Kumar K.V static inline int mb_find_next_bit(void *addr, int max, int start)
514ffad0a44SAneesh Kumar K.V {
515e7dfb246SAneesh Kumar K.V int fix = 0, ret, tmpmax;
516ffad0a44SAneesh Kumar K.V addr = mb_correct_addr_and_bit(&fix, addr);
517e7dfb246SAneesh Kumar K.V tmpmax = max + fix;
518ffad0a44SAneesh Kumar K.V start += fix;
519ffad0a44SAneesh Kumar K.V
520e7dfb246SAneesh Kumar K.V ret = ext4_find_next_bit(addr, tmpmax, start) - fix;
521e7dfb246SAneesh Kumar K.V if (ret > max)
522e7dfb246SAneesh Kumar K.V return max;
523e7dfb246SAneesh Kumar K.V return ret;
524ffad0a44SAneesh Kumar K.V }
525ffad0a44SAneesh Kumar K.V
mb_find_buddy(struct ext4_buddy * e4b,int order,int * max)526c9de560dSAlex Tomas static void *mb_find_buddy(struct ext4_buddy *e4b, int order, int *max)
527c9de560dSAlex Tomas {
528c9de560dSAlex Tomas char *bb;
529c9de560dSAlex Tomas
530c5e8f3f3STheodore Ts'o BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
531c9de560dSAlex Tomas BUG_ON(max == NULL);
532c9de560dSAlex Tomas
533c9de560dSAlex Tomas if (order > e4b->bd_blkbits + 1) {
534c9de560dSAlex Tomas *max = 0;
535c9de560dSAlex Tomas return NULL;
536c9de560dSAlex Tomas }
537c9de560dSAlex Tomas
538c9de560dSAlex Tomas /* at order 0 we see each particular block */
53984b775a3SColy Li if (order == 0) {
540c9de560dSAlex Tomas *max = 1 << (e4b->bd_blkbits + 3);
541c5e8f3f3STheodore Ts'o return e4b->bd_bitmap;
54284b775a3SColy Li }
543c9de560dSAlex Tomas
544c5e8f3f3STheodore Ts'o bb = e4b->bd_buddy + EXT4_SB(e4b->bd_sb)->s_mb_offsets[order];
545c9de560dSAlex Tomas *max = EXT4_SB(e4b->bd_sb)->s_mb_maxs[order];
546c9de560dSAlex Tomas
547c9de560dSAlex Tomas return bb;
548c9de560dSAlex Tomas }
549c9de560dSAlex Tomas
550c9de560dSAlex Tomas #ifdef DOUBLE_CHECK
mb_free_blocks_double(struct inode * inode,struct ext4_buddy * e4b,int first,int count)551c9de560dSAlex Tomas static void mb_free_blocks_double(struct inode *inode, struct ext4_buddy *e4b,
552c9de560dSAlex Tomas int first, int count)
553c9de560dSAlex Tomas {
554c9de560dSAlex Tomas int i;
555c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
556c9de560dSAlex Tomas
557c9de560dSAlex Tomas if (unlikely(e4b->bd_info->bb_bitmap == NULL))
558c9de560dSAlex Tomas return;
559bc8e6740SVincent Minet assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
560c9de560dSAlex Tomas for (i = 0; i < count; i++) {
561c9de560dSAlex Tomas if (!mb_test_bit(first + i, e4b->bd_info->bb_bitmap)) {
562c9de560dSAlex Tomas ext4_fsblk_t blocknr;
5635661bd68SAkinobu Mita
5645661bd68SAkinobu Mita blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
56553accfa9STheodore Ts'o blocknr += EXT4_C2B(EXT4_SB(sb), first + i);
5665d1b1b3fSAneesh Kumar K.V ext4_grp_locked_error(sb, e4b->bd_group,
567e29136f8STheodore Ts'o inode ? inode->i_ino : 0,
568e29136f8STheodore Ts'o blocknr,
569e29136f8STheodore Ts'o "freeing block already freed "
570e29136f8STheodore Ts'o "(bit %u)",
571e29136f8STheodore Ts'o first + i);
572736dedbbSWang Shilong ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
573736dedbbSWang Shilong EXT4_GROUP_INFO_BBITMAP_CORRUPT);
574c9de560dSAlex Tomas }
575c9de560dSAlex Tomas mb_clear_bit(first + i, e4b->bd_info->bb_bitmap);
576c9de560dSAlex Tomas }
577c9de560dSAlex Tomas }
578c9de560dSAlex Tomas
mb_mark_used_double(struct ext4_buddy * e4b,int first,int count)579c9de560dSAlex Tomas static void mb_mark_used_double(struct ext4_buddy *e4b, int first, int count)
580c9de560dSAlex Tomas {
581c9de560dSAlex Tomas int i;
582c9de560dSAlex Tomas
583c9de560dSAlex Tomas if (unlikely(e4b->bd_info->bb_bitmap == NULL))
584c9de560dSAlex Tomas return;
585bc8e6740SVincent Minet assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
586c9de560dSAlex Tomas for (i = 0; i < count; i++) {
587c9de560dSAlex Tomas BUG_ON(mb_test_bit(first + i, e4b->bd_info->bb_bitmap));
588c9de560dSAlex Tomas mb_set_bit(first + i, e4b->bd_info->bb_bitmap);
589c9de560dSAlex Tomas }
590c9de560dSAlex Tomas }
591c9de560dSAlex Tomas
mb_cmp_bitmaps(struct ext4_buddy * e4b,void * bitmap)592c9de560dSAlex Tomas static void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
593c9de560dSAlex Tomas {
594eb2b8ebbSRitesh Harjani if (unlikely(e4b->bd_info->bb_bitmap == NULL))
595eb2b8ebbSRitesh Harjani return;
596c9de560dSAlex Tomas if (memcmp(e4b->bd_info->bb_bitmap, bitmap, e4b->bd_sb->s_blocksize)) {
597c9de560dSAlex Tomas unsigned char *b1, *b2;
598c9de560dSAlex Tomas int i;
599c9de560dSAlex Tomas b1 = (unsigned char *) e4b->bd_info->bb_bitmap;
600c9de560dSAlex Tomas b2 = (unsigned char *) bitmap;
601c9de560dSAlex Tomas for (i = 0; i < e4b->bd_sb->s_blocksize; i++) {
602c9de560dSAlex Tomas if (b1[i] != b2[i]) {
6039d8b9ec4STheodore Ts'o ext4_msg(e4b->bd_sb, KERN_ERR,
6049d8b9ec4STheodore Ts'o "corruption in group %u "
6054776004fSTheodore Ts'o "at byte %u(%u): %x in copy != %x "
6069d8b9ec4STheodore Ts'o "on disk/prealloc",
607c9de560dSAlex Tomas e4b->bd_group, i, i * 8, b1[i], b2[i]);
608c9de560dSAlex Tomas BUG();
609c9de560dSAlex Tomas }
610c9de560dSAlex Tomas }
611c9de560dSAlex Tomas }
612c9de560dSAlex Tomas }
613c9de560dSAlex Tomas
mb_group_bb_bitmap_alloc(struct super_block * sb,struct ext4_group_info * grp,ext4_group_t group)614a3450215SRitesh Harjani static void mb_group_bb_bitmap_alloc(struct super_block *sb,
615a3450215SRitesh Harjani struct ext4_group_info *grp, ext4_group_t group)
616a3450215SRitesh Harjani {
617a3450215SRitesh Harjani struct buffer_head *bh;
618a3450215SRitesh Harjani
619a3450215SRitesh Harjani grp->bb_bitmap = kmalloc(sb->s_blocksize, GFP_NOFS);
620eb2b8ebbSRitesh Harjani if (!grp->bb_bitmap)
621eb2b8ebbSRitesh Harjani return;
622a3450215SRitesh Harjani
623a3450215SRitesh Harjani bh = ext4_read_block_bitmap(sb, group);
624eb2b8ebbSRitesh Harjani if (IS_ERR_OR_NULL(bh)) {
625eb2b8ebbSRitesh Harjani kfree(grp->bb_bitmap);
626eb2b8ebbSRitesh Harjani grp->bb_bitmap = NULL;
627eb2b8ebbSRitesh Harjani return;
628eb2b8ebbSRitesh Harjani }
629a3450215SRitesh Harjani
630a3450215SRitesh Harjani memcpy(grp->bb_bitmap, bh->b_data, sb->s_blocksize);
631a3450215SRitesh Harjani put_bh(bh);
632a3450215SRitesh Harjani }
633a3450215SRitesh Harjani
mb_group_bb_bitmap_free(struct ext4_group_info * grp)634a3450215SRitesh Harjani static void mb_group_bb_bitmap_free(struct ext4_group_info *grp)
635a3450215SRitesh Harjani {
636a3450215SRitesh Harjani kfree(grp->bb_bitmap);
637a3450215SRitesh Harjani }
638a3450215SRitesh Harjani
639c9de560dSAlex Tomas #else
mb_free_blocks_double(struct inode * inode,struct ext4_buddy * e4b,int first,int count)640c9de560dSAlex Tomas static inline void mb_free_blocks_double(struct inode *inode,
641c9de560dSAlex Tomas struct ext4_buddy *e4b, int first, int count)
642c9de560dSAlex Tomas {
643c9de560dSAlex Tomas return;
644c9de560dSAlex Tomas }
mb_mark_used_double(struct ext4_buddy * e4b,int first,int count)645c9de560dSAlex Tomas static inline void mb_mark_used_double(struct ext4_buddy *e4b,
646c9de560dSAlex Tomas int first, int count)
647c9de560dSAlex Tomas {
648c9de560dSAlex Tomas return;
649c9de560dSAlex Tomas }
mb_cmp_bitmaps(struct ext4_buddy * e4b,void * bitmap)650c9de560dSAlex Tomas static inline void mb_cmp_bitmaps(struct ext4_buddy *e4b, void *bitmap)
651c9de560dSAlex Tomas {
652c9de560dSAlex Tomas return;
653c9de560dSAlex Tomas }
654a3450215SRitesh Harjani
mb_group_bb_bitmap_alloc(struct super_block * sb,struct ext4_group_info * grp,ext4_group_t group)655a3450215SRitesh Harjani static inline void mb_group_bb_bitmap_alloc(struct super_block *sb,
656a3450215SRitesh Harjani struct ext4_group_info *grp, ext4_group_t group)
657a3450215SRitesh Harjani {
658a3450215SRitesh Harjani return;
659a3450215SRitesh Harjani }
660a3450215SRitesh Harjani
mb_group_bb_bitmap_free(struct ext4_group_info * grp)661a3450215SRitesh Harjani static inline void mb_group_bb_bitmap_free(struct ext4_group_info *grp)
662a3450215SRitesh Harjani {
663a3450215SRitesh Harjani return;
664a3450215SRitesh Harjani }
665c9de560dSAlex Tomas #endif
666c9de560dSAlex Tomas
667c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
668c9de560dSAlex Tomas
669c9de560dSAlex Tomas #define MB_CHECK_ASSERT(assert) \
670c9de560dSAlex Tomas do { \
671c9de560dSAlex Tomas if (!(assert)) { \
672c9de560dSAlex Tomas printk(KERN_EMERG \
673c9de560dSAlex Tomas "Assertion failure in %s() at %s:%d: \"%s\"\n", \
674c9de560dSAlex Tomas function, file, line, # assert); \
675c9de560dSAlex Tomas BUG(); \
676c9de560dSAlex Tomas } \
677c9de560dSAlex Tomas } while (0)
678c9de560dSAlex Tomas
__mb_check_buddy(struct ext4_buddy * e4b,char * file,const char * function,int line)679c9de560dSAlex Tomas static int __mb_check_buddy(struct ext4_buddy *e4b, char *file,
680c9de560dSAlex Tomas const char *function, int line)
681c9de560dSAlex Tomas {
682c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
683c9de560dSAlex Tomas int order = e4b->bd_blkbits + 1;
684c9de560dSAlex Tomas int max;
685c9de560dSAlex Tomas int max2;
686c9de560dSAlex Tomas int i;
687c9de560dSAlex Tomas int j;
688c9de560dSAlex Tomas int k;
689c9de560dSAlex Tomas int count;
690c9de560dSAlex Tomas struct ext4_group_info *grp;
691c9de560dSAlex Tomas int fragments = 0;
692c9de560dSAlex Tomas int fstart;
693c9de560dSAlex Tomas struct list_head *cur;
694c9de560dSAlex Tomas void *buddy;
695c9de560dSAlex Tomas void *buddy2;
696c9de560dSAlex Tomas
697addd752cSChunguang Xu if (e4b->bd_info->bb_check_counter++ % 10)
698c9de560dSAlex Tomas return 0;
699c9de560dSAlex Tomas
700c9de560dSAlex Tomas while (order > 1) {
701c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, order, &max);
702c9de560dSAlex Tomas MB_CHECK_ASSERT(buddy);
703c9de560dSAlex Tomas buddy2 = mb_find_buddy(e4b, order - 1, &max2);
704c9de560dSAlex Tomas MB_CHECK_ASSERT(buddy2);
705c9de560dSAlex Tomas MB_CHECK_ASSERT(buddy != buddy2);
706c9de560dSAlex Tomas MB_CHECK_ASSERT(max * 2 == max2);
707c9de560dSAlex Tomas
708c9de560dSAlex Tomas count = 0;
709c9de560dSAlex Tomas for (i = 0; i < max; i++) {
710c9de560dSAlex Tomas
711c9de560dSAlex Tomas if (mb_test_bit(i, buddy)) {
712af2b3275SJinke Han /* only single bit in buddy2 may be 0 */
713c9de560dSAlex Tomas if (!mb_test_bit(i << 1, buddy2)) {
714c9de560dSAlex Tomas MB_CHECK_ASSERT(
715c9de560dSAlex Tomas mb_test_bit((i<<1)+1, buddy2));
716c9de560dSAlex Tomas }
717c9de560dSAlex Tomas continue;
718c9de560dSAlex Tomas }
719c9de560dSAlex Tomas
7200a10da73SRobin Dong /* both bits in buddy2 must be 1 */
721c9de560dSAlex Tomas MB_CHECK_ASSERT(mb_test_bit(i << 1, buddy2));
722c9de560dSAlex Tomas MB_CHECK_ASSERT(mb_test_bit((i << 1) + 1, buddy2));
723c9de560dSAlex Tomas
724c9de560dSAlex Tomas for (j = 0; j < (1 << order); j++) {
725c9de560dSAlex Tomas k = (i * (1 << order)) + j;
726c9de560dSAlex Tomas MB_CHECK_ASSERT(
727c5e8f3f3STheodore Ts'o !mb_test_bit(k, e4b->bd_bitmap));
728c9de560dSAlex Tomas }
729c9de560dSAlex Tomas count++;
730c9de560dSAlex Tomas }
731c9de560dSAlex Tomas MB_CHECK_ASSERT(e4b->bd_info->bb_counters[order] == count);
732c9de560dSAlex Tomas order--;
733c9de560dSAlex Tomas }
734c9de560dSAlex Tomas
735c9de560dSAlex Tomas fstart = -1;
736c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, 0, &max);
737c9de560dSAlex Tomas for (i = 0; i < max; i++) {
738c9de560dSAlex Tomas if (!mb_test_bit(i, buddy)) {
739c9de560dSAlex Tomas MB_CHECK_ASSERT(i >= e4b->bd_info->bb_first_free);
740c9de560dSAlex Tomas if (fstart == -1) {
741c9de560dSAlex Tomas fragments++;
742c9de560dSAlex Tomas fstart = i;
743c9de560dSAlex Tomas }
744c9de560dSAlex Tomas continue;
745c9de560dSAlex Tomas }
746c9de560dSAlex Tomas fstart = -1;
747c9de560dSAlex Tomas /* check used bits only */
748c9de560dSAlex Tomas for (j = 0; j < e4b->bd_blkbits + 1; j++) {
749c9de560dSAlex Tomas buddy2 = mb_find_buddy(e4b, j, &max2);
750c9de560dSAlex Tomas k = i >> j;
751c9de560dSAlex Tomas MB_CHECK_ASSERT(k < max2);
752c9de560dSAlex Tomas MB_CHECK_ASSERT(mb_test_bit(k, buddy2));
753c9de560dSAlex Tomas }
754c9de560dSAlex Tomas }
755c9de560dSAlex Tomas MB_CHECK_ASSERT(!EXT4_MB_GRP_NEED_INIT(e4b->bd_info));
756c9de560dSAlex Tomas MB_CHECK_ASSERT(e4b->bd_info->bb_fragments == fragments);
757c9de560dSAlex Tomas
758c9de560dSAlex Tomas grp = ext4_get_group_info(sb, e4b->bd_group);
7595354b2afSTheodore Ts'o if (!grp)
7605354b2afSTheodore Ts'o return NULL;
761c9de560dSAlex Tomas list_for_each(cur, &grp->bb_prealloc_list) {
762c9de560dSAlex Tomas ext4_group_t groupnr;
763c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
76460bd63d1SSolofo Ramangalahy pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
76560bd63d1SSolofo Ramangalahy ext4_get_group_no_and_offset(sb, pa->pa_pstart, &groupnr, &k);
766c9de560dSAlex Tomas MB_CHECK_ASSERT(groupnr == e4b->bd_group);
76760bd63d1SSolofo Ramangalahy for (i = 0; i < pa->pa_len; i++)
768c9de560dSAlex Tomas MB_CHECK_ASSERT(mb_test_bit(k + i, buddy));
769c9de560dSAlex Tomas }
770c9de560dSAlex Tomas return 0;
771c9de560dSAlex Tomas }
772c9de560dSAlex Tomas #undef MB_CHECK_ASSERT
773c9de560dSAlex Tomas #define mb_check_buddy(e4b) __mb_check_buddy(e4b, \
77446e665e9SHarvey Harrison __FILE__, __func__, __LINE__)
775c9de560dSAlex Tomas #else
776c9de560dSAlex Tomas #define mb_check_buddy(e4b)
777c9de560dSAlex Tomas #endif
778c9de560dSAlex Tomas
7797c786059SColy Li /*
7807c786059SColy Li * Divide blocks started from @first with length @len into
7817c786059SColy Li * smaller chunks with power of 2 blocks.
7827c786059SColy Li * Clear the bits in bitmap which the blocks of the chunk(s) covered,
7837c786059SColy Li * then increase bb_counters[] for corresponded chunk size.
7847c786059SColy Li */
ext4_mb_mark_free_simple(struct super_block * sb,void * buddy,ext4_grpblk_t first,ext4_grpblk_t len,struct ext4_group_info * grp)785c9de560dSAlex Tomas static void ext4_mb_mark_free_simple(struct super_block *sb,
786a36b4498SEric Sandeen void *buddy, ext4_grpblk_t first, ext4_grpblk_t len,
787c9de560dSAlex Tomas struct ext4_group_info *grp)
788c9de560dSAlex Tomas {
789c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
790a36b4498SEric Sandeen ext4_grpblk_t min;
791a36b4498SEric Sandeen ext4_grpblk_t max;
792a36b4498SEric Sandeen ext4_grpblk_t chunk;
79369e43e8cSChandan Rajendra unsigned int border;
794c9de560dSAlex Tomas
7957137d7a4STheodore Ts'o BUG_ON(len > EXT4_CLUSTERS_PER_GROUP(sb));
796c9de560dSAlex Tomas
797c9de560dSAlex Tomas border = 2 << sb->s_blocksize_bits;
798c9de560dSAlex Tomas
799c9de560dSAlex Tomas while (len > 0) {
800c9de560dSAlex Tomas /* find how many blocks can be covered since this position */
801c9de560dSAlex Tomas max = ffs(first | border) - 1;
802c9de560dSAlex Tomas
803c9de560dSAlex Tomas /* find how many blocks of power 2 we need to mark */
804c9de560dSAlex Tomas min = fls(len) - 1;
805c9de560dSAlex Tomas
806c9de560dSAlex Tomas if (max < min)
807c9de560dSAlex Tomas min = max;
808c9de560dSAlex Tomas chunk = 1 << min;
809c9de560dSAlex Tomas
810c9de560dSAlex Tomas /* mark multiblock chunks only */
811c9de560dSAlex Tomas grp->bb_counters[min]++;
812c9de560dSAlex Tomas if (min > 0)
813c9de560dSAlex Tomas mb_clear_bit(first >> min,
814c9de560dSAlex Tomas buddy + sbi->s_mb_offsets[min]);
815c9de560dSAlex Tomas
816c9de560dSAlex Tomas len -= chunk;
817c9de560dSAlex Tomas first += chunk;
818c9de560dSAlex Tomas }
819c9de560dSAlex Tomas }
820c9de560dSAlex Tomas
mb_avg_fragment_size_order(struct super_block * sb,ext4_grpblk_t len)82183e80a6eSJan Kara static int mb_avg_fragment_size_order(struct super_block *sb, ext4_grpblk_t len)
822196e402aSHarshad Shirwadkar {
82383e80a6eSJan Kara int order;
824196e402aSHarshad Shirwadkar
825196e402aSHarshad Shirwadkar /*
82683e80a6eSJan Kara * We don't bother with a special lists groups with only 1 block free
82783e80a6eSJan Kara * extents and for completely empty groups.
828196e402aSHarshad Shirwadkar */
82983e80a6eSJan Kara order = fls(len) - 2;
83083e80a6eSJan Kara if (order < 0)
83183e80a6eSJan Kara return 0;
83283e80a6eSJan Kara if (order == MB_NUM_ORDERS(sb))
83383e80a6eSJan Kara order--;
834677ff458SBaokun Li if (WARN_ON_ONCE(order > MB_NUM_ORDERS(sb)))
835677ff458SBaokun Li order = MB_NUM_ORDERS(sb) - 1;
83683e80a6eSJan Kara return order;
83783e80a6eSJan Kara }
83883e80a6eSJan Kara
83983e80a6eSJan Kara /* Move group to appropriate avg_fragment_size list */
840196e402aSHarshad Shirwadkar static void
mb_update_avg_fragment_size(struct super_block * sb,struct ext4_group_info * grp)841196e402aSHarshad Shirwadkar mb_update_avg_fragment_size(struct super_block *sb, struct ext4_group_info *grp)
842196e402aSHarshad Shirwadkar {
843196e402aSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(sb);
84483e80a6eSJan Kara int new_order;
845196e402aSHarshad Shirwadkar
846f32d2a74SBaokun Li if (!test_opt2(sb, MB_OPTIMIZE_SCAN) || grp->bb_fragments == 0)
847196e402aSHarshad Shirwadkar return;
848196e402aSHarshad Shirwadkar
84983e80a6eSJan Kara new_order = mb_avg_fragment_size_order(sb,
85083e80a6eSJan Kara grp->bb_free / grp->bb_fragments);
85183e80a6eSJan Kara if (new_order == grp->bb_avg_fragment_size_order)
85283e80a6eSJan Kara return;
853196e402aSHarshad Shirwadkar
85483e80a6eSJan Kara if (grp->bb_avg_fragment_size_order != -1) {
85583e80a6eSJan Kara write_lock(&sbi->s_mb_avg_fragment_size_locks[
85683e80a6eSJan Kara grp->bb_avg_fragment_size_order]);
85783e80a6eSJan Kara list_del(&grp->bb_avg_fragment_size_node);
85883e80a6eSJan Kara write_unlock(&sbi->s_mb_avg_fragment_size_locks[
85983e80a6eSJan Kara grp->bb_avg_fragment_size_order]);
86083e80a6eSJan Kara }
86183e80a6eSJan Kara grp->bb_avg_fragment_size_order = new_order;
86283e80a6eSJan Kara write_lock(&sbi->s_mb_avg_fragment_size_locks[
86383e80a6eSJan Kara grp->bb_avg_fragment_size_order]);
86483e80a6eSJan Kara list_add_tail(&grp->bb_avg_fragment_size_node,
86583e80a6eSJan Kara &sbi->s_mb_avg_fragment_size[grp->bb_avg_fragment_size_order]);
86683e80a6eSJan Kara write_unlock(&sbi->s_mb_avg_fragment_size_locks[
86783e80a6eSJan Kara grp->bb_avg_fragment_size_order]);
868196e402aSHarshad Shirwadkar }
869196e402aSHarshad Shirwadkar
870196e402aSHarshad Shirwadkar /*
871196e402aSHarshad Shirwadkar * Choose next group by traversing largest_free_order lists. Updates *new_cr if
872196e402aSHarshad Shirwadkar * cr level needs an update.
873196e402aSHarshad Shirwadkar */
ext4_mb_choose_next_group_p2_aligned(struct ext4_allocation_context * ac,enum criteria * new_cr,ext4_group_t * group,ext4_group_t ngroups)874f52f3d2bSOjaswin Mujoo static void ext4_mb_choose_next_group_p2_aligned(struct ext4_allocation_context *ac,
8754eb7a4a1SOjaswin Mujoo enum criteria *new_cr, ext4_group_t *group, ext4_group_t ngroups)
876196e402aSHarshad Shirwadkar {
877196e402aSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
878919eb90cSKemeng Shi struct ext4_group_info *iter;
879196e402aSHarshad Shirwadkar int i;
880196e402aSHarshad Shirwadkar
881196e402aSHarshad Shirwadkar if (ac->ac_status == AC_STATUS_FOUND)
882196e402aSHarshad Shirwadkar return;
883196e402aSHarshad Shirwadkar
884f52f3d2bSOjaswin Mujoo if (unlikely(sbi->s_mb_stats && ac->ac_flags & EXT4_MB_CR_POWER2_ALIGNED_OPTIMIZED))
885f52f3d2bSOjaswin Mujoo atomic_inc(&sbi->s_bal_p2_aligned_bad_suggestions);
886196e402aSHarshad Shirwadkar
887196e402aSHarshad Shirwadkar for (i = ac->ac_2order; i < MB_NUM_ORDERS(ac->ac_sb); i++) {
888196e402aSHarshad Shirwadkar if (list_empty(&sbi->s_mb_largest_free_orders[i]))
889196e402aSHarshad Shirwadkar continue;
890196e402aSHarshad Shirwadkar read_lock(&sbi->s_mb_largest_free_orders_locks[i]);
891196e402aSHarshad Shirwadkar if (list_empty(&sbi->s_mb_largest_free_orders[i])) {
892196e402aSHarshad Shirwadkar read_unlock(&sbi->s_mb_largest_free_orders_locks[i]);
893196e402aSHarshad Shirwadkar continue;
894196e402aSHarshad Shirwadkar }
895196e402aSHarshad Shirwadkar list_for_each_entry(iter, &sbi->s_mb_largest_free_orders[i],
896196e402aSHarshad Shirwadkar bb_largest_free_order_node) {
897196e402aSHarshad Shirwadkar if (sbi->s_mb_stats)
898f52f3d2bSOjaswin Mujoo atomic64_inc(&sbi->s_bal_cX_groups_considered[CR_POWER2_ALIGNED]);
899f52f3d2bSOjaswin Mujoo if (likely(ext4_mb_good_group(ac, iter->bb_group, CR_POWER2_ALIGNED))) {
900919eb90cSKemeng Shi *group = iter->bb_group;
901919eb90cSKemeng Shi ac->ac_flags |= EXT4_MB_CR_POWER2_ALIGNED_OPTIMIZED;
902919eb90cSKemeng Shi read_unlock(&sbi->s_mb_largest_free_orders_locks[i]);
903919eb90cSKemeng Shi return;
904196e402aSHarshad Shirwadkar }
905196e402aSHarshad Shirwadkar }
906196e402aSHarshad Shirwadkar read_unlock(&sbi->s_mb_largest_free_orders_locks[i]);
907196e402aSHarshad Shirwadkar }
908196e402aSHarshad Shirwadkar
909919eb90cSKemeng Shi /* Increment cr and search again if no group is found */
910f52f3d2bSOjaswin Mujoo *new_cr = CR_GOAL_LEN_FAST;
911196e402aSHarshad Shirwadkar }
912196e402aSHarshad Shirwadkar
913196e402aSHarshad Shirwadkar /*
914856d865cSOjaswin Mujoo * Find a suitable group of given order from the average fragments list.
915856d865cSOjaswin Mujoo */
916856d865cSOjaswin Mujoo static struct ext4_group_info *
ext4_mb_find_good_group_avg_frag_lists(struct ext4_allocation_context * ac,int order)917856d865cSOjaswin Mujoo ext4_mb_find_good_group_avg_frag_lists(struct ext4_allocation_context *ac, int order)
918856d865cSOjaswin Mujoo {
919856d865cSOjaswin Mujoo struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
920856d865cSOjaswin Mujoo struct list_head *frag_list = &sbi->s_mb_avg_fragment_size[order];
921856d865cSOjaswin Mujoo rwlock_t *frag_list_lock = &sbi->s_mb_avg_fragment_size_locks[order];
922856d865cSOjaswin Mujoo struct ext4_group_info *grp = NULL, *iter;
923856d865cSOjaswin Mujoo enum criteria cr = ac->ac_criteria;
924856d865cSOjaswin Mujoo
925856d865cSOjaswin Mujoo if (list_empty(frag_list))
926856d865cSOjaswin Mujoo return NULL;
927856d865cSOjaswin Mujoo read_lock(frag_list_lock);
928856d865cSOjaswin Mujoo if (list_empty(frag_list)) {
929856d865cSOjaswin Mujoo read_unlock(frag_list_lock);
930856d865cSOjaswin Mujoo return NULL;
931856d865cSOjaswin Mujoo }
932856d865cSOjaswin Mujoo list_for_each_entry(iter, frag_list, bb_avg_fragment_size_node) {
933856d865cSOjaswin Mujoo if (sbi->s_mb_stats)
934856d865cSOjaswin Mujoo atomic64_inc(&sbi->s_bal_cX_groups_considered[cr]);
935856d865cSOjaswin Mujoo if (likely(ext4_mb_good_group(ac, iter->bb_group, cr))) {
936856d865cSOjaswin Mujoo grp = iter;
937856d865cSOjaswin Mujoo break;
938856d865cSOjaswin Mujoo }
939856d865cSOjaswin Mujoo }
940856d865cSOjaswin Mujoo read_unlock(frag_list_lock);
941856d865cSOjaswin Mujoo return grp;
942856d865cSOjaswin Mujoo }
943856d865cSOjaswin Mujoo
944856d865cSOjaswin Mujoo /*
94583e80a6eSJan Kara * Choose next group by traversing average fragment size list of suitable
94683e80a6eSJan Kara * order. Updates *new_cr if cr level needs an update.
947196e402aSHarshad Shirwadkar */
ext4_mb_choose_next_group_goal_fast(struct ext4_allocation_context * ac,enum criteria * new_cr,ext4_group_t * group,ext4_group_t ngroups)948f52f3d2bSOjaswin Mujoo static void ext4_mb_choose_next_group_goal_fast(struct ext4_allocation_context *ac,
9494eb7a4a1SOjaswin Mujoo enum criteria *new_cr, ext4_group_t *group, ext4_group_t ngroups)
950196e402aSHarshad Shirwadkar {
951196e402aSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
952856d865cSOjaswin Mujoo struct ext4_group_info *grp = NULL;
95383e80a6eSJan Kara int i;
954196e402aSHarshad Shirwadkar
955f52f3d2bSOjaswin Mujoo if (unlikely(ac->ac_flags & EXT4_MB_CR_GOAL_LEN_FAST_OPTIMIZED)) {
956196e402aSHarshad Shirwadkar if (sbi->s_mb_stats)
957f52f3d2bSOjaswin Mujoo atomic_inc(&sbi->s_bal_goal_fast_bad_suggestions);
95883e80a6eSJan Kara }
95983e80a6eSJan Kara
96083e80a6eSJan Kara for (i = mb_avg_fragment_size_order(ac->ac_sb, ac->ac_g_ex.fe_len);
96183e80a6eSJan Kara i < MB_NUM_ORDERS(ac->ac_sb); i++) {
962856d865cSOjaswin Mujoo grp = ext4_mb_find_good_group_avg_frag_lists(ac, i);
96383e80a6eSJan Kara if (grp) {
964196e402aSHarshad Shirwadkar *group = grp->bb_group;
965f52f3d2bSOjaswin Mujoo ac->ac_flags |= EXT4_MB_CR_GOAL_LEN_FAST_OPTIMIZED;
966b50675a4SKemeng Shi return;
9677e170922SOjaswin Mujoo }
9687e170922SOjaswin Mujoo }
9697e170922SOjaswin Mujoo
970772c9f69SRitesh Harjani /*
971772c9f69SRitesh Harjani * CR_BEST_AVAIL_LEN works based on the concept that we have
972772c9f69SRitesh Harjani * a larger normalized goal len request which can be trimmed to
973772c9f69SRitesh Harjani * a smaller goal len such that it can still satisfy original
974772c9f69SRitesh Harjani * request len. However, allocation request for non-regular
975772c9f69SRitesh Harjani * files never gets normalized.
976772c9f69SRitesh Harjani * See function ext4_mb_normalize_request() (EXT4_MB_HINT_DATA).
977772c9f69SRitesh Harjani */
978772c9f69SRitesh Harjani if (ac->ac_flags & EXT4_MB_HINT_DATA)
979b50675a4SKemeng Shi *new_cr = CR_BEST_AVAIL_LEN;
980772c9f69SRitesh Harjani else
981772c9f69SRitesh Harjani *new_cr = CR_GOAL_LEN_SLOW;
982b50675a4SKemeng Shi }
983b50675a4SKemeng Shi
9847e170922SOjaswin Mujoo /*
985f52f3d2bSOjaswin Mujoo * We couldn't find a group in CR_GOAL_LEN_FAST so try to find the highest free fragment
9867e170922SOjaswin Mujoo * order we have and proactively trim the goal request length to that order to
9877e170922SOjaswin Mujoo * find a suitable group faster.
9887e170922SOjaswin Mujoo *
9897e170922SOjaswin Mujoo * This optimizes allocation speed at the cost of slightly reduced
9907e170922SOjaswin Mujoo * preallocations. However, we make sure that we don't trim the request too
991f52f3d2bSOjaswin Mujoo * much and fall to CR_GOAL_LEN_SLOW in that case.
9927e170922SOjaswin Mujoo */
ext4_mb_choose_next_group_best_avail(struct ext4_allocation_context * ac,enum criteria * new_cr,ext4_group_t * group,ext4_group_t ngroups)993f52f3d2bSOjaswin Mujoo static void ext4_mb_choose_next_group_best_avail(struct ext4_allocation_context *ac,
9947e170922SOjaswin Mujoo enum criteria *new_cr, ext4_group_t *group, ext4_group_t ngroups)
9957e170922SOjaswin Mujoo {
9967e170922SOjaswin Mujoo struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
9977e170922SOjaswin Mujoo struct ext4_group_info *grp = NULL;
9987e170922SOjaswin Mujoo int i, order, min_order;
9997e170922SOjaswin Mujoo unsigned long num_stripe_clusters = 0;
10007e170922SOjaswin Mujoo
1001f52f3d2bSOjaswin Mujoo if (unlikely(ac->ac_flags & EXT4_MB_CR_BEST_AVAIL_LEN_OPTIMIZED)) {
10027e170922SOjaswin Mujoo if (sbi->s_mb_stats)
1003f52f3d2bSOjaswin Mujoo atomic_inc(&sbi->s_bal_best_avail_bad_suggestions);
10047e170922SOjaswin Mujoo }
10057e170922SOjaswin Mujoo
10067e170922SOjaswin Mujoo /*
10077e170922SOjaswin Mujoo * mb_avg_fragment_size_order() returns order in a way that makes
10087e170922SOjaswin Mujoo * retrieving back the length using (1 << order) inaccurate. Hence, use
10097e170922SOjaswin Mujoo * fls() instead since we need to know the actual length while modifying
10107e170922SOjaswin Mujoo * goal length.
10117e170922SOjaswin Mujoo */
10125d5460faSOjaswin Mujoo order = fls(ac->ac_g_ex.fe_len) - 1;
1013677ff458SBaokun Li if (WARN_ON_ONCE(order - 1 > MB_NUM_ORDERS(ac->ac_sb)))
1014677ff458SBaokun Li order = MB_NUM_ORDERS(ac->ac_sb);
1015f52f3d2bSOjaswin Mujoo min_order = order - sbi->s_mb_best_avail_max_trim_order;
10167e170922SOjaswin Mujoo if (min_order < 0)
10177e170922SOjaswin Mujoo min_order = 0;
10187e170922SOjaswin Mujoo
10197e170922SOjaswin Mujoo if (sbi->s_stripe > 0) {
10207e170922SOjaswin Mujoo /*
10217e170922SOjaswin Mujoo * We are assuming that stripe size is always a multiple of
10227e170922SOjaswin Mujoo * cluster ratio otherwise __ext4_fill_super exists early.
10237e170922SOjaswin Mujoo */
10247e170922SOjaswin Mujoo num_stripe_clusters = EXT4_NUM_B2C(sbi, sbi->s_stripe);
10257e170922SOjaswin Mujoo if (1 << min_order < num_stripe_clusters)
10265d5460faSOjaswin Mujoo /*
10275d5460faSOjaswin Mujoo * We consider 1 order less because later we round
10285d5460faSOjaswin Mujoo * up the goal len to num_stripe_clusters
10295d5460faSOjaswin Mujoo */
10305d5460faSOjaswin Mujoo min_order = fls(num_stripe_clusters) - 1;
10317e170922SOjaswin Mujoo }
10327e170922SOjaswin Mujoo
10335d5460faSOjaswin Mujoo if (1 << min_order < ac->ac_o_ex.fe_len)
10345d5460faSOjaswin Mujoo min_order = fls(ac->ac_o_ex.fe_len);
10355d5460faSOjaswin Mujoo
10367e170922SOjaswin Mujoo for (i = order; i >= min_order; i--) {
10377e170922SOjaswin Mujoo int frag_order;
10387e170922SOjaswin Mujoo /*
10397e170922SOjaswin Mujoo * Scale down goal len to make sure we find something
10407e170922SOjaswin Mujoo * in the free fragments list. Basically, reduce
10417e170922SOjaswin Mujoo * preallocations.
10427e170922SOjaswin Mujoo */
10437e170922SOjaswin Mujoo ac->ac_g_ex.fe_len = 1 << i;
10447e170922SOjaswin Mujoo
10457e170922SOjaswin Mujoo if (num_stripe_clusters > 0) {
10467e170922SOjaswin Mujoo /*
10474c0cfebdSTheodore Ts'o * Try to round up the adjusted goal length to
10484c0cfebdSTheodore Ts'o * stripe size (in cluster units) multiple for
10494c0cfebdSTheodore Ts'o * efficiency.
10507e170922SOjaswin Mujoo */
10517e170922SOjaswin Mujoo ac->ac_g_ex.fe_len = roundup(ac->ac_g_ex.fe_len,
10527e170922SOjaswin Mujoo num_stripe_clusters);
10537e170922SOjaswin Mujoo }
10547e170922SOjaswin Mujoo
10557e170922SOjaswin Mujoo frag_order = mb_avg_fragment_size_order(ac->ac_sb,
10567e170922SOjaswin Mujoo ac->ac_g_ex.fe_len);
10577e170922SOjaswin Mujoo
10587e170922SOjaswin Mujoo grp = ext4_mb_find_good_group_avg_frag_lists(ac, frag_order);
10597e170922SOjaswin Mujoo if (grp) {
10607e170922SOjaswin Mujoo *group = grp->bb_group;
1061f52f3d2bSOjaswin Mujoo ac->ac_flags |= EXT4_MB_CR_BEST_AVAIL_LEN_OPTIMIZED;
1062bcb123acSKemeng Shi return;
1063bcb123acSKemeng Shi }
1064bcb123acSKemeng Shi }
1065bcb123acSKemeng Shi
1066f52f3d2bSOjaswin Mujoo /* Reset goal length to original goal length before falling into CR_GOAL_LEN_SLOW */
10677e170922SOjaswin Mujoo ac->ac_g_ex.fe_len = ac->ac_orig_goal_len;
1068f52f3d2bSOjaswin Mujoo *new_cr = CR_GOAL_LEN_SLOW;
1069196e402aSHarshad Shirwadkar }
1070196e402aSHarshad Shirwadkar
should_optimize_scan(struct ext4_allocation_context * ac)1071196e402aSHarshad Shirwadkar static inline int should_optimize_scan(struct ext4_allocation_context *ac)
1072196e402aSHarshad Shirwadkar {
1073196e402aSHarshad Shirwadkar if (unlikely(!test_opt2(ac->ac_sb, MB_OPTIMIZE_SCAN)))
1074196e402aSHarshad Shirwadkar return 0;
1075f52f3d2bSOjaswin Mujoo if (ac->ac_criteria >= CR_GOAL_LEN_SLOW)
1076196e402aSHarshad Shirwadkar return 0;
1077077d0c2cSOjaswin Mujoo if (!ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS))
1078196e402aSHarshad Shirwadkar return 0;
1079196e402aSHarshad Shirwadkar return 1;
1080196e402aSHarshad Shirwadkar }
1081196e402aSHarshad Shirwadkar
1082196e402aSHarshad Shirwadkar /*
1083196e402aSHarshad Shirwadkar * Return next linear group for allocation. If linear traversal should not be
1084196e402aSHarshad Shirwadkar * performed, this function just returns the same group
1085196e402aSHarshad Shirwadkar */
108660c672b7SKemeng Shi static ext4_group_t
next_linear_group(struct ext4_allocation_context * ac,ext4_group_t group,ext4_group_t ngroups)108760c672b7SKemeng Shi next_linear_group(struct ext4_allocation_context *ac, ext4_group_t group,
108860c672b7SKemeng Shi ext4_group_t ngroups)
1089196e402aSHarshad Shirwadkar {
1090196e402aSHarshad Shirwadkar if (!should_optimize_scan(ac))
1091196e402aSHarshad Shirwadkar goto inc_and_return;
1092196e402aSHarshad Shirwadkar
1093196e402aSHarshad Shirwadkar if (ac->ac_groups_linear_remaining) {
1094196e402aSHarshad Shirwadkar ac->ac_groups_linear_remaining--;
1095196e402aSHarshad Shirwadkar goto inc_and_return;
1096196e402aSHarshad Shirwadkar }
1097196e402aSHarshad Shirwadkar
1098196e402aSHarshad Shirwadkar return group;
1099196e402aSHarshad Shirwadkar inc_and_return:
1100196e402aSHarshad Shirwadkar /*
1101196e402aSHarshad Shirwadkar * Artificially restricted ngroups for non-extent
1102196e402aSHarshad Shirwadkar * files makes group > ngroups possible on first loop.
1103196e402aSHarshad Shirwadkar */
1104196e402aSHarshad Shirwadkar return group + 1 >= ngroups ? 0 : group + 1;
1105196e402aSHarshad Shirwadkar }
1106196e402aSHarshad Shirwadkar
1107196e402aSHarshad Shirwadkar /*
1108196e402aSHarshad Shirwadkar * ext4_mb_choose_next_group: choose next group for allocation.
1109196e402aSHarshad Shirwadkar *
1110196e402aSHarshad Shirwadkar * @ac Allocation Context
1111196e402aSHarshad Shirwadkar * @new_cr This is an output parameter. If the there is no good group
1112196e402aSHarshad Shirwadkar * available at current CR level, this field is updated to indicate
1113196e402aSHarshad Shirwadkar * the new cr level that should be used.
1114196e402aSHarshad Shirwadkar * @group This is an input / output parameter. As an input it indicates the
1115196e402aSHarshad Shirwadkar * next group that the allocator intends to use for allocation. As
1116196e402aSHarshad Shirwadkar * output, this field indicates the next group that should be used as
1117196e402aSHarshad Shirwadkar * determined by the optimization functions.
1118196e402aSHarshad Shirwadkar * @ngroups Total number of groups
1119196e402aSHarshad Shirwadkar */
ext4_mb_choose_next_group(struct ext4_allocation_context * ac,enum criteria * new_cr,ext4_group_t * group,ext4_group_t ngroups)1120196e402aSHarshad Shirwadkar static void ext4_mb_choose_next_group(struct ext4_allocation_context *ac,
11214eb7a4a1SOjaswin Mujoo enum criteria *new_cr, ext4_group_t *group, ext4_group_t ngroups)
1122196e402aSHarshad Shirwadkar {
1123196e402aSHarshad Shirwadkar *new_cr = ac->ac_criteria;
1124196e402aSHarshad Shirwadkar
11254fca50d4SJan Kara if (!should_optimize_scan(ac) || ac->ac_groups_linear_remaining) {
11264fca50d4SJan Kara *group = next_linear_group(ac, *group, ngroups);
1127196e402aSHarshad Shirwadkar return;
11284fca50d4SJan Kara }
1129196e402aSHarshad Shirwadkar
1130f52f3d2bSOjaswin Mujoo if (*new_cr == CR_POWER2_ALIGNED) {
1131f52f3d2bSOjaswin Mujoo ext4_mb_choose_next_group_p2_aligned(ac, new_cr, group, ngroups);
1132f52f3d2bSOjaswin Mujoo } else if (*new_cr == CR_GOAL_LEN_FAST) {
1133f52f3d2bSOjaswin Mujoo ext4_mb_choose_next_group_goal_fast(ac, new_cr, group, ngroups);
1134f52f3d2bSOjaswin Mujoo } else if (*new_cr == CR_BEST_AVAIL_LEN) {
1135f52f3d2bSOjaswin Mujoo ext4_mb_choose_next_group_best_avail(ac, new_cr, group, ngroups);
1136196e402aSHarshad Shirwadkar } else {
1137196e402aSHarshad Shirwadkar /*
1138196e402aSHarshad Shirwadkar * TODO: For CR=2, we can arrange groups in an rb tree sorted by
1139196e402aSHarshad Shirwadkar * bb_free. But until that happens, we should never come here.
1140196e402aSHarshad Shirwadkar */
1141196e402aSHarshad Shirwadkar WARN_ON(1);
1142196e402aSHarshad Shirwadkar }
1143196e402aSHarshad Shirwadkar }
1144196e402aSHarshad Shirwadkar
11458a57d9d6SCurt Wohlgemuth /*
11468a57d9d6SCurt Wohlgemuth * Cache the order of the largest free extent we have available in this block
11478a57d9d6SCurt Wohlgemuth * group.
11488a57d9d6SCurt Wohlgemuth */
11498a57d9d6SCurt Wohlgemuth static void
mb_set_largest_free_order(struct super_block * sb,struct ext4_group_info * grp)11508a57d9d6SCurt Wohlgemuth mb_set_largest_free_order(struct super_block *sb, struct ext4_group_info *grp)
11518a57d9d6SCurt Wohlgemuth {
1152196e402aSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(sb);
11538a57d9d6SCurt Wohlgemuth int i;
11548a57d9d6SCurt Wohlgemuth
11551940265eSJan Kara for (i = MB_NUM_ORDERS(sb) - 1; i >= 0; i--)
11561940265eSJan Kara if (grp->bb_counters[i] > 0)
11571940265eSJan Kara break;
11581940265eSJan Kara /* No need to move between order lists? */
11591940265eSJan Kara if (!test_opt2(sb, MB_OPTIMIZE_SCAN) ||
11601940265eSJan Kara i == grp->bb_largest_free_order) {
11611940265eSJan Kara grp->bb_largest_free_order = i;
11621940265eSJan Kara return;
11631940265eSJan Kara }
11641940265eSJan Kara
11651940265eSJan Kara if (grp->bb_largest_free_order >= 0) {
1166196e402aSHarshad Shirwadkar write_lock(&sbi->s_mb_largest_free_orders_locks[
1167196e402aSHarshad Shirwadkar grp->bb_largest_free_order]);
1168196e402aSHarshad Shirwadkar list_del_init(&grp->bb_largest_free_order_node);
1169196e402aSHarshad Shirwadkar write_unlock(&sbi->s_mb_largest_free_orders_locks[
1170196e402aSHarshad Shirwadkar grp->bb_largest_free_order]);
1171196e402aSHarshad Shirwadkar }
11728a57d9d6SCurt Wohlgemuth grp->bb_largest_free_order = i;
11731940265eSJan Kara if (grp->bb_largest_free_order >= 0 && grp->bb_free) {
1174196e402aSHarshad Shirwadkar write_lock(&sbi->s_mb_largest_free_orders_locks[
1175196e402aSHarshad Shirwadkar grp->bb_largest_free_order]);
1176196e402aSHarshad Shirwadkar list_add_tail(&grp->bb_largest_free_order_node,
1177196e402aSHarshad Shirwadkar &sbi->s_mb_largest_free_orders[grp->bb_largest_free_order]);
1178196e402aSHarshad Shirwadkar write_unlock(&sbi->s_mb_largest_free_orders_locks[
1179196e402aSHarshad Shirwadkar grp->bb_largest_free_order]);
1180196e402aSHarshad Shirwadkar }
11818a57d9d6SCurt Wohlgemuth }
11828a57d9d6SCurt Wohlgemuth
1183089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_generate_buddy(struct super_block * sb,void * buddy,void * bitmap,ext4_group_t group,struct ext4_group_info * grp)1184089ceeccSEric Sandeen void ext4_mb_generate_buddy(struct super_block *sb,
11855354b2afSTheodore Ts'o void *buddy, void *bitmap, ext4_group_t group,
11865354b2afSTheodore Ts'o struct ext4_group_info *grp)
1187c9de560dSAlex Tomas {
1188e43bb4e6SNamjae Jeon struct ext4_sb_info *sbi = EXT4_SB(sb);
11897137d7a4STheodore Ts'o ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
1190a36b4498SEric Sandeen ext4_grpblk_t i = 0;
1191a36b4498SEric Sandeen ext4_grpblk_t first;
1192a36b4498SEric Sandeen ext4_grpblk_t len;
1193c9de560dSAlex Tomas unsigned free = 0;
1194c9de560dSAlex Tomas unsigned fragments = 0;
1195c9de560dSAlex Tomas unsigned long long period = get_cycles();
1196c9de560dSAlex Tomas
1197c9de560dSAlex Tomas /* initialize buddy from bitmap which is aggregation
1198c9de560dSAlex Tomas * of on-disk bitmap and preallocations */
1199ffad0a44SAneesh Kumar K.V i = mb_find_next_zero_bit(bitmap, max, 0);
1200c9de560dSAlex Tomas grp->bb_first_free = i;
1201c9de560dSAlex Tomas while (i < max) {
1202c9de560dSAlex Tomas fragments++;
1203c9de560dSAlex Tomas first = i;
1204ffad0a44SAneesh Kumar K.V i = mb_find_next_bit(bitmap, max, i);
1205c9de560dSAlex Tomas len = i - first;
1206c9de560dSAlex Tomas free += len;
1207c9de560dSAlex Tomas if (len > 1)
1208c9de560dSAlex Tomas ext4_mb_mark_free_simple(sb, buddy, first, len, grp);
1209c9de560dSAlex Tomas else
1210c9de560dSAlex Tomas grp->bb_counters[0]++;
1211c9de560dSAlex Tomas if (i < max)
1212ffad0a44SAneesh Kumar K.V i = mb_find_next_zero_bit(bitmap, max, i);
1213c9de560dSAlex Tomas }
1214c9de560dSAlex Tomas grp->bb_fragments = fragments;
1215c9de560dSAlex Tomas
1216c9de560dSAlex Tomas if (free != grp->bb_free) {
1217e29136f8STheodore Ts'o ext4_grp_locked_error(sb, group, 0, 0,
121894d4c066STheodore Ts'o "block bitmap and bg descriptor "
121994d4c066STheodore Ts'o "inconsistent: %u vs %u free clusters",
1220e29136f8STheodore Ts'o free, grp->bb_free);
1221e56eb659SAneesh Kumar K.V /*
1222163a203dSDarrick J. Wong * If we intend to continue, we consider group descriptor
1223e56eb659SAneesh Kumar K.V * corrupt and update bb_free using bitmap value
1224e56eb659SAneesh Kumar K.V */
1225c9de560dSAlex Tomas grp->bb_free = free;
1226db79e6d1SWang Shilong ext4_mark_group_bitmap_corrupted(sb, group,
1227db79e6d1SWang Shilong EXT4_GROUP_INFO_BBITMAP_CORRUPT);
1228c9de560dSAlex Tomas }
12298a57d9d6SCurt Wohlgemuth mb_set_largest_free_order(sb, grp);
123083e80a6eSJan Kara mb_update_avg_fragment_size(sb, grp);
1231c9de560dSAlex Tomas
1232c9de560dSAlex Tomas clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
1233c9de560dSAlex Tomas
1234c9de560dSAlex Tomas period = get_cycles() - period;
123567d25186SHarshad Shirwadkar atomic_inc(&sbi->s_mb_buddies_generated);
123667d25186SHarshad Shirwadkar atomic64_add(period, &sbi->s_mb_generation_time);
1237c9de560dSAlex Tomas }
1238c9de560dSAlex Tomas
mb_regenerate_buddy(struct ext4_buddy * e4b)1239ea42d6cfSBaokun Li static void mb_regenerate_buddy(struct ext4_buddy *e4b)
1240ea42d6cfSBaokun Li {
1241ea42d6cfSBaokun Li int count;
1242ea42d6cfSBaokun Li int order = 1;
1243ea42d6cfSBaokun Li void *buddy;
1244ea42d6cfSBaokun Li
1245ea42d6cfSBaokun Li while ((buddy = mb_find_buddy(e4b, order++, &count)))
1246ea42d6cfSBaokun Li mb_set_bits(buddy, 0, count);
1247ea42d6cfSBaokun Li
1248ea42d6cfSBaokun Li e4b->bd_info->bb_fragments = 0;
1249ea42d6cfSBaokun Li memset(e4b->bd_info->bb_counters, 0,
1250ea42d6cfSBaokun Li sizeof(*e4b->bd_info->bb_counters) *
1251ea42d6cfSBaokun Li (e4b->bd_sb->s_blocksize_bits + 2));
1252ea42d6cfSBaokun Li
1253ea42d6cfSBaokun Li ext4_mb_generate_buddy(e4b->bd_sb, e4b->bd_buddy,
1254ea42d6cfSBaokun Li e4b->bd_bitmap, e4b->bd_group, e4b->bd_info);
1255ea42d6cfSBaokun Li }
1256ea42d6cfSBaokun Li
1257c9de560dSAlex Tomas /* The buddy information is attached the buddy cache inode
1258c9de560dSAlex Tomas * for convenience. The information regarding each group
1259c9de560dSAlex Tomas * is loaded via ext4_mb_load_buddy. The information involve
1260c9de560dSAlex Tomas * block bitmap and buddy information. The information are
1261c9de560dSAlex Tomas * stored in the inode as
1262c9de560dSAlex Tomas *
1263c9de560dSAlex Tomas * { page }
1264c3a326a6SAneesh Kumar K.V * [ group 0 bitmap][ group 0 buddy] [group 1][ group 1]...
1265c9de560dSAlex Tomas *
1266c9de560dSAlex Tomas *
1267c9de560dSAlex Tomas * one block each for bitmap and buddy information.
1268c9de560dSAlex Tomas * So for each group we take up 2 blocks. A page can
1269ea1754a0SKirill A. Shutemov * contain blocks_per_page (PAGE_SIZE / blocksize) blocks.
1270c9de560dSAlex Tomas * So it can have information regarding groups_per_page which
1271c9de560dSAlex Tomas * is blocks_per_page/2
12728a57d9d6SCurt Wohlgemuth *
12738a57d9d6SCurt Wohlgemuth * Locking note: This routine takes the block group lock of all groups
12748a57d9d6SCurt Wohlgemuth * for this page; do not hold this lock when calling this routine!
1275c9de560dSAlex Tomas */
1276c9de560dSAlex Tomas
ext4_mb_init_cache(struct page * page,char * incore,gfp_t gfp)1277adb7ef60SKonstantin Khlebnikov static int ext4_mb_init_cache(struct page *page, char *incore, gfp_t gfp)
1278c9de560dSAlex Tomas {
12798df9675fSTheodore Ts'o ext4_group_t ngroups;
128089cadf6eSLu Hongfei unsigned int blocksize;
1281c9de560dSAlex Tomas int blocks_per_page;
1282c9de560dSAlex Tomas int groups_per_page;
1283c9de560dSAlex Tomas int err = 0;
1284c9de560dSAlex Tomas int i;
1285813e5727STheodore Ts'o ext4_group_t first_group, group;
1286c9de560dSAlex Tomas int first_block;
1287c9de560dSAlex Tomas struct super_block *sb;
1288c9de560dSAlex Tomas struct buffer_head *bhs;
1289fa77dcfaSDarrick J. Wong struct buffer_head **bh = NULL;
1290c9de560dSAlex Tomas struct inode *inode;
1291c9de560dSAlex Tomas char *data;
1292c9de560dSAlex Tomas char *bitmap;
12939b8b7d35SAmir Goldstein struct ext4_group_info *grinfo;
1294c9de560dSAlex Tomas
1295c9de560dSAlex Tomas inode = page->mapping->host;
1296c9de560dSAlex Tomas sb = inode->i_sb;
12978df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb);
129893407472SFabian Frederick blocksize = i_blocksize(inode);
129909cbfeafSKirill A. Shutemov blocks_per_page = PAGE_SIZE / blocksize;
1300c9de560dSAlex Tomas
1301d3df1453SRitesh Harjani mb_debug(sb, "init page %lu\n", page->index);
1302d3df1453SRitesh Harjani
1303c9de560dSAlex Tomas groups_per_page = blocks_per_page >> 1;
1304c9de560dSAlex Tomas if (groups_per_page == 0)
1305c9de560dSAlex Tomas groups_per_page = 1;
1306c9de560dSAlex Tomas
1307c9de560dSAlex Tomas /* allocate buffer_heads to read bitmaps */
1308c9de560dSAlex Tomas if (groups_per_page > 1) {
1309c9de560dSAlex Tomas i = sizeof(struct buffer_head *) * groups_per_page;
1310adb7ef60SKonstantin Khlebnikov bh = kzalloc(i, gfp);
1311139f46d3SKemeng Shi if (bh == NULL)
1312139f46d3SKemeng Shi return -ENOMEM;
1313c9de560dSAlex Tomas } else
1314c9de560dSAlex Tomas bh = &bhs;
1315c9de560dSAlex Tomas
1316c9de560dSAlex Tomas first_group = page->index * blocks_per_page / 2;
1317c9de560dSAlex Tomas
1318c9de560dSAlex Tomas /* read all groups the page covers into the cache */
1319813e5727STheodore Ts'o for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
1320813e5727STheodore Ts'o if (group >= ngroups)
1321c9de560dSAlex Tomas break;
1322c9de560dSAlex Tomas
1323813e5727STheodore Ts'o grinfo = ext4_get_group_info(sb, group);
13245354b2afSTheodore Ts'o if (!grinfo)
13255354b2afSTheodore Ts'o continue;
13269b8b7d35SAmir Goldstein /*
13279b8b7d35SAmir Goldstein * If page is uptodate then we came here after online resize
13289b8b7d35SAmir Goldstein * which added some new uninitialized group info structs, so
13299b8b7d35SAmir Goldstein * we must skip all initialized uptodate buddies on the page,
13309b8b7d35SAmir Goldstein * which may be currently in use by an allocating task.
13319b8b7d35SAmir Goldstein */
13329b8b7d35SAmir Goldstein if (PageUptodate(page) && !EXT4_MB_GRP_NEED_INIT(grinfo)) {
13339b8b7d35SAmir Goldstein bh[i] = NULL;
13349b8b7d35SAmir Goldstein continue;
13359b8b7d35SAmir Goldstein }
1336cfd73237SAlex Zhuravlev bh[i] = ext4_read_block_bitmap_nowait(sb, group, false);
13379008a58eSDarrick J. Wong if (IS_ERR(bh[i])) {
13389008a58eSDarrick J. Wong err = PTR_ERR(bh[i]);
13399008a58eSDarrick J. Wong bh[i] = NULL;
1340c9de560dSAlex Tomas goto out;
13412ccb5fb9SAneesh Kumar K.V }
1342d3df1453SRitesh Harjani mb_debug(sb, "read bitmap for group %u\n", group);
1343c9de560dSAlex Tomas }
1344c9de560dSAlex Tomas
1345c9de560dSAlex Tomas /* wait for I/O completion */
1346813e5727STheodore Ts'o for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
13479008a58eSDarrick J. Wong int err2;
13489008a58eSDarrick J. Wong
13499008a58eSDarrick J. Wong if (!bh[i])
13509008a58eSDarrick J. Wong continue;
13519008a58eSDarrick J. Wong err2 = ext4_wait_block_bitmap(sb, group, bh[i]);
13529008a58eSDarrick J. Wong if (!err)
13539008a58eSDarrick J. Wong err = err2;
1354813e5727STheodore Ts'o }
1355c9de560dSAlex Tomas
1356c9de560dSAlex Tomas first_block = page->index * blocks_per_page;
1357c9de560dSAlex Tomas for (i = 0; i < blocks_per_page; i++) {
1358c9de560dSAlex Tomas group = (first_block + i) >> 1;
13598df9675fSTheodore Ts'o if (group >= ngroups)
1360c9de560dSAlex Tomas break;
1361c9de560dSAlex Tomas
13629b8b7d35SAmir Goldstein if (!bh[group - first_group])
13639b8b7d35SAmir Goldstein /* skip initialized uptodate buddy */
13649b8b7d35SAmir Goldstein continue;
13659b8b7d35SAmir Goldstein
1366bbdc322fSLukas Czerner if (!buffer_verified(bh[group - first_group]))
1367bbdc322fSLukas Czerner /* Skip faulty bitmaps */
1368bbdc322fSLukas Czerner continue;
1369bbdc322fSLukas Czerner err = 0;
1370bbdc322fSLukas Czerner
1371c9de560dSAlex Tomas /*
1372c9de560dSAlex Tomas * data carry information regarding this
1373c9de560dSAlex Tomas * particular group in the format specified
1374c9de560dSAlex Tomas * above
1375c9de560dSAlex Tomas *
1376c9de560dSAlex Tomas */
1377c9de560dSAlex Tomas data = page_address(page) + (i * blocksize);
1378c9de560dSAlex Tomas bitmap = bh[group - first_group]->b_data;
1379c9de560dSAlex Tomas
1380c9de560dSAlex Tomas /*
1381c9de560dSAlex Tomas * We place the buddy block and bitmap block
1382c9de560dSAlex Tomas * close together
1383c9de560dSAlex Tomas */
1384825ba5adSWang Jianjian grinfo = ext4_get_group_info(sb, group);
1385825ba5adSWang Jianjian if (!grinfo) {
1386825ba5adSWang Jianjian err = -EFSCORRUPTED;
1387825ba5adSWang Jianjian goto out;
1388825ba5adSWang Jianjian }
1389c9de560dSAlex Tomas if ((first_block + i) & 1) {
1390c9de560dSAlex Tomas /* this is block of buddy */
1391c9de560dSAlex Tomas BUG_ON(incore == NULL);
1392d3df1453SRitesh Harjani mb_debug(sb, "put buddy for group %u in page %lu/%x\n",
1393c9de560dSAlex Tomas group, page->index, i * blocksize);
1394f307333eSTheodore Ts'o trace_ext4_mb_buddy_bitmap_load(sb, group);
1395c9de560dSAlex Tomas grinfo->bb_fragments = 0;
1396c9de560dSAlex Tomas memset(grinfo->bb_counters, 0,
13971927805eSEric Sandeen sizeof(*grinfo->bb_counters) *
13984b68f6dfSHarshad Shirwadkar (MB_NUM_ORDERS(sb)));
1399c9de560dSAlex Tomas /*
1400c9de560dSAlex Tomas * incore got set to the group block bitmap below
1401c9de560dSAlex Tomas */
14027a2fcbf7SAneesh Kumar K.V ext4_lock_group(sb, group);
14039b8b7d35SAmir Goldstein /* init the buddy */
14049b8b7d35SAmir Goldstein memset(data, 0xff, blocksize);
14055354b2afSTheodore Ts'o ext4_mb_generate_buddy(sb, data, incore, group, grinfo);
14067a2fcbf7SAneesh Kumar K.V ext4_unlock_group(sb, group);
1407c9de560dSAlex Tomas incore = NULL;
1408c9de560dSAlex Tomas } else {
1409c9de560dSAlex Tomas /* this is block of bitmap */
1410c9de560dSAlex Tomas BUG_ON(incore != NULL);
1411d3df1453SRitesh Harjani mb_debug(sb, "put bitmap for group %u in page %lu/%x\n",
1412c9de560dSAlex Tomas group, page->index, i * blocksize);
1413f307333eSTheodore Ts'o trace_ext4_mb_bitmap_load(sb, group);
1414c9de560dSAlex Tomas
1415c9de560dSAlex Tomas /* see comments in ext4_mb_put_pa() */
1416c9de560dSAlex Tomas ext4_lock_group(sb, group);
1417c9de560dSAlex Tomas memcpy(data, bitmap, blocksize);
1418c9de560dSAlex Tomas
1419c9de560dSAlex Tomas /* mark all preallocated blks used in in-core bitmap */
1420c9de560dSAlex Tomas ext4_mb_generate_from_pa(sb, data, group);
1421825ba5adSWang Jianjian WARN_ON_ONCE(!RB_EMPTY_ROOT(&grinfo->bb_free_root));
1422c9de560dSAlex Tomas ext4_unlock_group(sb, group);
1423c9de560dSAlex Tomas
1424c9de560dSAlex Tomas /* set incore so that the buddy information can be
1425c9de560dSAlex Tomas * generated using this
1426c9de560dSAlex Tomas */
1427c9de560dSAlex Tomas incore = data;
1428c9de560dSAlex Tomas }
1429c9de560dSAlex Tomas }
1430c9de560dSAlex Tomas SetPageUptodate(page);
1431c9de560dSAlex Tomas
1432c9de560dSAlex Tomas out:
1433c9de560dSAlex Tomas if (bh) {
14349b8b7d35SAmir Goldstein for (i = 0; i < groups_per_page; i++)
1435c9de560dSAlex Tomas brelse(bh[i]);
1436c9de560dSAlex Tomas if (bh != &bhs)
1437c9de560dSAlex Tomas kfree(bh);
1438c9de560dSAlex Tomas }
1439c9de560dSAlex Tomas return err;
1440c9de560dSAlex Tomas }
1441c9de560dSAlex Tomas
14428a57d9d6SCurt Wohlgemuth /*
14432de8807bSAmir Goldstein * Lock the buddy and bitmap pages. This make sure other parallel init_group
14442de8807bSAmir Goldstein * on the same buddy page doesn't happen whild holding the buddy page lock.
14452de8807bSAmir Goldstein * Return locked buddy and bitmap pages on e4b struct. If buddy and bitmap
14462de8807bSAmir Goldstein * are on the same page e4b->bd_buddy_page is NULL and return value is 0.
1447eee4adc7SEric Sandeen */
ext4_mb_get_buddy_page_lock(struct super_block * sb,ext4_group_t group,struct ext4_buddy * e4b,gfp_t gfp)14482de8807bSAmir Goldstein static int ext4_mb_get_buddy_page_lock(struct super_block *sb,
1449adb7ef60SKonstantin Khlebnikov ext4_group_t group, struct ext4_buddy *e4b, gfp_t gfp)
1450eee4adc7SEric Sandeen {
14512de8807bSAmir Goldstein struct inode *inode = EXT4_SB(sb)->s_buddy_cache;
14522de8807bSAmir Goldstein int block, pnum, poff;
1453eee4adc7SEric Sandeen int blocks_per_page;
14542de8807bSAmir Goldstein struct page *page;
14552de8807bSAmir Goldstein
14562de8807bSAmir Goldstein e4b->bd_buddy_page = NULL;
14572de8807bSAmir Goldstein e4b->bd_bitmap_page = NULL;
1458eee4adc7SEric Sandeen
145909cbfeafSKirill A. Shutemov blocks_per_page = PAGE_SIZE / sb->s_blocksize;
1460eee4adc7SEric Sandeen /*
1461eee4adc7SEric Sandeen * the buddy cache inode stores the block bitmap
1462eee4adc7SEric Sandeen * and buddy information in consecutive blocks.
1463eee4adc7SEric Sandeen * So for each group we need two blocks.
1464eee4adc7SEric Sandeen */
1465eee4adc7SEric Sandeen block = group * 2;
1466eee4adc7SEric Sandeen pnum = block / blocks_per_page;
14672de8807bSAmir Goldstein poff = block % blocks_per_page;
1468adb7ef60SKonstantin Khlebnikov page = find_or_create_page(inode->i_mapping, pnum, gfp);
14692de8807bSAmir Goldstein if (!page)
1470c57ab39bSYounger Liu return -ENOMEM;
14712de8807bSAmir Goldstein BUG_ON(page->mapping != inode->i_mapping);
14722de8807bSAmir Goldstein e4b->bd_bitmap_page = page;
14732de8807bSAmir Goldstein e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1474eee4adc7SEric Sandeen
14752de8807bSAmir Goldstein if (blocks_per_page >= 2) {
14762de8807bSAmir Goldstein /* buddy and bitmap are on the same page */
14772de8807bSAmir Goldstein return 0;
1478eee4adc7SEric Sandeen }
1479eee4adc7SEric Sandeen
14802de8807bSAmir Goldstein block++;
1481eee4adc7SEric Sandeen pnum = block / blocks_per_page;
1482adb7ef60SKonstantin Khlebnikov page = find_or_create_page(inode->i_mapping, pnum, gfp);
14832de8807bSAmir Goldstein if (!page)
1484c57ab39bSYounger Liu return -ENOMEM;
14852de8807bSAmir Goldstein BUG_ON(page->mapping != inode->i_mapping);
14862de8807bSAmir Goldstein e4b->bd_buddy_page = page;
14872de8807bSAmir Goldstein return 0;
1488eee4adc7SEric Sandeen }
1489eee4adc7SEric Sandeen
ext4_mb_put_buddy_page_lock(struct ext4_buddy * e4b)14902de8807bSAmir Goldstein static void ext4_mb_put_buddy_page_lock(struct ext4_buddy *e4b)
14912de8807bSAmir Goldstein {
14922de8807bSAmir Goldstein if (e4b->bd_bitmap_page) {
14932de8807bSAmir Goldstein unlock_page(e4b->bd_bitmap_page);
149409cbfeafSKirill A. Shutemov put_page(e4b->bd_bitmap_page);
14952de8807bSAmir Goldstein }
14962de8807bSAmir Goldstein if (e4b->bd_buddy_page) {
14972de8807bSAmir Goldstein unlock_page(e4b->bd_buddy_page);
149809cbfeafSKirill A. Shutemov put_page(e4b->bd_buddy_page);
14992de8807bSAmir Goldstein }
1500eee4adc7SEric Sandeen }
1501eee4adc7SEric Sandeen
1502eee4adc7SEric Sandeen /*
15038a57d9d6SCurt Wohlgemuth * Locking note: This routine calls ext4_mb_init_cache(), which takes the
15048a57d9d6SCurt Wohlgemuth * block group lock of all groups for this page; do not hold the BG lock when
15058a57d9d6SCurt Wohlgemuth * calling this routine!
15068a57d9d6SCurt Wohlgemuth */
1507b6a758ecSAneesh Kumar K.V static noinline_for_stack
ext4_mb_init_group(struct super_block * sb,ext4_group_t group,gfp_t gfp)1508adb7ef60SKonstantin Khlebnikov int ext4_mb_init_group(struct super_block *sb, ext4_group_t group, gfp_t gfp)
1509b6a758ecSAneesh Kumar K.V {
1510b6a758ecSAneesh Kumar K.V
1511b6a758ecSAneesh Kumar K.V struct ext4_group_info *this_grp;
15122de8807bSAmir Goldstein struct ext4_buddy e4b;
15132de8807bSAmir Goldstein struct page *page;
15142de8807bSAmir Goldstein int ret = 0;
1515b6a758ecSAneesh Kumar K.V
1516b10a44c3STheodore Ts'o might_sleep();
1517d3df1453SRitesh Harjani mb_debug(sb, "init group %u\n", group);
1518b6a758ecSAneesh Kumar K.V this_grp = ext4_get_group_info(sb, group);
15195354b2afSTheodore Ts'o if (!this_grp)
15205354b2afSTheodore Ts'o return -EFSCORRUPTED;
15215354b2afSTheodore Ts'o
1522b6a758ecSAneesh Kumar K.V /*
152308c3a813SAneesh Kumar K.V * This ensures that we don't reinit the buddy cache
152408c3a813SAneesh Kumar K.V * page which map to the group from which we are already
152508c3a813SAneesh Kumar K.V * allocating. If we are looking at the buddy cache we would
152608c3a813SAneesh Kumar K.V * have taken a reference using ext4_mb_load_buddy and that
15272de8807bSAmir Goldstein * would have pinned buddy page to page cache.
15282457aec6SMel Gorman * The call to ext4_mb_get_buddy_page_lock will mark the
15292457aec6SMel Gorman * page accessed.
1530b6a758ecSAneesh Kumar K.V */
1531adb7ef60SKonstantin Khlebnikov ret = ext4_mb_get_buddy_page_lock(sb, group, &e4b, gfp);
15322de8807bSAmir Goldstein if (ret || !EXT4_MB_GRP_NEED_INIT(this_grp)) {
1533b6a758ecSAneesh Kumar K.V /*
1534b6a758ecSAneesh Kumar K.V * somebody initialized the group
1535b6a758ecSAneesh Kumar K.V * return without doing anything
1536b6a758ecSAneesh Kumar K.V */
1537b6a758ecSAneesh Kumar K.V goto err;
1538b6a758ecSAneesh Kumar K.V }
15392de8807bSAmir Goldstein
15402de8807bSAmir Goldstein page = e4b.bd_bitmap_page;
1541adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_cache(page, NULL, gfp);
15422de8807bSAmir Goldstein if (ret)
1543b6a758ecSAneesh Kumar K.V goto err;
15442de8807bSAmir Goldstein if (!PageUptodate(page)) {
1545b6a758ecSAneesh Kumar K.V ret = -EIO;
1546b6a758ecSAneesh Kumar K.V goto err;
1547b6a758ecSAneesh Kumar K.V }
1548b6a758ecSAneesh Kumar K.V
15492de8807bSAmir Goldstein if (e4b.bd_buddy_page == NULL) {
1550b6a758ecSAneesh Kumar K.V /*
1551b6a758ecSAneesh Kumar K.V * If both the bitmap and buddy are in
1552b6a758ecSAneesh Kumar K.V * the same page we don't need to force
1553b6a758ecSAneesh Kumar K.V * init the buddy
1554b6a758ecSAneesh Kumar K.V */
15552de8807bSAmir Goldstein ret = 0;
1556b6a758ecSAneesh Kumar K.V goto err;
1557b6a758ecSAneesh Kumar K.V }
15582de8807bSAmir Goldstein /* init buddy cache */
15592de8807bSAmir Goldstein page = e4b.bd_buddy_page;
1560adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_cache(page, e4b.bd_bitmap, gfp);
15612de8807bSAmir Goldstein if (ret)
15622de8807bSAmir Goldstein goto err;
15632de8807bSAmir Goldstein if (!PageUptodate(page)) {
1564b6a758ecSAneesh Kumar K.V ret = -EIO;
1565b6a758ecSAneesh Kumar K.V goto err;
1566b6a758ecSAneesh Kumar K.V }
1567b6a758ecSAneesh Kumar K.V err:
15682de8807bSAmir Goldstein ext4_mb_put_buddy_page_lock(&e4b);
1569b6a758ecSAneesh Kumar K.V return ret;
1570b6a758ecSAneesh Kumar K.V }
1571b6a758ecSAneesh Kumar K.V
15728a57d9d6SCurt Wohlgemuth /*
15738a57d9d6SCurt Wohlgemuth * Locking note: This routine calls ext4_mb_init_cache(), which takes the
15748a57d9d6SCurt Wohlgemuth * block group lock of all groups for this page; do not hold the BG lock when
15758a57d9d6SCurt Wohlgemuth * calling this routine!
15768a57d9d6SCurt Wohlgemuth */
15774ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_load_buddy_gfp(struct super_block * sb,ext4_group_t group,struct ext4_buddy * e4b,gfp_t gfp)1578adb7ef60SKonstantin Khlebnikov ext4_mb_load_buddy_gfp(struct super_block *sb, ext4_group_t group,
1579adb7ef60SKonstantin Khlebnikov struct ext4_buddy *e4b, gfp_t gfp)
1580c9de560dSAlex Tomas {
1581c9de560dSAlex Tomas int blocks_per_page;
1582c9de560dSAlex Tomas int block;
1583c9de560dSAlex Tomas int pnum;
1584c9de560dSAlex Tomas int poff;
1585c9de560dSAlex Tomas struct page *page;
1586fdf6c7a7SShen Feng int ret;
1587920313a7SAneesh Kumar K.V struct ext4_group_info *grp;
1588920313a7SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb);
1589920313a7SAneesh Kumar K.V struct inode *inode = sbi->s_buddy_cache;
1590c9de560dSAlex Tomas
1591b10a44c3STheodore Ts'o might_sleep();
1592d3df1453SRitesh Harjani mb_debug(sb, "load group %u\n", group);
1593c9de560dSAlex Tomas
159409cbfeafSKirill A. Shutemov blocks_per_page = PAGE_SIZE / sb->s_blocksize;
1595920313a7SAneesh Kumar K.V grp = ext4_get_group_info(sb, group);
15965354b2afSTheodore Ts'o if (!grp)
15975354b2afSTheodore Ts'o return -EFSCORRUPTED;
1598c9de560dSAlex Tomas
1599c9de560dSAlex Tomas e4b->bd_blkbits = sb->s_blocksize_bits;
1600529da704STao Ma e4b->bd_info = grp;
1601c9de560dSAlex Tomas e4b->bd_sb = sb;
1602c9de560dSAlex Tomas e4b->bd_group = group;
1603c9de560dSAlex Tomas e4b->bd_buddy_page = NULL;
1604c9de560dSAlex Tomas e4b->bd_bitmap_page = NULL;
1605c9de560dSAlex Tomas
1606f41c0750SAneesh Kumar K.V if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
1607f41c0750SAneesh Kumar K.V /*
1608f41c0750SAneesh Kumar K.V * we need full data about the group
1609f41c0750SAneesh Kumar K.V * to make a good selection
1610f41c0750SAneesh Kumar K.V */
1611adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_group(sb, group, gfp);
1612f41c0750SAneesh Kumar K.V if (ret)
1613f41c0750SAneesh Kumar K.V return ret;
1614f41c0750SAneesh Kumar K.V }
1615f41c0750SAneesh Kumar K.V
1616c9de560dSAlex Tomas /*
1617c9de560dSAlex Tomas * the buddy cache inode stores the block bitmap
1618c9de560dSAlex Tomas * and buddy information in consecutive blocks.
1619c9de560dSAlex Tomas * So for each group we need two blocks.
1620c9de560dSAlex Tomas */
1621c9de560dSAlex Tomas block = group * 2;
1622c9de560dSAlex Tomas pnum = block / blocks_per_page;
1623c9de560dSAlex Tomas poff = block % blocks_per_page;
1624c9de560dSAlex Tomas
1625c9de560dSAlex Tomas /* we could use find_or_create_page(), but it locks page
1626c9de560dSAlex Tomas * what we'd like to avoid in fast path ... */
16272457aec6SMel Gorman page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
1628c9de560dSAlex Tomas if (page == NULL || !PageUptodate(page)) {
1629c9de560dSAlex Tomas if (page)
1630920313a7SAneesh Kumar K.V /*
1631920313a7SAneesh Kumar K.V * drop the page reference and try
1632920313a7SAneesh Kumar K.V * to get the page with lock. If we
1633920313a7SAneesh Kumar K.V * are not uptodate that implies
1634920313a7SAneesh Kumar K.V * somebody just created the page but
1635920313a7SAneesh Kumar K.V * is yet to initialize the same. So
1636920313a7SAneesh Kumar K.V * wait for it to initialize.
1637920313a7SAneesh Kumar K.V */
163809cbfeafSKirill A. Shutemov put_page(page);
1639adb7ef60SKonstantin Khlebnikov page = find_or_create_page(inode->i_mapping, pnum, gfp);
1640c9de560dSAlex Tomas if (page) {
164119b8b035STheodore Ts'o if (WARN_RATELIMIT(page->mapping != inode->i_mapping,
164219b8b035STheodore Ts'o "ext4: bitmap's paging->mapping != inode->i_mapping\n")) {
164319b8b035STheodore Ts'o /* should never happen */
164419b8b035STheodore Ts'o unlock_page(page);
164519b8b035STheodore Ts'o ret = -EINVAL;
164619b8b035STheodore Ts'o goto err;
164719b8b035STheodore Ts'o }
1648c9de560dSAlex Tomas if (!PageUptodate(page)) {
1649adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_cache(page, NULL, gfp);
1650fdf6c7a7SShen Feng if (ret) {
1651fdf6c7a7SShen Feng unlock_page(page);
1652fdf6c7a7SShen Feng goto err;
1653fdf6c7a7SShen Feng }
1654c9de560dSAlex Tomas mb_cmp_bitmaps(e4b, page_address(page) +
1655c9de560dSAlex Tomas (poff * sb->s_blocksize));
1656c9de560dSAlex Tomas }
1657c9de560dSAlex Tomas unlock_page(page);
1658c9de560dSAlex Tomas }
1659c9de560dSAlex Tomas }
1660c57ab39bSYounger Liu if (page == NULL) {
1661c57ab39bSYounger Liu ret = -ENOMEM;
1662c57ab39bSYounger Liu goto err;
1663c57ab39bSYounger Liu }
1664c57ab39bSYounger Liu if (!PageUptodate(page)) {
1665fdf6c7a7SShen Feng ret = -EIO;
1666c9de560dSAlex Tomas goto err;
1667fdf6c7a7SShen Feng }
16682457aec6SMel Gorman
16692457aec6SMel Gorman /* Pages marked accessed already */
1670c9de560dSAlex Tomas e4b->bd_bitmap_page = page;
1671c9de560dSAlex Tomas e4b->bd_bitmap = page_address(page) + (poff * sb->s_blocksize);
1672c9de560dSAlex Tomas
1673c9de560dSAlex Tomas block++;
1674c9de560dSAlex Tomas pnum = block / blocks_per_page;
1675c9de560dSAlex Tomas poff = block % blocks_per_page;
1676c9de560dSAlex Tomas
16772457aec6SMel Gorman page = find_get_page_flags(inode->i_mapping, pnum, FGP_ACCESSED);
1678c9de560dSAlex Tomas if (page == NULL || !PageUptodate(page)) {
1679c9de560dSAlex Tomas if (page)
168009cbfeafSKirill A. Shutemov put_page(page);
1681adb7ef60SKonstantin Khlebnikov page = find_or_create_page(inode->i_mapping, pnum, gfp);
1682c9de560dSAlex Tomas if (page) {
168319b8b035STheodore Ts'o if (WARN_RATELIMIT(page->mapping != inode->i_mapping,
168419b8b035STheodore Ts'o "ext4: buddy bitmap's page->mapping != inode->i_mapping\n")) {
168519b8b035STheodore Ts'o /* should never happen */
168619b8b035STheodore Ts'o unlock_page(page);
168719b8b035STheodore Ts'o ret = -EINVAL;
168819b8b035STheodore Ts'o goto err;
168919b8b035STheodore Ts'o }
1690fdf6c7a7SShen Feng if (!PageUptodate(page)) {
1691adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_cache(page, e4b->bd_bitmap,
1692adb7ef60SKonstantin Khlebnikov gfp);
1693fdf6c7a7SShen Feng if (ret) {
1694fdf6c7a7SShen Feng unlock_page(page);
1695fdf6c7a7SShen Feng goto err;
1696fdf6c7a7SShen Feng }
1697fdf6c7a7SShen Feng }
1698c9de560dSAlex Tomas unlock_page(page);
1699c9de560dSAlex Tomas }
1700c9de560dSAlex Tomas }
1701c57ab39bSYounger Liu if (page == NULL) {
1702c57ab39bSYounger Liu ret = -ENOMEM;
1703c57ab39bSYounger Liu goto err;
1704c57ab39bSYounger Liu }
1705c57ab39bSYounger Liu if (!PageUptodate(page)) {
1706fdf6c7a7SShen Feng ret = -EIO;
1707c9de560dSAlex Tomas goto err;
1708fdf6c7a7SShen Feng }
17092457aec6SMel Gorman
17102457aec6SMel Gorman /* Pages marked accessed already */
1711c9de560dSAlex Tomas e4b->bd_buddy_page = page;
1712c9de560dSAlex Tomas e4b->bd_buddy = page_address(page) + (poff * sb->s_blocksize);
1713c9de560dSAlex Tomas
1714c9de560dSAlex Tomas return 0;
1715c9de560dSAlex Tomas
1716c9de560dSAlex Tomas err:
171726626f11SYang Ruirui if (page)
171809cbfeafSKirill A. Shutemov put_page(page);
1719c9de560dSAlex Tomas if (e4b->bd_bitmap_page)
172009cbfeafSKirill A. Shutemov put_page(e4b->bd_bitmap_page);
1721285164b8SKemeng Shi
1722c9de560dSAlex Tomas e4b->bd_buddy = NULL;
1723c9de560dSAlex Tomas e4b->bd_bitmap = NULL;
1724fdf6c7a7SShen Feng return ret;
1725c9de560dSAlex Tomas }
1726c9de560dSAlex Tomas
ext4_mb_load_buddy(struct super_block * sb,ext4_group_t group,struct ext4_buddy * e4b)1727adb7ef60SKonstantin Khlebnikov static int ext4_mb_load_buddy(struct super_block *sb, ext4_group_t group,
1728adb7ef60SKonstantin Khlebnikov struct ext4_buddy *e4b)
1729adb7ef60SKonstantin Khlebnikov {
1730adb7ef60SKonstantin Khlebnikov return ext4_mb_load_buddy_gfp(sb, group, e4b, GFP_NOFS);
1731adb7ef60SKonstantin Khlebnikov }
1732adb7ef60SKonstantin Khlebnikov
ext4_mb_unload_buddy(struct ext4_buddy * e4b)1733e39e07fdSJing Zhang static void ext4_mb_unload_buddy(struct ext4_buddy *e4b)
1734c9de560dSAlex Tomas {
1735c9de560dSAlex Tomas if (e4b->bd_bitmap_page)
173609cbfeafSKirill A. Shutemov put_page(e4b->bd_bitmap_page);
1737c9de560dSAlex Tomas if (e4b->bd_buddy_page)
173809cbfeafSKirill A. Shutemov put_page(e4b->bd_buddy_page);
1739c9de560dSAlex Tomas }
1740c9de560dSAlex Tomas
1741c9de560dSAlex Tomas
mb_find_order_for_block(struct ext4_buddy * e4b,int block)1742c9de560dSAlex Tomas static int mb_find_order_for_block(struct ext4_buddy *e4b, int block)
1743c9de560dSAlex Tomas {
1744ce3cca33SChunguang Xu int order = 1, max;
1745c9de560dSAlex Tomas void *bb;
1746c9de560dSAlex Tomas
1747c5e8f3f3STheodore Ts'o BUG_ON(e4b->bd_bitmap == e4b->bd_buddy);
1748c9de560dSAlex Tomas BUG_ON(block >= (1 << (e4b->bd_blkbits + 3)));
1749c9de560dSAlex Tomas
1750c9de560dSAlex Tomas while (order <= e4b->bd_blkbits + 1) {
1751ce3cca33SChunguang Xu bb = mb_find_buddy(e4b, order, &max);
1752ce3cca33SChunguang Xu if (!mb_test_bit(block >> order, bb)) {
1753c9de560dSAlex Tomas /* this block is part of buddy of order 'order' */
1754c9de560dSAlex Tomas return order;
1755c9de560dSAlex Tomas }
1756c9de560dSAlex Tomas order++;
1757c9de560dSAlex Tomas }
1758c9de560dSAlex Tomas return 0;
1759c9de560dSAlex Tomas }
1760c9de560dSAlex Tomas
mb_clear_bits(void * bm,int cur,int len)1761955ce5f5SAneesh Kumar K.V static void mb_clear_bits(void *bm, int cur, int len)
1762c9de560dSAlex Tomas {
1763c9de560dSAlex Tomas __u32 *addr;
1764c9de560dSAlex Tomas
1765c9de560dSAlex Tomas len = cur + len;
1766c9de560dSAlex Tomas while (cur < len) {
1767c9de560dSAlex Tomas if ((cur & 31) == 0 && (len - cur) >= 32) {
1768c9de560dSAlex Tomas /* fast path: clear whole word at once */
1769c9de560dSAlex Tomas addr = bm + (cur >> 3);
1770c9de560dSAlex Tomas *addr = 0;
1771c9de560dSAlex Tomas cur += 32;
1772c9de560dSAlex Tomas continue;
1773c9de560dSAlex Tomas }
1774e8134b27SAneesh Kumar K.V mb_clear_bit(cur, bm);
1775c9de560dSAlex Tomas cur++;
1776c9de560dSAlex Tomas }
1777c9de560dSAlex Tomas }
1778c9de560dSAlex Tomas
1779eabe0444SAndrey Sidorov /* clear bits in given range
1780eabe0444SAndrey Sidorov * will return first found zero bit if any, -1 otherwise
1781eabe0444SAndrey Sidorov */
mb_test_and_clear_bits(void * bm,int cur,int len)1782eabe0444SAndrey Sidorov static int mb_test_and_clear_bits(void *bm, int cur, int len)
1783eabe0444SAndrey Sidorov {
1784eabe0444SAndrey Sidorov __u32 *addr;
1785eabe0444SAndrey Sidorov int zero_bit = -1;
1786eabe0444SAndrey Sidorov
1787eabe0444SAndrey Sidorov len = cur + len;
1788eabe0444SAndrey Sidorov while (cur < len) {
1789eabe0444SAndrey Sidorov if ((cur & 31) == 0 && (len - cur) >= 32) {
1790eabe0444SAndrey Sidorov /* fast path: clear whole word at once */
1791eabe0444SAndrey Sidorov addr = bm + (cur >> 3);
1792eabe0444SAndrey Sidorov if (*addr != (__u32)(-1) && zero_bit == -1)
1793eabe0444SAndrey Sidorov zero_bit = cur + mb_find_next_zero_bit(addr, 32, 0);
1794eabe0444SAndrey Sidorov *addr = 0;
1795eabe0444SAndrey Sidorov cur += 32;
1796eabe0444SAndrey Sidorov continue;
1797eabe0444SAndrey Sidorov }
1798eabe0444SAndrey Sidorov if (!mb_test_and_clear_bit(cur, bm) && zero_bit == -1)
1799eabe0444SAndrey Sidorov zero_bit = cur;
1800eabe0444SAndrey Sidorov cur++;
1801eabe0444SAndrey Sidorov }
1802eabe0444SAndrey Sidorov
1803eabe0444SAndrey Sidorov return zero_bit;
1804eabe0444SAndrey Sidorov }
1805eabe0444SAndrey Sidorov
mb_set_bits(void * bm,int cur,int len)1806123e3016SRitesh Harjani void mb_set_bits(void *bm, int cur, int len)
1807c9de560dSAlex Tomas {
1808c9de560dSAlex Tomas __u32 *addr;
1809c9de560dSAlex Tomas
1810c9de560dSAlex Tomas len = cur + len;
1811c9de560dSAlex Tomas while (cur < len) {
1812c9de560dSAlex Tomas if ((cur & 31) == 0 && (len - cur) >= 32) {
1813c9de560dSAlex Tomas /* fast path: set whole word at once */
1814c9de560dSAlex Tomas addr = bm + (cur >> 3);
1815c9de560dSAlex Tomas *addr = 0xffffffff;
1816c9de560dSAlex Tomas cur += 32;
1817c9de560dSAlex Tomas continue;
1818c9de560dSAlex Tomas }
1819e8134b27SAneesh Kumar K.V mb_set_bit(cur, bm);
1820c9de560dSAlex Tomas cur++;
1821c9de560dSAlex Tomas }
1822c9de560dSAlex Tomas }
1823c9de560dSAlex Tomas
mb_buddy_adjust_border(int * bit,void * bitmap,int side)1824eabe0444SAndrey Sidorov static inline int mb_buddy_adjust_border(int* bit, void* bitmap, int side)
1825eabe0444SAndrey Sidorov {
1826eabe0444SAndrey Sidorov if (mb_test_bit(*bit + side, bitmap)) {
1827eabe0444SAndrey Sidorov mb_clear_bit(*bit, bitmap);
1828eabe0444SAndrey Sidorov (*bit) -= side;
1829eabe0444SAndrey Sidorov return 1;
1830eabe0444SAndrey Sidorov }
1831eabe0444SAndrey Sidorov else {
1832eabe0444SAndrey Sidorov (*bit) += side;
1833eabe0444SAndrey Sidorov mb_set_bit(*bit, bitmap);
1834eabe0444SAndrey Sidorov return -1;
1835eabe0444SAndrey Sidorov }
1836eabe0444SAndrey Sidorov }
1837eabe0444SAndrey Sidorov
mb_buddy_mark_free(struct ext4_buddy * e4b,int first,int last)1838eabe0444SAndrey Sidorov static void mb_buddy_mark_free(struct ext4_buddy *e4b, int first, int last)
1839eabe0444SAndrey Sidorov {
1840eabe0444SAndrey Sidorov int max;
1841eabe0444SAndrey Sidorov int order = 1;
1842eabe0444SAndrey Sidorov void *buddy = mb_find_buddy(e4b, order, &max);
1843eabe0444SAndrey Sidorov
1844eabe0444SAndrey Sidorov while (buddy) {
1845eabe0444SAndrey Sidorov void *buddy2;
1846eabe0444SAndrey Sidorov
1847eabe0444SAndrey Sidorov /* Bits in range [first; last] are known to be set since
1848eabe0444SAndrey Sidorov * corresponding blocks were allocated. Bits in range
1849eabe0444SAndrey Sidorov * (first; last) will stay set because they form buddies on
1850eabe0444SAndrey Sidorov * upper layer. We just deal with borders if they don't
1851eabe0444SAndrey Sidorov * align with upper layer and then go up.
1852eabe0444SAndrey Sidorov * Releasing entire group is all about clearing
1853eabe0444SAndrey Sidorov * single bit of highest order buddy.
1854eabe0444SAndrey Sidorov */
1855eabe0444SAndrey Sidorov
1856eabe0444SAndrey Sidorov /* Example:
1857eabe0444SAndrey Sidorov * ---------------------------------
1858eabe0444SAndrey Sidorov * | 1 | 1 | 1 | 1 |
1859eabe0444SAndrey Sidorov * ---------------------------------
1860eabe0444SAndrey Sidorov * | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1861eabe0444SAndrey Sidorov * ---------------------------------
1862eabe0444SAndrey Sidorov * 0 1 2 3 4 5 6 7
1863eabe0444SAndrey Sidorov * \_____________________/
1864eabe0444SAndrey Sidorov *
1865eabe0444SAndrey Sidorov * Neither [1] nor [6] is aligned to above layer.
1866eabe0444SAndrey Sidorov * Left neighbour [0] is free, so mark it busy,
1867eabe0444SAndrey Sidorov * decrease bb_counters and extend range to
1868eabe0444SAndrey Sidorov * [0; 6]
1869eabe0444SAndrey Sidorov * Right neighbour [7] is busy. It can't be coaleasced with [6], so
1870eabe0444SAndrey Sidorov * mark [6] free, increase bb_counters and shrink range to
1871eabe0444SAndrey Sidorov * [0; 5].
1872eabe0444SAndrey Sidorov * Then shift range to [0; 2], go up and do the same.
1873eabe0444SAndrey Sidorov */
1874eabe0444SAndrey Sidorov
1875eabe0444SAndrey Sidorov
1876eabe0444SAndrey Sidorov if (first & 1)
1877eabe0444SAndrey Sidorov e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&first, buddy, -1);
1878eabe0444SAndrey Sidorov if (!(last & 1))
1879eabe0444SAndrey Sidorov e4b->bd_info->bb_counters[order] += mb_buddy_adjust_border(&last, buddy, 1);
1880eabe0444SAndrey Sidorov if (first > last)
1881eabe0444SAndrey Sidorov break;
1882eabe0444SAndrey Sidorov order++;
1883eabe0444SAndrey Sidorov
1884976620bdSKemeng Shi buddy2 = mb_find_buddy(e4b, order, &max);
1885976620bdSKemeng Shi if (!buddy2) {
1886eabe0444SAndrey Sidorov mb_clear_bits(buddy, first, last - first + 1);
1887eabe0444SAndrey Sidorov e4b->bd_info->bb_counters[order - 1] += last - first + 1;
1888eabe0444SAndrey Sidorov break;
1889eabe0444SAndrey Sidorov }
1890eabe0444SAndrey Sidorov first >>= 1;
1891eabe0444SAndrey Sidorov last >>= 1;
1892eabe0444SAndrey Sidorov buddy = buddy2;
1893eabe0444SAndrey Sidorov }
1894eabe0444SAndrey Sidorov }
1895eabe0444SAndrey Sidorov
mb_free_blocks(struct inode * inode,struct ext4_buddy * e4b,int first,int count)18967e5a8cddSShen Feng static void mb_free_blocks(struct inode *inode, struct ext4_buddy *e4b,
1897c9de560dSAlex Tomas int first, int count)
1898c9de560dSAlex Tomas {
1899eabe0444SAndrey Sidorov int left_is_free = 0;
1900eabe0444SAndrey Sidorov int right_is_free = 0;
1901eabe0444SAndrey Sidorov int block;
1902eabe0444SAndrey Sidorov int last = first + count - 1;
1903c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
1904c9de560dSAlex Tomas
1905c99d1e6eSTheodore Ts'o if (WARN_ON(count == 0))
1906c99d1e6eSTheodore Ts'o return;
1907eabe0444SAndrey Sidorov BUG_ON(last >= (sb->s_blocksize << 3));
1908bc8e6740SVincent Minet assert_spin_locked(ext4_group_lock_ptr(sb, e4b->bd_group));
1909163a203dSDarrick J. Wong /* Don't bother if the block group is corrupt. */
1910163a203dSDarrick J. Wong if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
1911163a203dSDarrick J. Wong return;
1912163a203dSDarrick J. Wong
1913c9de560dSAlex Tomas mb_check_buddy(e4b);
1914c9de560dSAlex Tomas mb_free_blocks_double(inode, e4b, first, count);
1915c9de560dSAlex Tomas
1916eabe0444SAndrey Sidorov /* access memory sequentially: check left neighbour,
1917eabe0444SAndrey Sidorov * clear range and then check right neighbour
1918eabe0444SAndrey Sidorov */
1919c9de560dSAlex Tomas if (first != 0)
1920eabe0444SAndrey Sidorov left_is_free = !mb_test_bit(first - 1, e4b->bd_bitmap);
1921eabe0444SAndrey Sidorov block = mb_test_and_clear_bits(e4b->bd_bitmap, first, count);
1922eabe0444SAndrey Sidorov if (last + 1 < EXT4_SB(sb)->s_mb_maxs[0])
1923eabe0444SAndrey Sidorov right_is_free = !mb_test_bit(last + 1, e4b->bd_bitmap);
1924c9de560dSAlex Tomas
1925eabe0444SAndrey Sidorov if (unlikely(block != -1)) {
1926e43bb4e6SNamjae Jeon struct ext4_sb_info *sbi = EXT4_SB(sb);
1927c9de560dSAlex Tomas ext4_fsblk_t blocknr;
19285661bd68SAkinobu Mita
1929aafdc920SBaokun Li /*
1930aafdc920SBaokun Li * Fastcommit replay can free already freed blocks which
1931aafdc920SBaokun Li * corrupts allocation info. Regenerate it.
1932aafdc920SBaokun Li */
1933aafdc920SBaokun Li if (sbi->s_mount_state & EXT4_FC_REPLAY) {
1934aafdc920SBaokun Li mb_regenerate_buddy(e4b);
1935aafdc920SBaokun Li goto check;
1936aafdc920SBaokun Li }
1937aafdc920SBaokun Li
19385661bd68SAkinobu Mita blocknr = ext4_group_first_block_no(sb, e4b->bd_group);
193949598e04SJun Piao blocknr += EXT4_C2B(sbi, block);
19405d1b1b3fSAneesh Kumar K.V ext4_grp_locked_error(sb, e4b->bd_group,
1941aafdc920SBaokun Li inode ? inode->i_ino : 0, blocknr,
19428016e29fSHarshad Shirwadkar "freeing already freed block (bit %u); block bitmap corrupt.",
1943163a203dSDarrick J. Wong block);
1944aafdc920SBaokun Li ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
1945db79e6d1SWang Shilong EXT4_GROUP_INFO_BBITMAP_CORRUPT);
1946aafdc920SBaokun Li return;
19478016e29fSHarshad Shirwadkar }
1948aafdc920SBaokun Li
1949aafdc920SBaokun Li this_cpu_inc(discard_pa_seq);
1950aafdc920SBaokun Li e4b->bd_info->bb_free += count;
1951aafdc920SBaokun Li if (first < e4b->bd_info->bb_first_free)
1952aafdc920SBaokun Li e4b->bd_info->bb_first_free = first;
1953c9de560dSAlex Tomas
1954eabe0444SAndrey Sidorov /* let's maintain fragments counter */
1955eabe0444SAndrey Sidorov if (left_is_free && right_is_free)
1956eabe0444SAndrey Sidorov e4b->bd_info->bb_fragments--;
1957eabe0444SAndrey Sidorov else if (!left_is_free && !right_is_free)
1958eabe0444SAndrey Sidorov e4b->bd_info->bb_fragments++;
1959c9de560dSAlex Tomas
1960eabe0444SAndrey Sidorov /* buddy[0] == bd_bitmap is a special case, so handle
1961eabe0444SAndrey Sidorov * it right away and let mb_buddy_mark_free stay free of
1962eabe0444SAndrey Sidorov * zero order checks.
1963eabe0444SAndrey Sidorov * Check if neighbours are to be coaleasced,
1964eabe0444SAndrey Sidorov * adjust bitmap bb_counters and borders appropriately.
1965eabe0444SAndrey Sidorov */
1966eabe0444SAndrey Sidorov if (first & 1) {
1967eabe0444SAndrey Sidorov first += !left_is_free;
1968eabe0444SAndrey Sidorov e4b->bd_info->bb_counters[0] += left_is_free ? -1 : 1;
1969c9de560dSAlex Tomas }
1970eabe0444SAndrey Sidorov if (!(last & 1)) {
1971eabe0444SAndrey Sidorov last -= !right_is_free;
1972eabe0444SAndrey Sidorov e4b->bd_info->bb_counters[0] += right_is_free ? -1 : 1;
1973c9de560dSAlex Tomas }
1974eabe0444SAndrey Sidorov
1975eabe0444SAndrey Sidorov if (first <= last)
1976eabe0444SAndrey Sidorov mb_buddy_mark_free(e4b, first >> 1, last >> 1);
1977eabe0444SAndrey Sidorov
19788a57d9d6SCurt Wohlgemuth mb_set_largest_free_order(sb, e4b->bd_info);
1979196e402aSHarshad Shirwadkar mb_update_avg_fragment_size(sb, e4b->bd_info);
1980aafdc920SBaokun Li check:
1981c9de560dSAlex Tomas mb_check_buddy(e4b);
1982c9de560dSAlex Tomas }
1983c9de560dSAlex Tomas
mb_find_extent(struct ext4_buddy * e4b,int block,int needed,struct ext4_free_extent * ex)198415c006a2SRobin Dong static int mb_find_extent(struct ext4_buddy *e4b, int block,
1985c9de560dSAlex Tomas int needed, struct ext4_free_extent *ex)
1986c9de560dSAlex Tomas {
1987c9de560dSAlex Tomas int next = block;
198815c006a2SRobin Dong int max, order;
1989c9de560dSAlex Tomas void *buddy;
1990c9de560dSAlex Tomas
1991bc8e6740SVincent Minet assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
1992c9de560dSAlex Tomas BUG_ON(ex == NULL);
1993c9de560dSAlex Tomas
199415c006a2SRobin Dong buddy = mb_find_buddy(e4b, 0, &max);
1995c9de560dSAlex Tomas BUG_ON(buddy == NULL);
1996c9de560dSAlex Tomas BUG_ON(block >= max);
1997c9de560dSAlex Tomas if (mb_test_bit(block, buddy)) {
1998c9de560dSAlex Tomas ex->fe_len = 0;
1999c9de560dSAlex Tomas ex->fe_start = 0;
2000c9de560dSAlex Tomas ex->fe_group = 0;
2001c9de560dSAlex Tomas return 0;
2002c9de560dSAlex Tomas }
2003c9de560dSAlex Tomas
2004c9de560dSAlex Tomas /* find actual order */
2005c9de560dSAlex Tomas order = mb_find_order_for_block(e4b, block);
2006c9de560dSAlex Tomas block = block >> order;
2007c9de560dSAlex Tomas
2008c9de560dSAlex Tomas ex->fe_len = 1 << order;
2009c9de560dSAlex Tomas ex->fe_start = block << order;
2010c9de560dSAlex Tomas ex->fe_group = e4b->bd_group;
2011c9de560dSAlex Tomas
2012c9de560dSAlex Tomas /* calc difference from given start */
2013c9de560dSAlex Tomas next = next - ex->fe_start;
2014c9de560dSAlex Tomas ex->fe_len -= next;
2015c9de560dSAlex Tomas ex->fe_start += next;
2016c9de560dSAlex Tomas
2017c9de560dSAlex Tomas while (needed > ex->fe_len &&
2018d8ec0c39SAlan Cox mb_find_buddy(e4b, order, &max)) {
2019c9de560dSAlex Tomas
2020c9de560dSAlex Tomas if (block + 1 >= max)
2021c9de560dSAlex Tomas break;
2022c9de560dSAlex Tomas
2023c9de560dSAlex Tomas next = (block + 1) * (1 << order);
2024c5e8f3f3STheodore Ts'o if (mb_test_bit(next, e4b->bd_bitmap))
2025c9de560dSAlex Tomas break;
2026c9de560dSAlex Tomas
2027b051d8dcSRobin Dong order = mb_find_order_for_block(e4b, next);
2028c9de560dSAlex Tomas
2029c9de560dSAlex Tomas block = next >> order;
2030c9de560dSAlex Tomas ex->fe_len += 1 << order;
2031c9de560dSAlex Tomas }
2032c9de560dSAlex Tomas
203331562b95SJan Kara if (ex->fe_start + ex->fe_len > EXT4_CLUSTERS_PER_GROUP(e4b->bd_sb)) {
203443c73221STheodore Ts'o /* Should never happen! (but apparently sometimes does?!?) */
203543c73221STheodore Ts'o WARN_ON(1);
2036cd84bbbaSStephen Brennan ext4_grp_locked_error(e4b->bd_sb, e4b->bd_group, 0, 0,
2037cd84bbbaSStephen Brennan "corruption or bug in mb_find_extent "
203843c73221STheodore Ts'o "block=%d, order=%d needed=%d ex=%u/%d/%d@%u",
203943c73221STheodore Ts'o block, order, needed, ex->fe_group, ex->fe_start,
204043c73221STheodore Ts'o ex->fe_len, ex->fe_logical);
204143c73221STheodore Ts'o ex->fe_len = 0;
204243c73221STheodore Ts'o ex->fe_start = 0;
204343c73221STheodore Ts'o ex->fe_group = 0;
204443c73221STheodore Ts'o }
2045c9de560dSAlex Tomas return ex->fe_len;
2046c9de560dSAlex Tomas }
2047c9de560dSAlex Tomas
mb_mark_used(struct ext4_buddy * e4b,struct ext4_free_extent * ex)2048c9de560dSAlex Tomas static int mb_mark_used(struct ext4_buddy *e4b, struct ext4_free_extent *ex)
2049c9de560dSAlex Tomas {
2050c9de560dSAlex Tomas int ord;
2051c9de560dSAlex Tomas int mlen = 0;
2052c9de560dSAlex Tomas int max = 0;
2053c9de560dSAlex Tomas int cur;
2054c9de560dSAlex Tomas int start = ex->fe_start;
2055c9de560dSAlex Tomas int len = ex->fe_len;
2056c9de560dSAlex Tomas unsigned ret = 0;
2057c9de560dSAlex Tomas int len0 = len;
2058c9de560dSAlex Tomas void *buddy;
2059218a6944Shanjinke bool split = false;
2060c9de560dSAlex Tomas
2061c9de560dSAlex Tomas BUG_ON(start + len > (e4b->bd_sb->s_blocksize << 3));
2062c9de560dSAlex Tomas BUG_ON(e4b->bd_group != ex->fe_group);
2063bc8e6740SVincent Minet assert_spin_locked(ext4_group_lock_ptr(e4b->bd_sb, e4b->bd_group));
2064c9de560dSAlex Tomas mb_check_buddy(e4b);
2065c9de560dSAlex Tomas mb_mark_used_double(e4b, start, len);
2066c9de560dSAlex Tomas
206707b5b8e1SRitesh Harjani this_cpu_inc(discard_pa_seq);
2068c9de560dSAlex Tomas e4b->bd_info->bb_free -= len;
2069c9de560dSAlex Tomas if (e4b->bd_info->bb_first_free == start)
2070c9de560dSAlex Tomas e4b->bd_info->bb_first_free += len;
2071c9de560dSAlex Tomas
2072c9de560dSAlex Tomas /* let's maintain fragments counter */
2073c9de560dSAlex Tomas if (start != 0)
2074c5e8f3f3STheodore Ts'o mlen = !mb_test_bit(start - 1, e4b->bd_bitmap);
2075c9de560dSAlex Tomas if (start + len < EXT4_SB(e4b->bd_sb)->s_mb_maxs[0])
2076c5e8f3f3STheodore Ts'o max = !mb_test_bit(start + len, e4b->bd_bitmap);
2077c9de560dSAlex Tomas if (mlen && max)
2078c9de560dSAlex Tomas e4b->bd_info->bb_fragments++;
2079c9de560dSAlex Tomas else if (!mlen && !max)
2080c9de560dSAlex Tomas e4b->bd_info->bb_fragments--;
2081c9de560dSAlex Tomas
2082c9de560dSAlex Tomas /* let's maintain buddy itself */
2083c9de560dSAlex Tomas while (len) {
2084218a6944Shanjinke if (!split)
2085c9de560dSAlex Tomas ord = mb_find_order_for_block(e4b, start);
2086c9de560dSAlex Tomas
2087c9de560dSAlex Tomas if (((start >> ord) << ord) == start && len >= (1 << ord)) {
2088c9de560dSAlex Tomas /* the whole chunk may be allocated at once! */
2089c9de560dSAlex Tomas mlen = 1 << ord;
2090218a6944Shanjinke if (!split)
2091c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, ord, &max);
2092218a6944Shanjinke else
2093218a6944Shanjinke split = false;
2094c9de560dSAlex Tomas BUG_ON((start >> ord) >= max);
2095c9de560dSAlex Tomas mb_set_bit(start >> ord, buddy);
2096c9de560dSAlex Tomas e4b->bd_info->bb_counters[ord]--;
2097c9de560dSAlex Tomas start += mlen;
2098c9de560dSAlex Tomas len -= mlen;
2099c9de560dSAlex Tomas BUG_ON(len < 0);
2100c9de560dSAlex Tomas continue;
2101c9de560dSAlex Tomas }
2102c9de560dSAlex Tomas
2103c9de560dSAlex Tomas /* store for history */
2104c9de560dSAlex Tomas if (ret == 0)
2105c9de560dSAlex Tomas ret = len | (ord << 16);
2106c9de560dSAlex Tomas
2107c9de560dSAlex Tomas /* we have to split large buddy */
2108c9de560dSAlex Tomas BUG_ON(ord <= 0);
2109c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, ord, &max);
2110c9de560dSAlex Tomas mb_set_bit(start >> ord, buddy);
2111c9de560dSAlex Tomas e4b->bd_info->bb_counters[ord]--;
2112c9de560dSAlex Tomas
2113c9de560dSAlex Tomas ord--;
2114c9de560dSAlex Tomas cur = (start >> ord) & ~1U;
2115c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, ord, &max);
2116c9de560dSAlex Tomas mb_clear_bit(cur, buddy);
2117c9de560dSAlex Tomas mb_clear_bit(cur + 1, buddy);
2118c9de560dSAlex Tomas e4b->bd_info->bb_counters[ord]++;
2119c9de560dSAlex Tomas e4b->bd_info->bb_counters[ord]++;
2120218a6944Shanjinke split = true;
2121c9de560dSAlex Tomas }
21228a57d9d6SCurt Wohlgemuth mb_set_largest_free_order(e4b->bd_sb, e4b->bd_info);
2123c9de560dSAlex Tomas
2124196e402aSHarshad Shirwadkar mb_update_avg_fragment_size(e4b->bd_sb, e4b->bd_info);
2125123e3016SRitesh Harjani mb_set_bits(e4b->bd_bitmap, ex->fe_start, len0);
2126c9de560dSAlex Tomas mb_check_buddy(e4b);
2127c9de560dSAlex Tomas
2128c9de560dSAlex Tomas return ret;
2129c9de560dSAlex Tomas }
2130c9de560dSAlex Tomas
2131c9de560dSAlex Tomas /*
2132c9de560dSAlex Tomas * Must be called under group lock!
2133c9de560dSAlex Tomas */
ext4_mb_use_best_found(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)2134c9de560dSAlex Tomas static void ext4_mb_use_best_found(struct ext4_allocation_context *ac,
2135c9de560dSAlex Tomas struct ext4_buddy *e4b)
2136c9de560dSAlex Tomas {
2137c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2138c9de560dSAlex Tomas int ret;
2139c9de560dSAlex Tomas
2140c9de560dSAlex Tomas BUG_ON(ac->ac_b_ex.fe_group != e4b->bd_group);
2141c9de560dSAlex Tomas BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2142c9de560dSAlex Tomas
2143c9de560dSAlex Tomas ac->ac_b_ex.fe_len = min(ac->ac_b_ex.fe_len, ac->ac_g_ex.fe_len);
2144c9de560dSAlex Tomas ac->ac_b_ex.fe_logical = ac->ac_g_ex.fe_logical;
2145c9de560dSAlex Tomas ret = mb_mark_used(e4b, &ac->ac_b_ex);
2146c9de560dSAlex Tomas
2147c9de560dSAlex Tomas /* preallocation can change ac_b_ex, thus we store actually
2148c9de560dSAlex Tomas * allocated blocks for history */
2149c9de560dSAlex Tomas ac->ac_f_ex = ac->ac_b_ex;
2150c9de560dSAlex Tomas
2151c9de560dSAlex Tomas ac->ac_status = AC_STATUS_FOUND;
2152c9de560dSAlex Tomas ac->ac_tail = ret & 0xffff;
2153c9de560dSAlex Tomas ac->ac_buddy = ret >> 16;
2154c9de560dSAlex Tomas
2155c3a326a6SAneesh Kumar K.V /*
2156c3a326a6SAneesh Kumar K.V * take the page reference. We want the page to be pinned
2157c3a326a6SAneesh Kumar K.V * so that we don't get a ext4_mb_init_cache_call for this
2158c3a326a6SAneesh Kumar K.V * group until we update the bitmap. That would mean we
2159c3a326a6SAneesh Kumar K.V * double allocate blocks. The reference is dropped
2160c3a326a6SAneesh Kumar K.V * in ext4_mb_release_context
2161c3a326a6SAneesh Kumar K.V */
2162c9de560dSAlex Tomas ac->ac_bitmap_page = e4b->bd_bitmap_page;
2163c9de560dSAlex Tomas get_page(ac->ac_bitmap_page);
2164c9de560dSAlex Tomas ac->ac_buddy_page = e4b->bd_buddy_page;
2165c9de560dSAlex Tomas get_page(ac->ac_buddy_page);
2166c9de560dSAlex Tomas /* store last allocated for subsequent stream allocation */
21674ba74d00STheodore Ts'o if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
2168c9de560dSAlex Tomas spin_lock(&sbi->s_md_lock);
2169c9de560dSAlex Tomas sbi->s_mb_last_group = ac->ac_f_ex.fe_group;
2170c9de560dSAlex Tomas sbi->s_mb_last_start = ac->ac_f_ex.fe_start;
2171c9de560dSAlex Tomas spin_unlock(&sbi->s_md_lock);
2172c9de560dSAlex Tomas }
217353f86b17SRitesh Harjani /*
217453f86b17SRitesh Harjani * As we've just preallocated more space than
217553f86b17SRitesh Harjani * user requested originally, we store allocated
217653f86b17SRitesh Harjani * space in a special descriptor.
217753f86b17SRitesh Harjani */
217853f86b17SRitesh Harjani if (ac->ac_o_ex.fe_len < ac->ac_b_ex.fe_len)
217953f86b17SRitesh Harjani ext4_mb_new_preallocation(ac);
218053f86b17SRitesh Harjani
2181c9de560dSAlex Tomas }
2182c9de560dSAlex Tomas
ext4_mb_check_limits(struct ext4_allocation_context * ac,struct ext4_buddy * e4b,int finish_group)2183c9de560dSAlex Tomas static void ext4_mb_check_limits(struct ext4_allocation_context *ac,
2184c9de560dSAlex Tomas struct ext4_buddy *e4b,
2185c9de560dSAlex Tomas int finish_group)
2186c9de560dSAlex Tomas {
2187c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2188c9de560dSAlex Tomas struct ext4_free_extent *bex = &ac->ac_b_ex;
2189c9de560dSAlex Tomas struct ext4_free_extent *gex = &ac->ac_g_ex;
2190c9de560dSAlex Tomas
2191032115fcSAneesh Kumar K.V if (ac->ac_status == AC_STATUS_FOUND)
2192032115fcSAneesh Kumar K.V return;
2193c9de560dSAlex Tomas /*
2194c9de560dSAlex Tomas * We don't want to scan for a whole year
2195c9de560dSAlex Tomas */
2196c9de560dSAlex Tomas if (ac->ac_found > sbi->s_mb_max_to_scan &&
2197c9de560dSAlex Tomas !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2198c9de560dSAlex Tomas ac->ac_status = AC_STATUS_BREAK;
2199c9de560dSAlex Tomas return;
2200c9de560dSAlex Tomas }
2201c9de560dSAlex Tomas
2202c9de560dSAlex Tomas /*
2203c9de560dSAlex Tomas * Haven't found good chunk so far, let's continue
2204c9de560dSAlex Tomas */
2205c9de560dSAlex Tomas if (bex->fe_len < gex->fe_len)
2206c9de560dSAlex Tomas return;
2207c9de560dSAlex Tomas
22083582e745SOjaswin Mujoo if (finish_group || ac->ac_found > sbi->s_mb_min_to_scan)
2209c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2210c9de560dSAlex Tomas }
2211c9de560dSAlex Tomas
2212c9de560dSAlex Tomas /*
2213c9de560dSAlex Tomas * The routine checks whether found extent is good enough. If it is,
2214c9de560dSAlex Tomas * then the extent gets marked used and flag is set to the context
2215c9de560dSAlex Tomas * to stop scanning. Otherwise, the extent is compared with the
2216c9de560dSAlex Tomas * previous found extent and if new one is better, then it's stored
2217c9de560dSAlex Tomas * in the context. Later, the best found extent will be used, if
2218c9de560dSAlex Tomas * mballoc can't find good enough extent.
2219c9de560dSAlex Tomas *
22203582e745SOjaswin Mujoo * The algorithm used is roughly as follows:
22213582e745SOjaswin Mujoo *
22223582e745SOjaswin Mujoo * * If free extent found is exactly as big as goal, then
22233582e745SOjaswin Mujoo * stop the scan and use it immediately
22243582e745SOjaswin Mujoo *
22253582e745SOjaswin Mujoo * * If free extent found is smaller than goal, then keep retrying
22263582e745SOjaswin Mujoo * upto a max of sbi->s_mb_max_to_scan times (default 200). After
22273582e745SOjaswin Mujoo * that stop scanning and use whatever we have.
22283582e745SOjaswin Mujoo *
22293582e745SOjaswin Mujoo * * If free extent found is bigger than goal, then keep retrying
22303582e745SOjaswin Mujoo * upto a max of sbi->s_mb_min_to_scan times (default 10) before
22313582e745SOjaswin Mujoo * stopping the scan and using the extent.
22323582e745SOjaswin Mujoo *
22333582e745SOjaswin Mujoo *
2234c9de560dSAlex Tomas * FIXME: real allocation policy is to be designed yet!
2235c9de560dSAlex Tomas */
ext4_mb_measure_extent(struct ext4_allocation_context * ac,struct ext4_free_extent * ex,struct ext4_buddy * e4b)2236c9de560dSAlex Tomas static void ext4_mb_measure_extent(struct ext4_allocation_context *ac,
2237c9de560dSAlex Tomas struct ext4_free_extent *ex,
2238c9de560dSAlex Tomas struct ext4_buddy *e4b)
2239c9de560dSAlex Tomas {
2240c9de560dSAlex Tomas struct ext4_free_extent *bex = &ac->ac_b_ex;
2241c9de560dSAlex Tomas struct ext4_free_extent *gex = &ac->ac_g_ex;
2242c9de560dSAlex Tomas
2243c9de560dSAlex Tomas BUG_ON(ex->fe_len <= 0);
22447137d7a4STheodore Ts'o BUG_ON(ex->fe_len > EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
22457137d7a4STheodore Ts'o BUG_ON(ex->fe_start >= EXT4_CLUSTERS_PER_GROUP(ac->ac_sb));
2246c9de560dSAlex Tomas BUG_ON(ac->ac_status != AC_STATUS_CONTINUE);
2247c9de560dSAlex Tomas
2248c9de560dSAlex Tomas ac->ac_found++;
2249fdd9a009SOjaswin Mujoo ac->ac_cX_found[ac->ac_criteria]++;
2250c9de560dSAlex Tomas
2251c9de560dSAlex Tomas /*
2252c9de560dSAlex Tomas * The special case - take what you catch first
2253c9de560dSAlex Tomas */
2254c9de560dSAlex Tomas if (unlikely(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2255c9de560dSAlex Tomas *bex = *ex;
2256c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2257c9de560dSAlex Tomas return;
2258c9de560dSAlex Tomas }
2259c9de560dSAlex Tomas
2260c9de560dSAlex Tomas /*
2261c9de560dSAlex Tomas * Let's check whether the chuck is good enough
2262c9de560dSAlex Tomas */
2263c9de560dSAlex Tomas if (ex->fe_len == gex->fe_len) {
2264c9de560dSAlex Tomas *bex = *ex;
2265c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2266c9de560dSAlex Tomas return;
2267c9de560dSAlex Tomas }
2268c9de560dSAlex Tomas
2269c9de560dSAlex Tomas /*
2270c9de560dSAlex Tomas * If this is first found extent, just store it in the context
2271c9de560dSAlex Tomas */
2272c9de560dSAlex Tomas if (bex->fe_len == 0) {
2273c9de560dSAlex Tomas *bex = *ex;
2274c9de560dSAlex Tomas return;
2275c9de560dSAlex Tomas }
2276c9de560dSAlex Tomas
2277c9de560dSAlex Tomas /*
2278c9de560dSAlex Tomas * If new found extent is better, store it in the context
2279c9de560dSAlex Tomas */
2280c9de560dSAlex Tomas if (bex->fe_len < gex->fe_len) {
2281c9de560dSAlex Tomas /* if the request isn't satisfied, any found extent
2282c9de560dSAlex Tomas * larger than previous best one is better */
2283c9de560dSAlex Tomas if (ex->fe_len > bex->fe_len)
2284c9de560dSAlex Tomas *bex = *ex;
2285c9de560dSAlex Tomas } else if (ex->fe_len > gex->fe_len) {
2286c9de560dSAlex Tomas /* if the request is satisfied, then we try to find
2287c9de560dSAlex Tomas * an extent that still satisfy the request, but is
2288c9de560dSAlex Tomas * smaller than previous one */
2289c9de560dSAlex Tomas if (ex->fe_len < bex->fe_len)
2290c9de560dSAlex Tomas *bex = *ex;
2291c9de560dSAlex Tomas }
2292c9de560dSAlex Tomas
2293c9de560dSAlex Tomas ext4_mb_check_limits(ac, e4b, 0);
2294c9de560dSAlex Tomas }
2295c9de560dSAlex Tomas
2296089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_try_best_found(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)229785b67ffbSKemeng Shi void ext4_mb_try_best_found(struct ext4_allocation_context *ac,
2298c9de560dSAlex Tomas struct ext4_buddy *e4b)
2299c9de560dSAlex Tomas {
2300c9de560dSAlex Tomas struct ext4_free_extent ex = ac->ac_b_ex;
2301c9de560dSAlex Tomas ext4_group_t group = ex.fe_group;
2302c9de560dSAlex Tomas int max;
2303c9de560dSAlex Tomas int err;
2304c9de560dSAlex Tomas
2305c9de560dSAlex Tomas BUG_ON(ex.fe_len <= 0);
2306c9de560dSAlex Tomas err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
2307c9de560dSAlex Tomas if (err)
230885b67ffbSKemeng Shi return;
2309c9de560dSAlex Tomas
2310c9de560dSAlex Tomas ext4_lock_group(ac->ac_sb, group);
23110184747bSBaokun Li if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
23120184747bSBaokun Li goto out;
23130184747bSBaokun Li
231415c006a2SRobin Dong max = mb_find_extent(e4b, ex.fe_start, ex.fe_len, &ex);
2315c9de560dSAlex Tomas
2316c9de560dSAlex Tomas if (max > 0) {
2317c9de560dSAlex Tomas ac->ac_b_ex = ex;
2318c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2319c9de560dSAlex Tomas }
2320c9de560dSAlex Tomas
23210184747bSBaokun Li out:
2322c9de560dSAlex Tomas ext4_unlock_group(ac->ac_sb, group);
2323e39e07fdSJing Zhang ext4_mb_unload_buddy(e4b);
2324c9de560dSAlex Tomas }
2325c9de560dSAlex Tomas
2326089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_find_by_goal(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)2327089ceeccSEric Sandeen int ext4_mb_find_by_goal(struct ext4_allocation_context *ac,
2328c9de560dSAlex Tomas struct ext4_buddy *e4b)
2329c9de560dSAlex Tomas {
2330c9de560dSAlex Tomas ext4_group_t group = ac->ac_g_ex.fe_group;
2331c9de560dSAlex Tomas int max;
2332c9de560dSAlex Tomas int err;
2333c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
2334838cd0cfSYongqiang Yang struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2335c9de560dSAlex Tomas struct ext4_free_extent ex;
2336c9de560dSAlex Tomas
23375354b2afSTheodore Ts'o if (!grp)
23385354b2afSTheodore Ts'o return -EFSCORRUPTED;
233901e4ca29SKemeng Shi if (!(ac->ac_flags & (EXT4_MB_HINT_TRY_GOAL | EXT4_MB_HINT_GOAL_ONLY)))
2340c9de560dSAlex Tomas return 0;
2341838cd0cfSYongqiang Yang if (grp->bb_free == 0)
2342838cd0cfSYongqiang Yang return 0;
2343c9de560dSAlex Tomas
2344c9de560dSAlex Tomas err = ext4_mb_load_buddy(ac->ac_sb, group, e4b);
2345c9de560dSAlex Tomas if (err)
2346c9de560dSAlex Tomas return err;
2347c9de560dSAlex Tomas
2348c9de560dSAlex Tomas ext4_lock_group(ac->ac_sb, group);
2349d3bbe77aSBaokun Li if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
2350d3bbe77aSBaokun Li goto out;
2351d3bbe77aSBaokun Li
235215c006a2SRobin Dong max = mb_find_extent(e4b, ac->ac_g_ex.fe_start,
2353c9de560dSAlex Tomas ac->ac_g_ex.fe_len, &ex);
2354ab0c00fcSTheodore Ts'o ex.fe_logical = 0xDEADFA11; /* debug value */
2355c9de560dSAlex Tomas
2356c3defd99SKemeng Shi if (max >= ac->ac_g_ex.fe_len &&
2357c3defd99SKemeng Shi ac->ac_g_ex.fe_len == EXT4_B2C(sbi, sbi->s_stripe)) {
2358c9de560dSAlex Tomas ext4_fsblk_t start;
2359c9de560dSAlex Tomas
236099c515e3SKemeng Shi start = ext4_grp_offs_to_block(ac->ac_sb, &ex);
2361c9de560dSAlex Tomas /* use do_div to get remainder (would be 64-bit modulo) */
2362c9de560dSAlex Tomas if (do_div(start, sbi->s_stripe) == 0) {
2363c9de560dSAlex Tomas ac->ac_found++;
2364c9de560dSAlex Tomas ac->ac_b_ex = ex;
2365c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2366c9de560dSAlex Tomas }
2367c9de560dSAlex Tomas } else if (max >= ac->ac_g_ex.fe_len) {
2368c9de560dSAlex Tomas BUG_ON(ex.fe_len <= 0);
2369c9de560dSAlex Tomas BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
2370c9de560dSAlex Tomas BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
2371c9de560dSAlex Tomas ac->ac_found++;
2372c9de560dSAlex Tomas ac->ac_b_ex = ex;
2373c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2374c9de560dSAlex Tomas } else if (max > 0 && (ac->ac_flags & EXT4_MB_HINT_MERGE)) {
2375c9de560dSAlex Tomas /* Sometimes, caller may want to merge even small
2376c9de560dSAlex Tomas * number of blocks to an existing extent */
2377c9de560dSAlex Tomas BUG_ON(ex.fe_len <= 0);
2378c9de560dSAlex Tomas BUG_ON(ex.fe_group != ac->ac_g_ex.fe_group);
2379c9de560dSAlex Tomas BUG_ON(ex.fe_start != ac->ac_g_ex.fe_start);
2380c9de560dSAlex Tomas ac->ac_found++;
2381c9de560dSAlex Tomas ac->ac_b_ex = ex;
2382c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2383c9de560dSAlex Tomas }
2384d3bbe77aSBaokun Li out:
2385c9de560dSAlex Tomas ext4_unlock_group(ac->ac_sb, group);
2386e39e07fdSJing Zhang ext4_mb_unload_buddy(e4b);
2387c9de560dSAlex Tomas
2388c9de560dSAlex Tomas return 0;
2389c9de560dSAlex Tomas }
2390c9de560dSAlex Tomas
2391c9de560dSAlex Tomas /*
2392c9de560dSAlex Tomas * The routine scans buddy structures (not bitmap!) from given order
2393c9de560dSAlex Tomas * to max order and tries to find big enough chunk to satisfy the req
2394c9de560dSAlex Tomas */
2395089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_simple_scan_group(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)2396089ceeccSEric Sandeen void ext4_mb_simple_scan_group(struct ext4_allocation_context *ac,
2397c9de560dSAlex Tomas struct ext4_buddy *e4b)
2398c9de560dSAlex Tomas {
2399c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
2400c9de560dSAlex Tomas struct ext4_group_info *grp = e4b->bd_info;
2401c9de560dSAlex Tomas void *buddy;
2402c9de560dSAlex Tomas int i;
2403c9de560dSAlex Tomas int k;
2404c9de560dSAlex Tomas int max;
2405c9de560dSAlex Tomas
2406c9de560dSAlex Tomas BUG_ON(ac->ac_2order <= 0);
24074b68f6dfSHarshad Shirwadkar for (i = ac->ac_2order; i < MB_NUM_ORDERS(sb); i++) {
2408c9de560dSAlex Tomas if (grp->bb_counters[i] == 0)
2409c9de560dSAlex Tomas continue;
2410c9de560dSAlex Tomas
2411c9de560dSAlex Tomas buddy = mb_find_buddy(e4b, i, &max);
241219b8b035STheodore Ts'o if (WARN_RATELIMIT(buddy == NULL,
241319b8b035STheodore Ts'o "ext4: mb_simple_scan_group: mb_find_buddy failed, (%d)\n", i))
241419b8b035STheodore Ts'o continue;
2415c9de560dSAlex Tomas
2416ffad0a44SAneesh Kumar K.V k = mb_find_next_zero_bit(buddy, max, 0);
2417eb576086SDmitry Monakhov if (k >= max) {
2418eb576086SDmitry Monakhov ext4_grp_locked_error(ac->ac_sb, e4b->bd_group, 0, 0,
2419eb576086SDmitry Monakhov "%d free clusters of order %d. But found 0",
2420eb576086SDmitry Monakhov grp->bb_counters[i], i);
2421eb576086SDmitry Monakhov ext4_mark_group_bitmap_corrupted(ac->ac_sb,
2422eb576086SDmitry Monakhov e4b->bd_group,
2423eb576086SDmitry Monakhov EXT4_GROUP_INFO_BBITMAP_CORRUPT);
2424eb576086SDmitry Monakhov break;
2425eb576086SDmitry Monakhov }
2426c9de560dSAlex Tomas ac->ac_found++;
2427fdd9a009SOjaswin Mujoo ac->ac_cX_found[ac->ac_criteria]++;
2428c9de560dSAlex Tomas
2429c9de560dSAlex Tomas ac->ac_b_ex.fe_len = 1 << i;
2430c9de560dSAlex Tomas ac->ac_b_ex.fe_start = k << i;
2431c9de560dSAlex Tomas ac->ac_b_ex.fe_group = e4b->bd_group;
2432c9de560dSAlex Tomas
2433c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2434c9de560dSAlex Tomas
243553f86b17SRitesh Harjani BUG_ON(ac->ac_f_ex.fe_len != ac->ac_g_ex.fe_len);
2436c9de560dSAlex Tomas
2437c9de560dSAlex Tomas if (EXT4_SB(sb)->s_mb_stats)
2438c9de560dSAlex Tomas atomic_inc(&EXT4_SB(sb)->s_bal_2orders);
2439c9de560dSAlex Tomas
2440c9de560dSAlex Tomas break;
2441c9de560dSAlex Tomas }
2442c9de560dSAlex Tomas }
2443c9de560dSAlex Tomas
2444c9de560dSAlex Tomas /*
2445c9de560dSAlex Tomas * The routine scans the group and measures all found extents.
2446c9de560dSAlex Tomas * In order to optimize scanning, caller must pass number of
2447c9de560dSAlex Tomas * free blocks in the group, so the routine can know upper limit.
2448c9de560dSAlex Tomas */
2449089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_complex_scan_group(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)2450089ceeccSEric Sandeen void ext4_mb_complex_scan_group(struct ext4_allocation_context *ac,
2451c9de560dSAlex Tomas struct ext4_buddy *e4b)
2452c9de560dSAlex Tomas {
2453c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
2454c5e8f3f3STheodore Ts'o void *bitmap = e4b->bd_bitmap;
2455c9de560dSAlex Tomas struct ext4_free_extent ex;
24561b420011SOjaswin Mujoo int i, j, freelen;
2457c9de560dSAlex Tomas int free;
2458c9de560dSAlex Tomas
2459c9de560dSAlex Tomas free = e4b->bd_info->bb_free;
2460907ea529STheodore Ts'o if (WARN_ON(free <= 0))
2461907ea529STheodore Ts'o return;
2462c9de560dSAlex Tomas
2463c9de560dSAlex Tomas i = e4b->bd_info->bb_first_free;
2464c9de560dSAlex Tomas
2465c9de560dSAlex Tomas while (free && ac->ac_status == AC_STATUS_CONTINUE) {
2466ffad0a44SAneesh Kumar K.V i = mb_find_next_zero_bit(bitmap,
24677137d7a4STheodore Ts'o EXT4_CLUSTERS_PER_GROUP(sb), i);
24687137d7a4STheodore Ts'o if (i >= EXT4_CLUSTERS_PER_GROUP(sb)) {
246926346ff6SAneesh Kumar K.V /*
2470e56eb659SAneesh Kumar K.V * IF we have corrupt bitmap, we won't find any
247126346ff6SAneesh Kumar K.V * free blocks even though group info says we
2472b483bb77SRandy Dunlap * have free blocks
247326346ff6SAneesh Kumar K.V */
2474e29136f8STheodore Ts'o ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
247553accfa9STheodore Ts'o "%d free clusters as per "
2476fde4d95aSTheodore Ts'o "group info. But bitmap says 0",
247726346ff6SAneesh Kumar K.V free);
2478736dedbbSWang Shilong ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2479736dedbbSWang Shilong EXT4_GROUP_INFO_BBITMAP_CORRUPT);
2480c9de560dSAlex Tomas break;
2481c9de560dSAlex Tomas }
2482c9de560dSAlex Tomas
2483304749c0SOjaswin Mujoo if (!ext4_mb_cr_expensive(ac->ac_criteria)) {
24841b420011SOjaswin Mujoo /*
2485f52f3d2bSOjaswin Mujoo * In CR_GOAL_LEN_FAST and CR_BEST_AVAIL_LEN, we are
2486f52f3d2bSOjaswin Mujoo * sure that this group will have a large enough
2487f52f3d2bSOjaswin Mujoo * continuous free extent, so skip over the smaller free
2488f52f3d2bSOjaswin Mujoo * extents
24891b420011SOjaswin Mujoo */
24901b420011SOjaswin Mujoo j = mb_find_next_bit(bitmap,
24911b420011SOjaswin Mujoo EXT4_CLUSTERS_PER_GROUP(sb), i);
24921b420011SOjaswin Mujoo freelen = j - i;
24931b420011SOjaswin Mujoo
24941b420011SOjaswin Mujoo if (freelen < ac->ac_g_ex.fe_len) {
24951b420011SOjaswin Mujoo i = j;
24961b420011SOjaswin Mujoo free -= freelen;
24971b420011SOjaswin Mujoo continue;
24981b420011SOjaswin Mujoo }
24991b420011SOjaswin Mujoo }
25001b420011SOjaswin Mujoo
250115c006a2SRobin Dong mb_find_extent(e4b, i, ac->ac_g_ex.fe_len, &ex);
2502907ea529STheodore Ts'o if (WARN_ON(ex.fe_len <= 0))
2503907ea529STheodore Ts'o break;
250426346ff6SAneesh Kumar K.V if (free < ex.fe_len) {
2505e29136f8STheodore Ts'o ext4_grp_locked_error(sb, e4b->bd_group, 0, 0,
250653accfa9STheodore Ts'o "%d free clusters as per "
2507fde4d95aSTheodore Ts'o "group info. But got %d blocks",
250826346ff6SAneesh Kumar K.V free, ex.fe_len);
2509736dedbbSWang Shilong ext4_mark_group_bitmap_corrupted(sb, e4b->bd_group,
2510736dedbbSWang Shilong EXT4_GROUP_INFO_BBITMAP_CORRUPT);
2511e56eb659SAneesh Kumar K.V /*
2512e56eb659SAneesh Kumar K.V * The number of free blocks differs. This mostly
2513e56eb659SAneesh Kumar K.V * indicate that the bitmap is corrupt. So exit
2514e56eb659SAneesh Kumar K.V * without claiming the space.
2515e56eb659SAneesh Kumar K.V */
2516e56eb659SAneesh Kumar K.V break;
251726346ff6SAneesh Kumar K.V }
2518ab0c00fcSTheodore Ts'o ex.fe_logical = 0xDEADC0DE; /* debug value */
2519c9de560dSAlex Tomas ext4_mb_measure_extent(ac, &ex, e4b);
2520c9de560dSAlex Tomas
2521c9de560dSAlex Tomas i += ex.fe_len;
2522c9de560dSAlex Tomas free -= ex.fe_len;
2523c9de560dSAlex Tomas }
2524c9de560dSAlex Tomas
2525c9de560dSAlex Tomas ext4_mb_check_limits(ac, e4b, 1);
2526c9de560dSAlex Tomas }
2527c9de560dSAlex Tomas
2528c9de560dSAlex Tomas /*
2529c9de560dSAlex Tomas * This is a special case for storages like raid5
2530506bf2d8SEric Sandeen * we try to find stripe-aligned chunks for stripe-size-multiple requests
2531c9de560dSAlex Tomas */
2532089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_scan_aligned(struct ext4_allocation_context * ac,struct ext4_buddy * e4b)2533089ceeccSEric Sandeen void ext4_mb_scan_aligned(struct ext4_allocation_context *ac,
2534c9de560dSAlex Tomas struct ext4_buddy *e4b)
2535c9de560dSAlex Tomas {
2536c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
2537c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
2538c5e8f3f3STheodore Ts'o void *bitmap = e4b->bd_bitmap;
2539c9de560dSAlex Tomas struct ext4_free_extent ex;
2540c9de560dSAlex Tomas ext4_fsblk_t first_group_block;
2541c9de560dSAlex Tomas ext4_fsblk_t a;
2542c3defd99SKemeng Shi ext4_grpblk_t i, stripe;
2543c9de560dSAlex Tomas int max;
2544c9de560dSAlex Tomas
2545c9de560dSAlex Tomas BUG_ON(sbi->s_stripe == 0);
2546c9de560dSAlex Tomas
2547c9de560dSAlex Tomas /* find first stripe-aligned block in group */
25485661bd68SAkinobu Mita first_group_block = ext4_group_first_block_no(sb, e4b->bd_group);
25495661bd68SAkinobu Mita
2550c9de560dSAlex Tomas a = first_group_block + sbi->s_stripe - 1;
2551c9de560dSAlex Tomas do_div(a, sbi->s_stripe);
2552c9de560dSAlex Tomas i = (a * sbi->s_stripe) - first_group_block;
2553c9de560dSAlex Tomas
2554c3defd99SKemeng Shi stripe = EXT4_B2C(sbi, sbi->s_stripe);
2555c3defd99SKemeng Shi i = EXT4_B2C(sbi, i);
25567137d7a4STheodore Ts'o while (i < EXT4_CLUSTERS_PER_GROUP(sb)) {
2557c9de560dSAlex Tomas if (!mb_test_bit(i, bitmap)) {
2558c3defd99SKemeng Shi max = mb_find_extent(e4b, i, stripe, &ex);
2559c3defd99SKemeng Shi if (max >= stripe) {
2560c9de560dSAlex Tomas ac->ac_found++;
2561fdd9a009SOjaswin Mujoo ac->ac_cX_found[ac->ac_criteria]++;
2562ab0c00fcSTheodore Ts'o ex.fe_logical = 0xDEADF00D; /* debug value */
2563c9de560dSAlex Tomas ac->ac_b_ex = ex;
2564c9de560dSAlex Tomas ext4_mb_use_best_found(ac, e4b);
2565c9de560dSAlex Tomas break;
2566c9de560dSAlex Tomas }
2567c9de560dSAlex Tomas }
2568c3defd99SKemeng Shi i += stripe;
2569c9de560dSAlex Tomas }
2570c9de560dSAlex Tomas }
2571c9de560dSAlex Tomas
257242ac1848SLukas Czerner /*
25738ef123feSRitesh Harjani * This is also called BEFORE we load the buddy bitmap.
257442ac1848SLukas Czerner * Returns either 1 or 0 indicating that the group is either suitable
25758ef123feSRitesh Harjani * for the allocation or not.
257642ac1848SLukas Czerner */
ext4_mb_good_group(struct ext4_allocation_context * ac,ext4_group_t group,enum criteria cr)25778ef123feSRitesh Harjani static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
25784eb7a4a1SOjaswin Mujoo ext4_group_t group, enum criteria cr)
2579c9de560dSAlex Tomas {
25808ef123feSRitesh Harjani ext4_grpblk_t free, fragments;
2581a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(ac->ac_sb));
2582c9de560dSAlex Tomas struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
2583c9de560dSAlex Tomas
2584f52f3d2bSOjaswin Mujoo BUG_ON(cr < CR_POWER2_ALIGNED || cr >= EXT4_MB_NUM_CRS);
25858a57d9d6SCurt Wohlgemuth
2586a9ce5993SKemeng Shi if (unlikely(!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
25878ef123feSRitesh Harjani return false;
258801fc48e8STheodore Ts'o
2589dddcd2f9Sbrookxu free = grp->bb_free;
2590dddcd2f9Sbrookxu if (free == 0)
25918ef123feSRitesh Harjani return false;
2592c9de560dSAlex Tomas
2593c9de560dSAlex Tomas fragments = grp->bb_fragments;
2594c9de560dSAlex Tomas if (fragments == 0)
25958ef123feSRitesh Harjani return false;
2596c9de560dSAlex Tomas
2597c9de560dSAlex Tomas switch (cr) {
2598f52f3d2bSOjaswin Mujoo case CR_POWER2_ALIGNED:
2599c9de560dSAlex Tomas BUG_ON(ac->ac_2order == 0);
2600c9de560dSAlex Tomas
2601a4912123STheodore Ts'o /* Avoid using the first bg of a flexgroup for data files */
2602a4912123STheodore Ts'o if ((ac->ac_flags & EXT4_MB_HINT_DATA) &&
2603a4912123STheodore Ts'o (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) &&
2604a4912123STheodore Ts'o ((group % flex_size) == 0))
26058ef123feSRitesh Harjani return false;
2606a4912123STheodore Ts'o
2607dddcd2f9Sbrookxu if (free < ac->ac_g_ex.fe_len)
2608dddcd2f9Sbrookxu return false;
2609dddcd2f9Sbrookxu
26104b68f6dfSHarshad Shirwadkar if (ac->ac_2order >= MB_NUM_ORDERS(ac->ac_sb))
26118ef123feSRitesh Harjani return true;
261240ae3487STheodore Ts'o
261340ae3487STheodore Ts'o if (grp->bb_largest_free_order < ac->ac_2order)
26148ef123feSRitesh Harjani return false;
261540ae3487STheodore Ts'o
26168ef123feSRitesh Harjani return true;
2617f52f3d2bSOjaswin Mujoo case CR_GOAL_LEN_FAST:
2618f52f3d2bSOjaswin Mujoo case CR_BEST_AVAIL_LEN:
2619c9de560dSAlex Tomas if ((free / fragments) >= ac->ac_g_ex.fe_len)
26208ef123feSRitesh Harjani return true;
2621c9de560dSAlex Tomas break;
2622f52f3d2bSOjaswin Mujoo case CR_GOAL_LEN_SLOW:
2623c9de560dSAlex Tomas if (free >= ac->ac_g_ex.fe_len)
26248ef123feSRitesh Harjani return true;
2625c9de560dSAlex Tomas break;
2626f52f3d2bSOjaswin Mujoo case CR_ANY_FREE:
26278ef123feSRitesh Harjani return true;
2628c9de560dSAlex Tomas default:
2629c9de560dSAlex Tomas BUG();
2630c9de560dSAlex Tomas }
2631c9de560dSAlex Tomas
26328ef123feSRitesh Harjani return false;
26338ef123feSRitesh Harjani }
26348ef123feSRitesh Harjani
26358ef123feSRitesh Harjani /*
26368ef123feSRitesh Harjani * This could return negative error code if something goes wrong
26378ef123feSRitesh Harjani * during ext4_mb_init_group(). This should not be called with
26388ef123feSRitesh Harjani * ext4_lock_group() held.
2639a5fda113STheodore Ts'o *
2640a5fda113STheodore Ts'o * Note: because we are conditionally operating with the group lock in
2641a5fda113STheodore Ts'o * the EXT4_MB_STRICT_CHECK case, we need to fake out sparse in this
2642a5fda113STheodore Ts'o * function using __acquire and __release. This means we need to be
2643a5fda113STheodore Ts'o * super careful before messing with the error path handling via "goto
2644a5fda113STheodore Ts'o * out"!
26458ef123feSRitesh Harjani */
ext4_mb_good_group_nolock(struct ext4_allocation_context * ac,ext4_group_t group,enum criteria cr)26468ef123feSRitesh Harjani static int ext4_mb_good_group_nolock(struct ext4_allocation_context *ac,
26474eb7a4a1SOjaswin Mujoo ext4_group_t group, enum criteria cr)
26488ef123feSRitesh Harjani {
26498ef123feSRitesh Harjani struct ext4_group_info *grp = ext4_get_group_info(ac->ac_sb, group);
265099377830SRitesh Harjani struct super_block *sb = ac->ac_sb;
2651c1d2c7d4SAlex Zhuravlev struct ext4_sb_info *sbi = EXT4_SB(sb);
265299377830SRitesh Harjani bool should_lock = ac->ac_flags & EXT4_MB_STRICT_CHECK;
26538ef123feSRitesh Harjani ext4_grpblk_t free;
26548ef123feSRitesh Harjani int ret = 0;
26558ef123feSRitesh Harjani
26565354b2afSTheodore Ts'o if (!grp)
26575354b2afSTheodore Ts'o return -EFSCORRUPTED;
2658a6c75eafSHarshad Shirwadkar if (sbi->s_mb_stats)
2659a6c75eafSHarshad Shirwadkar atomic64_inc(&sbi->s_bal_cX_groups_considered[ac->ac_criteria]);
2660a5fda113STheodore Ts'o if (should_lock) {
266199377830SRitesh Harjani ext4_lock_group(sb, group);
2662a5fda113STheodore Ts'o __release(ext4_group_lock_ptr(sb, group));
2663a5fda113STheodore Ts'o }
26648ef123feSRitesh Harjani free = grp->bb_free;
26658ef123feSRitesh Harjani if (free == 0)
26668ef123feSRitesh Harjani goto out;
2667304749c0SOjaswin Mujoo /*
2668304749c0SOjaswin Mujoo * In all criterias except CR_ANY_FREE we try to avoid groups that
2669304749c0SOjaswin Mujoo * can't possibly satisfy the full goal request due to insufficient
2670304749c0SOjaswin Mujoo * free blocks.
2671304749c0SOjaswin Mujoo */
2672304749c0SOjaswin Mujoo if (cr < CR_ANY_FREE && free < ac->ac_g_ex.fe_len)
26738ef123feSRitesh Harjani goto out;
26748ef123feSRitesh Harjani if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
26758ef123feSRitesh Harjani goto out;
2676a5fda113STheodore Ts'o if (should_lock) {
2677a5fda113STheodore Ts'o __acquire(ext4_group_lock_ptr(sb, group));
267899377830SRitesh Harjani ext4_unlock_group(sb, group);
2679a5fda113STheodore Ts'o }
26808ef123feSRitesh Harjani
26818ef123feSRitesh Harjani /* We only do this if the grp has never been initialized */
26828ef123feSRitesh Harjani if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
2683c1d2c7d4SAlex Zhuravlev struct ext4_group_desc *gdp =
2684c1d2c7d4SAlex Zhuravlev ext4_get_group_desc(sb, group, NULL);
2685c1d2c7d4SAlex Zhuravlev int ret;
2686c1d2c7d4SAlex Zhuravlev
2687f52f3d2bSOjaswin Mujoo /*
2688f52f3d2bSOjaswin Mujoo * cr=CR_POWER2_ALIGNED/CR_GOAL_LEN_FAST is a very optimistic
2689f52f3d2bSOjaswin Mujoo * search to find large good chunks almost for free. If buddy
2690f52f3d2bSOjaswin Mujoo * data is not ready, then this optimization makes no sense. But
2691f52f3d2bSOjaswin Mujoo * we never skip the first block group in a flex_bg, since this
2692f52f3d2bSOjaswin Mujoo * gets used for metadata block allocation, and we want to make
2693f52f3d2bSOjaswin Mujoo * sure we locate metadata blocks in the first block group in
2694f52f3d2bSOjaswin Mujoo * the flex_bg if possible.
2695c1d2c7d4SAlex Zhuravlev */
2696304749c0SOjaswin Mujoo if (!ext4_mb_cr_expensive(cr) &&
2697c1d2c7d4SAlex Zhuravlev (!sbi->s_log_groups_per_flex ||
2698c1d2c7d4SAlex Zhuravlev ((group & ((1 << sbi->s_log_groups_per_flex) - 1)) != 0)) &&
2699c1d2c7d4SAlex Zhuravlev !(ext4_has_group_desc_csum(sb) &&
2700c1d2c7d4SAlex Zhuravlev (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))))
2701c1d2c7d4SAlex Zhuravlev return 0;
2702c1d2c7d4SAlex Zhuravlev ret = ext4_mb_init_group(sb, group, GFP_NOFS);
27038ef123feSRitesh Harjani if (ret)
27048ef123feSRitesh Harjani return ret;
27058ef123feSRitesh Harjani }
27068ef123feSRitesh Harjani
2707a5fda113STheodore Ts'o if (should_lock) {
270899377830SRitesh Harjani ext4_lock_group(sb, group);
2709a5fda113STheodore Ts'o __release(ext4_group_lock_ptr(sb, group));
2710a5fda113STheodore Ts'o }
27118ef123feSRitesh Harjani ret = ext4_mb_good_group(ac, group, cr);
27128ef123feSRitesh Harjani out:
2713a5fda113STheodore Ts'o if (should_lock) {
2714a5fda113STheodore Ts'o __acquire(ext4_group_lock_ptr(sb, group));
271599377830SRitesh Harjani ext4_unlock_group(sb, group);
2716a5fda113STheodore Ts'o }
27178ef123feSRitesh Harjani return ret;
2718c9de560dSAlex Tomas }
2719c9de560dSAlex Tomas
2720cfd73237SAlex Zhuravlev /*
2721cfd73237SAlex Zhuravlev * Start prefetching @nr block bitmaps starting at @group.
2722cfd73237SAlex Zhuravlev * Return the next group which needs to be prefetched.
2723cfd73237SAlex Zhuravlev */
ext4_mb_prefetch(struct super_block * sb,ext4_group_t group,unsigned int nr,int * cnt)27243d392b26STheodore Ts'o ext4_group_t ext4_mb_prefetch(struct super_block *sb, ext4_group_t group,
2725cfd73237SAlex Zhuravlev unsigned int nr, int *cnt)
2726cfd73237SAlex Zhuravlev {
2727cfd73237SAlex Zhuravlev ext4_group_t ngroups = ext4_get_groups_count(sb);
2728cfd73237SAlex Zhuravlev struct buffer_head *bh;
2729cfd73237SAlex Zhuravlev struct blk_plug plug;
2730cfd73237SAlex Zhuravlev
2731cfd73237SAlex Zhuravlev blk_start_plug(&plug);
2732cfd73237SAlex Zhuravlev while (nr-- > 0) {
2733cfd73237SAlex Zhuravlev struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group,
2734cfd73237SAlex Zhuravlev NULL);
2735cfd73237SAlex Zhuravlev struct ext4_group_info *grp = ext4_get_group_info(sb, group);
2736cfd73237SAlex Zhuravlev
2737cfd73237SAlex Zhuravlev /*
2738cfd73237SAlex Zhuravlev * Prefetch block groups with free blocks; but don't
2739cfd73237SAlex Zhuravlev * bother if it is marked uninitialized on disk, since
2740cfd73237SAlex Zhuravlev * it won't require I/O to read. Also only try to
2741cfd73237SAlex Zhuravlev * prefetch once, so we avoid getblk() call, which can
2742cfd73237SAlex Zhuravlev * be expensive.
2743cfd73237SAlex Zhuravlev */
27445354b2afSTheodore Ts'o if (gdp && grp && !EXT4_MB_GRP_TEST_AND_SET_READ(grp) &&
2745cfd73237SAlex Zhuravlev EXT4_MB_GRP_NEED_INIT(grp) &&
27463c629604SOjaswin Mujoo ext4_free_group_clusters(sb, gdp) > 0 ) {
2747cfd73237SAlex Zhuravlev bh = ext4_read_block_bitmap_nowait(sb, group, true);
2748cfd73237SAlex Zhuravlev if (bh && !IS_ERR(bh)) {
2749cfd73237SAlex Zhuravlev if (!buffer_uptodate(bh) && cnt)
2750cfd73237SAlex Zhuravlev (*cnt)++;
2751cfd73237SAlex Zhuravlev brelse(bh);
2752cfd73237SAlex Zhuravlev }
2753cfd73237SAlex Zhuravlev }
2754cfd73237SAlex Zhuravlev if (++group >= ngroups)
2755cfd73237SAlex Zhuravlev group = 0;
2756cfd73237SAlex Zhuravlev }
2757cfd73237SAlex Zhuravlev blk_finish_plug(&plug);
2758cfd73237SAlex Zhuravlev return group;
2759cfd73237SAlex Zhuravlev }
2760cfd73237SAlex Zhuravlev
2761cfd73237SAlex Zhuravlev /*
2762cfd73237SAlex Zhuravlev * Prefetching reads the block bitmap into the buffer cache; but we
2763cfd73237SAlex Zhuravlev * need to make sure that the buddy bitmap in the page cache has been
2764cfd73237SAlex Zhuravlev * initialized. Note that ext4_mb_init_group() will block if the I/O
2765cfd73237SAlex Zhuravlev * is not yet completed, or indeed if it was not initiated by
2766cfd73237SAlex Zhuravlev * ext4_mb_prefetch did not start the I/O.
2767cfd73237SAlex Zhuravlev *
2768cfd73237SAlex Zhuravlev * TODO: We should actually kick off the buddy bitmap setup in a work
2769cfd73237SAlex Zhuravlev * queue when the buffer I/O is completed, so that we don't block
2770cfd73237SAlex Zhuravlev * waiting for the block allocation bitmap read to finish when
2771cfd73237SAlex Zhuravlev * ext4_mb_prefetch_fini is called from ext4_mb_regular_allocator().
2772cfd73237SAlex Zhuravlev */
ext4_mb_prefetch_fini(struct super_block * sb,ext4_group_t group,unsigned int nr)27733d392b26STheodore Ts'o void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
2774cfd73237SAlex Zhuravlev unsigned int nr)
2775cfd73237SAlex Zhuravlev {
277622fab984SKemeng Shi struct ext4_group_desc *gdp;
277722fab984SKemeng Shi struct ext4_group_info *grp;
2778cfd73237SAlex Zhuravlev
277922fab984SKemeng Shi while (nr-- > 0) {
2780cfd73237SAlex Zhuravlev if (!group)
2781cfd73237SAlex Zhuravlev group = ext4_get_groups_count(sb);
2782cfd73237SAlex Zhuravlev group--;
278322fab984SKemeng Shi gdp = ext4_get_group_desc(sb, group, NULL);
2784cfd73237SAlex Zhuravlev grp = ext4_get_group_info(sb, group);
2785cfd73237SAlex Zhuravlev
27865354b2afSTheodore Ts'o if (grp && gdp && EXT4_MB_GRP_NEED_INIT(grp) &&
27873c629604SOjaswin Mujoo ext4_free_group_clusters(sb, gdp) > 0) {
2788cfd73237SAlex Zhuravlev if (ext4_mb_init_group(sb, group, GFP_NOFS))
2789cfd73237SAlex Zhuravlev break;
2790cfd73237SAlex Zhuravlev }
2791cfd73237SAlex Zhuravlev }
2792cfd73237SAlex Zhuravlev }
2793cfd73237SAlex Zhuravlev
27944ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_regular_allocator(struct ext4_allocation_context * ac)27954ddfef7bSEric Sandeen ext4_mb_regular_allocator(struct ext4_allocation_context *ac)
2796c9de560dSAlex Tomas {
2797cfd73237SAlex Zhuravlev ext4_group_t prefetch_grp = 0, ngroups, group, i;
27984c0cfebdSTheodore Ts'o enum criteria new_cr, cr = CR_GOAL_LEN_FAST;
279942ac1848SLukas Czerner int err = 0, first_err = 0;
2800cfd73237SAlex Zhuravlev unsigned int nr = 0, prefetch_ios = 0;
2801c9de560dSAlex Tomas struct ext4_sb_info *sbi;
2802c9de560dSAlex Tomas struct super_block *sb;
2803c9de560dSAlex Tomas struct ext4_buddy e4b;
280466d5e027Sbrookxu int lost;
2805c9de560dSAlex Tomas
2806c9de560dSAlex Tomas sb = ac->ac_sb;
2807c9de560dSAlex Tomas sbi = EXT4_SB(sb);
28088df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb);
2809fb0a387dSEric Sandeen /* non-extent files are limited to low blocks/groups */
281012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)))
2811fb0a387dSEric Sandeen ngroups = sbi->s_blockfile_groups;
2812fb0a387dSEric Sandeen
2813c9de560dSAlex Tomas BUG_ON(ac->ac_status == AC_STATUS_FOUND);
2814c9de560dSAlex Tomas
2815c9de560dSAlex Tomas /* first, try the goal */
2816c9de560dSAlex Tomas err = ext4_mb_find_by_goal(ac, &e4b);
2817c9de560dSAlex Tomas if (err || ac->ac_status == AC_STATUS_FOUND)
2818c9de560dSAlex Tomas goto out;
2819c9de560dSAlex Tomas
2820c9de560dSAlex Tomas if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
2821c9de560dSAlex Tomas goto out;
2822c9de560dSAlex Tomas
2823c9de560dSAlex Tomas /*
2824e9a3cd48Sbrookxu * ac->ac_2order is set only if the fe_len is a power of 2
28254eea9fbeSKemeng Shi * if ac->ac_2order is set we also set criteria to CR_POWER2_ALIGNED
28264eea9fbeSKemeng Shi * so that we try exact allocation using buddy.
2827c9de560dSAlex Tomas */
2828c9de560dSAlex Tomas i = fls(ac->ac_g_ex.fe_len);
2829c9de560dSAlex Tomas ac->ac_2order = 0;
2830c9de560dSAlex Tomas /*
2831c9de560dSAlex Tomas * We search using buddy data only if the order of the request
2832c9de560dSAlex Tomas * is greater than equal to the sbi_s_mb_order2_reqs
2833b713a5ecSTheodore Ts'o * You can tune it via /sys/fs/ext4/<partition>/mb_order2_req
2834d9b22cf9SJan Kara * We also support searching for power-of-two requests only for
2835d9b22cf9SJan Kara * requests upto maximum buddy size we have constructed.
2836c9de560dSAlex Tomas */
28374b68f6dfSHarshad Shirwadkar if (i >= sbi->s_mb_order2_reqs && i <= MB_NUM_ORDERS(sb)) {
2838bb60caa2SKemeng Shi if (is_power_of_2(ac->ac_g_ex.fe_len))
28391a5d5e5dSJeremy Cline ac->ac_2order = array_index_nospec(i - 1,
28404b68f6dfSHarshad Shirwadkar MB_NUM_ORDERS(sb));
2841c9de560dSAlex Tomas }
2842c9de560dSAlex Tomas
28434ba74d00STheodore Ts'o /* if stream allocation is enabled, use global goal */
28444ba74d00STheodore Ts'o if (ac->ac_flags & EXT4_MB_STREAM_ALLOC) {
2845c9de560dSAlex Tomas /* TBD: may be hot point */
2846c9de560dSAlex Tomas spin_lock(&sbi->s_md_lock);
2847c9de560dSAlex Tomas ac->ac_g_ex.fe_group = sbi->s_mb_last_group;
2848c9de560dSAlex Tomas ac->ac_g_ex.fe_start = sbi->s_mb_last_start;
2849c9de560dSAlex Tomas spin_unlock(&sbi->s_md_lock);
2850c9de560dSAlex Tomas }
28514ba74d00STheodore Ts'o
2852c9de560dSAlex Tomas /*
28534c0cfebdSTheodore Ts'o * Let's just scan groups to find more-less suitable blocks We
28544c0cfebdSTheodore Ts'o * start with CR_GOAL_LEN_FAST, unless it is power of 2
28554c0cfebdSTheodore Ts'o * aligned, in which case let's do that faster approach first.
2856c9de560dSAlex Tomas */
28574c0cfebdSTheodore Ts'o if (ac->ac_2order)
28584c0cfebdSTheodore Ts'o cr = CR_POWER2_ALIGNED;
2859c9de560dSAlex Tomas repeat:
28604eb7a4a1SOjaswin Mujoo for (; cr < EXT4_MB_NUM_CRS && ac->ac_status == AC_STATUS_CONTINUE; cr++) {
2861c9de560dSAlex Tomas ac->ac_criteria = cr;
2862ed8f9c75SAneesh Kumar K.V /*
2863ed8f9c75SAneesh Kumar K.V * searching for the right group start
2864ed8f9c75SAneesh Kumar K.V * from the goal value specified
2865ed8f9c75SAneesh Kumar K.V */
2866ed8f9c75SAneesh Kumar K.V group = ac->ac_g_ex.fe_group;
2867196e402aSHarshad Shirwadkar ac->ac_groups_linear_remaining = sbi->s_mb_max_linear_groups;
2868cfd73237SAlex Zhuravlev prefetch_grp = group;
2869ed8f9c75SAneesh Kumar K.V
28704fca50d4SJan Kara for (i = 0, new_cr = cr; i < ngroups; i++,
28714fca50d4SJan Kara ext4_mb_choose_next_group(ac, &new_cr, &group, ngroups)) {
28724fca50d4SJan Kara int ret = 0;
2873196e402aSHarshad Shirwadkar
28742ed5724dSTheodore Ts'o cond_resched();
2875196e402aSHarshad Shirwadkar if (new_cr != cr) {
2876196e402aSHarshad Shirwadkar cr = new_cr;
2877196e402aSHarshad Shirwadkar goto repeat;
2878196e402aSHarshad Shirwadkar }
2879c9de560dSAlex Tomas
2880cfd73237SAlex Zhuravlev /*
2881cfd73237SAlex Zhuravlev * Batch reads of the block allocation bitmaps
2882cfd73237SAlex Zhuravlev * to get multiple READs in flight; limit
28834eea9fbeSKemeng Shi * prefetching at inexpensive CR, otherwise mballoc
28844eea9fbeSKemeng Shi * can spend a lot of time loading imperfect groups
2885cfd73237SAlex Zhuravlev */
2886cfd73237SAlex Zhuravlev if ((prefetch_grp == group) &&
2887304749c0SOjaswin Mujoo (ext4_mb_cr_expensive(cr) ||
2888cfd73237SAlex Zhuravlev prefetch_ios < sbi->s_mb_prefetch_limit)) {
2889cfd73237SAlex Zhuravlev nr = sbi->s_mb_prefetch;
2890cfd73237SAlex Zhuravlev if (ext4_has_feature_flex_bg(sb)) {
289182ef1370SChunguang Xu nr = 1 << sbi->s_log_groups_per_flex;
289282ef1370SChunguang Xu nr -= group & (nr - 1);
289382ef1370SChunguang Xu nr = min(nr, sbi->s_mb_prefetch);
2894cfd73237SAlex Zhuravlev }
2895cfd73237SAlex Zhuravlev prefetch_grp = ext4_mb_prefetch(sb, group,
2896cfd73237SAlex Zhuravlev nr, &prefetch_ios);
2897cfd73237SAlex Zhuravlev }
2898cfd73237SAlex Zhuravlev
28998a57d9d6SCurt Wohlgemuth /* This now checks without needing the buddy page */
29008ef123feSRitesh Harjani ret = ext4_mb_good_group_nolock(ac, group, cr);
290142ac1848SLukas Czerner if (ret <= 0) {
290242ac1848SLukas Czerner if (!first_err)
290342ac1848SLukas Czerner first_err = ret;
2904c9de560dSAlex Tomas continue;
290542ac1848SLukas Czerner }
2906c9de560dSAlex Tomas
2907c9de560dSAlex Tomas err = ext4_mb_load_buddy(sb, group, &e4b);
2908c9de560dSAlex Tomas if (err)
2909c9de560dSAlex Tomas goto out;
2910c9de560dSAlex Tomas
2911c9de560dSAlex Tomas ext4_lock_group(sb, group);
29128a57d9d6SCurt Wohlgemuth
29138a57d9d6SCurt Wohlgemuth /*
29148a57d9d6SCurt Wohlgemuth * We need to check again after locking the
29158a57d9d6SCurt Wohlgemuth * block group
29168a57d9d6SCurt Wohlgemuth */
291742ac1848SLukas Czerner ret = ext4_mb_good_group(ac, group, cr);
29188ef123feSRitesh Harjani if (ret == 0) {
2919c9de560dSAlex Tomas ext4_unlock_group(sb, group);
2920e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
2921c9de560dSAlex Tomas continue;
2922c9de560dSAlex Tomas }
2923c9de560dSAlex Tomas
2924c9de560dSAlex Tomas ac->ac_groups_scanned++;
2925f52f3d2bSOjaswin Mujoo if (cr == CR_POWER2_ALIGNED)
2926c9de560dSAlex Tomas ext4_mb_simple_scan_group(ac, &e4b);
2927f52f3d2bSOjaswin Mujoo else if ((cr == CR_GOAL_LEN_FAST ||
2928f52f3d2bSOjaswin Mujoo cr == CR_BEST_AVAIL_LEN) &&
2929f52f3d2bSOjaswin Mujoo sbi->s_stripe &&
2930c3defd99SKemeng Shi !(ac->ac_g_ex.fe_len %
2931c3defd99SKemeng Shi EXT4_B2C(sbi, sbi->s_stripe)))
2932c9de560dSAlex Tomas ext4_mb_scan_aligned(ac, &e4b);
2933c9de560dSAlex Tomas else
2934c9de560dSAlex Tomas ext4_mb_complex_scan_group(ac, &e4b);
2935c9de560dSAlex Tomas
2936c9de560dSAlex Tomas ext4_unlock_group(sb, group);
2937e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
2938c9de560dSAlex Tomas
2939c9de560dSAlex Tomas if (ac->ac_status != AC_STATUS_CONTINUE)
2940c9de560dSAlex Tomas break;
2941c9de560dSAlex Tomas }
2942a6c75eafSHarshad Shirwadkar /* Processed all groups and haven't found blocks */
2943a6c75eafSHarshad Shirwadkar if (sbi->s_mb_stats && i == ngroups)
2944a6c75eafSHarshad Shirwadkar atomic64_inc(&sbi->s_bal_cX_failed[cr]);
29457e170922SOjaswin Mujoo
2946f52f3d2bSOjaswin Mujoo if (i == ngroups && ac->ac_criteria == CR_BEST_AVAIL_LEN)
29477e170922SOjaswin Mujoo /* Reset goal length to original goal length before
2948f52f3d2bSOjaswin Mujoo * falling into CR_GOAL_LEN_SLOW */
29497e170922SOjaswin Mujoo ac->ac_g_ex.fe_len = ac->ac_orig_goal_len;
2950c9de560dSAlex Tomas }
2951c9de560dSAlex Tomas
2952c9de560dSAlex Tomas if (ac->ac_b_ex.fe_len > 0 && ac->ac_status != AC_STATUS_FOUND &&
2953c9de560dSAlex Tomas !(ac->ac_flags & EXT4_MB_HINT_FIRST)) {
2954c9de560dSAlex Tomas /*
2955c9de560dSAlex Tomas * We've been searching too long. Let's try to allocate
2956c9de560dSAlex Tomas * the best chunk we've found so far
2957c9de560dSAlex Tomas */
2958c9de560dSAlex Tomas ext4_mb_try_best_found(ac, &e4b);
2959c9de560dSAlex Tomas if (ac->ac_status != AC_STATUS_FOUND) {
2960c9de560dSAlex Tomas /*
2961c9de560dSAlex Tomas * Someone more lucky has already allocated it.
2962c9de560dSAlex Tomas * The only thing we can do is just take first
2963c9de560dSAlex Tomas * found block(s)
2964c9de560dSAlex Tomas */
296566d5e027Sbrookxu lost = atomic_inc_return(&sbi->s_mb_lost_chunks);
296666d5e027Sbrookxu mb_debug(sb, "lost chunk, group: %u, start: %d, len: %d, lost: %d\n",
2967c55ee7d2Sbrookxu ac->ac_b_ex.fe_group, ac->ac_b_ex.fe_start,
2968c55ee7d2Sbrookxu ac->ac_b_ex.fe_len, lost);
2969c55ee7d2Sbrookxu
2970c9de560dSAlex Tomas ac->ac_b_ex.fe_group = 0;
2971c9de560dSAlex Tomas ac->ac_b_ex.fe_start = 0;
2972c9de560dSAlex Tomas ac->ac_b_ex.fe_len = 0;
2973c9de560dSAlex Tomas ac->ac_status = AC_STATUS_CONTINUE;
2974c9de560dSAlex Tomas ac->ac_flags |= EXT4_MB_HINT_FIRST;
2975f52f3d2bSOjaswin Mujoo cr = CR_ANY_FREE;
2976c9de560dSAlex Tomas goto repeat;
2977c9de560dSAlex Tomas }
2978c9de560dSAlex Tomas }
2979a6c75eafSHarshad Shirwadkar
2980a6c75eafSHarshad Shirwadkar if (sbi->s_mb_stats && ac->ac_status == AC_STATUS_FOUND)
2981a6c75eafSHarshad Shirwadkar atomic64_inc(&sbi->s_bal_cX_hits[ac->ac_criteria]);
2982c9de560dSAlex Tomas out:
298342ac1848SLukas Czerner if (!err && ac->ac_status != AC_STATUS_FOUND && first_err)
298442ac1848SLukas Czerner err = first_err;
2985bbc4ec77SRitesh Harjani
2986d3df1453SRitesh Harjani mb_debug(sb, "Best len %d, origin len %d, ac_status %u, ac_flags 0x%x, cr %d ret %d\n",
2987bbc4ec77SRitesh Harjani ac->ac_b_ex.fe_len, ac->ac_o_ex.fe_len, ac->ac_status,
2988bbc4ec77SRitesh Harjani ac->ac_flags, cr, err);
2989cfd73237SAlex Zhuravlev
2990cfd73237SAlex Zhuravlev if (nr)
2991cfd73237SAlex Zhuravlev ext4_mb_prefetch_fini(sb, prefetch_grp, nr);
2992cfd73237SAlex Zhuravlev
2993c9de560dSAlex Tomas return err;
2994c9de560dSAlex Tomas }
2995c9de560dSAlex Tomas
ext4_mb_seq_groups_start(struct seq_file * seq,loff_t * pos)2996c9de560dSAlex Tomas static void *ext4_mb_seq_groups_start(struct seq_file *seq, loff_t *pos)
2997c9de560dSAlex Tomas {
2998359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
2999c9de560dSAlex Tomas ext4_group_t group;
3000c9de560dSAlex Tomas
30018df9675fSTheodore Ts'o if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
3002c9de560dSAlex Tomas return NULL;
3003c9de560dSAlex Tomas group = *pos + 1;
3004a9df9a49STheodore Ts'o return (void *) ((unsigned long) group);
3005c9de560dSAlex Tomas }
3006c9de560dSAlex Tomas
ext4_mb_seq_groups_next(struct seq_file * seq,void * v,loff_t * pos)3007c9de560dSAlex Tomas static void *ext4_mb_seq_groups_next(struct seq_file *seq, void *v, loff_t *pos)
3008c9de560dSAlex Tomas {
3009359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
3010c9de560dSAlex Tomas ext4_group_t group;
3011c9de560dSAlex Tomas
3012c9de560dSAlex Tomas ++*pos;
30138df9675fSTheodore Ts'o if (*pos < 0 || *pos >= ext4_get_groups_count(sb))
3014c9de560dSAlex Tomas return NULL;
3015c9de560dSAlex Tomas group = *pos + 1;
3016a9df9a49STheodore Ts'o return (void *) ((unsigned long) group);
3017c9de560dSAlex Tomas }
3018c9de560dSAlex Tomas
ext4_mb_seq_groups_show(struct seq_file * seq,void * v)3019c9de560dSAlex Tomas static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
3020c9de560dSAlex Tomas {
3021359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
3022a9df9a49STheodore Ts'o ext4_group_t group = (ext4_group_t) ((unsigned long) v);
3023c9de560dSAlex Tomas int i;
30241c8457caSAditya Kali int err, buddy_loaded = 0;
3025c9de560dSAlex Tomas struct ext4_buddy e4b;
30261c8457caSAditya Kali struct ext4_group_info *grinfo;
30272df2c340SArnd Bergmann unsigned char blocksize_bits = min_t(unsigned char,
30282df2c340SArnd Bergmann sb->s_blocksize_bits,
30292df2c340SArnd Bergmann EXT4_MAX_BLOCK_LOG_SIZE);
3030c9de560dSAlex Tomas struct sg {
3031c9de560dSAlex Tomas struct ext4_group_info info;
3032b80b32b6STheodore Ts'o ext4_grpblk_t counters[EXT4_MAX_BLOCK_LOG_SIZE + 2];
3033c9de560dSAlex Tomas } sg;
3034c9de560dSAlex Tomas
3035c9de560dSAlex Tomas group--;
3036c9de560dSAlex Tomas if (group == 0)
303797b4af2fSRasmus Villemoes seq_puts(seq, "#group: free frags first ["
303897b4af2fSRasmus Villemoes " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
3039802cf1f9SHuaitong Han " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
3040c9de560dSAlex Tomas
3041b80b32b6STheodore Ts'o i = (blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
3042b80b32b6STheodore Ts'o sizeof(struct ext4_group_info);
3043b80b32b6STheodore Ts'o
30441c8457caSAditya Kali grinfo = ext4_get_group_info(sb, group);
30455354b2afSTheodore Ts'o if (!grinfo)
30465354b2afSTheodore Ts'o return 0;
30471c8457caSAditya Kali /* Load the group info in memory only if not already loaded. */
30481c8457caSAditya Kali if (unlikely(EXT4_MB_GRP_NEED_INIT(grinfo))) {
3049c9de560dSAlex Tomas err = ext4_mb_load_buddy(sb, group, &e4b);
3050c9de560dSAlex Tomas if (err) {
3051a9df9a49STheodore Ts'o seq_printf(seq, "#%-5u: I/O error\n", group);
3052c9de560dSAlex Tomas return 0;
3053c9de560dSAlex Tomas }
30541c8457caSAditya Kali buddy_loaded = 1;
30551c8457caSAditya Kali }
30561c8457caSAditya Kali
30575354b2afSTheodore Ts'o memcpy(&sg, grinfo, i);
30581c8457caSAditya Kali
30591c8457caSAditya Kali if (buddy_loaded)
3060e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
3061c9de560dSAlex Tomas
3062a9df9a49STheodore Ts'o seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
3063c9de560dSAlex Tomas sg.info.bb_fragments, sg.info.bb_first_free);
3064c9de560dSAlex Tomas for (i = 0; i <= 13; i++)
30652df2c340SArnd Bergmann seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
3066c9de560dSAlex Tomas sg.info.bb_counters[i] : 0);
306758546b2aSZhang Yi seq_puts(seq, " ]");
306858546b2aSZhang Yi if (EXT4_MB_GRP_BBITMAP_CORRUPT(&sg.info))
306958546b2aSZhang Yi seq_puts(seq, " Block bitmap corrupted!");
307058546b2aSZhang Yi seq_puts(seq, "\n");
3071c9de560dSAlex Tomas
3072c9de560dSAlex Tomas return 0;
3073c9de560dSAlex Tomas }
3074c9de560dSAlex Tomas
ext4_mb_seq_groups_stop(struct seq_file * seq,void * v)3075c9de560dSAlex Tomas static void ext4_mb_seq_groups_stop(struct seq_file *seq, void *v)
3076c9de560dSAlex Tomas {
3077c9de560dSAlex Tomas }
3078c9de560dSAlex Tomas
3079247dbed8SChristoph Hellwig const struct seq_operations ext4_mb_seq_groups_ops = {
3080c9de560dSAlex Tomas .start = ext4_mb_seq_groups_start,
3081c9de560dSAlex Tomas .next = ext4_mb_seq_groups_next,
3082c9de560dSAlex Tomas .stop = ext4_mb_seq_groups_stop,
3083c9de560dSAlex Tomas .show = ext4_mb_seq_groups_show,
3084c9de560dSAlex Tomas };
3085c9de560dSAlex Tomas
ext4_seq_mb_stats_show(struct seq_file * seq,void * offset)3086a6c75eafSHarshad Shirwadkar int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset)
3087a6c75eafSHarshad Shirwadkar {
3088c30365b9SYu Zhe struct super_block *sb = seq->private;
3089a6c75eafSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(sb);
3090a6c75eafSHarshad Shirwadkar
3091a6c75eafSHarshad Shirwadkar seq_puts(seq, "mballoc:\n");
3092a6c75eafSHarshad Shirwadkar if (!sbi->s_mb_stats) {
3093a6c75eafSHarshad Shirwadkar seq_puts(seq, "\tmb stats collection turned off.\n");
3094f52f3d2bSOjaswin Mujoo seq_puts(
3095f52f3d2bSOjaswin Mujoo seq,
3096f52f3d2bSOjaswin Mujoo "\tTo enable, please write \"1\" to sysfs file mb_stats.\n");
3097a6c75eafSHarshad Shirwadkar return 0;
3098a6c75eafSHarshad Shirwadkar }
3099a6c75eafSHarshad Shirwadkar seq_printf(seq, "\treqs: %u\n", atomic_read(&sbi->s_bal_reqs));
3100a6c75eafSHarshad Shirwadkar seq_printf(seq, "\tsuccess: %u\n", atomic_read(&sbi->s_bal_success));
3101a6c75eafSHarshad Shirwadkar
3102f52f3d2bSOjaswin Mujoo seq_printf(seq, "\tgroups_scanned: %u\n",
3103f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_groups_scanned));
3104a6c75eafSHarshad Shirwadkar
3105f52f3d2bSOjaswin Mujoo /* CR_POWER2_ALIGNED stats */
3106f52f3d2bSOjaswin Mujoo seq_puts(seq, "\tcr_p2_aligned_stats:\n");
3107f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\thits: %llu\n",
3108f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_hits[CR_POWER2_ALIGNED]));
3109f52f3d2bSOjaswin Mujoo seq_printf(
3110f52f3d2bSOjaswin Mujoo seq, "\t\tgroups_considered: %llu\n",
3111f52f3d2bSOjaswin Mujoo atomic64_read(
3112f52f3d2bSOjaswin Mujoo &sbi->s_bal_cX_groups_considered[CR_POWER2_ALIGNED]));
3113f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\textents_scanned: %u\n",
3114f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_cX_ex_scanned[CR_POWER2_ALIGNED]));
3115a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tuseless_loops: %llu\n",
3116f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_failed[CR_POWER2_ALIGNED]));
3117196e402aSHarshad Shirwadkar seq_printf(seq, "\t\tbad_suggestions: %u\n",
3118f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_p2_aligned_bad_suggestions));
3119a6c75eafSHarshad Shirwadkar
3120f52f3d2bSOjaswin Mujoo /* CR_GOAL_LEN_FAST stats */
3121f52f3d2bSOjaswin Mujoo seq_puts(seq, "\tcr_goal_fast_stats:\n");
3122f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\thits: %llu\n",
3123f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_hits[CR_GOAL_LEN_FAST]));
3124a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tgroups_considered: %llu\n",
3125f52f3d2bSOjaswin Mujoo atomic64_read(
3126f52f3d2bSOjaswin Mujoo &sbi->s_bal_cX_groups_considered[CR_GOAL_LEN_FAST]));
3127f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\textents_scanned: %u\n",
3128f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_cX_ex_scanned[CR_GOAL_LEN_FAST]));
3129a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tuseless_loops: %llu\n",
3130f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_failed[CR_GOAL_LEN_FAST]));
3131196e402aSHarshad Shirwadkar seq_printf(seq, "\t\tbad_suggestions: %u\n",
3132f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_goal_fast_bad_suggestions));
3133a6c75eafSHarshad Shirwadkar
3134f52f3d2bSOjaswin Mujoo /* CR_BEST_AVAIL_LEN stats */
3135f52f3d2bSOjaswin Mujoo seq_puts(seq, "\tcr_best_avail_stats:\n");
3136f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\thits: %llu\n",
3137f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_hits[CR_BEST_AVAIL_LEN]));
3138f52f3d2bSOjaswin Mujoo seq_printf(
3139f52f3d2bSOjaswin Mujoo seq, "\t\tgroups_considered: %llu\n",
3140f52f3d2bSOjaswin Mujoo atomic64_read(
3141f52f3d2bSOjaswin Mujoo &sbi->s_bal_cX_groups_considered[CR_BEST_AVAIL_LEN]));
3142f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\textents_scanned: %u\n",
3143f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_cX_ex_scanned[CR_BEST_AVAIL_LEN]));
31447e170922SOjaswin Mujoo seq_printf(seq, "\t\tuseless_loops: %llu\n",
3145f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_failed[CR_BEST_AVAIL_LEN]));
31467e170922SOjaswin Mujoo seq_printf(seq, "\t\tbad_suggestions: %u\n",
3147f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_best_avail_bad_suggestions));
31487e170922SOjaswin Mujoo
3149f52f3d2bSOjaswin Mujoo /* CR_GOAL_LEN_SLOW stats */
3150f52f3d2bSOjaswin Mujoo seq_puts(seq, "\tcr_goal_slow_stats:\n");
3151f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\thits: %llu\n",
3152f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_hits[CR_GOAL_LEN_SLOW]));
3153a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tgroups_considered: %llu\n",
3154f52f3d2bSOjaswin Mujoo atomic64_read(
3155f52f3d2bSOjaswin Mujoo &sbi->s_bal_cX_groups_considered[CR_GOAL_LEN_SLOW]));
3156f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\textents_scanned: %u\n",
3157f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_cX_ex_scanned[CR_GOAL_LEN_SLOW]));
3158a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tuseless_loops: %llu\n",
3159f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_failed[CR_GOAL_LEN_SLOW]));
3160a6c75eafSHarshad Shirwadkar
3161f52f3d2bSOjaswin Mujoo /* CR_ANY_FREE stats */
3162f52f3d2bSOjaswin Mujoo seq_puts(seq, "\tcr_any_free_stats:\n");
3163f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\thits: %llu\n",
3164f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_hits[CR_ANY_FREE]));
3165f52f3d2bSOjaswin Mujoo seq_printf(
3166f52f3d2bSOjaswin Mujoo seq, "\t\tgroups_considered: %llu\n",
3167f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_groups_considered[CR_ANY_FREE]));
3168f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\textents_scanned: %u\n",
3169f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_cX_ex_scanned[CR_ANY_FREE]));
3170a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tuseless_loops: %llu\n",
3171f52f3d2bSOjaswin Mujoo atomic64_read(&sbi->s_bal_cX_failed[CR_ANY_FREE]));
3172f52f3d2bSOjaswin Mujoo
3173f52f3d2bSOjaswin Mujoo /* Aggregates */
3174f52f3d2bSOjaswin Mujoo seq_printf(seq, "\textents_scanned: %u\n",
3175f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_ex_scanned));
3176a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tgoal_hits: %u\n", atomic_read(&sbi->s_bal_goals));
3177f52f3d2bSOjaswin Mujoo seq_printf(seq, "\t\tlen_goal_hits: %u\n",
3178f52f3d2bSOjaswin Mujoo atomic_read(&sbi->s_bal_len_goals));
3179a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\t2^n_hits: %u\n", atomic_read(&sbi->s_bal_2orders));
3180a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tbreaks: %u\n", atomic_read(&sbi->s_bal_breaks));
3181a6c75eafSHarshad Shirwadkar seq_printf(seq, "\t\tlost: %u\n", atomic_read(&sbi->s_mb_lost_chunks));
3182a6c75eafSHarshad Shirwadkar seq_printf(seq, "\tbuddies_generated: %u/%u\n",
3183a6c75eafSHarshad Shirwadkar atomic_read(&sbi->s_mb_buddies_generated),
3184a6c75eafSHarshad Shirwadkar ext4_get_groups_count(sb));
3185a6c75eafSHarshad Shirwadkar seq_printf(seq, "\tbuddies_time_used: %llu\n",
3186a6c75eafSHarshad Shirwadkar atomic64_read(&sbi->s_mb_generation_time));
3187a6c75eafSHarshad Shirwadkar seq_printf(seq, "\tpreallocated: %u\n",
3188a6c75eafSHarshad Shirwadkar atomic_read(&sbi->s_mb_preallocated));
3189f52f3d2bSOjaswin Mujoo seq_printf(seq, "\tdiscarded: %u\n", atomic_read(&sbi->s_mb_discarded));
3190a6c75eafSHarshad Shirwadkar return 0;
3191a6c75eafSHarshad Shirwadkar }
3192a6c75eafSHarshad Shirwadkar
ext4_mb_seq_structs_summary_start(struct seq_file * seq,loff_t * pos)3193f68f4063SHarshad Shirwadkar static void *ext4_mb_seq_structs_summary_start(struct seq_file *seq, loff_t *pos)
3194a5fda113STheodore Ts'o __acquires(&EXT4_SB(sb)->s_mb_rb_lock)
3195f68f4063SHarshad Shirwadkar {
3196359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
3197f68f4063SHarshad Shirwadkar unsigned long position;
3198f68f4063SHarshad Shirwadkar
319983e80a6eSJan Kara if (*pos < 0 || *pos >= 2*MB_NUM_ORDERS(sb))
3200f68f4063SHarshad Shirwadkar return NULL;
3201f68f4063SHarshad Shirwadkar position = *pos + 1;
3202f68f4063SHarshad Shirwadkar return (void *) ((unsigned long) position);
3203f68f4063SHarshad Shirwadkar }
3204f68f4063SHarshad Shirwadkar
ext4_mb_seq_structs_summary_next(struct seq_file * seq,void * v,loff_t * pos)3205f68f4063SHarshad Shirwadkar static void *ext4_mb_seq_structs_summary_next(struct seq_file *seq, void *v, loff_t *pos)
3206f68f4063SHarshad Shirwadkar {
3207359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
3208f68f4063SHarshad Shirwadkar unsigned long position;
3209f68f4063SHarshad Shirwadkar
3210f68f4063SHarshad Shirwadkar ++*pos;
321183e80a6eSJan Kara if (*pos < 0 || *pos >= 2*MB_NUM_ORDERS(sb))
3212f68f4063SHarshad Shirwadkar return NULL;
3213f68f4063SHarshad Shirwadkar position = *pos + 1;
3214f68f4063SHarshad Shirwadkar return (void *) ((unsigned long) position);
3215f68f4063SHarshad Shirwadkar }
3216f68f4063SHarshad Shirwadkar
ext4_mb_seq_structs_summary_show(struct seq_file * seq,void * v)3217f68f4063SHarshad Shirwadkar static int ext4_mb_seq_structs_summary_show(struct seq_file *seq, void *v)
3218f68f4063SHarshad Shirwadkar {
3219359745d7SMuchun Song struct super_block *sb = pde_data(file_inode(seq->file));
3220f68f4063SHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(sb);
3221f68f4063SHarshad Shirwadkar unsigned long position = ((unsigned long) v);
3222f68f4063SHarshad Shirwadkar struct ext4_group_info *grp;
322383e80a6eSJan Kara unsigned int count;
3224f68f4063SHarshad Shirwadkar
3225f68f4063SHarshad Shirwadkar position--;
3226f68f4063SHarshad Shirwadkar if (position >= MB_NUM_ORDERS(sb)) {
322783e80a6eSJan Kara position -= MB_NUM_ORDERS(sb);
322883e80a6eSJan Kara if (position == 0)
322983e80a6eSJan Kara seq_puts(seq, "avg_fragment_size_lists:\n");
3230f68f4063SHarshad Shirwadkar
323183e80a6eSJan Kara count = 0;
323283e80a6eSJan Kara read_lock(&sbi->s_mb_avg_fragment_size_locks[position]);
323383e80a6eSJan Kara list_for_each_entry(grp, &sbi->s_mb_avg_fragment_size[position],
323483e80a6eSJan Kara bb_avg_fragment_size_node)
323583e80a6eSJan Kara count++;
323683e80a6eSJan Kara read_unlock(&sbi->s_mb_avg_fragment_size_locks[position]);
323783e80a6eSJan Kara seq_printf(seq, "\tlist_order_%u_groups: %u\n",
323883e80a6eSJan Kara (unsigned int)position, count);
3239f68f4063SHarshad Shirwadkar return 0;
3240f68f4063SHarshad Shirwadkar }
3241f68f4063SHarshad Shirwadkar
3242f68f4063SHarshad Shirwadkar if (position == 0) {
3243f68f4063SHarshad Shirwadkar seq_printf(seq, "optimize_scan: %d\n",
3244f68f4063SHarshad Shirwadkar test_opt2(sb, MB_OPTIMIZE_SCAN) ? 1 : 0);
3245f68f4063SHarshad Shirwadkar seq_puts(seq, "max_free_order_lists:\n");
3246f68f4063SHarshad Shirwadkar }
3247f68f4063SHarshad Shirwadkar count = 0;
324883e80a6eSJan Kara read_lock(&sbi->s_mb_largest_free_orders_locks[position]);
3249f68f4063SHarshad Shirwadkar list_for_each_entry(grp, &sbi->s_mb_largest_free_orders[position],
3250f68f4063SHarshad Shirwadkar bb_largest_free_order_node)
3251f68f4063SHarshad Shirwadkar count++;
325283e80a6eSJan Kara read_unlock(&sbi->s_mb_largest_free_orders_locks[position]);
3253f68f4063SHarshad Shirwadkar seq_printf(seq, "\tlist_order_%u_groups: %u\n",
3254f68f4063SHarshad Shirwadkar (unsigned int)position, count);
3255f68f4063SHarshad Shirwadkar
3256f68f4063SHarshad Shirwadkar return 0;
3257f68f4063SHarshad Shirwadkar }
3258f68f4063SHarshad Shirwadkar
ext4_mb_seq_structs_summary_stop(struct seq_file * seq,void * v)3259f68f4063SHarshad Shirwadkar static void ext4_mb_seq_structs_summary_stop(struct seq_file *seq, void *v)
3260f68f4063SHarshad Shirwadkar {
3261f68f4063SHarshad Shirwadkar }
3262f68f4063SHarshad Shirwadkar
3263f68f4063SHarshad Shirwadkar const struct seq_operations ext4_mb_seq_structs_summary_ops = {
3264f68f4063SHarshad Shirwadkar .start = ext4_mb_seq_structs_summary_start,
3265f68f4063SHarshad Shirwadkar .next = ext4_mb_seq_structs_summary_next,
3266f68f4063SHarshad Shirwadkar .stop = ext4_mb_seq_structs_summary_stop,
3267f68f4063SHarshad Shirwadkar .show = ext4_mb_seq_structs_summary_show,
3268f68f4063SHarshad Shirwadkar };
3269f68f4063SHarshad Shirwadkar
get_groupinfo_cache(int blocksize_bits)3270fb1813f4SCurt Wohlgemuth static struct kmem_cache *get_groupinfo_cache(int blocksize_bits)
3271fb1813f4SCurt Wohlgemuth {
3272fb1813f4SCurt Wohlgemuth int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
3273fb1813f4SCurt Wohlgemuth struct kmem_cache *cachep = ext4_groupinfo_caches[cache_index];
3274fb1813f4SCurt Wohlgemuth
3275fb1813f4SCurt Wohlgemuth BUG_ON(!cachep);
3276fb1813f4SCurt Wohlgemuth return cachep;
3277fb1813f4SCurt Wohlgemuth }
32785f21b0e6SFrederic Bohe
327928623c2fSTheodore Ts'o /*
328028623c2fSTheodore Ts'o * Allocate the top-level s_group_info array for the specified number
328128623c2fSTheodore Ts'o * of groups
328228623c2fSTheodore Ts'o */
ext4_mb_alloc_groupinfo(struct super_block * sb,ext4_group_t ngroups)328328623c2fSTheodore Ts'o int ext4_mb_alloc_groupinfo(struct super_block *sb, ext4_group_t ngroups)
328428623c2fSTheodore Ts'o {
328528623c2fSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb);
328628623c2fSTheodore Ts'o unsigned size;
3287df3da4eaSSuraj Jitindar Singh struct ext4_group_info ***old_groupinfo, ***new_groupinfo;
328828623c2fSTheodore Ts'o
328928623c2fSTheodore Ts'o size = (ngroups + EXT4_DESC_PER_BLOCK(sb) - 1) >>
329028623c2fSTheodore Ts'o EXT4_DESC_PER_BLOCK_BITS(sb);
329128623c2fSTheodore Ts'o if (size <= sbi->s_group_info_size)
329228623c2fSTheodore Ts'o return 0;
329328623c2fSTheodore Ts'o
329428623c2fSTheodore Ts'o size = roundup_pow_of_two(sizeof(*sbi->s_group_info) * size);
3295a7c3e901SMichal Hocko new_groupinfo = kvzalloc(size, GFP_KERNEL);
329628623c2fSTheodore Ts'o if (!new_groupinfo) {
329728623c2fSTheodore Ts'o ext4_msg(sb, KERN_ERR, "can't allocate buddy meta group");
329828623c2fSTheodore Ts'o return -ENOMEM;
329928623c2fSTheodore Ts'o }
3300df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3301df3da4eaSSuraj Jitindar Singh old_groupinfo = rcu_dereference(sbi->s_group_info);
3302df3da4eaSSuraj Jitindar Singh if (old_groupinfo)
3303df3da4eaSSuraj Jitindar Singh memcpy(new_groupinfo, old_groupinfo,
330428623c2fSTheodore Ts'o sbi->s_group_info_size * sizeof(*sbi->s_group_info));
3305df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
3306df3da4eaSSuraj Jitindar Singh rcu_assign_pointer(sbi->s_group_info, new_groupinfo);
330728623c2fSTheodore Ts'o sbi->s_group_info_size = size / sizeof(*sbi->s_group_info);
3308df3da4eaSSuraj Jitindar Singh if (old_groupinfo)
3309df3da4eaSSuraj Jitindar Singh ext4_kvfree_array_rcu(old_groupinfo);
331028623c2fSTheodore Ts'o ext4_debug("allocated s_groupinfo array for %d meta_bg's\n",
331128623c2fSTheodore Ts'o sbi->s_group_info_size);
331228623c2fSTheodore Ts'o return 0;
331328623c2fSTheodore Ts'o }
331428623c2fSTheodore Ts'o
33155f21b0e6SFrederic Bohe /* Create and initialize ext4_group_info data for the given group. */
ext4_mb_add_groupinfo(struct super_block * sb,ext4_group_t group,struct ext4_group_desc * desc)3316920313a7SAneesh Kumar K.V int ext4_mb_add_groupinfo(struct super_block *sb, ext4_group_t group,
33175f21b0e6SFrederic Bohe struct ext4_group_desc *desc)
33185f21b0e6SFrederic Bohe {
3319fb1813f4SCurt Wohlgemuth int i;
33205f21b0e6SFrederic Bohe int metalen = 0;
3321df3da4eaSSuraj Jitindar Singh int idx = group >> EXT4_DESC_PER_BLOCK_BITS(sb);
33225f21b0e6SFrederic Bohe struct ext4_sb_info *sbi = EXT4_SB(sb);
33235f21b0e6SFrederic Bohe struct ext4_group_info **meta_group_info;
3324fb1813f4SCurt Wohlgemuth struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
33255f21b0e6SFrederic Bohe
33265f21b0e6SFrederic Bohe /*
33275f21b0e6SFrederic Bohe * First check if this group is the first of a reserved block.
33285f21b0e6SFrederic Bohe * If it's true, we have to allocate a new table of pointers
33295f21b0e6SFrederic Bohe * to ext4_group_info structures
33305f21b0e6SFrederic Bohe */
33315f21b0e6SFrederic Bohe if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
33325f21b0e6SFrederic Bohe metalen = sizeof(*meta_group_info) <<
33335f21b0e6SFrederic Bohe EXT4_DESC_PER_BLOCK_BITS(sb);
33344fdb5543SDmitry Monakhov meta_group_info = kmalloc(metalen, GFP_NOFS);
33355f21b0e6SFrederic Bohe if (meta_group_info == NULL) {
33367f6a11e7SJoe Perches ext4_msg(sb, KERN_ERR, "can't allocate mem "
33379d8b9ec4STheodore Ts'o "for a buddy group");
3338df119095SKemeng Shi return -ENOMEM;
33395f21b0e6SFrederic Bohe }
3340df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3341df3da4eaSSuraj Jitindar Singh rcu_dereference(sbi->s_group_info)[idx] = meta_group_info;
3342df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
33435f21b0e6SFrederic Bohe }
33445f21b0e6SFrederic Bohe
3345df3da4eaSSuraj Jitindar Singh meta_group_info = sbi_array_rcu_deref(sbi, s_group_info, idx);
33465f21b0e6SFrederic Bohe i = group & (EXT4_DESC_PER_BLOCK(sb) - 1);
33475f21b0e6SFrederic Bohe
33484fdb5543SDmitry Monakhov meta_group_info[i] = kmem_cache_zalloc(cachep, GFP_NOFS);
33495f21b0e6SFrederic Bohe if (meta_group_info[i] == NULL) {
33507f6a11e7SJoe Perches ext4_msg(sb, KERN_ERR, "can't allocate buddy mem");
33515f21b0e6SFrederic Bohe goto exit_group_info;
33525f21b0e6SFrederic Bohe }
33535f21b0e6SFrederic Bohe set_bit(EXT4_GROUP_INFO_NEED_INIT_BIT,
33545f21b0e6SFrederic Bohe &(meta_group_info[i]->bb_state));
33555f21b0e6SFrederic Bohe
33565f21b0e6SFrederic Bohe /*
33575f21b0e6SFrederic Bohe * initialize bb_free to be able to skip
33585f21b0e6SFrederic Bohe * empty groups without initialization
33595f21b0e6SFrederic Bohe */
33608844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) &&
33618844618dSTheodore Ts'o (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
33625f21b0e6SFrederic Bohe meta_group_info[i]->bb_free =
3363cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb, group, desc);
33645f21b0e6SFrederic Bohe } else {
33655f21b0e6SFrederic Bohe meta_group_info[i]->bb_free =
3366021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, desc);
33675f21b0e6SFrederic Bohe }
33685f21b0e6SFrederic Bohe
33695f21b0e6SFrederic Bohe INIT_LIST_HEAD(&meta_group_info[i]->bb_prealloc_list);
3370920313a7SAneesh Kumar K.V init_rwsem(&meta_group_info[i]->alloc_sem);
337164e290ecSVenkatesh Pallipadi meta_group_info[i]->bb_free_root = RB_ROOT;
3372196e402aSHarshad Shirwadkar INIT_LIST_HEAD(&meta_group_info[i]->bb_largest_free_order_node);
337383e80a6eSJan Kara INIT_LIST_HEAD(&meta_group_info[i]->bb_avg_fragment_size_node);
33748a57d9d6SCurt Wohlgemuth meta_group_info[i]->bb_largest_free_order = -1; /* uninit */
337583e80a6eSJan Kara meta_group_info[i]->bb_avg_fragment_size_order = -1; /* uninit */
3376196e402aSHarshad Shirwadkar meta_group_info[i]->bb_group = group;
33775f21b0e6SFrederic Bohe
3378a3450215SRitesh Harjani mb_group_bb_bitmap_alloc(sb, meta_group_info[i], group);
33795f21b0e6SFrederic Bohe return 0;
33805f21b0e6SFrederic Bohe
33815f21b0e6SFrederic Bohe exit_group_info:
33825f21b0e6SFrederic Bohe /* If a meta_group_info table has been allocated, release it now */
3383caaf7a29STao Ma if (group % EXT4_DESC_PER_BLOCK(sb) == 0) {
3384df3da4eaSSuraj Jitindar Singh struct ext4_group_info ***group_info;
3385df3da4eaSSuraj Jitindar Singh
3386df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3387df3da4eaSSuraj Jitindar Singh group_info = rcu_dereference(sbi->s_group_info);
3388df3da4eaSSuraj Jitindar Singh kfree(group_info[idx]);
3389df3da4eaSSuraj Jitindar Singh group_info[idx] = NULL;
3390df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
3391caaf7a29STao Ma }
33925f21b0e6SFrederic Bohe return -ENOMEM;
33935f21b0e6SFrederic Bohe } /* ext4_mb_add_groupinfo */
33945f21b0e6SFrederic Bohe
ext4_mb_init_backend(struct super_block * sb)3395c9de560dSAlex Tomas static int ext4_mb_init_backend(struct super_block *sb)
3396c9de560dSAlex Tomas {
33978df9675fSTheodore Ts'o ext4_group_t ngroups = ext4_get_groups_count(sb);
3398c9de560dSAlex Tomas ext4_group_t i;
3399c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
340028623c2fSTheodore Ts'o int err;
34015f21b0e6SFrederic Bohe struct ext4_group_desc *desc;
3402df3da4eaSSuraj Jitindar Singh struct ext4_group_info ***group_info;
3403fb1813f4SCurt Wohlgemuth struct kmem_cache *cachep;
3404c9de560dSAlex Tomas
340528623c2fSTheodore Ts'o err = ext4_mb_alloc_groupinfo(sb, ngroups);
340628623c2fSTheodore Ts'o if (err)
340728623c2fSTheodore Ts'o return err;
34085f21b0e6SFrederic Bohe
3409c9de560dSAlex Tomas sbi->s_buddy_cache = new_inode(sb);
3410c9de560dSAlex Tomas if (sbi->s_buddy_cache == NULL) {
34119d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't get new inode");
3412c9de560dSAlex Tomas goto err_freesgi;
3413c9de560dSAlex Tomas }
341448e6061bSYu Jian /* To avoid potentially colliding with an valid on-disk inode number,
341548e6061bSYu Jian * use EXT4_BAD_INO for the buddy cache inode number. This inode is
341648e6061bSYu Jian * not in the inode hash, so it should never be found by iget(), but
341748e6061bSYu Jian * this will avoid confusion if it ever shows up during debugging. */
341848e6061bSYu Jian sbi->s_buddy_cache->i_ino = EXT4_BAD_INO;
3419c9de560dSAlex Tomas EXT4_I(sbi->s_buddy_cache)->i_disksize = 0;
34208df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) {
34214b99faa2SKhazhismel Kumykov cond_resched();
3422c9de560dSAlex Tomas desc = ext4_get_group_desc(sb, i, NULL);
3423c9de560dSAlex Tomas if (desc == NULL) {
34249d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't read descriptor %u", i);
3425c9de560dSAlex Tomas goto err_freebuddy;
3426c9de560dSAlex Tomas }
34275f21b0e6SFrederic Bohe if (ext4_mb_add_groupinfo(sb, i, desc) != 0)
34285f21b0e6SFrederic Bohe goto err_freebuddy;
3429c9de560dSAlex Tomas }
3430c9de560dSAlex Tomas
3431cfd73237SAlex Zhuravlev if (ext4_has_feature_flex_bg(sb)) {
3432f91436d5SSabyrzhan Tasbolatov /* a single flex group is supposed to be read by a single IO.
3433f91436d5SSabyrzhan Tasbolatov * 2 ^ s_log_groups_per_flex != UINT_MAX as s_mb_prefetch is
3434f91436d5SSabyrzhan Tasbolatov * unsigned integer, so the maximum shift is 32.
3435f91436d5SSabyrzhan Tasbolatov */
3436f91436d5SSabyrzhan Tasbolatov if (sbi->s_es->s_log_groups_per_flex >= 32) {
3437f91436d5SSabyrzhan Tasbolatov ext4_msg(sb, KERN_ERR, "too many log groups per flexible block group");
3438a8867f4eSPhillip Potter goto err_freebuddy;
3439f91436d5SSabyrzhan Tasbolatov }
3440f91436d5SSabyrzhan Tasbolatov sbi->s_mb_prefetch = min_t(uint, 1 << sbi->s_es->s_log_groups_per_flex,
344182ef1370SChunguang Xu BLK_MAX_SEGMENT_SIZE >> (sb->s_blocksize_bits - 9));
3442cfd73237SAlex Zhuravlev sbi->s_mb_prefetch *= 8; /* 8 prefetch IOs in flight at most */
3443cfd73237SAlex Zhuravlev } else {
3444cfd73237SAlex Zhuravlev sbi->s_mb_prefetch = 32;
3445cfd73237SAlex Zhuravlev }
3446cfd73237SAlex Zhuravlev if (sbi->s_mb_prefetch > ext4_get_groups_count(sb))
3447cfd73237SAlex Zhuravlev sbi->s_mb_prefetch = ext4_get_groups_count(sb);
3448cfd73237SAlex Zhuravlev /* now many real IOs to prefetch within a single allocation at cr=0
3449cfd73237SAlex Zhuravlev * given cr=0 is an CPU-related optimization we shouldn't try to
3450cfd73237SAlex Zhuravlev * load too many groups, at some point we should start to use what
3451cfd73237SAlex Zhuravlev * we've got in memory.
3452cfd73237SAlex Zhuravlev * with an average random access time 5ms, it'd take a second to get
3453cfd73237SAlex Zhuravlev * 200 groups (* N with flex_bg), so let's make this limit 4
3454cfd73237SAlex Zhuravlev */
3455cfd73237SAlex Zhuravlev sbi->s_mb_prefetch_limit = sbi->s_mb_prefetch * 4;
3456cfd73237SAlex Zhuravlev if (sbi->s_mb_prefetch_limit > ext4_get_groups_count(sb))
3457cfd73237SAlex Zhuravlev sbi->s_mb_prefetch_limit = ext4_get_groups_count(sb);
3458cfd73237SAlex Zhuravlev
3459c9de560dSAlex Tomas return 0;
3460c9de560dSAlex Tomas
3461c9de560dSAlex Tomas err_freebuddy:
3462fb1813f4SCurt Wohlgemuth cachep = get_groupinfo_cache(sb->s_blocksize_bits);
34635354b2afSTheodore Ts'o while (i-- > 0) {
34645354b2afSTheodore Ts'o struct ext4_group_info *grp = ext4_get_group_info(sb, i);
34655354b2afSTheodore Ts'o
34665354b2afSTheodore Ts'o if (grp)
34675354b2afSTheodore Ts'o kmem_cache_free(cachep, grp);
34685354b2afSTheodore Ts'o }
346928623c2fSTheodore Ts'o i = sbi->s_group_info_size;
3470df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3471df3da4eaSSuraj Jitindar Singh group_info = rcu_dereference(sbi->s_group_info);
3472f1fa3342SRoel Kluin while (i-- > 0)
3473df3da4eaSSuraj Jitindar Singh kfree(group_info[i]);
3474df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
3475c9de560dSAlex Tomas iput(sbi->s_buddy_cache);
3476c9de560dSAlex Tomas err_freesgi:
3477df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3478df3da4eaSSuraj Jitindar Singh kvfree(rcu_dereference(sbi->s_group_info));
3479df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
3480c9de560dSAlex Tomas return -ENOMEM;
3481c9de560dSAlex Tomas }
3482c9de560dSAlex Tomas
ext4_groupinfo_destroy_slabs(void)34832892c15dSEric Sandeen static void ext4_groupinfo_destroy_slabs(void)
34842892c15dSEric Sandeen {
34852892c15dSEric Sandeen int i;
34862892c15dSEric Sandeen
34872892c15dSEric Sandeen for (i = 0; i < NR_GRPINFO_CACHES; i++) {
34882892c15dSEric Sandeen kmem_cache_destroy(ext4_groupinfo_caches[i]);
34892892c15dSEric Sandeen ext4_groupinfo_caches[i] = NULL;
34902892c15dSEric Sandeen }
34912892c15dSEric Sandeen }
34922892c15dSEric Sandeen
ext4_groupinfo_create_slab(size_t size)34932892c15dSEric Sandeen static int ext4_groupinfo_create_slab(size_t size)
34942892c15dSEric Sandeen {
34952892c15dSEric Sandeen static DEFINE_MUTEX(ext4_grpinfo_slab_create_mutex);
34962892c15dSEric Sandeen int slab_size;
34972892c15dSEric Sandeen int blocksize_bits = order_base_2(size);
34982892c15dSEric Sandeen int cache_index = blocksize_bits - EXT4_MIN_BLOCK_LOG_SIZE;
34992892c15dSEric Sandeen struct kmem_cache *cachep;
35002892c15dSEric Sandeen
35012892c15dSEric Sandeen if (cache_index >= NR_GRPINFO_CACHES)
35022892c15dSEric Sandeen return -EINVAL;
35032892c15dSEric Sandeen
35042892c15dSEric Sandeen if (unlikely(cache_index < 0))
35052892c15dSEric Sandeen cache_index = 0;
35062892c15dSEric Sandeen
35072892c15dSEric Sandeen mutex_lock(&ext4_grpinfo_slab_create_mutex);
35082892c15dSEric Sandeen if (ext4_groupinfo_caches[cache_index]) {
35092892c15dSEric Sandeen mutex_unlock(&ext4_grpinfo_slab_create_mutex);
35102892c15dSEric Sandeen return 0; /* Already created */
35112892c15dSEric Sandeen }
35122892c15dSEric Sandeen
35132892c15dSEric Sandeen slab_size = offsetof(struct ext4_group_info,
35142892c15dSEric Sandeen bb_counters[blocksize_bits + 2]);
35152892c15dSEric Sandeen
35162892c15dSEric Sandeen cachep = kmem_cache_create(ext4_groupinfo_slab_names[cache_index],
35172892c15dSEric Sandeen slab_size, 0, SLAB_RECLAIM_ACCOUNT,
35182892c15dSEric Sandeen NULL);
35192892c15dSEric Sandeen
3520823ba01fSTao Ma ext4_groupinfo_caches[cache_index] = cachep;
3521823ba01fSTao Ma
35222892c15dSEric Sandeen mutex_unlock(&ext4_grpinfo_slab_create_mutex);
35232892c15dSEric Sandeen if (!cachep) {
35249d8b9ec4STheodore Ts'o printk(KERN_EMERG
35259d8b9ec4STheodore Ts'o "EXT4-fs: no memory for groupinfo slab cache\n");
35262892c15dSEric Sandeen return -ENOMEM;
35272892c15dSEric Sandeen }
35282892c15dSEric Sandeen
35292892c15dSEric Sandeen return 0;
35302892c15dSEric Sandeen }
35312892c15dSEric Sandeen
ext4_discard_work(struct work_struct * work)353255cdd0afSWang Jianchao static void ext4_discard_work(struct work_struct *work)
353355cdd0afSWang Jianchao {
353455cdd0afSWang Jianchao struct ext4_sb_info *sbi = container_of(work,
353555cdd0afSWang Jianchao struct ext4_sb_info, s_discard_work);
353655cdd0afSWang Jianchao struct super_block *sb = sbi->s_sb;
353755cdd0afSWang Jianchao struct ext4_free_data *fd, *nfd;
353855cdd0afSWang Jianchao struct ext4_buddy e4b;
35390f6bc579SRuan Jinjie LIST_HEAD(discard_list);
354055cdd0afSWang Jianchao ext4_group_t grp, load_grp;
354155cdd0afSWang Jianchao int err = 0;
354255cdd0afSWang Jianchao
354355cdd0afSWang Jianchao spin_lock(&sbi->s_md_lock);
354455cdd0afSWang Jianchao list_splice_init(&sbi->s_discard_list, &discard_list);
354555cdd0afSWang Jianchao spin_unlock(&sbi->s_md_lock);
354655cdd0afSWang Jianchao
354755cdd0afSWang Jianchao load_grp = UINT_MAX;
354855cdd0afSWang Jianchao list_for_each_entry_safe(fd, nfd, &discard_list, efd_list) {
354955cdd0afSWang Jianchao /*
35505036ab8dSWang Jianchao * If filesystem is umounting or no memory or suffering
35515036ab8dSWang Jianchao * from no space, give up the discard
355255cdd0afSWang Jianchao */
35535036ab8dSWang Jianchao if ((sb->s_flags & SB_ACTIVE) && !err &&
35545036ab8dSWang Jianchao !atomic_read(&sbi->s_retry_alloc_pending)) {
355555cdd0afSWang Jianchao grp = fd->efd_group;
355655cdd0afSWang Jianchao if (grp != load_grp) {
355755cdd0afSWang Jianchao if (load_grp != UINT_MAX)
355855cdd0afSWang Jianchao ext4_mb_unload_buddy(&e4b);
355955cdd0afSWang Jianchao
356055cdd0afSWang Jianchao err = ext4_mb_load_buddy(sb, grp, &e4b);
356155cdd0afSWang Jianchao if (err) {
356255cdd0afSWang Jianchao kmem_cache_free(ext4_free_data_cachep, fd);
356355cdd0afSWang Jianchao load_grp = UINT_MAX;
356455cdd0afSWang Jianchao continue;
356555cdd0afSWang Jianchao } else {
356655cdd0afSWang Jianchao load_grp = grp;
356755cdd0afSWang Jianchao }
356855cdd0afSWang Jianchao }
356955cdd0afSWang Jianchao
357055cdd0afSWang Jianchao ext4_lock_group(sb, grp);
357155cdd0afSWang Jianchao ext4_try_to_trim_range(sb, &e4b, fd->efd_start_cluster,
357255cdd0afSWang Jianchao fd->efd_start_cluster + fd->efd_count - 1, 1);
357355cdd0afSWang Jianchao ext4_unlock_group(sb, grp);
357455cdd0afSWang Jianchao }
357555cdd0afSWang Jianchao kmem_cache_free(ext4_free_data_cachep, fd);
357655cdd0afSWang Jianchao }
357755cdd0afSWang Jianchao
357855cdd0afSWang Jianchao if (load_grp != UINT_MAX)
357955cdd0afSWang Jianchao ext4_mb_unload_buddy(&e4b);
358055cdd0afSWang Jianchao }
358155cdd0afSWang Jianchao
ext4_mb_init(struct super_block * sb)35829d99012fSAkira Fujita int ext4_mb_init(struct super_block *sb)
3583c9de560dSAlex Tomas {
3584c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
35856be2ded1SAneesh Kumar K.V unsigned i, j;
3586935244cdSNicolai Stange unsigned offset, offset_incr;
3587c9de560dSAlex Tomas unsigned max;
358874767c5aSShen Feng int ret;
3589c9de560dSAlex Tomas
35904b68f6dfSHarshad Shirwadkar i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_offsets);
3591c9de560dSAlex Tomas
3592c9de560dSAlex Tomas sbi->s_mb_offsets = kmalloc(i, GFP_KERNEL);
3593c9de560dSAlex Tomas if (sbi->s_mb_offsets == NULL) {
3594fb1813f4SCurt Wohlgemuth ret = -ENOMEM;
3595fb1813f4SCurt Wohlgemuth goto out;
3596c9de560dSAlex Tomas }
3597ff7ef329SYasunori Goto
35984b68f6dfSHarshad Shirwadkar i = MB_NUM_ORDERS(sb) * sizeof(*sbi->s_mb_maxs);
3599c9de560dSAlex Tomas sbi->s_mb_maxs = kmalloc(i, GFP_KERNEL);
3600c9de560dSAlex Tomas if (sbi->s_mb_maxs == NULL) {
3601fb1813f4SCurt Wohlgemuth ret = -ENOMEM;
3602fb1813f4SCurt Wohlgemuth goto out;
3603fb1813f4SCurt Wohlgemuth }
3604fb1813f4SCurt Wohlgemuth
36052892c15dSEric Sandeen ret = ext4_groupinfo_create_slab(sb->s_blocksize);
36062892c15dSEric Sandeen if (ret < 0)
3607fb1813f4SCurt Wohlgemuth goto out;
3608c9de560dSAlex Tomas
3609c9de560dSAlex Tomas /* order 0 is regular bitmap */
3610c9de560dSAlex Tomas sbi->s_mb_maxs[0] = sb->s_blocksize << 3;
3611c9de560dSAlex Tomas sbi->s_mb_offsets[0] = 0;
3612c9de560dSAlex Tomas
3613c9de560dSAlex Tomas i = 1;
3614c9de560dSAlex Tomas offset = 0;
3615935244cdSNicolai Stange offset_incr = 1 << (sb->s_blocksize_bits - 1);
3616c9de560dSAlex Tomas max = sb->s_blocksize << 2;
3617c9de560dSAlex Tomas do {
3618c9de560dSAlex Tomas sbi->s_mb_offsets[i] = offset;
3619c9de560dSAlex Tomas sbi->s_mb_maxs[i] = max;
3620935244cdSNicolai Stange offset += offset_incr;
3621935244cdSNicolai Stange offset_incr = offset_incr >> 1;
3622c9de560dSAlex Tomas max = max >> 1;
3623c9de560dSAlex Tomas i++;
36244b68f6dfSHarshad Shirwadkar } while (i < MB_NUM_ORDERS(sb));
36254b68f6dfSHarshad Shirwadkar
362683e80a6eSJan Kara sbi->s_mb_avg_fragment_size =
362783e80a6eSJan Kara kmalloc_array(MB_NUM_ORDERS(sb), sizeof(struct list_head),
362883e80a6eSJan Kara GFP_KERNEL);
362983e80a6eSJan Kara if (!sbi->s_mb_avg_fragment_size) {
363083e80a6eSJan Kara ret = -ENOMEM;
363183e80a6eSJan Kara goto out;
363283e80a6eSJan Kara }
363383e80a6eSJan Kara sbi->s_mb_avg_fragment_size_locks =
363483e80a6eSJan Kara kmalloc_array(MB_NUM_ORDERS(sb), sizeof(rwlock_t),
363583e80a6eSJan Kara GFP_KERNEL);
363683e80a6eSJan Kara if (!sbi->s_mb_avg_fragment_size_locks) {
363783e80a6eSJan Kara ret = -ENOMEM;
363883e80a6eSJan Kara goto out;
363983e80a6eSJan Kara }
364083e80a6eSJan Kara for (i = 0; i < MB_NUM_ORDERS(sb); i++) {
364183e80a6eSJan Kara INIT_LIST_HEAD(&sbi->s_mb_avg_fragment_size[i]);
364283e80a6eSJan Kara rwlock_init(&sbi->s_mb_avg_fragment_size_locks[i]);
364383e80a6eSJan Kara }
3644196e402aSHarshad Shirwadkar sbi->s_mb_largest_free_orders =
3645196e402aSHarshad Shirwadkar kmalloc_array(MB_NUM_ORDERS(sb), sizeof(struct list_head),
3646196e402aSHarshad Shirwadkar GFP_KERNEL);
3647196e402aSHarshad Shirwadkar if (!sbi->s_mb_largest_free_orders) {
3648196e402aSHarshad Shirwadkar ret = -ENOMEM;
3649196e402aSHarshad Shirwadkar goto out;
3650196e402aSHarshad Shirwadkar }
3651196e402aSHarshad Shirwadkar sbi->s_mb_largest_free_orders_locks =
3652196e402aSHarshad Shirwadkar kmalloc_array(MB_NUM_ORDERS(sb), sizeof(rwlock_t),
3653196e402aSHarshad Shirwadkar GFP_KERNEL);
3654196e402aSHarshad Shirwadkar if (!sbi->s_mb_largest_free_orders_locks) {
3655196e402aSHarshad Shirwadkar ret = -ENOMEM;
3656196e402aSHarshad Shirwadkar goto out;
3657196e402aSHarshad Shirwadkar }
3658196e402aSHarshad Shirwadkar for (i = 0; i < MB_NUM_ORDERS(sb); i++) {
3659196e402aSHarshad Shirwadkar INIT_LIST_HEAD(&sbi->s_mb_largest_free_orders[i]);
3660196e402aSHarshad Shirwadkar rwlock_init(&sbi->s_mb_largest_free_orders_locks[i]);
3661196e402aSHarshad Shirwadkar }
3662c9de560dSAlex Tomas
3663c9de560dSAlex Tomas spin_lock_init(&sbi->s_md_lock);
3664d08854f5STheodore Ts'o sbi->s_mb_free_pending = 0;
3665a0154344SDaeho Jeong INIT_LIST_HEAD(&sbi->s_freed_data_list);
366655cdd0afSWang Jianchao INIT_LIST_HEAD(&sbi->s_discard_list);
366755cdd0afSWang Jianchao INIT_WORK(&sbi->s_discard_work, ext4_discard_work);
36685036ab8dSWang Jianchao atomic_set(&sbi->s_retry_alloc_pending, 0);
3669c9de560dSAlex Tomas
3670c9de560dSAlex Tomas sbi->s_mb_max_to_scan = MB_DEFAULT_MAX_TO_SCAN;
3671c9de560dSAlex Tomas sbi->s_mb_min_to_scan = MB_DEFAULT_MIN_TO_SCAN;
3672c9de560dSAlex Tomas sbi->s_mb_stats = MB_DEFAULT_STATS;
3673c9de560dSAlex Tomas sbi->s_mb_stream_request = MB_DEFAULT_STREAM_THRESHOLD;
3674c9de560dSAlex Tomas sbi->s_mb_order2_reqs = MB_DEFAULT_ORDER2_REQS;
3675f52f3d2bSOjaswin Mujoo sbi->s_mb_best_avail_max_trim_order = MB_DEFAULT_BEST_AVAIL_TRIM_ORDER;
36767e170922SOjaswin Mujoo
367727baebb8STheodore Ts'o /*
367827baebb8STheodore Ts'o * The default group preallocation is 512, which for 4k block
367927baebb8STheodore Ts'o * sizes translates to 2 megabytes. However for bigalloc file
368027baebb8STheodore Ts'o * systems, this is probably too big (i.e, if the cluster size
368127baebb8STheodore Ts'o * is 1 megabyte, then group preallocation size becomes half a
368227baebb8STheodore Ts'o * gigabyte!). As a default, we will keep a two megabyte
368327baebb8STheodore Ts'o * group pralloc size for cluster sizes up to 64k, and after
368427baebb8STheodore Ts'o * that, we will force a minimum group preallocation size of
368527baebb8STheodore Ts'o * 32 clusters. This translates to 8 megs when the cluster
368627baebb8STheodore Ts'o * size is 256k, and 32 megs when the cluster size is 1 meg,
368727baebb8STheodore Ts'o * which seems reasonable as a default.
368827baebb8STheodore Ts'o */
368927baebb8STheodore Ts'o sbi->s_mb_group_prealloc = max(MB_DEFAULT_GROUP_PREALLOC >>
369027baebb8STheodore Ts'o sbi->s_cluster_bits, 32);
3691d7a1fee1SDan Ehrenberg /*
3692d7a1fee1SDan Ehrenberg * If there is a s_stripe > 1, then we set the s_mb_group_prealloc
3693d7a1fee1SDan Ehrenberg * to the lowest multiple of s_stripe which is bigger than
3694d7a1fee1SDan Ehrenberg * the s_mb_group_prealloc as determined above. We want
3695d7a1fee1SDan Ehrenberg * the preallocation size to be an exact multiple of the
3696d7a1fee1SDan Ehrenberg * RAID stripe size so that preallocations don't fragment
3697d7a1fee1SDan Ehrenberg * the stripes.
3698d7a1fee1SDan Ehrenberg */
3699d7a1fee1SDan Ehrenberg if (sbi->s_stripe > 1) {
3700d7a1fee1SDan Ehrenberg sbi->s_mb_group_prealloc = roundup(
3701c3defd99SKemeng Shi sbi->s_mb_group_prealloc, EXT4_B2C(sbi, sbi->s_stripe));
3702d7a1fee1SDan Ehrenberg }
3703c9de560dSAlex Tomas
3704730c213cSEric Sandeen sbi->s_locality_groups = alloc_percpu(struct ext4_locality_group);
3705c9de560dSAlex Tomas if (sbi->s_locality_groups == NULL) {
3706fb1813f4SCurt Wohlgemuth ret = -ENOMEM;
3707029b10c5SAndrey Tsyvarev goto out;
3708c9de560dSAlex Tomas }
3709730c213cSEric Sandeen for_each_possible_cpu(i) {
3710c9de560dSAlex Tomas struct ext4_locality_group *lg;
3711730c213cSEric Sandeen lg = per_cpu_ptr(sbi->s_locality_groups, i);
3712c9de560dSAlex Tomas mutex_init(&lg->lg_mutex);
37136be2ded1SAneesh Kumar K.V for (j = 0; j < PREALLOC_TB_SIZE; j++)
37146be2ded1SAneesh Kumar K.V INIT_LIST_HEAD(&lg->lg_prealloc_list[j]);
3715c9de560dSAlex Tomas spin_lock_init(&lg->lg_prealloc_lock);
3716c9de560dSAlex Tomas }
3717c9de560dSAlex Tomas
371810f0d2a5SChristoph Hellwig if (bdev_nonrot(sb->s_bdev))
3719196e402aSHarshad Shirwadkar sbi->s_mb_max_linear_groups = 0;
3720196e402aSHarshad Shirwadkar else
3721196e402aSHarshad Shirwadkar sbi->s_mb_max_linear_groups = MB_DEFAULT_LINEAR_LIMIT;
372279a77c5aSYu Jian /* init file for buddy data */
372379a77c5aSYu Jian ret = ext4_mb_init_backend(sb);
37247aa0baeaSTao Ma if (ret != 0)
37257aa0baeaSTao Ma goto out_free_locality_groups;
372679a77c5aSYu Jian
37277aa0baeaSTao Ma return 0;
37287aa0baeaSTao Ma
37297aa0baeaSTao Ma out_free_locality_groups:
37307aa0baeaSTao Ma free_percpu(sbi->s_locality_groups);
37317aa0baeaSTao Ma sbi->s_locality_groups = NULL;
3732fb1813f4SCurt Wohlgemuth out:
373383e80a6eSJan Kara kfree(sbi->s_mb_avg_fragment_size);
373483e80a6eSJan Kara kfree(sbi->s_mb_avg_fragment_size_locks);
3735196e402aSHarshad Shirwadkar kfree(sbi->s_mb_largest_free_orders);
3736196e402aSHarshad Shirwadkar kfree(sbi->s_mb_largest_free_orders_locks);
3737fb1813f4SCurt Wohlgemuth kfree(sbi->s_mb_offsets);
37387aa0baeaSTao Ma sbi->s_mb_offsets = NULL;
3739fb1813f4SCurt Wohlgemuth kfree(sbi->s_mb_maxs);
37407aa0baeaSTao Ma sbi->s_mb_maxs = NULL;
3741fb1813f4SCurt Wohlgemuth return ret;
3742c9de560dSAlex Tomas }
3743c9de560dSAlex Tomas
3744955ce5f5SAneesh Kumar K.V /* need to called with the ext4 group lock held */
ext4_mb_cleanup_pa(struct ext4_group_info * grp)3745d3df1453SRitesh Harjani static int ext4_mb_cleanup_pa(struct ext4_group_info *grp)
3746c9de560dSAlex Tomas {
3747c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
3748c9de560dSAlex Tomas struct list_head *cur, *tmp;
3749c9de560dSAlex Tomas int count = 0;
3750c9de560dSAlex Tomas
3751c9de560dSAlex Tomas list_for_each_safe(cur, tmp, &grp->bb_prealloc_list) {
3752c9de560dSAlex Tomas pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
3753c9de560dSAlex Tomas list_del(&pa->pa_group_list);
3754c9de560dSAlex Tomas count++;
3755688f05a0SAneesh Kumar K.V kmem_cache_free(ext4_pspace_cachep, pa);
3756c9de560dSAlex Tomas }
3757d3df1453SRitesh Harjani return count;
3758c9de560dSAlex Tomas }
3759c9de560dSAlex Tomas
ext4_mb_release(struct super_block * sb)3760c9de560dSAlex Tomas int ext4_mb_release(struct super_block *sb)
3761c9de560dSAlex Tomas {
37628df9675fSTheodore Ts'o ext4_group_t ngroups = ext4_get_groups_count(sb);
3763c9de560dSAlex Tomas ext4_group_t i;
3764c9de560dSAlex Tomas int num_meta_group_infos;
3765df3da4eaSSuraj Jitindar Singh struct ext4_group_info *grinfo, ***group_info;
3766c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
3767fb1813f4SCurt Wohlgemuth struct kmem_cache *cachep = get_groupinfo_cache(sb->s_blocksize_bits);
3768d3df1453SRitesh Harjani int count;
3769c9de560dSAlex Tomas
377055cdd0afSWang Jianchao if (test_opt(sb, DISCARD)) {
377155cdd0afSWang Jianchao /*
377255cdd0afSWang Jianchao * wait the discard work to drain all of ext4_free_data
377355cdd0afSWang Jianchao */
377455cdd0afSWang Jianchao flush_work(&sbi->s_discard_work);
377555cdd0afSWang Jianchao WARN_ON_ONCE(!list_empty(&sbi->s_discard_list));
377655cdd0afSWang Jianchao }
377755cdd0afSWang Jianchao
3778c9de560dSAlex Tomas if (sbi->s_group_info) {
37798df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) {
37804b99faa2SKhazhismel Kumykov cond_resched();
3781c9de560dSAlex Tomas grinfo = ext4_get_group_info(sb, i);
37825354b2afSTheodore Ts'o if (!grinfo)
37835354b2afSTheodore Ts'o continue;
3784a3450215SRitesh Harjani mb_group_bb_bitmap_free(grinfo);
3785c9de560dSAlex Tomas ext4_lock_group(sb, i);
3786d3df1453SRitesh Harjani count = ext4_mb_cleanup_pa(grinfo);
3787d3df1453SRitesh Harjani if (count)
3788d3df1453SRitesh Harjani mb_debug(sb, "mballoc: %d PAs left\n",
3789d3df1453SRitesh Harjani count);
3790c9de560dSAlex Tomas ext4_unlock_group(sb, i);
3791fb1813f4SCurt Wohlgemuth kmem_cache_free(cachep, grinfo);
3792c9de560dSAlex Tomas }
37938df9675fSTheodore Ts'o num_meta_group_infos = (ngroups +
3794c9de560dSAlex Tomas EXT4_DESC_PER_BLOCK(sb) - 1) >>
3795c9de560dSAlex Tomas EXT4_DESC_PER_BLOCK_BITS(sb);
3796df3da4eaSSuraj Jitindar Singh rcu_read_lock();
3797df3da4eaSSuraj Jitindar Singh group_info = rcu_dereference(sbi->s_group_info);
3798c9de560dSAlex Tomas for (i = 0; i < num_meta_group_infos; i++)
3799df3da4eaSSuraj Jitindar Singh kfree(group_info[i]);
3800df3da4eaSSuraj Jitindar Singh kvfree(group_info);
3801df3da4eaSSuraj Jitindar Singh rcu_read_unlock();
3802c9de560dSAlex Tomas }
380383e80a6eSJan Kara kfree(sbi->s_mb_avg_fragment_size);
380483e80a6eSJan Kara kfree(sbi->s_mb_avg_fragment_size_locks);
3805196e402aSHarshad Shirwadkar kfree(sbi->s_mb_largest_free_orders);
3806196e402aSHarshad Shirwadkar kfree(sbi->s_mb_largest_free_orders_locks);
3807c9de560dSAlex Tomas kfree(sbi->s_mb_offsets);
3808c9de560dSAlex Tomas kfree(sbi->s_mb_maxs);
3809c9de560dSAlex Tomas iput(sbi->s_buddy_cache);
3810c9de560dSAlex Tomas if (sbi->s_mb_stats) {
38119d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_INFO,
38129d8b9ec4STheodore Ts'o "mballoc: %u blocks %u reqs (%u success)",
3813c9de560dSAlex Tomas atomic_read(&sbi->s_bal_allocated),
3814c9de560dSAlex Tomas atomic_read(&sbi->s_bal_reqs),
3815c9de560dSAlex Tomas atomic_read(&sbi->s_bal_success));
38169d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_INFO,
3817a6c75eafSHarshad Shirwadkar "mballoc: %u extents scanned, %u groups scanned, %u goal hits, "
38189d8b9ec4STheodore Ts'o "%u 2^N hits, %u breaks, %u lost",
3819c9de560dSAlex Tomas atomic_read(&sbi->s_bal_ex_scanned),
3820a6c75eafSHarshad Shirwadkar atomic_read(&sbi->s_bal_groups_scanned),
3821c9de560dSAlex Tomas atomic_read(&sbi->s_bal_goals),
3822c9de560dSAlex Tomas atomic_read(&sbi->s_bal_2orders),
3823c9de560dSAlex Tomas atomic_read(&sbi->s_bal_breaks),
3824c9de560dSAlex Tomas atomic_read(&sbi->s_mb_lost_chunks));
38259d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_INFO,
382667d25186SHarshad Shirwadkar "mballoc: %u generated and it took %llu",
382767d25186SHarshad Shirwadkar atomic_read(&sbi->s_mb_buddies_generated),
382867d25186SHarshad Shirwadkar atomic64_read(&sbi->s_mb_generation_time));
38299d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_INFO,
38309d8b9ec4STheodore Ts'o "mballoc: %u preallocated, %u discarded",
3831c9de560dSAlex Tomas atomic_read(&sbi->s_mb_preallocated),
3832c9de560dSAlex Tomas atomic_read(&sbi->s_mb_discarded));
3833c9de560dSAlex Tomas }
3834c9de560dSAlex Tomas
3835730c213cSEric Sandeen free_percpu(sbi->s_locality_groups);
3836c9de560dSAlex Tomas
3837c9de560dSAlex Tomas return 0;
3838c9de560dSAlex Tomas }
3839c9de560dSAlex Tomas
ext4_issue_discard(struct super_block * sb,ext4_group_t block_group,ext4_grpblk_t cluster,int count,struct bio ** biop)384077ca6cdfSLukas Czerner static inline int ext4_issue_discard(struct super_block *sb,
3841a0154344SDaeho Jeong ext4_group_t block_group, ext4_grpblk_t cluster, int count,
3842a0154344SDaeho Jeong struct bio **biop)
38435c521830SJiaying Zhang {
38445c521830SJiaying Zhang ext4_fsblk_t discard_block;
38455c521830SJiaying Zhang
384684130193STheodore Ts'o discard_block = (EXT4_C2B(EXT4_SB(sb), cluster) +
384784130193STheodore Ts'o ext4_group_first_block_no(sb, block_group));
384884130193STheodore Ts'o count = EXT4_C2B(EXT4_SB(sb), count);
38495c521830SJiaying Zhang trace_ext4_discard_blocks(sb,
38505c521830SJiaying Zhang (unsigned long long) discard_block, count);
3851a0154344SDaeho Jeong if (biop) {
3852a0154344SDaeho Jeong return __blkdev_issue_discard(sb->s_bdev,
3853a0154344SDaeho Jeong (sector_t)discard_block << (sb->s_blocksize_bits - 9),
3854a0154344SDaeho Jeong (sector_t)count << (sb->s_blocksize_bits - 9),
385544abff2cSChristoph Hellwig GFP_NOFS, biop);
3856a0154344SDaeho Jeong } else
385793259636SLukas Czerner return sb_issue_discard(sb, discard_block, count, GFP_NOFS, 0);
38585c521830SJiaying Zhang }
38595c521830SJiaying Zhang
ext4_free_data_in_buddy(struct super_block * sb,struct ext4_free_data * entry)3860a0154344SDaeho Jeong static void ext4_free_data_in_buddy(struct super_block *sb,
3861a0154344SDaeho Jeong struct ext4_free_data *entry)
3862c9de560dSAlex Tomas {
3863c9de560dSAlex Tomas struct ext4_buddy e4b;
3864c894058dSAneesh Kumar K.V struct ext4_group_info *db;
3865c7f2bafaSKemeng Shi int err, count = 0;
3866c9de560dSAlex Tomas
3867d3df1453SRitesh Harjani mb_debug(sb, "gonna free %u blocks in group %u (0x%p):",
386818aadd47SBobi Jam entry->efd_count, entry->efd_group, entry);
3869c9de560dSAlex Tomas
387018aadd47SBobi Jam err = ext4_mb_load_buddy(sb, entry->efd_group, &e4b);
3871c9de560dSAlex Tomas /* we expect to find existing buddy because it's pinned */
3872c9de560dSAlex Tomas BUG_ON(err != 0);
3873c9de560dSAlex Tomas
3874d08854f5STheodore Ts'o spin_lock(&EXT4_SB(sb)->s_md_lock);
3875d08854f5STheodore Ts'o EXT4_SB(sb)->s_mb_free_pending -= entry->efd_count;
3876d08854f5STheodore Ts'o spin_unlock(&EXT4_SB(sb)->s_md_lock);
387718aadd47SBobi Jam
3878c894058dSAneesh Kumar K.V db = e4b.bd_info;
3879c9de560dSAlex Tomas /* there are blocks to put in buddy to make them really free */
388018aadd47SBobi Jam count += entry->efd_count;
388118aadd47SBobi Jam ext4_lock_group(sb, entry->efd_group);
3882c894058dSAneesh Kumar K.V /* Take it out of per group rb tree */
388318aadd47SBobi Jam rb_erase(&entry->efd_node, &(db->bb_free_root));
388418aadd47SBobi Jam mb_free_blocks(NULL, &e4b, entry->efd_start_cluster, entry->efd_count);
3885c9de560dSAlex Tomas
38863d56b8d2STao Ma /*
38873d56b8d2STao Ma * Clear the trimmed flag for the group so that the next
38883d56b8d2STao Ma * ext4_trim_fs can trim it.
38893d56b8d2STao Ma */
38903d56b8d2STao Ma EXT4_MB_GRP_CLEAR_TRIMMED(db);
38913d56b8d2STao Ma
3892c894058dSAneesh Kumar K.V if (!db->bb_free_root.rb_node) {
3893c894058dSAneesh Kumar K.V /* No more items in the per group rb tree
3894c894058dSAneesh Kumar K.V * balance refcounts from ext4_mb_free_metadata()
3895c894058dSAneesh Kumar K.V */
389609cbfeafSKirill A. Shutemov put_page(e4b.bd_buddy_page);
389709cbfeafSKirill A. Shutemov put_page(e4b.bd_bitmap_page);
3898c894058dSAneesh Kumar K.V }
389918aadd47SBobi Jam ext4_unlock_group(sb, entry->efd_group);
3900e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
3901c9de560dSAlex Tomas
3902c7f2bafaSKemeng Shi mb_debug(sb, "freed %d blocks in 1 structures\n", count);
3903c9de560dSAlex Tomas }
3904c9de560dSAlex Tomas
3905a0154344SDaeho Jeong /*
3906a0154344SDaeho Jeong * This function is called by the jbd2 layer once the commit has finished,
3907a0154344SDaeho Jeong * so we know we can free the blocks that were released with that commit.
3908a0154344SDaeho Jeong */
ext4_process_freed_data(struct super_block * sb,tid_t commit_tid)3909a0154344SDaeho Jeong void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid)
3910a0154344SDaeho Jeong {
3911a0154344SDaeho Jeong struct ext4_sb_info *sbi = EXT4_SB(sb);
3912a0154344SDaeho Jeong struct ext4_free_data *entry, *tmp;
39130f6bc579SRuan Jinjie LIST_HEAD(freed_data_list);
3914a0154344SDaeho Jeong struct list_head *cut_pos = NULL;
391555cdd0afSWang Jianchao bool wake;
3916a0154344SDaeho Jeong
3917a0154344SDaeho Jeong spin_lock(&sbi->s_md_lock);
3918a0154344SDaeho Jeong list_for_each_entry(entry, &sbi->s_freed_data_list, efd_list) {
3919a0154344SDaeho Jeong if (entry->efd_tid != commit_tid)
3920a0154344SDaeho Jeong break;
3921a0154344SDaeho Jeong cut_pos = &entry->efd_list;
3922a0154344SDaeho Jeong }
3923a0154344SDaeho Jeong if (cut_pos)
3924a0154344SDaeho Jeong list_cut_position(&freed_data_list, &sbi->s_freed_data_list,
3925a0154344SDaeho Jeong cut_pos);
3926a0154344SDaeho Jeong spin_unlock(&sbi->s_md_lock);
3927a0154344SDaeho Jeong
392855cdd0afSWang Jianchao list_for_each_entry(entry, &freed_data_list, efd_list)
3929a0154344SDaeho Jeong ext4_free_data_in_buddy(sb, entry);
393055cdd0afSWang Jianchao
393155cdd0afSWang Jianchao if (test_opt(sb, DISCARD)) {
393255cdd0afSWang Jianchao spin_lock(&sbi->s_md_lock);
393355cdd0afSWang Jianchao wake = list_empty(&sbi->s_discard_list);
393455cdd0afSWang Jianchao list_splice_tail(&freed_data_list, &sbi->s_discard_list);
393555cdd0afSWang Jianchao spin_unlock(&sbi->s_md_lock);
393655cdd0afSWang Jianchao if (wake)
393755cdd0afSWang Jianchao queue_work(system_unbound_wq, &sbi->s_discard_work);
393855cdd0afSWang Jianchao } else {
393955cdd0afSWang Jianchao list_for_each_entry_safe(entry, tmp, &freed_data_list, efd_list)
394055cdd0afSWang Jianchao kmem_cache_free(ext4_free_data_cachep, entry);
394155cdd0afSWang Jianchao }
3942a0154344SDaeho Jeong }
3943a0154344SDaeho Jeong
ext4_init_mballoc(void)39445dabfc78STheodore Ts'o int __init ext4_init_mballoc(void)
3945c9de560dSAlex Tomas {
394616828088STheodore Ts'o ext4_pspace_cachep = KMEM_CACHE(ext4_prealloc_space,
394716828088STheodore Ts'o SLAB_RECLAIM_ACCOUNT);
3948c9de560dSAlex Tomas if (ext4_pspace_cachep == NULL)
3949f283529aSRitesh Harjani goto out;
3950c9de560dSAlex Tomas
395116828088STheodore Ts'o ext4_ac_cachep = KMEM_CACHE(ext4_allocation_context,
395216828088STheodore Ts'o SLAB_RECLAIM_ACCOUNT);
3953f283529aSRitesh Harjani if (ext4_ac_cachep == NULL)
3954f283529aSRitesh Harjani goto out_pa_free;
3955c894058dSAneesh Kumar K.V
395618aadd47SBobi Jam ext4_free_data_cachep = KMEM_CACHE(ext4_free_data,
395716828088STheodore Ts'o SLAB_RECLAIM_ACCOUNT);
3958f283529aSRitesh Harjani if (ext4_free_data_cachep == NULL)
3959f283529aSRitesh Harjani goto out_ac_free;
3960f283529aSRitesh Harjani
3961c9de560dSAlex Tomas return 0;
3962f283529aSRitesh Harjani
3963f283529aSRitesh Harjani out_ac_free:
3964f283529aSRitesh Harjani kmem_cache_destroy(ext4_ac_cachep);
3965f283529aSRitesh Harjani out_pa_free:
3966f283529aSRitesh Harjani kmem_cache_destroy(ext4_pspace_cachep);
3967f283529aSRitesh Harjani out:
3968f283529aSRitesh Harjani return -ENOMEM;
3969c9de560dSAlex Tomas }
3970c9de560dSAlex Tomas
ext4_exit_mballoc(void)39715dabfc78STheodore Ts'o void ext4_exit_mballoc(void)
3972c9de560dSAlex Tomas {
39733e03f9caSJesper Dangaard Brouer /*
39743e03f9caSJesper Dangaard Brouer * Wait for completion of call_rcu()'s on ext4_pspace_cachep
39753e03f9caSJesper Dangaard Brouer * before destroying the slab cache.
39763e03f9caSJesper Dangaard Brouer */
39773e03f9caSJesper Dangaard Brouer rcu_barrier();
3978c9de560dSAlex Tomas kmem_cache_destroy(ext4_pspace_cachep);
3979256bdb49SEric Sandeen kmem_cache_destroy(ext4_ac_cachep);
398018aadd47SBobi Jam kmem_cache_destroy(ext4_free_data_cachep);
39812892c15dSEric Sandeen ext4_groupinfo_destroy_slabs();
3982c9de560dSAlex Tomas }
3983c9de560dSAlex Tomas
3984c9de560dSAlex Tomas
3985c9de560dSAlex Tomas /*
398673b2c716SUwe Kleine-König * Check quota and mark chosen space (ac->ac_b_ex) non-free in bitmaps
3987c9de560dSAlex Tomas * Returns 0 if success or error code
3988c9de560dSAlex Tomas */
39894ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_mark_diskspace_used(struct ext4_allocation_context * ac,handle_t * handle,unsigned int reserv_clstrs)39904ddfef7bSEric Sandeen ext4_mb_mark_diskspace_used(struct ext4_allocation_context *ac,
399153accfa9STheodore Ts'o handle_t *handle, unsigned int reserv_clstrs)
3992c9de560dSAlex Tomas {
3993c9de560dSAlex Tomas struct buffer_head *bitmap_bh = NULL;
3994c9de560dSAlex Tomas struct ext4_group_desc *gdp;
3995c9de560dSAlex Tomas struct buffer_head *gdp_bh;
3996c9de560dSAlex Tomas struct ext4_sb_info *sbi;
3997c9de560dSAlex Tomas struct super_block *sb;
3998c9de560dSAlex Tomas ext4_fsblk_t block;
3999519deca0SAneesh Kumar K.V int err, len;
4000c9de560dSAlex Tomas
4001c9de560dSAlex Tomas BUG_ON(ac->ac_status != AC_STATUS_FOUND);
4002c9de560dSAlex Tomas BUG_ON(ac->ac_b_ex.fe_len <= 0);
4003c9de560dSAlex Tomas
4004c9de560dSAlex Tomas sb = ac->ac_sb;
4005c9de560dSAlex Tomas sbi = EXT4_SB(sb);
4006c9de560dSAlex Tomas
4007574ca174STheodore Ts'o bitmap_bh = ext4_read_block_bitmap(sb, ac->ac_b_ex.fe_group);
40089008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) {
4009fb28f9ceSKemeng Shi return PTR_ERR(bitmap_bh);
40109008a58eSDarrick J. Wong }
4011c9de560dSAlex Tomas
40125d601255Sliang xie BUFFER_TRACE(bitmap_bh, "getting write access");
4013188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bitmap_bh,
4014188c299eSJan Kara EXT4_JTR_NONE);
4015c9de560dSAlex Tomas if (err)
4016c9de560dSAlex Tomas goto out_err;
4017c9de560dSAlex Tomas
4018c9de560dSAlex Tomas err = -EIO;
4019c9de560dSAlex Tomas gdp = ext4_get_group_desc(sb, ac->ac_b_ex.fe_group, &gdp_bh);
4020c9de560dSAlex Tomas if (!gdp)
4021c9de560dSAlex Tomas goto out_err;
4022c9de560dSAlex Tomas
4023a9df9a49STheodore Ts'o ext4_debug("using block group %u(%d)\n", ac->ac_b_ex.fe_group,
4024021b65bbSTheodore Ts'o ext4_free_group_clusters(sb, gdp));
402503cddb80SAneesh Kumar K.V
40265d601255Sliang xie BUFFER_TRACE(gdp_bh, "get_write_access");
4027188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gdp_bh, EXT4_JTR_NONE);
4028c9de560dSAlex Tomas if (err)
4029c9de560dSAlex Tomas goto out_err;
4030c9de560dSAlex Tomas
4031bda00de7SAkinobu Mita block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
4032c9de560dSAlex Tomas
403353accfa9STheodore Ts'o len = EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
4034ce9f24ccSJan Kara if (!ext4_inode_block_valid(ac->ac_inode, block, len)) {
403512062dddSEric Sandeen ext4_error(sb, "Allocating blocks %llu-%llu which overlap "
40361084f252STheodore Ts'o "fs metadata", block, block+len);
4037519deca0SAneesh Kumar K.V /* File system mounted not to panic on error
4038554a5cccSVegard Nossum * Fix the bitmap and return EFSCORRUPTED
4039519deca0SAneesh Kumar K.V * We leak some of the blocks here.
4040519deca0SAneesh Kumar K.V */
4041955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, ac->ac_b_ex.fe_group);
4042123e3016SRitesh Harjani mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
4043519deca0SAneesh Kumar K.V ac->ac_b_ex.fe_len);
4044955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
40450390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4046519deca0SAneesh Kumar K.V if (!err)
4047554a5cccSVegard Nossum err = -EFSCORRUPTED;
4048519deca0SAneesh Kumar K.V goto out_err;
4049c9de560dSAlex Tomas }
4050955ce5f5SAneesh Kumar K.V
4051955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, ac->ac_b_ex.fe_group);
4052c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
4053c9de560dSAlex Tomas {
4054c9de560dSAlex Tomas int i;
4055c9de560dSAlex Tomas for (i = 0; i < ac->ac_b_ex.fe_len; i++) {
4056c9de560dSAlex Tomas BUG_ON(mb_test_bit(ac->ac_b_ex.fe_start + i,
4057c9de560dSAlex Tomas bitmap_bh->b_data));
4058c9de560dSAlex Tomas }
4059c9de560dSAlex Tomas }
4060c9de560dSAlex Tomas #endif
4061123e3016SRitesh Harjani mb_set_bits(bitmap_bh->b_data, ac->ac_b_ex.fe_start,
4062c3e94d1dSYongqiang Yang ac->ac_b_ex.fe_len);
40638844618dSTheodore Ts'o if (ext4_has_group_desc_csum(sb) &&
40648844618dSTheodore Ts'o (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
4065c9de560dSAlex Tomas gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
4066021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp,
4067cff1dfd7STheodore Ts'o ext4_free_clusters_after_init(sb,
4068560671a0SAneesh Kumar K.V ac->ac_b_ex.fe_group, gdp));
4069c9de560dSAlex Tomas }
4070021b65bbSTheodore Ts'o len = ext4_free_group_clusters(sb, gdp) - ac->ac_b_ex.fe_len;
4071021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, len);
40721df9bde4SKemeng Shi ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
4073feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, ac->ac_b_ex.fe_group, gdp);
4074955ce5f5SAneesh Kumar K.V
4075955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
407657042651STheodore Ts'o percpu_counter_sub(&sbi->s_freeclusters_counter, ac->ac_b_ex.fe_len);
4077d2a17637SMingming Cao /*
40786bc6e63fSAneesh Kumar K.V * Now reduce the dirty block count also. Should not go negative
4079d2a17637SMingming Cao */
40806bc6e63fSAneesh Kumar K.V if (!(ac->ac_flags & EXT4_MB_DELALLOC_RESERVED))
40816bc6e63fSAneesh Kumar K.V /* release all the reserved blocks if non delalloc */
408257042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter,
408357042651STheodore Ts'o reserv_clstrs);
4084c9de560dSAlex Tomas
4085772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) {
4086772cb7c8SJose R. Santos ext4_group_t flex_group = ext4_flex_group(sbi,
4087772cb7c8SJose R. Santos ac->ac_b_ex.fe_group);
408890ba983fSTheodore Ts'o atomic64_sub(ac->ac_b_ex.fe_len,
40897c990728SSuraj Jitindar Singh &sbi_array_rcu_deref(sbi, s_flex_groups,
40907c990728SSuraj Jitindar Singh flex_group)->free_clusters);
4091772cb7c8SJose R. Santos }
4092772cb7c8SJose R. Santos
40930390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
4094c9de560dSAlex Tomas if (err)
4095c9de560dSAlex Tomas goto out_err;
40960390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, gdp_bh);
4097c9de560dSAlex Tomas
4098c9de560dSAlex Tomas out_err:
409942a10addSAneesh Kumar K.V brelse(bitmap_bh);
4100c9de560dSAlex Tomas return err;
4101c9de560dSAlex Tomas }
4102c9de560dSAlex Tomas
4103c9de560dSAlex Tomas /*
41048016e29fSHarshad Shirwadkar * Idempotent helper for Ext4 fast commit replay path to set the state of
41058016e29fSHarshad Shirwadkar * blocks in bitmaps and update counters.
41068016e29fSHarshad Shirwadkar */
ext4_mb_mark_bb(struct super_block * sb,ext4_fsblk_t block,int len,int state)41078016e29fSHarshad Shirwadkar void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
41088016e29fSHarshad Shirwadkar int len, int state)
41098016e29fSHarshad Shirwadkar {
41108016e29fSHarshad Shirwadkar struct buffer_head *bitmap_bh = NULL;
41118016e29fSHarshad Shirwadkar struct ext4_group_desc *gdp;
41128016e29fSHarshad Shirwadkar struct buffer_head *gdp_bh;
41138016e29fSHarshad Shirwadkar struct ext4_sb_info *sbi = EXT4_SB(sb);
41148016e29fSHarshad Shirwadkar ext4_group_t group;
41158016e29fSHarshad Shirwadkar ext4_grpblk_t blkoff;
4116a50bda14SSu Hui int i, err = 0;
41178016e29fSHarshad Shirwadkar int already;
4118bfdc502aSRitesh Harjani unsigned int clen, clen_changed, thisgrp_len;
41198016e29fSHarshad Shirwadkar
4120bfdc502aSRitesh Harjani while (len > 0) {
41218016e29fSHarshad Shirwadkar ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
4122bfdc502aSRitesh Harjani
4123bfdc502aSRitesh Harjani /*
4124bfdc502aSRitesh Harjani * Check to see if we are freeing blocks across a group
4125bfdc502aSRitesh Harjani * boundary.
4126bfdc502aSRitesh Harjani * In case of flex_bg, this can happen that (block, len) may
4127bfdc502aSRitesh Harjani * span across more than one group. In that case we need to
4128bfdc502aSRitesh Harjani * get the corresponding group metadata to work with.
4129bfdc502aSRitesh Harjani * For this we have goto again loop.
4130bfdc502aSRitesh Harjani */
4131bfdc502aSRitesh Harjani thisgrp_len = min_t(unsigned int, (unsigned int)len,
4132bfdc502aSRitesh Harjani EXT4_BLOCKS_PER_GROUP(sb) - EXT4_C2B(sbi, blkoff));
4133bfdc502aSRitesh Harjani clen = EXT4_NUM_B2C(sbi, thisgrp_len);
4134bfdc502aSRitesh Harjani
41358c91c579SRitesh Harjani if (!ext4_sb_block_valid(sb, NULL, block, thisgrp_len)) {
41368c91c579SRitesh Harjani ext4_error(sb, "Marking blocks in system zone - "
41378c91c579SRitesh Harjani "Block = %llu, len = %u",
41388c91c579SRitesh Harjani block, thisgrp_len);
41398c91c579SRitesh Harjani bitmap_bh = NULL;
41408c91c579SRitesh Harjani break;
41418c91c579SRitesh Harjani }
41428c91c579SRitesh Harjani
41438016e29fSHarshad Shirwadkar bitmap_bh = ext4_read_block_bitmap(sb, group);
41448016e29fSHarshad Shirwadkar if (IS_ERR(bitmap_bh)) {
41458016e29fSHarshad Shirwadkar err = PTR_ERR(bitmap_bh);
41468016e29fSHarshad Shirwadkar bitmap_bh = NULL;
4147bfdc502aSRitesh Harjani break;
41488016e29fSHarshad Shirwadkar }
41498016e29fSHarshad Shirwadkar
41508016e29fSHarshad Shirwadkar err = -EIO;
41518016e29fSHarshad Shirwadkar gdp = ext4_get_group_desc(sb, group, &gdp_bh);
41528016e29fSHarshad Shirwadkar if (!gdp)
4153bfdc502aSRitesh Harjani break;
41548016e29fSHarshad Shirwadkar
41558016e29fSHarshad Shirwadkar ext4_lock_group(sb, group);
41568016e29fSHarshad Shirwadkar already = 0;
41578016e29fSHarshad Shirwadkar for (i = 0; i < clen; i++)
4158bfdc502aSRitesh Harjani if (!mb_test_bit(blkoff + i, bitmap_bh->b_data) ==
4159bfdc502aSRitesh Harjani !state)
41608016e29fSHarshad Shirwadkar already++;
41618016e29fSHarshad Shirwadkar
4162a5c0e2fdSRitesh Harjani clen_changed = clen - already;
41638016e29fSHarshad Shirwadkar if (state)
4164123e3016SRitesh Harjani mb_set_bits(bitmap_bh->b_data, blkoff, clen);
41658016e29fSHarshad Shirwadkar else
4166bd8247eeSRitesh Harjani mb_clear_bits(bitmap_bh->b_data, blkoff, clen);
41678016e29fSHarshad Shirwadkar if (ext4_has_group_desc_csum(sb) &&
41688016e29fSHarshad Shirwadkar (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
41698016e29fSHarshad Shirwadkar gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
41708016e29fSHarshad Shirwadkar ext4_free_group_clusters_set(sb, gdp,
4171bfdc502aSRitesh Harjani ext4_free_clusters_after_init(sb, group, gdp));
41728016e29fSHarshad Shirwadkar }
41738016e29fSHarshad Shirwadkar if (state)
4174a5c0e2fdSRitesh Harjani clen = ext4_free_group_clusters(sb, gdp) - clen_changed;
41758016e29fSHarshad Shirwadkar else
4176a5c0e2fdSRitesh Harjani clen = ext4_free_group_clusters(sb, gdp) + clen_changed;
41778016e29fSHarshad Shirwadkar
41788016e29fSHarshad Shirwadkar ext4_free_group_clusters_set(sb, gdp, clen);
41791df9bde4SKemeng Shi ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
41808016e29fSHarshad Shirwadkar ext4_group_desc_csum_set(sb, group, gdp);
41818016e29fSHarshad Shirwadkar
41828016e29fSHarshad Shirwadkar ext4_unlock_group(sb, group);
41838016e29fSHarshad Shirwadkar
41848016e29fSHarshad Shirwadkar if (sbi->s_log_groups_per_flex) {
41858016e29fSHarshad Shirwadkar ext4_group_t flex_group = ext4_flex_group(sbi, group);
4186a5c0e2fdSRitesh Harjani struct flex_groups *fg = sbi_array_rcu_deref(sbi,
4187a5c0e2fdSRitesh Harjani s_flex_groups, flex_group);
41888016e29fSHarshad Shirwadkar
4189a5c0e2fdSRitesh Harjani if (state)
4190a5c0e2fdSRitesh Harjani atomic64_sub(clen_changed, &fg->free_clusters);
4191a5c0e2fdSRitesh Harjani else
4192a5c0e2fdSRitesh Harjani atomic64_add(clen_changed, &fg->free_clusters);
4193bfdc502aSRitesh Harjani
41948016e29fSHarshad Shirwadkar }
41958016e29fSHarshad Shirwadkar
41968016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
41978016e29fSHarshad Shirwadkar if (err)
4198bfdc502aSRitesh Harjani break;
41998016e29fSHarshad Shirwadkar sync_dirty_buffer(bitmap_bh);
42008016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
42018016e29fSHarshad Shirwadkar sync_dirty_buffer(gdp_bh);
4202bfdc502aSRitesh Harjani if (err)
4203bfdc502aSRitesh Harjani break;
42048016e29fSHarshad Shirwadkar
4205bfdc502aSRitesh Harjani block += thisgrp_len;
4206bfdc502aSRitesh Harjani len -= thisgrp_len;
4207bfdc502aSRitesh Harjani brelse(bitmap_bh);
4208bfdc502aSRitesh Harjani BUG_ON(len < 0);
4209bfdc502aSRitesh Harjani }
4210bfdc502aSRitesh Harjani
4211bfdc502aSRitesh Harjani if (err)
42128016e29fSHarshad Shirwadkar brelse(bitmap_bh);
42138016e29fSHarshad Shirwadkar }
42148016e29fSHarshad Shirwadkar
42158016e29fSHarshad Shirwadkar /*
4216c9de560dSAlex Tomas * here we normalize request for locality group
4217d7a1fee1SDan Ehrenberg * Group request are normalized to s_mb_group_prealloc, which goes to
4218d7a1fee1SDan Ehrenberg * s_strip if we set the same via mount option.
4219d7a1fee1SDan Ehrenberg * s_mb_group_prealloc can be configured via
4220b713a5ecSTheodore Ts'o * /sys/fs/ext4/<partition>/mb_group_prealloc
4221c9de560dSAlex Tomas *
4222c9de560dSAlex Tomas * XXX: should we try to preallocate more than the group has now?
4223c9de560dSAlex Tomas */
ext4_mb_normalize_group_request(struct ext4_allocation_context * ac)4224c9de560dSAlex Tomas static void ext4_mb_normalize_group_request(struct ext4_allocation_context *ac)
4225c9de560dSAlex Tomas {
4226c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
4227c9de560dSAlex Tomas struct ext4_locality_group *lg = ac->ac_lg;
4228c9de560dSAlex Tomas
4229c9de560dSAlex Tomas BUG_ON(lg == NULL);
4230c9de560dSAlex Tomas ac->ac_g_ex.fe_len = EXT4_SB(sb)->s_mb_group_prealloc;
4231d3df1453SRitesh Harjani mb_debug(sb, "goal %u blocks for locality group\n", ac->ac_g_ex.fe_len);
4232c9de560dSAlex Tomas }
4233c9de560dSAlex Tomas
423438727786SOjaswin Mujoo /*
423538727786SOjaswin Mujoo * This function returns the next element to look at during inode
423638727786SOjaswin Mujoo * PA rbtree walk. We assume that we have held the inode PA rbtree lock
423738727786SOjaswin Mujoo * (ei->i_prealloc_lock)
423838727786SOjaswin Mujoo *
423938727786SOjaswin Mujoo * new_start The start of the range we want to compare
424038727786SOjaswin Mujoo * cur_start The existing start that we are comparing against
424138727786SOjaswin Mujoo * node The node of the rb_tree
424238727786SOjaswin Mujoo */
424338727786SOjaswin Mujoo static inline struct rb_node*
ext4_mb_pa_rb_next_iter(ext4_lblk_t new_start,ext4_lblk_t cur_start,struct rb_node * node)424438727786SOjaswin Mujoo ext4_mb_pa_rb_next_iter(ext4_lblk_t new_start, ext4_lblk_t cur_start, struct rb_node *node)
424538727786SOjaswin Mujoo {
424638727786SOjaswin Mujoo if (new_start < cur_start)
424738727786SOjaswin Mujoo return node->rb_left;
424838727786SOjaswin Mujoo else
424938727786SOjaswin Mujoo return node->rb_right;
425038727786SOjaswin Mujoo }
425138727786SOjaswin Mujoo
42527692094aSOjaswin Mujoo static inline void
ext4_mb_pa_assert_overlap(struct ext4_allocation_context * ac,ext4_lblk_t start,loff_t end)42537692094aSOjaswin Mujoo ext4_mb_pa_assert_overlap(struct ext4_allocation_context *ac,
4254bedc5d34SBaokun Li ext4_lblk_t start, loff_t end)
42557692094aSOjaswin Mujoo {
42567692094aSOjaswin Mujoo struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
42577692094aSOjaswin Mujoo struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
42587692094aSOjaswin Mujoo struct ext4_prealloc_space *tmp_pa;
4259bedc5d34SBaokun Li ext4_lblk_t tmp_pa_start;
4260bedc5d34SBaokun Li loff_t tmp_pa_end;
426138727786SOjaswin Mujoo struct rb_node *iter;
42627692094aSOjaswin Mujoo
426338727786SOjaswin Mujoo read_lock(&ei->i_prealloc_lock);
426438727786SOjaswin Mujoo for (iter = ei->i_prealloc_node.rb_node; iter;
426538727786SOjaswin Mujoo iter = ext4_mb_pa_rb_next_iter(start, tmp_pa_start, iter)) {
426638727786SOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
426738727786SOjaswin Mujoo pa_node.inode_node);
42687692094aSOjaswin Mujoo tmp_pa_start = tmp_pa->pa_lstart;
4269bedc5d34SBaokun Li tmp_pa_end = pa_logical_end(sbi, tmp_pa);
42707692094aSOjaswin Mujoo
427138727786SOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
427238727786SOjaswin Mujoo if (tmp_pa->pa_deleted == 0)
42737692094aSOjaswin Mujoo BUG_ON(!(start >= tmp_pa_end || end <= tmp_pa_start));
42747692094aSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
42757692094aSOjaswin Mujoo }
427638727786SOjaswin Mujoo read_unlock(&ei->i_prealloc_lock);
42777692094aSOjaswin Mujoo }
42787692094aSOjaswin Mujoo
4279c9de560dSAlex Tomas /*
42800830344cSOjaswin Mujoo * Given an allocation context "ac" and a range "start", "end", check
42810830344cSOjaswin Mujoo * and adjust boundaries if the range overlaps with any of the existing
42820830344cSOjaswin Mujoo * preallocatoins stored in the corresponding inode of the allocation context.
42830830344cSOjaswin Mujoo *
42840830344cSOjaswin Mujoo * Parameters:
42850830344cSOjaswin Mujoo * ac allocation context
42860830344cSOjaswin Mujoo * start start of the new range
42870830344cSOjaswin Mujoo * end end of the new range
42880830344cSOjaswin Mujoo */
42890830344cSOjaswin Mujoo static inline void
ext4_mb_pa_adjust_overlap(struct ext4_allocation_context * ac,ext4_lblk_t * start,loff_t * end)42900830344cSOjaswin Mujoo ext4_mb_pa_adjust_overlap(struct ext4_allocation_context *ac,
4291bedc5d34SBaokun Li ext4_lblk_t *start, loff_t *end)
42920830344cSOjaswin Mujoo {
42930830344cSOjaswin Mujoo struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
42940830344cSOjaswin Mujoo struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
429538727786SOjaswin Mujoo struct ext4_prealloc_space *tmp_pa = NULL, *left_pa = NULL, *right_pa = NULL;
429638727786SOjaswin Mujoo struct rb_node *iter;
4297bedc5d34SBaokun Li ext4_lblk_t new_start, tmp_pa_start, right_pa_start = -1;
4298bedc5d34SBaokun Li loff_t new_end, tmp_pa_end, left_pa_end = -1;
42990830344cSOjaswin Mujoo
43000830344cSOjaswin Mujoo new_start = *start;
43010830344cSOjaswin Mujoo new_end = *end;
43020830344cSOjaswin Mujoo
430338727786SOjaswin Mujoo /*
430438727786SOjaswin Mujoo * Adjust the normalized range so that it doesn't overlap with any
430538727786SOjaswin Mujoo * existing preallocated blocks(PAs). Make sure to hold the rbtree lock
430638727786SOjaswin Mujoo * so it doesn't change underneath us.
430738727786SOjaswin Mujoo */
430838727786SOjaswin Mujoo read_lock(&ei->i_prealloc_lock);
43090830344cSOjaswin Mujoo
431038727786SOjaswin Mujoo /* Step 1: find any one immediate neighboring PA of the normalized range */
431138727786SOjaswin Mujoo for (iter = ei->i_prealloc_node.rb_node; iter;
431238727786SOjaswin Mujoo iter = ext4_mb_pa_rb_next_iter(ac->ac_o_ex.fe_logical,
431338727786SOjaswin Mujoo tmp_pa_start, iter)) {
431438727786SOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
431538727786SOjaswin Mujoo pa_node.inode_node);
43160830344cSOjaswin Mujoo tmp_pa_start = tmp_pa->pa_lstart;
4317bedc5d34SBaokun Li tmp_pa_end = pa_logical_end(sbi, tmp_pa);
43180830344cSOjaswin Mujoo
43190830344cSOjaswin Mujoo /* PA must not overlap original request */
432038727786SOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
432138727786SOjaswin Mujoo if (tmp_pa->pa_deleted == 0)
43220830344cSOjaswin Mujoo BUG_ON(!(ac->ac_o_ex.fe_logical >= tmp_pa_end ||
43230830344cSOjaswin Mujoo ac->ac_o_ex.fe_logical < tmp_pa_start));
43240830344cSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
43250830344cSOjaswin Mujoo }
43260830344cSOjaswin Mujoo
432738727786SOjaswin Mujoo /*
432838727786SOjaswin Mujoo * Step 2: check if the found PA is left or right neighbor and
432938727786SOjaswin Mujoo * get the other neighbor
433038727786SOjaswin Mujoo */
433138727786SOjaswin Mujoo if (tmp_pa) {
433238727786SOjaswin Mujoo if (tmp_pa->pa_lstart < ac->ac_o_ex.fe_logical) {
433338727786SOjaswin Mujoo struct rb_node *tmp;
433438727786SOjaswin Mujoo
433538727786SOjaswin Mujoo left_pa = tmp_pa;
433638727786SOjaswin Mujoo tmp = rb_next(&left_pa->pa_node.inode_node);
433738727786SOjaswin Mujoo if (tmp) {
433838727786SOjaswin Mujoo right_pa = rb_entry(tmp,
433938727786SOjaswin Mujoo struct ext4_prealloc_space,
434038727786SOjaswin Mujoo pa_node.inode_node);
434138727786SOjaswin Mujoo }
434238727786SOjaswin Mujoo } else {
434338727786SOjaswin Mujoo struct rb_node *tmp;
434438727786SOjaswin Mujoo
434538727786SOjaswin Mujoo right_pa = tmp_pa;
434638727786SOjaswin Mujoo tmp = rb_prev(&right_pa->pa_node.inode_node);
434738727786SOjaswin Mujoo if (tmp) {
434838727786SOjaswin Mujoo left_pa = rb_entry(tmp,
434938727786SOjaswin Mujoo struct ext4_prealloc_space,
435038727786SOjaswin Mujoo pa_node.inode_node);
435138727786SOjaswin Mujoo }
435238727786SOjaswin Mujoo }
435338727786SOjaswin Mujoo }
435438727786SOjaswin Mujoo
435538727786SOjaswin Mujoo /* Step 3: get the non deleted neighbors */
435638727786SOjaswin Mujoo if (left_pa) {
435738727786SOjaswin Mujoo for (iter = &left_pa->pa_node.inode_node;;
435838727786SOjaswin Mujoo iter = rb_prev(iter)) {
435938727786SOjaswin Mujoo if (!iter) {
436038727786SOjaswin Mujoo left_pa = NULL;
436138727786SOjaswin Mujoo break;
436238727786SOjaswin Mujoo }
436338727786SOjaswin Mujoo
436438727786SOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
436538727786SOjaswin Mujoo pa_node.inode_node);
436638727786SOjaswin Mujoo left_pa = tmp_pa;
436738727786SOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
436838727786SOjaswin Mujoo if (tmp_pa->pa_deleted == 0) {
436938727786SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
437038727786SOjaswin Mujoo break;
43710830344cSOjaswin Mujoo }
43720830344cSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
43730830344cSOjaswin Mujoo }
437438727786SOjaswin Mujoo }
437538727786SOjaswin Mujoo
437638727786SOjaswin Mujoo if (right_pa) {
437738727786SOjaswin Mujoo for (iter = &right_pa->pa_node.inode_node;;
437838727786SOjaswin Mujoo iter = rb_next(iter)) {
437938727786SOjaswin Mujoo if (!iter) {
438038727786SOjaswin Mujoo right_pa = NULL;
438138727786SOjaswin Mujoo break;
438238727786SOjaswin Mujoo }
438338727786SOjaswin Mujoo
438438727786SOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
438538727786SOjaswin Mujoo pa_node.inode_node);
438638727786SOjaswin Mujoo right_pa = tmp_pa;
438738727786SOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
438838727786SOjaswin Mujoo if (tmp_pa->pa_deleted == 0) {
438938727786SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
439038727786SOjaswin Mujoo break;
439138727786SOjaswin Mujoo }
439238727786SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
439338727786SOjaswin Mujoo }
439438727786SOjaswin Mujoo }
439538727786SOjaswin Mujoo
439638727786SOjaswin Mujoo if (left_pa) {
4397bedc5d34SBaokun Li left_pa_end = pa_logical_end(sbi, left_pa);
439838727786SOjaswin Mujoo BUG_ON(left_pa_end > ac->ac_o_ex.fe_logical);
439938727786SOjaswin Mujoo }
440038727786SOjaswin Mujoo
440138727786SOjaswin Mujoo if (right_pa) {
440238727786SOjaswin Mujoo right_pa_start = right_pa->pa_lstart;
440338727786SOjaswin Mujoo BUG_ON(right_pa_start <= ac->ac_o_ex.fe_logical);
440438727786SOjaswin Mujoo }
440538727786SOjaswin Mujoo
440638727786SOjaswin Mujoo /* Step 4: trim our normalized range to not overlap with the neighbors */
440738727786SOjaswin Mujoo if (left_pa) {
440838727786SOjaswin Mujoo if (left_pa_end > new_start)
440938727786SOjaswin Mujoo new_start = left_pa_end;
441038727786SOjaswin Mujoo }
441138727786SOjaswin Mujoo
441238727786SOjaswin Mujoo if (right_pa) {
441338727786SOjaswin Mujoo if (right_pa_start < new_end)
441438727786SOjaswin Mujoo new_end = right_pa_start;
441538727786SOjaswin Mujoo }
441638727786SOjaswin Mujoo read_unlock(&ei->i_prealloc_lock);
44170830344cSOjaswin Mujoo
44180830344cSOjaswin Mujoo /* XXX: extra loop to check we really don't overlap preallocations */
44190830344cSOjaswin Mujoo ext4_mb_pa_assert_overlap(ac, new_start, new_end);
44200830344cSOjaswin Mujoo
44210830344cSOjaswin Mujoo *start = new_start;
44220830344cSOjaswin Mujoo *end = new_end;
44230830344cSOjaswin Mujoo }
44240830344cSOjaswin Mujoo
44250830344cSOjaswin Mujoo /*
4426c9de560dSAlex Tomas * Normalization means making request better in terms of
4427c9de560dSAlex Tomas * size and alignment
4428c9de560dSAlex Tomas */
44294ddfef7bSEric Sandeen static noinline_for_stack void
ext4_mb_normalize_request(struct ext4_allocation_context * ac,struct ext4_allocation_request * ar)44304ddfef7bSEric Sandeen ext4_mb_normalize_request(struct ext4_allocation_context *ac,
4431c9de560dSAlex Tomas struct ext4_allocation_request *ar)
4432c9de560dSAlex Tomas {
443353accfa9STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4434b07ffe69SKemeng Shi struct ext4_super_block *es = sbi->s_es;
4435c9de560dSAlex Tomas int bsbits, max;
4436bedc5d34SBaokun Li loff_t size, start_off, end;
44371592d2c5SCurt Wohlgemuth loff_t orig_size __maybe_unused;
44385a0790c2SAndi Kleen ext4_lblk_t start;
4439c9de560dSAlex Tomas
4440c9de560dSAlex Tomas /* do normalize only data requests, metadata requests
4441c9de560dSAlex Tomas do not need preallocation */
4442c9de560dSAlex Tomas if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
4443c9de560dSAlex Tomas return;
4444c9de560dSAlex Tomas
4445c9de560dSAlex Tomas /* sometime caller may want exact blocks */
4446c9de560dSAlex Tomas if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
4447c9de560dSAlex Tomas return;
4448c9de560dSAlex Tomas
4449c9de560dSAlex Tomas /* caller may indicate that preallocation isn't
4450c9de560dSAlex Tomas * required (it's a tail, for example) */
4451c9de560dSAlex Tomas if (ac->ac_flags & EXT4_MB_HINT_NOPREALLOC)
4452c9de560dSAlex Tomas return;
4453c9de560dSAlex Tomas
4454c9de560dSAlex Tomas if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC) {
4455c9de560dSAlex Tomas ext4_mb_normalize_group_request(ac);
4456c9de560dSAlex Tomas return ;
4457c9de560dSAlex Tomas }
4458c9de560dSAlex Tomas
4459c9de560dSAlex Tomas bsbits = ac->ac_sb->s_blocksize_bits;
4460c9de560dSAlex Tomas
4461c9de560dSAlex Tomas /* first, let's learn actual file size
4462c9de560dSAlex Tomas * given current request is allocated */
446343bbddc0SBaokun Li size = extent_logical_end(sbi, &ac->ac_o_ex);
4464c9de560dSAlex Tomas size = size << bsbits;
4465c9de560dSAlex Tomas if (size < i_size_read(ac->ac_inode))
4466c9de560dSAlex Tomas size = i_size_read(ac->ac_inode);
44675a0790c2SAndi Kleen orig_size = size;
4468c9de560dSAlex Tomas
44691930479cSValerie Clement /* max size of free chunks */
44701930479cSValerie Clement max = 2 << bsbits;
4471c9de560dSAlex Tomas
44721930479cSValerie Clement #define NRL_CHECK_SIZE(req, size, max, chunk_size) \
44731930479cSValerie Clement (req <= (size) || max <= (chunk_size))
4474c9de560dSAlex Tomas
4475c9de560dSAlex Tomas /* first, try to predict filesize */
4476c9de560dSAlex Tomas /* XXX: should this table be tunable? */
4477c9de560dSAlex Tomas start_off = 0;
4478c9de560dSAlex Tomas if (size <= 16 * 1024) {
4479c9de560dSAlex Tomas size = 16 * 1024;
4480c9de560dSAlex Tomas } else if (size <= 32 * 1024) {
4481c9de560dSAlex Tomas size = 32 * 1024;
4482c9de560dSAlex Tomas } else if (size <= 64 * 1024) {
4483c9de560dSAlex Tomas size = 64 * 1024;
4484c9de560dSAlex Tomas } else if (size <= 128 * 1024) {
4485c9de560dSAlex Tomas size = 128 * 1024;
4486c9de560dSAlex Tomas } else if (size <= 256 * 1024) {
4487c9de560dSAlex Tomas size = 256 * 1024;
4488c9de560dSAlex Tomas } else if (size <= 512 * 1024) {
4489c9de560dSAlex Tomas size = 512 * 1024;
4490c9de560dSAlex Tomas } else if (size <= 1024 * 1024) {
4491c9de560dSAlex Tomas size = 1024 * 1024;
44921930479cSValerie Clement } else if (NRL_CHECK_SIZE(size, 4 * 1024 * 1024, max, 2 * 1024)) {
4493c9de560dSAlex Tomas start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
44941930479cSValerie Clement (21 - bsbits)) << 21;
44951930479cSValerie Clement size = 2 * 1024 * 1024;
44961930479cSValerie Clement } else if (NRL_CHECK_SIZE(size, 8 * 1024 * 1024, max, 4 * 1024)) {
4497c9de560dSAlex Tomas start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
4498c9de560dSAlex Tomas (22 - bsbits)) << 22;
4499c9de560dSAlex Tomas size = 4 * 1024 * 1024;
4500b3916da0SKemeng Shi } else if (NRL_CHECK_SIZE(EXT4_C2B(sbi, ac->ac_o_ex.fe_len),
45011930479cSValerie Clement (8<<20)>>bsbits, max, 8 * 1024)) {
4502c9de560dSAlex Tomas start_off = ((loff_t)ac->ac_o_ex.fe_logical >>
4503c9de560dSAlex Tomas (23 - bsbits)) << 23;
4504c9de560dSAlex Tomas size = 8 * 1024 * 1024;
4505c9de560dSAlex Tomas } else {
4506c9de560dSAlex Tomas start_off = (loff_t) ac->ac_o_ex.fe_logical << bsbits;
450791a48aafSKemeng Shi size = (loff_t) EXT4_C2B(sbi,
4508b27b1535SXiaoguang Wang ac->ac_o_ex.fe_len) << bsbits;
4509c9de560dSAlex Tomas }
45105a0790c2SAndi Kleen size = size >> bsbits;
45115a0790c2SAndi Kleen start = start_off >> bsbits;
4512c9de560dSAlex Tomas
4513a08f789dSBaokun Li /*
4514a08f789dSBaokun Li * For tiny groups (smaller than 8MB) the chosen allocation
4515a08f789dSBaokun Li * alignment may be larger than group size. Make sure the
4516a08f789dSBaokun Li * alignment does not move allocation to a different group which
4517a08f789dSBaokun Li * makes mballoc fail assertions later.
4518a08f789dSBaokun Li */
4519a08f789dSBaokun Li start = max(start, rounddown(ac->ac_o_ex.fe_logical,
4520a08f789dSBaokun Li (ext4_lblk_t)EXT4_BLOCKS_PER_GROUP(ac->ac_sb)));
4521a08f789dSBaokun Li
45224f18d187SBaokun Li /* avoid unnecessary preallocation that may trigger assertions */
45234f18d187SBaokun Li if (start + size > EXT_MAX_BLOCKS)
45244f18d187SBaokun Li size = EXT_MAX_BLOCKS - start;
45254f18d187SBaokun Li
4526c9de560dSAlex Tomas /* don't cover already allocated blocks in selected range */
4527c9de560dSAlex Tomas if (ar->pleft && start <= ar->lleft) {
4528c9de560dSAlex Tomas size -= ar->lleft + 1 - start;
4529c9de560dSAlex Tomas start = ar->lleft + 1;
4530c9de560dSAlex Tomas }
4531c9de560dSAlex Tomas if (ar->pright && start + size - 1 >= ar->lright)
4532c9de560dSAlex Tomas size -= start + size - ar->lright;
4533c9de560dSAlex Tomas
4534cd648b8aSJan Kara /*
4535cd648b8aSJan Kara * Trim allocation request for filesystems with artificially small
4536cd648b8aSJan Kara * groups.
4537cd648b8aSJan Kara */
4538cd648b8aSJan Kara if (size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb))
4539cd648b8aSJan Kara size = EXT4_BLOCKS_PER_GROUP(ac->ac_sb);
4540cd648b8aSJan Kara
4541c9de560dSAlex Tomas end = start + size;
4542c9de560dSAlex Tomas
45430830344cSOjaswin Mujoo ext4_mb_pa_adjust_overlap(ac, &start, &end);
4544c9de560dSAlex Tomas
4545c9de560dSAlex Tomas size = end - start;
4546c9de560dSAlex Tomas
4547cf4ff938SBaokun Li /*
4548cf4ff938SBaokun Li * In this function "start" and "size" are normalized for better
4549cf4ff938SBaokun Li * alignment and length such that we could preallocate more blocks.
4550cf4ff938SBaokun Li * This normalization is done such that original request of
4551cf4ff938SBaokun Li * ac->ac_o_ex.fe_logical & fe_len should always lie within "start" and
4552cf4ff938SBaokun Li * "size" boundaries.
4553cf4ff938SBaokun Li * (Note fe_len can be relaxed since FS block allocation API does not
4554cf4ff938SBaokun Li * provide gurantee on number of contiguous blocks allocation since that
4555cf4ff938SBaokun Li * depends upon free space left, etc).
4556cf4ff938SBaokun Li * In case of inode pa, later we use the allocated blocks
45571221b235SKemeng Shi * [pa_pstart + fe_logical - pa_lstart, fe_len/size] from the preallocated
4558cf4ff938SBaokun Li * range of goal/best blocks [start, size] to put it at the
4559cf4ff938SBaokun Li * ac_o_ex.fe_logical extent of this inode.
4560cf4ff938SBaokun Li * (See ext4_mb_use_inode_pa() for more details)
4561cf4ff938SBaokun Li */
4562cf4ff938SBaokun Li if (start + size <= ac->ac_o_ex.fe_logical ||
4563c9de560dSAlex Tomas start > ac->ac_o_ex.fe_logical) {
45649d8b9ec4STheodore Ts'o ext4_msg(ac->ac_sb, KERN_ERR,
45659d8b9ec4STheodore Ts'o "start %lu, size %lu, fe_logical %lu",
4566c9de560dSAlex Tomas (unsigned long) start, (unsigned long) size,
4567c9de560dSAlex Tomas (unsigned long) ac->ac_o_ex.fe_logical);
4568dfe076c1SDmitry Monakhov BUG();
4569c9de560dSAlex Tomas }
4570b5b60778SMaurizio Lombardi BUG_ON(size <= 0 || size > EXT4_BLOCKS_PER_GROUP(ac->ac_sb));
4571c9de560dSAlex Tomas
4572c9de560dSAlex Tomas /* now prepare goal request */
4573c9de560dSAlex Tomas
4574c9de560dSAlex Tomas /* XXX: is it better to align blocks WRT to logical
4575c9de560dSAlex Tomas * placement or satisfy big request as is */
4576c9de560dSAlex Tomas ac->ac_g_ex.fe_logical = start;
457753accfa9STheodore Ts'o ac->ac_g_ex.fe_len = EXT4_NUM_B2C(sbi, size);
45787e170922SOjaswin Mujoo ac->ac_orig_goal_len = ac->ac_g_ex.fe_len;
4579c9de560dSAlex Tomas
4580c9de560dSAlex Tomas /* define goal start in order to merge */
4581b07ffe69SKemeng Shi if (ar->pright && (ar->lright == (start + size)) &&
4582b07ffe69SKemeng Shi ar->pright >= size &&
4583b07ffe69SKemeng Shi ar->pright - size >= le32_to_cpu(es->s_first_data_block)) {
4584c9de560dSAlex Tomas /* merge to the right */
4585c9de560dSAlex Tomas ext4_get_group_no_and_offset(ac->ac_sb, ar->pright - size,
4586b07ffe69SKemeng Shi &ac->ac_g_ex.fe_group,
4587b07ffe69SKemeng Shi &ac->ac_g_ex.fe_start);
4588c9de560dSAlex Tomas ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
4589c9de560dSAlex Tomas }
4590b07ffe69SKemeng Shi if (ar->pleft && (ar->lleft + 1 == start) &&
4591b07ffe69SKemeng Shi ar->pleft + 1 < ext4_blocks_count(es)) {
4592c9de560dSAlex Tomas /* merge to the left */
4593c9de560dSAlex Tomas ext4_get_group_no_and_offset(ac->ac_sb, ar->pleft + 1,
4594b07ffe69SKemeng Shi &ac->ac_g_ex.fe_group,
4595b07ffe69SKemeng Shi &ac->ac_g_ex.fe_start);
4596c9de560dSAlex Tomas ac->ac_flags |= EXT4_MB_HINT_TRY_GOAL;
4597c9de560dSAlex Tomas }
4598c9de560dSAlex Tomas
4599d3df1453SRitesh Harjani mb_debug(ac->ac_sb, "goal: %lld(was %lld) blocks at %u\n", size,
4600d3df1453SRitesh Harjani orig_size, start);
4601c9de560dSAlex Tomas }
4602c9de560dSAlex Tomas
ext4_mb_collect_stats(struct ext4_allocation_context * ac)4603c9de560dSAlex Tomas static void ext4_mb_collect_stats(struct ext4_allocation_context *ac)
4604c9de560dSAlex Tomas {
4605c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4606c9de560dSAlex Tomas
4607a6c75eafSHarshad Shirwadkar if (sbi->s_mb_stats && ac->ac_g_ex.fe_len >= 1) {
4608c9de560dSAlex Tomas atomic_inc(&sbi->s_bal_reqs);
4609c9de560dSAlex Tomas atomic_add(ac->ac_b_ex.fe_len, &sbi->s_bal_allocated);
4610291dae47SCurt Wohlgemuth if (ac->ac_b_ex.fe_len >= ac->ac_o_ex.fe_len)
4611c9de560dSAlex Tomas atomic_inc(&sbi->s_bal_success);
4612fdd9a009SOjaswin Mujoo
4613c9de560dSAlex Tomas atomic_add(ac->ac_found, &sbi->s_bal_ex_scanned);
4614fdd9a009SOjaswin Mujoo for (int i=0; i<EXT4_MB_NUM_CRS; i++) {
4615fdd9a009SOjaswin Mujoo atomic_add(ac->ac_cX_found[i], &sbi->s_bal_cX_ex_scanned[i]);
4616fdd9a009SOjaswin Mujoo }
4617fdd9a009SOjaswin Mujoo
4618a6c75eafSHarshad Shirwadkar atomic_add(ac->ac_groups_scanned, &sbi->s_bal_groups_scanned);
4619c9de560dSAlex Tomas if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
4620c9de560dSAlex Tomas ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
4621c9de560dSAlex Tomas atomic_inc(&sbi->s_bal_goals);
46227e170922SOjaswin Mujoo /* did we allocate as much as normalizer originally wanted? */
46237e170922SOjaswin Mujoo if (ac->ac_f_ex.fe_len == ac->ac_orig_goal_len)
46243ef5d263SOjaswin Mujoo atomic_inc(&sbi->s_bal_len_goals);
46257e170922SOjaswin Mujoo
4626c9de560dSAlex Tomas if (ac->ac_found > sbi->s_mb_max_to_scan)
4627c9de560dSAlex Tomas atomic_inc(&sbi->s_bal_breaks);
4628c9de560dSAlex Tomas }
4629c9de560dSAlex Tomas
4630296c355cSTheodore Ts'o if (ac->ac_op == EXT4_MB_HISTORY_ALLOC)
4631296c355cSTheodore Ts'o trace_ext4_mballoc_alloc(ac);
4632296c355cSTheodore Ts'o else
4633296c355cSTheodore Ts'o trace_ext4_mballoc_prealloc(ac);
4634c9de560dSAlex Tomas }
4635c9de560dSAlex Tomas
4636c9de560dSAlex Tomas /*
4637b844167eSCurt Wohlgemuth * Called on failure; free up any blocks from the inode PA for this
4638b844167eSCurt Wohlgemuth * context. We don't need this for MB_GROUP_PA because we only change
4639b844167eSCurt Wohlgemuth * pa_free in ext4_mb_release_context(), but on failure, we've already
4640b844167eSCurt Wohlgemuth * zeroed out ac->ac_b_ex.fe_len, so group_pa->pa_free is not changed.
4641b844167eSCurt Wohlgemuth */
ext4_discard_allocated_blocks(struct ext4_allocation_context * ac)4642b844167eSCurt Wohlgemuth static void ext4_discard_allocated_blocks(struct ext4_allocation_context *ac)
4643b844167eSCurt Wohlgemuth {
4644b844167eSCurt Wohlgemuth struct ext4_prealloc_space *pa = ac->ac_pa;
464586f0afd4STheodore Ts'o struct ext4_buddy e4b;
464686f0afd4STheodore Ts'o int err;
4647b844167eSCurt Wohlgemuth
464886f0afd4STheodore Ts'o if (pa == NULL) {
4649c99d1e6eSTheodore Ts'o if (ac->ac_f_ex.fe_len == 0)
4650c99d1e6eSTheodore Ts'o return;
465186f0afd4STheodore Ts'o err = ext4_mb_load_buddy(ac->ac_sb, ac->ac_f_ex.fe_group, &e4b);
465219b8b035STheodore Ts'o if (WARN_RATELIMIT(err,
465319b8b035STheodore Ts'o "ext4: mb_load_buddy failed (%d)", err))
465486f0afd4STheodore Ts'o /*
465586f0afd4STheodore Ts'o * This should never happen since we pin the
465686f0afd4STheodore Ts'o * pages in the ext4_allocation_context so
465786f0afd4STheodore Ts'o * ext4_mb_load_buddy() should never fail.
465886f0afd4STheodore Ts'o */
465986f0afd4STheodore Ts'o return;
466086f0afd4STheodore Ts'o ext4_lock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
466186f0afd4STheodore Ts'o mb_free_blocks(ac->ac_inode, &e4b, ac->ac_f_ex.fe_start,
466286f0afd4STheodore Ts'o ac->ac_f_ex.fe_len);
466386f0afd4STheodore Ts'o ext4_unlock_group(ac->ac_sb, ac->ac_f_ex.fe_group);
4664c99d1e6eSTheodore Ts'o ext4_mb_unload_buddy(&e4b);
466586f0afd4STheodore Ts'o return;
466686f0afd4STheodore Ts'o }
466736cb0f52SKemeng Shi if (pa->pa_type == MB_INODE_PA) {
466836cb0f52SKemeng Shi spin_lock(&pa->pa_lock);
4669400db9d3SZheng Liu pa->pa_free += ac->ac_b_ex.fe_len;
467036cb0f52SKemeng Shi spin_unlock(&pa->pa_lock);
467136cb0f52SKemeng Shi }
4672b844167eSCurt Wohlgemuth }
4673b844167eSCurt Wohlgemuth
4674b844167eSCurt Wohlgemuth /*
4675c9de560dSAlex Tomas * use blocks preallocated to inode
4676c9de560dSAlex Tomas */
ext4_mb_use_inode_pa(struct ext4_allocation_context * ac,struct ext4_prealloc_space * pa)4677c9de560dSAlex Tomas static void ext4_mb_use_inode_pa(struct ext4_allocation_context *ac,
4678c9de560dSAlex Tomas struct ext4_prealloc_space *pa)
4679c9de560dSAlex Tomas {
468053accfa9STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
4681c9de560dSAlex Tomas ext4_fsblk_t start;
4682c9de560dSAlex Tomas ext4_fsblk_t end;
4683c9de560dSAlex Tomas int len;
4684c9de560dSAlex Tomas
4685c9de560dSAlex Tomas /* found preallocated blocks, use them */
4686c9de560dSAlex Tomas start = pa->pa_pstart + (ac->ac_o_ex.fe_logical - pa->pa_lstart);
468753accfa9STheodore Ts'o end = min(pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len),
468853accfa9STheodore Ts'o start + EXT4_C2B(sbi, ac->ac_o_ex.fe_len));
468953accfa9STheodore Ts'o len = EXT4_NUM_B2C(sbi, end - start);
4690c9de560dSAlex Tomas ext4_get_group_no_and_offset(ac->ac_sb, start, &ac->ac_b_ex.fe_group,
4691c9de560dSAlex Tomas &ac->ac_b_ex.fe_start);
4692c9de560dSAlex Tomas ac->ac_b_ex.fe_len = len;
4693c9de560dSAlex Tomas ac->ac_status = AC_STATUS_FOUND;
4694c9de560dSAlex Tomas ac->ac_pa = pa;
4695c9de560dSAlex Tomas
4696c9de560dSAlex Tomas BUG_ON(start < pa->pa_pstart);
469753accfa9STheodore Ts'o BUG_ON(end > pa->pa_pstart + EXT4_C2B(sbi, pa->pa_len));
4698c9de560dSAlex Tomas BUG_ON(pa->pa_free < len);
469993cdf49fSOjaswin Mujoo BUG_ON(ac->ac_b_ex.fe_len <= 0);
4700c9de560dSAlex Tomas pa->pa_free -= len;
4701c9de560dSAlex Tomas
4702d3df1453SRitesh Harjani mb_debug(ac->ac_sb, "use %llu/%d from inode pa %p\n", start, len, pa);
4703c9de560dSAlex Tomas }
4704c9de560dSAlex Tomas
4705c9de560dSAlex Tomas /*
4706c9de560dSAlex Tomas * use blocks preallocated to locality group
4707c9de560dSAlex Tomas */
ext4_mb_use_group_pa(struct ext4_allocation_context * ac,struct ext4_prealloc_space * pa)4708c9de560dSAlex Tomas static void ext4_mb_use_group_pa(struct ext4_allocation_context *ac,
4709c9de560dSAlex Tomas struct ext4_prealloc_space *pa)
4710c9de560dSAlex Tomas {
471103cddb80SAneesh Kumar K.V unsigned int len = ac->ac_o_ex.fe_len;
47126be2ded1SAneesh Kumar K.V
4713c9de560dSAlex Tomas ext4_get_group_no_and_offset(ac->ac_sb, pa->pa_pstart,
4714c9de560dSAlex Tomas &ac->ac_b_ex.fe_group,
4715c9de560dSAlex Tomas &ac->ac_b_ex.fe_start);
4716c9de560dSAlex Tomas ac->ac_b_ex.fe_len = len;
4717c9de560dSAlex Tomas ac->ac_status = AC_STATUS_FOUND;
4718c9de560dSAlex Tomas ac->ac_pa = pa;
4719c9de560dSAlex Tomas
47201221b235SKemeng Shi /* we don't correct pa_pstart or pa_len here to avoid
472126346ff6SAneesh Kumar K.V * possible race when the group is being loaded concurrently
4722c9de560dSAlex Tomas * instead we correct pa later, after blocks are marked
472326346ff6SAneesh Kumar K.V * in on-disk bitmap -- see ext4_mb_release_context()
472426346ff6SAneesh Kumar K.V * Other CPUs are prevented from allocating from this pa by lg_mutex
4725c9de560dSAlex Tomas */
4726d3df1453SRitesh Harjani mb_debug(ac->ac_sb, "use %u/%u from group pa %p\n",
47271afdc588SKemeng Shi pa->pa_lstart, len, pa);
4728c9de560dSAlex Tomas }
4729c9de560dSAlex Tomas
4730c9de560dSAlex Tomas /*
47315e745b04SAneesh Kumar K.V * Return the prealloc space that have minimal distance
47325e745b04SAneesh Kumar K.V * from the goal block. @cpa is the prealloc
47335e745b04SAneesh Kumar K.V * space that is having currently known minimal distance
47345e745b04SAneesh Kumar K.V * from the goal block.
47355e745b04SAneesh Kumar K.V */
47365e745b04SAneesh Kumar K.V static struct ext4_prealloc_space *
ext4_mb_check_group_pa(ext4_fsblk_t goal_block,struct ext4_prealloc_space * pa,struct ext4_prealloc_space * cpa)47375e745b04SAneesh Kumar K.V ext4_mb_check_group_pa(ext4_fsblk_t goal_block,
47385e745b04SAneesh Kumar K.V struct ext4_prealloc_space *pa,
47395e745b04SAneesh Kumar K.V struct ext4_prealloc_space *cpa)
47405e745b04SAneesh Kumar K.V {
47415e745b04SAneesh Kumar K.V ext4_fsblk_t cur_distance, new_distance;
47425e745b04SAneesh Kumar K.V
47435e745b04SAneesh Kumar K.V if (cpa == NULL) {
47445e745b04SAneesh Kumar K.V atomic_inc(&pa->pa_count);
47455e745b04SAneesh Kumar K.V return pa;
47465e745b04SAneesh Kumar K.V }
474779211c8eSAndrew Morton cur_distance = abs(goal_block - cpa->pa_pstart);
474879211c8eSAndrew Morton new_distance = abs(goal_block - pa->pa_pstart);
47495e745b04SAneesh Kumar K.V
47505a54b2f1SColy Li if (cur_distance <= new_distance)
47515e745b04SAneesh Kumar K.V return cpa;
47525e745b04SAneesh Kumar K.V
47535e745b04SAneesh Kumar K.V /* drop the previous reference */
47545e745b04SAneesh Kumar K.V atomic_dec(&cpa->pa_count);
47555e745b04SAneesh Kumar K.V atomic_inc(&pa->pa_count);
47565e745b04SAneesh Kumar K.V return pa;
47575e745b04SAneesh Kumar K.V }
47585e745b04SAneesh Kumar K.V
47595e745b04SAneesh Kumar K.V /*
47601eff5904SKemeng Shi * check if found pa meets EXT4_MB_HINT_GOAL_ONLY
47611eff5904SKemeng Shi */
47621eff5904SKemeng Shi static bool
ext4_mb_pa_goal_check(struct ext4_allocation_context * ac,struct ext4_prealloc_space * pa)47631eff5904SKemeng Shi ext4_mb_pa_goal_check(struct ext4_allocation_context *ac,
47641eff5904SKemeng Shi struct ext4_prealloc_space *pa)
47651eff5904SKemeng Shi {
47661eff5904SKemeng Shi struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
47671eff5904SKemeng Shi ext4_fsblk_t start;
47681eff5904SKemeng Shi
47691eff5904SKemeng Shi if (likely(!(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY)))
47701eff5904SKemeng Shi return true;
47711eff5904SKemeng Shi
47721eff5904SKemeng Shi /*
47731eff5904SKemeng Shi * If EXT4_MB_HINT_GOAL_ONLY is set, ac_g_ex will not be adjusted
47741eff5904SKemeng Shi * in ext4_mb_normalize_request and will keep same with ac_o_ex
47751eff5904SKemeng Shi * from ext4_mb_initialize_context. Choose ac_g_ex here to keep
47761eff5904SKemeng Shi * consistent with ext4_mb_find_by_goal.
47771eff5904SKemeng Shi */
47781eff5904SKemeng Shi start = pa->pa_pstart +
47791eff5904SKemeng Shi (ac->ac_g_ex.fe_logical - pa->pa_lstart);
47801eff5904SKemeng Shi if (ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex) != start)
47811eff5904SKemeng Shi return false;
47821eff5904SKemeng Shi
47831eff5904SKemeng Shi if (ac->ac_g_ex.fe_len > pa->pa_len -
47841eff5904SKemeng Shi EXT4_B2C(sbi, ac->ac_g_ex.fe_logical - pa->pa_lstart))
47851eff5904SKemeng Shi return false;
47861eff5904SKemeng Shi
47871eff5904SKemeng Shi return true;
47881eff5904SKemeng Shi }
47891eff5904SKemeng Shi
47901eff5904SKemeng Shi /*
4791c9de560dSAlex Tomas * search goal blocks in preallocated space
4792c9de560dSAlex Tomas */
47934fca8f07SRitesh Harjani static noinline_for_stack bool
ext4_mb_use_preallocated(struct ext4_allocation_context * ac)47944ddfef7bSEric Sandeen ext4_mb_use_preallocated(struct ext4_allocation_context *ac)
4795c9de560dSAlex Tomas {
479653accfa9STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
47976be2ded1SAneesh Kumar K.V int order, i;
4798c9de560dSAlex Tomas struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
4799c9de560dSAlex Tomas struct ext4_locality_group *lg;
48009d3de7eeSOjaswin Mujoo struct ext4_prealloc_space *tmp_pa = NULL, *cpa = NULL;
480138727786SOjaswin Mujoo struct rb_node *iter;
48025e745b04SAneesh Kumar K.V ext4_fsblk_t goal_block;
4803c9de560dSAlex Tomas
4804c9de560dSAlex Tomas /* only data can be preallocated */
4805c9de560dSAlex Tomas if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
48064fca8f07SRitesh Harjani return false;
4807c9de560dSAlex Tomas
48089d3de7eeSOjaswin Mujoo /*
48099d3de7eeSOjaswin Mujoo * first, try per-file preallocation by searching the inode pa rbtree.
48109d3de7eeSOjaswin Mujoo *
48119d3de7eeSOjaswin Mujoo * Here, we can't do a direct traversal of the tree because
48129d3de7eeSOjaswin Mujoo * ext4_mb_discard_group_preallocation() can paralelly mark the pa
48139d3de7eeSOjaswin Mujoo * deleted and that can cause direct traversal to skip some entries.
48149d3de7eeSOjaswin Mujoo */
481538727786SOjaswin Mujoo read_lock(&ei->i_prealloc_lock);
48169d3de7eeSOjaswin Mujoo
48179d3de7eeSOjaswin Mujoo if (RB_EMPTY_ROOT(&ei->i_prealloc_node)) {
48189d3de7eeSOjaswin Mujoo goto try_group_pa;
48199d3de7eeSOjaswin Mujoo }
48209d3de7eeSOjaswin Mujoo
48219d3de7eeSOjaswin Mujoo /*
48229d3de7eeSOjaswin Mujoo * Step 1: Find a pa with logical start immediately adjacent to the
48239d3de7eeSOjaswin Mujoo * original logical start. This could be on the left or right.
48249d3de7eeSOjaswin Mujoo *
48259d3de7eeSOjaswin Mujoo * (tmp_pa->pa_lstart never changes so we can skip locking for it).
48269d3de7eeSOjaswin Mujoo */
482738727786SOjaswin Mujoo for (iter = ei->i_prealloc_node.rb_node; iter;
482838727786SOjaswin Mujoo iter = ext4_mb_pa_rb_next_iter(ac->ac_o_ex.fe_logical,
48299d3de7eeSOjaswin Mujoo tmp_pa->pa_lstart, iter)) {
483038727786SOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
483138727786SOjaswin Mujoo pa_node.inode_node);
48329d3de7eeSOjaswin Mujoo }
4833c9de560dSAlex Tomas
48349d3de7eeSOjaswin Mujoo /*
48359d3de7eeSOjaswin Mujoo * Step 2: The adjacent pa might be to the right of logical start, find
48369d3de7eeSOjaswin Mujoo * the left adjacent pa. After this step we'd have a valid tmp_pa whose
48379d3de7eeSOjaswin Mujoo * logical start is towards the left of original request's logical start
48389d3de7eeSOjaswin Mujoo */
48399d3de7eeSOjaswin Mujoo if (tmp_pa->pa_lstart > ac->ac_o_ex.fe_logical) {
48409d3de7eeSOjaswin Mujoo struct rb_node *tmp;
48419d3de7eeSOjaswin Mujoo tmp = rb_prev(&tmp_pa->pa_node.inode_node);
4842bcf43499SOjaswin Mujoo
48439d3de7eeSOjaswin Mujoo if (tmp) {
48449d3de7eeSOjaswin Mujoo tmp_pa = rb_entry(tmp, struct ext4_prealloc_space,
48459d3de7eeSOjaswin Mujoo pa_node.inode_node);
48469d3de7eeSOjaswin Mujoo } else {
48479d3de7eeSOjaswin Mujoo /*
48489d3de7eeSOjaswin Mujoo * If there is no adjacent pa to the left then finding
48499d3de7eeSOjaswin Mujoo * an overlapping pa is not possible hence stop searching
48509d3de7eeSOjaswin Mujoo * inode pa tree
48519d3de7eeSOjaswin Mujoo */
48529d3de7eeSOjaswin Mujoo goto try_group_pa;
48539d3de7eeSOjaswin Mujoo }
48549d3de7eeSOjaswin Mujoo }
48559d3de7eeSOjaswin Mujoo
48569d3de7eeSOjaswin Mujoo BUG_ON(!(tmp_pa && tmp_pa->pa_lstart <= ac->ac_o_ex.fe_logical));
48579d3de7eeSOjaswin Mujoo
48589d3de7eeSOjaswin Mujoo /*
48599d3de7eeSOjaswin Mujoo * Step 3: If the left adjacent pa is deleted, keep moving left to find
48609d3de7eeSOjaswin Mujoo * the first non deleted adjacent pa. After this step we should have a
48619d3de7eeSOjaswin Mujoo * valid tmp_pa which is guaranteed to be non deleted.
48629d3de7eeSOjaswin Mujoo */
48639d3de7eeSOjaswin Mujoo for (iter = &tmp_pa->pa_node.inode_node;; iter = rb_prev(iter)) {
48649d3de7eeSOjaswin Mujoo if (!iter) {
48659d3de7eeSOjaswin Mujoo /*
48669d3de7eeSOjaswin Mujoo * no non deleted left adjacent pa, so stop searching
48679d3de7eeSOjaswin Mujoo * inode pa tree
48689d3de7eeSOjaswin Mujoo */
48699d3de7eeSOjaswin Mujoo goto try_group_pa;
48709d3de7eeSOjaswin Mujoo }
48719d3de7eeSOjaswin Mujoo tmp_pa = rb_entry(iter, struct ext4_prealloc_space,
48729d3de7eeSOjaswin Mujoo pa_node.inode_node);
48739d3de7eeSOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
48749d3de7eeSOjaswin Mujoo if (tmp_pa->pa_deleted == 0) {
48759d3de7eeSOjaswin Mujoo /*
48769d3de7eeSOjaswin Mujoo * We will keep holding the pa_lock from
48779d3de7eeSOjaswin Mujoo * this point on because we don't want group discard
48789d3de7eeSOjaswin Mujoo * to delete this pa underneath us. Since group
48799d3de7eeSOjaswin Mujoo * discard is anyways an ENOSPC operation it
48809d3de7eeSOjaswin Mujoo * should be okay for it to wait a few more cycles.
48819d3de7eeSOjaswin Mujoo */
48829d3de7eeSOjaswin Mujoo break;
48839d3de7eeSOjaswin Mujoo } else {
48849d3de7eeSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
48859d3de7eeSOjaswin Mujoo }
48869d3de7eeSOjaswin Mujoo }
48879d3de7eeSOjaswin Mujoo
48889d3de7eeSOjaswin Mujoo BUG_ON(!(tmp_pa && tmp_pa->pa_lstart <= ac->ac_o_ex.fe_logical));
48899d3de7eeSOjaswin Mujoo BUG_ON(tmp_pa->pa_deleted == 1);
48909d3de7eeSOjaswin Mujoo
48919d3de7eeSOjaswin Mujoo /*
48929d3de7eeSOjaswin Mujoo * Step 4: We now have the non deleted left adjacent pa. Only this
48939d3de7eeSOjaswin Mujoo * pa can possibly satisfy the request hence check if it overlaps
48949d3de7eeSOjaswin Mujoo * original logical start and stop searching if it doesn't.
48959d3de7eeSOjaswin Mujoo */
489643bbddc0SBaokun Li if (ac->ac_o_ex.fe_logical >= pa_logical_end(sbi, tmp_pa)) {
48979d3de7eeSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
48989d3de7eeSOjaswin Mujoo goto try_group_pa;
48999d3de7eeSOjaswin Mujoo }
4900c9de560dSAlex Tomas
4901fb0a387dSEric Sandeen /* non-extent files can't have physical blocks past 2^32 */
490212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(ac->ac_inode, EXT4_INODE_EXTENTS)) &&
4903bcf43499SOjaswin Mujoo (tmp_pa->pa_pstart + EXT4_C2B(sbi, tmp_pa->pa_len) >
4904e86a7182SOjaswin Mujoo EXT4_MAX_BLOCK_FILE_PHYS)) {
4905e86a7182SOjaswin Mujoo /*
49069d3de7eeSOjaswin Mujoo * Since PAs don't overlap, we won't find any other PA to
49079d3de7eeSOjaswin Mujoo * satisfy this.
4908e86a7182SOjaswin Mujoo */
49099d3de7eeSOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
49109d3de7eeSOjaswin Mujoo goto try_group_pa;
4911e86a7182SOjaswin Mujoo }
4912fb0a387dSEric Sandeen
49139d3de7eeSOjaswin Mujoo if (tmp_pa->pa_free && likely(ext4_mb_pa_goal_check(ac, tmp_pa))) {
4914bcf43499SOjaswin Mujoo atomic_inc(&tmp_pa->pa_count);
4915bcf43499SOjaswin Mujoo ext4_mb_use_inode_pa(ac, tmp_pa);
4916bcf43499SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
491738727786SOjaswin Mujoo read_unlock(&ei->i_prealloc_lock);
49184fca8f07SRitesh Harjani return true;
49199d3de7eeSOjaswin Mujoo } else {
49209d3de7eeSOjaswin Mujoo /*
49219d3de7eeSOjaswin Mujoo * We found a valid overlapping pa but couldn't use it because
49229d3de7eeSOjaswin Mujoo * it had no free blocks. This should ideally never happen
49239d3de7eeSOjaswin Mujoo * because:
49249d3de7eeSOjaswin Mujoo *
49259d3de7eeSOjaswin Mujoo * 1. When a new inode pa is added to rbtree it must have
49269d3de7eeSOjaswin Mujoo * pa_free > 0 since otherwise we won't actually need
49279d3de7eeSOjaswin Mujoo * preallocation.
49289d3de7eeSOjaswin Mujoo *
49299d3de7eeSOjaswin Mujoo * 2. An inode pa that is in the rbtree can only have it's
49309d3de7eeSOjaswin Mujoo * pa_free become zero when another thread calls:
49319d3de7eeSOjaswin Mujoo * ext4_mb_new_blocks
49329d3de7eeSOjaswin Mujoo * ext4_mb_use_preallocated
49339d3de7eeSOjaswin Mujoo * ext4_mb_use_inode_pa
49349d3de7eeSOjaswin Mujoo *
49359d3de7eeSOjaswin Mujoo * 3. Further, after the above calls make pa_free == 0, we will
49369d3de7eeSOjaswin Mujoo * immediately remove it from the rbtree in:
49379d3de7eeSOjaswin Mujoo * ext4_mb_new_blocks
49389d3de7eeSOjaswin Mujoo * ext4_mb_release_context
49399d3de7eeSOjaswin Mujoo * ext4_mb_put_pa
49409d3de7eeSOjaswin Mujoo *
49419d3de7eeSOjaswin Mujoo * 4. Since the pa_free becoming 0 and pa_free getting removed
49429d3de7eeSOjaswin Mujoo * from tree both happen in ext4_mb_new_blocks, which is always
49439d3de7eeSOjaswin Mujoo * called with i_data_sem held for data allocations, we can be
49449d3de7eeSOjaswin Mujoo * sure that another process will never see a pa in rbtree with
49459d3de7eeSOjaswin Mujoo * pa_free == 0.
49469d3de7eeSOjaswin Mujoo */
49479d3de7eeSOjaswin Mujoo WARN_ON_ONCE(tmp_pa->pa_free == 0);
4948c9de560dSAlex Tomas }
4949bcf43499SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
49509d3de7eeSOjaswin Mujoo try_group_pa:
495138727786SOjaswin Mujoo read_unlock(&ei->i_prealloc_lock);
4952c9de560dSAlex Tomas
4953c9de560dSAlex Tomas /* can we use group allocation? */
4954c9de560dSAlex Tomas if (!(ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC))
49554fca8f07SRitesh Harjani return false;
4956c9de560dSAlex Tomas
4957c9de560dSAlex Tomas /* inode may have no locality group for some reason */
4958c9de560dSAlex Tomas lg = ac->ac_lg;
4959c9de560dSAlex Tomas if (lg == NULL)
49604fca8f07SRitesh Harjani return false;
49616be2ded1SAneesh Kumar K.V order = fls(ac->ac_o_ex.fe_len) - 1;
49626be2ded1SAneesh Kumar K.V if (order > PREALLOC_TB_SIZE - 1)
49636be2ded1SAneesh Kumar K.V /* The max size of hash table is PREALLOC_TB_SIZE */
49646be2ded1SAneesh Kumar K.V order = PREALLOC_TB_SIZE - 1;
4965c9de560dSAlex Tomas
4966bda00de7SAkinobu Mita goal_block = ext4_grp_offs_to_block(ac->ac_sb, &ac->ac_g_ex);
49675e745b04SAneesh Kumar K.V /*
49685e745b04SAneesh Kumar K.V * search for the prealloc space that is having
49695e745b04SAneesh Kumar K.V * minimal distance from the goal block.
49705e745b04SAneesh Kumar K.V */
49716be2ded1SAneesh Kumar K.V for (i = order; i < PREALLOC_TB_SIZE; i++) {
4972c9de560dSAlex Tomas rcu_read_lock();
4973bcf43499SOjaswin Mujoo list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[i],
4974a8e38fd3SOjaswin Mujoo pa_node.lg_list) {
4975bcf43499SOjaswin Mujoo spin_lock(&tmp_pa->pa_lock);
4976bcf43499SOjaswin Mujoo if (tmp_pa->pa_deleted == 0 &&
4977bcf43499SOjaswin Mujoo tmp_pa->pa_free >= ac->ac_o_ex.fe_len) {
49785e745b04SAneesh Kumar K.V
49795e745b04SAneesh Kumar K.V cpa = ext4_mb_check_group_pa(goal_block,
4980bcf43499SOjaswin Mujoo tmp_pa, cpa);
49815e745b04SAneesh Kumar K.V }
4982bcf43499SOjaswin Mujoo spin_unlock(&tmp_pa->pa_lock);
49835e745b04SAneesh Kumar K.V }
49845e745b04SAneesh Kumar K.V rcu_read_unlock();
49855e745b04SAneesh Kumar K.V }
49865e745b04SAneesh Kumar K.V if (cpa) {
49875e745b04SAneesh Kumar K.V ext4_mb_use_group_pa(ac, cpa);
49884fca8f07SRitesh Harjani return true;
4989c9de560dSAlex Tomas }
49904fca8f07SRitesh Harjani return false;
4991c9de560dSAlex Tomas }
4992c9de560dSAlex Tomas
4993c9de560dSAlex Tomas /*
4994c9de560dSAlex Tomas * the function goes through all preallocation in this group and marks them
4995c9de560dSAlex Tomas * used in in-core bitmap. buddy must be generated from this bitmap
4996955ce5f5SAneesh Kumar K.V * Need to be called with ext4 group lock held
4997c9de560dSAlex Tomas */
4998089ceeccSEric Sandeen static noinline_for_stack
ext4_mb_generate_from_pa(struct super_block * sb,void * bitmap,ext4_group_t group)4999089ceeccSEric Sandeen void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
5000c9de560dSAlex Tomas ext4_group_t group)
5001c9de560dSAlex Tomas {
5002c9de560dSAlex Tomas struct ext4_group_info *grp = ext4_get_group_info(sb, group);
5003c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
5004c9de560dSAlex Tomas struct list_head *cur;
5005c9de560dSAlex Tomas ext4_group_t groupnr;
5006c9de560dSAlex Tomas ext4_grpblk_t start;
5007c9de560dSAlex Tomas int preallocated = 0;
5008c9de560dSAlex Tomas int len;
5009c9de560dSAlex Tomas
50105354b2afSTheodore Ts'o if (!grp)
50115354b2afSTheodore Ts'o return;
50125354b2afSTheodore Ts'o
5013c9de560dSAlex Tomas /* all form of preallocation discards first load group,
5014c9de560dSAlex Tomas * so the only competing code is preallocation use.
5015c9de560dSAlex Tomas * we don't need any locking here
5016c9de560dSAlex Tomas * notice we do NOT ignore preallocations with pa_deleted
5017c9de560dSAlex Tomas * otherwise we could leave used blocks available for
5018c9de560dSAlex Tomas * allocation in buddy when concurrent ext4_mb_put_pa()
5019c9de560dSAlex Tomas * is dropping preallocation
5020c9de560dSAlex Tomas */
5021c9de560dSAlex Tomas list_for_each(cur, &grp->bb_prealloc_list) {
5022c9de560dSAlex Tomas pa = list_entry(cur, struct ext4_prealloc_space, pa_group_list);
5023c9de560dSAlex Tomas spin_lock(&pa->pa_lock);
5024c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, pa->pa_pstart,
5025c9de560dSAlex Tomas &groupnr, &start);
5026c9de560dSAlex Tomas len = pa->pa_len;
5027c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5028c9de560dSAlex Tomas if (unlikely(len == 0))
5029c9de560dSAlex Tomas continue;
5030c9de560dSAlex Tomas BUG_ON(groupnr != group);
5031123e3016SRitesh Harjani mb_set_bits(bitmap, start, len);
5032c9de560dSAlex Tomas preallocated += len;
5033c9de560dSAlex Tomas }
5034d3df1453SRitesh Harjani mb_debug(sb, "preallocated %d for group %u\n", preallocated, group);
5035c9de560dSAlex Tomas }
5036c9de560dSAlex Tomas
ext4_mb_mark_pa_deleted(struct super_block * sb,struct ext4_prealloc_space * pa)503727bc446eSbrookxu static void ext4_mb_mark_pa_deleted(struct super_block *sb,
503827bc446eSbrookxu struct ext4_prealloc_space *pa)
503927bc446eSbrookxu {
504027bc446eSbrookxu struct ext4_inode_info *ei;
504127bc446eSbrookxu
504227bc446eSbrookxu if (pa->pa_deleted) {
504327bc446eSbrookxu ext4_warning(sb, "deleted pa, type:%d, pblk:%llu, lblk:%u, len:%d\n",
504427bc446eSbrookxu pa->pa_type, pa->pa_pstart, pa->pa_lstart,
504527bc446eSbrookxu pa->pa_len);
504627bc446eSbrookxu return;
504727bc446eSbrookxu }
504827bc446eSbrookxu
504927bc446eSbrookxu pa->pa_deleted = 1;
505027bc446eSbrookxu
505127bc446eSbrookxu if (pa->pa_type == MB_INODE_PA) {
505227bc446eSbrookxu ei = EXT4_I(pa->pa_inode);
505327bc446eSbrookxu atomic_dec(&ei->i_prealloc_active);
505427bc446eSbrookxu }
505527bc446eSbrookxu }
505627bc446eSbrookxu
ext4_mb_pa_free(struct ext4_prealloc_space * pa)505782089725SOjaswin Mujoo static inline void ext4_mb_pa_free(struct ext4_prealloc_space *pa)
5058c9de560dSAlex Tomas {
505982089725SOjaswin Mujoo BUG_ON(!pa);
50604e8d2139SJunho Ryu BUG_ON(atomic_read(&pa->pa_count));
50614e8d2139SJunho Ryu BUG_ON(pa->pa_deleted == 0);
5062c9de560dSAlex Tomas kmem_cache_free(ext4_pspace_cachep, pa);
5063c9de560dSAlex Tomas }
5064c9de560dSAlex Tomas
ext4_mb_pa_callback(struct rcu_head * head)506582089725SOjaswin Mujoo static void ext4_mb_pa_callback(struct rcu_head *head)
506682089725SOjaswin Mujoo {
506782089725SOjaswin Mujoo struct ext4_prealloc_space *pa;
506882089725SOjaswin Mujoo
506982089725SOjaswin Mujoo pa = container_of(head, struct ext4_prealloc_space, u.pa_rcu);
507082089725SOjaswin Mujoo ext4_mb_pa_free(pa);
507182089725SOjaswin Mujoo }
507282089725SOjaswin Mujoo
5073c9de560dSAlex Tomas /*
5074c9de560dSAlex Tomas * drops a reference to preallocated space descriptor
5075c9de560dSAlex Tomas * if this was the last reference and the space is consumed
5076c9de560dSAlex Tomas */
ext4_mb_put_pa(struct ext4_allocation_context * ac,struct super_block * sb,struct ext4_prealloc_space * pa)5077c9de560dSAlex Tomas static void ext4_mb_put_pa(struct ext4_allocation_context *ac,
5078c9de560dSAlex Tomas struct super_block *sb, struct ext4_prealloc_space *pa)
5079c9de560dSAlex Tomas {
5080a9df9a49STheodore Ts'o ext4_group_t grp;
5081d33a1976SEric Sandeen ext4_fsblk_t grp_blk;
508238727786SOjaswin Mujoo struct ext4_inode_info *ei = EXT4_I(ac->ac_inode);
5083c9de560dSAlex Tomas
5084c9de560dSAlex Tomas /* in this short window concurrent discard can set pa_deleted */
5085c9de560dSAlex Tomas spin_lock(&pa->pa_lock);
50864e8d2139SJunho Ryu if (!atomic_dec_and_test(&pa->pa_count) || pa->pa_free != 0) {
50874e8d2139SJunho Ryu spin_unlock(&pa->pa_lock);
50884e8d2139SJunho Ryu return;
50894e8d2139SJunho Ryu }
50904e8d2139SJunho Ryu
5091c9de560dSAlex Tomas if (pa->pa_deleted == 1) {
5092c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5093c9de560dSAlex Tomas return;
5094c9de560dSAlex Tomas }
5095c9de560dSAlex Tomas
509627bc446eSbrookxu ext4_mb_mark_pa_deleted(sb, pa);
5097c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5098c9de560dSAlex Tomas
5099d33a1976SEric Sandeen grp_blk = pa->pa_pstart;
5100cc0fb9adSAneesh Kumar K.V /*
5101cc0fb9adSAneesh Kumar K.V * If doing group-based preallocation, pa_pstart may be in the
5102cc0fb9adSAneesh Kumar K.V * next group when pa is used up
5103cc0fb9adSAneesh Kumar K.V */
5104cc0fb9adSAneesh Kumar K.V if (pa->pa_type == MB_GROUP_PA)
5105d33a1976SEric Sandeen grp_blk--;
5106d33a1976SEric Sandeen
5107bd86298eSLukas Czerner grp = ext4_get_group_number(sb, grp_blk);
5108c9de560dSAlex Tomas
5109c9de560dSAlex Tomas /*
5110c9de560dSAlex Tomas * possible race:
5111c9de560dSAlex Tomas *
5112c9de560dSAlex Tomas * P1 (buddy init) P2 (regular allocation)
5113c9de560dSAlex Tomas * find block B in PA
5114c9de560dSAlex Tomas * copy on-disk bitmap to buddy
5115c9de560dSAlex Tomas * mark B in on-disk bitmap
5116c9de560dSAlex Tomas * drop PA from group
5117c9de560dSAlex Tomas * mark all PAs in buddy
5118c9de560dSAlex Tomas *
5119c9de560dSAlex Tomas * thus, P1 initializes buddy with B available. to prevent this
5120c9de560dSAlex Tomas * we make "copy" and "mark all PAs" atomic and serialize "drop PA"
5121c9de560dSAlex Tomas * against that pair
5122c9de560dSAlex Tomas */
5123c9de560dSAlex Tomas ext4_lock_group(sb, grp);
5124c9de560dSAlex Tomas list_del(&pa->pa_group_list);
5125c9de560dSAlex Tomas ext4_unlock_group(sb, grp);
5126c9de560dSAlex Tomas
5127a8e38fd3SOjaswin Mujoo if (pa->pa_type == MB_INODE_PA) {
512838727786SOjaswin Mujoo write_lock(pa->pa_node_lock.inode_lock);
512938727786SOjaswin Mujoo rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node);
513038727786SOjaswin Mujoo write_unlock(pa->pa_node_lock.inode_lock);
513138727786SOjaswin Mujoo ext4_mb_pa_free(pa);
5132a8e38fd3SOjaswin Mujoo } else {
5133a8e38fd3SOjaswin Mujoo spin_lock(pa->pa_node_lock.lg_lock);
5134a8e38fd3SOjaswin Mujoo list_del_rcu(&pa->pa_node.lg_list);
5135a8e38fd3SOjaswin Mujoo spin_unlock(pa->pa_node_lock.lg_lock);
513638727786SOjaswin Mujoo call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
513738727786SOjaswin Mujoo }
5138a8e38fd3SOjaswin Mujoo }
5139c9de560dSAlex Tomas
ext4_mb_pa_rb_insert(struct rb_root * root,struct rb_node * new)514038727786SOjaswin Mujoo static void ext4_mb_pa_rb_insert(struct rb_root *root, struct rb_node *new)
514138727786SOjaswin Mujoo {
514238727786SOjaswin Mujoo struct rb_node **iter = &root->rb_node, *parent = NULL;
514338727786SOjaswin Mujoo struct ext4_prealloc_space *iter_pa, *new_pa;
514438727786SOjaswin Mujoo ext4_lblk_t iter_start, new_start;
514538727786SOjaswin Mujoo
514638727786SOjaswin Mujoo while (*iter) {
514738727786SOjaswin Mujoo iter_pa = rb_entry(*iter, struct ext4_prealloc_space,
514838727786SOjaswin Mujoo pa_node.inode_node);
514938727786SOjaswin Mujoo new_pa = rb_entry(new, struct ext4_prealloc_space,
515038727786SOjaswin Mujoo pa_node.inode_node);
515138727786SOjaswin Mujoo iter_start = iter_pa->pa_lstart;
515238727786SOjaswin Mujoo new_start = new_pa->pa_lstart;
515338727786SOjaswin Mujoo
515438727786SOjaswin Mujoo parent = *iter;
515538727786SOjaswin Mujoo if (new_start < iter_start)
515638727786SOjaswin Mujoo iter = &((*iter)->rb_left);
515738727786SOjaswin Mujoo else
515838727786SOjaswin Mujoo iter = &((*iter)->rb_right);
515938727786SOjaswin Mujoo }
516038727786SOjaswin Mujoo
516138727786SOjaswin Mujoo rb_link_node(new, parent, iter);
516238727786SOjaswin Mujoo rb_insert_color(new, root);
5163c9de560dSAlex Tomas }
5164c9de560dSAlex Tomas
5165c9de560dSAlex Tomas /*
5166c9de560dSAlex Tomas * creates new preallocated space for given inode
5167c9de560dSAlex Tomas */
516853f86b17SRitesh Harjani static noinline_for_stack void
ext4_mb_new_inode_pa(struct ext4_allocation_context * ac)51694ddfef7bSEric Sandeen ext4_mb_new_inode_pa(struct ext4_allocation_context *ac)
5170c9de560dSAlex Tomas {
5171c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
517253accfa9STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb);
5173c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
5174c9de560dSAlex Tomas struct ext4_group_info *grp;
5175c9de560dSAlex Tomas struct ext4_inode_info *ei;
5176c9de560dSAlex Tomas
5177c9de560dSAlex Tomas /* preallocate only when found space is larger then requested */
5178c9de560dSAlex Tomas BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
5179c9de560dSAlex Tomas BUG_ON(ac->ac_status != AC_STATUS_FOUND);
5180c9de560dSAlex Tomas BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
518153f86b17SRitesh Harjani BUG_ON(ac->ac_pa == NULL);
5182c9de560dSAlex Tomas
518353f86b17SRitesh Harjani pa = ac->ac_pa;
5184c9de560dSAlex Tomas
51857e170922SOjaswin Mujoo if (ac->ac_b_ex.fe_len < ac->ac_orig_goal_len) {
5186bc056e71SBaokun Li struct ext4_free_extent ex = {
5187bc056e71SBaokun Li .fe_logical = ac->ac_g_ex.fe_logical,
5188bc056e71SBaokun Li .fe_len = ac->ac_orig_goal_len,
5189bc056e71SBaokun Li };
5190bc056e71SBaokun Li loff_t orig_goal_end = extent_logical_end(sbi, &ex);
5191ba191200SBaokun Li loff_t o_ex_end = extent_logical_end(sbi, &ac->ac_o_ex);
5192c9de560dSAlex Tomas
5193ba191200SBaokun Li /*
5194ba191200SBaokun Li * We can't allocate as much as normalizer wants, so we try
5195ba191200SBaokun Li * to get proper lstart to cover the original request, except
5196ba191200SBaokun Li * when the goal doesn't cover the original request as below:
5197ba191200SBaokun Li *
5198ba191200SBaokun Li * orig_ex:2045/2055(10), isize:8417280 -> normalized:0/2048
5199ba191200SBaokun Li * best_ex:0/200(200) -> adjusted: 1848/2048(200)
5200ba191200SBaokun Li */
5201c9de560dSAlex Tomas BUG_ON(ac->ac_g_ex.fe_logical > ac->ac_o_ex.fe_logical);
5202c9de560dSAlex Tomas BUG_ON(ac->ac_g_ex.fe_len < ac->ac_o_ex.fe_len);
5203c9de560dSAlex Tomas
520493cdf49fSOjaswin Mujoo /*
520593cdf49fSOjaswin Mujoo * Use the below logic for adjusting best extent as it keeps
520693cdf49fSOjaswin Mujoo * fragmentation in check while ensuring logical range of best
520793cdf49fSOjaswin Mujoo * extent doesn't overflow out of goal extent:
520893cdf49fSOjaswin Mujoo *
52097e170922SOjaswin Mujoo * 1. Check if best ex can be kept at end of goal (before
52107e170922SOjaswin Mujoo * cr_best_avail trimmed it) and still cover original start
521193cdf49fSOjaswin Mujoo * 2. Else, check if best ex can be kept at start of goal and
5212ba191200SBaokun Li * still cover original end
521393cdf49fSOjaswin Mujoo * 3. Else, keep the best ex at start of original request.
521493cdf49fSOjaswin Mujoo */
5215bc056e71SBaokun Li ex.fe_len = ac->ac_b_ex.fe_len;
5216bc056e71SBaokun Li
5217bc056e71SBaokun Li ex.fe_logical = orig_goal_end - EXT4_C2B(sbi, ex.fe_len);
5218bc056e71SBaokun Li if (ac->ac_o_ex.fe_logical >= ex.fe_logical)
521993cdf49fSOjaswin Mujoo goto adjust_bex;
5220c9de560dSAlex Tomas
5221bc056e71SBaokun Li ex.fe_logical = ac->ac_g_ex.fe_logical;
5222ba191200SBaokun Li if (o_ex_end <= extent_logical_end(sbi, &ex))
522393cdf49fSOjaswin Mujoo goto adjust_bex;
5224c9de560dSAlex Tomas
5225bc056e71SBaokun Li ex.fe_logical = ac->ac_o_ex.fe_logical;
522693cdf49fSOjaswin Mujoo adjust_bex:
5227bc056e71SBaokun Li ac->ac_b_ex.fe_logical = ex.fe_logical;
5228c9de560dSAlex Tomas
5229c9de560dSAlex Tomas BUG_ON(ac->ac_o_ex.fe_logical < ac->ac_b_ex.fe_logical);
5230bc056e71SBaokun Li BUG_ON(extent_logical_end(sbi, &ex) > orig_goal_end);
5231c9de560dSAlex Tomas }
5232c9de560dSAlex Tomas
5233c9de560dSAlex Tomas pa->pa_lstart = ac->ac_b_ex.fe_logical;
5234c9de560dSAlex Tomas pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
5235c9de560dSAlex Tomas pa->pa_len = ac->ac_b_ex.fe_len;
5236c9de560dSAlex Tomas pa->pa_free = pa->pa_len;
5237c9de560dSAlex Tomas spin_lock_init(&pa->pa_lock);
5238d794bf8eSAneesh Kumar K.V INIT_LIST_HEAD(&pa->pa_group_list);
5239c9de560dSAlex Tomas pa->pa_deleted = 0;
5240cc0fb9adSAneesh Kumar K.V pa->pa_type = MB_INODE_PA;
5241c9de560dSAlex Tomas
5242d3df1453SRitesh Harjani mb_debug(sb, "new inode pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
5243d3df1453SRitesh Harjani pa->pa_len, pa->pa_lstart);
52449bffad1eSTheodore Ts'o trace_ext4_mb_new_inode_pa(ac, pa);
5245c9de560dSAlex Tomas
524653accfa9STheodore Ts'o atomic_add(pa->pa_free, &sbi->s_mb_preallocated);
5247abc075d4SKemeng Shi ext4_mb_use_inode_pa(ac, pa);
5248c9de560dSAlex Tomas
5249c9de560dSAlex Tomas ei = EXT4_I(ac->ac_inode);
5250c9de560dSAlex Tomas grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
52515354b2afSTheodore Ts'o if (!grp)
52525354b2afSTheodore Ts'o return;
5253c9de560dSAlex Tomas
5254a8e38fd3SOjaswin Mujoo pa->pa_node_lock.inode_lock = &ei->i_prealloc_lock;
5255c9de560dSAlex Tomas pa->pa_inode = ac->ac_inode;
5256c9de560dSAlex Tomas
5257c9de560dSAlex Tomas list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
5258c9de560dSAlex Tomas
525938727786SOjaswin Mujoo write_lock(pa->pa_node_lock.inode_lock);
526038727786SOjaswin Mujoo ext4_mb_pa_rb_insert(&ei->i_prealloc_node, &pa->pa_node.inode_node);
526138727786SOjaswin Mujoo write_unlock(pa->pa_node_lock.inode_lock);
526227bc446eSbrookxu atomic_inc(&ei->i_prealloc_active);
5263c9de560dSAlex Tomas }
5264c9de560dSAlex Tomas
5265c9de560dSAlex Tomas /*
5266c9de560dSAlex Tomas * creates new preallocated space for locality group inodes belongs to
5267c9de560dSAlex Tomas */
526853f86b17SRitesh Harjani static noinline_for_stack void
ext4_mb_new_group_pa(struct ext4_allocation_context * ac)52694ddfef7bSEric Sandeen ext4_mb_new_group_pa(struct ext4_allocation_context *ac)
5270c9de560dSAlex Tomas {
5271c9de560dSAlex Tomas struct super_block *sb = ac->ac_sb;
5272c9de560dSAlex Tomas struct ext4_locality_group *lg;
5273c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
5274c9de560dSAlex Tomas struct ext4_group_info *grp;
5275c9de560dSAlex Tomas
5276c9de560dSAlex Tomas /* preallocate only when found space is larger then requested */
5277c9de560dSAlex Tomas BUG_ON(ac->ac_o_ex.fe_len >= ac->ac_b_ex.fe_len);
5278c9de560dSAlex Tomas BUG_ON(ac->ac_status != AC_STATUS_FOUND);
5279c9de560dSAlex Tomas BUG_ON(!S_ISREG(ac->ac_inode->i_mode));
528053f86b17SRitesh Harjani BUG_ON(ac->ac_pa == NULL);
5281c9de560dSAlex Tomas
528253f86b17SRitesh Harjani pa = ac->ac_pa;
5283c9de560dSAlex Tomas
5284c9de560dSAlex Tomas pa->pa_pstart = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
5285c9de560dSAlex Tomas pa->pa_lstart = pa->pa_pstart;
5286c9de560dSAlex Tomas pa->pa_len = ac->ac_b_ex.fe_len;
5287c9de560dSAlex Tomas pa->pa_free = pa->pa_len;
5288c9de560dSAlex Tomas spin_lock_init(&pa->pa_lock);
5289a8e38fd3SOjaswin Mujoo INIT_LIST_HEAD(&pa->pa_node.lg_list);
5290d794bf8eSAneesh Kumar K.V INIT_LIST_HEAD(&pa->pa_group_list);
5291c9de560dSAlex Tomas pa->pa_deleted = 0;
5292cc0fb9adSAneesh Kumar K.V pa->pa_type = MB_GROUP_PA;
5293c9de560dSAlex Tomas
5294d3df1453SRitesh Harjani mb_debug(sb, "new group pa %p: %llu/%d for %u\n", pa, pa->pa_pstart,
5295d3df1453SRitesh Harjani pa->pa_len, pa->pa_lstart);
52969bffad1eSTheodore Ts'o trace_ext4_mb_new_group_pa(ac, pa);
5297c9de560dSAlex Tomas
5298c9de560dSAlex Tomas ext4_mb_use_group_pa(ac, pa);
5299c9de560dSAlex Tomas atomic_add(pa->pa_free, &EXT4_SB(sb)->s_mb_preallocated);
5300c9de560dSAlex Tomas
5301c9de560dSAlex Tomas grp = ext4_get_group_info(sb, ac->ac_b_ex.fe_group);
53025354b2afSTheodore Ts'o if (!grp)
53035354b2afSTheodore Ts'o return;
5304c9de560dSAlex Tomas lg = ac->ac_lg;
5305c9de560dSAlex Tomas BUG_ON(lg == NULL);
5306c9de560dSAlex Tomas
5307a8e38fd3SOjaswin Mujoo pa->pa_node_lock.lg_lock = &lg->lg_prealloc_lock;
5308c9de560dSAlex Tomas pa->pa_inode = NULL;
5309c9de560dSAlex Tomas
5310c9de560dSAlex Tomas list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
5311c9de560dSAlex Tomas
53126be2ded1SAneesh Kumar K.V /*
53136be2ded1SAneesh Kumar K.V * We will later add the new pa to the right bucket
53146be2ded1SAneesh Kumar K.V * after updating the pa_free in ext4_mb_release_context
53156be2ded1SAneesh Kumar K.V */
5316c9de560dSAlex Tomas }
5317c9de560dSAlex Tomas
ext4_mb_new_preallocation(struct ext4_allocation_context * ac)531853f86b17SRitesh Harjani static void ext4_mb_new_preallocation(struct ext4_allocation_context *ac)
5319c9de560dSAlex Tomas {
5320c9de560dSAlex Tomas if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
532153f86b17SRitesh Harjani ext4_mb_new_group_pa(ac);
5322c9de560dSAlex Tomas else
532353f86b17SRitesh Harjani ext4_mb_new_inode_pa(ac);
5324c9de560dSAlex Tomas }
5325c9de560dSAlex Tomas
5326c9de560dSAlex Tomas /*
5327c9de560dSAlex Tomas * finds all unused blocks in on-disk bitmap, frees them in
5328c9de560dSAlex Tomas * in-core bitmap and buddy.
5329c9de560dSAlex Tomas * @pa must be unlinked from inode and group lists, so that
5330c9de560dSAlex Tomas * nobody else can find/use it.
5331c9de560dSAlex Tomas * the caller MUST hold group/inode locks.
5332c9de560dSAlex Tomas * TODO: optimize the case when there are no in-core structures yet
5333c9de560dSAlex Tomas */
53344ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_release_inode_pa(struct ext4_buddy * e4b,struct buffer_head * bitmap_bh,struct ext4_prealloc_space * pa)53354ddfef7bSEric Sandeen ext4_mb_release_inode_pa(struct ext4_buddy *e4b, struct buffer_head *bitmap_bh,
53363e1e5f50SEric Sandeen struct ext4_prealloc_space *pa)
5337c9de560dSAlex Tomas {
5338c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
5339c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
5340498e5f24STheodore Ts'o unsigned int end;
5341498e5f24STheodore Ts'o unsigned int next;
5342c9de560dSAlex Tomas ext4_group_t group;
5343c9de560dSAlex Tomas ext4_grpblk_t bit;
5344ba80b101STheodore Ts'o unsigned long long grp_blk_start;
5345c9de560dSAlex Tomas int free = 0;
5346c9de560dSAlex Tomas
5347c9de560dSAlex Tomas BUG_ON(pa->pa_deleted == 0);
5348c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
534953accfa9STheodore Ts'o grp_blk_start = pa->pa_pstart - EXT4_C2B(sbi, bit);
5350c9de560dSAlex Tomas BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
5351c9de560dSAlex Tomas end = bit + pa->pa_len;
5352c9de560dSAlex Tomas
5353c9de560dSAlex Tomas while (bit < end) {
5354ffad0a44SAneesh Kumar K.V bit = mb_find_next_zero_bit(bitmap_bh->b_data, end, bit);
5355c9de560dSAlex Tomas if (bit >= end)
5356c9de560dSAlex Tomas break;
5357ffad0a44SAneesh Kumar K.V next = mb_find_next_bit(bitmap_bh->b_data, end, bit);
5358d3df1453SRitesh Harjani mb_debug(sb, "free preallocated %u/%u in group %u\n",
53595a0790c2SAndi Kleen (unsigned) ext4_group_first_block_no(sb, group) + bit,
53605a0790c2SAndi Kleen (unsigned) next - bit, (unsigned) group);
5361c9de560dSAlex Tomas free += next - bit;
5362c9de560dSAlex Tomas
53633e1e5f50SEric Sandeen trace_ext4_mballoc_discard(sb, NULL, group, bit, next - bit);
536453accfa9STheodore Ts'o trace_ext4_mb_release_inode_pa(pa, (grp_blk_start +
536553accfa9STheodore Ts'o EXT4_C2B(sbi, bit)),
5366a9c667f8SLukas Czerner next - bit);
5367c9de560dSAlex Tomas mb_free_blocks(pa->pa_inode, e4b, bit, next - bit);
5368c9de560dSAlex Tomas bit = next + 1;
5369c9de560dSAlex Tomas }
5370c9de560dSAlex Tomas if (free != pa->pa_free) {
53719d8b9ec4STheodore Ts'o ext4_msg(e4b->bd_sb, KERN_CRIT,
537236bad423SRitesh Harjani "pa %p: logic %lu, phys. %lu, len %d",
5373c9de560dSAlex Tomas pa, (unsigned long) pa->pa_lstart,
5374c9de560dSAlex Tomas (unsigned long) pa->pa_pstart,
537536bad423SRitesh Harjani pa->pa_len);
5376e29136f8STheodore Ts'o ext4_grp_locked_error(sb, group, 0, 0, "free %u, pa_free %u",
537726346ff6SAneesh Kumar K.V free, pa->pa_free);
5378e56eb659SAneesh Kumar K.V /*
5379e56eb659SAneesh Kumar K.V * pa is already deleted so we use the value obtained
5380e56eb659SAneesh Kumar K.V * from the bitmap and continue.
5381e56eb659SAneesh Kumar K.V */
5382c9de560dSAlex Tomas }
5383c9de560dSAlex Tomas atomic_add(free, &sbi->s_mb_discarded);
5384c9de560dSAlex Tomas
5385863c37fcSzhong jiang return 0;
5386c9de560dSAlex Tomas }
5387c9de560dSAlex Tomas
53884ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_release_group_pa(struct ext4_buddy * e4b,struct ext4_prealloc_space * pa)53894ddfef7bSEric Sandeen ext4_mb_release_group_pa(struct ext4_buddy *e4b,
53903e1e5f50SEric Sandeen struct ext4_prealloc_space *pa)
5391c9de560dSAlex Tomas {
5392c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
5393c9de560dSAlex Tomas ext4_group_t group;
5394c9de560dSAlex Tomas ext4_grpblk_t bit;
5395c9de560dSAlex Tomas
539660e07cf5SYongqiang Yang trace_ext4_mb_release_group_pa(sb, pa);
5397c9de560dSAlex Tomas BUG_ON(pa->pa_deleted == 0);
5398c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
5399463808f2STheodore Ts'o if (unlikely(group != e4b->bd_group && pa->pa_len != 0)) {
5400463808f2STheodore Ts'o ext4_warning(sb, "bad group: expected %u, group %u, pa_start %llu",
5401463808f2STheodore Ts'o e4b->bd_group, group, pa->pa_pstart);
5402463808f2STheodore Ts'o return 0;
5403463808f2STheodore Ts'o }
5404c9de560dSAlex Tomas mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
5405c9de560dSAlex Tomas atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
54063e1e5f50SEric Sandeen trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
5407c9de560dSAlex Tomas
5408c9de560dSAlex Tomas return 0;
5409c9de560dSAlex Tomas }
5410c9de560dSAlex Tomas
5411c9de560dSAlex Tomas /*
5412c9de560dSAlex Tomas * releases all preallocations in given group
5413c9de560dSAlex Tomas *
5414c9de560dSAlex Tomas * first, we need to decide discard policy:
5415c9de560dSAlex Tomas * - when do we discard
5416c9de560dSAlex Tomas * 1) ENOSPC
5417c9de560dSAlex Tomas * - how many do we discard
5418c9de560dSAlex Tomas * 1) how many requested
5419c9de560dSAlex Tomas */
54204ddfef7bSEric Sandeen static noinline_for_stack int
ext4_mb_discard_group_preallocations(struct super_block * sb,ext4_group_t group,int * busy)54214ddfef7bSEric Sandeen ext4_mb_discard_group_preallocations(struct super_block *sb,
54228c80fb31SChunguang Xu ext4_group_t group, int *busy)
5423c9de560dSAlex Tomas {
5424c9de560dSAlex Tomas struct ext4_group_info *grp = ext4_get_group_info(sb, group);
5425c9de560dSAlex Tomas struct buffer_head *bitmap_bh = NULL;
5426c9de560dSAlex Tomas struct ext4_prealloc_space *pa, *tmp;
54270f6bc579SRuan Jinjie LIST_HEAD(list);
5428c9de560dSAlex Tomas struct ext4_buddy e4b;
542938727786SOjaswin Mujoo struct ext4_inode_info *ei;
5430c9de560dSAlex Tomas int err;
54318c80fb31SChunguang Xu int free = 0;
5432c9de560dSAlex Tomas
54335354b2afSTheodore Ts'o if (!grp)
54345354b2afSTheodore Ts'o return 0;
5435d3df1453SRitesh Harjani mb_debug(sb, "discard preallocation for group %u\n", group);
5436c9de560dSAlex Tomas if (list_empty(&grp->bb_prealloc_list))
5437bbc4ec77SRitesh Harjani goto out_dbg;
5438c9de560dSAlex Tomas
5439574ca174STheodore Ts'o bitmap_bh = ext4_read_block_bitmap(sb, group);
54409008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) {
54419008a58eSDarrick J. Wong err = PTR_ERR(bitmap_bh);
544254d3adbcSTheodore Ts'o ext4_error_err(sb, -err,
544354d3adbcSTheodore Ts'o "Error %d reading block bitmap for %u",
54449008a58eSDarrick J. Wong err, group);
5445bbc4ec77SRitesh Harjani goto out_dbg;
5446c9de560dSAlex Tomas }
5447c9de560dSAlex Tomas
5448c9de560dSAlex Tomas err = ext4_mb_load_buddy(sb, group, &e4b);
5449ce89f46cSAneesh Kumar K.V if (err) {
54509651e6b2SKonstantin Khlebnikov ext4_warning(sb, "Error %d loading buddy information for %u",
54519651e6b2SKonstantin Khlebnikov err, group);
5452ce89f46cSAneesh Kumar K.V put_bh(bitmap_bh);
5453bbc4ec77SRitesh Harjani goto out_dbg;
5454ce89f46cSAneesh Kumar K.V }
5455c9de560dSAlex Tomas
5456c9de560dSAlex Tomas ext4_lock_group(sb, group);
5457c9de560dSAlex Tomas list_for_each_entry_safe(pa, tmp,
5458c9de560dSAlex Tomas &grp->bb_prealloc_list, pa_group_list) {
5459c9de560dSAlex Tomas spin_lock(&pa->pa_lock);
5460c9de560dSAlex Tomas if (atomic_read(&pa->pa_count)) {
5461c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
54628c80fb31SChunguang Xu *busy = 1;
5463c9de560dSAlex Tomas continue;
5464c9de560dSAlex Tomas }
5465c9de560dSAlex Tomas if (pa->pa_deleted) {
5466c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5467c9de560dSAlex Tomas continue;
5468c9de560dSAlex Tomas }
5469c9de560dSAlex Tomas
5470c9de560dSAlex Tomas /* seems this one can be freed ... */
547127bc446eSbrookxu ext4_mb_mark_pa_deleted(sb, pa);
5472c9de560dSAlex Tomas
547370022da8SYe Bin if (!free)
547470022da8SYe Bin this_cpu_inc(discard_pa_seq);
547570022da8SYe Bin
5476c9de560dSAlex Tomas /* we can trust pa_free ... */
5477c9de560dSAlex Tomas free += pa->pa_free;
5478c9de560dSAlex Tomas
5479c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5480c9de560dSAlex Tomas
5481c9de560dSAlex Tomas list_del(&pa->pa_group_list);
5482c9de560dSAlex Tomas list_add(&pa->u.pa_tmp_list, &list);
5483c9de560dSAlex Tomas }
5484c9de560dSAlex Tomas
5485c9de560dSAlex Tomas /* now free all selected PAs */
5486c9de560dSAlex Tomas list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
5487c9de560dSAlex Tomas
5488c9de560dSAlex Tomas /* remove from object (inode or locality group) */
5489a8e38fd3SOjaswin Mujoo if (pa->pa_type == MB_GROUP_PA) {
5490a8e38fd3SOjaswin Mujoo spin_lock(pa->pa_node_lock.lg_lock);
5491a8e38fd3SOjaswin Mujoo list_del_rcu(&pa->pa_node.lg_list);
5492a8e38fd3SOjaswin Mujoo spin_unlock(pa->pa_node_lock.lg_lock);
5493a8e38fd3SOjaswin Mujoo } else {
549438727786SOjaswin Mujoo write_lock(pa->pa_node_lock.inode_lock);
549538727786SOjaswin Mujoo ei = EXT4_I(pa->pa_inode);
549638727786SOjaswin Mujoo rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node);
549738727786SOjaswin Mujoo write_unlock(pa->pa_node_lock.inode_lock);
5498a8e38fd3SOjaswin Mujoo }
5499c9de560dSAlex Tomas
5500c9de560dSAlex Tomas list_del(&pa->u.pa_tmp_list);
550138727786SOjaswin Mujoo
550238727786SOjaswin Mujoo if (pa->pa_type == MB_GROUP_PA) {
550338727786SOjaswin Mujoo ext4_mb_release_group_pa(&e4b, pa);
5504c9de560dSAlex Tomas call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
550538727786SOjaswin Mujoo } else {
550638727786SOjaswin Mujoo ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
550738727786SOjaswin Mujoo ext4_mb_pa_free(pa);
550838727786SOjaswin Mujoo }
5509c9de560dSAlex Tomas }
5510c9de560dSAlex Tomas
5511c9de560dSAlex Tomas ext4_unlock_group(sb, group);
5512e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
5513c9de560dSAlex Tomas put_bh(bitmap_bh);
5514bbc4ec77SRitesh Harjani out_dbg:
5515d3df1453SRitesh Harjani mb_debug(sb, "discarded (%d) blocks preallocated for group %u bb_free (%d)\n",
55168c80fb31SChunguang Xu free, group, grp->bb_free);
55178c80fb31SChunguang Xu return free;
5518c9de560dSAlex Tomas }
5519c9de560dSAlex Tomas
5520c9de560dSAlex Tomas /*
5521c9de560dSAlex Tomas * releases all non-used preallocated blocks for given inode
5522c9de560dSAlex Tomas *
5523c9de560dSAlex Tomas * It's important to discard preallocations under i_data_sem
5524c9de560dSAlex Tomas * We don't want another block to be served from the prealloc
5525c9de560dSAlex Tomas * space when we are discarding the inode prealloc space.
5526c9de560dSAlex Tomas *
5527c9de560dSAlex Tomas * FIXME!! Make sure it is valid at all the call sites
5528c9de560dSAlex Tomas */
ext4_discard_preallocations(struct inode * inode,unsigned int needed)552927bc446eSbrookxu void ext4_discard_preallocations(struct inode *inode, unsigned int needed)
5530c9de560dSAlex Tomas {
5531c9de560dSAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode);
5532c9de560dSAlex Tomas struct super_block *sb = inode->i_sb;
5533c9de560dSAlex Tomas struct buffer_head *bitmap_bh = NULL;
5534c9de560dSAlex Tomas struct ext4_prealloc_space *pa, *tmp;
5535c9de560dSAlex Tomas ext4_group_t group = 0;
55360f6bc579SRuan Jinjie LIST_HEAD(list);
5537c9de560dSAlex Tomas struct ext4_buddy e4b;
553838727786SOjaswin Mujoo struct rb_node *iter;
5539c9de560dSAlex Tomas int err;
5540c9de560dSAlex Tomas
5541c2ea3fdeSTheodore Ts'o if (!S_ISREG(inode->i_mode)) {
5542c9de560dSAlex Tomas return;
5543c9de560dSAlex Tomas }
5544c9de560dSAlex Tomas
55458016e29fSHarshad Shirwadkar if (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY)
55468016e29fSHarshad Shirwadkar return;
55478016e29fSHarshad Shirwadkar
5548d3df1453SRitesh Harjani mb_debug(sb, "discard preallocation for inode %lu\n",
5549d3df1453SRitesh Harjani inode->i_ino);
555027bc446eSbrookxu trace_ext4_discard_preallocations(inode,
555127bc446eSbrookxu atomic_read(&ei->i_prealloc_active), needed);
5552c9de560dSAlex Tomas
555327bc446eSbrookxu if (needed == 0)
555427bc446eSbrookxu needed = UINT_MAX;
555527bc446eSbrookxu
5556c9de560dSAlex Tomas repeat:
5557c9de560dSAlex Tomas /* first, collect all pa's in the inode */
555838727786SOjaswin Mujoo write_lock(&ei->i_prealloc_lock);
555938727786SOjaswin Mujoo for (iter = rb_first(&ei->i_prealloc_node); iter && needed;
556038727786SOjaswin Mujoo iter = rb_next(iter)) {
556138727786SOjaswin Mujoo pa = rb_entry(iter, struct ext4_prealloc_space,
556238727786SOjaswin Mujoo pa_node.inode_node);
5563a8e38fd3SOjaswin Mujoo BUG_ON(pa->pa_node_lock.inode_lock != &ei->i_prealloc_lock);
556438727786SOjaswin Mujoo
5565c9de560dSAlex Tomas spin_lock(&pa->pa_lock);
5566c9de560dSAlex Tomas if (atomic_read(&pa->pa_count)) {
5567c9de560dSAlex Tomas /* this shouldn't happen often - nobody should
5568c9de560dSAlex Tomas * use preallocation while we're discarding it */
5569c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
557038727786SOjaswin Mujoo write_unlock(&ei->i_prealloc_lock);
55719d8b9ec4STheodore Ts'o ext4_msg(sb, KERN_ERR,
55729d8b9ec4STheodore Ts'o "uh-oh! used pa while discarding");
5573c9de560dSAlex Tomas WARN_ON(1);
5574c9de560dSAlex Tomas schedule_timeout_uninterruptible(HZ);
5575c9de560dSAlex Tomas goto repeat;
5576c9de560dSAlex Tomas
5577c9de560dSAlex Tomas }
5578c9de560dSAlex Tomas if (pa->pa_deleted == 0) {
557927bc446eSbrookxu ext4_mb_mark_pa_deleted(sb, pa);
5580c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
558138727786SOjaswin Mujoo rb_erase(&pa->pa_node.inode_node, &ei->i_prealloc_node);
5582c9de560dSAlex Tomas list_add(&pa->u.pa_tmp_list, &list);
558327bc446eSbrookxu needed--;
5584c9de560dSAlex Tomas continue;
5585c9de560dSAlex Tomas }
5586c9de560dSAlex Tomas
5587c9de560dSAlex Tomas /* someone is deleting pa right now */
5588c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
558938727786SOjaswin Mujoo write_unlock(&ei->i_prealloc_lock);
5590c9de560dSAlex Tomas
5591c9de560dSAlex Tomas /* we have to wait here because pa_deleted
5592c9de560dSAlex Tomas * doesn't mean pa is already unlinked from
5593c9de560dSAlex Tomas * the list. as we might be called from
5594c9de560dSAlex Tomas * ->clear_inode() the inode will get freed
5595c9de560dSAlex Tomas * and concurrent thread which is unlinking
5596c9de560dSAlex Tomas * pa from inode's list may access already
5597c9de560dSAlex Tomas * freed memory, bad-bad-bad */
5598c9de560dSAlex Tomas
5599c9de560dSAlex Tomas /* XXX: if this happens too often, we can
5600c9de560dSAlex Tomas * add a flag to force wait only in case
5601c9de560dSAlex Tomas * of ->clear_inode(), but not in case of
5602c9de560dSAlex Tomas * regular truncate */
5603c9de560dSAlex Tomas schedule_timeout_uninterruptible(HZ);
5604c9de560dSAlex Tomas goto repeat;
5605c9de560dSAlex Tomas }
560638727786SOjaswin Mujoo write_unlock(&ei->i_prealloc_lock);
5607c9de560dSAlex Tomas
5608c9de560dSAlex Tomas list_for_each_entry_safe(pa, tmp, &list, u.pa_tmp_list) {
5609cc0fb9adSAneesh Kumar K.V BUG_ON(pa->pa_type != MB_INODE_PA);
5610bd86298eSLukas Czerner group = ext4_get_group_number(sb, pa->pa_pstart);
5611c9de560dSAlex Tomas
56129651e6b2SKonstantin Khlebnikov err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
56139651e6b2SKonstantin Khlebnikov GFP_NOFS|__GFP_NOFAIL);
5614ce89f46cSAneesh Kumar K.V if (err) {
561554d3adbcSTheodore Ts'o ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
56169651e6b2SKonstantin Khlebnikov err, group);
5617ce89f46cSAneesh Kumar K.V continue;
5618ce89f46cSAneesh Kumar K.V }
5619c9de560dSAlex Tomas
5620574ca174STheodore Ts'o bitmap_bh = ext4_read_block_bitmap(sb, group);
56219008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) {
56229008a58eSDarrick J. Wong err = PTR_ERR(bitmap_bh);
562354d3adbcSTheodore Ts'o ext4_error_err(sb, -err, "Error %d reading block bitmap for %u",
56249008a58eSDarrick J. Wong err, group);
5625e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
5626ce89f46cSAneesh Kumar K.V continue;
5627c9de560dSAlex Tomas }
5628c9de560dSAlex Tomas
5629c9de560dSAlex Tomas ext4_lock_group(sb, group);
5630c9de560dSAlex Tomas list_del(&pa->pa_group_list);
56313e1e5f50SEric Sandeen ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
5632c9de560dSAlex Tomas ext4_unlock_group(sb, group);
5633c9de560dSAlex Tomas
5634e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
5635c9de560dSAlex Tomas put_bh(bitmap_bh);
5636c9de560dSAlex Tomas
5637c9de560dSAlex Tomas list_del(&pa->u.pa_tmp_list);
563838727786SOjaswin Mujoo ext4_mb_pa_free(pa);
5639c9de560dSAlex Tomas }
5640c9de560dSAlex Tomas }
5641c9de560dSAlex Tomas
ext4_mb_pa_alloc(struct ext4_allocation_context * ac)564253f86b17SRitesh Harjani static int ext4_mb_pa_alloc(struct ext4_allocation_context *ac)
564353f86b17SRitesh Harjani {
564453f86b17SRitesh Harjani struct ext4_prealloc_space *pa;
564553f86b17SRitesh Harjani
564653f86b17SRitesh Harjani BUG_ON(ext4_pspace_cachep == NULL);
564753f86b17SRitesh Harjani pa = kmem_cache_zalloc(ext4_pspace_cachep, GFP_NOFS);
564853f86b17SRitesh Harjani if (!pa)
564953f86b17SRitesh Harjani return -ENOMEM;
565053f86b17SRitesh Harjani atomic_set(&pa->pa_count, 1);
565153f86b17SRitesh Harjani ac->ac_pa = pa;
565253f86b17SRitesh Harjani return 0;
565353f86b17SRitesh Harjani }
565453f86b17SRitesh Harjani
ext4_mb_pa_put_free(struct ext4_allocation_context * ac)565582089725SOjaswin Mujoo static void ext4_mb_pa_put_free(struct ext4_allocation_context *ac)
565653f86b17SRitesh Harjani {
565753f86b17SRitesh Harjani struct ext4_prealloc_space *pa = ac->ac_pa;
565853f86b17SRitesh Harjani
565953f86b17SRitesh Harjani BUG_ON(!pa);
566053f86b17SRitesh Harjani ac->ac_pa = NULL;
566153f86b17SRitesh Harjani WARN_ON(!atomic_dec_and_test(&pa->pa_count));
566282089725SOjaswin Mujoo /*
566382089725SOjaswin Mujoo * current function is only called due to an error or due to
566482089725SOjaswin Mujoo * len of found blocks < len of requested blocks hence the PA has not
566582089725SOjaswin Mujoo * been added to grp->bb_prealloc_list. So we don't need to lock it
566682089725SOjaswin Mujoo */
566782089725SOjaswin Mujoo pa->pa_deleted = 1;
566882089725SOjaswin Mujoo ext4_mb_pa_free(pa);
566953f86b17SRitesh Harjani }
567053f86b17SRitesh Harjani
56716ba495e9STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
ext4_mb_show_pa(struct super_block * sb)5672e68cf40cSRitesh Harjani static inline void ext4_mb_show_pa(struct super_block *sb)
5673c9de560dSAlex Tomas {
5674e68cf40cSRitesh Harjani ext4_group_t i, ngroups;
5675c9de560dSAlex Tomas
567695257987SJan Kara if (ext4_forced_shutdown(sb))
5677e3570639SEric Sandeen return;
5678e3570639SEric Sandeen
56798df9675fSTheodore Ts'o ngroups = ext4_get_groups_count(sb);
5680d3df1453SRitesh Harjani mb_debug(sb, "groups: ");
56818df9675fSTheodore Ts'o for (i = 0; i < ngroups; i++) {
5682c9de560dSAlex Tomas struct ext4_group_info *grp = ext4_get_group_info(sb, i);
5683c9de560dSAlex Tomas struct ext4_prealloc_space *pa;
5684c9de560dSAlex Tomas ext4_grpblk_t start;
5685c9de560dSAlex Tomas struct list_head *cur;
56865354b2afSTheodore Ts'o
56875354b2afSTheodore Ts'o if (!grp)
56885354b2afSTheodore Ts'o continue;
5689c9de560dSAlex Tomas ext4_lock_group(sb, i);
5690c9de560dSAlex Tomas list_for_each(cur, &grp->bb_prealloc_list) {
5691c9de560dSAlex Tomas pa = list_entry(cur, struct ext4_prealloc_space,
5692c9de560dSAlex Tomas pa_group_list);
5693c9de560dSAlex Tomas spin_lock(&pa->pa_lock);
5694c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, pa->pa_pstart,
5695c9de560dSAlex Tomas NULL, &start);
5696c9de560dSAlex Tomas spin_unlock(&pa->pa_lock);
5697d3df1453SRitesh Harjani mb_debug(sb, "PA:%u:%d:%d\n", i, start,
5698d3df1453SRitesh Harjani pa->pa_len);
5699c9de560dSAlex Tomas }
570060bd63d1SSolofo Ramangalahy ext4_unlock_group(sb, i);
5701d3df1453SRitesh Harjani mb_debug(sb, "%u: %d/%d\n", i, grp->bb_free,
5702d3df1453SRitesh Harjani grp->bb_fragments);
5703c9de560dSAlex Tomas }
5704c9de560dSAlex Tomas }
5705e68cf40cSRitesh Harjani
ext4_mb_show_ac(struct ext4_allocation_context * ac)5706e68cf40cSRitesh Harjani static void ext4_mb_show_ac(struct ext4_allocation_context *ac)
5707e68cf40cSRitesh Harjani {
5708e68cf40cSRitesh Harjani struct super_block *sb = ac->ac_sb;
5709e68cf40cSRitesh Harjani
571095257987SJan Kara if (ext4_forced_shutdown(sb))
5711e68cf40cSRitesh Harjani return;
5712e68cf40cSRitesh Harjani
5713d3df1453SRitesh Harjani mb_debug(sb, "Can't allocate:"
5714e68cf40cSRitesh Harjani " Allocation context details:");
5715d3df1453SRitesh Harjani mb_debug(sb, "status %u flags 0x%x",
5716e68cf40cSRitesh Harjani ac->ac_status, ac->ac_flags);
5717d3df1453SRitesh Harjani mb_debug(sb, "orig %lu/%lu/%lu@%lu, "
5718e68cf40cSRitesh Harjani "goal %lu/%lu/%lu@%lu, "
5719e68cf40cSRitesh Harjani "best %lu/%lu/%lu@%lu cr %d",
5720e68cf40cSRitesh Harjani (unsigned long)ac->ac_o_ex.fe_group,
5721e68cf40cSRitesh Harjani (unsigned long)ac->ac_o_ex.fe_start,
5722e68cf40cSRitesh Harjani (unsigned long)ac->ac_o_ex.fe_len,
5723e68cf40cSRitesh Harjani (unsigned long)ac->ac_o_ex.fe_logical,
5724e68cf40cSRitesh Harjani (unsigned long)ac->ac_g_ex.fe_group,
5725e68cf40cSRitesh Harjani (unsigned long)ac->ac_g_ex.fe_start,
5726e68cf40cSRitesh Harjani (unsigned long)ac->ac_g_ex.fe_len,
5727e68cf40cSRitesh Harjani (unsigned long)ac->ac_g_ex.fe_logical,
5728e68cf40cSRitesh Harjani (unsigned long)ac->ac_b_ex.fe_group,
5729e68cf40cSRitesh Harjani (unsigned long)ac->ac_b_ex.fe_start,
5730e68cf40cSRitesh Harjani (unsigned long)ac->ac_b_ex.fe_len,
5731e68cf40cSRitesh Harjani (unsigned long)ac->ac_b_ex.fe_logical,
5732e68cf40cSRitesh Harjani (int)ac->ac_criteria);
5733d3df1453SRitesh Harjani mb_debug(sb, "%u found", ac->ac_found);
5734569f196fSRitesh Harjani mb_debug(sb, "used pa: %s, ", ac->ac_pa ? "yes" : "no");
5735569f196fSRitesh Harjani if (ac->ac_pa)
5736569f196fSRitesh Harjani mb_debug(sb, "pa_type %s\n", ac->ac_pa->pa_type == MB_GROUP_PA ?
5737569f196fSRitesh Harjani "group pa" : "inode pa");
5738e68cf40cSRitesh Harjani ext4_mb_show_pa(sb);
5739e68cf40cSRitesh Harjani }
5740c9de560dSAlex Tomas #else
ext4_mb_show_pa(struct super_block * sb)5741e68cf40cSRitesh Harjani static inline void ext4_mb_show_pa(struct super_block *sb)
5742e68cf40cSRitesh Harjani {
5743e68cf40cSRitesh Harjani }
ext4_mb_show_ac(struct ext4_allocation_context * ac)5744c9de560dSAlex Tomas static inline void ext4_mb_show_ac(struct ext4_allocation_context *ac)
5745c9de560dSAlex Tomas {
5746e68cf40cSRitesh Harjani ext4_mb_show_pa(ac->ac_sb);
5747c9de560dSAlex Tomas }
5748c9de560dSAlex Tomas #endif
5749c9de560dSAlex Tomas
5750c9de560dSAlex Tomas /*
5751c9de560dSAlex Tomas * We use locality group preallocation for small size file. The size of the
5752c9de560dSAlex Tomas * file is determined by the current size or the resulting size after
5753c9de560dSAlex Tomas * allocation which ever is larger
5754c9de560dSAlex Tomas *
5755b713a5ecSTheodore Ts'o * One can tune this size via /sys/fs/ext4/<partition>/mb_stream_req
5756c9de560dSAlex Tomas */
ext4_mb_group_or_file(struct ext4_allocation_context * ac)5757c9de560dSAlex Tomas static void ext4_mb_group_or_file(struct ext4_allocation_context *ac)
5758c9de560dSAlex Tomas {
5759c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
5760c9de560dSAlex Tomas int bsbits = ac->ac_sb->s_blocksize_bits;
5761c9de560dSAlex Tomas loff_t size, isize;
5762a9f2a293SJan Kara bool inode_pa_eligible, group_pa_eligible;
5763c9de560dSAlex Tomas
5764c9de560dSAlex Tomas if (!(ac->ac_flags & EXT4_MB_HINT_DATA))
5765c9de560dSAlex Tomas return;
5766c9de560dSAlex Tomas
57674ba74d00STheodore Ts'o if (unlikely(ac->ac_flags & EXT4_MB_HINT_GOAL_ONLY))
57684ba74d00STheodore Ts'o return;
57694ba74d00STheodore Ts'o
5770a9f2a293SJan Kara group_pa_eligible = sbi->s_mb_group_prealloc > 0;
5771a9f2a293SJan Kara inode_pa_eligible = true;
577243bbddc0SBaokun Li size = extent_logical_end(sbi, &ac->ac_o_ex);
577350797481STheodore Ts'o isize = (i_size_read(ac->ac_inode) + ac->ac_sb->s_blocksize - 1)
577450797481STheodore Ts'o >> bsbits;
5775c9de560dSAlex Tomas
5776a9f2a293SJan Kara /* No point in using inode preallocation for closed files */
577782dd124cSNikolay Borisov if ((size == isize) && !ext4_fs_is_busy(sbi) &&
5778a9f2a293SJan Kara !inode_is_open_for_write(ac->ac_inode))
5779a9f2a293SJan Kara inode_pa_eligible = false;
578050797481STheodore Ts'o
578171780577STheodore Ts'o size = max(size, isize);
5782a9f2a293SJan Kara /* Don't use group allocation for large files */
5783a9f2a293SJan Kara if (size > sbi->s_mb_stream_request)
5784a9f2a293SJan Kara group_pa_eligible = false;
5785a9f2a293SJan Kara
5786a9f2a293SJan Kara if (!group_pa_eligible) {
5787a9f2a293SJan Kara if (inode_pa_eligible)
57884ba74d00STheodore Ts'o ac->ac_flags |= EXT4_MB_STREAM_ALLOC;
5789a9f2a293SJan Kara else
5790a9f2a293SJan Kara ac->ac_flags |= EXT4_MB_HINT_NOPREALLOC;
5791c9de560dSAlex Tomas return;
57924ba74d00STheodore Ts'o }
5793c9de560dSAlex Tomas
5794c9de560dSAlex Tomas BUG_ON(ac->ac_lg != NULL);
5795c9de560dSAlex Tomas /*
5796c9de560dSAlex Tomas * locality group prealloc space are per cpu. The reason for having
5797c9de560dSAlex Tomas * per cpu locality group is to reduce the contention between block
5798c9de560dSAlex Tomas * request from multiple CPUs.
5799c9de560dSAlex Tomas */
5800a0b6bc63SChristoph Lameter ac->ac_lg = raw_cpu_ptr(sbi->s_locality_groups);
5801c9de560dSAlex Tomas
5802c9de560dSAlex Tomas /* we're going to use group allocation */
5803c9de560dSAlex Tomas ac->ac_flags |= EXT4_MB_HINT_GROUP_ALLOC;
5804c9de560dSAlex Tomas
5805c9de560dSAlex Tomas /* serialize all allocations in the group */
5806c9de560dSAlex Tomas mutex_lock(&ac->ac_lg->lg_mutex);
5807c9de560dSAlex Tomas }
5808c9de560dSAlex Tomas
5809d73eff68SGuoqing Jiang static noinline_for_stack void
ext4_mb_initialize_context(struct ext4_allocation_context * ac,struct ext4_allocation_request * ar)58104ddfef7bSEric Sandeen ext4_mb_initialize_context(struct ext4_allocation_context *ac,
5811c9de560dSAlex Tomas struct ext4_allocation_request *ar)
5812c9de560dSAlex Tomas {
5813c9de560dSAlex Tomas struct super_block *sb = ar->inode->i_sb;
5814c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
5815c9de560dSAlex Tomas struct ext4_super_block *es = sbi->s_es;
5816c9de560dSAlex Tomas ext4_group_t group;
5817498e5f24STheodore Ts'o unsigned int len;
5818498e5f24STheodore Ts'o ext4_fsblk_t goal;
5819c9de560dSAlex Tomas ext4_grpblk_t block;
5820c9de560dSAlex Tomas
5821c9de560dSAlex Tomas /* we can't allocate > group size */
5822c9de560dSAlex Tomas len = ar->len;
5823c9de560dSAlex Tomas
5824c9de560dSAlex Tomas /* just a dirty hack to filter too big requests */
582540ae3487STheodore Ts'o if (len >= EXT4_CLUSTERS_PER_GROUP(sb))
582640ae3487STheodore Ts'o len = EXT4_CLUSTERS_PER_GROUP(sb);
5827c9de560dSAlex Tomas
5828c9de560dSAlex Tomas /* start searching from the goal */
5829c9de560dSAlex Tomas goal = ar->goal;
5830c9de560dSAlex Tomas if (goal < le32_to_cpu(es->s_first_data_block) ||
5831c9de560dSAlex Tomas goal >= ext4_blocks_count(es))
5832c9de560dSAlex Tomas goal = le32_to_cpu(es->s_first_data_block);
5833c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, goal, &group, &block);
5834c9de560dSAlex Tomas
5835c9de560dSAlex Tomas /* set up allocation goals */
5836f5a44db5STheodore Ts'o ac->ac_b_ex.fe_logical = EXT4_LBLK_CMASK(sbi, ar->logical);
5837c9de560dSAlex Tomas ac->ac_status = AC_STATUS_CONTINUE;
5838c9de560dSAlex Tomas ac->ac_sb = sb;
5839c9de560dSAlex Tomas ac->ac_inode = ar->inode;
584053accfa9STheodore Ts'o ac->ac_o_ex.fe_logical = ac->ac_b_ex.fe_logical;
5841c9de560dSAlex Tomas ac->ac_o_ex.fe_group = group;
5842c9de560dSAlex Tomas ac->ac_o_ex.fe_start = block;
5843c9de560dSAlex Tomas ac->ac_o_ex.fe_len = len;
584453accfa9STheodore Ts'o ac->ac_g_ex = ac->ac_o_ex;
58457e170922SOjaswin Mujoo ac->ac_orig_goal_len = ac->ac_g_ex.fe_len;
5846c9de560dSAlex Tomas ac->ac_flags = ar->flags;
5847c9de560dSAlex Tomas
58483cb77bd2Sbrookxu /* we have to define context: we'll work with a file or
5849c9de560dSAlex Tomas * locality group. this is a policy, actually */
5850c9de560dSAlex Tomas ext4_mb_group_or_file(ac);
5851c9de560dSAlex Tomas
5852d3df1453SRitesh Harjani mb_debug(sb, "init ac: %u blocks @ %u, goal %u, flags 0x%x, 2^%d, "
5853c9de560dSAlex Tomas "left: %u/%u, right %u/%u to %swritable\n",
5854c9de560dSAlex Tomas (unsigned) ar->len, (unsigned) ar->logical,
5855c9de560dSAlex Tomas (unsigned) ar->goal, ac->ac_flags, ac->ac_2order,
5856c9de560dSAlex Tomas (unsigned) ar->lleft, (unsigned) ar->pleft,
5857c9de560dSAlex Tomas (unsigned) ar->lright, (unsigned) ar->pright,
585882dd124cSNikolay Borisov inode_is_open_for_write(ar->inode) ? "" : "non-");
5859c9de560dSAlex Tomas }
5860c9de560dSAlex Tomas
58616be2ded1SAneesh Kumar K.V static noinline_for_stack void
ext4_mb_discard_lg_preallocations(struct super_block * sb,struct ext4_locality_group * lg,int order,int total_entries)58626be2ded1SAneesh Kumar K.V ext4_mb_discard_lg_preallocations(struct super_block *sb,
58636be2ded1SAneesh Kumar K.V struct ext4_locality_group *lg,
58646be2ded1SAneesh Kumar K.V int order, int total_entries)
58656be2ded1SAneesh Kumar K.V {
58666be2ded1SAneesh Kumar K.V ext4_group_t group = 0;
58676be2ded1SAneesh Kumar K.V struct ext4_buddy e4b;
58680f6bc579SRuan Jinjie LIST_HEAD(discard_list);
58696be2ded1SAneesh Kumar K.V struct ext4_prealloc_space *pa, *tmp;
58706be2ded1SAneesh Kumar K.V
5871d3df1453SRitesh Harjani mb_debug(sb, "discard locality group preallocation\n");
58726be2ded1SAneesh Kumar K.V
58736be2ded1SAneesh Kumar K.V spin_lock(&lg->lg_prealloc_lock);
58746be2ded1SAneesh Kumar K.V list_for_each_entry_rcu(pa, &lg->lg_prealloc_list[order],
5875a8e38fd3SOjaswin Mujoo pa_node.lg_list,
587692e9c58cSMadhuparna Bhowmik lockdep_is_held(&lg->lg_prealloc_lock)) {
58776be2ded1SAneesh Kumar K.V spin_lock(&pa->pa_lock);
58786be2ded1SAneesh Kumar K.V if (atomic_read(&pa->pa_count)) {
58796be2ded1SAneesh Kumar K.V /*
58806be2ded1SAneesh Kumar K.V * This is the pa that we just used
58816be2ded1SAneesh Kumar K.V * for block allocation. So don't
58826be2ded1SAneesh Kumar K.V * free that
58836be2ded1SAneesh Kumar K.V */
58846be2ded1SAneesh Kumar K.V spin_unlock(&pa->pa_lock);
58856be2ded1SAneesh Kumar K.V continue;
58866be2ded1SAneesh Kumar K.V }
58876be2ded1SAneesh Kumar K.V if (pa->pa_deleted) {
58886be2ded1SAneesh Kumar K.V spin_unlock(&pa->pa_lock);
58896be2ded1SAneesh Kumar K.V continue;
58906be2ded1SAneesh Kumar K.V }
58916be2ded1SAneesh Kumar K.V /* only lg prealloc space */
5892cc0fb9adSAneesh Kumar K.V BUG_ON(pa->pa_type != MB_GROUP_PA);
58936be2ded1SAneesh Kumar K.V
58946be2ded1SAneesh Kumar K.V /* seems this one can be freed ... */
589527bc446eSbrookxu ext4_mb_mark_pa_deleted(sb, pa);
58966be2ded1SAneesh Kumar K.V spin_unlock(&pa->pa_lock);
58976be2ded1SAneesh Kumar K.V
5898a8e38fd3SOjaswin Mujoo list_del_rcu(&pa->pa_node.lg_list);
58996be2ded1SAneesh Kumar K.V list_add(&pa->u.pa_tmp_list, &discard_list);
59006be2ded1SAneesh Kumar K.V
59016be2ded1SAneesh Kumar K.V total_entries--;
59026be2ded1SAneesh Kumar K.V if (total_entries <= 5) {
59036be2ded1SAneesh Kumar K.V /*
59046be2ded1SAneesh Kumar K.V * we want to keep only 5 entries
59056be2ded1SAneesh Kumar K.V * allowing it to grow to 8. This
59066be2ded1SAneesh Kumar K.V * mak sure we don't call discard
59076be2ded1SAneesh Kumar K.V * soon for this list.
59086be2ded1SAneesh Kumar K.V */
59096be2ded1SAneesh Kumar K.V break;
59106be2ded1SAneesh Kumar K.V }
59116be2ded1SAneesh Kumar K.V }
59126be2ded1SAneesh Kumar K.V spin_unlock(&lg->lg_prealloc_lock);
59136be2ded1SAneesh Kumar K.V
59146be2ded1SAneesh Kumar K.V list_for_each_entry_safe(pa, tmp, &discard_list, u.pa_tmp_list) {
59159651e6b2SKonstantin Khlebnikov int err;
59166be2ded1SAneesh Kumar K.V
5917bd86298eSLukas Czerner group = ext4_get_group_number(sb, pa->pa_pstart);
59189651e6b2SKonstantin Khlebnikov err = ext4_mb_load_buddy_gfp(sb, group, &e4b,
59199651e6b2SKonstantin Khlebnikov GFP_NOFS|__GFP_NOFAIL);
59209651e6b2SKonstantin Khlebnikov if (err) {
592154d3adbcSTheodore Ts'o ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
59229651e6b2SKonstantin Khlebnikov err, group);
59236be2ded1SAneesh Kumar K.V continue;
59246be2ded1SAneesh Kumar K.V }
59256be2ded1SAneesh Kumar K.V ext4_lock_group(sb, group);
59266be2ded1SAneesh Kumar K.V list_del(&pa->pa_group_list);
59273e1e5f50SEric Sandeen ext4_mb_release_group_pa(&e4b, pa);
59286be2ded1SAneesh Kumar K.V ext4_unlock_group(sb, group);
59296be2ded1SAneesh Kumar K.V
5930e39e07fdSJing Zhang ext4_mb_unload_buddy(&e4b);
59316be2ded1SAneesh Kumar K.V list_del(&pa->u.pa_tmp_list);
59326be2ded1SAneesh Kumar K.V call_rcu(&(pa)->u.pa_rcu, ext4_mb_pa_callback);
59336be2ded1SAneesh Kumar K.V }
59346be2ded1SAneesh Kumar K.V }
59356be2ded1SAneesh Kumar K.V
59366be2ded1SAneesh Kumar K.V /*
59376be2ded1SAneesh Kumar K.V * We have incremented pa_count. So it cannot be freed at this
59386be2ded1SAneesh Kumar K.V * point. Also we hold lg_mutex. So no parallel allocation is
59396be2ded1SAneesh Kumar K.V * possible from this lg. That means pa_free cannot be updated.
59406be2ded1SAneesh Kumar K.V *
59416be2ded1SAneesh Kumar K.V * A parallel ext4_mb_discard_group_preallocations is possible.
59426be2ded1SAneesh Kumar K.V * which can cause the lg_prealloc_list to be updated.
59436be2ded1SAneesh Kumar K.V */
59446be2ded1SAneesh Kumar K.V
ext4_mb_add_n_trim(struct ext4_allocation_context * ac)59456be2ded1SAneesh Kumar K.V static void ext4_mb_add_n_trim(struct ext4_allocation_context *ac)
59466be2ded1SAneesh Kumar K.V {
59476be2ded1SAneesh Kumar K.V int order, added = 0, lg_prealloc_count = 1;
59486be2ded1SAneesh Kumar K.V struct super_block *sb = ac->ac_sb;
59496be2ded1SAneesh Kumar K.V struct ext4_locality_group *lg = ac->ac_lg;
59506be2ded1SAneesh Kumar K.V struct ext4_prealloc_space *tmp_pa, *pa = ac->ac_pa;
59516be2ded1SAneesh Kumar K.V
59526be2ded1SAneesh Kumar K.V order = fls(pa->pa_free) - 1;
59536be2ded1SAneesh Kumar K.V if (order > PREALLOC_TB_SIZE - 1)
59546be2ded1SAneesh Kumar K.V /* The max size of hash table is PREALLOC_TB_SIZE */
59556be2ded1SAneesh Kumar K.V order = PREALLOC_TB_SIZE - 1;
59566be2ded1SAneesh Kumar K.V /* Add the prealloc space to lg */
5957f1167009SNiu Yawei spin_lock(&lg->lg_prealloc_lock);
59586be2ded1SAneesh Kumar K.V list_for_each_entry_rcu(tmp_pa, &lg->lg_prealloc_list[order],
5959a8e38fd3SOjaswin Mujoo pa_node.lg_list,
596092e9c58cSMadhuparna Bhowmik lockdep_is_held(&lg->lg_prealloc_lock)) {
59616be2ded1SAneesh Kumar K.V spin_lock(&tmp_pa->pa_lock);
59626be2ded1SAneesh Kumar K.V if (tmp_pa->pa_deleted) {
5963e7c9e3e9STheodore Ts'o spin_unlock(&tmp_pa->pa_lock);
59646be2ded1SAneesh Kumar K.V continue;
59656be2ded1SAneesh Kumar K.V }
59666be2ded1SAneesh Kumar K.V if (!added && pa->pa_free < tmp_pa->pa_free) {
59676be2ded1SAneesh Kumar K.V /* Add to the tail of the previous entry */
5968a8e38fd3SOjaswin Mujoo list_add_tail_rcu(&pa->pa_node.lg_list,
5969a8e38fd3SOjaswin Mujoo &tmp_pa->pa_node.lg_list);
59706be2ded1SAneesh Kumar K.V added = 1;
59716be2ded1SAneesh Kumar K.V /*
59726be2ded1SAneesh Kumar K.V * we want to count the total
59736be2ded1SAneesh Kumar K.V * number of entries in the list
59746be2ded1SAneesh Kumar K.V */
59756be2ded1SAneesh Kumar K.V }
59766be2ded1SAneesh Kumar K.V spin_unlock(&tmp_pa->pa_lock);
59776be2ded1SAneesh Kumar K.V lg_prealloc_count++;
59786be2ded1SAneesh Kumar K.V }
59796be2ded1SAneesh Kumar K.V if (!added)
5980a8e38fd3SOjaswin Mujoo list_add_tail_rcu(&pa->pa_node.lg_list,
59816be2ded1SAneesh Kumar K.V &lg->lg_prealloc_list[order]);
5982f1167009SNiu Yawei spin_unlock(&lg->lg_prealloc_lock);
59836be2ded1SAneesh Kumar K.V
59846be2ded1SAneesh Kumar K.V /* Now trim the list to be not more than 8 elements */
5985ad635507SKemeng Shi if (lg_prealloc_count > 8)
59866be2ded1SAneesh Kumar K.V ext4_mb_discard_lg_preallocations(sb, lg,
59876be2ded1SAneesh Kumar K.V order, lg_prealloc_count);
59886be2ded1SAneesh Kumar K.V }
59896be2ded1SAneesh Kumar K.V
5990c9de560dSAlex Tomas /*
5991c9de560dSAlex Tomas * release all resource we used in allocation
5992c9de560dSAlex Tomas */
ext4_mb_release_context(struct ext4_allocation_context * ac)5993c9de560dSAlex Tomas static int ext4_mb_release_context(struct ext4_allocation_context *ac)
5994c9de560dSAlex Tomas {
599553accfa9STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(ac->ac_sb);
59966be2ded1SAneesh Kumar K.V struct ext4_prealloc_space *pa = ac->ac_pa;
59976be2ded1SAneesh Kumar K.V if (pa) {
5998cc0fb9adSAneesh Kumar K.V if (pa->pa_type == MB_GROUP_PA) {
5999c9de560dSAlex Tomas /* see comment in ext4_mb_use_group_pa() */
60006be2ded1SAneesh Kumar K.V spin_lock(&pa->pa_lock);
600153accfa9STheodore Ts'o pa->pa_pstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
600253accfa9STheodore Ts'o pa->pa_lstart += EXT4_C2B(sbi, ac->ac_b_ex.fe_len);
60036be2ded1SAneesh Kumar K.V pa->pa_free -= ac->ac_b_ex.fe_len;
60046be2ded1SAneesh Kumar K.V pa->pa_len -= ac->ac_b_ex.fe_len;
60056be2ded1SAneesh Kumar K.V spin_unlock(&pa->pa_lock);
600666d5e027Sbrookxu
60076be2ded1SAneesh Kumar K.V /*
60086be2ded1SAneesh Kumar K.V * We want to add the pa to the right bucket.
60096be2ded1SAneesh Kumar K.V * Remove it from the list and while adding
60106be2ded1SAneesh Kumar K.V * make sure the list to which we are adding
601144183d42SAmir Goldstein * doesn't grow big.
60126be2ded1SAneesh Kumar K.V */
601366d5e027Sbrookxu if (likely(pa->pa_free)) {
6014a8e38fd3SOjaswin Mujoo spin_lock(pa->pa_node_lock.lg_lock);
6015a8e38fd3SOjaswin Mujoo list_del_rcu(&pa->pa_node.lg_list);
6016a8e38fd3SOjaswin Mujoo spin_unlock(pa->pa_node_lock.lg_lock);
60176be2ded1SAneesh Kumar K.V ext4_mb_add_n_trim(ac);
6018c9de560dSAlex Tomas }
601966d5e027Sbrookxu }
602027bc446eSbrookxu
60216be2ded1SAneesh Kumar K.V ext4_mb_put_pa(ac, ac->ac_sb, pa);
6022c9de560dSAlex Tomas }
6023c9de560dSAlex Tomas if (ac->ac_bitmap_page)
602409cbfeafSKirill A. Shutemov put_page(ac->ac_bitmap_page);
6025c9de560dSAlex Tomas if (ac->ac_buddy_page)
602609cbfeafSKirill A. Shutemov put_page(ac->ac_buddy_page);
6027c9de560dSAlex Tomas if (ac->ac_flags & EXT4_MB_HINT_GROUP_ALLOC)
6028c9de560dSAlex Tomas mutex_unlock(&ac->ac_lg->lg_mutex);
6029c9de560dSAlex Tomas ext4_mb_collect_stats(ac);
6030c9de560dSAlex Tomas return 0;
6031c9de560dSAlex Tomas }
6032c9de560dSAlex Tomas
ext4_mb_discard_preallocations(struct super_block * sb,int needed)6033c9de560dSAlex Tomas static int ext4_mb_discard_preallocations(struct super_block *sb, int needed)
6034c9de560dSAlex Tomas {
60358df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb);
6036c9de560dSAlex Tomas int ret;
60378c80fb31SChunguang Xu int freed = 0, busy = 0;
60388c80fb31SChunguang Xu int retry = 0;
6039c9de560dSAlex Tomas
60409bffad1eSTheodore Ts'o trace_ext4_mb_discard_preallocations(sb, needed);
60418c80fb31SChunguang Xu
60428c80fb31SChunguang Xu if (needed == 0)
60438c80fb31SChunguang Xu needed = EXT4_CLUSTERS_PER_GROUP(sb) + 1;
60448c80fb31SChunguang Xu repeat:
60458df9675fSTheodore Ts'o for (i = 0; i < ngroups && needed > 0; i++) {
60468c80fb31SChunguang Xu ret = ext4_mb_discard_group_preallocations(sb, i, &busy);
6047c9de560dSAlex Tomas freed += ret;
6048c9de560dSAlex Tomas needed -= ret;
60498c80fb31SChunguang Xu cond_resched();
60508c80fb31SChunguang Xu }
60518c80fb31SChunguang Xu
60528c80fb31SChunguang Xu if (needed > 0 && busy && ++retry < 3) {
60538c80fb31SChunguang Xu busy = 0;
60548c80fb31SChunguang Xu goto repeat;
6055c9de560dSAlex Tomas }
6056c9de560dSAlex Tomas
6057c9de560dSAlex Tomas return freed;
6058c9de560dSAlex Tomas }
6059c9de560dSAlex Tomas
ext4_mb_discard_preallocations_should_retry(struct super_block * sb,struct ext4_allocation_context * ac,u64 * seq)6060cf5e2ca6SRitesh Harjani static bool ext4_mb_discard_preallocations_should_retry(struct super_block *sb,
606107b5b8e1SRitesh Harjani struct ext4_allocation_context *ac, u64 *seq)
6062cf5e2ca6SRitesh Harjani {
6063cf5e2ca6SRitesh Harjani int freed;
606407b5b8e1SRitesh Harjani u64 seq_retry = 0;
606507b5b8e1SRitesh Harjani bool ret = false;
6066cf5e2ca6SRitesh Harjani
6067cf5e2ca6SRitesh Harjani freed = ext4_mb_discard_preallocations(sb, ac->ac_o_ex.fe_len);
606807b5b8e1SRitesh Harjani if (freed) {
606907b5b8e1SRitesh Harjani ret = true;
607007b5b8e1SRitesh Harjani goto out_dbg;
607107b5b8e1SRitesh Harjani }
607207b5b8e1SRitesh Harjani seq_retry = ext4_get_discard_pa_seq_sum();
607399377830SRitesh Harjani if (!(ac->ac_flags & EXT4_MB_STRICT_CHECK) || seq_retry != *seq) {
607499377830SRitesh Harjani ac->ac_flags |= EXT4_MB_STRICT_CHECK;
607507b5b8e1SRitesh Harjani *seq = seq_retry;
607607b5b8e1SRitesh Harjani ret = true;
607707b5b8e1SRitesh Harjani }
607807b5b8e1SRitesh Harjani
607907b5b8e1SRitesh Harjani out_dbg:
608007b5b8e1SRitesh Harjani mb_debug(sb, "freed %d, retry ? %s\n", freed, ret ? "yes" : "no");
608107b5b8e1SRitesh Harjani return ret;
6082cf5e2ca6SRitesh Harjani }
6083cf5e2ca6SRitesh Harjani
6084ad78b5efSKemeng Shi /*
6085ad78b5efSKemeng Shi * Simple allocator for Ext4 fast commit replay path. It searches for blocks
6086ad78b5efSKemeng Shi * linearly starting at the goal block and also excludes the blocks which
6087ad78b5efSKemeng Shi * are going to be in use after fast commit replay.
6088ad78b5efSKemeng Shi */
6089ad78b5efSKemeng Shi static ext4_fsblk_t
ext4_mb_new_blocks_simple(struct ext4_allocation_request * ar,int * errp)6090ad78b5efSKemeng Shi ext4_mb_new_blocks_simple(struct ext4_allocation_request *ar, int *errp)
6091ad78b5efSKemeng Shi {
6092ad78b5efSKemeng Shi struct buffer_head *bitmap_bh;
6093ad78b5efSKemeng Shi struct super_block *sb = ar->inode->i_sb;
6094ad78b5efSKemeng Shi struct ext4_sb_info *sbi = EXT4_SB(sb);
6095ad78b5efSKemeng Shi ext4_group_t group, nr;
6096ad78b5efSKemeng Shi ext4_grpblk_t blkoff;
6097ad78b5efSKemeng Shi ext4_grpblk_t max = EXT4_CLUSTERS_PER_GROUP(sb);
6098ad78b5efSKemeng Shi ext4_grpblk_t i = 0;
6099ad78b5efSKemeng Shi ext4_fsblk_t goal, block;
610079ebf48cSLu Hongfei struct ext4_super_block *es = sbi->s_es;
6101ad78b5efSKemeng Shi
6102ad78b5efSKemeng Shi goal = ar->goal;
6103ad78b5efSKemeng Shi if (goal < le32_to_cpu(es->s_first_data_block) ||
6104ad78b5efSKemeng Shi goal >= ext4_blocks_count(es))
6105ad78b5efSKemeng Shi goal = le32_to_cpu(es->s_first_data_block);
6106ad78b5efSKemeng Shi
6107ad78b5efSKemeng Shi ar->len = 0;
6108ad78b5efSKemeng Shi ext4_get_group_no_and_offset(sb, goal, &group, &blkoff);
6109ad78b5efSKemeng Shi for (nr = ext4_get_groups_count(sb); nr > 0; nr--) {
6110ad78b5efSKemeng Shi bitmap_bh = ext4_read_block_bitmap(sb, group);
6111ad78b5efSKemeng Shi if (IS_ERR(bitmap_bh)) {
6112ad78b5efSKemeng Shi *errp = PTR_ERR(bitmap_bh);
6113ad78b5efSKemeng Shi pr_warn("Failed to read block bitmap\n");
6114ad78b5efSKemeng Shi return 0;
6115ad78b5efSKemeng Shi }
6116ad78b5efSKemeng Shi
6117ad78b5efSKemeng Shi while (1) {
6118ad78b5efSKemeng Shi i = mb_find_next_zero_bit(bitmap_bh->b_data, max,
6119ad78b5efSKemeng Shi blkoff);
6120ad78b5efSKemeng Shi if (i >= max)
6121ad78b5efSKemeng Shi break;
6122ad78b5efSKemeng Shi if (ext4_fc_replay_check_excluded(sb,
6123ad78b5efSKemeng Shi ext4_group_first_block_no(sb, group) +
6124ad78b5efSKemeng Shi EXT4_C2B(sbi, i))) {
6125ad78b5efSKemeng Shi blkoff = i + 1;
6126ad78b5efSKemeng Shi } else
6127ad78b5efSKemeng Shi break;
6128ad78b5efSKemeng Shi }
6129ad78b5efSKemeng Shi brelse(bitmap_bh);
6130ad78b5efSKemeng Shi if (i < max)
6131ad78b5efSKemeng Shi break;
6132ad78b5efSKemeng Shi
6133ad78b5efSKemeng Shi if (++group >= ext4_get_groups_count(sb))
6134ad78b5efSKemeng Shi group = 0;
6135ad78b5efSKemeng Shi
6136ad78b5efSKemeng Shi blkoff = 0;
6137ad78b5efSKemeng Shi }
6138ad78b5efSKemeng Shi
6139ad78b5efSKemeng Shi if (i >= max) {
6140ad78b5efSKemeng Shi *errp = -ENOSPC;
6141ad78b5efSKemeng Shi return 0;
6142ad78b5efSKemeng Shi }
6143ad78b5efSKemeng Shi
6144ad78b5efSKemeng Shi block = ext4_group_first_block_no(sb, group) + EXT4_C2B(sbi, i);
6145ad78b5efSKemeng Shi ext4_mb_mark_bb(sb, block, 1, 1);
6146ad78b5efSKemeng Shi ar->len = 1;
6147ad78b5efSKemeng Shi
614807fa88b0SDan Carpenter *errp = 0;
6149ad78b5efSKemeng Shi return block;
6150ad78b5efSKemeng Shi }
61518016e29fSHarshad Shirwadkar
6152c9de560dSAlex Tomas /*
6153c9de560dSAlex Tomas * Main entry point into mballoc to allocate blocks
6154c9de560dSAlex Tomas * it tries to use preallocation first, then falls back
6155c9de560dSAlex Tomas * to usual allocation
6156c9de560dSAlex Tomas */
ext4_mb_new_blocks(handle_t * handle,struct ext4_allocation_request * ar,int * errp)6157c9de560dSAlex Tomas ext4_fsblk_t ext4_mb_new_blocks(handle_t *handle,
6158c9de560dSAlex Tomas struct ext4_allocation_request *ar, int *errp)
6159c9de560dSAlex Tomas {
6160256bdb49SEric Sandeen struct ext4_allocation_context *ac = NULL;
6161c9de560dSAlex Tomas struct ext4_sb_info *sbi;
6162c9de560dSAlex Tomas struct super_block *sb;
6163c9de560dSAlex Tomas ext4_fsblk_t block = 0;
616460e58e0fSMingming Cao unsigned int inquota = 0;
616553accfa9STheodore Ts'o unsigned int reserv_clstrs = 0;
616680fa46d6STheodore Ts'o int retries = 0;
616707b5b8e1SRitesh Harjani u64 seq;
6168c9de560dSAlex Tomas
6169b10a44c3STheodore Ts'o might_sleep();
6170c9de560dSAlex Tomas sb = ar->inode->i_sb;
6171c9de560dSAlex Tomas sbi = EXT4_SB(sb);
6172c9de560dSAlex Tomas
61739bffad1eSTheodore Ts'o trace_ext4_request_blocks(ar);
61748016e29fSHarshad Shirwadkar if (sbi->s_mount_state & EXT4_FC_REPLAY)
6175ad78b5efSKemeng Shi return ext4_mb_new_blocks_simple(ar, errp);
6176ba80b101STheodore Ts'o
617745dc63e7SDmitry Monakhov /* Allow to use superuser reservation for quota file */
617802749a4cSTahsin Erdogan if (ext4_is_quota_file(ar->inode))
617945dc63e7SDmitry Monakhov ar->flags |= EXT4_MB_USE_ROOT_BLOCKS;
618045dc63e7SDmitry Monakhov
6181e3cf5d5dSTheodore Ts'o if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0) {
618260e58e0fSMingming Cao /* Without delayed allocation we need to verify
618360e58e0fSMingming Cao * there is enough free blocks to do block allocation
618460e58e0fSMingming Cao * and verify allocation doesn't exceed the quota limits.
6185d2a17637SMingming Cao */
618655f020dbSAllison Henderson while (ar->len &&
6187e7d5f315STheodore Ts'o ext4_claim_free_clusters(sbi, ar->len, ar->flags)) {
618855f020dbSAllison Henderson
6189030ba6bcSAneesh Kumar K.V /* let others to free the space */
6190bb8b20edSLukas Czerner cond_resched();
6191030ba6bcSAneesh Kumar K.V ar->len = ar->len >> 1;
6192030ba6bcSAneesh Kumar K.V }
6193030ba6bcSAneesh Kumar K.V if (!ar->len) {
6194bbc4ec77SRitesh Harjani ext4_mb_show_pa(sb);
619507031431SMingming Cao *errp = -ENOSPC;
619607031431SMingming Cao return 0;
619707031431SMingming Cao }
619853accfa9STheodore Ts'o reserv_clstrs = ar->len;
619955f020dbSAllison Henderson if (ar->flags & EXT4_MB_USE_ROOT_BLOCKS) {
620053accfa9STheodore Ts'o dquot_alloc_block_nofail(ar->inode,
620153accfa9STheodore Ts'o EXT4_C2B(sbi, ar->len));
620255f020dbSAllison Henderson } else {
620355f020dbSAllison Henderson while (ar->len &&
620453accfa9STheodore Ts'o dquot_alloc_block(ar->inode,
620553accfa9STheodore Ts'o EXT4_C2B(sbi, ar->len))) {
620655f020dbSAllison Henderson
6207c9de560dSAlex Tomas ar->flags |= EXT4_MB_HINT_NOPREALLOC;
6208c9de560dSAlex Tomas ar->len--;
6209c9de560dSAlex Tomas }
621055f020dbSAllison Henderson }
621160e58e0fSMingming Cao inquota = ar->len;
6212c9de560dSAlex Tomas if (ar->len == 0) {
6213c9de560dSAlex Tomas *errp = -EDQUOT;
62146c7a120aSAditya Kali goto out;
6215c9de560dSAlex Tomas }
621660e58e0fSMingming Cao }
6217d2a17637SMingming Cao
621885556c9aSWei Yongjun ac = kmem_cache_zalloc(ext4_ac_cachep, GFP_NOFS);
6219833576b3STheodore Ts'o if (!ac) {
6220363d4251SShen Feng ar->len = 0;
6221256bdb49SEric Sandeen *errp = -ENOMEM;
62226c7a120aSAditya Kali goto out;
6223256bdb49SEric Sandeen }
6224256bdb49SEric Sandeen
6225d73eff68SGuoqing Jiang ext4_mb_initialize_context(ac, ar);
6226c9de560dSAlex Tomas
6227256bdb49SEric Sandeen ac->ac_op = EXT4_MB_HISTORY_PREALLOC;
622881198536SRitesh Harjani seq = this_cpu_read(discard_pa_seq);
6229256bdb49SEric Sandeen if (!ext4_mb_use_preallocated(ac)) {
6230256bdb49SEric Sandeen ac->ac_op = EXT4_MB_HISTORY_ALLOC;
6231256bdb49SEric Sandeen ext4_mb_normalize_request(ac, ar);
623253f86b17SRitesh Harjani
623353f86b17SRitesh Harjani *errp = ext4_mb_pa_alloc(ac);
623453f86b17SRitesh Harjani if (*errp)
623553f86b17SRitesh Harjani goto errout;
6236c9de560dSAlex Tomas repeat:
6237c9de560dSAlex Tomas /* allocate space in core */
62386c7a120aSAditya Kali *errp = ext4_mb_regular_allocator(ac);
623953f86b17SRitesh Harjani /*
624053f86b17SRitesh Harjani * pa allocated above is added to grp->bb_prealloc_list only
624153f86b17SRitesh Harjani * when we were able to allocate some block i.e. when
624253f86b17SRitesh Harjani * ac->ac_status == AC_STATUS_FOUND.
624353f86b17SRitesh Harjani * And error from above mean ac->ac_status != AC_STATUS_FOUND
624453f86b17SRitesh Harjani * So we have to free this pa here itself.
624553f86b17SRitesh Harjani */
62462c00ef3eSAlexey Khoroshilov if (*errp) {
624782089725SOjaswin Mujoo ext4_mb_pa_put_free(ac);
62482c00ef3eSAlexey Khoroshilov ext4_discard_allocated_blocks(ac);
62492c00ef3eSAlexey Khoroshilov goto errout;
62502c00ef3eSAlexey Khoroshilov }
625153f86b17SRitesh Harjani if (ac->ac_status == AC_STATUS_FOUND &&
625253f86b17SRitesh Harjani ac->ac_o_ex.fe_len >= ac->ac_f_ex.fe_len)
625382089725SOjaswin Mujoo ext4_mb_pa_put_free(ac);
6254c9de560dSAlex Tomas }
6255256bdb49SEric Sandeen if (likely(ac->ac_status == AC_STATUS_FOUND)) {
625653accfa9STheodore Ts'o *errp = ext4_mb_mark_diskspace_used(ac, handle, reserv_clstrs);
6257554a5cccSVegard Nossum if (*errp) {
6258b844167eSCurt Wohlgemuth ext4_discard_allocated_blocks(ac);
62596d138cedSEric Sandeen goto errout;
62606d138cedSEric Sandeen } else {
6261256bdb49SEric Sandeen block = ext4_grp_offs_to_block(sb, &ac->ac_b_ex);
6262256bdb49SEric Sandeen ar->len = ac->ac_b_ex.fe_len;
6263519deca0SAneesh Kumar K.V }
6264c9de560dSAlex Tomas } else {
626580fa46d6STheodore Ts'o if (++retries < 3 &&
626680fa46d6STheodore Ts'o ext4_mb_discard_preallocations_should_retry(sb, ac, &seq))
6267c9de560dSAlex Tomas goto repeat;
626853f86b17SRitesh Harjani /*
626953f86b17SRitesh Harjani * If block allocation fails then the pa allocated above
627053f86b17SRitesh Harjani * needs to be freed here itself.
627153f86b17SRitesh Harjani */
627282089725SOjaswin Mujoo ext4_mb_pa_put_free(ac);
6273c9de560dSAlex Tomas *errp = -ENOSPC;
62746c7a120aSAditya Kali }
62756c7a120aSAditya Kali
62766c7a120aSAditya Kali if (*errp) {
6277aaae558dSKemeng Shi errout:
6278256bdb49SEric Sandeen ac->ac_b_ex.fe_len = 0;
6279c9de560dSAlex Tomas ar->len = 0;
6280256bdb49SEric Sandeen ext4_mb_show_ac(ac);
6281c9de560dSAlex Tomas }
6282256bdb49SEric Sandeen ext4_mb_release_context(ac);
6283363d4251SShen Feng kmem_cache_free(ext4_ac_cachep, ac);
6284aaae558dSKemeng Shi out:
628560e58e0fSMingming Cao if (inquota && ar->len < inquota)
628653accfa9STheodore Ts'o dquot_free_block(ar->inode, EXT4_C2B(sbi, inquota - ar->len));
62870087d9fbSAneesh Kumar K.V if (!ar->len) {
6288e3cf5d5dSTheodore Ts'o if ((ar->flags & EXT4_MB_DELALLOC_RESERVED) == 0)
62890087d9fbSAneesh Kumar K.V /* release all the reserved blocks if non delalloc */
629057042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter,
629153accfa9STheodore Ts'o reserv_clstrs);
62920087d9fbSAneesh Kumar K.V }
6293c9de560dSAlex Tomas
62949bffad1eSTheodore Ts'o trace_ext4_allocate_blocks(ar, (unsigned long long)block);
6295ba80b101STheodore Ts'o
6296c9de560dSAlex Tomas return block;
6297c9de560dSAlex Tomas }
6298c9de560dSAlex Tomas
6299c894058dSAneesh Kumar K.V /*
6300c894058dSAneesh Kumar K.V * We can merge two free data extents only if the physical blocks
6301c894058dSAneesh Kumar K.V * are contiguous, AND the extents were freed by the same transaction,
6302c894058dSAneesh Kumar K.V * AND the blocks are associated with the same group.
6303c894058dSAneesh Kumar K.V */
ext4_try_merge_freed_extent(struct ext4_sb_info * sbi,struct ext4_free_data * entry,struct ext4_free_data * new_entry,struct rb_root * entry_rb_root)6304a0154344SDaeho Jeong static void ext4_try_merge_freed_extent(struct ext4_sb_info *sbi,
6305a0154344SDaeho Jeong struct ext4_free_data *entry,
6306a0154344SDaeho Jeong struct ext4_free_data *new_entry,
6307a0154344SDaeho Jeong struct rb_root *entry_rb_root)
6308c894058dSAneesh Kumar K.V {
6309a0154344SDaeho Jeong if ((entry->efd_tid != new_entry->efd_tid) ||
6310a0154344SDaeho Jeong (entry->efd_group != new_entry->efd_group))
6311a0154344SDaeho Jeong return;
6312a0154344SDaeho Jeong if (entry->efd_start_cluster + entry->efd_count ==
6313a0154344SDaeho Jeong new_entry->efd_start_cluster) {
6314a0154344SDaeho Jeong new_entry->efd_start_cluster = entry->efd_start_cluster;
6315a0154344SDaeho Jeong new_entry->efd_count += entry->efd_count;
6316a0154344SDaeho Jeong } else if (new_entry->efd_start_cluster + new_entry->efd_count ==
6317a0154344SDaeho Jeong entry->efd_start_cluster) {
6318a0154344SDaeho Jeong new_entry->efd_count += entry->efd_count;
6319a0154344SDaeho Jeong } else
6320a0154344SDaeho Jeong return;
6321a0154344SDaeho Jeong spin_lock(&sbi->s_md_lock);
6322a0154344SDaeho Jeong list_del(&entry->efd_list);
6323a0154344SDaeho Jeong spin_unlock(&sbi->s_md_lock);
6324a0154344SDaeho Jeong rb_erase(&entry->efd_node, entry_rb_root);
6325a0154344SDaeho Jeong kmem_cache_free(ext4_free_data_cachep, entry);
6326c894058dSAneesh Kumar K.V }
6327c894058dSAneesh Kumar K.V
632885b67ffbSKemeng Shi static noinline_for_stack void
ext4_mb_free_metadata(handle_t * handle,struct ext4_buddy * e4b,struct ext4_free_data * new_entry)63294ddfef7bSEric Sandeen ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
63307a2fcbf7SAneesh Kumar K.V struct ext4_free_data *new_entry)
6331c9de560dSAlex Tomas {
6332e29136f8STheodore Ts'o ext4_group_t group = e4b->bd_group;
633384130193STheodore Ts'o ext4_grpblk_t cluster;
6334d08854f5STheodore Ts'o ext4_grpblk_t clusters = new_entry->efd_count;
63357a2fcbf7SAneesh Kumar K.V struct ext4_free_data *entry;
6336c9de560dSAlex Tomas struct ext4_group_info *db = e4b->bd_info;
6337c9de560dSAlex Tomas struct super_block *sb = e4b->bd_sb;
6338c9de560dSAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb);
6339c894058dSAneesh Kumar K.V struct rb_node **n = &db->bb_free_root.rb_node, *node;
6340c894058dSAneesh Kumar K.V struct rb_node *parent = NULL, *new_node;
6341c894058dSAneesh Kumar K.V
63420390131bSFrank Mayhar BUG_ON(!ext4_handle_valid(handle));
6343c9de560dSAlex Tomas BUG_ON(e4b->bd_bitmap_page == NULL);
6344c9de560dSAlex Tomas BUG_ON(e4b->bd_buddy_page == NULL);
6345c9de560dSAlex Tomas
634618aadd47SBobi Jam new_node = &new_entry->efd_node;
634718aadd47SBobi Jam cluster = new_entry->efd_start_cluster;
6348c9de560dSAlex Tomas
6349c894058dSAneesh Kumar K.V if (!*n) {
6350c894058dSAneesh Kumar K.V /* first free block exent. We need to
6351c894058dSAneesh Kumar K.V protect buddy cache from being freed,
6352c9de560dSAlex Tomas * otherwise we'll refresh it from
6353c9de560dSAlex Tomas * on-disk bitmap and lose not-yet-available
6354c9de560dSAlex Tomas * blocks */
635509cbfeafSKirill A. Shutemov get_page(e4b->bd_buddy_page);
635609cbfeafSKirill A. Shutemov get_page(e4b->bd_bitmap_page);
6357c894058dSAneesh Kumar K.V }
6358c894058dSAneesh Kumar K.V while (*n) {
6359c894058dSAneesh Kumar K.V parent = *n;
636018aadd47SBobi Jam entry = rb_entry(parent, struct ext4_free_data, efd_node);
636118aadd47SBobi Jam if (cluster < entry->efd_start_cluster)
6362c894058dSAneesh Kumar K.V n = &(*n)->rb_left;
636318aadd47SBobi Jam else if (cluster >= (entry->efd_start_cluster + entry->efd_count))
6364c894058dSAneesh Kumar K.V n = &(*n)->rb_right;
6365c894058dSAneesh Kumar K.V else {
6366e29136f8STheodore Ts'o ext4_grp_locked_error(sb, group, 0,
636784130193STheodore Ts'o ext4_group_first_block_no(sb, group) +
636884130193STheodore Ts'o EXT4_C2B(sbi, cluster),
6369e29136f8STheodore Ts'o "Block already on to-be-freed list");
6370cca41553SChunguang Xu kmem_cache_free(ext4_free_data_cachep, new_entry);
637185b67ffbSKemeng Shi return;
6372c9de560dSAlex Tomas }
6373c9de560dSAlex Tomas }
6374c9de560dSAlex Tomas
6375c894058dSAneesh Kumar K.V rb_link_node(new_node, parent, n);
6376c894058dSAneesh Kumar K.V rb_insert_color(new_node, &db->bb_free_root);
6377c894058dSAneesh Kumar K.V
6378c894058dSAneesh Kumar K.V /* Now try to see the extent can be merged to left and right */
6379c894058dSAneesh Kumar K.V node = rb_prev(new_node);
6380c894058dSAneesh Kumar K.V if (node) {
638118aadd47SBobi Jam entry = rb_entry(node, struct ext4_free_data, efd_node);
6382a0154344SDaeho Jeong ext4_try_merge_freed_extent(sbi, entry, new_entry,
6383a0154344SDaeho Jeong &(db->bb_free_root));
6384c9de560dSAlex Tomas }
6385c894058dSAneesh Kumar K.V
6386c894058dSAneesh Kumar K.V node = rb_next(new_node);
6387c894058dSAneesh Kumar K.V if (node) {
638818aadd47SBobi Jam entry = rb_entry(node, struct ext4_free_data, efd_node);
6389a0154344SDaeho Jeong ext4_try_merge_freed_extent(sbi, entry, new_entry,
6390a0154344SDaeho Jeong &(db->bb_free_root));
6391c894058dSAneesh Kumar K.V }
6392a0154344SDaeho Jeong
6393d08854f5STheodore Ts'o spin_lock(&sbi->s_md_lock);
6394a0154344SDaeho Jeong list_add_tail(&new_entry->efd_list, &sbi->s_freed_data_list);
6395d08854f5STheodore Ts'o sbi->s_mb_free_pending += clusters;
6396d08854f5STheodore Ts'o spin_unlock(&sbi->s_md_lock);
6397c9de560dSAlex Tomas }
6398c9de560dSAlex Tomas
ext4_free_blocks_simple(struct inode * inode,ext4_fsblk_t block,unsigned long count)63998016e29fSHarshad Shirwadkar static void ext4_free_blocks_simple(struct inode *inode, ext4_fsblk_t block,
64008016e29fSHarshad Shirwadkar unsigned long count)
64018016e29fSHarshad Shirwadkar {
64028016e29fSHarshad Shirwadkar struct buffer_head *bitmap_bh;
64038016e29fSHarshad Shirwadkar struct super_block *sb = inode->i_sb;
64048016e29fSHarshad Shirwadkar struct ext4_group_desc *gdp;
64058016e29fSHarshad Shirwadkar struct buffer_head *gdp_bh;
64068016e29fSHarshad Shirwadkar ext4_group_t group;
64078016e29fSHarshad Shirwadkar ext4_grpblk_t blkoff;
64088016e29fSHarshad Shirwadkar int already_freed = 0, err, i;
64098016e29fSHarshad Shirwadkar
64108016e29fSHarshad Shirwadkar ext4_get_group_no_and_offset(sb, block, &group, &blkoff);
64118016e29fSHarshad Shirwadkar bitmap_bh = ext4_read_block_bitmap(sb, group);
64128016e29fSHarshad Shirwadkar if (IS_ERR(bitmap_bh)) {
64138016e29fSHarshad Shirwadkar pr_warn("Failed to read block bitmap\n");
64148016e29fSHarshad Shirwadkar return;
64158016e29fSHarshad Shirwadkar }
64168016e29fSHarshad Shirwadkar gdp = ext4_get_group_desc(sb, group, &gdp_bh);
64178016e29fSHarshad Shirwadkar if (!gdp)
64181b5c9d34SKemeng Shi goto err_out;
64198016e29fSHarshad Shirwadkar
64208016e29fSHarshad Shirwadkar for (i = 0; i < count; i++) {
64218016e29fSHarshad Shirwadkar if (!mb_test_bit(blkoff + i, bitmap_bh->b_data))
64228016e29fSHarshad Shirwadkar already_freed++;
64238016e29fSHarshad Shirwadkar }
64248016e29fSHarshad Shirwadkar mb_clear_bits(bitmap_bh->b_data, blkoff, count);
64258016e29fSHarshad Shirwadkar err = ext4_handle_dirty_metadata(NULL, NULL, bitmap_bh);
64268016e29fSHarshad Shirwadkar if (err)
64271b5c9d34SKemeng Shi goto err_out;
64288016e29fSHarshad Shirwadkar ext4_free_group_clusters_set(
64298016e29fSHarshad Shirwadkar sb, gdp, ext4_free_group_clusters(sb, gdp) +
64308016e29fSHarshad Shirwadkar count - already_freed);
64311df9bde4SKemeng Shi ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
64328016e29fSHarshad Shirwadkar ext4_group_desc_csum_set(sb, group, gdp);
64338016e29fSHarshad Shirwadkar ext4_handle_dirty_metadata(NULL, NULL, gdp_bh);
64348016e29fSHarshad Shirwadkar sync_dirty_buffer(bitmap_bh);
64358016e29fSHarshad Shirwadkar sync_dirty_buffer(gdp_bh);
64361b5c9d34SKemeng Shi
64371b5c9d34SKemeng Shi err_out:
64388016e29fSHarshad Shirwadkar brelse(bitmap_bh);
64398016e29fSHarshad Shirwadkar }
64408016e29fSHarshad Shirwadkar
644144338711STheodore Ts'o /**
64428ac3939dSRitesh Harjani * ext4_mb_clear_bb() -- helper function for freeing blocks.
64438ac3939dSRitesh Harjani * Used by ext4_free_blocks()
644444338711STheodore Ts'o * @handle: handle for this transaction
644544338711STheodore Ts'o * @inode: inode
6446c60990b3STheodore Ts'o * @block: starting physical block to be freed
6447c60990b3STheodore Ts'o * @count: number of blocks to be freed
64485def1360SYongqiang Yang * @flags: flags used by ext4_free_blocks
6449c9de560dSAlex Tomas */
ext4_mb_clear_bb(handle_t * handle,struct inode * inode,ext4_fsblk_t block,unsigned long count,int flags)64508ac3939dSRitesh Harjani static void ext4_mb_clear_bb(handle_t *handle, struct inode *inode,
64518ac3939dSRitesh Harjani ext4_fsblk_t block, unsigned long count,
64528ac3939dSRitesh Harjani int flags)
6453c9de560dSAlex Tomas {
645426346ff6SAneesh Kumar K.V struct buffer_head *bitmap_bh = NULL;
6455c9de560dSAlex Tomas struct super_block *sb = inode->i_sb;
6456c9de560dSAlex Tomas struct ext4_group_desc *gdp;
64575354b2afSTheodore Ts'o struct ext4_group_info *grp;
6458498e5f24STheodore Ts'o unsigned int overflow;
6459c9de560dSAlex Tomas ext4_grpblk_t bit;
6460c9de560dSAlex Tomas struct buffer_head *gd_bh;
6461c9de560dSAlex Tomas ext4_group_t block_group;
6462c9de560dSAlex Tomas struct ext4_sb_info *sbi;
6463c9de560dSAlex Tomas struct ext4_buddy e4b;
646484130193STheodore Ts'o unsigned int count_clusters;
6465c9de560dSAlex Tomas int err = 0;
6466c9de560dSAlex Tomas int ret;
6467c9de560dSAlex Tomas
64688016e29fSHarshad Shirwadkar sbi = EXT4_SB(sb);
64698016e29fSHarshad Shirwadkar
64701e1c2b86SLukas Czerner if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
64711e1c2b86SLukas Czerner !ext4_inode_block_valid(inode, block, count)) {
64721e1c2b86SLukas Czerner ext4_error(sb, "Freeing blocks in system zone - "
64731e1c2b86SLukas Czerner "Block = %llu, count = %lu", block, count);
64741e1c2b86SLukas Czerner /* err = 0. ext4_std_error should be a no op */
64751e1c2b86SLukas Czerner goto error_return;
64761e1c2b86SLukas Czerner }
64771e1c2b86SLukas Czerner flags |= EXT4_FREE_BLOCKS_VALIDATED;
64781e1c2b86SLukas Czerner
6479c9de560dSAlex Tomas do_more:
6480c9de560dSAlex Tomas overflow = 0;
6481c9de560dSAlex Tomas ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
6482c9de560dSAlex Tomas
64835354b2afSTheodore Ts'o grp = ext4_get_group_info(sb, block_group);
64845354b2afSTheodore Ts'o if (unlikely(!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
6485163a203dSDarrick J. Wong return;
6486163a203dSDarrick J. Wong
6487c9de560dSAlex Tomas /*
6488c9de560dSAlex Tomas * Check to see if we are freeing blocks across a group
6489c9de560dSAlex Tomas * boundary.
6490c9de560dSAlex Tomas */
649184130193STheodore Ts'o if (EXT4_C2B(sbi, bit) + count > EXT4_BLOCKS_PER_GROUP(sb)) {
649284130193STheodore Ts'o overflow = EXT4_C2B(sbi, bit) + count -
649384130193STheodore Ts'o EXT4_BLOCKS_PER_GROUP(sb);
6494c9de560dSAlex Tomas count -= overflow;
64951e1c2b86SLukas Czerner /* The range changed so it's no longer validated */
64961e1c2b86SLukas Czerner flags &= ~EXT4_FREE_BLOCKS_VALIDATED;
6497c9de560dSAlex Tomas }
6498810da240SLukas Czerner count_clusters = EXT4_NUM_B2C(sbi, count);
6499574ca174STheodore Ts'o bitmap_bh = ext4_read_block_bitmap(sb, block_group);
65009008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) {
65019008a58eSDarrick J. Wong err = PTR_ERR(bitmap_bh);
65029008a58eSDarrick J. Wong bitmap_bh = NULL;
6503c9de560dSAlex Tomas goto error_return;
6504ce89f46cSAneesh Kumar K.V }
6505c9de560dSAlex Tomas gdp = ext4_get_group_desc(sb, block_group, &gd_bh);
6506ce89f46cSAneesh Kumar K.V if (!gdp) {
6507ce89f46cSAneesh Kumar K.V err = -EIO;
6508c9de560dSAlex Tomas goto error_return;
6509ce89f46cSAneesh Kumar K.V }
6510c9de560dSAlex Tomas
65111e1c2b86SLukas Czerner if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
65121e1c2b86SLukas Czerner !ext4_inode_block_valid(inode, block, count)) {
651312062dddSEric Sandeen ext4_error(sb, "Freeing blocks in system zone - "
65140610b6e9STheodore Ts'o "Block = %llu, count = %lu", block, count);
6515519deca0SAneesh Kumar K.V /* err = 0. ext4_std_error should be a no op */
6516519deca0SAneesh Kumar K.V goto error_return;
6517c9de560dSAlex Tomas }
6518c9de560dSAlex Tomas
6519c9de560dSAlex Tomas BUFFER_TRACE(bitmap_bh, "getting write access");
6520188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bitmap_bh,
6521188c299eSJan Kara EXT4_JTR_NONE);
6522c9de560dSAlex Tomas if (err)
6523c9de560dSAlex Tomas goto error_return;
6524c9de560dSAlex Tomas
6525c9de560dSAlex Tomas /*
6526c9de560dSAlex Tomas * We are about to modify some metadata. Call the journal APIs
6527c9de560dSAlex Tomas * to unshare ->b_data if a currently-committing transaction is
6528c9de560dSAlex Tomas * using it
6529c9de560dSAlex Tomas */
6530c9de560dSAlex Tomas BUFFER_TRACE(gd_bh, "get_write_access");
6531188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gd_bh, EXT4_JTR_NONE);
6532c9de560dSAlex Tomas if (err)
6533c9de560dSAlex Tomas goto error_return;
6534c9de560dSAlex Tomas #ifdef AGGRESSIVE_CHECK
6535c9de560dSAlex Tomas {
6536c9de560dSAlex Tomas int i;
653784130193STheodore Ts'o for (i = 0; i < count_clusters; i++)
6538c9de560dSAlex Tomas BUG_ON(!mb_test_bit(bit + i, bitmap_bh->b_data));
6539c9de560dSAlex Tomas }
6540c9de560dSAlex Tomas #endif
654184130193STheodore Ts'o trace_ext4_mballoc_free(sb, inode, block_group, bit, count_clusters);
6542c9de560dSAlex Tomas
6543adb7ef60SKonstantin Khlebnikov /* __GFP_NOFAIL: retry infinitely, ignore TIF_MEMDIE and memcg limit. */
6544adb7ef60SKonstantin Khlebnikov err = ext4_mb_load_buddy_gfp(sb, block_group, &e4b,
6545adb7ef60SKonstantin Khlebnikov GFP_NOFS|__GFP_NOFAIL);
6546920313a7SAneesh Kumar K.V if (err)
6547920313a7SAneesh Kumar K.V goto error_return;
6548e6362609STheodore Ts'o
6549f96c450dSDaeho Jeong /*
6550f96c450dSDaeho Jeong * We need to make sure we don't reuse the freed block until after the
6551f96c450dSDaeho Jeong * transaction is committed. We make an exception if the inode is to be
6552f96c450dSDaeho Jeong * written in writeback mode since writeback mode has weak data
6553f96c450dSDaeho Jeong * consistency guarantees.
6554f96c450dSDaeho Jeong */
6555f96c450dSDaeho Jeong if (ext4_handle_valid(handle) &&
6556f96c450dSDaeho Jeong ((flags & EXT4_FREE_BLOCKS_METADATA) ||
6557f96c450dSDaeho Jeong !ext4_should_writeback_data(inode))) {
65587a2fcbf7SAneesh Kumar K.V struct ext4_free_data *new_entry;
65597a2fcbf7SAneesh Kumar K.V /*
65607444a072SMichal Hocko * We use __GFP_NOFAIL because ext4_free_blocks() is not allowed
65617444a072SMichal Hocko * to fail.
65627a2fcbf7SAneesh Kumar K.V */
65637444a072SMichal Hocko new_entry = kmem_cache_alloc(ext4_free_data_cachep,
65647444a072SMichal Hocko GFP_NOFS|__GFP_NOFAIL);
656518aadd47SBobi Jam new_entry->efd_start_cluster = bit;
656618aadd47SBobi Jam new_entry->efd_group = block_group;
656718aadd47SBobi Jam new_entry->efd_count = count_clusters;
656818aadd47SBobi Jam new_entry->efd_tid = handle->h_transaction->t_tid;
6569955ce5f5SAneesh Kumar K.V
65707a2fcbf7SAneesh Kumar K.V ext4_lock_group(sb, block_group);
657184130193STheodore Ts'o mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
65727a2fcbf7SAneesh Kumar K.V ext4_mb_free_metadata(handle, &e4b, new_entry);
6573c9de560dSAlex Tomas } else {
65747a2fcbf7SAneesh Kumar K.V /* need to update group_info->bb_free and bitmap
65757a2fcbf7SAneesh Kumar K.V * with group lock held. generate_buddy look at
65767a2fcbf7SAneesh Kumar K.V * them with group lock_held
65777a2fcbf7SAneesh Kumar K.V */
6578d71c1ae2SLukas Czerner if (test_opt(sb, DISCARD)) {
6579247c3d21SKemeng Shi err = ext4_issue_discard(sb, block_group, bit,
6580247c3d21SKemeng Shi count_clusters, NULL);
6581d71c1ae2SLukas Czerner if (err && err != -EOPNOTSUPP)
6582d71c1ae2SLukas Czerner ext4_msg(sb, KERN_WARNING, "discard request in"
6583a00b482bSRitesh Harjani " group:%u block:%d count:%lu failed"
6584d71c1ae2SLukas Czerner " with %d", block_group, bit, count,
6585d71c1ae2SLukas Czerner err);
6586e4006410Syangerkun }
6587e4006410Syangerkun
65888f9ff189SLukas Czerner EXT4_MB_GRP_CLEAR_TRIMMED(e4b.bd_info);
6589d71c1ae2SLukas Czerner
6590955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, block_group);
659184130193STheodore Ts'o mb_clear_bits(bitmap_bh->b_data, bit, count_clusters);
659284130193STheodore Ts'o mb_free_blocks(inode, &e4b, bit, count_clusters);
6593c9de560dSAlex Tomas }
6594c9de560dSAlex Tomas
6595021b65bbSTheodore Ts'o ret = ext4_free_group_clusters(sb, gdp) + count_clusters;
6596021b65bbSTheodore Ts'o ext4_free_group_clusters_set(sb, gdp, ret);
65971df9bde4SKemeng Shi ext4_block_bitmap_csum_set(sb, gdp, bitmap_bh);
6598feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, gdp);
6599955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, block_group);
6600c9de560dSAlex Tomas
6601772cb7c8SJose R. Santos if (sbi->s_log_groups_per_flex) {
6602772cb7c8SJose R. Santos ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
660390ba983fSTheodore Ts'o atomic64_add(count_clusters,
66047c990728SSuraj Jitindar Singh &sbi_array_rcu_deref(sbi, s_flex_groups,
66057c990728SSuraj Jitindar Singh flex_group)->free_clusters);
6606772cb7c8SJose R. Santos }
6607772cb7c8SJose R. Santos
66089fe67149SEric Whitney /*
66099fe67149SEric Whitney * on a bigalloc file system, defer the s_freeclusters_counter
66109fe67149SEric Whitney * update to the caller (ext4_remove_space and friends) so they
66119fe67149SEric Whitney * can determine if a cluster freed here should be rereserved
66129fe67149SEric Whitney */
66139fe67149SEric Whitney if (!(flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)) {
66147b415bf6SAditya Kali if (!(flags & EXT4_FREE_BLOCKS_NO_QUOT_UPDATE))
66157b415bf6SAditya Kali dquot_free_block(inode, EXT4_C2B(sbi, count_clusters));
66169fe67149SEric Whitney percpu_counter_add(&sbi->s_freeclusters_counter,
66179fe67149SEric Whitney count_clusters);
66189fe67149SEric Whitney }
66197d734532SJan Kara
66207d734532SJan Kara ext4_mb_unload_buddy(&e4b);
66217b415bf6SAditya Kali
66227a2fcbf7SAneesh Kumar K.V /* We dirtied the bitmap block */
66237a2fcbf7SAneesh Kumar K.V BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
66247a2fcbf7SAneesh Kumar K.V err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
66257a2fcbf7SAneesh Kumar K.V
6626c9de560dSAlex Tomas /* And the group descriptor block */
6627c9de560dSAlex Tomas BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
66280390131bSFrank Mayhar ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
6629c9de560dSAlex Tomas if (!err)
6630c9de560dSAlex Tomas err = ret;
6631c9de560dSAlex Tomas
6632c9de560dSAlex Tomas if (overflow && !err) {
6633c9de560dSAlex Tomas block += count;
6634c9de560dSAlex Tomas count = overflow;
6635c9de560dSAlex Tomas put_bh(bitmap_bh);
66361e1c2b86SLukas Czerner /* The range changed so it's no longer validated */
66371e1c2b86SLukas Czerner flags &= ~EXT4_FREE_BLOCKS_VALIDATED;
6638c9de560dSAlex Tomas goto do_more;
6639c9de560dSAlex Tomas }
6640c9de560dSAlex Tomas error_return:
6641c9de560dSAlex Tomas brelse(bitmap_bh);
6642c9de560dSAlex Tomas ext4_std_error(sb, err);
6643c9de560dSAlex Tomas }
66447360d173SLukas Czerner
66457360d173SLukas Czerner /**
66468ac3939dSRitesh Harjani * ext4_free_blocks() -- Free given blocks and update quota
66478ac3939dSRitesh Harjani * @handle: handle for this transaction
66488ac3939dSRitesh Harjani * @inode: inode
66498ac3939dSRitesh Harjani * @bh: optional buffer of the block to be freed
66508ac3939dSRitesh Harjani * @block: starting physical block to be freed
66518ac3939dSRitesh Harjani * @count: number of blocks to be freed
66528ac3939dSRitesh Harjani * @flags: flags used by ext4_free_blocks
66538ac3939dSRitesh Harjani */
ext4_free_blocks(handle_t * handle,struct inode * inode,struct buffer_head * bh,ext4_fsblk_t block,unsigned long count,int flags)66548ac3939dSRitesh Harjani void ext4_free_blocks(handle_t *handle, struct inode *inode,
66558ac3939dSRitesh Harjani struct buffer_head *bh, ext4_fsblk_t block,
66568ac3939dSRitesh Harjani unsigned long count, int flags)
66578ac3939dSRitesh Harjani {
66588ac3939dSRitesh Harjani struct super_block *sb = inode->i_sb;
66598ac3939dSRitesh Harjani unsigned int overflow;
66608ac3939dSRitesh Harjani struct ext4_sb_info *sbi;
66618ac3939dSRitesh Harjani
66628ac3939dSRitesh Harjani sbi = EXT4_SB(sb);
66638ac3939dSRitesh Harjani
66648ac3939dSRitesh Harjani if (bh) {
66658ac3939dSRitesh Harjani if (block)
66668ac3939dSRitesh Harjani BUG_ON(block != bh->b_blocknr);
66678ac3939dSRitesh Harjani else
66688ac3939dSRitesh Harjani block = bh->b_blocknr;
66698ac3939dSRitesh Harjani }
66708ac3939dSRitesh Harjani
667111b6890bSKemeng Shi if (sbi->s_mount_state & EXT4_FC_REPLAY) {
66722ec6d0a5SKemeng Shi ext4_free_blocks_simple(inode, block, EXT4_NUM_B2C(sbi, count));
667311b6890bSKemeng Shi return;
667411b6890bSKemeng Shi }
667511b6890bSKemeng Shi
667611b6890bSKemeng Shi might_sleep();
667711b6890bSKemeng Shi
66788ac3939dSRitesh Harjani if (!(flags & EXT4_FREE_BLOCKS_VALIDATED) &&
66798ac3939dSRitesh Harjani !ext4_inode_block_valid(inode, block, count)) {
66808ac3939dSRitesh Harjani ext4_error(sb, "Freeing blocks not in datazone - "
66818ac3939dSRitesh Harjani "block = %llu, count = %lu", block, count);
66828ac3939dSRitesh Harjani return;
66838ac3939dSRitesh Harjani }
66841e1c2b86SLukas Czerner flags |= EXT4_FREE_BLOCKS_VALIDATED;
66858ac3939dSRitesh Harjani
66868ac3939dSRitesh Harjani ext4_debug("freeing block %llu\n", block);
66878ac3939dSRitesh Harjani trace_ext4_free_blocks(inode, block, count, flags);
66888ac3939dSRitesh Harjani
66898ac3939dSRitesh Harjani if (bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
66908ac3939dSRitesh Harjani BUG_ON(count > 1);
66918ac3939dSRitesh Harjani
66928ac3939dSRitesh Harjani ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
66938ac3939dSRitesh Harjani inode, bh, block);
66948ac3939dSRitesh Harjani }
66958ac3939dSRitesh Harjani
66968ac3939dSRitesh Harjani /*
66978ac3939dSRitesh Harjani * If the extent to be freed does not begin on a cluster
66988ac3939dSRitesh Harjani * boundary, we need to deal with partial clusters at the
66998ac3939dSRitesh Harjani * beginning and end of the extent. Normally we will free
67008ac3939dSRitesh Harjani * blocks at the beginning or the end unless we are explicitly
67018ac3939dSRitesh Harjani * requested to avoid doing so.
67028ac3939dSRitesh Harjani */
67038ac3939dSRitesh Harjani overflow = EXT4_PBLK_COFF(sbi, block);
67048ac3939dSRitesh Harjani if (overflow) {
67058ac3939dSRitesh Harjani if (flags & EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER) {
67068ac3939dSRitesh Harjani overflow = sbi->s_cluster_ratio - overflow;
67078ac3939dSRitesh Harjani block += overflow;
67088ac3939dSRitesh Harjani if (count > overflow)
67098ac3939dSRitesh Harjani count -= overflow;
67108ac3939dSRitesh Harjani else
67118ac3939dSRitesh Harjani return;
67128ac3939dSRitesh Harjani } else {
67138ac3939dSRitesh Harjani block -= overflow;
67148ac3939dSRitesh Harjani count += overflow;
67158ac3939dSRitesh Harjani }
67161e1c2b86SLukas Czerner /* The range changed so it's no longer validated */
67171e1c2b86SLukas Czerner flags &= ~EXT4_FREE_BLOCKS_VALIDATED;
67188ac3939dSRitesh Harjani }
67198ac3939dSRitesh Harjani overflow = EXT4_LBLK_COFF(sbi, count);
67208ac3939dSRitesh Harjani if (overflow) {
67218ac3939dSRitesh Harjani if (flags & EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER) {
67228ac3939dSRitesh Harjani if (count > overflow)
67238ac3939dSRitesh Harjani count -= overflow;
67248ac3939dSRitesh Harjani else
67258ac3939dSRitesh Harjani return;
67268ac3939dSRitesh Harjani } else
67278ac3939dSRitesh Harjani count += sbi->s_cluster_ratio - overflow;
67281e1c2b86SLukas Czerner /* The range changed so it's no longer validated */
67291e1c2b86SLukas Czerner flags &= ~EXT4_FREE_BLOCKS_VALIDATED;
67308ac3939dSRitesh Harjani }
67318ac3939dSRitesh Harjani
67328ac3939dSRitesh Harjani if (!bh && (flags & EXT4_FREE_BLOCKS_FORGET)) {
67338ac3939dSRitesh Harjani int i;
67348ac3939dSRitesh Harjani int is_metadata = flags & EXT4_FREE_BLOCKS_METADATA;
67358ac3939dSRitesh Harjani
67368ac3939dSRitesh Harjani for (i = 0; i < count; i++) {
67378ac3939dSRitesh Harjani cond_resched();
67388ac3939dSRitesh Harjani if (is_metadata)
67398ac3939dSRitesh Harjani bh = sb_find_get_block(inode->i_sb, block + i);
67408ac3939dSRitesh Harjani ext4_forget(handle, is_metadata, inode, bh, block + i);
67418ac3939dSRitesh Harjani }
67428ac3939dSRitesh Harjani }
67438ac3939dSRitesh Harjani
67448ac3939dSRitesh Harjani ext4_mb_clear_bb(handle, inode, block, count, flags);
67458ac3939dSRitesh Harjani }
67468ac3939dSRitesh Harjani
67478ac3939dSRitesh Harjani /**
67480529155eSYongqiang Yang * ext4_group_add_blocks() -- Add given blocks to an existing group
67492846e820SAmir Goldstein * @handle: handle to this transaction
67502846e820SAmir Goldstein * @sb: super block
67514907cb7bSAnatol Pomozov * @block: start physical block to add to the block group
67522846e820SAmir Goldstein * @count: number of blocks to free
67532846e820SAmir Goldstein *
6754e73a347bSAmir Goldstein * This marks the blocks as free in the bitmap and buddy.
67552846e820SAmir Goldstein */
ext4_group_add_blocks(handle_t * handle,struct super_block * sb,ext4_fsblk_t block,unsigned long count)6756cc7365dfSYongqiang Yang int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
67572846e820SAmir Goldstein ext4_fsblk_t block, unsigned long count)
67582846e820SAmir Goldstein {
67592846e820SAmir Goldstein struct buffer_head *bitmap_bh = NULL;
67602846e820SAmir Goldstein struct buffer_head *gd_bh;
67612846e820SAmir Goldstein ext4_group_t block_group;
67622846e820SAmir Goldstein ext4_grpblk_t bit;
67632846e820SAmir Goldstein unsigned int i;
67642846e820SAmir Goldstein struct ext4_group_desc *desc;
67652846e820SAmir Goldstein struct ext4_sb_info *sbi = EXT4_SB(sb);
6766e73a347bSAmir Goldstein struct ext4_buddy e4b;
6767d77147ffSharshads int err = 0, ret, free_clusters_count;
6768d77147ffSharshads ext4_grpblk_t clusters_freed;
6769d77147ffSharshads ext4_fsblk_t first_cluster = EXT4_B2C(sbi, block);
6770d77147ffSharshads ext4_fsblk_t last_cluster = EXT4_B2C(sbi, block + count - 1);
6771d77147ffSharshads unsigned long cluster_count = last_cluster - first_cluster + 1;
67722846e820SAmir Goldstein
67732846e820SAmir Goldstein ext4_debug("Adding block(s) %llu-%llu\n", block, block + count - 1);
67742846e820SAmir Goldstein
67754740b830SYongqiang Yang if (count == 0)
67764740b830SYongqiang Yang return 0;
67774740b830SYongqiang Yang
67782846e820SAmir Goldstein ext4_get_group_no_and_offset(sb, block, &block_group, &bit);
67792846e820SAmir Goldstein /*
67802846e820SAmir Goldstein * Check to see if we are freeing blocks across a group
67812846e820SAmir Goldstein * boundary.
67822846e820SAmir Goldstein */
6783d77147ffSharshads if (bit + cluster_count > EXT4_CLUSTERS_PER_GROUP(sb)) {
6784d77147ffSharshads ext4_warning(sb, "too many blocks added to group %u",
6785cc7365dfSYongqiang Yang block_group);
6786cc7365dfSYongqiang Yang err = -EINVAL;
67872846e820SAmir Goldstein goto error_return;
6788cc7365dfSYongqiang Yang }
67892cd05cc3STheodore Ts'o
67902846e820SAmir Goldstein bitmap_bh = ext4_read_block_bitmap(sb, block_group);
67919008a58eSDarrick J. Wong if (IS_ERR(bitmap_bh)) {
67929008a58eSDarrick J. Wong err = PTR_ERR(bitmap_bh);
67939008a58eSDarrick J. Wong bitmap_bh = NULL;
67942846e820SAmir Goldstein goto error_return;
6795cc7365dfSYongqiang Yang }
6796cc7365dfSYongqiang Yang
67972846e820SAmir Goldstein desc = ext4_get_group_desc(sb, block_group, &gd_bh);
6798cc7365dfSYongqiang Yang if (!desc) {
6799cc7365dfSYongqiang Yang err = -EIO;
68002846e820SAmir Goldstein goto error_return;
6801cc7365dfSYongqiang Yang }
68022846e820SAmir Goldstein
6803a00b482bSRitesh Harjani if (!ext4_sb_block_valid(sb, NULL, block, count)) {
68042846e820SAmir Goldstein ext4_error(sb, "Adding blocks in system zones - "
68052846e820SAmir Goldstein "Block = %llu, count = %lu",
68062846e820SAmir Goldstein block, count);
6807cc7365dfSYongqiang Yang err = -EINVAL;
68082846e820SAmir Goldstein goto error_return;
68092846e820SAmir Goldstein }
68102846e820SAmir Goldstein
68112cd05cc3STheodore Ts'o BUFFER_TRACE(bitmap_bh, "getting write access");
6812188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, bitmap_bh,
6813188c299eSJan Kara EXT4_JTR_NONE);
68142846e820SAmir Goldstein if (err)
68152846e820SAmir Goldstein goto error_return;
68162846e820SAmir Goldstein
68172846e820SAmir Goldstein /*
68182846e820SAmir Goldstein * We are about to modify some metadata. Call the journal APIs
68192846e820SAmir Goldstein * to unshare ->b_data if a currently-committing transaction is
68202846e820SAmir Goldstein * using it
68212846e820SAmir Goldstein */
68222846e820SAmir Goldstein BUFFER_TRACE(gd_bh, "get_write_access");
6823188c299eSJan Kara err = ext4_journal_get_write_access(handle, sb, gd_bh, EXT4_JTR_NONE);
68242846e820SAmir Goldstein if (err)
68252846e820SAmir Goldstein goto error_return;
6826e73a347bSAmir Goldstein
6827d77147ffSharshads for (i = 0, clusters_freed = 0; i < cluster_count; i++) {
68282846e820SAmir Goldstein BUFFER_TRACE(bitmap_bh, "clear bit");
6829e73a347bSAmir Goldstein if (!mb_test_bit(bit + i, bitmap_bh->b_data)) {
68302846e820SAmir Goldstein ext4_error(sb, "bit already cleared for block %llu",
68312846e820SAmir Goldstein (ext4_fsblk_t)(block + i));
68322846e820SAmir Goldstein BUFFER_TRACE(bitmap_bh, "bit already cleared");
68332846e820SAmir Goldstein } else {
6834d77147ffSharshads clusters_freed++;
68352846e820SAmir Goldstein }
68362846e820SAmir Goldstein }
6837e73a347bSAmir Goldstein
6838e73a347bSAmir Goldstein err = ext4_mb_load_buddy(sb, block_group, &e4b);
6839e73a347bSAmir Goldstein if (err)
6840e73a347bSAmir Goldstein goto error_return;
6841e73a347bSAmir Goldstein
6842e73a347bSAmir Goldstein /*
6843e73a347bSAmir Goldstein * need to update group_info->bb_free and bitmap
6844e73a347bSAmir Goldstein * with group lock held. generate_buddy look at
6845e73a347bSAmir Goldstein * them with group lock_held
6846e73a347bSAmir Goldstein */
68472846e820SAmir Goldstein ext4_lock_group(sb, block_group);
6848d77147ffSharshads mb_clear_bits(bitmap_bh->b_data, bit, cluster_count);
6849d77147ffSharshads mb_free_blocks(NULL, &e4b, bit, cluster_count);
6850d77147ffSharshads free_clusters_count = clusters_freed +
6851d77147ffSharshads ext4_free_group_clusters(sb, desc);
6852d77147ffSharshads ext4_free_group_clusters_set(sb, desc, free_clusters_count);
68531df9bde4SKemeng Shi ext4_block_bitmap_csum_set(sb, desc, bitmap_bh);
6854feb0ab32SDarrick J. Wong ext4_group_desc_csum_set(sb, block_group, desc);
68552846e820SAmir Goldstein ext4_unlock_group(sb, block_group);
685657042651STheodore Ts'o percpu_counter_add(&sbi->s_freeclusters_counter,
6857d77147ffSharshads clusters_freed);
68582846e820SAmir Goldstein
68592846e820SAmir Goldstein if (sbi->s_log_groups_per_flex) {
68602846e820SAmir Goldstein ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
6861d77147ffSharshads atomic64_add(clusters_freed,
68627c990728SSuraj Jitindar Singh &sbi_array_rcu_deref(sbi, s_flex_groups,
68637c990728SSuraj Jitindar Singh flex_group)->free_clusters);
68642846e820SAmir Goldstein }
6865e73a347bSAmir Goldstein
6866e73a347bSAmir Goldstein ext4_mb_unload_buddy(&e4b);
68672846e820SAmir Goldstein
68682846e820SAmir Goldstein /* We dirtied the bitmap block */
68692846e820SAmir Goldstein BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
68702846e820SAmir Goldstein err = ext4_handle_dirty_metadata(handle, NULL, bitmap_bh);
68712846e820SAmir Goldstein
68722846e820SAmir Goldstein /* And the group descriptor block */
68732846e820SAmir Goldstein BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
68742846e820SAmir Goldstein ret = ext4_handle_dirty_metadata(handle, NULL, gd_bh);
68752846e820SAmir Goldstein if (!err)
68762846e820SAmir Goldstein err = ret;
68772846e820SAmir Goldstein
68782846e820SAmir Goldstein error_return:
68792846e820SAmir Goldstein brelse(bitmap_bh);
68802846e820SAmir Goldstein ext4_std_error(sb, err);
6881cc7365dfSYongqiang Yang return err;
68822846e820SAmir Goldstein }
68832846e820SAmir Goldstein
68842846e820SAmir Goldstein /**
68857360d173SLukas Czerner * ext4_trim_extent -- function to TRIM one single free extent in the group
68867360d173SLukas Czerner * @sb: super block for the file system
68877360d173SLukas Czerner * @start: starting block of the free extent in the alloc. group
68887360d173SLukas Czerner * @count: number of blocks to TRIM
68897360d173SLukas Czerner * @e4b: ext4 buddy for the group
68907360d173SLukas Czerner *
68917360d173SLukas Czerner * Trim "count" blocks starting at "start" in the "group". To assure that no
68927360d173SLukas Czerner * one will allocate those blocks, mark it as used in buddy bitmap. This must
68937360d173SLukas Czerner * be called with under the group lock.
68947360d173SLukas Czerner */
ext4_trim_extent(struct super_block * sb,int start,int count,struct ext4_buddy * e4b)6895bd2eea8dSWang Jianchao static int ext4_trim_extent(struct super_block *sb,
6896bd2eea8dSWang Jianchao int start, int count, struct ext4_buddy *e4b)
6897e2cbd587Sjon ernst __releases(bitlock)
6898e2cbd587Sjon ernst __acquires(bitlock)
68997360d173SLukas Czerner {
69007360d173SLukas Czerner struct ext4_free_extent ex;
6901bd2eea8dSWang Jianchao ext4_group_t group = e4b->bd_group;
6902d71c1ae2SLukas Czerner int ret = 0;
69037360d173SLukas Czerner
6904b3d4c2b1STao Ma trace_ext4_trim_extent(sb, group, start, count);
6905b3d4c2b1STao Ma
69067360d173SLukas Czerner assert_spin_locked(ext4_group_lock_ptr(sb, group));
69077360d173SLukas Czerner
69087360d173SLukas Czerner ex.fe_start = start;
69097360d173SLukas Czerner ex.fe_group = group;
69107360d173SLukas Czerner ex.fe_len = count;
69117360d173SLukas Czerner
69127360d173SLukas Czerner /*
69137360d173SLukas Czerner * Mark blocks used, so no one can reuse them while
69147360d173SLukas Czerner * being trimmed.
69157360d173SLukas Czerner */
69167360d173SLukas Czerner mb_mark_used(e4b, &ex);
69177360d173SLukas Czerner ext4_unlock_group(sb, group);
6918a0154344SDaeho Jeong ret = ext4_issue_discard(sb, group, start, count, NULL);
69197360d173SLukas Czerner ext4_lock_group(sb, group);
69207360d173SLukas Czerner mb_free_blocks(NULL, e4b, start, ex.fe_len);
6921d71c1ae2SLukas Czerner return ret;
69227360d173SLukas Czerner }
69237360d173SLukas Czerner
ext4_last_grp_cluster(struct super_block * sb,ext4_group_t grp)692445e4ab32SJan Kara static ext4_grpblk_t ext4_last_grp_cluster(struct super_block *sb,
692545e4ab32SJan Kara ext4_group_t grp)
692645e4ab32SJan Kara {
6927da9008daSSuraj Jitindar Singh unsigned long nr_clusters_in_group;
6928da9008daSSuraj Jitindar Singh
6929da9008daSSuraj Jitindar Singh if (grp < (ext4_get_groups_count(sb) - 1))
6930da9008daSSuraj Jitindar Singh nr_clusters_in_group = EXT4_CLUSTERS_PER_GROUP(sb);
6931da9008daSSuraj Jitindar Singh else
6932da9008daSSuraj Jitindar Singh nr_clusters_in_group = (ext4_blocks_count(EXT4_SB(sb)->s_es) -
6933da9008daSSuraj Jitindar Singh ext4_group_first_block_no(sb, grp))
6934da9008daSSuraj Jitindar Singh >> EXT4_CLUSTER_BITS(sb);
6935da9008daSSuraj Jitindar Singh
6936da9008daSSuraj Jitindar Singh return nr_clusters_in_group - 1;
693745e4ab32SJan Kara }
693845e4ab32SJan Kara
ext4_trim_interrupted(void)69395229a658SJan Kara static bool ext4_trim_interrupted(void)
69405229a658SJan Kara {
69415229a658SJan Kara return fatal_signal_pending(current) || freezing(current);
69425229a658SJan Kara }
69435229a658SJan Kara
ext4_try_to_trim_range(struct super_block * sb,struct ext4_buddy * e4b,ext4_grpblk_t start,ext4_grpblk_t max,ext4_grpblk_t minblocks)69446920b391SWang Jianchao static int ext4_try_to_trim_range(struct super_block *sb,
69456920b391SWang Jianchao struct ext4_buddy *e4b, ext4_grpblk_t start,
69466920b391SWang Jianchao ext4_grpblk_t max, ext4_grpblk_t minblocks)
6947a5fda113STheodore Ts'o __acquires(ext4_group_lock_ptr(sb, e4b->bd_group))
6948a5fda113STheodore Ts'o __releases(ext4_group_lock_ptr(sb, e4b->bd_group))
69496920b391SWang Jianchao {
6950c1eacba3SYe Bin ext4_grpblk_t next, count, free_count, last, origin_start;
695145e4ab32SJan Kara bool set_trimmed = false;
69526920b391SWang Jianchao void *bitmap;
69536920b391SWang Jianchao
69540a56dcceSBaokun Li if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(e4b->bd_info)))
69550a56dcceSBaokun Li return 0;
69560a56dcceSBaokun Li
6957c1eacba3SYe Bin last = ext4_last_grp_cluster(sb, e4b->bd_group);
69586920b391SWang Jianchao bitmap = e4b->bd_bitmap;
6959c1eacba3SYe Bin if (start == 0 && max >= last)
696045e4ab32SJan Kara set_trimmed = true;
6961c1eacba3SYe Bin origin_start = start;
6962de8bf0e5SKemeng Shi start = max(e4b->bd_info->bb_first_free, start);
69636920b391SWang Jianchao count = 0;
69646920b391SWang Jianchao free_count = 0;
69656920b391SWang Jianchao
69666920b391SWang Jianchao while (start <= max) {
69676920b391SWang Jianchao start = mb_find_next_zero_bit(bitmap, max + 1, start);
69686920b391SWang Jianchao if (start > max)
69696920b391SWang Jianchao break;
6970c1eacba3SYe Bin
6971c1eacba3SYe Bin next = mb_find_next_bit(bitmap, last + 1, start);
6972c1eacba3SYe Bin if (origin_start == 0 && next >= last)
6973c1eacba3SYe Bin set_trimmed = true;
69746920b391SWang Jianchao
69756920b391SWang Jianchao if ((next - start) >= minblocks) {
6976afcc4e32SLukas Bulwahn int ret = ext4_trim_extent(sb, start, next - start, e4b);
6977afcc4e32SLukas Bulwahn
69786920b391SWang Jianchao if (ret && ret != -EOPNOTSUPP)
697945e4ab32SJan Kara return count;
69806920b391SWang Jianchao count += next - start;
69816920b391SWang Jianchao }
69826920b391SWang Jianchao free_count += next - start;
69836920b391SWang Jianchao start = next + 1;
69846920b391SWang Jianchao
69855229a658SJan Kara if (ext4_trim_interrupted())
69865229a658SJan Kara return count;
69876920b391SWang Jianchao
69886920b391SWang Jianchao if (need_resched()) {
69896920b391SWang Jianchao ext4_unlock_group(sb, e4b->bd_group);
69906920b391SWang Jianchao cond_resched();
69916920b391SWang Jianchao ext4_lock_group(sb, e4b->bd_group);
69926920b391SWang Jianchao }
69936920b391SWang Jianchao
69946920b391SWang Jianchao if ((e4b->bd_info->bb_free - free_count) < minblocks)
69956920b391SWang Jianchao break;
69966920b391SWang Jianchao }
69976920b391SWang Jianchao
699845e4ab32SJan Kara if (set_trimmed)
699945e4ab32SJan Kara EXT4_MB_GRP_SET_TRIMMED(e4b->bd_info);
700045e4ab32SJan Kara
70016920b391SWang Jianchao return count;
70026920b391SWang Jianchao }
70036920b391SWang Jianchao
70047360d173SLukas Czerner /**
70057360d173SLukas Czerner * ext4_trim_all_free -- function to trim all free space in alloc. group
70067360d173SLukas Czerner * @sb: super block for file system
700722612283STao Ma * @group: group to be trimmed
70087360d173SLukas Czerner * @start: first group block to examine
70097360d173SLukas Czerner * @max: last group block to examine
70107360d173SLukas Czerner * @minblocks: minimum extent block count
70117360d173SLukas Czerner *
70127360d173SLukas Czerner * ext4_trim_all_free walks through group's block bitmap searching for free
70137360d173SLukas Czerner * extents. When the free extent is found, mark it as used in group buddy
70147360d173SLukas Czerner * bitmap. Then issue a TRIM command on this extent and free the extent in
7015b6f5558cSWang Jianchao * the group buddy bitmap.
70167360d173SLukas Czerner */
70170b75a840SLukas Czerner static ext4_grpblk_t
ext4_trim_all_free(struct super_block * sb,ext4_group_t group,ext4_grpblk_t start,ext4_grpblk_t max,ext4_grpblk_t minblocks)701878944086SLukas Czerner ext4_trim_all_free(struct super_block *sb, ext4_group_t group,
701978944086SLukas Czerner ext4_grpblk_t start, ext4_grpblk_t max,
702045e4ab32SJan Kara ext4_grpblk_t minblocks)
70217360d173SLukas Czerner {
702278944086SLukas Czerner struct ext4_buddy e4b;
70236920b391SWang Jianchao int ret;
70247360d173SLukas Czerner
7025b3d4c2b1STao Ma trace_ext4_trim_all_free(sb, group, start, max);
7026b3d4c2b1STao Ma
702778944086SLukas Czerner ret = ext4_mb_load_buddy(sb, group, &e4b);
702878944086SLukas Czerner if (ret) {
70299651e6b2SKonstantin Khlebnikov ext4_warning(sb, "Error %d loading buddy information for %u",
70309651e6b2SKonstantin Khlebnikov ret, group);
703178944086SLukas Czerner return ret;
703278944086SLukas Czerner }
703328739eeaSLukas Czerner
703428739eeaSLukas Czerner ext4_lock_group(sb, group);
70353d56b8d2STao Ma
70366920b391SWang Jianchao if (!EXT4_MB_GRP_WAS_TRIMMED(e4b.bd_info) ||
703745e4ab32SJan Kara minblocks < EXT4_SB(sb)->s_last_trim_minblks)
70386920b391SWang Jianchao ret = ext4_try_to_trim_range(sb, &e4b, start, max, minblocks);
703945e4ab32SJan Kara else
70406920b391SWang Jianchao ret = 0;
70416920b391SWang Jianchao
70427360d173SLukas Czerner ext4_unlock_group(sb, group);
704378944086SLukas Czerner ext4_mb_unload_buddy(&e4b);
70447360d173SLukas Czerner
70457360d173SLukas Czerner ext4_debug("trimmed %d blocks in the group %d\n",
70466920b391SWang Jianchao ret, group);
70477360d173SLukas Czerner
7048d71c1ae2SLukas Czerner return ret;
70497360d173SLukas Czerner }
70507360d173SLukas Czerner
70517360d173SLukas Czerner /**
70527360d173SLukas Czerner * ext4_trim_fs() -- trim ioctl handle function
70537360d173SLukas Czerner * @sb: superblock for filesystem
70547360d173SLukas Czerner * @range: fstrim_range structure
70557360d173SLukas Czerner *
70567360d173SLukas Czerner * start: First Byte to trim
70577360d173SLukas Czerner * len: number of Bytes to trim from start
70587360d173SLukas Czerner * minlen: minimum extent length in Bytes
70597360d173SLukas Czerner * ext4_trim_fs goes through all allocation groups containing Bytes from
70607360d173SLukas Czerner * start to start+len. For each such a group ext4_trim_all_free function
70617360d173SLukas Czerner * is invoked to trim all free space.
70627360d173SLukas Czerner */
ext4_trim_fs(struct super_block * sb,struct fstrim_range * range)70637360d173SLukas Czerner int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
70647360d173SLukas Czerner {
70657b47ef52SChristoph Hellwig unsigned int discard_granularity = bdev_discard_granularity(sb->s_bdev);
706678944086SLukas Czerner struct ext4_group_info *grp;
7067913eed83SLukas Czerner ext4_group_t group, first_group, last_group;
70687137d7a4STheodore Ts'o ext4_grpblk_t cnt = 0, first_cluster, last_cluster;
7069913eed83SLukas Czerner uint64_t start, end, minlen, trimmed = 0;
70700f0a25bfSJan Kara ext4_fsblk_t first_data_blk =
70710f0a25bfSJan Kara le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
7072913eed83SLukas Czerner ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
70737360d173SLukas Czerner int ret = 0;
70747360d173SLukas Czerner
70757360d173SLukas Czerner start = range->start >> sb->s_blocksize_bits;
7076913eed83SLukas Czerner end = start + (range->len >> sb->s_blocksize_bits) - 1;
7077aaf7d73eSLukas Czerner minlen = EXT4_NUM_B2C(EXT4_SB(sb),
7078aaf7d73eSLukas Czerner range->minlen >> sb->s_blocksize_bits);
70797360d173SLukas Czerner
70805de35e8dSLukas Czerner if (minlen > EXT4_CLUSTERS_PER_GROUP(sb) ||
70815de35e8dSLukas Czerner start >= max_blks ||
70825de35e8dSLukas Czerner range->len < sb->s_blocksize)
70837360d173SLukas Czerner return -EINVAL;
7084173b6e38SJan Kara /* No point to try to trim less than discard granularity */
70857b47ef52SChristoph Hellwig if (range->minlen < discard_granularity) {
7086173b6e38SJan Kara minlen = EXT4_NUM_B2C(EXT4_SB(sb),
70877b47ef52SChristoph Hellwig discard_granularity >> sb->s_blocksize_bits);
7088173b6e38SJan Kara if (minlen > EXT4_CLUSTERS_PER_GROUP(sb))
7089173b6e38SJan Kara goto out;
7090173b6e38SJan Kara }
709145e4ab32SJan Kara if (end >= max_blks - 1)
7092913eed83SLukas Czerner end = max_blks - 1;
7093913eed83SLukas Czerner if (end <= first_data_blk)
709422f10457STao Ma goto out;
7095913eed83SLukas Czerner if (start < first_data_blk)
70960f0a25bfSJan Kara start = first_data_blk;
70977360d173SLukas Czerner
7098913eed83SLukas Czerner /* Determine first and last group to examine based on start and end */
70997360d173SLukas Czerner ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) start,
71007137d7a4STheodore Ts'o &first_group, &first_cluster);
7101913eed83SLukas Czerner ext4_get_group_no_and_offset(sb, (ext4_fsblk_t) end,
71027137d7a4STheodore Ts'o &last_group, &last_cluster);
71037360d173SLukas Czerner
7104913eed83SLukas Czerner /* end now represents the last cluster to discard in this group */
7105913eed83SLukas Czerner end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
71067360d173SLukas Czerner
71077360d173SLukas Czerner for (group = first_group; group <= last_group; group++) {
71085229a658SJan Kara if (ext4_trim_interrupted())
71095229a658SJan Kara break;
711078944086SLukas Czerner grp = ext4_get_group_info(sb, group);
71115354b2afSTheodore Ts'o if (!grp)
71125354b2afSTheodore Ts'o continue;
711378944086SLukas Czerner /* We only do this if the grp has never been initialized */
711478944086SLukas Czerner if (unlikely(EXT4_MB_GRP_NEED_INIT(grp))) {
7115adb7ef60SKonstantin Khlebnikov ret = ext4_mb_init_group(sb, group, GFP_NOFS);
711678944086SLukas Czerner if (ret)
71177360d173SLukas Czerner break;
71187360d173SLukas Czerner }
71197360d173SLukas Czerner
71200ba08517STao Ma /*
7121913eed83SLukas Czerner * For all the groups except the last one, last cluster will
7122913eed83SLukas Czerner * always be EXT4_CLUSTERS_PER_GROUP(sb)-1, so we only need to
7123913eed83SLukas Czerner * change it for the last group, note that last_cluster is
7124913eed83SLukas Czerner * already computed earlier by ext4_get_group_no_and_offset()
71250ba08517STao Ma */
712645e4ab32SJan Kara if (group == last_group)
7127913eed83SLukas Czerner end = last_cluster;
712878944086SLukas Czerner if (grp->bb_free >= minlen) {
71297137d7a4STheodore Ts'o cnt = ext4_trim_all_free(sb, group, first_cluster,
713045e4ab32SJan Kara end, minlen);
71317360d173SLukas Czerner if (cnt < 0) {
71327360d173SLukas Czerner ret = cnt;
71337360d173SLukas Czerner break;
71347360d173SLukas Czerner }
71357360d173SLukas Czerner trimmed += cnt;
713621e7fd22SLukas Czerner }
7137913eed83SLukas Czerner
7138913eed83SLukas Czerner /*
7139913eed83SLukas Czerner * For every group except the first one, we are sure
7140913eed83SLukas Czerner * that the first cluster to discard will be cluster #0.
7141913eed83SLukas Czerner */
71427137d7a4STheodore Ts'o first_cluster = 0;
71437360d173SLukas Czerner }
71447360d173SLukas Czerner
71453d56b8d2STao Ma if (!ret)
71462327fb2eSLukas Czerner EXT4_SB(sb)->s_last_trim_minblks = minlen;
71473d56b8d2STao Ma
714822f10457STao Ma out:
7149aaf7d73eSLukas Czerner range->len = EXT4_C2B(EXT4_SB(sb), trimmed) << sb->s_blocksize_bits;
71507360d173SLukas Czerner return ret;
71517360d173SLukas Czerner }
71520c9ec4beSDarrick J. Wong
71530c9ec4beSDarrick J. Wong /* Iterate all the free extents in the group. */
71540c9ec4beSDarrick J. Wong int
ext4_mballoc_query_range(struct super_block * sb,ext4_group_t group,ext4_grpblk_t first,ext4_grpblk_t end,ext4_mballoc_query_range_fn meta_formatter,ext4_mballoc_query_range_fn formatter,void * priv)71550c9ec4beSDarrick J. Wong ext4_mballoc_query_range(
71560c9ec4beSDarrick J. Wong struct super_block *sb,
71570c9ec4beSDarrick J. Wong ext4_group_t group,
7158*2bd7c608STheodore Ts'o ext4_grpblk_t first,
71590c9ec4beSDarrick J. Wong ext4_grpblk_t end,
7160*2bd7c608STheodore Ts'o ext4_mballoc_query_range_fn meta_formatter,
71610c9ec4beSDarrick J. Wong ext4_mballoc_query_range_fn formatter,
71620c9ec4beSDarrick J. Wong void *priv)
71630c9ec4beSDarrick J. Wong {
71640c9ec4beSDarrick J. Wong void *bitmap;
7165*2bd7c608STheodore Ts'o ext4_grpblk_t start, next;
71660c9ec4beSDarrick J. Wong struct ext4_buddy e4b;
71670c9ec4beSDarrick J. Wong int error;
71680c9ec4beSDarrick J. Wong
71690c9ec4beSDarrick J. Wong error = ext4_mb_load_buddy(sb, group, &e4b);
71700c9ec4beSDarrick J. Wong if (error)
71710c9ec4beSDarrick J. Wong return error;
71720c9ec4beSDarrick J. Wong bitmap = e4b.bd_bitmap;
71730c9ec4beSDarrick J. Wong
71740c9ec4beSDarrick J. Wong ext4_lock_group(sb, group);
71750c9ec4beSDarrick J. Wong
7176*2bd7c608STheodore Ts'o start = max(e4b.bd_info->bb_first_free, first);
71770c9ec4beSDarrick J. Wong if (end >= EXT4_CLUSTERS_PER_GROUP(sb))
71780c9ec4beSDarrick J. Wong end = EXT4_CLUSTERS_PER_GROUP(sb) - 1;
7179*2bd7c608STheodore Ts'o if (meta_formatter && start != first) {
7180*2bd7c608STheodore Ts'o if (start > end)
7181*2bd7c608STheodore Ts'o start = end;
7182*2bd7c608STheodore Ts'o ext4_unlock_group(sb, group);
7183*2bd7c608STheodore Ts'o error = meta_formatter(sb, group, first, start - first,
7184*2bd7c608STheodore Ts'o priv);
7185*2bd7c608STheodore Ts'o if (error)
7186*2bd7c608STheodore Ts'o goto out_unload;
7187*2bd7c608STheodore Ts'o ext4_lock_group(sb, group);
7188*2bd7c608STheodore Ts'o }
71890c9ec4beSDarrick J. Wong while (start <= end) {
71900c9ec4beSDarrick J. Wong start = mb_find_next_zero_bit(bitmap, end + 1, start);
71910c9ec4beSDarrick J. Wong if (start > end)
71920c9ec4beSDarrick J. Wong break;
71930c9ec4beSDarrick J. Wong next = mb_find_next_bit(bitmap, end + 1, start);
71940c9ec4beSDarrick J. Wong
71950c9ec4beSDarrick J. Wong ext4_unlock_group(sb, group);
71960c9ec4beSDarrick J. Wong error = formatter(sb, group, start, next - start, priv);
71970c9ec4beSDarrick J. Wong if (error)
71980c9ec4beSDarrick J. Wong goto out_unload;
71990c9ec4beSDarrick J. Wong ext4_lock_group(sb, group);
72000c9ec4beSDarrick J. Wong
72010c9ec4beSDarrick J. Wong start = next + 1;
72020c9ec4beSDarrick J. Wong }
72030c9ec4beSDarrick J. Wong
72040c9ec4beSDarrick J. Wong ext4_unlock_group(sb, group);
72050c9ec4beSDarrick J. Wong out_unload:
72060c9ec4beSDarrick J. Wong ext4_mb_unload_buddy(&e4b);
72070c9ec4beSDarrick J. Wong
72080c9ec4beSDarrick J. Wong return error;
72090c9ec4beSDarrick J. Wong }
7210