blob: cfb0d91289ec76620add04e8b2d96ef49d466be9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/file.c
3 *
4 * vfs operations that deal with files
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005 *
Steve Frenchf19159d2010-04-21 04:12:10 +00006 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Author(s): Steve French (sfrench@us.ibm.com)
Jeremy Allison7ee1af72006-08-02 21:56:33 +00008 * Jeremy Allison (jra@samba.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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 French37c0eb42005-10-05 14:50:29 -070025#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/stat.h>
27#include <linux/fcntl.h>
28#include <linux/pagemap.h>
29#include <linux/pagevec.h>
Steve French37c0eb42005-10-05 14:50:29 -070030#include <linux/writeback.h>
Andrew Morton6f88cc22006-12-10 02:19:44 -080031#include <linux/task_io_accounting_ops.h>
Steve French23e7dd72005-10-20 13:44:56 -070032#include <linux/delay.h>
Jeff Layton3bc303c2009-09-21 06:47:50 -040033#include <linux/mount.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Jeff Layton690c5e32011-10-19 15:30:16 -040035#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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 Jayaraman9451a9a2010-07-05 18:12:45 +053044#include "fscache.h"
Long Libd3dcc62017-11-22 17:38:47 -070045#include "smbdirect.h"
Steve French07b92d02013-02-18 10:34:26 -060046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static 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 Laytone10f7b52008-05-14 10:21:33 -070060 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
61 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
62 FILE_READ_DATA);
Steve French7fc8f4e2009-02-23 20:43:11 +000063}
Jeff Laytone10f7b52008-05-14 10:21:33 -070064
Jeff Layton608712f2010-10-15 15:33:56 -040065static u32 cifs_posix_convert_flags(unsigned int flags)
Steve French7fc8f4e2009-02-23 20:43:11 +000066{
Jeff Layton608712f2010-10-15 15:33:56 -040067 u32 posix_flags = 0;
Jeff Laytone10f7b52008-05-14 10:21:33 -070068
Steve French7fc8f4e2009-02-23 20:43:11 +000069 if ((flags & O_ACCMODE) == O_RDONLY)
Jeff Layton608712f2010-10-15 15:33:56 -040070 posix_flags = SMB_O_RDONLY;
Steve French7fc8f4e2009-02-23 20:43:11 +000071 else if ((flags & O_ACCMODE) == O_WRONLY)
Jeff Layton608712f2010-10-15 15:33:56 -040072 posix_flags = SMB_O_WRONLY;
73 else if ((flags & O_ACCMODE) == O_RDWR)
74 posix_flags = SMB_O_RDWR;
75
Steve French07b92d02013-02-18 10:34:26 -060076 if (flags & O_CREAT) {
Jeff Layton608712f2010-10-15 15:33:56 -040077 posix_flags |= SMB_O_CREAT;
Steve French07b92d02013-02-18 10:34:26 -060078 if (flags & O_EXCL)
79 posix_flags |= SMB_O_EXCL;
80 } else if (flags & O_EXCL)
Joe Perchesf96637b2013-05-04 22:12:25 -050081 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 French07b92d02013-02-18 10:34:26 -060083
Jeff Layton608712f2010-10-15 15:33:56 -040084 if (flags & O_TRUNC)
85 posix_flags |= SMB_O_TRUNC;
86 /* be safe and imply O_SYNC for O_DSYNC */
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +010087 if (flags & O_DSYNC)
Jeff Layton608712f2010-10-15 15:33:56 -040088 posix_flags |= SMB_O_SYNC;
Steve French7fc8f4e2009-02-23 20:43:11 +000089 if (flags & O_DIRECTORY)
Jeff Layton608712f2010-10-15 15:33:56 -040090 posix_flags |= SMB_O_DIRECTORY;
Steve French7fc8f4e2009-02-23 20:43:11 +000091 if (flags & O_NOFOLLOW)
Jeff Layton608712f2010-10-15 15:33:56 -040092 posix_flags |= SMB_O_NOFOLLOW;
Steve French7fc8f4e2009-02-23 20:43:11 +000093 if (flags & O_DIRECT)
Jeff Layton608712f2010-10-15 15:33:56 -040094 posix_flags |= SMB_O_DIRECT;
Steve French7fc8f4e2009-02-23 20:43:11 +000095
96 return posix_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static 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 French55aa2e02006-05-30 18:09:31 +0000107 else if ((flags & O_TRUNC) == O_TRUNC)
108 return FILE_OVERWRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 else
110 return FILE_OPEN;
111}
112
Jeff Layton608712f2010-10-15 15:33:56 -0400113int cifs_posix_open(char *full_path, struct inode **pinode,
114 struct super_block *sb, int mode, unsigned int f_flags,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400115 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
Jeff Layton608712f2010-10-15 15:33:56 -0400116{
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 French96daf2b2011-05-27 04:34:02 +0000123 struct cifs_tcon *tcon;
Jeff Layton608712f2010-10-15 15:33:56 -0400124
Joe Perchesf96637b2013-05-04 22:12:25 -0500125 cifs_dbg(FYI, "posix open %s\n", full_path);
Jeff Layton608712f2010-10-15 15:33:56 -0400126
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 Akirabc8ebdc42015-02-13 15:35:58 +0900143 cifs_remap(cifs_sb));
Jeff Layton608712f2010-10-15 15:33:56 -0400144 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
169posix_open_ret:
170 kfree(presp_data);
171 return rc;
172}
173
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300174static int
175cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700176 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
177 struct cifs_fid *fid, unsigned int xid)
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300178{
179 int rc;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700180 int desired_access;
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300181 int disposition;
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -0500182 int create_options = CREATE_NOT_DIR;
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300183 FILE_ALL_INFO *buf;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700184 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400185 struct cifs_open_parms oparms;
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300186
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700187 if (!server->ops->open)
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700188 return -ENOSYS;
189
190 desired_access = cifs_convert_flags(f_flags);
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300191
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 Pargaonkar3d3ea8e2011-09-26 09:56:44 -0500224 if (backup_cred(cifs_sb))
225 create_options |= CREATE_OPEN_BACKUP_INTENT;
226
Steve French1013e762017-09-22 01:40:27 -0500227 /* 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 Shilovsky226730b2013-07-05 12:00:30 +0400234 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 Shilovsky9cbc0b72013-07-09 18:40:58 +0400241 oparms.reconnect = false;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400242
243 rc = server->ops->open(xid, &oparms, oplock, buf);
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300244
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 French42eacf92014-02-10 14:08:16 -0600253 xid, fid);
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300254
Pavel Shilovskyd72c2112019-09-30 10:06:18 -0700255 if (rc) {
256 server->ops->close(xid, tcon, fid);
257 if (rc == -ESTALE)
258 rc = -EOPENSTALE;
259 }
260
Pavel Shilovskyeeb910a2010-11-25 15:12:39 +0300261out:
262 kfree(buf);
263 return rc;
264}
265
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400266static bool
267cifs_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 Wysochanski80b42f42019-10-23 05:02:33 -0400283void
284cifs_down_write(struct rw_semaphore *sem)
285{
286 while (!down_write_trylock(sem))
287 msleep(10);
288}
289
Jeff Layton15ecb432010-10-15 15:34:02 -0400290struct cifsFileInfo *
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700291cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
Jeff Layton15ecb432010-10-15 15:34:02 -0400292 struct tcon_link *tlink, __u32 oplock)
293{
Goldwyn Rodrigues1f1735c2016-04-18 06:41:52 -0500294 struct dentry *dentry = file_dentry(file);
David Howells2b0143b2015-03-17 22:25:59 +0000295 struct inode *inode = d_inode(dentry);
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700296 struct cifsInodeInfo *cinode = CIFS_I(inode);
297 struct cifsFileInfo *cfile;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700298 struct cifs_fid_locks *fdlocks;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700299 struct cifs_tcon *tcon = tlink_tcon(tlink);
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400300 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Layton15ecb432010-10-15 15:34:02 -0400301
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700302 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
303 if (cfile == NULL)
304 return cfile;
Jeff Layton15ecb432010-10-15 15:34:02 -0400305
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700306 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 Shilovskyf45d3412012-09-19 06:22:43 -0700315
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700316 cfile->count = 1;
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700317 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 Shilovsky4b4de762012-09-18 16:20:26 -0700323 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700324 mutex_init(&cfile->fh_mutex);
Steve French3afca262016-09-22 18:58:16 -0500325 spin_lock_init(&cfile->file_info_lock);
Jeff Layton15ecb432010-10-15 15:34:02 -0400326
Mateusz Guzik24261fc2013-03-08 16:30:03 +0100327 cifs_sb_active(inode->i_sb);
328
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400329 /*
330 * If the server returned a read oplock and we have mandatory brlocks,
331 * set oplock level to None.
332 */
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400333 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500334 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400335 oplock = 0;
336 }
337
Pavel Shilovskydf871e52019-11-27 16:18:39 -0800338 cifs_down_write(&cinode->lock_sem);
339 list_add(&fdlocks->llist, &cinode->llist);
340 up_write(&cinode->lock_sem);
341
Steve French3afca262016-09-22 18:58:16 -0500342 spin_lock(&tcon->open_file_lock);
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400343 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700344 oplock = fid->pending_open->oplock;
345 list_del(&fid->pending_open->olist);
346
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400347 fid->purge_cache = false;
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400348 server->ops->set_fid(cfile, fid, oplock);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700349
350 list_add(&cfile->tlist, &tcon->openFileList);
Steve French3afca262016-09-22 18:58:16 -0500351
Jeff Layton15ecb432010-10-15 15:34:02 -0400352 /* if readable file instance put first in list*/
Ronnie Sahlbergacc07942019-06-05 10:38:38 +1000353 spin_lock(&cinode->open_file_lock);
Jeff Layton15ecb432010-10-15 15:34:02 -0400354 if (file->f_mode & FMODE_READ)
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700355 list_add(&cfile->flist, &cinode->openFileList);
Jeff Layton15ecb432010-10-15 15:34:02 -0400356 else
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700357 list_add_tail(&cfile->flist, &cinode->openFileList);
Ronnie Sahlbergacc07942019-06-05 10:38:38 +1000358 spin_unlock(&cinode->open_file_lock);
Steve French3afca262016-09-22 18:58:16 -0500359 spin_unlock(&tcon->open_file_lock);
Jeff Layton15ecb432010-10-15 15:34:02 -0400360
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400361 if (fid->purge_cache)
Jeff Layton4f73c7d2014-04-30 09:31:47 -0400362 cifs_zap_mapping(inode);
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400363
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700364 file->private_data = cfile;
365 return cfile;
Jeff Layton15ecb432010-10-15 15:34:02 -0400366}
367
Jeff Layton764a1b12012-07-25 14:59:54 -0400368struct cifsFileInfo *
369cifsFileInfo_get(struct cifsFileInfo *cifs_file)
370{
Steve French3afca262016-09-22 18:58:16 -0500371 spin_lock(&cifs_file->file_info_lock);
Jeff Layton764a1b12012-07-25 14:59:54 -0400372 cifsFileInfo_get_locked(cifs_file);
Steve French3afca262016-09-22 18:58:16 -0500373 spin_unlock(&cifs_file->file_info_lock);
Jeff Layton764a1b12012-07-25 14:59:54 -0400374 return cifs_file;
375}
376
Aurelien Aptel8092ecc2019-03-29 10:49:12 +0100377/**
378 * cifsFileInfo_put - release a reference of file priv data
379 *
380 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
Steve Frenchcdff08e2010-10-21 22:46:14 +0000381 */
Jeff Laytonb33879a2010-10-15 15:34:04 -0400382void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
383{
Aurelien Aptel8092ecc2019-03-29 10:49:12 +0100384 _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 */
400void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, bool wait_oplock_handler)
401{
David Howells2b0143b2015-03-17 22:25:59 +0000402 struct inode *inode = d_inode(cifs_file->dentry);
Steve French96daf2b2011-05-27 04:34:02 +0000403 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700404 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskye66673e2010-11-02 12:00:42 +0300405 struct cifsInodeInfo *cifsi = CIFS_I(inode);
Mateusz Guzik24261fc2013-03-08 16:30:03 +0100406 struct super_block *sb = inode->i_sb;
407 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000408 struct cifsLockInfo *li, *tmp;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700409 struct cifs_fid fid;
410 struct cifs_pending_open open;
Sachin Prabhuca7df8e2015-01-15 12:22:04 +0000411 bool oplock_break_cancelled;
Steve Frenchcdff08e2010-10-21 22:46:14 +0000412
Steve French3afca262016-09-22 18:58:16 -0500413 spin_lock(&tcon->open_file_lock);
Pavel Shilovsky01332b02019-10-23 15:37:19 -0700414 spin_lock(&cifsi->open_file_lock);
Steve French3afca262016-09-22 18:58:16 -0500415 spin_lock(&cifs_file->file_info_lock);
Jeff Layton5f6dbc92010-10-15 15:34:06 -0400416 if (--cifs_file->count > 0) {
Steve French3afca262016-09-22 18:58:16 -0500417 spin_unlock(&cifs_file->file_info_lock);
Pavel Shilovsky01332b02019-10-23 15:37:19 -0700418 spin_unlock(&cifsi->open_file_lock);
Steve French3afca262016-09-22 18:58:16 -0500419 spin_unlock(&tcon->open_file_lock);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000420 return;
Jeff Laytonb33879a2010-10-15 15:34:04 -0400421 }
Steve French3afca262016-09-22 18:58:16 -0500422 spin_unlock(&cifs_file->file_info_lock);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000423
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700424 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 Frenchcdff08e2010-10-21 22:46:14 +0000430 /* 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 Perchesf96637b2013-05-04 22:12:25 -0500435 cifs_dbg(FYI, "closing last open instance for inode %p\n",
David Howells2b0143b2015-03-17 22:25:59 +0000436 d_inode(cifs_file->dentry));
Pavel Shilovsky25364132012-09-18 16:20:27 -0700437 /*
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 Shilovsky4f8ba8a2010-11-21 22:36:12 +0300442 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400443 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
Pavel Shilovskyc6723622010-11-03 10:58:57 +0300444 cifs_set_oplock_level(cifsi, 0);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000445 }
Steve French3afca262016-09-22 18:58:16 -0500446
Pavel Shilovsky01332b02019-10-23 15:37:19 -0700447 spin_unlock(&cifsi->open_file_lock);
Steve French3afca262016-09-22 18:58:16 -0500448 spin_unlock(&tcon->open_file_lock);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000449
Aurelien Aptel8092ecc2019-03-29 10:49:12 +0100450 oplock_break_cancelled = wait_oplock_handler ?
451 cancel_work_sync(&cifs_file->oplock_break) : false;
Jeff Laytonad635942011-07-26 12:20:17 -0400452
Steve Frenchcdff08e2010-10-21 22:46:14 +0000453 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -0700454 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400455 unsigned int xid;
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -0700456
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400457 xid = get_xid();
Pavel Shilovsky0ff78a22012-09-18 16:20:26 -0700458 if (server->ops->close)
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400459 server->ops->close(xid, tcon, &cifs_file->fid);
460 _free_xid(xid);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000461 }
462
Sachin Prabhuca7df8e2015-01-15 12:22:04 +0000463 if (oplock_break_cancelled)
464 cifs_done_oplock_break(cifsi);
465
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700466 cifs_del_pending_open(&open);
467
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700468 /*
469 * Delete any outstanding lock records. We'll lose them when the file
Steve Frenchcdff08e2010-10-21 22:46:14 +0000470 * is closed anyway.
471 */
Dave Wysochanski80b42f42019-10-23 05:02:33 -0400472 cifs_down_write(&cifsi->lock_sem);
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700473 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
Steve Frenchcdff08e2010-10-21 22:46:14 +0000474 list_del(&li->llist);
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400475 cifs_del_lock_waiters(li);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000476 kfree(li);
477 }
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700478 list_del(&cifs_file->llist->llist);
479 kfree(cifs_file->llist);
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -0700480 up_write(&cifsi->lock_sem);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000481
482 cifs_put_tlink(cifs_file->tlink);
483 dput(cifs_file->dentry);
Mateusz Guzik24261fc2013-03-08 16:30:03 +0100484 cifs_sb_deactive(sb);
Steve Frenchcdff08e2010-10-21 22:46:14 +0000485 kfree(cifs_file);
Jeff Laytonb33879a2010-10-15 15:34:04 -0400486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488int cifs_open(struct inode *inode, struct file *file)
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 int rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400492 unsigned int xid;
Jeff Layton590a3fe2009-09-12 11:54:28 -0400493 __u32 oplock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct cifs_sb_info *cifs_sb;
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700495 struct TCP_Server_Info *server;
Steve French96daf2b2011-05-27 04:34:02 +0000496 struct cifs_tcon *tcon;
Jeff Layton7ffec372010-09-29 19:51:11 -0400497 struct tcon_link *tlink;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700498 struct cifsFileInfo *cfile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 char *full_path = NULL;
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300500 bool posix_open_ok = false;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700501 struct cifs_fid fid;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700502 struct cifs_pending_open open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400504 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -0400507 tlink = cifs_sb_tlink(cifs_sb);
508 if (IS_ERR(tlink)) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400509 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400510 return PTR_ERR(tlink);
511 }
512 tcon = tlink_tcon(tlink);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700513 server = tcon->ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Goldwyn Rodrigues1f1735c2016-04-18 06:41:52 -0500515 full_path = build_path_from_dentry(file_dentry(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530517 rc = -ENOMEM;
Jeff Layton232341b2010-08-05 13:58:38 -0400518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Joe Perchesf96637b2013-05-04 22:12:25 -0500521 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +0000522 inode, file->f_flags, full_path);
Steve French276a74a2009-03-03 18:00:34 +0000523
Namjae Jeon787aded2014-08-22 14:22:51 +0900524 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 Shilovsky233839b2012-09-19 06:22:45 -0700532 if (server->oplocks)
Steve French276a74a2009-03-03 18:00:34 +0000533 oplock = REQ_OPLOCK;
534 else
535 oplock = 0;
536
Steve French64cc2c62009-03-04 19:54:08 +0000537 if (!tcon->broken_posix_open && tcon->unix_ext &&
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400538 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
539 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Steve French276a74a2009-03-03 18:00:34 +0000540 /* can not refresh inode info since size could be stale */
Jeff Layton2422f672010-06-16 13:40:16 -0400541 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
Steve Frenchfa588e02010-04-22 19:21:55 +0000542 cifs_sb->mnt_file_mode /* ignored */,
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700543 file->f_flags, &oplock, &fid.netfid, xid);
Steve French276a74a2009-03-03 18:00:34 +0000544 if (rc == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500545 cifs_dbg(FYI, "posix open succeeded\n");
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300546 posix_open_ok = true;
Steve French64cc2c62009-03-04 19:54:08 +0000547 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
548 if (tcon->ses->serverNOS)
Joe Perchesf96637b2013-05-04 22:12:25 -0500549 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 French64cc2c62009-03-04 19:54:08 +0000552 tcon->broken_posix_open = true;
Steve French276a74a2009-03-03 18:00:34 +0000553 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
554 (rc != -EOPNOTSUPP)) /* path not found or net err */
555 goto out;
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700556 /*
557 * Else fallthrough to retry open the old way on network i/o
558 * or DFS errors.
559 */
Steve French276a74a2009-03-03 18:00:34 +0000560 }
561
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700562 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 Shilovsky7e12edd2010-11-25 17:20:20 +0300567 if (!posix_open_ok) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700568 if (server->ops->get_lease_key)
569 server->ops->get_lease_key(inode, &fid);
570
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300571 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700572 file->f_flags, &oplock, &fid, xid);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700573 if (rc) {
574 cifs_del_pending_open(&open);
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300575 goto out;
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700576 }
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300577 }
Jeff Layton47c78b72010-06-16 13:40:17 -0400578
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700579 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
580 if (cfile == NULL) {
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700581 if (server->ops->close)
582 server->ops->close(xid, tcon, &fid);
Pavel Shilovsky233839b2012-09-19 06:22:45 -0700583 cifs_del_pending_open(&open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 rc = -ENOMEM;
585 goto out;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530588 cifs_fscache_set_inode_cookie(inode, file);
589
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300590 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700591 /*
592 * Time to set mode which we can not set earlier due to
593 * problems creating new read-only files.
594 */
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300595 struct cifs_unix_set_info_args args = {
596 .mode = inode->i_mode,
Eric W. Biederman49418b22013-02-06 00:57:56 -0800597 .uid = INVALID_UID, /* no change */
598 .gid = INVALID_GID, /* no change */
Pavel Shilovsky7e12edd2010-11-25 17:20:20 +0300599 .ctime = NO_CHANGE_64,
600 .atime = NO_CHANGE_64,
601 .mtime = NO_CHANGE_64,
602 .device = 0,
603 };
Pavel Shilovskyfb1214e2012-09-18 16:20:26 -0700604 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
605 cfile->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400610 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400611 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return rc;
613}
614
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400615static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
616
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700617/*
618 * Try to reacquire byte range locks that were released when session
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400619 * to server was lost.
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700620 */
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400621static int
622cifs_relock_file(struct cifsFileInfo *cfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400624 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000625 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400626 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int rc = 0;
628
Rabin Vincent560d3882017-05-03 17:17:21 +0200629 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400630 if (cinode->can_cache_brlcks) {
Pavel Shilovsky689c3db2013-07-11 11:17:45 +0400631 /* can cache locks - no need to relock */
632 up_read(&cinode->lock_sem);
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400633 return rc;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Pavel Shilovskyf152fd52012-11-22 17:10:57 +0400636 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 Shilovsky689c3db2013-07-11 11:17:45 +0400643 up_read(&cinode->lock_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return rc;
645}
646
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700647static int
648cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 int rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400651 unsigned int xid;
Jeff Layton590a3fe2009-09-12 11:54:28 -0400652 __u32 oplock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct cifs_sb_info *cifs_sb;
Steve French96daf2b2011-05-27 04:34:02 +0000654 struct cifs_tcon *tcon;
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700655 struct TCP_Server_Info *server;
656 struct cifsInodeInfo *cinode;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000657 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 char *full_path = NULL;
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700659 int desired_access;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int disposition = FILE_OPEN;
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -0500661 int create_options = CREATE_NOT_DIR;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400662 struct cifs_open_parms oparms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400664 xid = get_xid();
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700665 mutex_lock(&cfile->fh_mutex);
666 if (!cfile->invalidHandle) {
667 mutex_unlock(&cfile->fh_mutex);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530668 rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400669 free_xid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530670 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
David Howells2b0143b2015-03-17 22:25:59 +0000673 inode = d_inode(cfile->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 cifs_sb = CIFS_SB(inode->i_sb);
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700675 tcon = tlink_tcon(cfile->tlink);
676 server = tcon->ses->server;
Steve French3a9f4622007-04-04 17:10:24 +0000677
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700678 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700685 if (full_path == NULL) {
Steve French3a9f4622007-04-04 17:10:24 +0000686 rc = -ENOMEM;
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700687 mutex_unlock(&cfile->fh_mutex);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400688 free_xid(xid);
Steve French3a9f4622007-04-04 17:10:24 +0000689 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Joe Perchesf96637b2013-05-04 22:12:25 -0500692 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
693 inode, cfile->f_flags, full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Pavel Shilovsky10b9b982012-03-20 12:55:09 +0300695 if (tcon->ses->server->oplocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 oplock = REQ_OPLOCK;
697 else
Steve French4b18f2a2008-04-29 00:06:05 +0000698 oplock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400700 if (tcon->unix_ext && cap_unix(tcon->ses) &&
Steve French7fc8f4e2009-02-23 20:43:11 +0000701 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400702 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Jeff Layton608712f2010-10-15 15:33:56 -0400703 /*
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 Shilovsky2ae78ba2012-09-18 16:20:27 -0700707 unsigned int oflags = cfile->f_flags &
Jeff Layton15886172010-10-15 15:33:59 -0400708 ~(O_CREAT | O_EXCL | O_TRUNC);
Jeff Layton608712f2010-10-15 15:33:56 -0400709
Jeff Layton2422f672010-06-16 13:40:16 -0400710 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700711 cifs_sb->mnt_file_mode /* ignored */,
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400712 oflags, &oplock, &cfile->fid.netfid, xid);
Steve French7fc8f4e2009-02-23 20:43:11 +0000713 if (rc == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500714 cifs_dbg(FYI, "posix reopen succeeded\n");
Andi Shytife090e42013-07-29 20:04:35 +0200715 oparms.reconnect = true;
Steve French7fc8f4e2009-02-23 20:43:11 +0000716 goto reopen_success;
717 }
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700718 /*
719 * fallthrough to retry open the old way on errors, especially
720 * in the reconnect path it is important to retry hard
721 */
Steve French7fc8f4e2009-02-23 20:43:11 +0000722 }
723
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700724 desired_access = cifs_convert_flags(cfile->f_flags);
Steve French7fc8f4e2009-02-23 20:43:11 +0000725
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -0500726 if (backup_cred(cifs_sb))
727 create_options |= CREATE_OPEN_BACKUP_INTENT;
728
Pavel Shilovsky3ddc09c2019-11-12 17:16:35 -0800729 /* 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 Shilovskyb8c32db2012-09-19 06:22:44 -0700736 if (server->ops->get_lease_key)
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400737 server->ops->get_lease_key(inode, &cfile->fid);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700738
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400739 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 Shilovsky9cbc0b72013-07-09 18:40:58 +0400745 oparms.fid = &cfile->fid;
746 oparms.reconnect = true;
Pavel Shilovsky226730b2013-07-05 12:00:30 +0400747
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700748 /*
749 * Can not refresh inode by passing in file_info buf to be returned by
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400750 * ops->open and then calling get_inode_info with returned buf since
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700751 * 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 Shilovsky226730b2013-07-05 12:00:30 +0400755 rc = server->ops->open(xid, &oparms, &oplock, NULL);
Pavel Shilovskyb33fcf12013-07-11 10:58:30 +0400756 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 Torvalds1da177e2005-04-16 15:20:36 -0700763 if (rc) {
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700764 mutex_unlock(&cfile->fh_mutex);
Joe Perchesf96637b2013-05-04 22:12:25 -0500765 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
766 cifs_dbg(FYI, "oplock: %d\n", oplock);
Jeff Layton15886172010-10-15 15:33:59 -0400767 goto reopen_error_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Jeff Layton15886172010-10-15 15:33:59 -0400769
770reopen_success:
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700771 cfile->invalidHandle = false;
772 mutex_unlock(&cfile->fh_mutex);
773 cinode = CIFS_I(inode);
Jeff Layton15886172010-10-15 15:33:59 -0400774
775 if (can_flush) {
776 rc = filemap_write_and_wait(inode->i_mapping);
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -0800777 if (!is_interrupt_error(rc))
778 mapping_set_error(inode->i_mapping, rc);
Jeff Layton15886172010-10-15 15:33:59 -0400779
Jeff Layton15886172010-10-15 15:33:59 -0400780 if (tcon->unix_ext)
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700781 rc = cifs_get_inode_info_unix(&inode, full_path,
782 inode->i_sb, xid);
Jeff Layton15886172010-10-15 15:33:59 -0400783 else
Pavel Shilovsky2ae78ba2012-09-18 16:20:27 -0700784 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 Shilovskye66673e2010-11-02 12:00:42 +0300793
Pavel Shilovskyde740252016-10-11 15:34:07 -0700794 /*
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 Shilovsky9cbc0b72013-07-09 18:40:58 +0400803 server->ops->set_fid(cfile, &cfile->fid, oplock);
804 if (oparms.reconnect)
805 cifs_relock_file(cfile);
Jeff Layton15886172010-10-15 15:33:59 -0400806
807reopen_error_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400809 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return rc;
811}
812
813int cifs_close(struct inode *inode, struct file *file)
814{
Jeff Layton77970692011-04-05 16:23:47 -0700815 if (file->private_data != NULL) {
816 cifsFileInfo_put(file->private_data);
817 file->private_data = NULL;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Steve Frenchcdff08e2010-10-21 22:46:14 +0000820 /* return code from the ->release op is always ignored */
821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Steve French52ace1e2016-09-22 19:23:56 -0500824void
825cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
826{
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700827 struct cifsFileInfo *open_file;
Steve French52ace1e2016-09-22 19:23:56 -0500828 struct list_head *tmp;
829 struct list_head *tmp1;
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700830 struct list_head tmp_list;
831
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800832 if (!tcon->use_persistent || !tcon->need_reopen_files)
833 return;
834
835 tcon->need_reopen_files = false;
836
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700837 cifs_dbg(FYI, "Reopen persistent handles");
838 INIT_LIST_HEAD(&tmp_list);
Steve French52ace1e2016-09-22 19:23:56 -0500839
840 /* list all files open on tree connection, reopen resilient handles */
841 spin_lock(&tcon->open_file_lock);
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700842 list_for_each(tmp, &tcon->openFileList) {
Steve French52ace1e2016-09-22 19:23:56 -0500843 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700844 if (!open_file->invalidHandle)
845 continue;
846 cifsFileInfo_get(open_file);
847 list_add_tail(&open_file->rlist, &tmp_list);
Steve French52ace1e2016-09-22 19:23:56 -0500848 }
849 spin_unlock(&tcon->open_file_lock);
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700850
851 list_for_each_safe(tmp, tmp1, &tmp_list) {
852 open_file = list_entry(tmp, struct cifsFileInfo, rlist);
Pavel Shilovsky96a988f2016-11-29 11:31:23 -0800853 if (cifs_reopen_file(open_file, false /* do not flush */))
854 tcon->need_reopen_files = true;
Pavel Shilovskyf2cca6a2016-10-07 17:26:36 -0700855 list_del_init(&open_file->rlist);
856 cifsFileInfo_put(open_file);
857 }
Steve French52ace1e2016-09-22 19:23:56 -0500858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860int cifs_closedir(struct inode *inode, struct file *file)
861{
862 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400863 unsigned int xid;
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700864 struct cifsFileInfo *cfile = file->private_data;
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700865 struct cifs_tcon *tcon;
866 struct TCP_Server_Info *server;
867 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Joe Perchesf96637b2013-05-04 22:12:25 -0500869 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700871 if (cfile == NULL)
872 return rc;
873
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400874 xid = get_xid();
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700875 tcon = tlink_tcon(cfile->tlink);
876 server = tcon->ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Joe Perchesf96637b2013-05-04 22:12:25 -0500878 cifs_dbg(FYI, "Freeing private data in close dir\n");
Steve French3afca262016-09-22 18:58:16 -0500879 spin_lock(&cfile->file_info_lock);
Pavel Shilovsky52755802014-08-18 20:49:57 +0400880 if (server->ops->dir_needs_close(cfile)) {
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700881 cfile->invalidHandle = true;
Steve French3afca262016-09-22 18:58:16 -0500882 spin_unlock(&cfile->file_info_lock);
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700883 if (server->ops->close_dir)
884 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
885 else
886 rc = -ENOSYS;
Joe Perchesf96637b2013-05-04 22:12:25 -0500887 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700888 /* not much we can do if it fails anyway, ignore rc */
889 rc = 0;
890 } else
Steve French3afca262016-09-22 18:58:16 -0500891 spin_unlock(&cfile->file_info_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700893 buf = cfile->srch_inf.ntwrk_buf_start;
894 if (buf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500895 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700896 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 Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Pavel Shilovsky92fc65a2012-09-18 16:20:32 -0700902
903 cifs_put_tlink(cfile->tlink);
904 kfree(file->private_data);
905 file->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /* BB can we lock the filestruct while this is going on? */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400907 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return rc;
909}
910
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400911static struct cifsLockInfo *
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300912cifs_lock_init(__u64 offset, __u64 length, __u8 type)
Jeremy Allison7ee1af72006-08-02 21:56:33 +0000913{
Pavel Shilovskya88b4702011-10-29 17:17:59 +0400914 struct cifsLockInfo *lock =
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000915 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
Pavel Shilovskya88b4702011-10-29 17:17:59 +0400916 if (!lock)
917 return lock;
918 lock->offset = offset;
919 lock->length = length;
920 lock->type = type;
Pavel Shilovskya88b4702011-10-29 17:17:59 +0400921 lock->pid = current->tgid;
922 INIT_LIST_HEAD(&lock->blist);
923 init_waitqueue_head(&lock->block_q);
924 return lock;
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400925}
926
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700927void
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400928cifs_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 Shilovsky081c0412012-11-27 18:38:53 +0400937#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 Shilovsky85160e02011-10-22 15:33:29 +0400942static bool
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700943cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
944 __u64 length, __u8 type, struct cifsFileInfo *cfile,
Pavel Shilovsky081c0412012-11-27 18:38:53 +0400945 struct cifsLockInfo **conf_lock, int rw_check)
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400946{
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300947 struct cifsLockInfo *li;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700948 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
Pavel Shilovsky106dc532012-02-28 14:23:34 +0300949 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400950
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700951 list_for_each_entry(li, &fdlocks->locks, llist) {
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400952 if (offset + length <= li->offset ||
953 offset >= li->offset + li->length)
954 continue;
Pavel Shilovsky081c0412012-11-27 18:38:53 +0400955 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 Shilovskyf45d3412012-09-19 06:22:43 -0700962 if ((type & server->vals->shared_lock_type) &&
963 ((server->ops->compare_fids(cfile, cur_cfile) &&
964 current->tgid == li->pid) || type == li->type))
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400965 continue;
Pavel Shilovsky579f9052012-09-19 06:22:44 -0700966 if (conf_lock)
967 *conf_lock = li;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700968 return true;
Pavel Shilovsky85160e02011-10-22 15:33:29 +0400969 }
970 return false;
971}
972
Pavel Shilovsky579f9052012-09-19 06:22:44 -0700973bool
Pavel Shilovsky55157df2012-02-28 14:04:17 +0300974cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
Pavel Shilovsky579f9052012-09-19 06:22:44 -0700975 __u8 type, struct cifsLockInfo **conf_lock,
Pavel Shilovsky081c0412012-11-27 18:38:53 +0400976 int rw_check)
Pavel Shilovsky161ebf92011-10-29 17:17:58 +0400977{
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300978 bool rc = false;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700979 struct cifs_fid_locks *cur;
David Howells2b0143b2015-03-17 22:25:59 +0000980 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300981
Pavel Shilovskyf45d3412012-09-19 06:22:43 -0700982 list_for_each_entry(cur, &cinode->llist, llist) {
983 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
Pavel Shilovsky579f9052012-09-19 06:22:44 -0700984 cfile, conf_lock, rw_check);
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300985 if (rc)
986 break;
987 }
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +0300988
989 return rc;
Pavel Shilovsky161ebf92011-10-29 17:17:58 +0400990}
991
Pavel Shilovsky9a5101c2011-11-07 16:11:24 +0300992/*
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 Shilovsky85160e02011-10-22 15:33:29 +0400999static int
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001000cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1001 __u8 type, struct file_lock *flock)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001002{
1003 int rc = 0;
1004 struct cifsLockInfo *conf_lock;
David Howells2b0143b2015-03-17 22:25:59 +00001005 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001006 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001007 bool exist;
1008
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001009 down_read(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001010
Pavel Shilovsky55157df2012-02-28 14:04:17 +03001011 exist = cifs_find_lock_conflict(cfile, offset, length, type,
Pavel Shilovsky081c0412012-11-27 18:38:53 +04001012 &conf_lock, CIFS_LOCK_OP);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001013 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 Shilovsky106dc532012-02-28 14:23:34 +03001017 if (conf_lock->type & server->vals->shared_lock_type)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001018 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 Shilovsky1b4b55a2012-09-19 06:22:44 -07001026 up_read(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001027 return rc;
1028}
1029
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001030static void
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001031cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001032{
David Howells2b0143b2015-03-17 22:25:59 +00001033 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Dave Wysochanski80b42f42019-10-23 05:02:33 -04001034 cifs_down_write(&cinode->lock_sem);
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001035 list_add_tail(&lock->llist, &cfile->llist->locks);
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001036 up_write(&cinode->lock_sem);
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001037}
1038
Pavel Shilovsky9a5101c2011-11-07 16:11:24 +03001039/*
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 Shilovsky85160e02011-10-22 15:33:29 +04001045static int
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001046cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001047 bool wait)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001048{
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001049 struct cifsLockInfo *conf_lock;
David Howells2b0143b2015-03-17 22:25:59 +00001050 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001051 bool exist;
1052 int rc = 0;
1053
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001054try_again:
1055 exist = false;
Dave Wysochanski80b42f42019-10-23 05:02:33 -04001056 cifs_down_write(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001057
Pavel Shilovsky55157df2012-02-28 14:04:17 +03001058 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
Pavel Shilovsky081c0412012-11-27 18:38:53 +04001059 lock->type, &conf_lock, CIFS_LOCK_OP);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001060 if (!exist && cinode->can_cache_brlcks) {
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001061 list_add_tail(&lock->llist, &cfile->llist->locks);
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001062 up_write(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001063 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 Shilovsky1b4b55a2012-09-19 06:22:44 -07001072 up_write(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001073 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 Wysochanski80b42f42019-10-23 05:02:33 -04001078 cifs_down_write(&cinode->lock_sem);
Pavel Shilovskya88b4702011-10-29 17:17:59 +04001079 list_del_init(&lock->blist);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001080 }
1081
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001082 up_write(&cinode->lock_sem);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001083 return rc;
1084}
1085
Pavel Shilovsky9a5101c2011-11-07 16:11:24 +03001086/*
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 Shilovsky85160e02011-10-22 15:33:29 +04001093static int
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001094cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1095{
1096 int rc = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05001097 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001098 unsigned char saved_type = flock->fl_type;
1099
Pavel Shilovsky50792762011-10-29 17:17:57 +04001100 if ((flock->fl_flags & FL_POSIX) == 0)
1101 return 1;
1102
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001103 down_read(&cinode->lock_sem);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001104 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 Shilovsky1b4b55a2012-09-19 06:22:44 -07001111 up_read(&cinode->lock_sem);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001112 return rc;
1113}
1114
Pavel Shilovsky9a5101c2011-11-07 16:11:24 +03001115/*
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 Shilovsky4f6bcec2011-10-22 15:33:30 +04001121static int
1122cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1123{
Al Viro496ad9a2013-01-23 17:07:38 -05001124 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
Pavel Shilovsky50792762011-10-29 17:17:57 +04001125 int rc = 1;
1126
1127 if ((flock->fl_flags & FL_POSIX) == 0)
1128 return rc;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001129
Pavel Shilovsky66189be2012-03-28 21:56:19 +04001130try_again:
Dave Wysochanski80b42f42019-10-23 05:02:33 -04001131 cifs_down_write(&cinode->lock_sem);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001132 if (!cinode->can_cache_brlcks) {
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001133 up_write(&cinode->lock_sem);
Pavel Shilovsky50792762011-10-29 17:17:57 +04001134 return rc;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001135 }
Pavel Shilovsky66189be2012-03-28 21:56:19 +04001136
1137 rc = posix_lock_file(file, flock, NULL);
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001138 up_write(&cinode->lock_sem);
Pavel Shilovsky66189be2012-03-28 21:56:19 +04001139 if (rc == FILE_LOCK_DEFERRED) {
1140 rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
1141 if (!rc)
1142 goto try_again;
Jeff Layton1a9e64a2013-06-21 08:58:10 -04001143 posix_unblock_lock(flock);
Pavel Shilovsky66189be2012-03-28 21:56:19 +04001144 }
Steve French9ebb3892012-04-01 13:52:54 -05001145 return rc;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001146}
1147
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001148int
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001149cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001150{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001151 unsigned int xid;
1152 int rc = 0, stored_rc;
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001153 struct cifsLockInfo *li, *tmp;
1154 struct cifs_tcon *tcon;
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001155 unsigned int num, max_num, max_buf;
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001156 LOCKING_ANDX_RANGE *buf, *cur;
Colin Ian King4d61eda2017-09-19 16:27:39 +01001157 static const int types[] = {
1158 LOCKING_ANDX_LARGE_FILES,
1159 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1160 };
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001161 int i;
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001162
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001163 xid = get_xid();
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001164 tcon = tlink_tcon(cfile->tlink);
1165
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001166 /*
1167 * Accessing maxBuf is racy with cifs_reconnect - need to store value
Ross Lagerwall2a71a472019-01-08 18:30:57 +00001168 * and check it before using.
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001169 */
1170 max_buf = tcon->ses->server->maxBuf;
Ross Lagerwall2a71a472019-01-08 18:30:57 +00001171 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001172 free_xid(xid);
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001173 return -EINVAL;
1174 }
1175
Ross Lagerwall63715c12019-01-08 18:30:56 +00001176 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 Shilovsky0013fb42012-05-31 13:03:26 +04001180 max_num = (max_buf - sizeof(struct smb_hdr)) /
1181 sizeof(LOCKING_ANDX_RANGE);
Fabian Frederick4b99d392014-12-10 15:41:17 -08001182 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001183 if (!buf) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001184 free_xid(xid);
Pavel Shilovskye2f28862012-08-29 21:13:38 +04001185 return -ENOMEM;
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001186 }
1187
1188 for (i = 0; i < 2; i++) {
1189 cur = buf;
1190 num = 0;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001191 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001192 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 Shilovsky4b4de762012-09-18 16:20:26 -07001200 stored_rc = cifs_lockv(xid, tcon,
1201 cfile->fid.netfid,
Pavel Shilovsky04a6aa82012-02-28 14:16:55 +03001202 (__u8)li->type, 0, num,
1203 buf);
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001204 if (stored_rc)
1205 rc = stored_rc;
1206 cur = buf;
1207 num = 0;
1208 } else
1209 cur++;
1210 }
1211
1212 if (num) {
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001213 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
Pavel Shilovsky04a6aa82012-02-28 14:16:55 +03001214 (__u8)types[i], 0, num, buf);
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001215 if (stored_rc)
1216 rc = stored_rc;
1217 }
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001218 }
1219
Pavel Shilovsky32b9aaf2011-10-22 15:33:32 +04001220 kfree(buf);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001221 free_xid(xid);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001222 return rc;
1223}
1224
Jeff Layton3d224622016-05-24 06:27:44 -04001225static __u32
1226hash_lockowner(fl_owner_t owner)
1227{
1228 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1229}
1230
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001231struct 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 Shilovsky4f6bcec2011-10-22 15:33:30 +04001240static int
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001241cifs_push_posix_locks(struct cifsFileInfo *cfile)
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001242{
David Howells2b0143b2015-03-17 22:25:59 +00001243 struct inode *inode = d_inode(cfile->dentry);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001244 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001245 struct file_lock *flock;
1246 struct file_lock_context *flctx = inode->i_flctx;
Jeff Laytone084c1b2015-02-16 14:32:03 -05001247 unsigned int count = 0, i;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001248 int rc = 0, xid, type;
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001249 struct list_head locks_to_send, *el;
1250 struct lock_to_push *lck, *tmp;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001251 __u64 length;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001252
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001253 xid = get_xid();
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001254
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001255 if (!flctx)
1256 goto out;
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001257
Jeff Laytone084c1b2015-02-16 14:32:03 -05001258 spin_lock(&flctx->flc_lock);
1259 list_for_each(el, &flctx->flc_posix) {
1260 count++;
1261 }
1262 spin_unlock(&flctx->flc_lock);
1263
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001264 INIT_LIST_HEAD(&locks_to_send);
1265
1266 /*
Jeff Laytone084c1b2015-02-16 14:32:03 -05001267 * 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 Shilovskyce858522012-03-17 09:46:55 +03001269 * protects locking operations of this inode.
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001270 */
Jeff Laytone084c1b2015-02-16 14:32:03 -05001271 for (i = 0; i < count; i++) {
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001272 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 Shilovskyd5751462012-03-05 09:39:20 +03001280 el = locks_to_send.next;
Jeff Layton6109c852015-01-16 15:05:57 -05001281 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -05001282 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
Pavel Shilovskyce858522012-03-17 09:46:55 +03001283 if (el == &locks_to_send) {
1284 /*
1285 * The list ended. We don't have enough allocated
1286 * structures - something is really wrong.
1287 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001288 cifs_dbg(VFS, "Can't push all brlocks!\n");
Pavel Shilovskyce858522012-03-17 09:46:55 +03001289 break;
1290 }
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001291 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 Shilovskyd5751462012-03-05 09:39:20 +03001296 lck = list_entry(el, struct lock_to_push, llist);
Jeff Layton3d224622016-05-24 06:27:44 -04001297 lck->pid = hash_lockowner(flock->fl_owner);
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001298 lck->netfid = cfile->fid.netfid;
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001299 lck->length = length;
1300 lck->type = type;
1301 lck->offset = flock->fl_start;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001302 }
Jeff Layton6109c852015-01-16 15:05:57 -05001303 spin_unlock(&flctx->flc_lock);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001304
1305 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001306 int stored_rc;
1307
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001308 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
Jeff Laytonc5fd3632012-07-23 13:28:37 -04001309 lck->offset, lck->length, NULL,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001310 lck->type, 0);
1311 if (stored_rc)
1312 rc = stored_rc;
1313 list_del(&lck->llist);
1314 kfree(lck);
1315 }
1316
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001317out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001318 free_xid(xid);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001319 return rc;
Pavel Shilovskyd5751462012-03-05 09:39:20 +03001320err_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 Shilovsky4f6bcec2011-10-22 15:33:30 +04001326}
1327
1328static int
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001329cifs_push_locks(struct cifsFileInfo *cfile)
Pavel Shilovsky9ec3c882012-11-22 17:00:10 +04001330{
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001331 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
David Howells2b0143b2015-03-17 22:25:59 +00001332 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001333 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky9ec3c882012-11-22 17:00:10 +04001334 int rc = 0;
1335
1336 /* we are going to update can_cache_brlcks here - need a write access */
Dave Wysochanski80b42f42019-10-23 05:02:33 -04001337 cifs_down_write(&cinode->lock_sem);
Pavel Shilovsky9ec3c882012-11-22 17:00:10 +04001338 if (!cinode->can_cache_brlcks) {
1339 up_write(&cinode->lock_sem);
1340 return rc;
1341 }
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001342
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001343 if (cap_unix(tcon->ses) &&
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001344 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1345 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001346 rc = cifs_push_posix_locks(cfile);
1347 else
1348 rc = tcon->ses->server->ops->push_mand_locks(cfile);
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001349
Pavel Shilovskyb8db9282012-11-22 17:07:16 +04001350 cinode->can_cache_brlcks = false;
1351 up_write(&cinode->lock_sem);
1352 return rc;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001353}
1354
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001355static void
Pavel Shilovsky04a6aa82012-02-28 14:16:55 +03001356cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001357 bool *wait_flag, struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001359 if (flock->fl_flags & FL_POSIX)
Joe Perchesf96637b2013-05-04 22:12:25 -05001360 cifs_dbg(FYI, "Posix\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001361 if (flock->fl_flags & FL_FLOCK)
Joe Perchesf96637b2013-05-04 22:12:25 -05001362 cifs_dbg(FYI, "Flock\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001363 if (flock->fl_flags & FL_SLEEP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001364 cifs_dbg(FYI, "Blocking lock\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001365 *wait_flag = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001367 if (flock->fl_flags & FL_ACCESS)
Joe Perchesf96637b2013-05-04 22:12:25 -05001368 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001369 if (flock->fl_flags & FL_LEASE)
Joe Perchesf96637b2013-05-04 22:12:25 -05001370 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001371 if (flock->fl_flags &
Jeff Layton3d6d8542012-09-19 06:22:46 -07001372 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1373 FL_ACCESS | FL_LEASE | FL_CLOSE)))
Joe Perchesf96637b2013-05-04 22:12:25 -05001374 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001376 *type = server->vals->large_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001377 if (flock->fl_type == F_WRLCK) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001378 cifs_dbg(FYI, "F_WRLCK\n");
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001379 *type |= server->vals->exclusive_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001380 *lock = 1;
1381 } else if (flock->fl_type == F_UNLCK) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001382 cifs_dbg(FYI, "F_UNLCK\n");
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001383 *type |= server->vals->unlock_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001384 *unlock = 1;
1385 /* Check if unlock includes more than one lock range */
1386 } else if (flock->fl_type == F_RDLCK) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001387 cifs_dbg(FYI, "F_RDLCK\n");
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001388 *type |= server->vals->shared_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001389 *lock = 1;
1390 } else if (flock->fl_type == F_EXLCK) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001391 cifs_dbg(FYI, "F_EXLCK\n");
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001392 *type |= server->vals->exclusive_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001393 *lock = 1;
1394 } else if (flock->fl_type == F_SHLCK) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001395 cifs_dbg(FYI, "F_SHLCK\n");
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001396 *type |= server->vals->shared_lock_type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001397 *lock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05001399 cifs_dbg(FYI, "Unknown type of lock\n");
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001402static int
Pavel Shilovsky04a6aa82012-02-28 14:16:55 +03001403cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001404 bool wait_flag, bool posix_lck, unsigned int xid)
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001405{
1406 int rc = 0;
1407 __u64 length = 1 + flock->fl_end - flock->fl_start;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001408 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1409 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001410 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001411 __u16 netfid = cfile->fid.netfid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001413 if (posix_lck) {
1414 int posix_lock_type;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001415
1416 rc = cifs_posix_lock_test(file, flock);
1417 if (!rc)
1418 return rc;
1419
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001420 if (type & server->vals->shared_lock_type)
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001421 posix_lock_type = CIFS_RDLCK;
1422 else
1423 posix_lock_type = CIFS_WRLCK;
Jeff Layton3d224622016-05-24 06:27:44 -04001424 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1425 hash_lockowner(flock->fl_owner),
Jeff Laytonc5fd3632012-07-23 13:28:37 -04001426 flock->fl_start, length, flock,
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001427 posix_lock_type, wait_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return rc;
1429 }
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001430
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001431 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001432 if (!rc)
1433 return rc;
1434
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001435 /* BB we could chain these into one lock request BB */
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001436 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1437 1, 0, false);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001438 if (rc == 0) {
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001439 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1440 type, 0, 1, false);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001441 flock->fl_type = F_UNLCK;
1442 if (rc != 0)
Joe Perchesf96637b2013-05-04 22:12:25 -05001443 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1444 rc);
Pavel Shilovskya88b4702011-10-29 17:17:59 +04001445 return 0;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001446 }
1447
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001448 if (type & server->vals->shared_lock_type) {
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001449 flock->fl_type = F_WRLCK;
Pavel Shilovskya88b4702011-10-29 17:17:59 +04001450 return 0;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001451 }
1452
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001453 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 Shilovsky03776f42010-08-17 11:26:00 +04001458 if (rc == 0) {
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001459 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1460 type | server->vals->shared_lock_type, 0, 1, false);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001461 flock->fl_type = F_RDLCK;
1462 if (rc != 0)
Joe Perchesf96637b2013-05-04 22:12:25 -05001463 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1464 rc);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001465 } else
1466 flock->fl_type = F_WRLCK;
1467
Pavel Shilovskya88b4702011-10-29 17:17:59 +04001468 return 0;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001469}
1470
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001471void
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001472cifs_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 Shilovskyf7ba7fe2012-09-19 06:22:43 -07001479void
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001480cifs_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 Shilovskyd39a4f72012-09-19 06:22:43 -07001490int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001491cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1492 unsigned int xid)
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001493{
1494 int rc = 0, stored_rc;
Colin Ian King4d61eda2017-09-19 16:27:39 +01001495 static const int types[] = {
1496 LOCKING_ANDX_LARGE_FILES,
1497 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1498 };
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001499 unsigned int i;
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001500 unsigned int max_num, num, max_buf;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001501 LOCKING_ANDX_RANGE *buf, *cur;
1502 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
David Howells2b0143b2015-03-17 22:25:59 +00001503 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001504 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 Shilovsky0013fb42012-05-31 13:03:26 +04001510 /*
1511 * Accessing maxBuf is racy with cifs_reconnect - need to store value
Ross Lagerwall2a71a472019-01-08 18:30:57 +00001512 * and check it before using.
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001513 */
1514 max_buf = tcon->ses->server->maxBuf;
Ross Lagerwall2a71a472019-01-08 18:30:57 +00001515 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
Pavel Shilovsky0013fb42012-05-31 13:03:26 +04001516 return -EINVAL;
1517
Ross Lagerwall63715c12019-01-08 18:30:56 +00001518 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 Shilovsky0013fb42012-05-31 13:03:26 +04001522 max_num = (max_buf - sizeof(struct smb_hdr)) /
1523 sizeof(LOCKING_ANDX_RANGE);
Fabian Frederick4b99d392014-12-10 15:41:17 -08001524 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001525 if (!buf)
1526 return -ENOMEM;
1527
Dave Wysochanski80b42f42019-10-23 05:02:33 -04001528 cifs_down_write(&cinode->lock_sem);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001529 for (i = 0; i < 2; i++) {
1530 cur = buf;
1531 num = 0;
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001532 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001533 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 Shilovsky9ee305b2011-10-22 15:33:31 +04001539 if (types[i] != li->type)
1540 continue;
Pavel Shilovskyea319d52012-05-31 13:59:36 +04001541 if (cinode->can_cache_brlcks) {
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001542 /*
1543 * We can cache brlock requests - simply remove
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001544 * a lock from the file's list.
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001545 */
1546 list_del(&li->llist);
1547 cifs_del_lock_waiters(li);
1548 kfree(li);
Pavel Shilovskyea319d52012-05-31 13:59:36 +04001549 continue;
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001550 }
Pavel Shilovskyea319d52012-05-31 13:59:36 +04001551 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 Shilovsky4b4de762012-09-18 16:20:26 -07001563 stored_rc = cifs_lockv(xid, tcon,
1564 cfile->fid.netfid,
Pavel Shilovskyea319d52012-05-31 13:59:36 +04001565 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 Shilovskyf45d3412012-09-19 06:22:43 -07001573 &cfile->llist->locks);
Pavel Shilovskyea319d52012-05-31 13:59:36 +04001574 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 Shilovsky9ee305b2011-10-22 15:33:31 +04001585 }
1586 if (num) {
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001587 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001588 types[i], num, 0, buf);
1589 if (stored_rc) {
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001590 cifs_move_llist(&tmp_llist,
1591 &cfile->llist->locks);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001592 rc = stored_rc;
1593 } else
1594 cifs_free_llist(&tmp_llist);
1595 }
1596 }
1597
Pavel Shilovsky1b4b55a2012-09-19 06:22:44 -07001598 up_write(&cinode->lock_sem);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001599 kfree(buf);
1600 return rc;
1601}
1602
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001603static int
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001604cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001605 bool wait_flag, bool posix_lck, int lock, int unlock,
1606 unsigned int xid)
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001607{
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 Shilovsky106dc532012-02-28 14:23:34 +03001612 struct TCP_Server_Info *server = tcon->ses->server;
David Howells2b0143b2015-03-17 22:25:59 +00001613 struct inode *inode = d_inode(cfile->dentry);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001614
1615 if (posix_lck) {
Steve French08547b02006-02-28 22:39:25 +00001616 int posix_lock_type;
Pavel Shilovsky4f6bcec2011-10-22 15:33:30 +04001617
1618 rc = cifs_posix_lock_set(file, flock);
1619 if (!rc || rc < 0)
1620 return rc;
1621
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001622 if (type & server->vals->shared_lock_type)
Steve French08547b02006-02-28 22:39:25 +00001623 posix_lock_type = CIFS_RDLCK;
1624 else
1625 posix_lock_type = CIFS_WRLCK;
Steve French50c2f752007-07-13 00:33:32 +00001626
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001627 if (unlock == 1)
Steve Frenchbeb84dc2006-03-03 23:36:34 +00001628 posix_lock_type = CIFS_UNLCK;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001629
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001630 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
Jeff Layton3d224622016-05-24 06:27:44 -04001631 hash_lockowner(flock->fl_owner),
1632 flock->fl_start, length,
Pavel Shilovskyf45d3412012-09-19 06:22:43 -07001633 NULL, posix_lock_type, wait_flag);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001634 goto out;
Jeremy Allison7ee1af72006-08-02 21:56:33 +00001635 }
1636
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001637 if (lock) {
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001638 struct cifsLockInfo *lock;
1639
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001640 lock = cifs_lock_init(flock->fl_start, length, type);
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001641 if (!lock)
1642 return -ENOMEM;
1643
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001644 rc = cifs_lock_add_if(cfile, lock, wait_flag);
Pavel Shilovsky21cb2d92012-11-22 18:56:39 +04001645 if (rc < 0) {
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001646 kfree(lock);
Pavel Shilovsky21cb2d92012-11-22 18:56:39 +04001647 return rc;
1648 }
1649 if (!rc)
Pavel Shilovsky85160e02011-10-22 15:33:29 +04001650 goto out;
1651
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +04001652 /*
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 Shilovsky18cceb62013-09-05 13:01:06 +04001659 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1660 CIFS_CACHE_READ(CIFS_I(inode))) {
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001661 cifs_zap_mapping(inode);
Joe Perchesf96637b2013-05-04 22:12:25 -05001662 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1663 inode);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001664 CIFS_I(inode)->oplock = 0;
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +04001665 }
1666
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001667 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1668 type, 1, 0, wait_flag);
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001669 if (rc) {
1670 kfree(lock);
Pavel Shilovsky21cb2d92012-11-22 18:56:39 +04001671 return rc;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001672 }
Pavel Shilovsky161ebf92011-10-29 17:17:58 +04001673
Pavel Shilovskyfbd35ac2012-02-24 15:41:06 +03001674 cifs_lock_add(cfile, lock);
Pavel Shilovsky9ee305b2011-10-22 15:33:31 +04001675 } else if (unlock)
Pavel Shilovskyd39a4f72012-09-19 06:22:43 -07001676 rc = server->ops->mand_unlock_range(cfile, flock, xid);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001677
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001678out:
Aurelien Aptel29386512019-03-14 18:44:16 +01001679 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 Coddington4f656362015-10-22 13:38:14 -04001691 rc = locks_lock_file_wait(file, flock);
Aurelien Aptel29386512019-03-14 18:44:16 +01001692 }
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001693 return rc;
1694}
1695
1696int 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 Shilovsky04a6aa82012-02-28 14:16:55 +03001707 __u32 type;
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001708
1709 rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001710 xid = get_xid();
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001711
Joe Perchesf96637b2013-05-04 22:12:25 -05001712 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 Shilovsky03776f42010-08-17 11:26:00 +04001715
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001716 cfile = (struct cifsFileInfo *)file->private_data;
1717 tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky106dc532012-02-28 14:23:34 +03001718
1719 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1720 tcon->ses->server);
1721
Al Viro7119e222014-10-22 00:25:12 -04001722 cifs_sb = CIFS_FILE_SB(file);
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07001723 netfid = cfile->fid.netfid;
Al Viro496ad9a2013-01-23 17:07:38 -05001724 cinode = CIFS_I(file_inode(file));
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001725
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001726 if (cap_unix(tcon->ses) &&
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001727 (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 Shilovsky4f6bcec2011-10-22 15:33:30 +04001735 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001736 free_xid(xid);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001737 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 Shilovsky6d5786a2012-06-20 11:21:16 +04001745 free_xid(xid);
Pavel Shilovsky03776f42010-08-17 11:26:00 +04001746 return -EOPNOTSUPP;
1747 }
1748
1749 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1750 xid);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001751 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return rc;
1753}
1754
Jeff Layton597b0272012-03-23 14:40:56 -04001755/*
1756 * update the file size (if needed) after a write. Should be called with
1757 * the inode->i_lock held
1758 */
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05001759void
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04001760cifs_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 Shilovskyba9ad7252012-09-18 16:20:30 -07001769static ssize_t
1770cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1771 size_t write_size, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 int rc = 0;
1774 unsigned int bytes_written = 0;
1775 unsigned int total_written;
1776 struct cifs_sb_info *cifs_sb;
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001777 struct cifs_tcon *tcon;
1778 struct TCP_Server_Info *server;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001779 unsigned int xid;
Jeff Layton7da4b492010-10-15 15:34:00 -04001780 struct dentry *dentry = open_file->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00001781 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001782 struct cifs_io_parms io_parms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Jeff Layton7da4b492010-10-15 15:34:00 -04001784 cifs_sb = CIFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Al Viro35c265e2014-08-19 20:25:34 -04001786 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1787 write_size, *offset, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001789 tcon = tlink_tcon(open_file->tlink);
1790 server = tcon->ses->server;
1791
1792 if (!server->ops->sync_write)
1793 return -ENOSYS;
Steve French50c2f752007-07-13 00:33:32 +00001794
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001795 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 for (total_written = 0; write_size > total_written;
1798 total_written += bytes_written) {
1799 rc = -EAGAIN;
1800 while (rc == -EAGAIN) {
Jeff Laytonca83ce32011-04-12 09:13:44 -04001801 struct kvec iov[2];
1802 unsigned int len;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (open_file->invalidHandle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* we could deadlock if we called
1806 filemap_fdatawait from here so tell
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001807 reopen_file not to flush data to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 server now */
Jeff Layton15886172010-10-15 15:33:59 -04001809 rc = cifs_reopen_file(open_file, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (rc != 0)
1811 break;
1812 }
Steve French3e844692005-10-03 13:37:24 -07001813
David Howells2b0143b2015-03-17 22:25:59 +00001814 len = min(server->ops->wp_retry_size(d_inode(dentry)),
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001815 (unsigned int)write_size - total_written);
Jeff Laytonca83ce32011-04-12 09:13:44 -04001816 /* iov[0] is reserved for smb header */
1817 iov[1].iov_base = (char *)write_data + total_written;
1818 iov[1].iov_len = len;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001819 io_parms.pid = pid;
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001820 io_parms.tcon = tcon;
1821 io_parms.offset = *offset;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001822 io_parms.length = len;
Steve Frenchdb8b6312014-09-22 05:13:55 -05001823 rc = server->ops->sync_write(xid, &open_file->fid,
1824 &io_parms, &bytes_written, iov, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 if (rc || (bytes_written == 0)) {
1827 if (total_written)
1828 break;
1829 else {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001830 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return rc;
1832 }
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04001833 } else {
David Howells2b0143b2015-03-17 22:25:59 +00001834 spin_lock(&d_inode(dentry)->i_lock);
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001835 cifs_update_eof(cifsi, *offset, bytes_written);
David Howells2b0143b2015-03-17 22:25:59 +00001836 spin_unlock(&d_inode(dentry)->i_lock);
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001837 *offset += bytes_written;
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04001838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840
Pavel Shilovskyba9ad7252012-09-18 16:20:30 -07001841 cifs_stats_bytes_written(tcon, total_written);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jeff Layton7da4b492010-10-15 15:34:00 -04001843 if (total_written > 0) {
David Howells2b0143b2015-03-17 22:25:59 +00001844 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 Torvalds1da177e2005-04-16 15:20:36 -07001848 }
David Howells2b0143b2015-03-17 22:25:59 +00001849 mark_inode_dirty_sync(d_inode(dentry));
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001850 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 return total_written;
1852}
1853
Jeff Layton6508d902010-09-29 19:51:11 -04001854struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1855 bool fsuid_only)
Steve French630f3f0c2007-10-25 21:17:17 +00001856{
1857 struct cifsFileInfo *open_file = NULL;
Jeff Layton6508d902010-09-29 19:51:11 -04001858 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 French630f3f0c2007-10-25 21:17:17 +00001863
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001864 spin_lock(&cifs_inode->open_file_lock);
Steve French630f3f0c2007-10-25 21:17:17 +00001865 /* 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. Biedermanfef59fd2013-02-06 02:23:02 -08001869 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
Jeff Layton6508d902010-09-29 19:51:11 -04001870 continue;
Jeff Layton2e396b82010-10-15 15:34:01 -04001871 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
Steve French630f3f0c2007-10-25 21:17:17 +00001872 if (!open_file->invalidHandle) {
1873 /* found a good file */
1874 /* lock it so it will not be closed on us */
Steve French3afca262016-09-22 18:58:16 -05001875 cifsFileInfo_get(open_file);
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001876 spin_unlock(&cifs_inode->open_file_lock);
Steve French630f3f0c2007-10-25 21:17:17 +00001877 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 Wysochanskia8de7092019-10-03 15:16:27 +10001884 spin_unlock(&cifs_inode->open_file_lock);
Steve French630f3f0c2007-10-25 21:17:17 +00001885 return NULL;
1886}
Steve French630f3f0c2007-10-25 21:17:17 +00001887
Jeff Layton6508d902010-09-29 19:51:11 -04001888struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
1889 bool fsuid_only)
Steve French6148a742005-10-05 12:23:19 -07001890{
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001891 struct cifsFileInfo *open_file, *inv_file = NULL;
Jeff Laytond3892292010-11-02 16:22:50 -04001892 struct cifs_sb_info *cifs_sb;
Jeff Layton2846d382008-09-22 21:33:33 -04001893 bool any_available = false;
Steve Frenchdd99cd82005-10-05 19:32:49 -07001894 int rc;
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001895 unsigned int refind = 0;
Steve French6148a742005-10-05 12:23:19 -07001896
Steve French60808232006-04-22 15:53:05 +00001897 /* 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 Frenchfb8c4b12007-07-10 01:16:18 +00001901 if (cifs_inode == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001902 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
Steve French60808232006-04-22 15:53:05 +00001903 dump_stack();
1904 return NULL;
1905 }
1906
Jeff Laytond3892292010-11-02 16:22:50 -04001907 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1908
Jeff Layton6508d902010-09-29 19:51:11 -04001909 /* only filter by fsuid on multiuser mounts */
1910 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1911 fsuid_only = false;
1912
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001913 spin_lock(&cifs_inode->open_file_lock);
Steve French9b22b0b2007-10-02 01:11:08 +00001914refind_writable:
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001915 if (refind > MAX_REOPEN_ATT) {
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001916 spin_unlock(&cifs_inode->open_file_lock);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001917 return NULL;
1918 }
Steve French6148a742005-10-05 12:23:19 -07001919 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
Jeff Layton6508d902010-09-29 19:51:11 -04001920 if (!any_available && open_file->pid != current->tgid)
1921 continue;
Eric W. Biedermanfef59fd2013-02-06 02:23:02 -08001922 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
Jeff Layton6508d902010-09-29 19:51:11 -04001923 continue;
Jeff Layton2e396b82010-10-15 15:34:01 -04001924 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
Steve French9b22b0b2007-10-02 01:11:08 +00001925 if (!open_file->invalidHandle) {
1926 /* found a good writable file */
Steve French3afca262016-09-22 18:58:16 -05001927 cifsFileInfo_get(open_file);
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001928 spin_unlock(&cifs_inode->open_file_lock);
Steve French9b22b0b2007-10-02 01:11:08 +00001929 return open_file;
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001930 } else {
1931 if (!inv_file)
1932 inv_file = open_file;
Steve French9b22b0b2007-10-02 01:11:08 +00001933 }
Steve French6148a742005-10-05 12:23:19 -07001934 }
1935 }
Jeff Layton2846d382008-09-22 21:33:33 -04001936 /* 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 Pargaonkar2c0c2a02012-05-21 09:20:12 -05001941
1942 if (inv_file) {
1943 any_available = false;
Steve French3afca262016-09-22 18:58:16 -05001944 cifsFileInfo_get(inv_file);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001945 }
1946
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001947 spin_unlock(&cifs_inode->open_file_lock);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001948
1949 if (inv_file) {
1950 rc = cifs_reopen_file(inv_file, false);
1951 if (!rc)
1952 return inv_file;
1953 else {
Ronnie Sahlbergacc07942019-06-05 10:38:38 +10001954 spin_lock(&cifs_inode->open_file_lock);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001955 list_move_tail(&inv_file->flist,
1956 &cifs_inode->openFileList);
Ronnie Sahlbergacc07942019-06-05 10:38:38 +10001957 spin_unlock(&cifs_inode->open_file_lock);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001958 cifsFileInfo_put(inv_file);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001959 ++refind;
David Disseldorpe1e9bda2015-03-13 14:20:29 +01001960 inv_file = NULL;
Dave Wysochanskia8de7092019-10-03 15:16:27 +10001961 spin_lock(&cifs_inode->open_file_lock);
Shirish Pargaonkar2c0c2a02012-05-21 09:20:12 -05001962 goto refind_writable;
1963 }
1964 }
1965
Steve French6148a742005-10-05 12:23:19 -07001966 return NULL;
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
1970{
1971 struct address_space *mapping = page->mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001972 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 char *write_data;
1974 int rc = -EFAULT;
1975 int bytes_written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct inode *inode;
Steve French6148a742005-10-05 12:23:19 -07001977 struct cifsFileInfo *open_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 if (!mapping || !mapping->host)
1980 return -EFAULT;
1981
1982 inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 offset += (loff_t)from;
1985 write_data = kmap(page);
1986 write_data += from;
1987
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 if ((to > PAGE_SIZE) || (from > to)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 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 Frenchfb8c4b12007-07-10 01:16:18 +00002001 to = (unsigned)(mapping->host->i_size - offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Jeff Layton6508d902010-09-29 19:51:11 -04002003 open_file = find_writable_file(CIFS_I(mapping->host), false);
Steve French6148a742005-10-05 12:23:19 -07002004 if (open_file) {
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002005 bytes_written = cifs_write(open_file, open_file->pid,
2006 write_data, to - from, &offset);
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04002007 cifsFileInfo_put(open_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* Does mm or vfs already set times? */
Deepa Dinamanic2050a42016-09-14 07:48:06 -07002009 inode->i_atime = inode->i_mtime = current_time(inode);
Steve Frenchbb5a9a02007-12-31 04:21:29 +00002010 if ((bytes_written > 0) && (offset))
Steve French6148a742005-10-05 12:23:19 -07002011 rc = 0;
Steve Frenchbb5a9a02007-12-31 04:21:29 +00002012 else if (bytes_written < 0)
2013 rc = bytes_written;
Steve French6148a742005-10-05 12:23:19 -07002014 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05002015 cifs_dbg(FYI, "No writeable filehandles for inode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 rc = -EIO;
2017 }
2018
2019 kunmap(page);
2020 return rc;
2021}
2022
Pavel Shilovsky90ac1382014-06-19 16:11:00 +04002023static struct cifs_writedata *
2024wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2025 pgoff_t end, pgoff_t *index,
2026 unsigned int *found_pages)
2027{
Pavel Shilovsky90ac1382014-06-19 16:11:00 +04002028 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 Kara9c19a9c2017-11-15 17:35:26 -08002035 *found_pages = find_get_pages_range_tag(mapping, index, end,
2036 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
Pavel Shilovsky90ac1382014-06-19 16:11:00 +04002037 return wdata;
2038}
2039
Pavel Shilovsky7e48ff82014-06-19 15:01:03 +04002040static unsigned int
2041wdata_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 Wilcoxb93b0162018-04-10 16:36:56 -07002052 * 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 Shilovsky7e48ff82014-06-19 15:01:03 +04002056 */
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. Shutemov09cbfea2016-04-01 15:29:47 +03002112 put_page(wdata->pages[i]);
Pavel Shilovsky7e48ff82014-06-19 15:01:03 +04002113 wdata->pages[i] = NULL;
2114 }
2115
2116 return nr_pages;
2117}
2118
Pavel Shilovsky619aa482014-06-19 15:28:37 +04002119static int
2120wdata_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. Shutemov09cbfea2016-04-01 15:29:47 +03002130 wdata->pagesz = PAGE_SIZE;
Pavel Shilovsky619aa482014-06-19 15:28:37 +04002131 wdata->tailsz = min(i_size_read(mapping->host) -
2132 page_offset(wdata->pages[nr_pages - 1]),
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002133 (loff_t)PAGE_SIZE);
2134 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
Pavel Shilovsky619aa482014-06-19 15:28:37 +04002135
Pavel Shilovsky66231a42014-06-19 16:15:16 +04002136 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 Shilovsky619aa482014-06-19 15:28:37 +04002143 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 Shilovsky66231a42014-06-19 16:15:16 +04002146 }
Pavel Shilovsky619aa482014-06-19 15:28:37 +04002147
2148 for (i = 0; i < nr_pages; ++i)
2149 unlock_page(wdata->pages[i]);
2150
2151 return rc;
2152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154static int cifs_writepages(struct address_space *mapping,
Steve French37c0eb42005-10-05 14:50:29 -07002155 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002157 struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002158 struct TCP_Server_Info *server;
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002159 bool done = false, scanned = false, range_whole = false;
2160 pgoff_t end, index;
2161 struct cifs_writedata *wdata;
Steve French37c0eb42005-10-05 14:50:29 -07002162 int rc = 0;
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002163 int saved_rc = 0;
Steve French50c2f752007-07-13 00:33:32 +00002164
Steve French37c0eb42005-10-05 14:50:29 -07002165 /*
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002166 * If wsize is smaller than the page cache size, default to writing
Steve French37c0eb42005-10-05 14:50:29 -07002167 * one page at a time via cifs_writepage
2168 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002169 if (cifs_sb->wsize < PAGE_SIZE)
Steve French37c0eb42005-10-05 14:50:29 -07002170 return generic_writepages(mapping, wbc);
2171
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07002172 if (wbc->range_cyclic) {
Steve French37c0eb42005-10-05 14:50:29 -07002173 index = mapping->writeback_index; /* Start from prev offset */
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07002174 end = -1;
2175 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002176 index = wbc->range_start >> PAGE_SHIFT;
2177 end = wbc->range_end >> PAGE_SHIFT;
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07002178 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002179 range_whole = true;
2180 scanned = true;
Steve French37c0eb42005-10-05 14:50:29 -07002181 }
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002182 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
Steve French37c0eb42005-10-05 14:50:29 -07002183retry:
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002184 while (!done && index <= end) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002185 unsigned int i, nr_pages, found_pages, wsize, credits;
Pavel Shilovsky66231a42014-06-19 16:15:16 +04002186 pgoff_t next = 0, tofind, saved_index = index;
Steve French37c0eb42005-10-05 14:50:29 -07002187
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002188 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2189 &wsize, &credits);
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002190 if (rc != 0) {
2191 done = true;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002192 break;
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002193 }
Steve French37c0eb42005-10-05 14:50:29 -07002194
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002195 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
Steve French37c0eb42005-10-05 14:50:29 -07002196
Pavel Shilovsky90ac1382014-06-19 16:11:00 +04002197 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2198 &found_pages);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002199 if (!wdata) {
2200 rc = -ENOMEM;
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002201 done = true;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002202 add_credits_and_wake_if(server, credits, 0);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002203 break;
2204 }
2205
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002206 if (found_pages == 0) {
2207 kref_put(&wdata->refcount, cifs_writedata_release);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002208 add_credits_and_wake_if(server, credits, 0);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002209 break;
2210 }
2211
Pavel Shilovsky7e48ff82014-06-19 15:01:03 +04002212 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2213 end, &index, &next, &done);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002214
2215 /* nothing to write? */
2216 if (nr_pages == 0) {
2217 kref_put(&wdata->refcount, cifs_writedata_release);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002218 add_credits_and_wake_if(server, credits, 0);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002219 continue;
2220 }
2221
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002222 wdata->credits = credits;
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002223
Pavel Shilovsky619aa482014-06-19 15:28:37 +04002224 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
Jeff Layton941b8532011-01-11 07:24:01 -05002225
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002226 /* send failure -- clean up the mess */
2227 if (rc != 0) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002228 add_credits_and_wake_if(server, wdata->credits, 0);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002229 for (i = 0; i < nr_pages; ++i) {
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002230 if (is_retryable_error(rc))
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002231 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. Shutemov09cbfea2016-04-01 15:29:47 +03002236 put_page(wdata->pages[i]);
Steve French37c0eb42005-10-05 14:50:29 -07002237 }
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002238 if (!is_retryable_error(rc))
Jeff Layton941b8532011-01-11 07:24:01 -05002239 mapping_set_error(mapping, rc);
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002240 }
2241 kref_put(&wdata->refcount, cifs_writedata_release);
Jeff Layton941b8532011-01-11 07:24:01 -05002242
Pavel Shilovsky66231a42014-06-19 16:15:16 +04002243 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2244 index = saved_index;
2245 continue;
2246 }
2247
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002248 /* 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 Laytonc3d17b62011-05-19 16:22:57 -04002257 wbc->nr_to_write -= nr_pages;
2258 if (wbc->nr_to_write <= 0)
2259 done = true;
Dave Kleikampb066a482008-11-18 03:49:05 +00002260
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002261 index = next;
Steve French37c0eb42005-10-05 14:50:29 -07002262 }
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002263
Steve French37c0eb42005-10-05 14:50:29 -07002264 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 Laytonc3d17b62011-05-19 16:22:57 -04002269 scanned = true;
Steve French37c0eb42005-10-05 14:50:29 -07002270 index = 0;
2271 goto retry;
2272 }
Jeff Laytonc3d17b62011-05-19 16:22:57 -04002273
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002274 if (saved_rc != 0)
2275 rc = saved_rc;
2276
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -07002277 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
Steve French37c0eb42005-10-05 14:50:29 -07002278 mapping->writeback_index = index;
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return rc;
2281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002283static int
2284cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002286 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002287 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002289 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290/* BB add check for wbc flags */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002291 get_page(page);
Steve Frenchad7a2922008-02-07 23:25:02 +00002292 if (!PageUptodate(page))
Joe Perchesf96637b2013-05-04 22:12:25 -05002293 cifs_dbg(FYI, "ppw - page not up to date\n");
Linus Torvaldscb876f42006-12-23 16:19:07 -08002294
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 Frenchfb8c4b12007-07-10 01:16:18 +00002305 set_page_writeback(page);
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002306retry_write:
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002307 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
Pavel Shilovskyfb2dabe2019-01-08 11:15:28 -08002308 if (is_retryable_error(rc)) {
2309 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
Jeff Layton97b37f22017-05-25 06:59:52 -04002310 goto retry_write;
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002311 redirty_page_for_writepage(wbc, page);
Jeff Layton97b37f22017-05-25 06:59:52 -04002312 } else if (rc != 0) {
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002313 SetPageError(page);
Jeff Layton97b37f22017-05-25 06:59:52 -04002314 mapping_set_error(page->mapping, rc);
2315 } else {
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002316 SetPageUptodate(page);
Jeff Layton97b37f22017-05-25 06:59:52 -04002317 }
Linus Torvaldscb876f42006-12-23 16:19:07 -08002318 end_page_writeback(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002319 put_page(page);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002320 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return rc;
2322}
2323
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04002324static 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 Piggind9414772008-09-24 11:32:59 -04002331static 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 Torvalds1da177e2005-04-16 15:20:36 -07002334{
Nick Piggind9414772008-09-24 11:32:59 -04002335 int rc;
2336 struct inode *inode = mapping->host;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00002337 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 Torvalds1da177e2005-04-16 15:20:36 -07002345
Joe Perchesf96637b2013-05-04 22:12:25 -05002346 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00002347 page, pos, copied);
Steve Frenchad7a2922008-02-07 23:25:02 +00002348
Jeff Laytona98ee8c2008-11-26 19:32:33 +00002349 if (PageChecked(page)) {
2350 if (copied == len)
2351 SetPageUptodate(page);
2352 ClearPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002353 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
Nick Piggind9414772008-09-24 11:32:59 -04002354 SetPageUptodate(page);
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (!PageUptodate(page)) {
Nick Piggind9414772008-09-24 11:32:59 -04002357 char *page_data;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002358 unsigned offset = pos & (PAGE_SIZE - 1);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002359 unsigned int xid;
Nick Piggind9414772008-09-24 11:32:59 -04002360
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002361 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /* 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 Shilovskyd4ffff12011-05-26 06:02:00 +00002368 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
Nick Piggind9414772008-09-24 11:32:59 -04002369 /* if (rc < 0) should we set writebehind rc? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 kunmap(page);
Nick Piggind9414772008-09-24 11:32:59 -04002371
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002372 free_xid(xid);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002373 } else {
Nick Piggind9414772008-09-24 11:32:59 -04002374 rc = copied;
2375 pos += copied;
Pavel Shilovskyca8aa292012-12-21 15:05:47 +04002376 set_page_dirty(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 }
2378
Nick Piggind9414772008-09-24 11:32:59 -04002379 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. Shutemov09cbfea2016-04-01 15:29:47 +03002387 put_page(page);
Nick Piggind9414772008-09-24 11:32:59 -04002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return rc;
2390}
2391
Josef Bacik02c24a82011-07-16 20:44:56 -04002392int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2393 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002395 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 int rc = 0;
Steve French96daf2b2011-05-27 04:34:02 +00002397 struct cifs_tcon *tcon;
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -07002398 struct TCP_Server_Info *server;
Joe Perchesc21dfb62010-07-12 13:50:14 -07002399 struct cifsFileInfo *smbfile = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -05002400 struct inode *inode = file_inode(file);
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002401 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jeff Layton3b49c9a2017-07-07 15:20:52 -04002403 rc = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -04002404 if (rc)
2405 return rc;
Al Viro59551022016-01-22 15:40:57 -05002406 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -04002407
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002408 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Al Viro35c265e2014-08-19 20:25:34 -04002410 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2411 file, datasync);
Steve French50c2f752007-07-13 00:33:32 +00002412
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002413 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
Jeff Layton4f73c7d2014-04-30 09:31:47 -04002414 rc = cifs_zap_mapping(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002415 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002416 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04002417 rc = 0; /* don't care about it in fsync */
2418 }
2419 }
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002420
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002421 tcon = tlink_tcon(smbfile->tlink);
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -07002422 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 Shilovsky8be7e6b2010-12-12 13:11:13 +03002429
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002430 free_xid(xid);
Al Viro59551022016-01-22 15:40:57 -05002431 inode_unlock(inode);
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002432 return rc;
2433}
2434
Josef Bacik02c24a82011-07-16 20:44:56 -04002435int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002436{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002437 unsigned int xid;
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002438 int rc = 0;
Steve French96daf2b2011-05-27 04:34:02 +00002439 struct cifs_tcon *tcon;
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -07002440 struct TCP_Server_Info *server;
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002441 struct cifsFileInfo *smbfile = file->private_data;
Al Viro7119e222014-10-22 00:25:12 -04002442 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
Josef Bacik02c24a82011-07-16 20:44:56 -04002443 struct inode *inode = file->f_mapping->host;
2444
Jeff Layton3b49c9a2017-07-07 15:20:52 -04002445 rc = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -04002446 if (rc)
2447 return rc;
Al Viro59551022016-01-22 15:40:57 -05002448 inode_lock(inode);
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002449
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002450 xid = get_xid();
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002451
Al Viro35c265e2014-08-19 20:25:34 -04002452 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2453 file, datasync);
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +03002454
2455 tcon = tlink_tcon(smbfile->tlink);
Pavel Shilovsky1d8c4c02012-09-18 16:20:27 -07002456 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 Frenchb298f222009-02-21 21:17:43 +00002463
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002464 free_xid(xid);
Al Viro59551022016-01-22 15:40:57 -05002465 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return rc;
2467}
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469/*
2470 * As file closes, flush all cached write data for this inode checking
2471 * for write behind errors.
2472 */
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -07002473int cifs_flush(struct file *file, fl_owner_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Al Viro496ad9a2013-01-23 17:07:38 -05002475 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 int rc = 0;
2477
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002478 if (file->f_mode & FMODE_WRITE)
Jeff Laytond3f13222010-10-15 15:34:07 -04002479 rc = filemap_write_and_wait(inode->i_mapping);
Steve French50c2f752007-07-13 00:33:32 +00002480
Joe Perchesf96637b2013-05-04 22:12:25 -05002481 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 return rc;
2484}
2485
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002486static int
2487cifs_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 Laytone94f7ba2012-03-23 14:40:56 -04002493 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002494 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 Laytone94f7ba2012-03-23 14:40:56 -04002501 break;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002502 }
2503 }
2504
Jeff Laytone94f7ba2012-03-23 14:40:56 -04002505 if (rc) {
2506 for (i = 0; i < num_pages; i++)
2507 put_page(pages[i]);
2508 }
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002509 return rc;
2510}
2511
2512static inline
2513size_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 Laytona7103b92012-03-23 14:40:56 -04002519 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002520
2521 if (cur_len)
2522 *cur_len = clen;
2523
2524 return num_pages;
2525}
2526
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002527static void
Steve French4a5c80d2014-02-07 20:45:12 -06002528cifs_uncached_writedata_release(struct kref *refcount)
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002529{
2530 int i;
Steve French4a5c80d2014-02-07 20:45:12 -06002531 struct cifs_writedata *wdata = container_of(refcount,
2532 struct cifs_writedata, refcount);
2533
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002534 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
Steve French4a5c80d2014-02-07 20:45:12 -06002535 for (i = 0; i < wdata->nr_pages; i++)
2536 put_page(wdata->pages[i]);
2537 cifs_writedata_release(refcount);
2538}
2539
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002540static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
2541
Steve French4a5c80d2014-02-07 20:45:12 -06002542static void
2543cifs_uncached_writev_complete(struct work_struct *work)
2544{
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002545 struct cifs_writedata *wdata = container_of(work,
2546 struct cifs_writedata, work);
David Howells2b0143b2015-03-17 22:25:59 +00002547 struct inode *inode = d_inode(wdata->cfile->dentry);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002548 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 Shilovskyc610c4b2017-04-25 11:52:31 -07002557 collect_uncached_write_data(wdata->ctx);
2558 /* the below call can possibly free the last ref to aio ctx */
Steve French4a5c80d2014-02-07 20:45:12 -06002559 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002560}
2561
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002562static int
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002563wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2564 size_t *len, unsigned long *num_pages)
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002565{
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002566 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 Shilovsky43de94e2014-06-20 16:10:52 +04002604static int
2605cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2606 struct cifsFileInfo *open_file,
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002607 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
2608 struct cifs_aio_ctx *ctx)
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002609{
Pavel Shilovsky43de94e2014-06-20 16:10:52 +04002610 int rc = 0;
2611 size_t cur_len;
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002612 unsigned long nr_pages, num_pages, i;
Pavel Shilovsky43de94e2014-06-20 16:10:52 +04002613 struct cifs_writedata *wdata;
Al Virofc56b982016-09-21 18:18:23 -04002614 struct iov_iter saved_from = *from;
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002615 loff_t saved_offset = offset;
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002616 pid_t pid;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002617 struct TCP_Server_Info *server;
2618
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00002619 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2620 pid = open_file->pid;
2621 else
2622 pid = current->tgid;
2623
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002624 server = tlink_tcon(open_file->tlink)->ses->server;
Pavel Shilovsky76429c12011-01-31 16:03:08 +03002625
2626 do {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002627 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 Laytonda82f7e2012-03-23 14:40:56 -04002635 wdata = cifs_writedata_alloc(nr_pages,
2636 cifs_uncached_writev_complete);
2637 if (!wdata) {
2638 rc = -ENOMEM;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002639 add_credits_and_wake_if(server, credits, 0);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002640 break;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002641 }
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002642
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002643 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2644 if (rc) {
2645 kfree(wdata);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002646 add_credits_and_wake_if(server, credits, 0);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002647 break;
2648 }
2649
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002650 num_pages = nr_pages;
2651 rc = wdata_fill_from_iovec(wdata, from, &cur_len, &num_pages);
2652 if (rc) {
Jeff Layton5d81de82014-02-14 07:20:35 -05002653 for (i = 0; i < nr_pages; i++)
2654 put_page(wdata->pages[i]);
2655 kfree(wdata);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002656 add_credits_and_wake_if(server, credits, 0);
Jeff Layton5d81de82014-02-14 07:20:35 -05002657 break;
2658 }
2659
2660 /*
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002661 * Bring nr_pages down to the number of pages we actually used,
2662 * and free any pages that we didn't use.
Jeff Layton5d81de82014-02-14 07:20:35 -05002663 */
Pavel Shilovsky66386c02014-06-20 15:48:40 +04002664 for ( ; nr_pages > num_pages; nr_pages--)
Jeff Layton5d81de82014-02-14 07:20:35 -05002665 put_page(wdata->pages[nr_pages - 1]);
2666
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002667 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 Laytoneddb0792012-09-18 16:20:35 -07002673 wdata->pagesz = PAGE_SIZE;
2674 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002675 wdata->credits = credits;
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002676 wdata->ctx = ctx;
2677 kref_get(&ctx->refcount);
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002678
2679 if (!wdata->cfile->invalidHandle ||
Germano Percossi1fa839b2017-04-07 12:29:38 +01002680 !(rc = cifs_reopen_file(wdata->cfile, false)))
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002681 rc = server->ops->async_writev(wdata,
2682 cifs_uncached_writedata_release);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002683 if (rc) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002684 add_credits_and_wake_if(server, wdata->credits, 0);
Steve French4a5c80d2014-02-07 20:45:12 -06002685 kref_put(&wdata->refcount,
2686 cifs_uncached_writedata_release);
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002687 if (rc == -EAGAIN) {
Al Virofc56b982016-09-21 18:18:23 -04002688 *from = saved_from;
Pavel Shilovsky6ec0b012014-06-20 16:30:46 +04002689 iov_iter_advance(from, offset - saved_offset);
2690 continue;
2691 }
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002692 break;
2693 }
2694
Pavel Shilovsky43de94e2014-06-20 16:10:52 +04002695 list_add_tail(&wdata->list, wdata_list);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002696 offset += cur_len;
2697 len -= cur_len;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002698 } while (len > 0);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002699
2700 return rc;
2701}
2702
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002703static 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 */
2728restart_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 Viroe9d15932015-04-06 22:44:11 -04002784ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002785{
Al Viroe9d15932015-04-06 22:44:11 -04002786 struct file *file = iocb->ki_filp;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002787 ssize_t total_written = 0;
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002788 struct cifsFileInfo *cfile;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002789 struct cifs_tcon *tcon;
2790 struct cifs_sb_info *cifs_sb;
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002791 struct cifs_aio_ctx *ctx;
Al Virofc56b982016-09-21 18:18:23 -04002792 struct iov_iter saved_from = *from;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002793 int rc;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002794
Al Viroe9d15932015-04-06 22:44:11 -04002795 /*
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 Viro3309dd02015-04-09 12:55:47 -04002801 rc = generic_write_checks(iocb, from);
2802 if (rc <= 0)
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002803 return rc;
2804
Al Viro7119e222014-10-22 00:25:12 -04002805 cifs_sb = CIFS_FILE_SB(file);
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002806 cfile = file->private_data;
2807 tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002808
2809 if (!tcon->ses->server->ops->async_writev)
2810 return -ENOSYS;
2811
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002812 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 Shilovsky72432ff2011-01-24 14:16:35 -05002834
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002835 /*
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 Shilovskyc610c4b2017-04-25 11:52:31 -07002841 if (!list_empty(&ctx->list))
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002842 rc = 0;
2843
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002844 mutex_unlock(&ctx->aio_mutex);
Jeff Laytonda82f7e2012-03-23 14:40:56 -04002845
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002846 if (rc) {
2847 kref_put(&ctx->refcount, cifs_aio_ctx_release);
2848 return rc;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002849 }
2850
Pavel Shilovskyc610c4b2017-04-25 11:52:31 -07002851 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 Viroe9d15932015-04-06 22:44:11 -04002869 if (unlikely(!total_written))
2870 return rc;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002871
Al Viroe9d15932015-04-06 22:44:11 -04002872 iocb->ki_pos += total_written;
Al Viroe9d15932015-04-06 22:44:11 -04002873 return total_written;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002874}
2875
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002876static ssize_t
Al Viro3dae8752014-04-03 12:05:17 -04002877cifs_writev(struct kiocb *iocb, struct iov_iter *from)
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002878{
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002879 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 Viro5f380c72015-04-07 11:28:12 -04002884 ssize_t rc;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002885
Rabin Vincent966681c2017-06-29 16:01:42 +02002886 inode_lock(inode);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002887 /*
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 Viro5f380c72015-04-07 11:28:12 -04002892
Al Viro3309dd02015-04-09 12:55:47 -04002893 rc = generic_write_checks(iocb, from);
2894 if (rc <= 0)
Al Viro5f380c72015-04-07 11:28:12 -04002895 goto out;
2896
Al Viro5f380c72015-04-07 11:28:12 -04002897 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002898 server->vals->exclusive_lock_type, NULL,
Al Viro5f380c72015-04-07 11:28:12 -04002899 CIFS_WRITE_OP))
Al Viro3dae8752014-04-03 12:05:17 -04002900 rc = __generic_file_write_iter(iocb, from);
Al Viro5f380c72015-04-07 11:28:12 -04002901 else
2902 rc = -EACCES;
2903out:
Rabin Vincent966681c2017-06-29 16:01:42 +02002904 up_read(&cinode->lock_sem);
Al Viro59551022016-01-22 15:40:57 -05002905 inode_unlock(inode);
Al Viro19dfc1f2014-04-03 10:27:17 -04002906
Christoph Hellwige2592212016-04-07 08:52:01 -07002907 if (rc > 0)
2908 rc = generic_write_sync(iocb, rc);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002909 return rc;
2910}
2911
2912ssize_t
Al Viro3dae8752014-04-03 12:05:17 -04002913cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002914{
Al Viro496ad9a2013-01-23 17:07:38 -05002915 struct inode *inode = file_inode(iocb->ki_filp);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07002916 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 Shilovsky88cf75a2012-12-21 15:07:52 +04002921 ssize_t written;
Pavel Shilovskyca8aa292012-12-21 15:05:47 +04002922
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002923 written = cifs_get_writer(cinode);
2924 if (written)
2925 return written;
2926
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002927 if (CIFS_CACHE_WRITE(cinode)) {
Pavel Shilovsky88cf75a2012-12-21 15:07:52 +04002928 if (cap_unix(tcon->ses) &&
2929 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002930 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
Al Viro3dae8752014-04-03 12:05:17 -04002931 written = generic_file_write_iter(iocb, from);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002932 goto out;
2933 }
Al Viro3dae8752014-04-03 12:05:17 -04002934 written = cifs_writev(iocb, from);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002935 goto out;
Pavel Shilovskyc299dd02012-12-06 22:07:52 +04002936 }
Pavel Shilovskyca8aa292012-12-21 15:05:47 +04002937 /*
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 Viro3dae8752014-04-03 12:05:17 -04002943 written = cifs_user_writev(iocb, from);
Pavel Shilovsky43eaa6c2019-03-04 17:48:01 -08002944 if (CIFS_CACHE_READ(cinode)) {
Pavel Shilovsky88cf75a2012-12-21 15:07:52 +04002945 /*
Pavel Shilovsky43eaa6c2019-03-04 17:48:01 -08002946 * 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 Shilovsky88cf75a2012-12-21 15:07:52 +04002951 */
Jeff Layton4f73c7d2014-04-30 09:31:47 -04002952 cifs_zap_mapping(inode);
Pavel Shilovsky43eaa6c2019-03-04 17:48:01 -08002953 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05002954 inode);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002955 cinode->oplock = 0;
Pavel Shilovsky88cf75a2012-12-21 15:07:52 +04002956 }
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002957out:
2958 cifs_put_writer(cinode);
Pavel Shilovsky88cf75a2012-12-21 15:07:52 +04002959 return written;
Pavel Shilovsky72432ff2011-01-24 14:16:35 -05002960}
2961
Jeff Layton0471ca32012-05-16 07:13:16 -04002962static struct cifs_readdata *
Long Lif9f5aca2018-05-30 12:47:54 -07002963cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
Jeff Layton0471ca32012-05-16 07:13:16 -04002964{
2965 struct cifs_readdata *rdata;
Jeff Laytonf4e49cd2012-09-18 16:20:36 -07002966
Long Lif9f5aca2018-05-30 12:47:54 -07002967 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
Jeff Layton0471ca32012-05-16 07:13:16 -04002968 if (rdata != NULL) {
Long Lif9f5aca2018-05-30 12:47:54 -07002969 rdata->pages = pages;
Jeff Layton6993f742012-05-16 07:13:17 -04002970 kref_init(&rdata->refcount);
Jeff Layton1c892542012-05-16 07:13:17 -04002971 INIT_LIST_HEAD(&rdata->list);
2972 init_completion(&rdata->done);
Jeff Layton0471ca32012-05-16 07:13:16 -04002973 INIT_WORK(&rdata->work, complete);
Jeff Layton0471ca32012-05-16 07:13:16 -04002974 }
Jeff Laytonf4e49cd2012-09-18 16:20:36 -07002975
Jeff Layton0471ca32012-05-16 07:13:16 -04002976 return rdata;
2977}
2978
Long Lif9f5aca2018-05-30 12:47:54 -07002979static struct cifs_readdata *
2980cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
2981{
2982 struct page **pages =
Kees Cook6396bb22018-06-12 14:03:40 -07002983 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
Long Lif9f5aca2018-05-30 12:47:54 -07002984 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 Layton6993f742012-05-16 07:13:17 -04002995void
2996cifs_readdata_release(struct kref *refcount)
Jeff Layton0471ca32012-05-16 07:13:16 -04002997{
Jeff Layton6993f742012-05-16 07:13:17 -04002998 struct cifs_readdata *rdata = container_of(refcount,
2999 struct cifs_readdata, refcount);
Long Libd3dcc62017-11-22 17:38:47 -07003000#ifdef CONFIG_CIFS_SMB_DIRECT
3001 if (rdata->mr) {
3002 smbd_deregister_mr(rdata->mr);
3003 rdata->mr = NULL;
3004 }
3005#endif
Jeff Layton6993f742012-05-16 07:13:17 -04003006 if (rdata->cfile)
3007 cifsFileInfo_put(rdata->cfile);
3008
Long Lif9f5aca2018-05-30 12:47:54 -07003009 kvfree(rdata->pages);
Jeff Layton0471ca32012-05-16 07:13:16 -04003010 kfree(rdata);
3011}
3012
Jeff Layton2a1bb132012-05-16 07:13:17 -04003013static int
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003014cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
Jeff Layton1c892542012-05-16 07:13:17 -04003015{
3016 int rc = 0;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003017 struct page *page;
Jeff Layton1c892542012-05-16 07:13:17 -04003018 unsigned int i;
3019
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003020 for (i = 0; i < nr_pages; i++) {
Jeff Layton1c892542012-05-16 07:13:17 -04003021 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3022 if (!page) {
3023 rc = -ENOMEM;
3024 break;
3025 }
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003026 rdata->pages[i] = page;
Jeff Layton1c892542012-05-16 07:13:17 -04003027 }
3028
3029 if (rc) {
Roberto Bergantinos Corpas297a2512019-05-28 09:38:14 +02003030 unsigned int nr_page_failed = i;
3031
3032 for (i = 0; i < nr_page_failed; i++) {
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003033 put_page(rdata->pages[i]);
3034 rdata->pages[i] = NULL;
Jeff Layton1c892542012-05-16 07:13:17 -04003035 }
3036 }
3037 return rc;
3038}
3039
3040static void
3041cifs_uncached_readdata_release(struct kref *refcount)
3042{
Jeff Layton1c892542012-05-16 07:13:17 -04003043 struct cifs_readdata *rdata = container_of(refcount,
3044 struct cifs_readdata, refcount);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003045 unsigned int i;
Jeff Layton1c892542012-05-16 07:13:17 -04003046
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003047 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003048 for (i = 0; i < rdata->nr_pages; i++) {
3049 put_page(rdata->pages[i]);
3050 rdata->pages[i] = NULL;
Jeff Layton1c892542012-05-16 07:13:17 -04003051 }
3052 cifs_readdata_release(refcount);
3053}
3054
Jeff Layton1c892542012-05-16 07:13:17 -04003055/**
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 Viro7f25bba2014-02-04 14:07:43 -05003058 * @iter: destination for our data
Jeff Layton1c892542012-05-16 07:13:17 -04003059 *
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 Viro7f25bba2014-02-04 14:07:43 -05003064static int
3065cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
Jeff Layton1c892542012-05-16 07:13:17 -04003066{
Pavel Shilovsky34a54d62014-07-10 10:03:29 +04003067 size_t remaining = rdata->got_bytes;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003068 unsigned int i;
Jeff Layton1c892542012-05-16 07:13:17 -04003069
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003070 for (i = 0; i < rdata->nr_pages; i++) {
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003071 struct page *page = rdata->pages[i];
Geert Uytterhoevene686bd82014-04-13 20:46:21 +02003072 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
Pavel Shilovsky9c257022017-01-19 13:53:15 -08003073 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 Viro7f25bba2014-02-04 14:07:43 -05003082 remaining -= written;
3083 if (written < copy && iov_iter_count(iter) > 0)
3084 break;
Jeff Layton1c892542012-05-16 07:13:17 -04003085 }
Al Viro7f25bba2014-02-04 14:07:43 -05003086 return remaining ? -EFAULT : 0;
Jeff Layton1c892542012-05-16 07:13:17 -04003087}
3088
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003089static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3090
Jeff Layton1c892542012-05-16 07:13:17 -04003091static void
3092cifs_uncached_readv_complete(struct work_struct *work)
3093{
3094 struct cifs_readdata *rdata = container_of(work,
3095 struct cifs_readdata, work);
Jeff Layton1c892542012-05-16 07:13:17 -04003096
3097 complete(&rdata->done);
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003098 collect_uncached_read_data(rdata->ctx);
3099 /* the below call can possibly free the last ref to aio ctx */
Jeff Layton1c892542012-05-16 07:13:17 -04003100 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3101}
3102
3103static int
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003104uncached_fill_pages(struct TCP_Server_Info *server,
3105 struct cifs_readdata *rdata, struct iov_iter *iter,
3106 unsigned int len)
Jeff Layton1c892542012-05-16 07:13:17 -04003107{
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003108 int result = 0;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003109 unsigned int i;
3110 unsigned int nr_pages = rdata->nr_pages;
Long Li1dbe3462018-05-30 12:47:55 -07003111 unsigned int page_offset = rdata->page_offset;
Jeff Layton1c892542012-05-16 07:13:17 -04003112
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003113 rdata->got_bytes = 0;
Jeff Layton8321fec2012-09-19 06:22:32 -07003114 rdata->tailsz = PAGE_SIZE;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003115 for (i = 0; i < nr_pages; i++) {
3116 struct page *page = rdata->pages[i];
Al Viro71335662016-01-09 19:54:50 -05003117 size_t n;
Long Li1dbe3462018-05-30 12:47:55 -07003118 unsigned int segment_size = rdata->pagesz;
3119
3120 if (i == 0)
3121 segment_size -= page_offset;
3122 else
3123 page_offset = 0;
3124
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003125
Al Viro71335662016-01-09 19:54:50 -05003126 if (len <= 0) {
Jeff Layton1c892542012-05-16 07:13:17 -04003127 /* no need to hold page hostage */
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003128 rdata->pages[i] = NULL;
3129 rdata->nr_pages--;
Jeff Layton1c892542012-05-16 07:13:17 -04003130 put_page(page);
Jeff Layton8321fec2012-09-19 06:22:32 -07003131 continue;
Jeff Layton1c892542012-05-16 07:13:17 -04003132 }
Long Li1dbe3462018-05-30 12:47:55 -07003133
Al Viro71335662016-01-09 19:54:50 -05003134 n = len;
Long Li1dbe3462018-05-30 12:47:55 -07003135 if (len >= segment_size)
Al Viro71335662016-01-09 19:54:50 -05003136 /* enough data to fill the page */
Long Li1dbe3462018-05-30 12:47:55 -07003137 n = segment_size;
3138 else
Al Viro71335662016-01-09 19:54:50 -05003139 rdata->tailsz = len;
Long Li1dbe3462018-05-30 12:47:55 -07003140 len -= n;
3141
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003142 if (iter)
Long Li1dbe3462018-05-30 12:47:55 -07003143 result = copy_page_from_iter(
3144 page, page_offset, n, iter);
Long Libd3dcc62017-11-22 17:38:47 -07003145#ifdef CONFIG_CIFS_SMB_DIRECT
3146 else if (rdata->mr)
3147 result = n;
3148#endif
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003149 else
Long Li1dbe3462018-05-30 12:47:55 -07003150 result = cifs_read_page_from_socket(
3151 server, page, page_offset, n);
Jeff Layton8321fec2012-09-19 06:22:32 -07003152 if (result < 0)
3153 break;
3154
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003155 rdata->got_bytes += result;
Jeff Layton1c892542012-05-16 07:13:17 -04003156 }
3157
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003158 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3159 rdata->got_bytes : result;
Jeff Layton1c892542012-05-16 07:13:17 -04003160}
3161
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003162static int
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003163cifs_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
3169static int
3170cifs_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
3177static int
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003178cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003179 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3180 struct cifs_aio_ctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003182 struct cifs_readdata *rdata;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003183 unsigned int npages, rsize, credits;
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003184 size_t cur_len;
3185 int rc;
Jeff Layton1c892542012-05-16 07:13:17 -04003186 pid_t pid;
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003187 struct TCP_Server_Info *server;
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003188
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003189 server = tlink_tcon(open_file->tlink)->ses->server;
Pavel Shilovskyfc9c5962012-09-18 16:20:28 -07003190
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003191 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3192 pid = open_file->pid;
3193 else
3194 pid = current->tgid;
3195
Jeff Layton1c892542012-05-16 07:13:17 -04003196 do {
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003197 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 Layton1c892542012-05-16 07:13:17 -04003203 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003204
Jeff Layton1c892542012-05-16 07:13:17 -04003205 /* allocate a readdata struct */
3206 rdata = cifs_readdata_alloc(npages,
3207 cifs_uncached_readv_complete);
3208 if (!rdata) {
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003209 add_credits_and_wake_if(server, credits, 0);
Jeff Layton1c892542012-05-16 07:13:17 -04003210 rc = -ENOMEM;
Jeff Laytonbae9f742014-04-15 12:48:49 -04003211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003213
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003214 rc = cifs_read_allocate_pages(rdata, npages);
Jeff Layton1c892542012-05-16 07:13:17 -04003215 if (rc)
3216 goto error;
3217
3218 rdata->cfile = cifsFileInfo_get(open_file);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003219 rdata->nr_pages = npages;
Jeff Layton1c892542012-05-16 07:13:17 -04003220 rdata->offset = offset;
3221 rdata->bytes = cur_len;
3222 rdata->pid = pid;
Jeff Layton8321fec2012-09-19 06:22:32 -07003223 rdata->pagesz = PAGE_SIZE;
Long Li1dbe3462018-05-30 12:47:55 -07003224 rdata->tailsz = PAGE_SIZE;
Jeff Layton8321fec2012-09-19 06:22:32 -07003225 rdata->read_into_pages = cifs_uncached_read_into_pages;
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003226 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003227 rdata->credits = credits;
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003228 rdata->ctx = ctx;
3229 kref_get(&ctx->refcount);
Jeff Layton1c892542012-05-16 07:13:17 -04003230
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003231 if (!rdata->cfile->invalidHandle ||
Germano Percossi1fa839b2017-04-07 12:29:38 +01003232 !(rc = cifs_reopen_file(rdata->cfile, true)))
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003233 rc = server->ops->async_readv(rdata);
Jeff Layton1c892542012-05-16 07:13:17 -04003234error:
3235 if (rc) {
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003236 add_credits_and_wake_if(server, rdata->credits, 0);
Jeff Layton1c892542012-05-16 07:13:17 -04003237 kref_put(&rdata->refcount,
3238 cifs_uncached_readdata_release);
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003239 if (rc == -EAGAIN)
3240 continue;
Jeff Layton1c892542012-05-16 07:13:17 -04003241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Jeff Layton1c892542012-05-16 07:13:17 -04003243
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003244 list_add_tail(&rdata->list, rdata_list);
Jeff Layton1c892542012-05-16 07:13:17 -04003245 offset += cur_len;
3246 len -= cur_len;
3247 } while (len > 0);
3248
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003249 return rc;
3250}
3251
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003252static void
3253collect_uncached_read_data(struct cifs_aio_ctx *ctx)
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003254{
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003255 struct cifs_readdata *rdata, *tmp;
3256 struct iov_iter *to = &ctx->iter;
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003257 struct cifs_sb_info *cifs_sb;
3258 struct cifs_tcon *tcon;
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003259 unsigned int i;
3260 int rc;
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003261
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003262 tcon = tlink_tcon(ctx->cfile->tlink);
3263 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003264
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003265 mutex_lock(&ctx->aio_mutex);
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003266
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003267 if (list_empty(&ctx->list)) {
3268 mutex_unlock(&ctx->aio_mutex);
3269 return;
3270 }
Pavel Shilovsky0ada36b2014-06-25 10:42:28 +04003271
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003272 rc = ctx->rc;
Jeff Layton1c892542012-05-16 07:13:17 -04003273 /* the loop below should proceed in the order of increasing offsets */
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003274again:
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003275 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
Jeff Layton1c892542012-05-16 07:13:17 -04003276 if (!rc) {
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003277 if (!try_wait_for_completion(&rdata->done)) {
3278 mutex_unlock(&ctx->aio_mutex);
3279 return;
3280 }
3281
3282 if (rdata->result == -EAGAIN) {
Al Viro74027f42014-02-04 13:47:26 -05003283 /* resend call if it's a retryable error */
Pavel Shilovskyfb8a3e52014-07-10 11:50:39 +04003284 struct list_head tmp_list;
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003285 unsigned int got_bytes = rdata->got_bytes;
Jeff Layton1c892542012-05-16 07:13:17 -04003286
Pavel Shilovskyfb8a3e52014-07-10 11:50:39 +04003287 list_del_init(&rdata->list);
3288 INIT_LIST_HEAD(&tmp_list);
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003289
Pavel Shilovskyd913ed12014-07-10 11:31:48 +04003290 /*
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 Shilovsky6685c5e2017-04-25 11:52:30 -07003308 &tmp_list, ctx);
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003309
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003310 list_splice(&tmp_list, &ctx->list);
Pavel Shilovsky25f40252014-06-25 10:45:07 +04003311
Pavel Shilovskyfb8a3e52014-07-10 11:50:39 +04003312 kref_put(&rdata->refcount,
3313 cifs_uncached_readdata_release);
3314 goto again;
3315 } else if (rdata->result)
3316 rc = rdata->result;
3317 else
Jeff Layton1c892542012-05-16 07:13:17 -04003318 rc = cifs_readdata_to_iov(rdata, to);
Pavel Shilovskyfb8a3e52014-07-10 11:50:39 +04003319
Pavel Shilovsky2e8a05d2014-07-10 10:21:15 +04003320 /* if there was a short read -- discard anything left */
3321 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3322 rc = -ENODATA;
Jeff Layton1c892542012-05-16 07:13:17 -04003323 }
3324 list_del_init(&rdata->list);
3325 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003327
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003328 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 Viro7f25bba2014-02-04 14:07:43 -05003333
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003334 ctx->total_len = ctx->len - iov_iter_count(to);
3335
3336 cifs_stats_bytes_read(tcon, ctx->total_len);
Jeff Layton1c892542012-05-16 07:13:17 -04003337
Pavel Shilovsky09a47072012-09-18 16:20:29 -07003338 /* mask nodata case */
3339 if (rc == -ENODATA)
3340 rc = 0;
3341
Yilu Lin9bc02252020-03-18 11:59:19 +08003342 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003343
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
3352ssize_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 Carpenter8a7b0d82017-05-05 08:30:40 +03003387 if (to->type == ITER_IOVEC)
Pavel Shilovsky6685c5e2017-04-25 11:52:30 -07003388 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 Viro0165e812014-02-04 14:19:48 -05003432 if (total_read) {
Al Viroe6a7bcb2014-04-02 19:53:36 -04003433 iocb->ki_pos += total_read;
Al Viro0165e812014-02-04 14:19:48 -05003434 return total_read;
3435 }
3436 return rc;
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003437}
3438
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003439ssize_t
Al Viroe6a7bcb2014-04-02 19:53:36 -04003440cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003441{
Al Viro496ad9a2013-01-23 17:07:38 -05003442 struct inode *inode = file_inode(iocb->ki_filp);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003443 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 Shilovskya70307e2010-12-14 11:50:41 +03003449
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 Shilovsky18cceb62013-09-05 13:01:06 +04003458 if (!CIFS_CACHE_READ(cinode))
Al Viroe6a7bcb2014-04-02 19:53:36 -04003459 return cifs_user_readv(iocb, to);
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003460
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003461 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 Viroe6a7bcb2014-04-02 19:53:36 -04003464 return generic_file_read_iter(iocb, to);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003465
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 Viroe6a7bcb2014-04-02 19:53:36 -04003471 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003472 tcon->ses->server->vals->shared_lock_type,
Pavel Shilovsky081c0412012-11-27 18:38:53 +04003473 NULL, CIFS_READ_OP))
Al Viroe6a7bcb2014-04-02 19:53:36 -04003474 rc = generic_file_read_iter(iocb, to);
Pavel Shilovsky579f9052012-09-19 06:22:44 -07003475 up_read(&cinode->lock_sem);
3476 return rc;
Pavel Shilovskya70307e2010-12-14 11:50:41 +03003477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003479static ssize_t
3480cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 int rc = -EACCES;
3483 unsigned int bytes_read = 0;
3484 unsigned int total_read;
3485 unsigned int current_read_size;
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003486 unsigned int rsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 struct cifs_sb_info *cifs_sb;
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003488 struct cifs_tcon *tcon;
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003489 struct TCP_Server_Info *server;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003490 unsigned int xid;
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003491 char *cur_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 struct cifsFileInfo *open_file;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003493 struct cifs_io_parms io_parms;
Steve Frenchec637e32005-12-12 20:53:18 -08003494 int buf_type = CIFS_NO_BUFFER;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003495 __u32 pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003497 xid = get_xid();
Al Viro7119e222014-10-22 00:25:12 -04003498 cifs_sb = CIFS_FILE_SB(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003500 /* FIXME: set up handlers for larger reads and/or convert to async */
3501 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 if (file->private_data == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05303504 rc = -EBADF;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003505 free_xid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05303506 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 }
Joe Perchesc21dfb62010-07-12 13:50:14 -07003508 open_file = file->private_data;
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003509 tcon = tlink_tcon(open_file->tlink);
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003510 server = tcon->ses->server;
3511
3512 if (!server->ops->sync_read) {
3513 free_xid(xid);
3514 return -ENOSYS;
3515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003517 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3518 pid = open_file->pid;
3519 else
3520 pid = current->tgid;
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
Joe Perchesf96637b2013-05-04 22:12:25 -05003523 cifs_dbg(FYI, "attempting read on write only file instance\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003525 for (total_read = 0, cur_offset = read_data; read_size > total_read;
3526 total_read += bytes_read, cur_offset += bytes_read) {
Pavel Shilovskye374d902014-06-25 16:19:02 +04003527 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 Shilovsky29e20f92012-07-13 13:58:14 +04003536 tcon->ses->server->vals->cap_large_files)) {
Pavel Shilovskye374d902014-06-25 16:19:02 +04003537 current_read_size = min_t(uint,
3538 current_read_size, CIFSMaxBufSize);
3539 }
Steve Frenchcdff08e2010-10-21 22:46:14 +00003540 if (open_file->invalidHandle) {
Jeff Layton15886172010-10-15 15:33:59 -04003541 rc = cifs_reopen_file(open_file, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 if (rc != 0)
3543 break;
3544 }
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003545 io_parms.pid = pid;
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003546 io_parms.tcon = tcon;
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003547 io_parms.offset = *offset;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003548 io_parms.length = current_read_size;
Steve Frenchdb8b6312014-09-22 05:13:55 -05003549 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003550 &bytes_read, &cur_offset,
3551 &buf_type);
Pavel Shilovskye374d902014-06-25 16:19:02 +04003552 } while (rc == -EAGAIN);
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (rc || (bytes_read == 0)) {
3555 if (total_read) {
3556 break;
3557 } else {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003558 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 return rc;
3560 }
3561 } else {
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04003562 cifs_stats_bytes_read(tcon, total_read);
Pavel Shilovskyf9c6e232012-09-18 16:20:29 -07003563 *offset += bytes_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 }
3565 }
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003566 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return total_read;
3568}
3569
Jeff Laytonca83ce32011-04-12 09:13:44 -04003570/*
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 Joardera5240cb2018-04-15 00:58:25 +05303574static vm_fault_t
Dave Jiang11bac802017-02-24 14:56:41 -08003575cifs_page_mkwrite(struct vm_fault *vmf)
Jeff Laytonca83ce32011-04-12 09:13:44 -04003576{
3577 struct page *page = vmf->page;
3578
3579 lock_page(page);
3580 return VM_FAULT_LOCKED;
3581}
3582
Kirill A. Shutemov7cbea8d2015-09-09 15:39:26 -07003583static const struct vm_operations_struct cifs_file_vm_ops = {
Jeff Laytonca83ce32011-04-12 09:13:44 -04003584 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -07003585 .map_pages = filemap_map_pages,
Jeff Laytonca83ce32011-04-12 09:13:44 -04003586 .page_mkwrite = cifs_page_mkwrite,
3587};
3588
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03003589int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
3590{
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003591 int xid, rc = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05003592 struct inode *inode = file_inode(file);
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03003593
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003594 xid = get_xid();
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03003595
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003596 if (!CIFS_CACHE_READ(CIFS_I(inode)))
Jeff Layton4f73c7d2014-04-30 09:31:47 -04003597 rc = cifs_zap_mapping(inode);
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003598 if (!rc)
3599 rc = generic_file_mmap(file, vma);
3600 if (!rc)
Jeff Laytonca83ce32011-04-12 09:13:44 -04003601 vma->vm_ops = &cifs_file_vm_ops;
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003602
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003603 free_xid(xid);
Pavel Shilovsky7a6a19b2010-12-14 11:29:51 +03003604 return rc;
3605}
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
3608{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 int rc, xid;
3610
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003611 xid = get_xid();
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003612
Jeff Laytonabab0952010-02-12 07:44:18 -05003613 rc = cifs_revalidate_file(file);
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003614 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003615 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
3616 rc);
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003617 if (!rc)
3618 rc = generic_file_mmap(file, vma);
3619 if (!rc)
Jeff Laytonca83ce32011-04-12 09:13:44 -04003620 vma->vm_ops = &cifs_file_vm_ops;
Matthew Wilcoxf04a7032017-12-15 12:48:32 -08003621
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003622 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 return rc;
3624}
3625
Jeff Layton0471ca32012-05-16 07:13:16 -04003626static void
3627cifs_readv_complete(struct work_struct *work)
3628{
Pavel Shilovskyb770ddf2014-07-10 11:31:53 +04003629 unsigned int i, got_bytes;
Jeff Layton0471ca32012-05-16 07:13:16 -04003630 struct cifs_readdata *rdata = container_of(work,
3631 struct cifs_readdata, work);
Jeff Layton0471ca32012-05-16 07:13:16 -04003632
Pavel Shilovskyb770ddf2014-07-10 11:31:53 +04003633 got_bytes = rdata->got_bytes;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003634 for (i = 0; i < rdata->nr_pages; i++) {
3635 struct page *page = rdata->pages[i];
3636
Jeff Layton0471ca32012-05-16 07:13:16 -04003637 lru_cache_add_file(page);
3638
Pavel Shilovskyb770ddf2014-07-10 11:31:53 +04003639 if (rdata->result == 0 ||
3640 (rdata->result == -EAGAIN && got_bytes)) {
Jeff Layton0471ca32012-05-16 07:13:16 -04003641 flush_dcache_page(page);
3642 SetPageUptodate(page);
3643 }
3644
3645 unlock_page(page);
3646
Pavel Shilovskyb770ddf2014-07-10 11:31:53 +04003647 if (rdata->result == 0 ||
3648 (rdata->result == -EAGAIN && got_bytes))
Jeff Layton0471ca32012-05-16 07:13:16 -04003649 cifs_readpage_to_fscache(rdata->mapping->host, page);
3650
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003651 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
Pavel Shilovskyb770ddf2014-07-10 11:31:53 +04003652
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003653 put_page(page);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003654 rdata->pages[i] = NULL;
Jeff Layton0471ca32012-05-16 07:13:16 -04003655 }
Jeff Layton6993f742012-05-16 07:13:17 -04003656 kref_put(&rdata->refcount, cifs_readdata_release);
Jeff Layton0471ca32012-05-16 07:13:16 -04003657}
3658
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003659static int
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003660readpages_fill_pages(struct TCP_Server_Info *server,
3661 struct cifs_readdata *rdata, struct iov_iter *iter,
3662 unsigned int len)
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003663{
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003664 int result = 0;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003665 unsigned int i;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003666 u64 eof;
3667 pgoff_t eof_index;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003668 unsigned int nr_pages = rdata->nr_pages;
Long Li1dbe3462018-05-30 12:47:55 -07003669 unsigned int page_offset = rdata->page_offset;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003670
3671 /* determine the eof that the server (probably) has */
3672 eof = CIFS_I(rdata->mapping->host)->server_eof;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003673 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05003674 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003675
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003676 rdata->got_bytes = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003677 rdata->tailsz = PAGE_SIZE;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003678 for (i = 0; i < nr_pages; i++) {
3679 struct page *page = rdata->pages[i];
Long Li1dbe3462018-05-30 12:47:55 -07003680 unsigned int to_read = rdata->pagesz;
3681 size_t n;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003682
Long Li1dbe3462018-05-30 12:47:55 -07003683 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 Layton8321fec2012-09-19 06:22:32 -07003692 } else if (len > 0) {
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003693 /* enough for partial page, fill and zero the rest */
Long Li1dbe3462018-05-30 12:47:55 -07003694 zero_user(page, len + page_offset, to_read - len);
Al Viro71335662016-01-09 19:54:50 -05003695 n = rdata->tailsz = len;
Jeff Layton8321fec2012-09-19 06:22:32 -07003696 len = 0;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003697 } 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. Shutemov09cbfea2016-04-01 15:29:47 +03003706 zero_user(page, 0, PAGE_SIZE);
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003707 lru_cache_add_file(page);
3708 flush_dcache_page(page);
3709 SetPageUptodate(page);
3710 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003711 put_page(page);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003712 rdata->pages[i] = NULL;
3713 rdata->nr_pages--;
Jeff Layton8321fec2012-09-19 06:22:32 -07003714 continue;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003715 } else {
3716 /* no need to hold page hostage */
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003717 lru_cache_add_file(page);
3718 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003719 put_page(page);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003720 rdata->pages[i] = NULL;
3721 rdata->nr_pages--;
Jeff Layton8321fec2012-09-19 06:22:32 -07003722 continue;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003723 }
Jeff Layton8321fec2012-09-19 06:22:32 -07003724
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003725 if (iter)
Long Li1dbe3462018-05-30 12:47:55 -07003726 result = copy_page_from_iter(
3727 page, page_offset, n, iter);
Long Libd3dcc62017-11-22 17:38:47 -07003728#ifdef CONFIG_CIFS_SMB_DIRECT
3729 else if (rdata->mr)
3730 result = n;
3731#endif
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003732 else
Long Li1dbe3462018-05-30 12:47:55 -07003733 result = cifs_read_page_from_socket(
3734 server, page, page_offset, n);
Jeff Layton8321fec2012-09-19 06:22:32 -07003735 if (result < 0)
3736 break;
3737
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003738 rdata->got_bytes += result;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003739 }
3740
Pavel Shilovskyb3160ae2014-07-10 10:16:25 +04003741 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3742 rdata->got_bytes : result;
Jeff Layton8d5ce4d2012-05-16 07:13:16 -04003743}
3744
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003745static int
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003746cifs_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
3752static int
3753cifs_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
3760static int
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003761readpages_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 Hocko8a5c7432016-07-26 15:24:53 -07003768 gfp_t gfp = readahead_gfp_mask(mapping);
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003769
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003770 INIT_LIST_HEAD(tmplist);
3771
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003772 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. Shutemov48c935a2016-01-15 16:51:24 -08003779 __SetPageLocked(page);
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003780 rc = add_to_page_cache_locked(page, mapping,
Michal Hocko063d99b2015-10-15 15:28:24 -07003781 page->index, gfp);
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003782
3783 /* give up if we can't stick it in the cache */
3784 if (rc) {
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08003785 __ClearPageLocked(page);
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003786 return rc;
3787 }
3788
3789 /* move first page to the tmplist */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003790 *offset = (loff_t)page->index << PAGE_SHIFT;
3791 *bytes = PAGE_SIZE;
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003792 *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. Shutemov09cbfea2016-04-01 15:29:47 +03003803 if (*bytes + PAGE_SIZE > rsize)
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003804 break;
3805
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08003806 __SetPageLocked(page);
Michal Hocko063d99b2015-10-15 15:28:24 -07003807 if (add_to_page_cache_locked(page, mapping, page->index, gfp)) {
Kirill A. Shutemov48c935a2016-01-15 16:51:24 -08003808 __ClearPageLocked(page);
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003809 break;
3810 }
3811 list_move_tail(&page->lru, tmplist);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003812 (*bytes) += PAGE_SIZE;
Pavel Shilovsky387eb922014-06-24 13:08:54 +04003813 expected_index++;
3814 (*nr_pages)++;
3815 }
3816 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819static int cifs_readpages(struct file *file, struct address_space *mapping,
3820 struct list_head *page_list, unsigned num_pages)
3821{
Jeff Layton690c5e32011-10-19 15:30:16 -04003822 int rc;
3823 struct list_head tmplist;
3824 struct cifsFileInfo *open_file = file->private_data;
Al Viro7119e222014-10-22 00:25:12 -04003825 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003826 struct TCP_Server_Info *server;
Jeff Layton690c5e32011-10-19 15:30:16 -04003827 pid_t pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Jeff Layton690c5e32011-10-19 15:30:16 -04003829 /*
Suresh Jayaraman56698232010-07-05 18:13:25 +05303830 * Reads as many pages as possible from fscache. Returns -ENOBUFS
3831 * immediately if the cookie is negative
David Howells54afa992013-09-04 17:10:39 +00003832 *
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 Jayaraman56698232010-07-05 18:13:25 +05303835 */
3836 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
3837 &num_pages);
3838 if (rc == 0)
Jeff Layton690c5e32011-10-19 15:30:16 -04003839 return rc;
Suresh Jayaraman56698232010-07-05 18:13:25 +05303840
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003841 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3842 pid = open_file->pid;
3843 else
3844 pid = current->tgid;
3845
Jeff Layton690c5e32011-10-19 15:30:16 -04003846 rc = 0;
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003847 server = tlink_tcon(open_file->tlink)->ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Joe Perchesf96637b2013-05-04 22:12:25 -05003849 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
3850 __func__, file, mapping, num_pages);
Jeff Layton690c5e32011-10-19 15:30:16 -04003851
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 Shilovskybed9da02014-06-25 11:28:57 +04003864 unsigned int i, nr_pages, bytes, rsize;
Jeff Layton690c5e32011-10-19 15:30:16 -04003865 loff_t offset;
3866 struct page *page, *tpage;
3867 struct cifs_readdata *rdata;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003868 unsigned credits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003870 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3871 &rsize, &credits);
3872 if (rc)
3873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Jeff Layton690c5e32011-10-19 15:30:16 -04003875 /*
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003876 * 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 Layton690c5e32011-10-19 15:30:16 -04003880 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003881 if (unlikely(rsize < PAGE_SIZE)) {
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003882 add_credits_and_wake_if(server, credits, 0);
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003886 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 Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
Jeff Layton690c5e32011-10-19 15:30:16 -04003891 }
3892
Jeff Layton0471ca32012-05-16 07:13:16 -04003893 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
Jeff Layton690c5e32011-10-19 15:30:16 -04003894 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. Shutemov09cbfea2016-04-01 15:29:47 +03003900 put_page(page);
Jeff Layton690c5e32011-10-19 15:30:16 -04003901 }
3902 rc = -ENOMEM;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003903 add_credits_and_wake_if(server, credits, 0);
Jeff Layton690c5e32011-10-19 15:30:16 -04003904 break;
3905 }
3906
Jeff Layton6993f742012-05-16 07:13:17 -04003907 rdata->cfile = cifsFileInfo_get(open_file);
Jeff Layton690c5e32011-10-19 15:30:16 -04003908 rdata->mapping = mapping;
3909 rdata->offset = offset;
3910 rdata->bytes = bytes;
3911 rdata->pid = pid;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003912 rdata->pagesz = PAGE_SIZE;
Long Li1dbe3462018-05-30 12:47:55 -07003913 rdata->tailsz = PAGE_SIZE;
Jeff Layton8321fec2012-09-19 06:22:32 -07003914 rdata->read_into_pages = cifs_readpages_read_into_pages;
Pavel Shilovskyd70b9102016-11-17 16:20:18 -08003915 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003916 rdata->credits = credits;
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003917
3918 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
3919 list_del(&page->lru);
3920 rdata->pages[rdata->nr_pages++] = page;
3921 }
Jeff Layton690c5e32011-10-19 15:30:16 -04003922
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003923 if (!rdata->cfile->invalidHandle ||
Germano Percossi1fa839b2017-04-07 12:29:38 +01003924 !(rc = cifs_reopen_file(rdata->cfile, true)))
Pavel Shilovsky69cebd72014-06-24 13:42:03 +04003925 rc = server->ops->async_readv(rdata);
3926 if (rc) {
Pavel Shilovskybed9da02014-06-25 11:28:57 +04003927 add_credits_and_wake_if(server, rdata->credits, 0);
Jeff Laytonc5fab6f2012-09-19 06:22:30 -07003928 for (i = 0; i < rdata->nr_pages; i++) {
3929 page = rdata->pages[i];
Jeff Layton690c5e32011-10-19 15:30:16 -04003930 lru_cache_add_file(page);
3931 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003932 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Pavel Shilovsky1209bbd2014-10-02 20:13:35 +04003934 /* Fallback to the readpage in error/reconnect cases */
Jeff Layton6993f742012-05-16 07:13:17 -04003935 kref_put(&rdata->refcount, cifs_readdata_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 break;
3937 }
Jeff Layton6993f742012-05-16 07:13:17 -04003938
3939 kref_put(&rdata->refcount, cifs_readdata_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
3941
David Howells54afa992013-09-04 17:10:39 +00003942 /* 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 Torvalds1da177e2005-04-16 15:20:36 -07003947 return rc;
3948}
3949
Sachin Prabhua9e9b7b2013-09-13 14:11:56 +01003950/*
3951 * cifs_readpage_worker must be called with the page pinned
3952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953static int cifs_readpage_worker(struct file *file, struct page *page,
3954 loff_t *poffset)
3955{
3956 char *read_data;
3957 int rc;
3958
Suresh Jayaraman56698232010-07-05 18:13:25 +05303959 /* Is the page cached? */
Al Viro496ad9a2013-01-23 17:07:38 -05003960 rc = cifs_readpage_from_fscache(file_inode(file), page);
Suresh Jayaraman56698232010-07-05 18:13:25 +05303961 if (rc == 0)
3962 goto read_complete;
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 read_data = kmap(page);
3965 /* for reads over a certain size could initiate async read ahead */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003966
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003967 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (rc < 0)
3970 goto io_error;
3971 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003972 cifs_dbg(FYI, "Bytes read %d\n", rc);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003973
Al Viro496ad9a2013-01-23 17:07:38 -05003974 file_inode(file)->i_atime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003975 current_time(file_inode(file));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003976
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003977 if (PAGE_SIZE > rc)
3978 memset(read_data + rc, 0, PAGE_SIZE - rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 flush_dcache_page(page);
3981 SetPageUptodate(page);
Suresh Jayaraman9dc06552010-07-05 18:13:11 +05303982
3983 /* send this page to the cache */
Al Viro496ad9a2013-01-23 17:07:38 -05003984 cifs_readpage_to_fscache(file_inode(file), page);
Suresh Jayaraman9dc06552010-07-05 18:13:11 +05303985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 rc = 0;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988io_error:
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003989 kunmap(page);
Sachin Prabhu466bd312013-09-13 14:11:57 +01003990 unlock_page(page);
Suresh Jayaraman56698232010-07-05 18:13:25 +05303991
3992read_complete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 return rc;
3994}
3995
3996static int cifs_readpage(struct file *file, struct page *page)
3997{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003998 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 int rc = -EACCES;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004000 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004002 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
4004 if (file->private_data == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05304005 rc = -EBADF;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004006 free_xid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05304007 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009
Joe Perchesf96637b2013-05-04 22:12:25 -05004010 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004011 page, (int)offset, (int)offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 rc = cifs_readpage_worker(file, page, &offset);
4014
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004015 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 return rc;
4017}
4018
Steve Frencha403a0a2007-07-26 15:54:16 +00004019static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4020{
4021 struct cifsFileInfo *open_file;
4022
Dave Wysochanskia8de7092019-10-03 15:16:27 +10004023 spin_lock(&cifs_inode->open_file_lock);
Steve Frencha403a0a2007-07-26 15:54:16 +00004024 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
Jeff Layton2e396b82010-10-15 15:34:01 -04004025 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
Dave Wysochanskia8de7092019-10-03 15:16:27 +10004026 spin_unlock(&cifs_inode->open_file_lock);
Steve Frencha403a0a2007-07-26 15:54:16 +00004027 return 1;
4028 }
4029 }
Dave Wysochanskia8de7092019-10-03 15:16:27 +10004030 spin_unlock(&cifs_inode->open_file_lock);
Steve Frencha403a0a2007-07-26 15:54:16 +00004031 return 0;
4032}
4033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034/* 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 Frenchfb8c4b12007-07-10 01:16:18 +00004037 refreshing the inode only on increases in the file size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 but this is tricky to do without racing with writebehind
4039 page caching in the current Linux kernel design */
Steve French4b18f2a2008-04-29 00:06:05 +00004040bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Steve Frencha403a0a2007-07-26 15:54:16 +00004042 if (!cifsInode)
Steve French4b18f2a2008-04-29 00:06:05 +00004043 return true;
Steve French23e7dd72005-10-20 13:44:56 -07004044
Steve Frencha403a0a2007-07-26 15:54:16 +00004045 if (is_inode_writable(cifsInode)) {
4046 /* This inode is open for write at least once */
Steve Frenchc32a0b62006-01-12 14:41:28 -08004047 struct cifs_sb_info *cifs_sb;
4048
Steve Frenchc32a0b62006-01-12 14:41:28 -08004049 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
Steve Frenchad7a2922008-02-07 23:25:02 +00004050 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004051 /* since no page cache to corrupt on directio
Steve Frenchc32a0b62006-01-12 14:41:28 -08004052 we can change size safely */
Steve French4b18f2a2008-04-29 00:06:05 +00004053 return true;
Steve Frenchc32a0b62006-01-12 14:41:28 -08004054 }
4055
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004056 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
Steve French4b18f2a2008-04-29 00:06:05 +00004057 return true;
Steve French7ba52632007-02-08 18:14:13 +00004058
Steve French4b18f2a2008-04-29 00:06:05 +00004059 return false;
Steve French23e7dd72005-10-20 13:44:56 -07004060 } else
Steve French4b18f2a2008-04-29 00:06:05 +00004061 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Nick Piggind9414772008-09-24 11:32:59 -04004064static 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 Torvalds1da177e2005-04-16 15:20:36 -07004067{
Sachin Prabhu466bd312013-09-13 14:11:57 +01004068 int oncethru = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004069 pgoff_t index = pos >> PAGE_SHIFT;
4070 loff_t offset = pos & (PAGE_SIZE - 1);
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004071 loff_t page_start = pos & PAGE_MASK;
4072 loff_t i_size;
4073 struct page *page;
4074 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Joe Perchesf96637b2013-05-04 22:12:25 -05004076 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
Nick Piggind9414772008-09-24 11:32:59 -04004077
Sachin Prabhu466bd312013-09-13 14:11:57 +01004078start:
Nick Piggin54566b22009-01-04 12:00:53 -08004079 page = grab_cache_page_write_begin(mapping, index, flags);
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004080 if (!page) {
4081 rc = -ENOMEM;
4082 goto out;
4083 }
Nick Piggind9414772008-09-24 11:32:59 -04004084
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004085 if (PageUptodate(page))
4086 goto out;
Steve French8a236262007-03-06 00:31:00 +00004087
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004088 /*
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. Shutemov09cbfea2016-04-01 15:29:47 +03004093 if (len == PAGE_SIZE)
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004094 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 Shilovsky18cceb62013-09-05 13:01:06 +04004102 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004103 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. Shutemov09cbfea2016-04-01 15:29:47 +03004108 PAGE_SIZE);
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004109 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07004119
Sachin Prabhu466bd312013-09-13 14:11:57 +01004120 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004121 /*
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. Shutemov09cbfea2016-04-01 15:29:47 +03004127 put_page(page);
Sachin Prabhu466bd312013-09-13 14:11:57 +01004128 oncethru = 1;
4129 goto start;
Steve French8a236262007-03-06 00:31:00 +00004130 } 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 Piggind9414772008-09-24 11:32:59 -04004134 this will be written out by write_end so is fine */
Steve French8a236262007-03-06 00:31:00 +00004135 }
Jeff Laytona98ee8c2008-11-26 19:32:33 +00004136out:
4137 *pagep = page;
4138 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304141static 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 Czernerd47992f2013-05-21 23:17:23 -04004149static void cifs_invalidate_page(struct page *page, unsigned int offset,
4150 unsigned int length)
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304151{
4152 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
4153
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004154 if (offset == 0 && length == PAGE_SIZE)
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304155 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
4156}
4157
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04004158static int cifs_launder_page(struct page *page)
4159{
4160 int rc = 0;
4161 loff_t range_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004162 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04004163 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 Perchesf96637b2013-05-04 22:12:25 -05004170 cifs_dbg(FYI, "Launder page: %p\n", page);
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04004171
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 Heo9b646972010-07-20 22:09:02 +02004179void cifs_oplock_break(struct work_struct *work)
Jeff Layton3bc303c2009-09-21 06:47:50 -04004180{
4181 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4182 oplock_break);
David Howells2b0143b2015-03-17 22:25:59 +00004183 struct inode *inode = d_inode(cfile->dentry);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004184 struct cifsInodeInfo *cinode = CIFS_I(inode);
Pavel Shilovsky95a3f2f2012-09-18 16:20:33 -07004185 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004186 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytoneb4b7562010-10-22 14:52:29 -04004187 int rc = 0;
Jeff Layton3bc303c2009-09-21 06:47:50 -04004188
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004189 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
NeilBrown74316202014-07-07 15:16:04 +10004190 TASK_UNINTERRUPTIBLE);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004191
4192 server->ops->downgrade_oplock(server, cinode,
4193 test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags));
4194
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04004195 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +04004196 cifs_has_mand_locks(cinode)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004197 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4198 inode);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04004199 cinode->oplock = 0;
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +04004200 }
4201
Jeff Layton3bc303c2009-09-21 06:47:50 -04004202 if (inode && S_ISREG(inode->i_mode)) {
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04004203 if (CIFS_CACHE_READ(cinode))
Al Viro8737c932009-12-24 06:47:55 -05004204 break_lease(inode, O_RDONLY);
Steve Frenchd54ff732010-04-27 04:38:15 +00004205 else
Al Viro8737c932009-12-24 06:47:55 -05004206 break_lease(inode, O_WRONLY);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004207 rc = filemap_fdatawrite(inode->i_mapping);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04004208 if (!CIFS_CACHE_READ(cinode)) {
Jeff Laytoneb4b7562010-10-22 14:52:29 -04004209 rc = filemap_fdatawait(inode->i_mapping);
4210 mapping_set_error(inode->i_mapping, rc);
Jeff Layton4f73c7d2014-04-30 09:31:47 -04004211 cifs_zap_mapping(inode);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004212 }
Joe Perchesf96637b2013-05-04 22:12:25 -05004213 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004214 }
4215
Pavel Shilovsky85160e02011-10-22 15:33:29 +04004216 rc = cifs_push_locks(cfile);
4217 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004218 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
Pavel Shilovsky85160e02011-10-22 15:33:29 +04004219
Jeff Layton3bc303c2009-09-21 06:47:50 -04004220 /*
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 Frenchcdff08e2010-10-21 22:46:14 +00004226 if (!cfile->oplock_break_cancelled) {
Pavel Shilovsky95a3f2f2012-09-18 16:20:33 -07004227 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4228 cinode);
Joe Perchesf96637b2013-05-04 22:12:25 -05004229 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004230 }
Aurelien Aptel8092ecc2019-03-29 10:49:12 +01004231 _cifsFileInfo_put(cfile, false /* do not wait for ourself */);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004232 cifs_done_oplock_break(cinode);
Jeff Layton3bc303c2009-09-21 06:47:50 -04004233}
4234
Steve Frenchdca69282013-11-11 16:42:37 -06004235/*
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 */
4244static ssize_t
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07004245cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
Steve Frenchdca69282013-11-11 16:42:37 -06004246{
4247 /*
4248 * FIXME
4249 * Eventually need to support direct IO for non forcedirectio mounts
4250 */
4251 return -EINVAL;
4252}
4253
4254
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07004255const struct address_space_operations cifs_addr_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 .readpage = cifs_readpage,
4257 .readpages = cifs_readpages,
4258 .writepage = cifs_writepage,
Steve French37c0eb42005-10-05 14:50:29 -07004259 .writepages = cifs_writepages,
Nick Piggind9414772008-09-24 11:32:59 -04004260 .write_begin = cifs_write_begin,
4261 .write_end = cifs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 .set_page_dirty = __set_page_dirty_nobuffers,
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304263 .releasepage = cifs_release_page,
Steve Frenchdca69282013-11-11 16:42:37 -06004264 .direct_IO = cifs_direct_io,
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304265 .invalidatepage = cifs_invalidate_page,
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04004266 .launder_page = cifs_launder_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267};
Dave Kleikamp273d81d2006-06-01 19:41:23 +00004268
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 Hellwigf5e54d62006-06-28 04:26:44 -07004274const struct address_space_operations cifs_addr_ops_smallbuf = {
Dave Kleikamp273d81d2006-06-01 19:41:23 +00004275 .readpage = cifs_readpage,
4276 .writepage = cifs_writepage,
4277 .writepages = cifs_writepages,
Nick Piggind9414772008-09-24 11:32:59 -04004278 .write_begin = cifs_write_begin,
4279 .write_end = cifs_write_end,
Dave Kleikamp273d81d2006-06-01 19:41:23 +00004280 .set_page_dirty = __set_page_dirty_nobuffers,
Suresh Jayaraman85f2d6b2010-07-05 18:13:00 +05304281 .releasepage = cifs_release_page,
4282 .invalidatepage = cifs_invalidate_page,
Pavel Shilovsky9ad15062011-04-08 05:29:10 +04004283 .launder_page = cifs_launder_page,
Dave Kleikamp273d81d2006-06-01 19:41:23 +00004284};