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