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/smp_lock.h>
26#include <linux/time.h>
27#include <linux/highuid.h>
28#include <linux/pagemap.h>
29#include <linux/quotaops.h>
30#include <linux/module.h>
31#include <linux/writeback.h>
32#include <linux/buffer_head.h>
33#include <linux/mpage.h>
34#include <linux/fiemap.h>
35#include "ext2.h"
36#include "acl.h"
37#include "xip.h"
38
39MODULE_AUTHOR("Remy Card and others");
40MODULE_DESCRIPTION("Second Extended Filesystem");
41MODULE_LICENSE("GPL");
42
43static int ext2_update_inode(struct inode * inode, int do_sync);
44
45
46
47
48static inline int ext2_inode_is_fast_symlink(struct inode *inode)
49{
50 int ea_blocks = EXT2_I(inode)->i_file_acl ?
51 (inode->i_sb->s_blocksize >> 9) : 0;
52
53 return (S_ISLNK(inode->i_mode) &&
54 inode->i_blocks - ea_blocks == 0);
55}
56
57
58
59
60void ext2_delete_inode (struct inode * inode)
61{
62 truncate_inode_pages(&inode->i_data, 0);
63
64 if (is_bad_inode(inode))
65 goto no_delete;
66 EXT2_I(inode)->i_dtime = get_seconds();
67 mark_inode_dirty(inode);
68 ext2_update_inode(inode, inode_needs_sync(inode));
69
70 inode->i_size = 0;
71 if (inode->i_blocks)
72 ext2_truncate (inode);
73 ext2_free_inode (inode);
74
75 return;
76no_delete:
77 clear_inode(inode);
78}
79
80typedef struct {
81 __le32 *p;
82 __le32 key;
83 struct buffer_head *bh;
84} Indirect;
85
86static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
87{
88 p->key = *(p->p = v);
89 p->bh = bh;
90}
91
92static inline int verify_chain(Indirect *from, Indirect *to)
93{
94 while (from <= to && from->key == *from->p)
95 from++;
96 return (from > to);
97}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129static int ext2_block_to_path(struct inode *inode,
130 long i_block, int offsets[4], int *boundary)
131{
132 int ptrs = EXT2_ADDR_PER_BLOCK(inode->i_sb);
133 int ptrs_bits = EXT2_ADDR_PER_BLOCK_BITS(inode->i_sb);
134 const long direct_blocks = EXT2_NDIR_BLOCKS,
135 indirect_blocks = ptrs,
136 double_blocks = (1 << (ptrs_bits * 2));
137 int n = 0;
138 int final = 0;
139
140 if (i_block < 0) {
141 ext2_warning (inode->i_sb, "ext2_block_to_path", "block < 0");
142 } else if (i_block < direct_blocks) {
143 offsets[n++] = i_block;
144 final = direct_blocks;
145 } else if ( (i_block -= direct_blocks) < indirect_blocks) {
146 offsets[n++] = EXT2_IND_BLOCK;
147 offsets[n++] = i_block;
148 final = ptrs;
149 } else if ((i_block -= indirect_blocks) < double_blocks) {
150 offsets[n++] = EXT2_DIND_BLOCK;
151 offsets[n++] = i_block >> ptrs_bits;
152 offsets[n++] = i_block & (ptrs - 1);
153 final = ptrs;
154 } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
155 offsets[n++] = EXT2_TIND_BLOCK;
156 offsets[n++] = i_block >> (ptrs_bits * 2);
157 offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
158 offsets[n++] = i_block & (ptrs - 1);
159 final = ptrs;
160 } else {
161 ext2_warning (inode->i_sb, "ext2_block_to_path", "block > big");
162 }
163 if (boundary)
164 *boundary = final - 1 - (i_block & (ptrs - 1));
165
166 return n;
167}
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198static Indirect *ext2_get_branch(struct inode *inode,
199 int depth,
200 int *offsets,
201 Indirect chain[4],
202 int *err)
203{
204 struct super_block *sb = inode->i_sb;
205 Indirect *p = chain;
206 struct buffer_head *bh;
207
208 *err = 0;
209
210 add_chain (chain, NULL, EXT2_I(inode)->i_data + *offsets);
211 if (!p->key)
212 goto no_block;
213 while (--depth) {
214 bh = sb_bread(sb, le32_to_cpu(p->key));
215 if (!bh)
216 goto failure;
217 read_lock(&EXT2_I(inode)->i_meta_lock);
218 if (!verify_chain(chain, p))
219 goto changed;
220 add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
221 read_unlock(&EXT2_I(inode)->i_meta_lock);
222 if (!p->key)
223 goto no_block;
224 }
225 return NULL;
226
227changed:
228 read_unlock(&EXT2_I(inode)->i_meta_lock);
229 brelse(bh);
230 *err = -EAGAIN;
231 goto no_block;
232failure:
233 *err = -EIO;
234no_block:
235 return p;
236}
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258static ext2_fsblk_t ext2_find_near(struct inode *inode, Indirect *ind)
259{
260 struct ext2_inode_info *ei = EXT2_I(inode);
261 __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
262 __le32 *p;
263 ext2_fsblk_t bg_start;
264 ext2_fsblk_t colour;
265
266
267 for (p = ind->p - 1; p >= start; p--)
268 if (*p)
269 return le32_to_cpu(*p);
270
271
272 if (ind->bh)
273 return ind->bh->b_blocknr;
274
275
276
277
278
279 bg_start = ext2_group_first_block_no(inode->i_sb, ei->i_block_group);
280 colour = (current->pid % 16) *
281 (EXT2_BLOCKS_PER_GROUP(inode->i_sb) / 16);
282 return bg_start + colour;
283}
284
285
286
287
288
289
290
291
292
293
294static inline ext2_fsblk_t ext2_find_goal(struct inode *inode, long block,
295 Indirect *partial)
296{
297 struct ext2_block_alloc_info *block_i;
298
299 block_i = EXT2_I(inode)->i_block_alloc_info;
300
301
302
303
304
305 if (block_i && (block == block_i->last_alloc_logical_block + 1)
306 && (block_i->last_alloc_physical_block != 0)) {
307 return block_i->last_alloc_physical_block + 1;
308 }
309
310 return ext2_find_near(inode, partial);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325static int
326ext2_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
327 int blocks_to_boundary)
328{
329 unsigned long count = 0;
330
331
332
333
334
335 if (k > 0) {
336
337 if (blks < blocks_to_boundary + 1)
338 count += blks;
339 else
340 count += blocks_to_boundary + 1;
341 return count;
342 }
343
344 count++;
345 while (count < blks && count <= blocks_to_boundary
346 && le32_to_cpu(*(branch[0].p + count)) == 0) {
347 count++;
348 }
349 return count;
350}
351
352
353
354
355
356
357
358
359
360
361
362static int ext2_alloc_blocks(struct inode *inode,
363 ext2_fsblk_t goal, int indirect_blks, int blks,
364 ext2_fsblk_t new_blocks[4], int *err)
365{
366 int target, i;
367 unsigned long count = 0;
368 int index = 0;
369 ext2_fsblk_t current_block = 0;
370 int ret = 0;
371
372
373
374
375
376
377
378
379
380 target = blks + indirect_blks;
381
382 while (1) {
383 count = target;
384
385 current_block = ext2_new_blocks(inode,goal,&count,err);
386 if (*err)
387 goto failed_out;
388
389 target -= count;
390
391 while (index < indirect_blks && count) {
392 new_blocks[index++] = current_block++;
393 count--;
394 }
395
396 if (count > 0)
397 break;
398 }
399
400
401 new_blocks[index] = current_block;
402
403
404 ret = count;
405 *err = 0;
406 return ret;
407failed_out:
408 for (i = 0; i <index; i++)
409 ext2_free_blocks(inode, new_blocks[i], 1);
410 return ret;
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438static int ext2_alloc_branch(struct inode *inode,
439 int indirect_blks, int *blks, ext2_fsblk_t goal,
440 int *offsets, Indirect *branch)
441{
442 int blocksize = inode->i_sb->s_blocksize;
443 int i, n = 0;
444 int err = 0;
445 struct buffer_head *bh;
446 int num;
447 ext2_fsblk_t new_blocks[4];
448 ext2_fsblk_t current_block;
449
450 num = ext2_alloc_blocks(inode, goal, indirect_blks,
451 *blks, new_blocks, &err);
452 if (err)
453 return err;
454
455 branch[0].key = cpu_to_le32(new_blocks[0]);
456
457
458
459 for (n = 1; n <= indirect_blks; n++) {
460
461
462
463
464
465 bh = sb_getblk(inode->i_sb, new_blocks[n-1]);
466 branch[n].bh = bh;
467 lock_buffer(bh);
468 memset(bh->b_data, 0, blocksize);
469 branch[n].p = (__le32 *) bh->b_data + offsets[n];
470 branch[n].key = cpu_to_le32(new_blocks[n]);
471 *branch[n].p = branch[n].key;
472 if ( n == indirect_blks) {
473 current_block = new_blocks[n];
474
475
476
477
478
479 for (i=1; i < num; i++)
480 *(branch[n].p + i) = cpu_to_le32(++current_block);
481 }
482 set_buffer_uptodate(bh);
483 unlock_buffer(bh);
484 mark_buffer_dirty_inode(bh, inode);
485
486
487
488
489 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
490 sync_dirty_buffer(bh);
491 }
492 *blks = num;
493 return err;
494}
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510static void ext2_splice_branch(struct inode *inode,
511 long block, Indirect *where, int num, int blks)
512{
513 int i;
514 struct ext2_block_alloc_info *block_i;
515 ext2_fsblk_t current_block;
516
517 block_i = EXT2_I(inode)->i_block_alloc_info;
518
519
520
521
522 *where->p = where->key;
523
524
525
526
527
528 if (num == 0 && blks > 1) {
529 current_block = le32_to_cpu(where->key) + 1;
530 for (i = 1; i < blks; i++)
531 *(where->p + i ) = cpu_to_le32(current_block++);
532 }
533
534
535
536
537
538
539 if (block_i) {
540 block_i->last_alloc_logical_block = block + blks - 1;
541 block_i->last_alloc_physical_block =
542 le32_to_cpu(where[num].key) + blks - 1;
543 }
544
545
546
547
548 if (where->bh)
549 mark_buffer_dirty_inode(where->bh, inode);
550
551 inode->i_ctime = CURRENT_TIME_SEC;
552 mark_inode_dirty(inode);
553}
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573static int ext2_get_blocks(struct inode *inode,
574 sector_t iblock, unsigned long maxblocks,
575 struct buffer_head *bh_result,
576 int create)
577{
578 int err = -EIO;
579 int offsets[4];
580 Indirect chain[4];
581 Indirect *partial;
582 ext2_fsblk_t goal;
583 int indirect_blks;
584 int blocks_to_boundary = 0;
585 int depth;
586 struct ext2_inode_info *ei = EXT2_I(inode);
587 int count = 0;
588 ext2_fsblk_t first_block = 0;
589
590 depth = ext2_block_to_path(inode,iblock,offsets,&blocks_to_boundary);
591
592 if (depth == 0)
593 return (err);
594reread:
595 partial = ext2_get_branch(inode, depth, offsets, chain, &err);
596
597
598 if (!partial) {
599 first_block = le32_to_cpu(chain[depth - 1].key);
600 clear_buffer_new(bh_result);
601 count++;
602
603 while (count < maxblocks && count <= blocks_to_boundary) {
604 ext2_fsblk_t blk;
605
606 if (!verify_chain(chain, partial)) {
607
608
609
610
611
612
613 count = 0;
614 goto changed;
615 }
616 blk = le32_to_cpu(*(chain[depth-1].p + count));
617 if (blk == first_block + count)
618 count++;
619 else
620 break;
621 }
622 goto got_it;
623 }
624
625
626 if (!create || err == -EIO)
627 goto cleanup;
628
629 mutex_lock(&ei->truncate_mutex);
630
631
632
633
634
635 if (S_ISREG(inode->i_mode) && (!ei->i_block_alloc_info))
636 ext2_init_block_alloc_info(inode);
637
638 goal = ext2_find_goal(inode, iblock, partial);
639
640
641 indirect_blks = (chain + depth) - partial - 1;
642
643
644
645
646 count = ext2_blks_to_allocate(partial, indirect_blks,
647 maxblocks, blocks_to_boundary);
648
649
650
651 err = ext2_alloc_branch(inode, indirect_blks, &count, goal,
652 offsets + (partial - chain), partial);
653
654 if (err) {
655 mutex_unlock(&ei->truncate_mutex);
656 goto cleanup;
657 }
658
659 if (ext2_use_xip(inode->i_sb)) {
660
661
662
663 err = ext2_clear_xip_target (inode,
664 le32_to_cpu(chain[depth-1].key));
665 if (err) {
666 mutex_unlock(&ei->truncate_mutex);
667 goto cleanup;
668 }
669 }
670
671 ext2_splice_branch(inode, iblock, partial, indirect_blks, count);
672 mutex_unlock(&ei->truncate_mutex);
673 set_buffer_new(bh_result);
674got_it:
675 map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key));
676 if (count > blocks_to_boundary)
677 set_buffer_boundary(bh_result);
678 err = count;
679
680 partial = chain + depth - 1;
681cleanup:
682 while (partial > chain) {
683 brelse(partial->bh);
684 partial--;
685 }
686 return err;
687changed:
688 while (partial > chain) {
689 brelse(partial->bh);
690 partial--;
691 }
692 goto reread;
693}
694
695int ext2_get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
696{
697 unsigned max_blocks = bh_result->b_size >> inode->i_blkbits;
698 int ret = ext2_get_blocks(inode, iblock, max_blocks,
699 bh_result, create);
700 if (ret > 0) {
701 bh_result->b_size = (ret << inode->i_blkbits);
702 ret = 0;
703 }
704 return ret;
705
706}
707
708int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
709 u64 start, u64 len)
710{
711 return generic_block_fiemap(inode, fieinfo, start, len,
712 ext2_get_block);
713}
714
715static int ext2_writepage(struct page *page, struct writeback_control *wbc)
716{
717 return block_write_full_page(page, ext2_get_block, wbc);
718}
719
720static int ext2_readpage(struct file *file, struct page *page)
721{
722 return mpage_readpage(page, ext2_get_block);
723}
724
725static int
726ext2_readpages(struct file *file, struct address_space *mapping,
727 struct list_head *pages, unsigned nr_pages)
728{
729 return mpage_readpages(mapping, pages, nr_pages, ext2_get_block);
730}
731
732int __ext2_write_begin(struct file *file, struct address_space *mapping,
733 loff_t pos, unsigned len, unsigned flags,
734 struct page **pagep, void **fsdata)
735{
736 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
737 ext2_get_block);
738}
739
740static int
741ext2_write_begin(struct file *file, struct address_space *mapping,
742 loff_t pos, unsigned len, unsigned flags,
743 struct page **pagep, void **fsdata)
744{
745 *pagep = NULL;
746 return __ext2_write_begin(file, mapping, pos, len, flags, pagep,fsdata);
747}
748
749static int
750ext2_nobh_write_begin(struct file *file, struct address_space *mapping,
751 loff_t pos, unsigned len, unsigned flags,
752 struct page **pagep, void **fsdata)
753{
754
755
756
757
758
759 return nobh_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
760 ext2_get_block);
761}
762
763static int ext2_nobh_writepage(struct page *page,
764 struct writeback_control *wbc)
765{
766 return nobh_writepage(page, ext2_get_block, wbc);
767}
768
769static sector_t ext2_bmap(struct address_space *mapping, sector_t block)
770{
771 return generic_block_bmap(mapping,block,ext2_get_block);
772}
773
774static ssize_t
775ext2_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
776 loff_t offset, unsigned long nr_segs)
777{
778 struct file *file = iocb->ki_filp;
779 struct inode *inode = file->f_mapping->host;
780
781 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
782 offset, nr_segs, ext2_get_block, NULL);
783}
784
785static int
786ext2_writepages(struct address_space *mapping, struct writeback_control *wbc)
787{
788 return mpage_writepages(mapping, wbc, ext2_get_block);
789}
790
791const struct address_space_operations ext2_aops = {
792 .readpage = ext2_readpage,
793 .readpages = ext2_readpages,
794 .writepage = ext2_writepage,
795 .sync_page = block_sync_page,
796 .write_begin = ext2_write_begin,
797 .write_end = generic_write_end,
798 .bmap = ext2_bmap,
799 .direct_IO = ext2_direct_IO,
800 .writepages = ext2_writepages,
801 .migratepage = buffer_migrate_page,
802 .is_partially_uptodate = block_is_partially_uptodate,
803};
804
805const struct address_space_operations ext2_aops_xip = {
806 .bmap = ext2_bmap,
807 .get_xip_mem = ext2_get_xip_mem,
808};
809
810const struct address_space_operations ext2_nobh_aops = {
811 .readpage = ext2_readpage,
812 .readpages = ext2_readpages,
813 .writepage = ext2_nobh_writepage,
814 .sync_page = block_sync_page,
815 .write_begin = ext2_nobh_write_begin,
816 .write_end = nobh_write_end,
817 .bmap = ext2_bmap,
818 .direct_IO = ext2_direct_IO,
819 .writepages = ext2_writepages,
820 .migratepage = buffer_migrate_page,
821};
822
823
824
825
826
827
828static inline int all_zeroes(__le32 *p, __le32 *q)
829{
830 while (p < q)
831 if (*p++)
832 return 0;
833 return 1;
834}
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870static Indirect *ext2_find_shared(struct inode *inode,
871 int depth,
872 int offsets[4],
873 Indirect chain[4],
874 __le32 *top)
875{
876 Indirect *partial, *p;
877 int k, err;
878
879 *top = 0;
880 for (k = depth; k > 1 && !offsets[k-1]; k--)
881 ;
882 partial = ext2_get_branch(inode, k, offsets, chain, &err);
883 if (!partial)
884 partial = chain + k-1;
885
886
887
888
889 write_lock(&EXT2_I(inode)->i_meta_lock);
890 if (!partial->key && *partial->p) {
891 write_unlock(&EXT2_I(inode)->i_meta_lock);
892 goto no_top;
893 }
894 for (p=partial; p>chain && all_zeroes((__le32*)p->bh->b_data,p->p); p--)
895 ;
896
897
898
899
900
901
902 if (p == chain + k - 1 && p > chain) {
903 p->p--;
904 } else {
905 *top = *p->p;
906 *p->p = 0;
907 }
908 write_unlock(&EXT2_I(inode)->i_meta_lock);
909
910 while(partial > p)
911 {
912 brelse(partial->bh);
913 partial--;
914 }
915no_top:
916 return partial;
917}
918
919
920
921
922
923
924
925
926
927
928
929static inline void ext2_free_data(struct inode *inode, __le32 *p, __le32 *q)
930{
931 unsigned long block_to_free = 0, count = 0;
932 unsigned long nr;
933
934 for ( ; p < q ; p++) {
935 nr = le32_to_cpu(*p);
936 if (nr) {
937 *p = 0;
938
939 if (count == 0)
940 goto free_this;
941 else if (block_to_free == nr - count)
942 count++;
943 else {
944 mark_inode_dirty(inode);
945 ext2_free_blocks (inode, block_to_free, count);
946 free_this:
947 block_to_free = nr;
948 count = 1;
949 }
950 }
951 }
952 if (count > 0) {
953 mark_inode_dirty(inode);
954 ext2_free_blocks (inode, block_to_free, count);
955 }
956}
957
958
959
960
961
962
963
964
965
966
967
968
969static void ext2_free_branches(struct inode *inode, __le32 *p, __le32 *q, int depth)
970{
971 struct buffer_head * bh;
972 unsigned long nr;
973
974 if (depth--) {
975 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
976 for ( ; p < q ; p++) {
977 nr = le32_to_cpu(*p);
978 if (!nr)
979 continue;
980 *p = 0;
981 bh = sb_bread(inode->i_sb, nr);
982
983
984
985
986 if (!bh) {
987 ext2_error(inode->i_sb, "ext2_free_branches",
988 "Read failure, inode=%ld, block=%ld",
989 inode->i_ino, nr);
990 continue;
991 }
992 ext2_free_branches(inode,
993 (__le32*)bh->b_data,
994 (__le32*)bh->b_data + addr_per_block,
995 depth);
996 bforget(bh);
997 ext2_free_blocks(inode, nr, 1);
998 mark_inode_dirty(inode);
999 }
1000 } else
1001 ext2_free_data(inode, p, q);
1002}
1003
1004void ext2_truncate(struct inode *inode)
1005{
1006 __le32 *i_data = EXT2_I(inode)->i_data;
1007 struct ext2_inode_info *ei = EXT2_I(inode);
1008 int addr_per_block = EXT2_ADDR_PER_BLOCK(inode->i_sb);
1009 int offsets[4];
1010 Indirect chain[4];
1011 Indirect *partial;
1012 __le32 nr = 0;
1013 int n;
1014 long iblock;
1015 unsigned blocksize;
1016
1017 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1018 S_ISLNK(inode->i_mode)))
1019 return;
1020 if (ext2_inode_is_fast_symlink(inode))
1021 return;
1022 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1023 return;
1024
1025 blocksize = inode->i_sb->s_blocksize;
1026 iblock = (inode->i_size + blocksize-1)
1027 >> EXT2_BLOCK_SIZE_BITS(inode->i_sb);
1028
1029 if (mapping_is_xip(inode->i_mapping))
1030 xip_truncate_page(inode->i_mapping, inode->i_size);
1031 else if (test_opt(inode->i_sb, NOBH))
1032 nobh_truncate_page(inode->i_mapping,
1033 inode->i_size, ext2_get_block);
1034 else
1035 block_truncate_page(inode->i_mapping,
1036 inode->i_size, ext2_get_block);
1037
1038 n = ext2_block_to_path(inode, iblock, offsets, NULL);
1039 if (n == 0)
1040 return;
1041
1042
1043
1044
1045
1046 mutex_lock(&ei->truncate_mutex);
1047
1048 if (n == 1) {
1049 ext2_free_data(inode, i_data+offsets[0],
1050 i_data + EXT2_NDIR_BLOCKS);
1051 goto do_indirects;
1052 }
1053
1054 partial = ext2_find_shared(inode, n, offsets, chain, &nr);
1055
1056 if (nr) {
1057 if (partial == chain)
1058 mark_inode_dirty(inode);
1059 else
1060 mark_buffer_dirty_inode(partial->bh, inode);
1061 ext2_free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
1062 }
1063
1064 while (partial > chain) {
1065 ext2_free_branches(inode,
1066 partial->p + 1,
1067 (__le32*)partial->bh->b_data+addr_per_block,
1068 (chain+n-1) - partial);
1069 mark_buffer_dirty_inode(partial->bh, inode);
1070 brelse (partial->bh);
1071 partial--;
1072 }
1073do_indirects:
1074
1075 switch (offsets[0]) {
1076 default:
1077 nr = i_data[EXT2_IND_BLOCK];
1078 if (nr) {
1079 i_data[EXT2_IND_BLOCK] = 0;
1080 mark_inode_dirty(inode);
1081 ext2_free_branches(inode, &nr, &nr+1, 1);
1082 }
1083 case EXT2_IND_BLOCK:
1084 nr = i_data[EXT2_DIND_BLOCK];
1085 if (nr) {
1086 i_data[EXT2_DIND_BLOCK] = 0;
1087 mark_inode_dirty(inode);
1088 ext2_free_branches(inode, &nr, &nr+1, 2);
1089 }
1090 case EXT2_DIND_BLOCK:
1091 nr = i_data[EXT2_TIND_BLOCK];
1092 if (nr) {
1093 i_data[EXT2_TIND_BLOCK] = 0;
1094 mark_inode_dirty(inode);
1095 ext2_free_branches(inode, &nr, &nr+1, 3);
1096 }
1097 case EXT2_TIND_BLOCK:
1098 ;
1099 }
1100
1101 ext2_discard_reservation(inode);
1102
1103 mutex_unlock(&ei->truncate_mutex);
1104 inode->i_mtime = inode->i_ctime = CURRENT_TIME_SEC;
1105 if (inode_needs_sync(inode)) {
1106 sync_mapping_buffers(inode->i_mapping);
1107 ext2_sync_inode (inode);
1108 } else {
1109 mark_inode_dirty(inode);
1110 }
1111}
1112
1113static struct ext2_inode *ext2_get_inode(struct super_block *sb, ino_t ino,
1114 struct buffer_head **p)
1115{
1116 struct buffer_head * bh;
1117 unsigned long block_group;
1118 unsigned long block;
1119 unsigned long offset;
1120 struct ext2_group_desc * gdp;
1121
1122 *p = NULL;
1123 if ((ino != EXT2_ROOT_INO && ino < EXT2_FIRST_INO(sb)) ||
1124 ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
1125 goto Einval;
1126
1127 block_group = (ino - 1) / EXT2_INODES_PER_GROUP(sb);
1128 gdp = ext2_get_group_desc(sb, block_group, NULL);
1129 if (!gdp)
1130 goto Egdp;
1131
1132
1133
1134 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(sb)) * EXT2_INODE_SIZE(sb);
1135 block = le32_to_cpu(gdp->bg_inode_table) +
1136 (offset >> EXT2_BLOCK_SIZE_BITS(sb));
1137 if (!(bh = sb_bread(sb, block)))
1138 goto Eio;
1139
1140 *p = bh;
1141 offset &= (EXT2_BLOCK_SIZE(sb) - 1);
1142 return (struct ext2_inode *) (bh->b_data + offset);
1143
1144Einval:
1145 ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
1146 (unsigned long) ino);
1147 return ERR_PTR(-EINVAL);
1148Eio:
1149 ext2_error(sb, "ext2_get_inode",
1150 "unable to read inode block - inode=%lu, block=%lu",
1151 (unsigned long) ino, block);
1152Egdp:
1153 return ERR_PTR(-EIO);
1154}
1155
1156void ext2_set_inode_flags(struct inode *inode)
1157{
1158 unsigned int flags = EXT2_I(inode)->i_flags;
1159
1160 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
1161 if (flags & EXT2_SYNC_FL)
1162 inode->i_flags |= S_SYNC;
1163 if (flags & EXT2_APPEND_FL)
1164 inode->i_flags |= S_APPEND;
1165 if (flags & EXT2_IMMUTABLE_FL)
1166 inode->i_flags |= S_IMMUTABLE;
1167 if (flags & EXT2_NOATIME_FL)
1168 inode->i_flags |= S_NOATIME;
1169 if (flags & EXT2_DIRSYNC_FL)
1170 inode->i_flags |= S_DIRSYNC;
1171}
1172
1173
1174void ext2_get_inode_flags(struct ext2_inode_info *ei)
1175{
1176 unsigned int flags = ei->vfs_inode.i_flags;
1177
1178 ei->i_flags &= ~(EXT2_SYNC_FL|EXT2_APPEND_FL|
1179 EXT2_IMMUTABLE_FL|EXT2_NOATIME_FL|EXT2_DIRSYNC_FL);
1180 if (flags & S_SYNC)
1181 ei->i_flags |= EXT2_SYNC_FL;
1182 if (flags & S_APPEND)
1183 ei->i_flags |= EXT2_APPEND_FL;
1184 if (flags & S_IMMUTABLE)
1185 ei->i_flags |= EXT2_IMMUTABLE_FL;
1186 if (flags & S_NOATIME)
1187 ei->i_flags |= EXT2_NOATIME_FL;
1188 if (flags & S_DIRSYNC)
1189 ei->i_flags |= EXT2_DIRSYNC_FL;
1190}
1191
1192struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
1193{
1194 struct ext2_inode_info *ei;
1195 struct buffer_head * bh;
1196 struct ext2_inode *raw_inode;
1197 struct inode *inode;
1198 long ret = -EIO;
1199 int n;
1200
1201 inode = iget_locked(sb, ino);
1202 if (!inode)
1203 return ERR_PTR(-ENOMEM);
1204 if (!(inode->i_state & I_NEW))
1205 return inode;
1206
1207 ei = EXT2_I(inode);
1208#ifdef CONFIG_EXT2_FS_POSIX_ACL
1209 ei->i_acl = EXT2_ACL_NOT_CACHED;
1210 ei->i_default_acl = EXT2_ACL_NOT_CACHED;
1211#endif
1212 ei->i_block_alloc_info = NULL;
1213
1214 raw_inode = ext2_get_inode(inode->i_sb, ino, &bh);
1215 if (IS_ERR(raw_inode)) {
1216 ret = PTR_ERR(raw_inode);
1217 goto bad_inode;
1218 }
1219
1220 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
1221 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
1222 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
1223 if (!(test_opt (inode->i_sb, NO_UID32))) {
1224 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
1225 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
1226 }
1227 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
1228 inode->i_size = le32_to_cpu(raw_inode->i_size);
1229 inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
1230 inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
1231 inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
1232 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
1233 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
1234
1235
1236
1237
1238
1239 if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
1240
1241 brelse (bh);
1242 ret = -ESTALE;
1243 goto bad_inode;
1244 }
1245 inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
1246 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
1247 ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
1248 ei->i_frag_no = raw_inode->i_frag;
1249 ei->i_frag_size = raw_inode->i_fsize;
1250 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
1251 ei->i_dir_acl = 0;
1252 if (S_ISREG(inode->i_mode))
1253 inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
1254 else
1255 ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
1256 ei->i_dtime = 0;
1257 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
1258 ei->i_state = 0;
1259 ei->i_block_group = (ino - 1) / EXT2_INODES_PER_GROUP(inode->i_sb);
1260 ei->i_dir_start_lookup = 0;
1261
1262
1263
1264
1265
1266 for (n = 0; n < EXT2_N_BLOCKS; n++)
1267 ei->i_data[n] = raw_inode->i_block[n];
1268
1269 if (S_ISREG(inode->i_mode)) {
1270 inode->i_op = &ext2_file_inode_operations;
1271 if (ext2_use_xip(inode->i_sb)) {
1272 inode->i_mapping->a_ops = &ext2_aops_xip;
1273 inode->i_fop = &ext2_xip_file_operations;
1274 } else if (test_opt(inode->i_sb, NOBH)) {
1275 inode->i_mapping->a_ops = &ext2_nobh_aops;
1276 inode->i_fop = &ext2_file_operations;
1277 } else {
1278 inode->i_mapping->a_ops = &ext2_aops;
1279 inode->i_fop = &ext2_file_operations;
1280 }
1281 } else if (S_ISDIR(inode->i_mode)) {
1282 inode->i_op = &ext2_dir_inode_operations;
1283 inode->i_fop = &ext2_dir_operations;
1284 if (test_opt(inode->i_sb, NOBH))
1285 inode->i_mapping->a_ops = &ext2_nobh_aops;
1286 else
1287 inode->i_mapping->a_ops = &ext2_aops;
1288 } else if (S_ISLNK(inode->i_mode)) {
1289 if (ext2_inode_is_fast_symlink(inode))
1290 inode->i_op = &ext2_fast_symlink_inode_operations;
1291 else {
1292 inode->i_op = &ext2_symlink_inode_operations;
1293 if (test_opt(inode->i_sb, NOBH))
1294 inode->i_mapping->a_ops = &ext2_nobh_aops;
1295 else
1296 inode->i_mapping->a_ops = &ext2_aops;
1297 }
1298 } else {
1299 inode->i_op = &ext2_special_inode_operations;
1300 if (raw_inode->i_block[0])
1301 init_special_inode(inode, inode->i_mode,
1302 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
1303 else
1304 init_special_inode(inode, inode->i_mode,
1305 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
1306 }
1307 brelse (bh);
1308 ext2_set_inode_flags(inode);
1309 unlock_new_inode(inode);
1310 return inode;
1311
1312bad_inode:
1313 iget_failed(inode);
1314 return ERR_PTR(ret);
1315}
1316
1317static int ext2_update_inode(struct inode * inode, int do_sync)
1318{
1319 struct ext2_inode_info *ei = EXT2_I(inode);
1320 struct super_block *sb = inode->i_sb;
1321 ino_t ino = inode->i_ino;
1322 uid_t uid = inode->i_uid;
1323 gid_t gid = inode->i_gid;
1324 struct buffer_head * bh;
1325 struct ext2_inode * raw_inode = ext2_get_inode(sb, ino, &bh);
1326 int n;
1327 int err = 0;
1328
1329 if (IS_ERR(raw_inode))
1330 return -EIO;
1331
1332
1333
1334 if (ei->i_state & EXT2_STATE_NEW)
1335 memset(raw_inode, 0, EXT2_SB(sb)->s_inode_size);
1336
1337 ext2_get_inode_flags(ei);
1338 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
1339 if (!(test_opt(sb, NO_UID32))) {
1340 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(uid));
1341 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(gid));
1342
1343
1344
1345
1346 if (!ei->i_dtime) {
1347 raw_inode->i_uid_high = cpu_to_le16(high_16_bits(uid));
1348 raw_inode->i_gid_high = cpu_to_le16(high_16_bits(gid));
1349 } else {
1350 raw_inode->i_uid_high = 0;
1351 raw_inode->i_gid_high = 0;
1352 }
1353 } else {
1354 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(uid));
1355 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(gid));
1356 raw_inode->i_uid_high = 0;
1357 raw_inode->i_gid_high = 0;
1358 }
1359 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
1360 raw_inode->i_size = cpu_to_le32(inode->i_size);
1361 raw_inode->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1362 raw_inode->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1363 raw_inode->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1364
1365 raw_inode->i_blocks = cpu_to_le32(inode->i_blocks);
1366 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
1367 raw_inode->i_flags = cpu_to_le32(ei->i_flags);
1368 raw_inode->i_faddr = cpu_to_le32(ei->i_faddr);
1369 raw_inode->i_frag = ei->i_frag_no;
1370 raw_inode->i_fsize = ei->i_frag_size;
1371 raw_inode->i_file_acl = cpu_to_le32(ei->i_file_acl);
1372 if (!S_ISREG(inode->i_mode))
1373 raw_inode->i_dir_acl = cpu_to_le32(ei->i_dir_acl);
1374 else {
1375 raw_inode->i_size_high = cpu_to_le32(inode->i_size >> 32);
1376 if (inode->i_size > 0x7fffffffULL) {
1377 if (!EXT2_HAS_RO_COMPAT_FEATURE(sb,
1378 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) ||
1379 EXT2_SB(sb)->s_es->s_rev_level ==
1380 cpu_to_le32(EXT2_GOOD_OLD_REV)) {
1381
1382
1383
1384 lock_kernel();
1385 ext2_update_dynamic_rev(sb);
1386 EXT2_SET_RO_COMPAT_FEATURE(sb,
1387 EXT2_FEATURE_RO_COMPAT_LARGE_FILE);
1388 unlock_kernel();
1389 ext2_write_super(sb);
1390 }
1391 }
1392 }
1393
1394 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
1395 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1396 if (old_valid_dev(inode->i_rdev)) {
1397 raw_inode->i_block[0] =
1398 cpu_to_le32(old_encode_dev(inode->i_rdev));
1399 raw_inode->i_block[1] = 0;
1400 } else {
1401 raw_inode->i_block[0] = 0;
1402 raw_inode->i_block[1] =
1403 cpu_to_le32(new_encode_dev(inode->i_rdev));
1404 raw_inode->i_block[2] = 0;
1405 }
1406 } else for (n = 0; n < EXT2_N_BLOCKS; n++)
1407 raw_inode->i_block[n] = ei->i_data[n];
1408 mark_buffer_dirty(bh);
1409 if (do_sync) {
1410 sync_dirty_buffer(bh);
1411 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1412 printk ("IO error syncing ext2 inode [%s:%08lx]\n",
1413 sb->s_id, (unsigned long) ino);
1414 err = -EIO;
1415 }
1416 }
1417 ei->i_state &= ~EXT2_STATE_NEW;
1418 brelse (bh);
1419 return err;
1420}
1421
1422int ext2_write_inode(struct inode *inode, int wait)
1423{
1424 return ext2_update_inode(inode, wait);
1425}
1426
1427int ext2_sync_inode(struct inode *inode)
1428{
1429 struct writeback_control wbc = {
1430 .sync_mode = WB_SYNC_ALL,
1431 .nr_to_write = 0,
1432 };
1433 return sync_inode(inode, &wbc);
1434}
1435
1436int ext2_setattr(struct dentry *dentry, struct iattr *iattr)
1437{
1438 struct inode *inode = dentry->d_inode;
1439 int error;
1440
1441 error = inode_change_ok(inode, iattr);
1442 if (error)
1443 return error;
1444 if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
1445 (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
1446 error = DQUOT_TRANSFER(inode, iattr) ? -EDQUOT : 0;
1447 if (error)
1448 return error;
1449 }
1450 error = inode_setattr(inode, iattr);
1451 if (!error && (iattr->ia_valid & ATTR_MODE))
1452 error = ext2_acl_chmod(inode);
1453 return error;
1454}
1455