locks: convert posix locks to file_lock_context
[deliverable/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>
1da177e4
LT
36#include <asm/div64.h>
37#include "cifsfs.h"
38#include "cifspdu.h"
39#include "cifsglob.h"
40#include "cifsproto.h"
41#include "cifs_unicode.h"
42#include "cifs_debug.h"
43#include "cifs_fs_sb.h"
9451a9a5 44#include "fscache.h"
1da177e4 45
07b92d0d 46
1da177e4
LT
47static inline int cifs_convert_flags(unsigned int flags)
48{
49 if ((flags & O_ACCMODE) == O_RDONLY)
50 return GENERIC_READ;
51 else if ((flags & O_ACCMODE) == O_WRONLY)
52 return GENERIC_WRITE;
53 else if ((flags & O_ACCMODE) == O_RDWR) {
54 /* GENERIC_ALL is too much permission to request
55 can cause unnecessary access denied on create */
56 /* return GENERIC_ALL; */
57 return (GENERIC_READ | GENERIC_WRITE);
58 }
59
e10f7b55
JL
60 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
61 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
62 FILE_READ_DATA);
7fc8f4e9 63}
e10f7b55 64
608712fe 65static u32 cifs_posix_convert_flags(unsigned int flags)
7fc8f4e9 66{
608712fe 67 u32 posix_flags = 0;
e10f7b55 68
7fc8f4e9 69 if ((flags & O_ACCMODE) == O_RDONLY)
608712fe 70 posix_flags = SMB_O_RDONLY;
7fc8f4e9 71 else if ((flags & O_ACCMODE) == O_WRONLY)
608712fe
JL
72 posix_flags = SMB_O_WRONLY;
73 else if ((flags & O_ACCMODE) == O_RDWR)
74 posix_flags = SMB_O_RDWR;
75
07b92d0d 76 if (flags & O_CREAT) {
608712fe 77 posix_flags |= SMB_O_CREAT;
07b92d0d
SF
78 if (flags & O_EXCL)
79 posix_flags |= SMB_O_EXCL;
80 } else if (flags & O_EXCL)
f96637be
JP
81 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
82 current->comm, current->tgid);
07b92d0d 83
608712fe
JL
84 if (flags & O_TRUNC)
85 posix_flags |= SMB_O_TRUNC;
86 /* be safe and imply O_SYNC for O_DSYNC */
6b2f3d1f 87 if (flags & O_DSYNC)
608712fe 88 posix_flags |= SMB_O_SYNC;
7fc8f4e9 89 if (flags & O_DIRECTORY)
608712fe 90 posix_flags |= SMB_O_DIRECTORY;
7fc8f4e9 91 if (flags & O_NOFOLLOW)
608712fe 92 posix_flags |= SMB_O_NOFOLLOW;
7fc8f4e9 93 if (flags & O_DIRECT)
608712fe 94 posix_flags |= SMB_O_DIRECT;
7fc8f4e9
SF
95
96 return posix_flags;
1da177e4
LT
97}
98
99static inline int cifs_get_disposition(unsigned int flags)
100{
101 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
102 return FILE_CREATE;
103 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
104 return FILE_OVERWRITE_IF;
105 else if ((flags & O_CREAT) == O_CREAT)
106 return FILE_OPEN_IF;
55aa2e09
SF
107 else if ((flags & O_TRUNC) == O_TRUNC)
108 return FILE_OVERWRITE;
1da177e4
LT
109 else
110 return FILE_OPEN;
111}
112
608712fe
JL
113int cifs_posix_open(char *full_path, struct inode **pinode,
114 struct super_block *sb, int mode, unsigned int f_flags,
6d5786a3 115 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
608712fe
JL
116{
117 int rc;
118 FILE_UNIX_BASIC_INFO *presp_data;
119 __u32 posix_flags = 0;
120 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
121 struct cifs_fattr fattr;
122 struct tcon_link *tlink;
96daf2b0 123 struct cifs_tcon *tcon;
608712fe 124
f96637be 125 cifs_dbg(FYI, "posix open %s\n", full_path);
608712fe
JL
126
127 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
128 if (presp_data == NULL)
129 return -ENOMEM;
130
131 tlink = cifs_sb_tlink(cifs_sb);
132 if (IS_ERR(tlink)) {
133 rc = PTR_ERR(tlink);
134 goto posix_open_ret;
135 }
136
137 tcon = tlink_tcon(tlink);
138 mode &= ~current_umask();
139
140 posix_flags = cifs_posix_convert_flags(f_flags);
141 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
142 poplock, full_path, cifs_sb->local_nls,
143 cifs_sb->mnt_cifs_flags &
144 CIFS_MOUNT_MAP_SPECIAL_CHR);
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
3d3ea8e6
SP
225 if (backup_cred(cifs_sb))
226 create_options |= CREATE_OPEN_BACKUP_INTENT;
227
226730b4
PS
228 oparms.tcon = tcon;
229 oparms.cifs_sb = cifs_sb;
230 oparms.desired_access = desired_access;
231 oparms.create_options = create_options;
232 oparms.disposition = disposition;
233 oparms.path = full_path;
234 oparms.fid = fid;
9cbc0b73 235 oparms.reconnect = false;
226730b4
PS
236
237 rc = server->ops->open(xid, &oparms, oplock, buf);
eeb910a6
PS
238
239 if (rc)
240 goto out;
241
242 if (tcon->unix_ext)
243 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
244 xid);
245 else
246 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
42eacf9e 247 xid, fid);
eeb910a6
PS
248
249out:
250 kfree(buf);
251 return rc;
252}
253
63b7d3a4
PS
254static bool
255cifs_has_mand_locks(struct cifsInodeInfo *cinode)
256{
257 struct cifs_fid_locks *cur;
258 bool has_locks = false;
259
260 down_read(&cinode->lock_sem);
261 list_for_each_entry(cur, &cinode->llist, llist) {
262 if (!list_empty(&cur->locks)) {
263 has_locks = true;
264 break;
265 }
266 }
267 up_read(&cinode->lock_sem);
268 return has_locks;
269}
270
15ecb436 271struct cifsFileInfo *
fb1214e4 272cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
15ecb436
JL
273 struct tcon_link *tlink, __u32 oplock)
274{
275 struct dentry *dentry = file->f_path.dentry;
276 struct inode *inode = dentry->d_inode;
4b4de76e
PS
277 struct cifsInodeInfo *cinode = CIFS_I(inode);
278 struct cifsFileInfo *cfile;
f45d3416 279 struct cifs_fid_locks *fdlocks;
233839b1 280 struct cifs_tcon *tcon = tlink_tcon(tlink);
63b7d3a4 281 struct TCP_Server_Info *server = tcon->ses->server;
4b4de76e
PS
282
283 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
284 if (cfile == NULL)
285 return cfile;
286
f45d3416
PS
287 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
288 if (!fdlocks) {
289 kfree(cfile);
290 return NULL;
291 }
292
293 INIT_LIST_HEAD(&fdlocks->locks);
294 fdlocks->cfile = cfile;
295 cfile->llist = fdlocks;
1b4b55a1 296 down_write(&cinode->lock_sem);
f45d3416 297 list_add(&fdlocks->llist, &cinode->llist);
1b4b55a1 298 up_write(&cinode->lock_sem);
f45d3416 299
4b4de76e 300 cfile->count = 1;
4b4de76e
PS
301 cfile->pid = current->tgid;
302 cfile->uid = current_fsuid();
303 cfile->dentry = dget(dentry);
304 cfile->f_flags = file->f_flags;
305 cfile->invalidHandle = false;
306 cfile->tlink = cifs_get_tlink(tlink);
4b4de76e 307 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
f45d3416 308 mutex_init(&cfile->fh_mutex);
15ecb436 309
24261fc2
MG
310 cifs_sb_active(inode->i_sb);
311
63b7d3a4
PS
312 /*
313 * If the server returned a read oplock and we have mandatory brlocks,
314 * set oplock level to None.
315 */
53ef1016 316 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
f96637be 317 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
63b7d3a4
PS
318 oplock = 0;
319 }
320
4477288a 321 spin_lock(&cifs_file_list_lock);
63b7d3a4 322 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
233839b1
PS
323 oplock = fid->pending_open->oplock;
324 list_del(&fid->pending_open->olist);
325
42873b0a 326 fid->purge_cache = false;
63b7d3a4 327 server->ops->set_fid(cfile, fid, oplock);
233839b1
PS
328
329 list_add(&cfile->tlist, &tcon->openFileList);
15ecb436
JL
330 /* if readable file instance put first in list*/
331 if (file->f_mode & FMODE_READ)
4b4de76e 332 list_add(&cfile->flist, &cinode->openFileList);
15ecb436 333 else
4b4de76e 334 list_add_tail(&cfile->flist, &cinode->openFileList);
4477288a 335 spin_unlock(&cifs_file_list_lock);
15ecb436 336
42873b0a 337 if (fid->purge_cache)
4f73c7d3 338 cifs_zap_mapping(inode);
42873b0a 339
4b4de76e
PS
340 file->private_data = cfile;
341 return cfile;
15ecb436
JL
342}
343
764a1b1a
JL
344struct cifsFileInfo *
345cifsFileInfo_get(struct cifsFileInfo *cifs_file)
346{
347 spin_lock(&cifs_file_list_lock);
348 cifsFileInfo_get_locked(cifs_file);
349 spin_unlock(&cifs_file_list_lock);
350 return cifs_file;
351}
352
cdff08e7
SF
353/*
354 * Release a reference on the file private data. This may involve closing
5f6dbc9e
JL
355 * the filehandle out on the server. Must be called without holding
356 * cifs_file_list_lock.
cdff08e7 357 */
b33879aa
JL
358void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
359{
e66673e3 360 struct inode *inode = cifs_file->dentry->d_inode;
96daf2b0 361 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
233839b1 362 struct TCP_Server_Info *server = tcon->ses->server;
e66673e3 363 struct cifsInodeInfo *cifsi = CIFS_I(inode);
24261fc2
MG
364 struct super_block *sb = inode->i_sb;
365 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
cdff08e7 366 struct cifsLockInfo *li, *tmp;
233839b1
PS
367 struct cifs_fid fid;
368 struct cifs_pending_open open;
cdff08e7
SF
369
370 spin_lock(&cifs_file_list_lock);
5f6dbc9e 371 if (--cifs_file->count > 0) {
cdff08e7
SF
372 spin_unlock(&cifs_file_list_lock);
373 return;
374 }
375
233839b1
PS
376 if (server->ops->get_lease_key)
377 server->ops->get_lease_key(inode, &fid);
378
379 /* store open in pending opens to make sure we don't miss lease break */
380 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
381
cdff08e7
SF
382 /* remove it from the lists */
383 list_del(&cifs_file->flist);
384 list_del(&cifs_file->tlist);
385
386 if (list_empty(&cifsi->openFileList)) {
f96637be
JP
387 cifs_dbg(FYI, "closing last open instance for inode %p\n",
388 cifs_file->dentry->d_inode);
25364138
PS
389 /*
390 * In strict cache mode we need invalidate mapping on the last
391 * close because it may cause a error when we open this file
392 * again and get at least level II oplock.
393 */
4f8ba8a0 394 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
aff8d5ca 395 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
c6723628 396 cifs_set_oplock_level(cifsi, 0);
cdff08e7
SF
397 }
398 spin_unlock(&cifs_file_list_lock);
399
ad635942
JL
400 cancel_work_sync(&cifs_file->oplock_break);
401
cdff08e7 402 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
0ff78a22 403 struct TCP_Server_Info *server = tcon->ses->server;
6d5786a3 404 unsigned int xid;
0ff78a22 405
6d5786a3 406 xid = get_xid();
0ff78a22 407 if (server->ops->close)
760ad0ca
PS
408 server->ops->close(xid, tcon, &cifs_file->fid);
409 _free_xid(xid);
cdff08e7
SF
410 }
411
233839b1
PS
412 cifs_del_pending_open(&open);
413
f45d3416
PS
414 /*
415 * Delete any outstanding lock records. We'll lose them when the file
cdff08e7
SF
416 * is closed anyway.
417 */
1b4b55a1 418 down_write(&cifsi->lock_sem);
f45d3416 419 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
cdff08e7 420 list_del(&li->llist);
85160e03 421 cifs_del_lock_waiters(li);
cdff08e7 422 kfree(li);
b33879aa 423 }
f45d3416
PS
424 list_del(&cifs_file->llist->llist);
425 kfree(cifs_file->llist);
1b4b55a1 426 up_write(&cifsi->lock_sem);
cdff08e7
SF
427
428 cifs_put_tlink(cifs_file->tlink);
429 dput(cifs_file->dentry);
24261fc2 430 cifs_sb_deactive(sb);
cdff08e7 431 kfree(cifs_file);
b33879aa
JL
432}
433
1da177e4 434int cifs_open(struct inode *inode, struct file *file)
233839b1 435
1da177e4
LT
436{
437 int rc = -EACCES;
6d5786a3 438 unsigned int xid;
590a3fe0 439 __u32 oplock;
1da177e4 440 struct cifs_sb_info *cifs_sb;
b8c32dbb 441 struct TCP_Server_Info *server;
96daf2b0 442 struct cifs_tcon *tcon;
7ffec372 443 struct tcon_link *tlink;
fb1214e4 444 struct cifsFileInfo *cfile = NULL;
1da177e4 445 char *full_path = NULL;
7e12eddb 446 bool posix_open_ok = false;
fb1214e4 447 struct cifs_fid fid;
233839b1 448 struct cifs_pending_open open;
1da177e4 449
6d5786a3 450 xid = get_xid();
1da177e4
LT
451
452 cifs_sb = CIFS_SB(inode->i_sb);
7ffec372
JL
453 tlink = cifs_sb_tlink(cifs_sb);
454 if (IS_ERR(tlink)) {
6d5786a3 455 free_xid(xid);
7ffec372
JL
456 return PTR_ERR(tlink);
457 }
458 tcon = tlink_tcon(tlink);
b8c32dbb 459 server = tcon->ses->server;
1da177e4 460
e6a00296 461 full_path = build_path_from_dentry(file->f_path.dentry);
1da177e4 462 if (full_path == NULL) {
0f3bc09e 463 rc = -ENOMEM;
232341ba 464 goto out;
1da177e4
LT
465 }
466
f96637be 467 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
b6b38f70 468 inode, file->f_flags, full_path);
276a74a4 469
787aded6
NJ
470 if (file->f_flags & O_DIRECT &&
471 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
472 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
473 file->f_op = &cifs_file_direct_nobrl_ops;
474 else
475 file->f_op = &cifs_file_direct_ops;
476 }
477
233839b1 478 if (server->oplocks)
276a74a4
SF
479 oplock = REQ_OPLOCK;
480 else
481 oplock = 0;
482
64cc2c63 483 if (!tcon->broken_posix_open && tcon->unix_ext &&
29e20f9c
PS
484 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
485 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
276a74a4 486 /* can not refresh inode info since size could be stale */
2422f676 487 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
fa588e0c 488 cifs_sb->mnt_file_mode /* ignored */,
fb1214e4 489 file->f_flags, &oplock, &fid.netfid, xid);
276a74a4 490 if (rc == 0) {
f96637be 491 cifs_dbg(FYI, "posix open succeeded\n");
7e12eddb 492 posix_open_ok = true;
64cc2c63
SF
493 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
494 if (tcon->ses->serverNOS)
f96637be
JP
495 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",
496 tcon->ses->serverName,
497 tcon->ses->serverNOS);
64cc2c63 498 tcon->broken_posix_open = true;
276a74a4
SF
499 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
500 (rc != -EOPNOTSUPP)) /* path not found or net err */
501 goto out;
fb1214e4
PS
502 /*
503 * Else fallthrough to retry open the old way on network i/o
504 * or DFS errors.
505 */
276a74a4
SF
506 }
507
233839b1
PS
508 if (server->ops->get_lease_key)
509 server->ops->get_lease_key(inode, &fid);
510
511 cifs_add_pending_open(&fid, tlink, &open);
512
7e12eddb 513 if (!posix_open_ok) {
b8c32dbb
PS
514 if (server->ops->get_lease_key)
515 server->ops->get_lease_key(inode, &fid);
516
7e12eddb 517 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
fb1214e4 518 file->f_flags, &oplock, &fid, xid);
233839b1
PS
519 if (rc) {
520 cifs_del_pending_open(&open);
7e12eddb 521 goto out;
233839b1 522 }
7e12eddb 523 }
47c78b7f 524
fb1214e4
PS
525 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
526 if (cfile == NULL) {
b8c32dbb
PS
527 if (server->ops->close)
528 server->ops->close(xid, tcon, &fid);
233839b1 529 cifs_del_pending_open(&open);
1da177e4
LT
530 rc = -ENOMEM;
531 goto out;
532 }
1da177e4 533
9451a9a5
SJ
534 cifs_fscache_set_inode_cookie(inode, file);
535
7e12eddb 536 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
fb1214e4
PS
537 /*
538 * Time to set mode which we can not set earlier due to
539 * problems creating new read-only files.
540 */
7e12eddb
PS
541 struct cifs_unix_set_info_args args = {
542 .mode = inode->i_mode,
49418b2c
EB
543 .uid = INVALID_UID, /* no change */
544 .gid = INVALID_GID, /* no change */
7e12eddb
PS
545 .ctime = NO_CHANGE_64,
546 .atime = NO_CHANGE_64,
547 .mtime = NO_CHANGE_64,
548 .device = 0,
549 };
fb1214e4
PS
550 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
551 cfile->pid);
1da177e4
LT
552 }
553
554out:
1da177e4 555 kfree(full_path);
6d5786a3 556 free_xid(xid);
7ffec372 557 cifs_put_tlink(tlink);
1da177e4
LT
558 return rc;
559}
560
f152fd5f
PS
561static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
562
2ae78ba8
PS
563/*
564 * Try to reacquire byte range locks that were released when session
f152fd5f 565 * to server was lost.
2ae78ba8 566 */
f152fd5f
PS
567static int
568cifs_relock_file(struct cifsFileInfo *cfile)
1da177e4 569{
f152fd5f
PS
570 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
571 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
572 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1da177e4
LT
573 int rc = 0;
574
689c3db4 575 down_read(&cinode->lock_sem);
f152fd5f 576 if (cinode->can_cache_brlcks) {
689c3db4
PS
577 /* can cache locks - no need to relock */
578 up_read(&cinode->lock_sem);
f152fd5f
PS
579 return rc;
580 }
581
582 if (cap_unix(tcon->ses) &&
583 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
584 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
585 rc = cifs_push_posix_locks(cfile);
586 else
587 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1da177e4 588
689c3db4 589 up_read(&cinode->lock_sem);
1da177e4
LT
590 return rc;
591}
592
2ae78ba8
PS
593static int
594cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
1da177e4
LT
595{
596 int rc = -EACCES;
6d5786a3 597 unsigned int xid;
590a3fe0 598 __u32 oplock;
1da177e4 599 struct cifs_sb_info *cifs_sb;
96daf2b0 600 struct cifs_tcon *tcon;
2ae78ba8
PS
601 struct TCP_Server_Info *server;
602 struct cifsInodeInfo *cinode;
fb8c4b14 603 struct inode *inode;
1da177e4 604 char *full_path = NULL;
2ae78ba8 605 int desired_access;
1da177e4 606 int disposition = FILE_OPEN;
3d3ea8e6 607 int create_options = CREATE_NOT_DIR;
226730b4 608 struct cifs_open_parms oparms;
1da177e4 609
6d5786a3 610 xid = get_xid();
2ae78ba8
PS
611 mutex_lock(&cfile->fh_mutex);
612 if (!cfile->invalidHandle) {
613 mutex_unlock(&cfile->fh_mutex);
0f3bc09e 614 rc = 0;
6d5786a3 615 free_xid(xid);
0f3bc09e 616 return rc;
1da177e4
LT
617 }
618
2ae78ba8 619 inode = cfile->dentry->d_inode;
1da177e4 620 cifs_sb = CIFS_SB(inode->i_sb);
2ae78ba8
PS
621 tcon = tlink_tcon(cfile->tlink);
622 server = tcon->ses->server;
623
624 /*
625 * Can not grab rename sem here because various ops, including those
626 * that already have the rename sem can end up causing writepage to get
627 * called and if the server was down that means we end up here, and we
628 * can never tell if the caller already has the rename_sem.
629 */
630 full_path = build_path_from_dentry(cfile->dentry);
1da177e4 631 if (full_path == NULL) {
3a9f462f 632 rc = -ENOMEM;
2ae78ba8 633 mutex_unlock(&cfile->fh_mutex);
6d5786a3 634 free_xid(xid);
3a9f462f 635 return rc;
1da177e4
LT
636 }
637
f96637be
JP
638 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
639 inode, cfile->f_flags, full_path);
1da177e4 640
10b9b98e 641 if (tcon->ses->server->oplocks)
1da177e4
LT
642 oplock = REQ_OPLOCK;
643 else
4b18f2a9 644 oplock = 0;
1da177e4 645
29e20f9c 646 if (tcon->unix_ext && cap_unix(tcon->ses) &&
7fc8f4e9 647 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
29e20f9c 648 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
608712fe
JL
649 /*
650 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
651 * original open. Must mask them off for a reopen.
652 */
2ae78ba8 653 unsigned int oflags = cfile->f_flags &
15886177 654 ~(O_CREAT | O_EXCL | O_TRUNC);
608712fe 655
2422f676 656 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
2ae78ba8 657 cifs_sb->mnt_file_mode /* ignored */,
9cbc0b73 658 oflags, &oplock, &cfile->fid.netfid, xid);
7fc8f4e9 659 if (rc == 0) {
f96637be 660 cifs_dbg(FYI, "posix reopen succeeded\n");
fe090e4e 661 oparms.reconnect = true;
7fc8f4e9
SF
662 goto reopen_success;
663 }
2ae78ba8
PS
664 /*
665 * fallthrough to retry open the old way on errors, especially
666 * in the reconnect path it is important to retry hard
667 */
7fc8f4e9
SF
668 }
669
2ae78ba8 670 desired_access = cifs_convert_flags(cfile->f_flags);
7fc8f4e9 671
3d3ea8e6
SP
672 if (backup_cred(cifs_sb))
673 create_options |= CREATE_OPEN_BACKUP_INTENT;
674
b8c32dbb 675 if (server->ops->get_lease_key)
9cbc0b73 676 server->ops->get_lease_key(inode, &cfile->fid);
b8c32dbb 677
226730b4
PS
678 oparms.tcon = tcon;
679 oparms.cifs_sb = cifs_sb;
680 oparms.desired_access = desired_access;
681 oparms.create_options = create_options;
682 oparms.disposition = disposition;
683 oparms.path = full_path;
9cbc0b73
PS
684 oparms.fid = &cfile->fid;
685 oparms.reconnect = true;
226730b4 686
2ae78ba8
PS
687 /*
688 * Can not refresh inode by passing in file_info buf to be returned by
d81b8a40 689 * ops->open and then calling get_inode_info with returned buf since
2ae78ba8
PS
690 * file might have write behind data that needs to be flushed and server
691 * version of file size can be stale. If we knew for sure that inode was
692 * not dirty locally we could do this.
693 */
226730b4 694 rc = server->ops->open(xid, &oparms, &oplock, NULL);
b33fcf1c
PS
695 if (rc == -ENOENT && oparms.reconnect == false) {
696 /* durable handle timeout is expired - open the file again */
697 rc = server->ops->open(xid, &oparms, &oplock, NULL);
698 /* indicate that we need to relock the file */
699 oparms.reconnect = true;
700 }
701
1da177e4 702 if (rc) {
2ae78ba8 703 mutex_unlock(&cfile->fh_mutex);
f96637be
JP
704 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
705 cifs_dbg(FYI, "oplock: %d\n", oplock);
15886177
JL
706 goto reopen_error_exit;
707 }
708
7fc8f4e9 709reopen_success:
2ae78ba8
PS
710 cfile->invalidHandle = false;
711 mutex_unlock(&cfile->fh_mutex);
712 cinode = CIFS_I(inode);
15886177
JL
713
714 if (can_flush) {
715 rc = filemap_write_and_wait(inode->i_mapping);
eb4b756b 716 mapping_set_error(inode->i_mapping, rc);
15886177 717
15886177 718 if (tcon->unix_ext)
2ae78ba8
PS
719 rc = cifs_get_inode_info_unix(&inode, full_path,
720 inode->i_sb, xid);
15886177 721 else
2ae78ba8
PS
722 rc = cifs_get_inode_info(&inode, full_path, NULL,
723 inode->i_sb, xid, NULL);
724 }
725 /*
726 * Else we are writing out data to server already and could deadlock if
727 * we tried to flush data, and since we do not know if we have data that
728 * would invalidate the current end of file on the server we can not go
729 * to the server to get the new inode info.
730 */
731
9cbc0b73
PS
732 server->ops->set_fid(cfile, &cfile->fid, oplock);
733 if (oparms.reconnect)
734 cifs_relock_file(cfile);
15886177
JL
735
736reopen_error_exit:
1da177e4 737 kfree(full_path);
6d5786a3 738 free_xid(xid);
1da177e4
LT
739 return rc;
740}
741
742int cifs_close(struct inode *inode, struct file *file)
743{
77970693
JL
744 if (file->private_data != NULL) {
745 cifsFileInfo_put(file->private_data);
746 file->private_data = NULL;
747 }
7ee1af76 748
cdff08e7
SF
749 /* return code from the ->release op is always ignored */
750 return 0;
1da177e4
LT
751}
752
753int cifs_closedir(struct inode *inode, struct file *file)
754{
755 int rc = 0;
6d5786a3 756 unsigned int xid;
4b4de76e 757 struct cifsFileInfo *cfile = file->private_data;
92fc65a7
PS
758 struct cifs_tcon *tcon;
759 struct TCP_Server_Info *server;
760 char *buf;
1da177e4 761
f96637be 762 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1da177e4 763
92fc65a7
PS
764 if (cfile == NULL)
765 return rc;
766
6d5786a3 767 xid = get_xid();
92fc65a7
PS
768 tcon = tlink_tcon(cfile->tlink);
769 server = tcon->ses->server;
1da177e4 770
f96637be 771 cifs_dbg(FYI, "Freeing private data in close dir\n");
92fc65a7 772 spin_lock(&cifs_file_list_lock);
52755808 773 if (server->ops->dir_needs_close(cfile)) {
92fc65a7
PS
774 cfile->invalidHandle = true;
775 spin_unlock(&cifs_file_list_lock);
776 if (server->ops->close_dir)
777 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
778 else
779 rc = -ENOSYS;
f96637be 780 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
92fc65a7
PS
781 /* not much we can do if it fails anyway, ignore rc */
782 rc = 0;
783 } else
784 spin_unlock(&cifs_file_list_lock);
785
786 buf = cfile->srch_inf.ntwrk_buf_start;
787 if (buf) {
f96637be 788 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
92fc65a7
PS
789 cfile->srch_inf.ntwrk_buf_start = NULL;
790 if (cfile->srch_inf.smallBuf)
791 cifs_small_buf_release(buf);
792 else
793 cifs_buf_release(buf);
1da177e4 794 }
92fc65a7
PS
795
796 cifs_put_tlink(cfile->tlink);
797 kfree(file->private_data);
798 file->private_data = NULL;
1da177e4 799 /* BB can we lock the filestruct while this is going on? */
6d5786a3 800 free_xid(xid);
1da177e4
LT
801 return rc;
802}
803
85160e03 804static struct cifsLockInfo *
fbd35aca 805cifs_lock_init(__u64 offset, __u64 length, __u8 type)
7ee1af76 806{
a88b4707 807 struct cifsLockInfo *lock =
fb8c4b14 808 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
a88b4707
PS
809 if (!lock)
810 return lock;
811 lock->offset = offset;
812 lock->length = length;
813 lock->type = type;
a88b4707
PS
814 lock->pid = current->tgid;
815 INIT_LIST_HEAD(&lock->blist);
816 init_waitqueue_head(&lock->block_q);
817 return lock;
85160e03
PS
818}
819
f7ba7fe6 820void
85160e03
PS
821cifs_del_lock_waiters(struct cifsLockInfo *lock)
822{
823 struct cifsLockInfo *li, *tmp;
824 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
825 list_del_init(&li->blist);
826 wake_up(&li->block_q);
827 }
828}
829
081c0414
PS
830#define CIFS_LOCK_OP 0
831#define CIFS_READ_OP 1
832#define CIFS_WRITE_OP 2
833
834/* @rw_check : 0 - no op, 1 - read, 2 - write */
85160e03 835static bool
f45d3416
PS
836cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
837 __u64 length, __u8 type, struct cifsFileInfo *cfile,
081c0414 838 struct cifsLockInfo **conf_lock, int rw_check)
85160e03 839{
fbd35aca 840 struct cifsLockInfo *li;
f45d3416 841 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
106dc538 842 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
85160e03 843
f45d3416 844 list_for_each_entry(li, &fdlocks->locks, llist) {
85160e03
PS
845 if (offset + length <= li->offset ||
846 offset >= li->offset + li->length)
847 continue;
081c0414
PS
848 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
849 server->ops->compare_fids(cfile, cur_cfile)) {
850 /* shared lock prevents write op through the same fid */
851 if (!(li->type & server->vals->shared_lock_type) ||
852 rw_check != CIFS_WRITE_OP)
853 continue;
854 }
f45d3416
PS
855 if ((type & server->vals->shared_lock_type) &&
856 ((server->ops->compare_fids(cfile, cur_cfile) &&
857 current->tgid == li->pid) || type == li->type))
85160e03 858 continue;
579f9053
PS
859 if (conf_lock)
860 *conf_lock = li;
f45d3416 861 return true;
85160e03
PS
862 }
863 return false;
864}
865
579f9053 866bool
55157dfb 867cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
579f9053 868 __u8 type, struct cifsLockInfo **conf_lock,
081c0414 869 int rw_check)
161ebf9f 870{
fbd35aca 871 bool rc = false;
f45d3416 872 struct cifs_fid_locks *cur;
55157dfb 873 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
fbd35aca 874
f45d3416
PS
875 list_for_each_entry(cur, &cinode->llist, llist) {
876 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
579f9053 877 cfile, conf_lock, rw_check);
fbd35aca
PS
878 if (rc)
879 break;
880 }
fbd35aca
PS
881
882 return rc;
161ebf9f
PS
883}
884
9a5101c8
PS
885/*
886 * Check if there is another lock that prevents us to set the lock (mandatory
887 * style). If such a lock exists, update the flock structure with its
888 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
889 * or leave it the same if we can't. Returns 0 if we don't need to request to
890 * the server or 1 otherwise.
891 */
85160e03 892static int
fbd35aca
PS
893cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
894 __u8 type, struct file_lock *flock)
85160e03
PS
895{
896 int rc = 0;
897 struct cifsLockInfo *conf_lock;
fbd35aca 898 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
106dc538 899 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
85160e03
PS
900 bool exist;
901
1b4b55a1 902 down_read(&cinode->lock_sem);
85160e03 903
55157dfb 904 exist = cifs_find_lock_conflict(cfile, offset, length, type,
081c0414 905 &conf_lock, CIFS_LOCK_OP);
85160e03
PS
906 if (exist) {
907 flock->fl_start = conf_lock->offset;
908 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
909 flock->fl_pid = conf_lock->pid;
106dc538 910 if (conf_lock->type & server->vals->shared_lock_type)
85160e03
PS
911 flock->fl_type = F_RDLCK;
912 else
913 flock->fl_type = F_WRLCK;
914 } else if (!cinode->can_cache_brlcks)
915 rc = 1;
916 else
917 flock->fl_type = F_UNLCK;
918
1b4b55a1 919 up_read(&cinode->lock_sem);
85160e03
PS
920 return rc;
921}
922
161ebf9f 923static void
fbd35aca 924cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
85160e03 925{
fbd35aca 926 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
1b4b55a1 927 down_write(&cinode->lock_sem);
f45d3416 928 list_add_tail(&lock->llist, &cfile->llist->locks);
1b4b55a1 929 up_write(&cinode->lock_sem);
7ee1af76
JA
930}
931
9a5101c8
PS
932/*
933 * Set the byte-range lock (mandatory style). Returns:
934 * 1) 0, if we set the lock and don't need to request to the server;
935 * 2) 1, if no locks prevent us but we need to request to the server;
936 * 3) -EACCESS, if there is a lock that prevents us and wait is false.
937 */
85160e03 938static int
fbd35aca 939cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
161ebf9f 940 bool wait)
85160e03 941{
161ebf9f 942 struct cifsLockInfo *conf_lock;
fbd35aca 943 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
85160e03
PS
944 bool exist;
945 int rc = 0;
946
85160e03
PS
947try_again:
948 exist = false;
1b4b55a1 949 down_write(&cinode->lock_sem);
85160e03 950
55157dfb 951 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
081c0414 952 lock->type, &conf_lock, CIFS_LOCK_OP);
85160e03 953 if (!exist && cinode->can_cache_brlcks) {
f45d3416 954 list_add_tail(&lock->llist, &cfile->llist->locks);
1b4b55a1 955 up_write(&cinode->lock_sem);
85160e03
PS
956 return rc;
957 }
958
959 if (!exist)
960 rc = 1;
961 else if (!wait)
962 rc = -EACCES;
963 else {
964 list_add_tail(&lock->blist, &conf_lock->blist);
1b4b55a1 965 up_write(&cinode->lock_sem);
85160e03
PS
966 rc = wait_event_interruptible(lock->block_q,
967 (lock->blist.prev == &lock->blist) &&
968 (lock->blist.next == &lock->blist));
969 if (!rc)
970 goto try_again;
1b4b55a1 971 down_write(&cinode->lock_sem);
a88b4707 972 list_del_init(&lock->blist);
85160e03
PS
973 }
974
1b4b55a1 975 up_write(&cinode->lock_sem);
85160e03
PS
976 return rc;
977}
978
9a5101c8
PS
979/*
980 * Check if there is another lock that prevents us to set the lock (posix
981 * style). If such a lock exists, update the flock structure with its
982 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
983 * or leave it the same if we can't. Returns 0 if we don't need to request to
984 * the server or 1 otherwise.
985 */
85160e03 986static int
4f6bcec9
PS
987cifs_posix_lock_test(struct file *file, struct file_lock *flock)
988{
989 int rc = 0;
496ad9aa 990 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
4f6bcec9
PS
991 unsigned char saved_type = flock->fl_type;
992
50792760
PS
993 if ((flock->fl_flags & FL_POSIX) == 0)
994 return 1;
995
1b4b55a1 996 down_read(&cinode->lock_sem);
4f6bcec9
PS
997 posix_test_lock(file, flock);
998
999 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1000 flock->fl_type = saved_type;
1001 rc = 1;
1002 }
1003
1b4b55a1 1004 up_read(&cinode->lock_sem);
4f6bcec9
PS
1005 return rc;
1006}
1007
9a5101c8
PS
1008/*
1009 * Set the byte-range lock (posix style). Returns:
1010 * 1) 0, if we set the lock and don't need to request to the server;
1011 * 2) 1, if we need to request to the server;
1012 * 3) <0, if the error occurs while setting the lock.
1013 */
4f6bcec9
PS
1014static int
1015cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1016{
496ad9aa 1017 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
50792760
PS
1018 int rc = 1;
1019
1020 if ((flock->fl_flags & FL_POSIX) == 0)
1021 return rc;
4f6bcec9 1022
66189be7 1023try_again:
1b4b55a1 1024 down_write(&cinode->lock_sem);
4f6bcec9 1025 if (!cinode->can_cache_brlcks) {
1b4b55a1 1026 up_write(&cinode->lock_sem);
50792760 1027 return rc;
4f6bcec9 1028 }
66189be7
PS
1029
1030 rc = posix_lock_file(file, flock, NULL);
1b4b55a1 1031 up_write(&cinode->lock_sem);
66189be7
PS
1032 if (rc == FILE_LOCK_DEFERRED) {
1033 rc = wait_event_interruptible(flock->fl_wait, !flock->fl_next);
1034 if (!rc)
1035 goto try_again;
1a9e64a7 1036 posix_unblock_lock(flock);
66189be7 1037 }
9ebb389d 1038 return rc;
4f6bcec9
PS
1039}
1040
d39a4f71 1041int
4f6bcec9 1042cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
85160e03 1043{
6d5786a3
PS
1044 unsigned int xid;
1045 int rc = 0, stored_rc;
85160e03
PS
1046 struct cifsLockInfo *li, *tmp;
1047 struct cifs_tcon *tcon;
0013fb4c 1048 unsigned int num, max_num, max_buf;
32b9aaf1
PS
1049 LOCKING_ANDX_RANGE *buf, *cur;
1050 int types[] = {LOCKING_ANDX_LARGE_FILES,
1051 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
1052 int i;
85160e03 1053
6d5786a3 1054 xid = get_xid();
85160e03
PS
1055 tcon = tlink_tcon(cfile->tlink);
1056
0013fb4c
PS
1057 /*
1058 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1059 * and check it for zero before using.
1060 */
1061 max_buf = tcon->ses->server->maxBuf;
1062 if (!max_buf) {
6d5786a3 1063 free_xid(xid);
0013fb4c
PS
1064 return -EINVAL;
1065 }
1066
1067 max_num = (max_buf - sizeof(struct smb_hdr)) /
1068 sizeof(LOCKING_ANDX_RANGE);
4b99d39b 1069 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
32b9aaf1 1070 if (!buf) {
6d5786a3 1071 free_xid(xid);
e2f2886a 1072 return -ENOMEM;
32b9aaf1
PS
1073 }
1074
1075 for (i = 0; i < 2; i++) {
1076 cur = buf;
1077 num = 0;
f45d3416 1078 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
32b9aaf1
PS
1079 if (li->type != types[i])
1080 continue;
1081 cur->Pid = cpu_to_le16(li->pid);
1082 cur->LengthLow = cpu_to_le32((u32)li->length);
1083 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1084 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1085 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1086 if (++num == max_num) {
4b4de76e
PS
1087 stored_rc = cifs_lockv(xid, tcon,
1088 cfile->fid.netfid,
04a6aa8a
PS
1089 (__u8)li->type, 0, num,
1090 buf);
32b9aaf1
PS
1091 if (stored_rc)
1092 rc = stored_rc;
1093 cur = buf;
1094 num = 0;
1095 } else
1096 cur++;
1097 }
1098
1099 if (num) {
4b4de76e 1100 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
04a6aa8a 1101 (__u8)types[i], 0, num, buf);
32b9aaf1
PS
1102 if (stored_rc)
1103 rc = stored_rc;
1104 }
85160e03
PS
1105 }
1106
32b9aaf1 1107 kfree(buf);
6d5786a3 1108 free_xid(xid);
85160e03
PS
1109 return rc;
1110}
1111
d5751469
PS
1112struct lock_to_push {
1113 struct list_head llist;
1114 __u64 offset;
1115 __u64 length;
1116 __u32 pid;
1117 __u16 netfid;
1118 __u8 type;
1119};
1120
4f6bcec9 1121static int
b8db928b 1122cifs_push_posix_locks(struct cifsFileInfo *cfile)
4f6bcec9 1123{
1c8c601a 1124 struct inode *inode = cfile->dentry->d_inode;
4f6bcec9 1125 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
bd61e0a9
JL
1126 struct file_lock *flock;
1127 struct file_lock_context *flctx = inode->i_flctx;
1128 unsigned int count = 0, i;
4f6bcec9 1129 int rc = 0, xid, type;
d5751469
PS
1130 struct list_head locks_to_send, *el;
1131 struct lock_to_push *lck, *tmp;
4f6bcec9 1132 __u64 length;
4f6bcec9 1133
6d5786a3 1134 xid = get_xid();
4f6bcec9 1135
bd61e0a9
JL
1136 if (!flctx)
1137 goto out;
1138
1c8c601a 1139 spin_lock(&inode->i_lock);
bd61e0a9
JL
1140 list_for_each(el, &flctx->flc_posix) {
1141 count++;
d5751469 1142 }
1c8c601a 1143 spin_unlock(&inode->i_lock);
d5751469 1144
4f6bcec9
PS
1145 INIT_LIST_HEAD(&locks_to_send);
1146
d5751469 1147 /*
ce85852b 1148 * Allocating count locks is enough because no FL_POSIX locks can be
1b4b55a1 1149 * added to the list while we are holding cinode->lock_sem that
ce85852b 1150 * protects locking operations of this inode.
d5751469 1151 */
bd61e0a9 1152 for (i = 0; i < count; i++) {
d5751469
PS
1153 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1154 if (!lck) {
1155 rc = -ENOMEM;
1156 goto err_out;
1157 }
1158 list_add_tail(&lck->llist, &locks_to_send);
1159 }
1160
d5751469 1161 el = locks_to_send.next;
1c8c601a 1162 spin_lock(&inode->i_lock);
bd61e0a9 1163 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
d5751469 1164 if (el == &locks_to_send) {
ce85852b
PS
1165 /*
1166 * The list ended. We don't have enough allocated
1167 * structures - something is really wrong.
1168 */
f96637be 1169 cifs_dbg(VFS, "Can't push all brlocks!\n");
d5751469
PS
1170 break;
1171 }
4f6bcec9
PS
1172 length = 1 + flock->fl_end - flock->fl_start;
1173 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1174 type = CIFS_RDLCK;
1175 else
1176 type = CIFS_WRLCK;
d5751469 1177 lck = list_entry(el, struct lock_to_push, llist);
4f6bcec9 1178 lck->pid = flock->fl_pid;
4b4de76e 1179 lck->netfid = cfile->fid.netfid;
d5751469
PS
1180 lck->length = length;
1181 lck->type = type;
1182 lck->offset = flock->fl_start;
4f6bcec9 1183 }
1c8c601a 1184 spin_unlock(&inode->i_lock);
4f6bcec9
PS
1185
1186 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
4f6bcec9
PS
1187 int stored_rc;
1188
4f6bcec9 1189 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
c5fd363d 1190 lck->offset, lck->length, NULL,
4f6bcec9
PS
1191 lck->type, 0);
1192 if (stored_rc)
1193 rc = stored_rc;
1194 list_del(&lck->llist);
1195 kfree(lck);
1196 }
1197
d5751469 1198out:
6d5786a3 1199 free_xid(xid);
4f6bcec9 1200 return rc;
d5751469
PS
1201err_out:
1202 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1203 list_del(&lck->llist);
1204 kfree(lck);
1205 }
1206 goto out;
4f6bcec9
PS
1207}
1208
9ec3c882 1209static int
b8db928b 1210cifs_push_locks(struct cifsFileInfo *cfile)
9ec3c882 1211{
b8db928b 1212 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
9ec3c882 1213 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
b8db928b 1214 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
9ec3c882
PS
1215 int rc = 0;
1216
1217 /* we are going to update can_cache_brlcks here - need a write access */
1218 down_write(&cinode->lock_sem);
1219 if (!cinode->can_cache_brlcks) {
1220 up_write(&cinode->lock_sem);
1221 return rc;
1222 }
4f6bcec9 1223
29e20f9c 1224 if (cap_unix(tcon->ses) &&
4f6bcec9
PS
1225 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1226 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
b8db928b
PS
1227 rc = cifs_push_posix_locks(cfile);
1228 else
1229 rc = tcon->ses->server->ops->push_mand_locks(cfile);
4f6bcec9 1230
b8db928b
PS
1231 cinode->can_cache_brlcks = false;
1232 up_write(&cinode->lock_sem);
1233 return rc;
4f6bcec9
PS
1234}
1235
03776f45 1236static void
04a6aa8a 1237cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
106dc538 1238 bool *wait_flag, struct TCP_Server_Info *server)
1da177e4 1239{
03776f45 1240 if (flock->fl_flags & FL_POSIX)
f96637be 1241 cifs_dbg(FYI, "Posix\n");
03776f45 1242 if (flock->fl_flags & FL_FLOCK)
f96637be 1243 cifs_dbg(FYI, "Flock\n");
03776f45 1244 if (flock->fl_flags & FL_SLEEP) {
f96637be 1245 cifs_dbg(FYI, "Blocking lock\n");
03776f45 1246 *wait_flag = true;
1da177e4 1247 }
03776f45 1248 if (flock->fl_flags & FL_ACCESS)
f96637be 1249 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
03776f45 1250 if (flock->fl_flags & FL_LEASE)
f96637be 1251 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
03776f45 1252 if (flock->fl_flags &
3d6d854a
JL
1253 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1254 FL_ACCESS | FL_LEASE | FL_CLOSE)))
f96637be 1255 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1da177e4 1256
106dc538 1257 *type = server->vals->large_lock_type;
03776f45 1258 if (flock->fl_type == F_WRLCK) {
f96637be 1259 cifs_dbg(FYI, "F_WRLCK\n");
106dc538 1260 *type |= server->vals->exclusive_lock_type;
03776f45
PS
1261 *lock = 1;
1262 } else if (flock->fl_type == F_UNLCK) {
f96637be 1263 cifs_dbg(FYI, "F_UNLCK\n");
106dc538 1264 *type |= server->vals->unlock_lock_type;
03776f45
PS
1265 *unlock = 1;
1266 /* Check if unlock includes more than one lock range */
1267 } else if (flock->fl_type == F_RDLCK) {
f96637be 1268 cifs_dbg(FYI, "F_RDLCK\n");
106dc538 1269 *type |= server->vals->shared_lock_type;
03776f45
PS
1270 *lock = 1;
1271 } else if (flock->fl_type == F_EXLCK) {
f96637be 1272 cifs_dbg(FYI, "F_EXLCK\n");
106dc538 1273 *type |= server->vals->exclusive_lock_type;
03776f45
PS
1274 *lock = 1;
1275 } else if (flock->fl_type == F_SHLCK) {
f96637be 1276 cifs_dbg(FYI, "F_SHLCK\n");
106dc538 1277 *type |= server->vals->shared_lock_type;
03776f45 1278 *lock = 1;
1da177e4 1279 } else
f96637be 1280 cifs_dbg(FYI, "Unknown type of lock\n");
03776f45 1281}
1da177e4 1282
03776f45 1283static int
04a6aa8a 1284cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
6d5786a3 1285 bool wait_flag, bool posix_lck, unsigned int xid)
03776f45
PS
1286{
1287 int rc = 0;
1288 __u64 length = 1 + flock->fl_end - flock->fl_start;
4f6bcec9
PS
1289 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1290 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
106dc538 1291 struct TCP_Server_Info *server = tcon->ses->server;
4b4de76e 1292 __u16 netfid = cfile->fid.netfid;
f05337c6 1293
03776f45
PS
1294 if (posix_lck) {
1295 int posix_lock_type;
4f6bcec9
PS
1296
1297 rc = cifs_posix_lock_test(file, flock);
1298 if (!rc)
1299 return rc;
1300
106dc538 1301 if (type & server->vals->shared_lock_type)
03776f45
PS
1302 posix_lock_type = CIFS_RDLCK;
1303 else
1304 posix_lock_type = CIFS_WRLCK;
4f6bcec9 1305 rc = CIFSSMBPosixLock(xid, tcon, netfid, current->tgid,
c5fd363d 1306 flock->fl_start, length, flock,
4f6bcec9 1307 posix_lock_type, wait_flag);
03776f45
PS
1308 return rc;
1309 }
1da177e4 1310
fbd35aca 1311 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
85160e03
PS
1312 if (!rc)
1313 return rc;
1314
03776f45 1315 /* BB we could chain these into one lock request BB */
d39a4f71
PS
1316 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1317 1, 0, false);
03776f45 1318 if (rc == 0) {
d39a4f71
PS
1319 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1320 type, 0, 1, false);
03776f45
PS
1321 flock->fl_type = F_UNLCK;
1322 if (rc != 0)
f96637be
JP
1323 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1324 rc);
a88b4707 1325 return 0;
1da177e4 1326 }
7ee1af76 1327
106dc538 1328 if (type & server->vals->shared_lock_type) {
03776f45 1329 flock->fl_type = F_WRLCK;
a88b4707 1330 return 0;
7ee1af76
JA
1331 }
1332
d39a4f71
PS
1333 type &= ~server->vals->exclusive_lock_type;
1334
1335 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1336 type | server->vals->shared_lock_type,
1337 1, 0, false);
03776f45 1338 if (rc == 0) {
d39a4f71
PS
1339 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1340 type | server->vals->shared_lock_type, 0, 1, false);
03776f45
PS
1341 flock->fl_type = F_RDLCK;
1342 if (rc != 0)
f96637be
JP
1343 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1344 rc);
03776f45
PS
1345 } else
1346 flock->fl_type = F_WRLCK;
1347
a88b4707 1348 return 0;
03776f45
PS
1349}
1350
f7ba7fe6 1351void
9ee305b7
PS
1352cifs_move_llist(struct list_head *source, struct list_head *dest)
1353{
1354 struct list_head *li, *tmp;
1355 list_for_each_safe(li, tmp, source)
1356 list_move(li, dest);
1357}
1358
f7ba7fe6 1359void
9ee305b7
PS
1360cifs_free_llist(struct list_head *llist)
1361{
1362 struct cifsLockInfo *li, *tmp;
1363 list_for_each_entry_safe(li, tmp, llist, llist) {
1364 cifs_del_lock_waiters(li);
1365 list_del(&li->llist);
1366 kfree(li);
1367 }
1368}
1369
d39a4f71 1370int
6d5786a3
PS
1371cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1372 unsigned int xid)
9ee305b7
PS
1373{
1374 int rc = 0, stored_rc;
1375 int types[] = {LOCKING_ANDX_LARGE_FILES,
1376 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES};
1377 unsigned int i;
0013fb4c 1378 unsigned int max_num, num, max_buf;
9ee305b7
PS
1379 LOCKING_ANDX_RANGE *buf, *cur;
1380 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1381 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
1382 struct cifsLockInfo *li, *tmp;
1383 __u64 length = 1 + flock->fl_end - flock->fl_start;
1384 struct list_head tmp_llist;
1385
1386 INIT_LIST_HEAD(&tmp_llist);
1387
0013fb4c
PS
1388 /*
1389 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1390 * and check it for zero before using.
1391 */
1392 max_buf = tcon->ses->server->maxBuf;
1393 if (!max_buf)
1394 return -EINVAL;
1395
1396 max_num = (max_buf - sizeof(struct smb_hdr)) /
1397 sizeof(LOCKING_ANDX_RANGE);
4b99d39b 1398 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
9ee305b7
PS
1399 if (!buf)
1400 return -ENOMEM;
1401
1b4b55a1 1402 down_write(&cinode->lock_sem);
9ee305b7
PS
1403 for (i = 0; i < 2; i++) {
1404 cur = buf;
1405 num = 0;
f45d3416 1406 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
9ee305b7
PS
1407 if (flock->fl_start > li->offset ||
1408 (flock->fl_start + length) <
1409 (li->offset + li->length))
1410 continue;
1411 if (current->tgid != li->pid)
1412 continue;
9ee305b7
PS
1413 if (types[i] != li->type)
1414 continue;
ea319d57 1415 if (cinode->can_cache_brlcks) {
9ee305b7
PS
1416 /*
1417 * We can cache brlock requests - simply remove
fbd35aca 1418 * a lock from the file's list.
9ee305b7
PS
1419 */
1420 list_del(&li->llist);
1421 cifs_del_lock_waiters(li);
1422 kfree(li);
ea319d57 1423 continue;
9ee305b7 1424 }
ea319d57
PS
1425 cur->Pid = cpu_to_le16(li->pid);
1426 cur->LengthLow = cpu_to_le32((u32)li->length);
1427 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1428 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1429 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1430 /*
1431 * We need to save a lock here to let us add it again to
1432 * the file's list if the unlock range request fails on
1433 * the server.
1434 */
1435 list_move(&li->llist, &tmp_llist);
1436 if (++num == max_num) {
4b4de76e
PS
1437 stored_rc = cifs_lockv(xid, tcon,
1438 cfile->fid.netfid,
ea319d57
PS
1439 li->type, num, 0, buf);
1440 if (stored_rc) {
1441 /*
1442 * We failed on the unlock range
1443 * request - add all locks from the tmp
1444 * list to the head of the file's list.
1445 */
1446 cifs_move_llist(&tmp_llist,
f45d3416 1447 &cfile->llist->locks);
ea319d57
PS
1448 rc = stored_rc;
1449 } else
1450 /*
1451 * The unlock range request succeed -
1452 * free the tmp list.
1453 */
1454 cifs_free_llist(&tmp_llist);
1455 cur = buf;
1456 num = 0;
1457 } else
1458 cur++;
9ee305b7
PS
1459 }
1460 if (num) {
4b4de76e 1461 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
9ee305b7
PS
1462 types[i], num, 0, buf);
1463 if (stored_rc) {
f45d3416
PS
1464 cifs_move_llist(&tmp_llist,
1465 &cfile->llist->locks);
9ee305b7
PS
1466 rc = stored_rc;
1467 } else
1468 cifs_free_llist(&tmp_llist);
1469 }
1470 }
1471
1b4b55a1 1472 up_write(&cinode->lock_sem);
9ee305b7
PS
1473 kfree(buf);
1474 return rc;
1475}
1476
03776f45 1477static int
f45d3416 1478cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
6d5786a3
PS
1479 bool wait_flag, bool posix_lck, int lock, int unlock,
1480 unsigned int xid)
03776f45
PS
1481{
1482 int rc = 0;
1483 __u64 length = 1 + flock->fl_end - flock->fl_start;
1484 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1485 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
106dc538 1486 struct TCP_Server_Info *server = tcon->ses->server;
63b7d3a4 1487 struct inode *inode = cfile->dentry->d_inode;
03776f45
PS
1488
1489 if (posix_lck) {
08547b03 1490 int posix_lock_type;
4f6bcec9
PS
1491
1492 rc = cifs_posix_lock_set(file, flock);
1493 if (!rc || rc < 0)
1494 return rc;
1495
106dc538 1496 if (type & server->vals->shared_lock_type)
08547b03
SF
1497 posix_lock_type = CIFS_RDLCK;
1498 else
1499 posix_lock_type = CIFS_WRLCK;
50c2f753 1500
03776f45 1501 if (unlock == 1)
beb84dc8 1502 posix_lock_type = CIFS_UNLCK;
7ee1af76 1503
f45d3416
PS
1504 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1505 current->tgid, flock->fl_start, length,
1506 NULL, posix_lock_type, wait_flag);
03776f45
PS
1507 goto out;
1508 }
7ee1af76 1509
03776f45 1510 if (lock) {
161ebf9f
PS
1511 struct cifsLockInfo *lock;
1512
fbd35aca 1513 lock = cifs_lock_init(flock->fl_start, length, type);
161ebf9f
PS
1514 if (!lock)
1515 return -ENOMEM;
1516
fbd35aca 1517 rc = cifs_lock_add_if(cfile, lock, wait_flag);
21cb2d90 1518 if (rc < 0) {
161ebf9f 1519 kfree(lock);
21cb2d90
PS
1520 return rc;
1521 }
1522 if (!rc)
85160e03
PS
1523 goto out;
1524
63b7d3a4
PS
1525 /*
1526 * Windows 7 server can delay breaking lease from read to None
1527 * if we set a byte-range lock on a file - break it explicitly
1528 * before sending the lock to the server to be sure the next
1529 * read won't conflict with non-overlapted locks due to
1530 * pagereading.
1531 */
18cceb6a
PS
1532 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1533 CIFS_CACHE_READ(CIFS_I(inode))) {
4f73c7d3 1534 cifs_zap_mapping(inode);
f96637be
JP
1535 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1536 inode);
18cceb6a 1537 CIFS_I(inode)->oplock = 0;
63b7d3a4
PS
1538 }
1539
d39a4f71
PS
1540 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1541 type, 1, 0, wait_flag);
161ebf9f
PS
1542 if (rc) {
1543 kfree(lock);
21cb2d90 1544 return rc;
03776f45 1545 }
161ebf9f 1546
fbd35aca 1547 cifs_lock_add(cfile, lock);
9ee305b7 1548 } else if (unlock)
d39a4f71 1549 rc = server->ops->mand_unlock_range(cfile, flock, xid);
03776f45 1550
03776f45
PS
1551out:
1552 if (flock->fl_flags & FL_POSIX)
9ebb389d 1553 posix_lock_file_wait(file, flock);
03776f45
PS
1554 return rc;
1555}
1556
1557int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1558{
1559 int rc, xid;
1560 int lock = 0, unlock = 0;
1561 bool wait_flag = false;
1562 bool posix_lck = false;
1563 struct cifs_sb_info *cifs_sb;
1564 struct cifs_tcon *tcon;
1565 struct cifsInodeInfo *cinode;
1566 struct cifsFileInfo *cfile;
1567 __u16 netfid;
04a6aa8a 1568 __u32 type;
03776f45
PS
1569
1570 rc = -EACCES;
6d5786a3 1571 xid = get_xid();
03776f45 1572
f96637be
JP
1573 cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
1574 cmd, flock->fl_flags, flock->fl_type,
1575 flock->fl_start, flock->fl_end);
03776f45 1576
03776f45
PS
1577 cfile = (struct cifsFileInfo *)file->private_data;
1578 tcon = tlink_tcon(cfile->tlink);
106dc538
PS
1579
1580 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1581 tcon->ses->server);
1582
7119e220 1583 cifs_sb = CIFS_FILE_SB(file);
4b4de76e 1584 netfid = cfile->fid.netfid;
496ad9aa 1585 cinode = CIFS_I(file_inode(file));
03776f45 1586
29e20f9c 1587 if (cap_unix(tcon->ses) &&
03776f45
PS
1588 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1589 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1590 posix_lck = true;
1591 /*
1592 * BB add code here to normalize offset and length to account for
1593 * negative length which we can not accept over the wire.
1594 */
1595 if (IS_GETLK(cmd)) {
4f6bcec9 1596 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
6d5786a3 1597 free_xid(xid);
03776f45
PS
1598 return rc;
1599 }
1600
1601 if (!lock && !unlock) {
1602 /*
1603 * if no lock or unlock then nothing to do since we do not
1604 * know what it is
1605 */
6d5786a3 1606 free_xid(xid);
03776f45 1607 return -EOPNOTSUPP;
7ee1af76
JA
1608 }
1609
03776f45
PS
1610 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1611 xid);
6d5786a3 1612 free_xid(xid);
1da177e4
LT
1613 return rc;
1614}
1615
597b027f
JL
1616/*
1617 * update the file size (if needed) after a write. Should be called with
1618 * the inode->i_lock held
1619 */
72432ffc 1620void
fbec9ab9
JL
1621cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1622 unsigned int bytes_written)
1623{
1624 loff_t end_of_write = offset + bytes_written;
1625
1626 if (end_of_write > cifsi->server_eof)
1627 cifsi->server_eof = end_of_write;
1628}
1629
ba9ad725
PS
1630static ssize_t
1631cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1632 size_t write_size, loff_t *offset)
1da177e4
LT
1633{
1634 int rc = 0;
1635 unsigned int bytes_written = 0;
1636 unsigned int total_written;
1637 struct cifs_sb_info *cifs_sb;
ba9ad725
PS
1638 struct cifs_tcon *tcon;
1639 struct TCP_Server_Info *server;
6d5786a3 1640 unsigned int xid;
7da4b49a
JL
1641 struct dentry *dentry = open_file->dentry;
1642 struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode);
fa2989f4 1643 struct cifs_io_parms io_parms;
1da177e4 1644
7da4b49a 1645 cifs_sb = CIFS_SB(dentry->d_sb);
1da177e4 1646
35c265e0
AV
1647 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
1648 write_size, *offset, dentry);
1da177e4 1649
ba9ad725
PS
1650 tcon = tlink_tcon(open_file->tlink);
1651 server = tcon->ses->server;
1652
1653 if (!server->ops->sync_write)
1654 return -ENOSYS;
50c2f753 1655
6d5786a3 1656 xid = get_xid();
1da177e4 1657
1da177e4
LT
1658 for (total_written = 0; write_size > total_written;
1659 total_written += bytes_written) {
1660 rc = -EAGAIN;
1661 while (rc == -EAGAIN) {
ca83ce3d
JL
1662 struct kvec iov[2];
1663 unsigned int len;
1664
1da177e4 1665 if (open_file->invalidHandle) {
1da177e4
LT
1666 /* we could deadlock if we called
1667 filemap_fdatawait from here so tell
fb8c4b14 1668 reopen_file not to flush data to
1da177e4 1669 server now */
15886177 1670 rc = cifs_reopen_file(open_file, false);
1da177e4
LT
1671 if (rc != 0)
1672 break;
1673 }
ca83ce3d 1674
cb7e9eab
PS
1675 len = min(server->ops->wp_retry_size(dentry->d_inode),
1676 (unsigned int)write_size - total_written);
ca83ce3d
JL
1677 /* iov[0] is reserved for smb header */
1678 iov[1].iov_base = (char *)write_data + total_written;
1679 iov[1].iov_len = len;
fa2989f4 1680 io_parms.pid = pid;
ba9ad725
PS
1681 io_parms.tcon = tcon;
1682 io_parms.offset = *offset;
fa2989f4 1683 io_parms.length = len;
db8b631d
SF
1684 rc = server->ops->sync_write(xid, &open_file->fid,
1685 &io_parms, &bytes_written, iov, 1);
1da177e4
LT
1686 }
1687 if (rc || (bytes_written == 0)) {
1688 if (total_written)
1689 break;
1690 else {
6d5786a3 1691 free_xid(xid);
1da177e4
LT
1692 return rc;
1693 }
fbec9ab9 1694 } else {
597b027f 1695 spin_lock(&dentry->d_inode->i_lock);
ba9ad725 1696 cifs_update_eof(cifsi, *offset, bytes_written);
597b027f 1697 spin_unlock(&dentry->d_inode->i_lock);
ba9ad725 1698 *offset += bytes_written;
fbec9ab9 1699 }
1da177e4
LT
1700 }
1701
ba9ad725 1702 cifs_stats_bytes_written(tcon, total_written);
1da177e4 1703
7da4b49a
JL
1704 if (total_written > 0) {
1705 spin_lock(&dentry->d_inode->i_lock);
ba9ad725
PS
1706 if (*offset > dentry->d_inode->i_size)
1707 i_size_write(dentry->d_inode, *offset);
7da4b49a 1708 spin_unlock(&dentry->d_inode->i_lock);
1da177e4 1709 }
7da4b49a 1710 mark_inode_dirty_sync(dentry->d_inode);
6d5786a3 1711 free_xid(xid);
1da177e4
LT
1712 return total_written;
1713}
1714
6508d904
JL
1715struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
1716 bool fsuid_only)
630f3f0c
SF
1717{
1718 struct cifsFileInfo *open_file = NULL;
6508d904
JL
1719 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1720
1721 /* only filter by fsuid on multiuser mounts */
1722 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1723 fsuid_only = false;
630f3f0c 1724
4477288a 1725 spin_lock(&cifs_file_list_lock);
630f3f0c
SF
1726 /* we could simply get the first_list_entry since write-only entries
1727 are always at the end of the list but since the first entry might
1728 have a close pending, we go through the whole list */
1729 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
fef59fd7 1730 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
6508d904 1731 continue;
2e396b83 1732 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
630f3f0c
SF
1733 if (!open_file->invalidHandle) {
1734 /* found a good file */
1735 /* lock it so it will not be closed on us */
764a1b1a 1736 cifsFileInfo_get_locked(open_file);
4477288a 1737 spin_unlock(&cifs_file_list_lock);
630f3f0c
SF
1738 return open_file;
1739 } /* else might as well continue, and look for
1740 another, or simply have the caller reopen it
1741 again rather than trying to fix this handle */
1742 } else /* write only file */
1743 break; /* write only files are last so must be done */
1744 }
4477288a 1745 spin_unlock(&cifs_file_list_lock);
630f3f0c
SF
1746 return NULL;
1747}
630f3f0c 1748
6508d904
JL
1749struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
1750 bool fsuid_only)
6148a742 1751{
2c0c2a08 1752 struct cifsFileInfo *open_file, *inv_file = NULL;
d3892294 1753 struct cifs_sb_info *cifs_sb;
2846d386 1754 bool any_available = false;
dd99cd80 1755 int rc;
2c0c2a08 1756 unsigned int refind = 0;
6148a742 1757
60808233
SF
1758 /* Having a null inode here (because mapping->host was set to zero by
1759 the VFS or MM) should not happen but we had reports of on oops (due to
1760 it being zero) during stress testcases so we need to check for it */
1761
fb8c4b14 1762 if (cifs_inode == NULL) {
f96637be 1763 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
60808233
SF
1764 dump_stack();
1765 return NULL;
1766 }
1767
d3892294
JL
1768 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1769
6508d904
JL
1770 /* only filter by fsuid on multiuser mounts */
1771 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1772 fsuid_only = false;
1773
4477288a 1774 spin_lock(&cifs_file_list_lock);
9b22b0b7 1775refind_writable:
2c0c2a08
SP
1776 if (refind > MAX_REOPEN_ATT) {
1777 spin_unlock(&cifs_file_list_lock);
1778 return NULL;
1779 }
6148a742 1780 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
6508d904
JL
1781 if (!any_available && open_file->pid != current->tgid)
1782 continue;
fef59fd7 1783 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
6148a742 1784 continue;
2e396b83 1785 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
9b22b0b7
SF
1786 if (!open_file->invalidHandle) {
1787 /* found a good writable file */
764a1b1a 1788 cifsFileInfo_get_locked(open_file);
4477288a 1789 spin_unlock(&cifs_file_list_lock);
9b22b0b7 1790 return open_file;
2c0c2a08
SP
1791 } else {
1792 if (!inv_file)
1793 inv_file = open_file;
9b22b0b7 1794 }
6148a742
SF
1795 }
1796 }
2846d386
JL
1797 /* couldn't find useable FH with same pid, try any available */
1798 if (!any_available) {
1799 any_available = true;
1800 goto refind_writable;
1801 }
2c0c2a08
SP
1802
1803 if (inv_file) {
1804 any_available = false;
764a1b1a 1805 cifsFileInfo_get_locked(inv_file);
2c0c2a08
SP
1806 }
1807
4477288a 1808 spin_unlock(&cifs_file_list_lock);
2c0c2a08
SP
1809
1810 if (inv_file) {
1811 rc = cifs_reopen_file(inv_file, false);
1812 if (!rc)
1813 return inv_file;
1814 else {
1815 spin_lock(&cifs_file_list_lock);
1816 list_move_tail(&inv_file->flist,
1817 &cifs_inode->openFileList);
1818 spin_unlock(&cifs_file_list_lock);
1819 cifsFileInfo_put(inv_file);
1820 spin_lock(&cifs_file_list_lock);
1821 ++refind;
1822 goto refind_writable;
1823 }
1824 }
1825
6148a742
SF
1826 return NULL;
1827}
1828
1da177e4
LT
1829static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
1830{
1831 struct address_space *mapping = page->mapping;
1832 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1833 char *write_data;
1834 int rc = -EFAULT;
1835 int bytes_written = 0;
1da177e4 1836 struct inode *inode;
6148a742 1837 struct cifsFileInfo *open_file;
1da177e4
LT
1838
1839 if (!mapping || !mapping->host)
1840 return -EFAULT;
1841
1842 inode = page->mapping->host;
1da177e4
LT
1843
1844 offset += (loff_t)from;
1845 write_data = kmap(page);
1846 write_data += from;
1847
1848 if ((to > PAGE_CACHE_SIZE) || (from > to)) {
1849 kunmap(page);
1850 return -EIO;
1851 }
1852
1853 /* racing with truncate? */
1854 if (offset > mapping->host->i_size) {
1855 kunmap(page);
1856 return 0; /* don't care */
1857 }
1858
1859 /* check to make sure that we are not extending the file */
1860 if (mapping->host->i_size - offset < (loff_t)to)
fb8c4b14 1861 to = (unsigned)(mapping->host->i_size - offset);
1da177e4 1862
6508d904 1863 open_file = find_writable_file(CIFS_I(mapping->host), false);
6148a742 1864 if (open_file) {
fa2989f4
PS
1865 bytes_written = cifs_write(open_file, open_file->pid,
1866 write_data, to - from, &offset);
6ab409b5 1867 cifsFileInfo_put(open_file);
1da177e4 1868 /* Does mm or vfs already set times? */
6148a742 1869 inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
bb5a9a04 1870 if ((bytes_written > 0) && (offset))
6148a742 1871 rc = 0;
bb5a9a04
SF
1872 else if (bytes_written < 0)
1873 rc = bytes_written;
6148a742 1874 } else {
f96637be 1875 cifs_dbg(FYI, "No writeable filehandles for inode\n");
1da177e4
LT
1876 rc = -EIO;
1877 }
1878
1879 kunmap(page);
1880 return rc;
1881}
1882
90ac1387
PS
1883static struct cifs_writedata *
1884wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
1885 pgoff_t end, pgoff_t *index,
1886 unsigned int *found_pages)
1887{
1888 unsigned int nr_pages;
1889 struct page **pages;
1890 struct cifs_writedata *wdata;
1891
1892 wdata = cifs_writedata_alloc((unsigned int)tofind,
1893 cifs_writev_complete);
1894 if (!wdata)
1895 return NULL;
1896
1897 /*
1898 * find_get_pages_tag seems to return a max of 256 on each
1899 * iteration, so we must call it several times in order to
1900 * fill the array or the wsize is effectively limited to
1901 * 256 * PAGE_CACHE_SIZE.
1902 */
1903 *found_pages = 0;
1904 pages = wdata->pages;
1905 do {
1906 nr_pages = find_get_pages_tag(mapping, index,
1907 PAGECACHE_TAG_DIRTY, tofind,
1908 pages);
1909 *found_pages += nr_pages;
1910 tofind -= nr_pages;
1911 pages += nr_pages;
1912 } while (nr_pages && tofind && *index <= end);
1913
1914 return wdata;
1915}
1916
7e48ff82
PS
1917static unsigned int
1918wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
1919 struct address_space *mapping,
1920 struct writeback_control *wbc,
1921 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
1922{
1923 unsigned int nr_pages = 0, i;
1924 struct page *page;
1925
1926 for (i = 0; i < found_pages; i++) {
1927 page = wdata->pages[i];
1928 /*
1929 * At this point we hold neither mapping->tree_lock nor
1930 * lock on the page itself: the page may be truncated or
1931 * invalidated (changing page->mapping to NULL), or even
1932 * swizzled back from swapper_space to tmpfs file
1933 * mapping
1934 */
1935
1936 if (nr_pages == 0)
1937 lock_page(page);
1938 else if (!trylock_page(page))
1939 break;
1940
1941 if (unlikely(page->mapping != mapping)) {
1942 unlock_page(page);
1943 break;
1944 }
1945
1946 if (!wbc->range_cyclic && page->index > end) {
1947 *done = true;
1948 unlock_page(page);
1949 break;
1950 }
1951
1952 if (*next && (page->index != *next)) {
1953 /* Not next consecutive page */
1954 unlock_page(page);
1955 break;
1956 }
1957
1958 if (wbc->sync_mode != WB_SYNC_NONE)
1959 wait_on_page_writeback(page);
1960
1961 if (PageWriteback(page) ||
1962 !clear_page_dirty_for_io(page)) {
1963 unlock_page(page);
1964 break;
1965 }
1966
1967 /*
1968 * This actually clears the dirty bit in the radix tree.
1969 * See cifs_writepage() for more commentary.
1970 */
1971 set_page_writeback(page);
1972 if (page_offset(page) >= i_size_read(mapping->host)) {
1973 *done = true;
1974 unlock_page(page);
1975 end_page_writeback(page);
1976 break;
1977 }
1978
1979 wdata->pages[i] = page;
1980 *next = page->index + 1;
1981 ++nr_pages;
1982 }
1983
1984 /* reset index to refind any pages skipped */
1985 if (nr_pages == 0)
1986 *index = wdata->pages[0]->index + 1;
1987
1988 /* put any pages we aren't going to use */
1989 for (i = nr_pages; i < found_pages; i++) {
1990 page_cache_release(wdata->pages[i]);
1991 wdata->pages[i] = NULL;
1992 }
1993
1994 return nr_pages;
1995}
1996
619aa48e
PS
1997static int
1998wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
1999 struct address_space *mapping, struct writeback_control *wbc)
2000{
2001 int rc = 0;
2002 struct TCP_Server_Info *server;
2003 unsigned int i;
2004
2005 wdata->sync_mode = wbc->sync_mode;
2006 wdata->nr_pages = nr_pages;
2007 wdata->offset = page_offset(wdata->pages[0]);
2008 wdata->pagesz = PAGE_CACHE_SIZE;
2009 wdata->tailsz = min(i_size_read(mapping->host) -
2010 page_offset(wdata->pages[nr_pages - 1]),
2011 (loff_t)PAGE_CACHE_SIZE);
2012 wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) + wdata->tailsz;
2013
66231a47
PS
2014 if (wdata->cfile != NULL)
2015 cifsFileInfo_put(wdata->cfile);
2016 wdata->cfile = find_writable_file(CIFS_I(mapping->host), false);
2017 if (!wdata->cfile) {
2018 cifs_dbg(VFS, "No writable handles for inode\n");
2019 rc = -EBADF;
2020 } else {
619aa48e
PS
2021 wdata->pid = wdata->cfile->pid;
2022 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2023 rc = server->ops->async_writev(wdata, cifs_writedata_release);
66231a47 2024 }
619aa48e
PS
2025
2026 for (i = 0; i < nr_pages; ++i)
2027 unlock_page(wdata->pages[i]);
2028
2029 return rc;
2030}
2031
1da177e4 2032static int cifs_writepages(struct address_space *mapping,
37c0eb46 2033 struct writeback_control *wbc)
1da177e4 2034{
c3d17b63 2035 struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
cb7e9eab 2036 struct TCP_Server_Info *server;
c3d17b63
JL
2037 bool done = false, scanned = false, range_whole = false;
2038 pgoff_t end, index;
2039 struct cifs_writedata *wdata;
37c0eb46 2040 int rc = 0;
50c2f753 2041
37c0eb46 2042 /*
c3d17b63 2043 * If wsize is smaller than the page cache size, default to writing
37c0eb46
SF
2044 * one page at a time via cifs_writepage
2045 */
2046 if (cifs_sb->wsize < PAGE_CACHE_SIZE)
2047 return generic_writepages(mapping, wbc);
2048
111ebb6e 2049 if (wbc->range_cyclic) {
37c0eb46 2050 index = mapping->writeback_index; /* Start from prev offset */
111ebb6e
OH
2051 end = -1;
2052 } else {
2053 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2054 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2055 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
c3d17b63
JL
2056 range_whole = true;
2057 scanned = true;
37c0eb46 2058 }
cb7e9eab 2059 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
37c0eb46 2060retry:
c3d17b63 2061 while (!done && index <= end) {
cb7e9eab 2062 unsigned int i, nr_pages, found_pages, wsize, credits;
66231a47 2063 pgoff_t next = 0, tofind, saved_index = index;
c3d17b63 2064
cb7e9eab
PS
2065 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2066 &wsize, &credits);
2067 if (rc)
2068 break;
c3d17b63 2069
cb7e9eab 2070 tofind = min((wsize / PAGE_CACHE_SIZE) - 1, end - index) + 1;
c3d17b63 2071
90ac1387
PS
2072 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2073 &found_pages);
c3d17b63
JL
2074 if (!wdata) {
2075 rc = -ENOMEM;
cb7e9eab 2076 add_credits_and_wake_if(server, credits, 0);
c3d17b63
JL
2077 break;
2078 }
2079
c3d17b63
JL
2080 if (found_pages == 0) {
2081 kref_put(&wdata->refcount, cifs_writedata_release);
cb7e9eab 2082 add_credits_and_wake_if(server, credits, 0);
c3d17b63
JL
2083 break;
2084 }
2085
7e48ff82
PS
2086 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2087 end, &index, &next, &done);
37c0eb46 2088
c3d17b63
JL
2089 /* nothing to write? */
2090 if (nr_pages == 0) {
2091 kref_put(&wdata->refcount, cifs_writedata_release);
cb7e9eab 2092 add_credits_and_wake_if(server, credits, 0);
c3d17b63 2093 continue;
37c0eb46 2094 }
fbec9ab9 2095
cb7e9eab 2096 wdata->credits = credits;
941b853d 2097
619aa48e 2098 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
f3983c21 2099
c3d17b63
JL
2100 /* send failure -- clean up the mess */
2101 if (rc != 0) {
cb7e9eab 2102 add_credits_and_wake_if(server, wdata->credits, 0);
c3d17b63 2103 for (i = 0; i < nr_pages; ++i) {
941b853d 2104 if (rc == -EAGAIN)
c3d17b63
JL
2105 redirty_page_for_writepage(wbc,
2106 wdata->pages[i]);
2107 else
2108 SetPageError(wdata->pages[i]);
2109 end_page_writeback(wdata->pages[i]);
2110 page_cache_release(wdata->pages[i]);
37c0eb46 2111 }
941b853d
JL
2112 if (rc != -EAGAIN)
2113 mapping_set_error(mapping, rc);
c3d17b63
JL
2114 }
2115 kref_put(&wdata->refcount, cifs_writedata_release);
941b853d 2116
66231a47
PS
2117 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2118 index = saved_index;
2119 continue;
2120 }
2121
c3d17b63
JL
2122 wbc->nr_to_write -= nr_pages;
2123 if (wbc->nr_to_write <= 0)
2124 done = true;
b066a48c 2125
c3d17b63 2126 index = next;
37c0eb46 2127 }
c3d17b63 2128
37c0eb46
SF
2129 if (!scanned && !done) {
2130 /*
2131 * We hit the last page and there is more work to be done: wrap
2132 * back to the start of the file
2133 */
c3d17b63 2134 scanned = true;
37c0eb46
SF
2135 index = 0;
2136 goto retry;
2137 }
c3d17b63 2138
111ebb6e 2139 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
37c0eb46
SF
2140 mapping->writeback_index = index;
2141
1da177e4
LT
2142 return rc;
2143}
1da177e4 2144
9ad1506b
PS
2145static int
2146cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
1da177e4 2147{
9ad1506b 2148 int rc;
6d5786a3 2149 unsigned int xid;
1da177e4 2150
6d5786a3 2151 xid = get_xid();
1da177e4
LT
2152/* BB add check for wbc flags */
2153 page_cache_get(page);
ad7a2926 2154 if (!PageUptodate(page))
f96637be 2155 cifs_dbg(FYI, "ppw - page not up to date\n");
cb876f45
LT
2156
2157 /*
2158 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2159 *
2160 * A writepage() implementation always needs to do either this,
2161 * or re-dirty the page with "redirty_page_for_writepage()" in
2162 * the case of a failure.
2163 *
2164 * Just unlocking the page will cause the radix tree tag-bits
2165 * to fail to update with the state of the page correctly.
2166 */
fb8c4b14 2167 set_page_writeback(page);
9ad1506b 2168retry_write:
1da177e4 2169 rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
9ad1506b
PS
2170 if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL)
2171 goto retry_write;
2172 else if (rc == -EAGAIN)
2173 redirty_page_for_writepage(wbc, page);
2174 else if (rc != 0)
2175 SetPageError(page);
2176 else
2177 SetPageUptodate(page);
cb876f45
LT
2178 end_page_writeback(page);
2179 page_cache_release(page);
6d5786a3 2180 free_xid(xid);
1da177e4
LT
2181 return rc;
2182}
2183
9ad1506b
PS
2184static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2185{
2186 int rc = cifs_writepage_locked(page, wbc);
2187 unlock_page(page);
2188 return rc;
2189}
2190
d9414774
NP
2191static int cifs_write_end(struct file *file, struct address_space *mapping,
2192 loff_t pos, unsigned len, unsigned copied,
2193 struct page *page, void *fsdata)
1da177e4 2194{
d9414774
NP
2195 int rc;
2196 struct inode *inode = mapping->host;
d4ffff1f
PS
2197 struct cifsFileInfo *cfile = file->private_data;
2198 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2199 __u32 pid;
2200
2201 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2202 pid = cfile->pid;
2203 else
2204 pid = current->tgid;
1da177e4 2205
f96637be 2206 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
b6b38f70 2207 page, pos, copied);
d9414774 2208
a98ee8c1
JL
2209 if (PageChecked(page)) {
2210 if (copied == len)
2211 SetPageUptodate(page);
2212 ClearPageChecked(page);
2213 } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
d9414774 2214 SetPageUptodate(page);
ad7a2926 2215
1da177e4 2216 if (!PageUptodate(page)) {
d9414774
NP
2217 char *page_data;
2218 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
6d5786a3 2219 unsigned int xid;
d9414774 2220
6d5786a3 2221 xid = get_xid();
1da177e4
LT
2222 /* this is probably better than directly calling
2223 partialpage_write since in this function the file handle is
2224 known which we might as well leverage */
2225 /* BB check if anything else missing out of ppw
2226 such as updating last write time */
2227 page_data = kmap(page);
d4ffff1f 2228 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
d9414774 2229 /* if (rc < 0) should we set writebehind rc? */
1da177e4 2230 kunmap(page);
d9414774 2231
6d5786a3 2232 free_xid(xid);
fb8c4b14 2233 } else {
d9414774
NP
2234 rc = copied;
2235 pos += copied;
ca8aa29c 2236 set_page_dirty(page);
1da177e4
LT
2237 }
2238
d9414774
NP
2239 if (rc > 0) {
2240 spin_lock(&inode->i_lock);
2241 if (pos > inode->i_size)
2242 i_size_write(inode, pos);
2243 spin_unlock(&inode->i_lock);
2244 }
2245
2246 unlock_page(page);
2247 page_cache_release(page);
2248
1da177e4
LT
2249 return rc;
2250}
2251
02c24a82
JB
2252int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2253 int datasync)
1da177e4 2254{
6d5786a3 2255 unsigned int xid;
1da177e4 2256 int rc = 0;
96daf2b0 2257 struct cifs_tcon *tcon;
1d8c4c00 2258 struct TCP_Server_Info *server;
c21dfb69 2259 struct cifsFileInfo *smbfile = file->private_data;
496ad9aa 2260 struct inode *inode = file_inode(file);
8be7e6ba 2261 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1da177e4 2262
02c24a82
JB
2263 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2264 if (rc)
2265 return rc;
2266 mutex_lock(&inode->i_mutex);
2267
6d5786a3 2268 xid = get_xid();
1da177e4 2269
35c265e0
AV
2270 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2271 file, datasync);
50c2f753 2272
18cceb6a 2273 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
4f73c7d3 2274 rc = cifs_zap_mapping(inode);
6feb9891 2275 if (rc) {
f96637be 2276 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
6feb9891
PS
2277 rc = 0; /* don't care about it in fsync */
2278 }
2279 }
eb4b756b 2280
8be7e6ba 2281 tcon = tlink_tcon(smbfile->tlink);
1d8c4c00
PS
2282 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2283 server = tcon->ses->server;
2284 if (server->ops->flush)
2285 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2286 else
2287 rc = -ENOSYS;
2288 }
8be7e6ba 2289
6d5786a3 2290 free_xid(xid);
02c24a82 2291 mutex_unlock(&inode->i_mutex);
8be7e6ba
PS
2292 return rc;
2293}
2294
02c24a82 2295int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
8be7e6ba 2296{
6d5786a3 2297 unsigned int xid;
8be7e6ba 2298 int rc = 0;
96daf2b0 2299 struct cifs_tcon *tcon;
1d8c4c00 2300 struct TCP_Server_Info *server;
8be7e6ba 2301 struct cifsFileInfo *smbfile = file->private_data;
7119e220 2302 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
02c24a82
JB
2303 struct inode *inode = file->f_mapping->host;
2304
2305 rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
2306 if (rc)
2307 return rc;
2308 mutex_lock(&inode->i_mutex);
8be7e6ba 2309
6d5786a3 2310 xid = get_xid();
8be7e6ba 2311
35c265e0
AV
2312 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2313 file, datasync);
8be7e6ba
PS
2314
2315 tcon = tlink_tcon(smbfile->tlink);
1d8c4c00
PS
2316 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2317 server = tcon->ses->server;
2318 if (server->ops->flush)
2319 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2320 else
2321 rc = -ENOSYS;
2322 }
b298f223 2323
6d5786a3 2324 free_xid(xid);
02c24a82 2325 mutex_unlock(&inode->i_mutex);
1da177e4
LT
2326 return rc;
2327}
2328
1da177e4
LT
2329/*
2330 * As file closes, flush all cached write data for this inode checking
2331 * for write behind errors.
2332 */
75e1fcc0 2333int cifs_flush(struct file *file, fl_owner_t id)
1da177e4 2334{
496ad9aa 2335 struct inode *inode = file_inode(file);
1da177e4
LT
2336 int rc = 0;
2337
eb4b756b 2338 if (file->f_mode & FMODE_WRITE)
d3f1322a 2339 rc = filemap_write_and_wait(inode->i_mapping);
50c2f753 2340
f96637be 2341 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
1da177e4
LT
2342
2343 return rc;
2344}
2345
72432ffc
PS
2346static int
2347cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
2348{
2349 int rc = 0;
2350 unsigned long i;
2351
2352 for (i = 0; i < num_pages; i++) {
e94f7ba1 2353 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
72432ffc
PS
2354 if (!pages[i]) {
2355 /*
2356 * save number of pages we have already allocated and
2357 * return with ENOMEM error
2358 */
2359 num_pages = i;
2360 rc = -ENOMEM;
e94f7ba1 2361 break;
72432ffc
PS
2362 }
2363 }
2364
e94f7ba1
JL
2365 if (rc) {
2366 for (i = 0; i < num_pages; i++)
2367 put_page(pages[i]);
2368 }
72432ffc
PS
2369 return rc;
2370}
2371
2372static inline
2373size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
2374{
2375 size_t num_pages;
2376 size_t clen;
2377
2378 clen = min_t(const size_t, len, wsize);
a7103b99 2379 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
72432ffc
PS
2380
2381 if (cur_len)
2382 *cur_len = clen;
2383
2384 return num_pages;
2385}
2386
da82f7e7 2387static void
4a5c80d7 2388cifs_uncached_writedata_release(struct kref *refcount)
da82f7e7
JL
2389{
2390 int i;
4a5c80d7
SF
2391 struct cifs_writedata *wdata = container_of(refcount,
2392 struct cifs_writedata, refcount);
2393
2394 for (i = 0; i < wdata->nr_pages; i++)
2395 put_page(wdata->pages[i]);
2396 cifs_writedata_release(refcount);
2397}
2398
2399static void
2400cifs_uncached_writev_complete(struct work_struct *work)
2401{
da82f7e7
JL
2402 struct cifs_writedata *wdata = container_of(work,
2403 struct cifs_writedata, work);
2404 struct inode *inode = wdata->cfile->dentry->d_inode;
2405 struct cifsInodeInfo *cifsi = CIFS_I(inode);
2406
2407 spin_lock(&inode->i_lock);
2408 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
2409 if (cifsi->server_eof > inode->i_size)
2410 i_size_write(inode, cifsi->server_eof);
2411 spin_unlock(&inode->i_lock);
2412
2413 complete(&wdata->done);
2414
4a5c80d7 2415 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
da82f7e7
JL
2416}
2417
da82f7e7 2418static int
66386c08
PS
2419wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
2420 size_t *len, unsigned long *num_pages)
da82f7e7 2421{
66386c08
PS
2422 size_t save_len, copied, bytes, cur_len = *len;
2423 unsigned long i, nr_pages = *num_pages;
c9de5c80 2424
66386c08
PS
2425 save_len = cur_len;
2426 for (i = 0; i < nr_pages; i++) {
2427 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
2428 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
2429 cur_len -= copied;
2430 /*
2431 * If we didn't copy as much as we expected, then that
2432 * may mean we trod into an unmapped area. Stop copying
2433 * at that point. On the next pass through the big
2434 * loop, we'll likely end up getting a zero-length
2435 * write and bailing out of it.
2436 */
2437 if (copied < bytes)
2438 break;
2439 }
2440 cur_len = save_len - cur_len;
2441 *len = cur_len;
da82f7e7 2442
66386c08
PS
2443 /*
2444 * If we have no data to send, then that probably means that
2445 * the copy above failed altogether. That's most likely because
2446 * the address in the iovec was bogus. Return -EFAULT and let
2447 * the caller free anything we allocated and bail out.
2448 */
2449 if (!cur_len)
2450 return -EFAULT;
da82f7e7 2451
66386c08
PS
2452 /*
2453 * i + 1 now represents the number of pages we actually used in
2454 * the copy phase above.
2455 */
2456 *num_pages = i + 1;
2457 return 0;
da82f7e7
JL
2458}
2459
43de94ea
PS
2460static int
2461cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
2462 struct cifsFileInfo *open_file,
2463 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list)
72432ffc 2464{
43de94ea
PS
2465 int rc = 0;
2466 size_t cur_len;
66386c08 2467 unsigned long nr_pages, num_pages, i;
43de94ea 2468 struct cifs_writedata *wdata;
6ec0b01b
PS
2469 struct iov_iter saved_from;
2470 loff_t saved_offset = offset;
da82f7e7 2471 pid_t pid;
6ec0b01b 2472 struct TCP_Server_Info *server;
d4ffff1f
PS
2473
2474 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2475 pid = open_file->pid;
2476 else
2477 pid = current->tgid;
2478
6ec0b01b
PS
2479 server = tlink_tcon(open_file->tlink)->ses->server;
2480 memcpy(&saved_from, from, sizeof(struct iov_iter));
2481
72432ffc 2482 do {
cb7e9eab
PS
2483 unsigned int wsize, credits;
2484
2485 rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
2486 &wsize, &credits);
2487 if (rc)
2488 break;
da82f7e7 2489
cb7e9eab 2490 nr_pages = get_numpages(wsize, len, &cur_len);
da82f7e7
JL
2491 wdata = cifs_writedata_alloc(nr_pages,
2492 cifs_uncached_writev_complete);
2493 if (!wdata) {
2494 rc = -ENOMEM;
cb7e9eab 2495 add_credits_and_wake_if(server, credits, 0);
da82f7e7
JL
2496 break;
2497 }
2498
2499 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
2500 if (rc) {
2501 kfree(wdata);
cb7e9eab 2502 add_credits_and_wake_if(server, credits, 0);
da82f7e7
JL
2503 break;
2504 }
2505
66386c08
PS
2506 num_pages = nr_pages;
2507 rc = wdata_fill_from_iovec(wdata, from, &cur_len, &num_pages);
2508 if (rc) {
5d81de8e
JL
2509 for (i = 0; i < nr_pages; i++)
2510 put_page(wdata->pages[i]);
2511 kfree(wdata);
cb7e9eab 2512 add_credits_and_wake_if(server, credits, 0);
5d81de8e
JL
2513 break;
2514 }
2515
2516 /*
66386c08
PS
2517 * Bring nr_pages down to the number of pages we actually used,
2518 * and free any pages that we didn't use.
5d81de8e 2519 */
66386c08 2520 for ( ; nr_pages > num_pages; nr_pages--)
5d81de8e
JL
2521 put_page(wdata->pages[nr_pages - 1]);
2522
da82f7e7
JL
2523 wdata->sync_mode = WB_SYNC_ALL;
2524 wdata->nr_pages = nr_pages;
2525 wdata->offset = (__u64)offset;
2526 wdata->cfile = cifsFileInfo_get(open_file);
2527 wdata->pid = pid;
2528 wdata->bytes = cur_len;
eddb079d
JL
2529 wdata->pagesz = PAGE_SIZE;
2530 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
cb7e9eab 2531 wdata->credits = credits;
6ec0b01b
PS
2532
2533 if (!wdata->cfile->invalidHandle ||
2534 !cifs_reopen_file(wdata->cfile, false))
2535 rc = server->ops->async_writev(wdata,
2536 cifs_uncached_writedata_release);
da82f7e7 2537 if (rc) {
cb7e9eab 2538 add_credits_and_wake_if(server, wdata->credits, 0);
4a5c80d7
SF
2539 kref_put(&wdata->refcount,
2540 cifs_uncached_writedata_release);
6ec0b01b
PS
2541 if (rc == -EAGAIN) {
2542 memcpy(from, &saved_from,
2543 sizeof(struct iov_iter));
2544 iov_iter_advance(from, offset - saved_offset);
2545 continue;
2546 }
72432ffc
PS
2547 break;
2548 }
2549
43de94ea 2550 list_add_tail(&wdata->list, wdata_list);
da82f7e7
JL
2551 offset += cur_len;
2552 len -= cur_len;
72432ffc
PS
2553 } while (len > 0);
2554
43de94ea
PS
2555 return rc;
2556}
2557
2558static ssize_t
2559cifs_iovec_write(struct file *file, struct iov_iter *from, loff_t *poffset)
2560{
2561 size_t len;
2562 ssize_t total_written = 0;
2563 struct cifsFileInfo *open_file;
2564 struct cifs_tcon *tcon;
2565 struct cifs_sb_info *cifs_sb;
2566 struct cifs_writedata *wdata, *tmp;
2567 struct list_head wdata_list;
6ec0b01b 2568 struct iov_iter saved_from;
43de94ea
PS
2569 int rc;
2570
2571 len = iov_iter_count(from);
2572 rc = generic_write_checks(file, poffset, &len, 0);
2573 if (rc)
2574 return rc;
2575
2576 if (!len)
2577 return 0;
2578
2579 iov_iter_truncate(from, len);
2580
2581 INIT_LIST_HEAD(&wdata_list);
7119e220 2582 cifs_sb = CIFS_FILE_SB(file);
43de94ea
PS
2583 open_file = file->private_data;
2584 tcon = tlink_tcon(open_file->tlink);
2585
2586 if (!tcon->ses->server->ops->async_writev)
2587 return -ENOSYS;
2588
6ec0b01b
PS
2589 memcpy(&saved_from, from, sizeof(struct iov_iter));
2590
43de94ea
PS
2591 rc = cifs_write_from_iter(*poffset, len, from, open_file, cifs_sb,
2592 &wdata_list);
2593
da82f7e7
JL
2594 /*
2595 * If at least one write was successfully sent, then discard any rc
2596 * value from the later writes. If the other write succeeds, then
2597 * we'll end up returning whatever was written. If it fails, then
2598 * we'll get a new rc value from that.
2599 */
2600 if (!list_empty(&wdata_list))
2601 rc = 0;
2602
2603 /*
2604 * Wait for and collect replies for any successful sends in order of
2605 * increasing offset. Once an error is hit or we get a fatal signal
2606 * while waiting, then return without waiting for any more replies.
2607 */
2608restart_loop:
2609 list_for_each_entry_safe(wdata, tmp, &wdata_list, list) {
2610 if (!rc) {
2611 /* FIXME: freezable too? */
2612 rc = wait_for_completion_killable(&wdata->done);
2613 if (rc)
2614 rc = -EINTR;
2615 else if (wdata->result)
2616 rc = wdata->result;
2617 else
2618 total_written += wdata->bytes;
2619
2620 /* resend call if it's a retryable error */
2621 if (rc == -EAGAIN) {
6ec0b01b
PS
2622 struct list_head tmp_list;
2623 struct iov_iter tmp_from;
2624
2625 INIT_LIST_HEAD(&tmp_list);
2626 list_del_init(&wdata->list);
2627
2628 memcpy(&tmp_from, &saved_from,
2629 sizeof(struct iov_iter));
2630 iov_iter_advance(&tmp_from,
2631 wdata->offset - *poffset);
2632
2633 rc = cifs_write_from_iter(wdata->offset,
2634 wdata->bytes, &tmp_from,
2635 open_file, cifs_sb, &tmp_list);
2636
2637 list_splice(&tmp_list, &wdata_list);
2638
2639 kref_put(&wdata->refcount,
2640 cifs_uncached_writedata_release);
da82f7e7
JL
2641 goto restart_loop;
2642 }
2643 }
2644 list_del_init(&wdata->list);
4a5c80d7 2645 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
72432ffc
PS
2646 }
2647
da82f7e7
JL
2648 if (total_written > 0)
2649 *poffset += total_written;
72432ffc 2650
da82f7e7
JL
2651 cifs_stats_bytes_written(tcon, total_written);
2652 return total_written ? total_written : (ssize_t)rc;
72432ffc
PS
2653}
2654
3dae8750 2655ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
72432ffc
PS
2656{
2657 ssize_t written;
2658 struct inode *inode;
3dae8750 2659 loff_t pos = iocb->ki_pos;
72432ffc 2660
496ad9aa 2661 inode = file_inode(iocb->ki_filp);
72432ffc
PS
2662
2663 /*
2664 * BB - optimize the way when signing is disabled. We can drop this
2665 * extra memory-to-memory copying and use iovec buffers for constructing
2666 * write request.
2667 */
2668
3dae8750 2669 written = cifs_iovec_write(iocb->ki_filp, from, &pos);
72432ffc 2670 if (written > 0) {
aff8d5ca 2671 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
72432ffc
PS
2672 iocb->ki_pos = pos;
2673 }
2674
2675 return written;
2676}
2677
579f9053 2678static ssize_t
3dae8750 2679cifs_writev(struct kiocb *iocb, struct iov_iter *from)
72432ffc 2680{
579f9053
PS
2681 struct file *file = iocb->ki_filp;
2682 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
2683 struct inode *inode = file->f_mapping->host;
2684 struct cifsInodeInfo *cinode = CIFS_I(inode);
2685 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
2686 ssize_t rc = -EACCES;
19dfc1f5 2687 loff_t lock_pos = iocb->ki_pos;
72432ffc 2688
579f9053
PS
2689 /*
2690 * We need to hold the sem to be sure nobody modifies lock list
2691 * with a brlock that prevents writing.
2692 */
2693 down_read(&cinode->lock_sem);
19dfc1f5
AV
2694 mutex_lock(&inode->i_mutex);
2695 if (file->f_flags & O_APPEND)
2696 lock_pos = i_size_read(inode);
3dae8750 2697 if (!cifs_find_lock_conflict(cfile, lock_pos, iov_iter_count(from),
579f9053 2698 server->vals->exclusive_lock_type, NULL,
19dfc1f5 2699 CIFS_WRITE_OP)) {
3dae8750 2700 rc = __generic_file_write_iter(iocb, from);
19dfc1f5
AV
2701 mutex_unlock(&inode->i_mutex);
2702
2703 if (rc > 0) {
2704 ssize_t err;
2705
2706 err = generic_write_sync(file, iocb->ki_pos - rc, rc);
1f80c0cc 2707 if (err < 0)
19dfc1f5
AV
2708 rc = err;
2709 }
2710 } else {
2711 mutex_unlock(&inode->i_mutex);
2712 }
579f9053 2713 up_read(&cinode->lock_sem);
579f9053
PS
2714 return rc;
2715}
2716
2717ssize_t
3dae8750 2718cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
579f9053 2719{
496ad9aa 2720 struct inode *inode = file_inode(iocb->ki_filp);
579f9053
PS
2721 struct cifsInodeInfo *cinode = CIFS_I(inode);
2722 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2723 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
2724 iocb->ki_filp->private_data;
2725 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
88cf75aa 2726 ssize_t written;
ca8aa29c 2727
c11f1df5
SP
2728 written = cifs_get_writer(cinode);
2729 if (written)
2730 return written;
2731
18cceb6a 2732 if (CIFS_CACHE_WRITE(cinode)) {
88cf75aa
PS
2733 if (cap_unix(tcon->ses) &&
2734 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
c11f1df5 2735 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3dae8750 2736 written = generic_file_write_iter(iocb, from);
c11f1df5
SP
2737 goto out;
2738 }
3dae8750 2739 written = cifs_writev(iocb, from);
c11f1df5 2740 goto out;
25078105 2741 }
25078105 2742 /*
ca8aa29c
PS
2743 * For non-oplocked files in strict cache mode we need to write the data
2744 * to the server exactly from the pos to pos+len-1 rather than flush all
2745 * affected pages because it may cause a error with mandatory locks on
2746 * these pages but not on the region from pos to ppos+len-1.
72432ffc 2747 */
3dae8750 2748 written = cifs_user_writev(iocb, from);
18cceb6a 2749 if (written > 0 && CIFS_CACHE_READ(cinode)) {
88cf75aa
PS
2750 /*
2751 * Windows 7 server can delay breaking level2 oplock if a write
2752 * request comes - break it on the client to prevent reading
2753 * an old data.
2754 */
4f73c7d3 2755 cifs_zap_mapping(inode);
f96637be
JP
2756 cifs_dbg(FYI, "Set no oplock for inode=%p after a write operation\n",
2757 inode);
18cceb6a 2758 cinode->oplock = 0;
88cf75aa 2759 }
c11f1df5
SP
2760out:
2761 cifs_put_writer(cinode);
88cf75aa 2762 return written;
72432ffc
PS
2763}
2764
0471ca3f 2765static struct cifs_readdata *
f4e49cd2 2766cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
0471ca3f
JL
2767{
2768 struct cifs_readdata *rdata;
f4e49cd2 2769
c5fab6f4
JL
2770 rdata = kzalloc(sizeof(*rdata) + (sizeof(struct page *) * nr_pages),
2771 GFP_KERNEL);
0471ca3f 2772 if (rdata != NULL) {
6993f74a 2773 kref_init(&rdata->refcount);
1c892549
JL
2774 INIT_LIST_HEAD(&rdata->list);
2775 init_completion(&rdata->done);
0471ca3f 2776 INIT_WORK(&rdata->work, complete);
0471ca3f 2777 }
f4e49cd2 2778
0471ca3f
JL
2779 return rdata;
2780}
2781
6993f74a
JL
2782void
2783cifs_readdata_release(struct kref *refcount)
0471ca3f 2784{
6993f74a
JL
2785 struct cifs_readdata *rdata = container_of(refcount,
2786 struct cifs_readdata, refcount);
2787
2788 if (rdata->cfile)
2789 cifsFileInfo_put(rdata->cfile);
2790
0471ca3f
JL
2791 kfree(rdata);
2792}
2793
1c892549 2794static int
c5fab6f4 2795cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
1c892549
JL
2796{
2797 int rc = 0;
c5fab6f4 2798 struct page *page;
1c892549
JL
2799 unsigned int i;
2800
c5fab6f4 2801 for (i = 0; i < nr_pages; i++) {
1c892549
JL
2802 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2803 if (!page) {
2804 rc = -ENOMEM;
2805 break;
2806 }
c5fab6f4 2807 rdata->pages[i] = page;
1c892549
JL
2808 }
2809
2810 if (rc) {
c5fab6f4
JL
2811 for (i = 0; i < nr_pages; i++) {
2812 put_page(rdata->pages[i]);
2813 rdata->pages[i] = NULL;
1c892549
JL
2814 }
2815 }
2816 return rc;
2817}
2818
2819static void
2820cifs_uncached_readdata_release(struct kref *refcount)
2821{
1c892549
JL
2822 struct cifs_readdata *rdata = container_of(refcount,
2823 struct cifs_readdata, refcount);
c5fab6f4 2824 unsigned int i;
1c892549 2825
c5fab6f4
JL
2826 for (i = 0; i < rdata->nr_pages; i++) {
2827 put_page(rdata->pages[i]);
2828 rdata->pages[i] = NULL;
1c892549
JL
2829 }
2830 cifs_readdata_release(refcount);
2831}
2832
1c892549
JL
2833/**
2834 * cifs_readdata_to_iov - copy data from pages in response to an iovec
2835 * @rdata: the readdata response with list of pages holding data
7f25bba8 2836 * @iter: destination for our data
1c892549
JL
2837 *
2838 * This function copies data from a list of pages in a readdata response into
2839 * an array of iovecs. It will first calculate where the data should go
2840 * based on the info in the readdata and then copy the data into that spot.
2841 */
7f25bba8
AV
2842static int
2843cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
1c892549 2844{
34a54d61 2845 size_t remaining = rdata->got_bytes;
c5fab6f4 2846 unsigned int i;
1c892549 2847
c5fab6f4 2848 for (i = 0; i < rdata->nr_pages; i++) {
c5fab6f4 2849 struct page *page = rdata->pages[i];
e686bd8d 2850 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
7f25bba8
AV
2851 size_t written = copy_page_to_iter(page, 0, copy, iter);
2852 remaining -= written;
2853 if (written < copy && iov_iter_count(iter) > 0)
2854 break;
1c892549 2855 }
7f25bba8 2856 return remaining ? -EFAULT : 0;
1c892549
JL
2857}
2858
2859static void
2860cifs_uncached_readv_complete(struct work_struct *work)
2861{
2862 struct cifs_readdata *rdata = container_of(work,
2863 struct cifs_readdata, work);
1c892549
JL
2864
2865 complete(&rdata->done);
2866 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
2867}
2868
2869static int
8321fec4
JL
2870cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
2871 struct cifs_readdata *rdata, unsigned int len)
1c892549 2872{
b3160aeb 2873 int result = 0;
c5fab6f4
JL
2874 unsigned int i;
2875 unsigned int nr_pages = rdata->nr_pages;
8321fec4 2876 struct kvec iov;
1c892549 2877
b3160aeb 2878 rdata->got_bytes = 0;
8321fec4 2879 rdata->tailsz = PAGE_SIZE;
c5fab6f4
JL
2880 for (i = 0; i < nr_pages; i++) {
2881 struct page *page = rdata->pages[i];
2882
8321fec4 2883 if (len >= PAGE_SIZE) {
1c892549 2884 /* enough data to fill the page */
8321fec4
JL
2885 iov.iov_base = kmap(page);
2886 iov.iov_len = PAGE_SIZE;
f96637be
JP
2887 cifs_dbg(FYI, "%u: iov_base=%p iov_len=%zu\n",
2888 i, iov.iov_base, iov.iov_len);
8321fec4
JL
2889 len -= PAGE_SIZE;
2890 } else if (len > 0) {
1c892549 2891 /* enough for partial page, fill and zero the rest */
8321fec4
JL
2892 iov.iov_base = kmap(page);
2893 iov.iov_len = len;
f96637be
JP
2894 cifs_dbg(FYI, "%u: iov_base=%p iov_len=%zu\n",
2895 i, iov.iov_base, iov.iov_len);
8321fec4
JL
2896 memset(iov.iov_base + len, '\0', PAGE_SIZE - len);
2897 rdata->tailsz = len;
2898 len = 0;
1c892549
JL
2899 } else {
2900 /* no need to hold page hostage */
c5fab6f4
JL
2901 rdata->pages[i] = NULL;
2902 rdata->nr_pages--;
1c892549 2903 put_page(page);
8321fec4 2904 continue;
1c892549 2905 }
8321fec4
JL
2906
2907 result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
2908 kunmap(page);
2909 if (result < 0)
2910 break;
2911
b3160aeb 2912 rdata->got_bytes += result;
1c892549
JL
2913 }
2914
b3160aeb
PS
2915 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
2916 rdata->got_bytes : result;
1c892549
JL
2917}
2918
0ada36b2
PS
2919static int
2920cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
2921 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list)
1da177e4 2922{
0ada36b2 2923 struct cifs_readdata *rdata;
bed9da02 2924 unsigned int npages, rsize, credits;
0ada36b2
PS
2925 size_t cur_len;
2926 int rc;
1c892549 2927 pid_t pid;
25f40259 2928 struct TCP_Server_Info *server;
a70307ee 2929
25f40259 2930 server = tlink_tcon(open_file->tlink)->ses->server;
fc9c5966 2931
d4ffff1f
PS
2932 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2933 pid = open_file->pid;
2934 else
2935 pid = current->tgid;
2936
1c892549 2937 do {
bed9da02
PS
2938 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
2939 &rsize, &credits);
2940 if (rc)
2941 break;
2942
2943 cur_len = min_t(const size_t, len, rsize);
1c892549 2944 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
a70307ee 2945
1c892549
JL
2946 /* allocate a readdata struct */
2947 rdata = cifs_readdata_alloc(npages,
2948 cifs_uncached_readv_complete);
2949 if (!rdata) {
bed9da02 2950 add_credits_and_wake_if(server, credits, 0);
1c892549 2951 rc = -ENOMEM;
bae9f746 2952 break;
1da177e4 2953 }
a70307ee 2954
c5fab6f4 2955 rc = cifs_read_allocate_pages(rdata, npages);
1c892549
JL
2956 if (rc)
2957 goto error;
2958
2959 rdata->cfile = cifsFileInfo_get(open_file);
c5fab6f4 2960 rdata->nr_pages = npages;
1c892549
JL
2961 rdata->offset = offset;
2962 rdata->bytes = cur_len;
2963 rdata->pid = pid;
8321fec4
JL
2964 rdata->pagesz = PAGE_SIZE;
2965 rdata->read_into_pages = cifs_uncached_read_into_pages;
bed9da02 2966 rdata->credits = credits;
1c892549 2967
25f40259
PS
2968 if (!rdata->cfile->invalidHandle ||
2969 !cifs_reopen_file(rdata->cfile, true))
2970 rc = server->ops->async_readv(rdata);
1c892549
JL
2971error:
2972 if (rc) {
bed9da02 2973 add_credits_and_wake_if(server, rdata->credits, 0);
1c892549
JL
2974 kref_put(&rdata->refcount,
2975 cifs_uncached_readdata_release);
25f40259
PS
2976 if (rc == -EAGAIN)
2977 continue;
1c892549
JL
2978 break;
2979 }
2980
0ada36b2 2981 list_add_tail(&rdata->list, rdata_list);
1c892549
JL
2982 offset += cur_len;
2983 len -= cur_len;
2984 } while (len > 0);
2985
0ada36b2
PS
2986 return rc;
2987}
2988
2989ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
2990{
2991 struct file *file = iocb->ki_filp;
2992 ssize_t rc;
2993 size_t len;
2994 ssize_t total_read = 0;
2995 loff_t offset = iocb->ki_pos;
2996 struct cifs_sb_info *cifs_sb;
2997 struct cifs_tcon *tcon;
2998 struct cifsFileInfo *open_file;
2999 struct cifs_readdata *rdata, *tmp;
3000 struct list_head rdata_list;
3001
3002 len = iov_iter_count(to);
3003 if (!len)
3004 return 0;
3005
3006 INIT_LIST_HEAD(&rdata_list);
7119e220 3007 cifs_sb = CIFS_FILE_SB(file);
0ada36b2
PS
3008 open_file = file->private_data;
3009 tcon = tlink_tcon(open_file->tlink);
3010
3011 if (!tcon->ses->server->ops->async_readv)
3012 return -ENOSYS;
3013
3014 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
3015 cifs_dbg(FYI, "attempting read on write only file instance\n");
3016
3017 rc = cifs_send_async_read(offset, len, open_file, cifs_sb, &rdata_list);
3018
1c892549
JL
3019 /* if at least one read request send succeeded, then reset rc */
3020 if (!list_empty(&rdata_list))
3021 rc = 0;
3022
e6a7bcb4 3023 len = iov_iter_count(to);
1c892549 3024 /* the loop below should proceed in the order of increasing offsets */
25f40259 3025again:
1c892549
JL
3026 list_for_each_entry_safe(rdata, tmp, &rdata_list, list) {
3027 if (!rc) {
1c892549
JL
3028 /* FIXME: freezable sleep too? */
3029 rc = wait_for_completion_killable(&rdata->done);
3030 if (rc)
3031 rc = -EINTR;
fb8a3e52 3032 else if (rdata->result == -EAGAIN) {
74027f4a 3033 /* resend call if it's a retryable error */
fb8a3e52 3034 struct list_head tmp_list;
d913ed17 3035 unsigned int got_bytes = rdata->got_bytes;
25f40259 3036
fb8a3e52
PS
3037 list_del_init(&rdata->list);
3038 INIT_LIST_HEAD(&tmp_list);
25f40259 3039
d913ed17
PS
3040 /*
3041 * Got a part of data and then reconnect has
3042 * happened -- fill the buffer and continue
3043 * reading.
3044 */
3045 if (got_bytes && got_bytes < rdata->bytes) {
3046 rc = cifs_readdata_to_iov(rdata, to);
3047 if (rc) {
3048 kref_put(&rdata->refcount,
3049 cifs_uncached_readdata_release);
3050 continue;
3051 }
74027f4a 3052 }
d913ed17
PS
3053
3054 rc = cifs_send_async_read(
3055 rdata->offset + got_bytes,
3056 rdata->bytes - got_bytes,
3057 rdata->cfile, cifs_sb,
3058 &tmp_list);
25f40259 3059
fb8a3e52 3060 list_splice(&tmp_list, &rdata_list);
25f40259 3061
fb8a3e52
PS
3062 kref_put(&rdata->refcount,
3063 cifs_uncached_readdata_release);
3064 goto again;
3065 } else if (rdata->result)
3066 rc = rdata->result;
3067 else
e6a7bcb4 3068 rc = cifs_readdata_to_iov(rdata, to);
1c892549 3069
2e8a05d8
PS
3070 /* if there was a short read -- discard anything left */
3071 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
3072 rc = -ENODATA;
1da177e4 3073 }
1c892549
JL
3074 list_del_init(&rdata->list);
3075 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
1da177e4 3076 }
a70307ee 3077
e6a7bcb4 3078 total_read = len - iov_iter_count(to);
7f25bba8 3079
1c892549 3080 cifs_stats_bytes_read(tcon, total_read);
1c892549 3081
09a4707e
PS
3082 /* mask nodata case */
3083 if (rc == -ENODATA)
3084 rc = 0;
3085
0165e810 3086 if (total_read) {
e6a7bcb4 3087 iocb->ki_pos += total_read;
0165e810
AV
3088 return total_read;
3089 }
3090 return rc;
a70307ee
PS
3091}
3092
579f9053 3093ssize_t
e6a7bcb4 3094cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
a70307ee 3095{
496ad9aa 3096 struct inode *inode = file_inode(iocb->ki_filp);
579f9053
PS
3097 struct cifsInodeInfo *cinode = CIFS_I(inode);
3098 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3099 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3100 iocb->ki_filp->private_data;
3101 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3102 int rc = -EACCES;
a70307ee
PS
3103
3104 /*
3105 * In strict cache mode we need to read from the server all the time
3106 * if we don't have level II oplock because the server can delay mtime
3107 * change - so we can't make a decision about inode invalidating.
3108 * And we can also fail with pagereading if there are mandatory locks
3109 * on pages affected by this read but not on the region from pos to
3110 * pos+len-1.
3111 */
18cceb6a 3112 if (!CIFS_CACHE_READ(cinode))
e6a7bcb4 3113 return cifs_user_readv(iocb, to);
a70307ee 3114
579f9053
PS
3115 if (cap_unix(tcon->ses) &&
3116 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
3117 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
e6a7bcb4 3118 return generic_file_read_iter(iocb, to);
579f9053
PS
3119
3120 /*
3121 * We need to hold the sem to be sure nobody modifies lock list
3122 * with a brlock that prevents reading.
3123 */
3124 down_read(&cinode->lock_sem);
e6a7bcb4 3125 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
579f9053 3126 tcon->ses->server->vals->shared_lock_type,
081c0414 3127 NULL, CIFS_READ_OP))
e6a7bcb4 3128 rc = generic_file_read_iter(iocb, to);
579f9053
PS
3129 up_read(&cinode->lock_sem);
3130 return rc;
a70307ee 3131}
1da177e4 3132
f9c6e234
PS
3133static ssize_t
3134cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
1da177e4
LT
3135{
3136 int rc = -EACCES;
3137 unsigned int bytes_read = 0;
3138 unsigned int total_read;
3139 unsigned int current_read_size;
5eba8ab3 3140 unsigned int rsize;
1da177e4 3141 struct cifs_sb_info *cifs_sb;
29e20f9c 3142 struct cifs_tcon *tcon;
f9c6e234 3143 struct TCP_Server_Info *server;
6d5786a3 3144 unsigned int xid;
f9c6e234 3145 char *cur_offset;
1da177e4 3146 struct cifsFileInfo *open_file;
d4ffff1f 3147 struct cifs_io_parms io_parms;
ec637e3f 3148 int buf_type = CIFS_NO_BUFFER;
d4ffff1f 3149 __u32 pid;
1da177e4 3150
6d5786a3 3151 xid = get_xid();
7119e220 3152 cifs_sb = CIFS_FILE_SB(file);
1da177e4 3153
5eba8ab3
JL
3154 /* FIXME: set up handlers for larger reads and/or convert to async */
3155 rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
3156
1da177e4 3157 if (file->private_data == NULL) {
0f3bc09e 3158 rc = -EBADF;
6d5786a3 3159 free_xid(xid);
0f3bc09e 3160 return rc;
1da177e4 3161 }
c21dfb69 3162 open_file = file->private_data;
29e20f9c 3163 tcon = tlink_tcon(open_file->tlink);
f9c6e234
PS
3164 server = tcon->ses->server;
3165
3166 if (!server->ops->sync_read) {
3167 free_xid(xid);
3168 return -ENOSYS;
3169 }
1da177e4 3170
d4ffff1f
PS
3171 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3172 pid = open_file->pid;
3173 else
3174 pid = current->tgid;
3175
1da177e4 3176 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
f96637be 3177 cifs_dbg(FYI, "attempting read on write only file instance\n");
1da177e4 3178
f9c6e234
PS
3179 for (total_read = 0, cur_offset = read_data; read_size > total_read;
3180 total_read += bytes_read, cur_offset += bytes_read) {
e374d90f
PS
3181 do {
3182 current_read_size = min_t(uint, read_size - total_read,
3183 rsize);
3184 /*
3185 * For windows me and 9x we do not want to request more
3186 * than it negotiated since it will refuse the read
3187 * then.
3188 */
3189 if ((tcon->ses) && !(tcon->ses->capabilities &
29e20f9c 3190 tcon->ses->server->vals->cap_large_files)) {
e374d90f
PS
3191 current_read_size = min_t(uint,
3192 current_read_size, CIFSMaxBufSize);
3193 }
cdff08e7 3194 if (open_file->invalidHandle) {
15886177 3195 rc = cifs_reopen_file(open_file, true);
1da177e4
LT
3196 if (rc != 0)
3197 break;
3198 }
d4ffff1f 3199 io_parms.pid = pid;
29e20f9c 3200 io_parms.tcon = tcon;
f9c6e234 3201 io_parms.offset = *offset;
d4ffff1f 3202 io_parms.length = current_read_size;
db8b631d 3203 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
f9c6e234
PS
3204 &bytes_read, &cur_offset,
3205 &buf_type);
e374d90f
PS
3206 } while (rc == -EAGAIN);
3207
1da177e4
LT
3208 if (rc || (bytes_read == 0)) {
3209 if (total_read) {
3210 break;
3211 } else {
6d5786a3 3212 free_xid(xid);
1da177e4
LT
3213 return rc;
3214 }
3215 } else {
29e20f9c 3216 cifs_stats_bytes_read(tcon, total_read);
f9c6e234 3217 *offset += bytes_read;
1da177e4
LT
3218 }
3219 }
6d5786a3 3220 free_xid(xid);
1da177e4
LT
3221 return total_read;
3222}
3223
ca83ce3d
JL
3224/*
3225 * If the page is mmap'ed into a process' page tables, then we need to make
3226 * sure that it doesn't change while being written back.
3227 */
3228static int
3229cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
3230{
3231 struct page *page = vmf->page;
3232
3233 lock_page(page);
3234 return VM_FAULT_LOCKED;
3235}
3236
3237static struct vm_operations_struct cifs_file_vm_ops = {
3238 .fault = filemap_fault,
f1820361 3239 .map_pages = filemap_map_pages,
ca83ce3d 3240 .page_mkwrite = cifs_page_mkwrite,
0b173bc4 3241 .remap_pages = generic_file_remap_pages,
ca83ce3d
JL
3242};
3243
7a6a19b1
PS
3244int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
3245{
3246 int rc, xid;
496ad9aa 3247 struct inode *inode = file_inode(file);
7a6a19b1 3248
6d5786a3 3249 xid = get_xid();
7a6a19b1 3250
18cceb6a 3251 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
4f73c7d3 3252 rc = cifs_zap_mapping(inode);
6feb9891
PS
3253 if (rc)
3254 return rc;
3255 }
7a6a19b1
PS
3256
3257 rc = generic_file_mmap(file, vma);
ca83ce3d
JL
3258 if (rc == 0)
3259 vma->vm_ops = &cifs_file_vm_ops;
6d5786a3 3260 free_xid(xid);
7a6a19b1
PS
3261 return rc;
3262}
3263
1da177e4
LT
3264int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
3265{
1da177e4
LT
3266 int rc, xid;
3267
6d5786a3 3268 xid = get_xid();
abab095d 3269 rc = cifs_revalidate_file(file);
1da177e4 3270 if (rc) {
f96637be
JP
3271 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
3272 rc);
6d5786a3 3273 free_xid(xid);
1da177e4
LT
3274 return rc;
3275 }
3276 rc = generic_file_mmap(file, vma);
ca83ce3d
JL
3277 if (rc == 0)
3278 vma->vm_ops = &cifs_file_vm_ops;
6d5786a3 3279 free_xid(xid);
1da177e4
LT
3280 return rc;
3281}
3282
0471ca3f
JL
3283static void
3284cifs_readv_complete(struct work_struct *work)
3285{
b770ddfa 3286 unsigned int i, got_bytes;
0471ca3f
JL
3287 struct cifs_readdata *rdata = container_of(work,
3288 struct cifs_readdata, work);
0471ca3f 3289
b770ddfa 3290 got_bytes = rdata->got_bytes;
c5fab6f4
JL
3291 for (i = 0; i < rdata->nr_pages; i++) {
3292 struct page *page = rdata->pages[i];
3293
0471ca3f
JL
3294 lru_cache_add_file(page);
3295
b770ddfa
PS
3296 if (rdata->result == 0 ||
3297 (rdata->result == -EAGAIN && got_bytes)) {
0471ca3f
JL
3298 flush_dcache_page(page);
3299 SetPageUptodate(page);
3300 }
3301
3302 unlock_page(page);
3303
b770ddfa
PS
3304 if (rdata->result == 0 ||
3305 (rdata->result == -EAGAIN && got_bytes))
0471ca3f
JL
3306 cifs_readpage_to_fscache(rdata->mapping->host, page);
3307
b770ddfa
PS
3308 got_bytes -= min_t(unsigned int, PAGE_CACHE_SIZE, got_bytes);
3309
0471ca3f 3310 page_cache_release(page);
c5fab6f4 3311 rdata->pages[i] = NULL;
0471ca3f 3312 }
6993f74a 3313 kref_put(&rdata->refcount, cifs_readdata_release);
0471ca3f
JL
3314}
3315
8d5ce4d2 3316static int
8321fec4
JL
3317cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
3318 struct cifs_readdata *rdata, unsigned int len)
8d5ce4d2 3319{
b3160aeb 3320 int result = 0;
c5fab6f4 3321 unsigned int i;
8d5ce4d2
JL
3322 u64 eof;
3323 pgoff_t eof_index;
c5fab6f4 3324 unsigned int nr_pages = rdata->nr_pages;
8321fec4 3325 struct kvec iov;
8d5ce4d2
JL
3326
3327 /* determine the eof that the server (probably) has */
3328 eof = CIFS_I(rdata->mapping->host)->server_eof;
3329 eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
f96637be 3330 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
8d5ce4d2 3331
b3160aeb 3332 rdata->got_bytes = 0;
8321fec4 3333 rdata->tailsz = PAGE_CACHE_SIZE;
c5fab6f4
JL
3334 for (i = 0; i < nr_pages; i++) {
3335 struct page *page = rdata->pages[i];
3336
8321fec4 3337 if (len >= PAGE_CACHE_SIZE) {
8d5ce4d2 3338 /* enough data to fill the page */
8321fec4
JL
3339 iov.iov_base = kmap(page);
3340 iov.iov_len = PAGE_CACHE_SIZE;
f96637be
JP
3341 cifs_dbg(FYI, "%u: idx=%lu iov_base=%p iov_len=%zu\n",
3342 i, page->index, iov.iov_base, iov.iov_len);
8321fec4
JL
3343 len -= PAGE_CACHE_SIZE;
3344 } else if (len > 0) {
8d5ce4d2 3345 /* enough for partial page, fill and zero the rest */
8321fec4
JL
3346 iov.iov_base = kmap(page);
3347 iov.iov_len = len;
f96637be
JP
3348 cifs_dbg(FYI, "%u: idx=%lu iov_base=%p iov_len=%zu\n",
3349 i, page->index, iov.iov_base, iov.iov_len);
8321fec4
JL
3350 memset(iov.iov_base + len,
3351 '\0', PAGE_CACHE_SIZE - len);
3352 rdata->tailsz = len;
3353 len = 0;
8d5ce4d2
JL
3354 } else if (page->index > eof_index) {
3355 /*
3356 * The VFS will not try to do readahead past the
3357 * i_size, but it's possible that we have outstanding
3358 * writes with gaps in the middle and the i_size hasn't
3359 * caught up yet. Populate those with zeroed out pages
3360 * to prevent the VFS from repeatedly attempting to
3361 * fill them until the writes are flushed.
3362 */
3363 zero_user(page, 0, PAGE_CACHE_SIZE);
8d5ce4d2
JL
3364 lru_cache_add_file(page);
3365 flush_dcache_page(page);
3366 SetPageUptodate(page);
3367 unlock_page(page);
3368 page_cache_release(page);
c5fab6f4
JL
3369 rdata->pages[i] = NULL;
3370 rdata->nr_pages--;
8321fec4 3371 continue;
8d5ce4d2
JL
3372 } else {
3373 /* no need to hold page hostage */
8d5ce4d2
JL
3374 lru_cache_add_file(page);
3375 unlock_page(page);
3376 page_cache_release(page);
c5fab6f4
JL
3377 rdata->pages[i] = NULL;
3378 rdata->nr_pages--;
8321fec4 3379 continue;
8d5ce4d2 3380 }
8321fec4
JL
3381
3382 result = cifs_readv_from_socket(server, &iov, 1, iov.iov_len);
3383 kunmap(page);
3384 if (result < 0)
3385 break;
3386
b3160aeb 3387 rdata->got_bytes += result;
8d5ce4d2
JL
3388 }
3389
b3160aeb
PS
3390 return rdata->got_bytes > 0 && result != -ECONNABORTED ?
3391 rdata->got_bytes : result;
8d5ce4d2
JL
3392}
3393
387eb92a
PS
3394static int
3395readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
3396 unsigned int rsize, struct list_head *tmplist,
3397 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
3398{
3399 struct page *page, *tpage;
3400 unsigned int expected_index;
3401 int rc;
3402
69cebd75
PS
3403 INIT_LIST_HEAD(tmplist);
3404
387eb92a
PS
3405 page = list_entry(page_list->prev, struct page, lru);
3406
3407 /*
3408 * Lock the page and put it in the cache. Since no one else
3409 * should have access to this page, we're safe to simply set
3410 * PG_locked without checking it first.
3411 */
3412 __set_page_locked(page);
3413 rc = add_to_page_cache_locked(page, mapping,
3414 page->index, GFP_KERNEL);
3415
3416 /* give up if we can't stick it in the cache */
3417 if (rc) {
3418 __clear_page_locked(page);
3419 return rc;
3420 }
3421
3422 /* move first page to the tmplist */
3423 *offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
3424 *bytes = PAGE_CACHE_SIZE;
3425 *nr_pages = 1;
3426 list_move_tail(&page->lru, tmplist);
3427
3428 /* now try and add more pages onto the request */
3429 expected_index = page->index + 1;
3430 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
3431 /* discontinuity ? */
3432 if (page->index != expected_index)
3433 break;
3434
3435 /* would this page push the read over the rsize? */
3436 if (*bytes + PAGE_CACHE_SIZE > rsize)
3437 break;
3438
3439 __set_page_locked(page);
3440 if (add_to_page_cache_locked(page, mapping, page->index,
3441 GFP_KERNEL)) {
3442 __clear_page_locked(page);
3443 break;
3444 }
3445 list_move_tail(&page->lru, tmplist);
3446 (*bytes) += PAGE_CACHE_SIZE;
3447 expected_index++;
3448 (*nr_pages)++;
3449 }
3450 return rc;
8d5ce4d2
JL
3451}
3452
1da177e4
LT
3453static int cifs_readpages(struct file *file, struct address_space *mapping,
3454 struct list_head *page_list, unsigned num_pages)
3455{
690c5e31
JL
3456 int rc;
3457 struct list_head tmplist;
3458 struct cifsFileInfo *open_file = file->private_data;
7119e220 3459 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
69cebd75 3460 struct TCP_Server_Info *server;
690c5e31 3461 pid_t pid;
1da177e4 3462
56698236
SJ
3463 /*
3464 * Reads as many pages as possible from fscache. Returns -ENOBUFS
3465 * immediately if the cookie is negative
54afa990
DH
3466 *
3467 * After this point, every page in the list might have PG_fscache set,
3468 * so we will need to clean that up off of every page we don't use.
56698236
SJ
3469 */
3470 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
3471 &num_pages);
3472 if (rc == 0)
690c5e31 3473 return rc;
56698236 3474
d4ffff1f
PS
3475 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3476 pid = open_file->pid;
3477 else
3478 pid = current->tgid;
3479
690c5e31 3480 rc = 0;
69cebd75 3481 server = tlink_tcon(open_file->tlink)->ses->server;
1da177e4 3482
f96637be
JP
3483 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
3484 __func__, file, mapping, num_pages);
690c5e31
JL
3485
3486 /*
3487 * Start with the page at end of list and move it to private
3488 * list. Do the same with any following pages until we hit
3489 * the rsize limit, hit an index discontinuity, or run out of
3490 * pages. Issue the async read and then start the loop again
3491 * until the list is empty.
3492 *
3493 * Note that list order is important. The page_list is in
3494 * the order of declining indexes. When we put the pages in
3495 * the rdata->pages, then we want them in increasing order.
3496 */
3497 while (!list_empty(page_list)) {
bed9da02 3498 unsigned int i, nr_pages, bytes, rsize;
690c5e31
JL
3499 loff_t offset;
3500 struct page *page, *tpage;
3501 struct cifs_readdata *rdata;
bed9da02 3502 unsigned credits;
1da177e4 3503
bed9da02
PS
3504 rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
3505 &rsize, &credits);
3506 if (rc)
3507 break;
690c5e31
JL
3508
3509 /*
69cebd75
PS
3510 * Give up immediately if rsize is too small to read an entire
3511 * page. The VFS will fall back to readpage. We should never
3512 * reach this point however since we set ra_pages to 0 when the
3513 * rsize is smaller than a cache page.
690c5e31 3514 */
bed9da02
PS
3515 if (unlikely(rsize < PAGE_CACHE_SIZE)) {
3516 add_credits_and_wake_if(server, credits, 0);
69cebd75 3517 return 0;
bed9da02 3518 }
690c5e31 3519
bed9da02
PS
3520 rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
3521 &nr_pages, &offset, &bytes);
690c5e31 3522 if (rc) {
bed9da02 3523 add_credits_and_wake_if(server, credits, 0);
690c5e31
JL
3524 break;
3525 }
3526
0471ca3f 3527 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
690c5e31
JL
3528 if (!rdata) {
3529 /* best to give up if we're out of mem */
3530 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
3531 list_del(&page->lru);
3532 lru_cache_add_file(page);
3533 unlock_page(page);
3534 page_cache_release(page);
3535 }
3536 rc = -ENOMEM;
bed9da02 3537 add_credits_and_wake_if(server, credits, 0);
690c5e31
JL
3538 break;
3539 }
3540
6993f74a 3541 rdata->cfile = cifsFileInfo_get(open_file);
690c5e31
JL
3542 rdata->mapping = mapping;
3543 rdata->offset = offset;
3544 rdata->bytes = bytes;
3545 rdata->pid = pid;
8321fec4
JL
3546 rdata->pagesz = PAGE_CACHE_SIZE;
3547 rdata->read_into_pages = cifs_readpages_read_into_pages;
bed9da02 3548 rdata->credits = credits;
c5fab6f4
JL
3549
3550 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
3551 list_del(&page->lru);
3552 rdata->pages[rdata->nr_pages++] = page;
3553 }
690c5e31 3554
69cebd75
PS
3555 if (!rdata->cfile->invalidHandle ||
3556 !cifs_reopen_file(rdata->cfile, true))
3557 rc = server->ops->async_readv(rdata);
3558 if (rc) {
bed9da02 3559 add_credits_and_wake_if(server, rdata->credits, 0);
c5fab6f4
JL
3560 for (i = 0; i < rdata->nr_pages; i++) {
3561 page = rdata->pages[i];
690c5e31
JL
3562 lru_cache_add_file(page);
3563 unlock_page(page);
3564 page_cache_release(page);
1da177e4 3565 }
1209bbdf 3566 /* Fallback to the readpage in error/reconnect cases */
6993f74a 3567 kref_put(&rdata->refcount, cifs_readdata_release);
1da177e4
LT
3568 break;
3569 }
6993f74a
JL
3570
3571 kref_put(&rdata->refcount, cifs_readdata_release);
1da177e4
LT
3572 }
3573
54afa990
DH
3574 /* Any pages that have been shown to fscache but didn't get added to
3575 * the pagecache must be uncached before they get returned to the
3576 * allocator.
3577 */
3578 cifs_fscache_readpages_cancel(mapping->host, page_list);
1da177e4
LT
3579 return rc;
3580}
3581
a9e9b7bc
SP
3582/*
3583 * cifs_readpage_worker must be called with the page pinned
3584 */
1da177e4
LT
3585static int cifs_readpage_worker(struct file *file, struct page *page,
3586 loff_t *poffset)
3587{
3588 char *read_data;
3589 int rc;
3590
56698236 3591 /* Is the page cached? */
496ad9aa 3592 rc = cifs_readpage_from_fscache(file_inode(file), page);
56698236
SJ
3593 if (rc == 0)
3594 goto read_complete;
3595
1da177e4
LT
3596 read_data = kmap(page);
3597 /* for reads over a certain size could initiate async read ahead */
fb8c4b14 3598
1da177e4 3599 rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
fb8c4b14 3600
1da177e4
LT
3601 if (rc < 0)
3602 goto io_error;
3603 else
f96637be 3604 cifs_dbg(FYI, "Bytes read %d\n", rc);
fb8c4b14 3605
496ad9aa
AV
3606 file_inode(file)->i_atime =
3607 current_fs_time(file_inode(file)->i_sb);
fb8c4b14 3608
1da177e4
LT
3609 if (PAGE_CACHE_SIZE > rc)
3610 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
3611
3612 flush_dcache_page(page);
3613 SetPageUptodate(page);
9dc06558
SJ
3614
3615 /* send this page to the cache */
496ad9aa 3616 cifs_readpage_to_fscache(file_inode(file), page);
9dc06558 3617
1da177e4 3618 rc = 0;
fb8c4b14 3619
1da177e4 3620io_error:
fb8c4b14 3621 kunmap(page);
466bd31b 3622 unlock_page(page);
56698236
SJ
3623
3624read_complete:
1da177e4
LT
3625 return rc;
3626}
3627
3628static int cifs_readpage(struct file *file, struct page *page)
3629{
3630 loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
3631 int rc = -EACCES;
6d5786a3 3632 unsigned int xid;
1da177e4 3633
6d5786a3 3634 xid = get_xid();
1da177e4
LT
3635
3636 if (file->private_data == NULL) {
0f3bc09e 3637 rc = -EBADF;
6d5786a3 3638 free_xid(xid);
0f3bc09e 3639 return rc;
1da177e4
LT
3640 }
3641
f96637be 3642 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
b6b38f70 3643 page, (int)offset, (int)offset);
1da177e4
LT
3644
3645 rc = cifs_readpage_worker(file, page, &offset);
3646
6d5786a3 3647 free_xid(xid);
1da177e4
LT
3648 return rc;
3649}
3650
a403a0a3
SF
3651static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
3652{
3653 struct cifsFileInfo *open_file;
3654
4477288a 3655 spin_lock(&cifs_file_list_lock);
a403a0a3 3656 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2e396b83 3657 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4477288a 3658 spin_unlock(&cifs_file_list_lock);
a403a0a3
SF
3659 return 1;
3660 }
3661 }
4477288a 3662 spin_unlock(&cifs_file_list_lock);
a403a0a3
SF
3663 return 0;
3664}
3665
1da177e4
LT
3666/* We do not want to update the file size from server for inodes
3667 open for write - to avoid races with writepage extending
3668 the file - in the future we could consider allowing
fb8c4b14 3669 refreshing the inode only on increases in the file size
1da177e4
LT
3670 but this is tricky to do without racing with writebehind
3671 page caching in the current Linux kernel design */
4b18f2a9 3672bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
1da177e4 3673{
a403a0a3 3674 if (!cifsInode)
4b18f2a9 3675 return true;
50c2f753 3676
a403a0a3
SF
3677 if (is_inode_writable(cifsInode)) {
3678 /* This inode is open for write at least once */
c32a0b68
SF
3679 struct cifs_sb_info *cifs_sb;
3680
c32a0b68 3681 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
ad7a2926 3682 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
fb8c4b14 3683 /* since no page cache to corrupt on directio
c32a0b68 3684 we can change size safely */
4b18f2a9 3685 return true;
c32a0b68
SF
3686 }
3687
fb8c4b14 3688 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
4b18f2a9 3689 return true;
7ba52631 3690
4b18f2a9 3691 return false;
23e7dd7d 3692 } else
4b18f2a9 3693 return true;
1da177e4
LT
3694}
3695
d9414774
NP
3696static int cifs_write_begin(struct file *file, struct address_space *mapping,
3697 loff_t pos, unsigned len, unsigned flags,
3698 struct page **pagep, void **fsdata)
1da177e4 3699{
466bd31b 3700 int oncethru = 0;
d9414774
NP
3701 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
3702 loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
a98ee8c1
JL
3703 loff_t page_start = pos & PAGE_MASK;
3704 loff_t i_size;
3705 struct page *page;
3706 int rc = 0;
d9414774 3707
f96637be 3708 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
d9414774 3709
466bd31b 3710start:
54566b2c 3711 page = grab_cache_page_write_begin(mapping, index, flags);
a98ee8c1
JL
3712 if (!page) {
3713 rc = -ENOMEM;
3714 goto out;
3715 }
8a236264 3716
a98ee8c1
JL
3717 if (PageUptodate(page))
3718 goto out;
8a236264 3719
a98ee8c1
JL
3720 /*
3721 * If we write a full page it will be up to date, no need to read from
3722 * the server. If the write is short, we'll end up doing a sync write
3723 * instead.
3724 */
3725 if (len == PAGE_CACHE_SIZE)
3726 goto out;
8a236264 3727
a98ee8c1
JL
3728 /*
3729 * optimize away the read when we have an oplock, and we're not
3730 * expecting to use any of the data we'd be reading in. That
3731 * is, when the page lies beyond the EOF, or straddles the EOF
3732 * and the write will cover all of the existing data.
3733 */
18cceb6a 3734 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
a98ee8c1
JL
3735 i_size = i_size_read(mapping->host);
3736 if (page_start >= i_size ||
3737 (offset == 0 && (pos + len) >= i_size)) {
3738 zero_user_segments(page, 0, offset,
3739 offset + len,
3740 PAGE_CACHE_SIZE);
3741 /*
3742 * PageChecked means that the parts of the page
3743 * to which we're not writing are considered up
3744 * to date. Once the data is copied to the
3745 * page, it can be set uptodate.
3746 */
3747 SetPageChecked(page);
3748 goto out;
3749 }
3750 }
d9414774 3751
466bd31b 3752 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
a98ee8c1
JL
3753 /*
3754 * might as well read a page, it is fast enough. If we get
3755 * an error, we don't need to return it. cifs_write_end will
3756 * do a sync write instead since PG_uptodate isn't set.
3757 */
3758 cifs_readpage_worker(file, page, &page_start);
466bd31b
SP
3759 page_cache_release(page);
3760 oncethru = 1;
3761 goto start;
8a236264
SF
3762 } else {
3763 /* we could try using another file handle if there is one -
3764 but how would we lock it to prevent close of that handle
3765 racing with this read? In any case
d9414774 3766 this will be written out by write_end so is fine */
1da177e4 3767 }
a98ee8c1
JL
3768out:
3769 *pagep = page;
3770 return rc;
1da177e4
LT
3771}
3772
85f2d6b4
SJ
3773static int cifs_release_page(struct page *page, gfp_t gfp)
3774{
3775 if (PagePrivate(page))
3776 return 0;
3777
3778 return cifs_fscache_release_page(page, gfp);
3779}
3780
d47992f8
LC
3781static void cifs_invalidate_page(struct page *page, unsigned int offset,
3782 unsigned int length)
85f2d6b4
SJ
3783{
3784 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
3785
d47992f8 3786 if (offset == 0 && length == PAGE_CACHE_SIZE)
85f2d6b4
SJ
3787 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
3788}
3789
9ad1506b
PS
3790static int cifs_launder_page(struct page *page)
3791{
3792 int rc = 0;
3793 loff_t range_start = page_offset(page);
3794 loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
3795 struct writeback_control wbc = {
3796 .sync_mode = WB_SYNC_ALL,
3797 .nr_to_write = 0,
3798 .range_start = range_start,
3799 .range_end = range_end,
3800 };
3801
f96637be 3802 cifs_dbg(FYI, "Launder page: %p\n", page);
9ad1506b
PS
3803
3804 if (clear_page_dirty_for_io(page))
3805 rc = cifs_writepage_locked(page, &wbc);
3806
3807 cifs_fscache_invalidate_page(page, page->mapping->host);
3808 return rc;
3809}
3810
9b646972 3811void cifs_oplock_break(struct work_struct *work)
3bc303c2
JL
3812{
3813 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
3814 oplock_break);
a5e18bc3 3815 struct inode *inode = cfile->dentry->d_inode;
3bc303c2 3816 struct cifsInodeInfo *cinode = CIFS_I(inode);
95a3f2f3 3817 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
c11f1df5 3818 struct TCP_Server_Info *server = tcon->ses->server;
eb4b756b 3819 int rc = 0;
3bc303c2 3820
c11f1df5 3821 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
74316201 3822 TASK_UNINTERRUPTIBLE);
c11f1df5
SP
3823
3824 server->ops->downgrade_oplock(server, cinode,
3825 test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags));
3826
18cceb6a 3827 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
63b7d3a4 3828 cifs_has_mand_locks(cinode)) {
f96637be
JP
3829 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
3830 inode);
18cceb6a 3831 cinode->oplock = 0;
63b7d3a4
PS
3832 }
3833
3bc303c2 3834 if (inode && S_ISREG(inode->i_mode)) {
18cceb6a 3835 if (CIFS_CACHE_READ(cinode))
8737c930 3836 break_lease(inode, O_RDONLY);
d54ff732 3837 else
8737c930 3838 break_lease(inode, O_WRONLY);
3bc303c2 3839 rc = filemap_fdatawrite(inode->i_mapping);
18cceb6a 3840 if (!CIFS_CACHE_READ(cinode)) {
eb4b756b
JL
3841 rc = filemap_fdatawait(inode->i_mapping);
3842 mapping_set_error(inode->i_mapping, rc);
4f73c7d3 3843 cifs_zap_mapping(inode);
3bc303c2 3844 }
f96637be 3845 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
3bc303c2
JL
3846 }
3847
85160e03
PS
3848 rc = cifs_push_locks(cfile);
3849 if (rc)
f96637be 3850 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
85160e03 3851
3bc303c2
JL
3852 /*
3853 * releasing stale oplock after recent reconnect of smb session using
3854 * a now incorrect file handle is not a data integrity issue but do
3855 * not bother sending an oplock release if session to server still is
3856 * disconnected since oplock already released by the server
3857 */
cdff08e7 3858 if (!cfile->oplock_break_cancelled) {
95a3f2f3
PS
3859 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
3860 cinode);
f96637be 3861 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
3bc303c2 3862 }
c11f1df5 3863 cifs_done_oplock_break(cinode);
3bc303c2
JL
3864}
3865
dca69288
SF
3866/*
3867 * The presence of cifs_direct_io() in the address space ops vector
3868 * allowes open() O_DIRECT flags which would have failed otherwise.
3869 *
3870 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
3871 * so this method should never be called.
3872 *
3873 * Direct IO is not yet supported in the cached mode.
3874 */
3875static ssize_t
d8d3d94b
AV
3876cifs_direct_io(int rw, struct kiocb *iocb, struct iov_iter *iter,
3877 loff_t pos)
dca69288
SF
3878{
3879 /*
3880 * FIXME
3881 * Eventually need to support direct IO for non forcedirectio mounts
3882 */
3883 return -EINVAL;
3884}
3885
3886
f5e54d6e 3887const struct address_space_operations cifs_addr_ops = {
1da177e4
LT
3888 .readpage = cifs_readpage,
3889 .readpages = cifs_readpages,
3890 .writepage = cifs_writepage,
37c0eb46 3891 .writepages = cifs_writepages,
d9414774
NP
3892 .write_begin = cifs_write_begin,
3893 .write_end = cifs_write_end,
1da177e4 3894 .set_page_dirty = __set_page_dirty_nobuffers,
85f2d6b4 3895 .releasepage = cifs_release_page,
dca69288 3896 .direct_IO = cifs_direct_io,
85f2d6b4 3897 .invalidatepage = cifs_invalidate_page,
9ad1506b 3898 .launder_page = cifs_launder_page,
1da177e4 3899};
273d81d6
DK
3900
3901/*
3902 * cifs_readpages requires the server to support a buffer large enough to
3903 * contain the header plus one complete page of data. Otherwise, we need
3904 * to leave cifs_readpages out of the address space operations.
3905 */
f5e54d6e 3906const struct address_space_operations cifs_addr_ops_smallbuf = {
273d81d6
DK
3907 .readpage = cifs_readpage,
3908 .writepage = cifs_writepage,
3909 .writepages = cifs_writepages,
d9414774
NP
3910 .write_begin = cifs_write_begin,
3911 .write_end = cifs_write_end,
273d81d6 3912 .set_page_dirty = __set_page_dirty_nobuffers,
85f2d6b4
SJ
3913 .releasepage = cifs_release_page,
3914 .invalidatepage = cifs_invalidate_page,
9ad1506b 3915 .launder_page = cifs_launder_page,
273d81d6 3916};
This page took 0.88102 seconds and 5 git commands to generate.