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