readdir.c 6.8 KB

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