readdir.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. res = mutex_lock_killable(&inode->i_mutex);
  29. if (res)
  30. goto out;
  31. res = -ENOENT;
  32. if (!IS_DEADDIR(inode)) {
  33. res = file->f_op->readdir(file, buf, filler);
  34. file_accessed(file);
  35. }
  36. mutex_unlock(&inode->i_mutex);
  37. out:
  38. return res;
  39. }
  40. EXPORT_SYMBOL(vfs_readdir);
  41. /*
  42. * Traditional linux readdir() handling..
  43. *
  44. * "count=1" is a special case, meaning that the buffer is one
  45. * dirent-structure in size and that the code can't handle more
  46. * anyway. Thus the special "fillonedir()" function for that
  47. * case (the low-level handlers don't need to care about this).
  48. */
  49. #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
  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. return -EOVERFLOW;
  72. buf->result++;
  73. dirent = buf->dirent;
  74. if (!access_ok(VERIFY_WRITE, dirent,
  75. (unsigned long)(dirent->d_name + namlen + 1) -
  76. (unsigned long)dirent))
  77. goto efault;
  78. if ( __put_user(d_ino, &dirent->d_ino) ||
  79. __put_user(offset, &dirent->d_offset) ||
  80. __put_user(namlen, &dirent->d_namlen) ||
  81. __copy_to_user(dirent->d_name, name, namlen) ||
  82. __put_user(0, dirent->d_name + namlen))
  83. goto efault;
  84. return 0;
  85. efault:
  86. buf->result = -EFAULT;
  87. return -EFAULT;
  88. }
  89. asmlinkage long old_readdir(unsigned int fd, struct old_linux_dirent __user * dirent, unsigned int count)
  90. {
  91. int error;
  92. struct file * file;
  93. struct readdir_callback buf;
  94. error = -EBADF;
  95. file = fget(fd);
  96. if (!file)
  97. goto out;
  98. buf.result = 0;
  99. buf.dirent = dirent;
  100. error = vfs_readdir(file, fillonedir, &buf);
  101. if (error >= 0)
  102. error = buf.result;
  103. fput(file);
  104. out:
  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(NAME_OFFSET(dirent) + namlen + 2, sizeof(long));
  131. buf->error = -EINVAL; /* only used if we fail.. */
  132. if (reclen > buf->count)
  133. return -EINVAL;
  134. d_ino = ino;
  135. if (sizeof(d_ino) < sizeof(ino) && d_ino != ino)
  136. return -EOVERFLOW;
  137. dirent = buf->previous;
  138. if (dirent) {
  139. if (__put_user(offset, &dirent->d_off))
  140. goto efault;
  141. }
  142. dirent = buf->current_dir;
  143. if (__put_user(d_ino, &dirent->d_ino))
  144. goto efault;
  145. if (__put_user(reclen, &dirent->d_reclen))
  146. goto efault;
  147. if (copy_to_user(dirent->d_name, name, namlen))
  148. goto efault;
  149. if (__put_user(0, dirent->d_name + namlen))
  150. goto efault;
  151. if (__put_user(d_type, (char __user *) dirent + reclen - 1))
  152. goto efault;
  153. buf->previous = dirent;
  154. dirent = (void __user *)dirent + reclen;
  155. buf->current_dir = dirent;
  156. buf->count -= reclen;
  157. return 0;
  158. efault:
  159. buf->error = -EFAULT;
  160. return -EFAULT;
  161. }
  162. asmlinkage long sys_getdents(unsigned int fd, struct linux_dirent __user * dirent, unsigned int count)
  163. {
  164. struct file * file;
  165. struct linux_dirent __user * lastdirent;
  166. struct getdents_callback buf;
  167. int error;
  168. error = -EFAULT;
  169. if (!access_ok(VERIFY_WRITE, dirent, count))
  170. goto out;
  171. error = -EBADF;
  172. file = fget(fd);
  173. if (!file)
  174. goto out;
  175. buf.current_dir = dirent;
  176. buf.previous = NULL;
  177. buf.count = count;
  178. buf.error = 0;
  179. error = vfs_readdir(file, filldir, &buf);
  180. if (error < 0)
  181. goto out_putf;
  182. error = buf.error;
  183. lastdirent = buf.previous;
  184. if (lastdirent) {
  185. if (put_user(file->f_pos, &lastdirent->d_off))
  186. error = -EFAULT;
  187. else
  188. error = count - buf.count;
  189. }
  190. out_putf:
  191. fput(file);
  192. out:
  193. return error;
  194. }
  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 = ALIGN(NAME_OFFSET(dirent) + namlen + 1, 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. 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. }