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) {
55 if (xas_retry(&xas, folio))
60 folio_clear_dirty_for_io(folio);
69 * Completion of write to server.
71 void cifs_pages_written_back(struct inode *inode, loff_t start, unsigned int len)
73 struct address_space *mapping = inode->i_mapping;
77 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
84 end = (start + len - 1) / PAGE_SIZE;
85 xas_for_each(&xas, folio, end) {
86 if (xas_retry(&xas, folio))
88 if (!folio_test_writeback(folio)) {
89 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
90 len, start, folio->index, end);
94 folio_detach_private(folio);
95 folio_end_writeback(folio);
102 * Failure of write to server.
104 void cifs_pages_write_failed(struct inode *inode, loff_t start, unsigned int len)
106 struct address_space *mapping = inode->i_mapping;
110 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
117 end = (start + len - 1) / PAGE_SIZE;
118 xas_for_each(&xas, folio, end) {
119 if (xas_retry(&xas, folio))
121 if (!folio_test_writeback(folio)) {
122 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
123 len, start, folio->index, end);
127 folio_set_error(folio);
128 folio_end_writeback(folio);
135 * Redirty pages after a temporary failure.
137 void cifs_pages_write_redirty(struct inode *inode, loff_t start, unsigned int len)
139 struct address_space *mapping = inode->i_mapping;
143 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
150 end = (start + len - 1) / PAGE_SIZE;
151 xas_for_each(&xas, folio, end) {
152 if (!folio_test_writeback(folio)) {
153 WARN_ONCE(1, "bad %x @%llx page %lx %lx\n",
154 len, start, folio->index, end);
158 filemap_dirty_folio(folio->mapping, folio);
159 folio_end_writeback(folio);
166 * Mark as invalid, all open files on tree connections since they
167 * were closed when session to server was lost.
170 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
172 struct cifsFileInfo *open_file = NULL;
173 struct list_head *tmp;
174 struct list_head *tmp1;
176 /* only send once per connect */
177 spin_lock(&tcon->tc_lock);
178 if (tcon->need_reconnect)
179 tcon->status = TID_NEED_RECON;
181 if (tcon->status != TID_NEED_RECON) {
182 spin_unlock(&tcon->tc_lock);
185 tcon->status = TID_IN_FILES_INVALIDATE;
186 spin_unlock(&tcon->tc_lock);
188 /* list all files open on tree connection and mark them invalid */
189 spin_lock(&tcon->open_file_lock);
190 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
191 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
192 open_file->invalidHandle = true;
193 open_file->oplock_break_cancelled = true;
195 spin_unlock(&tcon->open_file_lock);
197 invalidate_all_cached_dirs(tcon);
198 spin_lock(&tcon->tc_lock);
199 if (tcon->status == TID_IN_FILES_INVALIDATE)
200 tcon->status = TID_NEED_TCON;
201 spin_unlock(&tcon->tc_lock);
204 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
209 static inline int cifs_convert_flags(unsigned int flags)
211 if ((flags & O_ACCMODE) == O_RDONLY)
213 else if ((flags & O_ACCMODE) == O_WRONLY)
214 return GENERIC_WRITE;
215 else if ((flags & O_ACCMODE) == O_RDWR) {
216 /* GENERIC_ALL is too much permission to request
217 can cause unnecessary access denied on create */
218 /* return GENERIC_ALL; */
219 return (GENERIC_READ | GENERIC_WRITE);
222 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
223 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
227 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
228 static u32 cifs_posix_convert_flags(unsigned int flags)
232 if ((flags & O_ACCMODE) == O_RDONLY)
233 posix_flags = SMB_O_RDONLY;
234 else if ((flags & O_ACCMODE) == O_WRONLY)
235 posix_flags = SMB_O_WRONLY;
236 else if ((flags & O_ACCMODE) == O_RDWR)
237 posix_flags = SMB_O_RDWR;
239 if (flags & O_CREAT) {
240 posix_flags |= SMB_O_CREAT;
242 posix_flags |= SMB_O_EXCL;
243 } else if (flags & O_EXCL)
244 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
245 current->comm, current->tgid);
248 posix_flags |= SMB_O_TRUNC;
249 /* be safe and imply O_SYNC for O_DSYNC */
251 posix_flags |= SMB_O_SYNC;
252 if (flags & O_DIRECTORY)
253 posix_flags |= SMB_O_DIRECTORY;
254 if (flags & O_NOFOLLOW)
255 posix_flags |= SMB_O_NOFOLLOW;
256 if (flags & O_DIRECT)
257 posix_flags |= SMB_O_DIRECT;
261 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
263 static inline int cifs_get_disposition(unsigned int flags)
265 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
267 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
268 return FILE_OVERWRITE_IF;
269 else if ((flags & O_CREAT) == O_CREAT)
271 else if ((flags & O_TRUNC) == O_TRUNC)
272 return FILE_OVERWRITE;
277 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
278 int cifs_posix_open(const char *full_path, struct inode **pinode,
279 struct super_block *sb, int mode, unsigned int f_flags,
280 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
283 FILE_UNIX_BASIC_INFO *presp_data;
284 __u32 posix_flags = 0;
285 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
286 struct cifs_fattr fattr;
287 struct tcon_link *tlink;
288 struct cifs_tcon *tcon;
290 cifs_dbg(FYI, "posix open %s\n", full_path);
292 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
293 if (presp_data == NULL)
296 tlink = cifs_sb_tlink(cifs_sb);
302 tcon = tlink_tcon(tlink);
303 mode &= ~current_umask();
305 posix_flags = cifs_posix_convert_flags(f_flags);
306 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
307 poplock, full_path, cifs_sb->local_nls,
308 cifs_remap(cifs_sb));
309 cifs_put_tlink(tlink);
314 if (presp_data->Type == cpu_to_le32(-1))
315 goto posix_open_ret; /* open ok, caller does qpathinfo */
318 goto posix_open_ret; /* caller does not need info */
320 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
322 /* get new inode and set it up */
323 if (*pinode == NULL) {
324 cifs_fill_uniqueid(sb, &fattr);
325 *pinode = cifs_iget(sb, &fattr);
331 cifs_revalidate_mapping(*pinode);
332 rc = cifs_fattr_to_inode(*pinode, &fattr, false);
339 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
341 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
342 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
343 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
348 int create_options = CREATE_NOT_DIR;
349 struct TCP_Server_Info *server = tcon->ses->server;
350 struct cifs_open_parms oparms;
352 if (!server->ops->open)
355 desired_access = cifs_convert_flags(f_flags);
357 /*********************************************************************
358 * open flag mapping table:
360 * POSIX Flag CIFS Disposition
361 * ---------- ----------------
362 * O_CREAT FILE_OPEN_IF
363 * O_CREAT | O_EXCL FILE_CREATE
364 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
365 * O_TRUNC FILE_OVERWRITE
366 * none of the above FILE_OPEN
368 * Note that there is not a direct match between disposition
369 * FILE_SUPERSEDE (ie create whether or not file exists although
370 * O_CREAT | O_TRUNC is similar but truncates the existing
371 * file rather than creating a new file as FILE_SUPERSEDE does
372 * (which uses the attributes / metadata passed in on open call)
374 *? O_SYNC is a reasonable match to CIFS writethrough flag
375 *? and the read write flags match reasonably. O_LARGEFILE
376 *? is irrelevant because largefile support is always used
377 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
378 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
379 *********************************************************************/
381 disposition = cifs_get_disposition(f_flags);
383 /* BB pass O_SYNC flag through on file attributes .. BB */
385 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
386 if (f_flags & O_SYNC)
387 create_options |= CREATE_WRITE_THROUGH;
389 if (f_flags & O_DIRECT)
390 create_options |= CREATE_NO_BUFFER;
392 oparms = (struct cifs_open_parms) {
395 .desired_access = desired_access,
396 .create_options = cifs_create_options(cifs_sb, create_options),
397 .disposition = disposition,
402 rc = server->ops->open(xid, &oparms, oplock, buf);
406 /* TODO: Add support for calling posix query info but with passing in fid */
408 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
411 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
415 server->ops->close(xid, tcon, fid);
424 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
426 struct cifs_fid_locks *cur;
427 bool has_locks = false;
429 down_read(&cinode->lock_sem);
430 list_for_each_entry(cur, &cinode->llist, llist) {
431 if (!list_empty(&cur->locks)) {
436 up_read(&cinode->lock_sem);
441 cifs_down_write(struct rw_semaphore *sem)
443 while (!down_write_trylock(sem))
447 static void cifsFileInfo_put_work(struct work_struct *work);
449 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
450 struct tcon_link *tlink, __u32 oplock,
451 const char *symlink_target)
453 struct dentry *dentry = file_dentry(file);
454 struct inode *inode = d_inode(dentry);
455 struct cifsInodeInfo *cinode = CIFS_I(inode);
456 struct cifsFileInfo *cfile;
457 struct cifs_fid_locks *fdlocks;
458 struct cifs_tcon *tcon = tlink_tcon(tlink);
459 struct TCP_Server_Info *server = tcon->ses->server;
461 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
465 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
471 if (symlink_target) {
472 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
473 if (!cfile->symlink_target) {
480 INIT_LIST_HEAD(&fdlocks->locks);
481 fdlocks->cfile = cfile;
482 cfile->llist = fdlocks;
485 cfile->pid = current->tgid;
486 cfile->uid = current_fsuid();
487 cfile->dentry = dget(dentry);
488 cfile->f_flags = file->f_flags;
489 cfile->status_file_deleted = false;
490 cfile->invalidHandle = false;
491 cfile->deferred_close_scheduled = false;
492 cfile->tlink = cifs_get_tlink(tlink);
493 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
494 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
495 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
496 mutex_init(&cfile->fh_mutex);
497 spin_lock_init(&cfile->file_info_lock);
499 cifs_sb_active(inode->i_sb);
502 * If the server returned a read oplock and we have mandatory brlocks,
503 * set oplock level to None.
505 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
506 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
510 cifs_down_write(&cinode->lock_sem);
511 list_add(&fdlocks->llist, &cinode->llist);
512 up_write(&cinode->lock_sem);
514 spin_lock(&tcon->open_file_lock);
515 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
516 oplock = fid->pending_open->oplock;
517 list_del(&fid->pending_open->olist);
519 fid->purge_cache = false;
520 server->ops->set_fid(cfile, fid, oplock);
522 list_add(&cfile->tlist, &tcon->openFileList);
523 atomic_inc(&tcon->num_local_opens);
525 /* if readable file instance put first in list*/
526 spin_lock(&cinode->open_file_lock);
527 if (file->f_mode & FMODE_READ)
528 list_add(&cfile->flist, &cinode->openFileList);
530 list_add_tail(&cfile->flist, &cinode->openFileList);
531 spin_unlock(&cinode->open_file_lock);
532 spin_unlock(&tcon->open_file_lock);
534 if (fid->purge_cache)
535 cifs_zap_mapping(inode);
537 file->private_data = cfile;
541 struct cifsFileInfo *
542 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
544 spin_lock(&cifs_file->file_info_lock);
545 cifsFileInfo_get_locked(cifs_file);
546 spin_unlock(&cifs_file->file_info_lock);
550 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
552 struct inode *inode = d_inode(cifs_file->dentry);
553 struct cifsInodeInfo *cifsi = CIFS_I(inode);
554 struct cifsLockInfo *li, *tmp;
555 struct super_block *sb = inode->i_sb;
558 * Delete any outstanding lock records. We'll lose them when the file
561 cifs_down_write(&cifsi->lock_sem);
562 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
563 list_del(&li->llist);
564 cifs_del_lock_waiters(li);
567 list_del(&cifs_file->llist->llist);
568 kfree(cifs_file->llist);
569 up_write(&cifsi->lock_sem);
571 cifs_put_tlink(cifs_file->tlink);
572 dput(cifs_file->dentry);
573 cifs_sb_deactive(sb);
574 kfree(cifs_file->symlink_target);
578 static void cifsFileInfo_put_work(struct work_struct *work)
580 struct cifsFileInfo *cifs_file = container_of(work,
581 struct cifsFileInfo, put);
583 cifsFileInfo_put_final(cifs_file);
587 * cifsFileInfo_put - release a reference of file priv data
589 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
591 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
593 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
595 _cifsFileInfo_put(cifs_file, true, true);
599 * _cifsFileInfo_put - release a reference of file priv data
601 * This may involve closing the filehandle @cifs_file out on the
602 * server. Must be called without holding tcon->open_file_lock,
603 * cinode->open_file_lock and cifs_file->file_info_lock.
605 * If @wait_for_oplock_handler is true and we are releasing the last
606 * reference, wait for any running oplock break handler of the file
607 * and cancel any pending one.
609 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
610 * @wait_oplock_handler: must be false if called from oplock_break_handler
611 * @offload: not offloaded on close and oplock breaks
614 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
615 bool wait_oplock_handler, bool offload)
617 struct inode *inode = d_inode(cifs_file->dentry);
618 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
619 struct TCP_Server_Info *server = tcon->ses->server;
620 struct cifsInodeInfo *cifsi = CIFS_I(inode);
621 struct super_block *sb = inode->i_sb;
622 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
623 struct cifs_fid fid = {};
624 struct cifs_pending_open open;
625 bool oplock_break_cancelled;
627 spin_lock(&tcon->open_file_lock);
628 spin_lock(&cifsi->open_file_lock);
629 spin_lock(&cifs_file->file_info_lock);
630 if (--cifs_file->count > 0) {
631 spin_unlock(&cifs_file->file_info_lock);
632 spin_unlock(&cifsi->open_file_lock);
633 spin_unlock(&tcon->open_file_lock);
636 spin_unlock(&cifs_file->file_info_lock);
638 if (server->ops->get_lease_key)
639 server->ops->get_lease_key(inode, &fid);
641 /* store open in pending opens to make sure we don't miss lease break */
642 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
644 /* remove it from the lists */
645 list_del(&cifs_file->flist);
646 list_del(&cifs_file->tlist);
647 atomic_dec(&tcon->num_local_opens);
649 if (list_empty(&cifsi->openFileList)) {
650 cifs_dbg(FYI, "closing last open instance for inode %p\n",
651 d_inode(cifs_file->dentry));
653 * In strict cache mode we need invalidate mapping on the last
654 * close because it may cause a error when we open this file
655 * again and get at least level II oplock.
657 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
658 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
659 cifs_set_oplock_level(cifsi, 0);
662 spin_unlock(&cifsi->open_file_lock);
663 spin_unlock(&tcon->open_file_lock);
665 oplock_break_cancelled = wait_oplock_handler ?
666 cancel_work_sync(&cifs_file->oplock_break) : false;
668 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
669 struct TCP_Server_Info *server = tcon->ses->server;
673 if (server->ops->close_getattr)
674 server->ops->close_getattr(xid, tcon, cifs_file);
675 else if (server->ops->close)
676 server->ops->close(xid, tcon, &cifs_file->fid);
680 if (oplock_break_cancelled)
681 cifs_done_oplock_break(cifsi);
683 cifs_del_pending_open(&open);
686 queue_work(fileinfo_put_wq, &cifs_file->put);
688 cifsFileInfo_put_final(cifs_file);
691 int cifs_open(struct inode *inode, struct file *file)
697 struct cifs_sb_info *cifs_sb;
698 struct TCP_Server_Info *server;
699 struct cifs_tcon *tcon;
700 struct tcon_link *tlink;
701 struct cifsFileInfo *cfile = NULL;
703 const char *full_path;
704 bool posix_open_ok = false;
705 struct cifs_fid fid = {};
706 struct cifs_pending_open open;
707 struct cifs_open_info_data data = {};
711 cifs_sb = CIFS_SB(inode->i_sb);
712 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
717 tlink = cifs_sb_tlink(cifs_sb);
720 return PTR_ERR(tlink);
722 tcon = tlink_tcon(tlink);
723 server = tcon->ses->server;
725 page = alloc_dentry_path();
726 full_path = build_path_from_dentry(file_dentry(file), page);
727 if (IS_ERR(full_path)) {
728 rc = PTR_ERR(full_path);
732 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
733 inode, file->f_flags, full_path);
735 if (file->f_flags & O_DIRECT &&
736 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
737 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
738 file->f_op = &cifs_file_direct_nobrl_ops;
740 file->f_op = &cifs_file_direct_ops;
743 /* Get the cached handle as SMB2 close is deferred */
744 rc = cifs_get_readable_path(tcon, full_path, &cfile);
746 if (file->f_flags == cfile->f_flags) {
747 file->private_data = cfile;
748 spin_lock(&CIFS_I(inode)->deferred_lock);
749 cifs_del_deferred_close(cfile);
750 spin_unlock(&CIFS_I(inode)->deferred_lock);
753 _cifsFileInfo_put(cfile, true, false);
762 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
763 if (!tcon->broken_posix_open && tcon->unix_ext &&
764 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
765 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
766 /* can not refresh inode info since size could be stale */
767 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
768 cifs_sb->ctx->file_mode /* ignored */,
769 file->f_flags, &oplock, &fid.netfid, xid);
771 cifs_dbg(FYI, "posix open succeeded\n");
772 posix_open_ok = true;
773 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
774 if (tcon->ses->serverNOS)
775 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",
777 tcon->ses->serverNOS);
778 tcon->broken_posix_open = true;
779 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
780 (rc != -EOPNOTSUPP)) /* path not found or net err */
783 * Else fallthrough to retry open the old way on network i/o
787 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
789 if (server->ops->get_lease_key)
790 server->ops->get_lease_key(inode, &fid);
792 cifs_add_pending_open(&fid, tlink, &open);
794 if (!posix_open_ok) {
795 if (server->ops->get_lease_key)
796 server->ops->get_lease_key(inode, &fid);
798 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
801 cifs_del_pending_open(&open);
806 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
808 if (server->ops->close)
809 server->ops->close(xid, tcon, &fid);
810 cifs_del_pending_open(&open);
815 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
816 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
818 * Time to set mode which we can not set earlier due to
819 * problems creating new read-only files.
821 struct cifs_unix_set_info_args args = {
822 .mode = inode->i_mode,
823 .uid = INVALID_UID, /* no change */
824 .gid = INVALID_GID, /* no change */
825 .ctime = NO_CHANGE_64,
826 .atime = NO_CHANGE_64,
827 .mtime = NO_CHANGE_64,
830 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
833 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
836 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
837 file->f_mode & FMODE_WRITE);
838 if (file->f_flags & O_DIRECT &&
839 (!((file->f_flags & O_ACCMODE) != O_RDONLY) ||
840 file->f_flags & O_APPEND))
841 cifs_invalidate_cache(file_inode(file),
842 FSCACHE_INVAL_DIO_WRITE);
845 free_dentry_path(page);
847 cifs_put_tlink(tlink);
848 cifs_free_open_info(&data);
852 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
853 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
854 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
857 * Try to reacquire byte range locks that were released when session
858 * to server was lost.
861 cifs_relock_file(struct cifsFileInfo *cfile)
863 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
864 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
866 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
867 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
868 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
870 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
871 if (cinode->can_cache_brlcks) {
872 /* can cache locks - no need to relock */
873 up_read(&cinode->lock_sem);
877 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
878 if (cap_unix(tcon->ses) &&
879 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
880 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
881 rc = cifs_push_posix_locks(cfile);
883 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
884 rc = tcon->ses->server->ops->push_mand_locks(cfile);
886 up_read(&cinode->lock_sem);
891 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
896 struct cifs_sb_info *cifs_sb;
897 struct cifs_tcon *tcon;
898 struct TCP_Server_Info *server;
899 struct cifsInodeInfo *cinode;
902 const char *full_path;
904 int disposition = FILE_OPEN;
905 int create_options = CREATE_NOT_DIR;
906 struct cifs_open_parms oparms;
909 mutex_lock(&cfile->fh_mutex);
910 if (!cfile->invalidHandle) {
911 mutex_unlock(&cfile->fh_mutex);
916 inode = d_inode(cfile->dentry);
917 cifs_sb = CIFS_SB(inode->i_sb);
918 tcon = tlink_tcon(cfile->tlink);
919 server = tcon->ses->server;
922 * Can not grab rename sem here because various ops, including those
923 * that already have the rename sem can end up causing writepage to get
924 * called and if the server was down that means we end up here, and we
925 * can never tell if the caller already has the rename_sem.
927 page = alloc_dentry_path();
928 full_path = build_path_from_dentry(cfile->dentry, page);
929 if (IS_ERR(full_path)) {
930 mutex_unlock(&cfile->fh_mutex);
931 free_dentry_path(page);
933 return PTR_ERR(full_path);
936 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
937 inode, cfile->f_flags, full_path);
939 if (tcon->ses->server->oplocks)
944 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
945 if (tcon->unix_ext && cap_unix(tcon->ses) &&
946 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
947 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
949 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
950 * original open. Must mask them off for a reopen.
952 unsigned int oflags = cfile->f_flags &
953 ~(O_CREAT | O_EXCL | O_TRUNC);
955 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
956 cifs_sb->ctx->file_mode /* ignored */,
957 oflags, &oplock, &cfile->fid.netfid, xid);
959 cifs_dbg(FYI, "posix reopen succeeded\n");
960 oparms.reconnect = true;
964 * fallthrough to retry open the old way on errors, especially
965 * in the reconnect path it is important to retry hard
968 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
970 desired_access = cifs_convert_flags(cfile->f_flags);
972 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
973 if (cfile->f_flags & O_SYNC)
974 create_options |= CREATE_WRITE_THROUGH;
976 if (cfile->f_flags & O_DIRECT)
977 create_options |= CREATE_NO_BUFFER;
979 if (server->ops->get_lease_key)
980 server->ops->get_lease_key(inode, &cfile->fid);
982 oparms = (struct cifs_open_parms) {
985 .desired_access = desired_access,
986 .create_options = cifs_create_options(cifs_sb, create_options),
987 .disposition = disposition,
994 * Can not refresh inode by passing in file_info buf to be returned by
995 * ops->open and then calling get_inode_info with returned buf since
996 * file might have write behind data that needs to be flushed and server
997 * version of file size can be stale. If we knew for sure that inode was
998 * not dirty locally we could do this.
1000 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1001 if (rc == -ENOENT && oparms.reconnect == false) {
1002 /* durable handle timeout is expired - open the file again */
1003 rc = server->ops->open(xid, &oparms, &oplock, NULL);
1004 /* indicate that we need to relock the file */
1005 oparms.reconnect = true;
1009 mutex_unlock(&cfile->fh_mutex);
1010 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
1011 cifs_dbg(FYI, "oplock: %d\n", oplock);
1012 goto reopen_error_exit;
1015 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1017 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1018 cfile->invalidHandle = false;
1019 mutex_unlock(&cfile->fh_mutex);
1020 cinode = CIFS_I(inode);
1023 rc = filemap_write_and_wait(inode->i_mapping);
1024 if (!is_interrupt_error(rc))
1025 mapping_set_error(inode->i_mapping, rc);
1027 if (tcon->posix_extensions) {
1028 rc = smb311_posix_get_inode_info(&inode, full_path,
1029 NULL, inode->i_sb, xid);
1030 } else if (tcon->unix_ext) {
1031 rc = cifs_get_inode_info_unix(&inode, full_path,
1034 rc = cifs_get_inode_info(&inode, full_path, NULL,
1035 inode->i_sb, xid, NULL);
1039 * Else we are writing out data to server already and could deadlock if
1040 * we tried to flush data, and since we do not know if we have data that
1041 * would invalidate the current end of file on the server we can not go
1042 * to the server to get the new inode info.
1046 * If the server returned a read oplock and we have mandatory brlocks,
1047 * set oplock level to None.
1049 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
1050 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
1054 server->ops->set_fid(cfile, &cfile->fid, oplock);
1055 if (oparms.reconnect)
1056 cifs_relock_file(cfile);
1059 free_dentry_path(page);
1064 void smb2_deferred_work_close(struct work_struct *work)
1066 struct cifsFileInfo *cfile = container_of(work,
1067 struct cifsFileInfo, deferred.work);
1069 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1070 cifs_del_deferred_close(cfile);
1071 cfile->deferred_close_scheduled = false;
1072 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
1073 _cifsFileInfo_put(cfile, true, false);
1076 int cifs_close(struct inode *inode, struct file *file)
1078 struct cifsFileInfo *cfile;
1079 struct cifsInodeInfo *cinode = CIFS_I(inode);
1080 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1081 struct cifs_deferred_close *dclose;
1083 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
1085 if (file->private_data != NULL) {
1086 cfile = file->private_data;
1087 file->private_data = NULL;
1088 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
1089 if ((cifs_sb->ctx->closetimeo && cinode->oplock == CIFS_CACHE_RHW_FLG)
1090 && cinode->lease_granted &&
1091 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
1092 dclose && !(cfile->status_file_deleted)) {
1093 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
1094 inode_set_mtime_to_ts(inode,
1095 inode_set_ctime_current(inode));
1097 spin_lock(&cinode->deferred_lock);
1098 cifs_add_deferred_close(cfile, dclose);
1099 if (cfile->deferred_close_scheduled &&
1100 delayed_work_pending(&cfile->deferred)) {
1102 * If there is no pending work, mod_delayed_work queues new work.
1103 * So, Increase the ref count to avoid use-after-free.
1105 if (!mod_delayed_work(deferredclose_wq,
1106 &cfile->deferred, cifs_sb->ctx->closetimeo))
1107 cifsFileInfo_get(cfile);
1109 /* Deferred close for files */
1110 queue_delayed_work(deferredclose_wq,
1111 &cfile->deferred, cifs_sb->ctx->closetimeo);
1112 cfile->deferred_close_scheduled = true;
1113 spin_unlock(&cinode->deferred_lock);
1116 spin_unlock(&cinode->deferred_lock);
1117 _cifsFileInfo_put(cfile, true, false);
1119 _cifsFileInfo_put(cfile, true, false);
1124 /* return code from the ->release op is always ignored */
1129 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
1131 struct cifsFileInfo *open_file, *tmp;
1132 struct list_head tmp_list;
1134 if (!tcon->use_persistent || !tcon->need_reopen_files)
1137 tcon->need_reopen_files = false;
1139 cifs_dbg(FYI, "Reopen persistent handles\n");
1140 INIT_LIST_HEAD(&tmp_list);
1142 /* list all files open on tree connection, reopen resilient handles */
1143 spin_lock(&tcon->open_file_lock);
1144 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1145 if (!open_file->invalidHandle)
1147 cifsFileInfo_get(open_file);
1148 list_add_tail(&open_file->rlist, &tmp_list);
1150 spin_unlock(&tcon->open_file_lock);
1152 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1153 if (cifs_reopen_file(open_file, false /* do not flush */))
1154 tcon->need_reopen_files = true;
1155 list_del_init(&open_file->rlist);
1156 cifsFileInfo_put(open_file);
1160 int cifs_closedir(struct inode *inode, struct file *file)
1164 struct cifsFileInfo *cfile = file->private_data;
1165 struct cifs_tcon *tcon;
1166 struct TCP_Server_Info *server;
1169 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1175 tcon = tlink_tcon(cfile->tlink);
1176 server = tcon->ses->server;
1178 cifs_dbg(FYI, "Freeing private data in close dir\n");
1179 spin_lock(&cfile->file_info_lock);
1180 if (server->ops->dir_needs_close(cfile)) {
1181 cfile->invalidHandle = true;
1182 spin_unlock(&cfile->file_info_lock);
1183 if (server->ops->close_dir)
1184 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1187 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1188 /* not much we can do if it fails anyway, ignore rc */
1191 spin_unlock(&cfile->file_info_lock);
1193 buf = cfile->srch_inf.ntwrk_buf_start;
1195 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1196 cfile->srch_inf.ntwrk_buf_start = NULL;
1197 if (cfile->srch_inf.smallBuf)
1198 cifs_small_buf_release(buf);
1200 cifs_buf_release(buf);
1203 cifs_put_tlink(cfile->tlink);
1204 kfree(file->private_data);
1205 file->private_data = NULL;
1206 /* BB can we lock the filestruct while this is going on? */
1211 static struct cifsLockInfo *
1212 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1214 struct cifsLockInfo *lock =
1215 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1218 lock->offset = offset;
1219 lock->length = length;
1221 lock->pid = current->tgid;
1222 lock->flags = flags;
1223 INIT_LIST_HEAD(&lock->blist);
1224 init_waitqueue_head(&lock->block_q);
1229 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1231 struct cifsLockInfo *li, *tmp;
1232 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1233 list_del_init(&li->blist);
1234 wake_up(&li->block_q);
1238 #define CIFS_LOCK_OP 0
1239 #define CIFS_READ_OP 1
1240 #define CIFS_WRITE_OP 2
1242 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1244 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1245 __u64 length, __u8 type, __u16 flags,
1246 struct cifsFileInfo *cfile,
1247 struct cifsLockInfo **conf_lock, int rw_check)
1249 struct cifsLockInfo *li;
1250 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1251 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1253 list_for_each_entry(li, &fdlocks->locks, llist) {
1254 if (offset + length <= li->offset ||
1255 offset >= li->offset + li->length)
1257 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1258 server->ops->compare_fids(cfile, cur_cfile)) {
1259 /* shared lock prevents write op through the same fid */
1260 if (!(li->type & server->vals->shared_lock_type) ||
1261 rw_check != CIFS_WRITE_OP)
1264 if ((type & server->vals->shared_lock_type) &&
1265 ((server->ops->compare_fids(cfile, cur_cfile) &&
1266 current->tgid == li->pid) || type == li->type))
1268 if (rw_check == CIFS_LOCK_OP &&
1269 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1270 server->ops->compare_fids(cfile, cur_cfile))
1280 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1281 __u8 type, __u16 flags,
1282 struct cifsLockInfo **conf_lock, int rw_check)
1285 struct cifs_fid_locks *cur;
1286 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1288 list_for_each_entry(cur, &cinode->llist, llist) {
1289 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1290 flags, cfile, conf_lock,
1300 * Check if there is another lock that prevents us to set the lock (mandatory
1301 * style). If such a lock exists, update the flock structure with its
1302 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1303 * or leave it the same if we can't. Returns 0 if we don't need to request to
1304 * the server or 1 otherwise.
1307 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1308 __u8 type, struct file_lock *flock)
1311 struct cifsLockInfo *conf_lock;
1312 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1313 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1316 down_read(&cinode->lock_sem);
1318 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1319 flock->c.flc_flags, &conf_lock,
1322 flock->fl_start = conf_lock->offset;
1323 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1324 flock->c.flc_pid = conf_lock->pid;
1325 if (conf_lock->type & server->vals->shared_lock_type)
1326 flock->c.flc_type = F_RDLCK;
1328 flock->c.flc_type = F_WRLCK;
1329 } else if (!cinode->can_cache_brlcks)
1332 flock->c.flc_type = F_UNLCK;
1334 up_read(&cinode->lock_sem);
1339 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1341 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1342 cifs_down_write(&cinode->lock_sem);
1343 list_add_tail(&lock->llist, &cfile->llist->locks);
1344 up_write(&cinode->lock_sem);
1348 * Set the byte-range lock (mandatory style). Returns:
1349 * 1) 0, if we set the lock and don't need to request to the server;
1350 * 2) 1, if no locks prevent us but we need to request to the server;
1351 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1354 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1357 struct cifsLockInfo *conf_lock;
1358 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1364 cifs_down_write(&cinode->lock_sem);
1366 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1367 lock->type, lock->flags, &conf_lock,
1369 if (!exist && cinode->can_cache_brlcks) {
1370 list_add_tail(&lock->llist, &cfile->llist->locks);
1371 up_write(&cinode->lock_sem);
1380 list_add_tail(&lock->blist, &conf_lock->blist);
1381 up_write(&cinode->lock_sem);
1382 rc = wait_event_interruptible(lock->block_q,
1383 (lock->blist.prev == &lock->blist) &&
1384 (lock->blist.next == &lock->blist));
1387 cifs_down_write(&cinode->lock_sem);
1388 list_del_init(&lock->blist);
1391 up_write(&cinode->lock_sem);
1395 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1397 * Check if there is another lock that prevents us to set the lock (posix
1398 * style). If such a lock exists, update the flock structure with its
1399 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1400 * or leave it the same if we can't. Returns 0 if we don't need to request to
1401 * the server or 1 otherwise.
1404 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1407 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1408 unsigned char saved_type = flock->c.flc_type;
1410 if ((flock->c.flc_flags & FL_POSIX) == 0)
1413 down_read(&cinode->lock_sem);
1414 posix_test_lock(file, flock);
1416 if (lock_is_unlock(flock) && !cinode->can_cache_brlcks) {
1417 flock->c.flc_type = saved_type;
1421 up_read(&cinode->lock_sem);
1426 * Set the byte-range lock (posix style). Returns:
1427 * 1) <0, if the error occurs while setting the lock;
1428 * 2) 0, if we set the lock and don't need to request to the server;
1429 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1430 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1433 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1435 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1436 int rc = FILE_LOCK_DEFERRED + 1;
1438 if ((flock->c.flc_flags & FL_POSIX) == 0)
1441 cifs_down_write(&cinode->lock_sem);
1442 if (!cinode->can_cache_brlcks) {
1443 up_write(&cinode->lock_sem);
1447 rc = posix_lock_file(file, flock, NULL);
1448 up_write(&cinode->lock_sem);
1453 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1456 int rc = 0, stored_rc;
1457 struct cifsLockInfo *li, *tmp;
1458 struct cifs_tcon *tcon;
1459 unsigned int num, max_num, max_buf;
1460 LOCKING_ANDX_RANGE *buf, *cur;
1461 static const int types[] = {
1462 LOCKING_ANDX_LARGE_FILES,
1463 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1468 tcon = tlink_tcon(cfile->tlink);
1471 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1472 * and check it before using.
1474 max_buf = tcon->ses->server->maxBuf;
1475 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1480 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1482 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1484 max_num = (max_buf - sizeof(struct smb_hdr)) /
1485 sizeof(LOCKING_ANDX_RANGE);
1486 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1492 for (i = 0; i < 2; i++) {
1495 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1496 if (li->type != types[i])
1498 cur->Pid = cpu_to_le16(li->pid);
1499 cur->LengthLow = cpu_to_le32((u32)li->length);
1500 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1501 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1502 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1503 if (++num == max_num) {
1504 stored_rc = cifs_lockv(xid, tcon,
1506 (__u8)li->type, 0, num,
1517 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1518 (__u8)types[i], 0, num, buf);
1530 hash_lockowner(fl_owner_t owner)
1532 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1534 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1536 struct lock_to_push {
1537 struct list_head llist;
1545 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1547 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1549 struct inode *inode = d_inode(cfile->dentry);
1550 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1551 struct file_lock *flock;
1552 struct file_lock_context *flctx = locks_inode_context(inode);
1553 unsigned int count = 0, i;
1554 int rc = 0, xid, type;
1555 struct list_head locks_to_send, *el;
1556 struct lock_to_push *lck, *tmp;
1564 spin_lock(&flctx->flc_lock);
1565 list_for_each(el, &flctx->flc_posix) {
1568 spin_unlock(&flctx->flc_lock);
1570 INIT_LIST_HEAD(&locks_to_send);
1573 * Allocating count locks is enough because no FL_POSIX locks can be
1574 * added to the list while we are holding cinode->lock_sem that
1575 * protects locking operations of this inode.
1577 for (i = 0; i < count; i++) {
1578 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1583 list_add_tail(&lck->llist, &locks_to_send);
1586 el = locks_to_send.next;
1587 spin_lock(&flctx->flc_lock);
1588 for_each_file_lock(flock, &flctx->flc_posix) {
1589 unsigned char ftype = flock->c.flc_type;
1591 if (el == &locks_to_send) {
1593 * The list ended. We don't have enough allocated
1594 * structures - something is really wrong.
1596 cifs_dbg(VFS, "Can't push all brlocks!\n");
1599 length = cifs_flock_len(flock);
1600 if (ftype == F_RDLCK || ftype == F_SHLCK)
1604 lck = list_entry(el, struct lock_to_push, llist);
1605 lck->pid = hash_lockowner(flock->c.flc_owner);
1606 lck->netfid = cfile->fid.netfid;
1607 lck->length = length;
1609 lck->offset = flock->fl_start;
1611 spin_unlock(&flctx->flc_lock);
1613 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1616 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1617 lck->offset, lck->length, NULL,
1621 list_del(&lck->llist);
1629 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1630 list_del(&lck->llist);
1635 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1638 cifs_push_locks(struct cifsFileInfo *cfile)
1640 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1641 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1643 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1644 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1645 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1647 /* we are going to update can_cache_brlcks here - need a write access */
1648 cifs_down_write(&cinode->lock_sem);
1649 if (!cinode->can_cache_brlcks) {
1650 up_write(&cinode->lock_sem);
1654 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1655 if (cap_unix(tcon->ses) &&
1656 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1657 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1658 rc = cifs_push_posix_locks(cfile);
1660 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1661 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1663 cinode->can_cache_brlcks = false;
1664 up_write(&cinode->lock_sem);
1669 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1670 bool *wait_flag, struct TCP_Server_Info *server)
1672 if (flock->c.flc_flags & FL_POSIX)
1673 cifs_dbg(FYI, "Posix\n");
1674 if (flock->c.flc_flags & FL_FLOCK)
1675 cifs_dbg(FYI, "Flock\n");
1676 if (flock->c.flc_flags & FL_SLEEP) {
1677 cifs_dbg(FYI, "Blocking lock\n");
1680 if (flock->c.flc_flags & FL_ACCESS)
1681 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1682 if (flock->c.flc_flags & FL_LEASE)
1683 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1684 if (flock->c.flc_flags &
1685 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1686 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1687 cifs_dbg(FYI, "Unknown lock flags 0x%x\n",
1688 flock->c.flc_flags);
1690 *type = server->vals->large_lock_type;
1691 if (lock_is_write(flock)) {
1692 cifs_dbg(FYI, "F_WRLCK\n");
1693 *type |= server->vals->exclusive_lock_type;
1695 } else if (lock_is_unlock(flock)) {
1696 cifs_dbg(FYI, "F_UNLCK\n");
1697 *type |= server->vals->unlock_lock_type;
1699 /* Check if unlock includes more than one lock range */
1700 } else if (lock_is_read(flock)) {
1701 cifs_dbg(FYI, "F_RDLCK\n");
1702 *type |= server->vals->shared_lock_type;
1704 } else if (flock->c.flc_type == F_EXLCK) {
1705 cifs_dbg(FYI, "F_EXLCK\n");
1706 *type |= server->vals->exclusive_lock_type;
1708 } else if (flock->c.flc_type == F_SHLCK) {
1709 cifs_dbg(FYI, "F_SHLCK\n");
1710 *type |= server->vals->shared_lock_type;
1713 cifs_dbg(FYI, "Unknown type of lock\n");
1717 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1718 bool wait_flag, bool posix_lck, unsigned int xid)
1721 __u64 length = cifs_flock_len(flock);
1722 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1723 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1724 struct TCP_Server_Info *server = tcon->ses->server;
1725 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1726 __u16 netfid = cfile->fid.netfid;
1729 int posix_lock_type;
1731 rc = cifs_posix_lock_test(file, flock);
1735 if (type & server->vals->shared_lock_type)
1736 posix_lock_type = CIFS_RDLCK;
1738 posix_lock_type = CIFS_WRLCK;
1739 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1740 hash_lockowner(flock->c.flc_owner),
1741 flock->fl_start, length, flock,
1742 posix_lock_type, wait_flag);
1745 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1747 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1751 /* BB we could chain these into one lock request BB */
1752 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1755 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1757 flock->c.flc_type = F_UNLCK;
1759 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1764 if (type & server->vals->shared_lock_type) {
1765 flock->c.flc_type = F_WRLCK;
1769 type &= ~server->vals->exclusive_lock_type;
1771 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1772 type | server->vals->shared_lock_type,
1775 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1776 type | server->vals->shared_lock_type, 0, 1, false);
1777 flock->c.flc_type = F_RDLCK;
1779 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1782 flock->c.flc_type = F_WRLCK;
1788 cifs_move_llist(struct list_head *source, struct list_head *dest)
1790 struct list_head *li, *tmp;
1791 list_for_each_safe(li, tmp, source)
1792 list_move(li, dest);
1796 cifs_free_llist(struct list_head *llist)
1798 struct cifsLockInfo *li, *tmp;
1799 list_for_each_entry_safe(li, tmp, llist, llist) {
1800 cifs_del_lock_waiters(li);
1801 list_del(&li->llist);
1806 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1808 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1811 int rc = 0, stored_rc;
1812 static const int types[] = {
1813 LOCKING_ANDX_LARGE_FILES,
1814 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1817 unsigned int max_num, num, max_buf;
1818 LOCKING_ANDX_RANGE *buf, *cur;
1819 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1820 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1821 struct cifsLockInfo *li, *tmp;
1822 __u64 length = cifs_flock_len(flock);
1823 struct list_head tmp_llist;
1825 INIT_LIST_HEAD(&tmp_llist);
1828 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1829 * and check it before using.
1831 max_buf = tcon->ses->server->maxBuf;
1832 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1835 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1837 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1839 max_num = (max_buf - sizeof(struct smb_hdr)) /
1840 sizeof(LOCKING_ANDX_RANGE);
1841 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1845 cifs_down_write(&cinode->lock_sem);
1846 for (i = 0; i < 2; i++) {
1849 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1850 if (flock->fl_start > li->offset ||
1851 (flock->fl_start + length) <
1852 (li->offset + li->length))
1854 if (current->tgid != li->pid)
1856 if (types[i] != li->type)
1858 if (cinode->can_cache_brlcks) {
1860 * We can cache brlock requests - simply remove
1861 * a lock from the file's list.
1863 list_del(&li->llist);
1864 cifs_del_lock_waiters(li);
1868 cur->Pid = cpu_to_le16(li->pid);
1869 cur->LengthLow = cpu_to_le32((u32)li->length);
1870 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1871 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1872 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1874 * We need to save a lock here to let us add it again to
1875 * the file's list if the unlock range request fails on
1878 list_move(&li->llist, &tmp_llist);
1879 if (++num == max_num) {
1880 stored_rc = cifs_lockv(xid, tcon,
1882 li->type, num, 0, buf);
1885 * We failed on the unlock range
1886 * request - add all locks from the tmp
1887 * list to the head of the file's list.
1889 cifs_move_llist(&tmp_llist,
1890 &cfile->llist->locks);
1894 * The unlock range request succeed -
1895 * free the tmp list.
1897 cifs_free_llist(&tmp_llist);
1904 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1905 types[i], num, 0, buf);
1907 cifs_move_llist(&tmp_llist,
1908 &cfile->llist->locks);
1911 cifs_free_llist(&tmp_llist);
1915 up_write(&cinode->lock_sem);
1919 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1922 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1923 bool wait_flag, bool posix_lck, int lock, int unlock,
1927 __u64 length = cifs_flock_len(flock);
1928 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1929 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1930 struct TCP_Server_Info *server = tcon->ses->server;
1931 struct inode *inode = d_inode(cfile->dentry);
1933 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1935 int posix_lock_type;
1937 rc = cifs_posix_lock_set(file, flock);
1938 if (rc <= FILE_LOCK_DEFERRED)
1941 if (type & server->vals->shared_lock_type)
1942 posix_lock_type = CIFS_RDLCK;
1944 posix_lock_type = CIFS_WRLCK;
1947 posix_lock_type = CIFS_UNLCK;
1949 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1950 hash_lockowner(flock->c.flc_owner),
1951 flock->fl_start, length,
1952 NULL, posix_lock_type, wait_flag);
1955 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1957 struct cifsLockInfo *lock;
1959 lock = cifs_lock_init(flock->fl_start, length, type,
1960 flock->c.flc_flags);
1964 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1973 * Windows 7 server can delay breaking lease from read to None
1974 * if we set a byte-range lock on a file - break it explicitly
1975 * before sending the lock to the server to be sure the next
1976 * read won't conflict with non-overlapted locks due to
1979 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1980 CIFS_CACHE_READ(CIFS_I(inode))) {
1981 cifs_zap_mapping(inode);
1982 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1984 CIFS_I(inode)->oplock = 0;
1987 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1988 type, 1, 0, wait_flag);
1994 cifs_lock_add(cfile, lock);
1996 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1999 if ((flock->c.flc_flags & FL_POSIX) || (flock->c.flc_flags & FL_FLOCK)) {
2001 * If this is a request to remove all locks because we
2002 * are closing the file, it doesn't matter if the
2003 * unlocking failed as both cifs.ko and the SMB server
2004 * remove the lock on file close
2007 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
2008 if (!(flock->c.flc_flags & FL_CLOSE))
2011 rc = locks_lock_file_wait(file, flock);
2016 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
2019 int lock = 0, unlock = 0;
2020 bool wait_flag = false;
2021 bool posix_lck = false;
2022 struct cifs_sb_info *cifs_sb;
2023 struct cifs_tcon *tcon;
2024 struct cifsFileInfo *cfile;
2029 if (!(fl->c.flc_flags & FL_FLOCK)) {
2035 cfile = (struct cifsFileInfo *)file->private_data;
2036 tcon = tlink_tcon(cfile->tlink);
2038 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
2040 cifs_sb = CIFS_FILE_SB(file);
2042 if (cap_unix(tcon->ses) &&
2043 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2044 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2047 if (!lock && !unlock) {
2049 * if no lock or unlock then nothing to do since we do not
2057 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
2065 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
2068 int lock = 0, unlock = 0;
2069 bool wait_flag = false;
2070 bool posix_lck = false;
2071 struct cifs_sb_info *cifs_sb;
2072 struct cifs_tcon *tcon;
2073 struct cifsFileInfo *cfile;
2079 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
2080 flock->c.flc_flags, flock->c.flc_type,
2081 (long long)flock->fl_start,
2082 (long long)flock->fl_end);
2084 cfile = (struct cifsFileInfo *)file->private_data;
2085 tcon = tlink_tcon(cfile->tlink);
2087 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
2089 cifs_sb = CIFS_FILE_SB(file);
2090 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
2092 if (cap_unix(tcon->ses) &&
2093 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
2094 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
2097 * BB add code here to normalize offset and length to account for
2098 * negative length which we can not accept over the wire.
2100 if (IS_GETLK(cmd)) {
2101 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
2106 if (!lock && !unlock) {
2108 * if no lock or unlock then nothing to do since we do not
2115 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
2122 * update the file size (if needed) after a write. Should be called with
2123 * the inode->i_lock held
2126 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
2127 unsigned int bytes_written)
2129 loff_t end_of_write = offset + bytes_written;
2131 if (end_of_write > cifsi->netfs.remote_i_size)
2132 netfs_resize_file(&cifsi->netfs, end_of_write, true);
2136 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
2137 size_t write_size, loff_t *offset)
2140 unsigned int bytes_written = 0;
2141 unsigned int total_written;
2142 struct cifs_tcon *tcon;
2143 struct TCP_Server_Info *server;
2145 struct dentry *dentry = open_file->dentry;
2146 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2147 struct cifs_io_parms io_parms = {0};
2149 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2150 write_size, *offset, dentry);
2152 tcon = tlink_tcon(open_file->tlink);
2153 server = tcon->ses->server;
2155 if (!server->ops->sync_write)
2160 for (total_written = 0; write_size > total_written;
2161 total_written += bytes_written) {
2163 while (rc == -EAGAIN) {
2167 if (open_file->invalidHandle) {
2168 /* we could deadlock if we called
2169 filemap_fdatawait from here so tell
2170 reopen_file not to flush data to
2172 rc = cifs_reopen_file(open_file, false);
2177 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2178 (unsigned int)write_size - total_written);
2179 /* iov[0] is reserved for smb header */
2180 iov[1].iov_base = (char *)write_data + total_written;
2181 iov[1].iov_len = len;
2183 io_parms.tcon = tcon;
2184 io_parms.offset = *offset;
2185 io_parms.length = len;
2186 rc = server->ops->sync_write(xid, &open_file->fid,
2187 &io_parms, &bytes_written, iov, 1);
2189 if (rc || (bytes_written == 0)) {
2197 spin_lock(&d_inode(dentry)->i_lock);
2198 cifs_update_eof(cifsi, *offset, bytes_written);
2199 spin_unlock(&d_inode(dentry)->i_lock);
2200 *offset += bytes_written;
2204 cifs_stats_bytes_written(tcon, total_written);
2206 if (total_written > 0) {
2207 spin_lock(&d_inode(dentry)->i_lock);
2208 if (*offset > d_inode(dentry)->i_size) {
2209 i_size_write(d_inode(dentry), *offset);
2210 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2212 spin_unlock(&d_inode(dentry)->i_lock);
2214 mark_inode_dirty_sync(d_inode(dentry));
2216 return total_written;
2219 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2222 struct cifsFileInfo *open_file = NULL;
2223 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2225 /* only filter by fsuid on multiuser mounts */
2226 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2229 spin_lock(&cifs_inode->open_file_lock);
2230 /* we could simply get the first_list_entry since write-only entries
2231 are always at the end of the list but since the first entry might
2232 have a close pending, we go through the whole list */
2233 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2234 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2236 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2237 if ((!open_file->invalidHandle)) {
2238 /* found a good file */
2239 /* lock it so it will not be closed on us */
2240 cifsFileInfo_get(open_file);
2241 spin_unlock(&cifs_inode->open_file_lock);
2243 } /* else might as well continue, and look for
2244 another, or simply have the caller reopen it
2245 again rather than trying to fix this handle */
2246 } else /* write only file */
2247 break; /* write only files are last so must be done */
2249 spin_unlock(&cifs_inode->open_file_lock);
2253 /* Return -EBADF if no handle is found and general rc otherwise */
2255 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2256 struct cifsFileInfo **ret_file)
2258 struct cifsFileInfo *open_file, *inv_file = NULL;
2259 struct cifs_sb_info *cifs_sb;
2260 bool any_available = false;
2262 unsigned int refind = 0;
2263 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2264 bool with_delete = flags & FIND_WR_WITH_DELETE;
2268 * Having a null inode here (because mapping->host was set to zero by
2269 * the VFS or MM) should not happen but we had reports of on oops (due
2270 * to it being zero) during stress testcases so we need to check for it
2273 if (cifs_inode == NULL) {
2274 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2279 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2281 /* only filter by fsuid on multiuser mounts */
2282 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2285 spin_lock(&cifs_inode->open_file_lock);
2287 if (refind > MAX_REOPEN_ATT) {
2288 spin_unlock(&cifs_inode->open_file_lock);
2291 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2292 if (!any_available && open_file->pid != current->tgid)
2294 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2296 if (with_delete && !(open_file->fid.access & DELETE))
2298 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2299 if (!open_file->invalidHandle) {
2300 /* found a good writable file */
2301 cifsFileInfo_get(open_file);
2302 spin_unlock(&cifs_inode->open_file_lock);
2303 *ret_file = open_file;
2307 inv_file = open_file;
2311 /* couldn't find useable FH with same pid, try any available */
2312 if (!any_available) {
2313 any_available = true;
2314 goto refind_writable;
2318 any_available = false;
2319 cifsFileInfo_get(inv_file);
2322 spin_unlock(&cifs_inode->open_file_lock);
2325 rc = cifs_reopen_file(inv_file, false);
2327 *ret_file = inv_file;
2331 spin_lock(&cifs_inode->open_file_lock);
2332 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2333 spin_unlock(&cifs_inode->open_file_lock);
2334 cifsFileInfo_put(inv_file);
2337 spin_lock(&cifs_inode->open_file_lock);
2338 goto refind_writable;
2344 struct cifsFileInfo *
2345 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2347 struct cifsFileInfo *cfile;
2350 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2352 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2358 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2360 struct cifsFileInfo **ret_file)
2362 struct cifsFileInfo *cfile;
2363 void *page = alloc_dentry_path();
2367 spin_lock(&tcon->open_file_lock);
2368 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2369 struct cifsInodeInfo *cinode;
2370 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2371 if (IS_ERR(full_path)) {
2372 spin_unlock(&tcon->open_file_lock);
2373 free_dentry_path(page);
2374 return PTR_ERR(full_path);
2376 if (strcmp(full_path, name))
2379 cinode = CIFS_I(d_inode(cfile->dentry));
2380 spin_unlock(&tcon->open_file_lock);
2381 free_dentry_path(page);
2382 return cifs_get_writable_file(cinode, flags, ret_file);
2385 spin_unlock(&tcon->open_file_lock);
2386 free_dentry_path(page);
2391 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2392 struct cifsFileInfo **ret_file)
2394 struct cifsFileInfo *cfile;
2395 void *page = alloc_dentry_path();
2399 spin_lock(&tcon->open_file_lock);
2400 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2401 struct cifsInodeInfo *cinode;
2402 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2403 if (IS_ERR(full_path)) {
2404 spin_unlock(&tcon->open_file_lock);
2405 free_dentry_path(page);
2406 return PTR_ERR(full_path);
2408 if (strcmp(full_path, name))
2411 cinode = CIFS_I(d_inode(cfile->dentry));
2412 spin_unlock(&tcon->open_file_lock);
2413 free_dentry_path(page);
2414 *ret_file = find_readable_file(cinode, 0);
2415 return *ret_file ? 0 : -ENOENT;
2418 spin_unlock(&tcon->open_file_lock);
2419 free_dentry_path(page);
2424 cifs_writedata_release(struct kref *refcount)
2426 struct cifs_writedata *wdata = container_of(refcount,
2427 struct cifs_writedata, refcount);
2428 #ifdef CONFIG_CIFS_SMB_DIRECT
2430 smbd_deregister_mr(wdata->mr);
2436 cifsFileInfo_put(wdata->cfile);
2442 * Write failed with a retryable error. Resend the write request. It's also
2443 * possible that the page was redirtied so re-clean the page.
2446 cifs_writev_requeue(struct cifs_writedata *wdata)
2449 struct inode *inode = d_inode(wdata->cfile->dentry);
2450 struct TCP_Server_Info *server;
2451 unsigned int rest_len = wdata->bytes;
2452 loff_t fpos = wdata->offset;
2454 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2456 struct cifs_writedata *wdata2;
2457 unsigned int wsize, cur_len;
2459 wsize = server->ops->wp_retry_size(inode);
2460 if (wsize < rest_len) {
2461 if (wsize < PAGE_SIZE) {
2465 cur_len = min(round_down(wsize, PAGE_SIZE), rest_len);
2470 wdata2 = cifs_writedata_alloc(cifs_writev_complete);
2476 wdata2->sync_mode = wdata->sync_mode;
2477 wdata2->offset = fpos;
2478 wdata2->bytes = cur_len;
2479 wdata2->iter = wdata->iter;
2481 iov_iter_advance(&wdata2->iter, fpos - wdata->offset);
2482 iov_iter_truncate(&wdata2->iter, wdata2->bytes);
2484 if (iov_iter_is_xarray(&wdata2->iter))
2485 /* Check for pages having been redirtied and clean
2486 * them. We can do this by walking the xarray. If
2487 * it's not an xarray, then it's a DIO and we shouldn't
2488 * be mucking around with the page bits.
2490 cifs_undirty_folios(inode, fpos, cur_len);
2492 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2494 if (!wdata2->cfile) {
2495 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2497 if (!is_retryable_error(rc))
2500 wdata2->pid = wdata2->cfile->pid;
2501 rc = server->ops->async_writev(wdata2,
2502 cifs_writedata_release);
2505 kref_put(&wdata2->refcount, cifs_writedata_release);
2507 if (is_retryable_error(rc))
2510 rest_len -= cur_len;
2515 rest_len -= cur_len;
2516 } while (rest_len > 0);
2518 /* Clean up remaining pages from the original wdata */
2519 if (iov_iter_is_xarray(&wdata->iter))
2520 cifs_pages_write_failed(inode, fpos, rest_len);
2522 if (rc != 0 && !is_retryable_error(rc))
2523 mapping_set_error(inode->i_mapping, rc);
2524 kref_put(&wdata->refcount, cifs_writedata_release);
2528 cifs_writev_complete(struct work_struct *work)
2530 struct cifs_writedata *wdata = container_of(work,
2531 struct cifs_writedata, work);
2532 struct inode *inode = d_inode(wdata->cfile->dentry);
2534 if (wdata->result == 0) {
2535 spin_lock(&inode->i_lock);
2536 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2537 spin_unlock(&inode->i_lock);
2538 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2540 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2541 return cifs_writev_requeue(wdata);
2543 if (wdata->result == -EAGAIN)
2544 cifs_pages_write_redirty(inode, wdata->offset, wdata->bytes);
2545 else if (wdata->result < 0)
2546 cifs_pages_write_failed(inode, wdata->offset, wdata->bytes);
2548 cifs_pages_written_back(inode, wdata->offset, wdata->bytes);
2550 if (wdata->result != -EAGAIN)
2551 mapping_set_error(inode->i_mapping, wdata->result);
2552 kref_put(&wdata->refcount, cifs_writedata_release);
2555 struct cifs_writedata *cifs_writedata_alloc(work_func_t complete)
2557 struct cifs_writedata *wdata;
2559 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2560 if (wdata != NULL) {
2561 kref_init(&wdata->refcount);
2562 INIT_LIST_HEAD(&wdata->list);
2563 init_completion(&wdata->done);
2564 INIT_WORK(&wdata->work, complete);
2569 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2571 struct address_space *mapping = page->mapping;
2572 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2575 int bytes_written = 0;
2576 struct inode *inode;
2577 struct cifsFileInfo *open_file;
2579 if (!mapping || !mapping->host)
2582 inode = page->mapping->host;
2584 offset += (loff_t)from;
2585 write_data = kmap(page);
2588 if ((to > PAGE_SIZE) || (from > to)) {
2593 /* racing with truncate? */
2594 if (offset > mapping->host->i_size) {
2596 return 0; /* don't care */
2599 /* check to make sure that we are not extending the file */
2600 if (mapping->host->i_size - offset < (loff_t)to)
2601 to = (unsigned)(mapping->host->i_size - offset);
2603 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2606 bytes_written = cifs_write(open_file, open_file->pid,
2607 write_data, to - from, &offset);
2608 cifsFileInfo_put(open_file);
2609 /* Does mm or vfs already set times? */
2610 simple_inode_init_ts(inode);
2611 if ((bytes_written > 0) && (offset))
2613 else if (bytes_written < 0)
2618 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2619 if (!is_retryable_error(rc))
2628 * Extend the region to be written back to include subsequent contiguously
2629 * dirty pages if possible, but don't sleep while doing so.
2631 static void cifs_extend_writeback(struct address_space *mapping,
2632 struct xa_state *xas,
2639 struct folio_batch batch;
2640 struct folio *folio;
2641 unsigned int nr_pages;
2642 pgoff_t index = (start + *_len) / PAGE_SIZE;
2647 folio_batch_init(&batch);
2650 /* Firstly, we gather up a batch of contiguous dirty pages
2651 * under the RCU read lock - but we can't clear the dirty flags
2652 * there if any of those pages are mapped.
2656 xas_for_each(xas, folio, ULONG_MAX) {
2658 if (xas_retry(xas, folio))
2660 if (xa_is_value(folio))
2662 if (folio->index != index) {
2667 if (!folio_try_get_rcu(folio)) {
2671 nr_pages = folio_nr_pages(folio);
2672 if (nr_pages > max_pages) {
2677 /* Has the page moved or been split? */
2678 if (unlikely(folio != xas_reload(xas))) {
2684 if (!folio_trylock(folio)) {
2689 if (!folio_test_dirty(folio) ||
2690 folio_test_writeback(folio)) {
2691 folio_unlock(folio);
2697 max_pages -= nr_pages;
2698 len = folio_size(folio);
2702 *_count -= nr_pages;
2704 if (max_pages <= 0 || *_len >= max_len || *_count <= 0)
2707 if (!folio_batch_add(&batch, folio))
2716 /* Now, if we obtained any pages, we can shift them to being
2717 * writable and mark them for caching.
2719 if (!folio_batch_count(&batch))
2722 for (i = 0; i < folio_batch_count(&batch); i++) {
2723 folio = batch.folios[i];
2724 /* The folio should be locked, dirty and not undergoing
2725 * writeback from the loop above.
2727 if (!folio_clear_dirty_for_io(folio))
2729 folio_start_writeback(folio);
2730 folio_unlock(folio);
2733 folio_batch_release(&batch);
2739 * Write back the locked page and any subsequent non-locked dirty pages.
2741 static ssize_t cifs_write_back_from_locked_folio(struct address_space *mapping,
2742 struct writeback_control *wbc,
2743 struct xa_state *xas,
2744 struct folio *folio,
2745 unsigned long long start,
2746 unsigned long long end)
2748 struct inode *inode = mapping->host;
2749 struct TCP_Server_Info *server;
2750 struct cifs_writedata *wdata;
2751 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2752 struct cifs_credits credits_on_stack;
2753 struct cifs_credits *credits = &credits_on_stack;
2754 struct cifsFileInfo *cfile = NULL;
2755 unsigned long long i_size = i_size_read(inode), max_len;
2756 unsigned int xid, wsize;
2757 size_t len = folio_size(folio);
2758 long count = wbc->nr_to_write;
2761 /* The folio should be locked, dirty and not undergoing writeback. */
2762 if (!folio_clear_dirty_for_io(folio))
2764 folio_start_writeback(folio);
2766 count -= folio_nr_pages(folio);
2769 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2771 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2773 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n", rc);
2777 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2782 wdata = cifs_writedata_alloc(cifs_writev_complete);
2788 wdata->sync_mode = wbc->sync_mode;
2789 wdata->offset = folio_pos(folio);
2790 wdata->pid = cfile->pid;
2791 wdata->credits = credits_on_stack;
2792 wdata->cfile = cfile;
2793 wdata->server = server;
2796 /* Find all consecutive lockable dirty pages that have contiguous
2797 * written regions, stopping when we find a page that is not
2798 * immediately lockable, is not dirty or is missing, or we reach the
2801 if (start < i_size) {
2802 /* Trim the write to the EOF; the extra data is ignored. Also
2803 * put an upper limit on the size of a single storedata op.
2806 max_len = min_t(unsigned long long, max_len, end - start + 1);
2807 max_len = min_t(unsigned long long, max_len, i_size - start);
2809 if (len < max_len) {
2810 int max_pages = INT_MAX;
2812 #ifdef CONFIG_CIFS_SMB_DIRECT
2813 if (server->smbd_conn)
2814 max_pages = server->smbd_conn->max_frmr_depth;
2816 max_pages -= folio_nr_pages(folio);
2819 cifs_extend_writeback(mapping, xas, &count, start,
2820 max_pages, max_len, &len);
2823 len = min_t(unsigned long long, len, i_size - start);
2825 /* We now have a contiguous set of dirty pages, each with writeback
2826 * set; the first page is still locked at this point, but all the rest
2827 * have been unlocked.
2829 folio_unlock(folio);
2832 if (start < i_size) {
2833 iov_iter_xarray(&wdata->iter, ITER_SOURCE, &mapping->i_pages,
2836 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2840 if (wdata->cfile->invalidHandle)
2843 rc = wdata->server->ops->async_writev(wdata,
2844 cifs_writedata_release);
2846 kref_put(&wdata->refcount, cifs_writedata_release);
2850 /* The dirty region was entirely beyond the EOF. */
2851 cifs_pages_written_back(inode, start, len);
2856 kref_put(&wdata->refcount, cifs_writedata_release);
2858 add_credits_and_wake_if(server, credits, 0);
2861 cifsFileInfo_put(cfile);
2865 wbc->nr_to_write = count;
2867 } else if (is_retryable_error(rc)) {
2868 cifs_pages_write_redirty(inode, start, len);
2870 cifs_pages_write_failed(inode, start, len);
2871 mapping_set_error(mapping, rc);
2873 /* Indication to update ctime and mtime as close is deferred */
2874 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2879 * write a region of pages back to the server
2881 static ssize_t cifs_writepages_begin(struct address_space *mapping,
2882 struct writeback_control *wbc,
2883 struct xa_state *xas,
2884 unsigned long long *_start,
2885 unsigned long long end)
2887 struct folio *folio;
2888 unsigned long long start = *_start;
2893 /* Find the first dirty page. */
2897 folio = xas_find_marked(xas, end / PAGE_SIZE, PAGECACHE_TAG_DIRTY);
2898 if (xas_retry(xas, folio) || xa_is_value(folio))
2903 if (!folio_try_get_rcu(folio)) {
2908 if (unlikely(folio != xas_reload(xas))) {
2921 start = folio_pos(folio); /* May regress with THPs */
2923 /* At this point we hold neither the i_pages lock nor the page lock:
2924 * the page may be truncated or invalidated (changing page->mapping to
2925 * NULL), or even swizzled back from swapper_space to tmpfs file
2929 if (wbc->sync_mode != WB_SYNC_NONE) {
2930 ret = folio_lock_killable(folio);
2934 if (!folio_trylock(folio))
2938 if (folio->mapping != mapping ||
2939 !folio_test_dirty(folio)) {
2940 start += folio_size(folio);
2941 folio_unlock(folio);
2945 if (folio_test_writeback(folio) ||
2946 folio_test_fscache(folio)) {
2947 folio_unlock(folio);
2948 if (wbc->sync_mode != WB_SYNC_NONE) {
2949 folio_wait_writeback(folio);
2950 #ifdef CONFIG_CIFS_FSCACHE
2951 folio_wait_fscache(folio);
2956 start += folio_size(folio);
2957 if (wbc->sync_mode == WB_SYNC_NONE) {
2958 if (skips >= 5 || need_resched()) {
2967 ret = cifs_write_back_from_locked_folio(mapping, wbc, xas, folio, start, end);
2970 *_start = start + ret;
2975 * Write a region of pages back to the server
2977 static int cifs_writepages_region(struct address_space *mapping,
2978 struct writeback_control *wbc,
2979 unsigned long long *_start,
2980 unsigned long long end)
2984 XA_STATE(xas, &mapping->i_pages, *_start / PAGE_SIZE);
2987 ret = cifs_writepages_begin(mapping, wbc, &xas, _start, end);
2988 if (ret > 0 && wbc->nr_to_write > 0)
2990 } while (ret > 0 && wbc->nr_to_write > 0);
2992 return ret > 0 ? 0 : ret;
2996 * Write some of the pending data back to the server
2998 static int cifs_writepages(struct address_space *mapping,
2999 struct writeback_control *wbc)
3004 /* We have to be careful as we can end up racing with setattr()
3005 * truncating the pagecache since the caller doesn't take a lock here
3009 if (wbc->range_cyclic && mapping->writeback_index) {
3010 start = mapping->writeback_index * PAGE_SIZE;
3011 ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX);
3015 if (wbc->nr_to_write <= 0) {
3016 mapping->writeback_index = start / PAGE_SIZE;
3021 end = mapping->writeback_index * PAGE_SIZE;
3022 mapping->writeback_index = 0;
3023 ret = cifs_writepages_region(mapping, wbc, &start, end);
3025 mapping->writeback_index = start / PAGE_SIZE;
3026 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
3028 ret = cifs_writepages_region(mapping, wbc, &start, LLONG_MAX);
3029 if (wbc->nr_to_write > 0 && ret == 0)
3030 mapping->writeback_index = start / PAGE_SIZE;
3032 start = wbc->range_start;
3033 ret = cifs_writepages_region(mapping, wbc, &start, wbc->range_end);
3041 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
3047 /* BB add check for wbc flags */
3049 if (!PageUptodate(page))
3050 cifs_dbg(FYI, "ppw - page not up to date\n");
3053 * Set the "writeback" flag, and clear "dirty" in the radix tree.
3055 * A writepage() implementation always needs to do either this,
3056 * or re-dirty the page with "redirty_page_for_writepage()" in
3057 * the case of a failure.
3059 * Just unlocking the page will cause the radix tree tag-bits
3060 * to fail to update with the state of the page correctly.
3062 set_page_writeback(page);
3064 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
3065 if (is_retryable_error(rc)) {
3066 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
3068 redirty_page_for_writepage(wbc, page);
3069 } else if (rc != 0) {
3071 mapping_set_error(page->mapping, rc);
3073 SetPageUptodate(page);
3075 end_page_writeback(page);
3081 static int cifs_write_end(struct file *file, struct address_space *mapping,
3082 loff_t pos, unsigned len, unsigned copied,
3083 struct page *page, void *fsdata)
3086 struct inode *inode = mapping->host;
3087 struct cifsFileInfo *cfile = file->private_data;
3088 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
3089 struct folio *folio = page_folio(page);
3092 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3095 pid = current->tgid;
3097 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
3100 if (folio_test_checked(folio)) {
3102 folio_mark_uptodate(folio);
3103 folio_clear_checked(folio);
3104 } else if (!folio_test_uptodate(folio) && copied == PAGE_SIZE)
3105 folio_mark_uptodate(folio);
3107 if (!folio_test_uptodate(folio)) {
3109 unsigned offset = pos & (PAGE_SIZE - 1);
3113 /* this is probably better than directly calling
3114 partialpage_write since in this function the file handle is
3115 known which we might as well leverage */
3116 /* BB check if anything else missing out of ppw
3117 such as updating last write time */
3118 page_data = kmap(page);
3119 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
3120 /* if (rc < 0) should we set writebehind rc? */
3127 set_page_dirty(page);
3131 spin_lock(&inode->i_lock);
3132 if (pos > inode->i_size) {
3133 loff_t additional_blocks = (512 - 1 + copied) >> 9;
3135 i_size_write(inode, pos);
3137 * Estimate new allocation size based on the amount written.
3138 * This will be updated from server on close (and on queryinfo)
3140 inode->i_blocks = min_t(blkcnt_t, (512 - 1 + pos) >> 9,
3141 inode->i_blocks + additional_blocks);
3143 spin_unlock(&inode->i_lock);
3148 /* Indication to update ctime and mtime as close is deferred */
3149 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
3154 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
3159 struct cifs_tcon *tcon;
3160 struct TCP_Server_Info *server;
3161 struct cifsFileInfo *smbfile = file->private_data;
3162 struct inode *inode = file_inode(file);
3163 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3165 rc = file_write_and_wait_range(file, start, end);
3167 trace_cifs_fsync_err(inode->i_ino, rc);
3173 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3176 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
3177 rc = cifs_zap_mapping(inode);
3179 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
3180 rc = 0; /* don't care about it in fsync */
3184 tcon = tlink_tcon(smbfile->tlink);
3185 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3186 server = tcon->ses->server;
3187 if (server->ops->flush == NULL) {
3189 goto strict_fsync_exit;
3192 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3193 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3195 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3196 cifsFileInfo_put(smbfile);
3198 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3200 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3208 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
3212 struct cifs_tcon *tcon;
3213 struct TCP_Server_Info *server;
3214 struct cifsFileInfo *smbfile = file->private_data;
3215 struct inode *inode = file_inode(file);
3216 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
3218 rc = file_write_and_wait_range(file, start, end);
3220 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
3226 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3229 tcon = tlink_tcon(smbfile->tlink);
3230 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3231 server = tcon->ses->server;
3232 if (server->ops->flush == NULL) {
3237 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3238 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3240 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3241 cifsFileInfo_put(smbfile);
3243 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3245 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3254 * As file closes, flush all cached write data for this inode checking
3255 * for write behind errors.
3257 int cifs_flush(struct file *file, fl_owner_t id)
3259 struct inode *inode = file_inode(file);
3262 if (file->f_mode & FMODE_WRITE)
3263 rc = filemap_write_and_wait(inode->i_mapping);
3265 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3267 /* get more nuanced writeback errors */
3268 rc = filemap_check_wb_err(file->f_mapping, 0);
3269 trace_cifs_flush_err(inode->i_ino, rc);
3275 cifs_uncached_writedata_release(struct kref *refcount)
3277 struct cifs_writedata *wdata = container_of(refcount,
3278 struct cifs_writedata, refcount);
3280 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3281 cifs_writedata_release(refcount);
3284 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3287 cifs_uncached_writev_complete(struct work_struct *work)
3289 struct cifs_writedata *wdata = container_of(work,
3290 struct cifs_writedata, work);
3291 struct inode *inode = d_inode(wdata->cfile->dentry);
3292 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3294 spin_lock(&inode->i_lock);
3295 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3296 if (cifsi->netfs.remote_i_size > inode->i_size)
3297 i_size_write(inode, cifsi->netfs.remote_i_size);
3298 spin_unlock(&inode->i_lock);
3300 complete(&wdata->done);
3301 collect_uncached_write_data(wdata->ctx);
3302 /* the below call can possibly free the last ref to aio ctx */
3303 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3307 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3308 struct cifs_aio_ctx *ctx)
3311 struct cifs_credits credits;
3313 struct TCP_Server_Info *server = wdata->server;
3316 if (wdata->cfile->invalidHandle) {
3317 rc = cifs_reopen_file(wdata->cfile, false);
3326 * Wait for credits to resend this wdata.
3327 * Note: we are attempting to resend the whole wdata not in
3331 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3336 if (wsize < wdata->bytes) {
3337 add_credits_and_wake_if(server, &credits, 0);
3340 } while (wsize < wdata->bytes);
3341 wdata->credits = credits;
3343 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3346 if (wdata->cfile->invalidHandle)
3349 wdata->replay = true;
3350 #ifdef CONFIG_CIFS_SMB_DIRECT
3352 wdata->mr->need_invalidate = true;
3353 smbd_deregister_mr(wdata->mr);
3357 rc = server->ops->async_writev(wdata,
3358 cifs_uncached_writedata_release);
3362 /* If the write was successfully sent, we are done */
3364 list_add_tail(&wdata->list, wdata_list);
3368 /* Roll back credits and retry if needed */
3369 add_credits_and_wake_if(server, &wdata->credits, 0);
3370 } while (rc == -EAGAIN);
3373 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3378 * Select span of a bvec iterator we're going to use. Limit it by both maximum
3379 * size and maximum number of segments.
3381 static size_t cifs_limit_bvec_subset(const struct iov_iter *iter, size_t max_size,
3382 size_t max_segs, unsigned int *_nsegs)
3384 const struct bio_vec *bvecs = iter->bvec;
3385 unsigned int nbv = iter->nr_segs, ix = 0, nsegs = 0;
3386 size_t len, span = 0, n = iter->count;
3387 size_t skip = iter->iov_offset;
3389 if (WARN_ON(!iov_iter_is_bvec(iter)) || n == 0)
3392 while (n && ix < nbv && skip) {
3393 len = bvecs[ix].bv_len;
3401 while (n && ix < nbv) {
3402 len = min3(n, bvecs[ix].bv_len - skip, max_size);
3407 if (max_size == 0 || nsegs >= max_segs)
3418 cifs_write_from_iter(loff_t fpos, size_t len, struct iov_iter *from,
3419 struct cifsFileInfo *open_file,
3420 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3421 struct cifs_aio_ctx *ctx)
3424 size_t cur_len, max_len;
3425 struct cifs_writedata *wdata;
3427 struct TCP_Server_Info *server;
3428 unsigned int xid, max_segs = INT_MAX;
3430 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3431 pid = open_file->pid;
3433 pid = current->tgid;
3435 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3438 #ifdef CONFIG_CIFS_SMB_DIRECT
3439 if (server->smbd_conn)
3440 max_segs = server->smbd_conn->max_frmr_depth;
3444 struct cifs_credits credits_on_stack;
3445 struct cifs_credits *credits = &credits_on_stack;
3446 unsigned int wsize, nsegs = 0;
3448 if (signal_pending(current)) {
3453 if (open_file->invalidHandle) {
3454 rc = cifs_reopen_file(open_file, false);
3461 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3466 max_len = min_t(const size_t, len, wsize);
3469 add_credits_and_wake_if(server, credits, 0);
3473 cur_len = cifs_limit_bvec_subset(from, max_len, max_segs, &nsegs);
3474 cifs_dbg(FYI, "write_from_iter len=%zx/%zx nsegs=%u/%lu/%u\n",
3475 cur_len, max_len, nsegs, from->nr_segs, max_segs);
3478 add_credits_and_wake_if(server, credits, 0);
3482 wdata = cifs_writedata_alloc(cifs_uncached_writev_complete);
3485 add_credits_and_wake_if(server, credits, 0);
3489 wdata->sync_mode = WB_SYNC_ALL;
3490 wdata->offset = (__u64)fpos;
3491 wdata->cfile = cifsFileInfo_get(open_file);
3492 wdata->server = server;
3494 wdata->bytes = cur_len;
3495 wdata->credits = credits_on_stack;
3496 wdata->iter = *from;
3498 kref_get(&ctx->refcount);
3500 iov_iter_truncate(&wdata->iter, cur_len);
3502 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3505 if (wdata->cfile->invalidHandle)
3508 rc = server->ops->async_writev(wdata,
3509 cifs_uncached_writedata_release);
3513 add_credits_and_wake_if(server, &wdata->credits, 0);
3514 kref_put(&wdata->refcount,
3515 cifs_uncached_writedata_release);
3521 list_add_tail(&wdata->list, wdata_list);
3522 iov_iter_advance(from, cur_len);
3531 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3533 struct cifs_writedata *wdata, *tmp;
3534 struct cifs_tcon *tcon;
3535 struct cifs_sb_info *cifs_sb;
3536 struct dentry *dentry = ctx->cfile->dentry;
3539 tcon = tlink_tcon(ctx->cfile->tlink);
3540 cifs_sb = CIFS_SB(dentry->d_sb);
3542 mutex_lock(&ctx->aio_mutex);
3544 if (list_empty(&ctx->list)) {
3545 mutex_unlock(&ctx->aio_mutex);
3551 * Wait for and collect replies for any successful sends in order of
3552 * increasing offset. Once an error is hit, then return without waiting
3553 * for any more replies.
3556 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3558 if (!try_wait_for_completion(&wdata->done)) {
3559 mutex_unlock(&ctx->aio_mutex);
3566 ctx->total_len += wdata->bytes;
3568 /* resend call if it's a retryable error */
3569 if (rc == -EAGAIN) {
3570 struct list_head tmp_list;
3571 struct iov_iter tmp_from = ctx->iter;
3573 INIT_LIST_HEAD(&tmp_list);
3574 list_del_init(&wdata->list);
3577 rc = cifs_resend_wdata(
3578 wdata, &tmp_list, ctx);
3580 iov_iter_advance(&tmp_from,
3581 wdata->offset - ctx->pos);
3583 rc = cifs_write_from_iter(wdata->offset,
3584 wdata->bytes, &tmp_from,
3585 ctx->cfile, cifs_sb, &tmp_list,
3588 kref_put(&wdata->refcount,
3589 cifs_uncached_writedata_release);
3592 list_splice(&tmp_list, &ctx->list);
3596 list_del_init(&wdata->list);
3597 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3600 cifs_stats_bytes_written(tcon, ctx->total_len);
3601 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3603 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3605 mutex_unlock(&ctx->aio_mutex);
3607 if (ctx->iocb && ctx->iocb->ki_complete)
3608 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
3610 complete(&ctx->done);
3613 static ssize_t __cifs_writev(
3614 struct kiocb *iocb, struct iov_iter *from, bool direct)
3616 struct file *file = iocb->ki_filp;
3617 ssize_t total_written = 0;
3618 struct cifsFileInfo *cfile;
3619 struct cifs_tcon *tcon;
3620 struct cifs_sb_info *cifs_sb;
3621 struct cifs_aio_ctx *ctx;
3624 rc = generic_write_checks(iocb, from);
3628 cifs_sb = CIFS_FILE_SB(file);
3629 cfile = file->private_data;
3630 tcon = tlink_tcon(cfile->tlink);
3632 if (!tcon->ses->server->ops->async_writev)
3635 ctx = cifs_aio_ctx_alloc();
3639 ctx->cfile = cifsFileInfo_get(cfile);
3641 if (!is_sync_kiocb(iocb))
3644 ctx->pos = iocb->ki_pos;
3645 ctx->direct_io = direct;
3646 ctx->nr_pinned_pages = 0;
3648 if (user_backed_iter(from)) {
3650 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
3651 * they contain references to the calling process's virtual
3652 * memory layout which won't be available in an async worker
3653 * thread. This also takes a pin on every folio involved.
3655 rc = netfs_extract_user_iter(from, iov_iter_count(from),
3658 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3662 ctx->nr_pinned_pages = rc;
3663 ctx->bv = (void *)ctx->iter.bvec;
3664 ctx->bv_need_unpin = iov_iter_extract_will_pin(from);
3665 } else if ((iov_iter_is_bvec(from) || iov_iter_is_kvec(from)) &&
3666 !is_sync_kiocb(iocb)) {
3668 * If the op is asynchronous, we need to copy the list attached
3669 * to a BVEC/KVEC-type iterator, but we assume that the storage
3670 * will be pinned by the caller; in any case, we may or may not
3671 * be able to pin the pages, so we don't try.
3673 ctx->bv = (void *)dup_iter(&ctx->iter, from, GFP_KERNEL);
3675 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3680 * Otherwise, we just pass the iterator down as-is and rely on
3681 * the caller to make sure the pages referred to by the
3682 * iterator don't evaporate.
3687 ctx->len = iov_iter_count(&ctx->iter);
3689 /* grab a lock here due to read response handlers can access ctx */
3690 mutex_lock(&ctx->aio_mutex);
3692 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &ctx->iter,
3693 cfile, cifs_sb, &ctx->list, ctx);
3696 * If at least one write was successfully sent, then discard any rc
3697 * value from the later writes. If the other write succeeds, then
3698 * we'll end up returning whatever was written. If it fails, then
3699 * we'll get a new rc value from that.
3701 if (!list_empty(&ctx->list))
3704 mutex_unlock(&ctx->aio_mutex);
3707 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3711 if (!is_sync_kiocb(iocb)) {
3712 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3713 return -EIOCBQUEUED;
3716 rc = wait_for_completion_killable(&ctx->done);
3718 mutex_lock(&ctx->aio_mutex);
3719 ctx->rc = rc = -EINTR;
3720 total_written = ctx->total_len;
3721 mutex_unlock(&ctx->aio_mutex);
3724 total_written = ctx->total_len;
3727 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3729 if (unlikely(!total_written))
3732 iocb->ki_pos += total_written;
3733 return total_written;
3736 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3738 struct file *file = iocb->ki_filp;
3740 cifs_revalidate_mapping(file->f_inode);
3741 return __cifs_writev(iocb, from, true);
3744 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3746 return __cifs_writev(iocb, from, false);
3750 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3752 struct file *file = iocb->ki_filp;
3753 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3754 struct inode *inode = file->f_mapping->host;
3755 struct cifsInodeInfo *cinode = CIFS_I(inode);
3756 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3761 * We need to hold the sem to be sure nobody modifies lock list
3762 * with a brlock that prevents writing.
3764 down_read(&cinode->lock_sem);
3766 rc = generic_write_checks(iocb, from);
3770 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3771 server->vals->exclusive_lock_type, 0,
3772 NULL, CIFS_WRITE_OP))
3773 rc = __generic_file_write_iter(iocb, from);
3777 up_read(&cinode->lock_sem);
3778 inode_unlock(inode);
3781 rc = generic_write_sync(iocb, rc);
3786 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3788 struct inode *inode = file_inode(iocb->ki_filp);
3789 struct cifsInodeInfo *cinode = CIFS_I(inode);
3790 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3791 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3792 iocb->ki_filp->private_data;
3793 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3796 written = cifs_get_writer(cinode);
3800 if (CIFS_CACHE_WRITE(cinode)) {
3801 if (cap_unix(tcon->ses) &&
3802 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3803 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3804 written = generic_file_write_iter(iocb, from);
3807 written = cifs_writev(iocb, from);
3811 * For non-oplocked files in strict cache mode we need to write the data
3812 * to the server exactly from the pos to pos+len-1 rather than flush all
3813 * affected pages because it may cause a error with mandatory locks on
3814 * these pages but not on the region from pos to ppos+len-1.
3816 written = cifs_user_writev(iocb, from);
3817 if (CIFS_CACHE_READ(cinode)) {
3819 * We have read level caching and we have just sent a write
3820 * request to the server thus making data in the cache stale.
3821 * Zap the cache and set oplock/lease level to NONE to avoid
3822 * reading stale data from the cache. All subsequent read
3823 * operations will read new data from the server.
3825 cifs_zap_mapping(inode);
3826 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3831 cifs_put_writer(cinode);
3835 static struct cifs_readdata *cifs_readdata_alloc(work_func_t complete)
3837 struct cifs_readdata *rdata;
3839 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3841 kref_init(&rdata->refcount);
3842 INIT_LIST_HEAD(&rdata->list);
3843 init_completion(&rdata->done);
3844 INIT_WORK(&rdata->work, complete);
3851 cifs_readdata_release(struct kref *refcount)
3853 struct cifs_readdata *rdata = container_of(refcount,
3854 struct cifs_readdata, refcount);
3857 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3858 #ifdef CONFIG_CIFS_SMB_DIRECT
3860 smbd_deregister_mr(rdata->mr);
3865 cifsFileInfo_put(rdata->cfile);
3870 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3873 cifs_uncached_readv_complete(struct work_struct *work)
3875 struct cifs_readdata *rdata = container_of(work,
3876 struct cifs_readdata, work);
3878 complete(&rdata->done);
3879 collect_uncached_read_data(rdata->ctx);
3880 /* the below call can possibly free the last ref to aio ctx */
3881 kref_put(&rdata->refcount, cifs_readdata_release);
3884 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3885 struct list_head *rdata_list,
3886 struct cifs_aio_ctx *ctx)
3889 struct cifs_credits credits;
3891 struct TCP_Server_Info *server;
3893 /* XXX: should we pick a new channel here? */
3894 server = rdata->server;
3897 if (rdata->cfile->invalidHandle) {
3898 rc = cifs_reopen_file(rdata->cfile, true);
3906 * Wait for credits to resend this rdata.
3907 * Note: we are attempting to resend the whole rdata not in
3911 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3917 if (rsize < rdata->bytes) {
3918 add_credits_and_wake_if(server, &credits, 0);
3921 } while (rsize < rdata->bytes);
3922 rdata->credits = credits;
3924 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3926 if (rdata->cfile->invalidHandle)
3929 #ifdef CONFIG_CIFS_SMB_DIRECT
3931 rdata->mr->need_invalidate = true;
3932 smbd_deregister_mr(rdata->mr);
3936 rc = server->ops->async_readv(rdata);
3940 /* If the read was successfully sent, we are done */
3942 /* Add to aio pending list */
3943 list_add_tail(&rdata->list, rdata_list);
3947 /* Roll back credits and retry if needed */
3948 add_credits_and_wake_if(server, &rdata->credits, 0);
3949 } while (rc == -EAGAIN);
3952 kref_put(&rdata->refcount, cifs_readdata_release);
3957 cifs_send_async_read(loff_t fpos, size_t len, struct cifsFileInfo *open_file,
3958 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3959 struct cifs_aio_ctx *ctx)
3961 struct cifs_readdata *rdata;
3962 unsigned int rsize, nsegs, max_segs = INT_MAX;
3963 struct cifs_credits credits_on_stack;
3964 struct cifs_credits *credits = &credits_on_stack;
3965 size_t cur_len, max_len;
3968 struct TCP_Server_Info *server;
3970 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3972 #ifdef CONFIG_CIFS_SMB_DIRECT
3973 if (server->smbd_conn)
3974 max_segs = server->smbd_conn->max_frmr_depth;
3977 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3978 pid = open_file->pid;
3980 pid = current->tgid;
3983 if (open_file->invalidHandle) {
3984 rc = cifs_reopen_file(open_file, true);
3991 if (cifs_sb->ctx->rsize == 0)
3992 cifs_sb->ctx->rsize =
3993 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
3996 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4001 max_len = min_t(size_t, len, rsize);
4003 cur_len = cifs_limit_bvec_subset(&ctx->iter, max_len,
4005 cifs_dbg(FYI, "read-to-iter len=%zx/%zx nsegs=%u/%lu/%u\n",
4006 cur_len, max_len, nsegs, ctx->iter.nr_segs, max_segs);
4009 add_credits_and_wake_if(server, credits, 0);
4013 rdata = cifs_readdata_alloc(cifs_uncached_readv_complete);
4015 add_credits_and_wake_if(server, credits, 0);
4020 rdata->server = server;
4021 rdata->cfile = cifsFileInfo_get(open_file);
4022 rdata->offset = fpos;
4023 rdata->bytes = cur_len;
4025 rdata->credits = credits_on_stack;
4027 kref_get(&ctx->refcount);
4029 rdata->iter = ctx->iter;
4030 iov_iter_truncate(&rdata->iter, cur_len);
4032 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4035 if (rdata->cfile->invalidHandle)
4038 rc = server->ops->async_readv(rdata);
4042 add_credits_and_wake_if(server, &rdata->credits, 0);
4043 kref_put(&rdata->refcount, cifs_readdata_release);
4049 list_add_tail(&rdata->list, rdata_list);
4050 iov_iter_advance(&ctx->iter, cur_len);
4059 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4061 struct cifs_readdata *rdata, *tmp;
4062 struct cifs_sb_info *cifs_sb;
4065 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4067 mutex_lock(&ctx->aio_mutex);
4069 if (list_empty(&ctx->list)) {
4070 mutex_unlock(&ctx->aio_mutex);
4075 /* the loop below should proceed in the order of increasing offsets */
4077 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4079 if (!try_wait_for_completion(&rdata->done)) {
4080 mutex_unlock(&ctx->aio_mutex);
4084 if (rdata->result == -EAGAIN) {
4085 /* resend call if it's a retryable error */
4086 struct list_head tmp_list;
4087 unsigned int got_bytes = rdata->got_bytes;
4089 list_del_init(&rdata->list);
4090 INIT_LIST_HEAD(&tmp_list);
4092 if (ctx->direct_io) {
4094 * Re-use rdata as this is a
4097 rc = cifs_resend_rdata(
4101 rc = cifs_send_async_read(
4102 rdata->offset + got_bytes,
4103 rdata->bytes - got_bytes,
4104 rdata->cfile, cifs_sb,
4107 kref_put(&rdata->refcount,
4108 cifs_readdata_release);
4111 list_splice(&tmp_list, &ctx->list);
4114 } else if (rdata->result)
4117 /* if there was a short read -- discard anything left */
4118 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4121 ctx->total_len += rdata->got_bytes;
4123 list_del_init(&rdata->list);
4124 kref_put(&rdata->refcount, cifs_readdata_release);
4127 /* mask nodata case */
4131 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4133 mutex_unlock(&ctx->aio_mutex);
4135 if (ctx->iocb && ctx->iocb->ki_complete)
4136 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
4138 complete(&ctx->done);
4141 static ssize_t __cifs_readv(
4142 struct kiocb *iocb, struct iov_iter *to, bool direct)
4145 struct file *file = iocb->ki_filp;
4146 struct cifs_sb_info *cifs_sb;
4147 struct cifsFileInfo *cfile;
4148 struct cifs_tcon *tcon;
4149 ssize_t rc, total_read = 0;
4150 loff_t offset = iocb->ki_pos;
4151 struct cifs_aio_ctx *ctx;
4153 len = iov_iter_count(to);
4157 cifs_sb = CIFS_FILE_SB(file);
4158 cfile = file->private_data;
4159 tcon = tlink_tcon(cfile->tlink);
4161 if (!tcon->ses->server->ops->async_readv)
4164 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4165 cifs_dbg(FYI, "attempting read on write only file instance\n");
4167 ctx = cifs_aio_ctx_alloc();
4172 ctx->direct_io = direct;
4174 ctx->cfile = cifsFileInfo_get(cfile);
4175 ctx->nr_pinned_pages = 0;
4177 if (!is_sync_kiocb(iocb))
4180 if (user_backed_iter(to)) {
4182 * Extract IOVEC/UBUF-type iterators to a BVEC-type iterator as
4183 * they contain references to the calling process's virtual
4184 * memory layout which won't be available in an async worker
4185 * thread. This also takes a pin on every folio involved.
4187 rc = netfs_extract_user_iter(to, iov_iter_count(to),
4190 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4194 ctx->nr_pinned_pages = rc;
4195 ctx->bv = (void *)ctx->iter.bvec;
4196 ctx->bv_need_unpin = iov_iter_extract_will_pin(to);
4197 ctx->should_dirty = true;
4198 } else if ((iov_iter_is_bvec(to) || iov_iter_is_kvec(to)) &&
4199 !is_sync_kiocb(iocb)) {
4201 * If the op is asynchronous, we need to copy the list attached
4202 * to a BVEC/KVEC-type iterator, but we assume that the storage
4203 * will be retained by the caller; in any case, we may or may
4204 * not be able to pin the pages, so we don't try.
4206 ctx->bv = (void *)dup_iter(&ctx->iter, to, GFP_KERNEL);
4208 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4213 * Otherwise, we just pass the iterator down as-is and rely on
4214 * the caller to make sure the pages referred to by the
4215 * iterator don't evaporate.
4221 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
4222 offset, offset + len - 1);
4224 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4229 /* grab a lock here due to read response handlers can access ctx */
4230 mutex_lock(&ctx->aio_mutex);
4232 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4234 /* if at least one read request send succeeded, then reset rc */
4235 if (!list_empty(&ctx->list))
4238 mutex_unlock(&ctx->aio_mutex);
4241 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4245 if (!is_sync_kiocb(iocb)) {
4246 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4247 return -EIOCBQUEUED;
4250 rc = wait_for_completion_killable(&ctx->done);
4252 mutex_lock(&ctx->aio_mutex);
4253 ctx->rc = rc = -EINTR;
4254 total_read = ctx->total_len;
4255 mutex_unlock(&ctx->aio_mutex);
4258 total_read = ctx->total_len;
4261 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4264 iocb->ki_pos += total_read;
4270 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4272 return __cifs_readv(iocb, to, true);
4275 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4277 return __cifs_readv(iocb, to, false);
4281 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4283 struct inode *inode = file_inode(iocb->ki_filp);
4284 struct cifsInodeInfo *cinode = CIFS_I(inode);
4285 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4286 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4287 iocb->ki_filp->private_data;
4288 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4292 * In strict cache mode we need to read from the server all the time
4293 * if we don't have level II oplock because the server can delay mtime
4294 * change - so we can't make a decision about inode invalidating.
4295 * And we can also fail with pagereading if there are mandatory locks
4296 * on pages affected by this read but not on the region from pos to
4299 if (!CIFS_CACHE_READ(cinode))
4300 return cifs_user_readv(iocb, to);
4302 if (cap_unix(tcon->ses) &&
4303 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4304 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4305 return generic_file_read_iter(iocb, to);
4308 * We need to hold the sem to be sure nobody modifies lock list
4309 * with a brlock that prevents reading.
4311 down_read(&cinode->lock_sem);
4312 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4313 tcon->ses->server->vals->shared_lock_type,
4314 0, NULL, CIFS_READ_OP))
4315 rc = generic_file_read_iter(iocb, to);
4316 up_read(&cinode->lock_sem);
4321 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4324 unsigned int bytes_read = 0;
4325 unsigned int total_read;
4326 unsigned int current_read_size;
4328 struct cifs_sb_info *cifs_sb;
4329 struct cifs_tcon *tcon;
4330 struct TCP_Server_Info *server;
4333 struct cifsFileInfo *open_file;
4334 struct cifs_io_parms io_parms = {0};
4335 int buf_type = CIFS_NO_BUFFER;
4339 cifs_sb = CIFS_FILE_SB(file);
4341 /* FIXME: set up handlers for larger reads and/or convert to async */
4342 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4344 if (file->private_data == NULL) {
4349 open_file = file->private_data;
4350 tcon = tlink_tcon(open_file->tlink);
4351 server = cifs_pick_channel(tcon->ses);
4353 if (!server->ops->sync_read) {
4358 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4359 pid = open_file->pid;
4361 pid = current->tgid;
4363 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4364 cifs_dbg(FYI, "attempting read on write only file instance\n");
4366 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4367 total_read += bytes_read, cur_offset += bytes_read) {
4369 current_read_size = min_t(uint, read_size - total_read,
4372 * For windows me and 9x we do not want to request more
4373 * than it negotiated since it will refuse the read
4376 if (!(tcon->ses->capabilities &
4377 tcon->ses->server->vals->cap_large_files)) {
4378 current_read_size = min_t(uint,
4379 current_read_size, CIFSMaxBufSize);
4381 if (open_file->invalidHandle) {
4382 rc = cifs_reopen_file(open_file, true);
4387 io_parms.tcon = tcon;
4388 io_parms.offset = *offset;
4389 io_parms.length = current_read_size;
4390 io_parms.server = server;
4391 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4392 &bytes_read, &cur_offset,
4394 } while (rc == -EAGAIN);
4396 if (rc || (bytes_read == 0)) {
4404 cifs_stats_bytes_read(tcon, total_read);
4405 *offset += bytes_read;
4413 * If the page is mmap'ed into a process' page tables, then we need to make
4414 * sure that it doesn't change while being written back.
4416 static vm_fault_t cifs_page_mkwrite(struct vm_fault *vmf)
4418 struct folio *folio = page_folio(vmf->page);
4420 /* Wait for the folio to be written to the cache before we allow it to
4421 * be modified. We then assume the entire folio will need writing back.
4423 #ifdef CONFIG_CIFS_FSCACHE
4424 if (folio_test_fscache(folio) &&
4425 folio_wait_fscache_killable(folio) < 0)
4426 return VM_FAULT_RETRY;
4429 folio_wait_writeback(folio);
4431 if (folio_lock_killable(folio) < 0)
4432 return VM_FAULT_RETRY;
4433 return VM_FAULT_LOCKED;
4436 static const struct vm_operations_struct cifs_file_vm_ops = {
4437 .fault = filemap_fault,
4438 .map_pages = filemap_map_pages,
4439 .page_mkwrite = cifs_page_mkwrite,
4442 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4445 struct inode *inode = file_inode(file);
4449 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4450 rc = cifs_zap_mapping(inode);
4452 rc = generic_file_mmap(file, vma);
4454 vma->vm_ops = &cifs_file_vm_ops;
4460 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4466 rc = cifs_revalidate_file(file);
4468 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4471 rc = generic_file_mmap(file, vma);
4473 vma->vm_ops = &cifs_file_vm_ops;
4480 * Unlock a bunch of folios in the pagecache.
4482 static void cifs_unlock_folios(struct address_space *mapping, pgoff_t first, pgoff_t last)
4484 struct folio *folio;
4485 XA_STATE(xas, &mapping->i_pages, first);
4488 xas_for_each(&xas, folio, last) {
4489 folio_unlock(folio);
4494 static void cifs_readahead_complete(struct work_struct *work)
4496 struct cifs_readdata *rdata = container_of(work,
4497 struct cifs_readdata, work);
4498 struct folio *folio;
4500 bool good = rdata->result == 0 || (rdata->result == -EAGAIN && rdata->got_bytes);
4502 XA_STATE(xas, &rdata->mapping->i_pages, rdata->offset / PAGE_SIZE);
4505 cifs_readahead_to_fscache(rdata->mapping->host,
4506 rdata->offset, rdata->bytes);
4508 if (iov_iter_count(&rdata->iter) > 0)
4509 iov_iter_zero(iov_iter_count(&rdata->iter), &rdata->iter);
4511 last = (rdata->offset + rdata->bytes - 1) / PAGE_SIZE;
4514 xas_for_each(&xas, folio, last) {
4516 flush_dcache_folio(folio);
4517 folio_mark_uptodate(folio);
4519 folio_unlock(folio);
4523 kref_put(&rdata->refcount, cifs_readdata_release);
4526 static void cifs_readahead(struct readahead_control *ractl)
4528 struct cifsFileInfo *open_file = ractl->file->private_data;
4529 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
4530 struct TCP_Server_Info *server;
4531 unsigned int xid, nr_pages, cache_nr_pages = 0;
4532 unsigned int ra_pages;
4533 pgoff_t next_cached = ULONG_MAX, ra_index;
4534 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
4535 cifs_inode_cookie(ractl->mapping->host)->cache_priv;
4536 bool check_cache = caching;
4540 /* Note that readahead_count() lags behind our dequeuing of pages from
4541 * the ractl, wo we have to keep track for ourselves.
4543 ra_pages = readahead_count(ractl);
4544 ra_index = readahead_index(ractl);
4548 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4549 pid = open_file->pid;
4551 pid = current->tgid;
4553 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4555 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4556 __func__, ractl->file, ractl->mapping, ra_pages);
4559 * Chop the readahead request up into rsize-sized read requests.
4561 while ((nr_pages = ra_pages)) {
4562 unsigned int i, rsize;
4563 struct cifs_readdata *rdata;
4564 struct cifs_credits credits_on_stack;
4565 struct cifs_credits *credits = &credits_on_stack;
4566 struct folio *folio;
4570 * Find out if we have anything cached in the range of
4571 * interest, and if so, where the next chunk of cached data is.
4575 rc = cifs_fscache_query_occupancy(
4576 ractl->mapping->host, ra_index, nr_pages,
4577 &next_cached, &cache_nr_pages);
4580 check_cache = false;
4583 if (ra_index == next_cached) {
4585 * TODO: Send a whole batch of pages to be read
4588 folio = readahead_folio(ractl);
4589 fsize = folio_nr_pages(folio);
4592 if (cifs_readpage_from_fscache(ractl->mapping->host,
4593 &folio->page) < 0) {
4595 * TODO: Deal with cache read failure
4596 * here, but for the moment, delegate
4601 folio_unlock(folio);
4602 next_cached += fsize;
4603 cache_nr_pages -= fsize;
4604 if (cache_nr_pages == 0)
4610 if (open_file->invalidHandle) {
4611 rc = cifs_reopen_file(open_file, true);
4619 if (cifs_sb->ctx->rsize == 0)
4620 cifs_sb->ctx->rsize =
4621 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4624 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4628 nr_pages = min_t(size_t, rsize / PAGE_SIZE, ra_pages);
4629 if (next_cached != ULONG_MAX)
4630 nr_pages = min_t(size_t, nr_pages, next_cached - ra_index);
4633 * Give up immediately if rsize is too small to read an entire
4634 * page. The VFS will fall back to readpage. We should never
4635 * reach this point however since we set ra_pages to 0 when the
4636 * rsize is smaller than a cache page.
4638 if (unlikely(!nr_pages)) {
4639 add_credits_and_wake_if(server, credits, 0);
4643 rdata = cifs_readdata_alloc(cifs_readahead_complete);
4645 /* best to give up if we're out of mem */
4646 add_credits_and_wake_if(server, credits, 0);
4650 rdata->offset = ra_index * PAGE_SIZE;
4651 rdata->bytes = nr_pages * PAGE_SIZE;
4652 rdata->cfile = cifsFileInfo_get(open_file);
4653 rdata->server = server;
4654 rdata->mapping = ractl->mapping;
4656 rdata->credits = credits_on_stack;
4658 for (i = 0; i < nr_pages; i++) {
4659 if (!readahead_folio(ractl))
4662 ra_pages -= nr_pages;
4663 ra_index += nr_pages;
4665 iov_iter_xarray(&rdata->iter, ITER_DEST, &rdata->mapping->i_pages,
4666 rdata->offset, rdata->bytes);
4668 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4670 if (rdata->cfile->invalidHandle)
4673 rc = server->ops->async_readv(rdata);
4677 add_credits_and_wake_if(server, &rdata->credits, 0);
4678 cifs_unlock_folios(rdata->mapping,
4679 rdata->offset / PAGE_SIZE,
4680 (rdata->offset + rdata->bytes - 1) / PAGE_SIZE);
4681 /* Fallback to the readpage in error/reconnect cases */
4682 kref_put(&rdata->refcount, cifs_readdata_release);
4686 kref_put(&rdata->refcount, cifs_readdata_release);
4693 * cifs_readpage_worker must be called with the page pinned
4695 static int cifs_readpage_worker(struct file *file, struct page *page,
4698 struct inode *inode = file_inode(file);
4699 struct timespec64 atime, mtime;
4703 /* Is the page cached? */
4704 rc = cifs_readpage_from_fscache(inode, page);
4708 read_data = kmap(page);
4709 /* for reads over a certain size could initiate async read ahead */
4711 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4716 cifs_dbg(FYI, "Bytes read %d\n", rc);
4718 /* we do not want atime to be less than mtime, it broke some apps */
4719 atime = inode_set_atime_to_ts(inode, current_time(inode));
4720 mtime = inode_get_mtime(inode);
4721 if (timespec64_compare(&atime, &mtime) < 0)
4722 inode_set_atime_to_ts(inode, inode_get_mtime(inode));
4725 memset(read_data + rc, 0, PAGE_SIZE - rc);
4727 flush_dcache_page(page);
4728 SetPageUptodate(page);
4739 static int cifs_read_folio(struct file *file, struct folio *folio)
4741 struct page *page = &folio->page;
4742 loff_t offset = page_file_offset(page);
4748 if (file->private_data == NULL) {
4754 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
4755 page, (int)offset, (int)offset);
4757 rc = cifs_readpage_worker(file, page, &offset);
4763 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4765 struct cifsFileInfo *open_file;
4767 spin_lock(&cifs_inode->open_file_lock);
4768 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4769 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4770 spin_unlock(&cifs_inode->open_file_lock);
4774 spin_unlock(&cifs_inode->open_file_lock);
4778 /* We do not want to update the file size from server for inodes
4779 open for write - to avoid races with writepage extending
4780 the file - in the future we could consider allowing
4781 refreshing the inode only on increases in the file size
4782 but this is tricky to do without racing with writebehind
4783 page caching in the current Linux kernel design */
4784 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file,
4790 if (is_inode_writable(cifsInode) ||
4791 ((cifsInode->oplock & CIFS_CACHE_RW_FLG) != 0 && from_readdir)) {
4792 /* This inode is open for write at least once */
4793 struct cifs_sb_info *cifs_sb;
4795 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
4796 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4797 /* since no page cache to corrupt on directio
4798 we can change size safely */
4802 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
4810 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4811 loff_t pos, unsigned len,
4812 struct page **pagep, void **fsdata)
4815 pgoff_t index = pos >> PAGE_SHIFT;
4816 loff_t offset = pos & (PAGE_SIZE - 1);
4817 loff_t page_start = pos & PAGE_MASK;
4822 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4825 page = grab_cache_page_write_begin(mapping, index);
4831 if (PageUptodate(page))
4835 * If we write a full page it will be up to date, no need to read from
4836 * the server. If the write is short, we'll end up doing a sync write
4839 if (len == PAGE_SIZE)
4843 * optimize away the read when we have an oplock, and we're not
4844 * expecting to use any of the data we'd be reading in. That
4845 * is, when the page lies beyond the EOF, or straddles the EOF
4846 * and the write will cover all of the existing data.
4848 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
4849 i_size = i_size_read(mapping->host);
4850 if (page_start >= i_size ||
4851 (offset == 0 && (pos + len) >= i_size)) {
4852 zero_user_segments(page, 0, offset,
4856 * PageChecked means that the parts of the page
4857 * to which we're not writing are considered up
4858 * to date. Once the data is copied to the
4859 * page, it can be set uptodate.
4861 SetPageChecked(page);
4866 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
4868 * might as well read a page, it is fast enough. If we get
4869 * an error, we don't need to return it. cifs_write_end will
4870 * do a sync write instead since PG_uptodate isn't set.
4872 cifs_readpage_worker(file, page, &page_start);
4877 /* we could try using another file handle if there is one -
4878 but how would we lock it to prevent close of that handle
4879 racing with this read? In any case
4880 this will be written out by write_end so is fine */
4887 static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
4889 if (folio_test_private(folio))
4891 if (folio_test_fscache(folio)) {
4892 if (current_is_kswapd() || !(gfp & __GFP_FS))
4894 folio_wait_fscache(folio);
4896 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
4900 static void cifs_invalidate_folio(struct folio *folio, size_t offset,
4903 folio_wait_fscache(folio);
4906 static int cifs_launder_folio(struct folio *folio)
4909 loff_t range_start = folio_pos(folio);
4910 loff_t range_end = range_start + folio_size(folio);
4911 struct writeback_control wbc = {
4912 .sync_mode = WB_SYNC_ALL,
4914 .range_start = range_start,
4915 .range_end = range_end,
4918 cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
4920 if (folio_clear_dirty_for_io(folio))
4921 rc = cifs_writepage_locked(&folio->page, &wbc);
4923 folio_wait_fscache(folio);
4927 void cifs_oplock_break(struct work_struct *work)
4929 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
4931 struct inode *inode = d_inode(cfile->dentry);
4932 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4933 struct cifsInodeInfo *cinode = CIFS_I(inode);
4934 struct cifs_tcon *tcon;
4935 struct TCP_Server_Info *server;
4936 struct tcon_link *tlink;
4938 bool purge_cache = false, oplock_break_cancelled;
4939 __u64 persistent_fid, volatile_fid;
4942 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
4943 TASK_UNINTERRUPTIBLE);
4945 tlink = cifs_sb_tlink(cifs_sb);
4948 tcon = tlink_tcon(tlink);
4949 server = tcon->ses->server;
4951 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
4952 cfile->oplock_epoch, &purge_cache);
4954 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
4955 cifs_has_mand_locks(cinode)) {
4956 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
4961 if (inode && S_ISREG(inode->i_mode)) {
4962 if (CIFS_CACHE_READ(cinode))
4963 break_lease(inode, O_RDONLY);
4965 break_lease(inode, O_WRONLY);
4966 rc = filemap_fdatawrite(inode->i_mapping);
4967 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
4968 rc = filemap_fdatawait(inode->i_mapping);
4969 mapping_set_error(inode->i_mapping, rc);
4970 cifs_zap_mapping(inode);
4972 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
4973 if (CIFS_CACHE_WRITE(cinode))
4974 goto oplock_break_ack;
4977 rc = cifs_push_locks(cfile);
4979 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
4983 * When oplock break is received and there are no active
4984 * file handles but cached, then schedule deferred close immediately.
4985 * So, new open will not use cached handle.
4988 if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
4989 cifs_close_deferred_file(cinode);
4991 persistent_fid = cfile->fid.persistent_fid;
4992 volatile_fid = cfile->fid.volatile_fid;
4993 net_fid = cfile->fid.netfid;
4994 oplock_break_cancelled = cfile->oplock_break_cancelled;
4996 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
4998 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
4999 * an acknowledgment to be sent when the file has already been closed.
5001 spin_lock(&cinode->open_file_lock);
5002 /* check list empty since can race with kill_sb calling tree disconnect */
5003 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
5004 spin_unlock(&cinode->open_file_lock);
5005 rc = server->ops->oplock_response(tcon, persistent_fid,
5006 volatile_fid, net_fid, cinode);
5007 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
5009 spin_unlock(&cinode->open_file_lock);
5011 cifs_put_tlink(tlink);
5013 cifs_done_oplock_break(cinode);
5017 * The presence of cifs_direct_io() in the address space ops vector
5018 * allowes open() O_DIRECT flags which would have failed otherwise.
5020 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
5021 * so this method should never be called.
5023 * Direct IO is not yet supported in the cached mode.
5026 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
5030 * Eventually need to support direct IO for non forcedirectio mounts
5035 static int cifs_swap_activate(struct swap_info_struct *sis,
5036 struct file *swap_file, sector_t *span)
5038 struct cifsFileInfo *cfile = swap_file->private_data;
5039 struct inode *inode = swap_file->f_mapping->host;
5040 unsigned long blocks;
5043 cifs_dbg(FYI, "swap activate\n");
5045 if (!swap_file->f_mapping->a_ops->swap_rw)
5046 /* Cannot support swap */
5049 spin_lock(&inode->i_lock);
5050 blocks = inode->i_blocks;
5051 isize = inode->i_size;
5052 spin_unlock(&inode->i_lock);
5053 if (blocks*512 < isize) {
5054 pr_warn("swap activate: swapfile has holes\n");
5059 pr_warn_once("Swap support over SMB3 is experimental\n");
5062 * TODO: consider adding ACL (or documenting how) to prevent other
5063 * users (on this or other systems) from reading it
5067 /* TODO: add sk_set_memalloc(inet) or similar */
5070 cfile->swapfile = true;
5072 * TODO: Since file already open, we can't open with DENY_ALL here
5073 * but we could add call to grab a byte range lock to prevent others
5074 * from reading or writing the file
5077 sis->flags |= SWP_FS_OPS;
5078 return add_swap_extent(sis, 0, sis->max, 0);
5081 static void cifs_swap_deactivate(struct file *file)
5083 struct cifsFileInfo *cfile = file->private_data;
5085 cifs_dbg(FYI, "swap deactivate\n");
5087 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5090 cfile->swapfile = false;
5092 /* do we need to unpin (or unlock) the file */
5095 const struct address_space_operations cifs_addr_ops = {
5096 .read_folio = cifs_read_folio,
5097 .readahead = cifs_readahead,
5098 .writepages = cifs_writepages,
5099 .write_begin = cifs_write_begin,
5100 .write_end = cifs_write_end,
5101 .dirty_folio = netfs_dirty_folio,
5102 .release_folio = cifs_release_folio,
5103 .direct_IO = cifs_direct_io,
5104 .invalidate_folio = cifs_invalidate_folio,
5105 .launder_folio = cifs_launder_folio,
5106 .migrate_folio = filemap_migrate_folio,
5108 * TODO: investigate and if useful we could add an is_dirty_writeback
5111 .swap_activate = cifs_swap_activate,
5112 .swap_deactivate = cifs_swap_deactivate,
5116 * cifs_readahead requires the server to support a buffer large enough to
5117 * contain the header plus one complete page of data. Otherwise, we need
5118 * to leave cifs_readahead out of the address space operations.
5120 const struct address_space_operations cifs_addr_ops_smallbuf = {
5121 .read_folio = cifs_read_folio,
5122 .writepages = cifs_writepages,
5123 .write_begin = cifs_write_begin,
5124 .write_end = cifs_write_end,
5125 .dirty_folio = netfs_dirty_folio,
5126 .release_folio = cifs_release_folio,
5127 .invalidate_folio = cifs_invalidate_folio,
5128 .launder_folio = cifs_launder_folio,
5129 .migrate_folio = filemap_migrate_folio,