blob: 59f786dded49ce2a8c56dfe45ddab9f86f1d01b2 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700155 RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of "
156 "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700157 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700159 if (!bi) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400160 reiserfs_error(s, "jdm-4055", "NULL bitmap info pointer "
161 "for bitmap %d", bmap_n);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 return 0;
163 }
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700164
Jeff Mahoney50652272006-09-30 23:28:44 -0700165 bh = reiserfs_read_bitmap_block(s, bmap_n);
166 if (bh == NULL)
167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 while (1) {
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400170cont:
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700171 if (bi->free_count < min) {
172 brelse(bh);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400173 return 0; /* No free blocks in this bitmap */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800176 /* search for a first zero bit -- beginning of a window */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 *beg = reiserfs_find_next_zero_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700178 ((unsigned long *)(bh->b_data), boundary, *beg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179
Jeff Mahoney098297b2014-04-23 10:00:36 -0400180 /*
181 * search for a zero bit fails or the rest of bitmap block
182 * cannot contain a zero window of minimum size
183 */
184 if (*beg + min > boundary) {
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700185 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
190 continue;
191 /* first zero bit found; we check next bits */
192 for (end = *beg + 1;; end++) {
193 if (end >= *beg + max || end >= boundary
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700194 || reiserfs_test_le_bit(end, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700195 next = end;
196 break;
197 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400198
199 /*
200 * finding the other end of zero bit window requires
201 * looking into journal structures (in case of
202 * searching for free blocks for unformatted nodes)
203 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
205 break;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Jeff Mahoney098297b2014-04-23 10:00:36 -0400208 /*
209 * now (*beg) points to beginning of zero bits window,
210 * (end) points to one bit after the window end
211 */
212
213 /* found window of proper size */
214 if (end - *beg >= min) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700215 int i;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700216 reiserfs_prepare_for_journal(s, bh, 1);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400217 /*
218 * try to set all blocks used checking are
219 * they still free
220 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700221 for (i = *beg; i < end; i++) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400222 /* Don't check in journal again. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700223 if (reiserfs_test_and_set_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700224 (i, bh->b_data)) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400225 /*
226 * bit was set by another process while
227 * we slept in prepare_for_journal()
228 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229 PROC_INFO_INC(s, scan_bitmap.stolen);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400230
231 /*
232 * we can continue with smaller set
233 * of allocated blocks, if length of
234 * this set is more or equal to `min'
235 */
236 if (i >= *beg + min) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 end = i;
238 break;
239 }
Jeff Mahoney098297b2014-04-23 10:00:36 -0400240
241 /*
242 * otherwise we clear all bit
243 * were set ...
244 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 while (--i >= *beg)
Akinobu Mita0c2fd1bfb2011-07-25 17:13:37 -0700246 reiserfs_clear_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700247 (i, bh->b_data);
248 reiserfs_restore_prepared_buffer(s, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 *beg = org;
Jeff Mahoney098297b2014-04-23 10:00:36 -0400250
251 /*
252 * Search again in current block
253 * from beginning
254 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 goto cont;
256 }
257 }
258 bi->free_count -= (end - *beg);
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400259 journal_mark_dirty(th, bh);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700260 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261
262 /* free block count calculation */
263 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
264 1);
265 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400266 journal_mark_dirty(th, SB_BUFFER_WITH_SB(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700267
268 return end - (*beg);
269 } else {
270 *beg = next;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275static int bmap_hash_id(struct super_block *s, u32 id)
276{
277 char *hash_in = NULL;
278 unsigned long hash;
279 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281 if (id <= 2) {
282 bm = 1;
283 } else {
284 hash_in = (char *)(&id);
285 hash = keyed_hash(hash_in, 4);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700286 bm = hash % reiserfs_bmap_count(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700287 if (!bm)
288 bm = 1;
289 }
290 /* this can only be true when SB_BMAP_NR = 1 */
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700291 if (bm >= reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 bm = 0;
293 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
297 * hashes the id and then returns > 0 if the block group for the
298 * corresponding hash is full
299 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700300static inline int block_group_used(struct super_block *s, u32 id)
301{
Jeff Mahoney50652272006-09-30 23:28:44 -0700302 int bm = bmap_hash_id(s, id);
303 struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
304
Jeff Mahoney098297b2014-04-23 10:00:36 -0400305 /*
306 * If we don't have cached information on this bitmap block, we're
Jeff Mahoney50652272006-09-30 23:28:44 -0700307 * going to have to load it later anyway. Loading it here allows us
Joe Perchesc78bad12008-02-03 17:33:42 +0200308 * to make a better decision. This favors long-term performance gain
Jeff Mahoney50652272006-09-30 23:28:44 -0700309 * with a better on-disk layout vs. a short term gain of skipping the
Jeff Mahoney098297b2014-04-23 10:00:36 -0400310 * read and potentially having a bad placement.
311 */
Jeff Mahoney4d208512007-10-18 23:39:26 -0700312 if (info->free_count == UINT_MAX) {
Jeff Mahoney50652272006-09-30 23:28:44 -0700313 struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
314 brelse(bh);
315 }
316
317 if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700318 return 0;
319 }
320 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * the packing is returned in disk byte order
325 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700327{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700328 __le32 packing;
329 if (TEST_OPTION(packing_groups, dir->i_sb)) {
330 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
331 /*
332 * some versions of reiserfsck expect packing locality 1 to be
333 * special
334 */
335 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
336 packing = INODE_PKEY(dir)->k_objectid;
337 else
338 packing = INODE_PKEY(dir)->k_dir_id;
339 } else
340 packing = INODE_PKEY(dir)->k_objectid;
341 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343
Jeff Mahoney098297b2014-04-23 10:00:36 -0400344/*
345 * Tries to find contiguous zero bit window (given size) in given region of
346 * bitmap and place new blocks there. Returns number of allocated blocks.
347 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700348static int scan_bitmap(struct reiserfs_transaction_handle *th,
349 b_blocknr_t * start, b_blocknr_t finish,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700350 int min, int max, int unfm, sector_t file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700352 int nr_allocated = 0;
353 struct super_block *s = th->t_super;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700354 unsigned int bm, off;
355 unsigned int end_bm, end_off;
356 unsigned int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700358 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360 PROC_INFO_INC(s, scan_bitmap.call);
Jeff Mahoney098297b2014-04-23 10:00:36 -0400361
362 /* No point in looking for more free blocks */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363 if (SB_FREE_BLOCKS(s) <= 0)
Jeff Mahoney098297b2014-04-23 10:00:36 -0400364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700366 get_bit_address(s, *start, &bm, &off);
367 get_bit_address(s, finish, &end_bm, &end_off);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700368 if (bm > reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 return 0;
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700370 if (end_bm > reiserfs_bmap_count(s))
371 end_bm = reiserfs_bmap_count(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jeff Mahoney098297b2014-04-23 10:00:36 -0400373 /*
374 * When the bitmap is more than 10% free, anyone can allocate.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 * When it's less than 10% free, only files that already use the
376 * bitmap are allowed. Once we pass 80% full, this restriction
377 * is lifted.
378 *
379 * We do this so that files that grow later still have space close to
380 * their original allocation. This improves locality, and presumably
381 * performance as a result.
382 *
383 * This is only an allocation policy and does not make up for getting a
384 * bad hint. Decent hinting must be implemented for this to work well.
385 */
386 if (TEST_OPTION(skip_busy, s)
387 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
388 for (; bm < end_bm; bm++, off = 0) {
389 if ((off && (!unfm || (file_block != 0)))
390 || SB_AP_BITMAP(s)[bm].free_count >
391 (s->s_blocksize << 3) / 10)
392 nr_allocated =
393 scan_bitmap_block(th, bm, &off, off_max,
394 min, max, unfm);
395 if (nr_allocated)
396 goto ret;
397 }
398 /* we know from above that start is a reasonable number */
399 get_bit_address(s, *start, &bm, &off);
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700402 for (; bm < end_bm; bm++, off = 0) {
403 nr_allocated =
404 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
405 if (nr_allocated)
406 goto ret;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 nr_allocated =
410 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
411
Jeff Mahoneycf776a72014-04-23 10:00:41 -0400412ret:
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 *start = bm * off_max + off;
414 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416}
417
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700418static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
419 struct inode *inode, b_blocknr_t block,
420 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700422 struct super_block *s = th->t_super;
423 struct reiserfs_super_block *rs;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700424 struct buffer_head *sbh, *bmbh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700425 struct reiserfs_bitmap_info *apbi;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700426 unsigned int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430 PROC_INFO_INC(s, free_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432 rs = SB_DISK_SUPER_BLOCK(s);
433 sbh = SB_BUFFER_WITH_SB(s);
434 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700438 if (nr >= reiserfs_bmap_count(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400439 reiserfs_error(s, "vs-4075", "block %lu is out of range",
440 block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700441 return;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Jeff Mahoney50652272006-09-30 23:28:44 -0700444 bmbh = reiserfs_read_bitmap_block(s, nr);
445 if (!bmbh)
446 return;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700447
448 reiserfs_prepare_for_journal(s, bmbh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 /* clear bit for the given block in bit map */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700451 if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400452 reiserfs_error(s, "vs-4080",
453 "block %lu: bit already cleared", block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454 }
455 apbi[nr].free_count++;
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400456 journal_mark_dirty(th, bmbh);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700457 brelse(bmbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 reiserfs_prepare_for_journal(s, sbh, 1);
460 /* update super block */
461 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Jeff Mahoney09f1b802014-04-23 10:00:39 -0400463 journal_mark_dirty(th, sbh);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400464 if (for_unformatted) {
465 int depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500466 dquot_free_block_nodirty(inode, 1);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400467 reiserfs_write_lock_nested(s, depth);
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471void reiserfs_free_block(struct reiserfs_transaction_handle *th,
472 struct inode *inode, b_blocknr_t block,
473 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 struct super_block *s = th->t_super;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700476 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700479 if (!is_reusable(s, block, 1))
480 return;
481
482 if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400483 reiserfs_error(th->t_super, "bitmap-4072",
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700484 "Trying to free block outside file system "
485 "boundaries (%lu > %lu)",
486 block, sb_block_count(REISERFS_SB(s)->s_rs));
487 return;
488 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700489 /* mark it before we clear it, just in case */
490 journal_mark_freed(th, s, block);
491 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
496 struct inode *inode, b_blocknr_t block)
497{
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700498 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700499 RFALSE(!th->t_super,
500 "vs-4060: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700501 if (!is_reusable(th->t_super, block, 1))
502 return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700503 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506static void __discard_prealloc(struct reiserfs_transaction_handle *th,
507 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700509 unsigned long save = ei->i_prealloc_block;
510 int dirty = 0;
511 struct inode *inode = &ei->vfs_inode;
512 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 if (ei->i_prealloc_count < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400515 reiserfs_error(th->t_super, "zam-4001",
516 "inode has negative prealloc blocks count.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700518 while (ei->i_prealloc_count > 0) {
519 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
520 ei->i_prealloc_block++;
521 ei->i_prealloc_count--;
522 dirty = 1;
523 }
524 if (dirty)
525 reiserfs_update_sd(th, inode);
526 ei->i_prealloc_block = save;
Jeff Mahoneya228bf82014-04-23 10:00:42 -0400527 list_del_init(&ei->i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
532 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 struct reiserfs_inode_info *ei = REISERFS_I(inode);
535 BUG_ON(!th->t_trans_id);
536 if (ei->i_prealloc_count)
537 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700540void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700542 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700544 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 while (!list_empty(plist)) {
547 struct reiserfs_inode_info *ei;
548 ei = list_entry(plist->next, struct reiserfs_inode_info,
549 i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700551 if (!ei->i_prealloc_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400552 reiserfs_error(th->t_super, "zam-4001",
553 "inode is in prealloc list but has "
554 "no preallocated blocks.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700557 __discard_prealloc(th, ei);
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700563 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
564 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
565 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700569int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700571 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Jeff Mahoney098297b2014-04-23 10:00:36 -0400573 /* clear default settings */
574 REISERFS_SB(s)->s_alloc_options.bits = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576 while ((this_char = strsep(&options, ":")) != NULL) {
577 if ((value = strchr(this_char, '=')) != NULL)
578 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700580 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
581 int temp;
582 SET_OPTION(concentrating_formatted_nodes);
583 temp = (value
584 && *value) ? simple_strtoul(value, &value,
585 0) : 10;
586 if (temp <= 0 || temp > 100) {
587 REISERFS_SB(s)->s_alloc_options.border = 10;
588 } else {
589 REISERFS_SB(s)->s_alloc_options.border =
590 100 / temp;
591 }
592 continue;
593 }
594 if (!strcmp(this_char, "displacing_large_files")) {
595 SET_OPTION(displacing_large_files);
596 REISERFS_SB(s)->s_alloc_options.large_file_size =
597 (value
598 && *value) ? simple_strtoul(value, &value, 0) : 16;
599 continue;
600 }
601 if (!strcmp(this_char, "displacing_new_packing_localities")) {
602 SET_OPTION(displacing_new_packing_localities);
603 continue;
604 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700606 if (!strcmp(this_char, "old_hashed_relocation")) {
607 SET_OPTION(old_hashed_relocation);
608 continue;
609 }
610
611 if (!strcmp(this_char, "new_hashed_relocation")) {
612 SET_OPTION(new_hashed_relocation);
613 continue;
614 }
615
616 if (!strcmp(this_char, "dirid_groups")) {
617 SET_OPTION(dirid_groups);
618 continue;
619 }
620 if (!strcmp(this_char, "oid_groups")) {
621 SET_OPTION(oid_groups);
622 continue;
623 }
624 if (!strcmp(this_char, "packing_groups")) {
625 SET_OPTION(packing_groups);
626 continue;
627 }
628 if (!strcmp(this_char, "hashed_formatted_nodes")) {
629 SET_OPTION(hashed_formatted_nodes);
630 continue;
631 }
632
633 if (!strcmp(this_char, "skip_busy")) {
634 SET_OPTION(skip_busy);
635 continue;
636 }
637
638 if (!strcmp(this_char, "hundredth_slices")) {
639 SET_OPTION(hundredth_slices);
640 continue;
641 }
642
643 if (!strcmp(this_char, "old_way")) {
644 SET_OPTION(old_way);
645 continue;
646 }
647
648 if (!strcmp(this_char, "displace_based_on_dirid")) {
649 SET_OPTION(displace_based_on_dirid);
650 continue;
651 }
652
653 if (!strcmp(this_char, "preallocmin")) {
654 REISERFS_SB(s)->s_alloc_options.preallocmin =
655 (value
656 && *value) ? simple_strtoul(value, &value, 0) : 4;
657 continue;
658 }
659
660 if (!strcmp(this_char, "preallocsize")) {
661 REISERFS_SB(s)->s_alloc_options.preallocsize =
662 (value
663 && *value) ? simple_strtoul(value, &value,
664 0) :
665 PREALLOCATION_SIZE;
666 continue;
667 }
668
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400669 reiserfs_warning(s, "zam-4001", "unknown option - %s",
670 this_char);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Jeff Mahoney1d889d92009-03-30 14:02:20 -0400674 reiserfs_info(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Jan Karac3aa0772011-12-21 20:17:10 +0100678static void print_sep(struct seq_file *seq, int *first)
679{
680 if (!*first)
681 seq_puts(seq, ":");
682 else
683 *first = 0;
684}
685
686void show_alloc_options(struct seq_file *seq, struct super_block *s)
687{
688 int first = 1;
689
690 if (SB_ALLOC_OPTS(s) == ((1 << _ALLOC_skip_busy) |
691 (1 << _ALLOC_dirid_groups) | (1 << _ALLOC_packing_groups)))
692 return;
693
694 seq_puts(seq, ",alloc=");
695
696 if (TEST_OPTION(concentrating_formatted_nodes, s)) {
697 print_sep(seq, &first);
698 if (REISERFS_SB(s)->s_alloc_options.border != 10) {
699 seq_printf(seq, "concentrating_formatted_nodes=%d",
700 100 / REISERFS_SB(s)->s_alloc_options.border);
701 } else
702 seq_puts(seq, "concentrating_formatted_nodes");
703 }
704 if (TEST_OPTION(displacing_large_files, s)) {
705 print_sep(seq, &first);
706 if (REISERFS_SB(s)->s_alloc_options.large_file_size != 16) {
707 seq_printf(seq, "displacing_large_files=%lu",
708 REISERFS_SB(s)->s_alloc_options.large_file_size);
709 } else
710 seq_puts(seq, "displacing_large_files");
711 }
712 if (TEST_OPTION(displacing_new_packing_localities, s)) {
713 print_sep(seq, &first);
714 seq_puts(seq, "displacing_new_packing_localities");
715 }
716 if (TEST_OPTION(old_hashed_relocation, s)) {
717 print_sep(seq, &first);
718 seq_puts(seq, "old_hashed_relocation");
719 }
720 if (TEST_OPTION(new_hashed_relocation, s)) {
721 print_sep(seq, &first);
722 seq_puts(seq, "new_hashed_relocation");
723 }
724 if (TEST_OPTION(dirid_groups, s)) {
725 print_sep(seq, &first);
726 seq_puts(seq, "dirid_groups");
727 }
728 if (TEST_OPTION(oid_groups, s)) {
729 print_sep(seq, &first);
730 seq_puts(seq, "oid_groups");
731 }
732 if (TEST_OPTION(packing_groups, s)) {
733 print_sep(seq, &first);
734 seq_puts(seq, "packing_groups");
735 }
736 if (TEST_OPTION(hashed_formatted_nodes, s)) {
737 print_sep(seq, &first);
738 seq_puts(seq, "hashed_formatted_nodes");
739 }
740 if (TEST_OPTION(skip_busy, s)) {
741 print_sep(seq, &first);
742 seq_puts(seq, "skip_busy");
743 }
744 if (TEST_OPTION(hundredth_slices, s)) {
745 print_sep(seq, &first);
746 seq_puts(seq, "hundredth_slices");
747 }
748 if (TEST_OPTION(old_way, s)) {
749 print_sep(seq, &first);
750 seq_puts(seq, "old_way");
751 }
752 if (TEST_OPTION(displace_based_on_dirid, s)) {
753 print_sep(seq, &first);
754 seq_puts(seq, "displace_based_on_dirid");
755 }
756 if (REISERFS_SB(s)->s_alloc_options.preallocmin != 0) {
757 print_sep(seq, &first);
758 seq_printf(seq, "preallocmin=%d",
759 REISERFS_SB(s)->s_alloc_options.preallocmin);
760 }
761 if (REISERFS_SB(s)->s_alloc_options.preallocsize != 17) {
762 print_sep(seq, &first);
763 seq_printf(seq, "preallocsize=%d",
764 REISERFS_SB(s)->s_alloc_options.preallocsize);
765 }
766}
767
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
769{
770 char *hash_in;
771 if (hint->formatted_node) {
772 hash_in = (char *)&hint->key.k_dir_id;
773 } else {
774 if (!hint->inode) {
Jeff Mahoney098297b2014-04-23 10:00:36 -0400775 /*hint->search_start = hint->beg;*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700776 hash_in = (char *)&hint->key.k_dir_id;
777 } else
778 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
779 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
780 else
781 hash_in =
782 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
783 }
784
785 hint->search_start =
786 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789/*
790 * Relocation based on dirid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200791 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700795 unsigned long hash;
796 __u32 dirid = 0;
797 int bm = 0;
798 struct super_block *sb = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
801 else if (hint->formatted_node)
802 dirid = hint->key.k_dir_id;
803
804 if (dirid) {
805 bm = bmap_hash_id(sb, dirid);
806 hash = bm * (sb->s_blocksize << 3);
807 /* give a portion of the block group to metadata */
808 if (hint->inode)
809 hash += sb->s_blocksize / 2;
810 hint->search_start = hash;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
814/*
815 * Relocation based on oid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200816 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700820 if (hint->inode) {
821 unsigned long hash;
822 __u32 oid;
823 __u32 dirid;
824 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700826 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Jeff Mahoney098297b2014-04-23 10:00:36 -0400828 /*
829 * keep the root dir and it's first set of subdirs close to
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700830 * the start of the disk
831 */
832 if (dirid <= 2)
833 hash = (hint->inode->i_sb->s_blocksize << 3);
834 else {
835 oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
836 bm = bmap_hash_id(hint->inode->i_sb, oid);
837 hash = bm * (hint->inode->i_sb->s_blocksize << 3);
838 }
839 hint->search_start = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Jeff Mahoney098297b2014-04-23 10:00:36 -0400843/*
844 * returns 1 if it finds an indirect item and gets valid hint info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * from it, otherwise 0
846 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700847static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800849 struct treepath *path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700850 struct buffer_head *bh;
851 struct item_head *ih;
852 int pos_in_item;
853 __le32 *item;
854 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Jeff Mahoney098297b2014-04-23 10:00:36 -0400856 /*
857 * reiserfs code can call this function w/o pointer to path
858 * structure supplied; then we rely on supplied search_start
859 */
860 if (!hint->path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700863 path = hint->path;
864 bh = get_last_bh(path);
865 RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400866 ih = tp_item_head(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700867 pos_in_item = path->pos_in_item;
Jeff Mahoney4cf5f7a2014-04-23 10:00:35 -0400868 item = tp_item_body(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Jeff Mahoney098297b2014-04-23 10:00:36 -0400872 /*
873 * for indirect item: go to left and look for the first non-hole entry
874 * in the indirect item
875 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700876 if (!hint->formatted_node && is_indirect_le_ih(ih)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700877 if (pos_in_item == I_UNFM_NUM(ih))
878 pos_in_item--;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700879 while (pos_in_item >= 0) {
880 int t = get_block_num(item, pos_in_item);
881 if (t) {
882 hint->search_start = t;
883 ret = 1;
884 break;
885 }
886 pos_in_item--;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700890 /* does result value fit into specified region? */
891 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
Jeff Mahoney098297b2014-04-23 10:00:36 -0400894/*
895 * should be, if formatted node, then try to put on first part of the device
896 * specified as number of percent with mount option device, else try to put
897 * on last of device. This is not to say it is good code to do so,
898 * but the effect should be measured.
899 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700900static inline void set_border_in_hint(struct super_block *s,
901 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700903 b_blocknr_t border =
904 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700906 if (hint->formatted_node)
907 hint->end = border - 1;
908 else
909 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700912static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
915 hint->search_start =
916 hint->beg +
917 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
918 4) % (hint->end - hint->beg);
919 else
920 hint->search_start =
921 hint->beg +
922 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
923 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700926static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700928 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700930 if (!hint->inode)
931 hash_in = (char *)&hint->key.k_dir_id;
932 else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
933 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
934 else
935 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700937 hint->search_start =
938 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700941static inline int
942this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
943 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700945 return hint->block ==
946 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700950static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700952 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 hint->th->displace_new_blocks = 0;
955 hint->search_start =
956 hint->beg + keyed_hash((char *)(&key->k_objectid),
957 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700961static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
962{
963 b_blocknr_t border;
964 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966 if (hint->formatted_node || hint->inode == NULL) {
967 return 0;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700970 hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
971 border =
972 hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
973 4) % (hint->end - hint->beg - 1);
974 if (border > hint->search_start)
975 hint->search_start = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977 return 1;
978}
979
980static inline int old_way(reiserfs_blocknr_hint_t * hint)
981{
982 b_blocknr_t border;
983
984 if (hint->formatted_node || hint->inode == NULL) {
985 return 0;
986 }
987
988 border =
989 hint->beg +
990 le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
991 hint->beg);
992 if (border > hint->search_start)
993 hint->search_start = border;
994
995 return 1;
996}
997
998static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
999{
1000 struct in_core_key *key = &hint->key;
1001 b_blocknr_t slice_start;
1002
1003 slice_start =
1004 (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
1005 if (slice_start > hint->search_start
1006 || slice_start + (hint->end / 100) <= hint->search_start) {
1007 hint->search_start = slice_start;
1008 }
1009}
1010
1011static void determine_search_start(reiserfs_blocknr_hint_t * hint,
1012 int amount_needed)
1013{
1014 struct super_block *s = hint->th->t_super;
1015 int unfm_hint;
1016
1017 hint->beg = 0;
1018 hint->end = SB_BLOCK_COUNT(s) - 1;
1019
1020 /* This is former border algorithm. Now with tunable border offset */
1021 if (concentrating_formatted_nodes(s))
1022 set_border_in_hint(s, hint);
1023
1024#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoney098297b2014-04-23 10:00:36 -04001025 /*
1026 * whenever we create a new directory, we displace it. At first
1027 * we will hash for location, later we might look for a moderately
1028 * empty place for it
1029 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001030 if (displacing_new_packing_localities(s)
1031 && hint->th->displace_new_blocks) {
1032 displace_new_packing_locality(hint);
1033
Jeff Mahoney098297b2014-04-23 10:00:36 -04001034 /*
1035 * we do not continue determine_search_start,
1036 * if new packing locality is being displaced
1037 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001038 return;
1039 }
1040#endif
1041
Jeff Mahoney098297b2014-04-23 10:00:36 -04001042 /*
1043 * all persons should feel encouraged to add more special cases
1044 * here and test them
1045 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001046
1047 if (displacing_large_files(s) && !hint->formatted_node
1048 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
1049 displace_large_file(hint);
1050 return;
1051 }
1052
Jeff Mahoney098297b2014-04-23 10:00:36 -04001053 /*
1054 * if none of our special cases is relevant, use the left
1055 * neighbor in the tree order of the new node we are allocating for
1056 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057 if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
1058 hash_formatted_node(hint);
1059 return;
1060 }
1061
1062 unfm_hint = get_left_neighbor(hint);
1063
Jeff Mahoney098297b2014-04-23 10:00:36 -04001064 /*
1065 * Mimic old block allocator behaviour, that is if VFS allowed for
1066 * preallocation, new blocks are displaced based on directory ID.
1067 * Also, if suggested search_start is less than last preallocated
1068 * block, we start searching from it, assuming that HDD dataflow
1069 * is faster in forward direction
1070 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001071 if (TEST_OPTION(old_way, s)) {
1072 if (!hint->formatted_node) {
1073 if (!reiserfs_hashed_relocation(s))
1074 old_way(hint);
1075 else if (!reiserfs_no_unhashed_relocation(s))
1076 old_hashed_relocation(hint);
1077
1078 if (hint->inode
1079 && hint->search_start <
1080 REISERFS_I(hint->inode)->i_prealloc_block)
1081 hint->search_start =
1082 REISERFS_I(hint->inode)->i_prealloc_block;
1083 }
1084 return;
1085 }
1086
1087 /* This is an approach proposed by Hans */
1088 if (TEST_OPTION(hundredth_slices, s)
1089 && !(displacing_large_files(s) && !hint->formatted_node)) {
1090 hundredth_slices(hint);
1091 return;
1092 }
1093
1094 /* old_hashed_relocation only works on unformatted */
1095 if (!unfm_hint && !hint->formatted_node &&
1096 TEST_OPTION(old_hashed_relocation, s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001099
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001100 /* new_hashed_relocation works with both formatted/unformatted nodes */
1101 if ((!unfm_hint || hint->formatted_node) &&
1102 TEST_OPTION(new_hashed_relocation, s)) {
1103 new_hashed_relocation(hint);
1104 }
Jeff Mahoney098297b2014-04-23 10:00:36 -04001105
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 /* dirid grouping works only on unformatted nodes */
1107 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
1108 dirid_groups(hint);
1109 }
1110#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1111 if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
1112 dirid_groups(hint);
1113 }
1114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001116 /* oid grouping works only on unformatted nodes */
1117 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
1118 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
1124{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001125 /* make minimum size a mount option and benchmark both ways */
1126 /* we preallocate blocks only for regular files, specific size */
1127 /* benchmark preallocating always and see what happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001129 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001131 if (!hint->formatted_node && hint->preallocate) {
1132 if (S_ISREG(hint->inode->i_mode)
1133 && hint->inode->i_size >=
1134 REISERFS_SB(hint->th->t_super)->s_alloc_options.
1135 preallocmin * hint->inode->i_sb->s_blocksize)
1136 hint->prealloc_size =
1137 REISERFS_SB(hint->th->t_super)->s_alloc_options.
1138 preallocsize - 1;
1139 }
1140 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
1144 b_blocknr_t * new_blocknrs,
1145 b_blocknr_t start,
1146 b_blocknr_t finish, int min,
1147 int amount_needed,
1148 int prealloc_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 int rest = amount_needed;
1151 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001153 while (rest > 0 && start <= finish) {
1154 nr_allocated = scan_bitmap(hint->th, &start, finish, min,
1155 rest + prealloc_size,
1156 !hint->formatted_node, hint->block);
1157
1158 if (nr_allocated == 0) /* no new blocks allocated, return */
1159 break;
1160
1161 /* fill free_blocknrs array first */
1162 while (rest > 0 && nr_allocated > 0) {
1163 *new_blocknrs++ = start++;
1164 rest--;
1165 nr_allocated--;
1166 }
1167
1168 /* do we have something to fill prealloc. array also ? */
1169 if (nr_allocated > 0) {
Jeff Mahoney098297b2014-04-23 10:00:36 -04001170 /*
1171 * it means prealloc_size was greater that 0 and
1172 * we do preallocation
1173 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001174 list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
1175 &SB_JOURNAL(hint->th->t_super)->
1176 j_prealloc_list);
1177 REISERFS_I(hint->inode)->i_prealloc_block = start;
1178 REISERFS_I(hint->inode)->i_prealloc_count =
1179 nr_allocated;
1180 break;
1181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001184 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
1187static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001188 (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
1189 int amount_needed) {
1190 struct super_block *s = hint->th->t_super;
1191 b_blocknr_t start = hint->search_start;
1192 b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
1193 int passno = 0;
1194 int nr_allocated = 0;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001195 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001197 determine_prealloc_size(hint);
1198 if (!hint->formatted_node) {
1199 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001201 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1202 "reiserquota: allocating %d blocks id=%u",
1203 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001205 depth = reiserfs_write_unlock_nested(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001206 quota_ret =
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001207 dquot_alloc_block_nodirty(hint->inode, amount_needed);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001208 if (quota_ret) { /* Quota exceeded? */
1209 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001210 return QUOTA_EXCEEDED;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001211 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001212 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001214 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1215 "reiserquota: allocating (prealloc) %d blocks id=%u",
1216 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217#endif
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001218 quota_ret = dquot_prealloc_block_nodirty(hint->inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219 hint->prealloc_size);
1220 if (quota_ret)
1221 hint->preallocate = hint->prealloc_size = 0;
1222 }
1223 /* for unformatted nodes, force large allocations */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001224 reiserfs_write_lock_nested(s, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001227 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001228 switch (passno++) {
1229 case 0: /* Search from hint->search_start to end of disk */
1230 start = hint->search_start;
1231 finish = SB_BLOCK_COUNT(s) - 1;
1232 break;
1233 case 1: /* Search from hint->beg to hint->search_start */
1234 start = hint->beg;
1235 finish = hint->search_start;
1236 break;
1237 case 2: /* Last chance: Search from 0 to hint->beg */
1238 start = 0;
1239 finish = hint->beg;
1240 break;
Jeff Mahoney098297b2014-04-23 10:00:36 -04001241 default:
1242 /* We've tried searching everywhere, not enough space */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001243 /* Free the blocks */
1244 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1247 "reiserquota: freeing (nospace) %d blocks id=%u",
1248 amount_needed +
1249 hint->prealloc_size -
1250 nr_allocated,
1251 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#endif
Jan Kara77db4f22009-01-26 17:14:18 +01001253 /* Free not allocated blocks */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001254 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001255 dquot_free_block_nodirty(hint->inode,
Jan Kara77db4f22009-01-26 17:14:18 +01001256 amount_needed + hint->prealloc_size -
1257 nr_allocated);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001258 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001259 }
1260 while (nr_allocated--)
1261 reiserfs_free_block(hint->th, hint->inode,
1262 new_blocknrs[nr_allocated],
1263 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 return NO_DISK_SPACE;
1266 }
1267 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1268 new_blocknrs +
1269 nr_allocated,
1270 start, finish,
Jeff Mahoney9ea0f942006-09-30 23:28:45 -07001271 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001272 amount_needed -
1273 nr_allocated,
1274 hint->
1275 prealloc_size))
1276 < amount_needed);
1277 if (!hint->formatted_node &&
1278 amount_needed + hint->prealloc_size >
1279 nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
1280 /* Some of preallocation blocks were not allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001282 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1283 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1284 amount_needed + hint->prealloc_size -
1285 nr_allocated -
1286 REISERFS_I(hint->inode)->i_prealloc_count,
1287 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001289
1290 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001291 dquot_free_block_nodirty(hint->inode, amount_needed +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001292 hint->prealloc_size - nr_allocated -
1293 REISERFS_I(hint->inode)->
1294 i_prealloc_count);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001295 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001298 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/* grab new blocknrs from preallocated list */
1302/* return amount still needed after using them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001303static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
1304 b_blocknr_t * new_blocknrs,
1305 int amount_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307 struct inode *inode = hint->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001309 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1310 while (amount_needed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001312 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1313 REISERFS_I(inode)->i_prealloc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001315 amount_needed--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001317 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1318 list_del(&REISERFS_I(inode)->i_prealloc_list);
1319 break;
1320 }
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001323 /* return amount still needed after using preallocated blocks */
1324 return amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Jeff Mahoney098297b2014-04-23 10:00:36 -04001327int reiserfs_allocate_blocknrs(reiserfs_blocknr_hint_t *hint,
1328 b_blocknr_t *new_blocknrs,
1329 int amount_needed,
1330 /* Amount of blocks we have already reserved */
1331 int reserved_by_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001333 int initial_amount_needed = amount_needed;
1334 int ret;
1335 struct super_block *s = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001337 /* Check if there is enough space, taking into account reserved space */
1338 if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
1339 amount_needed - reserved_by_us)
1340 return NO_DISK_SPACE;
1341 /* should this be if !hint->inode && hint->preallocate? */
1342 /* do you mean hint->formatted_node can be removed ? - Zam */
Jeff Mahoney098297b2014-04-23 10:00:36 -04001343 /*
1344 * hint->formatted_node cannot be removed because we try to access
1345 * inode information here, and there is often no inode associated with
1346 * metadata allocations - green
1347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001349 if (!hint->formatted_node && hint->preallocate) {
1350 amount_needed = use_preallocated_list_if_available
1351 (hint, new_blocknrs, amount_needed);
Jeff Mahoney098297b2014-04-23 10:00:36 -04001352
1353 /*
1354 * We have all the block numbers we need from the
1355 * prealloc list
1356 */
1357 if (amount_needed == 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001358 return CARRY_ON;
1359 new_blocknrs += (initial_amount_needed - amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001361
1362 /* find search start and save it in hint structure */
1363 determine_search_start(hint, amount_needed);
1364 if (hint->search_start >= SB_BLOCK_COUNT(s))
1365 hint->search_start = SB_BLOCK_COUNT(s) - 1;
1366
1367 /* allocation itself; fill new_blocknrs and preallocation arrays */
1368 ret = blocknrs_and_prealloc_arrays_from_search_start
1369 (hint, new_blocknrs, amount_needed);
1370
Jeff Mahoney098297b2014-04-23 10:00:36 -04001371 /*
1372 * We used prealloc. list to fill (partially) new_blocknrs array.
1373 * If final allocation fails we need to return blocks back to
1374 * prealloc. list or just free them. -- Zam (I chose second
1375 * variant)
1376 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001377 if (ret != CARRY_ON) {
1378 while (amount_needed++ < initial_amount_needed) {
1379 reiserfs_free_block(hint->th, hint->inode,
1380 *(--new_blocknrs), 1);
1381 }
1382 }
1383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Jeff Mahoney6f010462006-09-30 23:28:43 -07001386void reiserfs_cache_bitmap_metadata(struct super_block *sb,
1387 struct buffer_head *bh,
1388 struct reiserfs_bitmap_info *info)
1389{
1390 unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
1391
Jeff Mahoney4d208512007-10-18 23:39:26 -07001392 /* The first bit must ALWAYS be 1 */
Jeff Mahoney0030b642009-03-30 14:02:28 -04001393 if (!reiserfs_test_le_bit(0, (unsigned long *)bh->b_data))
1394 reiserfs_error(sb, "reiserfs-2025", "bitmap block %lu is "
1395 "corrupted: first bit must be 1", bh->b_blocknr);
Jeff Mahoney4d208512007-10-18 23:39:26 -07001396
1397 info->free_count = 0;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001398
1399 while (--cur >= (unsigned long *)bh->b_data) {
Jeff Mahoney6f010462006-09-30 23:28:43 -07001400 /* 0 and ~0 are special, we can optimize for them */
Jeff Mahoney4d208512007-10-18 23:39:26 -07001401 if (*cur == 0)
Jeff Mahoney6f010462006-09-30 23:28:43 -07001402 info->free_count += BITS_PER_LONG;
Jeff Mahoney4d208512007-10-18 23:39:26 -07001403 else if (*cur != ~0L) /* A mix, investigate */
Akinobu Mita9d6bf5a2011-07-25 17:13:38 -07001404 info->free_count += BITS_PER_LONG - hweight_long(*cur);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001405 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001406}
1407
1408struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
1409 unsigned int bitmap)
1410{
1411 b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
Jeff Mahoney50652272006-09-30 23:28:44 -07001412 struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001413 struct buffer_head *bh;
1414
Jeff Mahoney098297b2014-04-23 10:00:36 -04001415 /*
1416 * Way old format filesystems had the bitmaps packed up front.
1417 * I doubt there are any of these left, but just in case...
1418 */
Jeff Mahoney6f010462006-09-30 23:28:43 -07001419 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
Jeff Mahoneya228bf82014-04-23 10:00:42 -04001420 &REISERFS_SB(sb)->s_properties)))
Jeff Mahoney6f010462006-09-30 23:28:43 -07001421 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
1422 else if (bitmap == 0)
1423 block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
1424
Jeff Mahoney50652272006-09-30 23:28:44 -07001425 bh = sb_bread(sb, block);
1426 if (bh == NULL)
Eric Eric Sesterhenn00079e02006-10-06 22:19:45 -07001427 reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001428 "reading failed", __func__, block);
Jeff Mahoney50652272006-09-30 23:28:44 -07001429 else {
1430 if (buffer_locked(bh)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04001431 int depth;
Jeff Mahoney50652272006-09-30 23:28:44 -07001432 PROC_INFO_INC(sb, scan_bitmap.wait);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001433 depth = reiserfs_write_unlock_nested(sb);
Jeff Mahoney50652272006-09-30 23:28:44 -07001434 __wait_on_buffer(bh);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001435 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoney50652272006-09-30 23:28:44 -07001436 }
1437 BUG_ON(!buffer_uptodate(bh));
1438 BUG_ON(atomic_read(&bh->b_count) == 0);
1439
Jeff Mahoney4d208512007-10-18 23:39:26 -07001440 if (info->free_count == UINT_MAX)
Jeff Mahoney50652272006-09-30 23:28:44 -07001441 reiserfs_cache_bitmap_metadata(sb, bh, info);
1442 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001443
1444 return bh;
1445}
1446
1447int reiserfs_init_bitmap_cache(struct super_block *sb)
1448{
1449 struct reiserfs_bitmap_info *bitmap;
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001450 unsigned int bmap_nr = reiserfs_bmap_count(sb);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001451
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001452 bitmap = vmalloc(sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001453 if (bitmap == NULL)
1454 return -ENOMEM;
1455
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001456 memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001457
Jeff Mahoney6f010462006-09-30 23:28:43 -07001458 SB_AP_BITMAP(sb) = bitmap;
1459
1460 return 0;
1461}
Jeff Mahoney50652272006-09-30 23:28:44 -07001462
1463void reiserfs_free_bitmap_cache(struct super_block *sb)
1464{
1465 if (SB_AP_BITMAP(sb)) {
1466 vfree(SB_AP_BITMAP(sb));
1467 SB_AP_BITMAP(sb) = NULL;
1468 }
1469}