stree.c (d68caa9530a8ba54f97002e02bf6a0ad2462b8c0) stree.c (ee93961be1faddf9e9a638bc519145c20f0cfeba)
1/*
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

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

131
132 return 0;
133}
134
135inline int comp_short_le_keys(const struct reiserfs_key *key1,
136 const struct reiserfs_key *key2)
137{
138 __u32 *k1_u32, *k2_u32;
1/*
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

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

131
132 return 0;
133}
134
135inline int comp_short_le_keys(const struct reiserfs_key *key1,
136 const struct reiserfs_key *key2)
137{
138 __u32 *k1_u32, *k2_u32;
139 int n_key_length = REISERFS_SHORT_KEY_LEN;
139 int key_length = REISERFS_SHORT_KEY_LEN;
140
141 k1_u32 = (__u32 *) key1;
142 k2_u32 = (__u32 *) key2;
140
141 k1_u32 = (__u32 *) key1;
142 k2_u32 = (__u32 *) key2;
143 for (; n_key_length--; ++k1_u32, ++k2_u32) {
143 for (; key_length--; ++k1_u32, ++k2_u32) {
144 if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
145 return -1;
146 if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
147 return 1;
148 }
149 return 0;
150}
151

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

172
173/**************************************************************************
174 * Binary search toolkit function *
175 * Search for an item in the array by the item key *
176 * Returns: 1 if found, 0 if not found; *
177 * *pos = number of the searched element if found, else the *
178 * number of the first element that is larger than key. *
179 **************************************************************************/
144 if (le32_to_cpu(*k1_u32) < le32_to_cpu(*k2_u32))
145 return -1;
146 if (le32_to_cpu(*k1_u32) > le32_to_cpu(*k2_u32))
147 return 1;
148 }
149 return 0;
150}
151

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

172
173/**************************************************************************
174 * Binary search toolkit function *
175 * Search for an item in the array by the item key *
176 * Returns: 1 if found, 0 if not found; *
177 * *pos = number of the searched element if found, else the *
178 * number of the first element that is larger than key. *
179 **************************************************************************/
180/* For those not familiar with binary search: n_lbound is the leftmost item that it
181 could be, n_rbound the rightmost item that it could be. We examine the item
182 halfway between n_lbound and n_rbound, and that tells us either that we can increase
183 n_lbound, or decrease n_rbound, or that we have found it, or if n_lbound <= n_rbound that
180/* For those not familiar with binary search: lbound is the leftmost item that it
181 could be, rbound the rightmost item that it could be. We examine the item
182 halfway between lbound and rbound, and that tells us either that we can increase
183 lbound, or decrease rbound, or that we have found it, or if lbound <= rbound that
184 there are no possible items, and we have not found it. With each examination we
185 cut the number of possible items it could be by one more than half rounded down,
186 or we find it. */
187static inline int bin_search(const void *key, /* Key to search for. */
188 const void *base, /* First item in the array. */
189 int num, /* Number of items in the array. */
190 int width, /* Item size in the array.
191 searched. Lest the reader be
192 confused, note that this is crafted
193 as a general function, and when it
194 is applied specifically to the array
195 of item headers in a node, width
196 is actually the item header size not
197 the item size. */
198 int *pos /* Number of the searched for element. */
199 )
200{
184 there are no possible items, and we have not found it. With each examination we
185 cut the number of possible items it could be by one more than half rounded down,
186 or we find it. */
187static inline int bin_search(const void *key, /* Key to search for. */
188 const void *base, /* First item in the array. */
189 int num, /* Number of items in the array. */
190 int width, /* Item size in the array.
191 searched. Lest the reader be
192 confused, note that this is crafted
193 as a general function, and when it
194 is applied specifically to the array
195 of item headers in a node, width
196 is actually the item header size not
197 the item size. */
198 int *pos /* Number of the searched for element. */
199 )
200{
201 int n_rbound, n_lbound, n_j;
201 int rbound, lbound, j;
202
202
203 for (n_j = ((n_rbound = num - 1) + (n_lbound = 0)) / 2;
204 n_lbound <= n_rbound; n_j = (n_rbound + n_lbound) / 2)
203 for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
204 lbound <= rbound; j = (rbound + lbound) / 2)
205 switch (comp_keys
205 switch (comp_keys
206 ((struct reiserfs_key *)((char *)base +
207 n_j * width),
206 ((struct reiserfs_key *)((char *)base + j * width),
208 (struct cpu_key *)key)) {
209 case -1:
207 (struct cpu_key *)key)) {
208 case -1:
210 n_lbound = n_j + 1;
209 lbound = j + 1;
211 continue;
212 case 1:
210 continue;
211 case 1:
213 n_rbound = n_j - 1;
212 rbound = j - 1;
214 continue;
215 case 0:
213 continue;
214 case 0:
216 *pos = n_j;
215 *pos = j;
217 return ITEM_FOUND; /* Key found in the array. */
218 }
219
220 /* bin_search did not find given key, it returns position of key,
221 that is minimal and greater than the given one. */
216 return ITEM_FOUND; /* Key found in the array. */
217 }
218
219 /* bin_search did not find given key, it returns position of key,
220 that is minimal and greater than the given one. */
222 *pos = n_lbound;
221 *pos = lbound;
223 return ITEM_NOT_FOUND;
224}
225
226#ifdef CONFIG_REISERFS_CHECK
227extern struct tree_balance *cur_tb;
228#endif
229
230/* Minimal possible key. It is never in the tree. */

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

237 {{__constant_cpu_to_le32(0xffffffff),
238 __constant_cpu_to_le32(0xffffffff)},}
239};
240
241/* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom
242 of the path, and going upwards. We must check the path's validity at each step. If the key is not in
243 the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this
244 case we return a special key, either MIN_KEY or MAX_KEY. */
222 return ITEM_NOT_FOUND;
223}
224
225#ifdef CONFIG_REISERFS_CHECK
226extern struct tree_balance *cur_tb;
227#endif
228
229/* Minimal possible key. It is never in the tree. */

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

236 {{__constant_cpu_to_le32(0xffffffff),
237 __constant_cpu_to_le32(0xffffffff)},}
238};
239
240/* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom
241 of the path, and going upwards. We must check the path's validity at each step. If the key is not in
242 the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this
243 case we return a special key, either MIN_KEY or MAX_KEY. */
245static inline const struct reiserfs_key *get_lkey(const struct treepath
246 *chk_path,
247 const struct super_block
248 *sb)
244static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
245 const struct super_block *sb)
249{
246{
250 int n_position, n_path_offset = chk_path->path_length;
247 int position, path_offset = chk_path->path_length;
251 struct buffer_head *parent;
252
248 struct buffer_head *parent;
249
253 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
250 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
254 "PAP-5010: invalid offset in the path");
255
256 /* While not higher in path than first element. */
251 "PAP-5010: invalid offset in the path");
252
253 /* While not higher in path than first element. */
257 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
254 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
258
259 RFALSE(!buffer_uptodate
255
256 RFALSE(!buffer_uptodate
260 (PATH_OFFSET_PBUFFER(chk_path, n_path_offset)),
257 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
261 "PAP-5020: parent is not uptodate");
262
263 /* Parent at the path is not in the tree now. */
264 if (!B_IS_IN_TREE
265 (parent =
258 "PAP-5020: parent is not uptodate");
259
260 /* Parent at the path is not in the tree now. */
261 if (!B_IS_IN_TREE
262 (parent =
266 PATH_OFFSET_PBUFFER(chk_path, n_path_offset)))
263 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
267 return &MAX_KEY;
268 /* Check whether position in the parent is correct. */
264 return &MAX_KEY;
265 /* Check whether position in the parent is correct. */
269 if ((n_position =
266 if ((position =
270 PATH_OFFSET_POSITION(chk_path,
267 PATH_OFFSET_POSITION(chk_path,
271 n_path_offset)) >
268 path_offset)) >
272 B_NR_ITEMS(parent))
273 return &MAX_KEY;
274 /* Check whether parent at the path really points to the child. */
269 B_NR_ITEMS(parent))
270 return &MAX_KEY;
271 /* Check whether parent at the path really points to the child. */
275 if (B_N_CHILD_NUM(parent, n_position) !=
272 if (B_N_CHILD_NUM(parent, position) !=
276 PATH_OFFSET_PBUFFER(chk_path,
273 PATH_OFFSET_PBUFFER(chk_path,
277 n_path_offset + 1)->b_blocknr)
274 path_offset + 1)->b_blocknr)
278 return &MAX_KEY;
279 /* Return delimiting key if position in the parent is not equal to zero. */
275 return &MAX_KEY;
276 /* Return delimiting key if position in the parent is not equal to zero. */
280 if (n_position)
281 return B_N_PDELIM_KEY(parent, n_position - 1);
277 if (position)
278 return B_N_PDELIM_KEY(parent, position - 1);
282 }
283 /* Return MIN_KEY if we are in the root of the buffer tree. */
284 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
285 b_blocknr == SB_ROOT_BLOCK(sb))
286 return &MIN_KEY;
287 return &MAX_KEY;
288}
289
290/* Get delimiting key of the buffer at the path and its right neighbor. */
291inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
292 const struct super_block *sb)
293{
279 }
280 /* Return MIN_KEY if we are in the root of the buffer tree. */
281 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
282 b_blocknr == SB_ROOT_BLOCK(sb))
283 return &MIN_KEY;
284 return &MAX_KEY;
285}
286
287/* Get delimiting key of the buffer at the path and its right neighbor. */
288inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
289 const struct super_block *sb)
290{
294 int n_position, n_path_offset = chk_path->path_length;
291 int position, path_offset = chk_path->path_length;
295 struct buffer_head *parent;
296
292 struct buffer_head *parent;
293
297 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
294 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
298 "PAP-5030: invalid offset in the path");
299
295 "PAP-5030: invalid offset in the path");
296
300 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
297 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
301
302 RFALSE(!buffer_uptodate
298
299 RFALSE(!buffer_uptodate
303 (PATH_OFFSET_PBUFFER(chk_path, n_path_offset)),
300 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
304 "PAP-5040: parent is not uptodate");
305
306 /* Parent at the path is not in the tree now. */
307 if (!B_IS_IN_TREE
308 (parent =
301 "PAP-5040: parent is not uptodate");
302
303 /* Parent at the path is not in the tree now. */
304 if (!B_IS_IN_TREE
305 (parent =
309 PATH_OFFSET_PBUFFER(chk_path, n_path_offset)))
306 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
310 return &MIN_KEY;
311 /* Check whether position in the parent is correct. */
307 return &MIN_KEY;
308 /* Check whether position in the parent is correct. */
312 if ((n_position =
309 if ((position =
313 PATH_OFFSET_POSITION(chk_path,
310 PATH_OFFSET_POSITION(chk_path,
314 n_path_offset)) >
311 path_offset)) >
315 B_NR_ITEMS(parent))
316 return &MIN_KEY;
317 /* Check whether parent at the path really points to the child. */
312 B_NR_ITEMS(parent))
313 return &MIN_KEY;
314 /* Check whether parent at the path really points to the child. */
318 if (B_N_CHILD_NUM(parent, n_position) !=
315 if (B_N_CHILD_NUM(parent, position) !=
319 PATH_OFFSET_PBUFFER(chk_path,
316 PATH_OFFSET_PBUFFER(chk_path,
320 n_path_offset + 1)->b_blocknr)
317 path_offset + 1)->b_blocknr)
321 return &MIN_KEY;
322 /* Return delimiting key if position in the parent is not the last one. */
318 return &MIN_KEY;
319 /* Return delimiting key if position in the parent is not the last one. */
323 if (n_position != B_NR_ITEMS(parent))
324 return B_N_PDELIM_KEY(parent, n_position);
320 if (position != B_NR_ITEMS(parent))
321 return B_N_PDELIM_KEY(parent, position);
325 }
326 /* Return MAX_KEY if we are in the root of the buffer tree. */
327 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
328 b_blocknr == SB_ROOT_BLOCK(sb))
329 return &MAX_KEY;
330 return &MIN_KEY;
331}
332

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

366}
367
368/* Drop the reference to each buffer in a path and restore
369 * dirty bits clean when preparing the buffer for the log.
370 * This version should only be called from fix_nodes() */
371void pathrelse_and_restore(struct super_block *sb,
372 struct treepath *search_path)
373{
322 }
323 /* Return MAX_KEY if we are in the root of the buffer tree. */
324 if (PATH_OFFSET_PBUFFER(chk_path, FIRST_PATH_ELEMENT_OFFSET)->
325 b_blocknr == SB_ROOT_BLOCK(sb))
326 return &MAX_KEY;
327 return &MIN_KEY;
328}
329

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

363}
364
365/* Drop the reference to each buffer in a path and restore
366 * dirty bits clean when preparing the buffer for the log.
367 * This version should only be called from fix_nodes() */
368void pathrelse_and_restore(struct super_block *sb,
369 struct treepath *search_path)
370{
374 int n_path_offset = search_path->path_length;
371 int path_offset = search_path->path_length;
375
372
376 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
373 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
377 "clm-4000: invalid path offset");
378
374 "clm-4000: invalid path offset");
375
379 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
376 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
380 struct buffer_head *bh;
377 struct buffer_head *bh;
381 bh = PATH_OFFSET_PBUFFER(search_path, n_path_offset--);
378 bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
382 reiserfs_restore_prepared_buffer(sb, bh);
383 brelse(bh);
384 }
385 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
386}
387
388/* Drop the reference to each buffer in a path */
389void pathrelse(struct treepath *search_path)
390{
379 reiserfs_restore_prepared_buffer(sb, bh);
380 brelse(bh);
381 }
382 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
383}
384
385/* Drop the reference to each buffer in a path */
386void pathrelse(struct treepath *search_path)
387{
391 int n_path_offset = search_path->path_length;
388 int path_offset = search_path->path_length;
392
389
393 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
390 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
394 "PAP-5090: invalid path offset");
395
391 "PAP-5090: invalid path offset");
392
396 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
397 brelse(PATH_OFFSET_PBUFFER(search_path, n_path_offset--));
393 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
394 brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
398
399 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
400}
401
402static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
403{
404 struct block_head *blkh;
405 struct item_head *ih;

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

567 correctness of the bottom of the path */
568/* The function is NOT SCHEDULE-SAFE! */
569int search_by_key(struct super_block *sb, const struct cpu_key *key, /* Key to search. */
570 struct treepath *search_path,/* This structure was
571 allocated and initialized
572 by the calling
573 function. It is filled up
574 by this function. */
395
396 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
397}
398
399static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
400{
401 struct block_head *blkh;
402 struct item_head *ih;

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

564 correctness of the bottom of the path */
565/* The function is NOT SCHEDULE-SAFE! */
566int search_by_key(struct super_block *sb, const struct cpu_key *key, /* Key to search. */
567 struct treepath *search_path,/* This structure was
568 allocated and initialized
569 by the calling
570 function. It is filled up
571 by this function. */
575 int n_stop_level /* How far down the tree to search. To
572 int stop_level /* How far down the tree to search. To
576 stop at leaf level - set to
577 DISK_LEAF_NODE_LEVEL */
578 )
579{
573 stop at leaf level - set to
574 DISK_LEAF_NODE_LEVEL */
575 )
576{
580 b_blocknr_t n_block_number;
577 b_blocknr_t block_number;
581 int expected_level;
582 struct buffer_head *bh;
583 struct path_element *last_element;
578 int expected_level;
579 struct buffer_head *bh;
580 struct path_element *last_element;
584 int n_node_level, n_retval;
581 int node_level, retval;
585 int right_neighbor_of_leaf_node;
586 int fs_gen;
587 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
588 b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
589 int reada_count = 0;
590
591#ifdef CONFIG_REISERFS_CHECK
582 int right_neighbor_of_leaf_node;
583 int fs_gen;
584 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
585 b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
586 int reada_count = 0;
587
588#ifdef CONFIG_REISERFS_CHECK
592 int n_repeat_counter = 0;
589 int repeat_counter = 0;
593#endif
594
595 PROC_INFO_INC(sb, search_by_key);
596
597 /* As we add each node to a path we increase its count. This means that
598 we must be careful to release all nodes in a path before we either
599 discard the path struct or re-use the path struct, as we do here. */
600
601 pathrelse(search_path);
602
603 right_neighbor_of_leaf_node = 0;
604
605 /* With each iteration of this loop we search through the items in the
606 current node, and calculate the next current node(next path element)
607 for the next iteration of this loop.. */
590#endif
591
592 PROC_INFO_INC(sb, search_by_key);
593
594 /* As we add each node to a path we increase its count. This means that
595 we must be careful to release all nodes in a path before we either
596 discard the path struct or re-use the path struct, as we do here. */
597
598 pathrelse(search_path);
599
600 right_neighbor_of_leaf_node = 0;
601
602 /* With each iteration of this loop we search through the items in the
603 current node, and calculate the next current node(next path element)
604 for the next iteration of this loop.. */
608 n_block_number = SB_ROOT_BLOCK(sb);
605 block_number = SB_ROOT_BLOCK(sb);
609 expected_level = -1;
610 while (1) {
611
612#ifdef CONFIG_REISERFS_CHECK
606 expected_level = -1;
607 while (1) {
608
609#ifdef CONFIG_REISERFS_CHECK
613 if (!(++n_repeat_counter % 50000))
610 if (!(++repeat_counter % 50000))
614 reiserfs_warning(sb, "PAP-5100",
615 "%s: there were %d iterations of "
616 "while loop looking for key %K",
611 reiserfs_warning(sb, "PAP-5100",
612 "%s: there were %d iterations of "
613 "while loop looking for key %K",
617 current->comm, n_repeat_counter,
614 current->comm, repeat_counter,
618 key);
619#endif
620
621 /* prep path to have another element added to it. */
622 last_element =
623 PATH_OFFSET_PELEMENT(search_path,
624 ++search_path->path_length);
625 fs_gen = get_generation(sb);
626
627 /* Read the next tree node, and set the last element in the path to
628 have a pointer to it. */
629 if ((bh = last_element->pe_buffer =
615 key);
616#endif
617
618 /* prep path to have another element added to it. */
619 last_element =
620 PATH_OFFSET_PELEMENT(search_path,
621 ++search_path->path_length);
622 fs_gen = get_generation(sb);
623
624 /* Read the next tree node, and set the last element in the path to
625 have a pointer to it. */
626 if ((bh = last_element->pe_buffer =
630 sb_getblk(sb, n_block_number))) {
627 sb_getblk(sb, block_number))) {
631 if (!buffer_uptodate(bh) && reada_count > 1)
632 search_by_key_reada(sb, reada_bh,
633 reada_blocks, reada_count);
634 ll_rw_block(READ, 1, &bh);
635 wait_on_buffer(bh);
636 if (!buffer_uptodate(bh))
637 goto io_error;
638 } else {

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

656 PROC_INFO_INC(sb, search_by_key_fs_changed);
657 PROC_INFO_INC(sb, search_by_key_restarted);
658 PROC_INFO_INC(sb,
659 sbk_restarted[expected_level - 1]);
660 pathrelse(search_path);
661
662 /* Get the root block number so that we can repeat the search
663 starting from the root. */
628 if (!buffer_uptodate(bh) && reada_count > 1)
629 search_by_key_reada(sb, reada_bh,
630 reada_blocks, reada_count);
631 ll_rw_block(READ, 1, &bh);
632 wait_on_buffer(bh);
633 if (!buffer_uptodate(bh))
634 goto io_error;
635 } else {

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

653 PROC_INFO_INC(sb, search_by_key_fs_changed);
654 PROC_INFO_INC(sb, search_by_key_restarted);
655 PROC_INFO_INC(sb,
656 sbk_restarted[expected_level - 1]);
657 pathrelse(search_path);
658
659 /* Get the root block number so that we can repeat the search
660 starting from the root. */
664 n_block_number = SB_ROOT_BLOCK(sb);
661 block_number = SB_ROOT_BLOCK(sb);
665 expected_level = -1;
666 right_neighbor_of_leaf_node = 0;
667
668 /* repeat search from the root */
669 continue;
670 }
671
672 /* only check that the key is in the buffer if key is not

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

689 reiserfs_error(sb, "vs-5150",
690 "invalid format found in block %ld. "
691 "Fsck?", bh->b_blocknr);
692 pathrelse(search_path);
693 return IO_ERROR;
694 }
695
696 /* ok, we have acquired next formatted node in the tree */
662 expected_level = -1;
663 right_neighbor_of_leaf_node = 0;
664
665 /* repeat search from the root */
666 continue;
667 }
668
669 /* only check that the key is in the buffer if key is not

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

686 reiserfs_error(sb, "vs-5150",
687 "invalid format found in block %ld. "
688 "Fsck?", bh->b_blocknr);
689 pathrelse(search_path);
690 return IO_ERROR;
691 }
692
693 /* ok, we have acquired next formatted node in the tree */
697 n_node_level = B_LEVEL(bh);
694 node_level = B_LEVEL(bh);
698
695
699 PROC_INFO_BH_STAT(sb, bh, n_node_level - 1);
696 PROC_INFO_BH_STAT(sb, bh, node_level - 1);
700
697
701 RFALSE(n_node_level < n_stop_level,
698 RFALSE(node_level < stop_level,
702 "vs-5152: tree level (%d) is less than stop level (%d)",
699 "vs-5152: tree level (%d) is less than stop level (%d)",
703 n_node_level, n_stop_level);
700 node_level, stop_level);
704
701
705 n_retval = bin_search(key, B_N_PITEM_HEAD(bh, 0),
702 retval = bin_search(key, B_N_PITEM_HEAD(bh, 0),
706 B_NR_ITEMS(bh),
703 B_NR_ITEMS(bh),
707 (n_node_level ==
704 (node_level ==
708 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
709 KEY_SIZE,
710 &(last_element->pe_position));
705 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
706 KEY_SIZE,
707 &(last_element->pe_position));
711 if (n_node_level == n_stop_level) {
712 return n_retval;
708 if (node_level == stop_level) {
709 return retval;
713 }
714
715 /* we are not in the stop level */
710 }
711
712 /* we are not in the stop level */
716 if (n_retval == ITEM_FOUND)
713 if (retval == ITEM_FOUND)
717 /* item has been found, so we choose the pointer which is to the right of the found one */
718 last_element->pe_position++;
719
720 /* if item was not found we choose the position which is to
721 the left of the found item. This requires no code,
722 bin_search did it already. */
723
724 /* So we have chosen a position in the current node which is
725 an internal node. Now we calculate child block number by
726 position in the node. */
714 /* item has been found, so we choose the pointer which is to the right of the found one */
715 last_element->pe_position++;
716
717 /* if item was not found we choose the position which is to
718 the left of the found item. This requires no code,
719 bin_search did it already. */
720
721 /* So we have chosen a position in the current node which is
722 an internal node. Now we calculate child block number by
723 position in the node. */
727 n_block_number =
724 block_number =
728 B_N_CHILD_NUM(bh, last_element->pe_position);
729
730 /* if we are going to read leaf nodes, try for read ahead as well */
731 if ((search_path->reada & PATH_READA) &&
725 B_N_CHILD_NUM(bh, last_element->pe_position);
726
727 /* if we are going to read leaf nodes, try for read ahead as well */
728 if ((search_path->reada & PATH_READA) &&
732 n_node_level == DISK_LEAF_NODE_LEVEL + 1) {
729 node_level == DISK_LEAF_NODE_LEVEL + 1) {
733 int pos = last_element->pe_position;
734 int limit = B_NR_ITEMS(bh);
735 struct reiserfs_key *le_key;
736
737 if (search_path->reada & PATH_READA_BACK)
738 limit = 0;
739 while (reada_count < SEARCH_BY_KEY_READA) {
740 if (pos == limit)

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

776
777/* The function is NOT SCHEDULE-SAFE! */
778int search_for_position_by_key(struct super_block *sb, /* Pointer to the super block. */
779 const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
780 struct treepath *search_path /* Filled up by this function. */
781 )
782{
783 struct item_head *p_le_ih; /* pointer to on-disk structure */
730 int pos = last_element->pe_position;
731 int limit = B_NR_ITEMS(bh);
732 struct reiserfs_key *le_key;
733
734 if (search_path->reada & PATH_READA_BACK)
735 limit = 0;
736 while (reada_count < SEARCH_BY_KEY_READA) {
737 if (pos == limit)

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

773
774/* The function is NOT SCHEDULE-SAFE! */
775int search_for_position_by_key(struct super_block *sb, /* Pointer to the super block. */
776 const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */
777 struct treepath *search_path /* Filled up by this function. */
778 )
779{
780 struct item_head *p_le_ih; /* pointer to on-disk structure */
784 int n_blk_size;
781 int blk_size;
785 loff_t item_offset, offset;
786 struct reiserfs_dir_entry de;
787 int retval;
788
789 /* If searching for directory entry. */
790 if (is_direntry_cpu_key(p_cpu_key))
791 return search_by_entry_key(sb, p_cpu_key, search_path,
792 &de);

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

811
812 RFALSE(!PATH_LAST_POSITION(search_path),
813 "PAP-5170: position equals zero");
814
815 /* Item is not found. Set path to the previous item. */
816 p_le_ih =
817 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(search_path),
818 --PATH_LAST_POSITION(search_path));
782 loff_t item_offset, offset;
783 struct reiserfs_dir_entry de;
784 int retval;
785
786 /* If searching for directory entry. */
787 if (is_direntry_cpu_key(p_cpu_key))
788 return search_by_entry_key(sb, p_cpu_key, search_path,
789 &de);

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

808
809 RFALSE(!PATH_LAST_POSITION(search_path),
810 "PAP-5170: position equals zero");
811
812 /* Item is not found. Set path to the previous item. */
813 p_le_ih =
814 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(search_path),
815 --PATH_LAST_POSITION(search_path));
819 n_blk_size = sb->s_blocksize;
816 blk_size = sb->s_blocksize;
820
821 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
822 return FILE_NOT_FOUND;
823 }
824 // FIXME: quite ugly this far
825
826 item_offset = le_ih_k_offset(p_le_ih);
827 offset = cpu_key_k_offset(p_cpu_key);
828
829 /* Needed byte is contained in the item pointed to by the path. */
830 if (item_offset <= offset &&
817
818 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
819 return FILE_NOT_FOUND;
820 }
821 // FIXME: quite ugly this far
822
823 item_offset = le_ih_k_offset(p_le_ih);
824 offset = cpu_key_k_offset(p_cpu_key);
825
826 /* Needed byte is contained in the item pointed to by the path. */
827 if (item_offset <= offset &&
831 item_offset + op_bytes_number(p_le_ih, n_blk_size) > offset) {
828 item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
832 pos_in_item(search_path) = offset - item_offset;
833 if (is_indirect_le_ih(p_le_ih)) {
829 pos_in_item(search_path) = offset - item_offset;
830 if (is_indirect_le_ih(p_le_ih)) {
834 pos_in_item(search_path) /= n_blk_size;
831 pos_in_item(search_path) /= blk_size;
835 }
836 return POSITION_FOUND;
837 }
838
839 /* Needed byte is not contained in the item pointed to by the
840 path. Set pos_in_item out of the item. */
841 if (is_indirect_le_ih(p_le_ih))
842 pos_in_item(search_path) =

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

886 /* item has to be deleted */
887 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
888 return M_DELETE;
889 }
890 // new file gets truncated
891 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
892 //
893 round_len = ROUND_UP(new_file_length);
832 }
833 return POSITION_FOUND;
834 }
835
836 /* Needed byte is not contained in the item pointed to by the
837 path. Set pos_in_item out of the item. */
838 if (is_indirect_le_ih(p_le_ih))
839 pos_in_item(search_path) =

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

883 /* item has to be deleted */
884 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
885 return M_DELETE;
886 }
887 // new file gets truncated
888 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
889 //
890 round_len = ROUND_UP(new_file_length);
894 /* this was n_new_file_length < le_ih ... */
891 /* this was new_file_length < le_ih ... */
895 if (round_len < le_ih_k_offset(le_ih)) {
896 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
897 return M_DELETE; /* Delete this item. */
898 }
899 /* Calculate first position and size for cutting from item. */
900 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
901 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
902

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

948
949/* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
950 If the path points to an indirect item, remove some number of its unformatted nodes.
951 In case of file truncate calculate whether this item must be deleted/truncated or last
952 unformatted node of this item will be converted to a direct item.
953 This function returns a determination of what balance mode the calling function should employ. */
954static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *path, const struct cpu_key *item_key, int *removed, /* Number of unformatted nodes which were removed
955 from end of the file. */
892 if (round_len < le_ih_k_offset(le_ih)) {
893 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
894 return M_DELETE; /* Delete this item. */
895 }
896 /* Calculate first position and size for cutting from item. */
897 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
898 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
899

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

945
946/* If the path points to a directory or direct item, calculate mode and the size cut, for balance.
947 If the path points to an indirect item, remove some number of its unformatted nodes.
948 In case of file truncate calculate whether this item must be deleted/truncated or last
949 unformatted node of this item will be converted to a direct item.
950 This function returns a determination of what balance mode the calling function should employ. */
951static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *path, const struct cpu_key *item_key, int *removed, /* Number of unformatted nodes which were removed
952 from end of the file. */
956 int *cut_size, unsigned long long n_new_file_length /* MAX_KEY_OFFSET in case of delete. */
953 int *cut_size, unsigned long long new_file_length /* MAX_KEY_OFFSET in case of delete. */
957 )
958{
959 struct super_block *sb = inode->i_sb;
960 struct item_head *p_le_ih = PATH_PITEM_HEAD(path);
961 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
962
963 BUG_ON(!th->t_trans_id);
964
965 /* Stat_data item. */
966 if (is_statdata_le_ih(p_le_ih)) {
967
954 )
955{
956 struct super_block *sb = inode->i_sb;
957 struct item_head *p_le_ih = PATH_PITEM_HEAD(path);
958 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
959
960 BUG_ON(!th->t_trans_id);
961
962 /* Stat_data item. */
963 if (is_statdata_le_ih(p_le_ih)) {
964
968 RFALSE(n_new_file_length != max_reiserfs_offset(inode),
965 RFALSE(new_file_length != max_reiserfs_offset(inode),
969 "PAP-5210: mode must be M_DELETE");
970
971 *cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
972 return M_DELETE;
973 }
974
975 /* Directory item. */
976 if (is_direntry_le_ih(p_le_ih))
977 return prepare_for_direntry_item(path, p_le_ih, inode,
966 "PAP-5210: mode must be M_DELETE");
967
968 *cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
969 return M_DELETE;
970 }
971
972 /* Directory item. */
973 if (is_direntry_le_ih(p_le_ih))
974 return prepare_for_direntry_item(path, p_le_ih, inode,
978 n_new_file_length,
975 new_file_length,
979 cut_size);
980
981 /* Direct item. */
982 if (is_direct_le_ih(p_le_ih))
983 return prepare_for_direct_item(path, p_le_ih, inode,
976 cut_size);
977
978 /* Direct item. */
979 if (is_direct_le_ih(p_le_ih))
980 return prepare_for_direct_item(path, p_le_ih, inode,
984 n_new_file_length, cut_size);
981 new_file_length, cut_size);
985
986 /* Case of an indirect item. */
987 {
988 int blk_size = sb->s_blocksize;
989 struct item_head s_ih;
990 int need_re_search;
991 int delete = 0;
992 int result = M_CUT;
993 int pos = 0;
994
982
983 /* Case of an indirect item. */
984 {
985 int blk_size = sb->s_blocksize;
986 struct item_head s_ih;
987 int need_re_search;
988 int delete = 0;
989 int result = M_CUT;
990 int pos = 0;
991
995 if ( n_new_file_length == max_reiserfs_offset (inode) ) {
992 if ( new_file_length == max_reiserfs_offset (inode) ) {
996 /* prepare_for_delete_or_cut() is called by
997 * reiserfs_delete_item() */
993 /* prepare_for_delete_or_cut() is called by
994 * reiserfs_delete_item() */
998 n_new_file_length = 0;
995 new_file_length = 0;
999 delete = 1;
1000 }
1001
1002 do {
1003 need_re_search = 0;
1004 *cut_size = 0;
1005 bh = PATH_PLAST_BUFFER(path);
1006 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
1007 pos = I_UNFM_NUM(&s_ih);
1008
996 delete = 1;
997 }
998
999 do {
1000 need_re_search = 0;
1001 *cut_size = 0;
1002 bh = PATH_PLAST_BUFFER(path);
1003 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
1004 pos = I_UNFM_NUM(&s_ih);
1005
1009 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > n_new_file_length) {
1006 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
1010 __le32 *unfm;
1011 __u32 block;
1012
1013 /* Each unformatted block deletion may involve one additional
1014 * bitmap block into the transaction, thereby the initial
1015 * journal space reservation might not be enough. */
1016 if (!delete && (*cut_size) != 0 &&
1017 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)

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

1057 * direct item? */
1058 result = M_CONVERT;
1059 }
1060 return result;
1061 }
1062}
1063
1064/* Calculate number of bytes which will be deleted or cut during balance */
1007 __le32 *unfm;
1008 __u32 block;
1009
1010 /* Each unformatted block deletion may involve one additional
1011 * bitmap block into the transaction, thereby the initial
1012 * journal space reservation might not be enough. */
1013 if (!delete && (*cut_size) != 0 &&
1014 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)

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

1054 * direct item? */
1055 result = M_CONVERT;
1056 }
1057 return result;
1058 }
1059}
1060
1061/* Calculate number of bytes which will be deleted or cut during balance */
1065static int calc_deleted_bytes_number(struct tree_balance *tb, char c_mode)
1062static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
1066{
1063{
1067 int n_del_size;
1064 int del_size;
1068 struct item_head *p_le_ih = PATH_PITEM_HEAD(tb->tb_path);
1069
1070 if (is_statdata_le_ih(p_le_ih))
1071 return 0;
1072
1065 struct item_head *p_le_ih = PATH_PITEM_HEAD(tb->tb_path);
1066
1067 if (is_statdata_le_ih(p_le_ih))
1068 return 0;
1069
1073 n_del_size =
1074 (c_mode ==
1070 del_size =
1071 (mode ==
1075 M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
1076 if (is_direntry_le_ih(p_le_ih)) {
1072 M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
1073 if (is_direntry_le_ih(p_le_ih)) {
1077 // return EMPTY_DIR_SIZE; /* We delete emty directoris only. */
1078 // we can't use EMPTY_DIR_SIZE, as old format dirs have a different
1079 // empty size. ick. FIXME, is this right?
1080 //
1081 return n_del_size;
1074 /* return EMPTY_DIR_SIZE; We delete emty directoris only.
1075 * we can't use EMPTY_DIR_SIZE, as old format dirs have a different
1076 * empty size. ick. FIXME, is this right? */
1077 return del_size;
1082 }
1083
1084 if (is_indirect_le_ih(p_le_ih))
1078 }
1079
1080 if (is_indirect_le_ih(p_le_ih))
1085 n_del_size = (n_del_size / UNFM_P_SIZE) *
1081 del_size = (del_size / UNFM_P_SIZE) *
1086 (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
1082 (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
1087 return n_del_size;
1083 return del_size;
1088}
1089
1090static void init_tb_struct(struct reiserfs_transaction_handle *th,
1091 struct tree_balance *tb,
1092 struct super_block *sb,
1084}
1085
1086static void init_tb_struct(struct reiserfs_transaction_handle *th,
1087 struct tree_balance *tb,
1088 struct super_block *sb,
1093 struct treepath *path, int n_size)
1089 struct treepath *path, int size)
1094{
1095
1096 BUG_ON(!th->t_trans_id);
1097
1098 memset(tb, '\0', sizeof(struct tree_balance));
1099 tb->transaction_handle = th;
1100 tb->tb_sb = sb;
1101 tb->tb_path = path;
1102 PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1103 PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
1090{
1091
1092 BUG_ON(!th->t_trans_id);
1093
1094 memset(tb, '\0', sizeof(struct tree_balance));
1095 tb->transaction_handle = th;
1096 tb->tb_sb = sb;
1097 tb->tb_path = path;
1098 PATH_OFFSET_PBUFFER(path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL;
1099 PATH_OFFSET_POSITION(path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0;
1104 tb->insert_size[0] = n_size;
1100 tb->insert_size[0] = size;
1105}
1106
1107void padd_item(char *item, int total_length, int length)
1108{
1109 int i;
1110
1111 for (i = total_length; i > length;)
1112 item[--i] = 0;

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

1151 struct treepath *path, const struct cpu_key *item_key,
1152 struct inode *inode, struct buffer_head *un_bh)
1153{
1154 struct super_block *sb = inode->i_sb;
1155 struct tree_balance s_del_balance;
1156 struct item_head s_ih;
1157 struct item_head *q_ih;
1158 int quota_cut_bytes;
1101}
1102
1103void padd_item(char *item, int total_length, int length)
1104{
1105 int i;
1106
1107 for (i = total_length; i > length;)
1108 item[--i] = 0;

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

1147 struct treepath *path, const struct cpu_key *item_key,
1148 struct inode *inode, struct buffer_head *un_bh)
1149{
1150 struct super_block *sb = inode->i_sb;
1151 struct tree_balance s_del_balance;
1152 struct item_head s_ih;
1153 struct item_head *q_ih;
1154 int quota_cut_bytes;
1159 int n_ret_value, n_del_size, n_removed;
1155 int ret_value, del_size, removed;
1160
1161#ifdef CONFIG_REISERFS_CHECK
1156
1157#ifdef CONFIG_REISERFS_CHECK
1162 char c_mode;
1163 int n_iter = 0;
1158 char mode;
1159 int iter = 0;
1164#endif
1165
1166 BUG_ON(!th->t_trans_id);
1167
1168 init_tb_struct(th, &s_del_balance, sb, path,
1169 0 /*size is unknown */ );
1170
1171 while (1) {
1160#endif
1161
1162 BUG_ON(!th->t_trans_id);
1163
1164 init_tb_struct(th, &s_del_balance, sb, path,
1165 0 /*size is unknown */ );
1166
1167 while (1) {
1172 n_removed = 0;
1168 removed = 0;
1173
1174#ifdef CONFIG_REISERFS_CHECK
1169
1170#ifdef CONFIG_REISERFS_CHECK
1175 n_iter++;
1176 c_mode =
1171 iter++;
1172 mode =
1177#endif
1178 prepare_for_delete_or_cut(th, inode, path,
1173#endif
1174 prepare_for_delete_or_cut(th, inode, path,
1179 item_key, &n_removed,
1180 &n_del_size,
1175 item_key, &removed,
1176 &del_size,
1181 max_reiserfs_offset(inode));
1182
1177 max_reiserfs_offset(inode));
1178
1183 RFALSE(c_mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
1179 RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
1184
1185 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
1180
1181 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
1186 s_del_balance.insert_size[0] = n_del_size;
1182 s_del_balance.insert_size[0] = del_size;
1187
1183
1188 n_ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1189 if (n_ret_value != REPEAT_SEARCH)
1184 ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1185 if (ret_value != REPEAT_SEARCH)
1190 break;
1191
1192 PROC_INFO_INC(sb, delete_item_restarted);
1193
1194 // file system changed, repeat search
1186 break;
1187
1188 PROC_INFO_INC(sb, delete_item_restarted);
1189
1190 // file system changed, repeat search
1195 n_ret_value =
1191 ret_value =
1196 search_for_position_by_key(sb, item_key, path);
1192 search_for_position_by_key(sb, item_key, path);
1197 if (n_ret_value == IO_ERROR)
1193 if (ret_value == IO_ERROR)
1198 break;
1194 break;
1199 if (n_ret_value == FILE_NOT_FOUND) {
1195 if (ret_value == FILE_NOT_FOUND) {
1200 reiserfs_warning(sb, "vs-5340",
1201 "no items of the file %K found",
1202 item_key);
1203 break;
1204 }
1205 } /* while (1) */
1206
1196 reiserfs_warning(sb, "vs-5340",
1197 "no items of the file %K found",
1198 item_key);
1199 break;
1200 }
1201 } /* while (1) */
1202
1207 if (n_ret_value != CARRY_ON) {
1203 if (ret_value != CARRY_ON) {
1208 unfix_nodes(&s_del_balance);
1209 return 0;
1210 }
1211 // reiserfs_delete_item returns item length when success
1204 unfix_nodes(&s_del_balance);
1205 return 0;
1206 }
1207 // reiserfs_delete_item returns item length when success
1212 n_ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
1208 ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
1213 q_ih = get_ih(path);
1214 quota_cut_bytes = ih_item_len(q_ih);
1215
1216 /* hack so the quota code doesn't have to guess if the file
1217 ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
1218 ** We test the offset because the tail might have been
1219 ** split into multiple items, and we only want to decrement for
1220 ** the unfm node once

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

1250 ** can't use un_bh->b_data.
1251 ** -clm
1252 */
1253
1254 data = kmap_atomic(un_bh->b_page, KM_USER0);
1255 off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
1256 memcpy(data + off,
1257 B_I_PITEM(PATH_PLAST_BUFFER(path), &s_ih),
1209 q_ih = get_ih(path);
1210 quota_cut_bytes = ih_item_len(q_ih);
1211
1212 /* hack so the quota code doesn't have to guess if the file
1213 ** has a tail. On tail insert, we allocate quota for 1 unformatted node.
1214 ** We test the offset because the tail might have been
1215 ** split into multiple items, and we only want to decrement for
1216 ** the unfm node once

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

1246 ** can't use un_bh->b_data.
1247 ** -clm
1248 */
1249
1250 data = kmap_atomic(un_bh->b_page, KM_USER0);
1251 off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1));
1252 memcpy(data + off,
1253 B_I_PITEM(PATH_PLAST_BUFFER(path), &s_ih),
1258 n_ret_value);
1254 ret_value);
1259 kunmap_atomic(data, KM_USER0);
1260 }
1261 /* Perform balancing after all resources have been collected at once. */
1262 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
1263
1264#ifdef REISERQUOTA_DEBUG
1265 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
1266 "reiserquota delete_item(): freeing %u, id=%u type=%c",
1267 quota_cut_bytes, inode->i_uid, head2type(&s_ih));
1268#endif
1269 DQUOT_FREE_SPACE_NODIRTY(inode, quota_cut_bytes);
1270
1271 /* Return deleted body length */
1255 kunmap_atomic(data, KM_USER0);
1256 }
1257 /* Perform balancing after all resources have been collected at once. */
1258 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
1259
1260#ifdef REISERQUOTA_DEBUG
1261 reiserfs_debug(sb, REISERFS_DEBUG_CODE,
1262 "reiserquota delete_item(): freeing %u, id=%u type=%c",
1263 quota_cut_bytes, inode->i_uid, head2type(&s_ih));
1264#endif
1265 DQUOT_FREE_SPACE_NODIRTY(inode, quota_cut_bytes);
1266
1267 /* Return deleted body length */
1272 return n_ret_value;
1268 return ret_value;
1273}
1274
1275/* Summary Of Mechanisms For Handling Collisions Between Processes:
1276
1277 deletion of the body of the object is performed by iput(), with the
1278 result that if multiple processes are operating on a file, the
1279 deletion of the body of the file is deferred until the last process
1280 that has an open inode performs its iput().

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

1427 }
1428}
1429
1430static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
1431 struct inode *inode,
1432 struct page *page,
1433 struct treepath *path,
1434 const struct cpu_key *item_key,
1269}
1270
1271/* Summary Of Mechanisms For Handling Collisions Between Processes:
1272
1273 deletion of the body of the object is performed by iput(), with the
1274 result that if multiple processes are operating on a file, the
1275 deletion of the body of the file is deferred until the last process
1276 that has an open inode performs its iput().

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

1423 }
1424}
1425
1426static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
1427 struct inode *inode,
1428 struct page *page,
1429 struct treepath *path,
1430 const struct cpu_key *item_key,
1435 loff_t n_new_file_size, char *mode)
1431 loff_t new_file_size, char *mode)
1436{
1437 struct super_block *sb = inode->i_sb;
1432{
1433 struct super_block *sb = inode->i_sb;
1438 int n_block_size = sb->s_blocksize;
1434 int block_size = sb->s_blocksize;
1439 int cut_bytes;
1440 BUG_ON(!th->t_trans_id);
1435 int cut_bytes;
1436 BUG_ON(!th->t_trans_id);
1441 BUG_ON(n_new_file_size != inode->i_size);
1437 BUG_ON(new_file_size != inode->i_size);
1442
1443 /* the page being sent in could be NULL if there was an i/o error
1444 ** reading in the last block. The user will hit problems trying to
1445 ** read the file, but for now we just skip the indirect2direct
1446 */
1447 if (atomic_read(&inode->i_count) > 1 ||
1448 !tail_has_to_be_packed(inode) ||
1449 !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
1450 /* leave tail in an unformatted node */
1451 *mode = M_SKIP_BALANCING;
1452 cut_bytes =
1438
1439 /* the page being sent in could be NULL if there was an i/o error
1440 ** reading in the last block. The user will hit problems trying to
1441 ** read the file, but for now we just skip the indirect2direct
1442 */
1443 if (atomic_read(&inode->i_count) > 1 ||
1444 !tail_has_to_be_packed(inode) ||
1445 !page || (REISERFS_I(inode)->i_flags & i_nopack_mask)) {
1446 /* leave tail in an unformatted node */
1447 *mode = M_SKIP_BALANCING;
1448 cut_bytes =
1453 n_block_size - (n_new_file_size & (n_block_size - 1));
1449 block_size - (new_file_size & (block_size - 1));
1454 pathrelse(path);
1455 return cut_bytes;
1456 }
1457 /* Perform the conversion to a direct_item. */
1458 /* return indirect_to_direct(inode, path, item_key,
1450 pathrelse(path);
1451 return cut_bytes;
1452 }
1453 /* Perform the conversion to a direct_item. */
1454 /* return indirect_to_direct(inode, path, item_key,
1459 n_new_file_size, mode); */
1455 new_file_size, mode); */
1460 return indirect2direct(th, inode, page, path, item_key,
1456 return indirect2direct(th, inode, page, path, item_key,
1461 n_new_file_size, mode);
1457 new_file_size, mode);
1462}
1463
1464/* we did indirect_to_direct conversion. And we have inserted direct
1465 item successesfully, but there were no disk space to cut unfm
1466 pointer being converted. Therefore we have to delete inserted
1467 direct item(s) */
1468static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1469 struct inode *inode, struct treepath *path)

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

1507 mark_inode_dirty(inode);
1508}
1509
1510/* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
1511int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1512 struct treepath *path,
1513 struct cpu_key *item_key,
1514 struct inode *inode,
1458}
1459
1460/* we did indirect_to_direct conversion. And we have inserted direct
1461 item successesfully, but there were no disk space to cut unfm
1462 pointer being converted. Therefore we have to delete inserted
1463 direct item(s) */
1464static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1465 struct inode *inode, struct treepath *path)

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

1503 mark_inode_dirty(inode);
1504}
1505
1506/* (Truncate or cut entry) or delete object item. Returns < 0 on failure */
1507int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1508 struct treepath *path,
1509 struct cpu_key *item_key,
1510 struct inode *inode,
1515 struct page *page, loff_t n_new_file_size)
1511 struct page *page, loff_t new_file_size)
1516{
1517 struct super_block *sb = inode->i_sb;
1518 /* Every function which is going to call do_balance must first
1519 create a tree_balance structure. Then it must fill up this
1520 structure by using the init_tb_struct and fix_nodes functions.
1521 After that we can make tree balancing. */
1522 struct tree_balance s_cut_balance;
1523 struct item_head *p_le_ih;
1512{
1513 struct super_block *sb = inode->i_sb;
1514 /* Every function which is going to call do_balance must first
1515 create a tree_balance structure. Then it must fill up this
1516 structure by using the init_tb_struct and fix_nodes functions.
1517 After that we can make tree balancing. */
1518 struct tree_balance s_cut_balance;
1519 struct item_head *p_le_ih;
1524 int n_cut_size = 0, /* Amount to be cut. */
1525 n_ret_value = CARRY_ON, n_removed = 0, /* Number of the removed unformatted nodes. */
1526 n_is_inode_locked = 0;
1527 char c_mode; /* Mode of the balance. */
1520 int cut_size = 0, /* Amount to be cut. */
1521 ret_value = CARRY_ON, removed = 0, /* Number of the removed unformatted nodes. */
1522 is_inode_locked = 0;
1523 char mode; /* Mode of the balance. */
1528 int retval2 = -1;
1529 int quota_cut_bytes;
1530 loff_t tail_pos = 0;
1531
1532 BUG_ON(!th->t_trans_id);
1533
1534 init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
1524 int retval2 = -1;
1525 int quota_cut_bytes;
1526 loff_t tail_pos = 0;
1527
1528 BUG_ON(!th->t_trans_id);
1529
1530 init_tb_struct(th, &s_cut_balance, inode->i_sb, path,
1535 n_cut_size);
1531 cut_size);
1536
1537 /* Repeat this loop until we either cut the item without needing
1538 to balance, or we fix_nodes without schedule occurring */
1539 while (1) {
1540 /* Determine the balance mode, position of the first byte to
1541 be cut, and size to be cut. In case of the indirect item
1542 free unformatted nodes which are pointed to by the cut
1543 pointers. */
1544
1532
1533 /* Repeat this loop until we either cut the item without needing
1534 to balance, or we fix_nodes without schedule occurring */
1535 while (1) {
1536 /* Determine the balance mode, position of the first byte to
1537 be cut, and size to be cut. In case of the indirect item
1538 free unformatted nodes which are pointed to by the cut
1539 pointers. */
1540
1545 c_mode =
1541 mode =
1546 prepare_for_delete_or_cut(th, inode, path,
1542 prepare_for_delete_or_cut(th, inode, path,
1547 item_key, &n_removed,
1548 &n_cut_size, n_new_file_size);
1549 if (c_mode == M_CONVERT) {
1543 item_key, &removed,
1544 &cut_size, new_file_size);
1545 if (mode == M_CONVERT) {
1550 /* convert last unformatted node to direct item or leave
1551 tail in the unformatted node */
1546 /* convert last unformatted node to direct item or leave
1547 tail in the unformatted node */
1552 RFALSE(n_ret_value != CARRY_ON,
1548 RFALSE(ret_value != CARRY_ON,
1553 "PAP-5570: can not convert twice");
1554
1549 "PAP-5570: can not convert twice");
1550
1555 n_ret_value =
1551 ret_value =
1556 maybe_indirect_to_direct(th, inode, page,
1557 path, item_key,
1552 maybe_indirect_to_direct(th, inode, page,
1553 path, item_key,
1558 n_new_file_size, &c_mode);
1559 if (c_mode == M_SKIP_BALANCING)
1554 new_file_size, &mode);
1555 if (mode == M_SKIP_BALANCING)
1560 /* tail has been left in the unformatted node */
1556 /* tail has been left in the unformatted node */
1561 return n_ret_value;
1557 return ret_value;
1562
1558
1563 n_is_inode_locked = 1;
1559 is_inode_locked = 1;
1564
1565 /* removing of last unformatted node will change value we
1566 have to return to truncate. Save it */
1560
1561 /* removing of last unformatted node will change value we
1562 have to return to truncate. Save it */
1567 retval2 = n_ret_value;
1568 /*retval2 = sb->s_blocksize - (n_new_file_size & (sb->s_blocksize - 1)); */
1563 retval2 = ret_value;
1564 /*retval2 = sb->s_blocksize - (new_file_size & (sb->s_blocksize - 1)); */
1569
1570 /* So, we have performed the first part of the conversion:
1571 inserting the new direct item. Now we are removing the
1572 last unformatted node pointer. Set key to search for
1573 it. */
1574 set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1575 item_key->key_length = 4;
1565
1566 /* So, we have performed the first part of the conversion:
1567 inserting the new direct item. Now we are removing the
1568 last unformatted node pointer. Set key to search for
1569 it. */
1570 set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1571 item_key->key_length = 4;
1576 n_new_file_size -=
1577 (n_new_file_size & (sb->s_blocksize - 1));
1578 tail_pos = n_new_file_size;
1579 set_cpu_key_k_offset(item_key, n_new_file_size + 1);
1572 new_file_size -=
1573 (new_file_size & (sb->s_blocksize - 1));
1574 tail_pos = new_file_size;
1575 set_cpu_key_k_offset(item_key, new_file_size + 1);
1580 if (search_for_position_by_key
1581 (sb, item_key,
1582 path) == POSITION_NOT_FOUND) {
1583 print_block(PATH_PLAST_BUFFER(path), 3,
1584 PATH_LAST_POSITION(path) - 1,
1585 PATH_LAST_POSITION(path) + 1);
1586 reiserfs_panic(sb, "PAP-5580", "item to "
1587 "convert does not exist (%K)",
1588 item_key);
1589 }
1590 continue;
1591 }
1576 if (search_for_position_by_key
1577 (sb, item_key,
1578 path) == POSITION_NOT_FOUND) {
1579 print_block(PATH_PLAST_BUFFER(path), 3,
1580 PATH_LAST_POSITION(path) - 1,
1581 PATH_LAST_POSITION(path) + 1);
1582 reiserfs_panic(sb, "PAP-5580", "item to "
1583 "convert does not exist (%K)",
1584 item_key);
1585 }
1586 continue;
1587 }
1592 if (n_cut_size == 0) {
1588 if (cut_size == 0) {
1593 pathrelse(path);
1594 return 0;
1595 }
1596
1589 pathrelse(path);
1590 return 0;
1591 }
1592
1597 s_cut_balance.insert_size[0] = n_cut_size;
1593 s_cut_balance.insert_size[0] = cut_size;
1598
1594
1599 n_ret_value = fix_nodes(c_mode, &s_cut_balance, NULL, NULL);
1600 if (n_ret_value != REPEAT_SEARCH)
1595 ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
1596 if (ret_value != REPEAT_SEARCH)
1601 break;
1602
1603 PROC_INFO_INC(sb, cut_from_item_restarted);
1604
1597 break;
1598
1599 PROC_INFO_INC(sb, cut_from_item_restarted);
1600
1605 n_ret_value =
1601 ret_value =
1606 search_for_position_by_key(sb, item_key, path);
1602 search_for_position_by_key(sb, item_key, path);
1607 if (n_ret_value == POSITION_FOUND)
1603 if (ret_value == POSITION_FOUND)
1608 continue;
1609
1610 reiserfs_warning(sb, "PAP-5610", "item %K not found",
1611 item_key);
1612 unfix_nodes(&s_cut_balance);
1604 continue;
1605
1606 reiserfs_warning(sb, "PAP-5610", "item %K not found",
1607 item_key);
1608 unfix_nodes(&s_cut_balance);
1613 return (n_ret_value == IO_ERROR) ? -EIO : -ENOENT;
1609 return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
1614 } /* while */
1615
1616 // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
1610 } /* while */
1611
1612 // check fix_nodes results (IO_ERROR or NO_DISK_SPACE)
1617 if (n_ret_value != CARRY_ON) {
1618 if (n_is_inode_locked) {
1613 if (ret_value != CARRY_ON) {
1614 if (is_inode_locked) {
1619 // FIXME: this seems to be not needed: we are always able
1620 // to cut item
1621 indirect_to_direct_roll_back(th, inode, path);
1622 }
1615 // FIXME: this seems to be not needed: we are always able
1616 // to cut item
1617 indirect_to_direct_roll_back(th, inode, path);
1618 }
1623 if (n_ret_value == NO_DISK_SPACE)
1619 if (ret_value == NO_DISK_SPACE)
1624 reiserfs_warning(sb, "reiserfs-5092",
1625 "NO_DISK_SPACE");
1626 unfix_nodes(&s_cut_balance);
1627 return -EIO;
1628 }
1629
1630 /* go ahead and perform balancing */
1631
1620 reiserfs_warning(sb, "reiserfs-5092",
1621 "NO_DISK_SPACE");
1622 unfix_nodes(&s_cut_balance);
1623 return -EIO;
1624 }
1625
1626 /* go ahead and perform balancing */
1627
1632 RFALSE(c_mode == M_PASTE || c_mode == M_INSERT, "invalid mode");
1628 RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
1633
1634 /* Calculate number of bytes that need to be cut from the item. */
1635 quota_cut_bytes =
1629
1630 /* Calculate number of bytes that need to be cut from the item. */
1631 quota_cut_bytes =
1636 (c_mode ==
1632 (mode ==
1637 M_DELETE) ? ih_item_len(get_ih(path)) : -s_cut_balance.
1638 insert_size[0];
1639 if (retval2 == -1)
1633 M_DELETE) ? ih_item_len(get_ih(path)) : -s_cut_balance.
1634 insert_size[0];
1635 if (retval2 == -1)
1640 n_ret_value = calc_deleted_bytes_number(&s_cut_balance, c_mode);
1636 ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
1641 else
1637 else
1642 n_ret_value = retval2;
1638 ret_value = retval2;
1643
1644 /* For direct items, we only change the quota when deleting the last
1645 ** item.
1646 */
1647 p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
1648 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
1639
1640 /* For direct items, we only change the quota when deleting the last
1641 ** item.
1642 */
1643 p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
1644 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_le_ih)) {
1649 if (c_mode == M_DELETE &&
1645 if (mode == M_DELETE &&
1650 (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
1651 1) {
1652 // FIXME: this is to keep 3.5 happy
1653 REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
1654 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
1655 } else {
1656 quota_cut_bytes = 0;
1657 }
1658 }
1659#ifdef CONFIG_REISERFS_CHECK
1646 (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
1647 1) {
1648 // FIXME: this is to keep 3.5 happy
1649 REISERFS_I(inode)->i_first_direct_byte = U32_MAX;
1650 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
1651 } else {
1652 quota_cut_bytes = 0;
1653 }
1654 }
1655#ifdef CONFIG_REISERFS_CHECK
1660 if (n_is_inode_locked) {
1656 if (is_inode_locked) {
1661 struct item_head *le_ih =
1662 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1663 /* we are going to complete indirect2direct conversion. Make
1664 sure, that we exactly remove last unformatted node pointer
1665 of the item */
1666 if (!is_indirect_le_ih(le_ih))
1667 reiserfs_panic(sb, "vs-5652",
1668 "item must be indirect %h", le_ih);
1669
1657 struct item_head *le_ih =
1658 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1659 /* we are going to complete indirect2direct conversion. Make
1660 sure, that we exactly remove last unformatted node pointer
1661 of the item */
1662 if (!is_indirect_le_ih(le_ih))
1663 reiserfs_panic(sb, "vs-5652",
1664 "item must be indirect %h", le_ih);
1665
1670 if (c_mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
1666 if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
1671 reiserfs_panic(sb, "vs-5653", "completing "
1672 "indirect2direct conversion indirect "
1673 "item %h being deleted must be of "
1674 "4 byte long", le_ih);
1675
1667 reiserfs_panic(sb, "vs-5653", "completing "
1668 "indirect2direct conversion indirect "
1669 "item %h being deleted must be of "
1670 "4 byte long", le_ih);
1671
1676 if (c_mode == M_CUT
1672 if (mode == M_CUT
1677 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
1678 reiserfs_panic(sb, "vs-5654", "can not complete "
1679 "indirect2direct conversion of %h "
1680 "(CUT, insert_size==%d)",
1681 le_ih, s_cut_balance.insert_size[0]);
1682 }
1683 /* it would be useful to make sure, that right neighboring
1684 item is direct item of this file */
1685 }
1686#endif
1687
1673 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
1674 reiserfs_panic(sb, "vs-5654", "can not complete "
1675 "indirect2direct conversion of %h "
1676 "(CUT, insert_size==%d)",
1677 le_ih, s_cut_balance.insert_size[0]);
1678 }
1679 /* it would be useful to make sure, that right neighboring
1680 item is direct item of this file */
1681 }
1682#endif
1683
1688 do_balance(&s_cut_balance, NULL, NULL, c_mode);
1689 if (n_is_inode_locked) {
1684 do_balance(&s_cut_balance, NULL, NULL, mode);
1685 if (is_inode_locked) {
1690 /* we've done an indirect->direct conversion. when the data block
1691 ** was freed, it was removed from the list of blocks that must
1692 ** be flushed before the transaction commits, make sure to
1693 ** unmap and invalidate it
1694 */
1695 unmap_buffers(page, tail_pos);
1696 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
1697 }
1698#ifdef REISERQUOTA_DEBUG
1699 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1700 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1701 quota_cut_bytes, inode->i_uid, '?');
1702#endif
1703 DQUOT_FREE_SPACE_NODIRTY(inode, quota_cut_bytes);
1686 /* we've done an indirect->direct conversion. when the data block
1687 ** was freed, it was removed from the list of blocks that must
1688 ** be flushed before the transaction commits, make sure to
1689 ** unmap and invalidate it
1690 */
1691 unmap_buffers(page, tail_pos);
1692 REISERFS_I(inode)->i_flags &= ~i_pack_on_close_mask;
1693 }
1694#ifdef REISERQUOTA_DEBUG
1695 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1696 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1697 quota_cut_bytes, inode->i_uid, '?');
1698#endif
1699 DQUOT_FREE_SPACE_NODIRTY(inode, quota_cut_bytes);
1704 return n_ret_value;
1700 return ret_value;
1705}
1706
1707static void truncate_directory(struct reiserfs_transaction_handle *th,
1708 struct inode *inode)
1709{
1710 BUG_ON(!th->t_trans_id);
1711 if (inode->i_nlink)
1712 reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");

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

1728 file_release to convert
1729 the tail - no timestamps
1730 should be updated */
1731 )
1732{
1733 INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
1734 struct item_head *p_le_ih; /* Pointer to an item header. */
1735 struct cpu_key s_item_key; /* Key to search for a previous file item. */
1701}
1702
1703static void truncate_directory(struct reiserfs_transaction_handle *th,
1704 struct inode *inode)
1705{
1706 BUG_ON(!th->t_trans_id);
1707 if (inode->i_nlink)
1708 reiserfs_error(inode->i_sb, "vs-5655", "link count != 0");

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

1724 file_release to convert
1725 the tail - no timestamps
1726 should be updated */
1727 )
1728{
1729 INITIALIZE_PATH(s_search_path); /* Path to the current object item. */
1730 struct item_head *p_le_ih; /* Pointer to an item header. */
1731 struct cpu_key s_item_key; /* Key to search for a previous file item. */
1736 loff_t n_file_size, /* Old file size. */
1737 n_new_file_size; /* New file size. */
1738 int n_deleted; /* Number of deleted or truncated bytes. */
1732 loff_t file_size, /* Old file size. */
1733 new_file_size; /* New file size. */
1734 int deleted; /* Number of deleted or truncated bytes. */
1739 int retval;
1740 int err = 0;
1741
1742 BUG_ON(!th->t_trans_id);
1743 if (!
1744 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1745 || S_ISLNK(inode->i_mode)))
1746 return 0;
1747
1748 if (S_ISDIR(inode->i_mode)) {
1749 // deletion of directory - no need to update timestamps
1750 truncate_directory(th, inode);
1751 return 0;
1752 }
1753
1754 /* Get new file size. */
1735 int retval;
1736 int err = 0;
1737
1738 BUG_ON(!th->t_trans_id);
1739 if (!
1740 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
1741 || S_ISLNK(inode->i_mode)))
1742 return 0;
1743
1744 if (S_ISDIR(inode->i_mode)) {
1745 // deletion of directory - no need to update timestamps
1746 truncate_directory(th, inode);
1747 return 0;
1748 }
1749
1750 /* Get new file size. */
1755 n_new_file_size = inode->i_size;
1751 new_file_size = inode->i_size;
1756
1757 // FIXME: note, that key type is unimportant here
1758 make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
1759 TYPE_DIRECT, 3);
1760
1761 retval =
1762 search_for_position_by_key(inode->i_sb, &s_item_key,
1763 &s_search_path);

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

1777 goto out;
1778 }
1779
1780 s_search_path.pos_in_item--;
1781
1782 /* Get real file size (total length of all file items) */
1783 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1784 if (is_statdata_le_ih(p_le_ih))
1752
1753 // FIXME: note, that key type is unimportant here
1754 make_cpu_key(&s_item_key, inode, max_reiserfs_offset(inode),
1755 TYPE_DIRECT, 3);
1756
1757 retval =
1758 search_for_position_by_key(inode->i_sb, &s_item_key,
1759 &s_search_path);

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

1773 goto out;
1774 }
1775
1776 s_search_path.pos_in_item--;
1777
1778 /* Get real file size (total length of all file items) */
1779 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1780 if (is_statdata_le_ih(p_le_ih))
1785 n_file_size = 0;
1781 file_size = 0;
1786 else {
1787 loff_t offset = le_ih_k_offset(p_le_ih);
1788 int bytes =
1789 op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
1790
1791 /* this may mismatch with real file size: if last direct item
1792 had no padding zeros and last unformatted node had no free
1793 space, this file would have this file size */
1782 else {
1783 loff_t offset = le_ih_k_offset(p_le_ih);
1784 int bytes =
1785 op_bytes_number(p_le_ih, inode->i_sb->s_blocksize);
1786
1787 /* this may mismatch with real file size: if last direct item
1788 had no padding zeros and last unformatted node had no free
1789 space, this file would have this file size */
1794 n_file_size = offset + bytes - 1;
1790 file_size = offset + bytes - 1;
1795 }
1796 /*
1797 * are we doing a full truncate or delete, if so
1798 * kick in the reada code
1799 */
1791 }
1792 /*
1793 * are we doing a full truncate or delete, if so
1794 * kick in the reada code
1795 */
1800 if (n_new_file_size == 0)
1796 if (new_file_size == 0)
1801 s_search_path.reada = PATH_READA | PATH_READA_BACK;
1802
1797 s_search_path.reada = PATH_READA | PATH_READA_BACK;
1798
1803 if (n_file_size == 0 || n_file_size < n_new_file_size) {
1799 if (file_size == 0 || file_size < new_file_size) {
1804 goto update_and_out;
1805 }
1806
1807 /* Update key to search for the last file item. */
1800 goto update_and_out;
1801 }
1802
1803 /* Update key to search for the last file item. */
1808 set_cpu_key_k_offset(&s_item_key, n_file_size);
1804 set_cpu_key_k_offset(&s_item_key, file_size);
1809
1810 do {
1811 /* Cut or delete file item. */
1805
1806 do {
1807 /* Cut or delete file item. */
1812 n_deleted =
1808 deleted =
1813 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1809 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1814 inode, page, n_new_file_size);
1815 if (n_deleted < 0) {
1810 inode, page, new_file_size);
1811 if (deleted < 0) {
1816 reiserfs_warning(inode->i_sb, "vs-5665",
1817 "reiserfs_cut_from_item failed");
1818 reiserfs_check_path(&s_search_path);
1819 return 0;
1820 }
1821
1812 reiserfs_warning(inode->i_sb, "vs-5665",
1813 "reiserfs_cut_from_item failed");
1814 reiserfs_check_path(&s_search_path);
1815 return 0;
1816 }
1817
1822 RFALSE(n_deleted > n_file_size,
1818 RFALSE(deleted > file_size,
1823 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1819 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1824 n_deleted, n_file_size, &s_item_key);
1820 deleted, file_size, &s_item_key);
1825
1826 /* Change key to search the last file item. */
1821
1822 /* Change key to search the last file item. */
1827 n_file_size -= n_deleted;
1823 file_size -= deleted;
1828
1824
1829 set_cpu_key_k_offset(&s_item_key, n_file_size);
1825 set_cpu_key_k_offset(&s_item_key, file_size);
1830
1831 /* While there are bytes to truncate and previous file item is presented in the tree. */
1832
1833 /*
1834 ** This loop could take a really long time, and could log
1835 ** many more blocks than a transaction can hold. So, we do a polite
1836 ** journal end here, and if the transaction needs ending, we make
1837 ** sure the file is consistent before ending the current trans

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

1852 if (err)
1853 goto out;
1854 err = journal_begin(th, inode->i_sb,
1855 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
1856 if (err)
1857 goto out;
1858 reiserfs_update_inode_transaction(inode);
1859 }
1826
1827 /* While there are bytes to truncate and previous file item is presented in the tree. */
1828
1829 /*
1830 ** This loop could take a really long time, and could log
1831 ** many more blocks than a transaction can hold. So, we do a polite
1832 ** journal end here, and if the transaction needs ending, we make
1833 ** sure the file is consistent before ending the current trans

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

1848 if (err)
1849 goto out;
1850 err = journal_begin(th, inode->i_sb,
1851 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
1852 if (err)
1853 goto out;
1854 reiserfs_update_inode_transaction(inode);
1855 }
1860 } while (n_file_size > ROUND_UP(n_new_file_size) &&
1856 } while (file_size > ROUND_UP(new_file_size) &&
1861 search_for_position_by_key(inode->i_sb, &s_item_key,
1862 &s_search_path) == POSITION_FOUND);
1863
1857 search_for_position_by_key(inode->i_sb, &s_item_key,
1858 &s_search_path) == POSITION_FOUND);
1859
1864 RFALSE(n_file_size > ROUND_UP(n_new_file_size),
1860 RFALSE(file_size > ROUND_UP(new_file_size),
1865 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1861 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1866 n_new_file_size, n_file_size, s_item_key.on_disk_key.k_objectid);
1862 new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
1867
1868 update_and_out:
1869 if (update_timestamps) {
1870 // this is truncate, not file closing
1871 inode->i_mtime = CURRENT_TIME_SEC;
1872 inode->i_ctime = CURRENT_TIME_SEC;
1873 }
1874 reiserfs_update_sd(th, inode);

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

1913}
1914#endif /* config reiserfs check */
1915
1916/* Paste bytes to the existing item. Returns bytes number pasted into the item. */
1917int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *search_path, /* Path to the pasted item. */
1918 const struct cpu_key *key, /* Key to search for the needed item. */
1919 struct inode *inode, /* Inode item belongs to */
1920 const char *body, /* Pointer to the bytes to paste. */
1863
1864 update_and_out:
1865 if (update_timestamps) {
1866 // this is truncate, not file closing
1867 inode->i_mtime = CURRENT_TIME_SEC;
1868 inode->i_ctime = CURRENT_TIME_SEC;
1869 }
1870 reiserfs_update_sd(th, inode);

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

1909}
1910#endif /* config reiserfs check */
1911
1912/* Paste bytes to the existing item. Returns bytes number pasted into the item. */
1913int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *search_path, /* Path to the pasted item. */
1914 const struct cpu_key *key, /* Key to search for the needed item. */
1915 struct inode *inode, /* Inode item belongs to */
1916 const char *body, /* Pointer to the bytes to paste. */
1921 int n_pasted_size)
1917 int pasted_size)
1922{ /* Size of pasted bytes. */
1923 struct tree_balance s_paste_balance;
1924 int retval;
1925 int fs_gen;
1926
1927 BUG_ON(!th->t_trans_id);
1928
1929 fs_gen = get_generation(inode->i_sb);
1930
1931#ifdef REISERQUOTA_DEBUG
1932 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1933 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
1918{ /* Size of pasted bytes. */
1919 struct tree_balance s_paste_balance;
1920 int retval;
1921 int fs_gen;
1922
1923 BUG_ON(!th->t_trans_id);
1924
1925 fs_gen = get_generation(inode->i_sb);
1926
1927#ifdef REISERQUOTA_DEBUG
1928 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1929 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
1934 n_pasted_size, inode->i_uid,
1930 pasted_size, inode->i_uid,
1935 key2type(&(key->on_disk_key)));
1936#endif
1937
1931 key2type(&(key->on_disk_key)));
1932#endif
1933
1938 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, n_pasted_size)) {
1934 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, pasted_size)) {
1939 pathrelse(search_path);
1940 return -EDQUOT;
1941 }
1942 init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
1935 pathrelse(search_path);
1936 return -EDQUOT;
1937 }
1938 init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
1943 n_pasted_size);
1939 pasted_size);
1944#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1945 s_paste_balance.key = key->on_disk_key;
1946#endif
1947
1948 /* DQUOT_* can schedule, must check before the fix_nodes */
1949 if (fs_changed(fs_gen, inode->i_sb)) {
1950 goto search_again;
1951 }

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

1983 }
1984 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
1985 error_out:
1986 /* this also releases the path */
1987 unfix_nodes(&s_paste_balance);
1988#ifdef REISERQUOTA_DEBUG
1989 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1990 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
1940#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1941 s_paste_balance.key = key->on_disk_key;
1942#endif
1943
1944 /* DQUOT_* can schedule, must check before the fix_nodes */
1945 if (fs_changed(fs_gen, inode->i_sb)) {
1946 goto search_again;
1947 }

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

1979 }
1980 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
1981 error_out:
1982 /* this also releases the path */
1983 unfix_nodes(&s_paste_balance);
1984#ifdef REISERQUOTA_DEBUG
1985 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1986 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
1991 n_pasted_size, inode->i_uid,
1987 pasted_size, inode->i_uid,
1992 key2type(&(key->on_disk_key)));
1993#endif
1988 key2type(&(key->on_disk_key)));
1989#endif
1994 DQUOT_FREE_SPACE_NODIRTY(inode, n_pasted_size);
1990 DQUOT_FREE_SPACE_NODIRTY(inode, pasted_size);
1995 return retval;
1996}
1997
1998/* Insert new item into the buffer at the path.
1999 * th - active transaction handle
2000 * path - path to the inserted item
2001 * ih - pointer to the item header to insert
2002 * body - pointer to the bytes to insert

--- 83 unchanged lines hidden ---
1991 return retval;
1992}
1993
1994/* Insert new item into the buffer at the path.
1995 * th - active transaction handle
1996 * path - path to the inserted item
1997 * ih - pointer to the item header to insert
1998 * body - pointer to the bytes to insert

--- 83 unchanged lines hidden ---