readdir.c 7.1 KB

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