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
20 * Changes for use with Object Based Device File System
22 * Copyright (C) 1999, Seagate Technology Inc.
23 * (author Peter J. Braam, braam@stelias.com)
27 #include <asm/uaccess.h>
29 #include <linux/errno.h>
31 #include <linux/ext2_fs.h>
32 #include <linux/fcntl.h>
33 #include <linux/sched.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/locks.h>
37 #include <linux/quotaops.h>
38 #include <linux/iobuf.h>
39 #include <linux/obd_support.h>
40 #include <linux/obdfs.h>
42 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
43 size_t count, loff_t *ppos)
48 static int obdfs_readdir(struct file *, void *, filldir_t);
50 struct file_operations obdfs_dir_operations = {
51 NULL, /* lseek - default */
52 obdfs_dir_read, /* read */
53 NULL, /* write - bad */
54 obdfs_readdir, /* readdir */
55 NULL, /* poll - default */
58 NULL, /* no special open code */
60 NULL, /* no special release code */
63 NULL, /* check_media_change */
67 struct inode_operations obdfs_dir_inode_operations = {
68 &obdfs_dir_operations, /* default directory file-ops */
69 obdfs_create, /* create */
70 obdfs_lookup, /* lookup */
71 obdfs_link, /* link */
72 obdfs_unlink, /* unlink */
73 obdfs_symlink, /* symlink */
74 obdfs_mkdir, /* mkdir */
75 obdfs_rmdir, /* rmdir */
76 obdfs_mknod, /* mknod */
77 obdfs_rename, /* rename */
79 NULL, /* follow_link */
81 obdfs_readpage, /* readpage */
82 obdfs_writepage, /* writepage */
84 NULL, /* permission */
88 int obdfs_check_dir_entry (const char * function, struct inode * dir,
89 struct ext2_dir_entry_2 * de,
93 const char * error_msg = NULL;
98 error_msg = "null de passed";
102 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
103 error_msg = "rec_len is smaller than minimal";
104 else if (le16_to_cpu(de->rec_len) % 4 != 0)
105 error_msg = "rec_len % 4 != 0";
106 else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
107 error_msg = "rec_len is too small for name_len";
108 else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
109 dir->i_sb->s_blocksize)
110 error_msg = "directory entry across blocks";
111 #if 0 /* this one doesn't yet work for OBDFS */
114 if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
115 error_msg = "inode out of bounds";
117 if (error_msg != NULL)
118 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
119 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
120 dir->i_ino, error_msg, offset,
121 (unsigned long) le32_to_cpu(de->inode),
122 le16_to_cpu(de->rec_len), de->name_len);
124 return error_msg == NULL ? 1 : 0;
128 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
131 unsigned long offset;
133 struct ext2_dir_entry_2 * de;
134 struct super_block * sb;
136 struct inode *inode = filp->f_dentry->d_inode;
143 offset = filp->f_pos & (PAGE_SIZE - 1);
145 while (!error && !stored && filp->f_pos < inode->i_size) {
147 page = obdfs_getpage(inode, offset, 0, LOCKED);
148 PDEBUG(page, "readdir");
150 ext2_error (sb, "ext2_readdir",
151 "directory #%lu contains a hole at offset %lu",
152 inode->i_ino, (unsigned long)filp->f_pos);
153 filp->f_pos += PAGE_SIZE - offset;
158 /* XXX need to do read ahead and support stuff below */
160 /* If the dir block has changed since the last call to
161 * readdir(2), then we might be pointing to an invalid
162 * dirent right now. Scan from the start of the block
164 if (filp->f_version != inode->i_version) {
165 for (i = 0; i < sb->s_blocksize && i < offset; ) {
166 de = (struct ext2_dir_entry_2 *)
168 /* It's too expensive to do a full
169 * dirent test each time round this
170 * loop, but we do have to test at
171 * least that it is non-zero. A
172 * failure will be detected in the
173 * dirent test below. */
174 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
176 i += le16_to_cpu(de->rec_len);
179 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
181 filp->f_version = inode->i_version;
184 while (!error && filp->f_pos < inode->i_size
185 && offset < PAGE_SIZE) {
186 de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
188 if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
190 /* On error, skip the f_pos to the
192 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
198 offset += le16_to_cpu(de->rec_len);
199 if (le32_to_cpu(de->inode)) {
200 /* We might block in the next section
201 * if the data destination is
202 * currently swapped out. So, use a
203 * version stamp to detect whether or
204 * not the directory has been modified
205 * during the copy operation.
208 unsigned long version = inode->i_version;
210 error = filldir(dirent, de->name,
212 filp->f_pos, le32_to_cpu(de->inode));
216 if (version != inode->i_version)
221 filp->f_pos += le16_to_cpu(de->rec_len);
225 page_cache_release(page);