blob: dc198bc64c61ddae9db7a936365a820d71abf2d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4/* Reiserfs block (de)allocator, bitmap-based. */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/time.h>
Al Virof466c6f2012-03-17 01:16:43 -04007#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/buffer_head.h>
10#include <linux/kernel.h>
11#include <linux/pagemap.h>
Jeff Mahoney6f010462006-09-30 23:28:43 -070012#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/quotaops.h>
Jan Karac3aa0772011-12-21 20:17:10 +010014#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define PREALLOCATION_SIZE 9
17
18/* different reiserfs block allocator options */
19
20#define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)
21
22#define _ALLOC_concentrating_formatted_nodes 0
23#define _ALLOC_displacing_large_files 1
24#define _ALLOC_displacing_new_packing_localities 2
25#define _ALLOC_old_hashed_relocation 3
26#define _ALLOC_new_hashed_relocation 4
27#define _ALLOC_skip_busy 5
28#define _ALLOC_displace_based_on_dirid 6
29#define _ALLOC_hashed_formatted_nodes 7
30#define _ALLOC_old_way 8
31#define _ALLOC_hundredth_slices 9
32#define _ALLOC_dirid_groups 10
33#define _ALLOC_oid_groups 11
34#define _ALLOC_packing_groups 12
35
36#define concentrating_formatted_nodes(s) test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
37#define displacing_large_files(s) test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
38#define displacing_new_packing_localities(s) test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))
39
40#define SET_OPTION(optname) \
41 do { \
Jeff Mahoney1d889d92009-03-30 14:02:20 -040042 reiserfs_info(s, "block allocator option \"%s\" is set", #optname); \
43 set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 } while(0)
45#define TEST_OPTION(optname, s) \
46 test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
47
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048static inline void get_bit_address(struct super_block *s,
Jeff Mahoney3ee16672007-10-18 23:39:25 -070049 b_blocknr_t block,
50 unsigned int *bmap_nr,
51 unsigned int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Jeff Mahoney098297b2014-04-23 10:00:36 -040053 /*
54 * It is in the bitmap block number equal to the block
55 * number divided by the number of bits in a block.
56 */
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070057 *bmap_nr = block >> (s->s_blocksize_bits + 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070058 /* Within that bitmap block it is located at bit offset *offset. */
59 *offset = block & ((s->s_blocksize << 3) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070062int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Jeff Mahoney3ee16672007-10-18 23:39:25 -070064 unsigned int bmap, offset;
Jeff Mahoneycb680c12007-10-18 23:39:27 -070065 unsigned int bmap_count = reiserfs_bmap_count(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040068 reiserfs_error(s, "vs-4010",
69 "block number is out of range %lu (%u)",
70 block, SB_BLOCK_COUNT(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070074 get_bit_address(s, block, &bmap, &offset);
75
Jeff Mahoney098297b2014-04-23 10:00:36 -040076 /*
77 * Old format filesystem? Unlikely, but the bitmaps are all
78 * up front so we need to account for it.
79 */
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070080 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
Jeff Mahoneya228bf82014-04-23 10:00:42 -040081 &REISERFS_SB(s)->s_properties))) {
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070082 b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
Jeff Mahoneycb680c12007-10-18 23:39:27 -070083 if (block >= bmap1 &&
84 block <= bmap1 + bmap_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040085 reiserfs_error(s, "vs-4019", "bitmap block %lu(%u) "
86 "can't be freed or reused",
87 block, bmap_count);
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070088 return 0;
89 }
90 } else {
91 if (offset == 0) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040092 reiserfs_error(s, "vs-4020", "bitmap block %lu(%u) "
93 "can't be freed or reused",
94 block, bmap_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 return 0;
96 }
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Jeff Mahoneycb680c12007-10-18 23:39:27 -070099 if (bmap >= bmap_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400100 reiserfs_error(s, "vs-4030", "bitmap for requested block "
101 "is out of range: block=%lu, bitmap_nr=%u",
102 block, bmap);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700103 return 0;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700106 if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400107 reiserfs_error(s, "vs-4050", "this is root block (%u), "
108 "it must be busy", SB_ROOT_BLOCK(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700109 return 0;
110 }
111
112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jeff Mahoney098297b2014-04-23 10:00:36 -0400115/*
116 * Searches in journal structures for a given block number (bmap, off).
117 * If block is found in reiserfs journal it suggests next free block
118 * candidate to test.
119 */
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700120static inline int is_block_in_journal(struct super_block *s, unsigned int bmap,
121 int off, int *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 b_blocknr_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
126 if (tmp) { /* hint supplied */
127 *next = tmp;
128 PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
129 } else {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400130 (*next) = off + 1; /* inc offset to avoid looping. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700131 PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
132 }
133 PROC_INFO_INC(s, scan_bitmap.retry);
134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jeff Mahoney098297b2014-04-23 10:00:36 -0400139/*
140 * Searches for a window of zero bits with given minimum and maximum
141 * lengths in one bitmap block
142 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700144 unsigned int bmap_n, int *beg, int boundary,
145 int min, int max, int unfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 struct super_block *s = th->t_super;
148 struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700149 struct buffer_head *bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150 int end, next;
151 int org = *beg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 BUG_ON(!th->t_trans_id);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700154 RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of "
155 "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 if (!bi) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400159 reiserfs_error(s, "jdm-4055", "NULL bitmap info pointer "
160 "for bitmap %d", bmap_n);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700161 return 0;
162 }
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700163
Jeff Mahoney50652272006-09-30 23:28:44 -0700164 bh = reiserfs_read_bitmap_block(s, bmap_n);
165 if (bh == NULL)
166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 while (1) {
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400169cont:
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700170 if (bi->free_count < min) {
171 brelse(bh);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400172 return 0; /* No free blocks in this bitmap */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800175 /* search for a first zero bit -- beginning of a window */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 *beg = reiserfs_find_next_zero_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700177 ((unsigned long *)(bh->b_data), boundary, *beg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178
Jeff Mahoney098297b2014-04-23 10:00:36 -0400179 /*
180 * search for a zero bit fails or the rest of bitmap block
181 * cannot contain a zero window of minimum size
182 */
183 if (*beg + min > boundary) {
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700184 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
189 continue;
190 /* first zero bit found; we check next bits */
191 for (end = *beg + 1;; end++) {
192 if (end >= *beg + max || end >= boundary
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700193 || reiserfs_test_le_bit(end, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 next = end;
195 break;
196 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400197
198 /*
199 * finding the other end of zero bit window requires
200 * looking into journal structures (in case of
201 * searching for free blocks for unformatted nodes)
202 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700203 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
204 break;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Jeff Mahoney098297b2014-04-23 10:00:36 -0400207 /*
208 * now (*beg) points to beginning of zero bits window,
209 * (end) points to one bit after the window end
210 */
211
212 /* found window of proper size */
213 if (end - *beg >= min) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700214 int i;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700215 reiserfs_prepare_for_journal(s, bh, 1);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400216 /*
217 * try to set all blocks used checking are
218 * they still free
219 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700220 for (i = *beg; i < end; i++) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400221 /* Don't check in journal again. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 if (reiserfs_test_and_set_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700223 (i, bh->b_data)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400224 /*
225 * bit was set by another process while
226 * we slept in prepare_for_journal()
227 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700228 PROC_INFO_INC(s, scan_bitmap.stolen);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400229
230 /*
231 * we can continue with smaller set
232 * of allocated blocks, if length of
233 * this set is more or equal to `min'
234 */
235 if (i >= *beg + min) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236 end = i;
237 break;
238 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400239
240 /*
241 * otherwise we clear all bit
242 * were set ...
243 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700244 while (--i >= *beg)
Akinobu Mita0c2fd1bfb2011-07-25 17:13:37 -0700245 reiserfs_clear_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700246 (i, bh->b_data);
247 reiserfs_restore_prepared_buffer(s, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700248 *beg = org;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400249
250 /*
251 * Search again in current block
252 * from beginning
253 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700254 goto cont;
255 }
256 }
257 bi->free_count -= (end - *beg);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400258 journal_mark_dirty(th, bh);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700259 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260
261 /* free block count calculation */
262 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
263 1);
264 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400265 journal_mark_dirty(th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266
267 return end - (*beg);
268 } else {
269 *beg = next;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700274static int bmap_hash_id(struct super_block *s, u32 id)
275{
276 char *hash_in = NULL;
277 unsigned long hash;
278 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 if (id <= 2) {
281 bm = 1;
282 } else {
283 hash_in = (char *)(&id);
284 hash = keyed_hash(hash_in, 4);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700285 bm = hash % reiserfs_bmap_count(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700286 if (!bm)
287 bm = 1;
288 }
289 /* this can only be true when SB_BMAP_NR = 1 */
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700290 if (bm >= reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 bm = 0;
292 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295/*
296 * hashes the id and then returns > 0 if the block group for the
297 * corresponding hash is full
298 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700299static inline int block_group_used(struct super_block *s, u32 id)
300{
Jeff Mahoney50652272006-09-30 23:28:44 -0700301 int bm = bmap_hash_id(s, id);
302 struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
303
Jeff Mahoney098297b2014-04-23 10:00:36 -0400304 /*
305 * If we don't have cached information on this bitmap block, we're
Jeff Mahoney50652272006-09-30 23:28:44 -0700306 * going to have to load it later anyway. Loading it here allows us
Joe Perchesc78bad12008-02-03 17:33:42 +0200307 * to make a better decision. This favors long-term performance gain
Jeff Mahoney50652272006-09-30 23:28:44 -0700308 * with a better on-disk layout vs. a short term gain of skipping the
Jeff Mahoney098297b2014-04-23 10:00:36 -0400309 * read and potentially having a bad placement.
310 */
Jeff Mahoney4d208512007-10-18 23:39:26 -0700311 if (info->free_count == UINT_MAX) {
Jeff Mahoney50652272006-09-30 23:28:44 -0700312 struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
313 brelse(bh);
314 }
315
316 if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700317 return 0;
318 }
319 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322/*
323 * the packing is returned in disk byte order
324 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700326{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 __le32 packing;
328 if (TEST_OPTION(packing_groups, dir->i_sb)) {
329 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
330 /*
331 * some versions of reiserfsck expect packing locality 1 to be
332 * special
333 */
334 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
335 packing = INODE_PKEY(dir)->k_objectid;
336 else
337 packing = INODE_PKEY(dir)->k_dir_id;
338 } else
339 packing = INODE_PKEY(dir)->k_objectid;
340 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342
Jeff Mahoney098297b2014-04-23 10:00:36 -0400343/*
344 * Tries to find contiguous zero bit window (given size) in given region of
345 * bitmap and place new blocks there. Returns number of allocated blocks.
346 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700347static int scan_bitmap(struct reiserfs_transaction_handle *th,
348 b_blocknr_t * start, b_blocknr_t finish,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700349 int min, int max, int unfm, sector_t file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700351 int nr_allocated = 0;
352 struct super_block *s = th->t_super;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700353 unsigned int bm, off;
354 unsigned int end_bm, end_off;
355 unsigned int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700357 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700358 PROC_INFO_INC(s, scan_bitmap.call);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400359
360 /* No point in looking for more free blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700361 if (SB_FREE_BLOCKS(s) <= 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 get_bit_address(s, *start, &bm, &off);
365 get_bit_address(s, finish, &end_bm, &end_off);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700366 if (bm > reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700367 return 0;
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700368 if (end_bm > reiserfs_bmap_count(s))
369 end_bm = reiserfs_bmap_count(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jeff Mahoney098297b2014-04-23 10:00:36 -0400371 /*
372 * When the bitmap is more than 10% free, anyone can allocate.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700373 * When it's less than 10% free, only files that already use the
374 * bitmap are allowed. Once we pass 80% full, this restriction
375 * is lifted.
376 *
377 * We do this so that files that grow later still have space close to
378 * their original allocation. This improves locality, and presumably
379 * performance as a result.
380 *
381 * This is only an allocation policy and does not make up for getting a
382 * bad hint. Decent hinting must be implemented for this to work well.
383 */
384 if (TEST_OPTION(skip_busy, s)
385 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
386 for (; bm < end_bm; bm++, off = 0) {
387 if ((off && (!unfm || (file_block != 0)))
388 || SB_AP_BITMAP(s)[bm].free_count >
389 (s->s_blocksize << 3) / 10)
390 nr_allocated =
391 scan_bitmap_block(th, bm, &off, off_max,
392 min, max, unfm);
393 if (nr_allocated)
394 goto ret;
395 }
396 /* we know from above that start is a reasonable number */
397 get_bit_address(s, *start, &bm, &off);
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 for (; bm < end_bm; bm++, off = 0) {
401 nr_allocated =
402 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
403 if (nr_allocated)
404 goto ret;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700407 nr_allocated =
408 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
409
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400410ret:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411 *start = bm * off_max + off;
412 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414}
415
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700416static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
417 struct inode *inode, b_blocknr_t block,
418 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700420 struct super_block *s = th->t_super;
421 struct reiserfs_super_block *rs;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700422 struct buffer_head *sbh, *bmbh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700423 struct reiserfs_bitmap_info *apbi;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700424 unsigned int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427 PROC_INFO_INC(s, free_block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 rs = SB_DISK_SUPER_BLOCK(s);
429 sbh = SB_BUFFER_WITH_SB(s);
430 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700434 if (nr >= reiserfs_bmap_count(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400435 reiserfs_error(s, "vs-4075", "block %lu is out of range",
436 block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 return;
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jeff Mahoney50652272006-09-30 23:28:44 -0700440 bmbh = reiserfs_read_bitmap_block(s, nr);
441 if (!bmbh)
442 return;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700443
444 reiserfs_prepare_for_journal(s, bmbh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 /* clear bit for the given block in bit map */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700447 if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400448 reiserfs_error(s, "vs-4080",
449 "block %lu: bit already cleared", block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 }
451 apbi[nr].free_count++;
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400452 journal_mark_dirty(th, bmbh);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700453 brelse(bmbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700455 reiserfs_prepare_for_journal(s, sbh, 1);
456 /* update super block */
457 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400459 journal_mark_dirty(th, sbh);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400460 if (for_unformatted) {
461 int depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500462 dquot_free_block_nodirty(inode, 1);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400463 reiserfs_write_lock_nested(s, depth);
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467void reiserfs_free_block(struct reiserfs_transaction_handle *th,
468 struct inode *inode, b_blocknr_t block,
469 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 struct super_block *s = th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700473 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700474 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700475 if (!is_reusable(s, block, 1))
476 return;
477
478 if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400479 reiserfs_error(th->t_super, "bitmap-4072",
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700480 "Trying to free block outside file system "
481 "boundaries (%lu > %lu)",
482 block, sb_block_count(REISERFS_SB(s)->s_rs));
483 return;
484 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 /* mark it before we clear it, just in case */
486 journal_mark_freed(th, s, block);
487 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
492 struct inode *inode, b_blocknr_t block)
493{
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700494 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495 RFALSE(!th->t_super,
496 "vs-4060: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700497 if (!is_reusable(th->t_super, block, 1))
498 return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502static void __discard_prealloc(struct reiserfs_transaction_handle *th,
503 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 unsigned long save = ei->i_prealloc_block;
506 int dirty = 0;
507 struct inode *inode = &ei->vfs_inode;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700508
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700509 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 if (ei->i_prealloc_count < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400512 reiserfs_error(th->t_super, "zam-4001",
513 "inode has negative prealloc blocks count.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700515 while (ei->i_prealloc_count > 0) {
516 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
517 ei->i_prealloc_block++;
518 ei->i_prealloc_count--;
519 dirty = 1;
520 }
521 if (dirty)
522 reiserfs_update_sd(th, inode);
523 ei->i_prealloc_block = save;
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400524 list_del_init(&ei->i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700528void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
529 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531 struct reiserfs_inode_info *ei = REISERFS_I(inode);
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700532
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 BUG_ON(!th->t_trans_id);
534 if (ei->i_prealloc_count)
535 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700542 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700543 while (!list_empty(plist)) {
544 struct reiserfs_inode_info *ei;
545 ei = list_entry(plist->next, struct reiserfs_inode_info,
546 i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700548 if (!ei->i_prealloc_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400549 reiserfs_error(th->t_super, "zam-4001",
550 "inode is in prealloc list but has "
551 "no preallocated blocks.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700554 __discard_prealloc(th, ei);
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700558void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700560 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
561 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
562 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700566int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700568 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Jeff Mahoney098297b2014-04-23 10:00:36 -0400570 /* clear default settings */
571 REISERFS_SB(s)->s_alloc_options.bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700573 while ((this_char = strsep(&options, ":")) != NULL) {
574 if ((value = strchr(this_char, '=')) != NULL)
575 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700577 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
578 int temp;
579 SET_OPTION(concentrating_formatted_nodes);
580 temp = (value
581 && *value) ? simple_strtoul(value, &value,
582 0) : 10;
583 if (temp <= 0 || temp > 100) {
584 REISERFS_SB(s)->s_alloc_options.border = 10;
585 } else {
586 REISERFS_SB(s)->s_alloc_options.border =
587 100 / temp;
588 }
589 continue;
590 }
591 if (!strcmp(this_char, "displacing_large_files")) {
592 SET_OPTION(displacing_large_files);
593 REISERFS_SB(s)->s_alloc_options.large_file_size =
594 (value
595 && *value) ? simple_strtoul(value, &value, 0) : 16;
596 continue;
597 }
598 if (!strcmp(this_char, "displacing_new_packing_localities")) {
599 SET_OPTION(displacing_new_packing_localities);
600 continue;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700603 if (!strcmp(this_char, "old_hashed_relocation")) {
604 SET_OPTION(old_hashed_relocation);
605 continue;
606 }
607
608 if (!strcmp(this_char, "new_hashed_relocation")) {
609 SET_OPTION(new_hashed_relocation);
610 continue;
611 }
612
613 if (!strcmp(this_char, "dirid_groups")) {
614 SET_OPTION(dirid_groups);
615 continue;
616 }
617 if (!strcmp(this_char, "oid_groups")) {
618 SET_OPTION(oid_groups);
619 continue;
620 }
621 if (!strcmp(this_char, "packing_groups")) {
622 SET_OPTION(packing_groups);
623 continue;
624 }
625 if (!strcmp(this_char, "hashed_formatted_nodes")) {
626 SET_OPTION(hashed_formatted_nodes);
627 continue;
628 }
629
630 if (!strcmp(this_char, "skip_busy")) {
631 SET_OPTION(skip_busy);
632 continue;
633 }
634
635 if (!strcmp(this_char, "hundredth_slices")) {
636 SET_OPTION(hundredth_slices);
637 continue;
638 }
639
640 if (!strcmp(this_char, "old_way")) {
641 SET_OPTION(old_way);
642 continue;
643 }
644
645 if (!strcmp(this_char, "displace_based_on_dirid")) {
646 SET_OPTION(displace_based_on_dirid);
647 continue;
648 }
649
650 if (!strcmp(this_char, "preallocmin")) {
651 REISERFS_SB(s)->s_alloc_options.preallocmin =
652 (value
653 && *value) ? simple_strtoul(value, &value, 0) : 4;
654 continue;
655 }
656
657 if (!strcmp(this_char, "preallocsize")) {
658 REISERFS_SB(s)->s_alloc_options.preallocsize =
659 (value
660 && *value) ? simple_strtoul(value, &value,
661 0) :
662 PREALLOCATION_SIZE;
663 continue;
664 }
665
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400666 reiserfs_warning(s, "zam-4001", "unknown option - %s",
667 this_char);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
Jeff Mahoney1d889d92009-03-30 14:02:20 -0400671 reiserfs_info(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Jan Karac3aa0772011-12-21 20:17:10 +0100675static void print_sep(struct seq_file *seq, int *first)
676{
677 if (!*first)
678 seq_puts(seq, ":");
679 else
680 *first = 0;
681}
682
683void show_alloc_options(struct seq_file *seq, struct super_block *s)
684{
685 int first = 1;
686
687 if (SB_ALLOC_OPTS(s) == ((1 << _ALLOC_skip_busy) |
688 (1 << _ALLOC_dirid_groups) | (1 << _ALLOC_packing_groups)))
689 return;
690
691 seq_puts(seq, ",alloc=");
692
693 if (TEST_OPTION(concentrating_formatted_nodes, s)) {
694 print_sep(seq, &first);
695 if (REISERFS_SB(s)->s_alloc_options.border != 10) {
696 seq_printf(seq, "concentrating_formatted_nodes=%d",
697 100 / REISERFS_SB(s)->s_alloc_options.border);
698 } else
699 seq_puts(seq, "concentrating_formatted_nodes");
700 }
701 if (TEST_OPTION(displacing_large_files, s)) {
702 print_sep(seq, &first);
703 if (REISERFS_SB(s)->s_alloc_options.large_file_size != 16) {
704 seq_printf(seq, "displacing_large_files=%lu",
705 REISERFS_SB(s)->s_alloc_options.large_file_size);
706 } else
707 seq_puts(seq, "displacing_large_files");
708 }
709 if (TEST_OPTION(displacing_new_packing_localities, s)) {
710 print_sep(seq, &first);
711 seq_puts(seq, "displacing_new_packing_localities");
712 }
713 if (TEST_OPTION(old_hashed_relocation, s)) {
714 print_sep(seq, &first);
715 seq_puts(seq, "old_hashed_relocation");
716 }
717 if (TEST_OPTION(new_hashed_relocation, s)) {
718 print_sep(seq, &first);
719 seq_puts(seq, "new_hashed_relocation");
720 }
721 if (TEST_OPTION(dirid_groups, s)) {
722 print_sep(seq, &first);
723 seq_puts(seq, "dirid_groups");
724 }
725 if (TEST_OPTION(oid_groups, s)) {
726 print_sep(seq, &first);
727 seq_puts(seq, "oid_groups");
728 }
729 if (TEST_OPTION(packing_groups, s)) {
730 print_sep(seq, &first);
731 seq_puts(seq, "packing_groups");
732 }
733 if (TEST_OPTION(hashed_formatted_nodes, s)) {
734 print_sep(seq, &first);
735 seq_puts(seq, "hashed_formatted_nodes");
736 }
737 if (TEST_OPTION(skip_busy, s)) {
738 print_sep(seq, &first);
739 seq_puts(seq, "skip_busy");
740 }
741 if (TEST_OPTION(hundredth_slices, s)) {
742 print_sep(seq, &first);
743 seq_puts(seq, "hundredth_slices");
744 }
745 if (TEST_OPTION(old_way, s)) {
746 print_sep(seq, &first);
747 seq_puts(seq, "old_way");
748 }
749 if (TEST_OPTION(displace_based_on_dirid, s)) {
750 print_sep(seq, &first);
751 seq_puts(seq, "displace_based_on_dirid");
752 }
753 if (REISERFS_SB(s)->s_alloc_options.preallocmin != 0) {
754 print_sep(seq, &first);
755 seq_printf(seq, "preallocmin=%d",
756 REISERFS_SB(s)->s_alloc_options.preallocmin);
757 }
758 if (REISERFS_SB(s)->s_alloc_options.preallocsize != 17) {
759 print_sep(seq, &first);
760 seq_printf(seq, "preallocsize=%d",
761 REISERFS_SB(s)->s_alloc_options.preallocsize);
762 }
763}
764
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700765static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
766{
767 char *hash_in;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700768
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700769 if (hint->formatted_node) {
770 hash_in = (char *)&hint->key.k_dir_id;
771 } else {
772 if (!hint->inode) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400773 /*hint->search_start = hint->beg;*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700774 hash_in = (char *)&hint->key.k_dir_id;
775 } else
776 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
777 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
778 else
779 hash_in =
780 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
781 }
782
783 hint->search_start =
784 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * Relocation based on dirid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200789 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 unsigned long hash;
794 __u32 dirid = 0;
795 int bm = 0;
796 struct super_block *sb = hint->th->t_super;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700799 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
800 else if (hint->formatted_node)
801 dirid = hint->key.k_dir_id;
802
803 if (dirid) {
804 bm = bmap_hash_id(sb, dirid);
805 hash = bm * (sb->s_blocksize << 3);
806 /* give a portion of the block group to metadata */
807 if (hint->inode)
808 hash += sb->s_blocksize / 2;
809 hint->search_start = hash;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/*
814 * Relocation based on oid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200815 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700817static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 if (hint->inode) {
820 unsigned long hash;
821 __u32 oid;
822 __u32 dirid;
823 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Jeff Mahoney098297b2014-04-23 10:00:36 -0400827 /*
828 * keep the root dir and it's first set of subdirs close to
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829 * the start of the disk
830 */
831 if (dirid <= 2)
832 hash = (hint->inode->i_sb->s_blocksize << 3);
833 else {
834 oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
835 bm = bmap_hash_id(hint->inode->i_sb, oid);
836 hash = bm * (hint->inode->i_sb->s_blocksize << 3);
837 }
838 hint->search_start = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Jeff Mahoney098297b2014-04-23 10:00:36 -0400842/*
843 * returns 1 if it finds an indirect item and gets valid hint info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * from it, otherwise 0
845 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700846static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800848 struct treepath *path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700849 struct buffer_head *bh;
850 struct item_head *ih;
851 int pos_in_item;
852 __le32 *item;
853 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Jeff Mahoney098297b2014-04-23 10:00:36 -0400855 /*
856 * reiserfs code can call this function w/o pointer to path
857 * structure supplied; then we rely on supplied search_start
858 */
859 if (!hint->path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700862 path = hint->path;
863 bh = get_last_bh(path);
864 RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400865 ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866 pos_in_item = path->pos_in_item;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400867 item = tp_item_body(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700869 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jeff Mahoney098297b2014-04-23 10:00:36 -0400871 /*
872 * for indirect item: go to left and look for the first non-hole entry
873 * in the indirect item
874 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700875 if (!hint->formatted_node && is_indirect_le_ih(ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700876 if (pos_in_item == I_UNFM_NUM(ih))
877 pos_in_item--;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700878 while (pos_in_item >= 0) {
879 int t = get_block_num(item, pos_in_item);
880 if (t) {
881 hint->search_start = t;
882 ret = 1;
883 break;
884 }
885 pos_in_item--;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 /* does result value fit into specified region? */
890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Jeff Mahoney098297b2014-04-23 10:00:36 -0400893/*
894 * should be, if formatted node, then try to put on first part of the device
895 * specified as number of percent with mount option device, else try to put
896 * on last of device. This is not to say it is good code to do so,
897 * but the effect should be measured.
898 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700899static inline void set_border_in_hint(struct super_block *s,
900 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700902 b_blocknr_t border =
903 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700905 if (hint->formatted_node)
906 hint->end = border - 1;
907 else
908 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700911static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
914 hint->search_start =
915 hint->beg +
916 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
917 4) % (hint->end - hint->beg);
918 else
919 hint->search_start =
920 hint->beg +
921 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
922 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700925static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700927 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 if (!hint->inode)
930 hash_in = (char *)&hint->key.k_dir_id;
931 else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
932 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
933 else
934 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700936 hint->search_start =
937 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700940static inline int
941this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
942 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700944 return hint->block ==
945 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700949static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700951 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700953 hint->th->displace_new_blocks = 0;
954 hint->search_start =
955 hint->beg + keyed_hash((char *)(&key->k_objectid),
956 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700960static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
961{
962 b_blocknr_t border;
963 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700965 if (hint->formatted_node || hint->inode == NULL) {
966 return 0;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
970 border =
971 hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
972 4) % (hint->end - hint->beg - 1);
973 if (border > hint->search_start)
974 hint->search_start = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700976 return 1;
977}
978
979static inline int old_way(reiserfs_blocknr_hint_t * hint)
980{
981 b_blocknr_t border;
982
983 if (hint->formatted_node || hint->inode == NULL) {
984 return 0;
985 }
986
987 border =
988 hint->beg +
989 le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
990 hint->beg);
991 if (border > hint->search_start)
992 hint->search_start = border;
993
994 return 1;
995}
996
997static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
998{
999 struct in_core_key *key = &hint->key;
1000 b_blocknr_t slice_start;
1001
1002 slice_start =
1003 (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
1004 if (slice_start > hint->search_start
1005 || slice_start + (hint->end / 100) <= hint->search_start) {
1006 hint->search_start = slice_start;
1007 }
1008}
1009
1010static void determine_search_start(reiserfs_blocknr_hint_t * hint,
1011 int amount_needed)
1012{
1013 struct super_block *s = hint->th->t_super;
1014 int unfm_hint;
1015
1016 hint->beg = 0;
1017 hint->end = SB_BLOCK_COUNT(s) - 1;
1018
1019 /* This is former border algorithm. Now with tunable border offset */
1020 if (concentrating_formatted_nodes(s))
1021 set_border_in_hint(s, hint);
1022
1023#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoney098297b2014-04-23 10:00:36 -04001024 /*
1025 * whenever we create a new directory, we displace it. At first
1026 * we will hash for location, later we might look for a moderately
1027 * empty place for it
1028 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001029 if (displacing_new_packing_localities(s)
1030 && hint->th->displace_new_blocks) {
1031 displace_new_packing_locality(hint);
1032
Jeff Mahoney098297b2014-04-23 10:00:36 -04001033 /*
1034 * we do not continue determine_search_start,
1035 * if new packing locality is being displaced
1036 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001037 return;
1038 }
1039#endif
1040
Jeff Mahoney098297b2014-04-23 10:00:36 -04001041 /*
1042 * all persons should feel encouraged to add more special cases
1043 * here and test them
1044 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001045
1046 if (displacing_large_files(s) && !hint->formatted_node
1047 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
1048 displace_large_file(hint);
1049 return;
1050 }
1051
Jeff Mahoney098297b2014-04-23 10:00:36 -04001052 /*
1053 * if none of our special cases is relevant, use the left
1054 * neighbor in the tree order of the new node we are allocating for
1055 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001056 if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
1057 hash_formatted_node(hint);
1058 return;
1059 }
1060
1061 unfm_hint = get_left_neighbor(hint);
1062
Jeff Mahoney098297b2014-04-23 10:00:36 -04001063 /*
1064 * Mimic old block allocator behaviour, that is if VFS allowed for
1065 * preallocation, new blocks are displaced based on directory ID.
1066 * Also, if suggested search_start is less than last preallocated
1067 * block, we start searching from it, assuming that HDD dataflow
1068 * is faster in forward direction
1069 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 if (TEST_OPTION(old_way, s)) {
1071 if (!hint->formatted_node) {
1072 if (!reiserfs_hashed_relocation(s))
1073 old_way(hint);
1074 else if (!reiserfs_no_unhashed_relocation(s))
1075 old_hashed_relocation(hint);
1076
1077 if (hint->inode
1078 && hint->search_start <
1079 REISERFS_I(hint->inode)->i_prealloc_block)
1080 hint->search_start =
1081 REISERFS_I(hint->inode)->i_prealloc_block;
1082 }
1083 return;
1084 }
1085
1086 /* This is an approach proposed by Hans */
1087 if (TEST_OPTION(hundredth_slices, s)
1088 && !(displacing_large_files(s) && !hint->formatted_node)) {
1089 hundredth_slices(hint);
1090 return;
1091 }
1092
1093 /* old_hashed_relocation only works on unformatted */
1094 if (!unfm_hint && !hint->formatted_node &&
1095 TEST_OPTION(old_hashed_relocation, s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001097 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001099 /* new_hashed_relocation works with both formatted/unformatted nodes */
1100 if ((!unfm_hint || hint->formatted_node) &&
1101 TEST_OPTION(new_hashed_relocation, s)) {
1102 new_hashed_relocation(hint);
1103 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001104
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001105 /* dirid grouping works only on unformatted nodes */
1106 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
1107 dirid_groups(hint);
1108 }
1109#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1110 if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
1111 dirid_groups(hint);
1112 }
1113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001115 /* oid grouping works only on unformatted nodes */
1116 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
1117 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
1123{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124 /* make minimum size a mount option and benchmark both ways */
1125 /* we preallocate blocks only for regular files, specific size */
1126 /* benchmark preallocating always and see what happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130 if (!hint->formatted_node && hint->preallocate) {
1131 if (S_ISREG(hint->inode->i_mode)
1132 && hint->inode->i_size >=
1133 REISERFS_SB(hint->th->t_super)->s_alloc_options.
1134 preallocmin * hint->inode->i_sb->s_blocksize)
1135 hint->prealloc_size =
1136 REISERFS_SB(hint->th->t_super)->s_alloc_options.
1137 preallocsize - 1;
1138 }
1139 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001142static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
1143 b_blocknr_t * new_blocknrs,
1144 b_blocknr_t start,
1145 b_blocknr_t finish, int min,
1146 int amount_needed,
1147 int prealloc_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 int rest = amount_needed;
1150 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001152 while (rest > 0 && start <= finish) {
1153 nr_allocated = scan_bitmap(hint->th, &start, finish, min,
1154 rest + prealloc_size,
1155 !hint->formatted_node, hint->block);
1156
1157 if (nr_allocated == 0) /* no new blocks allocated, return */
1158 break;
1159
1160 /* fill free_blocknrs array first */
1161 while (rest > 0 && nr_allocated > 0) {
1162 *new_blocknrs++ = start++;
1163 rest--;
1164 nr_allocated--;
1165 }
1166
1167 /* do we have something to fill prealloc. array also ? */
1168 if (nr_allocated > 0) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001169 /*
1170 * it means prealloc_size was greater that 0 and
1171 * we do preallocation
1172 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001173 list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
1174 &SB_JOURNAL(hint->th->t_super)->
1175 j_prealloc_list);
1176 REISERFS_I(hint->inode)->i_prealloc_block = start;
1177 REISERFS_I(hint->inode)->i_prealloc_count =
1178 nr_allocated;
1179 break;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001187 (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
1188 int amount_needed) {
1189 struct super_block *s = hint->th->t_super;
1190 b_blocknr_t start = hint->search_start;
1191 b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
1192 int passno = 0;
1193 int nr_allocated = 0;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001194 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 determine_prealloc_size(hint);
1197 if (!hint->formatted_node) {
1198 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001200 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1201 "reiserquota: allocating %d blocks id=%u",
1202 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001204 depth = reiserfs_write_unlock_nested(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 quota_ret =
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001206 dquot_alloc_block_nodirty(hint->inode, amount_needed);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001207 if (quota_ret) { /* Quota exceeded? */
1208 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001209 return QUOTA_EXCEEDED;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001210 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001211 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1214 "reiserquota: allocating (prealloc) %d blocks id=%u",
1215 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#endif
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001217 quota_ret = dquot_prealloc_block_nodirty(hint->inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 hint->prealloc_size);
1219 if (quota_ret)
1220 hint->preallocate = hint->prealloc_size = 0;
1221 }
1222 /* for unformatted nodes, force large allocations */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001223 reiserfs_write_lock_nested(s, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001226 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 switch (passno++) {
1228 case 0: /* Search from hint->search_start to end of disk */
1229 start = hint->search_start;
1230 finish = SB_BLOCK_COUNT(s) - 1;
1231 break;
1232 case 1: /* Search from hint->beg to hint->search_start */
1233 start = hint->beg;
1234 finish = hint->search_start;
1235 break;
1236 case 2: /* Last chance: Search from 0 to hint->beg */
1237 start = 0;
1238 finish = hint->beg;
1239 break;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001240 default:
1241 /* We've tried searching everywhere, not enough space */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001242 /* Free the blocks */
1243 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001245 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1246 "reiserquota: freeing (nospace) %d blocks id=%u",
1247 amount_needed +
1248 hint->prealloc_size -
1249 nr_allocated,
1250 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#endif
Jan Kara77db4f22009-01-26 17:14:18 +01001252 /* Free not allocated blocks */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001253 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001254 dquot_free_block_nodirty(hint->inode,
Jan Kara77db4f22009-01-26 17:14:18 +01001255 amount_needed + hint->prealloc_size -
1256 nr_allocated);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001257 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001258 }
1259 while (nr_allocated--)
1260 reiserfs_free_block(hint->th, hint->inode,
1261 new_blocknrs[nr_allocated],
1262 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264 return NO_DISK_SPACE;
1265 }
1266 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1267 new_blocknrs +
1268 nr_allocated,
1269 start, finish,
Jeff Mahoney9ea0f942006-09-30 23:28:45 -07001270 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001271 amount_needed -
1272 nr_allocated,
1273 hint->
1274 prealloc_size))
1275 < amount_needed);
1276 if (!hint->formatted_node &&
1277 amount_needed + hint->prealloc_size >
1278 nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
1279 /* Some of preallocation blocks were not allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001281 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1282 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1283 amount_needed + hint->prealloc_size -
1284 nr_allocated -
1285 REISERFS_I(hint->inode)->i_prealloc_count,
1286 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001288
1289 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001290 dquot_free_block_nodirty(hint->inode, amount_needed +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 hint->prealloc_size - nr_allocated -
1292 REISERFS_I(hint->inode)->
1293 i_prealloc_count);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001294 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001297 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300/* grab new blocknrs from preallocated list */
1301/* return amount still needed after using them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
1303 b_blocknr_t * new_blocknrs,
1304 int amount_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001306 struct inode *inode = hint->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001308 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1309 while (amount_needed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001311 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1312 REISERFS_I(inode)->i_prealloc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001314 amount_needed--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001316 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1317 list_del(&REISERFS_I(inode)->i_prealloc_list);
1318 break;
1319 }
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001322 /* return amount still needed after using preallocated blocks */
1323 return amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Jeff Mahoney098297b2014-04-23 10:00:36 -04001326int reiserfs_allocate_blocknrs(reiserfs_blocknr_hint_t *hint,
1327 b_blocknr_t *new_blocknrs,
1328 int amount_needed,
1329 /* Amount of blocks we have already reserved */
1330 int reserved_by_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001332 int initial_amount_needed = amount_needed;
1333 int ret;
1334 struct super_block *s = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 /* Check if there is enough space, taking into account reserved space */
1337 if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
1338 amount_needed - reserved_by_us)
1339 return NO_DISK_SPACE;
1340 /* should this be if !hint->inode && hint->preallocate? */
1341 /* do you mean hint->formatted_node can be removed ? - Zam */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001342 /*
1343 * hint->formatted_node cannot be removed because we try to access
1344 * inode information here, and there is often no inode associated with
1345 * metadata allocations - green
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001348 if (!hint->formatted_node && hint->preallocate) {
1349 amount_needed = use_preallocated_list_if_available
1350 (hint, new_blocknrs, amount_needed);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001351
1352 /*
1353 * We have all the block numbers we need from the
1354 * prealloc list
1355 */
1356 if (amount_needed == 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001357 return CARRY_ON;
1358 new_blocknrs += (initial_amount_needed - amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001360
1361 /* find search start and save it in hint structure */
1362 determine_search_start(hint, amount_needed);
1363 if (hint->search_start >= SB_BLOCK_COUNT(s))
1364 hint->search_start = SB_BLOCK_COUNT(s) - 1;
1365
1366 /* allocation itself; fill new_blocknrs and preallocation arrays */
1367 ret = blocknrs_and_prealloc_arrays_from_search_start
1368 (hint, new_blocknrs, amount_needed);
1369
Jeff Mahoney098297b2014-04-23 10:00:36 -04001370 /*
1371 * We used prealloc. list to fill (partially) new_blocknrs array.
1372 * If final allocation fails we need to return blocks back to
1373 * prealloc. list or just free them. -- Zam (I chose second
1374 * variant)
1375 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001376 if (ret != CARRY_ON) {
1377 while (amount_needed++ < initial_amount_needed) {
1378 reiserfs_free_block(hint->th, hint->inode,
1379 *(--new_blocknrs), 1);
1380 }
1381 }
1382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Jeff Mahoney6f010462006-09-30 23:28:43 -07001385void reiserfs_cache_bitmap_metadata(struct super_block *sb,
1386 struct buffer_head *bh,
1387 struct reiserfs_bitmap_info *info)
1388{
1389 unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
1390
Jeff Mahoney4d208512007-10-18 23:39:26 -07001391 /* The first bit must ALWAYS be 1 */
Jeff Mahoney0030b642009-03-30 14:02:28 -04001392 if (!reiserfs_test_le_bit(0, (unsigned long *)bh->b_data))
1393 reiserfs_error(sb, "reiserfs-2025", "bitmap block %lu is "
1394 "corrupted: first bit must be 1", bh->b_blocknr);
Jeff Mahoney4d208512007-10-18 23:39:26 -07001395
1396 info->free_count = 0;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001397
1398 while (--cur >= (unsigned long *)bh->b_data) {
Jeff Mahoney6f010462006-09-30 23:28:43 -07001399 /* 0 and ~0 are special, we can optimize for them */
Jeff Mahoney4d208512007-10-18 23:39:26 -07001400 if (*cur == 0)
Jeff Mahoney6f010462006-09-30 23:28:43 -07001401 info->free_count += BITS_PER_LONG;
Jeff Mahoney4d208512007-10-18 23:39:26 -07001402 else if (*cur != ~0L) /* A mix, investigate */
Akinobu Mita9d6bf5a2011-07-25 17:13:38 -07001403 info->free_count += BITS_PER_LONG - hweight_long(*cur);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001404 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001405}
1406
1407struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
1408 unsigned int bitmap)
1409{
1410 b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
Jeff Mahoney50652272006-09-30 23:28:44 -07001411 struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001412 struct buffer_head *bh;
1413
Jeff Mahoney098297b2014-04-23 10:00:36 -04001414 /*
1415 * Way old format filesystems had the bitmaps packed up front.
1416 * I doubt there are any of these left, but just in case...
1417 */
Jeff Mahoney6f010462006-09-30 23:28:43 -07001418 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001419 &REISERFS_SB(sb)->s_properties)))
Jeff Mahoney6f010462006-09-30 23:28:43 -07001420 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
1421 else if (bitmap == 0)
1422 block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
1423
Jeff Mahoney50652272006-09-30 23:28:44 -07001424 bh = sb_bread(sb, block);
1425 if (bh == NULL)
Eric Eric Sesterhenn00079e02006-10-06 22:19:45 -07001426 reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001427 "reading failed", __func__, block);
Jeff Mahoney50652272006-09-30 23:28:44 -07001428 else {
1429 if (buffer_locked(bh)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04001430 int depth;
Jeff Mahoney50652272006-09-30 23:28:44 -07001431 PROC_INFO_INC(sb, scan_bitmap.wait);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001432 depth = reiserfs_write_unlock_nested(sb);
Jeff Mahoney50652272006-09-30 23:28:44 -07001433 __wait_on_buffer(bh);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001434 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoney50652272006-09-30 23:28:44 -07001435 }
1436 BUG_ON(!buffer_uptodate(bh));
1437 BUG_ON(atomic_read(&bh->b_count) == 0);
1438
Jeff Mahoney4d208512007-10-18 23:39:26 -07001439 if (info->free_count == UINT_MAX)
Jeff Mahoney50652272006-09-30 23:28:44 -07001440 reiserfs_cache_bitmap_metadata(sb, bh, info);
1441 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001442
1443 return bh;
1444}
1445
1446int reiserfs_init_bitmap_cache(struct super_block *sb)
1447{
1448 struct reiserfs_bitmap_info *bitmap;
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001449 unsigned int bmap_nr = reiserfs_bmap_count(sb);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001450
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001451 bitmap = vmalloc(sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001452 if (bitmap == NULL)
1453 return -ENOMEM;
1454
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001455 memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001456
Jeff Mahoney6f010462006-09-30 23:28:43 -07001457 SB_AP_BITMAP(sb) = bitmap;
1458
1459 return 0;
1460}
Jeff Mahoney50652272006-09-30 23:28:44 -07001461
1462void reiserfs_free_bitmap_cache(struct super_block *sb)
1463{
1464 if (SB_AP_BITMAP(sb)) {
1465 vfree(SB_AP_BITMAP(sb));
1466 SB_AP_BITMAP(sb) = NULL;
1467 }
1468}