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