readdir.c 6.9 KB

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