1 // SPDX-License-Identifier: LGPL-2.1
4 * vfs operations that deal with files
6 * Copyright (C) International Business Machines Corp., 2002,2010
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * Jeremy Allison (jra@samba.org)
12 #include <linux/filelock.h>
13 #include <linux/backing-dev.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/pagemap.h>
17 #include <linux/pagevec.h>
18 #include <linux/writeback.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include <linux/delay.h>
21 #include <linux/mount.h>
22 #include <linux/slab.h>
23 #include <linux/swap.h>
25 #include <asm/div64.h>
29 #include "cifsproto.h"
30 #include "smb2proto.h"
31 #include "cifs_unicode.h"
32 #include "cifs_debug.h"
33 #include "cifs_fs_sb.h"
35 #include "smbdirect.h"
36 #include "fs_context.h"
37 #include "cifs_ioctl.h"
38 #include "cached_dir.h"
41 * Remove the dirty flags from a span of pages.
43 static void cifs_undirty_folios(struct inode *inode, loff_t start, unsigned int len)
45 struct address_space *mapping = inode->i_mapping;
49 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
53 end = (start + len - 1) / PAGE_SIZE;
54 xas_for_each_marked(&xas, folio, end, PAGECACHE_TAG_DIRTY) {
58 folio_clear_dirty_for_io(folio);
67 * Completion of write to server.
69 void cifs_pages_written_back(struct inode *inode, loff_t start, unsigned int len)
71 struct address_space *mapping = inode->i_mapping;
75 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
82 end = (start + len - 1) / PAGE_SIZE;
83 xas_for_each(&xas, folio, end) {
84 if (!folio_test_writeback(folio)) {
85 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
86 len, start, folio_index(folio), end);
90 folio_detach_private(folio);
91 folio_end_writeback(folio);
98 * Failure of write to server.
100 void cifs_pages_write_failed(struct inode *inode, loff_t start, unsigned int len)
102 struct address_space *mapping = inode->i_mapping;
106 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
113 end = (start + len - 1) / PAGE_SIZE;
114 xas_for_each(&xas, folio, end) {
115 if (!folio_test_writeback(folio)) {
116 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
117 len, start, folio_index(folio), end);
121 folio_set_error(folio);
122 folio_end_writeback(folio);
129 * Redirty pages after a temporary failure.
131 void cifs_pages_write_redirty(struct inode *inode, loff_t start, unsigned int len)
133 struct address_space *mapping = inode->i_mapping;
137 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
144 end = (start + len - 1) / PAGE_SIZE;
145 xas_for_each(&xas, folio, end) {
146 if (!folio_test_writeback(folio)) {
147 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
148 len, start, folio_index(folio), end);
152 filemap_dirty_folio(folio->mapping, folio);
153 folio_end_writeback(folio);
160 * Mark as invalid, all open files on tree connections since they
161 * were closed when session to server was lost.
164 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
166 struct cifsFileInfo *open_file = NULL;
167 struct list_head *tmp;
168 struct list_head *tmp1;
170 /* only send once per connect */
171 spin_lock(&tcon->ses->ses_lock);
172 if ((tcon->ses->ses_status != SES_GOOD) || (tcon->status != TID_NEED_RECON)) {
173 spin_unlock(&tcon->ses->ses_lock);
176 tcon->status = TID_IN_FILES_INVALIDATE;
177 spin_unlock(&tcon->ses->ses_lock);
179 /* list all files open on tree connection and mark them invalid */
180 spin_lock(&tcon->open_file_lock);
181 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
182 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
183 open_file->invalidHandle = true;
184 open_file->oplock_break_cancelled = true;
186 spin_unlock(&tcon->open_file_lock);
188 invalidate_all_cached_dirs(tcon);
189 spin_lock(&tcon->tc_lock);
190 if (tcon->status == TID_IN_FILES_INVALIDATE)
191 tcon->status = TID_NEED_TCON;
192 spin_unlock(&tcon->tc_lock);
195 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
200 static inline int cifs_convert_flags(unsigned int flags)
202 if ((flags & O_ACCMODE) == O_RDONLY)
204 else if ((flags & O_ACCMODE) == O_WRONLY)
205 return GENERIC_WRITE;
206 else if ((flags & O_ACCMODE) == O_RDWR) {
207 /* GENERIC_ALL is too much permission to request
208 can cause unnecessary access denied on create */
209 /* return GENERIC_ALL; */
210 return (GENERIC_READ | GENERIC_WRITE);
213 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
214 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
218 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
219 static u32 cifs_posix_convert_flags(unsigned int flags)
223 if ((flags & O_ACCMODE) == O_RDONLY)
224 posix_flags = SMB_O_RDONLY;
225 else if ((flags & O_ACCMODE) == O_WRONLY)
226 posix_flags = SMB_O_WRONLY;
227 else if ((flags & O_ACCMODE) == O_RDWR)
228 posix_flags = SMB_O_RDWR;
230 if (flags & O_CREAT) {
231 posix_flags |= SMB_O_CREAT;
233 posix_flags |= SMB_O_EXCL;
234 } else if (flags & O_EXCL)
235 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
236 current->comm, current->tgid);
239 posix_flags |= SMB_O_TRUNC;
240 /* be safe and imply O_SYNC for O_DSYNC */
242 posix_flags |= SMB_O_SYNC;
243 if (flags & O_DIRECTORY)
244 posix_flags |= SMB_O_DIRECTORY;
245 if (flags & O_NOFOLLOW)
246 posix_flags |= SMB_O_NOFOLLOW;
247 if (flags & O_DIRECT)
248 posix_flags |= SMB_O_DIRECT;
252 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
254 static inline int cifs_get_disposition(unsigned int flags)
256 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
258 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
259 return FILE_OVERWRITE_IF;
260 else if ((flags & O_CREAT) == O_CREAT)
262 else if ((flags & O_TRUNC) == O_TRUNC)
263 return FILE_OVERWRITE;
268 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
269 int cifs_posix_open(const char *full_path, struct inode **pinode,
270 struct super_block *sb, int mode, unsigned int f_flags,
271 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
274 FILE_UNIX_BASIC_INFO *presp_data;
275 __u32 posix_flags = 0;
276 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
277 struct cifs_fattr fattr;
278 struct tcon_link *tlink;
279 struct cifs_tcon *tcon;
281 cifs_dbg(FYI, "posix open %s\n", full_path);
283 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
284 if (presp_data == NULL)
287 tlink = cifs_sb_tlink(cifs_sb);
293 tcon = tlink_tcon(tlink);
294 mode &= ~current_umask();
296 posix_flags = cifs_posix_convert_flags(f_flags);
297 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
298 poplock, full_path, cifs_sb->local_nls,
299 cifs_remap(cifs_sb));
300 cifs_put_tlink(tlink);
305 if (presp_data->Type == cpu_to_le32(-1))
306 goto posix_open_ret; /* open ok, caller does qpathinfo */
309 goto posix_open_ret; /* caller does not need info */
311 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
313 /* get new inode and set it up */
314 if (*pinode == NULL) {
315 cifs_fill_uniqueid(sb, &fattr);
316 *pinode = cifs_iget(sb, &fattr);
322 cifs_revalidate_mapping(*pinode);
323 rc = cifs_fattr_to_inode(*pinode, &fattr);
330 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
332 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
333 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
334 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
339 int create_options = CREATE_NOT_DIR;
340 struct TCP_Server_Info *server = tcon->ses->server;
341 struct cifs_open_parms oparms;
343 if (!server->ops->open)
346 desired_access = cifs_convert_flags(f_flags);
348 /*********************************************************************
349 * open flag mapping table:
351 * POSIX Flag CIFS Disposition
352 * ---------- ----------------
353 * O_CREAT FILE_OPEN_IF
354 * O_CREAT | O_EXCL FILE_CREATE
355 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
356 * O_TRUNC FILE_OVERWRITE
357 * none of the above FILE_OPEN
359 * Note that there is not a direct match between disposition
360 * FILE_SUPERSEDE (ie create whether or not file exists although
361 * O_CREAT | O_TRUNC is similar but truncates the existing
362 * file rather than creating a new file as FILE_SUPERSEDE does
363 * (which uses the attributes / metadata passed in on open call)
365 *? O_SYNC is a reasonable match to CIFS writethrough flag
366 *? and the read write flags match reasonably. O_LARGEFILE
367 *? is irrelevant because largefile support is always used
368 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
369 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
370 *********************************************************************/
372 disposition = cifs_get_disposition(f_flags);
374 /* BB pass O_SYNC flag through on file attributes .. BB */
376 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
377 if (f_flags & O_SYNC)
378 create_options |= CREATE_WRITE_THROUGH;
380 if (f_flags & O_DIRECT)
381 create_options |= CREATE_NO_BUFFER;
383 oparms = (struct cifs_open_parms) {
386 .desired_access = desired_access,
387 .create_options = cifs_create_options(cifs_sb, create_options),
388 .disposition = disposition,
393 rc = server->ops->open(xid, &oparms, oplock, buf);
397 /* TODO: Add support for calling posix query info but with passing in fid */
399 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
402 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
406 server->ops->close(xid, tcon, fid);
415 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
417 struct cifs_fid_locks *cur;
418 bool has_locks = false;
420 down_read(&cinode->lock_sem);
421 list_for_each_entry(cur, &cinode->llist, llist) {
422 if (!list_empty(&cur->locks)) {
427 up_read(&cinode->lock_sem);
432 cifs_down_write(struct rw_semaphore *sem)
434 while (!down_write_trylock(sem))
438 static void cifsFileInfo_put_work(struct work_struct *work);
440 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
441 struct tcon_link *tlink, __u32 oplock,
442 const char *symlink_target)
444 struct dentry *dentry = file_dentry(file);
445 struct inode *inode = d_inode(dentry);
446 struct cifsInodeInfo *cinode = CIFS_I(inode);
447 struct cifsFileInfo *cfile;
448 struct cifs_fid_locks *fdlocks;
449 struct cifs_tcon *tcon = tlink_tcon(tlink);
450 struct TCP_Server_Info *server = tcon->ses->server;
452 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
456 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
462 if (symlink_target) {
463 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
464 if (!cfile->symlink_target) {
471 INIT_LIST_HEAD(&fdlocks->locks);
472 fdlocks->cfile = cfile;
473 cfile->llist = fdlocks;
476 cfile->pid = current->tgid;
477 cfile->uid = current_fsuid();
478 cfile->dentry = dget(dentry);
479 cfile->f_flags = file->f_flags;
480 cfile->invalidHandle = false;
481 cfile->deferred_close_scheduled = false;
482 cfile->tlink = cifs_get_tlink(tlink);
483 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
484 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
485 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
486 mutex_init(&cfile->fh_mutex);
487 spin_lock_init(&cfile->file_info_lock);
489 cifs_sb_active(inode->i_sb);
492 * If the server returned a read oplock and we have mandatory brlocks,
493 * set oplock level to None.
495 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
496 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
500 cifs_down_write(&cinode->lock_sem);
501 list_add(&fdlocks->llist, &cinode->llist);
502 up_write(&cinode->lock_sem);
504 spin_lock(&tcon->open_file_lock);
505 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
506 oplock = fid->pending_open->oplock;
507 list_del(&fid->pending_open->olist);
509 fid->purge_cache = false;
510 server->ops->set_fid(cfile, fid, oplock);
512 list_add(&cfile->tlist, &tcon->openFileList);
513 atomic_inc(&tcon->num_local_opens);
515 /* if readable file instance put first in list*/
516 spin_lock(&cinode->open_file_lock);
517 if (file->f_mode & FMODE_READ)
518 list_add(&cfile->flist, &cinode->openFileList);
520 list_add_tail(&cfile->flist, &cinode->openFileList);
521 spin_unlock(&cinode->open_file_lock);
522 spin_unlock(&tcon->open_file_lock);
524 if (fid->purge_cache)
525 cifs_zap_mapping(inode);
527 file->private_data = cfile;
531 struct cifsFileInfo *
532 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
534 spin_lock(&cifs_file->file_info_lock);
535 cifsFileInfo_get_locked(cifs_file);
536 spin_unlock(&cifs_file->file_info_lock);
540 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
542 struct inode *inode = d_inode(cifs_file->dentry);
543 struct cifsInodeInfo *cifsi = CIFS_I(inode);
544 struct cifsLockInfo *li, *tmp;
545 struct super_block *sb = inode->i_sb;
548 * Delete any outstanding lock records. We'll lose them when the file
551 cifs_down_write(&cifsi->lock_sem);
552 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
553 list_del(&li->llist);
554 cifs_del_lock_waiters(li);
557 list_del(&cifs_file->llist->llist);
558 kfree(cifs_file->llist);
559 up_write(&cifsi->lock_sem);
561 cifs_put_tlink(cifs_file->tlink);
562 dput(cifs_file->dentry);
563 cifs_sb_deactive(sb);
564 kfree(cifs_file->symlink_target);
568 static void cifsFileInfo_put_work(struct work_struct *work)
570 struct cifsFileInfo *cifs_file = container_of(work,
571 struct cifsFileInfo, put);
573 cifsFileInfo_put_final(cifs_file);
577 * cifsFileInfo_put - release a reference of file priv data
579 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
581 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
583 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
585 _cifsFileInfo_put(cifs_file, true, true);
589 * _cifsFileInfo_put - release a reference of file priv data
591 * This may involve closing the filehandle @cifs_file out on the
592 * server. Must be called without holding tcon->open_file_lock,
593 * cinode->open_file_lock and cifs_file->file_info_lock.
595 * If @wait_for_oplock_handler is true and we are releasing the last
596 * reference, wait for any running oplock break handler of the file
597 * and cancel any pending one.
599 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
600 * @wait_oplock_handler: must be false if called from oplock_break_handler
601 * @offload: not offloaded on close and oplock breaks
604 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
605 bool wait_oplock_handler, bool offload)
607 struct inode *inode = d_inode(cifs_file->dentry);
608 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
609 struct TCP_Server_Info *server = tcon->ses->server;
610 struct cifsInodeInfo *cifsi = CIFS_I(inode);
611 struct super_block *sb = inode->i_sb;
612 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
613 struct cifs_fid fid = {};
614 struct cifs_pending_open open;
615 bool oplock_break_cancelled;
617 spin_lock(&tcon->open_file_lock);
618 spin_lock(&cifsi->open_file_lock);
619 spin_lock(&cifs_file->file_info_lock);
620 if (--cifs_file->count > 0) {
621 spin_unlock(&cifs_file->file_info_lock);
622 spin_unlock(&cifsi->open_file_lock);
623 spin_unlock(&tcon->open_file_lock);
626 spin_unlock(&cifs_file->file_info_lock);
628 if (server->ops->get_lease_key)
629 server->ops->get_lease_key(inode, &fid);
631 /* store open in pending opens to make sure we don't miss lease break */
632 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
634 /* remove it from the lists */
635 list_del(&cifs_file->flist);
636 list_del(&cifs_file->tlist);
637 atomic_dec(&tcon->num_local_opens);
639 if (list_empty(&cifsi->openFileList)) {
640 cifs_dbg(FYI, "closing last open instance for inode %p\n",
641 d_inode(cifs_file->dentry));
643 * In strict cache mode we need invalidate mapping on the last
644 * close because it may cause a error when we open this file
645 * again and get at least level II oplock.
647 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
648 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
649 cifs_set_oplock_level(cifsi, 0);
652 spin_unlock(&cifsi->open_file_lock);
653 spin_unlock(&tcon->open_file_lock);
655 oplock_break_cancelled = wait_oplock_handler ?
656 cancel_work_sync(&cifs_file->oplock_break) : false;
658 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
659 struct TCP_Server_Info *server = tcon->ses->server;
663 if (server->ops->close_getattr)
664 server->ops->close_getattr(xid, tcon, cifs_file);
665 else if (server->ops->close)
666 server->ops->close(xid, tcon, &cifs_file->fid);
670 if (oplock_break_cancelled)
671 cifs_done_oplock_break(cifsi);
673 cifs_del_pending_open(&open);
676 queue_work(fileinfo_put_wq, &cifs_file->put);
678 cifsFileInfo_put_final(cifs_file);
681 int cifs_open(struct inode *inode, struct file *file)
687 struct cifs_sb_info *cifs_sb;
688 struct TCP_Server_Info *server;
689 struct cifs_tcon *tcon;
690 struct tcon_link *tlink;
691 struct cifsFileInfo *cfile = NULL;
693 const char *full_path;
694 bool posix_open_ok = false;
695 struct cifs_fid fid = {};
696 struct cifs_pending_open open;
697 struct cifs_open_info_data data = {};
701 cifs_sb = CIFS_SB(inode->i_sb);
702 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
707 tlink = cifs_sb_tlink(cifs_sb);
710 return PTR_ERR(tlink);
712 tcon = tlink_tcon(tlink);
713 server = tcon->ses->server;
715 page = alloc_dentry_path();
716 full_path = build_path_from_dentry(file_dentry(file), page);
717 if (IS_ERR(full_path)) {
718 rc = PTR_ERR(full_path);
722 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
723 inode, file->f_flags, full_path);
725 if (file->f_flags & O_DIRECT &&
726 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
727 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
728 file->f_op = &cifs_file_direct_nobrl_ops;
730 file->f_op = &cifs_file_direct_ops;
733 /* Get the cached handle as SMB2 close is deferred */
734 rc = cifs_get_readable_path(tcon, full_path, &cfile);
736 if (file->f_flags == cfile->f_flags) {
737 file->private_data = cfile;
738 spin_lock(&CIFS_I(inode)->deferred_lock);
739 cifs_del_deferred_close(cfile);
740 spin_unlock(&CIFS_I(inode)->deferred_lock);
743 _cifsFileInfo_put(cfile, true, false);
752 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
753 if (!tcon->broken_posix_open && tcon->unix_ext &&
754 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
755 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
756 /* can not refresh inode info since size could be stale */
757 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
758 cifs_sb->ctx->file_mode /* ignored */,
759 file->f_flags, &oplock, &fid.netfid, xid);
761 cifs_dbg(FYI, "posix open succeeded\n");
762 posix_open_ok = true;
763 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
764 if (tcon->ses->serverNOS)
765 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",
767 tcon->ses->serverNOS);
768 tcon->broken_posix_open = true;
769 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
770 (rc != -EOPNOTSUPP)) /* path not found or net err */
773 * Else fallthrough to retry open the old way on network i/o
777 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
779 if (server->ops->get_lease_key)
780 server->ops->get_lease_key(inode, &fid);
782 cifs_add_pending_open(&fid, tlink, &open);
784 if (!posix_open_ok) {
785 if (server->ops->get_lease_key)
786 server->ops->get_lease_key(inode, &fid);
788 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
791 cifs_del_pending_open(&open);
796 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
798 if (server->ops->close)
799 server->ops->close(xid, tcon, &fid);
800 cifs_del_pending_open(&open);
805 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
806 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
808 * Time to set mode which we can not set earlier due to
809 * problems creating new read-only files.
811 struct cifs_unix_set_info_args args = {
812 .mode = inode->i_mode,
813 .uid = INVALID_UID, /* no change */
814 .gid = INVALID_GID, /* no change */
815 .ctime = NO_CHANGE_64,
816 .atime = NO_CHANGE_64,
817 .mtime = NO_CHANGE_64,
820 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
823 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
826 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
827 file->f_mode & FMODE_WRITE);
828 if (file->f_flags & O_DIRECT &&
829 (!((file->f_flags & O_ACCMODE) != O_RDONLY) ||
830 file->f_flags & O_APPEND))
831 cifs_invalidate_cache(file_inode(file),
832 FSCACHE_INVAL_DIO_WRITE);
835 free_dentry_path(page);
837 cifs_put_tlink(tlink);
838 cifs_free_open_info(&data);
842 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
843 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
844 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
847 * Try to reacquire byte range locks that were released when session
848 * to server was lost.
851 cifs_relock_file(struct cifsFileInfo *cfile)
853 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
854 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
856 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
857 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
858 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
860 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
861 if (cinode->can_cache_brlcks) {
862 /* can cache locks - no need to relock */
863 up_read(&cinode->lock_sem);
867 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
868 if (cap_unix(tcon->ses) &&
869 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
870 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
871 rc = cifs_push_posix_locks(cfile);
873 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
874 rc = tcon->ses->server->ops->push_mand_locks(cfile);
876 up_read(&cinode->lock_sem);
881 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
886 struct cifs_sb_info *cifs_sb;
887 struct cifs_tcon *tcon;
888 struct TCP_Server_Info *server;
889 struct cifsInodeInfo *cinode;
892 const char *full_path;
894 int disposition = FILE_OPEN;
895 int create_options = CREATE_NOT_DIR;
896 struct cifs_open_parms oparms;
899 mutex_lock(&cfile->fh_mutex);
900 if (!cfile->invalidHandle) {
901 mutex_unlock(&cfile->fh_mutex);
906 inode = d_inode(cfile->dentry);
907 cifs_sb = CIFS_SB(inode->i_sb);
908 tcon = tlink_tcon(cfile->tlink);
909 server = tcon->ses->server;
912 * Can not grab rename sem here because various ops, including those
913 * that already have the rename sem can end up causing writepage to get
914 * called and if the server was down that means we end up here, and we
915 * can never tell if the caller already has the rename_sem.
917 page = alloc_dentry_path();
918 full_path = build_path_from_dentry(cfile->dentry, page);
919 if (IS_ERR(full_path)) {
920 mutex_unlock(&cfile->fh_mutex);
921 free_dentry_path(page);
923 return PTR_ERR(full_path);
926 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
927 inode, cfile->f_flags, full_path);
929 if (tcon->ses->server->oplocks)
934 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
935 if (tcon->unix_ext && cap_unix(tcon->ses) &&
936 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
937 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
939 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
940 * original open. Must mask them off for a reopen.
942 unsigned int oflags = cfile->f_flags &
943 ~(O_CREAT | O_EXCL | O_TRUNC);
945 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
946 cifs_sb->ctx->file_mode /* ignored */,
947 oflags, &oplock, &cfile->fid.netfid, xid);
949 cifs_dbg(FYI, "posix reopen succeeded\n");
950 oparms.reconnect = true;
954 * fallthrough to retry open the old way on errors, especially
955 * in the reconnect path it is important to retry hard
958 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
960 desired_access = cifs_convert_flags(cfile->f_flags);
962 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
963 if (cfile->f_flags & O_SYNC)
964 create_options |= CREATE_WRITE_THROUGH;
966 if (cfile->f_flags & O_DIRECT)
967 create_options |= CREATE_NO_BUFFER;
969 if (server->ops->get_lease_key)
970 server->ops->get_lease_key(inode, &cfile->fid);
972 oparms = (struct cifs_open_parms) {
975 .desired_access = desired_access,
976 .create_options = cifs_create_options(cifs_sb, create_options),
977 .disposition = disposition,
984 * Can not refresh inode by passing in file_info buf to be returned by
985 * ops->open and then calling get_inode_info with returned buf since
986 * file might have write behind data that needs to be flushed and server
987 * version of file size can be stale. If we knew for sure that inode was
988 * not dirty locally we could do this.
990 rc = server->ops->open(xid, &oparms, &oplock, NULL);
991 if (rc == -ENOENT && oparms.reconnect == false) {
992 /* durable handle timeout is expired - open the file again */
993 rc = server->ops->open(xid, &oparms, &oplock, NULL);
994 /* indicate that we need to relock the file */
995 oparms.reconnect = true;
999 mutex_unlock(&cfile->fh_mutex);
1000 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
1001 cifs_dbg(FYI, "oplock: %d\n", oplock);
1002 goto reopen_error_exit;
1005 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1007 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1008 cfile->invalidHandle = false;
1009 mutex_unlock(&cfile->fh_mutex);
1010 cinode = CIFS_I(inode);
1013 rc = filemap_write_and_wait(inode->i_mapping);
1014 if (!is_interrupt_error(rc))
1015 mapping_set_error(inode->i_mapping, rc);
1017 if (tcon->posix_extensions)
1018 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
1019 else if (tcon->unix_ext)
1020 rc = cifs_get_inode_info_unix(&inode, full_path,
1023 rc = cifs_get_inode_info(&inode, full_path, NULL,
1024 inode->i_sb, xid, NULL);
1027 * Else we are writing out data to server already and could deadlock if
1028 * we tried to flush data, and since we do not know if we have data that
1029 * would invalidate the current end of file on the server we can not go
1030 * to the server to get the new inode info.
1034 * If the server returned a read oplock and we have mandatory brlocks,
1035 * set oplock level to None.
1037 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
1038 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
1042 server->ops->set_fid(cfile, &cfile->fid, oplock);
1043 if (oparms.reconnect)
1044 cifs_relock_file(cfile);
1047 free_dentry_path(page);
1052 void smb2_deferred_work_close(struct work_struct *work)
1054 struct cifsFileInfo *cfile = container_of(work,
1055 struct cifsFileInfo, deferred.work);
1057 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1058 cifs_del_deferred_close(cfile);
1059 cfile->deferred_close_scheduled = false;
1060 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1061 _cifsFileInfo_put(cfile, true, false);
1064 int cifs_close(struct inode *inode, struct file *file)
1066 struct cifsFileInfo *cfile;
1067 struct cifsInodeInfo *cinode = CIFS_I(inode);
1068 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1069 struct cifs_deferred_close *dclose;
1071 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
1073 if (file->private_data != NULL) {
1074 cfile = file->private_data;
1075 file->private_data = NULL;
1076 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
1077 if ((cinode->oplock == CIFS_CACHE_RHW_FLG) &&
1078 cinode->lease_granted &&
1079 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
1081 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
1082 inode->i_ctime = inode->i_mtime = current_time(inode);
1084 spin_lock(&cinode->deferred_lock);
1085 cifs_add_deferred_close(cfile, dclose);
1086 if (cfile->deferred_close_scheduled &&
1087 delayed_work_pending(&cfile->deferred)) {
1089 * If there is no pending work, mod_delayed_work queues new work.
1090 * So, Increase the ref count to avoid use-after-free.
1092 if (!mod_delayed_work(deferredclose_wq,
1093 &cfile->deferred, cifs_sb->ctx->closetimeo))
1094 cifsFileInfo_get(cfile);
1096 /* Deferred close for files */
1097 queue_delayed_work(deferredclose_wq,
1098 &cfile->deferred, cifs_sb->ctx->closetimeo);
1099 cfile->deferred_close_scheduled = true;
1100 spin_unlock(&cinode->deferred_lock);
1103 spin_unlock(&cinode->deferred_lock);
1104 _cifsFileInfo_put(cfile, true, false);
1106 _cifsFileInfo_put(cfile, true, false);
1111 /* return code from the ->release op is always ignored */
1116 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
1118 struct cifsFileInfo *open_file, *tmp;
1119 struct list_head tmp_list;
1121 if (!tcon->use_persistent || !tcon->need_reopen_files)
1124 tcon->need_reopen_files = false;
1126 cifs_dbg(FYI, "Reopen persistent handles\n");
1127 INIT_LIST_HEAD(&tmp_list);
1129 /* list all files open on tree connection, reopen resilient handles */
1130 spin_lock(&tcon->open_file_lock);
1131 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1132 if (!open_file->invalidHandle)
1134 cifsFileInfo_get(open_file);
1135 list_add_tail(&open_file->rlist, &tmp_list);
1137 spin_unlock(&tcon->open_file_lock);
1139 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1140 if (cifs_reopen_file(open_file, false /* do not flush */))
1141 tcon->need_reopen_files = true;
1142 list_del_init(&open_file->rlist);
1143 cifsFileInfo_put(open_file);
1147 int cifs_closedir(struct inode *inode, struct file *file)
1151 struct cifsFileInfo *cfile = file->private_data;
1152 struct cifs_tcon *tcon;
1153 struct TCP_Server_Info *server;
1156 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1162 tcon = tlink_tcon(cfile->tlink);
1163 server = tcon->ses->server;
1165 cifs_dbg(FYI, "Freeing private data in close dir\n");
1166 spin_lock(&cfile->file_info_lock);
1167 if (server->ops->dir_needs_close(cfile)) {
1168 cfile->invalidHandle = true;
1169 spin_unlock(&cfile->file_info_lock);
1170 if (server->ops->close_dir)
1171 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1174 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1175 /* not much we can do if it fails anyway, ignore rc */
1178 spin_unlock(&cfile->file_info_lock);
1180 buf = cfile->srch_inf.ntwrk_buf_start;
1182 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1183 cfile->srch_inf.ntwrk_buf_start = NULL;
1184 if (cfile->srch_inf.smallBuf)
1185 cifs_small_buf_release(buf);
1187 cifs_buf_release(buf);
1190 cifs_put_tlink(cfile->tlink);
1191 kfree(file->private_data);
1192 file->private_data = NULL;
1193 /* BB can we lock the filestruct while this is going on? */
1198 static struct cifsLockInfo *
1199 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1201 struct cifsLockInfo *lock =
1202 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1205 lock->offset = offset;
1206 lock->length = length;
1208 lock->pid = current->tgid;
1209 lock->flags = flags;
1210 INIT_LIST_HEAD(&lock->blist);
1211 init_waitqueue_head(&lock->block_q);
1216 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1218 struct cifsLockInfo *li, *tmp;
1219 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1220 list_del_init(&li->blist);
1221 wake_up(&li->block_q);
1225 #define CIFS_LOCK_OP 0
1226 #define CIFS_READ_OP 1
1227 #define CIFS_WRITE_OP 2
1229 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1231 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1232 __u64 length, __u8 type, __u16 flags,
1233 struct cifsFileInfo *cfile,
1234 struct cifsLockInfo **conf_lock, int rw_check)
1236 struct cifsLockInfo *li;
1237 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1238 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1240 list_for_each_entry(li, &fdlocks->locks, llist) {
1241 if (offset + length <= li->offset ||
1242 offset >= li->offset + li->length)
1244 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1245 server->ops->compare_fids(cfile, cur_cfile)) {
1246 /* shared lock prevents write op through the same fid */
1247 if (!(li->type & server->vals->shared_lock_type) ||
1248 rw_check != CIFS_WRITE_OP)
1251 if ((type & server->vals->shared_lock_type) &&
1252 ((server->ops->compare_fids(cfile, cur_cfile) &&
1253 current->tgid == li->pid) || type == li->type))
1255 if (rw_check == CIFS_LOCK_OP &&
1256 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1257 server->ops->compare_fids(cfile, cur_cfile))
1267 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1268 __u8 type, __u16 flags,
1269 struct cifsLockInfo **conf_lock, int rw_check)
1272 struct cifs_fid_locks *cur;
1273 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1275 list_for_each_entry(cur, &cinode->llist, llist) {
1276 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1277 flags, cfile, conf_lock,
1287 * Check if there is another lock that prevents us to set the lock (mandatory
1288 * style). If such a lock exists, update the flock structure with its
1289 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1290 * or leave it the same if we can't. Returns 0 if we don't need to request to
1291 * the server or 1 otherwise.
1294 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1295 __u8 type, struct file_lock *flock)
1298 struct cifsLockInfo *conf_lock;
1299 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1300 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1303 down_read(&cinode->lock_sem);
1305 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1306 flock->fl_flags, &conf_lock,
1309 flock->fl_start = conf_lock->offset;
1310 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1311 flock->fl_pid = conf_lock->pid;
1312 if (conf_lock->type & server->vals->shared_lock_type)
1313 flock->fl_type = F_RDLCK;
1315 flock->fl_type = F_WRLCK;
1316 } else if (!cinode->can_cache_brlcks)
1319 flock->fl_type = F_UNLCK;
1321 up_read(&cinode->lock_sem);
1326 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1328 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1329 cifs_down_write(&cinode->lock_sem);
1330 list_add_tail(&lock->llist, &cfile->llist->locks);
1331 up_write(&cinode->lock_sem);
1335 * Set the byte-range lock (mandatory style). Returns:
1336 * 1) 0, if we set the lock and don't need to request to the server;
1337 * 2) 1, if no locks prevent us but we need to request to the server;
1338 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1341 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1344 struct cifsLockInfo *conf_lock;
1345 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1351 cifs_down_write(&cinode->lock_sem);
1353 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1354 lock->type, lock->flags, &conf_lock,
1356 if (!exist && cinode->can_cache_brlcks) {
1357 list_add_tail(&lock->llist, &cfile->llist->locks);
1358 up_write(&cinode->lock_sem);
1367 list_add_tail(&lock->blist, &conf_lock->blist);
1368 up_write(&cinode->lock_sem);
1369 rc = wait_event_interruptible(lock->block_q,
1370 (lock->blist.prev == &lock->blist) &&
1371 (lock->blist.next == &lock->blist));
1374 cifs_down_write(&cinode->lock_sem);
1375 list_del_init(&lock->blist);
1378 up_write(&cinode->lock_sem);
1382 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1384 * Check if there is another lock that prevents us to set the lock (posix
1385 * style). If such a lock exists, update the flock structure with its
1386 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1387 * or leave it the same if we can't. Returns 0 if we don't need to request to
1388 * the server or 1 otherwise.
1391 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1394 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1395 unsigned char saved_type = flock->fl_type;
1397 if ((flock->fl_flags & FL_POSIX) == 0)
1400 down_read(&cinode->lock_sem);
1401 posix_test_lock(file, flock);
1403 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1404 flock->fl_type = saved_type;
1408 up_read(&cinode->lock_sem);
1413 * Set the byte-range lock (posix style). Returns:
1414 * 1) <0, if the error occurs while setting the lock;
1415 * 2) 0, if we set the lock and don't need to request to the server;
1416 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1417 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1420 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1422 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1423 int rc = FILE_LOCK_DEFERRED + 1;
1425 if ((flock->fl_flags & FL_POSIX) == 0)
1428 cifs_down_write(&cinode->lock_sem);
1429 if (!cinode->can_cache_brlcks) {
1430 up_write(&cinode->lock_sem);
1434 rc = posix_lock_file(file, flock, NULL);
1435 up_write(&cinode->lock_sem);
1440 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1443 int rc = 0, stored_rc;
1444 struct cifsLockInfo *li, *tmp;
1445 struct cifs_tcon *tcon;
1446 unsigned int num, max_num, max_buf;
1447 LOCKING_ANDX_RANGE *buf, *cur;
1448 static const int types[] = {
1449 LOCKING_ANDX_LARGE_FILES,
1450 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1455 tcon = tlink_tcon(cfile->tlink);
1458 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1459 * and check it before using.
1461 max_buf = tcon->ses->server->maxBuf;
1462 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1467 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1469 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1471 max_num = (max_buf - sizeof(struct smb_hdr)) /
1472 sizeof(LOCKING_ANDX_RANGE);
1473 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1479 for (i = 0; i < 2; i++) {
1482 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1483 if (li->type != types[i])
1485 cur->Pid = cpu_to_le16(li->pid);
1486 cur->LengthLow = cpu_to_le32((u32)li->length);
1487 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1488 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1489 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1490 if (++num == max_num) {
1491 stored_rc = cifs_lockv(xid, tcon,
1493 (__u8)li->type, 0, num,
1504 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1505 (__u8)types[i], 0, num, buf);
1517 hash_lockowner(fl_owner_t owner)
1519 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1521 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1523 struct lock_to_push {
1524 struct list_head llist;
1532 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1534 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1536 struct inode *inode = d_inode(cfile->dentry);
1537 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1538 struct file_lock *flock;
1539 struct file_lock_context *flctx = locks_inode_context(inode);
1540 unsigned int count = 0, i;
1541 int rc = 0, xid, type;
1542 struct list_head locks_to_send, *el;
1543 struct lock_to_push *lck, *tmp;
1551 spin_lock(&flctx->flc_lock);
1552 list_for_each(el, &flctx->flc_posix) {
1555 spin_unlock(&flctx->flc_lock);
1557 INIT_LIST_HEAD(&locks_to_send);
1560 * Allocating count locks is enough because no FL_POSIX locks can be
1561 * added to the list while we are holding cinode->lock_sem that
1562 * protects locking operations of this inode.
1564 for (i = 0; i < count; i++) {
1565 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1570 list_add_tail(&lck->llist, &locks_to_send);
1573 el = locks_to_send.next;
1574 spin_lock(&flctx->flc_lock);
1575 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1576 if (el == &locks_to_send) {
1578 * The list ended. We don't have enough allocated
1579 * structures - something is really wrong.
1581 cifs_dbg(VFS, "Can't push all brlocks!\n");
1584 length = cifs_flock_len(flock);
1585 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1589 lck = list_entry(el, struct lock_to_push, llist);
1590 lck->pid = hash_lockowner(flock->fl_owner);
1591 lck->netfid = cfile->fid.netfid;
1592 lck->length = length;
1594 lck->offset = flock->fl_start;
1596 spin_unlock(&flctx->flc_lock);
1598 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1601 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1602 lck->offset, lck->length, NULL,
1606 list_del(&lck->llist);
1614 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1615 list_del(&lck->llist);
1620 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1623 cifs_push_locks(struct cifsFileInfo *cfile)
1625 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1626 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1628 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1629 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1630 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1632 /* we are going to update can_cache_brlcks here - need a write access */
1633 cifs_down_write(&cinode->lock_sem);
1634 if (!cinode->can_cache_brlcks) {
1635 up_write(&cinode->lock_sem);
1639 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1640 if (cap_unix(tcon->ses) &&
1641 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1642 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1643 rc = cifs_push_posix_locks(cfile);
1645 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1646 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1648 cinode->can_cache_brlcks = false;
1649 up_write(&cinode->lock_sem);
1654 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1655 bool *wait_flag, struct TCP_Server_Info *server)
1657 if (flock->fl_flags & FL_POSIX)
1658 cifs_dbg(FYI, "Posix\n");
1659 if (flock->fl_flags & FL_FLOCK)
1660 cifs_dbg(FYI, "Flock\n");
1661 if (flock->fl_flags & FL_SLEEP) {
1662 cifs_dbg(FYI, "Blocking lock\n");
1665 if (flock->fl_flags & FL_ACCESS)
1666 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1667 if (flock->fl_flags & FL_LEASE)
1668 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1669 if (flock->fl_flags &
1670 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1671 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1672 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1674 *type = server->vals->large_lock_type;
1675 if (flock->fl_type == F_WRLCK) {
1676 cifs_dbg(FYI, "F_WRLCK\n");
1677 *type |= server->vals->exclusive_lock_type;
1679 } else if (flock->fl_type == F_UNLCK) {
1680 cifs_dbg(FYI, "F_UNLCK\n");
1681 *type |= server->vals->unlock_lock_type;
1683 /* Check if unlock includes more than one lock range */
1684 } else if (flock->fl_type == F_RDLCK) {
1685 cifs_dbg(FYI, "F_RDLCK\n");
1686 *type |= server->vals->shared_lock_type;
1688 } else if (flock->fl_type == F_EXLCK) {
1689 cifs_dbg(FYI, "F_EXLCK\n");
1690 *type |= server->vals->exclusive_lock_type;
1692 } else if (flock->fl_type == F_SHLCK) {
1693 cifs_dbg(FYI, "F_SHLCK\n");
1694 *type |= server->vals->shared_lock_type;
1697 cifs_dbg(FYI, "Unknown type of lock\n");
1701 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1702 bool wait_flag, bool posix_lck, unsigned int xid)
1705 __u64 length = cifs_flock_len(flock);
1706 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1707 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1708 struct TCP_Server_Info *server = tcon->ses->server;
1709 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1710 __u16 netfid = cfile->fid.netfid;
1713 int posix_lock_type;
1715 rc = cifs_posix_lock_test(file, flock);
1719 if (type & server->vals->shared_lock_type)
1720 posix_lock_type = CIFS_RDLCK;
1722 posix_lock_type = CIFS_WRLCK;
1723 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1724 hash_lockowner(flock->fl_owner),
1725 flock->fl_start, length, flock,
1726 posix_lock_type, wait_flag);
1729 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1731 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1735 /* BB we could chain these into one lock request BB */
1736 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1739 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1741 flock->fl_type = F_UNLCK;
1743 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1748 if (type & server->vals->shared_lock_type) {
1749 flock->fl_type = F_WRLCK;
1753 type &= ~server->vals->exclusive_lock_type;
1755 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1756 type | server->vals->shared_lock_type,
1759 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1760 type | server->vals->shared_lock_type, 0, 1, false);
1761 flock->fl_type = F_RDLCK;
1763 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1766 flock->fl_type = F_WRLCK;
1772 cifs_move_llist(struct list_head *source, struct list_head *dest)
1774 struct list_head *li, *tmp;
1775 list_for_each_safe(li, tmp, source)
1776 list_move(li, dest);
1780 cifs_free_llist(struct list_head *llist)
1782 struct cifsLockInfo *li, *tmp;
1783 list_for_each_entry_safe(li, tmp, llist, llist) {
1784 cifs_del_lock_waiters(li);
1785 list_del(&li->llist);
1790 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1792 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1795 int rc = 0, stored_rc;
1796 static const int types[] = {
1797 LOCKING_ANDX_LARGE_FILES,
1798 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1801 unsigned int max_num, num, max_buf;
1802 LOCKING_ANDX_RANGE *buf, *cur;
1803 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1804 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1805 struct cifsLockInfo *li, *tmp;
1806 __u64 length = cifs_flock_len(flock);
1807 struct list_head tmp_llist;
1809 INIT_LIST_HEAD(&tmp_llist);
1812 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1813 * and check it before using.
1815 max_buf = tcon->ses->server->maxBuf;
1816 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1819 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1821 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1823 max_num = (max_buf - sizeof(struct smb_hdr)) /
1824 sizeof(LOCKING_ANDX_RANGE);
1825 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1829 cifs_down_write(&cinode->lock_sem);
1830 for (i = 0; i < 2; i++) {
1833 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1834 if (flock->fl_start > li->offset ||
1835 (flock->fl_start + length) <
1836 (li->offset + li->length))
1838 if (current->tgid != li->pid)
1840 if (types[i] != li->type)
1842 if (cinode->can_cache_brlcks) {
1844 * We can cache brlock requests - simply remove
1845 * a lock from the file's list.
1847 list_del(&li->llist);
1848 cifs_del_lock_waiters(li);
1852 cur->Pid = cpu_to_le16(li->pid);
1853 cur->LengthLow = cpu_to_le32((u32)li->length);
1854 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1855 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1856 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1858 * We need to save a lock here to let us add it again to
1859 * the file's list if the unlock range request fails on
1862 list_move(&li->llist, &tmp_llist);
1863 if (++num == max_num) {
1864 stored_rc = cifs_lockv(xid, tcon,
1866 li->type, num, 0, buf);
1869 * We failed on the unlock range
1870 * request - add all locks from the tmp
1871 * list to the head of the file's list.
1873 cifs_move_llist(&tmp_llist,
1874 &cfile->llist->locks);
1878 * The unlock range request succeed -
1879 * free the tmp list.
1881 cifs_free_llist(&tmp_llist);
1888 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1889 types[i], num, 0, buf);
1891 cifs_move_llist(&tmp_llist,
1892 &cfile->llist->locks);
1895 cifs_free_llist(&tmp_llist);
1899 up_write(&cinode->lock_sem);
1903 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1906 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1907 bool wait_flag, bool posix_lck, int lock, int unlock,
1911 __u64 length = cifs_flock_len(flock);
1912 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1913 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1914 struct TCP_Server_Info *server = tcon->ses->server;
1915 struct inode *inode = d_inode(cfile->dentry);
1917 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1919 int posix_lock_type;
1921 rc = cifs_posix_lock_set(file, flock);
1922 if (rc <= FILE_LOCK_DEFERRED)
1925 if (type & server->vals->shared_lock_type)
1926 posix_lock_type = CIFS_RDLCK;
1928 posix_lock_type = CIFS_WRLCK;
1931 posix_lock_type = CIFS_UNLCK;
1933 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1934 hash_lockowner(flock->fl_owner),
1935 flock->fl_start, length,
1936 NULL, posix_lock_type, wait_flag);
1939 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1941 struct cifsLockInfo *lock;
1943 lock = cifs_lock_init(flock->fl_start, length, type,
1948 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1957 * Windows 7 server can delay breaking lease from read to None
1958 * if we set a byte-range lock on a file - break it explicitly
1959 * before sending the lock to the server to be sure the next
1960 * read won't conflict with non-overlapted locks due to
1963 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1964 CIFS_CACHE_READ(CIFS_I(inode))) {
1965 cifs_zap_mapping(inode);
1966 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1968 CIFS_I(inode)->oplock = 0;
1971 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1972 type, 1, 0, wait_flag);
1978 cifs_lock_add(cfile, lock);
1980 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1983 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1985 * If this is a request to remove all locks because we
1986 * are closing the file, it doesn't matter if the
1987 * unlocking failed as both cifs.ko and the SMB server
1988 * remove the lock on file close
1991 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1992 if (!(flock->fl_flags & FL_CLOSE))
1995 rc = locks_lock_file_wait(file, flock);
2000 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
2003 int lock = 0, unlock = 0;
2004 bool wait_flag = false;
2005 bool posix_lck = false;
2006 struct cifs_sb_info *cifs_sb;
2007 struct cifs_tcon *tcon;
2008 struct cifsFileInfo *cfile;
2013 if (!(fl->fl_flags & FL_FLOCK)) {
2019 cfile = (struct cifsFileInfo *)file->private_data;
2020 tcon = tlink_tcon(cfile->tlink);
2022 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
2024 cifs_sb = CIFS_FILE_SB(file);
2026 if (cap_unix(tcon->ses) &&
2027 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2028 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2031 if (!lock && !unlock) {
2033 * if no lock or unlock then nothing to do since we do not
2041 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
2049 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
2052 int lock = 0, unlock = 0;
2053 bool wait_flag = false;
2054 bool posix_lck = false;
2055 struct cifs_sb_info *cifs_sb;
2056 struct cifs_tcon *tcon;
2057 struct cifsFileInfo *cfile;
2063 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
2064 flock->fl_flags, flock->fl_type, (long long)flock->fl_start,
2065 (long long)flock->fl_end);
2067 cfile = (struct cifsFileInfo *)file->private_data;
2068 tcon = tlink_tcon(cfile->tlink);
2070 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
2072 cifs_sb = CIFS_FILE_SB(file);
2073 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
2075 if (cap_unix(tcon->ses) &&
2076 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2077 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2080 * BB add code here to normalize offset and length to account for
2081 * negative length which we can not accept over the wire.
2083 if (IS_GETLK(cmd)) {
2084 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
2089 if (!lock && !unlock) {
2091 * if no lock or unlock then nothing to do since we do not
2098 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
2105 * update the file size (if needed) after a write. Should be called with
2106 * the inode->i_lock held
2109 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
2110 unsigned int bytes_written)
2112 loff_t end_of_write = offset + bytes_written;
2114 if (end_of_write > cifsi->server_eof)
2115 cifsi->server_eof = end_of_write;
2119 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
2120 size_t write_size, loff_t *offset)
2123 unsigned int bytes_written = 0;
2124 unsigned int total_written;
2125 struct cifs_tcon *tcon;
2126 struct TCP_Server_Info *server;
2128 struct dentry *dentry = open_file->dentry;
2129 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2130 struct cifs_io_parms io_parms = {0};
2132 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2133 write_size, *offset, dentry);
2135 tcon = tlink_tcon(open_file->tlink);
2136 server = tcon->ses->server;
2138 if (!server->ops->sync_write)
2143 for (total_written = 0; write_size > total_written;
2144 total_written += bytes_written) {
2146 while (rc == -EAGAIN) {
2150 if (open_file->invalidHandle) {
2151 /* we could deadlock if we called
2152 filemap_fdatawait from here so tell
2153 reopen_file not to flush data to
2155 rc = cifs_reopen_file(open_file, false);
2160 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2161 (unsigned int)write_size - total_written);
2162 /* iov[0] is reserved for smb header */
2163 iov[1].iov_base = (char *)write_data + total_written;
2164 iov[1].iov_len = len;
2166 io_parms.tcon = tcon;
2167 io_parms.offset = *offset;
2168 io_parms.length = len;
2169 rc = server->ops->sync_write(xid, &open_file->fid,
2170 &io_parms, &bytes_written, iov, 1);
2172 if (rc || (bytes_written == 0)) {
2180 spin_lock(&d_inode(dentry)->i_lock);
2181 cifs_update_eof(cifsi, *offset, bytes_written);
2182 spin_unlock(&d_inode(dentry)->i_lock);
2183 *offset += bytes_written;
2187 cifs_stats_bytes_written(tcon, total_written);
2189 if (total_written > 0) {
2190 spin_lock(&d_inode(dentry)->i_lock);
2191 if (*offset > d_inode(dentry)->i_size) {
2192 i_size_write(d_inode(dentry), *offset);
2193 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2195 spin_unlock(&d_inode(dentry)->i_lock);
2197 mark_inode_dirty_sync(d_inode(dentry));
2199 return total_written;
2202 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2205 struct cifsFileInfo *open_file = NULL;
2206 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2208 /* only filter by fsuid on multiuser mounts */
2209 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2212 spin_lock(&cifs_inode->open_file_lock);
2213 /* we could simply get the first_list_entry since write-only entries
2214 are always at the end of the list but since the first entry might
2215 have a close pending, we go through the whole list */
2216 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2217 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2219 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2220 if ((!open_file->invalidHandle)) {
2221 /* found a good file */
2222 /* lock it so it will not be closed on us */
2223 cifsFileInfo_get(open_file);
2224 spin_unlock(&cifs_inode->open_file_lock);
2226 } /* else might as well continue, and look for
2227 another, or simply have the caller reopen it
2228 again rather than trying to fix this handle */
2229 } else /* write only file */
2230 break; /* write only files are last so must be done */
2232 spin_unlock(&cifs_inode->open_file_lock);
2236 /* Return -EBADF if no handle is found and general rc otherwise */
2238 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2239 struct cifsFileInfo **ret_file)
2241 struct cifsFileInfo *open_file, *inv_file = NULL;
2242 struct cifs_sb_info *cifs_sb;
2243 bool any_available = false;
2245 unsigned int refind = 0;
2246 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2247 bool with_delete = flags & FIND_WR_WITH_DELETE;
2251 * Having a null inode here (because mapping->host was set to zero by
2252 * the VFS or MM) should not happen but we had reports of on oops (due
2253 * to it being zero) during stress testcases so we need to check for it
2256 if (cifs_inode == NULL) {
2257 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2262 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2264 /* only filter by fsuid on multiuser mounts */
2265 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2268 spin_lock(&cifs_inode->open_file_lock);
2270 if (refind > MAX_REOPEN_ATT) {
2271 spin_unlock(&cifs_inode->open_file_lock);
2274 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2275 if (!any_available && open_file->pid != current->tgid)
2277 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2279 if (with_delete && !(open_file->fid.access & DELETE))
2281 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2282 if (!open_file->invalidHandle) {
2283 /* found a good writable file */
2284 cifsFileInfo_get(open_file);
2285 spin_unlock(&cifs_inode->open_file_lock);
2286 *ret_file = open_file;
2290 inv_file = open_file;
2294 /* couldn't find useable FH with same pid, try any available */
2295 if (!any_available) {
2296 any_available = true;
2297 goto refind_writable;
2301 any_available = false;
2302 cifsFileInfo_get(inv_file);
2305 spin_unlock(&cifs_inode->open_file_lock);
2308 rc = cifs_reopen_file(inv_file, false);
2310 *ret_file = inv_file;
2314 spin_lock(&cifs_inode->open_file_lock);
2315 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2316 spin_unlock(&cifs_inode->open_file_lock);
2317 cifsFileInfo_put(inv_file);
2320 spin_lock(&cifs_inode->open_file_lock);
2321 goto refind_writable;
2327 struct cifsFileInfo *
2328 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2330 struct cifsFileInfo *cfile;
2333 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2335 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2341 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2343 struct cifsFileInfo **ret_file)
2345 struct cifsFileInfo *cfile;
2346 void *page = alloc_dentry_path();
2350 spin_lock(&tcon->open_file_lock);
2351 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2352 struct cifsInodeInfo *cinode;
2353 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2354 if (IS_ERR(full_path)) {
2355 spin_unlock(&tcon->open_file_lock);
2356 free_dentry_path(page);
2357 return PTR_ERR(full_path);
2359 if (strcmp(full_path, name))
2362 cinode = CIFS_I(d_inode(cfile->dentry));
2363 spin_unlock(&tcon->open_file_lock);
2364 free_dentry_path(page);
2365 return cifs_get_writable_file(cinode, flags, ret_file);
2368 spin_unlock(&tcon->open_file_lock);
2369 free_dentry_path(page);
2374 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2375 struct cifsFileInfo **ret_file)
2377 struct cifsFileInfo *cfile;
2378 void *page = alloc_dentry_path();
2382 spin_lock(&tcon->open_file_lock);
2383 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2384 struct cifsInodeInfo *cinode;
2385 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2386 if (IS_ERR(full_path)) {
2387 spin_unlock(&tcon->open_file_lock);
2388 free_dentry_path(page);
2389 return PTR_ERR(full_path);
2391 if (strcmp(full_path, name))
2394 cinode = CIFS_I(d_inode(cfile->dentry));
2395 spin_unlock(&tcon->open_file_lock);
2396 free_dentry_path(page);
2397 *ret_file = find_readable_file(cinode, 0);
2398 return *ret_file ? 0 : -ENOENT;
2401 spin_unlock(&tcon->open_file_lock);
2402 free_dentry_path(page);
2407 cifs_writedata_release(struct kref *refcount)
2409 struct cifs_writedata *wdata = container_of(refcount,
2410 struct cifs_writedata, refcount);
2411 #ifdef CONFIG_CIFS_SMB_DIRECT
2413 smbd_deregister_mr(wdata->mr);
2419 cifsFileInfo_put(wdata->cfile);
2425 * Write failed with a retryable error. Resend the write request. It's also
2426 * possible that the page was redirtied so re-clean the page.
2429 cifs_writev_requeue(struct cifs_writedata *wdata)
2432 struct inode *inode = d_inode(wdata->cfile->dentry);
2433 struct TCP_Server_Info *server;
2434 unsigned int rest_len = wdata->bytes;
2435 loff_t fpos = wdata->offset;
2437 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2439 struct cifs_writedata *wdata2;
2440 unsigned int wsize, cur_len;
2442 wsize = server->ops->wp_retry_size(inode);
2443 if (wsize < rest_len) {
2444 if (wsize < PAGE_SIZE) {
2448 cur_len = min(round_down(wsize, PAGE_SIZE), rest_len);
2453 wdata2 = cifs_writedata_alloc(cifs_writev_complete);
2459 wdata2->sync_mode = wdata->sync_mode;
2460 wdata2->offset = fpos;
2461 wdata2->bytes = cur_len;
2462 wdata2->iter = wdata->iter;
2464 iov_iter_advance(&wdata2->iter, fpos - wdata->offset);
2465 iov_iter_truncate(&wdata2->iter, wdata2->bytes);
2467 if (iov_iter_is_xarray(&wdata2->iter))
2468 /* Check for pages having been redirtied and clean
2469 * them. We can do this by walking the xarray. If
2470 * it's not an xarray, then it's a DIO and we shouldn't
2471 * be mucking around with the page bits.
2473 cifs_undirty_folios(inode, fpos, cur_len);
2475 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2477 if (!wdata2->cfile) {
2478 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2480 if (!is_retryable_error(rc))
2483 wdata2->pid = wdata2->cfile->pid;
2484 rc = server->ops->async_writev(wdata2,
2485 cifs_writedata_release);
2488 kref_put(&wdata2->refcount, cifs_writedata_release);
2490 if (is_retryable_error(rc))
2493 rest_len -= cur_len;
2498 rest_len -= cur_len;
2499 } while (rest_len > 0);
2501 /* Clean up remaining pages from the original wdata */
2502 if (iov_iter_is_xarray(&wdata->iter))
2503 cifs_pages_write_failed(inode, fpos, rest_len);
2505 if (rc != 0 && !is_retryable_error(rc))
2506 mapping_set_error(inode->i_mapping, rc);
2507 kref_put(&wdata->refcount, cifs_writedata_release);
2511 cifs_writev_complete(struct work_struct *work)
2513 struct cifs_writedata *wdata = container_of(work,
2514 struct cifs_writedata, work);
2515 struct inode *inode = d_inode(wdata->cfile->dentry);
2517 if (wdata->result == 0) {
2518 spin_lock(&inode->i_lock);
2519 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2520 spin_unlock(&inode->i_lock);
2521 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2523 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2524 return cifs_writev_requeue(wdata);
2526 if (wdata->result == -EAGAIN)
2527 cifs_pages_write_redirty(inode, wdata->offset, wdata->bytes);
2528 else if (wdata->result < 0)
2529 cifs_pages_write_failed(inode, wdata->offset, wdata->bytes);
2531 cifs_pages_written_back(inode, wdata->offset, wdata->bytes);
2533 if (wdata->result != -EAGAIN)
2534 mapping_set_error(inode->i_mapping, wdata->result);
2535 kref_put(&wdata->refcount, cifs_writedata_release);
2538 struct cifs_writedata *cifs_writedata_alloc(work_func_t complete)
2540 struct cifs_writedata *wdata;
2542 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2543 if (wdata != NULL) {
2544 kref_init(&wdata->refcount);
2545 INIT_LIST_HEAD(&wdata->list);
2546 init_completion(&wdata->done);
2547 INIT_WORK(&wdata->work, complete);
2552 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2554 struct address_space *mapping = page->mapping;
2555 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2558 int bytes_written = 0;
2559 struct inode *inode;
2560 struct cifsFileInfo *open_file;
2562 if (!mapping || !mapping->host)
2565 inode = page->mapping->host;
2567 offset += (loff_t)from;
2568 write_data = kmap(page);
2571 if ((to > PAGE_SIZE) || (from > to)) {
2576 /* racing with truncate? */
2577 if (offset > mapping->host->i_size) {
2579 return 0; /* don't care */
2582 /* check to make sure that we are not extending the file */
2583 if (mapping->host->i_size - offset < (loff_t)to)
2584 to = (unsigned)(mapping->host->i_size - offset);
2586 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2589 bytes_written = cifs_write(open_file, open_file->pid,
2590 write_data, to - from, &offset);
2591 cifsFileInfo_put(open_file);
2592 /* Does mm or vfs already set times? */
2593 inode->i_atime = inode->i_mtime = current_time(inode);
2594 if ((bytes_written > 0) && (offset))
2596 else if (bytes_written < 0)
2601 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2602 if (!is_retryable_error(rc))
2611 * Extend the region to be written back to include subsequent contiguously
2612 * dirty pages if possible, but don't sleep while doing so.
2614 static void cifs_extend_writeback(struct address_space *mapping,
2621 struct folio_batch batch;
2622 struct folio *folio;
2623 unsigned int psize, nr_pages;
2625 pgoff_t index = (start + len) / PAGE_SIZE;
2628 XA_STATE(xas, &mapping->i_pages, index);
2630 folio_batch_init(&batch);
2633 /* Firstly, we gather up a batch of contiguous dirty pages
2634 * under the RCU read lock - but we can't clear the dirty flags
2635 * there if any of those pages are mapped.
2639 xas_for_each(&xas, folio, ULONG_MAX) {
2641 if (xas_retry(&xas, folio))
2643 if (xa_is_value(folio))
2645 if (folio_index(folio) != index)
2647 if (!folio_try_get_rcu(folio)) {
2651 nr_pages = folio_nr_pages(folio);
2652 if (nr_pages > max_pages)
2655 /* Has the page moved or been split? */
2656 if (unlikely(folio != xas_reload(&xas))) {
2661 if (!folio_trylock(folio)) {
2665 if (!folio_test_dirty(folio) || folio_test_writeback(folio)) {
2666 folio_unlock(folio);
2671 max_pages -= nr_pages;
2672 psize = folio_size(folio);
2675 if (max_pages <= 0 || len >= max_len || *_count <= 0)
2679 if (!folio_batch_add(&batch, folio))
2689 /* Now, if we obtained any pages, we can shift them to being
2690 * writable and mark them for caching.
2692 if (!folio_batch_count(&batch))
2695 for (i = 0; i < folio_batch_count(&batch); i++) {
2696 folio = batch.folios[i];
2697 /* The folio should be locked, dirty and not undergoing
2698 * writeback from the loop above.
2700 if (!folio_clear_dirty_for_io(folio))
2702 if (folio_start_writeback(folio))
2705 *_count -= folio_nr_pages(folio);
2706 folio_unlock(folio);
2709 folio_batch_release(&batch);
2717 * Write back the locked page and any subsequent non-locked dirty pages.
2719 static ssize_t cifs_write_back_from_locked_folio(struct address_space *mapping,
2720 struct writeback_control *wbc,
2721 struct folio *folio,
2722 loff_t start, loff_t end)
2724 struct inode *inode = mapping->host;
2725 struct TCP_Server_Info *server;
2726 struct cifs_writedata *wdata;
2727 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2728 struct cifs_credits credits_on_stack;
2729 struct cifs_credits *credits = &credits_on_stack;
2730 struct cifsFileInfo *cfile = NULL;
2731 unsigned int xid, wsize, len;
2732 loff_t i_size = i_size_read(inode);
2734 long count = wbc->nr_to_write;
2737 /* The folio should be locked, dirty and not undergoing writeback. */
2738 if (folio_start_writeback(folio))
2741 count -= folio_nr_pages(folio);
2742 len = folio_size(folio);
2745 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2747 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2749 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", rc);
2753 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2758 wdata = cifs_writedata_alloc(cifs_writev_complete);
2764 wdata->sync_mode = wbc->sync_mode;
2765 wdata->offset = folio_pos(folio);
2766 wdata->pid = cfile->pid;
2767 wdata->credits = credits_on_stack;
2768 wdata->cfile = cfile;
2769 wdata->server = server;
2772 /* Find all consecutive lockable dirty pages, stopping when we find a
2773 * page that is not immediately lockable, is not dirty or is missing,
2774 * or we reach the end of the range.
2776 if (start < i_size) {
2777 /* Trim the write to the EOF; the extra data is ignored. Also
2778 * put an upper limit on the size of a single storedata op.
2781 max_len = min_t(unsigned long long, max_len, end - start + 1);
2782 max_len = min_t(unsigned long long, max_len, i_size - start);
2784 if (len < max_len) {
2785 int max_pages = INT_MAX;
2787 #ifdef CONFIG_CIFS_SMB_DIRECT
2788 if (server->smbd_conn)
2789 max_pages = server->smbd_conn->max_frmr_depth;
2791 max_pages -= folio_nr_pages(folio);
2794 cifs_extend_writeback(mapping, &count, start,
2795 max_pages, max_len, &len);
2797 len = min_t(loff_t, len, max_len);
2802 /* We now have a contiguous set of dirty pages, each with writeback
2803 * set; the first page is still locked at this point, but all the rest
2804 * have been unlocked.
2806 folio_unlock(folio);
2808 if (start < i_size) {
2809 iov_iter_xarray(&wdata->iter, ITER_SOURCE, &mapping->i_pages,
2812 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2816 if (wdata->cfile->invalidHandle)
2819 rc = wdata->server->ops->async_writev(wdata,
2820 cifs_writedata_release);
2822 kref_put(&wdata->refcount, cifs_writedata_release);
2826 /* The dirty region was entirely beyond the EOF. */
2827 cifs_pages_written_back(inode, start, len);
2832 kref_put(&wdata->refcount, cifs_writedata_release);
2834 add_credits_and_wake_if(server, credits, 0);
2837 cifsFileInfo_put(cfile);
2841 wbc->nr_to_write = count;
2842 } else if (is_retryable_error(rc)) {
2843 cifs_pages_write_redirty(inode, start, len);
2845 cifs_pages_write_failed(inode, start, len);
2846 mapping_set_error(mapping, rc);
2848 /* Indication to update ctime and mtime as close is deferred */
2849 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2854 * write a region of pages back to the server
2856 static int cifs_writepages_region(struct address_space *mapping,
2857 struct writeback_control *wbc,
2858 loff_t start, loff_t end, loff_t *_next)
2860 struct folio_batch fbatch;
2863 folio_batch_init(&fbatch);
2866 pgoff_t index = start / PAGE_SIZE;
2868 nr = filemap_get_folios_tag(mapping, &index, end / PAGE_SIZE,
2869 PAGECACHE_TAG_DIRTY, &fbatch);
2873 for (int i = 0; i < nr; i++) {
2875 struct folio *folio = fbatch.folios[i];
2878 start = folio_pos(folio); /* May regress with THPs */
2880 /* At this point we hold neither the i_pages lock nor the
2881 * page lock: the page may be truncated or invalidated
2882 * (changing page->mapping to NULL), or even swizzled
2883 * back from swapper_space to tmpfs file mapping
2885 if (wbc->sync_mode != WB_SYNC_NONE) {
2886 ret = folio_lock_killable(folio);
2890 if (!folio_trylock(folio))
2894 if (folio_mapping(folio) != mapping ||
2895 !folio_test_dirty(folio)) {
2896 start += folio_size(folio);
2897 folio_unlock(folio);
2901 if (folio_test_writeback(folio) ||
2902 folio_test_fscache(folio)) {
2903 folio_unlock(folio);
2904 if (wbc->sync_mode == WB_SYNC_NONE)
2907 folio_wait_writeback(folio);
2908 #ifdef CONFIG_CIFS_FSCACHE
2909 folio_wait_fscache(folio);
2914 if (!folio_clear_dirty_for_io(folio))
2915 /* We hold the page lock - it should've been dirty. */
2918 ret = cifs_write_back_from_locked_folio(mapping, wbc, folio, start, end);
2926 folio_batch_release(&fbatch);
2932 * Too many skipped writes, or need to reschedule?
2933 * Treat it as a write error without an error code.
2935 if (skips >= 5 || need_resched()) {
2940 /* Otherwise, just skip that folio and go on to the next */
2942 start += folio_size(folio);
2946 folio_batch_release(&fbatch);
2948 } while (wbc->nr_to_write > 0);
2955 * Write some of the pending data back to the server
2957 static int cifs_writepages(struct address_space *mapping,
2958 struct writeback_control *wbc)
2963 /* We have to be careful as we can end up racing with setattr()
2964 * truncating the pagecache since the caller doesn't take a lock here
2968 if (wbc->range_cyclic) {
2969 start = mapping->writeback_index * PAGE_SIZE;
2970 ret = cifs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
2972 mapping->writeback_index = next / PAGE_SIZE;
2973 if (start > 0 && wbc->nr_to_write > 0) {
2974 ret = cifs_writepages_region(mapping, wbc, 0,
2977 mapping->writeback_index =
2981 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
2982 ret = cifs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
2983 if (wbc->nr_to_write > 0 && ret == 0)
2984 mapping->writeback_index = next / PAGE_SIZE;
2986 ret = cifs_writepages_region(mapping, wbc,
2987 wbc->range_start, wbc->range_end, &next);
2994 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
3000 /* BB add check for wbc flags */
3002 if (!PageUptodate(page))
3003 cifs_dbg(FYI, "ppw - page not up to date\n");
3006 * Set the "writeback" flag, and clear "dirty" in the radix tree.
3008 * A writepage() implementation always needs to do either this,
3009 * or re-dirty the page with "redirty_page_for_writepage()" in
3010 * the case of a failure.
3012 * Just unlocking the page will cause the radix tree tag-bits
3013 * to fail to update with the state of the page correctly.
3015 set_page_writeback(page);
3017 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
3018 if (is_retryable_error(rc)) {
3019 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
3021 redirty_page_for_writepage(wbc, page);
3022 } else if (rc != 0) {
3024 mapping_set_error(page->mapping, rc);
3026 SetPageUptodate(page);
3028 end_page_writeback(page);
3034 static int cifs_write_end(struct file *file, struct address_space *mapping,
3035 loff_t pos, unsigned len, unsigned copied,
3036 struct page *page, void *fsdata)
3039 struct inode *inode = mapping->host;
3040 struct cifsFileInfo *cfile = file->private_data;
3041 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
3042 struct folio *folio = page_folio(page);
3045 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3048 pid = current->tgid;
3050 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
3053 if (folio_test_checked(folio)) {
3055 folio_mark_uptodate(folio);
3056 folio_clear_checked(folio);
3057 } else if (!folio_test_uptodate(folio) && copied == PAGE_SIZE)
3058 folio_mark_uptodate(folio);
3060 if (!folio_test_uptodate(folio)) {
3062 unsigned offset = pos & (PAGE_SIZE - 1);
3066 /* this is probably better than directly calling
3067 partialpage_write since in this function the file handle is
3068 known which we might as well leverage */
3069 /* BB check if anything else missing out of ppw
3070 such as updating last write time */
3071 page_data = kmap(page);
3072 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
3073 /* if (rc < 0) should we set writebehind rc? */
3080 set_page_dirty(page);
3084 spin_lock(&inode->i_lock);
3085 if (pos > inode->i_size) {
3086 i_size_write(inode, pos);
3087 inode->i_blocks = (512 - 1 + pos) >> 9;
3089 spin_unlock(&inode->i_lock);
3094 /* Indication to update ctime and mtime as close is deferred */
3095 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
3100 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
3105 struct cifs_tcon *tcon;
3106 struct TCP_Server_Info *server;
3107 struct cifsFileInfo *smbfile = file->private_data;
3108 struct inode *inode = file_inode(file);
3109 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3111 rc = file_write_and_wait_range(file, start, end);
3113 trace_cifs_fsync_err(inode->i_ino, rc);
3119 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3122 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
3123 rc = cifs_zap_mapping(inode);
3125 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
3126 rc = 0; /* don't care about it in fsync */
3130 tcon = tlink_tcon(smbfile->tlink);
3131 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3132 server = tcon->ses->server;
3133 if (server->ops->flush == NULL) {
3135 goto strict_fsync_exit;
3138 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3139 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3141 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3142 cifsFileInfo_put(smbfile);
3144 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3146 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3154 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3158 struct cifs_tcon *tcon;
3159 struct TCP_Server_Info *server;
3160 struct cifsFileInfo *smbfile = file->private_data;
3161 struct inode *inode = file_inode(file);
3162 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
3164 rc = file_write_and_wait_range(file, start, end);
3166 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
3172 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3175 tcon = tlink_tcon(smbfile->tlink);
3176 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3177 server = tcon->ses->server;
3178 if (server->ops->flush == NULL) {
3183 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3184 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3186 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3187 cifsFileInfo_put(smbfile);
3189 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3191 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3200 * As file closes, flush all cached write data for this inode checking
3201 * for write behind errors.
3203 int cifs_flush(struct file *file, fl_owner_t id)
3205 struct inode *inode = file_inode(file);
3208 if (file->f_mode & FMODE_WRITE)
3209 rc = filemap_write_and_wait(inode->i_mapping);
3211 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3213 /* get more nuanced writeback errors */
3214 rc = filemap_check_wb_err(file->f_mapping, 0);
3215 trace_cifs_flush_err(inode->i_ino, rc);
3221 cifs_uncached_writedata_release(struct kref *refcount)
3223 struct cifs_writedata *wdata = container_of(refcount,
3224 struct cifs_writedata, refcount);
3226 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3227 cifs_writedata_release(refcount);
3230 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3233 cifs_uncached_writev_complete(struct work_struct *work)
3235 struct cifs_writedata *wdata = container_of(work,
3236 struct cifs_writedata, work);
3237 struct inode *inode = d_inode(wdata->cfile->dentry);
3238 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3240 spin_lock(&inode->i_lock);
3241 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3242 if (cifsi->server_eof > inode->i_size)
3243 i_size_write(inode, cifsi->server_eof);
3244 spin_unlock(&inode->i_lock);
3246 complete(&wdata->done);
3247 collect_uncached_write_data(wdata->ctx);
3248 /* the below call can possibly free the last ref to aio ctx */
3249 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3253 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3254 struct cifs_aio_ctx *ctx)
3257 struct cifs_credits credits;
3259 struct TCP_Server_Info *server = wdata->server;
3262 if (wdata->cfile->invalidHandle) {
3263 rc = cifs_reopen_file(wdata->cfile, false);
3272 * Wait for credits to resend this wdata.
3273 * Note: we are attempting to resend the whole wdata not in
3277 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3282 if (wsize < wdata->bytes) {
3283 add_credits_and_wake_if(server, &credits, 0);
3286 } while (wsize < wdata->bytes);
3287 wdata->credits = credits;
3289 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3292 if (wdata->cfile->invalidHandle)
3295 #ifdef CONFIG_CIFS_SMB_DIRECT
3297 wdata->mr->need_invalidate = true;
3298 smbd_deregister_mr(wdata->mr);
3302 rc = server->ops->async_writev(wdata,
3303 cifs_uncached_writedata_release);
3307 /* If the write was successfully sent, we are done */
3309 list_add_tail(&wdata->list, wdata_list);
3313 /* Roll back credits and retry if needed */
3314 add_credits_and_wake_if(server, &wdata->credits, 0);
3315 } while (rc == -EAGAIN);
3318 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3323 * Select span of a bvec iterator we're going to use. Limit it by both maximum
3324 * size and maximum number of segments.
3326 static size_t cifs_limit_bvec_subset(const struct iov_iter *iter, size_t max_size,
3327 size_t max_segs, unsigned int *_nsegs)
3329 const struct bio_vec *bvecs = iter->bvec;
3330 unsigned int nbv = iter->nr_segs, ix = 0, nsegs = 0;
3331 size_t len, span = 0, n = iter->count;
3332 size_t skip = iter->iov_offset;
3334 if (WARN_ON(!iov_iter_is_bvec(iter)) || n == 0)
3337 while (n && ix < nbv && skip) {
3338 len = bvecs[ix].bv_len;
3346 while (n && ix < nbv) {
3347 len = min3(n, bvecs[ix].bv_len - skip, max_size);
3351 if (span >= max_size || nsegs >= max_segs)
3362 cifs_write_from_iter(loff_t fpos, size_t len, struct iov_iter *from,
3363 struct cifsFileInfo *open_file,
3364 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3365 struct cifs_aio_ctx *ctx)
3368 size_t cur_len, max_len;
3369 struct cifs_writedata *wdata;
3371 struct TCP_Server_Info *server;
3372 unsigned int xid, max_segs = INT_MAX;
3374 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3375 pid = open_file->pid;
3377 pid = current->tgid;
3379 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3382 #ifdef CONFIG_CIFS_SMB_DIRECT
3383 if (server->smbd_conn)
3384 max_segs = server->smbd_conn->max_frmr_depth;
3388 struct cifs_credits credits_on_stack;
3389 struct cifs_credits *credits = &credits_on_stack;
3390 unsigned int wsize, nsegs = 0;
3392 if (signal_pending(current)) {
3397 if (open_file->invalidHandle) {
3398 rc = cifs_reopen_file(open_file, false);
3405 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3410 max_len = min_t(const size_t, len, wsize);
3413 add_credits_and_wake_if(server, credits, 0);
3417 cur_len = cifs_limit_bvec_subset(from, max_len, max_segs, &nsegs);
3418 cifs_dbg(FYI, "write_from_iter len=%zx/%zx nsegs=%u/%lu/%u\n",
3419 cur_len, max_len, nsegs, from->nr_segs, max_segs);
3422 add_credits_and_wake_if(server, credits, 0);
3426 wdata = cifs_writedata_alloc(cifs_uncached_writev_complete);
3429 add_credits_and_wake_if(server, credits, 0);
3433 wdata->sync_mode = WB_SYNC_ALL;
3434 wdata->offset = (__u64)fpos;
3435 wdata->cfile = cifsFileInfo_get(open_file);
3436 wdata->server = server;
3438 wdata->bytes = cur_len;
3439 wdata->credits = credits_on_stack;
3440 wdata->iter = *from;
3442 kref_get(&ctx->refcount);
3444 iov_iter_truncate(&wdata->iter, cur_len);
3446 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3449 if (wdata->cfile->invalidHandle)
3452 rc = server->ops->async_writev(wdata,
3453 cifs_uncached_writedata_release);
3457 add_credits_and_wake_if(server, &wdata->credits, 0);
3458 kref_put(&wdata->refcount,
3459 cifs_uncached_writedata_release);
3465 list_add_tail(&wdata->list, wdata_list);
3466 iov_iter_advance(from, cur_len);
3475 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3477 struct cifs_writedata *wdata, *tmp;
3478 struct cifs_tcon *tcon;
3479 struct cifs_sb_info *cifs_sb;
3480 struct dentry *dentry = ctx->cfile->dentry;
3483 tcon = tlink_tcon(ctx->cfile->tlink);
3484 cifs_sb = CIFS_SB(dentry->d_sb);
3486 mutex_lock(&ctx->aio_mutex);
3488 if (list_empty(&ctx->list)) {
3489 mutex_unlock(&ctx->aio_mutex);
3495 * Wait for and collect replies for any successful sends in order of
3496 * increasing offset. Once an error is hit, then return without waiting
3497 * for any more replies.
3500 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3502 if (!try_wait_for_completion(&wdata->done)) {
3503 mutex_unlock(&ctx->aio_mutex);
3510 ctx->total_len += wdata->bytes;
3512 /* resend call if it's a retryable error */
3513 if (rc == -EAGAIN) {
3514 struct list_head tmp_list;
3515 struct iov_iter tmp_from = ctx->iter;
3517 INIT_LIST_HEAD(&tmp_list);
3518 list_del_init(&wdata->list);
3521 rc = cifs_resend_wdata(
3522 wdata, &tmp_list, ctx);
3524 iov_iter_advance(&tmp_from,
3525 wdata->offset - ctx->pos);
3527 rc = cifs_write_from_iter(wdata->offset,
3528 wdata->bytes, &tmp_from,
3529 ctx->cfile, cifs_sb, &tmp_list,
3532 kref_put(&wdata->refcount,
3533 cifs_uncached_writedata_release);
3536 list_splice(&tmp_list, &ctx->list);
3540 list_del_init(&wdata->list);
3541 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3544 cifs_stats_bytes_written(tcon, ctx->total_len);
3545 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3547 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3549 mutex_unlock(&ctx->aio_mutex);
3551 if (ctx->iocb && ctx->iocb->ki_complete)
3552 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
3554 complete(&ctx->done);
3557 static ssize_t __cifs_writev(
3558 struct kiocb *iocb, struct iov_iter *from, bool direct)
3560 struct file *file = iocb->ki_filp;
3561 ssize_t total_written = 0;
3562 struct cifsFileInfo *cfile;
3563 struct cifs_tcon *tcon;
3564 struct cifs_sb_info *cifs_sb;
3565 struct cifs_aio_ctx *ctx;
3568 rc = generic_write_checks(iocb, from);
3572 cifs_sb = CIFS_FILE_SB(file);
3573 cfile = file->private_data;
3574 tcon = tlink_tcon(cfile->tlink);
3576 if (!tcon->ses->server->ops->async_writev)
3579 ctx = cifs_aio_ctx_alloc();
3583 ctx->cfile = cifsFileInfo_get(cfile);
3585 if (!is_sync_kiocb(iocb))
3588 ctx->pos = iocb->ki_pos;
3589 ctx->direct_io = direct;
3590 ctx->nr_pinned_pages = 0;
3592 if (user_backed_iter(from)) {
3594 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
3595 * they contain references to the calling process's virtual
3596 * memory layout which won't be available in an async worker
3597 * thread. This also takes a pin on every folio involved.
3599 rc = netfs_extract_user_iter(from, iov_iter_count(from),
3602 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3606 ctx->nr_pinned_pages = rc;
3607 ctx->bv = (void *)ctx->iter.bvec;
3608 ctx->bv_need_unpin = iov_iter_extract_will_pin(&ctx->iter);
3609 } else if ((iov_iter_is_bvec(from) || iov_iter_is_kvec(from)) &&
3610 !is_sync_kiocb(iocb)) {
3612 * If the op is asynchronous, we need to copy the list attached
3613 * to a BVEC/KVEC-type iterator, but we assume that the storage
3614 * will be pinned by the caller; in any case, we may or may not
3615 * be able to pin the pages, so we don't try.
3617 ctx->bv = (void *)dup_iter(&ctx->iter, from, GFP_KERNEL);
3619 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3624 * Otherwise, we just pass the iterator down as-is and rely on
3625 * the caller to make sure the pages referred to by the
3626 * iterator don't evaporate.
3631 ctx->len = iov_iter_count(&ctx->iter);
3633 /* grab a lock here due to read response handlers can access ctx */
3634 mutex_lock(&ctx->aio_mutex);
3636 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &ctx->iter,
3637 cfile, cifs_sb, &ctx->list, ctx);
3640 * If at least one write was successfully sent, then discard any rc
3641 * value from the later writes. If the other write succeeds, then
3642 * we'll end up returning whatever was written. If it fails, then
3643 * we'll get a new rc value from that.
3645 if (!list_empty(&ctx->list))
3648 mutex_unlock(&ctx->aio_mutex);
3651 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3655 if (!is_sync_kiocb(iocb)) {
3656 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3657 return -EIOCBQUEUED;
3660 rc = wait_for_completion_killable(&ctx->done);
3662 mutex_lock(&ctx->aio_mutex);
3663 ctx->rc = rc = -EINTR;
3664 total_written = ctx->total_len;
3665 mutex_unlock(&ctx->aio_mutex);
3668 total_written = ctx->total_len;
3671 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3673 if (unlikely(!total_written))
3676 iocb->ki_pos += total_written;
3677 return total_written;
3680 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3682 struct file *file = iocb->ki_filp;
3684 cifs_revalidate_mapping(file->f_inode);
3685 return __cifs_writev(iocb, from, true);
3688 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3690 return __cifs_writev(iocb, from, false);
3694 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3696 struct file *file = iocb->ki_filp;
3697 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3698 struct inode *inode = file->f_mapping->host;
3699 struct cifsInodeInfo *cinode = CIFS_I(inode);
3700 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3705 * We need to hold the sem to be sure nobody modifies lock list
3706 * with a brlock that prevents writing.
3708 down_read(&cinode->lock_sem);
3710 rc = generic_write_checks(iocb, from);
3714 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3715 server->vals->exclusive_lock_type, 0,
3716 NULL, CIFS_WRITE_OP))
3717 rc = __generic_file_write_iter(iocb, from);
3721 up_read(&cinode->lock_sem);
3722 inode_unlock(inode);
3725 rc = generic_write_sync(iocb, rc);
3730 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3732 struct inode *inode = file_inode(iocb->ki_filp);
3733 struct cifsInodeInfo *cinode = CIFS_I(inode);
3734 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3735 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3736 iocb->ki_filp->private_data;
3737 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3740 written = cifs_get_writer(cinode);
3744 if (CIFS_CACHE_WRITE(cinode)) {
3745 if (cap_unix(tcon->ses) &&
3746 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3747 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3748 written = generic_file_write_iter(iocb, from);
3751 written = cifs_writev(iocb, from);
3755 * For non-oplocked files in strict cache mode we need to write the data
3756 * to the server exactly from the pos to pos+len-1 rather than flush all
3757 * affected pages because it may cause a error with mandatory locks on
3758 * these pages but not on the region from pos to ppos+len-1.
3760 written = cifs_user_writev(iocb, from);
3761 if (CIFS_CACHE_READ(cinode)) {
3763 * We have read level caching and we have just sent a write
3764 * request to the server thus making data in the cache stale.
3765 * Zap the cache and set oplock/lease level to NONE to avoid
3766 * reading stale data from the cache. All subsequent read
3767 * operations will read new data from the server.
3769 cifs_zap_mapping(inode);
3770 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3775 cifs_put_writer(cinode);
3779 static struct cifs_readdata *cifs_readdata_alloc(work_func_t complete)
3781 struct cifs_readdata *rdata;
3783 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3785 kref_init(&rdata->refcount);
3786 INIT_LIST_HEAD(&rdata->list);
3787 init_completion(&rdata->done);
3788 INIT_WORK(&rdata->work, complete);
3795 cifs_readdata_release(struct kref *refcount)
3797 struct cifs_readdata *rdata = container_of(refcount,
3798 struct cifs_readdata, refcount);
3801 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3802 #ifdef CONFIG_CIFS_SMB_DIRECT
3804 smbd_deregister_mr(rdata->mr);
3809 cifsFileInfo_put(rdata->cfile);
3814 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3817 cifs_uncached_readv_complete(struct work_struct *work)
3819 struct cifs_readdata *rdata = container_of(work,
3820 struct cifs_readdata, work);
3822 complete(&rdata->done);
3823 collect_uncached_read_data(rdata->ctx);
3824 /* the below call can possibly free the last ref to aio ctx */
3825 kref_put(&rdata->refcount, cifs_readdata_release);
3828 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3829 struct list_head *rdata_list,
3830 struct cifs_aio_ctx *ctx)
3833 struct cifs_credits credits;
3835 struct TCP_Server_Info *server;
3837 /* XXX: should we pick a new channel here? */
3838 server = rdata->server;
3841 if (rdata->cfile->invalidHandle) {
3842 rc = cifs_reopen_file(rdata->cfile, true);
3850 * Wait for credits to resend this rdata.
3851 * Note: we are attempting to resend the whole rdata not in
3855 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3861 if (rsize < rdata->bytes) {
3862 add_credits_and_wake_if(server, &credits, 0);
3865 } while (rsize < rdata->bytes);
3866 rdata->credits = credits;
3868 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3870 if (rdata->cfile->invalidHandle)
3873 #ifdef CONFIG_CIFS_SMB_DIRECT
3875 rdata->mr->need_invalidate = true;
3876 smbd_deregister_mr(rdata->mr);
3880 rc = server->ops->async_readv(rdata);
3884 /* If the read was successfully sent, we are done */
3886 /* Add to aio pending list */
3887 list_add_tail(&rdata->list, rdata_list);
3891 /* Roll back credits and retry if needed */
3892 add_credits_and_wake_if(server, &rdata->credits, 0);
3893 } while (rc == -EAGAIN);
3896 kref_put(&rdata->refcount, cifs_readdata_release);
3901 cifs_send_async_read(loff_t fpos, size_t len, struct cifsFileInfo *open_file,
3902 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3903 struct cifs_aio_ctx *ctx)
3905 struct cifs_readdata *rdata;
3906 unsigned int rsize, nsegs, max_segs = INT_MAX;
3907 struct cifs_credits credits_on_stack;
3908 struct cifs_credits *credits = &credits_on_stack;
3909 size_t cur_len, max_len;
3912 struct TCP_Server_Info *server;
3914 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3916 #ifdef CONFIG_CIFS_SMB_DIRECT
3917 if (server->smbd_conn)
3918 max_segs = server->smbd_conn->max_frmr_depth;
3921 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3922 pid = open_file->pid;
3924 pid = current->tgid;
3927 if (open_file->invalidHandle) {
3928 rc = cifs_reopen_file(open_file, true);
3935 if (cifs_sb->ctx->rsize == 0)
3936 cifs_sb->ctx->rsize =
3937 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
3940 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
3945 max_len = min_t(size_t, len, rsize);
3947 cur_len = cifs_limit_bvec_subset(&ctx->iter, max_len,
3949 cifs_dbg(FYI, "read-to-iter len=%zx/%zx nsegs=%u/%lu/%u\n",
3950 cur_len, max_len, nsegs, ctx->iter.nr_segs, max_segs);
3953 add_credits_and_wake_if(server, credits, 0);
3957 rdata = cifs_readdata_alloc(cifs_uncached_readv_complete);
3959 add_credits_and_wake_if(server, credits, 0);
3964 rdata->server = server;
3965 rdata->cfile = cifsFileInfo_get(open_file);
3966 rdata->offset = fpos;
3967 rdata->bytes = cur_len;
3969 rdata->credits = credits_on_stack;
3971 kref_get(&ctx->refcount);
3973 rdata->iter = ctx->iter;
3974 iov_iter_truncate(&rdata->iter, cur_len);
3976 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3979 if (rdata->cfile->invalidHandle)
3982 rc = server->ops->async_readv(rdata);
3986 add_credits_and_wake_if(server, &rdata->credits, 0);
3987 kref_put(&rdata->refcount, cifs_readdata_release);
3993 list_add_tail(&rdata->list, rdata_list);
3994 iov_iter_advance(&ctx->iter, cur_len);
4003 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4005 struct cifs_readdata *rdata, *tmp;
4006 struct iov_iter *to = &ctx->iter;
4007 struct cifs_sb_info *cifs_sb;
4010 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4012 mutex_lock(&ctx->aio_mutex);
4014 if (list_empty(&ctx->list)) {
4015 mutex_unlock(&ctx->aio_mutex);
4020 /* the loop below should proceed in the order of increasing offsets */
4022 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4024 if (!try_wait_for_completion(&rdata->done)) {
4025 mutex_unlock(&ctx->aio_mutex);
4029 if (rdata->result == -EAGAIN) {
4030 /* resend call if it's a retryable error */
4031 struct list_head tmp_list;
4032 unsigned int got_bytes = rdata->got_bytes;
4034 list_del_init(&rdata->list);
4035 INIT_LIST_HEAD(&tmp_list);
4037 if (ctx->direct_io) {
4039 * Re-use rdata as this is a
4042 rc = cifs_resend_rdata(
4046 rc = cifs_send_async_read(
4047 rdata->offset + got_bytes,
4048 rdata->bytes - got_bytes,
4049 rdata->cfile, cifs_sb,
4052 kref_put(&rdata->refcount,
4053 cifs_readdata_release);
4056 list_splice(&tmp_list, &ctx->list);
4059 } else if (rdata->result)
4062 /* if there was a short read -- discard anything left */
4063 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4066 ctx->total_len += rdata->got_bytes;
4068 list_del_init(&rdata->list);
4069 kref_put(&rdata->refcount, cifs_readdata_release);
4072 if (!ctx->direct_io)
4073 ctx->total_len = ctx->len - iov_iter_count(to);
4075 /* mask nodata case */
4079 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4081 mutex_unlock(&ctx->aio_mutex);
4083 if (ctx->iocb && ctx->iocb->ki_complete)
4084 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
4086 complete(&ctx->done);
4089 static ssize_t __cifs_readv(
4090 struct kiocb *iocb, struct iov_iter *to, bool direct)
4093 struct file *file = iocb->ki_filp;
4094 struct cifs_sb_info *cifs_sb;
4095 struct cifsFileInfo *cfile;
4096 struct cifs_tcon *tcon;
4097 ssize_t rc, total_read = 0;
4098 loff_t offset = iocb->ki_pos;
4099 struct cifs_aio_ctx *ctx;
4101 len = iov_iter_count(to);
4105 cifs_sb = CIFS_FILE_SB(file);
4106 cfile = file->private_data;
4107 tcon = tlink_tcon(cfile->tlink);
4109 if (!tcon->ses->server->ops->async_readv)
4112 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4113 cifs_dbg(FYI, "attempting read on write only file instance\n");
4115 ctx = cifs_aio_ctx_alloc();
4120 ctx->direct_io = direct;
4122 ctx->cfile = cifsFileInfo_get(cfile);
4123 ctx->nr_pinned_pages = 0;
4125 if (!is_sync_kiocb(iocb))
4128 if (user_backed_iter(to)) {
4130 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
4131 * they contain references to the calling process's virtual
4132 * memory layout which won't be available in an async worker
4133 * thread. This also takes a pin on every folio involved.
4135 rc = netfs_extract_user_iter(to, iov_iter_count(to),
4138 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4142 ctx->nr_pinned_pages = rc;
4143 ctx->bv = (void *)ctx->iter.bvec;
4144 ctx->bv_need_unpin = iov_iter_extract_will_pin(&ctx->iter);
4145 ctx->should_dirty = true;
4146 } else if ((iov_iter_is_bvec(to) || iov_iter_is_kvec(to)) &&
4147 !is_sync_kiocb(iocb)) {
4149 * If the op is asynchronous, we need to copy the list attached
4150 * to a BVEC/KVEC-type iterator, but we assume that the storage
4151 * will be retained by the caller; in any case, we may or may
4152 * not be able to pin the pages, so we don't try.
4154 ctx->bv = (void *)dup_iter(&ctx->iter, to, GFP_KERNEL);
4156 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4161 * Otherwise, we just pass the iterator down as-is and rely on
4162 * the caller to make sure the pages referred to by the
4163 * iterator don't evaporate.
4169 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
4170 offset, offset + len - 1);
4172 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4177 /* grab a lock here due to read response handlers can access ctx */
4178 mutex_lock(&ctx->aio_mutex);
4180 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4182 /* if at least one read request send succeeded, then reset rc */
4183 if (!list_empty(&ctx->list))
4186 mutex_unlock(&ctx->aio_mutex);
4189 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4193 if (!is_sync_kiocb(iocb)) {
4194 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4195 return -EIOCBQUEUED;
4198 rc = wait_for_completion_killable(&ctx->done);
4200 mutex_lock(&ctx->aio_mutex);
4201 ctx->rc = rc = -EINTR;
4202 total_read = ctx->total_len;
4203 mutex_unlock(&ctx->aio_mutex);
4206 total_read = ctx->total_len;
4209 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4212 iocb->ki_pos += total_read;
4218 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4220 return __cifs_readv(iocb, to, true);
4223 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4225 return __cifs_readv(iocb, to, false);
4229 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4231 struct inode *inode = file_inode(iocb->ki_filp);
4232 struct cifsInodeInfo *cinode = CIFS_I(inode);
4233 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4234 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4235 iocb->ki_filp->private_data;
4236 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4240 * In strict cache mode we need to read from the server all the time
4241 * if we don't have level II oplock because the server can delay mtime
4242 * change - so we can't make a decision about inode invalidating.
4243 * And we can also fail with pagereading if there are mandatory locks
4244 * on pages affected by this read but not on the region from pos to
4247 if (!CIFS_CACHE_READ(cinode))
4248 return cifs_user_readv(iocb, to);
4250 if (cap_unix(tcon->ses) &&
4251 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4252 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4253 return generic_file_read_iter(iocb, to);
4256 * We need to hold the sem to be sure nobody modifies lock list
4257 * with a brlock that prevents reading.
4259 down_read(&cinode->lock_sem);
4260 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4261 tcon->ses->server->vals->shared_lock_type,
4262 0, NULL, CIFS_READ_OP))
4263 rc = generic_file_read_iter(iocb, to);
4264 up_read(&cinode->lock_sem);
4269 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4272 unsigned int bytes_read = 0;
4273 unsigned int total_read;
4274 unsigned int current_read_size;
4276 struct cifs_sb_info *cifs_sb;
4277 struct cifs_tcon *tcon;
4278 struct TCP_Server_Info *server;
4281 struct cifsFileInfo *open_file;
4282 struct cifs_io_parms io_parms = {0};
4283 int buf_type = CIFS_NO_BUFFER;
4287 cifs_sb = CIFS_FILE_SB(file);
4289 /* FIXME: set up handlers for larger reads and/or convert to async */
4290 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4292 if (file->private_data == NULL) {
4297 open_file = file->private_data;
4298 tcon = tlink_tcon(open_file->tlink);
4299 server = cifs_pick_channel(tcon->ses);
4301 if (!server->ops->sync_read) {
4306 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4307 pid = open_file->pid;
4309 pid = current->tgid;
4311 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4312 cifs_dbg(FYI, "attempting read on write only file instance\n");
4314 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4315 total_read += bytes_read, cur_offset += bytes_read) {
4317 current_read_size = min_t(uint, read_size - total_read,
4320 * For windows me and 9x we do not want to request more
4321 * than it negotiated since it will refuse the read
4324 if (!(tcon->ses->capabilities &
4325 tcon->ses->server->vals->cap_large_files)) {
4326 current_read_size = min_t(uint,
4327 current_read_size, CIFSMaxBufSize);
4329 if (open_file->invalidHandle) {
4330 rc = cifs_reopen_file(open_file, true);
4335 io_parms.tcon = tcon;
4336 io_parms.offset = *offset;
4337 io_parms.length = current_read_size;
4338 io_parms.server = server;
4339 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4340 &bytes_read, &cur_offset,
4342 } while (rc == -EAGAIN);
4344 if (rc || (bytes_read == 0)) {
4352 cifs_stats_bytes_read(tcon, total_read);
4353 *offset += bytes_read;
4361 * If the page is mmap'ed into a process' page tables, then we need to make
4362 * sure that it doesn't change while being written back.
4364 static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
4366 struct folio *folio = page_folio(vmf->page);
4368 /* Wait for the folio to be written to the cache before we allow it to
4369 * be modified. We then assume the entire folio will need writing back.
4371 #ifdef CONFIG_CIFS_FSCACHE
4372 if (folio_test_fscache(folio) &&
4373 folio_wait_fscache_killable(folio) < 0)
4374 return VM_FAULT_RETRY;
4377 folio_wait_writeback(folio);
4379 if (folio_lock_killable(folio) < 0)
4380 return VM_FAULT_RETRY;
4381 return VM_FAULT_LOCKED;
4384 static const struct vm_operations_struct cifs_file_vm_ops = {
4385 .fault = filemap_fault,
4386 .map_pages = filemap_map_pages,
4387 .page_mkwrite = cifs_page_mkwrite,
4390 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4393 struct inode *inode = file_inode(file);
4397 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4398 rc = cifs_zap_mapping(inode);
4400 rc = generic_file_mmap(file, vma);
4402 vma->vm_ops = &cifs_file_vm_ops;
4408 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4414 rc = cifs_revalidate_file(file);
4416 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4419 rc = generic_file_mmap(file, vma);
4421 vma->vm_ops = &cifs_file_vm_ops;
4428 * Unlock a bunch of folios in the pagecache.
4430 static void cifs_unlock_folios(struct address_space *mapping, pgoff_t first, pgoff_t last)
4432 struct folio *folio;
4433 XA_STATE(xas, &mapping->i_pages, first);
4436 xas_for_each(&xas, folio, last) {
4437 folio_unlock(folio);
4442 static void cifs_readahead_complete(struct work_struct *work)
4444 struct cifs_readdata *rdata = container_of(work,
4445 struct cifs_readdata, work);
4446 struct folio *folio;
4448 bool good = rdata->result == 0 || (rdata->result == -EAGAIN && rdata->got_bytes);
4450 XA_STATE(xas, &rdata->mapping->i_pages, rdata->offset / PAGE_SIZE);
4453 cifs_readahead_to_fscache(rdata->mapping->host,
4454 rdata->offset, rdata->bytes);
4456 if (iov_iter_count(&rdata->iter) > 0)
4457 iov_iter_zero(iov_iter_count(&rdata->iter), &rdata->iter);
4459 last = (rdata->offset + rdata->bytes - 1) / PAGE_SIZE;
4462 xas_for_each(&xas, folio, last) {
4464 flush_dcache_folio(folio);
4465 folio_mark_uptodate(folio);
4467 folio_unlock(folio);
4471 kref_put(&rdata->refcount, cifs_readdata_release);
4474 static void cifs_readahead(struct readahead_control *ractl)
4476 struct cifsFileInfo *open_file = ractl->file->private_data;
4477 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
4478 struct TCP_Server_Info *server;
4479 unsigned int xid, nr_pages, cache_nr_pages = 0;
4480 unsigned int ra_pages;
4481 pgoff_t next_cached = ULONG_MAX, ra_index;
4482 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
4483 cifs_inode_cookie(ractl->mapping->host)->cache_priv;
4484 bool check_cache = caching;
4488 /* Note that readahead_count() lags behind our dequeuing of pages from
4489 * the ractl, wo we have to keep track for ourselves.
4491 ra_pages = readahead_count(ractl);
4492 ra_index = readahead_index(ractl);
4496 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4497 pid = open_file->pid;
4499 pid = current->tgid;
4501 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4503 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4504 __func__, ractl->file, ractl->mapping, ra_pages);
4507 * Chop the readahead request up into rsize-sized read requests.
4509 while ((nr_pages = ra_pages)) {
4510 unsigned int i, rsize;
4511 struct cifs_readdata *rdata;
4512 struct cifs_credits credits_on_stack;
4513 struct cifs_credits *credits = &credits_on_stack;
4514 struct folio *folio;
4518 * Find out if we have anything cached in the range of
4519 * interest, and if so, where the next chunk of cached data is.
4523 rc = cifs_fscache_query_occupancy(
4524 ractl->mapping->host, ra_index, nr_pages,
4525 &next_cached, &cache_nr_pages);
4528 check_cache = false;
4531 if (ra_index == next_cached) {
4533 * TODO: Send a whole batch of pages to be read
4536 folio = readahead_folio(ractl);
4537 fsize = folio_nr_pages(folio);
4540 if (cifs_readpage_from_fscache(ractl->mapping->host,
4541 &folio->page) < 0) {
4543 * TODO: Deal with cache read failure
4544 * here, but for the moment, delegate
4549 folio_unlock(folio);
4550 next_cached += fsize;
4551 cache_nr_pages -= fsize;
4552 if (cache_nr_pages == 0)
4558 if (open_file->invalidHandle) {
4559 rc = cifs_reopen_file(open_file, true);
4567 if (cifs_sb->ctx->rsize == 0)
4568 cifs_sb->ctx->rsize =
4569 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4572 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4576 nr_pages = min_t(size_t, rsize / PAGE_SIZE, ra_pages);
4577 if (next_cached != ULONG_MAX)
4578 nr_pages = min_t(size_t, nr_pages, next_cached - ra_index);
4581 * Give up immediately if rsize is too small to read an entire
4582 * page. The VFS will fall back to readpage. We should never
4583 * reach this point however since we set ra_pages to 0 when the
4584 * rsize is smaller than a cache page.
4586 if (unlikely(!nr_pages)) {
4587 add_credits_and_wake_if(server, credits, 0);
4591 rdata = cifs_readdata_alloc(cifs_readahead_complete);
4593 /* best to give up if we're out of mem */
4594 add_credits_and_wake_if(server, credits, 0);
4598 rdata->offset = ra_index * PAGE_SIZE;
4599 rdata->bytes = nr_pages * PAGE_SIZE;
4600 rdata->cfile = cifsFileInfo_get(open_file);
4601 rdata->server = server;
4602 rdata->mapping = ractl->mapping;
4604 rdata->credits = credits_on_stack;
4606 for (i = 0; i < nr_pages; i++) {
4607 if (!readahead_folio(ractl))
4610 ra_pages -= nr_pages;
4611 ra_index += nr_pages;
4613 iov_iter_xarray(&rdata->iter, ITER_DEST, &rdata->mapping->i_pages,
4614 rdata->offset, rdata->bytes);
4616 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4618 if (rdata->cfile->invalidHandle)
4621 rc = server->ops->async_readv(rdata);
4625 add_credits_and_wake_if(server, &rdata->credits, 0);
4626 cifs_unlock_folios(rdata->mapping,
4627 rdata->offset / PAGE_SIZE,
4628 (rdata->offset + rdata->bytes - 1) / PAGE_SIZE);
4629 /* Fallback to the readpage in error/reconnect cases */
4630 kref_put(&rdata->refcount, cifs_readdata_release);
4634 kref_put(&rdata->refcount, cifs_readdata_release);
4641 * cifs_readpage_worker must be called with the page pinned
4643 static int cifs_readpage_worker(struct file *file, struct page *page,
4649 /* Is the page cached? */
4650 rc = cifs_readpage_from_fscache(file_inode(file), page);
4654 read_data = kmap(page);
4655 /* for reads over a certain size could initiate async read ahead */
4657 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4662 cifs_dbg(FYI, "Bytes read %d\n", rc);
4664 /* we do not want atime to be less than mtime, it broke some apps */
4665 file_inode(file)->i_atime = current_time(file_inode(file));
4666 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4667 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4669 file_inode(file)->i_atime = current_time(file_inode(file));
4672 memset(read_data + rc, 0, PAGE_SIZE - rc);
4674 flush_dcache_page(page);
4675 SetPageUptodate(page);
4686 static int cifs_read_folio(struct file *file, struct folio *folio)
4688 struct page *page = &folio->page;
4689 loff_t offset = page_file_offset(page);
4695 if (file->private_data == NULL) {
4701 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
4702 page, (int)offset, (int)offset);
4704 rc = cifs_readpage_worker(file, page, &offset);
4710 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4712 struct cifsFileInfo *open_file;
4714 spin_lock(&cifs_inode->open_file_lock);
4715 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4716 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4717 spin_unlock(&cifs_inode->open_file_lock);
4721 spin_unlock(&cifs_inode->open_file_lock);
4725 /* We do not want to update the file size from server for inodes
4726 open for write - to avoid races with writepage extending
4727 the file - in the future we could consider allowing
4728 refreshing the inode only on increases in the file size
4729 but this is tricky to do without racing with writebehind
4730 page caching in the current Linux kernel design */
4731 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4736 if (is_inode_writable(cifsInode)) {
4737 /* This inode is open for write at least once */
4738 struct cifs_sb_info *cifs_sb;
4740 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
4741 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4742 /* since no page cache to corrupt on directio
4743 we can change size safely */
4747 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
4755 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4756 loff_t pos, unsigned len,
4757 struct page **pagep, void **fsdata)
4760 pgoff_t index = pos >> PAGE_SHIFT;
4761 loff_t offset = pos & (PAGE_SIZE - 1);
4762 loff_t page_start = pos & PAGE_MASK;
4767 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4770 page = grab_cache_page_write_begin(mapping, index);
4776 if (PageUptodate(page))
4780 * If we write a full page it will be up to date, no need to read from
4781 * the server. If the write is short, we'll end up doing a sync write
4784 if (len == PAGE_SIZE)
4788 * optimize away the read when we have an oplock, and we're not
4789 * expecting to use any of the data we'd be reading in. That
4790 * is, when the page lies beyond the EOF, or straddles the EOF
4791 * and the write will cover all of the existing data.
4793 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4794 i_size = i_size_read(mapping->host);
4795 if (page_start >= i_size ||
4796 (offset == 0 && (pos + len) >= i_size)) {
4797 zero_user_segments(page, 0, offset,
4801 * PageChecked means that the parts of the page
4802 * to which we're not writing are considered up
4803 * to date. Once the data is copied to the
4804 * page, it can be set uptodate.
4806 SetPageChecked(page);
4811 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4813 * might as well read a page, it is fast enough. If we get
4814 * an error, we don't need to return it. cifs_write_end will
4815 * do a sync write instead since PG_uptodate isn't set.
4817 cifs_readpage_worker(file, page, &page_start);
4822 /* we could try using another file handle if there is one -
4823 but how would we lock it to prevent close of that handle
4824 racing with this read? In any case
4825 this will be written out by write_end so is fine */
4832 static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
4834 if (folio_test_private(folio))
4836 if (folio_test_fscache(folio)) {
4837 if (current_is_kswapd() || !(gfp & __GFP_FS))
4839 folio_wait_fscache(folio);
4841 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
4845 static void cifs_invalidate_folio(struct folio *folio, size_t offset,
4848 folio_wait_fscache(folio);
4851 static int cifs_launder_folio(struct folio *folio)
4854 loff_t range_start = folio_pos(folio);
4855 loff_t range_end = range_start + folio_size(folio);
4856 struct writeback_control wbc = {
4857 .sync_mode = WB_SYNC_ALL,
4859 .range_start = range_start,
4860 .range_end = range_end,
4863 cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
4865 if (folio_clear_dirty_for_io(folio))
4866 rc = cifs_writepage_locked(&folio->page, &wbc);
4868 folio_wait_fscache(folio);
4872 void cifs_oplock_break(struct work_struct *work)
4874 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4876 struct inode *inode = d_inode(cfile->dentry);
4877 struct cifsInodeInfo *cinode = CIFS_I(inode);
4878 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4879 struct TCP_Server_Info *server = tcon->ses->server;
4881 bool purge_cache = false;
4883 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4884 TASK_UNINTERRUPTIBLE);
4886 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4887 cfile->oplock_epoch, &purge_cache);
4889 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4890 cifs_has_mand_locks(cinode)) {
4891 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4896 if (inode && S_ISREG(inode->i_mode)) {
4897 if (CIFS_CACHE_READ(cinode))
4898 break_lease(inode, O_RDONLY);
4900 break_lease(inode, O_WRONLY);
4901 rc = filemap_fdatawrite(inode->i_mapping);
4902 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4903 rc = filemap_fdatawait(inode->i_mapping);
4904 mapping_set_error(inode->i_mapping, rc);
4905 cifs_zap_mapping(inode);
4907 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4908 if (CIFS_CACHE_WRITE(cinode))
4909 goto oplock_break_ack;
4912 rc = cifs_push_locks(cfile);
4914 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4918 * releasing stale oplock after recent reconnect of smb session using
4919 * a now incorrect file handle is not a data integrity issue but do
4920 * not bother sending an oplock release if session to server still is
4921 * disconnected since oplock already released by the server
4923 if (!cfile->oplock_break_cancelled) {
4924 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
4926 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
4929 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4930 cifs_done_oplock_break(cinode);
4934 * The presence of cifs_direct_io() in the address space ops vector
4935 * allowes open() O_DIRECT flags which would have failed otherwise.
4937 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
4938 * so this method should never be called.
4940 * Direct IO is not yet supported in the cached mode.
4943 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
4947 * Eventually need to support direct IO for non forcedirectio mounts
4952 static int cifs_swap_activate(struct swap_info_struct *sis,
4953 struct file *swap_file, sector_t *span)
4955 struct cifsFileInfo *cfile = swap_file->private_data;
4956 struct inode *inode = swap_file->f_mapping->host;
4957 unsigned long blocks;
4960 cifs_dbg(FYI, "swap activate\n");
4962 if (!swap_file->f_mapping->a_ops->swap_rw)
4963 /* Cannot support swap */
4966 spin_lock(&inode->i_lock);
4967 blocks = inode->i_blocks;
4968 isize = inode->i_size;
4969 spin_unlock(&inode->i_lock);
4970 if (blocks*512 < isize) {
4971 pr_warn("swap activate: swapfile has holes\n");
4976 pr_warn_once("Swap support over SMB3 is experimental\n");
4979 * TODO: consider adding ACL (or documenting how) to prevent other
4980 * users (on this or other systems) from reading it
4984 /* TODO: add sk_set_memalloc(inet) or similar */
4987 cfile->swapfile = true;
4989 * TODO: Since file already open, we can't open with DENY_ALL here
4990 * but we could add call to grab a byte range lock to prevent others
4991 * from reading or writing the file
4994 sis->flags |= SWP_FS_OPS;
4995 return add_swap_extent(sis, 0, sis->max, 0);
4998 static void cifs_swap_deactivate(struct file *file)
5000 struct cifsFileInfo *cfile = file->private_data;
5002 cifs_dbg(FYI, "swap deactivate\n");
5004 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5007 cfile->swapfile = false;
5009 /* do we need to unpin (or unlock) the file */
5013 * Mark a page as having been made dirty and thus needing writeback. We also
5014 * need to pin the cache object to write back to.
5016 #ifdef CONFIG_CIFS_FSCACHE
5017 static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio)
5019 return fscache_dirty_folio(mapping, folio,
5020 cifs_inode_cookie(mapping->host));
5023 #define cifs_dirty_folio filemap_dirty_folio
5026 const struct address_space_operations cifs_addr_ops = {
5027 .read_folio = cifs_read_folio,
5028 .readahead = cifs_readahead,
5029 .writepages = cifs_writepages,
5030 .write_begin = cifs_write_begin,
5031 .write_end = cifs_write_end,
5032 .dirty_folio = cifs_dirty_folio,
5033 .release_folio = cifs_release_folio,
5034 .direct_IO = cifs_direct_io,
5035 .invalidate_folio = cifs_invalidate_folio,
5036 .launder_folio = cifs_launder_folio,
5037 .migrate_folio = filemap_migrate_folio,
5039 * TODO: investigate and if useful we could add an is_dirty_writeback
5042 .swap_activate = cifs_swap_activate,
5043 .swap_deactivate = cifs_swap_deactivate,
5047 * cifs_readahead requires the server to support a buffer large enough to
5048 * contain the header plus one complete page of data. Otherwise, we need
5049 * to leave cifs_readahead out of the address space operations.
5051 const struct address_space_operations cifs_addr_ops_smallbuf = {
5052 .read_folio = cifs_read_folio,
5053 .writepages = cifs_writepages,
5054 .write_begin = cifs_write_begin,
5055 .write_end = cifs_write_end,
5056 .dirty_folio = cifs_dirty_folio,
5057 .release_folio = cifs_release_folio,
5058 .invalidate_folio = cifs_invalidate_folio,
5059 .launder_folio = cifs_launder_folio,
5060 .migrate_folio = filemap_migrate_folio,
5064 * Splice data from a file into a pipe.
5066 ssize_t cifs_splice_read(struct file *in, loff_t *ppos,
5067 struct pipe_inode_info *pipe, size_t len,
5070 if (unlikely(*ppos >= file_inode(in)->i_sb->s_maxbytes))
5074 if (in->f_flags & O_DIRECT)
5075 return direct_splice_read(in, ppos, pipe, len, flags);
5076 return filemap_splice_read(in, ppos, pipe, len, flags);