File: /usr/src/linux/fs/reiserfs/bitmap.c

1     /*
2      * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3      */
4     
5     #include <linux/config.h>
6     #include <linux/sched.h>
7     #include <linux/reiserfs_fs.h>
8     #include <linux/locks.h>
9     #include <asm/bitops.h>
10     #include <linux/list.h>
11     
12     #ifdef CONFIG_REISERFS_CHECK
13     
14     /* this is a safety check to make sure
15     ** blocks are reused properly.  used for debugging only.
16     **
17     ** this checks, that block can be reused, and it has correct state
18     **   (free or busy) 
19     */
20     int is_reusable (struct super_block * s, unsigned long block, int bit_value)
21     {
22         int i, j;
23       
24         if (block == 0 || block >= SB_BLOCK_COUNT (s)) {
25     	reiserfs_warning ("vs-4010: is_reusable: block number is out of range %lu (%u)\n",
26     			  block, SB_BLOCK_COUNT (s));
27     	return 0;
28         }
29     
30         /* it can't be one of the bitmap blocks */
31         for (i = 0; i < SB_BMAP_NR (s); i ++)
32     	if (block == SB_AP_BITMAP (s)[i]->b_blocknr) {
33     	    reiserfs_warning ("vs: 4020: is_reusable: "
34     			      "bitmap block %lu(%u) can't be freed or reused\n",
35     			      block, SB_BMAP_NR (s));
36     	    return 0;
37     	}
38       
39         i = block / (s->s_blocksize << 3);
40         if (i >= SB_BMAP_NR (s)) {
41     	reiserfs_warning ("vs-4030: is_reusable: there is no so many bitmap blocks: "
42     			  "block=%lu, bitmap_nr=%d\n", block, i);
43     	return 0;
44         }
45     
46         j = block % (s->s_blocksize << 3);
47         if ((bit_value == 0 && 
48              reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i]->b_data)) ||
49     	(bit_value == 1 && 
50     	 reiserfs_test_le_bit(j, SB_AP_BITMAP (s)[i]->b_data) == 0)) {
51     	reiserfs_warning ("vs-4040: is_reusable: corresponding bit of block %lu does not "
52     			  "match required value (i==%d, j==%d) test_bit==%d\n",
53     		block, i, j, reiserfs_test_le_bit (j, SB_AP_BITMAP (s)[i]->b_data));
54     	return 0;
55         }
56     
57         if (bit_value == 0 && block == SB_ROOT_BLOCK (s)) {
58     	reiserfs_warning ("vs-4050: is_reusable: this is root block (%u), "
59     			  "it must be busy", SB_ROOT_BLOCK (s));
60     	return 0;
61         }
62     
63         return 1;
64     }
65     
66     
67     
68     
69     #endif /* CONFIG_REISERFS_CHECK */
70     
71     #if 0
72     /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
73     int is_used (struct super_block * s, unsigned long block)
74     {
75         int i, j;
76     
77         i = block / (s->s_blocksize << 3);
78         j = block % (s->s_blocksize << 3);
79         if (reiserfs_test_le_bit(j, SB_AP_BITMAP (s)[i]->b_data))
80     	return 1;
81         return 0;
82       
83     }
84     /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
85     #endif
86     
87     
88     /* get address of corresponding bit (bitmap block number and offset in it) */
89     static inline void get_bit_address (struct super_block * s, unsigned long block, int * bmap_nr, int * offset)
90     {
91                                     /* It is in the bitmap block number equal to the block number divided by the number of
92                                        bits in a block. */
93         *bmap_nr = block / (s->s_blocksize << 3);
94                                     /* Within that bitmap block it is located at bit offset *offset. */
95         *offset = block % (s->s_blocksize << 3);
96         return;
97     }
98     
99     
100     /* There would be a modest performance benefit if we write a version
101        to free a list of blocks at once. -Hans */
102     				/* I wonder if it would be less modest
103                                        now that we use journaling. -Hans */
104     void reiserfs_free_block (struct reiserfs_transaction_handle *th, unsigned long block)
105     {
106         struct super_block * s = th->t_super;
107         struct reiserfs_super_block * rs;
108         struct buffer_head * sbh;
109         struct buffer_head ** apbh;
110         int nr, offset;
111     
112     #ifdef CONFIG_REISERFS_CHECK
113         if (!s)
114     	reiserfs_panic (s, "vs-4060: reiserfs_free_block: trying to free block on nonexistent device");
115     
116         if (is_reusable (s, block, 1) == 0)
117     	reiserfs_panic (s, "vs-4070: reiserfs_free_block: can not free such block");
118     #endif
119     
120       rs = SB_DISK_SUPER_BLOCK (s);
121       sbh = SB_BUFFER_WITH_SB (s);
122       apbh = SB_AP_BITMAP (s);
123     
124       get_bit_address (s, block, &nr, &offset);
125     
126       /* mark it before we clear it, just in case */
127       journal_mark_freed(th, s, block) ;
128     
129       reiserfs_prepare_for_journal(s, apbh[nr], 1 ) ;
130     
131       /* clear bit for the given block in bit map */
132       if (!reiserfs_test_and_clear_le_bit (offset, apbh[nr]->b_data)) {
133           reiserfs_warning ("vs-4080: reiserfs_free_block: "
134     			"free_block (%04x:%lu)[dev:blocknr]: bit already cleared\n", 
135     	    s->s_dev, block);
136       }
137       journal_mark_dirty (th, s, apbh[nr]);
138     
139       reiserfs_prepare_for_journal(s, sbh, 1) ;
140       /* update super block */
141       rs->s_free_blocks = cpu_to_le32 (le32_to_cpu (rs->s_free_blocks) + 1);
142     
143       journal_mark_dirty (th, s, sbh);
144       s->s_dirt = 1;
145     }
146     
147     
148     
149     /* beginning from offset-th bit in bmap_nr-th bitmap block,
150        find_forward finds the closest zero bit. It returns 1 and zero
151        bit address (bitmap, offset) if zero bit found or 0 if there is no
152        zero bit in the forward direction */
153     /* The function is NOT SCHEDULE-SAFE! */
154     static int find_forward (struct super_block * s, int * bmap_nr, int * offset, int for_unformatted)
155     {
156       int i, j;
157       struct buffer_head * bh;
158       unsigned long block_to_try = 0;
159       unsigned long next_block_to_try = 0 ;
160     
161       for (i = *bmap_nr; i < SB_BMAP_NR (s); i ++, *offset = 0) {
162         /* get corresponding bitmap block */
163         bh = SB_AP_BITMAP (s)[i];
164         if (buffer_locked (bh)) {
165             __wait_on_buffer (bh);
166         }
167     retry:
168         j = reiserfs_find_next_zero_le_bit ((unsigned long *)bh->b_data, 
169                                              s->s_blocksize << 3, *offset);
170     
171         /* wow, this really needs to be redone.  We can't allocate a block if
172         ** it is in the journal somehow.  reiserfs_in_journal makes a suggestion
173         ** for a good block if the one you ask for is in the journal.  Note,
174         ** reiserfs_in_journal might reject the block it suggests.  The big
175         ** gain from the suggestion is when a big file has been deleted, and
176         ** many blocks show free in the real bitmap, but are all not free
177         ** in the journal list bitmaps.
178         **
179         ** this whole system sucks.  The bitmaps should reflect exactly what
180         ** can and can't be allocated, and the journal should update them as
181         ** it goes.  TODO.
182         */
183         if (j < (s->s_blocksize << 3)) {
184           block_to_try = (i * (s->s_blocksize << 3)) + j; 
185     
186           /* the block is not in the journal, we can proceed */
187           if (!(reiserfs_in_journal(s, s->s_dev, block_to_try, s->s_blocksize, for_unformatted, &next_block_to_try))) {
188     	*bmap_nr = i;
189     	*offset = j;
190     	return 1;
191           } 
192           /* the block is in the journal */
193           else if ((j+1) < (s->s_blocksize << 3)) { /* try again */
194     	/* reiserfs_in_journal suggested a new block to try */
195     	if (next_block_to_try > 0) {
196     	  int new_i ;
197     	  get_bit_address (s, next_block_to_try, &new_i, offset);
198     
199     	  /* block is not in this bitmap. reset i and continue
200     	  ** we only reset i if new_i is in a later bitmap.
201     	  */
202     	  if (new_i > i) {
203     	    i = (new_i - 1 ); /* i gets incremented by the for loop */
204     	    continue ;
205     	  }
206     	} else {
207     	  /* no suggestion was made, just try the next block */
208     	  *offset = j+1 ;
209     	}
210     	goto retry ;
211           }
212         }
213       }
214         /* zero bit not found */
215         return 0;
216     }
217     
218     /* return 0 if no free blocks, else return 1 */
219     /* The function is NOT SCHEDULE-SAFE!  
220     ** because the bitmap block we want to change could be locked, and on its
221     ** way to the disk when we want to read it, and because of the 
222     ** flush_async_commits.  Per bitmap block locks won't help much, and 
223     ** really aren't needed, as we retry later on if we try to set the bit
224     ** and it is already set.
225     */
226     static int find_zero_bit_in_bitmap (struct super_block * s, 
227                                         unsigned long search_start, 
228     				    int * bmap_nr, int * offset, 
229     				    int for_unformatted)
230     {
231       int retry_count = 0 ;
232       /* get bit location (bitmap number and bit offset) of search_start block */
233       get_bit_address (s, search_start, bmap_nr, offset);
234     
235         /* note that we search forward in the bitmap, benchmarks have shown that it is better to allocate in increasing
236            sequence, which is probably due to the disk spinning in the forward direction.. */
237         if (find_forward (s, bmap_nr, offset, for_unformatted) == 0) {
238           /* there wasn't a free block with number greater than our
239              starting point, so we are going to go to the beginning of the disk */
240     
241     retry:
242           search_start = 0; /* caller will reset search_start for itself also. */
243           get_bit_address (s, search_start, bmap_nr, offset);
244           if (find_forward (s, bmap_nr,offset,for_unformatted) == 0) {
245     	if (for_unformatted) {	/* why only unformatted nodes? -Hans */
246     	  if (retry_count == 0) {
247     	    /* we've got a chance that flushing async commits will free up
248     	    ** some space.  Sync then retry
249     	    */
250     	    flush_async_commits(s) ;
251     	    retry_count++ ;
252     	    goto retry ;
253     	  } else if (retry_count > 0) {
254     	    /* nothing more we can do.  Make the others wait, flush
255     	    ** all log blocks to disk, and flush to their home locations.
256     	    ** this will free up any blocks held by the journal
257     	    */
258     	    SB_JOURNAL(s)->j_must_wait = 1 ;
259     	  }
260     	}
261             return 0;
262           }
263         }
264       return 1;
265     }
266     
267     /* get amount_needed free block numbers from scanning the bitmap of
268        free/used blocks.
269        
270        Optimize layout by trying to find them starting from search_start
271        and moving in increasing blocknr direction.  (This was found to be
272        faster than using a bi-directional elevator_direction, in part
273        because of disk spin direction, in part because by the time one
274        reaches the end of the disk the beginning of the disk is the least
275        congested).
276     
277        search_start is the block number of the left
278        semantic neighbor of the node we create.
279     
280        return CARRY_ON if everything is ok
281        return NO_DISK_SPACE if out of disk space
282        return NO_MORE_UNUSED_CONTIGUOUS_BLOCKS if the block we found is not contiguous to the last one
283        
284        return block numbers found, in the array free_blocknrs.  assumes
285        that any non-zero entries already present in the array are valid.
286        This feature is perhaps convenient coding when one might not have
287        used all blocknrs from the last time one called this function, or
288        perhaps it is an archaism from the days of schedule tracking, one
289        of us ought to reread the code that calls this, and analyze whether
290        it is still the right way to code it.
291     
292        spare space is used only when priority is set to 1. reiserfsck has
293        its own reiserfs_new_blocknrs, which can use reserved space
294     
295        exactly what reserved space?  the SPARE_SPACE?  if so, please comment reiserfs.h.
296     
297        Give example of who uses spare space, and say that it is a deadlock
298        avoidance mechanism.  -Hans */
299     
300     /* This function is NOT SCHEDULE-SAFE! */
301     
302     static int do_reiserfs_new_blocknrs (struct reiserfs_transaction_handle *th,
303                                          unsigned long * free_blocknrs, 
304     				     unsigned long search_start, 
305     				     int amount_needed, int priority, 
306     				     int for_unformatted,
307     				     int for_prealloc)
308     {
309       struct super_block * s = th->t_super;
310       int i, j;
311       unsigned long * block_list_start = free_blocknrs;
312       int init_amount_needed = amount_needed;
313       unsigned long new_block = 0 ; 
314     
315         if (SB_FREE_BLOCKS (s) < SPARE_SPACE && !priority)
316     	/* we can answer NO_DISK_SPACE being asked for new block with
317     	   priority 0 */
318     	return NO_DISK_SPACE;
319     
320     #ifdef CONFIG_REISERFS_CHECK
321         if (!s)
322     	reiserfs_panic (s, "vs-4090: reiserfs_new_blocknrs: trying to get new block from nonexistent device");
323         
324         if (search_start == MAX_B_NUM)
325     	reiserfs_panic (s, "vs-4100: reiserfs_new_blocknrs: we are optimizing location based on "
326     			"the bogus location of a temp buffer (%lu).", search_start);
327         
328         if (amount_needed < 1 || amount_needed > 2) 
329     	reiserfs_panic (s, "vs-4110: reiserfs_new_blocknrs: amount_needed parameter incorrect (%d)", amount_needed);
330     #endif /* CONFIG_REISERFS_CHECK */
331     
332       /* We continue the while loop if another process snatches our found
333        * free block from us after we find it but before we successfully
334        * mark it as in use, or if we need to use sync to free up some
335        * blocks on the preserve list.  */
336     
337       while (amount_needed--) {
338         /* skip over any blocknrs already gotten last time. */
339         if (*(free_blocknrs) != 0) {
340     #ifdef CONFIG_REISERFS_CHECK
341           if (is_reusable (s, *free_blocknrs, 1) == 0)
342     	reiserfs_panic(s, "vs-4120: reiserfs_new_blocknrs: bad blocknr on free_blocknrs list");
343     #endif /* CONFIG_REISERFS_CHECK */
344           free_blocknrs++;
345           continue;
346         }
347         /* look for zero bits in bitmap */
348         if (find_zero_bit_in_bitmap(s,search_start, &i, &j,for_unformatted) == 0) {
349           if (find_zero_bit_in_bitmap(s,search_start,&i,&j, for_unformatted) == 0) {
350     				/* recode without the goto and without
351     				   the if.  It will require a
352     				   duplicate for.  This is worth the
353     				   code clarity.  Your way was
354     				   admirable, and just a bit too
355     				   clever in saving instructions.:-)
356     				   I'd say create a new function, but
357     				   that would slow things also, yes?
358     				   -Hans */
359     free_and_return:
360     	for ( ; block_list_start != free_blocknrs; block_list_start++) {
361     	  reiserfs_free_block (th, *block_list_start);
362     	  *block_list_start = 0;
363     	}
364     	if (for_prealloc) 
365     	    return NO_MORE_UNUSED_CONTIGUOUS_BLOCKS;
366     	else
367     	    return NO_DISK_SPACE;
368           }
369         }
370         
371         /* i and j now contain the results of the search. i = bitmap block
372            number containing free block, j = offset in this block.  we
373            compute the blocknr which is our result, store it in
374            free_blocknrs, and increment the pointer so that on the next
375            loop we will insert into the next location in the array.  Also
376            in preparation for the next loop, search_start is changed so
377            that the next search will not rescan the same range but will
378            start where this search finished.  Note that while it is
379            possible that schedule has occurred and blocks have been freed
380            in that range, it is perhaps more important that the blocks
381            returned be near each other than that they be near their other
382            neighbors, and it also simplifies and speeds the code this way.  */
383     
384         /* journal: we need to make sure the block we are giving out is not
385         ** a log block, horrible things would happen there.
386         */
387         new_block = (i * (s->s_blocksize << 3)) + j; 
388         if (for_prealloc && (new_block - 1) != search_start) {
389           /* preallocated blocks must be contiguous, bail if we didnt find one.
390           ** this is not a bug.  We want to do the check here, before the
391           ** bitmap block is prepared, and before we set the bit and log the
392           ** bitmap. 
393           **
394           ** If we do the check after this function returns, we have to 
395           ** call reiserfs_free_block for new_block, which would be pure
396           ** overhead.
397           **
398           ** for_prealloc should only be set if the caller can deal with the
399           ** NO_MORE_UNUSED_CONTIGUOUS_BLOCKS return value.  This can be
400           ** returned before the disk is actually full
401           */
402           goto free_and_return ;
403         }
404         search_start = new_block ;
405         if (search_start >= reiserfs_get_journal_block(s) &&
406             search_start < (reiserfs_get_journal_block(s) + JOURNAL_BLOCK_COUNT)) {
407     	reiserfs_warning("vs-4130: reiserfs_new_blocknrs: trying to allocate log block %lu\n",
408     			 search_start) ;
409     	search_start++ ;
410     	amount_needed++ ;
411     	continue ;
412         }
413            
414     
415         reiserfs_prepare_for_journal(s, SB_AP_BITMAP(s)[i], 1) ;
416     
417     #ifdef CONFIG_REISERFS_CHECK
418         if (buffer_locked (SB_AP_BITMAP (s)[i]) || is_reusable (s, search_start, 0) == 0)
419     	reiserfs_panic (s, "vs-4140: reiserfs_new_blocknrs: bitmap block is locked or bad block number found");
420     #endif
421     
422         /* if this bit was already set, we've scheduled, and someone else
423         ** has allocated it.  loop around and try again
424         */
425         if (reiserfs_test_and_set_le_bit (j, SB_AP_BITMAP (s)[i]->b_data)) {
426     	reiserfs_warning("vs-4150: reiserfs_new_blocknrs, block not free");
427     	reiserfs_restore_prepared_buffer(s, SB_AP_BITMAP(s)[i]) ;
428     	amount_needed++ ;
429     	continue ;
430         }    
431         journal_mark_dirty (th, s, SB_AP_BITMAP (s)[i]); 
432         *free_blocknrs = search_start ;
433         free_blocknrs ++;
434       }
435     
436       reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s), 1) ;
437       /* update free block count in super block */
438       s->u.reiserfs_sb.s_rs->s_free_blocks = cpu_to_le32 (SB_FREE_BLOCKS (s) - init_amount_needed);
439       journal_mark_dirty (th, s, SB_BUFFER_WITH_SB (s));
440       s->s_dirt = 1;
441     
442       return CARRY_ON;
443     }
444     
445     // this is called only by get_empty_nodes with for_preserve_list==0
446     int reiserfs_new_blocknrs (struct reiserfs_transaction_handle *th, unsigned long * free_blocknrs,
447     			    unsigned long search_start, int amount_needed) {
448       return do_reiserfs_new_blocknrs(th, free_blocknrs, search_start, amount_needed, 0/*for_preserve_list-priority*/, 0/*for_formatted*/, 0/*for_prealloc */) ;
449     }
450     
451     
452     // called by get_new_buffer and by reiserfs_get_block with amount_needed == 1 and for_preserve_list == 0
453     int reiserfs_new_unf_blocknrs(struct reiserfs_transaction_handle *th, unsigned long * free_blocknrs,
454     			      unsigned long search_start) {
455       return do_reiserfs_new_blocknrs(th, free_blocknrs, search_start, 
456                                       1/*amount_needed*/,
457     				  0/*for_preserve_list-priority*/, 
458     				  1/*for formatted*/,
459     				  0/*for prealloc */) ;
460     }
461     
462     #ifdef REISERFS_PREALLOCATE
463     
464     /* 
465     ** We pre-allocate 8 blocks.  Pre-allocation is used for files > 16 KB only.
466     ** This lowers fragmentation on large files by grabbing a contiguous set of
467     ** blocks at once.  It also limits the number of times the bitmap block is
468     ** logged by making X number of allocation changes in a single transaction.
469     **
470     ** We are using a border to divide the disk into two parts.  The first part
471     ** is used for tree blocks, which have a very high turnover rate (they
472     ** are constantly allocated then freed)
473     **
474     ** The second part of the disk is for the unformatted nodes of larger files.
475     ** Putting them away from the tree blocks lowers fragmentation, and makes
476     ** it easier to group files together.  There are a number of different
477     ** allocation schemes being tried right now, each is documented below.
478     **
479     ** A great deal of the allocator's speed comes because reiserfs_get_block
480     ** sends us the block number of the last unformatted node in the file.  Once
481     ** a given block is allocated past the border, we don't collide with the
482     ** blocks near the search_start again.
483     ** 
484     */
485     int reiserfs_new_unf_blocknrs2 (struct reiserfs_transaction_handle *th, 
486     				struct inode       * p_s_inode,
487     				unsigned long      * free_blocknrs,
488     				unsigned long        search_start)
489     {
490       int ret=0, blks_gotten=0;
491       unsigned long border = 0;
492       unsigned long bstart = 0;
493       unsigned long hash_in, hash_out;
494       unsigned long saved_search_start=search_start;
495       int allocated[PREALLOCATION_SIZE];
496       int blks;
497     
498       if (!reiserfs_no_border(th->t_super)) {
499         /* we default to having the border at the 10% mark of the disk.  This
500         ** is an arbitrary decision and it needs tuning.  It also needs a limit
501         ** to prevent it from taking too much space on huge drives.
502         */
503         bstart = (SB_BLOCK_COUNT(th->t_super) / 10); 
504       }
505       if (!reiserfs_no_unhashed_relocation(th->t_super)) {
506         /* this is a very simple first attempt at preventing too much grouping
507         ** around the border value.  Since k_dir_id is never larger than the
508         ** highest allocated oid, it is far from perfect, and files will tend
509         ** to be grouped towards the start of the border
510         */
511         border = (INODE_PKEY(p_s_inode)->k_dir_id) % (SB_BLOCK_COUNT(th->t_super) - bstart - 1) ;
512       } else {
513         /* why would we want to delcare a local variable to this if statement
514         ** name border????? -chris
515         ** unsigned long border = 0;
516         */
517         if (!reiserfs_hashed_relocation(th->t_super)) {
518           hash_in = (INODE_PKEY(p_s_inode))->k_dir_id;
519     				/* I wonder if the CPU cost of the
520                                        hash will obscure the layout
521                                        effect? Of course, whether that
522                                        effect is good or bad we don't
523                                        know.... :-) */
524           
525           hash_out = keyed_hash(((char *) (&hash_in)), 4);
526           border = hash_out % (SB_BLOCK_COUNT(th->t_super) - bstart - 1) ;
527         }
528       }
529       border += bstart ;
530       allocated[0] = 0 ; /* important.  Allows a check later on to see if at
531                           * least one block was allocated.  This prevents false
532     		      * no disk space returns
533     		      */
534     
535       if ( (p_s_inode->i_size < 4 * 4096) || 
536            !(S_ISREG(p_s_inode->i_mode)) )
537         {
538           if ( search_start < border 
539     	   || (
540     				/* allow us to test whether it is a
541                                        good idea to prevent files from
542                                        getting too far away from their
543                                        packing locality by some unexpected
544                                        means.  This might be poor code for
545                                        directories whose files total
546                                        larger than 1/10th of the disk, and
547                                        it might be good code for
548                                        suffering from old insertions when the disk
549                                        was almost full. */
550                    /* changed from !reiserfs_test3(th->t_super), which doesn't
551                    ** seem like a good idea.  Think about adding blocks to
552                    ** a large file.  If you've allocated 10% of the disk
553                    ** in contiguous blocks, you start over at the border value
554                    ** for every new allocation.  This throws away all the
555                    ** information sent in about the last block that was allocated
556                    ** in the file.  Not a good general case at all.
557                    ** -chris
558                    */
559     	       reiserfs_test4(th->t_super) && 
560     	       (search_start > border + (SB_BLOCK_COUNT(th->t_super) / 10))
561     	       )
562     	   )
563     	search_start=border;
564       
565           ret = do_reiserfs_new_blocknrs(th, free_blocknrs, search_start, 
566     				     1/*amount_needed*/, 
567     				     0/*use reserved blocks for root */,
568     				     1/*for_formatted*/,
569     				     0/*for prealloc */) ;  
570           return ret;
571         }
572     
573       /* take a block off the prealloc list and return it -Hans */
574       if (p_s_inode->u.reiserfs_i.i_prealloc_count > 0) {
575         p_s_inode->u.reiserfs_i.i_prealloc_count--;
576         *free_blocknrs = p_s_inode->u.reiserfs_i.i_prealloc_block++;
577     
578         /* if no more preallocated blocks, remove inode from list */
579         if (! p_s_inode->u.reiserfs_i.i_prealloc_count) {
580           list_del(&p_s_inode->u.reiserfs_i.i_prealloc_list);
581         }
582         
583         return ret;
584       }
585     
586     				/* else get a new preallocation for the file */
587       reiserfs_discard_prealloc (th, p_s_inode);
588       /* this uses the last preallocated block as the search_start.  discard
589       ** prealloc does not zero out this number.
590       */
591       if (search_start <= p_s_inode->u.reiserfs_i.i_prealloc_block) {
592         search_start = p_s_inode->u.reiserfs_i.i_prealloc_block;
593       }
594       
595       /* doing the compare again forces search_start to be >= the border,
596       ** even if the file already had prealloction done.  This seems extra,
597       ** and should probably be removed
598       */
599       if ( search_start < border ) search_start=border; 
600     
601       /* If the disk free space is already below 10% we should 
602       ** start looking for the free blocks from the beginning 
603       ** of the partition, before the border line.
604       */
605       if ( SB_FREE_BLOCKS(th->t_super) <= (SB_BLOCK_COUNT(th->t_super) / 10) ) {
606         search_start=saved_search_start;
607       }
608     
609       *free_blocknrs = 0;
610       blks = PREALLOCATION_SIZE-1;
611       for (blks_gotten=0; blks_gotten<PREALLOCATION_SIZE; blks_gotten++) {
612         ret = do_reiserfs_new_blocknrs(th, free_blocknrs, search_start, 
613     				   1/*amount_needed*/, 
614     				   0/*for root reserved*/,
615     				   1/*for_formatted*/,
616     				   (blks_gotten > 0)/*must_be_contiguous*/) ;
617         /* if we didn't find a block this time, adjust blks to reflect
618         ** the actual number of blocks allocated
619         */ 
620         if (ret != CARRY_ON) {
621           blks = blks_gotten > 0 ? (blks_gotten - 1) : 0 ;
622           break ;
623         }
624         allocated[blks_gotten]= *free_blocknrs;
625     #ifdef CONFIG_REISERFS_CHECK
626         if ( (blks_gotten>0) && (allocated[blks_gotten] - allocated[blks_gotten-1]) != 1 ) {
627           /* this should be caught by new_blocknrs now, checking code */
628           reiserfs_warning("yura-1, reiserfs_new_unf_blocknrs2: pre-allocated not contiguous set of blocks!\n") ;
629           reiserfs_free_block(th, allocated[blks_gotten]);
630           blks = blks_gotten-1; 
631           break;
632         }
633     #endif
634         if (blks_gotten==0) {
635           p_s_inode->u.reiserfs_i.i_prealloc_block = *free_blocknrs;
636         }
637         search_start = *free_blocknrs; 
638         *free_blocknrs = 0;
639       }
640       p_s_inode->u.reiserfs_i.i_prealloc_count = blks;
641       *free_blocknrs = p_s_inode->u.reiserfs_i.i_prealloc_block;
642       p_s_inode->u.reiserfs_i.i_prealloc_block++;
643     
644       /* if inode has preallocated blocks, link him to list */
645       if (p_s_inode->u.reiserfs_i.i_prealloc_count) {
646         list_add(&p_s_inode->u.reiserfs_i.i_prealloc_list,
647     	     &SB_JOURNAL(th->t_super)->j_prealloc_list);
648       } 
649       /* we did actually manage to get 1 block */
650       if (ret != CARRY_ON && allocated[0] > 0) {
651         return CARRY_ON ;
652       }
653       /* NO_MORE_UNUSED_CONTIGUOUS_BLOCKS should only mean something to
654       ** the preallocation code.  The rest of the filesystem asks for a block
655       ** and should either get it, or know the disk is full.  The code
656       ** above should never allow ret == NO_MORE_UNUSED_CONTIGUOUS_BLOCK,
657       ** as it doesn't send for_prealloc = 1 to do_reiserfs_new_blocknrs
658       ** unless it has already successfully allocated at least one block.
659       ** Just in case, we translate into a return value the rest of the
660       ** filesystem can understand.
661       **
662       ** It is an error to change this without making the
663       ** rest of the filesystem understand NO_MORE_UNUSED_CONTIGUOUS_BLOCKS
664       ** If you consider it a bug to return NO_DISK_SPACE here, fix the rest
665       ** of the fs first.
666       */
667       if (ret == NO_MORE_UNUSED_CONTIGUOUS_BLOCKS) {
668     #ifdef CONFIG_REISERFS_CHECK
669         reiserfs_warning("reiser-2015: this shouldn't happen, may cause false out of disk space error");
670     #endif
671          return NO_DISK_SPACE; 
672       }
673       return ret;
674     }
675     
676     //
677     // a portion of this function, was derived from minix or ext2's
678     // analog. You should be able to tell which portion by looking at the
679     // ext2 code and comparing. 
680     static void __discard_prealloc (struct reiserfs_transaction_handle * th,
681     				struct inode * inode)
682     {
683       while (inode->u.reiserfs_i.i_prealloc_count > 0) {
684         reiserfs_free_block(th,inode->u.reiserfs_i.i_prealloc_block);
685         inode->u.reiserfs_i.i_prealloc_block++;
686         inode->u.reiserfs_i.i_prealloc_count --;
687       }
688       list_del (&(inode->u.reiserfs_i.i_prealloc_list));
689     }
690     
691     
692     void reiserfs_discard_prealloc (struct reiserfs_transaction_handle *th, 
693     				struct inode * inode)
694     {
695     #ifdef CONFIG_REISERFS_CHECK
696       if (inode->u.reiserfs_i.i_prealloc_count < 0)
697          reiserfs_warning("zam-4001:" __FUNCTION__ ": inode has negative prealloc blocks count.\n");
698     #endif  
699       if (inode->u.reiserfs_i.i_prealloc_count > 0) {
700         __discard_prealloc(th, inode);
701       }
702     }
703     
704     void reiserfs_discard_all_prealloc (struct reiserfs_transaction_handle *th)
705     {
706       struct list_head * plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
707       struct inode * inode;
708       
709       while (!list_empty(plist)) {
710         inode = list_entry(plist->next, struct inode, u.reiserfs_i.i_prealloc_list);
711     #ifdef CONFIG_REISERFS_CHECK
712         if (!inode->u.reiserfs_i.i_prealloc_count) {
713           reiserfs_warning("zam-4001:" __FUNCTION__ ": inode is in prealloc list but has no preallocated blocks.\n");
714         }
715     #endif    
716         __discard_prealloc(th, inode);
717       }
718     }
719     #endif
720