blob: 1bcffeab713cc0a27bb13d1ec5c23da03827c4b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4/* Reiserfs block (de)allocator, bitmap-based. */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/time.h>
Al Virof466c6f2012-03-17 01:16:43 -04007#include "reiserfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/buffer_head.h>
10#include <linux/kernel.h>
11#include <linux/pagemap.h>
Jeff Mahoney6f010462006-09-30 23:28:43 -070012#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/quotaops.h>
Jan Karac3aa0772011-12-21 20:17:10 +010014#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#define PREALLOCATION_SIZE 9
17
18/* different reiserfs block allocator options */
19
20#define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)
21
22#define _ALLOC_concentrating_formatted_nodes 0
23#define _ALLOC_displacing_large_files 1
24#define _ALLOC_displacing_new_packing_localities 2
25#define _ALLOC_old_hashed_relocation 3
26#define _ALLOC_new_hashed_relocation 4
27#define _ALLOC_skip_busy 5
28#define _ALLOC_displace_based_on_dirid 6
29#define _ALLOC_hashed_formatted_nodes 7
30#define _ALLOC_old_way 8
31#define _ALLOC_hundredth_slices 9
32#define _ALLOC_dirid_groups 10
33#define _ALLOC_oid_groups 11
34#define _ALLOC_packing_groups 12
35
36#define concentrating_formatted_nodes(s) test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
37#define displacing_large_files(s) test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
38#define displacing_new_packing_localities(s) test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))
39
40#define SET_OPTION(optname) \
41 do { \
Jeff Mahoney1d889d92009-03-30 14:02:20 -040042 reiserfs_info(s, "block allocator option \"%s\" is set", #optname); \
43 set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 } while(0)
45#define TEST_OPTION(optname, s) \
46 test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
47
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048static inline void get_bit_address(struct super_block *s,
Jeff Mahoney3ee16672007-10-18 23:39:25 -070049 b_blocknr_t block,
50 unsigned int *bmap_nr,
51 unsigned int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070053 /* It is in the bitmap block number equal to the block
54 * number divided by the number of bits in a block. */
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070055 *bmap_nr = block >> (s->s_blocksize_bits + 3);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070056 /* Within that bitmap block it is located at bit offset *offset. */
57 *offset = block & ((s->s_blocksize << 3) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070060int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jeff Mahoney3ee16672007-10-18 23:39:25 -070062 unsigned int bmap, offset;
Jeff Mahoneycb680c12007-10-18 23:39:27 -070063 unsigned int bmap_count = reiserfs_bmap_count(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070065 if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040066 reiserfs_error(s, "vs-4010",
67 "block number is out of range %lu (%u)",
68 block, SB_BLOCK_COUNT(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070072 get_bit_address(s, block, &bmap, &offset);
73
74 /* Old format filesystem? Unlikely, but the bitmaps are all up front so
75 * we need to account for it. */
76 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
77 &(REISERFS_SB(s)->s_properties)))) {
78 b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
Jeff Mahoneycb680c12007-10-18 23:39:27 -070079 if (block >= bmap1 &&
80 block <= bmap1 + bmap_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040081 reiserfs_error(s, "vs-4019", "bitmap block %lu(%u) "
82 "can't be freed or reused",
83 block, bmap_count);
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070084 return 0;
85 }
86 } else {
87 if (offset == 0) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040088 reiserfs_error(s, "vs-4020", "bitmap block %lu(%u) "
89 "can't be freed or reused",
90 block, bmap_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070091 return 0;
92 }
Jeff Mahoneye1fabd32006-09-30 23:28:40 -070093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Jeff Mahoneycb680c12007-10-18 23:39:27 -070095 if (bmap >= bmap_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -040096 reiserfs_error(s, "vs-4030", "bitmap for requested block "
97 "is out of range: block=%lu, bitmap_nr=%u",
98 block, bmap);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070099 return 0;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700102 if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400103 reiserfs_error(s, "vs-4050", "this is root block (%u), "
104 "it must be busy", SB_ROOT_BLOCK(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105 return 0;
106 }
107
108 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/* searches in journal structures for a given block number (bmap, off). If block
112 is found in reiserfs journal it suggests next free block candidate to test. */
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700113static inline int is_block_in_journal(struct super_block *s, unsigned int bmap,
114 int off, int *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700116 b_blocknr_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700118 if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
119 if (tmp) { /* hint supplied */
120 *next = tmp;
121 PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
122 } else {
123 (*next) = off + 1; /* inc offset to avoid looping. */
124 PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
125 }
126 PROC_INFO_INC(s, scan_bitmap.retry);
127 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
133 * block; */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700135 unsigned int bmap_n, int *beg, int boundary,
136 int min, int max, int unfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700138 struct super_block *s = th->t_super;
139 struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700140 struct buffer_head *bh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700141 int end, next;
142 int org = *beg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 BUG_ON(!th->t_trans_id);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700145 RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of "
146 "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 PROC_INFO_INC(s, scan_bitmap.bmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* this is unclear and lacks comments, explain how journal bitmaps
149 work here for the reader. Convey a sense of the design here. What
150 is a window? */
151/* - I mean `a window of zero bits' as in description of this function - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153 if (!bi) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400154 reiserfs_error(s, "jdm-4055", "NULL bitmap info pointer "
155 "for bitmap %d", bmap_n);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 return 0;
157 }
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700158
Jeff Mahoney50652272006-09-30 23:28:44 -0700159 bh = reiserfs_read_bitmap_block(s, bmap_n);
160 if (bh == NULL)
161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700163 while (1) {
164 cont:
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700165 if (bi->free_count < min) {
166 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167 return 0; // No free blocks in this bitmap
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800170 /* search for a first zero bit -- beginning of a window */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700171 *beg = reiserfs_find_next_zero_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700172 ((unsigned long *)(bh->b_data), boundary, *beg);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700173
174 if (*beg + min > boundary) { /* search for a zero bit fails or the rest of bitmap block
175 * cannot contain a zero window of minimum size */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700176 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700180 if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
181 continue;
182 /* first zero bit found; we check next bits */
183 for (end = *beg + 1;; end++) {
184 if (end >= *beg + max || end >= boundary
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700185 || reiserfs_test_le_bit(end, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186 next = end;
187 break;
188 }
189 /* finding the other end of zero bit window requires looking into journal structures (in
190 * case of searching for free blocks for unformatted nodes) */
191 if (unfm && is_block_in_journal(s, bmap_n, end, &next))
192 break;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700195 /* now (*beg) points to beginning of zero bits window,
196 * (end) points to one bit after the window end */
197 if (end - *beg >= min) { /* it seems we have found window of proper size */
198 int i;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700199 reiserfs_prepare_for_journal(s, bh, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700200 /* try to set all blocks used checking are they still free */
201 for (i = *beg; i < end; i++) {
202 /* It seems that we should not check in journal again. */
203 if (reiserfs_test_and_set_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700204 (i, bh->b_data)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 /* bit was set by another process
206 * while we slept in prepare_for_journal() */
207 PROC_INFO_INC(s, scan_bitmap.stolen);
208 if (i >= *beg + min) { /* we can continue with smaller set of allocated blocks,
209 * if length of this set is more or equal to `min' */
210 end = i;
211 break;
212 }
213 /* otherwise we clear all bit were set ... */
214 while (--i >= *beg)
Akinobu Mita0c2fd1bfb2011-07-25 17:13:37 -0700215 reiserfs_clear_le_bit
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700216 (i, bh->b_data);
217 reiserfs_restore_prepared_buffer(s, bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 *beg = org;
219 /* ... and search again in current block from beginning */
220 goto cont;
221 }
222 }
223 bi->free_count -= (end - *beg);
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700224 journal_mark_dirty(th, s, bh);
225 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226
227 /* free block count calculation */
228 reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
229 1);
230 PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
231 journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
232
233 return end - (*beg);
234 } else {
235 *beg = next;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240static int bmap_hash_id(struct super_block *s, u32 id)
241{
242 char *hash_in = NULL;
243 unsigned long hash;
244 unsigned bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 if (id <= 2) {
247 bm = 1;
248 } else {
249 hash_in = (char *)(&id);
250 hash = keyed_hash(hash_in, 4);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700251 bm = hash % reiserfs_bmap_count(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700252 if (!bm)
253 bm = 1;
254 }
255 /* this can only be true when SB_BMAP_NR = 1 */
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700256 if (bm >= reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700257 bm = 0;
258 return bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * hashes the id and then returns > 0 if the block group for the
263 * corresponding hash is full
264 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700265static inline int block_group_used(struct super_block *s, u32 id)
266{
Jeff Mahoney50652272006-09-30 23:28:44 -0700267 int bm = bmap_hash_id(s, id);
268 struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
269
270 /* If we don't have cached information on this bitmap block, we're
271 * going to have to load it later anyway. Loading it here allows us
Joe Perchesc78bad12008-02-03 17:33:42 +0200272 * to make a better decision. This favors long-term performance gain
Jeff Mahoney50652272006-09-30 23:28:44 -0700273 * with a better on-disk layout vs. a short term gain of skipping the
274 * read and potentially having a bad placement. */
Jeff Mahoney4d208512007-10-18 23:39:26 -0700275 if (info->free_count == UINT_MAX) {
Jeff Mahoney50652272006-09-30 23:28:44 -0700276 struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
277 brelse(bh);
278 }
279
280 if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700281 return 0;
282 }
283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/*
287 * the packing is returned in disk byte order
288 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700289__le32 reiserfs_choose_packing(struct inode * dir)
Al Viro3e8962b2005-05-01 08:59:18 -0700290{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700291 __le32 packing;
292 if (TEST_OPTION(packing_groups, dir->i_sb)) {
293 u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
294 /*
295 * some versions of reiserfsck expect packing locality 1 to be
296 * special
297 */
298 if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
299 packing = INODE_PKEY(dir)->k_objectid;
300 else
301 packing = INODE_PKEY(dir)->k_dir_id;
302 } else
303 packing = INODE_PKEY(dir)->k_objectid;
304 return packing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/* Tries to find contiguous zero bit window (given size) in given region of
308 * bitmap and place new blocks there. Returns number of allocated blocks. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700309static int scan_bitmap(struct reiserfs_transaction_handle *th,
310 b_blocknr_t * start, b_blocknr_t finish,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700311 int min, int max, int unfm, sector_t file_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 int nr_allocated = 0;
314 struct super_block *s = th->t_super;
315 /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
316 * - Hans, it is not a block number - Zam. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700318 unsigned int bm, off;
319 unsigned int end_bm, end_off;
320 unsigned int off_max = s->s_blocksize << 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700322 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323 PROC_INFO_INC(s, scan_bitmap.call);
324 if (SB_FREE_BLOCKS(s) <= 0)
325 return 0; // No point in looking for more free blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700327 get_bit_address(s, *start, &bm, &off);
328 get_bit_address(s, finish, &end_bm, &end_off);
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700329 if (bm > reiserfs_bmap_count(s))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700330 return 0;
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700331 if (end_bm > reiserfs_bmap_count(s))
332 end_bm = reiserfs_bmap_count(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700334 /* When the bitmap is more than 10% free, anyone can allocate.
335 * When it's less than 10% free, only files that already use the
336 * bitmap are allowed. Once we pass 80% full, this restriction
337 * is lifted.
338 *
339 * We do this so that files that grow later still have space close to
340 * their original allocation. This improves locality, and presumably
341 * performance as a result.
342 *
343 * This is only an allocation policy and does not make up for getting a
344 * bad hint. Decent hinting must be implemented for this to work well.
345 */
346 if (TEST_OPTION(skip_busy, s)
347 && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
348 for (; bm < end_bm; bm++, off = 0) {
349 if ((off && (!unfm || (file_block != 0)))
350 || SB_AP_BITMAP(s)[bm].free_count >
351 (s->s_blocksize << 3) / 10)
352 nr_allocated =
353 scan_bitmap_block(th, bm, &off, off_max,
354 min, max, unfm);
355 if (nr_allocated)
356 goto ret;
357 }
358 /* we know from above that start is a reasonable number */
359 get_bit_address(s, *start, &bm, &off);
360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 for (; bm < end_bm; bm++, off = 0) {
363 nr_allocated =
364 scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
365 if (nr_allocated)
366 goto ret;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 nr_allocated =
370 scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
371
372 ret:
373 *start = bm * off_max + off;
374 return nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376}
377
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700378static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
379 struct inode *inode, b_blocknr_t block,
380 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700382 struct super_block *s = th->t_super;
383 struct reiserfs_super_block *rs;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700384 struct buffer_head *sbh, *bmbh;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 struct reiserfs_bitmap_info *apbi;
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700386 unsigned int nr, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700389 PROC_INFO_INC(s, free_block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700390 rs = SB_DISK_SUPER_BLOCK(s);
391 sbh = SB_BUFFER_WITH_SB(s);
392 apbi = SB_AP_BITMAP(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394 get_bit_address(s, block, &nr, &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jeff Mahoneycb680c12007-10-18 23:39:27 -0700396 if (nr >= reiserfs_bmap_count(s)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400397 reiserfs_error(s, "vs-4075", "block %lu is out of range",
398 block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399 return;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Jeff Mahoney50652272006-09-30 23:28:44 -0700402 bmbh = reiserfs_read_bitmap_block(s, nr);
403 if (!bmbh)
404 return;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700405
406 reiserfs_prepare_for_journal(s, bmbh, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700408 /* clear bit for the given block in bit map */
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700409 if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400410 reiserfs_error(s, "vs-4080",
411 "block %lu: bit already cleared", block);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 }
413 apbi[nr].free_count++;
Jeff Mahoney0b3dc172006-09-30 23:28:42 -0700414 journal_mark_dirty(th, s, bmbh);
415 brelse(bmbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700417 reiserfs_prepare_for_journal(s, sbh, 1);
418 /* update super block */
419 set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700421 journal_mark_dirty(th, s, sbh);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400422 if (for_unformatted) {
423 int depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500424 dquot_free_block_nodirty(inode, 1);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -0400425 reiserfs_write_lock_nested(s, depth);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700429void reiserfs_free_block(struct reiserfs_transaction_handle *th,
430 struct inode *inode, b_blocknr_t block,
431 int for_unformatted)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700433 struct super_block *s = th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700435 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700436 RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700437 if (!is_reusable(s, block, 1))
438 return;
439
440 if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400441 reiserfs_error(th->t_super, "bitmap-4072",
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700442 "Trying to free block outside file system "
443 "boundaries (%lu > %lu)",
444 block, sb_block_count(REISERFS_SB(s)->s_rs));
445 return;
446 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 /* mark it before we clear it, just in case */
448 journal_mark_freed(th, s, block);
449 _reiserfs_free_block(th, inode, block, for_unformatted);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/* preallocated blocks don't need to be run through journal_mark_freed */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
454 struct inode *inode, b_blocknr_t block)
455{
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700456 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700457 RFALSE(!th->t_super,
458 "vs-4060: trying to free block on nonexistent device");
Jeff Mahoneyd4c3d192007-10-18 23:39:24 -0700459 if (!is_reusable(th->t_super, block, 1))
460 return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 _reiserfs_free_block(th, inode, block, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700464static void __discard_prealloc(struct reiserfs_transaction_handle *th,
465 struct reiserfs_inode_info *ei)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700467 unsigned long save = ei->i_prealloc_block;
468 int dirty = 0;
469 struct inode *inode = &ei->vfs_inode;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700470
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 if (ei->i_prealloc_count < 0)
Jeff Mahoney0030b642009-03-30 14:02:28 -0400474 reiserfs_error(th->t_super, "zam-4001",
475 "inode has negative prealloc blocks count.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 while (ei->i_prealloc_count > 0) {
478 reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
479 ei->i_prealloc_block++;
480 ei->i_prealloc_count--;
481 dirty = 1;
482 }
483 if (dirty)
484 reiserfs_update_sd(th, inode);
485 ei->i_prealloc_block = save;
486 list_del_init(&(ei->i_prealloc_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/* FIXME: It should be inline function */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
491 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700493 struct reiserfs_inode_info *ei = REISERFS_I(inode);
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700494
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700495 BUG_ON(!th->t_trans_id);
496 if (ei->i_prealloc_count)
497 __discard_prealloc(th, ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700500void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700502 struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700504 BUG_ON(!th->t_trans_id);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 while (!list_empty(plist)) {
506 struct reiserfs_inode_info *ei;
507 ei = list_entry(plist->next, struct reiserfs_inode_info,
508 i_prealloc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700510 if (!ei->i_prealloc_count) {
Jeff Mahoney0030b642009-03-30 14:02:28 -0400511 reiserfs_error(th->t_super, "zam-4001",
512 "inode is in prealloc list but has "
513 "no preallocated blocks.");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700516 __discard_prealloc(th, ei);
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700520void reiserfs_init_alloc_options(struct super_block *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700522 set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
523 set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
524 set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/* block allocator related options are parsed here */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700528int reiserfs_parse_alloc_options(struct super_block *s, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700530 char *this_char, *value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700534 while ((this_char = strsep(&options, ":")) != NULL) {
535 if ((value = strchr(this_char, '=')) != NULL)
536 *value++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538 if (!strcmp(this_char, "concentrating_formatted_nodes")) {
539 int temp;
540 SET_OPTION(concentrating_formatted_nodes);
541 temp = (value
542 && *value) ? simple_strtoul(value, &value,
543 0) : 10;
544 if (temp <= 0 || temp > 100) {
545 REISERFS_SB(s)->s_alloc_options.border = 10;
546 } else {
547 REISERFS_SB(s)->s_alloc_options.border =
548 100 / temp;
549 }
550 continue;
551 }
552 if (!strcmp(this_char, "displacing_large_files")) {
553 SET_OPTION(displacing_large_files);
554 REISERFS_SB(s)->s_alloc_options.large_file_size =
555 (value
556 && *value) ? simple_strtoul(value, &value, 0) : 16;
557 continue;
558 }
559 if (!strcmp(this_char, "displacing_new_packing_localities")) {
560 SET_OPTION(displacing_new_packing_localities);
561 continue;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700564 if (!strcmp(this_char, "old_hashed_relocation")) {
565 SET_OPTION(old_hashed_relocation);
566 continue;
567 }
568
569 if (!strcmp(this_char, "new_hashed_relocation")) {
570 SET_OPTION(new_hashed_relocation);
571 continue;
572 }
573
574 if (!strcmp(this_char, "dirid_groups")) {
575 SET_OPTION(dirid_groups);
576 continue;
577 }
578 if (!strcmp(this_char, "oid_groups")) {
579 SET_OPTION(oid_groups);
580 continue;
581 }
582 if (!strcmp(this_char, "packing_groups")) {
583 SET_OPTION(packing_groups);
584 continue;
585 }
586 if (!strcmp(this_char, "hashed_formatted_nodes")) {
587 SET_OPTION(hashed_formatted_nodes);
588 continue;
589 }
590
591 if (!strcmp(this_char, "skip_busy")) {
592 SET_OPTION(skip_busy);
593 continue;
594 }
595
596 if (!strcmp(this_char, "hundredth_slices")) {
597 SET_OPTION(hundredth_slices);
598 continue;
599 }
600
601 if (!strcmp(this_char, "old_way")) {
602 SET_OPTION(old_way);
603 continue;
604 }
605
606 if (!strcmp(this_char, "displace_based_on_dirid")) {
607 SET_OPTION(displace_based_on_dirid);
608 continue;
609 }
610
611 if (!strcmp(this_char, "preallocmin")) {
612 REISERFS_SB(s)->s_alloc_options.preallocmin =
613 (value
614 && *value) ? simple_strtoul(value, &value, 0) : 4;
615 continue;
616 }
617
618 if (!strcmp(this_char, "preallocsize")) {
619 REISERFS_SB(s)->s_alloc_options.preallocsize =
620 (value
621 && *value) ? simple_strtoul(value, &value,
622 0) :
623 PREALLOCATION_SIZE;
624 continue;
625 }
626
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400627 reiserfs_warning(s, "zam-4001", "unknown option - %s",
628 this_char);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Jeff Mahoney1d889d92009-03-30 14:02:20 -0400632 reiserfs_info(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700633 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jan Karac3aa0772011-12-21 20:17:10 +0100636static void print_sep(struct seq_file *seq, int *first)
637{
638 if (!*first)
639 seq_puts(seq, ":");
640 else
641 *first = 0;
642}
643
644void show_alloc_options(struct seq_file *seq, struct super_block *s)
645{
646 int first = 1;
647
648 if (SB_ALLOC_OPTS(s) == ((1 << _ALLOC_skip_busy) |
649 (1 << _ALLOC_dirid_groups) | (1 << _ALLOC_packing_groups)))
650 return;
651
652 seq_puts(seq, ",alloc=");
653
654 if (TEST_OPTION(concentrating_formatted_nodes, s)) {
655 print_sep(seq, &first);
656 if (REISERFS_SB(s)->s_alloc_options.border != 10) {
657 seq_printf(seq, "concentrating_formatted_nodes=%d",
658 100 / REISERFS_SB(s)->s_alloc_options.border);
659 } else
660 seq_puts(seq, "concentrating_formatted_nodes");
661 }
662 if (TEST_OPTION(displacing_large_files, s)) {
663 print_sep(seq, &first);
664 if (REISERFS_SB(s)->s_alloc_options.large_file_size != 16) {
665 seq_printf(seq, "displacing_large_files=%lu",
666 REISERFS_SB(s)->s_alloc_options.large_file_size);
667 } else
668 seq_puts(seq, "displacing_large_files");
669 }
670 if (TEST_OPTION(displacing_new_packing_localities, s)) {
671 print_sep(seq, &first);
672 seq_puts(seq, "displacing_new_packing_localities");
673 }
674 if (TEST_OPTION(old_hashed_relocation, s)) {
675 print_sep(seq, &first);
676 seq_puts(seq, "old_hashed_relocation");
677 }
678 if (TEST_OPTION(new_hashed_relocation, s)) {
679 print_sep(seq, &first);
680 seq_puts(seq, "new_hashed_relocation");
681 }
682 if (TEST_OPTION(dirid_groups, s)) {
683 print_sep(seq, &first);
684 seq_puts(seq, "dirid_groups");
685 }
686 if (TEST_OPTION(oid_groups, s)) {
687 print_sep(seq, &first);
688 seq_puts(seq, "oid_groups");
689 }
690 if (TEST_OPTION(packing_groups, s)) {
691 print_sep(seq, &first);
692 seq_puts(seq, "packing_groups");
693 }
694 if (TEST_OPTION(hashed_formatted_nodes, s)) {
695 print_sep(seq, &first);
696 seq_puts(seq, "hashed_formatted_nodes");
697 }
698 if (TEST_OPTION(skip_busy, s)) {
699 print_sep(seq, &first);
700 seq_puts(seq, "skip_busy");
701 }
702 if (TEST_OPTION(hundredth_slices, s)) {
703 print_sep(seq, &first);
704 seq_puts(seq, "hundredth_slices");
705 }
706 if (TEST_OPTION(old_way, s)) {
707 print_sep(seq, &first);
708 seq_puts(seq, "old_way");
709 }
710 if (TEST_OPTION(displace_based_on_dirid, s)) {
711 print_sep(seq, &first);
712 seq_puts(seq, "displace_based_on_dirid");
713 }
714 if (REISERFS_SB(s)->s_alloc_options.preallocmin != 0) {
715 print_sep(seq, &first);
716 seq_printf(seq, "preallocmin=%d",
717 REISERFS_SB(s)->s_alloc_options.preallocmin);
718 }
719 if (REISERFS_SB(s)->s_alloc_options.preallocsize != 17) {
720 print_sep(seq, &first);
721 seq_printf(seq, "preallocsize=%d",
722 REISERFS_SB(s)->s_alloc_options.preallocsize);
723 }
724}
725
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700726static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
727{
728 char *hash_in;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700729
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700730 if (hint->formatted_node) {
731 hash_in = (char *)&hint->key.k_dir_id;
732 } else {
733 if (!hint->inode) {
734 //hint->search_start = hint->beg;
735 hash_in = (char *)&hint->key.k_dir_id;
736 } else
737 if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
738 hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
739 else
740 hash_in =
741 (char *)(&INODE_PKEY(hint->inode)->k_objectid);
742 }
743
744 hint->search_start =
745 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748/*
749 * Relocation based on dirid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200750 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700752static void dirid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754 unsigned long hash;
755 __u32 dirid = 0;
756 int bm = 0;
757 struct super_block *sb = hint->th->t_super;
Fabian Frederickae0a50a2014-06-06 14:38:11 -0700758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (hint->inode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700760 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 Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Relocation based on oid, hashing them into a given bitmap block
Joe Perchesc78bad12008-02-03 17:33:42 +0200776 * files. Formatted nodes are unaffected, a separate policy covers them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700778static void oid_groups(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700780 if (hint->inode) {
781 unsigned long hash;
782 __u32 oid;
783 __u32 dirid;
784 int bm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700786 dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/* returns 1 if it finds an indirect item and gets valid hint info
803 * from it, otherwise 0
804 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700805static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800807 struct treepath *path;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700808 struct buffer_head *bh;
809 struct item_head *ih;
810 int pos_in_item;
811 __le32 *item;
812 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 if (!hint->path) /* reiserfs code can call this function w/o pointer to path
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 * structure supplied; then we rely on supplied search_start */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700818 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 Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825 hint->search_start = bh->b_blocknr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 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 Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 /* does result value fit into specified region? */
845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
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 Torvaldsbd4c6252005-07-12 20:21:28 -0700852static inline void set_border_in_hint(struct super_block *s,
853 reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700855 b_blocknr_t border =
856 SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700858 if (hint->formatted_node)
859 hint->end = border - 1;
860 else
861 hint->beg = border;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700864static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700866 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 Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700878static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700880 char *hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700882 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 Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 hint->search_start =
890 hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700893static inline int
894this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
895 hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897 return hint->block ==
898 REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700902static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700904 struct in_core_key *key = &hint->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700906 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 Torvalds1da177e2005-04-16 15:20:36 -0700910}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700913static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
914{
915 b_blocknr_t border;
916 u32 hash_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700918 if (hint->formatted_node || hint->inode == NULL) {
919 return 0;
920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700922 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 Torvalds1da177e2005-04-16 15:20:36 -0700928
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700929 return 1;
930}
931
932static 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
950static 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
963static 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 Torvalds1da177e2005-04-16 15:20:36 -07001038 old_hashed_relocation(hint);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001039 }
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 Torvalds1da177e2005-04-16 15:20:36 -07001054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001055 /* oid grouping works only on unformatted nodes */
1056 if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
1057 oid_groups(hint);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
1063{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001064 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001068 hint->prealloc_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001070 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 Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/* XXX I know it could be merged with upper-level function;
1083 but may be result function would be too complex. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001084static 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 Torvalds1da177e2005-04-16 15:20:36 -07001090{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001091 int rest = amount_needed;
1092 int nr_allocated;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001094 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 Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001122 return (amount_needed - rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125static inline int blocknrs_and_prealloc_arrays_from_search_start
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001126 (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;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001133 int depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001135 determine_prealloc_size(hint);
1136 if (!hint->formatted_node) {
1137 int quota_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001139 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1140 "reiserquota: allocating %d blocks id=%u",
1141 amount_needed, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001143 depth = reiserfs_write_unlock_nested(s);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001144 quota_ret =
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001145 dquot_alloc_block_nodirty(hint->inode, amount_needed);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001146 if (quota_ret) { /* Quota exceeded? */
1147 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001148 return QUOTA_EXCEEDED;
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001149 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001150 if (hint->preallocate && hint->prealloc_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001152 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1153 "reiserquota: allocating (prealloc) %d blocks id=%u",
1154 hint->prealloc_size, hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155#endif
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001156 quota_ret = dquot_prealloc_block_nodirty(hint->inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001157 hint->prealloc_size);
1158 if (quota_ret)
1159 hint->preallocate = hint->prealloc_size = 0;
1160 }
1161 /* for unformatted nodes, force large allocations */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001162 reiserfs_write_lock_nested(s, depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001165 do {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166 switch (passno++) {
1167 case 0: /* Search from hint->search_start to end of disk */
1168 start = hint->search_start;
1169 finish = SB_BLOCK_COUNT(s) - 1;
1170 break;
1171 case 1: /* Search from hint->beg to hint->search_start */
1172 start = hint->beg;
1173 finish = hint->search_start;
1174 break;
1175 case 2: /* Last chance: Search from 0 to hint->beg */
1176 start = 0;
1177 finish = hint->beg;
1178 break;
1179 default: /* We've tried searching everywhere, not enough space */
1180 /* Free the blocks */
1181 if (!hint->formatted_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001183 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1184 "reiserquota: freeing (nospace) %d blocks id=%u",
1185 amount_needed +
1186 hint->prealloc_size -
1187 nr_allocated,
1188 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189#endif
Jan Kara77db4f22009-01-26 17:14:18 +01001190 /* Free not allocated blocks */
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001191 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001192 dquot_free_block_nodirty(hint->inode,
Jan Kara77db4f22009-01-26 17:14:18 +01001193 amount_needed + hint->prealloc_size -
1194 nr_allocated);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001195 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 }
1197 while (nr_allocated--)
1198 reiserfs_free_block(hint->th, hint->inode,
1199 new_blocknrs[nr_allocated],
1200 !hint->formatted_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202 return NO_DISK_SPACE;
1203 }
1204 } while ((nr_allocated += allocate_without_wrapping_disk(hint,
1205 new_blocknrs +
1206 nr_allocated,
1207 start, finish,
Jeff Mahoney9ea0f942006-09-30 23:28:45 -07001208 1,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001209 amount_needed -
1210 nr_allocated,
1211 hint->
1212 prealloc_size))
1213 < amount_needed);
1214 if (!hint->formatted_node &&
1215 amount_needed + hint->prealloc_size >
1216 nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
1217 /* Some of preallocation blocks were not allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001219 reiserfs_debug(s, REISERFS_DEBUG_CODE,
1220 "reiserquota: freeing (failed prealloc) %d blocks id=%u",
1221 amount_needed + hint->prealloc_size -
1222 nr_allocated -
1223 REISERFS_I(hint->inode)->i_prealloc_count,
1224 hint->inode->i_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#endif
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001226
1227 depth = reiserfs_write_unlock_nested(s);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001228 dquot_free_block_nodirty(hint->inode, amount_needed +
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001229 hint->prealloc_size - nr_allocated -
1230 REISERFS_I(hint->inode)->
1231 i_prealloc_count);
Jeff Mahoneyd2d03952013-08-08 17:34:47 -04001232 reiserfs_write_lock_nested(s, depth);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001235 return CARRY_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238/* grab new blocknrs from preallocated list */
1239/* return amount still needed after using them */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001240static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
1241 b_blocknr_t * new_blocknrs,
1242 int amount_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 struct inode *inode = hint->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001246 if (REISERFS_I(inode)->i_prealloc_count > 0) {
1247 while (amount_needed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001249 *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
1250 REISERFS_I(inode)->i_prealloc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001252 amount_needed--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001254 if (REISERFS_I(inode)->i_prealloc_count <= 0) {
1255 list_del(&REISERFS_I(inode)->i_prealloc_list);
1256 break;
1257 }
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001260 /* return amount still needed after using preallocated blocks */
1261 return amount_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001264int 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
1265 already reserved */ )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001267 int initial_amount_needed = amount_needed;
1268 int ret;
1269 struct super_block *s = hint->th->t_super;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001271 /* Check if there is enough space, taking into account reserved space */
1272 if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
1273 amount_needed - reserved_by_us)
1274 return NO_DISK_SPACE;
1275 /* should this be if !hint->inode && hint->preallocate? */
1276 /* do you mean hint->formatted_node can be removed ? - Zam */
1277 /* hint->formatted_node cannot be removed because we try to access
1278 inode information here, and there is often no inode assotiated with
1279 metadata allocations - green */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001281 if (!hint->formatted_node && hint->preallocate) {
1282 amount_needed = use_preallocated_list_if_available
1283 (hint, new_blocknrs, amount_needed);
1284 if (amount_needed == 0) /* all blocknrs we need we got from
1285 prealloc. list */
1286 return CARRY_ON;
1287 new_blocknrs += (initial_amount_needed - amount_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001289
1290 /* find search start and save it in hint structure */
1291 determine_search_start(hint, amount_needed);
1292 if (hint->search_start >= SB_BLOCK_COUNT(s))
1293 hint->search_start = SB_BLOCK_COUNT(s) - 1;
1294
1295 /* allocation itself; fill new_blocknrs and preallocation arrays */
1296 ret = blocknrs_and_prealloc_arrays_from_search_start
1297 (hint, new_blocknrs, amount_needed);
1298
1299 /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
1300 * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
1301 * variant) */
1302
1303 if (ret != CARRY_ON) {
1304 while (amount_needed++ < initial_amount_needed) {
1305 reiserfs_free_block(hint->th, hint->inode,
1306 *(--new_blocknrs), 1);
1307 }
1308 }
1309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
Jeff Mahoney6f010462006-09-30 23:28:43 -07001312void reiserfs_cache_bitmap_metadata(struct super_block *sb,
1313 struct buffer_head *bh,
1314 struct reiserfs_bitmap_info *info)
1315{
1316 unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
1317
Jeff Mahoney4d208512007-10-18 23:39:26 -07001318 /* The first bit must ALWAYS be 1 */
Jeff Mahoney0030b642009-03-30 14:02:28 -04001319 if (!reiserfs_test_le_bit(0, (unsigned long *)bh->b_data))
1320 reiserfs_error(sb, "reiserfs-2025", "bitmap block %lu is "
1321 "corrupted: first bit must be 1", bh->b_blocknr);
Jeff Mahoney4d208512007-10-18 23:39:26 -07001322
1323 info->free_count = 0;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001324
1325 while (--cur >= (unsigned long *)bh->b_data) {
Jeff Mahoney6f010462006-09-30 23:28:43 -07001326 /* 0 and ~0 are special, we can optimize for them */
Jeff Mahoney4d208512007-10-18 23:39:26 -07001327 if (*cur == 0)
Jeff Mahoney6f010462006-09-30 23:28:43 -07001328 info->free_count += BITS_PER_LONG;
Jeff Mahoney4d208512007-10-18 23:39:26 -07001329 else if (*cur != ~0L) /* A mix, investigate */
Akinobu Mita9d6bf5a2011-07-25 17:13:38 -07001330 info->free_count += BITS_PER_LONG - hweight_long(*cur);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001331 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001332}
1333
1334struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
1335 unsigned int bitmap)
1336{
1337 b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
Jeff Mahoney50652272006-09-30 23:28:44 -07001338 struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
Jeff Mahoney6f010462006-09-30 23:28:43 -07001339 struct buffer_head *bh;
1340
1341 /* Way old format filesystems had the bitmaps packed up front.
1342 * I doubt there are any of these left, but just in case... */
1343 if (unlikely(test_bit(REISERFS_OLD_FORMAT,
1344 &(REISERFS_SB(sb)->s_properties))))
1345 block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
1346 else if (bitmap == 0)
1347 block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
1348
Jeff Mahoney50652272006-09-30 23:28:44 -07001349 bh = sb_bread(sb, block);
1350 if (bh == NULL)
Eric Eric Sesterhenn00079e02006-10-06 22:19:45 -07001351 reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
Harvey Harrisonfbe54982008-04-28 02:16:22 -07001352 "reading failed", __func__, block);
Jeff Mahoney50652272006-09-30 23:28:44 -07001353 else {
1354 if (buffer_locked(bh)) {
Jeff Mahoney278f6672013-08-08 17:34:46 -04001355 int depth;
Jeff Mahoney50652272006-09-30 23:28:44 -07001356 PROC_INFO_INC(sb, scan_bitmap.wait);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001357 depth = reiserfs_write_unlock_nested(sb);
Jeff Mahoney50652272006-09-30 23:28:44 -07001358 __wait_on_buffer(bh);
Jeff Mahoney278f6672013-08-08 17:34:46 -04001359 reiserfs_write_lock_nested(sb, depth);
Jeff Mahoney50652272006-09-30 23:28:44 -07001360 }
1361 BUG_ON(!buffer_uptodate(bh));
1362 BUG_ON(atomic_read(&bh->b_count) == 0);
1363
Jeff Mahoney4d208512007-10-18 23:39:26 -07001364 if (info->free_count == UINT_MAX)
Jeff Mahoney50652272006-09-30 23:28:44 -07001365 reiserfs_cache_bitmap_metadata(sb, bh, info);
1366 }
Jeff Mahoney6f010462006-09-30 23:28:43 -07001367
1368 return bh;
1369}
1370
1371int reiserfs_init_bitmap_cache(struct super_block *sb)
1372{
1373 struct reiserfs_bitmap_info *bitmap;
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001374 unsigned int bmap_nr = reiserfs_bmap_count(sb);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001375
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001376 bitmap = vmalloc(sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001377 if (bitmap == NULL)
1378 return -ENOMEM;
1379
Jeff Mahoneycb680c12007-10-18 23:39:27 -07001380 memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr);
Jeff Mahoney6f010462006-09-30 23:28:43 -07001381
Jeff Mahoney6f010462006-09-30 23:28:43 -07001382 SB_AP_BITMAP(sb) = bitmap;
1383
1384 return 0;
1385}
Jeff Mahoney50652272006-09-30 23:28:44 -07001386
1387void reiserfs_free_bitmap_cache(struct super_block *sb)
1388{
1389 if (SB_AP_BITMAP(sb)) {
1390 vfree(SB_AP_BITMAP(sb));
1391 SB_AP_BITMAP(sb) = NULL;
1392 }
1393}