Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/file.c |
| 3 | * |
| 4 | * vfs operations that deal with files |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 5 | * |
Steve French | f19159d | 2010-04-21 04:12:10 +0000 | [diff] [blame] | 6 | * Copyright (C) International Business Machines Corp., 2002,2010 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 8 | * Jeremy Allison (jra@samba.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * This library is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as published |
| 12 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 18 | * the GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public License |
| 21 | * along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | #include <linux/fs.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/stat.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/pagevec.h> |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 30 | #include <linux/writeback.h> |
Andrew Morton | 6f88cc2 | 2006-12-10 02:19:44 -0800 | [diff] [blame] | 31 | #include <linux/task_io_accounting_ops.h> |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 32 | #include <linux/delay.h> |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 33 | #include <linux/mount.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 35 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <asm/div64.h> |
| 37 | #include "cifsfs.h" |
| 38 | #include "cifspdu.h" |
| 39 | #include "cifsglob.h" |
| 40 | #include "cifsproto.h" |
| 41 | #include "cifs_unicode.h" |
| 42 | #include "cifs_debug.h" |
| 43 | #include "cifs_fs_sb.h" |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 44 | #include "fscache.h" |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 45 | #include "smbdirect.h" |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static inline int cifs_convert_flags(unsigned int flags) |
| 48 | { |
| 49 | if ((flags & O_ACCMODE) == O_RDONLY) |
| 50 | return GENERIC_READ; |
| 51 | else if ((flags & O_ACCMODE) == O_WRONLY) |
| 52 | return GENERIC_WRITE; |
| 53 | else if ((flags & O_ACCMODE) == O_RDWR) { |
| 54 | /* GENERIC_ALL is too much permission to request |
| 55 | can cause unnecessary access denied on create */ |
| 56 | /* return GENERIC_ALL; */ |
| 57 | return (GENERIC_READ | GENERIC_WRITE); |
| 58 | } |
| 59 | |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 60 | return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | |
| 61 | FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | |
| 62 | FILE_READ_DATA); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 63 | } |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 64 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 65 | static u32 cifs_posix_convert_flags(unsigned int flags) |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 66 | { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 67 | u32 posix_flags = 0; |
Jeff Layton | e10f7b5 | 2008-05-14 10:21:33 -0700 | [diff] [blame] | 68 | |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 69 | if ((flags & O_ACCMODE) == O_RDONLY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 70 | posix_flags = SMB_O_RDONLY; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 71 | else if ((flags & O_ACCMODE) == O_WRONLY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 72 | posix_flags = SMB_O_WRONLY; |
| 73 | else if ((flags & O_ACCMODE) == O_RDWR) |
| 74 | posix_flags = SMB_O_RDWR; |
| 75 | |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 76 | if (flags & O_CREAT) { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 77 | posix_flags |= SMB_O_CREAT; |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 78 | if (flags & O_EXCL) |
| 79 | posix_flags |= SMB_O_EXCL; |
| 80 | } else if (flags & O_EXCL) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 81 | cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", |
| 82 | current->comm, current->tgid); |
Steve French | 07b92d0 | 2013-02-18 10:34:26 -0600 | [diff] [blame] | 83 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 84 | if (flags & O_TRUNC) |
| 85 | posix_flags |= SMB_O_TRUNC; |
| 86 | /* be safe and imply O_SYNC for O_DSYNC */ |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 87 | if (flags & O_DSYNC) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 88 | posix_flags |= SMB_O_SYNC; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 89 | if (flags & O_DIRECTORY) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 90 | posix_flags |= SMB_O_DIRECTORY; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 91 | if (flags & O_NOFOLLOW) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 92 | posix_flags |= SMB_O_NOFOLLOW; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 93 | if (flags & O_DIRECT) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 94 | posix_flags |= SMB_O_DIRECT; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 95 | |
| 96 | return posix_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline int cifs_get_disposition(unsigned int flags) |
| 100 | { |
| 101 | if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 102 | return FILE_CREATE; |
| 103 | else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
| 104 | return FILE_OVERWRITE_IF; |
| 105 | else if ((flags & O_CREAT) == O_CREAT) |
| 106 | return FILE_OPEN_IF; |
Steve French | 55aa2e0 | 2006-05-30 18:09:31 +0000 | [diff] [blame] | 107 | else if ((flags & O_TRUNC) == O_TRUNC) |
| 108 | return FILE_OVERWRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | else |
| 110 | return FILE_OPEN; |
| 111 | } |
| 112 | |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 113 | int cifs_posix_open(char *full_path, struct inode **pinode, |
| 114 | struct super_block *sb, int mode, unsigned int f_flags, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 115 | __u32 *poplock, __u16 *pnetfid, unsigned int xid) |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 116 | { |
| 117 | int rc; |
| 118 | FILE_UNIX_BASIC_INFO *presp_data; |
| 119 | __u32 posix_flags = 0; |
| 120 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 121 | struct cifs_fattr fattr; |
| 122 | struct tcon_link *tlink; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 123 | struct cifs_tcon *tcon; |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 124 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 125 | cifs_dbg(FYI, "posix open %s\n", full_path); |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 126 | |
| 127 | presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
| 128 | if (presp_data == NULL) |
| 129 | return -ENOMEM; |
| 130 | |
| 131 | tlink = cifs_sb_tlink(cifs_sb); |
| 132 | if (IS_ERR(tlink)) { |
| 133 | rc = PTR_ERR(tlink); |
| 134 | goto posix_open_ret; |
| 135 | } |
| 136 | |
| 137 | tcon = tlink_tcon(tlink); |
| 138 | mode &= ~current_umask(); |
| 139 | |
| 140 | posix_flags = cifs_posix_convert_flags(f_flags); |
| 141 | rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, |
| 142 | poplock, full_path, cifs_sb->local_nls, |
Nakajima Akira | bc8ebdc4 | 2015-02-13 15:35:58 +0900 | [diff] [blame] | 143 | cifs_remap(cifs_sb)); |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 144 | cifs_put_tlink(tlink); |
| 145 | |
| 146 | if (rc) |
| 147 | goto posix_open_ret; |
| 148 | |
| 149 | if (presp_data->Type == cpu_to_le32(-1)) |
| 150 | goto posix_open_ret; /* open ok, caller does qpathinfo */ |
| 151 | |
| 152 | if (!pinode) |
| 153 | goto posix_open_ret; /* caller does not need info */ |
| 154 | |
| 155 | cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); |
| 156 | |
| 157 | /* get new inode and set it up */ |
| 158 | if (*pinode == NULL) { |
| 159 | cifs_fill_uniqueid(sb, &fattr); |
| 160 | *pinode = cifs_iget(sb, &fattr); |
| 161 | if (!*pinode) { |
| 162 | rc = -ENOMEM; |
| 163 | goto posix_open_ret; |
| 164 | } |
| 165 | } else { |
| 166 | cifs_fattr_to_inode(*pinode, &fattr); |
| 167 | } |
| 168 | |
| 169 | posix_open_ret: |
| 170 | kfree(presp_data); |
| 171 | return rc; |
| 172 | } |
| 173 | |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 174 | static int |
| 175 | cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 176 | struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, |
| 177 | struct cifs_fid *fid, unsigned int xid) |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 178 | { |
| 179 | int rc; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 180 | int desired_access; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 181 | int disposition; |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 182 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 183 | FILE_ALL_INFO *buf; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 184 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 185 | struct cifs_open_parms oparms; |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 186 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 187 | if (!server->ops->open) |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 188 | return -ENOSYS; |
| 189 | |
| 190 | desired_access = cifs_convert_flags(f_flags); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 191 | |
| 192 | /********************************************************************* |
| 193 | * open flag mapping table: |
| 194 | * |
| 195 | * POSIX Flag CIFS Disposition |
| 196 | * ---------- ---------------- |
| 197 | * O_CREAT FILE_OPEN_IF |
| 198 | * O_CREAT | O_EXCL FILE_CREATE |
| 199 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF |
| 200 | * O_TRUNC FILE_OVERWRITE |
| 201 | * none of the above FILE_OPEN |
| 202 | * |
| 203 | * Note that there is not a direct match between disposition |
| 204 | * FILE_SUPERSEDE (ie create whether or not file exists although |
| 205 | * O_CREAT | O_TRUNC is similar but truncates the existing |
| 206 | * file rather than creating a new file as FILE_SUPERSEDE does |
| 207 | * (which uses the attributes / metadata passed in on open call) |
| 208 | *? |
| 209 | *? O_SYNC is a reasonable match to CIFS writethrough flag |
| 210 | *? and the read write flags match reasonably. O_LARGEFILE |
| 211 | *? is irrelevant because largefile support is always used |
| 212 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, |
| 213 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation |
| 214 | *********************************************************************/ |
| 215 | |
| 216 | disposition = cifs_get_disposition(f_flags); |
| 217 | |
| 218 | /* BB pass O_SYNC flag through on file attributes .. BB */ |
| 219 | |
| 220 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
| 221 | if (!buf) |
| 222 | return -ENOMEM; |
| 223 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 224 | if (backup_cred(cifs_sb)) |
| 225 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 226 | |
Steve French | 1013e76 | 2017-09-22 01:40:27 -0500 | [diff] [blame] | 227 | /* O_SYNC also has bit for O_DSYNC so following check picks up either */ |
| 228 | if (f_flags & O_SYNC) |
| 229 | create_options |= CREATE_WRITE_THROUGH; |
| 230 | |
| 231 | if (f_flags & O_DIRECT) |
| 232 | create_options |= CREATE_NO_BUFFER; |
| 233 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 234 | oparms.tcon = tcon; |
| 235 | oparms.cifs_sb = cifs_sb; |
| 236 | oparms.desired_access = desired_access; |
| 237 | oparms.create_options = create_options; |
| 238 | oparms.disposition = disposition; |
| 239 | oparms.path = full_path; |
| 240 | oparms.fid = fid; |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 241 | oparms.reconnect = false; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 242 | |
| 243 | rc = server->ops->open(xid, &oparms, oplock, buf); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 244 | |
| 245 | if (rc) |
| 246 | goto out; |
| 247 | |
| 248 | if (tcon->unix_ext) |
| 249 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, |
| 250 | xid); |
| 251 | else |
| 252 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, |
Steve French | 42eacf9 | 2014-02-10 14:08:16 -0600 | [diff] [blame] | 253 | xid, fid); |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 254 | |
Pavel Shilovsky | d72c211 | 2019-09-30 10:06:18 -0700 | [diff] [blame] | 255 | if (rc) { |
| 256 | server->ops->close(xid, tcon, fid); |
| 257 | if (rc == -ESTALE) |
| 258 | rc = -EOPENSTALE; |
| 259 | } |
| 260 | |
Pavel Shilovsky | eeb910a | 2010-11-25 15:12:39 +0300 | [diff] [blame] | 261 | out: |
| 262 | kfree(buf); |
| 263 | return rc; |
| 264 | } |
| 265 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 266 | static bool |
| 267 | cifs_has_mand_locks(struct cifsInodeInfo *cinode) |
| 268 | { |
| 269 | struct cifs_fid_locks *cur; |
| 270 | bool has_locks = false; |
| 271 | |
| 272 | down_read(&cinode->lock_sem); |
| 273 | list_for_each_entry(cur, &cinode->llist, llist) { |
| 274 | if (!list_empty(&cur->locks)) { |
| 275 | has_locks = true; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | up_read(&cinode->lock_sem); |
| 280 | return has_locks; |
| 281 | } |
| 282 | |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 283 | void |
| 284 | cifs_down_write(struct rw_semaphore *sem) |
| 285 | { |
| 286 | while (!down_write_trylock(sem)) |
| 287 | msleep(10); |
| 288 | } |
| 289 | |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 290 | struct cifsFileInfo * |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 291 | cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 292 | struct tcon_link *tlink, __u32 oplock) |
| 293 | { |
Goldwyn Rodrigues | 1f1735c | 2016-04-18 06:41:52 -0500 | [diff] [blame] | 294 | struct dentry *dentry = file_dentry(file); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 295 | struct inode *inode = d_inode(dentry); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 296 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 297 | struct cifsFileInfo *cfile; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 298 | struct cifs_fid_locks *fdlocks; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 299 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 300 | struct TCP_Server_Info *server = tcon->ses->server; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 301 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 302 | cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); |
| 303 | if (cfile == NULL) |
| 304 | return cfile; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 305 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 306 | fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); |
| 307 | if (!fdlocks) { |
| 308 | kfree(cfile); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | INIT_LIST_HEAD(&fdlocks->locks); |
| 313 | fdlocks->cfile = cfile; |
| 314 | cfile->llist = fdlocks; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 315 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 316 | cfile->count = 1; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 317 | cfile->pid = current->tgid; |
| 318 | cfile->uid = current_fsuid(); |
| 319 | cfile->dentry = dget(dentry); |
| 320 | cfile->f_flags = file->f_flags; |
| 321 | cfile->invalidHandle = false; |
| 322 | cfile->tlink = cifs_get_tlink(tlink); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 323 | INIT_WORK(&cfile->oplock_break, cifs_oplock_break); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 324 | mutex_init(&cfile->fh_mutex); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 325 | spin_lock_init(&cfile->file_info_lock); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 326 | |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 327 | cifs_sb_active(inode->i_sb); |
| 328 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 329 | /* |
| 330 | * If the server returned a read oplock and we have mandatory brlocks, |
| 331 | * set oplock level to None. |
| 332 | */ |
Pavel Shilovsky | 53ef101 | 2013-09-05 16:11:28 +0400 | [diff] [blame] | 333 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 334 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 335 | oplock = 0; |
| 336 | } |
| 337 | |
Pavel Shilovsky | df871e5 | 2019-11-27 16:18:39 -0800 | [diff] [blame] | 338 | cifs_down_write(&cinode->lock_sem); |
| 339 | list_add(&fdlocks->llist, &cinode->llist); |
| 340 | up_write(&cinode->lock_sem); |
| 341 | |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 342 | spin_lock(&tcon->open_file_lock); |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 343 | if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 344 | oplock = fid->pending_open->oplock; |
| 345 | list_del(&fid->pending_open->olist); |
| 346 | |
Pavel Shilovsky | 42873b0 | 2013-09-05 21:30:16 +0400 | [diff] [blame] | 347 | fid->purge_cache = false; |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 348 | server->ops->set_fid(cfile, fid, oplock); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 349 | |
| 350 | list_add(&cfile->tlist, &tcon->openFileList); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 351 | |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 352 | /* if readable file instance put first in list*/ |
Ronnie Sahlberg | acc0794 | 2019-06-05 10:38:38 +1000 | [diff] [blame] | 353 | spin_lock(&cinode->open_file_lock); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 354 | if (file->f_mode & FMODE_READ) |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 355 | list_add(&cfile->flist, &cinode->openFileList); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 356 | else |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 357 | list_add_tail(&cfile->flist, &cinode->openFileList); |
Ronnie Sahlberg | acc0794 | 2019-06-05 10:38:38 +1000 | [diff] [blame] | 358 | spin_unlock(&cinode->open_file_lock); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 359 | spin_unlock(&tcon->open_file_lock); |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 360 | |
Pavel Shilovsky | 42873b0 | 2013-09-05 21:30:16 +0400 | [diff] [blame] | 361 | if (fid->purge_cache) |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 362 | cifs_zap_mapping(inode); |
Pavel Shilovsky | 42873b0 | 2013-09-05 21:30:16 +0400 | [diff] [blame] | 363 | |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 364 | file->private_data = cfile; |
| 365 | return cfile; |
Jeff Layton | 15ecb43 | 2010-10-15 15:34:02 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 368 | struct cifsFileInfo * |
| 369 | cifsFileInfo_get(struct cifsFileInfo *cifs_file) |
| 370 | { |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 371 | spin_lock(&cifs_file->file_info_lock); |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 372 | cifsFileInfo_get_locked(cifs_file); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 373 | spin_unlock(&cifs_file->file_info_lock); |
Jeff Layton | 764a1b1 | 2012-07-25 14:59:54 -0400 | [diff] [blame] | 374 | return cifs_file; |
| 375 | } |
| 376 | |
Aurelien Aptel | 8092ecc | 2019-03-29 10:49:12 +0100 | [diff] [blame] | 377 | /** |
| 378 | * cifsFileInfo_put - release a reference of file priv data |
| 379 | * |
| 380 | * Always potentially wait for oplock handler. See _cifsFileInfo_put(). |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 381 | */ |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 382 | void cifsFileInfo_put(struct cifsFileInfo *cifs_file) |
| 383 | { |
Aurelien Aptel | 8092ecc | 2019-03-29 10:49:12 +0100 | [diff] [blame] | 384 | _cifsFileInfo_put(cifs_file, true); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * _cifsFileInfo_put - release a reference of file priv data |
| 389 | * |
| 390 | * This may involve closing the filehandle @cifs_file out on the |
| 391 | * server. Must be called without holding tcon->open_file_lock and |
| 392 | * cifs_file->file_info_lock. |
| 393 | * |
| 394 | * If @wait_for_oplock_handler is true and we are releasing the last |
| 395 | * reference, wait for any running oplock break handler of the file |
| 396 | * and cancel any pending one. If calling this function from the |
| 397 | * oplock break handler, you need to pass false. |
| 398 | * |
| 399 | */ |
| 400 | void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, bool wait_oplock_handler) |
| 401 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 402 | struct inode *inode = d_inode(cifs_file->dentry); |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 403 | struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 404 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | e66673e | 2010-11-02 12:00:42 +0300 | [diff] [blame] | 405 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 406 | struct super_block *sb = inode->i_sb; |
| 407 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 408 | struct cifsLockInfo *li, *tmp; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 409 | struct cifs_fid fid; |
| 410 | struct cifs_pending_open open; |
Sachin Prabhu | ca7df8e | 2015-01-15 12:22:04 +0000 | [diff] [blame] | 411 | bool oplock_break_cancelled; |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 412 | |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 413 | spin_lock(&tcon->open_file_lock); |
Pavel Shilovsky | 01332b0 | 2019-10-23 15:37:19 -0700 | [diff] [blame] | 414 | spin_lock(&cifsi->open_file_lock); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 415 | spin_lock(&cifs_file->file_info_lock); |
Jeff Layton | 5f6dbc9 | 2010-10-15 15:34:06 -0400 | [diff] [blame] | 416 | if (--cifs_file->count > 0) { |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 417 | spin_unlock(&cifs_file->file_info_lock); |
Pavel Shilovsky | 01332b0 | 2019-10-23 15:37:19 -0700 | [diff] [blame] | 418 | spin_unlock(&cifsi->open_file_lock); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 419 | spin_unlock(&tcon->open_file_lock); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 420 | return; |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 421 | } |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 422 | spin_unlock(&cifs_file->file_info_lock); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 423 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 424 | if (server->ops->get_lease_key) |
| 425 | server->ops->get_lease_key(inode, &fid); |
| 426 | |
| 427 | /* store open in pending opens to make sure we don't miss lease break */ |
| 428 | cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); |
| 429 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 430 | /* remove it from the lists */ |
| 431 | list_del(&cifs_file->flist); |
| 432 | list_del(&cifs_file->tlist); |
| 433 | |
| 434 | if (list_empty(&cifsi->openFileList)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 435 | cifs_dbg(FYI, "closing last open instance for inode %p\n", |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 436 | d_inode(cifs_file->dentry)); |
Pavel Shilovsky | 2536413 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 437 | /* |
| 438 | * In strict cache mode we need invalidate mapping on the last |
| 439 | * close because it may cause a error when we open this file |
| 440 | * again and get at least level II oplock. |
| 441 | */ |
Pavel Shilovsky | 4f8ba8a | 2010-11-21 22:36:12 +0300 | [diff] [blame] | 442 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) |
Jeff Layton | aff8d5c | 2014-04-30 09:31:45 -0400 | [diff] [blame] | 443 | set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); |
Pavel Shilovsky | c672362 | 2010-11-03 10:58:57 +0300 | [diff] [blame] | 444 | cifs_set_oplock_level(cifsi, 0); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 445 | } |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 446 | |
Pavel Shilovsky | 01332b0 | 2019-10-23 15:37:19 -0700 | [diff] [blame] | 447 | spin_unlock(&cifsi->open_file_lock); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 448 | spin_unlock(&tcon->open_file_lock); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 449 | |
Aurelien Aptel | 8092ecc | 2019-03-29 10:49:12 +0100 | [diff] [blame] | 450 | oplock_break_cancelled = wait_oplock_handler ? |
| 451 | cancel_work_sync(&cifs_file->oplock_break) : false; |
Jeff Layton | ad63594 | 2011-07-26 12:20:17 -0400 | [diff] [blame] | 452 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 453 | if (!tcon->need_reconnect && !cifs_file->invalidHandle) { |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 454 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 455 | unsigned int xid; |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 456 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 457 | xid = get_xid(); |
Pavel Shilovsky | 0ff78a2 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 458 | if (server->ops->close) |
Pavel Shilovsky | 760ad0c | 2012-09-25 11:00:07 +0400 | [diff] [blame] | 459 | server->ops->close(xid, tcon, &cifs_file->fid); |
| 460 | _free_xid(xid); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Sachin Prabhu | ca7df8e | 2015-01-15 12:22:04 +0000 | [diff] [blame] | 463 | if (oplock_break_cancelled) |
| 464 | cifs_done_oplock_break(cifsi); |
| 465 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 466 | cifs_del_pending_open(&open); |
| 467 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 468 | /* |
| 469 | * Delete any outstanding lock records. We'll lose them when the file |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 470 | * is closed anyway. |
| 471 | */ |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 472 | cifs_down_write(&cifsi->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 473 | list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 474 | list_del(&li->llist); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 475 | cifs_del_lock_waiters(li); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 476 | kfree(li); |
| 477 | } |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 478 | list_del(&cifs_file->llist->llist); |
| 479 | kfree(cifs_file->llist); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 480 | up_write(&cifsi->lock_sem); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 481 | |
| 482 | cifs_put_tlink(cifs_file->tlink); |
| 483 | dput(cifs_file->dentry); |
Mateusz Guzik | 24261fc | 2013-03-08 16:30:03 +0100 | [diff] [blame] | 484 | cifs_sb_deactive(sb); |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 485 | kfree(cifs_file); |
Jeff Layton | b33879a | 2010-10-15 15:34:04 -0400 | [diff] [blame] | 486 | } |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | int cifs_open(struct inode *inode, struct file *file) |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | { |
| 491 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 492 | unsigned int xid; |
Jeff Layton | 590a3fe | 2009-09-12 11:54:28 -0400 | [diff] [blame] | 493 | __u32 oplock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 495 | struct TCP_Server_Info *server; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 496 | struct cifs_tcon *tcon; |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 497 | struct tcon_link *tlink; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 498 | struct cifsFileInfo *cfile = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | char *full_path = NULL; |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 500 | bool posix_open_ok = false; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 501 | struct cifs_fid fid; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 502 | struct cifs_pending_open open; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 504 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 507 | tlink = cifs_sb_tlink(cifs_sb); |
| 508 | if (IS_ERR(tlink)) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 509 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 510 | return PTR_ERR(tlink); |
| 511 | } |
| 512 | tcon = tlink_tcon(tlink); |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 513 | server = tcon->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Goldwyn Rodrigues | 1f1735c | 2016-04-18 06:41:52 -0500 | [diff] [blame] | 515 | full_path = build_path_from_dentry(file_dentry(file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 517 | rc = -ENOMEM; |
Jeff Layton | 232341b | 2010-08-05 13:58:38 -0400 | [diff] [blame] | 518 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 521 | cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 522 | inode, file->f_flags, full_path); |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 523 | |
Namjae Jeon | 787aded | 2014-08-22 14:22:51 +0900 | [diff] [blame] | 524 | if (file->f_flags & O_DIRECT && |
| 525 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { |
| 526 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 527 | file->f_op = &cifs_file_direct_nobrl_ops; |
| 528 | else |
| 529 | file->f_op = &cifs_file_direct_ops; |
| 530 | } |
| 531 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 532 | if (server->oplocks) |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 533 | oplock = REQ_OPLOCK; |
| 534 | else |
| 535 | oplock = 0; |
| 536 | |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 537 | if (!tcon->broken_posix_open && tcon->unix_ext && |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 538 | cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 539 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 540 | /* can not refresh inode info since size could be stale */ |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 541 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, |
Steve French | fa588e0 | 2010-04-22 19:21:55 +0000 | [diff] [blame] | 542 | cifs_sb->mnt_file_mode /* ignored */, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 543 | file->f_flags, &oplock, &fid.netfid, xid); |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 544 | if (rc == 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 545 | cifs_dbg(FYI, "posix open succeeded\n"); |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 546 | posix_open_ok = true; |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 547 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 548 | if (tcon->ses->serverNOS) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 549 | cifs_dbg(VFS, "server %s of type %s returned unexpected error on SMB posix open, disabling posix open support. Check if server update available.\n", |
| 550 | tcon->ses->serverName, |
| 551 | tcon->ses->serverNOS); |
Steve French | 64cc2c6 | 2009-03-04 19:54:08 +0000 | [diff] [blame] | 552 | tcon->broken_posix_open = true; |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 553 | } else if ((rc != -EIO) && (rc != -EREMOTE) && |
| 554 | (rc != -EOPNOTSUPP)) /* path not found or net err */ |
| 555 | goto out; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 556 | /* |
| 557 | * Else fallthrough to retry open the old way on network i/o |
| 558 | * or DFS errors. |
| 559 | */ |
Steve French | 276a74a | 2009-03-03 18:00:34 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 562 | if (server->ops->get_lease_key) |
| 563 | server->ops->get_lease_key(inode, &fid); |
| 564 | |
| 565 | cifs_add_pending_open(&fid, tlink, &open); |
| 566 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 567 | if (!posix_open_ok) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 568 | if (server->ops->get_lease_key) |
| 569 | server->ops->get_lease_key(inode, &fid); |
| 570 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 571 | rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 572 | file->f_flags, &oplock, &fid, xid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 573 | if (rc) { |
| 574 | cifs_del_pending_open(&open); |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 575 | goto out; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 576 | } |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 577 | } |
Jeff Layton | 47c78b7 | 2010-06-16 13:40:17 -0400 | [diff] [blame] | 578 | |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 579 | cfile = cifs_new_fileinfo(&fid, file, tlink, oplock); |
| 580 | if (cfile == NULL) { |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 581 | if (server->ops->close) |
| 582 | server->ops->close(xid, tcon, &fid); |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 583 | cifs_del_pending_open(&open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | rc = -ENOMEM; |
| 585 | goto out; |
| 586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Suresh Jayaraman | 9451a9a | 2010-07-05 18:12:45 +0530 | [diff] [blame] | 588 | cifs_fscache_set_inode_cookie(inode, file); |
| 589 | |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 590 | if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 591 | /* |
| 592 | * Time to set mode which we can not set earlier due to |
| 593 | * problems creating new read-only files. |
| 594 | */ |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 595 | struct cifs_unix_set_info_args args = { |
| 596 | .mode = inode->i_mode, |
Eric W. Biederman | 49418b2 | 2013-02-06 00:57:56 -0800 | [diff] [blame] | 597 | .uid = INVALID_UID, /* no change */ |
| 598 | .gid = INVALID_GID, /* no change */ |
Pavel Shilovsky | 7e12edd | 2010-11-25 17:20:20 +0300 | [diff] [blame] | 599 | .ctime = NO_CHANGE_64, |
| 600 | .atime = NO_CHANGE_64, |
| 601 | .mtime = NO_CHANGE_64, |
| 602 | .device = 0, |
| 603 | }; |
Pavel Shilovsky | fb1214e | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 604 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, |
| 605 | cfile->pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 610 | free_xid(xid); |
Jeff Layton | 7ffec37 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 611 | cifs_put_tlink(tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | return rc; |
| 613 | } |
| 614 | |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 615 | static int cifs_push_posix_locks(struct cifsFileInfo *cfile); |
| 616 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 617 | /* |
| 618 | * Try to reacquire byte range locks that were released when session |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 619 | * to server was lost. |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 620 | */ |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 621 | static int |
| 622 | cifs_relock_file(struct cifsFileInfo *cfile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 624 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 625 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 626 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | int rc = 0; |
| 628 | |
Rabin Vincent | 560d388 | 2017-05-03 17:17:21 +0200 | [diff] [blame] | 629 | down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 630 | if (cinode->can_cache_brlcks) { |
Pavel Shilovsky | 689c3db | 2013-07-11 11:17:45 +0400 | [diff] [blame] | 631 | /* can cache locks - no need to relock */ |
| 632 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 633 | return rc; |
| 634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Pavel Shilovsky | f152fd5 | 2012-11-22 17:10:57 +0400 | [diff] [blame] | 636 | if (cap_unix(tcon->ses) && |
| 637 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 638 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 639 | rc = cifs_push_posix_locks(cfile); |
| 640 | else |
| 641 | rc = tcon->ses->server->ops->push_mand_locks(cfile); |
| 642 | |
Pavel Shilovsky | 689c3db | 2013-07-11 11:17:45 +0400 | [diff] [blame] | 643 | up_read(&cinode->lock_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | return rc; |
| 645 | } |
| 646 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 647 | static int |
| 648 | cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
| 650 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 651 | unsigned int xid; |
Jeff Layton | 590a3fe | 2009-09-12 11:54:28 -0400 | [diff] [blame] | 652 | __u32 oplock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | struct cifs_sb_info *cifs_sb; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 654 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 655 | struct TCP_Server_Info *server; |
| 656 | struct cifsInodeInfo *cinode; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 657 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | char *full_path = NULL; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 659 | int desired_access; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | int disposition = FILE_OPEN; |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 661 | int create_options = CREATE_NOT_DIR; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 662 | struct cifs_open_parms oparms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 664 | xid = get_xid(); |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 665 | mutex_lock(&cfile->fh_mutex); |
| 666 | if (!cfile->invalidHandle) { |
| 667 | mutex_unlock(&cfile->fh_mutex); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 668 | rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 669 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 670 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 673 | inode = d_inode(cfile->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | cifs_sb = CIFS_SB(inode->i_sb); |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 675 | tcon = tlink_tcon(cfile->tlink); |
| 676 | server = tcon->ses->server; |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 677 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 678 | /* |
| 679 | * Can not grab rename sem here because various ops, including those |
| 680 | * that already have the rename sem can end up causing writepage to get |
| 681 | * called and if the server was down that means we end up here, and we |
| 682 | * can never tell if the caller already has the rename_sem. |
| 683 | */ |
| 684 | full_path = build_path_from_dentry(cfile->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | if (full_path == NULL) { |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 686 | rc = -ENOMEM; |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 687 | mutex_unlock(&cfile->fh_mutex); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 688 | free_xid(xid); |
Steve French | 3a9f462 | 2007-04-04 17:10:24 +0000 | [diff] [blame] | 689 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 692 | cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", |
| 693 | inode, cfile->f_flags, full_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Pavel Shilovsky | 10b9b98 | 2012-03-20 12:55:09 +0300 | [diff] [blame] | 695 | if (tcon->ses->server->oplocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | oplock = REQ_OPLOCK; |
| 697 | else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 698 | oplock = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 700 | if (tcon->unix_ext && cap_unix(tcon->ses) && |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 701 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 702 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 703 | /* |
| 704 | * O_CREAT, O_EXCL and O_TRUNC already had their effect on the |
| 705 | * original open. Must mask them off for a reopen. |
| 706 | */ |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 707 | unsigned int oflags = cfile->f_flags & |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 708 | ~(O_CREAT | O_EXCL | O_TRUNC); |
Jeff Layton | 608712f | 2010-10-15 15:33:56 -0400 | [diff] [blame] | 709 | |
Jeff Layton | 2422f67 | 2010-06-16 13:40:16 -0400 | [diff] [blame] | 710 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 711 | cifs_sb->mnt_file_mode /* ignored */, |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 712 | oflags, &oplock, &cfile->fid.netfid, xid); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 713 | if (rc == 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 714 | cifs_dbg(FYI, "posix reopen succeeded\n"); |
Andi Shyti | fe090e4 | 2013-07-29 20:04:35 +0200 | [diff] [blame] | 715 | oparms.reconnect = true; |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 716 | goto reopen_success; |
| 717 | } |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 718 | /* |
| 719 | * fallthrough to retry open the old way on errors, especially |
| 720 | * in the reconnect path it is important to retry hard |
| 721 | */ |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 724 | desired_access = cifs_convert_flags(cfile->f_flags); |
Steve French | 7fc8f4e | 2009-02-23 20:43:11 +0000 | [diff] [blame] | 725 | |
Shirish Pargaonkar | 3d3ea8e | 2011-09-26 09:56:44 -0500 | [diff] [blame] | 726 | if (backup_cred(cifs_sb)) |
| 727 | create_options |= CREATE_OPEN_BACKUP_INTENT; |
| 728 | |
Pavel Shilovsky | 3ddc09c | 2019-11-12 17:16:35 -0800 | [diff] [blame] | 729 | /* O_SYNC also has bit for O_DSYNC so following check picks up either */ |
| 730 | if (cfile->f_flags & O_SYNC) |
| 731 | create_options |= CREATE_WRITE_THROUGH; |
| 732 | |
| 733 | if (cfile->f_flags & O_DIRECT) |
| 734 | create_options |= CREATE_NO_BUFFER; |
| 735 | |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 736 | if (server->ops->get_lease_key) |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 737 | server->ops->get_lease_key(inode, &cfile->fid); |
Pavel Shilovsky | b8c32db | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 738 | |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 739 | oparms.tcon = tcon; |
| 740 | oparms.cifs_sb = cifs_sb; |
| 741 | oparms.desired_access = desired_access; |
| 742 | oparms.create_options = create_options; |
| 743 | oparms.disposition = disposition; |
| 744 | oparms.path = full_path; |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 745 | oparms.fid = &cfile->fid; |
| 746 | oparms.reconnect = true; |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 747 | |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 748 | /* |
| 749 | * Can not refresh inode by passing in file_info buf to be returned by |
Pavel Shilovsky | d81b8a4 | 2014-01-16 15:53:36 +0400 | [diff] [blame] | 750 | * ops->open and then calling get_inode_info with returned buf since |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 751 | * file might have write behind data that needs to be flushed and server |
| 752 | * version of file size can be stale. If we knew for sure that inode was |
| 753 | * not dirty locally we could do this. |
| 754 | */ |
Pavel Shilovsky | 226730b | 2013-07-05 12:00:30 +0400 | [diff] [blame] | 755 | rc = server->ops->open(xid, &oparms, &oplock, NULL); |
Pavel Shilovsky | b33fcf1 | 2013-07-11 10:58:30 +0400 | [diff] [blame] | 756 | if (rc == -ENOENT && oparms.reconnect == false) { |
| 757 | /* durable handle timeout is expired - open the file again */ |
| 758 | rc = server->ops->open(xid, &oparms, &oplock, NULL); |
| 759 | /* indicate that we need to relock the file */ |
| 760 | oparms.reconnect = true; |
| 761 | } |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | if (rc) { |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 764 | mutex_unlock(&cfile->fh_mutex); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 765 | cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); |
| 766 | cifs_dbg(FYI, "oplock: %d\n", oplock); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 767 | goto reopen_error_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 769 | |
| 770 | reopen_success: |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 771 | cfile->invalidHandle = false; |
| 772 | mutex_unlock(&cfile->fh_mutex); |
| 773 | cinode = CIFS_I(inode); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 774 | |
| 775 | if (can_flush) { |
| 776 | rc = filemap_write_and_wait(inode->i_mapping); |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 777 | if (!is_interrupt_error(rc)) |
| 778 | mapping_set_error(inode->i_mapping, rc); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 779 | |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 780 | if (tcon->unix_ext) |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 781 | rc = cifs_get_inode_info_unix(&inode, full_path, |
| 782 | inode->i_sb, xid); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 783 | else |
Pavel Shilovsky | 2ae78ba | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 784 | rc = cifs_get_inode_info(&inode, full_path, NULL, |
| 785 | inode->i_sb, xid, NULL); |
| 786 | } |
| 787 | /* |
| 788 | * Else we are writing out data to server already and could deadlock if |
| 789 | * we tried to flush data, and since we do not know if we have data that |
| 790 | * would invalidate the current end of file on the server we can not go |
| 791 | * to the server to get the new inode info. |
| 792 | */ |
Pavel Shilovsky | e66673e | 2010-11-02 12:00:42 +0300 | [diff] [blame] | 793 | |
Pavel Shilovsky | de74025 | 2016-10-11 15:34:07 -0700 | [diff] [blame] | 794 | /* |
| 795 | * If the server returned a read oplock and we have mandatory brlocks, |
| 796 | * set oplock level to None. |
| 797 | */ |
| 798 | if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { |
| 799 | cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); |
| 800 | oplock = 0; |
| 801 | } |
| 802 | |
Pavel Shilovsky | 9cbc0b7 | 2013-07-09 18:40:58 +0400 | [diff] [blame] | 803 | server->ops->set_fid(cfile, &cfile->fid, oplock); |
| 804 | if (oparms.reconnect) |
| 805 | cifs_relock_file(cfile); |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 806 | |
| 807 | reopen_error_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | kfree(full_path); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 809 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return rc; |
| 811 | } |
| 812 | |
| 813 | int cifs_close(struct inode *inode, struct file *file) |
| 814 | { |
Jeff Layton | 7797069 | 2011-04-05 16:23:47 -0700 | [diff] [blame] | 815 | if (file->private_data != NULL) { |
| 816 | cifsFileInfo_put(file->private_data); |
| 817 | file->private_data = NULL; |
| 818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 820 | /* return code from the ->release op is always ignored */ |
| 821 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 824 | void |
| 825 | cifs_reopen_persistent_handles(struct cifs_tcon *tcon) |
| 826 | { |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 827 | struct cifsFileInfo *open_file; |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 828 | struct list_head *tmp; |
| 829 | struct list_head *tmp1; |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 830 | struct list_head tmp_list; |
| 831 | |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 832 | if (!tcon->use_persistent || !tcon->need_reopen_files) |
| 833 | return; |
| 834 | |
| 835 | tcon->need_reopen_files = false; |
| 836 | |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 837 | cifs_dbg(FYI, "Reopen persistent handles"); |
| 838 | INIT_LIST_HEAD(&tmp_list); |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 839 | |
| 840 | /* list all files open on tree connection, reopen resilient handles */ |
| 841 | spin_lock(&tcon->open_file_lock); |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 842 | list_for_each(tmp, &tcon->openFileList) { |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 843 | open_file = list_entry(tmp, struct cifsFileInfo, tlist); |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 844 | if (!open_file->invalidHandle) |
| 845 | continue; |
| 846 | cifsFileInfo_get(open_file); |
| 847 | list_add_tail(&open_file->rlist, &tmp_list); |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 848 | } |
| 849 | spin_unlock(&tcon->open_file_lock); |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 850 | |
| 851 | list_for_each_safe(tmp, tmp1, &tmp_list) { |
| 852 | open_file = list_entry(tmp, struct cifsFileInfo, rlist); |
Pavel Shilovsky | 96a988f | 2016-11-29 11:31:23 -0800 | [diff] [blame] | 853 | if (cifs_reopen_file(open_file, false /* do not flush */)) |
| 854 | tcon->need_reopen_files = true; |
Pavel Shilovsky | f2cca6a | 2016-10-07 17:26:36 -0700 | [diff] [blame] | 855 | list_del_init(&open_file->rlist); |
| 856 | cifsFileInfo_put(open_file); |
| 857 | } |
Steve French | 52ace1e | 2016-09-22 19:23:56 -0500 | [diff] [blame] | 858 | } |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | int cifs_closedir(struct inode *inode, struct file *file) |
| 861 | { |
| 862 | int rc = 0; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 863 | unsigned int xid; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 864 | struct cifsFileInfo *cfile = file->private_data; |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 865 | struct cifs_tcon *tcon; |
| 866 | struct TCP_Server_Info *server; |
| 867 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 869 | cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 871 | if (cfile == NULL) |
| 872 | return rc; |
| 873 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 874 | xid = get_xid(); |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 875 | tcon = tlink_tcon(cfile->tlink); |
| 876 | server = tcon->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 878 | cifs_dbg(FYI, "Freeing private data in close dir\n"); |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 879 | spin_lock(&cfile->file_info_lock); |
Pavel Shilovsky | 5275580 | 2014-08-18 20:49:57 +0400 | [diff] [blame] | 880 | if (server->ops->dir_needs_close(cfile)) { |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 881 | cfile->invalidHandle = true; |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 882 | spin_unlock(&cfile->file_info_lock); |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 883 | if (server->ops->close_dir) |
| 884 | rc = server->ops->close_dir(xid, tcon, &cfile->fid); |
| 885 | else |
| 886 | rc = -ENOSYS; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 887 | cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 888 | /* not much we can do if it fails anyway, ignore rc */ |
| 889 | rc = 0; |
| 890 | } else |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 891 | spin_unlock(&cfile->file_info_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 893 | buf = cfile->srch_inf.ntwrk_buf_start; |
| 894 | if (buf) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 895 | cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 896 | cfile->srch_inf.ntwrk_buf_start = NULL; |
| 897 | if (cfile->srch_inf.smallBuf) |
| 898 | cifs_small_buf_release(buf); |
| 899 | else |
| 900 | cifs_buf_release(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
Pavel Shilovsky | 92fc65a | 2012-09-18 16:20:32 -0700 | [diff] [blame] | 902 | |
| 903 | cifs_put_tlink(cfile->tlink); |
| 904 | kfree(file->private_data); |
| 905 | file->private_data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* BB can we lock the filestruct while this is going on? */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 907 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return rc; |
| 909 | } |
| 910 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 911 | static struct cifsLockInfo * |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 912 | cifs_lock_init(__u64 offset, __u64 length, __u8 type) |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 913 | { |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 914 | struct cifsLockInfo *lock = |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 915 | kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 916 | if (!lock) |
| 917 | return lock; |
| 918 | lock->offset = offset; |
| 919 | lock->length = length; |
| 920 | lock->type = type; |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 921 | lock->pid = current->tgid; |
| 922 | INIT_LIST_HEAD(&lock->blist); |
| 923 | init_waitqueue_head(&lock->block_q); |
| 924 | return lock; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 925 | } |
| 926 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 927 | void |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 928 | cifs_del_lock_waiters(struct cifsLockInfo *lock) |
| 929 | { |
| 930 | struct cifsLockInfo *li, *tmp; |
| 931 | list_for_each_entry_safe(li, tmp, &lock->blist, blist) { |
| 932 | list_del_init(&li->blist); |
| 933 | wake_up(&li->block_q); |
| 934 | } |
| 935 | } |
| 936 | |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 937 | #define CIFS_LOCK_OP 0 |
| 938 | #define CIFS_READ_OP 1 |
| 939 | #define CIFS_WRITE_OP 2 |
| 940 | |
| 941 | /* @rw_check : 0 - no op, 1 - read, 2 - write */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 942 | static bool |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 943 | cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, |
| 944 | __u64 length, __u8 type, struct cifsFileInfo *cfile, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 945 | struct cifsLockInfo **conf_lock, int rw_check) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 946 | { |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 947 | struct cifsLockInfo *li; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 948 | struct cifsFileInfo *cur_cfile = fdlocks->cfile; |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 949 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 950 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 951 | list_for_each_entry(li, &fdlocks->locks, llist) { |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 952 | if (offset + length <= li->offset || |
| 953 | offset >= li->offset + li->length) |
| 954 | continue; |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 955 | if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && |
| 956 | server->ops->compare_fids(cfile, cur_cfile)) { |
| 957 | /* shared lock prevents write op through the same fid */ |
| 958 | if (!(li->type & server->vals->shared_lock_type) || |
| 959 | rw_check != CIFS_WRITE_OP) |
| 960 | continue; |
| 961 | } |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 962 | if ((type & server->vals->shared_lock_type) && |
| 963 | ((server->ops->compare_fids(cfile, cur_cfile) && |
| 964 | current->tgid == li->pid) || type == li->type)) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 965 | continue; |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 966 | if (conf_lock) |
| 967 | *conf_lock = li; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 968 | return true; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 969 | } |
| 970 | return false; |
| 971 | } |
| 972 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 973 | bool |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 974 | cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 975 | __u8 type, struct cifsLockInfo **conf_lock, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 976 | int rw_check) |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 977 | { |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 978 | bool rc = false; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 979 | struct cifs_fid_locks *cur; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 980 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 981 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 982 | list_for_each_entry(cur, &cinode->llist, llist) { |
| 983 | rc = cifs_find_fid_lock_conflict(cur, offset, length, type, |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 984 | cfile, conf_lock, rw_check); |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 985 | if (rc) |
| 986 | break; |
| 987 | } |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 988 | |
| 989 | return rc; |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 990 | } |
| 991 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 992 | /* |
| 993 | * Check if there is another lock that prevents us to set the lock (mandatory |
| 994 | * style). If such a lock exists, update the flock structure with its |
| 995 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks |
| 996 | * or leave it the same if we can't. Returns 0 if we don't need to request to |
| 997 | * the server or 1 otherwise. |
| 998 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 999 | static int |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1000 | cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, |
| 1001 | __u8 type, struct file_lock *flock) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1002 | { |
| 1003 | int rc = 0; |
| 1004 | struct cifsLockInfo *conf_lock; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1005 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1006 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1007 | bool exist; |
| 1008 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1009 | down_read(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1010 | |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 1011 | exist = cifs_find_lock_conflict(cfile, offset, length, type, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 1012 | &conf_lock, CIFS_LOCK_OP); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1013 | if (exist) { |
| 1014 | flock->fl_start = conf_lock->offset; |
| 1015 | flock->fl_end = conf_lock->offset + conf_lock->length - 1; |
| 1016 | flock->fl_pid = conf_lock->pid; |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1017 | if (conf_lock->type & server->vals->shared_lock_type) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1018 | flock->fl_type = F_RDLCK; |
| 1019 | else |
| 1020 | flock->fl_type = F_WRLCK; |
| 1021 | } else if (!cinode->can_cache_brlcks) |
| 1022 | rc = 1; |
| 1023 | else |
| 1024 | flock->fl_type = F_UNLCK; |
| 1025 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1026 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1027 | return rc; |
| 1028 | } |
| 1029 | |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1030 | static void |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1031 | cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1032 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1033 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1034 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1035 | list_add_tail(&lock->llist, &cfile->llist->locks); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1036 | up_write(&cinode->lock_sem); |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 1039 | /* |
| 1040 | * Set the byte-range lock (mandatory style). Returns: |
| 1041 | * 1) 0, if we set the lock and don't need to request to the server; |
| 1042 | * 2) 1, if no locks prevent us but we need to request to the server; |
| 1043 | * 3) -EACCESS, if there is a lock that prevents us and wait is false. |
| 1044 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1045 | static int |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1046 | cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1047 | bool wait) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1048 | { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1049 | struct cifsLockInfo *conf_lock; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1050 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1051 | bool exist; |
| 1052 | int rc = 0; |
| 1053 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1054 | try_again: |
| 1055 | exist = false; |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1056 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1057 | |
Pavel Shilovsky | 55157df | 2012-02-28 14:04:17 +0300 | [diff] [blame] | 1058 | exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 1059 | lock->type, &conf_lock, CIFS_LOCK_OP); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1060 | if (!exist && cinode->can_cache_brlcks) { |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1061 | list_add_tail(&lock->llist, &cfile->llist->locks); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1062 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1063 | return rc; |
| 1064 | } |
| 1065 | |
| 1066 | if (!exist) |
| 1067 | rc = 1; |
| 1068 | else if (!wait) |
| 1069 | rc = -EACCES; |
| 1070 | else { |
| 1071 | list_add_tail(&lock->blist, &conf_lock->blist); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1072 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1073 | rc = wait_event_interruptible(lock->block_q, |
| 1074 | (lock->blist.prev == &lock->blist) && |
| 1075 | (lock->blist.next == &lock->blist)); |
| 1076 | if (!rc) |
| 1077 | goto try_again; |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1078 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1079 | list_del_init(&lock->blist); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1080 | } |
| 1081 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1082 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1083 | return rc; |
| 1084 | } |
| 1085 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 1086 | /* |
| 1087 | * Check if there is another lock that prevents us to set the lock (posix |
| 1088 | * style). If such a lock exists, update the flock structure with its |
| 1089 | * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks |
| 1090 | * or leave it the same if we can't. Returns 0 if we don't need to request to |
| 1091 | * the server or 1 otherwise. |
| 1092 | */ |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1093 | static int |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1094 | cifs_posix_lock_test(struct file *file, struct file_lock *flock) |
| 1095 | { |
| 1096 | int rc = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1097 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1098 | unsigned char saved_type = flock->fl_type; |
| 1099 | |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 1100 | if ((flock->fl_flags & FL_POSIX) == 0) |
| 1101 | return 1; |
| 1102 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1103 | down_read(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1104 | posix_test_lock(file, flock); |
| 1105 | |
| 1106 | if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { |
| 1107 | flock->fl_type = saved_type; |
| 1108 | rc = 1; |
| 1109 | } |
| 1110 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1111 | up_read(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1112 | return rc; |
| 1113 | } |
| 1114 | |
Pavel Shilovsky | 9a5101c | 2011-11-07 16:11:24 +0300 | [diff] [blame] | 1115 | /* |
| 1116 | * Set the byte-range lock (posix style). Returns: |
| 1117 | * 1) 0, if we set the lock and don't need to request to the server; |
| 1118 | * 2) 1, if we need to request to the server; |
| 1119 | * 3) <0, if the error occurs while setting the lock. |
| 1120 | */ |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1121 | static int |
| 1122 | cifs_posix_lock_set(struct file *file, struct file_lock *flock) |
| 1123 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1124 | struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 1125 | int rc = 1; |
| 1126 | |
| 1127 | if ((flock->fl_flags & FL_POSIX) == 0) |
| 1128 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1129 | |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 1130 | try_again: |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1131 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1132 | if (!cinode->can_cache_brlcks) { |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1133 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 5079276 | 2011-10-29 17:17:57 +0400 | [diff] [blame] | 1134 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1135 | } |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 1136 | |
| 1137 | rc = posix_lock_file(file, flock, NULL); |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1138 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 1139 | if (rc == FILE_LOCK_DEFERRED) { |
| 1140 | rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next); |
| 1141 | if (!rc) |
| 1142 | goto try_again; |
Jeff Layton | 1a9e64a | 2013-06-21 08:58:10 -0400 | [diff] [blame] | 1143 | posix_unblock_lock(flock); |
Pavel Shilovsky | 66189be | 2012-03-28 21:56:19 +0400 | [diff] [blame] | 1144 | } |
Steve French | 9ebb389 | 2012-04-01 13:52:54 -0500 | [diff] [blame] | 1145 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1146 | } |
| 1147 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1148 | int |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1149 | cifs_push_mandatory_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1150 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1151 | unsigned int xid; |
| 1152 | int rc = 0, stored_rc; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1153 | struct cifsLockInfo *li, *tmp; |
| 1154 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1155 | unsigned int num, max_num, max_buf; |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1156 | LOCKING_ANDX_RANGE *buf, *cur; |
Colin Ian King | 4d61eda | 2017-09-19 16:27:39 +0100 | [diff] [blame] | 1157 | static const int types[] = { |
| 1158 | LOCKING_ANDX_LARGE_FILES, |
| 1159 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES |
| 1160 | }; |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1161 | int i; |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1162 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1163 | xid = get_xid(); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1164 | tcon = tlink_tcon(cfile->tlink); |
| 1165 | |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1166 | /* |
| 1167 | * Accessing maxBuf is racy with cifs_reconnect - need to store value |
Ross Lagerwall | 2a71a47 | 2019-01-08 18:30:57 +0000 | [diff] [blame] | 1168 | * and check it before using. |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1169 | */ |
| 1170 | max_buf = tcon->ses->server->maxBuf; |
Ross Lagerwall | 2a71a47 | 2019-01-08 18:30:57 +0000 | [diff] [blame] | 1171 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1172 | free_xid(xid); |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1173 | return -EINVAL; |
| 1174 | } |
| 1175 | |
Ross Lagerwall | 63715c1 | 2019-01-08 18:30:56 +0000 | [diff] [blame] | 1176 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
| 1177 | PAGE_SIZE); |
| 1178 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), |
| 1179 | PAGE_SIZE); |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1180 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
| 1181 | sizeof(LOCKING_ANDX_RANGE); |
Fabian Frederick | 4b99d39 | 2014-12-10 15:41:17 -0800 | [diff] [blame] | 1182 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1183 | if (!buf) { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1184 | free_xid(xid); |
Pavel Shilovsky | e2f2886 | 2012-08-29 21:13:38 +0400 | [diff] [blame] | 1185 | return -ENOMEM; |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | for (i = 0; i < 2; i++) { |
| 1189 | cur = buf; |
| 1190 | num = 0; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1191 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1192 | if (li->type != types[i]) |
| 1193 | continue; |
| 1194 | cur->Pid = cpu_to_le16(li->pid); |
| 1195 | cur->LengthLow = cpu_to_le32((u32)li->length); |
| 1196 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); |
| 1197 | cur->OffsetLow = cpu_to_le32((u32)li->offset); |
| 1198 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); |
| 1199 | if (++num == max_num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1200 | stored_rc = cifs_lockv(xid, tcon, |
| 1201 | cfile->fid.netfid, |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1202 | (__u8)li->type, 0, num, |
| 1203 | buf); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1204 | if (stored_rc) |
| 1205 | rc = stored_rc; |
| 1206 | cur = buf; |
| 1207 | num = 0; |
| 1208 | } else |
| 1209 | cur++; |
| 1210 | } |
| 1211 | |
| 1212 | if (num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1213 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1214 | (__u8)types[i], 0, num, buf); |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1215 | if (stored_rc) |
| 1216 | rc = stored_rc; |
| 1217 | } |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1218 | } |
| 1219 | |
Pavel Shilovsky | 32b9aaf | 2011-10-22 15:33:32 +0400 | [diff] [blame] | 1220 | kfree(buf); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1221 | free_xid(xid); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1222 | return rc; |
| 1223 | } |
| 1224 | |
Jeff Layton | 3d22462 | 2016-05-24 06:27:44 -0400 | [diff] [blame] | 1225 | static __u32 |
| 1226 | hash_lockowner(fl_owner_t owner) |
| 1227 | { |
| 1228 | return cifs_lock_secret ^ hash32_ptr((const void *)owner); |
| 1229 | } |
| 1230 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1231 | struct lock_to_push { |
| 1232 | struct list_head llist; |
| 1233 | __u64 offset; |
| 1234 | __u64 length; |
| 1235 | __u32 pid; |
| 1236 | __u16 netfid; |
| 1237 | __u8 type; |
| 1238 | }; |
| 1239 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1240 | static int |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1241 | cifs_push_posix_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1242 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1243 | struct inode *inode = d_inode(cfile->dentry); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1244 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1245 | struct file_lock *flock; |
| 1246 | struct file_lock_context *flctx = inode->i_flctx; |
Jeff Layton | e084c1b | 2015-02-16 14:32:03 -0500 | [diff] [blame] | 1247 | unsigned int count = 0, i; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1248 | int rc = 0, xid, type; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1249 | struct list_head locks_to_send, *el; |
| 1250 | struct lock_to_push *lck, *tmp; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1251 | __u64 length; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1252 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1253 | xid = get_xid(); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1254 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1255 | if (!flctx) |
| 1256 | goto out; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1257 | |
Jeff Layton | e084c1b | 2015-02-16 14:32:03 -0500 | [diff] [blame] | 1258 | spin_lock(&flctx->flc_lock); |
| 1259 | list_for_each(el, &flctx->flc_posix) { |
| 1260 | count++; |
| 1261 | } |
| 1262 | spin_unlock(&flctx->flc_lock); |
| 1263 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1264 | INIT_LIST_HEAD(&locks_to_send); |
| 1265 | |
| 1266 | /* |
Jeff Layton | e084c1b | 2015-02-16 14:32:03 -0500 | [diff] [blame] | 1267 | * Allocating count locks is enough because no FL_POSIX locks can be |
| 1268 | * added to the list while we are holding cinode->lock_sem that |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1269 | * protects locking operations of this inode. |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1270 | */ |
Jeff Layton | e084c1b | 2015-02-16 14:32:03 -0500 | [diff] [blame] | 1271 | for (i = 0; i < count; i++) { |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1272 | lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); |
| 1273 | if (!lck) { |
| 1274 | rc = -ENOMEM; |
| 1275 | goto err_out; |
| 1276 | } |
| 1277 | list_add_tail(&lck->llist, &locks_to_send); |
| 1278 | } |
| 1279 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1280 | el = locks_to_send.next; |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1281 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 1282 | list_for_each_entry(flock, &flctx->flc_posix, fl_list) { |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1283 | if (el == &locks_to_send) { |
| 1284 | /* |
| 1285 | * The list ended. We don't have enough allocated |
| 1286 | * structures - something is really wrong. |
| 1287 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1288 | cifs_dbg(VFS, "Can't push all brlocks!\n"); |
Pavel Shilovsky | ce85852 | 2012-03-17 09:46:55 +0300 | [diff] [blame] | 1289 | break; |
| 1290 | } |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1291 | length = 1 + flock->fl_end - flock->fl_start; |
| 1292 | if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) |
| 1293 | type = CIFS_RDLCK; |
| 1294 | else |
| 1295 | type = CIFS_WRLCK; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1296 | lck = list_entry(el, struct lock_to_push, llist); |
Jeff Layton | 3d22462 | 2016-05-24 06:27:44 -0400 | [diff] [blame] | 1297 | lck->pid = hash_lockowner(flock->fl_owner); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1298 | lck->netfid = cfile->fid.netfid; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1299 | lck->length = length; |
| 1300 | lck->type = type; |
| 1301 | lck->offset = flock->fl_start; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1302 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 1303 | spin_unlock(&flctx->flc_lock); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1304 | |
| 1305 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1306 | int stored_rc; |
| 1307 | |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1308 | stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 1309 | lck->offset, lck->length, NULL, |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1310 | lck->type, 0); |
| 1311 | if (stored_rc) |
| 1312 | rc = stored_rc; |
| 1313 | list_del(&lck->llist); |
| 1314 | kfree(lck); |
| 1315 | } |
| 1316 | |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1317 | out: |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1318 | free_xid(xid); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1319 | return rc; |
Pavel Shilovsky | d575146 | 2012-03-05 09:39:20 +0300 | [diff] [blame] | 1320 | err_out: |
| 1321 | list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { |
| 1322 | list_del(&lck->llist); |
| 1323 | kfree(lck); |
| 1324 | } |
| 1325 | goto out; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | static int |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1329 | cifs_push_locks(struct cifsFileInfo *cfile) |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1330 | { |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1331 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1332 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1333 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1334 | int rc = 0; |
| 1335 | |
| 1336 | /* we are going to update can_cache_brlcks here - need a write access */ |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1337 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | 9ec3c88 | 2012-11-22 17:00:10 +0400 | [diff] [blame] | 1338 | if (!cinode->can_cache_brlcks) { |
| 1339 | up_write(&cinode->lock_sem); |
| 1340 | return rc; |
| 1341 | } |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1342 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1343 | if (cap_unix(tcon->ses) && |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1344 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 1345 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1346 | rc = cifs_push_posix_locks(cfile); |
| 1347 | else |
| 1348 | rc = tcon->ses->server->ops->push_mand_locks(cfile); |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1349 | |
Pavel Shilovsky | b8db928 | 2012-11-22 17:07:16 +0400 | [diff] [blame] | 1350 | cinode->can_cache_brlcks = false; |
| 1351 | up_write(&cinode->lock_sem); |
| 1352 | return rc; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1353 | } |
| 1354 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1355 | static void |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1356 | cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1357 | bool *wait_flag, struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1359 | if (flock->fl_flags & FL_POSIX) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1360 | cifs_dbg(FYI, "Posix\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1361 | if (flock->fl_flags & FL_FLOCK) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1362 | cifs_dbg(FYI, "Flock\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1363 | if (flock->fl_flags & FL_SLEEP) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1364 | cifs_dbg(FYI, "Blocking lock\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1365 | *wait_flag = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1367 | if (flock->fl_flags & FL_ACCESS) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1368 | cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1369 | if (flock->fl_flags & FL_LEASE) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1370 | cifs_dbg(FYI, "Lease on file - not implemented yet\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1371 | if (flock->fl_flags & |
Jeff Layton | 3d6d854 | 2012-09-19 06:22:46 -0700 | [diff] [blame] | 1372 | (~(FL_POSIX | FL_FLOCK | FL_SLEEP | |
| 1373 | FL_ACCESS | FL_LEASE | FL_CLOSE))) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1374 | cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1376 | *type = server->vals->large_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1377 | if (flock->fl_type == F_WRLCK) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1378 | cifs_dbg(FYI, "F_WRLCK\n"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1379 | *type |= server->vals->exclusive_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1380 | *lock = 1; |
| 1381 | } else if (flock->fl_type == F_UNLCK) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1382 | cifs_dbg(FYI, "F_UNLCK\n"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1383 | *type |= server->vals->unlock_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1384 | *unlock = 1; |
| 1385 | /* Check if unlock includes more than one lock range */ |
| 1386 | } else if (flock->fl_type == F_RDLCK) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1387 | cifs_dbg(FYI, "F_RDLCK\n"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1388 | *type |= server->vals->shared_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1389 | *lock = 1; |
| 1390 | } else if (flock->fl_type == F_EXLCK) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1391 | cifs_dbg(FYI, "F_EXLCK\n"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1392 | *type |= server->vals->exclusive_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1393 | *lock = 1; |
| 1394 | } else if (flock->fl_type == F_SHLCK) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1395 | cifs_dbg(FYI, "F_SHLCK\n"); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1396 | *type |= server->vals->shared_lock_type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1397 | *lock = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1399 | cifs_dbg(FYI, "Unknown type of lock\n"); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1402 | static int |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1403 | cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1404 | bool wait_flag, bool posix_lck, unsigned int xid) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1405 | { |
| 1406 | int rc = 0; |
| 1407 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1408 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 1409 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1410 | struct TCP_Server_Info *server = tcon->ses->server; |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1411 | __u16 netfid = cfile->fid.netfid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1413 | if (posix_lck) { |
| 1414 | int posix_lock_type; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1415 | |
| 1416 | rc = cifs_posix_lock_test(file, flock); |
| 1417 | if (!rc) |
| 1418 | return rc; |
| 1419 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1420 | if (type & server->vals->shared_lock_type) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1421 | posix_lock_type = CIFS_RDLCK; |
| 1422 | else |
| 1423 | posix_lock_type = CIFS_WRLCK; |
Jeff Layton | 3d22462 | 2016-05-24 06:27:44 -0400 | [diff] [blame] | 1424 | rc = CIFSSMBPosixLock(xid, tcon, netfid, |
| 1425 | hash_lockowner(flock->fl_owner), |
Jeff Layton | c5fd363 | 2012-07-23 13:28:37 -0400 | [diff] [blame] | 1426 | flock->fl_start, length, flock, |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1427 | posix_lock_type, wait_flag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return rc; |
| 1429 | } |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1430 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1431 | rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1432 | if (!rc) |
| 1433 | return rc; |
| 1434 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1435 | /* BB we could chain these into one lock request BB */ |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1436 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, |
| 1437 | 1, 0, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1438 | if (rc == 0) { |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1439 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1440 | type, 0, 1, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1441 | flock->fl_type = F_UNLCK; |
| 1442 | if (rc != 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1443 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
| 1444 | rc); |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1445 | return 0; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1448 | if (type & server->vals->shared_lock_type) { |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1449 | flock->fl_type = F_WRLCK; |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1450 | return 0; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1451 | } |
| 1452 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1453 | type &= ~server->vals->exclusive_lock_type; |
| 1454 | |
| 1455 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1456 | type | server->vals->shared_lock_type, |
| 1457 | 1, 0, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1458 | if (rc == 0) { |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1459 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1460 | type | server->vals->shared_lock_type, 0, 1, false); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1461 | flock->fl_type = F_RDLCK; |
| 1462 | if (rc != 0) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1463 | cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", |
| 1464 | rc); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1465 | } else |
| 1466 | flock->fl_type = F_WRLCK; |
| 1467 | |
Pavel Shilovsky | a88b470 | 2011-10-29 17:17:59 +0400 | [diff] [blame] | 1468 | return 0; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1469 | } |
| 1470 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1471 | void |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1472 | cifs_move_llist(struct list_head *source, struct list_head *dest) |
| 1473 | { |
| 1474 | struct list_head *li, *tmp; |
| 1475 | list_for_each_safe(li, tmp, source) |
| 1476 | list_move(li, dest); |
| 1477 | } |
| 1478 | |
Pavel Shilovsky | f7ba7fe | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1479 | void |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1480 | cifs_free_llist(struct list_head *llist) |
| 1481 | { |
| 1482 | struct cifsLockInfo *li, *tmp; |
| 1483 | list_for_each_entry_safe(li, tmp, llist, llist) { |
| 1484 | cifs_del_lock_waiters(li); |
| 1485 | list_del(&li->llist); |
| 1486 | kfree(li); |
| 1487 | } |
| 1488 | } |
| 1489 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1490 | int |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1491 | cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, |
| 1492 | unsigned int xid) |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1493 | { |
| 1494 | int rc = 0, stored_rc; |
Colin Ian King | 4d61eda | 2017-09-19 16:27:39 +0100 | [diff] [blame] | 1495 | static const int types[] = { |
| 1496 | LOCKING_ANDX_LARGE_FILES, |
| 1497 | LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES |
| 1498 | }; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1499 | unsigned int i; |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1500 | unsigned int max_num, num, max_buf; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1501 | LOCKING_ANDX_RANGE *buf, *cur; |
| 1502 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1503 | struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1504 | struct cifsLockInfo *li, *tmp; |
| 1505 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
| 1506 | struct list_head tmp_llist; |
| 1507 | |
| 1508 | INIT_LIST_HEAD(&tmp_llist); |
| 1509 | |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1510 | /* |
| 1511 | * Accessing maxBuf is racy with cifs_reconnect - need to store value |
Ross Lagerwall | 2a71a47 | 2019-01-08 18:30:57 +0000 | [diff] [blame] | 1512 | * and check it before using. |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1513 | */ |
| 1514 | max_buf = tcon->ses->server->maxBuf; |
Ross Lagerwall | 2a71a47 | 2019-01-08 18:30:57 +0000 | [diff] [blame] | 1515 | if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1516 | return -EINVAL; |
| 1517 | |
Ross Lagerwall | 63715c1 | 2019-01-08 18:30:56 +0000 | [diff] [blame] | 1518 | BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > |
| 1519 | PAGE_SIZE); |
| 1520 | max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), |
| 1521 | PAGE_SIZE); |
Pavel Shilovsky | 0013fb4 | 2012-05-31 13:03:26 +0400 | [diff] [blame] | 1522 | max_num = (max_buf - sizeof(struct smb_hdr)) / |
| 1523 | sizeof(LOCKING_ANDX_RANGE); |
Fabian Frederick | 4b99d39 | 2014-12-10 15:41:17 -0800 | [diff] [blame] | 1524 | buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1525 | if (!buf) |
| 1526 | return -ENOMEM; |
| 1527 | |
Dave Wysochanski | 80b42f4 | 2019-10-23 05:02:33 -0400 | [diff] [blame] | 1528 | cifs_down_write(&cinode->lock_sem); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1529 | for (i = 0; i < 2; i++) { |
| 1530 | cur = buf; |
| 1531 | num = 0; |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1532 | list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1533 | if (flock->fl_start > li->offset || |
| 1534 | (flock->fl_start + length) < |
| 1535 | (li->offset + li->length)) |
| 1536 | continue; |
| 1537 | if (current->tgid != li->pid) |
| 1538 | continue; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1539 | if (types[i] != li->type) |
| 1540 | continue; |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1541 | if (cinode->can_cache_brlcks) { |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1542 | /* |
| 1543 | * We can cache brlock requests - simply remove |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1544 | * a lock from the file's list. |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1545 | */ |
| 1546 | list_del(&li->llist); |
| 1547 | cifs_del_lock_waiters(li); |
| 1548 | kfree(li); |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1549 | continue; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1550 | } |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1551 | cur->Pid = cpu_to_le16(li->pid); |
| 1552 | cur->LengthLow = cpu_to_le32((u32)li->length); |
| 1553 | cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); |
| 1554 | cur->OffsetLow = cpu_to_le32((u32)li->offset); |
| 1555 | cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); |
| 1556 | /* |
| 1557 | * We need to save a lock here to let us add it again to |
| 1558 | * the file's list if the unlock range request fails on |
| 1559 | * the server. |
| 1560 | */ |
| 1561 | list_move(&li->llist, &tmp_llist); |
| 1562 | if (++num == max_num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1563 | stored_rc = cifs_lockv(xid, tcon, |
| 1564 | cfile->fid.netfid, |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1565 | li->type, num, 0, buf); |
| 1566 | if (stored_rc) { |
| 1567 | /* |
| 1568 | * We failed on the unlock range |
| 1569 | * request - add all locks from the tmp |
| 1570 | * list to the head of the file's list. |
| 1571 | */ |
| 1572 | cifs_move_llist(&tmp_llist, |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1573 | &cfile->llist->locks); |
Pavel Shilovsky | ea319d5 | 2012-05-31 13:59:36 +0400 | [diff] [blame] | 1574 | rc = stored_rc; |
| 1575 | } else |
| 1576 | /* |
| 1577 | * The unlock range request succeed - |
| 1578 | * free the tmp list. |
| 1579 | */ |
| 1580 | cifs_free_llist(&tmp_llist); |
| 1581 | cur = buf; |
| 1582 | num = 0; |
| 1583 | } else |
| 1584 | cur++; |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1585 | } |
| 1586 | if (num) { |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1587 | stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1588 | types[i], num, 0, buf); |
| 1589 | if (stored_rc) { |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1590 | cifs_move_llist(&tmp_llist, |
| 1591 | &cfile->llist->locks); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1592 | rc = stored_rc; |
| 1593 | } else |
| 1594 | cifs_free_llist(&tmp_llist); |
| 1595 | } |
| 1596 | } |
| 1597 | |
Pavel Shilovsky | 1b4b55a | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 1598 | up_write(&cinode->lock_sem); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1599 | kfree(buf); |
| 1600 | return rc; |
| 1601 | } |
| 1602 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1603 | static int |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1604 | cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1605 | bool wait_flag, bool posix_lck, int lock, int unlock, |
| 1606 | unsigned int xid) |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1607 | { |
| 1608 | int rc = 0; |
| 1609 | __u64 length = 1 + flock->fl_end - flock->fl_start; |
| 1610 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 1611 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1612 | struct TCP_Server_Info *server = tcon->ses->server; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1613 | struct inode *inode = d_inode(cfile->dentry); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1614 | |
| 1615 | if (posix_lck) { |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 1616 | int posix_lock_type; |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1617 | |
| 1618 | rc = cifs_posix_lock_set(file, flock); |
| 1619 | if (!rc || rc < 0) |
| 1620 | return rc; |
| 1621 | |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1622 | if (type & server->vals->shared_lock_type) |
Steve French | 08547b0 | 2006-02-28 22:39:25 +0000 | [diff] [blame] | 1623 | posix_lock_type = CIFS_RDLCK; |
| 1624 | else |
| 1625 | posix_lock_type = CIFS_WRLCK; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1626 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1627 | if (unlock == 1) |
Steve French | beb84dc | 2006-03-03 23:36:34 +0000 | [diff] [blame] | 1628 | posix_lock_type = CIFS_UNLCK; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1629 | |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1630 | rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, |
Jeff Layton | 3d22462 | 2016-05-24 06:27:44 -0400 | [diff] [blame] | 1631 | hash_lockowner(flock->fl_owner), |
| 1632 | flock->fl_start, length, |
Pavel Shilovsky | f45d341 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1633 | NULL, posix_lock_type, wait_flag); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1634 | goto out; |
Jeremy Allison | 7ee1af7 | 2006-08-02 21:56:33 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1637 | if (lock) { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1638 | struct cifsLockInfo *lock; |
| 1639 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1640 | lock = cifs_lock_init(flock->fl_start, length, type); |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1641 | if (!lock) |
| 1642 | return -ENOMEM; |
| 1643 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1644 | rc = cifs_lock_add_if(cfile, lock, wait_flag); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1645 | if (rc < 0) { |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1646 | kfree(lock); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1647 | return rc; |
| 1648 | } |
| 1649 | if (!rc) |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 1650 | goto out; |
| 1651 | |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 1652 | /* |
| 1653 | * Windows 7 server can delay breaking lease from read to None |
| 1654 | * if we set a byte-range lock on a file - break it explicitly |
| 1655 | * before sending the lock to the server to be sure the next |
| 1656 | * read won't conflict with non-overlapted locks due to |
| 1657 | * pagereading. |
| 1658 | */ |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 1659 | if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && |
| 1660 | CIFS_CACHE_READ(CIFS_I(inode))) { |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 1661 | cifs_zap_mapping(inode); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1662 | cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", |
| 1663 | inode); |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 1664 | CIFS_I(inode)->oplock = 0; |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 1665 | } |
| 1666 | |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1667 | rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, |
| 1668 | type, 1, 0, wait_flag); |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1669 | if (rc) { |
| 1670 | kfree(lock); |
Pavel Shilovsky | 21cb2d9 | 2012-11-22 18:56:39 +0400 | [diff] [blame] | 1671 | return rc; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1672 | } |
Pavel Shilovsky | 161ebf9 | 2011-10-29 17:17:58 +0400 | [diff] [blame] | 1673 | |
Pavel Shilovsky | fbd35ac | 2012-02-24 15:41:06 +0300 | [diff] [blame] | 1674 | cifs_lock_add(cfile, lock); |
Pavel Shilovsky | 9ee305b | 2011-10-22 15:33:31 +0400 | [diff] [blame] | 1675 | } else if (unlock) |
Pavel Shilovsky | d39a4f7 | 2012-09-19 06:22:43 -0700 | [diff] [blame] | 1676 | rc = server->ops->mand_unlock_range(cfile, flock, xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1677 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1678 | out: |
Aurelien Aptel | 2938651 | 2019-03-14 18:44:16 +0100 | [diff] [blame] | 1679 | if (flock->fl_flags & FL_POSIX) { |
| 1680 | /* |
| 1681 | * If this is a request to remove all locks because we |
| 1682 | * are closing the file, it doesn't matter if the |
| 1683 | * unlocking failed as both cifs.ko and the SMB server |
| 1684 | * remove the lock on file close |
| 1685 | */ |
| 1686 | if (rc) { |
| 1687 | cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); |
| 1688 | if (!(flock->fl_flags & FL_CLOSE)) |
| 1689 | return rc; |
| 1690 | } |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 1691 | rc = locks_lock_file_wait(file, flock); |
Aurelien Aptel | 2938651 | 2019-03-14 18:44:16 +0100 | [diff] [blame] | 1692 | } |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1693 | return rc; |
| 1694 | } |
| 1695 | |
| 1696 | int cifs_lock(struct file *file, int cmd, struct file_lock *flock) |
| 1697 | { |
| 1698 | int rc, xid; |
| 1699 | int lock = 0, unlock = 0; |
| 1700 | bool wait_flag = false; |
| 1701 | bool posix_lck = false; |
| 1702 | struct cifs_sb_info *cifs_sb; |
| 1703 | struct cifs_tcon *tcon; |
| 1704 | struct cifsInodeInfo *cinode; |
| 1705 | struct cifsFileInfo *cfile; |
| 1706 | __u16 netfid; |
Pavel Shilovsky | 04a6aa8 | 2012-02-28 14:16:55 +0300 | [diff] [blame] | 1707 | __u32 type; |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1708 | |
| 1709 | rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1710 | xid = get_xid(); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1711 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1712 | cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n", |
| 1713 | cmd, flock->fl_flags, flock->fl_type, |
| 1714 | flock->fl_start, flock->fl_end); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1715 | |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1716 | cfile = (struct cifsFileInfo *)file->private_data; |
| 1717 | tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 106dc53 | 2012-02-28 14:23:34 +0300 | [diff] [blame] | 1718 | |
| 1719 | cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, |
| 1720 | tcon->ses->server); |
| 1721 | |
Al Viro | 7119e22 | 2014-10-22 00:25:12 -0400 | [diff] [blame] | 1722 | cifs_sb = CIFS_FILE_SB(file); |
Pavel Shilovsky | 4b4de76 | 2012-09-18 16:20:26 -0700 | [diff] [blame] | 1723 | netfid = cfile->fid.netfid; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1724 | cinode = CIFS_I(file_inode(file)); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1725 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 1726 | if (cap_unix(tcon->ses) && |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1727 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 1728 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
| 1729 | posix_lck = true; |
| 1730 | /* |
| 1731 | * BB add code here to normalize offset and length to account for |
| 1732 | * negative length which we can not accept over the wire. |
| 1733 | */ |
| 1734 | if (IS_GETLK(cmd)) { |
Pavel Shilovsky | 4f6bcec | 2011-10-22 15:33:30 +0400 | [diff] [blame] | 1735 | rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1736 | free_xid(xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1737 | return rc; |
| 1738 | } |
| 1739 | |
| 1740 | if (!lock && !unlock) { |
| 1741 | /* |
| 1742 | * if no lock or unlock then nothing to do since we do not |
| 1743 | * know what it is |
| 1744 | */ |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1745 | free_xid(xid); |
Pavel Shilovsky | 03776f4 | 2010-08-17 11:26:00 +0400 | [diff] [blame] | 1746 | return -EOPNOTSUPP; |
| 1747 | } |
| 1748 | |
| 1749 | rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, |
| 1750 | xid); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1751 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | return rc; |
| 1753 | } |
| 1754 | |
Jeff Layton | 597b027 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 1755 | /* |
| 1756 | * update the file size (if needed) after a write. Should be called with |
| 1757 | * the inode->i_lock held |
| 1758 | */ |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 1759 | void |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1760 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, |
| 1761 | unsigned int bytes_written) |
| 1762 | { |
| 1763 | loff_t end_of_write = offset + bytes_written; |
| 1764 | |
| 1765 | if (end_of_write > cifsi->server_eof) |
| 1766 | cifsi->server_eof = end_of_write; |
| 1767 | } |
| 1768 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1769 | static ssize_t |
| 1770 | cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, |
| 1771 | size_t write_size, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | { |
| 1773 | int rc = 0; |
| 1774 | unsigned int bytes_written = 0; |
| 1775 | unsigned int total_written; |
| 1776 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1777 | struct cifs_tcon *tcon; |
| 1778 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1779 | unsigned int xid; |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1780 | struct dentry *dentry = open_file->dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1781 | struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry)); |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1782 | struct cifs_io_parms io_parms; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1784 | cifs_sb = CIFS_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 1786 | cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n", |
| 1787 | write_size, *offset, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1789 | tcon = tlink_tcon(open_file->tlink); |
| 1790 | server = tcon->ses->server; |
| 1791 | |
| 1792 | if (!server->ops->sync_write) |
| 1793 | return -ENOSYS; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1794 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1795 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | for (total_written = 0; write_size > total_written; |
| 1798 | total_written += bytes_written) { |
| 1799 | rc = -EAGAIN; |
| 1800 | while (rc == -EAGAIN) { |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 1801 | struct kvec iov[2]; |
| 1802 | unsigned int len; |
| 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | if (open_file->invalidHandle) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | /* we could deadlock if we called |
| 1806 | filemap_fdatawait from here so tell |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1807 | reopen_file not to flush data to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | server now */ |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 1809 | rc = cifs_reopen_file(open_file, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | if (rc != 0) |
| 1811 | break; |
| 1812 | } |
Steve French | 3e84469 | 2005-10-03 13:37:24 -0700 | [diff] [blame] | 1813 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1814 | len = min(server->ops->wp_retry_size(d_inode(dentry)), |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 1815 | (unsigned int)write_size - total_written); |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 1816 | /* iov[0] is reserved for smb header */ |
| 1817 | iov[1].iov_base = (char *)write_data + total_written; |
| 1818 | iov[1].iov_len = len; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1819 | io_parms.pid = pid; |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1820 | io_parms.tcon = tcon; |
| 1821 | io_parms.offset = *offset; |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 1822 | io_parms.length = len; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 1823 | rc = server->ops->sync_write(xid, &open_file->fid, |
| 1824 | &io_parms, &bytes_written, iov, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | if (rc || (bytes_written == 0)) { |
| 1827 | if (total_written) |
| 1828 | break; |
| 1829 | else { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1830 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | return rc; |
| 1832 | } |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1833 | } else { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1834 | spin_lock(&d_inode(dentry)->i_lock); |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1835 | cifs_update_eof(cifsi, *offset, bytes_written); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1836 | spin_unlock(&d_inode(dentry)->i_lock); |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1837 | *offset += bytes_written; |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Pavel Shilovsky | ba9ad725 | 2012-09-18 16:20:30 -0700 | [diff] [blame] | 1841 | cifs_stats_bytes_written(tcon, total_written); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Jeff Layton | 7da4b49 | 2010-10-15 15:34:00 -0400 | [diff] [blame] | 1843 | if (total_written > 0) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1844 | spin_lock(&d_inode(dentry)->i_lock); |
| 1845 | if (*offset > d_inode(dentry)->i_size) |
| 1846 | i_size_write(d_inode(dentry), *offset); |
| 1847 | spin_unlock(&d_inode(dentry)->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | } |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1849 | mark_inode_dirty_sync(d_inode(dentry)); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 1850 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | return total_written; |
| 1852 | } |
| 1853 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1854 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, |
| 1855 | bool fsuid_only) |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1856 | { |
| 1857 | struct cifsFileInfo *open_file = NULL; |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1858 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
| 1859 | |
| 1860 | /* only filter by fsuid on multiuser mounts */ |
| 1861 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 1862 | fsuid_only = false; |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1863 | |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1864 | spin_lock(&cifs_inode->open_file_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1865 | /* we could simply get the first_list_entry since write-only entries |
| 1866 | are always at the end of the list but since the first entry might |
| 1867 | have a close pending, we go through the whole list */ |
| 1868 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Eric W. Biederman | fef59fd | 2013-02-06 02:23:02 -0800 | [diff] [blame] | 1869 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1870 | continue; |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 1871 | if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1872 | if (!open_file->invalidHandle) { |
| 1873 | /* found a good file */ |
| 1874 | /* lock it so it will not be closed on us */ |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 1875 | cifsFileInfo_get(open_file); |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1876 | spin_unlock(&cifs_inode->open_file_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1877 | return open_file; |
| 1878 | } /* else might as well continue, and look for |
| 1879 | another, or simply have the caller reopen it |
| 1880 | again rather than trying to fix this handle */ |
| 1881 | } else /* write only file */ |
| 1882 | break; /* write only files are last so must be done */ |
| 1883 | } |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1884 | spin_unlock(&cifs_inode->open_file_lock); |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1885 | return NULL; |
| 1886 | } |
Steve French | 630f3f0c | 2007-10-25 21:17:17 +0000 | [diff] [blame] | 1887 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1888 | struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode, |
| 1889 | bool fsuid_only) |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1890 | { |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1891 | struct cifsFileInfo *open_file, *inv_file = NULL; |
Jeff Layton | d389229 | 2010-11-02 16:22:50 -0400 | [diff] [blame] | 1892 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | 2846d38 | 2008-09-22 21:33:33 -0400 | [diff] [blame] | 1893 | bool any_available = false; |
Steve French | dd99cd8 | 2005-10-05 19:32:49 -0700 | [diff] [blame] | 1894 | int rc; |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1895 | unsigned int refind = 0; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1896 | |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1897 | /* Having a null inode here (because mapping->host was set to zero by |
| 1898 | the VFS or MM) should not happen but we had reports of on oops (due to |
| 1899 | it being zero) during stress testcases so we need to check for it */ |
| 1900 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1901 | if (cifs_inode == NULL) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1902 | cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); |
Steve French | 6080823 | 2006-04-22 15:53:05 +0000 | [diff] [blame] | 1903 | dump_stack(); |
| 1904 | return NULL; |
| 1905 | } |
| 1906 | |
Jeff Layton | d389229 | 2010-11-02 16:22:50 -0400 | [diff] [blame] | 1907 | cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
| 1908 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1909 | /* only filter by fsuid on multiuser mounts */ |
| 1910 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 1911 | fsuid_only = false; |
| 1912 | |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1913 | spin_lock(&cifs_inode->open_file_lock); |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1914 | refind_writable: |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1915 | if (refind > MAX_REOPEN_ATT) { |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1916 | spin_unlock(&cifs_inode->open_file_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1917 | return NULL; |
| 1918 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1919 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1920 | if (!any_available && open_file->pid != current->tgid) |
| 1921 | continue; |
Eric W. Biederman | fef59fd | 2013-02-06 02:23:02 -0800 | [diff] [blame] | 1922 | if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 1923 | continue; |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 1924 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1925 | if (!open_file->invalidHandle) { |
| 1926 | /* found a good writable file */ |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 1927 | cifsFileInfo_get(open_file); |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1928 | spin_unlock(&cifs_inode->open_file_lock); |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1929 | return open_file; |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1930 | } else { |
| 1931 | if (!inv_file) |
| 1932 | inv_file = open_file; |
Steve French | 9b22b0b | 2007-10-02 01:11:08 +0000 | [diff] [blame] | 1933 | } |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1934 | } |
| 1935 | } |
Jeff Layton | 2846d38 | 2008-09-22 21:33:33 -0400 | [diff] [blame] | 1936 | /* couldn't find useable FH with same pid, try any available */ |
| 1937 | if (!any_available) { |
| 1938 | any_available = true; |
| 1939 | goto refind_writable; |
| 1940 | } |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1941 | |
| 1942 | if (inv_file) { |
| 1943 | any_available = false; |
Steve French | 3afca26 | 2016-09-22 18:58:16 -0500 | [diff] [blame] | 1944 | cifsFileInfo_get(inv_file); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1945 | } |
| 1946 | |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1947 | spin_unlock(&cifs_inode->open_file_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1948 | |
| 1949 | if (inv_file) { |
| 1950 | rc = cifs_reopen_file(inv_file, false); |
| 1951 | if (!rc) |
| 1952 | return inv_file; |
| 1953 | else { |
Ronnie Sahlberg | acc0794 | 2019-06-05 10:38:38 +1000 | [diff] [blame] | 1954 | spin_lock(&cifs_inode->open_file_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1955 | list_move_tail(&inv_file->flist, |
| 1956 | &cifs_inode->openFileList); |
Ronnie Sahlberg | acc0794 | 2019-06-05 10:38:38 +1000 | [diff] [blame] | 1957 | spin_unlock(&cifs_inode->open_file_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1958 | cifsFileInfo_put(inv_file); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1959 | ++refind; |
David Disseldorp | e1e9bda | 2015-03-13 14:20:29 +0100 | [diff] [blame] | 1960 | inv_file = NULL; |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 1961 | spin_lock(&cifs_inode->open_file_lock); |
Shirish Pargaonkar | 2c0c2a0 | 2012-05-21 09:20:12 -0500 | [diff] [blame] | 1962 | goto refind_writable; |
| 1963 | } |
| 1964 | } |
| 1965 | |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1966 | return NULL; |
| 1967 | } |
| 1968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) |
| 1970 | { |
| 1971 | struct address_space *mapping = page->mapping; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1972 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | char *write_data; |
| 1974 | int rc = -EFAULT; |
| 1975 | int bytes_written = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | struct inode *inode; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 1977 | struct cifsFileInfo *open_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | |
| 1979 | if (!mapping || !mapping->host) |
| 1980 | return -EFAULT; |
| 1981 | |
| 1982 | inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | |
| 1984 | offset += (loff_t)from; |
| 1985 | write_data = kmap(page); |
| 1986 | write_data += from; |
| 1987 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1988 | if ((to > PAGE_SIZE) || (from > to)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | kunmap(page); |
| 1990 | return -EIO; |
| 1991 | } |
| 1992 | |
| 1993 | /* racing with truncate? */ |
| 1994 | if (offset > mapping->host->i_size) { |
| 1995 | kunmap(page); |
| 1996 | return 0; /* don't care */ |
| 1997 | } |
| 1998 | |
| 1999 | /* check to make sure that we are not extending the file */ |
| 2000 | if (mapping->host->i_size - offset < (loff_t)to) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2001 | to = (unsigned)(mapping->host->i_size - offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Jeff Layton | 6508d90 | 2010-09-29 19:51:11 -0400 | [diff] [blame] | 2003 | open_file = find_writable_file(CIFS_I(mapping->host), false); |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 2004 | if (open_file) { |
Pavel Shilovsky | fa2989f | 2011-05-26 10:01:59 +0400 | [diff] [blame] | 2005 | bytes_written = cifs_write(open_file, open_file->pid, |
| 2006 | write_data, to - from, &offset); |
Dave Kleikamp | 6ab409b | 2009-08-31 11:07:12 -0400 | [diff] [blame] | 2007 | cifsFileInfo_put(open_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | /* Does mm or vfs already set times? */ |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 2009 | inode->i_atime = inode->i_mtime = current_time(inode); |
Steve French | bb5a9a0 | 2007-12-31 04:21:29 +0000 | [diff] [blame] | 2010 | if ((bytes_written > 0) && (offset)) |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 2011 | rc = 0; |
Steve French | bb5a9a0 | 2007-12-31 04:21:29 +0000 | [diff] [blame] | 2012 | else if (bytes_written < 0) |
| 2013 | rc = bytes_written; |
Steve French | 6148a74 | 2005-10-05 12:23:19 -0700 | [diff] [blame] | 2014 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2015 | cifs_dbg(FYI, "No writeable filehandles for inode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | rc = -EIO; |
| 2017 | } |
| 2018 | |
| 2019 | kunmap(page); |
| 2020 | return rc; |
| 2021 | } |
| 2022 | |
Pavel Shilovsky | 90ac138 | 2014-06-19 16:11:00 +0400 | [diff] [blame] | 2023 | static struct cifs_writedata * |
| 2024 | wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping, |
| 2025 | pgoff_t end, pgoff_t *index, |
| 2026 | unsigned int *found_pages) |
| 2027 | { |
Pavel Shilovsky | 90ac138 | 2014-06-19 16:11:00 +0400 | [diff] [blame] | 2028 | struct cifs_writedata *wdata; |
| 2029 | |
| 2030 | wdata = cifs_writedata_alloc((unsigned int)tofind, |
| 2031 | cifs_writev_complete); |
| 2032 | if (!wdata) |
| 2033 | return NULL; |
| 2034 | |
Jan Kara | 9c19a9c | 2017-11-15 17:35:26 -0800 | [diff] [blame] | 2035 | *found_pages = find_get_pages_range_tag(mapping, index, end, |
| 2036 | PAGECACHE_TAG_DIRTY, tofind, wdata->pages); |
Pavel Shilovsky | 90ac138 | 2014-06-19 16:11:00 +0400 | [diff] [blame] | 2037 | return wdata; |
| 2038 | } |
| 2039 | |
Pavel Shilovsky | 7e48ff8 | 2014-06-19 15:01:03 +0400 | [diff] [blame] | 2040 | static unsigned int |
| 2041 | wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages, |
| 2042 | struct address_space *mapping, |
| 2043 | struct writeback_control *wbc, |
| 2044 | pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done) |
| 2045 | { |
| 2046 | unsigned int nr_pages = 0, i; |
| 2047 | struct page *page; |
| 2048 | |
| 2049 | for (i = 0; i < found_pages; i++) { |
| 2050 | page = wdata->pages[i]; |
| 2051 | /* |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 2052 | * At this point we hold neither the i_pages lock nor the |
| 2053 | * page lock: the page may be truncated or invalidated |
| 2054 | * (changing page->mapping to NULL), or even swizzled |
| 2055 | * back from swapper_space to tmpfs file mapping |
Pavel Shilovsky | 7e48ff8 | 2014-06-19 15:01:03 +0400 | [diff] [blame] | 2056 | */ |
| 2057 | |
| 2058 | if (nr_pages == 0) |
| 2059 | lock_page(page); |
| 2060 | else if (!trylock_page(page)) |
| 2061 | break; |
| 2062 | |
| 2063 | if (unlikely(page->mapping != mapping)) { |
| 2064 | unlock_page(page); |
| 2065 | break; |
| 2066 | } |
| 2067 | |
| 2068 | if (!wbc->range_cyclic && page->index > end) { |
| 2069 | *done = true; |
| 2070 | unlock_page(page); |
| 2071 | break; |
| 2072 | } |
| 2073 | |
| 2074 | if (*next && (page->index != *next)) { |
| 2075 | /* Not next consecutive page */ |
| 2076 | unlock_page(page); |
| 2077 | break; |
| 2078 | } |
| 2079 | |
| 2080 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 2081 | wait_on_page_writeback(page); |
| 2082 | |
| 2083 | if (PageWriteback(page) || |
| 2084 | !clear_page_dirty_for_io(page)) { |
| 2085 | unlock_page(page); |
| 2086 | break; |
| 2087 | } |
| 2088 | |
| 2089 | /* |
| 2090 | * This actually clears the dirty bit in the radix tree. |
| 2091 | * See cifs_writepage() for more commentary. |
| 2092 | */ |
| 2093 | set_page_writeback(page); |
| 2094 | if (page_offset(page) >= i_size_read(mapping->host)) { |
| 2095 | *done = true; |
| 2096 | unlock_page(page); |
| 2097 | end_page_writeback(page); |
| 2098 | break; |
| 2099 | } |
| 2100 | |
| 2101 | wdata->pages[i] = page; |
| 2102 | *next = page->index + 1; |
| 2103 | ++nr_pages; |
| 2104 | } |
| 2105 | |
| 2106 | /* reset index to refind any pages skipped */ |
| 2107 | if (nr_pages == 0) |
| 2108 | *index = wdata->pages[0]->index + 1; |
| 2109 | |
| 2110 | /* put any pages we aren't going to use */ |
| 2111 | for (i = nr_pages; i < found_pages; i++) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2112 | put_page(wdata->pages[i]); |
Pavel Shilovsky | 7e48ff8 | 2014-06-19 15:01:03 +0400 | [diff] [blame] | 2113 | wdata->pages[i] = NULL; |
| 2114 | } |
| 2115 | |
| 2116 | return nr_pages; |
| 2117 | } |
| 2118 | |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2119 | static int |
| 2120 | wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages, |
| 2121 | struct address_space *mapping, struct writeback_control *wbc) |
| 2122 | { |
| 2123 | int rc = 0; |
| 2124 | struct TCP_Server_Info *server; |
| 2125 | unsigned int i; |
| 2126 | |
| 2127 | wdata->sync_mode = wbc->sync_mode; |
| 2128 | wdata->nr_pages = nr_pages; |
| 2129 | wdata->offset = page_offset(wdata->pages[0]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2130 | wdata->pagesz = PAGE_SIZE; |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2131 | wdata->tailsz = min(i_size_read(mapping->host) - |
| 2132 | page_offset(wdata->pages[nr_pages - 1]), |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2133 | (loff_t)PAGE_SIZE); |
| 2134 | wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz; |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2135 | |
Pavel Shilovsky | 66231a4 | 2014-06-19 16:15:16 +0400 | [diff] [blame] | 2136 | if (wdata->cfile != NULL) |
| 2137 | cifsFileInfo_put(wdata->cfile); |
| 2138 | wdata->cfile = find_writable_file(CIFS_I(mapping->host), false); |
| 2139 | if (!wdata->cfile) { |
| 2140 | cifs_dbg(VFS, "No writable handles for inode\n"); |
| 2141 | rc = -EBADF; |
| 2142 | } else { |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2143 | wdata->pid = wdata->cfile->pid; |
| 2144 | server = tlink_tcon(wdata->cfile->tlink)->ses->server; |
| 2145 | rc = server->ops->async_writev(wdata, cifs_writedata_release); |
Pavel Shilovsky | 66231a4 | 2014-06-19 16:15:16 +0400 | [diff] [blame] | 2146 | } |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2147 | |
| 2148 | for (i = 0; i < nr_pages; ++i) |
| 2149 | unlock_page(wdata->pages[i]); |
| 2150 | |
| 2151 | return rc; |
| 2152 | } |
| 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | static int cifs_writepages(struct address_space *mapping, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2155 | struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | { |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2157 | struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2158 | struct TCP_Server_Info *server; |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2159 | bool done = false, scanned = false, range_whole = false; |
| 2160 | pgoff_t end, index; |
| 2161 | struct cifs_writedata *wdata; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2162 | int rc = 0; |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2163 | int saved_rc = 0; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2164 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2165 | /* |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2166 | * If wsize is smaller than the page cache size, default to writing |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2167 | * one page at a time via cifs_writepage |
| 2168 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2169 | if (cifs_sb->wsize < PAGE_SIZE) |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2170 | return generic_writepages(mapping, wbc); |
| 2171 | |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 2172 | if (wbc->range_cyclic) { |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2173 | index = mapping->writeback_index; /* Start from prev offset */ |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 2174 | end = -1; |
| 2175 | } else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2176 | index = wbc->range_start >> PAGE_SHIFT; |
| 2177 | end = wbc->range_end >> PAGE_SHIFT; |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 2178 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2179 | range_whole = true; |
| 2180 | scanned = true; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2181 | } |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2182 | server = cifs_sb_master_tcon(cifs_sb)->ses->server; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2183 | retry: |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2184 | while (!done && index <= end) { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2185 | unsigned int i, nr_pages, found_pages, wsize, credits; |
Pavel Shilovsky | 66231a4 | 2014-06-19 16:15:16 +0400 | [diff] [blame] | 2186 | pgoff_t next = 0, tofind, saved_index = index; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2187 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2188 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
| 2189 | &wsize, &credits); |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2190 | if (rc != 0) { |
| 2191 | done = true; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2192 | break; |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2193 | } |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2194 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2195 | tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2196 | |
Pavel Shilovsky | 90ac138 | 2014-06-19 16:11:00 +0400 | [diff] [blame] | 2197 | wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index, |
| 2198 | &found_pages); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2199 | if (!wdata) { |
| 2200 | rc = -ENOMEM; |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2201 | done = true; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2202 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2203 | break; |
| 2204 | } |
| 2205 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2206 | if (found_pages == 0) { |
| 2207 | kref_put(&wdata->refcount, cifs_writedata_release); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2208 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2209 | break; |
| 2210 | } |
| 2211 | |
Pavel Shilovsky | 7e48ff8 | 2014-06-19 15:01:03 +0400 | [diff] [blame] | 2212 | nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc, |
| 2213 | end, &index, &next, &done); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2214 | |
| 2215 | /* nothing to write? */ |
| 2216 | if (nr_pages == 0) { |
| 2217 | kref_put(&wdata->refcount, cifs_writedata_release); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2218 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2219 | continue; |
| 2220 | } |
| 2221 | |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2222 | wdata->credits = credits; |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2223 | |
Pavel Shilovsky | 619aa48 | 2014-06-19 15:28:37 +0400 | [diff] [blame] | 2224 | rc = wdata_send_pages(wdata, nr_pages, mapping, wbc); |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2225 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2226 | /* send failure -- clean up the mess */ |
| 2227 | if (rc != 0) { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2228 | add_credits_and_wake_if(server, wdata->credits, 0); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2229 | for (i = 0; i < nr_pages; ++i) { |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2230 | if (is_retryable_error(rc)) |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2231 | redirty_page_for_writepage(wbc, |
| 2232 | wdata->pages[i]); |
| 2233 | else |
| 2234 | SetPageError(wdata->pages[i]); |
| 2235 | end_page_writeback(wdata->pages[i]); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2236 | put_page(wdata->pages[i]); |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2237 | } |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2238 | if (!is_retryable_error(rc)) |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2239 | mapping_set_error(mapping, rc); |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2240 | } |
| 2241 | kref_put(&wdata->refcount, cifs_writedata_release); |
Jeff Layton | 941b853 | 2011-01-11 07:24:01 -0500 | [diff] [blame] | 2242 | |
Pavel Shilovsky | 66231a4 | 2014-06-19 16:15:16 +0400 | [diff] [blame] | 2243 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) { |
| 2244 | index = saved_index; |
| 2245 | continue; |
| 2246 | } |
| 2247 | |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2248 | /* Return immediately if we received a signal during writing */ |
| 2249 | if (is_interrupt_error(rc)) { |
| 2250 | done = true; |
| 2251 | break; |
| 2252 | } |
| 2253 | |
| 2254 | if (rc != 0 && saved_rc == 0) |
| 2255 | saved_rc = rc; |
| 2256 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2257 | wbc->nr_to_write -= nr_pages; |
| 2258 | if (wbc->nr_to_write <= 0) |
| 2259 | done = true; |
Dave Kleikamp | b066a48 | 2008-11-18 03:49:05 +0000 | [diff] [blame] | 2260 | |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2261 | index = next; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2262 | } |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2263 | |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2264 | if (!scanned && !done) { |
| 2265 | /* |
| 2266 | * We hit the last page and there is more work to be done: wrap |
| 2267 | * back to the start of the file |
| 2268 | */ |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2269 | scanned = true; |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2270 | index = 0; |
| 2271 | goto retry; |
| 2272 | } |
Jeff Layton | c3d17b6 | 2011-05-19 16:22:57 -0400 | [diff] [blame] | 2273 | |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2274 | if (saved_rc != 0) |
| 2275 | rc = saved_rc; |
| 2276 | |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 2277 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 2278 | mapping->writeback_index = index; |
| 2279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | return rc; |
| 2281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2283 | static int |
| 2284 | cifs_writepage_locked(struct page *page, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | { |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2286 | int rc; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2287 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2289 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | /* BB add check for wbc flags */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2291 | get_page(page); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2292 | if (!PageUptodate(page)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2293 | cifs_dbg(FYI, "ppw - page not up to date\n"); |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 2294 | |
| 2295 | /* |
| 2296 | * Set the "writeback" flag, and clear "dirty" in the radix tree. |
| 2297 | * |
| 2298 | * A writepage() implementation always needs to do either this, |
| 2299 | * or re-dirty the page with "redirty_page_for_writepage()" in |
| 2300 | * the case of a failure. |
| 2301 | * |
| 2302 | * Just unlocking the page will cause the radix tree tag-bits |
| 2303 | * to fail to update with the state of the page correctly. |
| 2304 | */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2305 | set_page_writeback(page); |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2306 | retry_write: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2307 | rc = cifs_partialpagewrite(page, 0, PAGE_SIZE); |
Pavel Shilovsky | fb2dabe | 2019-01-08 11:15:28 -0800 | [diff] [blame] | 2308 | if (is_retryable_error(rc)) { |
| 2309 | if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) |
Jeff Layton | 97b37f2 | 2017-05-25 06:59:52 -0400 | [diff] [blame] | 2310 | goto retry_write; |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2311 | redirty_page_for_writepage(wbc, page); |
Jeff Layton | 97b37f2 | 2017-05-25 06:59:52 -0400 | [diff] [blame] | 2312 | } else if (rc != 0) { |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2313 | SetPageError(page); |
Jeff Layton | 97b37f2 | 2017-05-25 06:59:52 -0400 | [diff] [blame] | 2314 | mapping_set_error(page->mapping, rc); |
| 2315 | } else { |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2316 | SetPageUptodate(page); |
Jeff Layton | 97b37f2 | 2017-05-25 06:59:52 -0400 | [diff] [blame] | 2317 | } |
Linus Torvalds | cb876f4 | 2006-12-23 16:19:07 -0800 | [diff] [blame] | 2318 | end_page_writeback(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2319 | put_page(page); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2320 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | return rc; |
| 2322 | } |
| 2323 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 2324 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) |
| 2325 | { |
| 2326 | int rc = cifs_writepage_locked(page, wbc); |
| 2327 | unlock_page(page); |
| 2328 | return rc; |
| 2329 | } |
| 2330 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2331 | static int cifs_write_end(struct file *file, struct address_space *mapping, |
| 2332 | loff_t pos, unsigned len, unsigned copied, |
| 2333 | struct page *page, void *fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2335 | int rc; |
| 2336 | struct inode *inode = mapping->host; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2337 | struct cifsFileInfo *cfile = file->private_data; |
| 2338 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); |
| 2339 | __u32 pid; |
| 2340 | |
| 2341 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 2342 | pid = cfile->pid; |
| 2343 | else |
| 2344 | pid = current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2346 | cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 2347 | page, pos, copied); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 2348 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 2349 | if (PageChecked(page)) { |
| 2350 | if (copied == len) |
| 2351 | SetPageUptodate(page); |
| 2352 | ClearPageChecked(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2353 | } else if (!PageUptodate(page) && copied == PAGE_SIZE) |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2354 | SetPageUptodate(page); |
| 2355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | if (!PageUptodate(page)) { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2357 | char *page_data; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2358 | unsigned offset = pos & (PAGE_SIZE - 1); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2359 | unsigned int xid; |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2360 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2361 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | /* this is probably better than directly calling |
| 2363 | partialpage_write since in this function the file handle is |
| 2364 | known which we might as well leverage */ |
| 2365 | /* BB check if anything else missing out of ppw |
| 2366 | such as updating last write time */ |
| 2367 | page_data = kmap(page); |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2368 | rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2369 | /* if (rc < 0) should we set writebehind rc? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | kunmap(page); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2371 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2372 | free_xid(xid); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2373 | } else { |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2374 | rc = copied; |
| 2375 | pos += copied; |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2376 | set_page_dirty(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2379 | if (rc > 0) { |
| 2380 | spin_lock(&inode->i_lock); |
| 2381 | if (pos > inode->i_size) |
| 2382 | i_size_write(inode, pos); |
| 2383 | spin_unlock(&inode->i_lock); |
| 2384 | } |
| 2385 | |
| 2386 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2387 | put_page(page); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 2388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | return rc; |
| 2390 | } |
| 2391 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2392 | int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, |
| 2393 | int datasync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2395 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | int rc = 0; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2397 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2398 | struct TCP_Server_Info *server; |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 2399 | struct cifsFileInfo *smbfile = file->private_data; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2400 | struct inode *inode = file_inode(file); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2401 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 2403 | rc = file_write_and_wait_range(file, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2404 | if (rc) |
| 2405 | return rc; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2406 | inode_lock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2407 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2408 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 2410 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
| 2411 | file, datasync); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2412 | |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 2413 | if (!CIFS_CACHE_READ(CIFS_I(inode))) { |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 2414 | rc = cifs_zap_mapping(inode); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2415 | if (rc) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2416 | cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc); |
Pavel Shilovsky | 6feb989 | 2011-04-07 18:18:11 +0400 | [diff] [blame] | 2417 | rc = 0; /* don't care about it in fsync */ |
| 2418 | } |
| 2419 | } |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2420 | |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2421 | tcon = tlink_tcon(smbfile->tlink); |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2422 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
| 2423 | server = tcon->ses->server; |
| 2424 | if (server->ops->flush) |
| 2425 | rc = server->ops->flush(xid, tcon, &smbfile->fid); |
| 2426 | else |
| 2427 | rc = -ENOSYS; |
| 2428 | } |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2429 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2430 | free_xid(xid); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2431 | inode_unlock(inode); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2432 | return rc; |
| 2433 | } |
| 2434 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2435 | int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2436 | { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2437 | unsigned int xid; |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2438 | int rc = 0; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2439 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2440 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2441 | struct cifsFileInfo *smbfile = file->private_data; |
Al Viro | 7119e22 | 2014-10-22 00:25:12 -0400 | [diff] [blame] | 2442 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2443 | struct inode *inode = file->f_mapping->host; |
| 2444 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 2445 | rc = file_write_and_wait_range(file, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 2446 | if (rc) |
| 2447 | return rc; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2448 | inode_lock(inode); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2449 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2450 | xid = get_xid(); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2451 | |
Al Viro | 35c265e | 2014-08-19 20:25:34 -0400 | [diff] [blame] | 2452 | cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", |
| 2453 | file, datasync); |
Pavel Shilovsky | 8be7e6b | 2010-12-12 13:11:13 +0300 | [diff] [blame] | 2454 | |
| 2455 | tcon = tlink_tcon(smbfile->tlink); |
Pavel Shilovsky | 1d8c4c0 | 2012-09-18 16:20:27 -0700 | [diff] [blame] | 2456 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { |
| 2457 | server = tcon->ses->server; |
| 2458 | if (server->ops->flush) |
| 2459 | rc = server->ops->flush(xid, tcon, &smbfile->fid); |
| 2460 | else |
| 2461 | rc = -ENOSYS; |
| 2462 | } |
Steve French | b298f22 | 2009-02-21 21:17:43 +0000 | [diff] [blame] | 2463 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2464 | free_xid(xid); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2465 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | return rc; |
| 2467 | } |
| 2468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | /* |
| 2470 | * As file closes, flush all cached write data for this inode checking |
| 2471 | * for write behind errors. |
| 2472 | */ |
Miklos Szeredi | 75e1fcc | 2006-06-23 02:05:12 -0700 | [diff] [blame] | 2473 | int cifs_flush(struct file *file, fl_owner_t id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2475 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | int rc = 0; |
| 2477 | |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 2478 | if (file->f_mode & FMODE_WRITE) |
Jeff Layton | d3f1322 | 2010-10-15 15:34:07 -0400 | [diff] [blame] | 2479 | rc = filemap_write_and_wait(inode->i_mapping); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2480 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2481 | cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | |
| 2483 | return rc; |
| 2484 | } |
| 2485 | |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2486 | static int |
| 2487 | cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) |
| 2488 | { |
| 2489 | int rc = 0; |
| 2490 | unsigned long i; |
| 2491 | |
| 2492 | for (i = 0; i < num_pages; i++) { |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2493 | pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2494 | if (!pages[i]) { |
| 2495 | /* |
| 2496 | * save number of pages we have already allocated and |
| 2497 | * return with ENOMEM error |
| 2498 | */ |
| 2499 | num_pages = i; |
| 2500 | rc = -ENOMEM; |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2501 | break; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
Jeff Layton | e94f7ba | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2505 | if (rc) { |
| 2506 | for (i = 0; i < num_pages; i++) |
| 2507 | put_page(pages[i]); |
| 2508 | } |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2509 | return rc; |
| 2510 | } |
| 2511 | |
| 2512 | static inline |
| 2513 | size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) |
| 2514 | { |
| 2515 | size_t num_pages; |
| 2516 | size_t clen; |
| 2517 | |
| 2518 | clen = min_t(const size_t, len, wsize); |
Jeff Layton | a7103b9 | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2519 | num_pages = DIV_ROUND_UP(clen, PAGE_SIZE); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2520 | |
| 2521 | if (cur_len) |
| 2522 | *cur_len = clen; |
| 2523 | |
| 2524 | return num_pages; |
| 2525 | } |
| 2526 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2527 | static void |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2528 | cifs_uncached_writedata_release(struct kref *refcount) |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2529 | { |
| 2530 | int i; |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2531 | struct cifs_writedata *wdata = container_of(refcount, |
| 2532 | struct cifs_writedata, refcount); |
| 2533 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2534 | kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2535 | for (i = 0; i < wdata->nr_pages; i++) |
| 2536 | put_page(wdata->pages[i]); |
| 2537 | cifs_writedata_release(refcount); |
| 2538 | } |
| 2539 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2540 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx); |
| 2541 | |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2542 | static void |
| 2543 | cifs_uncached_writev_complete(struct work_struct *work) |
| 2544 | { |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2545 | struct cifs_writedata *wdata = container_of(work, |
| 2546 | struct cifs_writedata, work); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2547 | struct inode *inode = d_inode(wdata->cfile->dentry); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2548 | struct cifsInodeInfo *cifsi = CIFS_I(inode); |
| 2549 | |
| 2550 | spin_lock(&inode->i_lock); |
| 2551 | cifs_update_eof(cifsi, wdata->offset, wdata->bytes); |
| 2552 | if (cifsi->server_eof > inode->i_size) |
| 2553 | i_size_write(inode, cifsi->server_eof); |
| 2554 | spin_unlock(&inode->i_lock); |
| 2555 | |
| 2556 | complete(&wdata->done); |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2557 | collect_uncached_write_data(wdata->ctx); |
| 2558 | /* the below call can possibly free the last ref to aio ctx */ |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2559 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2560 | } |
| 2561 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2562 | static int |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2563 | wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from, |
| 2564 | size_t *len, unsigned long *num_pages) |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2565 | { |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2566 | size_t save_len, copied, bytes, cur_len = *len; |
| 2567 | unsigned long i, nr_pages = *num_pages; |
| 2568 | |
| 2569 | save_len = cur_len; |
| 2570 | for (i = 0; i < nr_pages; i++) { |
| 2571 | bytes = min_t(const size_t, cur_len, PAGE_SIZE); |
| 2572 | copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from); |
| 2573 | cur_len -= copied; |
| 2574 | /* |
| 2575 | * If we didn't copy as much as we expected, then that |
| 2576 | * may mean we trod into an unmapped area. Stop copying |
| 2577 | * at that point. On the next pass through the big |
| 2578 | * loop, we'll likely end up getting a zero-length |
| 2579 | * write and bailing out of it. |
| 2580 | */ |
| 2581 | if (copied < bytes) |
| 2582 | break; |
| 2583 | } |
| 2584 | cur_len = save_len - cur_len; |
| 2585 | *len = cur_len; |
| 2586 | |
| 2587 | /* |
| 2588 | * If we have no data to send, then that probably means that |
| 2589 | * the copy above failed altogether. That's most likely because |
| 2590 | * the address in the iovec was bogus. Return -EFAULT and let |
| 2591 | * the caller free anything we allocated and bail out. |
| 2592 | */ |
| 2593 | if (!cur_len) |
| 2594 | return -EFAULT; |
| 2595 | |
| 2596 | /* |
| 2597 | * i + 1 now represents the number of pages we actually used in |
| 2598 | * the copy phase above. |
| 2599 | */ |
| 2600 | *num_pages = i + 1; |
| 2601 | return 0; |
| 2602 | } |
| 2603 | |
Pavel Shilovsky | 43de94e | 2014-06-20 16:10:52 +0400 | [diff] [blame] | 2604 | static int |
| 2605 | cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from, |
| 2606 | struct cifsFileInfo *open_file, |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2607 | struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, |
| 2608 | struct cifs_aio_ctx *ctx) |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2609 | { |
Pavel Shilovsky | 43de94e | 2014-06-20 16:10:52 +0400 | [diff] [blame] | 2610 | int rc = 0; |
| 2611 | size_t cur_len; |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2612 | unsigned long nr_pages, num_pages, i; |
Pavel Shilovsky | 43de94e | 2014-06-20 16:10:52 +0400 | [diff] [blame] | 2613 | struct cifs_writedata *wdata; |
Al Viro | fc56b98 | 2016-09-21 18:18:23 -0400 | [diff] [blame] | 2614 | struct iov_iter saved_from = *from; |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2615 | loff_t saved_offset = offset; |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2616 | pid_t pid; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2617 | struct TCP_Server_Info *server; |
| 2618 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 2619 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 2620 | pid = open_file->pid; |
| 2621 | else |
| 2622 | pid = current->tgid; |
| 2623 | |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2624 | server = tlink_tcon(open_file->tlink)->ses->server; |
Pavel Shilovsky | 76429c1 | 2011-01-31 16:03:08 +0300 | [diff] [blame] | 2625 | |
| 2626 | do { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2627 | unsigned int wsize, credits; |
| 2628 | |
| 2629 | rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize, |
| 2630 | &wsize, &credits); |
| 2631 | if (rc) |
| 2632 | break; |
| 2633 | |
| 2634 | nr_pages = get_numpages(wsize, len, &cur_len); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2635 | wdata = cifs_writedata_alloc(nr_pages, |
| 2636 | cifs_uncached_writev_complete); |
| 2637 | if (!wdata) { |
| 2638 | rc = -ENOMEM; |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2639 | add_credits_and_wake_if(server, credits, 0); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2640 | break; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2641 | } |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2642 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2643 | rc = cifs_write_allocate_pages(wdata->pages, nr_pages); |
| 2644 | if (rc) { |
| 2645 | kfree(wdata); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2646 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2647 | break; |
| 2648 | } |
| 2649 | |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2650 | num_pages = nr_pages; |
| 2651 | rc = wdata_fill_from_iovec(wdata, from, &cur_len, &num_pages); |
| 2652 | if (rc) { |
Jeff Layton | 5d81de8 | 2014-02-14 07:20:35 -0500 | [diff] [blame] | 2653 | for (i = 0; i < nr_pages; i++) |
| 2654 | put_page(wdata->pages[i]); |
| 2655 | kfree(wdata); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2656 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | 5d81de8 | 2014-02-14 07:20:35 -0500 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | |
| 2660 | /* |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2661 | * Bring nr_pages down to the number of pages we actually used, |
| 2662 | * and free any pages that we didn't use. |
Jeff Layton | 5d81de8 | 2014-02-14 07:20:35 -0500 | [diff] [blame] | 2663 | */ |
Pavel Shilovsky | 66386c0 | 2014-06-20 15:48:40 +0400 | [diff] [blame] | 2664 | for ( ; nr_pages > num_pages; nr_pages--) |
Jeff Layton | 5d81de8 | 2014-02-14 07:20:35 -0500 | [diff] [blame] | 2665 | put_page(wdata->pages[nr_pages - 1]); |
| 2666 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2667 | wdata->sync_mode = WB_SYNC_ALL; |
| 2668 | wdata->nr_pages = nr_pages; |
| 2669 | wdata->offset = (__u64)offset; |
| 2670 | wdata->cfile = cifsFileInfo_get(open_file); |
| 2671 | wdata->pid = pid; |
| 2672 | wdata->bytes = cur_len; |
Jeff Layton | eddb079 | 2012-09-18 16:20:35 -0700 | [diff] [blame] | 2673 | wdata->pagesz = PAGE_SIZE; |
| 2674 | wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE); |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2675 | wdata->credits = credits; |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2676 | wdata->ctx = ctx; |
| 2677 | kref_get(&ctx->refcount); |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2678 | |
| 2679 | if (!wdata->cfile->invalidHandle || |
Germano Percossi | 1fa839b | 2017-04-07 12:29:38 +0100 | [diff] [blame] | 2680 | !(rc = cifs_reopen_file(wdata->cfile, false))) |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2681 | rc = server->ops->async_writev(wdata, |
| 2682 | cifs_uncached_writedata_release); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2683 | if (rc) { |
Pavel Shilovsky | cb7e9ea | 2014-06-05 19:03:27 +0400 | [diff] [blame] | 2684 | add_credits_and_wake_if(server, wdata->credits, 0); |
Steve French | 4a5c80d | 2014-02-07 20:45:12 -0600 | [diff] [blame] | 2685 | kref_put(&wdata->refcount, |
| 2686 | cifs_uncached_writedata_release); |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2687 | if (rc == -EAGAIN) { |
Al Viro | fc56b98 | 2016-09-21 18:18:23 -0400 | [diff] [blame] | 2688 | *from = saved_from; |
Pavel Shilovsky | 6ec0b01 | 2014-06-20 16:30:46 +0400 | [diff] [blame] | 2689 | iov_iter_advance(from, offset - saved_offset); |
| 2690 | continue; |
| 2691 | } |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2692 | break; |
| 2693 | } |
| 2694 | |
Pavel Shilovsky | 43de94e | 2014-06-20 16:10:52 +0400 | [diff] [blame] | 2695 | list_add_tail(&wdata->list, wdata_list); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2696 | offset += cur_len; |
| 2697 | len -= cur_len; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2698 | } while (len > 0); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2699 | |
| 2700 | return rc; |
| 2701 | } |
| 2702 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2703 | static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) |
| 2704 | { |
| 2705 | struct cifs_writedata *wdata, *tmp; |
| 2706 | struct cifs_tcon *tcon; |
| 2707 | struct cifs_sb_info *cifs_sb; |
| 2708 | struct dentry *dentry = ctx->cfile->dentry; |
| 2709 | unsigned int i; |
| 2710 | int rc; |
| 2711 | |
| 2712 | tcon = tlink_tcon(ctx->cfile->tlink); |
| 2713 | cifs_sb = CIFS_SB(dentry->d_sb); |
| 2714 | |
| 2715 | mutex_lock(&ctx->aio_mutex); |
| 2716 | |
| 2717 | if (list_empty(&ctx->list)) { |
| 2718 | mutex_unlock(&ctx->aio_mutex); |
| 2719 | return; |
| 2720 | } |
| 2721 | |
| 2722 | rc = ctx->rc; |
| 2723 | /* |
| 2724 | * Wait for and collect replies for any successful sends in order of |
| 2725 | * increasing offset. Once an error is hit, then return without waiting |
| 2726 | * for any more replies. |
| 2727 | */ |
| 2728 | restart_loop: |
| 2729 | list_for_each_entry_safe(wdata, tmp, &ctx->list, list) { |
| 2730 | if (!rc) { |
| 2731 | if (!try_wait_for_completion(&wdata->done)) { |
| 2732 | mutex_unlock(&ctx->aio_mutex); |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | if (wdata->result) |
| 2737 | rc = wdata->result; |
| 2738 | else |
| 2739 | ctx->total_len += wdata->bytes; |
| 2740 | |
| 2741 | /* resend call if it's a retryable error */ |
| 2742 | if (rc == -EAGAIN) { |
| 2743 | struct list_head tmp_list; |
| 2744 | struct iov_iter tmp_from = ctx->iter; |
| 2745 | |
| 2746 | INIT_LIST_HEAD(&tmp_list); |
| 2747 | list_del_init(&wdata->list); |
| 2748 | |
| 2749 | iov_iter_advance(&tmp_from, |
| 2750 | wdata->offset - ctx->pos); |
| 2751 | |
| 2752 | rc = cifs_write_from_iter(wdata->offset, |
| 2753 | wdata->bytes, &tmp_from, |
| 2754 | ctx->cfile, cifs_sb, &tmp_list, |
| 2755 | ctx); |
| 2756 | |
| 2757 | list_splice(&tmp_list, &ctx->list); |
| 2758 | |
| 2759 | kref_put(&wdata->refcount, |
| 2760 | cifs_uncached_writedata_release); |
| 2761 | goto restart_loop; |
| 2762 | } |
| 2763 | } |
| 2764 | list_del_init(&wdata->list); |
| 2765 | kref_put(&wdata->refcount, cifs_uncached_writedata_release); |
| 2766 | } |
| 2767 | |
| 2768 | for (i = 0; i < ctx->npages; i++) |
| 2769 | put_page(ctx->bv[i].bv_page); |
| 2770 | |
| 2771 | cifs_stats_bytes_written(tcon, ctx->total_len); |
| 2772 | set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); |
| 2773 | |
| 2774 | ctx->rc = (rc == 0) ? ctx->total_len : rc; |
| 2775 | |
| 2776 | mutex_unlock(&ctx->aio_mutex); |
| 2777 | |
| 2778 | if (ctx->iocb && ctx->iocb->ki_complete) |
| 2779 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); |
| 2780 | else |
| 2781 | complete(&ctx->done); |
| 2782 | } |
| 2783 | |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2784 | ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2785 | { |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2786 | struct file *file = iocb->ki_filp; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2787 | ssize_t total_written = 0; |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2788 | struct cifsFileInfo *cfile; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2789 | struct cifs_tcon *tcon; |
| 2790 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2791 | struct cifs_aio_ctx *ctx; |
Al Viro | fc56b98 | 2016-09-21 18:18:23 -0400 | [diff] [blame] | 2792 | struct iov_iter saved_from = *from; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2793 | int rc; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2794 | |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2795 | /* |
| 2796 | * BB - optimize the way when signing is disabled. We can drop this |
| 2797 | * extra memory-to-memory copying and use iovec buffers for constructing |
| 2798 | * write request. |
| 2799 | */ |
| 2800 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2801 | rc = generic_write_checks(iocb, from); |
| 2802 | if (rc <= 0) |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2803 | return rc; |
| 2804 | |
Al Viro | 7119e22 | 2014-10-22 00:25:12 -0400 | [diff] [blame] | 2805 | cifs_sb = CIFS_FILE_SB(file); |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2806 | cfile = file->private_data; |
| 2807 | tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2808 | |
| 2809 | if (!tcon->ses->server->ops->async_writev) |
| 2810 | return -ENOSYS; |
| 2811 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2812 | ctx = cifs_aio_ctx_alloc(); |
| 2813 | if (!ctx) |
| 2814 | return -ENOMEM; |
| 2815 | |
| 2816 | ctx->cfile = cifsFileInfo_get(cfile); |
| 2817 | |
| 2818 | if (!is_sync_kiocb(iocb)) |
| 2819 | ctx->iocb = iocb; |
| 2820 | |
| 2821 | ctx->pos = iocb->ki_pos; |
| 2822 | |
| 2823 | rc = setup_aio_ctx_iter(ctx, from, WRITE); |
| 2824 | if (rc) { |
| 2825 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 2826 | return rc; |
| 2827 | } |
| 2828 | |
| 2829 | /* grab a lock here due to read response handlers can access ctx */ |
| 2830 | mutex_lock(&ctx->aio_mutex); |
| 2831 | |
| 2832 | rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from, |
| 2833 | cfile, cifs_sb, &ctx->list, ctx); |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2834 | |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2835 | /* |
| 2836 | * If at least one write was successfully sent, then discard any rc |
| 2837 | * value from the later writes. If the other write succeeds, then |
| 2838 | * we'll end up returning whatever was written. If it fails, then |
| 2839 | * we'll get a new rc value from that. |
| 2840 | */ |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2841 | if (!list_empty(&ctx->list)) |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2842 | rc = 0; |
| 2843 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2844 | mutex_unlock(&ctx->aio_mutex); |
Jeff Layton | da82f7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 2845 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2846 | if (rc) { |
| 2847 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 2848 | return rc; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2849 | } |
| 2850 | |
Pavel Shilovsky | c610c4b | 2017-04-25 11:52:31 -0700 | [diff] [blame] | 2851 | if (!is_sync_kiocb(iocb)) { |
| 2852 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 2853 | return -EIOCBQUEUED; |
| 2854 | } |
| 2855 | |
| 2856 | rc = wait_for_completion_killable(&ctx->done); |
| 2857 | if (rc) { |
| 2858 | mutex_lock(&ctx->aio_mutex); |
| 2859 | ctx->rc = rc = -EINTR; |
| 2860 | total_written = ctx->total_len; |
| 2861 | mutex_unlock(&ctx->aio_mutex); |
| 2862 | } else { |
| 2863 | rc = ctx->rc; |
| 2864 | total_written = ctx->total_len; |
| 2865 | } |
| 2866 | |
| 2867 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 2868 | |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2869 | if (unlikely(!total_written)) |
| 2870 | return rc; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2871 | |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2872 | iocb->ki_pos += total_written; |
Al Viro | e9d1593 | 2015-04-06 22:44:11 -0400 | [diff] [blame] | 2873 | return total_written; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2874 | } |
| 2875 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2876 | static ssize_t |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2877 | cifs_writev(struct kiocb *iocb, struct iov_iter *from) |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2878 | { |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2879 | struct file *file = iocb->ki_filp; |
| 2880 | struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; |
| 2881 | struct inode *inode = file->f_mapping->host; |
| 2882 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 2883 | struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2884 | ssize_t rc; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2885 | |
Rabin Vincent | 966681c | 2017-06-29 16:01:42 +0200 | [diff] [blame] | 2886 | inode_lock(inode); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2887 | /* |
| 2888 | * We need to hold the sem to be sure nobody modifies lock list |
| 2889 | * with a brlock that prevents writing. |
| 2890 | */ |
| 2891 | down_read(&cinode->lock_sem); |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2892 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 2893 | rc = generic_write_checks(iocb, from); |
| 2894 | if (rc <= 0) |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2895 | goto out; |
| 2896 | |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2897 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2898 | server->vals->exclusive_lock_type, NULL, |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2899 | CIFS_WRITE_OP)) |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2900 | rc = __generic_file_write_iter(iocb, from); |
Al Viro | 5f380c7 | 2015-04-07 11:28:12 -0400 | [diff] [blame] | 2901 | else |
| 2902 | rc = -EACCES; |
| 2903 | out: |
Rabin Vincent | 966681c | 2017-06-29 16:01:42 +0200 | [diff] [blame] | 2904 | up_read(&cinode->lock_sem); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2905 | inode_unlock(inode); |
Al Viro | 19dfc1f | 2014-04-03 10:27:17 -0400 | [diff] [blame] | 2906 | |
Christoph Hellwig | e259221 | 2016-04-07 08:52:01 -0700 | [diff] [blame] | 2907 | if (rc > 0) |
| 2908 | rc = generic_write_sync(iocb, rc); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2909 | return rc; |
| 2910 | } |
| 2911 | |
| 2912 | ssize_t |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2913 | cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2914 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2915 | struct inode *inode = file_inode(iocb->ki_filp); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 2916 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 2917 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 2918 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) |
| 2919 | iocb->ki_filp->private_data; |
| 2920 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2921 | ssize_t written; |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2922 | |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 2923 | written = cifs_get_writer(cinode); |
| 2924 | if (written) |
| 2925 | return written; |
| 2926 | |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 2927 | if (CIFS_CACHE_WRITE(cinode)) { |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2928 | if (cap_unix(tcon->ses) && |
| 2929 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 2930 | && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2931 | written = generic_file_write_iter(iocb, from); |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 2932 | goto out; |
| 2933 | } |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2934 | written = cifs_writev(iocb, from); |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 2935 | goto out; |
Pavel Shilovsky | c299dd0 | 2012-12-06 22:07:52 +0400 | [diff] [blame] | 2936 | } |
Pavel Shilovsky | ca8aa29 | 2012-12-21 15:05:47 +0400 | [diff] [blame] | 2937 | /* |
| 2938 | * For non-oplocked files in strict cache mode we need to write the data |
| 2939 | * to the server exactly from the pos to pos+len-1 rather than flush all |
| 2940 | * affected pages because it may cause a error with mandatory locks on |
| 2941 | * these pages but not on the region from pos to ppos+len-1. |
| 2942 | */ |
Al Viro | 3dae875 | 2014-04-03 12:05:17 -0400 | [diff] [blame] | 2943 | written = cifs_user_writev(iocb, from); |
Pavel Shilovsky | 43eaa6c | 2019-03-04 17:48:01 -0800 | [diff] [blame] | 2944 | if (CIFS_CACHE_READ(cinode)) { |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2945 | /* |
Pavel Shilovsky | 43eaa6c | 2019-03-04 17:48:01 -0800 | [diff] [blame] | 2946 | * We have read level caching and we have just sent a write |
| 2947 | * request to the server thus making data in the cache stale. |
| 2948 | * Zap the cache and set oplock/lease level to NONE to avoid |
| 2949 | * reading stale data from the cache. All subsequent read |
| 2950 | * operations will read new data from the server. |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2951 | */ |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 2952 | cifs_zap_mapping(inode); |
Pavel Shilovsky | 43eaa6c | 2019-03-04 17:48:01 -0800 | [diff] [blame] | 2953 | cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2954 | inode); |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 2955 | cinode->oplock = 0; |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2956 | } |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 2957 | out: |
| 2958 | cifs_put_writer(cinode); |
Pavel Shilovsky | 88cf75a | 2012-12-21 15:07:52 +0400 | [diff] [blame] | 2959 | return written; |
Pavel Shilovsky | 72432ff | 2011-01-24 14:16:35 -0500 | [diff] [blame] | 2960 | } |
| 2961 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2962 | static struct cifs_readdata * |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 2963 | cifs_readdata_direct_alloc(struct page **pages, work_func_t complete) |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2964 | { |
| 2965 | struct cifs_readdata *rdata; |
Jeff Layton | f4e49cd | 2012-09-18 16:20:36 -0700 | [diff] [blame] | 2966 | |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 2967 | rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2968 | if (rdata != NULL) { |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 2969 | rdata->pages = pages; |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2970 | kref_init(&rdata->refcount); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2971 | INIT_LIST_HEAD(&rdata->list); |
| 2972 | init_completion(&rdata->done); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2973 | INIT_WORK(&rdata->work, complete); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2974 | } |
Jeff Layton | f4e49cd | 2012-09-18 16:20:36 -0700 | [diff] [blame] | 2975 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2976 | return rdata; |
| 2977 | } |
| 2978 | |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 2979 | static struct cifs_readdata * |
| 2980 | cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete) |
| 2981 | { |
| 2982 | struct page **pages = |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2983 | kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 2984 | struct cifs_readdata *ret = NULL; |
| 2985 | |
| 2986 | if (pages) { |
| 2987 | ret = cifs_readdata_direct_alloc(pages, complete); |
| 2988 | if (!ret) |
| 2989 | kfree(pages); |
| 2990 | } |
| 2991 | |
| 2992 | return ret; |
| 2993 | } |
| 2994 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2995 | void |
| 2996 | cifs_readdata_release(struct kref *refcount) |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 2997 | { |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 2998 | struct cifs_readdata *rdata = container_of(refcount, |
| 2999 | struct cifs_readdata, refcount); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3000 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3001 | if (rdata->mr) { |
| 3002 | smbd_deregister_mr(rdata->mr); |
| 3003 | rdata->mr = NULL; |
| 3004 | } |
| 3005 | #endif |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3006 | if (rdata->cfile) |
| 3007 | cifsFileInfo_put(rdata->cfile); |
| 3008 | |
Long Li | f9f5aca | 2018-05-30 12:47:54 -0700 | [diff] [blame] | 3009 | kvfree(rdata->pages); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3010 | kfree(rdata); |
| 3011 | } |
| 3012 | |
Jeff Layton | 2a1bb13 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3013 | static int |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3014 | cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages) |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3015 | { |
| 3016 | int rc = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3017 | struct page *page; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3018 | unsigned int i; |
| 3019 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3020 | for (i = 0; i < nr_pages; i++) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3021 | page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM); |
| 3022 | if (!page) { |
| 3023 | rc = -ENOMEM; |
| 3024 | break; |
| 3025 | } |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3026 | rdata->pages[i] = page; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | if (rc) { |
Roberto Bergantinos Corpas | 297a251 | 2019-05-28 09:38:14 +0200 | [diff] [blame] | 3030 | unsigned int nr_page_failed = i; |
| 3031 | |
| 3032 | for (i = 0; i < nr_page_failed; i++) { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3033 | put_page(rdata->pages[i]); |
| 3034 | rdata->pages[i] = NULL; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | return rc; |
| 3038 | } |
| 3039 | |
| 3040 | static void |
| 3041 | cifs_uncached_readdata_release(struct kref *refcount) |
| 3042 | { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3043 | struct cifs_readdata *rdata = container_of(refcount, |
| 3044 | struct cifs_readdata, refcount); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3045 | unsigned int i; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3046 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3047 | kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3048 | for (i = 0; i < rdata->nr_pages; i++) { |
| 3049 | put_page(rdata->pages[i]); |
| 3050 | rdata->pages[i] = NULL; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3051 | } |
| 3052 | cifs_readdata_release(refcount); |
| 3053 | } |
| 3054 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3055 | /** |
| 3056 | * cifs_readdata_to_iov - copy data from pages in response to an iovec |
| 3057 | * @rdata: the readdata response with list of pages holding data |
Al Viro | 7f25bba | 2014-02-04 14:07:43 -0500 | [diff] [blame] | 3058 | * @iter: destination for our data |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3059 | * |
| 3060 | * This function copies data from a list of pages in a readdata response into |
| 3061 | * an array of iovecs. It will first calculate where the data should go |
| 3062 | * based on the info in the readdata and then copy the data into that spot. |
| 3063 | */ |
Al Viro | 7f25bba | 2014-02-04 14:07:43 -0500 | [diff] [blame] | 3064 | static int |
| 3065 | cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter) |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3066 | { |
Pavel Shilovsky | 34a54d6 | 2014-07-10 10:03:29 +0400 | [diff] [blame] | 3067 | size_t remaining = rdata->got_bytes; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3068 | unsigned int i; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3069 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3070 | for (i = 0; i < rdata->nr_pages; i++) { |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3071 | struct page *page = rdata->pages[i]; |
Geert Uytterhoeven | e686bd8 | 2014-04-13 20:46:21 +0200 | [diff] [blame] | 3072 | size_t copy = min_t(size_t, remaining, PAGE_SIZE); |
Pavel Shilovsky | 9c25702 | 2017-01-19 13:53:15 -0800 | [diff] [blame] | 3073 | size_t written; |
| 3074 | |
| 3075 | if (unlikely(iter->type & ITER_PIPE)) { |
| 3076 | void *addr = kmap_atomic(page); |
| 3077 | |
| 3078 | written = copy_to_iter(addr, copy, iter); |
| 3079 | kunmap_atomic(addr); |
| 3080 | } else |
| 3081 | written = copy_page_to_iter(page, 0, copy, iter); |
Al Viro | 7f25bba | 2014-02-04 14:07:43 -0500 | [diff] [blame] | 3082 | remaining -= written; |
| 3083 | if (written < copy && iov_iter_count(iter) > 0) |
| 3084 | break; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3085 | } |
Al Viro | 7f25bba | 2014-02-04 14:07:43 -0500 | [diff] [blame] | 3086 | return remaining ? -EFAULT : 0; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3087 | } |
| 3088 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3089 | static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); |
| 3090 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3091 | static void |
| 3092 | cifs_uncached_readv_complete(struct work_struct *work) |
| 3093 | { |
| 3094 | struct cifs_readdata *rdata = container_of(work, |
| 3095 | struct cifs_readdata, work); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3096 | |
| 3097 | complete(&rdata->done); |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3098 | collect_uncached_read_data(rdata->ctx); |
| 3099 | /* the below call can possibly free the last ref to aio ctx */ |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3100 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
| 3101 | } |
| 3102 | |
| 3103 | static int |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3104 | uncached_fill_pages(struct TCP_Server_Info *server, |
| 3105 | struct cifs_readdata *rdata, struct iov_iter *iter, |
| 3106 | unsigned int len) |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3107 | { |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3108 | int result = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3109 | unsigned int i; |
| 3110 | unsigned int nr_pages = rdata->nr_pages; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3111 | unsigned int page_offset = rdata->page_offset; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3112 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3113 | rdata->got_bytes = 0; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3114 | rdata->tailsz = PAGE_SIZE; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3115 | for (i = 0; i < nr_pages; i++) { |
| 3116 | struct page *page = rdata->pages[i]; |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3117 | size_t n; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3118 | unsigned int segment_size = rdata->pagesz; |
| 3119 | |
| 3120 | if (i == 0) |
| 3121 | segment_size -= page_offset; |
| 3122 | else |
| 3123 | page_offset = 0; |
| 3124 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3125 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3126 | if (len <= 0) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3127 | /* no need to hold page hostage */ |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3128 | rdata->pages[i] = NULL; |
| 3129 | rdata->nr_pages--; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3130 | put_page(page); |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3131 | continue; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3132 | } |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3133 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3134 | n = len; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3135 | if (len >= segment_size) |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3136 | /* enough data to fill the page */ |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3137 | n = segment_size; |
| 3138 | else |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3139 | rdata->tailsz = len; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3140 | len -= n; |
| 3141 | |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3142 | if (iter) |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3143 | result = copy_page_from_iter( |
| 3144 | page, page_offset, n, iter); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3145 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3146 | else if (rdata->mr) |
| 3147 | result = n; |
| 3148 | #endif |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3149 | else |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3150 | result = cifs_read_page_from_socket( |
| 3151 | server, page, page_offset, n); |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3152 | if (result < 0) |
| 3153 | break; |
| 3154 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3155 | rdata->got_bytes += result; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3156 | } |
| 3157 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3158 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
| 3159 | rdata->got_bytes : result; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3160 | } |
| 3161 | |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3162 | static int |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3163 | cifs_uncached_read_into_pages(struct TCP_Server_Info *server, |
| 3164 | struct cifs_readdata *rdata, unsigned int len) |
| 3165 | { |
| 3166 | return uncached_fill_pages(server, rdata, NULL, len); |
| 3167 | } |
| 3168 | |
| 3169 | static int |
| 3170 | cifs_uncached_copy_into_pages(struct TCP_Server_Info *server, |
| 3171 | struct cifs_readdata *rdata, |
| 3172 | struct iov_iter *iter) |
| 3173 | { |
| 3174 | return uncached_fill_pages(server, rdata, iter, iter->count); |
| 3175 | } |
| 3176 | |
| 3177 | static int |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3178 | cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file, |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3179 | struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, |
| 3180 | struct cifs_aio_ctx *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | { |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3182 | struct cifs_readdata *rdata; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3183 | unsigned int npages, rsize, credits; |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3184 | size_t cur_len; |
| 3185 | int rc; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3186 | pid_t pid; |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3187 | struct TCP_Server_Info *server; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3188 | |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3189 | server = tlink_tcon(open_file->tlink)->ses->server; |
Pavel Shilovsky | fc9c596 | 2012-09-18 16:20:28 -0700 | [diff] [blame] | 3190 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3191 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 3192 | pid = open_file->pid; |
| 3193 | else |
| 3194 | pid = current->tgid; |
| 3195 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3196 | do { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3197 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
| 3198 | &rsize, &credits); |
| 3199 | if (rc) |
| 3200 | break; |
| 3201 | |
| 3202 | cur_len = min_t(const size_t, len, rsize); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3203 | npages = DIV_ROUND_UP(cur_len, PAGE_SIZE); |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3204 | |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3205 | /* allocate a readdata struct */ |
| 3206 | rdata = cifs_readdata_alloc(npages, |
| 3207 | cifs_uncached_readv_complete); |
| 3208 | if (!rdata) { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3209 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3210 | rc = -ENOMEM; |
Jeff Layton | bae9f74 | 2014-04-15 12:48:49 -0400 | [diff] [blame] | 3211 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | } |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3213 | |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3214 | rc = cifs_read_allocate_pages(rdata, npages); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3215 | if (rc) |
| 3216 | goto error; |
| 3217 | |
| 3218 | rdata->cfile = cifsFileInfo_get(open_file); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3219 | rdata->nr_pages = npages; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3220 | rdata->offset = offset; |
| 3221 | rdata->bytes = cur_len; |
| 3222 | rdata->pid = pid; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3223 | rdata->pagesz = PAGE_SIZE; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3224 | rdata->tailsz = PAGE_SIZE; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3225 | rdata->read_into_pages = cifs_uncached_read_into_pages; |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3226 | rdata->copy_into_pages = cifs_uncached_copy_into_pages; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3227 | rdata->credits = credits; |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3228 | rdata->ctx = ctx; |
| 3229 | kref_get(&ctx->refcount); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3230 | |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3231 | if (!rdata->cfile->invalidHandle || |
Germano Percossi | 1fa839b | 2017-04-07 12:29:38 +0100 | [diff] [blame] | 3232 | !(rc = cifs_reopen_file(rdata->cfile, true))) |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3233 | rc = server->ops->async_readv(rdata); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3234 | error: |
| 3235 | if (rc) { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3236 | add_credits_and_wake_if(server, rdata->credits, 0); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3237 | kref_put(&rdata->refcount, |
| 3238 | cifs_uncached_readdata_release); |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3239 | if (rc == -EAGAIN) |
| 3240 | continue; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3241 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3242 | } |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3243 | |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3244 | list_add_tail(&rdata->list, rdata_list); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3245 | offset += cur_len; |
| 3246 | len -= cur_len; |
| 3247 | } while (len > 0); |
| 3248 | |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3249 | return rc; |
| 3250 | } |
| 3251 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3252 | static void |
| 3253 | collect_uncached_read_data(struct cifs_aio_ctx *ctx) |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3254 | { |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3255 | struct cifs_readdata *rdata, *tmp; |
| 3256 | struct iov_iter *to = &ctx->iter; |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3257 | struct cifs_sb_info *cifs_sb; |
| 3258 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3259 | unsigned int i; |
| 3260 | int rc; |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3261 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3262 | tcon = tlink_tcon(ctx->cfile->tlink); |
| 3263 | cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb); |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3264 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3265 | mutex_lock(&ctx->aio_mutex); |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3266 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3267 | if (list_empty(&ctx->list)) { |
| 3268 | mutex_unlock(&ctx->aio_mutex); |
| 3269 | return; |
| 3270 | } |
Pavel Shilovsky | 0ada36b | 2014-06-25 10:42:28 +0400 | [diff] [blame] | 3271 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3272 | rc = ctx->rc; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3273 | /* the loop below should proceed in the order of increasing offsets */ |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3274 | again: |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3275 | list_for_each_entry_safe(rdata, tmp, &ctx->list, list) { |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3276 | if (!rc) { |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3277 | if (!try_wait_for_completion(&rdata->done)) { |
| 3278 | mutex_unlock(&ctx->aio_mutex); |
| 3279 | return; |
| 3280 | } |
| 3281 | |
| 3282 | if (rdata->result == -EAGAIN) { |
Al Viro | 74027f4 | 2014-02-04 13:47:26 -0500 | [diff] [blame] | 3283 | /* resend call if it's a retryable error */ |
Pavel Shilovsky | fb8a3e5 | 2014-07-10 11:50:39 +0400 | [diff] [blame] | 3284 | struct list_head tmp_list; |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 3285 | unsigned int got_bytes = rdata->got_bytes; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3286 | |
Pavel Shilovsky | fb8a3e5 | 2014-07-10 11:50:39 +0400 | [diff] [blame] | 3287 | list_del_init(&rdata->list); |
| 3288 | INIT_LIST_HEAD(&tmp_list); |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3289 | |
Pavel Shilovsky | d913ed1 | 2014-07-10 11:31:48 +0400 | [diff] [blame] | 3290 | /* |
| 3291 | * Got a part of data and then reconnect has |
| 3292 | * happened -- fill the buffer and continue |
| 3293 | * reading. |
| 3294 | */ |
| 3295 | if (got_bytes && got_bytes < rdata->bytes) { |
| 3296 | rc = cifs_readdata_to_iov(rdata, to); |
| 3297 | if (rc) { |
| 3298 | kref_put(&rdata->refcount, |
| 3299 | cifs_uncached_readdata_release); |
| 3300 | continue; |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | rc = cifs_send_async_read( |
| 3305 | rdata->offset + got_bytes, |
| 3306 | rdata->bytes - got_bytes, |
| 3307 | rdata->cfile, cifs_sb, |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3308 | &tmp_list, ctx); |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3309 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3310 | list_splice(&tmp_list, &ctx->list); |
Pavel Shilovsky | 25f4025 | 2014-06-25 10:45:07 +0400 | [diff] [blame] | 3311 | |
Pavel Shilovsky | fb8a3e5 | 2014-07-10 11:50:39 +0400 | [diff] [blame] | 3312 | kref_put(&rdata->refcount, |
| 3313 | cifs_uncached_readdata_release); |
| 3314 | goto again; |
| 3315 | } else if (rdata->result) |
| 3316 | rc = rdata->result; |
| 3317 | else |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3318 | rc = cifs_readdata_to_iov(rdata, to); |
Pavel Shilovsky | fb8a3e5 | 2014-07-10 11:50:39 +0400 | [diff] [blame] | 3319 | |
Pavel Shilovsky | 2e8a05d | 2014-07-10 10:21:15 +0400 | [diff] [blame] | 3320 | /* if there was a short read -- discard anything left */ |
| 3321 | if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) |
| 3322 | rc = -ENODATA; |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3323 | } |
| 3324 | list_del_init(&rdata->list); |
| 3325 | kref_put(&rdata->refcount, cifs_uncached_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | } |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3327 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3328 | for (i = 0; i < ctx->npages; i++) { |
| 3329 | if (ctx->should_dirty) |
| 3330 | set_page_dirty(ctx->bv[i].bv_page); |
| 3331 | put_page(ctx->bv[i].bv_page); |
| 3332 | } |
Al Viro | 7f25bba | 2014-02-04 14:07:43 -0500 | [diff] [blame] | 3333 | |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3334 | ctx->total_len = ctx->len - iov_iter_count(to); |
| 3335 | |
| 3336 | cifs_stats_bytes_read(tcon, ctx->total_len); |
Jeff Layton | 1c89254 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3337 | |
Pavel Shilovsky | 09a4707 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3338 | /* mask nodata case */ |
| 3339 | if (rc == -ENODATA) |
| 3340 | rc = 0; |
| 3341 | |
Yilu Lin | 9bc0225 | 2020-03-18 11:59:19 +0800 | [diff] [blame] | 3342 | ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc; |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3343 | |
| 3344 | mutex_unlock(&ctx->aio_mutex); |
| 3345 | |
| 3346 | if (ctx->iocb && ctx->iocb->ki_complete) |
| 3347 | ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0); |
| 3348 | else |
| 3349 | complete(&ctx->done); |
| 3350 | } |
| 3351 | |
| 3352 | ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to) |
| 3353 | { |
| 3354 | struct file *file = iocb->ki_filp; |
| 3355 | ssize_t rc; |
| 3356 | size_t len; |
| 3357 | ssize_t total_read = 0; |
| 3358 | loff_t offset = iocb->ki_pos; |
| 3359 | struct cifs_sb_info *cifs_sb; |
| 3360 | struct cifs_tcon *tcon; |
| 3361 | struct cifsFileInfo *cfile; |
| 3362 | struct cifs_aio_ctx *ctx; |
| 3363 | |
| 3364 | len = iov_iter_count(to); |
| 3365 | if (!len) |
| 3366 | return 0; |
| 3367 | |
| 3368 | cifs_sb = CIFS_FILE_SB(file); |
| 3369 | cfile = file->private_data; |
| 3370 | tcon = tlink_tcon(cfile->tlink); |
| 3371 | |
| 3372 | if (!tcon->ses->server->ops->async_readv) |
| 3373 | return -ENOSYS; |
| 3374 | |
| 3375 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
| 3376 | cifs_dbg(FYI, "attempting read on write only file instance\n"); |
| 3377 | |
| 3378 | ctx = cifs_aio_ctx_alloc(); |
| 3379 | if (!ctx) |
| 3380 | return -ENOMEM; |
| 3381 | |
| 3382 | ctx->cfile = cifsFileInfo_get(cfile); |
| 3383 | |
| 3384 | if (!is_sync_kiocb(iocb)) |
| 3385 | ctx->iocb = iocb; |
| 3386 | |
Dan Carpenter | 8a7b0d8 | 2017-05-05 08:30:40 +0300 | [diff] [blame] | 3387 | if (to->type == ITER_IOVEC) |
Pavel Shilovsky | 6685c5e | 2017-04-25 11:52:30 -0700 | [diff] [blame] | 3388 | ctx->should_dirty = true; |
| 3389 | |
| 3390 | rc = setup_aio_ctx_iter(ctx, to, READ); |
| 3391 | if (rc) { |
| 3392 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 3393 | return rc; |
| 3394 | } |
| 3395 | |
| 3396 | len = ctx->len; |
| 3397 | |
| 3398 | /* grab a lock here due to read response handlers can access ctx */ |
| 3399 | mutex_lock(&ctx->aio_mutex); |
| 3400 | |
| 3401 | rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx); |
| 3402 | |
| 3403 | /* if at least one read request send succeeded, then reset rc */ |
| 3404 | if (!list_empty(&ctx->list)) |
| 3405 | rc = 0; |
| 3406 | |
| 3407 | mutex_unlock(&ctx->aio_mutex); |
| 3408 | |
| 3409 | if (rc) { |
| 3410 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 3411 | return rc; |
| 3412 | } |
| 3413 | |
| 3414 | if (!is_sync_kiocb(iocb)) { |
| 3415 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 3416 | return -EIOCBQUEUED; |
| 3417 | } |
| 3418 | |
| 3419 | rc = wait_for_completion_killable(&ctx->done); |
| 3420 | if (rc) { |
| 3421 | mutex_lock(&ctx->aio_mutex); |
| 3422 | ctx->rc = rc = -EINTR; |
| 3423 | total_read = ctx->total_len; |
| 3424 | mutex_unlock(&ctx->aio_mutex); |
| 3425 | } else { |
| 3426 | rc = ctx->rc; |
| 3427 | total_read = ctx->total_len; |
| 3428 | } |
| 3429 | |
| 3430 | kref_put(&ctx->refcount, cifs_aio_ctx_release); |
| 3431 | |
Al Viro | 0165e81 | 2014-02-04 14:19:48 -0500 | [diff] [blame] | 3432 | if (total_read) { |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3433 | iocb->ki_pos += total_read; |
Al Viro | 0165e81 | 2014-02-04 14:19:48 -0500 | [diff] [blame] | 3434 | return total_read; |
| 3435 | } |
| 3436 | return rc; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3437 | } |
| 3438 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3439 | ssize_t |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3440 | cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3441 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3442 | struct inode *inode = file_inode(iocb->ki_filp); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3443 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
| 3444 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 3445 | struct cifsFileInfo *cfile = (struct cifsFileInfo *) |
| 3446 | iocb->ki_filp->private_data; |
| 3447 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
| 3448 | int rc = -EACCES; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3449 | |
| 3450 | /* |
| 3451 | * In strict cache mode we need to read from the server all the time |
| 3452 | * if we don't have level II oplock because the server can delay mtime |
| 3453 | * change - so we can't make a decision about inode invalidating. |
| 3454 | * And we can also fail with pagereading if there are mandatory locks |
| 3455 | * on pages affected by this read but not on the region from pos to |
| 3456 | * pos+len-1. |
| 3457 | */ |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 3458 | if (!CIFS_CACHE_READ(cinode)) |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3459 | return cifs_user_readv(iocb, to); |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3460 | |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3461 | if (cap_unix(tcon->ses) && |
| 3462 | (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && |
| 3463 | ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3464 | return generic_file_read_iter(iocb, to); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3465 | |
| 3466 | /* |
| 3467 | * We need to hold the sem to be sure nobody modifies lock list |
| 3468 | * with a brlock that prevents reading. |
| 3469 | */ |
| 3470 | down_read(&cinode->lock_sem); |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3471 | if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3472 | tcon->ses->server->vals->shared_lock_type, |
Pavel Shilovsky | 081c041 | 2012-11-27 18:38:53 +0400 | [diff] [blame] | 3473 | NULL, CIFS_READ_OP)) |
Al Viro | e6a7bcb | 2014-04-02 19:53:36 -0400 | [diff] [blame] | 3474 | rc = generic_file_read_iter(iocb, to); |
Pavel Shilovsky | 579f905 | 2012-09-19 06:22:44 -0700 | [diff] [blame] | 3475 | up_read(&cinode->lock_sem); |
| 3476 | return rc; |
Pavel Shilovsky | a70307e | 2010-12-14 11:50:41 +0300 | [diff] [blame] | 3477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3479 | static ssize_t |
| 3480 | cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | { |
| 3482 | int rc = -EACCES; |
| 3483 | unsigned int bytes_read = 0; |
| 3484 | unsigned int total_read; |
| 3485 | unsigned int current_read_size; |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 3486 | unsigned int rsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | struct cifs_sb_info *cifs_sb; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3488 | struct cifs_tcon *tcon; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3489 | struct TCP_Server_Info *server; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3490 | unsigned int xid; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3491 | char *cur_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | struct cifsFileInfo *open_file; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3493 | struct cifs_io_parms io_parms; |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 3494 | int buf_type = CIFS_NO_BUFFER; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3495 | __u32 pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3497 | xid = get_xid(); |
Al Viro | 7119e22 | 2014-10-22 00:25:12 -0400 | [diff] [blame] | 3498 | cifs_sb = CIFS_FILE_SB(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 3500 | /* FIXME: set up handlers for larger reads and/or convert to async */ |
| 3501 | rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize); |
| 3502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | if (file->private_data == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 3504 | rc = -EBADF; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3505 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 3506 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | } |
Joe Perches | c21dfb6 | 2010-07-12 13:50:14 -0700 | [diff] [blame] | 3508 | open_file = file->private_data; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3509 | tcon = tlink_tcon(open_file->tlink); |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3510 | server = tcon->ses->server; |
| 3511 | |
| 3512 | if (!server->ops->sync_read) { |
| 3513 | free_xid(xid); |
| 3514 | return -ENOSYS; |
| 3515 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3517 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 3518 | pid = open_file->pid; |
| 3519 | else |
| 3520 | pid = current->tgid; |
| 3521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3523 | cifs_dbg(FYI, "attempting read on write only file instance\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3525 | for (total_read = 0, cur_offset = read_data; read_size > total_read; |
| 3526 | total_read += bytes_read, cur_offset += bytes_read) { |
Pavel Shilovsky | e374d90 | 2014-06-25 16:19:02 +0400 | [diff] [blame] | 3527 | do { |
| 3528 | current_read_size = min_t(uint, read_size - total_read, |
| 3529 | rsize); |
| 3530 | /* |
| 3531 | * For windows me and 9x we do not want to request more |
| 3532 | * than it negotiated since it will refuse the read |
| 3533 | * then. |
| 3534 | */ |
| 3535 | if ((tcon->ses) && !(tcon->ses->capabilities & |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3536 | tcon->ses->server->vals->cap_large_files)) { |
Pavel Shilovsky | e374d90 | 2014-06-25 16:19:02 +0400 | [diff] [blame] | 3537 | current_read_size = min_t(uint, |
| 3538 | current_read_size, CIFSMaxBufSize); |
| 3539 | } |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 3540 | if (open_file->invalidHandle) { |
Jeff Layton | 1588617 | 2010-10-15 15:33:59 -0400 | [diff] [blame] | 3541 | rc = cifs_reopen_file(open_file, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | if (rc != 0) |
| 3543 | break; |
| 3544 | } |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3545 | io_parms.pid = pid; |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3546 | io_parms.tcon = tcon; |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3547 | io_parms.offset = *offset; |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3548 | io_parms.length = current_read_size; |
Steve French | db8b631 | 2014-09-22 05:13:55 -0500 | [diff] [blame] | 3549 | rc = server->ops->sync_read(xid, &open_file->fid, &io_parms, |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3550 | &bytes_read, &cur_offset, |
| 3551 | &buf_type); |
Pavel Shilovsky | e374d90 | 2014-06-25 16:19:02 +0400 | [diff] [blame] | 3552 | } while (rc == -EAGAIN); |
| 3553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | if (rc || (bytes_read == 0)) { |
| 3555 | if (total_read) { |
| 3556 | break; |
| 3557 | } else { |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3558 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | return rc; |
| 3560 | } |
| 3561 | } else { |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 3562 | cifs_stats_bytes_read(tcon, total_read); |
Pavel Shilovsky | f9c6e23 | 2012-09-18 16:20:29 -0700 | [diff] [blame] | 3563 | *offset += bytes_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3564 | } |
| 3565 | } |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3566 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | return total_read; |
| 3568 | } |
| 3569 | |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3570 | /* |
| 3571 | * If the page is mmap'ed into a process' page tables, then we need to make |
| 3572 | * sure that it doesn't change while being written back. |
| 3573 | */ |
Souptick Joarder | a5240cb | 2018-04-15 00:58:25 +0530 | [diff] [blame] | 3574 | static vm_fault_t |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 3575 | cifs_page_mkwrite(struct vm_fault *vmf) |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3576 | { |
| 3577 | struct page *page = vmf->page; |
| 3578 | |
| 3579 | lock_page(page); |
| 3580 | return VM_FAULT_LOCKED; |
| 3581 | } |
| 3582 | |
Kirill A. Shutemov | 7cbea8d | 2015-09-09 15:39:26 -0700 | [diff] [blame] | 3583 | static const struct vm_operations_struct cifs_file_vm_ops = { |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3584 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 3585 | .map_pages = filemap_map_pages, |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3586 | .page_mkwrite = cifs_page_mkwrite, |
| 3587 | }; |
| 3588 | |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3589 | int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) |
| 3590 | { |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3591 | int xid, rc = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3592 | struct inode *inode = file_inode(file); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3593 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3594 | xid = get_xid(); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3595 | |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3596 | if (!CIFS_CACHE_READ(CIFS_I(inode))) |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 3597 | rc = cifs_zap_mapping(inode); |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3598 | if (!rc) |
| 3599 | rc = generic_file_mmap(file, vma); |
| 3600 | if (!rc) |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3601 | vma->vm_ops = &cifs_file_vm_ops; |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3602 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3603 | free_xid(xid); |
Pavel Shilovsky | 7a6a19b | 2010-12-14 11:29:51 +0300 | [diff] [blame] | 3604 | return rc; |
| 3605 | } |
| 3606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 3608 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | int rc, xid; |
| 3610 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3611 | xid = get_xid(); |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3612 | |
Jeff Layton | abab095 | 2010-02-12 07:44:18 -0500 | [diff] [blame] | 3613 | rc = cifs_revalidate_file(file); |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3614 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3615 | cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", |
| 3616 | rc); |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3617 | if (!rc) |
| 3618 | rc = generic_file_mmap(file, vma); |
| 3619 | if (!rc) |
Jeff Layton | ca83ce3 | 2011-04-12 09:13:44 -0400 | [diff] [blame] | 3620 | vma->vm_ops = &cifs_file_vm_ops; |
Matthew Wilcox | f04a703 | 2017-12-15 12:48:32 -0800 | [diff] [blame] | 3621 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 3622 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | return rc; |
| 3624 | } |
| 3625 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3626 | static void |
| 3627 | cifs_readv_complete(struct work_struct *work) |
| 3628 | { |
Pavel Shilovsky | b770ddf | 2014-07-10 11:31:53 +0400 | [diff] [blame] | 3629 | unsigned int i, got_bytes; |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3630 | struct cifs_readdata *rdata = container_of(work, |
| 3631 | struct cifs_readdata, work); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3632 | |
Pavel Shilovsky | b770ddf | 2014-07-10 11:31:53 +0400 | [diff] [blame] | 3633 | got_bytes = rdata->got_bytes; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3634 | for (i = 0; i < rdata->nr_pages; i++) { |
| 3635 | struct page *page = rdata->pages[i]; |
| 3636 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3637 | lru_cache_add_file(page); |
| 3638 | |
Pavel Shilovsky | b770ddf | 2014-07-10 11:31:53 +0400 | [diff] [blame] | 3639 | if (rdata->result == 0 || |
| 3640 | (rdata->result == -EAGAIN && got_bytes)) { |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3641 | flush_dcache_page(page); |
| 3642 | SetPageUptodate(page); |
| 3643 | } |
| 3644 | |
| 3645 | unlock_page(page); |
| 3646 | |
Pavel Shilovsky | b770ddf | 2014-07-10 11:31:53 +0400 | [diff] [blame] | 3647 | if (rdata->result == 0 || |
| 3648 | (rdata->result == -EAGAIN && got_bytes)) |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3649 | cifs_readpage_to_fscache(rdata->mapping->host, page); |
| 3650 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3651 | got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes); |
Pavel Shilovsky | b770ddf | 2014-07-10 11:31:53 +0400 | [diff] [blame] | 3652 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3653 | put_page(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3654 | rdata->pages[i] = NULL; |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3655 | } |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3656 | kref_put(&rdata->refcount, cifs_readdata_release); |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3657 | } |
| 3658 | |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3659 | static int |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3660 | readpages_fill_pages(struct TCP_Server_Info *server, |
| 3661 | struct cifs_readdata *rdata, struct iov_iter *iter, |
| 3662 | unsigned int len) |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3663 | { |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3664 | int result = 0; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3665 | unsigned int i; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3666 | u64 eof; |
| 3667 | pgoff_t eof_index; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3668 | unsigned int nr_pages = rdata->nr_pages; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3669 | unsigned int page_offset = rdata->page_offset; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3670 | |
| 3671 | /* determine the eof that the server (probably) has */ |
| 3672 | eof = CIFS_I(rdata->mapping->host)->server_eof; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3673 | eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3674 | cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index); |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3675 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3676 | rdata->got_bytes = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3677 | rdata->tailsz = PAGE_SIZE; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3678 | for (i = 0; i < nr_pages; i++) { |
| 3679 | struct page *page = rdata->pages[i]; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3680 | unsigned int to_read = rdata->pagesz; |
| 3681 | size_t n; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3682 | |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3683 | if (i == 0) |
| 3684 | to_read -= page_offset; |
| 3685 | else |
| 3686 | page_offset = 0; |
| 3687 | |
| 3688 | n = to_read; |
| 3689 | |
| 3690 | if (len >= to_read) { |
| 3691 | len -= to_read; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3692 | } else if (len > 0) { |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3693 | /* enough for partial page, fill and zero the rest */ |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3694 | zero_user(page, len + page_offset, to_read - len); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 3695 | n = rdata->tailsz = len; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3696 | len = 0; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3697 | } else if (page->index > eof_index) { |
| 3698 | /* |
| 3699 | * The VFS will not try to do readahead past the |
| 3700 | * i_size, but it's possible that we have outstanding |
| 3701 | * writes with gaps in the middle and the i_size hasn't |
| 3702 | * caught up yet. Populate those with zeroed out pages |
| 3703 | * to prevent the VFS from repeatedly attempting to |
| 3704 | * fill them until the writes are flushed. |
| 3705 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3706 | zero_user(page, 0, PAGE_SIZE); |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3707 | lru_cache_add_file(page); |
| 3708 | flush_dcache_page(page); |
| 3709 | SetPageUptodate(page); |
| 3710 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3711 | put_page(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3712 | rdata->pages[i] = NULL; |
| 3713 | rdata->nr_pages--; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3714 | continue; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3715 | } else { |
| 3716 | /* no need to hold page hostage */ |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3717 | lru_cache_add_file(page); |
| 3718 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3719 | put_page(page); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3720 | rdata->pages[i] = NULL; |
| 3721 | rdata->nr_pages--; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3722 | continue; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3723 | } |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3724 | |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3725 | if (iter) |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3726 | result = copy_page_from_iter( |
| 3727 | page, page_offset, n, iter); |
Long Li | bd3dcc6 | 2017-11-22 17:38:47 -0700 | [diff] [blame] | 3728 | #ifdef CONFIG_CIFS_SMB_DIRECT |
| 3729 | else if (rdata->mr) |
| 3730 | result = n; |
| 3731 | #endif |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3732 | else |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3733 | result = cifs_read_page_from_socket( |
| 3734 | server, page, page_offset, n); |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3735 | if (result < 0) |
| 3736 | break; |
| 3737 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3738 | rdata->got_bytes += result; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3739 | } |
| 3740 | |
Pavel Shilovsky | b3160ae | 2014-07-10 10:16:25 +0400 | [diff] [blame] | 3741 | return rdata->got_bytes > 0 && result != -ECONNABORTED ? |
| 3742 | rdata->got_bytes : result; |
Jeff Layton | 8d5ce4d | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3743 | } |
| 3744 | |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3745 | static int |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3746 | cifs_readpages_read_into_pages(struct TCP_Server_Info *server, |
| 3747 | struct cifs_readdata *rdata, unsigned int len) |
| 3748 | { |
| 3749 | return readpages_fill_pages(server, rdata, NULL, len); |
| 3750 | } |
| 3751 | |
| 3752 | static int |
| 3753 | cifs_readpages_copy_into_pages(struct TCP_Server_Info *server, |
| 3754 | struct cifs_readdata *rdata, |
| 3755 | struct iov_iter *iter) |
| 3756 | { |
| 3757 | return readpages_fill_pages(server, rdata, iter, iter->count); |
| 3758 | } |
| 3759 | |
| 3760 | static int |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3761 | readpages_get_pages(struct address_space *mapping, struct list_head *page_list, |
| 3762 | unsigned int rsize, struct list_head *tmplist, |
| 3763 | unsigned int *nr_pages, loff_t *offset, unsigned int *bytes) |
| 3764 | { |
| 3765 | struct page *page, *tpage; |
| 3766 | unsigned int expected_index; |
| 3767 | int rc; |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 3768 | gfp_t gfp = readahead_gfp_mask(mapping); |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3769 | |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3770 | INIT_LIST_HEAD(tmplist); |
| 3771 | |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3772 | page = list_entry(page_list->prev, struct page, lru); |
| 3773 | |
| 3774 | /* |
| 3775 | * Lock the page and put it in the cache. Since no one else |
| 3776 | * should have access to this page, we're safe to simply set |
| 3777 | * PG_locked without checking it first. |
| 3778 | */ |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 3779 | __SetPageLocked(page); |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3780 | rc = add_to_page_cache_locked(page, mapping, |
Michal Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 3781 | page->index, gfp); |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3782 | |
| 3783 | /* give up if we can't stick it in the cache */ |
| 3784 | if (rc) { |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 3785 | __ClearPageLocked(page); |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3786 | return rc; |
| 3787 | } |
| 3788 | |
| 3789 | /* move first page to the tmplist */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3790 | *offset = (loff_t)page->index << PAGE_SHIFT; |
| 3791 | *bytes = PAGE_SIZE; |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3792 | *nr_pages = 1; |
| 3793 | list_move_tail(&page->lru, tmplist); |
| 3794 | |
| 3795 | /* now try and add more pages onto the request */ |
| 3796 | expected_index = page->index + 1; |
| 3797 | list_for_each_entry_safe_reverse(page, tpage, page_list, lru) { |
| 3798 | /* discontinuity ? */ |
| 3799 | if (page->index != expected_index) |
| 3800 | break; |
| 3801 | |
| 3802 | /* would this page push the read over the rsize? */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3803 | if (*bytes + PAGE_SIZE > rsize) |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3804 | break; |
| 3805 | |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 3806 | __SetPageLocked(page); |
Michal Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 3807 | if (add_to_page_cache_locked(page, mapping, page->index, gfp)) { |
Kirill A. Shutemov | 48c935a | 2016-01-15 16:51:24 -0800 | [diff] [blame] | 3808 | __ClearPageLocked(page); |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3809 | break; |
| 3810 | } |
| 3811 | list_move_tail(&page->lru, tmplist); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3812 | (*bytes) += PAGE_SIZE; |
Pavel Shilovsky | 387eb92 | 2014-06-24 13:08:54 +0400 | [diff] [blame] | 3813 | expected_index++; |
| 3814 | (*nr_pages)++; |
| 3815 | } |
| 3816 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3817 | } |
| 3818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | static int cifs_readpages(struct file *file, struct address_space *mapping, |
| 3820 | struct list_head *page_list, unsigned num_pages) |
| 3821 | { |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3822 | int rc; |
| 3823 | struct list_head tmplist; |
| 3824 | struct cifsFileInfo *open_file = file->private_data; |
Al Viro | 7119e22 | 2014-10-22 00:25:12 -0400 | [diff] [blame] | 3825 | struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3826 | struct TCP_Server_Info *server; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3827 | pid_t pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3829 | /* |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3830 | * Reads as many pages as possible from fscache. Returns -ENOBUFS |
| 3831 | * immediately if the cookie is negative |
David Howells | 54afa99 | 2013-09-04 17:10:39 +0000 | [diff] [blame] | 3832 | * |
| 3833 | * After this point, every page in the list might have PG_fscache set, |
| 3834 | * so we will need to clean that up off of every page we don't use. |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3835 | */ |
| 3836 | rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list, |
| 3837 | &num_pages); |
| 3838 | if (rc == 0) |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3839 | return rc; |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3840 | |
Pavel Shilovsky | d4ffff1 | 2011-05-26 06:02:00 +0000 | [diff] [blame] | 3841 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) |
| 3842 | pid = open_file->pid; |
| 3843 | else |
| 3844 | pid = current->tgid; |
| 3845 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3846 | rc = 0; |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3847 | server = tlink_tcon(open_file->tlink)->ses->server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3849 | cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n", |
| 3850 | __func__, file, mapping, num_pages); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3851 | |
| 3852 | /* |
| 3853 | * Start with the page at end of list and move it to private |
| 3854 | * list. Do the same with any following pages until we hit |
| 3855 | * the rsize limit, hit an index discontinuity, or run out of |
| 3856 | * pages. Issue the async read and then start the loop again |
| 3857 | * until the list is empty. |
| 3858 | * |
| 3859 | * Note that list order is important. The page_list is in |
| 3860 | * the order of declining indexes. When we put the pages in |
| 3861 | * the rdata->pages, then we want them in increasing order. |
| 3862 | */ |
| 3863 | while (!list_empty(page_list)) { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3864 | unsigned int i, nr_pages, bytes, rsize; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3865 | loff_t offset; |
| 3866 | struct page *page, *tpage; |
| 3867 | struct cifs_readdata *rdata; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3868 | unsigned credits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3869 | |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3870 | rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize, |
| 3871 | &rsize, &credits); |
| 3872 | if (rc) |
| 3873 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3875 | /* |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3876 | * Give up immediately if rsize is too small to read an entire |
| 3877 | * page. The VFS will fall back to readpage. We should never |
| 3878 | * reach this point however since we set ra_pages to 0 when the |
| 3879 | * rsize is smaller than a cache page. |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3880 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3881 | if (unlikely(rsize < PAGE_SIZE)) { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3882 | add_credits_and_wake_if(server, credits, 0); |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3883 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3886 | rc = readpages_get_pages(mapping, page_list, rsize, &tmplist, |
| 3887 | &nr_pages, &offset, &bytes); |
| 3888 | if (rc) { |
| 3889 | add_credits_and_wake_if(server, credits, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | break; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3891 | } |
| 3892 | |
Jeff Layton | 0471ca3 | 2012-05-16 07:13:16 -0400 | [diff] [blame] | 3893 | rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3894 | if (!rdata) { |
| 3895 | /* best to give up if we're out of mem */ |
| 3896 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { |
| 3897 | list_del(&page->lru); |
| 3898 | lru_cache_add_file(page); |
| 3899 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3900 | put_page(page); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3901 | } |
| 3902 | rc = -ENOMEM; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3903 | add_credits_and_wake_if(server, credits, 0); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3904 | break; |
| 3905 | } |
| 3906 | |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3907 | rdata->cfile = cifsFileInfo_get(open_file); |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3908 | rdata->mapping = mapping; |
| 3909 | rdata->offset = offset; |
| 3910 | rdata->bytes = bytes; |
| 3911 | rdata->pid = pid; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3912 | rdata->pagesz = PAGE_SIZE; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 3913 | rdata->tailsz = PAGE_SIZE; |
Jeff Layton | 8321fec | 2012-09-19 06:22:32 -0700 | [diff] [blame] | 3914 | rdata->read_into_pages = cifs_readpages_read_into_pages; |
Pavel Shilovsky | d70b910 | 2016-11-17 16:20:18 -0800 | [diff] [blame] | 3915 | rdata->copy_into_pages = cifs_readpages_copy_into_pages; |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3916 | rdata->credits = credits; |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3917 | |
| 3918 | list_for_each_entry_safe(page, tpage, &tmplist, lru) { |
| 3919 | list_del(&page->lru); |
| 3920 | rdata->pages[rdata->nr_pages++] = page; |
| 3921 | } |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3922 | |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3923 | if (!rdata->cfile->invalidHandle || |
Germano Percossi | 1fa839b | 2017-04-07 12:29:38 +0100 | [diff] [blame] | 3924 | !(rc = cifs_reopen_file(rdata->cfile, true))) |
Pavel Shilovsky | 69cebd7 | 2014-06-24 13:42:03 +0400 | [diff] [blame] | 3925 | rc = server->ops->async_readv(rdata); |
| 3926 | if (rc) { |
Pavel Shilovsky | bed9da0 | 2014-06-25 11:28:57 +0400 | [diff] [blame] | 3927 | add_credits_and_wake_if(server, rdata->credits, 0); |
Jeff Layton | c5fab6f | 2012-09-19 06:22:30 -0700 | [diff] [blame] | 3928 | for (i = 0; i < rdata->nr_pages; i++) { |
| 3929 | page = rdata->pages[i]; |
Jeff Layton | 690c5e3 | 2011-10-19 15:30:16 -0400 | [diff] [blame] | 3930 | lru_cache_add_file(page); |
| 3931 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3932 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | } |
Pavel Shilovsky | 1209bbd | 2014-10-02 20:13:35 +0400 | [diff] [blame] | 3934 | /* Fallback to the readpage in error/reconnect cases */ |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3935 | kref_put(&rdata->refcount, cifs_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | break; |
| 3937 | } |
Jeff Layton | 6993f74 | 2012-05-16 07:13:17 -0400 | [diff] [blame] | 3938 | |
| 3939 | kref_put(&rdata->refcount, cifs_readdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | } |
| 3941 | |
David Howells | 54afa99 | 2013-09-04 17:10:39 +0000 | [diff] [blame] | 3942 | /* Any pages that have been shown to fscache but didn't get added to |
| 3943 | * the pagecache must be uncached before they get returned to the |
| 3944 | * allocator. |
| 3945 | */ |
| 3946 | cifs_fscache_readpages_cancel(mapping->host, page_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | return rc; |
| 3948 | } |
| 3949 | |
Sachin Prabhu | a9e9b7b | 2013-09-13 14:11:56 +0100 | [diff] [blame] | 3950 | /* |
| 3951 | * cifs_readpage_worker must be called with the page pinned |
| 3952 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | static int cifs_readpage_worker(struct file *file, struct page *page, |
| 3954 | loff_t *poffset) |
| 3955 | { |
| 3956 | char *read_data; |
| 3957 | int rc; |
| 3958 | |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3959 | /* Is the page cached? */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3960 | rc = cifs_readpage_from_fscache(file_inode(file), page); |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3961 | if (rc == 0) |
| 3962 | goto read_complete; |
| 3963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | read_data = kmap(page); |
| 3965 | /* for reads over a certain size could initiate async read ahead */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3966 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3967 | rc = cifs_read(file, read_data, PAGE_SIZE, poffset); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | if (rc < 0) |
| 3970 | goto io_error; |
| 3971 | else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3972 | cifs_dbg(FYI, "Bytes read %d\n", rc); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3973 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3974 | file_inode(file)->i_atime = |
Deepa Dinamani | c2050a4 | 2016-09-14 07:48:06 -0700 | [diff] [blame] | 3975 | current_time(file_inode(file)); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3976 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3977 | if (PAGE_SIZE > rc) |
| 3978 | memset(read_data + rc, 0, PAGE_SIZE - rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | |
| 3980 | flush_dcache_page(page); |
| 3981 | SetPageUptodate(page); |
Suresh Jayaraman | 9dc0655 | 2010-07-05 18:13:11 +0530 | [diff] [blame] | 3982 | |
| 3983 | /* send this page to the cache */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3984 | cifs_readpage_to_fscache(file_inode(file), page); |
Suresh Jayaraman | 9dc0655 | 2010-07-05 18:13:11 +0530 | [diff] [blame] | 3985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | rc = 0; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | io_error: |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3989 | kunmap(page); |
Sachin Prabhu | 466bd31 | 2013-09-13 14:11:57 +0100 | [diff] [blame] | 3990 | unlock_page(page); |
Suresh Jayaraman | 5669823 | 2010-07-05 18:13:25 +0530 | [diff] [blame] | 3991 | |
| 3992 | read_complete: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | return rc; |
| 3994 | } |
| 3995 | |
| 3996 | static int cifs_readpage(struct file *file, struct page *page) |
| 3997 | { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3998 | loff_t offset = (loff_t)page->index << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | int rc = -EACCES; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4000 | unsigned int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4002 | xid = get_xid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4003 | |
| 4004 | if (file->private_data == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 4005 | rc = -EBADF; |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4006 | free_xid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 4007 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | } |
| 4009 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4010 | cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n", |
Joe Perches | b6b38f7 | 2010-04-21 03:50:45 +0000 | [diff] [blame] | 4011 | page, (int)offset, (int)offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
| 4013 | rc = cifs_readpage_worker(file, page, &offset); |
| 4014 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 4015 | free_xid(xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | return rc; |
| 4017 | } |
| 4018 | |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4019 | static int is_inode_writable(struct cifsInodeInfo *cifs_inode) |
| 4020 | { |
| 4021 | struct cifsFileInfo *open_file; |
| 4022 | |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 4023 | spin_lock(&cifs_inode->open_file_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4024 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
Jeff Layton | 2e396b8 | 2010-10-15 15:34:01 -0400 | [diff] [blame] | 4025 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 4026 | spin_unlock(&cifs_inode->open_file_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4027 | return 1; |
| 4028 | } |
| 4029 | } |
Dave Wysochanski | a8de709 | 2019-10-03 15:16:27 +1000 | [diff] [blame] | 4030 | spin_unlock(&cifs_inode->open_file_lock); |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4031 | return 0; |
| 4032 | } |
| 4033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | /* We do not want to update the file size from server for inodes |
| 4035 | open for write - to avoid races with writepage extending |
| 4036 | the file - in the future we could consider allowing |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 4037 | refreshing the inode only on increases in the file size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | but this is tricky to do without racing with writebehind |
| 4039 | page caching in the current Linux kernel design */ |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4040 | bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | { |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4042 | if (!cifsInode) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4043 | return true; |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 4044 | |
Steve French | a403a0a | 2007-07-26 15:54:16 +0000 | [diff] [blame] | 4045 | if (is_inode_writable(cifsInode)) { |
| 4046 | /* This inode is open for write at least once */ |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 4047 | struct cifs_sb_info *cifs_sb; |
| 4048 | |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 4049 | cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb); |
Steve French | ad7a292 | 2008-02-07 23:25:02 +0000 | [diff] [blame] | 4050 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 4051 | /* since no page cache to corrupt on directio |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 4052 | we can change size safely */ |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4053 | return true; |
Steve French | c32a0b6 | 2006-01-12 14:41:28 -0800 | [diff] [blame] | 4054 | } |
| 4055 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 4056 | if (i_size_read(&cifsInode->vfs_inode) < end_of_file) |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4057 | return true; |
Steve French | 7ba5263 | 2007-02-08 18:14:13 +0000 | [diff] [blame] | 4058 | |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4059 | return false; |
Steve French | 23e7dd7 | 2005-10-20 13:44:56 -0700 | [diff] [blame] | 4060 | } else |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 4061 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | } |
| 4063 | |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4064 | static int cifs_write_begin(struct file *file, struct address_space *mapping, |
| 4065 | loff_t pos, unsigned len, unsigned flags, |
| 4066 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | { |
Sachin Prabhu | 466bd31 | 2013-09-13 14:11:57 +0100 | [diff] [blame] | 4068 | int oncethru = 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4069 | pgoff_t index = pos >> PAGE_SHIFT; |
| 4070 | loff_t offset = pos & (PAGE_SIZE - 1); |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4071 | loff_t page_start = pos & PAGE_MASK; |
| 4072 | loff_t i_size; |
| 4073 | struct page *page; |
| 4074 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4076 | cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len); |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4077 | |
Sachin Prabhu | 466bd31 | 2013-09-13 14:11:57 +0100 | [diff] [blame] | 4078 | start: |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4079 | page = grab_cache_page_write_begin(mapping, index, flags); |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4080 | if (!page) { |
| 4081 | rc = -ENOMEM; |
| 4082 | goto out; |
| 4083 | } |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4084 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4085 | if (PageUptodate(page)) |
| 4086 | goto out; |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 4087 | |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4088 | /* |
| 4089 | * If we write a full page it will be up to date, no need to read from |
| 4090 | * the server. If the write is short, we'll end up doing a sync write |
| 4091 | * instead. |
| 4092 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4093 | if (len == PAGE_SIZE) |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4094 | goto out; |
| 4095 | |
| 4096 | /* |
| 4097 | * optimize away the read when we have an oplock, and we're not |
| 4098 | * expecting to use any of the data we'd be reading in. That |
| 4099 | * is, when the page lies beyond the EOF, or straddles the EOF |
| 4100 | * and the write will cover all of the existing data. |
| 4101 | */ |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 4102 | if (CIFS_CACHE_READ(CIFS_I(mapping->host))) { |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4103 | i_size = i_size_read(mapping->host); |
| 4104 | if (page_start >= i_size || |
| 4105 | (offset == 0 && (pos + len) >= i_size)) { |
| 4106 | zero_user_segments(page, 0, offset, |
| 4107 | offset + len, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4108 | PAGE_SIZE); |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4109 | /* |
| 4110 | * PageChecked means that the parts of the page |
| 4111 | * to which we're not writing are considered up |
| 4112 | * to date. Once the data is copied to the |
| 4113 | * page, it can be set uptodate. |
| 4114 | */ |
| 4115 | SetPageChecked(page); |
| 4116 | goto out; |
| 4117 | } |
| 4118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | |
Sachin Prabhu | 466bd31 | 2013-09-13 14:11:57 +0100 | [diff] [blame] | 4120 | if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) { |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4121 | /* |
| 4122 | * might as well read a page, it is fast enough. If we get |
| 4123 | * an error, we don't need to return it. cifs_write_end will |
| 4124 | * do a sync write instead since PG_uptodate isn't set. |
| 4125 | */ |
| 4126 | cifs_readpage_worker(file, page, &page_start); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4127 | put_page(page); |
Sachin Prabhu | 466bd31 | 2013-09-13 14:11:57 +0100 | [diff] [blame] | 4128 | oncethru = 1; |
| 4129 | goto start; |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 4130 | } else { |
| 4131 | /* we could try using another file handle if there is one - |
| 4132 | but how would we lock it to prevent close of that handle |
| 4133 | racing with this read? In any case |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4134 | this will be written out by write_end so is fine */ |
Steve French | 8a23626 | 2007-03-06 00:31:00 +0000 | [diff] [blame] | 4135 | } |
Jeff Layton | a98ee8c | 2008-11-26 19:32:33 +0000 | [diff] [blame] | 4136 | out: |
| 4137 | *pagep = page; |
| 4138 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4141 | static int cifs_release_page(struct page *page, gfp_t gfp) |
| 4142 | { |
| 4143 | if (PagePrivate(page)) |
| 4144 | return 0; |
| 4145 | |
| 4146 | return cifs_fscache_release_page(page, gfp); |
| 4147 | } |
| 4148 | |
Lukas Czerner | d47992f | 2013-05-21 23:17:23 -0400 | [diff] [blame] | 4149 | static void cifs_invalidate_page(struct page *page, unsigned int offset, |
| 4150 | unsigned int length) |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4151 | { |
| 4152 | struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host); |
| 4153 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4154 | if (offset == 0 && length == PAGE_SIZE) |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4155 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); |
| 4156 | } |
| 4157 | |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 4158 | static int cifs_launder_page(struct page *page) |
| 4159 | { |
| 4160 | int rc = 0; |
| 4161 | loff_t range_start = page_offset(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 4162 | loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 4163 | struct writeback_control wbc = { |
| 4164 | .sync_mode = WB_SYNC_ALL, |
| 4165 | .nr_to_write = 0, |
| 4166 | .range_start = range_start, |
| 4167 | .range_end = range_end, |
| 4168 | }; |
| 4169 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4170 | cifs_dbg(FYI, "Launder page: %p\n", page); |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 4171 | |
| 4172 | if (clear_page_dirty_for_io(page)) |
| 4173 | rc = cifs_writepage_locked(page, &wbc); |
| 4174 | |
| 4175 | cifs_fscache_invalidate_page(page, page->mapping->host); |
| 4176 | return rc; |
| 4177 | } |
| 4178 | |
Tejun Heo | 9b64697 | 2010-07-20 22:09:02 +0200 | [diff] [blame] | 4179 | void cifs_oplock_break(struct work_struct *work) |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4180 | { |
| 4181 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, |
| 4182 | oplock_break); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 4183 | struct inode *inode = d_inode(cfile->dentry); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4184 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
Pavel Shilovsky | 95a3f2f | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4185 | struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 4186 | struct TCP_Server_Info *server = tcon->ses->server; |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 4187 | int rc = 0; |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4188 | |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 4189 | wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 4190 | TASK_UNINTERRUPTIBLE); |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 4191 | |
| 4192 | server->ops->downgrade_oplock(server, cinode, |
| 4193 | test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags)); |
| 4194 | |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 4195 | if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 4196 | cifs_has_mand_locks(cinode)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4197 | cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", |
| 4198 | inode); |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 4199 | cinode->oplock = 0; |
Pavel Shilovsky | 63b7d3a | 2012-12-24 14:41:19 +0400 | [diff] [blame] | 4200 | } |
| 4201 | |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4202 | if (inode && S_ISREG(inode->i_mode)) { |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 4203 | if (CIFS_CACHE_READ(cinode)) |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 4204 | break_lease(inode, O_RDONLY); |
Steve French | d54ff73 | 2010-04-27 04:38:15 +0000 | [diff] [blame] | 4205 | else |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 4206 | break_lease(inode, O_WRONLY); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4207 | rc = filemap_fdatawrite(inode->i_mapping); |
Pavel Shilovsky | 18cceb6 | 2013-09-05 13:01:06 +0400 | [diff] [blame] | 4208 | if (!CIFS_CACHE_READ(cinode)) { |
Jeff Layton | eb4b756 | 2010-10-22 14:52:29 -0400 | [diff] [blame] | 4209 | rc = filemap_fdatawait(inode->i_mapping); |
| 4210 | mapping_set_error(inode->i_mapping, rc); |
Jeff Layton | 4f73c7d | 2014-04-30 09:31:47 -0400 | [diff] [blame] | 4211 | cifs_zap_mapping(inode); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4212 | } |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4213 | cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4214 | } |
| 4215 | |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 4216 | rc = cifs_push_locks(cfile); |
| 4217 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4218 | cifs_dbg(VFS, "Push locks rc = %d\n", rc); |
Pavel Shilovsky | 85160e0 | 2011-10-22 15:33:29 +0400 | [diff] [blame] | 4219 | |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4220 | /* |
| 4221 | * releasing stale oplock after recent reconnect of smb session using |
| 4222 | * a now incorrect file handle is not a data integrity issue but do |
| 4223 | * not bother sending an oplock release if session to server still is |
| 4224 | * disconnected since oplock already released by the server |
| 4225 | */ |
Steve French | cdff08e | 2010-10-21 22:46:14 +0000 | [diff] [blame] | 4226 | if (!cfile->oplock_break_cancelled) { |
Pavel Shilovsky | 95a3f2f | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 4227 | rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid, |
| 4228 | cinode); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 4229 | cifs_dbg(FYI, "Oplock release rc = %d\n", rc); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4230 | } |
Aurelien Aptel | 8092ecc | 2019-03-29 10:49:12 +0100 | [diff] [blame] | 4231 | _cifsFileInfo_put(cfile, false /* do not wait for ourself */); |
Sachin Prabhu | c11f1df | 2014-03-11 16:11:47 +0000 | [diff] [blame] | 4232 | cifs_done_oplock_break(cinode); |
Jeff Layton | 3bc303c | 2009-09-21 06:47:50 -0400 | [diff] [blame] | 4233 | } |
| 4234 | |
Steve French | dca6928 | 2013-11-11 16:42:37 -0600 | [diff] [blame] | 4235 | /* |
| 4236 | * The presence of cifs_direct_io() in the address space ops vector |
| 4237 | * allowes open() O_DIRECT flags which would have failed otherwise. |
| 4238 | * |
| 4239 | * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests |
| 4240 | * so this method should never be called. |
| 4241 | * |
| 4242 | * Direct IO is not yet supported in the cached mode. |
| 4243 | */ |
| 4244 | static ssize_t |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 4245 | cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter) |
Steve French | dca6928 | 2013-11-11 16:42:37 -0600 | [diff] [blame] | 4246 | { |
| 4247 | /* |
| 4248 | * FIXME |
| 4249 | * Eventually need to support direct IO for non forcedirectio mounts |
| 4250 | */ |
| 4251 | return -EINVAL; |
| 4252 | } |
| 4253 | |
| 4254 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 4255 | const struct address_space_operations cifs_addr_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | .readpage = cifs_readpage, |
| 4257 | .readpages = cifs_readpages, |
| 4258 | .writepage = cifs_writepage, |
Steve French | 37c0eb4 | 2005-10-05 14:50:29 -0700 | [diff] [blame] | 4259 | .writepages = cifs_writepages, |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4260 | .write_begin = cifs_write_begin, |
| 4261 | .write_end = cifs_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | .set_page_dirty = __set_page_dirty_nobuffers, |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4263 | .releasepage = cifs_release_page, |
Steve French | dca6928 | 2013-11-11 16:42:37 -0600 | [diff] [blame] | 4264 | .direct_IO = cifs_direct_io, |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4265 | .invalidatepage = cifs_invalidate_page, |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 4266 | .launder_page = cifs_launder_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | }; |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 4268 | |
| 4269 | /* |
| 4270 | * cifs_readpages requires the server to support a buffer large enough to |
| 4271 | * contain the header plus one complete page of data. Otherwise, we need |
| 4272 | * to leave cifs_readpages out of the address space operations. |
| 4273 | */ |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 4274 | const struct address_space_operations cifs_addr_ops_smallbuf = { |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 4275 | .readpage = cifs_readpage, |
| 4276 | .writepage = cifs_writepage, |
| 4277 | .writepages = cifs_writepages, |
Nick Piggin | d941477 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 4278 | .write_begin = cifs_write_begin, |
| 4279 | .write_end = cifs_write_end, |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 4280 | .set_page_dirty = __set_page_dirty_nobuffers, |
Suresh Jayaraman | 85f2d6b | 2010-07-05 18:13:00 +0530 | [diff] [blame] | 4281 | .releasepage = cifs_release_page, |
| 4282 | .invalidatepage = cifs_invalidate_page, |
Pavel Shilovsky | 9ad1506 | 2011-04-08 05:29:10 +0400 | [diff] [blame] | 4283 | .launder_page = cifs_launder_page, |
Dave Kleikamp | 273d81d | 2006-06-01 19:41:23 +0000 | [diff] [blame] | 4284 | }; |