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