readdir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * linux/fs/readdir.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/time.h>
  9. #include <linux/mm.h>
  10. #include <linux/errno.h>
  11. #include <linux/stat.h>
  12. #include <linux/file.h>
  13. #include <linux/fs.h>
  14. #include <linux/dirent.h>
  15. #include <linux/security.h>
  16. #include <linux/syscalls.h>
  17. #include <linux/unistd.h>
  18. #include <asm/uaccess.h>
  19. int vfs_readdir(struct file *file, filldir_t filler, void *buf)
  20. {
  21. struct inode *inode = file->f_path.dentry->d_inode;
  22. int res = -ENOTDIR;
  23. if (!file->f_op || !file->f_op->readdir)
  24. goto out;
  25. res = security_file_permission(file, MAY_READ);
  26. if (res)
  27. goto out;
  28. res = mutex_lock_killable(&inode->i_mutex);
  29. if (res)
  30. goto out;
  31. res = -ENOENT;
  32. if (!IS_DEADDIR(inode)) {
  33. res = file->f_op->readdir(file, buf, filler);
  34. file_accessed(file);
  35. }
  36. mutex_unlock(&inode->i_mutex);
  37. out:
  38. return res;
  39. }
  40. EXPORT_SYMBOL(vfs_readdir);
  41. /*
  42. * Traditional linux readdir() handling..
  43. *
  44. * "count=1" is a special case, meaning that the buffer is one
  45. * dirent-structure in size and that the code can't handle more
  46. * anyway. Thus the special "fillonedir()" function for that
  47. * case (the low-level handlers don't need to care about this).
  48. */
  49. #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
  50. #ifdef __ARCH_WANT_OLD_READDIR
  51. struct old_linux_dirent {
  52. unsigned long d_ino;
  53. unsigned long d_offset;
  54. unsigned short d_namlen;
  55. char d_name[1];
  56. };
  57. struct readdir_callback {
  58. struct old_linux_dirent __user * dirent;
  59. int result;
  60. };
  61. static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
  62. u64 ino, unsigned int d_type)
  63. {
  64. struct readdir_callback * buf = (struct readdir_callback *) __buf;
  65. struct old_linux_dirent __user * dirent;
  66. unsigned long d_ino;
  67. if (buf->result)
  68. return -EINVAL;
  69. d_ino = ino;
  70. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  71. buf->result = -EOVERFLOW;
  72. return -EOVERFLOW;
  73. }
  74. buf->result++;
  75. dirent = buf->dirent;
  76. if (!access_ok(VERIFY_WRITE, dirent,
  77. (unsigned long)(dirent->d_name + namlen + 1) -
  78. (unsigned long)dirent))
  79. goto efault;
  80. if ( __put_user(d_ino, &dirent->d_ino) ||
  81. __put_user(offset, &dirent->d_offset) ||
  82. __put_user(namlen, &dirent->d_namlen) ||
  83. __copy_to_user(dirent->d_name, name, namlen) ||
  84. __put_user(0, dirent->d_name + namlen))
  85. goto efault;
  86. return 0;
  87. efault:
  88. buf->result = -EFAULT;
  89. return -EFAULT;
  90. }
  91. SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
  92. struct old_linux_dirent __user *, dirent, unsigned int, count)
  93. {
  94. int error;
  95. struct file * file;
  96. struct readdir_callback buf;
  97. error = -EBADF;
  98. file = fget(fd);
  99. if (!file)
  100. goto out;
  101. buf.result = 0;
  102. buf.dirent = dirent;
  103. error = vfs_readdir(file, fillonedir, &buf);
  104. if (buf.result)
  105. error = buf.result;
  106. fput(file);
  107. out:
  108. return error;
  109. }
  110. #endif /* __ARCH_WANT_OLD_READDIR */
  111. /*
  112. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  113. * interface.
  114. */
  115. struct linux_dirent {
  116. unsigned long d_ino;
  117. unsigned long d_off;
  118. unsigned short d_reclen;
  119. char d_name[1];
  120. };
  121. struct getdents_callback {
  122. struct linux_dirent __user * current_dir;
  123. struct linux_dirent __user * previous;
  124. int count;
  125. int error;
  126. };
  127. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  128. u64 ino, unsigned int d_type)
  129. {
  130. struct linux_dirent __user * dirent;
  131. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  132. unsigned long d_ino;
  133. int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 2, sizeof(long));
  134. buf->error = -EINVAL; /* only used if we fail.. */
  135. if (reclen > buf->count)
  136. return -EINVAL;
  137. d_ino = ino;
  138. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  139. buf->error = -EOVERFLOW;
  140. return -EOVERFLOW;
  141. }
  142. dirent = buf->previous;
  143. if (dirent) {
  144. if (__put_user(offset, &dirent->d_off))
  145. goto efault;
  146. }
  147. dirent = buf->current_dir;
  148. if (__put_user(d_ino, &dirent->d_ino))
  149. goto efault;
  150. if (__put_user(reclen, &dirent->d_reclen))
  151. goto efault;
  152. if (copy_to_user(dirent->d_name, name, namlen))
  153. goto efault;
  154. if (__put_user(0, dirent->d_name + namlen))
  155. goto efault;
  156. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  157. goto efault;
  158. buf->previous = dirent;
  159. dirent = (void __user *)dirent + reclen;
  160. buf->current_dir = dirent;
  161. buf->count -= reclen;
  162. return 0;
  163. efault:
  164. buf->error = -EFAULT;
  165. return -EFAULT;
  166. }
  167. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  168. struct linux_dirent __user *, dirent, unsigned int, count)
  169. {
  170. struct file * file;
  171. struct linux_dirent __user * lastdirent;
  172. struct getdents_callback buf;
  173. int error;
  174. error = -EFAULT;
  175. if (!access_ok(VERIFY_WRITE, dirent, count))
  176. goto out;
  177. error = -EBADF;
  178. file = fget(fd);
  179. if (!file)
  180. goto out;
  181. buf.current_dir = dirent;
  182. buf.previous = NULL;
  183. buf.count = count;
  184. buf.error = 0;
  185. error = vfs_readdir(file, filldir, &buf);
  186. if (error >= 0)
  187. error = buf.error;
  188. lastdirent = buf.previous;
  189. if (lastdirent) {
  190. if (put_user(file->f_pos, &lastdirent->d_off))
  191. error = -EFAULT;
  192. else
  193. error = count - buf.count;
  194. }
  195. fput(file);
  196. out:
  197. return error;
  198. }
  199. struct getdents_callback64 {
  200. struct linux_dirent64 __user * current_dir;
  201. struct linux_dirent64 __user * previous;
  202. int count;
  203. int error;
  204. };
  205. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  206. u64 ino, unsigned int d_type)
  207. {
  208. struct linux_dirent64 __user *dirent;
  209. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  210. int reclen = ALIGN(NAME_OFFSET(dirent) + namlen + 1, sizeof(u64));
  211. buf->error = -EINVAL; /* only used if we fail.. */
  212. if (reclen > buf->count)
  213. return -EINVAL;
  214. dirent = buf->previous;
  215. if (dirent) {
  216. if (__put_user(offset, &dirent->d_off))
  217. goto efault;
  218. }
  219. dirent = buf->current_dir;
  220. if (__put_user(ino, &dirent->d_ino))
  221. goto efault;
  222. if (__put_user(0, &dirent->d_off))
  223. goto efault;
  224. if (__put_user(reclen, &dirent->d_reclen))
  225. goto efault;
  226. if (__put_user(d_type, &dirent->d_type))
  227. goto efault;
  228. if (copy_to_user(dirent->d_name, name, namlen))
  229. goto efault;
  230. if (__put_user(0, dirent->d_name + namlen))
  231. goto efault;
  232. buf->previous = dirent;
  233. dirent = (void __user *)dirent + reclen;
  234. buf->current_dir = dirent;
  235. buf->count -= reclen;
  236. return 0;
  237. efault:
  238. buf->error = -EFAULT;
  239. return -EFAULT;
  240. }
  241. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  242. struct linux_dirent64 __user *, dirent, unsigned int, count)
  243. {
  244. struct file * file;
  245. struct linux_dirent64 __user * lastdirent;
  246. struct getdents_callback64 buf;
  247. int error;
  248. error = -EFAULT;
  249. if (!access_ok(VERIFY_WRITE, dirent, count))
  250. goto out;
  251. error = -EBADF;
  252. file = fget(fd);
  253. if (!file)
  254. goto out;
  255. buf.current_dir = dirent;
  256. buf.previous = NULL;
  257. buf.count = count;
  258. buf.error = 0;
  259. error = vfs_readdir(file, filldir64, &buf);
  260. if (error >= 0)
  261. error = buf.error;
  262. lastdirent = buf.previous;
  263. if (lastdirent) {
  264. typeof(lastdirent->d_off) d_off = file->f_pos;
  265. if (__put_user(d_off, &lastdirent->d_off))
  266. error = -EFAULT;
  267. else
  268. error = count - buf.count;
  269. }
  270. fput(file);
  271. out:
  272. return error;
  273. }