51 #include <linux/time.h>
52 #include <linux/string.h>
63 "PAP-1010: block (%b) has too big level (%z)", bh, bh);
88 if (n < cpu_key->on_disk_key.k_dir_id)
93 if (n < cpu_key->on_disk_key.k_objectid)
105 static inline int comp_keys(
const struct reiserfs_key *le_key,
113 if (le_key_k_offset(le_key_version(le_key), le_key) <
114 cpu_key_k_offset(cpu_key))
116 if (le_key_k_offset(le_key_version(le_key), le_key) >
117 cpu_key_k_offset(cpu_key))
124 if (le_key_k_type(le_key_version(le_key), le_key) <
125 cpu_key_k_type(cpu_key))
128 if (le_key_k_type(le_key_version(le_key), le_key) >
129 cpu_key_k_type(cpu_key))
138 __u32 *k1_u32, *k2_u32;
141 k1_u32 = (
__u32 *) key1;
142 k2_u32 = (
__u32 *) key2;
143 for (; key_length--; ++k1_u32, ++k2_u32) {
159 version = le_key_version(from);
161 to->
on_disk_key.k_offset = le_key_k_offset(version, from);
162 to->
on_disk_key.k_type = le_key_k_type(version, from);
187 static inline int bin_search(
const void *
key,
201 int rbound, lbound,
j;
203 for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
204 lbound <= rbound; j = (rbound + lbound) / 2)
207 (
struct cpu_key *)key)) {
245 struct buffer_head *
parent;
248 "PAP-5010: invalid offset in the path");
255 "PAP-5020: parent is not uptodate");
271 path_offset + 1)->b_blocknr)
289 struct buffer_head *parent;
292 "PAP-5030: invalid offset in the path");
298 "PAP-5040: parent is not uptodate");
314 path_offset + 1)->b_blocknr)
332 static inline int key_in_buffer(
struct treepath *chk_path,
333 const struct cpu_key *key,
340 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
343 "PAP-5060: device must not be NODEV");
345 if (comp_keys(get_lkey(chk_path, sb), key) == 1)
349 if (comp_keys(
get_rkey(chk_path, sb), key) != 1)
358 "path not properly relsed");
371 "clm-4000: invalid path offset");
374 struct buffer_head *bh;
388 "PAP-5090: invalid path offset");
396 static int is_leaf(
char *
buf,
int blocksize,
struct buffer_head *bh)
408 "this should be caught earlier");
416 "nr_item seems wrong: %z", bh);
424 "free space seems wrong: %z", bh);
432 prev_location = blocksize;
433 for (i = 0; i <
nr; i++, ih++) {
436 "wrong item type for item %h",
443 "item location seems wrong: %h",
450 "item length seems wrong: %h",
456 "item location seems wrong "
457 "(second one): %h", ih);
468 static int is_internal(
char *buf,
int blocksize,
struct buffer_head *bh)
476 if (nr <= DISK_LEAF_NODE_LEVEL || nr >
MAX_HEIGHT) {
479 "this should be caught earlier");
487 "number of key seems wrong: %z", bh);
494 "free space seems wrong: %z", bh);
503 static int is_tree_node(
struct buffer_head *bh,
int level)
507 "not match to the expected one %d",
512 return is_leaf(bh->b_data, bh->b_size, bh);
514 return is_internal(bh->b_data, bh->b_size, bh);
517 #define SEARCH_BY_KEY_READA 16
530 struct buffer_head **bh,
534 bool unlocked =
false;
536 for (i = 0; i < num; i++) {
537 bh[
i] = sb_getblk(s, b[i]);
546 for (j = 0; j <
i; j++) {
551 if (!buffer_uptodate(bh[j])) {
599 struct buffer_head *bh;
602 int right_neighbor_of_leaf_node;
608 #ifdef CONFIG_REISERFS_CHECK
609 int repeat_counter = 0;
620 right_neighbor_of_leaf_node = 0;
629 #ifdef CONFIG_REISERFS_CHECK
630 if (!(++repeat_counter % 50000))
632 "%s: there were %d iterations of "
633 "while loop looking for key %K",
647 sb_getblk(sb, block_number))) {
648 bool unlocked =
false;
650 if (!buffer_uptodate(bh) && reada_count > 1)
652 unlocked = search_by_key_reada(sb, reada_bh,
653 reada_blocks, reada_count);
659 if (!buffer_uptodate(bh) && !unlocked) {
668 if (!buffer_uptodate(bh))
677 if (expected_level == -1)
686 B_LEVEL(bh) != expected_level ||
687 !key_in_buffer(search_path, key, sb))) {
691 sbk_restarted[expected_level - 1]);
698 right_neighbor_of_leaf_node = 0;
707 RFALSE(comp_keys(&MAX_KEY, key) &&
708 !key_in_buffer(search_path, key, sb),
709 "PAP-5130: key is not in the buffer");
710 #ifdef CONFIG_REISERFS_CHECK
711 if (REISERFS_SB(sb)->
cur_tb) {
714 "schedule occurred in do_balance!");
720 if (!is_tree_node(bh, expected_level)) {
722 "invalid format found in block %ld. "
723 "Fsck?", bh->b_blocknr);
733 RFALSE(node_level < stop_level,
734 "vs-5152: tree level (%d) is less than stop level (%d)",
735 node_level, stop_level);
743 if (node_level == stop_level) {
774 reada_blocks[reada_count++] =
811 const struct cpu_key *p_cpu_key,
817 loff_t item_offset,
offset;
838 "PAP-5165: item length equals zero");
845 "PAP-5170: position equals zero");
858 item_offset = le_ih_k_offset(p_le_ih);
859 offset = cpu_key_k_offset(p_cpu_key);
862 if (item_offset <= offset &&
865 if (is_indirect_le_ih(p_le_ih)) {
873 if (is_indirect_le_ih(p_le_ih))
904 #define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
907 #define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
910 static inline int prepare_for_direct_item(
struct treepath *
path,
913 loff_t new_file_length,
int *cut_size)
917 if (new_file_length == max_reiserfs_offset(inode)) {
925 round_len =
ROUND_UP(new_file_length);
927 if (round_len < le_ih_k_offset(le_ih)) {
932 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
940 if (new_file_length < le_ih_k_offset(le_ih)) {
947 new_file_length + 1 - le_ih_k_offset(le_ih)));
951 static inline int prepare_for_direntry_item(
struct treepath *
path,
954 loff_t new_file_length,
958 new_file_length == max_reiserfs_offset(inode)) {
960 "PAP-5220: incorrect empty directory item (%h)", le_ih);
979 #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
988 int *cut_size,
unsigned long long new_file_length
998 if (is_statdata_le_ih(p_le_ih)) {
1000 RFALSE(new_file_length != max_reiserfs_offset(inode),
1001 "PAP-5210: mode must be M_DELETE");
1008 if (is_direntry_le_ih(p_le_ih))
1009 return prepare_for_direntry_item(path, p_le_ih, inode,
1014 if (is_direct_le_ih(p_le_ih))
1015 return prepare_for_direct_item(path, p_le_ih, inode,
1016 new_file_length, cut_size);
1027 if ( new_file_length == max_reiserfs_offset (inode) ) {
1030 new_file_length = 0;
1041 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
1048 if (!
delete && (*cut_size) != 0 &&
1085 }
while (need_re_search &&
1089 if (*cut_size == 0) {
1104 if (is_statdata_le_ih(p_le_ih))
1110 if (is_direntry_le_ih(p_le_ih)) {
1117 if (is_indirect_le_ih(p_le_ih))
1144 for (i = total_length; i >
length;)
1148 #ifdef REISERQUOTA_DEBUG
1151 if (is_direntry_le_key(2, ih))
1153 if (is_direct_le_key(2, ih))
1155 if (is_indirect_le_key(2, ih))
1157 if (is_statdata_le_key(2, ih))
1164 if (is_direntry_le_ih(ih))
1166 if (is_direct_le_ih(ih))
1168 if (is_indirect_le_ih(ih))
1170 if (is_statdata_le_ih(ih))
1184 struct treepath *path,
const struct cpu_key *item_key,
1185 struct inode *inode,
struct buffer_head *un_bh)
1191 int quota_cut_bytes;
1192 int ret_value, del_size, removed;
1194 #ifdef CONFIG_REISERFS_CHECK
1201 init_tb_struct(th, &s_del_balance, sb, path,
1207 #ifdef CONFIG_REISERFS_CHECK
1211 prepare_for_delete_or_cut(th, inode, path,
1214 max_reiserfs_offset(inode));
1234 "no items of the file %K found",
1245 ret_value = calc_deleted_bytes_number(&s_del_balance,
M_DELETE);
1256 if ((le_ih_k_offset(q_ih) & (sb->
s_blocksize - 1)) == 1) {
1259 quota_cut_bytes = 0;
1297 #ifdef REISERQUOTA_DEBUG
1299 "reiserquota delete_item(): freeing %u, id=%u type=%c",
1300 quota_cut_bytes, inode->
i_uid, head2type(&s_ih));
1302 dquot_free_space_nodirty(inode, quota_cut_bytes);
1332 struct cpu_key cpu_key;
1334 int quota_cut_bytes = 0;
1344 "i/o failure occurred trying "
1345 "to delete %K", &cpu_key);
1352 ((
unsigned long long)
1354 (le_key_version(key), key)) == 0
1355 && (
unsigned long long)
1357 (le_key_version(key),
1360 "%k not found", key);
1366 init_tb_struct(th, &tb, th->
t_super, &path,
1380 #ifdef REISERQUOTA_DEBUG
1382 "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1383 quota_cut_bytes, inode->
i_uid,
1386 dquot_free_space_nodirty(inode,
1393 "could not delete %K due to fix_nodes failure",
1403 struct inode *inode)
1415 #if defined( USE_INODE_GENERATION_COUNTER )
1417 __le32 *inode_generation;
1420 &REISERFS_SB(th->
t_super)->s_rs->s_inode_generation;
1421 le32_add_cpu(inode_generation, 1);
1430 static void unmap_buffers(
struct page *
page, loff_t pos)
1432 struct buffer_head *bh;
1433 struct buffer_head *
head;
1434 struct buffer_head *
next;
1435 unsigned long tail_index;
1436 unsigned long cur_index;
1439 if (page_has_buffers(page)) {
1442 head = page_buffers(page);
1445 next = bh->b_this_page;
1453 cur_index += bh->b_size;
1454 if (cur_index > tail_index) {
1458 }
while (bh != head);
1464 struct inode *inode,
1467 const struct cpu_key *item_key,
1468 loff_t new_file_size,
char *mode)
1486 block_size - (new_file_size & (block_size - 1));
1494 new_file_size, mode);
1502 struct inode *inode,
struct treepath *path)
1504 struct cpu_key tail_key;
1510 tail_key.key_length = 4;
1513 (cpu_key_k_offset(&tail_key) & (inode->
i_sb->s_blocksize - 1)) - 1;
1519 "found invalid item");
1522 "vs-5616: appended bytes found");
1529 || removed > tail_len,
1530 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1532 tail_len -= removed;
1533 set_cpu_key_k_offset(&tail_key,
1534 cpu_key_k_offset(&tail_key) - removed);
1537 "conversion has been rolled back due to "
1538 "lack of disk space");
1540 mark_inode_dirty(inode);
1546 struct cpu_key *item_key,
1547 struct inode *inode,
1548 struct page *page, loff_t new_file_size)
1559 is_inode_locked = 0;
1562 int quota_cut_bytes;
1563 loff_t tail_pos = 0;
1567 init_tb_struct(th, &s_cut_balance, inode->
i_sb, path,
1579 prepare_for_delete_or_cut(th, inode, path,
1581 &cut_size, new_file_size);
1586 "PAP-5570: can not convert twice");
1589 maybe_indirect_to_direct(th, inode, page,
1591 new_file_size, &mode);
1596 is_inode_locked = 1;
1600 retval2 = ret_value;
1611 tail_pos = new_file_size;
1612 set_cpu_key_k_offset(item_key, new_file_size + 1);
1620 "convert does not exist (%K)",
1625 if (cut_size == 0) {
1651 if (is_inode_locked) {
1654 indirect_to_direct_roll_back(th, inode, path);
1673 ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
1675 ret_value = retval2;
1683 (le_ih_k_offset(p_le_ih) & (sb->
s_blocksize - 1)) ==
1686 REISERFS_I(inode)->i_first_direct_byte =
U32_MAX;
1689 quota_cut_bytes = 0;
1692 #ifdef CONFIG_REISERFS_CHECK
1693 if (is_inode_locked) {
1699 if (!is_indirect_le_ih(le_ih))
1701 "item must be indirect %h", le_ih);
1705 "indirect2direct conversion indirect "
1706 "item %h being deleted must be of "
1707 "4 byte long", le_ih);
1712 "indirect2direct conversion of %h "
1713 "(CUT, insert_size==%d)",
1722 if (is_inode_locked) {
1728 unmap_buffers(page, tail_pos);
1731 #ifdef REISERQUOTA_DEBUG
1733 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1734 quota_cut_bytes, inode->
i_uid,
'?');
1736 dquot_free_space_nodirty(inode, quota_cut_bytes);
1741 struct inode *inode)
1750 reiserfs_update_sd(th, inode);
1758 struct inode *inode,
1760 int update_timestamps
1768 struct cpu_key s_item_key;
1783 truncate_directory(th, inode);
1788 new_file_size = inode->
i_size;
1791 make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
1799 "i/o failure occurred trying to truncate %K",
1806 "wrong result %d of search for %K", retval,
1813 s_search_path.pos_in_item--;
1817 if (is_statdata_le_ih(p_le_ih))
1820 loff_t
offset = le_ih_k_offset(p_le_ih);
1827 file_size = offset + bytes - 1;
1833 if (new_file_size == 0)
1836 if (file_size == 0 || file_size < new_file_size) {
1837 goto update_and_out;
1841 set_cpu_key_k_offset(&s_item_key, file_size);
1847 inode, page, new_file_size);
1850 "reiserfs_cut_from_item failed");
1855 RFALSE(deleted > file_size,
1856 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1857 deleted, file_size, &s_item_key);
1860 file_size -= deleted;
1862 set_cpu_key_k_offset(&s_item_key, file_size);
1878 if (update_timestamps) {
1882 reiserfs_update_sd(th, inode);
1893 }
while (file_size >
ROUND_UP(new_file_size) &&
1898 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1899 new_file_size, file_size, s_item_key.
on_disk_key.k_objectid);
1902 if (update_timestamps) {
1907 reiserfs_update_sd(th, inode);
1914 #ifdef CONFIG_REISERFS_CHECK
1916 static void check_research_for_paste(
struct treepath *path,
1917 const struct cpu_key *key)
1921 if (is_direct_le_ih(found_ih)) {
1922 if (le_ih_k_offset(found_ih) +
1925 cpu_key_k_offset(key)
1930 "%h or position (%d) does not match "
1931 "to key %K", found_ih,
1934 if (is_indirect_le_ih(found_ih)) {
1935 if (le_ih_k_offset(found_ih) +
1938 cpu_key_k_offset(key)
1942 "item (%h) or position (%d) does not "
1943 "match to key (%K)",
1951 const struct cpu_key *key,
1952 struct inode *inode,
1964 #ifdef REISERQUOTA_DEBUG
1966 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
1967 pasted_size, inode->
i_uid,
1972 retval = dquot_alloc_space_nodirty(inode, pasted_size);
1978 init_tb_struct(th, &s_paste_balance, th->
t_super, search_path,
1980 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
2004 "entry or pasted byte (%K) exists",
2009 #ifdef CONFIG_REISERFS_CHECK
2010 check_research_for_paste(search_path, key);
2024 #ifdef REISERQUOTA_DEBUG
2026 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
2027 pasted_size, inode->
i_uid,
2030 dquot_free_space_nodirty(inode, pasted_size);
2041 struct treepath *path,
const struct cpu_key *key,
2042 struct item_head *ih,
struct inode *inode,
2048 int quota_bytes = 0;
2061 #ifdef REISERQUOTA_DEBUG
2063 "reiserquota insert_item(): allocating %u id=%u type=%c",
2064 quota_bytes, inode->
i_uid, head2type(ih));
2069 retval = dquot_alloc_space_nodirty(inode, quota_bytes);
2076 init_tb_struct(th, &s_ins_balance, th->
t_super, path,
2078 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
2099 "key %K already exists in the tree",
2116 #ifdef REISERQUOTA_DEBUG
2118 "reiserquota insert_item(): freeing %u id=%u type=%c",
2119 quota_bytes, inode->
i_uid, head2type(ih));
2122 dquot_free_space_nodirty(inode, quota_bytes);