readdir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_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. #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. u64 ino, unsigned int d_type)
  62. {
  63. struct readdir_callback * buf = (struct readdir_callback *) __buf;
  64. struct old_linux_dirent __user * dirent;
  65. unsigned long d_ino;
  66. if (buf->result)
  67. return -EINVAL;
  68. d_ino = ino;
  69. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
  70. return -EOVERFLOW;
  71. buf->result++;
  72. dirent = buf->dirent;
  73. if (!access_ok(VERIFY_WRITE, dirent,
  74. (unsigned long)(dirent->d_name + namlen + 1) -
  75. (unsigned long)dirent))
  76. goto efault;
  77. if ( __put_user(d_ino, &dirent->d_ino) ||
  78. __put_user(offset, &dirent->d_offset) ||
  79. __put_user(namlen, &dirent->d_namlen) ||
  80. __copy_to_user(dirent->d_name, name, namlen) ||
  81. __put_user(0, dirent->d_name + namlen))
  82. goto efault;
  83. return 0;
  84. efault:
  85. buf->result = -EFAULT;
  86. return -EFAULT;
  87. }
  88. asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
  89. {
  90. int error;
  91. struct file * file;
  92. struct readdir_callback buf;
  93. error = -EBADF;
  94. file = fget(fd);
  95. if (!file)
  96. goto out;
  97. buf.result = 0;
  98. buf.dirent = dirent;
  99. error = vfs_readdir(file, fillonedir, &buf);
  100. if (error >= 0)
  101. error = buf.result;
  102. fput(file);
  103. out:
  104. return error;
  105. }
  106. #endif /* __ARCH_WANT_OLD_READDIR */
  107. /*
  108. * New, all-improved, singing, dancing, iBCS2-compliant getdents()
  109. * interface.
  110. */
  111. struct linux_dirent {
  112. unsigned long d_ino;
  113. unsigned long d_off;
  114. unsigned short d_reclen;
  115. char d_name[1];
  116. };
  117. struct getdents_callback {
  118. struct linux_dirent __user * current_dir;
  119. struct linux_dirent __user * previous;
  120. int count;
  121. int error;
  122. };
  123. static int filldir(void * __buf, const char * name, int namlen, loff_t offset,
  124. u64 ino, unsigned int d_type)
  125. {
  126. struct linux_dirent __user * dirent;
  127. struct getdents_callback * buf = (struct getdents_callback *) __buf;
  128. unsigned long d_ino;
  129. int reclen = ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
  130. buf->error = -EINVAL; /* only used if we fail.. */
  131. if (reclen > buf->count)
  132. return -EINVAL;
  133. d_ino = ino;
  134. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
  135. return -EOVERFLOW;
  136. dirent = buf->previous;
  137. if (dirent) {
  138. if (__put_user(offset, &dirent->d_off))
  139. goto efault;
  140. }
  141. dirent = buf->current_dir;
  142. if (__put_user(d_ino, &dirent->d_ino))
  143. goto efault;
  144. if (__put_user(reclen, &dirent->d_reclen))
  145. goto efault;
  146. if (copy_to_user(dirent->d_name, name, namlen))
  147. goto efault;
  148. if (__put_user(0, dirent->d_name + namlen))
  149. goto efault;
  150. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  151. goto efault;
  152. buf->previous = dirent;
  153. dirent = (void __user *)dirent + reclen;
  154. buf->current_dir = dirent;
  155. buf->count -= reclen;
  156. return 0;
  157. efault:
  158. buf->error = -EFAULT;
  159. return -EFAULT;
  160. }
  161. asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
  162. {
  163. struct file * file;
  164. struct linux_dirent __user * lastdirent;
  165. struct getdents_callback buf;
  166. int error;
  167. error = -EFAULT;
  168. if (!access_ok(VERIFY_WRITE, dirent, count))
  169. goto out;
  170. error = -EBADF;
  171. file = fget(fd);
  172. if (!file)
  173. goto out;
  174. buf.current_dir = dirent;
  175. buf.previous = NULL;
  176. buf.count = count;
  177. buf.error = 0;
  178. error = vfs_readdir(file, filldir, &buf);
  179. if (error < 0)
  180. goto out_putf;
  181. error = buf.error;
  182. lastdirent = buf.previous;
  183. if (lastdirent) {
  184. if (put_user(file->f_pos, &lastdirent->d_off))
  185. error = -EFAULT;
  186. else
  187. error = count - buf.count;
  188. }
  189. out_putf:
  190. fput(file);
  191. out:
  192. return error;
  193. }
  194. #define ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
  195. struct getdents_callback64 {
  196. struct linux_dirent64 __user * current_dir;
  197. struct linux_dirent64 __user * previous;
  198. int count;
  199. int error;
  200. };
  201. static int filldir64(void * __buf, const char * name, int namlen, loff_t offset,
  202. u64 ino, unsigned int d_type)
  203. {
  204. struct linux_dirent64 __user *dirent;
  205. struct getdents_callback64 * buf = (struct getdents_callback64 *) __buf;
  206. int reclen = ROUND_UP64(NAME_OFFSET(dirent) + namlen + 1);
  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. asmlinkage long sys_getdents64(unsigned int fd, struct linux_dirent64 __user * dirent, unsigned int count)
  238. {
  239. struct file * file;
  240. struct linux_dirent64 __user * lastdirent;
  241. struct getdents_callback64 buf;
  242. int error;
  243. error = -EFAULT;
  244. if (!access_ok(VERIFY_WRITE, dirent, count))
  245. goto out;
  246. error = -EBADF;
  247. file = fget(fd);
  248. if (!file)
  249. goto out;
  250. buf.current_dir = dirent;
  251. buf.previous = NULL;
  252. buf.count = count;
  253. buf.error = 0;
  254. error = vfs_readdir(file, filldir64, &buf);
  255. if (error < 0)
  256. goto out_putf;
  257. error = buf.error;
  258. lastdirent = buf.previous;
  259. if (lastdirent) {
  260. typeof(lastdirent->d_off) d_off = file->f_pos;
  261. error = -EFAULT;
  262. if (__put_user(d_off, &lastdirent->d_off))
  263. goto out_putf;
  264. error = count - buf.count;
  265. }
  266. out_putf:
  267. fput(file);
  268. out:
  269. return error;
  270. }