Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  *   fs/cifs/file.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *   vfs operations that deal with files
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *   Copyright (C) International Business Machines  Corp., 2002,2010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *   Author(s): Steve French (sfrench@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *              Jeremy Allison (jra@samba.org)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *   This library is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *   it under the terms of the GNU Lesser General Public License as published
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *   by the Free Software Foundation; either version 2.1 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  *   (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *   This library is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *   the GNU Lesser General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  *   You should have received a copy of the GNU Lesser General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  *   along with this library; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/pagevec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/task_io_accounting_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <asm/div64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include "cifsfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include "cifspdu.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include "cifsglob.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include "cifsproto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "cifs_unicode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include "cifs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include "cifs_fs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include "fscache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include "smbdirect.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static inline int cifs_convert_flags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	if ((flags & O_ACCMODE) == O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		return GENERIC_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	else if ((flags & O_ACCMODE) == O_WRONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		return GENERIC_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	else if ((flags & O_ACCMODE) == O_RDWR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 		/* GENERIC_ALL is too much permission to request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		   can cause unnecessary access denied on create */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 		/* return GENERIC_ALL; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		return (GENERIC_READ | GENERIC_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 		FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 		FILE_READ_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static u32 cifs_posix_convert_flags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	u32 posix_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	if ((flags & O_ACCMODE) == O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		posix_flags = SMB_O_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	else if ((flags & O_ACCMODE) == O_WRONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 		posix_flags = SMB_O_WRONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	else if ((flags & O_ACCMODE) == O_RDWR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		posix_flags = SMB_O_RDWR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	if (flags & O_CREAT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		posix_flags |= SMB_O_CREAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		if (flags & O_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			posix_flags |= SMB_O_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	} else if (flags & O_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   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",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			 current->comm, current->tgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 	if (flags & O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 		posix_flags |= SMB_O_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	/* be safe and imply O_SYNC for O_DSYNC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (flags & O_DSYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		posix_flags |= SMB_O_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (flags & O_DIRECTORY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		posix_flags |= SMB_O_DIRECTORY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	if (flags & O_NOFOLLOW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		posix_flags |= SMB_O_NOFOLLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (flags & O_DIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		posix_flags |= SMB_O_DIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	return posix_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) static inline int cifs_get_disposition(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		return FILE_CREATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		return FILE_OVERWRITE_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	else if ((flags & O_CREAT) == O_CREAT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		return FILE_OPEN_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	else if ((flags & O_TRUNC) == O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		return FILE_OVERWRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		return FILE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) int cifs_posix_open(char *full_path, struct inode **pinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 			struct super_block *sb, int mode, unsigned int f_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 			__u32 *poplock, __u16 *pnetfid, unsigned int xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	FILE_UNIX_BASIC_INFO *presp_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	__u32 posix_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct cifs_fattr fattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	struct tcon_link *tlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	cifs_dbg(FYI, "posix open %s\n", full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 	presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	if (presp_data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	tlink = cifs_sb_tlink(cifs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	if (IS_ERR(tlink)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		rc = PTR_ERR(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		goto posix_open_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	tcon = tlink_tcon(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	mode &= ~current_umask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	posix_flags = cifs_posix_convert_flags(f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 			     poplock, full_path, cifs_sb->local_nls,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 			     cifs_remap(cifs_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	cifs_put_tlink(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		goto posix_open_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	if (presp_data->Type == cpu_to_le32(-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 		goto posix_open_ret; /* open ok, caller does qpathinfo */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (!pinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		goto posix_open_ret; /* caller does not need info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	/* get new inode and set it up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	if (*pinode == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		cifs_fill_uniqueid(sb, &fattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		*pinode = cifs_iget(sb, &fattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 		if (!*pinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 			goto posix_open_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		cifs_revalidate_mapping(*pinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 		cifs_fattr_to_inode(*pinode, &fattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) posix_open_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	kfree(presp_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	     struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	     struct cifs_fid *fid, unsigned int xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	int desired_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	int disposition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	int create_options = CREATE_NOT_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	FILE_ALL_INFO *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	struct cifs_open_parms oparms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	if (!server->ops->open)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	desired_access = cifs_convert_flags(f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195)  *  open flag mapping table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197)  *	POSIX Flag            CIFS Disposition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  *	----------            ----------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  *	O_CREAT               FILE_OPEN_IF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  *	O_CREAT | O_EXCL      FILE_CREATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  *	O_CREAT | O_TRUNC     FILE_OVERWRITE_IF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  *	O_TRUNC               FILE_OVERWRITE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  *	none of the above     FILE_OPEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  *	Note that there is not a direct match between disposition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  *	FILE_SUPERSEDE (ie create whether or not file exists although
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  *	O_CREAT | O_TRUNC is similar but truncates the existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  *	file rather than creating a new file as FILE_SUPERSEDE does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  *	(which uses the attributes / metadata passed in on open call)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  *?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  *?  O_SYNC is a reasonable match to CIFS writethrough flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  *?  and the read write flags match reasonably.  O_LARGEFILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  *?  is irrelevant because largefile support is always used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  *?  by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  *	 O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	disposition = cifs_get_disposition(f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	/* BB pass O_SYNC flag through on file attributes .. BB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	/* O_SYNC also has bit for O_DSYNC so following check picks up either */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (f_flags & O_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		create_options |= CREATE_WRITE_THROUGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	if (f_flags & O_DIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		create_options |= CREATE_NO_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	oparms.tcon = tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	oparms.cifs_sb = cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	oparms.desired_access = desired_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	oparms.create_options = cifs_create_options(cifs_sb, create_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	oparms.disposition = disposition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	oparms.path = full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	oparms.fid = fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	oparms.reconnect = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	rc = server->ops->open(xid, &oparms, oplock, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	/* TODO: Add support for calling posix query info but with passing in fid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (tcon->unix_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 					      xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 					 xid, fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		server->ops->close(xid, tcon, fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		if (rc == -ESTALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			rc = -EOPENSTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) cifs_has_mand_locks(struct cifsInodeInfo *cinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct cifs_fid_locks *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	bool has_locks = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	down_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	list_for_each_entry(cur, &cinode->llist, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		if (!list_empty(&cur->locks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			has_locks = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	return has_locks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) cifs_down_write(struct rw_semaphore *sem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	while (!down_write_trylock(sem))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) static void cifsFileInfo_put_work(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) struct cifsFileInfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		  struct tcon_link *tlink, __u32 oplock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	struct dentry *dentry = file_dentry(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	struct cifsInodeInfo *cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	struct cifs_fid_locks *fdlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	struct cifs_tcon *tcon = tlink_tcon(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (cfile == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		return cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (!fdlocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		kfree(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	INIT_LIST_HEAD(&fdlocks->locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	fdlocks->cfile = cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	cfile->llist = fdlocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 	cfile->count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	cfile->pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	cfile->uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	cfile->dentry = dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	cfile->f_flags = file->f_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	cfile->invalidHandle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	cfile->tlink = cifs_get_tlink(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	INIT_WORK(&cfile->put, cifsFileInfo_put_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	mutex_init(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	spin_lock_init(&cfile->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	cifs_sb_active(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	 * If the server returned a read oplock and we have mandatory brlocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	 * set oplock level to None.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	list_add(&fdlocks->llist, &cinode->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	spin_lock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		oplock = fid->pending_open->oplock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	list_del(&fid->pending_open->olist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	fid->purge_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	server->ops->set_fid(cfile, fid, oplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	list_add(&cfile->tlist, &tcon->openFileList);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	atomic_inc(&tcon->num_local_opens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	/* if readable file instance put first in list*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	spin_lock(&cinode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (file->f_mode & FMODE_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		list_add(&cfile->flist, &cinode->openFileList);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		list_add_tail(&cfile->flist, &cinode->openFileList);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	spin_unlock(&cinode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	if (fid->purge_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	file->private_data = cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	return cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) struct cifsFileInfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) cifsFileInfo_get(struct cifsFileInfo *cifs_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	spin_lock(&cifs_file->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	cifsFileInfo_get_locked(cifs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	spin_unlock(&cifs_file->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	return cifs_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	struct inode *inode = d_inode(cifs_file->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct cifsLockInfo *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	 * Delete any outstanding lock records. We'll lose them when the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	 * is closed anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	cifs_down_write(&cifsi->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		list_del(&li->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		cifs_del_lock_waiters(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		kfree(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	list_del(&cifs_file->llist->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	kfree(cifs_file->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	up_write(&cifsi->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	cifs_put_tlink(cifs_file->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	dput(cifs_file->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	cifs_sb_deactive(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	kfree(cifs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) static void cifsFileInfo_put_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	struct cifsFileInfo *cifs_file = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			struct cifsFileInfo, put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	cifsFileInfo_put_final(cifs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * cifsFileInfo_put - release a reference of file priv data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419)  * Always potentially wait for oplock handler. See _cifsFileInfo_put().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	_cifsFileInfo_put(cifs_file, true, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427)  * _cifsFileInfo_put - release a reference of file priv data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429)  * This may involve closing the filehandle @cifs_file out on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430)  * server. Must be called without holding tcon->open_file_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431)  * cinode->open_file_lock and cifs_file->file_info_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433)  * If @wait_for_oplock_handler is true and we are releasing the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434)  * reference, wait for any running oplock break handler of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435)  * and cancel any pending one. If calling this function from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436)  * oplock break handler, you need to pass false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 		       bool wait_oplock_handler, bool offload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	struct inode *inode = d_inode(cifs_file->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	struct cifs_fid fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	struct cifs_pending_open open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	bool oplock_break_cancelled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	spin_lock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	spin_lock(&cifsi->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	spin_lock(&cifs_file->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	if (--cifs_file->count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		spin_unlock(&cifs_file->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		spin_unlock(&cifsi->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	spin_unlock(&cifs_file->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (server->ops->get_lease_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		server->ops->get_lease_key(inode, &fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	/* store open in pending opens to make sure we don't miss lease break */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	/* remove it from the lists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	list_del(&cifs_file->flist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	list_del(&cifs_file->tlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	atomic_dec(&tcon->num_local_opens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (list_empty(&cifsi->openFileList)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		cifs_dbg(FYI, "closing last open instance for inode %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 			 d_inode(cifs_file->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		 * In strict cache mode we need invalidate mapping on the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		 * close  because it may cause a error when we open this file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		 * again and get at least level II oplock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 			set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		cifs_set_oplock_level(cifsi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	spin_unlock(&cifsi->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	oplock_break_cancelled = wait_oplock_handler ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		cancel_work_sync(&cifs_file->oplock_break) : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 		struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 		unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		if (server->ops->close_getattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			server->ops->close_getattr(xid, tcon, cifs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		else if (server->ops->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			server->ops->close(xid, tcon, &cifs_file->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		_free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	if (oplock_break_cancelled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		cifs_done_oplock_break(cifsi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	cifs_del_pending_open(&open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	if (offload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		queue_work(fileinfo_put_wq, &cifs_file->put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 		cifsFileInfo_put_final(cifs_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) int cifs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	int rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	__u32 oplock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	struct tcon_link *tlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	struct cifsFileInfo *cfile = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	char *full_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	bool posix_open_ok = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct cifs_fid fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct cifs_pending_open open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	tlink = cifs_sb_tlink(cifs_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (IS_ERR(tlink)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		return PTR_ERR(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	tcon = tlink_tcon(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	full_path = build_path_from_dentry(file_dentry(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	if (full_path == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		 inode, file->f_flags, full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (file->f_flags & O_DIRECT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	    cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			file->f_op = &cifs_file_direct_nobrl_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			file->f_op = &cifs_file_direct_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	if (server->oplocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		oplock = REQ_OPLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (!tcon->broken_posix_open && tcon->unix_ext &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	    cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		/* can not refresh inode info since size could be stale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		rc = cifs_posix_open(full_path, &inode, inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 				cifs_sb->mnt_file_mode /* ignored */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 				file->f_flags, &oplock, &fid.netfid, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 			cifs_dbg(FYI, "posix open succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			posix_open_ok = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		} else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			if (tcon->ses->serverNOS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  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",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 					 tcon->ses->serverName,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 					 tcon->ses->serverNOS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			tcon->broken_posix_open = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		} else if ((rc != -EIO) && (rc != -EREMOTE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			 (rc != -EOPNOTSUPP)) /* path not found or net err */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		 * Else fallthrough to retry open the old way on network i/o
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		 * or DFS errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	if (server->ops->get_lease_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		server->ops->get_lease_key(inode, &fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	cifs_add_pending_open(&fid, tlink, &open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	if (!posix_open_ok) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 		if (server->ops->get_lease_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 			server->ops->get_lease_key(inode, &fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				  file->f_flags, &oplock, &fid, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 			cifs_del_pending_open(&open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	cfile = cifs_new_fileinfo(&fid, file, tlink, oplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	if (cfile == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		if (server->ops->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			server->ops->close(xid, tcon, &fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		cifs_del_pending_open(&open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	cifs_fscache_set_inode_cookie(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		 * Time to set mode which we can not set earlier due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		 * problems creating new read-only files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		struct cifs_unix_set_info_args args = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			.mode	= inode->i_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			.uid	= INVALID_UID, /* no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			.gid	= INVALID_GID, /* no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 			.ctime	= NO_CHANGE_64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			.atime	= NO_CHANGE_64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			.mtime	= NO_CHANGE_64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			.device	= 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 				       cfile->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	cifs_put_tlink(tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646)  * Try to reacquire byte range locks that were released when session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647)  * to server was lost.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) cifs_relock_file(struct cifsFileInfo *cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		/* can cache locks - no need to relock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		rc = cifs_push_posix_locks(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		rc = tcon->ses->server->ops->push_mand_locks(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	int rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	__u32 oplock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	struct cifsInodeInfo *cinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	char *full_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	int desired_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	int disposition = FILE_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	int create_options = CREATE_NOT_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	struct cifs_open_parms oparms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	mutex_lock(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	if (!cfile->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		mutex_unlock(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	inode = d_inode(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	 * Can not grab rename sem here because various ops, including those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	 * that already have the rename sem can end up causing writepage to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	 * called and if the server was down that means we end up here, and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	 * can never tell if the caller already has the rename_sem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	full_path = build_path_from_dentry(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (full_path == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		mutex_unlock(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		 inode, cfile->f_flags, full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (tcon->ses->server->oplocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		oplock = REQ_OPLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	if (tcon->unix_ext && cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	    (CIFS_UNIX_POSIX_PATH_OPS_CAP &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 				le64_to_cpu(tcon->fsUnixInfo.Capability))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * original open. Must mask them off for a reopen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		unsigned int oflags = cfile->f_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 						~(O_CREAT | O_EXCL | O_TRUNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		rc = cifs_posix_open(full_path, NULL, inode->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 				     cifs_sb->mnt_file_mode /* ignored */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 				     oflags, &oplock, &cfile->fid.netfid, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			cifs_dbg(FYI, "posix reopen succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 			oparms.reconnect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			goto reopen_success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		 * fallthrough to retry open the old way on errors, especially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		 * in the reconnect path it is important to retry hard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	desired_access = cifs_convert_flags(cfile->f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	/* O_SYNC also has bit for O_DSYNC so following check picks up either */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	if (cfile->f_flags & O_SYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		create_options |= CREATE_WRITE_THROUGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (cfile->f_flags & O_DIRECT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		create_options |= CREATE_NO_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	if (server->ops->get_lease_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		server->ops->get_lease_key(inode, &cfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	oparms.tcon = tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	oparms.cifs_sb = cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	oparms.desired_access = desired_access;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	oparms.create_options = cifs_create_options(cifs_sb, create_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	oparms.disposition = disposition;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	oparms.path = full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	oparms.fid = &cfile->fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	oparms.reconnect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 * Can not refresh inode by passing in file_info buf to be returned by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 * ops->open and then calling get_inode_info with returned buf since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 * file might have write behind data that needs to be flushed and server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 * version of file size can be stale. If we knew for sure that inode was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 * not dirty locally we could do this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	rc = server->ops->open(xid, &oparms, &oplock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	if (rc == -ENOENT && oparms.reconnect == false) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		/* durable handle timeout is expired - open the file again */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		rc = server->ops->open(xid, &oparms, &oplock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		/* indicate that we need to relock the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		oparms.reconnect = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		mutex_unlock(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		cifs_dbg(FYI, "oplock: %d\n", oplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		goto reopen_error_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) reopen_success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	cfile->invalidHandle = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	mutex_unlock(&cfile->fh_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	if (can_flush) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		rc = filemap_write_and_wait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (!is_interrupt_error(rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			mapping_set_error(inode->i_mapping, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		if (tcon->posix_extensions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		else if (tcon->unix_ext)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			rc = cifs_get_inode_info_unix(&inode, full_path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 						      inode->i_sb, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			rc = cifs_get_inode_info(&inode, full_path, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 						 inode->i_sb, xid, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	 * Else we are writing out data to server already and could deadlock if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	 * we tried to flush data, and since we do not know if we have data that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	 * would invalidate the current end of file on the server we can not go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	 * to the server to get the new inode info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	 * If the server returned a read oplock and we have mandatory brlocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	 * set oplock level to None.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	server->ops->set_fid(cfile, &cfile->fid, oplock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	if (oparms.reconnect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		cifs_relock_file(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) reopen_error_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) int cifs_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	if (file->private_data != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		_cifsFileInfo_put(file->private_data, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	/* return code from the ->release op is always ignored */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	struct cifsFileInfo *open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct list_head *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	struct list_head *tmp1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	struct list_head tmp_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (!tcon->use_persistent || !tcon->need_reopen_files)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	tcon->need_reopen_files = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	cifs_dbg(FYI, "Reopen persistent handles\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	INIT_LIST_HEAD(&tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	/* list all files open on tree connection, reopen resilient handles  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	spin_lock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	list_for_each(tmp, &tcon->openFileList) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		open_file = list_entry(tmp, struct cifsFileInfo, tlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 		if (!open_file->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		list_add_tail(&open_file->rlist, &tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	list_for_each_safe(tmp, tmp1, &tmp_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		open_file = list_entry(tmp, struct cifsFileInfo, rlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		if (cifs_reopen_file(open_file, false /* do not flush */))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 			tcon->need_reopen_files = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		list_del_init(&open_file->rlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		cifsFileInfo_put(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) int cifs_closedir(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	struct cifsFileInfo *cfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (cfile == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	cifs_dbg(FYI, "Freeing private data in close dir\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	spin_lock(&cfile->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	if (server->ops->dir_needs_close(cfile)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		cfile->invalidHandle = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		spin_unlock(&cfile->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (server->ops->close_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			rc = server->ops->close_dir(xid, tcon, &cfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 			rc = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		/* not much we can do if it fails anyway, ignore rc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		spin_unlock(&cfile->file_info_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	buf = cfile->srch_inf.ntwrk_buf_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	if (buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		cfile->srch_inf.ntwrk_buf_start = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		if (cfile->srch_inf.smallBuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 			cifs_small_buf_release(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			cifs_buf_release(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	cifs_put_tlink(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	kfree(file->private_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	/* BB can we lock the filestruct while this is going on? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) static struct cifsLockInfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	struct cifsLockInfo *lock =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (!lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		return lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	lock->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	lock->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	lock->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	lock->pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	lock->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	INIT_LIST_HEAD(&lock->blist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	init_waitqueue_head(&lock->block_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	return lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) cifs_del_lock_waiters(struct cifsLockInfo *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	struct cifsLockInfo *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		list_del_init(&li->blist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		wake_up(&li->block_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) #define CIFS_LOCK_OP	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) #define CIFS_READ_OP	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) #define CIFS_WRITE_OP	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) /* @rw_check : 0 - no op, 1 - read, 2 - write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) static bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			    __u64 length, __u8 type, __u16 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			    struct cifsFileInfo *cfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			    struct cifsLockInfo **conf_lock, int rw_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	struct cifsLockInfo *li;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	struct cifsFileInfo *cur_cfile = fdlocks->cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	list_for_each_entry(li, &fdlocks->locks, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (offset + length <= li->offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		    offset >= li->offset + li->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 		    server->ops->compare_fids(cfile, cur_cfile)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			/* shared lock prevents write op through the same fid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			if (!(li->type & server->vals->shared_lock_type) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			    rw_check != CIFS_WRITE_OP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		if ((type & server->vals->shared_lock_type) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		    ((server->ops->compare_fids(cfile, cur_cfile) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		     current->tgid == li->pid) || type == li->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		if (rw_check == CIFS_LOCK_OP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		    (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 		    server->ops->compare_fids(cfile, cur_cfile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		if (conf_lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 			*conf_lock = li;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 			__u8 type, __u16 flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			struct cifsLockInfo **conf_lock, int rw_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	bool rc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	struct cifs_fid_locks *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	list_for_each_entry(cur, &cinode->llist, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 						 flags, cfile, conf_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 						 rw_check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * Check if there is another lock that prevents us to set the lock (mandatory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * style). If such a lock exists, update the flock structure with its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * or leave it the same if we can't. Returns 0 if we don't need to request to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  * the server or 1 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	       __u8 type, struct file_lock *flock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	struct cifsLockInfo *conf_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	bool exist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	down_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	exist = cifs_find_lock_conflict(cfile, offset, length, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 					flock->fl_flags, &conf_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 					CIFS_LOCK_OP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	if (exist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		flock->fl_start = conf_lock->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		flock->fl_end = conf_lock->offset + conf_lock->length - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		flock->fl_pid = conf_lock->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (conf_lock->type & server->vals->shared_lock_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			flock->fl_type = F_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			flock->fl_type = F_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	} else if (!cinode->can_cache_brlcks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		flock->fl_type = F_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	list_add_tail(&lock->llist, &cfile->llist->locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)  * Set the byte-range lock (mandatory style). Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)  * 1) 0, if we set the lock and don't need to request to the server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)  * 2) 1, if no locks prevent us but we need to request to the server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)  * 3) -EACCES, if there is a lock that prevents us and wait is false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		 bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	struct cifsLockInfo *conf_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	bool exist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	exist = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 					lock->type, lock->flags, &conf_lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 					CIFS_LOCK_OP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	if (!exist && cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		list_add_tail(&lock->llist, &cfile->llist->locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (!exist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	else if (!wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 		list_add_tail(&lock->blist, &conf_lock->blist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		rc = wait_event_interruptible(lock->block_q,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 					(lock->blist.prev == &lock->blist) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 					(lock->blist.next == &lock->blist));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		list_del_init(&lock->blist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)  * Check if there is another lock that prevents us to set the lock (posix
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)  * style). If such a lock exists, update the flock structure with its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)  * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)  * or leave it the same if we can't. Returns 0 if we don't need to request to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)  * the server or 1 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) cifs_posix_lock_test(struct file *file, struct file_lock *flock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	unsigned char saved_type = flock->fl_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	if ((flock->fl_flags & FL_POSIX) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	down_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	posix_test_lock(file, flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 		flock->fl_type = saved_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 		rc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)  * Set the byte-range lock (posix style). Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)  * 1) <0, if the error occurs while setting the lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)  * 2) 0, if we set the lock and don't need to request to the server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) cifs_posix_lock_set(struct file *file, struct file_lock *flock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	int rc = FILE_LOCK_DEFERRED + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if ((flock->fl_flags & FL_POSIX) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (!cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	rc = posix_lock_file(file, flock, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	int rc = 0, stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	struct cifsLockInfo *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	unsigned int num, max_num, max_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	LOCKING_ANDX_RANGE *buf, *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	static const int types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 		LOCKING_ANDX_LARGE_FILES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	 * and check it before using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	max_buf = tcon->ses->server->maxBuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		     PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 			PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	max_num = (max_buf - sizeof(struct smb_hdr)) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 						sizeof(LOCKING_ANDX_RANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (!buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		cur = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 		num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			if (li->type != types[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 			cur->Pid = cpu_to_le16(li->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			cur->LengthLow = cpu_to_le32((u32)li->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 			cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 			cur->OffsetLow = cpu_to_le32((u32)li->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 			cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 			if (++num == max_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 				stored_rc = cifs_lockv(xid, tcon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 						       cfile->fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 						       (__u8)li->type, 0, num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 						       buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 				if (stored_rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 					rc = stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 				cur = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 				num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 				cur++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		if (num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 			stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 					       (__u8)types[i], 0, num, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 			if (stored_rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 				rc = stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) static __u32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) hash_lockowner(fl_owner_t owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	return cifs_lock_secret ^ hash32_ptr((const void *)owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) struct lock_to_push {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	struct list_head llist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	__u64 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	__u64 length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	__u32 pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	__u16 netfid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	__u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) cifs_push_posix_locks(struct cifsFileInfo *cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	struct inode *inode = d_inode(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	struct file_lock *flock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	struct file_lock_context *flctx = inode->i_flctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	unsigned int count = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	int rc = 0, xid, type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	struct list_head locks_to_send, *el;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	struct lock_to_push *lck, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	__u64 length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	if (!flctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	spin_lock(&flctx->flc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	list_for_each(el, &flctx->flc_posix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	spin_unlock(&flctx->flc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	INIT_LIST_HEAD(&locks_to_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	 * Allocating count locks is enough because no FL_POSIX locks can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	 * added to the list while we are holding cinode->lock_sem that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	 * protects locking operations of this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 		lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 		if (!lck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 			goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		list_add_tail(&lck->llist, &locks_to_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	el = locks_to_send.next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	spin_lock(&flctx->flc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		if (el == &locks_to_send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			 * The list ended. We don't have enough allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 			 * structures - something is really wrong.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			cifs_dbg(VFS, "Can't push all brlocks!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		length = 1 + flock->fl_end - flock->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			type = CIFS_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			type = CIFS_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		lck = list_entry(el, struct lock_to_push, llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		lck->pid = hash_lockowner(flock->fl_owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 		lck->netfid = cfile->fid.netfid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		lck->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		lck->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		lck->offset = flock->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	spin_unlock(&flctx->flc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		int stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 					     lck->offset, lck->length, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 					     lck->type, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 		if (stored_rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			rc = stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		list_del(&lck->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		kfree(lck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		list_del(&lck->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		kfree(lck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) cifs_push_locks(struct cifsFileInfo *cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	/* we are going to update can_cache_brlcks here - need a write access */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	if (!cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		rc = cifs_push_posix_locks(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		rc = tcon->ses->server->ops->push_mand_locks(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	cinode->can_cache_brlcks = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		bool *wait_flag, struct TCP_Server_Info *server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (flock->fl_flags & FL_POSIX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		cifs_dbg(FYI, "Posix\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 	if (flock->fl_flags & FL_FLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		cifs_dbg(FYI, "Flock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	if (flock->fl_flags & FL_SLEEP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		cifs_dbg(FYI, "Blocking lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		*wait_flag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	if (flock->fl_flags & FL_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	if (flock->fl_flags & FL_LEASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		cifs_dbg(FYI, "Lease on file - not implemented yet\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	if (flock->fl_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	    (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	       FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	*type = server->vals->large_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	if (flock->fl_type == F_WRLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		cifs_dbg(FYI, "F_WRLCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		*type |= server->vals->exclusive_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		*lock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	} else if (flock->fl_type == F_UNLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 		cifs_dbg(FYI, "F_UNLCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		*type |= server->vals->unlock_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		*unlock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 		/* Check if unlock includes more than one lock range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	} else if (flock->fl_type == F_RDLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		cifs_dbg(FYI, "F_RDLCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		*type |= server->vals->shared_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		*lock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	} else if (flock->fl_type == F_EXLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 		cifs_dbg(FYI, "F_EXLCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		*type |= server->vals->exclusive_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		*lock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	} else if (flock->fl_type == F_SHLCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		cifs_dbg(FYI, "F_SHLCK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		*type |= server->vals->shared_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		*lock = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		cifs_dbg(FYI, "Unknown type of lock\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	   bool wait_flag, bool posix_lck, unsigned int xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	__u64 length = 1 + flock->fl_end - flock->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 	__u16 netfid = cfile->fid.netfid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 	if (posix_lck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		int posix_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		rc = cifs_posix_lock_test(file, flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		if (type & server->vals->shared_lock_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			posix_lock_type = CIFS_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 			posix_lock_type = CIFS_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		rc = CIFSSMBPosixLock(xid, tcon, netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 				      hash_lockowner(flock->fl_owner),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 				      flock->fl_start, length, flock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 				      posix_lock_type, wait_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	/* BB we could chain these into one lock request BB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 				    1, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 		rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 					    type, 0, 1, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		flock->fl_type = F_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 			cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 				 rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if (type & server->vals->shared_lock_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		flock->fl_type = F_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	type &= ~server->vals->exclusive_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 				    type | server->vals->shared_lock_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 				    1, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 			type | server->vals->shared_lock_type, 0, 1, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 		flock->fl_type = F_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 			cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 				 rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		flock->fl_type = F_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) cifs_move_llist(struct list_head *source, struct list_head *dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	struct list_head *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	list_for_each_safe(li, tmp, source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 		list_move(li, dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) cifs_free_llist(struct list_head *llist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct cifsLockInfo *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	list_for_each_entry_safe(li, tmp, llist, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 		cifs_del_lock_waiters(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 		list_del(&li->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 		kfree(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 		  unsigned int xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	int rc = 0, stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	static const int types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		LOCKING_ANDX_LARGE_FILES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 		LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	unsigned int max_num, num, max_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	LOCKING_ANDX_RANGE *buf, *cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct cifsLockInfo *li, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	__u64 length = 1 + flock->fl_end - flock->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	struct list_head tmp_llist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	INIT_LIST_HEAD(&tmp_llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	 * Accessing maxBuf is racy with cifs_reconnect - need to store value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 	 * and check it before using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	max_buf = tcon->ses->server->maxBuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 		     PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 			PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	max_num = (max_buf - sizeof(struct smb_hdr)) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 						sizeof(LOCKING_ANDX_RANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	cifs_down_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 		cur = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 		list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			if (flock->fl_start > li->offset ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 			    (flock->fl_start + length) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			    (li->offset + li->length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			if (current->tgid != li->pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			if (types[i] != li->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 			if (cinode->can_cache_brlcks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 				 * We can cache brlock requests - simply remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 				 * a lock from the file's list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 				list_del(&li->llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 				cifs_del_lock_waiters(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 				kfree(li);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 			cur->Pid = cpu_to_le16(li->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 			cur->LengthLow = cpu_to_le32((u32)li->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 			cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 			cur->OffsetLow = cpu_to_le32((u32)li->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 			cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 			 * We need to save a lock here to let us add it again to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 			 * the file's list if the unlock range request fails on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 			 * the server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 			list_move(&li->llist, &tmp_llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 			if (++num == max_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 				stored_rc = cifs_lockv(xid, tcon,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 						       cfile->fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 						       li->type, num, 0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 				if (stored_rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 					 * We failed on the unlock range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 					 * request - add all locks from the tmp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 					 * list to the head of the file's list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 					cifs_move_llist(&tmp_llist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 							&cfile->llist->locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 					rc = stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 					 * The unlock range request succeed -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 					 * free the tmp list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 					cifs_free_llist(&tmp_llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 				cur = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 				num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 				cur++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		if (num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 					       types[i], num, 0, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			if (stored_rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 				cifs_move_llist(&tmp_llist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 						&cfile->llist->locks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 				rc = stored_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 				cifs_free_llist(&tmp_llist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	up_write(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 	   bool wait_flag, bool posix_lck, int lock, int unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 	   unsigned int xid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	__u64 length = 1 + flock->fl_end - flock->fl_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 	struct inode *inode = d_inode(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 	if (posix_lck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		int posix_lock_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 		rc = cifs_posix_lock_set(file, flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		if (rc <= FILE_LOCK_DEFERRED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		if (type & server->vals->shared_lock_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 			posix_lock_type = CIFS_RDLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 			posix_lock_type = CIFS_WRLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 		if (unlock == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			posix_lock_type = CIFS_UNLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 				      hash_lockowner(flock->fl_owner),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 				      flock->fl_start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 				      NULL, posix_lock_type, wait_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	if (lock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		struct cifsLockInfo *lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 		lock = cifs_lock_init(flock->fl_start, length, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 				      flock->fl_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 		if (!lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 		rc = cifs_lock_add_if(cfile, lock, wait_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 			kfree(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 		 * Windows 7 server can delay breaking lease from read to None
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		 * if we set a byte-range lock on a file - break it explicitly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		 * before sending the lock to the server to be sure the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		 * read won't conflict with non-overlapted locks due to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 		 * pagereading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 		if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 					CIFS_CACHE_READ(CIFS_I(inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 			cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 			cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 				 inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 			CIFS_I(inode)->oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 					    type, 1, 0, wait_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			kfree(lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 		cifs_lock_add(cfile, lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	} else if (unlock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 		rc = server->ops->mand_unlock_range(cfile, flock, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 	if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		 * If this is a request to remove all locks because we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		 * are closing the file, it doesn't matter if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		 * unlocking failed as both cifs.ko and the SMB server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 		 * remove the lock on file close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 			cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			if (!(flock->fl_flags & FL_CLOSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		rc = locks_lock_file_wait(file, flock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	int rc, xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	int lock = 0, unlock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	bool wait_flag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	bool posix_lck = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	__u32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	if (!(fl->fl_flags & FL_FLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 		return -ENOLCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	cfile = (struct cifsFileInfo *)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 			tcon->ses->server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) 	if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 		posix_lck = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	if (!lock && !unlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		 * if no lock or unlock then nothing to do since we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 		 * know what it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 			xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	int rc, xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 	int lock = 0, unlock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	bool wait_flag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	bool posix_lck = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	__u32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	cifs_dbg(FYI, "Lock parm: 0x%x flockflags: 0x%x flocktype: 0x%x start: %lld end: %lld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		 cmd, flock->fl_flags, flock->fl_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 		 flock->fl_start, flock->fl_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 	cfile = (struct cifsFileInfo *)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			tcon->ses->server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		posix_lck = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	 * BB add code here to normalize offset and length to account for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	 * negative length which we can not accept over the wire.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 	if (IS_GETLK(cmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 		rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if (!lock && !unlock) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		 * if no lock or unlock then nothing to do since we do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		 * know what it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 			xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828)  * update the file size (if needed) after a write. Should be called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829)  * the inode->i_lock held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 		      unsigned int bytes_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	loff_t end_of_write = offset + bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	if (end_of_write > cifsi->server_eof)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		cifsi->server_eof = end_of_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	   size_t write_size, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	unsigned int bytes_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	unsigned int total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	struct dentry *dentry = open_file->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 	struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	struct cifs_io_parms io_parms = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		 write_size, *offset, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	tcon = tlink_tcon(open_file->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 	server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 	if (!server->ops->sync_write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	for (total_written = 0; write_size > total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	     total_written += bytes_written) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 		while (rc == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 			struct kvec iov[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 			if (open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 				/* we could deadlock if we called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 				   filemap_fdatawait from here so tell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 				   reopen_file not to flush data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 				   server now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 				rc = cifs_reopen_file(open_file, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 				if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 			len = min(server->ops->wp_retry_size(d_inode(dentry)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 				  (unsigned int)write_size - total_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 			/* iov[0] is reserved for smb header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			iov[1].iov_base = (char *)write_data + total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 			iov[1].iov_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 			io_parms.pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 			io_parms.tcon = tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			io_parms.offset = *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 			io_parms.length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 			rc = server->ops->sync_write(xid, &open_file->fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 					&io_parms, &bytes_written, iov, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 		if (rc || (bytes_written == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 			if (total_written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 				free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			spin_lock(&d_inode(dentry)->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 			cifs_update_eof(cifsi, *offset, bytes_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 			spin_unlock(&d_inode(dentry)->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 			*offset += bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	cifs_stats_bytes_written(tcon, total_written);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	if (total_written > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		spin_lock(&d_inode(dentry)->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		if (*offset > d_inode(dentry)->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 			i_size_write(d_inode(dentry), *offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 		spin_unlock(&d_inode(dentry)->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	mark_inode_dirty_sync(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	return total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 					bool fsuid_only)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	struct cifsFileInfo *open_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	/* only filter by fsuid on multiuser mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 		fsuid_only = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 	spin_lock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 	/* we could simply get the first_list_entry since write-only entries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	   are always at the end of the list but since the first entry might
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 	   have a close pending, we go through the whole list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 	list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 		if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 		if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 			if (!open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 				/* found a good file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 				/* lock it so it will not be closed on us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 				cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 				spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 				return open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 			} /* else might as well continue, and look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 			     another, or simply have the caller reopen it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 			     again rather than trying to fix this handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		} else /* write only file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 			break; /* write only files are last so must be done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 	spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) /* Return -EBADF if no handle is found and general rc otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		       struct cifsFileInfo **ret_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	struct cifsFileInfo *open_file, *inv_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 	bool any_available = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	int rc = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 	unsigned int refind = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 	bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 	bool with_delete = flags & FIND_WR_WITH_DELETE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	*ret_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	 * Having a null inode here (because mapping->host was set to zero by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 	 * the VFS or MM) should not happen but we had reports of on oops (due
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) 	 * to it being zero) during stress testcases so we need to check for it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	if (cifs_inode == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 		cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	/* only filter by fsuid on multiuser mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		fsuid_only = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	spin_lock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) refind_writable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 	if (refind > MAX_REOPEN_ATT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		if (!any_available && open_file->pid != current->tgid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		if (with_delete && !(open_file->fid.access & DELETE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 			if (!open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 				/* found a good writable file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 				cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 				spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 				*ret_file = open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 				return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 				if (!inv_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 					inv_file = open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	/* couldn't find useable FH with same pid, try any available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	if (!any_available) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 		any_available = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		goto refind_writable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	if (inv_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		any_available = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 		cifsFileInfo_get(inv_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	if (inv_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 		rc = cifs_reopen_file(inv_file, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 			*ret_file = inv_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 		spin_lock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 		list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 		spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 		cifsFileInfo_put(inv_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 		++refind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 		inv_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 		spin_lock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		goto refind_writable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) struct cifsFileInfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 	return cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 		       int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 		       struct cifsFileInfo **ret_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 	struct list_head *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 	struct cifsInodeInfo *cinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 	char *full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 	*ret_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 	spin_lock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 	list_for_each(tmp, &tcon->openFileList) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 		cfile = list_entry(tmp, struct cifsFileInfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			     tlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		full_path = build_path_from_dentry(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 		if (full_path == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 		if (strcmp(full_path, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 			kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 		kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 		cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 		spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 		return cifs_get_writable_file(cinode, flags, ret_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		       struct cifsFileInfo **ret_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 	struct list_head *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	struct cifsInodeInfo *cinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	char *full_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	*ret_file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 	spin_lock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	list_for_each(tmp, &tcon->openFileList) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 		cfile = list_entry(tmp, struct cifsFileInfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			     tlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 		full_path = build_path_from_dentry(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 		if (full_path == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 			spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		if (strcmp(full_path, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		kfree(full_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 		cinode = CIFS_I(d_inode(cfile->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 		spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 		*ret_file = find_readable_file(cinode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 		return *ret_file ? 0 : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	spin_unlock(&tcon->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	struct address_space *mapping = page->mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 	loff_t offset = (loff_t)page->index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	char *write_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 	int rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 	int bytes_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	struct cifsFileInfo *open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (!mapping || !mapping->host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 	inode = page->mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	offset += (loff_t)from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) 	write_data = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 	write_data += from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) 	if ((to > PAGE_SIZE) || (from > to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	/* racing with truncate? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 	if (offset > mapping->host->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		return 0; /* don't care */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	/* check to make sure that we are not extending the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	if (mapping->host->i_size - offset < (loff_t)to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		to = (unsigned)(mapping->host->i_size - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 				    &open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 		bytes_written = cifs_write(open_file, open_file->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 					   write_data, to - from, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 		cifsFileInfo_put(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 		/* Does mm or vfs already set times? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		inode->i_atime = inode->i_mtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 		if ((bytes_written > 0) && (offset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 			rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		else if (bytes_written < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			rc = bytes_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 			rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 		cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 		if (!is_retryable_error(rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 			rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) static struct cifs_writedata *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 			  pgoff_t end, pgoff_t *index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 			  unsigned int *found_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 	struct cifs_writedata *wdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 	wdata = cifs_writedata_alloc((unsigned int)tofind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 				     cifs_writev_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 	if (!wdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	*found_pages = find_get_pages_range_tag(mapping, index, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 				PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	return wdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) static unsigned int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 		    struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 		    struct writeback_control *wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 		    pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 	unsigned int nr_pages = 0, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 	for (i = 0; i < found_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		page = wdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 		 * At this point we hold neither the i_pages lock nor the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 		 * page lock: the page may be truncated or invalidated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 		 * (changing page->mapping to NULL), or even swizzled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		 * back from swapper_space to tmpfs file mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 		if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 			lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 		else if (!trylock_page(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 		if (unlikely(page->mapping != mapping)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 		if (!wbc->range_cyclic && page->index > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 			*done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		if (*next && (page->index != *next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 			/* Not next consecutive page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		if (wbc->sync_mode != WB_SYNC_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 			wait_on_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 		if (PageWriteback(page) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 				!clear_page_dirty_for_io(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 		 * This actually clears the dirty bit in the radix tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 		 * See cifs_writepage() for more commentary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		if (page_offset(page) >= i_size_read(mapping->host)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 			*done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 			end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 		wdata->pages[i] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 		*next = page->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) 		++nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	/* reset index to refind any pages skipped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	if (nr_pages == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		*index = wdata->pages[0]->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	/* put any pages we aren't going to use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	for (i = nr_pages; i < found_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 		put_page(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 		wdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 	return nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 		 struct address_space *mapping, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	wdata->sync_mode = wbc->sync_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 	wdata->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	wdata->offset = page_offset(wdata->pages[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 	wdata->pagesz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	wdata->tailsz = min(i_size_read(mapping->host) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 			page_offset(wdata->pages[nr_pages - 1]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 			(loff_t)PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 	wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	wdata->pid = wdata->cfile->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	if (wdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 		rc = wdata->server->ops->async_writev(wdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 						      cifs_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) static int cifs_writepages(struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 			   struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	bool done = false, scanned = false, range_whole = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	pgoff_t end, index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	struct cifs_writedata *wdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	struct cifsFileInfo *cfile = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	int saved_rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) 	 * If wsize is smaller than the page cache size, default to writing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	 * one page at a time via cifs_writepage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	if (cifs_sb->wsize < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		return generic_writepages(mapping, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	if (wbc->range_cyclic) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 		index = mapping->writeback_index; /* Start from prev offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 		end = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 		index = wbc->range_start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 		end = wbc->range_end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 		if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 			range_whole = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		scanned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 	server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	while (!done && index <= end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		unsigned int i, nr_pages, found_pages, wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 		pgoff_t next = 0, tofind, saved_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		struct cifs_credits credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		struct cifs_credits *credits = &credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 		int get_file_rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 		if (cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 			cifsFileInfo_put(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 		rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 		/* in case of an error store it to return later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 			get_file_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 		rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 						   &wsize, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 		if (rc != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 						  &found_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		if (!wdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		if (found_pages == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 			kref_put(&wdata->refcount, cifs_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 					       end, &index, &next, &done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		/* nothing to write? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 		if (nr_pages == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 			kref_put(&wdata->refcount, cifs_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 		wdata->credits = credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 		wdata->cfile = cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 		wdata->server = server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 		cfile = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 		if (!wdata->cfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 			cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 				 get_file_rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 			if (is_retryable_error(get_file_rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 				rc = get_file_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 				rc = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 			rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		for (i = 0; i < nr_pages; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 			unlock_page(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		/* send failure -- clean up the mess */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		if (rc != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 			add_credits_and_wake_if(server, &wdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 			for (i = 0; i < nr_pages; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 				if (is_retryable_error(rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 					redirty_page_for_writepage(wbc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 							   wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 					SetPageError(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 				end_page_writeback(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 				put_page(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 			if (!is_retryable_error(rc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 				mapping_set_error(mapping, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 		kref_put(&wdata->refcount, cifs_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 		if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 			index = saved_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) 		/* Return immediately if we received a signal during writing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 		if (is_interrupt_error(rc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 		if (rc != 0 && saved_rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 			saved_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		wbc->nr_to_write -= nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 		if (wbc->nr_to_write <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 		index = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) 	if (!scanned && !done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) 		 * We hit the last page and there is more work to be done: wrap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) 		 * back to the start of the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) 		scanned = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 		index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	if (saved_rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		rc = saved_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 	if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		mapping->writeback_index = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 	if (cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		cifsFileInfo_put(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) /* BB add check for wbc flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	if (!PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		cifs_dbg(FYI, "ppw - page not up to date\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	 * Set the "writeback" flag, and clear "dirty" in the radix tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 	 * A writepage() implementation always needs to do either this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	 * or re-dirty the page with "redirty_page_for_writepage()" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	 * the case of a failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	 * Just unlocking the page will cause the radix tree tag-bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	 * to fail to update with the state of the page correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	set_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) retry_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	if (is_retryable_error(rc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 		if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 			goto retry_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 		redirty_page_for_writepage(wbc, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 	} else if (rc != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 		SetPageError(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		mapping_set_error(page->mapping, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 	end_page_writeback(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) static int cifs_writepage(struct page *page, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	int rc = cifs_writepage_locked(page, wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) static int cifs_write_end(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 			loff_t pos, unsigned len, unsigned copied,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 			struct page *page, void *fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	struct inode *inode = mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 	struct cifsFileInfo *cfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	__u32 pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		pid = cfile->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 		pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 	cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 		 page, pos, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 	if (PageChecked(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 		if (copied == len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 			SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 		ClearPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 	} else if (!PageUptodate(page) && copied == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 		SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 	if (!PageUptodate(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 		char *page_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 		unsigned offset = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 		unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) 		xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) 		/* this is probably better than directly calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 		   partialpage_write since in this function the file handle is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) 		   known which we might as well	leverage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) 		/* BB check if anything else missing out of ppw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) 		   such as updating last write time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) 		page_data = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) 		rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) 		/* if (rc < 0) should we set writebehind rc? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) 		kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) 		rc = copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 		pos += copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 		set_page_dirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 	if (rc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 		spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 		if (pos > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 			i_size_write(inode, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 		spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 	put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) 		      int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) 	struct cifsFileInfo *smbfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) 	rc = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		trace_cifs_fsync_err(inode->i_ino, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) 	cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) 		 file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) 	if (!CIFS_CACHE_READ(CIFS_I(inode))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) 		rc = cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 			cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 			rc = 0; /* don't care about it in fsync */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	tcon = tlink_tcon(smbfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 		if (server->ops->flush == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 			rc = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 			goto strict_fsync_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 		if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 			smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) 			if (smbfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 				rc = server->ops->flush(xid, tcon, &smbfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) 				cifsFileInfo_put(smbfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 				cifs_dbg(FYI, "ignore fsync for file not open for write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 			rc = server->ops->flush(xid, tcon, &smbfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) strict_fsync_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	struct cifsFileInfo *smbfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 	rc = file_write_and_wait_range(file, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 		trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 		 file, datasync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	tcon = tlink_tcon(smbfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 		server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) 		if (server->ops->flush == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) 			rc = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 			goto fsync_exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) 		if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) 			smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 			if (smbfile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 				rc = server->ops->flush(xid, tcon, &smbfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 				cifsFileInfo_put(smbfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 				cifs_dbg(FYI, "ignore fsync for file not open for write\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 			rc = server->ops->flush(xid, tcon, &smbfile->fid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) fsync_exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688)  * As file closes, flush all cached write data for this inode checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689)  * for write behind errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) int cifs_flush(struct file *file, fl_owner_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 	if (file->f_mode & FMODE_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 		rc = filemap_write_and_wait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) 		trace_cifs_flush_err(inode->i_ino, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 	unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711) 	for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) 		pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) 		if (!pages[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) 			 * save number of pages we have already allocated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) 			 * return with ENOMEM error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 			num_pages = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 		for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 			put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) static inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) 	size_t num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) 	size_t clen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737) 	clen = min_t(const size_t, len, wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) 	num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) 	if (cur_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 		*cur_len = clen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 	return num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) cifs_uncached_writedata_release(struct kref *refcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	struct cifs_writedata *wdata = container_of(refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 					struct cifs_writedata, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 	kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	for (i = 0; i < wdata->nr_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 		put_page(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 	cifs_writedata_release(refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) cifs_uncached_writev_complete(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 	struct cifs_writedata *wdata = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 					struct cifs_writedata, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	struct inode *inode = d_inode(wdata->cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 	struct cifsInodeInfo *cifsi = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 	cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 	if (cifsi->server_eof > inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		i_size_write(inode, cifsi->server_eof);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	complete(&wdata->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 	collect_uncached_write_data(wdata->ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	/* the below call can possibly free the last ref to aio ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	kref_put(&wdata->refcount, cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		      size_t *len, unsigned long *num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 	size_t save_len, copied, bytes, cur_len = *len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	unsigned long i, nr_pages = *num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 	save_len = cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 		bytes = min_t(const size_t, cur_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 		copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 		cur_len -= copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 		 * If we didn't copy as much as we expected, then that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 		 * may mean we trod into an unmapped area. Stop copying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 		 * at that point. On the next pass through the big
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 		 * loop, we'll likely end up getting a zero-length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 		 * write and bailing out of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 		if (copied < bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 	cur_len = save_len - cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 	*len = cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 	 * If we have no data to send, then that probably means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	 * the copy above failed altogether. That's most likely because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 	 * the address in the iovec was bogus. Return -EFAULT and let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 	 * the caller free anything we allocated and bail out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	if (!cur_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 	 * i + 1 now represents the number of pages we actually used in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	 * the copy phase above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 	*num_pages = i + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	unsigned int wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) 	struct cifs_credits credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	struct TCP_Server_Info *server = wdata->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 		if (wdata->cfile->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) 			rc = cifs_reopen_file(wdata->cfile, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) 			if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) 			else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) 		 * Wait for credits to resend this wdata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 		 * Note: we are attempting to resend the whole wdata not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 		 * segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 			rc = server->ops->wait_mtu_credits(server, wdata->bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 						&wsize, &credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 			if (wsize < wdata->bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 				add_credits_and_wake_if(server, &credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 				msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 		} while (wsize < wdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 		wdata->credits = credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 		rc = adjust_credits(server, &wdata->credits, wdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 			if (wdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 				rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) #ifdef CONFIG_CIFS_SMB_DIRECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) 				if (wdata->mr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 					wdata->mr->need_invalidate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) 					smbd_deregister_mr(wdata->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) 					wdata->mr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 				rc = server->ops->async_writev(wdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 					cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 		/* If the write was successfully sent, we are done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 			list_add_tail(&wdata->list, wdata_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) 		/* Roll back credits and retry if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) 		add_credits_and_wake_if(server, &wdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) 	} while (rc == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) 	kref_put(&wdata->refcount, cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894) cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) 		     struct cifsFileInfo *open_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) 		     struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897) 		     struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) 	size_t cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) 	unsigned long nr_pages, num_pages, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) 	struct cifs_writedata *wdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) 	struct iov_iter saved_from = *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) 	loff_t saved_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) 	struct page **pagevec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) 	size_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) 		pid = open_file->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) 		pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) 	server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) 		unsigned int wsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921) 		struct cifs_credits credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) 		struct cifs_credits *credits = &credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) 		if (open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) 			rc = cifs_reopen_file(open_file, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926) 			if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) 			else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) 		rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) 						   &wsize, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) 		cur_len = min_t(const size_t, len, wsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) 		if (ctx->direct_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) 			ssize_t result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) 			result = iov_iter_get_pages_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943) 				from, &pagevec, cur_len, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) 			if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) 				cifs_dbg(VFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) 					 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947) 					 result, iov_iter_type(from),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) 					 from->iov_offset, from->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) 				dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) 				rc = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) 			cur_len = (size_t)result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) 			iov_iter_advance(from, cur_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) 			nr_pages =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959) 				(cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) 			wdata = cifs_writedata_direct_alloc(pagevec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) 					     cifs_uncached_writev_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963) 			if (!wdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) 			wdata->page_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) 			wdata->tailsz =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) 				nr_pages > 1 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) 					cur_len - (PAGE_SIZE - start) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974) 					(nr_pages - 2) * PAGE_SIZE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) 					cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) 			nr_pages = get_numpages(wsize, len, &cur_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) 			wdata = cifs_writedata_alloc(nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979) 					     cifs_uncached_writev_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) 			if (!wdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) 			rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988) 				kvfree(wdata->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) 				kfree(wdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) 			num_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) 			rc = wdata_fill_from_iovec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) 				wdata, from, &cur_len, &num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) 				for (i = 0; i < nr_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) 					put_page(wdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) 				kvfree(wdata->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) 				kfree(wdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) 			 * Bring nr_pages down to the number of pages we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) 			 * actually used, and free any pages that we didn't use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) 			for ( ; nr_pages > num_pages; nr_pages--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) 				put_page(wdata->pages[nr_pages - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) 			wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) 		wdata->sync_mode = WB_SYNC_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) 		wdata->nr_pages = nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) 		wdata->offset = (__u64)offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) 		wdata->cfile = cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) 		wdata->server = server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) 		wdata->pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) 		wdata->bytes = cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) 		wdata->pagesz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) 		wdata->credits = credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) 		wdata->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) 		kref_get(&ctx->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) 		rc = adjust_credits(server, &wdata->credits, wdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) 			if (wdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) 				rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034) 				rc = server->ops->async_writev(wdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) 					cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) 			add_credits_and_wake_if(server, &wdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) 			kref_put(&wdata->refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041) 				 cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) 			if (rc == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) 				*from = saved_from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) 				iov_iter_advance(from, offset - saved_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) 		list_add_tail(&wdata->list, wdata_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) 		offset += cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) 		len -= cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053) 	} while (len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) 	struct cifs_writedata *wdata, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064) 	struct dentry *dentry = ctx->cfile->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) 	tcon = tlink_tcon(ctx->cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) 	cifs_sb = CIFS_SB(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) 	mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) 	if (list_empty(&ctx->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) 		mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) 	rc = ctx->rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) 	 * Wait for and collect replies for any successful sends in order of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) 	 * increasing offset. Once an error is hit, then return without waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) 	 * for any more replies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) restart_loop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) 	list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) 			if (!try_wait_for_completion(&wdata->done)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087) 				mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) 			if (wdata->result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) 				rc = wdata->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) 				ctx->total_len += wdata->bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) 			/* resend call if it's a retryable error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) 			if (rc == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) 				struct list_head tmp_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) 				struct iov_iter tmp_from = ctx->iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) 				INIT_LIST_HEAD(&tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) 				list_del_init(&wdata->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) 				if (ctx->direct_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) 					rc = cifs_resend_wdata(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106) 						wdata, &tmp_list, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) 				else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) 					iov_iter_advance(&tmp_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109) 						 wdata->offset - ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) 					rc = cifs_write_from_iter(wdata->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) 						wdata->bytes, &tmp_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113) 						ctx->cfile, cifs_sb, &tmp_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) 						ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) 					kref_put(&wdata->refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) 						cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) 				list_splice(&tmp_list, &ctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121) 				goto restart_loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) 		list_del_init(&wdata->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) 		kref_put(&wdata->refcount, cifs_uncached_writedata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) 	cifs_stats_bytes_written(tcon, ctx->total_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) 	set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131) 	ctx->rc = (rc == 0) ? ctx->total_len : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) 	mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) 	if (ctx->iocb && ctx->iocb->ki_complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) 		ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) 		complete(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) static ssize_t __cifs_writev(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) 	struct kiocb *iocb, struct iov_iter *from, bool direct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145) 	ssize_t total_written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) 	struct cifs_aio_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) 	struct iov_iter saved_from = *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) 	size_t len = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) 	 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) 	 * In this case, fall back to non-direct write function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) 	 * this could be improved by getting pages directly in ITER_KVEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159) 	if (direct && iov_iter_is_kvec(from)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) 		cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) 		direct = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) 	rc = generic_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) 	if (rc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) 	cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) 	cfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) 	if (!tcon->ses->server->ops->async_writev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) 	ctx = cifs_aio_ctx_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) 	if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) 	ctx->cfile = cifsFileInfo_get(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) 	if (!is_sync_kiocb(iocb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182) 		ctx->iocb = iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) 	ctx->pos = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186) 	if (direct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) 		ctx->direct_io = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188) 		ctx->iter = *from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) 		ctx->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191) 		rc = setup_aio_ctx_iter(ctx, from, WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193) 			kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) 	/* grab a lock here due to read response handlers can access ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) 	mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) 	rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) 				  cfile, cifs_sb, &ctx->list, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) 	 * If at least one write was successfully sent, then discard any rc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206) 	 * value from the later writes. If the other write succeeds, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) 	 * we'll end up returning whatever was written. If it fails, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) 	 * we'll get a new rc value from that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) 	if (!list_empty(&ctx->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) 	mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) 		kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) 	if (!is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) 		kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) 		return -EIOCBQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) 	rc = wait_for_completion_killable(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) 		mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228) 		ctx->rc = rc = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) 		total_written = ctx->total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) 		mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) 		rc = ctx->rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) 		total_written = ctx->total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) 	kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) 	if (unlikely(!total_written))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) 	iocb->ki_pos += total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) 	return total_written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) 	return __cifs_writev(iocb, from, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) 	return __cifs_writev(iocb, from, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) cifs_writev(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) 	struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) 	struct inode *inode = file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) 	struct cifsInodeInfo *cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) 	struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) 	inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) 	 * We need to hold the sem to be sure nobody modifies lock list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268) 	 * with a brlock that prevents writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) 	down_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) 	rc = generic_write_checks(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) 	if (rc <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) 	if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) 				     server->vals->exclusive_lock_type, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) 				     NULL, CIFS_WRITE_OP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) 		rc = __generic_file_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) 		rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) 	inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) 	if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287) 		rc = generic_write_sync(iocb, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) 	struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295) 	struct cifsInodeInfo *cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) 	struct cifsFileInfo *cfile = (struct cifsFileInfo *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) 						iocb->ki_filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) 	ssize_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) 	written = cifs_get_writer(cinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303) 	if (written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) 		return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) 	if (CIFS_CACHE_WRITE(cinode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) 		if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308) 		(CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) 		  && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) 			written = generic_file_write_iter(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) 		written = cifs_writev(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) 	 * For non-oplocked files in strict cache mode we need to write the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) 	 * to the server exactly from the pos to pos+len-1 rather than flush all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) 	 * affected pages because it may cause a error with mandatory locks on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320) 	 * these pages but not on the region from pos to ppos+len-1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) 	written = cifs_user_writev(iocb, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) 	if (CIFS_CACHE_READ(cinode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325) 		 * We have read level caching and we have just sent a write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) 		 * request to the server thus making data in the cache stale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) 		 * Zap the cache and set oplock/lease level to NONE to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) 		 * reading stale data from the cache. All subsequent read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) 		 * operations will read new data from the server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) 		cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) 		cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) 			 inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) 		cinode->oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) 	cifs_put_writer(cinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338) 	return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) static struct cifs_readdata *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342) cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344) 	struct cifs_readdata *rdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) 	rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) 	if (rdata != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) 		rdata->pages = pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) 		kref_init(&rdata->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350) 		INIT_LIST_HEAD(&rdata->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) 		init_completion(&rdata->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) 		INIT_WORK(&rdata->work, complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) 	return rdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) static struct cifs_readdata *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) 	struct page **pages =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) 		kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363) 	struct cifs_readdata *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) 	if (pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366) 		ret = cifs_readdata_direct_alloc(pages, complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) 			kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) cifs_readdata_release(struct kref *refcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) 	struct cifs_readdata *rdata = container_of(refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) 					struct cifs_readdata, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) #ifdef CONFIG_CIFS_SMB_DIRECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380) 	if (rdata->mr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) 		smbd_deregister_mr(rdata->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382) 		rdata->mr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) 	if (rdata->cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) 		cifsFileInfo_put(rdata->cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) 	kvfree(rdata->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) 	kfree(rdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) 		page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) 		if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405) 		rdata->pages[i] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) 		unsigned int nr_page_failed = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) 		for (i = 0; i < nr_page_failed; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) 			put_page(rdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) 			rdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) cifs_uncached_readdata_release(struct kref *refcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) 	struct cifs_readdata *rdata = container_of(refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) 					struct cifs_readdata, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) 	kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) 	for (i = 0; i < rdata->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428) 		put_page(rdata->pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) 	cifs_readdata_release(refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434)  * cifs_readdata_to_iov - copy data from pages in response to an iovec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435)  * @rdata:	the readdata response with list of pages holding data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436)  * @iter:	destination for our data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438)  * This function copies data from a list of pages in a readdata response into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439)  * an array of iovecs. It will first calculate where the data should go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440)  * based on the info in the readdata and then copy the data into that spot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) 	size_t remaining = rdata->got_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) 	for (i = 0; i < rdata->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) 		struct page *page = rdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) 		size_t copy = min_t(size_t, remaining, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451) 		size_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) 		if (unlikely(iov_iter_is_pipe(iter))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454) 			void *addr = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) 			written = copy_to_iter(addr, copy, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) 			kunmap_atomic(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459) 			written = copy_page_to_iter(page, 0, copy, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) 		remaining -= written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) 		if (written < copy && iov_iter_count(iter) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464) 	return remaining ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) cifs_uncached_readv_complete(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) 	struct cifs_readdata *rdata = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) 						struct cifs_readdata, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) 	complete(&rdata->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) 	collect_uncached_read_data(rdata->ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) 	/* the below call can possibly free the last ref to aio ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) 	kref_put(&rdata->refcount, cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) uncached_fill_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) 		    struct cifs_readdata *rdata, struct iov_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484) 		    unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) 	unsigned int nr_pages = rdata->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) 	unsigned int page_offset = rdata->page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) 	rdata->got_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) 	rdata->tailsz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) 		struct page *page = rdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) 		size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) 		unsigned int segment_size = rdata->pagesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) 		if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) 			segment_size -= page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) 			page_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) 		if (len <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) 			/* no need to hold page hostage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) 			rdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507) 			rdata->nr_pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) 		n = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) 		if (len >= segment_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514) 			/* enough data to fill the page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) 			n = segment_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) 			rdata->tailsz = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) 		len -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520) 		if (iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) 			result = copy_page_from_iter(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) 					page, page_offset, n, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) #ifdef CONFIG_CIFS_SMB_DIRECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524) 		else if (rdata->mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) 			result = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) 			result = cifs_read_page_from_socket(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) 					server, page, page_offset, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530) 		if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) 		rdata->got_bytes += result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536) 	return rdata->got_bytes > 0 && result != -ECONNABORTED ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) 						rdata->got_bytes : result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) 			      struct cifs_readdata *rdata, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) 	return uncached_fill_pages(server, rdata, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548) cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) 			      struct cifs_readdata *rdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) 			      struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) 	return uncached_fill_pages(server, rdata, iter, iter->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) static int cifs_resend_rdata(struct cifs_readdata *rdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) 			struct list_head *rdata_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) 			struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) 	unsigned int rsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) 	struct cifs_credits credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) 	/* XXX: should we pick a new channel here? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565) 	server = rdata->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) 		if (rdata->cfile->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) 			rc = cifs_reopen_file(rdata->cfile, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) 			if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) 			else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) 		 * Wait for credits to resend this rdata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) 		 * Note: we are attempting to resend the whole rdata not in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) 		 * segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) 			rc = server->ops->wait_mtu_credits(server, rdata->bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) 						&rsize, &credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) 			if (rsize < rdata->bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) 				add_credits_and_wake_if(server, &credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) 				msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) 		} while (rsize < rdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) 		rdata->credits = credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) 		rc = adjust_credits(server, &rdata->credits, rdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) 			if (rdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) 				rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599) 			else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) #ifdef CONFIG_CIFS_SMB_DIRECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) 				if (rdata->mr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) 					rdata->mr->need_invalidate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603) 					smbd_deregister_mr(rdata->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) 					rdata->mr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607) 				rc = server->ops->async_readv(rdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) 		/* If the read was successfully sent, we are done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) 			/* Add to aio pending list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) 			list_add_tail(&rdata->list, rdata_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) 		/* Roll back credits and retry if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) 		add_credits_and_wake_if(server, &rdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) 	} while (rc == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) 	kref_put(&rdata->refcount, cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) 		     struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) 		     struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632) 	struct cifs_readdata *rdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) 	unsigned int npages, rsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) 	struct cifs_credits credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) 	struct cifs_credits *credits = &credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) 	size_t cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640) 	struct page **pagevec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) 	size_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) 	struct iov_iter direct_iov = ctx->iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) 	server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) 		pid = open_file->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649) 		pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) 	if (ctx->direct_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) 		iov_iter_advance(&direct_iov, offset - ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) 		if (open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) 			rc = cifs_reopen_file(open_file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657) 			if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) 			else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) 		rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) 						   &rsize, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) 		cur_len = min_t(const size_t, len, rsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) 		if (ctx->direct_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671) 			ssize_t result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) 			result = iov_iter_get_pages_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) 					&direct_iov, &pagevec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) 					cur_len, &start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) 			if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) 				cifs_dbg(VFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678) 					 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) 					 result, iov_iter_type(&direct_iov),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) 					 direct_iov.iov_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) 					 direct_iov.count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682) 				dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) 				rc = result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) 			cur_len = (size_t)result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) 			iov_iter_advance(&direct_iov, cur_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691) 			rdata = cifs_readdata_direct_alloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) 					pagevec, cifs_uncached_readv_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) 			if (!rdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) 			npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) 			rdata->page_offset = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) 			rdata->tailsz = npages > 1 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) 				cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) 				cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) 			npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) 			/* allocate a readdata struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) 			rdata = cifs_readdata_alloc(npages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) 					    cifs_uncached_readv_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) 			if (!rdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) 				rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) 			rc = cifs_read_allocate_pages(rdata, npages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719) 				kvfree(rdata->pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) 				kfree(rdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) 				add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) 			rdata->tailsz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728) 		rdata->server = server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) 		rdata->cfile = cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) 		rdata->nr_pages = npages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) 		rdata->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732) 		rdata->bytes = cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) 		rdata->pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) 		rdata->pagesz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) 		rdata->read_into_pages = cifs_uncached_read_into_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) 		rdata->copy_into_pages = cifs_uncached_copy_into_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) 		rdata->credits = credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) 		rdata->ctx = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739) 		kref_get(&ctx->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) 		rc = adjust_credits(server, &rdata->credits, rdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) 			if (rdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) 				rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747) 				rc = server->ops->async_readv(rdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751) 			add_credits_and_wake_if(server, &rdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) 			kref_put(&rdata->refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) 				cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) 			if (rc == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) 				iov_iter_revert(&direct_iov, cur_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) 		list_add_tail(&rdata->list, rdata_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) 		offset += cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) 		len -= cur_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) 	} while (len > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) collect_uncached_read_data(struct cifs_aio_ctx *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) 	struct cifs_readdata *rdata, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) 	struct iov_iter *to = &ctx->iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) 	cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) 	mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) 	if (list_empty(&ctx->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) 		mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) 	rc = ctx->rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) 	/* the loop below should proceed in the order of increasing offsets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) 	list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) 			if (!try_wait_for_completion(&rdata->done)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) 				mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) 			if (rdata->result == -EAGAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) 				/* resend call if it's a retryable error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798) 				struct list_head tmp_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) 				unsigned int got_bytes = rdata->got_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) 				list_del_init(&rdata->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802) 				INIT_LIST_HEAD(&tmp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) 				 * Got a part of data and then reconnect has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) 				 * happened -- fill the buffer and continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) 				 * reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) 				if (got_bytes && got_bytes < rdata->bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) 					rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) 					if (!ctx->direct_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) 						rc = cifs_readdata_to_iov(rdata, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) 					if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) 						kref_put(&rdata->refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) 							cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) 						continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820) 				if (ctx->direct_io) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) 					/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) 					 * Re-use rdata as this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) 					 * direct I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) 					rc = cifs_resend_rdata(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) 						rdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827) 						&tmp_list, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) 					rc = cifs_send_async_read(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) 						rdata->offset + got_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831) 						rdata->bytes - got_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) 						rdata->cfile, cifs_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) 						&tmp_list, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) 					kref_put(&rdata->refcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) 						cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) 				list_splice(&tmp_list, &ctx->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) 				goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) 			} else if (rdata->result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) 				rc = rdata->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) 			else if (!ctx->direct_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845) 				rc = cifs_readdata_to_iov(rdata, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) 			/* if there was a short read -- discard anything left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848) 			if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) 				rc = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) 			ctx->total_len += rdata->got_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) 		list_del_init(&rdata->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854) 		kref_put(&rdata->refcount, cifs_uncached_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) 	if (!ctx->direct_io)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) 		ctx->total_len = ctx->len - iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) 	/* mask nodata case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) 	if (rc == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) 	ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) 	mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) 	if (ctx->iocb && ctx->iocb->ki_complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) 		ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) 		complete(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) static ssize_t __cifs_readv(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875) 	struct kiocb *iocb, struct iov_iter *to, bool direct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) 	struct file *file = iocb->ki_filp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) 	struct cifsFileInfo *cfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) 	ssize_t rc, total_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) 	loff_t offset = iocb->ki_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) 	struct cifs_aio_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) 	 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) 	 * fall back to data copy read path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) 	 * this could be improved by getting pages directly in ITER_KVEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) 	if (direct && iov_iter_is_kvec(to)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) 		cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) 		direct = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) 	len = iov_iter_count(to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) 	cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901) 	cfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) 	tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) 	if (!tcon->ses->server->ops->async_readv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) 	if ((file->f_flags & O_ACCMODE) == O_WRONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) 		cifs_dbg(FYI, "attempting read on write only file instance\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) 	ctx = cifs_aio_ctx_alloc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) 	if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914) 	ctx->cfile = cifsFileInfo_get(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) 	if (!is_sync_kiocb(iocb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) 		ctx->iocb = iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) 	if (iter_is_iovec(to))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) 		ctx->should_dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) 	if (direct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) 		ctx->pos = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) 		ctx->direct_io = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) 		ctx->iter = *to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) 		ctx->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) 		rc = setup_aio_ctx_iter(ctx, to, READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) 			kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) 		len = ctx->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) 	/* grab a lock here due to read response handlers can access ctx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) 	mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) 	rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) 	/* if at least one read request send succeeded, then reset rc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942) 	if (!list_empty(&ctx->list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945) 	mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948) 		kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) 	if (!is_sync_kiocb(iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953) 		kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) 		return -EIOCBQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957) 	rc = wait_for_completion_killable(&ctx->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) 		mutex_lock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960) 		ctx->rc = rc = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) 		total_read = ctx->total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) 		mutex_unlock(&ctx->aio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) 		rc = ctx->rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) 		total_read = ctx->total_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) 	kref_put(&ctx->refcount, cifs_aio_ctx_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) 	if (total_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) 		iocb->ki_pos += total_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) 		return total_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979) 	return __cifs_readv(iocb, to, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) 	return __cifs_readv(iocb, to, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) 	struct inode *inode = file_inode(iocb->ki_filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) 	struct cifsInodeInfo *cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993) 	struct cifsFileInfo *cfile = (struct cifsFileInfo *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) 						iocb->ki_filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) 	int rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) 	 * In strict cache mode we need to read from the server all the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) 	 * if we don't have level II oplock because the server can delay mtime
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) 	 * change - so we can't make a decision about inode invalidating.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) 	 * And we can also fail with pagereading if there are mandatory locks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) 	 * on pages affected by this read but not on the region from pos to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004) 	 * pos+len-1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006) 	if (!CIFS_CACHE_READ(cinode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) 		return cifs_user_readv(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) 	if (cap_unix(tcon->ses) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) 	    (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) 	    ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) 		return generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 	 * We need to hold the sem to be sure nobody modifies lock list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) 	 * with a brlock that prevents reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) 	down_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) 	if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) 				     tcon->ses->server->vals->shared_lock_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) 				     0, NULL, CIFS_READ_OP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) 		rc = generic_file_read_iter(iocb, to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) 	up_read(&cinode->lock_sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) 	int rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) 	unsigned int bytes_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) 	unsigned int total_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) 	unsigned int current_read_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) 	unsigned int rsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) 	struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) 	struct cifs_tcon *tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) 	char *cur_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) 	struct cifsFileInfo *open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041) 	struct cifs_io_parms io_parms = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) 	int buf_type = CIFS_NO_BUFFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) 	__u32 pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) 	cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048) 	/* FIXME: set up handlers for larger reads and/or convert to async */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) 	rsize = min_t(unsigned int, cifs_sb->rsize, CIFSMaxBufSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) 	if (file->private_data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) 		rc = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) 	open_file = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) 	tcon = tlink_tcon(open_file->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) 	server = cifs_pick_channel(tcon->ses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) 	if (!server->ops->sync_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) 		return -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) 		pid = open_file->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) 		pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) 	if ((file->f_flags & O_ACCMODE) == O_WRONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) 		cifs_dbg(FYI, "attempting read on write only file instance\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) 	for (total_read = 0, cur_offset = read_data; read_size > total_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) 	     total_read += bytes_read, cur_offset += bytes_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) 			current_read_size = min_t(uint, read_size - total_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) 						  rsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) 			 * For windows me and 9x we do not want to request more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) 			 * than it negotiated since it will refuse the read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081) 			 * then.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) 			if (!(tcon->ses->capabilities &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) 				tcon->ses->server->vals->cap_large_files)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) 				current_read_size = min_t(uint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) 					current_read_size, CIFSMaxBufSize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) 			if (open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) 				rc = cifs_reopen_file(open_file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) 				if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093) 			io_parms.pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) 			io_parms.tcon = tcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) 			io_parms.offset = *offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) 			io_parms.length = current_read_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) 			io_parms.server = server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) 			rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099) 						    &bytes_read, &cur_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) 						    &buf_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) 		} while (rc == -EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) 		if (rc || (bytes_read == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) 			if (total_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) 				free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) 				return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) 			cifs_stats_bytes_read(tcon, total_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) 			*offset += bytes_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116) 	return total_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120)  * If the page is mmap'ed into a process' page tables, then we need to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121)  * sure that it doesn't change while being written back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) static vm_fault_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) cifs_page_mkwrite(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) 	struct page *page = vmf->page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) 	lock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) 	return VM_FAULT_LOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) static const struct vm_operations_struct cifs_file_vm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) 	.fault = filemap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) 	.map_pages = filemap_map_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) 	.page_mkwrite = cifs_page_mkwrite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) 	int xid, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) 	struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) 	if (!CIFS_CACHE_READ(CIFS_I(inode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) 		rc = cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) 		rc = generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) 		vma->vm_ops = &cifs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) 	int rc, xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) 	rc = cifs_revalidate_file(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) 		cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) 			 rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) 		rc = generic_file_mmap(file, vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) 		vma->vm_ops = &cifs_file_vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) cifs_readv_complete(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) 	unsigned int i, got_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) 	struct cifs_readdata *rdata = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) 						struct cifs_readdata, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) 	got_bytes = rdata->got_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) 	for (i = 0; i < rdata->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) 		struct page *page = rdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) 		lru_cache_add(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) 		if (rdata->result == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) 		    (rdata->result == -EAGAIN && got_bytes)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) 			flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) 			SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) 		unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) 		if (rdata->result == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) 		    (rdata->result == -EAGAIN && got_bytes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) 			cifs_readpage_to_fscache(rdata->mapping->host, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200) 		got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) 		rdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) 	kref_put(&rdata->refcount, cifs_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) readpages_fill_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) 		     struct cifs_readdata *rdata, struct iov_iter *iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) 		     unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) 	u64 eof;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) 	pgoff_t eof_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) 	unsigned int nr_pages = rdata->nr_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218) 	unsigned int page_offset = rdata->page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) 	/* determine the eof that the server (probably) has */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) 	eof = CIFS_I(rdata->mapping->host)->server_eof;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222) 	eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) 	cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225) 	rdata->got_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) 	rdata->tailsz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) 	for (i = 0; i < nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) 		struct page *page = rdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4229) 		unsigned int to_read = rdata->pagesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4230) 		size_t n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4232) 		if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4233) 			to_read -= page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4234) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4235) 			page_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4237) 		n = to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4239) 		if (len >= to_read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4240) 			len -= to_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4241) 		} else if (len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4242) 			/* enough for partial page, fill and zero the rest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4243) 			zero_user(page, len + page_offset, to_read - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4244) 			n = rdata->tailsz = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4245) 			len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4246) 		} else if (page->index > eof_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4247) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4248) 			 * The VFS will not try to do readahead past the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4249) 			 * i_size, but it's possible that we have outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4250) 			 * writes with gaps in the middle and the i_size hasn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4251) 			 * caught up yet. Populate those with zeroed out pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4252) 			 * to prevent the VFS from repeatedly attempting to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4253) 			 * fill them until the writes are flushed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4254) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4255) 			zero_user(page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4256) 			lru_cache_add(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4257) 			flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4258) 			SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4259) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4260) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4261) 			rdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4262) 			rdata->nr_pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4263) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4264) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4265) 			/* no need to hold page hostage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4266) 			lru_cache_add(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4267) 			unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4268) 			put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4269) 			rdata->pages[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4270) 			rdata->nr_pages--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4271) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4274) 		if (iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4275) 			result = copy_page_from_iter(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4276) 					page, page_offset, n, iter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4277) #ifdef CONFIG_CIFS_SMB_DIRECT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4278) 		else if (rdata->mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4279) 			result = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4280) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4281) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4282) 			result = cifs_read_page_from_socket(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4283) 					server, page, page_offset, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4284) 		if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4285) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4287) 		rdata->got_bytes += result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4290) 	return rdata->got_bytes > 0 && result != -ECONNABORTED ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4291) 						rdata->got_bytes : result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4294) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4295) cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4296) 			       struct cifs_readdata *rdata, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4298) 	return readpages_fill_pages(server, rdata, NULL, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4301) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4302) cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4303) 			       struct cifs_readdata *rdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4304) 			       struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4306) 	return readpages_fill_pages(server, rdata, iter, iter->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4309) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4310) readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4311) 		    unsigned int rsize, struct list_head *tmplist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4312) 		    unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4314) 	struct page *page, *tpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4315) 	unsigned int expected_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4316) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4317) 	gfp_t gfp = readahead_gfp_mask(mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4319) 	INIT_LIST_HEAD(tmplist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4321) 	page = lru_to_page(page_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4323) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4324) 	 * Lock the page and put it in the cache. Since no one else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4325) 	 * should have access to this page, we're safe to simply set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4326) 	 * PG_locked without checking it first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4327) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4328) 	__SetPageLocked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4329) 	rc = add_to_page_cache_locked(page, mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4330) 				      page->index, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4332) 	/* give up if we can't stick it in the cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4333) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4334) 		__ClearPageLocked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4335) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4338) 	/* move first page to the tmplist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4339) 	*offset = (loff_t)page->index << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4340) 	*bytes = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4341) 	*nr_pages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4342) 	list_move_tail(&page->lru, tmplist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4344) 	/* now try and add more pages onto the request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4345) 	expected_index = page->index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4346) 	list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4347) 		/* discontinuity ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4348) 		if (page->index != expected_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4349) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4351) 		/* would this page push the read over the rsize? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4352) 		if (*bytes + PAGE_SIZE > rsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4353) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4355) 		__SetPageLocked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4356) 		rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4357) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4358) 			__ClearPageLocked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4359) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4360) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4361) 		list_move_tail(&page->lru, tmplist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4362) 		(*bytes) += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4363) 		expected_index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4364) 		(*nr_pages)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4366) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4369) static int cifs_readpages(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4370) 	struct list_head *page_list, unsigned num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4372) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4373) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4374) 	struct list_head tmplist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4375) 	struct cifsFileInfo *open_file = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4376) 	struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4377) 	struct TCP_Server_Info *server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4378) 	pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4379) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4381) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4382) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4383) 	 * Reads as many pages as possible from fscache. Returns -ENOBUFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4384) 	 * immediately if the cookie is negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4385) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4386) 	 * After this point, every page in the list might have PG_fscache set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4387) 	 * so we will need to clean that up off of every page we don't use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4388) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4389) 	rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4390) 					 &num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4391) 	if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4392) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4393) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4396) 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4397) 		pid = open_file->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4398) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4399) 		pid = current->tgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4401) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4402) 	server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4404) 	cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4405) 		 __func__, file, mapping, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4407) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4408) 	 * Start with the page at end of list and move it to private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4409) 	 * list. Do the same with any following pages until we hit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4410) 	 * the rsize limit, hit an index discontinuity, or run out of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4411) 	 * pages. Issue the async read and then start the loop again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4412) 	 * until the list is empty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4413) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4414) 	 * Note that list order is important. The page_list is in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4415) 	 * the order of declining indexes. When we put the pages in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4416) 	 * the rdata->pages, then we want them in increasing order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4417) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4418) 	while (!list_empty(page_list) && !err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4419) 		unsigned int i, nr_pages, bytes, rsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4420) 		loff_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4421) 		struct page *page, *tpage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4422) 		struct cifs_readdata *rdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4423) 		struct cifs_credits credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4424) 		struct cifs_credits *credits = &credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4426) 		if (open_file->invalidHandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4427) 			rc = cifs_reopen_file(open_file, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4428) 			if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4429) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4430) 			else if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4431) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4432) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4434) 		rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4435) 						   &rsize, credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4436) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4437) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4439) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4440) 		 * Give up immediately if rsize is too small to read an entire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4441) 		 * page. The VFS will fall back to readpage. We should never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4442) 		 * reach this point however since we set ra_pages to 0 when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4443) 		 * rsize is smaller than a cache page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4444) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4445) 		if (unlikely(rsize < PAGE_SIZE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4446) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4447) 			free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4448) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4449) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4451) 		nr_pages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4452) 		err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4453) 					 &nr_pages, &offset, &bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4454) 		if (!nr_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4455) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4456) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4457) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4459) 		rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4460) 		if (!rdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4461) 			/* best to give up if we're out of mem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4462) 			list_for_each_entry_safe(page, tpage, &tmplist, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4463) 				list_del(&page->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4464) 				lru_cache_add(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4465) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4466) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4467) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4468) 			rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4469) 			add_credits_and_wake_if(server, credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4470) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4471) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4473) 		rdata->cfile = cifsFileInfo_get(open_file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4474) 		rdata->server = server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4475) 		rdata->mapping = mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4476) 		rdata->offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4477) 		rdata->bytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4478) 		rdata->pid = pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4479) 		rdata->pagesz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4480) 		rdata->tailsz = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4481) 		rdata->read_into_pages = cifs_readpages_read_into_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4482) 		rdata->copy_into_pages = cifs_readpages_copy_into_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4483) 		rdata->credits = credits_on_stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4485) 		list_for_each_entry_safe(page, tpage, &tmplist, lru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4486) 			list_del(&page->lru);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4487) 			rdata->pages[rdata->nr_pages++] = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4490) 		rc = adjust_credits(server, &rdata->credits, rdata->bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4492) 		if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4493) 			if (rdata->cfile->invalidHandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4494) 				rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4495) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4496) 				rc = server->ops->async_readv(rdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4497) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4499) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4500) 			add_credits_and_wake_if(server, &rdata->credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4501) 			for (i = 0; i < rdata->nr_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4502) 				page = rdata->pages[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4503) 				lru_cache_add(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4504) 				unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4505) 				put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4506) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4507) 			/* Fallback to the readpage in error/reconnect cases */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4508) 			kref_put(&rdata->refcount, cifs_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4509) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4510) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4512) 		kref_put(&rdata->refcount, cifs_readdata_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4515) 	/* Any pages that have been shown to fscache but didn't get added to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4516) 	 * the pagecache must be uncached before they get returned to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4517) 	 * allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4518) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4519) 	cifs_fscache_readpages_cancel(mapping->host, page_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4520) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4521) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4525)  * cifs_readpage_worker must be called with the page pinned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4526)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4527) static int cifs_readpage_worker(struct file *file, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4528) 	loff_t *poffset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4530) 	char *read_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4531) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4533) 	/* Is the page cached? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4534) 	rc = cifs_readpage_from_fscache(file_inode(file), page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4535) 	if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4536) 		goto read_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4538) 	read_data = kmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4539) 	/* for reads over a certain size could initiate async read ahead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4541) 	rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4543) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4544) 		goto io_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4545) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4546) 		cifs_dbg(FYI, "Bytes read %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4548) 	/* we do not want atime to be less than mtime, it broke some apps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4549) 	file_inode(file)->i_atime = current_time(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4550) 	if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4551) 		file_inode(file)->i_atime = file_inode(file)->i_mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4552) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4553) 		file_inode(file)->i_atime = current_time(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4555) 	if (PAGE_SIZE > rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4556) 		memset(read_data + rc, 0, PAGE_SIZE - rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4558) 	flush_dcache_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4559) 	SetPageUptodate(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4561) 	/* send this page to the cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4562) 	cifs_readpage_to_fscache(file_inode(file), page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4564) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4566) io_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4567) 	kunmap(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4568) 	unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4570) read_complete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4571) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4574) static int cifs_readpage(struct file *file, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4576) 	loff_t offset = page_file_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4577) 	int rc = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4578) 	unsigned int xid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4580) 	xid = get_xid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4582) 	if (file->private_data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4583) 		rc = -EBADF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4584) 		free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4585) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4586) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4588) 	cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4589) 		 page, (int)offset, (int)offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4591) 	rc = cifs_readpage_worker(file, page, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4593) 	free_xid(xid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4594) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4597) static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4599) 	struct cifsFileInfo *open_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4601) 	spin_lock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4602) 	list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4603) 		if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4604) 			spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4605) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4606) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4607) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4608) 	spin_unlock(&cifs_inode->open_file_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4609) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4612) /* We do not want to update the file size from server for inodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4613)    open for write - to avoid races with writepage extending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4614)    the file - in the future we could consider allowing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4615)    refreshing the inode only on increases in the file size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4616)    but this is tricky to do without racing with writebehind
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4617)    page caching in the current Linux kernel design */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4618) bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4620) 	if (!cifsInode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4621) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4623) 	if (is_inode_writable(cifsInode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4624) 		/* This inode is open for write at least once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4625) 		struct cifs_sb_info *cifs_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4627) 		cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4628) 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4629) 			/* since no page cache to corrupt on directio
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4630) 			we can change size safely */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4631) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4632) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4634) 		if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4635) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4637) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4638) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4639) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4642) static int cifs_write_begin(struct file *file, struct address_space *mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4643) 			loff_t pos, unsigned len, unsigned flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4644) 			struct page **pagep, void **fsdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4646) 	int oncethru = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4647) 	pgoff_t index = pos >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4648) 	loff_t offset = pos & (PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4649) 	loff_t page_start = pos & PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4650) 	loff_t i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4651) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4652) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4654) 	cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4656) start:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4657) 	page = grab_cache_page_write_begin(mapping, index, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4658) 	if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4659) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4660) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4663) 	if (PageUptodate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4664) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4666) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4667) 	 * If we write a full page it will be up to date, no need to read from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4668) 	 * the server. If the write is short, we'll end up doing a sync write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4669) 	 * instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4670) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4671) 	if (len == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4672) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4674) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4675) 	 * optimize away the read when we have an oplock, and we're not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4676) 	 * expecting to use any of the data we'd be reading in. That
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4677) 	 * is, when the page lies beyond the EOF, or straddles the EOF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4678) 	 * and the write will cover all of the existing data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4679) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4680) 	if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4681) 		i_size = i_size_read(mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4682) 		if (page_start >= i_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4683) 		    (offset == 0 && (pos + len) >= i_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4684) 			zero_user_segments(page, 0, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4685) 					   offset + len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4686) 					   PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4687) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4688) 			 * PageChecked means that the parts of the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4689) 			 * to which we're not writing are considered up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4690) 			 * to date. Once the data is copied to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4691) 			 * page, it can be set uptodate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4692) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4693) 			SetPageChecked(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4694) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4695) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4696) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4698) 	if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4699) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4700) 		 * might as well read a page, it is fast enough. If we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4701) 		 * an error, we don't need to return it. cifs_write_end will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4702) 		 * do a sync write instead since PG_uptodate isn't set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4703) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4704) 		cifs_readpage_worker(file, page, &page_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4705) 		put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4706) 		oncethru = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4707) 		goto start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4708) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4709) 		/* we could try using another file handle if there is one -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4710) 		   but how would we lock it to prevent close of that handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4711) 		   racing with this read? In any case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4712) 		   this will be written out by write_end so is fine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4713) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4714) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4715) 	*pagep = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4716) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4719) static int cifs_release_page(struct page *page, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4721) 	if (PagePrivate(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4722) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4724) 	return cifs_fscache_release_page(page, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4727) static void cifs_invalidate_page(struct page *page, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4728) 				 unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4730) 	struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4732) 	if (offset == 0 && length == PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4733) 		cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4736) static int cifs_launder_page(struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4738) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4739) 	loff_t range_start = page_offset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4740) 	loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4741) 	struct writeback_control wbc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4742) 		.sync_mode = WB_SYNC_ALL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4743) 		.nr_to_write = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4744) 		.range_start = range_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4745) 		.range_end = range_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4746) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4748) 	cifs_dbg(FYI, "Launder page: %p\n", page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4750) 	if (clear_page_dirty_for_io(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4751) 		rc = cifs_writepage_locked(page, &wbc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4753) 	cifs_fscache_invalidate_page(page, page->mapping->host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4754) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4757) void cifs_oplock_break(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4759) 	struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4760) 						  oplock_break);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4761) 	struct inode *inode = d_inode(cfile->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4762) 	struct cifsInodeInfo *cinode = CIFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4763) 	struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4764) 	struct TCP_Server_Info *server = tcon->ses->server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4765) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4766) 	bool purge_cache = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4768) 	wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4769) 			TASK_UNINTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4771) 	server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4772) 				      cfile->oplock_epoch, &purge_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4774) 	if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4775) 						cifs_has_mand_locks(cinode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4776) 		cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4777) 			 inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4778) 		cinode->oplock = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4779) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4781) 	if (inode && S_ISREG(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4782) 		if (CIFS_CACHE_READ(cinode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4783) 			break_lease(inode, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4784) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4785) 			break_lease(inode, O_WRONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4786) 		rc = filemap_fdatawrite(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4787) 		if (!CIFS_CACHE_READ(cinode) || purge_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4788) 			rc = filemap_fdatawait(inode->i_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4789) 			mapping_set_error(inode->i_mapping, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4790) 			cifs_zap_mapping(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4791) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4792) 		cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4793) 		if (CIFS_CACHE_WRITE(cinode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4794) 			goto oplock_break_ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4795) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4797) 	rc = cifs_push_locks(cfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4798) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4799) 		cifs_dbg(VFS, "Push locks rc = %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4801) oplock_break_ack:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4802) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4803) 	 * releasing stale oplock after recent reconnect of smb session using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4804) 	 * a now incorrect file handle is not a data integrity issue but do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4805) 	 * not bother sending an oplock release if session to server still is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4806) 	 * disconnected since oplock already released by the server
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4807) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4808) 	if (!cfile->oplock_break_cancelled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4809) 		rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4810) 							     cinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4811) 		cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4812) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4813) 	_cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4814) 	cifs_done_oplock_break(cinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4817) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4818)  * The presence of cifs_direct_io() in the address space ops vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4819)  * allowes open() O_DIRECT flags which would have failed otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4820)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4821)  * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4822)  * so this method should never be called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4823)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4824)  * Direct IO is not yet supported in the cached mode. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4825)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4826) static ssize_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4827) cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4829)         /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4830)          * FIXME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4831)          * Eventually need to support direct IO for non forcedirectio mounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4832)          */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4833)         return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4836) static int cifs_swap_activate(struct swap_info_struct *sis,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4837) 			      struct file *swap_file, sector_t *span)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4839) 	struct cifsFileInfo *cfile = swap_file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4840) 	struct inode *inode = swap_file->f_mapping->host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4841) 	unsigned long blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4842) 	long long isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4844) 	cifs_dbg(FYI, "swap activate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4846) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4847) 	blocks = inode->i_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4848) 	isize = inode->i_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4849) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4850) 	if (blocks*512 < isize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4851) 		pr_warn("swap activate: swapfile has holes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4852) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4854) 	*span = sis->pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4856) 	pr_warn_once("Swap support over SMB3 is experimental\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4858) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4859) 	 * TODO: consider adding ACL (or documenting how) to prevent other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4860) 	 * users (on this or other systems) from reading it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4861) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4864) 	/* TODO: add sk_set_memalloc(inet) or similar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4866) 	if (cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4867) 		cfile->swapfile = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4868) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4869) 	 * TODO: Since file already open, we can't open with DENY_ALL here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4870) 	 * but we could add call to grab a byte range lock to prevent others
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4871) 	 * from reading or writing the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4872) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4874) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4877) static void cifs_swap_deactivate(struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4879) 	struct cifsFileInfo *cfile = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4881) 	cifs_dbg(FYI, "swap deactivate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4883) 	/* TODO: undo sk_set_memalloc(inet) will eventually be needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4885) 	if (cfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4886) 		cfile->swapfile = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4888) 	/* do we need to unpin (or unlock) the file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4891) const struct address_space_operations cifs_addr_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4892) 	.readpage = cifs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4893) 	.readpages = cifs_readpages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4894) 	.writepage = cifs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4895) 	.writepages = cifs_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4896) 	.write_begin = cifs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4897) 	.write_end = cifs_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4898) 	.set_page_dirty = __set_page_dirty_nobuffers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4899) 	.releasepage = cifs_release_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4900) 	.direct_IO = cifs_direct_io,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4901) 	.invalidatepage = cifs_invalidate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4902) 	.launder_page = cifs_launder_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4903) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4904) 	 * TODO: investigate and if useful we could add an cifs_migratePage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4905) 	 * helper (under an CONFIG_MIGRATION) in the future, and also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4906) 	 * investigate and add an is_dirty_writeback helper if needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4907) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4908) 	.swap_activate = cifs_swap_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4909) 	.swap_deactivate = cifs_swap_deactivate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4910) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4912) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4913)  * cifs_readpages requires the server to support a buffer large enough to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4914)  * contain the header plus one complete page of data.  Otherwise, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4915)  * to leave cifs_readpages out of the address space operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4916)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4917) const struct address_space_operations cifs_addr_ops_smallbuf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4918) 	.readpage = cifs_readpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4919) 	.writepage = cifs_writepage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4920) 	.writepages = cifs_writepages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4921) 	.write_begin = cifs_write_begin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4922) 	.write_end = cifs_write_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4923) 	.set_page_dirty = __set_page_dirty_nobuffers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4924) 	.releasepage = cifs_release_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4925) 	.invalidatepage = cifs_invalidate_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4926) 	.launder_page = cifs_launder_page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4927) };