5 #include <linux/time.h>
13 #include <linux/slab.h>
14 #include <asm/uaccess.h>
15 #include <asm/unaligned.h>
23 unsigned from,
unsigned to);
97 set_cpu_key_k_offset(key, offset);
98 set_cpu_key_k_type(key, type);
127 set_le_ih_k_offset(ih, offset);
128 set_le_ih_k_type(ih, type);
168 static inline void fix_tail_page_for_writing(
struct page *
page)
170 struct buffer_head *
head, *
next, *bh;
172 if (page && page_has_buffers(page)) {
173 head = page_buffers(page);
176 next = bh->b_this_page;
177 if (buffer_mapped(bh) && bh->b_blocknr == 0) {
181 }
while (bh != head);
199 static inline int indirect_item_found(
int retval,
struct item_head *ih)
204 static inline void set_block_dev_mapped(
struct buffer_head *bh,
207 map_bh(bh, inode->
i_sb, block);
217 block < (1 << (31 - inode->
i_sb->s_blocksize_bits)))
239 reiserfs_update_sd(th, inode);
257 static int _get_block_create_0(
struct inode *inode,
sector_t block,
258 struct buffer_head *bh_result,
int args)
262 struct buffer_head *bh;
274 (loff_t) block * inode->
i_sb->s_blocksize + 1,
TYPE_ANY,
281 kunmap(bh_result->b_page);
287 && !PageUptodate(bh_result->b_page)) {
295 if (is_indirect_le_ih(ih)) {
304 map_bh(bh_result, inode->
i_sb, blocknr);
307 set_buffer_boundary(bh_result);
312 if ((args & GET_BLOCK_NO_HOLE)
313 && !PageUptodate(bh_result->b_page)) {
319 kunmap(bh_result->b_page);
328 kunmap(bh_result->b_page);
336 if (buffer_uptodate(bh_result)) {
345 if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
346 set_buffer_uptodate(bh_result);
359 p = (
char *)
kmap(bh_result->b_page);
364 if (!is_direct_le_ih(ih)) {
376 inode->
i_size - (le_ih_k_offset(ih) - 1) -
397 set_cpu_key_k_offset(&
key, cpu_key_k_offset(&
key) + chars);
407 kunmap(bh_result->b_page);
418 map_bh(bh_result, inode->
i_sb, 0);
419 set_buffer_uptodate(bh_result);
425 static int reiserfs_bmap(
struct inode *inode,
sector_t block,
426 struct buffer_head *bh_result,
int create)
428 if (!file_capable(inode, block))
433 _get_block_create_0(inode, block, bh_result, 0);
455 static int reiserfs_get_block_create_0(
struct inode *inode,
sector_t block,
456 struct buffer_head *bh_result,
464 static int reiserfs_get_blocks_direct_io(
struct inode *inode,
466 struct buffer_head *bh_result,
471 bh_result->b_page =
NULL;
476 bh_result->b_size = (1 << inode->
i_blkbits);
484 if (buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
488 clear_buffer_mapped(bh_result);
499 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
520 static int convert_tail_for_hole(
struct inode *inode,
521 struct buffer_head *bh_result,
525 unsigned long tail_end;
526 unsigned long tail_start;
527 struct page *tail_page;
528 struct page *hole_page = bh_result->b_page;
531 if ((tail_offset & (bh_result->b_size - 1)) != 1)
536 tail_end = (tail_start | (bh_result->b_size - 1)) + 1;
542 if (!hole_page || index != hole_page->
index) {
543 tail_page = grab_cache_page(inode->
i_mapping, index);
549 tail_page = hole_page;
561 fix_tail_page_for_writing(tail_page);
563 tail_end - tail_start);
573 if (tail_page != hole_page) {
589 #ifdef REISERFS_PREALLOCATE
591 return reiserfs_new_unf_blocknrs2(th, inode, allocated_block_nr,
595 return reiserfs_new_unf_blocknrs(th, inode, allocated_block_nr, path,
600 struct buffer_head *bh_result,
int create)
602 int repeat, retval = 0;
607 struct buffer_head *bh, *unbh =
NULL;
626 (((loff_t) block) << inode->
i_sb->s_blocksize_bits) + 1;
631 if (!file_capable(inode, block)) {
642 ret = _get_block_create_0(inode, block, bh_result,
643 create | GET_BLOCK_READ_DIRECT);
652 reiserfs_transaction_running(inode->
i_sb))
666 if ((new_offset + inode->
i_sb->s_blocksize - 1) > inode->
i_size) {
691 if (allocation_needed
692 (retval, allocated_block_nr, ih, item, pos_in_item)) {
700 _allocate_block(th, block, inode, &allocated_block_nr,
709 retval = restart_transaction(th, inode, &path);
713 _allocate_block(th, block, inode,
714 &allocated_block_nr,
NULL, create);
732 if (indirect_item_found(retval, ih)) {
747 set_buffer_new(bh_result);
748 if (buffer_dirty(bh_result)
752 unfm_ptr = allocated_block_nr;
754 reiserfs_update_sd(th, inode);
756 set_block_dev_mapped(bh_result, unfm_ptr, inode);
781 if (is_statdata_le_ih(ih)) {
790 if (cpu_key_k_offset(&key) == 1) {
794 set_block_dev_mapped(bh_result,
795 allocated_block_nr, inode);
796 set_buffer_new(bh_result);
800 set_cpu_key_k_offset(&tmp_key, 1);
805 inode, (
char *)&unp);
808 allocated_block_nr, 1);
812 }
else if (is_direct_le_ih(ih)) {
817 ((le_ih_k_offset(ih) -
818 1) & ~(inode->
i_sb->s_blocksize - 1)) + 1;
819 if (tail_offset == cpu_key_k_offset(&key)) {
823 set_block_dev_mapped(bh_result,
824 allocated_block_nr, inode);
848 convert_tail_for_hole(inode, bh_result,
854 "convert tail failed "
855 "inode %lu, error %d",
858 if (allocated_block_nr) {
878 allocated_block_nr, 1);
889 set_buffer_uptodate(unbh);
916 unp_t unf_single = 0;
919 __u64 max_to_insert =
925 "vs-804: invalid position for append");
928 le_key_k_offset(version,
931 inode->
i_sb->s_blocksize),
935 RFALSE(cpu_key_k_offset(&tmp_key) > cpu_key_k_offset(&key),
936 "green-805: invalid offset");
939 ((cpu_key_k_offset(&key) -
940 cpu_key_k_offset(&tmp_key)) >> inode->
i_sb->
943 if (blocks_needed == 1) {
953 if (blocks_needed <= max_to_insert) {
956 un[blocks_needed - 1] =
958 set_block_dev_mapped(bh_result,
959 allocated_block_nr, inode);
960 set_buffer_new(bh_result);
967 max_to_insert ? max_to_insert : 1;
975 if (blocks_needed != 1)
980 allocated_block_nr, 1);
988 inode->
i_sb->s_blocksize * blocks_needed;
1004 retval = restart_transaction(th, inode, &path);
1013 if (need_resched()) {
1026 "%K should not be found", &key);
1028 if (allocated_block_nr)
1030 allocated_block_nr, 1);
1043 if (th && (!dangle || (retval && !th->
t_trans_id))) {
1046 reiserfs_update_sd(th, inode);
1067 static int real_space_diff(
struct inode *inode,
int sd_size)
1070 loff_t blocksize = inode->
i_sb->s_blocksize;
1090 static inline loff_t to_real_used_space(
struct inode *inode,
ulong blocks,
1095 (loff_t) (real_space_diff(inode, sd_size));
1097 return ((loff_t) real_space_diff(inode, sd_size)) +
1098 (((loff_t) blocks) << 9);
1102 static inline ulong to_fake_used_blocks(
struct inode *inode,
int sd_size)
1105 loff_t real_space = real_space_diff(inode, sd_size);
1109 bytes += (loff_t) 511;
1116 if (bytes < real_space)
1118 return (bytes - real_space) >> 9;
1129 static void init_inode(
struct inode *inode,
struct treepath *path)
1131 struct buffer_head *bh;
1141 INIT_LIST_HEAD(&(REISERFS_I(inode)->i_prealloc_list));
1142 REISERFS_I(inode)->i_flags = 0;
1143 REISERFS_I(inode)->i_prealloc_block = 0;
1144 REISERFS_I(inode)->i_prealloc_count = 0;
1145 REISERFS_I(inode)->i_trans_id = 0;
1146 REISERFS_I(inode)->i_jl =
NULL;
1147 reiserfs_init_xattr_rwsem(inode);
1152 unsigned long blocks;
1170 blocks = (inode->
i_size + 511) >> 9;
1182 REISERFS_I(inode)->i_first_direct_byte =
1191 to_real_used_space(inode, inode->
i_blocks,
1224 REISERFS_I(inode)->i_first_direct_byte = 0;
1227 to_real_used_space(inode, inode->
i_blocks,
1254 static void inode2sd(
void *sd,
struct inode *inode, loff_t
size)
1272 flags = REISERFS_I(inode)->i_attrs;
1278 static void inode2sd_v1(
void *sd,
struct inode *inode, loff_t size)
1298 REISERFS_I(inode)->i_first_direct_byte);
1304 static void update_stat_data(
struct treepath *path,
struct inode *inode,
1307 struct buffer_head *bh;
1313 if (!is_statdata_le_ih(ih))
1319 inode2sd_v1(
B_I_PITEM(bh, ih), inode, size);
1321 inode2sd(
B_I_PITEM(bh, ih), inode, size);
1328 struct inode *inode, loff_t size)
1332 struct buffer_head *bh;
1347 "i/o failure occurred trying to "
1348 "update %K stat data", &key);
1359 "stat data of object %k (nlink == %d) "
1360 "not found (pos %d)",
1383 update_stat_data(&path, inode, size);
1395 static void reiserfs_make_bad_inode(
struct inode *inode)
1421 unsigned long dirino;
1424 dirino = args->
dirid;
1438 "i/o failure occurred trying to find "
1439 "stat data of %K", &key);
1440 reiserfs_make_bad_inode(inode);
1446 reiserfs_make_bad_inode(inode);
1451 init_inode(inode, &path_to_sd);
1468 !REISERFS_SB(inode->
i_sb)->s_is_unlinked_ok) {
1470 "dead inode read from disk %K. "
1471 "This is likely to be race with knfsd. Ignore",
1473 reiserfs_make_bad_inode(inode);
1482 cache_no_acl(inode);
1509 struct inode *
inode;
1540 struct inode *
inode;
1543 key.on_disk_key.k_dir_id = dir_id;
1546 if (inode && !IS_ERR(inode) && generation != 0 &&
1557 int fh_len,
int fh_type)
1571 if (fh_type > fh_len) {
1572 if (fh_type != 6 || fh_len != 5)
1574 "nfsd/reiserfs, fhtype=%d, len=%d - odd",
1581 return reiserfs_get_dentry(sb, fid->
raw[0], fid->
raw[1],
1582 (fh_type == 3 || fh_type >= 5) ? fid->
raw[2] : 0);
1586 int fh_len,
int fh_type)
1588 if (fh_type > fh_len)
1593 return reiserfs_get_dentry(sb,
1594 (fh_type >= 5) ? fid->
raw[3] : fid->
raw[2],
1595 (fh_type >= 5) ? fid->
raw[4] : fid->
raw[3],
1596 (fh_type == 6) ? fid->
raw[5] : 0);
1600 struct inode *parent)
1604 if (parent && (maxlen < 5)) {
1607 }
else if (maxlen < 3) {
1612 data[0] = inode->
i_ino;
1617 data[3] = parent->
i_ino;
1638 int jbegin_count = 1;
1650 reiserfs_update_sd(&th, inode);
1661 struct inode *inode,
1667 char *
body = empty_dir;
1702 "i/o failure occurred creating new directory");
1708 "object with this key exists (%k)",
1721 struct treepath *path,
const char *symname,
1742 "i/o failure occurred creating new symlink");
1748 "object with this key exists (%k)",
1767 struct inode *dir,
umode_t mode,
const char *symname,
1771 struct inode *inode,
1800 if (!ih.
ih_key.k_objectid) {
1826 #if defined( USE_INODE_GENERATION_COUNTER )
1828 le32_to_cpu(REISERFS_SB(sb)->s_rs->s_inode_generation);
1846 REISERFS_I(inode)->i_first_direct_byte =
S_ISLNK(mode) ? 1 :
1849 INIT_LIST_HEAD(&(REISERFS_I(inode)->i_prealloc_list));
1850 REISERFS_I(inode)->i_flags = 0;
1851 REISERFS_I(inode)->i_prealloc_block = 0;
1852 REISERFS_I(inode)->i_prealloc_count = 0;
1853 REISERFS_I(inode)->i_trans_id = 0;
1854 REISERFS_I(inode)->i_jl =
NULL;
1855 REISERFS_I(inode)->i_attrs =
1858 reiserfs_init_xattr_rwsem(inode);
1877 if (i_uid_read(inode) & ~0xffff || i_gid_read(inode) & ~0xffff) {
1883 inode2sd_v1(&sd, inode, inode->
i_size);
1885 inode2sd(&sd, inode, inode->
i_size);
1900 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1901 if (REISERFS_I(dir)->new_packing_locality)
1912 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1914 REISERFS_I(dir)->new_packing_locality = 0;
1919 reiserfs_new_directory(th, inode, &ih, &path_to_key, dir);
1927 reiserfs_new_symlink(th, inode, &ih, &path_to_key, symname,
1934 goto out_inserted_sd;
1943 goto out_inserted_sd;
1947 "ACLs aren't enabled in the fs, "
1948 "but vfs thinks they are!");
1952 if (security->
name) {
1961 goto out_inserted_sd;
1965 reiserfs_update_sd(th, inode);
2011 static int grab_tail_page(
struct inode *inode,
2012 struct page **page_result,
2013 struct buffer_head **bh_result)
2019 unsigned long index = (inode->
i_size - 1) >> PAGE_CACHE_SHIFT;
2020 unsigned long pos = 0;
2021 unsigned long start = 0;
2022 unsigned long blocksize = inode->
i_sb->s_blocksize;
2024 struct buffer_head *bh;
2025 struct buffer_head *
head;
2034 if ((offset & (blocksize - 1)) == 0) {
2037 page = grab_cache_page(inode->
i_mapping, index);
2043 start = (offset / blocksize) * blocksize;
2046 reiserfs_get_block_create_0);
2050 head = page_buffers(page);
2056 bh = bh->b_this_page;
2058 }
while (bh != head);
2060 if (!buffer_uptodate(bh)) {
2067 "error reading block %lu", bh->b_blocknr);
2072 *page_result =
page;
2094 unsigned blocksize = inode->
i_sb->s_blocksize;
2096 struct page *page =
NULL;
2098 struct buffer_head *bh =
NULL;
2105 error = grab_tail_page(inode, &page, &bh);
2112 "grab_tail_page failed %d",
2134 if (update_timestamps)
2151 if (update_timestamps) {
2158 length = offset & (blocksize - 1);
2161 length = blocksize -
length;
2162 zero_user(page, offset, length);
2163 if (buffer_mapped(bh) && bh->b_blocknr != 0) {
2185 static int map_block_for_writepage(
struct inode *inode,
2186 struct buffer_head *bh_result,
2187 unsigned long block)
2193 struct buffer_head *bh;
2199 loff_t
byte_offset = ((loff_t)block << inode->
i_sb->s_blocksize_bits)+1;
2201 int use_get_block = 0;
2202 int bytes_copied = 0;
2204 int trans_running = 0;
2209 if (!buffer_uptodate(bh_result)) {
2213 kmap(bh_result->b_page);
2231 if (indirect_item_found(retval, ih)) {
2232 if (bytes_copied > 0) {
2234 "bytes_copied %d", bytes_copied);
2241 set_block_dev_mapped(bh_result,
2243 }
else if (is_direct_le_ih(ih)) {
2252 if (!trans_running) {
2279 bytes_copied += copy_size;
2280 set_block_dev_mapped(bh_result, 0, inode);
2283 if (bytes_copied < bh_result->b_size &&
2284 (byte_offset + bytes_copied) < inode->
i_size) {
2285 set_cpu_key_k_offset(&
key,
2286 cpu_key_k_offset(&
key) +
2292 "bad item inode %lu", inode->
i_ino);
2300 if (trans_running) {
2309 if (use_get_block) {
2314 if (!buffer_mapped(bh_result)
2315 || bh_result->b_blocknr == 0) {
2322 kunmap(bh_result->b_page);
2324 if (!retval && buffer_mapped(bh_result) && bh_result->b_blocknr == 0) {
2328 lock_buffer(bh_result);
2329 clear_buffer_dirty(bh_result);
2340 static int reiserfs_write_full_page(
struct page *page,
2343 struct inode *inode = page->
mapping->host;
2346 unsigned long block;
2348 struct buffer_head *
head, *bh;
2351 int checked = PageChecked(page);
2369 if (!page_has_buffers(page)) {
2371 (1 << BH_Dirty) | (1 << BH_Uptodate));
2373 head = page_buffers(page);
2378 if (page->
index >= end_index) {
2379 unsigned last_offset;
2383 if (page->
index >= end_index + 1 || !last_offset) {
2391 last_block = (i_size_read(inode) - 1) >> inode->
i_blkbits;
2394 if (block > last_block) {
2400 clear_buffer_dirty(bh);
2401 set_buffer_uptodate(bh);
2402 }
else if ((checked || buffer_dirty(bh)) &&
2403 (!buffer_mapped(bh) || (buffer_mapped(bh)
2410 if ((error = map_block_for_writepage(inode, bh, block))) {
2414 bh = bh->b_this_page;
2416 }
while (bh != head);
2425 ClearPageChecked(page);
2437 if (!buffer_mapped(bh))
2439 if (buffer_mapped(bh) && bh->b_blocknr == 0)
2453 if (!trylock_buffer(bh)) {
2458 if (test_clear_buffer_dirty(bh)) {
2463 }
while ((bh = bh->b_this_page) != head);
2471 BUG_ON(PageWriteback(page));
2472 set_page_writeback(page);
2481 struct buffer_head *next = bh->b_this_page;
2482 if (buffer_async_write(bh)) {
2488 }
while (bh != head);
2501 if (!buffer_uptodate(bh)) {
2505 bh = bh->b_this_page;
2506 }
while (bh != head);
2508 SetPageUptodate(page);
2518 ClearPageUptodate(page);
2522 if (buffer_mapped(bh) && buffer_dirty(bh) && bh->b_blocknr) {
2530 clear_buffer_dirty(bh);
2532 bh = bh->b_this_page;
2533 }
while (bh != head);
2535 BUG_ON(PageWriteback(page));
2536 set_page_writeback(page);
2539 struct buffer_head *next = bh->b_this_page;
2540 if (buffer_async_write(bh)) {
2541 clear_buffer_dirty(bh);
2547 }
while (bh != head);
2551 static int reiserfs_readpage(
struct file *
f,
struct page *page)
2558 struct inode *inode = page->
mapping->host;
2560 return reiserfs_write_full_page(page, wbc);
2563 static void reiserfs_truncate_failed_write(
struct inode *inode)
2569 static int reiserfs_write_begin(
struct file *
file,
2571 loff_t pos,
unsigned len,
unsigned flags,
2572 struct page **pagep,
void **fsdata)
2574 struct inode *
inode;
2580 inode = mapping->
host;
2583 (pos & (inode->
i_sb->s_blocksize - 1)) == 0) {
2585 *fsdata = (
void *)(
unsigned long)
flags;
2595 fix_tail_page_for_writing(page);
2596 if (reiserfs_transaction_running(inode->
i_sb)) {
2606 if (ret && reiserfs_transaction_running(inode->
i_sb)) {
2635 reiserfs_truncate_failed_write(inode);
2642 struct inode *inode = page->
mapping->host;
2650 fix_tail_page_for_writing(page);
2651 if (reiserfs_transaction_running(inode->
i_sb)) {
2662 if (ret && reiserfs_transaction_running(inode->
i_sb)) {
2696 static int reiserfs_write_end(
struct file *file,
struct address_space *mapping,
2697 loff_t pos,
unsigned len,
unsigned copied,
2698 struct page *page,
void *fsdata)
2700 struct inode *inode = page->
mapping->host;
2712 if (reiserfs_transaction_running(inode->
i_sb))
2719 if (!PageUptodate(page))
2732 if (pos + copied > inode->
i_size) {
2743 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
2750 inode->
i_size = pos + copied;
2756 mark_inode_dirty(inode);
2757 reiserfs_update_sd(&myth, inode);
2769 mark_inode_dirty(inode);
2781 if (pos + len > inode->
i_size)
2782 reiserfs_truncate_failed_write(inode);
2784 return ret == 0 ? copied :
ret;
2791 reiserfs_update_sd(th, inode);
2798 unsigned from,
unsigned to)
2800 struct inode *inode = page->
mapping->host;
2801 loff_t pos = ((loff_t) page->
index << PAGE_CACHE_SHIFT) + to;
2810 if (reiserfs_transaction_running(inode->
i_sb)) {
2819 if (pos > inode->
i_size) {
2828 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
2841 mark_inode_dirty(inode);
2842 reiserfs_update_sd(&myth, inode);
2850 mark_inode_dirty(inode);
2862 reiserfs_update_sd(th, inode);
2920 static int invalidatepage_can_drop(
struct inode *inode,
struct buffer_head *bh)
2927 if (!buffer_mapped(bh)) {
2933 if (reiserfs_file_data_log(inode)) {
2938 if (buffer_journaled(bh) || buffer_journal_dirty(bh)) {
2941 }
else if (buffer_dirty(bh)) {
2958 if (jh && (jl = jh->
jl)
2963 if (ret && bh->b_private) {
2972 static void reiserfs_invalidatepage(
struct page *page,
unsigned long offset)
2974 struct buffer_head *
head, *bh, *
next;
2975 struct inode *inode = page->
mapping->host;
2976 unsigned int curr_off = 0;
2979 BUG_ON(!PageLocked(page));
2982 ClearPageChecked(page);
2984 if (!page_has_buffers(page))
2987 head = page_buffers(page);
2990 unsigned int next_off = curr_off + bh->b_size;
2991 next = bh->b_this_page;
2996 if (offset <= curr_off) {
2997 if (invalidatepage_can_drop(inode, bh))
3002 curr_off = next_off;
3004 }
while (bh != head);
3011 if (!offset && ret) {
3019 static int reiserfs_set_page_dirty(
struct page *page)
3021 struct inode *inode = page->
mapping->host;
3022 if (reiserfs_file_data_log(inode)) {
3023 SetPageChecked(page);
3038 static int reiserfs_releasepage(
struct page *page,
gfp_t unused_gfp_flags)
3040 struct inode *inode = page->
mapping->host;
3042 struct buffer_head *
head;
3043 struct buffer_head *bh;
3048 head = page_buffers(page);
3051 if (bh->b_private) {
3052 if (!buffer_dirty(bh) && !buffer_locked(bh)) {
3059 bh = bh->b_this_page;
3060 }
while (bh != head);
3070 const struct iovec *iov, loff_t offset,
3071 unsigned long nr_segs)
3073 struct file *file = iocb->
ki_filp;
3074 struct inode *inode = file->
f_mapping->host;
3077 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
3078 reiserfs_get_blocks_direct_io);
3085 loff_t isize = i_size_read(inode);
3086 loff_t
end = offset + iov_length(iov, nr_segs);
3097 struct inode *inode = dentry->
d_inode;
3098 unsigned int ia_valid;
3109 if (is_quota_modification(inode, attr))
3127 if (REISERFS_I(inode)->i_prealloc_count > 0) {
3189 mark_inode_dirty(inode);
3203 attr->
ia_size != i_size_read(inode))
3208 mark_inode_dirty(inode);
3224 .writepage = reiserfs_writepage,
3225 .readpage = reiserfs_readpage,
3226 .readpages = reiserfs_readpages,
3227 .releasepage = reiserfs_releasepage,
3228 .invalidatepage = reiserfs_invalidatepage,
3229 .write_begin = reiserfs_write_begin,
3230 .write_end = reiserfs_write_end,
3231 .bmap = reiserfs_aop_bmap,
3232 .direct_IO = reiserfs_direct_IO,
3233 .set_page_dirty = reiserfs_set_page_dirty,