readdir.c 6.8 KB

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