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