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