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