Whamcloud - gitweb
New files:
[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 <asm/uaccess.h>
30
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/ext2_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 "obdfs.h"
42 #include <linux/obd_support.h>
43
44 #if 0
45 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
46                               size_t count, loff_t *ppos)
47 {
48         return -EISDIR;
49 }
50 #endif
51
52 int obdfs_check_dir_entry (const char * function, struct inode * dir,
53                           struct ext2_dir_entry_2 * de,
54                           struct page * page,
55                           unsigned long offset)
56 {
57         const char * error_msg = NULL;
58
59         ENTRY;
60         if ( !de ) {
61                 error_msg = "null de passed";
62                 return 1;
63         }
64
65         if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
66                 error_msg = "rec_len is smaller than minimal";
67         else if (le16_to_cpu(de->rec_len) % 4 != 0)
68                 error_msg = "rec_len % 4 != 0";
69         else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
70                 error_msg = "rec_len is too small for name_len";
71         else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
72                  dir->i_sb->s_blocksize)
73                 error_msg = "directory entry across blocks";
74 #if 0 /* this one doesn't yet work for OBDFS */
75         else 
76
77 if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
78                 error_msg = "inode out of bounds";
79 #endif
80         if (error_msg != NULL)
81                 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
82                             "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
83                             dir->i_ino, error_msg, offset,
84                             (unsigned long) le32_to_cpu(de->inode),
85                             le16_to_cpu(de->rec_len), de->name_len);
86         EXIT;
87         return error_msg == NULL ? 1 : 0;
88 }
89
90
91 int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
92 {
93         int error = 0;
94         unsigned long offset;
95         int stored;
96         struct ext2_dir_entry_2 * de;
97         struct super_block * sb;
98         struct page *page;
99         struct inode *inode = filp->f_dentry->d_inode;
100
101         ENTRY;
102
103         sb = inode->i_sb;
104
105         stored = 0;
106         offset = filp->f_pos & (PAGE_SIZE - 1);
107
108         while (!error && !stored && filp->f_pos < inode->i_size) {
109                 page = obdfs_getpage(inode, offset, 0, NOLOCK);
110                 PDEBUG(page, "readdir");
111                 if (!page) {
112                         ext2_error (sb, "ext2_readdir",
113                                     "directory #%lu contains a hole at offset %lu",
114                                     inode->i_ino, (unsigned long)filp->f_pos);
115                         filp->f_pos += PAGE_SIZE - offset;
116                         continue;
117                 }
118
119 #if 0
120                 /* XXX need to do read ahead and support stuff below */
121 revalidate:
122                 /* If the dir block has changed since the last call to
123                  * readdir(2), then we might be pointing to an invalid
124                  * dirent right now.  Scan from the start of the block
125                  * to make sure. */
126                 if (filp->f_version != inode->i_version) {
127                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
128                                 de = (struct ext2_dir_entry_2 *) 
129                                         (bh->b_data + i);
130                                 /* It's too expensive to do a full
131                                  * dirent test each time round this
132                                  * loop, but we do have to test at
133                                  * least that it is non-zero.  A
134                                  * failure will be detected in the
135                                  * dirent test below. */
136                                 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
137                                         break;
138                                 i += le16_to_cpu(de->rec_len);
139                         }
140                         offset = i;
141                         filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
142                                 | offset;
143                         filp->f_version = inode->i_version;
144                 }
145 #endif          
146                 while (!error && filp->f_pos < inode->i_size 
147                        && offset < PAGE_SIZE) {
148                         de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
149 #if 0
150                         if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
151                                                    bh, offset)) {
152                                 /* On error, skip the f_pos to the
153                                    next block. */
154                                 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
155                                               + sb->s_blocksize;
156                                 brelse (bh);
157                                 return stored;
158                         }
159 #endif
160                         offset += le16_to_cpu(de->rec_len);
161                         if (le32_to_cpu(de->inode)) {
162                                 /* We might block in the next section
163                                  * if the data destination is
164                                  * currently swapped out.  So, use a
165                                  * version stamp to detect whether or
166                                  * not the directory has been modified
167                                  * during the copy operation.
168                                  */
169                                 /* XXX                          unsigned long version = inode->i_version;
170                                  */
171                                 error = filldir(dirent, de->name,
172                                                 de->name_len,
173                                                 filp->f_pos, le32_to_cpu(de->inode));
174                                 if (error)
175                                         break;
176 #if 0
177                                 if (version != inode->i_version)
178                                         goto revalidate;
179 #endif
180                                 stored ++;
181                         }
182                         filp->f_pos += le16_to_cpu(de->rec_len);
183                 }
184                 offset = 0;
185                 page_cache_release(page);
186         }
187         UPDATE_ATIME(inode);
188         EXIT;
189         return 0;
190 }