1 |
199 |
simons |
/*
|
2 |
|
|
* linux/fs/ext2/dir.c
|
3 |
|
|
*
|
4 |
|
|
* Copyright (C) 1992, 1993, 1994, 1995
|
5 |
|
|
* Remy Card (card@masi.ibp.fr)
|
6 |
|
|
* Laboratoire MASI - Institut Blaise Pascal
|
7 |
|
|
* Universite Pierre et Marie Curie (Paris VI)
|
8 |
|
|
*
|
9 |
|
|
* from
|
10 |
|
|
*
|
11 |
|
|
* linux/fs/minix/dir.c
|
12 |
|
|
*
|
13 |
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
14 |
|
|
*
|
15 |
|
|
* ext2 directory handling functions
|
16 |
|
|
*
|
17 |
|
|
* Big-endian to little-endian byte-swapping/bitmaps by
|
18 |
|
|
* David S. Miller (davem@caip.rutgers.edu), 1995
|
19 |
|
|
*/
|
20 |
|
|
|
21 |
|
|
#include <asm/segment.h>
|
22 |
|
|
|
23 |
|
|
#include <linux/errno.h>
|
24 |
|
|
#include <linux/fs.h>
|
25 |
|
|
#include <linux/ext2_fs.h>
|
26 |
|
|
#include <linux/sched.h>
|
27 |
|
|
#include <linux/stat.h>
|
28 |
|
|
|
29 |
|
|
static int ext2_dir_read (struct inode * inode, struct file * filp,
|
30 |
|
|
char * buf, int count)
|
31 |
|
|
{
|
32 |
|
|
return -EISDIR;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
static int ext2_readdir (struct inode *, struct file *, void *, filldir_t);
|
36 |
|
|
|
37 |
|
|
static struct file_operations ext2_dir_operations = {
|
38 |
|
|
NULL, /* lseek - default */
|
39 |
|
|
ext2_dir_read, /* read */
|
40 |
|
|
NULL, /* write - bad */
|
41 |
|
|
ext2_readdir, /* readdir */
|
42 |
|
|
NULL, /* select - default */
|
43 |
|
|
ext2_ioctl, /* ioctl */
|
44 |
|
|
NULL, /* mmap */
|
45 |
|
|
NULL, /* no special open code */
|
46 |
|
|
NULL, /* no special release code */
|
47 |
|
|
ext2_sync_file, /* fsync */
|
48 |
|
|
NULL, /* fasync */
|
49 |
|
|
NULL, /* check_media_change */
|
50 |
|
|
NULL /* revalidate */
|
51 |
|
|
};
|
52 |
|
|
|
53 |
|
|
/*
|
54 |
|
|
* directories can handle most operations...
|
55 |
|
|
*/
|
56 |
|
|
struct inode_operations ext2_dir_inode_operations = {
|
57 |
|
|
&ext2_dir_operations, /* default directory file-ops */
|
58 |
|
|
ext2_create, /* create */
|
59 |
|
|
ext2_lookup, /* lookup */
|
60 |
|
|
ext2_link, /* link */
|
61 |
|
|
ext2_unlink, /* unlink */
|
62 |
|
|
ext2_symlink, /* symlink */
|
63 |
|
|
ext2_mkdir, /* mkdir */
|
64 |
|
|
ext2_rmdir, /* rmdir */
|
65 |
|
|
ext2_mknod, /* mknod */
|
66 |
|
|
ext2_rename, /* rename */
|
67 |
|
|
NULL, /* readlink */
|
68 |
|
|
NULL, /* follow_link */
|
69 |
|
|
NULL, /* readpage */
|
70 |
|
|
NULL, /* writepage */
|
71 |
|
|
NULL, /* bmap */
|
72 |
|
|
ext2_truncate, /* truncate */
|
73 |
|
|
ext2_permission, /* permission */
|
74 |
|
|
NULL /* smap */
|
75 |
|
|
};
|
76 |
|
|
|
77 |
|
|
int ext2_check_dir_entry (const char * function, struct inode * dir,
|
78 |
|
|
struct ext2_dir_entry * de, struct buffer_head * bh,
|
79 |
|
|
unsigned long offset)
|
80 |
|
|
{
|
81 |
|
|
const char * error_msg = NULL;
|
82 |
|
|
|
83 |
|
|
if (swab16(de->rec_len) < EXT2_DIR_REC_LEN(1))
|
84 |
|
|
error_msg = "rec_len is smaller than minimal";
|
85 |
|
|
else if (swab16(de->rec_len) % 4 != 0)
|
86 |
|
|
error_msg = "rec_len % 4 != 0";
|
87 |
|
|
else if (swab16(de->rec_len) < EXT2_DIR_REC_LEN(swab16(de->name_len)))
|
88 |
|
|
error_msg = "rec_len is too small for name_len";
|
89 |
|
|
else if (dir && ((char *) de - bh->b_data) + swab16(de->rec_len) >
|
90 |
|
|
dir->i_sb->s_blocksize)
|
91 |
|
|
error_msg = "directory entry across blocks";
|
92 |
|
|
else if (dir && swab32(de->inode) > swab32(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
|
93 |
|
|
error_msg = "inode out of bounds";
|
94 |
|
|
|
95 |
|
|
if (error_msg != NULL)
|
96 |
|
|
ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
|
97 |
|
|
"offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
|
98 |
|
|
dir->i_ino, error_msg, offset,
|
99 |
|
|
(unsigned long) swab32(de->inode),
|
100 |
|
|
swab16(de->rec_len), swab16(de->name_len));
|
101 |
|
|
return error_msg == NULL ? 1 : 0;
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
static int ext2_readdir (struct inode * inode, struct file * filp,
|
105 |
|
|
void * dirent, filldir_t filldir)
|
106 |
|
|
{
|
107 |
|
|
int error = 0;
|
108 |
|
|
unsigned long offset, blk;
|
109 |
|
|
int i, num, stored;
|
110 |
|
|
struct buffer_head * bh, * tmp, * bha[16];
|
111 |
|
|
struct ext2_dir_entry * de;
|
112 |
|
|
struct super_block * sb;
|
113 |
|
|
int err;
|
114 |
|
|
|
115 |
|
|
if (!inode || !S_ISDIR(inode->i_mode))
|
116 |
|
|
return -EBADF;
|
117 |
|
|
sb = inode->i_sb;
|
118 |
|
|
|
119 |
|
|
stored = 0;
|
120 |
|
|
bh = NULL;
|
121 |
|
|
offset = filp->f_pos & (sb->s_blocksize - 1);
|
122 |
|
|
|
123 |
|
|
while (!error && !stored && filp->f_pos < inode->i_size) {
|
124 |
|
|
blk = (filp->f_pos) >> EXT2_BLOCK_SIZE_BITS(sb);
|
125 |
|
|
bh = ext2_bread (inode, blk, 0, &err);
|
126 |
|
|
if (!bh) {
|
127 |
|
|
ext2_error (sb, "ext2_readdir",
|
128 |
|
|
"directory #%lu contains a hole at offset %lu",
|
129 |
|
|
inode->i_ino, (unsigned long)filp->f_pos);
|
130 |
|
|
filp->f_pos += sb->s_blocksize - offset;
|
131 |
|
|
continue;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/*
|
135 |
|
|
* Do the readahead
|
136 |
|
|
*/
|
137 |
|
|
if (!offset) {
|
138 |
|
|
for (i = 16 >> (EXT2_BLOCK_SIZE_BITS(sb) - 9), num = 0;
|
139 |
|
|
i > 0; i--) {
|
140 |
|
|
tmp = ext2_getblk (inode, ++blk, 0, &err);
|
141 |
|
|
if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
|
142 |
|
|
bha[num++] = tmp;
|
143 |
|
|
else
|
144 |
|
|
brelse (tmp);
|
145 |
|
|
}
|
146 |
|
|
if (num) {
|
147 |
|
|
ll_rw_block (READA, num, bha);
|
148 |
|
|
for (i = 0; i < num; i++)
|
149 |
|
|
brelse (bha[i]);
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
revalidate:
|
154 |
|
|
/* If the dir block has changed since the last call to
|
155 |
|
|
* readdir(2), then we might be pointing to an invalid
|
156 |
|
|
* dirent right now. Scan from the start of the block
|
157 |
|
|
* to make sure. */
|
158 |
|
|
if (filp->f_version != inode->i_version) {
|
159 |
|
|
for (i = 0; i < sb->s_blocksize && i < offset; ) {
|
160 |
|
|
de = (struct ext2_dir_entry *)
|
161 |
|
|
(bh->b_data + i);
|
162 |
|
|
/* It's too expensive to do a full
|
163 |
|
|
* dirent test each time round this
|
164 |
|
|
* loop, but we do have to test at
|
165 |
|
|
* least that it is non-zero. A
|
166 |
|
|
* failure will be detected in the
|
167 |
|
|
* dirent test below. */
|
168 |
|
|
if (swab16(de->rec_len) < EXT2_DIR_REC_LEN(1))
|
169 |
|
|
break;
|
170 |
|
|
i += swab16(de->rec_len);
|
171 |
|
|
}
|
172 |
|
|
offset = i;
|
173 |
|
|
filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
|
174 |
|
|
| offset;
|
175 |
|
|
filp->f_version = inode->i_version;
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
while (!error && filp->f_pos < inode->i_size
|
179 |
|
|
&& offset < sb->s_blocksize) {
|
180 |
|
|
de = (struct ext2_dir_entry *) (bh->b_data + offset);
|
181 |
|
|
if (!ext2_check_dir_entry ("ext2_readdir", inode, de,
|
182 |
|
|
bh, offset)) {
|
183 |
|
|
/* On error, skip the f_pos to the
|
184 |
|
|
next block. */
|
185 |
|
|
filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
|
186 |
|
|
+ sb->s_blocksize;
|
187 |
|
|
brelse (bh);
|
188 |
|
|
return stored;
|
189 |
|
|
}
|
190 |
|
|
offset += swab16(de->rec_len);
|
191 |
|
|
if (swab32(de->inode)) {
|
192 |
|
|
/* We might block in the next section
|
193 |
|
|
* if the data destination is
|
194 |
|
|
* currently swapped out. So, use a
|
195 |
|
|
* version stamp to detect whether or
|
196 |
|
|
* not the directory has been modified
|
197 |
|
|
* during the copy operation. */
|
198 |
|
|
unsigned long version;
|
199 |
|
|
dcache_add(inode, de->name, swab16(de->name_len),
|
200 |
|
|
swab32(de->inode));
|
201 |
|
|
version = inode->i_version;
|
202 |
|
|
error = filldir(dirent, de->name, swab16(de->name_len), filp->f_pos, swab32(de->inode));
|
203 |
|
|
if (error)
|
204 |
|
|
break;
|
205 |
|
|
if (version != inode->i_version)
|
206 |
|
|
goto revalidate;
|
207 |
|
|
stored ++;
|
208 |
|
|
}
|
209 |
|
|
filp->f_pos += swab16(de->rec_len);
|
210 |
|
|
}
|
211 |
|
|
offset = 0;
|
212 |
|
|
brelse (bh);
|
213 |
|
|
}
|
214 |
|
|
UPDATE_ATIME(inode);
|
215 |
|
|
return 0;
|
216 |
|
|
}
|