-
Notifications
You must be signed in to change notification settings - Fork 0
/
atfs.c
2941 lines (2644 loc) · 81.4 KB
/
atfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
402
403
404
405
406
407
408
409
410
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/slab.h>
#include <linux/pagemap.h>
#include <linux/iversion.h>
#include <linux/buffer_head.h>
#include <linux/mpage.h>
#include <linux/quotaops.h>
#include <linux/cred.h>
#include <linux/blkdev.h>
#include <linux/backing-dev.h>
#include "atfs.h"
static struct vfsmount *atfs_mnt;
const struct file_operations atfs_dir_operations;
const struct file_operations atfs_file_operations;
const struct address_space_operations atfs_aops;
const struct address_space_operations atfs_nobh_aops;
const struct inode_operations atfs_dir_inode_operations;
const struct inode_operations atfs_file_inode_operations;
const struct inode_operations atfs_special_inode_operations;
struct atfs_dir_entry {
__le32 inode; /* Inode number */
__le16 rec_len; /* Directory entry length */
__u8 name_len; /* Name length */
__u8 file_type;
char name[]; /* File name */
};
typedef struct {
__le32 *p;
__le32 key;
struct buffer_head *bh;
} Indirect;
static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v)
{
p->key = *(p->p = v);
p->bh = bh;
}
static inline int verify_chain(Indirect *from, Indirect *to)
{
while (from <= to && from->key == *from->p)
from++;
return (from > to);
}
typedef struct atfs_dir_entry atfs_dirent;
static inline unsigned atfs_chunk_size(struct inode *inode)
{
return inode->i_sb->s_blocksize;
}
static inline void atfs_put_page(struct page *page)
{
kunmap(page);
put_page(page);
}
__attribute__((optimize("O0")))
static struct page * atfs_get_page(struct inode *dir, unsigned long n,
int quiet)
{
struct address_space *mapping = dir->i_mapping;
struct page *page = read_mapping_page(mapping, n, NULL);
if (!IS_ERR(page)) {
kmap(page);
if (unlikely(!PageChecked(page))) {
if (PageError(page))
goto fail;
}
}
return page;
fail:
atfs_put_page(page);
return ERR_PTR(-EIO);
}
/*
* ATFS_DIR_PAD defines the directory entries boundaries
*
* NOTE: It must be a multiple of 4
*/
#define ATFS_DIR_PAD 4
#define ATFS_DIR_ROUND (ATFS_DIR_PAD - 1)
#define ATFS_DIR_REC_LEN(name_len) (((name_len) + 8 + ATFS_DIR_ROUND) & \
~ATFS_DIR_ROUND)
#define ATFS_MAX_REC_LEN ((1<<16)-1)
static inline unsigned atfs_rec_len_from_disk(__le16 dlen)
{
unsigned len = le16_to_cpu(dlen);
#if (PAGE_SIZE >= 65536)
if (len == ATFS_MAX_REC_LEN)
return 1 << 16;
#endif
return len;
}
static int atfs_set_bdev_super(struct super_block *sb, void *data)
{
sb->s_bdev = data;
sb->s_dev = sb->s_bdev->bd_dev;
sb->s_bdi = bdi_get(sb->s_bdev->bd_bdi);
return 0;
}
static int atfs_fill_super(struct super_block *sb, void *data)
{
int blocksize = BLOCK_SIZE;
sb->s_fs_info = data;
/*
* See what the current blocksize for the device is, and
* use that as the blocksize. Otherwise (or if the blocksize
* is smaller than the default) use the default.
* This is important for devices that have a hardware
* sectorsize that is larger than the default.
*/
blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
if (!blocksize) {
/*
* ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
* goto failed_sbi;
*/
}
return set_anon_super(sb, data);
}
struct atfs_group_desc * atfs_get_group_desc(struct super_block * sb,
unsigned int block_group,
struct buffer_head ** bh)
{
unsigned long group_desc;
unsigned long offset;
struct atfs_group_desc * desc;
struct atfs_sb_info *sbi = ATFS_SB(sb);
if (block_group >= sbi->s_groups_count) {
/*
* ext2_error (sb, "ext2_get_group_desc",
* "block_group >= groups_count - "
* "block_group = %d, groups_count = %lu",
* block_group, sbi->s_groups_count);
*
*/
return NULL;
}
group_desc = block_group >> ATFS_DESC_PER_BLOCK_BITS(sb);
offset = block_group & (ATFS_DESC_PER_BLOCK(sb) - 1);
if (!sbi->s_group_desc[group_desc]) {
/*
* ext2_error (sb, "ext2_get_group_desc",
* "Group descriptor not loaded - "
* "block_group = %d, group_desc = %lu, desc = %lu",
* block_group, group_desc, offset);
*/
return NULL;
}
desc = (struct atfs_group_desc *) sbi->s_group_desc[group_desc]->b_data;
if (bh)
*bh = sbi->s_group_desc[group_desc];
return desc + offset;
}
static struct atfs_inode *atfs_get_inode(struct super_block *sb, ino_t ino,
struct buffer_head **p)
{
struct buffer_head * bh;
unsigned long block_group;
unsigned long block;
unsigned long offset;
struct atfs_group_desc * gdp;
*p = NULL;
if ((ino != ATFS_ROOT_INO && ino < ATFS_FIRST_INO(sb)) ||
ino > le32_to_cpu(ATFS_SB(sb)->s_es->s_inodes_count))
goto Einval;
block_group = (ino - 1) / ATFS_INODES_PER_GROUP(sb);
gdp = atfs_get_group_desc(sb, block_group, NULL);
if (!gdp)
goto Egdp;
/*
* Figure out the offset within the block group inode table
*/
offset = ((ino - 1) % ATFS_INODES_PER_GROUP(sb)) * ATFS_INODE_SIZE(sb);
block = le32_to_cpu(gdp->bg_inode_table) +
(offset >> ATFS_BLOCK_SIZE_BITS(sb));
if (!(bh = sb_bread(sb, block)))
goto Eio;
*p = bh;
offset &= (ATFS_BLOCK_SIZE(sb) - 1);
return (struct atfs_inode *) (bh->b_data + offset);
Einval:
/*
* ext2_error(sb, "ext2_get_inode", "bad inode number: %lu",
* (unsigned long) ino);
*/
return ERR_PTR(-EINVAL);
Eio:
/*
* ext2_error(sb, "ext2_get_inode",
* "unable to read inode block - inode=%lu, block=%lu",
* (unsigned long) ino, block);
*/
Egdp:
return ERR_PTR(-EIO);
}
void atfs_set_inode_flags(struct inode *inode)
{
unsigned int flags = ATFS_I(inode)->i_flags;
inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME |
S_DIRSYNC | S_DAX);
if (flags & ATFS_SYNC_FL)
inode->i_flags |= S_SYNC;
if (flags & ATFS_APPEND_FL)
inode->i_flags |= S_APPEND;
if (flags & ATFS_IMMUTABLE_FL)
inode->i_flags |= S_IMMUTABLE;
if (flags & ATFS_NOATIME_FL)
inode->i_flags |= S_NOATIME;
if (flags & ATFS_DIRSYNC_FL)
inode->i_flags |= S_DIRSYNC;
if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
inode->i_flags |= S_DAX;
}
/*
* Returns 1 if the passed-in block region is valid; 0 if some part overlaps
* with filesystem metadata blocks.
*/
int atfs_data_block_valid(struct atfs_sb_info *sbi, atfs_fsblk_t start_blk,
unsigned int count)
{
if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
(start_blk + count - 1 < start_blk) ||
(start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))
return 0;
/* Ensure we do not step over superblock */
if ((start_blk <= sbi->s_sb_block) &&
(start_blk + count - 1 >= sbi->s_sb_block))
return 0;
return 1;
}
void atfs_set_file_ops(struct inode *inode)
{
inode->i_op = &atfs_file_inode_operations;
inode->i_fop = &atfs_file_operations;
if (test_opt(inode->i_sb, NOBH))
inode->i_mapping->a_ops = &atfs_nobh_aops;
else
inode->i_mapping->a_ops = &atfs_aops;
}
__attribute__((optimize("O0")))
struct inode *atfs_iget(struct super_block *sb, unsigned long ino)
{
struct atfs_inode_info *ei;
struct buffer_head * bh = NULL;
struct atfs_inode *raw_inode;
struct inode *inode;
long ret = -EIO;
int n;
uid_t i_uid;
gid_t i_gid;
inode = iget_locked(sb, ino);
if (!inode)
return ERR_PTR(-ENOMEM);
if (!(inode->i_state & I_NEW))
return inode;
ei = ATFS_I(inode);
ei->i_block_alloc_info = NULL;
raw_inode = atfs_get_inode(inode->i_sb, ino, &bh);
if (IS_ERR(raw_inode)) {
ret = PTR_ERR(raw_inode);
goto bad_inode;
}
inode->i_mode = le16_to_cpu(raw_inode->i_mode);
i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
if (!(test_opt (inode->i_sb, NO_UID32))) {
i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
}
i_uid_write(inode, i_uid);
i_gid_write(inode, i_gid);
set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
inode->i_size = le32_to_cpu(raw_inode->i_size);
inode->i_atime.tv_sec = (signed)le32_to_cpu(raw_inode->i_atime);
inode->i_ctime.tv_sec = (signed)le32_to_cpu(raw_inode->i_ctime);
inode->i_mtime.tv_sec = (signed)le32_to_cpu(raw_inode->i_mtime);
inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
/* We now have enough fields to check if the inode was active or not.
* This is needed because nfsd might try to access dead inodes
* the test is that same one that e2fsck uses
* NeilBrown 1999oct15
*/
if (inode->i_nlink == 0 && (inode->i_mode == 0 || ei->i_dtime)) {
/* this inode is deleted */
ret = -ESTALE;
goto bad_inode;
}
inode->i_blocks = le32_to_cpu(raw_inode->i_blocks);
ei->i_flags = le32_to_cpu(raw_inode->i_flags);
atfs_set_inode_flags(inode);
ei->i_faddr = le32_to_cpu(raw_inode->i_faddr);
ei->i_frag_no = raw_inode->i_frag;
ei->i_frag_size = raw_inode->i_fsize;
ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
ei->i_dir_acl = 0;
if (ei->i_file_acl &&
!atfs_data_block_valid(ATFS_SB(sb), ei->i_file_acl, 1)) {
/*
* ext2_error(sb, "ext2_iget", "bad extended attribute block %u",
* ei->i_file_acl);
*/
ret = -EFSCORRUPTED;
goto bad_inode;
}
if (S_ISREG(inode->i_mode))
inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
else
ei->i_dir_acl = le32_to_cpu(raw_inode->i_dir_acl);
if (i_size_read(inode) < 0) {
ret = -EFSCORRUPTED;
goto bad_inode;
}
ei->i_dtime = 0;
inode->i_generation = le32_to_cpu(raw_inode->i_generation);
ei->i_state = 0;
ei->i_block_group = (ino - 1) / ATFS_INODES_PER_GROUP(inode->i_sb);
ei->i_dir_start_lookup = 0;
/*
* NOTE! The in-memory inode i_data array is in little-endian order
* even on big-endian machines: we do NOT byteswap the block numbers!
*/
for (n = 0; n < ATFS_N_BLOCKS; n++)
ei->i_data[n] = raw_inode->i_block[n];
if (S_ISREG(inode->i_mode)) {
atfs_set_file_ops(inode);
} else if (S_ISDIR(inode->i_mode)) {
inode->i_op = &atfs_dir_inode_operations;
inode->i_fop = &atfs_dir_operations;
if (test_opt(inode->i_sb, NOBH))
inode->i_mapping->a_ops = &atfs_nobh_aops;
else
inode->i_mapping->a_ops = &atfs_aops;
}
/*
* else if (S_ISLNK(inode->i_mode)) {
* if (atfs_inode_is_fast_symlink(inode)) {
* inode->i_link = (char *)ei->i_data;
* inode->i_op = &ext2_fast_symlink_inode_operations;
* nd_terminate_link(ei->i_data, inode->i_size,
* sizeof(ei->i_data) - 1);
* } else {
* inode->i_op = &ext2_symlink_inode_operations;
* inode_nohighmem(inode);
* if (test_opt(inode->i_sb, NOBH))
* inode->i_mapping->a_ops = &ext2_nobh_aops;
* else
* inode->i_mapping->a_ops = &ext2_aops;
* }
* }
*/
else {
inode->i_op = &atfs_special_inode_operations;
if (raw_inode->i_block[0])
init_special_inode(inode, inode->i_mode,
old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
else
init_special_inode(inode, inode->i_mode,
new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
}
brelse (bh);
unlock_new_inode(inode);
return inode;
bad_inode:
brelse(bh);
iget_failed(inode);
return ERR_PTR(ret);
}
/*
* Return the offset into page `page_nr' of the last valid
* byte in that page, plus one.
*/
static unsigned
atfs_last_byte(struct inode *inode, unsigned long page_nr)
{
unsigned last_byte = inode->i_size;
last_byte -= page_nr << PAGE_SHIFT;
if (last_byte > PAGE_SIZE)
last_byte = PAGE_SIZE;
return last_byte;
}
/*
* p is at least 6 bytes before the end of page
*/
static inline atfs_dirent *atfs_next_entry(atfs_dirent *p)
{
return (atfs_dirent *)((char *)p +
atfs_rec_len_from_disk(p->rec_len));
}
/*
* NOTE! unlike strncmp, atfs_match returns 1 for success, 0 for failure.
*
* len <= ATFS_NAME_LEN and de != NULL are guaranteed by caller.
*/
static inline int atfs_match (int len, const char * const name,
struct atfs_dir_entry * de)
{
if (len != de->name_len)
return 0;
if (!de->inode)
return 0;
return !memcmp(name, de->name, len);
}
/*
* atfs_find_entry()
*
* finds an entry in the specified directory with the wanted name. It
* returns the page in which the entry was found (as a parameter - res_page),
* and the entry itself. Page is returned mapped and unlocked.
* Entry is guaranteed to be valid.
*/
__attribute__((optimize("O0")))
struct atfs_dir_entry *atfs_find_entry(struct inode *dir,
const struct qstr *child, struct page **res_page)
{
const char *name = child->name;
int namelen = child->len;
unsigned reclen = ATFS_DIR_REC_LEN(namelen);
unsigned long start, n;
unsigned long npages = dir_pages(dir);
struct page *page = NULL;
struct atfs_inode_info *ei = ATFS_I(dir);
atfs_dirent * de;
int dir_has_error = 0;
if (npages == 0)
goto out;
/* OFFSET_CACHE */
*res_page = NULL;
start = ei->i_dir_start_lookup;
if (start >= npages)
start = 0;
n = start;
do {
char *kaddr;
page = atfs_get_page(dir, n, dir_has_error);
if (!IS_ERR(page)) {
kaddr = page_address(page);
de = (atfs_dirent *) kaddr;
kaddr += atfs_last_byte(dir, n) - reclen;
while ((char *) de <= kaddr) {
if (de->rec_len == 0) {
/*
* ext2_error(dir->i_sb, __func__,
* "zero-length directory entry");
*/
atfs_put_page(page);
goto out;
}
if (atfs_match (namelen, name, de))
goto found;
de = atfs_next_entry(de);
}
atfs_put_page(page);
} else
dir_has_error = 1;
if (++n >= npages)
n = 0;
/* next page is past the blocks we've got */
if (unlikely(n > (dir->i_blocks >> (PAGE_SHIFT - 9)))) {
/*
* ext2_error(dir->i_sb, __func__,
* "dir %lu size %lld exceeds block count %llu",
* dir->i_ino, dir->i_size,
* (unsigned long long)dir->i_blocks);
*/
goto out;
}
} while (n != start);
out:
return NULL;
found:
*res_page = page;
ei->i_dir_start_lookup = n;
return de;
}
ino_t atfs_inode_by_name(struct inode *dir, const struct qstr *child)
{
ino_t res = 0;
struct atfs_dir_entry *de;
struct page *page;
de = atfs_find_entry(dir, child, &page);
if (de) {
res = le32_to_cpu(de->inode);
atfs_put_page(page);
}
return res;
}
static struct dentry *atfs_lookup(struct inode * dir,
struct dentry *dentry, unsigned int flags)
{
struct inode * inode;
ino_t ino;
printk(KERN_INFO "==atfs== atfs_lookup invoked");
if (dentry->d_name.len > ATFS_NAME_LEN)
return ERR_PTR(-ENAMETOOLONG);
ino = atfs_inode_by_name(dir, &dentry->d_name);
inode = NULL;
if (ino) {
inode = atfs_iget(dir->i_sb, ino);
if (inode == ERR_PTR(-ESTALE)) {
/*
* ext2_error(dir->i_sb, __func__,
* "deleted inode referenced: %lu",
* (unsigned long) ino);
*/
return ERR_PTR(-EIO);
}
}
return d_splice_alias(inode, dentry);
}
ssize_t atfs_file_read(struct file *filp, char __user *buf,
size_t count, loff_t *ppos)
{
printk(KERN_INFO "==atfs== atfs_file_read invoked");
return 0;
}
ssize_t atfs_file_write(struct file *filp, const char __user *buf,
size_t count, loff_t *ppos)
{
printk(KERN_INFO "==atfs== atfs_file_write invoked");
return 0;
}
static int atfs_file_open(struct inode *inode, struct file *filp)
{
//BUG();
printk(KERN_INFO "==atfs== atfs_file_open invoked...");
return 0;
}
static ssize_t atfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
return generic_file_read_iter(iocb, to);
}
static int atfs_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
{
struct inode * inode;
inode = new_inode(dir->i_sb);
inode->i_mode |= S_IFDIR;
inode->i_op = &atfs_dir_inode_operations;
inode->i_fop = &atfs_file_operations;
inode->i_state = I_NEW;
d_instantiate_new(dentry, inode);
return 0;
}
/**
* atfs_block_to_path - parse the block number into array of offsets
* @inode: inode in question (we are only interested in its superblock)
* @i_block: block number to be parsed
* @offsets: array to store the offsets in
* @boundary: set this non-zero if the referred-to block is likely to be
* followed (on disk) by an indirect block.
* To store the locations of file's data atfs uses a data structure common
* for UNIX filesystems - tree of pointers anchored in the inode, with
* data blocks at leaves and indirect blocks in intermediate nodes.
* This function translates the block number into path in that tree -
* return value is the path length and @offsets[n] is the offset of
* pointer to (n+1)th node in the nth one. If @block is out of range
* (negative or too large) warning is printed and zero returned.
*
* Note: function doesn't find node addresses, so no IO is needed. All
* we need to know is the capacity of indirect blocks (taken from the
* inode->i_sb).
*/
/*
* Portability note: the last comparison (check that we fit into triple
* indirect block) is spelled differently, because otherwise on an
* architecture with 32-bit longs and 8Kb pages we might get into trouble
* if our filesystem had 8Kb blocks. We might use long long, but that would
* kill us on x86. Oh, well, at least the sign propagation does not matter -
* i_block would have to be negative in the very beginning, so we would not
* get there at all.
*/
static int atfs_block_to_path(struct inode *inode,
long i_block, int offsets[4], int *boundary)
{
int ptrs = ATFS_ADDR_PER_BLOCK(inode->i_sb);
int ptrs_bits = ATFS_ADDR_PER_BLOCK_BITS(inode->i_sb);
const long direct_blocks = ATFS_NDIR_BLOCKS,
indirect_blocks = ptrs,
double_blocks = (1 << (ptrs_bits * 2));
int n = 0;
int final = 0;
printk(KERN_INFO "==atfs== atfs_block_to_path ptrs_bits:%d",ptrs_bits);
if (i_block < 0) {
/*
* ext2_msg(inode->i_sb, KERN_WARNING,
* "warning: %s: block < 0", __func__);
*/
} else if (i_block < direct_blocks) {
offsets[n++] = i_block;
final = direct_blocks;
} else if ( (i_block -= direct_blocks) < indirect_blocks) {
offsets[n++] = ATFS_IND_BLOCK;
offsets[n++] = i_block;
final = ptrs;
} else if ((i_block -= indirect_blocks) < double_blocks) {
offsets[n++] = ATFS_DIND_BLOCK;
offsets[n++] = i_block >> ptrs_bits;
offsets[n++] = i_block & (ptrs - 1);
final = ptrs;
} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
offsets[n++] = ATFS_TIND_BLOCK;
offsets[n++] = i_block >> (ptrs_bits * 2);
offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
offsets[n++] = i_block & (ptrs - 1);
final = ptrs;
} else {
/*
* ext2_msg(inode->i_sb, KERN_WARNING,
* "warning: %s: block is too big", __func__);
*/
}
if (boundary)
*boundary = final - 1 - (i_block & (ptrs - 1));
return n;
}
/**
* atfs_get_branch - read the chain of indirect blocks leading to data
* @inode: inode in question
* @depth: depth of the chain (1 - direct pointer, etc.)
* @offsets: offsets of pointers in inode/indirect blocks
* @chain: place to store the result
* @err: here we store the error value
*
* Function fills the array of triples <key, p, bh> and returns %NULL
* if everything went OK or the pointer to the last filled triple
* (incomplete one) otherwise. Upon the return chain[i].key contains
* the number of (i+1)-th block in the chain (as it is stored in memory,
* i.e. little-endian 32-bit), chain[i].p contains the address of that
* number (it points into struct inode for i==0 and into the bh->b_data
* for i>0) and chain[i].bh points to the buffer_head of i-th indirect
* block for i>0 and NULL for i==0. In other words, it holds the block
* numbers of the chain, addresses they were taken from (and where we can
* verify that chain did not change) and buffer_heads hosting these
* numbers.
*
* Function stops when it stumbles upon zero pointer (absent block)
* (pointer to last triple returned, *@err == 0)
* or when it gets an IO error reading an indirect block
* (ditto, *@err == -EIO)
* or when it notices that chain had been changed while it was reading
* (ditto, *@err == -EAGAIN)
* or when it reads all @depth-1 indirect blocks successfully and finds
* the whole chain, all way to the data (returns %NULL, *err == 0).
*/
static Indirect *atfs_get_branch(struct inode *inode,
int depth,
int *offsets,
Indirect chain[4],
int *err)
{
struct super_block *sb = inode->i_sb;
Indirect *p = chain;
struct buffer_head *bh;
*err = 0;
/* i_data is not going away, no lock needed */
add_chain (chain, NULL, ATFS_I(inode)->i_data + *offsets);
if (!p->key)
goto no_block;
while (--depth) {
bh = sb_bread(sb, le32_to_cpu(p->key));
if (!bh)
goto failure;
read_lock(&ATFS_I(inode)->i_meta_lock);
if (!verify_chain(chain, p))
goto changed;
add_chain(++p, bh, (__le32*)bh->b_data + *++offsets);
read_unlock(&ATFS_I(inode)->i_meta_lock);
if (!p->key)
goto no_block;
}
return NULL;
changed:
read_unlock(&ATFS_I(inode)->i_meta_lock);
brelse(bh);
*err = -EAGAIN;
goto no_block;
failure:
*err = -EIO;
no_block:
return p;
}
/**
* atfs_init_block_alloc_info()
* @inode: file inode structure
*
* Allocate and initialize the reservation window structure, and
* link the window to the atfs inode structure at last
*
* The reservation window structure is only dynamically allocated
* and linked to atfs inode the first time the open file
* needs a new block. So, before every atfs_new_block(s) call, for
* regular files, we should check whether the reservation window
* structure exists or not. In the latter case, this function is called.
* Fail to do so will result in block reservation being turned off for that
* open file.
*
* This function is called from atfs_get_blocks_handle(), also called
* when setting the reservation window size through ioctl before the file
* is open for write (needs block allocation).
*
* Needs truncate_mutex protection prior to calling this function.
*/
void atfs_init_block_alloc_info(struct inode *inode)
{
struct atfs_inode_info *ei = ATFS_I(inode);
struct atfs_block_alloc_info *block_i;
struct super_block *sb = inode->i_sb;
block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
if (block_i) {
struct atfs_reserve_window_node *rsv = &block_i->rsv_window_node;
rsv->rsv_start = ATFS_RESERVE_WINDOW_NOT_ALLOCATED;
rsv->rsv_end = ATFS_RESERVE_WINDOW_NOT_ALLOCATED;
/*
* if filesystem is mounted with NORESERVATION, the goal
* reservation window size is set to zero to indicate
* block reservation is off
*/
if (!test_opt(sb, RESERVATION))
rsv->rsv_goal_size = 0;
else
rsv->rsv_goal_size = ATFS_DEFAULT_RESERVE_BLOCKS;
rsv->rsv_alloc_hit = 0;
block_i->last_alloc_logical_block = 0;
block_i->last_alloc_physical_block = 0;
}
ei->i_block_alloc_info = block_i;
}
/**
* atfs_find_near - find a place for allocation with sufficient locality
* @inode: owner
* @ind: descriptor of indirect block.
*
* This function returns the preferred place for block allocation.
* It is used when heuristic for sequential allocation fails.
* Rules are:
* + if there is a block to the left of our position - allocate near it.
* + if pointer will live in indirect block - allocate near that block.
* + if pointer will live in inode - allocate in the same cylinder group.
*
* In the latter case we colour the starting block by the callers PID to
* prevent it from clashing with concurrent allocations for a different inode
* in the same block group. The PID is used here so that functionally related
* files will be close-by on-disk.
*
* Caller must make sure that @ind is valid and will stay that way.
*/
static atfs_fsblk_t atfs_find_near(struct inode *inode, Indirect *ind)
{
struct atfs_inode_info *ei = ATFS_I(inode);
__le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data;
__le32 *p;
atfs_fsblk_t bg_start;
atfs_fsblk_t colour;
/* Try to find previous block */
for (p = ind->p - 1; p >= start; p--)
if (*p)
return le32_to_cpu(*p);
/* No such thing, so let's try location of indirect block */
if (ind->bh)
return ind->bh->b_blocknr;
/*
* It is going to be referred from inode itself? OK, just put it into
* the same cylinder group then.
*/
bg_start = atfs_group_first_block_no(inode->i_sb, ei->i_block_group);
colour = (current->pid % 16) *
(ATFS_BLOCKS_PER_GROUP(inode->i_sb) / 16);
return bg_start + colour;
}
/**
* atfs_find_goal - find a preferred place for allocation.
* @inode: owner
* @block: block we want
* @partial: pointer to the last triple within a chain
*
* Returns preferred place for a block (the goal).
*/
static inline atfs_fsblk_t atfs_find_goal(struct inode *inode, long block,
Indirect *partial)
{
struct atfs_block_alloc_info *block_i;
block_i = ATFS_I(inode)->i_block_alloc_info;
/*
* try the heuristic for sequential allocation,
* failing that at least try to get decent locality.
*/
if (block_i && (block == block_i->last_alloc_logical_block + 1)
&& (block_i->last_alloc_physical_block != 0)) {
return block_i->last_alloc_physical_block + 1;
}
return atfs_find_near(inode, partial);
}
/**
* atfs_blks_to_allocate: Look up the block map and count the number
* of direct blocks need to be allocated for the given branch.
*
* @branch: chain of indirect blocks
* @k: number of blocks need for indirect blocks
* @blks: number of data blocks to be mapped.
* @blocks_to_boundary: the offset in the indirect block
*
* return the total number of blocks to be allocate, including the
* direct and indirect blocks.
*/
static int
atfs_blks_to_allocate(Indirect * branch, int k, unsigned long blks,
int blocks_to_boundary)
{
unsigned long count = 0;
/*
* Simple case, [t,d]Indirect block(s) has not allocated yet
* then it's clear blocks on that path have not allocated
*/
if (k > 0) {
/* right now don't hanel cross boundary allocation */
if (blks < blocks_to_boundary + 1)
count += blks;
else
count += blocks_to_boundary + 1;
return count;
}
count++;
while (count < blks && count <= blocks_to_boundary
&& le32_to_cpu(*(branch[0].p + count)) == 0) {
count++;
}
return count;
}
/**
* atfs_splice_branch - splice the allocated branch onto inode.
* @inode: owner
* @block: (logical) number of block we are adding
* @where: location of missing link
* @num: number of indirect blocks we are adding
* @blks: number of direct blocks we are adding
*
* This function fills the missing link and does all housekeeping needed in
* inode (->i_blocks, etc.). In case of success we end up with the full
* chain to new block and return 0.
*/
static void atfs_splice_branch(struct inode *inode,
long block, Indirect *where, int num, int blks)
{
int i;
struct atfs_block_alloc_info *block_i;
atfs_fsblk_t current_block;
block_i = ATFS_I(inode)->i_block_alloc_info;
/* XXX LOCKING probably should have i_meta_lock ?*/
/* That's it */
*where->p = where->key;
/*
* Update the host buffer_head or inode to point to more just allocated
* direct blocks blocks
*/
if (num == 0 && blks > 1) {
current_block = le32_to_cpu(where->key) + 1;
for (i = 1; i < blks; i++)
*(where->p + i ) = cpu_to_le32(current_block++);
}
/*
* update the most recently allocated logical & physical block
* in i_block_alloc_info, to assist find the proper goal block for next
* allocation
*/
if (block_i) {
block_i->last_alloc_logical_block = block + blks - 1;
block_i->last_alloc_physical_block =
le32_to_cpu(where[num].key) + blks - 1;
}
/* We are done with atomic stuff, now do the rest of housekeeping */
/* had we spliced it onto indirect block? */
if (where->bh)
mark_buffer_dirty_inode(where->bh, inode);
inode->i_ctime = current_time(inode);
mark_inode_dirty(inode);
}
static int atfs_valid_block_bitmap(struct super_block *sb,
struct atfs_group_desc *desc,
unsigned int block_group,
struct buffer_head *bh)
{
atfs_grpblk_t offset;
atfs_grpblk_t next_zero_bit;
atfs_fsblk_t bitmap_blk;
atfs_fsblk_t group_first_block;
group_first_block = atfs_group_first_block_no(sb, block_group);
/* check whether block bitmap block number is set */