readdir.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * linux/fs/readdir.c
  3. *
  4. * Copyright (C) 1995 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/time.h>
  8. #include <linux/mm.h>
  9. #include <linux/errno.h>
  10. #include <linux/stat.h>
  11. #include <linux/file.h>
  12. #include <linux/smp_lock.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_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. #define ROUND_UP(x) (((x)+sizeof(long)-1) & ~(sizeof(long)-1))
  49. #ifdef __ARCH_WANT_OLD_READDIR
  50. struct old_linux_dirent {
  51. unsigned long d_ino;
  52. unsigned long d_offset;
  53. unsigned short d_namlen;
  54. char d_name[1];
  55. };
  56. struct readdir_callback {
  57. struct old_linux_dirent __user * dirent;
  58. int result;
  59. };
  60. static int fillonedir(void * __buf, const char * name, int namlen, loff_t offset,
  61. ino_t ino, unsigned int d_type)
  62. {
  63. struct readdir_callback * buf = (struct readdir_callback *) __buf;
  64. struct old_linux_dirent __user * dirent;
  65. if (buf->result)
  66. return -EINVAL;
  67. buf->result++;
  68. dirent = buf->dirent;
  69. if (!access_ok(VERIFY_WRITE, dirent,
  70. (unsigned long)(dirent->d_name + namlen + 1) -
  71. (unsigned long)dirent))
  72. goto efault;
  73. if ( __put_user(ino, &dirent->d_ino) ||
  74. __put_user(offset, &dirent->d_offset) ||
  75. __put_user(namlen, &dirent->d_namlen) ||
  76. __copy_to_user(dirent->d_name, name, namlen) ||
  77. __put_user(0, dirent->d_name + namlen))
  78. goto efault;
  79. return 0;
  80. efault:
  81. buf->result = -EFAULT;
  82. return -EFAULT;
  83. }
  84. asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
  85. {
  86. int error;
  87. struct file * file;
  88. struct readdir_callback buf;
  89. error = -EBADF;
  90. file = fget(fd);
  91. if (!file)
  92. goto out;
  93. buf.result = 0;
  94. buf.dirent = dirent;
  95. error = vfs_readdir(file, fillonedir, &buf);
  96. if (error >= 0)
  97. error = buf.result;
  98. fput(file);
  99. out:
  100. return error;
  101. }
  102. #endif /* __ARCH_WANT_OLD_READDIR */
  103. /*
  104. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  105. * interface.
  106. */
  107. struct linux_dirent {
  108. unsigned long d_ino;
  109. unsigned long d_off;
  110. unsigned short d_reclen;
  111. char d_name[1];
  112. };
  113. struct getdents_callback {
  114. struct linux_dirent __user * current_dir;
  115. struct linux_dirent __user * previous;
  116. int count;
  117. int error;
  118. };
  119. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  120. ino_t ino, unsigned int d_type)
  121. {
  122. struct linux_dirent __user * dirent;
  123. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  124. int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
  125. buf->error = -EINVAL; /* only used if we fail.. */
  126. if (reclen > buf->count)
  127. return -EINVAL;
  128. dirent = buf->previous;
  129. if (dirent) {
  130. if (__put_user(offset, &dirent->d_off))
  131. goto efault;
  132. }
  133. dirent = buf->current_dir;
  134. if (__put_user(ino, &dirent->d_ino))
  135. goto efault;
  136. if (__put_user(reclen, &dirent->d_reclen))
  137. goto efault;
  138. if (copy_to_user(dirent->d_name, name, namlen))
  139. goto efault;
  140. if (__put_user(0, dirent->d_name + namlen))
  141. goto efault;
  142. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  143. goto efault;
  144. buf->previous = dirent;
  145. dirent = (void __user *)dirent + reclen;
  146. buf->current_dir = dirent;
  147. buf->count -= reclen;
  148. return 0;
  149. efault:
  150. buf->error = -EFAULT;
  151. return -EFAULT;
  152. }
  153. asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
  154. {
  155. struct file * file;
  156. struct linux_dirent __user * lastdirent;
  157. struct getdents_callback buf;
  158. int error;
  159. error = -EFAULT;
  160. if (!access_ok(VERIFY_WRITE, dirent, count))
  161. goto out;
  162. error = -EBADF;
  163. file = fget(fd);
  164. if (!file)
  165. goto out;
  166. buf.current_dir = dirent;
  167. buf.previous = NULL;
  168. buf.count = count;
  169. buf.error = 0;
  170. error = vfs_readdir(file, filldir, &buf);
  171. if (error < 0)
  172. goto out_putf;
  173. error = buf.error;
  174. lastdirent = buf.previous;
  175. if (lastdirent) {
  176. if (put_user(file->f_pos, &lastdirent->d_off))
  177. error = -EFAULT;
  178. else
  179. error = count - buf.count;
  180. }
  181. out_putf:
  182. fput(file);
  183. out:
  184. return error;
  185. }
  186. #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
  187. struct getdents_callback64 {
  188. struct linux_dirent64 __user * current_dir;
  189. struct linux_dirent64 __user * previous;
  190. int count;
  191. int error;
  192. };
  193. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  194. ino_t ino, unsigned int d_type)
  195. {
  196. struct linux_dirent64 __user *dirent;
  197. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  198. int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1);
  199. buf->error = -EINVAL; /* only used if we fail.. */
  200. if (reclen > buf->count)
  201. return -EINVAL;
  202. dirent = buf->previous;
  203. if (dirent) {
  204. if (__put_user(offset, &dirent->d_off))
  205. goto efault;
  206. }
  207. dirent = buf->current_dir;
  208. if (__put_user(ino, &dirent->d_ino))
  209. goto efault;
  210. if (__put_user(0, &dirent->d_off))
  211. goto efault;
  212. if (__put_user(reclen, &dirent->d_reclen))
  213. goto efault;
  214. if (__put_user(d_type, &dirent->d_type))
  215. goto efault;
  216. if (copy_to_user(dirent->d_name, name, namlen))
  217. goto efault;
  218. if (__put_user(0, dirent->d_name + namlen))
  219. goto efault;
  220. buf->previous = dirent;
  221. dirent = (void __user *)dirent + reclen;
  222. buf->current_dir = dirent;
  223. buf->count -= reclen;
  224. return 0;
  225. efault:
  226. buf->error = -EFAULT;
  227. return -EFAULT;
  228. }
  229. asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
  230. {
  231. struct file * file;
  232. struct linux_dirent64 __user * lastdirent;
  233. struct getdents_callback64 buf;
  234. int error;
  235. error = -EFAULT;
  236. if (!access_ok(VERIFY_WRITE, dirent, count))
  237. goto out;
  238. error = -EBADF;
  239. file = fget(fd);
  240. if (!file)
  241. goto out;
  242. buf.current_dir = dirent;
  243. buf.previous = NULL;
  244. buf.count = count;
  245. buf.error = 0;
  246. error = vfs_readdir(file, filldir64, &buf);
  247. if (error < 0)
  248. goto out_putf;
  249. error = buf.error;
  250. lastdirent = buf.previous;
  251. if (lastdirent) {
  252. typeof(lastdirent->d_off) d_off = file->f_pos;
  253. error = -EFAULT;
  254. if (__put_user(d_off, &lastdirent->d_off))
  255. goto out_putf;
  256. error = count - buf.count;
  257. }
  258. out_putf:
  259. fput(file);
  260. out:
  261. return error;
  262. }