Merge branch 'drm-atmel-hlcdc-4.1-fixes' of git://github.com/bbrezillon/linux-at91...
[deliverable/linux.git] / fs / ncpfs / file.c
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
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <asm/uaccess.h>
12
13 #include <linux/time.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/fcntl.h>
17 #include <linux/stat.h>
18 #include <linux/mm.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sched.h>
21
22 #include "ncp_fs.h"
23
24 static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
25 {
26 return filemap_write_and_wait_range(file->f_mapping, start, end);
27 }
28
29 /*
30 * Open a file with the specified read/write mode.
31 */
32 int ncp_make_open(struct inode *inode, int right)
33 {
34 int error;
35 int access;
36
37 error = -EINVAL;
38 if (!inode) {
39 pr_err("%s: got NULL inode\n", __func__);
40 goto out;
41 }
42
43 ncp_dbg(1, "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;
48 mutex_lock(&NCP_FINFO(inode)->open_mutex);
49 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 ncp_vdbg("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 ncp_vdbg("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
94 out_unlock:
95 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
96 out:
97 return error;
98 }
99
100 static ssize_t
101 ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
102 {
103 struct inode *inode = file_inode(file);
104 size_t already_read = 0;
105 off_t pos;
106 size_t bufsize;
107 int error;
108 void* freepage;
109 size_t freelen;
110
111 ncp_dbg(1, "enter %pD2\n", file);
112
113 pos = *ppos;
114
115 if ((ssize_t) count < 0) {
116 return -EINVAL;
117 }
118 if (!count)
119 return 0;
120 if (pos > inode->i_sb->s_maxbytes)
121 return 0;
122 if (pos + count > inode->i_sb->s_maxbytes) {
123 count = inode->i_sb->s_maxbytes - pos;
124 }
125
126 error = ncp_make_open(inode, O_RDONLY);
127 if (error) {
128 ncp_dbg(1, "open failed, error=%d\n", error);
129 return error;
130 }
131
132 bufsize = NCP_SERVER(inode)->buffer_size;
133
134 error = -EIO;
135 freelen = ncp_read_bounce_size(bufsize);
136 freepage = vmalloc(freelen);
137 if (!freepage)
138 goto outrel;
139 error = 0;
140 /* First read in as much as possible for each bufsize. */
141 while (already_read < count) {
142 int read_this_time;
143 size_t to_read = min_t(unsigned int,
144 bufsize - (pos % bufsize),
145 count - already_read);
146
147 error = ncp_read_bounce(NCP_SERVER(inode),
148 NCP_FINFO(inode)->file_handle,
149 pos, to_read, buf, &read_this_time,
150 freepage, freelen);
151 if (error) {
152 error = -EIO; /* NW errno -> Linux errno */
153 break;
154 }
155 pos += read_this_time;
156 buf += read_this_time;
157 already_read += read_this_time;
158
159 if (read_this_time != to_read) {
160 break;
161 }
162 }
163 vfree(freepage);
164
165 *ppos = pos;
166
167 file_accessed(file);
168
169 ncp_dbg(1, "exit %pD2\n", file);
170 outrel:
171 ncp_inode_close(inode);
172 return already_read ? already_read : error;
173 }
174
175 static ssize_t
176 ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
177 {
178 struct inode *inode = file_inode(file);
179 size_t already_written = 0;
180 off_t pos;
181 size_t bufsize;
182 int errno;
183 void* bouncebuffer;
184
185 ncp_dbg(1, "enter %pD2\n", file);
186 if ((ssize_t) count < 0)
187 return -EINVAL;
188 pos = *ppos;
189 if (file->f_flags & O_APPEND) {
190 pos = i_size_read(inode);
191 }
192
193 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
194 if (pos >= MAX_NON_LFS) {
195 return -EFBIG;
196 }
197 if (count > MAX_NON_LFS - (u32)pos) {
198 count = MAX_NON_LFS - (u32)pos;
199 }
200 }
201 if (pos >= inode->i_sb->s_maxbytes) {
202 if (count || pos > inode->i_sb->s_maxbytes) {
203 return -EFBIG;
204 }
205 }
206 if (pos + count > inode->i_sb->s_maxbytes) {
207 count = inode->i_sb->s_maxbytes - pos;
208 }
209
210 if (!count)
211 return 0;
212 errno = ncp_make_open(inode, O_WRONLY);
213 if (errno) {
214 ncp_dbg(1, "open failed, error=%d\n", errno);
215 return errno;
216 }
217 bufsize = NCP_SERVER(inode)->buffer_size;
218
219 already_written = 0;
220
221 errno = file_update_time(file);
222 if (errno)
223 goto outrel;
224
225 bouncebuffer = vmalloc(bufsize);
226 if (!bouncebuffer) {
227 errno = -EIO; /* -ENOMEM */
228 goto outrel;
229 }
230 while (already_written < count) {
231 int written_this_time;
232 size_t to_write = min_t(unsigned int,
233 bufsize - (pos % bufsize),
234 count - already_written);
235
236 if (copy_from_user(bouncebuffer, buf, to_write)) {
237 errno = -EFAULT;
238 break;
239 }
240 if (ncp_write_kernel(NCP_SERVER(inode),
241 NCP_FINFO(inode)->file_handle,
242 pos, to_write, bouncebuffer, &written_this_time) != 0) {
243 errno = -EIO;
244 break;
245 }
246 pos += written_this_time;
247 buf += written_this_time;
248 already_written += written_this_time;
249
250 if (written_this_time != to_write) {
251 break;
252 }
253 }
254 vfree(bouncebuffer);
255
256 *ppos = pos;
257
258 if (pos > i_size_read(inode)) {
259 mutex_lock(&inode->i_mutex);
260 if (pos > i_size_read(inode))
261 i_size_write(inode, pos);
262 mutex_unlock(&inode->i_mutex);
263 }
264 ncp_dbg(1, "exit %pD2\n", file);
265 outrel:
266 ncp_inode_close(inode);
267 return already_written ? already_written : errno;
268 }
269
270 static int ncp_release(struct inode *inode, struct file *file) {
271 if (ncp_make_closed(inode)) {
272 ncp_dbg(1, "failed to close\n");
273 }
274 return 0;
275 }
276
277 const struct file_operations ncp_file_operations =
278 {
279 .llseek = generic_file_llseek,
280 .read = ncp_file_read,
281 .write = ncp_file_write,
282 .unlocked_ioctl = ncp_ioctl,
283 #ifdef CONFIG_COMPAT
284 .compat_ioctl = ncp_compat_ioctl,
285 #endif
286 .mmap = ncp_mmap,
287 .release = ncp_release,
288 .fsync = ncp_fsync,
289 };
290
291 const struct inode_operations ncp_file_inode_operations =
292 {
293 .setattr = ncp_notify_change,
294 };
This page took 0.044001 seconds and 5 git commands to generate.