readdir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * linux/fs/readdir.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/stddef.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/time.h>
  10. #include <linux/mm.h>
  11. #include <linux/errno.h>
  12. #include <linux/stat.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/dirent.h>
  16. #include <linux/security.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/unistd.h>
  19. #include <asm/uaccess.h>
  20. int vfs_readdir(struct file *file, filldir_t filler, void *buf)
  21. {
  22. struct inode *inode = file->f_path.dentry->d_inode;
  23. int res = -ENOTDIR;
  24. if (!file->f_op || !file->f_op->readdir)
  25. goto out;
  26. res = security_file_permission(file, MAY_READ);
  27. if (res)
  28. goto out;
  29. res = mutex_lock_killable(&inode->i_mutex);
  30. if (res)
  31. goto out;
  32. res = -ENOENT;
  33. if (!IS_DEADDIR(inode)) {
  34. res = file->f_op->readdir(file, buf, filler);
  35. file_accessed(file);
  36. }
  37. mutex_unlock(&inode->i_mutex);
  38. out:
  39. return res;
  40. }
  41. EXPORT_SYMBOL(vfs_readdir);
  42. /*
  43. * Traditional linux readdir() handling..
  44. *
  45. * "count=1" is a special case, meaning that the buffer is one
  46. * dirent-structure in size and that the code can't handle more
  47. * anyway. Thus the special "fillonedir()" function for that
  48. * case (the low-level handlers don't need to care about this).
  49. */
  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(offsetof(struct linux_dirent, d_name) + namlen + 2,
  134. sizeof(long));
  135. buf->error = -EINVAL; /* only used if we fail.. */
  136. if (reclen > buf->count)
  137. return -EINVAL;
  138. d_ino = ino;
  139. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  140. buf->error = -EOVERFLOW;
  141. return -EOVERFLOW;
  142. }
  143. dirent = buf->previous;
  144. if (dirent) {
  145. if (__put_user(offset, &dirent->d_off))
  146. goto efault;
  147. }
  148. dirent = buf->current_dir;
  149. if (__put_user(d_ino, &dirent->d_ino))
  150. goto efault;
  151. if (__put_user(reclen, &dirent->d_reclen))
  152. goto efault;
  153. if (copy_to_user(dirent->d_name, name, namlen))
  154. goto efault;
  155. if (__put_user(0, dirent->d_name + namlen))
  156. goto efault;
  157. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  158. goto efault;
  159. buf->previous = dirent;
  160. dirent = (void __user *)dirent + reclen;
  161. buf->current_dir = dirent;
  162. buf->count -= reclen;
  163. return 0;
  164. efault:
  165. buf->error = -EFAULT;
  166. return -EFAULT;
  167. }
  168. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  169. struct linux_dirent __user *, dirent, unsigned int, count)
  170. {
  171. struct file * file;
  172. struct linux_dirent __user * lastdirent;
  173. struct getdents_callback buf;
  174. int error;
  175. error = -EFAULT;
  176. if (!access_ok(VERIFY_WRITE, dirent, count))
  177. goto out;
  178. error = -EBADF;
  179. file = fget(fd);
  180. if (!file)
  181. goto out;
  182. buf.current_dir = dirent;
  183. buf.previous = NULL;
  184. buf.count = count;
  185. buf.error = 0;
  186. error = vfs_readdir(file, filldir, &buf);
  187. if (error >= 0)
  188. error = buf.error;
  189. lastdirent = buf.previous;
  190. if (lastdirent) {
  191. if (put_user(file->f_pos, &lastdirent->d_off))
  192. error = -EFAULT;
  193. else
  194. error = count - buf.count;
  195. }
  196. fput(file);
  197. out:
  198. return error;
  199. }
  200. struct getdents_callback64 {
  201. struct linux_dirent64 __user * current_dir;
  202. struct linux_dirent64 __user * previous;
  203. int count;
  204. int error;
  205. };
  206. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  207. u64 ino, unsigned int d_type)
  208. {
  209. struct linux_dirent64 __user *dirent;
  210. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  211. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  212. sizeof(u64));
  213. buf->error = -EINVAL; /* only used if we fail.. */
  214. if (reclen > buf->count)
  215. return -EINVAL;
  216. dirent = buf->previous;
  217. if (dirent) {
  218. if (__put_user(offset, &dirent->d_off))
  219. goto efault;
  220. }
  221. dirent = buf->current_dir;
  222. if (__put_user(ino, &dirent->d_ino))
  223. goto efault;
  224. if (__put_user(0, &dirent->d_off))
  225. goto efault;
  226. if (__put_user(reclen, &dirent->d_reclen))
  227. goto efault;
  228. if (__put_user(d_type, &dirent->d_type))
  229. goto efault;
  230. if (copy_to_user(dirent->d_name, name, namlen))
  231. goto efault;
  232. if (__put_user(0, dirent->d_name + namlen))
  233. goto efault;
  234. buf->previous = dirent;
  235. dirent = (void __user *)dirent + reclen;
  236. buf->current_dir = dirent;
  237. buf->count -= reclen;
  238. return 0;
  239. efault:
  240. buf->error = -EFAULT;
  241. return -EFAULT;
  242. }
  243. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  244. struct linux_dirent64 __user *, dirent, unsigned int, count)
  245. {
  246. struct file * file;
  247. struct linux_dirent64 __user * lastdirent;
  248. struct getdents_callback64 buf;
  249. int error;
  250. error = -EFAULT;
  251. if (!access_ok(VERIFY_WRITE, dirent, count))
  252. goto out;
  253. error = -EBADF;
  254. file = fget(fd);
  255. if (!file)
  256. goto out;
  257. buf.current_dir = dirent;
  258. buf.previous = NULL;
  259. buf.count = count;
  260. buf.error = 0;
  261. error = vfs_readdir(file, filldir64, &buf);
  262. if (error >= 0)
  263. error = buf.error;
  264. lastdirent = buf.previous;
  265. if (lastdirent) {
  266. typeof(lastdirent->d_off) d_off = file->f_pos;
  267. if (__put_user(d_off, &lastdirent->d_off))
  268. error = -EFAULT;
  269. else
  270. error = count - buf.count;
  271. }
  272. fput(file);
  273. out:
  274. return error;
  275. }