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 * ext3 directory handling functions
17 * Big-endian to little-endian byte-swapping/bitmaps by
18 * David S. Miller (davem@caip.rutgers.edu), 1995
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
,
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
),
65 return error_msg
== NULL
? 1 : 0;
68 static int ext3_readdir(struct file
* filp
,
69 void * dirent
, filldir_t filldir
)
72 unsigned long offset
, blk
;
74 struct buffer_head
* bh
, * tmp
, * bha
[16];
75 struct ext3_dir_entry_2
* de
;
76 struct super_block
* sb
;
78 struct inode
*inode
= filp
->f_dentry
->d_inode
;
79 int dir_has_error
= 0;
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
);
92 ext3_error (sb
, __func__
, "directory #%lu "
93 "contains a hole at offset %lld",
94 inode
->i_ino
, filp
->f_pos
);
97 filp
->f_pos
+= sb
->s_blocksize
- offset
;
105 for (i
= 16 >> (EXT3_BLOCK_SIZE_BITS(sb
) - 9), num
= 0;
107 tmp
= ext3_getblk (NULL
, inode
, ++blk
, 0, &err
);
108 if (tmp
&& !buffer_uptodate(tmp
) &&
115 ll_rw_block (READA
, num
, bha
);
116 for (i
= 0; i
< num
; i
++)
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
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
*)
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
) <
139 i
+= le16_to_cpu(de
->rec_len
);
142 filp
->f_pos
= (filp
->f_pos
& ~(sb
->s_blocksize
- 1))
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
,
152 /* On error, skip the f_pos to the
154 filp
->f_pos
= (filp
->f_pos
|
155 (sb
->s_blocksize
- 1)) + 1;
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
)
175 ext3_filetype_table
[de
->file_type
];
176 error
= filldir(dirent
, de
->name
,
179 le32_to_cpu(de
->inode
),
183 if (version
!= filp
->f_version
)
187 filp
->f_pos
+= le16_to_cpu(de
->rec_len
);