readdir.c 6.9 KB

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