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#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/time.h>
28#include <linux/ext3_jbd.h>
29#include <linux/jbd.h>
30#include <linux/highuid.h>
31#include <linux/pagemap.h>
32#include <linux/quotaops.h>
33#include <linux/string.h>
34#include <linux/buffer_head.h>
35#include <linux/writeback.h>
36#include <linux/mpage.h>
37#include <linux/uio.h>
38#include <linux/bio.h>
39#include <linux/fiemap.h>
40#include <linux/namei.h>
41#include "xattr.h"
42#include "acl.h"
43
44static int ext3_writepage_trans_blocks(struct inode *inode);
45
46
47
48
49static int ext3_inode_is_fast_symlink(struct inode *inode)
50{
51 int ea_blocks = EXT3_I(inode)->i_file_acl ?
52 (inode->i_sb->s_blocksize >> 9) : 0;
53
54 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
55}
56
57
58
59
60
61
62
63
64
65
66int ext3_forget(handle_t *handle, int is_metadata, struct inode *inode,
67 struct buffer_head *bh, ext3_fsblk_t blocknr)
68{
69 int err;
70
71 might_sleep();
72
73 BUFFER_TRACE(bh, "enter");
74
75 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
76 "data mode %lx\n",
77 bh, is_metadata, inode->i_mode,
78 test_opt(inode->i_sb, DATA_FLAGS));
79
80
81
82
83
84
85 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ||
86 (!is_metadata && !ext3_should_journal_data(inode))) {
87 if (bh) {
88 BUFFER_TRACE(bh, "call journal_forget");
89 return ext3_journal_forget(handle, bh);
90 }
91 return 0;
92 }
93
94
95
96
97 BUFFER_TRACE(bh, "call ext3_journal_revoke");
98 err = ext3_journal_revoke(handle, blocknr, bh);
99 if (err)
100 ext3_abort(inode->i_sb, __func__,
101 "error %d when attempting revoke", err);
102 BUFFER_TRACE(bh, "exit");
103 return err;
104}
105
106
107
108
109
110static unsigned long blocks_for_truncate(struct inode *inode)
111{
112 unsigned long needed;
113
114 needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9);
115
116
117
118
119
120
121
122 if (needed < 2)
123 needed = 2;
124
125
126
127 if (needed > EXT3_MAX_TRANS_DATA)
128 needed = EXT3_MAX_TRANS_DATA;
129
130 return EXT3_DATA_TRANS_BLOCKS(inode->i_sb) + needed;
131}
132
133
134
135
136
137
138
139
140
141
142
143static handle_t *start_transaction(struct inode *inode)
144{
145 handle_t *result;
146
147 result = ext3_journal_start(inode, blocks_for_truncate(inode));
148 if (!IS_ERR(result))
149 return result;
150
151 ext3_std_error(inode->i_sb, PTR_ERR(result));
152 return result;
153}
154
155
156
157
158
159
160
161static int try_to_extend_transaction(handle_t *handle, struct inode *inode)
162{
163 if (handle->h_buffer_credits > EXT3_RESERVE_TRANS_BLOCKS)
164 return 0;
165 if (!ext3_journal_extend(handle, blocks_for_truncate(inode)))
166 return 0;
167 return 1;
168}
169
170
171
172
173
174
175static int ext3_journal_test_restart(handle_t *handle, struct inode *inode)
176{
177 jbd_debug(2, "restarting handle %p\n", handle);
178 return ext3_journal_restart(handle, blocks_for_truncate(inode));
179}
180
181
182
183
184void ext3_delete_inode (struct inode * inode)
185{
186 handle_t *handle;
187
188 truncate_inode_pages(&inode->i_data, 0);
189
190 if (is_bad_inode(inode))
191 goto no_delete;
192
193 handle = start_transaction(inode);
194 if (IS_ERR(handle)) {
195
196
197
198
199
200 ext3_orphan_del(NULL, inode);
201 goto no_delete;
202 }
203
204 if (IS_SYNC(inode))
205 handle->h_sync = 1;
206 inode->i_size = 0;
207 if (inode->i_blocks)
208 ext3_truncate(inode);
209
210
211
212
213
214
215
216
217 ext3_orphan_del(handle, inode);
218 EXT3_I(inode)->i_dtime = get_seconds();
219
220
221
222
223
224
225
226
227 if (ext3_mark_inode_dirty(handle, inode))
228
229 clear_inode(inode);
230 else
231 ext3_free_inode(handle, inode);
232 ext3_journal_stop(handle);
233 return;
234no_delete:
235 clear_inode(inode);
236}
237
238typedef struct {
239 __le32 *p;
240 __le32 key;
241 struct buffer_head *bh;
242} Indirect;
243
244static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
245{
246 p->key = *(p->p = v);
247 p->bh = bh;
248}
249
250static int verify_chain(Indirect *from, Indirect *to)
251{
252 while (from <= to && from->key == *from->p)
253 from++;
254 return (from > to);
255}
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288static int ext3_block_to_path(struct inode *inode,
289 long i_block, int offsets[4], int *boundary)
290{
291 int ptrs = EXT3_ADDR_PER_BLOCK(inode->i_sb);
292 int ptrs_bits = EXT3_ADDR_PER_BLOCK_BITS(inode->i_sb);
293 const long direct_blocks = EXT3_NDIR_BLOCKS,
294 indirect_blocks = ptrs,
295 double_blocks = (1 << (ptrs_bits * 2));
296 int n = 0;
297 int final = 0;
298
299 if (i_block < 0) {
300 ext3_warning (inode->i_sb, "ext3_block_to_path", "block < 0");
301 } else if (i_block < direct_blocks) {
302 offsets[n++] = i_block;
303 final = direct_blocks;
304 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
305 offsets[n++] = EXT3_IND_BLOCK;
306 offsets[n++] = i_block;
307 final = ptrs;
308 } else if ((i_block -= indirect_blocks) < double_blocks) {
309 offsets[n++] = EXT3_DIND_BLOCK;
310 offsets[n++] = i_block >> ptrs_bits;
311 offsets[n++] = i_block & (ptrs - 1);
312 final = ptrs;
313 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
314 offsets[n++] = EXT3_TIND_BLOCK;
315 offsets[n++] = i_block >> (ptrs_bits * 2);
316 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
317 offsets[n++] = i_block & (ptrs - 1);
318 final = ptrs;
319 } else {
320 ext3_warning(inode->i_sb, "ext3_block_to_path", "block > big");
321 }
322 if (boundary)
323 *boundary = final - 1 - (i_block & (ptrs - 1));
324 return n;
325}
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356static Indirect *ext3_get_branch(struct inode *inode, int depth, int *offsets,
357 Indirect chain[4], int *err)
358{
359 struct super_block *sb = inode->i_sb;
360 Indirect *p = chain;
361 struct buffer_head *bh;
362
363 *err = 0;
364
365 add_chain (chain, NULL, EXT3_I(inode)->i_data + *offsets);
366 if (!p->key)
367 goto no_block;
368 while (--depth) {
369 bh = sb_bread(sb, le32_to_cpu(p->key));
370 if (!bh)
371 goto failure;
372
373 if (!verify_chain(chain, p))
374 goto changed;
375 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
376
377 if (!p->key)
378 goto no_block;
379 }
380 return NULL;
381
382changed:
383 brelse(bh);
384 *err = -EAGAIN;
385 goto no_block;
386failure:
387 *err = -EIO;
388no_block:
389 return p;
390}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412static ext3_fsblk_t ext3_find_near(struct inode *inode, Indirect *ind)
413{
414 struct ext3_inode_info *ei = EXT3_I(inode);
415 __le32 *start = ind->bh ? (__le32*) ind->bh->b_data : ei->i_data;
416 __le32 *p;
417 ext3_fsblk_t bg_start;
418 ext3_grpblk_t colour;
419
420
421 for (p = ind->p - 1; p >= start; p--) {
422 if (*p)
423 return le32_to_cpu(*p);
424 }
425
426
427 if (ind->bh)
428 return ind->bh->b_blocknr;
429
430
431
432
433
434 bg_start = ext3_group_first_block_no(inode->i_sb, ei->i_block_group);
435 colour = (current->pid % 16) *
436 (EXT3_BLOCKS_PER_GROUP(inode->i_sb) / 16);
437 return bg_start + colour;
438}
439
440
441
442
443
444
445
446
447
448
449
450static ext3_fsblk_t ext3_find_goal(struct inode *inode, long block,
451 Indirect *partial)
452{
453 struct ext3_block_alloc_info *block_i;
454
455 block_i = EXT3_I(inode)->i_block_alloc_info;
456
457
458
459
460
461 if (block_i && (block == block_i->last_alloc_logical_block + 1)
462 && (block_i->last_alloc_physical_block != 0)) {
463 return block_i->last_alloc_physical_block + 1;
464 }
465
466 return ext3_find_near(inode, partial);
467}
468
469
470
471
472
473
474
475
476
477
478
479
480
481static int ext3_blks_to_allocate(Indirect *branch, int k, unsigned long blks,
482 int blocks_to_boundary)
483{
484 unsigned long count = 0;
485
486
487
488
489
490 if (k > 0) {
491
492 if (blks < blocks_to_boundary + 1)
493 count += blks;
494 else
495 count += blocks_to_boundary + 1;
496 return count;
497 }
498
499 count++;
500 while (count < blks && count <= blocks_to_boundary &&
501 le32_to_cpu(*(branch[0].p + count)) == 0) {
502 count++;
503 }
504 return count;
505}
506
507
508
509
510
511
512
513
514
515
516
517static int ext3_alloc_blocks(handle_t *handle, struct inode *inode,
518 ext3_fsblk_t goal, int indirect_blks, int blks,
519 ext3_fsblk_t new_blocks[4], int *err)
520{
521 int target, i;
522 unsigned long count = 0;
523 int index = 0;
524 ext3_fsblk_t current_block = 0;
525 int ret = 0;
526
527
528
529
530
531
532
533
534
535 target = blks + indirect_blks;
536
537 while (1) {
538 count = target;
539
540 current_block = ext3_new_blocks(handle,inode,goal,&count,err);
541 if (*err)
542 goto failed_out;
543
544 target -= count;
545
546 while (index < indirect_blks && count) {
547 new_blocks[index++] = current_block++;
548 count--;
549 }
550
551 if (count > 0)
552 break;
553 }
554
555
556 new_blocks[index] = current_block;
557
558
559 ret = count;
560 *err = 0;
561 return ret;
562failed_out:
563 for (i = 0; i <index; i++)
564 ext3_free_blocks(handle, inode, new_blocks[i], 1);
565 return ret;
566}
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593static int ext3_alloc_branch(handle_t *handle, struct inode *inode,
594 int indirect_blks, int *blks, ext3_fsblk_t goal,
595 int *offsets, Indirect *branch)
596{
597 int blocksize = inode->i_sb->s_blocksize;
598 int i, n = 0;
599 int err = 0;
600 struct buffer_head *bh;
601 int num;
602 ext3_fsblk_t new_blocks[4];
603 ext3_fsblk_t current_block;
604
605 num = ext3_alloc_blocks(handle, inode, goal, indirect_blks,
606 *blks, new_blocks, &err);
607 if (err)
608 return err;
609
610 branch[0].key = cpu_to_le32(new_blocks[0]);
611
612
613
614 for (n = 1; n <= indirect_blks; n++) {
615
616
617
618
619
620 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
621 branch[n].bh = bh;
622 lock_buffer(bh);
623 BUFFER_TRACE(bh, "call get_create_access");
624 err = ext3_journal_get_create_access(handle, bh);
625 if (err) {
626 unlock_buffer(bh);
627 brelse(bh);
628 goto failed;
629 }
630
631 memset(bh->b_data, 0, blocksize);
632 branch[n].p = (__le32 *) bh->b_data + offsets[n];
633 branch[n].key = cpu_to_le32(new_blocks[n]);
634 *branch[n].p = branch[n].key;
635 if ( n == indirect_blks) {
636 current_block = new_blocks[n];
637
638
639
640
641
642 for (i=1; i < num; i++)
643 *(branch[n].p + i) = cpu_to_le32(++current_block);
644 }
645 BUFFER_TRACE(bh, "marking uptodate");
646 set_buffer_uptodate(bh);
647 unlock_buffer(bh);
648
649 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
650 err = ext3_journal_dirty_metadata(handle, bh);
651 if (err)
652 goto failed;
653 }
654 *blks = num;
655 return err;
656failed:
657
658 for (i = 1; i <= n ; i++) {
659 BUFFER_TRACE(branch[i].bh, "call journal_forget");
660 ext3_journal_forget(handle, branch[i].bh);
661 }
662 for (i = 0; i <indirect_blks; i++)
663 ext3_free_blocks(handle, inode, new_blocks[i], 1);
664
665 ext3_free_blocks(handle, inode, new_blocks[i], num);
666
667 return err;
668}
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684static int ext3_splice_branch(handle_t *handle, struct inode *inode,
685 long block, Indirect *where, int num, int blks)
686{
687 int i;
688 int err = 0;
689 struct ext3_block_alloc_info *block_i;
690 ext3_fsblk_t current_block;
691
692 block_i = EXT3_I(inode)->i_block_alloc_info;
693
694
695
696
697
698 if (where->bh) {
699 BUFFER_TRACE(where->bh, "get_write_access");
700 err = ext3_journal_get_write_access(handle, where->bh);
701 if (err)
702 goto err_out;
703 }
704
705
706 *where->p = where->key;
707
708
709
710
711
712 if (num == 0 && blks > 1) {
713 current_block = le32_to_cpu(where->key) + 1;
714 for (i = 1; i < blks; i++)
715 *(where->p + i ) = cpu_to_le32(current_block++);
716 }
717
718
719
720
721
722
723 if (block_i) {
724 block_i->last_alloc_logical_block = block + blks - 1;
725 block_i->last_alloc_physical_block =
726 le32_to_cpu(where[num].key) + blks - 1;
727 }
728
729
730
731 inode->i_ctime = CURRENT_TIME_SEC;
732 ext3_mark_inode_dirty(handle, inode);
733
734
735 if (where->bh) {
736
737
738
739
740
741
742
743
744 jbd_debug(5, "splicing indirect only\n");
745 BUFFER_TRACE(where->bh, "call ext3_journal_dirty_metadata");
746 err = ext3_journal_dirty_metadata(handle, where->bh);
747 if (err)
748 goto err_out;
749 } else {
750
751
752
753
754 jbd_debug(5, "splicing direct\n");
755 }
756 return err;
757
758err_out:
759 for (i = 1; i <= num; i++) {
760 BUFFER_TRACE(where[i].bh, "call journal_forget");
761 ext3_journal_forget(handle, where[i].bh);
762 ext3_free_blocks(handle,inode,le32_to_cpu(where[i-1].key),1);
763 }
764 ext3_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks);
765
766 return err;
767}
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788int ext3_get_blocks_handle(handle_t *handle, struct inode *inode,
789 sector_t iblock, unsigned long maxblocks,
790 struct buffer_head *bh_result,
791 int create)
792{
793 int err = -EIO;
794 int offsets[4];
795 Indirect chain[4];
796 Indirect *partial;
797 ext3_fsblk_t goal;
798 int indirect_blks;
799 int blocks_to_boundary = 0;
800 int depth;
801 struct ext3_inode_info *ei = EXT3_I(inode);
802 int count = 0;
803 ext3_fsblk_t first_block = 0;
804
805
806 J_ASSERT(handle != NULL || create == 0);
807 depth = ext3_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
808
809 if (depth == 0)
810 goto out;
811
812 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
813
814
815 if (!partial) {
816 first_block = le32_to_cpu(chain[depth - 1].key);
817 clear_buffer_new(bh_result);
818 count++;
819
820 while (count < maxblocks && count <= blocks_to_boundary) {
821 ext3_fsblk_t blk;
822
823 if (!verify_chain(chain, chain + depth - 1)) {
824
825
826
827
828
829
830
831 err = -EAGAIN;
832 count = 0;
833 break;
834 }
835 blk = le32_to_cpu(*(chain[depth-1].p + count));
836
837 if (blk == first_block + count)
838 count++;
839 else
840 break;
841 }
842 if (err != -EAGAIN)
843 goto got_it;
844 }
845
846
847 if (!create || err == -EIO)
848 goto cleanup;
849
850 mutex_lock(&ei->truncate_mutex);
851
852
853
854
855
856
857
858
859
860
861
862
863
864 if (err == -EAGAIN || !verify_chain(chain, partial)) {
865 while (partial > chain) {
866 brelse(partial->bh);
867 partial--;
868 }
869 partial = ext3_get_branch(inode, depth, offsets, chain, &err);
870 if (!partial) {
871 count++;
872 mutex_unlock(&ei->truncate_mutex);
873 if (err)
874 goto cleanup;
875 clear_buffer_new(bh_result);
876 goto got_it;
877 }
878 }
879
880
881
882
883
884 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
885 ext3_init_block_alloc_info(inode);
886
887 goal = ext3_find_goal(inode, iblock, partial);
888
889
890 indirect_blks = (chain + depth) - partial - 1;
891
892
893
894
895
896 count = ext3_blks_to_allocate(partial, indirect_blks,
897 maxblocks, blocks_to_boundary);
898
899
900
901 err = ext3_alloc_branch(handle, inode, indirect_blks, &count, goal,
902 offsets + (partial - chain), partial);
903
904
905
906
907
908
909
910
911 if (!err)
912 err = ext3_splice_branch(handle, inode, iblock,
913 partial, indirect_blks, count);
914 mutex_unlock(&ei->truncate_mutex);
915 if (err)
916 goto cleanup;
917
918 set_buffer_new(bh_result);
919got_it:
920 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
921 if (count > blocks_to_boundary)
922 set_buffer_boundary(bh_result);
923 err = count;
924
925 partial = chain + depth - 1;
926cleanup:
927 while (partial > chain) {
928 BUFFER_TRACE(partial->bh, "call brelse");
929 brelse(partial->bh);
930 partial--;
931 }
932 BUFFER_TRACE(bh_result, "returned");
933out:
934 return err;
935}
936
937
938#define DIO_MAX_BLOCKS 4096
939
940
941
942
943
944
945
946#define DIO_CREDITS 25
947
948static int ext3_get_block(struct inode *inode, sector_t iblock,
949 struct buffer_head *bh_result, int create)
950{
951 handle_t *handle = ext3_journal_current_handle();
952 int ret = 0, started = 0;
953 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
954
955 if (create && !handle) {
956 if (max_blocks > DIO_MAX_BLOCKS)
957 max_blocks = DIO_MAX_BLOCKS;
958 handle = ext3_journal_start(inode, DIO_CREDITS +
959 2 * EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb));
960 if (IS_ERR(handle)) {
961 ret = PTR_ERR(handle);
962 goto out;
963 }
964 started = 1;
965 }
966
967 ret = ext3_get_blocks_handle(handle, inode, iblock,
968 max_blocks, bh_result, create);
969 if (ret > 0) {
970 bh_result->b_size = (ret << inode->i_blkbits);
971 ret = 0;
972 }
973 if (started)
974 ext3_journal_stop(handle);
975out:
976 return ret;
977}
978
979int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
980 u64 start, u64 len)
981{
982 return generic_block_fiemap(inode, fieinfo, start, len,
983 ext3_get_block);
984}
985
986
987
988
989struct buffer_head *ext3_getblk(handle_t *handle, struct inode *inode,
990 long block, int create, int *errp)
991{
992 struct buffer_head dummy;
993 int fatal = 0, err;
994
995 J_ASSERT(handle != NULL || create == 0);
996
997 dummy.b_state = 0;
998 dummy.b_blocknr = -1000;
999 buffer_trace_init(&dummy.b_history);
1000 err = ext3_get_blocks_handle(handle, inode, block, 1,
1001 &dummy, create);
1002
1003
1004
1005
1006 if (err > 0) {
1007 if (err > 1)
1008 WARN_ON(1);
1009 err = 0;
1010 }
1011 *errp = err;
1012 if (!err && buffer_mapped(&dummy)) {
1013 struct buffer_head *bh;
1014 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
1015 if (!bh) {
1016 *errp = -EIO;
1017 goto err;
1018 }
1019 if (buffer_new(&dummy)) {
1020 J_ASSERT(create != 0);
1021 J_ASSERT(handle != NULL);
1022
1023
1024
1025
1026
1027
1028
1029
1030 lock_buffer(bh);
1031 BUFFER_TRACE(bh, "call get_create_access");
1032 fatal = ext3_journal_get_create_access(handle, bh);
1033 if (!fatal && !buffer_uptodate(bh)) {
1034 memset(bh->b_data,0,inode->i_sb->s_blocksize);
1035 set_buffer_uptodate(bh);
1036 }
1037 unlock_buffer(bh);
1038 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
1039 err = ext3_journal_dirty_metadata(handle, bh);
1040 if (!fatal)
1041 fatal = err;
1042 } else {
1043 BUFFER_TRACE(bh, "not a new buffer");
1044 }
1045 if (fatal) {
1046 *errp = fatal;
1047 brelse(bh);
1048 bh = NULL;
1049 }
1050 return bh;
1051 }
1052err:
1053 return NULL;
1054}
1055
1056struct buffer_head *ext3_bread(handle_t *handle, struct inode *inode,
1057 int block, int create, int *err)
1058{
1059 struct buffer_head * bh;
1060
1061 bh = ext3_getblk(handle, inode, block, create, err);
1062 if (!bh)
1063 return bh;
1064 if (buffer_uptodate(bh))
1065 return bh;
1066 ll_rw_block(READ_META, 1, &bh);
1067 wait_on_buffer(bh);
1068 if (buffer_uptodate(bh))
1069 return bh;
1070 put_bh(bh);
1071 *err = -EIO;
1072 return NULL;
1073}
1074
1075static int walk_page_buffers( handle_t *handle,
1076 struct buffer_head *head,
1077 unsigned from,
1078 unsigned to,
1079 int *partial,
1080 int (*fn)( handle_t *handle,
1081 struct buffer_head *bh))
1082{
1083 struct buffer_head *bh;
1084 unsigned block_start, block_end;
1085 unsigned blocksize = head->b_size;
1086 int err, ret = 0;
1087 struct buffer_head *next;
1088
1089 for ( bh = head, block_start = 0;
1090 ret == 0 && (bh != head || !block_start);
1091 block_start = block_end, bh = next)
1092 {
1093 next = bh->b_this_page;
1094 block_end = block_start + blocksize;
1095 if (block_end <= from || block_start >= to) {
1096 if (partial && !buffer_uptodate(bh))
1097 *partial = 1;
1098 continue;
1099 }
1100 err = (*fn)(handle, bh);
1101 if (!ret)
1102 ret = err;
1103 }
1104 return ret;
1105}
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132static int do_journal_get_write_access(handle_t *handle,
1133 struct buffer_head *bh)
1134{
1135 if (!buffer_mapped(bh) || buffer_freed(bh))
1136 return 0;
1137 return ext3_journal_get_write_access(handle, bh);
1138}
1139
1140static int ext3_write_begin(struct file *file, struct address_space *mapping,
1141 loff_t pos, unsigned len, unsigned flags,
1142 struct page **pagep, void **fsdata)
1143{
1144 struct inode *inode = mapping->host;
1145 int ret;
1146 handle_t *handle;
1147 int retries = 0;
1148 struct page *page;
1149 pgoff_t index;
1150 unsigned from, to;
1151
1152
1153 int needed_blocks = ext3_writepage_trans_blocks(inode) + 1;
1154
1155 index = pos >> PAGE_CACHE_SHIFT;
1156 from = pos & (PAGE_CACHE_SIZE - 1);
1157 to = from + len;
1158
1159retry:
1160 page = grab_cache_page_write_begin(mapping, index, flags);
1161 if (!page)
1162 return -ENOMEM;
1163 *pagep = page;
1164
1165 handle = ext3_journal_start(inode, needed_blocks);
1166 if (IS_ERR(handle)) {
1167 unlock_page(page);
1168 page_cache_release(page);
1169 ret = PTR_ERR(handle);
1170 goto out;
1171 }
1172 ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1173 ext3_get_block);
1174 if (ret)
1175 goto write_begin_failed;
1176
1177 if (ext3_should_journal_data(inode)) {
1178 ret = walk_page_buffers(handle, page_buffers(page),
1179 from, to, NULL, do_journal_get_write_access);
1180 }
1181write_begin_failed:
1182 if (ret) {
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1193 ext3_orphan_add(handle, inode);
1194 ext3_journal_stop(handle);
1195 unlock_page(page);
1196 page_cache_release(page);
1197 if (pos + len > inode->i_size)
1198 ext3_truncate(inode);
1199 }
1200 if (ret == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
1201 goto retry;
1202out:
1203 return ret;
1204}
1205
1206
1207int ext3_journal_dirty_data(handle_t *handle, struct buffer_head *bh)
1208{
1209 int err = journal_dirty_data(handle, bh);
1210 if (err)
1211 ext3_journal_abort_handle(__func__, __func__,
1212 bh, handle, err);
1213 return err;
1214}
1215
1216
1217static int journal_dirty_data_fn(handle_t *handle, struct buffer_head *bh)
1218{
1219
1220
1221
1222
1223 if (buffer_mapped(bh) && buffer_uptodate(bh))
1224 return ext3_journal_dirty_data(handle, bh);
1225 return 0;
1226}
1227
1228
1229static int write_end_fn(handle_t *handle, struct buffer_head *bh)
1230{
1231 if (!buffer_mapped(bh) || buffer_freed(bh))
1232 return 0;
1233 set_buffer_uptodate(bh);
1234 return ext3_journal_dirty_metadata(handle, bh);
1235}
1236
1237
1238
1239
1240
1241
1242
1243static void update_file_sizes(struct inode *inode, loff_t pos, unsigned copied)
1244{
1245
1246 if (pos + copied > inode->i_size)
1247 i_size_write(inode, pos + copied);
1248 if (pos + copied > EXT3_I(inode)->i_disksize) {
1249 EXT3_I(inode)->i_disksize = pos + copied;
1250 mark_inode_dirty(inode);
1251 }
1252}
1253
1254
1255
1256
1257
1258
1259
1260
1261static int ext3_ordered_write_end(struct file *file,
1262 struct address_space *mapping,
1263 loff_t pos, unsigned len, unsigned copied,
1264 struct page *page, void *fsdata)
1265{
1266 handle_t *handle = ext3_journal_current_handle();
1267 struct inode *inode = file->f_mapping->host;
1268 unsigned from, to;
1269 int ret = 0, ret2;
1270
1271 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1272
1273 from = pos & (PAGE_CACHE_SIZE - 1);
1274 to = from + copied;
1275 ret = walk_page_buffers(handle, page_buffers(page),
1276 from, to, NULL, journal_dirty_data_fn);
1277
1278 if (ret == 0)
1279 update_file_sizes(inode, pos, copied);
1280
1281
1282
1283
1284 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1285 ext3_orphan_add(handle, inode);
1286 ret2 = ext3_journal_stop(handle);
1287 if (!ret)
1288 ret = ret2;
1289 unlock_page(page);
1290 page_cache_release(page);
1291
1292 if (pos + len > inode->i_size)
1293 ext3_truncate(inode);
1294 return ret ? ret : copied;
1295}
1296
1297static int ext3_writeback_write_end(struct file *file,
1298 struct address_space *mapping,
1299 loff_t pos, unsigned len, unsigned copied,
1300 struct page *page, void *fsdata)
1301{
1302 handle_t *handle = ext3_journal_current_handle();
1303 struct inode *inode = file->f_mapping->host;
1304 int ret;
1305
1306 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
1307 update_file_sizes(inode, pos, copied);
1308
1309
1310
1311
1312 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1313 ext3_orphan_add(handle, inode);
1314 ret = ext3_journal_stop(handle);
1315 unlock_page(page);
1316 page_cache_release(page);
1317
1318 if (pos + len > inode->i_size)
1319 ext3_truncate(inode);
1320 return ret ? ret : copied;
1321}
1322
1323static int ext3_journalled_write_end(struct file *file,
1324 struct address_space *mapping,
1325 loff_t pos, unsigned len, unsigned copied,
1326 struct page *page, void *fsdata)
1327{
1328 handle_t *handle = ext3_journal_current_handle();
1329 struct inode *inode = mapping->host;
1330 int ret = 0, ret2;
1331 int partial = 0;
1332 unsigned from, to;
1333
1334 from = pos & (PAGE_CACHE_SIZE - 1);
1335 to = from + len;
1336
1337 if (copied < len) {
1338 if (!PageUptodate(page))
1339 copied = 0;
1340 page_zero_new_buffers(page, from + copied, to);
1341 to = from + copied;
1342 }
1343
1344 ret = walk_page_buffers(handle, page_buffers(page), from,
1345 to, &partial, write_end_fn);
1346 if (!partial)
1347 SetPageUptodate(page);
1348
1349 if (pos + copied > inode->i_size)
1350 i_size_write(inode, pos + copied);
1351
1352
1353
1354
1355 if (pos + len > inode->i_size && ext3_can_truncate(inode))
1356 ext3_orphan_add(handle, inode);
1357 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1358 if (inode->i_size > EXT3_I(inode)->i_disksize) {
1359 EXT3_I(inode)->i_disksize = inode->i_size;
1360 ret2 = ext3_mark_inode_dirty(handle, inode);
1361 if (!ret)
1362 ret = ret2;
1363 }
1364
1365 ret2 = ext3_journal_stop(handle);
1366 if (!ret)
1367 ret = ret2;
1368 unlock_page(page);
1369 page_cache_release(page);
1370
1371 if (pos + len > inode->i_size)
1372 ext3_truncate(inode);
1373 return ret ? ret : copied;
1374}
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
1391{
1392 struct inode *inode = mapping->host;
1393 journal_t *journal;
1394 int err;
1395
1396 if (EXT3_I(inode)->i_state & EXT3_STATE_JDATA) {
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415 EXT3_I(inode)->i_state &= ~EXT3_STATE_JDATA;
1416 journal = EXT3_JOURNAL(inode);
1417 journal_lock_updates(journal);
1418 err = journal_flush(journal);
1419 journal_unlock_updates(journal);
1420
1421 if (err)
1422 return 0;
1423 }
1424
1425 return generic_block_bmap(mapping,block,ext3_get_block);
1426}
1427
1428static int bget_one(handle_t *handle, struct buffer_head *bh)
1429{
1430 get_bh(bh);
1431 return 0;
1432}
1433
1434static int bput_one(handle_t *handle, struct buffer_head *bh)
1435{
1436 put_bh(bh);
1437 return 0;
1438}
1439
1440static int buffer_unmapped(handle_t *handle, struct buffer_head *bh)
1441{
1442 return !buffer_mapped(bh);
1443}
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497static int ext3_ordered_writepage(struct page *page,
1498 struct writeback_control *wbc)
1499{
1500 struct inode *inode = page->mapping->host;
1501 struct buffer_head *page_bufs;
1502 handle_t *handle = NULL;
1503 int ret = 0;
1504 int err;
1505
1506 J_ASSERT(PageLocked(page));
1507
1508
1509
1510
1511
1512 if (ext3_journal_current_handle())
1513 goto out_fail;
1514
1515 if (!page_has_buffers(page)) {
1516 create_empty_buffers(page, inode->i_sb->s_blocksize,
1517 (1 << BH_Dirty)|(1 << BH_Uptodate));
1518 page_bufs = page_buffers(page);
1519 } else {
1520 page_bufs = page_buffers(page);
1521 if (!walk_page_buffers(NULL, page_bufs, 0, PAGE_CACHE_SIZE,
1522 NULL, buffer_unmapped)) {
1523
1524
1525 return block_write_full_page(page, NULL, wbc);
1526 }
1527 }
1528 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1529
1530 if (IS_ERR(handle)) {
1531 ret = PTR_ERR(handle);
1532 goto out_fail;
1533 }
1534
1535 walk_page_buffers(handle, page_bufs, 0,
1536 PAGE_CACHE_SIZE, NULL, bget_one);
1537
1538 ret = block_write_full_page(page, ext3_get_block, wbc);
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552 if (ret == 0) {
1553 err = walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE,
1554 NULL, journal_dirty_data_fn);
1555 if (!ret)
1556 ret = err;
1557 }
1558 walk_page_buffers(handle, page_bufs, 0,
1559 PAGE_CACHE_SIZE, NULL, bput_one);
1560 err = ext3_journal_stop(handle);
1561 if (!ret)
1562 ret = err;
1563 return ret;
1564
1565out_fail:
1566 redirty_page_for_writepage(wbc, page);
1567 unlock_page(page);
1568 return ret;
1569}
1570
1571static int ext3_writeback_writepage(struct page *page,
1572 struct writeback_control *wbc)
1573{
1574 struct inode *inode = page->mapping->host;
1575 handle_t *handle = NULL;
1576 int ret = 0;
1577 int err;
1578
1579 if (ext3_journal_current_handle())
1580 goto out_fail;
1581
1582 if (page_has_buffers(page)) {
1583 if (!walk_page_buffers(NULL, page_buffers(page), 0,
1584 PAGE_CACHE_SIZE, NULL, buffer_unmapped)) {
1585
1586
1587 return block_write_full_page(page, NULL, wbc);
1588 }
1589 }
1590
1591 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1592 if (IS_ERR(handle)) {
1593 ret = PTR_ERR(handle);
1594 goto out_fail;
1595 }
1596
1597 if (test_opt(inode->i_sb, NOBH) && ext3_should_writeback_data(inode))
1598 ret = nobh_writepage(page, ext3_get_block, wbc);
1599 else
1600 ret = block_write_full_page(page, ext3_get_block, wbc);
1601
1602 err = ext3_journal_stop(handle);
1603 if (!ret)
1604 ret = err;
1605 return ret;
1606
1607out_fail:
1608 redirty_page_for_writepage(wbc, page);
1609 unlock_page(page);
1610 return ret;
1611}
1612
1613static int ext3_journalled_writepage(struct page *page,
1614 struct writeback_control *wbc)
1615{
1616 struct inode *inode = page->mapping->host;
1617 handle_t *handle = NULL;
1618 int ret = 0;
1619 int err;
1620
1621 if (ext3_journal_current_handle())
1622 goto no_write;
1623
1624 handle = ext3_journal_start(inode, ext3_writepage_trans_blocks(inode));
1625 if (IS_ERR(handle)) {
1626 ret = PTR_ERR(handle);
1627 goto no_write;
1628 }
1629
1630 if (!page_has_buffers(page) || PageChecked(page)) {
1631
1632
1633
1634
1635 ClearPageChecked(page);
1636 ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE,
1637 ext3_get_block);
1638 if (ret != 0) {
1639 ext3_journal_stop(handle);
1640 goto out_unlock;
1641 }
1642 ret = walk_page_buffers(handle, page_buffers(page), 0,
1643 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access);
1644
1645 err = walk_page_buffers(handle, page_buffers(page), 0,
1646 PAGE_CACHE_SIZE, NULL, write_end_fn);
1647 if (ret == 0)
1648 ret = err;
1649 EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1650 unlock_page(page);
1651 } else {
1652
1653
1654
1655
1656
1657 ret = block_write_full_page(page, ext3_get_block, wbc);
1658 }
1659 err = ext3_journal_stop(handle);
1660 if (!ret)
1661 ret = err;
1662out:
1663 return ret;
1664
1665no_write:
1666 redirty_page_for_writepage(wbc, page);
1667out_unlock:
1668 unlock_page(page);
1669 goto out;
1670}
1671
1672static int ext3_readpage(struct file *file, struct page *page)
1673{
1674 return mpage_readpage(page, ext3_get_block);
1675}
1676
1677static int
1678ext3_readpages(struct file *file, struct address_space *mapping,
1679 struct list_head *pages, unsigned nr_pages)
1680{
1681 return mpage_readpages(mapping, pages, nr_pages, ext3_get_block);
1682}
1683
1684static void ext3_invalidatepage(struct page *page, unsigned long offset)
1685{
1686 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1687
1688
1689
1690
1691 if (offset == 0)
1692 ClearPageChecked(page);
1693
1694 journal_invalidatepage(journal, page, offset);
1695}
1696
1697static int ext3_releasepage(struct page *page, gfp_t wait)
1698{
1699 journal_t *journal = EXT3_JOURNAL(page->mapping->host);
1700
1701 WARN_ON(PageChecked(page));
1702 if (!page_has_buffers(page))
1703 return 0;
1704 return journal_try_to_free_buffers(journal, page, wait);
1705}
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716static ssize_t ext3_direct_IO(int rw, struct kiocb *iocb,
1717 const struct iovec *iov, loff_t offset,
1718 unsigned long nr_segs)
1719{
1720 struct file *file = iocb->ki_filp;
1721 struct inode *inode = file->f_mapping->host;
1722 struct ext3_inode_info *ei = EXT3_I(inode);
1723 handle_t *handle;
1724 ssize_t ret;
1725 int orphan = 0;
1726 size_t count = iov_length(iov, nr_segs);
1727
1728 if (rw == WRITE) {
1729 loff_t final_size = offset + count;
1730
1731 if (final_size > inode->i_size) {
1732
1733 handle = ext3_journal_start(inode, 2);
1734 if (IS_ERR(handle)) {
1735 ret = PTR_ERR(handle);
1736 goto out;
1737 }
1738 ret = ext3_orphan_add(handle, inode);
1739 if (ret) {
1740 ext3_journal_stop(handle);
1741 goto out;
1742 }
1743 orphan = 1;
1744 ei->i_disksize = inode->i_size;
1745 ext3_journal_stop(handle);
1746 }
1747 }
1748
1749 ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1750 offset, nr_segs,
1751 ext3_get_block, NULL);
1752
1753 if (orphan) {
1754 int err;
1755
1756
1757 handle = ext3_journal_start(inode, 2);
1758 if (IS_ERR(handle)) {
1759
1760
1761
1762 ret = PTR_ERR(handle);
1763 goto out;
1764 }
1765 if (inode->i_nlink)
1766 ext3_orphan_del(handle, inode);
1767 if (ret > 0) {
1768 loff_t end = offset + ret;
1769 if (end > inode->i_size) {
1770 ei->i_disksize = end;
1771 i_size_write(inode, end);
1772
1773
1774
1775
1776
1777
1778
1779 ext3_mark_inode_dirty(handle, inode);
1780 }
1781 }
1782 err = ext3_journal_stop(handle);
1783 if (ret == 0)
1784 ret = err;
1785 }
1786out:
1787 return ret;
1788}
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803static int ext3_journalled_set_page_dirty(struct page *page)
1804{
1805 SetPageChecked(page);
1806 return __set_page_dirty_nobuffers(page);
1807}
1808
1809static const struct address_space_operations ext3_ordered_aops = {
1810 .readpage = ext3_readpage,
1811 .readpages = ext3_readpages,
1812 .writepage = ext3_ordered_writepage,
1813 .sync_page = block_sync_page,
1814 .write_begin = ext3_write_begin,
1815 .write_end = ext3_ordered_write_end,
1816 .bmap = ext3_bmap,
1817 .invalidatepage = ext3_invalidatepage,
1818 .releasepage = ext3_releasepage,
1819 .direct_IO = ext3_direct_IO,
1820 .migratepage = buffer_migrate_page,
1821 .is_partially_uptodate = block_is_partially_uptodate,
1822};
1823
1824static const struct address_space_operations ext3_writeback_aops = {
1825 .readpage = ext3_readpage,
1826 .readpages = ext3_readpages,
1827 .writepage = ext3_writeback_writepage,
1828 .sync_page = block_sync_page,
1829 .write_begin = ext3_write_begin,
1830 .write_end = ext3_writeback_write_end,
1831 .bmap = ext3_bmap,
1832 .invalidatepage = ext3_invalidatepage,
1833 .releasepage = ext3_releasepage,
1834 .direct_IO = ext3_direct_IO,
1835 .migratepage = buffer_migrate_page,
1836 .is_partially_uptodate = block_is_partially_uptodate,
1837};
1838
1839static const struct address_space_operations ext3_journalled_aops = {
1840 .readpage = ext3_readpage,
1841 .readpages = ext3_readpages,
1842 .writepage = ext3_journalled_writepage,
1843 .sync_page = block_sync_page,
1844 .write_begin = ext3_write_begin,
1845 .write_end = ext3_journalled_write_end,
1846 .set_page_dirty = ext3_journalled_set_page_dirty,
1847 .bmap = ext3_bmap,
1848 .invalidatepage = ext3_invalidatepage,
1849 .releasepage = ext3_releasepage,
1850 .is_partially_uptodate = block_is_partially_uptodate,
1851};
1852
1853void ext3_set_aops(struct inode *inode)
1854{
1855 if (ext3_should_order_data(inode))
1856 inode->i_mapping->a_ops = &ext3_ordered_aops;
1857 else if (ext3_should_writeback_data(inode))
1858 inode->i_mapping->a_ops = &ext3_writeback_aops;
1859 else
1860 inode->i_mapping->a_ops = &ext3_journalled_aops;
1861}
1862
1863
1864
1865
1866
1867
1868
1869static int ext3_block_truncate_page(handle_t *handle, struct page *page,
1870 struct address_space *mapping, loff_t from)
1871{
1872 ext3_fsblk_t index = from >> PAGE_CACHE_SHIFT;
1873 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1874 unsigned blocksize, iblock, length, pos;
1875 struct inode *inode = mapping->host;
1876 struct buffer_head *bh;
1877 int err = 0;
1878
1879 blocksize = inode->i_sb->s_blocksize;
1880 length = blocksize - (offset & (blocksize - 1));
1881 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1882
1883
1884
1885
1886
1887 if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) &&
1888 ext3_should_writeback_data(inode) && PageUptodate(page)) {
1889 zero_user(page, offset, length);
1890 set_page_dirty(page);
1891 goto unlock;
1892 }
1893
1894 if (!page_has_buffers(page))
1895 create_empty_buffers(page, blocksize, 0);
1896
1897
1898 bh = page_buffers(page);
1899 pos = blocksize;
1900 while (offset >= pos) {
1901 bh = bh->b_this_page;
1902 iblock++;
1903 pos += blocksize;
1904 }
1905
1906 err = 0;
1907 if (buffer_freed(bh)) {
1908 BUFFER_TRACE(bh, "freed: skip");
1909 goto unlock;
1910 }
1911
1912 if (!buffer_mapped(bh)) {
1913 BUFFER_TRACE(bh, "unmapped");
1914 ext3_get_block(inode, iblock, bh, 0);
1915
1916 if (!buffer_mapped(bh)) {
1917 BUFFER_TRACE(bh, "still unmapped");
1918 goto unlock;
1919 }
1920 }
1921
1922
1923 if (PageUptodate(page))
1924 set_buffer_uptodate(bh);
1925
1926 if (!buffer_uptodate(bh)) {
1927 err = -EIO;
1928 ll_rw_block(READ, 1, &bh);
1929 wait_on_buffer(bh);
1930
1931 if (!buffer_uptodate(bh))
1932 goto unlock;
1933 }
1934
1935 if (ext3_should_journal_data(inode)) {
1936 BUFFER_TRACE(bh, "get write access");
1937 err = ext3_journal_get_write_access(handle, bh);
1938 if (err)
1939 goto unlock;
1940 }
1941
1942 zero_user(page, offset, length);
1943 BUFFER_TRACE(bh, "zeroed end of block");
1944
1945 err = 0;
1946 if (ext3_should_journal_data(inode)) {
1947 err = ext3_journal_dirty_metadata(handle, bh);
1948 } else {
1949 if (ext3_should_order_data(inode))
1950 err = ext3_journal_dirty_data(handle, bh);
1951 mark_buffer_dirty(bh);
1952 }
1953
1954unlock:
1955 unlock_page(page);
1956 page_cache_release(page);
1957 return err;
1958}
1959
1960
1961
1962
1963
1964
1965static inline int all_zeroes(__le32 *p, __le32 *q)
1966{
1967 while (p < q)
1968 if (*p++)
1969 return 0;
1970 return 1;
1971}
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008static Indirect *ext3_find_shared(struct inode *inode, int depth,
2009 int offsets[4], Indirect chain[4], __le32 *top)
2010{
2011 Indirect *partial, *p;
2012 int k, err;
2013
2014 *top = 0;
2015
2016 for (k = depth; k > 1 && !offsets[k-1]; k--)
2017 ;
2018 partial = ext3_get_branch(inode, k, offsets, chain, &err);
2019
2020 if (!partial)
2021 partial = chain + k-1;
2022
2023
2024
2025
2026 if (!partial->key && *partial->p)
2027
2028 goto no_top;
2029 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
2030 ;
2031
2032
2033
2034
2035
2036
2037 if (p == chain + k - 1 && p > chain) {
2038 p->p--;
2039 } else {
2040 *top = *p->p;
2041
2042#if 0
2043 *p->p = 0;
2044#endif
2045 }
2046
2047
2048 while(partial > p) {
2049 brelse(partial->bh);
2050 partial--;
2051 }
2052no_top:
2053 return partial;
2054}
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064static void ext3_clear_blocks(handle_t *handle, struct inode *inode,
2065 struct buffer_head *bh, ext3_fsblk_t block_to_free,
2066 unsigned long count, __le32 *first, __le32 *last)
2067{
2068 __le32 *p;
2069 if (try_to_extend_transaction(handle, inode)) {
2070 if (bh) {
2071 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2072 ext3_journal_dirty_metadata(handle, bh);
2073 }
2074 ext3_mark_inode_dirty(handle, inode);
2075 ext3_journal_test_restart(handle, inode);
2076 if (bh) {
2077 BUFFER_TRACE(bh, "retaking write access");
2078 ext3_journal_get_write_access(handle, bh);
2079 }
2080 }
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090 for (p = first; p < last; p++) {
2091 u32 nr = le32_to_cpu(*p);
2092 if (nr) {
2093 struct buffer_head *bh;
2094
2095 *p = 0;
2096 bh = sb_find_get_block(inode->i_sb, nr);
2097 ext3_forget(handle, 0, inode, bh, nr);
2098 }
2099 }
2100
2101 ext3_free_blocks(handle, inode, block_to_free, count);
2102}
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123static void ext3_free_data(handle_t *handle, struct inode *inode,
2124 struct buffer_head *this_bh,
2125 __le32 *first, __le32 *last)
2126{
2127 ext3_fsblk_t block_to_free = 0;
2128 unsigned long count = 0;
2129 __le32 *block_to_free_p = NULL;
2130
2131
2132 ext3_fsblk_t nr;
2133 __le32 *p;
2134
2135 int err;
2136
2137 if (this_bh) {
2138 BUFFER_TRACE(this_bh, "get_write_access");
2139 err = ext3_journal_get_write_access(handle, this_bh);
2140
2141
2142 if (err)
2143 return;
2144 }
2145
2146 for (p = first; p < last; p++) {
2147 nr = le32_to_cpu(*p);
2148 if (nr) {
2149
2150 if (count == 0) {
2151 block_to_free = nr;
2152 block_to_free_p = p;
2153 count = 1;
2154 } else if (nr == block_to_free + count) {
2155 count++;
2156 } else {
2157 ext3_clear_blocks(handle, inode, this_bh,
2158 block_to_free,
2159 count, block_to_free_p, p);
2160 block_to_free = nr;
2161 block_to_free_p = p;
2162 count = 1;
2163 }
2164 }
2165 }
2166
2167 if (count > 0)
2168 ext3_clear_blocks(handle, inode, this_bh, block_to_free,
2169 count, block_to_free_p, p);
2170
2171 if (this_bh) {
2172 BUFFER_TRACE(this_bh, "call ext3_journal_dirty_metadata");
2173
2174
2175
2176
2177
2178
2179
2180 if (bh2jh(this_bh))
2181 ext3_journal_dirty_metadata(handle, this_bh);
2182 else
2183 ext3_error(inode->i_sb, "ext3_free_data",
2184 "circular indirect block detected, "
2185 "inode=%lu, block=%llu",
2186 inode->i_ino,
2187 (unsigned long long)this_bh->b_blocknr);
2188 }
2189}
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204static void ext3_free_branches(handle_t *handle, struct inode *inode,
2205 struct buffer_head *parent_bh,
2206 __le32 *first, __le32 *last, int depth)
2207{
2208 ext3_fsblk_t nr;
2209 __le32 *p;
2210
2211 if (is_handle_aborted(handle))
2212 return;
2213
2214 if (depth--) {
2215 struct buffer_head *bh;
2216 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2217 p = last;
2218 while (--p >= first) {
2219 nr = le32_to_cpu(*p);
2220 if (!nr)
2221 continue;
2222
2223
2224 bh = sb_bread(inode->i_sb, nr);
2225
2226
2227
2228
2229
2230 if (!bh) {
2231 ext3_error(inode->i_sb, "ext3_free_branches",
2232 "Read failure, inode=%lu, block="E3FSBLK,
2233 inode->i_ino, nr);
2234 continue;
2235 }
2236
2237
2238 BUFFER_TRACE(bh, "free child branches");
2239 ext3_free_branches(handle, inode, bh,
2240 (__le32*)bh->b_data,
2241 (__le32*)bh->b_data + addr_per_block,
2242 depth);
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281 if (is_handle_aborted(handle))
2282 return;
2283 if (try_to_extend_transaction(handle, inode)) {
2284 ext3_mark_inode_dirty(handle, inode);
2285 ext3_journal_test_restart(handle, inode);
2286 }
2287
2288 ext3_free_blocks(handle, inode, nr, 1);
2289
2290 if (parent_bh) {
2291
2292
2293
2294
2295 BUFFER_TRACE(parent_bh, "get_write_access");
2296 if (!ext3_journal_get_write_access(handle,
2297 parent_bh)){
2298 *p = 0;
2299 BUFFER_TRACE(parent_bh,
2300 "call ext3_journal_dirty_metadata");
2301 ext3_journal_dirty_metadata(handle,
2302 parent_bh);
2303 }
2304 }
2305 }
2306 } else {
2307
2308 BUFFER_TRACE(parent_bh, "free data blocks");
2309 ext3_free_data(handle, inode, parent_bh, first, last);
2310 }
2311}
2312
2313int ext3_can_truncate(struct inode *inode)
2314{
2315 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2316 return 0;
2317 if (S_ISREG(inode->i_mode))
2318 return 1;
2319 if (S_ISDIR(inode->i_mode))
2320 return 1;
2321 if (S_ISLNK(inode->i_mode))
2322 return !ext3_inode_is_fast_symlink(inode);
2323 return 0;
2324}
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354void ext3_truncate(struct inode *inode)
2355{
2356 handle_t *handle;
2357 struct ext3_inode_info *ei = EXT3_I(inode);
2358 __le32 *i_data = ei->i_data;
2359 int addr_per_block = EXT3_ADDR_PER_BLOCK(inode->i_sb);
2360 struct address_space *mapping = inode->i_mapping;
2361 int offsets[4];
2362 Indirect chain[4];
2363 Indirect *partial;
2364 __le32 nr = 0;
2365 int n;
2366 long last_block;
2367 unsigned blocksize = inode->i_sb->s_blocksize;
2368 struct page *page;
2369
2370 if (!ext3_can_truncate(inode))
2371 goto out_notrans;
2372
2373 if (inode->i_size == 0 && ext3_should_writeback_data(inode))
2374 ei->i_state |= EXT3_STATE_FLUSH_ON_CLOSE;
2375
2376
2377
2378
2379
2380 if ((inode->i_size & (blocksize - 1)) == 0) {
2381
2382 page = NULL;
2383 } else {
2384 page = grab_cache_page(mapping,
2385 inode->i_size >> PAGE_CACHE_SHIFT);
2386 if (!page)
2387 goto out_notrans;
2388 }
2389
2390 handle = start_transaction(inode);
2391 if (IS_ERR(handle)) {
2392 if (page) {
2393 clear_highpage(page);
2394 flush_dcache_page(page);
2395 unlock_page(page);
2396 page_cache_release(page);
2397 }
2398 goto out_notrans;
2399 }
2400
2401 last_block = (inode->i_size + blocksize-1)
2402 >> EXT3_BLOCK_SIZE_BITS(inode->i_sb);
2403
2404 if (page)
2405 ext3_block_truncate_page(handle, page, mapping, inode->i_size);
2406
2407 n = ext3_block_to_path(inode, last_block, offsets, NULL);
2408 if (n == 0)
2409 goto out_stop;
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420 if (ext3_orphan_add(handle, inode))
2421 goto out_stop;
2422
2423
2424
2425
2426
2427
2428
2429
2430 ei->i_disksize = inode->i_size;
2431
2432
2433
2434
2435
2436 mutex_lock(&ei->truncate_mutex);
2437
2438 if (n == 1) {
2439 ext3_free_data(handle, inode, NULL, i_data+offsets[0],
2440 i_data + EXT3_NDIR_BLOCKS);
2441 goto do_indirects;
2442 }
2443
2444 partial = ext3_find_shared(inode, n, offsets, chain, &nr);
2445
2446 if (nr) {
2447 if (partial == chain) {
2448
2449 ext3_free_branches(handle, inode, NULL,
2450 &nr, &nr+1, (chain+n-1) - partial);
2451 *partial->p = 0;
2452
2453
2454
2455
2456 } else {
2457
2458 BUFFER_TRACE(partial->bh, "get_write_access");
2459 ext3_free_branches(handle, inode, partial->bh,
2460 partial->p,
2461 partial->p+1, (chain+n-1) - partial);
2462 }
2463 }
2464
2465 while (partial > chain) {
2466 ext3_free_branches(handle, inode, partial->bh, partial->p + 1,
2467 (__le32*)partial->bh->b_data+addr_per_block,
2468 (chain+n-1) - partial);
2469 BUFFER_TRACE(partial->bh, "call brelse");
2470 brelse (partial->bh);
2471 partial--;
2472 }
2473do_indirects:
2474
2475 switch (offsets[0]) {
2476 default:
2477 nr = i_data[EXT3_IND_BLOCK];
2478 if (nr) {
2479 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 1);
2480 i_data[EXT3_IND_BLOCK] = 0;
2481 }
2482 case EXT3_IND_BLOCK:
2483 nr = i_data[EXT3_DIND_BLOCK];
2484 if (nr) {
2485 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 2);
2486 i_data[EXT3_DIND_BLOCK] = 0;
2487 }
2488 case EXT3_DIND_BLOCK:
2489 nr = i_data[EXT3_TIND_BLOCK];
2490 if (nr) {
2491 ext3_free_branches(handle, inode, NULL, &nr, &nr+1, 3);
2492 i_data[EXT3_TIND_BLOCK] = 0;
2493 }
2494 case EXT3_TIND_BLOCK:
2495 ;
2496 }
2497
2498 ext3_discard_reservation(inode);
2499
2500 mutex_unlock(&ei->truncate_mutex);
2501 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
2502 ext3_mark_inode_dirty(handle, inode);
2503
2504
2505
2506
2507
2508 if (IS_SYNC(inode))
2509 handle->h_sync = 1;
2510out_stop:
2511
2512
2513
2514
2515
2516
2517
2518 if (inode->i_nlink)
2519 ext3_orphan_del(handle, inode);
2520
2521 ext3_journal_stop(handle);
2522 return;
2523out_notrans:
2524
2525
2526
2527
2528 if (inode->i_nlink)
2529 ext3_orphan_del(NULL, inode);
2530}
2531
2532static ext3_fsblk_t ext3_get_inode_block(struct super_block *sb,
2533 unsigned long ino, struct ext3_iloc *iloc)
2534{
2535 unsigned long block_group;
2536 unsigned long offset;
2537 ext3_fsblk_t block;
2538 struct ext3_group_desc *gdp;
2539
2540 if (!ext3_valid_inum(sb, ino)) {
2541
2542
2543
2544
2545
2546 return 0;
2547 }
2548
2549 block_group = (ino - 1) / EXT3_INODES_PER_GROUP(sb);
2550 gdp = ext3_get_group_desc(sb, block_group, NULL);
2551 if (!gdp)
2552 return 0;
2553
2554
2555
2556 offset = ((ino - 1) % EXT3_INODES_PER_GROUP(sb)) *
2557 EXT3_INODE_SIZE(sb);
2558 block = le32_to_cpu(gdp->bg_inode_table) +
2559 (offset >> EXT3_BLOCK_SIZE_BITS(sb));
2560
2561 iloc->block_group = block_group;
2562 iloc->offset = offset & (EXT3_BLOCK_SIZE(sb) - 1);
2563 return block;
2564}
2565
2566
2567
2568
2569
2570
2571
2572static int __ext3_get_inode_loc(struct inode *inode,
2573 struct ext3_iloc *iloc, int in_mem)
2574{
2575 ext3_fsblk_t block;
2576 struct buffer_head *bh;
2577
2578 block = ext3_get_inode_block(inode->i_sb, inode->i_ino, iloc);
2579 if (!block)
2580 return -EIO;
2581
2582 bh = sb_getblk(inode->i_sb, block);
2583 if (!bh) {
2584 ext3_error (inode->i_sb, "ext3_get_inode_loc",
2585 "unable to read inode block - "
2586 "inode=%lu, block="E3FSBLK,
2587 inode->i_ino, block);
2588 return -EIO;
2589 }
2590 if (!buffer_uptodate(bh)) {
2591 lock_buffer(bh);
2592
2593
2594
2595
2596
2597
2598
2599 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
2600 set_buffer_uptodate(bh);
2601
2602 if (buffer_uptodate(bh)) {
2603
2604 unlock_buffer(bh);
2605 goto has_buffer;
2606 }
2607
2608
2609
2610
2611
2612
2613 if (in_mem) {
2614 struct buffer_head *bitmap_bh;
2615 struct ext3_group_desc *desc;
2616 int inodes_per_buffer;
2617 int inode_offset, i;
2618 int block_group;
2619 int start;
2620
2621 block_group = (inode->i_ino - 1) /
2622 EXT3_INODES_PER_GROUP(inode->i_sb);
2623 inodes_per_buffer = bh->b_size /
2624 EXT3_INODE_SIZE(inode->i_sb);
2625 inode_offset = ((inode->i_ino - 1) %
2626 EXT3_INODES_PER_GROUP(inode->i_sb));
2627 start = inode_offset & ~(inodes_per_buffer - 1);
2628
2629
2630 desc = ext3_get_group_desc(inode->i_sb,
2631 block_group, NULL);
2632 if (!desc)
2633 goto make_io;
2634
2635 bitmap_bh = sb_getblk(inode->i_sb,
2636 le32_to_cpu(desc->bg_inode_bitmap));
2637 if (!bitmap_bh)
2638 goto make_io;
2639
2640
2641
2642
2643
2644
2645 if (!buffer_uptodate(bitmap_bh)) {
2646 brelse(bitmap_bh);
2647 goto make_io;
2648 }
2649 for (i = start; i < start + inodes_per_buffer; i++) {
2650 if (i == inode_offset)
2651 continue;
2652 if (ext3_test_bit(i, bitmap_bh->b_data))
2653 break;
2654 }
2655 brelse(bitmap_bh);
2656 if (i == start + inodes_per_buffer) {
2657
2658 memset(bh->b_data, 0, bh->b_size);
2659 set_buffer_uptodate(bh);
2660 unlock_buffer(bh);
2661 goto has_buffer;
2662 }
2663 }
2664
2665make_io:
2666
2667
2668
2669
2670
2671 get_bh(bh);
2672 bh->b_end_io = end_buffer_read_sync;
2673 submit_bh(READ_META, bh);
2674 wait_on_buffer(bh);
2675 if (!buffer_uptodate(bh)) {
2676 ext3_error(inode->i_sb, "ext3_get_inode_loc",
2677 "unable to read inode block - "
2678 "inode=%lu, block="E3FSBLK,
2679 inode->i_ino, block);
2680 brelse(bh);
2681 return -EIO;
2682 }
2683 }
2684has_buffer:
2685 iloc->bh = bh;
2686 return 0;
2687}
2688
2689int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
2690{
2691
2692 return __ext3_get_inode_loc(inode, iloc,
2693 !(EXT3_I(inode)->i_state & EXT3_STATE_XATTR));
2694}
2695
2696void ext3_set_inode_flags(struct inode *inode)
2697{
2698 unsigned int flags = EXT3_I(inode)->i_flags;
2699
2700 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
2701 if (flags & EXT3_SYNC_FL)
2702 inode->i_flags |= S_SYNC;
2703 if (flags & EXT3_APPEND_FL)
2704 inode->i_flags |= S_APPEND;
2705 if (flags & EXT3_IMMUTABLE_FL)
2706 inode->i_flags |= S_IMMUTABLE;
2707 if (flags & EXT3_NOATIME_FL)
2708 inode->i_flags |= S_NOATIME;
2709 if (flags & EXT3_DIRSYNC_FL)
2710 inode->i_flags |= S_DIRSYNC;
2711}
2712
2713
2714void ext3_get_inode_flags(struct ext3_inode_info *ei)
2715{
2716 unsigned int flags = ei->vfs_inode.i_flags;
2717
2718 ei->i_flags &= ~(EXT3_SYNC_FL|EXT3_APPEND_FL|
2719 EXT3_IMMUTABLE_FL|EXT3_NOATIME_FL|EXT3_DIRSYNC_FL);
2720 if (flags & S_SYNC)
2721 ei->i_flags |= EXT3_SYNC_FL;
2722 if (flags & S_APPEND)
2723 ei->i_flags |= EXT3_APPEND_FL;
2724 if (flags & S_IMMUTABLE)
2725 ei->i_flags |= EXT3_IMMUTABLE_FL;
2726 if (flags & S_NOATIME)
2727 ei->i_flags |= EXT3_NOATIME_FL;
2728 if (flags & S_DIRSYNC)
2729 ei->i_flags |= EXT3_DIRSYNC_FL;
2730}
2731
2732struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
2733{
2734 struct ext3_iloc iloc;
2735 struct ext3_inode *raw_inode;
2736 struct ext3_inode_info *ei;
2737 struct buffer_head *bh;
2738 struct inode *inode;
2739 long ret;
2740 int block;
2741
2742 inode = iget_locked(sb, ino);
2743 if (!inode)
2744 return ERR_PTR(-ENOMEM);
2745 if (!(inode->i_state & I_NEW))
2746 return inode;
2747
2748 ei = EXT3_I(inode);
2749 ei->i_block_alloc_info = NULL;
2750
2751 ret = __ext3_get_inode_loc(inode, &iloc, 0);
2752 if (ret < 0)
2753 goto bad_inode;
2754 bh = iloc.bh;
2755 raw_inode = ext3_raw_inode(&iloc);
2756 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
2757 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
2758 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
2759 if(!(test_opt (inode->i_sb, NO_UID32))) {
2760 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
2761 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
2762 }
2763 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
2764 inode->i_size = le32_to_cpu(raw_inode->i_size);
2765 inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
2766 inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
2767 inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
2768 inode->i_atime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_mtime.tv_nsec = 0;
2769
2770 ei->i_state = 0;
2771 ei->i_dir_start_lookup = 0;
2772 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
2773
2774
2775
2776
2777
2778 if (inode->i_nlink == 0) {
2779 if (inode->i_mode == 0 ||
2780 !(EXT3_SB(inode->i_sb)->s_mount_state & EXT3_ORPHAN_FS)) {
2781
2782 brelse (bh);
2783 ret = -ESTALE;
2784 goto bad_inode;
2785 }
2786
2787
2788
2789
2790 }
2791 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
2792 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
2793#ifdef EXT3_FRAGMENTS
2794 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
2795 ei->i_frag_no = raw_inode->i_frag;
2796 ei->i_frag_size = raw_inode->i_fsize;
2797#endif
2798 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
2799 if (!S_ISREG(inode->i_mode)) {
2800 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
2801 } else {
2802 inode->i_size |=
2803 ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
2804 }
2805 ei->i_disksize = inode->i_size;
2806 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
2807 ei->i_block_group = iloc.block_group;
2808
2809
2810
2811
2812 for (block = 0; block < EXT3_N_BLOCKS; block++)
2813 ei->i_data[block] = raw_inode->i_block[block];
2814 INIT_LIST_HEAD(&ei->i_orphan);
2815
2816 if (inode->i_ino >= EXT3_FIRST_INO(inode->i_sb) + 1 &&
2817 EXT3_INODE_SIZE(inode->i_sb) > EXT3_GOOD_OLD_INODE_SIZE) {
2818
2819
2820
2821
2822
2823 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
2824 if (EXT3_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
2825 EXT3_INODE_SIZE(inode->i_sb)) {
2826 brelse (bh);
2827 ret = -EIO;
2828 goto bad_inode;
2829 }
2830 if (ei->i_extra_isize == 0) {
2831
2832 ei->i_extra_isize = sizeof(struct ext3_inode) -
2833 EXT3_GOOD_OLD_INODE_SIZE;
2834 } else {
2835 __le32 *magic = (void *)raw_inode +
2836 EXT3_GOOD_OLD_INODE_SIZE +
2837 ei->i_extra_isize;
2838 if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
2839 ei->i_state |= EXT3_STATE_XATTR;
2840 }
2841 } else
2842 ei->i_extra_isize = 0;
2843
2844 if (S_ISREG(inode->i_mode)) {
2845 inode->i_op = &ext3_file_inode_operations;
2846 inode->i_fop = &ext3_file_operations;
2847 ext3_set_aops(inode);
2848 } else if (S_ISDIR(inode->i_mode)) {
2849 inode->i_op = &ext3_dir_inode_operations;
2850 inode->i_fop = &ext3_dir_operations;
2851 } else if (S_ISLNK(inode->i_mode)) {
2852 if (ext3_inode_is_fast_symlink(inode)) {
2853 inode->i_op = &ext3_fast_symlink_inode_operations;
2854 nd_terminate_link(ei->i_data, inode->i_size,
2855 sizeof(ei->i_data) - 1);
2856 } else {
2857 inode->i_op = &ext3_symlink_inode_operations;
2858 ext3_set_aops(inode);
2859 }
2860 } else {
2861 inode->i_op = &ext3_special_inode_operations;
2862 if (raw_inode->i_block[0])
2863 init_special_inode(inode, inode->i_mode,
2864 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
2865 else
2866 init_special_inode(inode, inode->i_mode,
2867 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
2868 }
2869 brelse (iloc.bh);
2870 ext3_set_inode_flags(inode);
2871 unlock_new_inode(inode);
2872 return inode;
2873
2874bad_inode:
2875 iget_failed(inode);
2876 return ERR_PTR(ret);
2877}
2878
2879
2880
2881
2882
2883
2884
2885
2886static int ext3_do_update_inode(handle_t *handle,
2887 struct inode *inode,
2888 struct ext3_iloc *iloc)
2889{
2890 struct ext3_inode *raw_inode = ext3_raw_inode(iloc);
2891 struct ext3_inode_info *ei = EXT3_I(inode);
2892 struct buffer_head *bh = iloc->bh;
2893 int err = 0, rc, block;
2894
2895
2896
2897 if (ei->i_state & EXT3_STATE_NEW)
2898 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
2899
2900 ext3_get_inode_flags(ei);
2901 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
2902 if(!(test_opt(inode->i_sb, NO_UID32))) {
2903 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
2904 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
2905
2906
2907
2908
2909 if(!ei->i_dtime) {
2910 raw_inode->i_uid_high =
2911 cpu_to_le16(high_16_bits(inode->i_uid));
2912 raw_inode->i_gid_high =
2913 cpu_to_le16(high_16_bits(inode->i_gid));
2914 } else {
2915 raw_inode->i_uid_high = 0;
2916 raw_inode->i_gid_high = 0;
2917 }
2918 } else {
2919 raw_inode->i_uid_low =
2920 cpu_to_le16(fs_high2lowuid(inode->i_uid));
2921 raw_inode->i_gid_low =
2922 cpu_to_le16(fs_high2lowgid(inode->i_gid));
2923 raw_inode->i_uid_high = 0;
2924 raw_inode->i_gid_high = 0;
2925 }
2926 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
2927 raw_inode->i_size = cpu_to_le32(ei->i_disksize);
2928 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
2929 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
2930 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
2931 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
2932 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
2933 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
2934#ifdef EXT3_FRAGMENTS
2935 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
2936 raw_inode->i_frag = ei->i_frag_no;
2937 raw_inode->i_fsize = ei->i_frag_size;
2938#endif
2939 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
2940 if (!S_ISREG(inode->i_mode)) {
2941 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
2942 } else {
2943 raw_inode->i_size_high =
2944 cpu_to_le32(ei->i_disksize >> 32);
2945 if (ei->i_disksize > 0x7fffffffULL) {
2946 struct super_block *sb = inode->i_sb;
2947 if (!EXT3_HAS_RO_COMPAT_FEATURE(sb,
2948 EXT3_FEATURE_RO_COMPAT_LARGE_FILE) ||
2949 EXT3_SB(sb)->s_es->s_rev_level ==
2950 cpu_to_le32(EXT3_GOOD_OLD_REV)) {
2951
2952
2953
2954 err = ext3_journal_get_write_access(handle,
2955 EXT3_SB(sb)->s_sbh);
2956 if (err)
2957 goto out_brelse;
2958 ext3_update_dynamic_rev(sb);
2959 EXT3_SET_RO_COMPAT_FEATURE(sb,
2960 EXT3_FEATURE_RO_COMPAT_LARGE_FILE);
2961 handle->h_sync = 1;
2962 err = ext3_journal_dirty_metadata(handle,
2963 EXT3_SB(sb)->s_sbh);
2964 }
2965 }
2966 }
2967 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
2968 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
2969 if (old_valid_dev(inode->i_rdev)) {
2970 raw_inode->i_block[0] =
2971 cpu_to_le32(old_encode_dev(inode->i_rdev));
2972 raw_inode->i_block[1] = 0;
2973 } else {
2974 raw_inode->i_block[0] = 0;
2975 raw_inode->i_block[1] =
2976 cpu_to_le32(new_encode_dev(inode->i_rdev));
2977 raw_inode->i_block[2] = 0;
2978 }
2979 } else for (block = 0; block < EXT3_N_BLOCKS; block++)
2980 raw_inode->i_block[block] = ei->i_data[block];
2981
2982 if (ei->i_extra_isize)
2983 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
2984
2985 BUFFER_TRACE(bh, "call ext3_journal_dirty_metadata");
2986 rc = ext3_journal_dirty_metadata(handle, bh);
2987 if (!err)
2988 err = rc;
2989 ei->i_state &= ~EXT3_STATE_NEW;
2990
2991out_brelse:
2992 brelse (bh);
2993 ext3_std_error(inode->i_sb, err);
2994 return err;
2995}
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032int ext3_write_inode(struct inode *inode, int wait)
3033{
3034 if (current->flags & PF_MEMALLOC)
3035 return 0;
3036
3037 if (ext3_journal_current_handle()) {
3038 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
3039 dump_stack();
3040 return -EIO;
3041 }
3042
3043 if (!wait)
3044 return 0;
3045
3046 return ext3_force_commit(inode->i_sb);
3047}
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066int ext3_setattr(struct dentry *dentry, struct iattr *attr)
3067{
3068 struct inode *inode = dentry->d_inode;
3069 int error, rc = 0;
3070 const unsigned int ia_valid = attr->ia_valid;
3071
3072 error = inode_change_ok(inode, attr);
3073 if (error)
3074 return error;
3075
3076 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
3077 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
3078 handle_t *handle;
3079
3080
3081
3082 handle = ext3_journal_start(inode, 2*(EXT3_QUOTA_INIT_BLOCKS(inode->i_sb)+
3083 EXT3_QUOTA_DEL_BLOCKS(inode->i_sb))+3);
3084 if (IS_ERR(handle)) {
3085 error = PTR_ERR(handle);
3086 goto err_out;
3087 }
3088 error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0;
3089 if (error) {
3090 ext3_journal_stop(handle);
3091 return error;
3092 }
3093
3094
3095 if (attr->ia_valid & ATTR_UID)
3096 inode->i_uid = attr->ia_uid;
3097 if (attr->ia_valid & ATTR_GID)
3098 inode->i_gid = attr->ia_gid;
3099 error = ext3_mark_inode_dirty(handle, inode);
3100 ext3_journal_stop(handle);
3101 }
3102
3103 if (S_ISREG(inode->i_mode) &&
3104 attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) {
3105 handle_t *handle;
3106
3107 handle = ext3_journal_start(inode, 3);
3108 if (IS_ERR(handle)) {
3109 error = PTR_ERR(handle);
3110 goto err_out;
3111 }
3112
3113 error = ext3_orphan_add(handle, inode);
3114 EXT3_I(inode)->i_disksize = attr->ia_size;
3115 rc = ext3_mark_inode_dirty(handle, inode);
3116 if (!error)
3117 error = rc;
3118 ext3_journal_stop(handle);
3119 }
3120
3121 rc = inode_setattr(inode, attr);
3122
3123 if (!rc && (ia_valid & ATTR_MODE))
3124 rc = ext3_acl_chmod(inode);
3125
3126err_out:
3127 ext3_std_error(inode->i_sb, error);
3128 if (!error)
3129 error = rc;
3130 return error;
3131}
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161static int ext3_writepage_trans_blocks(struct inode *inode)
3162{
3163 int bpp = ext3_journal_blocks_per_page(inode);
3164 int indirects = (EXT3_NDIR_BLOCKS % bpp) ? 5 : 3;
3165 int ret;
3166
3167 if (ext3_should_journal_data(inode))
3168 ret = 3 * (bpp + indirects) + 2;
3169 else
3170 ret = 2 * (bpp + indirects) + 2;
3171
3172#ifdef CONFIG_QUOTA
3173
3174
3175 ret += 2*EXT3_QUOTA_TRANS_BLOCKS(inode->i_sb);
3176#endif
3177
3178 return ret;
3179}
3180
3181
3182
3183
3184
3185int ext3_mark_iloc_dirty(handle_t *handle,
3186 struct inode *inode, struct ext3_iloc *iloc)
3187{
3188 int err = 0;
3189
3190
3191 get_bh(iloc->bh);
3192
3193
3194 err = ext3_do_update_inode(handle, inode, iloc);
3195 put_bh(iloc->bh);
3196 return err;
3197}
3198
3199
3200
3201
3202
3203
3204int
3205ext3_reserve_inode_write(handle_t *handle, struct inode *inode,
3206 struct ext3_iloc *iloc)
3207{
3208 int err = 0;
3209 if (handle) {
3210 err = ext3_get_inode_loc(inode, iloc);
3211 if (!err) {
3212 BUFFER_TRACE(iloc->bh, "get_write_access");
3213 err = ext3_journal_get_write_access(handle, iloc->bh);
3214 if (err) {
3215 brelse(iloc->bh);
3216 iloc->bh = NULL;
3217 }
3218 }
3219 }
3220 ext3_std_error(inode->i_sb, err);
3221 return err;
3222}
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245int ext3_mark_inode_dirty(handle_t *handle, struct inode *inode)
3246{
3247 struct ext3_iloc iloc;
3248 int err;
3249
3250 might_sleep();
3251 err = ext3_reserve_inode_write(handle, inode, &iloc);
3252 if (!err)
3253 err = ext3_mark_iloc_dirty(handle, inode, &iloc);
3254 return err;
3255}
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271void ext3_dirty_inode(struct inode *inode)
3272{
3273 handle_t *current_handle = ext3_journal_current_handle();
3274 handle_t *handle;
3275
3276 handle = ext3_journal_start(inode, 2);
3277 if (IS_ERR(handle))
3278 goto out;
3279 if (current_handle &&
3280 current_handle->h_transaction != handle->h_transaction) {
3281
3282 printk(KERN_EMERG "%s: transactions do not match!\n",
3283 __func__);
3284 } else {
3285 jbd_debug(5, "marking dirty. outer handle=%p\n",
3286 current_handle);
3287 ext3_mark_inode_dirty(handle, inode);
3288 }
3289 ext3_journal_stop(handle);
3290out:
3291 return;
3292}
3293
3294#if 0
3295
3296
3297
3298
3299
3300
3301
3302static int ext3_pin_inode(handle_t *handle, struct inode *inode)
3303{
3304 struct ext3_iloc iloc;
3305
3306 int err = 0;
3307 if (handle) {
3308 err = ext3_get_inode_loc(inode, &iloc);
3309 if (!err) {
3310 BUFFER_TRACE(iloc.bh, "get_write_access");
3311 err = journal_get_write_access(handle, iloc.bh);
3312 if (!err)
3313 err = ext3_journal_dirty_metadata(handle,
3314 iloc.bh);
3315 brelse(iloc.bh);
3316 }
3317 }
3318 ext3_std_error(inode->i_sb, err);
3319 return err;
3320}
3321#endif
3322
3323int ext3_change_inode_journal_flag(struct inode *inode, int val)
3324{
3325 journal_t *journal;
3326 handle_t *handle;
3327 int err;
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339 journal = EXT3_JOURNAL(inode);
3340 if (is_journal_aborted(journal))
3341 return -EROFS;
3342
3343 journal_lock_updates(journal);
3344 journal_flush(journal);
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354 if (val)
3355 EXT3_I(inode)->i_flags |= EXT3_JOURNAL_DATA_FL;
3356 else
3357 EXT3_I(inode)->i_flags &= ~EXT3_JOURNAL_DATA_FL;
3358 ext3_set_aops(inode);
3359
3360 journal_unlock_updates(journal);
3361
3362
3363
3364 handle = ext3_journal_start(inode, 1);
3365 if (IS_ERR(handle))
3366 return PTR_ERR(handle);
3367
3368 err = ext3_mark_inode_dirty(handle, inode);
3369 handle->h_sync = 1;
3370 ext3_journal_stop(handle);
3371 ext3_std_error(inode->i_sb, err);
3372
3373 return err;
3374}
3375