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 */
66 struct inode_operations obdfs_dir_inode_operations = {
67 &obdfs_dir_operations, /* default directory file-ops */
68 obdfs_create, /* create */
69 obdfs_lookup, /* lookup */
70 obdfs_link, /* link */
71 obdfs_unlink, /* unlink */
72 obdfs_symlink, /* symlink */
73 obdfs_mkdir, /* mkdir */
74 obdfs_rmdir, /* rmdir */
75 obdfs_mknod, /* mknod */
76 obdfs_rename, /* rename */
78 NULL, /* follow_link */
80 obdfs_readpage, /* readpage */
81 obdfs_writepage, /* writepage */
83 NULL, /* permission */
87 int obdfs_check_dir_entry (const char * function, struct inode * dir,
88 struct ext2_dir_entry_2 * de,
92 const char * error_msg = NULL;
97 error_msg = "null de passed";
101 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
102 error_msg = "rec_len is smaller than minimal";
103 else if (le16_to_cpu(de->rec_len) % 4 != 0)
104 error_msg = "rec_len % 4 != 0";
105 else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
106 error_msg = "rec_len is too small for name_len";
107 else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
108 dir->i_sb->s_blocksize)
109 error_msg = "directory entry across blocks";
110 #if 0 /* this one doesn't yet work for OBDFS */
113 if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
114 error_msg = "inode out of bounds";
116 if (error_msg != NULL)
117 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
118 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
119 dir->i_ino, error_msg, offset,
120 (unsigned long) le32_to_cpu(de->inode),
121 le16_to_cpu(de->rec_len), de->name_len);
123 return error_msg == NULL ? 1 : 0;
127 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
130 unsigned long offset;
132 struct ext2_dir_entry_2 * de;
133 struct super_block * sb;
135 struct inode *inode = filp->f_dentry->d_inode;
142 offset = filp->f_pos & (PAGE_SIZE - 1);
145 while (!error && !stored && filp->f_pos < inode->i_size) {
146 page = obdfs_getpage(inode, offset, 0, LOCKED);
147 PDEBUG(page, "readdir");
149 ext2_error (sb, "ext2_readdir",
150 "directory #%lu contains a hole at offset %lu",
151 inode->i_ino, (unsigned long)filp->f_pos);
152 filp->f_pos += PAGE_SIZE - offset;
157 /* XXX need to do read ahead and support stuff below */
159 /* If the dir block has changed since the last call to
160 * readdir(2), then we might be pointing to an invalid
161 * dirent right now. Scan from the start of the block
163 if (filp->f_version != inode->i_version) {
164 for (i = 0; i < sb->s_blocksize && i < offset; ) {
165 de = (struct ext2_dir_entry_2 *)
167 /* It's too expensive to do a full
168 * dirent test each time round this
169 * loop, but we do have to test at
170 * least that it is non-zero. A
171 * failure will be detected in the
172 * dirent test below. */
173 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
175 i += le16_to_cpu(de->rec_len);
178 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
180 filp->f_version = inode->i_version;
183 while (!error && filp->f_pos < inode->i_size
184 && offset < PAGE_SIZE) {
185 de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
187 if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
189 /* On error, skip the f_pos to the
191 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
197 offset += le16_to_cpu(de->rec_len);
198 if (le32_to_cpu(de->inode)) {
199 /* We might block in the next section
200 * if the data destination is
201 * currently swapped out. So, use a
202 * version stamp to detect whether or
203 * not the directory has been modified
204 * during the copy operation.
207 unsigned long version = inode->i_version;
209 error = filldir(dirent, de->name,
211 filp->f_pos, le32_to_cpu(de->inode));
215 if (version != inode->i_version)
220 filp->f_pos += le16_to_cpu(de->rec_len);
224 page_cache_release(page);