* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / fs / ext2 / dir.c
blobcf9e615bd716ddc91655d3217c03af441bd10574
1 /*
2 * linux/fs/ext2/dir.c
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)
9 * from
11 * linux/fs/minix/dir.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext2 directory handling functions
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
21 #include <asm/uaccess.h>
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>
29 static ssize_t ext2_dir_read (struct file * filp, char * buf,
30 size_t count, loff_t *ppos)
32 return -EISDIR;
35 static int ext2_readdir(struct file *, void *, filldir_t);
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, /* poll - default */
43 ext2_ioctl, /* ioctl */
44 NULL, /* mmap */
45 NULL, /* no special open code */
46 NULL, /* flush */
47 NULL, /* no special release code */
48 ext2_sync_file, /* fsync */
49 NULL, /* fasync */
50 NULL, /* check_media_change */
51 NULL /* revalidate */
55 * directories can handle most operations...
57 struct inode_operations ext2_dir_inode_operations = {
58 &ext2_dir_operations, /* default directory file-ops */
59 ext2_create, /* create */
60 ext2_lookup, /* lookup */
61 ext2_link, /* link */
62 ext2_unlink, /* unlink */
63 ext2_symlink, /* symlink */
64 ext2_mkdir, /* mkdir */
65 ext2_rmdir, /* rmdir */
66 ext2_mknod, /* mknod */
67 ext2_rename, /* rename */
68 NULL, /* readlink */
69 NULL, /* follow_link */
70 NULL, /* get_block */
71 NULL, /* readpage */
72 NULL, /* writepage */
73 NULL, /* flushpage */
74 NULL, /* truncate */
75 ext2_permission, /* permission */
76 NULL, /* smap */
77 NULL /* revalidate */
80 int ext2_check_dir_entry (const char * function, struct inode * dir,
81 struct ext2_dir_entry_2 * de,
82 struct buffer_head * bh,
83 unsigned long offset)
85 const char * error_msg = NULL;
87 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
88 error_msg = "rec_len is smaller than minimal";
89 else if (le16_to_cpu(de->rec_len) % 4 != 0)
90 error_msg = "rec_len % 4 != 0";
91 else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
92 error_msg = "rec_len is too small for name_len";
93 else if (dir && ((char *) de - bh->b_data) + le16_to_cpu(de->rec_len) >
94 dir->i_sb->s_blocksize)
95 error_msg = "directory entry across blocks";
96 else if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
97 error_msg = "inode out of bounds";
99 if (error_msg != NULL)
100 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
101 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
102 dir->i_ino, error_msg, offset,
103 (unsigned long) le32_to_cpu(de->inode),
104 le16_to_cpu(de->rec_len), de->name_len);
105 return error_msg == NULL ? 1 : 0;
108 static int ext2_readdir(struct file * filp,
109 void * dirent, filldir_t filldir)
111 int error = 0;
112 unsigned long offset, blk;
113 int i, num, stored;
114 struct buffer_head * bh, * tmp, * bha[16];
115 struct ext2_dir_entry_2 * de;
116 struct super_block * sb;
117 int err;
118 struct inode *inode = filp->f_dentry->d_inode;
120 sb = inode->i_sb;
122 stored = 0;
123 bh = NULL;
124 offset = filp->f_pos & (sb->s_blocksize - 1);
126 while (!error && !stored && filp->f_pos < inode->i_size) {
127 blk = (filp->f_pos) >> EXT2_BLOCK_SIZE_BITS(sb);
128 bh = ext2_bread (inode, blk, 0, &err);
129 if (!bh) {
130 ext2_error (sb, "ext2_readdir",
131 "directory #%lu contains a hole at offset %lu",
132 inode->i_ino, (unsigned long)filp->f_pos);
133 filp->f_pos += sb->s_blocksize - offset;
134 continue;
138 * Do the readahead
140 if (!offset) {
141 for (i = 16 >> (EXT2_BLOCK_SIZE_BITS(sb) - 9), num = 0;
142 i > 0; i--) {
143 tmp = ext2_getblk (inode, ++blk, 0, &err);
144 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
145 bha[num++] = tmp;
146 else
147 brelse (tmp);
149 if (num) {
150 ll_rw_block (READA, num, bha);
151 for (i = 0; i < num; i++)
152 brelse (bha[i]);
156 revalidate:
157 /* If the dir block has changed since the last call to
158 * readdir(2), then we might be pointing to an invalid
159 * dirent right now. Scan from the start of the block
160 * to make sure. */
161 if (filp->f_version != inode->i_version) {
162 for (i = 0; i < sb->s_blocksize && i < offset; ) {
163 de = (struct ext2_dir_entry_2 *)
164 (bh->b_data + i);
165 /* It's too expensive to do a full
166 * dirent test each time round this
167 * loop, but we do have to test at
168 * least that it is non-zero. A
169 * failure will be detected in the
170 * dirent test below. */
171 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
172 break;
173 i += le16_to_cpu(de->rec_len);
175 offset = i;
176 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
177 | offset;
178 filp->f_version = inode->i_version;
181 while (!error && filp->f_pos < inode->i_size
182 && offset < sb->s_blocksize) {
183 de = (struct ext2_dir_entry_2 *) (bh->b_data + offset);
184 if (!ext2_check_dir_entry ("ext2_readdir", inode, de,
185 bh, offset)) {
186 /* On error, skip the f_pos to the
187 next block. */
188 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
189 + sb->s_blocksize;
190 brelse (bh);
191 return stored;
193 offset += le16_to_cpu(de->rec_len);
194 if (le32_to_cpu(de->inode)) {
195 /* We might block in the next section
196 * if the data destination is
197 * currently swapped out. So, use a
198 * version stamp to detect whether or
199 * not the directory has been modified
200 * during the copy operation.
202 unsigned long version = inode->i_version;
204 error = filldir(dirent, de->name,
205 de->name_len,
206 filp->f_pos, le32_to_cpu(de->inode));
207 if (error)
208 break;
209 if (version != inode->i_version)
210 goto revalidate;
211 stored ++;
213 filp->f_pos += le16_to_cpu(de->rec_len);
215 offset = 0;
216 brelse (bh);
218 UPDATE_ATIME(inode);
219 return 0;