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/fcntl.h>
32 #include <linux/sched.h>
33 #include <linux/stat.h>
34 #include <linux/string.h>
35 #include <linux/locks.h>
36 #include <linux/quotaops.h>
37 #include <linux/iobuf.h>
38 #include <linux/obd_support.h>
39 #include <linux/obdfs.h>
41 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
42 size_t count, loff_t *ppos)
47 static int obdfs_readdir(struct file *, void *, filldir_t);
49 struct file_operations obdfs_dir_operations = {
51 readdir: obdfs_readdir
54 struct inode_operations obdfs_dir_inode_operations = {
59 symlink: obdfs_symlink,
64 truncate: obdfs_truncate
67 int obdfs_check_dir_entry (const char * function, struct inode * dir,
68 struct ext2_dir_entry_2 * de,
72 const char * error_msg = NULL;
77 error_msg = "null de passed";
81 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
82 error_msg = "rec_len is smaller than minimal";
83 else if (le16_to_cpu(de->rec_len) % 4 != 0)
84 error_msg = "rec_len % 4 != 0";
85 else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
86 error_msg = "rec_len is too small for name_len";
87 else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
88 dir->i_sb->s_blocksize)
89 error_msg = "directory entry across blocks";
90 #if 0 /* this one doesn't yet work for OBDFS */
93 if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
94 error_msg = "inode out of bounds";
96 if (error_msg != NULL)
97 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
98 "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
99 dir->i_ino, error_msg, offset,
100 (unsigned long) le32_to_cpu(de->inode),
101 le16_to_cpu(de->rec_len), de->name_len);
103 return error_msg == NULL ? 1 : 0;
107 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
110 unsigned long offset;
112 struct ext2_dir_entry_2 * de;
113 struct super_block * sb;
115 struct inode *inode = filp->f_dentry->d_inode;
122 offset = filp->f_pos & (PAGE_SIZE - 1);
125 while (!error && !stored && filp->f_pos < inode->i_size) {
126 page = obdfs_getpage(inode, filp->f_pos, 0, LOCKED);
127 /* PDEBUG(page, "readdir"); */
129 ext2_error (sb, "ext2_readdir",
130 "directory #%lu contains a hole at offset %lu",
131 inode->i_ino, (unsigned long)filp->f_pos);
132 filp->f_pos += PAGE_SIZE - offset;
137 /* XXX need to do read ahead and support stuff below */
139 /* If the dir block has changed since the last call to
140 * readdir(2), then we might be pointing to an invalid
141 * dirent right now. Scan from the start of the block
143 if (filp->f_version != inode->i_version) {
144 for (i = 0; i < sb->s_blocksize && i < offset; ) {
145 de = (struct ext2_dir_entry_2 *)
147 /* It's too expensive to do a full
148 * dirent test each time round this
149 * loop, but we do have to test at
150 * least that it is non-zero. A
151 * failure will be detected in the
152 * dirent test below. */
153 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
155 i += le16_to_cpu(de->rec_len);
158 filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
160 filp->f_version = inode->i_version;
163 while (!error && filp->f_pos < inode->i_size
164 && offset < PAGE_SIZE) {
165 de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
167 if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
169 /* On error, skip the f_pos to the
171 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
177 offset += le16_to_cpu(de->rec_len);
178 if (le32_to_cpu(de->inode)) {
179 unsigned char d_type = DT_UNKNOWN;
180 /* We might block in the next section
181 * if the data destination is
182 * currently swapped out. So, use a
183 * version stamp to detect whether or
184 * not the directory has been modified
185 * during the copy operation.
188 unsigned long version = inode->i_version;
191 (dirent, de->name, de->name_len,
192 filp->f_pos, le32_to_cpu(de->inode),
197 if (version != inode->i_version)
202 filp->f_pos += le16_to_cpu(de->rec_len);
205 obd_unlock_page(page);
206 page_cache_release(page);