blob: 8f5074e1ecb9eaf2b39b0064edc17f5528516ca2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <linux/time.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/fcntl.h>
15#include <linux/stat.h>
16#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/vmalloc.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Al Viro32c419d2011-01-12 17:37:47 -050020#include "ncp_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Josef Bacik02c24a82011-07-16 20:44:56 -040022static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Josef Bacik02c24a82011-07-16 20:44:56 -040024 return filemap_write_and_wait_range(file->f_mapping, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
27/*
28 * Open a file with the specified read/write mode.
29 */
30int ncp_make_open(struct inode *inode, int right)
31{
32 int error;
33 int access;
34
35 error = -EINVAL;
36 if (!inode) {
37 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
38 goto out;
39 }
40
41 DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
42 atomic_read(&NCP_FINFO(inode)->opened),
43 NCP_FINFO(inode)->volNumber,
44 NCP_FINFO(inode)->dirEntNum);
45 error = -EACCES;
Ingo Molnar8e3f9042006-03-23 03:00:43 -080046 mutex_lock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
48 struct ncp_entry_info finfo;
49 int result;
50
51 /* tries max. rights */
52 finfo.access = O_RDWR;
53 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
54 inode, NULL, OC_MODE_OPEN,
55 0, AR_READ | AR_WRITE, &finfo);
56 if (!result)
57 goto update;
58 /* RDWR did not succeeded, try readonly or writeonly as requested */
59 switch (right) {
60 case O_RDONLY:
61 finfo.access = O_RDONLY;
62 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
63 inode, NULL, OC_MODE_OPEN,
64 0, AR_READ, &finfo);
65 break;
66 case O_WRONLY:
67 finfo.access = O_WRONLY;
68 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
69 inode, NULL, OC_MODE_OPEN,
70 0, AR_WRITE, &finfo);
71 break;
72 }
73 if (result) {
74 PPRINTK("ncp_make_open: failed, result=%d\n", result);
75 goto out_unlock;
76 }
77 /*
78 * Update the inode information.
79 */
80 update:
81 ncp_update_inode(inode, &finfo);
82 atomic_set(&NCP_FINFO(inode)->opened, 1);
83 }
84
85 access = NCP_FINFO(inode)->access;
86 PPRINTK("ncp_make_open: file open, access=%x\n", access);
87 if (access == right || access == O_RDWR) {
88 atomic_inc(&NCP_FINFO(inode)->opened);
89 error = 0;
90 }
91
92out_unlock:
Ingo Molnar8e3f9042006-03-23 03:00:43 -080093 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094out:
95 return error;
96}
97
98static ssize_t
99ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
100{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800101 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct inode *inode = dentry->d_inode;
103 size_t already_read = 0;
104 off_t pos;
105 size_t bufsize;
106 int error;
107 void* freepage;
108 size_t freelen;
109
Al Viro84eb3532013-09-16 10:59:55 -0400110 DPRINTK("ncp_file_read: enter %pd2\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 pos = *ppos;
113
114 if ((ssize_t) count < 0) {
115 return -EINVAL;
116 }
117 if (!count)
118 return 0;
119 if (pos > inode->i_sb->s_maxbytes)
120 return 0;
121 if (pos + count > inode->i_sb->s_maxbytes) {
122 count = inode->i_sb->s_maxbytes - pos;
123 }
124
125 error = ncp_make_open(inode, O_RDONLY);
126 if (error) {
127 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
128 return error;
129 }
130
131 bufsize = NCP_SERVER(inode)->buffer_size;
132
133 error = -EIO;
134 freelen = ncp_read_bounce_size(bufsize);
135 freepage = vmalloc(freelen);
136 if (!freepage)
137 goto outrel;
138 error = 0;
139 /* First read in as much as possible for each bufsize. */
140 while (already_read < count) {
141 int read_this_time;
142 size_t to_read = min_t(unsigned int,
143 bufsize - (pos % bufsize),
144 count - already_read);
145
146 error = ncp_read_bounce(NCP_SERVER(inode),
147 NCP_FINFO(inode)->file_handle,
148 pos, to_read, buf, &read_this_time,
149 freepage, freelen);
150 if (error) {
151 error = -EIO; /* NW errno -> Linux errno */
152 break;
153 }
154 pos += read_this_time;
155 buf += read_this_time;
156 already_read += read_this_time;
157
158 if (read_this_time != to_read) {
159 break;
160 }
161 }
162 vfree(freepage);
163
164 *ppos = pos;
165
166 file_accessed(file);
167
Al Viro84eb3532013-09-16 10:59:55 -0400168 DPRINTK("ncp_file_read: exit %pd2\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169outrel:
170 ncp_inode_close(inode);
171 return already_read ? already_read : error;
172}
173
174static ssize_t
175ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
176{
Josef Sipek92e5bae2006-12-08 02:37:22 -0800177 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct inode *inode = dentry->d_inode;
179 size_t already_written = 0;
180 off_t pos;
181 size_t bufsize;
182 int errno;
183 void* bouncebuffer;
184
Al Viro84eb3532013-09-16 10:59:55 -0400185 DPRINTK("ncp_file_write: enter %pd2\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if ((ssize_t) count < 0)
187 return -EINVAL;
188 pos = *ppos;
189 if (file->f_flags & O_APPEND) {
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200190 pos = i_size_read(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
194 if (pos >= MAX_NON_LFS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 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 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
215 return errno;
216 }
217 bufsize = NCP_SERVER(inode)->buffer_size;
218
219 already_written = 0;
220
Josef Bacikc3b2da32012-03-26 09:59:21 -0400221 errno = file_update_time(file);
222 if (errno)
223 goto outrel;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *ppos = pos;
257
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200258 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Al Viro84eb3532013-09-16 10:59:55 -0400264 DPRINTK("ncp_file_write: exit %pd2\n", dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265outrel:
266 ncp_inode_close(inode);
267 return already_written ? already_written : errno;
268}
269
270static int ncp_release(struct inode *inode, struct file *file) {
271 if (ncp_make_closed(inode)) {
272 DPRINTK("ncp_release: failed to close\n");
273 }
274 return 0;
275}
276
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800277const struct file_operations ncp_file_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200279 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .read = ncp_file_read,
281 .write = ncp_file_write,
John Kacur93d84b62010-05-05 15:15:37 +0200282 .unlocked_ioctl = ncp_ioctl,
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700283#ifdef CONFIG_COMPAT
284 .compat_ioctl = ncp_compat_ioctl,
285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .mmap = ncp_mmap,
287 .release = ncp_release,
288 .fsync = ncp_fsync,
289};
290
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800291const struct inode_operations ncp_file_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 .setattr = ncp_notify_change,
294};