blob: 8f8cc0334dddf3bcfbaaac2d177312c8a7a8f3e3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * file.c
4 *
5 * Copyright (C) 1995, 1996 by Volker Lendecke
6 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7 *
8 */
9
Joe Perchesb41f8b82014-04-08 16:04:14 -070010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <linux/time.h>
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/fcntl.h>
18#include <linux/stat.h>
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/vmalloc.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040021#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Al Viro32c419d2011-01-12 17:37:47 -050023#include "ncp_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Josef Bacik02c24a82011-07-16 20:44:56 -040025static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Jeff Layton3b49c9a2017-07-07 15:20:52 -040027 return file_write_and_wait_range(file, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30/*
31 * Open a file with the specified read/write mode.
32 */
33int ncp_make_open(struct inode *inode, int right)
34{
35 int error;
36 int access;
37
38 error = -EINVAL;
39 if (!inode) {
Joe Perchesb41f8b82014-04-08 16:04:14 -070040 pr_err("%s: got NULL inode\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 goto out;
42 }
43
Joe Perchesd3b73ca2014-04-08 16:04:15 -070044 ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 atomic_read(&NCP_FINFO(inode)->opened),
46 NCP_FINFO(inode)->volNumber,
47 NCP_FINFO(inode)->dirEntNum);
48 error = -EACCES;
Ingo Molnar8e3f9042006-03-23 03:00:43 -080049 mutex_lock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
51 struct ncp_entry_info finfo;
52 int result;
53
54 /* tries max. rights */
55 finfo.access = O_RDWR;
56 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
57 inode, NULL, OC_MODE_OPEN,
58 0, AR_READ | AR_WRITE, &finfo);
59 if (!result)
60 goto update;
61 /* RDWR did not succeeded, try readonly or writeonly as requested */
62 switch (right) {
63 case O_RDONLY:
64 finfo.access = O_RDONLY;
65 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
66 inode, NULL, OC_MODE_OPEN,
67 0, AR_READ, &finfo);
68 break;
69 case O_WRONLY:
70 finfo.access = O_WRONLY;
71 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
72 inode, NULL, OC_MODE_OPEN,
73 0, AR_WRITE, &finfo);
74 break;
75 }
76 if (result) {
Joe Perchese45ca8b2014-04-08 16:04:16 -070077 ncp_vdbg("failed, result=%d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 goto out_unlock;
79 }
80 /*
81 * Update the inode information.
82 */
83 update:
84 ncp_update_inode(inode, &finfo);
85 atomic_set(&NCP_FINFO(inode)->opened, 1);
86 }
87
88 access = NCP_FINFO(inode)->access;
Joe Perchese45ca8b2014-04-08 16:04:16 -070089 ncp_vdbg("file open, access=%x\n", access);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (access == right || access == O_RDWR) {
91 atomic_inc(&NCP_FINFO(inode)->opened);
92 error = 0;
93 }
94
95out_unlock:
Ingo Molnar8e3f9042006-03-23 03:00:43 -080096 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097out:
98 return error;
99}
100
101static ssize_t
Al Viro274a4882015-04-02 23:30:18 -0400102ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Al Viro274a4882015-04-02 23:30:18 -0400104 struct file *file = iocb->ki_filp;
Al Viroa67f7972014-10-31 02:41:28 -0400105 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 size_t already_read = 0;
Al Viro274a4882015-04-02 23:30:18 -0400107 off_t pos = iocb->ki_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 size_t bufsize;
109 int error;
Al Viro274a4882015-04-02 23:30:18 -0400110 void *freepage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size_t freelen;
112
Al Viroa67f7972014-10-31 02:41:28 -0400113 ncp_dbg(1, "enter %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Al Viro274a4882015-04-02 23:30:18 -0400115 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 0;
117 if (pos > inode->i_sb->s_maxbytes)
118 return 0;
Al Viro274a4882015-04-02 23:30:18 -0400119 iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 error = ncp_make_open(inode, O_RDONLY);
122 if (error) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700123 ncp_dbg(1, "open failed, error=%d\n", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return error;
125 }
126
127 bufsize = NCP_SERVER(inode)->buffer_size;
128
129 error = -EIO;
130 freelen = ncp_read_bounce_size(bufsize);
131 freepage = vmalloc(freelen);
132 if (!freepage)
133 goto outrel;
134 error = 0;
135 /* First read in as much as possible for each bufsize. */
Al Viro274a4882015-04-02 23:30:18 -0400136 while (iov_iter_count(to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int read_this_time;
Al Viro274a4882015-04-02 23:30:18 -0400138 size_t to_read = min_t(size_t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 bufsize - (pos % bufsize),
Al Viro274a4882015-04-02 23:30:18 -0400140 iov_iter_count(to));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 error = ncp_read_bounce(NCP_SERVER(inode),
143 NCP_FINFO(inode)->file_handle,
Al Viro274a4882015-04-02 23:30:18 -0400144 pos, to_read, to, &read_this_time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 freepage, freelen);
146 if (error) {
147 error = -EIO; /* NW errno -> Linux errno */
148 break;
149 }
150 pos += read_this_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 already_read += read_this_time;
152
Al Viro274a4882015-04-02 23:30:18 -0400153 if (read_this_time != to_read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 vfree(freepage);
157
Al Viro274a4882015-04-02 23:30:18 -0400158 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 file_accessed(file);
161
Al Viroa67f7972014-10-31 02:41:28 -0400162 ncp_dbg(1, "exit %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163outrel:
164 ncp_inode_close(inode);
165 return already_read ? already_read : error;
166}
167
168static ssize_t
Al Viro274a4882015-04-02 23:30:18 -0400169ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Al Viro274a4882015-04-02 23:30:18 -0400171 struct file *file = iocb->ki_filp;
Al Viroa67f7972014-10-31 02:41:28 -0400172 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 size_t already_written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 size_t bufsize;
175 int errno;
Al Viro274a4882015-04-02 23:30:18 -0400176 void *bouncebuffer;
Al Viro3309dd02015-04-09 12:55:47 -0400177 off_t pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Al Viroa67f7972014-10-31 02:41:28 -0400179 ncp_dbg(1, "enter %pD2\n", file);
Al Viro3309dd02015-04-09 12:55:47 -0400180 errno = generic_write_checks(iocb, from);
181 if (errno <= 0)
Al Viro274a4882015-04-02 23:30:18 -0400182 return errno;
Al Viro274a4882015-04-02 23:30:18 -0400183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 errno = ncp_make_open(inode, O_WRONLY);
185 if (errno) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700186 ncp_dbg(1, "open failed, error=%d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return errno;
188 }
189 bufsize = NCP_SERVER(inode)->buffer_size;
190
Josef Bacikc3b2da32012-03-26 09:59:21 -0400191 errno = file_update_time(file);
192 if (errno)
193 goto outrel;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 bouncebuffer = vmalloc(bufsize);
196 if (!bouncebuffer) {
197 errno = -EIO; /* -ENOMEM */
198 goto outrel;
199 }
Al Viro3309dd02015-04-09 12:55:47 -0400200 pos = iocb->ki_pos;
Al Viro274a4882015-04-02 23:30:18 -0400201 while (iov_iter_count(from)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int written_this_time;
Al Viro274a4882015-04-02 23:30:18 -0400203 size_t to_write = min_t(size_t,
Al Viro3309dd02015-04-09 12:55:47 -0400204 bufsize - (pos % bufsize),
Al Viro274a4882015-04-02 23:30:18 -0400205 iov_iter_count(from));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Al Virocbbd26b2016-11-01 22:09:04 -0400207 if (!copy_from_iter_full(bouncebuffer, to_write, from)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 errno = -EFAULT;
209 break;
210 }
211 if (ncp_write_kernel(NCP_SERVER(inode),
212 NCP_FINFO(inode)->file_handle,
213 pos, to_write, bouncebuffer, &written_this_time) != 0) {
214 errno = -EIO;
215 break;
216 }
217 pos += written_this_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 already_written += written_this_time;
219
Al Viro274a4882015-04-02 23:30:18 -0400220 if (written_this_time != to_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 vfree(bouncebuffer);
224
Al Viro274a4882015-04-02 23:30:18 -0400225 iocb->ki_pos = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200227 if (pos > i_size_read(inode)) {
Al Viro59551022016-01-22 15:40:57 -0500228 inode_lock(inode);
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200229 if (pos > i_size_read(inode))
230 i_size_write(inode, pos);
Al Viro59551022016-01-22 15:40:57 -0500231 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Al Viroa67f7972014-10-31 02:41:28 -0400233 ncp_dbg(1, "exit %pD2\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234outrel:
235 ncp_inode_close(inode);
236 return already_written ? already_written : errno;
237}
238
239static int ncp_release(struct inode *inode, struct file *file) {
240 if (ncp_make_closed(inode)) {
Joe Perchesd3b73ca2014-04-08 16:04:15 -0700241 ncp_dbg(1, "failed to close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 return 0;
244}
245
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800246const struct file_operations ncp_file_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Petr Vandrovec2e54eb92010-09-27 01:47:33 +0200248 .llseek = generic_file_llseek,
Al Viro274a4882015-04-02 23:30:18 -0400249 .read_iter = ncp_file_read_iter,
250 .write_iter = ncp_file_write_iter,
John Kacur93d84b62010-05-05 15:15:37 +0200251 .unlocked_ioctl = ncp_ioctl,
Petr Vandrovec54f67f62006-09-30 23:27:55 -0700252#ifdef CONFIG_COMPAT
253 .compat_ioctl = ncp_compat_ioctl,
254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .mmap = ncp_mmap,
256 .release = ncp_release,
257 .fsync = ncp_fsync,
258};
259
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800260const struct inode_operations ncp_file_inode_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 .setattr = ncp_notify_change,
263};