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