file.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/sched.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/ncp_fs.h>
  21. #include "ncplib_kernel.h"
  22. static int ncp_fsync(struct file *file, struct dentry *dentry, int datasync)
  23. {
  24. return 0;
  25. }
  26. /*
  27. * Open a file with the specified read/write mode.
  28. */
  29. int ncp_make_open(struct inode *inode, int right)
  30. {
  31. int error;
  32. int access;
  33. error = -EINVAL;
  34. if (!inode) {
  35. printk(KERN_ERR "ncp_make_open: got NULL inode\n");
  36. goto out;
  37. }
  38. DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
  39. atomic_read(&NCP_FINFO(inode)->opened),
  40. NCP_FINFO(inode)->volNumber,
  41. NCP_FINFO(inode)->dirEntNum);
  42. error = -EACCES;
  43. mutex_lock(&NCP_FINFO(inode)->open_mutex);
  44. if (!atomic_read(&NCP_FINFO(inode)->opened)) {
  45. struct ncp_entry_info finfo;
  46. int result;
  47. /* tries max. rights */
  48. finfo.access = O_RDWR;
  49. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  50. inode, NULL, OC_MODE_OPEN,
  51. 0, AR_READ | AR_WRITE, &finfo);
  52. if (!result)
  53. goto update;
  54. /* RDWR did not succeeded, try readonly or writeonly as requested */
  55. switch (right) {
  56. case O_RDONLY:
  57. finfo.access = O_RDONLY;
  58. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  59. inode, NULL, OC_MODE_OPEN,
  60. 0, AR_READ, &finfo);
  61. break;
  62. case O_WRONLY:
  63. finfo.access = O_WRONLY;
  64. result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  65. inode, NULL, OC_MODE_OPEN,
  66. 0, AR_WRITE, &finfo);
  67. break;
  68. }
  69. if (result) {
  70. PPRINTK("ncp_make_open: failed, result=%d\n", result);
  71. goto out_unlock;
  72. }
  73. /*
  74. * Update the inode information.
  75. */
  76. update:
  77. ncp_update_inode(inode, &finfo);
  78. atomic_set(&NCP_FINFO(inode)->opened, 1);
  79. }
  80. access = NCP_FINFO(inode)->access;
  81. PPRINTK("ncp_make_open: file open, access=%x\n", access);
  82. if (access == right || access == O_RDWR) {
  83. atomic_inc(&NCP_FINFO(inode)->opened);
  84. error = 0;
  85. }
  86. out_unlock:
  87. mutex_unlock(&NCP_FINFO(inode)->open_mutex);
  88. out:
  89. return error;
  90. }
  91. static ssize_t
  92. ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  93. {
  94. struct dentry *dentry = file->f_path.dentry;
  95. struct inode *inode = dentry->d_inode;
  96. size_t already_read = 0;
  97. off_t pos;
  98. size_t bufsize;
  99. int error;
  100. void* freepage;
  101. size_t freelen;
  102. DPRINTK("ncp_file_read: enter %s/%s\n",
  103. dentry->d_parent->d_name.name, dentry->d_name.name);
  104. if (!ncp_conn_valid(NCP_SERVER(inode)))
  105. return -EIO;
  106. pos = *ppos;
  107. if ((ssize_t) count < 0) {
  108. return -EINVAL;
  109. }
  110. if (!count)
  111. return 0;
  112. if (pos > inode->i_sb->s_maxbytes)
  113. return 0;
  114. if (pos + count > inode->i_sb->s_maxbytes) {
  115. count = inode->i_sb->s_maxbytes - pos;
  116. }
  117. error = ncp_make_open(inode, O_RDONLY);
  118. if (error) {
  119. DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
  120. return error;
  121. }
  122. bufsize = NCP_SERVER(inode)->buffer_size;
  123. error = -EIO;
  124. freelen = ncp_read_bounce_size(bufsize);
  125. freepage = vmalloc(freelen);
  126. if (!freepage)
  127. goto outrel;
  128. error = 0;
  129. /* First read in as much as possible for each bufsize. */
  130. while (already_read < count) {
  131. int read_this_time;
  132. size_t to_read = min_t(unsigned int,
  133. bufsize - (pos % bufsize),
  134. count - already_read);
  135. error = ncp_read_bounce(NCP_SERVER(inode),
  136. NCP_FINFO(inode)->file_handle,
  137. pos, to_read, buf, &read_this_time,
  138. freepage, freelen);
  139. if (error) {
  140. error = -EIO; /* NW errno -> Linux errno */
  141. break;
  142. }
  143. pos += read_this_time;
  144. buf += read_this_time;
  145. already_read += read_this_time;
  146. if (read_this_time != to_read) {
  147. break;
  148. }
  149. }
  150. vfree(freepage);
  151. *ppos = pos;
  152. file_accessed(file);
  153. DPRINTK("ncp_file_read: exit %s/%s\n",
  154. dentry->d_parent->d_name.name, dentry->d_name.name);
  155. outrel:
  156. ncp_inode_close(inode);
  157. return already_read ? already_read : error;
  158. }
  159. static ssize_t
  160. ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  161. {
  162. struct dentry *dentry = file->f_path.dentry;
  163. struct inode *inode = dentry->d_inode;
  164. size_t already_written = 0;
  165. off_t pos;
  166. size_t bufsize;
  167. int errno;
  168. void* bouncebuffer;
  169. DPRINTK("ncp_file_write: enter %s/%s\n",
  170. dentry->d_parent->d_name.name, dentry->d_name.name);
  171. if (!ncp_conn_valid(NCP_SERVER(inode)))
  172. return -EIO;
  173. if ((ssize_t) count < 0)
  174. return -EINVAL;
  175. pos = *ppos;
  176. if (file->f_flags & O_APPEND) {
  177. pos = inode->i_size;
  178. }
  179. if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
  180. if (pos >= MAX_NON_LFS) {
  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. 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. static loff_t ncp_remote_llseek(struct file *file, loff_t offset, int origin)
  250. {
  251. loff_t ret;
  252. lock_kernel();
  253. ret = generic_file_llseek_unlocked(file, offset, origin);
  254. unlock_kernel();
  255. return ret;
  256. }
  257. const struct file_operations ncp_file_operations =
  258. {
  259. .llseek = ncp_remote_llseek,
  260. .read = ncp_file_read,
  261. .write = ncp_file_write,
  262. .ioctl = ncp_ioctl,
  263. #ifdef CONFIG_COMPAT
  264. .compat_ioctl = ncp_compat_ioctl,
  265. #endif
  266. .mmap = ncp_mmap,
  267. .release = ncp_release,
  268. .fsync = ncp_fsync,
  269. };
  270. const struct inode_operations ncp_file_inode_operations =
  271. {
  272. .setattr = ncp_notify_change,
  273. };