Whamcloud - gitweb
- ext2_obd.c --- fix the bugs in read/write for Linux 2.4.3
[fs/lustre-release.git] / lustre / obdfs / dir.c
1 /*
2  *  linux/fs/ext2/dir.c
3  *
4  * This code is issued under the GNU General Public License.
5  * See the file COPYING in this distribution
6  *
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)
11  *
12  *  from
13  *
14  *  linux/fs/minix/dir.c
15  *
16  *  Copyright (C) 1991, 1992  Linus Torvalds
17  *
18  *  ext2 directory handling functions
19  *
20  *  Big-endian to little-endian byte-swapping/bitmaps by
21  *        David S. Miller (davem@caip.rutgers.edu), 1995
22  *
23  *  Changes for use with Object Based Device File System
24  *    
25  *  Copyright (C) 1999, Seagate Technology Inc. 
26  *   (author Peter J. Braam, braam@stelias.com)
27  * 
28  */
29
30 #include <asm/uaccess.h>
31
32 #include <linux/errno.h>
33 #include <linux/fs.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>
43
44 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
45                               size_t count, loff_t *ppos)
46 {
47         return -EISDIR;
48 }
49
50 static int obdfs_readdir(struct file *, void *, filldir_t);
51
52 struct file_operations obdfs_dir_operations = {
53         read: obdfs_dir_read,
54         readdir: obdfs_readdir
55 };
56
57 struct inode_operations obdfs_dir_inode_operations = {
58         create: obdfs_create,
59         lookup: obdfs_lookup,
60         link: obdfs_link,
61         unlink: obdfs_unlink,
62         symlink: obdfs_symlink,
63         mkdir: obdfs_mkdir,
64         rmdir: obdfs_rmdir,
65         mknod: obdfs_mknod,
66         rename: obdfs_rename,
67         truncate: obdfs_truncate
68 };
69
70 int obdfs_check_dir_entry (const char * function, struct inode * dir,
71                           struct ext2_dir_entry_2 * de,
72                           struct page * page,
73                           unsigned long offset)
74 {
75         const char * error_msg = NULL;
76         return 1;
77
78         ENTRY;
79         if ( !de ) {
80                 error_msg = "null de passed";
81                 return 1;
82         }
83
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 */
94         else 
95
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";
98 #endif
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);
105         EXIT;
106         return error_msg == NULL ? 1 : 0;
107 }
108
109
110 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
111 {
112         int error = 0;
113         unsigned long offset;
114         int stored;
115         struct ext2_dir_entry_2 * de;
116         struct super_block * sb;
117         struct page *page;
118         struct inode *inode = filp->f_dentry->d_inode;
119
120         ENTRY;
121
122         sb = inode->i_sb;
123
124         stored = 0;
125         offset = filp->f_pos & (PAGE_SIZE - 1);
126
127         OIDEBUG(inode);
128         while (!error && !stored && filp->f_pos < inode->i_size) {
129                 page = obdfs_getpage(inode, filp->f_pos, 0, LOCKED);
130                 /* PDEBUG(page, "readdir"); */
131                 if (!page) {
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;
136                         continue;
137                 }
138
139 #if 0
140                 /* XXX need to do read ahead and support stuff below */
141 revalidate:
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
145                  * to make sure. */
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 *) 
149                                         (bh->b_data + i);
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))
157                                         break;
158                                 i += le16_to_cpu(de->rec_len);
159                         }
160                         offset = i;
161                         filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
162                                 | offset;
163                         filp->f_version = inode->i_version;
164                 }
165 #endif          
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);
169 #if 0
170                         if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
171                                                    bh, offset)) {
172                                 /* On error, skip the f_pos to the
173                                    next block. */
174                                 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
175                                               + sb->s_blocksize;
176                                 brelse (bh);
177                                 return stored;
178                         }
179 #endif
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.
189                                  */
190                                 /* XXX
191                                 unsigned long version = inode->i_version;
192                                  */
193                                 error = filldir
194                                         (dirent, de->name, de->name_len,
195                                          filp->f_pos, le32_to_cpu(de->inode),
196                                          d_type);
197                                 if (error)
198                                         break;
199 #if 0
200                                 if (version != inode->i_version)
201                                         goto revalidate;
202 #endif
203                                 stored ++;
204                         }
205                         filp->f_pos += le16_to_cpu(de->rec_len);
206                 }
207                 offset = 0;
208                 obd_unlock_page(page);
209                 page_cache_release(page);
210         }
211         UPDATE_ATIME(inode);
212         EXIT;
213         return 0;
214 }