138c8a9a5SSteve French // SPDX-License-Identifier: LGPL-2.1 238c8a9a5SSteve French /* 338c8a9a5SSteve French * 438c8a9a5SSteve French * vfs operations that deal with files 538c8a9a5SSteve French * 638c8a9a5SSteve French * Copyright (C) International Business Machines Corp., 2002,2010 738c8a9a5SSteve French * Author(s): Steve French (sfrench@us.ibm.com) 838c8a9a5SSteve French * Jeremy Allison (jra@samba.org) 938c8a9a5SSteve French * 1038c8a9a5SSteve French */ 1138c8a9a5SSteve French #include <linux/fs.h> 1238c8a9a5SSteve French #include <linux/filelock.h> 1338c8a9a5SSteve French #include <linux/backing-dev.h> 1438c8a9a5SSteve French #include <linux/stat.h> 1538c8a9a5SSteve French #include <linux/fcntl.h> 1638c8a9a5SSteve French #include <linux/pagemap.h> 1738c8a9a5SSteve French #include <linux/pagevec.h> 1838c8a9a5SSteve French #include <linux/writeback.h> 1938c8a9a5SSteve French #include <linux/task_io_accounting_ops.h> 2038c8a9a5SSteve French #include <linux/delay.h> 2138c8a9a5SSteve French #include <linux/mount.h> 2238c8a9a5SSteve French #include <linux/slab.h> 2338c8a9a5SSteve French #include <linux/swap.h> 2438c8a9a5SSteve French #include <linux/mm.h> 2538c8a9a5SSteve French #include <asm/div64.h> 2638c8a9a5SSteve French #include "cifsfs.h" 2738c8a9a5SSteve French #include "cifspdu.h" 2838c8a9a5SSteve French #include "cifsglob.h" 2938c8a9a5SSteve French #include "cifsproto.h" 3038c8a9a5SSteve French #include "smb2proto.h" 3138c8a9a5SSteve French #include "cifs_unicode.h" 3238c8a9a5SSteve French #include "cifs_debug.h" 3338c8a9a5SSteve French #include "cifs_fs_sb.h" 3438c8a9a5SSteve French #include "fscache.h" 3538c8a9a5SSteve French #include "smbdirect.h" 3638c8a9a5SSteve French #include "fs_context.h" 3738c8a9a5SSteve French #include "cifs_ioctl.h" 3838c8a9a5SSteve French #include "cached_dir.h" 3938c8a9a5SSteve French 4038c8a9a5SSteve French /* 4138c8a9a5SSteve French * Remove the dirty flags from a span of pages. 4238c8a9a5SSteve French */ 4338c8a9a5SSteve French static void cifs_undirty_folios(struct inode *inode, loff_t start, unsigned int len) 4438c8a9a5SSteve French { 4538c8a9a5SSteve French struct address_space *mapping = inode->i_mapping; 4638c8a9a5SSteve French struct folio *folio; 4738c8a9a5SSteve French pgoff_t end; 4838c8a9a5SSteve French 4938c8a9a5SSteve French XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 5038c8a9a5SSteve French 5138c8a9a5SSteve French rcu_read_lock(); 5238c8a9a5SSteve French 5338c8a9a5SSteve French end = (start + len - 1) / PAGE_SIZE; 5438c8a9a5SSteve French xas_for_each_marked(&xas, folio, end, PAGECACHE_TAG_DIRTY) { 5538c8a9a5SSteve French if (xas_retry(&xas, folio)) 5638c8a9a5SSteve French continue; 5738c8a9a5SSteve French xas_pause(&xas); 5838c8a9a5SSteve French rcu_read_unlock(); 5938c8a9a5SSteve French folio_lock(folio); 6038c8a9a5SSteve French folio_clear_dirty_for_io(folio); 6138c8a9a5SSteve French folio_unlock(folio); 6238c8a9a5SSteve French rcu_read_lock(); 6338c8a9a5SSteve French } 6438c8a9a5SSteve French 6538c8a9a5SSteve French rcu_read_unlock(); 6638c8a9a5SSteve French } 6738c8a9a5SSteve French 6838c8a9a5SSteve French /* 6938c8a9a5SSteve French * Completion of write to server. 7038c8a9a5SSteve French */ 7138c8a9a5SSteve French void cifs_pages_written_back(struct inode *inode, loff_t start, unsigned int len) 7238c8a9a5SSteve French { 7338c8a9a5SSteve French struct address_space *mapping = inode->i_mapping; 7438c8a9a5SSteve French struct folio *folio; 7538c8a9a5SSteve French pgoff_t end; 7638c8a9a5SSteve French 7738c8a9a5SSteve French XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 7838c8a9a5SSteve French 7938c8a9a5SSteve French if (!len) 8038c8a9a5SSteve French return; 8138c8a9a5SSteve French 8238c8a9a5SSteve French rcu_read_lock(); 8338c8a9a5SSteve French 8438c8a9a5SSteve French end = (start + len - 1) / PAGE_SIZE; 8538c8a9a5SSteve French xas_for_each(&xas, folio, end) { 8638c8a9a5SSteve French if (xas_retry(&xas, folio)) 8738c8a9a5SSteve French continue; 8838c8a9a5SSteve French if (!folio_test_writeback(folio)) { 8938c8a9a5SSteve French WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", 90d3c79235SDavid Howells len, start, folio->index, end); 9138c8a9a5SSteve French continue; 9238c8a9a5SSteve French } 9338c8a9a5SSteve French 9438c8a9a5SSteve French folio_detach_private(folio); 9538c8a9a5SSteve French folio_end_writeback(folio); 9638c8a9a5SSteve French } 9738c8a9a5SSteve French 9838c8a9a5SSteve French rcu_read_unlock(); 9938c8a9a5SSteve French } 10038c8a9a5SSteve French 10138c8a9a5SSteve French /* 10238c8a9a5SSteve French * Failure of write to server. 10338c8a9a5SSteve French */ 10438c8a9a5SSteve French void cifs_pages_write_failed(struct inode *inode, loff_t start, unsigned int len) 10538c8a9a5SSteve French { 10638c8a9a5SSteve French struct address_space *mapping = inode->i_mapping; 10738c8a9a5SSteve French struct folio *folio; 10838c8a9a5SSteve French pgoff_t end; 10938c8a9a5SSteve French 11038c8a9a5SSteve French XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 11138c8a9a5SSteve French 11238c8a9a5SSteve French if (!len) 11338c8a9a5SSteve French return; 11438c8a9a5SSteve French 11538c8a9a5SSteve French rcu_read_lock(); 11638c8a9a5SSteve French 11738c8a9a5SSteve French end = (start + len - 1) / PAGE_SIZE; 11838c8a9a5SSteve French xas_for_each(&xas, folio, end) { 11938c8a9a5SSteve French if (xas_retry(&xas, folio)) 12038c8a9a5SSteve French continue; 12138c8a9a5SSteve French if (!folio_test_writeback(folio)) { 12238c8a9a5SSteve French WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", 123d3c79235SDavid Howells len, start, folio->index, end); 12438c8a9a5SSteve French continue; 12538c8a9a5SSteve French } 12638c8a9a5SSteve French 12738c8a9a5SSteve French folio_set_error(folio); 12838c8a9a5SSteve French folio_end_writeback(folio); 12938c8a9a5SSteve French } 13038c8a9a5SSteve French 13138c8a9a5SSteve French rcu_read_unlock(); 13238c8a9a5SSteve French } 13338c8a9a5SSteve French 13438c8a9a5SSteve French /* 13538c8a9a5SSteve French * Redirty pages after a temporary failure. 13638c8a9a5SSteve French */ 13738c8a9a5SSteve French void cifs_pages_write_redirty(struct inode *inode, loff_t start, unsigned int len) 13838c8a9a5SSteve French { 13938c8a9a5SSteve French struct address_space *mapping = inode->i_mapping; 14038c8a9a5SSteve French struct folio *folio; 14138c8a9a5SSteve French pgoff_t end; 14238c8a9a5SSteve French 14338c8a9a5SSteve French XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE); 14438c8a9a5SSteve French 14538c8a9a5SSteve French if (!len) 14638c8a9a5SSteve French return; 14738c8a9a5SSteve French 14838c8a9a5SSteve French rcu_read_lock(); 14938c8a9a5SSteve French 15038c8a9a5SSteve French end = (start + len - 1) / PAGE_SIZE; 15138c8a9a5SSteve French xas_for_each(&xas, folio, end) { 15238c8a9a5SSteve French if (!folio_test_writeback(folio)) { 15338c8a9a5SSteve French WARN_ONCE(1, "bad %x @%llx page %lx %lx\n", 154d3c79235SDavid Howells len, start, folio->index, end); 15538c8a9a5SSteve French continue; 15638c8a9a5SSteve French } 15738c8a9a5SSteve French 15838c8a9a5SSteve French filemap_dirty_folio(folio->mapping, folio); 15938c8a9a5SSteve French folio_end_writeback(folio); 16038c8a9a5SSteve French } 16138c8a9a5SSteve French 16238c8a9a5SSteve French rcu_read_unlock(); 16338c8a9a5SSteve French } 16438c8a9a5SSteve French 16538c8a9a5SSteve French /* 16638c8a9a5SSteve French * Mark as invalid, all open files on tree connections since they 16738c8a9a5SSteve French * were closed when session to server was lost. 16838c8a9a5SSteve French */ 16938c8a9a5SSteve French void 17038c8a9a5SSteve French cifs_mark_open_files_invalid(struct cifs_tcon *tcon) 17138c8a9a5SSteve French { 17238c8a9a5SSteve French struct cifsFileInfo *open_file = NULL; 17338c8a9a5SSteve French struct list_head *tmp; 17438c8a9a5SSteve French struct list_head *tmp1; 17538c8a9a5SSteve French 17638c8a9a5SSteve French /* only send once per connect */ 17738c8a9a5SSteve French spin_lock(&tcon->tc_lock); 178cd743cfeSShyam Prasad N if (tcon->need_reconnect) 179cd743cfeSShyam Prasad N tcon->status = TID_NEED_RECON; 180cd743cfeSShyam Prasad N 18138c8a9a5SSteve French if (tcon->status != TID_NEED_RECON) { 18238c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 18338c8a9a5SSteve French return; 18438c8a9a5SSteve French } 18538c8a9a5SSteve French tcon->status = TID_IN_FILES_INVALIDATE; 18638c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 18738c8a9a5SSteve French 18838c8a9a5SSteve French /* list all files open on tree connection and mark them invalid */ 18938c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 19038c8a9a5SSteve French list_for_each_safe(tmp, tmp1, &tcon->openFileList) { 19138c8a9a5SSteve French open_file = list_entry(tmp, struct cifsFileInfo, tlist); 19238c8a9a5SSteve French open_file->invalidHandle = true; 19338c8a9a5SSteve French open_file->oplock_break_cancelled = true; 19438c8a9a5SSteve French } 19538c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 19638c8a9a5SSteve French 19738c8a9a5SSteve French invalidate_all_cached_dirs(tcon); 19838c8a9a5SSteve French spin_lock(&tcon->tc_lock); 19938c8a9a5SSteve French if (tcon->status == TID_IN_FILES_INVALIDATE) 20038c8a9a5SSteve French tcon->status = TID_NEED_TCON; 20138c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 20238c8a9a5SSteve French 20338c8a9a5SSteve French /* 20438c8a9a5SSteve French * BB Add call to invalidate_inodes(sb) for all superblocks mounted 20538c8a9a5SSteve French * to this tcon. 20638c8a9a5SSteve French */ 20738c8a9a5SSteve French } 20838c8a9a5SSteve French 2090fdada1eSDavid Howells static inline int cifs_convert_flags(unsigned int flags, int rdwr_for_fscache) 21038c8a9a5SSteve French { 21138c8a9a5SSteve French if ((flags & O_ACCMODE) == O_RDONLY) 21238c8a9a5SSteve French return GENERIC_READ; 21338c8a9a5SSteve French else if ((flags & O_ACCMODE) == O_WRONLY) 2140fdada1eSDavid Howells return rdwr_for_fscache == 1 ? (GENERIC_READ | GENERIC_WRITE) : GENERIC_WRITE; 21538c8a9a5SSteve French else if ((flags & O_ACCMODE) == O_RDWR) { 21638c8a9a5SSteve French /* GENERIC_ALL is too much permission to request 21738c8a9a5SSteve French can cause unnecessary access denied on create */ 21838c8a9a5SSteve French /* return GENERIC_ALL; */ 21938c8a9a5SSteve French return (GENERIC_READ | GENERIC_WRITE); 22038c8a9a5SSteve French } 22138c8a9a5SSteve French 22238c8a9a5SSteve French return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES | 22338c8a9a5SSteve French FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA | 22438c8a9a5SSteve French FILE_READ_DATA); 22538c8a9a5SSteve French } 22638c8a9a5SSteve French 22738c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 22838c8a9a5SSteve French static u32 cifs_posix_convert_flags(unsigned int flags) 22938c8a9a5SSteve French { 23038c8a9a5SSteve French u32 posix_flags = 0; 23138c8a9a5SSteve French 23238c8a9a5SSteve French if ((flags & O_ACCMODE) == O_RDONLY) 23338c8a9a5SSteve French posix_flags = SMB_O_RDONLY; 23438c8a9a5SSteve French else if ((flags & O_ACCMODE) == O_WRONLY) 23538c8a9a5SSteve French posix_flags = SMB_O_WRONLY; 23638c8a9a5SSteve French else if ((flags & O_ACCMODE) == O_RDWR) 23738c8a9a5SSteve French posix_flags = SMB_O_RDWR; 23838c8a9a5SSteve French 23938c8a9a5SSteve French if (flags & O_CREAT) { 24038c8a9a5SSteve French posix_flags |= SMB_O_CREAT; 24138c8a9a5SSteve French if (flags & O_EXCL) 24238c8a9a5SSteve French posix_flags |= SMB_O_EXCL; 24338c8a9a5SSteve French } else if (flags & O_EXCL) 24438c8a9a5SSteve French cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n", 24538c8a9a5SSteve French current->comm, current->tgid); 24638c8a9a5SSteve French 24738c8a9a5SSteve French if (flags & O_TRUNC) 24838c8a9a5SSteve French posix_flags |= SMB_O_TRUNC; 24938c8a9a5SSteve French /* be safe and imply O_SYNC for O_DSYNC */ 25038c8a9a5SSteve French if (flags & O_DSYNC) 25138c8a9a5SSteve French posix_flags |= SMB_O_SYNC; 25238c8a9a5SSteve French if (flags & O_DIRECTORY) 25338c8a9a5SSteve French posix_flags |= SMB_O_DIRECTORY; 25438c8a9a5SSteve French if (flags & O_NOFOLLOW) 25538c8a9a5SSteve French posix_flags |= SMB_O_NOFOLLOW; 25638c8a9a5SSteve French if (flags & O_DIRECT) 25738c8a9a5SSteve French posix_flags |= SMB_O_DIRECT; 25838c8a9a5SSteve French 25938c8a9a5SSteve French return posix_flags; 26038c8a9a5SSteve French } 26138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 26238c8a9a5SSteve French 26338c8a9a5SSteve French static inline int cifs_get_disposition(unsigned int flags) 26438c8a9a5SSteve French { 26538c8a9a5SSteve French if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 26638c8a9a5SSteve French return FILE_CREATE; 26738c8a9a5SSteve French else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 26838c8a9a5SSteve French return FILE_OVERWRITE_IF; 26938c8a9a5SSteve French else if ((flags & O_CREAT) == O_CREAT) 27038c8a9a5SSteve French return FILE_OPEN_IF; 27138c8a9a5SSteve French else if ((flags & O_TRUNC) == O_TRUNC) 27238c8a9a5SSteve French return FILE_OVERWRITE; 27338c8a9a5SSteve French else 27438c8a9a5SSteve French return FILE_OPEN; 27538c8a9a5SSteve French } 27638c8a9a5SSteve French 27738c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 27838c8a9a5SSteve French int cifs_posix_open(const char *full_path, struct inode **pinode, 27938c8a9a5SSteve French struct super_block *sb, int mode, unsigned int f_flags, 28038c8a9a5SSteve French __u32 *poplock, __u16 *pnetfid, unsigned int xid) 28138c8a9a5SSteve French { 28238c8a9a5SSteve French int rc; 28338c8a9a5SSteve French FILE_UNIX_BASIC_INFO *presp_data; 28438c8a9a5SSteve French __u32 posix_flags = 0; 28538c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 28638c8a9a5SSteve French struct cifs_fattr fattr; 28738c8a9a5SSteve French struct tcon_link *tlink; 28838c8a9a5SSteve French struct cifs_tcon *tcon; 28938c8a9a5SSteve French 29038c8a9a5SSteve French cifs_dbg(FYI, "posix open %s\n", full_path); 29138c8a9a5SSteve French 29238c8a9a5SSteve French presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); 29338c8a9a5SSteve French if (presp_data == NULL) 29438c8a9a5SSteve French return -ENOMEM; 29538c8a9a5SSteve French 29638c8a9a5SSteve French tlink = cifs_sb_tlink(cifs_sb); 29738c8a9a5SSteve French if (IS_ERR(tlink)) { 29838c8a9a5SSteve French rc = PTR_ERR(tlink); 29938c8a9a5SSteve French goto posix_open_ret; 30038c8a9a5SSteve French } 30138c8a9a5SSteve French 30238c8a9a5SSteve French tcon = tlink_tcon(tlink); 30338c8a9a5SSteve French mode &= ~current_umask(); 30438c8a9a5SSteve French 30538c8a9a5SSteve French posix_flags = cifs_posix_convert_flags(f_flags); 30638c8a9a5SSteve French rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, 30738c8a9a5SSteve French poplock, full_path, cifs_sb->local_nls, 30838c8a9a5SSteve French cifs_remap(cifs_sb)); 30938c8a9a5SSteve French cifs_put_tlink(tlink); 31038c8a9a5SSteve French 31138c8a9a5SSteve French if (rc) 31238c8a9a5SSteve French goto posix_open_ret; 31338c8a9a5SSteve French 31438c8a9a5SSteve French if (presp_data->Type == cpu_to_le32(-1)) 31538c8a9a5SSteve French goto posix_open_ret; /* open ok, caller does qpathinfo */ 31638c8a9a5SSteve French 31738c8a9a5SSteve French if (!pinode) 31838c8a9a5SSteve French goto posix_open_ret; /* caller does not need info */ 31938c8a9a5SSteve French 32038c8a9a5SSteve French cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); 32138c8a9a5SSteve French 32238c8a9a5SSteve French /* get new inode and set it up */ 32338c8a9a5SSteve French if (*pinode == NULL) { 32438c8a9a5SSteve French cifs_fill_uniqueid(sb, &fattr); 32538c8a9a5SSteve French *pinode = cifs_iget(sb, &fattr); 32638c8a9a5SSteve French if (!*pinode) { 32738c8a9a5SSteve French rc = -ENOMEM; 32838c8a9a5SSteve French goto posix_open_ret; 32938c8a9a5SSteve French } 33038c8a9a5SSteve French } else { 33138c8a9a5SSteve French cifs_revalidate_mapping(*pinode); 3329179aa27SBharath SM rc = cifs_fattr_to_inode(*pinode, &fattr, false); 33338c8a9a5SSteve French } 33438c8a9a5SSteve French 33538c8a9a5SSteve French posix_open_ret: 33638c8a9a5SSteve French kfree(presp_data); 33738c8a9a5SSteve French return rc; 33838c8a9a5SSteve French } 33938c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 34038c8a9a5SSteve French 34138c8a9a5SSteve French static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, 34238c8a9a5SSteve French struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock, 34338c8a9a5SSteve French struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf) 34438c8a9a5SSteve French { 34538c8a9a5SSteve French int rc; 34638c8a9a5SSteve French int desired_access; 34738c8a9a5SSteve French int disposition; 34838c8a9a5SSteve French int create_options = CREATE_NOT_DIR; 34938c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 35038c8a9a5SSteve French struct cifs_open_parms oparms; 3510fdada1eSDavid Howells int rdwr_for_fscache = 0; 35238c8a9a5SSteve French 35338c8a9a5SSteve French if (!server->ops->open) 35438c8a9a5SSteve French return -ENOSYS; 35538c8a9a5SSteve French 3560fdada1eSDavid Howells /* If we're caching, we need to be able to fill in around partial writes. */ 3570fdada1eSDavid Howells if (cifs_fscache_enabled(inode) && (f_flags & O_ACCMODE) == O_WRONLY) 3580fdada1eSDavid Howells rdwr_for_fscache = 1; 3590fdada1eSDavid Howells 3600fdada1eSDavid Howells desired_access = cifs_convert_flags(f_flags, rdwr_for_fscache); 36138c8a9a5SSteve French 36238c8a9a5SSteve French /********************************************************************* 36338c8a9a5SSteve French * open flag mapping table: 36438c8a9a5SSteve French * 36538c8a9a5SSteve French * POSIX Flag CIFS Disposition 36638c8a9a5SSteve French * ---------- ---------------- 36738c8a9a5SSteve French * O_CREAT FILE_OPEN_IF 36838c8a9a5SSteve French * O_CREAT | O_EXCL FILE_CREATE 36938c8a9a5SSteve French * O_CREAT | O_TRUNC FILE_OVERWRITE_IF 37038c8a9a5SSteve French * O_TRUNC FILE_OVERWRITE 37138c8a9a5SSteve French * none of the above FILE_OPEN 37238c8a9a5SSteve French * 37338c8a9a5SSteve French * Note that there is not a direct match between disposition 37438c8a9a5SSteve French * FILE_SUPERSEDE (ie create whether or not file exists although 37538c8a9a5SSteve French * O_CREAT | O_TRUNC is similar but truncates the existing 37638c8a9a5SSteve French * file rather than creating a new file as FILE_SUPERSEDE does 37738c8a9a5SSteve French * (which uses the attributes / metadata passed in on open call) 37838c8a9a5SSteve French *? 37938c8a9a5SSteve French *? O_SYNC is a reasonable match to CIFS writethrough flag 38038c8a9a5SSteve French *? and the read write flags match reasonably. O_LARGEFILE 38138c8a9a5SSteve French *? is irrelevant because largefile support is always used 38238c8a9a5SSteve French *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, 38338c8a9a5SSteve French * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation 38438c8a9a5SSteve French *********************************************************************/ 38538c8a9a5SSteve French 38638c8a9a5SSteve French disposition = cifs_get_disposition(f_flags); 38738c8a9a5SSteve French 38838c8a9a5SSteve French /* BB pass O_SYNC flag through on file attributes .. BB */ 38938c8a9a5SSteve French 39038c8a9a5SSteve French /* O_SYNC also has bit for O_DSYNC so following check picks up either */ 39138c8a9a5SSteve French if (f_flags & O_SYNC) 39238c8a9a5SSteve French create_options |= CREATE_WRITE_THROUGH; 39338c8a9a5SSteve French 39438c8a9a5SSteve French if (f_flags & O_DIRECT) 39538c8a9a5SSteve French create_options |= CREATE_NO_BUFFER; 39638c8a9a5SSteve French 3970fdada1eSDavid Howells retry_open: 39838c8a9a5SSteve French oparms = (struct cifs_open_parms) { 39938c8a9a5SSteve French .tcon = tcon, 40038c8a9a5SSteve French .cifs_sb = cifs_sb, 40138c8a9a5SSteve French .desired_access = desired_access, 40238c8a9a5SSteve French .create_options = cifs_create_options(cifs_sb, create_options), 40338c8a9a5SSteve French .disposition = disposition, 40438c8a9a5SSteve French .path = full_path, 40538c8a9a5SSteve French .fid = fid, 40638c8a9a5SSteve French }; 40738c8a9a5SSteve French 40838c8a9a5SSteve French rc = server->ops->open(xid, &oparms, oplock, buf); 4090fdada1eSDavid Howells if (rc) { 4100fdada1eSDavid Howells if (rc == -EACCES && rdwr_for_fscache == 1) { 4110fdada1eSDavid Howells desired_access = cifs_convert_flags(f_flags, 0); 4120fdada1eSDavid Howells rdwr_for_fscache = 2; 4130fdada1eSDavid Howells goto retry_open; 4140fdada1eSDavid Howells } 41538c8a9a5SSteve French return rc; 4160fdada1eSDavid Howells } 4170fdada1eSDavid Howells if (rdwr_for_fscache == 2) 4180fdada1eSDavid Howells cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 41938c8a9a5SSteve French 42038c8a9a5SSteve French /* TODO: Add support for calling posix query info but with passing in fid */ 42138c8a9a5SSteve French if (tcon->unix_ext) 42238c8a9a5SSteve French rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, 42338c8a9a5SSteve French xid); 42438c8a9a5SSteve French else 42538c8a9a5SSteve French rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, 42638c8a9a5SSteve French xid, fid); 42738c8a9a5SSteve French 42838c8a9a5SSteve French if (rc) { 42938c8a9a5SSteve French server->ops->close(xid, tcon, fid); 43038c8a9a5SSteve French if (rc == -ESTALE) 43138c8a9a5SSteve French rc = -EOPENSTALE; 43238c8a9a5SSteve French } 43338c8a9a5SSteve French 43438c8a9a5SSteve French return rc; 43538c8a9a5SSteve French } 43638c8a9a5SSteve French 43738c8a9a5SSteve French static bool 43838c8a9a5SSteve French cifs_has_mand_locks(struct cifsInodeInfo *cinode) 43938c8a9a5SSteve French { 44038c8a9a5SSteve French struct cifs_fid_locks *cur; 44138c8a9a5SSteve French bool has_locks = false; 44238c8a9a5SSteve French 44338c8a9a5SSteve French down_read(&cinode->lock_sem); 44438c8a9a5SSteve French list_for_each_entry(cur, &cinode->llist, llist) { 44538c8a9a5SSteve French if (!list_empty(&cur->locks)) { 44638c8a9a5SSteve French has_locks = true; 44738c8a9a5SSteve French break; 44838c8a9a5SSteve French } 44938c8a9a5SSteve French } 45038c8a9a5SSteve French up_read(&cinode->lock_sem); 45138c8a9a5SSteve French return has_locks; 45238c8a9a5SSteve French } 45338c8a9a5SSteve French 45438c8a9a5SSteve French void 45538c8a9a5SSteve French cifs_down_write(struct rw_semaphore *sem) 45638c8a9a5SSteve French { 45738c8a9a5SSteve French while (!down_write_trylock(sem)) 45838c8a9a5SSteve French msleep(10); 45938c8a9a5SSteve French } 46038c8a9a5SSteve French 46138c8a9a5SSteve French static void cifsFileInfo_put_work(struct work_struct *work); 4626f17163bSRitvik Budhiraja void serverclose_work(struct work_struct *work); 46338c8a9a5SSteve French 46438c8a9a5SSteve French struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file, 46538c8a9a5SSteve French struct tcon_link *tlink, __u32 oplock, 46638c8a9a5SSteve French const char *symlink_target) 46738c8a9a5SSteve French { 46838c8a9a5SSteve French struct dentry *dentry = file_dentry(file); 46938c8a9a5SSteve French struct inode *inode = d_inode(dentry); 47038c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 47138c8a9a5SSteve French struct cifsFileInfo *cfile; 47238c8a9a5SSteve French struct cifs_fid_locks *fdlocks; 47338c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(tlink); 47438c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 47538c8a9a5SSteve French 47638c8a9a5SSteve French cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); 47738c8a9a5SSteve French if (cfile == NULL) 47838c8a9a5SSteve French return cfile; 47938c8a9a5SSteve French 48038c8a9a5SSteve French fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL); 48138c8a9a5SSteve French if (!fdlocks) { 48238c8a9a5SSteve French kfree(cfile); 48338c8a9a5SSteve French return NULL; 48438c8a9a5SSteve French } 48538c8a9a5SSteve French 48638c8a9a5SSteve French if (symlink_target) { 48738c8a9a5SSteve French cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL); 48838c8a9a5SSteve French if (!cfile->symlink_target) { 48938c8a9a5SSteve French kfree(fdlocks); 49038c8a9a5SSteve French kfree(cfile); 49138c8a9a5SSteve French return NULL; 49238c8a9a5SSteve French } 49338c8a9a5SSteve French } 49438c8a9a5SSteve French 49538c8a9a5SSteve French INIT_LIST_HEAD(&fdlocks->locks); 49638c8a9a5SSteve French fdlocks->cfile = cfile; 49738c8a9a5SSteve French cfile->llist = fdlocks; 49838c8a9a5SSteve French 49938c8a9a5SSteve French cfile->count = 1; 50038c8a9a5SSteve French cfile->pid = current->tgid; 50138c8a9a5SSteve French cfile->uid = current_fsuid(); 50238c8a9a5SSteve French cfile->dentry = dget(dentry); 50338c8a9a5SSteve French cfile->f_flags = file->f_flags; 50438c8a9a5SSteve French cfile->invalidHandle = false; 50538c8a9a5SSteve French cfile->deferred_close_scheduled = false; 50638c8a9a5SSteve French cfile->tlink = cifs_get_tlink(tlink); 50738c8a9a5SSteve French INIT_WORK(&cfile->oplock_break, cifs_oplock_break); 50838c8a9a5SSteve French INIT_WORK(&cfile->put, cifsFileInfo_put_work); 5096f17163bSRitvik Budhiraja INIT_WORK(&cfile->serverclose, serverclose_work); 51038c8a9a5SSteve French INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close); 51138c8a9a5SSteve French mutex_init(&cfile->fh_mutex); 51238c8a9a5SSteve French spin_lock_init(&cfile->file_info_lock); 51338c8a9a5SSteve French 51438c8a9a5SSteve French cifs_sb_active(inode->i_sb); 51538c8a9a5SSteve French 51638c8a9a5SSteve French /* 51738c8a9a5SSteve French * If the server returned a read oplock and we have mandatory brlocks, 51838c8a9a5SSteve French * set oplock level to None. 51938c8a9a5SSteve French */ 52038c8a9a5SSteve French if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 52138c8a9a5SSteve French cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 52238c8a9a5SSteve French oplock = 0; 52338c8a9a5SSteve French } 52438c8a9a5SSteve French 52538c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 52638c8a9a5SSteve French list_add(&fdlocks->llist, &cinode->llist); 52738c8a9a5SSteve French up_write(&cinode->lock_sem); 52838c8a9a5SSteve French 52938c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 53038c8a9a5SSteve French if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) 53138c8a9a5SSteve French oplock = fid->pending_open->oplock; 53238c8a9a5SSteve French list_del(&fid->pending_open->olist); 53338c8a9a5SSteve French 53438c8a9a5SSteve French fid->purge_cache = false; 53538c8a9a5SSteve French server->ops->set_fid(cfile, fid, oplock); 53638c8a9a5SSteve French 53738c8a9a5SSteve French list_add(&cfile->tlist, &tcon->openFileList); 53838c8a9a5SSteve French atomic_inc(&tcon->num_local_opens); 53938c8a9a5SSteve French 54038c8a9a5SSteve French /* if readable file instance put first in list*/ 54138c8a9a5SSteve French spin_lock(&cinode->open_file_lock); 54238c8a9a5SSteve French if (file->f_mode & FMODE_READ) 54338c8a9a5SSteve French list_add(&cfile->flist, &cinode->openFileList); 54438c8a9a5SSteve French else 54538c8a9a5SSteve French list_add_tail(&cfile->flist, &cinode->openFileList); 54638c8a9a5SSteve French spin_unlock(&cinode->open_file_lock); 54738c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 54838c8a9a5SSteve French 54938c8a9a5SSteve French if (fid->purge_cache) 55038c8a9a5SSteve French cifs_zap_mapping(inode); 55138c8a9a5SSteve French 55238c8a9a5SSteve French file->private_data = cfile; 55338c8a9a5SSteve French return cfile; 55438c8a9a5SSteve French } 55538c8a9a5SSteve French 55638c8a9a5SSteve French struct cifsFileInfo * 55738c8a9a5SSteve French cifsFileInfo_get(struct cifsFileInfo *cifs_file) 55838c8a9a5SSteve French { 55938c8a9a5SSteve French spin_lock(&cifs_file->file_info_lock); 56038c8a9a5SSteve French cifsFileInfo_get_locked(cifs_file); 56138c8a9a5SSteve French spin_unlock(&cifs_file->file_info_lock); 56238c8a9a5SSteve French return cifs_file; 56338c8a9a5SSteve French } 56438c8a9a5SSteve French 56538c8a9a5SSteve French static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file) 56638c8a9a5SSteve French { 56738c8a9a5SSteve French struct inode *inode = d_inode(cifs_file->dentry); 56838c8a9a5SSteve French struct cifsInodeInfo *cifsi = CIFS_I(inode); 56938c8a9a5SSteve French struct cifsLockInfo *li, *tmp; 57038c8a9a5SSteve French struct super_block *sb = inode->i_sb; 57138c8a9a5SSteve French 57238c8a9a5SSteve French /* 57338c8a9a5SSteve French * Delete any outstanding lock records. We'll lose them when the file 57438c8a9a5SSteve French * is closed anyway. 57538c8a9a5SSteve French */ 57638c8a9a5SSteve French cifs_down_write(&cifsi->lock_sem); 57738c8a9a5SSteve French list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) { 57838c8a9a5SSteve French list_del(&li->llist); 57938c8a9a5SSteve French cifs_del_lock_waiters(li); 58038c8a9a5SSteve French kfree(li); 58138c8a9a5SSteve French } 58238c8a9a5SSteve French list_del(&cifs_file->llist->llist); 58338c8a9a5SSteve French kfree(cifs_file->llist); 58438c8a9a5SSteve French up_write(&cifsi->lock_sem); 58538c8a9a5SSteve French 58638c8a9a5SSteve French cifs_put_tlink(cifs_file->tlink); 58738c8a9a5SSteve French dput(cifs_file->dentry); 58838c8a9a5SSteve French cifs_sb_deactive(sb); 58938c8a9a5SSteve French kfree(cifs_file->symlink_target); 59038c8a9a5SSteve French kfree(cifs_file); 59138c8a9a5SSteve French } 59238c8a9a5SSteve French 59338c8a9a5SSteve French static void cifsFileInfo_put_work(struct work_struct *work) 59438c8a9a5SSteve French { 59538c8a9a5SSteve French struct cifsFileInfo *cifs_file = container_of(work, 59638c8a9a5SSteve French struct cifsFileInfo, put); 59738c8a9a5SSteve French 59838c8a9a5SSteve French cifsFileInfo_put_final(cifs_file); 59938c8a9a5SSteve French } 60038c8a9a5SSteve French 6016f17163bSRitvik Budhiraja void serverclose_work(struct work_struct *work) 6026f17163bSRitvik Budhiraja { 6036f17163bSRitvik Budhiraja struct cifsFileInfo *cifs_file = container_of(work, 6046f17163bSRitvik Budhiraja struct cifsFileInfo, serverclose); 6056f17163bSRitvik Budhiraja 6066f17163bSRitvik Budhiraja struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 6076f17163bSRitvik Budhiraja 6086f17163bSRitvik Budhiraja struct TCP_Server_Info *server = tcon->ses->server; 6096f17163bSRitvik Budhiraja int rc = 0; 6106f17163bSRitvik Budhiraja int retries = 0; 6116f17163bSRitvik Budhiraja int MAX_RETRIES = 4; 6126f17163bSRitvik Budhiraja 6136f17163bSRitvik Budhiraja do { 6146f17163bSRitvik Budhiraja if (server->ops->close_getattr) 6156f17163bSRitvik Budhiraja rc = server->ops->close_getattr(0, tcon, cifs_file); 6166f17163bSRitvik Budhiraja else if (server->ops->close) 6176f17163bSRitvik Budhiraja rc = server->ops->close(0, tcon, &cifs_file->fid); 6186f17163bSRitvik Budhiraja 6196f17163bSRitvik Budhiraja if (rc == -EBUSY || rc == -EAGAIN) { 6206f17163bSRitvik Budhiraja retries++; 6216f17163bSRitvik Budhiraja msleep(250); 6226f17163bSRitvik Budhiraja } 6236f17163bSRitvik Budhiraja } while ((rc == -EBUSY || rc == -EAGAIN) && (retries < MAX_RETRIES) 6246f17163bSRitvik Budhiraja ); 6256f17163bSRitvik Budhiraja 6266f17163bSRitvik Budhiraja if (retries == MAX_RETRIES) 6276f17163bSRitvik Budhiraja pr_warn("Serverclose failed %d times, giving up\n", MAX_RETRIES); 6286f17163bSRitvik Budhiraja 6296f17163bSRitvik Budhiraja if (cifs_file->offload) 6306f17163bSRitvik Budhiraja queue_work(fileinfo_put_wq, &cifs_file->put); 6316f17163bSRitvik Budhiraja else 6326f17163bSRitvik Budhiraja cifsFileInfo_put_final(cifs_file); 6336f17163bSRitvik Budhiraja } 6346f17163bSRitvik Budhiraja 63538c8a9a5SSteve French /** 63638c8a9a5SSteve French * cifsFileInfo_put - release a reference of file priv data 63738c8a9a5SSteve French * 63838c8a9a5SSteve French * Always potentially wait for oplock handler. See _cifsFileInfo_put(). 63938c8a9a5SSteve French * 64038c8a9a5SSteve French * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 64138c8a9a5SSteve French */ 64238c8a9a5SSteve French void cifsFileInfo_put(struct cifsFileInfo *cifs_file) 64338c8a9a5SSteve French { 64438c8a9a5SSteve French _cifsFileInfo_put(cifs_file, true, true); 64538c8a9a5SSteve French } 64638c8a9a5SSteve French 64738c8a9a5SSteve French /** 64838c8a9a5SSteve French * _cifsFileInfo_put - release a reference of file priv data 64938c8a9a5SSteve French * 65038c8a9a5SSteve French * This may involve closing the filehandle @cifs_file out on the 65138c8a9a5SSteve French * server. Must be called without holding tcon->open_file_lock, 65238c8a9a5SSteve French * cinode->open_file_lock and cifs_file->file_info_lock. 65338c8a9a5SSteve French * 65438c8a9a5SSteve French * If @wait_for_oplock_handler is true and we are releasing the last 65538c8a9a5SSteve French * reference, wait for any running oplock break handler of the file 65638c8a9a5SSteve French * and cancel any pending one. 65738c8a9a5SSteve French * 65838c8a9a5SSteve French * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file 65938c8a9a5SSteve French * @wait_oplock_handler: must be false if called from oplock_break_handler 66038c8a9a5SSteve French * @offload: not offloaded on close and oplock breaks 66138c8a9a5SSteve French * 66238c8a9a5SSteve French */ 66338c8a9a5SSteve French void _cifsFileInfo_put(struct cifsFileInfo *cifs_file, 66438c8a9a5SSteve French bool wait_oplock_handler, bool offload) 66538c8a9a5SSteve French { 66638c8a9a5SSteve French struct inode *inode = d_inode(cifs_file->dentry); 66738c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); 66838c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 66938c8a9a5SSteve French struct cifsInodeInfo *cifsi = CIFS_I(inode); 67038c8a9a5SSteve French struct super_block *sb = inode->i_sb; 67138c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(sb); 67238c8a9a5SSteve French struct cifs_fid fid = {}; 67338c8a9a5SSteve French struct cifs_pending_open open; 67438c8a9a5SSteve French bool oplock_break_cancelled; 6756f17163bSRitvik Budhiraja bool serverclose_offloaded = false; 67638c8a9a5SSteve French 67738c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 67838c8a9a5SSteve French spin_lock(&cifsi->open_file_lock); 67938c8a9a5SSteve French spin_lock(&cifs_file->file_info_lock); 6806f17163bSRitvik Budhiraja 6816f17163bSRitvik Budhiraja cifs_file->offload = offload; 68238c8a9a5SSteve French if (--cifs_file->count > 0) { 68338c8a9a5SSteve French spin_unlock(&cifs_file->file_info_lock); 68438c8a9a5SSteve French spin_unlock(&cifsi->open_file_lock); 68538c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 68638c8a9a5SSteve French return; 68738c8a9a5SSteve French } 68838c8a9a5SSteve French spin_unlock(&cifs_file->file_info_lock); 68938c8a9a5SSteve French 69038c8a9a5SSteve French if (server->ops->get_lease_key) 69138c8a9a5SSteve French server->ops->get_lease_key(inode, &fid); 69238c8a9a5SSteve French 69338c8a9a5SSteve French /* store open in pending opens to make sure we don't miss lease break */ 69438c8a9a5SSteve French cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open); 69538c8a9a5SSteve French 69638c8a9a5SSteve French /* remove it from the lists */ 69738c8a9a5SSteve French list_del(&cifs_file->flist); 69838c8a9a5SSteve French list_del(&cifs_file->tlist); 69938c8a9a5SSteve French atomic_dec(&tcon->num_local_opens); 70038c8a9a5SSteve French 70138c8a9a5SSteve French if (list_empty(&cifsi->openFileList)) { 70238c8a9a5SSteve French cifs_dbg(FYI, "closing last open instance for inode %p\n", 70338c8a9a5SSteve French d_inode(cifs_file->dentry)); 70438c8a9a5SSteve French /* 70538c8a9a5SSteve French * In strict cache mode we need invalidate mapping on the last 70638c8a9a5SSteve French * close because it may cause a error when we open this file 70738c8a9a5SSteve French * again and get at least level II oplock. 70838c8a9a5SSteve French */ 70938c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) 71038c8a9a5SSteve French set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags); 71138c8a9a5SSteve French cifs_set_oplock_level(cifsi, 0); 71238c8a9a5SSteve French } 71338c8a9a5SSteve French 71438c8a9a5SSteve French spin_unlock(&cifsi->open_file_lock); 71538c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 71638c8a9a5SSteve French 71738c8a9a5SSteve French oplock_break_cancelled = wait_oplock_handler ? 71838c8a9a5SSteve French cancel_work_sync(&cifs_file->oplock_break) : false; 71938c8a9a5SSteve French 72038c8a9a5SSteve French if (!tcon->need_reconnect && !cifs_file->invalidHandle) { 72138c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 72238c8a9a5SSteve French unsigned int xid; 7236f17163bSRitvik Budhiraja int rc = 0; 72438c8a9a5SSteve French 72538c8a9a5SSteve French xid = get_xid(); 72638c8a9a5SSteve French if (server->ops->close_getattr) 7276f17163bSRitvik Budhiraja rc = server->ops->close_getattr(xid, tcon, cifs_file); 72838c8a9a5SSteve French else if (server->ops->close) 7296f17163bSRitvik Budhiraja rc = server->ops->close(xid, tcon, &cifs_file->fid); 73038c8a9a5SSteve French _free_xid(xid); 7316f17163bSRitvik Budhiraja 7326f17163bSRitvik Budhiraja if (rc == -EBUSY || rc == -EAGAIN) { 7336f17163bSRitvik Budhiraja // Server close failed, hence offloading it as an async op 7346f17163bSRitvik Budhiraja queue_work(serverclose_wq, &cifs_file->serverclose); 7356f17163bSRitvik Budhiraja serverclose_offloaded = true; 7366f17163bSRitvik Budhiraja } 73738c8a9a5SSteve French } 73838c8a9a5SSteve French 73938c8a9a5SSteve French if (oplock_break_cancelled) 74038c8a9a5SSteve French cifs_done_oplock_break(cifsi); 74138c8a9a5SSteve French 74238c8a9a5SSteve French cifs_del_pending_open(&open); 74338c8a9a5SSteve French 7446f17163bSRitvik Budhiraja // if serverclose has been offloaded to wq (on failure), it will 7456f17163bSRitvik Budhiraja // handle offloading put as well. If serverclose not offloaded, 7466f17163bSRitvik Budhiraja // we need to handle offloading put here. 7476f17163bSRitvik Budhiraja if (!serverclose_offloaded) { 74838c8a9a5SSteve French if (offload) 74938c8a9a5SSteve French queue_work(fileinfo_put_wq, &cifs_file->put); 75038c8a9a5SSteve French else 75138c8a9a5SSteve French cifsFileInfo_put_final(cifs_file); 75238c8a9a5SSteve French } 7536f17163bSRitvik Budhiraja } 75438c8a9a5SSteve French 75538c8a9a5SSteve French int cifs_open(struct inode *inode, struct file *file) 75638c8a9a5SSteve French 75738c8a9a5SSteve French { 75838c8a9a5SSteve French int rc = -EACCES; 75938c8a9a5SSteve French unsigned int xid; 76038c8a9a5SSteve French __u32 oplock; 76138c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 76238c8a9a5SSteve French struct TCP_Server_Info *server; 76338c8a9a5SSteve French struct cifs_tcon *tcon; 76438c8a9a5SSteve French struct tcon_link *tlink; 76538c8a9a5SSteve French struct cifsFileInfo *cfile = NULL; 76638c8a9a5SSteve French void *page; 76738c8a9a5SSteve French const char *full_path; 76838c8a9a5SSteve French bool posix_open_ok = false; 76938c8a9a5SSteve French struct cifs_fid fid = {}; 77038c8a9a5SSteve French struct cifs_pending_open open; 77138c8a9a5SSteve French struct cifs_open_info_data data = {}; 77238c8a9a5SSteve French 77338c8a9a5SSteve French xid = get_xid(); 77438c8a9a5SSteve French 77538c8a9a5SSteve French cifs_sb = CIFS_SB(inode->i_sb); 77638c8a9a5SSteve French if (unlikely(cifs_forced_shutdown(cifs_sb))) { 77738c8a9a5SSteve French free_xid(xid); 77838c8a9a5SSteve French return -EIO; 77938c8a9a5SSteve French } 78038c8a9a5SSteve French 78138c8a9a5SSteve French tlink = cifs_sb_tlink(cifs_sb); 78238c8a9a5SSteve French if (IS_ERR(tlink)) { 78338c8a9a5SSteve French free_xid(xid); 78438c8a9a5SSteve French return PTR_ERR(tlink); 78538c8a9a5SSteve French } 78638c8a9a5SSteve French tcon = tlink_tcon(tlink); 78738c8a9a5SSteve French server = tcon->ses->server; 78838c8a9a5SSteve French 78938c8a9a5SSteve French page = alloc_dentry_path(); 79038c8a9a5SSteve French full_path = build_path_from_dentry(file_dentry(file), page); 79138c8a9a5SSteve French if (IS_ERR(full_path)) { 79238c8a9a5SSteve French rc = PTR_ERR(full_path); 79338c8a9a5SSteve French goto out; 79438c8a9a5SSteve French } 79538c8a9a5SSteve French 79638c8a9a5SSteve French cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n", 79738c8a9a5SSteve French inode, file->f_flags, full_path); 79838c8a9a5SSteve French 79938c8a9a5SSteve French if (file->f_flags & O_DIRECT && 80038c8a9a5SSteve French cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { 80138c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) 80238c8a9a5SSteve French file->f_op = &cifs_file_direct_nobrl_ops; 80338c8a9a5SSteve French else 80438c8a9a5SSteve French file->f_op = &cifs_file_direct_ops; 80538c8a9a5SSteve French } 80638c8a9a5SSteve French 80738c8a9a5SSteve French /* Get the cached handle as SMB2 close is deferred */ 80838c8a9a5SSteve French rc = cifs_get_readable_path(tcon, full_path, &cfile); 80938c8a9a5SSteve French if (rc == 0) { 81038c8a9a5SSteve French if (file->f_flags == cfile->f_flags) { 81138c8a9a5SSteve French file->private_data = cfile; 81238c8a9a5SSteve French spin_lock(&CIFS_I(inode)->deferred_lock); 81338c8a9a5SSteve French cifs_del_deferred_close(cfile); 81438c8a9a5SSteve French spin_unlock(&CIFS_I(inode)->deferred_lock); 81538c8a9a5SSteve French goto use_cache; 81638c8a9a5SSteve French } else { 81738c8a9a5SSteve French _cifsFileInfo_put(cfile, true, false); 81838c8a9a5SSteve French } 81938c8a9a5SSteve French } 82038c8a9a5SSteve French 82138c8a9a5SSteve French if (server->oplocks) 82238c8a9a5SSteve French oplock = REQ_OPLOCK; 82338c8a9a5SSteve French else 82438c8a9a5SSteve French oplock = 0; 82538c8a9a5SSteve French 82638c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 82738c8a9a5SSteve French if (!tcon->broken_posix_open && tcon->unix_ext && 82838c8a9a5SSteve French cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP & 82938c8a9a5SSteve French le64_to_cpu(tcon->fsUnixInfo.Capability))) { 83038c8a9a5SSteve French /* can not refresh inode info since size could be stale */ 83138c8a9a5SSteve French rc = cifs_posix_open(full_path, &inode, inode->i_sb, 83238c8a9a5SSteve French cifs_sb->ctx->file_mode /* ignored */, 83338c8a9a5SSteve French file->f_flags, &oplock, &fid.netfid, xid); 83438c8a9a5SSteve French if (rc == 0) { 83538c8a9a5SSteve French cifs_dbg(FYI, "posix open succeeded\n"); 83638c8a9a5SSteve French posix_open_ok = true; 83738c8a9a5SSteve French } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { 83838c8a9a5SSteve French if (tcon->ses->serverNOS) 83938c8a9a5SSteve French 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", 84038c8a9a5SSteve French tcon->ses->ip_addr, 84138c8a9a5SSteve French tcon->ses->serverNOS); 84238c8a9a5SSteve French tcon->broken_posix_open = true; 84338c8a9a5SSteve French } else if ((rc != -EIO) && (rc != -EREMOTE) && 84438c8a9a5SSteve French (rc != -EOPNOTSUPP)) /* path not found or net err */ 84538c8a9a5SSteve French goto out; 84638c8a9a5SSteve French /* 84738c8a9a5SSteve French * Else fallthrough to retry open the old way on network i/o 84838c8a9a5SSteve French * or DFS errors. 84938c8a9a5SSteve French */ 85038c8a9a5SSteve French } 85138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 85238c8a9a5SSteve French 85338c8a9a5SSteve French if (server->ops->get_lease_key) 85438c8a9a5SSteve French server->ops->get_lease_key(inode, &fid); 85538c8a9a5SSteve French 85638c8a9a5SSteve French cifs_add_pending_open(&fid, tlink, &open); 85738c8a9a5SSteve French 85838c8a9a5SSteve French if (!posix_open_ok) { 85938c8a9a5SSteve French if (server->ops->get_lease_key) 86038c8a9a5SSteve French server->ops->get_lease_key(inode, &fid); 86138c8a9a5SSteve French 86238c8a9a5SSteve French rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid, 86338c8a9a5SSteve French xid, &data); 86438c8a9a5SSteve French if (rc) { 86538c8a9a5SSteve French cifs_del_pending_open(&open); 86638c8a9a5SSteve French goto out; 86738c8a9a5SSteve French } 86838c8a9a5SSteve French } 86938c8a9a5SSteve French 87038c8a9a5SSteve French cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target); 87138c8a9a5SSteve French if (cfile == NULL) { 87238c8a9a5SSteve French if (server->ops->close) 87338c8a9a5SSteve French server->ops->close(xid, tcon, &fid); 87438c8a9a5SSteve French cifs_del_pending_open(&open); 87538c8a9a5SSteve French rc = -ENOMEM; 87638c8a9a5SSteve French goto out; 87738c8a9a5SSteve French } 87838c8a9a5SSteve French 87938c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 88038c8a9a5SSteve French if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { 88138c8a9a5SSteve French /* 88238c8a9a5SSteve French * Time to set mode which we can not set earlier due to 88338c8a9a5SSteve French * problems creating new read-only files. 88438c8a9a5SSteve French */ 88538c8a9a5SSteve French struct cifs_unix_set_info_args args = { 88638c8a9a5SSteve French .mode = inode->i_mode, 88738c8a9a5SSteve French .uid = INVALID_UID, /* no change */ 88838c8a9a5SSteve French .gid = INVALID_GID, /* no change */ 88938c8a9a5SSteve French .ctime = NO_CHANGE_64, 89038c8a9a5SSteve French .atime = NO_CHANGE_64, 89138c8a9a5SSteve French .mtime = NO_CHANGE_64, 89238c8a9a5SSteve French .device = 0, 89338c8a9a5SSteve French }; 89438c8a9a5SSteve French CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid, 89538c8a9a5SSteve French cfile->pid); 89638c8a9a5SSteve French } 89738c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 89838c8a9a5SSteve French 89938c8a9a5SSteve French use_cache: 90038c8a9a5SSteve French fscache_use_cookie(cifs_inode_cookie(file_inode(file)), 90138c8a9a5SSteve French file->f_mode & FMODE_WRITE); 9020fdada1eSDavid Howells if (!(file->f_flags & O_DIRECT)) 9030fdada1eSDavid Howells goto out; 9040fdada1eSDavid Howells if ((file->f_flags & (O_ACCMODE | O_APPEND)) == O_RDONLY) 9050fdada1eSDavid Howells goto out; 9060fdada1eSDavid Howells cifs_invalidate_cache(file_inode(file), FSCACHE_INVAL_DIO_WRITE); 90738c8a9a5SSteve French 90838c8a9a5SSteve French out: 90938c8a9a5SSteve French free_dentry_path(page); 91038c8a9a5SSteve French free_xid(xid); 91138c8a9a5SSteve French cifs_put_tlink(tlink); 91238c8a9a5SSteve French cifs_free_open_info(&data); 91338c8a9a5SSteve French return rc; 91438c8a9a5SSteve French } 91538c8a9a5SSteve French 91638c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 91738c8a9a5SSteve French static int cifs_push_posix_locks(struct cifsFileInfo *cfile); 91838c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 91938c8a9a5SSteve French 92038c8a9a5SSteve French /* 92138c8a9a5SSteve French * Try to reacquire byte range locks that were released when session 92238c8a9a5SSteve French * to server was lost. 92338c8a9a5SSteve French */ 92438c8a9a5SSteve French static int 92538c8a9a5SSteve French cifs_relock_file(struct cifsFileInfo *cfile) 92638c8a9a5SSteve French { 92738c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 92838c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 92938c8a9a5SSteve French int rc = 0; 93038c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 93138c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 93238c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 93338c8a9a5SSteve French 93438c8a9a5SSteve French down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING); 93538c8a9a5SSteve French if (cinode->can_cache_brlcks) { 93638c8a9a5SSteve French /* can cache locks - no need to relock */ 93738c8a9a5SSteve French up_read(&cinode->lock_sem); 93838c8a9a5SSteve French return rc; 93938c8a9a5SSteve French } 94038c8a9a5SSteve French 94138c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 94238c8a9a5SSteve French if (cap_unix(tcon->ses) && 94338c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 94438c8a9a5SSteve French ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 94538c8a9a5SSteve French rc = cifs_push_posix_locks(cfile); 94638c8a9a5SSteve French else 94738c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 94838c8a9a5SSteve French rc = tcon->ses->server->ops->push_mand_locks(cfile); 94938c8a9a5SSteve French 95038c8a9a5SSteve French up_read(&cinode->lock_sem); 95138c8a9a5SSteve French return rc; 95238c8a9a5SSteve French } 95338c8a9a5SSteve French 95438c8a9a5SSteve French static int 95538c8a9a5SSteve French cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush) 95638c8a9a5SSteve French { 95738c8a9a5SSteve French int rc = -EACCES; 95838c8a9a5SSteve French unsigned int xid; 95938c8a9a5SSteve French __u32 oplock; 96038c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 96138c8a9a5SSteve French struct cifs_tcon *tcon; 96238c8a9a5SSteve French struct TCP_Server_Info *server; 96338c8a9a5SSteve French struct cifsInodeInfo *cinode; 96438c8a9a5SSteve French struct inode *inode; 96538c8a9a5SSteve French void *page; 96638c8a9a5SSteve French const char *full_path; 96738c8a9a5SSteve French int desired_access; 96838c8a9a5SSteve French int disposition = FILE_OPEN; 96938c8a9a5SSteve French int create_options = CREATE_NOT_DIR; 97038c8a9a5SSteve French struct cifs_open_parms oparms; 9710fdada1eSDavid Howells int rdwr_for_fscache = 0; 97238c8a9a5SSteve French 97338c8a9a5SSteve French xid = get_xid(); 97438c8a9a5SSteve French mutex_lock(&cfile->fh_mutex); 97538c8a9a5SSteve French if (!cfile->invalidHandle) { 97638c8a9a5SSteve French mutex_unlock(&cfile->fh_mutex); 97738c8a9a5SSteve French free_xid(xid); 97838c8a9a5SSteve French return 0; 97938c8a9a5SSteve French } 98038c8a9a5SSteve French 98138c8a9a5SSteve French inode = d_inode(cfile->dentry); 98238c8a9a5SSteve French cifs_sb = CIFS_SB(inode->i_sb); 98338c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 98438c8a9a5SSteve French server = tcon->ses->server; 98538c8a9a5SSteve French 98638c8a9a5SSteve French /* 98738c8a9a5SSteve French * Can not grab rename sem here because various ops, including those 98838c8a9a5SSteve French * that already have the rename sem can end up causing writepage to get 98938c8a9a5SSteve French * called and if the server was down that means we end up here, and we 99038c8a9a5SSteve French * can never tell if the caller already has the rename_sem. 99138c8a9a5SSteve French */ 99238c8a9a5SSteve French page = alloc_dentry_path(); 99338c8a9a5SSteve French full_path = build_path_from_dentry(cfile->dentry, page); 99438c8a9a5SSteve French if (IS_ERR(full_path)) { 99538c8a9a5SSteve French mutex_unlock(&cfile->fh_mutex); 99638c8a9a5SSteve French free_dentry_path(page); 99738c8a9a5SSteve French free_xid(xid); 99838c8a9a5SSteve French return PTR_ERR(full_path); 99938c8a9a5SSteve French } 100038c8a9a5SSteve French 100138c8a9a5SSteve French cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n", 100238c8a9a5SSteve French inode, cfile->f_flags, full_path); 100338c8a9a5SSteve French 100438c8a9a5SSteve French if (tcon->ses->server->oplocks) 100538c8a9a5SSteve French oplock = REQ_OPLOCK; 100638c8a9a5SSteve French else 100738c8a9a5SSteve French oplock = 0; 100838c8a9a5SSteve French 100938c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 101038c8a9a5SSteve French if (tcon->unix_ext && cap_unix(tcon->ses) && 101138c8a9a5SSteve French (CIFS_UNIX_POSIX_PATH_OPS_CAP & 101238c8a9a5SSteve French le64_to_cpu(tcon->fsUnixInfo.Capability))) { 101338c8a9a5SSteve French /* 101438c8a9a5SSteve French * O_CREAT, O_EXCL and O_TRUNC already had their effect on the 101538c8a9a5SSteve French * original open. Must mask them off for a reopen. 101638c8a9a5SSteve French */ 101738c8a9a5SSteve French unsigned int oflags = cfile->f_flags & 101838c8a9a5SSteve French ~(O_CREAT | O_EXCL | O_TRUNC); 101938c8a9a5SSteve French 102038c8a9a5SSteve French rc = cifs_posix_open(full_path, NULL, inode->i_sb, 102138c8a9a5SSteve French cifs_sb->ctx->file_mode /* ignored */, 102238c8a9a5SSteve French oflags, &oplock, &cfile->fid.netfid, xid); 102338c8a9a5SSteve French if (rc == 0) { 102438c8a9a5SSteve French cifs_dbg(FYI, "posix reopen succeeded\n"); 102538c8a9a5SSteve French oparms.reconnect = true; 102638c8a9a5SSteve French goto reopen_success; 102738c8a9a5SSteve French } 102838c8a9a5SSteve French /* 102938c8a9a5SSteve French * fallthrough to retry open the old way on errors, especially 103038c8a9a5SSteve French * in the reconnect path it is important to retry hard 103138c8a9a5SSteve French */ 103238c8a9a5SSteve French } 103338c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 103438c8a9a5SSteve French 10350fdada1eSDavid Howells /* If we're caching, we need to be able to fill in around partial writes. */ 10360fdada1eSDavid Howells if (cifs_fscache_enabled(inode) && (cfile->f_flags & O_ACCMODE) == O_WRONLY) 10370fdada1eSDavid Howells rdwr_for_fscache = 1; 10380fdada1eSDavid Howells 10390fdada1eSDavid Howells desired_access = cifs_convert_flags(cfile->f_flags, rdwr_for_fscache); 104038c8a9a5SSteve French 104138c8a9a5SSteve French /* O_SYNC also has bit for O_DSYNC so following check picks up either */ 104238c8a9a5SSteve French if (cfile->f_flags & O_SYNC) 104338c8a9a5SSteve French create_options |= CREATE_WRITE_THROUGH; 104438c8a9a5SSteve French 104538c8a9a5SSteve French if (cfile->f_flags & O_DIRECT) 104638c8a9a5SSteve French create_options |= CREATE_NO_BUFFER; 104738c8a9a5SSteve French 104838c8a9a5SSteve French if (server->ops->get_lease_key) 104938c8a9a5SSteve French server->ops->get_lease_key(inode, &cfile->fid); 105038c8a9a5SSteve French 10510fdada1eSDavid Howells retry_open: 105238c8a9a5SSteve French oparms = (struct cifs_open_parms) { 105338c8a9a5SSteve French .tcon = tcon, 105438c8a9a5SSteve French .cifs_sb = cifs_sb, 105538c8a9a5SSteve French .desired_access = desired_access, 105638c8a9a5SSteve French .create_options = cifs_create_options(cifs_sb, create_options), 105738c8a9a5SSteve French .disposition = disposition, 105838c8a9a5SSteve French .path = full_path, 105938c8a9a5SSteve French .fid = &cfile->fid, 106038c8a9a5SSteve French .reconnect = true, 106138c8a9a5SSteve French }; 106238c8a9a5SSteve French 106338c8a9a5SSteve French /* 106438c8a9a5SSteve French * Can not refresh inode by passing in file_info buf to be returned by 106538c8a9a5SSteve French * ops->open and then calling get_inode_info with returned buf since 106638c8a9a5SSteve French * file might have write behind data that needs to be flushed and server 106738c8a9a5SSteve French * version of file size can be stale. If we knew for sure that inode was 106838c8a9a5SSteve French * not dirty locally we could do this. 106938c8a9a5SSteve French */ 107038c8a9a5SSteve French rc = server->ops->open(xid, &oparms, &oplock, NULL); 107138c8a9a5SSteve French if (rc == -ENOENT && oparms.reconnect == false) { 107238c8a9a5SSteve French /* durable handle timeout is expired - open the file again */ 107338c8a9a5SSteve French rc = server->ops->open(xid, &oparms, &oplock, NULL); 107438c8a9a5SSteve French /* indicate that we need to relock the file */ 107538c8a9a5SSteve French oparms.reconnect = true; 107638c8a9a5SSteve French } 10770fdada1eSDavid Howells if (rc == -EACCES && rdwr_for_fscache == 1) { 10780fdada1eSDavid Howells desired_access = cifs_convert_flags(cfile->f_flags, 0); 10790fdada1eSDavid Howells rdwr_for_fscache = 2; 10800fdada1eSDavid Howells goto retry_open; 10810fdada1eSDavid Howells } 108238c8a9a5SSteve French 108338c8a9a5SSteve French if (rc) { 108438c8a9a5SSteve French mutex_unlock(&cfile->fh_mutex); 108538c8a9a5SSteve French cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc); 108638c8a9a5SSteve French cifs_dbg(FYI, "oplock: %d\n", oplock); 108738c8a9a5SSteve French goto reopen_error_exit; 108838c8a9a5SSteve French } 108938c8a9a5SSteve French 10900fdada1eSDavid Howells if (rdwr_for_fscache == 2) 10910fdada1eSDavid Howells cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); 10920fdada1eSDavid Howells 109338c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 109438c8a9a5SSteve French reopen_success: 109538c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 109638c8a9a5SSteve French cfile->invalidHandle = false; 109738c8a9a5SSteve French mutex_unlock(&cfile->fh_mutex); 109838c8a9a5SSteve French cinode = CIFS_I(inode); 109938c8a9a5SSteve French 110038c8a9a5SSteve French if (can_flush) { 110138c8a9a5SSteve French rc = filemap_write_and_wait(inode->i_mapping); 110238c8a9a5SSteve French if (!is_interrupt_error(rc)) 110338c8a9a5SSteve French mapping_set_error(inode->i_mapping, rc); 110438c8a9a5SSteve French 110502bcf865SSteve French if (tcon->posix_extensions) { 110602bcf865SSteve French rc = smb311_posix_get_inode_info(&inode, full_path, 110702bcf865SSteve French NULL, inode->i_sb, xid); 110802bcf865SSteve French } else if (tcon->unix_ext) { 110938c8a9a5SSteve French rc = cifs_get_inode_info_unix(&inode, full_path, 111038c8a9a5SSteve French inode->i_sb, xid); 111102bcf865SSteve French } else { 111238c8a9a5SSteve French rc = cifs_get_inode_info(&inode, full_path, NULL, 111338c8a9a5SSteve French inode->i_sb, xid, NULL); 111438c8a9a5SSteve French } 111502bcf865SSteve French } 111638c8a9a5SSteve French /* 111738c8a9a5SSteve French * Else we are writing out data to server already and could deadlock if 111838c8a9a5SSteve French * we tried to flush data, and since we do not know if we have data that 111938c8a9a5SSteve French * would invalidate the current end of file on the server we can not go 112038c8a9a5SSteve French * to the server to get the new inode info. 112138c8a9a5SSteve French */ 112238c8a9a5SSteve French 112338c8a9a5SSteve French /* 112438c8a9a5SSteve French * If the server returned a read oplock and we have mandatory brlocks, 112538c8a9a5SSteve French * set oplock level to None. 112638c8a9a5SSteve French */ 112738c8a9a5SSteve French if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) { 112838c8a9a5SSteve French cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n"); 112938c8a9a5SSteve French oplock = 0; 113038c8a9a5SSteve French } 113138c8a9a5SSteve French 113238c8a9a5SSteve French server->ops->set_fid(cfile, &cfile->fid, oplock); 113338c8a9a5SSteve French if (oparms.reconnect) 113438c8a9a5SSteve French cifs_relock_file(cfile); 113538c8a9a5SSteve French 113638c8a9a5SSteve French reopen_error_exit: 113738c8a9a5SSteve French free_dentry_path(page); 113838c8a9a5SSteve French free_xid(xid); 113938c8a9a5SSteve French return rc; 114038c8a9a5SSteve French } 114138c8a9a5SSteve French 114238c8a9a5SSteve French void smb2_deferred_work_close(struct work_struct *work) 114338c8a9a5SSteve French { 114438c8a9a5SSteve French struct cifsFileInfo *cfile = container_of(work, 114538c8a9a5SSteve French struct cifsFileInfo, deferred.work); 114638c8a9a5SSteve French 114738c8a9a5SSteve French spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 114838c8a9a5SSteve French cifs_del_deferred_close(cfile); 114938c8a9a5SSteve French cfile->deferred_close_scheduled = false; 115038c8a9a5SSteve French spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock); 115138c8a9a5SSteve French _cifsFileInfo_put(cfile, true, false); 115238c8a9a5SSteve French } 115338c8a9a5SSteve French 115438c8a9a5SSteve French int cifs_close(struct inode *inode, struct file *file) 115538c8a9a5SSteve French { 115638c8a9a5SSteve French struct cifsFileInfo *cfile; 115738c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 115838c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 115938c8a9a5SSteve French struct cifs_deferred_close *dclose; 116038c8a9a5SSteve French 116138c8a9a5SSteve French cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE); 116238c8a9a5SSteve French 116338c8a9a5SSteve French if (file->private_data != NULL) { 116438c8a9a5SSteve French cfile = file->private_data; 116538c8a9a5SSteve French file->private_data = NULL; 116638c8a9a5SSteve French dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL); 1167df9d70c1SBharath SM if ((cifs_sb->ctx->closetimeo && cinode->oplock == CIFS_CACHE_RHW_FLG) 1168df9d70c1SBharath SM && cinode->lease_granted && 116938c8a9a5SSteve French !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) && 117038c8a9a5SSteve French dclose) { 117138c8a9a5SSteve French if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) { 117223171df5SJeff Layton inode_set_mtime_to_ts(inode, 117323171df5SJeff Layton inode_set_ctime_current(inode)); 117438c8a9a5SSteve French } 117538c8a9a5SSteve French spin_lock(&cinode->deferred_lock); 117638c8a9a5SSteve French cifs_add_deferred_close(cfile, dclose); 117738c8a9a5SSteve French if (cfile->deferred_close_scheduled && 117838c8a9a5SSteve French delayed_work_pending(&cfile->deferred)) { 117938c8a9a5SSteve French /* 118038c8a9a5SSteve French * If there is no pending work, mod_delayed_work queues new work. 118138c8a9a5SSteve French * So, Increase the ref count to avoid use-after-free. 118238c8a9a5SSteve French */ 118338c8a9a5SSteve French if (!mod_delayed_work(deferredclose_wq, 118438c8a9a5SSteve French &cfile->deferred, cifs_sb->ctx->closetimeo)) 118538c8a9a5SSteve French cifsFileInfo_get(cfile); 118638c8a9a5SSteve French } else { 118738c8a9a5SSteve French /* Deferred close for files */ 118838c8a9a5SSteve French queue_delayed_work(deferredclose_wq, 118938c8a9a5SSteve French &cfile->deferred, cifs_sb->ctx->closetimeo); 119038c8a9a5SSteve French cfile->deferred_close_scheduled = true; 119138c8a9a5SSteve French spin_unlock(&cinode->deferred_lock); 119238c8a9a5SSteve French return 0; 119338c8a9a5SSteve French } 119438c8a9a5SSteve French spin_unlock(&cinode->deferred_lock); 119538c8a9a5SSteve French _cifsFileInfo_put(cfile, true, false); 119638c8a9a5SSteve French } else { 119738c8a9a5SSteve French _cifsFileInfo_put(cfile, true, false); 119838c8a9a5SSteve French kfree(dclose); 119938c8a9a5SSteve French } 120038c8a9a5SSteve French } 120138c8a9a5SSteve French 120238c8a9a5SSteve French /* return code from the ->release op is always ignored */ 120338c8a9a5SSteve French return 0; 120438c8a9a5SSteve French } 120538c8a9a5SSteve French 120638c8a9a5SSteve French void 120738c8a9a5SSteve French cifs_reopen_persistent_handles(struct cifs_tcon *tcon) 120838c8a9a5SSteve French { 120938c8a9a5SSteve French struct cifsFileInfo *open_file, *tmp; 121038c8a9a5SSteve French struct list_head tmp_list; 121138c8a9a5SSteve French 121238c8a9a5SSteve French if (!tcon->use_persistent || !tcon->need_reopen_files) 121338c8a9a5SSteve French return; 121438c8a9a5SSteve French 121538c8a9a5SSteve French tcon->need_reopen_files = false; 121638c8a9a5SSteve French 121738c8a9a5SSteve French cifs_dbg(FYI, "Reopen persistent handles\n"); 121838c8a9a5SSteve French INIT_LIST_HEAD(&tmp_list); 121938c8a9a5SSteve French 122038c8a9a5SSteve French /* list all files open on tree connection, reopen resilient handles */ 122138c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 122238c8a9a5SSteve French list_for_each_entry(open_file, &tcon->openFileList, tlist) { 122338c8a9a5SSteve French if (!open_file->invalidHandle) 122438c8a9a5SSteve French continue; 122538c8a9a5SSteve French cifsFileInfo_get(open_file); 122638c8a9a5SSteve French list_add_tail(&open_file->rlist, &tmp_list); 122738c8a9a5SSteve French } 122838c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 122938c8a9a5SSteve French 123038c8a9a5SSteve French list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) { 123138c8a9a5SSteve French if (cifs_reopen_file(open_file, false /* do not flush */)) 123238c8a9a5SSteve French tcon->need_reopen_files = true; 123338c8a9a5SSteve French list_del_init(&open_file->rlist); 123438c8a9a5SSteve French cifsFileInfo_put(open_file); 123538c8a9a5SSteve French } 123638c8a9a5SSteve French } 123738c8a9a5SSteve French 123838c8a9a5SSteve French int cifs_closedir(struct inode *inode, struct file *file) 123938c8a9a5SSteve French { 124038c8a9a5SSteve French int rc = 0; 124138c8a9a5SSteve French unsigned int xid; 124238c8a9a5SSteve French struct cifsFileInfo *cfile = file->private_data; 124338c8a9a5SSteve French struct cifs_tcon *tcon; 124438c8a9a5SSteve French struct TCP_Server_Info *server; 124538c8a9a5SSteve French char *buf; 124638c8a9a5SSteve French 124738c8a9a5SSteve French cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode); 124838c8a9a5SSteve French 124938c8a9a5SSteve French if (cfile == NULL) 125038c8a9a5SSteve French return rc; 125138c8a9a5SSteve French 125238c8a9a5SSteve French xid = get_xid(); 125338c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 125438c8a9a5SSteve French server = tcon->ses->server; 125538c8a9a5SSteve French 125638c8a9a5SSteve French cifs_dbg(FYI, "Freeing private data in close dir\n"); 125738c8a9a5SSteve French spin_lock(&cfile->file_info_lock); 125838c8a9a5SSteve French if (server->ops->dir_needs_close(cfile)) { 125938c8a9a5SSteve French cfile->invalidHandle = true; 126038c8a9a5SSteve French spin_unlock(&cfile->file_info_lock); 126138c8a9a5SSteve French if (server->ops->close_dir) 126238c8a9a5SSteve French rc = server->ops->close_dir(xid, tcon, &cfile->fid); 126338c8a9a5SSteve French else 126438c8a9a5SSteve French rc = -ENOSYS; 126538c8a9a5SSteve French cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc); 126638c8a9a5SSteve French /* not much we can do if it fails anyway, ignore rc */ 126738c8a9a5SSteve French rc = 0; 126838c8a9a5SSteve French } else 126938c8a9a5SSteve French spin_unlock(&cfile->file_info_lock); 127038c8a9a5SSteve French 127138c8a9a5SSteve French buf = cfile->srch_inf.ntwrk_buf_start; 127238c8a9a5SSteve French if (buf) { 127338c8a9a5SSteve French cifs_dbg(FYI, "closedir free smb buf in srch struct\n"); 127438c8a9a5SSteve French cfile->srch_inf.ntwrk_buf_start = NULL; 127538c8a9a5SSteve French if (cfile->srch_inf.smallBuf) 127638c8a9a5SSteve French cifs_small_buf_release(buf); 127738c8a9a5SSteve French else 127838c8a9a5SSteve French cifs_buf_release(buf); 127938c8a9a5SSteve French } 128038c8a9a5SSteve French 128138c8a9a5SSteve French cifs_put_tlink(cfile->tlink); 128238c8a9a5SSteve French kfree(file->private_data); 128338c8a9a5SSteve French file->private_data = NULL; 128438c8a9a5SSteve French /* BB can we lock the filestruct while this is going on? */ 128538c8a9a5SSteve French free_xid(xid); 128638c8a9a5SSteve French return rc; 128738c8a9a5SSteve French } 128838c8a9a5SSteve French 128938c8a9a5SSteve French static struct cifsLockInfo * 129038c8a9a5SSteve French cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags) 129138c8a9a5SSteve French { 129238c8a9a5SSteve French struct cifsLockInfo *lock = 129338c8a9a5SSteve French kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL); 129438c8a9a5SSteve French if (!lock) 129538c8a9a5SSteve French return lock; 129638c8a9a5SSteve French lock->offset = offset; 129738c8a9a5SSteve French lock->length = length; 129838c8a9a5SSteve French lock->type = type; 129938c8a9a5SSteve French lock->pid = current->tgid; 130038c8a9a5SSteve French lock->flags = flags; 130138c8a9a5SSteve French INIT_LIST_HEAD(&lock->blist); 130238c8a9a5SSteve French init_waitqueue_head(&lock->block_q); 130338c8a9a5SSteve French return lock; 130438c8a9a5SSteve French } 130538c8a9a5SSteve French 130638c8a9a5SSteve French void 130738c8a9a5SSteve French cifs_del_lock_waiters(struct cifsLockInfo *lock) 130838c8a9a5SSteve French { 130938c8a9a5SSteve French struct cifsLockInfo *li, *tmp; 131038c8a9a5SSteve French list_for_each_entry_safe(li, tmp, &lock->blist, blist) { 131138c8a9a5SSteve French list_del_init(&li->blist); 131238c8a9a5SSteve French wake_up(&li->block_q); 131338c8a9a5SSteve French } 131438c8a9a5SSteve French } 131538c8a9a5SSteve French 131638c8a9a5SSteve French #define CIFS_LOCK_OP 0 131738c8a9a5SSteve French #define CIFS_READ_OP 1 131838c8a9a5SSteve French #define CIFS_WRITE_OP 2 131938c8a9a5SSteve French 132038c8a9a5SSteve French /* @rw_check : 0 - no op, 1 - read, 2 - write */ 132138c8a9a5SSteve French static bool 132238c8a9a5SSteve French cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset, 132338c8a9a5SSteve French __u64 length, __u8 type, __u16 flags, 132438c8a9a5SSteve French struct cifsFileInfo *cfile, 132538c8a9a5SSteve French struct cifsLockInfo **conf_lock, int rw_check) 132638c8a9a5SSteve French { 132738c8a9a5SSteve French struct cifsLockInfo *li; 132838c8a9a5SSteve French struct cifsFileInfo *cur_cfile = fdlocks->cfile; 132938c8a9a5SSteve French struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 133038c8a9a5SSteve French 133138c8a9a5SSteve French list_for_each_entry(li, &fdlocks->locks, llist) { 133238c8a9a5SSteve French if (offset + length <= li->offset || 133338c8a9a5SSteve French offset >= li->offset + li->length) 133438c8a9a5SSteve French continue; 133538c8a9a5SSteve French if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid && 133638c8a9a5SSteve French server->ops->compare_fids(cfile, cur_cfile)) { 133738c8a9a5SSteve French /* shared lock prevents write op through the same fid */ 133838c8a9a5SSteve French if (!(li->type & server->vals->shared_lock_type) || 133938c8a9a5SSteve French rw_check != CIFS_WRITE_OP) 134038c8a9a5SSteve French continue; 134138c8a9a5SSteve French } 134238c8a9a5SSteve French if ((type & server->vals->shared_lock_type) && 134338c8a9a5SSteve French ((server->ops->compare_fids(cfile, cur_cfile) && 134438c8a9a5SSteve French current->tgid == li->pid) || type == li->type)) 134538c8a9a5SSteve French continue; 134638c8a9a5SSteve French if (rw_check == CIFS_LOCK_OP && 134738c8a9a5SSteve French (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) && 134838c8a9a5SSteve French server->ops->compare_fids(cfile, cur_cfile)) 134938c8a9a5SSteve French continue; 135038c8a9a5SSteve French if (conf_lock) 135138c8a9a5SSteve French *conf_lock = li; 135238c8a9a5SSteve French return true; 135338c8a9a5SSteve French } 135438c8a9a5SSteve French return false; 135538c8a9a5SSteve French } 135638c8a9a5SSteve French 135738c8a9a5SSteve French bool 135838c8a9a5SSteve French cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 135938c8a9a5SSteve French __u8 type, __u16 flags, 136038c8a9a5SSteve French struct cifsLockInfo **conf_lock, int rw_check) 136138c8a9a5SSteve French { 136238c8a9a5SSteve French bool rc = false; 136338c8a9a5SSteve French struct cifs_fid_locks *cur; 136438c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 136538c8a9a5SSteve French 136638c8a9a5SSteve French list_for_each_entry(cur, &cinode->llist, llist) { 136738c8a9a5SSteve French rc = cifs_find_fid_lock_conflict(cur, offset, length, type, 136838c8a9a5SSteve French flags, cfile, conf_lock, 136938c8a9a5SSteve French rw_check); 137038c8a9a5SSteve French if (rc) 137138c8a9a5SSteve French break; 137238c8a9a5SSteve French } 137338c8a9a5SSteve French 137438c8a9a5SSteve French return rc; 137538c8a9a5SSteve French } 137638c8a9a5SSteve French 137738c8a9a5SSteve French /* 137838c8a9a5SSteve French * Check if there is another lock that prevents us to set the lock (mandatory 137938c8a9a5SSteve French * style). If such a lock exists, update the flock structure with its 138038c8a9a5SSteve French * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 138138c8a9a5SSteve French * or leave it the same if we can't. Returns 0 if we don't need to request to 138238c8a9a5SSteve French * the server or 1 otherwise. 138338c8a9a5SSteve French */ 138438c8a9a5SSteve French static int 138538c8a9a5SSteve French cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length, 138638c8a9a5SSteve French __u8 type, struct file_lock *flock) 138738c8a9a5SSteve French { 138838c8a9a5SSteve French int rc = 0; 138938c8a9a5SSteve French struct cifsLockInfo *conf_lock; 139038c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 139138c8a9a5SSteve French struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 139238c8a9a5SSteve French bool exist; 139338c8a9a5SSteve French 139438c8a9a5SSteve French down_read(&cinode->lock_sem); 139538c8a9a5SSteve French 139638c8a9a5SSteve French exist = cifs_find_lock_conflict(cfile, offset, length, type, 139738c8a9a5SSteve French flock->fl_flags, &conf_lock, 139838c8a9a5SSteve French CIFS_LOCK_OP); 139938c8a9a5SSteve French if (exist) { 140038c8a9a5SSteve French flock->fl_start = conf_lock->offset; 140138c8a9a5SSteve French flock->fl_end = conf_lock->offset + conf_lock->length - 1; 140238c8a9a5SSteve French flock->fl_pid = conf_lock->pid; 140338c8a9a5SSteve French if (conf_lock->type & server->vals->shared_lock_type) 140438c8a9a5SSteve French flock->fl_type = F_RDLCK; 140538c8a9a5SSteve French else 140638c8a9a5SSteve French flock->fl_type = F_WRLCK; 140738c8a9a5SSteve French } else if (!cinode->can_cache_brlcks) 140838c8a9a5SSteve French rc = 1; 140938c8a9a5SSteve French else 141038c8a9a5SSteve French flock->fl_type = F_UNLCK; 141138c8a9a5SSteve French 141238c8a9a5SSteve French up_read(&cinode->lock_sem); 141338c8a9a5SSteve French return rc; 141438c8a9a5SSteve French } 141538c8a9a5SSteve French 141638c8a9a5SSteve French static void 141738c8a9a5SSteve French cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock) 141838c8a9a5SSteve French { 141938c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 142038c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 142138c8a9a5SSteve French list_add_tail(&lock->llist, &cfile->llist->locks); 142238c8a9a5SSteve French up_write(&cinode->lock_sem); 142338c8a9a5SSteve French } 142438c8a9a5SSteve French 142538c8a9a5SSteve French /* 142638c8a9a5SSteve French * Set the byte-range lock (mandatory style). Returns: 142738c8a9a5SSteve French * 1) 0, if we set the lock and don't need to request to the server; 142838c8a9a5SSteve French * 2) 1, if no locks prevent us but we need to request to the server; 142938c8a9a5SSteve French * 3) -EACCES, if there is a lock that prevents us and wait is false. 143038c8a9a5SSteve French */ 143138c8a9a5SSteve French static int 143238c8a9a5SSteve French cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock, 143338c8a9a5SSteve French bool wait) 143438c8a9a5SSteve French { 143538c8a9a5SSteve French struct cifsLockInfo *conf_lock; 143638c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 143738c8a9a5SSteve French bool exist; 143838c8a9a5SSteve French int rc = 0; 143938c8a9a5SSteve French 144038c8a9a5SSteve French try_again: 144138c8a9a5SSteve French exist = false; 144238c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 144338c8a9a5SSteve French 144438c8a9a5SSteve French exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length, 144538c8a9a5SSteve French lock->type, lock->flags, &conf_lock, 144638c8a9a5SSteve French CIFS_LOCK_OP); 144738c8a9a5SSteve French if (!exist && cinode->can_cache_brlcks) { 144838c8a9a5SSteve French list_add_tail(&lock->llist, &cfile->llist->locks); 144938c8a9a5SSteve French up_write(&cinode->lock_sem); 145038c8a9a5SSteve French return rc; 145138c8a9a5SSteve French } 145238c8a9a5SSteve French 145338c8a9a5SSteve French if (!exist) 145438c8a9a5SSteve French rc = 1; 145538c8a9a5SSteve French else if (!wait) 145638c8a9a5SSteve French rc = -EACCES; 145738c8a9a5SSteve French else { 145838c8a9a5SSteve French list_add_tail(&lock->blist, &conf_lock->blist); 145938c8a9a5SSteve French up_write(&cinode->lock_sem); 146038c8a9a5SSteve French rc = wait_event_interruptible(lock->block_q, 146138c8a9a5SSteve French (lock->blist.prev == &lock->blist) && 146238c8a9a5SSteve French (lock->blist.next == &lock->blist)); 146338c8a9a5SSteve French if (!rc) 146438c8a9a5SSteve French goto try_again; 146538c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 146638c8a9a5SSteve French list_del_init(&lock->blist); 146738c8a9a5SSteve French } 146838c8a9a5SSteve French 146938c8a9a5SSteve French up_write(&cinode->lock_sem); 147038c8a9a5SSteve French return rc; 147138c8a9a5SSteve French } 147238c8a9a5SSteve French 147338c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 147438c8a9a5SSteve French /* 147538c8a9a5SSteve French * Check if there is another lock that prevents us to set the lock (posix 147638c8a9a5SSteve French * style). If such a lock exists, update the flock structure with its 147738c8a9a5SSteve French * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks 147838c8a9a5SSteve French * or leave it the same if we can't. Returns 0 if we don't need to request to 147938c8a9a5SSteve French * the server or 1 otherwise. 148038c8a9a5SSteve French */ 148138c8a9a5SSteve French static int 148238c8a9a5SSteve French cifs_posix_lock_test(struct file *file, struct file_lock *flock) 148338c8a9a5SSteve French { 148438c8a9a5SSteve French int rc = 0; 148538c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 148638c8a9a5SSteve French unsigned char saved_type = flock->fl_type; 148738c8a9a5SSteve French 148838c8a9a5SSteve French if ((flock->fl_flags & FL_POSIX) == 0) 148938c8a9a5SSteve French return 1; 149038c8a9a5SSteve French 149138c8a9a5SSteve French down_read(&cinode->lock_sem); 149238c8a9a5SSteve French posix_test_lock(file, flock); 149338c8a9a5SSteve French 149438c8a9a5SSteve French if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) { 149538c8a9a5SSteve French flock->fl_type = saved_type; 149638c8a9a5SSteve French rc = 1; 149738c8a9a5SSteve French } 149838c8a9a5SSteve French 149938c8a9a5SSteve French up_read(&cinode->lock_sem); 150038c8a9a5SSteve French return rc; 150138c8a9a5SSteve French } 150238c8a9a5SSteve French 150338c8a9a5SSteve French /* 150438c8a9a5SSteve French * Set the byte-range lock (posix style). Returns: 150538c8a9a5SSteve French * 1) <0, if the error occurs while setting the lock; 150638c8a9a5SSteve French * 2) 0, if we set the lock and don't need to request to the server; 150738c8a9a5SSteve French * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock; 150838c8a9a5SSteve French * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server. 150938c8a9a5SSteve French */ 151038c8a9a5SSteve French static int 151138c8a9a5SSteve French cifs_posix_lock_set(struct file *file, struct file_lock *flock) 151238c8a9a5SSteve French { 151338c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(file_inode(file)); 151438c8a9a5SSteve French int rc = FILE_LOCK_DEFERRED + 1; 151538c8a9a5SSteve French 151638c8a9a5SSteve French if ((flock->fl_flags & FL_POSIX) == 0) 151738c8a9a5SSteve French return rc; 151838c8a9a5SSteve French 151938c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 152038c8a9a5SSteve French if (!cinode->can_cache_brlcks) { 152138c8a9a5SSteve French up_write(&cinode->lock_sem); 152238c8a9a5SSteve French return rc; 152338c8a9a5SSteve French } 152438c8a9a5SSteve French 152538c8a9a5SSteve French rc = posix_lock_file(file, flock, NULL); 152638c8a9a5SSteve French up_write(&cinode->lock_sem); 152738c8a9a5SSteve French return rc; 152838c8a9a5SSteve French } 152938c8a9a5SSteve French 153038c8a9a5SSteve French int 153138c8a9a5SSteve French cifs_push_mandatory_locks(struct cifsFileInfo *cfile) 153238c8a9a5SSteve French { 153338c8a9a5SSteve French unsigned int xid; 153438c8a9a5SSteve French int rc = 0, stored_rc; 153538c8a9a5SSteve French struct cifsLockInfo *li, *tmp; 153638c8a9a5SSteve French struct cifs_tcon *tcon; 153738c8a9a5SSteve French unsigned int num, max_num, max_buf; 153838c8a9a5SSteve French LOCKING_ANDX_RANGE *buf, *cur; 153938c8a9a5SSteve French static const int types[] = { 154038c8a9a5SSteve French LOCKING_ANDX_LARGE_FILES, 154138c8a9a5SSteve French LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 154238c8a9a5SSteve French }; 154338c8a9a5SSteve French int i; 154438c8a9a5SSteve French 154538c8a9a5SSteve French xid = get_xid(); 154638c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 154738c8a9a5SSteve French 154838c8a9a5SSteve French /* 154938c8a9a5SSteve French * Accessing maxBuf is racy with cifs_reconnect - need to store value 155038c8a9a5SSteve French * and check it before using. 155138c8a9a5SSteve French */ 155238c8a9a5SSteve French max_buf = tcon->ses->server->maxBuf; 155338c8a9a5SSteve French if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) { 155438c8a9a5SSteve French free_xid(xid); 155538c8a9a5SSteve French return -EINVAL; 155638c8a9a5SSteve French } 155738c8a9a5SSteve French 155838c8a9a5SSteve French BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 155938c8a9a5SSteve French PAGE_SIZE); 156038c8a9a5SSteve French max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 156138c8a9a5SSteve French PAGE_SIZE); 156238c8a9a5SSteve French max_num = (max_buf - sizeof(struct smb_hdr)) / 156338c8a9a5SSteve French sizeof(LOCKING_ANDX_RANGE); 156438c8a9a5SSteve French buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 156538c8a9a5SSteve French if (!buf) { 156638c8a9a5SSteve French free_xid(xid); 156738c8a9a5SSteve French return -ENOMEM; 156838c8a9a5SSteve French } 156938c8a9a5SSteve French 157038c8a9a5SSteve French for (i = 0; i < 2; i++) { 157138c8a9a5SSteve French cur = buf; 157238c8a9a5SSteve French num = 0; 157338c8a9a5SSteve French list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 157438c8a9a5SSteve French if (li->type != types[i]) 157538c8a9a5SSteve French continue; 157638c8a9a5SSteve French cur->Pid = cpu_to_le16(li->pid); 157738c8a9a5SSteve French cur->LengthLow = cpu_to_le32((u32)li->length); 157838c8a9a5SSteve French cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 157938c8a9a5SSteve French cur->OffsetLow = cpu_to_le32((u32)li->offset); 158038c8a9a5SSteve French cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 158138c8a9a5SSteve French if (++num == max_num) { 158238c8a9a5SSteve French stored_rc = cifs_lockv(xid, tcon, 158338c8a9a5SSteve French cfile->fid.netfid, 158438c8a9a5SSteve French (__u8)li->type, 0, num, 158538c8a9a5SSteve French buf); 158638c8a9a5SSteve French if (stored_rc) 158738c8a9a5SSteve French rc = stored_rc; 158838c8a9a5SSteve French cur = buf; 158938c8a9a5SSteve French num = 0; 159038c8a9a5SSteve French } else 159138c8a9a5SSteve French cur++; 159238c8a9a5SSteve French } 159338c8a9a5SSteve French 159438c8a9a5SSteve French if (num) { 159538c8a9a5SSteve French stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 159638c8a9a5SSteve French (__u8)types[i], 0, num, buf); 159738c8a9a5SSteve French if (stored_rc) 159838c8a9a5SSteve French rc = stored_rc; 159938c8a9a5SSteve French } 160038c8a9a5SSteve French } 160138c8a9a5SSteve French 160238c8a9a5SSteve French kfree(buf); 160338c8a9a5SSteve French free_xid(xid); 160438c8a9a5SSteve French return rc; 160538c8a9a5SSteve French } 160638c8a9a5SSteve French 160738c8a9a5SSteve French static __u32 160838c8a9a5SSteve French hash_lockowner(fl_owner_t owner) 160938c8a9a5SSteve French { 161038c8a9a5SSteve French return cifs_lock_secret ^ hash32_ptr((const void *)owner); 161138c8a9a5SSteve French } 161238c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 161338c8a9a5SSteve French 161438c8a9a5SSteve French struct lock_to_push { 161538c8a9a5SSteve French struct list_head llist; 161638c8a9a5SSteve French __u64 offset; 161738c8a9a5SSteve French __u64 length; 161838c8a9a5SSteve French __u32 pid; 161938c8a9a5SSteve French __u16 netfid; 162038c8a9a5SSteve French __u8 type; 162138c8a9a5SSteve French }; 162238c8a9a5SSteve French 162338c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 162438c8a9a5SSteve French static int 162538c8a9a5SSteve French cifs_push_posix_locks(struct cifsFileInfo *cfile) 162638c8a9a5SSteve French { 162738c8a9a5SSteve French struct inode *inode = d_inode(cfile->dentry); 162838c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 162938c8a9a5SSteve French struct file_lock *flock; 163038c8a9a5SSteve French struct file_lock_context *flctx = locks_inode_context(inode); 163138c8a9a5SSteve French unsigned int count = 0, i; 163238c8a9a5SSteve French int rc = 0, xid, type; 163338c8a9a5SSteve French struct list_head locks_to_send, *el; 163438c8a9a5SSteve French struct lock_to_push *lck, *tmp; 163538c8a9a5SSteve French __u64 length; 163638c8a9a5SSteve French 163738c8a9a5SSteve French xid = get_xid(); 163838c8a9a5SSteve French 163938c8a9a5SSteve French if (!flctx) 164038c8a9a5SSteve French goto out; 164138c8a9a5SSteve French 164238c8a9a5SSteve French spin_lock(&flctx->flc_lock); 164338c8a9a5SSteve French list_for_each(el, &flctx->flc_posix) { 164438c8a9a5SSteve French count++; 164538c8a9a5SSteve French } 164638c8a9a5SSteve French spin_unlock(&flctx->flc_lock); 164738c8a9a5SSteve French 164838c8a9a5SSteve French INIT_LIST_HEAD(&locks_to_send); 164938c8a9a5SSteve French 165038c8a9a5SSteve French /* 165138c8a9a5SSteve French * Allocating count locks is enough because no FL_POSIX locks can be 165238c8a9a5SSteve French * added to the list while we are holding cinode->lock_sem that 165338c8a9a5SSteve French * protects locking operations of this inode. 165438c8a9a5SSteve French */ 165538c8a9a5SSteve French for (i = 0; i < count; i++) { 165638c8a9a5SSteve French lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL); 165738c8a9a5SSteve French if (!lck) { 165838c8a9a5SSteve French rc = -ENOMEM; 165938c8a9a5SSteve French goto err_out; 166038c8a9a5SSteve French } 166138c8a9a5SSteve French list_add_tail(&lck->llist, &locks_to_send); 166238c8a9a5SSteve French } 166338c8a9a5SSteve French 166438c8a9a5SSteve French el = locks_to_send.next; 166538c8a9a5SSteve French spin_lock(&flctx->flc_lock); 166638c8a9a5SSteve French list_for_each_entry(flock, &flctx->flc_posix, fl_list) { 166738c8a9a5SSteve French if (el == &locks_to_send) { 166838c8a9a5SSteve French /* 166938c8a9a5SSteve French * The list ended. We don't have enough allocated 167038c8a9a5SSteve French * structures - something is really wrong. 167138c8a9a5SSteve French */ 167238c8a9a5SSteve French cifs_dbg(VFS, "Can't push all brlocks!\n"); 167338c8a9a5SSteve French break; 167438c8a9a5SSteve French } 167538c8a9a5SSteve French length = cifs_flock_len(flock); 167638c8a9a5SSteve French if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK) 167738c8a9a5SSteve French type = CIFS_RDLCK; 167838c8a9a5SSteve French else 167938c8a9a5SSteve French type = CIFS_WRLCK; 168038c8a9a5SSteve French lck = list_entry(el, struct lock_to_push, llist); 168138c8a9a5SSteve French lck->pid = hash_lockowner(flock->fl_owner); 168238c8a9a5SSteve French lck->netfid = cfile->fid.netfid; 168338c8a9a5SSteve French lck->length = length; 168438c8a9a5SSteve French lck->type = type; 168538c8a9a5SSteve French lck->offset = flock->fl_start; 168638c8a9a5SSteve French } 168738c8a9a5SSteve French spin_unlock(&flctx->flc_lock); 168838c8a9a5SSteve French 168938c8a9a5SSteve French list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 169038c8a9a5SSteve French int stored_rc; 169138c8a9a5SSteve French 169238c8a9a5SSteve French stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid, 169338c8a9a5SSteve French lck->offset, lck->length, NULL, 169438c8a9a5SSteve French lck->type, 0); 169538c8a9a5SSteve French if (stored_rc) 169638c8a9a5SSteve French rc = stored_rc; 169738c8a9a5SSteve French list_del(&lck->llist); 169838c8a9a5SSteve French kfree(lck); 169938c8a9a5SSteve French } 170038c8a9a5SSteve French 170138c8a9a5SSteve French out: 170238c8a9a5SSteve French free_xid(xid); 170338c8a9a5SSteve French return rc; 170438c8a9a5SSteve French err_out: 170538c8a9a5SSteve French list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) { 170638c8a9a5SSteve French list_del(&lck->llist); 170738c8a9a5SSteve French kfree(lck); 170838c8a9a5SSteve French } 170938c8a9a5SSteve French goto out; 171038c8a9a5SSteve French } 171138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 171238c8a9a5SSteve French 171338c8a9a5SSteve French static int 171438c8a9a5SSteve French cifs_push_locks(struct cifsFileInfo *cfile) 171538c8a9a5SSteve French { 171638c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 171738c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 171838c8a9a5SSteve French int rc = 0; 171938c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 172038c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 172138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 172238c8a9a5SSteve French 172338c8a9a5SSteve French /* we are going to update can_cache_brlcks here - need a write access */ 172438c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 172538c8a9a5SSteve French if (!cinode->can_cache_brlcks) { 172638c8a9a5SSteve French up_write(&cinode->lock_sem); 172738c8a9a5SSteve French return rc; 172838c8a9a5SSteve French } 172938c8a9a5SSteve French 173038c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 173138c8a9a5SSteve French if (cap_unix(tcon->ses) && 173238c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 173338c8a9a5SSteve French ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 173438c8a9a5SSteve French rc = cifs_push_posix_locks(cfile); 173538c8a9a5SSteve French else 173638c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 173738c8a9a5SSteve French rc = tcon->ses->server->ops->push_mand_locks(cfile); 173838c8a9a5SSteve French 173938c8a9a5SSteve French cinode->can_cache_brlcks = false; 174038c8a9a5SSteve French up_write(&cinode->lock_sem); 174138c8a9a5SSteve French return rc; 174238c8a9a5SSteve French } 174338c8a9a5SSteve French 174438c8a9a5SSteve French static void 174538c8a9a5SSteve French cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock, 174638c8a9a5SSteve French bool *wait_flag, struct TCP_Server_Info *server) 174738c8a9a5SSteve French { 174838c8a9a5SSteve French if (flock->fl_flags & FL_POSIX) 174938c8a9a5SSteve French cifs_dbg(FYI, "Posix\n"); 175038c8a9a5SSteve French if (flock->fl_flags & FL_FLOCK) 175138c8a9a5SSteve French cifs_dbg(FYI, "Flock\n"); 175238c8a9a5SSteve French if (flock->fl_flags & FL_SLEEP) { 175338c8a9a5SSteve French cifs_dbg(FYI, "Blocking lock\n"); 175438c8a9a5SSteve French *wait_flag = true; 175538c8a9a5SSteve French } 175638c8a9a5SSteve French if (flock->fl_flags & FL_ACCESS) 175738c8a9a5SSteve French cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n"); 175838c8a9a5SSteve French if (flock->fl_flags & FL_LEASE) 175938c8a9a5SSteve French cifs_dbg(FYI, "Lease on file - not implemented yet\n"); 176038c8a9a5SSteve French if (flock->fl_flags & 176138c8a9a5SSteve French (~(FL_POSIX | FL_FLOCK | FL_SLEEP | 176238c8a9a5SSteve French FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK))) 176338c8a9a5SSteve French cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags); 176438c8a9a5SSteve French 176538c8a9a5SSteve French *type = server->vals->large_lock_type; 176638c8a9a5SSteve French if (flock->fl_type == F_WRLCK) { 176738c8a9a5SSteve French cifs_dbg(FYI, "F_WRLCK\n"); 176838c8a9a5SSteve French *type |= server->vals->exclusive_lock_type; 176938c8a9a5SSteve French *lock = 1; 177038c8a9a5SSteve French } else if (flock->fl_type == F_UNLCK) { 177138c8a9a5SSteve French cifs_dbg(FYI, "F_UNLCK\n"); 177238c8a9a5SSteve French *type |= server->vals->unlock_lock_type; 177338c8a9a5SSteve French *unlock = 1; 177438c8a9a5SSteve French /* Check if unlock includes more than one lock range */ 177538c8a9a5SSteve French } else if (flock->fl_type == F_RDLCK) { 177638c8a9a5SSteve French cifs_dbg(FYI, "F_RDLCK\n"); 177738c8a9a5SSteve French *type |= server->vals->shared_lock_type; 177838c8a9a5SSteve French *lock = 1; 177938c8a9a5SSteve French } else if (flock->fl_type == F_EXLCK) { 178038c8a9a5SSteve French cifs_dbg(FYI, "F_EXLCK\n"); 178138c8a9a5SSteve French *type |= server->vals->exclusive_lock_type; 178238c8a9a5SSteve French *lock = 1; 178338c8a9a5SSteve French } else if (flock->fl_type == F_SHLCK) { 178438c8a9a5SSteve French cifs_dbg(FYI, "F_SHLCK\n"); 178538c8a9a5SSteve French *type |= server->vals->shared_lock_type; 178638c8a9a5SSteve French *lock = 1; 178738c8a9a5SSteve French } else 178838c8a9a5SSteve French cifs_dbg(FYI, "Unknown type of lock\n"); 178938c8a9a5SSteve French } 179038c8a9a5SSteve French 179138c8a9a5SSteve French static int 179238c8a9a5SSteve French cifs_getlk(struct file *file, struct file_lock *flock, __u32 type, 179338c8a9a5SSteve French bool wait_flag, bool posix_lck, unsigned int xid) 179438c8a9a5SSteve French { 179538c8a9a5SSteve French int rc = 0; 179638c8a9a5SSteve French __u64 length = cifs_flock_len(flock); 179738c8a9a5SSteve French struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 179838c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 179938c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 180038c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 180138c8a9a5SSteve French __u16 netfid = cfile->fid.netfid; 180238c8a9a5SSteve French 180338c8a9a5SSteve French if (posix_lck) { 180438c8a9a5SSteve French int posix_lock_type; 180538c8a9a5SSteve French 180638c8a9a5SSteve French rc = cifs_posix_lock_test(file, flock); 180738c8a9a5SSteve French if (!rc) 180838c8a9a5SSteve French return rc; 180938c8a9a5SSteve French 181038c8a9a5SSteve French if (type & server->vals->shared_lock_type) 181138c8a9a5SSteve French posix_lock_type = CIFS_RDLCK; 181238c8a9a5SSteve French else 181338c8a9a5SSteve French posix_lock_type = CIFS_WRLCK; 181438c8a9a5SSteve French rc = CIFSSMBPosixLock(xid, tcon, netfid, 181538c8a9a5SSteve French hash_lockowner(flock->fl_owner), 181638c8a9a5SSteve French flock->fl_start, length, flock, 181738c8a9a5SSteve French posix_lock_type, wait_flag); 181838c8a9a5SSteve French return rc; 181938c8a9a5SSteve French } 182038c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 182138c8a9a5SSteve French 182238c8a9a5SSteve French rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock); 182338c8a9a5SSteve French if (!rc) 182438c8a9a5SSteve French return rc; 182538c8a9a5SSteve French 182638c8a9a5SSteve French /* BB we could chain these into one lock request BB */ 182738c8a9a5SSteve French rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type, 182838c8a9a5SSteve French 1, 0, false); 182938c8a9a5SSteve French if (rc == 0) { 183038c8a9a5SSteve French rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 183138c8a9a5SSteve French type, 0, 1, false); 183238c8a9a5SSteve French flock->fl_type = F_UNLCK; 183338c8a9a5SSteve French if (rc != 0) 183438c8a9a5SSteve French cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 183538c8a9a5SSteve French rc); 183638c8a9a5SSteve French return 0; 183738c8a9a5SSteve French } 183838c8a9a5SSteve French 183938c8a9a5SSteve French if (type & server->vals->shared_lock_type) { 184038c8a9a5SSteve French flock->fl_type = F_WRLCK; 184138c8a9a5SSteve French return 0; 184238c8a9a5SSteve French } 184338c8a9a5SSteve French 184438c8a9a5SSteve French type &= ~server->vals->exclusive_lock_type; 184538c8a9a5SSteve French 184638c8a9a5SSteve French rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 184738c8a9a5SSteve French type | server->vals->shared_lock_type, 184838c8a9a5SSteve French 1, 0, false); 184938c8a9a5SSteve French if (rc == 0) { 185038c8a9a5SSteve French rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 185138c8a9a5SSteve French type | server->vals->shared_lock_type, 0, 1, false); 185238c8a9a5SSteve French flock->fl_type = F_RDLCK; 185338c8a9a5SSteve French if (rc != 0) 185438c8a9a5SSteve French cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n", 185538c8a9a5SSteve French rc); 185638c8a9a5SSteve French } else 185738c8a9a5SSteve French flock->fl_type = F_WRLCK; 185838c8a9a5SSteve French 185938c8a9a5SSteve French return 0; 186038c8a9a5SSteve French } 186138c8a9a5SSteve French 186238c8a9a5SSteve French void 186338c8a9a5SSteve French cifs_move_llist(struct list_head *source, struct list_head *dest) 186438c8a9a5SSteve French { 186538c8a9a5SSteve French struct list_head *li, *tmp; 186638c8a9a5SSteve French list_for_each_safe(li, tmp, source) 186738c8a9a5SSteve French list_move(li, dest); 186838c8a9a5SSteve French } 186938c8a9a5SSteve French 187038c8a9a5SSteve French void 187138c8a9a5SSteve French cifs_free_llist(struct list_head *llist) 187238c8a9a5SSteve French { 187338c8a9a5SSteve French struct cifsLockInfo *li, *tmp; 187438c8a9a5SSteve French list_for_each_entry_safe(li, tmp, llist, llist) { 187538c8a9a5SSteve French cifs_del_lock_waiters(li); 187638c8a9a5SSteve French list_del(&li->llist); 187738c8a9a5SSteve French kfree(li); 187838c8a9a5SSteve French } 187938c8a9a5SSteve French } 188038c8a9a5SSteve French 188138c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 188238c8a9a5SSteve French int 188338c8a9a5SSteve French cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock, 188438c8a9a5SSteve French unsigned int xid) 188538c8a9a5SSteve French { 188638c8a9a5SSteve French int rc = 0, stored_rc; 188738c8a9a5SSteve French static const int types[] = { 188838c8a9a5SSteve French LOCKING_ANDX_LARGE_FILES, 188938c8a9a5SSteve French LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES 189038c8a9a5SSteve French }; 189138c8a9a5SSteve French unsigned int i; 189238c8a9a5SSteve French unsigned int max_num, num, max_buf; 189338c8a9a5SSteve French LOCKING_ANDX_RANGE *buf, *cur; 189438c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 189538c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry)); 189638c8a9a5SSteve French struct cifsLockInfo *li, *tmp; 189738c8a9a5SSteve French __u64 length = cifs_flock_len(flock); 189838c8a9a5SSteve French struct list_head tmp_llist; 189938c8a9a5SSteve French 190038c8a9a5SSteve French INIT_LIST_HEAD(&tmp_llist); 190138c8a9a5SSteve French 190238c8a9a5SSteve French /* 190338c8a9a5SSteve French * Accessing maxBuf is racy with cifs_reconnect - need to store value 190438c8a9a5SSteve French * and check it before using. 190538c8a9a5SSteve French */ 190638c8a9a5SSteve French max_buf = tcon->ses->server->maxBuf; 190738c8a9a5SSteve French if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) 190838c8a9a5SSteve French return -EINVAL; 190938c8a9a5SSteve French 191038c8a9a5SSteve French BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) > 191138c8a9a5SSteve French PAGE_SIZE); 191238c8a9a5SSteve French max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr), 191338c8a9a5SSteve French PAGE_SIZE); 191438c8a9a5SSteve French max_num = (max_buf - sizeof(struct smb_hdr)) / 191538c8a9a5SSteve French sizeof(LOCKING_ANDX_RANGE); 191638c8a9a5SSteve French buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL); 191738c8a9a5SSteve French if (!buf) 191838c8a9a5SSteve French return -ENOMEM; 191938c8a9a5SSteve French 192038c8a9a5SSteve French cifs_down_write(&cinode->lock_sem); 192138c8a9a5SSteve French for (i = 0; i < 2; i++) { 192238c8a9a5SSteve French cur = buf; 192338c8a9a5SSteve French num = 0; 192438c8a9a5SSteve French list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) { 192538c8a9a5SSteve French if (flock->fl_start > li->offset || 192638c8a9a5SSteve French (flock->fl_start + length) < 192738c8a9a5SSteve French (li->offset + li->length)) 192838c8a9a5SSteve French continue; 192938c8a9a5SSteve French if (current->tgid != li->pid) 193038c8a9a5SSteve French continue; 193138c8a9a5SSteve French if (types[i] != li->type) 193238c8a9a5SSteve French continue; 193338c8a9a5SSteve French if (cinode->can_cache_brlcks) { 193438c8a9a5SSteve French /* 193538c8a9a5SSteve French * We can cache brlock requests - simply remove 193638c8a9a5SSteve French * a lock from the file's list. 193738c8a9a5SSteve French */ 193838c8a9a5SSteve French list_del(&li->llist); 193938c8a9a5SSteve French cifs_del_lock_waiters(li); 194038c8a9a5SSteve French kfree(li); 194138c8a9a5SSteve French continue; 194238c8a9a5SSteve French } 194338c8a9a5SSteve French cur->Pid = cpu_to_le16(li->pid); 194438c8a9a5SSteve French cur->LengthLow = cpu_to_le32((u32)li->length); 194538c8a9a5SSteve French cur->LengthHigh = cpu_to_le32((u32)(li->length>>32)); 194638c8a9a5SSteve French cur->OffsetLow = cpu_to_le32((u32)li->offset); 194738c8a9a5SSteve French cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32)); 194838c8a9a5SSteve French /* 194938c8a9a5SSteve French * We need to save a lock here to let us add it again to 195038c8a9a5SSteve French * the file's list if the unlock range request fails on 195138c8a9a5SSteve French * the server. 195238c8a9a5SSteve French */ 195338c8a9a5SSteve French list_move(&li->llist, &tmp_llist); 195438c8a9a5SSteve French if (++num == max_num) { 195538c8a9a5SSteve French stored_rc = cifs_lockv(xid, tcon, 195638c8a9a5SSteve French cfile->fid.netfid, 195738c8a9a5SSteve French li->type, num, 0, buf); 195838c8a9a5SSteve French if (stored_rc) { 195938c8a9a5SSteve French /* 196038c8a9a5SSteve French * We failed on the unlock range 196138c8a9a5SSteve French * request - add all locks from the tmp 196238c8a9a5SSteve French * list to the head of the file's list. 196338c8a9a5SSteve French */ 196438c8a9a5SSteve French cifs_move_llist(&tmp_llist, 196538c8a9a5SSteve French &cfile->llist->locks); 196638c8a9a5SSteve French rc = stored_rc; 196738c8a9a5SSteve French } else 196838c8a9a5SSteve French /* 196938c8a9a5SSteve French * The unlock range request succeed - 197038c8a9a5SSteve French * free the tmp list. 197138c8a9a5SSteve French */ 197238c8a9a5SSteve French cifs_free_llist(&tmp_llist); 197338c8a9a5SSteve French cur = buf; 197438c8a9a5SSteve French num = 0; 197538c8a9a5SSteve French } else 197638c8a9a5SSteve French cur++; 197738c8a9a5SSteve French } 197838c8a9a5SSteve French if (num) { 197938c8a9a5SSteve French stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid, 198038c8a9a5SSteve French types[i], num, 0, buf); 198138c8a9a5SSteve French if (stored_rc) { 198238c8a9a5SSteve French cifs_move_llist(&tmp_llist, 198338c8a9a5SSteve French &cfile->llist->locks); 198438c8a9a5SSteve French rc = stored_rc; 198538c8a9a5SSteve French } else 198638c8a9a5SSteve French cifs_free_llist(&tmp_llist); 198738c8a9a5SSteve French } 198838c8a9a5SSteve French } 198938c8a9a5SSteve French 199038c8a9a5SSteve French up_write(&cinode->lock_sem); 199138c8a9a5SSteve French kfree(buf); 199238c8a9a5SSteve French return rc; 199338c8a9a5SSteve French } 199438c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 199538c8a9a5SSteve French 199638c8a9a5SSteve French static int 199738c8a9a5SSteve French cifs_setlk(struct file *file, struct file_lock *flock, __u32 type, 199838c8a9a5SSteve French bool wait_flag, bool posix_lck, int lock, int unlock, 199938c8a9a5SSteve French unsigned int xid) 200038c8a9a5SSteve French { 200138c8a9a5SSteve French int rc = 0; 200238c8a9a5SSteve French __u64 length = cifs_flock_len(flock); 200338c8a9a5SSteve French struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 200438c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 200538c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 200638c8a9a5SSteve French struct inode *inode = d_inode(cfile->dentry); 200738c8a9a5SSteve French 200838c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 200938c8a9a5SSteve French if (posix_lck) { 201038c8a9a5SSteve French int posix_lock_type; 201138c8a9a5SSteve French 201238c8a9a5SSteve French rc = cifs_posix_lock_set(file, flock); 201338c8a9a5SSteve French if (rc <= FILE_LOCK_DEFERRED) 201438c8a9a5SSteve French return rc; 201538c8a9a5SSteve French 201638c8a9a5SSteve French if (type & server->vals->shared_lock_type) 201738c8a9a5SSteve French posix_lock_type = CIFS_RDLCK; 201838c8a9a5SSteve French else 201938c8a9a5SSteve French posix_lock_type = CIFS_WRLCK; 202038c8a9a5SSteve French 202138c8a9a5SSteve French if (unlock == 1) 202238c8a9a5SSteve French posix_lock_type = CIFS_UNLCK; 202338c8a9a5SSteve French 202438c8a9a5SSteve French rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid, 202538c8a9a5SSteve French hash_lockowner(flock->fl_owner), 202638c8a9a5SSteve French flock->fl_start, length, 202738c8a9a5SSteve French NULL, posix_lock_type, wait_flag); 202838c8a9a5SSteve French goto out; 202938c8a9a5SSteve French } 203038c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 203138c8a9a5SSteve French if (lock) { 203238c8a9a5SSteve French struct cifsLockInfo *lock; 203338c8a9a5SSteve French 203438c8a9a5SSteve French lock = cifs_lock_init(flock->fl_start, length, type, 203538c8a9a5SSteve French flock->fl_flags); 203638c8a9a5SSteve French if (!lock) 203738c8a9a5SSteve French return -ENOMEM; 203838c8a9a5SSteve French 203938c8a9a5SSteve French rc = cifs_lock_add_if(cfile, lock, wait_flag); 204038c8a9a5SSteve French if (rc < 0) { 204138c8a9a5SSteve French kfree(lock); 204238c8a9a5SSteve French return rc; 204338c8a9a5SSteve French } 204438c8a9a5SSteve French if (!rc) 204538c8a9a5SSteve French goto out; 204638c8a9a5SSteve French 204738c8a9a5SSteve French /* 204838c8a9a5SSteve French * Windows 7 server can delay breaking lease from read to None 204938c8a9a5SSteve French * if we set a byte-range lock on a file - break it explicitly 205038c8a9a5SSteve French * before sending the lock to the server to be sure the next 205138c8a9a5SSteve French * read won't conflict with non-overlapted locks due to 205238c8a9a5SSteve French * pagereading. 205338c8a9a5SSteve French */ 205438c8a9a5SSteve French if (!CIFS_CACHE_WRITE(CIFS_I(inode)) && 205538c8a9a5SSteve French CIFS_CACHE_READ(CIFS_I(inode))) { 205638c8a9a5SSteve French cifs_zap_mapping(inode); 205738c8a9a5SSteve French cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n", 205838c8a9a5SSteve French inode); 205938c8a9a5SSteve French CIFS_I(inode)->oplock = 0; 206038c8a9a5SSteve French } 206138c8a9a5SSteve French 206238c8a9a5SSteve French rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, 206338c8a9a5SSteve French type, 1, 0, wait_flag); 206438c8a9a5SSteve French if (rc) { 206538c8a9a5SSteve French kfree(lock); 206638c8a9a5SSteve French return rc; 206738c8a9a5SSteve French } 206838c8a9a5SSteve French 206938c8a9a5SSteve French cifs_lock_add(cfile, lock); 207038c8a9a5SSteve French } else if (unlock) 207138c8a9a5SSteve French rc = server->ops->mand_unlock_range(cfile, flock, xid); 207238c8a9a5SSteve French 207338c8a9a5SSteve French out: 207438c8a9a5SSteve French if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) { 207538c8a9a5SSteve French /* 207638c8a9a5SSteve French * If this is a request to remove all locks because we 207738c8a9a5SSteve French * are closing the file, it doesn't matter if the 207838c8a9a5SSteve French * unlocking failed as both cifs.ko and the SMB server 207938c8a9a5SSteve French * remove the lock on file close 208038c8a9a5SSteve French */ 208138c8a9a5SSteve French if (rc) { 208238c8a9a5SSteve French cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc); 208338c8a9a5SSteve French if (!(flock->fl_flags & FL_CLOSE)) 208438c8a9a5SSteve French return rc; 208538c8a9a5SSteve French } 208638c8a9a5SSteve French rc = locks_lock_file_wait(file, flock); 208738c8a9a5SSteve French } 208838c8a9a5SSteve French return rc; 208938c8a9a5SSteve French } 209038c8a9a5SSteve French 209138c8a9a5SSteve French int cifs_flock(struct file *file, int cmd, struct file_lock *fl) 209238c8a9a5SSteve French { 209338c8a9a5SSteve French int rc, xid; 209438c8a9a5SSteve French int lock = 0, unlock = 0; 209538c8a9a5SSteve French bool wait_flag = false; 209638c8a9a5SSteve French bool posix_lck = false; 209738c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 209838c8a9a5SSteve French struct cifs_tcon *tcon; 209938c8a9a5SSteve French struct cifsFileInfo *cfile; 210038c8a9a5SSteve French __u32 type; 210138c8a9a5SSteve French 210238c8a9a5SSteve French xid = get_xid(); 210338c8a9a5SSteve French 210438c8a9a5SSteve French if (!(fl->fl_flags & FL_FLOCK)) { 210538c8a9a5SSteve French rc = -ENOLCK; 210638c8a9a5SSteve French free_xid(xid); 210738c8a9a5SSteve French return rc; 210838c8a9a5SSteve French } 210938c8a9a5SSteve French 211038c8a9a5SSteve French cfile = (struct cifsFileInfo *)file->private_data; 211138c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 211238c8a9a5SSteve French 211338c8a9a5SSteve French cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag, 211438c8a9a5SSteve French tcon->ses->server); 211538c8a9a5SSteve French cifs_sb = CIFS_FILE_SB(file); 211638c8a9a5SSteve French 211738c8a9a5SSteve French if (cap_unix(tcon->ses) && 211838c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 211938c8a9a5SSteve French ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 212038c8a9a5SSteve French posix_lck = true; 212138c8a9a5SSteve French 212238c8a9a5SSteve French if (!lock && !unlock) { 212338c8a9a5SSteve French /* 212438c8a9a5SSteve French * if no lock or unlock then nothing to do since we do not 212538c8a9a5SSteve French * know what it is 212638c8a9a5SSteve French */ 212738c8a9a5SSteve French rc = -EOPNOTSUPP; 212838c8a9a5SSteve French free_xid(xid); 212938c8a9a5SSteve French return rc; 213038c8a9a5SSteve French } 213138c8a9a5SSteve French 213238c8a9a5SSteve French rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock, 213338c8a9a5SSteve French xid); 213438c8a9a5SSteve French free_xid(xid); 213538c8a9a5SSteve French return rc; 213638c8a9a5SSteve French 213738c8a9a5SSteve French 213838c8a9a5SSteve French } 213938c8a9a5SSteve French 214038c8a9a5SSteve French int cifs_lock(struct file *file, int cmd, struct file_lock *flock) 214138c8a9a5SSteve French { 214238c8a9a5SSteve French int rc, xid; 214338c8a9a5SSteve French int lock = 0, unlock = 0; 214438c8a9a5SSteve French bool wait_flag = false; 214538c8a9a5SSteve French bool posix_lck = false; 214638c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 214738c8a9a5SSteve French struct cifs_tcon *tcon; 214838c8a9a5SSteve French struct cifsFileInfo *cfile; 214938c8a9a5SSteve French __u32 type; 215038c8a9a5SSteve French 215138c8a9a5SSteve French rc = -EACCES; 215238c8a9a5SSteve French xid = get_xid(); 215338c8a9a5SSteve French 215438c8a9a5SSteve French cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd, 215538c8a9a5SSteve French flock->fl_flags, flock->fl_type, (long long)flock->fl_start, 215638c8a9a5SSteve French (long long)flock->fl_end); 215738c8a9a5SSteve French 215838c8a9a5SSteve French cfile = (struct cifsFileInfo *)file->private_data; 215938c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 216038c8a9a5SSteve French 216138c8a9a5SSteve French cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag, 216238c8a9a5SSteve French tcon->ses->server); 216338c8a9a5SSteve French cifs_sb = CIFS_FILE_SB(file); 216438c8a9a5SSteve French set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags); 216538c8a9a5SSteve French 216638c8a9a5SSteve French if (cap_unix(tcon->ses) && 216738c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 216838c8a9a5SSteve French ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 216938c8a9a5SSteve French posix_lck = true; 217038c8a9a5SSteve French /* 217138c8a9a5SSteve French * BB add code here to normalize offset and length to account for 217238c8a9a5SSteve French * negative length which we can not accept over the wire. 217338c8a9a5SSteve French */ 217438c8a9a5SSteve French if (IS_GETLK(cmd)) { 217538c8a9a5SSteve French rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid); 217638c8a9a5SSteve French free_xid(xid); 217738c8a9a5SSteve French return rc; 217838c8a9a5SSteve French } 217938c8a9a5SSteve French 218038c8a9a5SSteve French if (!lock && !unlock) { 218138c8a9a5SSteve French /* 218238c8a9a5SSteve French * if no lock or unlock then nothing to do since we do not 218338c8a9a5SSteve French * know what it is 218438c8a9a5SSteve French */ 218538c8a9a5SSteve French free_xid(xid); 218638c8a9a5SSteve French return -EOPNOTSUPP; 218738c8a9a5SSteve French } 218838c8a9a5SSteve French 218938c8a9a5SSteve French rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock, 219038c8a9a5SSteve French xid); 219138c8a9a5SSteve French free_xid(xid); 219238c8a9a5SSteve French return rc; 219338c8a9a5SSteve French } 219438c8a9a5SSteve French 219538c8a9a5SSteve French /* 219638c8a9a5SSteve French * update the file size (if needed) after a write. Should be called with 219738c8a9a5SSteve French * the inode->i_lock held 219838c8a9a5SSteve French */ 219938c8a9a5SSteve French void 220038c8a9a5SSteve French cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, 220138c8a9a5SSteve French unsigned int bytes_written) 220238c8a9a5SSteve French { 220338c8a9a5SSteve French loff_t end_of_write = offset + bytes_written; 220438c8a9a5SSteve French 220538c8a9a5SSteve French if (end_of_write > cifsi->server_eof) 220638c8a9a5SSteve French cifsi->server_eof = end_of_write; 220738c8a9a5SSteve French } 220838c8a9a5SSteve French 220938c8a9a5SSteve French static ssize_t 221038c8a9a5SSteve French cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data, 221138c8a9a5SSteve French size_t write_size, loff_t *offset) 221238c8a9a5SSteve French { 221338c8a9a5SSteve French int rc = 0; 221438c8a9a5SSteve French unsigned int bytes_written = 0; 221538c8a9a5SSteve French unsigned int total_written; 221638c8a9a5SSteve French struct cifs_tcon *tcon; 221738c8a9a5SSteve French struct TCP_Server_Info *server; 221838c8a9a5SSteve French unsigned int xid; 221938c8a9a5SSteve French struct dentry *dentry = open_file->dentry; 222038c8a9a5SSteve French struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry)); 222138c8a9a5SSteve French struct cifs_io_parms io_parms = {0}; 222238c8a9a5SSteve French 222338c8a9a5SSteve French cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n", 222438c8a9a5SSteve French write_size, *offset, dentry); 222538c8a9a5SSteve French 222638c8a9a5SSteve French tcon = tlink_tcon(open_file->tlink); 222738c8a9a5SSteve French server = tcon->ses->server; 222838c8a9a5SSteve French 222938c8a9a5SSteve French if (!server->ops->sync_write) 223038c8a9a5SSteve French return -ENOSYS; 223138c8a9a5SSteve French 223238c8a9a5SSteve French xid = get_xid(); 223338c8a9a5SSteve French 223438c8a9a5SSteve French for (total_written = 0; write_size > total_written; 223538c8a9a5SSteve French total_written += bytes_written) { 223638c8a9a5SSteve French rc = -EAGAIN; 223738c8a9a5SSteve French while (rc == -EAGAIN) { 223838c8a9a5SSteve French struct kvec iov[2]; 223938c8a9a5SSteve French unsigned int len; 224038c8a9a5SSteve French 224138c8a9a5SSteve French if (open_file->invalidHandle) { 224238c8a9a5SSteve French /* we could deadlock if we called 224338c8a9a5SSteve French filemap_fdatawait from here so tell 224438c8a9a5SSteve French reopen_file not to flush data to 224538c8a9a5SSteve French server now */ 224638c8a9a5SSteve French rc = cifs_reopen_file(open_file, false); 224738c8a9a5SSteve French if (rc != 0) 224838c8a9a5SSteve French break; 224938c8a9a5SSteve French } 225038c8a9a5SSteve French 225138c8a9a5SSteve French len = min(server->ops->wp_retry_size(d_inode(dentry)), 225238c8a9a5SSteve French (unsigned int)write_size - total_written); 225338c8a9a5SSteve French /* iov[0] is reserved for smb header */ 225438c8a9a5SSteve French iov[1].iov_base = (char *)write_data + total_written; 225538c8a9a5SSteve French iov[1].iov_len = len; 225638c8a9a5SSteve French io_parms.pid = pid; 225738c8a9a5SSteve French io_parms.tcon = tcon; 225838c8a9a5SSteve French io_parms.offset = *offset; 225938c8a9a5SSteve French io_parms.length = len; 226038c8a9a5SSteve French rc = server->ops->sync_write(xid, &open_file->fid, 226138c8a9a5SSteve French &io_parms, &bytes_written, iov, 1); 226238c8a9a5SSteve French } 226338c8a9a5SSteve French if (rc || (bytes_written == 0)) { 226438c8a9a5SSteve French if (total_written) 226538c8a9a5SSteve French break; 226638c8a9a5SSteve French else { 226738c8a9a5SSteve French free_xid(xid); 226838c8a9a5SSteve French return rc; 226938c8a9a5SSteve French } 227038c8a9a5SSteve French } else { 227138c8a9a5SSteve French spin_lock(&d_inode(dentry)->i_lock); 227238c8a9a5SSteve French cifs_update_eof(cifsi, *offset, bytes_written); 227338c8a9a5SSteve French spin_unlock(&d_inode(dentry)->i_lock); 227438c8a9a5SSteve French *offset += bytes_written; 227538c8a9a5SSteve French } 227638c8a9a5SSteve French } 227738c8a9a5SSteve French 227838c8a9a5SSteve French cifs_stats_bytes_written(tcon, total_written); 227938c8a9a5SSteve French 228038c8a9a5SSteve French if (total_written > 0) { 228138c8a9a5SSteve French spin_lock(&d_inode(dentry)->i_lock); 228238c8a9a5SSteve French if (*offset > d_inode(dentry)->i_size) { 228338c8a9a5SSteve French i_size_write(d_inode(dentry), *offset); 228438c8a9a5SSteve French d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9; 228538c8a9a5SSteve French } 228638c8a9a5SSteve French spin_unlock(&d_inode(dentry)->i_lock); 228738c8a9a5SSteve French } 228838c8a9a5SSteve French mark_inode_dirty_sync(d_inode(dentry)); 228938c8a9a5SSteve French free_xid(xid); 229038c8a9a5SSteve French return total_written; 229138c8a9a5SSteve French } 229238c8a9a5SSteve French 229338c8a9a5SSteve French struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, 229438c8a9a5SSteve French bool fsuid_only) 229538c8a9a5SSteve French { 229638c8a9a5SSteve French struct cifsFileInfo *open_file = NULL; 229738c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 229838c8a9a5SSteve French 229938c8a9a5SSteve French /* only filter by fsuid on multiuser mounts */ 230038c8a9a5SSteve French if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 230138c8a9a5SSteve French fsuid_only = false; 230238c8a9a5SSteve French 230338c8a9a5SSteve French spin_lock(&cifs_inode->open_file_lock); 230438c8a9a5SSteve French /* we could simply get the first_list_entry since write-only entries 230538c8a9a5SSteve French are always at the end of the list but since the first entry might 230638c8a9a5SSteve French have a close pending, we go through the whole list */ 230738c8a9a5SSteve French list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 230838c8a9a5SSteve French if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 230938c8a9a5SSteve French continue; 231038c8a9a5SSteve French if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { 231138c8a9a5SSteve French if ((!open_file->invalidHandle)) { 231238c8a9a5SSteve French /* found a good file */ 231338c8a9a5SSteve French /* lock it so it will not be closed on us */ 231438c8a9a5SSteve French cifsFileInfo_get(open_file); 231538c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 231638c8a9a5SSteve French return open_file; 231738c8a9a5SSteve French } /* else might as well continue, and look for 231838c8a9a5SSteve French another, or simply have the caller reopen it 231938c8a9a5SSteve French again rather than trying to fix this handle */ 232038c8a9a5SSteve French } else /* write only file */ 232138c8a9a5SSteve French break; /* write only files are last so must be done */ 232238c8a9a5SSteve French } 232338c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 232438c8a9a5SSteve French return NULL; 232538c8a9a5SSteve French } 232638c8a9a5SSteve French 232738c8a9a5SSteve French /* Return -EBADF if no handle is found and general rc otherwise */ 232838c8a9a5SSteve French int 232938c8a9a5SSteve French cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags, 233038c8a9a5SSteve French struct cifsFileInfo **ret_file) 233138c8a9a5SSteve French { 233238c8a9a5SSteve French struct cifsFileInfo *open_file, *inv_file = NULL; 233338c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 233438c8a9a5SSteve French bool any_available = false; 233538c8a9a5SSteve French int rc = -EBADF; 233638c8a9a5SSteve French unsigned int refind = 0; 233738c8a9a5SSteve French bool fsuid_only = flags & FIND_WR_FSUID_ONLY; 233838c8a9a5SSteve French bool with_delete = flags & FIND_WR_WITH_DELETE; 233938c8a9a5SSteve French *ret_file = NULL; 234038c8a9a5SSteve French 234138c8a9a5SSteve French /* 234238c8a9a5SSteve French * Having a null inode here (because mapping->host was set to zero by 234338c8a9a5SSteve French * the VFS or MM) should not happen but we had reports of on oops (due 234438c8a9a5SSteve French * to it being zero) during stress testcases so we need to check for it 234538c8a9a5SSteve French */ 234638c8a9a5SSteve French 234738c8a9a5SSteve French if (cifs_inode == NULL) { 234838c8a9a5SSteve French cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n"); 234938c8a9a5SSteve French dump_stack(); 235038c8a9a5SSteve French return rc; 235138c8a9a5SSteve French } 235238c8a9a5SSteve French 235338c8a9a5SSteve French cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb); 235438c8a9a5SSteve French 235538c8a9a5SSteve French /* only filter by fsuid on multiuser mounts */ 235638c8a9a5SSteve French if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 235738c8a9a5SSteve French fsuid_only = false; 235838c8a9a5SSteve French 235938c8a9a5SSteve French spin_lock(&cifs_inode->open_file_lock); 236038c8a9a5SSteve French refind_writable: 236138c8a9a5SSteve French if (refind > MAX_REOPEN_ATT) { 236238c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 236338c8a9a5SSteve French return rc; 236438c8a9a5SSteve French } 236538c8a9a5SSteve French list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 236638c8a9a5SSteve French if (!any_available && open_file->pid != current->tgid) 236738c8a9a5SSteve French continue; 236838c8a9a5SSteve French if (fsuid_only && !uid_eq(open_file->uid, current_fsuid())) 236938c8a9a5SSteve French continue; 237038c8a9a5SSteve French if (with_delete && !(open_file->fid.access & DELETE)) 237138c8a9a5SSteve French continue; 237238c8a9a5SSteve French if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 237338c8a9a5SSteve French if (!open_file->invalidHandle) { 237438c8a9a5SSteve French /* found a good writable file */ 237538c8a9a5SSteve French cifsFileInfo_get(open_file); 237638c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 237738c8a9a5SSteve French *ret_file = open_file; 237838c8a9a5SSteve French return 0; 237938c8a9a5SSteve French } else { 238038c8a9a5SSteve French if (!inv_file) 238138c8a9a5SSteve French inv_file = open_file; 238238c8a9a5SSteve French } 238338c8a9a5SSteve French } 238438c8a9a5SSteve French } 238538c8a9a5SSteve French /* couldn't find useable FH with same pid, try any available */ 238638c8a9a5SSteve French if (!any_available) { 238738c8a9a5SSteve French any_available = true; 238838c8a9a5SSteve French goto refind_writable; 238938c8a9a5SSteve French } 239038c8a9a5SSteve French 239138c8a9a5SSteve French if (inv_file) { 239238c8a9a5SSteve French any_available = false; 239338c8a9a5SSteve French cifsFileInfo_get(inv_file); 239438c8a9a5SSteve French } 239538c8a9a5SSteve French 239638c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 239738c8a9a5SSteve French 239838c8a9a5SSteve French if (inv_file) { 239938c8a9a5SSteve French rc = cifs_reopen_file(inv_file, false); 240038c8a9a5SSteve French if (!rc) { 240138c8a9a5SSteve French *ret_file = inv_file; 240238c8a9a5SSteve French return 0; 240338c8a9a5SSteve French } 240438c8a9a5SSteve French 240538c8a9a5SSteve French spin_lock(&cifs_inode->open_file_lock); 240638c8a9a5SSteve French list_move_tail(&inv_file->flist, &cifs_inode->openFileList); 240738c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 240838c8a9a5SSteve French cifsFileInfo_put(inv_file); 240938c8a9a5SSteve French ++refind; 241038c8a9a5SSteve French inv_file = NULL; 241138c8a9a5SSteve French spin_lock(&cifs_inode->open_file_lock); 241238c8a9a5SSteve French goto refind_writable; 241338c8a9a5SSteve French } 241438c8a9a5SSteve French 241538c8a9a5SSteve French return rc; 241638c8a9a5SSteve French } 241738c8a9a5SSteve French 241838c8a9a5SSteve French struct cifsFileInfo * 241938c8a9a5SSteve French find_writable_file(struct cifsInodeInfo *cifs_inode, int flags) 242038c8a9a5SSteve French { 242138c8a9a5SSteve French struct cifsFileInfo *cfile; 242238c8a9a5SSteve French int rc; 242338c8a9a5SSteve French 242438c8a9a5SSteve French rc = cifs_get_writable_file(cifs_inode, flags, &cfile); 242538c8a9a5SSteve French if (rc) 242638c8a9a5SSteve French cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc); 242738c8a9a5SSteve French 242838c8a9a5SSteve French return cfile; 242938c8a9a5SSteve French } 243038c8a9a5SSteve French 243138c8a9a5SSteve French int 243238c8a9a5SSteve French cifs_get_writable_path(struct cifs_tcon *tcon, const char *name, 243338c8a9a5SSteve French int flags, 243438c8a9a5SSteve French struct cifsFileInfo **ret_file) 243538c8a9a5SSteve French { 243638c8a9a5SSteve French struct cifsFileInfo *cfile; 243738c8a9a5SSteve French void *page = alloc_dentry_path(); 243838c8a9a5SSteve French 243938c8a9a5SSteve French *ret_file = NULL; 244038c8a9a5SSteve French 244138c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 244238c8a9a5SSteve French list_for_each_entry(cfile, &tcon->openFileList, tlist) { 244338c8a9a5SSteve French struct cifsInodeInfo *cinode; 244438c8a9a5SSteve French const char *full_path = build_path_from_dentry(cfile->dentry, page); 244538c8a9a5SSteve French if (IS_ERR(full_path)) { 244638c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 244738c8a9a5SSteve French free_dentry_path(page); 244838c8a9a5SSteve French return PTR_ERR(full_path); 244938c8a9a5SSteve French } 245038c8a9a5SSteve French if (strcmp(full_path, name)) 245138c8a9a5SSteve French continue; 245238c8a9a5SSteve French 245338c8a9a5SSteve French cinode = CIFS_I(d_inode(cfile->dentry)); 245438c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 245538c8a9a5SSteve French free_dentry_path(page); 245638c8a9a5SSteve French return cifs_get_writable_file(cinode, flags, ret_file); 245738c8a9a5SSteve French } 245838c8a9a5SSteve French 245938c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 246038c8a9a5SSteve French free_dentry_path(page); 246138c8a9a5SSteve French return -ENOENT; 246238c8a9a5SSteve French } 246338c8a9a5SSteve French 246438c8a9a5SSteve French int 246538c8a9a5SSteve French cifs_get_readable_path(struct cifs_tcon *tcon, const char *name, 246638c8a9a5SSteve French struct cifsFileInfo **ret_file) 246738c8a9a5SSteve French { 246838c8a9a5SSteve French struct cifsFileInfo *cfile; 246938c8a9a5SSteve French void *page = alloc_dentry_path(); 247038c8a9a5SSteve French 247138c8a9a5SSteve French *ret_file = NULL; 247238c8a9a5SSteve French 247338c8a9a5SSteve French spin_lock(&tcon->open_file_lock); 247438c8a9a5SSteve French list_for_each_entry(cfile, &tcon->openFileList, tlist) { 247538c8a9a5SSteve French struct cifsInodeInfo *cinode; 247638c8a9a5SSteve French const char *full_path = build_path_from_dentry(cfile->dentry, page); 247738c8a9a5SSteve French if (IS_ERR(full_path)) { 247838c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 247938c8a9a5SSteve French free_dentry_path(page); 248038c8a9a5SSteve French return PTR_ERR(full_path); 248138c8a9a5SSteve French } 248238c8a9a5SSteve French if (strcmp(full_path, name)) 248338c8a9a5SSteve French continue; 248438c8a9a5SSteve French 248538c8a9a5SSteve French cinode = CIFS_I(d_inode(cfile->dentry)); 248638c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 248738c8a9a5SSteve French free_dentry_path(page); 248838c8a9a5SSteve French *ret_file = find_readable_file(cinode, 0); 248938c8a9a5SSteve French return *ret_file ? 0 : -ENOENT; 249038c8a9a5SSteve French } 249138c8a9a5SSteve French 249238c8a9a5SSteve French spin_unlock(&tcon->open_file_lock); 249338c8a9a5SSteve French free_dentry_path(page); 249438c8a9a5SSteve French return -ENOENT; 249538c8a9a5SSteve French } 249638c8a9a5SSteve French 249738c8a9a5SSteve French void 249838c8a9a5SSteve French cifs_writedata_release(struct kref *refcount) 249938c8a9a5SSteve French { 250038c8a9a5SSteve French struct cifs_writedata *wdata = container_of(refcount, 250138c8a9a5SSteve French struct cifs_writedata, refcount); 250238c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 250338c8a9a5SSteve French if (wdata->mr) { 250438c8a9a5SSteve French smbd_deregister_mr(wdata->mr); 250538c8a9a5SSteve French wdata->mr = NULL; 250638c8a9a5SSteve French } 250738c8a9a5SSteve French #endif 250838c8a9a5SSteve French 250938c8a9a5SSteve French if (wdata->cfile) 251038c8a9a5SSteve French cifsFileInfo_put(wdata->cfile); 251138c8a9a5SSteve French 251238c8a9a5SSteve French kfree(wdata); 251338c8a9a5SSteve French } 251438c8a9a5SSteve French 251538c8a9a5SSteve French /* 251638c8a9a5SSteve French * Write failed with a retryable error. Resend the write request. It's also 251738c8a9a5SSteve French * possible that the page was redirtied so re-clean the page. 251838c8a9a5SSteve French */ 251938c8a9a5SSteve French static void 252038c8a9a5SSteve French cifs_writev_requeue(struct cifs_writedata *wdata) 252138c8a9a5SSteve French { 252238c8a9a5SSteve French int rc = 0; 252338c8a9a5SSteve French struct inode *inode = d_inode(wdata->cfile->dentry); 252438c8a9a5SSteve French struct TCP_Server_Info *server; 252538c8a9a5SSteve French unsigned int rest_len = wdata->bytes; 252638c8a9a5SSteve French loff_t fpos = wdata->offset; 252738c8a9a5SSteve French 252838c8a9a5SSteve French server = tlink_tcon(wdata->cfile->tlink)->ses->server; 252938c8a9a5SSteve French do { 253038c8a9a5SSteve French struct cifs_writedata *wdata2; 253138c8a9a5SSteve French unsigned int wsize, cur_len; 253238c8a9a5SSteve French 253338c8a9a5SSteve French wsize = server->ops->wp_retry_size(inode); 253438c8a9a5SSteve French if (wsize < rest_len) { 253538c8a9a5SSteve French if (wsize < PAGE_SIZE) { 253638c8a9a5SSteve French rc = -EOPNOTSUPP; 253738c8a9a5SSteve French break; 253838c8a9a5SSteve French } 253938c8a9a5SSteve French cur_len = min(round_down(wsize, PAGE_SIZE), rest_len); 254038c8a9a5SSteve French } else { 254138c8a9a5SSteve French cur_len = rest_len; 254238c8a9a5SSteve French } 254338c8a9a5SSteve French 254438c8a9a5SSteve French wdata2 = cifs_writedata_alloc(cifs_writev_complete); 254538c8a9a5SSteve French if (!wdata2) { 254638c8a9a5SSteve French rc = -ENOMEM; 254738c8a9a5SSteve French break; 254838c8a9a5SSteve French } 254938c8a9a5SSteve French 255038c8a9a5SSteve French wdata2->sync_mode = wdata->sync_mode; 255138c8a9a5SSteve French wdata2->offset = fpos; 255238c8a9a5SSteve French wdata2->bytes = cur_len; 255338c8a9a5SSteve French wdata2->iter = wdata->iter; 255438c8a9a5SSteve French 255538c8a9a5SSteve French iov_iter_advance(&wdata2->iter, fpos - wdata->offset); 255638c8a9a5SSteve French iov_iter_truncate(&wdata2->iter, wdata2->bytes); 255738c8a9a5SSteve French 255838c8a9a5SSteve French if (iov_iter_is_xarray(&wdata2->iter)) 255938c8a9a5SSteve French /* Check for pages having been redirtied and clean 256038c8a9a5SSteve French * them. We can do this by walking the xarray. If 256138c8a9a5SSteve French * it's not an xarray, then it's a DIO and we shouldn't 256238c8a9a5SSteve French * be mucking around with the page bits. 256338c8a9a5SSteve French */ 256438c8a9a5SSteve French cifs_undirty_folios(inode, fpos, cur_len); 256538c8a9a5SSteve French 256638c8a9a5SSteve French rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, 256738c8a9a5SSteve French &wdata2->cfile); 256838c8a9a5SSteve French if (!wdata2->cfile) { 256938c8a9a5SSteve French cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n", 257038c8a9a5SSteve French rc); 257138c8a9a5SSteve French if (!is_retryable_error(rc)) 257238c8a9a5SSteve French rc = -EBADF; 257338c8a9a5SSteve French } else { 257438c8a9a5SSteve French wdata2->pid = wdata2->cfile->pid; 257538c8a9a5SSteve French rc = server->ops->async_writev(wdata2, 257638c8a9a5SSteve French cifs_writedata_release); 257738c8a9a5SSteve French } 257838c8a9a5SSteve French 257938c8a9a5SSteve French kref_put(&wdata2->refcount, cifs_writedata_release); 258038c8a9a5SSteve French if (rc) { 258138c8a9a5SSteve French if (is_retryable_error(rc)) 258238c8a9a5SSteve French continue; 258338c8a9a5SSteve French fpos += cur_len; 258438c8a9a5SSteve French rest_len -= cur_len; 258538c8a9a5SSteve French break; 258638c8a9a5SSteve French } 258738c8a9a5SSteve French 258838c8a9a5SSteve French fpos += cur_len; 258938c8a9a5SSteve French rest_len -= cur_len; 259038c8a9a5SSteve French } while (rest_len > 0); 259138c8a9a5SSteve French 259238c8a9a5SSteve French /* Clean up remaining pages from the original wdata */ 259338c8a9a5SSteve French if (iov_iter_is_xarray(&wdata->iter)) 259438c8a9a5SSteve French cifs_pages_write_failed(inode, fpos, rest_len); 259538c8a9a5SSteve French 259638c8a9a5SSteve French if (rc != 0 && !is_retryable_error(rc)) 259738c8a9a5SSteve French mapping_set_error(inode->i_mapping, rc); 259838c8a9a5SSteve French kref_put(&wdata->refcount, cifs_writedata_release); 259938c8a9a5SSteve French } 260038c8a9a5SSteve French 260138c8a9a5SSteve French void 260238c8a9a5SSteve French cifs_writev_complete(struct work_struct *work) 260338c8a9a5SSteve French { 260438c8a9a5SSteve French struct cifs_writedata *wdata = container_of(work, 260538c8a9a5SSteve French struct cifs_writedata, work); 260638c8a9a5SSteve French struct inode *inode = d_inode(wdata->cfile->dentry); 260738c8a9a5SSteve French 260838c8a9a5SSteve French if (wdata->result == 0) { 260938c8a9a5SSteve French spin_lock(&inode->i_lock); 261038c8a9a5SSteve French cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes); 261138c8a9a5SSteve French spin_unlock(&inode->i_lock); 261238c8a9a5SSteve French cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink), 261338c8a9a5SSteve French wdata->bytes); 261438c8a9a5SSteve French } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN) 261538c8a9a5SSteve French return cifs_writev_requeue(wdata); 261638c8a9a5SSteve French 261738c8a9a5SSteve French if (wdata->result == -EAGAIN) 261838c8a9a5SSteve French cifs_pages_write_redirty(inode, wdata->offset, wdata->bytes); 261938c8a9a5SSteve French else if (wdata->result < 0) 262038c8a9a5SSteve French cifs_pages_write_failed(inode, wdata->offset, wdata->bytes); 262138c8a9a5SSteve French else 262238c8a9a5SSteve French cifs_pages_written_back(inode, wdata->offset, wdata->bytes); 262338c8a9a5SSteve French 262438c8a9a5SSteve French if (wdata->result != -EAGAIN) 262538c8a9a5SSteve French mapping_set_error(inode->i_mapping, wdata->result); 262638c8a9a5SSteve French kref_put(&wdata->refcount, cifs_writedata_release); 262738c8a9a5SSteve French } 262838c8a9a5SSteve French 262938c8a9a5SSteve French struct cifs_writedata *cifs_writedata_alloc(work_func_t complete) 263038c8a9a5SSteve French { 263138c8a9a5SSteve French struct cifs_writedata *wdata; 263238c8a9a5SSteve French 263338c8a9a5SSteve French wdata = kzalloc(sizeof(*wdata), GFP_NOFS); 263438c8a9a5SSteve French if (wdata != NULL) { 263538c8a9a5SSteve French kref_init(&wdata->refcount); 263638c8a9a5SSteve French INIT_LIST_HEAD(&wdata->list); 263738c8a9a5SSteve French init_completion(&wdata->done); 263838c8a9a5SSteve French INIT_WORK(&wdata->work, complete); 263938c8a9a5SSteve French } 264038c8a9a5SSteve French return wdata; 264138c8a9a5SSteve French } 264238c8a9a5SSteve French 264338c8a9a5SSteve French static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) 264438c8a9a5SSteve French { 264538c8a9a5SSteve French struct address_space *mapping = page->mapping; 264638c8a9a5SSteve French loff_t offset = (loff_t)page->index << PAGE_SHIFT; 264738c8a9a5SSteve French char *write_data; 264838c8a9a5SSteve French int rc = -EFAULT; 264938c8a9a5SSteve French int bytes_written = 0; 265038c8a9a5SSteve French struct inode *inode; 265138c8a9a5SSteve French struct cifsFileInfo *open_file; 265238c8a9a5SSteve French 265338c8a9a5SSteve French if (!mapping || !mapping->host) 265438c8a9a5SSteve French return -EFAULT; 265538c8a9a5SSteve French 265638c8a9a5SSteve French inode = page->mapping->host; 265738c8a9a5SSteve French 265838c8a9a5SSteve French offset += (loff_t)from; 265938c8a9a5SSteve French write_data = kmap(page); 266038c8a9a5SSteve French write_data += from; 266138c8a9a5SSteve French 266238c8a9a5SSteve French if ((to > PAGE_SIZE) || (from > to)) { 266338c8a9a5SSteve French kunmap(page); 266438c8a9a5SSteve French return -EIO; 266538c8a9a5SSteve French } 266638c8a9a5SSteve French 266738c8a9a5SSteve French /* racing with truncate? */ 266838c8a9a5SSteve French if (offset > mapping->host->i_size) { 266938c8a9a5SSteve French kunmap(page); 267038c8a9a5SSteve French return 0; /* don't care */ 267138c8a9a5SSteve French } 267238c8a9a5SSteve French 267338c8a9a5SSteve French /* check to make sure that we are not extending the file */ 267438c8a9a5SSteve French if (mapping->host->i_size - offset < (loff_t)to) 267538c8a9a5SSteve French to = (unsigned)(mapping->host->i_size - offset); 267638c8a9a5SSteve French 267738c8a9a5SSteve French rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY, 267838c8a9a5SSteve French &open_file); 267938c8a9a5SSteve French if (!rc) { 268038c8a9a5SSteve French bytes_written = cifs_write(open_file, open_file->pid, 268138c8a9a5SSteve French write_data, to - from, &offset); 268238c8a9a5SSteve French cifsFileInfo_put(open_file); 268338c8a9a5SSteve French /* Does mm or vfs already set times? */ 268423171df5SJeff Layton simple_inode_init_ts(inode); 268538c8a9a5SSteve French if ((bytes_written > 0) && (offset)) 268638c8a9a5SSteve French rc = 0; 268738c8a9a5SSteve French else if (bytes_written < 0) 268838c8a9a5SSteve French rc = bytes_written; 268938c8a9a5SSteve French else 269038c8a9a5SSteve French rc = -EFAULT; 269138c8a9a5SSteve French } else { 269238c8a9a5SSteve French cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc); 269338c8a9a5SSteve French if (!is_retryable_error(rc)) 269438c8a9a5SSteve French rc = -EIO; 269538c8a9a5SSteve French } 269638c8a9a5SSteve French 269738c8a9a5SSteve French kunmap(page); 269838c8a9a5SSteve French return rc; 269938c8a9a5SSteve French } 270038c8a9a5SSteve French 270138c8a9a5SSteve French /* 270238c8a9a5SSteve French * Extend the region to be written back to include subsequent contiguously 270338c8a9a5SSteve French * dirty pages if possible, but don't sleep while doing so. 270438c8a9a5SSteve French */ 270538c8a9a5SSteve French static void cifs_extend_writeback(struct address_space *mapping, 2706e45deec3SDavid Howells struct xa_state *xas, 270738c8a9a5SSteve French long *_count, 270838c8a9a5SSteve French loff_t start, 270938c8a9a5SSteve French int max_pages, 2710e45deec3SDavid Howells loff_t max_len, 2711e45deec3SDavid Howells size_t *_len) 271238c8a9a5SSteve French { 271338c8a9a5SSteve French struct folio_batch batch; 271438c8a9a5SSteve French struct folio *folio; 2715e45deec3SDavid Howells unsigned int nr_pages; 2716e45deec3SDavid Howells pgoff_t index = (start + *_len) / PAGE_SIZE; 2717e45deec3SDavid Howells size_t len; 271838c8a9a5SSteve French bool stop = true; 271938c8a9a5SSteve French unsigned int i; 272038c8a9a5SSteve French 272138c8a9a5SSteve French folio_batch_init(&batch); 272238c8a9a5SSteve French 272338c8a9a5SSteve French do { 272438c8a9a5SSteve French /* Firstly, we gather up a batch of contiguous dirty pages 272538c8a9a5SSteve French * under the RCU read lock - but we can't clear the dirty flags 272638c8a9a5SSteve French * there if any of those pages are mapped. 272738c8a9a5SSteve French */ 272838c8a9a5SSteve French rcu_read_lock(); 272938c8a9a5SSteve French 2730e45deec3SDavid Howells xas_for_each(xas, folio, ULONG_MAX) { 273138c8a9a5SSteve French stop = true; 2732e45deec3SDavid Howells if (xas_retry(xas, folio)) 273338c8a9a5SSteve French continue; 273438c8a9a5SSteve French if (xa_is_value(folio)) 273538c8a9a5SSteve French break; 2736e45deec3SDavid Howells if (folio->index != index) { 2737e45deec3SDavid Howells xas_reset(xas); 273838c8a9a5SSteve French break; 2739e45deec3SDavid Howells } 2740e45deec3SDavid Howells 274138c8a9a5SSteve French if (!folio_try_get_rcu(folio)) { 2742e45deec3SDavid Howells xas_reset(xas); 274338c8a9a5SSteve French continue; 274438c8a9a5SSteve French } 274538c8a9a5SSteve French nr_pages = folio_nr_pages(folio); 2746e45deec3SDavid Howells if (nr_pages > max_pages) { 2747e45deec3SDavid Howells xas_reset(xas); 274838c8a9a5SSteve French break; 2749e45deec3SDavid Howells } 275038c8a9a5SSteve French 275138c8a9a5SSteve French /* Has the page moved or been split? */ 2752e45deec3SDavid Howells if (unlikely(folio != xas_reload(xas))) { 275338c8a9a5SSteve French folio_put(folio); 2754e45deec3SDavid Howells xas_reset(xas); 275538c8a9a5SSteve French break; 275638c8a9a5SSteve French } 275738c8a9a5SSteve French 275838c8a9a5SSteve French if (!folio_trylock(folio)) { 275938c8a9a5SSteve French folio_put(folio); 2760e45deec3SDavid Howells xas_reset(xas); 276138c8a9a5SSteve French break; 276238c8a9a5SSteve French } 2763e45deec3SDavid Howells if (!folio_test_dirty(folio) || 2764e45deec3SDavid Howells folio_test_writeback(folio)) { 276538c8a9a5SSteve French folio_unlock(folio); 276638c8a9a5SSteve French folio_put(folio); 2767e45deec3SDavid Howells xas_reset(xas); 276838c8a9a5SSteve French break; 276938c8a9a5SSteve French } 277038c8a9a5SSteve French 277138c8a9a5SSteve French max_pages -= nr_pages; 2772e45deec3SDavid Howells len = folio_size(folio); 277338c8a9a5SSteve French stop = false; 277438c8a9a5SSteve French 277538c8a9a5SSteve French index += nr_pages; 2776e45deec3SDavid Howells *_count -= nr_pages; 2777e45deec3SDavid Howells *_len += len; 2778e45deec3SDavid Howells if (max_pages <= 0 || *_len >= max_len || *_count <= 0) 2779e45deec3SDavid Howells stop = true; 2780e45deec3SDavid Howells 278138c8a9a5SSteve French if (!folio_batch_add(&batch, folio)) 278238c8a9a5SSteve French break; 278338c8a9a5SSteve French if (stop) 278438c8a9a5SSteve French break; 278538c8a9a5SSteve French } 278638c8a9a5SSteve French 2787e45deec3SDavid Howells xas_pause(xas); 278838c8a9a5SSteve French rcu_read_unlock(); 278938c8a9a5SSteve French 279038c8a9a5SSteve French /* Now, if we obtained any pages, we can shift them to being 279138c8a9a5SSteve French * writable and mark them for caching. 279238c8a9a5SSteve French */ 279338c8a9a5SSteve French if (!folio_batch_count(&batch)) 279438c8a9a5SSteve French break; 279538c8a9a5SSteve French 279638c8a9a5SSteve French for (i = 0; i < folio_batch_count(&batch); i++) { 279738c8a9a5SSteve French folio = batch.folios[i]; 279838c8a9a5SSteve French /* The folio should be locked, dirty and not undergoing 279938c8a9a5SSteve French * writeback from the loop above. 280038c8a9a5SSteve French */ 280138c8a9a5SSteve French if (!folio_clear_dirty_for_io(folio)) 280238c8a9a5SSteve French WARN_ON(1); 28032e411c57SMatthew Wilcox (Oracle) folio_start_writeback(folio); 280438c8a9a5SSteve French folio_unlock(folio); 280538c8a9a5SSteve French } 280638c8a9a5SSteve French 280738c8a9a5SSteve French folio_batch_release(&batch); 280838c8a9a5SSteve French cond_resched(); 280938c8a9a5SSteve French } while (!stop); 281038c8a9a5SSteve French } 281138c8a9a5SSteve French 281238c8a9a5SSteve French /* 281338c8a9a5SSteve French * Write back the locked page and any subsequent non-locked dirty pages. 281438c8a9a5SSteve French */ 281538c8a9a5SSteve French static ssize_t cifs_write_back_from_locked_folio(struct address_space *mapping, 281638c8a9a5SSteve French struct writeback_control *wbc, 2817e45deec3SDavid Howells struct xa_state *xas, 281838c8a9a5SSteve French struct folio *folio, 2819e45deec3SDavid Howells unsigned long long start, 2820e45deec3SDavid Howells unsigned long long end) 282138c8a9a5SSteve French { 282238c8a9a5SSteve French struct inode *inode = mapping->host; 282338c8a9a5SSteve French struct TCP_Server_Info *server; 282438c8a9a5SSteve French struct cifs_writedata *wdata; 282538c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 282638c8a9a5SSteve French struct cifs_credits credits_on_stack; 282738c8a9a5SSteve French struct cifs_credits *credits = &credits_on_stack; 282838c8a9a5SSteve French struct cifsFileInfo *cfile = NULL; 2829e45deec3SDavid Howells unsigned long long i_size = i_size_read(inode), max_len; 2830e45deec3SDavid Howells unsigned int xid, wsize; 2831e45deec3SDavid Howells size_t len = folio_size(folio); 283238c8a9a5SSteve French long count = wbc->nr_to_write; 283338c8a9a5SSteve French int rc; 283438c8a9a5SSteve French 283538c8a9a5SSteve French /* The folio should be locked, dirty and not undergoing writeback. */ 2836e45deec3SDavid Howells if (!folio_clear_dirty_for_io(folio)) 2837e45deec3SDavid Howells WARN_ON_ONCE(1); 28382e411c57SMatthew Wilcox (Oracle) folio_start_writeback(folio); 283938c8a9a5SSteve French 284038c8a9a5SSteve French count -= folio_nr_pages(folio); 284138c8a9a5SSteve French 284238c8a9a5SSteve French xid = get_xid(); 284338c8a9a5SSteve French server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses); 284438c8a9a5SSteve French 284538c8a9a5SSteve French rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile); 284638c8a9a5SSteve French if (rc) { 284738c8a9a5SSteve French cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", rc); 284838c8a9a5SSteve French goto err_xid; 284938c8a9a5SSteve French } 285038c8a9a5SSteve French 285138c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, 285238c8a9a5SSteve French &wsize, credits); 285338c8a9a5SSteve French if (rc != 0) 285438c8a9a5SSteve French goto err_close; 285538c8a9a5SSteve French 285638c8a9a5SSteve French wdata = cifs_writedata_alloc(cifs_writev_complete); 285738c8a9a5SSteve French if (!wdata) { 285838c8a9a5SSteve French rc = -ENOMEM; 285938c8a9a5SSteve French goto err_uncredit; 286038c8a9a5SSteve French } 286138c8a9a5SSteve French 286238c8a9a5SSteve French wdata->sync_mode = wbc->sync_mode; 286338c8a9a5SSteve French wdata->offset = folio_pos(folio); 286438c8a9a5SSteve French wdata->pid = cfile->pid; 286538c8a9a5SSteve French wdata->credits = credits_on_stack; 286638c8a9a5SSteve French wdata->cfile = cfile; 286738c8a9a5SSteve French wdata->server = server; 286838c8a9a5SSteve French cfile = NULL; 286938c8a9a5SSteve French 2870e45deec3SDavid Howells /* Find all consecutive lockable dirty pages that have contiguous 2871e45deec3SDavid Howells * written regions, stopping when we find a page that is not 2872e45deec3SDavid Howells * immediately lockable, is not dirty or is missing, or we reach the 2873e45deec3SDavid Howells * end of the range. 287438c8a9a5SSteve French */ 287538c8a9a5SSteve French if (start < i_size) { 287638c8a9a5SSteve French /* Trim the write to the EOF; the extra data is ignored. Also 287738c8a9a5SSteve French * put an upper limit on the size of a single storedata op. 287838c8a9a5SSteve French */ 287938c8a9a5SSteve French max_len = wsize; 288038c8a9a5SSteve French max_len = min_t(unsigned long long, max_len, end - start + 1); 288138c8a9a5SSteve French max_len = min_t(unsigned long long, max_len, i_size - start); 288238c8a9a5SSteve French 288338c8a9a5SSteve French if (len < max_len) { 288438c8a9a5SSteve French int max_pages = INT_MAX; 288538c8a9a5SSteve French 288638c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 288738c8a9a5SSteve French if (server->smbd_conn) 288838c8a9a5SSteve French max_pages = server->smbd_conn->max_frmr_depth; 288938c8a9a5SSteve French #endif 289038c8a9a5SSteve French max_pages -= folio_nr_pages(folio); 289138c8a9a5SSteve French 289238c8a9a5SSteve French if (max_pages > 0) 2893e45deec3SDavid Howells cifs_extend_writeback(mapping, xas, &count, start, 289438c8a9a5SSteve French max_pages, max_len, &len); 289538c8a9a5SSteve French } 289638c8a9a5SSteve French } 2897e45deec3SDavid Howells len = min_t(unsigned long long, len, i_size - start); 289838c8a9a5SSteve French 289938c8a9a5SSteve French /* We now have a contiguous set of dirty pages, each with writeback 290038c8a9a5SSteve French * set; the first page is still locked at this point, but all the rest 290138c8a9a5SSteve French * have been unlocked. 290238c8a9a5SSteve French */ 290338c8a9a5SSteve French folio_unlock(folio); 2904e45deec3SDavid Howells wdata->bytes = len; 290538c8a9a5SSteve French 290638c8a9a5SSteve French if (start < i_size) { 290738c8a9a5SSteve French iov_iter_xarray(&wdata->iter, ITER_SOURCE, &mapping->i_pages, 290838c8a9a5SSteve French start, len); 290938c8a9a5SSteve French 291038c8a9a5SSteve French rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes); 291138c8a9a5SSteve French if (rc) 291238c8a9a5SSteve French goto err_wdata; 291338c8a9a5SSteve French 291438c8a9a5SSteve French if (wdata->cfile->invalidHandle) 291538c8a9a5SSteve French rc = -EAGAIN; 291638c8a9a5SSteve French else 291738c8a9a5SSteve French rc = wdata->server->ops->async_writev(wdata, 291838c8a9a5SSteve French cifs_writedata_release); 291938c8a9a5SSteve French if (rc >= 0) { 292038c8a9a5SSteve French kref_put(&wdata->refcount, cifs_writedata_release); 292138c8a9a5SSteve French goto err_close; 292238c8a9a5SSteve French } 292338c8a9a5SSteve French } else { 292438c8a9a5SSteve French /* The dirty region was entirely beyond the EOF. */ 292538c8a9a5SSteve French cifs_pages_written_back(inode, start, len); 292638c8a9a5SSteve French rc = 0; 292738c8a9a5SSteve French } 292838c8a9a5SSteve French 292938c8a9a5SSteve French err_wdata: 293038c8a9a5SSteve French kref_put(&wdata->refcount, cifs_writedata_release); 293138c8a9a5SSteve French err_uncredit: 293238c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 293338c8a9a5SSteve French err_close: 293438c8a9a5SSteve French if (cfile) 293538c8a9a5SSteve French cifsFileInfo_put(cfile); 293638c8a9a5SSteve French err_xid: 293738c8a9a5SSteve French free_xid(xid); 293838c8a9a5SSteve French if (rc == 0) { 293938c8a9a5SSteve French wbc->nr_to_write = count; 294038c8a9a5SSteve French rc = len; 294138c8a9a5SSteve French } else if (is_retryable_error(rc)) { 294238c8a9a5SSteve French cifs_pages_write_redirty(inode, start, len); 294338c8a9a5SSteve French } else { 294438c8a9a5SSteve French cifs_pages_write_failed(inode, start, len); 294538c8a9a5SSteve French mapping_set_error(mapping, rc); 294638c8a9a5SSteve French } 294738c8a9a5SSteve French /* Indication to update ctime and mtime as close is deferred */ 294838c8a9a5SSteve French set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); 294938c8a9a5SSteve French return rc; 295038c8a9a5SSteve French } 295138c8a9a5SSteve French 295238c8a9a5SSteve French /* 295338c8a9a5SSteve French * write a region of pages back to the server 295438c8a9a5SSteve French */ 2955e45deec3SDavid Howells static ssize_t cifs_writepages_begin(struct address_space *mapping, 295638c8a9a5SSteve French struct writeback_control *wbc, 2957e45deec3SDavid Howells struct xa_state *xas, 2958e45deec3SDavid Howells unsigned long long *_start, 2959e45deec3SDavid Howells unsigned long long end) 296038c8a9a5SSteve French { 2961e45deec3SDavid Howells struct folio *folio; 2962e45deec3SDavid Howells unsigned long long start = *_start; 2963e45deec3SDavid Howells ssize_t ret; 296438c8a9a5SSteve French int skips = 0; 296538c8a9a5SSteve French 2966e45deec3SDavid Howells search_again: 2967e45deec3SDavid Howells /* Find the first dirty page. */ 2968e45deec3SDavid Howells rcu_read_lock(); 296938c8a9a5SSteve French 2970e45deec3SDavid Howells for (;;) { 2971e45deec3SDavid Howells folio = xas_find_marked(xas, end / PAGE_SIZE, PAGECACHE_TAG_DIRTY); 2972e45deec3SDavid Howells if (xas_retry(xas, folio) || xa_is_value(folio)) 2973e45deec3SDavid Howells continue; 2974e45deec3SDavid Howells if (!folio) 297538c8a9a5SSteve French break; 297638c8a9a5SSteve French 2977e45deec3SDavid Howells if (!folio_try_get_rcu(folio)) { 2978e45deec3SDavid Howells xas_reset(xas); 2979e45deec3SDavid Howells continue; 2980e45deec3SDavid Howells } 298138c8a9a5SSteve French 2982e45deec3SDavid Howells if (unlikely(folio != xas_reload(xas))) { 2983e45deec3SDavid Howells folio_put(folio); 2984e45deec3SDavid Howells xas_reset(xas); 2985e45deec3SDavid Howells continue; 2986e45deec3SDavid Howells } 2987e45deec3SDavid Howells 2988e45deec3SDavid Howells xas_pause(xas); 2989e45deec3SDavid Howells break; 2990e45deec3SDavid Howells } 2991e45deec3SDavid Howells rcu_read_unlock(); 2992e45deec3SDavid Howells if (!folio) 2993e45deec3SDavid Howells return 0; 2994e45deec3SDavid Howells 299538c8a9a5SSteve French start = folio_pos(folio); /* May regress with THPs */ 299638c8a9a5SSteve French 2997e45deec3SDavid Howells /* At this point we hold neither the i_pages lock nor the page lock: 2998e45deec3SDavid Howells * the page may be truncated or invalidated (changing page->mapping to 2999e45deec3SDavid Howells * NULL), or even swizzled back from swapper_space to tmpfs file 3000e45deec3SDavid Howells * mapping 300138c8a9a5SSteve French */ 3002e45deec3SDavid Howells lock_again: 300338c8a9a5SSteve French if (wbc->sync_mode != WB_SYNC_NONE) { 300438c8a9a5SSteve French ret = folio_lock_killable(folio); 300538c8a9a5SSteve French if (ret < 0) 3006e45deec3SDavid Howells return ret; 300738c8a9a5SSteve French } else { 300838c8a9a5SSteve French if (!folio_trylock(folio)) 3009e45deec3SDavid Howells goto search_again; 301038c8a9a5SSteve French } 301138c8a9a5SSteve French 3012d3c79235SDavid Howells if (folio->mapping != mapping || 301338c8a9a5SSteve French !folio_test_dirty(folio)) { 301438c8a9a5SSteve French start += folio_size(folio); 301538c8a9a5SSteve French folio_unlock(folio); 3016e45deec3SDavid Howells goto search_again; 301738c8a9a5SSteve French } 301838c8a9a5SSteve French 301938c8a9a5SSteve French if (folio_test_writeback(folio) || 302038c8a9a5SSteve French folio_test_fscache(folio)) { 302138c8a9a5SSteve French folio_unlock(folio); 3022e45deec3SDavid Howells if (wbc->sync_mode != WB_SYNC_NONE) { 302338c8a9a5SSteve French folio_wait_writeback(folio); 302438c8a9a5SSteve French #ifdef CONFIG_CIFS_FSCACHE 302538c8a9a5SSteve French folio_wait_fscache(folio); 302638c8a9a5SSteve French #endif 3027e45deec3SDavid Howells goto lock_again; 302838c8a9a5SSteve French } 302938c8a9a5SSteve French 3030e45deec3SDavid Howells start += folio_size(folio); 3031e45deec3SDavid Howells if (wbc->sync_mode == WB_SYNC_NONE) { 303238c8a9a5SSteve French if (skips >= 5 || need_resched()) { 303338c8a9a5SSteve French ret = 0; 3034e45deec3SDavid Howells goto out; 303538c8a9a5SSteve French } 303638c8a9a5SSteve French skips++; 3037e45deec3SDavid Howells } 3038e45deec3SDavid Howells goto search_again; 303938c8a9a5SSteve French } 304038c8a9a5SSteve French 3041e45deec3SDavid Howells ret = cifs_write_back_from_locked_folio(mapping, wbc, xas, folio, start, end); 3042e45deec3SDavid Howells out: 3043e45deec3SDavid Howells if (ret > 0) 3044e45deec3SDavid Howells *_start = start + ret; 3045e45deec3SDavid Howells return ret; 3046e45deec3SDavid Howells } 304738c8a9a5SSteve French 3048e45deec3SDavid Howells /* 3049e45deec3SDavid Howells * Write a region of pages back to the server 3050e45deec3SDavid Howells */ 3051e45deec3SDavid Howells static int cifs_writepages_region(struct address_space *mapping, 3052e45deec3SDavid Howells struct writeback_control *wbc, 3053e45deec3SDavid Howells unsigned long long *_start, 3054e45deec3SDavid Howells unsigned long long end) 3055e45deec3SDavid Howells { 3056e45deec3SDavid Howells ssize_t ret; 3057e45deec3SDavid Howells 3058e45deec3SDavid Howells XA_STATE(xas, &mapping->i_pages, *_start / PAGE_SIZE); 3059e45deec3SDavid Howells 3060e45deec3SDavid Howells do { 3061e45deec3SDavid Howells ret = cifs_writepages_begin(mapping, wbc, &xas, _start, end); 3062e45deec3SDavid Howells if (ret > 0 && wbc->nr_to_write > 0) 3063e45deec3SDavid Howells cond_resched(); 3064e45deec3SDavid Howells } while (ret > 0 && wbc->nr_to_write > 0); 3065e45deec3SDavid Howells 3066e45deec3SDavid Howells return ret > 0 ? 0 : ret; 306738c8a9a5SSteve French } 306838c8a9a5SSteve French 306938c8a9a5SSteve French /* 307038c8a9a5SSteve French * Write some of the pending data back to the server 307138c8a9a5SSteve French */ 307238c8a9a5SSteve French static int cifs_writepages(struct address_space *mapping, 307338c8a9a5SSteve French struct writeback_control *wbc) 307438c8a9a5SSteve French { 3075e45deec3SDavid Howells loff_t start, end; 307638c8a9a5SSteve French int ret; 307738c8a9a5SSteve French 307838c8a9a5SSteve French /* We have to be careful as we can end up racing with setattr() 307938c8a9a5SSteve French * truncating the pagecache since the caller doesn't take a lock here 308038c8a9a5SSteve French * to prevent it. 308138c8a9a5SSteve French */ 308238c8a9a5SSteve French 3083e45deec3SDavid Howells if (wbc->range_cyclic && mapping->writeback_index) { 308438c8a9a5SSteve French start = mapping->writeback_index * PAGE_SIZE; 3085e45deec3SDavid Howells ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX); 3086e45deec3SDavid Howells if (ret < 0) 3087e45deec3SDavid Howells goto out; 3088e45deec3SDavid Howells 3089e45deec3SDavid Howells if (wbc->nr_to_write <= 0) { 3090e45deec3SDavid Howells mapping->writeback_index = start / PAGE_SIZE; 3091e45deec3SDavid Howells goto out; 309238c8a9a5SSteve French } 309338c8a9a5SSteve French 3094e45deec3SDavid Howells start = 0; 3095e45deec3SDavid Howells end = mapping->writeback_index * PAGE_SIZE; 3096e45deec3SDavid Howells mapping->writeback_index = 0; 3097e45deec3SDavid Howells ret = cifs_writepages_region(mapping, wbc, &start, end); 3098e45deec3SDavid Howells if (ret == 0) 3099e45deec3SDavid Howells mapping->writeback_index = start / PAGE_SIZE; 3100e45deec3SDavid Howells } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { 3101e45deec3SDavid Howells start = 0; 3102e45deec3SDavid Howells ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX); 3103e45deec3SDavid Howells if (wbc->nr_to_write > 0 && ret == 0) 3104e45deec3SDavid Howells mapping->writeback_index = start / PAGE_SIZE; 3105e45deec3SDavid Howells } else { 3106e45deec3SDavid Howells start = wbc->range_start; 3107e45deec3SDavid Howells ret = cifs_writepages_region(mapping, wbc, &start, wbc->range_end); 3108e45deec3SDavid Howells } 3109e45deec3SDavid Howells 3110e45deec3SDavid Howells out: 311138c8a9a5SSteve French return ret; 311238c8a9a5SSteve French } 311338c8a9a5SSteve French 311438c8a9a5SSteve French static int 311538c8a9a5SSteve French cifs_writepage_locked(struct page *page, struct writeback_control *wbc) 311638c8a9a5SSteve French { 311738c8a9a5SSteve French int rc; 311838c8a9a5SSteve French unsigned int xid; 311938c8a9a5SSteve French 312038c8a9a5SSteve French xid = get_xid(); 312138c8a9a5SSteve French /* BB add check for wbc flags */ 312238c8a9a5SSteve French get_page(page); 312338c8a9a5SSteve French if (!PageUptodate(page)) 312438c8a9a5SSteve French cifs_dbg(FYI, "ppw - page not up to date\n"); 312538c8a9a5SSteve French 312638c8a9a5SSteve French /* 312738c8a9a5SSteve French * Set the "writeback" flag, and clear "dirty" in the radix tree. 312838c8a9a5SSteve French * 312938c8a9a5SSteve French * A writepage() implementation always needs to do either this, 313038c8a9a5SSteve French * or re-dirty the page with "redirty_page_for_writepage()" in 313138c8a9a5SSteve French * the case of a failure. 313238c8a9a5SSteve French * 313338c8a9a5SSteve French * Just unlocking the page will cause the radix tree tag-bits 313438c8a9a5SSteve French * to fail to update with the state of the page correctly. 313538c8a9a5SSteve French */ 313638c8a9a5SSteve French set_page_writeback(page); 313738c8a9a5SSteve French retry_write: 313838c8a9a5SSteve French rc = cifs_partialpagewrite(page, 0, PAGE_SIZE); 313938c8a9a5SSteve French if (is_retryable_error(rc)) { 314038c8a9a5SSteve French if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) 314138c8a9a5SSteve French goto retry_write; 314238c8a9a5SSteve French redirty_page_for_writepage(wbc, page); 314338c8a9a5SSteve French } else if (rc != 0) { 314438c8a9a5SSteve French SetPageError(page); 314538c8a9a5SSteve French mapping_set_error(page->mapping, rc); 314638c8a9a5SSteve French } else { 314738c8a9a5SSteve French SetPageUptodate(page); 314838c8a9a5SSteve French } 314938c8a9a5SSteve French end_page_writeback(page); 315038c8a9a5SSteve French put_page(page); 315138c8a9a5SSteve French free_xid(xid); 315238c8a9a5SSteve French return rc; 315338c8a9a5SSteve French } 315438c8a9a5SSteve French 315538c8a9a5SSteve French static int cifs_write_end(struct file *file, struct address_space *mapping, 315638c8a9a5SSteve French loff_t pos, unsigned len, unsigned copied, 315738c8a9a5SSteve French struct page *page, void *fsdata) 315838c8a9a5SSteve French { 315938c8a9a5SSteve French int rc; 316038c8a9a5SSteve French struct inode *inode = mapping->host; 316138c8a9a5SSteve French struct cifsFileInfo *cfile = file->private_data; 316238c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); 316338c8a9a5SSteve French struct folio *folio = page_folio(page); 316438c8a9a5SSteve French __u32 pid; 316538c8a9a5SSteve French 316638c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 316738c8a9a5SSteve French pid = cfile->pid; 316838c8a9a5SSteve French else 316938c8a9a5SSteve French pid = current->tgid; 317038c8a9a5SSteve French 317138c8a9a5SSteve French cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n", 317238c8a9a5SSteve French page, pos, copied); 317338c8a9a5SSteve French 317438c8a9a5SSteve French if (folio_test_checked(folio)) { 317538c8a9a5SSteve French if (copied == len) 317638c8a9a5SSteve French folio_mark_uptodate(folio); 317738c8a9a5SSteve French folio_clear_checked(folio); 317838c8a9a5SSteve French } else if (!folio_test_uptodate(folio) && copied == PAGE_SIZE) 317938c8a9a5SSteve French folio_mark_uptodate(folio); 318038c8a9a5SSteve French 318138c8a9a5SSteve French if (!folio_test_uptodate(folio)) { 318238c8a9a5SSteve French char *page_data; 318338c8a9a5SSteve French unsigned offset = pos & (PAGE_SIZE - 1); 318438c8a9a5SSteve French unsigned int xid; 318538c8a9a5SSteve French 318638c8a9a5SSteve French xid = get_xid(); 318738c8a9a5SSteve French /* this is probably better than directly calling 318838c8a9a5SSteve French partialpage_write since in this function the file handle is 318938c8a9a5SSteve French known which we might as well leverage */ 319038c8a9a5SSteve French /* BB check if anything else missing out of ppw 319138c8a9a5SSteve French such as updating last write time */ 319238c8a9a5SSteve French page_data = kmap(page); 319338c8a9a5SSteve French rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); 319438c8a9a5SSteve French /* if (rc < 0) should we set writebehind rc? */ 319538c8a9a5SSteve French kunmap(page); 319638c8a9a5SSteve French 319738c8a9a5SSteve French free_xid(xid); 319838c8a9a5SSteve French } else { 319938c8a9a5SSteve French rc = copied; 320038c8a9a5SSteve French pos += copied; 320138c8a9a5SSteve French set_page_dirty(page); 320238c8a9a5SSteve French } 320338c8a9a5SSteve French 320438c8a9a5SSteve French if (rc > 0) { 320538c8a9a5SSteve French spin_lock(&inode->i_lock); 320638c8a9a5SSteve French if (pos > inode->i_size) { 320738c8a9a5SSteve French i_size_write(inode, pos); 320838c8a9a5SSteve French inode->i_blocks = (512 - 1 + pos) >> 9; 320938c8a9a5SSteve French } 321038c8a9a5SSteve French spin_unlock(&inode->i_lock); 321138c8a9a5SSteve French } 321238c8a9a5SSteve French 321338c8a9a5SSteve French unlock_page(page); 321438c8a9a5SSteve French put_page(page); 321538c8a9a5SSteve French /* Indication to update ctime and mtime as close is deferred */ 321638c8a9a5SSteve French set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags); 321738c8a9a5SSteve French 321838c8a9a5SSteve French return rc; 321938c8a9a5SSteve French } 322038c8a9a5SSteve French 322138c8a9a5SSteve French int cifs_strict_fsync(struct file *file, loff_t start, loff_t end, 322238c8a9a5SSteve French int datasync) 322338c8a9a5SSteve French { 322438c8a9a5SSteve French unsigned int xid; 322538c8a9a5SSteve French int rc = 0; 322638c8a9a5SSteve French struct cifs_tcon *tcon; 322738c8a9a5SSteve French struct TCP_Server_Info *server; 322838c8a9a5SSteve French struct cifsFileInfo *smbfile = file->private_data; 322938c8a9a5SSteve French struct inode *inode = file_inode(file); 323038c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 323138c8a9a5SSteve French 323238c8a9a5SSteve French rc = file_write_and_wait_range(file, start, end); 323338c8a9a5SSteve French if (rc) { 323438c8a9a5SSteve French trace_cifs_fsync_err(inode->i_ino, rc); 323538c8a9a5SSteve French return rc; 323638c8a9a5SSteve French } 323738c8a9a5SSteve French 323838c8a9a5SSteve French xid = get_xid(); 323938c8a9a5SSteve French 324038c8a9a5SSteve French cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 324138c8a9a5SSteve French file, datasync); 324238c8a9a5SSteve French 324338c8a9a5SSteve French if (!CIFS_CACHE_READ(CIFS_I(inode))) { 324438c8a9a5SSteve French rc = cifs_zap_mapping(inode); 324538c8a9a5SSteve French if (rc) { 324638c8a9a5SSteve French cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc); 324738c8a9a5SSteve French rc = 0; /* don't care about it in fsync */ 324838c8a9a5SSteve French } 324938c8a9a5SSteve French } 325038c8a9a5SSteve French 325138c8a9a5SSteve French tcon = tlink_tcon(smbfile->tlink); 325238c8a9a5SSteve French if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { 325338c8a9a5SSteve French server = tcon->ses->server; 325438c8a9a5SSteve French if (server->ops->flush == NULL) { 325538c8a9a5SSteve French rc = -ENOSYS; 325638c8a9a5SSteve French goto strict_fsync_exit; 325738c8a9a5SSteve French } 325838c8a9a5SSteve French 325938c8a9a5SSteve French if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 326038c8a9a5SSteve French smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); 326138c8a9a5SSteve French if (smbfile) { 326238c8a9a5SSteve French rc = server->ops->flush(xid, tcon, &smbfile->fid); 326338c8a9a5SSteve French cifsFileInfo_put(smbfile); 326438c8a9a5SSteve French } else 326538c8a9a5SSteve French cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 326638c8a9a5SSteve French } else 326738c8a9a5SSteve French rc = server->ops->flush(xid, tcon, &smbfile->fid); 326838c8a9a5SSteve French } 326938c8a9a5SSteve French 327038c8a9a5SSteve French strict_fsync_exit: 327138c8a9a5SSteve French free_xid(xid); 327238c8a9a5SSteve French return rc; 327338c8a9a5SSteve French } 327438c8a9a5SSteve French 327538c8a9a5SSteve French int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 327638c8a9a5SSteve French { 327738c8a9a5SSteve French unsigned int xid; 327838c8a9a5SSteve French int rc = 0; 327938c8a9a5SSteve French struct cifs_tcon *tcon; 328038c8a9a5SSteve French struct TCP_Server_Info *server; 328138c8a9a5SSteve French struct cifsFileInfo *smbfile = file->private_data; 328238c8a9a5SSteve French struct inode *inode = file_inode(file); 328338c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file); 328438c8a9a5SSteve French 328538c8a9a5SSteve French rc = file_write_and_wait_range(file, start, end); 328638c8a9a5SSteve French if (rc) { 328738c8a9a5SSteve French trace_cifs_fsync_err(file_inode(file)->i_ino, rc); 328838c8a9a5SSteve French return rc; 328938c8a9a5SSteve French } 329038c8a9a5SSteve French 329138c8a9a5SSteve French xid = get_xid(); 329238c8a9a5SSteve French 329338c8a9a5SSteve French cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n", 329438c8a9a5SSteve French file, datasync); 329538c8a9a5SSteve French 329638c8a9a5SSteve French tcon = tlink_tcon(smbfile->tlink); 329738c8a9a5SSteve French if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) { 329838c8a9a5SSteve French server = tcon->ses->server; 329938c8a9a5SSteve French if (server->ops->flush == NULL) { 330038c8a9a5SSteve French rc = -ENOSYS; 330138c8a9a5SSteve French goto fsync_exit; 330238c8a9a5SSteve French } 330338c8a9a5SSteve French 330438c8a9a5SSteve French if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) { 330538c8a9a5SSteve French smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY); 330638c8a9a5SSteve French if (smbfile) { 330738c8a9a5SSteve French rc = server->ops->flush(xid, tcon, &smbfile->fid); 330838c8a9a5SSteve French cifsFileInfo_put(smbfile); 330938c8a9a5SSteve French } else 331038c8a9a5SSteve French cifs_dbg(FYI, "ignore fsync for file not open for write\n"); 331138c8a9a5SSteve French } else 331238c8a9a5SSteve French rc = server->ops->flush(xid, tcon, &smbfile->fid); 331338c8a9a5SSteve French } 331438c8a9a5SSteve French 331538c8a9a5SSteve French fsync_exit: 331638c8a9a5SSteve French free_xid(xid); 331738c8a9a5SSteve French return rc; 331838c8a9a5SSteve French } 331938c8a9a5SSteve French 332038c8a9a5SSteve French /* 332138c8a9a5SSteve French * As file closes, flush all cached write data for this inode checking 332238c8a9a5SSteve French * for write behind errors. 332338c8a9a5SSteve French */ 332438c8a9a5SSteve French int cifs_flush(struct file *file, fl_owner_t id) 332538c8a9a5SSteve French { 332638c8a9a5SSteve French struct inode *inode = file_inode(file); 332738c8a9a5SSteve French int rc = 0; 332838c8a9a5SSteve French 332938c8a9a5SSteve French if (file->f_mode & FMODE_WRITE) 333038c8a9a5SSteve French rc = filemap_write_and_wait(inode->i_mapping); 333138c8a9a5SSteve French 333238c8a9a5SSteve French cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc); 333338c8a9a5SSteve French if (rc) { 333438c8a9a5SSteve French /* get more nuanced writeback errors */ 333538c8a9a5SSteve French rc = filemap_check_wb_err(file->f_mapping, 0); 333638c8a9a5SSteve French trace_cifs_flush_err(inode->i_ino, rc); 333738c8a9a5SSteve French } 333838c8a9a5SSteve French return rc; 333938c8a9a5SSteve French } 334038c8a9a5SSteve French 334138c8a9a5SSteve French static void 334238c8a9a5SSteve French cifs_uncached_writedata_release(struct kref *refcount) 334338c8a9a5SSteve French { 334438c8a9a5SSteve French struct cifs_writedata *wdata = container_of(refcount, 334538c8a9a5SSteve French struct cifs_writedata, refcount); 334638c8a9a5SSteve French 334738c8a9a5SSteve French kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release); 334838c8a9a5SSteve French cifs_writedata_release(refcount); 334938c8a9a5SSteve French } 335038c8a9a5SSteve French 335138c8a9a5SSteve French static void collect_uncached_write_data(struct cifs_aio_ctx *ctx); 335238c8a9a5SSteve French 335338c8a9a5SSteve French static void 335438c8a9a5SSteve French cifs_uncached_writev_complete(struct work_struct *work) 335538c8a9a5SSteve French { 335638c8a9a5SSteve French struct cifs_writedata *wdata = container_of(work, 335738c8a9a5SSteve French struct cifs_writedata, work); 335838c8a9a5SSteve French struct inode *inode = d_inode(wdata->cfile->dentry); 335938c8a9a5SSteve French struct cifsInodeInfo *cifsi = CIFS_I(inode); 336038c8a9a5SSteve French 336138c8a9a5SSteve French spin_lock(&inode->i_lock); 336238c8a9a5SSteve French cifs_update_eof(cifsi, wdata->offset, wdata->bytes); 336338c8a9a5SSteve French if (cifsi->server_eof > inode->i_size) 336438c8a9a5SSteve French i_size_write(inode, cifsi->server_eof); 336538c8a9a5SSteve French spin_unlock(&inode->i_lock); 336638c8a9a5SSteve French 336738c8a9a5SSteve French complete(&wdata->done); 336838c8a9a5SSteve French collect_uncached_write_data(wdata->ctx); 336938c8a9a5SSteve French /* the below call can possibly free the last ref to aio ctx */ 337038c8a9a5SSteve French kref_put(&wdata->refcount, cifs_uncached_writedata_release); 337138c8a9a5SSteve French } 337238c8a9a5SSteve French 337338c8a9a5SSteve French static int 337438c8a9a5SSteve French cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list, 337538c8a9a5SSteve French struct cifs_aio_ctx *ctx) 337638c8a9a5SSteve French { 337738c8a9a5SSteve French unsigned int wsize; 337838c8a9a5SSteve French struct cifs_credits credits; 337938c8a9a5SSteve French int rc; 338038c8a9a5SSteve French struct TCP_Server_Info *server = wdata->server; 338138c8a9a5SSteve French 338238c8a9a5SSteve French do { 338338c8a9a5SSteve French if (wdata->cfile->invalidHandle) { 338438c8a9a5SSteve French rc = cifs_reopen_file(wdata->cfile, false); 338538c8a9a5SSteve French if (rc == -EAGAIN) 338638c8a9a5SSteve French continue; 338738c8a9a5SSteve French else if (rc) 338838c8a9a5SSteve French break; 338938c8a9a5SSteve French } 339038c8a9a5SSteve French 339138c8a9a5SSteve French 339238c8a9a5SSteve French /* 339338c8a9a5SSteve French * Wait for credits to resend this wdata. 339438c8a9a5SSteve French * Note: we are attempting to resend the whole wdata not in 339538c8a9a5SSteve French * segments 339638c8a9a5SSteve French */ 339738c8a9a5SSteve French do { 339838c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, wdata->bytes, 339938c8a9a5SSteve French &wsize, &credits); 340038c8a9a5SSteve French if (rc) 340138c8a9a5SSteve French goto fail; 340238c8a9a5SSteve French 340338c8a9a5SSteve French if (wsize < wdata->bytes) { 340438c8a9a5SSteve French add_credits_and_wake_if(server, &credits, 0); 340538c8a9a5SSteve French msleep(1000); 340638c8a9a5SSteve French } 340738c8a9a5SSteve French } while (wsize < wdata->bytes); 340838c8a9a5SSteve French wdata->credits = credits; 340938c8a9a5SSteve French 341038c8a9a5SSteve French rc = adjust_credits(server, &wdata->credits, wdata->bytes); 341138c8a9a5SSteve French 341238c8a9a5SSteve French if (!rc) { 341338c8a9a5SSteve French if (wdata->cfile->invalidHandle) 341438c8a9a5SSteve French rc = -EAGAIN; 341538c8a9a5SSteve French else { 3416*cdd7870aSShyam Prasad N wdata->replay = true; 341738c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 341838c8a9a5SSteve French if (wdata->mr) { 341938c8a9a5SSteve French wdata->mr->need_invalidate = true; 342038c8a9a5SSteve French smbd_deregister_mr(wdata->mr); 342138c8a9a5SSteve French wdata->mr = NULL; 342238c8a9a5SSteve French } 342338c8a9a5SSteve French #endif 342438c8a9a5SSteve French rc = server->ops->async_writev(wdata, 342538c8a9a5SSteve French cifs_uncached_writedata_release); 342638c8a9a5SSteve French } 342738c8a9a5SSteve French } 342838c8a9a5SSteve French 342938c8a9a5SSteve French /* If the write was successfully sent, we are done */ 343038c8a9a5SSteve French if (!rc) { 343138c8a9a5SSteve French list_add_tail(&wdata->list, wdata_list); 343238c8a9a5SSteve French return 0; 343338c8a9a5SSteve French } 343438c8a9a5SSteve French 343538c8a9a5SSteve French /* Roll back credits and retry if needed */ 343638c8a9a5SSteve French add_credits_and_wake_if(server, &wdata->credits, 0); 343738c8a9a5SSteve French } while (rc == -EAGAIN); 343838c8a9a5SSteve French 343938c8a9a5SSteve French fail: 344038c8a9a5SSteve French kref_put(&wdata->refcount, cifs_uncached_writedata_release); 344138c8a9a5SSteve French return rc; 344238c8a9a5SSteve French } 344338c8a9a5SSteve French 344438c8a9a5SSteve French /* 344538c8a9a5SSteve French * Select span of a bvec iterator we're going to use. Limit it by both maximum 344638c8a9a5SSteve French * size and maximum number of segments. 344738c8a9a5SSteve French */ 344838c8a9a5SSteve French static size_t cifs_limit_bvec_subset(const struct iov_iter *iter, size_t max_size, 344938c8a9a5SSteve French size_t max_segs, unsigned int *_nsegs) 345038c8a9a5SSteve French { 345138c8a9a5SSteve French const struct bio_vec *bvecs = iter->bvec; 345238c8a9a5SSteve French unsigned int nbv = iter->nr_segs, ix = 0, nsegs = 0; 345338c8a9a5SSteve French size_t len, span = 0, n = iter->count; 345438c8a9a5SSteve French size_t skip = iter->iov_offset; 345538c8a9a5SSteve French 345638c8a9a5SSteve French if (WARN_ON(!iov_iter_is_bvec(iter)) || n == 0) 345738c8a9a5SSteve French return 0; 345838c8a9a5SSteve French 345938c8a9a5SSteve French while (n && ix < nbv && skip) { 346038c8a9a5SSteve French len = bvecs[ix].bv_len; 346138c8a9a5SSteve French if (skip < len) 346238c8a9a5SSteve French break; 346338c8a9a5SSteve French skip -= len; 346438c8a9a5SSteve French n -= len; 346538c8a9a5SSteve French ix++; 346638c8a9a5SSteve French } 346738c8a9a5SSteve French 346838c8a9a5SSteve French while (n && ix < nbv) { 346938c8a9a5SSteve French len = min3(n, bvecs[ix].bv_len - skip, max_size); 347038c8a9a5SSteve French span += len; 347138c8a9a5SSteve French max_size -= len; 347238c8a9a5SSteve French nsegs++; 347338c8a9a5SSteve French ix++; 347438c8a9a5SSteve French if (max_size == 0 || nsegs >= max_segs) 347538c8a9a5SSteve French break; 347638c8a9a5SSteve French skip = 0; 347738c8a9a5SSteve French n -= len; 347838c8a9a5SSteve French } 347938c8a9a5SSteve French 348038c8a9a5SSteve French *_nsegs = nsegs; 348138c8a9a5SSteve French return span; 348238c8a9a5SSteve French } 348338c8a9a5SSteve French 348438c8a9a5SSteve French static int 348538c8a9a5SSteve French cifs_write_from_iter(loff_t fpos, size_t len, struct iov_iter *from, 348638c8a9a5SSteve French struct cifsFileInfo *open_file, 348738c8a9a5SSteve French struct cifs_sb_info *cifs_sb, struct list_head *wdata_list, 348838c8a9a5SSteve French struct cifs_aio_ctx *ctx) 348938c8a9a5SSteve French { 349038c8a9a5SSteve French int rc = 0; 349138c8a9a5SSteve French size_t cur_len, max_len; 349238c8a9a5SSteve French struct cifs_writedata *wdata; 349338c8a9a5SSteve French pid_t pid; 349438c8a9a5SSteve French struct TCP_Server_Info *server; 349538c8a9a5SSteve French unsigned int xid, max_segs = INT_MAX; 349638c8a9a5SSteve French 349738c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 349838c8a9a5SSteve French pid = open_file->pid; 349938c8a9a5SSteve French else 350038c8a9a5SSteve French pid = current->tgid; 350138c8a9a5SSteve French 350238c8a9a5SSteve French server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); 350338c8a9a5SSteve French xid = get_xid(); 350438c8a9a5SSteve French 350538c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 350638c8a9a5SSteve French if (server->smbd_conn) 350738c8a9a5SSteve French max_segs = server->smbd_conn->max_frmr_depth; 350838c8a9a5SSteve French #endif 350938c8a9a5SSteve French 351038c8a9a5SSteve French do { 351138c8a9a5SSteve French struct cifs_credits credits_on_stack; 351238c8a9a5SSteve French struct cifs_credits *credits = &credits_on_stack; 351338c8a9a5SSteve French unsigned int wsize, nsegs = 0; 351438c8a9a5SSteve French 351538c8a9a5SSteve French if (signal_pending(current)) { 351638c8a9a5SSteve French rc = -EINTR; 351738c8a9a5SSteve French break; 351838c8a9a5SSteve French } 351938c8a9a5SSteve French 352038c8a9a5SSteve French if (open_file->invalidHandle) { 352138c8a9a5SSteve French rc = cifs_reopen_file(open_file, false); 352238c8a9a5SSteve French if (rc == -EAGAIN) 352338c8a9a5SSteve French continue; 352438c8a9a5SSteve French else if (rc) 352538c8a9a5SSteve French break; 352638c8a9a5SSteve French } 352738c8a9a5SSteve French 352838c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize, 352938c8a9a5SSteve French &wsize, credits); 353038c8a9a5SSteve French if (rc) 353138c8a9a5SSteve French break; 353238c8a9a5SSteve French 353338c8a9a5SSteve French max_len = min_t(const size_t, len, wsize); 353438c8a9a5SSteve French if (!max_len) { 353538c8a9a5SSteve French rc = -EAGAIN; 353638c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 353738c8a9a5SSteve French break; 353838c8a9a5SSteve French } 353938c8a9a5SSteve French 354038c8a9a5SSteve French cur_len = cifs_limit_bvec_subset(from, max_len, max_segs, &nsegs); 354138c8a9a5SSteve French cifs_dbg(FYI, "write_from_iter len=%zx/%zx nsegs=%u/%lu/%u\n", 354238c8a9a5SSteve French cur_len, max_len, nsegs, from->nr_segs, max_segs); 354338c8a9a5SSteve French if (cur_len == 0) { 354438c8a9a5SSteve French rc = -EIO; 354538c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 354638c8a9a5SSteve French break; 354738c8a9a5SSteve French } 354838c8a9a5SSteve French 354938c8a9a5SSteve French wdata = cifs_writedata_alloc(cifs_uncached_writev_complete); 355038c8a9a5SSteve French if (!wdata) { 355138c8a9a5SSteve French rc = -ENOMEM; 355238c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 355338c8a9a5SSteve French break; 355438c8a9a5SSteve French } 355538c8a9a5SSteve French 355638c8a9a5SSteve French wdata->sync_mode = WB_SYNC_ALL; 355738c8a9a5SSteve French wdata->offset = (__u64)fpos; 355838c8a9a5SSteve French wdata->cfile = cifsFileInfo_get(open_file); 355938c8a9a5SSteve French wdata->server = server; 356038c8a9a5SSteve French wdata->pid = pid; 356138c8a9a5SSteve French wdata->bytes = cur_len; 356238c8a9a5SSteve French wdata->credits = credits_on_stack; 356338c8a9a5SSteve French wdata->iter = *from; 356438c8a9a5SSteve French wdata->ctx = ctx; 356538c8a9a5SSteve French kref_get(&ctx->refcount); 356638c8a9a5SSteve French 356738c8a9a5SSteve French iov_iter_truncate(&wdata->iter, cur_len); 356838c8a9a5SSteve French 356938c8a9a5SSteve French rc = adjust_credits(server, &wdata->credits, wdata->bytes); 357038c8a9a5SSteve French 357138c8a9a5SSteve French if (!rc) { 357238c8a9a5SSteve French if (wdata->cfile->invalidHandle) 357338c8a9a5SSteve French rc = -EAGAIN; 357438c8a9a5SSteve French else 357538c8a9a5SSteve French rc = server->ops->async_writev(wdata, 357638c8a9a5SSteve French cifs_uncached_writedata_release); 357738c8a9a5SSteve French } 357838c8a9a5SSteve French 357938c8a9a5SSteve French if (rc) { 358038c8a9a5SSteve French add_credits_and_wake_if(server, &wdata->credits, 0); 358138c8a9a5SSteve French kref_put(&wdata->refcount, 358238c8a9a5SSteve French cifs_uncached_writedata_release); 358338c8a9a5SSteve French if (rc == -EAGAIN) 358438c8a9a5SSteve French continue; 358538c8a9a5SSteve French break; 358638c8a9a5SSteve French } 358738c8a9a5SSteve French 358838c8a9a5SSteve French list_add_tail(&wdata->list, wdata_list); 358938c8a9a5SSteve French iov_iter_advance(from, cur_len); 359038c8a9a5SSteve French fpos += cur_len; 359138c8a9a5SSteve French len -= cur_len; 359238c8a9a5SSteve French } while (len > 0); 359338c8a9a5SSteve French 359438c8a9a5SSteve French free_xid(xid); 359538c8a9a5SSteve French return rc; 359638c8a9a5SSteve French } 359738c8a9a5SSteve French 359838c8a9a5SSteve French static void collect_uncached_write_data(struct cifs_aio_ctx *ctx) 359938c8a9a5SSteve French { 360038c8a9a5SSteve French struct cifs_writedata *wdata, *tmp; 360138c8a9a5SSteve French struct cifs_tcon *tcon; 360238c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 360338c8a9a5SSteve French struct dentry *dentry = ctx->cfile->dentry; 360438c8a9a5SSteve French ssize_t rc; 360538c8a9a5SSteve French 360638c8a9a5SSteve French tcon = tlink_tcon(ctx->cfile->tlink); 360738c8a9a5SSteve French cifs_sb = CIFS_SB(dentry->d_sb); 360838c8a9a5SSteve French 360938c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 361038c8a9a5SSteve French 361138c8a9a5SSteve French if (list_empty(&ctx->list)) { 361238c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 361338c8a9a5SSteve French return; 361438c8a9a5SSteve French } 361538c8a9a5SSteve French 361638c8a9a5SSteve French rc = ctx->rc; 361738c8a9a5SSteve French /* 361838c8a9a5SSteve French * Wait for and collect replies for any successful sends in order of 361938c8a9a5SSteve French * increasing offset. Once an error is hit, then return without waiting 362038c8a9a5SSteve French * for any more replies. 362138c8a9a5SSteve French */ 362238c8a9a5SSteve French restart_loop: 362338c8a9a5SSteve French list_for_each_entry_safe(wdata, tmp, &ctx->list, list) { 362438c8a9a5SSteve French if (!rc) { 362538c8a9a5SSteve French if (!try_wait_for_completion(&wdata->done)) { 362638c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 362738c8a9a5SSteve French return; 362838c8a9a5SSteve French } 362938c8a9a5SSteve French 363038c8a9a5SSteve French if (wdata->result) 363138c8a9a5SSteve French rc = wdata->result; 363238c8a9a5SSteve French else 363338c8a9a5SSteve French ctx->total_len += wdata->bytes; 363438c8a9a5SSteve French 363538c8a9a5SSteve French /* resend call if it's a retryable error */ 363638c8a9a5SSteve French if (rc == -EAGAIN) { 363738c8a9a5SSteve French struct list_head tmp_list; 363838c8a9a5SSteve French struct iov_iter tmp_from = ctx->iter; 363938c8a9a5SSteve French 364038c8a9a5SSteve French INIT_LIST_HEAD(&tmp_list); 364138c8a9a5SSteve French list_del_init(&wdata->list); 364238c8a9a5SSteve French 364338c8a9a5SSteve French if (ctx->direct_io) 364438c8a9a5SSteve French rc = cifs_resend_wdata( 364538c8a9a5SSteve French wdata, &tmp_list, ctx); 364638c8a9a5SSteve French else { 364738c8a9a5SSteve French iov_iter_advance(&tmp_from, 364838c8a9a5SSteve French wdata->offset - ctx->pos); 364938c8a9a5SSteve French 365038c8a9a5SSteve French rc = cifs_write_from_iter(wdata->offset, 365138c8a9a5SSteve French wdata->bytes, &tmp_from, 365238c8a9a5SSteve French ctx->cfile, cifs_sb, &tmp_list, 365338c8a9a5SSteve French ctx); 365438c8a9a5SSteve French 365538c8a9a5SSteve French kref_put(&wdata->refcount, 365638c8a9a5SSteve French cifs_uncached_writedata_release); 365738c8a9a5SSteve French } 365838c8a9a5SSteve French 365938c8a9a5SSteve French list_splice(&tmp_list, &ctx->list); 366038c8a9a5SSteve French goto restart_loop; 366138c8a9a5SSteve French } 366238c8a9a5SSteve French } 366338c8a9a5SSteve French list_del_init(&wdata->list); 366438c8a9a5SSteve French kref_put(&wdata->refcount, cifs_uncached_writedata_release); 366538c8a9a5SSteve French } 366638c8a9a5SSteve French 366738c8a9a5SSteve French cifs_stats_bytes_written(tcon, ctx->total_len); 366838c8a9a5SSteve French set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags); 366938c8a9a5SSteve French 367038c8a9a5SSteve French ctx->rc = (rc == 0) ? ctx->total_len : rc; 367138c8a9a5SSteve French 367238c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 367338c8a9a5SSteve French 367438c8a9a5SSteve French if (ctx->iocb && ctx->iocb->ki_complete) 367538c8a9a5SSteve French ctx->iocb->ki_complete(ctx->iocb, ctx->rc); 367638c8a9a5SSteve French else 367738c8a9a5SSteve French complete(&ctx->done); 367838c8a9a5SSteve French } 367938c8a9a5SSteve French 368038c8a9a5SSteve French static ssize_t __cifs_writev( 368138c8a9a5SSteve French struct kiocb *iocb, struct iov_iter *from, bool direct) 368238c8a9a5SSteve French { 368338c8a9a5SSteve French struct file *file = iocb->ki_filp; 368438c8a9a5SSteve French ssize_t total_written = 0; 368538c8a9a5SSteve French struct cifsFileInfo *cfile; 368638c8a9a5SSteve French struct cifs_tcon *tcon; 368738c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 368838c8a9a5SSteve French struct cifs_aio_ctx *ctx; 368938c8a9a5SSteve French int rc; 369038c8a9a5SSteve French 369138c8a9a5SSteve French rc = generic_write_checks(iocb, from); 369238c8a9a5SSteve French if (rc <= 0) 369338c8a9a5SSteve French return rc; 369438c8a9a5SSteve French 369538c8a9a5SSteve French cifs_sb = CIFS_FILE_SB(file); 369638c8a9a5SSteve French cfile = file->private_data; 369738c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 369838c8a9a5SSteve French 369938c8a9a5SSteve French if (!tcon->ses->server->ops->async_writev) 370038c8a9a5SSteve French return -ENOSYS; 370138c8a9a5SSteve French 370238c8a9a5SSteve French ctx = cifs_aio_ctx_alloc(); 370338c8a9a5SSteve French if (!ctx) 370438c8a9a5SSteve French return -ENOMEM; 370538c8a9a5SSteve French 370638c8a9a5SSteve French ctx->cfile = cifsFileInfo_get(cfile); 370738c8a9a5SSteve French 370838c8a9a5SSteve French if (!is_sync_kiocb(iocb)) 370938c8a9a5SSteve French ctx->iocb = iocb; 371038c8a9a5SSteve French 371138c8a9a5SSteve French ctx->pos = iocb->ki_pos; 371238c8a9a5SSteve French ctx->direct_io = direct; 371338c8a9a5SSteve French ctx->nr_pinned_pages = 0; 371438c8a9a5SSteve French 371538c8a9a5SSteve French if (user_backed_iter(from)) { 371638c8a9a5SSteve French /* 371738c8a9a5SSteve French * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as 371838c8a9a5SSteve French * they contain references to the calling process's virtual 371938c8a9a5SSteve French * memory layout which won't be available in an async worker 372038c8a9a5SSteve French * thread. This also takes a pin on every folio involved. 372138c8a9a5SSteve French */ 372238c8a9a5SSteve French rc = netfs_extract_user_iter(from, iov_iter_count(from), 372338c8a9a5SSteve French &ctx->iter, 0); 372438c8a9a5SSteve French if (rc < 0) { 372538c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 372638c8a9a5SSteve French return rc; 372738c8a9a5SSteve French } 372838c8a9a5SSteve French 372938c8a9a5SSteve French ctx->nr_pinned_pages = rc; 373038c8a9a5SSteve French ctx->bv = (void *)ctx->iter.bvec; 373138c8a9a5SSteve French ctx->bv_need_unpin = iov_iter_extract_will_pin(from); 373238c8a9a5SSteve French } else if ((iov_iter_is_bvec(from) || iov_iter_is_kvec(from)) && 373338c8a9a5SSteve French !is_sync_kiocb(iocb)) { 373438c8a9a5SSteve French /* 373538c8a9a5SSteve French * If the op is asynchronous, we need to copy the list attached 373638c8a9a5SSteve French * to a BVEC/KVEC-type iterator, but we assume that the storage 373738c8a9a5SSteve French * will be pinned by the caller; in any case, we may or may not 373838c8a9a5SSteve French * be able to pin the pages, so we don't try. 373938c8a9a5SSteve French */ 374038c8a9a5SSteve French ctx->bv = (void *)dup_iter(&ctx->iter, from, GFP_KERNEL); 374138c8a9a5SSteve French if (!ctx->bv) { 374238c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 374338c8a9a5SSteve French return -ENOMEM; 374438c8a9a5SSteve French } 374538c8a9a5SSteve French } else { 374638c8a9a5SSteve French /* 374738c8a9a5SSteve French * Otherwise, we just pass the iterator down as-is and rely on 374838c8a9a5SSteve French * the caller to make sure the pages referred to by the 374938c8a9a5SSteve French * iterator don't evaporate. 375038c8a9a5SSteve French */ 375138c8a9a5SSteve French ctx->iter = *from; 375238c8a9a5SSteve French } 375338c8a9a5SSteve French 375438c8a9a5SSteve French ctx->len = iov_iter_count(&ctx->iter); 375538c8a9a5SSteve French 375638c8a9a5SSteve French /* grab a lock here due to read response handlers can access ctx */ 375738c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 375838c8a9a5SSteve French 375938c8a9a5SSteve French rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &ctx->iter, 376038c8a9a5SSteve French cfile, cifs_sb, &ctx->list, ctx); 376138c8a9a5SSteve French 376238c8a9a5SSteve French /* 376338c8a9a5SSteve French * If at least one write was successfully sent, then discard any rc 376438c8a9a5SSteve French * value from the later writes. If the other write succeeds, then 376538c8a9a5SSteve French * we'll end up returning whatever was written. If it fails, then 376638c8a9a5SSteve French * we'll get a new rc value from that. 376738c8a9a5SSteve French */ 376838c8a9a5SSteve French if (!list_empty(&ctx->list)) 376938c8a9a5SSteve French rc = 0; 377038c8a9a5SSteve French 377138c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 377238c8a9a5SSteve French 377338c8a9a5SSteve French if (rc) { 377438c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 377538c8a9a5SSteve French return rc; 377638c8a9a5SSteve French } 377738c8a9a5SSteve French 377838c8a9a5SSteve French if (!is_sync_kiocb(iocb)) { 377938c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 378038c8a9a5SSteve French return -EIOCBQUEUED; 378138c8a9a5SSteve French } 378238c8a9a5SSteve French 378338c8a9a5SSteve French rc = wait_for_completion_killable(&ctx->done); 378438c8a9a5SSteve French if (rc) { 378538c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 378638c8a9a5SSteve French ctx->rc = rc = -EINTR; 378738c8a9a5SSteve French total_written = ctx->total_len; 378838c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 378938c8a9a5SSteve French } else { 379038c8a9a5SSteve French rc = ctx->rc; 379138c8a9a5SSteve French total_written = ctx->total_len; 379238c8a9a5SSteve French } 379338c8a9a5SSteve French 379438c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 379538c8a9a5SSteve French 379638c8a9a5SSteve French if (unlikely(!total_written)) 379738c8a9a5SSteve French return rc; 379838c8a9a5SSteve French 379938c8a9a5SSteve French iocb->ki_pos += total_written; 380038c8a9a5SSteve French return total_written; 380138c8a9a5SSteve French } 380238c8a9a5SSteve French 380338c8a9a5SSteve French ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from) 380438c8a9a5SSteve French { 380538c8a9a5SSteve French struct file *file = iocb->ki_filp; 380638c8a9a5SSteve French 380738c8a9a5SSteve French cifs_revalidate_mapping(file->f_inode); 380838c8a9a5SSteve French return __cifs_writev(iocb, from, true); 380938c8a9a5SSteve French } 381038c8a9a5SSteve French 381138c8a9a5SSteve French ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) 381238c8a9a5SSteve French { 381338c8a9a5SSteve French return __cifs_writev(iocb, from, false); 381438c8a9a5SSteve French } 381538c8a9a5SSteve French 381638c8a9a5SSteve French static ssize_t 381738c8a9a5SSteve French cifs_writev(struct kiocb *iocb, struct iov_iter *from) 381838c8a9a5SSteve French { 381938c8a9a5SSteve French struct file *file = iocb->ki_filp; 382038c8a9a5SSteve French struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data; 382138c8a9a5SSteve French struct inode *inode = file->f_mapping->host; 382238c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 382338c8a9a5SSteve French struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server; 382438c8a9a5SSteve French ssize_t rc; 382538c8a9a5SSteve French 382638c8a9a5SSteve French inode_lock(inode); 382738c8a9a5SSteve French /* 382838c8a9a5SSteve French * We need to hold the sem to be sure nobody modifies lock list 382938c8a9a5SSteve French * with a brlock that prevents writing. 383038c8a9a5SSteve French */ 383138c8a9a5SSteve French down_read(&cinode->lock_sem); 383238c8a9a5SSteve French 383338c8a9a5SSteve French rc = generic_write_checks(iocb, from); 383438c8a9a5SSteve French if (rc <= 0) 383538c8a9a5SSteve French goto out; 383638c8a9a5SSteve French 383738c8a9a5SSteve French if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from), 383838c8a9a5SSteve French server->vals->exclusive_lock_type, 0, 383938c8a9a5SSteve French NULL, CIFS_WRITE_OP)) 384038c8a9a5SSteve French rc = __generic_file_write_iter(iocb, from); 384138c8a9a5SSteve French else 384238c8a9a5SSteve French rc = -EACCES; 384338c8a9a5SSteve French out: 384438c8a9a5SSteve French up_read(&cinode->lock_sem); 384538c8a9a5SSteve French inode_unlock(inode); 384638c8a9a5SSteve French 384738c8a9a5SSteve French if (rc > 0) 384838c8a9a5SSteve French rc = generic_write_sync(iocb, rc); 384938c8a9a5SSteve French return rc; 385038c8a9a5SSteve French } 385138c8a9a5SSteve French 385238c8a9a5SSteve French ssize_t 385338c8a9a5SSteve French cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from) 385438c8a9a5SSteve French { 385538c8a9a5SSteve French struct inode *inode = file_inode(iocb->ki_filp); 385638c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 385738c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 385838c8a9a5SSteve French struct cifsFileInfo *cfile = (struct cifsFileInfo *) 385938c8a9a5SSteve French iocb->ki_filp->private_data; 386038c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 386138c8a9a5SSteve French ssize_t written; 386238c8a9a5SSteve French 386338c8a9a5SSteve French written = cifs_get_writer(cinode); 386438c8a9a5SSteve French if (written) 386538c8a9a5SSteve French return written; 386638c8a9a5SSteve French 386738c8a9a5SSteve French if (CIFS_CACHE_WRITE(cinode)) { 386838c8a9a5SSteve French if (cap_unix(tcon->ses) && 386938c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) 387038c8a9a5SSteve French && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) { 387138c8a9a5SSteve French written = generic_file_write_iter(iocb, from); 387238c8a9a5SSteve French goto out; 387338c8a9a5SSteve French } 387438c8a9a5SSteve French written = cifs_writev(iocb, from); 387538c8a9a5SSteve French goto out; 387638c8a9a5SSteve French } 387738c8a9a5SSteve French /* 387838c8a9a5SSteve French * For non-oplocked files in strict cache mode we need to write the data 387938c8a9a5SSteve French * to the server exactly from the pos to pos+len-1 rather than flush all 388038c8a9a5SSteve French * affected pages because it may cause a error with mandatory locks on 388138c8a9a5SSteve French * these pages but not on the region from pos to ppos+len-1. 388238c8a9a5SSteve French */ 388338c8a9a5SSteve French written = cifs_user_writev(iocb, from); 388438c8a9a5SSteve French if (CIFS_CACHE_READ(cinode)) { 388538c8a9a5SSteve French /* 388638c8a9a5SSteve French * We have read level caching and we have just sent a write 388738c8a9a5SSteve French * request to the server thus making data in the cache stale. 388838c8a9a5SSteve French * Zap the cache and set oplock/lease level to NONE to avoid 388938c8a9a5SSteve French * reading stale data from the cache. All subsequent read 389038c8a9a5SSteve French * operations will read new data from the server. 389138c8a9a5SSteve French */ 389238c8a9a5SSteve French cifs_zap_mapping(inode); 389338c8a9a5SSteve French cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n", 389438c8a9a5SSteve French inode); 389538c8a9a5SSteve French cinode->oplock = 0; 389638c8a9a5SSteve French } 389738c8a9a5SSteve French out: 389838c8a9a5SSteve French cifs_put_writer(cinode); 389938c8a9a5SSteve French return written; 390038c8a9a5SSteve French } 390138c8a9a5SSteve French 390238c8a9a5SSteve French static struct cifs_readdata *cifs_readdata_alloc(work_func_t complete) 390338c8a9a5SSteve French { 390438c8a9a5SSteve French struct cifs_readdata *rdata; 390538c8a9a5SSteve French 390638c8a9a5SSteve French rdata = kzalloc(sizeof(*rdata), GFP_KERNEL); 390738c8a9a5SSteve French if (rdata) { 390838c8a9a5SSteve French kref_init(&rdata->refcount); 390938c8a9a5SSteve French INIT_LIST_HEAD(&rdata->list); 391038c8a9a5SSteve French init_completion(&rdata->done); 391138c8a9a5SSteve French INIT_WORK(&rdata->work, complete); 391238c8a9a5SSteve French } 391338c8a9a5SSteve French 391438c8a9a5SSteve French return rdata; 391538c8a9a5SSteve French } 391638c8a9a5SSteve French 391738c8a9a5SSteve French void 391838c8a9a5SSteve French cifs_readdata_release(struct kref *refcount) 391938c8a9a5SSteve French { 392038c8a9a5SSteve French struct cifs_readdata *rdata = container_of(refcount, 392138c8a9a5SSteve French struct cifs_readdata, refcount); 392238c8a9a5SSteve French 392338c8a9a5SSteve French if (rdata->ctx) 392438c8a9a5SSteve French kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release); 392538c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 392638c8a9a5SSteve French if (rdata->mr) { 392738c8a9a5SSteve French smbd_deregister_mr(rdata->mr); 392838c8a9a5SSteve French rdata->mr = NULL; 392938c8a9a5SSteve French } 393038c8a9a5SSteve French #endif 393138c8a9a5SSteve French if (rdata->cfile) 393238c8a9a5SSteve French cifsFileInfo_put(rdata->cfile); 393338c8a9a5SSteve French 393438c8a9a5SSteve French kfree(rdata); 393538c8a9a5SSteve French } 393638c8a9a5SSteve French 393738c8a9a5SSteve French static void collect_uncached_read_data(struct cifs_aio_ctx *ctx); 393838c8a9a5SSteve French 393938c8a9a5SSteve French static void 394038c8a9a5SSteve French cifs_uncached_readv_complete(struct work_struct *work) 394138c8a9a5SSteve French { 394238c8a9a5SSteve French struct cifs_readdata *rdata = container_of(work, 394338c8a9a5SSteve French struct cifs_readdata, work); 394438c8a9a5SSteve French 394538c8a9a5SSteve French complete(&rdata->done); 394638c8a9a5SSteve French collect_uncached_read_data(rdata->ctx); 394738c8a9a5SSteve French /* the below call can possibly free the last ref to aio ctx */ 394838c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 394938c8a9a5SSteve French } 395038c8a9a5SSteve French 395138c8a9a5SSteve French static int cifs_resend_rdata(struct cifs_readdata *rdata, 395238c8a9a5SSteve French struct list_head *rdata_list, 395338c8a9a5SSteve French struct cifs_aio_ctx *ctx) 395438c8a9a5SSteve French { 395538c8a9a5SSteve French unsigned int rsize; 395638c8a9a5SSteve French struct cifs_credits credits; 395738c8a9a5SSteve French int rc; 395838c8a9a5SSteve French struct TCP_Server_Info *server; 395938c8a9a5SSteve French 396038c8a9a5SSteve French /* XXX: should we pick a new channel here? */ 396138c8a9a5SSteve French server = rdata->server; 396238c8a9a5SSteve French 396338c8a9a5SSteve French do { 396438c8a9a5SSteve French if (rdata->cfile->invalidHandle) { 396538c8a9a5SSteve French rc = cifs_reopen_file(rdata->cfile, true); 396638c8a9a5SSteve French if (rc == -EAGAIN) 396738c8a9a5SSteve French continue; 396838c8a9a5SSteve French else if (rc) 396938c8a9a5SSteve French break; 397038c8a9a5SSteve French } 397138c8a9a5SSteve French 397238c8a9a5SSteve French /* 397338c8a9a5SSteve French * Wait for credits to resend this rdata. 397438c8a9a5SSteve French * Note: we are attempting to resend the whole rdata not in 397538c8a9a5SSteve French * segments 397638c8a9a5SSteve French */ 397738c8a9a5SSteve French do { 397838c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, rdata->bytes, 397938c8a9a5SSteve French &rsize, &credits); 398038c8a9a5SSteve French 398138c8a9a5SSteve French if (rc) 398238c8a9a5SSteve French goto fail; 398338c8a9a5SSteve French 398438c8a9a5SSteve French if (rsize < rdata->bytes) { 398538c8a9a5SSteve French add_credits_and_wake_if(server, &credits, 0); 398638c8a9a5SSteve French msleep(1000); 398738c8a9a5SSteve French } 398838c8a9a5SSteve French } while (rsize < rdata->bytes); 398938c8a9a5SSteve French rdata->credits = credits; 399038c8a9a5SSteve French 399138c8a9a5SSteve French rc = adjust_credits(server, &rdata->credits, rdata->bytes); 399238c8a9a5SSteve French if (!rc) { 399338c8a9a5SSteve French if (rdata->cfile->invalidHandle) 399438c8a9a5SSteve French rc = -EAGAIN; 399538c8a9a5SSteve French else { 399638c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 399738c8a9a5SSteve French if (rdata->mr) { 399838c8a9a5SSteve French rdata->mr->need_invalidate = true; 399938c8a9a5SSteve French smbd_deregister_mr(rdata->mr); 400038c8a9a5SSteve French rdata->mr = NULL; 400138c8a9a5SSteve French } 400238c8a9a5SSteve French #endif 400338c8a9a5SSteve French rc = server->ops->async_readv(rdata); 400438c8a9a5SSteve French } 400538c8a9a5SSteve French } 400638c8a9a5SSteve French 400738c8a9a5SSteve French /* If the read was successfully sent, we are done */ 400838c8a9a5SSteve French if (!rc) { 400938c8a9a5SSteve French /* Add to aio pending list */ 401038c8a9a5SSteve French list_add_tail(&rdata->list, rdata_list); 401138c8a9a5SSteve French return 0; 401238c8a9a5SSteve French } 401338c8a9a5SSteve French 401438c8a9a5SSteve French /* Roll back credits and retry if needed */ 401538c8a9a5SSteve French add_credits_and_wake_if(server, &rdata->credits, 0); 401638c8a9a5SSteve French } while (rc == -EAGAIN); 401738c8a9a5SSteve French 401838c8a9a5SSteve French fail: 401938c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 402038c8a9a5SSteve French return rc; 402138c8a9a5SSteve French } 402238c8a9a5SSteve French 402338c8a9a5SSteve French static int 402438c8a9a5SSteve French cifs_send_async_read(loff_t fpos, size_t len, struct cifsFileInfo *open_file, 402538c8a9a5SSteve French struct cifs_sb_info *cifs_sb, struct list_head *rdata_list, 402638c8a9a5SSteve French struct cifs_aio_ctx *ctx) 402738c8a9a5SSteve French { 402838c8a9a5SSteve French struct cifs_readdata *rdata; 402938c8a9a5SSteve French unsigned int rsize, nsegs, max_segs = INT_MAX; 403038c8a9a5SSteve French struct cifs_credits credits_on_stack; 403138c8a9a5SSteve French struct cifs_credits *credits = &credits_on_stack; 403238c8a9a5SSteve French size_t cur_len, max_len; 403338c8a9a5SSteve French int rc; 403438c8a9a5SSteve French pid_t pid; 403538c8a9a5SSteve French struct TCP_Server_Info *server; 403638c8a9a5SSteve French 403738c8a9a5SSteve French server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); 403838c8a9a5SSteve French 403938c8a9a5SSteve French #ifdef CONFIG_CIFS_SMB_DIRECT 404038c8a9a5SSteve French if (server->smbd_conn) 404138c8a9a5SSteve French max_segs = server->smbd_conn->max_frmr_depth; 404238c8a9a5SSteve French #endif 404338c8a9a5SSteve French 404438c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 404538c8a9a5SSteve French pid = open_file->pid; 404638c8a9a5SSteve French else 404738c8a9a5SSteve French pid = current->tgid; 404838c8a9a5SSteve French 404938c8a9a5SSteve French do { 405038c8a9a5SSteve French if (open_file->invalidHandle) { 405138c8a9a5SSteve French rc = cifs_reopen_file(open_file, true); 405238c8a9a5SSteve French if (rc == -EAGAIN) 405338c8a9a5SSteve French continue; 405438c8a9a5SSteve French else if (rc) 405538c8a9a5SSteve French break; 405638c8a9a5SSteve French } 405738c8a9a5SSteve French 405838c8a9a5SSteve French if (cifs_sb->ctx->rsize == 0) 405938c8a9a5SSteve French cifs_sb->ctx->rsize = 406038c8a9a5SSteve French server->ops->negotiate_rsize(tlink_tcon(open_file->tlink), 406138c8a9a5SSteve French cifs_sb->ctx); 406238c8a9a5SSteve French 406338c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, 406438c8a9a5SSteve French &rsize, credits); 406538c8a9a5SSteve French if (rc) 406638c8a9a5SSteve French break; 406738c8a9a5SSteve French 406838c8a9a5SSteve French max_len = min_t(size_t, len, rsize); 406938c8a9a5SSteve French 407038c8a9a5SSteve French cur_len = cifs_limit_bvec_subset(&ctx->iter, max_len, 407138c8a9a5SSteve French max_segs, &nsegs); 407238c8a9a5SSteve French cifs_dbg(FYI, "read-to-iter len=%zx/%zx nsegs=%u/%lu/%u\n", 407338c8a9a5SSteve French cur_len, max_len, nsegs, ctx->iter.nr_segs, max_segs); 407438c8a9a5SSteve French if (cur_len == 0) { 407538c8a9a5SSteve French rc = -EIO; 407638c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 407738c8a9a5SSteve French break; 407838c8a9a5SSteve French } 407938c8a9a5SSteve French 408038c8a9a5SSteve French rdata = cifs_readdata_alloc(cifs_uncached_readv_complete); 408138c8a9a5SSteve French if (!rdata) { 408238c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 408338c8a9a5SSteve French rc = -ENOMEM; 408438c8a9a5SSteve French break; 408538c8a9a5SSteve French } 408638c8a9a5SSteve French 408738c8a9a5SSteve French rdata->server = server; 408838c8a9a5SSteve French rdata->cfile = cifsFileInfo_get(open_file); 408938c8a9a5SSteve French rdata->offset = fpos; 409038c8a9a5SSteve French rdata->bytes = cur_len; 409138c8a9a5SSteve French rdata->pid = pid; 409238c8a9a5SSteve French rdata->credits = credits_on_stack; 409338c8a9a5SSteve French rdata->ctx = ctx; 409438c8a9a5SSteve French kref_get(&ctx->refcount); 409538c8a9a5SSteve French 409638c8a9a5SSteve French rdata->iter = ctx->iter; 409738c8a9a5SSteve French iov_iter_truncate(&rdata->iter, cur_len); 409838c8a9a5SSteve French 409938c8a9a5SSteve French rc = adjust_credits(server, &rdata->credits, rdata->bytes); 410038c8a9a5SSteve French 410138c8a9a5SSteve French if (!rc) { 410238c8a9a5SSteve French if (rdata->cfile->invalidHandle) 410338c8a9a5SSteve French rc = -EAGAIN; 410438c8a9a5SSteve French else 410538c8a9a5SSteve French rc = server->ops->async_readv(rdata); 410638c8a9a5SSteve French } 410738c8a9a5SSteve French 410838c8a9a5SSteve French if (rc) { 410938c8a9a5SSteve French add_credits_and_wake_if(server, &rdata->credits, 0); 411038c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 411138c8a9a5SSteve French if (rc == -EAGAIN) 411238c8a9a5SSteve French continue; 411338c8a9a5SSteve French break; 411438c8a9a5SSteve French } 411538c8a9a5SSteve French 411638c8a9a5SSteve French list_add_tail(&rdata->list, rdata_list); 411738c8a9a5SSteve French iov_iter_advance(&ctx->iter, cur_len); 411838c8a9a5SSteve French fpos += cur_len; 411938c8a9a5SSteve French len -= cur_len; 412038c8a9a5SSteve French } while (len > 0); 412138c8a9a5SSteve French 412238c8a9a5SSteve French return rc; 412338c8a9a5SSteve French } 412438c8a9a5SSteve French 412538c8a9a5SSteve French static void 412638c8a9a5SSteve French collect_uncached_read_data(struct cifs_aio_ctx *ctx) 412738c8a9a5SSteve French { 412838c8a9a5SSteve French struct cifs_readdata *rdata, *tmp; 412938c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 413038c8a9a5SSteve French int rc; 413138c8a9a5SSteve French 413238c8a9a5SSteve French cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb); 413338c8a9a5SSteve French 413438c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 413538c8a9a5SSteve French 413638c8a9a5SSteve French if (list_empty(&ctx->list)) { 413738c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 413838c8a9a5SSteve French return; 413938c8a9a5SSteve French } 414038c8a9a5SSteve French 414138c8a9a5SSteve French rc = ctx->rc; 414238c8a9a5SSteve French /* the loop below should proceed in the order of increasing offsets */ 414338c8a9a5SSteve French again: 414438c8a9a5SSteve French list_for_each_entry_safe(rdata, tmp, &ctx->list, list) { 414538c8a9a5SSteve French if (!rc) { 414638c8a9a5SSteve French if (!try_wait_for_completion(&rdata->done)) { 414738c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 414838c8a9a5SSteve French return; 414938c8a9a5SSteve French } 415038c8a9a5SSteve French 415138c8a9a5SSteve French if (rdata->result == -EAGAIN) { 415238c8a9a5SSteve French /* resend call if it's a retryable error */ 415338c8a9a5SSteve French struct list_head tmp_list; 415438c8a9a5SSteve French unsigned int got_bytes = rdata->got_bytes; 415538c8a9a5SSteve French 415638c8a9a5SSteve French list_del_init(&rdata->list); 415738c8a9a5SSteve French INIT_LIST_HEAD(&tmp_list); 415838c8a9a5SSteve French 415938c8a9a5SSteve French if (ctx->direct_io) { 416038c8a9a5SSteve French /* 416138c8a9a5SSteve French * Re-use rdata as this is a 416238c8a9a5SSteve French * direct I/O 416338c8a9a5SSteve French */ 416438c8a9a5SSteve French rc = cifs_resend_rdata( 416538c8a9a5SSteve French rdata, 416638c8a9a5SSteve French &tmp_list, ctx); 416738c8a9a5SSteve French } else { 416838c8a9a5SSteve French rc = cifs_send_async_read( 416938c8a9a5SSteve French rdata->offset + got_bytes, 417038c8a9a5SSteve French rdata->bytes - got_bytes, 417138c8a9a5SSteve French rdata->cfile, cifs_sb, 417238c8a9a5SSteve French &tmp_list, ctx); 417338c8a9a5SSteve French 417438c8a9a5SSteve French kref_put(&rdata->refcount, 417538c8a9a5SSteve French cifs_readdata_release); 417638c8a9a5SSteve French } 417738c8a9a5SSteve French 417838c8a9a5SSteve French list_splice(&tmp_list, &ctx->list); 417938c8a9a5SSteve French 418038c8a9a5SSteve French goto again; 418138c8a9a5SSteve French } else if (rdata->result) 418238c8a9a5SSteve French rc = rdata->result; 418338c8a9a5SSteve French 418438c8a9a5SSteve French /* if there was a short read -- discard anything left */ 418538c8a9a5SSteve French if (rdata->got_bytes && rdata->got_bytes < rdata->bytes) 418638c8a9a5SSteve French rc = -ENODATA; 418738c8a9a5SSteve French 418838c8a9a5SSteve French ctx->total_len += rdata->got_bytes; 418938c8a9a5SSteve French } 419038c8a9a5SSteve French list_del_init(&rdata->list); 419138c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 419238c8a9a5SSteve French } 419338c8a9a5SSteve French 419438c8a9a5SSteve French /* mask nodata case */ 419538c8a9a5SSteve French if (rc == -ENODATA) 419638c8a9a5SSteve French rc = 0; 419738c8a9a5SSteve French 419838c8a9a5SSteve French ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc; 419938c8a9a5SSteve French 420038c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 420138c8a9a5SSteve French 420238c8a9a5SSteve French if (ctx->iocb && ctx->iocb->ki_complete) 420338c8a9a5SSteve French ctx->iocb->ki_complete(ctx->iocb, ctx->rc); 420438c8a9a5SSteve French else 420538c8a9a5SSteve French complete(&ctx->done); 420638c8a9a5SSteve French } 420738c8a9a5SSteve French 420838c8a9a5SSteve French static ssize_t __cifs_readv( 420938c8a9a5SSteve French struct kiocb *iocb, struct iov_iter *to, bool direct) 421038c8a9a5SSteve French { 421138c8a9a5SSteve French size_t len; 421238c8a9a5SSteve French struct file *file = iocb->ki_filp; 421338c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 421438c8a9a5SSteve French struct cifsFileInfo *cfile; 421538c8a9a5SSteve French struct cifs_tcon *tcon; 421638c8a9a5SSteve French ssize_t rc, total_read = 0; 421738c8a9a5SSteve French loff_t offset = iocb->ki_pos; 421838c8a9a5SSteve French struct cifs_aio_ctx *ctx; 421938c8a9a5SSteve French 422038c8a9a5SSteve French len = iov_iter_count(to); 422138c8a9a5SSteve French if (!len) 422238c8a9a5SSteve French return 0; 422338c8a9a5SSteve French 422438c8a9a5SSteve French cifs_sb = CIFS_FILE_SB(file); 422538c8a9a5SSteve French cfile = file->private_data; 422638c8a9a5SSteve French tcon = tlink_tcon(cfile->tlink); 422738c8a9a5SSteve French 422838c8a9a5SSteve French if (!tcon->ses->server->ops->async_readv) 422938c8a9a5SSteve French return -ENOSYS; 423038c8a9a5SSteve French 423138c8a9a5SSteve French if ((file->f_flags & O_ACCMODE) == O_WRONLY) 423238c8a9a5SSteve French cifs_dbg(FYI, "attempting read on write only file instance\n"); 423338c8a9a5SSteve French 423438c8a9a5SSteve French ctx = cifs_aio_ctx_alloc(); 423538c8a9a5SSteve French if (!ctx) 423638c8a9a5SSteve French return -ENOMEM; 423738c8a9a5SSteve French 423838c8a9a5SSteve French ctx->pos = offset; 423938c8a9a5SSteve French ctx->direct_io = direct; 424038c8a9a5SSteve French ctx->len = len; 424138c8a9a5SSteve French ctx->cfile = cifsFileInfo_get(cfile); 424238c8a9a5SSteve French ctx->nr_pinned_pages = 0; 424338c8a9a5SSteve French 424438c8a9a5SSteve French if (!is_sync_kiocb(iocb)) 424538c8a9a5SSteve French ctx->iocb = iocb; 424638c8a9a5SSteve French 424738c8a9a5SSteve French if (user_backed_iter(to)) { 424838c8a9a5SSteve French /* 424938c8a9a5SSteve French * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as 425038c8a9a5SSteve French * they contain references to the calling process's virtual 425138c8a9a5SSteve French * memory layout which won't be available in an async worker 425238c8a9a5SSteve French * thread. This also takes a pin on every folio involved. 425338c8a9a5SSteve French */ 425438c8a9a5SSteve French rc = netfs_extract_user_iter(to, iov_iter_count(to), 425538c8a9a5SSteve French &ctx->iter, 0); 425638c8a9a5SSteve French if (rc < 0) { 425738c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 425838c8a9a5SSteve French return rc; 425938c8a9a5SSteve French } 426038c8a9a5SSteve French 426138c8a9a5SSteve French ctx->nr_pinned_pages = rc; 426238c8a9a5SSteve French ctx->bv = (void *)ctx->iter.bvec; 426338c8a9a5SSteve French ctx->bv_need_unpin = iov_iter_extract_will_pin(to); 426438c8a9a5SSteve French ctx->should_dirty = true; 426538c8a9a5SSteve French } else if ((iov_iter_is_bvec(to) || iov_iter_is_kvec(to)) && 426638c8a9a5SSteve French !is_sync_kiocb(iocb)) { 426738c8a9a5SSteve French /* 426838c8a9a5SSteve French * If the op is asynchronous, we need to copy the list attached 426938c8a9a5SSteve French * to a BVEC/KVEC-type iterator, but we assume that the storage 427038c8a9a5SSteve French * will be retained by the caller; in any case, we may or may 427138c8a9a5SSteve French * not be able to pin the pages, so we don't try. 427238c8a9a5SSteve French */ 427338c8a9a5SSteve French ctx->bv = (void *)dup_iter(&ctx->iter, to, GFP_KERNEL); 427438c8a9a5SSteve French if (!ctx->bv) { 427538c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 427638c8a9a5SSteve French return -ENOMEM; 427738c8a9a5SSteve French } 427838c8a9a5SSteve French } else { 427938c8a9a5SSteve French /* 428038c8a9a5SSteve French * Otherwise, we just pass the iterator down as-is and rely on 428138c8a9a5SSteve French * the caller to make sure the pages referred to by the 428238c8a9a5SSteve French * iterator don't evaporate. 428338c8a9a5SSteve French */ 428438c8a9a5SSteve French ctx->iter = *to; 428538c8a9a5SSteve French } 428638c8a9a5SSteve French 428738c8a9a5SSteve French if (direct) { 428838c8a9a5SSteve French rc = filemap_write_and_wait_range(file->f_inode->i_mapping, 428938c8a9a5SSteve French offset, offset + len - 1); 429038c8a9a5SSteve French if (rc) { 429138c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 429238c8a9a5SSteve French return -EAGAIN; 429338c8a9a5SSteve French } 429438c8a9a5SSteve French } 429538c8a9a5SSteve French 429638c8a9a5SSteve French /* grab a lock here due to read response handlers can access ctx */ 429738c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 429838c8a9a5SSteve French 429938c8a9a5SSteve French rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx); 430038c8a9a5SSteve French 430138c8a9a5SSteve French /* if at least one read request send succeeded, then reset rc */ 430238c8a9a5SSteve French if (!list_empty(&ctx->list)) 430338c8a9a5SSteve French rc = 0; 430438c8a9a5SSteve French 430538c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 430638c8a9a5SSteve French 430738c8a9a5SSteve French if (rc) { 430838c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 430938c8a9a5SSteve French return rc; 431038c8a9a5SSteve French } 431138c8a9a5SSteve French 431238c8a9a5SSteve French if (!is_sync_kiocb(iocb)) { 431338c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 431438c8a9a5SSteve French return -EIOCBQUEUED; 431538c8a9a5SSteve French } 431638c8a9a5SSteve French 431738c8a9a5SSteve French rc = wait_for_completion_killable(&ctx->done); 431838c8a9a5SSteve French if (rc) { 431938c8a9a5SSteve French mutex_lock(&ctx->aio_mutex); 432038c8a9a5SSteve French ctx->rc = rc = -EINTR; 432138c8a9a5SSteve French total_read = ctx->total_len; 432238c8a9a5SSteve French mutex_unlock(&ctx->aio_mutex); 432338c8a9a5SSteve French } else { 432438c8a9a5SSteve French rc = ctx->rc; 432538c8a9a5SSteve French total_read = ctx->total_len; 432638c8a9a5SSteve French } 432738c8a9a5SSteve French 432838c8a9a5SSteve French kref_put(&ctx->refcount, cifs_aio_ctx_release); 432938c8a9a5SSteve French 433038c8a9a5SSteve French if (total_read) { 433138c8a9a5SSteve French iocb->ki_pos += total_read; 433238c8a9a5SSteve French return total_read; 433338c8a9a5SSteve French } 433438c8a9a5SSteve French return rc; 433538c8a9a5SSteve French } 433638c8a9a5SSteve French 433738c8a9a5SSteve French ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to) 433838c8a9a5SSteve French { 433938c8a9a5SSteve French return __cifs_readv(iocb, to, true); 434038c8a9a5SSteve French } 434138c8a9a5SSteve French 434238c8a9a5SSteve French ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to) 434338c8a9a5SSteve French { 434438c8a9a5SSteve French return __cifs_readv(iocb, to, false); 434538c8a9a5SSteve French } 434638c8a9a5SSteve French 434738c8a9a5SSteve French ssize_t 434838c8a9a5SSteve French cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to) 434938c8a9a5SSteve French { 435038c8a9a5SSteve French struct inode *inode = file_inode(iocb->ki_filp); 435138c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 435238c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 435338c8a9a5SSteve French struct cifsFileInfo *cfile = (struct cifsFileInfo *) 435438c8a9a5SSteve French iocb->ki_filp->private_data; 435538c8a9a5SSteve French struct cifs_tcon *tcon = tlink_tcon(cfile->tlink); 435638c8a9a5SSteve French int rc = -EACCES; 435738c8a9a5SSteve French 435838c8a9a5SSteve French /* 435938c8a9a5SSteve French * In strict cache mode we need to read from the server all the time 436038c8a9a5SSteve French * if we don't have level II oplock because the server can delay mtime 436138c8a9a5SSteve French * change - so we can't make a decision about inode invalidating. 436238c8a9a5SSteve French * And we can also fail with pagereading if there are mandatory locks 436338c8a9a5SSteve French * on pages affected by this read but not on the region from pos to 436438c8a9a5SSteve French * pos+len-1. 436538c8a9a5SSteve French */ 436638c8a9a5SSteve French if (!CIFS_CACHE_READ(cinode)) 436738c8a9a5SSteve French return cifs_user_readv(iocb, to); 436838c8a9a5SSteve French 436938c8a9a5SSteve French if (cap_unix(tcon->ses) && 437038c8a9a5SSteve French (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) && 437138c8a9a5SSteve French ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) 437238c8a9a5SSteve French return generic_file_read_iter(iocb, to); 437338c8a9a5SSteve French 437438c8a9a5SSteve French /* 437538c8a9a5SSteve French * We need to hold the sem to be sure nobody modifies lock list 437638c8a9a5SSteve French * with a brlock that prevents reading. 437738c8a9a5SSteve French */ 437838c8a9a5SSteve French down_read(&cinode->lock_sem); 437938c8a9a5SSteve French if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to), 438038c8a9a5SSteve French tcon->ses->server->vals->shared_lock_type, 438138c8a9a5SSteve French 0, NULL, CIFS_READ_OP)) 438238c8a9a5SSteve French rc = generic_file_read_iter(iocb, to); 438338c8a9a5SSteve French up_read(&cinode->lock_sem); 438438c8a9a5SSteve French return rc; 438538c8a9a5SSteve French } 438638c8a9a5SSteve French 438738c8a9a5SSteve French static ssize_t 438838c8a9a5SSteve French cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset) 438938c8a9a5SSteve French { 439038c8a9a5SSteve French int rc = -EACCES; 439138c8a9a5SSteve French unsigned int bytes_read = 0; 439238c8a9a5SSteve French unsigned int total_read; 439338c8a9a5SSteve French unsigned int current_read_size; 439438c8a9a5SSteve French unsigned int rsize; 439538c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 439638c8a9a5SSteve French struct cifs_tcon *tcon; 439738c8a9a5SSteve French struct TCP_Server_Info *server; 439838c8a9a5SSteve French unsigned int xid; 439938c8a9a5SSteve French char *cur_offset; 440038c8a9a5SSteve French struct cifsFileInfo *open_file; 440138c8a9a5SSteve French struct cifs_io_parms io_parms = {0}; 440238c8a9a5SSteve French int buf_type = CIFS_NO_BUFFER; 440338c8a9a5SSteve French __u32 pid; 440438c8a9a5SSteve French 440538c8a9a5SSteve French xid = get_xid(); 440638c8a9a5SSteve French cifs_sb = CIFS_FILE_SB(file); 440738c8a9a5SSteve French 440838c8a9a5SSteve French /* FIXME: set up handlers for larger reads and/or convert to async */ 440938c8a9a5SSteve French rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize); 441038c8a9a5SSteve French 441138c8a9a5SSteve French if (file->private_data == NULL) { 441238c8a9a5SSteve French rc = -EBADF; 441338c8a9a5SSteve French free_xid(xid); 441438c8a9a5SSteve French return rc; 441538c8a9a5SSteve French } 441638c8a9a5SSteve French open_file = file->private_data; 441738c8a9a5SSteve French tcon = tlink_tcon(open_file->tlink); 441838c8a9a5SSteve French server = cifs_pick_channel(tcon->ses); 441938c8a9a5SSteve French 442038c8a9a5SSteve French if (!server->ops->sync_read) { 442138c8a9a5SSteve French free_xid(xid); 442238c8a9a5SSteve French return -ENOSYS; 442338c8a9a5SSteve French } 442438c8a9a5SSteve French 442538c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 442638c8a9a5SSteve French pid = open_file->pid; 442738c8a9a5SSteve French else 442838c8a9a5SSteve French pid = current->tgid; 442938c8a9a5SSteve French 443038c8a9a5SSteve French if ((file->f_flags & O_ACCMODE) == O_WRONLY) 443138c8a9a5SSteve French cifs_dbg(FYI, "attempting read on write only file instance\n"); 443238c8a9a5SSteve French 443338c8a9a5SSteve French for (total_read = 0, cur_offset = read_data; read_size > total_read; 443438c8a9a5SSteve French total_read += bytes_read, cur_offset += bytes_read) { 443538c8a9a5SSteve French do { 443638c8a9a5SSteve French current_read_size = min_t(uint, read_size - total_read, 443738c8a9a5SSteve French rsize); 443838c8a9a5SSteve French /* 443938c8a9a5SSteve French * For windows me and 9x we do not want to request more 444038c8a9a5SSteve French * than it negotiated since it will refuse the read 444138c8a9a5SSteve French * then. 444238c8a9a5SSteve French */ 444338c8a9a5SSteve French if (!(tcon->ses->capabilities & 444438c8a9a5SSteve French tcon->ses->server->vals->cap_large_files)) { 444538c8a9a5SSteve French current_read_size = min_t(uint, 444638c8a9a5SSteve French current_read_size, CIFSMaxBufSize); 444738c8a9a5SSteve French } 444838c8a9a5SSteve French if (open_file->invalidHandle) { 444938c8a9a5SSteve French rc = cifs_reopen_file(open_file, true); 445038c8a9a5SSteve French if (rc != 0) 445138c8a9a5SSteve French break; 445238c8a9a5SSteve French } 445338c8a9a5SSteve French io_parms.pid = pid; 445438c8a9a5SSteve French io_parms.tcon = tcon; 445538c8a9a5SSteve French io_parms.offset = *offset; 445638c8a9a5SSteve French io_parms.length = current_read_size; 445738c8a9a5SSteve French io_parms.server = server; 445838c8a9a5SSteve French rc = server->ops->sync_read(xid, &open_file->fid, &io_parms, 445938c8a9a5SSteve French &bytes_read, &cur_offset, 446038c8a9a5SSteve French &buf_type); 446138c8a9a5SSteve French } while (rc == -EAGAIN); 446238c8a9a5SSteve French 446338c8a9a5SSteve French if (rc || (bytes_read == 0)) { 446438c8a9a5SSteve French if (total_read) { 446538c8a9a5SSteve French break; 446638c8a9a5SSteve French } else { 446738c8a9a5SSteve French free_xid(xid); 446838c8a9a5SSteve French return rc; 446938c8a9a5SSteve French } 447038c8a9a5SSteve French } else { 447138c8a9a5SSteve French cifs_stats_bytes_read(tcon, total_read); 447238c8a9a5SSteve French *offset += bytes_read; 447338c8a9a5SSteve French } 447438c8a9a5SSteve French } 447538c8a9a5SSteve French free_xid(xid); 447638c8a9a5SSteve French return total_read; 447738c8a9a5SSteve French } 447838c8a9a5SSteve French 447938c8a9a5SSteve French /* 448038c8a9a5SSteve French * If the page is mmap'ed into a process' page tables, then we need to make 448138c8a9a5SSteve French * sure that it doesn't change while being written back. 448238c8a9a5SSteve French */ 448338c8a9a5SSteve French static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf) 448438c8a9a5SSteve French { 448538c8a9a5SSteve French struct folio *folio = page_folio(vmf->page); 448638c8a9a5SSteve French 448738c8a9a5SSteve French /* Wait for the folio to be written to the cache before we allow it to 448838c8a9a5SSteve French * be modified. We then assume the entire folio will need writing back. 448938c8a9a5SSteve French */ 449038c8a9a5SSteve French #ifdef CONFIG_CIFS_FSCACHE 449138c8a9a5SSteve French if (folio_test_fscache(folio) && 449238c8a9a5SSteve French folio_wait_fscache_killable(folio) < 0) 449338c8a9a5SSteve French return VM_FAULT_RETRY; 449438c8a9a5SSteve French #endif 449538c8a9a5SSteve French 449638c8a9a5SSteve French folio_wait_writeback(folio); 449738c8a9a5SSteve French 449838c8a9a5SSteve French if (folio_lock_killable(folio) < 0) 449938c8a9a5SSteve French return VM_FAULT_RETRY; 450038c8a9a5SSteve French return VM_FAULT_LOCKED; 450138c8a9a5SSteve French } 450238c8a9a5SSteve French 450338c8a9a5SSteve French static const struct vm_operations_struct cifs_file_vm_ops = { 450438c8a9a5SSteve French .fault = filemap_fault, 450538c8a9a5SSteve French .map_pages = filemap_map_pages, 450638c8a9a5SSteve French .page_mkwrite = cifs_page_mkwrite, 450738c8a9a5SSteve French }; 450838c8a9a5SSteve French 450938c8a9a5SSteve French int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) 451038c8a9a5SSteve French { 451138c8a9a5SSteve French int xid, rc = 0; 451238c8a9a5SSteve French struct inode *inode = file_inode(file); 451338c8a9a5SSteve French 451438c8a9a5SSteve French xid = get_xid(); 451538c8a9a5SSteve French 451638c8a9a5SSteve French if (!CIFS_CACHE_READ(CIFS_I(inode))) 451738c8a9a5SSteve French rc = cifs_zap_mapping(inode); 451838c8a9a5SSteve French if (!rc) 451938c8a9a5SSteve French rc = generic_file_mmap(file, vma); 452038c8a9a5SSteve French if (!rc) 452138c8a9a5SSteve French vma->vm_ops = &cifs_file_vm_ops; 452238c8a9a5SSteve French 452338c8a9a5SSteve French free_xid(xid); 452438c8a9a5SSteve French return rc; 452538c8a9a5SSteve French } 452638c8a9a5SSteve French 452738c8a9a5SSteve French int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) 452838c8a9a5SSteve French { 452938c8a9a5SSteve French int rc, xid; 453038c8a9a5SSteve French 453138c8a9a5SSteve French xid = get_xid(); 453238c8a9a5SSteve French 453338c8a9a5SSteve French rc = cifs_revalidate_file(file); 453438c8a9a5SSteve French if (rc) 453538c8a9a5SSteve French cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n", 453638c8a9a5SSteve French rc); 453738c8a9a5SSteve French if (!rc) 453838c8a9a5SSteve French rc = generic_file_mmap(file, vma); 453938c8a9a5SSteve French if (!rc) 454038c8a9a5SSteve French vma->vm_ops = &cifs_file_vm_ops; 454138c8a9a5SSteve French 454238c8a9a5SSteve French free_xid(xid); 454338c8a9a5SSteve French return rc; 454438c8a9a5SSteve French } 454538c8a9a5SSteve French 454638c8a9a5SSteve French /* 454738c8a9a5SSteve French * Unlock a bunch of folios in the pagecache. 454838c8a9a5SSteve French */ 454938c8a9a5SSteve French static void cifs_unlock_folios(struct address_space *mapping, pgoff_t first, pgoff_t last) 455038c8a9a5SSteve French { 455138c8a9a5SSteve French struct folio *folio; 455238c8a9a5SSteve French XA_STATE(xas, &mapping->i_pages, first); 455338c8a9a5SSteve French 455438c8a9a5SSteve French rcu_read_lock(); 455538c8a9a5SSteve French xas_for_each(&xas, folio, last) { 455638c8a9a5SSteve French folio_unlock(folio); 455738c8a9a5SSteve French } 455838c8a9a5SSteve French rcu_read_unlock(); 455938c8a9a5SSteve French } 456038c8a9a5SSteve French 456138c8a9a5SSteve French static void cifs_readahead_complete(struct work_struct *work) 456238c8a9a5SSteve French { 456338c8a9a5SSteve French struct cifs_readdata *rdata = container_of(work, 456438c8a9a5SSteve French struct cifs_readdata, work); 456538c8a9a5SSteve French struct folio *folio; 456638c8a9a5SSteve French pgoff_t last; 456738c8a9a5SSteve French bool good = rdata->result == 0 || (rdata->result == -EAGAIN && rdata->got_bytes); 456838c8a9a5SSteve French 456938c8a9a5SSteve French XA_STATE(xas, &rdata->mapping->i_pages, rdata->offset / PAGE_SIZE); 457038c8a9a5SSteve French 457138c8a9a5SSteve French if (good) 457238c8a9a5SSteve French cifs_readahead_to_fscache(rdata->mapping->host, 457338c8a9a5SSteve French rdata->offset, rdata->bytes); 457438c8a9a5SSteve French 457538c8a9a5SSteve French if (iov_iter_count(&rdata->iter) > 0) 457638c8a9a5SSteve French iov_iter_zero(iov_iter_count(&rdata->iter), &rdata->iter); 457738c8a9a5SSteve French 457838c8a9a5SSteve French last = (rdata->offset + rdata->bytes - 1) / PAGE_SIZE; 457938c8a9a5SSteve French 458038c8a9a5SSteve French rcu_read_lock(); 458138c8a9a5SSteve French xas_for_each(&xas, folio, last) { 458238c8a9a5SSteve French if (good) { 458338c8a9a5SSteve French flush_dcache_folio(folio); 458438c8a9a5SSteve French folio_mark_uptodate(folio); 458538c8a9a5SSteve French } 458638c8a9a5SSteve French folio_unlock(folio); 458738c8a9a5SSteve French } 458838c8a9a5SSteve French rcu_read_unlock(); 458938c8a9a5SSteve French 459038c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 459138c8a9a5SSteve French } 459238c8a9a5SSteve French 459338c8a9a5SSteve French static void cifs_readahead(struct readahead_control *ractl) 459438c8a9a5SSteve French { 459538c8a9a5SSteve French struct cifsFileInfo *open_file = ractl->file->private_data; 459638c8a9a5SSteve French struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file); 459738c8a9a5SSteve French struct TCP_Server_Info *server; 459838c8a9a5SSteve French unsigned int xid, nr_pages, cache_nr_pages = 0; 459938c8a9a5SSteve French unsigned int ra_pages; 460038c8a9a5SSteve French pgoff_t next_cached = ULONG_MAX, ra_index; 460138c8a9a5SSteve French bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) && 460238c8a9a5SSteve French cifs_inode_cookie(ractl->mapping->host)->cache_priv; 460338c8a9a5SSteve French bool check_cache = caching; 460438c8a9a5SSteve French pid_t pid; 460538c8a9a5SSteve French int rc = 0; 460638c8a9a5SSteve French 460738c8a9a5SSteve French /* Note that readahead_count() lags behind our dequeuing of pages from 460838c8a9a5SSteve French * the ractl, wo we have to keep track for ourselves. 460938c8a9a5SSteve French */ 461038c8a9a5SSteve French ra_pages = readahead_count(ractl); 461138c8a9a5SSteve French ra_index = readahead_index(ractl); 461238c8a9a5SSteve French 461338c8a9a5SSteve French xid = get_xid(); 461438c8a9a5SSteve French 461538c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) 461638c8a9a5SSteve French pid = open_file->pid; 461738c8a9a5SSteve French else 461838c8a9a5SSteve French pid = current->tgid; 461938c8a9a5SSteve French 462038c8a9a5SSteve French server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses); 462138c8a9a5SSteve French 462238c8a9a5SSteve French cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n", 462338c8a9a5SSteve French __func__, ractl->file, ractl->mapping, ra_pages); 462438c8a9a5SSteve French 462538c8a9a5SSteve French /* 462638c8a9a5SSteve French * Chop the readahead request up into rsize-sized read requests. 462738c8a9a5SSteve French */ 462838c8a9a5SSteve French while ((nr_pages = ra_pages)) { 462938c8a9a5SSteve French unsigned int i, rsize; 463038c8a9a5SSteve French struct cifs_readdata *rdata; 463138c8a9a5SSteve French struct cifs_credits credits_on_stack; 463238c8a9a5SSteve French struct cifs_credits *credits = &credits_on_stack; 463338c8a9a5SSteve French struct folio *folio; 463438c8a9a5SSteve French pgoff_t fsize; 463538c8a9a5SSteve French 463638c8a9a5SSteve French /* 463738c8a9a5SSteve French * Find out if we have anything cached in the range of 463838c8a9a5SSteve French * interest, and if so, where the next chunk of cached data is. 463938c8a9a5SSteve French */ 464038c8a9a5SSteve French if (caching) { 464138c8a9a5SSteve French if (check_cache) { 464238c8a9a5SSteve French rc = cifs_fscache_query_occupancy( 464338c8a9a5SSteve French ractl->mapping->host, ra_index, nr_pages, 464438c8a9a5SSteve French &next_cached, &cache_nr_pages); 464538c8a9a5SSteve French if (rc < 0) 464638c8a9a5SSteve French caching = false; 464738c8a9a5SSteve French check_cache = false; 464838c8a9a5SSteve French } 464938c8a9a5SSteve French 465038c8a9a5SSteve French if (ra_index == next_cached) { 465138c8a9a5SSteve French /* 465238c8a9a5SSteve French * TODO: Send a whole batch of pages to be read 465338c8a9a5SSteve French * by the cache. 465438c8a9a5SSteve French */ 465538c8a9a5SSteve French folio = readahead_folio(ractl); 465638c8a9a5SSteve French fsize = folio_nr_pages(folio); 465738c8a9a5SSteve French ra_pages -= fsize; 465838c8a9a5SSteve French ra_index += fsize; 465938c8a9a5SSteve French if (cifs_readpage_from_fscache(ractl->mapping->host, 466038c8a9a5SSteve French &folio->page) < 0) { 466138c8a9a5SSteve French /* 466238c8a9a5SSteve French * TODO: Deal with cache read failure 466338c8a9a5SSteve French * here, but for the moment, delegate 466438c8a9a5SSteve French * that to readpage. 466538c8a9a5SSteve French */ 466638c8a9a5SSteve French caching = false; 466738c8a9a5SSteve French } 466838c8a9a5SSteve French folio_unlock(folio); 466938c8a9a5SSteve French next_cached += fsize; 467038c8a9a5SSteve French cache_nr_pages -= fsize; 467138c8a9a5SSteve French if (cache_nr_pages == 0) 467238c8a9a5SSteve French check_cache = true; 467338c8a9a5SSteve French continue; 467438c8a9a5SSteve French } 467538c8a9a5SSteve French } 467638c8a9a5SSteve French 467738c8a9a5SSteve French if (open_file->invalidHandle) { 467838c8a9a5SSteve French rc = cifs_reopen_file(open_file, true); 467938c8a9a5SSteve French if (rc) { 468038c8a9a5SSteve French if (rc == -EAGAIN) 468138c8a9a5SSteve French continue; 468238c8a9a5SSteve French break; 468338c8a9a5SSteve French } 468438c8a9a5SSteve French } 468538c8a9a5SSteve French 468638c8a9a5SSteve French if (cifs_sb->ctx->rsize == 0) 468738c8a9a5SSteve French cifs_sb->ctx->rsize = 468838c8a9a5SSteve French server->ops->negotiate_rsize(tlink_tcon(open_file->tlink), 468938c8a9a5SSteve French cifs_sb->ctx); 469038c8a9a5SSteve French 469138c8a9a5SSteve French rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize, 469238c8a9a5SSteve French &rsize, credits); 469338c8a9a5SSteve French if (rc) 469438c8a9a5SSteve French break; 469538c8a9a5SSteve French nr_pages = min_t(size_t, rsize / PAGE_SIZE, ra_pages); 469638c8a9a5SSteve French if (next_cached != ULONG_MAX) 469738c8a9a5SSteve French nr_pages = min_t(size_t, nr_pages, next_cached - ra_index); 469838c8a9a5SSteve French 469938c8a9a5SSteve French /* 470038c8a9a5SSteve French * Give up immediately if rsize is too small to read an entire 470138c8a9a5SSteve French * page. The VFS will fall back to readpage. We should never 470238c8a9a5SSteve French * reach this point however since we set ra_pages to 0 when the 470338c8a9a5SSteve French * rsize is smaller than a cache page. 470438c8a9a5SSteve French */ 470538c8a9a5SSteve French if (unlikely(!nr_pages)) { 470638c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 470738c8a9a5SSteve French break; 470838c8a9a5SSteve French } 470938c8a9a5SSteve French 471038c8a9a5SSteve French rdata = cifs_readdata_alloc(cifs_readahead_complete); 471138c8a9a5SSteve French if (!rdata) { 471238c8a9a5SSteve French /* best to give up if we're out of mem */ 471338c8a9a5SSteve French add_credits_and_wake_if(server, credits, 0); 471438c8a9a5SSteve French break; 471538c8a9a5SSteve French } 471638c8a9a5SSteve French 471738c8a9a5SSteve French rdata->offset = ra_index * PAGE_SIZE; 471838c8a9a5SSteve French rdata->bytes = nr_pages * PAGE_SIZE; 471938c8a9a5SSteve French rdata->cfile = cifsFileInfo_get(open_file); 472038c8a9a5SSteve French rdata->server = server; 472138c8a9a5SSteve French rdata->mapping = ractl->mapping; 472238c8a9a5SSteve French rdata->pid = pid; 472338c8a9a5SSteve French rdata->credits = credits_on_stack; 472438c8a9a5SSteve French 472538c8a9a5SSteve French for (i = 0; i < nr_pages; i++) { 472638c8a9a5SSteve French if (!readahead_folio(ractl)) 472738c8a9a5SSteve French WARN_ON(1); 472838c8a9a5SSteve French } 472938c8a9a5SSteve French ra_pages -= nr_pages; 473038c8a9a5SSteve French ra_index += nr_pages; 473138c8a9a5SSteve French 473238c8a9a5SSteve French iov_iter_xarray(&rdata->iter, ITER_DEST, &rdata->mapping->i_pages, 473338c8a9a5SSteve French rdata->offset, rdata->bytes); 473438c8a9a5SSteve French 473538c8a9a5SSteve French rc = adjust_credits(server, &rdata->credits, rdata->bytes); 473638c8a9a5SSteve French if (!rc) { 473738c8a9a5SSteve French if (rdata->cfile->invalidHandle) 473838c8a9a5SSteve French rc = -EAGAIN; 473938c8a9a5SSteve French else 474038c8a9a5SSteve French rc = server->ops->async_readv(rdata); 474138c8a9a5SSteve French } 474238c8a9a5SSteve French 474338c8a9a5SSteve French if (rc) { 474438c8a9a5SSteve French add_credits_and_wake_if(server, &rdata->credits, 0); 474538c8a9a5SSteve French cifs_unlock_folios(rdata->mapping, 474638c8a9a5SSteve French rdata->offset / PAGE_SIZE, 474738c8a9a5SSteve French (rdata->offset + rdata->bytes - 1) / PAGE_SIZE); 474838c8a9a5SSteve French /* Fallback to the readpage in error/reconnect cases */ 474938c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 475038c8a9a5SSteve French break; 475138c8a9a5SSteve French } 475238c8a9a5SSteve French 475338c8a9a5SSteve French kref_put(&rdata->refcount, cifs_readdata_release); 475438c8a9a5SSteve French } 475538c8a9a5SSteve French 475638c8a9a5SSteve French free_xid(xid); 475738c8a9a5SSteve French } 475838c8a9a5SSteve French 475938c8a9a5SSteve French /* 476038c8a9a5SSteve French * cifs_readpage_worker must be called with the page pinned 476138c8a9a5SSteve French */ 476238c8a9a5SSteve French static int cifs_readpage_worker(struct file *file, struct page *page, 476338c8a9a5SSteve French loff_t *poffset) 476438c8a9a5SSteve French { 476523171df5SJeff Layton struct inode *inode = file_inode(file); 476623171df5SJeff Layton struct timespec64 atime, mtime; 476738c8a9a5SSteve French char *read_data; 476838c8a9a5SSteve French int rc; 476938c8a9a5SSteve French 477038c8a9a5SSteve French /* Is the page cached? */ 477123171df5SJeff Layton rc = cifs_readpage_from_fscache(inode, page); 477238c8a9a5SSteve French if (rc == 0) 477338c8a9a5SSteve French goto read_complete; 477438c8a9a5SSteve French 477538c8a9a5SSteve French read_data = kmap(page); 477638c8a9a5SSteve French /* for reads over a certain size could initiate async read ahead */ 477738c8a9a5SSteve French 477838c8a9a5SSteve French rc = cifs_read(file, read_data, PAGE_SIZE, poffset); 477938c8a9a5SSteve French 478038c8a9a5SSteve French if (rc < 0) 478138c8a9a5SSteve French goto io_error; 478238c8a9a5SSteve French else 478338c8a9a5SSteve French cifs_dbg(FYI, "Bytes read %d\n", rc); 478438c8a9a5SSteve French 478538c8a9a5SSteve French /* we do not want atime to be less than mtime, it broke some apps */ 478623171df5SJeff Layton atime = inode_set_atime_to_ts(inode, current_time(inode)); 478723171df5SJeff Layton mtime = inode_get_mtime(inode); 47889a498744SZizhi Wo if (timespec64_compare(&atime, &mtime) < 0) 478923171df5SJeff Layton inode_set_atime_to_ts(inode, inode_get_mtime(inode)); 479038c8a9a5SSteve French 479138c8a9a5SSteve French if (PAGE_SIZE > rc) 479238c8a9a5SSteve French memset(read_data + rc, 0, PAGE_SIZE - rc); 479338c8a9a5SSteve French 479438c8a9a5SSteve French flush_dcache_page(page); 479538c8a9a5SSteve French SetPageUptodate(page); 479638c8a9a5SSteve French rc = 0; 479738c8a9a5SSteve French 479838c8a9a5SSteve French io_error: 479938c8a9a5SSteve French kunmap(page); 480038c8a9a5SSteve French 480138c8a9a5SSteve French read_complete: 480269513dd6SRussell Harmon via samba-technical unlock_page(page); 480338c8a9a5SSteve French return rc; 480438c8a9a5SSteve French } 480538c8a9a5SSteve French 480638c8a9a5SSteve French static int cifs_read_folio(struct file *file, struct folio *folio) 480738c8a9a5SSteve French { 480838c8a9a5SSteve French struct page *page = &folio->page; 480938c8a9a5SSteve French loff_t offset = page_file_offset(page); 481038c8a9a5SSteve French int rc = -EACCES; 481138c8a9a5SSteve French unsigned int xid; 481238c8a9a5SSteve French 481338c8a9a5SSteve French xid = get_xid(); 481438c8a9a5SSteve French 481538c8a9a5SSteve French if (file->private_data == NULL) { 481638c8a9a5SSteve French rc = -EBADF; 481738c8a9a5SSteve French free_xid(xid); 481838c8a9a5SSteve French return rc; 481938c8a9a5SSteve French } 482038c8a9a5SSteve French 482138c8a9a5SSteve French cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n", 482238c8a9a5SSteve French page, (int)offset, (int)offset); 482338c8a9a5SSteve French 482438c8a9a5SSteve French rc = cifs_readpage_worker(file, page, &offset); 482538c8a9a5SSteve French 482638c8a9a5SSteve French free_xid(xid); 482738c8a9a5SSteve French return rc; 482838c8a9a5SSteve French } 482938c8a9a5SSteve French 483038c8a9a5SSteve French static int is_inode_writable(struct cifsInodeInfo *cifs_inode) 483138c8a9a5SSteve French { 483238c8a9a5SSteve French struct cifsFileInfo *open_file; 483338c8a9a5SSteve French 483438c8a9a5SSteve French spin_lock(&cifs_inode->open_file_lock); 483538c8a9a5SSteve French list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { 483638c8a9a5SSteve French if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { 483738c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 483838c8a9a5SSteve French return 1; 483938c8a9a5SSteve French } 484038c8a9a5SSteve French } 484138c8a9a5SSteve French spin_unlock(&cifs_inode->open_file_lock); 484238c8a9a5SSteve French return 0; 484338c8a9a5SSteve French } 484438c8a9a5SSteve French 484538c8a9a5SSteve French /* We do not want to update the file size from server for inodes 484638c8a9a5SSteve French open for write - to avoid races with writepage extending 484738c8a9a5SSteve French the file - in the future we could consider allowing 484838c8a9a5SSteve French refreshing the inode only on increases in the file size 484938c8a9a5SSteve French but this is tricky to do without racing with writebehind 485038c8a9a5SSteve French page caching in the current Linux kernel design */ 48519179aa27SBharath SM bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file, 48529179aa27SBharath SM bool from_readdir) 485338c8a9a5SSteve French { 485438c8a9a5SSteve French if (!cifsInode) 485538c8a9a5SSteve French return true; 485638c8a9a5SSteve French 48579179aa27SBharath SM if (is_inode_writable(cifsInode) || 48589179aa27SBharath SM ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) { 485938c8a9a5SSteve French /* This inode is open for write at least once */ 486038c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 486138c8a9a5SSteve French 486238c8a9a5SSteve French cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb); 486338c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { 486438c8a9a5SSteve French /* since no page cache to corrupt on directio 486538c8a9a5SSteve French we can change size safely */ 486638c8a9a5SSteve French return true; 486738c8a9a5SSteve French } 486838c8a9a5SSteve French 486938c8a9a5SSteve French if (i_size_read(&cifsInode->netfs.inode) < end_of_file) 487038c8a9a5SSteve French return true; 487138c8a9a5SSteve French 487238c8a9a5SSteve French return false; 487338c8a9a5SSteve French } else 487438c8a9a5SSteve French return true; 487538c8a9a5SSteve French } 487638c8a9a5SSteve French 487738c8a9a5SSteve French static int cifs_write_begin(struct file *file, struct address_space *mapping, 487838c8a9a5SSteve French loff_t pos, unsigned len, 487938c8a9a5SSteve French struct page **pagep, void **fsdata) 488038c8a9a5SSteve French { 488138c8a9a5SSteve French int oncethru = 0; 488238c8a9a5SSteve French pgoff_t index = pos >> PAGE_SHIFT; 488338c8a9a5SSteve French loff_t offset = pos & (PAGE_SIZE - 1); 488438c8a9a5SSteve French loff_t page_start = pos & PAGE_MASK; 488538c8a9a5SSteve French loff_t i_size; 488638c8a9a5SSteve French struct page *page; 488738c8a9a5SSteve French int rc = 0; 488838c8a9a5SSteve French 488938c8a9a5SSteve French cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len); 489038c8a9a5SSteve French 489138c8a9a5SSteve French start: 489238c8a9a5SSteve French page = grab_cache_page_write_begin(mapping, index); 489338c8a9a5SSteve French if (!page) { 489438c8a9a5SSteve French rc = -ENOMEM; 489538c8a9a5SSteve French goto out; 489638c8a9a5SSteve French } 489738c8a9a5SSteve French 489838c8a9a5SSteve French if (PageUptodate(page)) 489938c8a9a5SSteve French goto out; 490038c8a9a5SSteve French 490138c8a9a5SSteve French /* 490238c8a9a5SSteve French * If we write a full page it will be up to date, no need to read from 490338c8a9a5SSteve French * the server. If the write is short, we'll end up doing a sync write 490438c8a9a5SSteve French * instead. 490538c8a9a5SSteve French */ 490638c8a9a5SSteve French if (len == PAGE_SIZE) 490738c8a9a5SSteve French goto out; 490838c8a9a5SSteve French 490938c8a9a5SSteve French /* 491038c8a9a5SSteve French * optimize away the read when we have an oplock, and we're not 491138c8a9a5SSteve French * expecting to use any of the data we'd be reading in. That 491238c8a9a5SSteve French * is, when the page lies beyond the EOF, or straddles the EOF 491338c8a9a5SSteve French * and the write will cover all of the existing data. 491438c8a9a5SSteve French */ 491538c8a9a5SSteve French if (CIFS_CACHE_READ(CIFS_I(mapping->host))) { 491638c8a9a5SSteve French i_size = i_size_read(mapping->host); 491738c8a9a5SSteve French if (page_start >= i_size || 491838c8a9a5SSteve French (offset == 0 && (pos + len) >= i_size)) { 491938c8a9a5SSteve French zero_user_segments(page, 0, offset, 492038c8a9a5SSteve French offset + len, 492138c8a9a5SSteve French PAGE_SIZE); 492238c8a9a5SSteve French /* 492338c8a9a5SSteve French * PageChecked means that the parts of the page 492438c8a9a5SSteve French * to which we're not writing are considered up 492538c8a9a5SSteve French * to date. Once the data is copied to the 492638c8a9a5SSteve French * page, it can be set uptodate. 492738c8a9a5SSteve French */ 492838c8a9a5SSteve French SetPageChecked(page); 492938c8a9a5SSteve French goto out; 493038c8a9a5SSteve French } 493138c8a9a5SSteve French } 493238c8a9a5SSteve French 493338c8a9a5SSteve French if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) { 493438c8a9a5SSteve French /* 493538c8a9a5SSteve French * might as well read a page, it is fast enough. If we get 493638c8a9a5SSteve French * an error, we don't need to return it. cifs_write_end will 493738c8a9a5SSteve French * do a sync write instead since PG_uptodate isn't set. 493838c8a9a5SSteve French */ 493938c8a9a5SSteve French cifs_readpage_worker(file, page, &page_start); 494038c8a9a5SSteve French put_page(page); 494138c8a9a5SSteve French oncethru = 1; 494238c8a9a5SSteve French goto start; 494338c8a9a5SSteve French } else { 494438c8a9a5SSteve French /* we could try using another file handle if there is one - 494538c8a9a5SSteve French but how would we lock it to prevent close of that handle 494638c8a9a5SSteve French racing with this read? In any case 494738c8a9a5SSteve French this will be written out by write_end so is fine */ 494838c8a9a5SSteve French } 494938c8a9a5SSteve French out: 495038c8a9a5SSteve French *pagep = page; 495138c8a9a5SSteve French return rc; 495238c8a9a5SSteve French } 495338c8a9a5SSteve French 495438c8a9a5SSteve French static bool cifs_release_folio(struct folio *folio, gfp_t gfp) 495538c8a9a5SSteve French { 495638c8a9a5SSteve French if (folio_test_private(folio)) 495738c8a9a5SSteve French return 0; 495838c8a9a5SSteve French if (folio_test_fscache(folio)) { 495938c8a9a5SSteve French if (current_is_kswapd() || !(gfp & __GFP_FS)) 496038c8a9a5SSteve French return false; 496138c8a9a5SSteve French folio_wait_fscache(folio); 496238c8a9a5SSteve French } 496338c8a9a5SSteve French fscache_note_page_release(cifs_inode_cookie(folio->mapping->host)); 496438c8a9a5SSteve French return true; 496538c8a9a5SSteve French } 496638c8a9a5SSteve French 496738c8a9a5SSteve French static void cifs_invalidate_folio(struct folio *folio, size_t offset, 496838c8a9a5SSteve French size_t length) 496938c8a9a5SSteve French { 497038c8a9a5SSteve French folio_wait_fscache(folio); 497138c8a9a5SSteve French } 497238c8a9a5SSteve French 497338c8a9a5SSteve French static int cifs_launder_folio(struct folio *folio) 497438c8a9a5SSteve French { 497538c8a9a5SSteve French int rc = 0; 497638c8a9a5SSteve French loff_t range_start = folio_pos(folio); 497738c8a9a5SSteve French loff_t range_end = range_start + folio_size(folio); 497838c8a9a5SSteve French struct writeback_control wbc = { 497938c8a9a5SSteve French .sync_mode = WB_SYNC_ALL, 498038c8a9a5SSteve French .nr_to_write = 0, 498138c8a9a5SSteve French .range_start = range_start, 498238c8a9a5SSteve French .range_end = range_end, 498338c8a9a5SSteve French }; 498438c8a9a5SSteve French 498538c8a9a5SSteve French cifs_dbg(FYI, "Launder page: %lu\n", folio->index); 498638c8a9a5SSteve French 498738c8a9a5SSteve French if (folio_clear_dirty_for_io(folio)) 498838c8a9a5SSteve French rc = cifs_writepage_locked(&folio->page, &wbc); 498938c8a9a5SSteve French 499038c8a9a5SSteve French folio_wait_fscache(folio); 499138c8a9a5SSteve French return rc; 499238c8a9a5SSteve French } 499338c8a9a5SSteve French 499438c8a9a5SSteve French void cifs_oplock_break(struct work_struct *work) 499538c8a9a5SSteve French { 499638c8a9a5SSteve French struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, 499738c8a9a5SSteve French oplock_break); 499838c8a9a5SSteve French struct inode *inode = d_inode(cfile->dentry); 4999e8f5f849SSteve French struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); 500038c8a9a5SSteve French struct cifsInodeInfo *cinode = CIFS_I(inode); 5001e8f5f849SSteve French struct cifs_tcon *tcon; 5002e8f5f849SSteve French struct TCP_Server_Info *server; 5003e8f5f849SSteve French struct tcon_link *tlink; 500438c8a9a5SSteve French int rc = 0; 500538c8a9a5SSteve French bool purge_cache = false, oplock_break_cancelled; 500638c8a9a5SSteve French __u64 persistent_fid, volatile_fid; 500738c8a9a5SSteve French __u16 net_fid; 500838c8a9a5SSteve French 500938c8a9a5SSteve French wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS, 501038c8a9a5SSteve French TASK_UNINTERRUPTIBLE); 501138c8a9a5SSteve French 5012e8f5f849SSteve French tlink = cifs_sb_tlink(cifs_sb); 5013e8f5f849SSteve French if (IS_ERR(tlink)) 5014e8f5f849SSteve French goto out; 5015e8f5f849SSteve French tcon = tlink_tcon(tlink); 5016e8f5f849SSteve French server = tcon->ses->server; 5017e8f5f849SSteve French 501838c8a9a5SSteve French server->ops->downgrade_oplock(server, cinode, cfile->oplock_level, 501938c8a9a5SSteve French cfile->oplock_epoch, &purge_cache); 502038c8a9a5SSteve French 502138c8a9a5SSteve French if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) && 502238c8a9a5SSteve French cifs_has_mand_locks(cinode)) { 502338c8a9a5SSteve French cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n", 502438c8a9a5SSteve French inode); 502538c8a9a5SSteve French cinode->oplock = 0; 502638c8a9a5SSteve French } 502738c8a9a5SSteve French 502838c8a9a5SSteve French if (inode && S_ISREG(inode->i_mode)) { 502938c8a9a5SSteve French if (CIFS_CACHE_READ(cinode)) 503038c8a9a5SSteve French break_lease(inode, O_RDONLY); 503138c8a9a5SSteve French else 503238c8a9a5SSteve French break_lease(inode, O_WRONLY); 503338c8a9a5SSteve French rc = filemap_fdatawrite(inode->i_mapping); 503438c8a9a5SSteve French if (!CIFS_CACHE_READ(cinode) || purge_cache) { 503538c8a9a5SSteve French rc = filemap_fdatawait(inode->i_mapping); 503638c8a9a5SSteve French mapping_set_error(inode->i_mapping, rc); 503738c8a9a5SSteve French cifs_zap_mapping(inode); 503838c8a9a5SSteve French } 503938c8a9a5SSteve French cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc); 504038c8a9a5SSteve French if (CIFS_CACHE_WRITE(cinode)) 504138c8a9a5SSteve French goto oplock_break_ack; 504238c8a9a5SSteve French } 504338c8a9a5SSteve French 504438c8a9a5SSteve French rc = cifs_push_locks(cfile); 504538c8a9a5SSteve French if (rc) 504638c8a9a5SSteve French cifs_dbg(VFS, "Push locks rc = %d\n", rc); 504738c8a9a5SSteve French 504838c8a9a5SSteve French oplock_break_ack: 504938c8a9a5SSteve French /* 505038c8a9a5SSteve French * When oplock break is received and there are no active 505138c8a9a5SSteve French * file handles but cached, then schedule deferred close immediately. 505238c8a9a5SSteve French * So, new open will not use cached handle. 505338c8a9a5SSteve French */ 505438c8a9a5SSteve French 505538c8a9a5SSteve French if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes)) 505638c8a9a5SSteve French cifs_close_deferred_file(cinode); 505738c8a9a5SSteve French 505838c8a9a5SSteve French persistent_fid = cfile->fid.persistent_fid; 505938c8a9a5SSteve French volatile_fid = cfile->fid.volatile_fid; 506038c8a9a5SSteve French net_fid = cfile->fid.netfid; 506138c8a9a5SSteve French oplock_break_cancelled = cfile->oplock_break_cancelled; 506238c8a9a5SSteve French 506338c8a9a5SSteve French _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false); 506438c8a9a5SSteve French /* 5065da787d5bSBharath SM * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require 5066da787d5bSBharath SM * an acknowledgment to be sent when the file has already been closed. 506738c8a9a5SSteve French */ 5068da787d5bSBharath SM spin_lock(&cinode->open_file_lock); 5069e8f5f849SSteve French /* check list empty since can race with kill_sb calling tree disconnect */ 5070e8f5f849SSteve French if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) { 5071da787d5bSBharath SM spin_unlock(&cinode->open_file_lock); 5072e8f5f849SSteve French rc = server->ops->oplock_response(tcon, persistent_fid, 507338c8a9a5SSteve French volatile_fid, net_fid, cinode); 507438c8a9a5SSteve French cifs_dbg(FYI, "Oplock release rc = %d\n", rc); 5075c774e677SSteve French } else 5076da787d5bSBharath SM spin_unlock(&cinode->open_file_lock); 507738c8a9a5SSteve French 5078e8f5f849SSteve French cifs_put_tlink(tlink); 5079e8f5f849SSteve French out: 508038c8a9a5SSteve French cifs_done_oplock_break(cinode); 508138c8a9a5SSteve French } 508238c8a9a5SSteve French 508338c8a9a5SSteve French /* 508438c8a9a5SSteve French * The presence of cifs_direct_io() in the address space ops vector 508538c8a9a5SSteve French * allowes open() O_DIRECT flags which would have failed otherwise. 508638c8a9a5SSteve French * 508738c8a9a5SSteve French * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests 508838c8a9a5SSteve French * so this method should never be called. 508938c8a9a5SSteve French * 509038c8a9a5SSteve French * Direct IO is not yet supported in the cached mode. 509138c8a9a5SSteve French */ 509238c8a9a5SSteve French static ssize_t 509338c8a9a5SSteve French cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter) 509438c8a9a5SSteve French { 509538c8a9a5SSteve French /* 509638c8a9a5SSteve French * FIXME 509738c8a9a5SSteve French * Eventually need to support direct IO for non forcedirectio mounts 509838c8a9a5SSteve French */ 509938c8a9a5SSteve French return -EINVAL; 510038c8a9a5SSteve French } 510138c8a9a5SSteve French 510238c8a9a5SSteve French static int cifs_swap_activate(struct swap_info_struct *sis, 510338c8a9a5SSteve French struct file *swap_file, sector_t *span) 510438c8a9a5SSteve French { 510538c8a9a5SSteve French struct cifsFileInfo *cfile = swap_file->private_data; 510638c8a9a5SSteve French struct inode *inode = swap_file->f_mapping->host; 510738c8a9a5SSteve French unsigned long blocks; 510838c8a9a5SSteve French long long isize; 510938c8a9a5SSteve French 511038c8a9a5SSteve French cifs_dbg(FYI, "swap activate\n"); 511138c8a9a5SSteve French 511238c8a9a5SSteve French if (!swap_file->f_mapping->a_ops->swap_rw) 511338c8a9a5SSteve French /* Cannot support swap */ 511438c8a9a5SSteve French return -EINVAL; 511538c8a9a5SSteve French 511638c8a9a5SSteve French spin_lock(&inode->i_lock); 511738c8a9a5SSteve French blocks = inode->i_blocks; 511838c8a9a5SSteve French isize = inode->i_size; 511938c8a9a5SSteve French spin_unlock(&inode->i_lock); 512038c8a9a5SSteve French if (blocks*512 < isize) { 512138c8a9a5SSteve French pr_warn("swap activate: swapfile has holes\n"); 512238c8a9a5SSteve French return -EINVAL; 512338c8a9a5SSteve French } 512438c8a9a5SSteve French *span = sis->pages; 512538c8a9a5SSteve French 512638c8a9a5SSteve French pr_warn_once("Swap support over SMB3 is experimental\n"); 512738c8a9a5SSteve French 512838c8a9a5SSteve French /* 512938c8a9a5SSteve French * TODO: consider adding ACL (or documenting how) to prevent other 513038c8a9a5SSteve French * users (on this or other systems) from reading it 513138c8a9a5SSteve French */ 513238c8a9a5SSteve French 513338c8a9a5SSteve French 513438c8a9a5SSteve French /* TODO: add sk_set_memalloc(inet) or similar */ 513538c8a9a5SSteve French 513638c8a9a5SSteve French if (cfile) 513738c8a9a5SSteve French cfile->swapfile = true; 513838c8a9a5SSteve French /* 513938c8a9a5SSteve French * TODO: Since file already open, we can't open with DENY_ALL here 514038c8a9a5SSteve French * but we could add call to grab a byte range lock to prevent others 514138c8a9a5SSteve French * from reading or writing the file 514238c8a9a5SSteve French */ 514338c8a9a5SSteve French 514438c8a9a5SSteve French sis->flags |= SWP_FS_OPS; 514538c8a9a5SSteve French return add_swap_extent(sis, 0, sis->max, 0); 514638c8a9a5SSteve French } 514738c8a9a5SSteve French 514838c8a9a5SSteve French static void cifs_swap_deactivate(struct file *file) 514938c8a9a5SSteve French { 515038c8a9a5SSteve French struct cifsFileInfo *cfile = file->private_data; 515138c8a9a5SSteve French 515238c8a9a5SSteve French cifs_dbg(FYI, "swap deactivate\n"); 515338c8a9a5SSteve French 515438c8a9a5SSteve French /* TODO: undo sk_set_memalloc(inet) will eventually be needed */ 515538c8a9a5SSteve French 515638c8a9a5SSteve French if (cfile) 515738c8a9a5SSteve French cfile->swapfile = false; 515838c8a9a5SSteve French 515938c8a9a5SSteve French /* do we need to unpin (or unlock) the file */ 516038c8a9a5SSteve French } 516138c8a9a5SSteve French 516238c8a9a5SSteve French /* 516338c8a9a5SSteve French * Mark a page as having been made dirty and thus needing writeback. We also 516438c8a9a5SSteve French * need to pin the cache object to write back to. 516538c8a9a5SSteve French */ 516638c8a9a5SSteve French #ifdef CONFIG_CIFS_FSCACHE 516738c8a9a5SSteve French static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio) 516838c8a9a5SSteve French { 516938c8a9a5SSteve French return fscache_dirty_folio(mapping, folio, 517038c8a9a5SSteve French cifs_inode_cookie(mapping->host)); 517138c8a9a5SSteve French } 517238c8a9a5SSteve French #else 517338c8a9a5SSteve French #define cifs_dirty_folio filemap_dirty_folio 517438c8a9a5SSteve French #endif 517538c8a9a5SSteve French 517638c8a9a5SSteve French const struct address_space_operations cifs_addr_ops = { 517738c8a9a5SSteve French .read_folio = cifs_read_folio, 517838c8a9a5SSteve French .readahead = cifs_readahead, 517938c8a9a5SSteve French .writepages = cifs_writepages, 518038c8a9a5SSteve French .write_begin = cifs_write_begin, 518138c8a9a5SSteve French .write_end = cifs_write_end, 518238c8a9a5SSteve French .dirty_folio = cifs_dirty_folio, 518338c8a9a5SSteve French .release_folio = cifs_release_folio, 518438c8a9a5SSteve French .direct_IO = cifs_direct_io, 518538c8a9a5SSteve French .invalidate_folio = cifs_invalidate_folio, 518638c8a9a5SSteve French .launder_folio = cifs_launder_folio, 518738c8a9a5SSteve French .migrate_folio = filemap_migrate_folio, 518838c8a9a5SSteve French /* 518938c8a9a5SSteve French * TODO: investigate and if useful we could add an is_dirty_writeback 519038c8a9a5SSteve French * helper if needed 519138c8a9a5SSteve French */ 519238c8a9a5SSteve French .swap_activate = cifs_swap_activate, 519338c8a9a5SSteve French .swap_deactivate = cifs_swap_deactivate, 519438c8a9a5SSteve French }; 519538c8a9a5SSteve French 519638c8a9a5SSteve French /* 519738c8a9a5SSteve French * cifs_readahead requires the server to support a buffer large enough to 519838c8a9a5SSteve French * contain the header plus one complete page of data. Otherwise, we need 519938c8a9a5SSteve French * to leave cifs_readahead out of the address space operations. 520038c8a9a5SSteve French */ 520138c8a9a5SSteve French const struct address_space_operations cifs_addr_ops_smallbuf = { 520238c8a9a5SSteve French .read_folio = cifs_read_folio, 520338c8a9a5SSteve French .writepages = cifs_writepages, 520438c8a9a5SSteve French .write_begin = cifs_write_begin, 520538c8a9a5SSteve French .write_end = cifs_write_end, 520638c8a9a5SSteve French .dirty_folio = cifs_dirty_folio, 520738c8a9a5SSteve French .release_folio = cifs_release_folio, 520838c8a9a5SSteve French .invalidate_folio = cifs_invalidate_folio, 520938c8a9a5SSteve French .launder_folio = cifs_launder_folio, 521038c8a9a5SSteve French .migrate_folio = filemap_migrate_folio, 521138c8a9a5SSteve French }; 5212