This source file includes following definitions.
- ext2_dir_read
- ext2_check_dir_entry
- ext2_readdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <asm/segment.h>
16
17 #include <linux/errno.h>
18 #include <linux/fs.h>
19 #include <linux/ext2_fs.h>
20 #include <linux/stat.h>
21 #include <linux/sched.h>
22
23 #if 0
24 static int ext2_dir_read (struct inode * inode, struct file * filp,
25 char * buf, int count)
26 {
27 return -EISDIR;
28 }
29 #endif
30
31 int ext2_file_read (struct inode *, struct file *, char *, int);
32 static int ext2_readdir (struct inode *, struct file *, struct dirent *, int);
33
34 static struct file_operations ext2_dir_operations = {
35 NULL,
36 ext2_file_read,
37 NULL,
38 ext2_readdir,
39 NULL,
40 ext2_ioctl,
41 NULL,
42 NULL,
43 NULL,
44 NULL
45 };
46
47
48
49
50 struct inode_operations ext2_dir_inode_operations = {
51 &ext2_dir_operations,
52 ext2_create,
53 ext2_lookup,
54 ext2_link,
55 ext2_unlink,
56 ext2_symlink,
57 ext2_mkdir,
58 ext2_rmdir,
59 ext2_mknod,
60 ext2_rename,
61 NULL,
62 NULL,
63 NULL,
64 ext2_truncate,
65 ext2_permission
66 };
67
68 int ext2_check_dir_entry (char * function, struct inode * dir,
69 struct ext2_dir_entry * de, struct buffer_head * bh,
70 unsigned int offset)
71 {
72 char * error_msg = NULL;
73
74 if (de->rec_len < EXT2_DIR_REC_LEN(1))
75 error_msg = "rec_len is smaller than minimal";
76 else if (de->rec_len % 4 != 0)
77 error_msg = "rec_len % 4 != 0";
78 else if (de->rec_len < EXT2_DIR_REC_LEN(de->name_len))
79 error_msg = "rec_len is too small for name_len";
80 else if (((char *) de - bh->b_data) + de->rec_len >
81 dir->i_sb->s_blocksize)
82 error_msg = "directory entry accross blocks";
83
84 if (error_msg != NULL) {
85 printk ("%s: bad directory entry (dev %04x, dir %d): %s\n",
86 function, dir->i_dev, dir->i_ino, error_msg);
87 printk ("offset=%d, inode=%d, rec_len=%d, name_len=%d\n",
88 offset, de->inode, de->rec_len, de->name_len);
89 }
90 return error_msg == NULL ? 1 : 0;
91 }
92
93 static int ext2_readdir (struct inode * inode, struct file * filp,
94 struct dirent * dirent, int count)
95 {
96 unsigned int offset, i, err;
97 struct buffer_head * bh;
98 struct ext2_dir_entry * de;
99 struct super_block * sb;
100
101 if (!inode || !S_ISDIR(inode->i_mode))
102 return -EBADF;
103 sb = inode->i_sb;
104 while (filp->f_pos < inode->i_size) {
105 offset = filp->f_pos & (sb->s_blocksize - 1);
106 bh = ext2_bread (inode, (filp->f_pos) >> EXT2_BLOCK_SIZE_BITS(sb), 0, &err);
107 if (!bh) {
108 filp->f_pos += sb->s_blocksize - offset;
109 continue;
110 }
111 de = (struct ext2_dir_entry *) (offset + bh->b_data);
112 while (offset < sb->s_blocksize && filp->f_pos < inode->i_size) {
113 if (! ext2_check_dir_entry ("ext2_readdir", inode, de,
114 bh, offset)) {
115 brelse (bh);
116 return 0;
117 }
118 offset += de->rec_len;
119 filp->f_pos += de->rec_len;
120 if (de->inode) {
121 memcpy_tofs (dirent->d_name, de->name,
122 de->name_len);
123 put_fs_long (de->inode, &dirent->d_ino);
124 put_fs_byte (0, de->name_len + dirent->d_name);
125 put_fs_word (de->name_len, &dirent->d_reclen);
126 #ifndef DONT_USE_DCACHE
127 ext2_dcache_add (inode->i_dev, inode->i_ino,
128 de->name, de->name_len,
129 de->inode);
130 #endif
131 i = de->name_len;
132 brelse (bh);
133 return i;
134 }
135 de = (struct ext2_dir_entry *) ((char *) de +
136 de->rec_len);
137 }
138 brelse (bh);
139 }
140 return 0;
141 }