Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright 1999 Hans Reiser, see reiserfs/README for licensing and copyright
0004  * details
0005  */
0006 
0007 #include <linux/time.h>
0008 #include <linux/pagemap.h>
0009 #include <linux/buffer_head.h>
0010 #include "reiserfs.h"
0011 
0012 /*
0013  * access to tail : when one is going to read tail it must make sure, that is
0014  * not running.  direct2indirect and indirect2direct can not run concurrently
0015  */
0016 
0017 /*
0018  * Converts direct items to an unformatted node. Panics if file has no
0019  * tail. -ENOSPC if no disk space for conversion
0020  */
0021 /*
0022  * path points to first direct item of the file regardless of how many of
0023  * them are there
0024  */
0025 int direct2indirect(struct reiserfs_transaction_handle *th, struct inode *inode,
0026             struct treepath *path, struct buffer_head *unbh,
0027             loff_t tail_offset)
0028 {
0029     struct super_block *sb = inode->i_sb;
0030     struct buffer_head *up_to_date_bh;
0031     struct item_head *p_le_ih = tp_item_head(path);
0032     unsigned long total_tail = 0;
0033 
0034     /* Key to search for the last byte of the converted item. */
0035     struct cpu_key end_key;
0036 
0037     /*
0038      * new indirect item to be inserted or key
0039      * of unfm pointer to be pasted
0040      */
0041     struct item_head ind_ih;
0042     int blk_size;
0043     /* returned value for reiserfs_insert_item and clones */
0044     int  retval;
0045     /* Handle on an unformatted node that will be inserted in the tree. */
0046     unp_t unfm_ptr;
0047 
0048     BUG_ON(!th->t_trans_id);
0049 
0050     REISERFS_SB(sb)->s_direct2indirect++;
0051 
0052     blk_size = sb->s_blocksize;
0053 
0054     /*
0055      * and key to search for append or insert pointer to the new
0056      * unformatted node.
0057      */
0058     copy_item_head(&ind_ih, p_le_ih);
0059     set_le_ih_k_offset(&ind_ih, tail_offset);
0060     set_le_ih_k_type(&ind_ih, TYPE_INDIRECT);
0061 
0062     /* Set the key to search for the place for new unfm pointer */
0063     make_cpu_key(&end_key, inode, tail_offset, TYPE_INDIRECT, 4);
0064 
0065     /* FIXME: we could avoid this */
0066     if (search_for_position_by_key(sb, &end_key, path) == POSITION_FOUND) {
0067         reiserfs_error(sb, "PAP-14030",
0068                    "pasted or inserted byte exists in "
0069                    "the tree %K. Use fsck to repair.", &end_key);
0070         pathrelse(path);
0071         return -EIO;
0072     }
0073 
0074     p_le_ih = tp_item_head(path);
0075 
0076     unfm_ptr = cpu_to_le32(unbh->b_blocknr);
0077 
0078     if (is_statdata_le_ih(p_le_ih)) {
0079         /* Insert new indirect item. */
0080         set_ih_free_space(&ind_ih, 0);  /* delete at nearest future */
0081         put_ih_item_len(&ind_ih, UNFM_P_SIZE);
0082         PATH_LAST_POSITION(path)++;
0083         retval =
0084             reiserfs_insert_item(th, path, &end_key, &ind_ih, inode,
0085                      (char *)&unfm_ptr);
0086     } else {
0087         /* Paste into last indirect item of an object. */
0088         retval = reiserfs_paste_into_item(th, path, &end_key, inode,
0089                             (char *)&unfm_ptr,
0090                             UNFM_P_SIZE);
0091     }
0092     if (retval) {
0093         return retval;
0094     }
0095     /*
0096      * note: from here there are two keys which have matching first
0097      *  three key components. They only differ by the fourth one.
0098      */
0099 
0100     /* Set the key to search for the direct items of the file */
0101     make_cpu_key(&end_key, inode, max_reiserfs_offset(inode), TYPE_DIRECT,
0102              4);
0103 
0104     /*
0105      * Move bytes from the direct items to the new unformatted node
0106      * and delete them.
0107      */
0108     while (1) {
0109         int tail_size;
0110 
0111         /*
0112          * end_key.k_offset is set so, that we will always have found
0113          * last item of the file
0114          */
0115         if (search_for_position_by_key(sb, &end_key, path) ==
0116             POSITION_FOUND)
0117             reiserfs_panic(sb, "PAP-14050",
0118                        "direct item (%K) not found", &end_key);
0119         p_le_ih = tp_item_head(path);
0120         RFALSE(!is_direct_le_ih(p_le_ih),
0121                "vs-14055: direct item expected(%K), found %h",
0122                &end_key, p_le_ih);
0123         tail_size = (le_ih_k_offset(p_le_ih) & (blk_size - 1))
0124             + ih_item_len(p_le_ih) - 1;
0125 
0126         /*
0127          * we only send the unbh pointer if the buffer is not
0128          * up to date.  this avoids overwriting good data from
0129          * writepage() with old data from the disk or buffer cache
0130          * Special case: unbh->b_page will be NULL if we are coming
0131          * through DIRECT_IO handler here.
0132          */
0133         if (!unbh->b_page || buffer_uptodate(unbh)
0134             || PageUptodate(unbh->b_page)) {
0135             up_to_date_bh = NULL;
0136         } else {
0137             up_to_date_bh = unbh;
0138         }
0139         retval = reiserfs_delete_item(th, path, &end_key, inode,
0140                         up_to_date_bh);
0141 
0142         total_tail += retval;
0143 
0144         /* done: file does not have direct items anymore */
0145         if (tail_size == retval)
0146             break;
0147 
0148     }
0149     /*
0150      * if we've copied bytes from disk into the page, we need to zero
0151      * out the unused part of the block (it was not up to date before)
0152      */
0153     if (up_to_date_bh) {
0154         unsigned pgoff =
0155             (tail_offset + total_tail - 1) & (PAGE_SIZE - 1);
0156         char *kaddr = kmap_atomic(up_to_date_bh->b_page);
0157         memset(kaddr + pgoff, 0, blk_size - total_tail);
0158         kunmap_atomic(kaddr);
0159     }
0160 
0161     REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
0162 
0163     return 0;
0164 }
0165 
0166 /* stolen from fs/buffer.c */
0167 void reiserfs_unmap_buffer(struct buffer_head *bh)
0168 {
0169     lock_buffer(bh);
0170     if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
0171         BUG();
0172     }
0173     clear_buffer_dirty(bh);
0174     /*
0175      * Remove the buffer from whatever list it belongs to. We are mostly
0176      * interested in removing it from per-sb j_dirty_buffers list, to avoid
0177      * BUG() on attempt to write not mapped buffer
0178      */
0179     if ((!list_empty(&bh->b_assoc_buffers) || bh->b_private) && bh->b_page) {
0180         struct inode *inode = bh->b_page->mapping->host;
0181         struct reiserfs_journal *j = SB_JOURNAL(inode->i_sb);
0182         spin_lock(&j->j_dirty_buffers_lock);
0183         list_del_init(&bh->b_assoc_buffers);
0184         reiserfs_free_jh(bh);
0185         spin_unlock(&j->j_dirty_buffers_lock);
0186     }
0187     clear_buffer_mapped(bh);
0188     clear_buffer_req(bh);
0189     clear_buffer_new(bh);
0190     bh->b_bdev = NULL;
0191     unlock_buffer(bh);
0192 }
0193 
0194 /*
0195  * this first locks inode (neither reads nor sync are permitted),
0196  * reads tail through page cache, insert direct item. When direct item
0197  * inserted successfully inode is left locked. Return value is always
0198  * what we expect from it (number of cut bytes). But when tail remains
0199  * in the unformatted node, we set mode to SKIP_BALANCING and unlock
0200  * inode
0201  */
0202 int indirect2direct(struct reiserfs_transaction_handle *th,
0203             struct inode *inode, struct page *page,
0204             struct treepath *path,  /* path to the indirect item. */
0205             const struct cpu_key *item_key, /* Key to look for
0206                              * unformatted node
0207                              * pointer to be cut. */
0208             loff_t n_new_file_size, /* New file size. */
0209             char *mode)
0210 {
0211     struct super_block *sb = inode->i_sb;
0212     struct item_head s_ih;
0213     unsigned long block_size = sb->s_blocksize;
0214     char *tail;
0215     int tail_len, round_tail_len;
0216     loff_t pos, pos1;   /* position of first byte of the tail */
0217     struct cpu_key key;
0218 
0219     BUG_ON(!th->t_trans_id);
0220 
0221     REISERFS_SB(sb)->s_indirect2direct++;
0222 
0223     *mode = M_SKIP_BALANCING;
0224 
0225     /* store item head path points to. */
0226     copy_item_head(&s_ih, tp_item_head(path));
0227 
0228     tail_len = (n_new_file_size & (block_size - 1));
0229     if (get_inode_sd_version(inode) == STAT_DATA_V2)
0230         round_tail_len = ROUND_UP(tail_len);
0231     else
0232         round_tail_len = tail_len;
0233 
0234     pos =
0235         le_ih_k_offset(&s_ih) - 1 + (ih_item_len(&s_ih) / UNFM_P_SIZE -
0236                      1) * sb->s_blocksize;
0237     pos1 = pos;
0238 
0239     /*
0240      * we are protected by i_mutex. The tail can not disapper, not
0241      * append can be done either
0242      * we are in truncate or packing tail in file_release
0243      */
0244 
0245     tail = (char *)kmap(page);  /* this can schedule */
0246 
0247     if (path_changed(&s_ih, path)) {
0248         /* re-search indirect item */
0249         if (search_for_position_by_key(sb, item_key, path)
0250             == POSITION_NOT_FOUND)
0251             reiserfs_panic(sb, "PAP-5520",
0252                        "item to be converted %K does not exist",
0253                        item_key);
0254         copy_item_head(&s_ih, tp_item_head(path));
0255 #ifdef CONFIG_REISERFS_CHECK
0256         pos = le_ih_k_offset(&s_ih) - 1 +
0257             (ih_item_len(&s_ih) / UNFM_P_SIZE -
0258              1) * sb->s_blocksize;
0259         if (pos != pos1)
0260             reiserfs_panic(sb, "vs-5530", "tail position "
0261                        "changed while we were reading it");
0262 #endif
0263     }
0264 
0265     /* Set direct item header to insert. */
0266     make_le_item_head(&s_ih, NULL, get_inode_item_key_version(inode),
0267               pos1 + 1, TYPE_DIRECT, round_tail_len,
0268               0xffff /*ih_free_space */ );
0269 
0270     /*
0271      * we want a pointer to the first byte of the tail in the page.
0272      * the page was locked and this part of the page was up to date when
0273      * indirect2direct was called, so we know the bytes are still valid
0274      */
0275     tail = tail + (pos & (PAGE_SIZE - 1));
0276 
0277     PATH_LAST_POSITION(path)++;
0278 
0279     key = *item_key;
0280     set_cpu_key_k_type(&key, TYPE_DIRECT);
0281     key.key_length = 4;
0282     /* Insert tail as new direct item in the tree */
0283     if (reiserfs_insert_item(th, path, &key, &s_ih, inode,
0284                  tail ? tail : NULL) < 0) {
0285         /*
0286          * No disk memory. So we can not convert last unformatted node
0287          * to the direct item.  In this case we used to adjust
0288          * indirect items's ih_free_space. Now ih_free_space is not
0289          * used, it would be ideal to write zeros to corresponding
0290          * unformatted node. For now i_size is considered as guard for
0291          * going out of file size
0292          */
0293         kunmap(page);
0294         return block_size - round_tail_len;
0295     }
0296     kunmap(page);
0297 
0298     /* make sure to get the i_blocks changes from reiserfs_insert_item */
0299     reiserfs_update_sd(th, inode);
0300 
0301     /*
0302      * note: we have now the same as in above direct2indirect
0303      * conversion: there are two keys which have matching first three
0304      * key components. They only differ by the fourth one.
0305      */
0306 
0307     /*
0308      * We have inserted new direct item and must remove last
0309      * unformatted node.
0310      */
0311     *mode = M_CUT;
0312 
0313     /* we store position of first direct item in the in-core inode */
0314     /* mark_file_with_tail (inode, pos1 + 1); */
0315     REISERFS_I(inode)->i_first_direct_byte = pos1 + 1;
0316 
0317     return block_size - round_tail_len;
0318 }