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)
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>
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
)
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 */
45 NULL
, /* no special open code */
47 NULL
, /* no special release code */
48 ext2_sync_file
, /* fsync */
50 NULL
, /* check_media_change */
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 */
62 ext2_unlink
, /* unlink */
63 ext2_symlink
, /* symlink */
64 ext2_mkdir
, /* mkdir */
65 ext2_rmdir
, /* rmdir */
66 ext2_mknod
, /* mknod */
67 ext2_rename
, /* rename */
69 NULL
, /* follow_link */
75 ext2_permission
, /* permission */
80 int ext2_check_dir_entry (const char * function
, struct inode
* dir
,
81 struct ext2_dir_entry_2
* de
,
82 struct buffer_head
* bh
,
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
)
112 unsigned long offset
, blk
;
114 struct buffer_head
* bh
, * tmp
, * bha
[16];
115 struct ext2_dir_entry_2
* de
;
116 struct super_block
* sb
;
118 struct inode
*inode
= filp
->f_dentry
->d_inode
;
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
);
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
;
141 for (i
= 16 >> (EXT2_BLOCK_SIZE_BITS(sb
) - 9), num
= 0;
143 tmp
= ext2_getblk (inode
, ++blk
, 0, &err
);
144 if (tmp
&& !buffer_uptodate(tmp
) && !buffer_locked(tmp
))
150 ll_rw_block (READA
, num
, bha
);
151 for (i
= 0; i
< num
; i
++)
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
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
*)
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))
173 i
+= le16_to_cpu(de
->rec_len
);
176 filp
->f_pos
= (filp
->f_pos
& ~(sb
->s_blocksize
- 1))
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
,
186 /* On error, skip the f_pos to the
188 filp
->f_pos
= (filp
->f_pos
& (sb
->s_blocksize
- 1))
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
,
206 filp
->f_pos
, le32_to_cpu(de
->inode
));
209 if (version
!= inode
->i_version
)
213 filp
->f_pos
+= le16_to_cpu(de
->rec_len
);