Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 12 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/vmalloc.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 21 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Al Viro | 32c419d | 2011-01-12 17:37:47 -0500 | [diff] [blame] | 23 | #include "ncp_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 25 | static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | { |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 27 | return file_write_and_wait_range(file, start, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Open a file with the specified read/write mode. |
| 32 | */ |
| 33 | int ncp_make_open(struct inode *inode, int right) |
| 34 | { |
| 35 | int error; |
| 36 | int access; |
| 37 | |
| 38 | error = -EINVAL; |
| 39 | if (!inode) { |
Joe Perches | b41f8b8 | 2014-04-08 16:04:14 -0700 | [diff] [blame] | 40 | pr_err("%s: got NULL inode\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | goto out; |
| 42 | } |
| 43 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 44 | ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | atomic_read(&NCP_FINFO(inode)->opened), |
| 46 | NCP_FINFO(inode)->volNumber, |
| 47 | NCP_FINFO(inode)->dirEntNum); |
| 48 | error = -EACCES; |
Ingo Molnar | 8e3f904 | 2006-03-23 03:00:43 -0800 | [diff] [blame] | 49 | mutex_lock(&NCP_FINFO(inode)->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 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 Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 77 | ncp_vdbg("failed, result=%d\n", result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 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 Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 89 | ncp_vdbg("file open, access=%x\n", access); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (access == right || access == O_RDWR) { |
| 91 | atomic_inc(&NCP_FINFO(inode)->opened); |
| 92 | error = 0; |
| 93 | } |
| 94 | |
| 95 | out_unlock: |
Ingo Molnar | 8e3f904 | 2006-03-23 03:00:43 -0800 | [diff] [blame] | 96 | mutex_unlock(&NCP_FINFO(inode)->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | out: |
| 98 | return error; |
| 99 | } |
| 100 | |
| 101 | static ssize_t |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 102 | ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 104 | struct file *file = iocb->ki_filp; |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 105 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | size_t already_read = 0; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 107 | off_t pos = iocb->ki_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | size_t bufsize; |
| 109 | int error; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 110 | void *freepage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | size_t freelen; |
| 112 | |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 113 | ncp_dbg(1, "enter %pD2\n", file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 115 | if (!iov_iter_count(to)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | if (pos > inode->i_sb->s_maxbytes) |
| 118 | return 0; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 119 | iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | error = ncp_make_open(inode, O_RDONLY); |
| 122 | if (error) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 123 | ncp_dbg(1, "open failed, error=%d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | 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 Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 136 | while (iov_iter_count(to)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int read_this_time; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 138 | size_t to_read = min_t(size_t, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | bufsize - (pos % bufsize), |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 140 | iov_iter_count(to)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | error = ncp_read_bounce(NCP_SERVER(inode), |
| 143 | NCP_FINFO(inode)->file_handle, |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 144 | pos, to_read, to, &read_this_time, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | freepage, freelen); |
| 146 | if (error) { |
| 147 | error = -EIO; /* NW errno -> Linux errno */ |
| 148 | break; |
| 149 | } |
| 150 | pos += read_this_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | already_read += read_this_time; |
| 152 | |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 153 | if (read_this_time != to_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | vfree(freepage); |
| 157 | |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 158 | iocb->ki_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | file_accessed(file); |
| 161 | |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 162 | ncp_dbg(1, "exit %pD2\n", file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | outrel: |
| 164 | ncp_inode_close(inode); |
| 165 | return already_read ? already_read : error; |
| 166 | } |
| 167 | |
| 168 | static ssize_t |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 169 | ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 171 | struct file *file = iocb->ki_filp; |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 172 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | size_t already_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | size_t bufsize; |
| 175 | int errno; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 176 | void *bouncebuffer; |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 177 | off_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 179 | ncp_dbg(1, "enter %pD2\n", file); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 180 | errno = generic_write_checks(iocb, from); |
| 181 | if (errno <= 0) |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 182 | return errno; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | errno = ncp_make_open(inode, O_WRONLY); |
| 185 | if (errno) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 186 | ncp_dbg(1, "open failed, error=%d\n", errno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return errno; |
| 188 | } |
| 189 | bufsize = NCP_SERVER(inode)->buffer_size; |
| 190 | |
Josef Bacik | c3b2da3 | 2012-03-26 09:59:21 -0400 | [diff] [blame] | 191 | errno = file_update_time(file); |
| 192 | if (errno) |
| 193 | goto outrel; |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | bouncebuffer = vmalloc(bufsize); |
| 196 | if (!bouncebuffer) { |
| 197 | errno = -EIO; /* -ENOMEM */ |
| 198 | goto outrel; |
| 199 | } |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 200 | pos = iocb->ki_pos; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 201 | while (iov_iter_count(from)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | int written_this_time; |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 203 | size_t to_write = min_t(size_t, |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 204 | bufsize - (pos % bufsize), |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 205 | iov_iter_count(from)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 207 | if (!copy_from_iter_full(bouncebuffer, to_write, from)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | already_written += written_this_time; |
| 219 | |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 220 | if (written_this_time != to_write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | vfree(bouncebuffer); |
| 224 | |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 225 | iocb->ki_pos = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 227 | if (pos > i_size_read(inode)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 228 | inode_lock(inode); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 229 | if (pos > i_size_read(inode)) |
| 230 | i_size_write(inode, pos); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 231 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 233 | ncp_dbg(1, "exit %pD2\n", file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | outrel: |
| 235 | ncp_inode_close(inode); |
| 236 | return already_written ? already_written : errno; |
| 237 | } |
| 238 | |
| 239 | static int ncp_release(struct inode *inode, struct file *file) { |
| 240 | if (ncp_make_closed(inode)) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 241 | ncp_dbg(1, "failed to close\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | return 0; |
| 244 | } |
| 245 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 246 | const struct file_operations ncp_file_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 248 | .llseek = generic_file_llseek, |
Al Viro | 274a488 | 2015-04-02 23:30:18 -0400 | [diff] [blame] | 249 | .read_iter = ncp_file_read_iter, |
| 250 | .write_iter = ncp_file_write_iter, |
John Kacur | 93d84b6 | 2010-05-05 15:15:37 +0200 | [diff] [blame] | 251 | .unlocked_ioctl = ncp_ioctl, |
Petr Vandrovec | 54f67f6 | 2006-09-30 23:27:55 -0700 | [diff] [blame] | 252 | #ifdef CONFIG_COMPAT |
| 253 | .compat_ioctl = ncp_compat_ioctl, |
| 254 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | .mmap = ncp_mmap, |
| 256 | .release = ncp_release, |
| 257 | .fsync = ncp_fsync, |
| 258 | }; |
| 259 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 260 | const struct inode_operations ncp_file_inode_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
| 262 | .setattr = ncp_notify_change, |
| 263 | }; |