file.c 6.8 KB

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