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/backing-dev.h>
13 #include <linux/stat.h>
14 #include <linux/fcntl.h>
15 #include <linux/pagemap.h>
16 #include <linux/pagevec.h>
17 #include <linux/writeback.h>
18 #include <linux/task_io_accounting_ops.h>
19 #include <linux/delay.h>
20 #include <linux/mount.h>
21 #include <linux/slab.h>
22 #include <linux/swap.h>
24 #include <asm/div64.h>
28 #include "cifsproto.h"
29 #include "smb2proto.h"
30 #include "cifs_unicode.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
34 #include "smbdirect.h"
35 #include "fs_context.h"
36 #include "cifs_ioctl.h"
37 #include "cached_dir.h"
40 * Mark as invalid, all open files on tree connections since they
41 * were closed when session to server was lost.
44 cifs_mark_open_files_invalid(struct cifs_tcon *tcon)
46 struct cifsFileInfo *open_file = NULL;
47 struct list_head *tmp;
48 struct list_head *tmp1;
50 /* only send once per connect */
51 spin_lock(&tcon->ses->ses_lock);
52 if ((tcon->ses->ses_status != SES_GOOD) || (tcon->status != TID_NEED_RECON)) {
53 spin_unlock(&tcon->ses->ses_lock);
56 tcon->status = TID_IN_FILES_INVALIDATE;
57 spin_unlock(&tcon->ses->ses_lock);
59 /* list all files open on tree connection and mark them invalid */
60 spin_lock(&tcon->open_file_lock);
61 list_for_each_safe(tmp, tmp1, &tcon->openFileList) {
62 open_file = list_entry(tmp, struct cifsFileInfo, tlist);
63 open_file->invalidHandle = true;
64 open_file->oplock_break_cancelled = true;
66 spin_unlock(&tcon->open_file_lock);
68 invalidate_all_cached_dirs(tcon);
69 spin_lock(&tcon->tc_lock);
70 if (tcon->status == TID_IN_FILES_INVALIDATE)
71 tcon->status = TID_NEED_TCON;
72 spin_unlock(&tcon->tc_lock);
75 * BB Add call to invalidate_inodes(sb) for all superblocks mounted
80 static inline int cifs_convert_flags(unsigned int flags)
82 if ((flags & O_ACCMODE) == O_RDONLY)
84 else if ((flags & O_ACCMODE) == O_WRONLY)
86 else if ((flags & O_ACCMODE) == O_RDWR) {
87 /* GENERIC_ALL is too much permission to request
88 can cause unnecessary access denied on create */
89 /* return GENERIC_ALL; */
90 return (GENERIC_READ | GENERIC_WRITE);
93 return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
94 FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
98 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
99 static u32 cifs_posix_convert_flags(unsigned int flags)
103 if ((flags & O_ACCMODE) == O_RDONLY)
104 posix_flags = SMB_O_RDONLY;
105 else if ((flags & O_ACCMODE) == O_WRONLY)
106 posix_flags = SMB_O_WRONLY;
107 else if ((flags & O_ACCMODE) == O_RDWR)
108 posix_flags = SMB_O_RDWR;
110 if (flags & O_CREAT) {
111 posix_flags |= SMB_O_CREAT;
113 posix_flags |= SMB_O_EXCL;
114 } else if (flags & O_EXCL)
115 cifs_dbg(FYI, "Application %s pid %d has incorrectly set O_EXCL flag but not O_CREAT on file open. Ignoring O_EXCL\n",
116 current->comm, current->tgid);
119 posix_flags |= SMB_O_TRUNC;
120 /* be safe and imply O_SYNC for O_DSYNC */
122 posix_flags |= SMB_O_SYNC;
123 if (flags & O_DIRECTORY)
124 posix_flags |= SMB_O_DIRECTORY;
125 if (flags & O_NOFOLLOW)
126 posix_flags |= SMB_O_NOFOLLOW;
127 if (flags & O_DIRECT)
128 posix_flags |= SMB_O_DIRECT;
132 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
134 static inline int cifs_get_disposition(unsigned int flags)
136 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
138 else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
139 return FILE_OVERWRITE_IF;
140 else if ((flags & O_CREAT) == O_CREAT)
142 else if ((flags & O_TRUNC) == O_TRUNC)
143 return FILE_OVERWRITE;
148 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
149 int cifs_posix_open(const char *full_path, struct inode **pinode,
150 struct super_block *sb, int mode, unsigned int f_flags,
151 __u32 *poplock, __u16 *pnetfid, unsigned int xid)
154 FILE_UNIX_BASIC_INFO *presp_data;
155 __u32 posix_flags = 0;
156 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
157 struct cifs_fattr fattr;
158 struct tcon_link *tlink;
159 struct cifs_tcon *tcon;
161 cifs_dbg(FYI, "posix open %s\n", full_path);
163 presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
164 if (presp_data == NULL)
167 tlink = cifs_sb_tlink(cifs_sb);
173 tcon = tlink_tcon(tlink);
174 mode &= ~current_umask();
176 posix_flags = cifs_posix_convert_flags(f_flags);
177 rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
178 poplock, full_path, cifs_sb->local_nls,
179 cifs_remap(cifs_sb));
180 cifs_put_tlink(tlink);
185 if (presp_data->Type == cpu_to_le32(-1))
186 goto posix_open_ret; /* open ok, caller does qpathinfo */
189 goto posix_open_ret; /* caller does not need info */
191 cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
193 /* get new inode and set it up */
194 if (*pinode == NULL) {
195 cifs_fill_uniqueid(sb, &fattr);
196 *pinode = cifs_iget(sb, &fattr);
202 cifs_revalidate_mapping(*pinode);
203 rc = cifs_fattr_to_inode(*pinode, &fattr);
210 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
212 static int cifs_nt_open(const char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb,
213 struct cifs_tcon *tcon, unsigned int f_flags, __u32 *oplock,
214 struct cifs_fid *fid, unsigned int xid, struct cifs_open_info_data *buf)
219 int create_options = CREATE_NOT_DIR;
220 struct TCP_Server_Info *server = tcon->ses->server;
221 struct cifs_open_parms oparms;
223 if (!server->ops->open)
226 desired_access = cifs_convert_flags(f_flags);
228 /*********************************************************************
229 * open flag mapping table:
231 * POSIX Flag CIFS Disposition
232 * ---------- ----------------
233 * O_CREAT FILE_OPEN_IF
234 * O_CREAT | O_EXCL FILE_CREATE
235 * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
236 * O_TRUNC FILE_OVERWRITE
237 * none of the above FILE_OPEN
239 * Note that there is not a direct match between disposition
240 * FILE_SUPERSEDE (ie create whether or not file exists although
241 * O_CREAT | O_TRUNC is similar but truncates the existing
242 * file rather than creating a new file as FILE_SUPERSEDE does
243 * (which uses the attributes / metadata passed in on open call)
245 *? O_SYNC is a reasonable match to CIFS writethrough flag
246 *? and the read write flags match reasonably. O_LARGEFILE
247 *? is irrelevant because largefile support is always used
248 *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
249 * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
250 *********************************************************************/
252 disposition = cifs_get_disposition(f_flags);
254 /* BB pass O_SYNC flag through on file attributes .. BB */
256 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
257 if (f_flags & O_SYNC)
258 create_options |= CREATE_WRITE_THROUGH;
260 if (f_flags & O_DIRECT)
261 create_options |= CREATE_NO_BUFFER;
263 oparms = (struct cifs_open_parms) {
266 .desired_access = desired_access,
267 .create_options = cifs_create_options(cifs_sb, create_options),
268 .disposition = disposition,
273 rc = server->ops->open(xid, &oparms, oplock, buf);
277 /* TODO: Add support for calling posix query info but with passing in fid */
279 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
282 rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb,
286 server->ops->close(xid, tcon, fid);
295 cifs_has_mand_locks(struct cifsInodeInfo *cinode)
297 struct cifs_fid_locks *cur;
298 bool has_locks = false;
300 down_read(&cinode->lock_sem);
301 list_for_each_entry(cur, &cinode->llist, llist) {
302 if (!list_empty(&cur->locks)) {
307 up_read(&cinode->lock_sem);
312 cifs_down_write(struct rw_semaphore *sem)
314 while (!down_write_trylock(sem))
318 static void cifsFileInfo_put_work(struct work_struct *work);
320 struct cifsFileInfo *cifs_new_fileinfo(struct cifs_fid *fid, struct file *file,
321 struct tcon_link *tlink, __u32 oplock,
322 const char *symlink_target)
324 struct dentry *dentry = file_dentry(file);
325 struct inode *inode = d_inode(dentry);
326 struct cifsInodeInfo *cinode = CIFS_I(inode);
327 struct cifsFileInfo *cfile;
328 struct cifs_fid_locks *fdlocks;
329 struct cifs_tcon *tcon = tlink_tcon(tlink);
330 struct TCP_Server_Info *server = tcon->ses->server;
332 cfile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
336 fdlocks = kzalloc(sizeof(struct cifs_fid_locks), GFP_KERNEL);
342 if (symlink_target) {
343 cfile->symlink_target = kstrdup(symlink_target, GFP_KERNEL);
344 if (!cfile->symlink_target) {
351 INIT_LIST_HEAD(&fdlocks->locks);
352 fdlocks->cfile = cfile;
353 cfile->llist = fdlocks;
356 cfile->pid = current->tgid;
357 cfile->uid = current_fsuid();
358 cfile->dentry = dget(dentry);
359 cfile->f_flags = file->f_flags;
360 cfile->invalidHandle = false;
361 cfile->deferred_close_scheduled = false;
362 cfile->tlink = cifs_get_tlink(tlink);
363 INIT_WORK(&cfile->oplock_break, cifs_oplock_break);
364 INIT_WORK(&cfile->put, cifsFileInfo_put_work);
365 INIT_DELAYED_WORK(&cfile->deferred, smb2_deferred_work_close);
366 mutex_init(&cfile->fh_mutex);
367 spin_lock_init(&cfile->file_info_lock);
369 cifs_sb_active(inode->i_sb);
372 * If the server returned a read oplock and we have mandatory brlocks,
373 * set oplock level to None.
375 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
376 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
380 cifs_down_write(&cinode->lock_sem);
381 list_add(&fdlocks->llist, &cinode->llist);
382 up_write(&cinode->lock_sem);
384 spin_lock(&tcon->open_file_lock);
385 if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock)
386 oplock = fid->pending_open->oplock;
387 list_del(&fid->pending_open->olist);
389 fid->purge_cache = false;
390 server->ops->set_fid(cfile, fid, oplock);
392 list_add(&cfile->tlist, &tcon->openFileList);
393 atomic_inc(&tcon->num_local_opens);
395 /* if readable file instance put first in list*/
396 spin_lock(&cinode->open_file_lock);
397 if (file->f_mode & FMODE_READ)
398 list_add(&cfile->flist, &cinode->openFileList);
400 list_add_tail(&cfile->flist, &cinode->openFileList);
401 spin_unlock(&cinode->open_file_lock);
402 spin_unlock(&tcon->open_file_lock);
404 if (fid->purge_cache)
405 cifs_zap_mapping(inode);
407 file->private_data = cfile;
411 struct cifsFileInfo *
412 cifsFileInfo_get(struct cifsFileInfo *cifs_file)
414 spin_lock(&cifs_file->file_info_lock);
415 cifsFileInfo_get_locked(cifs_file);
416 spin_unlock(&cifs_file->file_info_lock);
420 static void cifsFileInfo_put_final(struct cifsFileInfo *cifs_file)
422 struct inode *inode = d_inode(cifs_file->dentry);
423 struct cifsInodeInfo *cifsi = CIFS_I(inode);
424 struct cifsLockInfo *li, *tmp;
425 struct super_block *sb = inode->i_sb;
428 * Delete any outstanding lock records. We'll lose them when the file
431 cifs_down_write(&cifsi->lock_sem);
432 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
433 list_del(&li->llist);
434 cifs_del_lock_waiters(li);
437 list_del(&cifs_file->llist->llist);
438 kfree(cifs_file->llist);
439 up_write(&cifsi->lock_sem);
441 cifs_put_tlink(cifs_file->tlink);
442 dput(cifs_file->dentry);
443 cifs_sb_deactive(sb);
444 kfree(cifs_file->symlink_target);
448 static void cifsFileInfo_put_work(struct work_struct *work)
450 struct cifsFileInfo *cifs_file = container_of(work,
451 struct cifsFileInfo, put);
453 cifsFileInfo_put_final(cifs_file);
457 * cifsFileInfo_put - release a reference of file priv data
459 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
461 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
463 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
465 _cifsFileInfo_put(cifs_file, true, true);
469 * _cifsFileInfo_put - release a reference of file priv data
471 * This may involve closing the filehandle @cifs_file out on the
472 * server. Must be called without holding tcon->open_file_lock,
473 * cinode->open_file_lock and cifs_file->file_info_lock.
475 * If @wait_for_oplock_handler is true and we are releasing the last
476 * reference, wait for any running oplock break handler of the file
477 * and cancel any pending one.
479 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
480 * @wait_oplock_handler: must be false if called from oplock_break_handler
481 * @offload: not offloaded on close and oplock breaks
484 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
485 bool wait_oplock_handler, bool offload)
487 struct inode *inode = d_inode(cifs_file->dentry);
488 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
489 struct TCP_Server_Info *server = tcon->ses->server;
490 struct cifsInodeInfo *cifsi = CIFS_I(inode);
491 struct super_block *sb = inode->i_sb;
492 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
493 struct cifs_fid fid = {};
494 struct cifs_pending_open open;
495 bool oplock_break_cancelled;
497 spin_lock(&tcon->open_file_lock);
498 spin_lock(&cifsi->open_file_lock);
499 spin_lock(&cifs_file->file_info_lock);
500 if (--cifs_file->count > 0) {
501 spin_unlock(&cifs_file->file_info_lock);
502 spin_unlock(&cifsi->open_file_lock);
503 spin_unlock(&tcon->open_file_lock);
506 spin_unlock(&cifs_file->file_info_lock);
508 if (server->ops->get_lease_key)
509 server->ops->get_lease_key(inode, &fid);
511 /* store open in pending opens to make sure we don't miss lease break */
512 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
514 /* remove it from the lists */
515 list_del(&cifs_file->flist);
516 list_del(&cifs_file->tlist);
517 atomic_dec(&tcon->num_local_opens);
519 if (list_empty(&cifsi->openFileList)) {
520 cifs_dbg(FYI, "closing last open instance for inode %p\n",
521 d_inode(cifs_file->dentry));
523 * In strict cache mode we need invalidate mapping on the last
524 * close because it may cause a error when we open this file
525 * again and get at least level II oplock.
527 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
528 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
529 cifs_set_oplock_level(cifsi, 0);
532 spin_unlock(&cifsi->open_file_lock);
533 spin_unlock(&tcon->open_file_lock);
535 oplock_break_cancelled = wait_oplock_handler ?
536 cancel_work_sync(&cifs_file->oplock_break) : false;
538 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
539 struct TCP_Server_Info *server = tcon->ses->server;
543 if (server->ops->close_getattr)
544 server->ops->close_getattr(xid, tcon, cifs_file);
545 else if (server->ops->close)
546 server->ops->close(xid, tcon, &cifs_file->fid);
550 if (oplock_break_cancelled)
551 cifs_done_oplock_break(cifsi);
553 cifs_del_pending_open(&open);
556 queue_work(fileinfo_put_wq, &cifs_file->put);
558 cifsFileInfo_put_final(cifs_file);
561 int cifs_open(struct inode *inode, struct file *file)
567 struct cifs_sb_info *cifs_sb;
568 struct TCP_Server_Info *server;
569 struct cifs_tcon *tcon;
570 struct tcon_link *tlink;
571 struct cifsFileInfo *cfile = NULL;
573 const char *full_path;
574 bool posix_open_ok = false;
575 struct cifs_fid fid = {};
576 struct cifs_pending_open open;
577 struct cifs_open_info_data data = {};
581 cifs_sb = CIFS_SB(inode->i_sb);
582 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
587 tlink = cifs_sb_tlink(cifs_sb);
590 return PTR_ERR(tlink);
592 tcon = tlink_tcon(tlink);
593 server = tcon->ses->server;
595 page = alloc_dentry_path();
596 full_path = build_path_from_dentry(file_dentry(file), page);
597 if (IS_ERR(full_path)) {
598 rc = PTR_ERR(full_path);
602 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
603 inode, file->f_flags, full_path);
605 if (file->f_flags & O_DIRECT &&
606 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
607 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
608 file->f_op = &cifs_file_direct_nobrl_ops;
610 file->f_op = &cifs_file_direct_ops;
613 /* Get the cached handle as SMB2 close is deferred */
614 rc = cifs_get_readable_path(tcon, full_path, &cfile);
616 if (file->f_flags == cfile->f_flags) {
617 file->private_data = cfile;
618 spin_lock(&CIFS_I(inode)->deferred_lock);
619 cifs_del_deferred_close(cfile);
620 spin_unlock(&CIFS_I(inode)->deferred_lock);
623 _cifsFileInfo_put(cfile, true, false);
632 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
633 if (!tcon->broken_posix_open && tcon->unix_ext &&
634 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
635 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
636 /* can not refresh inode info since size could be stale */
637 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
638 cifs_sb->ctx->file_mode /* ignored */,
639 file->f_flags, &oplock, &fid.netfid, xid);
641 cifs_dbg(FYI, "posix open succeeded\n");
642 posix_open_ok = true;
643 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
644 if (tcon->ses->serverNOS)
645 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",
647 tcon->ses->serverNOS);
648 tcon->broken_posix_open = true;
649 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
650 (rc != -EOPNOTSUPP)) /* path not found or net err */
653 * Else fallthrough to retry open the old way on network i/o
657 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
659 if (server->ops->get_lease_key)
660 server->ops->get_lease_key(inode, &fid);
662 cifs_add_pending_open(&fid, tlink, &open);
664 if (!posix_open_ok) {
665 if (server->ops->get_lease_key)
666 server->ops->get_lease_key(inode, &fid);
668 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
671 cifs_del_pending_open(&open);
676 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
678 if (server->ops->close)
679 server->ops->close(xid, tcon, &fid);
680 cifs_del_pending_open(&open);
685 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
686 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
688 * Time to set mode which we can not set earlier due to
689 * problems creating new read-only files.
691 struct cifs_unix_set_info_args args = {
692 .mode = inode->i_mode,
693 .uid = INVALID_UID, /* no change */
694 .gid = INVALID_GID, /* no change */
695 .ctime = NO_CHANGE_64,
696 .atime = NO_CHANGE_64,
697 .mtime = NO_CHANGE_64,
700 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
703 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
706 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
707 file->f_mode & FMODE_WRITE);
708 if (file->f_flags & O_DIRECT &&
709 (!((file->f_flags & O_ACCMODE) != O_RDONLY) ||
710 file->f_flags & O_APPEND))
711 cifs_invalidate_cache(file_inode(file),
712 FSCACHE_INVAL_DIO_WRITE);
715 free_dentry_path(page);
717 cifs_put_tlink(tlink);
718 cifs_free_open_info(&data);
722 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
723 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
724 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
727 * Try to reacquire byte range locks that were released when session
728 * to server was lost.
731 cifs_relock_file(struct cifsFileInfo *cfile)
733 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
734 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
736 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
737 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
738 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
740 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
741 if (cinode->can_cache_brlcks) {
742 /* can cache locks - no need to relock */
743 up_read(&cinode->lock_sem);
747 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
748 if (cap_unix(tcon->ses) &&
749 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
750 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
751 rc = cifs_push_posix_locks(cfile);
753 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
754 rc = tcon->ses->server->ops->push_mand_locks(cfile);
756 up_read(&cinode->lock_sem);
761 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
766 struct cifs_sb_info *cifs_sb;
767 struct cifs_tcon *tcon;
768 struct TCP_Server_Info *server;
769 struct cifsInodeInfo *cinode;
772 const char *full_path;
774 int disposition = FILE_OPEN;
775 int create_options = CREATE_NOT_DIR;
776 struct cifs_open_parms oparms;
779 mutex_lock(&cfile->fh_mutex);
780 if (!cfile->invalidHandle) {
781 mutex_unlock(&cfile->fh_mutex);
786 inode = d_inode(cfile->dentry);
787 cifs_sb = CIFS_SB(inode->i_sb);
788 tcon = tlink_tcon(cfile->tlink);
789 server = tcon->ses->server;
792 * Can not grab rename sem here because various ops, including those
793 * that already have the rename sem can end up causing writepage to get
794 * called and if the server was down that means we end up here, and we
795 * can never tell if the caller already has the rename_sem.
797 page = alloc_dentry_path();
798 full_path = build_path_from_dentry(cfile->dentry, page);
799 if (IS_ERR(full_path)) {
800 mutex_unlock(&cfile->fh_mutex);
801 free_dentry_path(page);
803 return PTR_ERR(full_path);
806 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
807 inode, cfile->f_flags, full_path);
809 if (tcon->ses->server->oplocks)
814 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
815 if (tcon->unix_ext && cap_unix(tcon->ses) &&
816 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
817 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
819 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
820 * original open. Must mask them off for a reopen.
822 unsigned int oflags = cfile->f_flags &
823 ~(O_CREAT | O_EXCL | O_TRUNC);
825 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
826 cifs_sb->ctx->file_mode /* ignored */,
827 oflags, &oplock, &cfile->fid.netfid, xid);
829 cifs_dbg(FYI, "posix reopen succeeded\n");
830 oparms.reconnect = true;
834 * fallthrough to retry open the old way on errors, especially
835 * in the reconnect path it is important to retry hard
838 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
840 desired_access = cifs_convert_flags(cfile->f_flags);
842 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
843 if (cfile->f_flags & O_SYNC)
844 create_options |= CREATE_WRITE_THROUGH;
846 if (cfile->f_flags & O_DIRECT)
847 create_options |= CREATE_NO_BUFFER;
849 if (server->ops->get_lease_key)
850 server->ops->get_lease_key(inode, &cfile->fid);
852 oparms = (struct cifs_open_parms) {
855 .desired_access = desired_access,
856 .create_options = cifs_create_options(cifs_sb, create_options),
857 .disposition = disposition,
864 * Can not refresh inode by passing in file_info buf to be returned by
865 * ops->open and then calling get_inode_info with returned buf since
866 * file might have write behind data that needs to be flushed and server
867 * version of file size can be stale. If we knew for sure that inode was
868 * not dirty locally we could do this.
870 rc = server->ops->open(xid, &oparms, &oplock, NULL);
871 if (rc == -ENOENT && oparms.reconnect == false) {
872 /* durable handle timeout is expired - open the file again */
873 rc = server->ops->open(xid, &oparms, &oplock, NULL);
874 /* indicate that we need to relock the file */
875 oparms.reconnect = true;
879 mutex_unlock(&cfile->fh_mutex);
880 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
881 cifs_dbg(FYI, "oplock: %d\n", oplock);
882 goto reopen_error_exit;
885 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
887 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
888 cfile->invalidHandle = false;
889 mutex_unlock(&cfile->fh_mutex);
890 cinode = CIFS_I(inode);
893 rc = filemap_write_and_wait(inode->i_mapping);
894 if (!is_interrupt_error(rc))
895 mapping_set_error(inode->i_mapping, rc);
897 if (tcon->posix_extensions)
898 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
899 else if (tcon->unix_ext)
900 rc = cifs_get_inode_info_unix(&inode, full_path,
903 rc = cifs_get_inode_info(&inode, full_path, NULL,
904 inode->i_sb, xid, NULL);
907 * Else we are writing out data to server already and could deadlock if
908 * we tried to flush data, and since we do not know if we have data that
909 * would invalidate the current end of file on the server we can not go
910 * to the server to get the new inode info.
914 * If the server returned a read oplock and we have mandatory brlocks,
915 * set oplock level to None.
917 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
918 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
922 server->ops->set_fid(cfile, &cfile->fid, oplock);
923 if (oparms.reconnect)
924 cifs_relock_file(cfile);
927 free_dentry_path(page);
932 void smb2_deferred_work_close(struct work_struct *work)
934 struct cifsFileInfo *cfile = container_of(work,
935 struct cifsFileInfo, deferred.work);
937 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
938 cifs_del_deferred_close(cfile);
939 cfile->deferred_close_scheduled = false;
940 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
941 _cifsFileInfo_put(cfile, true, false);
944 int cifs_close(struct inode *inode, struct file *file)
946 struct cifsFileInfo *cfile;
947 struct cifsInodeInfo *cinode = CIFS_I(inode);
948 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
949 struct cifs_deferred_close *dclose;
951 cifs_fscache_unuse_inode_cookie(inode, file->f_mode & FMODE_WRITE);
953 if (file->private_data != NULL) {
954 cfile = file->private_data;
955 file->private_data = NULL;
956 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
957 if ((cinode->oplock == CIFS_CACHE_RHW_FLG) &&
958 cinode->lease_granted &&
959 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
961 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
962 inode->i_ctime = inode->i_mtime = current_time(inode);
964 spin_lock(&cinode->deferred_lock);
965 cifs_add_deferred_close(cfile, dclose);
966 if (cfile->deferred_close_scheduled &&
967 delayed_work_pending(&cfile->deferred)) {
969 * If there is no pending work, mod_delayed_work queues new work.
970 * So, Increase the ref count to avoid use-after-free.
972 if (!mod_delayed_work(deferredclose_wq,
973 &cfile->deferred, cifs_sb->ctx->closetimeo))
974 cifsFileInfo_get(cfile);
976 /* Deferred close for files */
977 queue_delayed_work(deferredclose_wq,
978 &cfile->deferred, cifs_sb->ctx->closetimeo);
979 cfile->deferred_close_scheduled = true;
980 spin_unlock(&cinode->deferred_lock);
983 spin_unlock(&cinode->deferred_lock);
984 _cifsFileInfo_put(cfile, true, false);
986 _cifsFileInfo_put(cfile, true, false);
991 /* return code from the ->release op is always ignored */
996 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
998 struct cifsFileInfo *open_file, *tmp;
999 struct list_head tmp_list;
1001 if (!tcon->use_persistent || !tcon->need_reopen_files)
1004 tcon->need_reopen_files = false;
1006 cifs_dbg(FYI, "Reopen persistent handles\n");
1007 INIT_LIST_HEAD(&tmp_list);
1009 /* list all files open on tree connection, reopen resilient handles */
1010 spin_lock(&tcon->open_file_lock);
1011 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1012 if (!open_file->invalidHandle)
1014 cifsFileInfo_get(open_file);
1015 list_add_tail(&open_file->rlist, &tmp_list);
1017 spin_unlock(&tcon->open_file_lock);
1019 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1020 if (cifs_reopen_file(open_file, false /* do not flush */))
1021 tcon->need_reopen_files = true;
1022 list_del_init(&open_file->rlist);
1023 cifsFileInfo_put(open_file);
1027 int cifs_closedir(struct inode *inode, struct file *file)
1031 struct cifsFileInfo *cfile = file->private_data;
1032 struct cifs_tcon *tcon;
1033 struct TCP_Server_Info *server;
1036 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1042 tcon = tlink_tcon(cfile->tlink);
1043 server = tcon->ses->server;
1045 cifs_dbg(FYI, "Freeing private data in close dir\n");
1046 spin_lock(&cfile->file_info_lock);
1047 if (server->ops->dir_needs_close(cfile)) {
1048 cfile->invalidHandle = true;
1049 spin_unlock(&cfile->file_info_lock);
1050 if (server->ops->close_dir)
1051 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1054 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1055 /* not much we can do if it fails anyway, ignore rc */
1058 spin_unlock(&cfile->file_info_lock);
1060 buf = cfile->srch_inf.ntwrk_buf_start;
1062 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1063 cfile->srch_inf.ntwrk_buf_start = NULL;
1064 if (cfile->srch_inf.smallBuf)
1065 cifs_small_buf_release(buf);
1067 cifs_buf_release(buf);
1070 cifs_put_tlink(cfile->tlink);
1071 kfree(file->private_data);
1072 file->private_data = NULL;
1073 /* BB can we lock the filestruct while this is going on? */
1078 static struct cifsLockInfo *
1079 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1081 struct cifsLockInfo *lock =
1082 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1085 lock->offset = offset;
1086 lock->length = length;
1088 lock->pid = current->tgid;
1089 lock->flags = flags;
1090 INIT_LIST_HEAD(&lock->blist);
1091 init_waitqueue_head(&lock->block_q);
1096 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1098 struct cifsLockInfo *li, *tmp;
1099 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1100 list_del_init(&li->blist);
1101 wake_up(&li->block_q);
1105 #define CIFS_LOCK_OP 0
1106 #define CIFS_READ_OP 1
1107 #define CIFS_WRITE_OP 2
1109 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1111 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1112 __u64 length, __u8 type, __u16 flags,
1113 struct cifsFileInfo *cfile,
1114 struct cifsLockInfo **conf_lock, int rw_check)
1116 struct cifsLockInfo *li;
1117 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1118 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1120 list_for_each_entry(li, &fdlocks->locks, llist) {
1121 if (offset + length <= li->offset ||
1122 offset >= li->offset + li->length)
1124 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1125 server->ops->compare_fids(cfile, cur_cfile)) {
1126 /* shared lock prevents write op through the same fid */
1127 if (!(li->type & server->vals->shared_lock_type) ||
1128 rw_check != CIFS_WRITE_OP)
1131 if ((type & server->vals->shared_lock_type) &&
1132 ((server->ops->compare_fids(cfile, cur_cfile) &&
1133 current->tgid == li->pid) || type == li->type))
1135 if (rw_check == CIFS_LOCK_OP &&
1136 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1137 server->ops->compare_fids(cfile, cur_cfile))
1147 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1148 __u8 type, __u16 flags,
1149 struct cifsLockInfo **conf_lock, int rw_check)
1152 struct cifs_fid_locks *cur;
1153 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1155 list_for_each_entry(cur, &cinode->llist, llist) {
1156 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1157 flags, cfile, conf_lock,
1167 * Check if there is another lock that prevents us to set the lock (mandatory
1168 * style). If such a lock exists, update the flock structure with its
1169 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1170 * or leave it the same if we can't. Returns 0 if we don't need to request to
1171 * the server or 1 otherwise.
1174 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1175 __u8 type, struct file_lock *flock)
1178 struct cifsLockInfo *conf_lock;
1179 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1180 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1183 down_read(&cinode->lock_sem);
1185 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1186 flock->fl_flags, &conf_lock,
1189 flock->fl_start = conf_lock->offset;
1190 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1191 flock->fl_pid = conf_lock->pid;
1192 if (conf_lock->type & server->vals->shared_lock_type)
1193 flock->fl_type = F_RDLCK;
1195 flock->fl_type = F_WRLCK;
1196 } else if (!cinode->can_cache_brlcks)
1199 flock->fl_type = F_UNLCK;
1201 up_read(&cinode->lock_sem);
1206 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1208 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1209 cifs_down_write(&cinode->lock_sem);
1210 list_add_tail(&lock->llist, &cfile->llist->locks);
1211 up_write(&cinode->lock_sem);
1215 * Set the byte-range lock (mandatory style). Returns:
1216 * 1) 0, if we set the lock and don't need to request to the server;
1217 * 2) 1, if no locks prevent us but we need to request to the server;
1218 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1221 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1224 struct cifsLockInfo *conf_lock;
1225 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1231 cifs_down_write(&cinode->lock_sem);
1233 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1234 lock->type, lock->flags, &conf_lock,
1236 if (!exist && cinode->can_cache_brlcks) {
1237 list_add_tail(&lock->llist, &cfile->llist->locks);
1238 up_write(&cinode->lock_sem);
1247 list_add_tail(&lock->blist, &conf_lock->blist);
1248 up_write(&cinode->lock_sem);
1249 rc = wait_event_interruptible(lock->block_q,
1250 (lock->blist.prev == &lock->blist) &&
1251 (lock->blist.next == &lock->blist));
1254 cifs_down_write(&cinode->lock_sem);
1255 list_del_init(&lock->blist);
1258 up_write(&cinode->lock_sem);
1262 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1264 * Check if there is another lock that prevents us to set the lock (posix
1265 * style). If such a lock exists, update the flock structure with its
1266 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1267 * or leave it the same if we can't. Returns 0 if we don't need to request to
1268 * the server or 1 otherwise.
1271 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1274 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1275 unsigned char saved_type = flock->fl_type;
1277 if ((flock->fl_flags & FL_POSIX) == 0)
1280 down_read(&cinode->lock_sem);
1281 posix_test_lock(file, flock);
1283 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1284 flock->fl_type = saved_type;
1288 up_read(&cinode->lock_sem);
1293 * Set the byte-range lock (posix style). Returns:
1294 * 1) <0, if the error occurs while setting the lock;
1295 * 2) 0, if we set the lock and don't need to request to the server;
1296 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1297 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1300 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1302 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1303 int rc = FILE_LOCK_DEFERRED + 1;
1305 if ((flock->fl_flags & FL_POSIX) == 0)
1308 cifs_down_write(&cinode->lock_sem);
1309 if (!cinode->can_cache_brlcks) {
1310 up_write(&cinode->lock_sem);
1314 rc = posix_lock_file(file, flock, NULL);
1315 up_write(&cinode->lock_sem);
1320 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1323 int rc = 0, stored_rc;
1324 struct cifsLockInfo *li, *tmp;
1325 struct cifs_tcon *tcon;
1326 unsigned int num, max_num, max_buf;
1327 LOCKING_ANDX_RANGE *buf, *cur;
1328 static const int types[] = {
1329 LOCKING_ANDX_LARGE_FILES,
1330 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1335 tcon = tlink_tcon(cfile->tlink);
1338 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1339 * and check it before using.
1341 max_buf = tcon->ses->server->maxBuf;
1342 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1347 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1349 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1351 max_num = (max_buf - sizeof(struct smb_hdr)) /
1352 sizeof(LOCKING_ANDX_RANGE);
1353 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1359 for (i = 0; i < 2; i++) {
1362 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1363 if (li->type != types[i])
1365 cur->Pid = cpu_to_le16(li->pid);
1366 cur->LengthLow = cpu_to_le32((u32)li->length);
1367 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1368 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1369 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1370 if (++num == max_num) {
1371 stored_rc = cifs_lockv(xid, tcon,
1373 (__u8)li->type, 0, num,
1384 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1385 (__u8)types[i], 0, num, buf);
1397 hash_lockowner(fl_owner_t owner)
1399 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1401 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1403 struct lock_to_push {
1404 struct list_head llist;
1412 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1414 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1416 struct inode *inode = d_inode(cfile->dentry);
1417 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1418 struct file_lock *flock;
1419 struct file_lock_context *flctx = inode->i_flctx;
1420 unsigned int count = 0, i;
1421 int rc = 0, xid, type;
1422 struct list_head locks_to_send, *el;
1423 struct lock_to_push *lck, *tmp;
1431 spin_lock(&flctx->flc_lock);
1432 list_for_each(el, &flctx->flc_posix) {
1435 spin_unlock(&flctx->flc_lock);
1437 INIT_LIST_HEAD(&locks_to_send);
1440 * Allocating count locks is enough because no FL_POSIX locks can be
1441 * added to the list while we are holding cinode->lock_sem that
1442 * protects locking operations of this inode.
1444 for (i = 0; i < count; i++) {
1445 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1450 list_add_tail(&lck->llist, &locks_to_send);
1453 el = locks_to_send.next;
1454 spin_lock(&flctx->flc_lock);
1455 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1456 if (el == &locks_to_send) {
1458 * The list ended. We don't have enough allocated
1459 * structures - something is really wrong.
1461 cifs_dbg(VFS, "Can't push all brlocks!\n");
1464 length = cifs_flock_len(flock);
1465 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1469 lck = list_entry(el, struct lock_to_push, llist);
1470 lck->pid = hash_lockowner(flock->fl_owner);
1471 lck->netfid = cfile->fid.netfid;
1472 lck->length = length;
1474 lck->offset = flock->fl_start;
1476 spin_unlock(&flctx->flc_lock);
1478 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1481 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1482 lck->offset, lck->length, NULL,
1486 list_del(&lck->llist);
1494 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1495 list_del(&lck->llist);
1500 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1503 cifs_push_locks(struct cifsFileInfo *cfile)
1505 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1506 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1508 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1509 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1510 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1512 /* we are going to update can_cache_brlcks here - need a write access */
1513 cifs_down_write(&cinode->lock_sem);
1514 if (!cinode->can_cache_brlcks) {
1515 up_write(&cinode->lock_sem);
1519 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1520 if (cap_unix(tcon->ses) &&
1521 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1522 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1523 rc = cifs_push_posix_locks(cfile);
1525 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1526 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1528 cinode->can_cache_brlcks = false;
1529 up_write(&cinode->lock_sem);
1534 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1535 bool *wait_flag, struct TCP_Server_Info *server)
1537 if (flock->fl_flags & FL_POSIX)
1538 cifs_dbg(FYI, "Posix\n");
1539 if (flock->fl_flags & FL_FLOCK)
1540 cifs_dbg(FYI, "Flock\n");
1541 if (flock->fl_flags & FL_SLEEP) {
1542 cifs_dbg(FYI, "Blocking lock\n");
1545 if (flock->fl_flags & FL_ACCESS)
1546 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1547 if (flock->fl_flags & FL_LEASE)
1548 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1549 if (flock->fl_flags &
1550 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1551 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1552 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1554 *type = server->vals->large_lock_type;
1555 if (flock->fl_type == F_WRLCK) {
1556 cifs_dbg(FYI, "F_WRLCK\n");
1557 *type |= server->vals->exclusive_lock_type;
1559 } else if (flock->fl_type == F_UNLCK) {
1560 cifs_dbg(FYI, "F_UNLCK\n");
1561 *type |= server->vals->unlock_lock_type;
1563 /* Check if unlock includes more than one lock range */
1564 } else if (flock->fl_type == F_RDLCK) {
1565 cifs_dbg(FYI, "F_RDLCK\n");
1566 *type |= server->vals->shared_lock_type;
1568 } else if (flock->fl_type == F_EXLCK) {
1569 cifs_dbg(FYI, "F_EXLCK\n");
1570 *type |= server->vals->exclusive_lock_type;
1572 } else if (flock->fl_type == F_SHLCK) {
1573 cifs_dbg(FYI, "F_SHLCK\n");
1574 *type |= server->vals->shared_lock_type;
1577 cifs_dbg(FYI, "Unknown type of lock\n");
1581 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1582 bool wait_flag, bool posix_lck, unsigned int xid)
1585 __u64 length = cifs_flock_len(flock);
1586 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1587 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1588 struct TCP_Server_Info *server = tcon->ses->server;
1589 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1590 __u16 netfid = cfile->fid.netfid;
1593 int posix_lock_type;
1595 rc = cifs_posix_lock_test(file, flock);
1599 if (type & server->vals->shared_lock_type)
1600 posix_lock_type = CIFS_RDLCK;
1602 posix_lock_type = CIFS_WRLCK;
1603 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1604 hash_lockowner(flock->fl_owner),
1605 flock->fl_start, length, flock,
1606 posix_lock_type, wait_flag);
1609 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1611 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1615 /* BB we could chain these into one lock request BB */
1616 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1619 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1621 flock->fl_type = F_UNLCK;
1623 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1628 if (type & server->vals->shared_lock_type) {
1629 flock->fl_type = F_WRLCK;
1633 type &= ~server->vals->exclusive_lock_type;
1635 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1636 type | server->vals->shared_lock_type,
1639 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1640 type | server->vals->shared_lock_type, 0, 1, false);
1641 flock->fl_type = F_RDLCK;
1643 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1646 flock->fl_type = F_WRLCK;
1652 cifs_move_llist(struct list_head *source, struct list_head *dest)
1654 struct list_head *li, *tmp;
1655 list_for_each_safe(li, tmp, source)
1656 list_move(li, dest);
1660 cifs_free_llist(struct list_head *llist)
1662 struct cifsLockInfo *li, *tmp;
1663 list_for_each_entry_safe(li, tmp, llist, llist) {
1664 cifs_del_lock_waiters(li);
1665 list_del(&li->llist);
1670 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1672 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1675 int rc = 0, stored_rc;
1676 static const int types[] = {
1677 LOCKING_ANDX_LARGE_FILES,
1678 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1681 unsigned int max_num, num, max_buf;
1682 LOCKING_ANDX_RANGE *buf, *cur;
1683 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1684 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1685 struct cifsLockInfo *li, *tmp;
1686 __u64 length = cifs_flock_len(flock);
1687 struct list_head tmp_llist;
1689 INIT_LIST_HEAD(&tmp_llist);
1692 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1693 * and check it before using.
1695 max_buf = tcon->ses->server->maxBuf;
1696 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1699 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1701 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1703 max_num = (max_buf - sizeof(struct smb_hdr)) /
1704 sizeof(LOCKING_ANDX_RANGE);
1705 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1709 cifs_down_write(&cinode->lock_sem);
1710 for (i = 0; i < 2; i++) {
1713 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1714 if (flock->fl_start > li->offset ||
1715 (flock->fl_start + length) <
1716 (li->offset + li->length))
1718 if (current->tgid != li->pid)
1720 if (types[i] != li->type)
1722 if (cinode->can_cache_brlcks) {
1724 * We can cache brlock requests - simply remove
1725 * a lock from the file's list.
1727 list_del(&li->llist);
1728 cifs_del_lock_waiters(li);
1732 cur->Pid = cpu_to_le16(li->pid);
1733 cur->LengthLow = cpu_to_le32((u32)li->length);
1734 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1735 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1736 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1738 * We need to save a lock here to let us add it again to
1739 * the file's list if the unlock range request fails on
1742 list_move(&li->llist, &tmp_llist);
1743 if (++num == max_num) {
1744 stored_rc = cifs_lockv(xid, tcon,
1746 li->type, num, 0, buf);
1749 * We failed on the unlock range
1750 * request - add all locks from the tmp
1751 * list to the head of the file's list.
1753 cifs_move_llist(&tmp_llist,
1754 &cfile->llist->locks);
1758 * The unlock range request succeed -
1759 * free the tmp list.
1761 cifs_free_llist(&tmp_llist);
1768 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1769 types[i], num, 0, buf);
1771 cifs_move_llist(&tmp_llist,
1772 &cfile->llist->locks);
1775 cifs_free_llist(&tmp_llist);
1779 up_write(&cinode->lock_sem);
1783 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1786 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1787 bool wait_flag, bool posix_lck, int lock, int unlock,
1791 __u64 length = cifs_flock_len(flock);
1792 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1793 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1794 struct TCP_Server_Info *server = tcon->ses->server;
1795 struct inode *inode = d_inode(cfile->dentry);
1797 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1799 int posix_lock_type;
1801 rc = cifs_posix_lock_set(file, flock);
1802 if (rc <= FILE_LOCK_DEFERRED)
1805 if (type & server->vals->shared_lock_type)
1806 posix_lock_type = CIFS_RDLCK;
1808 posix_lock_type = CIFS_WRLCK;
1811 posix_lock_type = CIFS_UNLCK;
1813 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1814 hash_lockowner(flock->fl_owner),
1815 flock->fl_start, length,
1816 NULL, posix_lock_type, wait_flag);
1819 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1821 struct cifsLockInfo *lock;
1823 lock = cifs_lock_init(flock->fl_start, length, type,
1828 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1837 * Windows 7 server can delay breaking lease from read to None
1838 * if we set a byte-range lock on a file - break it explicitly
1839 * before sending the lock to the server to be sure the next
1840 * read won't conflict with non-overlapted locks due to
1843 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1844 CIFS_CACHE_READ(CIFS_I(inode))) {
1845 cifs_zap_mapping(inode);
1846 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1848 CIFS_I(inode)->oplock = 0;
1851 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1852 type, 1, 0, wait_flag);
1858 cifs_lock_add(cfile, lock);
1860 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1863 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1865 * If this is a request to remove all locks because we
1866 * are closing the file, it doesn't matter if the
1867 * unlocking failed as both cifs.ko and the SMB server
1868 * remove the lock on file close
1871 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1872 if (!(flock->fl_flags & FL_CLOSE))
1875 rc = locks_lock_file_wait(file, flock);
1880 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1883 int lock = 0, unlock = 0;
1884 bool wait_flag = false;
1885 bool posix_lck = false;
1886 struct cifs_sb_info *cifs_sb;
1887 struct cifs_tcon *tcon;
1888 struct cifsFileInfo *cfile;
1893 if (!(fl->fl_flags & FL_FLOCK)) {
1899 cfile = (struct cifsFileInfo *)file->private_data;
1900 tcon = tlink_tcon(cfile->tlink);
1902 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1904 cifs_sb = CIFS_FILE_SB(file);
1906 if (cap_unix(tcon->ses) &&
1907 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1908 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1911 if (!lock && !unlock) {
1913 * if no lock or unlock then nothing to do since we do not
1921 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1929 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1932 int lock = 0, unlock = 0;
1933 bool wait_flag = false;
1934 bool posix_lck = false;
1935 struct cifs_sb_info *cifs_sb;
1936 struct cifs_tcon *tcon;
1937 struct cifsFileInfo *cfile;
1943 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
1944 flock->fl_flags, flock->fl_type, (long long)flock->fl_start,
1945 (long long)flock->fl_end);
1947 cfile = (struct cifsFileInfo *)file->private_data;
1948 tcon = tlink_tcon(cfile->tlink);
1950 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1952 cifs_sb = CIFS_FILE_SB(file);
1953 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
1955 if (cap_unix(tcon->ses) &&
1956 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1957 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1960 * BB add code here to normalize offset and length to account for
1961 * negative length which we can not accept over the wire.
1963 if (IS_GETLK(cmd)) {
1964 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1969 if (!lock && !unlock) {
1971 * if no lock or unlock then nothing to do since we do not
1978 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1985 * update the file size (if needed) after a write. Should be called with
1986 * the inode->i_lock held
1989 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1990 unsigned int bytes_written)
1992 loff_t end_of_write = offset + bytes_written;
1994 if (end_of_write > cifsi->server_eof)
1995 cifsi->server_eof = end_of_write;
1999 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
2000 size_t write_size, loff_t *offset)
2003 unsigned int bytes_written = 0;
2004 unsigned int total_written;
2005 struct cifs_tcon *tcon;
2006 struct TCP_Server_Info *server;
2008 struct dentry *dentry = open_file->dentry;
2009 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2010 struct cifs_io_parms io_parms = {0};
2012 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2013 write_size, *offset, dentry);
2015 tcon = tlink_tcon(open_file->tlink);
2016 server = tcon->ses->server;
2018 if (!server->ops->sync_write)
2023 for (total_written = 0; write_size > total_written;
2024 total_written += bytes_written) {
2026 while (rc == -EAGAIN) {
2030 if (open_file->invalidHandle) {
2031 /* we could deadlock if we called
2032 filemap_fdatawait from here so tell
2033 reopen_file not to flush data to
2035 rc = cifs_reopen_file(open_file, false);
2040 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2041 (unsigned int)write_size - total_written);
2042 /* iov[0] is reserved for smb header */
2043 iov[1].iov_base = (char *)write_data + total_written;
2044 iov[1].iov_len = len;
2046 io_parms.tcon = tcon;
2047 io_parms.offset = *offset;
2048 io_parms.length = len;
2049 rc = server->ops->sync_write(xid, &open_file->fid,
2050 &io_parms, &bytes_written, iov, 1);
2052 if (rc || (bytes_written == 0)) {
2060 spin_lock(&d_inode(dentry)->i_lock);
2061 cifs_update_eof(cifsi, *offset, bytes_written);
2062 spin_unlock(&d_inode(dentry)->i_lock);
2063 *offset += bytes_written;
2067 cifs_stats_bytes_written(tcon, total_written);
2069 if (total_written > 0) {
2070 spin_lock(&d_inode(dentry)->i_lock);
2071 if (*offset > d_inode(dentry)->i_size) {
2072 i_size_write(d_inode(dentry), *offset);
2073 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2075 spin_unlock(&d_inode(dentry)->i_lock);
2077 mark_inode_dirty_sync(d_inode(dentry));
2079 return total_written;
2082 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2085 struct cifsFileInfo *open_file = NULL;
2086 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2088 /* only filter by fsuid on multiuser mounts */
2089 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2092 spin_lock(&cifs_inode->open_file_lock);
2093 /* we could simply get the first_list_entry since write-only entries
2094 are always at the end of the list but since the first entry might
2095 have a close pending, we go through the whole list */
2096 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2097 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2099 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2100 if ((!open_file->invalidHandle)) {
2101 /* found a good file */
2102 /* lock it so it will not be closed on us */
2103 cifsFileInfo_get(open_file);
2104 spin_unlock(&cifs_inode->open_file_lock);
2106 } /* else might as well continue, and look for
2107 another, or simply have the caller reopen it
2108 again rather than trying to fix this handle */
2109 } else /* write only file */
2110 break; /* write only files are last so must be done */
2112 spin_unlock(&cifs_inode->open_file_lock);
2116 /* Return -EBADF if no handle is found and general rc otherwise */
2118 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2119 struct cifsFileInfo **ret_file)
2121 struct cifsFileInfo *open_file, *inv_file = NULL;
2122 struct cifs_sb_info *cifs_sb;
2123 bool any_available = false;
2125 unsigned int refind = 0;
2126 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2127 bool with_delete = flags & FIND_WR_WITH_DELETE;
2131 * Having a null inode here (because mapping->host was set to zero by
2132 * the VFS or MM) should not happen but we had reports of on oops (due
2133 * to it being zero) during stress testcases so we need to check for it
2136 if (cifs_inode == NULL) {
2137 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2142 cifs_sb = CIFS_SB(cifs_inode->netfs.inode.i_sb);
2144 /* only filter by fsuid on multiuser mounts */
2145 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2148 spin_lock(&cifs_inode->open_file_lock);
2150 if (refind > MAX_REOPEN_ATT) {
2151 spin_unlock(&cifs_inode->open_file_lock);
2154 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2155 if (!any_available && open_file->pid != current->tgid)
2157 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2159 if (with_delete && !(open_file->fid.access & DELETE))
2161 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2162 if (!open_file->invalidHandle) {
2163 /* found a good writable file */
2164 cifsFileInfo_get(open_file);
2165 spin_unlock(&cifs_inode->open_file_lock);
2166 *ret_file = open_file;
2170 inv_file = open_file;
2174 /* couldn't find useable FH with same pid, try any available */
2175 if (!any_available) {
2176 any_available = true;
2177 goto refind_writable;
2181 any_available = false;
2182 cifsFileInfo_get(inv_file);
2185 spin_unlock(&cifs_inode->open_file_lock);
2188 rc = cifs_reopen_file(inv_file, false);
2190 *ret_file = inv_file;
2194 spin_lock(&cifs_inode->open_file_lock);
2195 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2196 spin_unlock(&cifs_inode->open_file_lock);
2197 cifsFileInfo_put(inv_file);
2200 spin_lock(&cifs_inode->open_file_lock);
2201 goto refind_writable;
2207 struct cifsFileInfo *
2208 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2210 struct cifsFileInfo *cfile;
2213 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2215 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2221 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2223 struct cifsFileInfo **ret_file)
2225 struct cifsFileInfo *cfile;
2226 void *page = alloc_dentry_path();
2230 spin_lock(&tcon->open_file_lock);
2231 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2232 struct cifsInodeInfo *cinode;
2233 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2234 if (IS_ERR(full_path)) {
2235 spin_unlock(&tcon->open_file_lock);
2236 free_dentry_path(page);
2237 return PTR_ERR(full_path);
2239 if (strcmp(full_path, name))
2242 cinode = CIFS_I(d_inode(cfile->dentry));
2243 spin_unlock(&tcon->open_file_lock);
2244 free_dentry_path(page);
2245 return cifs_get_writable_file(cinode, flags, ret_file);
2248 spin_unlock(&tcon->open_file_lock);
2249 free_dentry_path(page);
2254 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2255 struct cifsFileInfo **ret_file)
2257 struct cifsFileInfo *cfile;
2258 void *page = alloc_dentry_path();
2262 spin_lock(&tcon->open_file_lock);
2263 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2264 struct cifsInodeInfo *cinode;
2265 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2266 if (IS_ERR(full_path)) {
2267 spin_unlock(&tcon->open_file_lock);
2268 free_dentry_path(page);
2269 return PTR_ERR(full_path);
2271 if (strcmp(full_path, name))
2274 cinode = CIFS_I(d_inode(cfile->dentry));
2275 spin_unlock(&tcon->open_file_lock);
2276 free_dentry_path(page);
2277 *ret_file = find_readable_file(cinode, 0);
2278 return *ret_file ? 0 : -ENOENT;
2281 spin_unlock(&tcon->open_file_lock);
2282 free_dentry_path(page);
2287 cifs_writedata_release(struct kref *refcount)
2289 struct cifs_writedata *wdata = container_of(refcount,
2290 struct cifs_writedata, refcount);
2291 #ifdef CONFIG_CIFS_SMB_DIRECT
2293 smbd_deregister_mr(wdata->mr);
2299 cifsFileInfo_put(wdata->cfile);
2301 kvfree(wdata->pages);
2306 * Write failed with a retryable error. Resend the write request. It's also
2307 * possible that the page was redirtied so re-clean the page.
2310 cifs_writev_requeue(struct cifs_writedata *wdata)
2313 struct inode *inode = d_inode(wdata->cfile->dentry);
2314 struct TCP_Server_Info *server;
2315 unsigned int rest_len;
2317 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2319 rest_len = wdata->bytes;
2321 struct cifs_writedata *wdata2;
2322 unsigned int j, nr_pages, wsize, tailsz, cur_len;
2324 wsize = server->ops->wp_retry_size(inode);
2325 if (wsize < rest_len) {
2326 nr_pages = wsize / PAGE_SIZE;
2331 cur_len = nr_pages * PAGE_SIZE;
2334 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
2336 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
2339 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2345 for (j = 0; j < nr_pages; j++) {
2346 wdata2->pages[j] = wdata->pages[i + j];
2347 lock_page(wdata2->pages[j]);
2348 clear_page_dirty_for_io(wdata2->pages[j]);
2351 wdata2->sync_mode = wdata->sync_mode;
2352 wdata2->nr_pages = nr_pages;
2353 wdata2->offset = page_offset(wdata2->pages[0]);
2354 wdata2->pagesz = PAGE_SIZE;
2355 wdata2->tailsz = tailsz;
2356 wdata2->bytes = cur_len;
2358 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2360 if (!wdata2->cfile) {
2361 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2363 if (!is_retryable_error(rc))
2366 wdata2->pid = wdata2->cfile->pid;
2367 rc = server->ops->async_writev(wdata2,
2368 cifs_writedata_release);
2371 for (j = 0; j < nr_pages; j++) {
2372 unlock_page(wdata2->pages[j]);
2373 if (rc != 0 && !is_retryable_error(rc)) {
2374 SetPageError(wdata2->pages[j]);
2375 end_page_writeback(wdata2->pages[j]);
2376 put_page(wdata2->pages[j]);
2380 kref_put(&wdata2->refcount, cifs_writedata_release);
2382 if (is_retryable_error(rc))
2388 rest_len -= cur_len;
2390 } while (i < wdata->nr_pages);
2392 /* cleanup remaining pages from the original wdata */
2393 for (; i < wdata->nr_pages; i++) {
2394 SetPageError(wdata->pages[i]);
2395 end_page_writeback(wdata->pages[i]);
2396 put_page(wdata->pages[i]);
2399 if (rc != 0 && !is_retryable_error(rc))
2400 mapping_set_error(inode->i_mapping, rc);
2401 kref_put(&wdata->refcount, cifs_writedata_release);
2405 cifs_writev_complete(struct work_struct *work)
2407 struct cifs_writedata *wdata = container_of(work,
2408 struct cifs_writedata, work);
2409 struct inode *inode = d_inode(wdata->cfile->dentry);
2412 if (wdata->result == 0) {
2413 spin_lock(&inode->i_lock);
2414 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2415 spin_unlock(&inode->i_lock);
2416 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2418 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2419 return cifs_writev_requeue(wdata);
2421 for (i = 0; i < wdata->nr_pages; i++) {
2422 struct page *page = wdata->pages[i];
2424 if (wdata->result == -EAGAIN)
2425 __set_page_dirty_nobuffers(page);
2426 else if (wdata->result < 0)
2428 end_page_writeback(page);
2429 cifs_readpage_to_fscache(inode, page);
2432 if (wdata->result != -EAGAIN)
2433 mapping_set_error(inode->i_mapping, wdata->result);
2434 kref_put(&wdata->refcount, cifs_writedata_release);
2437 struct cifs_writedata *
2438 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
2440 struct cifs_writedata *writedata = NULL;
2441 struct page **pages =
2442 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
2444 writedata = cifs_writedata_direct_alloc(pages, complete);
2452 struct cifs_writedata *
2453 cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2455 struct cifs_writedata *wdata;
2457 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2458 if (wdata != NULL) {
2459 wdata->pages = pages;
2460 kref_init(&wdata->refcount);
2461 INIT_LIST_HEAD(&wdata->list);
2462 init_completion(&wdata->done);
2463 INIT_WORK(&wdata->work, complete);
2469 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2471 struct address_space *mapping = page->mapping;
2472 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2475 int bytes_written = 0;
2476 struct inode *inode;
2477 struct cifsFileInfo *open_file;
2479 if (!mapping || !mapping->host)
2482 inode = page->mapping->host;
2484 offset += (loff_t)from;
2485 write_data = kmap(page);
2488 if ((to > PAGE_SIZE) || (from > to)) {
2493 /* racing with truncate? */
2494 if (offset > mapping->host->i_size) {
2496 return 0; /* don't care */
2499 /* check to make sure that we are not extending the file */
2500 if (mapping->host->i_size - offset < (loff_t)to)
2501 to = (unsigned)(mapping->host->i_size - offset);
2503 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2506 bytes_written = cifs_write(open_file, open_file->pid,
2507 write_data, to - from, &offset);
2508 cifsFileInfo_put(open_file);
2509 /* Does mm or vfs already set times? */
2510 inode->i_atime = inode->i_mtime = current_time(inode);
2511 if ((bytes_written > 0) && (offset))
2513 else if (bytes_written < 0)
2518 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2519 if (!is_retryable_error(rc))
2527 static struct cifs_writedata *
2528 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2529 pgoff_t end, pgoff_t *index,
2530 unsigned int *found_pages)
2532 struct cifs_writedata *wdata;
2534 wdata = cifs_writedata_alloc((unsigned int)tofind,
2535 cifs_writev_complete);
2539 *found_pages = find_get_pages_range_tag(mapping, index, end,
2540 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2545 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2546 struct address_space *mapping,
2547 struct writeback_control *wbc,
2548 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2550 unsigned int nr_pages = 0, i;
2553 for (i = 0; i < found_pages; i++) {
2554 page = wdata->pages[i];
2556 * At this point we hold neither the i_pages lock nor the
2557 * page lock: the page may be truncated or invalidated
2558 * (changing page->mapping to NULL), or even swizzled
2559 * back from swapper_space to tmpfs file mapping
2564 else if (!trylock_page(page))
2567 if (unlikely(page->mapping != mapping)) {
2572 if (!wbc->range_cyclic && page->index > end) {
2578 if (*next && (page->index != *next)) {
2579 /* Not next consecutive page */
2584 if (wbc->sync_mode != WB_SYNC_NONE)
2585 wait_on_page_writeback(page);
2587 if (PageWriteback(page) ||
2588 !clear_page_dirty_for_io(page)) {
2594 * This actually clears the dirty bit in the radix tree.
2595 * See cifs_writepage() for more commentary.
2597 set_page_writeback(page);
2598 if (page_offset(page) >= i_size_read(mapping->host)) {
2601 end_page_writeback(page);
2605 wdata->pages[i] = page;
2606 *next = page->index + 1;
2610 /* reset index to refind any pages skipped */
2612 *index = wdata->pages[0]->index + 1;
2614 /* put any pages we aren't going to use */
2615 for (i = nr_pages; i < found_pages; i++) {
2616 put_page(wdata->pages[i]);
2617 wdata->pages[i] = NULL;
2624 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2625 struct address_space *mapping, struct writeback_control *wbc)
2629 wdata->sync_mode = wbc->sync_mode;
2630 wdata->nr_pages = nr_pages;
2631 wdata->offset = page_offset(wdata->pages[0]);
2632 wdata->pagesz = PAGE_SIZE;
2633 wdata->tailsz = min(i_size_read(mapping->host) -
2634 page_offset(wdata->pages[nr_pages - 1]),
2636 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2637 wdata->pid = wdata->cfile->pid;
2639 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2643 if (wdata->cfile->invalidHandle)
2646 rc = wdata->server->ops->async_writev(wdata,
2647 cifs_writedata_release);
2652 static int cifs_writepages(struct address_space *mapping,
2653 struct writeback_control *wbc)
2655 struct inode *inode = mapping->host;
2656 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2657 struct TCP_Server_Info *server;
2658 bool done = false, scanned = false, range_whole = false;
2660 struct cifs_writedata *wdata;
2661 struct cifsFileInfo *cfile = NULL;
2667 * If wsize is smaller than the page cache size, default to writing
2668 * one page at a time via cifs_writepage
2670 if (cifs_sb->ctx->wsize < PAGE_SIZE)
2671 return generic_writepages(mapping, wbc);
2674 if (wbc->range_cyclic) {
2675 index = mapping->writeback_index; /* Start from prev offset */
2678 index = wbc->range_start >> PAGE_SHIFT;
2679 end = wbc->range_end >> PAGE_SHIFT;
2680 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2684 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2687 while (!done && index <= end) {
2688 unsigned int i, nr_pages, found_pages, wsize;
2689 pgoff_t next = 0, tofind, saved_index = index;
2690 struct cifs_credits credits_on_stack;
2691 struct cifs_credits *credits = &credits_on_stack;
2692 int get_file_rc = 0;
2695 cifsFileInfo_put(cfile);
2697 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2699 /* in case of an error store it to return later */
2703 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2710 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2712 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2717 add_credits_and_wake_if(server, credits, 0);
2721 if (found_pages == 0) {
2722 kref_put(&wdata->refcount, cifs_writedata_release);
2723 add_credits_and_wake_if(server, credits, 0);
2727 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2728 end, &index, &next, &done);
2730 /* nothing to write? */
2731 if (nr_pages == 0) {
2732 kref_put(&wdata->refcount, cifs_writedata_release);
2733 add_credits_and_wake_if(server, credits, 0);
2737 wdata->credits = credits_on_stack;
2738 wdata->cfile = cfile;
2739 wdata->server = server;
2742 if (!wdata->cfile) {
2743 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2745 if (is_retryable_error(get_file_rc))
2750 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2752 for (i = 0; i < nr_pages; ++i)
2753 unlock_page(wdata->pages[i]);
2755 /* send failure -- clean up the mess */
2757 add_credits_and_wake_if(server, &wdata->credits, 0);
2758 for (i = 0; i < nr_pages; ++i) {
2759 if (is_retryable_error(rc))
2760 redirty_page_for_writepage(wbc,
2763 SetPageError(wdata->pages[i]);
2764 end_page_writeback(wdata->pages[i]);
2765 put_page(wdata->pages[i]);
2767 if (!is_retryable_error(rc))
2768 mapping_set_error(mapping, rc);
2770 kref_put(&wdata->refcount, cifs_writedata_release);
2772 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2773 index = saved_index;
2777 /* Return immediately if we received a signal during writing */
2778 if (is_interrupt_error(rc)) {
2783 if (rc != 0 && saved_rc == 0)
2786 wbc->nr_to_write -= nr_pages;
2787 if (wbc->nr_to_write <= 0)
2793 if (!scanned && !done) {
2795 * We hit the last page and there is more work to be done: wrap
2796 * back to the start of the file
2806 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2807 mapping->writeback_index = index;
2810 cifsFileInfo_put(cfile);
2812 /* Indication to update ctime and mtime as close is deferred */
2813 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2818 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2824 /* BB add check for wbc flags */
2826 if (!PageUptodate(page))
2827 cifs_dbg(FYI, "ppw - page not up to date\n");
2830 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2832 * A writepage() implementation always needs to do either this,
2833 * or re-dirty the page with "redirty_page_for_writepage()" in
2834 * the case of a failure.
2836 * Just unlocking the page will cause the radix tree tag-bits
2837 * to fail to update with the state of the page correctly.
2839 set_page_writeback(page);
2841 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2842 if (is_retryable_error(rc)) {
2843 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2845 redirty_page_for_writepage(wbc, page);
2846 } else if (rc != 0) {
2848 mapping_set_error(page->mapping, rc);
2850 SetPageUptodate(page);
2852 end_page_writeback(page);
2858 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2860 int rc = cifs_writepage_locked(page, wbc);
2865 static int cifs_write_end(struct file *file, struct address_space *mapping,
2866 loff_t pos, unsigned len, unsigned copied,
2867 struct page *page, void *fsdata)
2870 struct inode *inode = mapping->host;
2871 struct cifsFileInfo *cfile = file->private_data;
2872 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2875 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2878 pid = current->tgid;
2880 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2883 if (PageChecked(page)) {
2885 SetPageUptodate(page);
2886 ClearPageChecked(page);
2887 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2888 SetPageUptodate(page);
2890 if (!PageUptodate(page)) {
2892 unsigned offset = pos & (PAGE_SIZE - 1);
2896 /* this is probably better than directly calling
2897 partialpage_write since in this function the file handle is
2898 known which we might as well leverage */
2899 /* BB check if anything else missing out of ppw
2900 such as updating last write time */
2901 page_data = kmap(page);
2902 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2903 /* if (rc < 0) should we set writebehind rc? */
2910 set_page_dirty(page);
2914 spin_lock(&inode->i_lock);
2915 if (pos > inode->i_size) {
2916 i_size_write(inode, pos);
2917 inode->i_blocks = (512 - 1 + pos) >> 9;
2919 spin_unlock(&inode->i_lock);
2924 /* Indication to update ctime and mtime as close is deferred */
2925 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2930 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2935 struct cifs_tcon *tcon;
2936 struct TCP_Server_Info *server;
2937 struct cifsFileInfo *smbfile = file->private_data;
2938 struct inode *inode = file_inode(file);
2939 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2941 rc = file_write_and_wait_range(file, start, end);
2943 trace_cifs_fsync_err(inode->i_ino, rc);
2949 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2952 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2953 rc = cifs_zap_mapping(inode);
2955 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2956 rc = 0; /* don't care about it in fsync */
2960 tcon = tlink_tcon(smbfile->tlink);
2961 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2962 server = tcon->ses->server;
2963 if (server->ops->flush == NULL) {
2965 goto strict_fsync_exit;
2968 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2969 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2971 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2972 cifsFileInfo_put(smbfile);
2974 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2976 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2984 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2988 struct cifs_tcon *tcon;
2989 struct TCP_Server_Info *server;
2990 struct cifsFileInfo *smbfile = file->private_data;
2991 struct inode *inode = file_inode(file);
2992 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2994 rc = file_write_and_wait_range(file, start, end);
2996 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
3002 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
3005 tcon = tlink_tcon(smbfile->tlink);
3006 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3007 server = tcon->ses->server;
3008 if (server->ops->flush == NULL) {
3013 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3014 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3016 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3017 cifsFileInfo_put(smbfile);
3019 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3021 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3030 * As file closes, flush all cached write data for this inode checking
3031 * for write behind errors.
3033 int cifs_flush(struct file *file, fl_owner_t id)
3035 struct inode *inode = file_inode(file);
3038 if (file->f_mode & FMODE_WRITE)
3039 rc = filemap_write_and_wait(inode->i_mapping);
3041 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3043 /* get more nuanced writeback errors */
3044 rc = filemap_check_wb_err(file->f_mapping, 0);
3045 trace_cifs_flush_err(inode->i_ino, rc);
3051 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
3056 for (i = 0; i < num_pages; i++) {
3057 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3060 * save number of pages we have already allocated and
3061 * return with ENOMEM error
3070 for (i = 0; i < num_pages; i++)
3077 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
3082 clen = min_t(const size_t, len, wsize);
3083 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
3092 cifs_uncached_writedata_release(struct kref *refcount)
3095 struct cifs_writedata *wdata = container_of(refcount,
3096 struct cifs_writedata, refcount);
3098 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3099 for (i = 0; i < wdata->nr_pages; i++)
3100 put_page(wdata->pages[i]);
3101 cifs_writedata_release(refcount);
3104 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3107 cifs_uncached_writev_complete(struct work_struct *work)
3109 struct cifs_writedata *wdata = container_of(work,
3110 struct cifs_writedata, work);
3111 struct inode *inode = d_inode(wdata->cfile->dentry);
3112 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3114 spin_lock(&inode->i_lock);
3115 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3116 if (cifsi->server_eof > inode->i_size)
3117 i_size_write(inode, cifsi->server_eof);
3118 spin_unlock(&inode->i_lock);
3120 complete(&wdata->done);
3121 collect_uncached_write_data(wdata->ctx);
3122 /* the below call can possibly free the last ref to aio ctx */
3123 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3127 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
3128 size_t *len, unsigned long *num_pages)
3130 size_t save_len, copied, bytes, cur_len = *len;
3131 unsigned long i, nr_pages = *num_pages;
3134 for (i = 0; i < nr_pages; i++) {
3135 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
3136 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
3139 * If we didn't copy as much as we expected, then that
3140 * may mean we trod into an unmapped area. Stop copying
3141 * at that point. On the next pass through the big
3142 * loop, we'll likely end up getting a zero-length
3143 * write and bailing out of it.
3148 cur_len = save_len - cur_len;
3152 * If we have no data to send, then that probably means that
3153 * the copy above failed altogether. That's most likely because
3154 * the address in the iovec was bogus. Return -EFAULT and let
3155 * the caller free anything we allocated and bail out.
3161 * i + 1 now represents the number of pages we actually used in
3162 * the copy phase above.
3169 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3170 struct cifs_aio_ctx *ctx)
3173 struct cifs_credits credits;
3175 struct TCP_Server_Info *server = wdata->server;
3178 if (wdata->cfile->invalidHandle) {
3179 rc = cifs_reopen_file(wdata->cfile, false);
3188 * Wait for credits to resend this wdata.
3189 * Note: we are attempting to resend the whole wdata not in
3193 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3198 if (wsize < wdata->bytes) {
3199 add_credits_and_wake_if(server, &credits, 0);
3202 } while (wsize < wdata->bytes);
3203 wdata->credits = credits;
3205 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3208 if (wdata->cfile->invalidHandle)
3211 #ifdef CONFIG_CIFS_SMB_DIRECT
3213 wdata->mr->need_invalidate = true;
3214 smbd_deregister_mr(wdata->mr);
3218 rc = server->ops->async_writev(wdata,
3219 cifs_uncached_writedata_release);
3223 /* If the write was successfully sent, we are done */
3225 list_add_tail(&wdata->list, wdata_list);
3229 /* Roll back credits and retry if needed */
3230 add_credits_and_wake_if(server, &wdata->credits, 0);
3231 } while (rc == -EAGAIN);
3234 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3239 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
3240 struct cifsFileInfo *open_file,
3241 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3242 struct cifs_aio_ctx *ctx)
3246 unsigned long nr_pages, num_pages, i;
3247 struct cifs_writedata *wdata;
3248 struct iov_iter saved_from = *from;
3249 loff_t saved_offset = offset;
3251 struct TCP_Server_Info *server;
3252 struct page **pagevec;
3256 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3257 pid = open_file->pid;
3259 pid = current->tgid;
3261 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3266 struct cifs_credits credits_on_stack;
3267 struct cifs_credits *credits = &credits_on_stack;
3269 if (open_file->invalidHandle) {
3270 rc = cifs_reopen_file(open_file, false);
3277 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3282 cur_len = min_t(const size_t, len, wsize);
3284 if (ctx->direct_io) {
3287 result = iov_iter_get_pages_alloc2(
3288 from, &pagevec, cur_len, &start);
3291 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3292 result, iov_iter_type(from),
3293 from->iov_offset, from->count);
3297 add_credits_and_wake_if(server, credits, 0);
3300 cur_len = (size_t)result;
3303 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
3305 wdata = cifs_writedata_direct_alloc(pagevec,
3306 cifs_uncached_writev_complete);
3309 for (i = 0; i < nr_pages; i++)
3310 put_page(pagevec[i]);
3312 add_credits_and_wake_if(server, credits, 0);
3317 wdata->page_offset = start;
3320 cur_len - (PAGE_SIZE - start) -
3321 (nr_pages - 2) * PAGE_SIZE :
3324 nr_pages = get_numpages(wsize, len, &cur_len);
3325 wdata = cifs_writedata_alloc(nr_pages,
3326 cifs_uncached_writev_complete);
3329 add_credits_and_wake_if(server, credits, 0);
3333 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
3335 kvfree(wdata->pages);
3337 add_credits_and_wake_if(server, credits, 0);
3341 num_pages = nr_pages;
3342 rc = wdata_fill_from_iovec(
3343 wdata, from, &cur_len, &num_pages);
3345 for (i = 0; i < nr_pages; i++)
3346 put_page(wdata->pages[i]);
3347 kvfree(wdata->pages);
3349 add_credits_and_wake_if(server, credits, 0);
3354 * Bring nr_pages down to the number of pages we
3355 * actually used, and free any pages that we didn't use.
3357 for ( ; nr_pages > num_pages; nr_pages--)
3358 put_page(wdata->pages[nr_pages - 1]);
3360 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
3363 wdata->sync_mode = WB_SYNC_ALL;
3364 wdata->nr_pages = nr_pages;
3365 wdata->offset = (__u64)offset;
3366 wdata->cfile = cifsFileInfo_get(open_file);
3367 wdata->server = server;
3369 wdata->bytes = cur_len;
3370 wdata->pagesz = PAGE_SIZE;
3371 wdata->credits = credits_on_stack;
3373 kref_get(&ctx->refcount);
3375 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3378 if (wdata->cfile->invalidHandle)
3381 rc = server->ops->async_writev(wdata,
3382 cifs_uncached_writedata_release);
3386 add_credits_and_wake_if(server, &wdata->credits, 0);
3387 kref_put(&wdata->refcount,
3388 cifs_uncached_writedata_release);
3389 if (rc == -EAGAIN) {
3391 iov_iter_advance(from, offset - saved_offset);
3397 list_add_tail(&wdata->list, wdata_list);
3406 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3408 struct cifs_writedata *wdata, *tmp;
3409 struct cifs_tcon *tcon;
3410 struct cifs_sb_info *cifs_sb;
3411 struct dentry *dentry = ctx->cfile->dentry;
3414 tcon = tlink_tcon(ctx->cfile->tlink);
3415 cifs_sb = CIFS_SB(dentry->d_sb);
3417 mutex_lock(&ctx->aio_mutex);
3419 if (list_empty(&ctx->list)) {
3420 mutex_unlock(&ctx->aio_mutex);
3426 * Wait for and collect replies for any successful sends in order of
3427 * increasing offset. Once an error is hit, then return without waiting
3428 * for any more replies.
3431 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3433 if (!try_wait_for_completion(&wdata->done)) {
3434 mutex_unlock(&ctx->aio_mutex);
3441 ctx->total_len += wdata->bytes;
3443 /* resend call if it's a retryable error */
3444 if (rc == -EAGAIN) {
3445 struct list_head tmp_list;
3446 struct iov_iter tmp_from = ctx->iter;
3448 INIT_LIST_HEAD(&tmp_list);
3449 list_del_init(&wdata->list);
3452 rc = cifs_resend_wdata(
3453 wdata, &tmp_list, ctx);
3455 iov_iter_advance(&tmp_from,
3456 wdata->offset - ctx->pos);
3458 rc = cifs_write_from_iter(wdata->offset,
3459 wdata->bytes, &tmp_from,
3460 ctx->cfile, cifs_sb, &tmp_list,
3463 kref_put(&wdata->refcount,
3464 cifs_uncached_writedata_release);
3467 list_splice(&tmp_list, &ctx->list);
3471 list_del_init(&wdata->list);
3472 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3475 cifs_stats_bytes_written(tcon, ctx->total_len);
3476 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3478 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3480 mutex_unlock(&ctx->aio_mutex);
3482 if (ctx->iocb && ctx->iocb->ki_complete)
3483 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
3485 complete(&ctx->done);
3488 static ssize_t __cifs_writev(
3489 struct kiocb *iocb, struct iov_iter *from, bool direct)
3491 struct file *file = iocb->ki_filp;
3492 ssize_t total_written = 0;
3493 struct cifsFileInfo *cfile;
3494 struct cifs_tcon *tcon;
3495 struct cifs_sb_info *cifs_sb;
3496 struct cifs_aio_ctx *ctx;
3497 struct iov_iter saved_from = *from;
3498 size_t len = iov_iter_count(from);
3502 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3503 * In this case, fall back to non-direct write function.
3504 * this could be improved by getting pages directly in ITER_KVEC
3506 if (direct && iov_iter_is_kvec(from)) {
3507 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3511 rc = generic_write_checks(iocb, from);
3515 cifs_sb = CIFS_FILE_SB(file);
3516 cfile = file->private_data;
3517 tcon = tlink_tcon(cfile->tlink);
3519 if (!tcon->ses->server->ops->async_writev)
3522 ctx = cifs_aio_ctx_alloc();
3526 ctx->cfile = cifsFileInfo_get(cfile);
3528 if (!is_sync_kiocb(iocb))
3531 ctx->pos = iocb->ki_pos;
3534 ctx->direct_io = true;
3538 rc = setup_aio_ctx_iter(ctx, from, ITER_SOURCE);
3540 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3545 /* grab a lock here due to read response handlers can access ctx */
3546 mutex_lock(&ctx->aio_mutex);
3548 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3549 cfile, cifs_sb, &ctx->list, ctx);
3552 * If at least one write was successfully sent, then discard any rc
3553 * value from the later writes. If the other write succeeds, then
3554 * we'll end up returning whatever was written. If it fails, then
3555 * we'll get a new rc value from that.
3557 if (!list_empty(&ctx->list))
3560 mutex_unlock(&ctx->aio_mutex);
3563 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3567 if (!is_sync_kiocb(iocb)) {
3568 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3569 return -EIOCBQUEUED;
3572 rc = wait_for_completion_killable(&ctx->done);
3574 mutex_lock(&ctx->aio_mutex);
3575 ctx->rc = rc = -EINTR;
3576 total_written = ctx->total_len;
3577 mutex_unlock(&ctx->aio_mutex);
3580 total_written = ctx->total_len;
3583 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3585 if (unlikely(!total_written))
3588 iocb->ki_pos += total_written;
3589 return total_written;
3592 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3594 struct file *file = iocb->ki_filp;
3596 cifs_revalidate_mapping(file->f_inode);
3597 return __cifs_writev(iocb, from, true);
3600 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3602 return __cifs_writev(iocb, from, false);
3606 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3608 struct file *file = iocb->ki_filp;
3609 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3610 struct inode *inode = file->f_mapping->host;
3611 struct cifsInodeInfo *cinode = CIFS_I(inode);
3612 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3617 * We need to hold the sem to be sure nobody modifies lock list
3618 * with a brlock that prevents writing.
3620 down_read(&cinode->lock_sem);
3622 rc = generic_write_checks(iocb, from);
3626 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3627 server->vals->exclusive_lock_type, 0,
3628 NULL, CIFS_WRITE_OP))
3629 rc = __generic_file_write_iter(iocb, from);
3633 up_read(&cinode->lock_sem);
3634 inode_unlock(inode);
3637 rc = generic_write_sync(iocb, rc);
3642 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3644 struct inode *inode = file_inode(iocb->ki_filp);
3645 struct cifsInodeInfo *cinode = CIFS_I(inode);
3646 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3647 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3648 iocb->ki_filp->private_data;
3649 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3652 written = cifs_get_writer(cinode);
3656 if (CIFS_CACHE_WRITE(cinode)) {
3657 if (cap_unix(tcon->ses) &&
3658 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3659 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3660 written = generic_file_write_iter(iocb, from);
3663 written = cifs_writev(iocb, from);
3667 * For non-oplocked files in strict cache mode we need to write the data
3668 * to the server exactly from the pos to pos+len-1 rather than flush all
3669 * affected pages because it may cause a error with mandatory locks on
3670 * these pages but not on the region from pos to ppos+len-1.
3672 written = cifs_user_writev(iocb, from);
3673 if (CIFS_CACHE_READ(cinode)) {
3675 * We have read level caching and we have just sent a write
3676 * request to the server thus making data in the cache stale.
3677 * Zap the cache and set oplock/lease level to NONE to avoid
3678 * reading stale data from the cache. All subsequent read
3679 * operations will read new data from the server.
3681 cifs_zap_mapping(inode);
3682 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3687 cifs_put_writer(cinode);
3691 static struct cifs_readdata *
3692 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3694 struct cifs_readdata *rdata;
3696 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3697 if (rdata != NULL) {
3698 rdata->pages = pages;
3699 kref_init(&rdata->refcount);
3700 INIT_LIST_HEAD(&rdata->list);
3701 init_completion(&rdata->done);
3702 INIT_WORK(&rdata->work, complete);
3708 static struct cifs_readdata *
3709 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3711 struct page **pages =
3712 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3713 struct cifs_readdata *ret = NULL;
3716 ret = cifs_readdata_direct_alloc(pages, complete);
3725 cifs_readdata_release(struct kref *refcount)
3727 struct cifs_readdata *rdata = container_of(refcount,
3728 struct cifs_readdata, refcount);
3729 #ifdef CONFIG_CIFS_SMB_DIRECT
3731 smbd_deregister_mr(rdata->mr);
3736 cifsFileInfo_put(rdata->cfile);
3738 kvfree(rdata->pages);
3743 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3749 for (i = 0; i < nr_pages; i++) {
3750 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3755 rdata->pages[i] = page;
3759 unsigned int nr_page_failed = i;
3761 for (i = 0; i < nr_page_failed; i++) {
3762 put_page(rdata->pages[i]);
3763 rdata->pages[i] = NULL;
3770 cifs_uncached_readdata_release(struct kref *refcount)
3772 struct cifs_readdata *rdata = container_of(refcount,
3773 struct cifs_readdata, refcount);
3776 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3777 for (i = 0; i < rdata->nr_pages; i++) {
3778 put_page(rdata->pages[i]);
3780 cifs_readdata_release(refcount);
3784 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3785 * @rdata: the readdata response with list of pages holding data
3786 * @iter: destination for our data
3788 * This function copies data from a list of pages in a readdata response into
3789 * an array of iovecs. It will first calculate where the data should go
3790 * based on the info in the readdata and then copy the data into that spot.
3793 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3795 size_t remaining = rdata->got_bytes;
3798 for (i = 0; i < rdata->nr_pages; i++) {
3799 struct page *page = rdata->pages[i];
3800 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3803 if (unlikely(iov_iter_is_pipe(iter))) {
3804 void *addr = kmap_atomic(page);
3806 written = copy_to_iter(addr, copy, iter);
3807 kunmap_atomic(addr);
3809 written = copy_page_to_iter(page, 0, copy, iter);
3810 remaining -= written;
3811 if (written < copy && iov_iter_count(iter) > 0)
3814 return remaining ? -EFAULT : 0;
3817 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3820 cifs_uncached_readv_complete(struct work_struct *work)
3822 struct cifs_readdata *rdata = container_of(work,
3823 struct cifs_readdata, work);
3825 complete(&rdata->done);
3826 collect_uncached_read_data(rdata->ctx);
3827 /* the below call can possibly free the last ref to aio ctx */
3828 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3832 uncached_fill_pages(struct TCP_Server_Info *server,
3833 struct cifs_readdata *rdata, struct iov_iter *iter,
3838 unsigned int nr_pages = rdata->nr_pages;
3839 unsigned int page_offset = rdata->page_offset;
3841 rdata->got_bytes = 0;
3842 rdata->tailsz = PAGE_SIZE;
3843 for (i = 0; i < nr_pages; i++) {
3844 struct page *page = rdata->pages[i];
3846 unsigned int segment_size = rdata->pagesz;
3849 segment_size -= page_offset;
3855 /* no need to hold page hostage */
3856 rdata->pages[i] = NULL;
3863 if (len >= segment_size)
3864 /* enough data to fill the page */
3867 rdata->tailsz = len;
3871 result = copy_page_from_iter(
3872 page, page_offset, n, iter);
3873 #ifdef CONFIG_CIFS_SMB_DIRECT
3878 result = cifs_read_page_from_socket(
3879 server, page, page_offset, n);
3883 rdata->got_bytes += result;
3886 return result != -ECONNABORTED && rdata->got_bytes > 0 ?
3887 rdata->got_bytes : result;
3891 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3892 struct cifs_readdata *rdata, unsigned int len)
3894 return uncached_fill_pages(server, rdata, NULL, len);
3898 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3899 struct cifs_readdata *rdata,
3900 struct iov_iter *iter)
3902 return uncached_fill_pages(server, rdata, iter, iter->count);
3905 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3906 struct list_head *rdata_list,
3907 struct cifs_aio_ctx *ctx)
3910 struct cifs_credits credits;
3912 struct TCP_Server_Info *server;
3914 /* XXX: should we pick a new channel here? */
3915 server = rdata->server;
3918 if (rdata->cfile->invalidHandle) {
3919 rc = cifs_reopen_file(rdata->cfile, true);
3927 * Wait for credits to resend this rdata.
3928 * Note: we are attempting to resend the whole rdata not in
3932 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3938 if (rsize < rdata->bytes) {
3939 add_credits_and_wake_if(server, &credits, 0);
3942 } while (rsize < rdata->bytes);
3943 rdata->credits = credits;
3945 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3947 if (rdata->cfile->invalidHandle)
3950 #ifdef CONFIG_CIFS_SMB_DIRECT
3952 rdata->mr->need_invalidate = true;
3953 smbd_deregister_mr(rdata->mr);
3957 rc = server->ops->async_readv(rdata);
3961 /* If the read was successfully sent, we are done */
3963 /* Add to aio pending list */
3964 list_add_tail(&rdata->list, rdata_list);
3968 /* Roll back credits and retry if needed */
3969 add_credits_and_wake_if(server, &rdata->credits, 0);
3970 } while (rc == -EAGAIN);
3973 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3978 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3979 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3980 struct cifs_aio_ctx *ctx)
3982 struct cifs_readdata *rdata;
3983 unsigned int npages, rsize;
3984 struct cifs_credits credits_on_stack;
3985 struct cifs_credits *credits = &credits_on_stack;
3989 struct TCP_Server_Info *server;
3990 struct page **pagevec;
3992 struct iov_iter direct_iov = ctx->iter;
3994 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3996 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3997 pid = open_file->pid;
3999 pid = current->tgid;
4002 iov_iter_advance(&direct_iov, offset - ctx->pos);
4005 if (open_file->invalidHandle) {
4006 rc = cifs_reopen_file(open_file, true);
4013 if (cifs_sb->ctx->rsize == 0)
4014 cifs_sb->ctx->rsize =
4015 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4018 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4023 cur_len = min_t(const size_t, len, rsize);
4025 if (ctx->direct_io) {
4028 result = iov_iter_get_pages_alloc2(
4029 &direct_iov, &pagevec,
4033 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
4034 result, iov_iter_type(&direct_iov),
4035 direct_iov.iov_offset,
4040 add_credits_and_wake_if(server, credits, 0);
4043 cur_len = (size_t)result;
4045 rdata = cifs_readdata_direct_alloc(
4046 pagevec, cifs_uncached_readv_complete);
4048 add_credits_and_wake_if(server, credits, 0);
4053 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
4054 rdata->page_offset = start;
4055 rdata->tailsz = npages > 1 ?
4056 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
4061 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
4062 /* allocate a readdata struct */
4063 rdata = cifs_readdata_alloc(npages,
4064 cifs_uncached_readv_complete);
4066 add_credits_and_wake_if(server, credits, 0);
4071 rc = cifs_read_allocate_pages(rdata, npages);
4073 kvfree(rdata->pages);
4075 add_credits_and_wake_if(server, credits, 0);
4079 rdata->tailsz = PAGE_SIZE;
4082 rdata->server = server;
4083 rdata->cfile = cifsFileInfo_get(open_file);
4084 rdata->nr_pages = npages;
4085 rdata->offset = offset;
4086 rdata->bytes = cur_len;
4088 rdata->pagesz = PAGE_SIZE;
4089 rdata->read_into_pages = cifs_uncached_read_into_pages;
4090 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
4091 rdata->credits = credits_on_stack;
4093 kref_get(&ctx->refcount);
4095 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4098 if (rdata->cfile->invalidHandle)
4101 rc = server->ops->async_readv(rdata);
4105 add_credits_and_wake_if(server, &rdata->credits, 0);
4106 kref_put(&rdata->refcount,
4107 cifs_uncached_readdata_release);
4108 if (rc == -EAGAIN) {
4109 iov_iter_revert(&direct_iov, cur_len);
4115 list_add_tail(&rdata->list, rdata_list);
4124 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4126 struct cifs_readdata *rdata, *tmp;
4127 struct iov_iter *to = &ctx->iter;
4128 struct cifs_sb_info *cifs_sb;
4131 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4133 mutex_lock(&ctx->aio_mutex);
4135 if (list_empty(&ctx->list)) {
4136 mutex_unlock(&ctx->aio_mutex);
4141 /* the loop below should proceed in the order of increasing offsets */
4143 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4145 if (!try_wait_for_completion(&rdata->done)) {
4146 mutex_unlock(&ctx->aio_mutex);
4150 if (rdata->result == -EAGAIN) {
4151 /* resend call if it's a retryable error */
4152 struct list_head tmp_list;
4153 unsigned int got_bytes = rdata->got_bytes;
4155 list_del_init(&rdata->list);
4156 INIT_LIST_HEAD(&tmp_list);
4159 * Got a part of data and then reconnect has
4160 * happened -- fill the buffer and continue
4163 if (got_bytes && got_bytes < rdata->bytes) {
4165 if (!ctx->direct_io)
4166 rc = cifs_readdata_to_iov(rdata, to);
4168 kref_put(&rdata->refcount,
4169 cifs_uncached_readdata_release);
4174 if (ctx->direct_io) {
4176 * Re-use rdata as this is a
4179 rc = cifs_resend_rdata(
4183 rc = cifs_send_async_read(
4184 rdata->offset + got_bytes,
4185 rdata->bytes - got_bytes,
4186 rdata->cfile, cifs_sb,
4189 kref_put(&rdata->refcount,
4190 cifs_uncached_readdata_release);
4193 list_splice(&tmp_list, &ctx->list);
4196 } else if (rdata->result)
4198 else if (!ctx->direct_io)
4199 rc = cifs_readdata_to_iov(rdata, to);
4201 /* if there was a short read -- discard anything left */
4202 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4205 ctx->total_len += rdata->got_bytes;
4207 list_del_init(&rdata->list);
4208 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
4211 if (!ctx->direct_io)
4212 ctx->total_len = ctx->len - iov_iter_count(to);
4214 /* mask nodata case */
4218 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4220 mutex_unlock(&ctx->aio_mutex);
4222 if (ctx->iocb && ctx->iocb->ki_complete)
4223 ctx->iocb->ki_complete(ctx->iocb, ctx->rc);
4225 complete(&ctx->done);
4228 static ssize_t __cifs_readv(
4229 struct kiocb *iocb, struct iov_iter *to, bool direct)
4232 struct file *file = iocb->ki_filp;
4233 struct cifs_sb_info *cifs_sb;
4234 struct cifsFileInfo *cfile;
4235 struct cifs_tcon *tcon;
4236 ssize_t rc, total_read = 0;
4237 loff_t offset = iocb->ki_pos;
4238 struct cifs_aio_ctx *ctx;
4241 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
4242 * fall back to data copy read path
4243 * this could be improved by getting pages directly in ITER_KVEC
4245 if (direct && iov_iter_is_kvec(to)) {
4246 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
4250 len = iov_iter_count(to);
4254 cifs_sb = CIFS_FILE_SB(file);
4255 cfile = file->private_data;
4256 tcon = tlink_tcon(cfile->tlink);
4258 if (!tcon->ses->server->ops->async_readv)
4261 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4262 cifs_dbg(FYI, "attempting read on write only file instance\n");
4264 ctx = cifs_aio_ctx_alloc();
4268 ctx->cfile = cifsFileInfo_get(cfile);
4270 if (!is_sync_kiocb(iocb))
4273 if (user_backed_iter(to))
4274 ctx->should_dirty = true;
4278 ctx->direct_io = true;
4282 rc = setup_aio_ctx_iter(ctx, to, ITER_DEST);
4284 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4291 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
4292 offset, offset + len - 1);
4294 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4299 /* grab a lock here due to read response handlers can access ctx */
4300 mutex_lock(&ctx->aio_mutex);
4302 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4304 /* if at least one read request send succeeded, then reset rc */
4305 if (!list_empty(&ctx->list))
4308 mutex_unlock(&ctx->aio_mutex);
4311 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4315 if (!is_sync_kiocb(iocb)) {
4316 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4317 return -EIOCBQUEUED;
4320 rc = wait_for_completion_killable(&ctx->done);
4322 mutex_lock(&ctx->aio_mutex);
4323 ctx->rc = rc = -EINTR;
4324 total_read = ctx->total_len;
4325 mutex_unlock(&ctx->aio_mutex);
4328 total_read = ctx->total_len;
4331 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4334 iocb->ki_pos += total_read;
4340 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4342 return __cifs_readv(iocb, to, true);
4345 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4347 return __cifs_readv(iocb, to, false);
4351 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4353 struct inode *inode = file_inode(iocb->ki_filp);
4354 struct cifsInodeInfo *cinode = CIFS_I(inode);
4355 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4356 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4357 iocb->ki_filp->private_data;
4358 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4362 * In strict cache mode we need to read from the server all the time
4363 * if we don't have level II oplock because the server can delay mtime
4364 * change - so we can't make a decision about inode invalidating.
4365 * And we can also fail with pagereading if there are mandatory locks
4366 * on pages affected by this read but not on the region from pos to
4369 if (!CIFS_CACHE_READ(cinode))
4370 return cifs_user_readv(iocb, to);
4372 if (cap_unix(tcon->ses) &&
4373 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4374 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4375 return generic_file_read_iter(iocb, to);
4378 * We need to hold the sem to be sure nobody modifies lock list
4379 * with a brlock that prevents reading.
4381 down_read(&cinode->lock_sem);
4382 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4383 tcon->ses->server->vals->shared_lock_type,
4384 0, NULL, CIFS_READ_OP))
4385 rc = generic_file_read_iter(iocb, to);
4386 up_read(&cinode->lock_sem);
4391 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4394 unsigned int bytes_read = 0;
4395 unsigned int total_read;
4396 unsigned int current_read_size;
4398 struct cifs_sb_info *cifs_sb;
4399 struct cifs_tcon *tcon;
4400 struct TCP_Server_Info *server;
4403 struct cifsFileInfo *open_file;
4404 struct cifs_io_parms io_parms = {0};
4405 int buf_type = CIFS_NO_BUFFER;
4409 cifs_sb = CIFS_FILE_SB(file);
4411 /* FIXME: set up handlers for larger reads and/or convert to async */
4412 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4414 if (file->private_data == NULL) {
4419 open_file = file->private_data;
4420 tcon = tlink_tcon(open_file->tlink);
4421 server = cifs_pick_channel(tcon->ses);
4423 if (!server->ops->sync_read) {
4428 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4429 pid = open_file->pid;
4431 pid = current->tgid;
4433 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4434 cifs_dbg(FYI, "attempting read on write only file instance\n");
4436 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4437 total_read += bytes_read, cur_offset += bytes_read) {
4439 current_read_size = min_t(uint, read_size - total_read,
4442 * For windows me and 9x we do not want to request more
4443 * than it negotiated since it will refuse the read
4446 if (!(tcon->ses->capabilities &
4447 tcon->ses->server->vals->cap_large_files)) {
4448 current_read_size = min_t(uint,
4449 current_read_size, CIFSMaxBufSize);
4451 if (open_file->invalidHandle) {
4452 rc = cifs_reopen_file(open_file, true);
4457 io_parms.tcon = tcon;
4458 io_parms.offset = *offset;
4459 io_parms.length = current_read_size;
4460 io_parms.server = server;
4461 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4462 &bytes_read, &cur_offset,
4464 } while (rc == -EAGAIN);
4466 if (rc || (bytes_read == 0)) {
4474 cifs_stats_bytes_read(tcon, total_read);
4475 *offset += bytes_read;
4483 * If the page is mmap'ed into a process' page tables, then we need to make
4484 * sure that it doesn't change while being written back.
4487 cifs_page_mkwrite(struct vm_fault *vmf)
4489 struct page *page = vmf->page;
4491 /* Wait for the page to be written to the cache before we allow it to
4492 * be modified. We then assume the entire page will need writing back.
4494 #ifdef CONFIG_CIFS_FSCACHE
4495 if (PageFsCache(page) &&
4496 wait_on_page_fscache_killable(page) < 0)
4497 return VM_FAULT_RETRY;
4500 wait_on_page_writeback(page);
4502 if (lock_page_killable(page) < 0)
4503 return VM_FAULT_RETRY;
4504 return VM_FAULT_LOCKED;
4507 static const struct vm_operations_struct cifs_file_vm_ops = {
4508 .fault = filemap_fault,
4509 .map_pages = filemap_map_pages,
4510 .page_mkwrite = cifs_page_mkwrite,
4513 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4516 struct inode *inode = file_inode(file);
4520 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4521 rc = cifs_zap_mapping(inode);
4523 rc = generic_file_mmap(file, vma);
4525 vma->vm_ops = &cifs_file_vm_ops;
4531 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4537 rc = cifs_revalidate_file(file);
4539 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4542 rc = generic_file_mmap(file, vma);
4544 vma->vm_ops = &cifs_file_vm_ops;
4551 cifs_readv_complete(struct work_struct *work)
4553 unsigned int i, got_bytes;
4554 struct cifs_readdata *rdata = container_of(work,
4555 struct cifs_readdata, work);
4557 got_bytes = rdata->got_bytes;
4558 for (i = 0; i < rdata->nr_pages; i++) {
4559 struct page *page = rdata->pages[i];
4561 if (rdata->result == 0 ||
4562 (rdata->result == -EAGAIN && got_bytes)) {
4563 flush_dcache_page(page);
4564 SetPageUptodate(page);
4568 if (rdata->result == 0 ||
4569 (rdata->result == -EAGAIN && got_bytes))
4570 cifs_readpage_to_fscache(rdata->mapping->host, page);
4574 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4577 rdata->pages[i] = NULL;
4579 kref_put(&rdata->refcount, cifs_readdata_release);
4583 readpages_fill_pages(struct TCP_Server_Info *server,
4584 struct cifs_readdata *rdata, struct iov_iter *iter,
4591 unsigned int nr_pages = rdata->nr_pages;
4592 unsigned int page_offset = rdata->page_offset;
4594 /* determine the eof that the server (probably) has */
4595 eof = CIFS_I(rdata->mapping->host)->server_eof;
4596 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4597 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4599 rdata->got_bytes = 0;
4600 rdata->tailsz = PAGE_SIZE;
4601 for (i = 0; i < nr_pages; i++) {
4602 struct page *page = rdata->pages[i];
4603 unsigned int to_read = rdata->pagesz;
4607 to_read -= page_offset;
4613 if (len >= to_read) {
4615 } else if (len > 0) {
4616 /* enough for partial page, fill and zero the rest */
4617 zero_user(page, len + page_offset, to_read - len);
4618 n = rdata->tailsz = len;
4620 } else if (page->index > eof_index) {
4622 * The VFS will not try to do readahead past the
4623 * i_size, but it's possible that we have outstanding
4624 * writes with gaps in the middle and the i_size hasn't
4625 * caught up yet. Populate those with zeroed out pages
4626 * to prevent the VFS from repeatedly attempting to
4627 * fill them until the writes are flushed.
4629 zero_user(page, 0, PAGE_SIZE);
4630 flush_dcache_page(page);
4631 SetPageUptodate(page);
4634 rdata->pages[i] = NULL;
4638 /* no need to hold page hostage */
4641 rdata->pages[i] = NULL;
4647 result = copy_page_from_iter(
4648 page, page_offset, n, iter);
4649 #ifdef CONFIG_CIFS_SMB_DIRECT
4654 result = cifs_read_page_from_socket(
4655 server, page, page_offset, n);
4659 rdata->got_bytes += result;
4662 return result != -ECONNABORTED && rdata->got_bytes > 0 ?
4663 rdata->got_bytes : result;
4667 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4668 struct cifs_readdata *rdata, unsigned int len)
4670 return readpages_fill_pages(server, rdata, NULL, len);
4674 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4675 struct cifs_readdata *rdata,
4676 struct iov_iter *iter)
4678 return readpages_fill_pages(server, rdata, iter, iter->count);
4681 static void cifs_readahead(struct readahead_control *ractl)
4684 struct cifsFileInfo *open_file = ractl->file->private_data;
4685 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(ractl->file);
4686 struct TCP_Server_Info *server;
4688 unsigned int xid, nr_pages, last_batch_size = 0, cache_nr_pages = 0;
4689 pgoff_t next_cached = ULONG_MAX;
4690 bool caching = fscache_cookie_enabled(cifs_inode_cookie(ractl->mapping->host)) &&
4691 cifs_inode_cookie(ractl->mapping->host)->cache_priv;
4692 bool check_cache = caching;
4696 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4697 pid = open_file->pid;
4699 pid = current->tgid;
4702 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4704 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4705 __func__, ractl->file, ractl->mapping, readahead_count(ractl));
4708 * Chop the readahead request up into rsize-sized read requests.
4710 while ((nr_pages = readahead_count(ractl) - last_batch_size)) {
4711 unsigned int i, got, rsize;
4713 struct cifs_readdata *rdata;
4714 struct cifs_credits credits_on_stack;
4715 struct cifs_credits *credits = &credits_on_stack;
4716 pgoff_t index = readahead_index(ractl) + last_batch_size;
4719 * Find out if we have anything cached in the range of
4720 * interest, and if so, where the next chunk of cached data is.
4724 rc = cifs_fscache_query_occupancy(
4725 ractl->mapping->host, index, nr_pages,
4726 &next_cached, &cache_nr_pages);
4729 check_cache = false;
4732 if (index == next_cached) {
4734 * TODO: Send a whole batch of pages to be read
4737 struct folio *folio = readahead_folio(ractl);
4739 last_batch_size = folio_nr_pages(folio);
4740 if (cifs_readpage_from_fscache(ractl->mapping->host,
4741 &folio->page) < 0) {
4743 * TODO: Deal with cache read failure
4744 * here, but for the moment, delegate
4749 folio_unlock(folio);
4752 if (cache_nr_pages == 0)
4758 if (open_file->invalidHandle) {
4759 rc = cifs_reopen_file(open_file, true);
4767 if (cifs_sb->ctx->rsize == 0)
4768 cifs_sb->ctx->rsize =
4769 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4772 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4776 nr_pages = min_t(size_t, rsize / PAGE_SIZE, readahead_count(ractl));
4777 nr_pages = min_t(size_t, nr_pages, next_cached - index);
4780 * Give up immediately if rsize is too small to read an entire
4781 * page. The VFS will fall back to readpage. We should never
4782 * reach this point however since we set ra_pages to 0 when the
4783 * rsize is smaller than a cache page.
4785 if (unlikely(!nr_pages)) {
4786 add_credits_and_wake_if(server, credits, 0);
4790 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4792 /* best to give up if we're out of mem */
4793 add_credits_and_wake_if(server, credits, 0);
4797 got = __readahead_batch(ractl, rdata->pages, nr_pages);
4798 if (got != nr_pages) {
4799 pr_warn("__readahead_batch() returned %u/%u\n",
4804 rdata->nr_pages = nr_pages;
4805 rdata->bytes = readahead_batch_length(ractl);
4806 rdata->cfile = cifsFileInfo_get(open_file);
4807 rdata->server = server;
4808 rdata->mapping = ractl->mapping;
4809 rdata->offset = readahead_pos(ractl);
4811 rdata->pagesz = PAGE_SIZE;
4812 rdata->tailsz = PAGE_SIZE;
4813 rdata->read_into_pages = cifs_readpages_read_into_pages;
4814 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4815 rdata->credits = credits_on_stack;
4817 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4819 if (rdata->cfile->invalidHandle)
4822 rc = server->ops->async_readv(rdata);
4826 add_credits_and_wake_if(server, &rdata->credits, 0);
4827 for (i = 0; i < rdata->nr_pages; i++) {
4828 page = rdata->pages[i];
4832 /* Fallback to the readpage in error/reconnect cases */
4833 kref_put(&rdata->refcount, cifs_readdata_release);
4837 kref_put(&rdata->refcount, cifs_readdata_release);
4838 last_batch_size = nr_pages;
4845 * cifs_readpage_worker must be called with the page pinned
4847 static int cifs_readpage_worker(struct file *file, struct page *page,
4853 /* Is the page cached? */
4854 rc = cifs_readpage_from_fscache(file_inode(file), page);
4858 read_data = kmap(page);
4859 /* for reads over a certain size could initiate async read ahead */
4861 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4866 cifs_dbg(FYI, "Bytes read %d\n", rc);
4868 /* we do not want atime to be less than mtime, it broke some apps */
4869 file_inode(file)->i_atime = current_time(file_inode(file));
4870 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)))
4871 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4873 file_inode(file)->i_atime = current_time(file_inode(file));
4876 memset(read_data + rc, 0, PAGE_SIZE - rc);
4878 flush_dcache_page(page);
4879 SetPageUptodate(page);
4881 /* send this page to the cache */
4882 cifs_readpage_to_fscache(file_inode(file), page);
4894 static int cifs_read_folio(struct file *file, struct folio *folio)
4896 struct page *page = &folio->page;
4897 loff_t offset = page_file_offset(page);
4903 if (file->private_data == NULL) {
4909 cifs_dbg(FYI, "read_folio %p at offset %d 0x%x\n",
4910 page, (int)offset, (int)offset);
4912 rc = cifs_readpage_worker(file, page, &offset);
4918 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4920 struct cifsFileInfo *open_file;
4922 spin_lock(&cifs_inode->open_file_lock);
4923 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4924 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4925 spin_unlock(&cifs_inode->open_file_lock);
4929 spin_unlock(&cifs_inode->open_file_lock);
4933 /* We do not want to update the file size from server for inodes
4934 open for write - to avoid races with writepage extending
4935 the file - in the future we could consider allowing
4936 refreshing the inode only on increases in the file size
4937 but this is tricky to do without racing with writebehind
4938 page caching in the current Linux kernel design */
4939 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4944 if (is_inode_writable(cifsInode)) {
4945 /* This inode is open for write at least once */
4946 struct cifs_sb_info *cifs_sb;
4948 cifs_sb = CIFS_SB(cifsInode->netfs.inode.i_sb);
4949 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
4950 /* since no page cache to corrupt on directio
4951 we can change size safely */
4955 if (i_size_read(&cifsInode->netfs.inode) < end_of_file)
4963 static int cifs_write_begin(struct file *file, struct address_space *mapping,
4964 loff_t pos, unsigned len,
4965 struct page **pagep, void **fsdata)
4968 pgoff_t index = pos >> PAGE_SHIFT;
4969 loff_t offset = pos & (PAGE_SIZE - 1);
4970 loff_t page_start = pos & PAGE_MASK;
4975 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
4978 page = grab_cache_page_write_begin(mapping, index);
4984 if (PageUptodate(page))
4988 * If we write a full page it will be up to date, no need to read from
4989 * the server. If the write is short, we'll end up doing a sync write
4992 if (len == PAGE_SIZE)
4996 * optimize away the read when we have an oplock, and we're not
4997 * expecting to use any of the data we'd be reading in. That
4998 * is, when the page lies beyond the EOF, or straddles the EOF
4999 * and the write will cover all of the existing data.
5001 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
5002 i_size = i_size_read(mapping->host);
5003 if (page_start >= i_size ||
5004 (offset == 0 && (pos + len) >= i_size)) {
5005 zero_user_segments(page, 0, offset,
5009 * PageChecked means that the parts of the page
5010 * to which we're not writing are considered up
5011 * to date. Once the data is copied to the
5012 * page, it can be set uptodate.
5014 SetPageChecked(page);
5019 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
5021 * might as well read a page, it is fast enough. If we get
5022 * an error, we don't need to return it. cifs_write_end will
5023 * do a sync write instead since PG_uptodate isn't set.
5025 cifs_readpage_worker(file, page, &page_start);
5030 /* we could try using another file handle if there is one -
5031 but how would we lock it to prevent close of that handle
5032 racing with this read? In any case
5033 this will be written out by write_end so is fine */
5040 static bool cifs_release_folio(struct folio *folio, gfp_t gfp)
5042 if (folio_test_private(folio))
5044 if (folio_test_fscache(folio)) {
5045 if (current_is_kswapd() || !(gfp & __GFP_FS))
5047 folio_wait_fscache(folio);
5049 fscache_note_page_release(cifs_inode_cookie(folio->mapping->host));
5053 static void cifs_invalidate_folio(struct folio *folio, size_t offset,
5056 folio_wait_fscache(folio);
5059 static int cifs_launder_folio(struct folio *folio)
5062 loff_t range_start = folio_pos(folio);
5063 loff_t range_end = range_start + folio_size(folio);
5064 struct writeback_control wbc = {
5065 .sync_mode = WB_SYNC_ALL,
5067 .range_start = range_start,
5068 .range_end = range_end,
5071 cifs_dbg(FYI, "Launder page: %lu\n", folio->index);
5073 if (folio_clear_dirty_for_io(folio))
5074 rc = cifs_writepage_locked(&folio->page, &wbc);
5076 folio_wait_fscache(folio);
5080 void cifs_oplock_break(struct work_struct *work)
5082 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
5084 struct inode *inode = d_inode(cfile->dentry);
5085 struct cifsInodeInfo *cinode = CIFS_I(inode);
5086 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
5087 struct TCP_Server_Info *server = tcon->ses->server;
5089 bool purge_cache = false;
5091 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
5092 TASK_UNINTERRUPTIBLE);
5094 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
5095 cfile->oplock_epoch, &purge_cache);
5097 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
5098 cifs_has_mand_locks(cinode)) {
5099 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
5104 if (inode && S_ISREG(inode->i_mode)) {
5105 if (CIFS_CACHE_READ(cinode))
5106 break_lease(inode, O_RDONLY);
5108 break_lease(inode, O_WRONLY);
5109 rc = filemap_fdatawrite(inode->i_mapping);
5110 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
5111 rc = filemap_fdatawait(inode->i_mapping);
5112 mapping_set_error(inode->i_mapping, rc);
5113 cifs_zap_mapping(inode);
5115 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
5116 if (CIFS_CACHE_WRITE(cinode))
5117 goto oplock_break_ack;
5120 rc = cifs_push_locks(cfile);
5122 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
5126 * releasing stale oplock after recent reconnect of smb session using
5127 * a now incorrect file handle is not a data integrity issue but do
5128 * not bother sending an oplock release if session to server still is
5129 * disconnected since oplock already released by the server
5131 if (!cfile->oplock_break_cancelled) {
5132 rc = tcon->ses->server->ops->oplock_response(tcon, &cfile->fid,
5134 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
5137 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
5138 cifs_done_oplock_break(cinode);
5142 * The presence of cifs_direct_io() in the address space ops vector
5143 * allowes open() O_DIRECT flags which would have failed otherwise.
5145 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
5146 * so this method should never be called.
5148 * Direct IO is not yet supported in the cached mode.
5151 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
5155 * Eventually need to support direct IO for non forcedirectio mounts
5160 static int cifs_swap_activate(struct swap_info_struct *sis,
5161 struct file *swap_file, sector_t *span)
5163 struct cifsFileInfo *cfile = swap_file->private_data;
5164 struct inode *inode = swap_file->f_mapping->host;
5165 unsigned long blocks;
5168 cifs_dbg(FYI, "swap activate\n");
5170 if (!swap_file->f_mapping->a_ops->swap_rw)
5171 /* Cannot support swap */
5174 spin_lock(&inode->i_lock);
5175 blocks = inode->i_blocks;
5176 isize = inode->i_size;
5177 spin_unlock(&inode->i_lock);
5178 if (blocks*512 < isize) {
5179 pr_warn("swap activate: swapfile has holes\n");
5184 pr_warn_once("Swap support over SMB3 is experimental\n");
5187 * TODO: consider adding ACL (or documenting how) to prevent other
5188 * users (on this or other systems) from reading it
5192 /* TODO: add sk_set_memalloc(inet) or similar */
5195 cfile->swapfile = true;
5197 * TODO: Since file already open, we can't open with DENY_ALL here
5198 * but we could add call to grab a byte range lock to prevent others
5199 * from reading or writing the file
5202 sis->flags |= SWP_FS_OPS;
5203 return add_swap_extent(sis, 0, sis->max, 0);
5206 static void cifs_swap_deactivate(struct file *file)
5208 struct cifsFileInfo *cfile = file->private_data;
5210 cifs_dbg(FYI, "swap deactivate\n");
5212 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5215 cfile->swapfile = false;
5217 /* do we need to unpin (or unlock) the file */
5221 * Mark a page as having been made dirty and thus needing writeback. We also
5222 * need to pin the cache object to write back to.
5224 #ifdef CONFIG_CIFS_FSCACHE
5225 static bool cifs_dirty_folio(struct address_space *mapping, struct folio *folio)
5227 return fscache_dirty_folio(mapping, folio,
5228 cifs_inode_cookie(mapping->host));
5231 #define cifs_dirty_folio filemap_dirty_folio
5234 const struct address_space_operations cifs_addr_ops = {
5235 .read_folio = cifs_read_folio,
5236 .readahead = cifs_readahead,
5237 .writepage = cifs_writepage,
5238 .writepages = cifs_writepages,
5239 .write_begin = cifs_write_begin,
5240 .write_end = cifs_write_end,
5241 .dirty_folio = cifs_dirty_folio,
5242 .release_folio = cifs_release_folio,
5243 .direct_IO = cifs_direct_io,
5244 .invalidate_folio = cifs_invalidate_folio,
5245 .launder_folio = cifs_launder_folio,
5247 * TODO: investigate and if useful we could add an cifs_migratePage
5248 * helper (under an CONFIG_MIGRATION) in the future, and also
5249 * investigate and add an is_dirty_writeback helper if needed
5251 .swap_activate = cifs_swap_activate,
5252 .swap_deactivate = cifs_swap_deactivate,
5256 * cifs_readahead requires the server to support a buffer large enough to
5257 * contain the header plus one complete page of data. Otherwise, we need
5258 * to leave cifs_readahead out of the address space operations.
5260 const struct address_space_operations cifs_addr_ops_smallbuf = {
5261 .read_folio = cifs_read_folio,
5262 .writepage = cifs_writepage,
5263 .writepages = cifs_writepages,
5264 .write_begin = cifs_write_begin,
5265 .write_end = cifs_write_end,
5266 .dirty_folio = cifs_dirty_folio,
5267 .release_folio = cifs_release_folio,
5268 .invalidate_folio = cifs_invalidate_folio,
5269 .launder_folio = cifs_launder_folio,