blob: f7275176305eede67aae77d702d7ffebce2462c0 [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,
Jeff Mahoney3ee16672007-10-18 23:39:25 -070050 b_blocknr_t block,
51 unsigned int *bmap_nr,
52 unsigned int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070054 /* It is in the bitmap block number equal to the block
55 * number divided by the number of bits in a block. */
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070056 *bmap_nr = block >> (s->s_blocksize_bits + 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070057 /* Within that bitmap block it is located at bit offset *offset. */
58 *offset = block & ((s->s_blocksize << 3) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Jeff Mahoney3ee16672007-10-18 23:39:25 -070063 unsigned int bmap, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
66 reiserfs_warning(s,
67 "vs-4010: is_reusable: block number is out of range %lu (%u)",
68 block, SB_BLOCK_COUNT(s));
69 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070072 get_bit_address(s, block, &bmap, &offset);
73
74 /* Old format filesystem? Unlikely, but the bitmaps are all up front so
75 * we need to account for it. */
76 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
77 &(REISERFS_SB(s)->s_properties)))) {
78 b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
79 if (block >= bmap1 && block <= bmap1 + SB_BMAP_NR(s)) {
80 reiserfs_warning(s, "vs: 4019: is_reusable: "
81 "bitmap block %lu(%u) can't be freed or reused",
82 block, SB_BMAP_NR(s));
83 return 0;
84 }
85 } else {
86 if (offset == 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087 reiserfs_warning(s, "vs: 4020: is_reusable: "
88 "bitmap block %lu(%u) can't be freed or reused",
89 block, SB_BMAP_NR(s));
90 return 0;
91 }
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070094 if (bmap >= SB_BMAP_NR(s)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 reiserfs_warning(s,
96 "vs-4030: is_reusable: there is no so many bitmap blocks: "
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070097 "block=%lu, bitmap_nr=%d", block, bmap);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098 return 0;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700101 if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
102 reiserfs_warning(s,
103 "vs-4050: is_reusable: this is root block (%u), "
104 "it must be busy", SB_ROOT_BLOCK(s));
105 return 0;
106 }
107
108 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* searches in journal structures for a given block number (bmap, off). If block
112 is found in reiserfs journal it suggests next free block candidate to test. */
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700113static inline int is_block_in_journal(struct super_block *s, unsigned int bmap,
114 int off, int *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700116 b_blocknr_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
119 if (tmp) { /* hint supplied */
120 *next = tmp;
121 PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
122 } else {
123 (*next) = off + 1; /* inc offset to avoid looping. */
124 PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
125 }
126 PROC_INFO_INC(s, scan_bitmap.retry);
127 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
133 * block; */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700135 unsigned int bmap_n, int *beg, int boundary,
136 int min, int max, int unfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700138 struct super_block *s = th->t_super;
139 struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700140 struct buffer_head *bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141 int end, next;
142 int org = *beg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700146 RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
147 bmap_n, SB_BMAP_NR(s) - 1);
148 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* this is unclear and lacks comments, explain how journal bitmaps
150 work here for the reader. Convey a sense of the design here. What
151 is a window? */
152/* - I mean `a window of zero bits' as in description of this function - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 if (!bi) {
155 reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d",
156 bmap_n);
157 return 0;
158 }
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700159
Jeff Mahoney50652272006-09-30 23:28:44 -0700160 bh = reiserfs_read_bitmap_block(s, bmap_n);
161 if (bh == NULL)
162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700164 while (1) {
165 cont:
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700166 if (bi->free_count < min) {
167 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 return 0; // No free blocks in this bitmap
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 /* search for a first zero bit -- beggining of a window */
172 *beg = reiserfs_find_next_zero_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700173 ((unsigned long *)(bh->b_data), boundary, *beg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700174
175 if (*beg + min > boundary) { /* search for a zero bit fails or the rest of bitmap block
176 * cannot contain a zero window of minimum size */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700177 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
182 continue;
183 /* first zero bit found; we check next bits */
184 for (end = *beg + 1;; end++) {
185 if (end >= *beg + max || end >= boundary
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700186 || reiserfs_test_le_bit(end, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700187 next = end;
188 break;
189 }
190 /* finding the other end of zero bit window requires looking into journal structures (in
191 * case of searching for free blocks for unformatted nodes) */
192 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
193 break;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700196 /* now (*beg) points to beginning of zero bits window,
197 * (end) points to one bit after the window end */
198 if (end - *beg >= min) { /* it seems we have found window of proper size */
199 int i;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700200 reiserfs_prepare_for_journal(s, bh, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700201 /* try to set all blocks used checking are they still free */
202 for (i = *beg; i < end; i++) {
203 /* It seems that we should not check in journal again. */
204 if (reiserfs_test_and_set_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700205 (i, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700206 /* bit was set by another process
207 * while we slept in prepare_for_journal() */
208 PROC_INFO_INC(s, scan_bitmap.stolen);
209 if (i >= *beg + min) { /* we can continue with smaller set of allocated blocks,
210 * if length of this set is more or equal to `min' */
211 end = i;
212 break;
213 }
214 /* otherwise we clear all bit were set ... */
215 while (--i >= *beg)
216 reiserfs_test_and_clear_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700217 (i, bh->b_data);
218 reiserfs_restore_prepared_buffer(s, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 *beg = org;
220 /* ... and search again in current block from beginning */
221 goto cont;
222 }
223 }
224 bi->free_count -= (end - *beg);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700225 journal_mark_dirty(th, s, bh);
226 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700227
228 /* free block count calculation */
229 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
230 1);
231 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
232 journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
233
234 return end - (*beg);
235 } else {
236 *beg = next;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700241static int bmap_hash_id(struct super_block *s, u32 id)
242{
243 char *hash_in = NULL;
244 unsigned long hash;
245 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247 if (id <= 2) {
248 bm = 1;
249 } else {
250 hash_in = (char *)(&id);
251 hash = keyed_hash(hash_in, 4);
252 bm = hash % SB_BMAP_NR(s);
253 if (!bm)
254 bm = 1;
255 }
256 /* this can only be true when SB_BMAP_NR = 1 */
257 if (bm >= SB_BMAP_NR(s))
258 bm = 0;
259 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * hashes the id and then returns > 0 if the block group for the
264 * corresponding hash is full
265 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700266static inline int block_group_used(struct super_block *s, u32 id)
267{
Jeff Mahoney50652272006-09-30 23:28:44 -0700268 int bm = bmap_hash_id(s, id);
269 struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
270
271 /* If we don't have cached information on this bitmap block, we're
272 * going to have to load it later anyway. Loading it here allows us
273 * to make a better decision. This favors long-term performace gain
274 * with a better on-disk layout vs. a short term gain of skipping the
275 * read and potentially having a bad placement. */
276 if (info->first_zero_hint == 0) {
277 struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
278 brelse(bh);
279 }
280
281 if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700282 return 0;
283 }
284 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * the packing is returned in disk byte order
289 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700291{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700292 __le32 packing;
293 if (TEST_OPTION(packing_groups, dir->i_sb)) {
294 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
295 /*
296 * some versions of reiserfsck expect packing locality 1 to be
297 * special
298 */
299 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
300 packing = INODE_PKEY(dir)->k_objectid;
301 else
302 packing = INODE_PKEY(dir)->k_dir_id;
303 } else
304 packing = INODE_PKEY(dir)->k_objectid;
305 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/* Tries to find contiguous zero bit window (given size) in given region of
309 * bitmap and place new blocks there. Returns number of allocated blocks. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700310static int scan_bitmap(struct reiserfs_transaction_handle *th,
311 b_blocknr_t * start, b_blocknr_t finish,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700312 int min, int max, int unfm, sector_t file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 int nr_allocated = 0;
315 struct super_block *s = th->t_super;
316 /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
317 * - Hans, it is not a block number - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700319 unsigned int bm, off;
320 unsigned int end_bm, end_off;
321 unsigned int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 PROC_INFO_INC(s, scan_bitmap.call);
326 if (SB_FREE_BLOCKS(s) <= 0)
327 return 0; // No point in looking for more free blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329 get_bit_address(s, *start, &bm, &off);
330 get_bit_address(s, finish, &end_bm, &end_off);
331 if (bm > SB_BMAP_NR(s))
332 return 0;
333 if (end_bm > SB_BMAP_NR(s))
334 end_bm = SB_BMAP_NR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700336 /* When the bitmap is more than 10% free, anyone can allocate.
337 * When it's less than 10% free, only files that already use the
338 * bitmap are allowed. Once we pass 80% full, this restriction
339 * is lifted.
340 *
341 * We do this so that files that grow later still have space close to
342 * their original allocation. This improves locality, and presumably
343 * performance as a result.
344 *
345 * This is only an allocation policy and does not make up for getting a
346 * bad hint. Decent hinting must be implemented for this to work well.
347 */
348 if (TEST_OPTION(skip_busy, s)
349 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
350 for (; bm < end_bm; bm++, off = 0) {
351 if ((off && (!unfm || (file_block != 0)))
352 || SB_AP_BITMAP(s)[bm].free_count >
353 (s->s_blocksize << 3) / 10)
354 nr_allocated =
355 scan_bitmap_block(th, bm, &off, off_max,
356 min, max, unfm);
357 if (nr_allocated)
358 goto ret;
359 }
360 /* we know from above that start is a reasonable number */
361 get_bit_address(s, *start, &bm, &off);
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700364 for (; bm < end_bm; bm++, off = 0) {
365 nr_allocated =
366 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
367 if (nr_allocated)
368 goto ret;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371 nr_allocated =
372 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
373
374 ret:
375 *start = bm * off_max + off;
376 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378}
379
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700380static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
381 struct inode *inode, b_blocknr_t block,
382 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700384 struct super_block *s = th->t_super;
385 struct reiserfs_super_block *rs;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700386 struct buffer_head *sbh, *bmbh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700387 struct reiserfs_bitmap_info *apbi;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700388 unsigned int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700392 PROC_INFO_INC(s, free_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 rs = SB_DISK_SUPER_BLOCK(s);
395 sbh = SB_BUFFER_WITH_SB(s);
396 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 if (nr >= sb_bmap_nr(rs)) {
401 reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
402 "block %lu is out of range on %s",
403 block, reiserfs_bdevname(s));
404 return;
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Jeff Mahoney50652272006-09-30 23:28:44 -0700407 bmbh = reiserfs_read_bitmap_block(s, nr);
408 if (!bmbh)
409 return;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700410
411 reiserfs_prepare_for_journal(s, bmbh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 /* clear bit for the given block in bit map */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700414 if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700415 reiserfs_warning(s, "vs-4080: reiserfs_free_block: "
416 "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
417 reiserfs_bdevname(s), block);
418 }
419 apbi[nr].free_count++;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700420 journal_mark_dirty(th, s, bmbh);
421 brelse(bmbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700423 reiserfs_prepare_for_journal(s, sbh, 1);
424 /* update super block */
425 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700427 journal_mark_dirty(th, s, sbh);
428 if (for_unformatted)
429 DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700432void reiserfs_free_block(struct reiserfs_transaction_handle *th,
433 struct inode *inode, b_blocknr_t block,
434 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436 struct super_block *s = th->t_super;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700439 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700440 if (!is_reusable(s, block, 1))
441 return;
442
443 if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
444 reiserfs_panic(th->t_super, "bitmap-4072",
445 "Trying to free block outside file system "
446 "boundaries (%lu > %lu)",
447 block, sb_block_count(REISERFS_SB(s)->s_rs));
448 return;
449 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 /* mark it before we clear it, just in case */
451 journal_mark_freed(th, s, block);
452 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700456static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
457 struct inode *inode, b_blocknr_t block)
458{
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700459 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700460 RFALSE(!th->t_super,
461 "vs-4060: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700462 if (!is_reusable(th->t_super, block, 1))
463 return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467static void __discard_prealloc(struct reiserfs_transaction_handle *th,
468 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 unsigned long save = ei->i_prealloc_block;
471 int dirty = 0;
472 struct inode *inode = &ei->vfs_inode;
473 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 if (ei->i_prealloc_count < 0)
476 reiserfs_warning(th->t_super,
477 "zam-4001:%s: inode has negative prealloc blocks count.",
478 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700480 while (ei->i_prealloc_count > 0) {
481 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
482 ei->i_prealloc_block++;
483 ei->i_prealloc_count--;
484 dirty = 1;
485 }
486 if (dirty)
487 reiserfs_update_sd(th, inode);
488 ei->i_prealloc_block = save;
489 list_del_init(&(ei->i_prealloc_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
494 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700496 struct reiserfs_inode_info *ei = REISERFS_I(inode);
497 BUG_ON(!th->t_trans_id);
498 if (ei->i_prealloc_count)
499 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 while (!list_empty(plist)) {
509 struct reiserfs_inode_info *ei;
510 ei = list_entry(plist->next, struct reiserfs_inode_info,
511 i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700513 if (!ei->i_prealloc_count) {
514 reiserfs_warning(th->t_super,
515 "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.",
516 __FUNCTION__);
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700519 __discard_prealloc(th, ei);
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700525 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
526 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
527 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700531int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700537 while ((this_char = strsep(&options, ":")) != NULL) {
538 if ((value = strchr(this_char, '=')) != NULL)
539 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700541 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
542 int temp;
543 SET_OPTION(concentrating_formatted_nodes);
544 temp = (value
545 && *value) ? simple_strtoul(value, &value,
546 0) : 10;
547 if (temp <= 0 || temp > 100) {
548 REISERFS_SB(s)->s_alloc_options.border = 10;
549 } else {
550 REISERFS_SB(s)->s_alloc_options.border =
551 100 / temp;
552 }
553 continue;
554 }
555 if (!strcmp(this_char, "displacing_large_files")) {
556 SET_OPTION(displacing_large_files);
557 REISERFS_SB(s)->s_alloc_options.large_file_size =
558 (value
559 && *value) ? simple_strtoul(value, &value, 0) : 16;
560 continue;
561 }
562 if (!strcmp(this_char, "displacing_new_packing_localities")) {
563 SET_OPTION(displacing_new_packing_localities);
564 continue;
565 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 if (!strcmp(this_char, "old_hashed_relocation")) {
568 SET_OPTION(old_hashed_relocation);
569 continue;
570 }
571
572 if (!strcmp(this_char, "new_hashed_relocation")) {
573 SET_OPTION(new_hashed_relocation);
574 continue;
575 }
576
577 if (!strcmp(this_char, "dirid_groups")) {
578 SET_OPTION(dirid_groups);
579 continue;
580 }
581 if (!strcmp(this_char, "oid_groups")) {
582 SET_OPTION(oid_groups);
583 continue;
584 }
585 if (!strcmp(this_char, "packing_groups")) {
586 SET_OPTION(packing_groups);
587 continue;
588 }
589 if (!strcmp(this_char, "hashed_formatted_nodes")) {
590 SET_OPTION(hashed_formatted_nodes);
591 continue;
592 }
593
594 if (!strcmp(this_char, "skip_busy")) {
595 SET_OPTION(skip_busy);
596 continue;
597 }
598
599 if (!strcmp(this_char, "hundredth_slices")) {
600 SET_OPTION(hundredth_slices);
601 continue;
602 }
603
604 if (!strcmp(this_char, "old_way")) {
605 SET_OPTION(old_way);
606 continue;
607 }
608
609 if (!strcmp(this_char, "displace_based_on_dirid")) {
610 SET_OPTION(displace_based_on_dirid);
611 continue;
612 }
613
614 if (!strcmp(this_char, "preallocmin")) {
615 REISERFS_SB(s)->s_alloc_options.preallocmin =
616 (value
617 && *value) ? simple_strtoul(value, &value, 0) : 4;
618 continue;
619 }
620
621 if (!strcmp(this_char, "preallocsize")) {
622 REISERFS_SB(s)->s_alloc_options.preallocsize =
623 (value
624 && *value) ? simple_strtoul(value, &value,
625 0) :
626 PREALLOCATION_SIZE;
627 continue;
628 }
629
630 reiserfs_warning(s, "zam-4001: %s : unknown option - %s",
631 __FUNCTION__, this_char);
632 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700635 reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
636 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700639static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
640{
641 char *hash_in;
642 if (hint->formatted_node) {
643 hash_in = (char *)&hint->key.k_dir_id;
644 } else {
645 if (!hint->inode) {
646 //hint->search_start = hint->beg;
647 hash_in = (char *)&hint->key.k_dir_id;
648 } else
649 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
650 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
651 else
652 hash_in =
653 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
654 }
655
656 hint->search_start =
657 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * Relocation based on dirid, hashing them into a given bitmap block
662 * files. Formatted nodes are unaffected, a seperate policy covers them
663 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700666 unsigned long hash;
667 __u32 dirid = 0;
668 int bm = 0;
669 struct super_block *sb = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700671 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
672 else if (hint->formatted_node)
673 dirid = hint->key.k_dir_id;
674
675 if (dirid) {
676 bm = bmap_hash_id(sb, dirid);
677 hash = bm * (sb->s_blocksize << 3);
678 /* give a portion of the block group to metadata */
679 if (hint->inode)
680 hash += sb->s_blocksize / 2;
681 hint->search_start = hash;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/*
686 * Relocation based on oid, hashing them into a given bitmap block
687 * files. Formatted nodes are unaffected, a seperate policy covers them
688 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700691 if (hint->inode) {
692 unsigned long hash;
693 __u32 oid;
694 __u32 dirid;
695 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700699 /* keep the root dir and it's first set of subdirs close to
700 * the start of the disk
701 */
702 if (dirid <= 2)
703 hash = (hint->inode->i_sb->s_blocksize << 3);
704 else {
705 oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
706 bm = bmap_hash_id(hint->inode->i_sb, oid);
707 hash = bm * (hint->inode->i_sb->s_blocksize << 3);
708 }
709 hint->search_start = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/* returns 1 if it finds an indirect item and gets valid hint info
714 * from it, otherwise 0
715 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700716static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800718 struct treepath *path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700719 struct buffer_head *bh;
720 struct item_head *ih;
721 int pos_in_item;
722 __le32 *item;
723 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700725 if (!hint->path) /* reiserfs code can call this function w/o pointer to path
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * structure supplied; then we rely on supplied search_start */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700729 path = hint->path;
730 bh = get_last_bh(path);
731 RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
732 ih = get_ih(path);
733 pos_in_item = path->pos_in_item;
734 item = get_item(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700736 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700738 if (!hint->formatted_node && is_indirect_le_ih(ih)) {
739 /* for indirect item: go to left and look for the first non-hole entry
740 in the indirect item */
741 if (pos_in_item == I_UNFM_NUM(ih))
742 pos_in_item--;
743// pos_in_item = I_UNFM_NUM (ih) - 1;
744 while (pos_in_item >= 0) {
745 int t = get_block_num(item, pos_in_item);
746 if (t) {
747 hint->search_start = t;
748 ret = 1;
749 break;
750 }
751 pos_in_item--;
752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700755 /* does result value fit into specified region? */
756 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/* should be, if formatted node, then try to put on first part of the device
760 specified as number of percent with mount option device, else try to put
761 on last of device. This is not to say it is good code to do so,
762 but the effect should be measured. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700763static inline void set_border_in_hint(struct super_block *s,
764 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700766 b_blocknr_t border =
767 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700769 if (hint->formatted_node)
770 hint->end = border - 1;
771 else
772 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700775static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700777 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
778 hint->search_start =
779 hint->beg +
780 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
781 4) % (hint->end - hint->beg);
782 else
783 hint->search_start =
784 hint->beg +
785 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
786 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700789static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700791 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700793 if (!hint->inode)
794 hash_in = (char *)&hint->key.k_dir_id;
795 else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
796 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
797 else
798 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700800 hint->search_start =
801 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700804static inline int
805this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
806 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808 return hint->block ==
809 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700813static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700815 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700817 hint->th->displace_new_blocks = 0;
818 hint->search_start =
819 hint->beg + keyed_hash((char *)(&key->k_objectid),
820 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700824static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
825{
826 b_blocknr_t border;
827 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829 if (hint->formatted_node || hint->inode == NULL) {
830 return 0;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700833 hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
834 border =
835 hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
836 4) % (hint->end - hint->beg - 1);
837 if (border > hint->search_start)
838 hint->search_start = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700840 return 1;
841}
842
843static inline int old_way(reiserfs_blocknr_hint_t * hint)
844{
845 b_blocknr_t border;
846
847 if (hint->formatted_node || hint->inode == NULL) {
848 return 0;
849 }
850
851 border =
852 hint->beg +
853 le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
854 hint->beg);
855 if (border > hint->search_start)
856 hint->search_start = border;
857
858 return 1;
859}
860
861static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
862{
863 struct in_core_key *key = &hint->key;
864 b_blocknr_t slice_start;
865
866 slice_start =
867 (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
868 if (slice_start > hint->search_start
869 || slice_start + (hint->end / 100) <= hint->search_start) {
870 hint->search_start = slice_start;
871 }
872}
873
874static void determine_search_start(reiserfs_blocknr_hint_t * hint,
875 int amount_needed)
876{
877 struct super_block *s = hint->th->t_super;
878 int unfm_hint;
879
880 hint->beg = 0;
881 hint->end = SB_BLOCK_COUNT(s) - 1;
882
883 /* This is former border algorithm. Now with tunable border offset */
884 if (concentrating_formatted_nodes(s))
885 set_border_in_hint(s, hint);
886
887#ifdef DISPLACE_NEW_PACKING_LOCALITIES
888 /* whenever we create a new directory, we displace it. At first we will
889 hash for location, later we might look for a moderately empty place for
890 it */
891 if (displacing_new_packing_localities(s)
892 && hint->th->displace_new_blocks) {
893 displace_new_packing_locality(hint);
894
895 /* we do not continue determine_search_start,
896 * if new packing locality is being displaced */
897 return;
898 }
899#endif
900
901 /* all persons should feel encouraged to add more special cases here and
902 * test them */
903
904 if (displacing_large_files(s) && !hint->formatted_node
905 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
906 displace_large_file(hint);
907 return;
908 }
909
910 /* if none of our special cases is relevant, use the left neighbor in the
911 tree order of the new node we are allocating for */
912 if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
913 hash_formatted_node(hint);
914 return;
915 }
916
917 unfm_hint = get_left_neighbor(hint);
918
919 /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
920 new blocks are displaced based on directory ID. Also, if suggested search_start
921 is less than last preallocated block, we start searching from it, assuming that
922 HDD dataflow is faster in forward direction */
923 if (TEST_OPTION(old_way, s)) {
924 if (!hint->formatted_node) {
925 if (!reiserfs_hashed_relocation(s))
926 old_way(hint);
927 else if (!reiserfs_no_unhashed_relocation(s))
928 old_hashed_relocation(hint);
929
930 if (hint->inode
931 && hint->search_start <
932 REISERFS_I(hint->inode)->i_prealloc_block)
933 hint->search_start =
934 REISERFS_I(hint->inode)->i_prealloc_block;
935 }
936 return;
937 }
938
939 /* This is an approach proposed by Hans */
940 if (TEST_OPTION(hundredth_slices, s)
941 && !(displacing_large_files(s) && !hint->formatted_node)) {
942 hundredth_slices(hint);
943 return;
944 }
945
946 /* old_hashed_relocation only works on unformatted */
947 if (!unfm_hint && !hint->formatted_node &&
948 TEST_OPTION(old_hashed_relocation, s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700950 }
951 /* new_hashed_relocation works with both formatted/unformatted nodes */
952 if ((!unfm_hint || hint->formatted_node) &&
953 TEST_OPTION(new_hashed_relocation, s)) {
954 new_hashed_relocation(hint);
955 }
956 /* dirid grouping works only on unformatted nodes */
957 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
958 dirid_groups(hint);
959 }
960#ifdef DISPLACE_NEW_PACKING_LOCALITIES
961 if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
962 dirid_groups(hint);
963 }
964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700966 /* oid grouping works only on unformatted nodes */
967 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
968 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
974{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700975 /* make minimum size a mount option and benchmark both ways */
976 /* we preallocate blocks only for regular files, specific size */
977 /* benchmark preallocating always and see what happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700979 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700981 if (!hint->formatted_node && hint->preallocate) {
982 if (S_ISREG(hint->inode->i_mode)
983 && hint->inode->i_size >=
984 REISERFS_SB(hint->th->t_super)->s_alloc_options.
985 preallocmin * hint->inode->i_sb->s_blocksize)
986 hint->prealloc_size =
987 REISERFS_SB(hint->th->t_super)->s_alloc_options.
988 preallocsize - 1;
989 }
990 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/* XXX I know it could be merged with upper-level function;
994 but may be result function would be too complex. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700995static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
996 b_blocknr_t * new_blocknrs,
997 b_blocknr_t start,
998 b_blocknr_t finish, int min,
999 int amount_needed,
1000 int prealloc_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001002 int rest = amount_needed;
1003 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001005 while (rest > 0 && start <= finish) {
1006 nr_allocated = scan_bitmap(hint->th, &start, finish, min,
1007 rest + prealloc_size,
1008 !hint->formatted_node, hint->block);
1009
1010 if (nr_allocated == 0) /* no new blocks allocated, return */
1011 break;
1012
1013 /* fill free_blocknrs array first */
1014 while (rest > 0 && nr_allocated > 0) {
1015 *new_blocknrs++ = start++;
1016 rest--;
1017 nr_allocated--;
1018 }
1019
1020 /* do we have something to fill prealloc. array also ? */
1021 if (nr_allocated > 0) {
1022 /* it means prealloc_size was greater that 0 and we do preallocation */
1023 list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
1024 &SB_JOURNAL(hint->th->t_super)->
1025 j_prealloc_list);
1026 REISERFS_I(hint->inode)->i_prealloc_block = start;
1027 REISERFS_I(hint->inode)->i_prealloc_count =
1028 nr_allocated;
1029 break;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001033 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001037 (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
1038 int amount_needed) {
1039 struct super_block *s = hint->th->t_super;
1040 b_blocknr_t start = hint->search_start;
1041 b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
1042 int passno = 0;
1043 int nr_allocated = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001045 determine_prealloc_size(hint);
1046 if (!hint->formatted_node) {
1047 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001049 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1050 "reiserquota: allocating %d blocks id=%u",
1051 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 quota_ret =
1054 DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
1055 if (quota_ret) /* Quota exceeded? */
1056 return QUOTA_EXCEEDED;
1057 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001059 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1060 "reiserquota: allocating (prealloc) %d blocks id=%u",
1061 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001063 quota_ret =
1064 DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode,
1065 hint->prealloc_size);
1066 if (quota_ret)
1067 hint->preallocate = hint->prealloc_size = 0;
1068 }
1069 /* for unformatted nodes, force large allocations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001072 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001073 switch (passno++) {
1074 case 0: /* Search from hint->search_start to end of disk */
1075 start = hint->search_start;
1076 finish = SB_BLOCK_COUNT(s) - 1;
1077 break;
1078 case 1: /* Search from hint->beg to hint->search_start */
1079 start = hint->beg;
1080 finish = hint->search_start;
1081 break;
1082 case 2: /* Last chance: Search from 0 to hint->beg */
1083 start = 0;
1084 finish = hint->beg;
1085 break;
1086 default: /* We've tried searching everywhere, not enough space */
1087 /* Free the blocks */
1088 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001090 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1091 "reiserquota: freeing (nospace) %d blocks id=%u",
1092 amount_needed +
1093 hint->prealloc_size -
1094 nr_allocated,
1095 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001097 DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated); /* Free not allocated blocks */
1098 }
1099 while (nr_allocated--)
1100 reiserfs_free_block(hint->th, hint->inode,
1101 new_blocknrs[nr_allocated],
1102 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001104 return NO_DISK_SPACE;
1105 }
1106 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1107 new_blocknrs +
1108 nr_allocated,
1109 start, finish,
Jeff Mahoney9ea0f942006-09-30 23:28:45 -07001110 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001111 amount_needed -
1112 nr_allocated,
1113 hint->
1114 prealloc_size))
1115 < amount_needed);
1116 if (!hint->formatted_node &&
1117 amount_needed + hint->prealloc_size >
1118 nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
1119 /* Some of preallocation blocks were not allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001121 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1122 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1123 amount_needed + hint->prealloc_size -
1124 nr_allocated -
1125 REISERFS_I(hint->inode)->i_prealloc_count,
1126 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001128 DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed +
1129 hint->prealloc_size - nr_allocated -
1130 REISERFS_I(hint->inode)->
1131 i_prealloc_count);
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001134 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137/* grab new blocknrs from preallocated list */
1138/* return amount still needed after using them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001139static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
1140 b_blocknr_t * new_blocknrs,
1141 int amount_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001143 struct inode *inode = hint->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001145 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1146 while (amount_needed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1149 REISERFS_I(inode)->i_prealloc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001151 amount_needed--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001153 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1154 list_del(&REISERFS_I(inode)->i_prealloc_list);
1155 break;
1156 }
1157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001159 /* return amount still needed after using preallocated blocks */
1160 return amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001163int 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
1164 already reserved */ )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166 int initial_amount_needed = amount_needed;
1167 int ret;
1168 struct super_block *s = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001170 /* Check if there is enough space, taking into account reserved space */
1171 if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
1172 amount_needed - reserved_by_us)
1173 return NO_DISK_SPACE;
1174 /* should this be if !hint->inode && hint->preallocate? */
1175 /* do you mean hint->formatted_node can be removed ? - Zam */
1176 /* hint->formatted_node cannot be removed because we try to access
1177 inode information here, and there is often no inode assotiated with
1178 metadata allocations - green */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001180 if (!hint->formatted_node && hint->preallocate) {
1181 amount_needed = use_preallocated_list_if_available
1182 (hint, new_blocknrs, amount_needed);
1183 if (amount_needed == 0) /* all blocknrs we need we got from
1184 prealloc. list */
1185 return CARRY_ON;
1186 new_blocknrs += (initial_amount_needed - amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001188
1189 /* find search start and save it in hint structure */
1190 determine_search_start(hint, amount_needed);
1191 if (hint->search_start >= SB_BLOCK_COUNT(s))
1192 hint->search_start = SB_BLOCK_COUNT(s) - 1;
1193
1194 /* allocation itself; fill new_blocknrs and preallocation arrays */
1195 ret = blocknrs_and_prealloc_arrays_from_search_start
1196 (hint, new_blocknrs, amount_needed);
1197
1198 /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
1199 * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
1200 * variant) */
1201
1202 if (ret != CARRY_ON) {
1203 while (amount_needed++ < initial_amount_needed) {
1204 reiserfs_free_block(hint->th, hint->inode,
1205 *(--new_blocknrs), 1);
1206 }
1207 }
1208 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Jeff Mahoney6f010462006-09-30 23:28:43 -07001211void reiserfs_cache_bitmap_metadata(struct super_block *sb,
1212 struct buffer_head *bh,
1213 struct reiserfs_bitmap_info *info)
1214{
1215 unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
1216
1217 info->first_zero_hint = 1 << (sb->s_blocksize_bits + 3);
1218
1219 while (--cur >= (unsigned long *)bh->b_data) {
1220 int base = ((char *)cur - bh->b_data) << 3;
1221
1222 /* 0 and ~0 are special, we can optimize for them */
1223 if (*cur == 0) {
1224 info->first_zero_hint = base;
1225 info->free_count += BITS_PER_LONG;
1226 } else if (*cur != ~0L) { /* A mix, investigate */
1227 int b;
1228 for (b = BITS_PER_LONG - 1; b >= 0; b--) {
1229 if (!reiserfs_test_le_bit(b, cur)) {
1230 info->first_zero_hint = base + b;
1231 info->free_count++;
1232 }
1233 }
1234 }
1235 }
1236 /* The first bit must ALWAYS be 1 */
1237 BUG_ON(info->first_zero_hint == 0);
1238}
1239
1240struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
1241 unsigned int bitmap)
1242{
1243 b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
Jeff Mahoney50652272006-09-30 23:28:44 -07001244 struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001245 struct buffer_head *bh;
1246
1247 /* Way old format filesystems had the bitmaps packed up front.
1248 * I doubt there are any of these left, but just in case... */
1249 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
1250 &(REISERFS_SB(sb)->s_properties))))
1251 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
1252 else if (bitmap == 0)
1253 block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
1254
Jeff Mahoney50652272006-09-30 23:28:44 -07001255 bh = sb_bread(sb, block);
1256 if (bh == NULL)
Eric Eric Sesterhenn00079e02006-10-06 22:19:45 -07001257 reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
1258 "reading failed", __FUNCTION__, block);
Jeff Mahoney50652272006-09-30 23:28:44 -07001259 else {
1260 if (buffer_locked(bh)) {
1261 PROC_INFO_INC(sb, scan_bitmap.wait);
1262 __wait_on_buffer(bh);
1263 }
1264 BUG_ON(!buffer_uptodate(bh));
1265 BUG_ON(atomic_read(&bh->b_count) == 0);
1266
1267 if (info->first_zero_hint == 0)
1268 reiserfs_cache_bitmap_metadata(sb, bh, info);
1269 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001270
1271 return bh;
1272}
1273
1274int reiserfs_init_bitmap_cache(struct super_block *sb)
1275{
1276 struct reiserfs_bitmap_info *bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001277
1278 bitmap = vmalloc(sizeof (*bitmap) * SB_BMAP_NR(sb));
1279 if (bitmap == NULL)
1280 return -ENOMEM;
1281
1282 memset(bitmap, 0, sizeof (*bitmap) * SB_BMAP_NR(sb));
1283
Jeff Mahoney6f010462006-09-30 23:28:43 -07001284 SB_AP_BITMAP(sb) = bitmap;
1285
1286 return 0;
1287}
Jeff Mahoney50652272006-09-30 23:28:44 -07001288
1289void reiserfs_free_bitmap_cache(struct super_block *sb)
1290{
1291 if (SB_AP_BITMAP(sb)) {
1292 vfree(SB_AP_BITMAP(sb));
1293 SB_AP_BITMAP(sb) = NULL;
1294 }
1295}