reiserfs: run scripts/Lindent on reiserfs code
Linus Torvalds [Wed, 13 Jul 2005 03:21:28 +0000 (20:21 -0700)]
This was a pure indentation change, using:

scripts/Lindent fs/reiserfs/*.c include/linux/reiserfs_*.h

to make reiserfs match the regular Linux indentation style.  As Jeff
Mahoney <jeffm@suse.com> writes:

 The ReiserFS code is a mix of a number of different coding styles, sometimes
 different even from line-to-line. Since the code has been relatively stable
 for quite some time and there are few outstanding patches to be applied, it
 is time to reformat the code to conform to the Linux style standard outlined
 in Documentation/CodingStyle.

 This patch contains the result of running scripts/Lindent against
 fs/reiserfs/*.c and include/linux/reiserfs_*.h. There are places where the
 code can be made to look better, but I'd rather keep those patches separate
 so that there isn't a subtle by-hand hand accident in the middle of a huge
 patch. To be clear: This patch is reformatting *only*.

 A number of patches may follow that continue to make the code more consistent
 with the Linux coding style.

 Hans wasn't particularly enthusiastic about these patches, but said he
 wouldn't really oppose them either.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>

30 files changed:
fs/reiserfs/bitmap.c
fs/reiserfs/dir.c
fs/reiserfs/do_balan.c
fs/reiserfs/file.c
fs/reiserfs/fix_node.c
fs/reiserfs/hashes.c
fs/reiserfs/ibalance.c
fs/reiserfs/inode.c
fs/reiserfs/ioctl.c
fs/reiserfs/item_ops.c
fs/reiserfs/journal.c
fs/reiserfs/lbalance.c
fs/reiserfs/namei.c
fs/reiserfs/objectid.c
fs/reiserfs/prints.c
fs/reiserfs/procfs.c
fs/reiserfs/resize.c
fs/reiserfs/stree.c
fs/reiserfs/super.c
fs/reiserfs/tail_conversion.c
fs/reiserfs/xattr.c
fs/reiserfs/xattr_acl.c
fs/reiserfs/xattr_security.c
fs/reiserfs/xattr_trusted.c
fs/reiserfs/xattr_user.c
include/linux/reiserfs_acl.h
include/linux/reiserfs_fs.h
include/linux/reiserfs_fs_i.h
include/linux/reiserfs_fs_sb.h
include/linux/reiserfs_xattr.h

index 49c479c..909f71e 100644 (file)
 #define TEST_OPTION(optname, s) \
     test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
 
-static inline void get_bit_address (struct super_block * s,
-                                   b_blocknr_t block, int * bmap_nr, int * offset)
+static inline void get_bit_address(struct super_block *s,
+                                  b_blocknr_t block, int *bmap_nr, int *offset)
 {
-    /* It is in the bitmap block number equal to the block
-     * number divided by the number of bits in a block. */
-    *bmap_nr = block / (s->s_blocksize << 3);
-    /* Within that bitmap block it is located at bit offset *offset. */
-    *offset = block & ((s->s_blocksize << 3) - 1 );
-    return;
+       /* It is in the bitmap block number equal to the block
+        * number divided by the number of bits in a block. */
+       *bmap_nr = block / (s->s_blocksize << 3);
+       /* Within that bitmap block it is located at bit offset *offset. */
+       *offset = block & ((s->s_blocksize << 3) - 1);
+       return;
 }
 
 #ifdef CONFIG_REISERFS_CHECK
-int is_reusable (struct super_block * s, b_blocknr_t block, int bit_value)
+int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
 {
-    int i, j;
+       int i, j;
 
-    if (block == 0 || block >= SB_BLOCK_COUNT (s)) {
-       reiserfs_warning (s, "vs-4010: is_reusable: block number is out of range %lu (%u)",
-                         block, SB_BLOCK_COUNT (s));
-       return 0;
-    }
-
-    /* it can't be one of the bitmap blocks */
-    for (i = 0; i < SB_BMAP_NR (s); i ++)
-       if (block == SB_AP_BITMAP (s)[i].bh->b_blocknr) {
-           reiserfs_warning (s, "vs: 4020: is_reusable: "
-                             "bitmap block %lu(%u) can't be freed or reused",
-                             block, SB_BMAP_NR (s));
-           return 0;
+       if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
+               reiserfs_warning(s,
+                                "vs-4010: is_reusable: block number is out of range %lu (%u)",
+                                block, SB_BLOCK_COUNT(s));
+               return 0;
        }
-  
-    get_bit_address (s, block, &i, &j);
 
-    if (i >= SB_BMAP_NR (s)) {
-       reiserfs_warning (s, "vs-4030: is_reusable: there is no so many bitmap blocks: "
-                         "block=%lu, bitmap_nr=%d", block, i);
-       return 0;
-    }
+       /* it can't be one of the bitmap blocks */
+       for (i = 0; i < SB_BMAP_NR(s); i++)
+               if (block == SB_AP_BITMAP(s)[i].bh->b_blocknr) {
+                       reiserfs_warning(s, "vs: 4020: is_reusable: "
+                                        "bitmap block %lu(%u) can't be freed or reused",
+                                        block, SB_BMAP_NR(s));
+                       return 0;
+               }
 
-    if ((bit_value == 0 && 
-         reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i].bh->b_data)) ||
-       (bit_value == 1 && 
-        reiserfs_test_le_bit(j, SB_AP_BITMAP (s)[i].bh->b_data) == 0)) {
-       reiserfs_warning (s, "vs-4040: is_reusable: corresponding bit of block %lu does not "
-                         "match required value (i==%d, j==%d) test_bit==%d",
-               block, i, j, reiserfs_test_le_bit (j, SB_AP_BITMAP (s)[i].bh->b_data));
+       get_bit_address(s, block, &i, &j);
 
-       return 0;
-    }
+       if (i >= SB_BMAP_NR(s)) {
+               reiserfs_warning(s,
+                                "vs-4030: is_reusable: there is no so many bitmap blocks: "
+                                "block=%lu, bitmap_nr=%d", block, i);
+               return 0;
+       }
 
-    if (bit_value == 0 && block == SB_ROOT_BLOCK (s)) {
-       reiserfs_warning (s, "vs-4050: is_reusable: this is root block (%u), "
-                         "it must be busy", SB_ROOT_BLOCK (s));
-       return 0;
-    }
+       if ((bit_value == 0 &&
+            reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i].bh->b_data)) ||
+           (bit_value == 1 &&
+            reiserfs_test_le_bit(j, SB_AP_BITMAP(s)[i].bh->b_data) == 0)) {
+               reiserfs_warning(s,
+                                "vs-4040: is_reusable: corresponding bit of block %lu does not "
+                                "match required value (i==%d, j==%d) test_bit==%d",
+                                block, i, j, reiserfs_test_le_bit(j,
+                                                                  SB_AP_BITMAP
+                                                                  (s)[i].bh->
+                                                                  b_data));
+
+               return 0;
+       }
 
-    return 1;
+       if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
+               reiserfs_warning(s,
+                                "vs-4050: is_reusable: this is root block (%u), "
+                                "it must be busy", SB_ROOT_BLOCK(s));
+               return 0;
+       }
+
+       return 1;
 }
-#endif /* CONFIG_REISERFS_CHECK */
+#endif                         /* CONFIG_REISERFS_CHECK */
 
 /* searches in journal structures for a given block number (bmap, off). If block
    is found in reiserfs journal it suggests next free block candidate to test. */
-static inline  int is_block_in_journal (struct super_block * s, int bmap, int
-off, int *next)
+static inline int is_block_in_journal(struct super_block *s, int bmap, int
+                                     off, int *next)
 {
-    b_blocknr_t tmp;
-
-    if (reiserfs_in_journal (s, bmap, off, 1, &tmp)) {
-       if (tmp) {              /* hint supplied */
-           *next = tmp;
-           PROC_INFO_INC( s, scan_bitmap.in_journal_hint );
-       } else {
-           (*next) = off + 1;          /* inc offset to avoid looping. */
-           PROC_INFO_INC( s, scan_bitmap.in_journal_nohint );
+       b_blocknr_t tmp;
+
+       if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
+               if (tmp) {      /* hint supplied */
+                       *next = tmp;
+                       PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
+               } else {
+                       (*next) = off + 1;      /* inc offset to avoid looping. */
+                       PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
+               }
+               PROC_INFO_INC(s, scan_bitmap.retry);
+               return 1;
        }
-       PROC_INFO_INC( s, scan_bitmap.retry );
-       return 1;
-    }
-    return 0;
+       return 0;
 }
 
 /* it searches for a window of zero bits with given minimum and maximum lengths in one bitmap
  * block; */
-static int scan_bitmap_block (struct reiserfs_transaction_handle *th,
-                             int bmap_n, int *beg, int boundary, int min, int max, int unfm)
+static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
+                            int bmap_n, int *beg, int boundary, int min,
+                            int max, int unfm)
 {
-    struct super_block *s = th->t_super;
-    struct reiserfs_bitmap_info *bi=&SB_AP_BITMAP(s)[bmap_n];
-    int end, next;
-    int org = *beg;
+       struct super_block *s = th->t_super;
+       struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
+       int end, next;
+       int org = *beg;
 
-    BUG_ON (!th->t_trans_id);
+       BUG_ON(!th->t_trans_id);
 
-    RFALSE(bmap_n >= SB_BMAP_NR (s), "Bitmap %d is out of range (0..%d)",bmap_n, SB_BMAP_NR (s) - 1);
-    PROC_INFO_INC( s, scan_bitmap.bmap );
+       RFALSE(bmap_n >= SB_BMAP_NR(s), "Bitmap %d is out of range (0..%d)",
+              bmap_n, SB_BMAP_NR(s) - 1);
+       PROC_INFO_INC(s, scan_bitmap.bmap);
 /* this is unclear and lacks comments, explain how journal bitmaps
    work here for the reader.  Convey a sense of the design here. What
    is a window? */
 /* - I mean `a window of zero bits' as in description of this function - Zam. */
-  
-    if ( !bi ) {
-       reiserfs_warning (s, "NULL bitmap info pointer for bitmap %d", bmap_n);
-       return 0;
-    }
-    if (buffer_locked (bi->bh)) {
-       PROC_INFO_INC( s, scan_bitmap.wait );
-       __wait_on_buffer (bi->bh);
-    }
-
-    while (1) {
-       cont:
-       if (bi->free_count < min)
-               return 0; // No free blocks in this bitmap
-
-       /* search for a first zero bit -- beggining of a window */
-       *beg = reiserfs_find_next_zero_le_bit
-               ((unsigned long*)(bi->bh->b_data), boundary, *beg);
-  
-       if (*beg + min > boundary) { /* search for a zero bit fails or the rest of bitmap block
-                                     * cannot contain a zero window of minimum size */
-           return 0;
-       }
 
-       if (unfm && is_block_in_journal(s,bmap_n, *beg, beg))
-           continue;
-       /* first zero bit found; we check next bits */
-       for (end = *beg + 1;; end ++) {
-           if (end >= *beg + max || end >= boundary || reiserfs_test_le_bit (end, bi->bh->b_data)) {
-               next = end;
-               break;
-           }
-           /* finding the other end of zero bit window requires looking into journal structures (in
-            * case of searching for free blocks for unformatted nodes) */
-           if (unfm && is_block_in_journal(s, bmap_n, end, &next))
-               break;
+       if (!bi) {
+               reiserfs_warning(s, "NULL bitmap info pointer for bitmap %d",
+                                bmap_n);
+               return 0;
+       }
+       if (buffer_locked(bi->bh)) {
+               PROC_INFO_INC(s, scan_bitmap.wait);
+               __wait_on_buffer(bi->bh);
        }
 
-       /* now (*beg) points to beginning of zero bits window,
-        * (end) points to one bit after the window end */
-       if (end - *beg >= min) { /* it seems we have found window of proper size */
-           int i;
-           reiserfs_prepare_for_journal (s, bi->bh, 1);
-           /* try to set all blocks used checking are they still free */
-           for (i = *beg; i < end; i++) {
-               /* It seems that we should not check in journal again. */
-               if (reiserfs_test_and_set_le_bit (i, bi->bh->b_data)) {
-                   /* bit was set by another process
-                    * while we slept in prepare_for_journal() */
-                   PROC_INFO_INC( s, scan_bitmap.stolen );
-                   if (i >= *beg + min)        { /* we can continue with smaller set of allocated blocks,
-                                          * if length of this set is more or equal to `min' */
-                       end = i;
-                       break;
-                   }
-                   /* otherwise we clear all bit were set ... */
-                   while (--i >= *beg)
-                       reiserfs_test_and_clear_le_bit (i, bi->bh->b_data);
-                   reiserfs_restore_prepared_buffer (s, bi->bh);
-                   *beg = org;
-                   /* ... and search again in current block from beginning */
-                   goto cont;  
+       while (1) {
+             cont:
+               if (bi->free_count < min)
+                       return 0;       // No free blocks in this bitmap
+
+               /* search for a first zero bit -- beggining of a window */
+               *beg = reiserfs_find_next_zero_le_bit
+                   ((unsigned long *)(bi->bh->b_data), boundary, *beg);
+
+               if (*beg + min > boundary) {    /* search for a zero bit fails or the rest of bitmap block
+                                                * cannot contain a zero window of minimum size */
+                       return 0;
                }
-           }
-           bi->free_count -= (end - *beg);
-           journal_mark_dirty (th, s, bi->bh);
 
-           /* free block count calculation */
-           reiserfs_prepare_for_journal (s, SB_BUFFER_WITH_SB(s), 1);
-           PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
-           journal_mark_dirty (th, s, SB_BUFFER_WITH_SB(s));
+               if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
+                       continue;
+               /* first zero bit found; we check next bits */
+               for (end = *beg + 1;; end++) {
+                       if (end >= *beg + max || end >= boundary
+                           || reiserfs_test_le_bit(end, bi->bh->b_data)) {
+                               next = end;
+                               break;
+                       }
+                       /* finding the other end of zero bit window requires looking into journal structures (in
+                        * case of searching for free blocks for unformatted nodes) */
+                       if (unfm && is_block_in_journal(s, bmap_n, end, &next))
+                               break;
+               }
 
-           return end - (*beg);
-       } else {
-           *beg = next;
+               /* now (*beg) points to beginning of zero bits window,
+                * (end) points to one bit after the window end */
+               if (end - *beg >= min) {        /* it seems we have found window of proper size */
+                       int i;
+                       reiserfs_prepare_for_journal(s, bi->bh, 1);
+                       /* try to set all blocks used checking are they still free */
+                       for (i = *beg; i < end; i++) {
+                               /* It seems that we should not check in journal again. */
+                               if (reiserfs_test_and_set_le_bit
+                                   (i, bi->bh->b_data)) {
+                                       /* bit was set by another process
+                                        * while we slept in prepare_for_journal() */
+                                       PROC_INFO_INC(s, scan_bitmap.stolen);
+                                       if (i >= *beg + min) {  /* we can continue with smaller set of allocated blocks,
+                                                                * if length of this set is more or equal to `min' */
+                                               end = i;
+                                               break;
+                                       }
+                                       /* otherwise we clear all bit were set ... */
+                                       while (--i >= *beg)
+                                               reiserfs_test_and_clear_le_bit
+                                                   (i, bi->bh->b_data);
+                                       reiserfs_restore_prepared_buffer(s,
+                                                                        bi->
+                                                                        bh);
+                                       *beg = org;
+                                       /* ... and search again in current block from beginning */
+                                       goto cont;
+                               }
+                       }
+                       bi->free_count -= (end - *beg);
+                       journal_mark_dirty(th, s, bi->bh);
+
+                       /* free block count calculation */
+                       reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
+                                                    1);
+                       PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
+                       journal_mark_dirty(th, s, SB_BUFFER_WITH_SB(s));
+
+                       return end - (*beg);
+               } else {
+                       *beg = next;
+               }
        }
-    }
 }
 
-static int bmap_hash_id(struct super_block *s, u32 id) {
-    char * hash_in = NULL;
-    unsigned long hash;
-    unsigned bm;
-
-    if (id <= 2) {
-       bm = 1;
-    } else {
-        hash_in = (char *)(&id);
-        hash = keyed_hash(hash_in, 4);
-       bm = hash % SB_BMAP_NR(s);
-       if (!bm)
-           bm = 1;
-    }
-    /* this can only be true when SB_BMAP_NR = 1 */
-    if (bm >= SB_BMAP_NR(s))
-       bm = 0;
-    return bm;
+static int bmap_hash_id(struct super_block *s, u32 id)
+{
+       char *hash_in = NULL;
+       unsigned long hash;
+       unsigned bm;
+
+       if (id <= 2) {
+               bm = 1;
+       } else {
+               hash_in = (char *)(&id);
+               hash = keyed_hash(hash_in, 4);
+               bm = hash % SB_BMAP_NR(s);
+               if (!bm)
+                       bm = 1;
+       }
+       /* this can only be true when SB_BMAP_NR = 1 */
+       if (bm >= SB_BMAP_NR(s))
+               bm = 0;
+       return bm;
 }
 
 /*
  * hashes the id and then returns > 0 if the block group for the
  * corresponding hash is full
  */
-static inline int block_group_used(struct super_block *s, u32 id) {
-    int bm;
-    bm = bmap_hash_id(s, id);
-    if (SB_AP_BITMAP(s)[bm].free_count > ((s->s_blocksize << 3) * 60 / 100) ) {
-        return 0;
-    }
-    return 1;
+static inline int block_group_used(struct super_block *s, u32 id)
+{
+       int bm;
+       bm = bmap_hash_id(s, id);
+       if (SB_AP_BITMAP(s)[bm].free_count > ((s->s_blocksize << 3) * 60 / 100)) {
+               return 0;
+       }
+       return 1;
 }
 
 /*
  * the packing is returned in disk byte order
  */
-__le32 reiserfs_choose_packing(struct inode *dir)
+__le32 reiserfs_choose_packing(struct inode * dir)
 {
-    __le32 packing;
-    if (TEST_OPTION(packing_groups, dir->i_sb)) {
-       u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
-       /*
-        * some versions of reiserfsck expect packing locality 1 to be
-        * special
-        */
-       if (parent_dir == 1 || block_group_used(dir->i_sb,parent_dir))
-            packing = INODE_PKEY(dir)->k_objectid;
-        else
-            packing = INODE_PKEY(dir)->k_dir_id;
-    } else
-        packing = INODE_PKEY(dir)->k_objectid;
-    return packing;
+       __le32 packing;
+       if (TEST_OPTION(packing_groups, dir->i_sb)) {
+               u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
+               /*
+                * some versions of reiserfsck expect packing locality 1 to be
+                * special
+                */
+               if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
+                       packing = INODE_PKEY(dir)->k_objectid;
+               else
+                       packing = INODE_PKEY(dir)->k_dir_id;
+       } else
+               packing = INODE_PKEY(dir)->k_objectid;
+       return packing;
 }
-  
+
 /* Tries to find contiguous zero bit window (given size) in given region of
  * bitmap and place new blocks there. Returns number of allocated blocks. */
-static int scan_bitmap (struct reiserfs_transaction_handle *th,
-                       b_blocknr_t *start, b_blocknr_t finish,
-                       int min, int max, int unfm, unsigned long file_block)
+static int scan_bitmap(struct reiserfs_transaction_handle *th,
+                      b_blocknr_t * start, b_blocknr_t finish,
+                      int min, int max, int unfm, unsigned long file_block)
 {
-    int nr_allocated=0;
-    struct super_block * s = th->t_super;
-    /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
-     * - Hans, it is not a block number - Zam. */
-
-    int bm, off;
-    int end_bm, end_off;
-    int off_max = s->s_blocksize << 3;
-
-    BUG_ON (!th->t_trans_id);
-
-    PROC_INFO_INC( s, scan_bitmap.call ); 
-    if ( SB_FREE_BLOCKS(s) <= 0)
-       return 0; // No point in looking for more free blocks
-
-    get_bit_address (s, *start, &bm, &off);
-    get_bit_address (s, finish, &end_bm, &end_off);
-    if (bm > SB_BMAP_NR(s))
-        return 0;
-    if (end_bm > SB_BMAP_NR(s))
-        end_bm = SB_BMAP_NR(s);
-
-    /* When the bitmap is more than 10% free, anyone can allocate.
-     * When it's less than 10% free, only files that already use the
-     * bitmap are allowed. Once we pass 80% full, this restriction
-     * is lifted.
-     *
-     * We do this so that files that grow later still have space close to
-     * their original allocation. This improves locality, and presumably
-     * performance as a result.
-     *
-     * This is only an allocation policy and does not make up for getting a
-     * bad hint. Decent hinting must be implemented for this to work well.
-     */
-    if ( TEST_OPTION(skip_busy, s) && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s)/20 ) {
-       for (;bm < end_bm; bm++, off = 0) {
-           if ( ( off && (!unfm || (file_block != 0))) || SB_AP_BITMAP(s)[bm].free_count > (s->s_blocksize << 3) / 10 )
-               nr_allocated = scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
-           if (nr_allocated)
-               goto ret;
-        }
-       /* we know from above that start is a reasonable number */
-       get_bit_address (s, *start, &bm, &off);
-    }
-
-    for (;bm < end_bm; bm++, off = 0) {
-       nr_allocated = scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
-       if (nr_allocated)
-           goto ret;
-    }
-
-    nr_allocated = scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
-  
- ret:
-    *start = bm * off_max + off;
-    return nr_allocated;
+       int nr_allocated = 0;
+       struct super_block *s = th->t_super;
+       /* find every bm and bmap and bmap_nr in this file, and change them all to bitmap_blocknr
+        * - Hans, it is not a block number - Zam. */
+
+       int bm, off;
+       int end_bm, end_off;
+       int off_max = s->s_blocksize << 3;
+
+       BUG_ON(!th->t_trans_id);
+
+       PROC_INFO_INC(s, scan_bitmap.call);
+       if (SB_FREE_BLOCKS(s) <= 0)
+               return 0;       // No point in looking for more free blocks
+
+       get_bit_address(s, *start, &bm, &off);
+       get_bit_address(s, finish, &end_bm, &end_off);
+       if (bm > SB_BMAP_NR(s))
+               return 0;
+       if (end_bm > SB_BMAP_NR(s))
+               end_bm = SB_BMAP_NR(s);
+
+       /* When the bitmap is more than 10% free, anyone can allocate.
+        * When it's less than 10% free, only files that already use the
+        * bitmap are allowed. Once we pass 80% full, this restriction
+        * is lifted.
+        *
+        * We do this so that files that grow later still have space close to
+        * their original allocation. This improves locality, and presumably
+        * performance as a result.
+        *
+        * This is only an allocation policy and does not make up for getting a
+        * bad hint. Decent hinting must be implemented for this to work well.
+        */
+       if (TEST_OPTION(skip_busy, s)
+           && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
+               for (; bm < end_bm; bm++, off = 0) {
+                       if ((off && (!unfm || (file_block != 0)))
+                           || SB_AP_BITMAP(s)[bm].free_count >
+                           (s->s_blocksize << 3) / 10)
+                               nr_allocated =
+                                   scan_bitmap_block(th, bm, &off, off_max,
+                                                     min, max, unfm);
+                       if (nr_allocated)
+                               goto ret;
+               }
+               /* we know from above that start is a reasonable number */
+               get_bit_address(s, *start, &bm, &off);
+       }
+
+       for (; bm < end_bm; bm++, off = 0) {
+               nr_allocated =
+                   scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
+               if (nr_allocated)
+                       goto ret;
+       }
+
+       nr_allocated =
+           scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
+
+      ret:
+       *start = bm * off_max + off;
+       return nr_allocated;
 
 }
 
-static void _reiserfs_free_block (struct reiserfs_transaction_handle *th,
-                                 struct inode *inode, b_blocknr_t block,
-                                 int for_unformatted)
+static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
+                                struct inode *inode, b_blocknr_t block,
+                                int for_unformatted)
 {
-    struct super_block * s = th->t_super;
-    struct reiserfs_super_block * rs;
-    struct buffer_head * sbh;
-    struct reiserfs_bitmap_info *apbi;
-    int nr, offset;
+       struct super_block *s = th->t_super;
+       struct reiserfs_super_block *rs;
+       struct buffer_head *sbh;
+       struct reiserfs_bitmap_info *apbi;
+       int nr, offset;
 
-    BUG_ON (!th->t_trans_id);
+       BUG_ON(!th->t_trans_id);
 
-    PROC_INFO_INC( s, free_block );
+       PROC_INFO_INC(s, free_block);
 
-    rs = SB_DISK_SUPER_BLOCK (s);
-    sbh = SB_BUFFER_WITH_SB (s);
-    apbi = SB_AP_BITMAP(s);
+       rs = SB_DISK_SUPER_BLOCK(s);
+       sbh = SB_BUFFER_WITH_SB(s);
+       apbi = SB_AP_BITMAP(s);
 
-    get_bit_address (s, block, &nr, &offset);
+       get_bit_address(s, block, &nr, &offset);
 
-    if (nr >= sb_bmap_nr (rs)) {
-       reiserfs_warning (s, "vs-4075: reiserfs_free_block: "
-                         "block %lu is out of range on %s",
-                         block, reiserfs_bdevname (s));
-       return;
-    }
-
-    reiserfs_prepare_for_journal(s, apbi[nr].bh, 1 ) ;
-
-    /* clear bit for the given block in bit map */
-    if (!reiserfs_test_and_clear_le_bit (offset, apbi[nr].bh->b_data)) {
-       reiserfs_warning (s, "vs-4080: reiserfs_free_block: "
-                         "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
-                         reiserfs_bdevname (s), block);
-    }
-    apbi[nr].free_count ++;
-    journal_mark_dirty (th, s, apbi[nr].bh);
-
-    reiserfs_prepare_for_journal(s, sbh, 1) ;
-    /* update super block */
-    set_sb_free_blocks( rs, sb_free_blocks(rs) + 1 );
-
-    journal_mark_dirty (th, s, sbh);
-    if (for_unformatted)
-        DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
+       if (nr >= sb_bmap_nr(rs)) {
+               reiserfs_warning(s, "vs-4075: reiserfs_free_block: "
+                                "block %lu is out of range on %s",
+                                block, reiserfs_bdevname(s));
+               return;
+       }
+
+       reiserfs_prepare_for_journal(s, apbi[nr].bh, 1);
+
+       /* clear bit for the given block in bit map */
+       if (!reiserfs_test_and_clear_le_bit(offset, apbi[nr].bh->b_data)) {
+               reiserfs_warning(s, "vs-4080: reiserfs_free_block: "
+                                "free_block (%s:%lu)[dev:blocknr]: bit already cleared",
+                                reiserfs_bdevname(s), block);
+       }
+       apbi[nr].free_count++;
+       journal_mark_dirty(th, s, apbi[nr].bh);
+
+       reiserfs_prepare_for_journal(s, sbh, 1);
+       /* update super block */
+       set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
+
+       journal_mark_dirty(th, s, sbh);
+       if (for_unformatted)
+               DQUOT_FREE_BLOCK_NODIRTY(inode, 1);
 }
 
-void reiserfs_free_block (struct reiserfs_transaction_handle *th, 
-                         struct inode *inode, b_blocknr_t block,
-                         int for_unformatted)
+void reiserfs_free_block(struct reiserfs_transaction_handle *th,
+                        struct inode *inode, b_blocknr_t block,
+                        int for_unformatted)
 {
-    struct super_block * s = th->t_super;
+       struct super_block *s = th->t_super;
 
-    BUG_ON (!th->t_trans_id);
+       BUG_ON(!th->t_trans_id);
 
-    RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
-    RFALSE(is_reusable (s, block, 1) == 0, "vs-4071: can not free such block");
-    /* mark it before we clear it, just in case */
-    journal_mark_freed(th, s, block) ;
-    _reiserfs_free_block(th, inode, block, for_unformatted) ;
+       RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
+       RFALSE(is_reusable(s, block, 1) == 0,
+              "vs-4071: can not free such block");
+       /* mark it before we clear it, just in case */
+       journal_mark_freed(th, s, block);
+       _reiserfs_free_block(th, inode, block, for_unformatted);
 }
 
 /* preallocated blocks don't need to be run through journal_mark_freed */
-static void reiserfs_free_prealloc_block (struct reiserfs_transaction_handle *th,
-                         struct inode *inode, b_blocknr_t block) {
-    RFALSE(!th->t_super, "vs-4060: trying to free block on nonexistent device");
-    RFALSE(is_reusable (th->t_super, block, 1) == 0, "vs-4070: can not free such block");
-    BUG_ON (!th->t_trans_id);
-    _reiserfs_free_block(th, inode, block, 1) ;
+static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
+                                        struct inode *inode, b_blocknr_t block)
+{
+       RFALSE(!th->t_super,
+              "vs-4060: trying to free block on nonexistent device");
+       RFALSE(is_reusable(th->t_super, block, 1) == 0,
+              "vs-4070: can not free such block");
+       BUG_ON(!th->t_trans_id);
+       _reiserfs_free_block(th, inode, block, 1);
 }
 
-static void __discard_prealloc (struct reiserfs_transaction_handle * th,
-                               struct reiserfs_inode_info *ei)
+static void __discard_prealloc(struct reiserfs_transaction_handle *th,
+                              struct reiserfs_inode_info *ei)
 {
-    unsigned long save = ei->i_prealloc_block ;
-    int dirty = 0;
-    struct inode *inode = &ei->vfs_inode;
-    BUG_ON (!th->t_trans_id);
+       unsigned long save = ei->i_prealloc_block;
+       int dirty = 0;
+       struct inode *inode = &ei->vfs_inode;
+       BUG_ON(!th->t_trans_id);
 #ifdef CONFIG_REISERFS_CHECK
-    if (ei->i_prealloc_count < 0)
-       reiserfs_warning (th->t_super, "zam-4001:%s: inode has negative prealloc blocks count.", __FUNCTION__ );
+       if (ei->i_prealloc_count < 0)
+               reiserfs_warning(th->t_super,
+                                "zam-4001:%s: inode has negative prealloc blocks count.",
+                                __FUNCTION__);
 #endif
-    while (ei->i_prealloc_count > 0) {
-       reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
-       ei->i_prealloc_block++;
-       ei->i_prealloc_count --;
-       dirty = 1;
-    }
-    if (dirty)
-       reiserfs_update_sd(th, inode);
-    ei->i_prealloc_block = save;
-    list_del_init(&(ei->i_prealloc_list));
+       while (ei->i_prealloc_count > 0) {
+               reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
+               ei->i_prealloc_block++;
+               ei->i_prealloc_count--;
+               dirty = 1;
+       }
+       if (dirty)
+               reiserfs_update_sd(th, inode);
+       ei->i_prealloc_block = save;
+       list_del_init(&(ei->i_prealloc_list));
 }
 
 /* FIXME: It should be inline function */
-void reiserfs_discard_prealloc (struct reiserfs_transaction_handle *th, 
-                               struct inode *inode)
+void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
+                              struct inode *inode)
 {
-    struct reiserfs_inode_info *ei = REISERFS_I(inode);
-    BUG_ON (!th->t_trans_id);
-    if (ei->i_prealloc_count)
-       __discard_prealloc(th, ei);
+       struct reiserfs_inode_info *ei = REISERFS_I(inode);
+       BUG_ON(!th->t_trans_id);
+       if (ei->i_prealloc_count)
+               __discard_prealloc(th, ei);
 }
 
-void reiserfs_discard_all_prealloc (struct reiserfs_transaction_handle *th)
+void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
 {
-    struct list_head * plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
+       struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
 
-    BUG_ON (!th->t_trans_id);
+       BUG_ON(!th->t_trans_id);
 
-    while (!list_empty(plist)) {
-       struct reiserfs_inode_info *ei;
-       ei = list_entry(plist->next, struct reiserfs_inode_info, i_prealloc_list);
+       while (!list_empty(plist)) {
+               struct reiserfs_inode_info *ei;
+               ei = list_entry(plist->next, struct reiserfs_inode_info,
+                               i_prealloc_list);
 #ifdef CONFIG_REISERFS_CHECK
-       if (!ei->i_prealloc_count) {
-           reiserfs_warning (th->t_super, "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.", __FUNCTION__);
-       }
+               if (!ei->i_prealloc_count) {
+                       reiserfs_warning(th->t_super,
+                                        "zam-4001:%s: inode is in prealloc list but has no preallocated blocks.",
+                                        __FUNCTION__);
+               }
 #endif
-       __discard_prealloc(th, ei);
-    }
+               __discard_prealloc(th, ei);
+       }
 }
 
-void reiserfs_init_alloc_options (struct super_block *s)
+void reiserfs_init_alloc_options(struct super_block *s)
 {
-    set_bit (_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
-    set_bit (_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
-    set_bit (_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
+       set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
+       set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
+       set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
 }
 
 /* block allocator related options are parsed here */
-int reiserfs_parse_alloc_options(struct super_block * s, char * options)
+int reiserfs_parse_alloc_options(struct super_block *s, char *options)
 {
-    char * this_char, * value;
-
-    REISERFS_SB(s)->s_alloc_options.bits = 0; /* clear default settings */
-
-    while ( (this_char = strsep (&options, ":")) != NULL ) {
-       if ((value = strchr (this_char, '=')) != NULL)
-           *value++ = 0;
-
-       if (!strcmp(this_char, "concentrating_formatted_nodes")) {
-           int temp;
-           SET_OPTION(concentrating_formatted_nodes);
-           temp = (value && *value) ? simple_strtoul (value, &value, 0) : 10;
-           if (temp <= 0 || temp > 100) {
-               REISERFS_SB(s)->s_alloc_options.border = 10;
-           } else {
-               REISERFS_SB(s)->s_alloc_options.border = 100 / temp;
-          }
-           continue;
-       }
-       if (!strcmp(this_char, "displacing_large_files")) {
-           SET_OPTION(displacing_large_files);
-           REISERFS_SB(s)->s_alloc_options.large_file_size =
-               (value && *value) ? simple_strtoul (value, &value, 0) : 16;
-           continue;
-       }
-       if (!strcmp(this_char, "displacing_new_packing_localities")) {
-           SET_OPTION(displacing_new_packing_localities);
-           continue;
-       };
-
-       if (!strcmp(this_char, "old_hashed_relocation")) {
-           SET_OPTION(old_hashed_relocation);
-           continue;
-       }
+       char *this_char, *value;
+
+       REISERFS_SB(s)->s_alloc_options.bits = 0;       /* clear default settings */
+
+       while ((this_char = strsep(&options, ":")) != NULL) {
+               if ((value = strchr(this_char, '=')) != NULL)
+                       *value++ = 0;
+
+               if (!strcmp(this_char, "concentrating_formatted_nodes")) {
+                       int temp;
+                       SET_OPTION(concentrating_formatted_nodes);
+                       temp = (value
+                               && *value) ? simple_strtoul(value, &value,
+                                                           0) : 10;
+                       if (temp <= 0 || temp > 100) {
+                               REISERFS_SB(s)->s_alloc_options.border = 10;
+                       } else {
+                               REISERFS_SB(s)->s_alloc_options.border =
+                                   100 / temp;
+                       }
+                       continue;
+               }
+               if (!strcmp(this_char, "displacing_large_files")) {
+                       SET_OPTION(displacing_large_files);
+                       REISERFS_SB(s)->s_alloc_options.large_file_size =
+                           (value
+                            && *value) ? simple_strtoul(value, &value, 0) : 16;
+                       continue;
+               }
+               if (!strcmp(this_char, "displacing_new_packing_localities")) {
+                       SET_OPTION(displacing_new_packing_localities);
+                       continue;
+               };
+
+               if (!strcmp(this_char, "old_hashed_relocation")) {
+                       SET_OPTION(old_hashed_relocation);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "new_hashed_relocation")) {
-           SET_OPTION(new_hashed_relocation);
-           continue;
-       }
+               if (!strcmp(this_char, "new_hashed_relocation")) {
+                       SET_OPTION(new_hashed_relocation);
+                       continue;
+               }
 
-        if (!strcmp(this_char, "dirid_groups")) {
-           SET_OPTION(dirid_groups);
-           continue;
-        }
-        if (!strcmp(this_char, "oid_groups")) {
-           SET_OPTION(oid_groups);
-           continue;
-        }
-        if (!strcmp(this_char, "packing_groups")) {
-           SET_OPTION(packing_groups);
-           continue;
-        }
-       if (!strcmp(this_char, "hashed_formatted_nodes")) {
-           SET_OPTION(hashed_formatted_nodes);
-           continue;
-       }
+               if (!strcmp(this_char, "dirid_groups")) {
+                       SET_OPTION(dirid_groups);
+                       continue;
+               }
+               if (!strcmp(this_char, "oid_groups")) {
+                       SET_OPTION(oid_groups);
+                       continue;
+               }
+               if (!strcmp(this_char, "packing_groups")) {
+                       SET_OPTION(packing_groups);
+                       continue;
+               }
+               if (!strcmp(this_char, "hashed_formatted_nodes")) {
+                       SET_OPTION(hashed_formatted_nodes);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "skip_busy")) {
-           SET_OPTION(skip_busy);
-           continue;
-       }
+               if (!strcmp(this_char, "skip_busy")) {
+                       SET_OPTION(skip_busy);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "hundredth_slices")) {
-           SET_OPTION(hundredth_slices);
-           continue;
-       }
+               if (!strcmp(this_char, "hundredth_slices")) {
+                       SET_OPTION(hundredth_slices);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "old_way")) {
-           SET_OPTION(old_way);
-           continue;
-       }
+               if (!strcmp(this_char, "old_way")) {
+                       SET_OPTION(old_way);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "displace_based_on_dirid")) {
-           SET_OPTION(displace_based_on_dirid);
-           continue;
-       }
+               if (!strcmp(this_char, "displace_based_on_dirid")) {
+                       SET_OPTION(displace_based_on_dirid);
+                       continue;
+               }
 
-       if (!strcmp(this_char, "preallocmin")) {
-           REISERFS_SB(s)->s_alloc_options.preallocmin =
-               (value && *value) ? simple_strtoul (value, &value, 0) : 4;
-           continue;
-       }
+               if (!strcmp(this_char, "preallocmin")) {
+                       REISERFS_SB(s)->s_alloc_options.preallocmin =
+                           (value
+                            && *value) ? simple_strtoul(value, &value, 0) : 4;
+                       continue;
+               }
+
+               if (!strcmp(this_char, "preallocsize")) {
+                       REISERFS_SB(s)->s_alloc_options.preallocsize =
+                           (value
+                            && *value) ? simple_strtoul(value, &value,
+                                                        0) :
+                           PREALLOCATION_SIZE;
+                       continue;
+               }
 
-       if (!strcmp(this_char, "preallocsize")) {
-           REISERFS_SB(s)->s_alloc_options.preallocsize =
-               (value && *value) ? simple_strtoul (value, &value, 0) : PREALLOCATION_SIZE;
-           continue;
+               reiserfs_warning(s, "zam-4001: %s : unknown option - %s",
+                                __FUNCTION__, this_char);
+               return 1;
        }
 
-       reiserfs_warning (s, "zam-4001: %s : unknown option - %s",
-                         __FUNCTION__ , this_char);
-       return 1;
-      }
-  
-    reiserfs_warning (s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
-    return 0;
+       reiserfs_warning(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
+       return 0;
 }
-  
-static inline void new_hashed_relocation (reiserfs_blocknr_hint_t * hint)
+
+static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
 {
-    char * hash_in;
-    if (hint->formatted_node) {
-           hash_in = (char*)&hint->key.k_dir_id;
-    } else {
-       if (!hint->inode) {
-           //hint->search_start = hint->beg;
-           hash_in = (char*)&hint->key.k_dir_id;
-       } else 
-           if ( TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
-               hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
-           else
-               hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
-      }
+       char *hash_in;
+       if (hint->formatted_node) {
+               hash_in = (char *)&hint->key.k_dir_id;
+       } else {
+               if (!hint->inode) {
+                       //hint->search_start = hint->beg;
+                       hash_in = (char *)&hint->key.k_dir_id;
+               } else
+                   if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
+                       hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
+               else
+                       hash_in =
+                           (char *)(&INODE_PKEY(hint->inode)->k_objectid);
+       }
 
-    hint->search_start = hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
+       hint->search_start =
+           hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
 }
 
 /*
  * Relocation based on dirid, hashing them into a given bitmap block
  * files. Formatted nodes are unaffected, a seperate policy covers them
  */
-static void
-dirid_groups (reiserfs_blocknr_hint_t *hint)
+static void dirid_groups(reiserfs_blocknr_hint_t * hint)
 {
-    unsigned long hash;
-    __u32 dirid = 0;
-    int bm = 0;
-    struct super_block *sb = hint->th->t_super;
-    if (hint->inode)
-       dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
-    else if (hint->formatted_node)
-        dirid = hint->key.k_dir_id;
-
-    if (dirid) {
-       bm = bmap_hash_id(sb, dirid);
-       hash = bm * (sb->s_blocksize << 3);
-       /* give a portion of the block group to metadata */
+       unsigned long hash;
+       __u32 dirid = 0;
+       int bm = 0;
+       struct super_block *sb = hint->th->t_super;
        if (hint->inode)
-           hash += sb->s_blocksize/2;
-       hint->search_start = hash;
-    }
+               dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
+       else if (hint->formatted_node)
+               dirid = hint->key.k_dir_id;
+
+       if (dirid) {
+               bm = bmap_hash_id(sb, dirid);
+               hash = bm * (sb->s_blocksize << 3);
+               /* give a portion of the block group to metadata */
+               if (hint->inode)
+                       hash += sb->s_blocksize / 2;
+               hint->search_start = hash;
+       }
 }
 
 /*
  * Relocation based on oid, hashing them into a given bitmap block
  * files. Formatted nodes are unaffected, a seperate policy covers them
  */
-static void
-oid_groups (reiserfs_blocknr_hint_t *hint)
+static void oid_groups(reiserfs_blocknr_hint_t * hint)
 {
-    if (hint->inode) {
-       unsigned long hash;
-       __u32 oid;
-       __u32 dirid;
-       int bm;
-
-       dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
-
-       /* keep the root dir and it's first set of subdirs close to
-        * the start of the disk
-        */
-       if (dirid <= 2)
-           hash = (hint->inode->i_sb->s_blocksize << 3);
-       else {
-           oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
-           bm = bmap_hash_id(hint->inode->i_sb, oid);
-           hash = bm * (hint->inode->i_sb->s_blocksize << 3);
+       if (hint->inode) {
+               unsigned long hash;
+               __u32 oid;
+               __u32 dirid;
+               int bm;
+
+               dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
+
+               /* keep the root dir and it's first set of subdirs close to
+                * the start of the disk
+                */
+               if (dirid <= 2)
+                       hash = (hint->inode->i_sb->s_blocksize << 3);
+               else {
+                       oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
+                       bm = bmap_hash_id(hint->inode->i_sb, oid);
+                       hash = bm * (hint->inode->i_sb->s_blocksize << 3);
+               }
+               hint->search_start = hash;
        }
-       hint->search_start = hash;
-    }
 }
 
 /* returns 1 if it finds an indirect item and gets valid hint info
  * from it, otherwise 0
  */
-static int get_left_neighbor(reiserfs_blocknr_hint_t *hint)
+static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
 {
-    struct path * path;
-    struct buffer_head * bh;
-    struct item_head * ih;
-    int pos_in_item;
-    __le32 * item;
-    int ret = 0;
-
-    if (!hint->path)           /* reiserfs code can call this function w/o pointer to path
+       struct path *path;
+       struct buffer_head *bh;
+       struct item_head *ih;
+       int pos_in_item;
+       __le32 *item;
+       int ret = 0;
+
+       if (!hint->path)        /* reiserfs code can call this function w/o pointer to path
                                 * structure supplied; then we rely on supplied search_start */
-       return 0;
-
-    path = hint->path;
-    bh = get_last_bh(path);
-    RFALSE( !bh, "green-4002: Illegal path specified to get_left_neighbor");
-    ih = get_ih(path);
-    pos_in_item = path->pos_in_item;
-    item = get_item (path);
-
-    hint->search_start = bh->b_blocknr;
-
-    if (!hint->formatted_node && is_indirect_le_ih (ih)) {
-       /* for indirect item: go to left and look for the first non-hole entry
-          in the indirect item */
-       if (pos_in_item == I_UNFM_NUM (ih))
-           pos_in_item--;
-//         pos_in_item = I_UNFM_NUM (ih) - 1;
-       while (pos_in_item >= 0) {
-           int t=get_block_num(item,pos_in_item);
-           if (t) {
-               hint->search_start = t;
-               ret = 1;
-               break;
-           }
-           pos_in_item --;
+               return 0;
+
+       path = hint->path;
+       bh = get_last_bh(path);
+       RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
+       ih = get_ih(path);
+       pos_in_item = path->pos_in_item;
+       item = get_item(path);
+
+       hint->search_start = bh->b_blocknr;
+
+       if (!hint->formatted_node && is_indirect_le_ih(ih)) {
+               /* for indirect item: go to left and look for the first non-hole entry
+                  in the indirect item */
+               if (pos_in_item == I_UNFM_NUM(ih))
+                       pos_in_item--;
+//          pos_in_item = I_UNFM_NUM (ih) - 1;
+               while (pos_in_item >= 0) {
+                       int t = get_block_num(item, pos_in_item);
+                       if (t) {
+                               hint->search_start = t;
+                               ret = 1;
+                               break;
+                       }
+                       pos_in_item--;
+               }
        }
-    }
 
-    /* does result value fit into specified region? */
-    return ret;
+       /* does result value fit into specified region? */
+       return ret;
 }
 
 /* should be, if formatted node, then try to put on first part of the device
    specified as number of percent with mount option device, else try to put
    on last of device.  This is not to say it is good code to do so,
    but the effect should be measured.  */
-static inline void set_border_in_hint(struct super_block *s, reiserfs_blocknr_hint_t *hint)
+static inline void set_border_in_hint(struct super_block *s,
+                                     reiserfs_blocknr_hint_t * hint)
 {
-    b_blocknr_t border = SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
+       b_blocknr_t border =
+           SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
 
-    if (hint->formatted_node)
-       hint->end = border - 1;
-    else
-       hint->beg = border;
+       if (hint->formatted_node)
+               hint->end = border - 1;
+       else
+               hint->beg = border;
 }
 
-static inline void displace_large_file(reiserfs_blocknr_hint_t *hint)
+static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
 {
-    if ( TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
-       hint->search_start = hint->beg + keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id), 4) % (hint->end - hint->beg);
-    else
-       hint->search_start = hint->beg + keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid), 4) % (hint->end - hint->beg);
+       if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
+               hint->search_start =
+                   hint->beg +
+                   keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
+                              4) % (hint->end - hint->beg);
+       else
+               hint->search_start =
+                   hint->beg +
+                   keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
+                              4) % (hint->end - hint->beg);
 }
 
-static inline void hash_formatted_node(reiserfs_blocknr_hint_t *hint)
+static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
 {
-   char * hash_in;
+       char *hash_in;
 
-   if (!hint->inode)
-       hash_in = (char*)&hint->key.k_dir_id;
-    else if ( TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
-       hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
-    else
-       hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
+       if (!hint->inode)
+               hash_in = (char *)&hint->key.k_dir_id;
+       else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
+               hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
+       else
+               hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
 
-       hint->search_start = hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
+       hint->search_start =
+           hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
 }
 
-static inline int this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *hint)
+static inline int
+this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
+                                                  hint)
 {
-    return hint->block == REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
+       return hint->block ==
+           REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
 }
 
 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
-static inline void displace_new_packing_locality (reiserfs_blocknr_hint_t *hint)
+static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
 {
-    struct in_core_key * key = &hint->key;
+       struct in_core_key *key = &hint->key;
 
-    hint->th->displace_new_blocks = 0;
-    hint->search_start = hint->beg + keyed_hash((char*)(&key->k_objectid),4) % (hint->end - hint->beg);
+       hint->th->displace_new_blocks = 0;
+       hint->search_start =
+           hint->beg + keyed_hash((char *)(&key->k_objectid),
+                                  4) % (hint->end - hint->beg);
 }
-  #endif
+#endif
 
-static inline int old_hashed_relocation (reiserfs_blocknr_hint_t * hint)
+static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
 {
-    b_blocknr_t border;
-    u32 hash_in;
-    
-    if (hint->formatted_node || hint->inode == NULL) {
-       return 0;
-      }
+       b_blocknr_t border;
+       u32 hash_in;
 
-    hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
-    border = hint->beg + (u32) keyed_hash(((char *) (&hash_in)), 4) % (hint->end - hint->beg - 1);
-    if (border > hint->search_start)
-       hint->search_start = border;
+       if (hint->formatted_node || hint->inode == NULL) {
+               return 0;
+       }
 
-    return 1;
-  }
-  
-static inline int old_way (reiserfs_blocknr_hint_t * hint)
-{
-    b_blocknr_t border;
-    
-    if (hint->formatted_node || hint->inode == NULL) {
-       return 0;
-    }
-  
-      border = hint->beg + le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end  - hint->beg);
-    if (border > hint->search_start)
-       hint->search_start = border;
+       hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
+       border =
+           hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
+                                        4) % (hint->end - hint->beg - 1);
+       if (border > hint->search_start)
+               hint->search_start = border;
 
-    return 1;
+       return 1;
 }
 
-static inline void hundredth_slices (reiserfs_blocknr_hint_t * hint)
+static inline int old_way(reiserfs_blocknr_hint_t * hint)
 {
-    struct in_core_key * key = &hint->key;
-    b_blocknr_t slice_start;
+       b_blocknr_t border;
+
+       if (hint->formatted_node || hint->inode == NULL) {
+               return 0;
+       }
+
+       border =
+           hint->beg +
+           le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
+                                                             hint->beg);
+       if (border > hint->search_start)
+               hint->search_start = border;
 
-    slice_start = (keyed_hash((char*)(&key->k_dir_id),4) % 100) * (hint->end / 100);
-    if ( slice_start > hint->search_start || slice_start + (hint->end / 100) <= hint->search_start) {
-       hint->search_start = slice_start;
-    }
+       return 1;
+}
+
+static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
+{
+       struct in_core_key *key = &hint->key;
+       b_blocknr_t slice_start;
+
+       slice_start =
+           (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
+       if (slice_start > hint->search_start
+           || slice_start + (hint->end / 100) <= hint->search_start) {
+               hint->search_start = slice_start;
+       }
 }
-  
-static void determine_search_start(reiserfs_blocknr_hint_t *hint,
-                                         int amount_needed)
+
+static void determine_search_start(reiserfs_blocknr_hint_t * hint,
+                                  int amount_needed)
 {
-    struct super_block *s = hint->th->t_super;
-    int unfm_hint;
+       struct super_block *s = hint->th->t_super;
+       int unfm_hint;
 
-    hint->beg = 0;
-    hint->end = SB_BLOCK_COUNT(s) - 1;
+       hint->beg = 0;
+       hint->end = SB_BLOCK_COUNT(s) - 1;
 
-    /* This is former border algorithm. Now with tunable border offset */
-    if (concentrating_formatted_nodes(s))
-       set_border_in_hint(s, hint);
+       /* This is former border algorithm. Now with tunable border offset */
+       if (concentrating_formatted_nodes(s))
+               set_border_in_hint(s, hint);
 
 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
-    /* whenever we create a new directory, we displace it.  At first we will
-       hash for location, later we might look for a moderately empty place for
-       it */
-    if (displacing_new_packing_localities(s)
-       && hint->th->displace_new_blocks) {
-       displace_new_packing_locality(hint);
-
-       /* we do not continue determine_search_start,
-        * if new packing locality is being displaced */
-       return;
-    }                                
+       /* whenever we create a new directory, we displace it.  At first we will
+          hash for location, later we might look for a moderately empty place for
+          it */
+       if (displacing_new_packing_localities(s)
+           && hint->th->displace_new_blocks) {
+               displace_new_packing_locality(hint);
+
+               /* we do not continue determine_search_start,
+                * if new packing locality is being displaced */
+               return;
+       }
 #endif
-  
-    /* all persons should feel encouraged to add more special cases here and
-     * test them */
 
-    if (displacing_large_files(s) && !hint->formatted_node
-       && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
-       displace_large_file(hint);
-       return;
-    }
-
-    /* if none of our special cases is relevant, use the left neighbor in the
-       tree order of the new node we are allocating for */
-    if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes,s)) {
-        hash_formatted_node(hint);
-       return;
-    }
+       /* all persons should feel encouraged to add more special cases here and
+        * test them */
 
-    unfm_hint = get_left_neighbor(hint);
+       if (displacing_large_files(s) && !hint->formatted_node
+           && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
+               displace_large_file(hint);
+               return;
+       }
 
-    /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
-       new blocks are displaced based on directory ID. Also, if suggested search_start
-       is less than last preallocated block, we start searching from it, assuming that
-       HDD dataflow is faster in forward direction */
-    if ( TEST_OPTION(old_way, s)) {
-       if (!hint->formatted_node) {
-           if ( !reiserfs_hashed_relocation(s))
-               old_way(hint);
-           else if (!reiserfs_no_unhashed_relocation(s))
-               old_hashed_relocation(hint);
+       /* if none of our special cases is relevant, use the left neighbor in the
+          tree order of the new node we are allocating for */
+       if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
+               hash_formatted_node(hint);
+               return;
+       }
 
-           if ( hint->inode && hint->search_start < REISERFS_I(hint->inode)->i_prealloc_block)
-               hint->search_start = REISERFS_I(hint->inode)->i_prealloc_block;
+       unfm_hint = get_left_neighbor(hint);
+
+       /* Mimic old block allocator behaviour, that is if VFS allowed for preallocation,
+          new blocks are displaced based on directory ID. Also, if suggested search_start
+          is less than last preallocated block, we start searching from it, assuming that
+          HDD dataflow is faster in forward direction */
+       if (TEST_OPTION(old_way, s)) {
+               if (!hint->formatted_node) {
+                       if (!reiserfs_hashed_relocation(s))
+                               old_way(hint);
+                       else if (!reiserfs_no_unhashed_relocation(s))
+                               old_hashed_relocation(hint);
+
+                       if (hint->inode
+                           && hint->search_start <
+                           REISERFS_I(hint->inode)->i_prealloc_block)
+                               hint->search_start =
+                                   REISERFS_I(hint->inode)->i_prealloc_block;
+               }
+               return;
        }
-       return;
-    }
 
-    /* This is an approach proposed by Hans */
-    if ( TEST_OPTION(hundredth_slices, s) && ! (displacing_large_files(s) && !hint->formatted_node)) {
-       hundredth_slices(hint);
-       return;
-    }
-
-    /* old_hashed_relocation only works on unformatted */
-    if (!unfm_hint && !hint->formatted_node &&
-        TEST_OPTION(old_hashed_relocation, s))
-    {
-       old_hashed_relocation(hint);
-    }
-    /* new_hashed_relocation works with both formatted/unformatted nodes */
-    if ((!unfm_hint || hint->formatted_node) &&
-        TEST_OPTION(new_hashed_relocation, s))
-    {
-       new_hashed_relocation(hint);
-    }
-    /* dirid grouping works only on unformatted nodes */
-    if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups,s))
-    {
-        dirid_groups(hint);
-    }
+       /* This is an approach proposed by Hans */
+       if (TEST_OPTION(hundredth_slices, s)
+           && !(displacing_large_files(s) && !hint->formatted_node)) {
+               hundredth_slices(hint);
+               return;
+       }
 
+       /* old_hashed_relocation only works on unformatted */
+       if (!unfm_hint && !hint->formatted_node &&
+           TEST_OPTION(old_hashed_relocation, s)) {
+               old_hashed_relocation(hint);
+       }
+       /* new_hashed_relocation works with both formatted/unformatted nodes */
+       if ((!unfm_hint || hint->formatted_node) &&
+           TEST_OPTION(new_hashed_relocation, s)) {
+               new_hashed_relocation(hint);
+       }
+       /* dirid grouping works only on unformatted nodes */
+       if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
+               dirid_groups(hint);
+       }
 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
-    if (hint->formatted_node && TEST_OPTION(dirid_groups,s))
-    {
-        dirid_groups(hint);
-    }
+       if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
+               dirid_groups(hint);
+       }
 #endif
 
-    /* oid grouping works only on unformatted nodes */
-    if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups,s))
-    {
-        oid_groups(hint);
-    }
-    return;
+       /* oid grouping works only on unformatted nodes */
+       if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
+               oid_groups(hint);
+       }
+       return;
 }
 
 static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
 {
-    /* make minimum size a mount option and benchmark both ways */
-    /* we preallocate blocks only for regular files, specific size */
-    /* benchmark preallocating always and see what happens */
-
-    hint->prealloc_size = 0;
-
-    if (!hint->formatted_node && hint->preallocate) {
-       if (S_ISREG(hint->inode->i_mode)
-           && hint->inode->i_size >= REISERFS_SB(hint->th->t_super)->s_alloc_options.preallocmin * hint->inode->i_sb->s_blocksize)
-           hint->prealloc_size = REISERFS_SB(hint->th->t_super)->s_alloc_options.preallocsize - 1;
-    }
-    return CARRY_ON;
+       /* make minimum size a mount option and benchmark both ways */
+       /* we preallocate blocks only for regular files, specific size */
+       /* benchmark preallocating always and see what happens */
+
+       hint->prealloc_size = 0;
+
+       if (!hint->formatted_node && hint->preallocate) {
+               if (S_ISREG(hint->inode->i_mode)
+                   && hint->inode->i_size >=
+                   REISERFS_SB(hint->th->t_super)->s_alloc_options.
+                   preallocmin * hint->inode->i_sb->s_blocksize)
+                       hint->prealloc_size =
+                           REISERFS_SB(hint->th->t_super)->s_alloc_options.
+                           preallocsize - 1;
+       }
+       return CARRY_ON;
 }
 
 /* XXX I know it could be merged with upper-level function;
    but may be result function would be too complex. */
-static inline int allocate_without_wrapping_disk (reiserfs_blocknr_hint_t * hint,
-                                        b_blocknr_t * new_blocknrs,
-                                        b_blocknr_t start, b_blocknr_t finish,
-                                        int min,
-                                        int amount_needed, int prealloc_size)
+static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
+                                                b_blocknr_t * new_blocknrs,
+                                                b_blocknr_t start,
+                                                b_blocknr_t finish, int min,
+                                                int amount_needed,
+                                                int prealloc_size)
 {
-    int rest = amount_needed;
-    int nr_allocated;
-  
-    while (rest > 0 && start <= finish) {
-       nr_allocated = scan_bitmap (hint->th, &start, finish, min,
-                                   rest + prealloc_size, !hint->formatted_node,
-                                   hint->block);
-
-       if (nr_allocated == 0)  /* no new blocks allocated, return */
-           break;
-       
-       /* fill free_blocknrs array first */
-       while (rest > 0 && nr_allocated > 0) {
-           * new_blocknrs ++ = start ++;
-           rest --; nr_allocated --;
-       }
+       int rest = amount_needed;
+       int nr_allocated;
+
+       while (rest > 0 && start <= finish) {
+               nr_allocated = scan_bitmap(hint->th, &start, finish, min,
+                                          rest + prealloc_size,
+                                          !hint->formatted_node, hint->block);
+
+               if (nr_allocated == 0)  /* no new blocks allocated, return */
+                       break;
+
+               /* fill free_blocknrs array first */
+               while (rest > 0 && nr_allocated > 0) {
+                       *new_blocknrs++ = start++;
+                       rest--;
+                       nr_allocated--;
+               }
 
-       /* do we have something to fill prealloc. array also ? */
-       if (nr_allocated > 0) {
-           /* it means prealloc_size was greater that 0 and we do preallocation */
-           list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
-                    &SB_JOURNAL(hint->th->t_super)->j_prealloc_list);
-           REISERFS_I(hint->inode)->i_prealloc_block = start;
-           REISERFS_I(hint->inode)->i_prealloc_count = nr_allocated;
-           break;
+               /* do we have something to fill prealloc. array also ? */
+               if (nr_allocated > 0) {
+                       /* it means prealloc_size was greater that 0 and we do preallocation */
+                       list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
+                                &SB_JOURNAL(hint->th->t_super)->
+                                j_prealloc_list);
+                       REISERFS_I(hint->inode)->i_prealloc_block = start;
+                       REISERFS_I(hint->inode)->i_prealloc_count =
+                           nr_allocated;
+                       break;
+               }
        }
-    }
 
-    return (amount_needed - rest);
+       return (amount_needed - rest);
 }
 
 static inline int blocknrs_and_prealloc_arrays_from_search_start
-    (reiserfs_blocknr_hint_t *hint, b_blocknr_t *new_blocknrs, int amount_needed)
-{
-    struct super_block *s = hint->th->t_super;
-    b_blocknr_t start = hint->search_start;
-    b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
-    int passno = 0;
-    int nr_allocated = 0;
-    int bigalloc = 0;
-
-    determine_prealloc_size(hint);
-    if (!hint->formatted_node) {
-        int quota_ret;
+    (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
+     int amount_needed) {
+       struct super_block *s = hint->th->t_super;
+       b_blocknr_t start = hint->search_start;
+       b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
+       int passno = 0;
+       int nr_allocated = 0;
+       int bigalloc = 0;
+
+       determine_prealloc_size(hint);
+       if (!hint->formatted_node) {
+               int quota_ret;
 #ifdef REISERQUOTA_DEBUG
-       reiserfs_debug (s, REISERFS_DEBUG_CODE, "reiserquota: allocating %d blocks id=%u", amount_needed, hint->inode->i_uid);
+               reiserfs_debug(s, REISERFS_DEBUG_CODE,
+                              "reiserquota: allocating %d blocks id=%u",
+                              amount_needed, hint->inode->i_uid);
 #endif
-       quota_ret = DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
-       if (quota_ret)    /* Quota exceeded? */
-           return QUOTA_EXCEEDED;
-       if (hint->preallocate && hint->prealloc_size ) {
+               quota_ret =
+                   DQUOT_ALLOC_BLOCK_NODIRTY(hint->inode, amount_needed);
+               if (quota_ret)  /* Quota exceeded? */
+                       return QUOTA_EXCEEDED;
+               if (hint->preallocate && hint->prealloc_size) {
 #ifdef REISERQUOTA_DEBUG
-           reiserfs_debug (s, REISERFS_DEBUG_CODE, "reiserquota: allocating (prealloc) %d blocks id=%u", hint->prealloc_size, hint->inode->i_uid);
+                       reiserfs_debug(s, REISERFS_DEBUG_CODE,
+                                      "reiserquota: allocating (prealloc) %d blocks id=%u",
+                                      hint->prealloc_size, hint->inode->i_uid);
 #endif
-           quota_ret = DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode, hint->prealloc_size);
-           if (quota_ret)
-               hint->preallocate=hint->prealloc_size=0;
+                       quota_ret =
+                           DQUOT_PREALLOC_BLOCK_NODIRTY(hint->inode,
+                                                        hint->prealloc_size);
+                       if (quota_ret)
+                               hint->preallocate = hint->prealloc_size = 0;
+               }
+               /* for unformatted nodes, force large allocations */
+               bigalloc = amount_needed;
        }
-       /* for unformatted nodes, force large allocations */
-       bigalloc = amount_needed;
-    }
 
-    do {
-       /* in bigalloc mode, nr_allocated should stay zero until
-        * the entire allocation is filled
-        */
-       if (unlikely(bigalloc && nr_allocated)) {
-           reiserfs_warning(s, "bigalloc is %d, nr_allocated %d\n",
-           bigalloc, nr_allocated);
-           /* reset things to a sane value */
-           bigalloc = amount_needed - nr_allocated;
-       }
-       /*
-        * try pass 0 and pass 1 looking for a nice big
-        * contiguous allocation.  Then reset and look
-        * for anything you can find.
-        */
-       if (passno == 2 && bigalloc) {
-           passno = 0;
-           bigalloc = 0;
-       }
-       switch (passno++) {
-        case 0: /* Search from hint->search_start to end of disk */
-           start = hint->search_start;
-           finish = SB_BLOCK_COUNT(s) - 1;
-           break;
-        case 1: /* Search from hint->beg to hint->search_start */
-           start = hint->beg;
-           finish = hint->search_start;
-           break;
-       case 2: /* Last chance: Search from 0 to hint->beg */
-           start = 0;
-           finish = hint->beg;
-           break;
-       default: /* We've tried searching everywhere, not enough space */
-           /* Free the blocks */
-           if (!hint->formatted_node) {
+       do {
+               /* in bigalloc mode, nr_allocated should stay zero until
+                * the entire allocation is filled
+                */
+               if (unlikely(bigalloc && nr_allocated)) {
+                       reiserfs_warning(s, "bigalloc is %d, nr_allocated %d\n",
+                                        bigalloc, nr_allocated);
+                       /* reset things to a sane value */
+                       bigalloc = amount_needed - nr_allocated;
+               }
+               /*
+                * try pass 0 and pass 1 looking for a nice big
+                * contiguous allocation.  Then reset and look
+                * for anything you can find.
+                */
+               if (passno == 2 && bigalloc) {
+                       passno = 0;
+                       bigalloc = 0;
+               }
+               switch (passno++) {
+               case 0: /* Search from hint->search_start to end of disk */
+                       start = hint->search_start;
+                       finish = SB_BLOCK_COUNT(s) - 1;
+                       break;
+               case 1: /* Search from hint->beg to hint->search_start */
+                       start = hint->beg;
+                       finish = hint->search_start;
+                       break;
+               case 2: /* Last chance: Search from 0 to hint->beg */
+                       start = 0;
+                       finish = hint->beg;
+                       break;
+               default:        /* We've tried searching everywhere, not enough space */
+                       /* Free the blocks */
+                       if (!hint->formatted_node) {
 #ifdef REISERQUOTA_DEBUG
-               reiserfs_debug (s, REISERFS_DEBUG_CODE, "reiserquota: freeing (nospace) %d blocks id=%u", amount_needed + hint->prealloc_size - nr_allocated, hint->inode->i_uid);
+                               reiserfs_debug(s, REISERFS_DEBUG_CODE,
+                                              "reiserquota: freeing (nospace) %d blocks id=%u",
+                                              amount_needed +
+                                              hint->prealloc_size -
+                                              nr_allocated,
+                                              hint->inode->i_uid);
 #endif
-               DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated);     /* Free not allocated blocks */
-           }
-           while (nr_allocated --)
-               reiserfs_free_block(hint->th, hint->inode, new_blocknrs[nr_allocated], !hint->formatted_node);
-
-           return NO_DISK_SPACE;
-       }
-    } while ((nr_allocated += allocate_without_wrapping_disk (hint,
-                           new_blocknrs + nr_allocated, start, finish,
-                           bigalloc ? bigalloc : 1,
-                           amount_needed - nr_allocated,
-                           hint->prealloc_size))
-                       < amount_needed);
-    if ( !hint->formatted_node &&
-         amount_needed + hint->prealloc_size >
-        nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
-    /* Some of preallocation blocks were not allocated */
+                               DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed + hint->prealloc_size - nr_allocated);      /* Free not allocated blocks */
+                       }
+                       while (nr_allocated--)
+                               reiserfs_free_block(hint->th, hint->inode,
+                                                   new_blocknrs[nr_allocated],
+                                                   !hint->formatted_node);
+
+                       return NO_DISK_SPACE;
+               }
+       } while ((nr_allocated += allocate_without_wrapping_disk(hint,
+                                                                new_blocknrs +
+                                                                nr_allocated,
+                                                                start, finish,
+                                                                bigalloc ?
+                                                                bigalloc : 1,
+                                                                amount_needed -
+                                                                nr_allocated,
+                                                                hint->
+                                                                prealloc_size))
+                < amount_needed);
+       if (!hint->formatted_node &&
+           amount_needed + hint->prealloc_size >
+           nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
+               /* Some of preallocation blocks were not allocated */
 #ifdef REISERQUOTA_DEBUG
-       reiserfs_debug (s, REISERFS_DEBUG_CODE, "reiserquota: freeing (failed prealloc) %d blocks id=%u", amount_needed + hint->prealloc_size - nr_allocated - REISERFS_I(hint->inode)->i_prealloc_count, hint->inode->i_uid);
+               reiserfs_debug(s, REISERFS_DEBUG_CODE,
+                              "reiserquota: freeing (failed prealloc) %d blocks id=%u",
+                              amount_needed + hint->prealloc_size -
+                              nr_allocated -
+                              REISERFS_I(hint->inode)->i_prealloc_count,
+                              hint->inode->i_uid);
 #endif
-       DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed +
-                                hint->prealloc_size - nr_allocated -
-                                REISERFS_I(hint->inode)->i_prealloc_count);
-    }
+               DQUOT_FREE_BLOCK_NODIRTY(hint->inode, amount_needed +
+                                        hint->prealloc_size - nr_allocated -
+                                        REISERFS_I(hint->inode)->
+                                        i_prealloc_count);
+       }
 
-    return CARRY_ON;
+       return CARRY_ON;
 }
 
 /* grab new blocknrs from preallocated list */
 /* return amount still needed after using them */
-static int use_preallocated_list_if_available (reiserfs_blocknr_hint_t *hint,
-                                              b_blocknr_t *new_blocknrs, int amount_needed)
+static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
+                                             b_blocknr_t * new_blocknrs,
+                                             int amount_needed)
 {
-    struct inode * inode = hint->inode;
+       struct inode *inode = hint->inode;
 
-    if (REISERFS_I(inode)->i_prealloc_count > 0) {
-       while (amount_needed) {
+       if (REISERFS_I(inode)->i_prealloc_count > 0) {
+               while (amount_needed) {
 
-           *new_blocknrs ++ = REISERFS_I(inode)->i_prealloc_block ++;
-           REISERFS_I(inode)->i_prealloc_count --;
+                       *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
+                       REISERFS_I(inode)->i_prealloc_count--;
 
-           amount_needed --;
+                       amount_needed--;
 
-           if (REISERFS_I(inode)->i_prealloc_count <= 0) {
-               list_del(&REISERFS_I(inode)->i_prealloc_list);  
-               break;
-           }
+                       if (REISERFS_I(inode)->i_prealloc_count <= 0) {
+                               list_del(&REISERFS_I(inode)->i_prealloc_list);
+                               break;
+                       }
+               }
        }
-      }
-    /* return amount still needed after using preallocated blocks */
-    return amount_needed;
+       /* return amount still needed after using preallocated blocks */
+       return amount_needed;
 }
 
-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
-                                                     already reserved */)
+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
+                                                                                                                                          already reserved */ )
 {
-    int initial_amount_needed = amount_needed;
-    int ret;
-    struct super_block *s = hint->th->t_super;
-
-    /* Check if there is enough space, taking into account reserved space */
-    if ( SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
-        amount_needed - reserved_by_us)
-        return NO_DISK_SPACE;
-    /* should this be if !hint->inode &&  hint->preallocate? */
-    /* do you mean hint->formatted_node can be removed ? - Zam */
-    /* hint->formatted_node cannot be removed because we try to access
-       inode information here, and there is often no inode assotiated with
-       metadata allocations - green */
-
-    if (!hint->formatted_node && hint->preallocate) {
-       amount_needed = use_preallocated_list_if_available
+       int initial_amount_needed = amount_needed;
+       int ret;
+       struct super_block *s = hint->th->t_super;
+
+       /* Check if there is enough space, taking into account reserved space */
+       if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
+           amount_needed - reserved_by_us)
+               return NO_DISK_SPACE;
+       /* should this be if !hint->inode &&  hint->preallocate? */
+       /* do you mean hint->formatted_node can be removed ? - Zam */
+       /* hint->formatted_node cannot be removed because we try to access
+          inode information here, and there is often no inode assotiated with
+          metadata allocations - green */
+
+       if (!hint->formatted_node && hint->preallocate) {
+               amount_needed = use_preallocated_list_if_available
+                   (hint, new_blocknrs, amount_needed);
+               if (amount_needed == 0) /* all blocknrs we need we got from
+                                          prealloc. list */
+                       return CARRY_ON;
+               new_blocknrs += (initial_amount_needed - amount_needed);
+       }
+
+       /* find search start and save it in hint structure */
+       determine_search_start(hint, amount_needed);
+       if (hint->search_start >= SB_BLOCK_COUNT(s))
+               hint->search_start = SB_BLOCK_COUNT(s) - 1;
+
+       /* allocation itself; fill new_blocknrs and preallocation arrays */
+       ret = blocknrs_and_prealloc_arrays_from_search_start
            (hint, new_blocknrs, amount_needed);
-       if (amount_needed == 0) /* all blocknrs we need we got from
-                                   prealloc. list */
-           return CARRY_ON;
-       new_blocknrs += (initial_amount_needed - amount_needed);
-    }
-
-    /* find search start and save it in hint structure */
-    determine_search_start(hint, amount_needed);
-    if (hint->search_start >= SB_BLOCK_COUNT(s))
-        hint->search_start = SB_BLOCK_COUNT(s) - 1;
-
-    /* allocation itself; fill new_blocknrs and preallocation arrays */
-    ret = blocknrs_and_prealloc_arrays_from_search_start
-       (hint, new_blocknrs, amount_needed);
-
-    /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
-     * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
-     * variant) */
-
-    if (ret != CARRY_ON) {
-       while (amount_needed ++ < initial_amount_needed) {
-           reiserfs_free_block(hint->th, hint->inode, *(--new_blocknrs), 1);
+
+       /* we used prealloc. list to fill (partially) new_blocknrs array. If final allocation fails we
+        * need to return blocks back to prealloc. list or just free them. -- Zam (I chose second
+        * variant) */
+
+       if (ret != CARRY_ON) {
+               while (amount_needed++ < initial_amount_needed) {
+                       reiserfs_free_block(hint->th, hint->inode,
+                                           *(--new_blocknrs), 1);
+               }
        }
-    }
-    return ret;
+       return ret;
 }
 
 /* These 2 functions are here to provide blocks reservation to the rest of kernel */
 /* Reserve @blocks amount of blocks in fs pointed by @sb. Caller must make sure
    there are actually this much blocks on the FS available */
-void reiserfs_claim_blocks_to_be_allocated( 
-                                     struct super_block *sb, /* super block of
-                                                               filesystem where
-                                                               blocks should be
-                                                               reserved */
-                                     int blocks /* How much to reserve */
-                                         )
+void reiserfs_claim_blocks_to_be_allocated(struct super_block *sb,     /* super block of
+                                                                          filesystem where
+                                                                          blocks should be
+                                                                          reserved */
+                                          int blocks   /* How much to reserve */
+    )
 {
 
-    /* Fast case, if reservation is zero - exit immediately. */
-    if ( !blocks )
-       return;
+       /* Fast case, if reservation is zero - exit immediately. */
+       if (!blocks)
+               return;
 
-    spin_lock(&REISERFS_SB(sb)->bitmap_lock);
-    REISERFS_SB(sb)->reserved_blocks += blocks;
-    spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
+       spin_lock(&REISERFS_SB(sb)->bitmap_lock);
+       REISERFS_SB(sb)->reserved_blocks += blocks;
+       spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
 }
 
 /* Unreserve @blocks amount of blocks in fs pointed by @sb */
-void reiserfs_release_claimed_blocks( 
-                               struct super_block *sb, /* super block of
-                                                         filesystem where
-                                                         blocks should be
-                                                         reserved */
-                               int blocks /* How much to unreserve */
-                                         )
+void reiserfs_release_claimed_blocks(struct super_block *sb,   /* super block of
+                                                                  filesystem where
+                                                                  blocks should be
+                                                                  reserved */
+                                    int blocks /* How much to unreserve */
+    )
 {
 
-    /* Fast case, if unreservation is zero - exit immediately. */
-    if ( !blocks )
-       return;
+       /* Fast case, if unreservation is zero - exit immediately. */
+       if (!blocks)
+               return;
 
-    spin_lock(&REISERFS_SB(sb)->bitmap_lock);
-    REISERFS_SB(sb)->reserved_blocks -= blocks;
-    spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
-    RFALSE( REISERFS_SB(sb)->reserved_blocks < 0, "amount of blocks reserved became zero?");
+       spin_lock(&REISERFS_SB(sb)->bitmap_lock);
+       REISERFS_SB(sb)->reserved_blocks -= blocks;
+       spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
+       RFALSE(REISERFS_SB(sb)->reserved_blocks < 0,
+              "amount of blocks reserved became zero?");
 }
 
 /* This function estimates how much pages we will be able to write to FS
    used for reiserfs_file_write() purposes for now. */
-int reiserfs_can_fit_pages ( struct super_block *sb /* superblock of filesystem
-                                                      to estimate space */ )
+int reiserfs_can_fit_pages(struct super_block *sb      /* superblock of filesystem
+                                                          to estimate space */ )
 {
        int space;
 
        spin_lock(&REISERFS_SB(sb)->bitmap_lock);
-       space = (SB_FREE_BLOCKS(sb) - REISERFS_SB(sb)->reserved_blocks) >> ( PAGE_CACHE_SHIFT - sb->s_blocksize_bits);
+       space =
+           (SB_FREE_BLOCKS(sb) -
+            REISERFS_SB(sb)->reserved_blocks) >> (PAGE_CACHE_SHIFT -
+                                                  sb->s_blocksize_bits);
        spin_unlock(&REISERFS_SB(sb)->bitmap_lock);
 
-       return space>0?space:0;
+       return space > 0 ? space : 0;
 }
index fbde4b0..9dd71e8 100644 (file)
 #include <linux/buffer_head.h>
 #include <asm/uaccess.h>
 
-extern struct reiserfs_key  MIN_KEY;
+extern struct reiserfs_key MIN_KEY;
 
-static int reiserfs_readdir (struct file *, void *, filldir_t);
-static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry, int datasync) ;
+static int reiserfs_readdir(struct file *, void *, filldir_t);
+static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
+                             int datasync);
 
 struct file_operations reiserfs_dir_operations = {
-    .read      = generic_read_dir,
-    .readdir   = reiserfs_readdir,
-    .fsync     = reiserfs_dir_fsync,
-    .ioctl     = reiserfs_ioctl,
+       .read = generic_read_dir,
+       .readdir = reiserfs_readdir,
+       .fsync = reiserfs_dir_fsync,
+       .ioctl = reiserfs_ioctl,
 };
 
-static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry, int datasync) {
-  struct inode *inode = dentry->d_inode;
-  int err;
-  reiserfs_write_lock(inode->i_sb);
-  err = reiserfs_commit_for_inode(inode) ;
-  reiserfs_write_unlock(inode->i_sb) ;
-  if (err < 0)
-      return err;
-  return 0;
+static int reiserfs_dir_fsync(struct file *filp, struct dentry *dentry,
+                             int datasync)
+{
+       struct inode *inode = dentry->d_inode;
+       int err;
+       reiserfs_write_lock(inode->i_sb);
+       err = reiserfs_commit_for_inode(inode);
+       reiserfs_write_unlock(inode->i_sb);
+       if (err < 0)
+               return err;
+       return 0;
 }
 
-
 #define store_ih(where,what) copy_item_head (where, what)
 
 //
-static int reiserfs_readdir (struct file * filp, void * dirent, filldir_t filldir)
+static int reiserfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
-    struct inode *inode = filp->f_dentry->d_inode;
-    struct cpu_key pos_key;    /* key of current position in the directory (key of directory entry) */
-    INITIALIZE_PATH (path_to_entry);
-    struct buffer_head * bh;
-    int item_num, entry_num;
-    const struct reiserfs_key * rkey;
-    struct item_head * ih, tmp_ih;
-    int search_res;
-    char * local_buf;
-    loff_t next_pos;
-    char small_buf[32] ; /* avoid kmalloc if we can */
-    struct reiserfs_dir_entry de;
-    int ret = 0;
-
-    reiserfs_write_lock(inode->i_sb);
-
-    reiserfs_check_lock_depth(inode->i_sb, "readdir") ;
-
-    /* form key for search the next directory entry using f_pos field of
-       file structure */
-    make_cpu_key (&pos_key, inode, (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET,
-                 TYPE_DIRENTRY, 3);
-    next_pos = cpu_key_k_offset (&pos_key);
-
-    /*  reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos);*/
-
-    path_to_entry.reada = PATH_READA;
-    while (1) {
-    research:
-       /* search the directory item, containing entry with specified key */
-       search_res = search_by_entry_key (inode->i_sb, &pos_key, &path_to_entry, &de);
-       if (search_res == IO_ERROR) {
-           // FIXME: we could just skip part of directory which could
-           // not be read
-           ret = -EIO;
-           goto out;
-       }
-       entry_num = de.de_entry_num;
-       bh = de.de_bh;
-       item_num = de.de_item_num;
-       ih = de.de_ih;
-       store_ih (&tmp_ih, ih);
-               
-       /* we must have found item, that is item of this directory, */
-       RFALSE( COMP_SHORT_KEYS (&(ih->ih_key), &pos_key),
-               "vs-9000: found item %h does not match to dir we readdir %K",
-               ih, &pos_key);
-       RFALSE( item_num > B_NR_ITEMS (bh) - 1,
-               "vs-9005 item_num == %d, item amount == %d", 
-               item_num, B_NR_ITEMS (bh));
-      
-       /* and entry must be not more than number of entries in the item */
-       RFALSE( I_ENTRY_COUNT (ih) < entry_num,
-               "vs-9010: entry number is too big %d (%d)", 
-               entry_num, I_ENTRY_COUNT (ih));
-
-       if (search_res == POSITION_FOUND || entry_num < I_ENTRY_COUNT (ih)) {
-           /* go through all entries in the directory item beginning from the entry, that has been found */
-           struct reiserfs_de_head * deh = B_I_DEH (bh, ih) + entry_num;
-
-           for (; entry_num < I_ENTRY_COUNT (ih); entry_num ++, deh ++) {
-               int d_reclen;
-               char * d_name;
-               off_t d_off;
-               ino_t d_ino;
-
-               if (!de_visible (deh))
-                   /* it is hidden entry */
-                   continue;
-               d_reclen = entry_length (bh, ih, entry_num);
-               d_name = B_I_DEH_ENTRY_FILE_NAME (bh, ih, deh);
-               if (!d_name[d_reclen - 1])
-                   d_reclen = strlen (d_name);
-       
-               if (d_reclen > REISERFS_MAX_NAME(inode->i_sb->s_blocksize)){
-                   /* too big to send back to VFS */
-                   continue ;
-               }
-
-                /* Ignore the .reiserfs_priv entry */
-                if (reiserfs_xattrs (inode->i_sb) &&
-                    !old_format_only(inode->i_sb) &&
-                    filp->f_dentry == inode->i_sb->s_root &&
-                    REISERFS_SB(inode->i_sb)->priv_root &&
-                    REISERFS_SB(inode->i_sb)->priv_root->d_inode &&
-                    deh_objectid(deh) == le32_to_cpu (INODE_PKEY(REISERFS_SB(inode->i_sb)->priv_root->d_inode)->k_objectid)) {
-                  continue;
-                }
-
-               d_off = deh_offset (deh);
-               filp->f_pos = d_off ;
-               d_ino = deh_objectid (deh);
-               if (d_reclen <= 32) {
-                 local_buf = small_buf ;
-               } else {
-                   local_buf = reiserfs_kmalloc(d_reclen, GFP_NOFS, inode->i_sb) ;
-                   if (!local_buf) {
-                       pathrelse (&path_to_entry);
-                       ret = -ENOMEM ;
+       struct inode *inode = filp->f_dentry->d_inode;
+       struct cpu_key pos_key; /* key of current position in the directory (key of directory entry) */
+       INITIALIZE_PATH(path_to_entry);
+       struct buffer_head *bh;
+       int item_num, entry_num;
+       const struct reiserfs_key *rkey;
+       struct item_head *ih, tmp_ih;
+       int search_res;
+       char *local_buf;
+       loff_t next_pos;
+       char small_buf[32];     /* avoid kmalloc if we can */
+       struct reiserfs_dir_entry de;
+       int ret = 0;
+
+       reiserfs_write_lock(inode->i_sb);
+
+       reiserfs_check_lock_depth(inode->i_sb, "readdir");
+
+       /* form key for search the next directory entry using f_pos field of
+          file structure */
+       make_cpu_key(&pos_key, inode,
+                    (filp->f_pos) ? (filp->f_pos) : DOT_OFFSET, TYPE_DIRENTRY,
+                    3);
+       next_pos = cpu_key_k_offset(&pos_key);
+
+       /*  reiserfs_warning (inode->i_sb, "reiserfs_readdir 1: f_pos = %Ld", filp->f_pos); */
+
+       path_to_entry.reada = PATH_READA;
+       while (1) {
+             research:
+               /* search the directory item, containing entry with specified key */
+               search_res =
+                   search_by_entry_key(inode->i_sb, &pos_key, &path_to_entry,
+                                       &de);
+               if (search_res == IO_ERROR) {
+                       // FIXME: we could just skip part of directory which could
+                       // not be read
+                       ret = -EIO;
                        goto out;
-                   }
-                   if (item_moved (&tmp_ih, &path_to_entry)) {
-                       reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
-                       goto research;
-                   }
-               }
-               // Note, that we copy name to user space via temporary
-               // buffer (local_buf) because filldir will block if
-               // user space buffer is swapped out. At that time
-               // entry can move to somewhere else
-               memcpy (local_buf, d_name, d_reclen);
-               if (filldir (dirent, local_buf, d_reclen, d_off, d_ino, 
-                            DT_UNKNOWN) < 0) {
-                   if (local_buf != small_buf) {
-                       reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
-                   }
-                   goto end;
                }
-               if (local_buf != small_buf) {
-                   reiserfs_kfree(local_buf, d_reclen, inode->i_sb) ;
+               entry_num = de.de_entry_num;
+               bh = de.de_bh;
+               item_num = de.de_item_num;
+               ih = de.de_ih;
+               store_ih(&tmp_ih, ih);
+
+               /* we must have found item, that is item of this directory, */
+               RFALSE(COMP_SHORT_KEYS(&(ih->ih_key), &pos_key),
+                      "vs-9000: found item %h does not match to dir we readdir %K",
+                      ih, &pos_key);
+               RFALSE(item_num > B_NR_ITEMS(bh) - 1,
+                      "vs-9005 item_num == %d, item amount == %d",
+                      item_num, B_NR_ITEMS(bh));
+
+               /* and entry must be not more than number of entries in the item */
+               RFALSE(I_ENTRY_COUNT(ih) < entry_num,
+                      "vs-9010: entry number is too big %d (%d)",
+                      entry_num, I_ENTRY_COUNT(ih));
+
+               if (search_res == POSITION_FOUND
+                   || entry_num < I_ENTRY_COUNT(ih)) {
+                       /* go through all entries in the directory item beginning from the entry, that has been found */
+                       struct reiserfs_de_head *deh =
+                           B_I_DEH(bh, ih) + entry_num;
+
+                       for (; entry_num < I_ENTRY_COUNT(ih);
+                            entry_num++, deh++) {
+                               int d_reclen;
+                               char *d_name;
+                               off_t d_off;
+                               ino_t d_ino;
+
+                               if (!de_visible(deh))
+                                       /* it is hidden entry */
+                                       continue;
+                               d_reclen = entry_length(bh, ih, entry_num);
+                               d_name = B_I_DEH_ENTRY_FILE_NAME(bh, ih, deh);
+                               if (!d_name[d_reclen - 1])
+                                       d_reclen = strlen(d_name);
+
+                               if (d_reclen >
+                                   REISERFS_MAX_NAME(inode->i_sb->
+                                                     s_blocksize)) {
+                                       /* too big to send back to VFS */
+                                       continue;
+                               }
+
+                               /* Ignore the .reiserfs_priv entry */
+                               if (reiserfs_xattrs(inode->i_sb) &&
+                                   !old_format_only(inode->i_sb) &&
+                                   filp->f_dentry == inode->i_sb->s_root &&
+                                   REISERFS_SB(inode->i_sb)->priv_root &&
+                                   REISERFS_SB(inode->i_sb)->priv_root->d_inode
+                                   && deh_objectid(deh) ==
+                                   le32_to_cpu(INODE_PKEY
+                                               (REISERFS_SB(inode->i_sb)->
+                                                priv_root->d_inode)->
+                                               k_objectid)) {
+                                       continue;
+                               }
+
+                               d_off = deh_offset(deh);
+                               filp->f_pos = d_off;
+                               d_ino = deh_objectid(deh);
+                               if (d_reclen <= 32) {
+                                       local_buf = small_buf;
+                               } else {
+                                       local_buf =
+                                           reiserfs_kmalloc(d_reclen, GFP_NOFS,
+                                                            inode->i_sb);
+                                       if (!local_buf) {
+                                               pathrelse(&path_to_entry);
+                                               ret = -ENOMEM;
+                                               goto out;
+                                       }
+                                       if (item_moved(&tmp_ih, &path_to_entry)) {
+                                               reiserfs_kfree(local_buf,
+                                                              d_reclen,
+                                                              inode->i_sb);
+                                               goto research;
+                                       }
+                               }
+                               // Note, that we copy name to user space via temporary
+                               // buffer (local_buf) because filldir will block if
+                               // user space buffer is swapped out. At that time
+                               // entry can move to somewhere else
+                               memcpy(local_buf, d_name, d_reclen);
+                               if (filldir
+                                   (dirent, local_buf, d_reclen, d_off, d_ino,
+                                    DT_UNKNOWN) < 0) {
+                                       if (local_buf != small_buf) {
+                                               reiserfs_kfree(local_buf,
+                                                              d_reclen,
+                                                              inode->i_sb);
+                                       }
+                                       goto end;
+                               }
+                               if (local_buf != small_buf) {
+                                       reiserfs_kfree(local_buf, d_reclen,
+                                                      inode->i_sb);
+                               }
+                               // next entry should be looked for with such offset
+                               next_pos = deh_offset(deh) + 1;
+
+                               if (item_moved(&tmp_ih, &path_to_entry)) {
+                                       goto research;
+                               }
+                       }       /* for */
                }
 
-               // next entry should be looked for with such offset
-               next_pos = deh_offset (deh) + 1;
+               if (item_num != B_NR_ITEMS(bh) - 1)
+                       // end of directory has been reached
+                       goto end;
+
+               /* item we went through is last item of node. Using right
+                  delimiting key check is it directory end */
+               rkey = get_rkey(&path_to_entry, inode->i_sb);
+               if (!comp_le_keys(rkey, &MIN_KEY)) {
+                       /* set pos_key to key, that is the smallest and greater
+                          that key of the last entry in the item */
+                       set_cpu_key_k_offset(&pos_key, next_pos);
+                       continue;
+               }
 
-               if (item_moved (&tmp_ih, &path_to_entry)) {
-                   goto research;
+               if (COMP_SHORT_KEYS(rkey, &pos_key)) {
+                       // end of directory has been reached
+                       goto end;
                }
-           } /* for */
-       }
-
-       if (item_num != B_NR_ITEMS (bh) - 1)
-           // end of directory has been reached
-           goto end;
-
-       /* item we went through is last item of node. Using right
-          delimiting key check is it directory end */
-       rkey = get_rkey (&path_to_entry, inode->i_sb);
-       if (! comp_le_keys (rkey, &MIN_KEY)) {
-           /* set pos_key to key, that is the smallest and greater
-              that key of the last entry in the item */
-           set_cpu_key_k_offset (&pos_key, next_pos);
-           continue;
-       }
-
-       if ( COMP_SHORT_KEYS (rkey, &pos_key)) {
-           // end of directory has been reached
-           goto end;
-       }
-       
-       /* directory continues in the right neighboring block */
-       set_cpu_key_k_offset (&pos_key, le_key_k_offset (KEY_FORMAT_3_5, rkey));
-
-    } /* while */
-
-
- end:
-    filp->f_pos = next_pos;
-    pathrelse (&path_to_entry);
-    reiserfs_check_path(&path_to_entry) ;
- out:
-    reiserfs_write_unlock(inode->i_sb);
-    return ret;
+
+               /* directory continues in the right neighboring block */
+               set_cpu_key_k_offset(&pos_key,
+                                    le_key_k_offset(KEY_FORMAT_3_5, rkey));
+
+       }                       /* while */
+
+      end:
+       filp->f_pos = next_pos;
+       pathrelse(&path_to_entry);
+       reiserfs_check_path(&path_to_entry);
+      out:
+       reiserfs_write_unlock(inode->i_sb);
+       return ret;
 }
 
 /* compose directory item containing "." and ".." entries (entries are
    not aligned to 4 byte boundary) */
 /* the last four params are LE */
-void make_empty_dir_item_v1 (char * body, __le32 dirid, __le32 objid,
-                            __le32 par_dirid, __le32 par_objid)
+void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
+                           __le32 par_dirid, __le32 par_objid)
 {
-    struct reiserfs_de_head * deh;
-
-    memset (body, 0, EMPTY_DIR_SIZE_V1);
-    deh = (struct reiserfs_de_head *)body;
-    
-    /* direntry header of "." */
-    put_deh_offset( &(deh[0]), DOT_OFFSET );
-    /* these two are from make_le_item_head, and are are LE */
-    deh[0].deh_dir_id = dirid;
-    deh[0].deh_objectid = objid;
-    deh[0].deh_state = 0; /* Endian safe if 0 */
-    put_deh_location( &(deh[0]), EMPTY_DIR_SIZE_V1 - strlen( "." ));
-    mark_de_visible(&(deh[0]));
-  
-    /* direntry header of ".." */
-    put_deh_offset( &(deh[1]), DOT_DOT_OFFSET);
-    /* key of ".." for the root directory */
-    /* these two are from the inode, and are are LE */
-    deh[1].deh_dir_id = par_dirid;
-    deh[1].deh_objectid = par_objid;
-    deh[1].deh_state = 0; /* Endian safe if 0 */
-    put_deh_location( &(deh[1]), deh_location( &(deh[0]) ) - strlen( ".." ) );
-    mark_de_visible(&(deh[1]));
-
-    /* copy ".." and "." */
-    memcpy (body + deh_location( &(deh[0]) ), ".", 1);
-    memcpy (body + deh_location( &(deh[1]) ), "..", 2);
+       struct reiserfs_de_head *deh;
+
+       memset(body, 0, EMPTY_DIR_SIZE_V1);
+       deh = (struct reiserfs_de_head *)body;
+
+       /* direntry header of "." */
+       put_deh_offset(&(deh[0]), DOT_OFFSET);
+       /* these two are from make_le_item_head, and are are LE */
+       deh[0].deh_dir_id = dirid;
+       deh[0].deh_objectid = objid;
+       deh[0].deh_state = 0;   /* Endian safe if 0 */
+       put_deh_location(&(deh[0]), EMPTY_DIR_SIZE_V1 - strlen("."));
+       mark_de_visible(&(deh[0]));
+
+       /* direntry header of ".." */
+       put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
+       /* key of ".." for the root directory */
+       /* these two are from the inode, and are are LE */
+       deh[1].deh_dir_id = par_dirid;
+       deh[1].deh_objectid = par_objid;
+       deh[1].deh_state = 0;   /* Endian safe if 0 */
+       put_deh_location(&(deh[1]), deh_location(&(deh[0])) - strlen(".."));
+       mark_de_visible(&(deh[1]));
+
+       /* copy ".." and "." */
+       memcpy(body + deh_location(&(deh[0])), ".", 1);
+       memcpy(body + deh_location(&(deh[1])), "..", 2);
 }
 
 /* compose directory item containing "." and ".." entries */
-void make_empty_dir_item (char * body, __le32 dirid, __le32 objid,
-                         __le32 par_dirid, __le32 par_objid)
+void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
+                        __le32 par_dirid, __le32 par_objid)
 {
-    struct reiserfs_de_head * deh;
-
-    memset (body, 0, EMPTY_DIR_SIZE);
-    deh = (struct reiserfs_de_head *)body;
-    
-    /* direntry header of "." */
-    put_deh_offset( &(deh[0]), DOT_OFFSET );
-    /* these two are from make_le_item_head, and are are LE */
-    deh[0].deh_dir_id = dirid;
-    deh[0].deh_objectid = objid;
-    deh[0].deh_state = 0; /* Endian safe if 0 */
-    put_deh_location( &(deh[0]), EMPTY_DIR_SIZE - ROUND_UP( strlen( "." ) ) );
-    mark_de_visible(&(deh[0]));
-  
-    /* direntry header of ".." */
-    put_deh_offset( &(deh[1]), DOT_DOT_OFFSET );
-    /* key of ".." for the root directory */
-    /* these two are from the inode, and are are LE */
-    deh[1].deh_dir_id = par_dirid;
-    deh[1].deh_objectid = par_objid;
-    deh[1].deh_state = 0; /* Endian safe if 0 */
-    put_deh_location( &(deh[1]), deh_location( &(deh[0])) - ROUND_UP( strlen( ".." ) ) );
-    mark_de_visible(&(deh[1]));
-
-    /* copy ".." and "." */
-    memcpy (body + deh_location( &(deh[0]) ), ".", 1);
-    memcpy (body + deh_location( &(deh[1]) ), "..", 2);
+       struct reiserfs_de_head *deh;
+
+       memset(body, 0, EMPTY_DIR_SIZE);
+       deh = (struct reiserfs_de_head *)body;
+
+       /* direntry header of "." */
+       put_deh_offset(&(deh[0]), DOT_OFFSET);
+       /* these two are from make_le_item_head, and are are LE */
+       deh[0].deh_dir_id = dirid;
+       deh[0].deh_objectid = objid;
+       deh[0].deh_state = 0;   /* Endian safe if 0 */
+       put_deh_location(&(deh[0]), EMPTY_DIR_SIZE - ROUND_UP(strlen(".")));
+       mark_de_visible(&(deh[0]));
+
+       /* direntry header of ".." */
+       put_deh_offset(&(deh[1]), DOT_DOT_OFFSET);
+       /* key of ".." for the root directory */
+       /* these two are from the inode, and are are LE */
+       deh[1].deh_dir_id = par_dirid;
+       deh[1].deh_objectid = par_objid;
+       deh[1].deh_state = 0;   /* Endian safe if 0 */
+       put_deh_location(&(deh[1]),
+                        deh_location(&(deh[0])) - ROUND_UP(strlen("..")));
+       mark_de_visible(&(deh[1]));
+
+       /* copy ".." and "." */
+       memcpy(body + deh_location(&(deh[0])), ".", 1);
+       memcpy(body + deh_location(&(deh[1])), "..", 2);
 }
index 2118db2..b2264ba 100644 (file)
@@ -8,7 +8,6 @@
 /* balance the tree according to the analysis made before,             */
 /* and using buffers obtained after all above.                         */
 
-
 /**
  ** balance_leaf_when_delete
  ** balance_leaf
 
 #ifdef CONFIG_REISERFS_CHECK
 
-struct tree_balance * cur_tb = NULL; /* detects whether more than one
-                                        copy of tb exists as a means
-                                        of checking whether schedule
-                                        is interrupting do_balance */
+struct tree_balance *cur_tb = NULL;    /* detects whether more than one
+                                          copy of tb exists as a means
+                                          of checking whether schedule
+                                          is interrupting do_balance */
 #endif
 
-inline void do_balance_mark_leaf_dirty (struct tree_balance * tb, 
-                                       struct buffer_head * bh, int flag)
+inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
+                                      struct buffer_head *bh, int flag)
 {
-    journal_mark_dirty(tb->transaction_handle,
-                       tb->transaction_handle->t_super, bh) ;
+       journal_mark_dirty(tb->transaction_handle,
+                          tb->transaction_handle->t_super, bh);
 }
 
 #define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
 #define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
 
-
 /* summary: 
  if deleting something ( tb->insert_size[0] < 0 )
    return(balance_leaf_when_delete()); (flag d handled here)
@@ -64,8 +62,6 @@ be performed by do_balance.
 
 -Hans */
 
-
-
 /* Balance leaf node in case of delete or cut: insert_size[0] < 0
  *
  * lnum, rnum can have values >= -1
@@ -73,1384 +69,1933 @@ be performed by do_balance.
  *      0 means that nothing should be done with the neighbor
  *     >0 means to shift entirely or partly the specified number of items to the neighbor
  */
-static int balance_leaf_when_delete (struct tree_balance * tb, int flag)
+static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
 {
-    struct buffer_head * tbS0 = PATH_PLAST_BUFFER (tb->tb_path);
-    int item_pos = PATH_LAST_POSITION (tb->tb_path);
-    int pos_in_item = tb->tb_path->pos_in_item;
-    struct buffer_info bi;
-    int n;
-    struct item_head * ih;
+       struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
+       int item_pos = PATH_LAST_POSITION(tb->tb_path);
+       int pos_in_item = tb->tb_path->pos_in_item;
+       struct buffer_info bi;
+       int n;
+       struct item_head *ih;
 
-    RFALSE( tb->FR[0] && B_LEVEL (tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
-           "vs- 12000: level: wrong FR %z", tb->FR[0]);
-    RFALSE( tb->blknum[0] > 1,
-           "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
-    RFALSE( ! tb->blknum[0] && ! PATH_H_PPARENT(tb->tb_path, 0),
-           "PAP-12010: tree can not be empty");
+       RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
+              "vs- 12000: level: wrong FR %z", tb->FR[0]);
+       RFALSE(tb->blknum[0] > 1,
+              "PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
+       RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
+              "PAP-12010: tree can not be empty");
 
-    ih = B_N_PITEM_HEAD (tbS0, item_pos);
+       ih = B_N_PITEM_HEAD(tbS0, item_pos);
 
-    /* Delete or truncate the item */
+       /* Delete or truncate the item */
 
-    switch (flag) {
-    case M_DELETE:   /* delete item in S[0] */
+       switch (flag) {
+       case M_DELETE:          /* delete item in S[0] */
+
+               RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
+                      "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
+                      -tb->insert_size[0], ih);
+
+               bi.tb = tb;
+               bi.bi_bh = tbS0;
+               bi.bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
+               bi.bi_position = PATH_H_POSITION(tb->tb_path, 1);
+               leaf_delete_items(&bi, 0, item_pos, 1, -1);
+
+               if (!item_pos && tb->CFL[0]) {
+                       if (B_NR_ITEMS(tbS0)) {
+                               replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0,
+                                           0);
+                       } else {
+                               if (!PATH_H_POSITION(tb->tb_path, 1))
+                                       replace_key(tb, tb->CFL[0], tb->lkey[0],
+                                                   PATH_H_PPARENT(tb->tb_path,
+                                                                  0), 0);
+                       }
+               }
 
-       RFALSE( ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
-               "vs-12013: mode Delete, insert size %d, ih to be deleted %h",
-                -tb->insert_size [0], ih);
+               RFALSE(!item_pos && !tb->CFL[0],
+                      "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
+                      tb->L[0]);
 
-       bi.tb = tb;
-       bi.bi_bh = tbS0;
-       bi.bi_parent = PATH_H_PPARENT (tb->tb_path, 0);
-       bi.bi_position = PATH_H_POSITION (tb->tb_path, 1);
-       leaf_delete_items (&bi, 0, item_pos, 1, -1);
-
-       if ( ! item_pos && tb->CFL[0] ) {
-           if ( B_NR_ITEMS(tbS0) ) {
-               replace_key(tb, tb->CFL[0],tb->lkey[0],tbS0,0);
-           }
-           else {
-               if ( ! PATH_H_POSITION (tb->tb_path, 1) )
-                   replace_key(tb, tb->CFL[0],tb->lkey[0],PATH_H_PPARENT(tb->tb_path, 0),0);
-           }
-       } 
-
-       RFALSE( ! item_pos && !tb->CFL[0],
-               "PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0], tb->L[0]);
-    
-       break;
-
-    case M_CUT: {  /* cut item in S[0] */
-       bi.tb = tb;
-       bi.bi_bh = tbS0;
-       bi.bi_parent = PATH_H_PPARENT (tb->tb_path, 0);
-       bi.bi_position = PATH_H_POSITION (tb->tb_path, 1);
-       if (is_direntry_le_ih (ih)) {
-
-           /* UFS unlink semantics are such that you can only delete one directory entry at a time. */
-           /* when we cut a directory tb->insert_size[0] means number of entries to be cut (always 1) */
-           tb->insert_size[0] = -1;
-           leaf_cut_from_buffer (&bi, item_pos, pos_in_item, -tb->insert_size[0]);
-
-           RFALSE( ! item_pos && ! pos_in_item && ! tb->CFL[0],
-                   "PAP-12030: can not change delimiting key. CFL[0]=%p", 
-                   tb->CFL[0]);
-
-           if ( ! item_pos && ! pos_in_item && tb->CFL[0] ) {
-               replace_key(tb, tb->CFL[0],tb->lkey[0],tbS0,0);
-           }
-       } else {
-           leaf_cut_from_buffer (&bi, item_pos, pos_in_item, -tb->insert_size[0]);
-
-           RFALSE( ! ih_item_len(ih),
-               "PAP-12035: cut must leave non-zero dynamic length of item");
-       }
-       break;
-    }
-
-    default:
-       print_cur_tb ("12040");
-       reiserfs_panic (tb->tb_sb, "PAP-12040: balance_leaf_when_delete: unexpectable mode: %s(%d)",
-                       (flag == M_PASTE) ? "PASTE" : ((flag == M_INSERT) ? "INSERT" : "UNKNOWN"), flag);
-    }
-
-    /* the rule is that no shifting occurs unless by shifting a node can be freed */
-    n = B_NR_ITEMS(tbS0);
-    if ( tb->lnum[0] )     /* L[0] takes part in balancing */
-    {
-       if ( tb->lnum[0] == -1 )    /* L[0] must be joined with S[0] */
-       {
-           if ( tb->rnum[0] == -1 )    /* R[0] must be also joined with S[0] */
-           {                   
-               if ( tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0) )
-               {
-                   /* all contents of all the 3 buffers will be in L[0] */
-                   if ( PATH_H_POSITION (tb->tb_path, 1) == 0 && 1 < B_NR_ITEMS(tb->FR[0]) )
-                       replace_key(tb, tb->CFL[0],tb->lkey[0],tb->FR[0],1);
-
-                   leaf_move_items (LEAF_FROM_S_TO_L, tb, n, -1, NULL);
-                   leaf_move_items (LEAF_FROM_R_TO_L, tb, B_NR_ITEMS(tb->R[0]), -1, NULL);
-
-                   reiserfs_invalidate_buffer (tb, tbS0);
-                   reiserfs_invalidate_buffer (tb, tb->R[0]);
-
-                   return 0;
+               break;
+
+       case M_CUT:{            /* cut item in S[0] */
+                       bi.tb = tb;
+                       bi.bi_bh = tbS0;
+                       bi.bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
+                       bi.bi_position = PATH_H_POSITION(tb->tb_path, 1);
+                       if (is_direntry_le_ih(ih)) {
+
+                               /* UFS unlink semantics are such that you can only delete one directory entry at a time. */
+                               /* when we cut a directory tb->insert_size[0] means number of entries to be cut (always 1) */
+                               tb->insert_size[0] = -1;
+                               leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
+                                                    -tb->insert_size[0]);
+
+                               RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
+                                      "PAP-12030: can not change delimiting key. CFL[0]=%p",
+                                      tb->CFL[0]);
+
+                               if (!item_pos && !pos_in_item && tb->CFL[0]) {
+                                       replace_key(tb, tb->CFL[0], tb->lkey[0],
+                                                   tbS0, 0);
+                               }
+                       } else {
+                               leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
+                                                    -tb->insert_size[0]);
+
+                               RFALSE(!ih_item_len(ih),
+                                      "PAP-12035: cut must leave non-zero dynamic length of item");
+                       }
+                       break;
                }
-               /* all contents of all the 3 buffers will be in R[0] */
-               leaf_move_items (LEAF_FROM_S_TO_R, tb, n, -1, NULL);
-               leaf_move_items (LEAF_FROM_L_TO_R, tb, B_NR_ITEMS(tb->L[0]), -1, NULL);
 
-               /* right_delimiting_key is correct in R[0] */
-               replace_key(tb, tb->CFR[0],tb->rkey[0],tb->R[0],0);
+       default:
+               print_cur_tb("12040");
+               reiserfs_panic(tb->tb_sb,
+                              "PAP-12040: balance_leaf_when_delete: unexpectable mode: %s(%d)",
+                              (flag ==
+                               M_PASTE) ? "PASTE" : ((flag ==
+                                                      M_INSERT) ? "INSERT" :
+                                                     "UNKNOWN"), flag);
+       }
 
-               reiserfs_invalidate_buffer (tb, tbS0);
-               reiserfs_invalidate_buffer (tb, tb->L[0]);
+       /* the rule is that no shifting occurs unless by shifting a node can be freed */
+       n = B_NR_ITEMS(tbS0);
+       if (tb->lnum[0]) {      /* L[0] takes part in balancing */
+               if (tb->lnum[0] == -1) {        /* L[0] must be joined with S[0] */
+                       if (tb->rnum[0] == -1) {        /* R[0] must be also joined with S[0] */
+                               if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
+                                       /* all contents of all the 3 buffers will be in L[0] */
+                                       if (PATH_H_POSITION(tb->tb_path, 1) == 0
+                                           && 1 < B_NR_ITEMS(tb->FR[0]))
+                                               replace_key(tb, tb->CFL[0],
+                                                           tb->lkey[0],
+                                                           tb->FR[0], 1);
+
+                                       leaf_move_items(LEAF_FROM_S_TO_L, tb, n,
+                                                       -1, NULL);
+                                       leaf_move_items(LEAF_FROM_R_TO_L, tb,
+                                                       B_NR_ITEMS(tb->R[0]),
+                                                       -1, NULL);
+
+                                       reiserfs_invalidate_buffer(tb, tbS0);
+                                       reiserfs_invalidate_buffer(tb,
+                                                                  tb->R[0]);
+
+                                       return 0;
+                               }
+                               /* all contents of all the 3 buffers will be in R[0] */
+                               leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1,
+                                               NULL);
+                               leaf_move_items(LEAF_FROM_L_TO_R, tb,
+                                               B_NR_ITEMS(tb->L[0]), -1, NULL);
+
+                               /* right_delimiting_key is correct in R[0] */
+                               replace_key(tb, tb->CFR[0], tb->rkey[0],
+                                           tb->R[0], 0);
 
-               return -1;
-           }
+                               reiserfs_invalidate_buffer(tb, tbS0);
+                               reiserfs_invalidate_buffer(tb, tb->L[0]);
 
-           RFALSE( tb->rnum[0] != 0, 
-                   "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
-           /* all contents of L[0] and S[0] will be in L[0] */
-           leaf_shift_left(tb, n, -1);
+                               return -1;
+                       }
 
-           reiserfs_invalidate_buffer (tb, tbS0);
+                       RFALSE(tb->rnum[0] != 0,
+                              "PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
+                       /* all contents of L[0] and S[0] will be in L[0] */
+                       leaf_shift_left(tb, n, -1);
 
-           return 0;
+                       reiserfs_invalidate_buffer(tb, tbS0);
+
+                       return 0;
+               }
+               /* a part of contents of S[0] will be in L[0] and the rest part of S[0] will be in R[0] */
+
+               RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
+                      (tb->lnum[0] + tb->rnum[0] > n + 1),
+                      "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
+                      tb->rnum[0], tb->lnum[0], n);
+               RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
+                      (tb->lbytes != -1 || tb->rbytes != -1),
+                      "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split",
+                      tb->rbytes, tb->lbytes);
+               RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
+                      (tb->lbytes < 1 || tb->rbytes != -1),
+                      "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split",
+                      tb->rbytes, tb->lbytes);
+
+               leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
+               leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
+
+               reiserfs_invalidate_buffer(tb, tbS0);
+
+               return 0;
        }
-       /* a part of contents of S[0] will be in L[0] and the rest part of S[0] will be in R[0] */
-
-       RFALSE( ( tb->lnum[0] + tb->rnum[0] < n ) || 
-               ( tb->lnum[0] + tb->rnum[0] > n+1 ),
-               "PAP-12050: rnum(%d) and lnum(%d) and item number(%d) in S[0] are not consistent",
-               tb->rnum[0], tb->lnum[0], n);
-       RFALSE( ( tb->lnum[0] + tb->rnum[0] == n ) && 
-               (tb->lbytes != -1 || tb->rbytes != -1),
-               "PAP-12055: bad rbytes (%d)/lbytes (%d) parameters when items are not split", 
-               tb->rbytes, tb->lbytes);
-       RFALSE( ( tb->lnum[0] + tb->rnum[0] == n + 1 ) && 
-               (tb->lbytes < 1 || tb->rbytes != -1),
-               "PAP-12060: bad rbytes (%d)/lbytes (%d) parameters when items are split", 
-               tb->rbytes, tb->lbytes);
-
-       leaf_shift_left (tb, tb->lnum[0], tb->lbytes);
-       leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
-
-       reiserfs_invalidate_buffer (tb, tbS0);
 
-       return 0;
-    }
+       if (tb->rnum[0] == -1) {
+               /* all contents of R[0] and S[0] will be in R[0] */
+               leaf_shift_right(tb, n, -1);
+               reiserfs_invalidate_buffer(tb, tbS0);
+               return 0;
+       }
 
-    if ( tb->rnum[0] == -1 ) {
-       /* all contents of R[0] and S[0] will be in R[0] */
-       leaf_shift_right(tb, n, -1);
-       reiserfs_invalidate_buffer (tb, tbS0);
+       RFALSE(tb->rnum[0],
+              "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
        return 0;
-    }
-
-    RFALSE( tb->rnum[0], 
-           "PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
-    return 0;
 }
 
-
-static int balance_leaf (struct tree_balance * tb,
-                        struct item_head * ih,         /* item header of inserted item (this is on little endian) */
-                        const char * body,             /* body  of inserted item or bytes to paste */
-                        int flag,                      /* i - insert, d - delete, c - cut, p - paste
-                                                          (see comment to do_balance) */
-                        struct item_head * insert_key,  /* in our processing of one level we sometimes determine what
-                                                           must be inserted into the next higher level.  This insertion
-                                                           consists of a key or two keys and their corresponding
-                                                           pointers */
-                        struct buffer_head ** insert_ptr /* inserted node-ptrs for the next level */
+static int balance_leaf(struct tree_balance *tb, struct item_head *ih, /* item header of inserted item (this is on little endian) */
+                       const char *body,       /* body  of inserted item or bytes to paste */
+                       int flag,       /* i - insert, d - delete, c - cut, p - paste
+                                          (see comment to do_balance) */
+                       struct item_head *insert_key,   /* in our processing of one level we sometimes determine what
+                                                          must be inserted into the next higher level.  This insertion
+                                                          consists of a key or two keys and their corresponding
+                                                          pointers */
+                       struct buffer_head **insert_ptr /* inserted node-ptrs for the next level */
     )
 {
-    struct buffer_head * tbS0 = PATH_PLAST_BUFFER (tb->tb_path);
-    int item_pos = PATH_LAST_POSITION (tb->tb_path);   /*  index into the array of item headers in S[0] 
-                                                           of the affected item */
-    struct buffer_info bi;
-    struct buffer_head *S_new[2];  /* new nodes allocated to hold what could not fit into S */
-    int snum[2];           /* number of items that will be placed
-                               into S_new (includes partially shifted
-                               items) */
-    int sbytes[2];          /* if an item is partially shifted into S_new then 
-                              if it is a directory item 
-                              it is the number of entries from the item that are shifted into S_new
-                              else
-                              it is the number of bytes from the item that are shifted into S_new
-                           */
-    int n, i;
-    int ret_val;
-    int pos_in_item;
-    int zeros_num;
-
-    PROC_INFO_INC( tb -> tb_sb, balance_at[ 0 ] );
-
-    /* Make balance in case insert_size[0] < 0 */
-    if ( tb->insert_size[0] < 0 )
-       return balance_leaf_when_delete (tb, flag);
-  
-    zeros_num = 0;
-    if (flag == M_INSERT && body == 0)
-       zeros_num = ih_item_len( ih );
-
-    pos_in_item = tb->tb_path->pos_in_item;
-    /* for indirect item pos_in_item is measured in unformatted node
-       pointers. Recalculate to bytes */
-    if (flag != M_INSERT && is_indirect_le_ih (B_N_PITEM_HEAD (tbS0, item_pos)))
-       pos_in_item *= UNFM_P_SIZE;
-
-    if ( tb->lnum[0] > 0 ) {
-       /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
-       if ( item_pos < tb->lnum[0] ) {
-           /* new item or it part falls to L[0], shift it too */
-           n = B_NR_ITEMS(tb->L[0]);
-
-           switch (flag) {
-           case M_INSERT:   /* insert item into L[0] */
-
-               if ( item_pos == tb->lnum[0] - 1 && tb->lbytes != -1 ) {
-                   /* part of new item falls into L[0] */
-                   int new_item_len;
-                   int version;
-
-                   ret_val = leaf_shift_left (tb, tb->lnum[0]-1, -1);
-
-                   /* Calculate item length to insert to S[0] */
-                   new_item_len = ih_item_len(ih) - tb->lbytes;
-                   /* Calculate and check item length to insert to L[0] */
-                   put_ih_item_len(ih, ih_item_len(ih) - new_item_len );
-
-                   RFALSE( ih_item_len(ih) <= 0,
-                           "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
-                            ih_item_len(ih));
-
-                   /* Insert new item into L[0] */
-                   bi.tb = tb;
-                   bi.bi_bh = tb->L[0];
-                   bi.bi_parent = tb->FL[0];
-                   bi.bi_position = get_left_neighbor_position (tb, 0);
-                   leaf_insert_into_buf (&bi, n + item_pos - ret_val, ih, body,
-                                         zeros_num > ih_item_len(ih) ? ih_item_len(ih) : zeros_num);
-
-                   version = ih_version (ih);
-
-                   /* Calculate key component, item length and body to insert into S[0] */
-                    set_le_ih_k_offset( ih, le_ih_k_offset( ih ) + (tb->lbytes << (is_indirect_le_ih(ih)?tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT:0)) );
-
-                   put_ih_item_len( ih, new_item_len );
-                   if ( tb->lbytes >  zeros_num ) {
-                       body += (tb->lbytes - zeros_num);
-                       zeros_num = 0;
-                   }
-                   else
-                       zeros_num -= tb->lbytes;
-
-                   RFALSE( ih_item_len(ih) <= 0,
-                       "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
-                       ih_item_len(ih));
-               } else {
-                   /* new item in whole falls into L[0] */
-                   /* Shift lnum[0]-1 items to L[0] */
-                   ret_val = leaf_shift_left(tb, tb->lnum[0]-1, tb->lbytes);
-                   /* Insert new item into L[0] */
-                   bi.tb = tb;
-                   bi.bi_bh = tb->L[0];
-                   bi.bi_parent = tb->FL[0];
-                   bi.bi_position = get_left_neighbor_position (tb, 0);
-                   leaf_insert_into_buf (&bi, n + item_pos - ret_val, ih, body, zeros_num);
-                   tb->insert_size[0] = 0;
-                   zeros_num = 0;
-               }
-               break;
-
-           case M_PASTE:   /* append item in L[0] */
-
-               if ( item_pos == tb->lnum[0] - 1 && tb->lbytes != -1 ) {
-                   /* we must shift the part of the appended item */
-                   if ( is_direntry_le_ih (B_N_PITEM_HEAD (tbS0, item_pos))) {
-
-                       RFALSE( zeros_num,
-                               "PAP-12090: invalid parameter in case of a directory");
-                       /* directory item */
-                       if ( tb->lbytes > pos_in_item ) {
-                           /* new directory entry falls into L[0] */
-                           struct item_head * pasted;
-                           int l_pos_in_item = pos_in_item;
-                                                         
-                           /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
-                           ret_val = leaf_shift_left(tb, tb->lnum[0], tb->lbytes - 1);
-                           if ( ret_val && ! item_pos ) {
-                               pasted =  B_N_PITEM_HEAD(tb->L[0],B_NR_ITEMS(tb->L[0])-1);
-                               l_pos_in_item += I_ENTRY_COUNT(pasted) - (tb->lbytes-1);
-                           }
-
-                           /* Append given directory entry to directory item */
-                           bi.tb = tb;
-                           bi.bi_bh = tb->L[0];
-                           bi.bi_parent = tb->FL[0];
-                           bi.bi_position = get_left_neighbor_position (tb, 0);
-                           leaf_paste_in_buffer (&bi, n + item_pos - ret_val, l_pos_in_item,
-                                                 tb->insert_size[0], body, zeros_num);
-
-                           /* previous string prepared space for pasting new entry, following string pastes this entry */
-
-                           /* when we have merge directory item, pos_in_item has been changed too */
-
-                           /* paste new directory entry. 1 is entry number */
-                           leaf_paste_entries (bi.bi_bh, n + item_pos - ret_val, l_pos_in_item, 1,
-                                               (struct reiserfs_de_head *)body, 
-                                               body + DEH_SIZE, tb->insert_size[0]
-                               );
-                           tb->insert_size[0] = 0;
-                       } else {
-                           /* new directory item doesn't fall into L[0] */
-                           /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
-                           leaf_shift_left (tb, tb->lnum[0], tb->lbytes);
-                       }
-                       /* Calculate new position to append in item body */
-                       pos_in_item -= tb->lbytes;
-                   }
-                   else {
-                       /* regular object */
-                       RFALSE( tb->lbytes <= 0,
-                               "PAP-12095: there is nothing to shift to L[0]. lbytes=%d",
-                               tb->lbytes);
-                       RFALSE( pos_in_item != ih_item_len(B_N_PITEM_HEAD(tbS0, item_pos)),
-                                "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
-                               ih_item_len(B_N_PITEM_HEAD(tbS0,item_pos)), pos_in_item);
-
-                       if ( tb->lbytes >= pos_in_item ) {
-                           /* appended item will be in L[0] in whole */
-                           int l_n;
-
-                           /* this bytes number must be appended to the last item of L[h] */
-                           l_n = tb->lbytes - pos_in_item;
-
-                           /* Calculate new insert_size[0] */
-                           tb->insert_size[0] -= l_n;
-
-                           RFALSE( tb->insert_size[0] <= 0,
-                                   "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
-                                   tb->insert_size[0]);
-                           ret_val =  leaf_shift_left(tb,tb->lnum[0], 
-                                                      ih_item_len(B_N_PITEM_HEAD(tbS0,item_pos)));
-                           /* Append to body of item in L[0] */
-                           bi.tb = tb;
-                           bi.bi_bh = tb->L[0];
-                           bi.bi_parent = tb->FL[0];
-                           bi.bi_position = get_left_neighbor_position (tb, 0);
-                           leaf_paste_in_buffer(
-                               &bi,n + item_pos - ret_val,
-                               ih_item_len( B_N_PITEM_HEAD(tb->L[0],n+item_pos-ret_val)),
-                               l_n,body, zeros_num > l_n ? l_n : zeros_num
-                               );
-                           /* 0-th item in S0 can be only of DIRECT type when l_n != 0*/
-                           {
-                               int version;
-                               int temp_l = l_n;
-                               
-                               RFALSE (ih_item_len (B_N_PITEM_HEAD (tbS0, 0)),
-                                       "PAP-12106: item length must be 0");
-                               RFALSE (comp_short_le_keys (B_N_PKEY (tbS0, 0),
-                                                           B_N_PKEY (tb->L[0],
-                                                                           n + item_pos - ret_val)),
-                                       "PAP-12107: items must be of the same file");
-                               if (is_indirect_le_ih(B_N_PITEM_HEAD (tb->L[0],
-                                                                     n + item_pos - ret_val))) {
-                                   temp_l = l_n << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT);
+       struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
+       int item_pos = PATH_LAST_POSITION(tb->tb_path); /*  index into the array of item headers in S[0] 
+                                                          of the affected item */
+       struct buffer_info bi;
+       struct buffer_head *S_new[2];   /* new nodes allocated to hold what could not fit into S */
+       int snum[2];            /* number of items that will be placed
+                                  into S_new (includes partially shifted
+                                  items) */
+       int sbytes[2];          /* if an item is partially shifted into S_new then 
+                                  if it is a directory item 
+                                  it is the number of entries from the item that are shifted into S_new
+                                  else
+                                  it is the number of bytes from the item that are shifted into S_new
+                                */
+       int n, i;
+       int ret_val;
+       int pos_in_item;
+       int zeros_num;
+
+       PROC_INFO_INC(tb->tb_sb, balance_at[0]);
+
+       /* Make balance in case insert_size[0] < 0 */
+       if (tb->insert_size[0] < 0)
+               return balance_leaf_when_delete(tb, flag);
+
+       zeros_num = 0;
+       if (flag == M_INSERT && body == 0)
+               zeros_num = ih_item_len(ih);
+
+       pos_in_item = tb->tb_path->pos_in_item;
+       /* for indirect item pos_in_item is measured in unformatted node
+          pointers. Recalculate to bytes */
+       if (flag != M_INSERT
+           && is_indirect_le_ih(B_N_PITEM_HEAD(tbS0, item_pos)))
+               pos_in_item *= UNFM_P_SIZE;
+
+       if (tb->lnum[0] > 0) {
+               /* Shift lnum[0] items from S[0] to the left neighbor L[0] */
+               if (item_pos < tb->lnum[0]) {
+                       /* new item or it part falls to L[0], shift it too */
+                       n = B_NR_ITEMS(tb->L[0]);
+
+                       switch (flag) {
+                       case M_INSERT:  /* insert item into L[0] */
+
+                               if (item_pos == tb->lnum[0] - 1
+                                   && tb->lbytes != -1) {
+                                       /* part of new item falls into L[0] */
+                                       int new_item_len;
+                                       int version;
+
+                                       ret_val =
+                                           leaf_shift_left(tb, tb->lnum[0] - 1,
+                                                           -1);
+
+                                       /* Calculate item length to insert to S[0] */
+                                       new_item_len =
+                                           ih_item_len(ih) - tb->lbytes;
+                                       /* Calculate and check item length to insert to L[0] */
+                                       put_ih_item_len(ih,
+                                                       ih_item_len(ih) -
+                                                       new_item_len);
+
+                                       RFALSE(ih_item_len(ih) <= 0,
+                                              "PAP-12080: there is nothing to insert into L[0]: ih_item_len=%d",
+                                              ih_item_len(ih));
+
+                                       /* Insert new item into L[0] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = tb->L[0];
+                                       bi.bi_parent = tb->FL[0];
+                                       bi.bi_position =
+                                           get_left_neighbor_position(tb, 0);
+                                       leaf_insert_into_buf(&bi,
+                                                            n + item_pos -
+                                                            ret_val, ih, body,
+                                                            zeros_num >
+                                                            ih_item_len(ih) ?
+                                                            ih_item_len(ih) :
+                                                            zeros_num);
+
+                                       version = ih_version(ih);
+
+                                       /* Calculate key component, item length and body to insert into S[0] */
+                                       set_le_ih_k_offset(ih,
+                                                          le_ih_k_offset(ih) +
+                                                          (tb->
+                                                           lbytes <<
+                                                           (is_indirect_le_ih
+                                                            (ih) ? tb->tb_sb->
+                                                            s_blocksize_bits -
+                                                            UNFM_P_SHIFT :
+                                                            0)));
+
+                                       put_ih_item_len(ih, new_item_len);
+                                       if (tb->lbytes > zeros_num) {
+                                               body +=
+                                                   (tb->lbytes - zeros_num);
+                                               zeros_num = 0;
+                                       } else
+                                               zeros_num -= tb->lbytes;
+
+                                       RFALSE(ih_item_len(ih) <= 0,
+                                              "PAP-12085: there is nothing to insert into S[0]: ih_item_len=%d",
+                                              ih_item_len(ih));
+                               } else {
+                                       /* new item in whole falls into L[0] */
+                                       /* Shift lnum[0]-1 items to L[0] */
+                                       ret_val =
+                                           leaf_shift_left(tb, tb->lnum[0] - 1,
+                                                           tb->lbytes);
+                                       /* Insert new item into L[0] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = tb->L[0];
+                                       bi.bi_parent = tb->FL[0];
+                                       bi.bi_position =
+                                           get_left_neighbor_position(tb, 0);
+                                       leaf_insert_into_buf(&bi,
+                                                            n + item_pos -
+                                                            ret_val, ih, body,
+                                                            zeros_num);
+                                       tb->insert_size[0] = 0;
+                                       zeros_num = 0;
                                }
-                               /* update key of first item in S0 */
-                               version = ih_version (B_N_PITEM_HEAD (tbS0, 0));
-                               set_le_key_k_offset (version, B_N_PKEY (tbS0, 0), 
-                                                    le_key_k_offset (version, B_N_PKEY (tbS0, 0)) + temp_l);
-                               /* update left delimiting key */
-                               set_le_key_k_offset (version, B_N_PDELIM_KEY(tb->CFL[0],tb->lkey[0]),
-                                                    le_key_k_offset (version, B_N_PDELIM_KEY(tb->CFL[0],tb->lkey[0])) + temp_l);
-                           }
-
-                           /* Calculate new body, position in item and insert_size[0] */
-                           if ( l_n > zeros_num ) {
-                               body += (l_n - zeros_num);
-                               zeros_num = 0;
-                           }
-                           else
-                               zeros_num -= l_n;
-                           pos_in_item = 0;    
-
-                           RFALSE( comp_short_le_keys 
-                                   (B_N_PKEY(tbS0,0),
-                                    B_N_PKEY(tb->L[0],B_NR_ITEMS(tb->L[0])-1)) ||
-                               
-                                   !op_is_left_mergeable 
-                                   (B_N_PKEY (tbS0, 0), tbS0->b_size) ||
-                                   !op_is_left_mergeable
-                                   (B_N_PDELIM_KEY(tb->CFL[0],tb->lkey[0]), 
-                                    tbS0->b_size),
-                                   "PAP-12120: item must be merge-able with left neighboring item");
-                       }
-                       else /* only part of the appended item will be in L[0] */
-                       {
-                           /* Calculate position in item for append in S[0] */
-                           pos_in_item -= tb->lbytes;
-
-                           RFALSE( pos_in_item <= 0,
-                                   "PAP-12125: no place for paste. pos_in_item=%d", pos_in_item);
-
-                           /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
-                           leaf_shift_left(tb,tb->lnum[0],tb->lbytes);
-                       }
-                   }
-               }
-               else /* appended item will be in L[0] in whole */
-               {
-                   struct item_head * pasted;
-
-                       if ( ! item_pos  && op_is_left_mergeable (B_N_PKEY (tbS0, 0), tbS0->b_size) )
-                       { /* if we paste into first item of S[0] and it is left mergable */
-                           /* then increment pos_in_item by the size of the last item in L[0] */
-                           pasted = B_N_PITEM_HEAD(tb->L[0],n-1);
-                           if ( is_direntry_le_ih (pasted) )
-                               pos_in_item += ih_entry_count(pasted);
-                           else
-                               pos_in_item += ih_item_len(pasted);
+                               break;
+
+                       case M_PASTE:   /* append item in L[0] */
+
+                               if (item_pos == tb->lnum[0] - 1
+                                   && tb->lbytes != -1) {
+                                       /* we must shift the part of the appended item */
+                                       if (is_direntry_le_ih
+                                           (B_N_PITEM_HEAD(tbS0, item_pos))) {
+
+                                               RFALSE(zeros_num,
+                                                      "PAP-12090: invalid parameter in case of a directory");
+                                               /* directory item */
+                                               if (tb->lbytes > pos_in_item) {
+                                                       /* new directory entry falls into L[0] */
+                                                       struct item_head
+                                                           *pasted;
+                                                       int l_pos_in_item =
+                                                           pos_in_item;
+
+                                                       /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 entries from given directory item */
+                                                       ret_val =
+                                                           leaf_shift_left(tb,
+                                                                           tb->
+                                                                           lnum
+                                                                           [0],
+                                                                           tb->
+                                                                           lbytes
+                                                                           -
+                                                                           1);
+                                                       if (ret_val
+                                                           && !item_pos) {
+                                                               pasted =
+                                                                   B_N_PITEM_HEAD
+                                                                   (tb->L[0],
+                                                                    B_NR_ITEMS
+                                                                    (tb->
+                                                                     L[0]) -
+                                                                    1);
+                                                               l_pos_in_item +=
+                                                                   I_ENTRY_COUNT
+                                                                   (pasted) -
+                                                                   (tb->
+                                                                    lbytes -
+                                                                    1);
+                                                       }
+
+                                                       /* Append given directory entry to directory item */
+                                                       bi.tb = tb;
+                                                       bi.bi_bh = tb->L[0];
+                                                       bi.bi_parent =
+                                                           tb->FL[0];
+                                                       bi.bi_position =
+                                                           get_left_neighbor_position
+                                                           (tb, 0);
+                                                       leaf_paste_in_buffer
+                                                           (&bi,
+                                                            n + item_pos -
+                                                            ret_val,
+                                                            l_pos_in_item,
+                                                            tb->insert_size[0],
+                                                            body, zeros_num);
+
+                                                       /* previous string prepared space for pasting new entry, following string pastes this entry */
+
+                                                       /* when we have merge directory item, pos_in_item has been changed too */
+
+                                                       /* paste new directory entry. 1 is entry number */
+                                                       leaf_paste_entries(bi.
+                                                                          bi_bh,
+                                                                          n +
+                                                                          item_pos
+                                                                          -
+                                                                          ret_val,
+                                                                          l_pos_in_item,
+                                                                          1,
+                                                                          (struct
+                                                                           reiserfs_de_head
+                                                                           *)
+                                                                          body,
+                                                                          body
+                                                                          +
+                                                                          DEH_SIZE,
+                                                                          tb->
+                                                                          insert_size
+                                                                          [0]
+                                                           );
+                                                       tb->insert_size[0] = 0;
+                                               } else {
+                                                       /* new directory item doesn't fall into L[0] */
+                                                       /* Shift lnum[0]-1 items in whole. Shift lbytes directory entries from directory item number lnum[0] */
+                                                       leaf_shift_left(tb,
+                                                                       tb->
+                                                                       lnum[0],
+                                                                       tb->
+                                                                       lbytes);
+                                               }
+                                               /* Calculate new position to append in item body */
+                                               pos_in_item -= tb->lbytes;
+                                       } else {
+                                               /* regular object */
+                                               RFALSE(tb->lbytes <= 0,
+                                                      "PAP-12095: there is nothing to shift to L[0]. lbytes=%d",
+                                                      tb->lbytes);
+                                               RFALSE(pos_in_item !=
+                                                      ih_item_len
+                                                      (B_N_PITEM_HEAD
+                                                       (tbS0, item_pos)),
+                                                      "PAP-12100: incorrect position to paste: item_len=%d, pos_in_item=%d",
+                                                      ih_item_len
+                                                      (B_N_PITEM_HEAD
+                                                       (tbS0, item_pos)),
+                                                      pos_in_item);
+
+                                               if (tb->lbytes >= pos_in_item) {
+                                                       /* appended item will be in L[0] in whole */
+                                                       int l_n;
+
+                                                       /* this bytes number must be appended to the last item of L[h] */
+                                                       l_n =
+                                                           tb->lbytes -
+                                                           pos_in_item;
+
+                                                       /* Calculate new insert_size[0] */
+                                                       tb->insert_size[0] -=
+                                                           l_n;
+
+                                                       RFALSE(tb->
+                                                              insert_size[0] <=
+                                                              0,
+                                                              "PAP-12105: there is nothing to paste into L[0]. insert_size=%d",
+                                                              tb->
+                                                              insert_size[0]);
+                                                       ret_val =
+                                                           leaf_shift_left(tb,
+                                                                           tb->
+                                                                           lnum
+                                                                           [0],
+                                                                           ih_item_len
+                                                                           (B_N_PITEM_HEAD
+                                                                            (tbS0,
+                                                                             item_pos)));
+                                                       /* Append to body of item in L[0] */
+                                                       bi.tb = tb;
+                                                       bi.bi_bh = tb->L[0];
+                                                       bi.bi_parent =
+                                                           tb->FL[0];
+                                                       bi.bi_position =
+                                                           get_left_neighbor_position
+                                                           (tb, 0);
+                                                       leaf_paste_in_buffer
+                                                           (&bi,
+                                                            n + item_pos -
+                                                            ret_val,
+                                                            ih_item_len
+                                                            (B_N_PITEM_HEAD
+                                                             (tb->L[0],
+                                                              n + item_pos -
+                                                              ret_val)), l_n,
+                                                            body,
+                                                            zeros_num >
+                                                            l_n ? l_n :
+                                                            zeros_num);
+                                                       /* 0-th item in S0 can be only of DIRECT type when l_n != 0 */
+                                                       {
+                                                               int version;
+                                                               int temp_l =
+                                                                   l_n;
+
+                                                               RFALSE
+                                                                   (ih_item_len
+                                                                    (B_N_PITEM_HEAD
+                                                                     (tbS0,
+                                                                      0)),
+                                                                    "PAP-12106: item length must be 0");
+                                                               RFALSE
+                                                                   (comp_short_le_keys
+                                                                    (B_N_PKEY
+                                                                     (tbS0, 0),
+                                                                     B_N_PKEY
+                                                                     (tb->L[0],
+                                                                      n +
+                                                                      item_pos
+                                                                      -
+                                                                      ret_val)),
+                                                                    "PAP-12107: items must be of the same file");
+                                                               if (is_indirect_le_ih(B_N_PITEM_HEAD(tb->L[0], n + item_pos - ret_val))) {
+                                                                       temp_l =
+                                                                           l_n
+                                                                           <<
+                                                                           (tb->
+                                                                            tb_sb->
+                                                                            s_blocksize_bits
+                                                                            -
+                                                                            UNFM_P_SHIFT);
+                                                               }
+                                                               /* update key of first item in S0 */
+                                                               version =
+                                                                   ih_version
+                                                                   (B_N_PITEM_HEAD
+                                                                    (tbS0, 0));
+                                                               set_le_key_k_offset
+                                                                   (version,
+                                                                    B_N_PKEY
+                                                                    (tbS0, 0),
+                                                                    le_key_k_offset
+                                                                    (version,
+                                                                     B_N_PKEY
+                                                                     (tbS0,
+                                                                      0)) +
+                                                                    temp_l);
+                                                               /* update left delimiting key */
+                                                               set_le_key_k_offset
+                                                                   (version,
+                                                                    B_N_PDELIM_KEY
+                                                                    (tb->
+                                                                     CFL[0],
+                                                                     tb->
+                                                                     lkey[0]),
+                                                                    le_key_k_offset
+                                                                    (version,
+                                                                     B_N_PDELIM_KEY
+                                                                     (tb->
+                                                                      CFL[0],
+                                                                      tb->
+                                                                      lkey[0]))
+                                                                    + temp_l);
+                                                       }
+
+                                                       /* Calculate new body, position in item and insert_size[0] */
+                                                       if (l_n > zeros_num) {
+                                                               body +=
+                                                                   (l_n -
+                                                                    zeros_num);
+                                                               zeros_num = 0;
+                                                       } else
+                                                               zeros_num -=
+                                                                   l_n;
+                                                       pos_in_item = 0;
+
+                                                       RFALSE
+                                                           (comp_short_le_keys
+                                                            (B_N_PKEY(tbS0, 0),
+                                                             B_N_PKEY(tb->L[0],
+                                                                      B_NR_ITEMS
+                                                                      (tb->
+                                                                       L[0]) -
+                                                                      1))
+                                                            ||
+                                                            !op_is_left_mergeable
+                                                            (B_N_PKEY(tbS0, 0),
+                                                             tbS0->b_size)
+                                                            ||
+                                                            !op_is_left_mergeable
+                                                            (B_N_PDELIM_KEY
+                                                             (tb->CFL[0],
+                                                              tb->lkey[0]),
+                                                             tbS0->b_size),
+                                                            "PAP-12120: item must be merge-able with left neighboring item");
+                                               } else {        /* only part of the appended item will be in L[0] */
+
+                                                       /* Calculate position in item for append in S[0] */
+                                                       pos_in_item -=
+                                                           tb->lbytes;
+
+                                                       RFALSE(pos_in_item <= 0,
+                                                              "PAP-12125: no place for paste. pos_in_item=%d",
+                                                              pos_in_item);
+
+                                                       /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
+                                                       leaf_shift_left(tb,
+                                                                       tb->
+                                                                       lnum[0],
+                                                                       tb->
+                                                                       lbytes);
+                                               }
+                                       }
+                               } else {        /* appended item will be in L[0] in whole */
+
+                                       struct item_head *pasted;
+
+                                       if (!item_pos && op_is_left_mergeable(B_N_PKEY(tbS0, 0), tbS0->b_size)) {       /* if we paste into first item of S[0] and it is left mergable */
+                                               /* then increment pos_in_item by the size of the last item in L[0] */
+                                               pasted =
+                                                   B_N_PITEM_HEAD(tb->L[0],
+                                                                  n - 1);
+                                               if (is_direntry_le_ih(pasted))
+                                                       pos_in_item +=
+                                                           ih_entry_count
+                                                           (pasted);
+                                               else
+                                                       pos_in_item +=
+                                                           ih_item_len(pasted);
+                                       }
+
+                                       /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
+                                       ret_val =
+                                           leaf_shift_left(tb, tb->lnum[0],
+                                                           tb->lbytes);
+                                       /* Append to body of item in L[0] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = tb->L[0];
+                                       bi.bi_parent = tb->FL[0];
+                                       bi.bi_position =
+                                           get_left_neighbor_position(tb, 0);
+                                       leaf_paste_in_buffer(&bi,
+                                                            n + item_pos -
+                                                            ret_val,
+                                                            pos_in_item,
+                                                            tb->insert_size[0],
+                                                            body, zeros_num);
+
+                                       /* if appended item is directory, paste entry */
+                                       pasted =
+                                           B_N_PITEM_HEAD(tb->L[0],
+                                                          n + item_pos -
+                                                          ret_val);
+                                       if (is_direntry_le_ih(pasted))
+                                               leaf_paste_entries(bi.bi_bh,
+                                                                  n +
+                                                                  item_pos -
+                                                                  ret_val,
+                                                                  pos_in_item,
+                                                                  1,
+                                                                  (struct
+                                                                   reiserfs_de_head
+                                                                   *)body,
+                                                                  body +
+                                                                  DEH_SIZE,
+                                                                  tb->
+                                                                  insert_size
+                                                                  [0]
+                                                   );
+                                       /* if appended item is indirect item, put unformatted node into un list */
+                                       if (is_indirect_le_ih(pasted))
+                                               set_ih_free_space(pasted, 0);
+                                       tb->insert_size[0] = 0;
+                                       zeros_num = 0;
+                               }
+                               break;
+                       default:        /* cases d and t */
+                               reiserfs_panic(tb->tb_sb,
+                                              "PAP-12130: balance_leaf: lnum > 0: unexpectable mode: %s(%d)",
+                                              (flag ==
+                                               M_DELETE) ? "DELETE" : ((flag ==
+                                                                        M_CUT)
+                                                                       ? "CUT"
+                                                                       :
+                                                                       "UNKNOWN"),
+                                              flag);
                        }
-
-                   /* Shift lnum[0] - 1 items in whole. Shift lbytes - 1 byte from item number lnum[0] */
-                   ret_val = leaf_shift_left(tb,tb->lnum[0],tb->lbytes);
-                   /* Append to body of item in L[0] */
-                   bi.tb = tb;
-                   bi.bi_bh = tb->L[0];
-                   bi.bi_parent = tb->FL[0];
-                   bi.bi_position = get_left_neighbor_position (tb, 0);
-                   leaf_paste_in_buffer (&bi, n + item_pos - ret_val, pos_in_item, tb->insert_size[0],
-                                         body, zeros_num);
-
-                   /* if appended item is directory, paste entry */
-                   pasted = B_N_PITEM_HEAD (tb->L[0], n + item_pos - ret_val);
-                   if (is_direntry_le_ih (pasted))
-                       leaf_paste_entries (
-                           bi.bi_bh, n + item_pos - ret_val, pos_in_item, 1, 
-                           (struct reiserfs_de_head *)body, body + DEH_SIZE, tb->insert_size[0]
-                           );
-                   /* if appended item is indirect item, put unformatted node into un list */
-                   if (is_indirect_le_ih (pasted))
-                       set_ih_free_space (pasted, 0);
-                   tb->insert_size[0] = 0;
-                   zeros_num = 0;
+               } else {
+                       /* new item doesn't fall into L[0] */
+                       leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
                }
-               break;
-           default:    /* cases d and t */
-               reiserfs_panic (tb->tb_sb, "PAP-12130: balance_leaf: lnum > 0: unexpectable mode: %s(%d)",
-                               (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
-           }
-       } else { 
-           /* new item doesn't fall into L[0] */
-           leaf_shift_left(tb,tb->lnum[0],tb->lbytes);
        }
-    }  /* tb->lnum[0] > 0 */
 
-    /* Calculate new item position */
-    item_pos -= ( tb->lnum[0] - (( tb->lbytes != -1 ) ? 1 : 0));
-
-    if ( tb->rnum[0] > 0 ) {
-       /* shift rnum[0] items from S[0] to the right neighbor R[0] */
-       n = B_NR_ITEMS(tbS0);
-       switch ( flag ) {
-
-       case M_INSERT:   /* insert item */
-           if ( n - tb->rnum[0] < item_pos )
-           { /* new item or its part falls to R[0] */
-               if ( item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1 )
-               { /* part of new item falls into R[0] */
-                   loff_t old_key_comp, old_len, r_zeros_number;
-                   const char * r_body;
-                   int version;
-                   loff_t offset;
-
-                   leaf_shift_right(tb,tb->rnum[0]-1,-1);
-
-                   version = ih_version(ih);
-                   /* Remember key component and item length */
-                    old_key_comp = le_ih_k_offset( ih );
-                   old_len = ih_item_len(ih);
-
-                   /* Calculate key component and item length to insert into R[0] */
-                    offset = le_ih_k_offset( ih ) + ((old_len - tb->rbytes )<<(is_indirect_le_ih(ih)?tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT:0));
-                    set_le_ih_k_offset( ih, offset );
-                   put_ih_item_len( ih, tb->rbytes);
-                   /* Insert part of the item into R[0] */
-                   bi.tb = tb;
-                   bi.bi_bh = tb->R[0];
-                   bi.bi_parent = tb->FR[0];
-                   bi.bi_position = get_right_neighbor_position (tb, 0);
-                   if ( (old_len - tb->rbytes) > zeros_num ) {
-                       r_zeros_number = 0;
-                       r_body = body + (old_len - tb->rbytes) - zeros_num;
-                   }
-                   else {
-                       r_body = body;
-                       r_zeros_number = zeros_num - (old_len - tb->rbytes);
-                       zeros_num -= r_zeros_number;
-                   }
-
-                   leaf_insert_into_buf (&bi, 0, ih, r_body, r_zeros_number);
-
-                   /* Replace right delimiting key by first key in R[0] */
-                   replace_key(tb, tb->CFR[0],tb->rkey[0],tb->R[0],0);
-
-                   /* Calculate key component and item length to insert into S[0] */
-                    set_le_ih_k_offset( ih, old_key_comp );
-                   put_ih_item_len( ih, old_len - tb->rbytes );
-
-                   tb->insert_size[0] -= tb->rbytes;
+       /* tb->lnum[0] > 0 */
+       /* Calculate new item position */
+       item_pos -= (tb->lnum[0] - ((tb->lbytes != -1) ? 1 : 0));
+
+       if (tb->rnum[0] > 0) {
+               /* shift rnum[0] items from S[0] to the right neighbor R[0] */
+               n = B_NR_ITEMS(tbS0);
+               switch (flag) {
+
+               case M_INSERT:  /* insert item */
+                       if (n - tb->rnum[0] < item_pos) {       /* new item or its part falls to R[0] */
+                               if (item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {      /* part of new item falls into R[0] */
+                                       loff_t old_key_comp, old_len,
+                                           r_zeros_number;
+                                       const char *r_body;
+                                       int version;
+                                       loff_t offset;
+
+                                       leaf_shift_right(tb, tb->rnum[0] - 1,
+                                                        -1);
+
+                                       version = ih_version(ih);
+                                       /* Remember key component and item length */
+                                       old_key_comp = le_ih_k_offset(ih);
+                                       old_len = ih_item_len(ih);
+
+                                       /* Calculate key component and item length to insert into R[0] */
+                                       offset =
+                                           le_ih_k_offset(ih) +
+                                           ((old_len -
+                                             tb->
+                                             rbytes) << (is_indirect_le_ih(ih)
+                                                         ? tb->tb_sb->
+                                                         s_blocksize_bits -
+                                                         UNFM_P_SHIFT : 0));
+                                       set_le_ih_k_offset(ih, offset);
+                                       put_ih_item_len(ih, tb->rbytes);
+                                       /* Insert part of the item into R[0] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = tb->R[0];
+                                       bi.bi_parent = tb->FR[0];
+                                       bi.bi_position =
+                                           get_right_neighbor_position(tb, 0);
+                                       if ((old_len - tb->rbytes) > zeros_num) {
+                                               r_zeros_number = 0;
+                                               r_body =
+                                                   body + (old_len -
+                                                           tb->rbytes) -
+                                                   zeros_num;
+                                       } else {
+                                               r_body = body;
+                                               r_zeros_number =
+                                                   zeros_num - (old_len -
+                                                                tb->rbytes);
+                                               zeros_num -= r_zeros_number;
+                                       }
+
+                                       leaf_insert_into_buf(&bi, 0, ih, r_body,
+                                                            r_zeros_number);
+
+                                       /* Replace right delimiting key by first key in R[0] */
+                                       replace_key(tb, tb->CFR[0], tb->rkey[0],
+                                                   tb->R[0], 0);
+
+                                       /* Calculate key component and item length to insert into S[0] */
+                                       set_le_ih_k_offset(ih, old_key_comp);
+                                       put_ih_item_len(ih,
+                                                       old_len - tb->rbytes);
+
+                                       tb->insert_size[0] -= tb->rbytes;
+
+                               } else {        /* whole new item falls into R[0] */
+
+                                       /* Shift rnum[0]-1 items to R[0] */
+                                       ret_val =
+                                           leaf_shift_right(tb,
+                                                            tb->rnum[0] - 1,
+                                                            tb->rbytes);
+                                       /* Insert new item into R[0] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = tb->R[0];
+                                       bi.bi_parent = tb->FR[0];
+                                       bi.bi_position =
+                                           get_right_neighbor_position(tb, 0);
+                                       leaf_insert_into_buf(&bi,
+                                                            item_pos - n +
+                                                            tb->rnum[0] - 1,
+                                                            ih, body,
+                                                            zeros_num);
+
+                                       if (item_pos - n + tb->rnum[0] - 1 == 0) {
+                                               replace_key(tb, tb->CFR[0],
+                                                           tb->rkey[0],
+                                                           tb->R[0], 0);
+
+                                       }
+                                       zeros_num = tb->insert_size[0] = 0;
+                               }
+                       } else {        /* new item or part of it doesn't fall into R[0] */
 
-               }
-               else /* whole new item falls into R[0] */
-               {                                         
-                   /* Shift rnum[0]-1 items to R[0] */
-                   ret_val = leaf_shift_right(tb,tb->rnum[0]-1,tb->rbytes);
-                   /* Insert new item into R[0] */
-                   bi.tb = tb;
-                   bi.bi_bh = tb->R[0];
-                   bi.bi_parent = tb->FR[0];
-                   bi.bi_position = get_right_neighbor_position (tb, 0);
-                   leaf_insert_into_buf (&bi, item_pos - n + tb->rnum[0] - 1, ih, body, zeros_num);
-
-                   if ( item_pos - n + tb->rnum[0] - 1 == 0 ) {
-                       replace_key(tb, tb->CFR[0],tb->rkey[0],tb->R[0],0);
-
-                   }
-                   zeros_num = tb->insert_size[0] = 0;
-               }
-           }
-           else /* new item or part of it doesn't fall into R[0] */
-           {
-               leaf_shift_right(tb,tb->rnum[0],tb->rbytes);
-           }
-           break;
-
-       case M_PASTE:   /* append item */
-
-           if ( n - tb->rnum[0] <= item_pos )  /* pasted item or part of it falls to R[0] */
-           {
-               if ( item_pos == n - tb->rnum[0] && tb->rbytes != -1 )
-               { /* we must shift the part of the appended item */
-                   if ( is_direntry_le_ih (B_N_PITEM_HEAD(tbS0, item_pos)))
-                   { /* we append to directory item */
-                       int entry_count;
-
-                       RFALSE( zeros_num,
-                               "PAP-12145: invalid parameter in case of a directory");
-                       entry_count = I_ENTRY_COUNT(B_N_PITEM_HEAD(tbS0, item_pos));
-                       if ( entry_count - tb->rbytes < pos_in_item )
-                           /* new directory entry falls into R[0] */
-                       {
-                           int paste_entry_position;
-
-                           RFALSE( tb->rbytes - 1 >= entry_count || 
-                                   ! tb->insert_size[0],
-                                   "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
-                                   tb->rbytes, entry_count);
-                           /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
-                           leaf_shift_right(tb,tb->rnum[0],tb->rbytes - 1);
-                           /* Paste given directory entry to directory item */
-                           paste_entry_position = pos_in_item - entry_count + tb->rbytes - 1;
-                           bi.tb = tb;
-                           bi.bi_bh = tb->R[0];
-                           bi.bi_parent = tb->FR[0];
-                           bi.bi_position = get_right_neighbor_position (tb, 0);
-                           leaf_paste_in_buffer (&bi, 0, paste_entry_position,
-                                                 tb->insert_size[0],body,zeros_num);
-                           /* paste entry */
-                           leaf_paste_entries (
-                               bi.bi_bh, 0, paste_entry_position, 1, (struct reiserfs_de_head *)body, 
-                               body + DEH_SIZE, tb->insert_size[0]
-                               );                                                              
-                                               
-                           if ( paste_entry_position == 0 ) {
-                               /* change delimiting keys */
-                               replace_key(tb, tb->CFR[0],tb->rkey[0],tb->R[0],0);
-                           }
-
-                           tb->insert_size[0] = 0;
-                           pos_in_item++;
-                       }
-                       else /* new directory entry doesn't fall into R[0] */
-                       {
-                           leaf_shift_right(tb,tb->rnum[0],tb->rbytes);
-                       }
-                   }
-                   else /* regular object */
-                   {
-                       int n_shift, n_rem, r_zeros_number;
-                       const char * r_body;
-
-                       /* Calculate number of bytes which must be shifted from appended item */
-                       if ( (n_shift = tb->rbytes - tb->insert_size[0]) < 0 )
-                           n_shift = 0;
-
-                       RFALSE(pos_in_item != ih_item_len(B_N_PITEM_HEAD (tbS0, item_pos)),
-                              "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
-                               pos_in_item, ih_item_len( B_N_PITEM_HEAD(tbS0,item_pos)));
-
-                       leaf_shift_right(tb,tb->rnum[0],n_shift);
-                       /* Calculate number of bytes which must remain in body after appending to R[0] */
-                       if ( (n_rem = tb->insert_size[0] - tb->rbytes) < 0 )
-                           n_rem = 0;
-                       
-                       {
-                         int version;
-                         unsigned long temp_rem = n_rem;
-                         
-                         version = ih_version (B_N_PITEM_HEAD (tb->R[0],0));
-                         if (is_indirect_le_key(version,B_N_PKEY(tb->R[0],0))){
-                             temp_rem = n_rem << (tb->tb_sb->s_blocksize_bits -
-                                        UNFM_P_SHIFT);
-                         }
-                         set_le_key_k_offset (version, B_N_PKEY(tb->R[0],0), 
-                                              le_key_k_offset (version, B_N_PKEY(tb->R[0],0)) + temp_rem);
-                         set_le_key_k_offset (version, B_N_PDELIM_KEY(tb->CFR[0],tb->rkey[0]), 
-                                              le_key_k_offset (version, B_N_PDELIM_KEY(tb->CFR[0],tb->rkey[0])) + temp_rem);
+                               leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
                        }
+                       break;
+
+               case M_PASTE:   /* append item */
+
+                       if (n - tb->rnum[0] <= item_pos) {      /* pasted item or part of it falls to R[0] */
+                               if (item_pos == n - tb->rnum[0] && tb->rbytes != -1) {  /* we must shift the part of the appended item */
+                                       if (is_direntry_le_ih(B_N_PITEM_HEAD(tbS0, item_pos))) {        /* we append to directory item */
+                                               int entry_count;
+
+                                               RFALSE(zeros_num,
+                                                      "PAP-12145: invalid parameter in case of a directory");
+                                               entry_count =
+                                                   I_ENTRY_COUNT(B_N_PITEM_HEAD
+                                                                 (tbS0,
+                                                                  item_pos));
+                                               if (entry_count - tb->rbytes <
+                                                   pos_in_item)
+                                                       /* new directory entry falls into R[0] */
+                                               {
+                                                       int paste_entry_position;
+
+                                                       RFALSE(tb->rbytes - 1 >=
+                                                              entry_count
+                                                              || !tb->
+                                                              insert_size[0],
+                                                              "PAP-12150: no enough of entries to shift to R[0]: rbytes=%d, entry_count=%d",
+                                                              tb->rbytes,
+                                                              entry_count);
+                                                       /* Shift rnum[0]-1 items in whole. Shift rbytes-1 directory entries from directory item number rnum[0] */
+                                                       leaf_shift_right(tb,
+                                                                        tb->
+                                                                        rnum
+                                                                        [0],
+                                                                        tb->
+                                                                        rbytes
+                                                                        - 1);
+                                                       /* Paste given directory entry to directory item */
+                                                       paste_entry_position =
+                                                           pos_in_item -
+                                                           entry_count +
+                                                           tb->rbytes - 1;
+                                                       bi.tb = tb;
+                                                       bi.bi_bh = tb->R[0];
+                                                       bi.bi_parent =
+                                                           tb->FR[0];
+                                                       bi.bi_position =
+                                                           get_right_neighbor_position
+                                                           (tb, 0);
+                                                       leaf_paste_in_buffer
+                                                           (&bi, 0,
+                                                            paste_entry_position,
+                                                            tb->insert_size[0],
+                                                            body, zeros_num);
+                                                       /* paste entry */
+                                                       leaf_paste_entries(bi.
+                                                                          bi_bh,
+                                                                          0,
+                                                                          paste_entry_position,
+                                                                          1,
+                                                                          (struct
+                                                                           reiserfs_de_head
+                                                                           *)
+                                                                          body,
+                                                                          body
+                                                                          +
+                                                                          DEH_SIZE,
+                                                                          tb->
+                                                                          insert_size
+                                                                          [0]
+                                                           );
+
+                                                       if (paste_entry_position
+                                                           == 0) {
+                                                               /* change delimiting keys */
+                                                               replace_key(tb,
+                                                                           tb->
+                                                                           CFR
+                                                                           [0],
+                                                                           tb->
+                                                                           rkey
+                                                                           [0],
+                                                                           tb->
+                                                                           R
+                                                                           [0],
+                                                                           0);
+                                                       }
+
+                                                       tb->insert_size[0] = 0;
+                                                       pos_in_item++;
+                                               } else {        /* new directory entry doesn't fall into R[0] */
+
+                                                       leaf_shift_right(tb,
+                                                                        tb->
+                                                                        rnum
+                                                                        [0],
+                                                                        tb->
+                                                                        rbytes);
+                                               }
+                                       } else {        /* regular object */
+
+                                               int n_shift, n_rem,
+                                                   r_zeros_number;
+                                               const char *r_body;
+
+                                               /* Calculate number of bytes which must be shifted from appended item */
+                                               if ((n_shift =
+                                                    tb->rbytes -
+                                                    tb->insert_size[0]) < 0)
+                                                       n_shift = 0;
+
+                                               RFALSE(pos_in_item !=
+                                                      ih_item_len
+                                                      (B_N_PITEM_HEAD
+                                                       (tbS0, item_pos)),
+                                                      "PAP-12155: invalid position to paste. ih_item_len=%d, pos_in_item=%d",
+                                                      pos_in_item,
+                                                      ih_item_len
+                                                      (B_N_PITEM_HEAD
+                                                       (tbS0, item_pos)));
+
+                                               leaf_shift_right(tb,
+                                                                tb->rnum[0],
+                                                                n_shift);
+                                               /* Calculate number of bytes which must remain in body after appending to R[0] */
+                                               if ((n_rem =
+                                                    tb->insert_size[0] -
+                                                    tb->rbytes) < 0)
+                                                       n_rem = 0;
+
+                                               {
+                                                       int version;
+                                                       unsigned long temp_rem =
+                                                           n_rem;
+
+                                                       version =
+                                                           ih_version
+                                                           (B_N_PITEM_HEAD
+                                                            (tb->R[0], 0));
+                                                       if (is_indirect_le_key
+                                                           (version,
+                                                            B_N_PKEY(tb->R[0],
+                                                                     0))) {
+                                                               temp_rem =
+                                                                   n_rem <<
+                                                                   (tb->tb_sb->
+                                                                    s_blocksize_bits
+                                                                    -
+                                                                    UNFM_P_SHIFT);
+                                                       }
+                                                       set_le_key_k_offset
+                                                           (version,
+                                                            B_N_PKEY(tb->R[0],
+                                                                     0),
+                                                            le_key_k_offset
+                                                            (version,
+                                                             B_N_PKEY(tb->R[0],
+                                                                      0)) +
+                                                            temp_rem);
+                                                       set_le_key_k_offset
+                                                           (version,
+                                                            B_N_PDELIM_KEY(tb->
+                                                                           CFR
+                                                                           [0],
+                                                                           tb->
+                                                                           rkey
+                                                                           [0]),
+                                                            le_key_k_offset
+                                                            (version,
+                                                             B_N_PDELIM_KEY
+                                                             (tb->CFR[0],
+                                                              tb->rkey[0])) +
+                                                            temp_rem);
+                                               }
 /*               k_offset (B_N_PKEY(tb->R[0],0)) += n_rem;
                  k_offset (B_N_PDELIM_KEY(tb->CFR[0],tb->rkey[0])) += n_rem;*/
-                       do_balance_mark_internal_dirty (tb, tb->CFR[0], 0);
-
-                       /* Append part of body into R[0] */
-                       bi.tb = tb;
-                       bi.bi_bh = tb->R[0];
-                       bi.bi_parent = tb->FR[0];
-                       bi.bi_position = get_right_neighbor_position (tb, 0);
-                       if ( n_rem > zeros_num ) {
-                           r_zeros_number = 0;
-                           r_body = body + n_rem - zeros_num;
-                       }
-                       else {
-                           r_body = body;
-                           r_zeros_number = zeros_num - n_rem;
-                           zeros_num -= r_zeros_number;
-                       }
-
-                       leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem, r_body, r_zeros_number);
-
-                       if (is_indirect_le_ih (B_N_PITEM_HEAD(tb->R[0],0))) {
+                                               do_balance_mark_internal_dirty
+                                                   (tb, tb->CFR[0], 0);
+
+                                               /* Append part of body into R[0] */
+                                               bi.tb = tb;
+                                               bi.bi_bh = tb->R[0];
+                                               bi.bi_parent = tb->FR[0];
+                                               bi.bi_position =
+                                                   get_right_neighbor_position
+                                                   (tb, 0);
+                                               if (n_rem > zeros_num) {
+                                                       r_zeros_number = 0;
+                                                       r_body =
+                                                           body + n_rem -
+                                                           zeros_num;
+                                               } else {
+                                                       r_body = body;
+                                                       r_zeros_number =
+                                                           zeros_num - n_rem;
+                                                       zeros_num -=
+                                                           r_zeros_number;
+                                               }
+
+                                               leaf_paste_in_buffer(&bi, 0,
+                                                                    n_shift,
+                                                                    tb->
+                                                                    insert_size
+                                                                    [0] -
+                                                                    n_rem,
+                                                                    r_body,
+                                                                    r_zeros_number);
+
+                                               if (is_indirect_le_ih
+                                                   (B_N_PITEM_HEAD
+                                                    (tb->R[0], 0))) {
 #if 0
-                           RFALSE( n_rem,
-                                   "PAP-12160: paste more than one unformatted node pointer");
+                                                       RFALSE(n_rem,
+                                                              "PAP-12160: paste more than one unformatted node pointer");
 #endif
-                           set_ih_free_space (B_N_PITEM_HEAD(tb->R[0],0), 0);
-                       }
-                       tb->insert_size[0] = n_rem;
-                       if ( ! n_rem )
-                           pos_in_item ++;
-                   }
-               }
-               else /* pasted item in whole falls into R[0] */
-               {
-                   struct item_head * pasted;
+                                                       set_ih_free_space
+                                                           (B_N_PITEM_HEAD
+                                                            (tb->R[0], 0), 0);
+                                               }
+                                               tb->insert_size[0] = n_rem;
+                                               if (!n_rem)
+                                                       pos_in_item++;
+                                       }
+                               } else {        /* pasted item in whole falls into R[0] */
+
+                                       struct item_head *pasted;
+
+                                       ret_val =
+                                           leaf_shift_right(tb, tb->rnum[0],
+                                                            tb->rbytes);
+                                       /* append item in R[0] */
+                                       if (pos_in_item >= 0) {
+                                               bi.tb = tb;
+                                               bi.bi_bh = tb->R[0];
+                                               bi.bi_parent = tb->FR[0];
+                                               bi.bi_position =
+                                                   get_right_neighbor_position
+                                                   (tb, 0);
+                                               leaf_paste_in_buffer(&bi,
+                                                                    item_pos -
+                                                                    n +
+                                                                    tb->
+                                                                    rnum[0],
+                                                                    pos_in_item,
+                                                                    tb->
+                                                                    insert_size
+                                                                    [0], body,
+                                                                    zeros_num);
+                                       }
+
+                                       /* paste new entry, if item is directory item */
+                                       pasted =
+                                           B_N_PITEM_HEAD(tb->R[0],
+                                                          item_pos - n +
+                                                          tb->rnum[0]);
+                                       if (is_direntry_le_ih(pasted)
+                                           && pos_in_item >= 0) {
+                                               leaf_paste_entries(bi.bi_bh,
+                                                                  item_pos -
+                                                                  n +
+                                                                  tb->rnum[0],
+                                                                  pos_in_item,
+                                                                  1,
+                                                                  (struct
+                                                                   reiserfs_de_head
+                                                                   *)body,
+                                                                  body +
+                                                                  DEH_SIZE,
+                                                                  tb->
+                                                                  insert_size
+                                                                  [0]
+                                                   );
+                                               if (!pos_in_item) {
+
+                                                       RFALSE(item_pos - n +
+                                                              tb->rnum[0],
+                                                              "PAP-12165: directory item must be first item of node when pasting is in 0th position");
+
+                                                       /* update delimiting keys */
+                                                       replace_key(tb,
+                                                                   tb->CFR[0],
+                                                                   tb->rkey[0],
+                                                                   tb->R[0],
+                                                                   0);
+                                               }
+                                       }
+
+                                       if (is_indirect_le_ih(pasted))
+                                               set_ih_free_space(pasted, 0);
+                                       zeros_num = tb->insert_size[0] = 0;
+                               }
+                       } else {        /* new item doesn't fall into R[0] */
 
-                   ret_val = leaf_shift_right(tb,tb->rnum[0],tb->rbytes);
-                   /* append item in R[0] */
-                   if ( pos_in_item >= 0 ) {
-                       bi.tb = tb;
-                       bi.bi_bh = tb->R[0];
-                       bi.bi_parent = tb->FR[0];
-                       bi.bi_position = get_right_neighbor_position (tb, 0);
-                       leaf_paste_in_buffer(&bi,item_pos - n + tb->rnum[0], pos_in_item,
-                                            tb->insert_size[0],body, zeros_num);
-                   }
-
-                   /* paste new entry, if item is directory item */
-                   pasted = B_N_PITEM_HEAD(tb->R[0], item_pos - n + tb->rnum[0]);
-                   if (is_direntry_le_ih (pasted) && pos_in_item >= 0 ) {
-                       leaf_paste_entries (
-                           bi.bi_bh, item_pos - n + tb->rnum[0], pos_in_item, 1, 
-                           (struct reiserfs_de_head *)body, body + DEH_SIZE, tb->insert_size[0]
-                           );
-                       if ( ! pos_in_item ) {
-
-                           RFALSE( item_pos - n + tb->rnum[0],
-                                   "PAP-12165: directory item must be first item of node when pasting is in 0th position");
-
-                           /* update delimiting keys */
-                           replace_key(tb, tb->CFR[0],tb->rkey[0],tb->R[0],0);
+                               leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
                        }
-                   }
-
-                   if (is_indirect_le_ih (pasted))
-                       set_ih_free_space (pasted, 0);
-                   zeros_num = tb->insert_size[0] = 0;
+                       break;
+               default:        /* cases d and t */
+                       reiserfs_panic(tb->tb_sb,
+                                      "PAP-12175: balance_leaf: rnum > 0: unexpectable mode: %s(%d)",
+                                      (flag ==
+                                       M_DELETE) ? "DELETE" : ((flag ==
+                                                                M_CUT) ? "CUT"
+                                                               : "UNKNOWN"),
+                                      flag);
                }
-           }
-           else /* new item doesn't fall into R[0] */
-           {
-               leaf_shift_right(tb,tb->rnum[0],tb->rbytes);
-           }
-           break;
-       default:    /* cases d and t */
-           reiserfs_panic (tb->tb_sb, "PAP-12175: balance_leaf: rnum > 0: unexpectable mode: %s(%d)",
-                           (flag == M_DELETE) ? "DELETE" : ((flag == M_CUT) ? "CUT" : "UNKNOWN"), flag);
-       }
-    
-    }  /* tb->rnum[0] > 0 */
-
-
-    RFALSE( tb->blknum[0] > 3,
-           "PAP-12180: blknum can not be %d. It must be <= 3",  tb->blknum[0]);
-    RFALSE( tb->blknum[0] < 0,
-           "PAP-12185: blknum can not be %d. It must be >= 0",  tb->blknum[0]);
-
-    /* if while adding to a node we discover that it is possible to split
-       it in two, and merge the left part into the left neighbor and the
-       right part into the right neighbor, eliminating the node */
-    if ( tb->blknum[0] == 0 ) { /* node S[0] is empty now */
-
-       RFALSE( ! tb->lnum[0] || ! tb->rnum[0],
-               "PAP-12190: lnum and rnum must not be zero");
-       /* if insertion was done before 0-th position in R[0], right
-          delimiting key of the tb->L[0]'s and left delimiting key are
-          not set correctly */
-       if (tb->CFL[0]) {
-           if (!tb->CFR[0])
-               reiserfs_panic (tb->tb_sb, "vs-12195: balance_leaf: CFR not initialized");
-           copy_key (B_N_PDELIM_KEY (tb->CFL[0], tb->lkey[0]), B_N_PDELIM_KEY (tb->CFR[0], tb->rkey[0]));
-           do_balance_mark_internal_dirty (tb, tb->CFL[0], 0);
-       }
-
-       reiserfs_invalidate_buffer(tb,tbS0);                                                                    
-       return 0;
-    }
-
-
-    /* Fill new nodes that appear in place of S[0] */
 
-    /* I am told that this copying is because we need an array to enable
-       the looping code. -Hans */
-    snum[0] = tb->s1num,
-       snum[1] = tb->s2num;
-    sbytes[0] = tb->s1bytes;
-    sbytes[1] = tb->s2bytes;
-    for( i = tb->blknum[0] - 2; i >= 0; i-- ) {
-
-       RFALSE( !snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i, snum[i]);
+       }
 
-       /* here we shift from S to S_new nodes */
+       /* tb->rnum[0] > 0 */
+       RFALSE(tb->blknum[0] > 3,
+              "PAP-12180: blknum can not be %d. It must be <= 3",
+              tb->blknum[0]);
+       RFALSE(tb->blknum[0] < 0,
+              "PAP-12185: blknum can not be %d. It must be >= 0",
+              tb->blknum[0]);
+
+       /* if while adding to a node we discover that it is possible to split
+          it in two, and merge the left part into the left neighbor and the
+          right part into the right neighbor, eliminating the node */
+       if (tb->blknum[0] == 0) {       /* node S[0] is empty now */
+
+               RFALSE(!tb->lnum[0] || !tb->rnum[0],
+                      "PAP-12190: lnum and rnum must not be zero");
+               /* if insertion was done before 0-th position in R[0], right
+                  delimiting key of the tb->L[0]'s and left delimiting key are
+                  not set correctly */
+               if (tb->CFL[0]) {
+                       if (!tb->CFR[0])
+                               reiserfs_panic(tb->tb_sb,
+                                              "vs-12195: balance_leaf: CFR not initialized");
+                       copy_key(B_N_PDELIM_KEY(tb->CFL[0], tb->lkey[0]),
+                                B_N_PDELIM_KEY(tb->CFR[0], tb->rkey[0]));
+                       do_balance_mark_internal_dirty(tb, tb->CFL[0], 0);
+               }
 
-       S_new[i] = get_FEB(tb);
+               reiserfs_invalidate_buffer(tb, tbS0);
+               return 0;
+       }
 
-       /* initialized block type and tree level */
-        set_blkh_level( B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL );
+       /* Fill new nodes that appear in place of S[0] */
+
+       /* I am told that this copying is because we need an array to enable
+          the looping code. -Hans */
+       snum[0] = tb->s1num, snum[1] = tb->s2num;
+       sbytes[0] = tb->s1bytes;
+       sbytes[1] = tb->s2bytes;
+       for (i = tb->blknum[0] - 2; i >= 0; i--) {
+
+               RFALSE(!snum[i], "PAP-12200: snum[%d] == %d. Must be > 0", i,
+                      snum[i]);
+
+               /* here we shift from S to S_new nodes */
+
+               S_new[i] = get_FEB(tb);
+
+               /* initialized block type and tree level */
+               set_blkh_level(B_BLK_HEAD(S_new[i]), DISK_LEAF_NODE_LEVEL);
+
+               n = B_NR_ITEMS(tbS0);
+
+               switch (flag) {
+               case M_INSERT:  /* insert item */
+
+                       if (n - snum[i] < item_pos) {   /* new item or it's part falls to first new node S_new[i] */
+                               if (item_pos == n - snum[i] + 1 && sbytes[i] != -1) {   /* part of new item falls into S_new[i] */
+                                       int old_key_comp, old_len,
+                                           r_zeros_number;
+                                       const char *r_body;
+                                       int version;
+
+                                       /* Move snum[i]-1 items from S[0] to S_new[i] */
+                                       leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
+                                                       snum[i] - 1, -1,
+                                                       S_new[i]);
+                                       /* Remember key component and item length */
+                                       version = ih_version(ih);
+                                       old_key_comp = le_ih_k_offset(ih);
+                                       old_len = ih_item_len(ih);
+
+                                       /* Calculate key component and item length to insert into S_new[i] */
+                                       set_le_ih_k_offset(ih,
+                                                          le_ih_k_offset(ih) +
+                                                          ((old_len -
+                                                            sbytes[i]) <<
+                                                           (is_indirect_le_ih
+                                                            (ih) ? tb->tb_sb->
+                                                            s_blocksize_bits -
+                                                            UNFM_P_SHIFT :
+                                                            0)));
+
+                                       put_ih_item_len(ih, sbytes[i]);
+
+                                       /* Insert part of the item into S_new[i] before 0-th item */
+                                       bi.tb = tb;
+                                       bi.bi_bh = S_new[i];
+                                       bi.bi_parent = NULL;
+                                       bi.bi_position = 0;
+
+                                       if ((old_len - sbytes[i]) > zeros_num) {
+                                               r_zeros_number = 0;
+                                               r_body =
+                                                   body + (old_len -
+                                                           sbytes[i]) -
+                                                   zeros_num;
+                                       } else {
+                                               r_body = body;
+                                               r_zeros_number =
+                                                   zeros_num - (old_len -
+                                                                sbytes[i]);
+                                               zeros_num -= r_zeros_number;
+                                       }
+
+                                       leaf_insert_into_buf(&bi, 0, ih, r_body,
+                                                            r_zeros_number);
+
+                                       /* Calculate key component and item length to insert into S[i] */
+                                       set_le_ih_k_offset(ih, old_key_comp);
+                                       put_ih_item_len(ih,
+                                                       old_len - sbytes[i]);
+                                       tb->insert_size[0] -= sbytes[i];
+                               } else {        /* whole new item falls into S_new[i] */
+
+                                       /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
+                                       leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
+                                                       snum[i] - 1, sbytes[i],
+                                                       S_new[i]);
+
+                                       /* Insert new item into S_new[i] */
+                                       bi.tb = tb;
+                                       bi.bi_bh = S_new[i];
+                                       bi.bi_parent = NULL;
+                                       bi.bi_position = 0;
+                                       leaf_insert_into_buf(&bi,
+                                                            item_pos - n +
+                                                            snum[i] - 1, ih,
+                                                            body, zeros_num);
+
+                                       zeros_num = tb->insert_size[0] = 0;
+                               }
+                       }
 
+                       else {  /* new item or it part don't falls into S_new[i] */
 
-       n = B_NR_ITEMS(tbS0);
-       
-       switch (flag) {
-       case M_INSERT:   /* insert item */
-
-           if ( n - snum[i] < item_pos )
-           { /* new item or it's part falls to first new node S_new[i]*/
-               if ( item_pos == n - snum[i] + 1 && sbytes[i] != -1 )
-               { /* part of new item falls into S_new[i] */
-                   int old_key_comp, old_len, r_zeros_number;
-                   const char * r_body;
-                   int version;
-
-                   /* Move snum[i]-1 items from S[0] to S_new[i] */
-                   leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i] - 1, -1, S_new[i]);
-                   /* Remember key component and item length */
-                   version = ih_version (ih);
-                    old_key_comp = le_ih_k_offset( ih );
-                   old_len = ih_item_len(ih);
-
-                   /* Calculate key component and item length to insert into S_new[i] */
-                    set_le_ih_k_offset( ih,
-                                le_ih_k_offset(ih) + ((old_len - sbytes[i] )<<(is_indirect_le_ih(ih)?tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT:0)) );
-
-                   put_ih_item_len( ih, sbytes[i] );
-
-                   /* Insert part of the item into S_new[i] before 0-th item */
-                   bi.tb = tb;
-                   bi.bi_bh = S_new[i];
-                   bi.bi_parent = NULL;
-                   bi.bi_position = 0;
-
-                   if ( (old_len - sbytes[i]) > zeros_num ) {
-                       r_zeros_number = 0;
-                       r_body = body + (old_len - sbytes[i]) - zeros_num;
-                   }
-                   else {
-                       r_body = body;
-                       r_zeros_number = zeros_num - (old_len - sbytes[i]);
-                       zeros_num -= r_zeros_number;
-                   }
-
-                   leaf_insert_into_buf (&bi, 0, ih, r_body, r_zeros_number);
-
-                   /* Calculate key component and item length to insert into S[i] */
-                    set_le_ih_k_offset( ih, old_key_comp );
-                   put_ih_item_len( ih, old_len - sbytes[i] );
-                   tb->insert_size[0] -= sbytes[i];
-               }
-               else /* whole new item falls into S_new[i] */
-               {
-                   /* Shift snum[0] - 1 items to S_new[i] (sbytes[i] of split item) */
-                   leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i] - 1, sbytes[i], S_new[i]);
-
-                   /* Insert new item into S_new[i] */
-                   bi.tb = tb;
-                   bi.bi_bh = S_new[i];
-                   bi.bi_parent = NULL;
-                   bi.bi_position = 0;
-                   leaf_insert_into_buf (&bi, item_pos - n + snum[i] - 1, ih, body, zeros_num);
-
-                   zeros_num = tb->insert_size[0] = 0;
-               }
-           }
-
-           else /* new item or it part don't falls into S_new[i] */
-           {
-               leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i], sbytes[i], S_new[i]);
-           }
-           break;
-
-       case M_PASTE:   /* append item */
-
-           if ( n - snum[i] <= item_pos )  /* pasted item or part if it falls to S_new[i] */
-           {
-               if ( item_pos == n - snum[i] && sbytes[i] != -1 )
-               { /* we must shift part of the appended item */
-                   struct item_head * aux_ih;
-
-                   RFALSE( ih, "PAP-12210: ih must be 0");
-
-                   if ( is_direntry_le_ih (aux_ih = B_N_PITEM_HEAD(tbS0,item_pos))) {
-                       /* we append to directory item */
-
-                       int entry_count;
-               
-                       entry_count = ih_entry_count(aux_ih);
-
-                       if ( entry_count - sbytes[i] < pos_in_item  && pos_in_item <= entry_count ) {
-                           /* new directory entry falls into S_new[i] */
-                 
-                           RFALSE( ! tb->insert_size[0],
-                                   "PAP-12215: insert_size is already 0");
-                           RFALSE( sbytes[i] - 1 >= entry_count,
-                                   "PAP-12220: there are no so much entries (%d), only %d",
-                                   sbytes[i] - 1, entry_count);
-
-                           /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
-                           leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i], sbytes[i]-1, S_new[i]);
-                           /* Paste given directory entry to directory item */
-                           bi.tb = tb;
-                           bi.bi_bh = S_new[i];
-                           bi.bi_parent = NULL;
-                           bi.bi_position = 0;
-                           leaf_paste_in_buffer (&bi, 0, pos_in_item - entry_count + sbytes[i] - 1,
-                                                 tb->insert_size[0], body,zeros_num);
-                           /* paste new directory entry */
-                           leaf_paste_entries (
-                               bi.bi_bh, 0, pos_in_item - entry_count + sbytes[i] - 1,
-                               1, (struct reiserfs_de_head *)body, body + DEH_SIZE,
-                               tb->insert_size[0]
-                               );
-                           tb->insert_size[0] = 0;
-                           pos_in_item++;
-                       } else { /* new directory entry doesn't fall into S_new[i] */
-                           leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i], sbytes[i], S_new[i]);
+                               leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
+                                               snum[i], sbytes[i], S_new[i]);
                        }
-                   }
-                   else /* regular object */
-                   {
-                       int n_shift, n_rem, r_zeros_number;
-                       const char * r_body;
-
-                       RFALSE( pos_in_item != ih_item_len(B_N_PITEM_HEAD(tbS0,item_pos)) ||
-                               tb->insert_size[0] <= 0,
-                               "PAP-12225: item too short or insert_size <= 0");
-
-                       /* Calculate number of bytes which must be shifted from appended item */
-                       n_shift = sbytes[i] - tb->insert_size[0];
-                       if ( n_shift < 0 )
-                           n_shift = 0;
-                       leaf_move_items (LEAF_FROM_S_TO_SNEW, tb, snum[i], n_shift, S_new[i]);
-
-                       /* Calculate number of bytes which must remain in body after append to S_new[i] */
-                       n_rem = tb->insert_size[0] - sbytes[i];
-                       if ( n_rem < 0 )
-                           n_rem = 0;
-                       /* Append part of body into S_new[0] */
-                       bi.tb = tb;
-                       bi.bi_bh = S_new[i];
-                       bi.bi_parent = NULL;
-                       bi.bi_position = 0;
+                       break;
+
+               case M_PASTE:   /* append item */
+
+                       if (n - snum[i] <= item_pos) {  /* pasted item or part if it falls to S_new[i] */
+                               if (item_pos == n - snum[i] && sbytes[i] != -1) {       /* we must shift part of the appended item */
+                                       struct item_head *aux_ih;
+
+                                       RFALSE(ih, "PAP-12210: ih must be 0");
+
+                                       if (is_direntry_le_ih
+                                           (aux_ih =
+                                            B_N_PITEM_HEAD(tbS0, item_pos))) {
+                                               /* we append to directory item */
+
+                                               int entry_count;
+
+                                               entry_count =
+                                                   ih_entry_count(aux_ih);
+
+                                               if (entry_count - sbytes[i] <
+                                                   pos_in_item
+                                                   && pos_in_item <=
+                                                   entry_count) {
+                                                       /* new directory entry falls into S_new[i] */
+
+                                                       RFALSE(!tb->
+                                                              insert_size[0],
+                                                              "PAP-12215: insert_size is already 0");
+                                                       RFALSE(sbytes[i] - 1 >=
+                                                              entry_count,
+                                                              "PAP-12220: there are no so much entries (%d), only %d",
+                                                              sbytes[i] - 1,
+                                                              entry_count);
+
+                                                       /* Shift snum[i]-1 items in whole. Shift sbytes[i] directory entries from directory item number snum[i] */
+                                                       leaf_move_items
+                                                           (LEAF_FROM_S_TO_SNEW,
+                                                            tb, snum[i],
+                                                            sbytes[i] - 1,
+                                                            S_new[i]);
+                                                       /* Paste given directory entry to directory item */
+                                                       bi.tb = tb;
+                                                       bi.bi_bh = S_new[i];
+                                                       bi.bi_parent = NULL;
+                                                       bi.bi_position = 0;
+                                                       leaf_paste_in_buffer
+                                                           (&bi, 0,
+                                                            pos_in_item -
+                                                            entry_count +
+                                                            sbytes[i] - 1,
+                                                            tb->insert_size[0],
+                                                            body, zeros_num);
+                                                       /* paste new directory entry */
+                                                       leaf_paste_entries(bi.
+                                                                          bi_bh,
+                                                                          0,
+                                                                          pos_in_item
+                                                                          -
+                                                                          entry_count
+                                                                          +
+                                                                          sbytes
+                                                                          [i] -
+                                                                          1, 1,
+                                                                          (struct
+                                                                           reiserfs_de_head
+                                                                           *)
+                                                                          body,
+                                                                          body
+                                                                          +
+                                                                          DEH_SIZE,
+                                                                          tb->
+                                                                          insert_size
+                                                                          [0]
+                                                           );
+                                                       tb->insert_size[0] = 0;
+                                                       pos_in_item++;
+                                               } else {        /* new directory entry doesn't fall into S_new[i] */
+                                                       leaf_move_items
+                                                           (LEAF_FROM_S_TO_SNEW,
+                                                            tb, snum[i],
+                                                            sbytes[i],
+                                                            S_new[i]);
+                                               }
+                                       } else {        /* regular object */
+
+                                               int n_shift, n_rem,
+                                                   r_zeros_number;
+                                               const char *r_body;
+
+                                               RFALSE(pos_in_item !=
+                                                      ih_item_len
+                                                      (B_N_PITEM_HEAD
+                                                       (tbS0, item_pos))
+                                                      || tb->insert_size[0] <=
+                                                      0,
+                                                      "PAP-12225: item too short or insert_size <= 0");
+
+                                               /* Calculate number of bytes which must be shifted from appended item */
+                                               n_shift =
+                                                   sbytes[i] -
+                                                   tb->insert_size[0];
+                                               if (n_shift < 0)
+                                                       n_shift = 0;
+                                               leaf_move_items
+                                                   (LEAF_FROM_S_TO_SNEW, tb,
+                                                    snum[i], n_shift,
+                                                    S_new[i]);
+
+                                               /* Calculate number of bytes which must remain in body after append to S_new[i] */
+                                               n_rem =
+                                                   tb->insert_size[0] -
+                                                   sbytes[i];
+                                               if (n_rem < 0)
+                                                       n_rem = 0;
+                                               /* Append part of body into S_new[0] */
+                                               bi.tb = tb;
+                                               bi.bi_bh = S_new[i];
+                                               bi.bi_parent = NULL;
+                                               bi.bi_position = 0;
+
+                                               if (n_rem > zeros_num) {
+                                                       r_zeros_number = 0;
+                                                       r_body =
+                                                           body + n_rem -
+                                                           zeros_num;
+                                               } else {
+                                                       r_body = body;
+                                                       r_zeros_number =
+                                                           zeros_num - n_rem;
+                                                       zeros_num -=
+                                                           r_zeros_number;
+                                               }
+
+                                               leaf_paste_in_buffer(&bi, 0,
+                                                                    n_shift,
+                                                                    tb->
+                                                                    insert_size
+                                                                    [0] -
+                                                                    n_rem,
+                                                                    r_body,
+                                                                    r_zeros_number);
+                                               {
+                                                       struct item_head *tmp;
+
+                                                       tmp =
+                                                           B_N_PITEM_HEAD(S_new
+                                                                          [i],
+                                                                          0);
+                                                       if (is_indirect_le_ih
+                                                           (tmp)) {
+                                                               set_ih_free_space
+                                                                   (tmp, 0);
+                                                               set_le_ih_k_offset
+                                                                   (tmp,
+                                                                    le_ih_k_offset
+                                                                    (tmp) +
+                                                                    (n_rem <<
+                                                                     (tb->
+                                                                      tb_sb->
+                                                                      s_blocksize_bits
+                                                                      -
+                                                                      UNFM_P_SHIFT)));
+                                                       } else {
+                                                               set_le_ih_k_offset
+                                                                   (tmp,
+                                                                    le_ih_k_offset
+                                                                    (tmp) +
+                                                                    n_rem);
+                                                       }
+                                               }
+
+                                               tb->insert_size[0] = n_rem;
+                                               if (!n_rem)
+                                                       pos_in_item++;
+                                       }
+                               } else
+                                       /* item falls wholly into S_new[i] */
+                               {
+                                       int ret_val;
+                                       struct item_head *pasted;
 
-                       if ( n_rem > zeros_num ) {
-                           r_zeros_number = 0;
-                           r_body = body + n_rem - zeros_num;
-                       }
-                       else {
-                           r_body = body;
-                           r_zeros_number = zeros_num - n_rem;
-                           zeros_num -= r_zeros_number;
+#ifdef CONFIG_REISERFS_CHECK
+                                       struct item_head *ih =
+                                           B_N_PITEM_HEAD(tbS0, item_pos);
+
+                                       if (!is_direntry_le_ih(ih)
+                                           && (pos_in_item != ih_item_len(ih)
+                                               || tb->insert_size[0] <= 0))
+                                               reiserfs_panic(tb->tb_sb,
+                                                              "PAP-12235: balance_leaf: pos_in_item must be equal to ih_item_len");
+#endif                         /* CONFIG_REISERFS_CHECK */
+
+                                       ret_val =
+                                           leaf_move_items(LEAF_FROM_S_TO_SNEW,
+                                                           tb, snum[i],
+                                                           sbytes[i],
+                                                           S_new[i]);
+
+                                       RFALSE(ret_val,
+                                              "PAP-12240: unexpected value returned by leaf_move_items (%d)",
+                                              ret_val);
+
+                                       /* paste into item */
+                                       bi.tb = tb;
+                                       bi.bi_bh = S_new[i];
+                                       bi.bi_parent = NULL;
+                                       bi.bi_position = 0;
+                                       leaf_paste_in_buffer(&bi,
+                                                            item_pos - n +
+                                                            snum[i],
+                                                            pos_in_item,
+                                                            tb->insert_size[0],
+                                                            body, zeros_num);
+
+                                       pasted =
+                                           B_N_PITEM_HEAD(S_new[i],
+                                                          item_pos - n +
+                                                          snum[i]);
+                                       if (is_direntry_le_ih(pasted)) {
+                                               leaf_paste_entries(bi.bi_bh,
+                                                                  item_pos -
+                                                                  n + snum[i],
+                                                                  pos_in_item,
+                                                                  1,
+                                                                  (struct
+                                                                   reiserfs_de_head
+                                                                   *)body,
+                                                                  body +
+                                                                  DEH_SIZE,
+                                                                  tb->
+                                                                  insert_size
+                                                                  [0]
+                                                   );
+                                       }
+
+                                       /* if we paste to indirect item update ih_free_space */
+                                       if (is_indirect_le_ih(pasted))
+                                               set_ih_free_space(pasted, 0);
+                                       zeros_num = tb->insert_size[0] = 0;
+                               }
                        }
 
-                       leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0]-n_rem, r_body,r_zeros_number);
-                       {
-                           struct item_head * tmp;
-
-                           tmp = B_N_PITEM_HEAD(S_new[i],0);
-                           if (is_indirect_le_ih (tmp)) {
-                               set_ih_free_space (tmp, 0);
-                               set_le_ih_k_offset( tmp, le_ih_k_offset(tmp) + 
-                                                   (n_rem << (tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT)));
-                           } else {
-                               set_le_ih_k_offset( tmp, le_ih_k_offset(tmp) + 
-                                                   n_rem );
-                           }
-                       }
+                       else {  /* pasted item doesn't fall into S_new[i] */
 
-                       tb->insert_size[0] = n_rem;
-                       if ( ! n_rem )
-                           pos_in_item++;
-                   }
+                               leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
+                                               snum[i], sbytes[i], S_new[i]);
+                       }
+                       break;
+               default:        /* cases d and t */
+                       reiserfs_panic(tb->tb_sb,
+                                      "PAP-12245: balance_leaf: blknum > 2: unexpectable mode: %s(%d)",
+                                      (flag ==
+                                       M_DELETE) ? "DELETE" : ((flag ==
+                                                                M_CUT) ? "CUT"
+                                                               : "UNKNOWN"),
+                                      flag);
                }
-               else
-                   /* item falls wholly into S_new[i] */
-