Minor Ext2/Ext3 patches from 2.4.37 kernel tree.
[tomato.git] / release / src / linux / linux / fs / ext3 / dir.c
blob90d4bd0b15cc463a5655e123d520ed18dc1abc5a
1 /*
2 * linux/fs/ext3/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 * ext3 directory handling functions
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
21 #include <linux/fs.h>
22 #include <linux/jbd.h>
23 #include <linux/ext3_fs.h>
25 static unsigned char ext3_filetype_table[] = {
26 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
29 static int ext3_readdir(struct file *, void *, filldir_t);
31 struct file_operations ext3_dir_operations = {
32 read: generic_read_dir,
33 readdir: ext3_readdir, /* BKL held */
34 ioctl: ext3_ioctl, /* BKL held */
35 fsync: ext3_sync_file, /* BKL held */
38 int ext3_check_dir_entry (const char * function, struct inode * dir,
39 struct ext3_dir_entry_2 * de,
40 struct buffer_head * bh,
41 unsigned long offset)
43 const char * error_msg = NULL;
44 const int rlen = le16_to_cpu(de->rec_len);
46 if (rlen < EXT3_DIR_REC_LEN(1))
47 error_msg = "rec_len is smaller than minimal";
48 else if (rlen % 4 != 0)
49 error_msg = "rec_len % 4 != 0";
50 else if (rlen < EXT3_DIR_REC_LEN(de->name_len))
51 error_msg = "rec_len is too small for name_len";
52 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
53 error_msg = "directory entry across blocks";
54 else if (le32_to_cpu(de->inode) >
55 le32_to_cpu(dir->i_sb->u.ext3_sb.s_es->s_inodes_count))
56 error_msg = "inode out of bounds";
58 if (error_msg != NULL)
59 ext3_error (dir->i_sb, function,
60 "bad entry in directory #%lu: %s - "
61 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
62 dir->i_ino, error_msg, offset,
63 (unsigned long) le32_to_cpu(de->inode),
64 rlen, de->name_len);
65 return error_msg == NULL ? 1 : 0;
68 static int ext3_readdir(struct file * filp,
69 void * dirent, filldir_t filldir)
71 int error = 0;
72 unsigned long offset, blk;
73 int i, num, stored;
74 struct buffer_head * bh, * tmp, * bha[16];
75 struct ext3_dir_entry_2 * de;
76 struct super_block * sb;
77 int err;
78 struct inode *inode = filp->f_dentry->d_inode;
79 int dir_has_error = 0;
81 sb = inode->i_sb;
83 stored = 0;
84 bh = NULL;
85 offset = filp->f_pos & (sb->s_blocksize - 1);
87 while (!error && !stored && filp->f_pos < inode->i_size) {
88 blk = (filp->f_pos) >> EXT3_BLOCK_SIZE_BITS(sb);
89 bh = ext3_bread (0, inode, blk, 0, &err);
90 if (!bh) {
91 if (!dir_has_error) {
92 ext3_error (sb, __func__, "directory #%lu "
93 "contains a hole at offset %lld",
94 inode->i_ino, filp->f_pos);
95 dir_has_error = 1;
97 filp->f_pos += sb->s_blocksize - offset;
98 continue;
102 * Do the readahead
104 if (!offset) {
105 for (i = 16 >> (EXT3_BLOCK_SIZE_BITS(sb) - 9), num = 0;
106 i > 0; i--) {
107 tmp = ext3_getblk (NULL, inode, ++blk, 0, &err);
108 if (tmp && !buffer_uptodate(tmp) &&
109 !buffer_locked(tmp))
110 bha[num++] = tmp;
111 else
112 brelse (tmp);
114 if (num) {
115 ll_rw_block (READA, num, bha);
116 for (i = 0; i < num; i++)
117 brelse (bha[i]);
121 revalidate:
122 /* If the dir block has changed since the last call to
123 * readdir(2), then we might be pointing to an invalid
124 * dirent right now. Scan from the start of the block
125 * to make sure. */
126 if (filp->f_version != inode->i_version) {
127 for (i = 0; i < sb->s_blocksize && i < offset; ) {
128 de = (struct ext3_dir_entry_2 *)
129 (bh->b_data + i);
130 /* It's too expensive to do a full
131 * dirent test each time round this
132 * loop, but we do have to test at
133 * least that it is non-zero. A
134 * failure will be detected in the
135 * dirent test below. */
136 if (le16_to_cpu(de->rec_len) <
137 EXT3_DIR_REC_LEN(1))
138 break;
139 i += le16_to_cpu(de->rec_len);
141 offset = i;
142 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
143 | offset;
144 filp->f_version = inode->i_version;
147 while (!error && filp->f_pos < inode->i_size
148 && offset < sb->s_blocksize) {
149 de = (struct ext3_dir_entry_2 *) (bh->b_data + offset);
150 if (!ext3_check_dir_entry ("ext3_readdir", inode, de,
151 bh, offset)) {
152 /* On error, skip the f_pos to the
153 next block. */
154 filp->f_pos = (filp->f_pos |
155 (sb->s_blocksize - 1)) + 1;
156 brelse (bh);
157 return stored;
159 offset += le16_to_cpu(de->rec_len);
160 if (le32_to_cpu(de->inode)) {
161 /* We might block in the next section
162 * if the data destination is
163 * currently swapped out. So, use a
164 * version stamp to detect whether or
165 * not the directory has been modified
166 * during the copy operation.
168 unsigned long version = filp->f_version;
169 unsigned char d_type = DT_UNKNOWN;
171 if (EXT3_HAS_INCOMPAT_FEATURE(sb,
172 EXT3_FEATURE_INCOMPAT_FILETYPE)
173 && de->file_type < EXT3_FT_MAX)
174 d_type =
175 ext3_filetype_table[de->file_type];
176 error = filldir(dirent, de->name,
177 de->name_len,
178 filp->f_pos,
179 le32_to_cpu(de->inode),
180 d_type);
181 if (error)
182 break;
183 if (version != filp->f_version)
184 goto revalidate;
185 stored ++;
187 filp->f_pos += le16_to_cpu(de->rec_len);
189 offset = 0;
190 brelse (bh);
192 UPDATE_ATIME(inode);
193 return 0;