Linux-2.4.0-test2
[davej-history.git] / fs / readdir.c
blob8f40d846a7571fdd107f983ae3aff059989e37c5
1 /*
2 * linux/fs/readdir.c
4 * Copyright (C) 1995 Linus Torvalds
5 */
7 #include <linux/sched.h>
8 #include <linux/mm.h>
9 #include <linux/errno.h>
10 #include <linux/stat.h>
11 #include <linux/file.h>
12 #include <linux/smp_lock.h>
14 #include <asm/uaccess.h>
16 int vfs_readdir(struct file *file,
17 int (*filler)(void *,const char *,int,off_t,ino_t),
18 void *buf)
20 struct inode *inode = file->f_dentry->d_inode;
21 int res = -ENOTDIR;
22 if (!file->f_op || !file->f_op->readdir)
23 goto out;
24 down(&inode->i_sem);
25 down(&inode->i_zombie);
26 res = -ENOENT;
27 if (!IS_DEADDIR(inode))
28 res = file->f_op->readdir(file, buf, filler);
29 up(&inode->i_zombie);
30 up(&inode->i_sem);
31 out:
32 return res;
35 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
37 int i;
38 struct dentry *dentry = filp->f_dentry;
40 i = filp->f_pos;
41 switch (i) {
42 case 0:
43 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino) < 0)
44 break;
45 i++;
46 filp->f_pos++;
47 /* fallthrough */
48 case 1:
49 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino) < 0)
50 break;
51 i++;
52 filp->f_pos++;
53 /* fallthrough */
54 default: {
55 struct list_head *list = dentry->d_subdirs.next;
57 int j = i-2;
58 for (;;) {
59 if (list == &dentry->d_subdirs)
60 return 0;
61 if (!j)
62 break;
63 j--;
64 list = list->next;
67 do {
68 struct dentry *de = list_entry(list, struct dentry, d_child);
70 if (!d_unhashed(de) && de->d_inode) {
71 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino) < 0)
72 break;
74 filp->f_pos++;
75 list = list->next;
76 } while (list != &dentry->d_subdirs);
79 return 0;
83 * Traditional linux readdir() handling..
85 * "count=1" is a special case, meaning that the buffer is one
86 * dirent-structure in size and that the code can't handle more
87 * anyway. Thus the special "fillonedir()" function for that
88 * case (the low-level handlers don't need to care about this).
90 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
91 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
93 #ifndef __ia64__
95 struct old_linux_dirent {
96 unsigned long d_ino;
97 unsigned long d_offset;
98 unsigned short d_namlen;
99 char d_name[1];
102 struct readdir_callback {
103 struct old_linux_dirent * dirent;
104 int count;
107 static int fillonedir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
109 struct readdir_callback * buf = (struct readdir_callback *) __buf;
110 struct old_linux_dirent * dirent;
112 if (buf->count)
113 return -EINVAL;
114 buf->count++;
115 dirent = buf->dirent;
116 put_user(ino, &dirent->d_ino);
117 put_user(offset, &dirent->d_offset);
118 put_user(namlen, &dirent->d_namlen);
119 copy_to_user(dirent->d_name, name, namlen);
120 put_user(0, dirent->d_name + namlen);
121 return 0;
124 asmlinkage int old_readdir(unsigned int fd, void * dirent, unsigned int count)
126 int error;
127 struct file * file;
128 struct readdir_callback buf;
130 error = -EBADF;
131 file = fget(fd);
132 if (!file)
133 goto out;
135 buf.count = 0;
136 buf.dirent = dirent;
138 lock_kernel();
139 error = vfs_readdir(file, fillonedir, &buf);
140 if (error >= 0)
141 error = buf.count;
142 unlock_kernel();
144 fput(file);
145 out:
146 return error;
149 #endif /* !__ia64__ */
152 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
153 * interface.
155 struct linux_dirent {
156 unsigned long d_ino;
157 unsigned long d_off;
158 unsigned short d_reclen;
159 char d_name[1];
162 struct getdents_callback {
163 struct linux_dirent * current_dir;
164 struct linux_dirent * previous;
165 int count;
166 int error;
169 static int filldir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
171 struct linux_dirent * dirent;
172 struct getdents_callback * buf = (struct getdents_callback *) __buf;
173 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
175 buf->error = -EINVAL; /* only used if we fail.. */
176 if (reclen > buf->count)
177 return -EINVAL;
178 dirent = buf->previous;
179 if (dirent)
180 put_user(offset, &dirent->d_off);
181 dirent = buf->current_dir;
182 buf->previous = dirent;
183 put_user(ino, &dirent->d_ino);
184 put_user(reclen, &dirent->d_reclen);
185 copy_to_user(dirent->d_name, name, namlen);
186 put_user(0, dirent->d_name + namlen);
187 ((char *) dirent) += reclen;
188 buf->current_dir = dirent;
189 buf->count -= reclen;
190 return 0;
193 asmlinkage long sys_getdents(unsigned int fd, void * dirent, unsigned int count)
195 struct file * file;
196 struct linux_dirent * lastdirent;
197 struct getdents_callback buf;
198 int error;
200 error = -EBADF;
201 file = fget(fd);
202 if (!file)
203 goto out;
205 buf.current_dir = (struct linux_dirent *) dirent;
206 buf.previous = NULL;
207 buf.count = count;
208 buf.error = 0;
210 lock_kernel();
211 error = vfs_readdir(file, filldir, &buf);
212 if (error < 0)
213 goto out_putf;
214 error = buf.error;
215 lastdirent = buf.previous;
216 if (lastdirent) {
217 put_user(file->f_pos, &lastdirent->d_off);
218 error = count - buf.count;
221 out_putf:
222 unlock_kernel();
223 fput(file);
224 out:
225 return error;