readdir.c 7.3 KB

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