blob: 28a1b06081fcf8a804654f70caf279c900416ee7 [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>
7#include <linux/reiserfs_fs.h>
8#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/reiserfs_fs_sb.h>
14#include <linux/reiserfs_fs_i.h>
15#include <linux/quotaops.h>
16
17#define PREALLOCATION_SIZE 9
18
19/* different reiserfs block allocator options */
20
21#define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)
22
23#define _ALLOC_concentrating_formatted_nodes 0
24#define _ALLOC_displacing_large_files 1
25#define _ALLOC_displacing_new_packing_localities 2
26#define _ALLOC_old_hashed_relocation 3
27#define _ALLOC_new_hashed_relocation 4
28#define _ALLOC_skip_busy 5
29#define _ALLOC_displace_based_on_dirid 6
30#define _ALLOC_hashed_formatted_nodes 7
31#define _ALLOC_old_way 8
32#define _ALLOC_hundredth_slices 9
33#define _ALLOC_dirid_groups 10
34#define _ALLOC_oid_groups 11
35#define _ALLOC_packing_groups 12
36
37#define concentrating_formatted_nodes(s) test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
38#define displacing_large_files(s) test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
39#define displacing_new_packing_localities(s) test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))
40
41#define SET_OPTION(optname) \
42 do { \
43 reiserfs_warning(s, "reiserfs: option \"%s\" is set", #optname); \
44 set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
45 } while(0)
46#define TEST_OPTION(optname, s) \
47 test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
48
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070049static inline void get_bit_address(struct super_block *s,
50 b_blocknr_t block, int *bmap_nr, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070052 /* It is in the bitmap block number equal to the block
53 * number divided by the number of bits in a block. */
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070054 *bmap_nr = block >> (s->s_blocksize_bits + 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070055 /* Within that bitmap block it is located at bit offset *offset. */
56 *offset = block & ((s->s_blocksize << 3) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070059int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070061 int bmap, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
64 reiserfs_warning(s,
65 "vs-4010: is_reusable: block number is out of range %lu (%u)",
66 block, SB_BLOCK_COUNT(s));
67 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070070 get_bit_address(s, block, &bmap, &offset);
71
72 /* Old format filesystem? Unlikely, but the bitmaps are all up front so
73 * we need to account for it. */
74 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
75 &(REISERFS_SB(s)->s_properties)))) {
76 b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
77 if (block >= bmap1 && block <= bmap1 + SB_BMAP_NR(s)) {
78 reiserfs_warning(s, "vs: 4019: is_reusable: "
79 "bitmap block %lu(%u) can't be freed or reused",
80 block, SB_BMAP_NR(s));
81 return 0;
82 }
83 } else {
84 if (offset == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085 reiserfs_warning(s, "vs: 4020: is_reusable: "
86 "bitmap block %lu(%u) can't be freed or reused",
87 block, SB_BMAP_NR(s));
88 return 0;
89 }
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070092 if (bmap >= SB_BMAP_NR(s)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093 reiserfs_warning(s,
94 "vs-4030: is_reusable: there is no so many bitmap blocks: "
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070095 "block=%lu, bitmap_nr=%d", block, bmap);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070096 return 0;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
100 reiserfs_warning(s,
101 "vs-4050: is_reusable: this is root block (%u), "
102 "it must be busy", SB_ROOT_BLOCK(s));
103 return 0;
104 }
105
106 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* searches in journal structures for a given block number (bmap, off). If block
110 is found in reiserfs journal it suggests next free block candidate to test. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111static inline int is_block_in_journal(struct super_block *s, int bmap, int
112 off, int *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700114 b_blocknr_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700116 if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
117 if (tmp) { /* hint supplied */
118 *next = tmp;
119 PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
120 } else {
121 (*next) = off + 1; /* inc offset to avoid looping. */
122 PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
123 }
124 PROC_INFO_INC(s, scan_bitmap.retry);
125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
131 * block; */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
133 int bmap_n, int *beg, int boundary, int min,
134 int max, int unfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700136 struct super_block *s = th->t_super;
137 struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700138 struct buffer_head *bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139 int end, next;
140 int org = *beg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
145 bmap_n, SB_BMAP_NR(s) - 1);
146 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* this is unclear and lacks comments, explain how journal bitmaps
148 work here for the reader. Convey a sense of the design here. What
149 is a window? */
150/* - I mean `a window of zero bits' as in description of this function - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152 if (!bi) {
153 reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d",
154 bmap_n);
155 return 0;
156 }
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700157
Jeff Mahoney50652272006-09-30 23:28:44 -0700158 bh = reiserfs_read_bitmap_block(s, bmap_n);
159 if (bh == NULL)
160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 while (1) {
163 cont:
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700164 if (bi->free_count < min) {
165 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700166 return 0; // No free blocks in this bitmap
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169 /* search for a first zero bit -- beggining of a window */
170 *beg = reiserfs_find_next_zero_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700171 ((unsigned long *)(bh->b_data), boundary, *beg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700172
173 if (*beg + min > boundary) { /* search for a zero bit fails or the rest of bitmap block
174 * cannot contain a zero window of minimum size */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700175 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700179 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
180 continue;
181 /* first zero bit found; we check next bits */
182 for (end = *beg + 1;; end++) {
183 if (end >= *beg + max || end >= boundary
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700184 || reiserfs_test_le_bit(end, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700185 next = end;
186 break;
187 }
188 /* finding the other end of zero bit window requires looking into journal structures (in
189 * case of searching for free blocks for unformatted nodes) */
190 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
191 break;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700194 /* now (*beg) points to beginning of zero bits window,
195 * (end) points to one bit after the window end */
196 if (end - *beg >= min) { /* it seems we have found window of proper size */
197 int i;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700198 reiserfs_prepare_for_journal(s, bh, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 /* try to set all blocks used checking are they still free */
200 for (i = *beg; i < end; i++) {
201 /* It seems that we should not check in journal again. */
202 if (reiserfs_test_and_set_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700203 (i, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 /* bit was set by another process
205 * while we slept in prepare_for_journal() */
206 PROC_INFO_INC(s, scan_bitmap.stolen);
207 if (i >= *beg + min) { /* we can continue with smaller set of allocated blocks,
208 * if length of this set is more or equal to `min' */
209 end = i;
210 break;
211 }
212 /* otherwise we clear all bit were set ... */
213 while (--i >= *beg)
214 reiserfs_test_and_clear_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700215 (i, bh->b_data);
216 reiserfs_restore_prepared_buffer(s, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700217 *beg = org;
218 /* ... and search again in current block from beginning */
219 goto cont;
220 }
221 }
222 bi->free_count -= (end - *beg);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700223 journal_mark_dirty(th, s, bh);
224 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700225
226 /* free block count calculation */
227 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
228 1);
229 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
230 journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
231
232 return end - (*beg);
233 } else {
234 *beg = next;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700239static int bmap_hash_id(struct super_block *s, u32 id)
240{
241 char *hash_in = NULL;
242 unsigned long hash;
243 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700245 if (id <= 2) {
246 bm = 1;
247 } else {
248 hash_in = (char *)(&id);
249 hash = keyed_hash(hash_in, 4);
250 bm = hash % SB_BMAP_NR(s);
251 if (!bm)
252 bm = 1;
253 }
254 /* this can only be true when SB_BMAP_NR = 1 */
255 if (bm >= SB_BMAP_NR(s))
256 bm = 0;
257 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * hashes the id and then returns > 0 if the block group for the
262 * corresponding hash is full
263 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264static inline int block_group_used(struct super_block *s, u32 id)
265{
Jeff Mahoney50652272006-09-30 23:28:44 -0700266 int bm = bmap_hash_id(s, id);
267 struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
268
269 /* If we don't have cached information on this bitmap block, we're
270 * going to have to load it later anyway. Loading it here allows us
271 * to make a better decision. This favors long-term performace gain
272 * with a better on-disk layout vs. a short term gain of skipping the
273 * read and potentially having a bad placement. */
274 if (info->first_zero_hint == 0) {
275 struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
276 brelse(bh);
277 }
278
279 if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700280 return 0;
281 }
282 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286 * the packing is returned in disk byte order
287 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700288__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700289{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290 __le32 packing;
291 if (TEST_OPTION(packing_groups, dir->i_sb)) {
292 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
293 /*
294 * some versions of reiserfsck expect packing locality 1 to be
295 * special
296 */
297 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
298 packing = INODE_PKEY(dir)->k_objectid;
299 else
300 packing = INODE_PKEY(dir)->k_dir_id;
301 } else
302 packing = INODE_PKEY(dir)->k_objectid;
303 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* Tries to find contiguous zero bit window (given size) in given region of
307 * bitmap and place new blocks there. Returns number of allocated blocks. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308static int scan_bitmap(struct reiserfs_transaction_handle *th,
309 b_blocknr_t * start, b_blocknr_t finish,
310 int min, int max, int unfm, unsigned long file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700312 int nr_allocated = 0;
313 struct super_block *s = th->t_super;
314 /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
315 * - Hans, it is not a block number - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700317 int bm, off;
318 int end_bm, end_off;
319 int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700321 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323 PROC_INFO_INC(s, scan_bitmap.call);
324 if (SB_FREE_BLOCKS(s) <= 0)
325 return 0; // No point in looking for more free blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 get_bit_address(s, *start, &bm, &off);
328 get_bit_address(s, finish, &end_bm, &end_off);
329 if (bm > SB_BMAP_NR(s))
330 return 0;
331 if (end_bm > SB_BMAP_NR(s))
332 end_bm = SB_BMAP_NR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 /* When the bitmap is more than 10% free, anyone can allocate.
335 * When it's less than 10% free, only files that already use the
336 * bitmap are allowed. Once we pass 80% full, this restriction
337 * is lifted.
338 *
339 * We do this so that files that grow later still have space close to
340 * their original allocation. This improves locality, and presumably
341 * performance as a result.
342 *
343 * This is only an allocation policy and does not make up for getting a
344 * bad hint. Decent hinting must be implemented for this to work well.
345 */
346 if (TEST_OPTION(skip_busy, s)
347 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
348 for (; bm < end_bm; bm++, off = 0) {
349 if ((off && (!unfm || (file_block != 0)))
350 || SB_AP_BITMAP(s)[bm].free_count >
351 (s->s_blocksize << 3) / 10)
352 nr_allocated =
353 scan_bitmap_block(th, bm, &off, off_max,
354 min, max, unfm);
355 if (nr_allocated)
356 goto ret;
357 }
358 /* we know from above that start is a reasonable number */
359 get_bit_address(s, *start, &bm, &off);
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 for (; bm < end_bm; bm++, off = 0) {
363 nr_allocated =
364 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
365 if (nr_allocated)
366 goto ret;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 nr_allocated =
370 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
371
372 ret:
373 *start = bm * off_max + off;
374 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376}
377
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
379 struct inode *inode, b_blocknr_t block,
380 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 struct super_block *s = th->t_super;
383 struct reiserfs_super_block *rs;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700384 struct buffer_head *sbh, *bmbh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 struct reiserfs_bitmap_info *apbi;
386 int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 PROC_INFO_INC(s, free_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 rs = SB_DISK_SUPER_BLOCK(s);
393 sbh = SB_BUFFER_WITH_SB(s);
394 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700396 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 if (nr >= sb_bmap_nr(rs)) {
399 reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
400 "block %lu is out of range on %s",
401 block, reiserfs_bdevname(s));
402 return;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Jeff Mahoney50652272006-09-30 23:28:44 -0700405 bmbh = reiserfs_read_bitmap_block(s, nr);
406 if (!bmbh)
407 return;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700408
409 reiserfs_prepare_for_journal(s, bmbh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700411 /* clear bit for the given block in bit map */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700412 if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 reiserfs_warning(s, "vs-4080: reiserfs_free_block: "
414 "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
415 reiserfs_bdevname(s), block);
416 }
417 apbi[nr].free_count++;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700418 journal_mark_dirty(th, s, bmbh);
419 brelse(bmbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 reiserfs_prepare_for_journal(s, sbh, 1);
422 /* update super block */
423 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700425 journal_mark_dirty(th, s, sbh);
426 if (for_unformatted)
427 DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700430void reiserfs_free_block(struct reiserfs_transaction_handle *th,
431 struct inode *inode, b_blocknr_t block,
432 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 struct super_block *s = th->t_super;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700438 if (!is_reusable(s, block, 1))
439 return;
440
441 if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
442 reiserfs_panic(th->t_super, "bitmap-4072",
443 "Trying to free block outside file system "
444 "boundaries (%lu > %lu)",
445 block, sb_block_count(REISERFS_SB(s)->s_rs));
446 return;
447 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700448 /* mark it before we clear it, just in case */
449 journal_mark_freed(th, s, block);
450 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
455 struct inode *inode, b_blocknr_t block)
456{
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700457 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700458 RFALSE(!th->t_super,
459 "vs-4060: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700460 if (!is_reusable(th->t_super, block, 1))
461 return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700462 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700465static void __discard_prealloc(struct reiserfs_transaction_handle *th,
466 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700468 unsigned long save = ei->i_prealloc_block;
469 int dirty = 0;
470 struct inode *inode = &ei->vfs_inode;
471 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 if (ei->i_prealloc_count < 0)
474 reiserfs_warning(th->t_super,
475 "zam-4001:%s: inode has negative prealloc blocks count.",
476 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700478 while (ei->i_prealloc_count > 0) {
479 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
480 ei->i_prealloc_block++;
481 ei->i_prealloc_count--;
482 dirty = 1;
483 }
484 if (dirty)
485 reiserfs_update_sd(th, inode);
486 ei->i_prealloc_block = save;
487 list_del_init(&(ei->i_prealloc_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
492 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 struct reiserfs_inode_info *ei = REISERFS_I(inode);
495 BUG_ON(!th->t_trans_id);
496 if (ei->i_prealloc_count)
497 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700500void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506 while (!list_empty(plist)) {
507 struct reiserfs_inode_info *ei;
508 ei = list_entry(plist->next, struct reiserfs_inode_info,
509 i_prealloc_list);
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) {
512 reiserfs_warning(th->t_super,
513 "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.",
514 __FUNCTION__);
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700517 __discard_prealloc(th, ei);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700521void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
524 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
525 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700529int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 while ((this_char = strsep(&options, ":")) != NULL) {
536 if ((value = strchr(this_char, '=')) != NULL)
537 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
540 int temp;
541 SET_OPTION(concentrating_formatted_nodes);
542 temp = (value
543 && *value) ? simple_strtoul(value, &value,
544 0) : 10;
545 if (temp <= 0 || temp > 100) {
546 REISERFS_SB(s)->s_alloc_options.border = 10;
547 } else {
548 REISERFS_SB(s)->s_alloc_options.border =
549 100 / temp;
550 }
551 continue;
552 }
553 if (!strcmp(this_char, "displacing_large_files")) {
554 SET_OPTION(displacing_large_files);
555 REISERFS_SB(s)->s_alloc_options.large_file_size =
556 (value
557 && *value) ? simple_strtoul(value, &value, 0) : 16;
558 continue;
559 }
560 if (!strcmp(this_char, "displacing_new_packing_localities")) {
561 SET_OPTION(displacing_new_packing_localities);
562 continue;
563 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700565 if (!strcmp(this_char, "old_hashed_relocation")) {
566 SET_OPTION(old_hashed_relocation);
567 continue;
568 }
569
570 if (!strcmp(this_char, "new_hashed_relocation")) {
571 SET_OPTION(new_hashed_relocation);
572 continue;
573 }
574
575 if (!strcmp(this_char, "dirid_groups")) {
576 SET_OPTION(dirid_groups);
577 continue;
578 }
579 if (!strcmp(this_char, "oid_groups")) {
580 SET_OPTION(oid_groups);
581 continue;
582 }
583 if (!strcmp(this_char, "packing_groups")) {
584 SET_OPTION(packing_groups);
585 continue;
586 }
587 if (!strcmp(this_char, "hashed_formatted_nodes")) {
588 SET_OPTION(hashed_formatted_nodes);
589 continue;
590 }
591
592 if (!strcmp(this_char, "skip_busy")) {
593 SET_OPTION(skip_busy);
594 continue;
595 }
596
597 if (!strcmp(this_char, "hundredth_slices")) {
598 SET_OPTION(hundredth_slices);
599 continue;
600 }
601
602 if (!strcmp(this_char, "old_way")) {
603 SET_OPTION(old_way);
604 continue;
605 }
606
607 if (!strcmp(this_char, "displace_based_on_dirid")) {
608 SET_OPTION(displace_based_on_dirid);
609 continue;
610 }
611
612 if (!strcmp(this_char, "preallocmin")) {
613 REISERFS_SB(s)->s_alloc_options.preallocmin =
614 (value
615 && *value) ? simple_strtoul(value, &value, 0) : 4;
616 continue;
617 }
618
619 if (!strcmp(this_char, "preallocsize")) {
620 REISERFS_SB(s)->s_alloc_options.preallocsize =
621 (value
622 && *value) ? simple_strtoul(value, &value,
623 0) :
624 PREALLOCATION_SIZE;
625 continue;
626 }
627
628 reiserfs_warning(s, "zam-4001: %s : unknown option - %s",
629 __FUNCTION__, this_char);
630 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700637static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
638{
639 char *hash_in;
640 if (hint->formatted_node) {
641 hash_in = (char *)&hint->key.k_dir_id;
642 } else {
643 if (!hint->inode) {
644 //hint->search_start = hint->beg;
645 hash_in = (char *)&hint->key.k_dir_id;
646 } else
647 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
648 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
649 else
650 hash_in =
651 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
652 }
653
654 hint->search_start =
655 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
659 * Relocation based on dirid, hashing them into a given bitmap block
660 * files. Formatted nodes are unaffected, a seperate policy covers them
661 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700662static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664 unsigned long hash;
665 __u32 dirid = 0;
666 int bm = 0;
667 struct super_block *sb = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700669 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
670 else if (hint->formatted_node)
671 dirid = hint->key.k_dir_id;
672
673 if (dirid) {
674 bm = bmap_hash_id(sb, dirid);
675 hash = bm * (sb->s_blocksize << 3);
676 /* give a portion of the block group to metadata */
677 if (hint->inode)
678 hash += sb->s_blocksize / 2;
679 hint->search_start = hash;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
684 * Relocation based on oid, hashing them into a given bitmap block
685 * files. Formatted nodes are unaffected, a seperate policy covers them
686 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700687static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689 if (hint->inode) {
690 unsigned long hash;
691 __u32 oid;
692 __u32 dirid;
693 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 /* keep the root dir and it's first set of subdirs close to
698 * the start of the disk
699 */
700 if (dirid <= 2)
701 hash = (hint->inode->i_sb->s_blocksize << 3);
702 else {
703 oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
704 bm = bmap_hash_id(hint->inode->i_sb, oid);
705 hash = bm * (hint->inode->i_sb->s_blocksize << 3);
706 }
707 hint->search_start = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/* returns 1 if it finds an indirect item and gets valid hint info
712 * from it, otherwise 0
713 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700714static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800716 struct treepath *path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 struct buffer_head *bh;
718 struct item_head *ih;
719 int pos_in_item;
720 __le32 *item;
721 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700723 if (!hint->path) /* reiserfs code can call this function w/o pointer to path
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 * structure supplied; then we rely on supplied search_start */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 path = hint->path;
728 bh = get_last_bh(path);
729 RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
730 ih = get_ih(path);
731 pos_in_item = path->pos_in_item;
732 item = get_item(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 if (!hint->formatted_node && is_indirect_le_ih(ih)) {
737 /* for indirect item: go to left and look for the first non-hole entry
738 in the indirect item */
739 if (pos_in_item == I_UNFM_NUM(ih))
740 pos_in_item--;
741// pos_in_item = I_UNFM_NUM (ih) - 1;
742 while (pos_in_item >= 0) {
743 int t = get_block_num(item, pos_in_item);
744 if (t) {
745 hint->search_start = t;
746 ret = 1;
747 break;
748 }
749 pos_in_item--;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700753 /* does result value fit into specified region? */
754 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/* should be, if formatted node, then try to put on first part of the device
758 specified as number of percent with mount option device, else try to put
759 on last of device. This is not to say it is good code to do so,
760 but the effect should be measured. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700761static inline void set_border_in_hint(struct super_block *s,
762 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700764 b_blocknr_t border =
765 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700767 if (hint->formatted_node)
768 hint->end = border - 1;
769 else
770 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700775 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
776 hint->search_start =
777 hint->beg +
778 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
779 4) % (hint->end - hint->beg);
780 else
781 hint->search_start =
782 hint->beg +
783 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
784 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700787static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700789 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791 if (!hint->inode)
792 hash_in = (char *)&hint->key.k_dir_id;
793 else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
794 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
795 else
796 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700798 hint->search_start =
799 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700802static inline int
803this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
804 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700806 return hint->block ==
807 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700811static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700813 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700815 hint->th->displace_new_blocks = 0;
816 hint->search_start =
817 hint->beg + keyed_hash((char *)(&key->k_objectid),
818 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700822static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
823{
824 b_blocknr_t border;
825 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 if (hint->formatted_node || hint->inode == NULL) {
828 return 0;
829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
832 border =
833 hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
834 4) % (hint->end - hint->beg - 1);
835 if (border > hint->search_start)
836 hint->search_start = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700838 return 1;
839}
840
841static inline int old_way(reiserfs_blocknr_hint_t * hint)
842{
843 b_blocknr_t border;
844
845 if (hint->formatted_node || hint->inode == NULL) {
846 return 0;
847 }
848
849 border =
850 hint->beg +
851 le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
852 hint->beg);
853 if (border > hint->search_start)
854 hint->search_start = border;
855
856 return 1;
857}
858
859static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
860{
861 struct in_core_key *key = &hint->key;
862 b_blocknr_t slice_start;
863
864 slice_start =
865 (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
866 if (slice_start > hint->search_start
867 || slice_start + (hint->end / 100) <= hint->search_start) {
868 hint->search_start = slice_start;
869 }
870}
871
872static void determine_search_start(reiserfs_blocknr_hint_t * hint,
873 int amount_needed)
874{
875 struct super_block *s = hint->th->t_super;
876 int unfm_hint;
877
878 hint->beg = 0;
879 hint->end = SB_BLOCK_COUNT(s) - 1;
880
881 /* This is former border algorithm. Now with tunable border offset */
882 if (concentrating_formatted_nodes(s))
883 set_border_in_hint(s, hint);
884
885#ifdef DISPLACE_NEW_PACKING_LOCALITIES
886 /* whenever we create a new directory, we displace it. At first we will
887 hash for location, later we might look for a moderately empty place for
888 it */
889 if (displacing_new_packing_localities(s)
890 && hint->th->displace_new_blocks) {
891 displace_new_packing_locality(hint);
892
893 /* we do not continue determine_search_start,
894 * if new packing locality is being displaced */
895 return;
896 }
897#endif
898
899 /* all persons should feel encouraged to add more special cases here and
900 * test them */
901
902 if (displacing_large_files(s) && !hint->formatted_node
903 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
904 displace_large_file(hint);
905 return;
906 }
907
908 /* if none of our special cases is relevant, use the left neighbor in the
909 tree order of the new node we are allocating for */
910 if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
911 hash_formatted_node(hint);
912 return;
913 }
914
915 unfm_hint = get_left_neighbor(hint);
916
917 /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
918 new blocks are displaced based on directory ID. Also, if suggested search_start
919 is less than last preallocated block, we start searching from it, assuming that
920 HDD dataflow is faster in forward direction */
921 if (TEST_OPTION(old_way, s)) {
922 if (!hint->formatted_node) {
923 if (!reiserfs_hashed_relocation(s))
924 old_way(hint);
925 else if (!reiserfs_no_unhashed_relocation(s))
926 old_hashed_relocation(hint);
927
928 if (hint->inode
929 && hint->search_start <
930 REISERFS_I(hint->inode)->i_prealloc_block)
931 hint->search_start =
932 REISERFS_I(hint->inode)->i_prealloc_block;
933 }
934 return;
935 }
936
937 /* This is an approach proposed by Hans */
938 if (TEST_OPTION(hundredth_slices, s)
939 && !(displacing_large_files(s) && !hint->formatted_node)) {
940 hundredth_slices(hint);
941 return;
942 }
943
944 /* old_hashed_relocation only works on unformatted */
945 if (!unfm_hint && !hint->formatted_node &&
946 TEST_OPTION(old_hashed_relocation, s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 }
949 /* new_hashed_relocation works with both formatted/unformatted nodes */
950 if ((!unfm_hint || hint->formatted_node) &&
951 TEST_OPTION(new_hashed_relocation, s)) {
952 new_hashed_relocation(hint);
953 }
954 /* dirid grouping works only on unformatted nodes */
955 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
956 dirid_groups(hint);
957 }
958#ifdef DISPLACE_NEW_PACKING_LOCALITIES
959 if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
960 dirid_groups(hint);
961 }
962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700964 /* oid grouping works only on unformatted nodes */
965 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
966 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
972{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700973 /* make minimum size a mount option and benchmark both ways */
974 /* we preallocate blocks only for regular files, specific size */
975 /* benchmark preallocating always and see what happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700977 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 if (!hint->formatted_node && hint->preallocate) {
980 if (S_ISREG(hint->inode->i_mode)
981 && hint->inode->i_size >=
982 REISERFS_SB(hint->th->t_super)->s_alloc_options.
983 preallocmin * hint->inode->i_sb->s_blocksize)
984 hint->prealloc_size =
985 REISERFS_SB(hint->th->t_super)->s_alloc_options.
986 preallocsize - 1;
987 }
988 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991/* XXX I know it could be merged with upper-level function;
992 but may be result function would be too complex. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700993static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
994 b_blocknr_t * new_blocknrs,
995 b_blocknr_t start,
996 b_blocknr_t finish, int min,
997 int amount_needed,
998 int prealloc_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 int rest = amount_needed;
1001 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001003 while (rest > 0 && start <= finish) {
1004 nr_allocated = scan_bitmap(hint->th, &start, finish, min,
1005 rest + prealloc_size,
1006 !hint->formatted_node, hint->block);
1007
1008 if (nr_allocated == 0) /* no new blocks allocated, return */
1009 break;
1010
1011 /* fill free_blocknrs array first */
1012 while (rest > 0 && nr_allocated > 0) {
1013 *new_blocknrs++ = start++;
1014 rest--;
1015 nr_allocated--;
1016 }
1017
1018 /* do we have something to fill prealloc. array also ? */
1019 if (nr_allocated > 0) {
1020 /* it means prealloc_size was greater that 0 and we do preallocation */
1021 list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
1022 &SB_JOURNAL(hint->th->t_super)->
1023 j_prealloc_list);
1024 REISERFS_I(hint->inode)->i_prealloc_block = start;
1025 REISERFS_I(hint->inode)->i_prealloc_count =
1026 nr_allocated;
1027 break;
1028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 }
1030
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001031 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001035 (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
1036 int amount_needed) {
1037 struct super_block *s = hint->th->t_super;
1038 b_blocknr_t start = hint->search_start;
1039 b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
1040 int passno = 0;
1041 int nr_allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001043 determine_prealloc_size(hint);
1044 if (!hint->formatted_node) {
1045 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001047 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1048 "reiserquota: allocating %d blocks id=%u",
1049 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001051 quota_ret =
1052 DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
1053 if (quota_ret) /* Quota exceeded? */
1054 return QUOTA_EXCEEDED;
1055 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001057 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1058 "reiserquota: allocating (prealloc) %d blocks id=%u",
1059 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001061 quota_ret =
1062 DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode,
1063 hint->prealloc_size);
1064 if (quota_ret)
1065 hint->preallocate = hint->prealloc_size = 0;
1066 }
1067 /* for unformatted nodes, force large allocations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001071 switch (passno++) {
1072 case 0: /* Search from hint->search_start to end of disk */
1073 start = hint->search_start;
1074 finish = SB_BLOCK_COUNT(s) - 1;
1075 break;
1076 case 1: /* Search from hint->beg to hint->search_start */
1077 start = hint->beg;
1078 finish = hint->search_start;
1079 break;
1080 case 2: /* Last chance: Search from 0 to hint->beg */
1081 start = 0;
1082 finish = hint->beg;
1083 break;
1084 default: /* We've tried searching everywhere, not enough space */
1085 /* Free the blocks */
1086 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001088 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1089 "reiserquota: freeing (nospace) %d blocks id=%u",
1090 amount_needed +
1091 hint->prealloc_size -
1092 nr_allocated,
1093 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001095 DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated); /* Free not allocated blocks */
1096 }
1097 while (nr_allocated--)
1098 reiserfs_free_block(hint->th, hint->inode,
1099 new_blocknrs[nr_allocated],
1100 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001102 return NO_DISK_SPACE;
1103 }
1104 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1105 new_blocknrs +
1106 nr_allocated,
1107 start, finish,
Jeff Mahoney9ea0f942006-09-30 23:28:45 -07001108 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001109 amount_needed -
1110 nr_allocated,
1111 hint->
1112 prealloc_size))
1113 < amount_needed);
1114 if (!hint->formatted_node &&
1115 amount_needed + hint->prealloc_size >
1116 nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
1117 /* Some of preallocation blocks were not allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001119 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1120 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1121 amount_needed + hint->prealloc_size -
1122 nr_allocated -
1123 REISERFS_I(hint->inode)->i_prealloc_count,
1124 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001126 DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed +
1127 hint->prealloc_size - nr_allocated -
1128 REISERFS_I(hint->inode)->
1129 i_prealloc_count);
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001132 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135/* grab new blocknrs from preallocated list */
1136/* return amount still needed after using them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001137static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
1138 b_blocknr_t * new_blocknrs,
1139 int amount_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141 struct inode *inode = hint->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1144 while (amount_needed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001146 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1147 REISERFS_I(inode)->i_prealloc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001149 amount_needed--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1152 list_del(&REISERFS_I(inode)->i_prealloc_list);
1153 break;
1154 }
1155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001157 /* return amount still needed after using preallocated blocks */
1158 return amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001161int reiserfs_allocate_blocknrs(reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs, int amount_needed, int reserved_by_us /* Amount of blocks we have
1162 already reserved */ )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001164 int initial_amount_needed = amount_needed;
1165 int ret;
1166 struct super_block *s = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001168 /* Check if there is enough space, taking into account reserved space */
1169 if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
1170 amount_needed - reserved_by_us)
1171 return NO_DISK_SPACE;
1172 /* should this be if !hint->inode && hint->preallocate? */
1173 /* do you mean hint->formatted_node can be removed ? - Zam */
1174 /* hint->formatted_node cannot be removed because we try to access
1175 inode information here, and there is often no inode assotiated with
1176 metadata allocations - green */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001178 if (!hint->formatted_node && hint->preallocate) {
1179 amount_needed = use_preallocated_list_if_available
1180 (hint, new_blocknrs, amount_needed);
1181 if (amount_needed == 0) /* all blocknrs we need we got from
1182 prealloc. list */
1183 return CARRY_ON;
1184 new_blocknrs += (initial_amount_needed - amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001186
1187 /* find search start and save it in hint structure */
1188 determine_search_start(hint, amount_needed);
1189 if (hint->search_start >= SB_BLOCK_COUNT(s))
1190 hint->search_start = SB_BLOCK_COUNT(s) - 1;
1191
1192 /* allocation itself; fill new_blocknrs and preallocation arrays */
1193 ret = blocknrs_and_prealloc_arrays_from_search_start
1194 (hint, new_blocknrs, amount_needed);
1195
1196 /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
1197 * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
1198 * variant) */
1199
1200 if (ret != CARRY_ON) {
1201 while (amount_needed++ < initial_amount_needed) {
1202 reiserfs_free_block(hint->th, hint->inode,
1203 *(--new_blocknrs), 1);
1204 }
1205 }
1206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Jeff Mahoney6f010462006-09-30 23:28:43 -07001209void reiserfs_cache_bitmap_metadata(struct super_block *sb,
1210 struct buffer_head *bh,
1211 struct reiserfs_bitmap_info *info)
1212{
1213 unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
1214
1215 info->first_zero_hint = 1 << (sb->s_blocksize_bits + 3);
1216
1217 while (--cur >= (unsigned long *)bh->b_data) {
1218 int base = ((char *)cur - bh->b_data) << 3;
1219
1220 /* 0 and ~0 are special, we can optimize for them */
1221 if (*cur == 0) {
1222 info->first_zero_hint = base;
1223 info->free_count += BITS_PER_LONG;
1224 } else if (*cur != ~0L) { /* A mix, investigate */
1225 int b;
1226 for (b = BITS_PER_LONG - 1; b >= 0; b--) {
1227 if (!reiserfs_test_le_bit(b, cur)) {
1228 info->first_zero_hint = base + b;
1229 info->free_count++;
1230 }
1231 }
1232 }
1233 }
1234 /* The first bit must ALWAYS be 1 */
1235 BUG_ON(info->first_zero_hint == 0);
1236}
1237
1238struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
1239 unsigned int bitmap)
1240{
1241 b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
Jeff Mahoney50652272006-09-30 23:28:44 -07001242 struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001243 struct buffer_head *bh;
1244
1245 /* Way old format filesystems had the bitmaps packed up front.
1246 * I doubt there are any of these left, but just in case... */
1247 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
1248 &(REISERFS_SB(sb)->s_properties))))
1249 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
1250 else if (bitmap == 0)
1251 block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
1252
Jeff Mahoney50652272006-09-30 23:28:44 -07001253 bh = sb_bread(sb, block);
1254 if (bh == NULL)
Eric Eric Sesterhenn00079e02006-10-06 22:19:45 -07001255 reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
1256 "reading failed", __FUNCTION__, block);
Jeff Mahoney50652272006-09-30 23:28:44 -07001257 else {
1258 if (buffer_locked(bh)) {
1259 PROC_INFO_INC(sb, scan_bitmap.wait);
1260 __wait_on_buffer(bh);
1261 }
1262 BUG_ON(!buffer_uptodate(bh));
1263 BUG_ON(atomic_read(&bh->b_count) == 0);
1264
1265 if (info->first_zero_hint == 0)
1266 reiserfs_cache_bitmap_metadata(sb, bh, info);
1267 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001268
1269 return bh;
1270}
1271
1272int reiserfs_init_bitmap_cache(struct super_block *sb)
1273{
1274 struct reiserfs_bitmap_info *bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001275
1276 bitmap = vmalloc(sizeof (*bitmap) * SB_BMAP_NR(sb));
1277 if (bitmap == NULL)
1278 return -ENOMEM;
1279
1280 memset(bitmap, 0, sizeof (*bitmap) * SB_BMAP_NR(sb));
1281
Jeff Mahoney6f010462006-09-30 23:28:43 -07001282 SB_AP_BITMAP(sb) = bitmap;
1283
1284 return 0;
1285}
Jeff Mahoney50652272006-09-30 23:28:44 -07001286
1287void reiserfs_free_bitmap_cache(struct super_block *sb)
1288{
1289 if (SB_AP_BITMAP(sb)) {
1290 vfree(SB_AP_BITMAP(sb));
1291 SB_AP_BITMAP(sb) = NULL;
1292 }
1293}