Whamcloud - gitweb
7c8f96a4c2ca672df5a36de6e4474dea112049c0
[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/ext2_fs.h>
32 #include <linux/fcntl.h>
33 #include <linux/sched.h>
34 #include <linux/stat.h>
35 #include <linux/string.h>
36 #include <linux/locks.h>
37 #include <linux/quotaops.h>
38 #include <linux/iobuf.h>
39 #include <linux/obd_support.h>
40 #include <linux/obdfs.h>
41
42 static ssize_t obdfs_dir_read (struct file * filp, char * buf,
43                               size_t count, loff_t *ppos)
44 {
45         return -EISDIR;
46 }
47
48 static int obdfs_readdir(struct file *, void *, filldir_t);
49
50 struct file_operations obdfs_dir_operations = {
51         NULL,                   /* lseek - default */
52         obdfs_dir_read,         /* read */
53         NULL,                   /* write - bad */
54         obdfs_readdir,          /* readdir */
55         NULL,                   /* poll - default */
56         NULL,                   /* ioctl */
57         NULL,                   /* mmap */
58         NULL,                   /* no special open code */
59         NULL,                   /* flush */
60         NULL,                   /* no special release code */
61         NULL,                   /* fsync */
62         NULL,                   /* fasync */
63         NULL                    /* lock */
64 };
65
66 struct inode_operations obdfs_dir_inode_operations = {
67         &obdfs_dir_operations,  /* default directory file-ops */
68         obdfs_create,           /* create */
69         obdfs_lookup,           /* lookup */
70         obdfs_link,             /* link */
71         obdfs_unlink,           /* unlink */
72         obdfs_symlink,          /* symlink */
73         obdfs_mkdir,            /* mkdir */
74         obdfs_rmdir,            /* rmdir */
75         obdfs_mknod,            /* mknod */
76         obdfs_rename,           /* rename */
77         NULL,                   /* readlink */
78         NULL,                   /* follow_link */
79         NULL,                   /* get_block */
80         obdfs_readpage,         /* readpage */
81         obdfs_writepage,        /* writepage */
82         NULL,                   /* truncate */
83         NULL,                   /* permission */
84         NULL                    /* revalidate */
85 };
86
87 int obdfs_check_dir_entry (const char * function, struct inode * dir,
88                           struct ext2_dir_entry_2 * de,
89                           struct page * page,
90                           unsigned long offset)
91 {
92         const char * error_msg = NULL;
93         return 1;
94
95         ENTRY;
96         if ( !de ) {
97                 error_msg = "null de passed";
98                 return 1;
99         }
100
101         if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
102                 error_msg = "rec_len is smaller than minimal";
103         else if (le16_to_cpu(de->rec_len) % 4 != 0)
104                 error_msg = "rec_len % 4 != 0";
105         else if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(de->name_len))
106                 error_msg = "rec_len is too small for name_len";
107         else if (dir && ((char *) de - (char *)page_address(page)) + le16_to_cpu(de->rec_len) >
108                  dir->i_sb->s_blocksize)
109                 error_msg = "directory entry across blocks";
110 #if 0 /* this one doesn't yet work for OBDFS */
111         else 
112
113 if (dir && le32_to_cpu(de->inode) > le32_to_cpu(dir->i_sb->u.ext2_sb.s_es->s_inodes_count))
114                 error_msg = "inode out of bounds";
115 #endif
116         if (error_msg != NULL)
117                 ext2_error (dir->i_sb, function, "bad entry in directory #%lu: %s - "
118                             "offset=%lu, inode=%lu, rec_len=%d, name_len=%d",
119                             dir->i_ino, error_msg, offset,
120                             (unsigned long) le32_to_cpu(de->inode),
121                             le16_to_cpu(de->rec_len), de->name_len);
122         EXIT;
123         return error_msg == NULL ? 1 : 0;
124 }
125
126
127 static int obdfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
128 {
129         int error = 0;
130         unsigned long offset;
131         int stored;
132         struct ext2_dir_entry_2 * de;
133         struct super_block * sb;
134         struct page *page;
135         struct inode *inode = filp->f_dentry->d_inode;
136
137         ENTRY;
138
139         sb = inode->i_sb;
140
141         stored = 0;
142         offset = filp->f_pos & (PAGE_SIZE - 1);
143
144         OIDEBUG(inode);
145         while (!error && !stored && filp->f_pos < inode->i_size) {
146                 page = obdfs_getpage(inode, offset, 0, LOCKED);
147                 PDEBUG(page, "readdir");
148                 if (!page) {
149                         ext2_error (sb, "ext2_readdir",
150                                     "directory #%lu contains a hole at offset %lu",
151                                     inode->i_ino, (unsigned long)filp->f_pos);
152                         filp->f_pos += PAGE_SIZE - offset;
153                         continue;
154                 }
155
156 #if 0
157                 /* XXX need to do read ahead and support stuff below */
158 revalidate:
159                 /* If the dir block has changed since the last call to
160                  * readdir(2), then we might be pointing to an invalid
161                  * dirent right now.  Scan from the start of the block
162                  * to make sure. */
163                 if (filp->f_version != inode->i_version) {
164                         for (i = 0; i < sb->s_blocksize && i < offset; ) {
165                                 de = (struct ext2_dir_entry_2 *) 
166                                         (bh->b_data + i);
167                                 /* It's too expensive to do a full
168                                  * dirent test each time round this
169                                  * loop, but we do have to test at
170                                  * least that it is non-zero.  A
171                                  * failure will be detected in the
172                                  * dirent test below. */
173                                 if (le16_to_cpu(de->rec_len) < EXT2_DIR_REC_LEN(1))
174                                         break;
175                                 i += le16_to_cpu(de->rec_len);
176                         }
177                         offset = i;
178                         filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1))
179                                 | offset;
180                         filp->f_version = inode->i_version;
181                 }
182 #endif          
183                 while (!error && filp->f_pos < inode->i_size 
184                        && offset < PAGE_SIZE) {
185                         de = (struct ext2_dir_entry_2 *) ((char *)page_address(page) + offset);
186 #if 0
187                         if (!obdfs_check_dir_entry ("ext2_readdir", inode, de,
188                                                    bh, offset)) {
189                                 /* On error, skip the f_pos to the
190                                    next block. */
191                                 filp->f_pos = (filp->f_pos & (sb->s_blocksize - 1))
192                                               + sb->s_blocksize;
193                                 brelse (bh);
194                                 return stored;
195                         }
196 #endif
197                         offset += le16_to_cpu(de->rec_len);
198                         if (le32_to_cpu(de->inode)) {
199                                 /* We might block in the next section
200                                  * if the data destination is
201                                  * currently swapped out.  So, use a
202                                  * version stamp to detect whether or
203                                  * not the directory has been modified
204                                  * during the copy operation.
205                                  */
206                                 /* XXX
207                                 unsigned long version = inode->i_version;
208                                  */
209                                 error = filldir(dirent, de->name,
210                                                 de->name_len,
211                                                 filp->f_pos, le32_to_cpu(de->inode));
212                                 if (error)
213                                         break;
214 #if 0
215                                 if (version != inode->i_version)
216                                         goto revalidate;
217 #endif
218                                 stored ++;
219                         }
220                         filp->f_pos += le16_to_cpu(de->rec_len);
221                 }
222                 offset = 0;
223                 UnlockPage(page);
224                 page_cache_release(page);
225         }
226         UPDATE_ATIME(inode);
227         EXIT;
228         return 0;
229 }