file.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <asm/system.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, int datasync)
  20. {
  21. return 0;
  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. printk(KERN_ERR "ncp_make_open: got NULL inode\n");
  33. goto out;
  34. }
  35. DPRINTK("ncp_make_open: 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. PPRINTK("ncp_make_open: 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. PPRINTK("ncp_make_open: 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(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  90. {
  91. struct dentry *dentry = file->f_path.dentry;
  92. struct inode *inode = dentry->d_inode;
  93. size_t already_read = 0;
  94. off_t pos;
  95. size_t bufsize;
  96. int error;
  97. void* freepage;
  98. size_t freelen;
  99. DPRINTK("ncp_file_read: enter %s/%s\n",
  100. dentry->d_parent->d_name.name, dentry->d_name.name);
  101. pos = *ppos;
  102. if ((ssize_t) count < 0) {
  103. return -EINVAL;
  104. }
  105. if (!count)
  106. return 0;
  107. if (pos > inode->i_sb->s_maxbytes)
  108. return 0;
  109. if (pos + count > inode->i_sb->s_maxbytes) {
  110. count = inode->i_sb->s_maxbytes - pos;
  111. }
  112. error = ncp_make_open(inode, O_RDONLY);
  113. if (error) {
  114. DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
  115. return error;
  116. }
  117. bufsize = NCP_SERVER(inode)->buffer_size;
  118. error = -EIO;
  119. freelen = ncp_read_bounce_size(bufsize);
  120. freepage = vmalloc(freelen);
  121. if (!freepage)
  122. goto outrel;
  123. error = 0;
  124. /* First read in as much as possible for each bufsize. */
  125. while (already_read < count) {
  126. int read_this_time;
  127. size_t to_read = min_t(unsigned int,
  128. bufsize - (pos % bufsize),
  129. count - already_read);
  130. error = ncp_read_bounce(NCP_SERVER(inode),
  131. NCP_FINFO(inode)->file_handle,
  132. pos, to_read, buf, &read_this_time,
  133. freepage, freelen);
  134. if (error) {
  135. error = -EIO; /* NW errno -> Linux errno */
  136. break;
  137. }
  138. pos += read_this_time;
  139. buf += read_this_time;
  140. already_read += read_this_time;
  141. if (read_this_time != to_read) {
  142. break;
  143. }
  144. }
  145. vfree(freepage);
  146. *ppos = pos;
  147. file_accessed(file);
  148. DPRINTK("ncp_file_read: exit %s/%s\n",
  149. dentry->d_parent->d_name.name, dentry->d_name.name);
  150. outrel:
  151. ncp_inode_close(inode);
  152. return already_read ? already_read : error;
  153. }
  154. static ssize_t
  155. ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  156. {
  157. struct dentry *dentry = file->f_path.dentry;
  158. struct inode *inode = dentry->d_inode;
  159. size_t already_written = 0;
  160. off_t pos;
  161. size_t bufsize;
  162. int errno;
  163. void* bouncebuffer;
  164. DPRINTK("ncp_file_write: enter %s/%s\n",
  165. dentry->d_parent->d_name.name, dentry->d_name.name);
  166. if ((ssize_t) count < 0)
  167. return -EINVAL;
  168. pos = *ppos;
  169. if (file->f_flags & O_APPEND) {
  170. pos = i_size_read(inode);
  171. }
  172. if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
  173. if (pos >= MAX_NON_LFS) {
  174. return -EFBIG;
  175. }
  176. if (count > MAX_NON_LFS - (u32)pos) {
  177. count = MAX_NON_LFS - (u32)pos;
  178. }
  179. }
  180. if (pos >= inode->i_sb->s_maxbytes) {
  181. if (count || pos > inode->i_sb->s_maxbytes) {
  182. return -EFBIG;
  183. }
  184. }
  185. if (pos + count > inode->i_sb->s_maxbytes) {
  186. count = inode->i_sb->s_maxbytes - pos;
  187. }
  188. if (!count)
  189. return 0;
  190. errno = ncp_make_open(inode, O_WRONLY);
  191. if (errno) {
  192. DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
  193. return errno;
  194. }
  195. bufsize = NCP_SERVER(inode)->buffer_size;
  196. already_written = 0;
  197. bouncebuffer = vmalloc(bufsize);
  198. if (!bouncebuffer) {
  199. errno = -EIO; /* -ENOMEM */
  200. goto outrel;
  201. }
  202. while (already_written < count) {
  203. int written_this_time;
  204. size_t to_write = min_t(unsigned int,
  205. bufsize - (pos % bufsize),
  206. count - already_written);
  207. if (copy_from_user(bouncebuffer, buf, to_write)) {
  208. errno = -EFAULT;
  209. break;
  210. }
  211. if (ncp_write_kernel(NCP_SERVER(inode),
  212. NCP_FINFO(inode)->file_handle,
  213. pos, to_write, bouncebuffer, &written_this_time) != 0) {
  214. errno = -EIO;
  215. break;
  216. }
  217. pos += written_this_time;
  218. buf += written_this_time;
  219. already_written += written_this_time;
  220. if (written_this_time != to_write) {
  221. break;
  222. }
  223. }
  224. vfree(bouncebuffer);
  225. file_update_time(file);
  226. *ppos = pos;
  227. if (pos > i_size_read(inode)) {
  228. mutex_lock(&inode->i_mutex);
  229. if (pos > i_size_read(inode))
  230. i_size_write(inode, pos);
  231. mutex_unlock(&inode->i_mutex);
  232. }
  233. DPRINTK("ncp_file_write: exit %s/%s\n",
  234. dentry->d_parent->d_name.name, dentry->d_name.name);
  235. outrel:
  236. ncp_inode_close(inode);
  237. return already_written ? already_written : errno;
  238. }
  239. static int ncp_release(struct inode *inode, struct file *file) {
  240. if (ncp_make_closed(inode)) {
  241. DPRINTK("ncp_release: failed to close\n");
  242. }
  243. return 0;
  244. }
  245. const struct file_operations ncp_file_operations =
  246. {
  247. .llseek = generic_file_llseek,
  248. .read = ncp_file_read,
  249. .write = ncp_file_write,
  250. .unlocked_ioctl = ncp_ioctl,
  251. #ifdef CONFIG_COMPAT
  252. .compat_ioctl = ncp_compat_ioctl,
  253. #endif
  254. .mmap = ncp_mmap,
  255. .release = ncp_release,
  256. .fsync = ncp_fsync,
  257. };
  258. const struct inode_operations ncp_file_inode_operations =
  259. {
  260. .setattr = ncp_notify_change,
  261. };