file.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #include <asm/uaccess.h>
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/stat.h>
  14. #include <linux/mm.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/sched.h>
  17. #include "ncp_fs.h"
  18. static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  19. {
  20. return filemap_write_and_wait_range(file->f_mapping, start, end);
  21. }
  22. /*
  23. * Open a file with the specified read/write mode.
  24. */
  25. int ncp_make_open(struct inode *inode, int right)
  26. {
  27. int error;
  28. int access;
  29. error = -EINVAL;
  30. if (!inode) {
  31. printk(KERN_ERR "ncp_make_open: got NULL inode\n");
  32. goto out;
  33. }
  34. DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
  35. atomic_read(&NCP_FINFO(inode)->opened),
  36. NCP_FINFO(inode)->volNumber,
  37. NCP_FINFO(inode)->dirEntNum);
  38. error = -EACCES;
  39. mutex_lock(&NCP_FINFO(inode)->open_mutex);
  40. if (!atomic_read(&NCP_FINFO(inode)->opened)) {
  41. struct ncp_entry_info finfo;
  42. int result;
  43. /* tries max. rights */
  44. finfo.access = O_RDWR;
  45. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  46. inode, NULL, OC_MODE_OPEN,
  47. 0, AR_READ | AR_WRITE, &finfo);
  48. if (!result)
  49. goto update;
  50. /* RDWR did not succeeded, try readonly or writeonly as requested */
  51. switch (right) {
  52. case O_RDONLY:
  53. finfo.access = O_RDONLY;
  54. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  55. inode, NULL, OC_MODE_OPEN,
  56. 0, AR_READ, &finfo);
  57. break;
  58. case O_WRONLY:
  59. finfo.access = O_WRONLY;
  60. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  61. inode, NULL, OC_MODE_OPEN,
  62. 0, AR_WRITE, &finfo);
  63. break;
  64. }
  65. if (result) {
  66. PPRINTK("ncp_make_open: failed, result=%d\n", result);
  67. goto out_unlock;
  68. }
  69. /*
  70. * Update the inode information.
  71. */
  72. update:
  73. ncp_update_inode(inode, &finfo);
  74. atomic_set(&NCP_FINFO(inode)->opened, 1);
  75. }
  76. access = NCP_FINFO(inode)->access;
  77. PPRINTK("ncp_make_open: file open, access=%x\n", access);
  78. if (access == right || access == O_RDWR) {
  79. atomic_inc(&NCP_FINFO(inode)->opened);
  80. error = 0;
  81. }
  82. out_unlock:
  83. mutex_unlock(&NCP_FINFO(inode)->open_mutex);
  84. out:
  85. return error;
  86. }
  87. static ssize_t
  88. ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  89. {
  90. struct dentry *dentry = file->f_path.dentry;
  91. struct inode *inode = dentry->d_inode;
  92. size_t already_read = 0;
  93. off_t pos;
  94. size_t bufsize;
  95. int error;
  96. void* freepage;
  97. size_t freelen;
  98. DPRINTK("ncp_file_read: enter %s/%s\n",
  99. dentry->d_parent->d_name.name, dentry->d_name.name);
  100. pos = *ppos;
  101. if ((ssize_t) count < 0) {
  102. return -EINVAL;
  103. }
  104. if (!count)
  105. return 0;
  106. if (pos > inode->i_sb->s_maxbytes)
  107. return 0;
  108. if (pos + count > inode->i_sb->s_maxbytes) {
  109. count = inode->i_sb->s_maxbytes - pos;
  110. }
  111. error = ncp_make_open(inode, O_RDONLY);
  112. if (error) {
  113. DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
  114. return error;
  115. }
  116. bufsize = NCP_SERVER(inode)->buffer_size;
  117. error = -EIO;
  118. freelen = ncp_read_bounce_size(bufsize);
  119. freepage = vmalloc(freelen);
  120. if (!freepage)
  121. goto outrel;
  122. error = 0;
  123. /* First read in as much as possible for each bufsize. */
  124. while (already_read < count) {
  125. int read_this_time;
  126. size_t to_read = min_t(unsigned int,
  127. bufsize - (pos % bufsize),
  128. count - already_read);
  129. error = ncp_read_bounce(NCP_SERVER(inode),
  130. NCP_FINFO(inode)->file_handle,
  131. pos, to_read, buf, &read_this_time,
  132. freepage, freelen);
  133. if (error) {
  134. error = -EIO; /* NW errno -> Linux errno */
  135. break;
  136. }
  137. pos += read_this_time;
  138. buf += read_this_time;
  139. already_read += read_this_time;
  140. if (read_this_time != to_read) {
  141. break;
  142. }
  143. }
  144. vfree(freepage);
  145. *ppos = pos;
  146. file_accessed(file);
  147. DPRINTK("ncp_file_read: exit %s/%s\n",
  148. dentry->d_parent->d_name.name, dentry->d_name.name);
  149. outrel:
  150. ncp_inode_close(inode);
  151. return already_read ? already_read : error;
  152. }
  153. static ssize_t
  154. ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  155. {
  156. struct dentry *dentry = file->f_path.dentry;
  157. struct inode *inode = dentry->d_inode;
  158. size_t already_written = 0;
  159. off_t pos;
  160. size_t bufsize;
  161. int errno;
  162. void* bouncebuffer;
  163. DPRINTK("ncp_file_write: enter %s/%s\n",
  164. dentry->d_parent->d_name.name, dentry->d_name.name);
  165. if ((ssize_t) count < 0)
  166. return -EINVAL;
  167. pos = *ppos;
  168. if (file->f_flags & O_APPEND) {
  169. pos = i_size_read(inode);
  170. }
  171. if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
  172. if (pos >= MAX_NON_LFS) {
  173. return -EFBIG;
  174. }
  175. if (count > MAX_NON_LFS - (u32)pos) {
  176. count = MAX_NON_LFS - (u32)pos;
  177. }
  178. }
  179. if (pos >= inode->i_sb->s_maxbytes) {
  180. if (count || pos > inode->i_sb->s_maxbytes) {
  181. return -EFBIG;
  182. }
  183. }
  184. if (pos + count > inode->i_sb->s_maxbytes) {
  185. count = inode->i_sb->s_maxbytes - pos;
  186. }
  187. if (!count)
  188. return 0;
  189. errno = ncp_make_open(inode, O_WRONLY);
  190. if (errno) {
  191. DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
  192. return errno;
  193. }
  194. bufsize = NCP_SERVER(inode)->buffer_size;
  195. already_written = 0;
  196. errno = file_update_time(file);
  197. if (errno)
  198. goto outrel;
  199. bouncebuffer = vmalloc(bufsize);
  200. if (!bouncebuffer) {
  201. errno = -EIO; /* -ENOMEM */
  202. goto outrel;
  203. }
  204. while (already_written < count) {
  205. int written_this_time;
  206. size_t to_write = min_t(unsigned int,
  207. bufsize - (pos % bufsize),
  208. count - already_written);
  209. if (copy_from_user(bouncebuffer, buf, to_write)) {
  210. errno = -EFAULT;
  211. break;
  212. }
  213. if (ncp_write_kernel(NCP_SERVER(inode),
  214. NCP_FINFO(inode)->file_handle,
  215. pos, to_write, bouncebuffer, &written_this_time) != 0) {
  216. errno = -EIO;
  217. break;
  218. }
  219. pos += written_this_time;
  220. buf += written_this_time;
  221. already_written += written_this_time;
  222. if (written_this_time != to_write) {
  223. break;
  224. }
  225. }
  226. vfree(bouncebuffer);
  227. *ppos = pos;
  228. if (pos > i_size_read(inode)) {
  229. mutex_lock(&inode->i_mutex);
  230. if (pos > i_size_read(inode))
  231. i_size_write(inode, pos);
  232. mutex_unlock(&inode->i_mutex);
  233. }
  234. DPRINTK("ncp_file_write: exit %s/%s\n",
  235. dentry->d_parent->d_name.name, dentry->d_name.name);
  236. outrel:
  237. ncp_inode_close(inode);
  238. return already_written ? already_written : errno;
  239. }
  240. static int ncp_release(struct inode *inode, struct file *file) {
  241. if (ncp_make_closed(inode)) {
  242. DPRINTK("ncp_release: failed to close\n");
  243. }
  244. return 0;
  245. }
  246. const struct file_operations ncp_file_operations =
  247. {
  248. .llseek = generic_file_llseek,
  249. .read = ncp_file_read,
  250. .write = ncp_file_write,
  251. .unlocked_ioctl = ncp_ioctl,
  252. #ifdef CONFIG_COMPAT
  253. .compat_ioctl = ncp_compat_ioctl,
  254. #endif
  255. .mmap = ncp_mmap,
  256. .release = ncp_release,
  257. .fsync = ncp_fsync,
  258. };
  259. const struct inode_operations ncp_file_inode_operations =
  260. {
  261. .setattr = ncp_notify_change,
  262. };