blob: cb50aaf981dff5bf32dd2d4993ac14a1202dc27e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
9#include <asm/uaccess.h>
10#include <asm/system.h>
11
12#include <linux/time.h>
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/fcntl.h>
16#include <linux/stat.h>
17#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/vmalloc.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040019#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/ncp_fs.h>
22#include "ncplib_kernel.h"
23
Christoph Hellwig7ea80852010-05-26 17:53:25 +020024static int ncp_fsync(struct file *file, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 return 0;
27}
28
29/*
30 * Open a file with the specified read/write mode.
31 */
32int ncp_make_open(struct inode *inode, int right)
33{
34 int error;
35 int access;
36
37 error = -EINVAL;
38 if (!inode) {
39 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
40 goto out;
41 }
42
43 DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
44 atomic_read(&NCP_FINFO(inode)->opened),
45 NCP_FINFO(inode)->volNumber,
46 NCP_FINFO(inode)->dirEntNum);
47 error = -EACCES;
Ingo Molnar8e3f9042006-03-23 03:00:43 -080048 mutex_lock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
50 struct ncp_entry_info finfo;
51 int result;
52
53 /* tries max. rights */
54 finfo.access = O_RDWR;
55 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
56 inode, NULL, OC_MODE_OPEN,
57 0, AR_READ | AR_WRITE, &finfo);
58 if (!result)
59 goto update;
60 /* RDWR did not succeeded, try readonly or writeonly as requested */
61 switch (right) {
62 case O_RDONLY:
63 finfo.access = O_RDONLY;
64 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
65 inode, NULL, OC_MODE_OPEN,
66 0, AR_READ, &finfo);
67 break;
68 case O_WRONLY:
69 finfo.access = O_WRONLY;
70 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
71 inode, NULL, OC_MODE_OPEN,
72 0, AR_WRITE, &finfo);
73 break;
74 }
75 if (result) {
76 PPRINTK("ncp_make_open: failed, result=%d\n", result);
77 goto out_unlock;
78 }
79 /*
80 * Update the inode information.
81 */
82 update:
83 ncp_update_inode(inode, &finfo);
84 atomic_set(&NCP_FINFO(inode)->opened, 1);
85 }
86
87 access = NCP_FINFO(inode)->access;
88 PPRINTK("ncp_make_open: file open, access=%x\n", access);
89 if (access == right || access == O_RDWR) {
90 atomic_inc(&NCP_FINFO(inode)->opened);
91 error = 0;
92 }
93
94out_unlock:
Ingo Molnar8e3f9042006-03-23 03:00:43 -080095 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096out:
97 return error;
98}
99
100static ssize_t
101ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
102{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800103 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct inode *inode = dentry->d_inode;
105 size_t already_read = 0;
106 off_t pos;
107 size_t bufsize;
108 int error;
109 void* freepage;
110 size_t freelen;
111
112 DPRINTK("ncp_file_read: enter %s/%s\n",
113 dentry->d_parent->d_name.name, dentry->d_name.name);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 pos = *ppos;
116
117 if ((ssize_t) count < 0) {
118 return -EINVAL;
119 }
120 if (!count)
121 return 0;
122 if (pos > inode->i_sb->s_maxbytes)
123 return 0;
124 if (pos + count > inode->i_sb->s_maxbytes) {
125 count = inode->i_sb->s_maxbytes - pos;
126 }
127
128 error = ncp_make_open(inode, O_RDONLY);
129 if (error) {
130 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
131 return error;
132 }
133
134 bufsize = NCP_SERVER(inode)->buffer_size;
135
136 error = -EIO;
137 freelen = ncp_read_bounce_size(bufsize);
138 freepage = vmalloc(freelen);
139 if (!freepage)
140 goto outrel;
141 error = 0;
142 /* First read in as much as possible for each bufsize. */
143 while (already_read < count) {
144 int read_this_time;
145 size_t to_read = min_t(unsigned int,
146 bufsize - (pos % bufsize),
147 count - already_read);
148
149 error = ncp_read_bounce(NCP_SERVER(inode),
150 NCP_FINFO(inode)->file_handle,
151 pos, to_read, buf, &read_this_time,
152 freepage, freelen);
153 if (error) {
154 error = -EIO; /* NW errno -> Linux errno */
155 break;
156 }
157 pos += read_this_time;
158 buf += read_this_time;
159 already_read += read_this_time;
160
161 if (read_this_time != to_read) {
162 break;
163 }
164 }
165 vfree(freepage);
166
167 *ppos = pos;
168
169 file_accessed(file);
170
171 DPRINTK("ncp_file_read: exit %s/%s\n",
172 dentry->d_parent->d_name.name, dentry->d_name.name);
173outrel:
174 ncp_inode_close(inode);
175 return already_read ? already_read : error;
176}
177
178static ssize_t
179ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
180{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800181 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct inode *inode = dentry->d_inode;
183 size_t already_written = 0;
184 off_t pos;
185 size_t bufsize;
186 int errno;
187 void* bouncebuffer;
188
189 DPRINTK("ncp_file_write: enter %s/%s\n",
190 dentry->d_parent->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if ((ssize_t) count < 0)
192 return -EINVAL;
193 pos = *ppos;
194 if (file->f_flags & O_APPEND) {
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200195 pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
198 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
199 if (pos >= MAX_NON_LFS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EFBIG;
201 }
202 if (count > MAX_NON_LFS - (u32)pos) {
203 count = MAX_NON_LFS - (u32)pos;
204 }
205 }
206 if (pos >= inode->i_sb->s_maxbytes) {
207 if (count || pos > inode->i_sb->s_maxbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EFBIG;
209 }
210 }
211 if (pos + count > inode->i_sb->s_maxbytes) {
212 count = inode->i_sb->s_maxbytes - pos;
213 }
214
215 if (!count)
216 return 0;
217 errno = ncp_make_open(inode, O_WRONLY);
218 if (errno) {
219 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
220 return errno;
221 }
222 bufsize = NCP_SERVER(inode)->buffer_size;
223
224 already_written = 0;
225
226 bouncebuffer = vmalloc(bufsize);
227 if (!bouncebuffer) {
228 errno = -EIO; /* -ENOMEM */
229 goto outrel;
230 }
231 while (already_written < count) {
232 int written_this_time;
233 size_t to_write = min_t(unsigned int,
234 bufsize - (pos % bufsize),
235 count - already_written);
236
237 if (copy_from_user(bouncebuffer, buf, to_write)) {
238 errno = -EFAULT;
239 break;
240 }
241 if (ncp_write_kernel(NCP_SERVER(inode),
242 NCP_FINFO(inode)->file_handle,
243 pos, to_write, bouncebuffer, &written_this_time) != 0) {
244 errno = -EIO;
245 break;
246 }
247 pos += written_this_time;
248 buf += written_this_time;
249 already_written += written_this_time;
250
251 if (written_this_time != to_write) {
252 break;
253 }
254 }
255 vfree(bouncebuffer);
256
Christoph Hellwig870f4812006-01-09 20:52:01 -0800257 file_update_time(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 *ppos = pos;
260
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200261 if (pos > i_size_read(inode)) {
262 mutex_lock(&inode->i_mutex);
263 if (pos > i_size_read(inode))
264 i_size_write(inode, pos);
265 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 DPRINTK("ncp_file_write: exit %s/%s\n",
268 dentry->d_parent->d_name.name, dentry->d_name.name);
269outrel:
270 ncp_inode_close(inode);
271 return already_written ? already_written : errno;
272}
273
274static int ncp_release(struct inode *inode, struct file *file) {
275 if (ncp_make_closed(inode)) {
276 DPRINTK("ncp_release: failed to close\n");
277 }
278 return 0;
279}
280
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800281const struct file_operations ncp_file_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200283 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .read = ncp_file_read,
285 .write = ncp_file_write,
John Kacur93d84b62010-05-05 15:15:37 +0200286 .unlocked_ioctl = ncp_ioctl,
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700287#ifdef CONFIG_COMPAT
288 .compat_ioctl = ncp_compat_ioctl,
289#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 .mmap = ncp_mmap,
291 .release = ncp_release,
292 .fsync = ncp_fsync,
293};
294
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800295const struct inode_operations ncp_file_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 .setattr = ncp_notify_change,
298};