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