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->tc_lock);
52 if (tcon->status != TID_NEED_RECON) {
53 spin_unlock(&tcon->tc_lock);
56 tcon->status = TID_IN_FILES_INVALIDATE;
57 spin_unlock(&tcon->tc_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;
427 cifs_fscache_release_inode_cookie(inode);
430 * Delete any outstanding lock records. We'll lose them when the file
433 cifs_down_write(&cifsi->lock_sem);
434 list_for_each_entry_safe(li, tmp, &cifs_file->llist->locks, llist) {
435 list_del(&li->llist);
436 cifs_del_lock_waiters(li);
439 list_del(&cifs_file->llist->llist);
440 kfree(cifs_file->llist);
441 up_write(&cifsi->lock_sem);
443 cifs_put_tlink(cifs_file->tlink);
444 dput(cifs_file->dentry);
445 cifs_sb_deactive(sb);
446 kfree(cifs_file->symlink_target);
450 static void cifsFileInfo_put_work(struct work_struct *work)
452 struct cifsFileInfo *cifs_file = container_of(work,
453 struct cifsFileInfo, put);
455 cifsFileInfo_put_final(cifs_file);
459 * cifsFileInfo_put - release a reference of file priv data
461 * Always potentially wait for oplock handler. See _cifsFileInfo_put().
463 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
465 void cifsFileInfo_put(struct cifsFileInfo *cifs_file)
467 _cifsFileInfo_put(cifs_file, true, true);
471 * _cifsFileInfo_put - release a reference of file priv data
473 * This may involve closing the filehandle @cifs_file out on the
474 * server. Must be called without holding tcon->open_file_lock,
475 * cinode->open_file_lock and cifs_file->file_info_lock.
477 * If @wait_for_oplock_handler is true and we are releasing the last
478 * reference, wait for any running oplock break handler of the file
479 * and cancel any pending one.
481 * @cifs_file: cifs/smb3 specific info (eg refcounts) for an open file
482 * @wait_oplock_handler: must be false if called from oplock_break_handler
483 * @offload: not offloaded on close and oplock breaks
486 void _cifsFileInfo_put(struct cifsFileInfo *cifs_file,
487 bool wait_oplock_handler, bool offload)
489 struct inode *inode = d_inode(cifs_file->dentry);
490 struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink);
491 struct TCP_Server_Info *server = tcon->ses->server;
492 struct cifsInodeInfo *cifsi = CIFS_I(inode);
493 struct super_block *sb = inode->i_sb;
494 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
495 struct cifs_fid fid = {};
496 struct cifs_pending_open open;
497 bool oplock_break_cancelled;
499 spin_lock(&tcon->open_file_lock);
500 spin_lock(&cifsi->open_file_lock);
501 spin_lock(&cifs_file->file_info_lock);
502 if (--cifs_file->count > 0) {
503 spin_unlock(&cifs_file->file_info_lock);
504 spin_unlock(&cifsi->open_file_lock);
505 spin_unlock(&tcon->open_file_lock);
508 spin_unlock(&cifs_file->file_info_lock);
510 if (server->ops->get_lease_key)
511 server->ops->get_lease_key(inode, &fid);
513 /* store open in pending opens to make sure we don't miss lease break */
514 cifs_add_pending_open_locked(&fid, cifs_file->tlink, &open);
516 /* remove it from the lists */
517 list_del(&cifs_file->flist);
518 list_del(&cifs_file->tlist);
519 atomic_dec(&tcon->num_local_opens);
521 if (list_empty(&cifsi->openFileList)) {
522 cifs_dbg(FYI, "closing last open instance for inode %p\n",
523 d_inode(cifs_file->dentry));
525 * In strict cache mode we need invalidate mapping on the last
526 * close because it may cause a error when we open this file
527 * again and get at least level II oplock.
529 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
530 set_bit(CIFS_INO_INVALID_MAPPING, &cifsi->flags);
531 cifs_set_oplock_level(cifsi, 0);
534 spin_unlock(&cifsi->open_file_lock);
535 spin_unlock(&tcon->open_file_lock);
537 oplock_break_cancelled = wait_oplock_handler ?
538 cancel_work_sync(&cifs_file->oplock_break) : false;
540 if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
541 struct TCP_Server_Info *server = tcon->ses->server;
545 if (server->ops->close_getattr)
546 server->ops->close_getattr(xid, tcon, cifs_file);
547 else if (server->ops->close)
548 server->ops->close(xid, tcon, &cifs_file->fid);
552 if (oplock_break_cancelled)
553 cifs_done_oplock_break(cifsi);
555 cifs_del_pending_open(&open);
558 queue_work(fileinfo_put_wq, &cifs_file->put);
560 cifsFileInfo_put_final(cifs_file);
563 int cifs_open(struct inode *inode, struct file *file)
569 struct cifs_sb_info *cifs_sb;
570 struct TCP_Server_Info *server;
571 struct cifs_tcon *tcon;
572 struct tcon_link *tlink;
573 struct cifsFileInfo *cfile = NULL;
575 const char *full_path;
576 bool posix_open_ok = false;
577 struct cifs_fid fid = {};
578 struct cifs_pending_open open;
579 struct cifs_open_info_data data = {};
583 cifs_sb = CIFS_SB(inode->i_sb);
584 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
589 tlink = cifs_sb_tlink(cifs_sb);
592 return PTR_ERR(tlink);
594 tcon = tlink_tcon(tlink);
595 server = tcon->ses->server;
597 page = alloc_dentry_path();
598 full_path = build_path_from_dentry(file_dentry(file), page);
599 if (IS_ERR(full_path)) {
600 rc = PTR_ERR(full_path);
604 cifs_dbg(FYI, "inode = 0x%p file flags are 0x%x for %s\n",
605 inode, file->f_flags, full_path);
607 if (file->f_flags & O_DIRECT &&
608 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
609 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
610 file->f_op = &cifs_file_direct_nobrl_ops;
612 file->f_op = &cifs_file_direct_ops;
615 /* Get the cached handle as SMB2 close is deferred */
616 rc = cifs_get_readable_path(tcon, full_path, &cfile);
618 if (file->f_flags == cfile->f_flags) {
619 file->private_data = cfile;
620 spin_lock(&CIFS_I(inode)->deferred_lock);
621 cifs_del_deferred_close(cfile);
622 spin_unlock(&CIFS_I(inode)->deferred_lock);
625 _cifsFileInfo_put(cfile, true, false);
634 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
635 if (!tcon->broken_posix_open && tcon->unix_ext &&
636 cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
637 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
638 /* can not refresh inode info since size could be stale */
639 rc = cifs_posix_open(full_path, &inode, inode->i_sb,
640 cifs_sb->ctx->file_mode /* ignored */,
641 file->f_flags, &oplock, &fid.netfid, xid);
643 cifs_dbg(FYI, "posix open succeeded\n");
644 posix_open_ok = true;
645 } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
646 if (tcon->ses->serverNOS)
647 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",
649 tcon->ses->serverNOS);
650 tcon->broken_posix_open = true;
651 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
652 (rc != -EOPNOTSUPP)) /* path not found or net err */
655 * Else fallthrough to retry open the old way on network i/o
659 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
661 if (server->ops->get_lease_key)
662 server->ops->get_lease_key(inode, &fid);
664 cifs_add_pending_open(&fid, tlink, &open);
666 if (!posix_open_ok) {
667 if (server->ops->get_lease_key)
668 server->ops->get_lease_key(inode, &fid);
670 rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, file->f_flags, &oplock, &fid,
673 cifs_del_pending_open(&open);
678 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, data.symlink_target);
680 if (server->ops->close)
681 server->ops->close(xid, tcon, &fid);
682 cifs_del_pending_open(&open);
687 cifs_fscache_set_inode_cookie(inode, file);
689 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
690 if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
692 * Time to set mode which we can not set earlier due to
693 * problems creating new read-only files.
695 struct cifs_unix_set_info_args args = {
696 .mode = inode->i_mode,
697 .uid = INVALID_UID, /* no change */
698 .gid = INVALID_GID, /* no change */
699 .ctime = NO_CHANGE_64,
700 .atime = NO_CHANGE_64,
701 .mtime = NO_CHANGE_64,
704 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid.netfid,
707 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
710 free_dentry_path(page);
712 cifs_put_tlink(tlink);
713 cifs_free_open_info(&data);
717 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
718 static int cifs_push_posix_locks(struct cifsFileInfo *cfile);
719 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
722 * Try to reacquire byte range locks that were released when session
723 * to server was lost.
726 cifs_relock_file(struct cifsFileInfo *cfile)
728 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
729 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
731 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
732 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
733 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
735 down_read_nested(&cinode->lock_sem, SINGLE_DEPTH_NESTING);
736 if (cinode->can_cache_brlcks) {
737 /* can cache locks - no need to relock */
738 up_read(&cinode->lock_sem);
742 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
743 if (cap_unix(tcon->ses) &&
744 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
745 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
746 rc = cifs_push_posix_locks(cfile);
748 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
749 rc = tcon->ses->server->ops->push_mand_locks(cfile);
751 up_read(&cinode->lock_sem);
756 cifs_reopen_file(struct cifsFileInfo *cfile, bool can_flush)
761 struct cifs_sb_info *cifs_sb;
762 struct cifs_tcon *tcon;
763 struct TCP_Server_Info *server;
764 struct cifsInodeInfo *cinode;
767 const char *full_path;
769 int disposition = FILE_OPEN;
770 int create_options = CREATE_NOT_DIR;
771 struct cifs_open_parms oparms;
774 mutex_lock(&cfile->fh_mutex);
775 if (!cfile->invalidHandle) {
776 mutex_unlock(&cfile->fh_mutex);
781 inode = d_inode(cfile->dentry);
782 cifs_sb = CIFS_SB(inode->i_sb);
783 tcon = tlink_tcon(cfile->tlink);
784 server = tcon->ses->server;
787 * Can not grab rename sem here because various ops, including those
788 * that already have the rename sem can end up causing writepage to get
789 * called and if the server was down that means we end up here, and we
790 * can never tell if the caller already has the rename_sem.
792 page = alloc_dentry_path();
793 full_path = build_path_from_dentry(cfile->dentry, page);
794 if (IS_ERR(full_path)) {
795 mutex_unlock(&cfile->fh_mutex);
796 free_dentry_path(page);
798 return PTR_ERR(full_path);
801 cifs_dbg(FYI, "inode = 0x%p file flags 0x%x for %s\n",
802 inode, cfile->f_flags, full_path);
804 if (tcon->ses->server->oplocks)
809 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
810 if (tcon->unix_ext && cap_unix(tcon->ses) &&
811 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
812 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
814 * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
815 * original open. Must mask them off for a reopen.
817 unsigned int oflags = cfile->f_flags &
818 ~(O_CREAT | O_EXCL | O_TRUNC);
820 rc = cifs_posix_open(full_path, NULL, inode->i_sb,
821 cifs_sb->ctx->file_mode /* ignored */,
822 oflags, &oplock, &cfile->fid.netfid, xid);
824 cifs_dbg(FYI, "posix reopen succeeded\n");
825 oparms.reconnect = true;
829 * fallthrough to retry open the old way on errors, especially
830 * in the reconnect path it is important to retry hard
833 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
835 desired_access = cifs_convert_flags(cfile->f_flags);
837 /* O_SYNC also has bit for O_DSYNC so following check picks up either */
838 if (cfile->f_flags & O_SYNC)
839 create_options |= CREATE_WRITE_THROUGH;
841 if (cfile->f_flags & O_DIRECT)
842 create_options |= CREATE_NO_BUFFER;
844 if (server->ops->get_lease_key)
845 server->ops->get_lease_key(inode, &cfile->fid);
847 oparms = (struct cifs_open_parms) {
850 .desired_access = desired_access,
851 .create_options = cifs_create_options(cifs_sb, create_options),
852 .disposition = disposition,
859 * Can not refresh inode by passing in file_info buf to be returned by
860 * ops->open and then calling get_inode_info with returned buf since
861 * file might have write behind data that needs to be flushed and server
862 * version of file size can be stale. If we knew for sure that inode was
863 * not dirty locally we could do this.
865 rc = server->ops->open(xid, &oparms, &oplock, NULL);
866 if (rc == -ENOENT && oparms.reconnect == false) {
867 /* durable handle timeout is expired - open the file again */
868 rc = server->ops->open(xid, &oparms, &oplock, NULL);
869 /* indicate that we need to relock the file */
870 oparms.reconnect = true;
874 mutex_unlock(&cfile->fh_mutex);
875 cifs_dbg(FYI, "cifs_reopen returned 0x%x\n", rc);
876 cifs_dbg(FYI, "oplock: %d\n", oplock);
877 goto reopen_error_exit;
880 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
882 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
883 cfile->invalidHandle = false;
884 mutex_unlock(&cfile->fh_mutex);
885 cinode = CIFS_I(inode);
888 rc = filemap_write_and_wait(inode->i_mapping);
889 if (!is_interrupt_error(rc))
890 mapping_set_error(inode->i_mapping, rc);
892 if (tcon->posix_extensions)
893 rc = smb311_posix_get_inode_info(&inode, full_path, inode->i_sb, xid);
894 else if (tcon->unix_ext)
895 rc = cifs_get_inode_info_unix(&inode, full_path,
898 rc = cifs_get_inode_info(&inode, full_path, NULL,
899 inode->i_sb, xid, NULL);
902 * Else we are writing out data to server already and could deadlock if
903 * we tried to flush data, and since we do not know if we have data that
904 * would invalidate the current end of file on the server we can not go
905 * to the server to get the new inode info.
909 * If the server returned a read oplock and we have mandatory brlocks,
910 * set oplock level to None.
912 if (server->ops->is_read_op(oplock) && cifs_has_mand_locks(cinode)) {
913 cifs_dbg(FYI, "Reset oplock val from read to None due to mand locks\n");
917 server->ops->set_fid(cfile, &cfile->fid, oplock);
918 if (oparms.reconnect)
919 cifs_relock_file(cfile);
922 free_dentry_path(page);
927 void smb2_deferred_work_close(struct work_struct *work)
929 struct cifsFileInfo *cfile = container_of(work,
930 struct cifsFileInfo, deferred.work);
932 spin_lock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
933 cifs_del_deferred_close(cfile);
934 cfile->deferred_close_scheduled = false;
935 spin_unlock(&CIFS_I(d_inode(cfile->dentry))->deferred_lock);
936 _cifsFileInfo_put(cfile, true, false);
939 int cifs_close(struct inode *inode, struct file *file)
941 struct cifsFileInfo *cfile;
942 struct cifsInodeInfo *cinode = CIFS_I(inode);
943 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
944 struct cifs_deferred_close *dclose;
946 if (file->private_data != NULL) {
947 cfile = file->private_data;
948 file->private_data = NULL;
949 dclose = kmalloc(sizeof(struct cifs_deferred_close), GFP_KERNEL);
950 if ((cifs_sb->ctx->closetimeo && cinode->oplock == CIFS_CACHE_RHW_FLG)
951 && cinode->lease_granted &&
952 !test_bit(CIFS_INO_CLOSE_ON_LOCK, &cinode->flags) &&
954 if (test_and_clear_bit(CIFS_INO_MODIFIED_ATTR, &cinode->flags)) {
955 inode->i_ctime = inode->i_mtime = current_time(inode);
956 cifs_fscache_update_inode_cookie(inode);
958 spin_lock(&cinode->deferred_lock);
959 cifs_add_deferred_close(cfile, dclose);
960 if (cfile->deferred_close_scheduled &&
961 delayed_work_pending(&cfile->deferred)) {
963 * If there is no pending work, mod_delayed_work queues new work.
964 * So, Increase the ref count to avoid use-after-free.
966 if (!mod_delayed_work(deferredclose_wq,
967 &cfile->deferred, cifs_sb->ctx->closetimeo))
968 cifsFileInfo_get(cfile);
970 /* Deferred close for files */
971 queue_delayed_work(deferredclose_wq,
972 &cfile->deferred, cifs_sb->ctx->closetimeo);
973 cfile->deferred_close_scheduled = true;
974 spin_unlock(&cinode->deferred_lock);
977 spin_unlock(&cinode->deferred_lock);
978 _cifsFileInfo_put(cfile, true, false);
980 _cifsFileInfo_put(cfile, true, false);
985 /* return code from the ->release op is always ignored */
990 cifs_reopen_persistent_handles(struct cifs_tcon *tcon)
992 struct cifsFileInfo *open_file, *tmp;
993 struct list_head tmp_list;
995 if (!tcon->use_persistent || !tcon->need_reopen_files)
998 tcon->need_reopen_files = false;
1000 cifs_dbg(FYI, "Reopen persistent handles\n");
1001 INIT_LIST_HEAD(&tmp_list);
1003 /* list all files open on tree connection, reopen resilient handles */
1004 spin_lock(&tcon->open_file_lock);
1005 list_for_each_entry(open_file, &tcon->openFileList, tlist) {
1006 if (!open_file->invalidHandle)
1008 cifsFileInfo_get(open_file);
1009 list_add_tail(&open_file->rlist, &tmp_list);
1011 spin_unlock(&tcon->open_file_lock);
1013 list_for_each_entry_safe(open_file, tmp, &tmp_list, rlist) {
1014 if (cifs_reopen_file(open_file, false /* do not flush */))
1015 tcon->need_reopen_files = true;
1016 list_del_init(&open_file->rlist);
1017 cifsFileInfo_put(open_file);
1021 int cifs_closedir(struct inode *inode, struct file *file)
1025 struct cifsFileInfo *cfile = file->private_data;
1026 struct cifs_tcon *tcon;
1027 struct TCP_Server_Info *server;
1030 cifs_dbg(FYI, "Closedir inode = 0x%p\n", inode);
1036 tcon = tlink_tcon(cfile->tlink);
1037 server = tcon->ses->server;
1039 cifs_dbg(FYI, "Freeing private data in close dir\n");
1040 spin_lock(&cfile->file_info_lock);
1041 if (server->ops->dir_needs_close(cfile)) {
1042 cfile->invalidHandle = true;
1043 spin_unlock(&cfile->file_info_lock);
1044 if (server->ops->close_dir)
1045 rc = server->ops->close_dir(xid, tcon, &cfile->fid);
1048 cifs_dbg(FYI, "Closing uncompleted readdir with rc %d\n", rc);
1049 /* not much we can do if it fails anyway, ignore rc */
1052 spin_unlock(&cfile->file_info_lock);
1054 buf = cfile->srch_inf.ntwrk_buf_start;
1056 cifs_dbg(FYI, "closedir free smb buf in srch struct\n");
1057 cfile->srch_inf.ntwrk_buf_start = NULL;
1058 if (cfile->srch_inf.smallBuf)
1059 cifs_small_buf_release(buf);
1061 cifs_buf_release(buf);
1064 cifs_put_tlink(cfile->tlink);
1065 kfree(file->private_data);
1066 file->private_data = NULL;
1067 /* BB can we lock the filestruct while this is going on? */
1072 static struct cifsLockInfo *
1073 cifs_lock_init(__u64 offset, __u64 length, __u8 type, __u16 flags)
1075 struct cifsLockInfo *lock =
1076 kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
1079 lock->offset = offset;
1080 lock->length = length;
1082 lock->pid = current->tgid;
1083 lock->flags = flags;
1084 INIT_LIST_HEAD(&lock->blist);
1085 init_waitqueue_head(&lock->block_q);
1090 cifs_del_lock_waiters(struct cifsLockInfo *lock)
1092 struct cifsLockInfo *li, *tmp;
1093 list_for_each_entry_safe(li, tmp, &lock->blist, blist) {
1094 list_del_init(&li->blist);
1095 wake_up(&li->block_q);
1099 #define CIFS_LOCK_OP 0
1100 #define CIFS_READ_OP 1
1101 #define CIFS_WRITE_OP 2
1103 /* @rw_check : 0 - no op, 1 - read, 2 - write */
1105 cifs_find_fid_lock_conflict(struct cifs_fid_locks *fdlocks, __u64 offset,
1106 __u64 length, __u8 type, __u16 flags,
1107 struct cifsFileInfo *cfile,
1108 struct cifsLockInfo **conf_lock, int rw_check)
1110 struct cifsLockInfo *li;
1111 struct cifsFileInfo *cur_cfile = fdlocks->cfile;
1112 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1114 list_for_each_entry(li, &fdlocks->locks, llist) {
1115 if (offset + length <= li->offset ||
1116 offset >= li->offset + li->length)
1118 if (rw_check != CIFS_LOCK_OP && current->tgid == li->pid &&
1119 server->ops->compare_fids(cfile, cur_cfile)) {
1120 /* shared lock prevents write op through the same fid */
1121 if (!(li->type & server->vals->shared_lock_type) ||
1122 rw_check != CIFS_WRITE_OP)
1125 if ((type & server->vals->shared_lock_type) &&
1126 ((server->ops->compare_fids(cfile, cur_cfile) &&
1127 current->tgid == li->pid) || type == li->type))
1129 if (rw_check == CIFS_LOCK_OP &&
1130 (flags & FL_OFDLCK) && (li->flags & FL_OFDLCK) &&
1131 server->ops->compare_fids(cfile, cur_cfile))
1141 cifs_find_lock_conflict(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1142 __u8 type, __u16 flags,
1143 struct cifsLockInfo **conf_lock, int rw_check)
1146 struct cifs_fid_locks *cur;
1147 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1149 list_for_each_entry(cur, &cinode->llist, llist) {
1150 rc = cifs_find_fid_lock_conflict(cur, offset, length, type,
1151 flags, cfile, conf_lock,
1161 * Check if there is another lock that prevents us to set the lock (mandatory
1162 * style). If such a lock exists, update the flock structure with its
1163 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1164 * or leave it the same if we can't. Returns 0 if we don't need to request to
1165 * the server or 1 otherwise.
1168 cifs_lock_test(struct cifsFileInfo *cfile, __u64 offset, __u64 length,
1169 __u8 type, struct file_lock *flock)
1172 struct cifsLockInfo *conf_lock;
1173 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1174 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1177 down_read(&cinode->lock_sem);
1179 exist = cifs_find_lock_conflict(cfile, offset, length, type,
1180 flock->fl_flags, &conf_lock,
1183 flock->fl_start = conf_lock->offset;
1184 flock->fl_end = conf_lock->offset + conf_lock->length - 1;
1185 flock->fl_pid = conf_lock->pid;
1186 if (conf_lock->type & server->vals->shared_lock_type)
1187 flock->fl_type = F_RDLCK;
1189 flock->fl_type = F_WRLCK;
1190 } else if (!cinode->can_cache_brlcks)
1193 flock->fl_type = F_UNLCK;
1195 up_read(&cinode->lock_sem);
1200 cifs_lock_add(struct cifsFileInfo *cfile, struct cifsLockInfo *lock)
1202 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1203 cifs_down_write(&cinode->lock_sem);
1204 list_add_tail(&lock->llist, &cfile->llist->locks);
1205 up_write(&cinode->lock_sem);
1209 * Set the byte-range lock (mandatory style). Returns:
1210 * 1) 0, if we set the lock and don't need to request to the server;
1211 * 2) 1, if no locks prevent us but we need to request to the server;
1212 * 3) -EACCES, if there is a lock that prevents us and wait is false.
1215 cifs_lock_add_if(struct cifsFileInfo *cfile, struct cifsLockInfo *lock,
1218 struct cifsLockInfo *conf_lock;
1219 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1225 cifs_down_write(&cinode->lock_sem);
1227 exist = cifs_find_lock_conflict(cfile, lock->offset, lock->length,
1228 lock->type, lock->flags, &conf_lock,
1230 if (!exist && cinode->can_cache_brlcks) {
1231 list_add_tail(&lock->llist, &cfile->llist->locks);
1232 up_write(&cinode->lock_sem);
1241 list_add_tail(&lock->blist, &conf_lock->blist);
1242 up_write(&cinode->lock_sem);
1243 rc = wait_event_interruptible(lock->block_q,
1244 (lock->blist.prev == &lock->blist) &&
1245 (lock->blist.next == &lock->blist));
1248 cifs_down_write(&cinode->lock_sem);
1249 list_del_init(&lock->blist);
1252 up_write(&cinode->lock_sem);
1256 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1258 * Check if there is another lock that prevents us to set the lock (posix
1259 * style). If such a lock exists, update the flock structure with its
1260 * properties. Otherwise, set the flock type to F_UNLCK if we can cache brlocks
1261 * or leave it the same if we can't. Returns 0 if we don't need to request to
1262 * the server or 1 otherwise.
1265 cifs_posix_lock_test(struct file *file, struct file_lock *flock)
1268 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1269 unsigned char saved_type = flock->fl_type;
1271 if ((flock->fl_flags & FL_POSIX) == 0)
1274 down_read(&cinode->lock_sem);
1275 posix_test_lock(file, flock);
1277 if (flock->fl_type == F_UNLCK && !cinode->can_cache_brlcks) {
1278 flock->fl_type = saved_type;
1282 up_read(&cinode->lock_sem);
1287 * Set the byte-range lock (posix style). Returns:
1288 * 1) <0, if the error occurs while setting the lock;
1289 * 2) 0, if we set the lock and don't need to request to the server;
1290 * 3) FILE_LOCK_DEFERRED, if we will wait for some other file_lock;
1291 * 4) FILE_LOCK_DEFERRED + 1, if we need to request to the server.
1294 cifs_posix_lock_set(struct file *file, struct file_lock *flock)
1296 struct cifsInodeInfo *cinode = CIFS_I(file_inode(file));
1297 int rc = FILE_LOCK_DEFERRED + 1;
1299 if ((flock->fl_flags & FL_POSIX) == 0)
1302 cifs_down_write(&cinode->lock_sem);
1303 if (!cinode->can_cache_brlcks) {
1304 up_write(&cinode->lock_sem);
1308 rc = posix_lock_file(file, flock, NULL);
1309 up_write(&cinode->lock_sem);
1314 cifs_push_mandatory_locks(struct cifsFileInfo *cfile)
1317 int rc = 0, stored_rc;
1318 struct cifsLockInfo *li, *tmp;
1319 struct cifs_tcon *tcon;
1320 unsigned int num, max_num, max_buf;
1321 LOCKING_ANDX_RANGE *buf, *cur;
1322 static const int types[] = {
1323 LOCKING_ANDX_LARGE_FILES,
1324 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1329 tcon = tlink_tcon(cfile->tlink);
1332 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1333 * and check it before using.
1335 max_buf = tcon->ses->server->maxBuf;
1336 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE))) {
1341 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1343 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1345 max_num = (max_buf - sizeof(struct smb_hdr)) /
1346 sizeof(LOCKING_ANDX_RANGE);
1347 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1353 for (i = 0; i < 2; i++) {
1356 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1357 if (li->type != types[i])
1359 cur->Pid = cpu_to_le16(li->pid);
1360 cur->LengthLow = cpu_to_le32((u32)li->length);
1361 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1362 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1363 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1364 if (++num == max_num) {
1365 stored_rc = cifs_lockv(xid, tcon,
1367 (__u8)li->type, 0, num,
1378 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1379 (__u8)types[i], 0, num, buf);
1391 hash_lockowner(fl_owner_t owner)
1393 return cifs_lock_secret ^ hash32_ptr((const void *)owner);
1395 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1397 struct lock_to_push {
1398 struct list_head llist;
1406 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1408 cifs_push_posix_locks(struct cifsFileInfo *cfile)
1410 struct inode *inode = d_inode(cfile->dentry);
1411 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1412 struct file_lock *flock;
1413 struct file_lock_context *flctx = inode->i_flctx;
1414 unsigned int count = 0, i;
1415 int rc = 0, xid, type;
1416 struct list_head locks_to_send, *el;
1417 struct lock_to_push *lck, *tmp;
1425 spin_lock(&flctx->flc_lock);
1426 list_for_each(el, &flctx->flc_posix) {
1429 spin_unlock(&flctx->flc_lock);
1431 INIT_LIST_HEAD(&locks_to_send);
1434 * Allocating count locks is enough because no FL_POSIX locks can be
1435 * added to the list while we are holding cinode->lock_sem that
1436 * protects locking operations of this inode.
1438 for (i = 0; i < count; i++) {
1439 lck = kmalloc(sizeof(struct lock_to_push), GFP_KERNEL);
1444 list_add_tail(&lck->llist, &locks_to_send);
1447 el = locks_to_send.next;
1448 spin_lock(&flctx->flc_lock);
1449 list_for_each_entry(flock, &flctx->flc_posix, fl_list) {
1450 if (el == &locks_to_send) {
1452 * The list ended. We don't have enough allocated
1453 * structures - something is really wrong.
1455 cifs_dbg(VFS, "Can't push all brlocks!\n");
1458 length = cifs_flock_len(flock);
1459 if (flock->fl_type == F_RDLCK || flock->fl_type == F_SHLCK)
1463 lck = list_entry(el, struct lock_to_push, llist);
1464 lck->pid = hash_lockowner(flock->fl_owner);
1465 lck->netfid = cfile->fid.netfid;
1466 lck->length = length;
1468 lck->offset = flock->fl_start;
1470 spin_unlock(&flctx->flc_lock);
1472 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1475 stored_rc = CIFSSMBPosixLock(xid, tcon, lck->netfid, lck->pid,
1476 lck->offset, lck->length, NULL,
1480 list_del(&lck->llist);
1488 list_for_each_entry_safe(lck, tmp, &locks_to_send, llist) {
1489 list_del(&lck->llist);
1494 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1497 cifs_push_locks(struct cifsFileInfo *cfile)
1499 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1500 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1502 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1503 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
1504 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1506 /* we are going to update can_cache_brlcks here - need a write access */
1507 cifs_down_write(&cinode->lock_sem);
1508 if (!cinode->can_cache_brlcks) {
1509 up_write(&cinode->lock_sem);
1513 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1514 if (cap_unix(tcon->ses) &&
1515 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1516 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1517 rc = cifs_push_posix_locks(cfile);
1519 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1520 rc = tcon->ses->server->ops->push_mand_locks(cfile);
1522 cinode->can_cache_brlcks = false;
1523 up_write(&cinode->lock_sem);
1528 cifs_read_flock(struct file_lock *flock, __u32 *type, int *lock, int *unlock,
1529 bool *wait_flag, struct TCP_Server_Info *server)
1531 if (flock->fl_flags & FL_POSIX)
1532 cifs_dbg(FYI, "Posix\n");
1533 if (flock->fl_flags & FL_FLOCK)
1534 cifs_dbg(FYI, "Flock\n");
1535 if (flock->fl_flags & FL_SLEEP) {
1536 cifs_dbg(FYI, "Blocking lock\n");
1539 if (flock->fl_flags & FL_ACCESS)
1540 cifs_dbg(FYI, "Process suspended by mandatory locking - not implemented yet\n");
1541 if (flock->fl_flags & FL_LEASE)
1542 cifs_dbg(FYI, "Lease on file - not implemented yet\n");
1543 if (flock->fl_flags &
1544 (~(FL_POSIX | FL_FLOCK | FL_SLEEP |
1545 FL_ACCESS | FL_LEASE | FL_CLOSE | FL_OFDLCK)))
1546 cifs_dbg(FYI, "Unknown lock flags 0x%x\n", flock->fl_flags);
1548 *type = server->vals->large_lock_type;
1549 if (flock->fl_type == F_WRLCK) {
1550 cifs_dbg(FYI, "F_WRLCK\n");
1551 *type |= server->vals->exclusive_lock_type;
1553 } else if (flock->fl_type == F_UNLCK) {
1554 cifs_dbg(FYI, "F_UNLCK\n");
1555 *type |= server->vals->unlock_lock_type;
1557 /* Check if unlock includes more than one lock range */
1558 } else if (flock->fl_type == F_RDLCK) {
1559 cifs_dbg(FYI, "F_RDLCK\n");
1560 *type |= server->vals->shared_lock_type;
1562 } else if (flock->fl_type == F_EXLCK) {
1563 cifs_dbg(FYI, "F_EXLCK\n");
1564 *type |= server->vals->exclusive_lock_type;
1566 } else if (flock->fl_type == F_SHLCK) {
1567 cifs_dbg(FYI, "F_SHLCK\n");
1568 *type |= server->vals->shared_lock_type;
1571 cifs_dbg(FYI, "Unknown type of lock\n");
1575 cifs_getlk(struct file *file, struct file_lock *flock, __u32 type,
1576 bool wait_flag, bool posix_lck, unsigned int xid)
1579 __u64 length = cifs_flock_len(flock);
1580 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1581 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1582 struct TCP_Server_Info *server = tcon->ses->server;
1583 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1584 __u16 netfid = cfile->fid.netfid;
1587 int posix_lock_type;
1589 rc = cifs_posix_lock_test(file, flock);
1593 if (type & server->vals->shared_lock_type)
1594 posix_lock_type = CIFS_RDLCK;
1596 posix_lock_type = CIFS_WRLCK;
1597 rc = CIFSSMBPosixLock(xid, tcon, netfid,
1598 hash_lockowner(flock->fl_owner),
1599 flock->fl_start, length, flock,
1600 posix_lock_type, wait_flag);
1603 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1605 rc = cifs_lock_test(cfile, flock->fl_start, length, type, flock);
1609 /* BB we could chain these into one lock request BB */
1610 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length, type,
1613 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1615 flock->fl_type = F_UNLCK;
1617 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1622 if (type & server->vals->shared_lock_type) {
1623 flock->fl_type = F_WRLCK;
1627 type &= ~server->vals->exclusive_lock_type;
1629 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1630 type | server->vals->shared_lock_type,
1633 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1634 type | server->vals->shared_lock_type, 0, 1, false);
1635 flock->fl_type = F_RDLCK;
1637 cifs_dbg(VFS, "Error unlocking previously locked range %d during test of lock\n",
1640 flock->fl_type = F_WRLCK;
1646 cifs_move_llist(struct list_head *source, struct list_head *dest)
1648 struct list_head *li, *tmp;
1649 list_for_each_safe(li, tmp, source)
1650 list_move(li, dest);
1654 cifs_free_llist(struct list_head *llist)
1656 struct cifsLockInfo *li, *tmp;
1657 list_for_each_entry_safe(li, tmp, llist, llist) {
1658 cifs_del_lock_waiters(li);
1659 list_del(&li->llist);
1664 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1666 cifs_unlock_range(struct cifsFileInfo *cfile, struct file_lock *flock,
1669 int rc = 0, stored_rc;
1670 static const int types[] = {
1671 LOCKING_ANDX_LARGE_FILES,
1672 LOCKING_ANDX_SHARED_LOCK | LOCKING_ANDX_LARGE_FILES
1675 unsigned int max_num, num, max_buf;
1676 LOCKING_ANDX_RANGE *buf, *cur;
1677 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1678 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1679 struct cifsLockInfo *li, *tmp;
1680 __u64 length = cifs_flock_len(flock);
1681 struct list_head tmp_llist;
1683 INIT_LIST_HEAD(&tmp_llist);
1686 * Accessing maxBuf is racy with cifs_reconnect - need to store value
1687 * and check it before using.
1689 max_buf = tcon->ses->server->maxBuf;
1690 if (max_buf < (sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE)))
1693 BUILD_BUG_ON(sizeof(struct smb_hdr) + sizeof(LOCKING_ANDX_RANGE) >
1695 max_buf = min_t(unsigned int, max_buf - sizeof(struct smb_hdr),
1697 max_num = (max_buf - sizeof(struct smb_hdr)) /
1698 sizeof(LOCKING_ANDX_RANGE);
1699 buf = kcalloc(max_num, sizeof(LOCKING_ANDX_RANGE), GFP_KERNEL);
1703 cifs_down_write(&cinode->lock_sem);
1704 for (i = 0; i < 2; i++) {
1707 list_for_each_entry_safe(li, tmp, &cfile->llist->locks, llist) {
1708 if (flock->fl_start > li->offset ||
1709 (flock->fl_start + length) <
1710 (li->offset + li->length))
1712 if (current->tgid != li->pid)
1714 if (types[i] != li->type)
1716 if (cinode->can_cache_brlcks) {
1718 * We can cache brlock requests - simply remove
1719 * a lock from the file's list.
1721 list_del(&li->llist);
1722 cifs_del_lock_waiters(li);
1726 cur->Pid = cpu_to_le16(li->pid);
1727 cur->LengthLow = cpu_to_le32((u32)li->length);
1728 cur->LengthHigh = cpu_to_le32((u32)(li->length>>32));
1729 cur->OffsetLow = cpu_to_le32((u32)li->offset);
1730 cur->OffsetHigh = cpu_to_le32((u32)(li->offset>>32));
1732 * We need to save a lock here to let us add it again to
1733 * the file's list if the unlock range request fails on
1736 list_move(&li->llist, &tmp_llist);
1737 if (++num == max_num) {
1738 stored_rc = cifs_lockv(xid, tcon,
1740 li->type, num, 0, buf);
1743 * We failed on the unlock range
1744 * request - add all locks from the tmp
1745 * list to the head of the file's list.
1747 cifs_move_llist(&tmp_llist,
1748 &cfile->llist->locks);
1752 * The unlock range request succeed -
1753 * free the tmp list.
1755 cifs_free_llist(&tmp_llist);
1762 stored_rc = cifs_lockv(xid, tcon, cfile->fid.netfid,
1763 types[i], num, 0, buf);
1765 cifs_move_llist(&tmp_llist,
1766 &cfile->llist->locks);
1769 cifs_free_llist(&tmp_llist);
1773 up_write(&cinode->lock_sem);
1777 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1780 cifs_setlk(struct file *file, struct file_lock *flock, __u32 type,
1781 bool wait_flag, bool posix_lck, int lock, int unlock,
1785 __u64 length = cifs_flock_len(flock);
1786 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
1787 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
1788 struct TCP_Server_Info *server = tcon->ses->server;
1789 struct inode *inode = d_inode(cfile->dentry);
1791 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1793 int posix_lock_type;
1795 rc = cifs_posix_lock_set(file, flock);
1796 if (rc <= FILE_LOCK_DEFERRED)
1799 if (type & server->vals->shared_lock_type)
1800 posix_lock_type = CIFS_RDLCK;
1802 posix_lock_type = CIFS_WRLCK;
1805 posix_lock_type = CIFS_UNLCK;
1807 rc = CIFSSMBPosixLock(xid, tcon, cfile->fid.netfid,
1808 hash_lockowner(flock->fl_owner),
1809 flock->fl_start, length,
1810 NULL, posix_lock_type, wait_flag);
1813 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1815 struct cifsLockInfo *lock;
1817 lock = cifs_lock_init(flock->fl_start, length, type,
1822 rc = cifs_lock_add_if(cfile, lock, wait_flag);
1831 * Windows 7 server can delay breaking lease from read to None
1832 * if we set a byte-range lock on a file - break it explicitly
1833 * before sending the lock to the server to be sure the next
1834 * read won't conflict with non-overlapted locks due to
1837 if (!CIFS_CACHE_WRITE(CIFS_I(inode)) &&
1838 CIFS_CACHE_READ(CIFS_I(inode))) {
1839 cifs_zap_mapping(inode);
1840 cifs_dbg(FYI, "Set no oplock for inode=%p due to mand locks\n",
1842 CIFS_I(inode)->oplock = 0;
1845 rc = server->ops->mand_lock(xid, cfile, flock->fl_start, length,
1846 type, 1, 0, wait_flag);
1852 cifs_lock_add(cfile, lock);
1854 rc = server->ops->mand_unlock_range(cfile, flock, xid);
1857 if ((flock->fl_flags & FL_POSIX) || (flock->fl_flags & FL_FLOCK)) {
1859 * If this is a request to remove all locks because we
1860 * are closing the file, it doesn't matter if the
1861 * unlocking failed as both cifs.ko and the SMB server
1862 * remove the lock on file close
1865 cifs_dbg(VFS, "%s failed rc=%d\n", __func__, rc);
1866 if (!(flock->fl_flags & FL_CLOSE))
1869 rc = locks_lock_file_wait(file, flock);
1874 int cifs_flock(struct file *file, int cmd, struct file_lock *fl)
1877 int lock = 0, unlock = 0;
1878 bool wait_flag = false;
1879 bool posix_lck = false;
1880 struct cifs_sb_info *cifs_sb;
1881 struct cifs_tcon *tcon;
1882 struct cifsFileInfo *cfile;
1887 if (!(fl->fl_flags & FL_FLOCK)) {
1893 cfile = (struct cifsFileInfo *)file->private_data;
1894 tcon = tlink_tcon(cfile->tlink);
1896 cifs_read_flock(fl, &type, &lock, &unlock, &wait_flag,
1898 cifs_sb = CIFS_FILE_SB(file);
1900 if (cap_unix(tcon->ses) &&
1901 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1902 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1905 if (!lock && !unlock) {
1907 * if no lock or unlock then nothing to do since we do not
1915 rc = cifs_setlk(file, fl, type, wait_flag, posix_lck, lock, unlock,
1923 int cifs_lock(struct file *file, int cmd, struct file_lock *flock)
1926 int lock = 0, unlock = 0;
1927 bool wait_flag = false;
1928 bool posix_lck = false;
1929 struct cifs_sb_info *cifs_sb;
1930 struct cifs_tcon *tcon;
1931 struct cifsFileInfo *cfile;
1937 cifs_dbg(FYI, "%s: %pD2 cmd=0x%x type=0x%x flags=0x%x r=%lld:%lld\n", __func__, file, cmd,
1938 flock->fl_flags, flock->fl_type, (long long)flock->fl_start,
1939 (long long)flock->fl_end);
1941 cfile = (struct cifsFileInfo *)file->private_data;
1942 tcon = tlink_tcon(cfile->tlink);
1944 cifs_read_flock(flock, &type, &lock, &unlock, &wait_flag,
1946 cifs_sb = CIFS_FILE_SB(file);
1947 set_bit(CIFS_INO_CLOSE_ON_LOCK, &CIFS_I(d_inode(cfile->dentry))->flags);
1949 if (cap_unix(tcon->ses) &&
1950 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
1951 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
1954 * BB add code here to normalize offset and length to account for
1955 * negative length which we can not accept over the wire.
1957 if (IS_GETLK(cmd)) {
1958 rc = cifs_getlk(file, flock, type, wait_flag, posix_lck, xid);
1963 if (!lock && !unlock) {
1965 * if no lock or unlock then nothing to do since we do not
1972 rc = cifs_setlk(file, flock, type, wait_flag, posix_lck, lock, unlock,
1979 * update the file size (if needed) after a write. Should be called with
1980 * the inode->i_lock held
1983 cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
1984 unsigned int bytes_written)
1986 loff_t end_of_write = offset + bytes_written;
1988 if (end_of_write > cifsi->server_eof)
1989 cifsi->server_eof = end_of_write;
1993 cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
1994 size_t write_size, loff_t *offset)
1997 unsigned int bytes_written = 0;
1998 unsigned int total_written;
1999 struct cifs_tcon *tcon;
2000 struct TCP_Server_Info *server;
2002 struct dentry *dentry = open_file->dentry;
2003 struct cifsInodeInfo *cifsi = CIFS_I(d_inode(dentry));
2004 struct cifs_io_parms io_parms = {0};
2006 cifs_dbg(FYI, "write %zd bytes to offset %lld of %pd\n",
2007 write_size, *offset, dentry);
2009 tcon = tlink_tcon(open_file->tlink);
2010 server = tcon->ses->server;
2012 if (!server->ops->sync_write)
2017 for (total_written = 0; write_size > total_written;
2018 total_written += bytes_written) {
2020 while (rc == -EAGAIN) {
2024 if (open_file->invalidHandle) {
2025 /* we could deadlock if we called
2026 filemap_fdatawait from here so tell
2027 reopen_file not to flush data to
2029 rc = cifs_reopen_file(open_file, false);
2034 len = min(server->ops->wp_retry_size(d_inode(dentry)),
2035 (unsigned int)write_size - total_written);
2036 /* iov[0] is reserved for smb header */
2037 iov[1].iov_base = (char *)write_data + total_written;
2038 iov[1].iov_len = len;
2040 io_parms.tcon = tcon;
2041 io_parms.offset = *offset;
2042 io_parms.length = len;
2043 rc = server->ops->sync_write(xid, &open_file->fid,
2044 &io_parms, &bytes_written, iov, 1);
2046 if (rc || (bytes_written == 0)) {
2054 spin_lock(&d_inode(dentry)->i_lock);
2055 cifs_update_eof(cifsi, *offset, bytes_written);
2056 spin_unlock(&d_inode(dentry)->i_lock);
2057 *offset += bytes_written;
2061 cifs_stats_bytes_written(tcon, total_written);
2063 if (total_written > 0) {
2064 spin_lock(&d_inode(dentry)->i_lock);
2065 if (*offset > d_inode(dentry)->i_size) {
2066 i_size_write(d_inode(dentry), *offset);
2067 d_inode(dentry)->i_blocks = (512 - 1 + *offset) >> 9;
2069 spin_unlock(&d_inode(dentry)->i_lock);
2071 mark_inode_dirty_sync(d_inode(dentry));
2073 return total_written;
2076 struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
2079 struct cifsFileInfo *open_file = NULL;
2080 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
2082 /* only filter by fsuid on multiuser mounts */
2083 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2086 spin_lock(&cifs_inode->open_file_lock);
2087 /* we could simply get the first_list_entry since write-only entries
2088 are always at the end of the list but since the first entry might
2089 have a close pending, we go through the whole list */
2090 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2091 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2093 if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
2094 if ((!open_file->invalidHandle)) {
2095 /* found a good file */
2096 /* lock it so it will not be closed on us */
2097 cifsFileInfo_get(open_file);
2098 spin_unlock(&cifs_inode->open_file_lock);
2100 } /* else might as well continue, and look for
2101 another, or simply have the caller reopen it
2102 again rather than trying to fix this handle */
2103 } else /* write only file */
2104 break; /* write only files are last so must be done */
2106 spin_unlock(&cifs_inode->open_file_lock);
2110 /* Return -EBADF if no handle is found and general rc otherwise */
2112 cifs_get_writable_file(struct cifsInodeInfo *cifs_inode, int flags,
2113 struct cifsFileInfo **ret_file)
2115 struct cifsFileInfo *open_file, *inv_file = NULL;
2116 struct cifs_sb_info *cifs_sb;
2117 bool any_available = false;
2119 unsigned int refind = 0;
2120 bool fsuid_only = flags & FIND_WR_FSUID_ONLY;
2121 bool with_delete = flags & FIND_WR_WITH_DELETE;
2125 * Having a null inode here (because mapping->host was set to zero by
2126 * the VFS or MM) should not happen but we had reports of on oops (due
2127 * to it being zero) during stress testcases so we need to check for it
2130 if (cifs_inode == NULL) {
2131 cifs_dbg(VFS, "Null inode passed to cifs_writeable_file\n");
2136 cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
2138 /* only filter by fsuid on multiuser mounts */
2139 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
2142 spin_lock(&cifs_inode->open_file_lock);
2144 if (refind > MAX_REOPEN_ATT) {
2145 spin_unlock(&cifs_inode->open_file_lock);
2148 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
2149 if (!any_available && open_file->pid != current->tgid)
2151 if (fsuid_only && !uid_eq(open_file->uid, current_fsuid()))
2153 if (with_delete && !(open_file->fid.access & DELETE))
2155 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
2156 if (!open_file->invalidHandle) {
2157 /* found a good writable file */
2158 cifsFileInfo_get(open_file);
2159 spin_unlock(&cifs_inode->open_file_lock);
2160 *ret_file = open_file;
2164 inv_file = open_file;
2168 /* couldn't find useable FH with same pid, try any available */
2169 if (!any_available) {
2170 any_available = true;
2171 goto refind_writable;
2175 any_available = false;
2176 cifsFileInfo_get(inv_file);
2179 spin_unlock(&cifs_inode->open_file_lock);
2182 rc = cifs_reopen_file(inv_file, false);
2184 *ret_file = inv_file;
2188 spin_lock(&cifs_inode->open_file_lock);
2189 list_move_tail(&inv_file->flist, &cifs_inode->openFileList);
2190 spin_unlock(&cifs_inode->open_file_lock);
2191 cifsFileInfo_put(inv_file);
2194 spin_lock(&cifs_inode->open_file_lock);
2195 goto refind_writable;
2201 struct cifsFileInfo *
2202 find_writable_file(struct cifsInodeInfo *cifs_inode, int flags)
2204 struct cifsFileInfo *cfile;
2207 rc = cifs_get_writable_file(cifs_inode, flags, &cfile);
2209 cifs_dbg(FYI, "Couldn't find writable handle rc=%d\n", rc);
2215 cifs_get_writable_path(struct cifs_tcon *tcon, const char *name,
2217 struct cifsFileInfo **ret_file)
2219 struct cifsFileInfo *cfile;
2220 void *page = alloc_dentry_path();
2224 spin_lock(&tcon->open_file_lock);
2225 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2226 struct cifsInodeInfo *cinode;
2227 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2228 if (IS_ERR(full_path)) {
2229 spin_unlock(&tcon->open_file_lock);
2230 free_dentry_path(page);
2231 return PTR_ERR(full_path);
2233 if (strcmp(full_path, name))
2236 cinode = CIFS_I(d_inode(cfile->dentry));
2237 spin_unlock(&tcon->open_file_lock);
2238 free_dentry_path(page);
2239 return cifs_get_writable_file(cinode, flags, ret_file);
2242 spin_unlock(&tcon->open_file_lock);
2243 free_dentry_path(page);
2248 cifs_get_readable_path(struct cifs_tcon *tcon, const char *name,
2249 struct cifsFileInfo **ret_file)
2251 struct cifsFileInfo *cfile;
2252 void *page = alloc_dentry_path();
2256 spin_lock(&tcon->open_file_lock);
2257 list_for_each_entry(cfile, &tcon->openFileList, tlist) {
2258 struct cifsInodeInfo *cinode;
2259 const char *full_path = build_path_from_dentry(cfile->dentry, page);
2260 if (IS_ERR(full_path)) {
2261 spin_unlock(&tcon->open_file_lock);
2262 free_dentry_path(page);
2263 return PTR_ERR(full_path);
2265 if (strcmp(full_path, name))
2268 cinode = CIFS_I(d_inode(cfile->dentry));
2269 spin_unlock(&tcon->open_file_lock);
2270 free_dentry_path(page);
2271 *ret_file = find_readable_file(cinode, 0);
2272 return *ret_file ? 0 : -ENOENT;
2275 spin_unlock(&tcon->open_file_lock);
2276 free_dentry_path(page);
2281 cifs_writedata_release(struct kref *refcount)
2283 struct cifs_writedata *wdata = container_of(refcount,
2284 struct cifs_writedata, refcount);
2285 #ifdef CONFIG_CIFS_SMB_DIRECT
2287 smbd_deregister_mr(wdata->mr);
2293 cifsFileInfo_put(wdata->cfile);
2295 kvfree(wdata->pages);
2300 * Write failed with a retryable error. Resend the write request. It's also
2301 * possible that the page was redirtied so re-clean the page.
2304 cifs_writev_requeue(struct cifs_writedata *wdata)
2307 struct inode *inode = d_inode(wdata->cfile->dentry);
2308 struct TCP_Server_Info *server;
2309 unsigned int rest_len;
2311 server = tlink_tcon(wdata->cfile->tlink)->ses->server;
2313 rest_len = wdata->bytes;
2315 struct cifs_writedata *wdata2;
2316 unsigned int j, nr_pages, wsize, tailsz, cur_len;
2318 wsize = server->ops->wp_retry_size(inode);
2319 if (wsize < rest_len) {
2320 nr_pages = wsize / PAGE_SIZE;
2325 cur_len = nr_pages * PAGE_SIZE;
2328 nr_pages = DIV_ROUND_UP(rest_len, PAGE_SIZE);
2330 tailsz = rest_len - (nr_pages - 1) * PAGE_SIZE;
2333 wdata2 = cifs_writedata_alloc(nr_pages, cifs_writev_complete);
2339 for (j = 0; j < nr_pages; j++) {
2340 wdata2->pages[j] = wdata->pages[i + j];
2341 lock_page(wdata2->pages[j]);
2342 clear_page_dirty_for_io(wdata2->pages[j]);
2345 wdata2->sync_mode = wdata->sync_mode;
2346 wdata2->nr_pages = nr_pages;
2347 wdata2->offset = page_offset(wdata2->pages[0]);
2348 wdata2->pagesz = PAGE_SIZE;
2349 wdata2->tailsz = tailsz;
2350 wdata2->bytes = cur_len;
2352 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY,
2354 if (!wdata2->cfile) {
2355 cifs_dbg(VFS, "No writable handle to retry writepages rc=%d\n",
2357 if (!is_retryable_error(rc))
2360 wdata2->pid = wdata2->cfile->pid;
2361 rc = server->ops->async_writev(wdata2,
2362 cifs_writedata_release);
2365 for (j = 0; j < nr_pages; j++) {
2366 unlock_page(wdata2->pages[j]);
2367 if (rc != 0 && !is_retryable_error(rc)) {
2368 SetPageError(wdata2->pages[j]);
2369 end_page_writeback(wdata2->pages[j]);
2370 put_page(wdata2->pages[j]);
2374 kref_put(&wdata2->refcount, cifs_writedata_release);
2376 if (is_retryable_error(rc))
2382 rest_len -= cur_len;
2384 } while (i < wdata->nr_pages);
2386 /* cleanup remaining pages from the original wdata */
2387 for (; i < wdata->nr_pages; i++) {
2388 SetPageError(wdata->pages[i]);
2389 end_page_writeback(wdata->pages[i]);
2390 put_page(wdata->pages[i]);
2393 if (rc != 0 && !is_retryable_error(rc))
2394 mapping_set_error(inode->i_mapping, rc);
2395 kref_put(&wdata->refcount, cifs_writedata_release);
2399 cifs_writev_complete(struct work_struct *work)
2401 struct cifs_writedata *wdata = container_of(work,
2402 struct cifs_writedata, work);
2403 struct inode *inode = d_inode(wdata->cfile->dentry);
2406 if (wdata->result == 0) {
2407 spin_lock(&inode->i_lock);
2408 cifs_update_eof(CIFS_I(inode), wdata->offset, wdata->bytes);
2409 spin_unlock(&inode->i_lock);
2410 cifs_stats_bytes_written(tlink_tcon(wdata->cfile->tlink),
2412 } else if (wdata->sync_mode == WB_SYNC_ALL && wdata->result == -EAGAIN)
2413 return cifs_writev_requeue(wdata);
2415 for (i = 0; i < wdata->nr_pages; i++) {
2416 struct page *page = wdata->pages[i];
2418 if (wdata->result == -EAGAIN)
2419 __set_page_dirty_nobuffers(page);
2420 else if (wdata->result < 0)
2422 end_page_writeback(page);
2423 cifs_readpage_to_fscache(inode, page);
2426 if (wdata->result != -EAGAIN)
2427 mapping_set_error(inode->i_mapping, wdata->result);
2428 kref_put(&wdata->refcount, cifs_writedata_release);
2431 struct cifs_writedata *
2432 cifs_writedata_alloc(unsigned int nr_pages, work_func_t complete)
2434 struct cifs_writedata *writedata = NULL;
2435 struct page **pages =
2436 kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
2438 writedata = cifs_writedata_direct_alloc(pages, complete);
2446 struct cifs_writedata *
2447 cifs_writedata_direct_alloc(struct page **pages, work_func_t complete)
2449 struct cifs_writedata *wdata;
2451 wdata = kzalloc(sizeof(*wdata), GFP_NOFS);
2452 if (wdata != NULL) {
2453 wdata->pages = pages;
2454 kref_init(&wdata->refcount);
2455 INIT_LIST_HEAD(&wdata->list);
2456 init_completion(&wdata->done);
2457 INIT_WORK(&wdata->work, complete);
2463 static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
2465 struct address_space *mapping = page->mapping;
2466 loff_t offset = (loff_t)page->index << PAGE_SHIFT;
2469 int bytes_written = 0;
2470 struct inode *inode;
2471 struct cifsFileInfo *open_file;
2473 if (!mapping || !mapping->host)
2476 inode = page->mapping->host;
2478 offset += (loff_t)from;
2479 write_data = kmap(page);
2482 if ((to > PAGE_SIZE) || (from > to)) {
2487 /* racing with truncate? */
2488 if (offset > mapping->host->i_size) {
2490 return 0; /* don't care */
2493 /* check to make sure that we are not extending the file */
2494 if (mapping->host->i_size - offset < (loff_t)to)
2495 to = (unsigned)(mapping->host->i_size - offset);
2497 rc = cifs_get_writable_file(CIFS_I(mapping->host), FIND_WR_ANY,
2500 bytes_written = cifs_write(open_file, open_file->pid,
2501 write_data, to - from, &offset);
2502 cifsFileInfo_put(open_file);
2503 /* Does mm or vfs already set times? */
2504 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2505 if ((bytes_written > 0) && (offset))
2507 else if (bytes_written < 0)
2512 cifs_dbg(FYI, "No writable handle for write page rc=%d\n", rc);
2513 if (!is_retryable_error(rc))
2521 static struct cifs_writedata *
2522 wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
2523 pgoff_t end, pgoff_t *index,
2524 unsigned int *found_pages)
2526 struct cifs_writedata *wdata;
2528 wdata = cifs_writedata_alloc((unsigned int)tofind,
2529 cifs_writev_complete);
2533 *found_pages = find_get_pages_range_tag(mapping, index, end,
2534 PAGECACHE_TAG_DIRTY, tofind, wdata->pages);
2539 wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
2540 struct address_space *mapping,
2541 struct writeback_control *wbc,
2542 pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
2544 unsigned int nr_pages = 0, i;
2547 for (i = 0; i < found_pages; i++) {
2548 page = wdata->pages[i];
2550 * At this point we hold neither the i_pages lock nor the
2551 * page lock: the page may be truncated or invalidated
2552 * (changing page->mapping to NULL), or even swizzled
2553 * back from swapper_space to tmpfs file mapping
2558 else if (!trylock_page(page))
2561 if (unlikely(page->mapping != mapping)) {
2566 if (!wbc->range_cyclic && page->index > end) {
2572 if (*next && (page->index != *next)) {
2573 /* Not next consecutive page */
2578 if (wbc->sync_mode != WB_SYNC_NONE)
2579 wait_on_page_writeback(page);
2581 if (PageWriteback(page) ||
2582 !clear_page_dirty_for_io(page)) {
2588 * This actually clears the dirty bit in the radix tree.
2589 * See cifs_writepage() for more commentary.
2591 set_page_writeback(page);
2592 if (page_offset(page) >= i_size_read(mapping->host)) {
2595 end_page_writeback(page);
2599 wdata->pages[i] = page;
2600 *next = page->index + 1;
2604 /* reset index to refind any pages skipped */
2606 *index = wdata->pages[0]->index + 1;
2608 /* put any pages we aren't going to use */
2609 for (i = nr_pages; i < found_pages; i++) {
2610 put_page(wdata->pages[i]);
2611 wdata->pages[i] = NULL;
2618 wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
2619 struct address_space *mapping, struct writeback_control *wbc)
2623 wdata->sync_mode = wbc->sync_mode;
2624 wdata->nr_pages = nr_pages;
2625 wdata->offset = page_offset(wdata->pages[0]);
2626 wdata->pagesz = PAGE_SIZE;
2627 wdata->tailsz = min(i_size_read(mapping->host) -
2628 page_offset(wdata->pages[nr_pages - 1]),
2630 wdata->bytes = ((nr_pages - 1) * PAGE_SIZE) + wdata->tailsz;
2631 wdata->pid = wdata->cfile->pid;
2633 rc = adjust_credits(wdata->server, &wdata->credits, wdata->bytes);
2637 if (wdata->cfile->invalidHandle)
2640 rc = wdata->server->ops->async_writev(wdata,
2641 cifs_writedata_release);
2646 static int cifs_writepages(struct address_space *mapping,
2647 struct writeback_control *wbc)
2649 struct inode *inode = mapping->host;
2650 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2651 struct TCP_Server_Info *server;
2652 bool done = false, scanned = false, range_whole = false;
2654 struct cifs_writedata *wdata;
2655 struct cifsFileInfo *cfile = NULL;
2661 * If wsize is smaller than the page cache size, default to writing
2662 * one page at a time via cifs_writepage
2664 if (cifs_sb->ctx->wsize < PAGE_SIZE)
2665 return generic_writepages(mapping, wbc);
2668 if (wbc->range_cyclic) {
2669 index = mapping->writeback_index; /* Start from prev offset */
2672 index = wbc->range_start >> PAGE_SHIFT;
2673 end = wbc->range_end >> PAGE_SHIFT;
2674 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2678 server = cifs_pick_channel(cifs_sb_master_tcon(cifs_sb)->ses);
2681 while (!done && index <= end) {
2682 unsigned int i, nr_pages, found_pages, wsize;
2683 pgoff_t next = 0, tofind, saved_index = index;
2684 struct cifs_credits credits_on_stack;
2685 struct cifs_credits *credits = &credits_on_stack;
2686 int get_file_rc = 0;
2689 cifsFileInfo_put(cfile);
2691 rc = cifs_get_writable_file(CIFS_I(inode), FIND_WR_ANY, &cfile);
2693 /* in case of an error store it to return later */
2697 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
2704 tofind = min((wsize / PAGE_SIZE) - 1, end - index) + 1;
2706 wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
2711 add_credits_and_wake_if(server, credits, 0);
2715 if (found_pages == 0) {
2716 kref_put(&wdata->refcount, cifs_writedata_release);
2717 add_credits_and_wake_if(server, credits, 0);
2721 nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
2722 end, &index, &next, &done);
2724 /* nothing to write? */
2725 if (nr_pages == 0) {
2726 kref_put(&wdata->refcount, cifs_writedata_release);
2727 add_credits_and_wake_if(server, credits, 0);
2731 wdata->credits = credits_on_stack;
2732 wdata->cfile = cfile;
2733 wdata->server = server;
2736 if (!wdata->cfile) {
2737 cifs_dbg(VFS, "No writable handle in writepages rc=%d\n",
2739 if (is_retryable_error(get_file_rc))
2744 rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
2746 for (i = 0; i < nr_pages; ++i)
2747 unlock_page(wdata->pages[i]);
2749 /* send failure -- clean up the mess */
2751 add_credits_and_wake_if(server, &wdata->credits, 0);
2752 for (i = 0; i < nr_pages; ++i) {
2753 if (is_retryable_error(rc))
2754 redirty_page_for_writepage(wbc,
2757 SetPageError(wdata->pages[i]);
2758 end_page_writeback(wdata->pages[i]);
2759 put_page(wdata->pages[i]);
2761 if (!is_retryable_error(rc))
2762 mapping_set_error(mapping, rc);
2764 kref_put(&wdata->refcount, cifs_writedata_release);
2766 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN) {
2767 index = saved_index;
2771 /* Return immediately if we received a signal during writing */
2772 if (is_interrupt_error(rc)) {
2777 if (rc != 0 && saved_rc == 0)
2780 wbc->nr_to_write -= nr_pages;
2781 if (wbc->nr_to_write <= 0)
2787 if (!scanned && !done) {
2789 * We hit the last page and there is more work to be done: wrap
2790 * back to the start of the file
2800 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2801 mapping->writeback_index = index;
2804 cifsFileInfo_put(cfile);
2806 /* Indication to update ctime and mtime as close is deferred */
2807 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2812 cifs_writepage_locked(struct page *page, struct writeback_control *wbc)
2818 /* BB add check for wbc flags */
2820 if (!PageUptodate(page))
2821 cifs_dbg(FYI, "ppw - page not up to date\n");
2824 * Set the "writeback" flag, and clear "dirty" in the radix tree.
2826 * A writepage() implementation always needs to do either this,
2827 * or re-dirty the page with "redirty_page_for_writepage()" in
2828 * the case of a failure.
2830 * Just unlocking the page will cause the radix tree tag-bits
2831 * to fail to update with the state of the page correctly.
2833 set_page_writeback(page);
2835 rc = cifs_partialpagewrite(page, 0, PAGE_SIZE);
2836 if (is_retryable_error(rc)) {
2837 if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
2839 redirty_page_for_writepage(wbc, page);
2840 } else if (rc != 0) {
2842 mapping_set_error(page->mapping, rc);
2844 SetPageUptodate(page);
2846 end_page_writeback(page);
2852 static int cifs_writepage(struct page *page, struct writeback_control *wbc)
2854 int rc = cifs_writepage_locked(page, wbc);
2859 static int cifs_write_end(struct file *file, struct address_space *mapping,
2860 loff_t pos, unsigned len, unsigned copied,
2861 struct page *page, void *fsdata)
2864 struct inode *inode = mapping->host;
2865 struct cifsFileInfo *cfile = file->private_data;
2866 struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb);
2869 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
2872 pid = current->tgid;
2874 cifs_dbg(FYI, "write_end for page %p from pos %lld with %d bytes\n",
2877 if (PageChecked(page)) {
2879 SetPageUptodate(page);
2880 ClearPageChecked(page);
2881 } else if (!PageUptodate(page) && copied == PAGE_SIZE)
2882 SetPageUptodate(page);
2884 if (!PageUptodate(page)) {
2886 unsigned offset = pos & (PAGE_SIZE - 1);
2890 /* this is probably better than directly calling
2891 partialpage_write since in this function the file handle is
2892 known which we might as well leverage */
2893 /* BB check if anything else missing out of ppw
2894 such as updating last write time */
2895 page_data = kmap(page);
2896 rc = cifs_write(cfile, pid, page_data + offset, copied, &pos);
2897 /* if (rc < 0) should we set writebehind rc? */
2904 set_page_dirty(page);
2908 spin_lock(&inode->i_lock);
2909 if (pos > inode->i_size) {
2910 i_size_write(inode, pos);
2911 inode->i_blocks = (512 - 1 + pos) >> 9;
2913 spin_unlock(&inode->i_lock);
2918 /* Indication to update ctime and mtime as close is deferred */
2919 set_bit(CIFS_INO_MODIFIED_ATTR, &CIFS_I(inode)->flags);
2924 int cifs_strict_fsync(struct file *file, loff_t start, loff_t end,
2929 struct cifs_tcon *tcon;
2930 struct TCP_Server_Info *server;
2931 struct cifsFileInfo *smbfile = file->private_data;
2932 struct inode *inode = file_inode(file);
2933 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2935 rc = file_write_and_wait_range(file, start, end);
2937 trace_cifs_fsync_err(inode->i_ino, rc);
2943 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2946 if (!CIFS_CACHE_READ(CIFS_I(inode))) {
2947 rc = cifs_zap_mapping(inode);
2949 cifs_dbg(FYI, "rc: %d during invalidate phase\n", rc);
2950 rc = 0; /* don't care about it in fsync */
2954 tcon = tlink_tcon(smbfile->tlink);
2955 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2956 server = tcon->ses->server;
2957 if (server->ops->flush == NULL) {
2959 goto strict_fsync_exit;
2962 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
2963 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
2965 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2966 cifsFileInfo_put(smbfile);
2968 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
2970 rc = server->ops->flush(xid, tcon, &smbfile->fid);
2978 int cifs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
2982 struct cifs_tcon *tcon;
2983 struct TCP_Server_Info *server;
2984 struct cifsFileInfo *smbfile = file->private_data;
2985 struct inode *inode = file_inode(file);
2986 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
2988 rc = file_write_and_wait_range(file, start, end);
2990 trace_cifs_fsync_err(file_inode(file)->i_ino, rc);
2996 cifs_dbg(FYI, "Sync file - name: %pD datasync: 0x%x\n",
2999 tcon = tlink_tcon(smbfile->tlink);
3000 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
3001 server = tcon->ses->server;
3002 if (server->ops->flush == NULL) {
3007 if ((OPEN_FMODE(smbfile->f_flags) & FMODE_WRITE) == 0) {
3008 smbfile = find_writable_file(CIFS_I(inode), FIND_WR_ANY);
3010 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3011 cifsFileInfo_put(smbfile);
3013 cifs_dbg(FYI, "ignore fsync for file not open for write\n");
3015 rc = server->ops->flush(xid, tcon, &smbfile->fid);
3024 * As file closes, flush all cached write data for this inode checking
3025 * for write behind errors.
3027 int cifs_flush(struct file *file, fl_owner_t id)
3029 struct inode *inode = file_inode(file);
3032 if (file->f_mode & FMODE_WRITE)
3033 rc = filemap_write_and_wait(inode->i_mapping);
3035 cifs_dbg(FYI, "Flush inode %p file %p rc %d\n", inode, file, rc);
3037 /* get more nuanced writeback errors */
3038 rc = filemap_check_wb_err(file->f_mapping, 0);
3039 trace_cifs_flush_err(inode->i_ino, rc);
3045 cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
3050 for (i = 0; i < num_pages; i++) {
3051 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3054 * save number of pages we have already allocated and
3055 * return with ENOMEM error
3064 for (i = 0; i < num_pages; i++)
3071 size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
3076 clen = min_t(const size_t, len, wsize);
3077 num_pages = DIV_ROUND_UP(clen, PAGE_SIZE);
3086 cifs_uncached_writedata_release(struct kref *refcount)
3089 struct cifs_writedata *wdata = container_of(refcount,
3090 struct cifs_writedata, refcount);
3092 kref_put(&wdata->ctx->refcount, cifs_aio_ctx_release);
3093 for (i = 0; i < wdata->nr_pages; i++)
3094 put_page(wdata->pages[i]);
3095 cifs_writedata_release(refcount);
3098 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx);
3101 cifs_uncached_writev_complete(struct work_struct *work)
3103 struct cifs_writedata *wdata = container_of(work,
3104 struct cifs_writedata, work);
3105 struct inode *inode = d_inode(wdata->cfile->dentry);
3106 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3108 spin_lock(&inode->i_lock);
3109 cifs_update_eof(cifsi, wdata->offset, wdata->bytes);
3110 if (cifsi->server_eof > inode->i_size)
3111 i_size_write(inode, cifsi->server_eof);
3112 spin_unlock(&inode->i_lock);
3114 complete(&wdata->done);
3115 collect_uncached_write_data(wdata->ctx);
3116 /* the below call can possibly free the last ref to aio ctx */
3117 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3121 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
3122 size_t *len, unsigned long *num_pages)
3124 size_t save_len, copied, bytes, cur_len = *len;
3125 unsigned long i, nr_pages = *num_pages;
3128 for (i = 0; i < nr_pages; i++) {
3129 bytes = min_t(const size_t, cur_len, PAGE_SIZE);
3130 copied = copy_page_from_iter(wdata->pages[i], 0, bytes, from);
3133 * If we didn't copy as much as we expected, then that
3134 * may mean we trod into an unmapped area. Stop copying
3135 * at that point. On the next pass through the big
3136 * loop, we'll likely end up getting a zero-length
3137 * write and bailing out of it.
3142 cur_len = save_len - cur_len;
3146 * If we have no data to send, then that probably means that
3147 * the copy above failed altogether. That's most likely because
3148 * the address in the iovec was bogus. Return -EFAULT and let
3149 * the caller free anything we allocated and bail out.
3155 * i + 1 now represents the number of pages we actually used in
3156 * the copy phase above.
3163 cifs_resend_wdata(struct cifs_writedata *wdata, struct list_head *wdata_list,
3164 struct cifs_aio_ctx *ctx)
3167 struct cifs_credits credits;
3169 struct TCP_Server_Info *server = wdata->server;
3172 if (wdata->cfile->invalidHandle) {
3173 rc = cifs_reopen_file(wdata->cfile, false);
3182 * Wait for credits to resend this wdata.
3183 * Note: we are attempting to resend the whole wdata not in
3187 rc = server->ops->wait_mtu_credits(server, wdata->bytes,
3192 if (wsize < wdata->bytes) {
3193 add_credits_and_wake_if(server, &credits, 0);
3196 } while (wsize < wdata->bytes);
3197 wdata->credits = credits;
3199 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3202 if (wdata->cfile->invalidHandle)
3205 #ifdef CONFIG_CIFS_SMB_DIRECT
3207 wdata->mr->need_invalidate = true;
3208 smbd_deregister_mr(wdata->mr);
3212 rc = server->ops->async_writev(wdata,
3213 cifs_uncached_writedata_release);
3217 /* If the write was successfully sent, we are done */
3219 list_add_tail(&wdata->list, wdata_list);
3223 /* Roll back credits and retry if needed */
3224 add_credits_and_wake_if(server, &wdata->credits, 0);
3225 } while (rc == -EAGAIN);
3228 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3233 cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
3234 struct cifsFileInfo *open_file,
3235 struct cifs_sb_info *cifs_sb, struct list_head *wdata_list,
3236 struct cifs_aio_ctx *ctx)
3240 unsigned long nr_pages, num_pages, i;
3241 struct cifs_writedata *wdata;
3242 struct iov_iter saved_from = *from;
3243 loff_t saved_offset = offset;
3245 struct TCP_Server_Info *server;
3246 struct page **pagevec;
3250 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3251 pid = open_file->pid;
3253 pid = current->tgid;
3255 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3260 struct cifs_credits credits_on_stack;
3261 struct cifs_credits *credits = &credits_on_stack;
3263 if (open_file->invalidHandle) {
3264 rc = cifs_reopen_file(open_file, false);
3271 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->wsize,
3276 cur_len = min_t(const size_t, len, wsize);
3278 if (ctx->direct_io) {
3281 result = iov_iter_get_pages_alloc(
3282 from, &pagevec, cur_len, &start);
3285 "direct_writev couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
3286 result, iov_iter_type(from),
3287 from->iov_offset, from->count);
3291 add_credits_and_wake_if(server, credits, 0);
3294 cur_len = (size_t)result;
3295 iov_iter_advance(from, cur_len);
3298 (cur_len + start + PAGE_SIZE - 1) / PAGE_SIZE;
3300 wdata = cifs_writedata_direct_alloc(pagevec,
3301 cifs_uncached_writev_complete);
3304 for (i = 0; i < nr_pages; i++)
3305 put_page(pagevec[i]);
3307 add_credits_and_wake_if(server, credits, 0);
3312 wdata->page_offset = start;
3315 cur_len - (PAGE_SIZE - start) -
3316 (nr_pages - 2) * PAGE_SIZE :
3319 nr_pages = get_numpages(wsize, len, &cur_len);
3320 wdata = cifs_writedata_alloc(nr_pages,
3321 cifs_uncached_writev_complete);
3324 add_credits_and_wake_if(server, credits, 0);
3328 rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
3330 kvfree(wdata->pages);
3332 add_credits_and_wake_if(server, credits, 0);
3336 num_pages = nr_pages;
3337 rc = wdata_fill_from_iovec(
3338 wdata, from, &cur_len, &num_pages);
3340 for (i = 0; i < nr_pages; i++)
3341 put_page(wdata->pages[i]);
3342 kvfree(wdata->pages);
3344 add_credits_and_wake_if(server, credits, 0);
3349 * Bring nr_pages down to the number of pages we
3350 * actually used, and free any pages that we didn't use.
3352 for ( ; nr_pages > num_pages; nr_pages--)
3353 put_page(wdata->pages[nr_pages - 1]);
3355 wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
3358 wdata->sync_mode = WB_SYNC_ALL;
3359 wdata->nr_pages = nr_pages;
3360 wdata->offset = (__u64)offset;
3361 wdata->cfile = cifsFileInfo_get(open_file);
3362 wdata->server = server;
3364 wdata->bytes = cur_len;
3365 wdata->pagesz = PAGE_SIZE;
3366 wdata->credits = credits_on_stack;
3368 kref_get(&ctx->refcount);
3370 rc = adjust_credits(server, &wdata->credits, wdata->bytes);
3373 if (wdata->cfile->invalidHandle)
3376 rc = server->ops->async_writev(wdata,
3377 cifs_uncached_writedata_release);
3381 add_credits_and_wake_if(server, &wdata->credits, 0);
3382 kref_put(&wdata->refcount,
3383 cifs_uncached_writedata_release);
3384 if (rc == -EAGAIN) {
3386 iov_iter_advance(from, offset - saved_offset);
3392 list_add_tail(&wdata->list, wdata_list);
3401 static void collect_uncached_write_data(struct cifs_aio_ctx *ctx)
3403 struct cifs_writedata *wdata, *tmp;
3404 struct cifs_tcon *tcon;
3405 struct cifs_sb_info *cifs_sb;
3406 struct dentry *dentry = ctx->cfile->dentry;
3409 tcon = tlink_tcon(ctx->cfile->tlink);
3410 cifs_sb = CIFS_SB(dentry->d_sb);
3412 mutex_lock(&ctx->aio_mutex);
3414 if (list_empty(&ctx->list)) {
3415 mutex_unlock(&ctx->aio_mutex);
3421 * Wait for and collect replies for any successful sends in order of
3422 * increasing offset. Once an error is hit, then return without waiting
3423 * for any more replies.
3426 list_for_each_entry_safe(wdata, tmp, &ctx->list, list) {
3428 if (!try_wait_for_completion(&wdata->done)) {
3429 mutex_unlock(&ctx->aio_mutex);
3436 ctx->total_len += wdata->bytes;
3438 /* resend call if it's a retryable error */
3439 if (rc == -EAGAIN) {
3440 struct list_head tmp_list;
3441 struct iov_iter tmp_from = ctx->iter;
3443 INIT_LIST_HEAD(&tmp_list);
3444 list_del_init(&wdata->list);
3447 rc = cifs_resend_wdata(
3448 wdata, &tmp_list, ctx);
3450 iov_iter_advance(&tmp_from,
3451 wdata->offset - ctx->pos);
3453 rc = cifs_write_from_iter(wdata->offset,
3454 wdata->bytes, &tmp_from,
3455 ctx->cfile, cifs_sb, &tmp_list,
3458 kref_put(&wdata->refcount,
3459 cifs_uncached_writedata_release);
3462 list_splice(&tmp_list, &ctx->list);
3466 list_del_init(&wdata->list);
3467 kref_put(&wdata->refcount, cifs_uncached_writedata_release);
3470 cifs_stats_bytes_written(tcon, ctx->total_len);
3471 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags);
3473 ctx->rc = (rc == 0) ? ctx->total_len : rc;
3475 mutex_unlock(&ctx->aio_mutex);
3477 if (ctx->iocb && ctx->iocb->ki_complete)
3478 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
3480 complete(&ctx->done);
3483 static ssize_t __cifs_writev(
3484 struct kiocb *iocb, struct iov_iter *from, bool direct)
3486 struct file *file = iocb->ki_filp;
3487 ssize_t total_written = 0;
3488 struct cifsFileInfo *cfile;
3489 struct cifs_tcon *tcon;
3490 struct cifs_sb_info *cifs_sb;
3491 struct cifs_aio_ctx *ctx;
3492 struct iov_iter saved_from = *from;
3493 size_t len = iov_iter_count(from);
3497 * iov_iter_get_pages_alloc doesn't work with ITER_KVEC.
3498 * In this case, fall back to non-direct write function.
3499 * this could be improved by getting pages directly in ITER_KVEC
3501 if (direct && iov_iter_is_kvec(from)) {
3502 cifs_dbg(FYI, "use non-direct cifs_writev for kvec I/O\n");
3506 rc = generic_write_checks(iocb, from);
3510 cifs_sb = CIFS_FILE_SB(file);
3511 cfile = file->private_data;
3512 tcon = tlink_tcon(cfile->tlink);
3514 if (!tcon->ses->server->ops->async_writev)
3517 ctx = cifs_aio_ctx_alloc();
3521 ctx->cfile = cifsFileInfo_get(cfile);
3523 if (!is_sync_kiocb(iocb))
3526 ctx->pos = iocb->ki_pos;
3529 ctx->direct_io = true;
3533 rc = setup_aio_ctx_iter(ctx, from, WRITE);
3535 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3540 /* grab a lock here due to read response handlers can access ctx */
3541 mutex_lock(&ctx->aio_mutex);
3543 rc = cifs_write_from_iter(iocb->ki_pos, ctx->len, &saved_from,
3544 cfile, cifs_sb, &ctx->list, ctx);
3547 * If at least one write was successfully sent, then discard any rc
3548 * value from the later writes. If the other write succeeds, then
3549 * we'll end up returning whatever was written. If it fails, then
3550 * we'll get a new rc value from that.
3552 if (!list_empty(&ctx->list))
3555 mutex_unlock(&ctx->aio_mutex);
3558 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3562 if (!is_sync_kiocb(iocb)) {
3563 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3564 return -EIOCBQUEUED;
3567 rc = wait_for_completion_killable(&ctx->done);
3569 mutex_lock(&ctx->aio_mutex);
3570 ctx->rc = rc = -EINTR;
3571 total_written = ctx->total_len;
3572 mutex_unlock(&ctx->aio_mutex);
3575 total_written = ctx->total_len;
3578 kref_put(&ctx->refcount, cifs_aio_ctx_release);
3580 if (unlikely(!total_written))
3583 iocb->ki_pos += total_written;
3584 return total_written;
3587 ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from)
3589 struct file *file = iocb->ki_filp;
3591 cifs_revalidate_mapping(file->f_inode);
3592 return __cifs_writev(iocb, from, true);
3595 ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from)
3597 return __cifs_writev(iocb, from, false);
3601 cifs_writev(struct kiocb *iocb, struct iov_iter *from)
3603 struct file *file = iocb->ki_filp;
3604 struct cifsFileInfo *cfile = (struct cifsFileInfo *)file->private_data;
3605 struct inode *inode = file->f_mapping->host;
3606 struct cifsInodeInfo *cinode = CIFS_I(inode);
3607 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
3612 * We need to hold the sem to be sure nobody modifies lock list
3613 * with a brlock that prevents writing.
3615 down_read(&cinode->lock_sem);
3617 rc = generic_write_checks(iocb, from);
3621 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(from),
3622 server->vals->exclusive_lock_type, 0,
3623 NULL, CIFS_WRITE_OP))
3624 rc = __generic_file_write_iter(iocb, from);
3628 up_read(&cinode->lock_sem);
3629 inode_unlock(inode);
3632 rc = generic_write_sync(iocb, rc);
3637 cifs_strict_writev(struct kiocb *iocb, struct iov_iter *from)
3639 struct inode *inode = file_inode(iocb->ki_filp);
3640 struct cifsInodeInfo *cinode = CIFS_I(inode);
3641 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3642 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
3643 iocb->ki_filp->private_data;
3644 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
3647 written = cifs_get_writer(cinode);
3651 if (CIFS_CACHE_WRITE(cinode)) {
3652 if (cap_unix(tcon->ses) &&
3653 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability))
3654 && ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0)) {
3655 written = generic_file_write_iter(iocb, from);
3658 written = cifs_writev(iocb, from);
3662 * For non-oplocked files in strict cache mode we need to write the data
3663 * to the server exactly from the pos to pos+len-1 rather than flush all
3664 * affected pages because it may cause a error with mandatory locks on
3665 * these pages but not on the region from pos to ppos+len-1.
3667 written = cifs_user_writev(iocb, from);
3668 if (CIFS_CACHE_READ(cinode)) {
3670 * We have read level caching and we have just sent a write
3671 * request to the server thus making data in the cache stale.
3672 * Zap the cache and set oplock/lease level to NONE to avoid
3673 * reading stale data from the cache. All subsequent read
3674 * operations will read new data from the server.
3676 cifs_zap_mapping(inode);
3677 cifs_dbg(FYI, "Set Oplock/Lease to NONE for inode=%p after write\n",
3682 cifs_put_writer(cinode);
3686 static struct cifs_readdata *
3687 cifs_readdata_direct_alloc(struct page **pages, work_func_t complete)
3689 struct cifs_readdata *rdata;
3691 rdata = kzalloc(sizeof(*rdata), GFP_KERNEL);
3692 if (rdata != NULL) {
3693 rdata->pages = pages;
3694 kref_init(&rdata->refcount);
3695 INIT_LIST_HEAD(&rdata->list);
3696 init_completion(&rdata->done);
3697 INIT_WORK(&rdata->work, complete);
3703 static struct cifs_readdata *
3704 cifs_readdata_alloc(unsigned int nr_pages, work_func_t complete)
3706 struct page **pages =
3707 kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
3708 struct cifs_readdata *ret = NULL;
3711 ret = cifs_readdata_direct_alloc(pages, complete);
3720 cifs_readdata_release(struct kref *refcount)
3722 struct cifs_readdata *rdata = container_of(refcount,
3723 struct cifs_readdata, refcount);
3724 #ifdef CONFIG_CIFS_SMB_DIRECT
3726 smbd_deregister_mr(rdata->mr);
3731 cifsFileInfo_put(rdata->cfile);
3733 kvfree(rdata->pages);
3738 cifs_read_allocate_pages(struct cifs_readdata *rdata, unsigned int nr_pages)
3744 for (i = 0; i < nr_pages; i++) {
3745 page = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3750 rdata->pages[i] = page;
3754 unsigned int nr_page_failed = i;
3756 for (i = 0; i < nr_page_failed; i++) {
3757 put_page(rdata->pages[i]);
3758 rdata->pages[i] = NULL;
3765 cifs_uncached_readdata_release(struct kref *refcount)
3767 struct cifs_readdata *rdata = container_of(refcount,
3768 struct cifs_readdata, refcount);
3771 kref_put(&rdata->ctx->refcount, cifs_aio_ctx_release);
3772 for (i = 0; i < rdata->nr_pages; i++) {
3773 put_page(rdata->pages[i]);
3775 cifs_readdata_release(refcount);
3779 * cifs_readdata_to_iov - copy data from pages in response to an iovec
3780 * @rdata: the readdata response with list of pages holding data
3781 * @iter: destination for our data
3783 * This function copies data from a list of pages in a readdata response into
3784 * an array of iovecs. It will first calculate where the data should go
3785 * based on the info in the readdata and then copy the data into that spot.
3788 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
3790 size_t remaining = rdata->got_bytes;
3793 for (i = 0; i < rdata->nr_pages; i++) {
3794 struct page *page = rdata->pages[i];
3795 size_t copy = min_t(size_t, remaining, PAGE_SIZE);
3798 if (unlikely(iov_iter_is_pipe(iter))) {
3799 void *addr = kmap_atomic(page);
3801 written = copy_to_iter(addr, copy, iter);
3802 kunmap_atomic(addr);
3804 written = copy_page_to_iter(page, 0, copy, iter);
3805 remaining -= written;
3806 if (written < copy && iov_iter_count(iter) > 0)
3809 return remaining ? -EFAULT : 0;
3812 static void collect_uncached_read_data(struct cifs_aio_ctx *ctx);
3815 cifs_uncached_readv_complete(struct work_struct *work)
3817 struct cifs_readdata *rdata = container_of(work,
3818 struct cifs_readdata, work);
3820 complete(&rdata->done);
3821 collect_uncached_read_data(rdata->ctx);
3822 /* the below call can possibly free the last ref to aio ctx */
3823 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3827 uncached_fill_pages(struct TCP_Server_Info *server,
3828 struct cifs_readdata *rdata, struct iov_iter *iter,
3833 unsigned int nr_pages = rdata->nr_pages;
3834 unsigned int page_offset = rdata->page_offset;
3836 rdata->got_bytes = 0;
3837 rdata->tailsz = PAGE_SIZE;
3838 for (i = 0; i < nr_pages; i++) {
3839 struct page *page = rdata->pages[i];
3841 unsigned int segment_size = rdata->pagesz;
3844 segment_size -= page_offset;
3850 /* no need to hold page hostage */
3851 rdata->pages[i] = NULL;
3858 if (len >= segment_size)
3859 /* enough data to fill the page */
3862 rdata->tailsz = len;
3866 result = copy_page_from_iter(
3867 page, page_offset, n, iter);
3868 #ifdef CONFIG_CIFS_SMB_DIRECT
3873 result = cifs_read_page_from_socket(
3874 server, page, page_offset, n);
3878 rdata->got_bytes += result;
3881 return result != -ECONNABORTED && rdata->got_bytes > 0 ?
3882 rdata->got_bytes : result;
3886 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
3887 struct cifs_readdata *rdata, unsigned int len)
3889 return uncached_fill_pages(server, rdata, NULL, len);
3893 cifs_uncached_copy_into_pages(struct TCP_Server_Info *server,
3894 struct cifs_readdata *rdata,
3895 struct iov_iter *iter)
3897 return uncached_fill_pages(server, rdata, iter, iter->count);
3900 static int cifs_resend_rdata(struct cifs_readdata *rdata,
3901 struct list_head *rdata_list,
3902 struct cifs_aio_ctx *ctx)
3905 struct cifs_credits credits;
3907 struct TCP_Server_Info *server;
3909 /* XXX: should we pick a new channel here? */
3910 server = rdata->server;
3913 if (rdata->cfile->invalidHandle) {
3914 rc = cifs_reopen_file(rdata->cfile, true);
3922 * Wait for credits to resend this rdata.
3923 * Note: we are attempting to resend the whole rdata not in
3927 rc = server->ops->wait_mtu_credits(server, rdata->bytes,
3933 if (rsize < rdata->bytes) {
3934 add_credits_and_wake_if(server, &credits, 0);
3937 } while (rsize < rdata->bytes);
3938 rdata->credits = credits;
3940 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
3942 if (rdata->cfile->invalidHandle)
3945 #ifdef CONFIG_CIFS_SMB_DIRECT
3947 rdata->mr->need_invalidate = true;
3948 smbd_deregister_mr(rdata->mr);
3952 rc = server->ops->async_readv(rdata);
3956 /* If the read was successfully sent, we are done */
3958 /* Add to aio pending list */
3959 list_add_tail(&rdata->list, rdata_list);
3963 /* Roll back credits and retry if needed */
3964 add_credits_and_wake_if(server, &rdata->credits, 0);
3965 } while (rc == -EAGAIN);
3968 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
3973 cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
3974 struct cifs_sb_info *cifs_sb, struct list_head *rdata_list,
3975 struct cifs_aio_ctx *ctx)
3977 struct cifs_readdata *rdata;
3978 unsigned int npages, rsize;
3979 struct cifs_credits credits_on_stack;
3980 struct cifs_credits *credits = &credits_on_stack;
3984 struct TCP_Server_Info *server;
3985 struct page **pagevec;
3987 struct iov_iter direct_iov = ctx->iter;
3989 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
3991 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
3992 pid = open_file->pid;
3994 pid = current->tgid;
3997 iov_iter_advance(&direct_iov, offset - ctx->pos);
4000 if (open_file->invalidHandle) {
4001 rc = cifs_reopen_file(open_file, true);
4008 if (cifs_sb->ctx->rsize == 0)
4009 cifs_sb->ctx->rsize =
4010 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4013 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4018 cur_len = min_t(const size_t, len, rsize);
4020 if (ctx->direct_io) {
4023 result = iov_iter_get_pages_alloc(
4024 &direct_iov, &pagevec,
4028 "Couldn't get user pages (rc=%zd) iter type %d iov_offset %zd count %zd\n",
4029 result, iov_iter_type(&direct_iov),
4030 direct_iov.iov_offset,
4035 add_credits_and_wake_if(server, credits, 0);
4038 cur_len = (size_t)result;
4039 iov_iter_advance(&direct_iov, cur_len);
4041 rdata = cifs_readdata_direct_alloc(
4042 pagevec, cifs_uncached_readv_complete);
4044 add_credits_and_wake_if(server, credits, 0);
4049 npages = (cur_len + start + PAGE_SIZE-1) / PAGE_SIZE;
4050 rdata->page_offset = start;
4051 rdata->tailsz = npages > 1 ?
4052 cur_len-(PAGE_SIZE-start)-(npages-2)*PAGE_SIZE :
4057 npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
4058 /* allocate a readdata struct */
4059 rdata = cifs_readdata_alloc(npages,
4060 cifs_uncached_readv_complete);
4062 add_credits_and_wake_if(server, credits, 0);
4067 rc = cifs_read_allocate_pages(rdata, npages);
4069 kvfree(rdata->pages);
4071 add_credits_and_wake_if(server, credits, 0);
4075 rdata->tailsz = PAGE_SIZE;
4078 rdata->server = server;
4079 rdata->cfile = cifsFileInfo_get(open_file);
4080 rdata->nr_pages = npages;
4081 rdata->offset = offset;
4082 rdata->bytes = cur_len;
4084 rdata->pagesz = PAGE_SIZE;
4085 rdata->read_into_pages = cifs_uncached_read_into_pages;
4086 rdata->copy_into_pages = cifs_uncached_copy_into_pages;
4087 rdata->credits = credits_on_stack;
4089 kref_get(&ctx->refcount);
4091 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4094 if (rdata->cfile->invalidHandle)
4097 rc = server->ops->async_readv(rdata);
4101 add_credits_and_wake_if(server, &rdata->credits, 0);
4102 kref_put(&rdata->refcount,
4103 cifs_uncached_readdata_release);
4104 if (rc == -EAGAIN) {
4105 iov_iter_revert(&direct_iov, cur_len);
4111 list_add_tail(&rdata->list, rdata_list);
4120 collect_uncached_read_data(struct cifs_aio_ctx *ctx)
4122 struct cifs_readdata *rdata, *tmp;
4123 struct iov_iter *to = &ctx->iter;
4124 struct cifs_sb_info *cifs_sb;
4127 cifs_sb = CIFS_SB(ctx->cfile->dentry->d_sb);
4129 mutex_lock(&ctx->aio_mutex);
4131 if (list_empty(&ctx->list)) {
4132 mutex_unlock(&ctx->aio_mutex);
4137 /* the loop below should proceed in the order of increasing offsets */
4139 list_for_each_entry_safe(rdata, tmp, &ctx->list, list) {
4141 if (!try_wait_for_completion(&rdata->done)) {
4142 mutex_unlock(&ctx->aio_mutex);
4146 if (rdata->result == -EAGAIN) {
4147 /* resend call if it's a retryable error */
4148 struct list_head tmp_list;
4149 unsigned int got_bytes = rdata->got_bytes;
4151 list_del_init(&rdata->list);
4152 INIT_LIST_HEAD(&tmp_list);
4155 * Got a part of data and then reconnect has
4156 * happened -- fill the buffer and continue
4159 if (got_bytes && got_bytes < rdata->bytes) {
4161 if (!ctx->direct_io)
4162 rc = cifs_readdata_to_iov(rdata, to);
4164 kref_put(&rdata->refcount,
4165 cifs_uncached_readdata_release);
4170 if (ctx->direct_io) {
4172 * Re-use rdata as this is a
4175 rc = cifs_resend_rdata(
4179 rc = cifs_send_async_read(
4180 rdata->offset + got_bytes,
4181 rdata->bytes - got_bytes,
4182 rdata->cfile, cifs_sb,
4185 kref_put(&rdata->refcount,
4186 cifs_uncached_readdata_release);
4189 list_splice(&tmp_list, &ctx->list);
4192 } else if (rdata->result)
4194 else if (!ctx->direct_io)
4195 rc = cifs_readdata_to_iov(rdata, to);
4197 /* if there was a short read -- discard anything left */
4198 if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
4201 ctx->total_len += rdata->got_bytes;
4203 list_del_init(&rdata->list);
4204 kref_put(&rdata->refcount, cifs_uncached_readdata_release);
4207 if (!ctx->direct_io)
4208 ctx->total_len = ctx->len - iov_iter_count(to);
4210 /* mask nodata case */
4214 ctx->rc = (rc == 0) ? (ssize_t)ctx->total_len : rc;
4216 mutex_unlock(&ctx->aio_mutex);
4218 if (ctx->iocb && ctx->iocb->ki_complete)
4219 ctx->iocb->ki_complete(ctx->iocb, ctx->rc, 0);
4221 complete(&ctx->done);
4224 static ssize_t __cifs_readv(
4225 struct kiocb *iocb, struct iov_iter *to, bool direct)
4228 struct file *file = iocb->ki_filp;
4229 struct cifs_sb_info *cifs_sb;
4230 struct cifsFileInfo *cfile;
4231 struct cifs_tcon *tcon;
4232 ssize_t rc, total_read = 0;
4233 loff_t offset = iocb->ki_pos;
4234 struct cifs_aio_ctx *ctx;
4237 * iov_iter_get_pages_alloc() doesn't work with ITER_KVEC,
4238 * fall back to data copy read path
4239 * this could be improved by getting pages directly in ITER_KVEC
4241 if (direct && iov_iter_is_kvec(to)) {
4242 cifs_dbg(FYI, "use non-direct cifs_user_readv for kvec I/O\n");
4246 len = iov_iter_count(to);
4250 cifs_sb = CIFS_FILE_SB(file);
4251 cfile = file->private_data;
4252 tcon = tlink_tcon(cfile->tlink);
4254 if (!tcon->ses->server->ops->async_readv)
4257 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4258 cifs_dbg(FYI, "attempting read on write only file instance\n");
4260 ctx = cifs_aio_ctx_alloc();
4264 ctx->cfile = cifsFileInfo_get(cfile);
4266 if (!is_sync_kiocb(iocb))
4269 if (iter_is_iovec(to))
4270 ctx->should_dirty = true;
4274 ctx->direct_io = true;
4278 rc = setup_aio_ctx_iter(ctx, to, READ);
4280 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4287 rc = filemap_write_and_wait_range(file->f_inode->i_mapping,
4288 offset, offset + len - 1);
4290 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4295 /* grab a lock here due to read response handlers can access ctx */
4296 mutex_lock(&ctx->aio_mutex);
4298 rc = cifs_send_async_read(offset, len, cfile, cifs_sb, &ctx->list, ctx);
4300 /* if at least one read request send succeeded, then reset rc */
4301 if (!list_empty(&ctx->list))
4304 mutex_unlock(&ctx->aio_mutex);
4307 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4311 if (!is_sync_kiocb(iocb)) {
4312 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4313 return -EIOCBQUEUED;
4316 rc = wait_for_completion_killable(&ctx->done);
4318 mutex_lock(&ctx->aio_mutex);
4319 ctx->rc = rc = -EINTR;
4320 total_read = ctx->total_len;
4321 mutex_unlock(&ctx->aio_mutex);
4324 total_read = ctx->total_len;
4327 kref_put(&ctx->refcount, cifs_aio_ctx_release);
4330 iocb->ki_pos += total_read;
4336 ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to)
4338 return __cifs_readv(iocb, to, true);
4341 ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
4343 return __cifs_readv(iocb, to, false);
4347 cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to)
4349 struct inode *inode = file_inode(iocb->ki_filp);
4350 struct cifsInodeInfo *cinode = CIFS_I(inode);
4351 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4352 struct cifsFileInfo *cfile = (struct cifsFileInfo *)
4353 iocb->ki_filp->private_data;
4354 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4358 * In strict cache mode we need to read from the server all the time
4359 * if we don't have level II oplock because the server can delay mtime
4360 * change - so we can't make a decision about inode invalidating.
4361 * And we can also fail with pagereading if there are mandatory locks
4362 * on pages affected by this read but not on the region from pos to
4365 if (!CIFS_CACHE_READ(cinode))
4366 return cifs_user_readv(iocb, to);
4368 if (cap_unix(tcon->ses) &&
4369 (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
4370 ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
4371 return generic_file_read_iter(iocb, to);
4374 * We need to hold the sem to be sure nobody modifies lock list
4375 * with a brlock that prevents reading.
4377 down_read(&cinode->lock_sem);
4378 if (!cifs_find_lock_conflict(cfile, iocb->ki_pos, iov_iter_count(to),
4379 tcon->ses->server->vals->shared_lock_type,
4380 0, NULL, CIFS_READ_OP))
4381 rc = generic_file_read_iter(iocb, to);
4382 up_read(&cinode->lock_sem);
4387 cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
4390 unsigned int bytes_read = 0;
4391 unsigned int total_read;
4392 unsigned int current_read_size;
4394 struct cifs_sb_info *cifs_sb;
4395 struct cifs_tcon *tcon;
4396 struct TCP_Server_Info *server;
4399 struct cifsFileInfo *open_file;
4400 struct cifs_io_parms io_parms = {0};
4401 int buf_type = CIFS_NO_BUFFER;
4405 cifs_sb = CIFS_FILE_SB(file);
4407 /* FIXME: set up handlers for larger reads and/or convert to async */
4408 rsize = min_t(unsigned int, cifs_sb->ctx->rsize, CIFSMaxBufSize);
4410 if (file->private_data == NULL) {
4415 open_file = file->private_data;
4416 tcon = tlink_tcon(open_file->tlink);
4417 server = cifs_pick_channel(tcon->ses);
4419 if (!server->ops->sync_read) {
4424 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4425 pid = open_file->pid;
4427 pid = current->tgid;
4429 if ((file->f_flags & O_ACCMODE) == O_WRONLY)
4430 cifs_dbg(FYI, "attempting read on write only file instance\n");
4432 for (total_read = 0, cur_offset = read_data; read_size > total_read;
4433 total_read += bytes_read, cur_offset += bytes_read) {
4435 current_read_size = min_t(uint, read_size - total_read,
4438 * For windows me and 9x we do not want to request more
4439 * than it negotiated since it will refuse the read
4442 if (!(tcon->ses->capabilities &
4443 tcon->ses->server->vals->cap_large_files)) {
4444 current_read_size = min_t(uint,
4445 current_read_size, CIFSMaxBufSize);
4447 if (open_file->invalidHandle) {
4448 rc = cifs_reopen_file(open_file, true);
4453 io_parms.tcon = tcon;
4454 io_parms.offset = *offset;
4455 io_parms.length = current_read_size;
4456 io_parms.server = server;
4457 rc = server->ops->sync_read(xid, &open_file->fid, &io_parms,
4458 &bytes_read, &cur_offset,
4460 } while (rc == -EAGAIN);
4462 if (rc || (bytes_read == 0)) {
4470 cifs_stats_bytes_read(tcon, total_read);
4471 *offset += bytes_read;
4479 * If the page is mmap'ed into a process' page tables, then we need to make
4480 * sure that it doesn't change while being written back.
4483 cifs_page_mkwrite(struct vm_fault *vmf)
4485 struct page *page = vmf->page;
4486 struct file *file = vmf->vma->vm_file;
4487 struct inode *inode = file_inode(file);
4489 cifs_fscache_wait_on_page_write(inode, page);
4492 return VM_FAULT_LOCKED;
4495 static const struct vm_operations_struct cifs_file_vm_ops = {
4496 .fault = filemap_fault,
4497 .map_pages = filemap_map_pages,
4498 .page_mkwrite = cifs_page_mkwrite,
4501 int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
4504 struct inode *inode = file_inode(file);
4508 if (!CIFS_CACHE_READ(CIFS_I(inode)))
4509 rc = cifs_zap_mapping(inode);
4511 rc = generic_file_mmap(file, vma);
4513 vma->vm_ops = &cifs_file_vm_ops;
4519 int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
4525 rc = cifs_revalidate_file(file);
4527 cifs_dbg(FYI, "Validation prior to mmap failed, error=%d\n",
4530 rc = generic_file_mmap(file, vma);
4532 vma->vm_ops = &cifs_file_vm_ops;
4539 cifs_readv_complete(struct work_struct *work)
4541 unsigned int i, got_bytes;
4542 struct cifs_readdata *rdata = container_of(work,
4543 struct cifs_readdata, work);
4545 got_bytes = rdata->got_bytes;
4546 for (i = 0; i < rdata->nr_pages; i++) {
4547 struct page *page = rdata->pages[i];
4549 lru_cache_add(page);
4551 if (rdata->result == 0 ||
4552 (rdata->result == -EAGAIN && got_bytes)) {
4553 flush_dcache_page(page);
4554 SetPageUptodate(page);
4560 if (rdata->result == 0 ||
4561 (rdata->result == -EAGAIN && got_bytes))
4562 cifs_readpage_to_fscache(rdata->mapping->host, page);
4564 cifs_fscache_uncache_page(rdata->mapping->host, page);
4566 got_bytes -= min_t(unsigned int, PAGE_SIZE, got_bytes);
4569 rdata->pages[i] = NULL;
4571 kref_put(&rdata->refcount, cifs_readdata_release);
4575 readpages_fill_pages(struct TCP_Server_Info *server,
4576 struct cifs_readdata *rdata, struct iov_iter *iter,
4583 unsigned int nr_pages = rdata->nr_pages;
4584 unsigned int page_offset = rdata->page_offset;
4586 /* determine the eof that the server (probably) has */
4587 eof = CIFS_I(rdata->mapping->host)->server_eof;
4588 eof_index = eof ? (eof - 1) >> PAGE_SHIFT : 0;
4589 cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
4591 rdata->got_bytes = 0;
4592 rdata->tailsz = PAGE_SIZE;
4593 for (i = 0; i < nr_pages; i++) {
4594 struct page *page = rdata->pages[i];
4595 unsigned int to_read = rdata->pagesz;
4599 to_read -= page_offset;
4605 if (len >= to_read) {
4607 } else if (len > 0) {
4608 /* enough for partial page, fill and zero the rest */
4609 zero_user(page, len + page_offset, to_read - len);
4610 n = rdata->tailsz = len;
4612 } else if (page->index > eof_index) {
4614 * The VFS will not try to do readahead past the
4615 * i_size, but it's possible that we have outstanding
4616 * writes with gaps in the middle and the i_size hasn't
4617 * caught up yet. Populate those with zeroed out pages
4618 * to prevent the VFS from repeatedly attempting to
4619 * fill them until the writes are flushed.
4621 zero_user(page, 0, PAGE_SIZE);
4622 lru_cache_add(page);
4623 flush_dcache_page(page);
4624 SetPageUptodate(page);
4627 rdata->pages[i] = NULL;
4631 /* no need to hold page hostage */
4632 lru_cache_add(page);
4635 rdata->pages[i] = NULL;
4641 result = copy_page_from_iter(
4642 page, page_offset, n, iter);
4643 #ifdef CONFIG_CIFS_SMB_DIRECT
4648 result = cifs_read_page_from_socket(
4649 server, page, page_offset, n);
4653 rdata->got_bytes += result;
4656 return result != -ECONNABORTED && rdata->got_bytes > 0 ?
4657 rdata->got_bytes : result;
4661 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
4662 struct cifs_readdata *rdata, unsigned int len)
4664 return readpages_fill_pages(server, rdata, NULL, len);
4668 cifs_readpages_copy_into_pages(struct TCP_Server_Info *server,
4669 struct cifs_readdata *rdata,
4670 struct iov_iter *iter)
4672 return readpages_fill_pages(server, rdata, iter, iter->count);
4676 readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
4677 unsigned int rsize, struct list_head *tmplist,
4678 unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
4680 struct page *page, *tpage;
4681 unsigned int expected_index;
4683 gfp_t gfp = readahead_gfp_mask(mapping);
4685 INIT_LIST_HEAD(tmplist);
4687 page = lru_to_page(page_list);
4690 * Lock the page and put it in the cache. Since no one else
4691 * should have access to this page, we're safe to simply set
4692 * PG_locked without checking it first.
4694 __SetPageLocked(page);
4695 rc = add_to_page_cache_locked(page, mapping,
4698 /* give up if we can't stick it in the cache */
4700 __ClearPageLocked(page);
4704 /* move first page to the tmplist */
4705 *offset = (loff_t)page->index << PAGE_SHIFT;
4708 list_move_tail(&page->lru, tmplist);
4710 /* now try and add more pages onto the request */
4711 expected_index = page->index + 1;
4712 list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
4713 /* discontinuity ? */
4714 if (page->index != expected_index)
4717 /* would this page push the read over the rsize? */
4718 if (*bytes + PAGE_SIZE > rsize)
4721 __SetPageLocked(page);
4722 rc = add_to_page_cache_locked(page, mapping, page->index, gfp);
4724 __ClearPageLocked(page);
4727 list_move_tail(&page->lru, tmplist);
4728 (*bytes) += PAGE_SIZE;
4735 static int cifs_readpages(struct file *file, struct address_space *mapping,
4736 struct list_head *page_list, unsigned num_pages)
4740 struct list_head tmplist;
4741 struct cifsFileInfo *open_file = file->private_data;
4742 struct cifs_sb_info *cifs_sb = CIFS_FILE_SB(file);
4743 struct TCP_Server_Info *server;
4749 * Reads as many pages as possible from fscache. Returns -ENOBUFS
4750 * immediately if the cookie is negative
4752 * After this point, every page in the list might have PG_fscache set,
4753 * so we will need to clean that up off of every page we don't use.
4755 rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
4762 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
4763 pid = open_file->pid;
4765 pid = current->tgid;
4768 server = cifs_pick_channel(tlink_tcon(open_file->tlink)->ses);
4770 cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
4771 __func__, file, mapping, num_pages);
4774 * Start with the page at end of list and move it to private
4775 * list. Do the same with any following pages until we hit
4776 * the rsize limit, hit an index discontinuity, or run out of
4777 * pages. Issue the async read and then start the loop again
4778 * until the list is empty.
4780 * Note that list order is important. The page_list is in
4781 * the order of declining indexes. When we put the pages in
4782 * the rdata->pages, then we want them in increasing order.
4784 while (!list_empty(page_list) && !err) {
4785 unsigned int i, nr_pages, bytes, rsize;
4787 struct page *page, *tpage;
4788 struct cifs_readdata *rdata;
4789 struct cifs_credits credits_on_stack;
4790 struct cifs_credits *credits = &credits_on_stack;
4792 if (open_file->invalidHandle) {
4793 rc = cifs_reopen_file(open_file, true);
4800 if (cifs_sb->ctx->rsize == 0)
4801 cifs_sb->ctx->rsize =
4802 server->ops->negotiate_rsize(tlink_tcon(open_file->tlink),
4805 rc = server->ops->wait_mtu_credits(server, cifs_sb->ctx->rsize,
4811 * Give up immediately if rsize is too small to read an entire
4812 * page. The VFS will fall back to readpage. We should never
4813 * reach this point however since we set ra_pages to 0 when the
4814 * rsize is smaller than a cache page.
4816 if (unlikely(rsize < PAGE_SIZE)) {
4817 add_credits_and_wake_if(server, credits, 0);
4823 err = readpages_get_pages(mapping, page_list, rsize, &tmplist,
4824 &nr_pages, &offset, &bytes);
4826 add_credits_and_wake_if(server, credits, 0);
4830 rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
4832 /* best to give up if we're out of mem */
4833 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4834 list_del(&page->lru);
4835 lru_cache_add(page);
4840 add_credits_and_wake_if(server, credits, 0);
4844 rdata->cfile = cifsFileInfo_get(open_file);
4845 rdata->server = server;
4846 rdata->mapping = mapping;
4847 rdata->offset = offset;
4848 rdata->bytes = bytes;
4850 rdata->pagesz = PAGE_SIZE;
4851 rdata->tailsz = PAGE_SIZE;
4852 rdata->read_into_pages = cifs_readpages_read_into_pages;
4853 rdata->copy_into_pages = cifs_readpages_copy_into_pages;
4854 rdata->credits = credits_on_stack;
4856 list_for_each_entry_safe(page, tpage, &tmplist, lru) {
4857 list_del(&page->lru);
4858 rdata->pages[rdata->nr_pages++] = page;
4861 rc = adjust_credits(server, &rdata->credits, rdata->bytes);
4864 if (rdata->cfile->invalidHandle)
4867 rc = server->ops->async_readv(rdata);
4871 add_credits_and_wake_if(server, &rdata->credits, 0);
4872 for (i = 0; i < rdata->nr_pages; i++) {
4873 page = rdata->pages[i];
4874 lru_cache_add(page);
4878 /* Fallback to the readpage in error/reconnect cases */
4879 kref_put(&rdata->refcount, cifs_readdata_release);
4883 kref_put(&rdata->refcount, cifs_readdata_release);
4886 /* Any pages that have been shown to fscache but didn't get added to
4887 * the pagecache must be uncached before they get returned to the
4890 cifs_fscache_readpages_cancel(mapping->host, page_list);
4896 * cifs_readpage_worker must be called with the page pinned
4898 static int cifs_readpage_worker(struct file *file, struct page *page,
4904 /* Is the page cached? */
4905 rc = cifs_readpage_from_fscache(file_inode(file), page);
4909 read_data = kmap(page);
4910 /* for reads over a certain size could initiate async read ahead */
4912 rc = cifs_read(file, read_data, PAGE_SIZE, poffset);
4917 cifs_dbg(FYI, "Bytes read %d\n", rc);
4919 /* we do not want atime to be less than mtime, it broke some apps */
4920 file_inode(file)->i_atime = current_time(file_inode(file));
4921 if (timespec64_compare(&(file_inode(file)->i_atime), &(file_inode(file)->i_mtime)) < 0)
4922 file_inode(file)->i_atime = file_inode(file)->i_mtime;
4924 file_inode(file)->i_atime = current_time(file_inode(file));
4927 memset(read_data + rc, 0, PAGE_SIZE - rc);
4929 flush_dcache_page(page);
4930 SetPageUptodate(page);
4932 /* send this page to the cache */
4933 cifs_readpage_to_fscache(file_inode(file), page);
4945 static int cifs_readpage(struct file *file, struct page *page)
4947 loff_t offset = page_file_offset(page);
4953 if (file->private_data == NULL) {
4959 cifs_dbg(FYI, "readpage %p at offset %d 0x%x\n",
4960 page, (int)offset, (int)offset);
4962 rc = cifs_readpage_worker(file, page, &offset);
4968 static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
4970 struct cifsFileInfo *open_file;
4972 spin_lock(&cifs_inode->open_file_lock);
4973 list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
4974 if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
4975 spin_unlock(&cifs_inode->open_file_lock);
4979 spin_unlock(&cifs_inode->open_file_lock);
4983 /* We do not want to update the file size from server for inodes
4984 open for write - to avoid races with writepage extending
4985 the file - in the future we could consider allowing
4986 refreshing the inode only on increases in the file size
4987 but this is tricky to do without racing with writebehind
4988 page caching in the current Linux kernel design */
4989 bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
4994 if (is_inode_writable(cifsInode)) {
4995 /* This inode is open for write at least once */
4996 struct cifs_sb_info *cifs_sb;
4998 cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
4999 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
5000 /* since no page cache to corrupt on directio
5001 we can change size safely */
5005 if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
5013 static int cifs_write_begin(struct file *file, struct address_space *mapping,
5014 loff_t pos, unsigned len, unsigned flags,
5015 struct page **pagep, void **fsdata)
5018 pgoff_t index = pos >> PAGE_SHIFT;
5019 loff_t offset = pos & (PAGE_SIZE - 1);
5020 loff_t page_start = pos & PAGE_MASK;
5025 cifs_dbg(FYI, "write_begin from %lld len %d\n", (long long)pos, len);
5028 page = grab_cache_page_write_begin(mapping, index, flags);
5034 if (PageUptodate(page))
5038 * If we write a full page it will be up to date, no need to read from
5039 * the server. If the write is short, we'll end up doing a sync write
5042 if (len == PAGE_SIZE)
5046 * optimize away the read when we have an oplock, and we're not
5047 * expecting to use any of the data we'd be reading in. That
5048 * is, when the page lies beyond the EOF, or straddles the EOF
5049 * and the write will cover all of the existing data.
5051 if (CIFS_CACHE_READ(CIFS_I(mapping->host))) {
5052 i_size = i_size_read(mapping->host);
5053 if (page_start >= i_size ||
5054 (offset == 0 && (pos + len) >= i_size)) {
5055 zero_user_segments(page, 0, offset,
5059 * PageChecked means that the parts of the page
5060 * to which we're not writing are considered up
5061 * to date. Once the data is copied to the
5062 * page, it can be set uptodate.
5064 SetPageChecked(page);
5069 if ((file->f_flags & O_ACCMODE) != O_WRONLY && !oncethru) {
5071 * might as well read a page, it is fast enough. If we get
5072 * an error, we don't need to return it. cifs_write_end will
5073 * do a sync write instead since PG_uptodate isn't set.
5075 cifs_readpage_worker(file, page, &page_start);
5080 /* we could try using another file handle if there is one -
5081 but how would we lock it to prevent close of that handle
5082 racing with this read? In any case
5083 this will be written out by write_end so is fine */
5090 static int cifs_release_page(struct page *page, gfp_t gfp)
5092 if (PagePrivate(page))
5095 return cifs_fscache_release_page(page, gfp);
5098 static void cifs_invalidate_page(struct page *page, unsigned int offset,
5099 unsigned int length)
5101 struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
5103 if (offset == 0 && length == PAGE_SIZE)
5104 cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
5107 static int cifs_launder_page(struct page *page)
5110 loff_t range_start = page_offset(page);
5111 loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
5112 struct writeback_control wbc = {
5113 .sync_mode = WB_SYNC_ALL,
5115 .range_start = range_start,
5116 .range_end = range_end,
5119 cifs_dbg(FYI, "Launder page: %p\n", page);
5121 if (clear_page_dirty_for_io(page))
5122 rc = cifs_writepage_locked(page, &wbc);
5124 cifs_fscache_invalidate_page(page, page->mapping->host);
5128 void cifs_oplock_break(struct work_struct *work)
5130 struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
5132 struct inode *inode = d_inode(cfile->dentry);
5133 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5134 struct cifsInodeInfo *cinode = CIFS_I(inode);
5135 struct cifs_tcon *tcon;
5136 struct TCP_Server_Info *server;
5137 struct tcon_link *tlink;
5139 bool purge_cache = false, oplock_break_cancelled;
5140 __u64 persistent_fid, volatile_fid;
5143 wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
5144 TASK_UNINTERRUPTIBLE);
5146 tlink = cifs_sb_tlink(cifs_sb);
5149 tcon = tlink_tcon(tlink);
5150 server = tcon->ses->server;
5152 server->ops->downgrade_oplock(server, cinode, cfile->oplock_level,
5153 cfile->oplock_epoch, &purge_cache);
5155 if (!CIFS_CACHE_WRITE(cinode) && CIFS_CACHE_READ(cinode) &&
5156 cifs_has_mand_locks(cinode)) {
5157 cifs_dbg(FYI, "Reset oplock to None for inode=%p due to mand locks\n",
5162 if (inode && S_ISREG(inode->i_mode)) {
5163 if (CIFS_CACHE_READ(cinode))
5164 break_lease(inode, O_RDONLY);
5166 break_lease(inode, O_WRONLY);
5167 rc = filemap_fdatawrite(inode->i_mapping);
5168 if (!CIFS_CACHE_READ(cinode) || purge_cache) {
5169 rc = filemap_fdatawait(inode->i_mapping);
5170 mapping_set_error(inode->i_mapping, rc);
5171 cifs_zap_mapping(inode);
5173 cifs_dbg(FYI, "Oplock flush inode %p rc %d\n", inode, rc);
5174 if (CIFS_CACHE_WRITE(cinode))
5175 goto oplock_break_ack;
5178 rc = cifs_push_locks(cfile);
5180 cifs_dbg(VFS, "Push locks rc = %d\n", rc);
5184 * When oplock break is received and there are no active
5185 * file handles but cached, then schedule deferred close immediately.
5186 * So, new open will not use cached handle.
5189 if (!CIFS_CACHE_HANDLE(cinode) && !list_empty(&cinode->deferred_closes))
5190 cifs_close_deferred_file(cinode);
5192 persistent_fid = cfile->fid.persistent_fid;
5193 volatile_fid = cfile->fid.volatile_fid;
5194 net_fid = cfile->fid.netfid;
5195 oplock_break_cancelled = cfile->oplock_break_cancelled;
5197 _cifsFileInfo_put(cfile, false /* do not wait for ourself */, false);
5199 * MS-SMB2 3.2.5.19.1 and 3.2.5.19.2 (and MS-CIFS 3.2.5.42) do not require
5200 * an acknowledgment to be sent when the file has already been closed.
5202 spin_lock(&cinode->open_file_lock);
5203 /* check list empty since can race with kill_sb calling tree disconnect */
5204 if (!oplock_break_cancelled && !list_empty(&cinode->openFileList)) {
5205 spin_unlock(&cinode->open_file_lock);
5206 rc = server->ops->oplock_response(tcon, persistent_fid,
5207 volatile_fid, net_fid, cinode);
5208 cifs_dbg(FYI, "Oplock release rc = %d\n", rc);
5210 spin_unlock(&cinode->open_file_lock);
5212 cifs_put_tlink(tlink);
5214 cifs_done_oplock_break(cinode);
5218 * The presence of cifs_direct_io() in the address space ops vector
5219 * allowes open() O_DIRECT flags which would have failed otherwise.
5221 * In the non-cached mode (mount with cache=none), we shunt off direct read and write requests
5222 * so this method should never be called.
5224 * Direct IO is not yet supported in the cached mode.
5227 cifs_direct_io(struct kiocb *iocb, struct iov_iter *iter)
5231 * Eventually need to support direct IO for non forcedirectio mounts
5236 static int cifs_swap_activate(struct swap_info_struct *sis,
5237 struct file *swap_file, sector_t *span)
5239 struct cifsFileInfo *cfile = swap_file->private_data;
5240 struct inode *inode = swap_file->f_mapping->host;
5241 unsigned long blocks;
5244 cifs_dbg(FYI, "swap activate\n");
5246 spin_lock(&inode->i_lock);
5247 blocks = inode->i_blocks;
5248 isize = inode->i_size;
5249 spin_unlock(&inode->i_lock);
5250 if (blocks*512 < isize) {
5251 pr_warn("swap activate: swapfile has holes\n");
5256 pr_warn_once("Swap support over SMB3 is experimental\n");
5259 * TODO: consider adding ACL (or documenting how) to prevent other
5260 * users (on this or other systems) from reading it
5264 /* TODO: add sk_set_memalloc(inet) or similar */
5267 cfile->swapfile = true;
5269 * TODO: Since file already open, we can't open with DENY_ALL here
5270 * but we could add call to grab a byte range lock to prevent others
5271 * from reading or writing the file
5277 static void cifs_swap_deactivate(struct file *file)
5279 struct cifsFileInfo *cfile = file->private_data;
5281 cifs_dbg(FYI, "swap deactivate\n");
5283 /* TODO: undo sk_set_memalloc(inet) will eventually be needed */
5286 cfile->swapfile = false;
5288 /* do we need to unpin (or unlock) the file */
5291 const struct address_space_operations cifs_addr_ops = {
5292 .readpage = cifs_readpage,
5293 .readpages = cifs_readpages,
5294 .writepage = cifs_writepage,
5295 .writepages = cifs_writepages,
5296 .write_begin = cifs_write_begin,
5297 .write_end = cifs_write_end,
5298 .set_page_dirty = __set_page_dirty_nobuffers,
5299 .releasepage = cifs_release_page,
5300 .direct_IO = cifs_direct_io,
5301 .invalidatepage = cifs_invalidate_page,
5302 .launder_page = cifs_launder_page,
5304 * TODO: investigate and if useful we could add an cifs_migratePage
5305 * helper (under an CONFIG_MIGRATION) in the future, and also
5306 * investigate and add an is_dirty_writeback helper if needed
5308 .swap_activate = cifs_swap_activate,
5309 .swap_deactivate = cifs_swap_deactivate,
5313 * cifs_readpages requires the server to support a buffer large enough to
5314 * contain the header plus one complete page of data. Otherwise, we need
5315 * to leave cifs_readpages out of the address space operations.
5317 const struct address_space_operations cifs_addr_ops_smallbuf = {
5318 .readpage = cifs_readpage,
5319 .writepage = cifs_writepage,
5320 .writepages = cifs_writepages,
5321 .write_begin = cifs_write_begin,
5322 .write_end = cifs_write_end,
5323 .set_page_dirty = __set_page_dirty_nobuffers,
5324 .releasepage = cifs_release_page,
5325 .invalidatepage = cifs_invalidate_page,
5326 .launder_page = cifs_launder_page,