Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README | |
3 | */ | |
4 | /* Reiserfs block (de)allocator, bitmap-based. */ | |
5 | ||
1da177e4 LT |
6 | #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> | |
6f01046b | 12 | #include <linux/vmalloc.h> |
1da177e4 LT |
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 | ||
bd4c625c LT |
49 | static inline void get_bit_address(struct super_block *s, |
50 | b_blocknr_t block, int *bmap_nr, int *offset) | |
1da177e4 | 51 | { |
bd4c625c LT |
52 | /* It is in the bitmap block number equal to the block |
53 | * number divided by the number of bits in a block. */ | |
e1fabd3c | 54 | *bmap_nr = block >> (s->s_blocksize_bits + 3); |
bd4c625c LT |
55 | /* Within that bitmap block it is located at bit offset *offset. */ |
56 | *offset = block & ((s->s_blocksize << 3) - 1); | |
1da177e4 LT |
57 | } |
58 | ||
59 | #ifdef CONFIG_REISERFS_CHECK | |
bd4c625c | 60 | int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value) |
1da177e4 | 61 | { |
e1fabd3c | 62 | int bmap, offset; |
1da177e4 | 63 | |
bd4c625c LT |
64 | if (block == 0 || block >= SB_BLOCK_COUNT(s)) { |
65 | reiserfs_warning(s, | |
66 | "vs-4010: is_reusable: block number is out of range %lu (%u)", | |
67 | block, SB_BLOCK_COUNT(s)); | |
68 | return 0; | |
1da177e4 | 69 | } |
1da177e4 | 70 | |
e1fabd3c JM |
71 | get_bit_address(s, block, &bmap, &offset); |
72 | ||
73 | /* Old format filesystem? Unlikely, but the bitmaps are all up front so | |
74 | * we need to account for it. */ | |
75 | if (unlikely(test_bit(REISERFS_OLD_FORMAT, | |
76 | &(REISERFS_SB(s)->s_properties)))) { | |
77 | b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1; | |
78 | if (block >= bmap1 && block <= bmap1 + SB_BMAP_NR(s)) { | |
79 | reiserfs_warning(s, "vs: 4019: is_reusable: " | |
80 | "bitmap block %lu(%u) can't be freed or reused", | |
81 | block, SB_BMAP_NR(s)); | |
82 | return 0; | |
83 | } | |
84 | } else { | |
85 | if (offset == 0) { | |
bd4c625c LT |
86 | reiserfs_warning(s, "vs: 4020: is_reusable: " |
87 | "bitmap block %lu(%u) can't be freed or reused", | |
88 | block, SB_BMAP_NR(s)); | |
89 | return 0; | |
90 | } | |
e1fabd3c | 91 | } |
1da177e4 | 92 | |
e1fabd3c | 93 | if (bmap >= SB_BMAP_NR(s)) { |
bd4c625c LT |
94 | reiserfs_warning(s, |
95 | "vs-4030: is_reusable: there is no so many bitmap blocks: " | |
e1fabd3c | 96 | "block=%lu, bitmap_nr=%d", block, bmap); |
bd4c625c LT |
97 | return 0; |
98 | } | |
1da177e4 | 99 | |
bd4c625c LT |
100 | if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) { |
101 | reiserfs_warning(s, | |
102 | "vs-4050: is_reusable: this is root block (%u), " | |
103 | "it must be busy", SB_ROOT_BLOCK(s)); | |
104 | return 0; | |
105 | } | |
106 | ||
107 | return 1; | |
1da177e4 | 108 | } |
bd4c625c | 109 | #endif /* CONFIG_REISERFS_CHECK */ |
1da177e4 LT |
110 | |
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. */ | |
bd4c625c LT |
113 | static inline int is_block_in_journal(struct super_block *s, int bmap, int |
114 | off, int *next) | |
1da177e4 | 115 | { |
bd4c625c LT |
116 | b_blocknr_t tmp; |
117 | ||
118 | 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; | |
1da177e4 | 128 | } |
bd4c625c | 129 | return 0; |
1da177e4 LT |
130 | } |
131 | ||
132 | /* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap | |
133 | * block; */ | |
bd4c625c LT |
134 | static int scan_bitmap_block(struct reiserfs_transaction_handle *th, |
135 | int bmap_n, int *beg, int boundary, int min, | |
136 | int max, int unfm) | |
1da177e4 | 137 | { |
bd4c625c LT |
138 | struct super_block *s = th->t_super; |
139 | struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n]; | |
0b3dc17b | 140 | struct buffer_head *bh; |
bd4c625c LT |
141 | int end, next; |
142 | int org = *beg; | |
1da177e4 | 143 | |
bd4c625c | 144 | BUG_ON(!th->t_trans_id); |
1da177e4 | 145 | |
bd4c625c LT |
146 | 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); | |
1da177e4 LT |
149 | /* 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. */ | |
1da177e4 | 153 | |
bd4c625c LT |
154 | if (!bi) { |
155 | reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d", | |
156 | bmap_n); | |
157 | return 0; | |
158 | } | |
0b3dc17b | 159 | |
5065227b JM |
160 | bh = reiserfs_read_bitmap_block(s, bmap_n); |
161 | if (bh == NULL) | |
162 | return 0; | |
1da177e4 | 163 | |
bd4c625c LT |
164 | while (1) { |
165 | cont: | |
0b3dc17b JM |
166 | if (bi->free_count < min) { |
167 | brelse(bh); | |
bd4c625c | 168 | return 0; // No free blocks in this bitmap |
0b3dc17b | 169 | } |
bd4c625c LT |
170 | |
171 | /* search for a first zero bit -- beggining of a window */ | |
172 | *beg = reiserfs_find_next_zero_le_bit | |
0b3dc17b | 173 | ((unsigned long *)(bh->b_data), boundary, *beg); |
bd4c625c LT |
174 | |
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 */ | |
0b3dc17b | 177 | brelse(bh); |
bd4c625c | 178 | return 0; |
1da177e4 | 179 | } |
1da177e4 | 180 | |
bd4c625c LT |
181 | 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 | |
0b3dc17b | 186 | || reiserfs_test_le_bit(end, bh->b_data)) { |
bd4c625c LT |
187 | 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 | } | |
1da177e4 | 195 | |
bd4c625c LT |
196 | /* 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; | |
0b3dc17b | 200 | reiserfs_prepare_for_journal(s, bh, 1); |
bd4c625c LT |
201 | /* 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 | |
0b3dc17b | 205 | (i, bh->b_data)) { |
bd4c625c LT |
206 | /* 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 | |
0b3dc17b JM |
217 | (i, bh->b_data); |
218 | reiserfs_restore_prepared_buffer(s, bh); | |
bd4c625c LT |
219 | *beg = org; |
220 | /* ... and search again in current block from beginning */ | |
221 | goto cont; | |
222 | } | |
223 | } | |
224 | bi->free_count -= (end - *beg); | |
0b3dc17b JM |
225 | journal_mark_dirty(th, s, bh); |
226 | brelse(bh); | |
bd4c625c LT |
227 | |
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 | } | |
1da177e4 | 238 | } |
1da177e4 LT |
239 | } |
240 | ||
bd4c625c LT |
241 | static int bmap_hash_id(struct super_block *s, u32 id) |
242 | { | |
243 | char *hash_in = NULL; | |
244 | unsigned long hash; | |
245 | unsigned bm; | |
246 | ||
247 | 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; | |
1da177e4 LT |
260 | } |
261 | ||
262 | /* | |
263 | * hashes the id and then returns > 0 if the block group for the | |
264 | * corresponding hash is full | |
265 | */ | |
bd4c625c LT |
266 | static inline int block_group_used(struct super_block *s, u32 id) |
267 | { | |
5065227b JM |
268 | 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)) { | |
bd4c625c LT |
282 | return 0; |
283 | } | |
284 | return 1; | |
1da177e4 LT |
285 | } |
286 | ||
287 | /* | |
288 | * the packing is returned in disk byte order | |
289 | */ | |
bd4c625c | 290 | __le32 reiserfs_choose_packing(struct inode * dir) |
3e8962be | 291 | { |
bd4c625c LT |
292 | __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; | |
1da177e4 | 306 | } |
bd4c625c | 307 | |
1da177e4 LT |
308 | /* 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. */ | |
bd4c625c LT |
310 | static int scan_bitmap(struct reiserfs_transaction_handle *th, |
311 | b_blocknr_t * start, b_blocknr_t finish, | |
312 | int min, int max, int unfm, unsigned long file_block) | |
1da177e4 | 313 | { |
bd4c625c LT |
314 | 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. */ | |
318 | ||
319 | int bm, off; | |
320 | int end_bm, end_off; | |
321 | int off_max = s->s_blocksize << 3; | |
322 | ||
323 | BUG_ON(!th->t_trans_id); | |
324 | ||
325 | 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 | |
328 | ||
329 | 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); | |
335 | ||
336 | /* 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 | } | |
363 | ||
364 | 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 | } | |
370 | ||
371 | 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; | |
1da177e4 LT |
377 | |
378 | } | |
379 | ||
bd4c625c LT |
380 | static void _reiserfs_free_block(struct reiserfs_transaction_handle *th, |
381 | struct inode *inode, b_blocknr_t block, | |
382 | int for_unformatted) | |
1da177e4 | 383 | { |
bd4c625c LT |
384 | struct super_block *s = th->t_super; |
385 | struct reiserfs_super_block *rs; | |
0b3dc17b | 386 | struct buffer_head *sbh, *bmbh; |
bd4c625c LT |
387 | struct reiserfs_bitmap_info *apbi; |
388 | int nr, offset; | |
1da177e4 | 389 | |
bd4c625c | 390 | BUG_ON(!th->t_trans_id); |
1da177e4 | 391 | |
bd4c625c | 392 | PROC_INFO_INC(s, free_block); |
1da177e4 | 393 | |
bd4c625c LT |
394 | rs = SB_DISK_SUPER_BLOCK(s); |
395 | sbh = SB_BUFFER_WITH_SB(s); | |
396 | apbi = SB_AP_BITMAP(s); | |
1da177e4 | 397 | |
bd4c625c | 398 | get_bit_address(s, block, &nr, &offset); |
1da177e4 | 399 | |
bd4c625c LT |
400 | 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 | } | |
406 | ||
5065227b JM |
407 | bmbh = reiserfs_read_bitmap_block(s, nr); |
408 | if (!bmbh) | |
409 | return; | |
0b3dc17b JM |
410 | |
411 | reiserfs_prepare_for_journal(s, bmbh, 1); | |
bd4c625c LT |
412 | |
413 | /* clear bit for the given block in bit map */ | |
0b3dc17b | 414 | if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) { |
bd4c625c LT |
415 | 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++; | |
0b3dc17b JM |
420 | journal_mark_dirty(th, s, bmbh); |
421 | brelse(bmbh); | |
bd4c625c LT |
422 | |
423 | reiserfs_prepare_for_journal(s, sbh, 1); | |
424 | /* update super block */ | |
425 | set_sb_free_blocks(rs, sb_free_blocks(rs) + 1); | |
426 | ||
427 | journal_mark_dirty(th, s, sbh); | |
428 | if (for_unformatted) | |
429 | DQUOT_FREE_BLOCK_NODIRTY(inode, 1); | |
1da177e4 LT |
430 | } |
431 | ||
bd4c625c LT |
432 | void reiserfs_free_block(struct reiserfs_transaction_handle *th, |
433 | struct inode *inode, b_blocknr_t block, | |
434 | int for_unformatted) | |
1da177e4 | 435 | { |
bd4c625c | 436 | struct super_block *s = th->t_super; |
1da177e4 | 437 | |
bd4c625c | 438 | BUG_ON(!th->t_trans_id); |
1da177e4 | 439 | |
bd4c625c LT |
440 | RFALSE(!s, "vs-4061: trying to free block on nonexistent device"); |
441 | RFALSE(is_reusable(s, block, 1) == 0, | |
442 | "vs-4071: can not free such block"); | |
443 | /* mark it before we clear it, just in case */ | |
444 | journal_mark_freed(th, s, block); | |
445 | _reiserfs_free_block(th, inode, block, for_unformatted); | |
1da177e4 LT |
446 | } |
447 | ||
448 | /* preallocated blocks don't need to be run through journal_mark_freed */ | |
bd4c625c LT |
449 | static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th, |
450 | struct inode *inode, b_blocknr_t block) | |
451 | { | |
452 | RFALSE(!th->t_super, | |
453 | "vs-4060: trying to free block on nonexistent device"); | |
454 | RFALSE(is_reusable(th->t_super, block, 1) == 0, | |
455 | "vs-4070: can not free such block"); | |
456 | BUG_ON(!th->t_trans_id); | |
457 | _reiserfs_free_block(th, inode, block, 1); | |
1da177e4 LT |
458 | } |
459 | ||
bd4c625c LT |
460 | static void __discard_prealloc(struct reiserfs_transaction_handle *th, |
461 | struct reiserfs_inode_info *ei) | |
1da177e4 | 462 | { |
bd4c625c LT |
463 | unsigned long save = ei->i_prealloc_block; |
464 | int dirty = 0; | |
465 | struct inode *inode = &ei->vfs_inode; | |
466 | BUG_ON(!th->t_trans_id); | |
1da177e4 | 467 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
468 | if (ei->i_prealloc_count < 0) |
469 | reiserfs_warning(th->t_super, | |
470 | "zam-4001:%s: inode has negative prealloc blocks count.", | |
471 | __FUNCTION__); | |
1da177e4 | 472 | #endif |
bd4c625c LT |
473 | while (ei->i_prealloc_count > 0) { |
474 | reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block); | |
475 | ei->i_prealloc_block++; | |
476 | ei->i_prealloc_count--; | |
477 | dirty = 1; | |
478 | } | |
479 | if (dirty) | |
480 | reiserfs_update_sd(th, inode); | |
481 | ei->i_prealloc_block = save; | |
482 | list_del_init(&(ei->i_prealloc_list)); | |
1da177e4 LT |
483 | } |
484 | ||
485 | /* FIXME: It should be inline function */ | |
bd4c625c LT |
486 | void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th, |
487 | struct inode *inode) | |
1da177e4 | 488 | { |
bd4c625c LT |
489 | struct reiserfs_inode_info *ei = REISERFS_I(inode); |
490 | BUG_ON(!th->t_trans_id); | |
491 | if (ei->i_prealloc_count) | |
492 | __discard_prealloc(th, ei); | |
1da177e4 LT |
493 | } |
494 | ||
bd4c625c | 495 | void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th) |
1da177e4 | 496 | { |
bd4c625c | 497 | struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list; |
1da177e4 | 498 | |
bd4c625c | 499 | BUG_ON(!th->t_trans_id); |
1da177e4 | 500 | |
bd4c625c LT |
501 | while (!list_empty(plist)) { |
502 | struct reiserfs_inode_info *ei; | |
503 | ei = list_entry(plist->next, struct reiserfs_inode_info, | |
504 | i_prealloc_list); | |
1da177e4 | 505 | #ifdef CONFIG_REISERFS_CHECK |
bd4c625c LT |
506 | if (!ei->i_prealloc_count) { |
507 | reiserfs_warning(th->t_super, | |
508 | "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.", | |
509 | __FUNCTION__); | |
510 | } | |
1da177e4 | 511 | #endif |
bd4c625c LT |
512 | __discard_prealloc(th, ei); |
513 | } | |
1da177e4 LT |
514 | } |
515 | ||
bd4c625c | 516 | void reiserfs_init_alloc_options(struct super_block *s) |
1da177e4 | 517 | { |
bd4c625c LT |
518 | set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s)); |
519 | set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s)); | |
520 | set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s)); | |
1da177e4 LT |
521 | } |
522 | ||
523 | /* block allocator related options are parsed here */ | |
bd4c625c | 524 | int reiserfs_parse_alloc_options(struct super_block *s, char *options) |
1da177e4 | 525 | { |
bd4c625c LT |
526 | char *this_char, *value; |
527 | ||
528 | REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */ | |
529 | ||
530 | while ((this_char = strsep(&options, ":")) != NULL) { | |
531 | if ((value = strchr(this_char, '=')) != NULL) | |
532 | *value++ = 0; | |
533 | ||
534 | if (!strcmp(this_char, "concentrating_formatted_nodes")) { | |
535 | int temp; | |
536 | SET_OPTION(concentrating_formatted_nodes); | |
537 | temp = (value | |
538 | && *value) ? simple_strtoul(value, &value, | |
539 | 0) : 10; | |
540 | if (temp <= 0 || temp > 100) { | |
541 | REISERFS_SB(s)->s_alloc_options.border = 10; | |
542 | } else { | |
543 | REISERFS_SB(s)->s_alloc_options.border = | |
544 | 100 / temp; | |
545 | } | |
546 | continue; | |
547 | } | |
548 | if (!strcmp(this_char, "displacing_large_files")) { | |
549 | SET_OPTION(displacing_large_files); | |
550 | REISERFS_SB(s)->s_alloc_options.large_file_size = | |
551 | (value | |
552 | && *value) ? simple_strtoul(value, &value, 0) : 16; | |
553 | continue; | |
554 | } | |
555 | if (!strcmp(this_char, "displacing_new_packing_localities")) { | |
556 | SET_OPTION(displacing_new_packing_localities); | |
557 | continue; | |
558 | }; | |
559 | ||
560 | if (!strcmp(this_char, "old_hashed_relocation")) { | |
561 | SET_OPTION(old_hashed_relocation); | |
562 | continue; | |
563 | } | |
1da177e4 | 564 | |
bd4c625c LT |
565 | if (!strcmp(this_char, "new_hashed_relocation")) { |
566 | SET_OPTION(new_hashed_relocation); | |
567 | continue; | |
568 | } | |
1da177e4 | 569 | |
bd4c625c LT |
570 | if (!strcmp(this_char, "dirid_groups")) { |
571 | SET_OPTION(dirid_groups); | |
572 | continue; | |
573 | } | |
574 | if (!strcmp(this_char, "oid_groups")) { | |
575 | SET_OPTION(oid_groups); | |
576 | continue; | |
577 | } | |
578 | if (!strcmp(this_char, "packing_groups")) { | |
579 | SET_OPTION(packing_groups); | |
580 | continue; | |
581 | } | |
582 | if (!strcmp(this_char, "hashed_formatted_nodes")) { | |
583 | SET_OPTION(hashed_formatted_nodes); | |
584 | continue; | |
585 | } | |
1da177e4 | 586 | |
bd4c625c LT |
587 | if (!strcmp(this_char, "skip_busy")) { |
588 | SET_OPTION(skip_busy); | |
589 | continue; | |
590 | } | |
1da177e4 | 591 | |
bd4c625c LT |
592 | if (!strcmp(this_char, "hundredth_slices")) { |
593 | SET_OPTION(hundredth_slices); | |
594 | continue; | |
595 | } | |
1da177e4 | 596 | |
bd4c625c LT |
597 | if (!strcmp(this_char, "old_way")) { |
598 | SET_OPTION(old_way); | |
599 | continue; | |
600 | } | |
1da177e4 | 601 | |
bd4c625c LT |
602 | if (!strcmp(this_char, "displace_based_on_dirid")) { |
603 | SET_OPTION(displace_based_on_dirid); | |
604 | continue; | |
605 | } | |
1da177e4 | 606 | |
bd4c625c LT |
607 | if (!strcmp(this_char, "preallocmin")) { |
608 | REISERFS_SB(s)->s_alloc_options.preallocmin = | |
609 | (value | |
610 | && *value) ? simple_strtoul(value, &value, 0) : 4; | |
611 | continue; | |
612 | } | |
613 | ||
614 | if (!strcmp(this_char, "preallocsize")) { | |
615 | REISERFS_SB(s)->s_alloc_options.preallocsize = | |
616 | (value | |
617 | && *value) ? simple_strtoul(value, &value, | |
618 | 0) : | |
619 | PREALLOCATION_SIZE; | |
620 | continue; | |
621 | } | |
1da177e4 | 622 | |
bd4c625c LT |
623 | reiserfs_warning(s, "zam-4001: %s : unknown option - %s", |
624 | __FUNCTION__, this_char); | |
625 | return 1; | |
1da177e4 LT |
626 | } |
627 | ||
bd4c625c LT |
628 | reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s)); |
629 | return 0; | |
1da177e4 | 630 | } |
bd4c625c LT |
631 | |
632 | static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint) | |
1da177e4 | 633 | { |
bd4c625c LT |
634 | char *hash_in; |
635 | if (hint->formatted_node) { | |
636 | hash_in = (char *)&hint->key.k_dir_id; | |
637 | } else { | |
638 | if (!hint->inode) { | |
639 | //hint->search_start = hint->beg; | |
640 | hash_in = (char *)&hint->key.k_dir_id; | |
641 | } else | |
642 | if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) | |
643 | hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id); | |
644 | else | |
645 | hash_in = | |
646 | (char *)(&INODE_PKEY(hint->inode)->k_objectid); | |
647 | } | |
1da177e4 | 648 | |
bd4c625c LT |
649 | hint->search_start = |
650 | hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg); | |
1da177e4 LT |
651 | } |
652 | ||
653 | /* | |
654 | * Relocation based on dirid, hashing them into a given bitmap block | |
655 | * files. Formatted nodes are unaffected, a seperate policy covers them | |
656 | */ | |
bd4c625c | 657 | static void dirid_groups(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 658 | { |
bd4c625c LT |
659 | unsigned long hash; |
660 | __u32 dirid = 0; | |
661 | int bm = 0; | |
662 | struct super_block *sb = hint->th->t_super; | |
1da177e4 | 663 | if (hint->inode) |
bd4c625c LT |
664 | dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id); |
665 | else if (hint->formatted_node) | |
666 | dirid = hint->key.k_dir_id; | |
667 | ||
668 | if (dirid) { | |
669 | bm = bmap_hash_id(sb, dirid); | |
670 | hash = bm * (sb->s_blocksize << 3); | |
671 | /* give a portion of the block group to metadata */ | |
672 | if (hint->inode) | |
673 | hash += sb->s_blocksize / 2; | |
674 | hint->search_start = hash; | |
675 | } | |
1da177e4 LT |
676 | } |
677 | ||
678 | /* | |
679 | * Relocation based on oid, hashing them into a given bitmap block | |
680 | * files. Formatted nodes are unaffected, a seperate policy covers them | |
681 | */ | |
bd4c625c | 682 | static void oid_groups(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 683 | { |
bd4c625c LT |
684 | if (hint->inode) { |
685 | unsigned long hash; | |
686 | __u32 oid; | |
687 | __u32 dirid; | |
688 | int bm; | |
689 | ||
690 | dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id); | |
691 | ||
692 | /* keep the root dir and it's first set of subdirs close to | |
693 | * the start of the disk | |
694 | */ | |
695 | if (dirid <= 2) | |
696 | hash = (hint->inode->i_sb->s_blocksize << 3); | |
697 | else { | |
698 | oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid); | |
699 | bm = bmap_hash_id(hint->inode->i_sb, oid); | |
700 | hash = bm * (hint->inode->i_sb->s_blocksize << 3); | |
701 | } | |
702 | hint->search_start = hash; | |
1da177e4 | 703 | } |
1da177e4 LT |
704 | } |
705 | ||
706 | /* returns 1 if it finds an indirect item and gets valid hint info | |
707 | * from it, otherwise 0 | |
708 | */ | |
bd4c625c | 709 | static int get_left_neighbor(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 710 | { |
bd4c625c LT |
711 | struct path *path; |
712 | struct buffer_head *bh; | |
713 | struct item_head *ih; | |
714 | int pos_in_item; | |
715 | __le32 *item; | |
716 | int ret = 0; | |
717 | ||
718 | if (!hint->path) /* reiserfs code can call this function w/o pointer to path | |
1da177e4 | 719 | * structure supplied; then we rely on supplied search_start */ |
bd4c625c LT |
720 | return 0; |
721 | ||
722 | path = hint->path; | |
723 | bh = get_last_bh(path); | |
724 | RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor"); | |
725 | ih = get_ih(path); | |
726 | pos_in_item = path->pos_in_item; | |
727 | item = get_item(path); | |
728 | ||
729 | hint->search_start = bh->b_blocknr; | |
730 | ||
731 | if (!hint->formatted_node && is_indirect_le_ih(ih)) { | |
732 | /* for indirect item: go to left and look for the first non-hole entry | |
733 | in the indirect item */ | |
734 | if (pos_in_item == I_UNFM_NUM(ih)) | |
735 | pos_in_item--; | |
736 | // pos_in_item = I_UNFM_NUM (ih) - 1; | |
737 | while (pos_in_item >= 0) { | |
738 | int t = get_block_num(item, pos_in_item); | |
739 | if (t) { | |
740 | hint->search_start = t; | |
741 | ret = 1; | |
742 | break; | |
743 | } | |
744 | pos_in_item--; | |
745 | } | |
1da177e4 | 746 | } |
1da177e4 | 747 | |
bd4c625c LT |
748 | /* does result value fit into specified region? */ |
749 | return ret; | |
1da177e4 LT |
750 | } |
751 | ||
752 | /* should be, if formatted node, then try to put on first part of the device | |
753 | specified as number of percent with mount option device, else try to put | |
754 | on last of device. This is not to say it is good code to do so, | |
755 | but the effect should be measured. */ | |
bd4c625c LT |
756 | static inline void set_border_in_hint(struct super_block *s, |
757 | reiserfs_blocknr_hint_t * hint) | |
1da177e4 | 758 | { |
bd4c625c LT |
759 | b_blocknr_t border = |
760 | SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border; | |
1da177e4 | 761 | |
bd4c625c LT |
762 | if (hint->formatted_node) |
763 | hint->end = border - 1; | |
764 | else | |
765 | hint->beg = border; | |
1da177e4 LT |
766 | } |
767 | ||
bd4c625c | 768 | static inline void displace_large_file(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 769 | { |
bd4c625c LT |
770 | if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) |
771 | hint->search_start = | |
772 | hint->beg + | |
773 | keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id), | |
774 | 4) % (hint->end - hint->beg); | |
775 | else | |
776 | hint->search_start = | |
777 | hint->beg + | |
778 | keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid), | |
779 | 4) % (hint->end - hint->beg); | |
1da177e4 LT |
780 | } |
781 | ||
bd4c625c | 782 | static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 783 | { |
bd4c625c | 784 | char *hash_in; |
1da177e4 | 785 | |
bd4c625c LT |
786 | if (!hint->inode) |
787 | hash_in = (char *)&hint->key.k_dir_id; | |
788 | else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super)) | |
789 | hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id); | |
790 | else | |
791 | hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid); | |
1da177e4 | 792 | |
bd4c625c LT |
793 | hint->search_start = |
794 | hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg); | |
1da177e4 LT |
795 | } |
796 | ||
bd4c625c LT |
797 | static inline int |
798 | this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t * | |
799 | hint) | |
1da177e4 | 800 | { |
bd4c625c LT |
801 | return hint->block == |
802 | REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size; | |
1da177e4 LT |
803 | } |
804 | ||
805 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES | |
bd4c625c | 806 | static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 807 | { |
bd4c625c | 808 | struct in_core_key *key = &hint->key; |
1da177e4 | 809 | |
bd4c625c LT |
810 | hint->th->displace_new_blocks = 0; |
811 | hint->search_start = | |
812 | hint->beg + keyed_hash((char *)(&key->k_objectid), | |
813 | 4) % (hint->end - hint->beg); | |
1da177e4 | 814 | } |
bd4c625c | 815 | #endif |
1da177e4 | 816 | |
bd4c625c | 817 | static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 818 | { |
bd4c625c LT |
819 | b_blocknr_t border; |
820 | u32 hash_in; | |
1da177e4 | 821 | |
bd4c625c LT |
822 | if (hint->formatted_node || hint->inode == NULL) { |
823 | return 0; | |
824 | } | |
1da177e4 | 825 | |
bd4c625c LT |
826 | hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id); |
827 | border = | |
828 | hint->beg + (u32) keyed_hash(((char *)(&hash_in)), | |
829 | 4) % (hint->end - hint->beg - 1); | |
830 | if (border > hint->search_start) | |
831 | hint->search_start = border; | |
1da177e4 | 832 | |
bd4c625c | 833 | return 1; |
1da177e4 LT |
834 | } |
835 | ||
bd4c625c | 836 | static inline int old_way(reiserfs_blocknr_hint_t * hint) |
1da177e4 | 837 | { |
bd4c625c LT |
838 | b_blocknr_t border; |
839 | ||
840 | if (hint->formatted_node || hint->inode == NULL) { | |
841 | return 0; | |
842 | } | |
843 | ||
844 | border = | |
845 | hint->beg + | |
846 | le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end - | |
847 | hint->beg); | |
848 | if (border > hint->search_start) | |
849 | hint->search_start = border; | |
1da177e4 | 850 | |
bd4c625c LT |
851 | return 1; |
852 | } | |
853 | ||
854 | static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint) | |
855 | { | |
856 | struct in_core_key *key = &hint->key; | |
857 | b_blocknr_t slice_start; | |
858 | ||
859 | slice_start = | |
860 | (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100); | |
861 | if (slice_start > hint->search_start | |
862 | || slice_start + (hint->end / 100) <= hint->search_start) { | |
863 | hint->search_start = slice_start; | |
864 | } | |
1da177e4 | 865 | } |
bd4c625c LT |
866 | |
867 | static void determine_search_start(reiserfs_blocknr_hint_t * hint, | |
868 | int amount_needed) | |
1da177e4 | 869 | { |
bd4c625c LT |
870 | struct super_block *s = hint->th->t_super; |
871 | int unfm_hint; | |
1da177e4 | 872 | |
bd4c625c LT |
873 | hint->beg = 0; |
874 | hint->end = SB_BLOCK_COUNT(s) - 1; | |
1da177e4 | 875 | |
bd4c625c LT |
876 | /* This is former border algorithm. Now with tunable border offset */ |
877 | if (concentrating_formatted_nodes(s)) | |
878 | set_border_in_hint(s, hint); | |
1da177e4 LT |
879 | |
880 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES | |
bd4c625c LT |
881 | /* whenever we create a new directory, we displace it. At first we will |
882 | hash for location, later we might look for a moderately empty place for | |
883 | it */ | |
884 | if (displacing_new_packing_localities(s) | |
885 | && hint->th->displace_new_blocks) { | |
886 | displace_new_packing_locality(hint); | |
887 | ||
888 | /* we do not continue determine_search_start, | |
889 | * if new packing locality is being displaced */ | |
890 | return; | |
891 | } | |
1da177e4 | 892 | #endif |
1da177e4 | 893 | |
bd4c625c LT |
894 | /* all persons should feel encouraged to add more special cases here and |
895 | * test them */ | |
1da177e4 | 896 | |
bd4c625c LT |
897 | if (displacing_large_files(s) && !hint->formatted_node |
898 | && this_blocknr_allocation_would_make_it_a_large_file(hint)) { | |
899 | displace_large_file(hint); | |
900 | return; | |
901 | } | |
1da177e4 | 902 | |
bd4c625c LT |
903 | /* if none of our special cases is relevant, use the left neighbor in the |
904 | tree order of the new node we are allocating for */ | |
905 | if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) { | |
906 | hash_formatted_node(hint); | |
907 | return; | |
908 | } | |
1da177e4 | 909 | |
bd4c625c LT |
910 | unfm_hint = get_left_neighbor(hint); |
911 | ||
912 | /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation, | |
913 | new blocks are displaced based on directory ID. Also, if suggested search_start | |
914 | is less than last preallocated block, we start searching from it, assuming that | |
915 | HDD dataflow is faster in forward direction */ | |
916 | if (TEST_OPTION(old_way, s)) { | |
917 | if (!hint->formatted_node) { | |
918 | if (!reiserfs_hashed_relocation(s)) | |
919 | old_way(hint); | |
920 | else if (!reiserfs_no_unhashed_relocation(s)) | |
921 | old_hashed_relocation(hint); | |
922 | ||
923 | if (hint->inode | |
924 | && hint->search_start < | |
925 | REISERFS_I(hint->inode)->i_prealloc_block) | |
926 | hint->search_start = | |
927 | REISERFS_I(hint->inode)->i_prealloc_block; | |
928 | } | |
929 | return; | |
1da177e4 | 930 | } |
1da177e4 | 931 | |
bd4c625c LT |
932 | /* This is an approach proposed by Hans */ |
933 | if (TEST_OPTION(hundredth_slices, s) | |
934 | && !(displacing_large_files(s) && !hint->formatted_node)) { | |
935 | hundredth_slices(hint); | |
936 | return; | |
937 | } | |
1da177e4 | 938 | |
bd4c625c LT |
939 | /* old_hashed_relocation only works on unformatted */ |
940 | if (!unfm_hint && !hint->formatted_node && | |
941 | TEST_OPTION(old_hashed_relocation, s)) { | |
942 | old_hashed_relocation(hint); | |
943 | } | |
944 | /* new_hashed_relocation works with both formatted/unformatted nodes */ | |
945 | if ((!unfm_hint || hint->formatted_node) && | |
946 | TEST_OPTION(new_hashed_relocation, s)) { | |
947 | new_hashed_relocation(hint); | |
948 | } | |
949 | /* dirid grouping works only on unformatted nodes */ | |
950 | if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) { | |
951 | dirid_groups(hint); | |
952 | } | |
1da177e4 | 953 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES |
bd4c625c LT |
954 | if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) { |
955 | dirid_groups(hint); | |
956 | } | |
1da177e4 LT |
957 | #endif |
958 | ||
bd4c625c LT |
959 | /* oid grouping works only on unformatted nodes */ |
960 | if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) { | |
961 | oid_groups(hint); | |
962 | } | |
963 | return; | |
1da177e4 LT |
964 | } |
965 | ||
966 | static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint) | |
967 | { | |
bd4c625c LT |
968 | /* make minimum size a mount option and benchmark both ways */ |
969 | /* we preallocate blocks only for regular files, specific size */ | |
970 | /* benchmark preallocating always and see what happens */ | |
971 | ||
972 | hint->prealloc_size = 0; | |
973 | ||
974 | if (!hint->formatted_node && hint->preallocate) { | |
975 | if (S_ISREG(hint->inode->i_mode) | |
976 | && hint->inode->i_size >= | |
977 | REISERFS_SB(hint->th->t_super)->s_alloc_options. | |
978 | preallocmin * hint->inode->i_sb->s_blocksize) | |
979 | hint->prealloc_size = | |
980 | REISERFS_SB(hint->th->t_super)->s_alloc_options. | |
981 | preallocsize - 1; | |
982 | } | |
983 | return CARRY_ON; | |
1da177e4 LT |
984 | } |
985 | ||
986 | /* XXX I know it could be merged with upper-level function; | |
987 | but may be result function would be too complex. */ | |
bd4c625c LT |
988 | static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint, |
989 | b_blocknr_t * new_blocknrs, | |
990 | b_blocknr_t start, | |
991 | b_blocknr_t finish, int min, | |
992 | int amount_needed, | |
993 | int prealloc_size) | |
1da177e4 | 994 | { |
bd4c625c LT |
995 | int rest = amount_needed; |
996 | int nr_allocated; | |
997 | ||
998 | while (rest > 0 && start <= finish) { | |
999 | nr_allocated = scan_bitmap(hint->th, &start, finish, min, | |
1000 | rest + prealloc_size, | |
1001 | !hint->formatted_node, hint->block); | |
1002 | ||
1003 | if (nr_allocated == 0) /* no new blocks allocated, return */ | |
1004 | break; | |
1005 | ||
1006 | /* fill free_blocknrs array first */ | |
1007 | while (rest > 0 && nr_allocated > 0) { | |
1008 | *new_blocknrs++ = start++; | |
1009 | rest--; | |
1010 | nr_allocated--; | |
1011 | } | |
1da177e4 | 1012 | |
bd4c625c LT |
1013 | /* do we have something to fill prealloc. array also ? */ |
1014 | if (nr_allocated > 0) { | |
1015 | /* it means prealloc_size was greater that 0 and we do preallocation */ | |
1016 | list_add(&REISERFS_I(hint->inode)->i_prealloc_list, | |
1017 | &SB_JOURNAL(hint->th->t_super)-> | |
1018 | j_prealloc_list); | |
1019 | REISERFS_I(hint->inode)->i_prealloc_block = start; | |
1020 | REISERFS_I(hint->inode)->i_prealloc_count = | |
1021 | nr_allocated; | |
1022 | break; | |
1023 | } | |
1da177e4 | 1024 | } |
1da177e4 | 1025 | |
bd4c625c | 1026 | return (amount_needed - rest); |
1da177e4 LT |
1027 | } |
1028 | ||
1029 | static inline int blocknrs_and_prealloc_arrays_from_search_start | |
bd4c625c LT |
1030 | (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs, |
1031 | int amount_needed) { | |
1032 | struct super_block *s = hint->th->t_super; | |
1033 | b_blocknr_t start = hint->search_start; | |
1034 | b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1; | |
1035 | int passno = 0; | |
1036 | int nr_allocated = 0; | |
1037 | int bigalloc = 0; | |
1038 | ||
1039 | determine_prealloc_size(hint); | |
1040 | if (!hint->formatted_node) { | |
1041 | int quota_ret; | |
1da177e4 | 1042 | #ifdef REISERQUOTA_DEBUG |
bd4c625c LT |
1043 | reiserfs_debug(s, REISERFS_DEBUG_CODE, |
1044 | "reiserquota: allocating %d blocks id=%u", | |
1045 | amount_needed, hint->inode->i_uid); | |
1da177e4 | 1046 | #endif |
bd4c625c LT |
1047 | quota_ret = |
1048 | DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed); | |
1049 | if (quota_ret) /* Quota exceeded? */ | |
1050 | return QUOTA_EXCEEDED; | |
1051 | if (hint->preallocate && hint->prealloc_size) { | |
1da177e4 | 1052 | #ifdef REISERQUOTA_DEBUG |
bd4c625c LT |
1053 | reiserfs_debug(s, REISERFS_DEBUG_CODE, |
1054 | "reiserquota: allocating (prealloc) %d blocks id=%u", | |
1055 | hint->prealloc_size, hint->inode->i_uid); | |
1da177e4 | 1056 | #endif |
bd4c625c LT |
1057 | quota_ret = |
1058 | DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode, | |
1059 | hint->prealloc_size); | |
1060 | if (quota_ret) | |
1061 | hint->preallocate = hint->prealloc_size = 0; | |
1062 | } | |
1063 | /* for unformatted nodes, force large allocations */ | |
1064 | bigalloc = amount_needed; | |
1da177e4 | 1065 | } |
1da177e4 | 1066 | |
bd4c625c LT |
1067 | do { |
1068 | /* in bigalloc mode, nr_allocated should stay zero until | |
1069 | * the entire allocation is filled | |
1070 | */ | |
1071 | if (unlikely(bigalloc && nr_allocated)) { | |
1072 | reiserfs_warning(s, "bigalloc is %d, nr_allocated %d\n", | |
1073 | bigalloc, nr_allocated); | |
1074 | /* reset things to a sane value */ | |
1075 | bigalloc = amount_needed - nr_allocated; | |
1076 | } | |
1077 | /* | |
1078 | * try pass 0 and pass 1 looking for a nice big | |
1079 | * contiguous allocation. Then reset and look | |
1080 | * for anything you can find. | |
1081 | */ | |
1082 | if (passno == 2 && bigalloc) { | |
1083 | passno = 0; | |
1084 | bigalloc = 0; | |
1085 | } | |
1086 | switch (passno++) { | |
1087 | case 0: /* Search from hint->search_start to end of disk */ | |
1088 | start = hint->search_start; | |
1089 | finish = SB_BLOCK_COUNT(s) - 1; | |
1090 | break; | |
1091 | case 1: /* Search from hint->beg to hint->search_start */ | |
1092 | start = hint->beg; | |
1093 | finish = hint->search_start; | |
1094 | break; | |
1095 | case 2: /* Last chance: Search from 0 to hint->beg */ | |
1096 | start = 0; | |
1097 | finish = hint->beg; | |
1098 | break; | |
1099 | default: /* We've tried searching everywhere, not enough space */ | |
1100 | /* Free the blocks */ | |
1101 | if (!hint->formatted_node) { | |
1da177e4 | 1102 | #ifdef REISERQUOTA_DEBUG |
bd4c625c LT |
1103 | reiserfs_debug(s, REISERFS_DEBUG_CODE, |
1104 | "reiserquota: freeing (nospace) %d blocks id=%u", | |
1105 | amount_needed + | |
1106 | hint->prealloc_size - | |
1107 | nr_allocated, | |
1108 | hint->inode->i_uid); | |
1da177e4 | 1109 | #endif |
bd4c625c LT |
1110 | DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated); /* Free not allocated blocks */ |
1111 | } | |
1112 | while (nr_allocated--) | |
1113 | reiserfs_free_block(hint->th, hint->inode, | |
1114 | new_blocknrs[nr_allocated], | |
1115 | !hint->formatted_node); | |
1116 | ||
1117 | return NO_DISK_SPACE; | |
1118 | } | |
1119 | } while ((nr_allocated += allocate_without_wrapping_disk(hint, | |
1120 | new_blocknrs + | |
1121 | nr_allocated, | |
1122 | start, finish, | |
1123 | bigalloc ? | |
1124 | bigalloc : 1, | |
1125 | amount_needed - | |
1126 | nr_allocated, | |
1127 | hint-> | |
1128 | prealloc_size)) | |
1129 | < amount_needed); | |
1130 | if (!hint->formatted_node && | |
1131 | amount_needed + hint->prealloc_size > | |
1132 | nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) { | |
1133 | /* Some of preallocation blocks were not allocated */ | |
1da177e4 | 1134 | #ifdef REISERQUOTA_DEBUG |
bd4c625c LT |
1135 | reiserfs_debug(s, REISERFS_DEBUG_CODE, |
1136 | "reiserquota: freeing (failed prealloc) %d blocks id=%u", | |
1137 | amount_needed + hint->prealloc_size - | |
1138 | nr_allocated - | |
1139 | REISERFS_I(hint->inode)->i_prealloc_count, | |
1140 | hint->inode->i_uid); | |
1da177e4 | 1141 | #endif |
bd4c625c LT |
1142 | DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + |
1143 | hint->prealloc_size - nr_allocated - | |
1144 | REISERFS_I(hint->inode)-> | |
1145 | i_prealloc_count); | |
1146 | } | |
1da177e4 | 1147 | |
bd4c625c | 1148 | return CARRY_ON; |
1da177e4 LT |
1149 | } |
1150 | ||
1151 | /* grab new blocknrs from preallocated list */ | |
1152 | /* return amount still needed after using them */ | |
bd4c625c LT |
1153 | static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint, |
1154 | b_blocknr_t * new_blocknrs, | |
1155 | int amount_needed) | |
1da177e4 | 1156 | { |
bd4c625c | 1157 | struct inode *inode = hint->inode; |
1da177e4 | 1158 | |
bd4c625c LT |
1159 | if (REISERFS_I(inode)->i_prealloc_count > 0) { |
1160 | while (amount_needed) { | |
1da177e4 | 1161 | |
bd4c625c LT |
1162 | *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++; |
1163 | REISERFS_I(inode)->i_prealloc_count--; | |
1da177e4 | 1164 | |
bd4c625c | 1165 | amount_needed--; |
1da177e4 | 1166 | |
bd4c625c LT |
1167 | if (REISERFS_I(inode)->i_prealloc_count <= 0) { |
1168 | list_del(&REISERFS_I(inode)->i_prealloc_list); | |
1169 | break; | |
1170 | } | |
1171 | } | |
1da177e4 | 1172 | } |
bd4c625c LT |
1173 | /* return amount still needed after using preallocated blocks */ |
1174 | return amount_needed; | |
1da177e4 LT |
1175 | } |
1176 | ||
bd4c625c LT |
1177 | int 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 |
1178 | already reserved */ ) | |
1da177e4 | 1179 | { |
bd4c625c LT |
1180 | int initial_amount_needed = amount_needed; |
1181 | int ret; | |
1182 | struct super_block *s = hint->th->t_super; | |
1183 | ||
1184 | /* Check if there is enough space, taking into account reserved space */ | |
1185 | if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks < | |
1186 | amount_needed - reserved_by_us) | |
1187 | return NO_DISK_SPACE; | |
1188 | /* should this be if !hint->inode && hint->preallocate? */ | |
1189 | /* do you mean hint->formatted_node can be removed ? - Zam */ | |
1190 | /* hint->formatted_node cannot be removed because we try to access | |
1191 | inode information here, and there is often no inode assotiated with | |
1192 | metadata allocations - green */ | |
1193 | ||
1194 | if (!hint->formatted_node && hint->preallocate) { | |
1195 | amount_needed = use_preallocated_list_if_available | |
1196 | (hint, new_blocknrs, amount_needed); | |
1197 | if (amount_needed == 0) /* all blocknrs we need we got from | |
1198 | prealloc. list */ | |
1199 | return CARRY_ON; | |
1200 | new_blocknrs += (initial_amount_needed - amount_needed); | |
1201 | } | |
1202 | ||
1203 | /* find search start and save it in hint structure */ | |
1204 | determine_search_start(hint, amount_needed); | |
1205 | if (hint->search_start >= SB_BLOCK_COUNT(s)) | |
1206 | hint->search_start = SB_BLOCK_COUNT(s) - 1; | |
1207 | ||
1208 | /* allocation itself; fill new_blocknrs and preallocation arrays */ | |
1209 | ret = blocknrs_and_prealloc_arrays_from_search_start | |
1da177e4 | 1210 | (hint, new_blocknrs, amount_needed); |
bd4c625c LT |
1211 | |
1212 | /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we | |
1213 | * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second | |
1214 | * variant) */ | |
1215 | ||
1216 | if (ret != CARRY_ON) { | |
1217 | while (amount_needed++ < initial_amount_needed) { | |
1218 | reiserfs_free_block(hint->th, hint->inode, | |
1219 | *(--new_blocknrs), 1); | |
1220 | } | |
1da177e4 | 1221 | } |
bd4c625c | 1222 | return ret; |
1da177e4 LT |
1223 | } |
1224 | ||
1225 | /* These 2 functions are here to provide blocks reservation to the rest of kernel */ | |
1226 | /* Reserve @blocks amount of blocks in fs pointed by @sb. Caller must make sure | |
1227 | there are actually this much blocks on the FS available */ | |
bd4c625c LT |
1228 | void reiserfs_claim_blocks_to_be_allocated(struct super_block *sb, /* super block of |
1229 | filesystem where | |
1230 | blocks should be | |
1231 | reserved */ | |
1232 | int blocks /* How much to reserve */ | |
1233 | ) | |
1da177e4 LT |
1234 | { |
1235 | ||
bd4c625c LT |
1236 | /* Fast case, if reservation is zero - exit immediately. */ |
1237 | if (!blocks) | |
1238 | return; | |
1da177e4 | 1239 | |
bd4c625c LT |
1240 | spin_lock(&REISERFS_SB(sb)->bitmap_lock); |
1241 | REISERFS_SB(sb)->reserved_blocks += blocks; | |
1242 | spin_unlock(&REISERFS_SB(sb)->bitmap_lock); | |
1da177e4 LT |
1243 | } |
1244 | ||
1245 | /* Unreserve @blocks amount of blocks in fs pointed by @sb */ | |
bd4c625c LT |
1246 | void reiserfs_release_claimed_blocks(struct super_block *sb, /* super block of |
1247 | filesystem where | |
1248 | blocks should be | |
1249 | reserved */ | |
1250 | int blocks /* How much to unreserve */ | |
1251 | ) | |
1da177e4 LT |
1252 | { |
1253 | ||
bd4c625c LT |
1254 | /* Fast case, if unreservation is zero - exit immediately. */ |
1255 | if (!blocks) | |
1256 | return; | |
1da177e4 | 1257 | |
bd4c625c LT |
1258 | spin_lock(&REISERFS_SB(sb)->bitmap_lock); |
1259 | REISERFS_SB(sb)->reserved_blocks -= blocks; | |
1260 | spin_unlock(&REISERFS_SB(sb)->bitmap_lock); | |
1261 | RFALSE(REISERFS_SB(sb)->reserved_blocks < 0, | |
1262 | "amount of blocks reserved became zero?"); | |
1da177e4 LT |
1263 | } |
1264 | ||
1265 | /* This function estimates how much pages we will be able to write to FS | |
1266 | used for reiserfs_file_write() purposes for now. */ | |
bd4c625c LT |
1267 | int reiserfs_can_fit_pages(struct super_block *sb /* superblock of filesystem |
1268 | to estimate space */ ) | |
1da177e4 LT |
1269 | { |
1270 | int space; | |
1271 | ||
1272 | spin_lock(&REISERFS_SB(sb)->bitmap_lock); | |
bd4c625c LT |
1273 | space = |
1274 | (SB_FREE_BLOCKS(sb) - | |
1275 | REISERFS_SB(sb)->reserved_blocks) >> (PAGE_CACHE_SHIFT - | |
1276 | sb->s_blocksize_bits); | |
1da177e4 LT |
1277 | spin_unlock(&REISERFS_SB(sb)->bitmap_lock); |
1278 | ||
bd4c625c | 1279 | return space > 0 ? space : 0; |
1da177e4 | 1280 | } |
6f01046b JM |
1281 | |
1282 | void reiserfs_cache_bitmap_metadata(struct super_block *sb, | |
1283 | struct buffer_head *bh, | |
1284 | struct reiserfs_bitmap_info *info) | |
1285 | { | |
1286 | unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size); | |
1287 | ||
1288 | info->first_zero_hint = 1 << (sb->s_blocksize_bits + 3); | |
1289 | ||
1290 | while (--cur >= (unsigned long *)bh->b_data) { | |
1291 | int base = ((char *)cur - bh->b_data) << 3; | |
1292 | ||
1293 | /* 0 and ~0 are special, we can optimize for them */ | |
1294 | if (*cur == 0) { | |
1295 | info->first_zero_hint = base; | |
1296 | info->free_count += BITS_PER_LONG; | |
1297 | } else if (*cur != ~0L) { /* A mix, investigate */ | |
1298 | int b; | |
1299 | for (b = BITS_PER_LONG - 1; b >= 0; b--) { | |
1300 | if (!reiserfs_test_le_bit(b, cur)) { | |
1301 | info->first_zero_hint = base + b; | |
1302 | info->free_count++; | |
1303 | } | |
1304 | } | |
1305 | } | |
1306 | } | |
1307 | /* The first bit must ALWAYS be 1 */ | |
1308 | BUG_ON(info->first_zero_hint == 0); | |
1309 | } | |
1310 | ||
1311 | struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb, | |
1312 | unsigned int bitmap) | |
1313 | { | |
1314 | b_blocknr_t block = (sb->s_blocksize << 3) * bitmap; | |
5065227b | 1315 | struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap; |
6f01046b JM |
1316 | struct buffer_head *bh; |
1317 | ||
1318 | /* Way old format filesystems had the bitmaps packed up front. | |
1319 | * I doubt there are any of these left, but just in case... */ | |
1320 | if (unlikely(test_bit(REISERFS_OLD_FORMAT, | |
1321 | &(REISERFS_SB(sb)->s_properties)))) | |
1322 | block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap; | |
1323 | else if (bitmap == 0) | |
1324 | block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1; | |
1325 | ||
5065227b JM |
1326 | bh = sb_bread(sb, block); |
1327 | if (bh == NULL) | |
1328 | reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%lu) " | |
1329 | "reading failed", __FUNCTION__, bh->b_blocknr); | |
1330 | else { | |
1331 | if (buffer_locked(bh)) { | |
1332 | PROC_INFO_INC(sb, scan_bitmap.wait); | |
1333 | __wait_on_buffer(bh); | |
1334 | } | |
1335 | BUG_ON(!buffer_uptodate(bh)); | |
1336 | BUG_ON(atomic_read(&bh->b_count) == 0); | |
1337 | ||
1338 | if (info->first_zero_hint == 0) | |
1339 | reiserfs_cache_bitmap_metadata(sb, bh, info); | |
1340 | } | |
6f01046b JM |
1341 | |
1342 | return bh; | |
1343 | } | |
1344 | ||
1345 | int reiserfs_init_bitmap_cache(struct super_block *sb) | |
1346 | { | |
1347 | struct reiserfs_bitmap_info *bitmap; | |
6f01046b JM |
1348 | |
1349 | bitmap = vmalloc(sizeof (*bitmap) * SB_BMAP_NR(sb)); | |
1350 | if (bitmap == NULL) | |
1351 | return -ENOMEM; | |
1352 | ||
1353 | memset(bitmap, 0, sizeof (*bitmap) * SB_BMAP_NR(sb)); | |
1354 | ||
6f01046b JM |
1355 | SB_AP_BITMAP(sb) = bitmap; |
1356 | ||
1357 | return 0; | |
1358 | } | |
5065227b JM |
1359 | |
1360 | void reiserfs_free_bitmap_cache(struct super_block *sb) | |
1361 | { | |
1362 | if (SB_AP_BITMAP(sb)) { | |
1363 | vfree(SB_AP_BITMAP(sb)); | |
1364 | SB_AP_BITMAP(sb) = NULL; | |
1365 | } | |
1366 | } |