Import 2.1.112pre1
[davej-history.git] / fs / readdir.c
blobf517c9a87b8f588a9c637d2d5361a4bc91be12dd
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/types.h>
10 #include <linux/errno.h>
11 #include <linux/stat.h>
12 #include <linux/file.h>
13 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/smp_lock.h>
17 #include <asm/uaccess.h>
20 * Traditional linux readdir() handling..
22 * "count=1" is a special case, meaning that the buffer is one
23 * dirent-structure in size and that the code can't handle more
24 * anyway. Thus the special "fillonedir()" function for that
25 * case (the low-level handlers don't need to care about this).
27 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char *) (de)))
28 #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
30 struct old_linux_dirent {
31 unsigned long d_ino;
32 unsigned long d_offset;
33 unsigned short d_namlen;
34 char d_name[1];
37 struct readdir_callback {
38 struct old_linux_dirent * dirent;
39 int count;
42 static int fillonedir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
44 struct readdir_callback * buf = (struct readdir_callback *) __buf;
45 struct old_linux_dirent * dirent;
47 if (buf->count)
48 return -EINVAL;
49 buf->count++;
50 dirent = buf->dirent;
51 put_user(ino, &dirent->d_ino);
52 put_user(offset, &dirent->d_offset);
53 put_user(namlen, &dirent->d_namlen);
54 copy_to_user(dirent->d_name, name, namlen);
55 put_user(0, dirent->d_name + namlen);
56 return 0;
59 asmlinkage int old_readdir(unsigned int fd, void * dirent, unsigned int count)
61 int error;
62 struct file * file;
63 struct dentry * dentry;
64 struct inode * inode;
65 struct readdir_callback buf;
67 lock_kernel();
68 error = -EBADF;
69 file = fget(fd);
70 if (!file)
71 goto out;
73 dentry = file->f_dentry;
74 if (!dentry)
75 goto out_putf;
77 inode = dentry->d_inode;
78 if (!inode)
79 goto out_putf;
81 buf.count = 0;
82 buf.dirent = dirent;
84 error = -ENOTDIR;
85 if (!file->f_op || !file->f_op->readdir)
86 goto out_putf;
89 * Get the inode's semaphore to prevent changes
90 * to the directory while we read it.
92 down(&inode->i_sem);
93 error = file->f_op->readdir(file, &buf, fillonedir);
94 up(&inode->i_sem);
95 if (error < 0)
96 goto out_putf;
97 error = buf.count;
99 out_putf:
100 fput(file);
101 out:
102 unlock_kernel();
103 return error;
107 * New, all-improved, singing, dancing, iBCS2-compliant getdents()
108 * interface.
110 struct linux_dirent {
111 unsigned long d_ino;
112 unsigned long d_off;
113 unsigned short d_reclen;
114 char d_name[1];
117 struct getdents_callback {
118 struct linux_dirent * current_dir;
119 struct linux_dirent * previous;
120 int count;
121 int error;
124 static int filldir(void * __buf, const char * name, int namlen, off_t offset, ino_t ino)
126 struct linux_dirent * dirent;
127 struct getdents_callback * buf = (struct getdents_callback *) __buf;
128 int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 1);
130 buf->error = -EINVAL; /* only used if we fail.. */
131 if (reclen > buf->count)
132 return -EINVAL;
133 dirent = buf->previous;
134 if (dirent)
135 put_user(offset, &dirent->d_off);
136 dirent = buf->current_dir;
137 buf->previous = dirent;
138 put_user(ino, &dirent->d_ino);
139 put_user(reclen, &dirent->d_reclen);
140 copy_to_user(dirent->d_name, name, namlen);
141 put_user(0, dirent->d_name + namlen);
142 ((char *) dirent) += reclen;
143 buf->current_dir = dirent;
144 buf->count -= reclen;
145 return 0;
148 asmlinkage int sys_getdents(unsigned int fd, void * dirent, unsigned int count)
150 struct file * file;
151 struct dentry * dentry;
152 struct inode * inode;
153 struct linux_dirent * lastdirent;
154 struct getdents_callback buf;
155 int error;
157 lock_kernel();
158 error = -EBADF;
159 file = fget(fd);
160 if (!file)
161 goto out;
163 dentry = file->f_dentry;
164 if (!dentry)
165 goto out_putf;
167 inode = dentry->d_inode;
168 if (!inode)
169 goto out_putf;
171 buf.current_dir = (struct linux_dirent *) dirent;
172 buf.previous = NULL;
173 buf.count = count;
174 buf.error = 0;
176 error = -ENOTDIR;
177 if (!file->f_op || !file->f_op->readdir)
178 goto out_putf;
181 * Get the inode's semaphore to prevent changes
182 * to the directory while we read it.
184 down(&inode->i_sem);
185 error = file->f_op->readdir(file, &buf, filldir);
186 up(&inode->i_sem);
187 if (error < 0)
188 goto out_putf;
189 error = buf.error;
190 lastdirent = buf.previous;
191 if (lastdirent) {
192 put_user(file->f_pos, &lastdirent->d_off);
193 error = count - buf.count;
196 out_putf:
197 fput(file);
198 out:
199 unlock_kernel();
200 return error;