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#include <linux/time.h>
52#include <linux/string.h>
53#include <linux/pagemap.h>
54#include <linux/reiserfs_fs.h>
55#include <linux/buffer_head.h>
56#include <linux/quotaops.h>
57
58
59inline int B_IS_IN_TREE(const struct buffer_head *bh)
60{
61
62 RFALSE(B_LEVEL(bh) > MAX_HEIGHT,
63 "PAP-1010: block (%b) has too big level (%z)", bh, bh);
64
65 return (B_LEVEL(bh) != FREE_LEVEL);
66}
67
68
69
70
71inline void copy_item_head(struct item_head *to,
72 const struct item_head *from)
73{
74 memcpy(to, from, IH_SIZE);
75}
76
77
78
79
80
81
82
83inline int comp_short_keys(const struct reiserfs_key *le_key,
84 const struct cpu_key *cpu_key)
85{
86 __u32 n;
87 n = le32_to_cpu(le_key->k_dir_id);
88 if (n < cpu_key->on_disk_key.k_dir_id)
89 return -1;
90 if (n > cpu_key->on_disk_key.k_dir_id)
91 return 1;
92 n = le32_to_cpu(le_key->k_objectid);
93 if (n < cpu_key->on_disk_key.k_objectid)
94 return -1;
95 if (n > cpu_key->on_disk_key.k_objectid)
96 return 1;
97 return 0;
98}
99
100
101
102
103
104
105static inline int comp_keys(const struct reiserfs_key *le_key,
106 const struct cpu_key *cpu_key)
107{
108 int retval;
109
110 retval = comp_short_keys(le_key, cpu_key);
111 if (retval)
112 return retval;
113 if (le_key_k_offset(le_key_version(le_key), le_key) <
114 cpu_key_k_offset(cpu_key))
115 return -1;
116 if (le_key_k_offset(le_key_version(le_key), le_key) >
117 cpu_key_k_offset(cpu_key))
118 return 1;
119
120 if (cpu_key->key_length == 3)
121 return 0;
122
123
124 if (le_key_k_type(le_key_version(le_key), le_key) <
125 cpu_key_k_type(cpu_key))
126 return -1;
127
128 if (le_key_k_type(le_key_version(le_key), le_key) >
129 cpu_key_k_type(cpu_key))
130 return 1;
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 key_length = REISERFS_SHORT_KEY_LEN;
140
141 k1_u32 = (__u32 *) key1;
142 k2_u32 = (__u32 *) key2;
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
152inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from)
153{
154 int version;
155 to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id);
156 to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid);
157
158
159 version = le_key_version(from);
160 to->version = version;
161 to->on_disk_key.k_offset = le_key_k_offset(version, from);
162 to->on_disk_key.k_type = le_key_k_type(version, from);
163}
164
165
166
167inline int comp_le_keys(const struct reiserfs_key *k1,
168 const struct reiserfs_key *k2)
169{
170 return memcmp(k1, k2, sizeof(struct reiserfs_key));
171}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187static inline int bin_search(const void *key,
188 const void *base,
189 int num,
190 int width,
191
192
193
194
195
196
197
198 int *pos
199 )
200{
201 int rbound, lbound, j;
202
203 for (j = ((rbound = num - 1) + (lbound = 0)) / 2;
204 lbound <= rbound; j = (rbound + lbound) / 2)
205 switch (comp_keys
206 ((struct reiserfs_key *)((char *)base + j * width),
207 (struct cpu_key *)key)) {
208 case -1:
209 lbound = j + 1;
210 continue;
211 case 1:
212 rbound = j - 1;
213 continue;
214 case 0:
215 *pos = j;
216 return ITEM_FOUND;
217 }
218
219
220
221 *pos = lbound;
222 return ITEM_NOT_FOUND;
223}
224
225#ifdef CONFIG_REISERFS_CHECK
226extern struct tree_balance *cur_tb;
227#endif
228
229
230const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} };
231
232
233static const struct reiserfs_key MAX_KEY = {
234 __constant_cpu_to_le32(0xffffffff),
235 __constant_cpu_to_le32(0xffffffff),
236 {{__constant_cpu_to_le32(0xffffffff),
237 __constant_cpu_to_le32(0xffffffff)},}
238};
239
240
241
242
243
244static inline const struct reiserfs_key *get_lkey(const struct treepath *chk_path,
245 const struct super_block *sb)
246{
247 int position, path_offset = chk_path->path_length;
248 struct buffer_head *parent;
249
250 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
251 "PAP-5010: invalid offset in the path");
252
253
254 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
255
256 RFALSE(!buffer_uptodate
257 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
258 "PAP-5020: parent is not uptodate");
259
260
261 if (!B_IS_IN_TREE
262 (parent =
263 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
264 return &MAX_KEY;
265
266 if ((position =
267 PATH_OFFSET_POSITION(chk_path,
268 path_offset)) >
269 B_NR_ITEMS(parent))
270 return &MAX_KEY;
271
272 if (B_N_CHILD_NUM(parent, position) !=
273 PATH_OFFSET_PBUFFER(chk_path,
274 path_offset + 1)->b_blocknr)
275 return &MAX_KEY;
276
277 if (position)
278 return B_N_PDELIM_KEY(parent, position - 1);
279 }
280
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
288inline const struct reiserfs_key *get_rkey(const struct treepath *chk_path,
289 const struct super_block *sb)
290{
291 int position, path_offset = chk_path->path_length;
292 struct buffer_head *parent;
293
294 RFALSE(path_offset < FIRST_PATH_ELEMENT_OFFSET,
295 "PAP-5030: invalid offset in the path");
296
297 while (path_offset-- > FIRST_PATH_ELEMENT_OFFSET) {
298
299 RFALSE(!buffer_uptodate
300 (PATH_OFFSET_PBUFFER(chk_path, path_offset)),
301 "PAP-5040: parent is not uptodate");
302
303
304 if (!B_IS_IN_TREE
305 (parent =
306 PATH_OFFSET_PBUFFER(chk_path, path_offset)))
307 return &MIN_KEY;
308
309 if ((position =
310 PATH_OFFSET_POSITION(chk_path,
311 path_offset)) >
312 B_NR_ITEMS(parent))
313 return &MIN_KEY;
314
315 if (B_N_CHILD_NUM(parent, position) !=
316 PATH_OFFSET_PBUFFER(chk_path,
317 path_offset + 1)->b_blocknr)
318 return &MIN_KEY;
319
320 if (position != B_NR_ITEMS(parent))
321 return B_N_PDELIM_KEY(parent, position);
322 }
323
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
330
331
332
333
334
335static inline int key_in_buffer(struct treepath *chk_path,
336 const struct cpu_key *key,
337 struct super_block *sb
338 )
339{
340
341 RFALSE(!key || chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET
342 || chk_path->path_length > MAX_HEIGHT,
343 "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)",
344 key, chk_path->path_length);
345 RFALSE(!PATH_PLAST_BUFFER(chk_path)->b_bdev,
346 "PAP-5060: device must not be NODEV");
347
348 if (comp_keys(get_lkey(chk_path, sb), key) == 1)
349
350 return 0;
351
352 if (comp_keys(get_rkey(chk_path, sb), key) != 1)
353
354 return 0;
355 return 1;
356}
357
358int reiserfs_check_path(struct treepath *p)
359{
360 RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET,
361 "path not properly relsed");
362 return 0;
363}
364
365
366
367
368void pathrelse_and_restore(struct super_block *sb,
369 struct treepath *search_path)
370{
371 int path_offset = search_path->path_length;
372
373 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
374 "clm-4000: invalid path offset");
375
376 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) {
377 struct buffer_head *bh;
378 bh = PATH_OFFSET_PBUFFER(search_path, path_offset--);
379 reiserfs_restore_prepared_buffer(sb, bh);
380 brelse(bh);
381 }
382 search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
383}
384
385
386void pathrelse(struct treepath *search_path)
387{
388 int path_offset = search_path->path_length;
389
390 RFALSE(path_offset < ILLEGAL_PATH_ELEMENT_OFFSET,
391 "PAP-5090: invalid path offset");
392
393 while (path_offset > ILLEGAL_PATH_ELEMENT_OFFSET)
394 brelse(PATH_OFFSET_PBUFFER(search_path, path_offset--));
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;
403 int used_space;
404 int prev_location;
405 int i;
406 int nr;
407
408 blkh = (struct block_head *)buf;
409 if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) {
410 reiserfs_warning(NULL, "reiserfs-5080",
411 "this should be caught earlier");
412 return 0;
413 }
414
415 nr = blkh_nr_item(blkh);
416 if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) {
417
418 reiserfs_warning(NULL, "reiserfs-5081",
419 "nr_item seems wrong: %z", bh);
420 return 0;
421 }
422 ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1;
423 used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih));
424 if (used_space != blocksize - blkh_free_space(blkh)) {
425
426 reiserfs_warning(NULL, "reiserfs-5082",
427 "free space seems wrong: %z", bh);
428 return 0;
429 }
430
431
432
433
434 ih = (struct item_head *)(buf + BLKH_SIZE);
435 prev_location = blocksize;
436 for (i = 0; i < nr; i++, ih++) {
437 if (le_ih_k_type(ih) == TYPE_ANY) {
438 reiserfs_warning(NULL, "reiserfs-5083",
439 "wrong item type for item %h",
440 ih);
441 return 0;
442 }
443 if (ih_location(ih) >= blocksize
444 || ih_location(ih) < IH_SIZE * nr) {
445 reiserfs_warning(NULL, "reiserfs-5084",
446 "item location seems wrong: %h",
447 ih);
448 return 0;
449 }
450 if (ih_item_len(ih) < 1
451 || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) {
452 reiserfs_warning(NULL, "reiserfs-5085",
453 "item length seems wrong: %h",
454 ih);
455 return 0;
456 }
457 if (prev_location - ih_location(ih) != ih_item_len(ih)) {
458 reiserfs_warning(NULL, "reiserfs-5086",
459 "item location seems wrong "
460 "(second one): %h", ih);
461 return 0;
462 }
463 prev_location = ih_location(ih);
464 }
465
466
467 return 1;
468}
469
470
471static int is_internal(char *buf, int blocksize, struct buffer_head *bh)
472{
473 struct block_head *blkh;
474 int nr;
475 int used_space;
476
477 blkh = (struct block_head *)buf;
478 nr = blkh_level(blkh);
479 if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) {
480
481 reiserfs_warning(NULL, "reiserfs-5087",
482 "this should be caught earlier");
483 return 0;
484 }
485
486 nr = blkh_nr_item(blkh);
487 if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) {
488
489 reiserfs_warning(NULL, "reiserfs-5088",
490 "number of key seems wrong: %z", bh);
491 return 0;
492 }
493
494 used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1);
495 if (used_space != blocksize - blkh_free_space(blkh)) {
496 reiserfs_warning(NULL, "reiserfs-5089",
497 "free space seems wrong: %z", bh);
498 return 0;
499 }
500
501 return 1;
502}
503
504
505
506static int is_tree_node(struct buffer_head *bh, int level)
507{
508 if (B_LEVEL(bh) != level) {
509 reiserfs_warning(NULL, "reiserfs-5090", "node level %d does "
510 "not match to the expected one %d",
511 B_LEVEL(bh), level);
512 return 0;
513 }
514 if (level == DISK_LEAF_NODE_LEVEL)
515 return is_leaf(bh->b_data, bh->b_size, bh);
516
517 return is_internal(bh->b_data, bh->b_size, bh);
518}
519
520#define SEARCH_BY_KEY_READA 16
521
522
523static void search_by_key_reada(struct super_block *s,
524 struct buffer_head **bh,
525 b_blocknr_t *b, int num)
526{
527 int i, j;
528
529 for (i = 0; i < num; i++) {
530 bh[i] = sb_getblk(s, b[i]);
531 }
532 for (j = 0; j < i; j++) {
533
534
535
536
537 if (!buffer_uptodate(bh[j]))
538 ll_rw_block(READA, 1, bh + j);
539 brelse(bh[j]);
540 }
541}
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566int search_by_key(struct super_block *sb, const struct cpu_key *key,
567 struct treepath *search_path,
568
569
570
571
572 int stop_level
573
574
575 )
576{
577 b_blocknr_t block_number;
578 int expected_level;
579 struct buffer_head *bh;
580 struct path_element *last_element;
581 int node_level, retval;
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
589 int repeat_counter = 0;
590#endif
591
592 PROC_INFO_INC(sb, search_by_key);
593
594
595
596
597
598 pathrelse(search_path);
599
600 right_neighbor_of_leaf_node = 0;
601
602
603
604
605 block_number = SB_ROOT_BLOCK(sb);
606 expected_level = -1;
607 while (1) {
608
609#ifdef CONFIG_REISERFS_CHECK
610 if (!(++repeat_counter % 50000))
611 reiserfs_warning(sb, "PAP-5100",
612 "%s: there were %d iterations of "
613 "while loop looking for key %K",
614 current->comm, repeat_counter,
615 key);
616#endif
617
618
619 last_element =
620 PATH_OFFSET_PELEMENT(search_path,
621 ++search_path->path_length);
622 fs_gen = get_generation(sb);
623
624
625
626 if ((bh = last_element->pe_buffer =
627 sb_getblk(sb, block_number))) {
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 {
636 io_error:
637 search_path->path_length--;
638 pathrelse(search_path);
639 return IO_ERROR;
640 }
641 reada_count = 0;
642 if (expected_level == -1)
643 expected_level = SB_TREE_HEIGHT(sb);
644 expected_level--;
645
646
647
648
649 if (fs_changed(fs_gen, sb) &&
650 (!B_IS_IN_TREE(bh) ||
651 B_LEVEL(bh) != expected_level ||
652 !key_in_buffer(search_path, key, sb))) {
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
660
661 block_number = SB_ROOT_BLOCK(sb);
662 expected_level = -1;
663 right_neighbor_of_leaf_node = 0;
664
665
666 continue;
667 }
668
669
670
671
672 RFALSE(comp_keys(&MAX_KEY, key) &&
673 !key_in_buffer(search_path, key, sb),
674 "PAP-5130: key is not in the buffer");
675#ifdef CONFIG_REISERFS_CHECK
676 if (cur_tb) {
677 print_cur_tb("5140");
678 reiserfs_panic(sb, "PAP-5140",
679 "schedule occurred in do_balance!");
680 }
681#endif
682
683
684
685 if (!is_tree_node(bh, expected_level)) {
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
694 node_level = B_LEVEL(bh);
695
696 PROC_INFO_BH_STAT(sb, bh, node_level - 1);
697
698 RFALSE(node_level < stop_level,
699 "vs-5152: tree level (%d) is less than stop level (%d)",
700 node_level, stop_level);
701
702 retval = bin_search(key, B_N_PITEM_HEAD(bh, 0),
703 B_NR_ITEMS(bh),
704 (node_level ==
705 DISK_LEAF_NODE_LEVEL) ? IH_SIZE :
706 KEY_SIZE,
707 &(last_element->pe_position));
708 if (node_level == stop_level) {
709 return retval;
710 }
711
712
713 if (retval == ITEM_FOUND)
714
715 last_element->pe_position++;
716
717
718
719
720
721
722
723
724 block_number =
725 B_N_CHILD_NUM(bh, last_element->pe_position);
726
727
728 if ((search_path->reada & PATH_READA) &&
729 node_level == DISK_LEAF_NODE_LEVEL + 1) {
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)
738 break;
739 reada_blocks[reada_count++] =
740 B_N_CHILD_NUM(bh, pos);
741 if (search_path->reada & PATH_READA_BACK)
742 pos--;
743 else
744 pos++;
745
746
747
748
749 le_key = B_N_PDELIM_KEY(bh, pos);
750 if (le32_to_cpu(le_key->k_objectid) !=
751 key->on_disk_key.k_objectid) {
752 break;
753 }
754 }
755 }
756 }
757}
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775int search_for_position_by_key(struct super_block *sb,
776 const struct cpu_key *p_cpu_key,
777 struct treepath *search_path
778 )
779{
780 struct item_head *p_le_ih;
781 int blk_size;
782 loff_t item_offset, offset;
783 struct reiserfs_dir_entry de;
784 int retval;
785
786
787 if (is_direntry_cpu_key(p_cpu_key))
788 return search_by_entry_key(sb, p_cpu_key, search_path,
789 &de);
790
791
792
793
794 retval = search_item(sb, p_cpu_key, search_path);
795 if (retval == IO_ERROR)
796 return retval;
797 if (retval == ITEM_FOUND) {
798
799 RFALSE(!ih_item_len
800 (B_N_PITEM_HEAD
801 (PATH_PLAST_BUFFER(search_path),
802 PATH_LAST_POSITION(search_path))),
803 "PAP-5165: item length equals zero");
804
805 pos_in_item(search_path) = 0;
806 return POSITION_FOUND;
807 }
808
809 RFALSE(!PATH_LAST_POSITION(search_path),
810 "PAP-5170: position equals zero");
811
812
813 p_le_ih =
814 B_N_PITEM_HEAD(PATH_PLAST_BUFFER(search_path),
815 --PATH_LAST_POSITION(search_path));
816 blk_size = sb->s_blocksize;
817
818 if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) {
819 return FILE_NOT_FOUND;
820 }
821
822
823 item_offset = le_ih_k_offset(p_le_ih);
824 offset = cpu_key_k_offset(p_cpu_key);
825
826
827 if (item_offset <= offset &&
828 item_offset + op_bytes_number(p_le_ih, blk_size) > offset) {
829 pos_in_item(search_path) = offset - item_offset;
830 if (is_indirect_le_ih(p_le_ih)) {
831 pos_in_item(search_path) /= blk_size;
832 }
833 return POSITION_FOUND;
834 }
835
836
837
838 if (is_indirect_le_ih(p_le_ih))
839 pos_in_item(search_path) =
840 ih_item_len(p_le_ih) / UNFM_P_SIZE;
841 else
842 pos_in_item(search_path) = ih_item_len(p_le_ih);
843
844 return POSITION_NOT_FOUND;
845}
846
847
848int comp_items(const struct item_head *stored_ih, const struct treepath *path)
849{
850 struct buffer_head *bh = PATH_PLAST_BUFFER(path);
851 struct item_head *ih;
852
853
854 if (!B_IS_IN_TREE(bh))
855 return 1;
856
857
858 if (PATH_LAST_POSITION(path) >= B_NR_ITEMS(bh))
859 return 1;
860
861
862 ih = get_ih(path);
863 return memcmp(stored_ih, ih, IH_SIZE);
864}
865
866
867
868
869#define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1)
870
871
872#define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh)))
873
874
875static inline int prepare_for_direct_item(struct treepath *path,
876 struct item_head *le_ih,
877 struct inode *inode,
878 loff_t new_file_length, int *cut_size)
879{
880 loff_t round_len;
881
882 if (new_file_length == max_reiserfs_offset(inode)) {
883
884 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
885 return M_DELETE;
886 }
887
888 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) {
889
890 round_len = ROUND_UP(new_file_length);
891
892 if (round_len < le_ih_k_offset(le_ih)) {
893 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
894 return M_DELETE;
895 }
896
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
900 return M_CUT;
901 }
902
903
904
905 if (new_file_length < le_ih_k_offset(le_ih)) {
906 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
907 return M_DELETE;
908 }
909
910 *cut_size = -(ih_item_len(le_ih) -
911 (pos_in_item(path) =
912 new_file_length + 1 - le_ih_k_offset(le_ih)));
913 return M_CUT;
914}
915
916static inline int prepare_for_direntry_item(struct treepath *path,
917 struct item_head *le_ih,
918 struct inode *inode,
919 loff_t new_file_length,
920 int *cut_size)
921{
922 if (le_ih_k_offset(le_ih) == DOT_OFFSET &&
923 new_file_length == max_reiserfs_offset(inode)) {
924 RFALSE(ih_entry_count(le_ih) != 2,
925 "PAP-5220: incorrect empty directory item (%h)", le_ih);
926 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
927 return M_DELETE;
928 }
929
930 if (ih_entry_count(le_ih) == 1) {
931
932
933 *cut_size = -(IH_SIZE + ih_item_len(le_ih));
934 return M_DELETE;
935 }
936
937
938 *cut_size =
939 -(DEH_SIZE +
940 entry_length(get_last_bh(path), le_ih, pos_in_item(path)));
941 return M_CUT;
942}
943
944#define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1)
945
946
947
948
949
950
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,
952
953 int *cut_size, unsigned long long new_file_length
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
963 if (is_statdata_le_ih(p_le_ih)) {
964
965 RFALSE(new_file_length != max_reiserfs_offset(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
973 if (is_direntry_le_ih(p_le_ih))
974 return prepare_for_direntry_item(path, p_le_ih, inode,
975 new_file_length,
976 cut_size);
977
978
979 if (is_direct_le_ih(p_le_ih))
980 return prepare_for_direct_item(path, p_le_ih, inode,
981 new_file_length, cut_size);
982
983
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
992 if ( new_file_length == max_reiserfs_offset (inode) ) {
993
994
995 new_file_length = 0;
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
1006 while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > new_file_length) {
1007 __le32 *unfm;
1008 __u32 block;
1009
1010
1011
1012
1013 if (!delete && (*cut_size) != 0 &&
1014 reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD)
1015 break;
1016
1017 unfm = (__le32 *)B_I_PITEM(bh, &s_ih) + pos - 1;
1018 block = get_block_num(unfm, 0);
1019
1020 if (block != 0) {
1021 reiserfs_prepare_for_journal(sb, bh, 1);
1022 put_block_num(unfm, 0, 0);
1023 journal_mark_dirty(th, sb, bh);
1024 reiserfs_free_block(th, inode, block, 1);
1025 }
1026
1027 cond_resched();
1028
1029 if (item_moved (&s_ih, path)) {
1030 need_re_search = 1;
1031 break;
1032 }
1033
1034 pos --;
1035 (*removed)++;
1036 (*cut_size) -= UNFM_P_SIZE;
1037
1038 if (pos == 0) {
1039 (*cut_size) -= IH_SIZE;
1040 result = M_DELETE;
1041 break;
1042 }
1043 }
1044
1045
1046
1047 reiserfs_restore_prepared_buffer(sb, bh);
1048 } while (need_re_search &&
1049 search_for_position_by_key(sb, item_key, path) == POSITION_FOUND);
1050 pos_in_item(path) = pos * UNFM_P_SIZE;
1051
1052 if (*cut_size == 0) {
1053
1054
1055 result = M_CONVERT;
1056 }
1057 return result;
1058 }
1059}
1060
1061
1062static int calc_deleted_bytes_number(struct tree_balance *tb, char mode)
1063{
1064 int del_size;
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
1070 del_size =
1071 (mode ==
1072 M_DELETE) ? ih_item_len(p_le_ih) : -tb->insert_size[0];
1073 if (is_direntry_le_ih(p_le_ih)) {
1074
1075
1076
1077 return del_size;
1078 }
1079
1080 if (is_indirect_le_ih(p_le_ih))
1081 del_size = (del_size / UNFM_P_SIZE) *
1082 (PATH_PLAST_BUFFER(tb->tb_path)->b_size);
1083 return del_size;
1084}
1085
1086static void init_tb_struct(struct reiserfs_transaction_handle *th,
1087 struct tree_balance *tb,
1088 struct super_block *sb,
1089 struct treepath *path, int size)
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;
1100 tb->insert_size[0] = size;
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;
1109}
1110
1111#ifdef REISERQUOTA_DEBUG
1112char key2type(struct reiserfs_key *ih)
1113{
1114 if (is_direntry_le_key(2, ih))
1115 return 'd';
1116 if (is_direct_le_key(2, ih))
1117 return 'D';
1118 if (is_indirect_le_key(2, ih))
1119 return 'i';
1120 if (is_statdata_le_key(2, ih))
1121 return 's';
1122 return 'u';
1123}
1124
1125char head2type(struct item_head *ih)
1126{
1127 if (is_direntry_le_ih(ih))
1128 return 'd';
1129 if (is_direct_le_ih(ih))
1130 return 'D';
1131 if (is_indirect_le_ih(ih))
1132 return 'i';
1133 if (is_statdata_le_ih(ih))
1134 return 's';
1135 return 'u';
1136}
1137#endif
1138
1139
1140
1141
1142
1143
1144
1145
1146int reiserfs_delete_item(struct reiserfs_transaction_handle *th,
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;
1155 int ret_value, del_size, removed;
1156
1157#ifdef CONFIG_REISERFS_CHECK
1158 char mode;
1159 int iter = 0;
1160#endif
1161
1162 BUG_ON(!th->t_trans_id);
1163
1164 init_tb_struct(th, &s_del_balance, sb, path,
1165 0 );
1166
1167 while (1) {
1168 removed = 0;
1169
1170#ifdef CONFIG_REISERFS_CHECK
1171 iter++;
1172 mode =
1173#endif
1174 prepare_for_delete_or_cut(th, inode, path,
1175 item_key, &removed,
1176 &del_size,
1177 max_reiserfs_offset(inode));
1178
1179 RFALSE(mode != M_DELETE, "PAP-5320: mode must be M_DELETE");
1180
1181 copy_item_head(&s_ih, PATH_PITEM_HEAD(path));
1182 s_del_balance.insert_size[0] = del_size;
1183
1184 ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL);
1185 if (ret_value != REPEAT_SEARCH)
1186 break;
1187
1188 PROC_INFO_INC(sb, delete_item_restarted);
1189
1190
1191 ret_value =
1192 search_for_position_by_key(sb, item_key, path);
1193 if (ret_value == IO_ERROR)
1194 break;
1195 if (ret_value == FILE_NOT_FOUND) {
1196 reiserfs_warning(sb, "vs-5340",
1197 "no items of the file %K found",
1198 item_key);
1199 break;
1200 }
1201 }
1202
1203 if (ret_value != CARRY_ON) {
1204 unfix_nodes(&s_del_balance);
1205 return 0;
1206 }
1207
1208 ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE);
1209 q_ih = get_ih(path);
1210 quota_cut_bytes = ih_item_len(q_ih);
1211
1212
1213
1214
1215
1216
1217
1218 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(q_ih)) {
1219 if ((le_ih_k_offset(q_ih) & (sb->s_blocksize - 1)) == 1) {
1220 quota_cut_bytes = sb->s_blocksize + UNFM_P_SIZE;
1221 } else {
1222 quota_cut_bytes = 0;
1223 }
1224 }
1225
1226 if (un_bh) {
1227 int off;
1228 char *data;
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
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),
1254 ret_value);
1255 kunmap_atomic(data, KM_USER0);
1256 }
1257
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 vfs_dq_free_space_nodirty(inode, quota_cut_bytes);
1266
1267
1268 return ret_value;
1269}
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th,
1289 struct inode *inode, struct reiserfs_key *key)
1290{
1291 struct tree_balance tb;
1292 INITIALIZE_PATH(path);
1293 int item_len = 0;
1294 int tb_init = 0;
1295 struct cpu_key cpu_key;
1296 int retval;
1297 int quota_cut_bytes = 0;
1298
1299 BUG_ON(!th->t_trans_id);
1300
1301 le_key2cpu_key(&cpu_key, key);
1302
1303 while (1) {
1304 retval = search_item(th->t_super, &cpu_key, &path);
1305 if (retval == IO_ERROR) {
1306 reiserfs_error(th->t_super, "vs-5350",
1307 "i/o failure occurred trying "
1308 "to delete %K", &cpu_key);
1309 break;
1310 }
1311 if (retval != ITEM_FOUND) {
1312 pathrelse(&path);
1313
1314 if (!
1315 ((unsigned long long)
1316 GET_HASH_VALUE(le_key_k_offset
1317 (le_key_version(key), key)) == 0
1318 && (unsigned long long)
1319 GET_GENERATION_NUMBER(le_key_k_offset
1320 (le_key_version(key),
1321 key)) == 1))
1322 reiserfs_warning(th->t_super, "vs-5355",
1323 "%k not found", key);
1324 break;
1325 }
1326 if (!tb_init) {
1327 tb_init = 1;
1328 item_len = ih_item_len(PATH_PITEM_HEAD(&path));
1329 init_tb_struct(th, &tb, th->t_super, &path,
1330 -(IH_SIZE + item_len));
1331 }
1332 quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path));
1333
1334 retval = fix_nodes(M_DELETE, &tb, NULL, NULL);
1335 if (retval == REPEAT_SEARCH) {
1336 PROC_INFO_INC(th->t_super, delete_solid_item_restarted);
1337 continue;
1338 }
1339
1340 if (retval == CARRY_ON) {
1341 do_balance(&tb, NULL, NULL, M_DELETE);
1342 if (inode) {
1343#ifdef REISERQUOTA_DEBUG
1344 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
1345 "reiserquota delete_solid_item(): freeing %u id=%u type=%c",
1346 quota_cut_bytes, inode->i_uid,
1347 key2type(key));
1348#endif
1349 vfs_dq_free_space_nodirty(inode,
1350 quota_cut_bytes);
1351 }
1352 break;
1353 }
1354
1355 reiserfs_warning(th->t_super, "vs-5360",
1356 "could not delete %K due to fix_nodes failure",
1357 &cpu_key);
1358 unfix_nodes(&tb);
1359 break;
1360 }
1361
1362 reiserfs_check_path(&path);
1363}
1364
1365int reiserfs_delete_object(struct reiserfs_transaction_handle *th,
1366 struct inode *inode)
1367{
1368 int err;
1369 inode->i_size = 0;
1370 BUG_ON(!th->t_trans_id);
1371
1372
1373 err =
1374 reiserfs_do_truncate(th, inode, NULL, 0 );
1375 if (err)
1376 return err;
1377
1378#if defined( USE_INODE_GENERATION_COUNTER )
1379 if (!old_format_only(th->t_super)) {
1380 __le32 *inode_generation;
1381
1382 inode_generation =
1383 &REISERFS_SB(th->t_super)->s_rs->s_inode_generation;
1384 le32_add_cpu(inode_generation, 1);
1385 }
1386
1387#endif
1388 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1389
1390 return err;
1391}
1392
1393static void unmap_buffers(struct page *page, loff_t pos)
1394{
1395 struct buffer_head *bh;
1396 struct buffer_head *head;
1397 struct buffer_head *next;
1398 unsigned long tail_index;
1399 unsigned long cur_index;
1400
1401 if (page) {
1402 if (page_has_buffers(page)) {
1403 tail_index = pos & (PAGE_CACHE_SIZE - 1);
1404 cur_index = 0;
1405 head = page_buffers(page);
1406 bh = head;
1407 do {
1408 next = bh->b_this_page;
1409
1410
1411
1412
1413
1414
1415
1416 cur_index += bh->b_size;
1417 if (cur_index > tail_index) {
1418 reiserfs_unmap_buffer(bh);
1419 }
1420 bh = next;
1421 } while (bh != head);
1422 }
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,
1431 loff_t new_file_size, char *mode)
1432{
1433 struct super_block *sb = inode->i_sb;
1434 int block_size = sb->s_blocksize;
1435 int cut_bytes;
1436 BUG_ON(!th->t_trans_id);
1437 BUG_ON(new_file_size != inode->i_size);
1438
1439
1440
1441
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
1447 *mode = M_SKIP_BALANCING;
1448 cut_bytes =
1449 block_size - (new_file_size & (block_size - 1));
1450 pathrelse(path);
1451 return cut_bytes;
1452 }
1453
1454
1455
1456 return indirect2direct(th, inode, page, path, item_key,
1457 new_file_size, mode);
1458}
1459
1460
1461
1462
1463
1464static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th,
1465 struct inode *inode, struct treepath *path)
1466{
1467 struct cpu_key tail_key;
1468 int tail_len;
1469 int removed;
1470 BUG_ON(!th->t_trans_id);
1471
1472 make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4);
1473 tail_key.key_length = 4;
1474
1475 tail_len =
1476 (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1;
1477 while (tail_len) {
1478
1479 if (search_for_position_by_key(inode->i_sb, &tail_key, path) ==
1480 POSITION_NOT_FOUND)
1481 reiserfs_panic(inode->i_sb, "vs-5615",
1482 "found invalid item");
1483 RFALSE(path->pos_in_item !=
1484 ih_item_len(PATH_PITEM_HEAD(path)) - 1,
1485 "vs-5616: appended bytes found");
1486 PATH_LAST_POSITION(path)--;
1487
1488 removed =
1489 reiserfs_delete_item(th, path, &tail_key, inode,
1490 NULL );
1491 RFALSE(removed <= 0
1492 || removed > tail_len,
1493 "vs-5617: there was tail %d bytes, removed item length %d bytes",
1494 tail_len, removed);
1495 tail_len -= removed;
1496 set_cpu_key_k_offset(&tail_key,
1497 cpu_key_k_offset(&tail_key) - removed);
1498 }
1499 reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct "
1500 "conversion has been rolled back due to "
1501 "lack of disk space");
1502
1503 mark_inode_dirty(inode);
1504}
1505
1506
1507int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th,
1508 struct treepath *path,
1509 struct cpu_key *item_key,
1510 struct inode *inode,
1511 struct page *page, loff_t new_file_size)
1512{
1513 struct super_block *sb = inode->i_sb;
1514
1515
1516
1517
1518 struct tree_balance s_cut_balance;
1519 struct item_head *p_le_ih;
1520 int cut_size = 0,
1521 ret_value = CARRY_ON, removed = 0,
1522 is_inode_locked = 0;
1523 char mode;
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,
1531 cut_size);
1532
1533
1534
1535 while (1) {
1536
1537
1538
1539
1540
1541 mode =
1542 prepare_for_delete_or_cut(th, inode, path,
1543 item_key, &removed,
1544 &cut_size, new_file_size);
1545 if (mode == M_CONVERT) {
1546
1547
1548 RFALSE(ret_value != CARRY_ON,
1549 "PAP-5570: can not convert twice");
1550
1551 ret_value =
1552 maybe_indirect_to_direct(th, inode, page,
1553 path, item_key,
1554 new_file_size, &mode);
1555 if (mode == M_SKIP_BALANCING)
1556
1557 return ret_value;
1558
1559 is_inode_locked = 1;
1560
1561
1562
1563 retval2 = ret_value;
1564
1565
1566
1567
1568
1569
1570 set_cpu_key_k_type(item_key, TYPE_INDIRECT);
1571 item_key->key_length = 4;
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);
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 }
1588 if (cut_size == 0) {
1589 pathrelse(path);
1590 return 0;
1591 }
1592
1593 s_cut_balance.insert_size[0] = cut_size;
1594
1595 ret_value = fix_nodes(mode, &s_cut_balance, NULL, NULL);
1596 if (ret_value != REPEAT_SEARCH)
1597 break;
1598
1599 PROC_INFO_INC(sb, cut_from_item_restarted);
1600
1601 ret_value =
1602 search_for_position_by_key(sb, item_key, path);
1603 if (ret_value == POSITION_FOUND)
1604 continue;
1605
1606 reiserfs_warning(sb, "PAP-5610", "item %K not found",
1607 item_key);
1608 unfix_nodes(&s_cut_balance);
1609 return (ret_value == IO_ERROR) ? -EIO : -ENOENT;
1610 }
1611
1612
1613 if (ret_value != CARRY_ON) {
1614 if (is_inode_locked) {
1615
1616
1617 indirect_to_direct_roll_back(th, inode, path);
1618 }
1619 if (ret_value == NO_DISK_SPACE)
1620 reiserfs_warning(sb, "reiserfs-5092",
1621 "NO_DISK_SPACE");
1622 unfix_nodes(&s_cut_balance);
1623 return -EIO;
1624 }
1625
1626
1627
1628 RFALSE(mode == M_PASTE || mode == M_INSERT, "invalid mode");
1629
1630
1631 quota_cut_bytes =
1632 (mode ==
1633 M_DELETE) ? ih_item_len(get_ih(path)) : -s_cut_balance.
1634 insert_size[0];
1635 if (retval2 == -1)
1636 ret_value = calc_deleted_bytes_number(&s_cut_balance, mode);
1637 else
1638 ret_value = retval2;
1639
1640
1641
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)) {
1645 if (mode == M_DELETE &&
1646 (le_ih_k_offset(p_le_ih) & (sb->s_blocksize - 1)) ==
1647 1) {
1648
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
1656 if (is_inode_locked) {
1657 struct item_head *le_ih =
1658 PATH_PITEM_HEAD(s_cut_balance.tb_path);
1659
1660
1661
1662 if (!is_indirect_le_ih(le_ih))
1663 reiserfs_panic(sb, "vs-5652",
1664 "item must be indirect %h", le_ih);
1665
1666 if (mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE)
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
1672 if (mode == M_CUT
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
1680
1681 }
1682#endif
1683
1684 do_balance(&s_cut_balance, NULL, NULL, mode);
1685 if (is_inode_locked) {
1686
1687
1688
1689
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 vfs_dq_free_space_nodirty(inode, quota_cut_bytes);
1700 return ret_value;
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");
1709
1710 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET);
1711 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY);
1712 reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode));
1713 reiserfs_update_sd(th, inode);
1714 set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET);
1715 set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA);
1716}
1717
1718
1719
1720int reiserfs_do_truncate(struct reiserfs_transaction_handle *th,
1721 struct inode *inode,
1722 struct page *page,
1723 int update_timestamps
1724
1725
1726
1727 )
1728{
1729 INITIALIZE_PATH(s_search_path);
1730 struct item_head *p_le_ih;
1731 struct cpu_key s_item_key;
1732 loff_t file_size,
1733 new_file_size;
1734 int deleted;
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
1746 truncate_directory(th, inode);
1747 return 0;
1748 }
1749
1750
1751 new_file_size = inode->i_size;
1752
1753
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);
1760 if (retval == IO_ERROR) {
1761 reiserfs_error(inode->i_sb, "vs-5657",
1762 "i/o failure occurred trying to truncate %K",
1763 &s_item_key);
1764 err = -EIO;
1765 goto out;
1766 }
1767 if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) {
1768 reiserfs_error(inode->i_sb, "PAP-5660",
1769 "wrong result %d of search for %K", retval,
1770 &s_item_key);
1771
1772 err = -EIO;
1773 goto out;
1774 }
1775
1776 s_search_path.pos_in_item--;
1777
1778
1779 p_le_ih = PATH_PITEM_HEAD(&s_search_path);
1780 if (is_statdata_le_ih(p_le_ih))
1781 file_size = 0;
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
1788
1789
1790 file_size = offset + bytes - 1;
1791 }
1792
1793
1794
1795
1796 if (new_file_size == 0)
1797 s_search_path.reada = PATH_READA | PATH_READA_BACK;
1798
1799 if (file_size == 0 || file_size < new_file_size) {
1800 goto update_and_out;
1801 }
1802
1803
1804 set_cpu_key_k_offset(&s_item_key, file_size);
1805
1806 do {
1807
1808 deleted =
1809 reiserfs_cut_from_item(th, &s_search_path, &s_item_key,
1810 inode, page, new_file_size);
1811 if (deleted < 0) {
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
1818 RFALSE(deleted > file_size,
1819 "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K",
1820 deleted, file_size, &s_item_key);
1821
1822
1823 file_size -= deleted;
1824
1825 set_cpu_key_k_offset(&s_item_key, file_size);
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836 if (journal_transaction_should_end(th, 0) ||
1837 reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) {
1838 int orig_len_alloc = th->t_blocks_allocated;
1839 pathrelse(&s_search_path);
1840
1841 if (update_timestamps) {
1842 inode->i_mtime = CURRENT_TIME_SEC;
1843 inode->i_ctime = CURRENT_TIME_SEC;
1844 }
1845 reiserfs_update_sd(th, inode);
1846
1847 err = journal_end(th, inode->i_sb, orig_len_alloc);
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 }
1856 } while (file_size > ROUND_UP(new_file_size) &&
1857 search_for_position_by_key(inode->i_sb, &s_item_key,
1858 &s_search_path) == POSITION_FOUND);
1859
1860 RFALSE(file_size > ROUND_UP(new_file_size),
1861 "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d",
1862 new_file_size, file_size, s_item_key.on_disk_key.k_objectid);
1863
1864 update_and_out:
1865 if (update_timestamps) {
1866
1867 inode->i_mtime = CURRENT_TIME_SEC;
1868 inode->i_ctime = CURRENT_TIME_SEC;
1869 }
1870 reiserfs_update_sd(th, inode);
1871
1872 out:
1873 pathrelse(&s_search_path);
1874 return err;
1875}
1876
1877#ifdef CONFIG_REISERFS_CHECK
1878
1879static void check_research_for_paste(struct treepath *path,
1880 const struct cpu_key *key)
1881{
1882 struct item_head *found_ih = get_ih(path);
1883
1884 if (is_direct_le_ih(found_ih)) {
1885 if (le_ih_k_offset(found_ih) +
1886 op_bytes_number(found_ih,
1887 get_last_bh(path)->b_size) !=
1888 cpu_key_k_offset(key)
1889 || op_bytes_number(found_ih,
1890 get_last_bh(path)->b_size) !=
1891 pos_in_item(path))
1892 reiserfs_panic(NULL, "PAP-5720", "found direct item "
1893 "%h or position (%d) does not match "
1894 "to key %K", found_ih,
1895 pos_in_item(path), key);
1896 }
1897 if (is_indirect_le_ih(found_ih)) {
1898 if (le_ih_k_offset(found_ih) +
1899 op_bytes_number(found_ih,
1900 get_last_bh(path)->b_size) !=
1901 cpu_key_k_offset(key)
1902 || I_UNFM_NUM(found_ih) != pos_in_item(path)
1903 || get_ih_free_space(found_ih) != 0)
1904 reiserfs_panic(NULL, "PAP-5730", "found indirect "
1905 "item (%h) or position (%d) does not "
1906 "match to key (%K)",
1907 found_ih, pos_in_item(path), key);
1908 }
1909}
1910#endif
1911
1912
1913int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *search_path,
1914 const struct cpu_key *key,
1915 struct inode *inode,
1916 const char *body,
1917 int pasted_size)
1918{
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",
1930 pasted_size, inode->i_uid,
1931 key2type(&(key->on_disk_key)));
1932#endif
1933
1934 if (vfs_dq_alloc_space_nodirty(inode, pasted_size)) {
1935 pathrelse(search_path);
1936 return -EDQUOT;
1937 }
1938 init_tb_struct(th, &s_paste_balance, th->t_super, search_path,
1939 pasted_size);
1940#ifdef DISPLACE_NEW_PACKING_LOCALITIES
1941 s_paste_balance.key = key->on_disk_key;
1942#endif
1943
1944
1945 if (fs_changed(fs_gen, inode->i_sb)) {
1946 goto search_again;
1947 }
1948
1949 while ((retval =
1950 fix_nodes(M_PASTE, &s_paste_balance, NULL,
1951 body)) == REPEAT_SEARCH) {
1952 search_again:
1953
1954 PROC_INFO_INC(th->t_super, paste_into_item_restarted);
1955 retval =
1956 search_for_position_by_key(th->t_super, key,
1957 search_path);
1958 if (retval == IO_ERROR) {
1959 retval = -EIO;
1960 goto error_out;
1961 }
1962 if (retval == POSITION_FOUND) {
1963 reiserfs_warning(inode->i_sb, "PAP-5710",
1964 "entry or pasted byte (%K) exists",
1965 key);
1966 retval = -EEXIST;
1967 goto error_out;
1968 }
1969#ifdef CONFIG_REISERFS_CHECK
1970 check_research_for_paste(search_path, key);
1971#endif
1972 }
1973
1974
1975
1976 if (retval == CARRY_ON) {
1977 do_balance(&s_paste_balance, NULL , body, M_PASTE);
1978 return 0;
1979 }
1980 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
1981 error_out:
1982
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",
1987 pasted_size, inode->i_uid,
1988 key2type(&(key->on_disk_key)));
1989#endif
1990 vfs_dq_free_space_nodirty(inode, pasted_size);
1991 return retval;
1992}
1993
1994
1995
1996
1997
1998
1999
2000int reiserfs_insert_item(struct reiserfs_transaction_handle *th,
2001 struct treepath *path, const struct cpu_key *key,
2002 struct item_head *ih, struct inode *inode,
2003 const char *body)
2004{
2005 struct tree_balance s_ins_balance;
2006 int retval;
2007 int fs_gen = 0;
2008 int quota_bytes = 0;
2009
2010 BUG_ON(!th->t_trans_id);
2011
2012 if (inode) {
2013 fs_gen = get_generation(inode->i_sb);
2014 quota_bytes = ih_item_len(ih);
2015
2016
2017
2018
2019 if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(ih))
2020 quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE;
2021#ifdef REISERQUOTA_DEBUG
2022 reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE,
2023 "reiserquota insert_item(): allocating %u id=%u type=%c",
2024 quota_bytes, inode->i_uid, head2type(ih));
2025#endif
2026
2027
2028 if (vfs_dq_alloc_space_nodirty(inode, quota_bytes)) {
2029 pathrelse(path);
2030 return -EDQUOT;
2031 }
2032 }
2033 init_tb_struct(th, &s_ins_balance, th->t_super, path,
2034 IH_SIZE + ih_item_len(ih));
2035#ifdef DISPLACE_NEW_PACKING_LOCALITIES
2036 s_ins_balance.key = key->on_disk_key;
2037#endif
2038
2039 if (inode && fs_changed(fs_gen, inode->i_sb)) {
2040 goto search_again;
2041 }
2042
2043 while ((retval =
2044 fix_nodes(M_INSERT, &s_ins_balance, ih,
2045 body)) == REPEAT_SEARCH) {
2046 search_again:
2047
2048 PROC_INFO_INC(th->t_super, insert_item_restarted);
2049 retval = search_item(th->t_super, key, path);
2050 if (retval == IO_ERROR) {
2051 retval = -EIO;
2052 goto error_out;
2053 }
2054 if (retval == ITEM_FOUND) {
2055 reiserfs_warning(th->t_super, "PAP-5760",
2056 "key %K already exists in the tree",
2057 key);
2058 retval = -EEXIST;
2059 goto error_out;
2060 }
2061 }
2062
2063
2064 if (retval == CARRY_ON) {
2065 do_balance(&s_ins_balance, ih, body, M_INSERT);
2066 return 0;
2067 }
2068
2069 retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO;
2070 error_out:
2071
2072 unfix_nodes(&s_ins_balance);
2073#ifdef REISERQUOTA_DEBUG
2074 reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
2075 "reiserquota insert_item(): freeing %u id=%u type=%c",
2076 quota_bytes, inode->i_uid, head2type(ih));
2077#endif
2078 if (inode)
2079 vfs_dq_free_space_nodirty(inode, quota_bytes);
2080 return retval;
2081}
2082