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