]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ncpfs/file.c
ocfs2: move generic_write_checks() before the alignment checks
[mirror_ubuntu-zesty-kernel.git] / fs / ncpfs / file.c
CommitLineData
1da177e4
LT
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
b41f8b84
JP
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
1da177e4 11#include <asm/uaccess.h>
1da177e4
LT
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>
1da177e4 19#include <linux/vmalloc.h>
e8edc6e0 20#include <linux/sched.h>
1da177e4 21
32c419d9 22#include "ncp_fs.h"
1da177e4 23
02c24a82 24static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1da177e4 25{
02c24a82 26 return filemap_write_and_wait_range(file->f_mapping, start, end);
1da177e4
LT
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) {
b41f8b84 39 pr_err("%s: got NULL inode\n", __func__);
1da177e4
LT
40 goto out;
41 }
42
d3b73ca1 43 ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n",
1da177e4
LT
44 atomic_read(&NCP_FINFO(inode)->opened),
45 NCP_FINFO(inode)->volNumber,
46 NCP_FINFO(inode)->dirEntNum);
47 error = -EACCES;
8e3f9045 48 mutex_lock(&NCP_FINFO(inode)->open_mutex);
1da177e4
LT
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) {
e45ca8ba 76 ncp_vdbg("failed, result=%d\n", result);
1da177e4
LT
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;
e45ca8ba 88 ncp_vdbg("file open, access=%x\n", access);
1da177e4
LT
89 if (access == right || access == O_RDWR) {
90 atomic_inc(&NCP_FINFO(inode)->opened);
91 error = 0;
92 }
93
94out_unlock:
8e3f9045 95 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
1da177e4
LT
96out:
97 return error;
98}
99
100static ssize_t
274a4886 101ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1da177e4 102{
274a4886 103 struct file *file = iocb->ki_filp;
a67f797d 104 struct inode *inode = file_inode(file);
1da177e4 105 size_t already_read = 0;
274a4886 106 off_t pos = iocb->ki_pos;
1da177e4
LT
107 size_t bufsize;
108 int error;
274a4886 109 void *freepage;
1da177e4
LT
110 size_t freelen;
111
a67f797d 112 ncp_dbg(1, "enter %pD2\n", file);
1da177e4 113
274a4886 114 if (!iov_iter_count(to))
1da177e4
LT
115 return 0;
116 if (pos > inode->i_sb->s_maxbytes)
117 return 0;
274a4886 118 iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
1da177e4
LT
119
120 error = ncp_make_open(inode, O_RDONLY);
121 if (error) {
d3b73ca1 122 ncp_dbg(1, "open failed, error=%d\n", error);
1da177e4
LT
123 return error;
124 }
125
126 bufsize = NCP_SERVER(inode)->buffer_size;
127
128 error = -EIO;
129 freelen = ncp_read_bounce_size(bufsize);
130 freepage = vmalloc(freelen);
131 if (!freepage)
132 goto outrel;
133 error = 0;
134 /* First read in as much as possible for each bufsize. */
274a4886 135 while (iov_iter_count(to)) {
1da177e4 136 int read_this_time;
274a4886 137 size_t to_read = min_t(size_t,
1da177e4 138 bufsize - (pos % bufsize),
274a4886 139 iov_iter_count(to));
1da177e4
LT
140
141 error = ncp_read_bounce(NCP_SERVER(inode),
142 NCP_FINFO(inode)->file_handle,
274a4886 143 pos, to_read, to, &read_this_time,
1da177e4
LT
144 freepage, freelen);
145 if (error) {
146 error = -EIO; /* NW errno -> Linux errno */
147 break;
148 }
149 pos += read_this_time;
1da177e4
LT
150 already_read += read_this_time;
151
274a4886 152 if (read_this_time != to_read)
1da177e4 153 break;
1da177e4
LT
154 }
155 vfree(freepage);
156
274a4886 157 iocb->ki_pos = pos;
1da177e4
LT
158
159 file_accessed(file);
160
a67f797d 161 ncp_dbg(1, "exit %pD2\n", file);
1da177e4
LT
162outrel:
163 ncp_inode_close(inode);
164 return already_read ? already_read : error;
165}
166
167static ssize_t
274a4886 168ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4 169{
274a4886 170 struct file *file = iocb->ki_filp;
a67f797d 171 struct inode *inode = file_inode(file);
1da177e4 172 size_t already_written = 0;
274a4886
AV
173 loff_t pos = iocb->ki_pos;
174 size_t count = iov_iter_count(from);
1da177e4
LT
175 size_t bufsize;
176 int errno;
274a4886 177 void *bouncebuffer;
1da177e4 178
a67f797d 179 ncp_dbg(1, "enter %pD2\n", file);
0fa6b005 180 errno = generic_write_checks(file, &pos, &count);
274a4886
AV
181 if (errno)
182 return errno;
183 iov_iter_truncate(from, count);
1da177e4
LT
184
185 if (!count)
186 return 0;
274a4886 187
1da177e4
LT
188 errno = ncp_make_open(inode, O_WRONLY);
189 if (errno) {
d3b73ca1 190 ncp_dbg(1, "open failed, error=%d\n", errno);
1da177e4
LT
191 return errno;
192 }
193 bufsize = NCP_SERVER(inode)->buffer_size;
194
c3b2da31
JB
195 errno = file_update_time(file);
196 if (errno)
197 goto outrel;
198
1da177e4
LT
199 bouncebuffer = vmalloc(bufsize);
200 if (!bouncebuffer) {
201 errno = -EIO; /* -ENOMEM */
202 goto outrel;
203 }
274a4886 204 while (iov_iter_count(from)) {
1da177e4 205 int written_this_time;
274a4886
AV
206 size_t to_write = min_t(size_t,
207 bufsize - ((off_t)pos % bufsize),
208 iov_iter_count(from));
1da177e4 209
274a4886 210 if (copy_from_iter(bouncebuffer, to_write, from) != to_write) {
1da177e4
LT
211 errno = -EFAULT;
212 break;
213 }
214 if (ncp_write_kernel(NCP_SERVER(inode),
215 NCP_FINFO(inode)->file_handle,
216 pos, to_write, bouncebuffer, &written_this_time) != 0) {
217 errno = -EIO;
218 break;
219 }
220 pos += written_this_time;
1da177e4
LT
221 already_written += written_this_time;
222
274a4886 223 if (written_this_time != to_write)
1da177e4 224 break;
1da177e4
LT
225 }
226 vfree(bouncebuffer);
227
274a4886 228 iocb->ki_pos = pos;
1da177e4 229
2e54eb96
PV
230 if (pos > i_size_read(inode)) {
231 mutex_lock(&inode->i_mutex);
232 if (pos > i_size_read(inode))
233 i_size_write(inode, pos);
234 mutex_unlock(&inode->i_mutex);
1da177e4 235 }
a67f797d 236 ncp_dbg(1, "exit %pD2\n", file);
1da177e4
LT
237outrel:
238 ncp_inode_close(inode);
239 return already_written ? already_written : errno;
240}
241
242static int ncp_release(struct inode *inode, struct file *file) {
243 if (ncp_make_closed(inode)) {
d3b73ca1 244 ncp_dbg(1, "failed to close\n");
1da177e4
LT
245 }
246 return 0;
247}
248
4b6f5d20 249const struct file_operations ncp_file_operations =
1da177e4 250{
2e54eb96 251 .llseek = generic_file_llseek,
274a4886
AV
252 .read_iter = ncp_file_read_iter,
253 .write_iter = ncp_file_write_iter,
93d84b6d 254 .unlocked_ioctl = ncp_ioctl,
54f67f63
PV
255#ifdef CONFIG_COMPAT
256 .compat_ioctl = ncp_compat_ioctl,
257#endif
1da177e4
LT
258 .mmap = ncp_mmap,
259 .release = ncp_release,
260 .fsync = ncp_fsync,
261};
262
92e1d5be 263const struct inode_operations ncp_file_inode_operations =
1da177e4
LT
264{
265 .setattr = ncp_notify_change,
266};