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