blob: 8b9b131271363f2f9b3ef5ed9bcd7fa55a35cab2 [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
26 * decrement_counters_in_path
27 * reiserfs_check_path
28 * pathrelse_and_restore
29 * pathrelse
30 * search_by_key_reada
31 * search_by_key
32 * search_for_position_by_key
33 * comp_items
34 * prepare_for_direct_item
35 * prepare_for_direntry_item
36 * prepare_for_delete_or_cut
37 * calc_deleted_bytes_number
38 * init_tb_struct
39 * padd_item
40 * reiserfs_delete_item
41 * reiserfs_delete_solid_item
42 * reiserfs_delete_object
43 * maybe_indirect_to_direct
44 * indirect_to_direct_roll_back
45 * reiserfs_cut_from_item
46 * truncate_directory
47 * reiserfs_do_truncate
48 * reiserfs_paste_into_item
49 * reiserfs_insert_item
50 */
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/time.h>
53#include <linux/string.h>
54#include <linux/pagemap.h>
55#include <linux/reiserfs_fs.h>
56#include <linux/smp_lock.h>
57#include <linux/buffer_head.h>
58#include <linux/quotaops.h>
59
60/* Does the buffer contain a disk block which is in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061inline int B_IS_IN_TREE(const struct buffer_head *p_s_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070064 RFALSE(B_LEVEL(p_s_bh) > MAX_HEIGHT,
65 "PAP-1010: block (%b) has too big level (%z)", p_s_bh, p_s_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070067 return (B_LEVEL(p_s_bh) != FREE_LEVEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70//
71// to gets item head in le form
72//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070073inline void copy_item_head(struct item_head *p_v_to,
74 const struct item_head *p_v_from)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070076 memcpy(p_v_to, p_v_from, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* k1 is pointer to on-disk structure which is stored in little-endian
80 form. k2 is pointer to cpu variable. For key of items of the same
81 object this returns 0.
82 Returns: -1 if key1 < key2
83 0 if key1 == key2
84 1 if key1 > key2 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070085inline int comp_short_keys(const struct reiserfs_key *le_key,
86 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070088 __u32 n;
89 n = le32_to_cpu(le_key->k_dir_id);
90 if (n < cpu_key->on_disk_key.k_dir_id)
91 return -1;
92 if (n > cpu_key->on_disk_key.k_dir_id)
93 return 1;
94 n = le32_to_cpu(le_key->k_objectid);
95 if (n < cpu_key->on_disk_key.k_objectid)
96 return -1;
97 if (n > cpu_key->on_disk_key.k_objectid)
98 return 1;
99 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* k1 is pointer to on-disk structure which is stored in little-endian
103 form. k2 is pointer to cpu variable.
104 Compare keys using all 4 key fields.
105 Returns: -1 if key1 < key2 0
106 if key1 = key2 1 if key1 > key2 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700107static inline int comp_keys(const struct reiserfs_key *le_key,
108 const struct cpu_key *cpu_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700110 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700112 retval = comp_short_keys(le_key, cpu_key);
113 if (retval)
114 return retval;
115 if (le_key_k_offset(le_key_version(le_key), le_key) <
116 cpu_key_k_offset(cpu_key))
117 return -1;
118 if (le_key_k_offset(le_key_version(le_key), le_key) >
119 cpu_key_k_offset(cpu_key))
120 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122 if (cpu_key->key_length == 3)
123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700125 /* this part is needed only when tail conversion is in progress */
126 if (le_key_k_type(le_key_version(le_key), le_key) <
127 cpu_key_k_type(cpu_key))
128 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700130 if (le_key_k_type(le_key_version(le_key), le_key) >
131 cpu_key_k_type(cpu_key))
132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700137inline int comp_short_le_keys(const struct reiserfs_key *key1,
138 const struct reiserfs_key *key2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700140 __u32 *p_s_1_u32, *p_s_2_u32;
141 int n_key_length = REISERFS_SHORT_KEY_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700143 p_s_1_u32 = (__u32 *) key1;
144 p_s_2_u32 = (__u32 *) key2;
145 for (; n_key_length--; ++p_s_1_u32, ++p_s_2_u32) {
146 if (le32_to_cpu(*p_s_1_u32) < le32_to_cpu(*p_s_2_u32))
147 return -1;
148 if (le32_to_cpu(*p_s_1_u32) > le32_to_cpu(*p_s_2_u32))
149 return 1;
150 }
151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700154inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700156 int version;
157 to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
158 to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
159
160 // find out version of the key
161 version = le_key_version(from);
162 to->version = version;
163 to->on_disk_key.k_offset = le_key_k_offset(version, from);
164 to->on_disk_key.k_type = le_key_k_type(version, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167// this does not say which one is bigger, it only returns 1 if keys
168// are not equal, 0 otherwise
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700169inline int comp_le_keys(const struct reiserfs_key *k1,
170 const struct reiserfs_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700172 return memcmp(k1, k2, sizeof(struct reiserfs_key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/**************************************************************************
176 * Binary search toolkit function *
177 * Search for an item in the array by the item key *
178 * Returns: 1 if found, 0 if not found; *
179 * *p_n_pos = number of the searched element if found, else the *
180 * number of the first element that is larger than p_v_key. *
181 **************************************************************************/
182/* For those not familiar with binary search: n_lbound is the leftmost item that it
183 could be, n_rbound the rightmost item that it could be. We examine the item
184 halfway between n_lbound and n_rbound, and that tells us either that we can increase
185 n_lbound, or decrease n_rbound, or that we have found it, or if n_lbound <= n_rbound that
186 there are no possible items, and we have not found it. With each examination we
187 cut the number of possible items it could be by one more than half rounded down,
188 or we find it. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700189static inline int bin_search(const void *p_v_key, /* Key to search for. */
190 const void *p_v_base, /* First item in the array. */
191 int p_n_num, /* Number of items in the array. */
192 int p_n_width, /* Item size in the array.
193 searched. Lest the reader be
194 confused, note that this is crafted
195 as a general function, and when it
196 is applied specifically to the array
197 of item headers in a node, p_n_width
198 is actually the item header size not
199 the item size. */
200 int *p_n_pos /* Number of the searched for element. */
201 )
202{
203 int n_rbound, n_lbound, n_j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700205 for (n_j = ((n_rbound = p_n_num - 1) + (n_lbound = 0)) / 2;
206 n_lbound <= n_rbound; n_j = (n_rbound + n_lbound) / 2)
207 switch (comp_keys
208 ((struct reiserfs_key *)((char *)p_v_base +
209 n_j * p_n_width),
210 (struct cpu_key *)p_v_key)) {
211 case -1:
212 n_lbound = n_j + 1;
213 continue;
214 case 1:
215 n_rbound = n_j - 1;
216 continue;
217 case 0:
218 *p_n_pos = n_j;
219 return ITEM_FOUND; /* Key found in the array. */
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222 /* bin_search did not find given key, it returns position of key,
223 that is minimal and greater than the given one. */
224 *p_n_pos = n_lbound;
225 return ITEM_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700229extern struct tree_balance *cur_tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#endif
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* Minimal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235/* Maximal possible key. It is never in the tree. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700236static const struct reiserfs_key MAX_KEY = {
Al Viro3e8962b2005-05-01 08:59:18 -0700237 __constant_cpu_to_le32(0xffffffff),
238 __constant_cpu_to_le32(0xffffffff),
239 {{__constant_cpu_to_le32(0xffffffff),
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 __constant_cpu_to_le32(0xffffffff)},}
Al Viro3e8962b2005-05-01 08:59:18 -0700241};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom
244 of the path, and going upwards. We must check the path's validity at each step. If the key is not in
245 the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this
246 case we return a special key, either MIN_KEY or MAX_KEY. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700247static inline const struct reiserfs_key *get_lkey(const struct path
248 *p_s_chk_path,
249 const struct super_block
250 *p_s_sb)
251{
252 int n_position, n_path_offset = p_s_chk_path->path_length;
253 struct buffer_head *p_s_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700255 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
256 "PAP-5010: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700258 /* While not higher in path than first element. */
259 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700261 RFALSE(!buffer_uptodate
262 (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
263 "PAP-5020: parent is not uptodate");
264
265 /* Parent at the path is not in the tree now. */
266 if (!B_IS_IN_TREE
267 (p_s_parent =
268 PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
269 return &MAX_KEY;
270 /* Check whether position in the parent is correct. */
271 if ((n_position =
272 PATH_OFFSET_POSITION(p_s_chk_path,
273 n_path_offset)) >
274 B_NR_ITEMS(p_s_parent))
275 return &MAX_KEY;
276 /* Check whether parent at the path really points to the child. */
277 if (B_N_CHILD_NUM(p_s_parent, n_position) !=
278 PATH_OFFSET_PBUFFER(p_s_chk_path,
279 n_path_offset + 1)->b_blocknr)
280 return &MAX_KEY;
281 /* Return delimiting key if position in the parent is not equal to zero. */
282 if (n_position)
283 return B_N_PDELIM_KEY(p_s_parent, n_position - 1);
284 }
285 /* Return MIN_KEY if we are in the root of the buffer tree. */
286 if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
287 b_blocknr == SB_ROOT_BLOCK(p_s_sb))
288 return &MIN_KEY;
289 return &MAX_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/* Get delimiting key of the buffer at the path and its right neighbor. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700293inline const struct reiserfs_key *get_rkey(const struct path *p_s_chk_path,
294 const struct super_block *p_s_sb)
295{
296 int n_position, n_path_offset = p_s_chk_path->path_length;
297 struct buffer_head *p_s_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700299 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
300 "PAP-5030: invalid offset in the path");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700302 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700304 RFALSE(!buffer_uptodate
305 (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
306 "PAP-5040: parent is not uptodate");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308 /* Parent at the path is not in the tree now. */
309 if (!B_IS_IN_TREE
310 (p_s_parent =
311 PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
312 return &MIN_KEY;
313 /* Check whether position in the parent is correct. */
314 if ((n_position =
315 PATH_OFFSET_POSITION(p_s_chk_path,
316 n_path_offset)) >
317 B_NR_ITEMS(p_s_parent))
318 return &MIN_KEY;
319 /* Check whether parent at the path really points to the child. */
320 if (B_N_CHILD_NUM(p_s_parent, n_position) !=
321 PATH_OFFSET_PBUFFER(p_s_chk_path,
322 n_path_offset + 1)->b_blocknr)
323 return &MIN_KEY;
324 /* Return delimiting key if position in the parent is not the last one. */
325 if (n_position != B_NR_ITEMS(p_s_parent))
326 return B_N_PDELIM_KEY(p_s_parent, n_position);
327 }
328 /* Return MAX_KEY if we are in the root of the buffer tree. */
329 if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
330 b_blocknr == SB_ROOT_BLOCK(p_s_sb))
331 return &MAX_KEY;
332 return &MIN_KEY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* Check whether a key is contained in the tree rooted from a buffer at a path. */
336/* This works by looking at the left and right delimiting keys for the buffer in the last path_element in
337 the path. These delimiting keys are stored at least one level above that buffer in the tree. If the
338 buffer is the first or last node in the tree order then one of the delimiting keys may be absent, and in
339 this case get_lkey and get_rkey return a special key which is MIN_KEY or MAX_KEY. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700340static inline int key_in_buffer(struct path *p_s_chk_path, /* Path which should be checked. */
341 const struct cpu_key *p_s_key, /* Key which should be checked. */
342 struct super_block *p_s_sb /* Super block pointer. */
343 )
344{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700346 RFALSE(!p_s_key || p_s_chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
347 || p_s_chk_path->path_length > MAX_HEIGHT,
348 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
349 p_s_key, p_s_chk_path->path_length);
350 RFALSE(!PATH_PLAST_BUFFER(p_s_chk_path)->b_bdev,
351 "PAP-5060: device must not be NODEV");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700353 if (comp_keys(get_lkey(p_s_chk_path, p_s_sb), p_s_key) == 1)
354 /* left delimiting key is bigger, that the key we look for */
355 return 0;
356 // if ( comp_keys(p_s_key, get_rkey(p_s_chk_path, p_s_sb)) != -1 )
357 if (comp_keys(get_rkey(p_s_chk_path, p_s_sb), p_s_key) != 1)
358 /* p_s_key must be less than right delimitiing key */
359 return 0;
360 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700363inline void decrement_bcount(struct buffer_head *p_s_bh)
364{
365 if (p_s_bh) {
366 if (atomic_read(&(p_s_bh->b_count))) {
367 put_bh(p_s_bh);
368 return;
369 }
370 reiserfs_panic(NULL,
371 "PAP-5070: decrement_bcount: trying to free free buffer %b",
372 p_s_bh);
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* Decrement b_count field of the all buffers in the path. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700377void decrement_counters_in_path(struct path *p_s_search_path)
378{
379 int n_path_offset = p_s_search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET ||
382 n_path_offset > EXTENDED_MAX_HEIGHT - 1,
383 "PAP-5080: invalid path offset of %d", n_path_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700385 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
386 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700388 bh = PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--);
389 decrement_bcount(bh);
390 }
391 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700394int reiserfs_check_path(struct path *p)
395{
396 RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
397 "path not properly relsed");
398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401/* Release all buffers in the path. Restore dirty bits clean
402** when preparing the buffer for the log
403**
404** only called from fix_nodes()
405*/
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700406void pathrelse_and_restore(struct super_block *s, struct path *p_s_search_path)
407{
408 int n_path_offset = p_s_search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700410 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
411 "clm-4000: invalid path offset");
412
413 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
414 reiserfs_restore_prepared_buffer(s,
415 PATH_OFFSET_PBUFFER
416 (p_s_search_path,
417 n_path_offset));
418 brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
419 }
420 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/* Release all buffers in the path. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700424void pathrelse(struct path *p_s_search_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700426 int n_path_offset = p_s_search_path->path_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
429 "PAP-5090: invalid path offset");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700431 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
432 brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700434 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700437static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
438{
439 struct block_head *blkh;
440 struct item_head *ih;
441 int used_space;
442 int prev_location;
443 int i;
444 int nr;
445
446 blkh = (struct block_head *)buf;
447 if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
448 reiserfs_warning(NULL,
449 "is_leaf: this should be caught earlier");
450 return 0;
451 }
452
453 nr = blkh_nr_item(blkh);
454 if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
455 /* item number is too big or too small */
456 reiserfs_warning(NULL, "is_leaf: nr_item seems wrong: %z", bh);
457 return 0;
458 }
459 ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
460 used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
461 if (used_space != blocksize - blkh_free_space(blkh)) {
462 /* free space does not match to calculated amount of use space */
463 reiserfs_warning(NULL, "is_leaf: free space seems wrong: %z",
464 bh);
465 return 0;
466 }
467 // FIXME: it is_leaf will hit performance too much - we may have
468 // return 1 here
469
470 /* check tables of item heads */
471 ih = (struct item_head *)(buf + BLKH_SIZE);
472 prev_location = blocksize;
473 for (i = 0; i < nr; i++, ih++) {
474 if (le_ih_k_type(ih) == TYPE_ANY) {
475 reiserfs_warning(NULL,
476 "is_leaf: wrong item type for item %h",
477 ih);
478 return 0;
479 }
480 if (ih_location(ih) >= blocksize
481 || ih_location(ih) < IH_SIZE * nr) {
482 reiserfs_warning(NULL,
483 "is_leaf: item location seems wrong: %h",
484 ih);
485 return 0;
486 }
487 if (ih_item_len(ih) < 1
488 || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
489 reiserfs_warning(NULL,
490 "is_leaf: item length seems wrong: %h",
491 ih);
492 return 0;
493 }
494 if (prev_location - ih_location(ih) != ih_item_len(ih)) {
495 reiserfs_warning(NULL,
496 "is_leaf: item location seems wrong (second one): %h",
497 ih);
498 return 0;
499 }
500 prev_location = ih_location(ih);
501 }
502
503 // one may imagine much more checks
504 return 1;
505}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507/* returns 1 if buf looks like an internal node, 0 otherwise */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700508static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700510 struct block_head *blkh;
511 int nr;
512 int used_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700514 blkh = (struct block_head *)buf;
515 nr = blkh_level(blkh);
516 if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
517 /* this level is not possible for internal nodes */
518 reiserfs_warning(NULL,
519 "is_internal: this should be caught earlier");
520 return 0;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700523 nr = blkh_nr_item(blkh);
524 if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
525 /* for internal which is not root we might check min number of keys */
526 reiserfs_warning(NULL,
527 "is_internal: number of key seems wrong: %z",
528 bh);
529 return 0;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532 used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
533 if (used_space != blocksize - blkh_free_space(blkh)) {
534 reiserfs_warning(NULL,
535 "is_internal: free space seems wrong: %z", bh);
536 return 0;
537 }
538 // one may imagine much more checks
539 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542// make sure that bh contains formatted node of reiserfs tree of
543// 'level'-th level
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700544static int is_tree_node(struct buffer_head *bh, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700546 if (B_LEVEL(bh) != level) {
547 reiserfs_warning(NULL,
548 "is_tree_node: node level %d does not match to the expected one %d",
549 B_LEVEL(bh), level);
550 return 0;
551 }
552 if (level == DISK_LEAF_NODE_LEVEL)
553 return is_leaf(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700555 return is_internal(bh->b_data, bh->b_size, bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#define SEARCH_BY_KEY_READA 16
559
560/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561static void search_by_key_reada(struct super_block *s,
562 struct buffer_head **bh,
563 unsigned long *b, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700565 int i, j;
566
567 for (i = 0; i < num; i++) {
568 bh[i] = sb_getblk(s, b[i]);
569 }
570 for (j = 0; j < i; j++) {
571 /*
572 * note, this needs attention if we are getting rid of the BKL
573 * you have to make sure the prepared bit isn't set on this buffer
574 */
575 if (!buffer_uptodate(bh[j]))
576 ll_rw_block(READA, 1, bh + j);
577 brelse(bh[j]);
578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/**************************************************************************
582 * Algorithm SearchByKey *
583 * look for item in the Disk S+Tree by its key *
584 * Input: p_s_sb - super block *
585 * p_s_key - pointer to the key to search *
586 * Output: ITEM_FOUND, ITEM_NOT_FOUND or IO_ERROR *
587 * p_s_search_path - path from the root to the needed leaf *
588 **************************************************************************/
589
590/* This function fills up the path from the root to the leaf as it
591 descends the tree looking for the key. It uses reiserfs_bread to
592 try to find buffers in the cache given their block number. If it
593 does not find them in the cache it reads them from disk. For each
594 node search_by_key finds using reiserfs_bread it then uses
595 bin_search to look through that node. bin_search will find the
596 position of the block_number of the next node if it is looking
597 through an internal node. If it is looking through a leaf node
598 bin_search will find the position of the item which has key either
599 equal to given key, or which is the maximal key less than the given
600 key. search_by_key returns a path that must be checked for the
601 correctness of the top of the path but need not be checked for the
602 correctness of the bottom of the path */
603/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700604int search_by_key(struct super_block *p_s_sb, const struct cpu_key *p_s_key, /* Key to search. */
605 struct path *p_s_search_path, /* This structure was
606 allocated and initialized
607 by the calling
608 function. It is filled up
609 by this function. */
610 int n_stop_level /* How far down the tree to search. To
611 stop at leaf level - set to
612 DISK_LEAF_NODE_LEVEL */
613 )
614{
615 int n_block_number;
616 int expected_level;
617 struct buffer_head *p_s_bh;
618 struct path_element *p_s_last_element;
619 int n_node_level, n_retval;
620 int right_neighbor_of_leaf_node;
621 int fs_gen;
622 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
623 unsigned long reada_blocks[SEARCH_BY_KEY_READA];
624 int reada_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700627 int n_repeat_counter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700630 PROC_INFO_INC(p_s_sb, search_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 /* As we add each node to a path we increase its count. This means that
633 we must be careful to release all nodes in a path before we either
634 discard the path struct or re-use the path struct, as we do here. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700636 decrement_counters_in_path(p_s_search_path);
637
638 right_neighbor_of_leaf_node = 0;
639
640 /* With each iteration of this loop we search through the items in the
641 current node, and calculate the next current node(next path element)
642 for the next iteration of this loop.. */
643 n_block_number = SB_ROOT_BLOCK(p_s_sb);
644 expected_level = -1;
645 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700648 if (!(++n_repeat_counter % 50000))
649 reiserfs_warning(p_s_sb, "PAP-5100: search_by_key: %s:"
650 "there were %d iterations of while loop "
651 "looking for key %K",
652 current->comm, n_repeat_counter,
653 p_s_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
655
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656 /* prep path to have another element added to it. */
657 p_s_last_element =
658 PATH_OFFSET_PELEMENT(p_s_search_path,
659 ++p_s_search_path->path_length);
660 fs_gen = get_generation(p_s_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700662 /* Read the next tree node, and set the last element in the path to
663 have a pointer to it. */
664 if ((p_s_bh = p_s_last_element->pe_buffer =
665 sb_getblk(p_s_sb, n_block_number))) {
666 if (!buffer_uptodate(p_s_bh) && reada_count > 1) {
667 search_by_key_reada(p_s_sb, reada_bh,
668 reada_blocks, reada_count);
669 }
670 ll_rw_block(READ, 1, &p_s_bh);
671 wait_on_buffer(p_s_bh);
672 if (!buffer_uptodate(p_s_bh))
673 goto io_error;
674 } else {
675 io_error:
676 p_s_search_path->path_length--;
677 pathrelse(p_s_search_path);
678 return IO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700680 reada_count = 0;
681 if (expected_level == -1)
682 expected_level = SB_TREE_HEIGHT(p_s_sb);
683 expected_level--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700685 /* It is possible that schedule occurred. We must check whether the key
686 to search is still in the tree rooted from the current buffer. If
687 not then repeat search from the root. */
688 if (fs_changed(fs_gen, p_s_sb) &&
689 (!B_IS_IN_TREE(p_s_bh) ||
690 B_LEVEL(p_s_bh) != expected_level ||
691 !key_in_buffer(p_s_search_path, p_s_key, p_s_sb))) {
692 PROC_INFO_INC(p_s_sb, search_by_key_fs_changed);
693 PROC_INFO_INC(p_s_sb, search_by_key_restarted);
694 PROC_INFO_INC(p_s_sb,
695 sbk_restarted[expected_level - 1]);
696 decrement_counters_in_path(p_s_search_path);
697
698 /* Get the root block number so that we can repeat the search
699 starting from the root. */
700 n_block_number = SB_ROOT_BLOCK(p_s_sb);
701 expected_level = -1;
702 right_neighbor_of_leaf_node = 0;
703
704 /* repeat search from the root */
705 continue;
706 }
707
708 /* only check that the key is in the buffer if p_s_key is not
709 equal to the MAX_KEY. Latter case is only possible in
710 "finish_unfinished()" processing during mount. */
711 RFALSE(comp_keys(&MAX_KEY, p_s_key) &&
712 !key_in_buffer(p_s_search_path, p_s_key, p_s_sb),
713 "PAP-5130: key is not in the buffer");
714#ifdef CONFIG_REISERFS_CHECK
715 if (cur_tb) {
716 print_cur_tb("5140");
717 reiserfs_panic(p_s_sb,
718 "PAP-5140: search_by_key: schedule occurred in do_balance!");
719 }
720#endif
721
722 // make sure, that the node contents look like a node of
723 // certain level
724 if (!is_tree_node(p_s_bh, expected_level)) {
725 reiserfs_warning(p_s_sb, "vs-5150: search_by_key: "
726 "invalid format found in block %ld. Fsck?",
727 p_s_bh->b_blocknr);
728 pathrelse(p_s_search_path);
729 return IO_ERROR;
730 }
731
732 /* ok, we have acquired next formatted node in the tree */
733 n_node_level = B_LEVEL(p_s_bh);
734
735 PROC_INFO_BH_STAT(p_s_sb, p_s_bh, n_node_level - 1);
736
737 RFALSE(n_node_level < n_stop_level,
738 "vs-5152: tree level (%d) is less than stop level (%d)",
739 n_node_level, n_stop_level);
740
741 n_retval = bin_search(p_s_key, B_N_PITEM_HEAD(p_s_bh, 0),
742 B_NR_ITEMS(p_s_bh),
743 (n_node_level ==
744 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
745 KEY_SIZE,
746 &(p_s_last_element->pe_position));
747 if (n_node_level == n_stop_level) {
748 return n_retval;
749 }
750
751 /* we are not in the stop level */
752 if (n_retval == ITEM_FOUND)
753 /* item has been found, so we choose the pointer which is to the right of the found one */
754 p_s_last_element->pe_position++;
755
756 /* if item was not found we choose the position which is to
757 the left of the found item. This requires no code,
758 bin_search did it already. */
759
760 /* So we have chosen a position in the current node which is
761 an internal node. Now we calculate child block number by
762 position in the node. */
763 n_block_number =
764 B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position);
765
766 /* if we are going to read leaf nodes, try for read ahead as well */
767 if ((p_s_search_path->reada & PATH_READA) &&
768 n_node_level == DISK_LEAF_NODE_LEVEL + 1) {
769 int pos = p_s_last_element->pe_position;
770 int limit = B_NR_ITEMS(p_s_bh);
771 struct reiserfs_key *le_key;
772
773 if (p_s_search_path->reada & PATH_READA_BACK)
774 limit = 0;
775 while (reada_count < SEARCH_BY_KEY_READA) {
776 if (pos == limit)
777 break;
778 reada_blocks[reada_count++] =
779 B_N_CHILD_NUM(p_s_bh, pos);
780 if (p_s_search_path->reada & PATH_READA_BACK)
781 pos--;
782 else
783 pos++;
784
785 /*
786 * check to make sure we're in the same object
787 */
788 le_key = B_N_PDELIM_KEY(p_s_bh, pos);
789 if (le32_to_cpu(le_key->k_objectid) !=
790 p_s_key->on_disk_key.k_objectid) {
791 break;
792 }
793 }
794 }
795 }
796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798/* Form the path to an item and position in this item which contains
799 file byte defined by p_s_key. If there is no such item
800 corresponding to the key, we point the path to the item with
801 maximal key less than p_s_key, and *p_n_pos_in_item is set to one
802 past the last entry/byte in the item. If searching for entry in a
803 directory item, and it is not found, *p_n_pos_in_item is set to one
804 entry more than the entry with maximal key which is less than the
805 sought key.
806
807 Note that if there is no entry in this same node which is one more,
808 then we point to an imaginary entry. for direct items, the
809 position is in units of bytes, for indirect items the position is
810 in units of blocknr entries, for directory items the position is in
811 units of directory entries. */
812
813/* The function is NOT SCHEDULE-SAFE! */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700814int search_for_position_by_key(struct super_block *p_s_sb, /* Pointer to the super block. */
815 const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
816 struct path *p_s_search_path /* Filled up by this function. */
817 )
818{
819 struct item_head *p_le_ih; /* pointer to on-disk structure */
820 int n_blk_size;
821 loff_t item_offset, offset;
822 struct reiserfs_dir_entry de;
823 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700825 /* If searching for directory entry. */
826 if (is_direntry_cpu_key(p_cpu_key))
827 return search_by_entry_key(p_s_sb, p_cpu_key, p_s_search_path,
828 &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700830 /* If not searching for directory entry. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700832 /* If item is found. */
833 retval = search_item(p_s_sb, p_cpu_key, p_s_search_path);
834 if (retval == IO_ERROR)
835 return retval;
836 if (retval == ITEM_FOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700838 RFALSE(!ih_item_len
839 (B_N_PITEM_HEAD
840 (PATH_PLAST_BUFFER(p_s_search_path),
841 PATH_LAST_POSITION(p_s_search_path))),
842 "PAP-5165: item length equals zero");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700844 pos_in_item(p_s_search_path) = 0;
845 return POSITION_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700848 RFALSE(!PATH_LAST_POSITION(p_s_search_path),
849 "PAP-5170: position equals zero");
850
851 /* Item is not found. Set path to the previous item. */
852 p_le_ih =
853 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path),
854 --PATH_LAST_POSITION(p_s_search_path));
855 n_blk_size = p_s_sb->s_blocksize;
856
857 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
858 return FILE_NOT_FOUND;
859 }
860 // FIXME: quite ugly this far
861
862 item_offset = le_ih_k_offset(p_le_ih);
863 offset = cpu_key_k_offset(p_cpu_key);
864
865 /* Needed byte is contained in the item pointed to by the path. */
866 if (item_offset <= offset &&
867 item_offset + op_bytes_number(p_le_ih, n_blk_size) > offset) {
868 pos_in_item(p_s_search_path) = offset - item_offset;
869 if (is_indirect_le_ih(p_le_ih)) {
870 pos_in_item(p_s_search_path) /= n_blk_size;
871 }
872 return POSITION_FOUND;
873 }
874
875 /* Needed byte is not contained in the item pointed to by the
876 path. Set pos_in_item out of the item. */
877 if (is_indirect_le_ih(p_le_ih))
878 pos_in_item(p_s_search_path) =
879 ih_item_len(p_le_ih) / UNFM_P_SIZE;
880 else
881 pos_in_item(p_s_search_path) = ih_item_len(p_le_ih);
882
883 return POSITION_NOT_FOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/* Compare given item and item pointed to by the path. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700887int comp_items(const struct item_head *stored_ih, const struct path *p_s_path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700889 struct buffer_head *p_s_bh;
890 struct item_head *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700892 /* Last buffer at the path is not in the tree. */
893 if (!B_IS_IN_TREE(p_s_bh = PATH_PLAST_BUFFER(p_s_path)))
894 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700896 /* Last path position is invalid. */
897 if (PATH_LAST_POSITION(p_s_path) >= B_NR_ITEMS(p_s_bh))
898 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700900 /* we need only to know, whether it is the same item */
901 ih = get_ih(p_s_path);
902 return memcmp(stored_ih, ih, IH_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905/* unformatted nodes are not logged anymore, ever. This is safe
906** now
907*/
908#define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
909
910// block can not be forgotten as it is in I/O or held by someone
911#define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913// prepare for delete or cut of direct item
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700914static inline int prepare_for_direct_item(struct path *path,
915 struct item_head *le_ih,
916 struct inode *inode,
917 loff_t new_file_length, int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700919 loff_t round_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700921 if (new_file_length == max_reiserfs_offset(inode)) {
922 /* item has to be deleted */
923 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
924 return M_DELETE;
925 }
926 // new file gets truncated
927 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
928 //
929 round_len = ROUND_UP(new_file_length);
930 /* this was n_new_file_length < le_ih ... */
931 if (round_len < le_ih_k_offset(le_ih)) {
932 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
933 return M_DELETE; /* Delete this item. */
934 }
935 /* Calculate first position and size for cutting from item. */
936 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
937 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700939 return M_CUT; /* Cut from this item. */
940 }
941
942 // old file: items may have any length
943
944 if (new_file_length < le_ih_k_offset(le_ih)) {
945 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
946 return M_DELETE; /* Delete this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 /* Calculate first position and size for cutting from item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700949 *cut_size = -(ih_item_len(le_ih) -
950 (pos_in_item(path) =
951 new_file_length + 1 - le_ih_k_offset(le_ih)));
952 return M_CUT; /* Cut from this item. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700955static inline int prepare_for_direntry_item(struct path *path,
956 struct item_head *le_ih,
957 struct inode *inode,
958 loff_t new_file_length,
959 int *cut_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700961 if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
962 new_file_length == max_reiserfs_offset(inode)) {
963 RFALSE(ih_entry_count(le_ih) != 2,
964 "PAP-5220: incorrect empty directory item (%h)", le_ih);
965 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
966 return M_DELETE; /* Delete the directory item containing "." and ".." entry. */
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700969 if (ih_entry_count(le_ih) == 1) {
970 /* Delete the directory item such as there is one record only
971 in this item */
972 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
973 return M_DELETE;
974 }
975
976 /* Cut one record from the directory item. */
977 *cut_size =
978 -(DEH_SIZE +
979 entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
980 return M_CUT;
981}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -0800983#define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
986 If the path points to an indirect item, remove some number of its unformatted nodes.
987 In case of file truncate calculate whether this item must be deleted/truncated or last
988 unformatted node of this item will be converted to a direct item.
989 This function returns a determination of what balance mode the calling function should employ. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700990static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct path *p_s_path, const struct cpu_key *p_s_item_key, int *p_n_removed, /* Number of unformatted nodes which were removed
991 from end of the file. */
992 int *p_n_cut_size, unsigned long long n_new_file_length /* MAX_KEY_OFFSET in case of delete. */
993 )
994{
995 struct super_block *p_s_sb = inode->i_sb;
996 struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_path);
997 struct buffer_head *p_s_bh = PATH_PLAST_BUFFER(p_s_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700999 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001001 /* Stat_data item. */
1002 if (is_statdata_le_ih(p_le_ih)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001004 RFALSE(n_new_file_length != max_reiserfs_offset(inode),
1005 "PAP-5210: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001007 *p_n_cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
1008 return M_DELETE;
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001011 /* Directory item. */
1012 if (is_direntry_le_ih(p_le_ih))
1013 return prepare_for_direntry_item(p_s_path, p_le_ih, inode,
1014 n_new_file_length,
1015 p_n_cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001017 /* Direct item. */
1018 if (is_direct_le_ih(p_le_ih))
1019 return prepare_for_direct_item(p_s_path, p_le_ih, inode,
1020 n_new_file_length, p_n_cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001022 /* Case of an indirect item. */
1023 {
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001024 int blk_size = p_s_sb->s_blocksize;
1025 struct item_head s_ih;
1026 int need_re_search;
1027 int delete = 0;
1028 int result = M_CUT;
1029 int pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001031 if ( n_new_file_length == max_reiserfs_offset (inode) ) {
1032 /* prepare_for_delete_or_cut() is called by
1033 * reiserfs_delete_item() */
1034 n_new_file_length = 0;
1035 delete = 1;
1036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001038 do {
1039 need_re_search = 0;
1040 *p_n_cut_size = 0;
1041 p_s_bh = PATH_PLAST_BUFFER(p_s_path);
1042 copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
1043 pos = I_UNFM_NUM(&s_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001045 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > n_new_file_length) {
1046 __u32 *unfm, 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. */
1051 if (!delete && (*p_n_cut_size) != 0 &&
1052 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
1053 break;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001056 unfm = (__u32 *)B_I_PITEM(p_s_bh, &s_ih) + pos - 1;
1057 block = get_block_num(unfm, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001059 if (block != 0) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001060 reiserfs_prepare_for_journal(p_s_sb, p_s_bh, 1);
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001061 put_block_num(unfm, 0, 0);
1062 journal_mark_dirty (th, p_s_sb, p_s_bh);
1063 reiserfs_free_block(th, inode, block, 1);
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001066 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001068 if (item_moved (&s_ih, p_s_path)) {
1069 need_re_search = 1;
1070 break;
1071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001073 pos --;
1074 (*p_n_removed) ++;
1075 (*p_n_cut_size) -= UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001077 if (pos == 0) {
1078 (*p_n_cut_size) -= IH_SIZE;
1079 result = M_DELETE;
1080 break;
1081 }
1082 }
1083 /* a trick. If the buffer has been logged, this will do nothing. If
1084 ** we've broken the loop without logging it, it will restore the
1085 ** buffer */
1086 reiserfs_restore_prepared_buffer(p_s_sb, p_s_bh);
1087 } while (need_re_search &&
1088 search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path) == POSITION_FOUND);
1089 pos_in_item(p_s_path) = pos * UNFM_P_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001091 if (*p_n_cut_size == 0) {
1092 /* Nothing were cut. maybe convert last unformatted node to the
1093 * direct item? */
1094 result = M_CONVERT;
1095 }
1096 return result;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100/* Calculate number of bytes which will be deleted or cut during balance */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001101static int calc_deleted_bytes_number(struct tree_balance *p_s_tb, char c_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001103 int n_del_size;
1104 struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_tb->tb_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001106 if (is_statdata_le_ih(p_le_ih))
1107 return 0;
1108
1109 n_del_size =
1110 (c_mode ==
1111 M_DELETE) ? ih_item_len(p_le_ih) : -p_s_tb->insert_size[0];
1112 if (is_direntry_le_ih(p_le_ih)) {
1113 // return EMPTY_DIR_SIZE; /* We delete emty directoris only. */
1114 // we can't use EMPTY_DIR_SIZE, as old format dirs have a different
1115 // empty size. ick. FIXME, is this right?
1116 //
1117 return n_del_size;
1118 }
1119
1120 if (is_indirect_le_ih(p_le_ih))
1121 n_del_size = (n_del_size / UNFM_P_SIZE) * (PATH_PLAST_BUFFER(p_s_tb->tb_path)->b_size); // - get_ih_free_space (p_le_ih);
1122 return n_del_size;
1123}
1124
1125static void init_tb_struct(struct reiserfs_transaction_handle *th,
1126 struct tree_balance *p_s_tb,
1127 struct super_block *p_s_sb,
1128 struct path *p_s_path, int n_size)
1129{
1130
1131 BUG_ON(!th->t_trans_id);
1132
1133 memset(p_s_tb, '\0', sizeof(struct tree_balance));
1134 p_s_tb->transaction_handle = th;
1135 p_s_tb->tb_sb = p_s_sb;
1136 p_s_tb->tb_path = p_s_path;
1137 PATH_OFFSET_PBUFFER(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1138 PATH_OFFSET_POSITION(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
1139 p_s_tb->insert_size[0] = n_size;
1140}
1141
1142void padd_item(char *item, int total_length, int length)
1143{
1144 int i;
1145
1146 for (i = total_length; i > length;)
1147 item[--i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149
1150#ifdef REISERQUOTA_DEBUG
1151char key2type(struct reiserfs_key *ih)
1152{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001153 if (is_direntry_le_key(2, ih))
1154 return 'd';
1155 if (is_direct_le_key(2, ih))
1156 return 'D';
1157 if (is_indirect_le_key(2, ih))
1158 return 'i';
1159 if (is_statdata_le_key(2, ih))
1160 return 's';
1161 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164char head2type(struct item_head *ih)
1165{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001166 if (is_direntry_le_ih(ih))
1167 return 'd';
1168 if (is_direct_le_ih(ih))
1169 return 'D';
1170 if (is_indirect_le_ih(ih))
1171 return 'i';
1172 if (is_statdata_le_ih(ih))
1173 return 's';
1174 return 'u';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176#endif
1177
1178/* Delete object item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001179int reiserfs_delete_item(struct reiserfs_transaction_handle *th, struct path *p_s_path, /* Path to the deleted item. */
1180 const struct cpu_key *p_s_item_key, /* Key to search for the deleted item. */
1181 struct inode *p_s_inode, /* inode is here just to update i_blocks and quotas */
1182 struct buffer_head *p_s_un_bh)
1183{ /* NULL or unformatted node pointer. */
1184 struct super_block *p_s_sb = p_s_inode->i_sb;
1185 struct tree_balance s_del_balance;
1186 struct item_head s_ih;
1187 struct item_head *q_ih;
1188 int quota_cut_bytes;
1189 int n_ret_value, n_del_size, n_removed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001192 char c_mode;
1193 int n_iter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#endif
1195
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001196 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001198 init_tb_struct(th, &s_del_balance, p_s_sb, p_s_path,
1199 0 /*size is unknown */ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001201 while (1) {
1202 n_removed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001205 n_iter++;
1206 c_mode =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001208 prepare_for_delete_or_cut(th, p_s_inode, p_s_path,
1209 p_s_item_key, &n_removed,
1210 &n_del_size,
1211 max_reiserfs_offset(p_s_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001213 RFALSE(c_mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001215 copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
1216 s_del_balance.insert_size[0] = n_del_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001218 n_ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1219 if (n_ret_value != REPEAT_SEARCH)
1220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001222 PROC_INFO_INC(p_s_sb, delete_item_restarted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001224 // file system changed, repeat search
1225 n_ret_value =
1226 search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path);
1227 if (n_ret_value == IO_ERROR)
1228 break;
1229 if (n_ret_value == FILE_NOT_FOUND) {
1230 reiserfs_warning(p_s_sb,
1231 "vs-5340: reiserfs_delete_item: "
1232 "no items of the file %K found",
1233 p_s_item_key);
1234 break;
1235 }
1236 } /* while (1) */
1237
1238 if (n_ret_value != CARRY_ON) {
1239 unfix_nodes(&s_del_balance);
1240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001242 // reiserfs_delete_item returns item length when success
1243 n_ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
1244 q_ih = get_ih(p_s_path);
1245 quota_cut_bytes = ih_item_len(q_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001247 /* hack so the quota code doesn't have to guess if the file
1248 ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
1249 ** We test the offset because the tail might have been
1250 ** split into multiple items, and we only want to decrement for
1251 ** the unfm node once
1252 */
1253 if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(q_ih)) {
1254 if ((le_ih_k_offset(q_ih) & (p_s_sb->s_blocksize - 1)) == 1) {
1255 quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE;
1256 } else {
1257 quota_cut_bytes = 0;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001261 if (p_s_un_bh) {
1262 int off;
1263 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001265 /* We are in direct2indirect conversion, so move tail contents
1266 to the unformatted node */
1267 /* note, we do the copy before preparing the buffer because we
1268 ** don't care about the contents of the unformatted node yet.
1269 ** the only thing we really care about is the direct item's data
1270 ** is in the unformatted node.
1271 **
1272 ** Otherwise, we would have to call reiserfs_prepare_for_journal on
1273 ** the unformatted node, which might schedule, meaning we'd have to
1274 ** loop all the way back up to the start of the while loop.
1275 **
1276 ** The unformatted node must be dirtied later on. We can't be
1277 ** sure here if the entire tail has been deleted yet.
1278 **
1279 ** p_s_un_bh is from the page cache (all unformatted nodes are
1280 ** from the page cache) and might be a highmem page. So, we
1281 ** can't use p_s_un_bh->b_data.
1282 ** -clm
1283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001285 data = kmap_atomic(p_s_un_bh->b_page, KM_USER0);
1286 off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
1287 memcpy(data + off,
1288 B_I_PITEM(PATH_PLAST_BUFFER(p_s_path), &s_ih),
1289 n_ret_value);
1290 kunmap_atomic(data, KM_USER0);
1291 }
1292 /* Perform balancing after all resources have been collected at once. */
1293 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001296 reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE,
1297 "reiserquota delete_item(): freeing %u, id=%u type=%c",
1298 quota_cut_bytes, p_s_inode->i_uid, head2type(&s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001300 DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001302 /* Return deleted body length */
1303 return n_ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306/* Summary Of Mechanisms For Handling Collisions Between Processes:
1307
1308 deletion of the body of the object is performed by iput(), with the
1309 result that if multiple processes are operating on a file, the
1310 deletion of the body of the file is deferred until the last process
1311 that has an open inode performs its iput().
1312
1313 writes and truncates are protected from collisions by use of
1314 semaphores.
1315
1316 creates, linking, and mknod are protected from collisions with other
1317 processes by making the reiserfs_add_entry() the last step in the
1318 creation, and then rolling back all changes if there was a collision.
1319 - Hans
1320*/
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322/* this deletes item which never gets split */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001323void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1324 struct inode *inode, struct reiserfs_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001326 struct tree_balance tb;
1327 INITIALIZE_PATH(path);
1328 int item_len = 0;
1329 int tb_init = 0;
1330 struct cpu_key cpu_key;
1331 int retval;
1332 int quota_cut_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001334 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001336 le_key2cpu_key(&cpu_key, key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001338 while (1) {
1339 retval = search_item(th->t_super, &cpu_key, &path);
1340 if (retval == IO_ERROR) {
1341 reiserfs_warning(th->t_super,
1342 "vs-5350: reiserfs_delete_solid_item: "
1343 "i/o failure occurred trying to delete %K",
1344 &cpu_key);
1345 break;
1346 }
1347 if (retval != ITEM_FOUND) {
1348 pathrelse(&path);
1349 // No need for a warning, if there is just no free space to insert '..' item into the newly-created subdir
1350 if (!
1351 ((unsigned long long)
1352 GET_HASH_VALUE(le_key_k_offset
1353 (le_key_version(key), key)) == 0
1354 && (unsigned long long)
1355 GET_GENERATION_NUMBER(le_key_k_offset
1356 (le_key_version(key),
1357 key)) == 1))
1358 reiserfs_warning(th->t_super,
1359 "vs-5355: reiserfs_delete_solid_item: %k not found",
1360 key);
1361 break;
1362 }
1363 if (!tb_init) {
1364 tb_init = 1;
1365 item_len = ih_item_len(PATH_PITEM_HEAD(&path));
1366 init_tb_struct(th, &tb, th->t_super, &path,
1367 -(IH_SIZE + item_len));
1368 }
1369 quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path));
1370
1371 retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
1372 if (retval == REPEAT_SEARCH) {
1373 PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
1374 continue;
1375 }
1376
1377 if (retval == CARRY_ON) {
1378 do_balance(&tb, NULL, NULL, M_DELETE);
1379 if (inode) { /* Should we count quota for item? (we don't count quotas for save-links) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001381 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
1382 "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1383 quota_cut_bytes, inode->i_uid,
1384 key2type(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001386 DQUOT_FREE_SPACE_NODIRTY(inode,
1387 quota_cut_bytes);
1388 }
1389 break;
1390 }
1391 // IO_ERROR, NO_DISK_SPACE, etc
1392 reiserfs_warning(th->t_super,
1393 "vs-5360: reiserfs_delete_solid_item: "
1394 "could not delete %K due to fix_nodes failure",
1395 &cpu_key);
1396 unfix_nodes(&tb);
1397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001400 reiserfs_check_path(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001403int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1404 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001406 int err;
1407 inode->i_size = 0;
1408 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001410 /* for directory this deletes item containing "." and ".." */
1411 err =
1412 reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ );
1413 if (err)
1414 return err;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416#if defined( USE_INODE_GENERATION_COUNTER )
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001417 if (!old_format_only(th->t_super)) {
1418 __le32 *inode_generation;
1419
1420 inode_generation =
1421 &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
1422 *inode_generation =
1423 cpu_to_le32(le32_to_cpu(*inode_generation) + 1);
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/* USE_INODE_GENERATION_COUNTER */
1426#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001427 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001432static void unmap_buffers(struct page *page, loff_t pos)
1433{
1434 struct buffer_head *bh;
1435 struct buffer_head *head;
1436 struct buffer_head *next;
1437 unsigned long tail_index;
1438 unsigned long cur_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001440 if (page) {
1441 if (page_has_buffers(page)) {
1442 tail_index = pos & (PAGE_CACHE_SIZE - 1);
1443 cur_index = 0;
1444 head = page_buffers(page);
1445 bh = head;
1446 do {
1447 next = bh->b_this_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001449 /* we want to unmap the buffers that contain the tail, and
1450 ** all the buffers after it (since the tail must be at the
1451 ** end of the file). We don't want to unmap file data
1452 ** before the tail, since it might be dirty and waiting to
1453 ** reach disk
1454 */
1455 cur_index += bh->b_size;
1456 if (cur_index > tail_index) {
1457 reiserfs_unmap_buffer(bh);
1458 }
1459 bh = next;
1460 } while (bh != head);
1461 if (PAGE_SIZE == bh->b_size) {
1462 clear_page_dirty(page);
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001468static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
1469 struct inode *p_s_inode,
1470 struct page *page,
1471 struct path *p_s_path,
1472 const struct cpu_key *p_s_item_key,
1473 loff_t n_new_file_size, char *p_c_mode)
1474{
1475 struct super_block *p_s_sb = p_s_inode->i_sb;
1476 int n_block_size = p_s_sb->s_blocksize;
1477 int cut_bytes;
1478 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001480 if (n_new_file_size != p_s_inode->i_size)
1481 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001483 /* the page being sent in could be NULL if there was an i/o error
1484 ** reading in the last block. The user will hit problems trying to
1485 ** read the file, but for now we just skip the indirect2direct
1486 */
1487 if (atomic_read(&p_s_inode->i_count) > 1 ||
1488 !tail_has_to_be_packed(p_s_inode) ||
1489 !page || (REISERFS_I(p_s_inode)->i_flags & i_nopack_mask)) {
1490 // leave tail in an unformatted node
1491 *p_c_mode = M_SKIP_BALANCING;
1492 cut_bytes =
1493 n_block_size - (n_new_file_size & (n_block_size - 1));
1494 pathrelse(p_s_path);
1495 return cut_bytes;
1496 }
1497 /* Permorm the conversion to a direct_item. */
1498 /*return indirect_to_direct (p_s_inode, p_s_path, p_s_item_key, n_new_file_size, p_c_mode); */
1499 return indirect2direct(th, p_s_inode, page, p_s_path, p_s_item_key,
1500 n_new_file_size, p_c_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/* we did indirect_to_direct conversion. And we have inserted direct
1504 item successesfully, but there were no disk space to cut unfm
1505 pointer being converted. Therefore we have to delete inserted
1506 direct item(s) */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001507static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1508 struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001510 struct cpu_key tail_key;
1511 int tail_len;
1512 int removed;
1513 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001515 make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4); // !!!!
1516 tail_key.key_length = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001518 tail_len =
1519 (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
1520 while (tail_len) {
1521 /* look for the last byte of the tail */
1522 if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1523 POSITION_NOT_FOUND)
1524 reiserfs_panic(inode->i_sb,
1525 "vs-5615: indirect_to_direct_roll_back: found invalid item");
1526 RFALSE(path->pos_in_item !=
1527 ih_item_len(PATH_PITEM_HEAD(path)) - 1,
1528 "vs-5616: appended bytes found");
1529 PATH_LAST_POSITION(path)--;
1530
1531 removed =
1532 reiserfs_delete_item(th, path, &tail_key, inode,
1533 NULL /*unbh not needed */ );
1534 RFALSE(removed <= 0
1535 || removed > tail_len,
1536 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1537 tail_len, removed);
1538 tail_len -= removed;
1539 set_cpu_key_k_offset(&tail_key,
1540 cpu_key_k_offset(&tail_key) - removed);
1541 }
1542 reiserfs_warning(inode->i_sb,
1543 "indirect_to_direct_roll_back: indirect_to_direct conversion has been rolled back due to lack of disk space");
1544 //mark_file_without_tail (inode);
1545 mark_inode_dirty(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001549int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1550 struct path *p_s_path,
1551 struct cpu_key *p_s_item_key,
1552 struct inode *p_s_inode,
1553 struct page *page, loff_t n_new_file_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001555 struct super_block *p_s_sb = p_s_inode->i_sb;
1556 /* Every function which is going to call do_balance must first
1557 create a tree_balance structure. Then it must fill up this
1558 structure by using the init_tb_struct and fix_nodes functions.
1559 After that we can make tree balancing. */
1560 struct tree_balance s_cut_balance;
1561 struct item_head *p_le_ih;
1562 int n_cut_size = 0, /* Amount to be cut. */
1563 n_ret_value = CARRY_ON, n_removed = 0, /* Number of the removed unformatted nodes. */
1564 n_is_inode_locked = 0;
1565 char c_mode; /* Mode of the balance. */
1566 int retval2 = -1;
1567 int quota_cut_bytes;
1568 loff_t tail_pos = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001570 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001572 init_tb_struct(th, &s_cut_balance, p_s_inode->i_sb, p_s_path,
1573 n_cut_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001575 /* Repeat this loop until we either cut the item without needing
1576 to balance, or we fix_nodes without schedule occurring */
1577 while (1) {
1578 /* Determine the balance mode, position of the first byte to
1579 be cut, and size to be cut. In case of the indirect item
1580 free unformatted nodes which are pointed to by the cut
1581 pointers. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001583 c_mode =
1584 prepare_for_delete_or_cut(th, p_s_inode, p_s_path,
1585 p_s_item_key, &n_removed,
1586 &n_cut_size, n_new_file_size);
1587 if (c_mode == M_CONVERT) {
1588 /* convert last unformatted node to direct item or leave
1589 tail in the unformatted node */
1590 RFALSE(n_ret_value != CARRY_ON,
1591 "PAP-5570: can not convert twice");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001593 n_ret_value =
1594 maybe_indirect_to_direct(th, p_s_inode, page,
1595 p_s_path, p_s_item_key,
1596 n_new_file_size, &c_mode);
1597 if (c_mode == M_SKIP_BALANCING)
1598 /* tail has been left in the unformatted node */
1599 return n_ret_value;
1600
1601 n_is_inode_locked = 1;
1602
1603 /* removing of last unformatted node will change value we
1604 have to return to truncate. Save it */
1605 retval2 = n_ret_value;
1606 /*retval2 = p_s_sb->s_blocksize - (n_new_file_size & (p_s_sb->s_blocksize - 1)); */
1607
1608 /* So, we have performed the first part of the conversion:
1609 inserting the new direct item. Now we are removing the
1610 last unformatted node pointer. Set key to search for
1611 it. */
1612 set_cpu_key_k_type(p_s_item_key, TYPE_INDIRECT);
1613 p_s_item_key->key_length = 4;
1614 n_new_file_size -=
1615 (n_new_file_size & (p_s_sb->s_blocksize - 1));
1616 tail_pos = n_new_file_size;
1617 set_cpu_key_k_offset(p_s_item_key, n_new_file_size + 1);
1618 if (search_for_position_by_key
1619 (p_s_sb, p_s_item_key,
1620 p_s_path) == POSITION_NOT_FOUND) {
1621 print_block(PATH_PLAST_BUFFER(p_s_path), 3,
1622 PATH_LAST_POSITION(p_s_path) - 1,
1623 PATH_LAST_POSITION(p_s_path) + 1);
1624 reiserfs_panic(p_s_sb,
1625 "PAP-5580: reiserfs_cut_from_item: item to convert does not exist (%K)",
1626 p_s_item_key);
1627 }
1628 continue;
1629 }
1630 if (n_cut_size == 0) {
1631 pathrelse(p_s_path);
1632 return 0;
1633 }
1634
1635 s_cut_balance.insert_size[0] = n_cut_size;
1636
1637 n_ret_value = fix_nodes(c_mode, &s_cut_balance, NULL, NULL);
1638 if (n_ret_value != REPEAT_SEARCH)
1639 break;
1640
1641 PROC_INFO_INC(p_s_sb, cut_from_item_restarted);
1642
1643 n_ret_value =
1644 search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path);
1645 if (n_ret_value == POSITION_FOUND)
1646 continue;
1647
1648 reiserfs_warning(p_s_sb,
1649 "PAP-5610: reiserfs_cut_from_item: item %K not found",
1650 p_s_item_key);
1651 unfix_nodes(&s_cut_balance);
1652 return (n_ret_value == IO_ERROR) ? -EIO : -ENOENT;
1653 } /* while */
1654
1655 // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
1656 if (n_ret_value != CARRY_ON) {
1657 if (n_is_inode_locked) {
1658 // FIXME: this seems to be not needed: we are always able
1659 // to cut item
1660 indirect_to_direct_roll_back(th, p_s_inode, p_s_path);
1661 }
1662 if (n_ret_value == NO_DISK_SPACE)
1663 reiserfs_warning(p_s_sb, "NO_DISK_SPACE");
1664 unfix_nodes(&s_cut_balance);
1665 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001668 /* go ahead and perform balancing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001670 RFALSE(c_mode == M_PASTE || c_mode == M_INSERT, "invalid mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001672 /* Calculate number of bytes that need to be cut from the item. */
1673 quota_cut_bytes =
1674 (c_mode ==
1675 M_DELETE) ? ih_item_len(get_ih(p_s_path)) : -s_cut_balance.
1676 insert_size[0];
1677 if (retval2 == -1)
1678 n_ret_value = calc_deleted_bytes_number(&s_cut_balance, c_mode);
1679 else
1680 n_ret_value = retval2;
1681
1682 /* For direct items, we only change the quota when deleting the last
1683 ** item.
1684 */
1685 p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
1686 if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(p_le_ih)) {
1687 if (c_mode == M_DELETE &&
1688 (le_ih_k_offset(p_le_ih) & (p_s_sb->s_blocksize - 1)) ==
1689 1) {
1690 // FIXME: this is to keep 3.5 happy
1691 REISERFS_I(p_s_inode)->i_first_direct_byte = U32_MAX;
1692 quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE;
1693 } else {
1694 quota_cut_bytes = 0;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001698 if (n_is_inode_locked) {
1699 struct item_head *le_ih =
1700 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1701 /* we are going to complete indirect2direct conversion. Make
1702 sure, that we exactly remove last unformatted node pointer
1703 of the item */
1704 if (!is_indirect_le_ih(le_ih))
1705 reiserfs_panic(p_s_sb,
1706 "vs-5652: reiserfs_cut_from_item: "
1707 "item must be indirect %h", le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001709 if (c_mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
1710 reiserfs_panic(p_s_sb,
1711 "vs-5653: reiserfs_cut_from_item: "
1712 "completing indirect2direct conversion indirect item %h "
1713 "being deleted must be of 4 byte long",
1714 le_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001716 if (c_mode == M_CUT
1717 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
1718 reiserfs_panic(p_s_sb,
1719 "vs-5654: reiserfs_cut_from_item: "
1720 "can not complete indirect2direct conversion of %h (CUT, insert_size==%d)",
1721 le_ih, s_cut_balance.insert_size[0]);
1722 }
1723 /* it would be useful to make sure, that right neighboring
1724 item is direct item of this file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001727
1728 do_balance(&s_cut_balance, NULL, NULL, c_mode);
1729 if (n_is_inode_locked) {
1730 /* we've done an indirect->direct conversion. when the data block
1731 ** was freed, it was removed from the list of blocks that must
1732 ** be flushed before the transaction commits, make sure to
1733 ** unmap and invalidate it
1734 */
1735 unmap_buffers(page, tail_pos);
1736 REISERFS_I(p_s_inode)->i_flags &= ~i_pack_on_close_mask;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001739 reiserfs_debug(p_s_inode->i_sb, REISERFS_DEBUG_CODE,
1740 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1741 quota_cut_bytes, p_s_inode->i_uid, '?');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001743 DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
1744 return n_ret_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001747static void truncate_directory(struct reiserfs_transaction_handle *th,
1748 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001750 BUG_ON(!th->t_trans_id);
1751 if (inode->i_nlink)
1752 reiserfs_warning(inode->i_sb,
1753 "vs-5655: truncate_directory: link count != 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001755 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
1756 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
1757 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1758 reiserfs_update_sd(th, inode);
1759 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
1760 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/* Truncate file to the new size. Note, this must be called with a transaction
1764 already started */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001765int reiserfs_do_truncate(struct reiserfs_transaction_handle *th, struct inode *p_s_inode, /* ->i_size contains new
1766 size */
1767 struct page *page, /* up to date for last block */
1768 int update_timestamps /* when it is called by
1769 file_release to convert
1770 the tail - no timestamps
1771 should be updated */
1772 )
1773{
1774 INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
1775 struct item_head *p_le_ih; /* Pointer to an item header. */
1776 struct cpu_key s_item_key; /* Key to search for a previous file item. */
1777 loff_t n_file_size, /* Old file size. */
1778 n_new_file_size; /* New file size. */
1779 int n_deleted; /* Number of deleted or truncated bytes. */
1780 int retval;
1781 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001783 BUG_ON(!th->t_trans_id);
1784 if (!
1785 (S_ISREG(p_s_inode->i_mode) || S_ISDIR(p_s_inode->i_mode)
1786 || S_ISLNK(p_s_inode->i_mode)))
1787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001789 if (S_ISDIR(p_s_inode->i_mode)) {
1790 // deletion of directory - no need to update timestamps
1791 truncate_directory(th, p_s_inode);
1792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001795 /* Get new file size. */
1796 n_new_file_size = p_s_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001798 // FIXME: note, that key type is unimportant here
1799 make_cpu_key(&s_item_key, p_s_inode, max_reiserfs_offset(p_s_inode),
1800 TYPE_DIRECT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001802 retval =
1803 search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
1804 &s_search_path);
1805 if (retval == IO_ERROR) {
1806 reiserfs_warning(p_s_inode->i_sb,
1807 "vs-5657: reiserfs_do_truncate: "
1808 "i/o failure occurred trying to truncate %K",
1809 &s_item_key);
1810 err = -EIO;
1811 goto out;
1812 }
1813 if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
1814 reiserfs_warning(p_s_inode->i_sb,
1815 "PAP-5660: reiserfs_do_truncate: "
1816 "wrong result %d of search for %K", retval,
1817 &s_item_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001819 err = -EIO;
1820 goto out;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001823 s_search_path.pos_in_item--;
1824
1825 /* Get real file size (total length of all file items) */
1826 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1827 if (is_statdata_le_ih(p_le_ih))
1828 n_file_size = 0;
1829 else {
1830 loff_t offset = le_ih_k_offset(p_le_ih);
1831 int bytes =
1832 op_bytes_number(p_le_ih, p_s_inode->i_sb->s_blocksize);
1833
1834 /* this may mismatch with real file size: if last direct item
1835 had no padding zeros and last unformatted node had no free
1836 space, this file would have this file size */
1837 n_file_size = offset + bytes - 1;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001840 * are we doing a full truncate or delete, if so
1841 * kick in the reada code
1842 */
1843 if (n_new_file_size == 0)
1844 s_search_path.reada = PATH_READA | PATH_READA_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001846 if (n_file_size == 0 || n_file_size < n_new_file_size) {
1847 goto update_and_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001850 /* Update key to search for the last file item. */
1851 set_cpu_key_k_offset(&s_item_key, n_file_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001853 do {
1854 /* Cut or delete file item. */
1855 n_deleted =
1856 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1857 p_s_inode, page, n_new_file_size);
1858 if (n_deleted < 0) {
1859 reiserfs_warning(p_s_inode->i_sb,
1860 "vs-5665: reiserfs_do_truncate: reiserfs_cut_from_item failed");
1861 reiserfs_check_path(&s_search_path);
1862 return 0;
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001865 RFALSE(n_deleted > n_file_size,
1866 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1867 n_deleted, n_file_size, &s_item_key);
1868
1869 /* Change key to search the last file item. */
1870 n_file_size -= n_deleted;
1871
1872 set_cpu_key_k_offset(&s_item_key, n_file_size);
1873
1874 /* While there are bytes to truncate and previous file item is presented in the tree. */
1875
1876 /*
1877 ** This loop could take a really long time, and could log
1878 ** many more blocks than a transaction can hold. So, we do a polite
1879 ** journal end here, and if the transaction needs ending, we make
1880 ** sure the file is consistent before ending the current trans
1881 ** and starting a new one
1882 */
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001883 if (journal_transaction_should_end(th, 0) ||
1884 reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001885 int orig_len_alloc = th->t_blocks_allocated;
1886 decrement_counters_in_path(&s_search_path);
1887
1888 if (update_timestamps) {
1889 p_s_inode->i_mtime = p_s_inode->i_ctime =
1890 CURRENT_TIME_SEC;
1891 }
1892 reiserfs_update_sd(th, p_s_inode);
1893
1894 err = journal_end(th, p_s_inode->i_sb, orig_len_alloc);
1895 if (err)
1896 goto out;
1897 err = journal_begin(th, p_s_inode->i_sb,
Alexander Zarochentzev23f9e0f2006-03-25 03:06:57 -08001898 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001899 if (err)
1900 goto out;
1901 reiserfs_update_inode_transaction(p_s_inode);
1902 }
1903 } while (n_file_size > ROUND_UP(n_new_file_size) &&
1904 search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
1905 &s_search_path) == POSITION_FOUND);
1906
1907 RFALSE(n_file_size > ROUND_UP(n_new_file_size),
1908 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1909 n_new_file_size, n_file_size, s_item_key.on_disk_key.k_objectid);
1910
1911 update_and_out:
1912 if (update_timestamps) {
1913 // this is truncate, not file closing
1914 p_s_inode->i_mtime = p_s_inode->i_ctime = CURRENT_TIME_SEC;
1915 }
1916 reiserfs_update_sd(th, p_s_inode);
1917
1918 out:
1919 pathrelse(&s_search_path);
1920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923#ifdef CONFIG_REISERFS_CHECK
1924// this makes sure, that we __append__, not overwrite or add holes
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001925static void check_research_for_paste(struct path *path,
1926 const struct cpu_key *p_s_key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001928 struct item_head *found_ih = get_ih(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001930 if (is_direct_le_ih(found_ih)) {
1931 if (le_ih_k_offset(found_ih) +
1932 op_bytes_number(found_ih,
1933 get_last_bh(path)->b_size) !=
1934 cpu_key_k_offset(p_s_key)
1935 || op_bytes_number(found_ih,
1936 get_last_bh(path)->b_size) !=
1937 pos_in_item(path))
1938 reiserfs_panic(NULL,
1939 "PAP-5720: check_research_for_paste: "
1940 "found direct item %h or position (%d) does not match to key %K",
1941 found_ih, pos_in_item(path), p_s_key);
1942 }
1943 if (is_indirect_le_ih(found_ih)) {
1944 if (le_ih_k_offset(found_ih) +
1945 op_bytes_number(found_ih,
1946 get_last_bh(path)->b_size) !=
1947 cpu_key_k_offset(p_s_key)
1948 || I_UNFM_NUM(found_ih) != pos_in_item(path)
1949 || get_ih_free_space(found_ih) != 0)
1950 reiserfs_panic(NULL,
1951 "PAP-5730: check_research_for_paste: "
1952 "found indirect item (%h) or position (%d) does not match to key (%K)",
1953 found_ih, pos_in_item(path), p_s_key);
1954 }
1955}
1956#endif /* config reiserfs check */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958/* Paste bytes to the existing item. Returns bytes number pasted into the item. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001959int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct path *p_s_search_path, /* Path to the pasted item. */
1960 const struct cpu_key *p_s_key, /* Key to search for the needed item. */
1961 struct inode *inode, /* Inode item belongs to */
1962 const char *p_c_body, /* Pointer to the bytes to paste. */
1963 int n_pasted_size)
1964{ /* Size of pasted bytes. */
1965 struct tree_balance s_paste_balance;
1966 int retval;
1967 int fs_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001969 BUG_ON(!th->t_trans_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001971 fs_gen = get_generation(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001974 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1975 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
1976 n_pasted_size, inode->i_uid,
1977 key2type(&(p_s_key->on_disk_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978#endif
1979
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001980 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, n_pasted_size)) {
1981 pathrelse(p_s_search_path);
1982 return -EDQUOT;
1983 }
1984 init_tb_struct(th, &s_paste_balance, th->t_super, p_s_search_path,
1985 n_pasted_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001987 s_paste_balance.key = p_s_key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988#endif
1989
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001990 /* DQUOT_* can schedule, must check before the fix_nodes */
1991 if (fs_changed(fs_gen, inode->i_sb)) {
1992 goto search_again;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07001995 while ((retval =
1996 fix_nodes(M_PASTE, &s_paste_balance, NULL,
1997 p_c_body)) == REPEAT_SEARCH) {
1998 search_again:
1999 /* file system changed while we were in the fix_nodes */
2000 PROC_INFO_INC(th->t_super, paste_into_item_restarted);
2001 retval =
2002 search_for_position_by_key(th->t_super, p_s_key,
2003 p_s_search_path);
2004 if (retval == IO_ERROR) {
2005 retval = -EIO;
2006 goto error_out;
2007 }
2008 if (retval == POSITION_FOUND) {
2009 reiserfs_warning(inode->i_sb,
2010 "PAP-5710: reiserfs_paste_into_item: entry or pasted byte (%K) exists",
2011 p_s_key);
2012 retval = -EEXIST;
2013 goto error_out;
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002016 check_research_for_paste(p_s_search_path, p_s_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002020 /* Perform balancing after all resources are collected by fix_nodes, and
2021 accessing them will not risk triggering schedule. */
2022 if (retval == CARRY_ON) {
2023 do_balance(&s_paste_balance, NULL /*ih */ , p_c_body, M_PASTE);
2024 return 0;
2025 }
2026 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2027 error_out:
2028 /* this also releases the path */
2029 unfix_nodes(&s_paste_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002031 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2032 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
2033 n_pasted_size, inode->i_uid,
2034 key2type(&(p_s_key->on_disk_key)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002036 DQUOT_FREE_SPACE_NODIRTY(inode, n_pasted_size);
2037 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040/* Insert new item into the buffer at the path. */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002041int reiserfs_insert_item(struct reiserfs_transaction_handle *th, struct path *p_s_path, /* Path to the inserteded item. */
2042 const struct cpu_key *key, struct item_head *p_s_ih, /* Pointer to the item header to insert. */
2043 struct inode *inode, const char *p_c_body)
2044{ /* Pointer to the bytes to insert. */
2045 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);
2054 quota_bytes = ih_item_len(p_s_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 */
2059 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_s_ih)) {
2060 quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
2061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002063 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2064 "reiserquota insert_item(): allocating %u id=%u type=%c",
2065 quota_bytes, inode->i_uid, head2type(p_s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002067 /* We can't dirty inode here. It would be immediately written but
2068 * appropriate stat item isn't inserted yet... */
2069 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, quota_bytes)) {
2070 pathrelse(p_s_path);
2071 return -EDQUOT;
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002074 init_tb_struct(th, &s_ins_balance, th->t_super, p_s_path,
2075 IH_SIZE + ih_item_len(p_s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076#ifdef DISPLACE_NEW_PACKING_LOCALITIES
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002077 s_ins_balance.key = key->on_disk_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002079 /* DQUOT_* can schedule, must check to be sure calling fix_nodes is safe */
2080 if (inode && fs_changed(fs_gen, inode->i_sb)) {
2081 goto search_again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002083
2084 while ((retval =
2085 fix_nodes(M_INSERT, &s_ins_balance, p_s_ih,
2086 p_c_body)) == REPEAT_SEARCH) {
2087 search_again:
2088 /* file system changed while we were in the fix_nodes */
2089 PROC_INFO_INC(th->t_super, insert_item_restarted);
2090 retval = search_item(th->t_super, key, p_s_path);
2091 if (retval == IO_ERROR) {
2092 retval = -EIO;
2093 goto error_out;
2094 }
2095 if (retval == ITEM_FOUND) {
2096 reiserfs_warning(th->t_super,
2097 "PAP-5760: reiserfs_insert_item: "
2098 "key %K already exists in the tree",
2099 key);
2100 retval = -EEXIST;
2101 goto error_out;
2102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002105 /* make balancing after all resources will be collected at a time */
2106 if (retval == CARRY_ON) {
2107 do_balance(&s_ins_balance, p_s_ih, p_c_body, M_INSERT);
2108 return 0;
2109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002111 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2112 error_out:
2113 /* also releases the path */
2114 unfix_nodes(&s_ins_balance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#ifdef REISERQUOTA_DEBUG
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002116 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
2117 "reiserquota insert_item(): freeing %u id=%u type=%c",
2118 quota_bytes, inode->i_uid, head2type(p_s_ih));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#endif
Linus Torvaldsbd4c6252005-07-12 20:21:28 -07002120 if (inode)
2121 DQUOT_FREE_SPACE_NODIRTY(inode, quota_bytes);
2122 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}