blob: 909f71e9a30f4887c171b42dc5d8c799b4d03f6a [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
6#include <linux/config.h>
7#include <linux/time.h>
8#include <linux/reiserfs_fs.h>
9#include <linux/errno.h>
10#include <linux/buffer_head.h>
11#include <linux/kernel.h>
12#include <linux/pagemap.h>
13#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. */
54 *bmap_nr = block / (s->s_blocksize << 3);
55 /* Within that bitmap block it is located at bit offset *offset. */
56 *offset = block & ((s->s_blocksize << 3) - 1);
57 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
60#ifdef CONFIG_REISERFS_CHECK
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{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 int i, j;
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
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070072 /* it can't be one of the bitmap blocks */
73 for (i = 0; i < SB_BMAP_NR(s); i++)
74 if (block == SB_AP_BITMAP(s)[i].bh->b_blocknr) {
75 reiserfs_warning(s, "vs: 4020: is_reusable: "
76 "bitmap block %lu(%u) can't be freed or reused",
77 block, SB_BMAP_NR(s));
78 return 0;
79 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070081 get_bit_address(s, block, &i, &j);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083 if (i >= SB_BMAP_NR(s)) {
84 reiserfs_warning(s,
85 "vs-4030: is_reusable: there is no so many bitmap blocks: "
86 "block=%lu, bitmap_nr=%d", block, i);
87 return 0;
88 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 if ((bit_value == 0 &&
91 reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i].bh->b_data)) ||
92 (bit_value == 1 &&
93 reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i].bh->b_data) == 0)) {
94 reiserfs_warning(s,
95 "vs-4040: is_reusable: corresponding bit of block %lu does not "
96 "match required value (i==%d, j==%d) test_bit==%d",
97 block, i, j, reiserfs_test_le_bit(j,
98 SB_AP_BITMAP
99 (s)[i].bh->
100 b_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102 return 0;
103 }
104
105 if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
106 reiserfs_warning(s,
107 "vs-4050: is_reusable: this is root block (%u), "
108 "it must be busy", SB_ROOT_BLOCK(s));
109 return 0;
110 }
111
112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700114#endif /* CONFIG_REISERFS_CHECK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/* searches in journal structures for a given block number (bmap, off). If block
117 is found in reiserfs journal it suggests next free block candidate to test. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118static inline int is_block_in_journal(struct super_block *s, int bmap, int
119 off, int *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700121 b_blocknr_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
124 if (tmp) { /* hint supplied */
125 *next = tmp;
126 PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
127 } else {
128 (*next) = off + 1; /* inc offset to avoid looping. */
129 PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
130 }
131 PROC_INFO_INC(s, scan_bitmap.retry);
132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
137/* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
138 * block; */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700139static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
140 int bmap_n, int *beg, int boundary, int min,
141 int max, int unfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 struct super_block *s = th->t_super;
144 struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
145 int end, next;
146 int org = *beg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700148 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700150 RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
151 bmap_n, SB_BMAP_NR(s) - 1);
152 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* this is unclear and lacks comments, explain how journal bitmaps
154 work here for the reader. Convey a sense of the design here. What
155 is a window? */
156/* - I mean `a window of zero bits' as in description of this function - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158 if (!bi) {
159 reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d",
160 bmap_n);
161 return 0;
162 }
163 if (buffer_locked(bi->bh)) {
164 PROC_INFO_INC(s, scan_bitmap.wait);
165 __wait_on_buffer(bi->bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700168 while (1) {
169 cont:
170 if (bi->free_count < min)
171 return 0; // No free blocks in this bitmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173 /* search for a first zero bit -- beggining of a window */
174 *beg = reiserfs_find_next_zero_le_bit
175 ((unsigned long *)(bi->bh->b_data), boundary, *beg);
176
177 if (*beg + min > boundary) { /* search for a zero bit fails or the rest of bitmap block
178 * cannot contain a zero window of minimum size */
179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700182 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
183 continue;
184 /* first zero bit found; we check next bits */
185 for (end = *beg + 1;; end++) {
186 if (end >= *beg + max || end >= boundary
187 || reiserfs_test_le_bit(end, bi->bh->b_data)) {
188 next = end;
189 break;
190 }
191 /* finding the other end of zero bit window requires looking into journal structures (in
192 * case of searching for free blocks for unformatted nodes) */
193 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
194 break;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700197 /* now (*beg) points to beginning of zero bits window,
198 * (end) points to one bit after the window end */
199 if (end - *beg >= min) { /* it seems we have found window of proper size */
200 int i;
201 reiserfs_prepare_for_journal(s, bi->bh, 1);
202 /* try to set all blocks used checking are they still free */
203 for (i = *beg; i < end; i++) {
204 /* It seems that we should not check in journal again. */
205 if (reiserfs_test_and_set_le_bit
206 (i, bi->bh->b_data)) {
207 /* bit was set by another process
208 * while we slept in prepare_for_journal() */
209 PROC_INFO_INC(s, scan_bitmap.stolen);
210 if (i >= *beg + min) { /* we can continue with smaller set of allocated blocks,
211 * if length of this set is more or equal to `min' */
212 end = i;
213 break;
214 }
215 /* otherwise we clear all bit were set ... */
216 while (--i >= *beg)
217 reiserfs_test_and_clear_le_bit
218 (i, bi->bh->b_data);
219 reiserfs_restore_prepared_buffer(s,
220 bi->
221 bh);
222 *beg = org;
223 /* ... and search again in current block from beginning */
224 goto cont;
225 }
226 }
227 bi->free_count -= (end - *beg);
228 journal_mark_dirty(th, s, bi->bh);
229
230 /* free block count calculation */
231 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
232 1);
233 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
234 journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
235
236 return end - (*beg);
237 } else {
238 *beg = next;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700243static int bmap_hash_id(struct super_block *s, u32 id)
244{
245 char *hash_in = NULL;
246 unsigned long hash;
247 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 if (id <= 2) {
250 bm = 1;
251 } else {
252 hash_in = (char *)(&id);
253 hash = keyed_hash(hash_in, 4);
254 bm = hash % SB_BMAP_NR(s);
255 if (!bm)
256 bm = 1;
257 }
258 /* this can only be true when SB_BMAP_NR = 1 */
259 if (bm >= SB_BMAP_NR(s))
260 bm = 0;
261 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * hashes the id and then returns > 0 if the block group for the
266 * corresponding hash is full
267 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700268static inline int block_group_used(struct super_block *s, u32 id)
269{
270 int bm;
271 bm = bmap_hash_id(s, id);
272 if (SB_AP_BITMAP(s)[bm].free_count > ((s->s_blocksize << 3) * 60 / 100)) {
273 return 0;
274 }
275 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * the packing is returned in disk byte order
280 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700282{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 __le32 packing;
284 if (TEST_OPTION(packing_groups, dir->i_sb)) {
285 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
286 /*
287 * some versions of reiserfsck expect packing locality 1 to be
288 * special
289 */
290 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
291 packing = INODE_PKEY(dir)->k_objectid;
292 else
293 packing = INODE_PKEY(dir)->k_dir_id;
294 } else
295 packing = INODE_PKEY(dir)->k_objectid;
296 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/* Tries to find contiguous zero bit window (given size) in given region of
300 * bitmap and place new blocks there. Returns number of allocated blocks. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301static int scan_bitmap(struct reiserfs_transaction_handle *th,
302 b_blocknr_t * start, b_blocknr_t finish,
303 int min, int max, int unfm, unsigned long file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700305 int nr_allocated = 0;
306 struct super_block *s = th->t_super;
307 /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
308 * - Hans, it is not a block number - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700310 int bm, off;
311 int end_bm, end_off;
312 int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700314 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700316 PROC_INFO_INC(s, scan_bitmap.call);
317 if (SB_FREE_BLOCKS(s) <= 0)
318 return 0; // No point in looking for more free blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700320 get_bit_address(s, *start, &bm, &off);
321 get_bit_address(s, finish, &end_bm, &end_off);
322 if (bm > SB_BMAP_NR(s))
323 return 0;
324 if (end_bm > SB_BMAP_NR(s))
325 end_bm = SB_BMAP_NR(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 /* When the bitmap is more than 10% free, anyone can allocate.
328 * When it's less than 10% free, only files that already use the
329 * bitmap are allowed. Once we pass 80% full, this restriction
330 * is lifted.
331 *
332 * We do this so that files that grow later still have space close to
333 * their original allocation. This improves locality, and presumably
334 * performance as a result.
335 *
336 * This is only an allocation policy and does not make up for getting a
337 * bad hint. Decent hinting must be implemented for this to work well.
338 */
339 if (TEST_OPTION(skip_busy, s)
340 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
341 for (; bm < end_bm; bm++, off = 0) {
342 if ((off && (!unfm || (file_block != 0)))
343 || SB_AP_BITMAP(s)[bm].free_count >
344 (s->s_blocksize << 3) / 10)
345 nr_allocated =
346 scan_bitmap_block(th, bm, &off, off_max,
347 min, max, unfm);
348 if (nr_allocated)
349 goto ret;
350 }
351 /* we know from above that start is a reasonable number */
352 get_bit_address(s, *start, &bm, &off);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700355 for (; bm < end_bm; bm++, off = 0) {
356 nr_allocated =
357 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
358 if (nr_allocated)
359 goto ret;
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 nr_allocated =
363 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
364
365 ret:
366 *start = bm * off_max + off;
367 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369}
370
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700371static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
372 struct inode *inode, b_blocknr_t block,
373 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700375 struct super_block *s = th->t_super;
376 struct reiserfs_super_block *rs;
377 struct buffer_head *sbh;
378 struct reiserfs_bitmap_info *apbi;
379 int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383 PROC_INFO_INC(s, free_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 rs = SB_DISK_SUPER_BLOCK(s);
386 sbh = SB_BUFFER_WITH_SB(s);
387 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700389 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 if (nr >= sb_bmap_nr(rs)) {
392 reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
393 "block %lu is out of range on %s",
394 block, reiserfs_bdevname(s));
395 return;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700398 reiserfs_prepare_for_journal(s, apbi[nr].bh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700400 /* clear bit for the given block in bit map */
401 if (!reiserfs_test_and_clear_le_bit(offset, apbi[nr].bh->b_data)) {
402 reiserfs_warning(s, "vs-4080: reiserfs_free_block: "
403 "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
404 reiserfs_bdevname(s), block);
405 }
406 apbi[nr].free_count++;
407 journal_mark_dirty(th, s, apbi[nr].bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700409 reiserfs_prepare_for_journal(s, sbh, 1);
410 /* update super block */
411 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700413 journal_mark_dirty(th, s, sbh);
414 if (for_unformatted)
415 DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700418void reiserfs_free_block(struct reiserfs_transaction_handle *th,
419 struct inode *inode, b_blocknr_t block,
420 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700422 struct super_block *s = th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700424 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
427 RFALSE(is_reusable(s, block, 1) == 0,
428 "vs-4071: can not free such block");
429 /* mark it before we clear it, just in case */
430 journal_mark_freed(th, s, block);
431 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700435static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
436 struct inode *inode, b_blocknr_t block)
437{
438 RFALSE(!th->t_super,
439 "vs-4060: trying to free block on nonexistent device");
440 RFALSE(is_reusable(th->t_super, block, 1) == 0,
441 "vs-4070: can not free such block");
442 BUG_ON(!th->t_trans_id);
443 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446static void __discard_prealloc(struct reiserfs_transaction_handle *th,
447 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700449 unsigned long save = ei->i_prealloc_block;
450 int dirty = 0;
451 struct inode *inode = &ei->vfs_inode;
452 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454 if (ei->i_prealloc_count < 0)
455 reiserfs_warning(th->t_super,
456 "zam-4001:%s: inode has negative prealloc blocks count.",
457 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700459 while (ei->i_prealloc_count > 0) {
460 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
461 ei->i_prealloc_block++;
462 ei->i_prealloc_count--;
463 dirty = 1;
464 }
465 if (dirty)
466 reiserfs_update_sd(th, inode);
467 ei->i_prealloc_block = save;
468 list_del_init(&(ei->i_prealloc_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700472void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
473 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 struct reiserfs_inode_info *ei = REISERFS_I(inode);
476 BUG_ON(!th->t_trans_id);
477 if (ei->i_prealloc_count)
478 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700481void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700487 while (!list_empty(plist)) {
488 struct reiserfs_inode_info *ei;
489 ei = list_entry(plist->next, struct reiserfs_inode_info,
490 i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700492 if (!ei->i_prealloc_count) {
493 reiserfs_warning(th->t_super,
494 "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.",
495 __FUNCTION__);
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 __discard_prealloc(th, ei);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
505 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
506 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700510int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700512 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700516 while ((this_char = strsep(&options, ":")) != NULL) {
517 if ((value = strchr(this_char, '=')) != NULL)
518 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
521 int temp;
522 SET_OPTION(concentrating_formatted_nodes);
523 temp = (value
524 && *value) ? simple_strtoul(value, &value,
525 0) : 10;
526 if (temp <= 0 || temp > 100) {
527 REISERFS_SB(s)->s_alloc_options.border = 10;
528 } else {
529 REISERFS_SB(s)->s_alloc_options.border =
530 100 / temp;
531 }
532 continue;
533 }
534 if (!strcmp(this_char, "displacing_large_files")) {
535 SET_OPTION(displacing_large_files);
536 REISERFS_SB(s)->s_alloc_options.large_file_size =
537 (value
538 && *value) ? simple_strtoul(value, &value, 0) : 16;
539 continue;
540 }
541 if (!strcmp(this_char, "displacing_new_packing_localities")) {
542 SET_OPTION(displacing_new_packing_localities);
543 continue;
544 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 if (!strcmp(this_char, "old_hashed_relocation")) {
547 SET_OPTION(old_hashed_relocation);
548 continue;
549 }
550
551 if (!strcmp(this_char, "new_hashed_relocation")) {
552 SET_OPTION(new_hashed_relocation);
553 continue;
554 }
555
556 if (!strcmp(this_char, "dirid_groups")) {
557 SET_OPTION(dirid_groups);
558 continue;
559 }
560 if (!strcmp(this_char, "oid_groups")) {
561 SET_OPTION(oid_groups);
562 continue;
563 }
564 if (!strcmp(this_char, "packing_groups")) {
565 SET_OPTION(packing_groups);
566 continue;
567 }
568 if (!strcmp(this_char, "hashed_formatted_nodes")) {
569 SET_OPTION(hashed_formatted_nodes);
570 continue;
571 }
572
573 if (!strcmp(this_char, "skip_busy")) {
574 SET_OPTION(skip_busy);
575 continue;
576 }
577
578 if (!strcmp(this_char, "hundredth_slices")) {
579 SET_OPTION(hundredth_slices);
580 continue;
581 }
582
583 if (!strcmp(this_char, "old_way")) {
584 SET_OPTION(old_way);
585 continue;
586 }
587
588 if (!strcmp(this_char, "displace_based_on_dirid")) {
589 SET_OPTION(displace_based_on_dirid);
590 continue;
591 }
592
593 if (!strcmp(this_char, "preallocmin")) {
594 REISERFS_SB(s)->s_alloc_options.preallocmin =
595 (value
596 && *value) ? simple_strtoul(value, &value, 0) : 4;
597 continue;
598 }
599
600 if (!strcmp(this_char, "preallocsize")) {
601 REISERFS_SB(s)->s_alloc_options.preallocsize =
602 (value
603 && *value) ? simple_strtoul(value, &value,
604 0) :
605 PREALLOCATION_SIZE;
606 continue;
607 }
608
609 reiserfs_warning(s, "zam-4001: %s : unknown option - %s",
610 __FUNCTION__, this_char);
611 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614 reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700618static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
619{
620 char *hash_in;
621 if (hint->formatted_node) {
622 hash_in = (char *)&hint->key.k_dir_id;
623 } else {
624 if (!hint->inode) {
625 //hint->search_start = hint->beg;
626 hash_in = (char *)&hint->key.k_dir_id;
627 } else
628 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
629 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
630 else
631 hash_in =
632 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
633 }
634
635 hint->search_start =
636 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
640 * Relocation based on dirid, hashing them into a given bitmap block
641 * files. Formatted nodes are unaffected, a seperate policy covers them
642 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 unsigned long hash;
646 __u32 dirid = 0;
647 int bm = 0;
648 struct super_block *sb = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700650 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
651 else if (hint->formatted_node)
652 dirid = hint->key.k_dir_id;
653
654 if (dirid) {
655 bm = bmap_hash_id(sb, dirid);
656 hash = bm * (sb->s_blocksize << 3);
657 /* give a portion of the block group to metadata */
658 if (hint->inode)
659 hash += sb->s_blocksize / 2;
660 hint->search_start = hash;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664/*
665 * Relocation based on oid, hashing them into a given bitmap block
666 * files. Formatted nodes are unaffected, a seperate policy covers them
667 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700668static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670 if (hint->inode) {
671 unsigned long hash;
672 __u32 oid;
673 __u32 dirid;
674 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700678 /* keep the root dir and it's first set of subdirs close to
679 * the start of the disk
680 */
681 if (dirid <= 2)
682 hash = (hint->inode->i_sb->s_blocksize << 3);
683 else {
684 oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
685 bm = bmap_hash_id(hint->inode->i_sb, oid);
686 hash = bm * (hint->inode->i_sb->s_blocksize << 3);
687 }
688 hint->search_start = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/* returns 1 if it finds an indirect item and gets valid hint info
693 * from it, otherwise 0
694 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700695static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700697 struct path *path;
698 struct buffer_head *bh;
699 struct item_head *ih;
700 int pos_in_item;
701 __le32 *item;
702 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700704 if (!hint->path) /* reiserfs code can call this function w/o pointer to path
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * structure supplied; then we rely on supplied search_start */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708 path = hint->path;
709 bh = get_last_bh(path);
710 RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
711 ih = get_ih(path);
712 pos_in_item = path->pos_in_item;
713 item = get_item(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700715 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 if (!hint->formatted_node && is_indirect_le_ih(ih)) {
718 /* for indirect item: go to left and look for the first non-hole entry
719 in the indirect item */
720 if (pos_in_item == I_UNFM_NUM(ih))
721 pos_in_item--;
722// pos_in_item = I_UNFM_NUM (ih) - 1;
723 while (pos_in_item >= 0) {
724 int t = get_block_num(item, pos_in_item);
725 if (t) {
726 hint->search_start = t;
727 ret = 1;
728 break;
729 }
730 pos_in_item--;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700734 /* does result value fit into specified region? */
735 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738/* should be, if formatted node, then try to put on first part of the device
739 specified as number of percent with mount option device, else try to put
740 on last of device. This is not to say it is good code to do so,
741 but the effect should be measured. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700742static inline void set_border_in_hint(struct super_block *s,
743 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700745 b_blocknr_t border =
746 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700748 if (hint->formatted_node)
749 hint->end = border - 1;
750 else
751 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700756 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
757 hint->search_start =
758 hint->beg +
759 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
760 4) % (hint->end - hint->beg);
761 else
762 hint->search_start =
763 hint->beg +
764 keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
765 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700768static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700772 if (!hint->inode)
773 hash_in = (char *)&hint->key.k_dir_id;
774 else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
775 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
776 else
777 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700779 hint->search_start =
780 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700783static inline int
784this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
785 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700787 return hint->block ==
788 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
791#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700792static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700794 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700796 hint->th->displace_new_blocks = 0;
797 hint->search_start =
798 hint->beg + keyed_hash((char *)(&key->k_objectid),
799 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700803static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
804{
805 b_blocknr_t border;
806 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808 if (hint->formatted_node || hint->inode == NULL) {
809 return 0;
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700812 hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
813 border =
814 hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
815 4) % (hint->end - hint->beg - 1);
816 if (border > hint->search_start)
817 hint->search_start = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700819 return 1;
820}
821
822static inline int old_way(reiserfs_blocknr_hint_t * hint)
823{
824 b_blocknr_t border;
825
826 if (hint->formatted_node || hint->inode == NULL) {
827 return 0;
828 }
829
830 border =
831 hint->beg +
832 le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
833 hint->beg);
834 if (border > hint->search_start)
835 hint->search_start = border;
836
837 return 1;
838}
839
840static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
841{
842 struct in_core_key *key = &hint->key;
843 b_blocknr_t slice_start;
844
845 slice_start =
846 (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
847 if (slice_start > hint->search_start
848 || slice_start + (hint->end / 100) <= hint->search_start) {
849 hint->search_start = slice_start;
850 }
851}
852
853static void determine_search_start(reiserfs_blocknr_hint_t * hint,
854 int amount_needed)
855{
856 struct super_block *s = hint->th->t_super;
857 int unfm_hint;
858
859 hint->beg = 0;
860 hint->end = SB_BLOCK_COUNT(s) - 1;
861
862 /* This is former border algorithm. Now with tunable border offset */
863 if (concentrating_formatted_nodes(s))
864 set_border_in_hint(s, hint);
865
866#ifdef DISPLACE_NEW_PACKING_LOCALITIES
867 /* whenever we create a new directory, we displace it. At first we will
868 hash for location, later we might look for a moderately empty place for
869 it */
870 if (displacing_new_packing_localities(s)
871 && hint->th->displace_new_blocks) {
872 displace_new_packing_locality(hint);
873
874 /* we do not continue determine_search_start,
875 * if new packing locality is being displaced */
876 return;
877 }
878#endif
879
880 /* all persons should feel encouraged to add more special cases here and
881 * test them */
882
883 if (displacing_large_files(s) && !hint->formatted_node
884 && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
885 displace_large_file(hint);
886 return;
887 }
888
889 /* if none of our special cases is relevant, use the left neighbor in the
890 tree order of the new node we are allocating for */
891 if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
892 hash_formatted_node(hint);
893 return;
894 }
895
896 unfm_hint = get_left_neighbor(hint);
897
898 /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
899 new blocks are displaced based on directory ID. Also, if suggested search_start
900 is less than last preallocated block, we start searching from it, assuming that
901 HDD dataflow is faster in forward direction */
902 if (TEST_OPTION(old_way, s)) {
903 if (!hint->formatted_node) {
904 if (!reiserfs_hashed_relocation(s))
905 old_way(hint);
906 else if (!reiserfs_no_unhashed_relocation(s))
907 old_hashed_relocation(hint);
908
909 if (hint->inode
910 && hint->search_start <
911 REISERFS_I(hint->inode)->i_prealloc_block)
912 hint->search_start =
913 REISERFS_I(hint->inode)->i_prealloc_block;
914 }
915 return;
916 }
917
918 /* This is an approach proposed by Hans */
919 if (TEST_OPTION(hundredth_slices, s)
920 && !(displacing_large_files(s) && !hint->formatted_node)) {
921 hundredth_slices(hint);
922 return;
923 }
924
925 /* old_hashed_relocation only works on unformatted */
926 if (!unfm_hint && !hint->formatted_node &&
927 TEST_OPTION(old_hashed_relocation, s)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 }
930 /* new_hashed_relocation works with both formatted/unformatted nodes */
931 if ((!unfm_hint || hint->formatted_node) &&
932 TEST_OPTION(new_hashed_relocation, s)) {
933 new_hashed_relocation(hint);
934 }
935 /* dirid grouping works only on unformatted nodes */
936 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
937 dirid_groups(hint);
938 }
939#ifdef DISPLACE_NEW_PACKING_LOCALITIES
940 if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
941 dirid_groups(hint);
942 }
943#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700945 /* oid grouping works only on unformatted nodes */
946 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
947 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
953{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700954 /* make minimum size a mount option and benchmark both ways */
955 /* we preallocate blocks only for regular files, specific size */
956 /* benchmark preallocating always and see what happens */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700958 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700960 if (!hint->formatted_node && hint->preallocate) {
961 if (S_ISREG(hint->inode->i_mode)
962 && hint->inode->i_size >=
963 REISERFS_SB(hint->th->t_super)->s_alloc_options.
964 preallocmin * hint->inode->i_sb->s_blocksize)
965 hint->prealloc_size =
966 REISERFS_SB(hint->th->t_super)->s_alloc_options.
967 preallocsize - 1;
968 }
969 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/* XXX I know it could be merged with upper-level function;
973 but may be result function would be too complex. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700974static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
975 b_blocknr_t * new_blocknrs,
976 b_blocknr_t start,
977 b_blocknr_t finish, int min,
978 int amount_needed,
979 int prealloc_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700981 int rest = amount_needed;
982 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700984 while (rest > 0 && start <= finish) {
985 nr_allocated = scan_bitmap(hint->th, &start, finish, min,
986 rest + prealloc_size,
987 !hint->formatted_node, hint->block);
988
989 if (nr_allocated == 0) /* no new blocks allocated, return */
990 break;
991
992 /* fill free_blocknrs array first */
993 while (rest > 0 && nr_allocated > 0) {
994 *new_blocknrs++ = start++;
995 rest--;
996 nr_allocated--;
997 }
998
999 /* do we have something to fill prealloc. array also ? */
1000 if (nr_allocated > 0) {
1001 /* it means prealloc_size was greater that 0 and we do preallocation */
1002 list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
1003 &SB_JOURNAL(hint->th->t_super)->
1004 j_prealloc_list);
1005 REISERFS_I(hint->inode)->i_prealloc_block = start;
1006 REISERFS_I(hint->inode)->i_prealloc_count =
1007 nr_allocated;
1008 break;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001012 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001016 (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
1017 int amount_needed) {
1018 struct super_block *s = hint->th->t_super;
1019 b_blocknr_t start = hint->search_start;
1020 b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
1021 int passno = 0;
1022 int nr_allocated = 0;
1023 int bigalloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001025 determine_prealloc_size(hint);
1026 if (!hint->formatted_node) {
1027 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001029 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1030 "reiserquota: allocating %d blocks id=%u",
1031 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001033 quota_ret =
1034 DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
1035 if (quota_ret) /* Quota exceeded? */
1036 return QUOTA_EXCEEDED;
1037 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1040 "reiserquota: allocating (prealloc) %d blocks id=%u",
1041 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001043 quota_ret =
1044 DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode,
1045 hint->prealloc_size);
1046 if (quota_ret)
1047 hint->preallocate = hint->prealloc_size = 0;
1048 }
1049 /* for unformatted nodes, force large allocations */
1050 bigalloc = amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001053 do {
1054 /* in bigalloc mode, nr_allocated should stay zero until
1055 * the entire allocation is filled
1056 */
1057 if (unlikely(bigalloc && nr_allocated)) {
1058 reiserfs_warning(s, "bigalloc is %d, nr_allocated %d\n",
1059 bigalloc, nr_allocated);
1060 /* reset things to a sane value */
1061 bigalloc = amount_needed - nr_allocated;
1062 }
1063 /*
1064 * try pass 0 and pass 1 looking for a nice big
1065 * contiguous allocation. Then reset and look
1066 * for anything you can find.
1067 */
1068 if (passno == 2 && bigalloc) {
1069 passno = 0;
1070 bigalloc = 0;
1071 }
1072 switch (passno++) {
1073 case 0: /* Search from hint->search_start to end of disk */
1074 start = hint->search_start;
1075 finish = SB_BLOCK_COUNT(s) - 1;
1076 break;
1077 case 1: /* Search from hint->beg to hint->search_start */
1078 start = hint->beg;
1079 finish = hint->search_start;
1080 break;
1081 case 2: /* Last chance: Search from 0 to hint->beg */
1082 start = 0;
1083 finish = hint->beg;
1084 break;
1085 default: /* We've tried searching everywhere, not enough space */
1086 /* Free the blocks */
1087 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001089 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1090 "reiserquota: freeing (nospace) %d blocks id=%u",
1091 amount_needed +
1092 hint->prealloc_size -
1093 nr_allocated,
1094 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001096 DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated); /* Free not allocated blocks */
1097 }
1098 while (nr_allocated--)
1099 reiserfs_free_block(hint->th, hint->inode,
1100 new_blocknrs[nr_allocated],
1101 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001103 return NO_DISK_SPACE;
1104 }
1105 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1106 new_blocknrs +
1107 nr_allocated,
1108 start, finish,
1109 bigalloc ?
1110 bigalloc : 1,
1111 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
1211/* These 2 functions are here to provide blocks reservation to the rest of kernel */
1212/* Reserve @blocks amount of blocks in fs pointed by @sb. Caller must make sure
1213 there are actually this much blocks on the FS available */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001214void reiserfs_claim_blocks_to_be_allocated(struct super_block *sb, /* super block of
1215 filesystem where
1216 blocks should be
1217 reserved */
1218 int blocks /* How much to reserve */
1219 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 /* Fast case, if reservation is zero - exit immediately. */
1223 if (!blocks)
1224 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001226 spin_lock(&REISERFS_SB(sb)->bitmap_lock);
1227 REISERFS_SB(sb)->reserved_blocks += blocks;
1228 spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231/* Unreserve @blocks amount of blocks in fs pointed by @sb */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001232void reiserfs_release_claimed_blocks(struct super_block *sb, /* super block of
1233 filesystem where
1234 blocks should be
1235 reserved */
1236 int blocks /* How much to unreserve */
1237 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240 /* Fast case, if unreservation is zero - exit immediately. */
1241 if (!blocks)
1242 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 spin_lock(&REISERFS_SB(sb)->bitmap_lock);
1245 REISERFS_SB(sb)->reserved_blocks -= blocks;
1246 spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
1247 RFALSE(REISERFS_SB(sb)->reserved_blocks < 0,
1248 "amount of blocks reserved became zero?");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
1251/* This function estimates how much pages we will be able to write to FS
1252 used for reiserfs_file_write() purposes for now. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001253int reiserfs_can_fit_pages(struct super_block *sb /* superblock of filesystem
1254 to estimate space */ )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 int space;
1257
1258 spin_lock(&REISERFS_SB(sb)->bitmap_lock);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001259 space =
1260 (SB_FREE_BLOCKS(sb) -
1261 REISERFS_SB(sb)->reserved_blocks) >> (PAGE_CACHE_SHIFT -
1262 sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
1264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 return space > 0 ? space : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}