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