file.c 6.7 KB

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