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