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