Remove references to CONFIG_PROFILE. Kernel profiling is no longer a
[linux-2.6/linux-mips.git] / fs / readdir.c
blob059ab391d3e30908c97657a2bc500afe46a36956
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 struct old_linux_dirent {
94 unsigned long d_ino;
95 unsigned long d_offset;
96 unsigned short d_namlen;
97 char d_name[1];
100 struct readdir_callback {
101 struct old_linux_dirent * dirent;
102 int count;
105 static int fillonedir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
107 struct readdir_callback * buf = (struct readdir_callback *) __buf;
108 struct old_linux_dirent * dirent;
110 if (buf->count)
111 return -EINVAL;
112 buf->count++;
113 dirent = buf->dirent;
114 put_user(ino, &dirent->d_ino);
115 put_user(offset, &dirent->d_offset);
116 put_user(namlen, &dirent->d_namlen);
117 copy_to_user(dirent->d_name, name, namlen);
118 put_user(0, dirent->d_name + namlen);
119 return 0;
122 asmlinkage int old_readdir(unsigned int fd, void * dirent, unsigned int count)
124 int error;
125 struct file * file;
126 struct readdir_callback buf;
128 error = -EBADF;
129 file = fget(fd);
130 if (!file)
131 goto out;
133 buf.count = 0;
134 buf.dirent = dirent;
136 lock_kernel();
137 error = vfs_readdir(file, fillonedir, &buf);
138 if (error >= 0)
139 error = buf.count;
140 unlock_kernel();
142 fput(file);
143 out:
144 return error;
148 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
149 * interface.
151 struct linux_dirent {
152 unsigned long d_ino;
153 unsigned long d_off;
154 unsigned short d_reclen;
155 char d_name[1];
158 struct getdents_callback {
159 struct linux_dirent * current_dir;
160 struct linux_dirent * previous;
161 int count;
162 int error;
165 static int filldir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
167 struct linux_dirent * dirent;
168 struct getdents_callback * buf = (struct getdents_callback *) __buf;
169 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
171 buf->error = -EINVAL; /* only used if we fail.. */
172 if (reclen > buf->count)
173 return -EINVAL;
174 dirent = buf->previous;
175 if (dirent)
176 put_user(offset, &dirent->d_off);
177 dirent = buf->current_dir;
178 buf->previous = dirent;
179 put_user(ino, &dirent->d_ino);
180 put_user(reclen, &dirent->d_reclen);
181 copy_to_user(dirent->d_name, name, namlen);
182 put_user(0, dirent->d_name + namlen);
183 ((char *) dirent) += reclen;
184 buf->current_dir = dirent;
185 buf->count -= reclen;
186 return 0;
189 asmlinkage long sys_getdents(unsigned int fd, void * dirent, unsigned int count)
191 struct file * file;
192 struct linux_dirent * lastdirent;
193 struct getdents_callback buf;
194 int error;
196 error = -EBADF;
197 file = fget(fd);
198 if (!file)
199 goto out;
201 buf.current_dir = (struct linux_dirent *) dirent;
202 buf.previous = NULL;
203 buf.count = count;
204 buf.error = 0;
206 lock_kernel();
207 error = vfs_readdir(file, filldir, &buf);
208 if (error < 0)
209 goto out_putf;
210 error = buf.error;
211 lastdirent = buf.previous;
212 if (lastdirent) {
213 put_user(file->f_pos, &lastdirent->d_off);
214 error = count - buf.count;
217 out_putf:
218 unlock_kernel();
219 fput(file);
220 out:
221 return error;