a3b545ff5250f5d4117abd27df4216734bc5fa33
[linux-2.6.git] / fs / cifs / file.c
1 /*
2  *   fs/cifs/file.c
3  *
4  *   vfs operations that deal with files
5  *
6  *   Copyright (C) International Business Machines  Corp., 2002,2010
7  *   Author(s): Steve French (sfrench@us.ibm.com)
8  *              Jeremy Allison (jra@samba.org)
9  *
10  *   This library is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU Lesser General Public License as published
12  *   by the Free Software Foundation; either version 2.1 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This library is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
18  *   the GNU Lesser General Public License for more details.
19  *
20  *   You should have received a copy of the GNU Lesser General Public License
21  *   along with this library; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 #include <linux/fs.h>
25 #include <linux/backing-dev.h>
26 #include <linux/stat.h>
27 #include <linux/fcntl.h>
28 #include <linux/pagemap.h>
29 #include <linux/pagevec.h>
30 #include <linux/writeback.h>
31 #include <linux/task_io_accounting_ops.h>
32 #include <linux/delay.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <asm/div64.h>
37 #include "cifsfs.h"
38 #include "cifspdu.h"
39 #include "cifsglob.h"
40 #include "cifsproto.h"
41 #include "cifs_unicode.h"
42 #include "cifs_debug.h"
43 #include "cifs_fs_sb.h"
44 #include "fscache.h"
45
46 static inline int cifs_convert_flags(unsigned int flags)
47 {
48         if ((flags & O_ACCMODE) == O_RDONLY)
49                 return GENERIC_READ;
50         else if ((flags & O_ACCMODE) == O_WRONLY)
51                 return GENERIC_WRITE;
52         else if ((flags & O_ACCMODE) == O_RDWR) {
53                 /* GENERIC_ALL is too much permission to request
54                    can cause unnecessary access denied on create */
55                 /* return GENERIC_ALL; */
56                 return (GENERIC_READ | GENERIC_WRITE);
57         }
58
59         return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
60                 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
61                 FILE_READ_DATA);
62 }
63
64 static u32 cifs_posix_convert_flags(unsigned int flags)
65 {
66         u32 posix_flags = 0;
67
68         if ((flags & O_ACCMODE) == O_RDONLY)
69                 posix_flags = SMB_O_RDONLY;
70         else if ((flags & O_ACCMODE) == O_WRONLY)
71                 posix_flags = SMB_O_WRONLY;
72         else if ((flags & O_ACCMODE) == O_RDWR)
73                 posix_flags = SMB_O_RDWR;
74
75         if (flags & O_CREAT)
76                 posix_flags |= SMB_O_CREAT;
77         if (flags & O_EXCL)
78                 posix_flags |= SMB_O_EXCL;
79         if (flags & O_TRUNC)
80                 posix_flags |= SMB_O_TRUNC;
81         /* be safe and imply O_SYNC for O_DSYNC */
82         if (flags & O_DSYNC)
83                 posix_flags |= SMB_O_SYNC;
84         if (flags & O_DIRECTORY)
85                 posix_flags |= SMB_O_DIRECTORY;
86         if (flags & O_NOFOLLOW)
87                 posix_flags |= SMB_O_NOFOLLOW;
88         if (flags & O_DIRECT)
89                 posix_flags |= SMB_O_DIRECT;
90
91         return posix_flags;
92 }
93
94 static inline int cifs_get_disposition(unsigned int flags)
95 {
96         if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
97                 return FILE_CREATE;
98         else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
99                 return FILE_OVERWRITE_IF;
100         else if ((flags & O_CREAT) == O_CREAT)
101                 return FILE_OPEN_IF;
102         else if ((flags & O_TRUNC) == O_TRUNC)
103                 return FILE_OVERWRITE;
104         else
105                 return FILE_OPEN;
106 }
107
108 int cifs_posix_open(char *full_path, struct inode **pinode,
109                         struct super_block *sb, int mode, unsigned int f_flags,
110                         __u32 *poplock, __u16 *pnetfid, int xid)
111 {
112         int rc;
113         FILE_UNIX_BASIC_INFO *presp_data;
114         __u32 posix_flags = 0;
115         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
116         struct cifs_fattr fattr;
117         struct tcon_link *tlink;
118         struct cifs_tcon *tcon;
119
120         cFYI(1, "posix open %s", full_path);
121
122         presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
123         if (presp_data == NULL)
124                 return -ENOMEM;
125
126         tlink = cifs_sb_tlink(cifs_sb);
127         if (IS_ERR(tlink)) {
128                 rc = PTR_ERR(tlink);
129                 goto posix_open_ret;
130         }
131
132         tcon = tlink_tcon(tlink);
133         mode &= ~current_umask();
134
135         posix_flags = cifs_posix_convert_flags(f_flags);
136         rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
137                              poplock, full_path, cifs_sb->local_nls,
138                              cifs_sb->mnt_cifs_flags &
139                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
140         cifs_put_tlink(tlink);
141
142         if (rc)
143                 goto posix_open_ret;
144
145         if (presp_data->Type == cpu_to_le32(-1))
146                 goto posix_open_ret; /* open ok, caller does qpathinfo */
147
148         if (!pinode)
149                 goto posix_open_ret; /* caller does not need info */
150
151         cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
152
153         /* get new inode and set it up */
154         if (*pinode == NULL) {
155                 cifs_fill_uniqueid(sb, &fattr);
156                 *pinode = cifs_iget(sb, &fattr);
157                 if (!*pinode) {
158                         rc = -ENOMEM;
159                         goto posix_open_ret;
160                 }
161         } else {
162                 cifs_fattr_to_inode(*pinode, &fattr);
163         }
164
165 posix_open_ret:
166         kfree(presp_data);
167         return rc;
168 }
169
170 static int
171 cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
172              struct cifs_tcon *tcon, unsigned int f_flags, __u32 *poplock,
173              __u16 *pnetfid, int xid)
174 {
175         int rc;
176         int desiredAccess;
177         int disposition;
178         int create_options = CREATE_NOT_DIR;
179         FILE_ALL_INFO *buf;
180
181         desiredAccess = cifs_convert_flags(f_flags);
182
183 /*********************************************************************
184  *  open flag mapping table:
185  *
186  *      POSIX Flag            CIFS Disposition
187  *      ----------            ----------------
188  *      O_CREAT               FILE_OPEN_IF
189  *      O_CREAT | O_EXCL      FILE_CREATE
190  *      O_CREAT | O_TRUNC     FILE_OVERWRITE_IF
191  *      O_TRUNC               FILE_OVERWRITE
192  *      none of the above     FILE_OPEN
193  *
194  *      Note that there is not a direct match between disposition
195  *      FILE_SUPERSEDE (ie create whether or not file exists although
196  *      O_CREAT | O_TRUNC is similar but truncates the existing
197  *      file rather than creating a new file as FILE_SUPERSEDE does
198  *      (which uses the attributes / metadata passed in on open call)
199  *?
200  *?  O_SYNC is a reasonable match to CIFS writethrough flag
201  *?  and the read write flags match reasonably.  O_LARGEFILE
202  *?  is irrelevant because largefile support is always used
203  *?  by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
204  *       O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
205  *********************************************************************/
206
207         disposition = cifs_get_disposition(f_flags);
208
209         /* BB pass O_SYNC flag through on file attributes .. BB */
210
211         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
212         if (!buf)
213                 return -ENOMEM;
214
215         if (backup_cred(cifs_sb))
216                 create_options |= CREATE_OPEN_BACKUP_INTENT;
217
218         if (tcon->ses->capabilities & CAP_NT_SMBS)
219                 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
220                          desiredAccess, create_options, pnetfid, poplock, buf,
221                          cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
222                                  & CIFS_MOUNT_MAP_SPECIAL_CHR);
223         else
224                 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
225                         desiredAccess, CREATE_NOT_DIR, pnetfid, poplock, buf,
226                         cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
227                                 & CIFS_MOUNT_MAP_SPECIAL_CHR);
228
229         if (rc)
230                 goto out;
231
232         if (tcon->unix_ext)
233                 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
234                                               xid);
235         else
236                 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
237                                          xid, pnetfid);
238
239 out:
240         kfree(buf);
241         return rc;
242 }
243
244 struct cifsFileInfo *
245 cifs_new_fileinfo(__u16 fileHandle, struct file *file,
246                   struct tcon_link *tlink, __u32 oplock)
247 {
248         struct dentry *dentry = file->f_path.dentry;
249         struct inode *inode = dentry->d_inode;
250         struct cifsInodeInfo *pCifsInode = CIFS_I(inode);
251         struct cifsFileInfo *pCifsFile;
252
253         pCifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
254         if (pCifsFile == NULL)
255                 return pCifsFile;
256
257         pCifsFile->count = 1;
258         pCifsFile->netfid = fileHandle;
259         pCifsFile->pid = current->tgid;
260         pCifsFile->uid = current_fsuid();
261         pCifsFile->dentry = dget(dentry);
262         pCifsFile->f_flags = file->f_flags;
263         pCifsFile->invalidHandle = false;
264         pCifsFile->tlink = cifs_get_tlink(tlink);
265         mutex_init(&pCifsFile->fh_mutex);
266         INIT_WORK(&pCifsFile->oplock_break, cifs_oplock_break);
267
268         spin_lock(&cifs_file_list_lock);
269         list_add(&pCifsFile->tlist, &(tlink_tcon(tlink)->openFileList));
270         /* if readable file instance put first in list*/
271         if (file->f_mode & FMODE_READ)
272                 list_add(&pCifsFile->flist, &pCifsInode->openFileList);
273         else
274                 list_add_tail(&pCifsFile->flist, &pCifsInode->openFileList);
275         spin_unlock(&cifs_file_list_lock);
276
277         cifs_set_oplock_level(pCifsInode, oplock);
278
279         file->private_data = pCifsFile;
280         return pCifsFile;
281 }
282
283 /*
284  * Release a reference on the file private data. This may involve closing
285  * the filehandle out on the server. Must be called without holding
286  * cifs_file_list_lock.
287  */
288 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
289 {
290         struct inode *inode = cifs_file->dentry->d_inode;
291         struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
292         struct cifsInodeInfo *cifsi = CIFS_I(inode);
293         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
294         struct cifsLockInfo *li, *tmp;
295
296         spin_lock(&cifs_file_list_lock);
297         if (--cifs_file->count > 0) {
298                 spin_unlock(&cifs_file_list_lock);
299                 return;
300         }
301
302         /* remove it from the lists */
303         list_del(&cifs_file->flist);
304         list_del(&cifs_file->tlist);
305
306         if (list_empty(&cifsi->openFileList)) {
307                 cFYI(1, "closing last open instance for inode %p",
308                         cifs_file->dentry->d_inode);
309
310                 /* in strict cache mode we need invalidate mapping on the last
311                    close  because it may cause a error when we open this file
312                    again and get at least level II oplock */
313                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
314                         CIFS_I(inode)->invalid_mapping = true;
315
316                 cifs_set_oplock_level(cifsi, 0);
317         }
318         spin_unlock(&cifs_file_list_lock);
319
320         cancel_work_sync(&cifs_file->oplock_break);
321
322         if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
323                 int xid, rc;
324
325                 xid = GetXid();
326                 rc = CIFSSMBClose(xid, tcon, cifs_file->netfid);
327                 FreeXid(xid);
328         }
329
330         /* Delete any outstanding lock records. We'll lose them when the file
331          * is closed anyway.
332          */
333         mutex_lock(&cifsi->lock_mutex);
334         list_for_each_entry_safe(li, tmp, &cifsi->llist, llist) {
335                 if (li->netfid != cifs_file->netfid)
336                         continue;
337                 list_del(&li->llist);
338                 kfree(li);
339         }
340         mutex_unlock(&cifsi->lock_mutex);
341
342         cifs_put_tlink(cifs_file->tlink);
343         dput(cifs_file->dentry);
344         kfree(cifs_file);
345 }
346
347 int cifs_open(struct inode *inode, struct file *file)
348 {
349         int rc = -EACCES;
350         int xid;
351         __u32 oplock;
352         struct cifs_sb_info *cifs_sb;
353         struct cifs_tcon *tcon;
354         struct tcon_link *tlink;
355         struct cifsFileInfo *pCifsFile = NULL;
356         char *full_path = NULL;
357         bool posix_open_ok = false;
358         __u16 netfid;
359
360         xid = GetXid();
361
362         cifs_sb = CIFS_SB(inode->i_sb);
363         tlink = cifs_sb_tlink(cifs_sb);
364         if (IS_ERR(tlink)) {
365                 FreeXid(xid);
366                 return PTR_ERR(tlink);
367         }
368         tcon = tlink_tcon(tlink);
369
370         full_path = build_path_from_dentry(file->f_path.dentry);
371         if (full_path == NULL) {
372                 rc = -ENOMEM;
373                 goto out;
374         }
375
376         cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
377                  inode, file->f_flags, full_path);
378
379         if (enable_oplocks)
380                 oplock = REQ_OPLOCK;
381         else
382                 oplock = 0;
383
384         if (!tcon->broken_posix_open && tcon->unix_ext &&
385             (tcon->ses->capabilities & CAP_UNIX) &&
386             (CIFS_UNIX_POSIX_PATH_OPS_CAP &
387                         le64_to_cpu(tcon->fsUnixInfo.Capability))) {
388                 /* can not refresh inode info since size could be stale */
389                 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
390                                 cifs_sb->mnt_file_mode /* ignored */,
391                                 file->f_flags, &oplock, &netfid, xid);
392                 if (rc == 0) {
393                         cFYI(1, "posix open succeeded");
394                         posix_open_ok = true;
395                 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
396                         if (tcon->ses->serverNOS)
397                                 cERROR(1, "server %s of type %s returned"
398                                            " unexpected error on SMB posix open"
399                                            ", disabling posix open support."
400                                            " Check if server update available.",
401                                            tcon->ses->serverName,
402                                            tcon->ses->serverNOS);
403                         tcon->broken_posix_open = true;
404                 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
405                          (rc != -EOPNOTSUPP)) /* path not found or net err */
406                         goto out;
407                 /* else fallthrough to retry open the old way on network i/o
408                    or DFS errors */
409         }
410
411         if (!posix_open_ok) {
412                 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
413                                   file->f_flags, &oplock, &netfid, xid);
414                 if (rc)
415                         goto out;
416         }
417
418         pCifsFile = cifs_new_fileinfo(netfid, file, tlink, oplock);
419         if (pCifsFile == NULL) {
420                 CIFSSMBClose(xid, tcon, netfid);
421                 rc = -ENOMEM;
422                 goto out;
423         }
424
425         cifs_fscache_set_inode_cookie(inode, file);
426
427         if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
428                 /* time to set mode which we can not set earlier due to
429                    problems creating new read-only files */
430                 struct cifs_unix_set_info_args args = {
431                         .mode   = inode->i_mode,
432                         .uid    = NO_CHANGE_64,
433                         .gid    = NO_CHANGE_64,
434                         .ctime  = NO_CHANGE_64,
435                         .atime  = NO_CHANGE_64,
436                         .mtime  = NO_CHANGE_64,
437                         .device = 0,
438                 };
439                 CIFSSMBUnixSetFileInfo(xid, tcon, &args, netfid,
440                                         pCifsFile->pid);
441         }
442
443 out:
444         kfree(full_path);
445         FreeXid(xid);
446         cifs_put_tlink(tlink);
447         return rc;
448 }
449
450 /* Try to reacquire byte range locks that were released when session */
451 /* to server was lost */
452 static int cifs_relock_file(struct cifsFileInfo *cifsFile)
453 {
454         int rc = 0;
455
456 /* BB list all locks open on this file and relock */
457
458         return rc;
459 }
460
461 static int cifs_reopen_file(struct cifsFileInfo *pCifsFile, bool can_flush)
462 {
463         int rc = -EACCES;
464         int xid;
465         __u32 oplock;
466         struct cifs_sb_info *cifs_sb;
467         struct cifs_tcon *tcon;
468         struct cifsInodeInfo *pCifsInode;
469         struct inode *inode;
470         char *full_path = NULL;
471         int desiredAccess;
472         int disposition = FILE_OPEN;
473         int create_options = CREATE_NOT_DIR;
474         __u16 netfid;
475
476         xid = GetXid();
477         mutex_lock(&pCifsFile->fh_mutex);
478         if (!pCifsFile->invalidHandle) {
479                 mutex_unlock(&pCifsFile->fh_mutex);
480                 rc = 0;
481                 FreeXid(xid);
482                 return rc;
483         }
484
485         inode = pCifsFile->dentry->d_inode;
486         cifs_sb = CIFS_SB(inode->i_sb);
487         tcon = tlink_tcon(pCifsFile->tlink);
488
489 /* can not grab rename sem here because various ops, including
490    those that already have the rename sem can end up causing writepage
491    to get called and if the server was down that means we end up here,
492    and we can never tell if the caller already has the rename_sem */
493         full_path = build_path_from_dentry(pCifsFile->dentry);
494         if (full_path == NULL) {
495                 rc = -ENOMEM;
496                 mutex_unlock(&pCifsFile->fh_mutex);
497                 FreeXid(xid);
498                 return rc;
499         }
500
501         cFYI(1, "inode = 0x%p file flags 0x%x for %s",
502                  inode, pCifsFile->f_flags, full_path);
503
504         if (enable_oplocks)
505                 oplock = REQ_OPLOCK;
506         else
507                 oplock = 0;
508
509         if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
510             (CIFS_UNIX_POSIX_PATH_OPS_CAP &
511                         le64_to_cpu(tcon->fsUnixInfo.Capability))) {
512
513                 /*
514                  * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
515                  * original open. Must mask them off for a reopen.
516                  */
517                 unsigned int oflags = pCifsFile->f_flags &
518                                                 ~(O_CREAT | O_EXCL | O_TRUNC);
519
520                 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
521                                 cifs_sb->mnt_file_mode /* ignored */,
522                                 oflags, &oplock, &netfid, xid);
523                 if (rc == 0) {
524                         cFYI(1, "posix reopen succeeded");
525                         goto reopen_success;
526                 }
527                 /* fallthrough to retry open the old way on errors, especially
528                    in the reconnect path it is important to retry hard */
529         }
530
531         desiredAccess = cifs_convert_flags(pCifsFile->f_flags);
532
533         if (backup_cred(cifs_sb))
534                 create_options |= CREATE_OPEN_BACKUP_INTENT;
535
536         /* Can not refresh inode by passing in file_info buf to be returned
537            by SMBOpen and then calling get_inode_info with returned buf
538            since file might have write behind data that needs to be flushed
539            and server version of file size can be stale. If we knew for sure
540            that inode was not dirty locally we could do this */
541
542         rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
543                          create_options, &netfid, &oplock, NULL,
544                          cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
545                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
546         if (rc) {
547                 mutex_unlock(&pCifsFile->fh_mutex);
548                 cFYI(1, "cifs_open returned 0x%x", rc);
549                 cFYI(1, "oplock: %d", oplock);
550                 goto reopen_error_exit;
551         }
552
553 reopen_success:
554         pCifsFile->netfid = netfid;
555         pCifsFile->invalidHandle = false;
556         mutex_unlock(&pCifsFile->fh_mutex);
557         pCifsInode = CIFS_I(inode);
558
559         if (can_flush) {
560                 rc = filemap_write_and_wait(inode->i_mapping);
561                 mapping_set_error(inode->i_mapping, rc);
562
563                 if (tcon->unix_ext)
564                         rc = cifs_get_inode_info_unix(&inode,
565                                 full_path, inode->i_sb, xid);
566                 else
567                         rc = cifs_get_inode_info(&inode,
568                                 full_path, NULL, inode->i_sb,
569                                 xid, NULL);
570         } /* else we are writing out data to server already
571              and could deadlock if we tried to flush data, and
572              since we do not know if we have data that would
573              invalidate the current end of file on the server
574              we can not go to the server to get the new inod
575              info */
576
577         cifs_set_oplock_level(pCifsInode, oplock);
578
579         cifs_relock_file(pCifsFile);
580
581 reopen_error_exit:
582         kfree(full_path);
583         FreeXid(xid);
584         return rc;
585 }
586
587 int cifs_close(struct inode *inode, struct file *file)
588 {
589         if (file->private_data != NULL) {
590                 cifsFileInfo_put(file->private_data);
591                 file->private_data = NULL;
592         }
593
594         /* return code from the ->release op is always ignored */
595         return 0;
596 }
597
598 int cifs_closedir(struct inode *inode, struct file *file)
599 {
600         int rc = 0;
601         int xid;
602         struct cifsFileInfo *pCFileStruct = file->private_data;
603         char *ptmp;
604
605         cFYI(1, "Closedir inode = 0x%p", inode);
606
607         xid = GetXid();
608
609         if (pCFileStruct) {
610                 struct cifs_tcon *pTcon = tlink_tcon(pCFileStruct->tlink);
611
612                 cFYI(1, "Freeing private data in close dir");
613                 spin_lock(&cifs_file_list_lock);
614                 if (!pCFileStruct->srch_inf.endOfSearch &&
615                     !pCFileStruct->invalidHandle) {
616                         pCFileStruct->invalidHandle = true;
617                         spin_unlock(&cifs_file_list_lock);
618                         rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
619                         cFYI(1, "Closing uncompleted readdir with rc %d",
620                                  rc);
621                         /* not much we can do if it fails anyway, ignore rc */
622                         rc = 0;
623                 } else
624                         spin_unlock(&cifs_file_list_lock);
625                 ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
626                 if (ptmp) {
627                         cFYI(1, "closedir free smb buf in srch struct");
628                         pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
629                         if (pCFileStruct->srch_inf.smallBuf)
630                                 cifs_small_buf_release(ptmp);
631                         else
632                                 cifs_buf_release(ptmp);
633                 }
634                 cifs_put_tlink(pCFileStruct->tlink);
635                 kfree(file->private_data);
636                 file->private_data = NULL;
637         }
638         /* BB can we lock the filestruct while this is going on? */
639         FreeXid(xid);
640         return rc;
641 }
642
643 static int store_file_lock(struct cifsInodeInfo *cinode, __u64 len,
644                            __u64 offset, __u8 type, __u16 netfid)
645 {
646         struct cifsLockInfo *li =
647                 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
648         if (li == NULL)
649                 return -ENOMEM;
650         li->netfid = netfid;
651         li->offset = offset;
652         li->length = len;
653         li->type = type;
654         li->pid = current->tgid;
655         mutex_lock(&cinode->lock_mutex);
656         list_add_tail(&li->llist, &cinode->llist);
657         mutex_unlock(&cinode->lock_mutex);
658         return 0;
659 }
660
661 static void
662 cifs_read_flock(struct file_lock *flock, __u8 *type, int *lock, int *unlock,
663                 bool *wait_flag)
664 {
665         if (flock->fl_flags & FL_POSIX)
666                 cFYI(1, "Posix");
667         if (flock->fl_flags & FL_FLOCK)
668                 cFYI(1, "Flock");
669         if (flock->fl_flags & FL_SLEEP) {
670                 cFYI(1, "Blocking lock");
671                 *wait_flag = true;
672         }
673         if (flock->fl_flags & FL_ACCESS)
674                 cFYI(1, "Process suspended by mandatory locking - "
675                         "not implemented yet");
676         if (flock->fl_flags & FL_LEASE)
677                 cFYI(1, "Lease on file - not implemented yet");
678         if (flock->fl_flags &
679             (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
680                 cFYI(1, "Unknown lock flags 0x%x", flock->fl_flags);
681
682         *type = LOCKING_ANDX_LARGE_FILES;
683         if (flock->fl_type == F_WRLCK) {
684                 cFYI(1, "F_WRLCK ");
685                 *lock = 1;
686         } else if (flock->fl_type == F_UNLCK) {
687                 cFYI(1, "F_UNLCK");
688                 *unlock = 1;
689                 /* Check if unlock includes more than one lock range */
690         } else if (flock->fl_type == F_RDLCK) {
691                 cFYI(1, "F_RDLCK");
692                 *type |= LOCKING_ANDX_SHARED_LOCK;
693                 *lock = 1;
694         } else if (flock->fl_type == F_EXLCK) {
695                 cFYI(1, "F_EXLCK");
696                 *lock = 1;
697         } else if (flock->fl_type == F_SHLCK) {
698                 cFYI(1, "F_SHLCK");
699                 *type |= LOCKING_ANDX_SHARED_LOCK;
700                 *lock = 1;
701         } else
702                 cFYI(1, "Unknown type of lock");
703 }
704
705 static int
706 cifs_getlk(struct cifsFileInfo *cfile, struct file_lock *flock, __u8 type,
707            bool wait_flag, bool posix_lck, int xid)
708 {
709         int rc = 0;
710         __u64 length = 1 + flock->fl_end - flock->fl_start;
711         __u16 netfid = cfile->netfid;
712         struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
713
714         if (posix_lck) {
715                 int posix_lock_type;
716                 if (type & LOCKING_ANDX_SHARED_LOCK)
717                         posix_lock_type = CIFS_RDLCK;
718                 else
719                         posix_lock_type = CIFS_WRLCK;
720                 rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
721                                       length, flock, posix_lock_type,
722                                       wait_flag);
723                 return rc;
724         }
725
726         /* BB we could chain these into one lock request BB */
727         rc = CIFSSMBLock(xid, tcon, netfid, current->tgid, length,
728                          flock->fl_start, 0, 1, type, 0, 0);
729         if (rc == 0) {
730                 rc = CIFSSMBLock(xid, tcon, netfid, current->tgid,
731                                  length, flock->fl_start, 1, 0,
732                                  type, 0, 0);
733                 flock->fl_type = F_UNLCK;
734                 if (rc != 0)
735                         cERROR(1, "Error unlocking previously locked "
736                                    "range %d during test of lock", rc);
737                 rc = 0;
738                 return rc;
739         }
740
741         if (type & LOCKING_ANDX_SHARED_LOCK) {
742                 flock->fl_type = F_WRLCK;
743                 rc = 0;
744                 return rc;
745         }
746
747         rc = CIFSSMBLock(xid, tcon, netfid, current->tgid, length,
748                          flock->fl_start, 0, 1,
749                          type | LOCKING_ANDX_SHARED_LOCK, 0, 0);
750         if (rc == 0) {
751                 rc = CIFSSMBLock(xid, tcon, netfid, current->tgid,
752                                  length, flock->fl_start, 1, 0,
753                                  type | LOCKING_ANDX_SHARED_LOCK,
754                                  0, 0);
755                 flock->fl_type = F_RDLCK;
756                 if (rc != 0)
757                         cERROR(1, "Error unlocking previously locked "
758                                   "range %d during test of lock", rc);
759         } else
760                 flock->fl_type = F_WRLCK;
761
762         rc = 0;
763         return rc;
764 }
765
766 static int
767 cifs_setlk(struct file *file,  struct file_lock *flock, __u8 type,
768            bool wait_flag, bool posix_lck, int lock, int unlock, int xid)
769 {
770         int rc = 0;
771         __u64 length = 1 + flock->fl_end - flock->fl_start;
772         struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
773         struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
774         struct cifsInodeInfo *cinode = CIFS_I(file->f_path.dentry->d_inode);
775         __u16 netfid = cfile->netfid;
776
777         if (posix_lck) {
778                 int posix_lock_type;
779                 if (type & LOCKING_ANDX_SHARED_LOCK)
780                         posix_lock_type = CIFS_RDLCK;
781                 else
782                         posix_lock_type = CIFS_WRLCK;
783
784                 if (unlock == 1)
785                         posix_lock_type = CIFS_UNLCK;
786
787                 rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */, length,
788                                       flock, posix_lock_type, wait_flag);
789                 goto out;
790         }
791
792         if (lock) {
793                 rc = CIFSSMBLock(xid, tcon, netfid, current->tgid, length,
794                                  flock->fl_start, 0, lock, type, wait_flag, 0);
795                 if (rc == 0) {
796                         /* For Windows locks we must store them. */
797                         rc = store_file_lock(cinode, length, flock->fl_start,
798                                              type, netfid);
799                 }
800         } else if (unlock) {
801                 /*
802                  * For each stored lock that this unlock overlaps completely,
803                  * unlock it.
804                  */
805                 int stored_rc = 0;
806                 struct cifsLockInfo *li, *tmp;
807
808                 mutex_lock(&cinode->lock_mutex);
809                 list_for_each_entry_safe(li, tmp, &cinode->llist, llist) {
810                         if (flock->fl_start > li->offset ||
811                             (flock->fl_start + length) <
812                             (li->offset + li->length))
813                                 continue;
814                         if (current->tgid != li->pid)
815                                 continue;
816                         if (cfile->netfid != li->netfid)
817                                 continue;
818
819                         stored_rc = CIFSSMBLock(xid, tcon, netfid,
820                                                 current->tgid, li->length,
821                                                 li->offset, 1, 0, li->type,
822                                                 0, 0);
823                         if (stored_rc)
824                                 rc = stored_rc;
825                         else {
826                                 list_del(&li->llist);
827                                 kfree(li);
828                         }
829                 }
830                 mutex_unlock(&cinode->lock_mutex);
831         }
832 out:
833         if (flock->fl_flags & FL_POSIX)
834                 posix_lock_file_wait(file, flock);
835         return rc;
836 }
837
838 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
839 {
840         int rc, xid;
841         int lock = 0, unlock = 0;
842         bool wait_flag = false;
843         bool posix_lck = false;
844         struct cifs_sb_info *cifs_sb;
845         struct cifs_tcon *tcon;
846         struct cifsInodeInfo *cinode;
847         struct cifsFileInfo *cfile;
848         __u16 netfid;
849         __u8 type;
850
851         rc = -EACCES;
852         xid = GetXid();
853
854         cFYI(1, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld "
855                 "end: %lld", cmd, flock->fl_flags, flock->fl_type,
856                 flock->fl_start, flock->fl_end);
857
858         cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag);
859
860         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
861         cfile = (struct cifsFileInfo *)file->private_data;
862         tcon = tlink_tcon(cfile->tlink);
863         netfid = cfile->netfid;
864         cinode = CIFS_I(file->f_path.dentry->d_inode);
865
866         if ((tcon->ses->capabilities & CAP_UNIX) &&
867             (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
868             ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
869                 posix_lck = true;
870         /*
871          * BB add code here to normalize offset and length to account for
872          * negative length which we can not accept over the wire.
873          */
874         if (IS_GETLK(cmd)) {
875                 rc = cifs_getlk(cfile, flock, type, wait_flag, posix_lck, xid);
876                 FreeXid(xid);
877                 return rc;
878         }
879
880         if (!lock && !unlock) {
881                 /*
882                  * if no lock or unlock then nothing to do since we do not
883                  * know what it is
884                  */
885                 FreeXid(xid);
886                 return -EOPNOTSUPP;
887         }
888
889         rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
890                         xid);
891         FreeXid(xid);
892         return rc;
893 }
894
895 /* update the file size (if needed) after a write */
896 void
897 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
898                       unsigned int bytes_written)
899 {
900         loff_t end_of_write = offset + bytes_written;
901
902         if (end_of_write > cifsi->server_eof)
903                 cifsi->server_eof = end_of_write;
904 }
905
906 static ssize_t cifs_write(struct cifsFileInfo *open_file, __u32 pid,
907                           const char *write_data, size_t write_size,
908                           loff_t *poffset)
909 {
910         int rc = 0;
911         unsigned int bytes_written = 0;
912         unsigned int total_written;
913         struct cifs_sb_info *cifs_sb;
914         struct cifs_tcon *pTcon;
915         int xid;
916         struct dentry *dentry = open_file->dentry;
917         struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode);
918         struct cifs_io_parms io_parms;
919
920         cifs_sb = CIFS_SB(dentry->d_sb);
921
922         cFYI(1, "write %zd bytes to offset %lld of %s", write_size,
923            *poffset, dentry->d_name.name);
924
925         pTcon = tlink_tcon(open_file->tlink);
926
927         xid = GetXid();
928
929         for (total_written = 0; write_size > total_written;
930              total_written += bytes_written) {
931                 rc = -EAGAIN;
932                 while (rc == -EAGAIN) {
933                         struct kvec iov[2];
934                         unsigned int len;
935
936                         if (open_file->invalidHandle) {
937                                 /* we could deadlock if we called
938                                    filemap_fdatawait from here so tell
939                                    reopen_file not to flush data to
940                                    server now */
941                                 rc = cifs_reopen_file(open_file, false);
942                                 if (rc != 0)
943                                         break;
944                         }
945
946                         len = min((size_t)cifs_sb->wsize,
947                                   write_size - total_written);
948                         /* iov[0] is reserved for smb header */
949                         iov[1].iov_base = (char *)write_data + total_written;
950                         iov[1].iov_len = len;
951                         io_parms.netfid = open_file->netfid;
952                         io_parms.pid = pid;
953                         io_parms.tcon = pTcon;
954                         io_parms.offset = *poffset;
955                         io_parms.length = len;
956                         rc = CIFSSMBWrite2(xid, &io_parms, &bytes_written, iov,
957                                            1, 0);
958                 }
959                 if (rc || (bytes_written == 0)) {
960                         if (total_written)
961                                 break;
962                         else {
963                                 FreeXid(xid);
964                                 return rc;
965                         }
966                 } else {
967                         cifs_update_eof(cifsi, *poffset, bytes_written);
968                         *poffset += bytes_written;
969                 }
970         }
971
972         cifs_stats_bytes_written(pTcon, total_written);
973
974         if (total_written > 0) {
975                 spin_lock(&dentry->d_inode->i_lock);
976                 if (*poffset > dentry->d_inode->i_size)
977                         i_size_write(dentry->d_inode, *poffset);
978                 spin_unlock(&dentry->d_inode->i_lock);
979         }
980         mark_inode_dirty_sync(dentry->d_inode);
981         FreeXid(xid);
982         return total_written;
983 }
984
985 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
986                                         bool fsuid_only)
987 {
988         struct cifsFileInfo *open_file = NULL;
989         struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
990
991         /* only filter by fsuid on multiuser mounts */
992         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
993                 fsuid_only = false;
994
995         spin_lock(&cifs_file_list_lock);
996         /* we could simply get the first_list_entry since write-only entries
997            are always at the end of the list but since the first entry might
998            have a close pending, we go through the whole list */
999         list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1000                 if (fsuid_only && open_file->uid != current_fsuid())
1001                         continue;
1002                 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
1003                         if (!open_file->invalidHandle) {
1004                                 /* found a good file */
1005                                 /* lock it so it will not be closed on us */
1006                                 cifsFileInfo_get(open_file);
1007                                 spin_unlock(&cifs_file_list_lock);
1008                                 return open_file;
1009                         } /* else might as well continue, and look for
1010                              another, or simply have the caller reopen it
1011                              again rather than trying to fix this handle */
1012                 } else /* write only file */
1013                         break; /* write only files are last so must be done */
1014         }
1015         spin_unlock(&cifs_file_list_lock);
1016         return NULL;
1017 }
1018
1019 struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
1020                                         bool fsuid_only)
1021 {
1022         struct cifsFileInfo *open_file;
1023         struct cifs_sb_info *cifs_sb;
1024         bool any_available = false;
1025         int rc;
1026
1027         /* Having a null inode here (because mapping->host was set to zero by
1028         the VFS or MM) should not happen but we had reports of on oops (due to
1029         it being zero) during stress testcases so we need to check for it */
1030
1031         if (cifs_inode == NULL) {
1032                 cERROR(1, "Null inode passed to cifs_writeable_file");
1033                 dump_stack();
1034                 return NULL;
1035         }
1036
1037         cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
1038
1039         /* only filter by fsuid on multiuser mounts */
1040         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
1041                 fsuid_only = false;
1042
1043         spin_lock(&cifs_file_list_lock);
1044 refind_writable:
1045         list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
1046                 if (!any_available && open_file->pid != current->tgid)
1047                         continue;
1048                 if (fsuid_only && open_file->uid != current_fsuid())
1049                         continue;
1050                 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
1051                         cifsFileInfo_get(open_file);
1052
1053                         if (!open_file->invalidHandle) {
1054                                 /* found a good writable file */
1055                                 spin_unlock(&cifs_file_list_lock);
1056                                 return open_file;
1057                         }
1058
1059                         spin_unlock(&cifs_file_list_lock);
1060
1061                         /* Had to unlock since following call can block */
1062                         rc = cifs_reopen_file(open_file, false);
1063                         if (!rc)
1064                                 return open_file;
1065
1066                         /* if it fails, try another handle if possible */
1067                         cFYI(1, "wp failed on reopen file");
1068                         cifsFileInfo_put(open_file);
1069
1070                         spin_lock(&cifs_file_list_lock);
1071
1072                         /* else we simply continue to the next entry. Thus
1073                            we do not loop on reopen errors.  If we
1074                            can not reopen the file, for example if we
1075                            reconnected to a server with another client
1076                            racing to delete or lock the file we would not
1077                            make progress if we restarted before the beginning
1078                            of the loop here. */
1079                 }
1080         }
1081         /* couldn't find useable FH with same pid, try any available */
1082         if (!any_available) {
1083                 any_available = true;
1084                 goto refind_writable;
1085         }
1086         spin_unlock(&cifs_file_list_lock);
1087         return NULL;
1088 }
1089
1090 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
1091 {
1092         struct address_space *mapping = page->mapping;
1093         loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
1094         char *write_data;
1095         int rc = -EFAULT;
1096         int bytes_written = 0;
1097         struct inode *inode;
1098         struct cifsFileInfo *open_file;
1099
1100         if (!mapping || !mapping->host)
1101                 return -EFAULT;
1102
1103         inode = page->mapping->host;
1104
1105         offset += (loff_t)from;
1106         write_data = kmap(page);
1107         write_data += from;
1108
1109         if ((to > PAGE_CACHE_SIZE) || (from > to)) {
1110                 kunmap(page);
1111                 return -EIO;
1112         }
1113
1114         /* racing with truncate? */
1115         if (offset > mapping->host->i_size) {
1116                 kunmap(page);
1117                 return 0; /* don't care */
1118         }
1119
1120         /* check to make sure that we are not extending the file */
1121         if (mapping->host->i_size - offset < (loff_t)to)
1122                 to = (unsigned)(mapping->host->i_size - offset);
1123
1124         open_file = find_writable_file(CIFS_I(mapping->host), false);
1125         if (open_file) {
1126                 bytes_written = cifs_write(open_file, open_file->pid,
1127                                            write_data, to - from, &offset);
1128                 cifsFileInfo_put(open_file);
1129                 /* Does mm or vfs already set times? */
1130                 inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
1131                 if ((bytes_written > 0) && (offset))
1132                         rc = 0;
1133                 else if (bytes_written < 0)
1134                         rc = bytes_written;
1135         } else {
1136                 cFYI(1, "No writeable filehandles for inode");
1137                 rc = -EIO;
1138         }
1139
1140         kunmap(page);
1141         return rc;
1142 }
1143
1144 static int cifs_writepages(struct address_space *mapping,
1145                            struct writeback_control *wbc)
1146 {
1147         struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
1148         bool done = false, scanned = false, range_whole = false;
1149         pgoff_t end, index;
1150         struct cifs_writedata *wdata;
1151         struct page *page;
1152         int rc = 0;
1153
1154         /*
1155          * If wsize is smaller than the page cache size, default to writing
1156          * one page at a time via cifs_writepage
1157          */
1158         if (cifs_sb->wsize < PAGE_CACHE_SIZE)
1159                 return generic_writepages(mapping, wbc);
1160
1161         if (wbc->range_cyclic) {
1162                 index = mapping->writeback_index; /* Start from prev offset */
1163                 end = -1;
1164         } else {
1165                 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1166                 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1167                 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
1168                         range_whole = true;
1169                 scanned = true;
1170         }
1171 retry:
1172         while (!done && index <= end) {
1173                 unsigned int i, nr_pages, found_pages;
1174                 pgoff_t next = 0, tofind;
1175                 struct page **pages;
1176
1177                 tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1,
1178                                 end - index) + 1;
1179
1180                 wdata = cifs_writedata_alloc((unsigned int)tofind);
1181                 if (!wdata) {
1182                         rc = -ENOMEM;
1183                         break;
1184                 }
1185
1186                 /*
1187                  * find_get_pages_tag seems to return a max of 256 on each
1188                  * iteration, so we must call it several times in order to
1189                  * fill the array or the wsize is effectively limited to
1190                  * 256 * PAGE_CACHE_SIZE.
1191                  */
1192                 found_pages = 0;
1193                 pages = wdata->pages;
1194                 do {
1195                         nr_pages = find_get_pages_tag(mapping, &index,
1196                                                         PAGECACHE_TAG_DIRTY,
1197                                                         tofind, pages);
1198                         found_pages += nr_pages;
1199                         tofind -= nr_pages;
1200                         pages += nr_pages;
1201                 } while (nr_pages && tofind && index <= end);
1202
1203                 if (found_pages == 0) {
1204                         kref_put(&wdata->refcount, cifs_writedata_release);
1205                         break;
1206                 }
1207
1208                 nr_pages = 0;
1209                 for (i = 0; i < found_pages; i++) {
1210                         page = wdata->pages[i];
1211                         /*
1212                          * At this point we hold neither mapping->tree_lock nor
1213                          * lock on the page itself: the page may be truncated or
1214                          * invalidated (changing page->mapping to NULL), or even
1215                          * swizzled back from swapper_space to tmpfs file
1216                          * mapping
1217                          */
1218
1219                         if (nr_pages == 0)
1220                                 lock_page(page);
1221                         else if (!trylock_page(page))
1222                                 break;
1223
1224                         if (unlikely(page->mapping != mapping)) {
1225                                 unlock_page(page);
1226                                 break;
1227                         }
1228
1229                         if (!wbc->range_cyclic && page->index > end) {
1230                                 done = true;
1231                                 unlock_page(page);
1232                                 break;
1233                         }
1234
1235                         if (next && (page->index != next)) {
1236                                 /* Not next consecutive page */
1237                                 unlock_page(page);
1238                                 break;
1239                         }
1240
1241                         if (wbc->sync_mode != WB_SYNC_NONE)
1242                                 wait_on_page_writeback(page);
1243
1244                         if (PageWriteback(page) ||
1245                                         !clear_page_dirty_for_io(page)) {
1246                                 unlock_page(page);
1247                                 break;
1248                         }
1249
1250                         /*
1251                          * This actually clears the dirty bit in the radix tree.
1252                          * See cifs_writepage() for more commentary.
1253                          */
1254                         set_page_writeback(page);
1255
1256                         if (page_offset(page) >= mapping->host->i_size) {
1257                                 done = true;
1258                                 unlock_page(page);
1259                                 end_page_writeback(page);
1260                                 break;
1261                         }
1262
1263                         wdata->pages[i] = page;
1264                         next = page->index + 1;
1265                         ++nr_pages;
1266                 }
1267
1268                 /* reset index to refind any pages skipped */
1269                 if (nr_pages == 0)
1270                         index = wdata->pages[0]->index + 1;
1271
1272                 /* put any pages we aren't going to use */
1273                 for (i = nr_pages; i < found_pages; i++) {
1274                         page_cache_release(wdata->pages[i]);
1275                         wdata->pages[i] = NULL;
1276                 }
1277
1278                 /* nothing to write? */
1279                 if (nr_pages == 0) {
1280                         kref_put(&wdata->refcount, cifs_writedata_release);
1281                         continue;
1282                 }
1283
1284                 wdata->sync_mode = wbc->sync_mode;
1285                 wdata->nr_pages = nr_pages;
1286                 wdata->offset = page_offset(wdata->pages[0]);
1287
1288                 do {
1289                         if (wdata->cfile != NULL)
1290                                 cifsFileInfo_put(wdata->cfile);
1291                         wdata->cfile = find_writable_file(CIFS_I(mapping->host),
1292                                                           false);
1293                         if (!wdata->cfile) {
1294                                 cERROR(1, "No writable handles for inode");
1295                                 rc = -EBADF;
1296                                 break;
1297                         }
1298                         rc = cifs_async_writev(wdata);
1299                 } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN);
1300
1301                 for (i = 0; i < nr_pages; ++i)
1302                         unlock_page(wdata->pages[i]);
1303
1304                 /* send failure -- clean up the mess */
1305                 if (rc != 0) {
1306                         for (i = 0; i < nr_pages; ++i) {
1307                                 if (rc == -EAGAIN)
1308                                         redirty_page_for_writepage(wbc,
1309                                                            wdata->pages[i]);
1310                                 else
1311                                         SetPageError(wdata->pages[i]);
1312                                 end_page_writeback(wdata->pages[i]);
1313                                 page_cache_release(wdata->pages[i]);
1314                         }
1315                         if (rc != -EAGAIN)
1316                                 mapping_set_error(mapping, rc);
1317                 }
1318                 kref_put(&wdata->refcount, cifs_writedata_release);
1319
1320                 wbc->nr_to_write -= nr_pages;
1321                 if (wbc->nr_to_write <= 0)
1322                         done = true;
1323
1324                 index = next;
1325         }
1326
1327         if (!scanned && !done) {
1328                 /*
1329                  * We hit the last page and there is more work to be done: wrap
1330                  * back to the start of the file
1331                  */
1332                 scanned = true;
1333                 index = 0;
1334                 goto retry;
1335         }
1336
1337         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1338                 mapping->writeback_index = index;
1339
1340         return rc;
1341 }
1342
1343 static int
1344 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
1345 {
1346         int rc;
1347         int xid;
1348
1349         xid = GetXid();
1350 /* BB add check for wbc flags */
1351         page_cache_get(page);
1352         if (!PageUptodate(page))
1353                 cFYI(1, "ppw - page not up to date");
1354
1355         /*
1356          * Set the "writeback" flag, and clear "dirty" in the radix tree.
1357          *
1358          * A writepage() implementation always needs to do either this,
1359          * or re-dirty the page with "redirty_page_for_writepage()" in
1360          * the case of a failure.
1361          *
1362          * Just unlocking the page will cause the radix tree tag-bits
1363          * to fail to update with the state of the page correctly.
1364          */
1365         set_page_writeback(page);
1366 retry_write:
1367         rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
1368         if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL)
1369                 goto retry_write;
1370         else if (rc == -EAGAIN)
1371                 redirty_page_for_writepage(wbc, page);
1372         else if (rc != 0)
1373                 SetPageError(page);
1374         else
1375                 SetPageUptodate(page);
1376         end_page_writeback(page);
1377         page_cache_release(page);
1378         FreeXid(xid);
1379         return rc;
1380 }
1381
1382 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
1383 {
1384         int rc = cifs_writepage_locked(page, wbc);
1385         unlock_page(page);
1386         return rc;
1387 }
1388
1389 static int cifs_write_end(struct file *file, struct address_space *mapping,
1390                         loff_t pos, unsigned len, unsigned copied,
1391                         struct page *page, void *fsdata)
1392 {
1393         int rc;
1394         struct inode *inode = mapping->host;
1395         struct cifsFileInfo *cfile = file->private_data;
1396         struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1397         __u32 pid;
1398
1399         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
1400                 pid = cfile->pid;
1401         else
1402                 pid = current->tgid;
1403
1404         cFYI(1, "write_end for page %p from pos %lld with %d bytes",
1405                  page, pos, copied);
1406
1407         if (PageChecked(page)) {
1408                 if (copied == len)
1409                         SetPageUptodate(page);
1410                 ClearPageChecked(page);
1411         } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
1412                 SetPageUptodate(page);
1413
1414         if (!PageUptodate(page)) {
1415                 char *page_data;
1416                 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1417                 int xid;
1418
1419                 xid = GetXid();
1420                 /* this is probably better than directly calling
1421                    partialpage_write since in this function the file handle is
1422                    known which we might as well leverage */
1423                 /* BB check if anything else missing out of ppw
1424                    such as updating last write time */
1425                 page_data = kmap(page);
1426                 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
1427                 /* if (rc < 0) should we set writebehind rc? */
1428                 kunmap(page);
1429
1430                 FreeXid(xid);
1431         } else {
1432                 rc = copied;
1433                 pos += copied;
1434                 set_page_dirty(page);
1435         }
1436
1437         if (rc > 0) {
1438                 spin_lock(&inode->i_lock);
1439                 if (pos > inode->i_size)
1440                         i_size_write(inode, pos);
1441                 spin_unlock(&inode->i_lock);
1442         }
1443
1444         unlock_page(page);
1445         page_cache_release(page);
1446
1447         return rc;
1448 }
1449
1450 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
1451                       int datasync)
1452 {
1453         int xid;
1454         int rc = 0;
1455         struct cifs_tcon *tcon;
1456         struct cifsFileInfo *smbfile = file->private_data;
1457         struct inode *inode = file->f_path.dentry->d_inode;
1458         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1459
1460         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
1461         if (rc)
1462                 return rc;
1463         mutex_lock(&inode->i_mutex);
1464
1465         xid = GetXid();
1466
1467         cFYI(1, "Sync file - name: %s datasync: 0x%x",
1468                 file->f_path.dentry->d_name.name, datasync);
1469
1470         if (!CIFS_I(inode)->clientCanCacheRead) {
1471                 rc = cifs_invalidate_mapping(inode);
1472                 if (rc) {
1473                         cFYI(1, "rc: %d during invalidate phase", rc);
1474                         rc = 0; /* don't care about it in fsync */
1475                 }
1476         }
1477
1478         tcon = tlink_tcon(smbfile->tlink);
1479         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
1480                 rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
1481
1482         FreeXid(xid);
1483         mutex_unlock(&inode->i_mutex);
1484         return rc;
1485 }
1486
1487 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1488 {
1489         int xid;
1490         int rc = 0;
1491         struct cifs_tcon *tcon;
1492         struct cifsFileInfo *smbfile = file->private_data;
1493         struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1494         struct inode *inode = file->f_mapping->host;
1495
1496         rc = filemap_write_and_wait_range(inode->i_mapping, start, end);
1497         if (rc)
1498                 return rc;
1499         mutex_lock(&inode->i_mutex);
1500
1501         xid = GetXid();
1502
1503         cFYI(1, "Sync file - name: %s datasync: 0x%x",
1504                 file->f_path.dentry->d_name.name, datasync);
1505
1506         tcon = tlink_tcon(smbfile->tlink);
1507         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
1508                 rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
1509
1510         FreeXid(xid);
1511         mutex_unlock(&inode->i_mutex);
1512         return rc;
1513 }
1514
1515 /*
1516  * As file closes, flush all cached write data for this inode checking
1517  * for write behind errors.
1518  */
1519 int cifs_flush(struct file *file, fl_owner_t id)
1520 {
1521         struct inode *inode = file->f_path.dentry->d_inode;
1522         int rc = 0;
1523
1524         if (file->f_mode & FMODE_WRITE)
1525                 rc = filemap_write_and_wait(inode->i_mapping);
1526
1527         cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc);
1528
1529         return rc;
1530 }
1531
1532 static int
1533 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
1534 {
1535         int rc = 0;
1536         unsigned long i;
1537
1538         for (i = 0; i < num_pages; i++) {
1539                 pages[i] = alloc_page(__GFP_HIGHMEM);
1540                 if (!pages[i]) {
1541                         /*
1542                          * save number of pages we have already allocated and
1543                          * return with ENOMEM error
1544                          */
1545                         num_pages = i;
1546                         rc = -ENOMEM;
1547                         goto error;
1548                 }
1549         }
1550
1551         return rc;
1552
1553 error:
1554         for (i = 0; i < num_pages; i++)
1555                 put_page(pages[i]);
1556         return rc;
1557 }
1558
1559 static inline
1560 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
1561 {
1562         size_t num_pages;
1563         size_t clen;
1564
1565         clen = min_t(const size_t, len, wsize);
1566         num_pages = clen / PAGE_CACHE_SIZE;
1567         if (clen % PAGE_CACHE_SIZE)
1568                 num_pages++;
1569
1570         if (cur_len)
1571                 *cur_len = clen;
1572
1573         return num_pages;
1574 }
1575
1576 static ssize_t
1577 cifs_iovec_write(struct file *file, const struct iovec *iov,
1578                  unsigned long nr_segs, loff_t *poffset)
1579 {
1580         unsigned int written;
1581         unsigned long num_pages, npages, i;
1582         size_t copied, len, cur_len;
1583         ssize_t total_written = 0;
1584         struct kvec *to_send;
1585         struct page **pages;
1586         struct iov_iter it;
1587         struct inode *inode;
1588         struct cifsFileInfo *open_file;
1589         struct cifs_tcon *pTcon;
1590         struct cifs_sb_info *cifs_sb;
1591         struct cifs_io_parms io_parms;
1592         int xid, rc;
1593         __u32 pid;
1594
1595         len = iov_length(iov, nr_segs);
1596         if (!len)
1597                 return 0;
1598
1599         rc = generic_write_checks(file, poffset, &len, 0);
1600         if (rc)
1601                 return rc;
1602
1603         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1604         num_pages = get_numpages(cifs_sb->wsize, len, &cur_len);
1605
1606         pages = kmalloc(sizeof(struct pages *)*num_pages, GFP_KERNEL);
1607         if (!pages)
1608                 return -ENOMEM;
1609
1610         to_send = kmalloc(sizeof(struct kvec)*(num_pages + 1), GFP_KERNEL);
1611         if (!to_send) {
1612                 kfree(pages);
1613                 return -ENOMEM;
1614         }
1615
1616         rc = cifs_write_allocate_pages(pages, num_pages);
1617         if (rc) {
1618                 kfree(pages);
1619                 kfree(to_send);
1620                 return rc;
1621         }
1622
1623         xid = GetXid();
1624         open_file = file->private_data;
1625
1626         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
1627                 pid = open_file->pid;
1628         else
1629                 pid = current->tgid;
1630
1631         pTcon = tlink_tcon(open_file->tlink);
1632         inode = file->f_path.dentry->d_inode;
1633
1634         iov_iter_init(&it, iov, nr_segs, len, 0);
1635         npages = num_pages;
1636
1637         do {
1638                 size_t save_len = cur_len;
1639                 for (i = 0; i < npages; i++) {
1640                         copied = min_t(const size_t, cur_len, PAGE_CACHE_SIZE);
1641                         copied = iov_iter_copy_from_user(pages[i], &it, 0,
1642                                                          copied);
1643                         cur_len -= copied;
1644                         iov_iter_advance(&it, copied);
1645                         to_send[i+1].iov_base = kmap(pages[i]);
1646                         to_send[i+1].iov_len = copied;
1647                 }
1648
1649                 cur_len = save_len - cur_len;
1650
1651                 do {
1652                         if (open_file->invalidHandle) {
1653                                 rc = cifs_reopen_file(open_file, false);
1654                                 if (rc != 0)
1655                                         break;
1656                         }
1657                         io_parms.netfid = open_file->netfid;
1658                         io_parms.pid = pid;
1659                         io_parms.tcon = pTcon;
1660                         io_parms.offset = *poffset;
1661                         io_parms.length = cur_len;
1662                         rc = CIFSSMBWrite2(xid, &io_parms, &written, to_send,
1663                                            npages, 0);
1664                 } while (rc == -EAGAIN);
1665
1666                 for (i = 0; i < npages; i++)
1667                         kunmap(pages[i]);
1668
1669                 if (written) {
1670                         len -= written;
1671                         total_written += written;
1672                         cifs_update_eof(CIFS_I(inode), *poffset, written);
1673                         *poffset += written;
1674                 } else if (rc < 0) {
1675                         if (!total_written)
1676                                 total_written = rc;
1677                         break;
1678                 }
1679
1680                 /* get length and number of kvecs of the next write */
1681                 npages = get_numpages(cifs_sb->wsize, len, &cur_len);
1682         } while (len > 0);
1683
1684         if (total_written > 0) {
1685                 spin_lock(&inode->i_lock);
1686                 if (*poffset > inode->i_size)
1687                         i_size_write(inode, *poffset);
1688                 spin_unlock(&inode->i_lock);
1689         }
1690
1691         cifs_stats_bytes_written(pTcon, total_written);
1692         mark_inode_dirty_sync(inode);
1693
1694         for (i = 0; i < num_pages; i++)
1695                 put_page(pages[i]);
1696         kfree(to_send);
1697         kfree(pages);
1698         FreeXid(xid);
1699         return total_written;
1700 }
1701
1702 ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov,
1703                                 unsigned long nr_segs, loff_t pos)
1704 {
1705         ssize_t written;
1706         struct inode *inode;
1707
1708         inode = iocb->ki_filp->f_path.dentry->d_inode;
1709
1710         /*
1711          * BB - optimize the way when signing is disabled. We can drop this
1712          * extra memory-to-memory copying and use iovec buffers for constructing
1713          * write request.
1714          */
1715
1716         written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos);
1717         if (written > 0) {
1718                 CIFS_I(inode)->invalid_mapping = true;
1719                 iocb->ki_pos = pos;
1720         }
1721
1722         return written;
1723 }
1724
1725 ssize_t cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
1726                            unsigned long nr_segs, loff_t pos)
1727 {
1728         struct inode *inode;
1729
1730         inode = iocb->ki_filp->f_path.dentry->d_inode;
1731
1732         if (CIFS_I(inode)->clientCanCacheAll)
1733                 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1734
1735         /*
1736          * In strict cache mode we need to write the data to the server exactly
1737          * from the pos to pos+len-1 rather than flush all affected pages
1738          * because it may cause a error with mandatory locks on these pages but
1739          * not on the region from pos to ppos+len-1.
1740          */
1741
1742         return cifs_user_writev(iocb, iov, nr_segs, pos);
1743 }
1744
1745 static ssize_t
1746 cifs_iovec_read(struct file *file, const struct iovec *iov,
1747                  unsigned long nr_segs, loff_t *poffset)
1748 {
1749         int rc;
1750         int xid;
1751         ssize_t total_read;
1752         unsigned int bytes_read = 0;
1753         size_t len, cur_len;
1754         int iov_offset = 0;
1755         struct cifs_sb_info *cifs_sb;
1756         struct cifs_tcon *pTcon;
1757         struct cifsFileInfo *open_file;
1758         struct smb_com_read_rsp *pSMBr;
1759         struct cifs_io_parms io_parms;
1760         char *read_data;
1761         unsigned int rsize;
1762         __u32 pid;
1763
1764         if (!nr_segs)
1765                 return 0;
1766
1767         len = iov_length(iov, nr_segs);
1768         if (!len)
1769                 return 0;
1770
1771         xid = GetXid();
1772         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1773
1774         /* FIXME: set up handlers for larger reads and/or convert to async */
1775         rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
1776
1777         open_file = file->private_data;
1778         pTcon = tlink_tcon(open_file->tlink);
1779
1780         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
1781                 pid = open_file->pid;
1782         else
1783                 pid = current->tgid;
1784
1785         if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1786                 cFYI(1, "attempting read on write only file instance");
1787
1788         for (total_read = 0; total_read < len; total_read += bytes_read) {
1789                 cur_len = min_t(const size_t, len - total_read, rsize);
1790                 rc = -EAGAIN;
1791                 read_data = NULL;
1792
1793                 while (rc == -EAGAIN) {
1794                         int buf_type = CIFS_NO_BUFFER;
1795                         if (open_file->invalidHandle) {
1796                                 rc = cifs_reopen_file(open_file, true);
1797                                 if (rc != 0)
1798                                         break;
1799                         }
1800                         io_parms.netfid = open_file->netfid;
1801                         io_parms.pid = pid;
1802                         io_parms.tcon = pTcon;
1803                         io_parms.offset = *poffset;
1804                         io_parms.length = cur_len;
1805                         rc = CIFSSMBRead(xid, &io_parms, &bytes_read,
1806                                          &read_data, &buf_type);
1807                         pSMBr = (struct smb_com_read_rsp *)read_data;
1808                         if (read_data) {
1809                                 char *data_offset = read_data + 4 +
1810                                                 le16_to_cpu(pSMBr->DataOffset);
1811                                 if (memcpy_toiovecend(iov, data_offset,
1812                                                       iov_offset, bytes_read))
1813                                         rc = -EFAULT;
1814                                 if (buf_type == CIFS_SMALL_BUFFER)
1815                                         cifs_small_buf_release(read_data);
1816                                 else if (buf_type == CIFS_LARGE_BUFFER)
1817                                         cifs_buf_release(read_data);
1818                                 read_data = NULL;
1819                                 iov_offset += bytes_read;
1820                         }
1821                 }
1822
1823                 if (rc || (bytes_read == 0)) {
1824                         if (total_read) {
1825                                 break;
1826                         } else {
1827                                 FreeXid(xid);
1828                                 return rc;
1829                         }
1830                 } else {
1831                         cifs_stats_bytes_read(pTcon, bytes_read);
1832                         *poffset += bytes_read;
1833                 }
1834         }
1835
1836         FreeXid(xid);
1837         return total_read;
1838 }
1839
1840 ssize_t cifs_user_readv(struct kiocb *iocb, const struct iovec *iov,
1841                                unsigned long nr_segs, loff_t pos)
1842 {
1843         ssize_t read;
1844
1845         read = cifs_iovec_read(iocb->ki_filp, iov, nr_segs, &pos);
1846         if (read > 0)
1847                 iocb->ki_pos = pos;
1848
1849         return read;
1850 }
1851
1852 ssize_t cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,
1853                           unsigned long nr_segs, loff_t pos)
1854 {
1855         struct inode *inode;
1856
1857         inode = iocb->ki_filp->f_path.dentry->d_inode;
1858
1859         if (CIFS_I(inode)->clientCanCacheRead)
1860                 return generic_file_aio_read(iocb, iov, nr_segs, pos);
1861
1862         /*
1863          * In strict cache mode we need to read from the server all the time
1864          * if we don't have level II oplock because the server can delay mtime
1865          * change - so we can't make a decision about inode invalidating.
1866          * And we can also fail with pagereading if there are mandatory locks
1867          * on pages affected by this read but not on the region from pos to
1868          * pos+len-1.
1869          */
1870
1871         return cifs_user_readv(iocb, iov, nr_segs, pos);
1872 }
1873
1874 static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
1875                          loff_t *poffset)
1876 {
1877         int rc = -EACCES;
1878         unsigned int bytes_read = 0;
1879         unsigned int total_read;
1880         unsigned int current_read_size;
1881         unsigned int rsize;
1882         struct cifs_sb_info *cifs_sb;
1883         struct cifs_tcon *pTcon;
1884         int xid;
1885         char *current_offset;
1886         struct cifsFileInfo *open_file;
1887         struct cifs_io_parms io_parms;
1888         int buf_type = CIFS_NO_BUFFER;
1889         __u32 pid;
1890
1891         xid = GetXid();
1892         cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
1893
1894         /* FIXME: set up handlers for larger reads and/or convert to async */
1895         rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
1896
1897         if (file->private_data == NULL) {
1898                 rc = -EBADF;
1899                 FreeXid(xid);
1900                 return rc;
1901         }
1902         open_file = file->private_data;
1903         pTcon = tlink_tcon(open_file->tlink);
1904
1905         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
1906                 pid = open_file->pid;
1907         else
1908                 pid = current->tgid;
1909
1910         if ((file->f_flags & O_ACCMODE) == O_WRONLY)
1911                 cFYI(1, "attempting read on write only file instance");
1912
1913         for (total_read = 0, current_offset = read_data;
1914              read_size > total_read;
1915              total_read += bytes_read, current_offset += bytes_read) {
1916                 current_read_size = min_t(uint, read_size - total_read, rsize);
1917
1918                 /* For windows me and 9x we do not want to request more
1919                 than it negotiated since it will refuse the read then */
1920                 if ((pTcon->ses) &&
1921                         !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
1922                         current_read_size = min_t(uint, current_read_size,
1923                                         CIFSMaxBufSize);
1924                 }
1925                 rc = -EAGAIN;
1926                 while (rc == -EAGAIN) {
1927                         if (open_file->invalidHandle) {
1928                                 rc = cifs_reopen_file(open_file, true);
1929                                 if (rc != 0)
1930                                         break;
1931                         }
1932                         io_parms.netfid = open_file->netfid;
1933                         io_parms.pid = pid;
1934                         io_parms.tcon = pTcon;
1935                         io_parms.offset = *poffset;
1936                         io_parms.length = current_read_size;
1937                         rc = CIFSSMBRead(xid, &io_parms, &bytes_read,
1938                                          &current_offset, &buf_type);
1939                 }
1940                 if (rc || (bytes_read == 0)) {
1941                         if (total_read) {
1942                                 break;
1943                         } else {
1944                                 FreeXid(xid);
1945                                 return rc;
1946                         }
1947                 } else {
1948                         cifs_stats_bytes_read(pTcon, total_read);
1949                         *poffset += bytes_read;
1950                 }
1951         }
1952         FreeXid(xid);
1953         return total_read;
1954 }
1955
1956 /*
1957  * If the page is mmap'ed into a process' page tables, then we need to make
1958  * sure that it doesn't change while being written back.
1959  */
1960 static int
1961 cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1962 {
1963         struct page *page = vmf->page;
1964
1965         lock_page(page);
1966         return VM_FAULT_LOCKED;
1967 }
1968
1969 static struct vm_operations_struct cifs_file_vm_ops = {
1970         .fault = filemap_fault,
1971         .page_mkwrite = cifs_page_mkwrite,
1972 };
1973
1974 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
1975 {
1976         int rc, xid;
1977         struct inode *inode = file->f_path.dentry->d_inode;
1978
1979         xid = GetXid();
1980
1981         if (!CIFS_I(inode)->clientCanCacheRead) {
1982                 rc = cifs_invalidate_mapping(inode);
1983                 if (rc)
1984                         return rc;
1985         }
1986
1987         rc = generic_file_mmap(file, vma);
1988         if (rc == 0)
1989                 vma->vm_ops = &cifs_file_vm_ops;
1990         FreeXid(xid);
1991         return rc;
1992 }
1993
1994 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
1995 {
1996         int rc, xid;
1997
1998         xid = GetXid();
1999         rc = cifs_revalidate_file(file);
2000         if (rc) {
2001                 cFYI(1, "Validation prior to mmap failed, error=%d", rc);
2002                 FreeXid(xid);
2003                 return rc;
2004         }
2005         rc = generic_file_mmap(file, vma);
2006         if (rc == 0)
2007                 vma->vm_ops = &cifs_file_vm_ops;
2008         FreeXid(xid);
2009         return rc;
2010 }
2011
2012 static int cifs_readpages(struct file *file, struct address_space *mapping,
2013         struct list_head *page_list, unsigned num_pages)
2014 {
2015         int rc;
2016         struct list_head tmplist;
2017         struct cifsFileInfo *open_file = file->private_data;
2018         struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
2019         unsigned int rsize = cifs_sb->rsize;
2020         pid_t pid;
2021
2022         /*
2023          * Give up immediately if rsize is too small to read an entire page.
2024          * The VFS will fall back to readpage. We should never reach this
2025          * point however since we set ra_pages to 0 when the rsize is smaller
2026          * than a cache page.
2027          */
2028         if (unlikely(rsize < PAGE_CACHE_SIZE))
2029                 return 0;
2030
2031         /*
2032          * Reads as many pages as possible from fscache. Returns -ENOBUFS
2033          * immediately if the cookie is negative
2034          */
2035         rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
2036                                          &num_pages);
2037         if (rc == 0)
2038                 return rc;
2039
2040         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2041                 pid = open_file->pid;
2042         else
2043                 pid = current->tgid;
2044
2045         rc = 0;
2046         INIT_LIST_HEAD(&tmplist);
2047
2048         cFYI(1, "%s: file=%p mapping=%p num_pages=%u", __func__, file,
2049                 mapping, num_pages);
2050
2051         /*
2052          * Start with the page at end of list and move it to private
2053          * list. Do the same with any following pages until we hit
2054          * the rsize limit, hit an index discontinuity, or run out of
2055          * pages. Issue the async read and then start the loop again
2056          * until the list is empty.
2057          *
2058          * Note that list order is important. The page_list is in
2059          * the order of declining indexes. When we put the pages in
2060          * the rdata->pages, then we want them in increasing order.
2061          */
2062         while (!list_empty(page_list)) {
2063                 unsigned int bytes = PAGE_CACHE_SIZE;
2064                 unsigned int expected_index;
2065                 unsigned int nr_pages = 1;
2066                 loff_t offset;
2067                 struct page *page, *tpage;
2068                 struct cifs_readdata *rdata;
2069
2070                 page = list_entry(page_list->prev, struct page, lru);
2071
2072                 /*
2073                  * Lock the page and put it in the cache. Since no one else
2074                  * should have access to this page, we're safe to simply set
2075                  * PG_locked without checking it first.
2076                  */
2077                 __set_page_locked(page);
2078                 rc = add_to_page_cache_locked(page, mapping,
2079                                               page->index, GFP_KERNEL);
2080
2081                 /* give up if we can't stick it in the cache */
2082                 if (rc) {
2083                         __clear_page_locked(page);
2084                         break;
2085                 }
2086
2087                 /* move first page to the tmplist */
2088                 offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
2089                 list_move_tail(&page->lru, &tmplist);
2090
2091                 /* now try and add more pages onto the request */
2092                 expected_index = page->index + 1;
2093                 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
2094                         /* discontinuity ? */
2095                         if (page->index != expected_index)
2096                                 break;
2097
2098                         /* would this page push the read over the rsize? */
2099                         if (bytes + PAGE_CACHE_SIZE > rsize)
2100                                 break;
2101
2102                         __set_page_locked(page);
2103                         if (add_to_page_cache_locked(page, mapping,
2104                                                 page->index, GFP_KERNEL)) {
2105                                 __clear_page_locked(page);
2106                                 break;
2107                         }
2108                         list_move_tail(&page->lru, &tmplist);
2109                         bytes += PAGE_CACHE_SIZE;
2110                         expected_index++;
2111                         nr_pages++;
2112                 }
2113
2114                 rdata = cifs_readdata_alloc(nr_pages);
2115                 if (!rdata) {
2116                         /* best to give up if we're out of mem */
2117                         list_for_each_entry_safe(page, tpage, &tmplist, lru) {
2118                                 list_del(&page->lru);
2119                                 lru_cache_add_file(page);
2120                                 unlock_page(page);
2121                                 page_cache_release(page);
2122                         }
2123                         rc = -ENOMEM;
2124                         break;
2125                 }
2126
2127                 spin_lock(&cifs_file_list_lock);
2128                 cifsFileInfo_get(open_file);
2129                 spin_unlock(&cifs_file_list_lock);
2130                 rdata->cfile = open_file;
2131                 rdata->mapping = mapping;
2132                 rdata->offset = offset;
2133                 rdata->bytes = bytes;
2134                 rdata->pid = pid;
2135                 list_splice_init(&tmplist, &rdata->pages);
2136
2137                 do {
2138                         if (open_file->invalidHandle) {
2139                                 rc = cifs_reopen_file(open_file, true);
2140                                 if (rc != 0)
2141                                         continue;
2142                         }
2143                         rc = cifs_async_readv(rdata);
2144                 } while (rc == -EAGAIN);
2145
2146                 if (rc != 0) {
2147                         list_for_each_entry_safe(page, tpage, &rdata->pages,
2148                                                  lru) {
2149                                 list_del(&page->lru);
2150                                 lru_cache_add_file(page);
2151                                 unlock_page(page);
2152                                 page_cache_release(page);
2153                         }
2154                         cifs_readdata_free(rdata);
2155                         break;
2156                 }
2157         }
2158
2159         return rc;
2160 }
2161
2162 static int cifs_readpage_worker(struct file *file, struct page *page,
2163         loff_t *poffset)
2164 {
2165         char *read_data;
2166         int rc;
2167
2168         /* Is the page cached? */
2169         rc = cifs_readpage_from_fscache(file->f_path.dentry->d_inode, page);
2170         if (rc == 0)
2171                 goto read_complete;
2172
2173         page_cache_get(page);
2174         read_data = kmap(page);
2175         /* for reads over a certain size could initiate async read ahead */
2176
2177         rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
2178
2179         if (rc < 0)
2180                 goto io_error;
2181         else
2182                 cFYI(1, "Bytes read %d", rc);
2183
2184         file->f_path.dentry->d_inode->i_atime =
2185                 current_fs_time(file->f_path.dentry->d_inode->i_sb);
2186
2187         if (PAGE_CACHE_SIZE > rc)
2188                 memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
2189
2190         flush_dcache_page(page);
2191         SetPageUptodate(page);
2192
2193         /* send this page to the cache */
2194         cifs_readpage_to_fscache(file->f_path.dentry->d_inode, page);
2195
2196         rc = 0;
2197
2198 io_error:
2199         kunmap(page);
2200         page_cache_release(page);
2201
2202 read_complete:
2203         return rc;
2204 }
2205
2206 static int cifs_readpage(struct file *file, struct page *page)
2207 {
2208         loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
2209         int rc = -EACCES;
2210         int xid;
2211
2212         xid = GetXid();
2213
2214         if (file->private_data == NULL) {
2215                 rc = -EBADF;
2216                 FreeXid(xid);
2217                 return rc;
2218         }
2219
2220         cFYI(1, "readpage %p at offset %d 0x%x\n",
2221                  page, (int)offset, (int)offset);
2222
2223         rc = cifs_readpage_worker(file, page, &offset);
2224
2225         unlock_page(page);
2226
2227         FreeXid(xid);
2228         return rc;
2229 }
2230
2231 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
2232 {
2233         struct cifsFileInfo *open_file;
2234
2235         spin_lock(&cifs_file_list_lock);
2236         list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2237                 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2238                         spin_unlock(&cifs_file_list_lock);
2239                         return 1;
2240                 }
2241         }
2242         spin_unlock(&cifs_file_list_lock);
2243         return 0;
2244 }
2245
2246 /* We do not want to update the file size from server for inodes
2247    open for write - to avoid races with writepage extending
2248    the file - in the future we could consider allowing
2249    refreshing the inode only on increases in the file size
2250    but this is tricky to do without racing with writebehind
2251    page caching in the current Linux kernel design */
2252 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
2253 {
2254         if (!cifsInode)
2255                 return true;
2256
2257         if (is_inode_writable(cifsInode)) {
2258                 /* This inode is open for write at least once */
2259                 struct cifs_sb_info *cifs_sb;
2260
2261                 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
2262                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
2263                         /* since no page cache to corrupt on directio
2264                         we can change size safely */
2265                         return true;
2266                 }
2267
2268                 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
2269                         return true;
2270
2271                 return false;
2272         } else
2273                 return true;
2274 }
2275
2276 static int cifs_write_begin(struct file *file, struct address_space *mapping,
2277                         loff_t pos, unsigned len, unsigned flags,
2278                         struct page **pagep, void **fsdata)
2279 {
2280         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2281         loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
2282         loff_t page_start = pos & PAGE_MASK;
2283         loff_t i_size;
2284         struct page *page;
2285         int rc = 0;
2286
2287         cFYI(1, "write_begin from %lld len %d", (long long)pos, len);
2288
2289         page = grab_cache_page_write_begin(mapping, index, flags);
2290         if (!page) {
2291                 rc = -ENOMEM;
2292                 goto out;
2293         }
2294
2295         if (PageUptodate(page))
2296                 goto out;
2297
2298         /*
2299          * If we write a full page it will be up to date, no need to read from
2300          * the server. If the write is short, we'll end up doing a sync write
2301          * instead.
2302          */
2303         if (len == PAGE_CACHE_SIZE)
2304                 goto out;
2305
2306         /*
2307          * optimize away the read when we have an oplock, and we're not
2308          * expecting to use any of the data we'd be reading in. That
2309          * is, when the page lies beyond the EOF, or straddles the EOF
2310          * and the write will cover all of the existing data.
2311          */
2312         if (CIFS_I(mapping->host)->clientCanCacheRead) {
2313                 i_size = i_size_read(mapping->host);
2314                 if (page_start >= i_size ||
2315                     (offset == 0 && (pos + len) >= i_size)) {
2316                         zero_user_segments(page, 0, offset,
2317                                            offset + len,
2318                                            PAGE_CACHE_SIZE);
2319                         /*
2320                          * PageChecked means that the parts of the page
2321                          * to which we're not writing are considered up
2322                          * to date. Once the data is copied to the
2323                          * page, it can be set uptodate.
2324                          */
2325                         SetPageChecked(page);
2326                         goto out;
2327                 }
2328         }
2329
2330         if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
2331                 /*
2332                  * might as well read a page, it is fast enough. If we get
2333                  * an error, we don't need to return it. cifs_write_end will
2334                  * do a sync write instead since PG_uptodate isn't set.
2335                  */
2336                 cifs_readpage_worker(file, page, &page_start);
2337         } else {
2338                 /* we could try using another file handle if there is one -
2339                    but how would we lock it to prevent close of that handle
2340                    racing with this read? In any case
2341                    this will be written out by write_end so is fine */
2342         }
2343 out:
2344         *pagep = page;
2345         return rc;
2346 }
2347
2348 static int cifs_release_page(struct page *page, gfp_t gfp)
2349 {
2350         if (PagePrivate(page))
2351                 return 0;
2352
2353         return cifs_fscache_release_page(page, gfp);
2354 }
2355
2356 static void cifs_invalidate_page(struct page *page, unsigned long offset)
2357 {
2358         struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
2359
2360         if (offset == 0)
2361                 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
2362 }
2363
2364 static int cifs_launder_page(struct page *page)
2365 {
2366         int rc = 0;
2367         loff_t range_start = page_offset(page);
2368         loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
2369         struct writeback_control wbc = {
2370                 .sync_mode = WB_SYNC_ALL,
2371                 .nr_to_write = 0,
2372                 .range_start = range_start,
2373                 .range_end = range_end,
2374         };
2375
2376         cFYI(1, "Launder page: %p", page);
2377
2378         if (clear_page_dirty_for_io(page))
2379                 rc = cifs_writepage_locked(page, &wbc);
2380
2381         cifs_fscache_invalidate_page(page, page->mapping->host);
2382         return rc;
2383 }
2384
2385 void cifs_oplock_break(struct work_struct *work)
2386 {
2387         struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
2388                                                   oplock_break);
2389         struct inode *inode = cfile->dentry->d_inode;
2390         struct cifsInodeInfo *cinode = CIFS_I(inode);
2391         int rc = 0;
2392
2393         if (inode && S_ISREG(inode->i_mode)) {
2394                 if (cinode->clientCanCacheRead)
2395                         break_lease(inode, O_RDONLY);
2396                 else
2397                         break_lease(inode, O_WRONLY);
2398                 rc = filemap_fdatawrite(inode->i_mapping);
2399                 if (cinode->clientCanCacheRead == 0) {
2400                         rc = filemap_fdatawait(inode->i_mapping);
2401                         mapping_set_error(inode->i_mapping, rc);
2402                         invalidate_remote_inode(inode);
2403                 }
2404                 cFYI(1, "Oplock flush inode %p rc %d", inode, rc);
2405         }
2406
2407         /*
2408          * releasing stale oplock after recent reconnect of smb session using
2409          * a now incorrect file handle is not a data integrity issue but do
2410          * not bother sending an oplock release if session to server still is
2411          * disconnected since oplock already released by the server
2412          */
2413         if (!cfile->oplock_break_cancelled) {
2414                 rc = CIFSSMBLock(0, tlink_tcon(cfile->tlink), cfile->netfid,
2415                                  current->tgid, 0, 0, 0, 0,
2416                                  LOCKING_ANDX_OPLOCK_RELEASE, false,
2417                                  cinode->clientCanCacheRead ? 1 : 0);
2418                 cFYI(1, "Oplock release rc = %d", rc);
2419         }
2420 }
2421
2422 const struct address_space_operations cifs_addr_ops = {
2423         .readpage = cifs_readpage,
2424         .readpages = cifs_readpages,
2425         .writepage = cifs_writepage,
2426         .writepages = cifs_writepages,
2427         .write_begin = cifs_write_begin,
2428         .write_end = cifs_write_end,
2429         .set_page_dirty = __set_page_dirty_nobuffers,
2430         .releasepage = cifs_release_page,
2431         .invalidatepage = cifs_invalidate_page,
2432         .launder_page = cifs_launder_page,
2433 };
2434
2435 /*
2436  * cifs_readpages requires the server to support a buffer large enough to
2437  * contain the header plus one complete page of data.  Otherwise, we need
2438  * to leave cifs_readpages out of the address space operations.
2439  */
2440 const struct address_space_operations cifs_addr_ops_smallbuf = {
2441         .readpage = cifs_readpage,
2442         .writepage = cifs_writepage,
2443         .writepages = cifs_writepages,
2444         .write_begin = cifs_write_begin,
2445         .write_end = cifs_write_end,
2446         .set_page_dirty = __set_page_dirty_nobuffers,
2447         .releasepage = cifs_release_page,
2448         .invalidatepage = cifs_invalidate_page,
2449         .launder_page = cifs_launder_page,
2450 };