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