blob: 6b025a42d510be137280eb28117865c5b8354369 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5/*
6 * Written by Anatoly P. Pinchuk pap@namesys.botik.ru
7 * Programm System Institute
8 * Pereslavl-Zalessky Russia
9 */
10
11/*
12 * This file contains functions dealing with S+tree
13 *
14 * B_IS_IN_TREE
15 * copy_item_head
16 * comp_short_keys
17 * comp_keys
18 * comp_short_le_keys
19 * le_key2cpu_key
20 * comp_le_keys
21 * bin_search
22 * get_lkey
23 * get_rkey
24 * key_in_buffer
25 * decrement_bcount
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * reiserfs_check_path
27 * pathrelse_and_restore
28 * pathrelse
29 * search_by_key_reada
30 * search_by_key
31 * search_for_position_by_key
32 * comp_items
33 * prepare_for_direct_item
34 * prepare_for_direntry_item
35 * prepare_for_delete_or_cut
36 * calc_deleted_bytes_number
37 * init_tb_struct
38 * padd_item
39 * reiserfs_delete_item
40 * reiserfs_delete_solid_item
41 * reiserfs_delete_object
42 * maybe_indirect_to_direct
43 * indirect_to_direct_roll_back
44 * reiserfs_cut_from_item
45 * truncate_directory
46 * reiserfs_do_truncate
47 * reiserfs_paste_into_item
48 * reiserfs_insert_item
49 */
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/time.h>
52#include <linux/string.h>
53#include <linux/pagemap.h>
54#include <linux/reiserfs_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/buffer_head.h>
56#include <linux/quotaops.h>
57
58/* Does the buffer contain a disk block which is in the tree. */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040059inline int B_IS_IN_TREE(const struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040062 RFALSE(B_LEVEL(bh) > MAX_HEIGHT,
63 "PAP-1010: block (%b) has too big level (%z)", bh, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -040065 return (B_LEVEL(bh) != FREE_LEVEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68//
69// to gets item head in le form
70//
Jeff Mahoneyd68caa92009-03-30 14:02:49 -040071inline void copy_item_head(struct item_head *to,
72 const struct item_head *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -040074 memcpy(to, from, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* k1 is pointer to on-disk structure which is stored in little-endian
78 form. k2 is pointer to cpu variable. For key of items of the same
79 object this returns 0.
Jeff Mahoney0222e652009-03-30 14:02:44 -040080 Returns: -1 if key1 < key2
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 0 if key1 == key2
82 1 if key1 > key2 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070083inline int comp_short_keys(const struct reiserfs_key *le_key,
84 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070086 __u32 n;
87 n = le32_to_cpu(le_key->k_dir_id);
88 if (n < cpu_key->on_disk_key.k_dir_id)
89 return -1;
90 if (n > cpu_key->on_disk_key.k_dir_id)
91 return 1;
92 n = le32_to_cpu(le_key->k_objectid);
93 if (n < cpu_key->on_disk_key.k_objectid)
94 return -1;
95 if (n > cpu_key->on_disk_key.k_objectid)
96 return 1;
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* k1 is pointer to on-disk structure which is stored in little-endian
101 form. k2 is pointer to cpu variable.
102 Compare keys using all 4 key fields.
103 Returns: -1 if key1 < key2 0
104 if key1 = key2 1 if key1 > key2 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105static inline int comp_keys(const struct reiserfs_key *le_key,
106 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700108 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 retval = comp_short_keys(le_key, cpu_key);
111 if (retval)
112 return retval;
113 if (le_key_k_offset(le_key_version(le_key), le_key) <
114 cpu_key_k_offset(cpu_key))
115 return -1;
116 if (le_key_k_offset(le_key_version(le_key), le_key) >
117 cpu_key_k_offset(cpu_key))
118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700120 if (cpu_key->key_length == 3)
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700123 /* this part is needed only when tail conversion is in progress */
124 if (le_key_k_type(le_key_version(le_key), le_key) <
125 cpu_key_k_type(cpu_key))
126 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128 if (le_key_k_type(le_key_version(le_key), le_key) >
129 cpu_key_k_type(cpu_key))
130 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700135inline int comp_short_le_keys(const struct reiserfs_key *key1,
136 const struct reiserfs_key *key2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400138 __u32 *k1_u32, *k2_u32;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400139 int key_length = REISERFS_SHORT_KEY_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400141 k1_u32 = (__u32 *) key1;
142 k2_u32 = (__u32 *) key2;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400143 for (; key_length--; ++k1_u32, ++k2_u32) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400144 if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700145 return -1;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400146 if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 return 1;
148 }
149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700152inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154 int version;
155 to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
156 to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
157
158 // find out version of the key
159 version = le_key_version(from);
160 to->version = version;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165// this does not say which one is bigger, it only returns 1 if keys
166// are not equal, 0 otherwise
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167inline int comp_le_keys(const struct reiserfs_key *k1,
168 const struct reiserfs_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 return memcmp(k1, k2, sizeof(struct reiserfs_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/**************************************************************************
174 * Binary search toolkit function *
175 * Search for an item in the array by the item key *
176 * Returns: 1 if found, 0 if not found; *
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400177 * *pos = number of the searched element if found, else the *
178 * number of the first element that is larger than key. *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 **************************************************************************/
Jeff Mahoneyee939612009-03-30 14:02:50 -0400180/* For those not familiar with binary search: lbound is the leftmost item that it
181 could be, rbound the rightmost item that it could be. We examine the item
182 halfway between lbound and rbound, and that tells us either that we can increase
183 lbound, or decrease rbound, or that we have found it, or if lbound <= rbound that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 there are no possible items, and we have not found it. With each examination we
185 cut the number of possible items it could be by one more than half rounded down,
186 or we find it. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400187static inline int bin_search(const void *key, /* Key to search for. */
188 const void *base, /* First item in the array. */
189 int num, /* Number of items in the array. */
190 int width, /* Item size in the array.
191 searched. Lest the reader be
192 confused, note that this is crafted
193 as a general function, and when it
194 is applied specifically to the array
195 of item headers in a node, width
196 is actually the item header size not
197 the item size. */
198 int *pos /* Number of the searched for element. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700199 )
200{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400201 int rbound, lbound, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Jeff Mahoneyee939612009-03-30 14:02:50 -0400203 for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
204 lbound <= rbound; j = (rbound + lbound) / 2)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 switch (comp_keys
Jeff Mahoneyee939612009-03-30 14:02:50 -0400206 ((struct reiserfs_key *)((char *)base + j * width),
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400207 (struct cpu_key *)key)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700208 case -1:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400209 lbound = j + 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700210 continue;
211 case 1:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400212 rbound = j - 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700213 continue;
214 case 0:
Jeff Mahoneyee939612009-03-30 14:02:50 -0400215 *pos = j;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216 return ITEM_FOUND; /* Key found in the array. */
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700219 /* bin_search did not find given key, it returns position of key,
220 that is minimal and greater than the given one. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400221 *pos = lbound;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 return ITEM_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700226extern struct tree_balance *cur_tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/* Minimal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/* Maximal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233static const struct reiserfs_key MAX_KEY = {
Al Viro3e8962b2005-05-01 08:59:18 -0700234 __constant_cpu_to_le32(0xffffffff),
235 __constant_cpu_to_le32(0xffffffff),
236 {{__constant_cpu_to_le32(0xffffffff),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700237 __constant_cpu_to_le32(0xffffffff)},}
Al Viro3e8962b2005-05-01 08:59:18 -0700238};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom
241 of the path, and going upwards. We must check the path's validity at each step. If the key is not in
242 the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this
243 case we return a special key, either MIN_KEY or MAX_KEY. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400244static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
245 const struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400247 int position, path_offset = chk_path->path_length;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400248 struct buffer_head *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Jeff Mahoneyee939612009-03-30 14:02:50 -0400250 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700251 "PAP-5010: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 /* While not higher in path than first element. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400254 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700256 RFALSE(!buffer_uptodate
Jeff Mahoneyee939612009-03-30 14:02:50 -0400257 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700258 "PAP-5020: parent is not uptodate");
259
260 /* Parent at the path is not in the tree now. */
261 if (!B_IS_IN_TREE
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400262 (parent =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400263 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700264 return &MAX_KEY;
265 /* Check whether position in the parent is correct. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400266 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400267 PATH_OFFSET_POSITION(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400268 path_offset)) >
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400269 B_NR_ITEMS(parent))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700270 return &MAX_KEY;
271 /* Check whether parent at the path really points to the child. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400272 if (B_N_CHILD_NUM(parent, position) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400273 PATH_OFFSET_PBUFFER(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400274 path_offset + 1)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700275 return &MAX_KEY;
276 /* Return delimiting key if position in the parent is not equal to zero. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400277 if (position)
278 return B_N_PDELIM_KEY(parent, position - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700279 }
280 /* Return MIN_KEY if we are in the root of the buffer tree. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400281 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400282 b_blocknr == SB_ROOT_BLOCK(sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700283 return &MIN_KEY;
284 return &MAX_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/* Get delimiting key of the buffer at the path and its right neighbor. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400288inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400289 const struct super_block *sb)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700290{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400291 int position, path_offset = chk_path->path_length;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400292 struct buffer_head *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Jeff Mahoneyee939612009-03-30 14:02:50 -0400294 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700295 "PAP-5030: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Jeff Mahoneyee939612009-03-30 14:02:50 -0400297 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700299 RFALSE(!buffer_uptodate
Jeff Mahoneyee939612009-03-30 14:02:50 -0400300 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700301 "PAP-5040: parent is not uptodate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700303 /* Parent at the path is not in the tree now. */
304 if (!B_IS_IN_TREE
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400305 (parent =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400306 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700307 return &MIN_KEY;
308 /* Check whether position in the parent is correct. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400309 if ((position =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400310 PATH_OFFSET_POSITION(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400311 path_offset)) >
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400312 B_NR_ITEMS(parent))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 return &MIN_KEY;
314 /* Check whether parent at the path really points to the child. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400315 if (B_N_CHILD_NUM(parent, position) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400316 PATH_OFFSET_PBUFFER(chk_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -0400317 path_offset + 1)->b_blocknr)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700318 return &MIN_KEY;
319 /* Return delimiting key if position in the parent is not the last one. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400320 if (position != B_NR_ITEMS(parent))
321 return B_N_PDELIM_KEY(parent, position);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700322 }
323 /* Return MAX_KEY if we are in the root of the buffer tree. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400324 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400325 b_blocknr == SB_ROOT_BLOCK(sb))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700326 return &MAX_KEY;
327 return &MIN_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/* Check whether a key is contained in the tree rooted from a buffer at a path. */
331/* This works by looking at the left and right delimiting keys for the buffer in the last path_element in
332 the path. These delimiting keys are stored at least one level above that buffer in the tree. If the
333 buffer is the first or last node in the tree order then one of the delimiting keys may be absent, and in
334 this case get_lkey and get_rkey return a special key which is MIN_KEY or MAX_KEY. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400335static inline int key_in_buffer(struct treepath *chk_path, /* Path which should be checked. */
336 const struct cpu_key *key, /* Key which should be checked. */
337 struct super_block *sb
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700338 )
339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400341 RFALSE(!key || chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
342 || chk_path->path_length > MAX_HEIGHT,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700343 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400344 key, chk_path->path_length);
345 RFALSE(!PATH_PLAST_BUFFER(chk_path)->b_bdev,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 "PAP-5060: device must not be NODEV");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400348 if (comp_keys(get_lkey(chk_path, sb), key) == 1)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700349 /* left delimiting key is bigger, that the key we look for */
350 return 0;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400351 /* if ( comp_keys(key, get_rkey(chk_path, sb)) != -1 ) */
352 if (comp_keys(get_rkey(chk_path, sb), key) != 1)
353 /* key must be less than right delimitiing key */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700354 return 0;
355 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800358int reiserfs_check_path(struct treepath *p)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700359{
360 RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
361 "path not properly relsed");
362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400365/* Drop the reference to each buffer in a path and restore
366 * dirty bits clean when preparing the buffer for the log.
367 * This version should only be called from fix_nodes() */
368void pathrelse_and_restore(struct super_block *sb,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400369 struct treepath *search_path)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700370{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400371 int path_offset = search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Jeff Mahoneyee939612009-03-30 14:02:50 -0400373 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374 "clm-4000: invalid path offset");
375
Jeff Mahoneyee939612009-03-30 14:02:50 -0400376 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400377 struct buffer_head *bh;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400378 bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400379 reiserfs_restore_prepared_buffer(sb, bh);
380 brelse(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400382 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -0400385/* Drop the reference to each buffer in a path */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400386void pathrelse(struct treepath *search_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400388 int path_offset = search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Jeff Mahoneyee939612009-03-30 14:02:50 -0400390 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 "PAP-5090: invalid path offset");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Jeff Mahoneyee939612009-03-30 14:02:50 -0400393 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
394 brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400396 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700399static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
400{
401 struct block_head *blkh;
402 struct item_head *ih;
403 int used_space;
404 int prev_location;
405 int i;
406 int nr;
407
408 blkh = (struct block_head *)buf;
409 if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400410 reiserfs_warning(NULL, "reiserfs-5080",
411 "this should be caught earlier");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700412 return 0;
413 }
414
415 nr = blkh_nr_item(blkh);
416 if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
417 /* item number is too big or too small */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400418 reiserfs_warning(NULL, "reiserfs-5081",
419 "nr_item seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700420 return 0;
421 }
422 ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
423 used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
424 if (used_space != blocksize - blkh_free_space(blkh)) {
425 /* free space does not match to calculated amount of use space */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400426 reiserfs_warning(NULL, "reiserfs-5082",
427 "free space seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 return 0;
429 }
430 // FIXME: it is_leaf will hit performance too much - we may have
431 // return 1 here
432
433 /* check tables of item heads */
434 ih = (struct item_head *)(buf + BLKH_SIZE);
435 prev_location = blocksize;
436 for (i = 0; i < nr; i++, ih++) {
437 if (le_ih_k_type(ih) == TYPE_ANY) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400438 reiserfs_warning(NULL, "reiserfs-5083",
439 "wrong item type for item %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700440 ih);
441 return 0;
442 }
443 if (ih_location(ih) >= blocksize
444 || ih_location(ih) < IH_SIZE * nr) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400445 reiserfs_warning(NULL, "reiserfs-5084",
446 "item location seems wrong: %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700447 ih);
448 return 0;
449 }
450 if (ih_item_len(ih) < 1
451 || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400452 reiserfs_warning(NULL, "reiserfs-5085",
453 "item length seems wrong: %h",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700454 ih);
455 return 0;
456 }
457 if (prev_location - ih_location(ih) != ih_item_len(ih)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400458 reiserfs_warning(NULL, "reiserfs-5086",
459 "item location seems wrong "
460 "(second one): %h", ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700461 return 0;
462 }
463 prev_location = ih_location(ih);
464 }
465
466 // one may imagine much more checks
467 return 1;
468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470/* returns 1 if buf looks like an internal node, 0 otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700471static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700473 struct block_head *blkh;
474 int nr;
475 int used_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700477 blkh = (struct block_head *)buf;
478 nr = blkh_level(blkh);
479 if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
480 /* this level is not possible for internal nodes */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400481 reiserfs_warning(NULL, "reiserfs-5087",
482 "this should be caught earlier");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 return 0;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700486 nr = blkh_nr_item(blkh);
487 if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
488 /* for internal which is not root we might check min number of keys */
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400489 reiserfs_warning(NULL, "reiserfs-5088",
490 "number of key seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700491 return 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700494 used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
495 if (used_space != blocksize - blkh_free_space(blkh)) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400496 reiserfs_warning(NULL, "reiserfs-5089",
497 "free space seems wrong: %z", bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700498 return 0;
499 }
500 // one may imagine much more checks
501 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504// make sure that bh contains formatted node of reiserfs tree of
505// 'level'-th level
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700506static int is_tree_node(struct buffer_head *bh, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508 if (B_LEVEL(bh) != level) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400509 reiserfs_warning(NULL, "reiserfs-5090", "node level %d does "
510 "not match to the expected one %d",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700511 B_LEVEL(bh), level);
512 return 0;
513 }
514 if (level == DISK_LEAF_NODE_LEVEL)
515 return is_leaf(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700517 return is_internal(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#define SEARCH_BY_KEY_READA 16
521
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200522/*
523 * The function is NOT SCHEDULE-SAFE!
524 * It might unlock the write lock if we needed to wait for a block
525 * to be read. Note that in this case it won't recover the lock to avoid
526 * high contention resulting from too much lock requests, especially
527 * the caller (search_by_key) will perform other schedule-unsafe
528 * operations just after calling this function.
529 *
530 * @return true if we have unlocked
531 */
532static bool search_by_key_reada(struct super_block *s,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700533 struct buffer_head **bh,
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700534 b_blocknr_t *b, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700536 int i, j;
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200537 bool unlocked = false;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700538
539 for (i = 0; i < num; i++) {
540 bh[i] = sb_getblk(s, b[i]);
541 }
Frederic Weisbecker09eb47a2009-05-08 14:21:33 +0200542 /*
543 * We are going to read some blocks on which we
544 * have a reference. It's safe, though we might be
545 * reading blocks concurrently changed if we release
546 * the lock. But it's still fine because we check later
547 * if the tree changed
548 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700549 for (j = 0; j < i; j++) {
550 /*
551 * note, this needs attention if we are getting rid of the BKL
552 * you have to make sure the prepared bit isn't set on this buffer
553 */
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200554 if (!buffer_uptodate(bh[j])) {
555 if (!unlocked) {
556 reiserfs_write_unlock(s);
557 unlocked = true;
558 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700559 ll_rw_block(READA, 1, bh + j);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200560 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561 brelse(bh[j]);
562 }
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200563 return unlocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566/**************************************************************************
567 * Algorithm SearchByKey *
568 * look for item in the Disk S+Tree by its key *
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400569 * Input: sb - super block *
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400570 * key - pointer to the key to search *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * Output: ITEM_FOUND, ITEM_NOT_FOUND or IO_ERROR *
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400572 * search_path - path from the root to the needed leaf *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 **************************************************************************/
574
575/* This function fills up the path from the root to the leaf as it
576 descends the tree looking for the key. It uses reiserfs_bread to
577 try to find buffers in the cache given their block number. If it
578 does not find them in the cache it reads them from disk. For each
579 node search_by_key finds using reiserfs_bread it then uses
580 bin_search to look through that node. bin_search will find the
581 position of the block_number of the next node if it is looking
582 through an internal node. If it is looking through a leaf node
583 bin_search will find the position of the item which has key either
584 equal to given key, or which is the maximal key less than the given
585 key. search_by_key returns a path that must be checked for the
586 correctness of the top of the path but need not be checked for the
587 correctness of the bottom of the path */
588/* The function is NOT SCHEDULE-SAFE! */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400589int search_by_key(struct super_block *sb, const struct cpu_key *key, /* Key to search. */
590 struct treepath *search_path,/* This structure was
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700591 allocated and initialized
592 by the calling
593 function. It is filled up
594 by this function. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400595 int stop_level /* How far down the tree to search. To
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700596 stop at leaf level - set to
597 DISK_LEAF_NODE_LEVEL */
598 )
599{
Jeff Mahoneyee939612009-03-30 14:02:50 -0400600 b_blocknr_t block_number;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700601 int expected_level;
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400602 struct buffer_head *bh;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400603 struct path_element *last_element;
Jeff Mahoneyee939612009-03-30 14:02:50 -0400604 int node_level, retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700605 int right_neighbor_of_leaf_node;
606 int fs_gen;
607 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
Jeff Mahoney3ee16672007-10-18 23:39:25 -0700608 b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700609 int reada_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -0400612 int repeat_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400615 PROC_INFO_INC(sb, search_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 /* As we add each node to a path we increase its count. This means that
618 we must be careful to release all nodes in a path before we either
619 discard the path struct or re-use the path struct, as we do here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400621 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700622
623 right_neighbor_of_leaf_node = 0;
624
625 /* With each iteration of this loop we search through the items in the
626 current node, and calculate the next current node(next path element)
627 for the next iteration of this loop.. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400628 block_number = SB_ROOT_BLOCK(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700629 expected_level = -1;
630 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -0400633 if (!(++repeat_counter % 50000))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400634 reiserfs_warning(sb, "PAP-5100",
Jeff Mahoney45b03d52009-03-30 14:02:21 -0400635 "%s: there were %d iterations of "
636 "while loop looking for key %K",
Jeff Mahoneyee939612009-03-30 14:02:50 -0400637 current->comm, repeat_counter,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400638 key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#endif
640
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700641 /* prep path to have another element added to it. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400642 last_element =
643 PATH_OFFSET_PELEMENT(search_path,
644 ++search_path->path_length);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400645 fs_gen = get_generation(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700647 /* Read the next tree node, and set the last element in the path to
648 have a pointer to it. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400649 if ((bh = last_element->pe_buffer =
Jeff Mahoneyee939612009-03-30 14:02:50 -0400650 sb_getblk(sb, block_number))) {
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200651 bool unlocked = false;
652
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400653 if (!buffer_uptodate(bh) && reada_count > 1)
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200654 /* may unlock the write lock */
655 unlocked = search_by_key_reada(sb, reada_bh,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 reada_blocks, reada_count);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200657 /*
658 * If we haven't already unlocked the write lock,
659 * then we need to do that here before reading
660 * the current block
661 */
662 if (!buffer_uptodate(bh) && !unlocked) {
Frederic Weisbecker09eb47a2009-05-08 14:21:33 +0200663 reiserfs_write_unlock(sb);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200664 unlocked = true;
665 }
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400666 ll_rw_block(READ, 1, &bh);
667 wait_on_buffer(bh);
Frederic Weisbecker2ac62692009-05-14 02:56:39 +0200668
669 if (unlocked)
670 reiserfs_write_lock(sb);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400671 if (!buffer_uptodate(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 goto io_error;
673 } else {
674 io_error:
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400675 search_path->path_length--;
676 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700677 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700679 reada_count = 0;
680 if (expected_level == -1)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400681 expected_level = SB_TREE_HEIGHT(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700682 expected_level--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700684 /* It is possible that schedule occurred. We must check whether the key
685 to search is still in the tree rooted from the current buffer. If
686 not then repeat search from the root. */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400687 if (fs_changed(fs_gen, sb) &&
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400688 (!B_IS_IN_TREE(bh) ||
689 B_LEVEL(bh) != expected_level ||
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400690 !key_in_buffer(search_path, key, sb))) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400691 PROC_INFO_INC(sb, search_by_key_fs_changed);
692 PROC_INFO_INC(sb, search_by_key_restarted);
693 PROC_INFO_INC(sb,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700694 sbk_restarted[expected_level - 1]);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400695 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696
697 /* Get the root block number so that we can repeat the search
698 starting from the root. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400699 block_number = SB_ROOT_BLOCK(sb);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700700 expected_level = -1;
701 right_neighbor_of_leaf_node = 0;
702
703 /* repeat search from the root */
704 continue;
705 }
706
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400707 /* only check that the key is in the buffer if key is not
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708 equal to the MAX_KEY. Latter case is only possible in
709 "finish_unfinished()" processing during mount. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400710 RFALSE(comp_keys(&MAX_KEY, key) &&
711 !key_in_buffer(search_path, key, sb),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700712 "PAP-5130: key is not in the buffer");
713#ifdef CONFIG_REISERFS_CHECK
714 if (cur_tb) {
715 print_cur_tb("5140");
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400716 reiserfs_panic(sb, "PAP-5140",
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -0400717 "schedule occurred in do_balance!");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700718 }
719#endif
720
721 // make sure, that the node contents look like a node of
722 // certain level
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400723 if (!is_tree_node(bh, expected_level)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400724 reiserfs_error(sb, "vs-5150",
Jeff Mahoney0030b642009-03-30 14:02:28 -0400725 "invalid format found in block %ld. "
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400726 "Fsck?", bh->b_blocknr);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400727 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 return IO_ERROR;
729 }
730
731 /* ok, we have acquired next formatted node in the tree */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400732 node_level = B_LEVEL(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700733
Jeff Mahoneyee939612009-03-30 14:02:50 -0400734 PROC_INFO_BH_STAT(sb, bh, node_level - 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700735
Jeff Mahoneyee939612009-03-30 14:02:50 -0400736 RFALSE(node_level < stop_level,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700737 "vs-5152: tree level (%d) is less than stop level (%d)",
Jeff Mahoneyee939612009-03-30 14:02:50 -0400738 node_level, stop_level);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700739
Jeff Mahoneyee939612009-03-30 14:02:50 -0400740 retval = bin_search(key, B_N_PITEM_HEAD(bh, 0),
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400741 B_NR_ITEMS(bh),
Jeff Mahoneyee939612009-03-30 14:02:50 -0400742 (node_level ==
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
744 KEY_SIZE,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400745 &(last_element->pe_position));
Jeff Mahoneyee939612009-03-30 14:02:50 -0400746 if (node_level == stop_level) {
747 return retval;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700748 }
749
750 /* we are not in the stop level */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400751 if (retval == ITEM_FOUND)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700752 /* item has been found, so we choose the pointer which is to the right of the found one */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400753 last_element->pe_position++;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700754
755 /* if item was not found we choose the position which is to
756 the left of the found item. This requires no code,
757 bin_search did it already. */
758
759 /* So we have chosen a position in the current node which is
760 an internal node. Now we calculate child block number by
761 position in the node. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400762 block_number =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400763 B_N_CHILD_NUM(bh, last_element->pe_position);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700764
765 /* if we are going to read leaf nodes, try for read ahead as well */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400766 if ((search_path->reada & PATH_READA) &&
Jeff Mahoneyee939612009-03-30 14:02:50 -0400767 node_level == DISK_LEAF_NODE_LEVEL + 1) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400768 int pos = last_element->pe_position;
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400769 int limit = B_NR_ITEMS(bh);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700770 struct reiserfs_key *le_key;
771
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400772 if (search_path->reada & PATH_READA_BACK)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700773 limit = 0;
774 while (reada_count < SEARCH_BY_KEY_READA) {
775 if (pos == limit)
776 break;
777 reada_blocks[reada_count++] =
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400778 B_N_CHILD_NUM(bh, pos);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400779 if (search_path->reada & PATH_READA_BACK)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700780 pos--;
781 else
782 pos++;
783
784 /*
785 * check to make sure we're in the same object
786 */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400787 le_key = B_N_PDELIM_KEY(bh, pos);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700788 if (le32_to_cpu(le_key->k_objectid) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400789 key->on_disk_key.k_objectid) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700790 break;
791 }
792 }
793 }
794 }
795}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/* Form the path to an item and position in this item which contains
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400798 file byte defined by key. If there is no such item
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 corresponding to the key, we point the path to the item with
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400800 maximal key less than key, and *pos_in_item is set to one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 past the last entry/byte in the item. If searching for entry in a
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400802 directory item, and it is not found, *pos_in_item is set to one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 entry more than the entry with maximal key which is less than the
804 sought key.
805
806 Note that if there is no entry in this same node which is one more,
807 then we point to an imaginary entry. for direct items, the
808 position is in units of bytes, for indirect items the position is
809 in units of blocknr entries, for directory items the position is in
810 units of directory entries. */
811
812/* The function is NOT SCHEDULE-SAFE! */
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400813int search_for_position_by_key(struct super_block *sb, /* Pointer to the super block. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814 const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400815 struct treepath *search_path /* Filled up by this function. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700816 )
817{
818 struct item_head *p_le_ih; /* pointer to on-disk structure */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400819 int blk_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700820 loff_t item_offset, offset;
821 struct reiserfs_dir_entry de;
822 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700824 /* If searching for directory entry. */
825 if (is_direntry_cpu_key(p_cpu_key))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400826 return search_by_entry_key(sb, p_cpu_key, search_path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700827 &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700829 /* If not searching for directory entry. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700831 /* If item is found. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400832 retval = search_item(sb, p_cpu_key, search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700833 if (retval == IO_ERROR)
834 return retval;
835 if (retval == ITEM_FOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700837 RFALSE(!ih_item_len
838 (B_N_PITEM_HEAD
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400839 (PATH_PLAST_BUFFER(search_path),
840 PATH_LAST_POSITION(search_path))),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700841 "PAP-5165: item length equals zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400843 pos_in_item(search_path) = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 return POSITION_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400847 RFALSE(!PATH_LAST_POSITION(search_path),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700848 "PAP-5170: position equals zero");
849
850 /* Item is not found. Set path to the previous item. */
851 p_le_ih =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400852 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(search_path),
853 --PATH_LAST_POSITION(search_path));
Jeff Mahoneyee939612009-03-30 14:02:50 -0400854 blk_size = sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700855
856 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
857 return FILE_NOT_FOUND;
858 }
859 // FIXME: quite ugly this far
860
861 item_offset = le_ih_k_offset(p_le_ih);
862 offset = cpu_key_k_offset(p_cpu_key);
863
864 /* Needed byte is contained in the item pointed to by the path. */
865 if (item_offset <= offset &&
Jeff Mahoneyee939612009-03-30 14:02:50 -0400866 item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400867 pos_in_item(search_path) = offset - item_offset;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700868 if (is_indirect_le_ih(p_le_ih)) {
Jeff Mahoneyee939612009-03-30 14:02:50 -0400869 pos_in_item(search_path) /= blk_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700870 }
871 return POSITION_FOUND;
872 }
873
874 /* Needed byte is not contained in the item pointed to by the
875 path. Set pos_in_item out of the item. */
876 if (is_indirect_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400877 pos_in_item(search_path) =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700878 ih_item_len(p_le_ih) / UNFM_P_SIZE;
879 else
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400880 pos_in_item(search_path) = ih_item_len(p_le_ih);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700881
882 return POSITION_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/* Compare given item and item pointed to by the path. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400886int comp_items(const struct item_head *stored_ih, const struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400888 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700891 /* Last buffer at the path is not in the tree. */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -0400892 if (!B_IS_IN_TREE(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700893 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700895 /* Last path position is invalid. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400896 if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700897 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700899 /* we need only to know, whether it is the same item */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400900 ih = get_ih(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700901 return memcmp(stored_ih, ih, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* unformatted nodes are not logged anymore, ever. This is safe
905** now
906*/
907#define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
908
909// block can not be forgotten as it is in I/O or held by someone
910#define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912// prepare for delete or cut of direct item
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800913static inline int prepare_for_direct_item(struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914 struct item_head *le_ih,
915 struct inode *inode,
916 loff_t new_file_length, int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700918 loff_t round_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700920 if (new_file_length == max_reiserfs_offset(inode)) {
921 /* item has to be deleted */
922 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
923 return M_DELETE;
924 }
925 // new file gets truncated
926 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
Jeff Mahoney0222e652009-03-30 14:02:44 -0400927 //
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700928 round_len = ROUND_UP(new_file_length);
Jeff Mahoneyee939612009-03-30 14:02:50 -0400929 /* this was new_file_length < le_ih ... */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700930 if (round_len < le_ih_k_offset(le_ih)) {
931 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
932 return M_DELETE; /* Delete this item. */
933 }
934 /* Calculate first position and size for cutting from item. */
935 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
936 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700938 return M_CUT; /* Cut from this item. */
939 }
940
941 // old file: items may have any length
942
943 if (new_file_length < le_ih_k_offset(le_ih)) {
944 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
945 return M_DELETE; /* Delete this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 /* Calculate first position and size for cutting from item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700948 *cut_size = -(ih_item_len(le_ih) -
949 (pos_in_item(path) =
950 new_file_length + 1 - le_ih_k_offset(le_ih)));
951 return M_CUT; /* Cut from this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -0800954static inline int prepare_for_direntry_item(struct treepath *path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955 struct item_head *le_ih,
956 struct inode *inode,
957 loff_t new_file_length,
958 int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700960 if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
961 new_file_length == max_reiserfs_offset(inode)) {
962 RFALSE(ih_entry_count(le_ih) != 2,
963 "PAP-5220: incorrect empty directory item (%h)", le_ih);
964 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
965 return M_DELETE; /* Delete the directory item containing "." and ".." entry. */
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700968 if (ih_entry_count(le_ih) == 1) {
969 /* Delete the directory item such as there is one record only
970 in this item */
971 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
972 return M_DELETE;
973 }
974
975 /* Cut one record from the directory item. */
976 *cut_size =
977 -(DEH_SIZE +
978 entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
979 return M_CUT;
980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -0800982#define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984/* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
985 If the path points to an indirect item, remove some number of its unformatted nodes.
986 In case of file truncate calculate whether this item must be deleted/truncated or last
987 unformatted node of this item will be converted to a direct item.
988 This function returns a determination of what balance mode the calling function should employ. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400989static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *path, const struct cpu_key *item_key, int *removed, /* Number of unformatted nodes which were removed
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700990 from end of the file. */
Jeff Mahoneyee939612009-03-30 14:02:50 -0400991 int *cut_size, unsigned long long new_file_length /* MAX_KEY_OFFSET in case of delete. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700992 )
993{
Jeff Mahoneya9dd3642009-03-30 14:02:45 -0400994 struct super_block *sb = inode->i_sb;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -0400995 struct item_head *p_le_ih = PATH_PITEM_HEAD(path);
996 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700998 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001000 /* Stat_data item. */
1001 if (is_statdata_le_ih(p_le_ih)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jeff Mahoneyee939612009-03-30 14:02:50 -04001003 RFALSE(new_file_length != max_reiserfs_offset(inode),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004 "PAP-5210: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001006 *cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001007 return M_DELETE;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001010 /* Directory item. */
1011 if (is_direntry_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001012 return prepare_for_direntry_item(path, p_le_ih, inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001013 new_file_length,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001014 cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001016 /* Direct item. */
1017 if (is_direct_le_ih(p_le_ih))
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001018 return prepare_for_direct_item(path, p_le_ih, inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001019 new_file_length, cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001021 /* Case of an indirect item. */
1022 {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001023 int blk_size = sb->s_blocksize;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001024 struct item_head s_ih;
1025 int need_re_search;
1026 int delete = 0;
1027 int result = M_CUT;
1028 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Jeff Mahoneyee939612009-03-30 14:02:50 -04001030 if ( new_file_length == max_reiserfs_offset (inode) ) {
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001031 /* prepare_for_delete_or_cut() is called by
1032 * reiserfs_delete_item() */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001033 new_file_length = 0;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001034 delete = 1;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001037 do {
1038 need_re_search = 0;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001039 *cut_size = 0;
1040 bh = PATH_PLAST_BUFFER(path);
1041 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001042 pos = I_UNFM_NUM(&s_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Jeff Mahoneyee939612009-03-30 14:02:50 -04001044 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
Al Viro87588dd2007-07-26 17:47:03 +01001045 __le32 *unfm;
1046 __u32 block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001048 /* Each unformatted block deletion may involve one additional
1049 * bitmap block into the transaction, thereby the initial
1050 * journal space reservation might not be enough. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001051 if (!delete && (*cut_size) != 0 &&
1052 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001053 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001055 unfm = (__le32 *)B_I_PITEM(bh, &s_ih) + pos - 1;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001056 block = get_block_num(unfm, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001058 if (block != 0) {
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001059 reiserfs_prepare_for_journal(sb, bh, 1);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001060 put_block_num(unfm, 0, 0);
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001061 journal_mark_dirty(th, sb, bh);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001062 reiserfs_free_block(th, inode, block, 1);
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Frederic Weisbecker5e69e3a2009-04-30 23:36:33 +02001065 reiserfs_write_unlock(sb);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001066 cond_resched();
Frederic Weisbecker5e69e3a2009-04-30 23:36:33 +02001067 reiserfs_write_lock(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001069 if (item_moved (&s_ih, path)) {
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001070 need_re_search = 1;
1071 break;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001074 pos --;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001075 (*removed)++;
1076 (*cut_size) -= UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001078 if (pos == 0) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001079 (*cut_size) -= IH_SIZE;
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001080 result = M_DELETE;
1081 break;
1082 }
1083 }
1084 /* a trick. If the buffer has been logged, this will do nothing. If
1085 ** we've broken the loop without logging it, it will restore the
1086 ** buffer */
Jeff Mahoneyad31a4f2009-03-30 14:02:46 -04001087 reiserfs_restore_prepared_buffer(sb, bh);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001088 } while (need_re_search &&
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001089 search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
1090 pos_in_item(path) = pos * UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001092 if (*cut_size == 0) {
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001093 /* Nothing were cut. maybe convert last unformatted node to the
1094 * direct item? */
1095 result = M_CONVERT;
1096 }
1097 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101/* Calculate number of bytes which will be deleted or cut during balance */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001102static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Jeff Mahoneyee939612009-03-30 14:02:50 -04001104 int del_size;
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001105 struct item_head *p_le_ih = PATH_PITEM_HEAD(tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001107 if (is_statdata_le_ih(p_le_ih))
1108 return 0;
1109
Jeff Mahoneyee939612009-03-30 14:02:50 -04001110 del_size =
1111 (mode ==
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001112 M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001113 if (is_direntry_le_ih(p_le_ih)) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04001114 /* return EMPTY_DIR_SIZE; We delete emty directoris only.
1115 * we can't use EMPTY_DIR_SIZE, as old format dirs have a different
1116 * empty size. ick. FIXME, is this right? */
1117 return del_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001118 }
1119
1120 if (is_indirect_le_ih(p_le_ih))
Jeff Mahoneyee939612009-03-30 14:02:50 -04001121 del_size = (del_size / UNFM_P_SIZE) *
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001122 (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001123 return del_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001124}
1125
1126static void init_tb_struct(struct reiserfs_transaction_handle *th,
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001127 struct tree_balance *tb,
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001128 struct super_block *sb,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001129 struct treepath *path, int size)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001130{
1131
1132 BUG_ON(!th->t_trans_id);
1133
Jeff Mahoneya063ae12009-03-30 14:02:48 -04001134 memset(tb, '\0', sizeof(struct tree_balance));
1135 tb->transaction_handle = th;
1136 tb->tb_sb = sb;
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001137 tb->tb_path = path;
1138 PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1139 PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001140 tb->insert_size[0] = size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001141}
1142
1143void padd_item(char *item, int total_length, int length)
1144{
1145 int i;
1146
1147 for (i = total_length; i > length;)
1148 item[--i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151#ifdef REISERQUOTA_DEBUG
1152char key2type(struct reiserfs_key *ih)
1153{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001154 if (is_direntry_le_key(2, ih))
1155 return 'd';
1156 if (is_direct_le_key(2, ih))
1157 return 'D';
1158 if (is_indirect_le_key(2, ih))
1159 return 'i';
1160 if (is_statdata_le_key(2, ih))
1161 return 's';
1162 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165char head2type(struct item_head *ih)
1166{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001167 if (is_direntry_le_ih(ih))
1168 return 'd';
1169 if (is_direct_le_ih(ih))
1170 return 'D';
1171 if (is_indirect_le_ih(ih))
1172 return 'i';
1173 if (is_statdata_le_ih(ih))
1174 return 's';
1175 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177#endif
1178
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001179/* Delete object item.
1180 * th - active transaction handle
1181 * path - path to the deleted item
1182 * item_key - key to search for the deleted item
1183 * indode - used for updating i_blocks and quotas
1184 * un_bh - NULL or unformatted node pointer
1185 */
1186int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
1187 struct treepath *path, const struct cpu_key *item_key,
1188 struct inode *inode, struct buffer_head *un_bh)
1189{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001190 struct super_block *sb = inode->i_sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001191 struct tree_balance s_del_balance;
1192 struct item_head s_ih;
1193 struct item_head *q_ih;
1194 int quota_cut_bytes;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001195 int ret_value, del_size, removed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001198 char mode;
1199 int iter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200#endif
1201
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001202 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001204 init_tb_struct(th, &s_del_balance, sb, path,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 0 /*size is unknown */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001207 while (1) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04001208 removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001211 iter++;
1212 mode =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213#endif
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001214 prepare_for_delete_or_cut(th, inode, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001215 item_key, &removed,
1216 &del_size,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001217 max_reiserfs_offset(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Jeff Mahoneyee939612009-03-30 14:02:50 -04001219 RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001221 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
Jeff Mahoneyee939612009-03-30 14:02:50 -04001222 s_del_balance.insert_size[0] = del_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jeff Mahoneyee939612009-03-30 14:02:50 -04001224 ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1225 if (ret_value != REPEAT_SEARCH)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001228 PROC_INFO_INC(sb, delete_item_restarted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001230 // file system changed, repeat search
Jeff Mahoneyee939612009-03-30 14:02:50 -04001231 ret_value =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001232 search_for_position_by_key(sb, item_key, path);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001233 if (ret_value == IO_ERROR)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001234 break;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001235 if (ret_value == FILE_NOT_FOUND) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001236 reiserfs_warning(sb, "vs-5340",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001237 "no items of the file %K found",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001238 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001239 break;
1240 }
1241 } /* while (1) */
1242
Jeff Mahoneyee939612009-03-30 14:02:50 -04001243 if (ret_value != CARRY_ON) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001244 unfix_nodes(&s_del_balance);
1245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 // reiserfs_delete_item returns item length when success
Jeff Mahoneyee939612009-03-30 14:02:50 -04001248 ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001249 q_ih = get_ih(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001250 quota_cut_bytes = ih_item_len(q_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001252 /* hack so the quota code doesn't have to guess if the file
1253 ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
1254 ** We test the offset because the tail might have been
1255 ** split into multiple items, and we only want to decrement for
1256 ** the unfm node once
1257 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001258 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001259 if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
1260 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 } else {
1262 quota_cut_bytes = 0;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001266 if (un_bh) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001267 int off;
1268 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001270 /* We are in direct2indirect conversion, so move tail contents
1271 to the unformatted node */
1272 /* note, we do the copy before preparing the buffer because we
1273 ** don't care about the contents of the unformatted node yet.
1274 ** the only thing we really care about is the direct item's data
1275 ** is in the unformatted node.
1276 **
1277 ** Otherwise, we would have to call reiserfs_prepare_for_journal on
1278 ** the unformatted node, which might schedule, meaning we'd have to
1279 ** loop all the way back up to the start of the while loop.
1280 **
1281 ** The unformatted node must be dirtied later on. We can't be
1282 ** sure here if the entire tail has been deleted yet.
1283 **
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001284 ** un_bh is from the page cache (all unformatted nodes are
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 ** from the page cache) and might be a highmem page. So, we
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001286 ** can't use un_bh->b_data.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001287 ** -clm
1288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001290 data = kmap_atomic(un_bh->b_page, KM_USER0);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001291 off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
1292 memcpy(data + off,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001293 B_I_PITEM(PATH_PLAST_BUFFER(path), &s_ih),
Jeff Mahoneyee939612009-03-30 14:02:50 -04001294 ret_value);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001295 kunmap_atomic(data, KM_USER0);
1296 }
1297 /* Perform balancing after all resources have been collected at once. */
1298 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300#ifdef REISERQUOTA_DEBUG
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001301 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302 "reiserquota delete_item(): freeing %u, id=%u type=%c",
Jeff Mahoney995c7622009-03-30 14:02:47 -04001303 quota_cut_bytes, inode->i_uid, head2type(&s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304#endif
Linus Torvaldse1c50242009-03-30 12:29:21 -07001305 vfs_dq_free_space_nodirty(inode, quota_cut_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001307 /* Return deleted body length */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001308 return ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/* Summary Of Mechanisms For Handling Collisions Between Processes:
1312
1313 deletion of the body of the object is performed by iput(), with the
1314 result that if multiple processes are operating on a file, the
1315 deletion of the body of the file is deferred until the last process
1316 that has an open inode performs its iput().
1317
1318 writes and truncates are protected from collisions by use of
1319 semaphores.
1320
1321 creates, linking, and mknod are protected from collisions with other
1322 processes by making the reiserfs_add_entry() the last step in the
1323 creation, and then rolling back all changes if there was a collision.
1324 - Hans
1325*/
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/* this deletes item which never gets split */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001328void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1329 struct inode *inode, struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001331 struct tree_balance tb;
1332 INITIALIZE_PATH(path);
1333 int item_len = 0;
1334 int tb_init = 0;
1335 struct cpu_key cpu_key;
1336 int retval;
1337 int quota_cut_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001339 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001341 le_key2cpu_key(&cpu_key, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001343 while (1) {
1344 retval = search_item(th->t_super, &cpu_key, &path);
1345 if (retval == IO_ERROR) {
Jeff Mahoney0030b642009-03-30 14:02:28 -04001346 reiserfs_error(th->t_super, "vs-5350",
1347 "i/o failure occurred trying "
1348 "to delete %K", &cpu_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001349 break;
1350 }
1351 if (retval != ITEM_FOUND) {
1352 pathrelse(&path);
1353 // No need for a warning, if there is just no free space to insert '..' item into the newly-created subdir
1354 if (!
1355 ((unsigned long long)
1356 GET_HASH_VALUE(le_key_k_offset
1357 (le_key_version(key), key)) == 0
1358 && (unsigned long long)
1359 GET_GENERATION_NUMBER(le_key_k_offset
1360 (le_key_version(key),
1361 key)) == 1))
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001362 reiserfs_warning(th->t_super, "vs-5355",
1363 "%k not found", key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001364 break;
1365 }
1366 if (!tb_init) {
1367 tb_init = 1;
1368 item_len = ih_item_len(PATH_PITEM_HEAD(&path));
1369 init_tb_struct(th, &tb, th->t_super, &path,
1370 -(IH_SIZE + item_len));
1371 }
1372 quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path));
1373
1374 retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
1375 if (retval == REPEAT_SEARCH) {
1376 PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
1377 continue;
1378 }
1379
1380 if (retval == CARRY_ON) {
1381 do_balance(&tb, NULL, NULL, M_DELETE);
1382 if (inode) { /* Should we count quota for item? (we don't count quotas for save-links) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001384 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
1385 "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1386 quota_cut_bytes, inode->i_uid,
1387 key2type(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388#endif
Jan Kara77db4f22009-01-26 17:14:18 +01001389 vfs_dq_free_space_nodirty(inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001390 quota_cut_bytes);
1391 }
1392 break;
1393 }
1394 // IO_ERROR, NO_DISK_SPACE, etc
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001395 reiserfs_warning(th->t_super, "vs-5360",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001396 "could not delete %K due to fix_nodes failure",
1397 &cpu_key);
1398 unfix_nodes(&tb);
1399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001402 reiserfs_check_path(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001405int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1406 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001408 int err;
1409 inode->i_size = 0;
1410 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001412 /* for directory this deletes item containing "." and ".." */
1413 err =
1414 reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
1415 if (err)
1416 return err;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418#if defined( USE_INODE_GENERATION_COUNTER )
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001419 if (!old_format_only(th->t_super)) {
1420 __le32 *inode_generation;
1421
1422 inode_generation =
1423 &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
Marcin Slusarz9e902df2008-04-28 02:16:20 -07001424 le32_add_cpu(inode_generation, 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/* USE_INODE_GENERATION_COUNTER */
1427#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001428 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001433static void unmap_buffers(struct page *page, loff_t pos)
1434{
1435 struct buffer_head *bh;
1436 struct buffer_head *head;
1437 struct buffer_head *next;
1438 unsigned long tail_index;
1439 unsigned long cur_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001441 if (page) {
1442 if (page_has_buffers(page)) {
1443 tail_index = pos & (PAGE_CACHE_SIZE - 1);
1444 cur_index = 0;
1445 head = page_buffers(page);
1446 bh = head;
1447 do {
1448 next = bh->b_this_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001450 /* we want to unmap the buffers that contain the tail, and
1451 ** all the buffers after it (since the tail must be at the
1452 ** end of the file). We don't want to unmap file data
1453 ** before the tail, since it might be dirty and waiting to
1454 ** reach disk
1455 */
1456 cur_index += bh->b_size;
1457 if (cur_index > tail_index) {
1458 reiserfs_unmap_buffer(bh);
1459 }
1460 bh = next;
1461 } while (bh != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001466static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001467 struct inode *inode,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001468 struct page *page,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001469 struct treepath *path,
1470 const struct cpu_key *item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001471 loff_t new_file_size, char *mode)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001472{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001473 struct super_block *sb = inode->i_sb;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001474 int block_size = sb->s_blocksize;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001475 int cut_bytes;
1476 BUG_ON(!th->t_trans_id);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001477 BUG_ON(new_file_size != inode->i_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001479 /* the page being sent in could be NULL if there was an i/o error
1480 ** reading in the last block. The user will hit problems trying to
1481 ** read the file, but for now we just skip the indirect2direct
1482 */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001483 if (atomic_read(&inode->i_count) > 1 ||
1484 !tail_has_to_be_packed(inode) ||
1485 !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
Jeff Mahoney0222e652009-03-30 14:02:44 -04001486 /* leave tail in an unformatted node */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001487 *mode = M_SKIP_BALANCING;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001488 cut_bytes =
Jeff Mahoneyee939612009-03-30 14:02:50 -04001489 block_size - (new_file_size & (block_size - 1));
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001490 pathrelse(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001491 return cut_bytes;
1492 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001493 /* Perform the conversion to a direct_item. */
1494 /* return indirect_to_direct(inode, path, item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001495 new_file_size, mode); */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001496 return indirect2direct(th, inode, page, path, item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001497 new_file_size, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500/* we did indirect_to_direct conversion. And we have inserted direct
1501 item successesfully, but there were no disk space to cut unfm
1502 pointer being converted. Therefore we have to delete inserted
1503 direct item(s) */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001504static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001505 struct inode *inode, struct treepath *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001507 struct cpu_key tail_key;
1508 int tail_len;
1509 int removed;
1510 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001512 make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4); // !!!!
1513 tail_key.key_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001515 tail_len =
1516 (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
1517 while (tail_len) {
1518 /* look for the last byte of the tail */
1519 if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1520 POSITION_NOT_FOUND)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001521 reiserfs_panic(inode->i_sb, "vs-5615",
1522 "found invalid item");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001523 RFALSE(path->pos_in_item !=
1524 ih_item_len(PATH_PITEM_HEAD(path)) - 1,
1525 "vs-5616: appended bytes found");
1526 PATH_LAST_POSITION(path)--;
1527
1528 removed =
1529 reiserfs_delete_item(th, path, &tail_key, inode,
1530 NULL /*unbh not needed */ );
1531 RFALSE(removed <= 0
1532 || removed > tail_len,
1533 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1534 tail_len, removed);
1535 tail_len -= removed;
1536 set_cpu_key_k_offset(&tail_key,
1537 cpu_key_k_offset(&tail_key) - removed);
1538 }
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001539 reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct "
1540 "conversion has been rolled back due to "
1541 "lack of disk space");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001542 //mark_file_without_tail (inode);
1543 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546/* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001547int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001548 struct treepath *path,
1549 struct cpu_key *item_key,
Jeff Mahoney995c7622009-03-30 14:02:47 -04001550 struct inode *inode,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001551 struct page *page, loff_t new_file_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Jeff Mahoney995c7622009-03-30 14:02:47 -04001553 struct super_block *sb = inode->i_sb;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001554 /* Every function which is going to call do_balance must first
1555 create a tree_balance structure. Then it must fill up this
1556 structure by using the init_tb_struct and fix_nodes functions.
1557 After that we can make tree balancing. */
1558 struct tree_balance s_cut_balance;
1559 struct item_head *p_le_ih;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001560 int cut_size = 0, /* Amount to be cut. */
1561 ret_value = CARRY_ON, removed = 0, /* Number of the removed unformatted nodes. */
1562 is_inode_locked = 0;
1563 char mode; /* Mode of the balance. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001564 int retval2 = -1;
1565 int quota_cut_bytes;
1566 loff_t tail_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001568 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001570 init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001571 cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001573 /* Repeat this loop until we either cut the item without needing
1574 to balance, or we fix_nodes without schedule occurring */
1575 while (1) {
1576 /* Determine the balance mode, position of the first byte to
1577 be cut, and size to be cut. In case of the indirect item
1578 free unformatted nodes which are pointed to by the cut
1579 pointers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Jeff Mahoneyee939612009-03-30 14:02:50 -04001581 mode =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001582 prepare_for_delete_or_cut(th, inode, path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001583 item_key, &removed,
1584 &cut_size, new_file_size);
1585 if (mode == M_CONVERT) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001586 /* convert last unformatted node to direct item or leave
1587 tail in the unformatted node */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001588 RFALSE(ret_value != CARRY_ON,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001589 "PAP-5570: can not convert twice");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Jeff Mahoneyee939612009-03-30 14:02:50 -04001591 ret_value =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001592 maybe_indirect_to_direct(th, inode, page,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001593 path, item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001594 new_file_size, &mode);
1595 if (mode == M_SKIP_BALANCING)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001596 /* tail has been left in the unformatted node */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001597 return ret_value;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001598
Jeff Mahoneyee939612009-03-30 14:02:50 -04001599 is_inode_locked = 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001600
1601 /* removing of last unformatted node will change value we
1602 have to return to truncate. Save it */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001603 retval2 = ret_value;
1604 /*retval2 = sb->s_blocksize - (new_file_size & (sb->s_blocksize - 1)); */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001605
1606 /* So, we have performed the first part of the conversion:
1607 inserting the new direct item. Now we are removing the
1608 last unformatted node pointer. Set key to search for
1609 it. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001610 set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1611 item_key->key_length = 4;
Jeff Mahoneyee939612009-03-30 14:02:50 -04001612 new_file_size -=
1613 (new_file_size & (sb->s_blocksize - 1));
1614 tail_pos = new_file_size;
1615 set_cpu_key_k_offset(item_key, new_file_size + 1);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001616 if (search_for_position_by_key
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001617 (sb, item_key,
1618 path) == POSITION_NOT_FOUND) {
1619 print_block(PATH_PLAST_BUFFER(path), 3,
1620 PATH_LAST_POSITION(path) - 1,
1621 PATH_LAST_POSITION(path) + 1);
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001622 reiserfs_panic(sb, "PAP-5580", "item to "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001623 "convert does not exist (%K)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001624 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001625 }
1626 continue;
1627 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001628 if (cut_size == 0) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001629 pathrelse(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001630 return 0;
1631 }
1632
Jeff Mahoneyee939612009-03-30 14:02:50 -04001633 s_cut_balance.insert_size[0] = cut_size;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001634
Jeff Mahoneyee939612009-03-30 14:02:50 -04001635 ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
1636 if (ret_value != REPEAT_SEARCH)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001637 break;
1638
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001639 PROC_INFO_INC(sb, cut_from_item_restarted);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001640
Jeff Mahoneyee939612009-03-30 14:02:50 -04001641 ret_value =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001642 search_for_position_by_key(sb, item_key, path);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001643 if (ret_value == POSITION_FOUND)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001644 continue;
1645
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001646 reiserfs_warning(sb, "PAP-5610", "item %K not found",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001647 item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001648 unfix_nodes(&s_cut_balance);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001649 return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001650 } /* while */
1651
1652 // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001653 if (ret_value != CARRY_ON) {
1654 if (is_inode_locked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001655 // FIXME: this seems to be not needed: we are always able
1656 // to cut item
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001657 indirect_to_direct_roll_back(th, inode, path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001658 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001659 if (ret_value == NO_DISK_SPACE)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001660 reiserfs_warning(sb, "reiserfs-5092",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001661 "NO_DISK_SPACE");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001662 unfix_nodes(&s_cut_balance);
1663 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001666 /* go ahead and perform balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Jeff Mahoneyee939612009-03-30 14:02:50 -04001668 RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001670 /* Calculate number of bytes that need to be cut from the item. */
1671 quota_cut_bytes =
Jeff Mahoneyee939612009-03-30 14:02:50 -04001672 (mode ==
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001673 M_DELETE) ? ih_item_len(get_ih(path)) : -s_cut_balance.
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001674 insert_size[0];
1675 if (retval2 == -1)
Jeff Mahoneyee939612009-03-30 14:02:50 -04001676 ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001677 else
Jeff Mahoneyee939612009-03-30 14:02:50 -04001678 ret_value = retval2;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001679
1680 /* For direct items, we only change the quota when deleting the last
1681 ** item.
1682 */
1683 p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
Jeff Mahoney995c7622009-03-30 14:02:47 -04001684 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
Jeff Mahoneyee939612009-03-30 14:02:50 -04001685 if (mode == M_DELETE &&
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001686 (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001687 1) {
1688 // FIXME: this is to keep 3.5 happy
Jeff Mahoney995c7622009-03-30 14:02:47 -04001689 REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001690 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001691 } else {
1692 quota_cut_bytes = 0;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyee939612009-03-30 14:02:50 -04001696 if (is_inode_locked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001697 struct item_head *le_ih =
1698 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1699 /* we are going to complete indirect2direct conversion. Make
1700 sure, that we exactly remove last unformatted node pointer
1701 of the item */
1702 if (!is_indirect_le_ih(le_ih))
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001703 reiserfs_panic(sb, "vs-5652",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001704 "item must be indirect %h", le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jeff Mahoneyee939612009-03-30 14:02:50 -04001706 if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001707 reiserfs_panic(sb, "vs-5653", "completing "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001708 "indirect2direct conversion indirect "
1709 "item %h being deleted must be of "
1710 "4 byte long", le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Jeff Mahoneyee939612009-03-30 14:02:50 -04001712 if (mode == M_CUT
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001713 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
Jeff Mahoneya9dd3642009-03-30 14:02:45 -04001714 reiserfs_panic(sb, "vs-5654", "can not complete "
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001715 "indirect2direct conversion of %h "
1716 "(CUT, insert_size==%d)",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001717 le_ih, s_cut_balance.insert_size[0]);
1718 }
1719 /* it would be useful to make sure, that right neighboring
1720 item is direct item of this file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001723
Jeff Mahoneyee939612009-03-30 14:02:50 -04001724 do_balance(&s_cut_balance, NULL, NULL, mode);
1725 if (is_inode_locked) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001726 /* we've done an indirect->direct conversion. when the data block
1727 ** was freed, it was removed from the list of blocks that must
1728 ** be flushed before the transaction commits, make sure to
1729 ** unmap and invalidate it
1730 */
1731 unmap_buffers(page, tail_pos);
Jeff Mahoney995c7622009-03-30 14:02:47 -04001732 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734#ifdef REISERQUOTA_DEBUG
Jeff Mahoney995c7622009-03-30 14:02:47 -04001735 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001736 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
Jeff Mahoney995c7622009-03-30 14:02:47 -04001737 quota_cut_bytes, inode->i_uid, '?');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#endif
Linus Torvaldse1c50242009-03-30 12:29:21 -07001739 vfs_dq_free_space_nodirty(inode, quota_cut_bytes);
Jeff Mahoneyee939612009-03-30 14:02:50 -04001740 return ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001743static void truncate_directory(struct reiserfs_transaction_handle *th,
1744 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001746 BUG_ON(!th->t_trans_id);
1747 if (inode->i_nlink)
Jeff Mahoney0030b642009-03-30 14:02:28 -04001748 reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001750 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
1751 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
1752 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1753 reiserfs_update_sd(th, inode);
1754 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
1755 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/* Truncate file to the new size. Note, this must be called with a transaction
1759 already started */
Jeff Mahoney995c7622009-03-30 14:02:47 -04001760int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
1761 struct inode *inode, /* ->i_size contains new size */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001762 struct page *page, /* up to date for last block */
1763 int update_timestamps /* when it is called by
1764 file_release to convert
1765 the tail - no timestamps
1766 should be updated */
1767 )
1768{
1769 INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
1770 struct item_head *p_le_ih; /* Pointer to an item header. */
1771 struct cpu_key s_item_key; /* Key to search for a previous file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001772 loff_t file_size, /* Old file size. */
1773 new_file_size; /* New file size. */
1774 int deleted; /* Number of deleted or truncated bytes. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001775 int retval;
1776 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001778 BUG_ON(!th->t_trans_id);
1779 if (!
Jeff Mahoney995c7622009-03-30 14:02:47 -04001780 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1781 || S_ISLNK(inode->i_mode)))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Jeff Mahoney995c7622009-03-30 14:02:47 -04001784 if (S_ISDIR(inode->i_mode)) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001785 // deletion of directory - no need to update timestamps
Jeff Mahoney995c7622009-03-30 14:02:47 -04001786 truncate_directory(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
1789
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001790 /* Get new file size. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001791 new_file_size = inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001793 // FIXME: note, that key type is unimportant here
Jeff Mahoney995c7622009-03-30 14:02:47 -04001794 make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001795 TYPE_DIRECT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001797 retval =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001798 search_for_position_by_key(inode->i_sb, &s_item_key,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001799 &s_search_path);
1800 if (retval == IO_ERROR) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001801 reiserfs_error(inode->i_sb, "vs-5657",
Jeff Mahoney0030b642009-03-30 14:02:28 -04001802 "i/o failure occurred trying to truncate %K",
1803 &s_item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001804 err = -EIO;
1805 goto out;
1806 }
1807 if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001808 reiserfs_error(inode->i_sb, "PAP-5660",
Jeff Mahoney0030b642009-03-30 14:02:28 -04001809 "wrong result %d of search for %K", retval,
1810 &s_item_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001812 err = -EIO;
1813 goto out;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001816 s_search_path.pos_in_item--;
1817
1818 /* Get real file size (total length of all file items) */
1819 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1820 if (is_statdata_le_ih(p_le_ih))
Jeff Mahoneyee939612009-03-30 14:02:50 -04001821 file_size = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001822 else {
1823 loff_t offset = le_ih_k_offset(p_le_ih);
1824 int bytes =
Jeff Mahoney995c7622009-03-30 14:02:47 -04001825 op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001826
1827 /* this may mismatch with real file size: if last direct item
1828 had no padding zeros and last unformatted node had no free
1829 space, this file would have this file size */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001830 file_size = offset + bytes - 1;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 /*
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001833 * are we doing a full truncate or delete, if so
1834 * kick in the reada code
1835 */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001836 if (new_file_size == 0)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001837 s_search_path.reada = PATH_READA | PATH_READA_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Jeff Mahoneyee939612009-03-30 14:02:50 -04001839 if (file_size == 0 || file_size < new_file_size) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001840 goto update_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001843 /* Update key to search for the last file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001844 set_cpu_key_k_offset(&s_item_key, file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001846 do {
1847 /* Cut or delete file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001848 deleted =
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001849 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001850 inode, page, new_file_size);
1851 if (deleted < 0) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001852 reiserfs_warning(inode->i_sb, "vs-5665",
Jeff Mahoney45b03d52009-03-30 14:02:21 -04001853 "reiserfs_cut_from_item failed");
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001854 reiserfs_check_path(&s_search_path);
1855 return 0;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jeff Mahoneyee939612009-03-30 14:02:50 -04001858 RFALSE(deleted > file_size,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001859 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
Jeff Mahoneyee939612009-03-30 14:02:50 -04001860 deleted, file_size, &s_item_key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001861
1862 /* Change key to search the last file item. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001863 file_size -= deleted;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001864
Jeff Mahoneyee939612009-03-30 14:02:50 -04001865 set_cpu_key_k_offset(&s_item_key, file_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001866
1867 /* While there are bytes to truncate and previous file item is presented in the tree. */
1868
1869 /*
Jeff Mahoney0222e652009-03-30 14:02:44 -04001870 ** This loop could take a really long time, and could log
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001871 ** many more blocks than a transaction can hold. So, we do a polite
1872 ** journal end here, and if the transaction needs ending, we make
1873 ** sure the file is consistent before ending the current trans
1874 ** and starting a new one
1875 */
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001876 if (journal_transaction_should_end(th, 0) ||
1877 reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001878 int orig_len_alloc = th->t_blocks_allocated;
Jeff Mahoney3cd6dbe2009-03-30 14:02:43 -04001879 pathrelse(&s_search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001880
1881 if (update_timestamps) {
Jeff Mahoney995c7622009-03-30 14:02:47 -04001882 inode->i_mtime = CURRENT_TIME_SEC;
1883 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001884 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04001885 reiserfs_update_sd(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001886
Jeff Mahoney995c7622009-03-30 14:02:47 -04001887 err = journal_end(th, inode->i_sb, orig_len_alloc);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001888 if (err)
1889 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04001890 err = journal_begin(th, inode->i_sb,
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001891 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001892 if (err)
1893 goto out;
Jeff Mahoney995c7622009-03-30 14:02:47 -04001894 reiserfs_update_inode_transaction(inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001895 }
Jeff Mahoneyee939612009-03-30 14:02:50 -04001896 } while (file_size > ROUND_UP(new_file_size) &&
Jeff Mahoney995c7622009-03-30 14:02:47 -04001897 search_for_position_by_key(inode->i_sb, &s_item_key,
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001898 &s_search_path) == POSITION_FOUND);
1899
Jeff Mahoneyee939612009-03-30 14:02:50 -04001900 RFALSE(file_size > ROUND_UP(new_file_size),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001901 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
Jeff Mahoneyee939612009-03-30 14:02:50 -04001902 new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001903
1904 update_and_out:
1905 if (update_timestamps) {
1906 // this is truncate, not file closing
Jeff Mahoney995c7622009-03-30 14:02:47 -04001907 inode->i_mtime = CURRENT_TIME_SEC;
1908 inode->i_ctime = CURRENT_TIME_SEC;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001909 }
Jeff Mahoney995c7622009-03-30 14:02:47 -04001910 reiserfs_update_sd(th, inode);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001911
1912 out:
1913 pathrelse(&s_search_path);
1914 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917#ifdef CONFIG_REISERFS_CHECK
1918// this makes sure, that we __append__, not overwrite or add holes
Josef "Jeff" Sipekfec6d052006-12-08 02:36:32 -08001919static void check_research_for_paste(struct treepath *path,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001920 const struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001922 struct item_head *found_ih = get_ih(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001924 if (is_direct_le_ih(found_ih)) {
1925 if (le_ih_k_offset(found_ih) +
1926 op_bytes_number(found_ih,
1927 get_last_bh(path)->b_size) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001928 cpu_key_k_offset(key)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001929 || op_bytes_number(found_ih,
1930 get_last_bh(path)->b_size) !=
1931 pos_in_item(path))
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001932 reiserfs_panic(NULL, "PAP-5720", "found direct item "
1933 "%h or position (%d) does not match "
1934 "to key %K", found_ih,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001935 pos_in_item(path), key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001936 }
1937 if (is_indirect_le_ih(found_ih)) {
1938 if (le_ih_k_offset(found_ih) +
1939 op_bytes_number(found_ih,
1940 get_last_bh(path)->b_size) !=
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001941 cpu_key_k_offset(key)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001942 || I_UNFM_NUM(found_ih) != pos_in_item(path)
1943 || get_ih_free_space(found_ih) != 0)
Jeff Mahoneyc3a9c212009-03-30 14:02:25 -04001944 reiserfs_panic(NULL, "PAP-5730", "found indirect "
1945 "item (%h) or position (%d) does not "
1946 "match to key (%K)",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001947 found_ih, pos_in_item(path), key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001948 }
1949}
1950#endif /* config reiserfs check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952/* Paste bytes to the existing item. Returns bytes number pasted into the item. */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001953int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *search_path, /* Path to the pasted item. */
1954 const struct cpu_key *key, /* Key to search for the needed item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001955 struct inode *inode, /* Inode item belongs to */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001956 const char *body, /* Pointer to the bytes to paste. */
Jeff Mahoneyee939612009-03-30 14:02:50 -04001957 int pasted_size)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001958{ /* Size of pasted bytes. */
1959 struct tree_balance s_paste_balance;
1960 int retval;
1961 int fs_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001963 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001965 fs_gen = get_generation(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001968 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1969 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
Jeff Mahoneyee939612009-03-30 14:02:50 -04001970 pasted_size, inode->i_uid,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001971 key2type(&(key->on_disk_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972#endif
1973
Linus Torvaldse1c50242009-03-30 12:29:21 -07001974 if (vfs_dq_alloc_space_nodirty(inode, pasted_size)) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001975 pathrelse(search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001976 return -EDQUOT;
1977 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001978 init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
Jeff Mahoneyee939612009-03-30 14:02:50 -04001979 pasted_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001981 s_paste_balance.key = key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#endif
1983
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001984 /* DQUOT_* can schedule, must check before the fix_nodes */
1985 if (fs_changed(fs_gen, inode->i_sb)) {
1986 goto search_again;
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001989 while ((retval =
1990 fix_nodes(M_PASTE, &s_paste_balance, NULL,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001991 body)) == REPEAT_SEARCH) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001992 search_again:
1993 /* file system changed while we were in the fix_nodes */
1994 PROC_INFO_INC(th->t_super, paste_into_item_restarted);
1995 retval =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04001996 search_for_position_by_key(th->t_super, key,
1997 search_path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001998 if (retval == IO_ERROR) {
1999 retval = -EIO;
2000 goto error_out;
2001 }
2002 if (retval == POSITION_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002003 reiserfs_warning(inode->i_sb, "PAP-5710",
2004 "entry or pasted byte (%K) exists",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002005 key);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002006 retval = -EEXIST;
2007 goto error_out;
2008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009#ifdef CONFIG_REISERFS_CHECK
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002010 check_research_for_paste(search_path, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002014 /* Perform balancing after all resources are collected by fix_nodes, and
2015 accessing them will not risk triggering schedule. */
2016 if (retval == CARRY_ON) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002017 do_balance(&s_paste_balance, NULL /*ih */ , body, M_PASTE);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002018 return 0;
2019 }
2020 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2021 error_out:
2022 /* this also releases the path */
2023 unfix_nodes(&s_paste_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002025 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2026 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
Jeff Mahoneyee939612009-03-30 14:02:50 -04002027 pasted_size, inode->i_uid,
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002028 key2type(&(key->on_disk_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029#endif
Linus Torvaldse1c50242009-03-30 12:29:21 -07002030 vfs_dq_free_space_nodirty(inode, pasted_size);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002031 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002034/* Insert new item into the buffer at the path.
2035 * th - active transaction handle
2036 * path - path to the inserted item
2037 * ih - pointer to the item header to insert
2038 * body - pointer to the bytes to insert
2039 */
2040int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
2041 struct treepath *path, const struct cpu_key *key,
2042 struct item_head *ih, struct inode *inode,
2043 const char *body)
2044{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002045 struct tree_balance s_ins_balance;
2046 int retval;
2047 int fs_gen = 0;
2048 int quota_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002050 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002052 if (inode) { /* Do we count quotas for item? */
2053 fs_gen = get_generation(inode->i_sb);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002054 quota_bytes = ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002056 /* hack so the quota code doesn't have to guess if the file has
2057 ** a tail, links are always tails, so there's no guessing needed
2058 */
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002059 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002060 quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002062 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2063 "reiserquota insert_item(): allocating %u id=%u type=%c",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002064 quota_bytes, inode->i_uid, head2type(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002066 /* We can't dirty inode here. It would be immediately written but
2067 * appropriate stat item isn't inserted yet... */
Jan Kara77db4f22009-01-26 17:14:18 +01002068 if (vfs_dq_alloc_space_nodirty(inode, quota_bytes)) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002069 pathrelse(path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002070 return -EDQUOT;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002073 init_tb_struct(th, &s_ins_balance, th->t_super, path,
2074 IH_SIZE + ih_item_len(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002076 s_ins_balance.key = key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002078 /* DQUOT_* can schedule, must check to be sure calling fix_nodes is safe */
2079 if (inode && fs_changed(fs_gen, inode->i_sb)) {
2080 goto search_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002082
2083 while ((retval =
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002084 fix_nodes(M_INSERT, &s_ins_balance, ih,
2085 body)) == REPEAT_SEARCH) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002086 search_again:
2087 /* file system changed while we were in the fix_nodes */
2088 PROC_INFO_INC(th->t_super, insert_item_restarted);
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002089 retval = search_item(th->t_super, key, path);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002090 if (retval == IO_ERROR) {
2091 retval = -EIO;
2092 goto error_out;
2093 }
2094 if (retval == ITEM_FOUND) {
Jeff Mahoney45b03d52009-03-30 14:02:21 -04002095 reiserfs_warning(th->t_super, "PAP-5760",
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002096 "key %K already exists in the tree",
2097 key);
2098 retval = -EEXIST;
2099 goto error_out;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002103 /* make balancing after all resources will be collected at a time */
2104 if (retval == CARRY_ON) {
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002105 do_balance(&s_ins_balance, ih, body, M_INSERT);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002106 return 0;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002109 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2110 error_out:
2111 /* also releases the path */
2112 unfix_nodes(&s_ins_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002114 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
2115 "reiserquota insert_item(): freeing %u id=%u type=%c",
Jeff Mahoneyd68caa92009-03-30 14:02:49 -04002116 quota_bytes, inode->i_uid, head2type(ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002118 if (inode)
Jan Kara77db4f22009-01-26 17:14:18 +01002119 vfs_dq_free_space_nodirty(inode, quota_bytes);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002120 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}