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