blob: 5f9d5624e377f46517042227d82ce058dd9083ee [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
Joe Perchesb41f8b82014-04-08 16:04:14 -07009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/vmalloc.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Al Viro32c419d2011-01-12 17:37:47 -050022#include "ncp_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Josef Bacik02c24a82011-07-16 20:44:56 -040024static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Josef Bacik02c24a82011-07-16 20:44:56 -040026 return filemap_write_and_wait_range(file->f_mapping, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
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) {
Joe Perchesb41f8b82014-04-08 16:04:14 -070039 pr_err("%s: got NULL inode\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 goto out;
41 }
42
Joe Perchesd3b73ca2014-04-08 16:04:15 -070043 ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 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) {
Joe Perchese45ca8b2014-04-08 16:04:16 -070076 ncp_vdbg("failed, result=%d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 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;
Joe Perchese45ca8b2014-04-08 16:04:16 -070088 ncp_vdbg("file open, access=%x\n", access);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 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
Al Viro274a4882015-04-02 23:30:18 -0400101ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Al Viro274a4882015-04-02 23:30:18 -0400103 struct file *file = iocb->ki_filp;
Al Viroa67f7972014-10-31 02:41:28 -0400104 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 size_t already_read = 0;
Al Viro274a4882015-04-02 23:30:18 -0400106 off_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 size_t bufsize;
108 int error;
Al Viro274a4882015-04-02 23:30:18 -0400109 void *freepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 size_t freelen;
111
Al Viroa67f7972014-10-31 02:41:28 -0400112 ncp_dbg(1, "enter %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Al Viro274a4882015-04-02 23:30:18 -0400114 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116 if (pos > inode->i_sb->s_maxbytes)
117 return 0;
Al Viro274a4882015-04-02 23:30:18 -0400118 iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 error = ncp_make_open(inode, O_RDONLY);
121 if (error) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700122 ncp_dbg(1, "open failed, error=%d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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. */
Al Viro274a4882015-04-02 23:30:18 -0400135 while (iov_iter_count(to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int read_this_time;
Al Viro274a4882015-04-02 23:30:18 -0400137 size_t to_read = min_t(size_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 bufsize - (pos % bufsize),
Al Viro274a4882015-04-02 23:30:18 -0400139 iov_iter_count(to));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 error = ncp_read_bounce(NCP_SERVER(inode),
142 NCP_FINFO(inode)->file_handle,
Al Viro274a4882015-04-02 23:30:18 -0400143 pos, to_read, to, &read_this_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 freepage, freelen);
145 if (error) {
146 error = -EIO; /* NW errno -> Linux errno */
147 break;
148 }
149 pos += read_this_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 already_read += read_this_time;
151
Al Viro274a4882015-04-02 23:30:18 -0400152 if (read_this_time != to_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 vfree(freepage);
156
Al Viro274a4882015-04-02 23:30:18 -0400157 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 file_accessed(file);
160
Al Viroa67f7972014-10-31 02:41:28 -0400161 ncp_dbg(1, "exit %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162outrel:
163 ncp_inode_close(inode);
164 return already_read ? already_read : error;
165}
166
167static ssize_t
Al Viro274a4882015-04-02 23:30:18 -0400168ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Al Viro274a4882015-04-02 23:30:18 -0400170 struct file *file = iocb->ki_filp;
Al Viroa67f7972014-10-31 02:41:28 -0400171 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 size_t already_written = 0;
Al Viro274a4882015-04-02 23:30:18 -0400173 loff_t pos = iocb->ki_pos;
174 size_t count = iov_iter_count(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 size_t bufsize;
176 int errno;
Al Viro274a4882015-04-02 23:30:18 -0400177 void *bouncebuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Al Viroa67f7972014-10-31 02:41:28 -0400179 ncp_dbg(1, "enter %pD2\n", file);
Al Viro274a4882015-04-02 23:30:18 -0400180 errno = generic_write_checks(file, &pos, &count, 0);
181 if (errno)
182 return errno;
183 iov_iter_truncate(from, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 if (!count)
186 return 0;
Al Viro274a4882015-04-02 23:30:18 -0400187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 errno = ncp_make_open(inode, O_WRONLY);
189 if (errno) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700190 ncp_dbg(1, "open failed, error=%d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return errno;
192 }
193 bufsize = NCP_SERVER(inode)->buffer_size;
194
Josef Bacikc3b2da32012-03-26 09:59:21 -0400195 errno = file_update_time(file);
196 if (errno)
197 goto outrel;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bouncebuffer = vmalloc(bufsize);
200 if (!bouncebuffer) {
201 errno = -EIO; /* -ENOMEM */
202 goto outrel;
203 }
Al Viro274a4882015-04-02 23:30:18 -0400204 while (iov_iter_count(from)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int written_this_time;
Al Viro274a4882015-04-02 23:30:18 -0400206 size_t to_write = min_t(size_t,
207 bufsize - ((off_t)pos % bufsize),
208 iov_iter_count(from));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Al Viro274a4882015-04-02 23:30:18 -0400210 if (copy_from_iter(bouncebuffer, to_write, from) != to_write) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 already_written += written_this_time;
222
Al Viro274a4882015-04-02 23:30:18 -0400223 if (written_this_time != to_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
226 vfree(bouncebuffer);
227
Al Viro274a4882015-04-02 23:30:18 -0400228 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200230 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Al Viroa67f7972014-10-31 02:41:28 -0400236 ncp_dbg(1, "exit %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237outrel:
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)) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700244 ncp_dbg(1, "failed to close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 return 0;
247}
248
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800249const struct file_operations ncp_file_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200251 .llseek = generic_file_llseek,
Al Viro274a4882015-04-02 23:30:18 -0400252 .read = new_sync_read,
253 .write = new_sync_write,
254 .read_iter = ncp_file_read_iter,
255 .write_iter = ncp_file_write_iter,
John Kacur93d84b62010-05-05 15:15:37 +0200256 .unlocked_ioctl = ncp_ioctl,
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700257#ifdef CONFIG_COMPAT
258 .compat_ioctl = ncp_compat_ioctl,
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .mmap = ncp_mmap,
261 .release = ncp_release,
262 .fsync = ncp_fsync,
263};
264
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800265const struct inode_operations ncp_file_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 .setattr = ncp_notify_change,
268};