4 * This code is issued under the GNU General Public License.
5 * See the file COPYING in this distribution
7 * Copyright (C) 1992, 1993, 1994, 1995
8 * Remy Card (card@masi.ibp.fr)
9 * Laboratoire MASI - Institut Blaise Pascal
10 * Universite Pierre et Marie Curie (Paris VI)
14 * linux/fs/minix/dir.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
18 * ext2 directory handling functions
20 * Big-endian to little-endian byte-swapping/bitmaps by
21 * David S. Miller (davem@caip.rutgers.edu), 1995
23 * Changes for use with Object Based Device File System
25 * Copyright (C) 1999, Seagate Technology Inc.
26 * (author Peter J. Braam, braam@stelias.com)
30 #include <asm/uaccess.h>
32 #include <linux/errno.h>
34 #include <linux/fcntl.h>
35 #include <linux/sched.h>
36 #include <linux/stat.h>
37 #include <linux/string.h>
38 #include <linux/locks.h>
39 #include <linux/quotaops.h>
40 #include <linux/iobuf.h>
41 #include <linux/obd_support.h>
42 #include <linux/obdfs.h>
44 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
45 size_t count, loff_t *ppos)
50 static int obdfs_readdir(struct file *, void *, filldir_t);
52 struct file_operations obdfs_dir_operations = {
54 readdir: obdfs_readdir
57 struct inode_operations obdfs_dir_inode_operations = {
62 symlink: obdfs_symlink,
67 truncate: obdfs_truncate
70 int obdfs_check_dir_entry (const char * function, struct inode * dir,
71 struct ext2_dir_entry_2 * de,
75 const char * error_msg = NULL;
80 error_msg = "null de passed";
84 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
85 error_msg = "rec_len is smaller than minimal";
86 else if (le16_to_cpu(de->rec_len) % 4 != 0)
87 error_msg = "rec_len % 4 != 0";
88 else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
89 error_msg = "rec_len is too small for name_len";
90 else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
91 dir->i_sb->s_blocksize)
92 error_msg = "directory entry across blocks";
93 #if 0 /* this one doesn't yet work for OBDFS */
96 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);
106 return error_msg == NULL ? 1 : 0;
110 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
113 unsigned long offset;
115 struct ext2_dir_entry_2 * de;
116 struct super_block * sb;
118 struct inode *inode = filp->f_dentry->d_inode;
125 offset = filp->f_pos & (PAGE_SIZE - 1);
128 while (!error && !stored && filp->f_pos < inode->i_size) {
129 page = obdfs_getpage(inode, filp->f_pos, 0, LOCKED);
130 /* PDEBUG(page, "readdir"); */
132 ext2_error (sb, "ext2_readdir",
133 "directory #%lu contains a hole at offset %lu",
134 inode->i_ino, (unsigned long)filp->f_pos);
135 filp->f_pos += PAGE_SIZE - offset;
140 /* XXX need to do read ahead and support stuff below */
142 /* If the dir block has changed since the last call to
143 * readdir(2), then we might be pointing to an invalid
144 * dirent right now. Scan from the start of the block
146 if (filp->f_version != inode->i_version) {
147 for (i = 0; i < sb->s_blocksize && i < offset; ) {
148 de = (struct ext2_dir_entry_2 *)
150 /* It's too expensive to do a full
151 * dirent test each time round this
152 * loop, but we do have to test at
153 * least that it is non-zero. A
154 * failure will be detected in the
155 * dirent test below. */
156 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
158 i += le16_to_cpu(de->rec_len);
161 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
163 filp->f_version = inode->i_version;
166 while (!error && filp->f_pos < inode->i_size
167 && offset < PAGE_SIZE) {
168 de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
170 if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
172 /* On error, skip the f_pos to the
174 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
180 offset += le16_to_cpu(de->rec_len);
181 if (le32_to_cpu(de->inode)) {
182 unsigned char d_type = DT_UNKNOWN;
183 /* We might block in the next section
184 * if the data destination is
185 * currently swapped out. So, use a
186 * version stamp to detect whether or
187 * not the directory has been modified
188 * during the copy operation.
191 unsigned long version = inode->i_version;
194 (dirent, de->name, de->name_len,
195 filp->f_pos, le32_to_cpu(de->inode),
200 if (version != inode->i_version)
205 filp->f_pos += le16_to_cpu(de->rec_len);
208 obd_unlock_page(page);
209 page_cache_release(page);