inode.c (0222e6571c332563a48d4cf5487b67feabe60b5e) inode.c (995c762ea486b48c9777522071fbf132dea96807)
1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5#include <linux/time.h>
6#include <linux/fs.h>
7#include <linux/reiserfs_fs.h>
8#include <linux/reiserfs_acl.h>

--- 1973 unchanged lines hidden (view full) ---

1982** is set to an up to date buffer for the last block in the file. returns 0.
1983**
1984** tail conversion is not done, so bh_result might not be valid for writing
1985** check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
1986** trying to write the block.
1987**
1988** on failure, nonzero is returned, page_result and bh_result are untouched.
1989*/
1/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5#include <linux/time.h>
6#include <linux/fs.h>
7#include <linux/reiserfs_fs.h>
8#include <linux/reiserfs_acl.h>

--- 1973 unchanged lines hidden (view full) ---

1982** is set to an up to date buffer for the last block in the file. returns 0.
1983**
1984** tail conversion is not done, so bh_result might not be valid for writing
1985** check buffer_mapped(bh_result) and bh_result->b_blocknr != 0 before
1986** trying to write the block.
1987**
1988** on failure, nonzero is returned, page_result and bh_result are untouched.
1989*/
1990static int grab_tail_page(struct inode *p_s_inode,
1990static int grab_tail_page(struct inode *inode,
1991 struct page **page_result,
1992 struct buffer_head **bh_result)
1993{
1994
1995 /* we want the page with the last byte in the file,
1996 ** not the page that will hold the next byte for appending
1997 */
1991 struct page **page_result,
1992 struct buffer_head **bh_result)
1993{
1994
1995 /* we want the page with the last byte in the file,
1996 ** not the page that will hold the next byte for appending
1997 */
1998 unsigned long index = (p_s_inode->i_size - 1) >> PAGE_CACHE_SHIFT;
1998 unsigned long index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
1999 unsigned long pos = 0;
2000 unsigned long start = 0;
1999 unsigned long pos = 0;
2000 unsigned long start = 0;
2001 unsigned long blocksize = p_s_inode->i_sb->s_blocksize;
2002 unsigned long offset = (p_s_inode->i_size) & (PAGE_CACHE_SIZE - 1);
2001 unsigned long blocksize = inode->i_sb->s_blocksize;
2002 unsigned long offset = (inode->i_size) & (PAGE_CACHE_SIZE - 1);
2003 struct buffer_head *bh;
2004 struct buffer_head *head;
2005 struct page *page;
2006 int error;
2007
2008 /* we know that we are only called with inode->i_size > 0.
2009 ** we also know that a file tail can never be as big as a block
2010 ** If i_size % blocksize == 0, our file is currently block aligned
2011 ** and it won't need converting or zeroing after a truncate.
2012 */
2013 if ((offset & (blocksize - 1)) == 0) {
2014 return -ENOENT;
2015 }
2003 struct buffer_head *bh;
2004 struct buffer_head *head;
2005 struct page *page;
2006 int error;
2007
2008 /* we know that we are only called with inode->i_size > 0.
2009 ** we also know that a file tail can never be as big as a block
2010 ** If i_size % blocksize == 0, our file is currently block aligned
2011 ** and it won't need converting or zeroing after a truncate.
2012 */
2013 if ((offset & (blocksize - 1)) == 0) {
2014 return -ENOENT;
2015 }
2016 page = grab_cache_page(p_s_inode->i_mapping, index);
2016 page = grab_cache_page(inode->i_mapping, index);
2017 error = -ENOMEM;
2018 if (!page) {
2019 goto out;
2020 }
2021 /* start within the page of the last block in the file */
2022 start = (offset / blocksize) * blocksize;
2023
2024 error = block_prepare_write(page, start, offset,

--- 12 unchanged lines hidden (view full) ---

2037 } while (bh != head);
2038
2039 if (!buffer_uptodate(bh)) {
2040 /* note, this should never happen, prepare_write should
2041 ** be taking care of this for us. If the buffer isn't up to date,
2042 ** I've screwed up the code to find the buffer, or the code to
2043 ** call prepare_write
2044 */
2017 error = -ENOMEM;
2018 if (!page) {
2019 goto out;
2020 }
2021 /* start within the page of the last block in the file */
2022 start = (offset / blocksize) * blocksize;
2023
2024 error = block_prepare_write(page, start, offset,

--- 12 unchanged lines hidden (view full) ---

2037 } while (bh != head);
2038
2039 if (!buffer_uptodate(bh)) {
2040 /* note, this should never happen, prepare_write should
2041 ** be taking care of this for us. If the buffer isn't up to date,
2042 ** I've screwed up the code to find the buffer, or the code to
2043 ** call prepare_write
2044 */
2045 reiserfs_error(p_s_inode->i_sb, "clm-6000",
2045 reiserfs_error(inode->i_sb, "clm-6000",
2046 "error reading block %lu", bh->b_blocknr);
2047 error = -EIO;
2048 goto unlock;
2049 }
2050 *bh_result = bh;
2051 *page_result = page;
2052
2053 out:

--- 6 unchanged lines hidden (view full) ---

2060}
2061
2062/*
2063** vfs version of truncate file. Must NOT be called with
2064** a transaction already started.
2065**
2066** some code taken from block_truncate_page
2067*/
2046 "error reading block %lu", bh->b_blocknr);
2047 error = -EIO;
2048 goto unlock;
2049 }
2050 *bh_result = bh;
2051 *page_result = page;
2052
2053 out:

--- 6 unchanged lines hidden (view full) ---

2060}
2061
2062/*
2063** vfs version of truncate file. Must NOT be called with
2064** a transaction already started.
2065**
2066** some code taken from block_truncate_page
2067*/
2068int reiserfs_truncate_file(struct inode *p_s_inode, int update_timestamps)
2068int reiserfs_truncate_file(struct inode *inode, int update_timestamps)
2069{
2070 struct reiserfs_transaction_handle th;
2071 /* we want the offset for the first byte after the end of the file */
2069{
2070 struct reiserfs_transaction_handle th;
2071 /* we want the offset for the first byte after the end of the file */
2072 unsigned long offset = p_s_inode->i_size & (PAGE_CACHE_SIZE - 1);
2073 unsigned blocksize = p_s_inode->i_sb->s_blocksize;
2072 unsigned long offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
2073 unsigned blocksize = inode->i_sb->s_blocksize;
2074 unsigned length;
2075 struct page *page = NULL;
2076 int error;
2077 struct buffer_head *bh = NULL;
2078 int err2;
2079
2074 unsigned length;
2075 struct page *page = NULL;
2076 int error;
2077 struct buffer_head *bh = NULL;
2078 int err2;
2079
2080 reiserfs_write_lock(p_s_inode->i_sb);
2080 reiserfs_write_lock(inode->i_sb);
2081
2081
2082 if (p_s_inode->i_size > 0) {
2083 if ((error = grab_tail_page(p_s_inode, &page, &bh))) {
2082 if (inode->i_size > 0) {
2083 error = grab_tail_page(inode, &page, &bh);
2084 if (error) {
2084 // -ENOENT means we truncated past the end of the file,
2085 // and get_block_create_0 could not find a block to read in,
2086 // which is ok.
2087 if (error != -ENOENT)
2085 // -ENOENT means we truncated past the end of the file,
2086 // and get_block_create_0 could not find a block to read in,
2087 // which is ok.
2088 if (error != -ENOENT)
2088 reiserfs_error(p_s_inode->i_sb, "clm-6001",
2089 reiserfs_error(inode->i_sb, "clm-6001",
2089 "grab_tail_page failed %d",
2090 error);
2091 page = NULL;
2092 bh = NULL;
2093 }
2094 }
2095
2096 /* so, if page != NULL, we have a buffer head for the offset at
2097 ** the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
2098 ** then we have an unformatted node. Otherwise, we have a direct item,
2099 ** and no zeroing is required on disk. We zero after the truncate,
2100 ** because the truncate might pack the item anyway
2101 ** (it will unmap bh if it packs).
2102 */
2103 /* it is enough to reserve space in transaction for 2 balancings:
2104 one for "save" link adding and another for the first
2105 cut_from_item. 1 is for update_sd */
2090 "grab_tail_page failed %d",
2091 error);
2092 page = NULL;
2093 bh = NULL;
2094 }
2095 }
2096
2097 /* so, if page != NULL, we have a buffer head for the offset at
2098 ** the end of the file. if the bh is mapped, and bh->b_blocknr != 0,
2099 ** then we have an unformatted node. Otherwise, we have a direct item,
2100 ** and no zeroing is required on disk. We zero after the truncate,
2101 ** because the truncate might pack the item anyway
2102 ** (it will unmap bh if it packs).
2103 */
2104 /* it is enough to reserve space in transaction for 2 balancings:
2105 one for "save" link adding and another for the first
2106 cut_from_item. 1 is for update_sd */
2106 error = journal_begin(&th, p_s_inode->i_sb,
2107 error = journal_begin(&th, inode->i_sb,
2107 JOURNAL_PER_BALANCE_CNT * 2 + 1);
2108 if (error)
2109 goto out;
2108 JOURNAL_PER_BALANCE_CNT * 2 + 1);
2109 if (error)
2110 goto out;
2110 reiserfs_update_inode_transaction(p_s_inode);
2111 reiserfs_update_inode_transaction(inode);
2111 if (update_timestamps)
2112 /* we are doing real truncate: if the system crashes before the last
2113 transaction of truncating gets committed - on reboot the file
2114 either appears truncated properly or not truncated at all */
2112 if (update_timestamps)
2113 /* we are doing real truncate: if the system crashes before the last
2114 transaction of truncating gets committed - on reboot the file
2115 either appears truncated properly or not truncated at all */
2115 add_save_link(&th, p_s_inode, 1);
2116 err2 = reiserfs_do_truncate(&th, p_s_inode, page, update_timestamps);
2116 add_save_link(&th, inode, 1);
2117 err2 = reiserfs_do_truncate(&th, inode, page, update_timestamps);
2117 error =
2118 error =
2118 journal_end(&th, p_s_inode->i_sb, JOURNAL_PER_BALANCE_CNT * 2 + 1);
2119 journal_end(&th, inode->i_sb, JOURNAL_PER_BALANCE_CNT * 2 + 1);
2119 if (error)
2120 goto out;
2121
2122 /* check reiserfs_do_truncate after ending the transaction */
2123 if (err2) {
2124 error = err2;
2125 goto out;
2126 }
2127
2128 if (update_timestamps) {
2120 if (error)
2121 goto out;
2122
2123 /* check reiserfs_do_truncate after ending the transaction */
2124 if (err2) {
2125 error = err2;
2126 goto out;
2127 }
2128
2129 if (update_timestamps) {
2129 error = remove_save_link(p_s_inode, 1 /* truncate */ );
2130 error = remove_save_link(inode, 1 /* truncate */);
2130 if (error)
2131 goto out;
2132 }
2133
2134 if (page) {
2135 length = offset & (blocksize - 1);
2136 /* if we are not on a block boundary */
2137 if (length) {
2138 length = blocksize - length;
2139 zero_user(page, offset, length);
2140 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2141 mark_buffer_dirty(bh);
2142 }
2143 }
2144 unlock_page(page);
2145 page_cache_release(page);
2146 }
2147
2131 if (error)
2132 goto out;
2133 }
2134
2135 if (page) {
2136 length = offset & (blocksize - 1);
2137 /* if we are not on a block boundary */
2138 if (length) {
2139 length = blocksize - length;
2140 zero_user(page, offset, length);
2141 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2142 mark_buffer_dirty(bh);
2143 }
2144 }
2145 unlock_page(page);
2146 page_cache_release(page);
2147 }
2148
2148 reiserfs_write_unlock(p_s_inode->i_sb);
2149 reiserfs_write_unlock(inode->i_sb);
2149 return 0;
2150 out:
2151 if (page) {
2152 unlock_page(page);
2153 page_cache_release(page);
2154 }
2150 return 0;
2151 out:
2152 if (page) {
2153 unlock_page(page);
2154 page_cache_release(page);
2155 }
2155 reiserfs_write_unlock(p_s_inode->i_sb);
2156 reiserfs_write_unlock(inode->i_sb);
2156 return error;
2157}
2158
2159static int map_block_for_writepage(struct inode *inode,
2160 struct buffer_head *bh_result,
2161 unsigned long block)
2162{
2163 struct reiserfs_transaction_handle th;

--- 996 unchanged lines hidden ---
2157 return error;
2158}
2159
2160static int map_block_for_writepage(struct inode *inode,
2161 struct buffer_head *bh_result,
2162 unsigned long block)
2163{
2164 struct reiserfs_transaction_handle th;

--- 996 unchanged lines hidden ---