file.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * file.c
  3. *
  4. * Copyright (C) 1995, 1996 by Volker Lendecke
  5. * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
  6. *
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <asm/uaccess.h>
  10. #include <linux/time.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/fcntl.h>
  14. #include <linux/stat.h>
  15. #include <linux/mm.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/sched.h>
  18. #include "ncp_fs.h"
  19. static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  20. {
  21. return filemap_write_and_wait_range(file->f_mapping, start, end);
  22. }
  23. /*
  24. * Open a file with the specified read/write mode.
  25. */
  26. int ncp_make_open(struct inode *inode, int right)
  27. {
  28. int error;
  29. int access;
  30. error = -EINVAL;
  31. if (!inode) {
  32. pr_err("%s: got NULL inode\n", __func__);
  33. goto out;
  34. }
  35. ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n",
  36. atomic_read(&NCP_FINFO(inode)->opened),
  37. NCP_FINFO(inode)->volNumber,
  38. NCP_FINFO(inode)->dirEntNum);
  39. error = -EACCES;
  40. mutex_lock(&NCP_FINFO(inode)->open_mutex);
  41. if (!atomic_read(&NCP_FINFO(inode)->opened)) {
  42. struct ncp_entry_info finfo;
  43. int result;
  44. /* tries max. rights */
  45. finfo.access = O_RDWR;
  46. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  47. inode, NULL, OC_MODE_OPEN,
  48. 0, AR_READ | AR_WRITE, &finfo);
  49. if (!result)
  50. goto update;
  51. /* RDWR did not succeeded, try readonly or writeonly as requested */
  52. switch (right) {
  53. case O_RDONLY:
  54. finfo.access = O_RDONLY;
  55. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  56. inode, NULL, OC_MODE_OPEN,
  57. 0, AR_READ, &finfo);
  58. break;
  59. case O_WRONLY:
  60. finfo.access = O_WRONLY;
  61. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  62. inode, NULL, OC_MODE_OPEN,
  63. 0, AR_WRITE, &finfo);
  64. break;
  65. }
  66. if (result) {
  67. ncp_vdbg("failed, result=%d\n", result);
  68. goto out_unlock;
  69. }
  70. /*
  71. * Update the inode information.
  72. */
  73. update:
  74. ncp_update_inode(inode, &finfo);
  75. atomic_set(&NCP_FINFO(inode)->opened, 1);
  76. }
  77. access = NCP_FINFO(inode)->access;
  78. ncp_vdbg("file open, access=%x\n", access);
  79. if (access == right || access == O_RDWR) {
  80. atomic_inc(&NCP_FINFO(inode)->opened);
  81. error = 0;
  82. }
  83. out_unlock:
  84. mutex_unlock(&NCP_FINFO(inode)->open_mutex);
  85. out:
  86. return error;
  87. }
  88. static ssize_t
  89. ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
  90. {
  91. struct file *file = iocb->ki_filp;
  92. struct inode *inode = file_inode(file);
  93. size_t already_read = 0;
  94. off_t pos = iocb->ki_pos;
  95. size_t bufsize;
  96. int error;
  97. void *freepage;
  98. size_t freelen;
  99. ncp_dbg(1, "enter %pD2\n", file);
  100. if (!iov_iter_count(to))
  101. return 0;
  102. if (pos > inode->i_sb->s_maxbytes)
  103. return 0;
  104. iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
  105. error = ncp_make_open(inode, O_RDONLY);
  106. if (error) {
  107. ncp_dbg(1, "open failed, error=%d\n", error);
  108. return error;
  109. }
  110. bufsize = NCP_SERVER(inode)->buffer_size;
  111. error = -EIO;
  112. freelen = ncp_read_bounce_size(bufsize);
  113. freepage = vmalloc(freelen);
  114. if (!freepage)
  115. goto outrel;
  116. error = 0;
  117. /* First read in as much as possible for each bufsize. */
  118. while (iov_iter_count(to)) {
  119. int read_this_time;
  120. size_t to_read = min_t(size_t,
  121. bufsize - (pos % bufsize),
  122. iov_iter_count(to));
  123. error = ncp_read_bounce(NCP_SERVER(inode),
  124. NCP_FINFO(inode)->file_handle,
  125. pos, to_read, to, &read_this_time,
  126. freepage, freelen);
  127. if (error) {
  128. error = -EIO; /* NW errno -> Linux errno */
  129. break;
  130. }
  131. pos += read_this_time;
  132. already_read += read_this_time;
  133. if (read_this_time != to_read)
  134. break;
  135. }
  136. vfree(freepage);
  137. iocb->ki_pos = pos;
  138. file_accessed(file);
  139. ncp_dbg(1, "exit %pD2\n", file);
  140. outrel:
  141. ncp_inode_close(inode);
  142. return already_read ? already_read : error;
  143. }
  144. static ssize_t
  145. ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
  146. {
  147. struct file *file = iocb->ki_filp;
  148. struct inode *inode = file_inode(file);
  149. size_t already_written = 0;
  150. size_t bufsize;
  151. int errno;
  152. void *bouncebuffer;
  153. off_t pos;
  154. ncp_dbg(1, "enter %pD2\n", file);
  155. errno = generic_write_checks(iocb, from);
  156. if (errno <= 0)
  157. return errno;
  158. errno = ncp_make_open(inode, O_WRONLY);
  159. if (errno) {
  160. ncp_dbg(1, "open failed, error=%d\n", errno);
  161. return errno;
  162. }
  163. bufsize = NCP_SERVER(inode)->buffer_size;
  164. errno = file_update_time(file);
  165. if (errno)
  166. goto outrel;
  167. bouncebuffer = vmalloc(bufsize);
  168. if (!bouncebuffer) {
  169. errno = -EIO; /* -ENOMEM */
  170. goto outrel;
  171. }
  172. pos = iocb->ki_pos;
  173. while (iov_iter_count(from)) {
  174. int written_this_time;
  175. size_t to_write = min_t(size_t,
  176. bufsize - (pos % bufsize),
  177. iov_iter_count(from));
  178. if (copy_from_iter(bouncebuffer, to_write, from) != to_write) {
  179. errno = -EFAULT;
  180. break;
  181. }
  182. if (ncp_write_kernel(NCP_SERVER(inode),
  183. NCP_FINFO(inode)->file_handle,
  184. pos, to_write, bouncebuffer, &written_this_time) != 0) {
  185. errno = -EIO;
  186. break;
  187. }
  188. pos += written_this_time;
  189. already_written += written_this_time;
  190. if (written_this_time != to_write)
  191. break;
  192. }
  193. vfree(bouncebuffer);
  194. iocb->ki_pos = pos;
  195. if (pos > i_size_read(inode)) {
  196. mutex_lock(&inode->i_mutex);
  197. if (pos > i_size_read(inode))
  198. i_size_write(inode, pos);
  199. mutex_unlock(&inode->i_mutex);
  200. }
  201. ncp_dbg(1, "exit %pD2\n", file);
  202. outrel:
  203. ncp_inode_close(inode);
  204. return already_written ? already_written : errno;
  205. }
  206. static int ncp_release(struct inode *inode, struct file *file) {
  207. if (ncp_make_closed(inode)) {
  208. ncp_dbg(1, "failed to close\n");
  209. }
  210. return 0;
  211. }
  212. const struct file_operations ncp_file_operations =
  213. {
  214. .llseek = generic_file_llseek,
  215. .read_iter = ncp_file_read_iter,
  216. .write_iter = ncp_file_write_iter,
  217. .unlocked_ioctl = ncp_ioctl,
  218. #ifdef CONFIG_COMPAT
  219. .compat_ioctl = ncp_compat_ioctl,
  220. #endif
  221. .mmap = ncp_mmap,
  222. .release = ncp_release,
  223. .fsync = ncp_fsync,
  224. };
  225. const struct inode_operations ncp_file_inode_operations =
  226. {
  227. .setattr = ncp_notify_change,
  228. };