1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include <linux/time.h>
53#include <linux/string.h>
54#include <linux/pagemap.h>
55#include <linux/reiserfs_fs.h>
56#include <linux/buffer_head.h>
57#include <linux/quotaops.h>
58
59
60inline int B_IS_IN_TREE(const struct buffer_head *p_s_bh)
61{
62
63 RFALSE(B_LEVEL(p_s_bh) > MAX_HEIGHT,
64 "PAP-1010: block (%b) has too big level (%z)", p_s_bh, p_s_bh);
65
66 return (B_LEVEL(p_s_bh) != FREE_LEVEL);
67}
68
69
70
71
72inline void copy_item_head(struct item_head *p_v_to,
73 const struct item_head *p_v_from)
74{
75 memcpy(p_v_to, p_v_from, IH_SIZE);
76}
77
78
79
80
81
82
83
84inline int comp_short_keys(const struct reiserfs_key *le_key,
85 const struct cpu_key *cpu_key)
86{
87 __u32 n;
88 n = le32_to_cpu(le_key->k_dir_id);
89 if (n < cpu_key->on_disk_key.k_dir_id)
90 return -1;
91 if (n > cpu_key->on_disk_key.k_dir_id)
92 return 1;
93 n = le32_to_cpu(le_key->k_objectid);
94 if (n < cpu_key->on_disk_key.k_objectid)
95 return -1;
96 if (n > cpu_key->on_disk_key.k_objectid)
97 return 1;
98 return 0;
99}
100
101
102
103
104
105
106static inline int comp_keys(const struct reiserfs_key *le_key,
107 const struct cpu_key *cpu_key)
108{
109 int retval;
110
111 retval = comp_short_keys(le_key, cpu_key);
112 if (retval)
113 return retval;
114 if (le_key_k_offset(le_key_version(le_key), le_key) <
115 cpu_key_k_offset(cpu_key))
116 return -1;
117 if (le_key_k_offset(le_key_version(le_key), le_key) >
118 cpu_key_k_offset(cpu_key))
119 return 1;
120
121 if (cpu_key->key_length == 3)
122 return 0;
123
124
125 if (le_key_k_type(le_key_version(le_key), le_key) <
126 cpu_key_k_type(cpu_key))
127 return -1;
128
129 if (le_key_k_type(le_key_version(le_key), le_key) >
130 cpu_key_k_type(cpu_key))
131 return 1;
132
133 return 0;
134}
135
136inline int comp_short_le_keys(const struct reiserfs_key *key1,
137 const struct reiserfs_key *key2)
138{
139 __u32 *p_s_1_u32, *p_s_2_u32;
140 int n_key_length = REISERFS_SHORT_KEY_LEN;
141
142 p_s_1_u32 = (__u32 *) key1;
143 p_s_2_u32 = (__u32 *) key2;
144 for (; n_key_length--; ++p_s_1_u32, ++p_s_2_u32) {
145 if (le32_to_cpu(*p_s_1_u32) < le32_to_cpu(*p_s_2_u32))
146 return -1;
147 if (le32_to_cpu(*p_s_1_u32) > le32_to_cpu(*p_s_2_u32))
148 return 1;
149 }
150 return 0;
151}
152
153inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
154{
155 int version;
156 to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
157 to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
158
159
160 version = le_key_version(from);
161 to->version = version;
162 to->on_disk_key.k_offset = le_key_k_offset(version, from);
163 to->on_disk_key.k_type = le_key_k_type(version, from);
164}
165
166
167
168inline int comp_le_keys(const struct reiserfs_key *k1,
169 const struct reiserfs_key *k2)
170{
171 return memcmp(k1, k2, sizeof(struct reiserfs_key));
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188static inline int bin_search(const void *p_v_key,
189 const void *p_v_base,
190 int p_n_num,
191 int p_n_width,
192
193
194
195
196
197
198
199 int *p_n_pos
200 )
201{
202 int n_rbound, n_lbound, n_j;
203
204 for (n_j = ((n_rbound = p_n_num - 1) + (n_lbound = 0)) / 2;
205 n_lbound <= n_rbound; n_j = (n_rbound + n_lbound) / 2)
206 switch (comp_keys
207 ((struct reiserfs_key *)((char *)p_v_base +
208 n_j * p_n_width),
209 (struct cpu_key *)p_v_key)) {
210 case -1:
211 n_lbound = n_j + 1;
212 continue;
213 case 1:
214 n_rbound = n_j - 1;
215 continue;
216 case 0:
217 *p_n_pos = n_j;
218 return ITEM_FOUND;
219 }
220
221
222
223 *p_n_pos = n_lbound;
224 return ITEM_NOT_FOUND;
225}
226
227#ifdef CONFIG_REISERFS_CHECK
228extern struct tree_balance *cur_tb;
229#endif
230
231
232const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
233
234
235static const struct reiserfs_key MAX_KEY = {
236 __constant_cpu_to_le32(0xffffffff),
237 __constant_cpu_to_le32(0xffffffff),
238 {{__constant_cpu_to_le32(0xffffffff),
239 __constant_cpu_to_le32(0xffffffff)},}
240};
241
242
243
244
245
246static inline const struct reiserfs_key *get_lkey(const struct treepath
247 *p_s_chk_path,
248 const struct super_block
249 *p_s_sb)
250{
251 int n_position, n_path_offset = p_s_chk_path->path_length;
252 struct buffer_head *p_s_parent;
253
254 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
255 "PAP-5010: invalid offset in the path");
256
257
258 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
259
260 RFALSE(!buffer_uptodate
261 (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
262 "PAP-5020: parent is not uptodate");
263
264
265 if (!B_IS_IN_TREE
266 (p_s_parent =
267 PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
268 return &MAX_KEY;
269
270 if ((n_position =
271 PATH_OFFSET_POSITION(p_s_chk_path,
272 n_path_offset)) >
273 B_NR_ITEMS(p_s_parent))
274 return &MAX_KEY;
275
276 if (B_N_CHILD_NUM(p_s_parent, n_position) !=
277 PATH_OFFSET_PBUFFER(p_s_chk_path,
278 n_path_offset + 1)->b_blocknr)
279 return &MAX_KEY;
280
281 if (n_position)
282 return B_N_PDELIM_KEY(p_s_parent, n_position - 1);
283 }
284
285 if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
286 b_blocknr == SB_ROOT_BLOCK(p_s_sb))
287 return &MIN_KEY;
288 return &MAX_KEY;
289}
290
291
292inline const struct reiserfs_key *get_rkey(const struct treepath *p_s_chk_path,
293 const struct super_block *p_s_sb)
294{
295 int n_position, n_path_offset = p_s_chk_path->path_length;
296 struct buffer_head *p_s_parent;
297
298 RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET,
299 "PAP-5030: invalid offset in the path");
300
301 while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
302
303 RFALSE(!buffer_uptodate
304 (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)),
305 "PAP-5040: parent is not uptodate");
306
307
308 if (!B_IS_IN_TREE
309 (p_s_parent =
310 PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)))
311 return &MIN_KEY;
312
313 if ((n_position =
314 PATH_OFFSET_POSITION(p_s_chk_path,
315 n_path_offset)) >
316 B_NR_ITEMS(p_s_parent))
317 return &MIN_KEY;
318
319 if (B_N_CHILD_NUM(p_s_parent, n_position) !=
320 PATH_OFFSET_PBUFFER(p_s_chk_path,
321 n_path_offset + 1)->b_blocknr)
322 return &MIN_KEY;
323
324 if (n_position != B_NR_ITEMS(p_s_parent))
325 return B_N_PDELIM_KEY(p_s_parent, n_position);
326 }
327
328 if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)->
329 b_blocknr == SB_ROOT_BLOCK(p_s_sb))
330 return &MAX_KEY;
331 return &MIN_KEY;
332}
333
334
335
336
337
338
339static inline int key_in_buffer(struct treepath *p_s_chk_path,
340 const struct cpu_key *p_s_key,
341 struct super_block *p_s_sb
342 )
343{
344
345 RFALSE(!p_s_key || p_s_chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
346 || p_s_chk_path->path_length > MAX_HEIGHT,
347 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
348 p_s_key, p_s_chk_path->path_length);
349 RFALSE(!PATH_PLAST_BUFFER(p_s_chk_path)->b_bdev,
350 "PAP-5060: device must not be NODEV");
351
352 if (comp_keys(get_lkey(p_s_chk_path, p_s_sb), p_s_key) == 1)
353
354 return 0;
355
356 if (comp_keys(get_rkey(p_s_chk_path, p_s_sb), p_s_key) != 1)
357
358 return 0;
359 return 1;
360}
361
362inline void decrement_bcount(struct buffer_head *p_s_bh)
363{
364 if (p_s_bh) {
365 if (atomic_read(&(p_s_bh->b_count))) {
366 put_bh(p_s_bh);
367 return;
368 }
369 reiserfs_panic(NULL,
370 "PAP-5070: decrement_bcount: trying to free free buffer %b",
371 p_s_bh);
372 }
373}
374
375
376void decrement_counters_in_path(struct treepath *p_s_search_path)
377{
378 int n_path_offset = p_s_search_path->path_length;
379
380 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET ||
381 n_path_offset > EXTENDED_MAX_HEIGHT - 1,
382 "PAP-5080: invalid path offset of %d", n_path_offset);
383
384 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
385 struct buffer_head *bh;
386
387 bh = PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--);
388 decrement_bcount(bh);
389 }
390 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
391}
392
393int reiserfs_check_path(struct treepath *p)
394{
395 RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
396 "path not properly relsed");
397 return 0;
398}
399
400
401
402
403
404
405void pathrelse_and_restore(struct super_block *s, struct treepath *p_s_search_path)
406{
407 int n_path_offset = p_s_search_path->path_length;
408
409 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
410 "clm-4000: invalid path offset");
411
412 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
413 reiserfs_restore_prepared_buffer(s,
414 PATH_OFFSET_PBUFFER
415 (p_s_search_path,
416 n_path_offset));
417 brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
418 }
419 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
420}
421
422
423void pathrelse(struct treepath *p_s_search_path)
424{
425 int n_path_offset = p_s_search_path->path_length;
426
427 RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
428 "PAP-5090: invalid path offset");
429
430 while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
431 brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--));
432
433 p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
434}
435
436static int is_leaf(char *buf, int blocksize, struct buffer_head *bh)
437{
438 struct block_head *blkh;
439 struct item_head *ih;
440 int used_space;
441 int prev_location;
442 int i;
443 int nr;
444
445 blkh = (struct block_head *)buf;
446 if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
447 reiserfs_warning(NULL,
448 "is_leaf: this should be caught earlier");
449 return 0;
450 }
451
452 nr = blkh_nr_item(blkh);
453 if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
454
455 reiserfs_warning(NULL, "is_leaf: nr_item seems wrong: %z", bh);
456 return 0;
457 }
458 ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
459 used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
460 if (used_space != blocksize - blkh_free_space(blkh)) {
461
462 reiserfs_warning(NULL, "is_leaf: free space seems wrong: %z",
463 bh);
464 return 0;
465 }
466
467
468
469
470 ih = (struct item_head *)(buf + BLKH_SIZE);
471 prev_location = blocksize;
472 for (i = 0; i < nr; i++, ih++) {
473 if (le_ih_k_type(ih) == TYPE_ANY) {
474 reiserfs_warning(NULL,
475 "is_leaf: wrong item type for item %h",
476 ih);
477 return 0;
478 }
479 if (ih_location(ih) >= blocksize
480 || ih_location(ih) < IH_SIZE * nr) {
481 reiserfs_warning(NULL,
482 "is_leaf: item location seems wrong: %h",
483 ih);
484 return 0;
485 }
486 if (ih_item_len(ih) < 1
487 || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
488 reiserfs_warning(NULL,
489 "is_leaf: item length seems wrong: %h",
490 ih);
491 return 0;
492 }
493 if (prev_location - ih_location(ih) != ih_item_len(ih)) {
494 reiserfs_warning(NULL,
495 "is_leaf: item location seems wrong (second one): %h",
496 ih);
497 return 0;
498 }
499 prev_location = ih_location(ih);
500 }
501
502
503 return 1;
504}
505
506
507static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
508{
509 struct block_head *blkh;
510 int nr;
511 int used_space;
512
513 blkh = (struct block_head *)buf;
514 nr = blkh_level(blkh);
515 if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
516
517 reiserfs_warning(NULL,
518 "is_internal: this should be caught earlier");
519 return 0;
520 }
521
522 nr = blkh_nr_item(blkh);
523 if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
524
525 reiserfs_warning(NULL,
526 "is_internal: number of key seems wrong: %z",
527 bh);
528 return 0;
529 }
530
531 used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
532 if (used_space != blocksize - blkh_free_space(blkh)) {
533 reiserfs_warning(NULL,
534 "is_internal: free space seems wrong: %z", bh);
535 return 0;
536 }
537
538 return 1;
539}
540
541
542
543static int is_tree_node(struct buffer_head *bh, int level)
544{
545 if (B_LEVEL(bh) != level) {
546 reiserfs_warning(NULL,
547 "is_tree_node: node level %d does not match to the expected one %d",
548 B_LEVEL(bh), level);
549 return 0;
550 }
551 if (level == DISK_LEAF_NODE_LEVEL)
552 return is_leaf(bh->b_data, bh->b_size, bh);
553
554 return is_internal(bh->b_data, bh->b_size, bh);
555}
556
557#define SEARCH_BY_KEY_READA 16
558
559
560static void search_by_key_reada(struct super_block *s,
561 struct buffer_head **bh,
562 b_blocknr_t *b, int num)
563{
564 int i, j;
565
566 for (i = 0; i < num; i++) {
567 bh[i] = sb_getblk(s, b[i]);
568 }
569 for (j = 0; j < i; j++) {
570
571
572
573
574 if (!buffer_uptodate(bh[j]))
575 ll_rw_block(READA, 1, bh + j);
576 brelse(bh[j]);
577 }
578}
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603int search_by_key(struct super_block *p_s_sb, const struct cpu_key *p_s_key,
604 struct treepath *p_s_search_path,
605
606
607
608
609 int n_stop_level
610
611
612 )
613{
614 b_blocknr_t n_block_number;
615 int expected_level;
616 struct buffer_head *p_s_bh;
617 struct path_element *p_s_last_element;
618 int n_node_level, n_retval;
619 int right_neighbor_of_leaf_node;
620 int fs_gen;
621 struct buffer_head *reada_bh[SEARCH_BY_KEY_READA];
622 b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA];
623 int reada_count = 0;
624
625#ifdef CONFIG_REISERFS_CHECK
626 int n_repeat_counter = 0;
627#endif
628
629 PROC_INFO_INC(p_s_sb, search_by_key);
630
631
632
633
634
635 decrement_counters_in_path(p_s_search_path);
636
637 right_neighbor_of_leaf_node = 0;
638
639
640
641
642 n_block_number = SB_ROOT_BLOCK(p_s_sb);
643 expected_level = -1;
644 while (1) {
645
646#ifdef CONFIG_REISERFS_CHECK
647 if (!(++n_repeat_counter % 50000))
648 reiserfs_warning(p_s_sb, "PAP-5100: search_by_key: %s:"
649 "there were %d iterations of while loop "
650 "looking for key %K",
651 current->comm, n_repeat_counter,
652 p_s_key);
653#endif
654
655
656 p_s_last_element =
657 PATH_OFFSET_PELEMENT(p_s_search_path,
658 ++p_s_search_path->path_length);
659 fs_gen = get_generation(p_s_sb);
660
661
662
663 if ((p_s_bh = p_s_last_element->pe_buffer =
664 sb_getblk(p_s_sb, n_block_number))) {
665 if (!buffer_uptodate(p_s_bh) && reada_count > 1) {
666 search_by_key_reada(p_s_sb, reada_bh,
667 reada_blocks, reada_count);
668 }
669 ll_rw_block(READ, 1, &p_s_bh);
670 wait_on_buffer(p_s_bh);
671 if (!buffer_uptodate(p_s_bh))
672 goto io_error;
673 } else {
674 io_error:
675 p_s_search_path->path_length--;
676 pathrelse(p_s_search_path);
677 return IO_ERROR;
678 }
679 reada_count = 0;
680 if (expected_level == -1)
681 expected_level = SB_TREE_HEIGHT(p_s_sb);
682 expected_level--;
683
684
685
686
687 if (fs_changed(fs_gen, p_s_sb) &&
688 (!B_IS_IN_TREE(p_s_bh) ||
689 B_LEVEL(p_s_bh) != expected_level ||
690 !key_in_buffer(p_s_search_path, p_s_key, p_s_sb))) {
691 PROC_INFO_INC(p_s_sb, search_by_key_fs_changed);
692 PROC_INFO_INC(p_s_sb, search_by_key_restarted);
693 PROC_INFO_INC(p_s_sb,
694 sbk_restarted[expected_level - 1]);
695 decrement_counters_in_path(p_s_search_path);
696
697
698
699 n_block_number = SB_ROOT_BLOCK(p_s_sb);
700 expected_level = -1;
701 right_neighbor_of_leaf_node = 0;
702
703
704 continue;
705 }
706
707
708
709
710 RFALSE(comp_keys(&MAX_KEY, p_s_key) &&
711 !key_in_buffer(p_s_search_path, p_s_key, p_s_sb),
712 "PAP-5130: key is not in the buffer");
713#ifdef CONFIG_REISERFS_CHECK
714 if (cur_tb) {
715 print_cur_tb("5140");
716 reiserfs_panic(p_s_sb,
717 "PAP-5140: search_by_key: schedule occurred in do_balance!");
718 }
719#endif
720
721
722
723 if (!is_tree_node(p_s_bh, expected_level)) {
724 reiserfs_warning(p_s_sb, "vs-5150: search_by_key: "
725 "invalid format found in block %ld. Fsck?",
726 p_s_bh->b_blocknr);
727 pathrelse(p_s_search_path);
728 return IO_ERROR;
729 }
730
731
732 n_node_level = B_LEVEL(p_s_bh);
733
734 PROC_INFO_BH_STAT(p_s_sb, p_s_bh, n_node_level - 1);
735
736 RFALSE(n_node_level < n_stop_level,
737 "vs-5152: tree level (%d) is less than stop level (%d)",
738 n_node_level, n_stop_level);
739
740 n_retval = bin_search(p_s_key, B_N_PITEM_HEAD(p_s_bh, 0),
741 B_NR_ITEMS(p_s_bh),
742 (n_node_level ==
743 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
744 KEY_SIZE,
745 &(p_s_last_element->pe_position));
746 if (n_node_level == n_stop_level) {
747 return n_retval;
748 }
749
750
751 if (n_retval == ITEM_FOUND)
752
753 p_s_last_element->pe_position++;
754
755
756
757
758
759
760
761
762 n_block_number =
763 B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position);
764
765
766 if ((p_s_search_path->reada & PATH_READA) &&
767 n_node_level == DISK_LEAF_NODE_LEVEL + 1) {
768 int pos = p_s_last_element->pe_position;
769 int limit = B_NR_ITEMS(p_s_bh);
770 struct reiserfs_key *le_key;
771
772 if (p_s_search_path->reada & PATH_READA_BACK)
773 limit = 0;
774 while (reada_count < SEARCH_BY_KEY_READA) {
775 if (pos == limit)
776 break;
777 reada_blocks[reada_count++] =
778 B_N_CHILD_NUM(p_s_bh, pos);
779 if (p_s_search_path->reada & PATH_READA_BACK)
780 pos--;
781 else
782 pos++;
783
784
785
786
787 le_key = B_N_PDELIM_KEY(p_s_bh, pos);
788 if (le32_to_cpu(le_key->k_objectid) !=
789 p_s_key->on_disk_key.k_objectid) {
790 break;
791 }
792 }
793 }
794 }
795}
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813int search_for_position_by_key(struct super_block *p_s_sb,
814 const struct cpu_key *p_cpu_key,
815 struct treepath *p_s_search_path
816 )
817{
818 struct item_head *p_le_ih;
819 int n_blk_size;
820 loff_t item_offset, offset;
821 struct reiserfs_dir_entry de;
822 int retval;
823
824
825 if (is_direntry_cpu_key(p_cpu_key))
826 return search_by_entry_key(p_s_sb, p_cpu_key, p_s_search_path,
827 &de);
828
829
830
831
832 retval = search_item(p_s_sb, p_cpu_key, p_s_search_path);
833 if (retval == IO_ERROR)
834 return retval;
835 if (retval == ITEM_FOUND) {
836
837 RFALSE(!ih_item_len
838 (B_N_PITEM_HEAD
839 (PATH_PLAST_BUFFER(p_s_search_path),
840 PATH_LAST_POSITION(p_s_search_path))),
841 "PAP-5165: item length equals zero");
842
843 pos_in_item(p_s_search_path) = 0;
844 return POSITION_FOUND;
845 }
846
847 RFALSE(!PATH_LAST_POSITION(p_s_search_path),
848 "PAP-5170: position equals zero");
849
850
851 p_le_ih =
852 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path),
853 --PATH_LAST_POSITION(p_s_search_path));
854 n_blk_size = p_s_sb->s_blocksize;
855
856 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
857 return FILE_NOT_FOUND;
858 }
859
860
861 item_offset = le_ih_k_offset(p_le_ih);
862 offset = cpu_key_k_offset(p_cpu_key);
863
864
865 if (item_offset <= offset &&
866 item_offset + op_bytes_number(p_le_ih, n_blk_size) > offset) {
867 pos_in_item(p_s_search_path) = offset - item_offset;
868 if (is_indirect_le_ih(p_le_ih)) {
869 pos_in_item(p_s_search_path) /= n_blk_size;
870 }
871 return POSITION_FOUND;
872 }
873
874
875
876 if (is_indirect_le_ih(p_le_ih))
877 pos_in_item(p_s_search_path) =
878 ih_item_len(p_le_ih) / UNFM_P_SIZE;
879 else
880 pos_in_item(p_s_search_path) = ih_item_len(p_le_ih);
881
882 return POSITION_NOT_FOUND;
883}
884
885
886int comp_items(const struct item_head *stored_ih, const struct treepath *p_s_path)
887{
888 struct buffer_head *p_s_bh;
889 struct item_head *ih;
890
891
892 if (!B_IS_IN_TREE(p_s_bh = PATH_PLAST_BUFFER(p_s_path)))
893 return 1;
894
895
896 if (PATH_LAST_POSITION(p_s_path) >= B_NR_ITEMS(p_s_bh))
897 return 1;
898
899
900 ih = get_ih(p_s_path);
901 return memcmp(stored_ih, ih, IH_SIZE);
902}
903
904
905
906
907#define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
908
909
910#define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
911
912
913static inline int prepare_for_direct_item(struct treepath *path,
914 struct item_head *le_ih,
915 struct inode *inode,
916 loff_t new_file_length, int *cut_size)
917{
918 loff_t round_len;
919
920 if (new_file_length == max_reiserfs_offset(inode)) {
921
922 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
923 return M_DELETE;
924 }
925
926 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
927
928 round_len = ROUND_UP(new_file_length);
929
930 if (round_len < le_ih_k_offset(le_ih)) {
931 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
932 return M_DELETE;
933 }
934
935 pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1);
936 *cut_size = -(ih_item_len(le_ih) - pos_in_item(path));
937
938 return M_CUT;
939 }
940
941
942
943 if (new_file_length < le_ih_k_offset(le_ih)) {
944 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
945 return M_DELETE;
946 }
947
948 *cut_size = -(ih_item_len(le_ih) -
949 (pos_in_item(path) =
950 new_file_length + 1 - le_ih_k_offset(le_ih)));
951 return M_CUT;
952}
953
954static inline int prepare_for_direntry_item(struct treepath *path,
955 struct item_head *le_ih,
956 struct inode *inode,
957 loff_t new_file_length,
958 int *cut_size)
959{
960 if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
961 new_file_length == max_reiserfs_offset(inode)) {
962 RFALSE(ih_entry_count(le_ih) != 2,
963 "PAP-5220: incorrect empty directory item (%h)", le_ih);
964 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
965 return M_DELETE;
966 }
967
968 if (ih_entry_count(le_ih) == 1) {
969
970
971 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
972 return M_DELETE;
973 }
974
975
976 *cut_size =
977 -(DEH_SIZE +
978 entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
979 return M_CUT;
980}
981
982#define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
983
984
985
986
987
988
989static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *p_s_path, const struct cpu_key *p_s_item_key, int *p_n_removed,
990
991 int *p_n_cut_size, unsigned long long n_new_file_length
992 )
993{
994 struct super_block *p_s_sb = inode->i_sb;
995 struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_path);
996 struct buffer_head *p_s_bh = PATH_PLAST_BUFFER(p_s_path);
997
998 BUG_ON(!th->t_trans_id);
999
1000
1001 if (is_statdata_le_ih(p_le_ih)) {
1002
1003 RFALSE(n_new_file_length != max_reiserfs_offset(inode),
1004 "PAP-5210: mode must be M_DELETE");
1005
1006 *p_n_cut_size = -(IH_SIZE + ih_item_len(p_le_ih));
1007 return M_DELETE;
1008 }
1009
1010
1011 if (is_direntry_le_ih(p_le_ih))
1012 return prepare_for_direntry_item(p_s_path, p_le_ih, inode,
1013 n_new_file_length,
1014 p_n_cut_size);
1015
1016
1017 if (is_direct_le_ih(p_le_ih))
1018 return prepare_for_direct_item(p_s_path, p_le_ih, inode,
1019 n_new_file_length, p_n_cut_size);
1020
1021
1022 {
1023 int blk_size = p_s_sb->s_blocksize;
1024 struct item_head s_ih;
1025 int need_re_search;
1026 int delete = 0;
1027 int result = M_CUT;
1028 int pos = 0;
1029
1030 if ( n_new_file_length == max_reiserfs_offset (inode) ) {
1031
1032
1033 n_new_file_length = 0;
1034 delete = 1;
1035 }
1036
1037 do {
1038 need_re_search = 0;
1039 *p_n_cut_size = 0;
1040 p_s_bh = PATH_PLAST_BUFFER(p_s_path);
1041 copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
1042 pos = I_UNFM_NUM(&s_ih);
1043
1044 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > n_new_file_length) {
1045 __le32 *unfm;
1046 __u32 block;
1047
1048
1049
1050
1051 if (!delete && (*p_n_cut_size) != 0 &&
1052 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
1053 break;
1054 }
1055
1056 unfm = (__le32 *)B_I_PITEM(p_s_bh, &s_ih) + pos - 1;
1057 block = get_block_num(unfm, 0);
1058
1059 if (block != 0) {
1060 reiserfs_prepare_for_journal(p_s_sb, p_s_bh, 1);
1061 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 }
1065
1066 cond_resched();
1067
1068 if (item_moved (&s_ih, p_s_path)) {
1069 need_re_search = 1;
1070 break;
1071 }
1072
1073 pos --;
1074 (*p_n_removed) ++;
1075 (*p_n_cut_size) -= UNFM_P_SIZE;
1076
1077 if (pos == 0) {
1078 (*p_n_cut_size) -= IH_SIZE;
1079 result = M_DELETE;
1080 break;
1081 }
1082 }
1083
1084
1085
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;
1090
1091 if (*p_n_cut_size == 0) {
1092
1093
1094 result = M_CONVERT;
1095 }
1096 return result;
1097 }
1098}
1099
1100
1101static int calc_deleted_bytes_number(struct tree_balance *p_s_tb, char c_mode)
1102{
1103 int n_del_size;
1104 struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_tb->tb_path);
1105
1106 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
1114
1115
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);
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 treepath *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;
1148}
1149
1150#ifdef REISERQUOTA_DEBUG
1151char key2type(struct reiserfs_key *ih)
1152{
1153 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';
1162}
1163
1164char head2type(struct item_head *ih)
1165{
1166 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';
1175}
1176#endif
1177
1178
1179int reiserfs_delete_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path,
1180 const struct cpu_key *p_s_item_key,
1181 struct inode *p_s_inode,
1182 struct buffer_head *p_s_un_bh)
1183{
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;
1190
1191#ifdef CONFIG_REISERFS_CHECK
1192 char c_mode;
1193 int n_iter = 0;
1194#endif
1195
1196 BUG_ON(!th->t_trans_id);
1197
1198 init_tb_struct(th, &s_del_balance, p_s_sb, p_s_path,
1199 0 );
1200
1201 while (1) {
1202 n_removed = 0;
1203
1204#ifdef CONFIG_REISERFS_CHECK
1205 n_iter++;
1206 c_mode =
1207#endif
1208 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));
1212
1213 RFALSE(c_mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
1214
1215 copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path));
1216 s_del_balance.insert_size[0] = n_del_size;
1217
1218 n_ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1219 if (n_ret_value != REPEAT_SEARCH)
1220 break;
1221
1222 PROC_INFO_INC(p_s_sb, delete_item_restarted);
1223
1224
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 }
1237
1238 if (n_ret_value != CARRY_ON) {
1239 unfix_nodes(&s_del_balance);
1240 return 0;
1241 }
1242
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);
1246
1247
1248
1249
1250
1251
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 }
1259 }
1260
1261 if (p_s_un_bh) {
1262 int off;
1263 char *data;
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285 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
1293 do_balance(&s_del_balance, NULL, NULL, M_DELETE);
1294
1295#ifdef REISERQUOTA_DEBUG
1296 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));
1299#endif
1300 DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
1301
1302
1303 return n_ret_value;
1304}
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1324 struct inode *inode, struct reiserfs_key *key)
1325{
1326 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;
1333
1334 BUG_ON(!th->t_trans_id);
1335
1336 le_key2cpu_key(&cpu_key, key);
1337
1338 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
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) {
1380#ifdef REISERQUOTA_DEBUG
1381 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));
1385#endif
1386 DQUOT_FREE_SPACE_NODIRTY(inode,
1387 quota_cut_bytes);
1388 }
1389 break;
1390 }
1391
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;
1398 }
1399
1400 reiserfs_check_path(&path);
1401}
1402
1403int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1404 struct inode *inode)
1405{
1406 int err;
1407 inode->i_size = 0;
1408 BUG_ON(!th->t_trans_id);
1409
1410
1411 err =
1412 reiserfs_do_truncate(th, inode, NULL, 0 );
1413 if (err)
1414 return err;
1415
1416#if defined( USE_INODE_GENERATION_COUNTER )
1417 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 }
1425
1426#endif
1427 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1428
1429 return err;
1430}
1431
1432static 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;
1439
1440 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;
1448
1449
1450
1451
1452
1453
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 }
1462 }
1463}
1464
1465static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th,
1466 struct inode *p_s_inode,
1467 struct page *page,
1468 struct treepath *p_s_path,
1469 const struct cpu_key *p_s_item_key,
1470 loff_t n_new_file_size, char *p_c_mode)
1471{
1472 struct super_block *p_s_sb = p_s_inode->i_sb;
1473 int n_block_size = p_s_sb->s_blocksize;
1474 int cut_bytes;
1475 BUG_ON(!th->t_trans_id);
1476 BUG_ON(n_new_file_size != p_s_inode->i_size);
1477
1478
1479
1480
1481
1482 if (atomic_read(&p_s_inode->i_count) > 1 ||
1483 !tail_has_to_be_packed(p_s_inode) ||
1484 !page || (REISERFS_I(p_s_inode)->i_flags & i_nopack_mask)) {
1485
1486 *p_c_mode = M_SKIP_BALANCING;
1487 cut_bytes =
1488 n_block_size - (n_new_file_size & (n_block_size - 1));
1489 pathrelse(p_s_path);
1490 return cut_bytes;
1491 }
1492
1493
1494 return indirect2direct(th, p_s_inode, page, p_s_path, p_s_item_key,
1495 n_new_file_size, p_c_mode);
1496}
1497
1498
1499
1500
1501
1502static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1503 struct inode *inode, struct treepath *path)
1504{
1505 struct cpu_key tail_key;
1506 int tail_len;
1507 int removed;
1508 BUG_ON(!th->t_trans_id);
1509
1510 make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4);
1511 tail_key.key_length = 4;
1512
1513 tail_len =
1514 (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
1515 while (tail_len) {
1516
1517 if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1518 POSITION_NOT_FOUND)
1519 reiserfs_panic(inode->i_sb,
1520 "vs-5615: indirect_to_direct_roll_back: found invalid item");
1521 RFALSE(path->pos_in_item !=
1522 ih_item_len(PATH_PITEM_HEAD(path)) - 1,
1523 "vs-5616: appended bytes found");
1524 PATH_LAST_POSITION(path)--;
1525
1526 removed =
1527 reiserfs_delete_item(th, path, &tail_key, inode,
1528 NULL );
1529 RFALSE(removed <= 0
1530 || removed > tail_len,
1531 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1532 tail_len, removed);
1533 tail_len -= removed;
1534 set_cpu_key_k_offset(&tail_key,
1535 cpu_key_k_offset(&tail_key) - removed);
1536 }
1537 reiserfs_warning(inode->i_sb,
1538 "indirect_to_direct_roll_back: indirect_to_direct conversion has been rolled back due to lack of disk space");
1539
1540 mark_inode_dirty(inode);
1541}
1542
1543
1544int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1545 struct treepath *p_s_path,
1546 struct cpu_key *p_s_item_key,
1547 struct inode *p_s_inode,
1548 struct page *page, loff_t n_new_file_size)
1549{
1550 struct super_block *p_s_sb = p_s_inode->i_sb;
1551
1552
1553
1554
1555 struct tree_balance s_cut_balance;
1556 struct item_head *p_le_ih;
1557 int n_cut_size = 0,
1558 n_ret_value = CARRY_ON, n_removed = 0,
1559 n_is_inode_locked = 0;
1560 char c_mode;
1561 int retval2 = -1;
1562 int quota_cut_bytes;
1563 loff_t tail_pos = 0;
1564
1565 BUG_ON(!th->t_trans_id);
1566
1567 init_tb_struct(th, &s_cut_balance, p_s_inode->i_sb, p_s_path,
1568 n_cut_size);
1569
1570
1571
1572 while (1) {
1573
1574
1575
1576
1577
1578 c_mode =
1579 prepare_for_delete_or_cut(th, p_s_inode, p_s_path,
1580 p_s_item_key, &n_removed,
1581 &n_cut_size, n_new_file_size);
1582 if (c_mode == M_CONVERT) {
1583
1584
1585 RFALSE(n_ret_value != CARRY_ON,
1586 "PAP-5570: can not convert twice");
1587
1588 n_ret_value =
1589 maybe_indirect_to_direct(th, p_s_inode, page,
1590 p_s_path, p_s_item_key,
1591 n_new_file_size, &c_mode);
1592 if (c_mode == M_SKIP_BALANCING)
1593
1594 return n_ret_value;
1595
1596 n_is_inode_locked = 1;
1597
1598
1599
1600 retval2 = n_ret_value;
1601
1602
1603
1604
1605
1606
1607 set_cpu_key_k_type(p_s_item_key, TYPE_INDIRECT);
1608 p_s_item_key->key_length = 4;
1609 n_new_file_size -=
1610 (n_new_file_size & (p_s_sb->s_blocksize - 1));
1611 tail_pos = n_new_file_size;
1612 set_cpu_key_k_offset(p_s_item_key, n_new_file_size + 1);
1613 if (search_for_position_by_key
1614 (p_s_sb, p_s_item_key,
1615 p_s_path) == POSITION_NOT_FOUND) {
1616 print_block(PATH_PLAST_BUFFER(p_s_path), 3,
1617 PATH_LAST_POSITION(p_s_path) - 1,
1618 PATH_LAST_POSITION(p_s_path) + 1);
1619 reiserfs_panic(p_s_sb,
1620 "PAP-5580: reiserfs_cut_from_item: item to convert does not exist (%K)",
1621 p_s_item_key);
1622 }
1623 continue;
1624 }
1625 if (n_cut_size == 0) {
1626 pathrelse(p_s_path);
1627 return 0;
1628 }
1629
1630 s_cut_balance.insert_size[0] = n_cut_size;
1631
1632 n_ret_value = fix_nodes(c_mode, &s_cut_balance, NULL, NULL);
1633 if (n_ret_value != REPEAT_SEARCH)
1634 break;
1635
1636 PROC_INFO_INC(p_s_sb, cut_from_item_restarted);
1637
1638 n_ret_value =
1639 search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path);
1640 if (n_ret_value == POSITION_FOUND)
1641 continue;
1642
1643 reiserfs_warning(p_s_sb,
1644 "PAP-5610: reiserfs_cut_from_item: item %K not found",
1645 p_s_item_key);
1646 unfix_nodes(&s_cut_balance);
1647 return (n_ret_value == IO_ERROR) ? -EIO : -ENOENT;
1648 }
1649
1650
1651 if (n_ret_value != CARRY_ON) {
1652 if (n_is_inode_locked) {
1653
1654
1655 indirect_to_direct_roll_back(th, p_s_inode, p_s_path);
1656 }
1657 if (n_ret_value == NO_DISK_SPACE)
1658 reiserfs_warning(p_s_sb, "NO_DISK_SPACE");
1659 unfix_nodes(&s_cut_balance);
1660 return -EIO;
1661 }
1662
1663
1664
1665 RFALSE(c_mode == M_PASTE || c_mode == M_INSERT, "invalid mode");
1666
1667
1668 quota_cut_bytes =
1669 (c_mode ==
1670 M_DELETE) ? ih_item_len(get_ih(p_s_path)) : -s_cut_balance.
1671 insert_size[0];
1672 if (retval2 == -1)
1673 n_ret_value = calc_deleted_bytes_number(&s_cut_balance, c_mode);
1674 else
1675 n_ret_value = retval2;
1676
1677
1678
1679
1680 p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path);
1681 if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(p_le_ih)) {
1682 if (c_mode == M_DELETE &&
1683 (le_ih_k_offset(p_le_ih) & (p_s_sb->s_blocksize - 1)) ==
1684 1) {
1685
1686 REISERFS_I(p_s_inode)->i_first_direct_byte = U32_MAX;
1687 quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE;
1688 } else {
1689 quota_cut_bytes = 0;
1690 }
1691 }
1692#ifdef CONFIG_REISERFS_CHECK
1693 if (n_is_inode_locked) {
1694 struct item_head *le_ih =
1695 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1696
1697
1698
1699 if (!is_indirect_le_ih(le_ih))
1700 reiserfs_panic(p_s_sb,
1701 "vs-5652: reiserfs_cut_from_item: "
1702 "item must be indirect %h", le_ih);
1703
1704 if (c_mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
1705 reiserfs_panic(p_s_sb,
1706 "vs-5653: reiserfs_cut_from_item: "
1707 "completing indirect2direct conversion indirect item %h "
1708 "being deleted must be of 4 byte long",
1709 le_ih);
1710
1711 if (c_mode == M_CUT
1712 && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) {
1713 reiserfs_panic(p_s_sb,
1714 "vs-5654: reiserfs_cut_from_item: "
1715 "can not complete indirect2direct conversion of %h (CUT, insert_size==%d)",
1716 le_ih, s_cut_balance.insert_size[0]);
1717 }
1718
1719
1720 }
1721#endif
1722
1723 do_balance(&s_cut_balance, NULL, NULL, c_mode);
1724 if (n_is_inode_locked) {
1725
1726
1727
1728
1729
1730 unmap_buffers(page, tail_pos);
1731 REISERFS_I(p_s_inode)->i_flags &= ~i_pack_on_close_mask;
1732 }
1733#ifdef REISERQUOTA_DEBUG
1734 reiserfs_debug(p_s_inode->i_sb, REISERFS_DEBUG_CODE,
1735 "reiserquota cut_from_item(): freeing %u id=%u type=%c",
1736 quota_cut_bytes, p_s_inode->i_uid, '?');
1737#endif
1738 DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes);
1739 return n_ret_value;
1740}
1741
1742static void truncate_directory(struct reiserfs_transaction_handle *th,
1743 struct inode *inode)
1744{
1745 BUG_ON(!th->t_trans_id);
1746 if (inode->i_nlink)
1747 reiserfs_warning(inode->i_sb,
1748 "vs-5655: truncate_directory: link count != 0");
1749
1750 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
1751 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
1752 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1753 reiserfs_update_sd(th, inode);
1754 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
1755 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
1756}
1757
1758
1759
1760int reiserfs_do_truncate(struct reiserfs_transaction_handle *th, struct inode *p_s_inode,
1761
1762 struct page *page,
1763 int update_timestamps
1764
1765
1766
1767 )
1768{
1769 INITIALIZE_PATH(s_search_path);
1770 struct item_head *p_le_ih;
1771 struct cpu_key s_item_key;
1772 loff_t n_file_size,
1773 n_new_file_size;
1774 int n_deleted;
1775 int retval;
1776 int err = 0;
1777
1778 BUG_ON(!th->t_trans_id);
1779 if (!
1780 (S_ISREG(p_s_inode->i_mode) || S_ISDIR(p_s_inode->i_mode)
1781 || S_ISLNK(p_s_inode->i_mode)))
1782 return 0;
1783
1784 if (S_ISDIR(p_s_inode->i_mode)) {
1785
1786 truncate_directory(th, p_s_inode);
1787 return 0;
1788 }
1789
1790
1791 n_new_file_size = p_s_inode->i_size;
1792
1793
1794 make_cpu_key(&s_item_key, p_s_inode, max_reiserfs_offset(p_s_inode),
1795 TYPE_DIRECT, 3);
1796
1797 retval =
1798 search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
1799 &s_search_path);
1800 if (retval == IO_ERROR) {
1801 reiserfs_warning(p_s_inode->i_sb,
1802 "vs-5657: reiserfs_do_truncate: "
1803 "i/o failure occurred trying to truncate %K",
1804 &s_item_key);
1805 err = -EIO;
1806 goto out;
1807 }
1808 if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
1809 reiserfs_warning(p_s_inode->i_sb,
1810 "PAP-5660: reiserfs_do_truncate: "
1811 "wrong result %d of search for %K", retval,
1812 &s_item_key);
1813
1814 err = -EIO;
1815 goto out;
1816 }
1817
1818 s_search_path.pos_in_item--;
1819
1820
1821 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1822 if (is_statdata_le_ih(p_le_ih))
1823 n_file_size = 0;
1824 else {
1825 loff_t offset = le_ih_k_offset(p_le_ih);
1826 int bytes =
1827 op_bytes_number(p_le_ih, p_s_inode->i_sb->s_blocksize);
1828
1829
1830
1831
1832 n_file_size = offset + bytes - 1;
1833 }
1834
1835
1836
1837
1838 if (n_new_file_size == 0)
1839 s_search_path.reada = PATH_READA | PATH_READA_BACK;
1840
1841 if (n_file_size == 0 || n_file_size < n_new_file_size) {
1842 goto update_and_out;
1843 }
1844
1845
1846 set_cpu_key_k_offset(&s_item_key, n_file_size);
1847
1848 do {
1849
1850 n_deleted =
1851 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1852 p_s_inode, page, n_new_file_size);
1853 if (n_deleted < 0) {
1854 reiserfs_warning(p_s_inode->i_sb,
1855 "vs-5665: reiserfs_do_truncate: reiserfs_cut_from_item failed");
1856 reiserfs_check_path(&s_search_path);
1857 return 0;
1858 }
1859
1860 RFALSE(n_deleted > n_file_size,
1861 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1862 n_deleted, n_file_size, &s_item_key);
1863
1864
1865 n_file_size -= n_deleted;
1866
1867 set_cpu_key_k_offset(&s_item_key, n_file_size);
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 if (journal_transaction_should_end(th, 0) ||
1879 reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
1880 int orig_len_alloc = th->t_blocks_allocated;
1881 decrement_counters_in_path(&s_search_path);
1882
1883 if (update_timestamps) {
1884 p_s_inode->i_mtime = p_s_inode->i_ctime =
1885 CURRENT_TIME_SEC;
1886 }
1887 reiserfs_update_sd(th, p_s_inode);
1888
1889 err = journal_end(th, p_s_inode->i_sb, orig_len_alloc);
1890 if (err)
1891 goto out;
1892 err = journal_begin(th, p_s_inode->i_sb,
1893 JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ;
1894 if (err)
1895 goto out;
1896 reiserfs_update_inode_transaction(p_s_inode);
1897 }
1898 } while (n_file_size > ROUND_UP(n_new_file_size) &&
1899 search_for_position_by_key(p_s_inode->i_sb, &s_item_key,
1900 &s_search_path) == POSITION_FOUND);
1901
1902 RFALSE(n_file_size > ROUND_UP(n_new_file_size),
1903 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1904 n_new_file_size, n_file_size, s_item_key.on_disk_key.k_objectid);
1905
1906 update_and_out:
1907 if (update_timestamps) {
1908
1909 p_s_inode->i_mtime = p_s_inode->i_ctime = CURRENT_TIME_SEC;
1910 }
1911 reiserfs_update_sd(th, p_s_inode);
1912
1913 out:
1914 pathrelse(&s_search_path);
1915 return err;
1916}
1917
1918#ifdef CONFIG_REISERFS_CHECK
1919
1920static void check_research_for_paste(struct treepath *path,
1921 const struct cpu_key *p_s_key)
1922{
1923 struct item_head *found_ih = get_ih(path);
1924
1925 if (is_direct_le_ih(found_ih)) {
1926 if (le_ih_k_offset(found_ih) +
1927 op_bytes_number(found_ih,
1928 get_last_bh(path)->b_size) !=
1929 cpu_key_k_offset(p_s_key)
1930 || op_bytes_number(found_ih,
1931 get_last_bh(path)->b_size) !=
1932 pos_in_item(path))
1933 reiserfs_panic(NULL,
1934 "PAP-5720: check_research_for_paste: "
1935 "found direct item %h or position (%d) does not match to key %K",
1936 found_ih, pos_in_item(path), p_s_key);
1937 }
1938 if (is_indirect_le_ih(found_ih)) {
1939 if (le_ih_k_offset(found_ih) +
1940 op_bytes_number(found_ih,
1941 get_last_bh(path)->b_size) !=
1942 cpu_key_k_offset(p_s_key)
1943 || I_UNFM_NUM(found_ih) != pos_in_item(path)
1944 || get_ih_free_space(found_ih) != 0)
1945 reiserfs_panic(NULL,
1946 "PAP-5730: check_research_for_paste: "
1947 "found indirect item (%h) or position (%d) does not match to key (%K)",
1948 found_ih, pos_in_item(path), p_s_key);
1949 }
1950}
1951#endif
1952
1953
1954int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_search_path,
1955 const struct cpu_key *p_s_key,
1956 struct inode *inode,
1957 const char *p_c_body,
1958 int n_pasted_size)
1959{
1960 struct tree_balance s_paste_balance;
1961 int retval;
1962 int fs_gen;
1963
1964 BUG_ON(!th->t_trans_id);
1965
1966 fs_gen = get_generation(inode->i_sb);
1967
1968#ifdef REISERQUOTA_DEBUG
1969 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
1970 "reiserquota paste_into_item(): allocating %u id=%u type=%c",
1971 n_pasted_size, inode->i_uid,
1972 key2type(&(p_s_key->on_disk_key)));
1973#endif
1974
1975 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, n_pasted_size)) {
1976 pathrelse(p_s_search_path);
1977 return -EDQUOT;
1978 }
1979 init_tb_struct(th, &s_paste_balance, th->t_super, p_s_search_path,
1980 n_pasted_size);
1981#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1982 s_paste_balance.key = p_s_key->on_disk_key;
1983#endif
1984
1985
1986 if (fs_changed(fs_gen, inode->i_sb)) {
1987 goto search_again;
1988 }
1989
1990 while ((retval =
1991 fix_nodes(M_PASTE, &s_paste_balance, NULL,
1992 p_c_body)) == REPEAT_SEARCH) {
1993 search_again:
1994
1995 PROC_INFO_INC(th->t_super, paste_into_item_restarted);
1996 retval =
1997 search_for_position_by_key(th->t_super, p_s_key,
1998 p_s_search_path);
1999 if (retval == IO_ERROR) {
2000 retval = -EIO;
2001 goto error_out;
2002 }
2003 if (retval == POSITION_FOUND) {
2004 reiserfs_warning(inode->i_sb,
2005 "PAP-5710: reiserfs_paste_into_item: entry or pasted byte (%K) exists",
2006 p_s_key);
2007 retval = -EEXIST;
2008 goto error_out;
2009 }
2010#ifdef CONFIG_REISERFS_CHECK
2011 check_research_for_paste(p_s_search_path, p_s_key);
2012#endif
2013 }
2014
2015
2016
2017 if (retval == CARRY_ON) {
2018 do_balance(&s_paste_balance, NULL , p_c_body, M_PASTE);
2019 return 0;
2020 }
2021 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2022 error_out:
2023
2024 unfix_nodes(&s_paste_balance);
2025#ifdef REISERQUOTA_DEBUG
2026 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2027 "reiserquota paste_into_item(): freeing %u id=%u type=%c",
2028 n_pasted_size, inode->i_uid,
2029 key2type(&(p_s_key->on_disk_key)));
2030#endif
2031 DQUOT_FREE_SPACE_NODIRTY(inode, n_pasted_size);
2032 return retval;
2033}
2034
2035
2036int reiserfs_insert_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path,
2037 const struct cpu_key *key, struct item_head *p_s_ih,
2038 struct inode *inode, const char *p_c_body)
2039{
2040 struct tree_balance s_ins_balance;
2041 int retval;
2042 int fs_gen = 0;
2043 int quota_bytes = 0;
2044
2045 BUG_ON(!th->t_trans_id);
2046
2047 if (inode) {
2048 fs_gen = get_generation(inode->i_sb);
2049 quota_bytes = ih_item_len(p_s_ih);
2050
2051
2052
2053
2054 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_s_ih)) {
2055 quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
2056 }
2057#ifdef REISERQUOTA_DEBUG
2058 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2059 "reiserquota insert_item(): allocating %u id=%u type=%c",
2060 quota_bytes, inode->i_uid, head2type(p_s_ih));
2061#endif
2062
2063
2064 if (DQUOT_ALLOC_SPACE_NODIRTY(inode, quota_bytes)) {
2065 pathrelse(p_s_path);
2066 return -EDQUOT;
2067 }
2068 }
2069 init_tb_struct(th, &s_ins_balance, th->t_super, p_s_path,
2070 IH_SIZE + ih_item_len(p_s_ih));
2071#ifdef DISPLACE_NEW_PACKING_LOCALITIES
2072 s_ins_balance.key = key->on_disk_key;
2073#endif
2074
2075 if (inode && fs_changed(fs_gen, inode->i_sb)) {
2076 goto search_again;
2077 }
2078
2079 while ((retval =
2080 fix_nodes(M_INSERT, &s_ins_balance, p_s_ih,
2081 p_c_body)) == REPEAT_SEARCH) {
2082 search_again:
2083
2084 PROC_INFO_INC(th->t_super, insert_item_restarted);
2085 retval = search_item(th->t_super, key, p_s_path);
2086 if (retval == IO_ERROR) {
2087 retval = -EIO;
2088 goto error_out;
2089 }
2090 if (retval == ITEM_FOUND) {
2091 reiserfs_warning(th->t_super,
2092 "PAP-5760: reiserfs_insert_item: "
2093 "key %K already exists in the tree",
2094 key);
2095 retval = -EEXIST;
2096 goto error_out;
2097 }
2098 }
2099
2100
2101 if (retval == CARRY_ON) {
2102 do_balance(&s_ins_balance, p_s_ih, p_c_body, M_INSERT);
2103 return 0;
2104 }
2105
2106 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2107 error_out:
2108
2109 unfix_nodes(&s_ins_balance);
2110#ifdef REISERQUOTA_DEBUG
2111 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
2112 "reiserquota insert_item(): freeing %u id=%u type=%c",
2113 quota_bytes, inode->i_uid, head2type(p_s_ih));
2114#endif
2115 if (inode)
2116 DQUOT_FREE_SPACE_NODIRTY(inode, quota_bytes);
2117 return retval;
2118}
2119