readdir.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/export.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_inode(file);
  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 fd f = fdget(fd);
  96. struct readdir_callback buf;
  97. if (!f.file)
  98. return -EBADF;
  99. buf.result = 0;
  100. buf.dirent = dirent;
  101. error = vfs_readdir(f.file, fillonedir, &buf);
  102. if (buf.result)
  103. error = buf.result;
  104. fdput(f);
  105. return error;
  106. }
  107. #endif /* __ARCH_WANT_OLD_READDIR */
  108. /*
  109. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  110. * interface.
  111. */
  112. struct linux_dirent {
  113. unsigned long d_ino;
  114. unsigned long d_off;
  115. unsigned short d_reclen;
  116. char d_name[1];
  117. };
  118. struct getdents_callback {
  119. struct linux_dirent __user * current_dir;
  120. struct linux_dirent __user * previous;
  121. int count;
  122. int error;
  123. };
  124. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  125. u64 ino, unsigned int d_type)
  126. {
  127. struct linux_dirent __user * dirent;
  128. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  129. unsigned long d_ino;
  130. int reclen = ALIGN(offsetof(struct linux_dirent, d_name) + namlen + 2,
  131. sizeof(long));
  132. buf->error = -EINVAL; /* only used if we fail.. */
  133. if (reclen > buf->count)
  134. return -EINVAL;
  135. d_ino = ino;
  136. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
  137. buf->error = -EOVERFLOW;
  138. return -EOVERFLOW;
  139. }
  140. dirent = buf->previous;
  141. if (dirent) {
  142. if (__put_user(offset, &dirent->d_off))
  143. goto efault;
  144. }
  145. dirent = buf->current_dir;
  146. if (__put_user(d_ino, &dirent->d_ino))
  147. goto efault;
  148. if (__put_user(reclen, &dirent->d_reclen))
  149. goto efault;
  150. if (copy_to_user(dirent->d_name, name, namlen))
  151. goto efault;
  152. if (__put_user(0, dirent->d_name + namlen))
  153. goto efault;
  154. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  155. goto efault;
  156. buf->previous = dirent;
  157. dirent = (void __user *)dirent + reclen;
  158. buf->current_dir = dirent;
  159. buf->count -= reclen;
  160. return 0;
  161. efault:
  162. buf->error = -EFAULT;
  163. return -EFAULT;
  164. }
  165. SYSCALL_DEFINE3(getdents, unsigned int, fd,
  166. struct linux_dirent __user *, dirent, unsigned int, count)
  167. {
  168. struct fd f;
  169. struct linux_dirent __user * lastdirent;
  170. struct getdents_callback buf;
  171. int error;
  172. if (!access_ok(VERIFY_WRITE, dirent, count))
  173. return -EFAULT;
  174. f = fdget(fd);
  175. if (!f.file)
  176. return -EBADF;
  177. buf.current_dir = dirent;
  178. buf.previous = NULL;
  179. buf.count = count;
  180. buf.error = 0;
  181. error = vfs_readdir(f.file, filldir, &buf);
  182. if (error >= 0)
  183. error = buf.error;
  184. lastdirent = buf.previous;
  185. if (lastdirent) {
  186. if (put_user(f.file->f_pos, &lastdirent->d_off))
  187. error = -EFAULT;
  188. else
  189. error = count - buf.count;
  190. }
  191. fdput(f);
  192. return error;
  193. }
  194. struct getdents_callback64 {
  195. struct linux_dirent64 __user * current_dir;
  196. struct linux_dirent64 __user * previous;
  197. int count;
  198. int error;
  199. };
  200. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  201. u64 ino, unsigned int d_type)
  202. {
  203. struct linux_dirent64 __user *dirent;
  204. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  205. int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
  206. sizeof(u64));
  207. buf->error = -EINVAL; /* only used if we fail.. */
  208. if (reclen > buf->count)
  209. return -EINVAL;
  210. dirent = buf->previous;
  211. if (dirent) {
  212. if (__put_user(offset, &dirent->d_off))
  213. goto efault;
  214. }
  215. dirent = buf->current_dir;
  216. if (__put_user(ino, &dirent->d_ino))
  217. goto efault;
  218. if (__put_user(0, &dirent->d_off))
  219. goto efault;
  220. if (__put_user(reclen, &dirent->d_reclen))
  221. goto efault;
  222. if (__put_user(d_type, &dirent->d_type))
  223. goto efault;
  224. if (copy_to_user(dirent->d_name, name, namlen))
  225. goto efault;
  226. if (__put_user(0, dirent->d_name + namlen))
  227. goto efault;
  228. buf->previous = dirent;
  229. dirent = (void __user *)dirent + reclen;
  230. buf->current_dir = dirent;
  231. buf->count -= reclen;
  232. return 0;
  233. efault:
  234. buf->error = -EFAULT;
  235. return -EFAULT;
  236. }
  237. SYSCALL_DEFINE3(getdents64, unsigned int, fd,
  238. struct linux_dirent64 __user *, dirent, unsigned int, count)
  239. {
  240. struct fd f;
  241. struct linux_dirent64 __user * lastdirent;
  242. struct getdents_callback64 buf;
  243. int error;
  244. if (!access_ok(VERIFY_WRITE, dirent, count))
  245. return -EFAULT;
  246. f = fdget(fd);
  247. if (!f.file)
  248. return -EBADF;
  249. buf.current_dir = dirent;
  250. buf.previous = NULL;
  251. buf.count = count;
  252. buf.error = 0;
  253. error = vfs_readdir(f.file, filldir64, &buf);
  254. if (error >= 0)
  255. error = buf.error;
  256. lastdirent = buf.previous;
  257. if (lastdirent) {
  258. typeof(lastdirent->d_off) d_off = f.file->f_pos;
  259. if (__put_user(d_off, &lastdirent->d_off))
  260. error = -EFAULT;
  261. else
  262. error = count - buf.count;
  263. }
  264. fdput(f);
  265. return error;
  266. }